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 );
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 if ( ! access_allowed( be, conn, op, p,
276 children, NULL, ACL_WRITE, NULL ) )
278 rc = LDAP_INSUFFICIENT_ACCESS;
280 LDAP_LOG ( OPERATION, ERR,
281 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
283 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
286 text = "no write access to old parent's children";
291 LDAP_LOG ( OPERATION, DETAIL1,
292 "==>bdb_modrdn: wr to children %s is OK\n", p_ndn.bv_val, 0, 0 );
294 Debug( LDAP_DEBUG_TRACE,
295 "bdb_modrdn: wr to children of entry %s OK\n",
296 p_ndn.bv_val, 0, 0 );
299 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
300 p_dn = slap_empty_bv;
302 dnParent( &e->e_name, &p_dn );
306 LDAP_LOG ( OPERATION, DETAIL1,
307 "==>bdb_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
309 Debug( LDAP_DEBUG_TRACE,
310 "bdb_modrdn: parent dn=%s\n",
315 /* no parent, modrdn entry directly under root */
316 isroot = be_isroot( be, &op->o_ndn );
318 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
319 || be_isupdate( be, &op->o_ndn ) ) {
321 p = (Entry *)&slap_entry_root;
323 /* check parent for "children" acl */
324 rc = access_allowed( be, conn, op, p,
325 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 */
411 if ( newSuperior != NULL ) {
412 if ( newSuperior->bv_len ) {
414 np_ndn = nnewSuperior;
416 /* newSuperior == oldParent?, if so ==> ERROR */
417 /* newSuperior == entry being moved?, if so ==> ERROR */
418 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
420 rc = bdb_dn2entry_r( be,
421 ltid, nnewSuperior, &np, NULL, 0, locker, &lock );
427 case DB_LOCK_DEADLOCK:
428 case DB_LOCK_NOTGRANTED:
431 text = "ldap server busy";
435 text = "internal error";
441 LDAP_LOG ( OPERATION, DETAIL1,
442 "==>bdb_modrdn: newSup(ndn=%s) not here!\n",
443 np_ndn->bv_val, 0, 0 );
445 Debug( LDAP_DEBUG_TRACE,
446 "bdb_modrdn: newSup(ndn=%s) not here!\n",
447 np_ndn->bv_val, 0, 0);
449 text = "new superior not found";
455 LDAP_LOG ( OPERATION, DETAIL1,
456 "==>bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
457 np, (long) np->e_id, 0 );
459 Debug( LDAP_DEBUG_TRACE,
460 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
461 np, (long) np->e_id, 0 );
464 /* check newSuperior for "children" acl */
465 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE, NULL ) ) {
467 LDAP_LOG ( OPERATION, DETAIL1,
468 "==>bdb_modrdn: no wr to newSup children\n", 0, 0, 0 );
470 Debug( LDAP_DEBUG_TRACE,
471 "bdb_modrdn: no wr to newSup children\n",
474 text = "no write access to new superior's children";
475 rc = LDAP_INSUFFICIENT_ACCESS;
480 if ( is_entry_alias( np ) ) {
481 /* parent is an alias, don't allow add */
483 LDAP_LOG ( OPERATION, DETAIL1,
484 "==>bdb_modrdn: entry is alias\n", 0, 0, 0 );
486 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
489 text = "new superior is an alias";
490 rc = LDAP_ALIAS_PROBLEM;
495 if ( is_entry_referral( np ) ) {
496 /* parent is a referral, don't allow add */
498 LDAP_LOG ( OPERATION, DETAIL1,
499 "==>bdb_modrdn: entry is referral\n", 0, 0, 0 );
501 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
504 text = "new superior is a referral";
510 if ( isroot == -1 ) {
511 isroot = be_isroot( be, &op->o_ndn );
516 /* no parent, modrdn entry directly under root */
518 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
519 || be_isupdate( be, &op->o_ndn ) ) {
520 np = (Entry *)&slap_entry_root;
522 /* check parent for "children" acl */
523 rc = access_allowed( be, conn, op, np,
524 children, NULL, ACL_WRITE, NULL );
530 rc = LDAP_INSUFFICIENT_ACCESS;
532 LDAP_LOG ( OPERATION, ERR,
533 "==>bdb_modrdn: no access to superior\n", 0, 0, 0 );
535 Debug( LDAP_DEBUG_TRACE,
536 "no access to new superior\n",
539 text = "no write access to new superior's children";
544 LDAP_LOG ( OPERATION, DETAIL1,
545 "bdb_modrdn: wr to children entry \"\" OK\n", 0, 0, 0 );
547 Debug( LDAP_DEBUG_TRACE,
548 "bdb_modrdn: wr to children of entry \"\" OK\n",
554 LDAP_LOG ( OPERATION, ERR,
555 "bdb_modrdn: new superior=\"\", not root & \"\" "
556 "is not suffix\n", 0, 0, 0 );
558 Debug( LDAP_DEBUG_TRACE,
559 "bdb_modrdn: new superior=\"\", not root "
560 "& \"\" is not suffix\n",
563 text = "no write access to new superior's children";
564 rc = LDAP_INSUFFICIENT_ACCESS;
570 LDAP_LOG ( OPERATION, DETAIL1,
571 "bdb_modrdn: new superior=\"\"\n", 0, 0, 0 );
573 Debug( LDAP_DEBUG_TRACE,
574 "bdb_modrdn: new superior=\"\"\n",
580 LDAP_LOG ( OPERATION, DETAIL1,
581 "bdb_modrdn: wr to new parent's children OK\n", 0, 0, 0 );
583 Debug( LDAP_DEBUG_TRACE,
584 "bdb_modrdn: wr to new parent's children OK\n",
588 new_parent_dn = np_dn;
591 /* Build target dn and make sure target entry doesn't exist already. */
592 build_new_dn( &new_dn, new_parent_dn, newrdn );
594 dnNormalize2( NULL, &new_dn, &new_ndn );
597 LDAP_LOG ( OPERATION, RESULTS,
598 "bdb_modrdn: new ndn=%s\n", new_ndn.bv_val, 0, 0 );
600 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
601 new_ndn.bv_val, 0, 0 );
604 rc = bdb_dn2id ( be, ltid, &new_ndn, &id, 0 );
606 case DB_LOCK_DEADLOCK:
607 case DB_LOCK_NOTGRANTED:
612 rc = LDAP_ALREADY_EXISTS;
616 text = "internal error";
621 LDAP_LOG ( OPERATION, ERR,
622 "bdb_modrdn: new ndn=%s does not exist\n", new_ndn.bv_val, 0, 0 );
624 Debug( LDAP_DEBUG_TRACE,
625 "bdb_modrdn: new ndn=%s does not exist\n",
626 new_ndn.bv_val, 0, 0 );
629 /* Get attribute type and attribute value of our new rdn, we will
630 * need to add that to our new entry
632 if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text,
633 LDAP_DN_FORMAT_LDAP ) )
636 LDAP_LOG ( OPERATION, ERR,
637 "bdb_modrdn: can't figure out "
638 "type(s)/values(s) of newrdn\n",
641 Debug( LDAP_DEBUG_TRACE,
642 "bdb_modrdn: can't figure out "
643 "type(s)/values(s) of newrdn\n",
646 rc = LDAP_INVALID_DN_SYNTAX;
647 text = "unknown type(s) used in RDN";
652 LDAP_LOG ( OPERATION, RESULTS,
653 "bdb_modrdn: new_rdn_type=\"%s\", "
654 "new_rdn_val=\"%s\"\n",
655 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
656 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
658 Debug( LDAP_DEBUG_TRACE,
659 "bdb_modrdn: new_rdn_type=\"%s\", "
660 "new_rdn_val=\"%s\"\n",
661 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
662 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
665 if ( deleteoldrdn ) {
666 if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
667 LDAP_DN_FORMAT_LDAP ) )
670 LDAP_LOG ( OPERATION, ERR,
671 "bdb_modrdn: can't figure out "
672 "type(s)/values(s) of old_rdn\n",
675 Debug( LDAP_DEBUG_TRACE,
676 "bdb_modrdn: can't figure out "
677 "the old_rdn type(s)/value(s)\n",
681 text = "cannot parse RDN from old DN";
686 /* prepare modlist of modifications from old/new rdn */
687 rc = slap_modrdn2mods( be, conn, op, e, old_rdn, new_rdn,
688 deleteoldrdn, &mod );
689 if ( rc != LDAP_SUCCESS ) {
694 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
697 case DB_LOCK_DEADLOCK:
698 case DB_LOCK_NOTGRANTED:
702 text = "DN index delete fail";
706 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
708 /* Binary format uses a single contiguous block, cannot
709 * free individual fields. Leave new_dn/new_ndn set so
710 * they can be individually freed later.
713 e->e_nname = new_ndn;
715 new_dn.bv_val = NULL;
716 new_ndn.bv_val = NULL;
719 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
722 case DB_LOCK_DEADLOCK:
723 case DB_LOCK_NOTGRANTED:
727 text = "DN index add failed";
731 #ifdef LDAP_CLIENT_UPDATE
732 if ( rc == LDAP_SUCCESS && !op->o_noop ) {
733 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
734 bdb_psearch(be, conn, op, ps_list, e, LCUP_PSEARCH_BY_PREMODIFY );
737 #endif /* LDAP_CLIENT_UPDATE */
740 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
741 &text, textbuf, textlen );
743 if( rc != LDAP_SUCCESS ) {
745 case DB_LOCK_DEADLOCK:
746 case DB_LOCK_NOTGRANTED:
753 rc = bdb_id2entry_update( be, ltid, e );
756 case DB_LOCK_DEADLOCK:
757 case DB_LOCK_NOTGRANTED:
761 text = "entry update failed";
766 if(( rc=TXN_ABORT( ltid )) != 0 ) {
767 text = "txn_abort (no-op) failed";
774 char gid[DB_XIDDATASIZE];
776 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
777 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
779 if(( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
780 text = "txn_prepare failed";
782 if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
783 if(( rc=TXN_ABORT( ltid )) != 0 ) {
784 text ="cache update & txn_abort failed";
787 text = "cache update failed";
791 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
792 text = "txn_commit failed";
801 op->o_private = NULL;
803 if( rc == LDAP_SUCCESS ) {
805 LDAP_LOG ( OPERATION, RESULTS,
806 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
807 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
809 Debug(LDAP_DEBUG_TRACE,
810 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
811 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
815 bdb_cache_entry_commit( e );
820 LDAP_LOG ( OPERATION, RESULTS, "bdb_modrdn: %s : %s (%d)\n",
821 text, db_strerror(rc), rc );
823 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
824 text, db_strerror(rc), rc );
830 send_ldap_result( conn, op, rc,
831 NULL, text, NULL, NULL );
833 #ifdef LDAP_CLIENT_UPDATE
834 if ( rc == LDAP_SUCCESS && !op->o_noop ) {
835 /* Loop through in-scope entries for each psearch spec */
836 LDAP_LIST_FOREACH ( ps_list, &bdb->psearch_list, link ) {
837 bdb_psearch( be, conn, op, ps_list, e, LCUP_PSEARCH_BY_MODIFY );
839 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
840 while ( pm_list != NULL ) {
841 bdb_psearch(be, conn, op, pm_list->ps->op,
842 e, LCUP_PSEARCH_BY_SCOPEOUT);
843 LDAP_LIST_REMOVE ( pm_list, link );
845 pm_list = LDAP_LIST_NEXT ( pm_list, link );
849 #endif /* LDAP_CLIENT_UPDATE */
851 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
852 ldap_pvt_thread_yield();
853 TXN_CHECKPOINT( bdb->bi_dbenv,
854 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
858 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
859 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
861 /* LDAP v2 supporting correct attribute handling. */
862 if ( new_rdn != NULL ) {
863 ldap_rdnfree( new_rdn );
865 if ( old_rdn != NULL ) {
866 ldap_rdnfree( old_rdn );
870 for (; mod; mod=tmp ) {
876 /* LDAP v3 Support */
878 /* free new parent and reader lock */
879 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
883 /* free parent and reader lock */
884 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
889 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
893 #ifdef LDAP_CLIENT_UPDATE
894 pm_list = LDAP_LIST_FIRST(&op->premodify_list);
895 while ( pm_list != NULL ) {
896 LDAP_LIST_REMOVE ( pm_list, link );
898 pm_list = LDAP_LIST_NEXT ( pm_list, link );
903 op->o_private = NULL;
906 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );