search.c 30.9 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
/* search.c - search operation */
/* $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-2005 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>.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
15
16
17
18
19
20
21
22
 */

#include "portable.h"

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

#include "back-bdb.h"
23
#include "idl.h"
Kurt Zeilenga's avatar
Kurt Zeilenga committed
24
25

static int base_candidate(
26
	BackendDB	*be,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
27
28
	Entry	*e,
	ID		*ids );
29

30
static int search_candidates(
Howard Chu's avatar
Howard Chu committed
31
	Operation *op,
32
	SlapReply *rs,
33
	Entry *e,
34
	u_int32_t locker,
Howard Chu's avatar
Howard Chu committed
35
36
	ID	*ids,
	ID	*scopes );
37

38
39
static int parse_paged_cookie( Operation *op, SlapReply *rs );

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
40
static void send_paged_response( 
Pierangelo Masarati's avatar
Pierangelo Masarati committed
41
	Operation *op,
42
	SlapReply *rs,
43
	ID  *lastid,
44
	int tentries );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
45

46
47
48
/* Dereference aliases for a single alias entry. Return the final
 * dereferenced entry on success, NULL on any failure.
 */
49
static Entry * deref_base (
Howard Chu's avatar
Howard Chu committed
50
	Operation *op,
51
52
53
54
55
56
57
58
	SlapReply *rs,
	Entry *e,
	Entry **matched,
	u_int32_t locker,
	DB_LOCK *lock,
	ID	*tmp,
	ID	*visited )
{
Howard Chu's avatar
Howard Chu committed
59
	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
60
	struct berval ndn;
Howard Chu's avatar
Howard Chu committed
61
	EntryInfo *ei;
Howard Chu's avatar
Howard Chu committed
62
	DB_LOCK lockr;
63
64
65
66

	rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
	rs->sr_text = "maximum deref depth exceeded";

Howard Chu's avatar
Howard Chu committed
67
	while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
68
69
70
		/* Remember the last entry we looked at, so we can
		 * report broken links
		 */
71
		*matched = e;
72
73
74
75
76
77
78

		/* If this is part of a subtree or onelevel search,
		 * have we seen this ID before? If so, quit.
		 */
		if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
			e = NULL;
			break;
79
		}
80
81
82
83

		/* If we've seen this ID during this deref iteration,
		 * we've hit a loop.
		 */
84
85
86
87
88
89
		if ( bdb_idl_insert( tmp, e->e_id ) ) {
			rs->sr_err = LDAP_ALIAS_PROBLEM;
			rs->sr_text = "circular alias";
			e = NULL;
			break;
		}
90
91
92
93

		/* If there was a problem getting the aliasedObjectName,
		 * get_alias_dn will have set the error status.
		 */
94
95
96
97
		if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
			e = NULL;
			break;
		}
98

99
100
		rs->sr_err = bdb_dn2entry( op, NULL, &ndn, &ei,
			0, locker, &lockr );
Howard Chu's avatar
Howard Chu committed
101

102
103
104
105
106
		if ( ei ) {
			e = ei->bei_e;
		} else {
			e = NULL;
		}
Howard Chu's avatar
Howard Chu committed
107

108
109
110
111
112
		if (!e) {
			rs->sr_err = LDAP_ALIAS_PROBLEM;
			rs->sr_text = "aliasedObject not found";
			break;
		}
113
114
115
116

		/* Free the previous entry, continue to work with the
		 * one we just retrieved.
		 */
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
117
118
		bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
			*matched, lock);
Howard Chu's avatar
Howard Chu committed
119
		*lock = lockr;
120
121
122
123

		/* We found a regular entry. Return this to the caller. The
		 * entry is still locked for Read.
		 */
124
125
126
127
128
129
130
131
132
		if (!is_entry_alias(e)) {
			rs->sr_err = LDAP_SUCCESS;
			rs->sr_text = NULL;
			break;
		}
	}
	return e;
}

133
134
/* Look for and dereference all aliases within the search scope. Adds
 * the dereferenced entries to the "ids" list. Requires "stack" to be
135
136
 * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
 * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
137
 */
138
139
140
141
142
143
static int search_aliases(
	Operation *op,
	SlapReply *rs,
	Entry *e,
	u_int32_t locker,
	ID *ids,
Howard Chu's avatar
Howard Chu committed
144
	ID *scopes,
145
	ID *stack )
146
147
148
{
	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
	ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
149
	ID cursora, ida, cursoro, ido, *subscop2;
150
	Entry *matched, *a;
Howard Chu's avatar
Howard Chu committed
151
	EntryInfo *ei;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
152
	struct berval bv_alias = BER_BVC( "alias" );
153
#ifdef LDAP_COMP_MATCH
154
	AttributeAssertion aa_alias = { NULL, BER_BVNULL, NULL };
155
156
157
#else
	AttributeAssertion aa_alias = { NULL, BER_BVNULL };
#endif
158
	Filter	af;
Howard Chu's avatar
Howard Chu committed
159
160
	DB_LOCK locka, lockr;
	int first = 1;
161

162
	aliases = stack;	/* IDL of all aliases in the database */
Howard Chu's avatar
Howard Chu committed
163
164
165
166
167
168
	curscop = aliases + BDB_IDL_DB_SIZE;	/* Aliases in the current scope */
	subscop = curscop + BDB_IDL_DB_SIZE;	/* The current scope */
	visited = subscop + BDB_IDL_DB_SIZE;	/* IDs we've seen in this search */
	newsubs = visited + BDB_IDL_DB_SIZE;	/* New subtrees we've added */
	oldsubs = newsubs + BDB_IDL_DB_SIZE;	/* Subtrees added previously */
	tmp = oldsubs + BDB_IDL_DB_SIZE;	/* Scratch space for deref_base() */
169
170
171
172

	/* A copy of subscop, because subscop gets clobbered by
	 * the bdb_idl_union/intersection routines
	 */
Howard Chu's avatar
Howard Chu committed
173
	subscop2 = tmp + BDB_IDL_DB_SIZE;
174
175
176
177
178
179
180
181

	af.f_choice = LDAP_FILTER_EQUALITY;
	af.f_ava = &aa_alias;
	af.f_av_desc = slap_schema.si_ad_objectClass;
	af.f_av_value = bv_alias;
	af.f_next = NULL;

	/* Find all aliases in database */
182
	BDB_IDL_ZERO( aliases );
Howard Chu's avatar
Howard Chu committed
183
	rs->sr_err = bdb_filter_candidates( op, &af, aliases,
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
184
		curscop, visited );
185
186
187
188
189
190
	if (rs->sr_err != LDAP_SUCCESS) {
		return rs->sr_err;
	}
	oldsubs[0] = 1;
	oldsubs[1] = e->e_id;

Howard Chu's avatar
Howard Chu committed
191
192
	BDB_IDL_ZERO( ids );
	BDB_IDL_ZERO( visited );
193
194
	BDB_IDL_ZERO( newsubs );

195
	cursoro = 0;
196
197
198
	ido = bdb_idl_first( oldsubs, &cursoro );

	for (;;) {
199
200
		/* Set curscop to only the aliases in the current scope. Start with
		 * all the aliases, obtain the IDL for the current scope, and then
201
202
		 * get the intersection of these two IDLs. Add the current scope
		 * to the cumulative list of candidates.
203
		 */
204
		BDB_IDL_CPY( curscop, aliases );
205
		rs->sr_err = bdb_dn2idl( op, e, subscop,
206
			subscop2+BDB_IDL_DB_SIZE );
Howard Chu's avatar
Howard Chu committed
207
208
209
210
211
		if (first) {
			first = 0;
		} else {
			bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
		}
212
		BDB_IDL_CPY(subscop2, subscop);
213
		rs->sr_err = bdb_idl_intersection(curscop, subscop);
214
		bdb_idl_union( ids, subscop2 );
215

216
		/* Dereference all of the aliases in the current scope. */
217
		cursora = 0;
218
		for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
219
220
			ida = bdb_idl_next(curscop, &cursora))
		{
Howard Chu's avatar
Howard Chu committed
221
			ei = NULL;
222
retry1:
223
224
			rs->sr_err = bdb_cache_find_id(op, NULL,
				ida, &ei, 0, locker, &lockr );
225
			if (rs->sr_err != LDAP_SUCCESS) {
226
227
				if ( rs->sr_err == DB_LOCK_DEADLOCK ||
					rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
228
229
				continue;
			}
Howard Chu's avatar
Howard Chu committed
230
			a = ei->bei_e;
231
232
233
234

			/* This should only happen if the curscop IDL has maxed out and
			 * turned into a range that spans IDs indiscriminately
			 */
235
			if (!is_entry_alias(a)) {
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
236
237
				bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
					a, &lockr);
238
239
				continue;
			}
240
241

			/* Actually dereference the alias */
242
			BDB_IDL_ZERO(tmp);
Howard Chu's avatar
Howard Chu committed
243
			a = deref_base( op, rs, a, &matched, locker, &lockr,
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
244
				tmp, visited );
245
			if (a) {
246
				/* If the target was not already in our current candidates,
Howard Chu's avatar
Howard Chu committed
247
248
249
				 * make note of it in the newsubs list. Also
				 * set it in the scopes list so that bdb_search
				 * can check it.
250
				 */
251
				if (bdb_idl_insert(ids, a->e_id) == 0) {
252
					bdb_idl_insert(newsubs, a->e_id);
Howard Chu's avatar
Howard Chu committed
253
					bdb_idl_insert(scopes, a->e_id);
254
				}
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
255
256
257
				bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
					a, &lockr);

258
			} else if (matched) {
259
260
261
				/* Alias could not be dereferenced, or it deref'd to
				 * an ID we've already seen. Ignore it.
				 */
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
262
263
				bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
					matched, &lockr );
264
				rs->sr_text = NULL;
265
266
			}
		}
267
268
269
		/* If this is a OneLevel search, we're done; oldsubs only had one
		 * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
		 */
270
		if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
271
272
nextido:
		ido = bdb_idl_next( oldsubs, &cursoro );
273
274
275
276
		
		/* If we're done processing the old scopes, did we add any new
		 * scopes in this iteration? If so, go back and do those now.
		 */
277
278
279
280
		if (ido == NOID) {
			if (BDB_IDL_IS_ZERO(newsubs)) break;
			BDB_IDL_CPY(oldsubs, newsubs);
			BDB_IDL_ZERO(newsubs);
281
			cursoro = 0;
282
283
			ido = bdb_idl_first( oldsubs, &cursoro );
		}
284
285
286
287
288
289

		/* Find the entry corresponding to the next scope. If it can't
		 * be found, ignore it and move on. This should never happen;
		 * we should never see the ID of an entry that doesn't exist.
		 * Set the name so that the scope's IDL can be retrieved.
		 */
Howard Chu's avatar
Howard Chu committed
290
		ei = NULL;
291
sameido:
292
293
		rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
			0, locker, &locka );
294
295
296
297
298
299
		if ( rs->sr_err != LDAP_SUCCESS ) {
			if ( rs->sr_err == DB_LOCK_DEADLOCK ||
				rs->sr_err == DB_LOCK_NOTGRANTED )
				goto sameido;
			goto nextido;
		}
Howard Chu's avatar
Howard Chu committed
300
		e = ei->bei_e;
301
302
303
	}
	return rs->sr_err;
}
304

