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 retry: /* transaction retry */
77 bdb_cache_delete_entry(&bdb->bi_cache, e);
78 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
81 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
84 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
87 LDAP_LOG ( OPERATION, DETAIL1, "==>bdb_modrdn: retrying...\n", 0, 0, 0);
89 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
91 rc = TXN_ABORT( ltid );
96 text = "internal error";
99 ldap_pvt_thread_yield();
102 /* begin transaction */
103 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
104 bdb->bi_db_opflags );
108 LDAP_LOG ( OPERATION, ERR,
109 "==>bdb_modrdn: txn_begin failed: %s (%d)\n",
110 db_strerror(rc), rc, 0 );
112 Debug( LDAP_DEBUG_TRACE,
113 "bdb_delete: txn_begin failed: %s (%d)\n",
114 db_strerror(rc), rc, 0 );
117 text = "internal error";
121 locker = TXN_ID ( ltid );
124 opinfo.boi_txn = ltid;
126 op->o_private = &opinfo;
129 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
135 case DB_LOCK_DEADLOCK:
136 case DB_LOCK_NOTGRANTED:
139 text = "ldap server busy";
143 text = "internal error";
148 char* matched_dn = NULL;
151 if( matched != NULL ) {
152 matched_dn = ch_strdup( matched->e_dn );
153 refs = is_entry_referral( matched )
154 ? get_entry_referrals( be, conn, op, matched )
156 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched);
160 refs = referral_rewrite( default_referral,
161 NULL, dn, LDAP_SCOPE_DEFAULT );
164 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
165 matched_dn, NULL, refs, NULL );
167 ber_bvarray_free( refs );
173 /* check write on old entry */
174 rc = access_allowed( be, conn, op, e,
175 entry, NULL, ACL_WRITE, NULL );
177 switch( opinfo.boi_err ) {
178 case DB_LOCK_DEADLOCK:
179 case DB_LOCK_NOTGRANTED:
185 LDAP_LOG ( OPERATION, ERR,
186 "==>bdb_modrdn: no access to entry\n", 0, 0, 0 );
188 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
191 rc = LDAP_INSUFFICIENT_ACCESS;
195 if (!manageDSAit && is_entry_referral( e ) ) {
196 /* parent is a referral, don't allow add */
197 /* parent is an alias, don't allow add */
198 BerVarray refs = get_entry_referrals( be,
202 LDAP_LOG ( OPERATION, DETAIL1,
203 "==>bdb_modrdn: entry %s is referral \n", e->e_dn, 0, 0 );
205 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
209 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
210 e->e_dn, NULL, refs, NULL );
212 ber_bvarray_free( refs );
216 if ( be_issuffix( be, &e->e_nname ) ) {
217 p_ndn = slap_empty_bv;
219 dnParent( &e->e_nname, &p_ndn );
222 if ( p_ndn.bv_len != 0 ) {
223 /* Make sure parent entry exist and we can write its
226 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0, locker, &lock );
232 case DB_LOCK_DEADLOCK:
233 case DB_LOCK_NOTGRANTED:
236 text = "ldap server busy";
240 text = "internal error";
246 LDAP_LOG ( OPERATION, ERR,
247 "==>bdb_modrdn: parent does not exist\n", 0, 0, 0 );
249 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
256 /* check parent for "children" acl */
257 if ( ! access_allowed( be, conn, op, p,
258 children, NULL, ACL_WRITE, NULL ) )
261 LDAP_LOG ( OPERATION, ERR,
262 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
264 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
267 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
268 NULL, NULL, NULL, NULL );
273 LDAP_LOG ( OPERATION, DETAIL1,
274 "==>bdb_modrdn: wr to children %s is OK\n", p_ndn.bv_val, 0, 0 );
276 Debug( LDAP_DEBUG_TRACE,
277 "bdb_modrdn: wr to children of entry %s OK\n",
278 p_ndn.bv_val, 0, 0 );
281 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
282 p_dn = slap_empty_bv;
284 dnParent( &e->e_name, &p_dn );
288 LDAP_LOG ( OPERATION, DETAIL1,
289 "==>bdb_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
291 Debug( LDAP_DEBUG_TRACE,
292 "bdb_modrdn: parent dn=%s\n",
297 /* no parent, modrdn entry directly under root */
298 isroot = be_isroot( be, &op->o_ndn );
300 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
301 || be_isupdate( be, &op->o_ndn ) ) {
303 p = (Entry *)&slap_entry_root;
305 /* check parent for "children" acl */
306 rc = access_allowed( be, conn, op, p,
307 children, NULL, ACL_WRITE, NULL );
314 LDAP_LOG ( OPERATION, ERR,
315 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
317 Debug( LDAP_DEBUG_TRACE,
318 "no access to parent\n",
321 send_ldap_result( conn, op,
322 LDAP_INSUFFICIENT_ACCESS,
323 NULL, NULL, NULL, NULL );
328 LDAP_LOG ( OPERATION, DETAIL1,
329 "==>bdb_modrdn: wr to children of entry \"%s\" OK\n",
332 Debug( LDAP_DEBUG_TRACE,
333 "bdb_modrdn: wr to children of entry \"\" OK\n",
341 LDAP_LOG ( OPERATION, DETAIL1,
342 "==>bdb_modrdn: parent dn=\"\" \n", 0, 0, 0 );
344 Debug( LDAP_DEBUG_TRACE,
345 "bdb_modrdn: parent dn=\"\"\n",
351 LDAP_LOG ( OPERATION, ERR,
352 "==>bdb_modrdn: no parent, not root &\"\" is not "
353 "suffix\n", 0, 0, 0 );
355 Debug( LDAP_DEBUG_TRACE,
356 "bdb_modrdn: no parent, not root "
357 "& \"\" is not suffix\n",
360 rc = LDAP_INSUFFICIENT_ACCESS;
366 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
368 if ( newSuperior != NULL ) {
370 LDAP_LOG ( OPERATION, DETAIL1,
371 "==>bdb_modrdn: new parent \"%s\" requested...\n",
372 newSuperior->bv_val, 0, 0 );
374 Debug( LDAP_DEBUG_TRACE,
375 "bdb_modrdn: new parent \"%s\" requested...\n",
376 newSuperior->bv_val, 0, 0 );
379 /* newSuperior == oldParent? */
380 if( dn_match( &p_ndn, nnewSuperior ) ) {
382 LDAP_LOG( BACK_LDBM, INFO, "bdb_back_modrdn: "
383 "new parent \"%s\" same as the old parent \"%s\"\n",
384 newSuperior->bv_val, p_dn.bv_val, 0 );
386 Debug( LDAP_DEBUG_TRACE, "bdb_back_modrdn: "
387 "new parent \"%s\" same as the old parent \"%s\"\n",
388 newSuperior->bv_val, p_dn.bv_val, 0 );
390 newSuperior = NULL; /* ignore newSuperior */
393 if ( newSuperior != NULL ) {
394 if ( newSuperior->bv_len ) {
396 np_ndn = nnewSuperior;
398 /* newSuperior == oldParent?, if so ==> ERROR */
399 /* newSuperior == entry being moved?, if so ==> ERROR */
400 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
402 rc = bdb_dn2entry_r( be,
403 ltid, nnewSuperior, &np, NULL, 0, locker, &lock );
409 case DB_LOCK_DEADLOCK:
410 case DB_LOCK_NOTGRANTED:
413 text = "ldap server busy";
417 text = "internal error";
423 LDAP_LOG ( OPERATION, DETAIL1,
424 "==>bdb_modrdn: newSup(ndn=%s) not here!\n",
425 np_ndn->bv_val, 0, 0 );
427 Debug( LDAP_DEBUG_TRACE,
428 "bdb_modrdn: newSup(ndn=%s) not here!\n",
429 np_ndn->bv_val, 0, 0);
436 LDAP_LOG ( OPERATION, DETAIL1,
437 "==>bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
438 np, (long) np->e_id, 0 );
440 Debug( LDAP_DEBUG_TRACE,
441 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
442 np, (long) np->e_id, 0 );
445 /* check newSuperior for "children" acl */
446 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE, NULL ) ) {
448 LDAP_LOG ( OPERATION, DETAIL1,
449 "==>bdb_modrdn: no wr to newSup children\n", 0, 0, 0 );
451 Debug( LDAP_DEBUG_TRACE,
452 "bdb_modrdn: no wr to newSup children\n",
455 rc = LDAP_INSUFFICIENT_ACCESS;
460 if ( is_entry_alias( np ) ) {
461 /* parent is an alias, don't allow add */
463 LDAP_LOG ( OPERATION, DETAIL1,
464 "==>bdb_modrdn: entry is alias\n", 0, 0, 0 );
466 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
470 rc = LDAP_ALIAS_PROBLEM;
475 if ( is_entry_referral( np ) ) {
476 /* parent is a referral, don't allow add */
478 LDAP_LOG ( OPERATION, DETAIL1,
479 "==>bdb_modrdn: entry is referral\n", 0, 0, 0 );
481 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
490 if ( isroot == -1 ) {
491 isroot = be_isroot( be, &op->o_ndn );
496 /* no parent, modrdn entry directly under root */
498 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
499 || be_isupdate( be, &op->o_ndn ) ) {
500 np = (Entry *)&slap_entry_root;
502 /* check parent for "children" acl */
503 rc = access_allowed( be, conn, op, np,
504 children, NULL, ACL_WRITE, NULL );
511 LDAP_LOG ( OPERATION, ERR,
512 "==>bdb_modrdn: no access to superior\n", 0, 0, 0 );
514 Debug( LDAP_DEBUG_TRACE,
515 "no access to new superior\n",
518 send_ldap_result( conn, op,
519 LDAP_INSUFFICIENT_ACCESS,
520 NULL, NULL, NULL, NULL );
525 LDAP_LOG ( OPERATION, DETAIL1,
526 "bdb_modrdn: wr to children entry \"\" OK\n", 0, 0, 0 );
528 Debug( LDAP_DEBUG_TRACE,
529 "bdb_modrdn: wr to children of entry \"\" OK\n",
535 LDAP_LOG ( OPERATION, ERR,
536 "bdb_modrdn: new superior=\"\", not root & \"\" "
537 "is not suffix\n", 0, 0, 0 );
539 Debug( LDAP_DEBUG_TRACE,
540 "bdb_modrdn: new superior=\"\", not root "
541 "& \"\" is not suffix\n",
544 rc = LDAP_INSUFFICIENT_ACCESS;
550 LDAP_LOG ( OPERATION, DETAIL1,
551 "bdb_modrdn: new superior=\"\"\n", 0, 0, 0 );
553 Debug( LDAP_DEBUG_TRACE,
554 "bdb_modrdn: new superior=\"\"\n",
560 LDAP_LOG ( OPERATION, DETAIL1,
561 "bdb_modrdn: wr to new parent's children OK\n", 0, 0, 0 );
563 Debug( LDAP_DEBUG_TRACE,
564 "bdb_modrdn: wr to new parent's children OK\n",
568 new_parent_dn = np_dn;
571 /* Build target dn and make sure target entry doesn't exist already. */
572 build_new_dn( &new_dn, new_parent_dn, newrdn );
574 dnNormalize2( NULL, &new_dn, &new_ndn );
577 LDAP_LOG ( OPERATION, RESULTS,
578 "bdb_modrdn: new ndn=%s\n", new_ndn.bv_val, 0, 0 );
580 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
581 new_ndn.bv_val, 0, 0 );
584 rc = bdb_dn2id ( be, ltid, &new_ndn, &id, 0 );
586 case DB_LOCK_DEADLOCK:
587 case DB_LOCK_NOTGRANTED:
592 rc = LDAP_ALREADY_EXISTS;
596 text = "internal error";
601 LDAP_LOG ( OPERATION, ERR,
602 "bdb_modrdn: new ndn=%s does not exist\n", new_ndn.bv_val, 0, 0 );
604 Debug( LDAP_DEBUG_TRACE,
605 "bdb_modrdn: new ndn=%s does not exist\n",
606 new_ndn.bv_val, 0, 0 );
609 /* Get attribute type and attribute value of our new rdn, we will
610 * need to add that to our new entry
612 if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text,
613 LDAP_DN_FORMAT_LDAP ) )
616 LDAP_LOG ( OPERATION, ERR,
617 "bdb_modrdn: can't figure out "
618 "type(s)/values(s) of newrdn\n",
621 Debug( LDAP_DEBUG_TRACE,
622 "bdb_modrdn: can't figure out "
623 "type(s)/values(s) of newrdn\n",
626 rc = LDAP_INVALID_DN_SYNTAX;
627 text = "unknown type(s) used in RDN";
632 LDAP_LOG ( OPERATION, RESULTS,
633 "bdb_modrdn: new_rdn_type=\"%s\", "
634 "new_rdn_val=\"%s\"\n",
635 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
636 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
638 Debug( LDAP_DEBUG_TRACE,
639 "bdb_modrdn: new_rdn_type=\"%s\", "
640 "new_rdn_val=\"%s\"\n",
641 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
642 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
645 if ( deleteoldrdn ) {
646 if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
647 LDAP_DN_FORMAT_LDAP ) )
650 LDAP_LOG ( OPERATION, ERR,
651 "bdb_modrdn: can't figure out "
652 "type(s)/values(s) of old_rdn\n",
655 Debug( LDAP_DEBUG_TRACE,
656 "bdb_modrdn: can't figure out "
657 "the old_rdn type(s)/value(s)\n",
661 text = "cannot parse RDN from old DN";
666 /* prepare modlist of modifications from old/new rdn */
667 rc = slap_modrdn2mods( be, conn, op, e, old_rdn, new_rdn,
668 deleteoldrdn, &mod );
669 if ( rc != LDAP_SUCCESS ) {
674 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
677 case DB_LOCK_DEADLOCK:
678 case DB_LOCK_NOTGRANTED:
682 text = "DN index delete fail";
686 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
688 /* Binary format uses a single contiguous block, cannot
689 * free individual fields. Leave new_dn/new_ndn set so
690 * they can be individually freed later.
693 e->e_nname = new_ndn;
695 new_dn.bv_val = NULL;
696 new_ndn.bv_val = NULL;
699 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
702 case DB_LOCK_DEADLOCK:
703 case DB_LOCK_NOTGRANTED:
707 text = "DN index add failed";
712 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
713 &text, textbuf, textlen );
715 if( rc != LDAP_SUCCESS ) {
717 case DB_LOCK_DEADLOCK:
718 case DB_LOCK_NOTGRANTED:
725 rc = bdb_id2entry_update( be, ltid, e );
728 case DB_LOCK_DEADLOCK:
729 case DB_LOCK_NOTGRANTED:
733 text = "entry update failed";
738 if(( rc=TXN_ABORT( ltid )) != 0 ) {
739 text = "txn_abort (no-op) failed";
746 char gid[DB_XIDDATASIZE];
748 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
749 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
751 if(( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
752 text = "txn_prepare failed";
754 if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
755 if(( rc=TXN_ABORT( ltid )) != 0 ) {
756 text ="cache update & txn_abort failed";
759 text = "cache update failed";
763 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
764 text = "txn_commit failed";
773 op->o_private = NULL;
775 if( rc == LDAP_SUCCESS ) {
777 LDAP_LOG ( OPERATION, RESULTS,
778 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
779 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
781 Debug(LDAP_DEBUG_TRACE,
782 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
783 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
787 bdb_cache_entry_commit( e );
792 LDAP_LOG ( OPERATION, RESULTS, "bdb_modrdn: %s : %s (%d)\n",
793 text, db_strerror(rc), rc );
795 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
796 text, db_strerror(rc), rc );
802 send_ldap_result( conn, op, rc,
803 NULL, text, NULL, NULL );
805 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
806 ldap_pvt_thread_yield();
807 TXN_CHECKPOINT( bdb->bi_dbenv,
808 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
812 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
813 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
815 /* LDAP v2 supporting correct attribute handling. */
816 if ( new_rdn != NULL ) {
817 ldap_rdnfree( new_rdn );
819 if ( old_rdn != NULL ) {
820 ldap_rdnfree( old_rdn );
824 for (; mod; mod=tmp ) {
830 /* LDAP v3 Support */
832 /* free new parent and reader lock */
833 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
837 /* free parent and reader lock */
838 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
843 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
848 op->o_private = NULL;
851 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );