1 /* search.c - search operation */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
17 static int base_candidate(
21 static int search_candidates(
42 const char *filterstr,
46 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
49 const char *text = NULL;
52 ID candidates[BDB_IDL_SIZE];
54 struct berval **v2refs = NULL;
55 Entry *matched = NULL;
56 char *realbase = NULL;
60 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
63 manageDSAit = get_manageDSAit( op );
66 /* get entry with reader lock */
67 if ( deref & LDAP_DEREF_FINDING ) {
68 e = deref_dn_r( be, nbase, &err, &matched, &text );
73 rc = bdb_dn2entry( be, NULL, nbase, &e, &matched, 0 );
81 send_ldap_result( conn, op, rc=LDAP_OTHER,
82 NULL, "internal error", NULL, NULL );
87 char *matched_dn = NULL;
88 struct berval **refs = NULL;
90 if ( matched != NULL ) {
91 matched_dn = ch_strdup( matched->e_dn );
93 refs = is_entry_referral( matched )
94 ? get_entry_referrals( be, conn, op, matched )
98 refs = default_referral;
101 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
102 matched_dn, text, refs, NULL );
104 if( matched != NULL ) {
105 ber_bvecfree( refs );
107 bdb_entry_return( be, matched );
114 if (!manageDSAit && is_entry_referral( e ) ) {
115 /* entry is a referral, don't allow add */
116 char *matched_dn = ch_strdup( e->e_dn );
117 struct berval **refs = get_entry_referrals( be,
120 bdb_entry_return( be, e );
123 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
126 send_ldap_result( conn, op, LDAP_REFERRAL,
127 matched_dn, NULL, refs, NULL );
129 ber_bvecfree( refs );
135 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
136 tlimit = -1; /* allow root to set no limit */
138 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
139 be->be_timelimit : tlimit;
140 stoptime = op->o_time + tlimit;
143 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
144 slimit = -1; /* allow root to set no limit */
146 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
147 be->be_sizelimit : slimit;
150 if ( scope == LDAP_SCOPE_BASE ) {
151 rc = base_candidate( be, e, candidates );
154 rc = search_candidates( be, e, filter,
155 scope, deref, manageDSAit, candidates );
158 /* need normalized dn below */
159 realbase = ch_strdup( e->e_ndn );
161 /* start cursor at base entry's id */
164 bdb_entry_return( be, e );
167 if ( candidates[0] == 0 ) {
168 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
171 send_search_result( conn, op,
173 NULL, NULL, NULL, NULL, 0 );
179 for ( id = bdb_idl_first( candidates, &cursor );
181 id = bdb_idl_next( candidates, &cursor ) )
185 /* check for abandon */
186 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
187 abandon = op->o_abandon;
188 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
195 /* check time limit */
196 if ( tlimit != -1 && slap_get_time() > stoptime ) {
197 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
198 NULL, NULL, v2refs, NULL, nentries );
202 /* get the entry with reader lock */
203 rc = bdb_id2entry( be, NULL, id, &e );
206 if( !BDB_IDL_IS_RANGE(candidates) ) {
207 /* only complain for non-range IDLs */
208 Debug( LDAP_DEBUG_TRACE,
209 "bdb_search: candidate %ld not found\n",
217 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
222 e = deref_entry_r( be, e, &err, &matched, &text );
229 if( e->e_id == id ) {
234 /* need to skip alias which deref into scope */
235 if( scope & LDAP_SCOPE_ONELEVEL ) {
236 char *pdn = dn_parent( NULL, e->e_ndn );
238 if( strcmp( pdn, realbase ) ) {
245 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
246 /* alias is within scope */
247 Debug( LDAP_DEBUG_TRACE,
248 "bdb_search: \"%s\" in subtree\n",
258 * if it's a referral, add it to the list of referrals. only do
259 * this for non-base searches, and don't check the filter
260 * explicitly here since it's only a candidate anyway.
262 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
263 is_entry_referral( e ) )
265 struct berval **refs = get_entry_referrals(
268 send_search_reference( be, conn, op,
269 e, refs, scope, NULL, &v2refs );
271 ber_bvecfree( refs );
276 /* if it matches the filter and scope, send it */
277 rc = test_filter( be, conn, op, e, filter );
278 if ( rc == LDAP_COMPARE_TRUE ) {
282 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
283 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
284 (void) dn_normalize( dn );
285 scopeok = (dn == realbase)
287 : (strcmp( dn, realbase ) ? 0 : 1 );
291 scopeok = (realbase == NULL || *realbase == '\0');
294 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
295 dn = ch_strdup( e->e_ndn );
296 scopeok = dn_issuffix( dn, realbase );
304 /* check size limit */
305 if ( --slimit == -1 ) {
306 bdb_entry_return( be, e );
308 send_search_result( conn, op,
309 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
310 v2refs, NULL, nentries );
315 int result = send_search_entry( be, conn, op,
316 e, attrs, attrsonly, NULL);
319 case 0: /* entry sent ok */
322 case 1: /* entry not sent */
324 case -1: /* connection closed */
325 bdb_entry_return( be, e );
332 Debug( LDAP_DEBUG_TRACE,
333 "bdb_search: %ld scope not okay\n",
337 Debug( LDAP_DEBUG_TRACE,
338 "bdb_search: %ld does match filter\n",
344 /* free reader lock */
345 bdb_entry_return( be, e );
348 ldap_pvt_thread_yield();
350 send_search_result( conn, op,
351 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
352 NULL, NULL, v2refs, NULL, nentries );
357 ber_bvecfree( v2refs );
358 if( realbase ) ch_free( realbase );
364 static int base_candidate(
369 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
370 e->e_dn, (long) e->e_id, 0);
377 static int search_candidates(
387 Filter f, fand, rf, xf;
388 AttributeAssertion aa_ref;
389 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
392 AttributeAssertion aa_alias;
395 Debug(LDAP_DEBUG_TRACE,
396 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
397 e->e_dn, (long) e->e_id, scope );
400 xf.f_choice = LDAP_FILTER_OR;
404 /* match referrals */
405 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
406 rf.f_choice = LDAP_FILTER_EQUALITY;
408 rf.f_av_desc = slap_schema.si_ad_objectClass;
409 rf.f_av_value = &bv_ref;
415 if( deref & LDAP_DEREF_SEARCHING ) {
417 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
418 af.f_choice = LDAP_FILTER_EQUALITY;
419 af.f_ava = &aa_alias;
420 af.f_av_desc = slap_schema.si_ad_objectClass;
421 af.f_av_value = &bv_alias;
428 f.f_choice = LDAP_FILTER_AND;
430 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
431 ? SLAPD_FILTER_DN_SUBTREE
432 : SLAPD_FILTER_DN_ONE;
433 fand.f_dn = e->e_ndn;
434 fand.f_next = xf.f_or == filter ? filter : &xf ;
437 #ifdef BDB_FILTER_INDICES
440 BDB_IDL_ID( bdb, range, e->e_id );
441 rc = bdb_filter_candidates( be, range, &f, ids );
444 BDB_IDL_ID( bdb, ids, e->e_id );
448 Debug(LDAP_DEBUG_TRACE,
449 "search_candidates: id=%ld first=%ld last=%ld\n",
451 BDB_IDL_IS_RANGE( ids ) ? ids[2] : ids[ids[0]] );