modrdn.c 20.9 KB
Newer Older
1
2
/* modrdn.c - bdb backend modrdn routine */
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Notices    
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
5
 * Copyright 2000-2008 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Notices    
Kurt Zeilenga committed
6
7
8
9
10
11
12
13
14
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
15
16
17
18
19
20
21
22
23
24
 */

#include "portable.h"

#include <stdio.h>
#include <ac/string.h>

#include "back-bdb.h"

int
25
bdb_modrdn( Operation	*op, SlapReply *rs )
26
{
27
	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
28
	AttributeDescription *children = slap_schema.si_ad_children;
29
	AttributeDescription *entry = slap_schema.si_ad_entry;
30
	struct berval	p_dn, p_ndn;
31
	struct berval	new_dn = {0, NULL}, new_ndn = {0, NULL};
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
32
33
	Entry		*e = NULL;
	Entry		*p = NULL;
Howard Chu's avatar
Howard Chu committed
34
	EntryInfo	*ei = NULL, *eip = NULL, *nei = NULL, *neip = NULL;
35
	/* LDAP v2 supporting correct attribute handling. */
36
37
	char textbuf[SLAP_TEXT_BUFLEN];
	size_t textlen = sizeof textbuf;
Howard Chu's avatar
Howard Chu committed
38
	DB_TXN		*ltid = NULL, *lt2;
39
	struct bdb_op_info opinfo = {0};
40
	Entry dummy = {0};
41

42
	Entry		*np = NULL;			/* newSuperior Entry */
43
44
45
	struct berval	*np_dn = NULL;			/* newSuperior dn */
	struct berval	*np_ndn = NULL;			/* newSuperior ndn */
	struct berval	*new_parent_dn = NULL;	/* np_dn, p_dn, or NULL */
46
47
48

	int		manageDSAit = get_manageDSAit( op );

49
	BDB_LOCKER	locker = 0;
Howard Chu's avatar
Howard Chu committed
50
	DB_LOCK		lock, plock, nplock;
51

52
53
	int		num_retries = 0;

54
55
	LDAPControl **preread_ctrl = NULL;
	LDAPControl **postread_ctrl = NULL;
56
57
58
	LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
	int num_ctrls = 0;

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
59
	int	rc;
60

61
62
63
	int parent_is_glue = 0;
	int parent_is_leaf = 0;

Kurt Zeilenga's avatar
Kurt Zeilenga committed
64
65
66
67
#ifdef LDAP_X_TXN
	int settle = 0;
#endif

68
	Debug( LDAP_DEBUG_TRACE, "==>" LDAP_XSTRING(bdb_modrdn) "(%s,%s,%s)\n",
69
70
		op->o_req_dn.bv_val,op->oq_modrdn.rs_newrdn.bv_val,
		op->oq_modrdn.rs_newSup ? op->oq_modrdn.rs_newSup->bv_val : "NULL" );
71

Kurt Zeilenga's avatar
Kurt Zeilenga committed
72
#ifdef LDAP_X_TXN
Kurt Zeilenga's avatar
Kurt Zeilenga committed
73
74
75
	if( op->o_txnSpec ) {
		/* acquire connection lock */
		ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
76
		if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
77
78
79
			rs->sr_text = "invalid transaction identifier";
			rs->sr_err = LDAP_X_TXN_ID_INVALID;
			goto txnReturn;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
80
81
82
		} else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
			settle=1;
			goto txnReturn;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
83
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
84

Kurt Zeilenga's avatar
Kurt Zeilenga committed
85
86
87
88
89
90
91
92
93
94
95
96
		if( op->o_conn->c_txn_backend == NULL ) {
			op->o_conn->c_txn_backend = op->o_bd;

		} else if( op->o_conn->c_txn_backend != op->o_bd ) {
			rs->sr_text = "transaction cannot span multiple database contexts";
			rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
			goto txnReturn;
		}

		/* insert operation into transaction */

		rs->sr_text = "transaction specified";
97
		rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
98
99
100
101
102

txnReturn:
		/* release connection lock */
		ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
103
104
105
106
		if( !settle ) {
			send_ldap_result( op, rs );
			return rs->sr_err;
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
107
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
108
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
109
110
111

	ctrls[num_ctrls] = NULL;

112
	slap_mods_opattrs( op, &op->orr_modlist, 1 );
113

114
	if( 0 ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
115
retry:	/* transaction retry */
116
117
118
119
		if ( dummy.e_attrs ) {
			attrs_free( dummy.e_attrs );
			dummy.e_attrs = NULL;
		}
120
		if (e != NULL) {
121
			bdb_unlocked_cache_return_entry_w(&bdb->bi_cache, e);
122
			e = NULL;
123
124
		}
		if (p != NULL) {
125
			bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
126
			p = NULL;
127
128
		}
		if (np != NULL) {
129
			bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
130
			np = NULL;
131
		}
132
133
		Debug( LDAP_DEBUG_TRACE, "==>" LDAP_XSTRING(bdb_modrdn)
				": retrying...\n", 0, 0, 0 );
134

135
		rs->sr_err = TXN_ABORT( ltid );
136
137
		ltid = NULL;
		op->o_private = NULL;
138
		op->o_do_not_cache = opinfo.boi_acl_cache;
139
140
141
		if( rs->sr_err != 0 ) {
			rs->sr_err = LDAP_OTHER;
			rs->sr_text = "internal error";
142
143
			goto return_results;
		}
144
145
		if ( op->o_abandon ) {
			rs->sr_err = SLAPD_ABANDON;
146
			goto return_results;
147
		}
148
149
		parent_is_glue = 0;
		parent_is_leaf = 0;
Howard Chu's avatar
Howard Chu committed
150
		bdb_trans_backoff( ++num_retries );
151
152
	}

153
	/* begin transaction */
154
	rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
155
		bdb->bi_db_opflags );
156
157
	rs->sr_text = NULL;
	if( rs->sr_err != 0 ) {
158
		Debug( LDAP_DEBUG_TRACE,
159
160
			LDAP_XSTRING(bdb_modrdn) ": txn_begin failed: "
			"%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
161
162
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "internal error";
163
		goto return_results;
164
165
	}

166
167
	locker = TXN_ID ( ltid );

168
	opinfo.boi_bdb = op->o_bd;
169
170
	opinfo.boi_txn = ltid;
	opinfo.boi_err = 0;
171
	opinfo.boi_acl_cache = op->o_do_not_cache;
172
173
174
	op->o_private = &opinfo;

	/* get entry */
175
176
	rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei, 1,
		locker, &lock );
177

178
	switch( rs->sr_err ) {
179
180
181
182
183
184
	case 0:
	case DB_NOTFOUND:
		break;
	case DB_LOCK_DEADLOCK:
	case DB_LOCK_NOTGRANTED:
		goto retry;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
185
	case LDAP_BUSY:
186
		rs->sr_text = "ldap server busy";
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
187
		goto return_results;
188
	default:
189
190
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "internal error";
191
192
193
		goto return_results;
	}

Howard Chu's avatar
Howard Chu committed
194
	e = ei->bei_e;
195
	/* FIXME: dn2entry() should return non-glue entry */
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
196
197
198
	if (( rs->sr_err == DB_NOTFOUND ) ||
		( !manageDSAit && e && is_entry_glue( e )))
	{
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
199
		if( e != NULL ) {
Howard Chu's avatar
Howard Chu committed
200
201
202
			rs->sr_matched = ch_strdup( e->e_dn );
			rs->sr_ref = is_entry_referral( e )
				? get_entry_referrals( op, e )
203
				: NULL;
Howard Chu's avatar
Howard Chu committed
204
205
			bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, e);
			e = NULL;
206

207
		} else {
208
209
			rs->sr_ref = referral_rewrite( default_referral, NULL,
					&op->o_req_dn, LDAP_SCOPE_DEFAULT );
210
211
		}

212
213
		rs->sr_err = LDAP_REFERRAL;
		send_ldap_result( op, rs );
214

215
216
217
218
		ber_bvarray_free( rs->sr_ref );
		free( (char *)rs->sr_matched );
		rs->sr_ref = NULL;
		rs->sr_matched = NULL;
219
220
221
222

		goto done;
	}

223
224
225
226
227
228
229
	if ( get_assert( op ) &&
		( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
	{
		rs->sr_err = LDAP_ASSERTION_FAILED;
		goto return_results;
	}

230
	/* check write on old entry */
231
232
	rs->sr_err = access_allowed( op, e, entry, NULL, ACL_WRITE, NULL );
	if ( ! rs->sr_err ) {
233
234
235
236
237
238
		switch( opinfo.boi_err ) {
		case DB_LOCK_DEADLOCK:
		case DB_LOCK_NOTGRANTED:
			goto retry;
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
239
240
		Debug( LDAP_DEBUG_TRACE, "no access to entry\n", 0,
			0, 0 );
241
242
		rs->sr_text = "no write access to old entry";
		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
243
244
245
		goto return_results;
	}

246
#ifndef BDB_HIER
247
	rs->sr_err = bdb_cache_children( op, ltid, e );
248
249
	if ( rs->sr_err != DB_NOTFOUND ) {
		switch( rs->sr_err ) {
250
251
252
253
254
		case DB_LOCK_DEADLOCK:
		case DB_LOCK_NOTGRANTED:
			goto retry;
		case 0:
			Debug(LDAP_DEBUG_ARGS,
255
256
				"<=- " LDAP_XSTRING(bdb_modrdn)
				": non-leaf %s\n",
257
258
259
				op->o_req_dn.bv_val, 0, 0);
			rs->sr_err = LDAP_NOT_ALLOWED_ON_NONLEAF;
			rs->sr_text = "subtree rename not supported";
260
261
262
			break;
		default:
			Debug(LDAP_DEBUG_ARGS,
263
264
				"<=- " LDAP_XSTRING(bdb_modrdn)
				": has_children failed: %s (%d)\n",
265
266
267
				db_strerror(rs->sr_err), rs->sr_err, 0 );
			rs->sr_err = LDAP_OTHER;
			rs->sr_text = "internal error";
268
269
270
		}
		goto return_results;
	}
271
	ei->bei_state |= CACHE_ENTRY_NO_KIDS;
272
#endif
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
273

274
275
	if (!manageDSAit && is_entry_referral( e ) ) {
		/* parent is a referral, don't allow add */
276
		rs->sr_ref = get_entry_referrals( op, e );
277

278
279
		Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_modrdn)
			": entry %s is referral\n", e->e_dn, 0, 0 );
280

281
282
283
		rs->sr_err = LDAP_REFERRAL,
		rs->sr_matched = e->e_name.bv_val;
		send_ldap_result( op, rs );
284

285
286
287
		ber_bvarray_free( rs->sr_ref );
		rs->sr_ref = NULL;
		rs->sr_matched = NULL;
288
289
290
		goto done;
	}

291
	if ( be_issuffix( op->o_bd, &e->e_nname ) ) {
292
293
#ifdef BDB_MULTIPLE_SUFFIXES
		/* Allow renaming one suffix entry to another */
294
		p_ndn = slap_empty_bv;
295
296
297
298
299
300
#else
		/* There can only be one suffix entry */
		rs->sr_err = LDAP_NAMING_VIOLATION;
		rs->sr_text = "cannot rename suffix entry";
		goto return_results;
#endif
Pierangelo Masarati's avatar
Pierangelo Masarati committed
301
	} else {
302
		dnParent( &e->e_nname, &p_ndn );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
303
	}
304
	np_ndn = &p_ndn;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
305
306
	eip = ei->bei_parent;
	if ( eip && eip->bei_id ) {
307
308
309
		/* Make sure parent entry exist and we can write its 
		 * children.
		 */
310
311
		rs->sr_err = bdb_cache_find_id( op, ltid,
			eip->bei_id, &eip, 0, locker, &plock );
312

313
		switch( rs->sr_err ) {
314
315
316
317
318
319
		case 0:
		case DB_NOTFOUND:
			break;
		case DB_LOCK_DEADLOCK:
		case DB_LOCK_NOTGRANTED:
			goto retry;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
320
		case LDAP_BUSY:
321
			rs->sr_text = "ldap server busy";
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
322
			goto return_results;
323
		default:
324
325
			rs->sr_err = LDAP_OTHER;
			rs->sr_text = "internal error";
326
327
328
			goto return_results;
		}

Howard Chu's avatar
Howard Chu committed
329
		p = eip->bei_e;
330
		if( p == NULL) {
331
332
			Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_modrdn)
				": parent does not exist\n", 0, 0, 0);
333
334
			rs->sr_err = LDAP_OTHER;
			rs->sr_text = "old entry's parent does not exist";
335
336
			goto return_results;
		}
337
338
339
	} else {
		p = (Entry *)&slap_entry_root;
	}
340

341
342
343
344
345
346
	/* check parent for "children" acl */
	rs->sr_err = access_allowed( op, p,
		children, NULL,
		op->oq_modrdn.rs_newSup == NULL ?
			ACL_WRITE : ACL_WDEL,
		NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
347

348
349
	if ( !p_ndn.bv_len )
		p = NULL;
350

351
352
353
354
355
	if ( ! rs->sr_err ) {
		switch( opinfo.boi_err ) {
		case DB_LOCK_DEADLOCK:
		case DB_LOCK_NOTGRANTED:
			goto retry;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
356
		}
357

358
359
360
361
362
363
		rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
		Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
			0, 0 );
		rs->sr_text = "no write access to old parent's children";
		goto return_results;
	}
364

365
366
367
368
369
370
	Debug( LDAP_DEBUG_TRACE,
		LDAP_XSTRING(bdb_modrdn) ": wr to children "
		"of entry %s OK\n", p_ndn.bv_val, 0, 0 );
	
	if ( p_ndn.bv_val == slap_empty_bv.bv_val ) {
		p_dn = slap_empty_bv;
371
	} else {
372
		dnParent( &e->e_name, &p_dn );
373
374
	}

375
376
377
378
	Debug( LDAP_DEBUG_TRACE,
		LDAP_XSTRING(bdb_modrdn) ": parent dn=%s\n",
		p_dn.bv_val, 0, 0 );

379
	new_parent_dn = &p_dn;	/* New Parent unless newSuperior given */
380

381
	if ( op->oq_modrdn.rs_newSup != NULL ) {
382
		Debug( LDAP_DEBUG_TRACE, 
383
384
			LDAP_XSTRING(bdb_modrdn)
			": new parent \"%s\" requested...\n",
385
			op->oq_modrdn.rs_newSup->bv_val, 0, 0 );
386

387
		/*  newSuperior == oldParent? */
388
		if( dn_match( &p_ndn, op->oq_modrdn.rs_nnewSup ) ) {
389
390
			Debug( LDAP_DEBUG_TRACE, "bdb_back_modrdn: "
				"new parent \"%s\" same as the old parent \"%s\"\n",
391
392
				op->oq_modrdn.rs_newSup->bv_val, p_dn.bv_val, 0 );
			op->oq_modrdn.rs_newSup = NULL; /* ignore newSuperior */
393
394
		}
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
395

396
397
398
399
400
401
402
403
404
	/* There's a BDB_MULTIPLE_SUFFIXES case here that this code doesn't
	 * support. E.g., two suffixes dc=foo,dc=com and dc=bar,dc=net.
	 * We do not allow modDN
	 *   dc=foo,dc=com
	 *    newrdn dc=bar
	 *    newsup dc=net
	 * and we probably should. But since MULTIPLE_SUFFIXES is deprecated
	 * I'm ignoring this problem for now.
	 */
405
406
407
408
	if ( op->oq_modrdn.rs_newSup != NULL ) {
		if ( op->oq_modrdn.rs_newSup->bv_len ) {
			np_dn = op->oq_modrdn.rs_newSup;
			np_ndn = op->oq_modrdn.rs_nnewSup;
409

Howard Chu's avatar
Howard Chu committed
410
			/* newSuperior == oldParent? - checked above */
411
			/* newSuperior == entry being moved?, if so ==> ERROR */
Howard Chu's avatar
Howard Chu committed
412
			if ( dnIsSuffix( np_ndn, &e->e_nname )) {
Howard Chu's avatar
Howard Chu committed
413
414
				rs->sr_err = LDAP_NO_SUCH_OBJECT;
				rs->sr_text = "new superior not found";
Howard Chu's avatar
Howard Chu committed
415
416
				goto return_results;
			}
417
418
			/* Get Entry with dn=newSuperior. Does newSuperior exist? */

419
420
			rs->sr_err = bdb_dn2entry( op, ltid, np_ndn,
				&neip, 0, locker, &nplock );
421

422
			switch( rs->sr_err ) {
Howard Chu's avatar
Howard Chu committed
423
			case 0: np = neip->bei_e;
424
425
426
427
428
			case DB_NOTFOUND:
				break;
			case DB_LOCK_DEADLOCK:
			case DB_LOCK_NOTGRANTED:
				goto retry;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
429
			case LDAP_BUSY:
430
				rs->sr_text = "ldap server busy";
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
431
				goto return_results;
432
			default:
433
434
				rs->sr_err = LDAP_OTHER;
				rs->sr_text = "internal error";
435
436
437
438
439
				goto return_results;
			}

			if( np == NULL) {
				Debug( LDAP_DEBUG_TRACE,
440
441
					LDAP_XSTRING(bdb_modrdn)
					": newSup(ndn=%s) not here!\n",
442
					np_ndn->bv_val, 0, 0);
443
				rs->sr_text = "new superior not found";
Howard Chu's avatar
Howard Chu committed
444
				rs->sr_err = LDAP_NO_SUCH_OBJECT;
445
446
				goto return_results;
			}
447
448

			Debug( LDAP_DEBUG_TRACE,
449
450
				LDAP_XSTRING(bdb_modrdn)
				": wr to new parent OK np=%p, id=%ld\n",
451
				(void *) np, (long) np->e_id, 0 );
452
453

			/* check newSuperior for "children" acl */
454
			rs->sr_err = access_allowed( op, np, children,
455
				NULL, ACL_WADD, NULL );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
456

457
			if( ! rs->sr_err ) {
458
459
460
461
462
463
				switch( opinfo.boi_err ) {
				case DB_LOCK_DEADLOCK:
				case DB_LOCK_NOTGRANTED:
					goto retry;
				}

464
				Debug( LDAP_DEBUG_TRACE,
465
466
					LDAP_XSTRING(bdb_modrdn)
					": no wr to newSup children\n",
467
					0, 0, 0 );
468
469
				rs->sr_text = "no write access to new superior's children";
				rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
470
471
472
473
474
				goto return_results;
			}

			if ( is_entry_alias( np ) ) {
				/* parent is an alias, don't allow add */
475
476
477
				Debug( LDAP_DEBUG_TRACE,
					LDAP_XSTRING(bdb_modrdn)
					": entry is alias\n",
478
					0, 0, 0 );
479
480
				rs->sr_text = "new superior is an alias";
				rs->sr_err = LDAP_ALIAS_PROBLEM;
481
482
483
484
485
				goto return_results;
			}

			if ( is_entry_referral( np ) ) {
				/* parent is a referral, don't allow add */
486
487
488
				Debug( LDAP_DEBUG_TRACE,
					LDAP_XSTRING(bdb_modrdn)
					": entry is referral\n",
489
					0, 0, 0 );
490
491
				rs->sr_text = "new superior is a referral";
				rs->sr_err = LDAP_OTHER;
492
493
				goto return_results;
			}
494

495
		} else {
496
			np_dn = NULL;
497
498

			/* no parent, modrdn entry directly under root */
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
			if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv )
				|| be_isupdate( op ) ) {
				np = (Entry *)&slap_entry_root;

				/* check parent for "children" acl */
				rs->sr_err = access_allowed( op, np,
					children, NULL, ACL_WADD, NULL );

				np = NULL;

				if ( ! rs->sr_err ) {
					switch( opinfo.boi_err ) {
					case DB_LOCK_DEADLOCK:
					case DB_LOCK_NOTGRANTED:
						goto retry;
514
515
					}

516
					rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
517
518
519
520
521
					Debug( LDAP_DEBUG_TRACE, 
						"no access to new superior\n", 
						0, 0, 0 );
					rs->sr_text =
						"no write access to new superior's children";
522
523
524
					goto return_results;
				}
			}
525
526
527
		}

		Debug( LDAP_DEBUG_TRACE,
528
529
			LDAP_XSTRING(bdb_modrdn)
			": wr to new parent's children OK\n",
530
531
532
533
			0, 0, 0 );

		new_parent_dn = np_dn;
	}
534

535
	/* Build target dn and make sure target entry doesn't exist already. */
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
536
537
538
	if (!new_dn.bv_val) {
		build_new_dn( &new_dn, new_parent_dn, &op->oq_modrdn.rs_newrdn, NULL ); 
	}
539

540
541
	if (!new_ndn.bv_val) {
		struct berval bv = {0, NULL};
Kurt Zeilenga's avatar
Kurt Zeilenga committed
542
		dnNormalize( 0, NULL, NULL, &new_dn, &bv, op->o_tmpmemctx );
543
		ber_dupbv( &new_ndn, &bv );
544
545
		/* FIXME: why not call dnNormalize() w/o ctx? */
		op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
546
	}
547

548
	Debug( LDAP_DEBUG_TRACE, LDAP_XSTRING(bdb_modrdn) ": new ndn=%s\n",
549
		new_ndn.bv_val, 0, 0 );
550

Howard Chu's avatar
Howard Chu committed
551
552
	/* Shortcut the search */
	nei = neip ? neip : eip;
Howard Chu's avatar
Howard Chu committed
553
	rs->sr_err = bdb_cache_find_ndn ( op, locker, &new_ndn, &nei );
Howard Chu's avatar
Howard Chu committed
554
	if ( nei ) bdb_cache_entryinfo_unlock( nei );
