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 if (!manageDSAit && is_entry_referral( e ) ) {
182 /* parent is a referral, don't allow add */
183 /* parent is an alias, don't allow add */
184 BerVarray refs = get_entry_referrals( be,
188 LDAP_LOG ( OPERATION, DETAIL1,
189 "==>bdb_modrdn: entry %s is referral \n", e->e_dn, 0, 0 );
191 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
195 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
196 e->e_dn, NULL, refs, NULL );
198 ber_bvarray_free( refs );
202 if ( be_issuffix( be, &e->e_nname ) ) {
203 p_ndn = slap_empty_bv;
205 dnParent( &e->e_nname, &p_ndn );
208 if ( p_ndn.bv_len != 0 ) {
209 /* Make sure parent entry exist and we can write its
212 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0, locker, &lock );
218 case DB_LOCK_DEADLOCK:
219 case DB_LOCK_NOTGRANTED:
222 text = "ldap server busy";
226 text = "internal error";
232 LDAP_LOG ( OPERATION, ERR,
233 "==>bdb_modrdn: parent does not exist\n", 0, 0, 0 );
235 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
242 /* check parent for "children" acl */
243 if ( ! access_allowed( be, conn, op, p,
244 children, NULL, ACL_WRITE, NULL ) )
247 LDAP_LOG ( OPERATION, ERR,
248 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
250 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
253 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
254 NULL, NULL, NULL, NULL );
259 LDAP_LOG ( OPERATION, DETAIL1,
260 "==>bdb_modrdn: wr to children %s is OK\n", p_ndn.bv_val, 0, 0 );
262 Debug( LDAP_DEBUG_TRACE,
263 "bdb_modrdn: wr to children of entry %s OK\n",
264 p_ndn.bv_val, 0, 0 );
267 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
268 p_dn = slap_empty_bv;
270 dnParent( &e->e_name, &p_dn );
274 LDAP_LOG ( OPERATION, DETAIL1,
275 "==>bdb_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
277 Debug( LDAP_DEBUG_TRACE,
278 "bdb_modrdn: parent dn=%s\n",
283 /* no parent, modrdn entry directly under root */
284 isroot = be_isroot( be, &op->o_ndn );
286 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
287 || be_isupdate( be, &op->o_ndn ) ) {
289 p = (Entry *)&slap_entry_root;
291 /* check parent for "children" acl */
292 rc = access_allowed( be, conn, op, p,
293 children, NULL, ACL_WRITE, NULL );
300 LDAP_LOG ( OPERATION, ERR,
301 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
303 Debug( LDAP_DEBUG_TRACE,
304 "no access to parent\n",
307 send_ldap_result( conn, op,
308 LDAP_INSUFFICIENT_ACCESS,
309 NULL, NULL, NULL, NULL );
314 LDAP_LOG ( OPERATION, DETAIL1,
315 "==>bdb_modrdn: wr to children of entry \"%s\" OK\n",
318 Debug( LDAP_DEBUG_TRACE,
319 "bdb_modrdn: wr to children of entry \"\" OK\n",
327 LDAP_LOG ( OPERATION, DETAIL1,
328 "==>bdb_modrdn: parent dn=\"\" \n", 0, 0, 0 );
330 Debug( LDAP_DEBUG_TRACE,
331 "bdb_modrdn: parent dn=\"\"\n",
337 LDAP_LOG ( OPERATION, ERR,
338 "==>bdb_modrdn: no parent, not root &\"\" is not "
339 "suffix\n", 0, 0, 0 );
341 Debug( LDAP_DEBUG_TRACE,
342 "bdb_modrdn: no parent, not root "
343 "& \"\" is not suffix\n",
346 rc = LDAP_INSUFFICIENT_ACCESS;
352 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
354 if ( newSuperior != NULL ) {
356 LDAP_LOG ( OPERATION, DETAIL1,
357 "==>bdb_modrdn: new parent \"%s\" requested...\n",
358 newSuperior->bv_val, 0, 0 );
360 Debug( LDAP_DEBUG_TRACE,
361 "bdb_modrdn: new parent \"%s\" requested...\n",
362 newSuperior->bv_val, 0, 0 );
365 if ( newSuperior->bv_len ) {
367 np_ndn = nnewSuperior;
369 /* newSuperior == oldParent?, if so ==> ERROR */
370 /* newSuperior == entry being moved?, if so ==> ERROR */
371 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
373 rc = bdb_dn2entry_r( be, ltid, nnewSuperior, &np, NULL, 0, locker, &lock );
379 case DB_LOCK_DEADLOCK:
380 case DB_LOCK_NOTGRANTED:
383 text = "ldap server busy";
387 text = "internal error";
393 LDAP_LOG ( OPERATION, DETAIL1,
394 "==>bdb_modrdn: newSup(ndn=%s) not here!\n",
395 np_ndn->bv_val, 0, 0 );
397 Debug( LDAP_DEBUG_TRACE,
398 "bdb_modrdn: newSup(ndn=%s) not here!\n",
399 np_ndn->bv_val, 0, 0);
406 LDAP_LOG ( OPERATION, DETAIL1,
407 "==>bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
408 np, (long) np->e_id, 0 );
410 Debug( LDAP_DEBUG_TRACE,
411 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
412 np, (long) np->e_id, 0 );
415 /* check newSuperior for "children" acl */
416 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE, NULL ) ) {
418 LDAP_LOG ( OPERATION, DETAIL1,
419 "==>bdb_modrdn: no wr to newSup children\n", 0, 0, 0 );
421 Debug( LDAP_DEBUG_TRACE,
422 "bdb_modrdn: no wr to newSup children\n",
425 rc = LDAP_INSUFFICIENT_ACCESS;
430 if ( is_entry_alias( np ) ) {
431 /* parent is an alias, don't allow add */
433 LDAP_LOG ( OPERATION, DETAIL1,
434 "==>bdb_modrdn: entry is alias\n", 0, 0, 0 );
436 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
440 rc = LDAP_ALIAS_PROBLEM;
445 if ( is_entry_referral( np ) ) {
446 /* parent is a referral, don't allow add */
448 LDAP_LOG ( OPERATION, DETAIL1,
449 "==>bdb_modrdn: entry is referral\n", 0, 0, 0 );
451 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
460 if ( isroot == -1 ) {
461 isroot = be_isroot( be, &op->o_ndn );
466 /* no parent, modrdn entry directly under root */
468 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
469 || be_isupdate( be, &op->o_ndn ) ) {
470 np = (Entry *)&slap_entry_root;
472 /* check parent for "children" acl */
473 rc = access_allowed( be, conn, op, np,
474 children, NULL, ACL_WRITE, NULL );
481 LDAP_LOG ( OPERATION, ERR,
482 "==>bdb_modrdn: no access to superior\n", 0, 0, 0 );
484 Debug( LDAP_DEBUG_TRACE,
485 "no access to new superior\n",
488 send_ldap_result( conn, op,
489 LDAP_INSUFFICIENT_ACCESS,
490 NULL, NULL, NULL, NULL );
495 LDAP_LOG ( OPERATION, DETAIL1,
496 "bdb_modrdn: wr to children entry \"\" OK\n", 0, 0, 0 );
498 Debug( LDAP_DEBUG_TRACE,
499 "bdb_modrdn: wr to children of entry \"\" OK\n",
505 LDAP_LOG ( OPERATION, ERR,
506 "bdb_modrdn: new superior=\"\", not root & \"\" "
507 "is not suffix\n", 0, 0, 0 );
509 Debug( LDAP_DEBUG_TRACE,
510 "bdb_modrdn: new superior=\"\", not root "
511 "& \"\" is not suffix\n",
514 rc = LDAP_INSUFFICIENT_ACCESS;
520 LDAP_LOG ( OPERATION, DETAIL1,
521 "bdb_modrdn: new superior=\"\"\n", 0, 0, 0 );
523 Debug( LDAP_DEBUG_TRACE,
524 "bdb_modrdn: new superior=\"\"\n",
530 LDAP_LOG ( OPERATION, DETAIL1,
531 "bdb_modrdn: wr to new parent's children OK\n", 0, 0, 0 );
533 Debug( LDAP_DEBUG_TRACE,
534 "bdb_modrdn: wr to new parent's children OK\n",
538 new_parent_dn = np_dn;
541 /* check write on old entry */
542 rc = access_allowed( be, conn, op, e,
543 entry, NULL, ACL_WRITE, NULL );
545 switch( opinfo.boi_err ) {
546 case DB_LOCK_DEADLOCK:
547 case DB_LOCK_NOTGRANTED:
553 LDAP_LOG ( OPERATION, ERR,
554 "==>bdb_modrdn: no access to entry\n", 0, 0, 0 );
556 Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
559 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
560 NULL, NULL, NULL, NULL );
565 /* Build target dn and make sure target entry doesn't exist already. */
566 build_new_dn( &new_dn, new_parent_dn, newrdn );
568 dnNormalize2( NULL, &new_dn, &new_ndn );
571 LDAP_LOG ( OPERATION, RESULTS,
572 "bdb_modrdn: new ndn=%s\n", new_ndn.bv_val, 0, 0 );
574 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
575 new_ndn.bv_val, 0, 0 );
578 rc = bdb_dn2id ( be, ltid, &new_ndn, &id, 0 );
580 case DB_LOCK_DEADLOCK:
581 case DB_LOCK_NOTGRANTED:
586 rc = LDAP_ALREADY_EXISTS;
590 text = "internal error";
595 LDAP_LOG ( OPERATION, ERR,
596 "bdb_modrdn: new ndn=%s does not exist\n", new_ndn.bv_val, 0, 0 );
598 Debug( LDAP_DEBUG_TRACE,
599 "bdb_modrdn: new ndn=%s does not exist\n",
600 new_ndn.bv_val, 0, 0 );
603 /* Get attribute type and attribute value of our new rdn, we will
604 * need to add that to our new entry
606 if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text,
607 LDAP_DN_FORMAT_LDAP ) )
610 LDAP_LOG ( OPERATION, ERR,
611 "bdb_modrdn: can't figure out "
612 "type(s)/values(s) of newrdn\n",
615 Debug( LDAP_DEBUG_TRACE,
616 "bdb_modrdn: can't figure out "
617 "type(s)/values(s) of newrdn\n",
620 rc = LDAP_INVALID_DN_SYNTAX;
621 text = "unknown type(s) used in RDN";
626 LDAP_LOG ( OPERATION, RESULTS,
627 "bdb_modrdn: new_rdn_type=\"%s\", "
628 "new_rdn_val=\"%s\"\n",
629 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
630 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
632 Debug( LDAP_DEBUG_TRACE,
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 );
639 if ( deleteoldrdn ) {
640 if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
641 LDAP_DN_FORMAT_LDAP ) )
644 LDAP_LOG ( OPERATION, ERR,
645 "bdb_modrdn: can't figure out "
646 "type(s)/values(s) of old_rdn\n",
649 Debug( LDAP_DEBUG_TRACE,
650 "bdb_modrdn: can't figure out "
651 "the old_rdn type(s)/value(s)\n",
655 text = "cannot parse RDN from old DN";
660 /* prepare modlist of modifications from old/new rdn */
661 rc = slap_modrdn2mods( be, conn, op, e, old_rdn, new_rdn,
662 deleteoldrdn, &mod );
663 if ( rc != LDAP_SUCCESS ) {
668 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
671 case DB_LOCK_DEADLOCK:
672 case DB_LOCK_NOTGRANTED:
676 text = "DN index delete fail";
680 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
682 /* Binary format uses a single contiguous block, cannot
683 * free individual fields. Leave new_dn/new_ndn set so
684 * they can be individually freed later.
687 e->e_nname = new_ndn;
689 new_dn.bv_val = NULL;
690 new_ndn.bv_val = NULL;
693 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
696 case DB_LOCK_DEADLOCK:
697 case DB_LOCK_NOTGRANTED:
701 text = "DN index add failed";
706 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
707 &text, textbuf, textlen );
709 if( rc != LDAP_SUCCESS ) {
711 case DB_LOCK_DEADLOCK:
712 case DB_LOCK_NOTGRANTED:
719 rc = bdb_id2entry_update( be, ltid, e );
722 case DB_LOCK_DEADLOCK:
723 case DB_LOCK_NOTGRANTED:
727 text = "entry update failed";
732 if(( rc=TXN_ABORT( ltid )) != 0 ) {
733 text = "txn_abort (no-op) failed";
740 char gid[DB_XIDDATASIZE];
742 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
743 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
745 if(( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
746 text = "txn_prepare failed";
748 if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
749 if(( rc=TXN_ABORT( ltid )) != 0 ) {
750 text ="cache update & txn_abort failed";
753 text = "cache update failed";
757 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
758 text = "txn_commit failed";
767 op->o_private = NULL;
769 if( rc == LDAP_SUCCESS ) {
771 LDAP_LOG ( OPERATION, RESULTS,
772 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
773 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
775 Debug(LDAP_DEBUG_TRACE,
776 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
777 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
781 bdb_cache_entry_commit( e );
786 LDAP_LOG ( OPERATION, RESULTS, "bdb_modrdn: %s : %s (%d)\n",
787 text, db_strerror(rc), rc );
789 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
790 text, db_strerror(rc), rc );
796 send_ldap_result( conn, op, rc,
797 NULL, text, NULL, NULL );
799 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
800 ldap_pvt_thread_yield();
801 TXN_CHECKPOINT( bdb->bi_dbenv,
802 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
806 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
807 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
809 /* LDAP v2 supporting correct attribute handling. */
810 if ( new_rdn != NULL ) {
811 ldap_rdnfree( new_rdn );
813 if ( old_rdn != NULL ) {
814 ldap_rdnfree( old_rdn );
818 for (; mod; mod=tmp ) {
824 /* LDAP v3 Support */
826 /* free new parent and reader lock */
827 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
831 /* free parent and reader lock */
832 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
837 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
842 op->o_private = NULL;
845 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );