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 BerVarray v2refs = NULL;
55 Entry *matched = NULL;
56 struct berval realbase = { 0, NULL };
60 struct slap_limits_set *limit = NULL;
64 LDAP_LOG (( "search", LDAP_LEVEL_ENTRY,"bdb_back_search\n"));
66 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
70 manageDSAit = get_manageDSAit( op );
72 if ( nbase->bv_len == 0 ) {
73 /* DIT root special case */
74 e = (Entry *) &slap_entry_root;
78 /* get entry with reader lock */
79 if ( deref & LDAP_DEREF_FINDING ) {
80 e = deref_dn_r( be, nbase-, &err, &matched, &text );
85 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0 );
94 bdb_cache_return_entry_w(&bdb->bi_cache, e);
96 if (matched != NULL) {
97 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
99 send_ldap_result( conn, op, rc=LDAP_OTHER,
100 NULL, "internal error", NULL, NULL );
105 struct berval matched_dn = { 0, NULL };
106 BerVarray refs = NULL;
108 if ( matched != NULL ) {
111 ber_dupbv( &matched_dn, &matched->e_name );
113 erefs = is_entry_referral( matched )
114 ? get_entry_referrals( be, conn, op, matched )
117 bdb_cache_return_entry_r (&bdb->bi_cache, matched);
121 refs = referral_rewrite( erefs, &matched_dn,
123 ber_bvarray_free( erefs );
127 refs = referral_rewrite( default_referral,
131 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
132 matched_dn.bv_val, text, refs, NULL );
134 if ( refs ) ber_bvarray_free( refs );
135 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
139 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
140 /* entry is a referral, don't allow add */
141 struct berval matched_dn;
142 BerVarray erefs, refs;
144 ber_dupbv( &matched_dn, &e->e_name );
145 erefs = get_entry_referrals( be, conn, op, e );
148 bdb_cache_return_entry_r( &bdb->bi_cache, e );
152 refs = referral_rewrite( erefs, &matched_dn,
154 ber_bvarray_free( erefs );
158 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: entry is referral\n"));
160 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
164 send_ldap_result( conn, op, LDAP_REFERRAL,
166 refs ? NULL : "bad referral object",
169 ber_bvarray_free( refs );
170 ber_memfree( matched_dn.bv_val );
174 /* if not root, get appropriate limits */
175 if ( be_isroot( be, &op->o_ndn ) ) {
178 ( void ) get_limits( be, &op->o_ndn, &limit );
181 /* The time/size limits come first because they require very little
182 * effort, so there's no chance the candidates are selected and then
183 * the request is not honored only because of time/size constraints */
185 /* if no time limit requested, use soft limit (unless root!) */
188 tlimit = -1; /* allow root to set no limit */
196 /* if no limit is required, use soft limit */
198 tlimit = limit->lms_t_soft;
200 /* if requested limit higher than hard limit, abort */
201 } else if ( tlimit > limit->lms_t_hard ) {
202 /* no hard limit means use soft instead */
203 if ( limit->lms_t_hard == 0 ) {
204 tlimit = limit->lms_t_soft;
206 /* positive hard limit means abort */
207 } else if ( limit->lms_t_hard > 0 ) {
208 send_search_result( conn, op,
209 LDAP_UNWILLING_TO_PERFORM,
210 NULL, NULL, NULL, NULL, 0 );
215 /* negative hard limit means no limit */
218 /* if no limit is required, use soft limit */
220 slimit = limit->lms_s_soft;
222 /* if requested limit higher than hard limit, abort */
223 } else if ( slimit > limit->lms_s_hard ) {
224 /* no hard limit means use soft instead */
225 if ( limit->lms_s_hard == 0 ) {
226 slimit = limit->lms_s_soft;
228 /* positive hard limit means abort */
229 } else if ( limit->lms_s_hard > 0 ) {
230 send_search_result( conn, op,
231 LDAP_UNWILLING_TO_PERFORM,
232 NULL, NULL, NULL, NULL, 0 );
237 /* negative hard limit means no limit */
241 /* compute it anyway; root does not use it */
242 stoptime = op->o_time + tlimit;
244 /* select candidates */
245 if ( scope == LDAP_SCOPE_BASE ) {
246 rc = base_candidate( be, e, candidates );
249 BDB_IDL_ALL( bdb, candidates );
250 rc = search_candidates( be, op, e, filter,
251 scope, deref, candidates );
254 /* need normalized dn below */
255 ber_dupbv( &realbase, &e->e_nname );
257 /* start cursor at base entry's id
258 * FIXME: hack to make "" base work
259 * FIXME: moddn needs to assign new ID for this to work
261 cursor = e->e_id == NOID ? 1 : e->e_id;
263 if ( e != &slap_entry_root ) {
264 bdb_cache_return_entry_r(&bdb->bi_cache, e);
268 if ( candidates[0] == 0 ) {
270 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: no candidates\n"));
272 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
276 send_search_result( conn, op,
278 NULL, NULL, NULL, NULL, 0 );
284 /* if not root and candidates exceed to-be-checked entries, abort */
285 if ( !isroot && limit->lms_s_unchecked != -1 ) {
286 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
287 send_search_result( conn, op,
288 LDAP_UNWILLING_TO_PERFORM,
289 NULL, NULL, NULL, NULL, 0 );
295 for ( id = bdb_idl_first( candidates, &cursor );
297 id = bdb_idl_next( candidates, &cursor ) )
301 /* check for abandon */
302 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
303 abandon = op->o_abandon;
304 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
311 /* check time limit */
312 if ( tlimit != -1 && slap_get_time() > stoptime ) {
313 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
314 NULL, NULL, v2refs, NULL, nentries );
318 /* get the entry with reader lock */
319 rc = bdb_id2entry_r( be, NULL, id, &e );
322 if( !BDB_IDL_IS_RANGE(candidates) ) {
323 /* only complain for non-range IDLs */
325 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: candidate %ld not found\n", (long) id));
327 Debug( LDAP_DEBUG_TRACE,
328 "bdb_search: candidate %ld not found\n",
336 #ifdef BDB_SUBENTRIES
337 if ( is_entry_subentry( e ) ) {
338 if( scope != LDAP_SCOPE_BASE ) {
339 if(!get_subentries_visibility( op )) {
340 /* only subentries are visible */
344 } else if ( get_subentries( op ) &&
345 !get_subentries_visibility( op ))
347 /* only subentries are visible */
351 } else if ( get_subentries_visibility( op )) {
352 /* only subentries are visible */
358 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
363 e = deref_entry_r( be, e, &err, &matched, &text );
370 if( e->e_id == id ) {
375 /* need to skip alias which deref into scope */
376 if( scope & LDAP_SCOPE_ONELEVEL ) {
379 dnParent( &e->e_nname, &pdn ):
380 if ( ber_bvcmp( pdn, &realbase ) ) {
384 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
385 /* alias is within scope */
387 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: \"%s\" in subtree\n", e->edn));
389 Debug( LDAP_DEBUG_TRACE,
390 "bdb_search: \"%s\" in subtree\n",
401 * if it's a referral, add it to the list of referrals. only do
402 * this for non-base searches, and don't check the filter
403 * explicitly here since it's only a candidate anyway.
405 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
406 is_entry_referral( e ) )
408 BerVarray erefs = get_entry_referrals(
410 BerVarray refs = referral_rewrite( erefs,
412 scope == LDAP_SCOPE_SUBTREE
416 send_search_reference( be, conn, op,
417 e, refs, NULL, &v2refs );
419 ber_bvarray_free( refs );
424 /* if it matches the filter and scope, send it */
425 rc = test_filter( be, conn, op, e, filter );
426 if ( rc == LDAP_COMPARE_TRUE ) {
430 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
431 if ( be_issuffix( be, &e->e_nname ) ) {
432 scopeok = (realbase.bv_len == 0);
434 dnParent( &e->e_nname, &dn );
435 scopeok = dn_match( &dn, &realbase );
438 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
439 scopeok = dnIsSuffix( &e->e_nname, &realbase );
446 /* check size limit */
447 if ( --slimit == -1 ) {
448 bdb_cache_return_entry_r (&bdb->bi_cache, e);
450 send_search_result( conn, op,
451 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
452 v2refs, NULL, nentries );
462 result = send_search_entry( be, conn, op,
463 e, attrs, attrsonly, NULL);
467 case 0: /* entry sent ok */
470 case 1: /* entry not sent */
472 case -1: /* connection closed */
473 bdb_cache_return_entry_r(&bdb->bi_cache, e);
481 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: %ld scope not okay\n", (long) id));
483 Debug( LDAP_DEBUG_TRACE,
484 "bdb_search: %ld scope not okay\n",
490 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: %ld does match filter\n", (long) id));
492 Debug( LDAP_DEBUG_TRACE,
493 "bdb_search: %ld does match filter\n",
500 /* free reader lock */
501 bdb_cache_return_entry_r ( &bdb->bi_cache, e );
505 ldap_pvt_thread_yield();
507 send_search_result( conn, op,
508 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
509 NULL, NULL, v2refs, NULL, nentries );
515 /* free reader lock */
516 bdb_cache_return_entry_r ( &bdb->bi_cache, e );
519 if( v2refs ) ber_bvarray_free( v2refs );
520 if( realbase.bv_val ) ch_free( realbase.bv_val );
526 static int base_candidate(
532 LDAP_LOG (( "search", LDAP_LEVEL_ENTRY,"base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id));
534 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
535 e->e_dn, (long) e->e_id, 0);
543 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
544 * doesn't count, we're looking for explicit values. Also count depth
545 * of filter tree while we're at it.
547 static int oc_filter(
555 if( cur > *max ) *max = cur;
557 switch(f->f_choice) {
558 case LDAP_FILTER_EQUALITY:
559 case LDAP_FILTER_APPROX:
560 if (f->f_av_desc == slap_schema.si_ad_objectClass)
564 case LDAP_FILTER_SUBSTRINGS:
565 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
569 case LDAP_FILTER_AND:
572 for (f=f->f_and; f; f=f->f_next)
573 if ((rc = oc_filter(f, cur, max)))
582 static int search_candidates(
592 Filter f, scopef, rf, xf;
594 AttributeAssertion aa_ref;
595 #ifdef BDB_SUBENTRIES
597 AttributeAssertion aa_subentry;
601 AttributeAssertion aa_alias;
605 * This routine takes as input a filter (user-filter)
606 * and rewrites it as follows:
607 * (&(scope=DN)[(objectClass=subentry)]
608 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
612 LDAP_LOG (( "search", LDAP_LEVEL_ENTRY,"search_candidates: base=\"%s\" (0x%08lx) scope=%d\n", e->e_dn, (long) e->e_id, scope));
614 Debug(LDAP_DEBUG_TRACE,
615 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
616 e->e_dn, (long) e->e_id, scope );
620 xf.f_choice = LDAP_FILTER_OR;
623 /* If the user's filter doesn't mention objectClass, or if
624 * it just uses objectClass=*, these clauses are redundant.
626 if (oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
627 if( !get_manageDSAit(op) ) { /* match referrals */
628 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
629 rf.f_choice = LDAP_FILTER_EQUALITY;
631 rf.f_av_desc = slap_schema.si_ad_objectClass;
632 rf.f_av_value = bv_ref;
638 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
639 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
640 af.f_choice = LDAP_FILTER_EQUALITY;
641 af.f_ava = &aa_alias;
642 af.f_av_desc = slap_schema.si_ad_objectClass;
643 af.f_av_value = bv_alias;
648 /* We added one of these clauses, filter depth increased */
649 if( xf.f_or != filter ) depth++;
653 f.f_choice = LDAP_FILTER_AND;
655 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
656 ? SLAPD_FILTER_DN_SUBTREE
657 : SLAPD_FILTER_DN_ONE;
658 scopef.f_dn = &e->e_nname;
659 scopef.f_next = xf.f_or == filter ? filter : &xf ;
660 /* Filter depth increased again, adding scope clause */
663 #ifdef BDB_SUBENTRIES
664 if( get_subentries_visibility( op ) ) {
665 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
666 sf.f_choice = LDAP_FILTER_EQUALITY;
667 sf.f_ava = &aa_subentry;
668 sf.f_av_desc = slap_schema.si_ad_objectClass;
669 sf.f_av_value = bv_subentry;
670 sf.f_next = scopef.f_next;
675 /* Allocate IDL stack, plus 1 more for former tmp */
676 stack = malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
678 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
684 LDAP_LOG (( "search", LDAP_LEVEL_DETAIL1,"bdb_search_candidates: failed (rc=%d)\n", rc));
686 Debug(LDAP_DEBUG_TRACE,
687 "bdb_search_candidates: failed (rc=%d)\n",
693 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)));
695 Debug(LDAP_DEBUG_TRACE,
696 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
698 (long) BDB_IDL_FIRST(ids),
699 (long) BDB_IDL_LAST(ids) );