1 /* modrdn.cpp - ndb backend modrdn routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2008-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Howard Chu for inclusion
18 * in OpenLDAP Software. This work was sponsored by MySQL.
24 #include <ac/string.h>
29 ndb_back_modrdn( Operation *op, SlapReply *rs )
31 struct ndb_info *ni = (struct ndb_info *) op->o_bd->be_private;
32 AttributeDescription *children = slap_schema.si_ad_children;
33 AttributeDescription *entry = slap_schema.si_ad_entry;
34 struct berval new_dn = BER_BVNULL, new_ndn = BER_BVNULL;
37 char textbuf[SLAP_TEXT_BUFLEN];
38 size_t textlen = sizeof textbuf;
40 struct berval *np_dn = NULL; /* newSuperior dn */
41 struct berval *np_ndn = NULL; /* newSuperior ndn */
43 int manageDSAit = get_manageDSAit( op );
48 struct berval matched;
50 LDAPControl **preread_ctrl = NULL;
51 LDAPControl **postread_ctrl = NULL;
52 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
57 Debug( LDAP_DEBUG_ARGS, "==>" LDAP_XSTRING(ndb_back_modrdn) "(%s,%s,%s)\n",
58 op->o_req_dn.bv_val,op->oq_modrdn.rs_newrdn.bv_val,
59 op->oq_modrdn.rs_newSup ? op->oq_modrdn.rs_newSup->bv_val : "NULL" );
61 ctrls[num_ctrls] = NULL;
63 slap_mods_opattrs( op, &op->orr_modlist, 1 );
65 e.e_name = op->o_req_dn;
66 e.e_nname = op->o_req_ndn;
68 /* Get our NDB handle */
69 rs->sr_err = ndb_thread_handle( op, &NA.ndb );
78 retry: /* transaction retry */
82 attrs_free( e.e_attrs );
85 Debug( LDAP_DEBUG_TRACE, "==>" LDAP_XSTRING(ndb_back_modrdn)
86 ": retrying...\n", 0, 0, 0 );
87 if ( op->o_abandon ) {
88 rs->sr_err = SLAPD_ABANDON;
92 ber_bvarray_free_x( NA2.ocs, op->o_tmpmemctx );
95 ber_bvarray_free_x( NA.ocs, op->o_tmpmemctx );
97 ndb_trans_backoff( ++num_retries );
102 /* begin transaction */
103 NA.txn = NA.ndb->startTransaction();
106 Debug( LDAP_DEBUG_TRACE,
107 LDAP_XSTRING(ndb_back_modrdn) ": startTransaction failed: %s (%d)\n",
108 NA.ndb->getNdbError().message, NA.ndb->getNdbError().code, 0 );
109 rs->sr_err = LDAP_OTHER;
110 rs->sr_text = "internal error";
116 rs->sr_err = ndb_entry_get_info( op, &NA, 1, &matched );
117 switch( rs->sr_err ) {
120 case LDAP_NO_SUCH_OBJECT:
121 Debug( LDAP_DEBUG_ARGS,
122 "<=- ndb_back_modrdn: no such object %s\n",
123 op->o_req_dn.bv_val, 0, 0 );
124 rs->sr_matched = matched.bv_val;
126 ndb_check_referral( op, rs, &NA );
129 case DB_LOCK_DEADLOCK:
130 case DB_LOCK_NOTGRANTED:
134 rs->sr_text = "ldap server busy";
137 rs->sr_err = LDAP_OTHER;
138 rs->sr_text = "internal error";
142 /* acquire and lock entry */
143 rs->sr_err = ndb_entry_get_data( op, &NA, 1 );
147 if ( !manageDSAit && is_entry_glue( &e )) {
148 rs->sr_err = LDAP_NO_SUCH_OBJECT;
152 if ( get_assert( op ) &&
153 ( test_filter( op, &e, (Filter *)get_assertion( op )) != LDAP_COMPARE_TRUE ))
155 rs->sr_err = LDAP_ASSERTION_FAILED;
159 /* check write on old entry */
160 rs->sr_err = access_allowed( op, &e, entry, NULL, ACL_WRITE, NULL );
161 if ( ! rs->sr_err ) {
162 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
164 rs->sr_text = "no write access to old entry";
165 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
169 /* Can't do it if we have kids */
170 rs->sr_err = ndb_has_children( &NA, &rc );
172 Debug(LDAP_DEBUG_ARGS,
173 "<=- " LDAP_XSTRING(ndb_back_modrdn)
174 ": has_children failed: %s (%d)\n",
175 NA.txn->getNdbError().message, NA.txn->getNdbError().code, 0 );
176 rs->sr_err = LDAP_OTHER;
177 rs->sr_text = "internal error";
180 if ( rc == LDAP_COMPARE_TRUE ) {
181 Debug(LDAP_DEBUG_ARGS,
182 "<=- " LDAP_XSTRING(ndb_back_modrdn)
184 op->o_req_dn.bv_val, 0, 0);
185 rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
186 rs->sr_text = "subtree rename not supported";
190 if (!manageDSAit && is_entry_referral( &e ) ) {
191 /* entry is a referral, don't allow modrdn */
192 rs->sr_ref = get_entry_referrals( op, &e );
194 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(ndb_back_modrdn)
195 ": entry %s is referral\n", e.e_dn, 0, 0 );
197 rs->sr_err = LDAP_REFERRAL,
198 rs->sr_matched = op->o_req_dn.bv_val;
199 rs->sr_flags = REP_REF_MUSTBEFREED;
203 if ( be_issuffix( op->o_bd, &e.e_nname ) ) {
204 /* There can only be one suffix entry */
205 rs->sr_err = LDAP_NAMING_VIOLATION;
206 rs->sr_text = "cannot rename suffix entry";
209 dnParent( &e.e_nname, &e2.e_nname );
210 dnParent( &e.e_name, &e2.e_name );
213 /* check parent for "children" acl */
214 rs->sr_err = access_allowed( op, &e2,
216 op->oq_modrdn.rs_newSup == NULL ?
217 ACL_WRITE : ACL_WDEL,
220 if ( ! rs->sr_err ) {
221 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
222 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
224 rs->sr_text = "no write access to old parent's children";
228 Debug( LDAP_DEBUG_TRACE,
229 LDAP_XSTRING(ndb_back_modrdn) ": wr to children "
230 "of entry %s OK\n", e2.e_name.bv_val, 0, 0 );
232 if ( op->oq_modrdn.rs_newSup != NULL ) {
233 Debug( LDAP_DEBUG_TRACE,
234 LDAP_XSTRING(ndb_back_modrdn)
235 ": new parent \"%s\" requested...\n",
236 op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
238 /* newSuperior == oldParent? */
239 if( dn_match( &e2.e_nname, op->oq_modrdn.rs_nnewSup ) ) {
240 Debug( LDAP_DEBUG_TRACE, "bdb_back_modrdn: "
241 "new parent \"%s\" same as the old parent \"%s\"\n",
242 op->oq_modrdn.rs_newSup->bv_val, e2.e_name.bv_val, 0 );
243 op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
247 if ( op->oq_modrdn.rs_newSup != NULL ) {
248 if ( op->oq_modrdn.rs_newSup->bv_len ) {
250 np_dn = op->oq_modrdn.rs_newSup;
251 np_ndn = op->oq_modrdn.rs_nnewSup;
253 /* newSuperior == oldParent? - checked above */
254 /* newSuperior == entry being moved?, if so ==> ERROR */
255 if ( dnIsSuffix( np_ndn, &e.e_nname )) {
256 rs->sr_err = LDAP_NO_SUCH_OBJECT;
257 rs->sr_text = "new superior not found";
260 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
263 e2.e_nname = *np_ndn;
264 rs->sr_err = ndb_entry_get_info( op, &NA2, 1, NULL );
265 switch( rs->sr_err ) {
268 case LDAP_NO_SUCH_OBJECT:
269 Debug( LDAP_DEBUG_TRACE,
270 LDAP_XSTRING(ndb_back_modrdn)
271 ": newSup(ndn=%s) not here!\n",
272 np_ndn->bv_val, 0, 0);
273 rs->sr_text = "new superior not found";
276 case DB_LOCK_DEADLOCK:
277 case DB_LOCK_NOTGRANTED:
281 rs->sr_text = "ldap server busy";
284 rs->sr_err = LDAP_OTHER;
285 rs->sr_text = "internal error";
292 for ( i=0; !BER_BVISNULL( &NA2.ocs[i] ); i++);
294 a.a_desc = slap_schema.si_ad_objectClass;
300 if ( is_entry_alias( &e2 )) {
301 /* parent is an alias, don't allow move */
302 Debug( LDAP_DEBUG_TRACE,
303 LDAP_XSTRING(ndb_back_modrdn)
304 ": entry is alias\n",
306 rs->sr_text = "new superior is an alias";
307 rs->sr_err = LDAP_ALIAS_PROBLEM;
311 if ( is_entry_referral( &e2 ) ) {
312 /* parent is a referral, don't allow move */
313 Debug( LDAP_DEBUG_TRACE,
314 LDAP_XSTRING(ndb_back_modrdn)
315 ": entry is referral\n",
317 rs->sr_text = "new superior is a referral";
318 rs->sr_err = LDAP_OTHER;
324 /* check newSuperior for "children" acl */
325 rs->sr_err = access_allowed( op, &e2, children,
326 NULL, ACL_WADD, NULL );
328 Debug( LDAP_DEBUG_TRACE,
329 LDAP_XSTRING(ndb_back_modrdn)
330 ": no wr to newSup children\n",
332 rs->sr_text = "no write access to new superior's children";
333 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
337 Debug( LDAP_DEBUG_TRACE,
338 LDAP_XSTRING(ndb_back_modrdn)
339 ": wr to new parent OK id=%ld\n",
340 (long) e2.e_id, 0, 0 );
343 /* Build target dn and make sure target entry doesn't exist already. */
344 if (!new_dn.bv_val) {
345 build_new_dn( &new_dn, &e2.e_name, &op->oq_modrdn.rs_newrdn, NULL );
348 if (!new_ndn.bv_val) {
349 build_new_dn( &new_ndn, &e2.e_nname, &op->oq_modrdn.rs_nnewrdn, NULL );
352 Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(ndb_back_modrdn) ": new ndn=%s\n",
353 new_ndn.bv_val, 0, 0 );
355 /* Allow rename to same DN */
356 if ( !bvmatch ( &new_ndn, &e.e_nname )) {
359 e2.e_nname = new_ndn;
361 rs->sr_err = ndb_entry_get_info( op, &NA2, 1, NULL );
363 switch( rs->sr_err ) {
365 case DB_LOCK_DEADLOCK:
366 case DB_LOCK_NOTGRANTED:
369 case LDAP_NO_SUCH_OBJECT:
372 rs->sr_err = LDAP_ALREADY_EXISTS;
375 rs->sr_err = LDAP_OTHER;
376 rs->sr_text = "internal error";
381 assert( op->orr_modlist != NULL );
383 if( op->o_preread ) {
384 if( preread_ctrl == NULL ) {
385 preread_ctrl = &ctrls[num_ctrls++];
386 ctrls[num_ctrls] = NULL;
388 if( slap_read_controls( op, rs, &e,
389 &slap_pre_read_bv, preread_ctrl ) )
391 Debug( LDAP_DEBUG_TRACE,
392 "<=- " LDAP_XSTRING(ndb_back_modrdn)
393 ": pre-read failed!\n", 0, 0, 0 );
394 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
395 /* FIXME: is it correct to abort
396 * operation if control fails? */
403 rs->sr_err = ndb_entry_del_info( op->o_bd, &NA );
404 if ( rs->sr_err != 0 ) {
405 Debug(LDAP_DEBUG_TRACE,
406 "<=- " LDAP_XSTRING(ndb_back_modrdn)
407 ": dn2id del failed: %s (%d)\n",
408 NA.txn->getNdbError().message, NA.txn->getNdbError().code, 0 );
410 switch( rs->sr_err ) {
411 case DB_LOCK_DEADLOCK:
412 case DB_LOCK_NOTGRANTED:
416 rs->sr_err = LDAP_OTHER;
417 rs->sr_text = "DN index delete fail";
421 /* copy entry fields */
422 e2.e_attrs = e.e_attrs;
426 rs->sr_err = ndb_entry_put_info( op->o_bd, &NA2, 0 );
427 if ( rs->sr_err != 0 ) {
428 Debug(LDAP_DEBUG_TRACE,
429 "<=- " LDAP_XSTRING(ndb_back_modrdn)
430 ": dn2id add failed: %s (%d)\n",
431 NA.txn->getNdbError().message, NA.txn->getNdbError().code, 0 );
433 switch( rs->sr_err ) {
434 case DB_LOCK_DEADLOCK:
435 case DB_LOCK_NOTGRANTED:
439 rs->sr_err = LDAP_OTHER;
440 rs->sr_text = "DN index add failed";
445 rs->sr_err = ndb_modify_internal( op, &NA2,
446 &rs->sr_text, textbuf, textlen );
447 if( rs->sr_err != LDAP_SUCCESS ) {
448 Debug(LDAP_DEBUG_TRACE,
449 "<=- " LDAP_XSTRING(ndb_back_modrdn)
450 ": modify failed: %s (%d)\n",
451 NA.txn->getNdbError().message, NA.txn->getNdbError().code, 0 );
453 switch( rs->sr_err ) {
454 case DB_LOCK_DEADLOCK:
455 case DB_LOCK_NOTGRANTED:
462 e.e_attrs = e2.e_attrs;
464 if( op->o_postread ) {
465 if( postread_ctrl == NULL ) {
466 postread_ctrl = &ctrls[num_ctrls++];
467 ctrls[num_ctrls] = NULL;
469 if( slap_read_controls( op, rs, &e2,
470 &slap_post_read_bv, postread_ctrl ) )
472 Debug( LDAP_DEBUG_TRACE,
473 "<=- " LDAP_XSTRING(ndb_back_modrdn)
474 ": post-read failed!\n", 0, 0, 0 );
475 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
476 /* FIXME: is it correct to abort
477 * operation if control fails? */
484 if (( rs->sr_err=NA.txn->execute( NdbTransaction::Rollback,
485 NdbOperation::AbortOnError, 1 )) != 0 ) {
486 rs->sr_text = "txn_abort (no-op) failed";
488 rs->sr_err = LDAP_X_NO_OPERATION;
491 if (( rs->sr_err=NA.txn->execute( NdbTransaction::Commit,
492 NdbOperation::AbortOnError, 1 )) != 0 ) {
493 rs->sr_text = "txn_commit failed";
495 rs->sr_err = LDAP_SUCCESS;
499 if( rs->sr_err != LDAP_SUCCESS && rs->sr_err != LDAP_X_NO_OPERATION ) {
500 Debug( LDAP_DEBUG_TRACE,
501 LDAP_XSTRING(ndb_back_modrdn) ": txn_%s failed: %s (%d)\n",
502 op->o_noop ? "abort (no-op)" : "commit",
503 NA.txn->getNdbError().message, NA.txn->getNdbError().code );
504 rs->sr_err = LDAP_OTHER;
510 Debug(LDAP_DEBUG_TRACE,
511 LDAP_XSTRING(ndb_back_modrdn)
512 ": rdn modified%s id=%08lx dn=\"%s\"\n",
513 op->o_noop ? " (no-op)" : "",
514 e.e_id, op->o_req_dn.bv_val );
516 rs->sr_err = LDAP_SUCCESS;
518 if( num_ctrls ) rs->sr_ctrls = ctrls;
522 ber_bvarray_free_x( NA2.ocs, op->o_tmpmemctx );
527 ber_bvarray_free_x( NA.ocs, op->o_tmpmemctx );
532 attrs_free( e.e_attrs );
536 if( NA.txn != NULL ) {
537 NA.txn->execute( Rollback );
541 send_ldap_result( op, rs );
542 slap_graduate_commit_csn( op );
544 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
545 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
547 if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
548 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
549 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
551 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
552 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
553 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );