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 );
63 LDAP_LOG ( OPERATION, ENTRY, "==>bdb_modrdn(%s,%s,%s)\n",
64 dn->bv_val,newrdn->bv_val, newSuperior ? newSuperior->bv_val : "NULL" );
66 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
67 dn->bv_val, newrdn->bv_val,
68 newSuperior ? newSuperior->bv_val : "NULL" );
72 if( newSuperior != NULL ) {
73 rc = LDAP_UNWILLING_TO_PERFORM;
74 text = "newSuperior not implemented (yet)";
80 retry: /* transaction retry */
82 bdb_cache_delete_entry(&bdb->bi_cache, e);
83 bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
86 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
89 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
92 LDAP_LOG ( OPERATION, DETAIL1, "==>bdb_modrdn: retrying...\n", 0, 0, 0);
94 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
96 rc = TXN_ABORT( ltid );
101 text = "internal error";
104 ldap_pvt_thread_yield();
107 /* begin transaction */
108 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
109 bdb->bi_db_opflags );
113 LDAP_LOG ( OPERATION, ERR,
114 "==>bdb_modrdn: txn_begin failed: %s (%d)\n",
115 db_strerror(rc), rc, 0 );
117 Debug( LDAP_DEBUG_TRACE,
118 "bdb_delete: txn_begin failed: %s (%d)\n",
119 db_strerror(rc), rc, 0 );
122 text = "internal error";
126 locker = TXN_ID ( ltid );
129 opinfo.boi_txn = ltid;
131 op->o_private = &opinfo;
134 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, DB_RMW, locker, &lock );
140 case DB_LOCK_DEADLOCK:
141 case DB_LOCK_NOTGRANTED:
144 text = "ldap server busy";
148 text = "internal error";
153 char* matched_dn = NULL;
156 if( matched != NULL ) {
157 matched_dn = ch_strdup( matched->e_dn );
158 refs = is_entry_referral( matched )
159 ? get_entry_referrals( be, conn, op, matched )
161 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, matched);
165 refs = referral_rewrite( default_referral,
166 NULL, dn, LDAP_SCOPE_DEFAULT );
169 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
170 matched_dn, NULL, refs, NULL );
172 ber_bvarray_free( refs );
178 if (!manageDSAit && is_entry_referral( e ) ) {
179 /* parent is a referral, don't allow add */
180 /* parent is an alias, don't allow add */
181 BerVarray refs = get_entry_referrals( be,
185 LDAP_LOG ( OPERATION, DETAIL1,
186 "==>bdb_modrdn: entry %s is referral \n", e->e_dn, 0, 0 );
188 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
192 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
193 e->e_dn, NULL, refs, NULL );
195 ber_bvarray_free( refs );
199 if ( be_issuffix( be, &e->e_nname ) ) {
200 p_ndn = slap_empty_bv;
202 dnParent( &e->e_nname, &p_ndn );
205 if ( p_ndn.bv_len != 0 ) {
206 /* Make sure parent entry exist and we can write its
209 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0, locker, &lock );
215 case DB_LOCK_DEADLOCK:
216 case DB_LOCK_NOTGRANTED:
219 text = "ldap server busy";
223 text = "internal error";
229 LDAP_LOG ( OPERATION, ERR,
230 "==>bdb_modrdn: parent does not exist\n", 0, 0, 0 );
232 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
239 /* check parent for "children" acl */
240 if ( ! access_allowed( be, conn, op, p,
241 children, NULL, ACL_WRITE, NULL ) )
244 LDAP_LOG ( OPERATION, ERR,
245 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
247 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
250 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
251 NULL, NULL, NULL, NULL );
256 LDAP_LOG ( OPERATION, DETAIL1,
257 "==>bdb_modrdn: wr to children %s is OK\n", p_ndn.bv_val, 0, 0 );
259 Debug( LDAP_DEBUG_TRACE,
260 "bdb_modrdn: wr to children of entry %s OK\n",
261 p_ndn.bv_val, 0, 0 );
264 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
265 p_dn = slap_empty_bv;
267 dnParent( &e->e_name, &p_dn );
271 LDAP_LOG ( OPERATION, DETAIL1,
272 "==>bdb_modrdn: parent dn=%s\n", p_dn.bv_val, 0, 0 );
274 Debug( LDAP_DEBUG_TRACE,
275 "bdb_modrdn: parent dn=%s\n",
280 /* no parent, modrdn entry directly under root */
281 isroot = be_isroot( be, &op->o_ndn );
283 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
284 || be_isupdate( be, &op->o_ndn ) ) {
286 p = (Entry *)&slap_entry_root;
288 /* check parent for "children" acl */
289 rc = access_allowed( be, conn, op, p,
290 children, NULL, ACL_WRITE, NULL );
297 LDAP_LOG ( OPERATION, ERR,
298 "==>bdb_modrdn: no access to parent\n", 0, 0, 0 );
300 Debug( LDAP_DEBUG_TRACE,
301 "no access to parent\n",
304 send_ldap_result( conn, op,
305 LDAP_INSUFFICIENT_ACCESS,
306 NULL, NULL, NULL, NULL );
311 LDAP_LOG ( OPERATION, DETAIL1,
312 "==>bdb_modrdn: wr to children of entry \"%s\" OK\n",
315 Debug( LDAP_DEBUG_TRACE,
316 "bdb_modrdn: wr to children of entry \"\" OK\n",
324 LDAP_LOG ( OPERATION, DETAIL1,
325 "==>bdb_modrdn: parent dn=\"\" \n", 0, 0, 0 );
327 Debug( LDAP_DEBUG_TRACE,
328 "bdb_modrdn: parent dn=\"\"\n",
334 LDAP_LOG ( OPERATION, ERR,
335 "==>bdb_modrdn: no parent, not root &\"\" is not "
336 "suffix\n", 0, 0, 0 );
338 Debug( LDAP_DEBUG_TRACE,
339 "bdb_modrdn: no parent, not root "
340 "& \"\" is not suffix\n",
343 rc = LDAP_INSUFFICIENT_ACCESS;
349 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
351 if ( newSuperior != NULL ) {
353 LDAP_LOG ( OPERATION, DETAIL1,
354 "==>bdb_modrdn: new parent \"%s\" requested...\n",
355 newSuperior->bv_val, 0, 0 );
357 Debug( LDAP_DEBUG_TRACE,
358 "bdb_modrdn: new parent \"%s\" requested...\n",
359 newSuperior->bv_val, 0, 0 );
362 if ( newSuperior->bv_len ) {
364 np_ndn = nnewSuperior;
366 /* newSuperior == oldParent?, if so ==> ERROR */
367 /* newSuperior == entry being moved?, if so ==> ERROR */
368 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
370 rc = bdb_dn2entry_r( be, ltid, nnewSuperior, &np, NULL, 0, locker, &lock );
376 case DB_LOCK_DEADLOCK:
377 case DB_LOCK_NOTGRANTED:
380 text = "ldap server busy";
384 text = "internal error";
390 LDAP_LOG ( OPERATION, DETAIL1,
391 "==>bdb_modrdn: newSup(ndn=%s) not here!\n",
392 np_ndn->bv_val, 0, 0 );
394 Debug( LDAP_DEBUG_TRACE,
395 "bdb_modrdn: newSup(ndn=%s) not here!\n",
396 np_ndn->bv_val, 0, 0);
403 LDAP_LOG ( OPERATION, DETAIL1,
404 "==>bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
405 np, (long) np->e_id, 0 );
407 Debug( LDAP_DEBUG_TRACE,
408 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
409 np, (long) np->e_id, 0 );
412 /* check newSuperior for "children" acl */
413 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE, NULL ) ) {
415 LDAP_LOG ( OPERATION, DETAIL1,
416 "==>bdb_modrdn: no wr to newSup children\n", 0, 0, 0 );
418 Debug( LDAP_DEBUG_TRACE,
419 "bdb_modrdn: no wr to newSup children\n",
422 rc = LDAP_INSUFFICIENT_ACCESS;
427 if ( is_entry_alias( np ) ) {
428 /* parent is an alias, don't allow add */
430 LDAP_LOG ( OPERATION, DETAIL1,
431 "==>bdb_modrdn: entry is alias\n", 0, 0, 0 );
433 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
437 rc = LDAP_ALIAS_PROBLEM;
442 if ( is_entry_referral( np ) ) {
443 /* parent is a referral, don't allow add */
445 LDAP_LOG ( OPERATION, DETAIL1,
446 "==>bdb_modrdn: entry is referral\n", 0, 0, 0 );
448 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
457 if ( isroot == -1 ) {
458 isroot = be_isroot( be, &op->o_ndn );
463 /* no parent, modrdn entry directly under root */
465 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
466 || be_isupdate( be, &op->o_ndn ) ) {
467 np = (Entry *)&slap_entry_root;
469 /* check parent for "children" acl */
470 rc = access_allowed( be, conn, op, np,
471 children, NULL, ACL_WRITE, NULL );
478 LDAP_LOG ( OPERATION, ERR,
479 "==>bdb_modrdn: no access to superior\n", 0, 0, 0 );
481 Debug( LDAP_DEBUG_TRACE,
482 "no access to new superior\n",
485 send_ldap_result( conn, op,
486 LDAP_INSUFFICIENT_ACCESS,
487 NULL, NULL, NULL, NULL );
492 LDAP_LOG ( OPERATION, DETAIL1,
493 "bdb_modrdn: wr to children entry \"\" OK\n", 0, 0, 0 );
495 Debug( LDAP_DEBUG_TRACE,
496 "bdb_modrdn: wr to children of entry \"\" OK\n",
502 LDAP_LOG ( OPERATION, ERR,
503 "bdb_modrdn: new superior=\"\", not root & \"\" "
504 "is not suffix\n", 0, 0, 0 );
506 Debug( LDAP_DEBUG_TRACE,
507 "bdb_modrdn: new superior=\"\", not root "
508 "& \"\" is not suffix\n",
511 rc = LDAP_INSUFFICIENT_ACCESS;
517 LDAP_LOG ( OPERATION, DETAIL1,
518 "bdb_modrdn: new superior=\"\"\n", 0, 0, 0 );
520 Debug( LDAP_DEBUG_TRACE,
521 "bdb_modrdn: new superior=\"\"\n",
527 LDAP_LOG ( OPERATION, DETAIL1,
528 "bdb_modrdn: wr to new parent's children OK\n", 0, 0, 0 );
530 Debug( LDAP_DEBUG_TRACE,
531 "bdb_modrdn: wr to new parent's children OK\n",
535 new_parent_dn = np_dn;
538 /* Build target dn and make sure target entry doesn't exist already. */
539 build_new_dn( &new_dn, new_parent_dn, newrdn );
541 dnNormalize2( NULL, &new_dn, &new_ndn );
544 LDAP_LOG ( OPERATION, RESULTS,
545 "bdb_modrdn: new ndn=%s\n", new_ndn.bv_val, 0, 0 );
547 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
548 new_ndn.bv_val, 0, 0 );
551 rc = bdb_dn2id ( be, ltid, &new_ndn, &id, 0 );
553 case DB_LOCK_DEADLOCK:
554 case DB_LOCK_NOTGRANTED:
559 rc = LDAP_ALREADY_EXISTS;
563 text = "internal error";
568 LDAP_LOG ( OPERATION, ERR,
569 "bdb_modrdn: new ndn=%s does not exist\n", new_ndn.bv_val, 0, 0 );
571 Debug( LDAP_DEBUG_TRACE,
572 "bdb_modrdn: new ndn=%s does not exist\n",
573 new_ndn.bv_val, 0, 0 );
576 /* Get attribute type and attribute value of our new rdn, we will
577 * need to add that to our new entry
579 if ( ldap_bv2rdn( newrdn, &new_rdn, (char **)&text,
580 LDAP_DN_FORMAT_LDAP ) )
583 LDAP_LOG ( OPERATION, ERR,
584 "bdb_modrdn: can't figure out "
585 "type(s)/values(s) of newrdn\n",
588 Debug( LDAP_DEBUG_TRACE,
589 "bdb_modrdn: can't figure out "
590 "type(s)/values(s) of newrdn\n",
593 rc = LDAP_INVALID_DN_SYNTAX;
594 text = "unknown type(s) used in RDN";
599 LDAP_LOG ( OPERATION, RESULTS,
600 "bdb_modrdn: new_rdn_type=\"%s\", "
601 "new_rdn_val=\"%s\"\n",
602 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
603 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
605 Debug( LDAP_DEBUG_TRACE,
606 "bdb_modrdn: new_rdn_type=\"%s\", "
607 "new_rdn_val=\"%s\"\n",
608 new_rdn[ 0 ][ 0 ]->la_attr.bv_val,
609 new_rdn[ 0 ][ 0 ]->la_value.bv_val, 0 );
612 if ( deleteoldrdn ) {
613 if ( ldap_bv2rdn( dn, &old_rdn, (char **)&text,
614 LDAP_DN_FORMAT_LDAP ) )
617 LDAP_LOG ( OPERATION, ERR,
618 "bdb_modrdn: can't figure out "
619 "type(s)/values(s) of old_rdn\n",
622 Debug( LDAP_DEBUG_TRACE,
623 "bdb_modrdn: can't figure out "
624 "the old_rdn type(s)/value(s)\n",
628 text = "cannot parse RDN from old DN";
633 /* prepare modlist of modifications from old/new rdn */
634 rc = slap_modrdn2mods( be, conn, op, e, old_rdn, new_rdn,
635 deleteoldrdn, &mod );
636 if ( rc != LDAP_SUCCESS ) {
641 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
644 case DB_LOCK_DEADLOCK:
645 case DB_LOCK_NOTGRANTED:
649 text = "DN index delete fail";
653 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
655 /* Binary format uses a single contiguous block, cannot
656 * free individual fields. Leave new_dn/new_ndn set so
657 * they can be individually freed later.
660 e->e_nname = new_ndn;
662 new_dn.bv_val = NULL;
663 new_ndn.bv_val = NULL;
666 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
669 case DB_LOCK_DEADLOCK:
670 case DB_LOCK_NOTGRANTED:
674 text = "DN index add failed";
679 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
680 &text, textbuf, textlen );
682 if( rc != LDAP_SUCCESS ) {
684 case DB_LOCK_DEADLOCK:
685 case DB_LOCK_NOTGRANTED:
692 rc = bdb_id2entry_update( be, ltid, e );
695 case DB_LOCK_DEADLOCK:
696 case DB_LOCK_NOTGRANTED:
700 text = "entry update failed";
705 if(( rc=TXN_ABORT( ltid )) != 0 ) {
706 text = "txn_abort (no-op) failed";
712 char gid[DB_XIDDATASIZE];
714 snprintf( gid, sizeof( gid ), "%s-%08lx-%08lx",
715 bdb_uuid.bv_val, (long) op->o_connid, (long) op->o_opid );
717 if(( rc=TXN_PREPARE( ltid, gid )) != 0 ) {
718 text = "txn_prepare failed";
720 if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
721 if(( rc=TXN_ABORT( ltid )) != 0 ) {
722 text ="cache update & txn_abort failed";
725 text = "cache update failed";
729 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
730 text = "txn_commit failed";
739 op->o_private = NULL;
741 if( rc == LDAP_SUCCESS ) {
743 LDAP_LOG ( OPERATION, RESULTS,
744 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
745 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
747 Debug(LDAP_DEBUG_TRACE,
748 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
749 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
752 bdb_cache_entry_commit( e );
756 LDAP_LOG ( OPERATION, RESULTS, "bdb_modrdn: %s : %s (%d)\n",
757 text, db_strerror(rc), rc );
759 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
760 text, db_strerror(rc), rc );
766 send_ldap_result( conn, op, rc,
767 NULL, text, NULL, NULL );
769 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
770 ldap_pvt_thread_yield();
771 TXN_CHECKPOINT( bdb->bi_dbenv,
772 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
776 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
777 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
779 /* LDAP v2 supporting correct attribute handling. */
780 if ( new_rdn != NULL ) {
781 ldap_rdnfree( new_rdn );
783 if ( old_rdn != NULL ) {
784 ldap_rdnfree( old_rdn );
788 for (; mod; mod=tmp ) {
794 /* LDAP v3 Support */
796 /* free new parent and reader lock */
797 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
801 /* free parent and reader lock */
802 bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
807 bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
812 op->o_private = NULL;