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_val = NULL; /* Val of new rdn */
43 char *new_rdn_type = NULL; /* Type of new rdn */
44 char *old_rdn = NULL; /* Old rdn's attr type & val */
45 char *old_rdn_type = NULL; /* Type of old rdn attr. */
46 char *old_rdn_val = NULL; /* Old rdn attribute value */
48 Entry *np = NULL; /* newSuperior Entry */
49 char *np_dn = NULL; /* newSuperior dn */
50 char *np_ndn = NULL; /* newSuperior ndn */
51 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
53 /* Used to interface with bdb_modify_internal() */
54 struct berval add_bv; /* Stores new rdn att */
55 struct berval *add_bvals[2]; /* Stores new rdn att */
56 struct berval del_bv; /* Stores old rdn att */
57 struct berval *del_bvals[2]; /* Stores old rdn att */
58 Modifications mod[2]; /* Used to delete old rdn */
60 int manageDSAit = get_manageDSAit( op );
62 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn(%s,%s,%s)\n",
63 dn, newrdn, (newSuperior ? newSuperior : "NULL") );
65 if( newSuperior != NULL ) {
66 rc = LDAP_UNWILLING_TO_PERFORM;
67 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 = NULL;
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 )
127 bdb_entry_return( be, matched );
131 refs = default_referral;
134 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
135 matched_dn, NULL, refs, NULL );
137 if ( matched != NULL ) {
138 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,
151 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry 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 ) {
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 if( ! be_isroot( be, op->o_ndn ) ) {
213 Debug( LDAP_DEBUG_TRACE,
214 "bdb_modrdn: no parent & not root\n",
216 rc = LDAP_INSUFFICIENT_ACCESS;
220 Debug( LDAP_DEBUG_TRACE,
221 "bdb_modrdn: no parent, locked root\n",
225 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
227 if ( newSuperior != NULL ) {
228 Debug( LDAP_DEBUG_TRACE,
229 "bdb_modrdn: new parent \"%s\" requested...\n",
232 np_dn = ch_strdup( newSuperior );
233 np_ndn = ch_strdup( np_dn );
234 (void) dn_normalize( np_ndn );
236 /* newSuperior == oldParent?, if so ==> ERROR */
237 /* newSuperior == entry being moved?, if so ==> ERROR */
238 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
240 rc = bdb_dn2entry( be, ltid, np_ndn, &np, NULL, 0 );
246 case DB_LOCK_DEADLOCK:
247 case DB_LOCK_NOTGRANTED:
251 text = "internal error";
256 Debug( LDAP_DEBUG_TRACE,
257 "bdb_modrdn: newSup(ndn=%s) not here!\n",
263 Debug( LDAP_DEBUG_TRACE,
264 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
267 /* check newSuperior for "children" acl */
268 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
269 Debug( LDAP_DEBUG_TRACE,
270 "bdb_modrdn: no wr to newSup children\n",
272 rc = LDAP_INSUFFICIENT_ACCESS;
276 if ( is_entry_alias( np ) ) {
277 /* entry is an alias, don't allow bind */
278 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
281 rc = LDAP_ALIAS_PROBLEM;
285 if ( is_entry_referral( np ) ) {
286 /* parent is a referral, don't allow add */
287 /* parent is an alias, don't allow add */
288 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
291 rc = LDAP_OPERATIONS_ERROR;
295 Debug( LDAP_DEBUG_TRACE,
296 "bdb_modrdn: wr to new parent's children OK\n",
299 new_parent_dn = np_dn;
302 /* Build target dn and make sure target entry doesn't exist already. */
303 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
305 new_ndn = ch_strdup(new_dn);
306 (void) dn_normalize( new_ndn );
308 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
311 rc = bdb_dn2id ( be, ltid, new_ndn, &id );
313 case DB_LOCK_DEADLOCK:
314 case DB_LOCK_NOTGRANTED:
319 rc = LDAP_ALREADY_EXISTS;
323 text = "internal error";
327 Debug( LDAP_DEBUG_TRACE,
328 "bdb_modrdn: new ndn=%s does not exist\n",
331 /* Get attribute type and attribute value of our new rdn, we will
332 * need to add that to our new entry
335 new_rdn_type = rdn_attr_type( newrdn );
336 if ( new_rdn_type == NULL ) {
337 Debug( LDAP_DEBUG_TRACE,
338 "bdb_modrdn: can't figure out type of newrdn\n",
340 rc = LDAP_OPERATIONS_ERROR;
341 text = "unknown type used in RDN";
345 new_rdn_val = rdn_attr_value( newrdn );
346 if ( new_rdn_val == NULL ) {
347 Debug( LDAP_DEBUG_TRACE,
348 "bdb_modrdn: could not figure out val of newrdn\n",
350 rc = LDAP_OPERATIONS_ERROR;
351 text = "could not parse RDN value";
355 Debug( LDAP_DEBUG_TRACE,
356 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
357 new_rdn_val, new_rdn_type, 0 );
359 /* Retrieve the old rdn from the entry's dn */
361 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
362 Debug( LDAP_DEBUG_TRACE,
363 "bdb_modrdn: can't figure out old_rdn from dn\n",
366 text = "could not parse old DN";
370 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
371 Debug( LDAP_DEBUG_TRACE,
372 "bdb_back_modrdn: can't figure out the old_rdn type\n",
375 text = "cannot parse RDN from old DN";
379 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
380 /* Not a big deal but we may say something */
381 Debug( LDAP_DEBUG_TRACE,
382 "bdb_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
383 old_rdn_type, new_rdn_type, 0 );
386 /* Add new attribute value to the entry */
387 add_bvals[0] = &add_bv; /* Array of bervals */
390 add_bv.bv_val = new_rdn_val;
391 add_bv.bv_len = strlen(new_rdn_val);
393 mod[0].sml_desc = NULL;
394 rc = slap_str2ad( new_rdn_type, &mod[0].sml_desc, &text );
396 if( rc != LDAP_SUCCESS ) {
397 Debug( LDAP_DEBUG_TRACE,
398 "bdb_modrdn: %s: %s (new)\n",
399 text, new_rdn_type, 0 );
402 mod[0].sml_bvalues = add_bvals;
403 mod[0].sml_op = SLAP_MOD_SOFTADD;
404 mod[0].sml_next = NULL;
406 /* Remove old rdn value if required */
409 /* Get value of old rdn */
410 old_rdn_val = rdn_attr_value( old_rdn );
411 if ( old_rdn_val == NULL) {
412 Debug( LDAP_DEBUG_TRACE,
413 "bdb_modrdn: can't figure out old_rdn_val from old_rdn\n",
416 text = "could not parse value from old RDN";
420 del_bvals[0] = &del_bv; /* Array of bervals */
423 /* Remove old value of rdn as an attribute. */
424 del_bv.bv_val = old_rdn_val;
425 del_bv.bv_len = strlen(old_rdn_val);
427 mod[1].sml_desc = NULL;
428 rc = slap_str2ad( old_rdn_type, &mod[1].sml_desc, &text );
430 if( rc != LDAP_SUCCESS ) {
431 Debug( LDAP_DEBUG_TRACE,
432 "bdb_modrdn: %s: %s (old)\n",
433 text, old_rdn_type, 0 );
437 mod[0].sml_next = &mod[1];
438 mod[1].sml_bvalues = del_bvals;
439 mod[1].sml_op = LDAP_MOD_DELETE;
440 mod[1].sml_next = NULL;
444 rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
447 case DB_LOCK_DEADLOCK:
448 case DB_LOCK_NOTGRANTED:
452 text = "DN index delete fail";
464 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
467 case DB_LOCK_DEADLOCK:
468 case DB_LOCK_NOTGRANTED:
472 text = "DN index add failed";
477 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e,
478 &text, textbuf, textlen );
480 if( rc != LDAP_SUCCESS ) {
482 case DB_LOCK_DEADLOCK:
483 case DB_LOCK_NOTGRANTED:
489 /* NOTE: after this you must not free new_dn or new_ndn!
490 * They are used by cache.
494 rc = bdb_id2entry_update( be, ltid, e );
497 case DB_LOCK_DEADLOCK:
498 case DB_LOCK_NOTGRANTED:
502 text = "entry update failed";
506 rc = txn_commit( ltid, 0 );
508 op->o_private = NULL;
511 Debug( LDAP_DEBUG_TRACE,
512 "bdb_modrdn: txn_commit failed: %s (%d)\n",
513 db_strerror(rc), rc, 0 );
515 text = "commit failed";
517 Debug( LDAP_DEBUG_TRACE,
518 "bdb_modrdn: added id=%08x dn=\"%s\"\n",
519 e->e_id, e->e_dn, 0 );
525 send_ldap_result( conn, op, rc,
526 NULL, text, NULL, NULL );
528 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
529 ldap_pvt_thread_yield();
530 txn_checkpoint( bdb->bi_dbenv,
531 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
535 if( new_dn != NULL ) free( new_dn );
536 if( new_ndn != NULL ) free( new_ndn );
538 if( p_dn != NULL ) free( p_dn );
539 if( p_ndn != NULL ) free( p_ndn );
541 /* LDAP v2 supporting correct attribute handling. */
542 if( new_rdn_type != NULL ) free(new_rdn_type);
543 if( new_rdn_val != NULL ) free(new_rdn_val);
544 if( old_rdn != NULL ) free(old_rdn);
545 if( old_rdn_type != NULL ) free(old_rdn_type);
546 if( old_rdn_val != NULL ) free(old_rdn_val);
549 /* LDAP v3 Support */
550 if ( np_dn != NULL ) free( np_dn );
551 if ( np_ndn != NULL ) free( np_ndn );
554 /* free new parent and writer lock */
555 bdb_entry_return( be, np );
559 /* free parent and writer lock */
560 bdb_entry_return( be, p );
565 bdb_entry_return( be, e );
570 op->o_private = NULL;