1 /* search.c - search operation */
4 * Copyright 1998-2002 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 struct berval *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 BVarray v2refs = NULL;
55 Entry *matched = NULL;
56 struct berval realbase = { 0, NULL };
60 struct slap_limits_set *limit = NULL;
63 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
66 manageDSAit = get_manageDSAit( op );
68 if ( nbase->bv_len == 0 ) {
69 /* DIT root special case */
70 e = (Entry *) &slap_entry_root;
74 /* get entry with reader lock */
75 if ( deref & LDAP_DEREF_FINDING ) {
76 e = deref_dn_r( be, nbase-, &err, &matched, &text );
81 rc = bdb_dn2entry( be, NULL, nbase, &e, &matched, 0 );
89 send_ldap_result( conn, op, rc=LDAP_OTHER,
90 NULL, "internal error", NULL, NULL );
95 struct berval matched_dn = { 0, NULL };
98 if ( matched != NULL ) {
101 ber_dupbv( &matched_dn, &matched->e_name );
103 erefs = is_entry_referral( matched )
104 ? get_entry_referrals( be, conn, op, matched )
107 bdb_entry_return( be, matched );
111 refs = referral_rewrite( erefs, &matched_dn,
113 bvarray_free( erefs );
117 refs = referral_rewrite( default_referral,
121 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
122 matched_dn.bv_val, text, refs, NULL );
124 if ( refs ) bvarray_free( refs );
125 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
129 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
130 /* entry is a referral, don't allow add */
131 struct berval matched_dn;
134 ber_dupbv( &matched_dn, &e->e_name );
135 erefs = get_entry_referrals( be, conn, op, e );
138 bdb_entry_return( be, e );
142 refs = referral_rewrite( erefs, &matched_dn,
144 bvarray_free( erefs );
147 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
150 send_ldap_result( conn, op, LDAP_REFERRAL,
152 refs ? NULL : "bad referral object",
155 bvarray_free( refs );
156 ber_memfree( matched_dn.bv_val );
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 BDB_IDL_ALL( bdb, candidates );
236 rc = search_candidates( be, op, e, filter,
237 scope, deref, candidates );
240 /* need normalized dn below */
241 ber_dupbv( &realbase, &e->e_nname );
243 /* start cursor at base entry's id
244 * FIXME: hack to make "" base work */
245 cursor = e->e_id == NOID ? 1 : e->e_id;
247 if ( e != &slap_entry_root ) {
248 bdb_entry_return( be, e );
252 if ( candidates[0] == 0 ) {
253 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
256 send_search_result( conn, op,
258 NULL, NULL, NULL, NULL, 0 );
264 /* if not root and candidates exceed to-be-checked entries, abort */
265 if ( !isroot && limit->lms_s_unchecked != -1 ) {
266 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
267 send_search_result( conn, op,
268 LDAP_UNWILLING_TO_PERFORM,
269 NULL, NULL, NULL, NULL, 0 );
275 for ( id = bdb_idl_first( candidates, &cursor );
277 id = bdb_idl_next( candidates, &cursor ) )
281 /* check for abandon */
282 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
283 abandon = op->o_abandon;
284 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
291 /* check time limit */
292 if ( tlimit != -1 && slap_get_time() > stoptime ) {
293 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
294 NULL, NULL, v2refs, NULL, nentries );
298 /* get the entry with reader lock */
299 rc = bdb_id2entry( be, NULL, id, &e );
302 if( !BDB_IDL_IS_RANGE(candidates) ) {
303 /* only complain for non-range IDLs */
304 Debug( LDAP_DEBUG_TRACE,
305 "bdb_search: candidate %ld not found\n",
313 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
318 e = deref_entry_r( be, e, &err, &matched, &text );
325 if( e->e_id == id ) {
330 /* need to skip alias which deref into scope */
331 if( scope & LDAP_SCOPE_ONELEVEL ) {
332 char *pdn = dn_parent( NULL, e->e_ndn );
334 if( strcmp( pdn, realbase.bv_val ) ) {
339 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
340 /* alias is within scope */
341 Debug( LDAP_DEBUG_TRACE,
342 "bdb_search: \"%s\" in subtree\n",
352 * if it's a referral, add it to the list of referrals. only do
353 * this for non-base searches, and don't check the filter
354 * explicitly here since it's only a candidate anyway.
356 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
357 is_entry_referral( e ) )
359 BVarray erefs = get_entry_referrals(
361 BVarray refs = referral_rewrite( erefs,
363 scope == LDAP_SCOPE_SUBTREE
367 send_search_reference( be, conn, op,
368 e, refs, NULL, &v2refs );
370 bvarray_free( refs );
375 /* if it matches the filter and scope, send it */
376 rc = test_filter( be, conn, op, e, filter );
377 if ( rc == LDAP_COMPARE_TRUE ) {
381 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
382 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
383 scopeok = (dn == realbase.bv_val)
385 : (strcmp( dn, realbase.bv_val ) ? 0 : 1 );
387 scopeok = (realbase.bv_len == 0);
390 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
391 scopeok = dnIsSuffix( &e->e_nname, &realbase );
398 /* check size limit */
399 if ( --slimit == -1 ) {
400 bdb_entry_return( be, e );
402 send_search_result( conn, op,
403 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
404 v2refs, NULL, nentries );
409 int result = send_search_entry( be, conn, op,
410 e, attrs, attrsonly, NULL);
413 case 0: /* entry sent ok */
416 case 1: /* entry not sent */
418 case -1: /* connection closed */
419 bdb_entry_return( be, e );
426 Debug( LDAP_DEBUG_TRACE,
427 "bdb_search: %ld scope not okay\n",
431 Debug( LDAP_DEBUG_TRACE,
432 "bdb_search: %ld does match filter\n",
438 /* free reader lock */
439 bdb_entry_return( be, e );
442 ldap_pvt_thread_yield();
444 send_search_result( conn, op,
445 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
446 NULL, NULL, v2refs, NULL, nentries );
451 if( v2refs ) bvarray_free( v2refs );
452 if( realbase.bv_val ) ch_free( realbase.bv_val );
458 static int base_candidate(
463 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
464 e->e_dn, (long) e->e_id, 0);
471 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
472 * doesn't count, we're looking for explicit values.
474 static int oc_filter(
480 switch(f->f_choice) {
481 case LDAP_FILTER_EQUALITY:
482 case LDAP_FILTER_APPROX:
483 if (f->f_av_desc == slap_schema.si_ad_objectClass)
487 case LDAP_FILTER_SUBSTRINGS:
488 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
492 case LDAP_FILTER_AND:
494 for (f=f->f_and; f; f=f->f_next)
495 if ((rc = oc_filter(f)))
504 static int search_candidates(
514 Filter f, scopef, sf, rf, xf;
515 ID tmp[BDB_IDL_UM_SIZE];
516 AttributeAssertion aa_ref;
517 AttributeAssertion aa_subentry;
518 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
521 AttributeAssertion aa_alias;
525 * This routine takes as input a filter (user-filter)
526 * and rewrites it as follows:
527 * (&(scope=DN)[(objectClass=subentry)]
528 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
531 Debug(LDAP_DEBUG_TRACE,
532 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
533 e->e_dn, (long) e->e_id, scope );
536 xf.f_choice = LDAP_FILTER_OR;
539 /* If the user's filter doesn't mention objectClass, or if
540 * it just uses objectClass=*, these clauses are redundant.
542 if (oc_filter(filter) && !get_subentries_visibility(op) ) {
543 if( !get_manageDSAit(op) ) { /* match referrals */
544 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
545 rf.f_choice = LDAP_FILTER_EQUALITY;
547 rf.f_av_desc = slap_schema.si_ad_objectClass;
548 rf.f_av_value = bv_ref;
554 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
555 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
556 af.f_choice = LDAP_FILTER_EQUALITY;
557 af.f_ava = &aa_alias;
558 af.f_av_desc = slap_schema.si_ad_objectClass;
559 af.f_av_value = bv_alias;
567 f.f_choice = LDAP_FILTER_AND;
569 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
570 ? SLAPD_FILTER_DN_SUBTREE
571 : SLAPD_FILTER_DN_ONE;
572 scopef.f_dn = &e->e_nname;
573 scopef.f_next = xf.f_or == filter ? filter : &xf ;
575 if( get_subentries_visibility( op ) ) {
576 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
577 sf.f_choice = LDAP_FILTER_EQUALITY;
578 sf.f_ava = &aa_subentry;
579 sf.f_av_desc = slap_schema.si_ad_objectClass;
580 sf.f_av_value = bv_subentry;
581 sf.f_next = scopef.f_next;
585 #ifdef BDB_FILTER_INDICES
586 rc = bdb_filter_candidates( be, &f, ids, tmp );
588 /* FIXME: Original code:
589 BDB_IDL_ID( bdb, ids, e->e_id );
590 * this is a hack to make "" base work; when bdb_filter_candidates
591 * is used this should not be needed any more */
592 BDB_IDL_ID( bdb, ids, (e->e_id == NOID ? 1 : e->e_id) );
596 Debug(LDAP_DEBUG_TRACE,
597 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
599 (long) BDB_IDL_FIRST(ids),
600 (long) BDB_IDL_LAST(ids) );