1 /* modrdn.c - bdb backend modrdn routine */
4 * Copyright 1998-2000 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 */
73 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
74 rc = txn_abort( ltid );
79 text = "internal error";
85 /* begin transaction */
86 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
90 Debug( LDAP_DEBUG_TRACE,
91 "bdb_delete: txn_begin failed: %s (%d)\n",
92 db_strerror(rc), rc, 0 );
94 text = "internal error";
100 opinfo.boi_txn = ltid;
102 op->o_private = &opinfo;
105 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
111 case DB_LOCK_DEADLOCK:
112 case DB_LOCK_NOTGRANTED:
116 text = "internal error";
121 char* matched_dn = NULL;
122 struct berval** refs;
124 if( matched != NULL ) {
125 matched_dn = strdup( matched->e_dn );
126 refs = is_entry_referral( matched )
127 ? get_entry_referrals( be, conn, op, matched )
129 bdb_entry_return( be, matched );
133 refs = referral_rewrite( default_referral,
134 NULL, dn, LDAP_SCOPE_DEFAULT );
137 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
138 matched_dn, NULL, refs, NULL );
140 ber_bvecfree( refs );
146 if (!manageDSAit && is_entry_referral( e ) ) {
147 /* parent is a referral, don't allow add */
148 /* parent is an alias, don't allow add */
149 struct berval **refs = get_entry_referrals( be,
152 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
155 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
156 e->e_dn, NULL, refs, NULL );
158 ber_bvecfree( refs );
162 p_ndn.bv_val = dn_parent( be, e->e_ndn );
163 if (p_ndn.bv_val && *p_ndn.bv_val)
164 p_ndn.bv_len = e->e_nname.bv_len - (p_ndn.bv_val - e->e_ndn);
168 if ( p_ndn.bv_len != 0 ) {
169 /* Make sure parent entry exist and we can write its
172 rc = bdb_dn2entry( be, ltid, &p_ndn, &p, NULL, 0 );
178 case DB_LOCK_DEADLOCK:
179 case DB_LOCK_NOTGRANTED:
183 text = "internal error";
188 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
194 /* check parent for "children" acl */
195 if ( ! access_allowed( be, conn, op, p,
196 children, NULL, ACL_WRITE ) )
198 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
200 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
201 NULL, NULL, NULL, NULL );
205 Debug( LDAP_DEBUG_TRACE,
206 "bdb_modrdn: wr to children of entry %s OK\n",
207 p_ndn.bv_val, 0, 0 );
209 p_dn.bv_val = dn_parent( be, e->e_dn );
210 if (p_dn.bv_val && *p_dn.bv_val)
211 p_dn.bv_len = e->e_name.bv_len - (p_dn.bv_val - e->e_dn);
215 Debug( LDAP_DEBUG_TRACE,
216 "bdb_modrdn: parent dn=%s\n",
220 /* no parent, modrdn entry directly under root */
221 isroot = be_isroot( be, &op->o_ndn );
223 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
225 p = (Entry *)&slap_entry_root;
227 /* check parent for "children" acl */
228 rc = access_allowed( be, conn, op, p,
229 children, NULL, ACL_WRITE );
235 Debug( LDAP_DEBUG_TRACE,
236 "no access to parent\n",
238 send_ldap_result( conn, op,
239 LDAP_INSUFFICIENT_ACCESS,
240 NULL, NULL, NULL, NULL );
244 Debug( LDAP_DEBUG_TRACE,
245 "bdb_modrdn: wr to children of entry \"\" OK\n",
251 Debug( LDAP_DEBUG_TRACE,
252 "bdb_modrdn: parent dn=\"\"\n",
256 Debug( LDAP_DEBUG_TRACE,
257 "bdb_modrdn: no parent, not root "
258 "& \"\" is not suffix\n",
260 rc = LDAP_INSUFFICIENT_ACCESS;
266 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
268 if ( newSuperior != NULL ) {
269 Debug( LDAP_DEBUG_TRACE,
270 "bdb_modrdn: new parent \"%s\" requested...\n",
271 newSuperior->bv_val, 0, 0 );
273 if ( newSuperior->bv_len ) {
275 np_ndn = nnewSuperior;
277 /* newSuperior == oldParent?, if so ==> ERROR */
278 /* newSuperior == entry being moved?, if so ==> ERROR */
279 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
281 rc = bdb_dn2entry( be, ltid, nnewSuperior, &np, NULL, 0 );
287 case DB_LOCK_DEADLOCK:
288 case DB_LOCK_NOTGRANTED:
292 text = "internal error";
297 Debug( LDAP_DEBUG_TRACE,
298 "bdb_modrdn: newSup(ndn=%s) not here!\n",
299 np_ndn->bv_val, 0, 0);
304 Debug( LDAP_DEBUG_TRACE,
305 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
306 np, (long) np->e_id, 0 );
308 /* check newSuperior for "children" acl */
309 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
310 Debug( LDAP_DEBUG_TRACE,
311 "bdb_modrdn: no wr to newSup children\n",
313 rc = LDAP_INSUFFICIENT_ACCESS;
317 if ( is_entry_alias( np ) ) {
318 /* parent is an alias, don't allow add */
319 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
322 rc = LDAP_ALIAS_PROBLEM;
326 if ( is_entry_referral( np ) ) {
327 /* parent is a referral, don't allow add */
328 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
331 rc = LDAP_OPERATIONS_ERROR;
336 if ( isroot == -1 ) {
337 isroot = be_isroot( be, &op->o_ndn );
342 /* no parent, modrdn entry directly under root */
344 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
345 np = (Entry *)&slap_entry_root;
347 /* check parent for "children" acl */
348 rc = access_allowed( be, conn, op, np,
349 children, NULL, ACL_WRITE );
355 Debug( LDAP_DEBUG_TRACE,
356 "no access to new superior\n",
358 send_ldap_result( conn, op,
359 LDAP_INSUFFICIENT_ACCESS,
360 NULL, NULL, NULL, NULL );
364 Debug( LDAP_DEBUG_TRACE,
365 "bdb_modrdn: wr to children of entry \"\" OK\n",
369 Debug( LDAP_DEBUG_TRACE,
370 "bdb_modrdn: new superior=\"\", not root "
371 "& \"\" is not suffix\n",
373 rc = LDAP_INSUFFICIENT_ACCESS;
378 Debug( LDAP_DEBUG_TRACE,
379 "bdb_modrdn: new superior=\"\"\n",
383 Debug( LDAP_DEBUG_TRACE,
384 "bdb_modrdn: wr to new parent's children OK\n",
387 new_parent_dn = np_dn;
390 /* Build target dn and make sure target entry doesn't exist already. */
391 build_new_dn( &new_dn, new_parent_dn, newrdn );
393 dnNormalize2( NULL, &new_dn, &new_ndn );
395 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
396 new_ndn.bv_val, 0, 0 );
398 rc = bdb_dn2id ( be, ltid, &new_ndn, &id );
400 case DB_LOCK_DEADLOCK:
401 case DB_LOCK_NOTGRANTED:
406 rc = LDAP_ALREADY_EXISTS;
410 text = "internal error";
414 Debug( LDAP_DEBUG_TRACE,
415 "bdb_modrdn: new ndn=%s does not exist\n",
416 new_ndn.bv_val, 0, 0 );
418 /* Get attribute type and attribute value of our new rdn, we will
419 * need to add that to our new entry
421 if ( ldap_str2rdn( newrdn->bv_val, &new_rdn, &text, LDAP_DN_FORMAT_LDAP ) ) {
422 Debug( LDAP_DEBUG_TRACE,
423 "bdb_modrdn: can't figure out type(s)/values(s) "
424 "of newrdn\n", 0, 0, 0 );
425 rc = LDAP_OPERATIONS_ERROR;
426 text = "unknown type(s) used in RDN";
430 Debug( LDAP_DEBUG_TRACE,
431 "bdb_modrdn: new_rdn_type=\"%s\", new_rdn_val=\"%s\"\n",
432 new_rdn[0][0]->la_attr.bv_val, new_rdn[0][0]->la_value.bv_val, 0 );
434 if ( ldap_str2rdn( dn->bv_val, &old_rdn, &text, LDAP_DN_FORMAT_LDAP ) ) {
435 Debug( LDAP_DEBUG_TRACE,
436 "bdb_back_modrdn: can't figure out the old_rdn "
437 "type(s)/value(s)\n", 0, 0, 0 );
439 text = "cannot parse RDN from old DN";
444 if ( newSuperior == NULL
445 && charray_strcasecmp( ( const char ** )old_rdn_types,
446 ( const char ** )new_rdn_types ) != 0 ) {
447 /* Not a big deal but we may say something */
448 Debug( LDAP_DEBUG_TRACE,
449 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
451 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
455 /* Add new attribute values to the entry */
456 for ( a_cnt = 0; new_rdn[0][ a_cnt ]; a_cnt++ ) {
458 AttributeDescription *desc = NULL;
459 Modifications *mod_tmp;
461 rc = slap_bv2ad( &new_rdn[0][ a_cnt ]->la_attr, &desc, &text );
463 if ( rc != LDAP_SUCCESS ) {
464 Debug( LDAP_DEBUG_TRACE,
465 "bdb_modrdn: %s: %s (new)\n",
466 text, new_rdn[0][ a_cnt ]->la_attr.bv_val, 0 );
470 /* ACL check of newly added attrs */
471 if ( !access_allowed( be, conn, op, e, desc,
472 &new_rdn[0][ a_cnt ]->la_value, ACL_WRITE ) ) {
473 Debug( LDAP_DEBUG_TRACE,
474 "bdb_modrdn: access to attr \"%s\" "
475 "(new) not allowed\n",
476 new_rdn[0][ a_cnt ]->la_attr.bv_val, 0, 0 );
477 rc = LDAP_INSUFFICIENT_ACCESS;
481 /* Apply modification */
482 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
483 + 2 * sizeof( struct berval * ) );
484 mod_tmp->sml_desc = desc;
485 mod_tmp->sml_bvalues = ( struct berval ** )( mod_tmp + 1 );
486 mod_tmp->sml_bvalues[ 0 ] = &new_rdn[0][ a_cnt ]->la_value;
487 mod_tmp->sml_bvalues[ 1 ] = NULL;
488 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
489 mod_tmp->sml_next = mod;
493 /* Remove old rdn value if required */
494 if ( deleteoldrdn ) {
495 /* Get value of old rdn */
496 if ( old_rdn == NULL) {
497 Debug( LDAP_DEBUG_TRACE,
498 "bdb_modrdn: can't figure out old RDN value(s) "
499 "from old RDN\n", 0, 0, 0 );
501 text = "could not parse value(s) from old RDN";
505 for ( d_cnt = 0; old_rdn[0][ d_cnt ]; d_cnt++ ) {
507 AttributeDescription *desc = NULL;
508 Modifications *mod_tmp;
510 rc = slap_bv2ad( &old_rdn[0][ d_cnt ]->la_attr,
513 if ( rc != LDAP_SUCCESS ) {
514 Debug( LDAP_DEBUG_TRACE,
515 "bdb_modrdn: %s: %s (old)\n",
516 text, old_rdn[0][ d_cnt ]->la_attr.bv_val, 0 );
520 /* ACL check of newly added attrs */
521 if ( !access_allowed( be, conn, op, e, desc,
522 &old_rdn[0][d_cnt]->la_value, ACL_WRITE ) ) {
523 Debug( LDAP_DEBUG_TRACE,
524 "bdb_modrdn: access to attr \"%s\" "
525 "(old) not allowed\n",
526 old_rdn[0][ d_cnt ]->la_attr.bv_val, 0, 0 );
527 rc = LDAP_INSUFFICIENT_ACCESS;
531 /* Apply modification */
532 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
533 + 2 * sizeof ( struct berval * ) );
534 mod_tmp->sml_desc = desc;
535 mod_tmp->sml_bvalues = ( struct berval ** )(mod_tmp+1);
536 mod_tmp->sml_bvalues[ 0 ] = &old_rdn[0][ d_cnt ]->la_value;
537 mod_tmp->sml_bvalues[ 1 ] = NULL;
538 mod_tmp->sml_op = LDAP_MOD_DELETE;
539 mod_tmp->sml_next = mod;
545 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
548 case DB_LOCK_DEADLOCK:
549 case DB_LOCK_NOTGRANTED:
553 text = "DN index delete fail";
557 /* Binary format uses a single contiguous block, cannot
558 * free individual fields. Leave new_dn/new_ndn set so
559 * they can be individually freed later.
562 e->e_nname = new_ndn;
565 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
568 case DB_LOCK_DEADLOCK:
569 case DB_LOCK_NOTGRANTED:
573 text = "DN index add failed";
578 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
579 &text, textbuf, textlen );
581 if( rc != LDAP_SUCCESS ) {
583 case DB_LOCK_DEADLOCK:
584 case DB_LOCK_NOTGRANTED:
591 rc = bdb_id2entry_update( be, ltid, e );
594 case DB_LOCK_DEADLOCK:
595 case DB_LOCK_NOTGRANTED:
599 text = "entry update failed";
604 rc = txn_commit( ltid, 0 );
607 op->o_private = NULL;
610 Debug( LDAP_DEBUG_TRACE,
611 "bdb_modrdn: txn_commit failed: %s (%d)\n",
612 db_strerror(rc), rc, 0 );
614 text = "commit failed";
616 Debug( LDAP_DEBUG_TRACE,
617 "bdb_modrdn: added id=%08lx dn=\"%s\"\n",
618 e->e_id, e->e_dn, 0 );
624 send_ldap_result( conn, op, rc,
625 NULL, text, NULL, NULL );
627 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
628 ldap_pvt_thread_yield();
629 TXN_CHECKPOINT( bdb->bi_dbenv,
630 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
634 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
635 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
637 /* LDAP v2 supporting correct attribute handling. */
638 if( new_rdn != NULL ) ldap_rdnfree( new_rdn );
639 if( old_rdn != NULL ) ldap_rdnfree( old_rdn );
642 for (; mod; mod=tmp ) {
648 /* LDAP v3 Support */
650 /* free new parent and writer lock */
651 bdb_entry_return( be, np );
655 /* free parent and writer lock */
656 bdb_entry_return( be, p );
661 bdb_entry_return( be, e );
666 op->o_private = NULL;