Debian Codegraph @

servers/slapd/back-sql/modrdn.c

raw
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1999-2024 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Masarati.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by Dmitry Kovalev for inclusion
19 * by OpenLDAP Software. Additional significant contributors include
20 * Pierangelo Masarati.
21 */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include "ac/string.h"
28
29 #include "slap.h"
30 #include "proto-sql.h"
31
32 int
33 backsql_modrdn( Operation *op, SlapReply *rs )
34 {
35 backsql_info *bi = (backsql_info*)op->o_bd->be_private;
36 SQLHDBC dbh = SQL_NULL_HDBC;
37 SQLHSTMT sth = SQL_NULL_HSTMT;
38 RETCODE rc;
39 backsql_entryID e_id = BACKSQL_ENTRYID_INIT,
40 n_id = BACKSQL_ENTRYID_INIT;
41 backsql_srch_info bsi = { 0 };
42 backsql_oc_map_rec *oc = NULL;
43 struct berval pdn = BER_BVNULL, pndn = BER_BVNULL,
44 *new_pdn = NULL, *new_npdn = NULL,
45 realnew_dn = BER_BVNULL;
46 Entry r = { 0 },
47 p = { 0 },
48 n = { 0 },
49 *e = NULL;
50 int manageDSAit = get_manageDSAit( op );
51 struct berval *newSuperior = op->oq_modrdn.rs_newSup;
52
53 Debug( LDAP_DEBUG_TRACE, "==>backsql_modrdn() renaming entry \"%s\", "
54 "newrdn=\"%s\", newSuperior=\"%s\"\n",
55 op->o_req_dn.bv_val, op->oq_modrdn.rs_newrdn.bv_val,
56 newSuperior ? newSuperior->bv_val : "(NULL)" );
57
58 rs->sr_err = backsql_get_db_conn( op, &dbh );
59 if ( rs->sr_err != LDAP_SUCCESS ) {
60 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
61 "could not get connection handle - exiting\n" );
62 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
63 ? "SQL-backend error" : NULL;
64 e = NULL;
65 goto done;
66 }
67
68 bsi.bsi_e = &r;
69 rs->sr_err = backsql_init_search( &bsi, &op->o_req_ndn,
70 LDAP_SCOPE_BASE,
71 (time_t)(-1), NULL, dbh, op, rs,
72 slap_anlist_all_attributes,
73 ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY | BACKSQL_ISF_GET_OC ) );
74 switch ( rs->sr_err ) {
75 case LDAP_SUCCESS:
76 break;
77
78 case LDAP_REFERRAL:
79 if ( manageDSAit && !BER_BVISNULL( &bsi.bsi_e->e_nname ) &&
80 dn_match( &op->o_req_ndn, &bsi.bsi_e->e_nname ) )
81 {
82 rs->sr_err = LDAP_SUCCESS;
83 rs->sr_text = NULL;
84 rs->sr_matched = NULL;
85 if ( rs->sr_ref ) {
86 ber_bvarray_free( rs->sr_ref );
87 rs->sr_ref = NULL;
88 }
89 break;
90 }
91 e = &r;
92 /* fallthru */
93
94 default:
95 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
96 "could not retrieve modrdnDN ID - no such entry\n" );
97 if ( !BER_BVISNULL( &r.e_nname ) ) {
98 /* FIXME: should always be true! */
99 e = &r;
100
101 } else {
102 e = NULL;
103 }
104 goto done;
105 }
106
107 Debug( LDAP_DEBUG_TRACE,
108 " backsql_modrdn(): entry id=" BACKSQL_IDFMT "\n",
109 BACKSQL_IDARG(e_id.eid_id) );
110
111 if ( get_assert( op ) &&
112 ( test_filter( op, &r, get_assertion( op ) )
113 != LDAP_COMPARE_TRUE ) )
114 {
115 rs->sr_err = LDAP_ASSERTION_FAILED;
116 e = &r;
117 goto done;
118 }
119
120 if ( backsql_has_children( op, dbh, &op->o_req_ndn ) == LDAP_COMPARE_TRUE ) {
121 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
122 "entry \"%s\" has children\n",
123 op->o_req_dn.bv_val );
124 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
125 rs->sr_text = "subtree rename not supported";
126 e = &r;
127 goto done;
128 }
129
130 /*
131 * Check for entry access to target
132 */
133 if ( !access_allowed( op, &r, slap_schema.si_ad_entry,
134 NULL, ACL_WRITE, NULL ) ) {
135 Debug( LDAP_DEBUG_TRACE, " no access to entry\n" );
136 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
137 goto done;
138 }
139
140 dnParent( &op->o_req_dn, &pdn );
141 dnParent( &op->o_req_ndn, &pndn );
142
143 /*
144 * namingContext "" is not supported
145 */
146 if ( BER_BVISEMPTY( &pdn ) ) {
147 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
148 "parent is \"\" - aborting\n" );
149 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
150 rs->sr_text = "not allowed within namingContext";
151 e = NULL;
152 goto done;
153 }
154
155 /*
156 * Check for children access to parent
157 */
158 bsi.bsi_e = &p;
159 e_id = bsi.bsi_base_id;
160 memset( &bsi.bsi_base_id, 0, sizeof( bsi.bsi_base_id ) );
161 rs->sr_err = backsql_init_search( &bsi, &pndn,
162 LDAP_SCOPE_BASE,
163 (time_t)(-1), NULL, dbh, op, rs,
164 slap_anlist_all_attributes,
165 BACKSQL_ISF_GET_ENTRY );
166
167 Debug( LDAP_DEBUG_TRACE,
168 " backsql_modrdn(): old parent entry id is " BACKSQL_IDFMT "\n",
169 BACKSQL_IDARG(bsi.bsi_base_id.eid_id) );
170
171 if ( rs->sr_err != LDAP_SUCCESS ) {
172 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
173 "could not retrieve renameDN ID - no such entry\n" );
174 e = &p;
175 goto done;
176 }
177
178 if ( !access_allowed( op, &p, slap_schema.si_ad_children, NULL,
179 newSuperior ? ACL_WDEL : ACL_WRITE, NULL ) )
180 {
181 Debug( LDAP_DEBUG_TRACE, " no access to parent\n" );
182 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
183 goto done;
184 }
185
186 if ( newSuperior ) {
187 (void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
188
189 /*
190 * namingContext "" is not supported
191 */
192 if ( BER_BVISEMPTY( newSuperior ) ) {
193 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
194 "newSuperior is \"\" - aborting\n" );
195 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
196 rs->sr_text = "not allowed within namingContext";
197 e = NULL;
198 goto done;
199 }
200
201 new_pdn = newSuperior;
202 new_npdn = op->oq_modrdn.rs_nnewSup;
203
204 /*
205 * Check for children access to new parent
206 */
207 bsi.bsi_e = &n;
208 rs->sr_err = backsql_init_search( &bsi, new_npdn,
209 LDAP_SCOPE_BASE,
210 (time_t)(-1), NULL, dbh, op, rs,
211 slap_anlist_all_attributes,
212 ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) );
213 if ( rs->sr_err != LDAP_SUCCESS ) {
214 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
215 "could not retrieve renameDN ID - no such entry\n" );
216 e = &n;
217 goto done;
218 }
219
220 n_id = bsi.bsi_base_id;
221
222 Debug( LDAP_DEBUG_TRACE,
223 " backsql_modrdn(): new parent entry id=" BACKSQL_IDFMT "\n",
224 BACKSQL_IDARG(n_id.eid_id) );
225
226 if ( !access_allowed( op, &n, slap_schema.si_ad_children,
227 NULL, ACL_WADD, NULL ) ) {
228 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
229 "no access to new parent \"%s\"\n",
230 new_pdn->bv_val );
231 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
232 e = &n;
233 goto done;
234 }
235
236 } else {
237 n_id = bsi.bsi_base_id;
238 new_pdn = &pdn;
239 new_npdn = &pndn;
240 }
241
242 memset( &bsi.bsi_base_id, 0, sizeof( bsi.bsi_base_id ) );
243
244 if ( newSuperior && dn_match( &pndn, new_npdn ) ) {
245 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
246 "newSuperior is equal to old parent - ignored\n" );
247 newSuperior = NULL;
248 }
249
250 if ( newSuperior && dn_match( &op->o_req_ndn, new_npdn ) ) {
251 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
252 "newSuperior is equal to entry being moved "
253 "- aborting\n" );
254 rs->sr_err = LDAP_OTHER;
255 rs->sr_text = "newSuperior is equal to old DN";
256 e = &r;
257 goto done;
258 }
259
260 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): new entry dn is \"%s\"\n",
261 op->orr_newDN.bv_val );
262
263 realnew_dn = op->orr_newDN;
264 if ( backsql_api_dn2odbc( op, rs, &realnew_dn ) ) {
265 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(\"%s\"): "
266 "backsql_api_dn2odbc(\"%s\") failed\n",
267 op->o_req_dn.bv_val, realnew_dn.bv_val );
268 SQLFreeStmt( sth, SQL_DROP );
269
270 rs->sr_text = "SQL-backend error";
271 rs->sr_err = LDAP_OTHER;
272 e = NULL;
273 goto done;
274 }
275
276 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
277 "executing renentry_stmt\n" );
278
279 rc = backsql_Prepare( dbh, &sth, bi->sql_renentry_stmt, 0 );
280 if ( rc != SQL_SUCCESS ) {
281 Debug( LDAP_DEBUG_TRACE,
282 " backsql_modrdn(): "
283 "error preparing renentry_stmt\n" );
284 backsql_PrintErrors( bi->sql_db_env, dbh,
285 sth, rc );
286
287 rs->sr_text = "SQL-backend error";
288 rs->sr_err = LDAP_OTHER;
289 e = NULL;
290 goto done;
291 }
292
293 rc = backsql_BindParamBerVal( sth, 1, SQL_PARAM_INPUT, &realnew_dn );
294 if ( rc != SQL_SUCCESS ) {
295 Debug( LDAP_DEBUG_TRACE,
296 " backsql_modrdn(): "
297 "error binding DN parameter for objectClass %s\n",
298 oc->bom_oc->soc_cname.bv_val );
299 backsql_PrintErrors( bi->sql_db_env, dbh,
300 sth, rc );
301 SQLFreeStmt( sth, SQL_DROP );
302
303 rs->sr_text = "SQL-backend error";
304 rs->sr_err = LDAP_OTHER;
305 e = NULL;
306 goto done;
307 }
308
309 rc = backsql_BindParamID( sth, 2, SQL_PARAM_INPUT, &n_id.eid_id );
310 if ( rc != SQL_SUCCESS ) {
311 Debug( LDAP_DEBUG_TRACE,
312 " backsql_modrdn(): "
313 "error binding parent ID parameter for objectClass %s\n",
314 oc->bom_oc->soc_cname.bv_val );
315 backsql_PrintErrors( bi->sql_db_env, dbh,
316 sth, rc );
317 SQLFreeStmt( sth, SQL_DROP );
318
319 rs->sr_text = "SQL-backend error";
320 rs->sr_err = LDAP_OTHER;
321 e = NULL;
322 goto done;
323 }
324
325 rc = backsql_BindParamID( sth, 3, SQL_PARAM_INPUT, &e_id.eid_keyval );
326 if ( rc != SQL_SUCCESS ) {
327 Debug( LDAP_DEBUG_TRACE,
328 " backsql_modrdn(): "
329 "error binding entry ID parameter for objectClass %s\n",
330 oc->bom_oc->soc_cname.bv_val );
331 backsql_PrintErrors( bi->sql_db_env, dbh,
332 sth, rc );
333 SQLFreeStmt( sth, SQL_DROP );
334
335 rs->sr_text = "SQL-backend error";
336 rs->sr_err = LDAP_OTHER;
337 e = NULL;
338 goto done;
339 }
340
341 rc = backsql_BindParamID( sth, 4, SQL_PARAM_INPUT, &e_id.eid_id );
342 if ( rc != SQL_SUCCESS ) {
343 Debug( LDAP_DEBUG_TRACE,
344 " backsql_modrdn(): "
345 "error binding ID parameter for objectClass %s\n",
346 oc->bom_oc->soc_cname.bv_val );
347 backsql_PrintErrors( bi->sql_db_env, dbh,
348 sth, rc );
349 SQLFreeStmt( sth, SQL_DROP );
350
351 rs->sr_text = "SQL-backend error";
352 rs->sr_err = LDAP_OTHER;
353 e = NULL;
354 goto done;
355 }
356
357 rc = SQLExecute( sth );
358 if ( rc != SQL_SUCCESS ) {
359 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(): "
360 "could not rename ldap_entries record\n" );
361 backsql_PrintErrors( bi->sql_db_env, dbh, sth, rc );
362 SQLFreeStmt( sth, SQL_DROP );
363 rs->sr_err = LDAP_OTHER;
364 rs->sr_text = "SQL-backend error";
365 e = NULL;
366 goto done;
367 }
368 SQLFreeStmt( sth, SQL_DROP );
369
370 slap_mods_opattrs( op, &op->orr_modlist, 1 );
371
372 assert( e_id.eid_oc != NULL );
373 oc = e_id.eid_oc;
374
375 if ( op->orr_modlist != NULL ) {
376 rs->sr_err = backsql_modify_internal( op, rs, dbh, oc, &e_id, op->orr_modlist );
377 slap_graduate_commit_csn( op );
378 if ( rs->sr_err != LDAP_SUCCESS ) {
379 e = &r;
380 goto done;
381 }
382 }
383
384 if ( BACKSQL_CHECK_SCHEMA( bi ) ) {
385 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
386
387 backsql_entry_clean( op, &r );
388 (void)backsql_free_entryID( &e_id, 0, op->o_tmpmemctx );
389
390 bsi.bsi_e = &r;
391 rs->sr_err = backsql_init_search( &bsi, &op->orr_nnewDN,
392 LDAP_SCOPE_BASE,
393 (time_t)(-1), NULL, dbh, op, rs,
394 slap_anlist_all_attributes,
395 ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) );
396 switch ( rs->sr_err ) {
397 case LDAP_SUCCESS:
398 break;
399
400 case LDAP_REFERRAL:
401 if ( manageDSAit && !BER_BVISNULL( &bsi.bsi_e->e_nname ) &&
402 dn_match( &op->orr_nnewDN, &bsi.bsi_e->e_nname ) )
403 {
404 rs->sr_err = LDAP_SUCCESS;
405 rs->sr_text = NULL;
406 rs->sr_matched = NULL;
407 if ( rs->sr_ref ) {
408 ber_bvarray_free( rs->sr_ref );
409 rs->sr_ref = NULL;
410 }
411 break;
412 }
413 e = &r;
414 /* fallthru */
415
416 default:
417 Debug( LDAP_DEBUG_TRACE, "backsql_modrdn(): "
418 "could not retrieve modrdnDN ID - no such entry\n" );
419 if ( !BER_BVISNULL( &r.e_nname ) ) {
420 /* FIXME: should always be true! */
421 e = &r;
422
423 } else {
424 e = NULL;
425 }
426 goto done;
427 }
428
429 e_id = bsi.bsi_base_id;
430
431 rs->sr_err = entry_schema_check( op, &r, NULL, 0, 0, NULL,
432 &rs->sr_text, textbuf, sizeof( textbuf ) );
433 if ( rs->sr_err != LDAP_SUCCESS ) {
434 Debug( LDAP_DEBUG_TRACE, " backsql_modrdn(\"%s\"): "
435 "entry failed schema check -- aborting\n",
436 r.e_name.bv_val );
437 e = NULL;
438 goto done;
439 }
440 }
441
442 done:;
443 if ( e != NULL ) {
444 if ( !access_allowed( op, e, slap_schema.si_ad_entry, NULL,
445 ACL_DISCLOSE, NULL ) )
446 {
447 rs->sr_err = LDAP_NO_SUCH_OBJECT;
448 rs->sr_text = NULL;
449 rs->sr_matched = NULL;
450 if ( rs->sr_ref ) {
451 ber_bvarray_free( rs->sr_ref );
452 rs->sr_ref = NULL;
453 }
454 }
455 }
456
457 /*
458 * Commit only if all operations succeed
459 */
460 if ( sth != SQL_NULL_HSTMT ) {
461 SQLUSMALLINT CompletionType = SQL_ROLLBACK;
462
463 if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
464 CompletionType = SQL_COMMIT;
465 }
466
467 SQLTransact( SQL_NULL_HENV, dbh, CompletionType );
468 }
469
470 if ( op->o_noop && rs->sr_err == LDAP_SUCCESS ) {
471 rs->sr_err = LDAP_X_NO_OPERATION;
472 }
473
474 send_ldap_result( op, rs );
475 slap_graduate_commit_csn( op );
476
477 if ( !BER_BVISNULL( &realnew_dn ) && realnew_dn.bv_val != op->orr_newDN.bv_val ) {
478 ch_free( realnew_dn.bv_val );
479 }
480
481 if ( !BER_BVISNULL( &e_id.eid_ndn ) ) {
482 (void)backsql_free_entryID( &e_id, 0, op->o_tmpmemctx );
483 }
484
485 if ( !BER_BVISNULL( &n_id.eid_ndn ) ) {
486 (void)backsql_free_entryID( &n_id, 0, op->o_tmpmemctx );
487 }
488
489 if ( !BER_BVISNULL( &r.e_nname ) ) {
490 backsql_entry_clean( op, &r );
491 }
492
493 if ( !BER_BVISNULL( &p.e_nname ) ) {
494 backsql_entry_clean( op, &p );
495 }
496
497 if ( !BER_BVISNULL( &n.e_nname ) ) {
498 backsql_entry_clean( op, &n );
499 }
500
501 if ( rs->sr_ref ) {
502 ber_bvarray_free( rs->sr_ref );
503 rs->sr_ref = NULL;
504 }
505
506 Debug( LDAP_DEBUG_TRACE, "<==backsql_modrdn()\n" );
507
508 return rs->sr_err;
509 }
510