555
	switch( rs->sr_err ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
556
557
558
559
560
561
	case DB_LOCK_DEADLOCK:
	case DB_LOCK_NOTGRANTED:
		goto retry;
	case DB_NOTFOUND:
		break;
	case 0:
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
562
563
564
		/* Allow rename to same DN */
		if ( nei == ei )
			break;
565
		rs->sr_err = LDAP_ALREADY_EXISTS;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
566
567
		goto return_results;
	default:
568
569
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "internal error";
570
571
572
		goto return_results;
	}

573
	assert( op->orr_modlist != NULL );
Howard Chu's avatar
Howard Chu committed
574

575
	if( op->o_preread ) {
576
577
578
579
		if( preread_ctrl == NULL ) {
			preread_ctrl = &ctrls[num_ctrls++];
			ctrls[num_ctrls] = NULL;
		}
580
		if( slap_read_controls( op, rs, e,
581
			&slap_pre_read_bv, preread_ctrl ) )
582
583
		{
			Debug( LDAP_DEBUG_TRACE,        
584
				"<=- " LDAP_XSTRING(bdb_modrdn)
585
586
587
588
589
590
				": pre-read failed!\n", 0, 0, 0 );
			if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
				/* FIXME: is it correct to abort
				 * operation if control fails? */
				goto return_results;
			}
591
592
593
		}                   
	}

Howard Chu's avatar
Howard Chu committed
594
	/* nested transaction */
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
595
	rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, bdb->bi_db_opflags );
Howard Chu's avatar
Howard Chu committed
596
597
598
	rs->sr_text = NULL;
	if( rs->sr_err != 0 ) {
		Debug( LDAP_DEBUG_TRACE,
599
600
			LDAP_XSTRING(bdb_modrdn)
			": txn_begin(2) failed: %s (%d)\n",
Howard Chu's avatar
Howard Chu committed
601
602
603
604
605
606
			db_strerror(rs->sr_err), rs->sr_err, 0 );
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "internal error";
		goto return_results;
	}

607
	/* delete old DN */
608
	rs->sr_err = bdb_dn2id_delete( op, lt2, eip, e );
609
	if ( rs->sr_err != 0 ) {
Howard Chu's avatar
Howard Chu committed
610
		Debug(LDAP_DEBUG_TRACE,
611
612
			"<=- " LDAP_XSTRING(bdb_modrdn)
			": dn2id del failed: %s (%d)\n",
Howard Chu's avatar
Howard Chu committed
613
			db_strerror(rs->sr_err), rs->sr_err, 0 );
614
		switch( rs->sr_err ) {
615
616
617
618
		case DB_LOCK_DEADLOCK:
		case DB_LOCK_NOTGRANTED:
			goto retry;
		}
619
620
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "DN index delete fail";
621
622
623
		goto return_results;
	}

624
625
626
627
628
	/* copy the entry, then override some fields */
	dummy = *e;
	dummy.e_name = new_dn;
	dummy.e_nname = new_ndn;
	dummy.e_attrs = NULL;
629

630
631
	/* add new DN */
	rs->sr_err = bdb_dn2id_add( op, lt2, neip ? neip : eip, &dummy );
632
	if ( rs->sr_err != 0 ) {
Howard Chu's avatar
Howard Chu committed
633
		Debug(LDAP_DEBUG_TRACE,
634
635
			"<=- " LDAP_XSTRING(bdb_modrdn)
			": dn2id add failed: %s (%d)\n",
Howard Chu's avatar
Howard Chu committed
636
			db_strerror(rs->sr_err), rs->sr_err, 0 );
637
		switch( rs->sr_err ) {
638
639
640
641
		case DB_LOCK_DEADLOCK:
		case DB_LOCK_NOTGRANTED:
			goto retry;
		}
642
643
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "DN index add failed";
644
645
646
		goto return_results;
	}

647
648
	dummy.e_attrs = e->e_attrs;

649
	/* modify entry */
650
	rs->sr_err = bdb_modify_internal( op, lt2, op->orr_modlist, &dummy,
651
652
		&rs->sr_text, textbuf, textlen );
	if( rs->sr_err != LDAP_SUCCESS ) {
Howard Chu's avatar
Howard Chu committed
653
		Debug(LDAP_DEBUG_TRACE,
654
655
			"<=- " LDAP_XSTRING(bdb_modrdn)
			": modify failed: %s (%d)\n",
Howard Chu's avatar
Howard Chu committed
656
			db_strerror(rs->sr_err), rs->sr_err, 0 );
657
658
		if ( ( rs->sr_err == LDAP_INSUFFICIENT_ACCESS ) && opinfo.boi_err ) {
			rs->sr_err = opinfo.boi_err;
659
		}
660
		if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
661
		switch( rs->sr_err ) {
662
663
664
665
		case DB_LOCK_DEADLOCK:
		case DB_LOCK_NOTGRANTED:
			goto retry;
		}
666
667
		goto return_results;
	}
668

669
	/* id2entry index */
670
	rs->sr_err = bdb_id2entry_update( op->o_bd, lt2, &dummy );
671
	if ( rs->sr_err != 0 ) {
Howard Chu's avatar
Howard Chu committed
672
		Debug(LDAP_DEBUG_TRACE,
673
674
			"<=- " LDAP_XSTRING(bdb_modrdn)
			": id2entry failed: %s (%d)\n",
Howard Chu's avatar
Howard Chu committed
675
			db_strerror(rs->sr_err), rs->sr_err, 0 );
676
		switch( rs->sr_err ) {
677
678
679
680
		case DB_LOCK_DEADLOCK:
		case DB_LOCK_NOTGRANTED:
			goto retry;
		}
681
682
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "entry update failed";
683
684
		goto return_results;
	}
685

686
687
688
689
690
691
692
693
694
695
696
697
	if ( p_ndn.bv_len != 0 ) {
		parent_is_glue = is_entry_glue(p);
		rs->sr_err = bdb_cache_children( op, lt2, p );
		if ( rs->sr_err != DB_NOTFOUND ) {
			switch( rs->sr_err ) {
			case DB_LOCK_DEADLOCK:
			case DB_LOCK_NOTGRANTED:
				goto retry;
			case 0:
				break;
			default:
				Debug(LDAP_DEBUG_ARGS,
698
699
					"<=- " LDAP_XSTRING(bdb_modrdn)
					": has_children failed: %s (%d)\n",
700
701
702
703
704
705
706
707
708
709
					db_strerror(rs->sr_err), rs->sr_err, 0 );
				rs->sr_err = LDAP_OTHER;
				rs->sr_text = "internal error";
				goto return_results;
			}
			parent_is_leaf = 1;
		}
		bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
		p = NULL;
	}
710

Howard Chu's avatar
Howard Chu committed
711
712
713
714
715
	if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
		rs->sr_err = LDAP_OTHER;
		rs->sr_text = "txn_commit(2) failed";
		goto return_results;
	}
716

