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 */
176 /* if no limit is required, use soft limit */
178 tlimit = limit->lms_t_soft;
180 /* if requested limit higher than hard limit, abort */
181 } else if ( tlimit > limit->lms_t_hard ) {
182 /* no hard limit means use soft instead */
183 if ( limit->lms_t_hard == 0 ) {
184 tlimit = limit->lms_t_soft;
186 /* positive hard limit means abort */
187 } else if ( limit->lms_t_hard > 0 ) {
188 send_search_result( conn, op,
189 LDAP_UNWILLING_TO_PERFORM,
190 NULL, NULL, NULL, NULL, 0 );
195 /* negative hard limit means no limit */
198 /* if no limit is required, use soft limit */
200 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 */
221 /* compute it anyway; root does not use it */
222 stoptime = op->o_time + tlimit;
224 /* select candidates */
225 if ( scope == LDAP_SCOPE_BASE ) {
226 rc = base_candidate( be, e, candidates );
229 rc = search_candidates( be, e, filter,
230 scope, deref, manageDSAit, candidates );
233 /* need normalized dn below */
234 realbase = ch_strdup( e->e_ndn );
236 /* start cursor at base entry's id */
239 bdb_entry_return( be, e );
242 if ( candidates[0] == 0 ) {
243 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
246 send_search_result( conn, op,
248 NULL, NULL, NULL, NULL, 0 );
254 /* if not root and candidates exceed to-be-checked entries, abort */
255 if ( !isroot && limit->lms_s_unchecked != -1 ) {
256 if ( BDB_IDL_N(candidates) > limit->lms_s_unchecked ) {
257 send_search_result( conn, op,
258 LDAP_UNWILLING_TO_PERFORM,
259 NULL, NULL, NULL, NULL, 0 );
265 for ( id = bdb_idl_first( candidates, &cursor );
267 id = bdb_idl_next( candidates, &cursor ) )
271 /* check for abandon */
272 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
273 abandon = op->o_abandon;
274 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
281 /* check time limit */
282 if ( tlimit != -1 && slap_get_time() > stoptime ) {
283 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
284 NULL, NULL, v2refs, NULL, nentries );
288 /* get the entry with reader lock */
289 rc = bdb_id2entry( be, NULL, id, &e );
292 if( !BDB_IDL_IS_RANGE(candidates) ) {
293 /* only complain for non-range IDLs */
294 Debug( LDAP_DEBUG_TRACE,
295 "bdb_search: candidate %ld not found\n",
303 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
308 e = deref_entry_r( be, e, &err, &matched, &text );
315 if( e->e_id == id ) {
320 /* need to skip alias which deref into scope */
321 if( scope & LDAP_SCOPE_ONELEVEL ) {
322 char *pdn = dn_parent( NULL, e->e_ndn );
324 if( strcmp( pdn, realbase ) ) {
331 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
332 /* alias is within scope */
333 Debug( LDAP_DEBUG_TRACE,
334 "bdb_search: \"%s\" in subtree\n",
344 * if it's a referral, add it to the list of referrals. only do
345 * this for non-base searches, and don't check the filter
346 * explicitly here since it's only a candidate anyway.
348 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
349 is_entry_referral( e ) )
351 struct berval **refs = get_entry_referrals(
352 be, conn, op, e, NULL, scope );
354 send_search_reference( be, conn, op,
355 e, refs, NULL, &v2refs );
357 ber_bvecfree( refs );
362 /* if it matches the filter and scope, send it */
363 rc = test_filter( be, conn, op, e, filter );
364 if ( rc == LDAP_COMPARE_TRUE ) {
368 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
369 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
370 (void) dn_normalize( dn );
371 scopeok = (dn == realbase)
373 : (strcmp( dn, realbase ) ? 0 : 1 );
377 scopeok = (realbase == NULL || *realbase == '\0');
380 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
381 dn = ch_strdup( e->e_ndn );
382 scopeok = dn_issuffix( dn, realbase );
390 /* check size limit */
391 if ( --slimit == -1 ) {
392 bdb_entry_return( be, e );
394 send_search_result( conn, op,
395 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
396 v2refs, NULL, nentries );
401 int result = send_search_entry( be, conn, op,
402 e, attrs, attrsonly, NULL);
405 case 0: /* entry sent ok */
408 case 1: /* entry not sent */
410 case -1: /* connection closed */
411 bdb_entry_return( be, e );
418 Debug( LDAP_DEBUG_TRACE,
419 "bdb_search: %ld scope not okay\n",
423 Debug( LDAP_DEBUG_TRACE,
424 "bdb_search: %ld does match filter\n",
430 /* free reader lock */
431 bdb_entry_return( be, e );
434 ldap_pvt_thread_yield();
436 send_search_result( conn, op,
437 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
438 NULL, NULL, v2refs, NULL, nentries );
443 ber_bvecfree( v2refs );
444 if( realbase ) ch_free( realbase );
450 static int base_candidate(
455 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
456 e->e_dn, (long) e->e_id, 0);
463 static int search_candidates(
473 Filter f, fand, rf, xf;
474 AttributeAssertion aa_ref;
475 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
478 AttributeAssertion aa_alias;
481 Debug(LDAP_DEBUG_TRACE,
482 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
483 e->e_dn, (long) e->e_id, scope );
486 xf.f_choice = LDAP_FILTER_OR;
490 /* match referrals */
491 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
492 rf.f_choice = LDAP_FILTER_EQUALITY;
494 rf.f_av_desc = slap_schema.si_ad_objectClass;
495 rf.f_av_value = &bv_ref;
501 if( deref & LDAP_DEREF_SEARCHING ) {
503 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
504 af.f_choice = LDAP_FILTER_EQUALITY;
505 af.f_ava = &aa_alias;
506 af.f_av_desc = slap_schema.si_ad_objectClass;
507 af.f_av_value = &bv_alias;
514 f.f_choice = LDAP_FILTER_AND;
516 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
517 ? SLAPD_FILTER_DN_SUBTREE
518 : SLAPD_FILTER_DN_ONE;
519 fand.f_dn = e->e_ndn;
520 fand.f_next = xf.f_or == filter ? filter : &xf ;
523 #ifdef BDB_FILTER_INDICES
524 rc = bdb_filter_candidates( be, &f, ids );
526 BDB_IDL_ID( bdb, ids, e->e_id );
530 Debug(LDAP_DEBUG_TRACE,
531 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
533 (long) BDB_IDL_FIRST(ids),
534 (long) BDB_IDL_LAST(ids) );