305
int
Howard Chu's avatar
Howard Chu committed
306
bdb_search( Operation *op, SlapReply *rs )
307
308
{
	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
309
310
	time_t		stoptime;
	ID		id, cursor;
311
	ID		candidates[BDB_IDL_UM_SIZE];
Howard Chu's avatar
Howard Chu committed
312
	ID		scopes[BDB_IDL_DB_SIZE];
313
	Entry		*e = NULL, base, e_root = {0};
314
	Entry		*matched = NULL;
315
	EntryInfo	*ei, ei_root = {0};
Kurt Zeilenga's avatar
Kurt Zeilenga committed
316
	struct berval	realbase = BER_BVNULL;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
317
#ifdef SLAP_ACL_HONOR_DISCLOSE
318
	slap_mask_t	mask;
Hallvard Furuseth's avatar
Hallvard Furuseth committed
319
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
320
	int		manageDSAit;
321
	int		tentries = 0;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
322
	ID		lastid = NOID;
323
	AttributeName	*attrs;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
324

325
326
327
328
329
	u_int32_t	locker = 0;
	DB_LOCK		lock;
	struct	bdb_op_info	*opinfo = NULL;
	DB_TXN			*ltid = NULL;

330
	Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
Howard Chu's avatar
Howard Chu committed
331
	attrs = op->oq_search.rs_attrs;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
332

333
334
	opinfo = (struct bdb_op_info *) op->o_private;

Howard Chu's avatar
Howard Chu committed
335
	manageDSAit = get_manageDSAit( op );
336

337
	if ( opinfo && opinfo->boi_txn ) {
338
339
		ltid = opinfo->boi_txn;
		locker = TXN_ID( ltid );
340
341
	} else {
		rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
342

343
344
345
346
		switch(rs->sr_err) {
		case 0:
			break;
		default:
Howard Chu's avatar
Howard Chu committed
347
			send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
348
349
			return rs->sr_err;
		}
350
	}
351

Howard Chu's avatar
Howard Chu committed
352
	if ( op->o_req_ndn.bv_len == 0 ) {
353
		/* DIT root special case */
354
355
356
		ei_root.bei_e = &e_root;
		ei_root.bei_parent = &ei_root;
		e_root.e_private = &ei_root;
Howard Chu's avatar
Howard Chu committed
357
		e_root.e_id = 0;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
358
359
		BER_BVSTR( &e_root.e_nname, "" );
		BER_BVSTR( &e_root.e_name, "" );
360
		ei = &ei_root;
361
		rs->sr_err = LDAP_SUCCESS;
362
	} else {
363
dn2entry_retry:
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
364
		/* get entry with reader lock */
Howard Chu's avatar
Howard Chu committed
365
		rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
366
			1, locker, &lock );
367
	}
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
368

369
	switch(rs->sr_err) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
370
	case DB_NOTFOUND:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
371
372
		matched = ei->bei_e;
		break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
373
	case 0:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
374
375
		e = ei->bei_e;
		break;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
376
	case LDAP_BUSY:
Howard Chu's avatar
Howard Chu committed
377
		send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
378
379
		if ( !opinfo )
			LOCK_ID_FREE (bdb->bi_dbenv, locker );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
380
		return LDAP_BUSY;
381
382
383
	case DB_LOCK_DEADLOCK:
	case DB_LOCK_NOTGRANTED:
		goto dn2entry_retry;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
384
	default:
Howard Chu's avatar
Howard Chu committed
385
		send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
386
387
		if ( !opinfo )
			LOCK_ID_FREE (bdb->bi_dbenv, locker );
388
		return rs->sr_err;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
389
390
	}

391
392
	if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
		BDB_IDL_ZERO(candidates);
Howard Chu's avatar
Howard Chu committed
393
		e = deref_base( op, rs, e, &matched, locker, &lock,
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
394
			candidates, NULL );
395
396
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
397
	if ( e == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
398
		struct berval matched_dn = BER_BVNULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
399
400

		if ( matched != NULL ) {
401
			BerVarray erefs = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
402

403
404
405
#ifdef SLAP_ACL_HONOR_DISCLOSE
			/* return referral only if "disclose"
			 * is granted on the object */
406
407
408
409
410
411
			if ( ! access_allowed( op, matched,
						slap_schema.si_ad_entry,
						NULL, ACL_DISCLOSE, NULL ) )
			{
				rs->sr_err = LDAP_NO_SUCH_OBJECT;

412
413
414
			} else
#endif /* SLAP_ACL_HONOR_DISCLOSE */
			{
415
416
417
418
419
420
421
422
				ber_dupbv( &matched_dn, &matched->e_name );

				erefs = is_entry_referral( matched )
					? get_entry_referrals( op, matched )
					: NULL;
				rs->sr_err = LDAP_REFERRAL;
				rs->sr_matched = matched_dn.bv_val;
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
423

424
425
426
#ifdef SLAP_ZONE_ALLOC
			slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
#endif
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
427
428
			bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
				matched, &lock);
429
430
			matched = NULL;

431
			if ( erefs ) {
432
				rs->sr_ref = referral_rewrite( erefs, &matched_dn,
Howard Chu's avatar
Howard Chu committed
433
					&op->o_req_dn, op->oq_search.rs_scope );
434
				ber_bvarray_free( erefs );
435
436
			}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
437
		} else {
438
439
440
#ifdef SLAP_ZONE_ALLOC
			slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
#endif
441
			rs->sr_ref = referral_rewrite( default_referral,
Howard Chu's avatar
Howard Chu committed
442
				NULL, &op->o_req_dn, op->oq_search.rs_scope );
443
			rs->sr_err = LDAP_REFERRAL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
444
445
		}

Howard Chu's avatar
Howard Chu committed
446
		send_ldap_result( op, rs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
447

448
449
		if ( !opinfo )
			LOCK_ID_FREE (bdb->bi_dbenv, locker );
450
451
452
453
		if ( rs->sr_ref ) {
			ber_bvarray_free( rs->sr_ref );
			rs->sr_ref = NULL;
		}
454
		if ( !BER_BVISNULL( &matched_dn ) ) {
455
456
457
458
			ber_memfree( matched_dn.bv_val );
			rs->sr_matched = NULL;
		}
		return rs->sr_err;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
459
460
	}

461
462
463
464
465
#ifdef SLAP_ACL_HONOR_DISCLOSE
	/* NOTE: __NEW__ "search" access is required
	 * on searchBase object */
	if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
				NULL, ACL_SEARCH, NULL, &mask ) )
