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;
35 const char *text = 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";
75 /* begin transaction */
76 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
78 Debug( LDAP_DEBUG_TRACE,
79 "bdb_delete: txn_begin failed: %s (%d)\n",
80 db_strerror(rc), rc, 0 );
82 text = "internal error";
87 opinfo.boi_txn = ltid;
89 op->o_private = &opinfo;
92 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
98 case DB_LOCK_DEADLOCK:
99 case DB_LOCK_NOTGRANTED:
103 text = "internal error";
108 char* matched_dn = NULL;
109 struct berval** refs = NULL;
111 if( matched != NULL ) {
112 matched_dn = strdup( matched->e_dn );
113 refs = is_entry_referral( matched )
114 ? get_entry_referrals( be, conn, op, matched )
116 bdb_entry_return( be, matched );
118 refs = default_referral;
121 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
122 matched_dn, NULL, refs, NULL );
124 if ( matched != NULL ) {
125 ber_bvecfree( refs );
132 if (!manageDSAit && is_entry_referral( e ) ) {
133 /* parent is a referral, don't allow add */
134 /* parent is an alias, don't allow add */
135 struct berval **refs = get_entry_referrals( be,
138 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
141 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
142 e->e_dn, NULL, refs, NULL );
144 ber_bvecfree( refs );
148 p_ndn = dn_parent( be, e->e_ndn );
149 if ( p_ndn != NULL ) {
150 /* Make sure parent entry exist and we can write its
154 rc = bdb_dn2entry( be, ltid, p_ndn, &p, NULL, 0 );
160 case DB_LOCK_DEADLOCK:
161 case DB_LOCK_NOTGRANTED:
165 text = "internal error";
170 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
176 /* check parent for "children" acl */
177 if ( ! access_allowed( be, conn, op, p,
178 children, NULL, ACL_WRITE ) )
180 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
182 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
183 NULL, NULL, NULL, NULL );
187 Debug( LDAP_DEBUG_TRACE,
188 "bdb_modrdn: wr to children of entry %s OK\n",
191 p_dn = dn_parent( be, e->e_dn );
193 Debug( LDAP_DEBUG_TRACE,
194 "bdb_modrdn: parent dn=%s\n",
198 /* no parent, modrdn entry directly under root */
199 if( ! be_isroot( be, op->o_ndn ) ) {
200 Debug( LDAP_DEBUG_TRACE,
201 "bdb_modrdn: no parent & not root\n",
203 rc = LDAP_INSUFFICIENT_ACCESS;
207 Debug( LDAP_DEBUG_TRACE,
208 "bdb_modrdn: no parent, locked root\n",
212 new_parent_dn = p_dn; /* New Parent unless newSuperior given */
214 if ( newSuperior != NULL ) {
215 Debug( LDAP_DEBUG_TRACE,
216 "bdb_modrdn: new parent \"%s\" requested...\n",
219 np_dn = ch_strdup( newSuperior );
220 np_ndn = ch_strdup( np_dn );
221 (void) dn_normalize( np_ndn );
223 /* newSuperior == oldParent?, if so ==> ERROR */
224 /* newSuperior == entry being moved?, if so ==> ERROR */
225 /* Get Entry with dn=newSuperior. Does newSuperior exist? */
227 rc = bdb_dn2entry( be, ltid, np_ndn, &np, NULL, 0 );
233 case DB_LOCK_DEADLOCK:
234 case DB_LOCK_NOTGRANTED:
238 text = "internal error";
243 Debug( LDAP_DEBUG_TRACE,
244 "bdb_modrdn: newSup(ndn=%s) not here!\n",
250 Debug( LDAP_DEBUG_TRACE,
251 "bdb_modrdn: wr to new parent OK np=%p, id=%ld\n",
254 /* check newSuperior for "children" acl */
255 if ( !access_allowed( be, conn, op, np, children, NULL, ACL_WRITE ) ) {
256 Debug( LDAP_DEBUG_TRACE,
257 "bdb_modrdn: no wr to newSup children\n",
259 rc = LDAP_INSUFFICIENT_ACCESS;
263 if ( is_entry_alias( np ) ) {
264 /* entry is an alias, don't allow bind */
265 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is alias\n",
268 rc = LDAP_ALIAS_PROBLEM;
272 if ( is_entry_referral( np ) ) {
273 /* parent is a referral, don't allow add */
274 /* parent is an alias, don't allow add */
275 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
278 rc = LDAP_OPERATIONS_ERROR;
282 Debug( LDAP_DEBUG_TRACE,
283 "bdb_modrdn: wr to new parent's children OK\n",
286 new_parent_dn = np_dn;
289 /* Build target dn and make sure target entry doesn't exist already. */
290 build_new_dn( &new_dn, e->e_dn, new_parent_dn, newrdn );
292 new_ndn = ch_strdup(new_dn);
293 (void) dn_normalize( new_ndn );
295 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: new ndn=%s\n",
298 rc = bdb_dn2id ( be, ltid, new_ndn, &id );
301 case DB_LOCK_DEADLOCK:
302 case DB_LOCK_NOTGRANTED:
306 text = "internal error";
312 Debug( LDAP_DEBUG_TRACE,
313 "bdb_modrdn: new ndn=%s does not exist\n",
316 /* Get attribute type and attribute value of our new rdn, we will
317 * need to add that to our new entry
320 new_rdn_type = rdn_attr_type( newrdn );
321 if ( new_rdn_type == NULL ) {
322 Debug( LDAP_DEBUG_TRACE,
323 "bdb_modrdn: can't figure out type of newrdn\n",
325 rc = LDAP_OPERATIONS_ERROR;
326 text = "unknown type used in RDN";
330 new_rdn_val = rdn_attr_value( newrdn );
331 if ( new_rdn_val == NULL ) {
332 Debug( LDAP_DEBUG_TRACE,
333 "bdb_modrdn: could not figure out val of newrdn\n",
335 rc = LDAP_OPERATIONS_ERROR;
336 text = "could not parse RDN value";
340 Debug( LDAP_DEBUG_TRACE,
341 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
342 new_rdn_val, new_rdn_type, 0 );
344 /* Retrieve the old rdn from the entry's dn */
346 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
347 Debug( LDAP_DEBUG_TRACE,
348 "bdb_modrdn: can't figure out old_rdn from dn\n",
351 text = "could not parse old DN";
355 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
356 Debug( LDAP_DEBUG_TRACE,
357 "bdb_back_modrdn: can't figure out the old_rdn type\n",
360 text = "cannot parse RDN from old DN";
364 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
365 /* Not a big deal but we may say something */
366 Debug( LDAP_DEBUG_TRACE,
367 "bdb_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
368 old_rdn_type, new_rdn_type, 0 );
371 /* Add new attribute value to the entry */
372 add_bvals[0] = &add_bv; /* Array of bervals */
375 add_bv.bv_val = new_rdn_val;
376 add_bv.bv_len = strlen(new_rdn_val);
378 mod[0].sml_desc = NULL;
379 rc = slap_str2ad( new_rdn_type, &mod[0].sml_desc, &text );
381 if( rc != LDAP_SUCCESS ) {
382 Debug( LDAP_DEBUG_TRACE,
383 "bdb_modrdn: %s: %s (new)\n",
384 text, new_rdn_type, 0 );
387 mod[0].sml_bvalues = add_bvals;
388 mod[0].sml_op = SLAP_MOD_SOFTADD;
389 mod[0].sml_next = NULL;
391 /* Remove old rdn value if required */
394 /* Get value of old rdn */
395 old_rdn_val = rdn_attr_value( old_rdn );
396 if ( old_rdn_val == NULL) {
397 Debug( LDAP_DEBUG_TRACE,
398 "bdb_modrdn: can't figure out old_rdn_val from old_rdn\n",
401 text = "could not parse value from old RDN";
405 del_bvals[0] = &del_bv; /* Array of bervals */
408 /* Remove old value of rdn as an attribute. */
409 del_bv.bv_val = old_rdn_val;
410 del_bv.bv_len = strlen(old_rdn_val);
412 mod[1].sml_desc = NULL;
413 rc = slap_str2ad( old_rdn_type, &mod[1].sml_desc, &text );
415 if( rc != LDAP_SUCCESS ) {
416 Debug( LDAP_DEBUG_TRACE,
417 "bdb_modrdn: %s: %s (old)\n",
418 text, old_rdn_type, 0 );
422 mod[0].sml_next = &mod[1];
423 mod[1].sml_bvalues = del_bvals;
424 mod[1].sml_op = LDAP_MOD_DELETE;
425 mod[1].sml_next = NULL;
429 rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
432 text = "DN index delete fail";
444 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
447 text = "DN index add failed";
452 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e, &text );
454 if( rc != LDAP_SUCCESS ) {
458 /* NOTE: after this you must not free new_dn or new_ndn!
459 * They are used by cache.
463 rc = bdb_id2entry_add( be, ltid, e );
466 text = "entry update failed";
472 rc = txn_commit( ltid, 0 );
474 op->o_private = NULL;
477 Debug( LDAP_DEBUG_TRACE,
478 "bdb_modrdn: txn_commit failed: %s (%d)\n",
479 db_strerror(rc), rc, 0 );
481 text = "commit failed";
483 Debug( LDAP_DEBUG_TRACE,
484 "bdb_modrdn: added id=%08x dn=\"%s\"\n",
485 e->e_id, e->e_dn, 0 );
491 send_ldap_result( conn, op, rc,
492 NULL, text, NULL, NULL );
495 if( new_dn != NULL ) free( new_dn );
496 if( new_ndn != NULL ) free( new_ndn );
498 if( p_dn != NULL ) free( p_dn );
499 if( p_ndn != NULL ) free( p_ndn );
501 /* LDAP v2 supporting correct attribute handling. */
502 if( new_rdn_type != NULL ) free(new_rdn_type);
503 if( new_rdn_val != NULL ) free(new_rdn_val);
504 if( old_rdn != NULL ) free(old_rdn);
505 if( old_rdn_type != NULL ) free(old_rdn_type);
506 if( old_rdn_val != NULL ) free(old_rdn_val);
509 /* LDAP v3 Support */
510 if ( np_dn != NULL ) free( np_dn );
511 if ( np_ndn != NULL ) free( np_ndn );
514 /* free new parent and writer lock */
515 bdb_entry_return( be, np );
519 /* free parent and writer lock */
520 bdb_entry_return( be, p );
525 bdb_entry_return( be, e );
530 op->o_private = NULL;