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
30 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
31 AttributeDescription *children = slap_schema.si_ad_children;
32 struct berval p_dn, p_ndn;
33 struct berval new_dn = {0, NULL}, new_ndn = {0, NULL};
39 char textbuf[SLAP_TEXT_BUFLEN];
40 size_t textlen = sizeof textbuf;
42 struct bdb_op_info opinfo;
46 LDAPRDN *new_rdn = NULL;
47 LDAPRDN *old_rdn = NULL;
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 );
59 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
60 dn->bv_val, newrdn->bv_val,
61 newSuperior ? newSuperior->bv_val : "NULL" );
64 if( newSuperior != NULL ) {
65 rc = LDAP_UNWILLING_TO_PERFORM;
66 text = "newSuperior not implemented (yet)";
72 retry: /* transaction retry */
74 bdb_cache_delete_entry(&bdb->bi_cache, e);
75 bdb_cache_return_entry_w(&bdb->bi_cache, e);
78 bdb_cache_return_entry_r(&bdb->bi_cache, p);
81 bdb_cache_return_entry_r(&bdb->bi_cache, np);
83 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
84 rc = txn_abort( ltid );
89 text = "internal error";
92 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";
111 opinfo.boi_txn = ltid;
113 op->o_private = &opinfo;
116 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, 0 );
122 case DB_LOCK_DEADLOCK:
123 case DB_LOCK_NOTGRANTED:
127 text = "internal error";
132 char* matched_dn = NULL;
135 if( matched != NULL ) {
136 matched_dn = ch_strdup( matched->e_dn );
137 refs = is_entry_referral( matched )
138 ? get_entry_referrals( be, conn, op, matched )
140 bdb_cache_return_entry_r( &bdb->bi_cache, matched );
144 refs = referral_rewrite( default_referral,
145 NULL, dn, LDAP_SCOPE_DEFAULT );
148 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
149 matched_dn, NULL, refs, NULL );
151 ber_bvarray_free( refs );
157 if (!manageDSAit && is_entry_referral( e ) ) {
158 /* parent is a referral, don't allow add */
159 /* parent is an alias, don't allow add */
160 BerVarray refs = get_entry_referrals( be,
163 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
166 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
167 e->e_dn, NULL, refs, NULL );
169 ber_bvarray_free( refs );
173 if ( be_issuffix( be, e->e_nname.bv_val ) ) {
177 rc = dnParent( e->e_nname.bv_val, (const char **)&p_ndn.bv_val );
178 if ( rc != LDAP_SUCCESS ) {
179 text = "internal error";
182 p_ndn.bv_len = e->e_nname.bv_len - (p_ndn.bv_val - e->e_nname.bv_val);
185 if ( p_ndn.bv_len != 0 ) {
186 /* Make sure parent entry exist and we can write its
189 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0 );
195 case DB_LOCK_DEADLOCK:
196 case DB_LOCK_NOTGRANTED:
200 text = "internal error";
205 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
211 /* check parent for "children" acl */
212 if ( ! access_allowed( be, conn, op, p,
213 children, NULL, ACL_WRITE ) )
215 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
217 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
218 NULL, NULL, NULL, NULL );
222 Debug( LDAP_DEBUG_TRACE,
223 "bdb_modrdn: wr to children of entry %s OK\n",
224 p_ndn.bv_val, 0, 0 );
226 if ( be_issuffix( be, e->e_name.bv_val ) ) {
230 rc = dnParent( e->e_name.bv_val, &p_dn.bv_val );
231 if ( rc != LDAP_SUCCESS ) {
232 text = "internal error";
235 p_dn.bv_len = e->e_name.bv_len - (p_dn.bv_val - e->e_name.bv_val);
238 Debug( LDAP_DEBUG_TRACE,
239 "bdb_modrdn: parent dn=%s\n",
243 /* no parent, modrdn entry directly under root */
244 isroot = be_isroot( be, &op->o_ndn );
246 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
248 p = (Entry *)&slap_entry_root;
250 /* check parent for "children" acl */
251 rc = access_allowed( be, conn, op, p,
252 children, NULL, ACL_WRITE );
258 Debug( LDAP_DEBUG_TRACE,
259 "no access to parent\n",
261 send_ldap_result( conn, op,
262 LDAP_INSUFFICIENT_ACCESS,
263 NULL, NULL, NULL, NULL );
267 Debug( LDAP_DEBUG_TRACE,
268 "bdb_modrdn: wr to children of entry \"\" OK\n",
274 Debug( LDAP_DEBUG_TRACE,
275 "bdb_modrdn: parent dn=\"\"\n",
279 Debug( LDAP_DEBUG_TRACE,
280 "bdb_modrdn: no parent, not root "
281 "& \"\" is not suffix\n",
283 rc = LDAP_INSUFFICIENT_ACCESS;
289 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
291 if ( newSuperior != NULL ) {
292 Debug( LDAP_DEBUG_TRACE,
293 "bdb_modrdn: new parent \"%s\" requested...\n",
294 newSuperior->bv_val, 0, 0 );
296 if ( newSuperior->bv_len ) {
298 np_ndn = nnewSuperior;
300 /* newSuperior == oldParent?, if so ==> ERROR */
301 /* newSuperior == entry being moved?, if so ==> ERROR */
302 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
304 rc = bdb_dn2entry_r( be, ltid, nnewSuperior, &np, NULL, 0 );
310 case DB_LOCK_DEADLOCK:
311 case DB_LOCK_NOTGRANTED:
315 text = "internal error";
320 Debug( LDAP_DEBUG_TRACE,
321 "bdb_modrdn: newSup(ndn=%s) not here!\n",
322 np_ndn->bv_val, 0, 0);
327 Debug( LDAP_DEBUG_TRACE,
328 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
329 np, (long) np->e_id, 0 );
331 /* check newSuperior for "children" acl */
332 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
333 Debug( LDAP_DEBUG_TRACE,
334 "bdb_modrdn: no wr to newSup children\n",
336 rc = LDAP_INSUFFICIENT_ACCESS;
340 if ( is_entry_alias( np ) ) {
341 /* parent is an alias, don't allow add */
342 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
345 rc = LDAP_ALIAS_PROBLEM;
349 if ( is_entry_referral( np ) ) {
350 /* parent is a referral, don't allow add */
351 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
354 rc = LDAP_OPERATIONS_ERROR;
359 if ( isroot == -1 ) {
360 isroot = be_isroot( be, &op->o_ndn );
365 /* no parent, modrdn entry directly under root */
367 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
368 np = (Entry *)&slap_entry_root;
370 /* check parent for "children" acl */
371 rc = access_allowed( be, conn, op, np,
372 children, NULL, ACL_WRITE );
378 Debug( LDAP_DEBUG_TRACE,
379 "no access to new superior\n",
381 send_ldap_result( conn, op,
382 LDAP_INSUFFICIENT_ACCESS,
383 NULL, NULL, NULL, NULL );
387 Debug( LDAP_DEBUG_TRACE,
388 "bdb_modrdn: wr to children of entry \"\" OK\n",
392 Debug( LDAP_DEBUG_TRACE,
393 "bdb_modrdn: new superior=\"\", not root "
394 "& \"\" is not suffix\n",
396 rc = LDAP_INSUFFICIENT_ACCESS;
401 Debug( LDAP_DEBUG_TRACE,
402 "bdb_modrdn: new superior=\"\"\n",
406 Debug( LDAP_DEBUG_TRACE,
407 "bdb_modrdn: wr to new parent's children OK\n",
410 new_parent_dn = np_dn;
413 /* Build target dn and make sure target entry doesn't exist already. */
414 build_new_dn( &new_dn, new_parent_dn, newrdn );
416 dnNormalize2( NULL, &new_dn, &new_ndn );
418 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
419 new_ndn.bv_val, 0, 0 );
421 rc = bdb_dn2id ( be, ltid, &new_ndn, &id );
423 case DB_LOCK_DEADLOCK:
424 case DB_LOCK_NOTGRANTED:
429 rc = LDAP_ALREADY_EXISTS;
433 text = "internal error";
437 Debug( LDAP_DEBUG_TRACE,
438 "bdb_modrdn: new ndn=%s does not exist\n",
439 new_ndn.bv_val, 0, 0 );
441 /* Get attribute type and attribute value of our new rdn, we will
442 * need to add that to our new entry
444 if ( ldap_str2rdn( newrdn->bv_val, &new_rdn, (char **)&text,
445 LDAP_DN_FORMAT_LDAP ) )
447 Debug( LDAP_DEBUG_TRACE,
448 "bdb_modrdn: can't figure out type(s)/values(s) "
449 "of newrdn\n", 0, 0, 0 );
450 rc = LDAP_OPERATIONS_ERROR;
451 text = "unknown type(s) used in RDN";
455 Debug( LDAP_DEBUG_TRACE,
456 "bdb_modrdn: new_rdn_type=\"%s\", new_rdn_val=\"%s\"\n",
457 new_rdn[0][0]->la_attr.bv_val, new_rdn[0][0]->la_value.bv_val, 0 );
459 if ( ldap_str2rdn( dn->bv_val, &old_rdn, (char **)&text,
460 LDAP_DN_FORMAT_LDAP ) )
462 Debug( LDAP_DEBUG_TRACE,
463 "bdb_back_modrdn: can't figure out the old_rdn "
464 "type(s)/value(s)\n", 0, 0, 0 );
466 text = "cannot parse RDN from old DN";
471 if ( newSuperior == NULL
472 && charray_strcasecmp( ( const char ** )old_rdn_types,
473 ( const char ** )new_rdn_types ) != 0 ) {
474 /* Not a big deal but we may say something */
475 Debug( LDAP_DEBUG_TRACE,
476 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
478 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
482 /* Add new attribute values to the entry */
483 for ( a_cnt = 0; new_rdn[0][ a_cnt ]; a_cnt++ ) {
485 AttributeDescription *desc = NULL;
486 Modifications *mod_tmp;
488 rc = slap_bv2ad( &new_rdn[0][ a_cnt ]->la_attr, &desc, &text );
490 if ( rc != LDAP_SUCCESS ) {
491 Debug( LDAP_DEBUG_TRACE,
492 "bdb_modrdn: %s: %s (new)\n",
493 text, new_rdn[0][ a_cnt ]->la_attr.bv_val, 0 );
497 /* ACL check of newly added attrs */
498 if ( !access_allowed( be, conn, op, e, desc,
499 &new_rdn[0][ a_cnt ]->la_value, ACL_WRITE ) ) {
500 Debug( LDAP_DEBUG_TRACE,
501 "bdb_modrdn: access to attr \"%s\" "
502 "(new) not allowed\n",
503 new_rdn[0][ a_cnt ]->la_attr.bv_val, 0, 0 );
504 rc = LDAP_INSUFFICIENT_ACCESS;
508 /* Apply modification */
509 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
510 + 2 * sizeof( struct berval ) );
511 mod_tmp->sml_desc = desc;
512 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
513 mod_tmp->sml_bvalues[ 0 ] = new_rdn[0][ a_cnt ]->la_value;
514 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
515 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
516 mod_tmp->sml_next = mod;
520 /* Remove old rdn value if required */
521 if ( deleteoldrdn ) {
522 /* Get value of old rdn */
523 if ( old_rdn == NULL) {
524 Debug( LDAP_DEBUG_TRACE,
525 "bdb_modrdn: can't figure out old RDN value(s) "
526 "from old RDN\n", 0, 0, 0 );
528 text = "could not parse value(s) from old RDN";
532 for ( d_cnt = 0; old_rdn[0][ d_cnt ]; d_cnt++ ) {
534 AttributeDescription *desc = NULL;
535 Modifications *mod_tmp;
537 rc = slap_bv2ad( &old_rdn[0][ d_cnt ]->la_attr,
540 if ( rc != LDAP_SUCCESS ) {
541 Debug( LDAP_DEBUG_TRACE,
542 "bdb_modrdn: %s: %s (old)\n",
543 text, old_rdn[0][ d_cnt ]->la_attr.bv_val, 0 );
547 /* ACL check of newly added attrs */
548 if ( !access_allowed( be, conn, op, e, desc,
549 &old_rdn[0][d_cnt]->la_value, ACL_WRITE ) ) {
550 Debug( LDAP_DEBUG_TRACE,
551 "bdb_modrdn: access to attr \"%s\" "
552 "(old) not allowed\n",
553 old_rdn[0][ d_cnt ]->la_attr.bv_val, 0, 0 );
554 rc = LDAP_INSUFFICIENT_ACCESS;
558 /* Apply modification */
559 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
560 + 2 * sizeof ( struct berval ) );
561 mod_tmp->sml_desc = desc;
562 mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
563 mod_tmp->sml_bvalues[ 0 ] = old_rdn[0][ d_cnt ]->la_value;
564 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
565 mod_tmp->sml_op = LDAP_MOD_DELETE;
566 mod_tmp->sml_next = mod;
572 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
575 case DB_LOCK_DEADLOCK:
576 case DB_LOCK_NOTGRANTED:
580 text = "DN index delete fail";
584 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
586 /* Binary format uses a single contiguous block, cannot
587 * free individual fields. Leave new_dn/new_ndn set so
588 * they can be individually freed later.
591 e->e_nname = new_ndn;
593 new_dn.bv_val = NULL;
594 new_ndn.bv_val = NULL;
597 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
600 case DB_LOCK_DEADLOCK:
601 case DB_LOCK_NOTGRANTED:
605 text = "DN index add failed";
610 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
611 &text, textbuf, textlen );
613 if( rc != LDAP_SUCCESS ) {
615 case DB_LOCK_DEADLOCK:
616 case DB_LOCK_NOTGRANTED:
623 rc = bdb_id2entry_update( be, ltid, e );
626 case DB_LOCK_DEADLOCK:
627 case DB_LOCK_NOTGRANTED:
631 text = "entry update failed";
636 rc = txn_commit( ltid, 0 );
639 op->o_private = NULL;
642 Debug( LDAP_DEBUG_TRACE,
643 "bdb_modrdn: txn_commit failed: %s (%d)\n",
644 db_strerror(rc), rc, 0 );
646 text = "commit failed";
648 (void) bdb_cache_update_entry(&bdb->bi_cache, e);
649 Debug( LDAP_DEBUG_TRACE,
650 "bdb_modrdn: added id=%08lx dn=\"%s\"\n",
651 e->e_id, e->e_dn, 0 );
654 bdb_cache_entry_commit( e );
658 send_ldap_result( conn, op, rc,
659 NULL, text, NULL, NULL );
661 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
662 ldap_pvt_thread_yield();
663 TXN_CHECKPOINT( bdb->bi_dbenv,
664 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
668 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
669 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
671 /* LDAP v2 supporting correct attribute handling. */
672 if( new_rdn != NULL ) ldap_rdnfree( new_rdn );
673 if( old_rdn != NULL ) ldap_rdnfree( old_rdn );
676 for (; mod; mod=tmp ) {
682 /* LDAP v3 Support */
684 /* free new parent and reader lock */
685 bdb_cache_return_entry_r(&bdb->bi_cache, np);
689 /* free parent and reader lock */
690 bdb_cache_return_entry_r(&bdb->bi_cache, p);
695 bdb_cache_return_entry_w( &bdb->bi_cache, e );
700 op->o_private = NULL;