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 #ifdef LDAP_CLIENT_UPDATE
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 #ifdef LDAP_CLIENT_UPDATE
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;
143 op->o_private = &opinfo;
146 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
152 case DB_LOCK_DEADLOCK:
153 case DB_LOCK_NOTGRANTED:
156 text = "ldap server busy";
160 text = "internal error";
165 char* matched_dn = NULL;
168 if( matched != NULL ) {
169 matched_dn = ch_strdup( matched->e_dn );
170 refs = is_entry_referral( matched )
171 ? get_entry_referrals( be, conn, op, matched )
173 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched);
177 refs = referral_rewrite( default_referral,
178 NULL, dn, LDAP_SCOPE_DEFAULT );
181 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
182 matched_dn, NULL, refs, NULL );
184 ber_bvarray_free( refs );
190 /* check write on old entry */
191 rc = access_allowed( be, conn, op, e, entry, NULL, ACL_WRITE, NULL );
193 switch( opinfo.boi_err ) {
194 case DB_LOCK_DEADLOCK:
195 case DB_LOCK_NOTGRANTED:
201 LDAP_LOG ( OPERATION, ERR,
202 "==>bdb_modrdn: no access to entry\n", 0, 0, 0 );
204 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
207 text = "no write access to old entry";
208 rc = LDAP_INSUFFICIENT_ACCESS;
212 if (!manageDSAit && is_entry_referral( e ) ) {
213 /* parent is a referral, don't allow add */
214 /* parent is an alias, don't allow add */
215 BerVarray refs = get_entry_referrals( be,
219 LDAP_LOG ( OPERATION, DETAIL1,
220 "==>bdb_modrdn: entry %s is referral \n", e->e_dn, 0, 0 );
222 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
226 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
227 e->e_dn, NULL, refs, NULL );
229 ber_bvarray_free( refs );
233 if ( be_issuffix( be, &e->e_nname ) ) {
234 p_ndn = slap_empty_bv;
236 dnParent( &e->e_nname, &p_ndn );
239 if ( p_ndn.bv_len != 0 ) {
240 /* Make sure parent entry exist and we can write its
243 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0, locker, &lock );
249 case DB_LOCK_DEADLOCK:
250 case DB_LOCK_NOTGRANTED:
253 text = "ldap server busy";
257 text = "internal error";
263 LDAP_LOG ( OPERATION, ERR,
264 "==>bdb_modrdn: parent does not exist\n", 0, 0, 0 );
266 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
270 text = "old entry's parent does not exist";
274 /* check parent for "children" acl */
275 rc = access_allowed( be, conn, op, p,
276 children, NULL, ACL_WRITE, NULL );
279 rc = LDAP_INSUFFICIENT_ACCESS;
281 LDAP_LOG ( OPERATION, ERR,
282 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
284 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
287 text = "no write access to old parent's children";
292 LDAP_LOG ( OPERATION, DETAIL1,
293 "==>bdb_modrdn: wr to children %s is OK\n", p_ndn.bv_val, 0, 0 );
295 Debug( LDAP_DEBUG_TRACE,
296 "bdb_modrdn: wr to children of entry %s OK\n",
297 p_ndn.bv_val, 0, 0 );
300 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
301 p_dn = slap_empty_bv;
303 dnParent( &e->e_name, &p_dn );
307 LDAP_LOG ( OPERATION, DETAIL1,
308 "==>bdb_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
310 Debug( LDAP_DEBUG_TRACE,
311 "bdb_modrdn: parent dn=%s\n",
316 /* no parent, modrdn entry directly under root */
317 isroot = be_isroot( be, &op->o_ndn );
319 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
320 || be_isupdate( be, &op->o_ndn ) ) {
322 p = (Entry *)&slap_entry_root;
324 /* check parent for "children" acl */
325 rc = access_allowed( be, conn, op, p,
326 children, NULL, ACL_WRITE, NULL );
331 rc = LDAP_INSUFFICIENT_ACCESS;
333 LDAP_LOG ( OPERATION, ERR,
334 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
336 Debug( LDAP_DEBUG_TRACE,
337 "no access to parent\n",
340 text = "no write access to old parent";
345 LDAP_LOG ( OPERATION, DETAIL1,
346 "==>bdb_modrdn: wr to children of entry \"%s\" OK\n",
349 Debug( LDAP_DEBUG_TRACE,
350 "bdb_modrdn: wr to children of entry \"\" OK\n",
358 LDAP_LOG ( OPERATION, DETAIL1,
359 "==>bdb_modrdn: parent dn=\"\" \n", 0, 0, 0 );
361 Debug( LDAP_DEBUG_TRACE,
362 "bdb_modrdn: parent dn=\"\"\n",
368 LDAP_LOG ( OPERATION, ERR,
369 "==>bdb_modrdn: no parent, not root &\"\" is not "
370 "suffix\n", 0, 0, 0 );
372 Debug( LDAP_DEBUG_TRACE,
373 "bdb_modrdn: no parent, not root "
374 "& \"\" is not suffix\n",
377 text = "no write access to old parent";
378 rc = LDAP_INSUFFICIENT_ACCESS;
384 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
386 if ( newSuperior != NULL ) {
388 LDAP_LOG ( OPERATION, DETAIL1,
389 "==>bdb_modrdn: new parent \"%s\" requested...\n",
390 newSuperior->bv_val, 0, 0 );
392 Debug( LDAP_DEBUG_TRACE,
393 "bdb_modrdn: new parent \"%s\" requested...\n",
394 newSuperior->bv_val, 0, 0 );
397 /* newSuperior == oldParent? */
398 if( dn_match( &p_ndn, nnewSuperior ) ) {
400 LDAP_LOG( BACK_LDBM, INFO, "bdb_back_modrdn: "
401 "new parent \"%s\" same as the old parent \"%s\"\n",
402 newSuperior->bv_val, p_dn.bv_val, 0 );
404 Debug( LDAP_DEBUG_TRACE, "bdb_back_modrdn: "
405 "new parent \"%s\" same as the old parent \"%s\"\n",
406 newSuperior->bv_val, p_dn.bv_val, 0 );
408 newSuperior = NULL; /* ignore newSuperior */
412 if ( newSuperior != NULL ) {
413 if ( newSuperior->bv_len ) {
415 np_ndn = nnewSuperior;
417 /* newSuperior == oldParent?, if so ==> ERROR */
418 /* newSuperior == entry being moved?, if so ==> ERROR */
419 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
421 rc = bdb_dn2entry_r( be,
422 ltid, nnewSuperior, &np, NULL, 0, locker, &lock );
428 case DB_LOCK_DEADLOCK:
429 case DB_LOCK_NOTGRANTED:
432 text = "ldap server busy";
436 text = "internal error";
442 LDAP_LOG ( OPERATION, DETAIL1,
443 "==>bdb_modrdn: newSup(ndn=%s) not here!\n",
444 np_ndn->bv_val, 0, 0 );
446 Debug( LDAP_DEBUG_TRACE,
447 "bdb_modrdn: newSup(ndn=%s) not here!\n",
448 np_ndn->bv_val, 0, 0);
450 text = "new superior not found";
456 LDAP_LOG ( OPERATION, DETAIL1,
457 "==>bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
458 np, (long) np->e_id, 0 );
460 Debug( LDAP_DEBUG_TRACE,
461 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
462 np, (long) np->e_id, 0 );
465 /* check newSuperior for "children" acl */
466 rc = access_allowed( be, conn, op, np, children,
467 NULL, ACL_WRITE, NULL );
471 LDAP_LOG ( OPERATION, DETAIL1,
472 "==>bdb_modrdn: no wr to newSup children\n", 0, 0, 0 );
474 Debug( LDAP_DEBUG_TRACE,
475 "bdb_modrdn: no wr to newSup children\n",
478 text = "no write access to new superior's children";
479 rc = LDAP_INSUFFICIENT_ACCESS;
484 if ( is_entry_alias( np ) ) {
485 /* parent is an alias, don't allow add */
487 LDAP_LOG ( OPERATION, DETAIL1,
488 "==>bdb_modrdn: entry is alias\n", 0, 0, 0 );
490 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
493 text = "new superior is an alias";
494 rc = LDAP_ALIAS_PROBLEM;
499 if ( is_entry_referral( np ) ) {
500 /* parent is a referral, don't allow add */
502 LDAP_LOG ( OPERATION, DETAIL1,
503 "==>bdb_modrdn: entry is referral\n", 0, 0, 0 );
505 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
508 text = "new superior is a referral";
514 if ( isroot == -1 ) {
515 isroot = be_isroot( be, &op->o_ndn );
520 /* no parent, modrdn entry directly under root */
522 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
523 || be_isupdate( be, &op->o_ndn ) ) {
524 np = (Entry *)&slap_entry_root;
526 /* check parent for "children" acl */
527 rc = access_allowed( be, conn, op, np,
528 children, NULL, ACL_WRITE, NULL );
533 rc = LDAP_INSUFFICIENT_ACCESS;
535 LDAP_LOG ( OPERATION, ERR,
536 "==>bdb_modrdn: no access to superior\n", 0, 0, 0 );
538 Debug( LDAP_DEBUG_TRACE,
539 "no access to new superior\n",
542 text = "no write access to new superior's children";
547 LDAP_LOG ( OPERATION, DETAIL1,
548 "bdb_modrdn: wr to children entry \"\" OK\n", 0, 0, 0 );
550 Debug( LDAP_DEBUG_TRACE,
551 "bdb_modrdn: wr to children of entry \"\" OK\n",
557 LDAP_LOG ( OPERATION, ERR,
558 "bdb_modrdn: new superior=\"\", not root & \"\" "
559 "is not suffix\n", 0, 0, 0 );
561 Debug( LDAP_DEBUG_TRACE,
562 "bdb_modrdn: new superior=\"\", not root "
563 "& \"\" is not suffix\n",
566 text = "no write access to new superior's children";
567 rc = LDAP_INSUFFICIENT_ACCESS;
573 LDAP_LOG ( OPERATION, DETAIL1,
574 "bdb_modrdn: new superior=\"\"\n", 0, 0, 0 );
576 Debug( LDAP_DEBUG_TRACE,
577 "bdb_modrdn: new superior=\"\"\n",
583 LDAP_LOG ( OPERATION, DETAIL1,
584 "bdb_modrdn: wr to new parent's children OK\n", 0, 0, 0 );
586 Debug( LDAP_DEBUG_TRACE,
587 "bdb_modrdn: wr to new parent's children OK\n",
591 new_parent_dn = np_dn;
594 /* Build target dn and make sure target entry doesn't exist already. */
595 build_new_dn( &new_dn, new_parent_dn, newrdn );
597 dnNormalize2( NULL, &new_dn, &new_ndn );
600 LDAP_LOG ( OPERATION, RESULTS,
601 "bdb_modrdn: new ndn=%s\n", new_ndn.bv_val, 0, 0 );
603 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
604 new_ndn.bv_val, 0, 0 );
607 rc = bdb_dn2id ( be, ltid, &new_ndn, &id, 0 );
609 case DB_LOCK_DEADLOCK:
610 case DB_LOCK_NOTGRANTED:
615 rc = LDAP_ALREADY_EXISTS;
619 text = "internal error";
624 LDAP_LOG ( OPERATION, ERR,
625 "bdb_modrdn: new ndn=%s does not exist\n", new_ndn.bv_val, 0, 0 );
627 Debug( LDAP_DEBUG_TRACE,
628 "bdb_modrdn: new ndn=%s does not exist\n",
629 new_ndn.bv_val, 0, 0 );
632 /* Get attribute type and attribute value of our new rdn, we will
633 * need to add that to our new entry
635 if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text,
636 LDAP_DN_FORMAT_LDAP ) )
639 LDAP_LOG ( OPERATION, ERR,
640 "bdb_modrdn: can't figure out "
641 "type(s)/values(s) of newrdn\n",
644 Debug( LDAP_DEBUG_TRACE,
645 "bdb_modrdn: can't figure out "
646 "type(s)/values(s) of newrdn\n",
649 rc = LDAP_INVALID_DN_SYNTAX;
650 text = "unknown type(s) used in RDN";
655 LDAP_LOG ( OPERATION, RESULTS,
656 "bdb_modrdn: new_rdn_type=\"%s\", "
657 "new_rdn_val=\"%s\"\n",
658 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
659 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
661 Debug( LDAP_DEBUG_TRACE,
662 "bdb_modrdn: new_rdn_type=\"%s\", "
663 "new_rdn_val=\"%s\"\n",
664 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
665 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
668 if ( deleteoldrdn ) {
669 if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
670 LDAP_DN_FORMAT_LDAP ) )
673 LDAP_LOG ( OPERATION, ERR,
674 "bdb_modrdn: can't figure out "
675 "type(s)/values(s) of old_rdn\n",
678 Debug( LDAP_DEBUG_TRACE,
679 "bdb_modrdn: can't figure out "
680 "the old_rdn type(s)/value(s)\n",
684 text = "cannot parse RDN from old DN";
689 /* prepare modlist of modifications from old/new rdn */
690 rc = slap_modrdn2mods( be, conn, op, e, old_rdn, new_rdn,
691 deleteoldrdn, &mod );
692 if ( rc != LDAP_SUCCESS ) {
697 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
700 case DB_LOCK_DEADLOCK:
701 case DB_LOCK_NOTGRANTED:
705 text = "DN index delete fail";
709 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
711 /* Binary format uses a single contiguous block, cannot
712 * free individual fields. Leave new_dn/new_ndn set so
713 * they can be individually freed later.
716 e->e_nname = new_ndn;
718 new_dn.bv_val = NULL;
719 new_ndn.bv_val = NULL;
722 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
725 case DB_LOCK_DEADLOCK:
726 case DB_LOCK_NOTGRANTED:
730 text = "DN index add failed";
734 #ifdef LDAP_CLIENT_UPDATE
735 if ( rc == LDAP_SUCCESS && !op->o_noop ) {
736 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
737 bdb_psearch(be, conn, op, ps_list, e, LCUP_PSEARCH_BY_PREMODIFY );
740 #endif /* LDAP_CLIENT_UPDATE */
743 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
744 &text, textbuf, textlen );
746 if( rc != LDAP_SUCCESS ) {
748 case DB_LOCK_DEADLOCK:
749 case DB_LOCK_NOTGRANTED:
756 rc = bdb_id2entry_update( be, ltid, e );
759 case DB_LOCK_DEADLOCK:
760 case DB_LOCK_NOTGRANTED:
764 text = "entry update failed";
769 if(( rc=TXN_ABORT( ltid )) != 0 ) {
770 text = "txn_abort (no-op) failed";
777 char gid[DB_XIDDATASIZE];
779 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
780 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
782 if(( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
783 text = "txn_prepare failed";
785 if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
786 if(( rc=TXN_ABORT( ltid )) != 0 ) {
787 text ="cache update & txn_abort failed";
790 text = "cache update failed";
794 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
795 text = "txn_commit failed";
804 op->o_private = NULL;
806 if( rc == LDAP_SUCCESS ) {
808 LDAP_LOG ( OPERATION, RESULTS,
809 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
810 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
812 Debug(LDAP_DEBUG_TRACE,
813 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
814 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
818 bdb_cache_entry_commit( e );
823 LDAP_LOG ( OPERATION, RESULTS, "bdb_modrdn: %s : %s (%d)\n",
824 text, db_strerror(rc), rc );
826 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
827 text, db_strerror(rc), rc );
833 send_ldap_result( conn, op, rc,
834 NULL, text, NULL, NULL );
836 #ifdef LDAP_CLIENT_UPDATE
837 if ( rc == LDAP_SUCCESS && !op->o_noop ) {
838 /* Loop through in-scope entries for each psearch spec */
839 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
840 bdb_psearch( be, conn, op, ps_list, e, LCUP_PSEARCH_BY_MODIFY );
842 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
843 while ( pm_list != NULL ) {
844 bdb_psearch(be, conn, op, pm_list->ps->op,
845 e, LCUP_PSEARCH_BY_SCOPEOUT);
846 LDAP_LIST_REMOVE ( pm_list, link );
848 pm_list = LDAP_LIST_NEXT ( pm_list, link );
852 #endif /* LDAP_CLIENT_UPDATE */
854 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
855 ldap_pvt_thread_yield();
856 TXN_CHECKPOINT( bdb->bi_dbenv,
857 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
861 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
862 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
864 /* LDAP v2 supporting correct attribute handling. */
865 if ( new_rdn != NULL ) {
866 ldap_rdnfree( new_rdn );
868 if ( old_rdn != NULL ) {
869 ldap_rdnfree( old_rdn );
873 for (; mod; mod=tmp ) {
879 /* LDAP v3 Support */
881 /* free new parent and reader lock */
882 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
886 /* free parent and reader lock */
887 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
892 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
896 #ifdef LDAP_CLIENT_UPDATE
897 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
898 while ( pm_list != NULL ) {
899 LDAP_LIST_REMOVE ( pm_list, link );
901 pm_list = LDAP_LIST_NEXT ( pm_list, link );
906 op->o_private = NULL;
909 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );