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;
47 char **new_rdn_vals = NULL; /* Vals of new rdn */
48 char **new_rdn_types = NULL; /* Types of new rdn */
50 char *old_rdn = NULL; /* Old rdn's attr type & val */
51 char **old_rdn_types = NULL; /* Types of old rdn attr. */
52 char **old_rdn_vals = NULL; /* Old rdn attribute values */
54 Entry *np = NULL; /* newSuperior Entry */
55 char *np_dn = NULL; /* newSuperior dn */
56 char *np_ndn = NULL; /* newSuperior ndn */
57 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
59 /* Used to interface with bdb_modify_internal() */
60 Modifications *mod = NULL; /* Used to delete old rdn */
62 int manageDSAit = get_manageDSAit( op );
64 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
65 dn->bv_val, newrdn->bv_val,
66 newSuperior ? newSuperior->bv_val : "NULL" );
69 if( newSuperior != NULL ) {
70 rc = LDAP_UNWILLING_TO_PERFORM;
71 text = "newSuperior not implemented (yet)";
77 retry: /* transaction retry */
78 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
79 rc = txn_abort( ltid );
84 text = "internal error";
90 /* begin transaction */
91 rc = txn_begin( bdb->bi_dbenv, NULL, <id,
95 Debug( LDAP_DEBUG_TRACE,
96 "bdb_delete: txn_begin failed: %s (%d)\n",
97 db_strerror(rc), rc, 0 );
99 text = "internal error";
105 opinfo.boi_txn = ltid;
107 op->o_private = &opinfo;
110 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
116 case DB_LOCK_DEADLOCK:
117 case DB_LOCK_NOTGRANTED:
121 text = "internal error";
126 char* matched_dn = NULL;
127 struct berval** refs;
129 if( matched != NULL ) {
130 matched_dn = strdup( matched->e_dn );
131 refs = is_entry_referral( matched )
132 ? get_entry_referrals( be, conn, op, matched )
134 bdb_entry_return( be, matched );
138 refs = referral_rewrite( default_referral,
139 NULL, dn, LDAP_SCOPE_DEFAULT );
142 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
143 matched_dn, NULL, refs, NULL );
145 ber_bvecfree( refs );
151 if (!manageDSAit && is_entry_referral( e ) ) {
152 /* parent is a referral, don't allow add */
153 /* parent is an alias, don't allow add */
154 struct berval **refs = get_entry_referrals( be,
157 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
160 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
161 e->e_dn, NULL, refs, NULL );
163 ber_bvecfree( refs );
167 p_ndn = dn_parent( be, e->e_ndn );
169 if ( p_ndn != NULL && p_ndn[ 0 ] != '\0' ) {
170 /* Make sure parent entry exist and we can write its
174 bv.bv_len = strlen( p_ndn );
176 rc = bdb_dn2entry( be, ltid, &bv, &p, NULL, 0 );
182 case DB_LOCK_DEADLOCK:
183 case DB_LOCK_NOTGRANTED:
187 text = "internal error";
192 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
198 /* check parent for "children" acl */
199 if ( ! access_allowed( be, conn, op, p,
200 children, NULL, ACL_WRITE ) )
202 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
204 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
205 NULL, NULL, NULL, NULL );
209 Debug( LDAP_DEBUG_TRACE,
210 "bdb_modrdn: wr to children of entry %s OK\n",
213 p_dn = dn_parent( be, 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",
250 Debug( LDAP_DEBUG_TRACE,
251 "bdb_modrdn: parent dn=\"\"\n",
255 Debug( LDAP_DEBUG_TRACE,
256 "bdb_modrdn: no parent, not root "
257 "& \"\" is not suffix\n",
259 rc = LDAP_INSUFFICIENT_ACCESS;
265 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
267 if ( newSuperior != NULL ) {
268 Debug( LDAP_DEBUG_TRACE,
269 "bdb_modrdn: new parent \"%s\" requested...\n",
270 newSuperior->bv_val, 0, 0 );
272 if ( newSuperior->bv_len ) {
273 np_dn = ch_strdup( newSuperior->bv_val );
274 np_ndn = ch_strdup( np_dn );
275 (void) dn_normalize( np_ndn );
277 /* newSuperior == oldParent?, if so ==> ERROR */
278 /* newSuperior == entry being moved?, if so ==> ERROR */
279 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
282 bv.bv_len = strlen( np_ndn );
283 rc = bdb_dn2entry( be, ltid, &bv, &np, NULL, 0 );
289 case DB_LOCK_DEADLOCK:
290 case DB_LOCK_NOTGRANTED:
294 text = "internal error";
299 Debug( LDAP_DEBUG_TRACE,
300 "bdb_modrdn: newSup(ndn=%s) not here!\n",
306 Debug( LDAP_DEBUG_TRACE,
307 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
308 np, (long) np->e_id, 0 );
310 /* check newSuperior for "children" acl */
311 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
312 Debug( LDAP_DEBUG_TRACE,
313 "bdb_modrdn: no wr to newSup children\n",
315 rc = LDAP_INSUFFICIENT_ACCESS;
319 if ( is_entry_alias( np ) ) {
320 /* parent is an alias, don't allow add */
321 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
324 rc = LDAP_ALIAS_PROBLEM;
328 if ( is_entry_referral( np ) ) {
329 /* parent is a referral, don't allow add */
330 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
333 rc = LDAP_OPERATIONS_ERROR;
338 if ( isroot == -1 ) {
339 isroot = be_isroot( be, &op->o_ndn );
342 np_dn = ch_strdup( "" );
344 /* no parent, modrdn entry directly under root */
346 if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
347 np = (Entry *)&slap_entry_root;
349 /* check parent for "children" acl */
350 rc = access_allowed( be, conn, op, np,
351 children, NULL, ACL_WRITE );
357 Debug( LDAP_DEBUG_TRACE,
358 "no access to new superior\n",
360 send_ldap_result( conn, op,
361 LDAP_INSUFFICIENT_ACCESS,
362 NULL, NULL, NULL, NULL );
366 Debug( LDAP_DEBUG_TRACE,
367 "bdb_modrdn: wr to children of entry \"\" OK\n",
371 Debug( LDAP_DEBUG_TRACE,
372 "bdb_modrdn: new superior=\"\", not root "
373 "& \"\" is not suffix\n",
375 rc = LDAP_INSUFFICIENT_ACCESS;
380 Debug( LDAP_DEBUG_TRACE,
381 "bdb_modrdn: new superior=\"\"\n",
385 Debug( LDAP_DEBUG_TRACE,
386 "bdb_modrdn: wr to new parent's children OK\n",
389 new_parent_dn = np_dn;
392 /* Build target dn and make sure target entry doesn't exist already. */
393 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn->bv_val );
395 new_ndn = ch_strdup( new_dn );
396 (void) dn_normalize( new_ndn );
398 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
402 bv.bv_len = strlen( new_ndn );
403 rc = bdb_dn2id ( be, ltid, &bv, &id );
405 case DB_LOCK_DEADLOCK:
406 case DB_LOCK_NOTGRANTED:
411 rc = LDAP_ALREADY_EXISTS;
415 text = "internal error";
419 Debug( LDAP_DEBUG_TRACE,
420 "bdb_modrdn: new ndn=%s does not exist\n",
423 /* Get attribute type and attribute value of our new rdn, we will
424 * need to add that to our new entry
427 if ( rdn_attrs( newrdn->bv_val, &new_rdn_types, &new_rdn_vals ) ) {
428 Debug( LDAP_DEBUG_TRACE,
429 "bdb_modrdn: can't figure out type(s)/values(s) "
430 "of newrdn\n", 0, 0, 0 );
431 rc = LDAP_OPERATIONS_ERROR;
432 text = "unknown type(s) used in RDN";
436 Debug( LDAP_DEBUG_TRACE,
437 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
438 new_rdn_vals[0], new_rdn_types[0], 0 );
440 /* Retrieve the old rdn from the entry's dn */
441 if ( ( old_rdn = dn_rdn( be, dn->bv_val ) ) == NULL ) {
442 Debug( LDAP_DEBUG_TRACE,
443 "bdb_modrdn: can't figure out old_rdn from dn\n",
446 text = "could not parse old DN";
450 if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
451 Debug( LDAP_DEBUG_TRACE,
452 "bdb_back_modrdn: can't figure out the old_rdn "
453 "type(s)/value(s)\n", 0, 0, 0 );
455 text = "cannot parse RDN from old DN";
459 if ( newSuperior == NULL
460 && charray_strcasecmp( ( const char ** )old_rdn_types,
461 ( const char ** )new_rdn_types ) != 0 ) {
462 /* Not a big deal but we may say something */
463 Debug( LDAP_DEBUG_TRACE,
464 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
466 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
469 /* Add new attribute values to the entry */
470 for ( a_cnt = 0; new_rdn_types[ a_cnt ]; a_cnt++ ) {
472 AttributeDescription *desc = NULL;
473 Modifications *mod_tmp;
476 rc = slap_str2ad( new_rdn_types[ a_cnt ], &desc, &text );
478 if ( rc != LDAP_SUCCESS ) {
479 Debug( LDAP_DEBUG_TRACE,
480 "bdb_modrdn: %s: %s (new)\n",
481 text, new_rdn_types[ a_cnt ], 0 );
485 /* ACL check of newly added attrs */
486 val.bv_val = new_rdn_vals[ a_cnt ];
487 val.bv_len = strlen( val.bv_val );
488 if ( !access_allowed( be, conn, op, e,
489 desc, &val, ACL_WRITE ) ) {
490 Debug( LDAP_DEBUG_TRACE,
491 "bdb_modrdn: access to attr \"%s\" "
492 "(new) not allowed\n",
493 new_rdn_types[ a_cnt ], 0, 0 );
494 rc = LDAP_INSUFFICIENT_ACCESS;
498 /* Apply modification */
499 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
500 mod_tmp->sml_desc = desc;
501 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
502 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( new_rdn_vals[ a_cnt ] );
503 mod_tmp->sml_bvalues[ 1 ] = NULL;
504 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
505 mod_tmp->sml_next = mod;
509 /* Remove old rdn value if required */
510 if ( deleteoldrdn ) {
511 /* Get value of old rdn */
512 if ( old_rdn_vals == NULL) {
513 Debug( LDAP_DEBUG_TRACE,
514 "bdb_modrdn: can't figure out old RDN value(s) "
515 "from old RDN\n", 0, 0, 0 );
517 text = "could not parse value(s) from old RDN";
521 for ( d_cnt = 0; old_rdn_types[ d_cnt ]; d_cnt++ ) {
523 AttributeDescription *desc = NULL;
524 Modifications *mod_tmp;
527 rc = slap_str2ad( old_rdn_types[ d_cnt ],
530 if ( rc != LDAP_SUCCESS ) {
531 Debug( LDAP_DEBUG_TRACE,
532 "bdb_modrdn: %s: %s (old)\n",
533 text, old_rdn_types[ d_cnt ], 0 );
537 /* ACL check of newly added attrs */
538 val.bv_val = new_rdn_vals[ d_cnt ];
539 val.bv_len = strlen( val.bv_val );
540 if ( !access_allowed( be, conn, op, e,
541 desc, &val, ACL_WRITE ) ) {
542 Debug( LDAP_DEBUG_TRACE,
543 "bdb_modrdn: access to attr \"%s\" "
544 "(old) not allowed\n",
545 old_rdn_types[ d_cnt ], 0, 0 );
546 rc = LDAP_INSUFFICIENT_ACCESS;
550 /* Apply modification */
551 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
552 mod_tmp->sml_desc = desc;
553 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
554 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( old_rdn_vals[ d_cnt ] );
555 mod_tmp->sml_bvalues[ 1 ] = NULL;
556 mod_tmp->sml_op = LDAP_MOD_DELETE;
557 mod_tmp->sml_next = mod;
563 rc = bdb_dn2id_delete( be, ltid, p_ndn, e );
566 case DB_LOCK_DEADLOCK:
567 case DB_LOCK_NOTGRANTED:
571 text = "DN index delete fail";
575 /* Binary format uses a single contiguous block, cannot
576 * free individual fields. Leave new_dn/new_ndn set so
577 * they can be individually freed later.
580 e->e_name.bv_len = strlen( new_dn );
582 e->e_nname.bv_len = strlen( new_ndn );
585 rc = bdb_dn2id_add( be, ltid, np_ndn, e );
588 case DB_LOCK_DEADLOCK:
589 case DB_LOCK_NOTGRANTED:
593 text = "DN index add failed";
598 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
599 &text, textbuf, textlen );
601 if( rc != LDAP_SUCCESS ) {
603 case DB_LOCK_DEADLOCK:
604 case DB_LOCK_NOTGRANTED:
611 rc = bdb_id2entry_update( be, ltid, e );
614 case DB_LOCK_DEADLOCK:
615 case DB_LOCK_NOTGRANTED:
619 text = "entry update failed";
624 rc = txn_commit( ltid, 0 );
627 op->o_private = NULL;
630 Debug( LDAP_DEBUG_TRACE,
631 "bdb_modrdn: txn_commit failed: %s (%d)\n",
632 db_strerror(rc), rc, 0 );
634 text = "commit failed";
636 Debug( LDAP_DEBUG_TRACE,
637 "bdb_modrdn: added id=%08lx dn=\"%s\"\n",
638 e->e_id, e->e_dn, 0 );
644 send_ldap_result( conn, op, rc,
645 NULL, text, NULL, NULL );
647 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
648 ldap_pvt_thread_yield();
649 TXN_CHECKPOINT( bdb->bi_dbenv,
650 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
654 if( new_dn != NULL ) free( new_dn );
655 if( new_ndn != NULL ) free( new_ndn );
657 if( np_ndn == p_ndn ) np_ndn = NULL;
659 /* LDAP v2 supporting correct attribute handling. */
660 if( new_rdn_types != NULL ) charray_free(new_rdn_types);
661 if( new_rdn_vals != NULL ) charray_free(new_rdn_vals);
662 if( old_rdn != NULL ) free(old_rdn);
663 if( old_rdn_types != NULL ) charray_free(old_rdn_types);
664 if( old_rdn_vals != NULL ) charray_free(old_rdn_vals);
665 if( mod != NULL ) slap_mods_free(mod);
667 /* LDAP v3 Support */
668 if ( np_dn != NULL ) free( np_dn );
669 if ( np_ndn != NULL ) free( np_ndn );
672 /* free new parent and writer lock */
673 bdb_entry_return( be, np );
677 /* free parent and writer lock */
678 bdb_entry_return( be, p );
683 bdb_entry_return( be, e );
688 op->o_private = NULL;