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>
25 const char *newSuperior
28 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
29 AttributeDescription *children = slap_schema.si_ad_children;
30 char *p_dn = NULL, *p_ndn = NULL;
31 char *new_dn = NULL, *new_ndn = NULL;
37 char textbuf[SLAP_TEXT_BUFLEN];
38 size_t textlen = sizeof textbuf;
40 struct bdb_op_info opinfo;
43 char **new_rdn_vals = NULL; /* Vals of new rdn */
44 char **new_rdn_types = NULL; /* Types of new rdn */
46 char *old_rdn = NULL; /* Old rdn's attr type & val */
47 char **old_rdn_types = NULL; /* Types of old rdn attr. */
48 char **old_rdn_vals = NULL; /* Old rdn attribute values */
50 Entry *np = NULL; /* newSuperior Entry */
51 char *np_dn = NULL; /* newSuperior dn */
52 char *np_ndn = NULL; /* newSuperior ndn */
53 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
55 /* Used to interface with bdb_modify_internal() */
56 Modifications *mod = NULL; /* Used to delete old rdn */
58 int manageDSAit = get_manageDSAit( op );
60 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
61 dn, newrdn, (newSuperior ? newSuperior : "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,
128 dn, LDAP_SCOPE_DEFAULT )
130 bdb_entry_return( be, matched );
134 refs = referral_rewrite( default_referral,
135 NULL, dn, LDAP_SCOPE_DEFAULT );
138 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
139 matched_dn, NULL, refs, NULL );
141 ber_bvecfree( refs );
147 if (!manageDSAit && is_entry_referral( e ) ) {
148 /* parent is a referral, don't allow add */
149 /* parent is an alias, don't allow add */
150 struct berval **refs = get_entry_referrals( be,
151 conn, op, e, dn, LDAP_SCOPE_DEFAULT );
153 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
156 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
157 e->e_dn, NULL, refs, NULL );
159 ber_bvecfree( refs );
163 p_ndn = dn_parent( be, e->e_ndn );
164 if ( p_ndn != NULL && p_ndn[ 0 ] != '\0' ) {
165 /* Make sure parent entry exist and we can write its
169 rc = bdb_dn2entry( be, ltid, p_ndn, &p, NULL, 0 );
175 case DB_LOCK_DEADLOCK:
176 case DB_LOCK_NOTGRANTED:
180 text = "internal error";
185 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
191 /* check parent for "children" acl */
192 if ( ! access_allowed( be, conn, op, p,
193 children, NULL, ACL_WRITE ) )
195 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
197 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
198 NULL, NULL, NULL, NULL );
202 Debug( LDAP_DEBUG_TRACE,
203 "bdb_modrdn: wr to children of entry %s OK\n",
206 p_dn = dn_parent( be, e->e_dn );
208 Debug( LDAP_DEBUG_TRACE,
209 "bdb_modrdn: parent dn=%s\n",
213 /* no parent, modrdn entry directly under root */
214 isroot = be_isroot( be, op->o_ndn );
216 if ( be_issuffix( be, "" ) || be_isupdate( be, op->o_ndn ) ) {
218 p = (Entry *)&slap_entry_root;
220 /* check parent for "children" acl */
221 rc = access_allowed( be, conn, op, p,
222 children, NULL, ACL_WRITE );
228 Debug( LDAP_DEBUG_TRACE,
229 "no access to parent\n",
231 send_ldap_result( conn, op,
232 LDAP_INSUFFICIENT_ACCESS,
233 NULL, NULL, NULL, NULL );
237 Debug( LDAP_DEBUG_TRACE,
238 "bdb_modrdn: wr to children of entry \"\" OK\n",
241 p_dn = ch_strdup( "" );
243 Debug( LDAP_DEBUG_TRACE,
244 "bdb_modrdn: parent dn=\"\"\n",
248 Debug( LDAP_DEBUG_TRACE,
249 "bdb_modrdn: no parent, not root "
250 "& \"\" is not suffix\n",
252 rc = LDAP_INSUFFICIENT_ACCESS;
258 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
260 if ( newSuperior != NULL ) {
261 Debug( LDAP_DEBUG_TRACE,
262 "bdb_modrdn: new parent \"%s\" requested...\n",
265 if ( newSuperior[ 0 ] != '\0' ) {
266 np_dn = ch_strdup( newSuperior );
267 np_ndn = ch_strdup( np_dn );
268 (void) dn_normalize( np_ndn );
270 /* newSuperior == oldParent?, if so ==> ERROR */
271 /* newSuperior == entry being moved?, if so ==> ERROR */
272 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
274 rc = bdb_dn2entry( be, ltid, np_ndn, &np, NULL, 0 );
280 case DB_LOCK_DEADLOCK:
281 case DB_LOCK_NOTGRANTED:
285 text = "internal error";
290 Debug( LDAP_DEBUG_TRACE,
291 "bdb_modrdn: newSup(ndn=%s) not here!\n",
297 Debug( LDAP_DEBUG_TRACE,
298 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
299 np, (long) np->e_id, 0 );
301 /* check newSuperior for "children" acl */
302 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
303 Debug( LDAP_DEBUG_TRACE,
304 "bdb_modrdn: no wr to newSup children\n",
306 rc = LDAP_INSUFFICIENT_ACCESS;
310 if ( is_entry_alias( np ) ) {
311 /* parent is an alias, don't allow add */
312 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
315 rc = LDAP_ALIAS_PROBLEM;
319 if ( is_entry_referral( np ) ) {
320 /* parent is a referral, don't allow add */
321 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
324 rc = LDAP_OPERATIONS_ERROR;
329 if ( isroot == -1 ) {
330 isroot = be_isroot( be, op->o_ndn );
333 np_dn = ch_strdup( "" );
335 /* no parent, modrdn entry directly under root */
337 if ( be_issuffix( be, "" ) || be_isupdate( be, op->o_ndn ) ) {
339 np = (Entry *)&slap_entry_root;
341 /* check parent for "children" acl */
342 rc = access_allowed( be, conn, op, np,
343 children, NULL, ACL_WRITE );
349 Debug( LDAP_DEBUG_TRACE,
350 "no access to new superior\n",
352 send_ldap_result( conn, op,
353 LDAP_INSUFFICIENT_ACCESS,
354 NULL, NULL, NULL, NULL );
358 Debug( LDAP_DEBUG_TRACE,
359 "bdb_modrdn: wr to children of entry \"\" OK\n",
363 Debug( LDAP_DEBUG_TRACE,
364 "bdb_modrdn: new superior=\"\", not root "
365 "& \"\" is not suffix\n",
367 rc = LDAP_INSUFFICIENT_ACCESS;
372 Debug( LDAP_DEBUG_TRACE,
373 "bdb_modrdn: new superior=\"\"\n",
377 Debug( LDAP_DEBUG_TRACE,
378 "bdb_modrdn: wr to new parent's children OK\n",
381 new_parent_dn = np_dn;
384 /* Build target dn and make sure target entry doesn't exist already. */
385 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
387 new_ndn = ch_strdup(new_dn);
388 (void) dn_normalize( new_ndn );
390 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
393 rc = bdb_dn2id ( be, ltid, new_ndn, &id );
395 case DB_LOCK_DEADLOCK:
396 case DB_LOCK_NOTGRANTED:
401 rc = LDAP_ALREADY_EXISTS;
405 text = "internal error";
409 Debug( LDAP_DEBUG_TRACE,
410 "bdb_modrdn: new ndn=%s does not exist\n",
413 /* Get attribute type and attribute value of our new rdn, we will
414 * need to add that to our new entry
417 if ( rdn_attrs( newrdn, &new_rdn_types, &new_rdn_vals ) ) {
418 Debug( LDAP_DEBUG_TRACE,
419 "bdb_modrdn: can't figure out type(s)/values(s) "
420 "of newrdn\n", 0, 0, 0 );
421 rc = LDAP_OPERATIONS_ERROR;
422 text = "unknown type(s) used in RDN";
426 Debug( LDAP_DEBUG_TRACE,
427 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
428 new_rdn_vals[ 0 ], new_rdn_types[ 0 ], 0 );
430 /* Retrieve the old rdn from the entry's dn */
431 if ( ( old_rdn = dn_rdn( be, dn ) ) == NULL ) {
432 Debug( LDAP_DEBUG_TRACE,
433 "bdb_modrdn: can't figure out old_rdn from dn\n",
436 text = "could not parse old DN";
440 if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
441 Debug( LDAP_DEBUG_TRACE,
442 "bdb_back_modrdn: can't figure out the old_rdn "
443 "type(s)/value(s)\n", 0, 0, 0 );
445 text = "cannot parse RDN from old DN";
449 if ( newSuperior == NULL
450 && charray_strcasecmp( ( const char ** )old_rdn_types,
451 ( const char ** )new_rdn_types ) != 0 ) {
452 /* Not a big deal but we may say something */
453 Debug( LDAP_DEBUG_TRACE,
454 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
456 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
459 /* Add new attribute values to the entry */
460 for ( a_cnt = 0; new_rdn_types[ a_cnt ]; a_cnt++ ) {
462 AttributeDescription *desc = NULL;
463 Modifications *mod_tmp;
466 rc = slap_str2ad( new_rdn_types[ a_cnt ], &desc, &text );
468 if ( rc != LDAP_SUCCESS ) {
469 Debug( LDAP_DEBUG_TRACE,
470 "bdb_modrdn: %s: %s (new)\n",
471 text, new_rdn_types[ a_cnt ], 0 );
475 /* ACL check of newly added attrs */
476 val.bv_val = new_rdn_vals[ a_cnt ];
477 val.bv_len = strlen( val.bv_val );
478 if ( !access_allowed( be, conn, op, e,
479 desc, &val, ACL_WRITE ) ) {
480 Debug( LDAP_DEBUG_TRACE,
481 "bdb_modrdn: access to attr \"%s\" "
482 "(new) not allowed\n",
483 new_rdn_types[ a_cnt ], 0, 0 );
484 rc = LDAP_INSUFFICIENT_ACCESS;
488 /* Apply modification */
489 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
490 mod_tmp->sml_desc = desc;
491 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
492 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( new_rdn_vals[ a_cnt ] );
493 mod_tmp->sml_bvalues[ 1 ] = NULL;
494 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
495 mod_tmp->sml_next = mod;
499 /* Remove old rdn value if required */
500 if ( deleteoldrdn ) {
501 /* Get value of old rdn */
502 if ( old_rdn_vals == NULL) {
503 Debug( LDAP_DEBUG_TRACE,
504 "bdb_modrdn: can't figure out old RDN value(s) "
505 "from old RDN\n", 0, 0, 0 );
507 text = "could not parse value(s) from old RDN";
511 for ( d_cnt = 0; old_rdn_types[ d_cnt ]; d_cnt++ ) {
513 AttributeDescription *desc = NULL;
514 Modifications *mod_tmp;
517 rc = slap_str2ad( old_rdn_types[ d_cnt ],
520 if ( rc != LDAP_SUCCESS ) {
521 Debug( LDAP_DEBUG_TRACE,
522 "bdb_modrdn: %s: %s (old)\n",
523 text, old_rdn_types[ d_cnt ], 0 );
527 /* ACL check of newly added attrs */
528 val.bv_val = new_rdn_vals[ d_cnt ];
529 val.bv_len = strlen( val.bv_val );
530 if ( !access_allowed( be, conn, op, e,
531 desc, &val, ACL_WRITE ) ) {
532 Debug( LDAP_DEBUG_TRACE,
533 "bdb_modrdn: access to attr \"%s\" "
534 "(old) not allowed\n",
535 old_rdn_types[ d_cnt ], 0, 0 );
536 rc = LDAP_INSUFFICIENT_ACCESS;
540 /* Apply modification */
541 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
542 mod_tmp->sml_desc = desc;
543 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
544 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( old_rdn_vals[ d_cnt ] );
545 mod_tmp->sml_bvalues[ 1 ] = NULL;
546 mod_tmp->sml_op = LDAP_MOD_DELETE;
547 mod_tmp->sml_next = mod;
553 rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
556 case DB_LOCK_DEADLOCK:
557 case DB_LOCK_NOTGRANTED:
561 text = "DN index delete fail";
565 /* Binary format uses a single contiguous block, cannot
566 * free individual fields. Leave new_dn/new_ndn set so
567 * they can be individually freed later.
573 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
576 case DB_LOCK_DEADLOCK:
577 case DB_LOCK_NOTGRANTED:
581 text = "DN index add failed";
586 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
587 &text, textbuf, textlen );
589 if( rc != LDAP_SUCCESS ) {
591 case DB_LOCK_DEADLOCK:
592 case DB_LOCK_NOTGRANTED:
599 rc = bdb_id2entry_update( be, ltid, e );
602 case DB_LOCK_DEADLOCK:
603 case DB_LOCK_NOTGRANTED:
607 text = "entry update failed";
612 rc = txn_commit( ltid, 0 );
615 op->o_private = NULL;
618 Debug( LDAP_DEBUG_TRACE,
619 "bdb_modrdn: txn_commit failed: %s (%d)\n",
620 db_strerror(rc), rc, 0 );
622 text = "commit failed";
624 Debug( LDAP_DEBUG_TRACE,
625 "bdb_modrdn: added id=%08lx dn=\"%s\"\n",
626 e->e_id, e->e_dn, 0 );
632 send_ldap_result( conn, op, rc,
633 NULL, text, NULL, NULL );
635 if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
636 ldap_pvt_thread_yield();
637 txn_checkpoint( bdb->bi_dbenv,
638 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
642 if( new_dn != NULL ) free( new_dn );
643 if( new_ndn != NULL ) free( new_ndn );
645 if( p_dn != NULL ) free( p_dn );
646 if( p_ndn != NULL ) free( p_ndn );
648 /* LDAP v2 supporting correct attribute handling. */
649 if( new_rdn_types != NULL ) charray_free(new_rdn_types);
650 if( new_rdn_vals != NULL ) charray_free(new_rdn_vals);
651 if( old_rdn != NULL ) free(old_rdn);
652 if( old_rdn_types != NULL ) charray_free(old_rdn_types);
653 if( old_rdn_vals != NULL ) charray_free(old_rdn_vals);
654 if( mod != NULL ) slap_mods_free(mod);
656 /* LDAP v3 Support */
657 if ( np_dn != NULL ) free( np_dn );
658 if ( np_ndn != NULL ) free( np_ndn );
661 /* free new parent and writer lock */
662 bdb_entry_return( be, np );
666 /* free parent and writer lock */
667 bdb_entry_return( be, p );
672 bdb_entry_return( be, e );
677 op->o_private = NULL;