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>
16 static char bdb_gid[DB_XIDDATASIZE];
25 struct berval *newrdn,
26 struct berval *nnewrdn,
28 struct berval *newSuperior,
29 struct berval *nnewSuperior )
31 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
32 AttributeDescription *children = slap_schema.si_ad_children;
33 struct berval p_dn, p_ndn;
34 struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
40 char textbuf[SLAP_TEXT_BUFLEN];
41 size_t textlen = sizeof textbuf;
43 struct bdb_op_info opinfo;
47 LDAPRDN *new_rdn = NULL;
48 LDAPRDN *old_rdn = NULL;
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 );
60 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
61 dn->bv_val, newrdn->bv_val,
62 newSuperior ? newSuperior->bv_val : "NULL" );
65 if( newSuperior != NULL ) {
66 rc = LDAP_UNWILLING_TO_PERFORM;
67 text = "newSuperior not implemented (yet)";
73 retry: /* transaction retry */
75 bdb_cache_delete_entry(&bdb->bi_cache, e);
76 bdb_cache_return_entry_w(&bdb->bi_cache, e);
79 bdb_cache_return_entry_r(&bdb->bi_cache, p);
82 bdb_cache_return_entry_r(&bdb->bi_cache, np);
84 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
85 rc = TXN_ABORT( ltid );
90 text = "internal error";
93 ldap_pvt_thread_yield();
96 /* begin transaction */
97 rc = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
101 Debug( LDAP_DEBUG_TRACE,
102 "bdb_delete: txn_begin failed: %s (%d)\n",
103 db_strerror(rc), rc, 0 );
105 text = "internal error";
110 opinfo.boi_txn = ltid;
112 op->o_private = &opinfo;
115 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, 0 );
121 case DB_LOCK_DEADLOCK:
122 case DB_LOCK_NOTGRANTED:
126 text = "internal error";
131 char* matched_dn = NULL;
134 if( matched != NULL ) {
135 matched_dn = ch_strdup( matched->e_dn );
136 refs = is_entry_referral( matched )
137 ? get_entry_referrals( be, conn, op, matched )
139 bdb_cache_return_entry_r( &bdb->bi_cache, matched );
143 refs = referral_rewrite( default_referral,
144 NULL, dn, LDAP_SCOPE_DEFAULT );
147 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
148 matched_dn, NULL, refs, NULL );
150 ber_bvarray_free( refs );
156 if (!manageDSAit && is_entry_referral( e ) ) {
157 /* parent is a referral, don't allow add */
158 /* parent is an alias, don't allow add */
159 BerVarray refs = get_entry_referrals( be,
162 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
165 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
166 e->e_dn, NULL, refs, NULL );
168 ber_bvarray_free( refs );
172 if ( be_issuffix( be, &e->e_nname ) ) {
173 p_ndn = slap_empty_bv;
175 dnParent( &e->e_nname, &p_ndn );
178 if ( p_ndn.bv_len != 0 ) {
179 /* Make sure parent entry exist and we can write its
182 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0 );
188 case DB_LOCK_DEADLOCK:
189 case DB_LOCK_NOTGRANTED:
193 text = "internal error";
198 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
204 /* check parent for "children" acl */
205 if ( ! access_allowed( be, conn, op, p,
206 children, NULL, ACL_WRITE, NULL ) )
208 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
210 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
211 NULL, NULL, NULL, NULL );
215 Debug( LDAP_DEBUG_TRACE,
216 "bdb_modrdn: wr to children of entry %s OK\n",
217 p_ndn.bv_val, 0, 0 );
219 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
220 p_dn = slap_empty_bv;
222 dnParent( &e->e_name, &p_dn );
225 Debug( LDAP_DEBUG_TRACE,
226 "bdb_modrdn: parent dn=%s\n",
230 /* no parent, modrdn entry directly under root */
231 isroot = be_isroot( be, &op->o_ndn );
233 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
234 || be_isupdate( be, &op->o_ndn ) ) {
236 p = (Entry *)&slap_entry_root;
238 /* check parent for "children" acl */
239 rc = access_allowed( be, conn, op, p,
240 children, NULL, ACL_WRITE, NULL );
246 Debug( LDAP_DEBUG_TRACE,
247 "no access to parent\n",
249 send_ldap_result( conn, op,
250 LDAP_INSUFFICIENT_ACCESS,
251 NULL, NULL, NULL, NULL );
255 Debug( LDAP_DEBUG_TRACE,
256 "bdb_modrdn: wr to children of entry \"\" OK\n",
262 Debug( LDAP_DEBUG_TRACE,
263 "bdb_modrdn: parent dn=\"\"\n",
267 Debug( LDAP_DEBUG_TRACE,
268 "bdb_modrdn: no parent, not root "
269 "& \"\" is not suffix\n",
271 rc = LDAP_INSUFFICIENT_ACCESS;
277 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
279 if ( newSuperior != NULL ) {
280 Debug( LDAP_DEBUG_TRACE,
281 "bdb_modrdn: new parent \"%s\" requested...\n",
282 newSuperior->bv_val, 0, 0 );
284 if ( newSuperior->bv_len ) {
286 np_ndn = nnewSuperior;
288 /* newSuperior == oldParent?, if so ==> ERROR */
289 /* newSuperior == entry being moved?, if so ==> ERROR */
290 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
292 rc = bdb_dn2entry_r( be, ltid, nnewSuperior, &np, NULL, 0 );
298 case DB_LOCK_DEADLOCK:
299 case DB_LOCK_NOTGRANTED:
303 text = "internal error";
308 Debug( LDAP_DEBUG_TRACE,
309 "bdb_modrdn: newSup(ndn=%s) not here!\n",
310 np_ndn->bv_val, 0, 0);
315 Debug( LDAP_DEBUG_TRACE,
316 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
317 np, (long) np->e_id, 0 );
319 /* check newSuperior for "children" acl */
320 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE, NULL ) ) {
321 Debug( LDAP_DEBUG_TRACE,
322 "bdb_modrdn: no wr to newSup children\n",
324 rc = LDAP_INSUFFICIENT_ACCESS;
329 if ( is_entry_alias( np ) ) {
330 /* parent is an alias, don't allow add */
331 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
334 rc = LDAP_ALIAS_PROBLEM;
339 if ( is_entry_referral( np ) ) {
340 /* parent is a referral, don't allow add */
341 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
344 rc = LDAP_OPERATIONS_ERROR;
349 if ( isroot == -1 ) {
350 isroot = be_isroot( be, &op->o_ndn );
355 /* no parent, modrdn entry directly under root */
357 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
358 || be_isupdate( be, &op->o_ndn ) ) {
359 np = (Entry *)&slap_entry_root;
361 /* check parent for "children" acl */
362 rc = access_allowed( be, conn, op, np,
363 children, NULL, ACL_WRITE, NULL );
369 Debug( LDAP_DEBUG_TRACE,
370 "no access to new superior\n",
372 send_ldap_result( conn, op,
373 LDAP_INSUFFICIENT_ACCESS,
374 NULL, NULL, NULL, NULL );
378 Debug( LDAP_DEBUG_TRACE,
379 "bdb_modrdn: wr to children of entry \"\" OK\n",
383 Debug( LDAP_DEBUG_TRACE,
384 "bdb_modrdn: new superior=\"\", not root "
385 "& \"\" is not suffix\n",
387 rc = LDAP_INSUFFICIENT_ACCESS;
392 Debug( LDAP_DEBUG_TRACE,
393 "bdb_modrdn: new superior=\"\"\n",
397 Debug( LDAP_DEBUG_TRACE,
398 "bdb_modrdn: wr to new parent's children OK\n",
401 new_parent_dn = np_dn;
404 /* Build target dn and make sure target entry doesn't exist already. */
405 build_new_dn( &new_dn, new_parent_dn, newrdn );
407 dnNormalize2( NULL, &new_dn, &new_ndn );
409 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
410 new_ndn.bv_val, 0, 0 );
412 rc = bdb_dn2id ( be, ltid, &new_ndn, &id );
414 case DB_LOCK_DEADLOCK:
415 case DB_LOCK_NOTGRANTED:
420 rc = LDAP_ALREADY_EXISTS;
424 text = "internal error";
428 Debug( LDAP_DEBUG_TRACE,
429 "bdb_modrdn: new ndn=%s does not exist\n",
430 new_ndn.bv_val, 0, 0 );
432 /* Get attribute type and attribute value of our new rdn, we will
433 * need to add that to our new entry
435 if ( ldap_str2rdn( newrdn->bv_val, &new_rdn, (char **)&text,
436 LDAP_DN_FORMAT_LDAP ) )
438 Debug( LDAP_DEBUG_TRACE,
439 "bdb_modrdn: can't figure out type(s)/values(s) "
440 "of newrdn\n", 0, 0, 0 );
441 rc = LDAP_OPERATIONS_ERROR;
442 text = "unknown type(s) used in RDN";
446 Debug( LDAP_DEBUG_TRACE,
447 "bdb_modrdn: new_rdn_type=\"%s\", new_rdn_val=\"%s\"\n",
448 new_rdn[0][0]->la_attr.bv_val, new_rdn[0][0]->la_value.bv_val, 0 );
450 if ( ldap_str2rdn( dn->bv_val, &old_rdn, (char **)&text,
451 LDAP_DN_FORMAT_LDAP ) )
453 Debug( LDAP_DEBUG_TRACE,
454 "bdb_back_modrdn: can't figure out the old_rdn "
455 "type(s)/value(s)\n", 0, 0, 0 );
457 text = "cannot parse RDN from old DN";
462 if ( newSuperior == NULL
463 && charray_strcasecmp( ( const char ** )old_rdn_types,
464 ( const char ** )new_rdn_types ) != 0 ) {
465 /* Not a big deal but we may say something */
466 Debug( LDAP_DEBUG_TRACE,
467 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
469 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
473 /* Add new attribute values to the entry */
474 for ( a_cnt = 0; new_rdn[0][ a_cnt ]; a_cnt++ ) {
476 AttributeDescription *desc = NULL;
477 Modifications *mod_tmp;
479 rc = slap_bv2ad( &new_rdn[0][ a_cnt ]->la_attr, &desc, &text );
481 if ( rc != LDAP_SUCCESS ) {
482 Debug( LDAP_DEBUG_TRACE,
483 "bdb_modrdn: %s: %s (new)\n",
484 text, new_rdn[0][ a_cnt ]->la_attr.bv_val, 0 );
488 /* ACL check of newly added attrs */
489 if ( !access_allowed( be, conn, op, e, desc,
490 &new_rdn[0][ a_cnt ]->la_value, ACL_WRITE, NULL ) ) {
491 Debug( LDAP_DEBUG_TRACE,
492 "bdb_modrdn: access to attr \"%s\" "
493 "(new) not allowed\n",
494 new_rdn[0][ a_cnt ]->la_attr.bv_val, 0, 0 );
495 rc = LDAP_INSUFFICIENT_ACCESS;
499 /* Apply modification */
500 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
501 + 2 * sizeof( struct berval ) );
502 mod_tmp->sml_desc = desc;
503 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
504 mod_tmp->sml_bvalues[ 0 ] = new_rdn[0][ a_cnt ]->la_value;
505 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
506 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
507 mod_tmp->sml_next = mod;
511 /* Remove old rdn value if required */
512 if ( deleteoldrdn ) {
513 /* Get value of old rdn */
514 if ( old_rdn == NULL) {
515 Debug( LDAP_DEBUG_TRACE,
516 "bdb_modrdn: can't figure out old RDN value(s) "
517 "from old RDN\n", 0, 0, 0 );
519 text = "could not parse value(s) from old RDN";
523 for ( d_cnt = 0; old_rdn[0][ d_cnt ]; d_cnt++ ) {
525 AttributeDescription *desc = NULL;
526 Modifications *mod_tmp;
528 rc = slap_bv2ad( &old_rdn[0][ d_cnt ]->la_attr,
531 if ( rc != LDAP_SUCCESS ) {
532 Debug( LDAP_DEBUG_TRACE,
533 "bdb_modrdn: %s: %s (old)\n",
534 text, old_rdn[0][ d_cnt ]->la_attr.bv_val, 0 );
538 /* ACL check of newly added attrs */
539 if ( !access_allowed( be, conn, op, e, desc,
540 &old_rdn[0][d_cnt]->la_value, ACL_WRITE, NULL ) ) {
541 Debug( LDAP_DEBUG_TRACE,
542 "bdb_modrdn: access to attr \"%s\" "
543 "(old) not allowed\n",
544 old_rdn[0][ d_cnt ]->la_attr.bv_val, 0, 0 );
545 rc = LDAP_INSUFFICIENT_ACCESS;
549 /* Apply modification */
550 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
551 + 2 * sizeof ( struct berval ) );
552 mod_tmp->sml_desc = desc;
553 mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
554 mod_tmp->sml_bvalues[ 0 ] = old_rdn[0][ d_cnt ]->la_value;
555 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
556 mod_tmp->sml_op = LDAP_MOD_DELETE;
557 mod_tmp->sml_next = mod;
563 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
566 case DB_LOCK_DEADLOCK:
567 case DB_LOCK_NOTGRANTED:
571 text = "DN index delete fail";
575 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
577 /* Binary format uses a single contiguous block, cannot
578 * free individual fields. Leave new_dn/new_ndn set so
579 * they can be individually freed later.
582 e->e_nname = new_ndn;
584 new_dn.bv_val = NULL;
585 new_ndn.bv_val = NULL;
588 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
591 case DB_LOCK_DEADLOCK:
592 case DB_LOCK_NOTGRANTED:
596 text = "DN index add failed";
601 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
602 &text, textbuf, textlen );
604 if( rc != LDAP_SUCCESS ) {
606 case DB_LOCK_DEADLOCK:
607 case DB_LOCK_NOTGRANTED:
614 rc = bdb_id2entry_update( be, ltid, e );
617 case DB_LOCK_DEADLOCK:
618 case DB_LOCK_NOTGRANTED:
622 text = "entry update failed";
627 if(( rc=TXN_ABORT( ltid )) != 0 ) {
628 text = "txn_abort (no-op) failed";
634 if(( rc=TXN_PREPARE( ltid, bdb_gid )) != 0 ) {
635 text = "txn_prepare failed";
637 if( bdb_cache_update_entry(&bdb->bi_cache, e) == -1 ) {
638 if(( rc=TXN_ABORT( ltid )) != 0 ) {
639 text ="cache update & txn_abort failed";
642 text = "cache update failed";
646 if(( rc=TXN_COMMIT( ltid, 0 )) != 0 ) {
647 text = "txn_commit failed";
656 op->o_private = NULL;
658 if( rc == LDAP_SUCCESS ) {
659 Debug(LDAP_DEBUG_TRACE,
660 "bdb_modrdn: rdn modified%s id=%08lx dn=\"%s\"\n",
661 op->o_noop ? " (no-op)" : "", e->e_id, e->e_dn );
663 bdb_cache_entry_commit( e );
666 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
667 text, db_strerror(rc), rc );
672 send_ldap_result( conn, op, rc,
673 NULL, text, NULL, NULL );
675 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
676 ldap_pvt_thread_yield();
677 TXN_CHECKPOINT( bdb->bi_dbenv,
678 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
682 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
683 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
685 /* LDAP v2 supporting correct attribute handling. */
686 if( new_rdn != NULL ) ldap_rdnfree( new_rdn );
687 if( old_rdn != NULL ) ldap_rdnfree( old_rdn );
690 for (; mod; mod=tmp ) {
696 /* LDAP v3 Support */
698 /* free new parent and reader lock */
699 bdb_cache_return_entry_r(&bdb->bi_cache, np);
703 /* free parent and reader lock */
704 bdb_cache_return_entry_r(&bdb->bi_cache, p);
709 bdb_cache_return_entry_w( &bdb->bi_cache, e );
714 op->o_private = NULL;