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 struct slap_limits_set *limit = NULL;
63 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
66 manageDSAit = get_manageDSAit( op );
69 /* get entry with reader lock */
70 if ( deref & LDAP_DEREF_FINDING ) {
71 e = deref_dn_r( be, nbase, &err, &matched, &text );
76 rc = bdb_dn2entry( be, NULL, nbase, &e, &matched, 0 );
84 send_ldap_result( conn, op, rc=LDAP_OTHER,
85 NULL, "internal error", NULL, NULL );
90 char *matched_dn = NULL;
91 struct berval **refs = NULL;
93 if ( matched != NULL ) {
94 matched_dn = ch_strdup( matched->e_dn );
96 refs = is_entry_referral( matched )
97 ? get_entry_referrals( be, conn, op, matched )
101 refs = default_referral;
104 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
105 matched_dn, text, refs, NULL );
107 if( matched != NULL ) {
108 ber_bvecfree( refs );
110 bdb_entry_return( be, matched );
117 if (!manageDSAit && is_entry_referral( e ) ) {
118 /* entry is a referral, don't allow add */
119 char *matched_dn = ch_strdup( e->e_dn );
120 struct berval **refs = get_entry_referrals( be,
123 bdb_entry_return( be, e );
126 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
129 send_ldap_result( conn, op, LDAP_REFERRAL,
130 matched_dn, NULL, refs, NULL );
132 ber_bvecfree( refs );
138 /* if not root, get appropriate limits */
139 if ( be_isroot( be, op->o_ndn ) ) {
142 ( void ) get_limits( be, op->o_ndn, &limit );
145 /* The time/size limits come first because they require very little
146 * effort, so there's no chance the candidates are selected and then
147 * the request is not honored only because of time/size constraints */
149 /* if no time limit requested, use soft limit (unless root!) */
152 tlimit = -1; /* allow root to set no limit */
154 tlimit = limit->lms_t_soft;
157 /* if requested limit higher than hard limit, abort */
158 } else if ( tlimit > limit->lms_t_hard ) {
159 /* no hard limit means use soft instead */
160 if ( limit->lms_t_hard == 0 ) {
161 tlimit = limit->lms_t_soft;
163 /* positive hard limit means abort */
164 } else if ( limit->lms_t_hard > 0 ) {
165 send_search_result( conn, op,
166 LDAP_UNWILLING_TO_PERFORM,
167 NULL, NULL, NULL, NULL, 0 );
172 /* negative hard limit means no limit */
175 /* compute it anyway; root does not use it */
176 stoptime = op->o_time + tlimit;
178 /* if no size limit requested, use soft limit (unless root!) */
181 slimit = -1; /* allow root to set no limit */
183 slimit = limit->lms_s_soft;
186 /* if requested limit higher than hard limit, abort */
187 } else if ( slimit > limit->lms_s_hard ) {
188 /* no hard limit means use soft instead */
189 if ( limit->lms_s_hard == 0 ) {
190 slimit = limit->lms_s_soft;
192 /* positive hard limit means abort */
193 } else if ( limit->lms_s_hard > 0 ) {
194 send_search_result( conn, op,
195 LDAP_UNWILLING_TO_PERFORM,
196 NULL, NULL, NULL, NULL, 0 );
201 /* negative hard limit means no limit */
204 /* select candidates */
205 if ( scope == LDAP_SCOPE_BASE ) {
206 rc = base_candidate( be, e, candidates );
209 rc = search_candidates( be, e, filter,
210 scope, deref, manageDSAit, candidates );
213 /* need normalized dn below */
214 realbase = ch_strdup( e->e_ndn );
216 /* start cursor at base entry's id */
219 bdb_entry_return( be, e );
222 if ( candidates[0] == 0 ) {
223 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
226 send_search_result( conn, op,
228 NULL, NULL, NULL, NULL, 0 );
234 /* if not root and candidates exceed to-be-checked entries, abort */
235 if ( !isroot && limit->lms_s_unchecked != -1 ) {
237 1 + (BDB_IDL_IS_RANGE( candidates ) ? candidates[ 2 ] :
238 candidates[ candidates[ 0 ] ])-candidates[ 1 ];
240 if ( n > limit->lms_s_unchecked ) {
241 send_search_result( conn, op,
242 LDAP_UNWILLING_TO_PERFORM,
243 NULL, NULL, NULL, NULL, 0 );
249 for ( id = bdb_idl_first( candidates, &cursor );
251 id = bdb_idl_next( candidates, &cursor ) )
255 /* check for abandon */
256 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
257 abandon = op->o_abandon;
258 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
265 /* check time limit */
266 if ( tlimit != -1 && slap_get_time() > stoptime ) {
267 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
268 NULL, NULL, v2refs, NULL, nentries );
272 /* get the entry with reader lock */
273 rc = bdb_id2entry( be, NULL, id, &e );
276 if( !BDB_IDL_IS_RANGE(candidates) ) {
277 /* only complain for non-range IDLs */
278 Debug( LDAP_DEBUG_TRACE,
279 "bdb_search: candidate %ld not found\n",
287 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
292 e = deref_entry_r( be, e, &err, &matched, &text );
299 if( e->e_id == id ) {
304 /* need to skip alias which deref into scope */
305 if( scope & LDAP_SCOPE_ONELEVEL ) {
306 char *pdn = dn_parent( NULL, e->e_ndn );
308 if( strcmp( pdn, realbase ) ) {
315 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
316 /* alias is within scope */
317 Debug( LDAP_DEBUG_TRACE,
318 "bdb_search: \"%s\" in subtree\n",
328 * if it's a referral, add it to the list of referrals. only do
329 * this for non-base searches, and don't check the filter
330 * explicitly here since it's only a candidate anyway.
332 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
333 is_entry_referral( e ) )
335 struct berval **refs = get_entry_referrals(
338 send_search_reference( be, conn, op,
339 e, refs, scope, NULL, &v2refs );
341 ber_bvecfree( refs );
346 /* if it matches the filter and scope, send it */
347 rc = test_filter( be, conn, op, e, filter );
348 if ( rc == LDAP_COMPARE_TRUE ) {
352 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
353 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
354 (void) dn_normalize( dn );
355 scopeok = (dn == realbase)
357 : (strcmp( dn, realbase ) ? 0 : 1 );
361 scopeok = (realbase == NULL || *realbase == '\0');
364 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
365 dn = ch_strdup( e->e_ndn );
366 scopeok = dn_issuffix( dn, realbase );
374 /* check size limit */
375 if ( --slimit == -1 ) {
376 bdb_entry_return( be, e );
378 send_search_result( conn, op,
379 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
380 v2refs, NULL, nentries );
385 int result = send_search_entry( be, conn, op,
386 e, attrs, attrsonly, NULL);
389 case 0: /* entry sent ok */
392 case 1: /* entry not sent */
394 case -1: /* connection closed */
395 bdb_entry_return( be, e );
402 Debug( LDAP_DEBUG_TRACE,
403 "bdb_search: %ld scope not okay\n",
407 Debug( LDAP_DEBUG_TRACE,
408 "bdb_search: %ld does match filter\n",
414 /* free reader lock */
415 bdb_entry_return( be, e );
418 ldap_pvt_thread_yield();
420 send_search_result( conn, op,
421 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
422 NULL, NULL, v2refs, NULL, nentries );
427 ber_bvecfree( v2refs );
428 if( realbase ) ch_free( realbase );
434 static int base_candidate(
439 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
440 e->e_dn, (long) e->e_id, 0);
447 static int search_candidates(
457 Filter f, fand, rf, xf;
458 AttributeAssertion aa_ref;
459 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
462 AttributeAssertion aa_alias;
465 Debug(LDAP_DEBUG_TRACE,
466 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
467 e->e_dn, (long) e->e_id, scope );
470 xf.f_choice = LDAP_FILTER_OR;
474 /* match referrals */
475 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
476 rf.f_choice = LDAP_FILTER_EQUALITY;
478 rf.f_av_desc = slap_schema.si_ad_objectClass;
479 rf.f_av_value = &bv_ref;
485 if( deref & LDAP_DEREF_SEARCHING ) {
487 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
488 af.f_choice = LDAP_FILTER_EQUALITY;
489 af.f_ava = &aa_alias;
490 af.f_av_desc = slap_schema.si_ad_objectClass;
491 af.f_av_value = &bv_alias;
498 f.f_choice = LDAP_FILTER_AND;
500 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
501 ? SLAPD_FILTER_DN_SUBTREE
502 : SLAPD_FILTER_DN_ONE;
503 fand.f_dn = e->e_ndn;
504 fand.f_next = xf.f_or == filter ? filter : &xf ;
507 #ifdef BDB_FILTER_INDICES
510 BDB_IDL_ID( bdb, range, e->e_id );
511 rc = bdb_filter_candidates( be, range, &f, ids );
514 BDB_IDL_ID( bdb, ids, e->e_id );
518 Debug(LDAP_DEBUG_TRACE,
519 "search_candidates: id=%ld first=%ld last=%ld\n",
521 BDB_IDL_IS_RANGE( ids ) ? ids[2] : ids[ids[0]] );