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>
16 static int base_candidate(
20 static int search_candidates(
29 static ID idl_first( ID *ids, ID *cursor );
30 static ID idl_next( ID *ids, ID *cursor );
44 const char *filterstr,
48 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
51 const char *text = NULL;
54 ID candidates[BDB_IDL_SIZE];
56 struct berval **v2refs = NULL;
57 Entry *matched = NULL;
58 char *realbase = NULL;
62 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
65 manageDSAit = get_manageDSAit( op );
68 /* get entry with reader lock */
69 if ( deref & LDAP_DEREF_FINDING ) {
70 e = deref_dn_r( be, nbase, &err, &matched, &text );
75 rc = bdb_dn2entry( be, NULL, nbase, &e, &matched, 0 );
83 send_ldap_result( conn, op, rc=LDAP_OTHER,
84 NULL, "internal error", NULL, NULL );
89 char *matched_dn = NULL;
90 struct berval **refs = NULL;
92 if ( matched != NULL ) {
93 matched_dn = ch_strdup( matched->e_dn );
95 refs = is_entry_referral( matched )
96 ? get_entry_referrals( be, conn, op, matched )
100 refs = default_referral;
103 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
104 matched_dn, text, refs, NULL );
106 if( matched != NULL ) {
107 ber_bvecfree( refs );
109 bdb_entry_return( be, matched );
116 if (!manageDSAit && is_entry_referral( e ) ) {
117 /* entry is a referral, don't allow add */
118 char *matched_dn = ch_strdup( e->e_dn );
119 struct berval **refs = get_entry_referrals( be,
122 bdb_entry_return( be, e );
125 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
128 send_ldap_result( conn, op, LDAP_REFERRAL,
129 matched_dn, NULL, refs, NULL );
131 ber_bvecfree( refs );
137 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
138 tlimit = -1; /* allow root to set no limit */
140 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
141 be->be_timelimit : tlimit;
142 stoptime = op->o_time + tlimit;
145 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
146 slimit = -1; /* allow root to set no limit */
148 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
149 be->be_sizelimit : slimit;
152 if ( scope == LDAP_SCOPE_BASE ) {
153 rc = base_candidate( be, e, candidates );
156 rc = search_candidates( be, e, filter,
157 scope, deref, manageDSAit, candidates );
160 /* need normalized dn below */
161 realbase = ch_strdup( e->e_ndn );
163 /* start cursor at base entry's id */
166 bdb_entry_return( be, e );
169 if ( candidates[0] == 0 ) {
170 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
173 send_search_result( conn, op,
175 NULL, NULL, NULL, NULL, 0 );
181 for ( id = idl_first( candidates, &cursor );
183 id = idl_next( candidates, &cursor ) )
187 /* check for abandon */
188 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
189 abandon = op->o_abandon;
190 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
197 /* check time limit */
198 if ( tlimit != -1 && slap_get_time() > stoptime ) {
199 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
200 NULL, NULL, v2refs, NULL, nentries );
204 /* get the entry with reader lock */
205 rc = bdb_id2entry( be, NULL, id, &e );
208 if( !BDB_IDL_IS_RANGE(candidates) ) {
209 /* only complain for non-range IDLs */
210 Debug( LDAP_DEBUG_TRACE,
211 "bdb_search: candidate %ld not found\n",
219 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
224 e = deref_entry_r( be, e, &err, &matched, &text );
231 if( e->e_id == id ) {
236 /* need to skip alias which deref into scope */
237 if( scope & LDAP_SCOPE_ONELEVEL ) {
238 char *pdn = dn_parent( NULL, e->e_ndn );
240 if( strcmp( pdn, realbase ) ) {
247 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
248 /* alias is within scope */
249 Debug( LDAP_DEBUG_TRACE,
250 "bdb_search: \"%s\" in subtree\n",
260 * if it's a referral, add it to the list of referrals. only do
261 * this for non-base searches, and don't check the filter
262 * explicitly here since it's only a candidate anyway.
264 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
265 is_entry_referral( e ) )
267 struct berval **refs = get_entry_referrals(
270 send_search_reference( be, conn, op,
271 e, refs, scope, NULL, &v2refs );
273 ber_bvecfree( refs );
278 /* if it matches the filter and scope, send it */
279 rc = test_filter( be, conn, op, e, filter );
280 if ( rc == LDAP_COMPARE_TRUE ) {
284 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
285 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
286 (void) dn_normalize( dn );
287 scopeok = (dn == realbase)
289 : (strcmp( dn, realbase ) ? 0 : 1 );
293 scopeok = (realbase == NULL || *realbase == '\0');
296 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
297 dn = ch_strdup( e->e_ndn );
298 scopeok = dn_issuffix( dn, realbase );
306 /* check size limit */
307 if ( --slimit == -1 ) {
308 bdb_entry_return( be, e );
310 send_search_result( conn, op,
311 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
312 v2refs, NULL, nentries );
317 int result = send_search_entry( be, conn, op,
318 e, attrs, attrsonly, NULL);
321 case 0: /* entry sent ok */
324 case 1: /* entry not sent */
326 case -1: /* connection closed */
327 bdb_entry_return( be, e );
334 Debug( LDAP_DEBUG_TRACE,
335 "bdb_search: %ld scope not okay\n",
339 Debug( LDAP_DEBUG_TRACE,
340 "bdb_search: %ld does match filter\n",
346 /* free reader lock */
347 bdb_entry_return( be, e );
350 ldap_pvt_thread_yield();
352 send_search_result( conn, op,
353 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
354 NULL, NULL, v2refs, NULL, nentries );
359 ber_bvecfree( v2refs );
360 if( realbase ) ch_free( realbase );
366 static int base_candidate(
371 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
372 e->e_dn, (long) e->e_id, 0);
379 static int search_candidates(
389 Filter f, fand, rf, af, xf;
390 AttributeAssertion aa_ref, aa_alias;
392 Debug(LDAP_DEBUG_TRACE,
393 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
394 e->e_dn, (long) e->e_id, scope );
397 xf.f_choice = LDAP_FILTER_OR;
401 /* match referrals */
402 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
403 rf.f_choice = LDAP_FILTER_EQUALITY;
405 rf.f_av_desc = slap_schema.si_ad_objectClass;
406 rf.f_av_value = &bv_ref;
412 if( deref & LDAP_DEREF_SEARCHING ) {
414 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
415 af.f_choice = LDAP_FILTER_EQUALITY;
416 af.f_ava = &aa_alias;
417 af.f_av_desc = slap_schema.si_ad_objectClass;
418 af.f_av_value = &bv_alias;
425 f.f_choice = LDAP_FILTER_AND;
427 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
428 ? SLAPD_FILTER_DN_SUBTREE
429 : SLAPD_FILTER_DN_ONE;
430 fand.f_dn = e->e_ndn;
431 fand.f_next = xf.f_or == filter ? filter : &xf ;
434 rc = bdb_filter_candidates( be, &f, ids );
439 ids[2] = e->e_id+128;
443 Debug(LDAP_DEBUG_TRACE,
444 "search_candidates: id=%ld first=%ld last=%ld\n",
446 BDB_IDL_IS_RANGE( ids ) ? ids[2] : ids[ids[0]] );
451 static ID idl_first( ID *ids, ID *cursor )
460 if ( BDB_IDL_IS_RANGE( ids ) ) {
461 if( *cursor < ids[1] ) {
467 pos = bdb_idl_search( ids, *cursor );
477 static ID idl_next( ID *ids, ID *cursor )
479 if ( BDB_IDL_IS_RANGE( ids ) ) {
480 if( ids[2] < ++(*cursor) ) {
486 if ( *cursor < ids[0] ) {
487 return ids[(*cursor)++];