466
	{
467
468
469
470
471
		if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
			rs->sr_err = LDAP_NO_SUCH_OBJECT;
		} else {
			rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
		}
472
473
474
475
476
477
478

#ifdef SLAP_ZONE_ALLOC
		slap_zn_runlock(bdb->bi_cache.c_zctx, e);
#endif
		if ( e != &e_root ) {
			bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
		}
Howard Chu's avatar
Howard Chu committed
479
		send_ldap_result( op, rs );
480
481
		return 1;
	}
482
#endif /* SLAP_ACL_HONOR_DISCLOSE */
483

484
	if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
485
		/* entry is a referral, don't allow add */
486
487
		struct berval matched_dn = BER_BVNULL;
		BerVarray erefs = NULL;
488
489
		
		ber_dupbv( &matched_dn, &e->e_name );
490
		erefs = get_entry_referrals( op, e );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
491

492
493
		rs->sr_err = LDAP_REFERRAL;

494
495
496
#ifdef SLAP_ZONE_ALLOC
		slap_zn_runlock(bdb->bi_cache.c_zctx, e);
#endif
497
		bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
498
		e = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
499

500
		if ( erefs ) {
501
			rs->sr_ref = referral_rewrite( erefs, &matched_dn,
Howard Chu's avatar
Howard Chu committed
502
				&op->o_req_dn, op->oq_search.rs_scope );
503
			ber_bvarray_free( erefs );
504
505
506
507

			if ( !rs->sr_ref ) {
				rs->sr_text = "bad_referral object";
			}
508
509
		}

510
511
		Debug( LDAP_DEBUG_TRACE,
			LDAP_XSTRING(bdb_search) ": entry is referral\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
512
513
			0, 0, 0 );

514
		rs->sr_matched = matched_dn.bv_val;
Howard Chu's avatar
Howard Chu committed
515
		send_ldap_result( op, rs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
516

517
		if ( !opinfo ) {
518
			LOCK_ID_FREE (bdb->bi_dbenv, locker );
519
		}
520
521
		ber_bvarray_free( rs->sr_ref );
		rs->sr_ref = NULL;
522
		ber_memfree( matched_dn.bv_val );
523
		rs->sr_matched = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
524
525
526
		return 1;
	}

