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 const static Entry roote = { NOID, "", "", NULL, NULL };
55 struct berval **v2refs = NULL;
56 Entry *matched = NULL;
57 char *realbase = NULL;
61 struct slap_limits_set *limit = NULL;
64 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
67 manageDSAit = get_manageDSAit( op );
69 if ( *nbase == '\0' ) {
70 /* DIT root special case */
75 /* get entry with reader lock */
76 if ( deref & LDAP_DEREF_FINDING ) {
77 e = deref_dn_r( be, nbase, &err, &matched, &text );
82 rc = bdb_dn2entry( be, NULL, nbase, &e, &matched, 0 );
90 send_ldap_result( conn, op, rc=LDAP_OTHER,
91 NULL, "internal error", NULL, NULL );
96 char *matched_dn = NULL;
97 struct berval **refs = NULL;
99 if ( matched != NULL ) {
100 struct berval **erefs;
101 matched_dn = ch_strdup( matched->e_dn );
103 erefs = is_entry_referral( matched )
104 ? get_entry_referrals( be, conn, op, matched,
108 bdb_entry_return( be, matched );
112 refs = referral_rewrite( erefs, matched_dn,
114 ber_bvecfree( erefs );
118 refs = referral_rewrite( default_referral,
122 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
123 matched_dn, text, refs, NULL );
125 ber_bvecfree( refs );
131 if (!manageDSAit && e != &roote && is_entry_referral( e ) ) {
132 /* entry is a referral, don't allow add */
133 char *matched_dn = ch_strdup( e->e_dn );
134 struct berval **erefs = get_entry_referrals( be,
135 conn, op, e, base, scope );
136 struct berval **refs = NULL;
138 bdb_entry_return( be, e );
142 refs = referral_rewrite( erefs, matched_dn,
144 ber_bvecfree( erefs );
147 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
150 send_ldap_result( conn, op, LDAP_REFERRAL,
151 matched_dn, refs ? NULL : "bad referral object",
154 ber_bvecfree( refs );
160 /* if not root, get appropriate limits */
161 if ( be_isroot( be, op->o_ndn ) ) {
164 ( void ) get_limits( be, op->o_ndn, &limit );
167 /* The time/size limits come first because they require very little
168 * effort, so there's no chance the candidates are selected and then
169 * the request is not honored only because of time/size constraints */
171 /* if no time limit requested, use soft limit (unless root!) */
174 tlimit = -1; /* allow root to set no limit */
182 /* if no limit is required, use soft limit */
184 tlimit = limit->lms_t_soft;
186 /* if requested limit higher than hard limit, abort */
187 } else if ( tlimit > limit->lms_t_hard ) {
188 /* no hard limit means use soft instead */
189 if ( limit->lms_t_hard == 0 ) {
190 tlimit = limit->lms_t_soft;
192 /* positive hard limit means abort */
193 } else if ( limit->lms_t_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 /* if no limit is required, use soft limit */
206 slimit = limit->lms_s_soft;
208 /* if requested limit higher than hard limit, abort */
209 } else if ( slimit > limit->lms_s_hard ) {
210 /* no hard limit means use soft instead */
211 if ( limit->lms_s_hard == 0 ) {
212 slimit = limit->lms_s_soft;
214 /* positive hard limit means abort */
215 } else if ( limit->lms_s_hard > 0 ) {
216 send_search_result( conn, op,
217 LDAP_UNWILLING_TO_PERFORM,
218 NULL, NULL, NULL, NULL, 0 );
223 /* negative hard limit means no limit */
227 /* compute it anyway; root does not use it */
228 stoptime = op->o_time + tlimit;
230 /* select candidates */
231 if ( scope == LDAP_SCOPE_BASE ) {
232 rc = base_candidate( be, e, candidates );
235 rc = search_candidates( be, e, filter,
236 scope, deref, manageDSAit, candidates );
239 /* need normalized dn below */
240 realbase = ch_strdup( e->e_ndn );
242 /* start cursor at base entry's id
243 * FIXME: hack to make "" base work */
244 cursor = e->e_id == NOID ? 1 : e->e_id;
247 bdb_entry_return( be, e );
251 if ( candidates[0] == 0 ) {
252 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
255 send_search_result( conn, op,
257 NULL, NULL, NULL, NULL, 0 );
263 /* if not root and candidates exceed to-be-checked entries, abort */
264 if ( !isroot && limit->lms_s_unchecked != -1 ) {
265 if ( BDB_IDL_N(candidates) > limit->lms_s_unchecked ) {
266 send_search_result( conn, op,
267 LDAP_UNWILLING_TO_PERFORM,
268 NULL, NULL, NULL, NULL, 0 );
274 for ( id = bdb_idl_first( candidates, &cursor );
276 id = bdb_idl_next( candidates, &cursor ) )
280 /* check for abandon */
281 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
282 abandon = op->o_abandon;
283 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
290 /* check time limit */
291 if ( tlimit != -1 && slap_get_time() > stoptime ) {
292 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
293 NULL, NULL, v2refs, NULL, nentries );
297 /* get the entry with reader lock */
298 rc = bdb_id2entry( be, NULL, id, &e );
301 if( !BDB_IDL_IS_RANGE(candidates) ) {
302 /* only complain for non-range IDLs */
303 Debug( LDAP_DEBUG_TRACE,
304 "bdb_search: candidate %ld not found\n",
312 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
317 e = deref_entry_r( be, e, &err, &matched, &text );
324 if( e->e_id == id ) {
329 /* need to skip alias which deref into scope */
330 if( scope & LDAP_SCOPE_ONELEVEL ) {
331 char *pdn = dn_parent( NULL, e->e_ndn );
333 if( strcmp( pdn, realbase ) ) {
340 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
341 /* alias is within scope */
342 Debug( LDAP_DEBUG_TRACE,
343 "bdb_search: \"%s\" in subtree\n",
353 * if it's a referral, add it to the list of referrals. only do
354 * this for non-base searches, and don't check the filter
355 * explicitly here since it's only a candidate anyway.
357 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
358 is_entry_referral( e ) )
360 struct berval **refs = get_entry_referrals(
361 be, conn, op, e, NULL, scope );
363 send_search_reference( be, conn, op,
364 e, refs, NULL, &v2refs );
366 ber_bvecfree( refs );
371 /* if it matches the filter and scope, send it */
372 rc = test_filter( be, conn, op, e, filter );
373 if ( rc == LDAP_COMPARE_TRUE ) {
377 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
378 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
379 (void) dn_normalize( dn );
380 scopeok = (dn == realbase)
382 : (strcmp( dn, realbase ) ? 0 : 1 );
386 scopeok = (realbase == NULL || *realbase == '\0');
389 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
390 dn = ch_strdup( e->e_ndn );
391 scopeok = dn_issuffix( dn, realbase );
399 /* check size limit */
400 if ( --slimit == -1 ) {
401 bdb_entry_return( be, e );
403 send_search_result( conn, op,
404 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
405 v2refs, NULL, nentries );
410 int result = send_search_entry( be, conn, op,
411 e, attrs, attrsonly, NULL);
414 case 0: /* entry sent ok */
417 case 1: /* entry not sent */
419 case -1: /* connection closed */
420 bdb_entry_return( be, e );
427 Debug( LDAP_DEBUG_TRACE,
428 "bdb_search: %ld scope not okay\n",
432 Debug( LDAP_DEBUG_TRACE,
433 "bdb_search: %ld does match filter\n",
439 /* free reader lock */
440 bdb_entry_return( be, e );
443 ldap_pvt_thread_yield();
445 send_search_result( conn, op,
446 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
447 NULL, NULL, v2refs, NULL, nentries );
452 ber_bvecfree( v2refs );
453 if( realbase ) ch_free( realbase );
459 static int base_candidate(
464 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
465 e->e_dn, (long) e->e_id, 0);
472 static int search_candidates(
482 Filter f, fand, rf, xf;
483 AttributeAssertion aa_ref;
484 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
487 AttributeAssertion aa_alias;
490 Debug(LDAP_DEBUG_TRACE,
491 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
492 e->e_dn, (long) e->e_id, scope );
495 xf.f_choice = LDAP_FILTER_OR;
499 /* match referrals */
500 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
501 rf.f_choice = LDAP_FILTER_EQUALITY;
503 rf.f_av_desc = slap_schema.si_ad_objectClass;
504 rf.f_av_value = &bv_ref;
510 if( deref & LDAP_DEREF_SEARCHING ) {
512 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
513 af.f_choice = LDAP_FILTER_EQUALITY;
514 af.f_ava = &aa_alias;
515 af.f_av_desc = slap_schema.si_ad_objectClass;
516 af.f_av_value = &bv_alias;
523 f.f_choice = LDAP_FILTER_AND;
525 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
526 ? SLAPD_FILTER_DN_SUBTREE
527 : SLAPD_FILTER_DN_ONE;
528 fand.f_dn = e->e_ndn;
529 fand.f_next = xf.f_or == filter ? filter : &xf ;
532 #ifdef BDB_FILTER_INDICES
533 rc = bdb_filter_candidates( be, &f, ids );
535 /* FIXME: Original code:
536 BDB_IDL_ID( bdb, ids, e->e_id );
537 * this is a hack to make "" base work; when bdb_filter_candidates
538 * is used this should not be needed any more */
539 BDB_IDL_ID( bdb, ids, (e->e_id == NOID ? 1 : e->e_id) );
543 Debug(LDAP_DEBUG_TRACE,
544 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
546 (long) BDB_IDL_FIRST(ids),
547 (long) BDB_IDL_LAST(ids) );