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 char *p_dn = NULL, *p_ndn = NULL;
33 char *new_dn = NULL, *new_ndn = NULL;
39 char textbuf[SLAP_TEXT_BUFLEN];
40 size_t textlen = sizeof textbuf;
42 struct bdb_op_info opinfo;
45 char **new_rdn_vals = NULL; /* Vals of new rdn */
46 char **new_rdn_types = NULL; /* Types of new rdn */
48 char *old_rdn = NULL; /* Old rdn's attr type & val */
49 char **old_rdn_types = NULL; /* Types of old rdn attr. */
50 char **old_rdn_vals = NULL; /* Old rdn attribute values */
52 Entry *np = NULL; /* newSuperior Entry */
53 char *np_dn = NULL; /* newSuperior dn */
54 char *np_ndn = NULL; /* newSuperior ndn */
55 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
57 /* Used to interface with bdb_modify_internal() */
58 Modifications *mod = NULL; /* Used to delete old rdn */
60 int manageDSAit = get_manageDSAit( op );
62 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
63 dn->bv_val, newrdn->bv_val,
64 newSuperior ? newSuperior->bv_val : "NULL" );
67 if( newSuperior != NULL ) {
68 rc = LDAP_UNWILLING_TO_PERFORM;
69 text = "newSuperior not implemented (yet)";
75 retry: /* transaction retry */
76 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
77 rc = txn_abort( ltid );
82 text = "internal error";
88 /* begin transaction */
89 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
93 Debug( LDAP_DEBUG_TRACE,
94 "bdb_delete: txn_begin failed: %s (%d)\n",
95 db_strerror(rc), rc, 0 );
97 text = "internal error";
103 opinfo.boi_txn = ltid;
105 op->o_private = &opinfo;
108 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
114 case DB_LOCK_DEADLOCK:
115 case DB_LOCK_NOTGRANTED:
119 text = "internal error";
124 char* matched_dn = NULL;
125 struct berval** refs;
127 if( matched != NULL ) {
128 matched_dn = strdup( matched->e_dn );
129 refs = is_entry_referral( matched )
130 ? get_entry_referrals( be, conn, op, matched )
132 bdb_entry_return( be, matched );
136 refs = referral_rewrite( default_referral,
137 NULL, dn, LDAP_SCOPE_DEFAULT );
140 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
141 matched_dn, NULL, refs, NULL );
143 ber_bvecfree( refs );
149 if (!manageDSAit && is_entry_referral( e ) ) {
150 /* parent is a referral, don't allow add */
151 /* parent is an alias, don't allow add */
152 struct berval **refs = get_entry_referrals( be,
155 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
158 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
159 e->e_dn, NULL, refs, NULL );
161 ber_bvecfree( refs );
165 p_ndn = dn_parent( be, e->e_ndn );
167 if ( p_ndn != NULL && p_ndn[ 0 ] != '\0' ) {
168 /* 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",
209 p_dn = dn_parent( be, e->e_dn );
211 Debug( LDAP_DEBUG_TRACE,
212 "bdb_modrdn: parent dn=%s\n",
216 /* no parent, modrdn entry directly under root */
217 isroot = be_isroot( be, &op->o_ndn );
219 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
221 p = (Entry *)&slap_entry_root;
223 /* check parent for "children" acl */
224 rc = access_allowed( be, conn, op, p,
225 children, NULL, ACL_WRITE );
231 Debug( LDAP_DEBUG_TRACE,
232 "no access to parent\n",
234 send_ldap_result( conn, op,
235 LDAP_INSUFFICIENT_ACCESS,
236 NULL, NULL, NULL, NULL );
240 Debug( LDAP_DEBUG_TRACE,
241 "bdb_modrdn: wr to children of entry \"\" OK\n",
246 Debug( LDAP_DEBUG_TRACE,
247 "bdb_modrdn: parent dn=\"\"\n",
251 Debug( LDAP_DEBUG_TRACE,
252 "bdb_modrdn: no parent, not root "
253 "& \"\" is not suffix\n",
255 rc = LDAP_INSUFFICIENT_ACCESS;
261 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
263 if ( newSuperior != NULL ) {
264 Debug( LDAP_DEBUG_TRACE,
265 "bdb_modrdn: new parent \"%s\" requested...\n",
266 newSuperior->bv_val, 0, 0 );
268 if ( newSuperior->bv_len ) {
269 np_dn = ch_strdup( newSuperior->bv_val );
270 np_ndn = ch_strdup( np_dn );
271 (void) dn_normalize( np_ndn );
273 /* newSuperior == oldParent?, if so ==> ERROR */
274 /* newSuperior == entry being moved?, if so ==> ERROR */
275 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
277 rc = bdb_dn2entry( be, ltid, np_ndn, &np, NULL, 0 );
283 case DB_LOCK_DEADLOCK:
284 case DB_LOCK_NOTGRANTED:
288 text = "internal error";
293 Debug( LDAP_DEBUG_TRACE,
294 "bdb_modrdn: newSup(ndn=%s) not here!\n",
300 Debug( LDAP_DEBUG_TRACE,
301 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
302 np, (long) np->e_id, 0 );
304 /* check newSuperior for "children" acl */
305 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
306 Debug( LDAP_DEBUG_TRACE,
307 "bdb_modrdn: no wr to newSup children\n",
309 rc = LDAP_INSUFFICIENT_ACCESS;
313 if ( is_entry_alias( np ) ) {
314 /* parent is an alias, don't allow add */
315 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
318 rc = LDAP_ALIAS_PROBLEM;
322 if ( is_entry_referral( np ) ) {
323 /* parent is a referral, don't allow add */
324 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
327 rc = LDAP_OPERATIONS_ERROR;
332 if ( isroot == -1 ) {
333 isroot = be_isroot( be, &op->o_ndn );
336 np_dn = ch_strdup( "" );
338 /* no parent, modrdn entry directly under root */
340 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
341 np = (Entry *)&slap_entry_root;
343 /* check parent for "children" acl */
344 rc = access_allowed( be, conn, op, np,
345 children, NULL, ACL_WRITE );
351 Debug( LDAP_DEBUG_TRACE,
352 "no access to new superior\n",
354 send_ldap_result( conn, op,
355 LDAP_INSUFFICIENT_ACCESS,
356 NULL, NULL, NULL, NULL );
360 Debug( LDAP_DEBUG_TRACE,
361 "bdb_modrdn: wr to children of entry \"\" OK\n",
365 Debug( LDAP_DEBUG_TRACE,
366 "bdb_modrdn: new superior=\"\", not root "
367 "& \"\" is not suffix\n",
369 rc = LDAP_INSUFFICIENT_ACCESS;
374 Debug( LDAP_DEBUG_TRACE,
375 "bdb_modrdn: new superior=\"\"\n",
379 Debug( LDAP_DEBUG_TRACE,
380 "bdb_modrdn: wr to new parent's children OK\n",
383 new_parent_dn = np_dn;
386 /* Build target dn and make sure target entry doesn't exist already. */
387 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn->bv_val );
389 new_ndn = ch_strdup( new_dn );
390 (void) dn_normalize( new_ndn );
392 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
395 rc = bdb_dn2id ( be, ltid, new_ndn, &id );
397 case DB_LOCK_DEADLOCK:
398 case DB_LOCK_NOTGRANTED:
403 rc = LDAP_ALREADY_EXISTS;
407 text = "internal error";
411 Debug( LDAP_DEBUG_TRACE,
412 "bdb_modrdn: new ndn=%s does not exist\n",
415 /* Get attribute type and attribute value of our new rdn, we will
416 * need to add that to our new entry
419 if ( rdn_attrs( newrdn->bv_val, &new_rdn_types, &new_rdn_vals ) ) {
420 Debug( LDAP_DEBUG_TRACE,
421 "bdb_modrdn: can't figure out type(s)/values(s) "
422 "of newrdn\n", 0, 0, 0 );
423 rc = LDAP_OPERATIONS_ERROR;
424 text = "unknown type(s) used in RDN";
428 Debug( LDAP_DEBUG_TRACE,
429 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
430 new_rdn_vals[0], new_rdn_types[0], 0 );
432 /* Retrieve the old rdn from the entry's dn */
433 if ( ( old_rdn = dn_rdn( be, dn->bv_val ) ) == NULL ) {
434 Debug( LDAP_DEBUG_TRACE,
435 "bdb_modrdn: can't figure out old_rdn from dn\n",
438 text = "could not parse old DN";
442 if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
443 Debug( LDAP_DEBUG_TRACE,
444 "bdb_back_modrdn: can't figure out the old_rdn "
445 "type(s)/value(s)\n", 0, 0, 0 );
447 text = "cannot parse RDN from old DN";
451 if ( newSuperior == NULL
452 && charray_strcasecmp( ( const char ** )old_rdn_types,
453 ( const char ** )new_rdn_types ) != 0 ) {
454 /* Not a big deal but we may say something */
455 Debug( LDAP_DEBUG_TRACE,
456 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
458 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
461 /* Add new attribute values to the entry */
462 for ( a_cnt = 0; new_rdn_types[ a_cnt ]; a_cnt++ ) {
464 AttributeDescription *desc = NULL;
465 Modifications *mod_tmp;
468 rc = slap_str2ad( new_rdn_types[ a_cnt ], &desc, &text );
470 if ( rc != LDAP_SUCCESS ) {
471 Debug( LDAP_DEBUG_TRACE,
472 "bdb_modrdn: %s: %s (new)\n",
473 text, new_rdn_types[ a_cnt ], 0 );
477 /* ACL check of newly added attrs */
478 val.bv_val = new_rdn_vals[ a_cnt ];
479 val.bv_len = strlen( val.bv_val );
480 if ( !access_allowed( be, conn, op, e,
481 desc, &val, ACL_WRITE ) ) {
482 Debug( LDAP_DEBUG_TRACE,
483 "bdb_modrdn: access to attr \"%s\" "
484 "(new) not allowed\n",
485 new_rdn_types[ a_cnt ], 0, 0 );
486 rc = LDAP_INSUFFICIENT_ACCESS;
490 /* Apply modification */
491 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
492 mod_tmp->sml_desc = desc;
493 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
494 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( new_rdn_vals[ a_cnt ] );
495 mod_tmp->sml_bvalues[ 1 ] = NULL;
496 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
497 mod_tmp->sml_next = mod;
501 /* Remove old rdn value if required */
502 if ( deleteoldrdn ) {
503 /* Get value of old rdn */
504 if ( old_rdn_vals == NULL) {
505 Debug( LDAP_DEBUG_TRACE,
506 "bdb_modrdn: can't figure out old RDN value(s) "
507 "from old RDN\n", 0, 0, 0 );
509 text = "could not parse value(s) from old RDN";
513 for ( d_cnt = 0; old_rdn_types[ d_cnt ]; d_cnt++ ) {
515 AttributeDescription *desc = NULL;
516 Modifications *mod_tmp;
519 rc = slap_str2ad( old_rdn_types[ d_cnt ],
522 if ( rc != LDAP_SUCCESS ) {
523 Debug( LDAP_DEBUG_TRACE,
524 "bdb_modrdn: %s: %s (old)\n",
525 text, old_rdn_types[ d_cnt ], 0 );
529 /* ACL check of newly added attrs */
530 val.bv_val = new_rdn_vals[ d_cnt ];
531 val.bv_len = strlen( val.bv_val );
532 if ( !access_allowed( be, conn, op, e,
533 desc, &val, ACL_WRITE ) ) {
534 Debug( LDAP_DEBUG_TRACE,
535 "bdb_modrdn: access to attr \"%s\" "
536 "(old) not allowed\n",
537 old_rdn_types[ d_cnt ], 0, 0 );
538 rc = LDAP_INSUFFICIENT_ACCESS;
542 /* Apply modification */
543 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
544 mod_tmp->sml_desc = desc;
545 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
546 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( old_rdn_vals[ d_cnt ] );
547 mod_tmp->sml_bvalues[ 1 ] = NULL;
548 mod_tmp->sml_op = LDAP_MOD_DELETE;
549 mod_tmp->sml_next = mod;
555 rc = bdb_dn2id_delete( be, ltid, p_ndn, e );
558 case DB_LOCK_DEADLOCK:
559 case DB_LOCK_NOTGRANTED:
563 text = "DN index delete fail";
567 /* Binary format uses a single contiguous block, cannot
568 * free individual fields. Leave new_dn/new_ndn set so
569 * they can be individually freed later.
572 e->e_name.bv_len = strlen( new_dn );
574 e->e_nname.bv_len = strlen( new_ndn );
577 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
580 case DB_LOCK_DEADLOCK:
581 case DB_LOCK_NOTGRANTED:
585 text = "DN index add failed";
590 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
591 &text, textbuf, textlen );
593 if( rc != LDAP_SUCCESS ) {
595 case DB_LOCK_DEADLOCK:
596 case DB_LOCK_NOTGRANTED:
603 rc = bdb_id2entry_update( be, ltid, e );
606 case DB_LOCK_DEADLOCK:
607 case DB_LOCK_NOTGRANTED:
611 text = "entry update failed";
616 rc = txn_commit( ltid, 0 );
619 op->o_private = NULL;
622 Debug( LDAP_DEBUG_TRACE,
623 "bdb_modrdn: txn_commit failed: %s (%d)\n",
624 db_strerror(rc), rc, 0 );
626 text = "commit failed";
628 Debug( LDAP_DEBUG_TRACE,
629 "bdb_modrdn: added id=%08lx dn=\"%s\"\n",
630 e->e_id, e->e_dn, 0 );
636 send_ldap_result( conn, op, rc,
637 NULL, text, NULL, NULL );
639 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
640 ldap_pvt_thread_yield();
641 TXN_CHECKPOINT( bdb->bi_dbenv,
642 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
646 if( new_dn != NULL ) free( new_dn );
647 if( new_ndn != NULL ) free( new_ndn );
649 if( np_ndn == p_ndn ) np_ndn = NULL;
651 /* LDAP v2 supporting correct attribute handling. */
652 if( new_rdn_types != NULL ) charray_free(new_rdn_types);
653 if( new_rdn_vals != NULL ) charray_free(new_rdn_vals);
654 if( old_rdn != NULL ) free(old_rdn);
655 if( old_rdn_types != NULL ) charray_free(old_rdn_types);
656 if( old_rdn_vals != NULL ) charray_free(old_rdn_vals);
657 if( mod != NULL ) slap_mods_free(mod);
659 /* LDAP v3 Support */
660 if ( np_dn != NULL ) free( np_dn );
661 if ( np_ndn != NULL ) free( np_ndn );
664 /* free new parent and writer lock */
665 bdb_entry_return( be, np );
669 /* free parent and writer lock */
670 bdb_entry_return( be, p );
675 bdb_entry_return( be, e );
680 op->o_private = NULL;