527
528
529
530
	if ( get_assert( op ) &&
		( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
	{
		rs->sr_err = LDAP_ASSERTION_FAILED;
531
532
533
#ifdef SLAP_ZONE_ALLOC
		slap_zn_runlock(bdb->bi_cache.c_zctx, e);
#endif
534
535
536
		if ( e != &e_root ) {
			bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
		}
Howard Chu's avatar
Howard Chu committed
537
		send_ldap_result( op, rs );
538
539
540
		return 1;
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
541
	/* compute it anyway; root does not use it */
Howard Chu's avatar
Howard Chu committed
542
	stoptime = op->o_time + op->ors_tlimit;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
543

Howard Chu's avatar
Howard Chu committed
544
545
546
	/* need normalized dn below */
	ber_dupbv( &realbase, &e->e_nname );

547
548
549
	/* Copy info to base, must free entry before accessing the database
	 * in search_candidates, to avoid deadlocks.
	 */
550
	base.e_private = e->e_private;
551
552
	base.e_nname = realbase;
	base.e_id = e->e_id;
Howard Chu's avatar
Howard Chu committed
553

554
555
556
#ifdef SLAP_ZONE_ALLOC
	slap_zn_runlock(bdb->bi_cache.c_zctx, e);
#endif
557
	if ( e != &e_root ) {
Howard Chu's avatar
Howard Chu committed
558
559
560
561
		bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
	}
	e = NULL;

562
	/* select candidates */
Howard Chu's avatar
Howard Chu committed
563
	if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
564
		rs->sr_err = base_candidate( op->o_bd, &base, candidates );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
565
566

	} else {
567
		BDB_IDL_ZERO( candidates );
Howard Chu's avatar
Howard Chu committed
568
		BDB_IDL_ZERO( scopes );
Howard Chu's avatar
Howard Chu committed
569
		rs->sr_err = search_candidates( op, rs, &base,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
570
			locker, candidates, scopes );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
571
572
	}

573
	/* start cursor at beginning of candidates.
574
	 */
575
	cursor = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
576
577

	if ( candidates[0] == 0 ) {
578
579
		Debug( LDAP_DEBUG_TRACE,
			LDAP_XSTRING(bdb_search) ": no candidates\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
580
581
			0, 0, 0 );

582
		goto nochange;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
583
584
	}

585
	/* if not root and candidates exceed to-be-checked entries, abort */
Howard Chu's avatar
Howard Chu committed
586
587
588
	if ( op->ors_limit	/* isroot == FALSE */ &&
		op->ors_limit->lms_s_unchecked != -1 &&
		BDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
589
590
	{
		rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
Howard Chu's avatar
Howard Chu committed
591
		send_ldap_result( op, rs );
592
593
		rs->sr_err = LDAP_SUCCESS;
		goto done;
594
595
	}

Howard Chu's avatar
Howard Chu committed
596
597
	if ( op->ors_limit == NULL	/* isroot == TRUE */ ||
		!op->ors_limit->lms_s_pr_hide )
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
598
	{
599
600
601
		tentries = BDB_IDL_N(candidates);
	}

Howard Chu's avatar
Howard Chu committed
602
603
	if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
		PagedResultsState *ps = op->o_pagedresults_state;
604
		/* deferred cookie parsing */
Howard Chu's avatar
Howard Chu committed
605
		rs->sr_err = parse_paged_cookie( op, rs );
606
		if ( rs->sr_err != LDAP_SUCCESS ) {
Howard Chu's avatar
Howard Chu committed
607
			send_ldap_result( op, rs );
608
609
610
			goto done;
		}

Howard Chu's avatar
Howard Chu committed
611
		if ( (ID)( ps->ps_cookie ) == 0 ) {
Howard Chu's avatar
Howard Chu committed
612
			id = bdb_idl_first( candidates, &cursor );
613

Pierangelo Masarati's avatar
Pierangelo Masarati committed
614
		} else {
Howard Chu's avatar
Howard Chu committed
615
			if ( ps->ps_size == 0 ) {
616
617
				rs->sr_err = LDAP_SUCCESS;
				rs->sr_text = "search abandoned by pagedResult size=0";
Howard Chu's avatar
Howard Chu committed
618
				send_ldap_result( op, rs );
619
620
				goto done;
			}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
621
			for ( id = bdb_idl_first( candidates, &cursor );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
622
				id != NOID &&
Howard Chu's avatar
Howard Chu committed
623
					id <= (ID)( ps->ps_cookie );
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
624
625
626
627
				id = bdb_idl_next( candidates, &cursor ) )
			{
				/* empty */;
			}
Pierangelo Masarati's avatar
Pierangelo Masarati committed
628
		}
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
629

Pierangelo Masarati's avatar
Pierangelo Masarati committed
630
631
		if ( cursor == NOID ) {
			Debug( LDAP_DEBUG_TRACE, 
632
633
				LDAP_XSTRING(bdb_search)
				": no paged results candidates\n",
Pierangelo Masarati's avatar
Pierangelo Masarati committed
634
				0, 0, 0 );
Howard Chu's avatar
Howard Chu committed
635
			send_paged_response( op, rs, &lastid, 0 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
636

637
			rs->sr_err = LDAP_OTHER;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
638
639
640
641
642
			goto done;
		}
		goto loop_begin;
	}

643
	for ( id = bdb_idl_first( candidates, &cursor );
644
		  id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
645
	{
646
		int scopeok;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
647

Pierangelo Masarati's avatar
Pierangelo Masarati committed
648
loop_begin:
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
649

650
		/* check for abandon */
Howard Chu's avatar
Howard Chu committed
651
		if ( op->o_abandon ) {
652
			rs->sr_err = SLAPD_ABANDON;
653
654
			goto done;
		}
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
655

Kurt Zeilenga's avatar
Kurt Zeilenga committed
656
		/* check time limit */
Howard Chu's avatar
Howard Chu committed
657
		if ( op->ors_tlimit != SLAP_NO_LIMIT
658
659
				&& slap_get_time() > stoptime )
		{
660
661
			rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
			rs->sr_ref = rs->sr_v2ref;
Howard Chu's avatar
Howard Chu committed
662
			send_ldap_result( op, rs );
663
			rs->sr_err = LDAP_SUCCESS;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
664
665
666
			goto done;
		}

667
fetch_entry_retry:
668
			/* get the entry with reader lock */
Howard Chu's avatar
Howard Chu committed
669
			ei = NULL;
670
			rs->sr_err = bdb_cache_find_id( op, ltid,
671
				id, &ei, 0, locker, &lock );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
672

673
674
			if (rs->sr_err == LDAP_BUSY) {
				rs->sr_text = "ldap server busy";
Howard Chu's avatar
Howard Chu committed
675
				send_ldap_result( op, rs );
676
				goto done;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
677

Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
678
679
680
			} else if ( rs->sr_err == DB_LOCK_DEADLOCK
				|| rs->sr_err == DB_LOCK_NOTGRANTED )
			{
681
				goto fetch_entry_retry;
682
			}
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
683

684
			if ( ei && rs->sr_err == LDAP_SUCCESS ) {
Howard Chu's avatar
Howard Chu committed
685
686
687
688
689
				e = ei->bei_e;
			} else {
				e = NULL;
			}

690
			if ( e == NULL ) {
691
				if( !BDB_IDL_IS_RANGE(candidates) ) {
692
693
					/* only complain for non-range IDLs */
					Debug( LDAP_DEBUG_TRACE,
694
695
						LDAP_XSTRING(bdb_search)
						": candidate %ld not found\n",
696
697
						(long) id, 0, 0 );
				}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
698

699
700
				goto loop_continue;
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
701

702
		rs->sr_entry = e;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
703

704
#ifdef BDB_SUBENTRIES
Kurt Zeilenga's avatar
Kurt Zeilenga committed
705
		{
706
			if ( is_entry_subentry( e ) ) {
Howard Chu's avatar
Howard Chu committed
707
708
				if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
					if(!get_subentries_visibility( op )) {
709
710
711
712
						/* only subentries are visible */
						goto loop_continue;
					}

Howard Chu's avatar
Howard Chu committed
713
714
				} else if ( get_subentries( op ) &&
					!get_subentries_visibility( op ))
715
				{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
716
717
718
					/* only subentries are visible */
					goto loop_continue;
				}
719

Howard Chu's avatar
Howard Chu committed
720
			} else if ( get_subentries_visibility( op )) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
721
722
723
724
				/* only subentries are visible */
				goto loop_continue;
			}
		}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
725
#endif /* BDB_SUBENTRIES */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
726

727
728
729
730
731
732
733
734
735
		/* Does this candidate actually satisfy the search scope?
		 *
		 * Note that we don't lock access to the bei_parent pointer.
		 * Since only leaf nodes can be deleted, the parent of any
		 * node will always be a valid node. Also since we have
		 * a Read lock on the data, it cannot be renamed out of the
		 * scope while we are looking at it, and unless we're using
		 * BDB_HIER, its parents cannot be moved either.
		 */
736
		scopeok = 0;
Howard Chu's avatar
Howard Chu committed
737
		switch( op->ors_scope ) {
738
739
		case LDAP_SCOPE_BASE:
			/* This is always true, yes? */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
740
			if ( id == base.e_id ) scopeok = 1;
741
			break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
742

743
		case LDAP_SCOPE_ONELEVEL:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
744
			if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
745
			break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
746

747
#ifdef LDAP_SCOPE_CHILDREN
748
749
750
		case LDAP_SCOPE_CHILDREN:
			if ( id == base.e_id ) break;
			/* Fall-thru */
751
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
752
		case LDAP_SCOPE_SUBTREE: {
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
753
			EntryInfo *tmp;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
754
			for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
755
756
757
758
				if ( tmp->bei_id == base.e_id ) {
					scopeok = 1;
					break;
				}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
759
760
			}
			} break;
761
762
		}

763
		/* aliases were already dereferenced in candidate list */
Howard Chu's avatar
Howard Chu committed
764
		if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
Howard Chu's avatar
Howard Chu committed
765
766
767
			/* but if the search base is an alias, and we didn't
			 * deref it when finding, return it.
			 */
Howard Chu's avatar
Howard Chu committed
768
			if ( is_entry_alias(e) &&
Howard Chu's avatar
Howard Chu committed
769
				((op->ors_deref & LDAP_DEREF_FINDING) ||
Kurt Zeilenga's avatar
Kurt Zeilenga committed
770
					!bvmatch(&e->e_nname, &op->o_req_ndn)))
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
771
			{
Howard Chu's avatar
Howard Chu committed
772
				goto loop_continue;
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
773
			}
Howard Chu's avatar
Howard Chu committed
774
775

			/* scopes is only non-empty for onelevel or subtree */
776
			if ( !scopeok && BDB_IDL_N(scopes) ) {
Howard Chu's avatar
Howard Chu committed
777
				unsigned x;
Howard Chu's avatar
Howard Chu committed
778
				if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
779
780
					x = bdb_idl_search( scopes, e->e_id );
					if ( scopes[x] == e->e_id ) scopeok = 1;
Howard Chu's avatar
Howard Chu committed
781
				} else {
782
					/* subtree, walk up the tree */
Howard Chu's avatar
Howard Chu committed
783
					EntryInfo *tmp = BEI(e);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
784
785
					for (;tmp->bei_parent; tmp=tmp->bei_parent) {
						x = bdb_idl_search( scopes, tmp->bei_id );
Howard Chu's avatar
Howard Chu committed
786
787
788
789
790
791
792
						if ( scopes[x] == tmp->bei_id ) {
							scopeok = 1;
							break;
						}
					}
				}
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
793
		}
794
795

		/* Not in scope, ignore it */
796
797
		if ( !scopeok )
		{
798
			Debug( LDAP_DEBUG_TRACE,
799
800
				LDAP_XSTRING(bdb_search)
				": %ld scope not okay\n",
801
802
803
804
				(long) id, 0, 0 );
			goto loop_continue;
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
805
806
807
808
809
		/*
		 * if it's a referral, add it to the list of referrals. only do
		 * this for non-base searches, and don't check the filter
		 * explicitly here since it's only a candidate anyway.
		 */
Howard Chu's avatar
Howard Chu committed
810
		if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
811
			&& is_entry_referral( e ) )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
812
		{
Howard Chu's avatar
Howard Chu committed
813
			BerVarray erefs = get_entry_referrals( op, e );
814
			rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
Howard Chu's avatar
Howard Chu committed
815
				op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
816
					? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
817

Howard Chu's avatar
Howard Chu committed
818
			send_search_reference( op, rs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
819

820
821
822
			ber_bvarray_free( rs->sr_ref );
			ber_bvarray_free( erefs );
			rs->sr_ref = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
823
824
825
826

			goto loop_continue;
		}

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
827
		if ( !manageDSAit && is_entry_glue( e )) {
828
829
830
			goto loop_continue;
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
831
		/* if it matches the filter and scope, send it */
Howard Chu's avatar
Howard Chu committed
832
		rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
833

834
		if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
835
			/* check size limit */
Pierangelo Masarati's avatar
Pierangelo Masarati committed
836
			if ( --op->ors_slimit == -1 ) {
837
#ifdef SLAP_ZONE_ALLOC
838
				slap_zn_runlock(bdb->bi_cache.c_zctx, e);
839
#endif
840
				bdb_cache_return_entry_r( bdb->bi_dbenv,
841
						&bdb->bi_cache, e, &lock );
842
843
844
845
				e = NULL;
				rs->sr_entry = NULL;
				rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
				rs->sr_ref = rs->sr_v2ref;
Howard Chu's avatar
Howard Chu committed
846
				send_ldap_result( op, rs );