717
	if( op->o_postread ) {
718
719
720
721
		if( postread_ctrl == NULL ) {
			postread_ctrl = &ctrls[num_ctrls++];
			ctrls[num_ctrls] = NULL;
		}
722
		if( slap_read_controls( op, rs, &dummy,
723
			&slap_post_read_bv, postread_ctrl ) )
724
725
		{
			Debug( LDAP_DEBUG_TRACE,        
726
727
				"<=- " LDAP_XSTRING(bdb_modrdn)
				": post-read failed!\n", 0, 0, 0 );
728
729
730
731
732
			if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
				/* FIXME: is it correct to abort
				 * operation if control fails? */
				goto return_results;
			}
733
734
735
		}                   
	}

736
	if( op->o_noop ) {
737
738
		if(( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
			rs->sr_text = "txn_abort (no-op) failed";
739
		} else {
740
			rs->sr_err = LDAP_X_NO_OPERATION;
741
			ltid = NULL;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
742
743
			/* Only free attrs if they were dup'd.  */
			if ( dummy.e_attrs == e->e_attrs ) dummy.e_attrs = NULL;
744
			goto return_results;
745
746
		}

747
	} else {
748
749
		rc = bdb_cache_modrdn( bdb, e, &op->orr_nnewrdn, &dummy, neip,
			locker, &lock );
Howard Chu's avatar
Howard Chu committed
750
751
752
753
754
		switch( rc ) {
		case DB_LOCK_DEADLOCK:
		case DB_LOCK_NOTGRANTED:
			goto retry;
		}
755
756
757
		dummy.e_attrs = NULL;
		new_dn.bv_val = NULL;
		new_ndn.bv_val = NULL;
758

759
760
761
762
763
		if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
			rs->sr_text = "txn_commit failed";
		} else {
			rs->sr_err = LDAP_SUCCESS;
		}
764
	}
765
 
766
767
	ltid = NULL;
	op->o_private = NULL;
768
 
769
	if( rs->sr_err != LDAP_SUCCESS ) {
770
771
		Debug( LDAP_DEBUG_TRACE,
			LDAP_XSTRING(bdb_modrdn) ": %s : %s (%d)\n",
772
773
			rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
		rs->sr_err = LDAP_OTHER;
774
775

		goto return_results;
776
777
	}

778
	Debug(LDAP_DEBUG_TRACE,
779
780
		LDAP_XSTRING(bdb_modrdn)
		": rdn modified%s id=%08lx dn=\"%s\"\n",
781
		op->o_noop ? " (no-op)" : "",
782
		dummy.e_id, op->o_req_dn.bv_val );
783
784
785
	rs->sr_text = NULL;
	if( num_ctrls ) rs->sr_ctrls = ctrls;

786
return_results:
787
788
789
	if ( dummy.e_attrs ) {
		attrs_free( dummy.e_attrs );
	}
790
	send_ldap_result( op, rs );
791

792
	if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
793
		TXN_CHECKPOINT( bdb->bi_dbenv,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
794
795
			bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
	}
796
797
798
799
	
	if ( rs->sr_err == LDAP_SUCCESS && parent_is_glue && parent_is_leaf ) {
		op->o_delete_glue_parent = 1;
	}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
800

801
done:
802
803
	slap_graduate_commit_csn( op );

804
	if( new_dn.bv_val != NULL ) free( new_dn.bv_val );
805
	if( new_ndn.bv_val != NULL ) free( new_ndn.bv_val );
806
807
808

	/* LDAP v3 Support */
	if( np != NULL ) {
809
		/* free new parent and reader lock */
810
		bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, np);
811
812
813
	}

	if( p != NULL ) {
814
		/* free parent and reader lock */
815
		bdb_unlocked_cache_return_entry_r(&bdb->bi_cache, p);
816
817
818
819
	}

	/* free entry */
	if( e != NULL ) {
820
		bdb_unlocked_cache_return_entry_w( &bdb->bi_cache, e);
821
822
823
	}

	if( ltid != NULL ) {
824
		TXN_ABORT( ltid );
825
	}
826
	op->o_private = NULL;
827

828
	if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
829
830
		slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
		slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
831
	}
832
	if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
833
834
		slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
		slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
835
	}
836
	return rs->sr_err;
837
}