search.c 29.6 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-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>.
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
	BDB_LOCKER 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
	SlapReply *rs,
	Entry *e,
	Entry **matched,
54
	BDB_LOCKER locker,
55
56
57
58
	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";

67
	for (;;) {
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
		if (BDB_IDL_N(tmp) >= op->o_bd->be_max_deref_depth) {
			e = NULL;
			break;
		}

78
79
80
81
82
83
		/* 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;
84
		}
85
86
87
88

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

		/* If there was a problem getting the aliasedObjectName,
		 * get_alias_dn will have set the error status.
		 */
99
100
101
102
		if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
			e = NULL;
			break;
		}
103

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

107
108
109
110
111
		if ( ei ) {
			e = ei->bei_e;
		} else {
			e = NULL;
		}
Howard Chu's avatar
Howard Chu committed
112

113
114
115
116
117
		if (!e) {
			rs->sr_err = LDAP_ALIAS_PROBLEM;
			rs->sr_text = "aliasedObject not found";
			break;
		}
118
119
120
121

		/* Free the previous entry, continue to work with the
		 * one we just retrieved.
		 */
Howard Chu's avatar
Howard Chu committed
122
		bdb_cache_return_entry_r( bdb, *matched, lock);
Howard Chu's avatar
Howard Chu committed
123
		*lock = lockr;
124
125
126
127

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

137
138
/* Look for and dereference all aliases within the search scope. Adds
 * the dereferenced entries to the "ids" list. Requires "stack" to be
139
140
 * 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.
141
 */
142
143
144
145
static int search_aliases(
	Operation *op,
	SlapReply *rs,
	Entry *e,
146
	BDB_LOCKER locker,
147
	ID *ids,
Howard Chu's avatar
Howard Chu committed
148
	ID *scopes,
149
	ID *stack )
150
151
152
{
	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
	ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
153
	ID cursora, ida, cursoro, ido, *subscop2;
154
	Entry *matched, *a;
Howard Chu's avatar
Howard Chu committed
155
	EntryInfo *ei;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
156
	struct berval bv_alias = BER_BVC( "alias" );
Ralf Haferkamp's avatar
Ralf Haferkamp committed
157
	AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
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, locker, &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, locker, &e->e_nname, BEI(e), subscop,
206
			subscop2+BDB_IDL_DB_SIZE );
Howard Chu's avatar
Howard Chu committed
207
208
209
		if (first) {
			first = 0;
		} else {
Howard Chu's avatar
Howard Chu committed
210
			bdb_cache_return_entry_r (bdb, e, &locka);
Howard Chu's avatar
Howard Chu committed
211
		}
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)) {
Howard Chu's avatar
Howard Chu committed
236
				bdb_cache_return_entry_r (bdb, a, &lockr);
237
238
				continue;
			}
239
240

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

256
			} else if (matched) {
257
258
259
				/* Alias could not be dereferenced, or it deref'd to
				 * an ID we've already seen. Ignore it.
				 */
Howard Chu's avatar
Howard Chu committed
260
				bdb_cache_return_entry_r( bdb, matched, &lockr );
261
				rs->sr_text = NULL;
262
263
			}
		}
264
265
266
		/* 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.
		 */
267
		if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
268
269
nextido:
		ido = bdb_idl_next( oldsubs, &cursoro );
270
271
272
273
		
		/* 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.
		 */
274
275
276
277
		if (ido == NOID) {
			if (BDB_IDL_IS_ZERO(newsubs)) break;
			BDB_IDL_CPY(oldsubs, newsubs);
			BDB_IDL_ZERO(newsubs);
278
			cursoro = 0;
279
280
			ido = bdb_idl_first( oldsubs, &cursoro );
		}
281
282
283
284
285
286

		/* 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
287
		ei = NULL;
288
sameido:
289
290
		rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
			0, locker, &locka );
291
292
293
294
295
296
		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
297
		e = ei->bei_e;
298
299
300
	}
	return rs->sr_err;
}
301

302
int
Howard Chu's avatar
Howard Chu committed
303
bdb_search( Operation *op, SlapReply *rs )
304
305
{
	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
306
	ID		id, cursor;
307
	ID		lastid = NOID;
308
	ID		candidates[BDB_IDL_UM_SIZE];
Howard Chu's avatar
Howard Chu committed
309
	ID		scopes[BDB_IDL_DB_SIZE];
310
	Entry		*e = NULL, base, *e_root;
311
	Entry		*matched = NULL;
312
	EntryInfo	*ei;
313
	AttributeName	*attrs;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
314
	struct berval	realbase = BER_BVNULL;
315
	slap_mask_t	mask;
316
	time_t		stoptime;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
317
	int		manageDSAit;
318
319
	int		tentries = 0, nentries = 0;
	int		idflag = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
320

321
	BDB_LOCKER	locker = 0;
322
323
324
325
	DB_LOCK		lock;
	struct	bdb_op_info	*opinfo = NULL;
	DB_TXN			*ltid = NULL;

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

329
330
	opinfo = (struct bdb_op_info *) op->o_private;

Howard Chu's avatar
Howard Chu committed
331
	manageDSAit = get_manageDSAit( op );
332

333
	if ( opinfo && opinfo->boi_txn ) {
334
335
		ltid = opinfo->boi_txn;
		locker = TXN_ID( ltid );
336
337
	} else {
		rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
338

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

348
	e_root = bdb->bi_cache.c_dntree.bei_e;
Howard Chu's avatar
Howard Chu committed
349
	if ( op->o_req_ndn.bv_len == 0 ) {
350
		/* DIT root special case */
351
		ei = e_root->e_private;
352
		rs->sr_err = LDAP_SUCCESS;
353
	} else {
354
355
356
		if ( op->ors_deref & LDAP_DEREF_FINDING ) {
			BDB_IDL_ZERO(candidates);
		}
357
dn2entry_retry:
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
358
		/* get entry with reader lock */
Howard Chu's avatar
Howard Chu committed
359
		rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
360
			1, locker, &lock );
361
	}
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
362

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

385
386
387
388
389
390
391
392
393
394
395
	if ( op->ors_deref & LDAP_DEREF_FINDING ) {
		if ( matched && is_entry_alias( matched )) {
			struct berval stub;

			stub.bv_val = op->o_req_ndn.bv_val;
			stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
			e = deref_base( op, rs, matched, &matched, locker, &lock,
				candidates, NULL );
			if ( e ) {
				build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
					op->o_tmpmemctx );
Howard Chu's avatar
Howard Chu committed
396
				bdb_cache_return_entry_r (bdb, e, &lock);
397
398
399
400
401
402
403
				matched = NULL;
				goto dn2entry_retry;
			}
		} else if ( e && is_entry_alias( e )) {
			e = deref_base( op, rs, e, &matched, locker, &lock,
				candidates, NULL );
		}
404
405
	}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
406
	if ( e == NULL ) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
407
		struct berval matched_dn = BER_BVNULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
408
409

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

412
413
			/* return referral only if "disclose"
			 * is granted on the object */
414
415
416
417
418
419
			if ( ! access_allowed( op, matched,
						slap_schema.si_ad_entry,
						NULL, ACL_DISCLOSE, NULL ) )
			{
				rs->sr_err = LDAP_NO_SUCH_OBJECT;

420
			} else {
421
422
423
424
425
				ber_dupbv( &matched_dn, &matched->e_name );

				erefs = is_entry_referral( matched )
					? get_entry_referrals( op, matched )
					: NULL;
426
427
				if ( rs->sr_err == DB_NOTFOUND )
					rs->sr_err = LDAP_REFERRAL;
428
429
				rs->sr_matched = matched_dn.bv_val;
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
430

431
432
433
#ifdef SLAP_ZONE_ALLOC
			slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
#endif
Howard Chu's avatar
Howard Chu committed
434
			bdb_cache_return_entry_r (bdb, matched, &lock);
435
436
			matched = NULL;

437
			if ( erefs ) {
438
				rs->sr_ref = referral_rewrite( erefs, &matched_dn,
Howard Chu's avatar
Howard Chu committed
439
					&op->o_req_dn, op->oq_search.rs_scope );
440
				ber_bvarray_free( erefs );
441
442
			}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
443
		} else {
444
445
446
#ifdef SLAP_ZONE_ALLOC
			slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
#endif
447
			rs->sr_ref = referral_rewrite( default_referral,
Howard Chu's avatar
Howard Chu committed
448
				NULL, &op->o_req_dn, op->oq_search.rs_scope );
449
			rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
450
451
		}

Howard Chu's avatar
Howard Chu committed
452
		send_ldap_result( op, rs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
453

454
455
		if ( !opinfo )
			LOCK_ID_FREE (bdb->bi_dbenv, locker );
456
457
458
459
		if ( rs->sr_ref ) {
			ber_bvarray_free( rs->sr_ref );
			rs->sr_ref = NULL;
		}
460
		if ( !BER_BVISNULL( &matched_dn ) ) {
461
462
463
464
			ber_memfree( matched_dn.bv_val );
			rs->sr_matched = NULL;
		}
		return rs->sr_err;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
465
466
	}

467
468
469
470
	/* 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 ) )
471
	{
472
473
474
475
476
		if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
			rs->sr_err = LDAP_NO_SUCH_OBJECT;
		} else {
			rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
		}
477
478
479
480

#ifdef SLAP_ZONE_ALLOC
		slap_zn_runlock(bdb->bi_cache.c_zctx, e);
#endif
481
		if ( e != e_root ) {
Howard Chu's avatar
Howard Chu committed
482
			bdb_cache_return_entry_r(bdb, e, &lock);
483
		}
Howard Chu's avatar
Howard Chu committed
484
		send_ldap_result( op, rs );
485
		return rs->sr_err;
486
487
	}

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

496
497
		rs->sr_err = LDAP_REFERRAL;

498
499
500
#ifdef SLAP_ZONE_ALLOC
		slap_zn_runlock(bdb->bi_cache.c_zctx, e);
#endif
Howard Chu's avatar
Howard Chu committed
501
		bdb_cache_return_entry_r( bdb, e, &lock );
502
		e = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
503

504
		if ( erefs ) {
505
			rs->sr_ref = referral_rewrite( erefs, &matched_dn,
Howard Chu's avatar
Howard Chu committed
506
				&op->o_req_dn, op->oq_search.rs_scope );
507
			ber_bvarray_free( erefs );
508
509
510
511

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

514
515
		Debug( LDAP_DEBUG_TRACE,
			LDAP_XSTRING(bdb_search) ": entry is referral\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
516
517
			0, 0, 0 );

518
		rs->sr_matched = matched_dn.bv_val;
Howard Chu's avatar
Howard Chu committed
519
		send_ldap_result( op, rs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
520

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

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

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

Howard Chu's avatar
Howard Chu committed
548
549
550
	/* need normalized dn below */
	ber_dupbv( &realbase, &e->e_nname );

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

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

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

	} else {
571
		BDB_IDL_ZERO( candidates );
Howard Chu's avatar
Howard Chu committed
572
		BDB_IDL_ZERO( scopes );
Howard Chu's avatar
Howard Chu committed
573
		rs->sr_err = search_candidates( op, rs, &base,
Kurt Zeilenga's avatar
Kurt Zeilenga committed
574
			locker, candidates, scopes );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
575
576
	}

577
	/* start cursor at beginning of candidates.
578
	 */
579
	cursor = 0;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
580
581

	if ( candidates[0] == 0 ) {
582
583
		Debug( LDAP_DEBUG_TRACE,
			LDAP_XSTRING(bdb_search) ": no candidates\n",
Kurt Zeilenga's avatar
Kurt Zeilenga committed
584
585
			0, 0, 0 );

586
		goto nochange;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
587
588
	}

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

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

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

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
615
616
617
618
619
620
		cursor = (ID) ps->ps_cookie;
		if ( cursor && ps->ps_size == 0 ) {
			rs->sr_err = LDAP_SUCCESS;
			rs->sr_text = "search abandoned by pagedResult size=0";
			send_ldap_result( op, rs );
			goto done;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
621
		}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
622
623
		id = bdb_idl_first( candidates, &cursor );
		if ( id == NOID ) {
Pierangelo Masarati's avatar
Pierangelo Masarati committed
624
			Debug( LDAP_DEBUG_TRACE, 
625
626
				LDAP_XSTRING(bdb_search)
				": no paged results candidates\n",
Pierangelo Masarati's avatar
Pierangelo Masarati committed
627
				0, 0, 0 );
Howard Chu's avatar
Howard Chu committed
628
			send_paged_response( op, rs, &lastid, 0 );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
629

630
			rs->sr_err = LDAP_OTHER;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
631
632
			goto done;
		}
633
		nentries = ps->ps_count;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
634
635
		if ( id == (ID)ps->ps_cookie )
			id = bdb_idl_next( candidates, &cursor );
Pierangelo Masarati's avatar
Pierangelo Masarati committed
636
637
638
		goto loop_begin;
	}

639
	for ( id = bdb_idl_first( candidates, &cursor );
640
		  id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
641
	{
642
		int scopeok;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
643

Pierangelo Masarati's avatar
Pierangelo Masarati committed
644
loop_begin:
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
645

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

652
653
654
655
656
657
658
659
660
		/* mostly needed by internal searches,
		 * e.g. related to syncrepl, for whom
		 * abandon does not get set... */
		if ( slapd_shutdown ) {
			rs->sr_err = LDAP_UNAVAILABLE;
			send_ldap_disconnect( op, rs );
			goto done;
		}

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

672
673
674
675
676
677
678
679
		/* If we inspect more entries than will
		 * fit into the entry cache, stop caching
		 * any subsequent entries
		 */
		nentries++;
		if ( nentries > bdb->bi_cache.c_maxsize && !idflag )
			idflag = ID_NOCACHE;

680
fetch_entry_retry:
681
			/* get the entry with reader lock */
Howard Chu's avatar
Howard Chu committed
682
			ei = NULL;
683
			rs->sr_err = bdb_cache_find_id( op, ltid,
684
				id, &ei, idflag, locker, &lock );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
685

686
687
			if (rs->sr_err == LDAP_BUSY) {
				rs->sr_text = "ldap server busy";
Howard Chu's avatar
Howard Chu committed
688
				send_ldap_result( op, rs );
689
				goto done;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
690

Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
691
692
693
			} else if ( rs->sr_err == DB_LOCK_DEADLOCK
				|| rs->sr_err == DB_LOCK_NOTGRANTED )
			{
694
				goto fetch_entry_retry;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
695
696
697
698
			} else if ( rs->sr_err == LDAP_OTHER ) {
				rs->sr_text = "internal error";
				send_ldap_result( op, rs );
				goto done;
699
			}
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
700

701
			if ( ei && rs->sr_err == LDAP_SUCCESS ) {
Howard Chu's avatar
Howard Chu committed
702
703
704
705
706
				e = ei->bei_e;
			} else {
				e = NULL;
			}

707
			if ( e == NULL ) {
708
				if( !BDB_IDL_IS_RANGE(candidates) ) {
709
710
					/* only complain for non-range IDLs */
					Debug( LDAP_DEBUG_TRACE,
711
712
						LDAP_XSTRING(bdb_search)
						": candidate %ld not found\n",
713
714
						(long) id, 0, 0 );
				}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
715

716
717
				goto loop_continue;
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
718

719
		rs->sr_entry = e;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
720

721
722
723
		if ( is_entry_subentry( e ) ) {
			if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
				if(!get_subentries_visibility( op )) {
Kurt Zeilenga's avatar
Kurt Zeilenga committed
724
725
726
					/* only subentries are visible */
					goto loop_continue;
				}
727

728
729
730
			} else if ( get_subentries( op ) &&
				!get_subentries_visibility( op ))
			{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
731
732
733
				/* only subentries are visible */
				goto loop_continue;
			}
734
735
736
737

		} else if ( get_subentries_visibility( op )) {
			/* only subentries are visible */
			goto loop_continue;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
738
739
		}

740
741
742
743
744
745
746
747
748
		/* 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.
		 */
749
		scopeok = 0;
Howard Chu's avatar
Howard Chu committed
750
		switch( op->ors_scope ) {
751
752
		case LDAP_SCOPE_BASE:
			/* This is always true, yes? */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
753
			if ( id == base.e_id ) scopeok = 1;
754
			break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
755

756
		case LDAP_SCOPE_ONELEVEL:
Kurt Zeilenga's avatar
Kurt Zeilenga committed
757
			if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
758
			break;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
759

760
#ifdef LDAP_SCOPE_CHILDREN
761
762
763
		case LDAP_SCOPE_CHILDREN:
			if ( id == base.e_id ) break;
			/* Fall-thru */
764
#endif
Kurt Zeilenga's avatar
Kurt Zeilenga committed
765
		case LDAP_SCOPE_SUBTREE: {
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
766
			EntryInfo *tmp;
Pierangelo Masarati's avatar
Pierangelo Masarati committed
767
			for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
768
769
770
771
				if ( tmp->bei_id == base.e_id ) {
					scopeok = 1;
					break;
				}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
772
773
			}
			} break;
774
775
		}

776
		/* aliases were already dereferenced in candidate list */
Howard Chu's avatar
Howard Chu committed
777
		if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
Howard Chu's avatar
Howard Chu committed
778
779
780
			/* 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
781
			if ( is_entry_alias(e) &&
Howard Chu's avatar
Howard Chu committed
782
				((op->ors_deref & LDAP_DEREF_FINDING) ||
Kurt Zeilenga's avatar
Kurt Zeilenga committed
783
					!bvmatch(&e->e_nname, &op->o_req_ndn)))
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
784
			{
Howard Chu's avatar
Howard Chu committed
785
				goto loop_continue;
Kurt Zeilenga's avatar
Cleanup    
Kurt Zeilenga committed
786
			}
Howard Chu's avatar
Howard Chu committed
787
788

			/* scopes is only non-empty for onelevel or subtree */
789
			if ( !scopeok && BDB_IDL_N(scopes) ) {
Howard Chu's avatar
Howard Chu committed
790
				unsigned x;
Howard Chu's avatar
Howard Chu committed
791
				if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
792
793
					x = bdb_idl_search( scopes, e->e_id );
					if ( scopes[x] == e->e_id ) scopeok = 1;
Howard Chu's avatar
Howard Chu committed
794
				} else {
795
					/* subtree, walk up the tree */
Howard Chu's avatar
Howard Chu committed
796
					EntryInfo *tmp = BEI(e);
Kurt Zeilenga's avatar
Kurt Zeilenga committed
797
798
					for (;tmp->bei_parent; tmp=tmp->bei_parent) {
						x = bdb_idl_search( scopes, tmp->bei_id );
Howard Chu's avatar
Howard Chu committed
799
800
801
802
803
804
805
						if ( scopes[x] == tmp->bei_id ) {
							scopeok = 1;
							break;
						}
					}
				}
			}
Kurt Zeilenga's avatar
Kurt Zeilenga committed
806
		}
807
808

		/* Not in scope, ignore it */
809
810
		if ( !scopeok )
		{
811
			Debug( LDAP_DEBUG_TRACE,
812
813
				LDAP_XSTRING(bdb_search)
				": %ld scope not okay\n",
814
815
816
817
				(long) id, 0, 0 );
			goto loop_continue;
		}

Kurt Zeilenga's avatar
Kurt Zeilenga committed
818
819
820
821
822
		/*
		 * 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
823
		if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
824
			&& is_entry_referral( e ) )
Kurt Zeilenga's avatar
Kurt Zeilenga committed
825
		{
Howard Chu's avatar
Howard Chu committed
826
			BerVarray erefs = get_entry_referrals( op, e );
827
			rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
Howard Chu's avatar
Howard Chu committed
828
				op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
829
					? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
830

Howard Chu's avatar
Howard Chu committed
831
			send_search_reference( op, rs );
Kurt Zeilenga's avatar
Kurt Zeilenga committed
832

833
834
835
			ber_bvarray_free( rs->sr_ref );
			ber_bvarray_free( erefs );
			rs->sr_ref = NULL;
Kurt Zeilenga's avatar
Kurt Zeilenga committed
836
837
838
839

			goto loop_continue;
		}

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
840
		if ( !manageDSAit && is_entry_glue( e )) {
841
842
843
			goto loop_continue;
		}

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

847
		if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
848
			/* check size limit */
Howard Chu's avatar
Howard Chu committed
849
850
			if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
				if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
851
852
853
#ifdef SLAP_ZONE_ALLOC
					slap_zn_runlock(bdb->bi_cache.c_zctx, e);
#endif