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;
36 char textbuf[SLAP_TEXT_BUFLEN];
37 size_t textlen = sizeof textbuf;
39 struct bdb_op_info opinfo;
42 char **new_rdn_vals = NULL; /* Vals of new rdn */
43 char **new_rdn_types = NULL; /* Types of new rdn */
45 char *old_rdn = NULL; /* Old rdn's attr type & val */
46 char **old_rdn_types = NULL; /* Types of old rdn attr. */
47 char **old_rdn_vals = NULL; /* Old rdn attribute values */
49 Entry *np = NULL; /* newSuperior Entry */
50 char *np_dn = NULL; /* newSuperior dn */
51 char *np_ndn = NULL; /* newSuperior ndn */
52 char *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, newrdn, (newSuperior ? newSuperior : "NULL") );
62 if( newSuperior != NULL ) {
63 rc = LDAP_UNWILLING_TO_PERFORM;
64 text = "newSuperior not implemented (yet)";
69 retry: /* transaction retry */
70 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
71 rc = txn_abort( ltid );
76 text = "internal error";
82 /* begin transaction */
83 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
86 Debug( LDAP_DEBUG_TRACE,
87 "bdb_delete: txn_begin failed: %s (%d)\n",
88 db_strerror(rc), rc, 0 );
90 text = "internal error";
95 opinfo.boi_txn = ltid;
97 op->o_private = &opinfo;
100 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
106 case DB_LOCK_DEADLOCK:
107 case DB_LOCK_NOTGRANTED:
111 text = "internal error";
116 char* matched_dn = NULL;
117 struct berval** refs = NULL;
119 if( matched != NULL ) {
120 matched_dn = strdup( matched->e_dn );
121 refs = is_entry_referral( matched )
122 ? get_entry_referrals( be, conn, op, matched )
124 bdb_entry_return( be, matched );
128 refs = default_referral;
131 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
132 matched_dn, NULL, refs, NULL );
134 if ( matched != NULL ) {
135 ber_bvecfree( refs );
142 if (!manageDSAit && is_entry_referral( e ) ) {
143 /* parent is a referral, don't allow add */
144 /* parent is an alias, don't allow add */
145 struct berval **refs = get_entry_referrals( be,
148 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
151 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
152 e->e_dn, NULL, refs, NULL );
154 ber_bvecfree( refs );
158 p_ndn = dn_parent( be, e->e_ndn );
159 if ( p_ndn != NULL && p_ndn[ 0 ] != '\0' ) {
160 /* Make sure parent entry exist and we can write its
164 rc = bdb_dn2entry( be, ltid, p_ndn, &p, NULL, 0 );
170 case DB_LOCK_DEADLOCK:
171 case DB_LOCK_NOTGRANTED:
175 text = "internal error";
180 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
186 /* check parent for "children" acl */
187 if ( ! access_allowed( be, conn, op, p,
188 children, NULL, ACL_WRITE ) )
190 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
192 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
193 NULL, NULL, NULL, NULL );
197 Debug( LDAP_DEBUG_TRACE,
198 "bdb_modrdn: wr to children of entry %s OK\n",
201 p_dn = dn_parent( be, e->e_dn );
203 Debug( LDAP_DEBUG_TRACE,
204 "bdb_modrdn: parent dn=%s\n",
208 /* no parent, modrdn entry directly under root */
209 if( ! be_isroot( be, op->o_ndn ) && !be_issuffix( be, "" ) ) {
210 Debug( LDAP_DEBUG_TRACE,
211 "bdb_modrdn: no parent, not root "
212 "& \"\" is not suffix\n",
214 rc = LDAP_INSUFFICIENT_ACCESS;
218 Debug( LDAP_DEBUG_TRACE,
219 "bdb_modrdn: no parent, locked root\n",
223 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
225 if ( newSuperior != NULL ) {
226 Debug( LDAP_DEBUG_TRACE,
227 "bdb_modrdn: new parent \"%s\" requested...\n",
230 np_dn = ch_strdup( newSuperior );
231 np_ndn = ch_strdup( np_dn );
232 (void) dn_normalize( np_ndn );
234 /* newSuperior == oldParent?, if so ==> ERROR */
235 /* newSuperior == entry being moved?, if so ==> ERROR */
236 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
238 rc = bdb_dn2entry( be, ltid, np_ndn, &np, NULL, 0 );
244 case DB_LOCK_DEADLOCK:
245 case DB_LOCK_NOTGRANTED:
249 text = "internal error";
254 Debug( LDAP_DEBUG_TRACE,
255 "bdb_modrdn: newSup(ndn=%s) not here!\n",
261 Debug( LDAP_DEBUG_TRACE,
262 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
265 /* check newSuperior for "children" acl */
266 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
267 Debug( LDAP_DEBUG_TRACE,
268 "bdb_modrdn: no wr to newSup children\n",
270 rc = LDAP_INSUFFICIENT_ACCESS;
274 if ( is_entry_alias( np ) ) {
275 /* entry is an alias, don't allow bind */
276 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
279 rc = LDAP_ALIAS_PROBLEM;
283 if ( is_entry_referral( np ) ) {
284 /* parent is a referral, don't allow add */
285 /* parent is an alias, don't allow add */
286 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
289 rc = LDAP_OPERATIONS_ERROR;
293 Debug( LDAP_DEBUG_TRACE,
294 "bdb_modrdn: wr to new parent's children OK\n",
297 new_parent_dn = np_dn;
300 /* Build target dn and make sure target entry doesn't exist already. */
301 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
303 new_ndn = ch_strdup(new_dn);
304 (void) dn_normalize( new_ndn );
306 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
309 rc = bdb_dn2id ( be, ltid, new_ndn, &id );
311 case DB_LOCK_DEADLOCK:
312 case DB_LOCK_NOTGRANTED:
317 rc = LDAP_ALREADY_EXISTS;
321 text = "internal error";
325 Debug( LDAP_DEBUG_TRACE,
326 "bdb_modrdn: new ndn=%s does not exist\n",
329 /* Get attribute type and attribute value of our new rdn, we will
330 * need to add that to our new entry
333 if ( rdn_attrs( newrdn, &new_rdn_types, &new_rdn_vals ) ) {
334 Debug( LDAP_DEBUG_TRACE,
335 "bdb_modrdn: can't figure out type(s)/values(s) "
336 "of newrdn\n", 0, 0, 0 );
337 rc = LDAP_OPERATIONS_ERROR;
338 text = "unknown type(s) used in RDN";
342 Debug( LDAP_DEBUG_TRACE,
343 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
344 new_rdn_vals[ 0 ], new_rdn_types[ 0 ], 0 );
346 /* Retrieve the old rdn from the entry's dn */
347 if ( ( old_rdn = dn_rdn( be, dn ) ) == NULL ) {
348 Debug( LDAP_DEBUG_TRACE,
349 "bdb_modrdn: can't figure out old_rdn from dn\n",
352 text = "could not parse old DN";
356 if ( rdn_attrs( old_rdn, &old_rdn_types, &old_rdn_vals ) ) {
357 Debug( LDAP_DEBUG_TRACE,
358 "bdb_back_modrdn: can't figure out the old_rdn "
359 "type(s)/value(s)\n", 0, 0, 0 );
361 text = "cannot parse RDN from old DN";
365 if ( newSuperior == NULL
366 && charray_strcasecmp( ( const char ** )old_rdn_types,
367 ( const char ** )new_rdn_types ) != 0 ) {
368 /* Not a big deal but we may say something */
369 Debug( LDAP_DEBUG_TRACE,
370 "bdb_modrdn: old_rdn_type(s)=%s, new_rdn_type(s)=%s "
372 old_rdn_types[ 0 ], new_rdn_types[ 0 ], 0 );
375 /* Add new attribute values to the entry */
376 for ( a_cnt = 0; new_rdn_types[ a_cnt ]; a_cnt++ ) {
378 AttributeDescription *desc = NULL;
379 Modifications *mod_tmp;
382 rc = slap_str2ad( new_rdn_types[ a_cnt ], &desc, &text );
384 if ( rc != LDAP_SUCCESS ) {
385 Debug( LDAP_DEBUG_TRACE,
386 "bdb_modrdn: %s: %s (new)\n",
387 text, new_rdn_types[ a_cnt ], 0 );
391 /* ACL check of newly added attrs */
392 val.bv_val = new_rdn_vals[ a_cnt ];
393 val.bv_len = strlen( val.bv_val );
394 if ( !access_allowed( be, conn, op, p,
395 desc, &val, ACL_WRITE ) ) {
396 Debug( LDAP_DEBUG_TRACE,
397 "bdb_modrdn: access to attr \"%s\" "
398 "(new) not allowed\n",
399 new_rdn_types[ a_cnt ], 0, 0 );
401 rc = LDAP_INSUFFICIENT_ACCESS;
405 /* Apply modification */
406 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
407 mod_tmp->sml_desc = desc;
408 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
409 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( new_rdn_vals[ a_cnt ] );
410 mod_tmp->sml_bvalues[ 1 ] = NULL;
411 mod_tmp->sml_op = SLAP_MOD_SOFTADD;
412 mod_tmp->sml_next = mod;
416 /* Remove old rdn value if required */
417 if ( deleteoldrdn ) {
418 /* Get value of old rdn */
419 if ( old_rdn_vals == NULL) {
420 Debug( LDAP_DEBUG_TRACE,
421 "bdb_modrdn: can't figure out old RDN value(s) "
422 "from old RDN\n", 0, 0, 0 );
424 text = "could not parse value(s) from old RDN";
428 for ( d_cnt = 0; old_rdn_types[ d_cnt ]; d_cnt++ ) {
430 AttributeDescription *desc = NULL;
431 Modifications *mod_tmp;
434 rc = slap_str2ad( old_rdn_types[ d_cnt ],
437 if ( rc != LDAP_SUCCESS ) {
438 Debug( LDAP_DEBUG_TRACE,
439 "bdb_modrdn: %s: %s (old)\n",
440 text, old_rdn_types[ d_cnt ], 0 );
444 /* ACL check of newly added attrs */
445 val.bv_val = new_rdn_vals[ d_cnt ];
446 val.bv_len = strlen( val.bv_val );
447 if ( !access_allowed( be, conn, op, p,
448 desc, &val, ACL_WRITE ) ) {
449 Debug( LDAP_DEBUG_TRACE,
450 "bdb_modrdn: access to attr \"%s\" "
451 "(old) not allowed\n",
452 old_rdn_types[ d_cnt ], 0, 0 );
454 rc = LDAP_INSUFFICIENT_ACCESS;
458 /* Apply modification */
459 mod_tmp = ( Modifications * )ch_malloc( sizeof( Modifications ) );
460 mod_tmp->sml_desc = desc;
461 mod_tmp->sml_bvalues = ( struct berval ** )ch_malloc( 2*sizeof( struct berval * ) );
462 mod_tmp->sml_bvalues[ 0 ] = ber_bvstrdup( old_rdn_vals[ d_cnt ] );
463 mod_tmp->sml_bvalues[ 1 ] = NULL;
464 mod_tmp->sml_op = LDAP_MOD_DELETE;
465 mod_tmp->sml_next = mod;
471 rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
474 case DB_LOCK_DEADLOCK:
475 case DB_LOCK_NOTGRANTED:
479 text = "DN index delete fail";
491 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
494 case DB_LOCK_DEADLOCK:
495 case DB_LOCK_NOTGRANTED:
499 text = "DN index add failed";
504 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
505 &text, textbuf, textlen );
507 if( rc != LDAP_SUCCESS ) {
509 case DB_LOCK_DEADLOCK:
510 case DB_LOCK_NOTGRANTED:
516 /* NOTE: after this you must not free new_dn or new_ndn!
517 * They are used by cache.
521 rc = bdb_id2entry_update( be, ltid, e );
524 case DB_LOCK_DEADLOCK:
525 case DB_LOCK_NOTGRANTED:
529 text = "entry update failed";
533 rc = txn_commit( ltid, 0 );
535 op->o_private = NULL;
538 Debug( LDAP_DEBUG_TRACE,
539 "bdb_modrdn: txn_commit failed: %s (%d)\n",
540 db_strerror(rc), rc, 0 );
542 text = "commit failed";
544 Debug( LDAP_DEBUG_TRACE,
545 "bdb_modrdn: added id=%08x dn=\"%s\"\n",
546 e->e_id, e->e_dn, 0 );
552 send_ldap_result( conn, op, rc,
553 NULL, text, NULL, NULL );
555 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
556 ldap_pvt_thread_yield();
557 txn_checkpoint( bdb->bi_dbenv,
558 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
562 if( new_dn != NULL ) free( new_dn );
563 if( new_ndn != NULL ) free( new_ndn );
565 if( p_dn != NULL ) free( p_dn );
566 if( p_ndn != NULL ) free( p_ndn );
568 /* LDAP v2 supporting correct attribute handling. */
569 if( new_rdn_types != NULL ) charray_free(new_rdn_types);
570 if( new_rdn_vals != NULL ) charray_free(new_rdn_vals);
571 if( old_rdn != NULL ) free(old_rdn);
572 if( old_rdn_types != NULL ) charray_free(old_rdn_types);
573 if( old_rdn_vals != NULL ) charray_free(old_rdn_vals);
574 if( mod != NULL ) slap_mods_free(mod);
576 /* LDAP v3 Support */
577 if ( np_dn != NULL ) free( np_dn );
578 if ( np_ndn != NULL ) free( np_ndn );
581 /* free new parent and writer lock */
582 bdb_entry_return( be, np );
586 /* free parent and writer lock */
587 bdb_entry_return( be, p );
592 bdb_entry_return( be, e );
597 op->o_private = NULL;