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 struct berval p_dn, p_ndn;
32 struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
37 /* LDAP v2 supporting correct attribute handling. */
38 LDAPRDN *new_rdn = NULL;
39 LDAPRDN *old_rdn = NULL;
42 char textbuf[SLAP_TEXT_BUFLEN];
43 size_t textlen = sizeof textbuf;
45 struct bdb_op_info opinfo;
49 Entry *np = NULL; /* newSuperior Entry */
50 struct berval *np_dn = NULL; /* newSuperior dn */
51 struct berval *np_ndn = NULL; /* newSuperior ndn */
52 struct berval *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
54 /* Used to interface with bdb_modify_internal() */
55 Modifications *mod = NULL; /* Used to delete old rdn */
57 int manageDSAit = get_manageDSAit( op );
65 LDAP_LOG ( OPERATION, ENTRY, "==>bdb_modrdn(%s,%s,%s)\n",
66 dn->bv_val,newrdn->bv_val, newSuperior ? newSuperior->bv_val : "NULL" );
68 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
69 dn->bv_val, newrdn->bv_val,
70 newSuperior ? newSuperior->bv_val : "NULL" );
74 if( newSuperior != NULL ) {
75 rc = LDAP_UNWILLING_TO_PERFORM;
76 text = "newSuperior not implemented (yet)";
82 retry: /* transaction retry */
84 bdb_cache_delete_entry(&bdb->bi_cache, e);
85 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
88 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
91 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
94 LDAP_LOG ( OPERATION, DETAIL1, "==>bdb_modrdn: retrying...\n", 0, 0, 0);
96 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
98 rc = TXN_ABORT( ltid );
100 op->o_private = NULL;
103 text = "internal error";
106 ldap_pvt_thread_yield();
109 /* begin transaction */
110 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
111 bdb->bi_db_opflags );
115 LDAP_LOG ( OPERATION, ERR,
116 "==>bdb_modrdn: txn_begin failed: %s (%d)\n",
117 db_strerror(rc), rc, 0 );
119 Debug( LDAP_DEBUG_TRACE,
120 "bdb_delete: txn_begin failed: %s (%d)\n",
121 db_strerror(rc), rc, 0 );
124 text = "internal error";
128 locker = TXN_ID ( ltid );
131 opinfo.boi_txn = ltid;
133 op->o_private = &opinfo;
136 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
142 case DB_LOCK_DEADLOCK:
143 case DB_LOCK_NOTGRANTED:
146 text = "ldap server busy";
150 text = "internal error";
155 char* matched_dn = NULL;
158 if( matched != NULL ) {
159 matched_dn = ch_strdup( matched->e_dn );
160 refs = is_entry_referral( matched )
161 ? get_entry_referrals( be, conn, op, matched )
163 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched);
167 refs = referral_rewrite( default_referral,
168 NULL, dn, LDAP_SCOPE_DEFAULT );
171 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
172 matched_dn, NULL, refs, NULL );
174 ber_bvarray_free( refs );
180 if (!manageDSAit && is_entry_referral( e ) ) {
181 /* parent is a referral, don't allow add */
182 /* parent is an alias, don't allow add */
183 BerVarray refs = get_entry_referrals( be,
187 LDAP_LOG ( OPERATION, DETAIL1,
188 "==>bdb_modrdn: entry %s is referral \n", e->e_dn, 0, 0 );
190 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
194 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
195 e->e_dn, NULL, refs, NULL );
197 ber_bvarray_free( refs );
201 if ( be_issuffix( be, &e->e_nname ) ) {
202 p_ndn = slap_empty_bv;
204 dnParent( &e->e_nname, &p_ndn );
207 if ( p_ndn.bv_len != 0 ) {
208 /* Make sure parent entry exist and we can write its
211 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0, locker, &lock );
217 case DB_LOCK_DEADLOCK:
218 case DB_LOCK_NOTGRANTED:
221 text = "ldap server busy";
225 text = "internal error";
231 LDAP_LOG ( OPERATION, ERR,
232 "==>bdb_modrdn: parent does not exist\n", 0, 0, 0 );
234 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
241 /* check parent for "children" acl */
242 if ( ! access_allowed( be, conn, op, p,
243 children, NULL, ACL_WRITE, NULL ) )
246 LDAP_LOG ( OPERATION, ERR,
247 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
249 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
252 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
253 NULL, NULL, NULL, NULL );
258 LDAP_LOG ( OPERATION, DETAIL1,
259 "==>bdb_modrdn: wr to children %s is OK\n", p_ndn.bv_val, 0, 0 );
261 Debug( LDAP_DEBUG_TRACE,
262 "bdb_modrdn: wr to children of entry %s OK\n",
263 p_ndn.bv_val, 0, 0 );
266 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
267 p_dn = slap_empty_bv;
269 dnParent( &e->e_name, &p_dn );
273 LDAP_LOG ( OPERATION, DETAIL1,
274 "==>bdb_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
276 Debug( LDAP_DEBUG_TRACE,
277 "bdb_modrdn: parent dn=%s\n",
282 /* no parent, modrdn entry directly under root */
283 isroot = be_isroot( be, &op->o_ndn );
285 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
286 || be_isupdate( be, &op->o_ndn ) ) {
288 p = (Entry *)&slap_entry_root;
290 /* check parent for "children" acl */
291 rc = access_allowed( be, conn, op, p,
292 children, NULL, ACL_WRITE, NULL );
299 LDAP_LOG ( OPERATION, ERR,
300 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
302 Debug( LDAP_DEBUG_TRACE,
303 "no access to parent\n",
306 send_ldap_result( conn, op,
307 LDAP_INSUFFICIENT_ACCESS,
308 NULL, NULL, NULL, NULL );
313 LDAP_LOG ( OPERATION, DETAIL1,
314 "==>bdb_modrdn: wr to children of entry \"%s\" OK\n",
317 Debug( LDAP_DEBUG_TRACE,
318 "bdb_modrdn: wr to children of entry \"\" OK\n",
326 LDAP_LOG ( OPERATION, DETAIL1,
327 "==>bdb_modrdn: parent dn=\"\" \n", 0, 0, 0 );
329 Debug( LDAP_DEBUG_TRACE,
330 "bdb_modrdn: parent dn=\"\"\n",
336 LDAP_LOG ( OPERATION, ERR,
337 "==>bdb_modrdn: no parent, not root &\"\" is not "
338 "suffix\n", 0, 0, 0 );
340 Debug( LDAP_DEBUG_TRACE,
341 "bdb_modrdn: no parent, not root "
342 "& \"\" is not suffix\n",
345 rc = LDAP_INSUFFICIENT_ACCESS;
351 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
353 if ( newSuperior != NULL ) {
355 LDAP_LOG ( OPERATION, DETAIL1,
356 "==>bdb_modrdn: new parent \"%s\" requested...\n",
357 newSuperior->bv_val, 0, 0 );
359 Debug( LDAP_DEBUG_TRACE,
360 "bdb_modrdn: new parent \"%s\" requested...\n",
361 newSuperior->bv_val, 0, 0 );
364 if ( newSuperior->bv_len ) {
366 np_ndn = nnewSuperior;
368 /* newSuperior == oldParent?, if so ==> ERROR */
369 /* newSuperior == entry being moved?, if so ==> ERROR */
370 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
372 rc = bdb_dn2entry_r( be, ltid, nnewSuperior, &np, NULL, 0, locker, &lock );
378 case DB_LOCK_DEADLOCK:
379 case DB_LOCK_NOTGRANTED:
382 text = "ldap server busy";
386 text = "internal error";
392 LDAP_LOG ( OPERATION, DETAIL1,
393 "==>bdb_modrdn: newSup(ndn=%s) not here!\n",
394 np_ndn->bv_val, 0, 0 );
396 Debug( LDAP_DEBUG_TRACE,
397 "bdb_modrdn: newSup(ndn=%s) not here!\n",
398 np_ndn->bv_val, 0, 0);
405 LDAP_LOG ( OPERATION, DETAIL1,
406 "==>bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
407 np, (long) np->e_id, 0 );
409 Debug( LDAP_DEBUG_TRACE,
410 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
411 np, (long) np->e_id, 0 );
414 /* check newSuperior for "children" acl */
415 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE, NULL ) ) {
417 LDAP_LOG ( OPERATION, DETAIL1,
418 "==>bdb_modrdn: no wr to newSup children\n", 0, 0, 0 );
420 Debug( LDAP_DEBUG_TRACE,
421 "bdb_modrdn: no wr to newSup children\n",
424 rc = LDAP_INSUFFICIENT_ACCESS;
429 if ( is_entry_alias( np ) ) {
430 /* parent is an alias, don't allow add */
432 LDAP_LOG ( OPERATION, DETAIL1,
433 "==>bdb_modrdn: entry is alias\n", 0, 0, 0 );
435 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
439 rc = LDAP_ALIAS_PROBLEM;
444 if ( is_entry_referral( np ) ) {
445 /* parent is a referral, don't allow add */
447 LDAP_LOG ( OPERATION, DETAIL1,
448 "==>bdb_modrdn: entry is referral\n", 0, 0, 0 );
450 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
459 if ( isroot == -1 ) {
460 isroot = be_isroot( be, &op->o_ndn );
465 /* no parent, modrdn entry directly under root */
467 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
468 || be_isupdate( be, &op->o_ndn ) ) {
469 np = (Entry *)&slap_entry_root;
471 /* check parent for "children" acl */
472 rc = access_allowed( be, conn, op, np,
473 children, NULL, ACL_WRITE, NULL );
480 LDAP_LOG ( OPERATION, ERR,
481 "==>bdb_modrdn: no access to superior\n", 0, 0, 0 );
483 Debug( LDAP_DEBUG_TRACE,
484 "no access to new superior\n",
487 send_ldap_result( conn, op,
488 LDAP_INSUFFICIENT_ACCESS,
489 NULL, NULL, NULL, NULL );
494 LDAP_LOG ( OPERATION, DETAIL1,
495 "bdb_modrdn: wr to children entry \"\" OK\n", 0, 0, 0 );
497 Debug( LDAP_DEBUG_TRACE,
498 "bdb_modrdn: wr to children of entry \"\" OK\n",
504 LDAP_LOG ( OPERATION, ERR,
505 "bdb_modrdn: new superior=\"\", not root & \"\" "
506 "is not suffix\n", 0, 0, 0 );
508 Debug( LDAP_DEBUG_TRACE,
509 "bdb_modrdn: new superior=\"\", not root "
510 "& \"\" is not suffix\n",
513 rc = LDAP_INSUFFICIENT_ACCESS;
519 LDAP_LOG ( OPERATION, DETAIL1,
520 "bdb_modrdn: new superior=\"\"\n", 0, 0, 0 );
522 Debug( LDAP_DEBUG_TRACE,
523 "bdb_modrdn: new superior=\"\"\n",
529 LDAP_LOG ( OPERATION, DETAIL1,
530 "bdb_modrdn: wr to new parent's children OK\n", 0, 0, 0 );
532 Debug( LDAP_DEBUG_TRACE,
533 "bdb_modrdn: wr to new parent's children OK\n",
537 new_parent_dn = np_dn;
540 /* Build target dn and make sure target entry doesn't exist already. */
541 build_new_dn( &new_dn, new_parent_dn, newrdn );
543 dnNormalize2( NULL, &new_dn, &new_ndn );
546 LDAP_LOG ( OPERATION, RESULTS,
547 "bdb_modrdn: new ndn=%s\n", new_ndn.bv_val, 0, 0 );
549 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
550 new_ndn.bv_val, 0, 0 );
553 rc = bdb_dn2id ( be, ltid, &new_ndn, &id, 0 );
555 case DB_LOCK_DEADLOCK:
556 case DB_LOCK_NOTGRANTED:
561 rc = LDAP_ALREADY_EXISTS;
565 text = "internal error";
570 LDAP_LOG ( OPERATION, ERR,
571 "bdb_modrdn: new ndn=%s does not exist\n", new_ndn.bv_val, 0, 0 );
573 Debug( LDAP_DEBUG_TRACE,
574 "bdb_modrdn: new ndn=%s does not exist\n",
575 new_ndn.bv_val, 0, 0 );
578 /* Get attribute type and attribute value of our new rdn, we will
579 * need to add that to our new entry
581 if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text,
582 LDAP_DN_FORMAT_LDAP ) )
585 LDAP_LOG ( OPERATION, ERR,
586 "bdb_modrdn: can't figure out "
587 "type(s)/values(s) of newrdn\n",
590 Debug( LDAP_DEBUG_TRACE,
591 "bdb_modrdn: can't figure out "
592 "type(s)/values(s) of newrdn\n",
595 rc = LDAP_INVALID_DN_SYNTAX;
596 text = "unknown type(s) used in RDN";
601 LDAP_LOG ( OPERATION, RESULTS,
602 "bdb_modrdn: new_rdn_type=\"%s\", "
603 "new_rdn_val=\"%s\"\n",
604 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
605 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
607 Debug( LDAP_DEBUG_TRACE,
608 "bdb_modrdn: new_rdn_type=\"%s\", "
609 "new_rdn_val=\"%s\"\n",
610 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
611 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
614 if ( deleteoldrdn ) {
615 if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
616 LDAP_DN_FORMAT_LDAP ) )
619 LDAP_LOG ( OPERATION, ERR,
620 "bdb_modrdn: can't figure out "
621 "type(s)/values(s) of old_rdn\n",
624 Debug( LDAP_DEBUG_TRACE,
625 "bdb_modrdn: can't figure out "
626 "the old_rdn type(s)/value(s)\n",
630 text = "cannot parse RDN from old DN";
635 /* prepare modlist of modifications from old/new rdn */
636 rc = slap_modrdn2mods( be, conn, op, e, old_rdn, new_rdn,
637 deleteoldrdn, &mod );
638 if ( rc != LDAP_SUCCESS ) {
643 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
646 case DB_LOCK_DEADLOCK:
647 case DB_LOCK_NOTGRANTED:
651 text = "DN index delete fail";
655 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
657 /* Binary format uses a single contiguous block, cannot
658 * free individual fields. Leave new_dn/new_ndn set so
659 * they can be individually freed later.
662 e->e_nname = new_ndn;
664 new_dn.bv_val = NULL;
665 new_ndn.bv_val = NULL;
668 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
671 case DB_LOCK_DEADLOCK:
672 case DB_LOCK_NOTGRANTED:
676 text = "DN index add failed";
681 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
682 &text, textbuf, textlen );
684 if( rc != LDAP_SUCCESS ) {
686 case DB_LOCK_DEADLOCK:
687 case DB_LOCK_NOTGRANTED:
694 rc = bdb_id2entry_update( be, ltid, e );
697 case DB_LOCK_DEADLOCK:
698 case DB_LOCK_NOTGRANTED:
702 text = "entry update failed";
707 if(( rc=TXN_ABORT( ltid )) != 0 ) {
708 text = "txn_abort (no-op) failed";
715 char gid[DB_XIDDATASIZE];
717 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
718 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
720 if(( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
721 text = "txn_prepare failed";
723 if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
724 if(( rc=TXN_ABORT( ltid )) != 0 ) {
725 text ="cache update & txn_abort failed";
728 text = "cache update failed";
732 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
733 text = "txn_commit failed";
742 op->o_private = NULL;
744 if( rc == LDAP_SUCCESS ) {
746 LDAP_LOG ( OPERATION, RESULTS,
747 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
748 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
750 Debug(LDAP_DEBUG_TRACE,
751 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
752 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
756 bdb_cache_entry_commit( e );
761 LDAP_LOG ( OPERATION, RESULTS, "bdb_modrdn: %s : %s (%d)\n",
762 text, db_strerror(rc), rc );
764 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
765 text, db_strerror(rc), rc );
771 send_ldap_result( conn, op, rc,
772 NULL, text, NULL, NULL );
774 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
775 ldap_pvt_thread_yield();
776 TXN_CHECKPOINT( bdb->bi_dbenv,
777 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
781 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
782 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
784 /* LDAP v2 supporting correct attribute handling. */
785 if ( new_rdn != NULL ) {
786 ldap_rdnfree( new_rdn );
788 if ( old_rdn != NULL ) {
789 ldap_rdnfree( old_rdn );
793 for (; mod; mod=tmp ) {
799 /* LDAP v3 Support */
801 /* free new parent and reader lock */
802 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
806 /* free parent and reader lock */
807 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
812 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
817 op->o_private = NULL;
820 return ( ( rc == LDAP_SUCCESS ) ? noop : rc );