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, 0 );
89 Debug( LDAP_DEBUG_TRACE,
90 "bdb_delete: txn_begin failed: %s (%d)\n",
91 db_strerror(rc), rc, 0 );
93 text = "internal error";
98 opinfo.boi_txn = ltid;
100 op->o_private = &opinfo;
103 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
109 case DB_LOCK_DEADLOCK:
110 case DB_LOCK_NOTGRANTED:
114 text = "internal error";
119 char* matched_dn = NULL;
120 struct berval** refs;
122 if( matched != NULL ) {
123 matched_dn = strdup( matched->e_dn );
124 refs = is_entry_referral( matched )
125 ? get_entry_referrals( be, conn, op, matched,
126 dn, LDAP_SCOPE_DEFAULT )
128 bdb_entry_return( be, matched );
132 refs = referral_rewrite( default_referral,
133 NULL, dn, LDAP_SCOPE_DEFAULT );
136 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
137 matched_dn, NULL, refs, NULL );
139 ber_bvecfree( refs );
145 if (!manageDSAit && is_entry_referral( e ) ) {
146 /* parent is a referral, don't allow add */
147 /* parent is an alias, don't allow add */
148 struct berval **refs = get_entry_referrals( be,
149 conn, op, e, dn, LDAP_SCOPE_DEFAULT );
151 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry %s is referral\n",
154 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
155 e->e_dn, NULL, refs, NULL );
157 ber_bvecfree( refs );
161 p_ndn = dn_parent( be, e->e_ndn );
162 if ( p_ndn != NULL && p_ndn[ 0 ] != '\0' ) {
163 /* Make sure parent entry exist and we can write its
167 rc = bdb_dn2entry( be, ltid, p_ndn, &p, NULL, 0 );
173 case DB_LOCK_DEADLOCK:
174 case DB_LOCK_NOTGRANTED:
178 text = "internal error";
183 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
189 /* check parent for "children" acl */
190 if ( ! access_allowed( be, conn, op, p,
191 children, NULL, ACL_WRITE ) )
193 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
195 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
196 NULL, NULL, NULL, NULL );
200 Debug( LDAP_DEBUG_TRACE,
201 "bdb_modrdn: wr to children of entry %s OK\n",
204 p_dn = dn_parent( be, e->e_dn );
206 Debug( LDAP_DEBUG_TRACE,
207 "bdb_modrdn: parent dn=%s\n",
211 /* no parent, modrdn entry directly under root */
212 isroot = be_isroot( be, op->o_ndn );
214 if ( be_issuffix( be, "" ) || be_isupdate( be, op->o_ndn ) ) {
216 p = (Entry *)&slap_entry_root;
218 /* check parent for "children" acl */
219 rc = access_allowed( be, conn, op, p,
220 children, NULL, ACL_WRITE );
226 Debug( LDAP_DEBUG_TRACE,
227 "no access to parent\n",
229 send_ldap_result( conn, op,
230 LDAP_INSUFFICIENT_ACCESS,
231 NULL, NULL, NULL, NULL );
235 Debug( LDAP_DEBUG_TRACE,
236 "bdb_modrdn: wr to children of entry \"\" OK\n",
239 p_dn = ch_strdup( "" );
241 Debug( LDAP_DEBUG_TRACE,
242 "bdb_modrdn: parent dn=\"\"\n",
246 Debug( LDAP_DEBUG_TRACE,
247 "bdb_modrdn: no parent, not root "
248 "& \"\" is not suffix\n",
250 rc = LDAP_INSUFFICIENT_ACCESS;
256 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
258 if ( newSuperior != NULL ) {
259 Debug( LDAP_DEBUG_TRACE,
260 "bdb_modrdn: new parent \"%s\" requested...\n",
263 if ( newSuperior[ 0 ] != '\0' ) {
264 np_dn = ch_strdup( newSuperior );
265 np_ndn = ch_strdup( np_dn );
266 (void) dn_normalize( np_ndn );
268 /* newSuperior == oldParent?, if so ==> ERROR */
269 /* newSuperior == entry being moved?, if so ==> ERROR */
270 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
272 rc = bdb_dn2entry( be, ltid, np_ndn, &np, NULL, 0 );
278 case DB_LOCK_DEADLOCK:
279 case DB_LOCK_NOTGRANTED:
283 text = "internal error";
288 Debug( LDAP_DEBUG_TRACE,
289 "bdb_modrdn: newSup(ndn=%s) not here!\n",
295 Debug( LDAP_DEBUG_TRACE,
296 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
297 np, (long) np->e_id, 0 );
299 /* check newSuperior for "children" acl */
300 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
301 Debug( LDAP_DEBUG_TRACE,
302 "bdb_modrdn: no wr to newSup children\n",
304 rc = LDAP_INSUFFICIENT_ACCESS;
308 if ( is_entry_alias( np ) ) {
309 /* parent is an alias, don't allow add */
310 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
313 rc = LDAP_ALIAS_PROBLEM;
317 if ( is_entry_referral( np ) ) {
318 /* parent is a referral, don't allow add */
319 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
322 rc = LDAP_OPERATIONS_ERROR;
327 if ( isroot == -1 ) {
328 isroot = be_isroot( be, op->o_ndn );
331 np_dn = ch_strdup( "" );
333 /* no parent, modrdn entry directly under root */
335 if ( be_issuffix( be, "" ) || be_isupdate( be, op->o_ndn ) ) {
337 np = (Entry *)&slap_entry_root;
339 /* check parent for "children" acl */
340 rc = access_allowed( be, conn, op, np,
341 children, NULL, ACL_WRITE );
347 Debug( LDAP_DEBUG_TRACE,
348 "no access to new superior\n",
350 send_ldap_result( conn, op,
351 LDAP_INSUFFICIENT_ACCESS,
352 NULL, NULL, NULL, NULL );
356 Debug( LDAP_DEBUG_TRACE,
357 "bdb_modrdn: wr to children of entry \"\" OK\n",
361 Debug( LDAP_DEBUG_TRACE,
362 "bdb_modrdn: new superior=\"\", not root "
363 "& \"\" is not suffix\n",
365 rc = LDAP_INSUFFICIENT_ACCESS;
370 Debug( LDAP_DEBUG_TRACE,
371 "bdb_modrdn: new superior=\"\"\n",
375 Debug( LDAP_DEBUG_TRACE,
376 "bdb_modrdn: wr to new parent's children OK\n",
379 new_parent_dn = np_dn;
382 /* Build target dn and make sure target entry doesn't exist already. */
383 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
385 new_ndn = ch_strdup(new_dn);
386 (void) dn_normalize( new_ndn );
388 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
391 rc = bdb_dn2id ( be, ltid, new_ndn, &id );
393 case DB_LOCK_DEADLOCK:
394 case DB_LOCK_NOTGRANTED:
399 rc = LDAP_ALREADY_EXISTS;
403 text = "internal error";
407 Debug( LDAP_DEBUG_TRACE,
408 "bdb_modrdn: new ndn=%s does not exist\n",
411 /* Get attribute type and attribute value of our new rdn, we will
412 * need to add that to our new entry
415 if ( rdn_attrs( newrdn, &new_rdn_types, &new_rdn_vals ) ) {
416 Debug( LDAP_DEBUG_TRACE,
417 "bdb_modrdn: can't figure out type(s)/values(s) "
418 "of newrdn\n", 0, 0, 0 );
419 rc = LDAP_OPERATIONS_ERROR;
420 text = "unknown type(s) used in RDN";
424 Debug( LDAP_DEBUG_TRACE,
425 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
426 new_rdn_vals[ 0 ], new_rdn_types[ 0 ], 0 );
428 /* Retrieve the old rdn from the entry's dn */
429 if ( ( old_rdn = dn_rdn( be, dn ) ) == NULL ) {
430 Debug( LDAP_DEBUG_TRACE,
431 "bdb_modrdn: can't figure out old_rdn from dn\n",
434 text = "could not parse old DN";
438 if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
439 Debug( LDAP_DEBUG_TRACE,
440 "bdb_back_modrdn: can't figure out the old_rdn "
441 "type(s)/value(s)\n", 0, 0, 0 );
443 text = "cannot parse RDN from old DN";
447 if ( newSuperior == NULL
448 && charray_strcasecmp( ( const char ** )old_rdn_types,
449 ( const char ** )new_rdn_types ) != 0 ) {
450 /* Not a big deal but we may say something */
451 Debug( LDAP_DEBUG_TRACE,
452 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
454 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
457 /* Add new attribute values to the entry */
458 for ( a_cnt = 0; new_rdn_types[ a_cnt ]; a_cnt++ ) {
460 AttributeDescription *desc = NULL;
461 Modifications *mod_tmp;
464 rc = slap_str2ad( new_rdn_types[ a_cnt ], &desc, &text );
466 if ( rc != LDAP_SUCCESS ) {
467 Debug( LDAP_DEBUG_TRACE,
468 "bdb_modrdn: %s: %s (new)\n",
469 text, new_rdn_types[ a_cnt ], 0 );
473 /* ACL check of newly added attrs */
474 val.bv_val = new_rdn_vals[ a_cnt ];
475 val.bv_len = strlen( val.bv_val );
476 if ( !access_allowed( be, conn, op, e,
477 desc, &val, ACL_WRITE ) ) {
478 Debug( LDAP_DEBUG_TRACE,
479 "bdb_modrdn: access to attr \"%s\" "
480 "(new) not allowed\n",
481 new_rdn_types[ a_cnt ], 0, 0 );
482 rc = LDAP_INSUFFICIENT_ACCESS;
486 /* Apply modification */
487 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
488 mod_tmp->sml_desc = desc;
489 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
490 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( new_rdn_vals[ a_cnt ] );
491 mod_tmp->sml_bvalues[ 1 ] = NULL;
492 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
493 mod_tmp->sml_next = mod;
497 /* Remove old rdn value if required */
498 if ( deleteoldrdn ) {
499 /* Get value of old rdn */
500 if ( old_rdn_vals == NULL) {
501 Debug( LDAP_DEBUG_TRACE,
502 "bdb_modrdn: can't figure out old RDN value(s) "
503 "from old RDN\n", 0, 0, 0 );
505 text = "could not parse value(s) from old RDN";
509 for ( d_cnt = 0; old_rdn_types[ d_cnt ]; d_cnt++ ) {
511 AttributeDescription *desc = NULL;
512 Modifications *mod_tmp;
515 rc = slap_str2ad( old_rdn_types[ d_cnt ],
518 if ( rc != LDAP_SUCCESS ) {
519 Debug( LDAP_DEBUG_TRACE,
520 "bdb_modrdn: %s: %s (old)\n",
521 text, old_rdn_types[ d_cnt ], 0 );
525 /* ACL check of newly added attrs */
526 val.bv_val = new_rdn_vals[ d_cnt ];
527 val.bv_len = strlen( val.bv_val );
528 if ( !access_allowed( be, conn, op, e,
529 desc, &val, ACL_WRITE ) ) {
530 Debug( LDAP_DEBUG_TRACE,
531 "bdb_modrdn: access to attr \"%s\" "
532 "(old) not allowed\n",
533 old_rdn_types[ d_cnt ], 0, 0 );
534 rc = LDAP_INSUFFICIENT_ACCESS;
538 /* Apply modification */
539 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
540 mod_tmp->sml_desc = desc;
541 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
542 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( old_rdn_vals[ d_cnt ] );
543 mod_tmp->sml_bvalues[ 1 ] = NULL;
544 mod_tmp->sml_op = LDAP_MOD_DELETE;
545 mod_tmp->sml_next = mod;
551 rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
554 case DB_LOCK_DEADLOCK:
555 case DB_LOCK_NOTGRANTED:
559 text = "DN index delete fail";
563 /* Binary format uses a single contiguous block, cannot
564 * free individual fields. Leave new_dn/new_ndn set so
565 * they can be individually freed later.
571 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
574 case DB_LOCK_DEADLOCK:
575 case DB_LOCK_NOTGRANTED:
579 text = "DN index add failed";
584 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
585 &text, textbuf, textlen );
587 if( rc != LDAP_SUCCESS ) {
589 case DB_LOCK_DEADLOCK:
590 case DB_LOCK_NOTGRANTED:
597 rc = bdb_id2entry_update( be, ltid, e );
600 case DB_LOCK_DEADLOCK:
601 case DB_LOCK_NOTGRANTED:
605 text = "entry update failed";
609 rc = txn_commit( ltid, 0 );
611 op->o_private = NULL;
614 Debug( LDAP_DEBUG_TRACE,
615 "bdb_modrdn: txn_commit failed: %s (%d)\n",
616 db_strerror(rc), rc, 0 );
618 text = "commit failed";
620 Debug( LDAP_DEBUG_TRACE,
621 "bdb_modrdn: added id=%08lx dn=\"%s\"\n",
622 e->e_id, e->e_dn, 0 );
628 send_ldap_result( conn, op, rc,
629 NULL, text, NULL, NULL );
631 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
632 ldap_pvt_thread_yield();
633 txn_checkpoint( bdb->bi_dbenv,
634 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
638 if( new_dn != NULL ) free( new_dn );
639 if( new_ndn != NULL ) free( new_ndn );
641 if( p_dn != NULL ) free( p_dn );
642 if( p_ndn != NULL ) free( p_ndn );
644 /* LDAP v2 supporting correct attribute handling. */
645 if( new_rdn_types != NULL ) charray_free(new_rdn_types);
646 if( new_rdn_vals != NULL ) charray_free(new_rdn_vals);
647 if( old_rdn != NULL ) free(old_rdn);
648 if( old_rdn_types != NULL ) charray_free(old_rdn_types);
649 if( old_rdn_vals != NULL ) charray_free(old_rdn_vals);
650 if( mod != NULL ) slap_mods_free(mod);
652 /* LDAP v3 Support */
653 if ( np_dn != NULL ) free( np_dn );
654 if ( np_ndn != NULL ) free( np_ndn );
657 /* free new parent and writer lock */
658 bdb_entry_return( be, np );
662 /* free parent and writer lock */
663 bdb_entry_return( be, p );
668 bdb_entry_return( be, e );
673 op->o_private = NULL;