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 ) ) {
174 p_ndn = slap_empty_bv;
176 rc = dnParent( &e->e_nname, &p_ndn );
177 if ( rc != LDAP_SUCCESS ) {
178 text = "internal error";
183 if ( p_ndn.bv_len != 0 ) {
184 /* Make sure parent entry exist and we can write its
187 rc = bdb_dn2entry_r( be, ltid, &p_ndn, &p, NULL, 0 );
193 case DB_LOCK_DEADLOCK:
194 case DB_LOCK_NOTGRANTED:
198 text = "internal error";
203 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
209 /* check parent for "children" acl */
210 if ( ! access_allowed( be, conn, op, p,
211 children, NULL, ACL_WRITE ) )
213 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
215 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
216 NULL, NULL, NULL, NULL );
220 Debug( LDAP_DEBUG_TRACE,
221 "bdb_modrdn: wr to children of entry %s OK\n",
222 p_ndn.bv_val, 0, 0 );
224 if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
225 p_dn = slap_empty_bv;
227 rc = dnParent( &e->e_name, &p_dn );
228 if ( rc != LDAP_SUCCESS ) {
229 text = "internal error";
234 Debug( LDAP_DEBUG_TRACE,
235 "bdb_modrdn: parent dn=%s\n",
239 /* no parent, modrdn entry directly under root */
240 isroot = be_isroot( be, &op->o_ndn );
242 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
243 || be_isupdate( be, &op->o_ndn ) ) {
245 p = (Entry *)&slap_entry_root;
247 /* check parent for "children" acl */
248 rc = access_allowed( be, conn, op, p,
249 children, NULL, ACL_WRITE );
255 Debug( LDAP_DEBUG_TRACE,
256 "no access to parent\n",
258 send_ldap_result( conn, op,
259 LDAP_INSUFFICIENT_ACCESS,
260 NULL, NULL, NULL, NULL );
264 Debug( LDAP_DEBUG_TRACE,
265 "bdb_modrdn: wr to children of entry \"\" OK\n",
271 Debug( LDAP_DEBUG_TRACE,
272 "bdb_modrdn: parent dn=\"\"\n",
276 Debug( LDAP_DEBUG_TRACE,
277 "bdb_modrdn: no parent, not root "
278 "& \"\" is not suffix\n",
280 rc = LDAP_INSUFFICIENT_ACCESS;
286 new_parent_dn = &p_dn; /* New Parent unless newSuperior given */
288 if ( newSuperior != NULL ) {
289 Debug( LDAP_DEBUG_TRACE,
290 "bdb_modrdn: new parent \"%s\" requested...\n",
291 newSuperior->bv_val, 0, 0 );
293 if ( newSuperior->bv_len ) {
295 np_ndn = nnewSuperior;
297 /* newSuperior == oldParent?, if so ==> ERROR */
298 /* newSuperior == entry being moved?, if so ==> ERROR */
299 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
301 rc = bdb_dn2entry_r( be, ltid, nnewSuperior, &np, NULL, 0 );
307 case DB_LOCK_DEADLOCK:
308 case DB_LOCK_NOTGRANTED:
312 text = "internal error";
317 Debug( LDAP_DEBUG_TRACE,
318 "bdb_modrdn: newSup(ndn=%s) not here!\n",
319 np_ndn->bv_val, 0, 0);
324 Debug( LDAP_DEBUG_TRACE,
325 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
326 np, (long) np->e_id, 0 );
328 /* check newSuperior for "children" acl */
329 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
330 Debug( LDAP_DEBUG_TRACE,
331 "bdb_modrdn: no wr to newSup children\n",
333 rc = LDAP_INSUFFICIENT_ACCESS;
337 if ( is_entry_alias( np ) ) {
338 /* parent is an alias, don't allow add */
339 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
342 rc = LDAP_ALIAS_PROBLEM;
346 if ( is_entry_referral( np ) ) {
347 /* parent is a referral, don't allow add */
348 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
351 rc = LDAP_OPERATIONS_ERROR;
356 if ( isroot == -1 ) {
357 isroot = be_isroot( be, &op->o_ndn );
362 /* no parent, modrdn entry directly under root */
364 if ( be_issuffix( be, (struct berval *)&slap_empty_bv )
365 || be_isupdate( be, &op->o_ndn ) ) {
366 np = (Entry *)&slap_entry_root;
368 /* check parent for "children" acl */
369 rc = access_allowed( be, conn, op, np,
370 children, NULL, ACL_WRITE );
376 Debug( LDAP_DEBUG_TRACE,
377 "no access to new superior\n",
379 send_ldap_result( conn, op,
380 LDAP_INSUFFICIENT_ACCESS,
381 NULL, NULL, NULL, NULL );
385 Debug( LDAP_DEBUG_TRACE,
386 "bdb_modrdn: wr to children of entry \"\" OK\n",
390 Debug( LDAP_DEBUG_TRACE,
391 "bdb_modrdn: new superior=\"\", not root "
392 "& \"\" is not suffix\n",
394 rc = LDAP_INSUFFICIENT_ACCESS;
399 Debug( LDAP_DEBUG_TRACE,
400 "bdb_modrdn: new superior=\"\"\n",
404 Debug( LDAP_DEBUG_TRACE,
405 "bdb_modrdn: wr to new parent's children OK\n",
408 new_parent_dn = np_dn;
411 /* Build target dn and make sure target entry doesn't exist already. */
412 build_new_dn( &new_dn, new_parent_dn, newrdn );
414 dnNormalize2( NULL, &new_dn, &new_ndn );
416 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
417 new_ndn.bv_val, 0, 0 );
419 rc = bdb_dn2id ( be, ltid, &new_ndn, &id );
421 case DB_LOCK_DEADLOCK:
422 case DB_LOCK_NOTGRANTED:
427 rc = LDAP_ALREADY_EXISTS;
431 text = "internal error";
435 Debug( LDAP_DEBUG_TRACE,
436 "bdb_modrdn: new ndn=%s does not exist\n",
437 new_ndn.bv_val, 0, 0 );
439 /* Get attribute type and attribute value of our new rdn, we will
440 * need to add that to our new entry
442 if ( ldap_str2rdn( newrdn->bv_val, &new_rdn, (char **)&text,
443 LDAP_DN_FORMAT_LDAP ) )
445 Debug( LDAP_DEBUG_TRACE,
446 "bdb_modrdn: can't figure out type(s)/values(s) "
447 "of newrdn\n", 0, 0, 0 );
448 rc = LDAP_OPERATIONS_ERROR;
449 text = "unknown type(s) used in RDN";
453 Debug( LDAP_DEBUG_TRACE,
454 "bdb_modrdn: new_rdn_type=\"%s\", new_rdn_val=\"%s\"\n",
455 new_rdn[0][0]->la_attr.bv_val, new_rdn[0][0]->la_value.bv_val, 0 );
457 if ( ldap_str2rdn( dn->bv_val, &old_rdn, (char **)&text,
458 LDAP_DN_FORMAT_LDAP ) )
460 Debug( LDAP_DEBUG_TRACE,
461 "bdb_back_modrdn: can't figure out the old_rdn "
462 "type(s)/value(s)\n", 0, 0, 0 );
464 text = "cannot parse RDN from old DN";
469 if ( newSuperior == NULL
470 && charray_strcasecmp( ( const char ** )old_rdn_types,
471 ( const char ** )new_rdn_types ) != 0 ) {
472 /* Not a big deal but we may say something */
473 Debug( LDAP_DEBUG_TRACE,
474 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
476 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
480 /* Add new attribute values to the entry */
481 for ( a_cnt = 0; new_rdn[0][ a_cnt ]; a_cnt++ ) {
483 AttributeDescription *desc = NULL;
484 Modifications *mod_tmp;
486 rc = slap_bv2ad( &new_rdn[0][ a_cnt ]->la_attr, &desc, &text );
488 if ( rc != LDAP_SUCCESS ) {
489 Debug( LDAP_DEBUG_TRACE,
490 "bdb_modrdn: %s: %s (new)\n",
491 text, new_rdn[0][ a_cnt ]->la_attr.bv_val, 0 );
495 /* ACL check of newly added attrs */
496 if ( !access_allowed( be, conn, op, e, desc,
497 &new_rdn[0][ a_cnt ]->la_value, ACL_WRITE ) ) {
498 Debug( LDAP_DEBUG_TRACE,
499 "bdb_modrdn: access to attr \"%s\" "
500 "(new) not allowed\n",
501 new_rdn[0][ a_cnt ]->la_attr.bv_val, 0, 0 );
502 rc = LDAP_INSUFFICIENT_ACCESS;
506 /* Apply modification */
507 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
508 + 2 * sizeof( struct berval ) );
509 mod_tmp->sml_desc = desc;
510 mod_tmp->sml_bvalues = ( BerVarray )( mod_tmp + 1 );
511 mod_tmp->sml_bvalues[ 0 ] = new_rdn[0][ a_cnt ]->la_value;
512 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
513 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
514 mod_tmp->sml_next = mod;
518 /* Remove old rdn value if required */
519 if ( deleteoldrdn ) {
520 /* Get value of old rdn */
521 if ( old_rdn == NULL) {
522 Debug( LDAP_DEBUG_TRACE,
523 "bdb_modrdn: can't figure out old RDN value(s) "
524 "from old RDN\n", 0, 0, 0 );
526 text = "could not parse value(s) from old RDN";
530 for ( d_cnt = 0; old_rdn[0][ d_cnt ]; d_cnt++ ) {
532 AttributeDescription *desc = NULL;
533 Modifications *mod_tmp;
535 rc = slap_bv2ad( &old_rdn[0][ d_cnt ]->la_attr,
538 if ( rc != LDAP_SUCCESS ) {
539 Debug( LDAP_DEBUG_TRACE,
540 "bdb_modrdn: %s: %s (old)\n",
541 text, old_rdn[0][ d_cnt ]->la_attr.bv_val, 0 );
545 /* ACL check of newly added attrs */
546 if ( !access_allowed( be, conn, op, e, desc,
547 &old_rdn[0][d_cnt]->la_value, ACL_WRITE ) ) {
548 Debug( LDAP_DEBUG_TRACE,
549 "bdb_modrdn: access to attr \"%s\" "
550 "(old) not allowed\n",
551 old_rdn[0][ d_cnt ]->la_attr.bv_val, 0, 0 );
552 rc = LDAP_INSUFFICIENT_ACCESS;
556 /* Apply modification */
557 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications )
558 + 2 * sizeof ( struct berval ) );
559 mod_tmp->sml_desc = desc;
560 mod_tmp->sml_bvalues = ( BerVarray )(mod_tmp+1);
561 mod_tmp->sml_bvalues[ 0 ] = old_rdn[0][ d_cnt ]->la_value;
562 mod_tmp->sml_bvalues[ 1 ].bv_val = NULL;
563 mod_tmp->sml_op = LDAP_MOD_DELETE;
564 mod_tmp->sml_next = mod;
570 rc = bdb_dn2id_delete( be, ltid, p_ndn.bv_val, e );
573 case DB_LOCK_DEADLOCK:
574 case DB_LOCK_NOTGRANTED:
578 text = "DN index delete fail";
582 (void) bdb_cache_delete_entry(&bdb->bi_cache, e);
584 /* Binary format uses a single contiguous block, cannot
585 * free individual fields. Leave new_dn/new_ndn set so
586 * they can be individually freed later.
589 e->e_nname = new_ndn;
591 new_dn.bv_val = NULL;
592 new_ndn.bv_val = NULL;
595 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
598 case DB_LOCK_DEADLOCK:
599 case DB_LOCK_NOTGRANTED:
603 text = "DN index add failed";
608 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
609 &text, textbuf, textlen );
611 if( rc != LDAP_SUCCESS ) {
613 case DB_LOCK_DEADLOCK:
614 case DB_LOCK_NOTGRANTED:
621 rc = bdb_id2entry_update( be, ltid, e );
624 case DB_LOCK_DEADLOCK:
625 case DB_LOCK_NOTGRANTED:
629 text = "entry update failed";
634 rc = txn_commit( ltid, 0 );
637 op->o_private = NULL;
640 Debug( LDAP_DEBUG_TRACE,
641 "bdb_modrdn: txn_commit failed: %s (%d)\n",
642 db_strerror(rc), rc, 0 );
644 text = "commit failed";
646 (void) bdb_cache_update_entry(&bdb->bi_cache, e);
647 Debug( LDAP_DEBUG_TRACE,
648 "bdb_modrdn: added id=%08lx dn=\"%s\"\n",
649 e->e_id, e->e_dn, 0 );
652 bdb_cache_entry_commit( e );
656 send_ldap_result( conn, op, rc,
657 NULL, text, NULL, NULL );
659 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
660 ldap_pvt_thread_yield();
661 TXN_CHECKPOINT( bdb->bi_dbenv,
662 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
666 if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
667 if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
669 /* LDAP v2 supporting correct attribute handling. */
670 if( new_rdn != NULL ) ldap_rdnfree( new_rdn );
671 if( old_rdn != NULL ) ldap_rdnfree( old_rdn );
674 for (; mod; mod=tmp ) {
680 /* LDAP v3 Support */
682 /* free new parent and reader lock */
683 bdb_cache_return_entry_r(&bdb->bi_cache, np);
687 /* free parent and reader lock */
688 bdb_cache_return_entry_r(&bdb->bi_cache, p);
693 bdb_cache_return_entry_w( &bdb->bi_cache, e );
698 op->o_private = NULL;