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();
95 /* begin transaction */
96 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
100 Debug( LDAP_DEBUG_TRACE,
101 "bdb_delete: txn_begin failed: %s (%d)\n",
102 db_strerror(rc), rc, 0 );
104 text = "internal error";
109 opinfo.boi_txn = ltid;
111 op->o_private = &opinfo;
114 rc = bdb_dn2entry_w( be, ltid, ndn, &e, &matched, 0 );
120 case DB_LOCK_DEADLOCK:
121 case DB_LOCK_NOTGRANTED:
125 text = "internal error";
130 char* matched_dn = NULL;
133 if( matched != NULL ) {
134 matched_dn = ch_strdup( matched->e_dn );
135 refs = is_entry_referral( matched )
136 ? get_entry_referrals( be, conn, op, matched )
138 bdb_cache_return_entry_r( &bdb->bi_cache, matched );
142 refs = referral_rewrite( default_referral,
143 NULL, dn, LDAP_SCOPE_DEFAULT );
146 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
147 matched_dn, NULL, refs, NULL );
149 ber_bvarray_free( refs );
155 if (!manageDSAit && is_entry_referral( e ) ) {
156 /* parent is a referral, don't allow add */
157 /* parent is an alias, don't allow add */
158 BerVarray refs = get_entry_referrals( be,
161 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
164 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
165 e->e_dn, NULL, refs, NULL );
167 ber_bvarray_free( refs );
171 if ( be_issuffix( be, &e->e_nname ) ) {
172 p_ndn = slap_empty_bv;
174 dnParent( &e->e_nname, &p_ndn );
177 if ( p_ndn.bv_len != 0 ) {
178 /* Make sure parent entry exist and we can write its
181 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0 );
187 case DB_LOCK_DEADLOCK:
188 case DB_LOCK_NOTGRANTED:
192 text = "internal error";
197 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
203 /* check parent for "children" acl */
204 if ( ! access_allowed( be, conn, op, p,
205 children, NULL, ACL_WRITE ) )
207 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
209 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
210 NULL, NULL, NULL, NULL );
214 Debug( LDAP_DEBUG_TRACE,
215 "bdb_modrdn: wr to children of entry %s OK\n",
216 p_ndn.bv_val, 0, 0 );
218 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
219 p_dn = slap_empty_bv;
221 dnParent( &e->e_name, &p_dn );
224 Debug( LDAP_DEBUG_TRACE,
225 "bdb_modrdn: parent dn=%s\n",
229 /* no parent, modrdn entry directly under root */
230 isroot = be_isroot( be, &op->o_ndn );
232 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
233 || be_isupdate( be, &op->o_ndn ) ) {
235 p = (Entry *)&slap_entry_root;
237 /* check parent for "children" acl */
238 rc = access_allowed( be, conn, op, p,
239 children, NULL, ACL_WRITE );
245 Debug( LDAP_DEBUG_TRACE,
246 "no access to parent\n",
248 send_ldap_result( conn, op,
249 LDAP_INSUFFICIENT_ACCESS,
250 NULL, NULL, NULL, NULL );
254 Debug( LDAP_DEBUG_TRACE,
255 "bdb_modrdn: wr to children of entry \"\" OK\n",
261 Debug( LDAP_DEBUG_TRACE,
262 "bdb_modrdn: parent dn=\"\"\n",
266 Debug( LDAP_DEBUG_TRACE,
267 "bdb_modrdn: no parent, not root "
268 "& \"\" is not suffix\n",
270 rc = LDAP_INSUFFICIENT_ACCESS;
276 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
278 if ( newSuperior != NULL ) {
279 Debug( LDAP_DEBUG_TRACE,
280 "bdb_modrdn: new parent \"%s\" requested...\n",
281 newSuperior->bv_val, 0, 0 );
283 if ( newSuperior->bv_len ) {
285 np_ndn = nnewSuperior;
287 /* newSuperior == oldParent?, if so ==> ERROR */
288 /* newSuperior == entry being moved?, if so ==> ERROR */
289 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
291 rc = bdb_dn2entry_r( be, ltid, nnewSuperior, &np, NULL, 0 );
297 case DB_LOCK_DEADLOCK:
298 case DB_LOCK_NOTGRANTED:
302 text = "internal error";
307 Debug( LDAP_DEBUG_TRACE,
308 "bdb_modrdn: newSup(ndn=%s) not here!\n",
309 np_ndn->bv_val, 0, 0);
314 Debug( LDAP_DEBUG_TRACE,
315 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
316 np, (long) np->e_id, 0 );
318 /* check newSuperior for "children" acl */
319 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
320 Debug( LDAP_DEBUG_TRACE,
321 "bdb_modrdn: no wr to newSup children\n",
323 rc = LDAP_INSUFFICIENT_ACCESS;
328 if ( is_entry_alias( np ) ) {
329 /* parent is an alias, don't allow add */
330 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
333 rc = LDAP_ALIAS_PROBLEM;
338 if ( is_entry_referral( np ) ) {
339 /* parent is a referral, don't allow add */
340 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
343 rc = LDAP_OPERATIONS_ERROR;
348 if ( isroot == -1 ) {
349 isroot = be_isroot( be, &op->o_ndn );
354 /* no parent, modrdn entry directly under root */
356 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
357 || be_isupdate( be, &op->o_ndn ) ) {
358 np = (Entry *)&slap_entry_root;
360 /* check parent for "children" acl */
361 rc = access_allowed( be, conn, op, np,
362 children, NULL, ACL_WRITE );
368 Debug( LDAP_DEBUG_TRACE,
369 "no access to new superior\n",
371 send_ldap_result( conn, op,
372 LDAP_INSUFFICIENT_ACCESS,
373 NULL, NULL, NULL, NULL );
377 Debug( LDAP_DEBUG_TRACE,
378 "bdb_modrdn: wr to children of entry \"\" OK\n",
382 Debug( LDAP_DEBUG_TRACE,
383 "bdb_modrdn: new superior=\"\", not root "
384 "& \"\" is not suffix\n",
386 rc = LDAP_INSUFFICIENT_ACCESS;
391 Debug( LDAP_DEBUG_TRACE,
392 "bdb_modrdn: new superior=\"\"\n",
396 Debug( LDAP_DEBUG_TRACE,
397 "bdb_modrdn: wr to new parent's children OK\n",
400 new_parent_dn = np_dn;
403 /* Build target dn and make sure target entry doesn't exist already. */
404 build_new_dn( &new_dn, new_parent_dn, newrdn );
406 dnNormalize2( NULL, &new_dn, &new_ndn );
408 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
409 new_ndn.bv_val, 0, 0 );
411 rc = bdb_dn2id ( be, ltid, &new_ndn, &id );
413 case DB_LOCK_DEADLOCK:
414 case DB_LOCK_NOTGRANTED:
419 rc = LDAP_ALREADY_EXISTS;
423 text = "internal error";
427 Debug( LDAP_DEBUG_TRACE,
428 "bdb_modrdn: new ndn=%s does not exist\n",
429 new_ndn.bv_val, 0, 0 );
431 /* Get attribute type and attribute value of our new rdn, we will
432 * need to add that to our new entry
434 if ( ldap_str2rdn( newrdn->bv_val, &new_rdn, (char **)&text,
435 LDAP_DN_FORMAT_LDAP ) )
437 Debug( LDAP_DEBUG_TRACE,
438 "bdb_modrdn: can't figure out type(s)/values(s) "
439 "of newrdn\n", 0, 0, 0 );
440 rc = LDAP_OPERATIONS_ERROR;
441 text = "unknown type(s) used in RDN";
445 Debug( LDAP_DEBUG_TRACE,
446 "bdb_modrdn: new_rdn_type=\"%s\", new_rdn_val=\"%s\"\n",
447 new_rdn[0][0]->la_attr.bv_val, new_rdn[0][0]->la_value.bv_val, 0 );
449 if ( ldap_str2rdn( dn->bv_val, &old_rdn, (char **)&text,
450 LDAP_DN_FORMAT_LDAP ) )
452 Debug( LDAP_DEBUG_TRACE,
453 "bdb_back_modrdn: can't figure out the old_rdn "
454 "type(s)/value(s)\n", 0, 0, 0 );
456 text = "cannot parse RDN from old DN";
461 if ( newSuperior == NULL
462 && charray_strcasecmp( ( const char ** )old_rdn_types,
463 ( const char ** )new_rdn_types ) != 0 ) {
464 /* Not a big deal but we may say something */
465 Debug( LDAP_DEBUG_TRACE,
466 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
468 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
472 /* Add new attribute values to the entry */
473 for ( a_cnt = 0; new_rdn[0][ a_cnt ]; a_cnt++ ) {
475 AttributeDescription *desc = NULL;
476 Modifications *mod_tmp;
478 rc = slap_bv2ad( &new_rdn[0][ a_cnt ]->la_attr, &desc, &text );
480 if ( rc != LDAP_SUCCESS ) {
481 Debug( LDAP_DEBUG_TRACE,
482 "bdb_modrdn: %s: %s (new)\n",
483 text, new_rdn[0][ a_cnt ]->la_attr.bv_val, 0 );
487 /* ACL check of newly added attrs */
488 if ( !access_allowed( be, conn, op, e, desc,
489 &new_rdn[0][ a_cnt ]->la_value, ACL_WRITE ) ) {
490 Debug( LDAP_DEBUG_TRACE,
491 "bdb_modrdn: access to attr \"%s\" "
492 "(new) not allowed\n",
493 new_rdn[0][ a_cnt ]->la_attr.bv_val, 0, 0 );
494 rc = LDAP_INSUFFICIENT_ACCESS;
498 /* Apply modification */
499 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
500 + 2 * sizeof( struct berval ) );
501 mod_tmp->sml_desc = desc;
502 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
503 mod_tmp->sml_bvalues[ 0 ] = new_rdn[0][ a_cnt ]->la_value;
504 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
505 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
506 mod_tmp->sml_next = mod;
510 /* Remove old rdn value if required */
511 if ( deleteoldrdn ) {
512 /* Get value of old rdn */
513 if ( old_rdn == NULL) {
514 Debug( LDAP_DEBUG_TRACE,
515 "bdb_modrdn: can't figure out old RDN value(s) "
516 "from old RDN\n", 0, 0, 0 );
518 text = "could not parse value(s) from old RDN";
522 for ( d_cnt = 0; old_rdn[0][ d_cnt ]; d_cnt++ ) {
524 AttributeDescription *desc = NULL;
525 Modifications *mod_tmp;
527 rc = slap_bv2ad( &old_rdn[0][ d_cnt ]->la_attr,
530 if ( rc != LDAP_SUCCESS ) {
531 Debug( LDAP_DEBUG_TRACE,
532 "bdb_modrdn: %s: %s (old)\n",
533 text, old_rdn[0][ d_cnt ]->la_attr.bv_val, 0 );
537 /* ACL check of newly added attrs */
538 if ( !access_allowed( be, conn, op, e, desc,
539 &old_rdn[0][d_cnt]->la_value, ACL_WRITE ) ) {
540 Debug( LDAP_DEBUG_TRACE,
541 "bdb_modrdn: access to attr \"%s\" "
542 "(old) not allowed\n",
543 old_rdn[0][ d_cnt ]->la_attr.bv_val, 0, 0 );
544 rc = LDAP_INSUFFICIENT_ACCESS;
548 /* Apply modification */
549 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
550 + 2 * sizeof ( struct berval ) );
551 mod_tmp->sml_desc = desc;
552 mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
553 mod_tmp->sml_bvalues[ 0 ] = old_rdn[0][ d_cnt ]->la_value;
554 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
555 mod_tmp->sml_op = LDAP_MOD_DELETE;
556 mod_tmp->sml_next = mod;
562 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
565 case DB_LOCK_DEADLOCK:
566 case DB_LOCK_NOTGRANTED:
570 text = "DN index delete fail";
574 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
576 /* Binary format uses a single contiguous block, cannot
577 * free individual fields. Leave new_dn/new_ndn set so
578 * they can be individually freed later.
581 e->e_nname = new_ndn;
583 new_dn.bv_val = NULL;
584 new_ndn.bv_val = NULL;
587 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
590 case DB_LOCK_DEADLOCK:
591 case DB_LOCK_NOTGRANTED:
595 text = "DN index add failed";
600 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
601 &text, textbuf, textlen );
603 if( rc != LDAP_SUCCESS ) {
605 case DB_LOCK_DEADLOCK:
606 case DB_LOCK_NOTGRANTED:
613 rc = bdb_id2entry_update( be, ltid, e );
616 case DB_LOCK_DEADLOCK:
617 case DB_LOCK_NOTGRANTED:
621 text = "entry update failed";
626 rc = txn_abort( ltid );
628 rc = txn_commit( ltid, 0 );
631 op->o_private = NULL;
634 Debug( LDAP_DEBUG_TRACE,
635 "bdb_modrdn: txn_%s failed: %s (%d)\n",
636 op->o_noop ? "abort (no-op)" : "commit",
637 db_strerror(rc), rc );
639 text = "commit failed";
642 (void) bdb_cache_update_entry(&bdb->bi_cache, e);
643 Debug( LDAP_DEBUG_TRACE,
644 "bdb_modrdn: added%s id=%08lx dn=\"%s\"\n",
645 op->o_noop ? " (no-op)" : "",
649 bdb_cache_entry_commit( e );
653 send_ldap_result( conn, op, rc,
654 NULL, text, NULL, NULL );
656 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
657 ldap_pvt_thread_yield();
658 TXN_CHECKPOINT( bdb->bi_dbenv,
659 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
663 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
664 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
666 /* LDAP v2 supporting correct attribute handling. */
667 if( new_rdn != NULL ) ldap_rdnfree( new_rdn );
668 if( old_rdn != NULL ) ldap_rdnfree( old_rdn );
671 for (; mod; mod=tmp ) {
677 /* LDAP v3 Support */
679 /* free new parent and reader lock */
680 bdb_cache_return_entry_r(&bdb->bi_cache, np);
684 /* free parent and reader lock */
685 bdb_cache_return_entry_r(&bdb->bi_cache, p);
690 bdb_cache_return_entry_w( &bdb->bi_cache, e );
695 op->o_private = NULL;