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") );
63 if( newSuperior != NULL ) {
64 rc = LDAP_UNWILLING_TO_PERFORM;
65 text = "newSuperior not implemented (yet)";
70 retry: /* transaction retry */
71 Debug( LDAP_DEBUG_TRACE, "==>bdb_modrdn: retrying...\n", 0, 0, 0 );
72 rc = txn_abort( ltid );
77 text = "internal error";
83 /* begin transaction */
84 rc = txn_begin( bdb->bi_dbenv, NULL, <id, 0 );
87 Debug( LDAP_DEBUG_TRACE,
88 "bdb_delete: txn_begin failed: %s (%d)\n",
89 db_strerror(rc), rc, 0 );
91 text = "internal error";
96 opinfo.boi_txn = ltid;
98 op->o_private = &opinfo;
101 rc = bdb_dn2entry( be, ltid, ndn, &e, &matched, 0 );
107 case DB_LOCK_DEADLOCK:
108 case DB_LOCK_NOTGRANTED:
112 text = "internal error";
117 char* matched_dn = NULL;
118 struct berval** refs = NULL;
120 if( matched != NULL ) {
121 matched_dn = strdup( matched->e_dn );
122 refs = is_entry_referral( matched )
123 ? get_entry_referrals( be, conn, op, matched )
125 bdb_entry_return( be, matched );
129 refs = default_referral;
132 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
133 matched_dn, NULL, refs, NULL );
135 if ( matched != NULL ) {
136 ber_bvecfree( refs );
143 if (!manageDSAit && is_entry_referral( e ) ) {
144 /* parent is a referral, don't allow add */
145 /* parent is an alias, don't allow add */
146 struct berval **refs = get_entry_referrals( be,
149 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: entry is referral\n",
152 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
153 e->e_dn, NULL, refs, NULL );
155 ber_bvecfree( refs );
159 p_ndn = dn_parent( be, e->e_ndn );
160 if ( p_ndn != NULL ) {
161 /* Make sure parent entry exist and we can write its
165 rc = bdb_dn2entry( be, ltid, p_ndn, &p, NULL, 0 );
171 case DB_LOCK_DEADLOCK:
172 case DB_LOCK_NOTGRANTED:
176 text = "internal error";
181 Debug( LDAP_DEBUG_TRACE, "bdb_modrdn: parent does not exist\n",
187 /* check parent for "children" acl */
188 if ( ! access_allowed( be, conn, op, p,
189 children, NULL, ACL_WRITE ) )
191 Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
193 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
194 NULL, NULL, NULL, NULL );
198 Debug( LDAP_DEBUG_TRACE,
199 "bdb_modrdn: wr to children of entry %s OK\n",
202 p_dn = dn_parent( be, e->e_dn );
204 Debug( LDAP_DEBUG_TRACE,
205 "bdb_modrdn: parent dn=%s\n",
209 /* no parent, modrdn entry directly under root */
210 if( ! be_isroot( be, op->o_ndn ) ) {
211 Debug( LDAP_DEBUG_TRACE,
212 "bdb_modrdn: no parent & not root\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 new_rdn_type = rdn_attr_type( newrdn );
334 if ( new_rdn_type == NULL ) {
335 Debug( LDAP_DEBUG_TRACE,
336 "bdb_modrdn: can't figure out type of newrdn\n",
338 rc = LDAP_OPERATIONS_ERROR;
339 text = "unknown type used in RDN";
343 new_rdn_val = rdn_attr_value( newrdn );
344 if ( new_rdn_val == NULL ) {
345 Debug( LDAP_DEBUG_TRACE,
346 "bdb_modrdn: could not figure out val of newrdn\n",
348 rc = LDAP_OPERATIONS_ERROR;
349 text = "could not parse RDN value";
353 Debug( LDAP_DEBUG_TRACE,
354 "bdb_modrdn: new_rdn_val=\"%s\", new_rdn_type=\"%s\"\n",
355 new_rdn_val, new_rdn_type, 0 );
357 /* Retrieve the old rdn from the entry's dn */
359 if ( (old_rdn = dn_rdn( be, dn )) == NULL ) {
360 Debug( LDAP_DEBUG_TRACE,
361 "bdb_modrdn: can't figure out old_rdn from dn\n",
364 text = "could not parse old DN";
368 if ( (old_rdn_type = rdn_attr_type( old_rdn )) == NULL ) {
369 Debug( LDAP_DEBUG_TRACE,
370 "bdb_back_modrdn: can't figure out the old_rdn type\n",
373 text = "cannot parse RDN from old DN";
377 if ( strcasecmp( old_rdn_type, new_rdn_type ) != 0 ) {
378 /* Not a big deal but we may say something */
379 Debug( LDAP_DEBUG_TRACE,
380 "bdb_modrdn: old_rdn_type=%s, new_rdn_type=%s!\n",
381 old_rdn_type, new_rdn_type, 0 );
384 /* Add new attribute value to the entry */
385 add_bvals[0] = &add_bv; /* Array of bervals */
388 add_bv.bv_val = new_rdn_val;
389 add_bv.bv_len = strlen(new_rdn_val);
391 mod[0].sml_desc = NULL;
392 rc = slap_str2ad( new_rdn_type, &mod[0].sml_desc, &text );
394 if( rc != LDAP_SUCCESS ) {
395 Debug( LDAP_DEBUG_TRACE,
396 "bdb_modrdn: %s: %s (new)\n",
397 text, new_rdn_type, 0 );
400 mod[0].sml_bvalues = add_bvals;
401 mod[0].sml_op = SLAP_MOD_SOFTADD;
402 mod[0].sml_next = NULL;
404 /* Remove old rdn value if required */
407 /* Get value of old rdn */
408 old_rdn_val = rdn_attr_value( old_rdn );
409 if ( old_rdn_val == NULL) {
410 Debug( LDAP_DEBUG_TRACE,
411 "bdb_modrdn: can't figure out old_rdn_val from old_rdn\n",
414 text = "could not parse value from old RDN";
418 del_bvals[0] = &del_bv; /* Array of bervals */
421 /* Remove old value of rdn as an attribute. */
422 del_bv.bv_val = old_rdn_val;
423 del_bv.bv_len = strlen(old_rdn_val);
425 mod[1].sml_desc = NULL;
426 rc = slap_str2ad( old_rdn_type, &mod[1].sml_desc, &text );
428 if( rc != LDAP_SUCCESS ) {
429 Debug( LDAP_DEBUG_TRACE,
430 "bdb_modrdn: %s: %s (old)\n",
431 text, old_rdn_type, 0 );
435 mod[0].sml_next = &mod[1];
436 mod[1].sml_bvalues = del_bvals;
437 mod[1].sml_op = LDAP_MOD_DELETE;
438 mod[1].sml_next = NULL;
442 rc = bdb_dn2id_delete( be, ltid, e->e_ndn, e->e_id );
445 case DB_LOCK_DEADLOCK:
446 case DB_LOCK_NOTGRANTED:
450 text = "DN index delete fail";
462 rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
465 case DB_LOCK_DEADLOCK:
466 case DB_LOCK_NOTGRANTED:
470 text = "DN index add failed";
475 rc = bdb_modify_internal( be, conn, op, ltid, &mod[0], e, &text );
477 if( rc != LDAP_SUCCESS ) {
479 case DB_LOCK_DEADLOCK:
480 case DB_LOCK_NOTGRANTED:
486 /* NOTE: after this you must not free new_dn or new_ndn!
487 * They are used by cache.
491 rc = bdb_id2entry_update( be, ltid, e );
494 case DB_LOCK_DEADLOCK:
495 case DB_LOCK_NOTGRANTED:
499 text = "entry update failed";
503 rc = txn_commit( ltid, 0 );
505 op->o_private = NULL;
508 Debug( LDAP_DEBUG_TRACE,
509 "bdb_modrdn: txn_commit failed: %s (%d)\n",
510 db_strerror(rc), rc, 0 );
512 text = "commit failed";
514 Debug( LDAP_DEBUG_TRACE,
515 "bdb_modrdn: added id=%08x dn=\"%s\"\n",
516 e->e_id, e->e_dn, 0 );
522 send_ldap_result( conn, op, rc,
523 NULL, text, NULL, NULL );
525 if(rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
526 ldap_pvt_thread_yield();
527 txn_checkpoint( bdb->bi_dbenv,
528 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
532 if( new_dn != NULL ) free( new_dn );
533 if( new_ndn != NULL ) free( new_ndn );
535 if( p_dn != NULL ) free( p_dn );
536 if( p_ndn != NULL ) free( p_ndn );
538 /* LDAP v2 supporting correct attribute handling. */
539 if( new_rdn_type != NULL ) free(new_rdn_type);
540 if( new_rdn_val != NULL ) free(new_rdn_val);
541 if( old_rdn != NULL ) free(old_rdn);
542 if( old_rdn_type != NULL ) free(old_rdn_type);
543 if( old_rdn_val != NULL ) free(old_rdn_val);
546 /* LDAP v3 Support */
547 if ( np_dn != NULL ) free( np_dn );
548 if ( np_ndn != NULL ) free( np_ndn );
551 /* free new parent and writer lock */
552 bdb_entry_return( be, np );
556 /* free parent and writer lock */
557 bdb_entry_return( be, p );
562 bdb_entry_return( be, e );
567 op->o_private = NULL;