1 /* modrdn.c - bdb backend modrdn routine */
4 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
23 struct berval *newrdn,
24 struct berval *nnewrdn,
26 struct berval *newSuperior,
27 struct berval *nnewSuperior )
29 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
30 AttributeDescription *children = slap_schema.si_ad_children;
31 AttributeDescription *entry = slap_schema.si_ad_entry;
32 struct berval p_dn, p_ndn;
33 struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
38 /* LDAP v2 supporting correct attribute handling. */
39 LDAPRDN *new_rdn = NULL;
40 LDAPRDN *old_rdn = NULL;
43 char textbuf[SLAP_TEXT_BUFLEN];
44 size_t textlen = sizeof textbuf;
46 struct bdb_op_info opinfo;
50 Entry *np = NULL; /* newSuperior Entry */
51 struct berval *np_dn = NULL; /* newSuperior dn */
52 struct berval *np_ndn = NULL; /* newSuperior ndn */
53 struct berval *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
55 /* Used to interface with bdb_modify_internal() */
56 Modifications *mod = NULL; /* Used to delete old rdn */
58 int manageDSAit = get_manageDSAit( op );
66 LDAP_LOG ( OPERATION, ENTRY, "==>bdb_modrdn(%s,%s,%s)\n",
67 dn->bv_val,newrdn->bv_val, newSuperior ? newSuperior->bv_val : "NULL" );
69 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
70 dn->bv_val, newrdn->bv_val,
71 newSuperior ? newSuperior->bv_val : "NULL" );
75 if( newSuperior != NULL ) {
76 rc = LDAP_UNWILLING_TO_PERFORM;
77 text = "newSuperior not implemented (yet)";
83 retry: /* transaction retry */
85 bdb_cache_delete_entry(&bdb->bi_cache, e);
86 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
89 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
92 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
95 LDAP_LOG ( OPERATION, DETAIL1, "==>bdb_modrdn: retrying...\n", 0, 0, 0);
97 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
99 rc = TXN_ABORT( ltid );
101 op->o_private = NULL;
104 text = "internal error";
107 ldap_pvt_thread_yield();
110 /* begin transaction */
111 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
112 bdb->bi_db_opflags );
116 LDAP_LOG ( OPERATION, ERR,
117 "==>bdb_modrdn: txn_begin failed: %s (%d)\n",
118 db_strerror(rc), rc, 0 );
120 Debug( LDAP_DEBUG_TRACE,
121 "bdb_delete: txn_begin failed: %s (%d)\n",
122 db_strerror(rc), rc, 0 );
125 text = "internal error";
129 locker = TXN_ID ( ltid );
132 opinfo.boi_txn = ltid;
134 op->o_private = &opinfo;
137 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
143 case DB_LOCK_DEADLOCK:
144 case DB_LOCK_NOTGRANTED:
147 text = "ldap server busy";
151 text = "internal error";
156 char* matched_dn = NULL;
159 if( matched != NULL ) {
160 matched_dn = ch_strdup( matched->e_dn );
161 refs = is_entry_referral( matched )
162 ? get_entry_referrals( be, conn, op, matched )
164 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched);
168 refs = referral_rewrite( default_referral,
169 NULL, dn, LDAP_SCOPE_DEFAULT );
172 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
173 matched_dn, NULL, refs, NULL );
175 ber_bvarray_free( refs );
181 /* check write on old entry */
182 rc = access_allowed( be, conn, op, e,
183 entry, NULL, ACL_WRITE, NULL );
185 switch( opinfo.boi_err ) {
186 case DB_LOCK_DEADLOCK:
187 case DB_LOCK_NOTGRANTED:
193 LDAP_LOG ( OPERATION, ERR,
194 "==>bdb_modrdn: no access to entry\n", 0, 0, 0 );
196 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
199 rc = LDAP_INSUFFICIENT_ACCESS;
203 if (!manageDSAit && is_entry_referral( e ) ) {
204 /* parent is a referral, don't allow add */
205 /* parent is an alias, don't allow add */
206 BerVarray refs = get_entry_referrals( be,
210 LDAP_LOG ( OPERATION, DETAIL1,
211 "==>bdb_modrdn: entry %s is referral \n", e->e_dn, 0, 0 );
213 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
217 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
218 e->e_dn, NULL, refs, NULL );
220 ber_bvarray_free( refs );
224 if ( be_issuffix( be, &e->e_nname ) ) {
225 p_ndn = slap_empty_bv;
227 dnParent( &e->e_nname, &p_ndn );
230 if ( p_ndn.bv_len != 0 ) {
231 /* Make sure parent entry exist and we can write its
234 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0, locker, &lock );
240 case DB_LOCK_DEADLOCK:
241 case DB_LOCK_NOTGRANTED:
244 text = "ldap server busy";
248 text = "internal error";
254 LDAP_LOG ( OPERATION, ERR,
255 "==>bdb_modrdn: parent does not exist\n", 0, 0, 0 );
257 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
264 /* check parent for "children" acl */
265 if ( ! access_allowed( be, conn, op, p,
266 children, NULL, ACL_WRITE, NULL ) )
269 LDAP_LOG ( OPERATION, ERR,
270 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
272 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
275 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
276 NULL, NULL, NULL, NULL );
281 LDAP_LOG ( OPERATION, DETAIL1,
282 "==>bdb_modrdn: wr to children %s is OK\n", p_ndn.bv_val, 0, 0 );
284 Debug( LDAP_DEBUG_TRACE,
285 "bdb_modrdn: wr to children of entry %s OK\n",
286 p_ndn.bv_val, 0, 0 );
289 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
290 p_dn = slap_empty_bv;
292 dnParent( &e->e_name, &p_dn );
296 LDAP_LOG ( OPERATION, DETAIL1,
297 "==>bdb_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
299 Debug( LDAP_DEBUG_TRACE,
300 "bdb_modrdn: parent dn=%s\n",
305 /* no parent, modrdn entry directly under root */
306 isroot = be_isroot( be, &op->o_ndn );
308 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
309 || be_isupdate( be, &op->o_ndn ) ) {
311 p = (Entry *)&slap_entry_root;
313 /* check parent for "children" acl */
314 rc = access_allowed( be, conn, op, p,
315 children, NULL, ACL_WRITE, NULL );
322 LDAP_LOG ( OPERATION, ERR,
323 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
325 Debug( LDAP_DEBUG_TRACE,
326 "no access to parent\n",
329 send_ldap_result( conn, op,
330 LDAP_INSUFFICIENT_ACCESS,
331 NULL, NULL, NULL, NULL );
336 LDAP_LOG ( OPERATION, DETAIL1,
337 "==>bdb_modrdn: wr to children of entry \"%s\" OK\n",
340 Debug( LDAP_DEBUG_TRACE,
341 "bdb_modrdn: wr to children of entry \"\" OK\n",
349 LDAP_LOG ( OPERATION, DETAIL1,
350 "==>bdb_modrdn: parent dn=\"\" \n", 0, 0, 0 );
352 Debug( LDAP_DEBUG_TRACE,
353 "bdb_modrdn: parent dn=\"\"\n",
359 LDAP_LOG ( OPERATION, ERR,
360 "==>bdb_modrdn: no parent, not root &\"\" is not "
361 "suffix\n", 0, 0, 0 );
363 Debug( LDAP_DEBUG_TRACE,
364 "bdb_modrdn: no parent, not root "
365 "& \"\" is not suffix\n",
368 rc = LDAP_INSUFFICIENT_ACCESS;
374 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
376 if ( newSuperior != NULL ) {
378 LDAP_LOG ( OPERATION, DETAIL1,
379 "==>bdb_modrdn: new parent \"%s\" requested...\n",
380 newSuperior->bv_val, 0, 0 );
382 Debug( LDAP_DEBUG_TRACE,
383 "bdb_modrdn: new parent \"%s\" requested...\n",
384 newSuperior->bv_val, 0, 0 );
387 if ( newSuperior->bv_len ) {
389 np_ndn = nnewSuperior;
391 /* newSuperior == oldParent?, if so ==> ERROR */
392 /* newSuperior == entry being moved?, if so ==> ERROR */
393 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
395 rc = bdb_dn2entry_r( be, ltid, nnewSuperior, &np, NULL, 0, locker, &lock );
401 case DB_LOCK_DEADLOCK:
402 case DB_LOCK_NOTGRANTED:
405 text = "ldap server busy";
409 text = "internal error";
415 LDAP_LOG ( OPERATION, DETAIL1,
416 "==>bdb_modrdn: newSup(ndn=%s) not here!\n",
417 np_ndn->bv_val, 0, 0 );
419 Debug( LDAP_DEBUG_TRACE,
420 "bdb_modrdn: newSup(ndn=%s) not here!\n",
421 np_ndn->bv_val, 0, 0);
428 LDAP_LOG ( OPERATION, DETAIL1,
429 "==>bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
430 np, (long) np->e_id, 0 );
432 Debug( LDAP_DEBUG_TRACE,
433 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
434 np, (long) np->e_id, 0 );
437 /* check newSuperior for "children" acl */
438 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE, NULL ) ) {
440 LDAP_LOG ( OPERATION, DETAIL1,
441 "==>bdb_modrdn: no wr to newSup children\n", 0, 0, 0 );
443 Debug( LDAP_DEBUG_TRACE,
444 "bdb_modrdn: no wr to newSup children\n",
447 rc = LDAP_INSUFFICIENT_ACCESS;
452 if ( is_entry_alias( np ) ) {
453 /* parent is an alias, don't allow add */
455 LDAP_LOG ( OPERATION, DETAIL1,
456 "==>bdb_modrdn: entry is alias\n", 0, 0, 0 );
458 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
462 rc = LDAP_ALIAS_PROBLEM;
467 if ( is_entry_referral( np ) ) {
468 /* parent is a referral, don't allow add */
470 LDAP_LOG ( OPERATION, DETAIL1,
471 "==>bdb_modrdn: entry is referral\n", 0, 0, 0 );
473 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
482 if ( isroot == -1 ) {
483 isroot = be_isroot( be, &op->o_ndn );
488 /* no parent, modrdn entry directly under root */
490 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
491 || be_isupdate( be, &op->o_ndn ) ) {
492 np = (Entry *)&slap_entry_root;
494 /* check parent for "children" acl */
495 rc = access_allowed( be, conn, op, np,
496 children, NULL, ACL_WRITE, NULL );
503 LDAP_LOG ( OPERATION, ERR,
504 "==>bdb_modrdn: no access to superior\n", 0, 0, 0 );
506 Debug( LDAP_DEBUG_TRACE,
507 "no access to new superior\n",
510 send_ldap_result( conn, op,
511 LDAP_INSUFFICIENT_ACCESS,
512 NULL, NULL, NULL, NULL );
517 LDAP_LOG ( OPERATION, DETAIL1,
518 "bdb_modrdn: wr to children entry \"\" OK\n", 0, 0, 0 );
520 Debug( LDAP_DEBUG_TRACE,
521 "bdb_modrdn: wr to children of entry \"\" OK\n",
527 LDAP_LOG ( OPERATION, ERR,
528 "bdb_modrdn: new superior=\"\", not root & \"\" "
529 "is not suffix\n", 0, 0, 0 );
531 Debug( LDAP_DEBUG_TRACE,
532 "bdb_modrdn: new superior=\"\", not root "
533 "& \"\" is not suffix\n",
536 rc = LDAP_INSUFFICIENT_ACCESS;
542 LDAP_LOG ( OPERATION, DETAIL1,
543 "bdb_modrdn: new superior=\"\"\n", 0, 0, 0 );
545 Debug( LDAP_DEBUG_TRACE,
546 "bdb_modrdn: new superior=\"\"\n",
552 LDAP_LOG ( OPERATION, DETAIL1,
553 "bdb_modrdn: wr to new parent's children OK\n", 0, 0, 0 );
555 Debug( LDAP_DEBUG_TRACE,
556 "bdb_modrdn: wr to new parent's children OK\n",
560 new_parent_dn = np_dn;
563 /* Build target dn and make sure target entry doesn't exist already. */
564 build_new_dn( &new_dn, new_parent_dn, newrdn );
566 dnNormalize2( NULL, &new_dn, &new_ndn );
569 LDAP_LOG ( OPERATION, RESULTS,
570 "bdb_modrdn: new ndn=%s\n", new_ndn.bv_val, 0, 0 );
572 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
573 new_ndn.bv_val, 0, 0 );
576 rc = bdb_dn2id ( be, ltid, &new_ndn, &id, 0 );
578 case DB_LOCK_DEADLOCK:
579 case DB_LOCK_NOTGRANTED:
584 rc = LDAP_ALREADY_EXISTS;
588 text = "internal error";
593 LDAP_LOG ( OPERATION, ERR,
594 "bdb_modrdn: new ndn=%s does not exist\n", new_ndn.bv_val, 0, 0 );
596 Debug( LDAP_DEBUG_TRACE,
597 "bdb_modrdn: new ndn=%s does not exist\n",
598 new_ndn.bv_val, 0, 0 );
601 /* Get attribute type and attribute value of our new rdn, we will
602 * need to add that to our new entry
604 if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text,
605 LDAP_DN_FORMAT_LDAP ) )
608 LDAP_LOG ( OPERATION, ERR,
609 "bdb_modrdn: can't figure out "
610 "type(s)/values(s) of newrdn\n",
613 Debug( LDAP_DEBUG_TRACE,
614 "bdb_modrdn: can't figure out "
615 "type(s)/values(s) of newrdn\n",
618 rc = LDAP_INVALID_DN_SYNTAX;
619 text = "unknown type(s) used in RDN";
624 LDAP_LOG ( OPERATION, RESULTS,
625 "bdb_modrdn: new_rdn_type=\"%s\", "
626 "new_rdn_val=\"%s\"\n",
627 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
628 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
630 Debug( LDAP_DEBUG_TRACE,
631 "bdb_modrdn: new_rdn_type=\"%s\", "
632 "new_rdn_val=\"%s\"\n",
633 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
634 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
637 if ( deleteoldrdn ) {
638 if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
639 LDAP_DN_FORMAT_LDAP ) )
642 LDAP_LOG ( OPERATION, ERR,
643 "bdb_modrdn: can't figure out "
644 "type(s)/values(s) of old_rdn\n",
647 Debug( LDAP_DEBUG_TRACE,
648 "bdb_modrdn: can't figure out "
649 "the old_rdn type(s)/value(s)\n",
653 text = "cannot parse RDN from old DN";
658 /* prepare modlist of modifications from old/new rdn */
659 rc = slap_modrdn2mods( be, conn, op, e, old_rdn, new_rdn,
660 deleteoldrdn, &mod );
661 if ( rc != LDAP_SUCCESS ) {
666 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
669 case DB_LOCK_DEADLOCK:
670 case DB_LOCK_NOTGRANTED:
674 text = "DN index delete fail";
678 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
680 /* Binary format uses a single contiguous block, cannot
681 * free individual fields. Leave new_dn/new_ndn set so
682 * they can be individually freed later.
685 e->e_nname = new_ndn;
687 new_dn.bv_val = NULL;
688 new_ndn.bv_val = NULL;
691 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
694 case DB_LOCK_DEADLOCK:
695 case DB_LOCK_NOTGRANTED:
699 text = "DN index add failed";
704 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
705 &text, textbuf, textlen );
707 if( rc != LDAP_SUCCESS ) {
709 case DB_LOCK_DEADLOCK:
710 case DB_LOCK_NOTGRANTED:
717 rc = bdb_id2entry_update( be, ltid, e );
720 case DB_LOCK_DEADLOCK:
721 case DB_LOCK_NOTGRANTED:
725 text = "entry update failed";
730 if(( rc=TXN_ABORT( ltid )) != 0 ) {
731 text = "txn_abort (no-op) failed";
738 char gid[DB_XIDDATASIZE];
740 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
741 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
743 if(( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
744 text = "txn_prepare failed";
746 if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
747 if(( rc=TXN_ABORT( ltid )) != 0 ) {
748 text ="cache update & txn_abort failed";
751 text = "cache update failed";
755 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
756 text = "txn_commit failed";
765 op->o_private = NULL;
767 if( rc == LDAP_SUCCESS ) {
769 LDAP_LOG ( OPERATION, RESULTS,
770 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
771 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
773 Debug(LDAP_DEBUG_TRACE,
774 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
775 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
779 bdb_cache_entry_commit( e );
784 LDAP_LOG ( OPERATION, RESULTS, "bdb_modrdn: %s : %s (%d)\n",
785 text, db_strerror(rc), rc );
787 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
788 text, db_strerror(rc), rc );
794 send_ldap_result( conn, op, rc,
795 NULL, text, NULL, NULL );
797 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
798 ldap_pvt_thread_yield();
799 TXN_CHECKPOINT( bdb->bi_dbenv,
800 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
804 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
805 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
807 /* LDAP v2 supporting correct attribute handling. */
808 if ( new_rdn != NULL ) {
809 ldap_rdnfree( new_rdn );
811 if ( old_rdn != NULL ) {
812 ldap_rdnfree( old_rdn );
816 for (; mod; mod=tmp ) {
822 /* LDAP v3 Support */
824 /* free new parent and reader lock */
825 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
829 /* free parent and reader lock */
830 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
835 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
840 op->o_private = NULL;
843 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );