1 /* modrdn.c - bdb backend modrdn routine */
4 * Copyright 1998-2003 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 );
65 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
67 struct psid_entry* pm_list;
68 struct psid_entry* pm_prev;
72 LDAP_LOG ( OPERATION, ENTRY, "==>bdb_modrdn(%s,%s,%s)\n",
73 dn->bv_val,newrdn->bv_val, newSuperior ? newSuperior->bv_val : "NULL" );
75 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
76 dn->bv_val, newrdn->bv_val,
77 newSuperior ? newSuperior->bv_val : "NULL" );
81 retry: /* transaction retry */
83 bdb_cache_delete_entry(&bdb->bi_cache, e);
84 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
87 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
90 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
93 LDAP_LOG ( OPERATION, DETAIL1, "==>bdb_modrdn: retrying...\n", 0, 0, 0);
95 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
98 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
99 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
100 while ( pm_list != NULL ) {
101 LDAP_LIST_REMOVE ( pm_list, link );
103 pm_list = LDAP_LIST_NEXT ( pm_list, link );
108 rc = TXN_ABORT( ltid );
110 op->o_private = NULL;
113 text = "internal error";
116 ldap_pvt_thread_yield();
119 /* begin transaction */
120 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
121 bdb->bi_db_opflags );
125 LDAP_LOG ( OPERATION, ERR,
126 "==>bdb_modrdn: txn_begin failed: %s (%d)\n",
127 db_strerror(rc), rc, 0 );
129 Debug( LDAP_DEBUG_TRACE,
130 "bdb_delete: txn_begin failed: %s (%d)\n",
131 db_strerror(rc), rc, 0 );
134 text = "internal error";
138 locker = TXN_ID ( ltid );
141 opinfo.boi_txn = ltid;
142 opinfo.boi_locker = locker;
144 op->o_private = &opinfo;
147 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
153 case DB_LOCK_DEADLOCK:
154 case DB_LOCK_NOTGRANTED:
157 text = "ldap server busy";
161 text = "internal error";
166 char* matched_dn = NULL;
169 if( matched != NULL ) {
170 matched_dn = ch_strdup( matched->e_dn );
171 refs = is_entry_referral( matched )
172 ? get_entry_referrals( be, conn, op, matched )
174 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched);
178 refs = referral_rewrite( default_referral,
179 NULL, dn, LDAP_SCOPE_DEFAULT );
182 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
183 matched_dn, NULL, refs, NULL );
185 ber_bvarray_free( refs );
191 /* check write on old entry */
192 rc = access_allowed( be, conn, op, e, entry, NULL, ACL_WRITE, NULL );
194 switch( opinfo.boi_err ) {
195 case DB_LOCK_DEADLOCK:
196 case DB_LOCK_NOTGRANTED:
202 LDAP_LOG ( OPERATION, ERR,
203 "==>bdb_modrdn: no access to entry\n", 0, 0, 0 );
205 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
208 text = "no write access to old entry";
209 rc = LDAP_INSUFFICIENT_ACCESS;
213 if (!manageDSAit && is_entry_referral( e ) ) {
214 /* parent is a referral, don't allow add */
215 /* parent is an alias, don't allow add */
216 BerVarray refs = get_entry_referrals( be,
220 LDAP_LOG ( OPERATION, DETAIL1,
221 "==>bdb_modrdn: entry %s is referral \n", e->e_dn, 0, 0 );
223 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
227 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
228 e->e_dn, NULL, refs, NULL );
230 ber_bvarray_free( refs );
234 if ( be_issuffix( be, &e->e_nname ) ) {
235 p_ndn = slap_empty_bv;
237 dnParent( &e->e_nname, &p_ndn );
240 if ( p_ndn.bv_len != 0 ) {
241 /* Make sure parent entry exist and we can write its
244 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0, locker, &lock );
250 case DB_LOCK_DEADLOCK:
251 case DB_LOCK_NOTGRANTED:
254 text = "ldap server busy";
258 text = "internal error";
264 LDAP_LOG ( OPERATION, ERR,
265 "==>bdb_modrdn: parent does not exist\n", 0, 0, 0 );
267 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
271 text = "old entry's parent does not exist";
275 /* check parent for "children" acl */
276 rc = access_allowed( be, conn, op, p,
277 children, NULL, ACL_WRITE, NULL );
280 rc = LDAP_INSUFFICIENT_ACCESS;
282 LDAP_LOG ( OPERATION, ERR,
283 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
285 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
288 text = "no write access to old parent's children";
293 LDAP_LOG ( OPERATION, DETAIL1,
294 "==>bdb_modrdn: wr to children %s is OK\n", p_ndn.bv_val, 0, 0 );
296 Debug( LDAP_DEBUG_TRACE,
297 "bdb_modrdn: wr to children of entry %s OK\n",
298 p_ndn.bv_val, 0, 0 );
301 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
302 p_dn = slap_empty_bv;
304 dnParent( &e->e_name, &p_dn );
308 LDAP_LOG ( OPERATION, DETAIL1,
309 "==>bdb_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
311 Debug( LDAP_DEBUG_TRACE,
312 "bdb_modrdn: parent dn=%s\n",
317 /* no parent, modrdn entry directly under root */
318 isroot = be_isroot( be, &op->o_ndn );
320 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
321 || be_isupdate( be, &op->o_ndn ) ) {
323 p = (Entry *)&slap_entry_root;
325 /* check parent for "children" acl */
326 rc = access_allowed( be, conn, op, p,
327 children, NULL, ACL_WRITE, NULL );
332 rc = LDAP_INSUFFICIENT_ACCESS;
334 LDAP_LOG ( OPERATION, ERR,
335 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
337 Debug( LDAP_DEBUG_TRACE,
338 "no access to parent\n",
341 text = "no write access to old parent";
346 LDAP_LOG ( OPERATION, DETAIL1,
347 "==>bdb_modrdn: wr to children of entry \"%s\" OK\n",
350 Debug( LDAP_DEBUG_TRACE,
351 "bdb_modrdn: wr to children of entry \"\" OK\n",
359 LDAP_LOG ( OPERATION, DETAIL1,
360 "==>bdb_modrdn: parent dn=\"\" \n", 0, 0, 0 );
362 Debug( LDAP_DEBUG_TRACE,
363 "bdb_modrdn: parent dn=\"\"\n",
369 LDAP_LOG ( OPERATION, ERR,
370 "==>bdb_modrdn: no parent, not root &\"\" is not "
371 "suffix\n", 0, 0, 0 );
373 Debug( LDAP_DEBUG_TRACE,
374 "bdb_modrdn: no parent, not root "
375 "& \"\" is not suffix\n",
378 text = "no write access to old parent";
379 rc = LDAP_INSUFFICIENT_ACCESS;
385 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
387 if ( newSuperior != NULL ) {
389 LDAP_LOG ( OPERATION, DETAIL1,
390 "==>bdb_modrdn: new parent \"%s\" requested...\n",
391 newSuperior->bv_val, 0, 0 );
393 Debug( LDAP_DEBUG_TRACE,
394 "bdb_modrdn: new parent \"%s\" requested...\n",
395 newSuperior->bv_val, 0, 0 );
398 /* newSuperior == oldParent? */
399 if( dn_match( &p_ndn, nnewSuperior ) ) {
401 LDAP_LOG( BACK_LDBM, INFO, "bdb_back_modrdn: "
402 "new parent \"%s\" same as the old parent \"%s\"\n",
403 newSuperior->bv_val, p_dn.bv_val, 0 );
405 Debug( LDAP_DEBUG_TRACE, "bdb_back_modrdn: "
406 "new parent \"%s\" same as the old parent \"%s\"\n",
407 newSuperior->bv_val, p_dn.bv_val, 0 );
409 newSuperior = NULL; /* ignore newSuperior */
413 if ( newSuperior != NULL ) {
414 if ( newSuperior->bv_len ) {
416 np_ndn = nnewSuperior;
418 /* newSuperior == oldParent?, if so ==> ERROR */
419 /* newSuperior == entry being moved?, if so ==> ERROR */
420 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
422 rc = bdb_dn2entry_r( be,
423 ltid, nnewSuperior, &np, NULL, 0, locker, &lock );
429 case DB_LOCK_DEADLOCK:
430 case DB_LOCK_NOTGRANTED:
433 text = "ldap server busy";
437 text = "internal error";
443 LDAP_LOG ( OPERATION, DETAIL1,
444 "==>bdb_modrdn: newSup(ndn=%s) not here!\n",
445 np_ndn->bv_val, 0, 0 );
447 Debug( LDAP_DEBUG_TRACE,
448 "bdb_modrdn: newSup(ndn=%s) not here!\n",
449 np_ndn->bv_val, 0, 0);
451 text = "new superior not found";
457 LDAP_LOG ( OPERATION, DETAIL1,
458 "==>bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
459 np, (long) np->e_id, 0 );
461 Debug( LDAP_DEBUG_TRACE,
462 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
463 np, (long) np->e_id, 0 );
466 /* check newSuperior for "children" acl */
467 rc = access_allowed( be, conn, op, np, children,
468 NULL, ACL_WRITE, NULL );
472 LDAP_LOG ( OPERATION, DETAIL1,
473 "==>bdb_modrdn: no wr to newSup children\n", 0, 0, 0 );
475 Debug( LDAP_DEBUG_TRACE,
476 "bdb_modrdn: no wr to newSup children\n",
479 text = "no write access to new superior's children";
480 rc = LDAP_INSUFFICIENT_ACCESS;
485 if ( is_entry_alias( np ) ) {
486 /* parent is an alias, don't allow add */
488 LDAP_LOG ( OPERATION, DETAIL1,
489 "==>bdb_modrdn: entry is alias\n", 0, 0, 0 );
491 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
494 text = "new superior is an alias";
495 rc = LDAP_ALIAS_PROBLEM;
500 if ( is_entry_referral( np ) ) {
501 /* parent is a referral, don't allow add */
503 LDAP_LOG ( OPERATION, DETAIL1,
504 "==>bdb_modrdn: entry is referral\n", 0, 0, 0 );
506 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
509 text = "new superior is a referral";
515 if ( isroot == -1 ) {
516 isroot = be_isroot( be, &op->o_ndn );
521 /* no parent, modrdn entry directly under root */
523 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
524 || be_isupdate( be, &op->o_ndn ) ) {
525 np = (Entry *)&slap_entry_root;
527 /* check parent for "children" acl */
528 rc = access_allowed( be, conn, op, np,
529 children, NULL, ACL_WRITE, NULL );
534 rc = LDAP_INSUFFICIENT_ACCESS;
536 LDAP_LOG ( OPERATION, ERR,
537 "==>bdb_modrdn: no access to superior\n", 0, 0, 0 );
539 Debug( LDAP_DEBUG_TRACE,
540 "no access to new superior\n",
543 text = "no write access to new superior's children";
548 LDAP_LOG ( OPERATION, DETAIL1,
549 "bdb_modrdn: wr to children entry \"\" OK\n", 0, 0, 0 );
551 Debug( LDAP_DEBUG_TRACE,
552 "bdb_modrdn: wr to children of entry \"\" OK\n",
558 LDAP_LOG ( OPERATION, ERR,
559 "bdb_modrdn: new superior=\"\", not root & \"\" "
560 "is not suffix\n", 0, 0, 0 );
562 Debug( LDAP_DEBUG_TRACE,
563 "bdb_modrdn: new superior=\"\", not root "
564 "& \"\" is not suffix\n",
567 text = "no write access to new superior's children";
568 rc = LDAP_INSUFFICIENT_ACCESS;
574 LDAP_LOG ( OPERATION, DETAIL1,
575 "bdb_modrdn: new superior=\"\"\n", 0, 0, 0 );
577 Debug( LDAP_DEBUG_TRACE,
578 "bdb_modrdn: new superior=\"\"\n",
584 LDAP_LOG ( OPERATION, DETAIL1,
585 "bdb_modrdn: wr to new parent's children OK\n", 0, 0, 0 );
587 Debug( LDAP_DEBUG_TRACE,
588 "bdb_modrdn: wr to new parent's children OK\n",
592 new_parent_dn = np_dn;
595 /* Build target dn and make sure target entry doesn't exist already. */
596 build_new_dn( &new_dn, new_parent_dn, newrdn );
598 dnNormalize2( NULL, &new_dn, &new_ndn );
601 LDAP_LOG ( OPERATION, RESULTS,
602 "bdb_modrdn: new ndn=%s\n", new_ndn.bv_val, 0, 0 );
604 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
605 new_ndn.bv_val, 0, 0 );
608 rc = bdb_dn2id ( be, ltid, &new_ndn, &id, 0 );
610 case DB_LOCK_DEADLOCK:
611 case DB_LOCK_NOTGRANTED:
616 rc = LDAP_ALREADY_EXISTS;
620 text = "internal error";
625 LDAP_LOG ( OPERATION, ERR,
626 "bdb_modrdn: new ndn=%s does not exist\n", new_ndn.bv_val, 0, 0 );
628 Debug( LDAP_DEBUG_TRACE,
629 "bdb_modrdn: new ndn=%s does not exist\n",
630 new_ndn.bv_val, 0, 0 );
633 /* Get attribute type and attribute value of our new rdn, we will
634 * need to add that to our new entry
636 if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text,
637 LDAP_DN_FORMAT_LDAP ) )
640 LDAP_LOG ( OPERATION, ERR,
641 "bdb_modrdn: can't figure out "
642 "type(s)/values(s) of newrdn\n",
645 Debug( LDAP_DEBUG_TRACE,
646 "bdb_modrdn: can't figure out "
647 "type(s)/values(s) of newrdn\n",
650 rc = LDAP_INVALID_DN_SYNTAX;
651 text = "unknown type(s) used in RDN";
656 LDAP_LOG ( OPERATION, RESULTS,
657 "bdb_modrdn: new_rdn_type=\"%s\", "
658 "new_rdn_val=\"%s\"\n",
659 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
660 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
662 Debug( LDAP_DEBUG_TRACE,
663 "bdb_modrdn: new_rdn_type=\"%s\", "
664 "new_rdn_val=\"%s\"\n",
665 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
666 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
669 if ( deleteoldrdn ) {
670 if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
671 LDAP_DN_FORMAT_LDAP ) )
674 LDAP_LOG ( OPERATION, ERR,
675 "bdb_modrdn: can't figure out "
676 "type(s)/values(s) of old_rdn\n",
679 Debug( LDAP_DEBUG_TRACE,
680 "bdb_modrdn: can't figure out "
681 "the old_rdn type(s)/value(s)\n",
685 text = "cannot parse RDN from old DN";
690 /* prepare modlist of modifications from old/new rdn */
691 rc = slap_modrdn2mods( be, conn, op, e, old_rdn, new_rdn,
692 deleteoldrdn, &mod );
693 if ( rc != LDAP_SUCCESS ) {
698 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
701 case DB_LOCK_DEADLOCK:
702 case DB_LOCK_NOTGRANTED:
706 text = "DN index delete fail";
710 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
712 /* Binary format uses a single contiguous block, cannot
713 * free individual fields. Leave new_dn/new_ndn set so
714 * they can be individually freed later.
717 e->e_nname = new_ndn;
719 new_dn.bv_val = NULL;
720 new_ndn.bv_val = NULL;
723 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
726 case DB_LOCK_DEADLOCK:
727 case DB_LOCK_NOTGRANTED:
731 text = "DN index add failed";
735 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
736 if ( rc == LDAP_SUCCESS && !op->o_noop ) {
737 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
738 bdb_psearch(be, conn, op, ps_list, e, LDAP_PSEARCH_BY_PREMODIFY );
744 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
745 &text, textbuf, textlen );
747 if( rc != LDAP_SUCCESS ) {
749 case DB_LOCK_DEADLOCK:
750 case DB_LOCK_NOTGRANTED:
757 rc = bdb_id2entry_update( be, ltid, e );
760 case DB_LOCK_DEADLOCK:
761 case DB_LOCK_NOTGRANTED:
765 text = "entry update failed";
770 if(( rc=TXN_ABORT( ltid )) != 0 ) {
771 text = "txn_abort (no-op) failed";
778 char gid[DB_XIDDATASIZE];
780 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
781 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
783 if(( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
784 text = "txn_prepare failed";
786 if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
787 if(( rc=TXN_ABORT( ltid )) != 0 ) {
788 text ="cache update & txn_abort failed";
791 text = "cache update failed";
795 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
796 text = "txn_commit failed";
805 op->o_private = NULL;
807 if( rc == LDAP_SUCCESS ) {
809 LDAP_LOG ( OPERATION, RESULTS,
810 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
811 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
813 Debug(LDAP_DEBUG_TRACE,
814 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
815 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
819 bdb_cache_entry_commit( e );
824 LDAP_LOG ( OPERATION, RESULTS, "bdb_modrdn: %s : %s (%d)\n",
825 text, db_strerror(rc), rc );
827 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
828 text, db_strerror(rc), rc );
834 send_ldap_result( conn, op, rc,
835 NULL, text, NULL, NULL );
837 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
838 if ( rc == LDAP_SUCCESS && !op->o_noop ) {
839 /* Loop through in-scope entries for each psearch spec */
840 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
841 bdb_psearch( be, conn, op, ps_list, e, LDAP_PSEARCH_BY_MODIFY );
843 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
844 while ( pm_list != NULL ) {
845 bdb_psearch(be, conn, op, pm_list->ps->op,
846 e, LDAP_PSEARCH_BY_SCOPEOUT);
847 LDAP_LIST_REMOVE ( pm_list, link );
849 pm_list = LDAP_LIST_NEXT ( pm_list, link );
855 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
856 ldap_pvt_thread_yield();
857 TXN_CHECKPOINT( bdb->bi_dbenv,
858 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
862 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
863 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
865 /* LDAP v2 supporting correct attribute handling. */
866 if ( new_rdn != NULL ) {
867 ldap_rdnfree( new_rdn );
869 if ( old_rdn != NULL ) {
870 ldap_rdnfree( old_rdn );
874 for (; mod; mod=tmp ) {
880 /* LDAP v3 Support */
882 /* free new parent and reader lock */
883 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
887 /* free parent and reader lock */
888 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
893 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
897 #if defined(LDAP_CLIENT_UPDATE) || defined(LDAP_SYNC)
898 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
899 while ( pm_list != NULL ) {
900 LDAP_LIST_REMOVE ( pm_list, link );
902 pm_list = LDAP_LIST_NEXT ( pm_list, link );
907 op->o_private = NULL;
910 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );