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 struct bdb_op_info opinfo;
40 char *new_rdn_val = NULL; /* Val of new rdn */
41 char *new_rdn_type = NULL; /* Type of new rdn */
42 char *old_rdn = NULL; /* Old rdn's attr type & val */
43 char *old_rdn_type = NULL; /* Type of old rdn attr. */
44 char *old_rdn_val = NULL; /* Old rdn attribute value */
46 Entry *np = NULL; /* newSuperior Entry */
47 char *np_dn = NULL; /* newSuperior dn */
48 char *np_ndn = NULL; /* newSuperior ndn */
49 char *new_parent_dn = NULL; /* np_dn, p_dn, or NULL */
51 /* Used to interface with bdb_modify_internal() */
52 struct berval add_bv; /* Stores new rdn att */
53 struct berval *add_bvals[2]; /* Stores new rdn att */
54 struct berval del_bv; /* Stores old rdn att */
55 struct berval *del_bvals[2]; /* Stores old rdn att */
56 Modifications mod[2]; /* 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 /* transaction retry */
65 retry: rc = txn_abort( ltid );
70 text = "internal error";
76 /* begin transaction */
77 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
80 Debug( LDAP_DEBUG_TRACE,
81 "bdb_delete: txn_begin failed: %s (%d)\n",
82 db_strerror(rc), rc, 0 );
84 text = "internal error";
89 opinfo.boi_txn = ltid;
91 op->o_private = &opinfo;
94 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
100 case DB_LOCK_DEADLOCK:
101 case DB_LOCK_NOTGRANTED:
105 text = "internal error";
110 char* matched_dn = NULL;
111 struct berval** refs = NULL;
113 if( matched != NULL ) {
114 matched_dn = strdup( matched->e_dn );
115 refs = is_entry_referral( matched )
116 ? get_entry_referrals( be, conn, op, matched )
118 bdb_entry_return( be, matched );
122 refs = default_referral;
125 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
126 matched_dn, NULL, refs, NULL );
128 if ( matched != NULL ) {
129 ber_bvecfree( refs );
136 if (!manageDSAit && is_entry_referral( e ) ) {
137 /* parent is a referral, don't allow add */
138 /* parent is an alias, don't allow add */
139 struct berval **refs = get_entry_referrals( be,
142 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
145 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
146 e->e_dn, NULL, refs, NULL );
148 ber_bvecfree( refs );
152 p_ndn = dn_parent( be, e->e_ndn );
153 if ( p_ndn != NULL ) {
154 /* Make sure parent entry exist and we can write its
158 rc = bdb_dn2entry( be, ltid, p_ndn, &p, NULL, 0 );
164 case DB_LOCK_DEADLOCK:
165 case DB_LOCK_NOTGRANTED:
169 text = "internal error";
174 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
180 /* check parent for "children" acl */
181 if ( ! access_allowed( be, conn, op, p,
182 children, NULL, ACL_WRITE ) )
184 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
186 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
187 NULL, NULL, NULL, NULL );
191 Debug( LDAP_DEBUG_TRACE,
192 "bdb_modrdn: wr to children of entry %s OK\n",
195 p_dn = dn_parent( be, e->e_dn );
197 Debug( LDAP_DEBUG_TRACE,
198 "bdb_modrdn: parent dn=%s\n",
202 /* no parent, modrdn entry directly under root */
203 if( ! be_isroot( be, op->o_ndn ) ) {
204 Debug( LDAP_DEBUG_TRACE,
205 "bdb_modrdn: no parent & not root\n",
207 rc = LDAP_INSUFFICIENT_ACCESS;
211 Debug( LDAP_DEBUG_TRACE,
212 "bdb_modrdn: no parent, locked root\n",
216 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
218 if ( newSuperior != NULL ) {
219 Debug( LDAP_DEBUG_TRACE,
220 "bdb_modrdn: new parent \"%s\" requested...\n",
223 np_dn = ch_strdup( newSuperior );
224 np_ndn = ch_strdup( np_dn );
225 (void) dn_normalize( np_ndn );
227 /* newSuperior == oldParent?, if so ==> ERROR */
228 /* newSuperior == entry being moved?, if so ==> ERROR */
229 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
231 rc = bdb_dn2entry( be, ltid, np_ndn, &np, NULL, 0 );
237 case DB_LOCK_DEADLOCK:
238 case DB_LOCK_NOTGRANTED:
242 text = "internal error";
247 Debug( LDAP_DEBUG_TRACE,
248 "bdb_modrdn: newSup(ndn=%s) not here!\n",
254 Debug( LDAP_DEBUG_TRACE,
255 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
258 /* check newSuperior for "children" acl */
259 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
260 Debug( LDAP_DEBUG_TRACE,
261 "bdb_modrdn: no wr to newSup children\n",
263 rc = LDAP_INSUFFICIENT_ACCESS;
267 if ( is_entry_alias( np ) ) {
268 /* entry is an alias, don't allow bind */
269 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
272 rc = LDAP_ALIAS_PROBLEM;
276 if ( is_entry_referral( np ) ) {
277 /* parent is a referral, don't allow add */
278 /* parent is an alias, don't allow add */
279 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
282 rc = LDAP_OPERATIONS_ERROR;
286 Debug( LDAP_DEBUG_TRACE,
287 "bdb_modrdn: wr to new parent's children OK\n",
290 new_parent_dn = np_dn;
293 /* Build target dn and make sure target entry doesn't exist already. */
294 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
296 new_ndn = ch_strdup(new_dn);
297 (void) dn_normalize( new_ndn );
299 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
302 rc = bdb_dn2id ( be, ltid, new_ndn, &id );
304 case DB_LOCK_DEADLOCK:
305 case DB_LOCK_NOTGRANTED:
310 rc = LDAP_ALREADY_EXISTS;
314 text = "internal error";
318 Debug( LDAP_DEBUG_TRACE,
319 "bdb_modrdn: new ndn=%s does not exist\n",
322 /* Get attribute type and attribute value of our new rdn, we will
323 * need to add that to our new entry
326 new_rdn_type = rdn_attr_type( newrdn );
327 if ( new_rdn_type == NULL ) {
328 Debug( LDAP_DEBUG_TRACE,
329 "bdb_modrdn: can't figure out type of newrdn\n",
331 rc = LDAP_OPERATIONS_ERROR;
332 text = "unknown type used in RDN";
336 new_rdn_val = rdn_attr_value( newrdn );
337 if ( new_rdn_val == NULL ) {
338 Debug( LDAP_DEBUG_TRACE,
339 "bdb_modrdn: could not figure out val of newrdn\n",
341 rc = LDAP_OPERATIONS_ERROR;
342 text = "could not parse RDN value";
346 Debug( LDAP_DEBUG_TRACE,
347 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
348 new_rdn_val, new_rdn_type, 0 );
350 /* Retrieve the old rdn from the entry's dn */
352 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
353 Debug( LDAP_DEBUG_TRACE,
354 "bdb_modrdn: can't figure out old_rdn from dn\n",
357 text = "could not parse old DN";
361 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
362 Debug( LDAP_DEBUG_TRACE,
363 "bdb_back_modrdn: can't figure out the old_rdn type\n",
366 text = "cannot parse RDN from old DN";
370 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
371 /* Not a big deal but we may say something */
372 Debug( LDAP_DEBUG_TRACE,
373 "bdb_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
374 old_rdn_type, new_rdn_type, 0 );
377 /* Add new attribute value to the entry */
378 add_bvals[0] = &add_bv; /* Array of bervals */
381 add_bv.bv_val = new_rdn_val;
382 add_bv.bv_len = strlen(new_rdn_val);
384 mod[0].sml_desc = NULL;
385 rc = slap_str2ad( new_rdn_type, &mod[0].sml_desc, &text );
387 if( rc != LDAP_SUCCESS ) {
388 Debug( LDAP_DEBUG_TRACE,
389 "bdb_modrdn: %s: %s (new)\n",
390 text, new_rdn_type, 0 );
393 mod[0].sml_bvalues = add_bvals;
394 mod[0].sml_op = SLAP_MOD_SOFTADD;
395 mod[0].sml_next = NULL;
397 /* Remove old rdn value if required */
400 /* Get value of old rdn */
401 old_rdn_val = rdn_attr_value( old_rdn );
402 if ( old_rdn_val == NULL) {
403 Debug( LDAP_DEBUG_TRACE,
404 "bdb_modrdn: can't figure out old_rdn_val from old_rdn\n",
407 text = "could not parse value from old RDN";
411 del_bvals[0] = &del_bv; /* Array of bervals */
414 /* Remove old value of rdn as an attribute. */
415 del_bv.bv_val = old_rdn_val;
416 del_bv.bv_len = strlen(old_rdn_val);
418 mod[1].sml_desc = NULL;
419 rc = slap_str2ad( old_rdn_type, &mod[1].sml_desc, &text );
421 if( rc != LDAP_SUCCESS ) {
422 Debug( LDAP_DEBUG_TRACE,
423 "bdb_modrdn: %s: %s (old)\n",
424 text, old_rdn_type, 0 );
428 mod[0].sml_next = &mod[1];
429 mod[1].sml_bvalues = del_bvals;
430 mod[1].sml_op = LDAP_MOD_DELETE;
431 mod[1].sml_next = NULL;
435 rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
438 case DB_LOCK_DEADLOCK:
439 case DB_LOCK_NOTGRANTED:
443 text = "DN index delete fail";
455 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
458 case DB_LOCK_DEADLOCK:
459 case DB_LOCK_NOTGRANTED:
463 text = "DN index add failed";
468 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e, &text );
470 if( rc != LDAP_SUCCESS ) {
472 case DB_LOCK_DEADLOCK:
473 case DB_LOCK_NOTGRANTED:
479 /* NOTE: after this you must not free new_dn or new_ndn!
480 * They are used by cache.
484 rc = bdb_id2entry_update( be, ltid, e );
487 case DB_LOCK_DEADLOCK:
488 case DB_LOCK_NOTGRANTED:
492 text = "entry update failed";
496 rc = txn_commit( ltid, 0 );
498 op->o_private = NULL;
501 Debug( LDAP_DEBUG_TRACE,
502 "bdb_modrdn: txn_commit failed: %s (%d)\n",
503 db_strerror(rc), rc, 0 );
505 text = "commit failed";
507 Debug( LDAP_DEBUG_TRACE,
508 "bdb_modrdn: added id=%08x dn=\"%s\"\n",
509 e->e_id, e->e_dn, 0 );
515 send_ldap_result( conn, op, rc,
516 NULL, text, NULL, NULL );
519 if( new_dn != NULL ) free( new_dn );
520 if( new_ndn != NULL ) free( new_ndn );
522 if( p_dn != NULL ) free( p_dn );
523 if( p_ndn != NULL ) free( p_ndn );
525 /* LDAP v2 supporting correct attribute handling. */
526 if( new_rdn_type != NULL ) free(new_rdn_type);
527 if( new_rdn_val != NULL ) free(new_rdn_val);
528 if( old_rdn != NULL ) free(old_rdn);
529 if( old_rdn_type != NULL ) free(old_rdn_type);
530 if( old_rdn_val != NULL ) free(old_rdn_val);
533 /* LDAP v3 Support */
534 if ( np_dn != NULL ) free( np_dn );
535 if ( np_ndn != NULL ) free( np_ndn );
538 /* free new parent and writer lock */
539 bdb_entry_return( be, np );
543 /* free parent and writer lock */
544 bdb_entry_return( be, p );
549 bdb_entry_return( be, e );
554 op->o_private = NULL;