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;
48 const char *text = NULL;
51 ID candidates[BDB_IDL_UM_SIZE];
53 BerVarray v2refs = NULL;
54 Entry *matched = NULL;
55 struct berval realbase = { 0, NULL };
59 struct slap_limits_set *limit = NULL;
63 LDAP_LOG (( "search", LDAP_LEVEL_ENTRY,"bdb_back_search\n"));
65 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
69 manageDSAit = get_manageDSAit( op );
71 if ( nbase->bv_len == 0 ) {
72 /* DIT root special case */
73 e = (Entry *) &slap_entry_root;
77 /* get entry with reader lock */
78 if ( deref & LDAP_DEREF_FINDING ) {
79 e = deref_dn_r( be, nbase-, &err, &matched, &text );
84 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0 );
93 bdb_cache_return_entry_w(&bdb->bi_cache, e);
95 if (matched != NULL) {
96 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
98 send_ldap_result( conn, op, rc=LDAP_OTHER,
99 NULL, "internal error", NULL, NULL );
104 struct berval matched_dn = { 0, NULL };
105 BerVarray refs = NULL;
107 if ( matched != NULL ) {
110 ber_dupbv( &matched_dn, &matched->e_name );
112 erefs = is_entry_referral( matched )
113 ? get_entry_referrals( be, conn, op, matched )
116 bdb_cache_return_entry_r (&bdb->bi_cache, matched);
120 refs = referral_rewrite( erefs, &matched_dn,
122 ber_bvarray_free( erefs );
126 refs = referral_rewrite( default_referral,
130 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
131 matched_dn.bv_val, text, refs, NULL );
133 if ( refs ) ber_bvarray_free( refs );
134 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
138 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
139 /* entry is a referral, don't allow add */
140 struct berval matched_dn;
141 BerVarray erefs, refs;
143 ber_dupbv( &matched_dn, &e->e_name );
144 erefs = get_entry_referrals( be, conn, op, e );
147 bdb_cache_return_entry_r( &bdb->bi_cache, e );
151 refs = referral_rewrite( erefs, &matched_dn,
153 ber_bvarray_free( erefs );
157 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: entry is referral\n"));
159 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
163 send_ldap_result( conn, op, LDAP_REFERRAL,
165 refs ? NULL : "bad referral object",
168 ber_bvarray_free( refs );
169 ber_memfree( matched_dn.bv_val );
173 /* if not root, get appropriate limits */
174 if ( be_isroot( be, &op->o_ndn ) ) {
177 ( void ) get_limits( be, &op->o_ndn, &limit );
180 /* The time/size limits come first because they require very little
181 * effort, so there's no chance the candidates are selected and then
182 * the request is not honored only because of time/size constraints */
184 /* if no time limit requested, use soft limit (unless root!) */
187 tlimit = -1; /* allow root to set no limit */
195 /* if no limit is required, use soft limit */
197 tlimit = limit->lms_t_soft;
199 /* if requested limit higher than hard limit, abort */
200 } else if ( tlimit > limit->lms_t_hard ) {
201 /* no hard limit means use soft instead */
202 if ( limit->lms_t_hard == 0 ) {
203 tlimit = limit->lms_t_soft;
205 /* positive hard limit means abort */
206 } else if ( limit->lms_t_hard > 0 ) {
207 send_search_result( conn, op,
208 LDAP_UNWILLING_TO_PERFORM,
209 NULL, NULL, NULL, NULL, 0 );
214 /* negative hard limit means no limit */
217 /* if no limit is required, use soft limit */
219 slimit = limit->lms_s_soft;
221 /* if requested limit higher than hard limit, abort */
222 } else if ( slimit > limit->lms_s_hard ) {
223 /* no hard limit means use soft instead */
224 if ( limit->lms_s_hard == 0 ) {
225 slimit = limit->lms_s_soft;
227 /* positive hard limit means abort */
228 } else if ( limit->lms_s_hard > 0 ) {
229 send_search_result( conn, op,
230 LDAP_UNWILLING_TO_PERFORM,
231 NULL, NULL, NULL, NULL, 0 );
236 /* negative hard limit means no limit */
240 /* compute it anyway; root does not use it */
241 stoptime = op->o_time + tlimit;
243 /* select candidates */
244 if ( scope == LDAP_SCOPE_BASE ) {
245 rc = base_candidate( be, e, candidates );
248 BDB_IDL_ALL( bdb, candidates );
249 rc = search_candidates( be, op, e, filter,
250 scope, deref, candidates );
253 /* need normalized dn below */
254 ber_dupbv( &realbase, &e->e_nname );
256 /* start cursor at base entry's id
257 * FIXME: hack to make "" base work
258 * FIXME: moddn needs to assign new ID for this to work
260 cursor = e->e_id == NOID ? 1 : e->e_id;
262 if ( e != &slap_entry_root ) {
263 bdb_cache_return_entry_r(&bdb->bi_cache, e);
267 if ( candidates[0] == 0 ) {
269 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: no candidates\n"));
271 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
275 send_search_result( conn, op,
277 NULL, NULL, NULL, NULL, 0 );
283 /* if not root and candidates exceed to-be-checked entries, abort */
284 if ( !isroot && limit->lms_s_unchecked != -1 ) {
285 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
286 send_search_result( conn, op,
287 LDAP_UNWILLING_TO_PERFORM,
288 NULL, NULL, NULL, NULL, 0 );
294 for ( id = bdb_idl_first( candidates, &cursor );
296 id = bdb_idl_next( candidates, &cursor ) )
300 /* check for abandon */
301 if ( op->o_abandon ) {
306 /* check time limit */
307 if ( tlimit != -1 && slap_get_time() > stoptime ) {
308 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
309 NULL, NULL, v2refs, NULL, nentries );
313 /* get the entry with reader lock */
314 rc = bdb_id2entry_r( be, NULL, id, &e );
317 if( !BDB_IDL_IS_RANGE(candidates) ) {
318 /* only complain for non-range IDLs */
320 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: candidate %ld not found\n", (long) id));
322 Debug( LDAP_DEBUG_TRACE,
323 "bdb_search: candidate %ld not found\n",
331 #ifdef BDB_SUBENTRIES
332 if ( is_entry_subentry( e ) ) {
333 if( scope != LDAP_SCOPE_BASE ) {
334 if(!get_subentries_visibility( op )) {
335 /* only subentries are visible */
339 } else if ( get_subentries( op ) &&
340 !get_subentries_visibility( op ))
342 /* only subentries are visible */
346 } else if ( get_subentries_visibility( op )) {
347 /* only subentries are visible */
353 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
358 e = deref_entry_r( be, e, &err, &matched, &text );
365 if( e->e_id == id ) {
370 /* need to skip alias which deref into scope */
371 if( scope & LDAP_SCOPE_ONELEVEL ) {
374 dnParent( &e->e_nname, &pdn ):
375 if ( ber_bvcmp( pdn, &realbase ) ) {
379 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
380 /* alias is within scope */
382 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: \"%s\" in subtree\n", e->edn));
384 Debug( LDAP_DEBUG_TRACE,
385 "bdb_search: \"%s\" in subtree\n",
396 * if it's a referral, add it to the list of referrals. only do
397 * this for non-base searches, and don't check the filter
398 * explicitly here since it's only a candidate anyway.
400 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
401 is_entry_referral( e ) )
403 BerVarray erefs = get_entry_referrals(
405 BerVarray refs = referral_rewrite( erefs,
407 scope == LDAP_SCOPE_SUBTREE
411 send_search_reference( be, conn, op,
412 e, refs, NULL, &v2refs );
414 ber_bvarray_free( refs );
419 /* if it matches the filter and scope, send it */
420 rc = test_filter( be, conn, op, e, filter );
421 if ( rc == LDAP_COMPARE_TRUE ) {
425 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
426 if ( be_issuffix( be, &e->e_nname ) ) {
427 scopeok = (realbase.bv_len == 0);
429 dnParent( &e->e_nname, &dn );
430 scopeok = dn_match( &dn, &realbase );
433 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
434 scopeok = dnIsSuffix( &e->e_nname, &realbase );
441 /* check size limit */
442 if ( --slimit == -1 ) {
443 bdb_cache_return_entry_r (&bdb->bi_cache, e);
445 send_search_result( conn, op,
446 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
447 v2refs, NULL, nentries );
457 result = send_search_entry( be, conn, op,
458 e, attrs, attrsonly, NULL);
462 case 0: /* entry sent ok */
465 case 1: /* entry not sent */
467 case -1: /* connection closed */
468 bdb_cache_return_entry_r(&bdb->bi_cache, e);
476 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: %ld scope not okay\n", (long) id));
478 Debug( LDAP_DEBUG_TRACE,
479 "bdb_search: %ld scope not okay\n",
485 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: %ld does match filter\n", (long) id));
487 Debug( LDAP_DEBUG_TRACE,
488 "bdb_search: %ld does match filter\n",
495 /* free reader lock */
496 bdb_cache_return_entry_r ( &bdb->bi_cache, e );
500 ldap_pvt_thread_yield();
502 send_search_result( conn, op,
503 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
504 NULL, NULL, v2refs, NULL, nentries );
510 /* free reader lock */
511 bdb_cache_return_entry_r ( &bdb->bi_cache, e );
514 if( v2refs ) ber_bvarray_free( v2refs );
515 if( realbase.bv_val ) ch_free( realbase.bv_val );
521 static int base_candidate(
527 LDAP_LOG (( "search", LDAP_LEVEL_ENTRY,"base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id));
529 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
530 e->e_dn, (long) e->e_id, 0);
538 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
539 * doesn't count, we're looking for explicit values. Also count depth
540 * of filter tree while we're at it.
542 static int oc_filter(
550 if( cur > *max ) *max = cur;
552 switch(f->f_choice) {
553 case LDAP_FILTER_EQUALITY:
554 case LDAP_FILTER_APPROX:
555 if (f->f_av_desc == slap_schema.si_ad_objectClass)
559 case LDAP_FILTER_SUBSTRINGS:
560 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
564 case LDAP_FILTER_AND:
567 for (f=f->f_and; f; f=f->f_next)
568 if ((rc = oc_filter(f, cur, max)))
577 static int search_candidates(
587 Filter f, scopef, rf, xf;
589 AttributeAssertion aa_ref;
590 #ifdef BDB_SUBENTRIES
592 AttributeAssertion aa_subentry;
596 AttributeAssertion aa_alias;
600 * This routine takes as input a filter (user-filter)
601 * and rewrites it as follows:
602 * (&(scope=DN)[(objectClass=subentry)]
603 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
607 LDAP_LOG (( "search", LDAP_LEVEL_ENTRY,"search_candidates: base=\"%s\" (0x%08lx) scope=%d\n", e->e_dn, (long) e->e_id, scope));
609 Debug(LDAP_DEBUG_TRACE,
610 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
611 e->e_dn, (long) e->e_id, scope );
615 xf.f_choice = LDAP_FILTER_OR;
618 /* If the user's filter doesn't mention objectClass, or if
619 * it just uses objectClass=*, these clauses are redundant.
621 if (oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
622 if( !get_manageDSAit(op) ) { /* match referrals */
623 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
624 rf.f_choice = LDAP_FILTER_EQUALITY;
626 rf.f_av_desc = slap_schema.si_ad_objectClass;
627 rf.f_av_value = bv_ref;
633 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
634 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
635 af.f_choice = LDAP_FILTER_EQUALITY;
636 af.f_ava = &aa_alias;
637 af.f_av_desc = slap_schema.si_ad_objectClass;
638 af.f_av_value = bv_alias;
643 /* We added one of these clauses, filter depth increased */
644 if( xf.f_or != filter ) depth++;
648 f.f_choice = LDAP_FILTER_AND;
650 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
651 ? SLAPD_FILTER_DN_SUBTREE
652 : SLAPD_FILTER_DN_ONE;
653 scopef.f_dn = &e->e_nname;
654 scopef.f_next = xf.f_or == filter ? filter : &xf ;
655 /* Filter depth increased again, adding scope clause */
658 #ifdef BDB_SUBENTRIES
659 if( get_subentries_visibility( op ) ) {
660 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
661 sf.f_choice = LDAP_FILTER_EQUALITY;
662 sf.f_ava = &aa_subentry;
663 sf.f_av_desc = slap_schema.si_ad_objectClass;
664 sf.f_av_value = bv_subentry;
665 sf.f_next = scopef.f_next;
670 /* Allocate IDL stack, plus 1 more for former tmp */
671 stack = malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
673 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
679 LDAP_LOG (( "search", LDAP_LEVEL_DETAIL1,"bdb_search_candidates: failed (rc=%d)\n", rc));
681 Debug(LDAP_DEBUG_TRACE,
682 "bdb_search_candidates: failed (rc=%d)\n",
688 LDAP_LOG (( "search", LDAP_LEVEL_DETAIL1,"bdb_search_candidates: id=%ld first=%ld last=%ld\n", (long) ids[0], (long) BDB_IDL_FIRST(ids), (long) BDB_IDL_LAST(ids)));
690 Debug(LDAP_DEBUG_TRACE,
691 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
693 (long) BDB_IDL_FIRST(ids),
694 (long) BDB_IDL_LAST(ids) );