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_UM_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 struct berval **erefs;
95 matched_dn = ch_strdup( matched->e_dn );
97 erefs = is_entry_referral( matched )
98 ? get_entry_referrals( be, conn, op, matched,
102 bdb_entry_return( be, matched );
106 refs = referral_rewrite( erefs, matched_dn,
108 ber_bvecfree( erefs );
112 refs = referral_rewrite( default_referral,
116 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
117 matched_dn, text, refs, NULL );
119 ber_bvecfree( refs );
125 if (!manageDSAit && is_entry_referral( e ) ) {
126 /* entry is a referral, don't allow add */
127 char *matched_dn = ch_strdup( e->e_dn );
128 struct berval **erefs = get_entry_referrals( be,
129 conn, op, e, base, scope );
130 struct berval **refs = NULL;
132 bdb_entry_return( be, e );
136 refs = referral_rewrite( erefs, matched_dn,
138 ber_bvecfree( erefs );
141 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
144 send_ldap_result( conn, op, LDAP_REFERRAL,
145 matched_dn, refs ? NULL : "bad referral object",
148 ber_bvecfree( refs );
154 /* if not root, get appropriate limits */
155 if ( be_isroot( be, op->o_ndn ) ) {
158 ( void ) get_limits( be, op->o_ndn, &limit );
161 /* The time/size limits come first because they require very little
162 * effort, so there's no chance the candidates are selected and then
163 * the request is not honored only because of time/size constraints */
165 /* if no time limit requested, use soft limit (unless root!) */
168 tlimit = -1; /* allow root to set no limit */
170 tlimit = limit->lms_t_soft;
173 /* if requested limit higher than hard limit, abort */
174 } else if ( tlimit > limit->lms_t_hard ) {
175 /* no hard limit means use soft instead */
176 if ( limit->lms_t_hard == 0 ) {
177 tlimit = limit->lms_t_soft;
179 /* positive hard limit means abort */
180 } else if ( limit->lms_t_hard > 0 ) {
181 send_search_result( conn, op,
182 LDAP_UNWILLING_TO_PERFORM,
183 NULL, NULL, NULL, NULL, 0 );
188 /* negative hard limit means no limit */
191 /* compute it anyway; root does not use it */
192 stoptime = op->o_time + tlimit;
194 /* if no size limit requested, use soft limit (unless root!) */
197 slimit = -1; /* allow root to set no limit */
199 slimit = limit->lms_s_soft;
202 /* if requested limit higher than hard limit, abort */
203 } else if ( slimit > limit->lms_s_hard ) {
204 /* no hard limit means use soft instead */
205 if ( limit->lms_s_hard == 0 ) {
206 slimit = limit->lms_s_soft;
208 /* positive hard limit means abort */
209 } else if ( limit->lms_s_hard > 0 ) {
210 send_search_result( conn, op,
211 LDAP_UNWILLING_TO_PERFORM,
212 NULL, NULL, NULL, NULL, 0 );
217 /* negative hard limit means no limit */
220 /* select candidates */
221 if ( scope == LDAP_SCOPE_BASE ) {
222 rc = base_candidate( be, e, candidates );
225 rc = search_candidates( be, e, filter,
226 scope, deref, manageDSAit, candidates );
229 /* need normalized dn below */
230 realbase = ch_strdup( e->e_ndn );
232 /* start cursor at base entry's id */
235 bdb_entry_return( be, e );
238 if ( candidates[0] == 0 ) {
239 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
242 send_search_result( conn, op,
244 NULL, NULL, NULL, NULL, 0 );
250 /* if not root and candidates exceed to-be-checked entries, abort */
251 if ( !isroot && limit->lms_s_unchecked != -1 ) {
252 if ( BDB_IDL_N(candidates) > limit->lms_s_unchecked ) {
253 send_search_result( conn, op,
254 LDAP_UNWILLING_TO_PERFORM,
255 NULL, NULL, NULL, NULL, 0 );
261 for ( id = bdb_idl_first( candidates, &cursor );
263 id = bdb_idl_next( candidates, &cursor ) )
267 /* check for abandon */
268 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
269 abandon = op->o_abandon;
270 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
277 /* check time limit */
278 if ( tlimit != -1 && slap_get_time() > stoptime ) {
279 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
280 NULL, NULL, v2refs, NULL, nentries );
284 /* get the entry with reader lock */
285 rc = bdb_id2entry( be, NULL, id, &e );
288 if( !BDB_IDL_IS_RANGE(candidates) ) {
289 /* only complain for non-range IDLs */
290 Debug( LDAP_DEBUG_TRACE,
291 "bdb_search: candidate %ld not found\n",
299 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
304 e = deref_entry_r( be, e, &err, &matched, &text );
311 if( e->e_id == id ) {
316 /* need to skip alias which deref into scope */
317 if( scope & LDAP_SCOPE_ONELEVEL ) {
318 char *pdn = dn_parent( NULL, e->e_ndn );
320 if( strcmp( pdn, realbase ) ) {
327 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
328 /* alias is within scope */
329 Debug( LDAP_DEBUG_TRACE,
330 "bdb_search: \"%s\" in subtree\n",
340 * if it's a referral, add it to the list of referrals. only do
341 * this for non-base searches, and don't check the filter
342 * explicitly here since it's only a candidate anyway.
344 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
345 is_entry_referral( e ) )
347 struct berval **refs = get_entry_referrals(
348 be, conn, op, e, NULL, scope );
350 send_search_reference( be, conn, op,
351 e, refs, NULL, &v2refs );
353 ber_bvecfree( refs );
358 /* if it matches the filter and scope, send it */
359 rc = test_filter( be, conn, op, e, filter );
360 if ( rc == LDAP_COMPARE_TRUE ) {
364 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
365 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
366 (void) dn_normalize( dn );
367 scopeok = (dn == realbase)
369 : (strcmp( dn, realbase ) ? 0 : 1 );
373 scopeok = (realbase == NULL || *realbase == '\0');
376 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
377 dn = ch_strdup( e->e_ndn );
378 scopeok = dn_issuffix( dn, realbase );
386 /* check size limit */
387 if ( --slimit == -1 ) {
388 bdb_entry_return( be, e );
390 send_search_result( conn, op,
391 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
392 v2refs, NULL, nentries );
397 int result = send_search_entry( be, conn, op,
398 e, attrs, attrsonly, NULL);
401 case 0: /* entry sent ok */
404 case 1: /* entry not sent */
406 case -1: /* connection closed */
407 bdb_entry_return( be, e );
414 Debug( LDAP_DEBUG_TRACE,
415 "bdb_search: %ld scope not okay\n",
419 Debug( LDAP_DEBUG_TRACE,
420 "bdb_search: %ld does match filter\n",
426 /* free reader lock */
427 bdb_entry_return( be, e );
430 ldap_pvt_thread_yield();
432 send_search_result( conn, op,
433 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
434 NULL, NULL, v2refs, NULL, nentries );
439 ber_bvecfree( v2refs );
440 if( realbase ) ch_free( realbase );
446 static int base_candidate(
451 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
452 e->e_dn, (long) e->e_id, 0);
459 static int search_candidates(
469 Filter f, fand, rf, xf;
470 AttributeAssertion aa_ref;
471 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
474 AttributeAssertion aa_alias;
477 Debug(LDAP_DEBUG_TRACE,
478 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
479 e->e_dn, (long) e->e_id, scope );
482 xf.f_choice = LDAP_FILTER_OR;
486 /* match referrals */
487 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
488 rf.f_choice = LDAP_FILTER_EQUALITY;
490 rf.f_av_desc = slap_schema.si_ad_objectClass;
491 rf.f_av_value = &bv_ref;
497 if( deref & LDAP_DEREF_SEARCHING ) {
499 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
500 af.f_choice = LDAP_FILTER_EQUALITY;
501 af.f_ava = &aa_alias;
502 af.f_av_desc = slap_schema.si_ad_objectClass;
503 af.f_av_value = &bv_alias;
510 f.f_choice = LDAP_FILTER_AND;
512 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
513 ? SLAPD_FILTER_DN_SUBTREE
514 : SLAPD_FILTER_DN_ONE;
515 fand.f_dn = e->e_ndn;
516 fand.f_next = xf.f_or == filter ? filter : &xf ;
519 #ifdef BDB_FILTER_INDICES
520 rc = bdb_filter_candidates( be, &f, ids );
522 BDB_IDL_ID( bdb, ids, e->e_id );
526 Debug(LDAP_DEBUG_TRACE,
527 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
529 (long) BDB_IDL_FIRST(ids),
530 (long) BDB_IDL_LAST(ids) );