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_r(&bdb->bi_cache, e);
95 if (matched != NULL) {
96 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
98 send_ldap_result( conn, op, LDAP_BUSY,
99 NULL, "ldap server busy", NULL, NULL );
103 bdb_cache_return_entry_r(&bdb->bi_cache, e);
105 if (matched != NULL) {
106 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
108 send_ldap_result( conn, op, rc=LDAP_OTHER,
109 NULL, "internal error", NULL, NULL );
114 struct berval matched_dn = { 0, NULL };
115 BerVarray refs = NULL;
117 if ( matched != NULL ) {
120 ber_dupbv( &matched_dn, &matched->e_name );
122 erefs = is_entry_referral( matched )
123 ? get_entry_referrals( be, conn, op, matched )
126 bdb_cache_return_entry_r (&bdb->bi_cache, matched);
130 refs = referral_rewrite( erefs, &matched_dn,
132 ber_bvarray_free( erefs );
136 refs = referral_rewrite( default_referral,
140 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
141 matched_dn.bv_val, text, refs, NULL );
143 if ( refs ) ber_bvarray_free( refs );
144 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
148 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
149 /* entry is a referral, don't allow add */
150 struct berval matched_dn;
151 BerVarray erefs, refs;
153 ber_dupbv( &matched_dn, &e->e_name );
154 erefs = get_entry_referrals( be, conn, op, e );
157 bdb_cache_return_entry_r( &bdb->bi_cache, e );
161 refs = referral_rewrite( erefs, &matched_dn,
163 ber_bvarray_free( erefs );
167 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: entry is referral\n"));
169 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
173 send_ldap_result( conn, op, LDAP_REFERRAL,
175 refs ? NULL : "bad referral object",
178 ber_bvarray_free( refs );
179 ber_memfree( matched_dn.bv_val );
183 /* if not root, get appropriate limits */
184 if ( be_isroot( be, &op->o_ndn ) ) {
187 ( void ) get_limits( be, &op->o_ndn, &limit );
190 /* The time/size limits come first because they require very little
191 * effort, so there's no chance the candidates are selected and then
192 * the request is not honored only because of time/size constraints */
194 /* if no time limit requested, use soft limit (unless root!) */
197 tlimit = -1; /* allow root to set no limit */
205 /* if no limit is required, use soft limit */
207 tlimit = limit->lms_t_soft;
209 /* if requested limit higher than hard limit, abort */
210 } else if ( tlimit > limit->lms_t_hard ) {
211 /* no hard limit means use soft instead */
212 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
213 tlimit = limit->lms_t_soft;
215 /* positive hard limit means abort */
216 } else if ( limit->lms_t_hard > 0 ) {
217 send_search_result( conn, op,
218 LDAP_UNWILLING_TO_PERFORM,
219 NULL, NULL, NULL, NULL, 0 );
224 /* negative hard limit means no limit */
227 /* if no limit is required, use soft limit */
229 slimit = limit->lms_s_soft;
231 /* if requested limit higher than hard limit, abort */
232 } else if ( slimit > limit->lms_s_hard ) {
233 /* no hard limit means use soft instead */
234 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
235 slimit = limit->lms_s_soft;
237 /* positive hard limit means abort */
238 } else if ( limit->lms_s_hard > 0 ) {
239 send_search_result( conn, op,
240 LDAP_UNWILLING_TO_PERFORM,
241 NULL, NULL, NULL, NULL, 0 );
246 /* negative hard limit means no limit */
250 /* compute it anyway; root does not use it */
251 stoptime = op->o_time + tlimit;
253 /* select candidates */
254 if ( scope == LDAP_SCOPE_BASE ) {
255 rc = base_candidate( be, e, candidates );
258 BDB_IDL_ALL( bdb, candidates );
259 rc = search_candidates( be, op, e, filter,
260 scope, deref, candidates );
263 /* need normalized dn below */
264 ber_dupbv( &realbase, &e->e_nname );
266 /* start cursor at base entry's id
267 * FIXME: hack to make "" base work
268 * FIXME: moddn needs to assign new ID for this to work
270 cursor = e->e_id == NOID ? 1 : e->e_id;
272 if ( e != &slap_entry_root ) {
273 bdb_cache_return_entry_r(&bdb->bi_cache, e);
277 if ( candidates[0] == 0 ) {
279 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: no candidates\n"));
281 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
285 send_search_result( conn, op,
287 NULL, NULL, NULL, NULL, 0 );
293 /* if not root and candidates exceed to-be-checked entries, abort */
294 if ( !isroot && limit->lms_s_unchecked != -1 ) {
295 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
296 send_search_result( conn, op,
297 LDAP_ADMINLIMIT_EXCEEDED,
298 NULL, NULL, NULL, NULL, 0 );
304 for ( id = bdb_idl_first( candidates, &cursor );
306 id = bdb_idl_next( candidates, &cursor ) )
310 /* check for abandon */
311 if ( op->o_abandon ) {
316 /* check time limit */
317 if ( tlimit != -1 && slap_get_time() > stoptime ) {
318 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
319 NULL, NULL, v2refs, NULL, nentries );
323 /* get the entry with reader lock */
324 rc = bdb_id2entry_r( be, NULL, id, &e );
326 if (rc == LDAP_BUSY) {
327 send_ldap_result( conn, op, rc=LDAP_BUSY,
328 NULL, "ldap server busy", NULL, NULL );
333 if( !BDB_IDL_IS_RANGE(candidates) ) {
334 /* only complain for non-range IDLs */
336 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: candidate %ld not found\n", (long) id));
338 Debug( LDAP_DEBUG_TRACE,
339 "bdb_search: candidate %ld not found\n",
347 #ifdef BDB_SUBENTRIES
348 if ( is_entry_subentry( e ) ) {
349 if( scope != LDAP_SCOPE_BASE ) {
350 if(!get_subentries_visibility( op )) {
351 /* only subentries are visible */
355 } else if ( get_subentries( op ) &&
356 !get_subentries_visibility( op ))
358 /* only subentries are visible */
362 } else if ( get_subentries_visibility( op )) {
363 /* only subentries are visible */
369 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
374 e = deref_entry_r( be, e, &err, &matched, &text );
381 if( e->e_id == id ) {
386 /* need to skip alias which deref into scope */
387 if( scope & LDAP_SCOPE_ONELEVEL ) {
390 dnParent( &e->e_nname, &pdn ):
391 if ( ber_bvcmp( pdn, &realbase ) ) {
395 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
396 /* alias is within scope */
398 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: \"%s\" in subtree\n", e->edn));
400 Debug( LDAP_DEBUG_TRACE,
401 "bdb_search: \"%s\" in subtree\n",
412 * if it's a referral, add it to the list of referrals. only do
413 * this for non-base searches, and don't check the filter
414 * explicitly here since it's only a candidate anyway.
416 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
417 is_entry_referral( e ) )
419 BerVarray erefs = get_entry_referrals(
421 BerVarray refs = referral_rewrite( erefs,
423 scope == LDAP_SCOPE_SUBTREE
427 send_search_reference( be, conn, op,
428 e, refs, NULL, &v2refs );
430 ber_bvarray_free( refs );
435 /* if it matches the filter and scope, send it */
436 rc = test_filter( be, conn, op, e, filter );
437 if ( rc == LDAP_COMPARE_TRUE ) {
441 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
442 if ( be_issuffix( be, &e->e_nname ) ) {
443 scopeok = (realbase.bv_len == 0);
445 dnParent( &e->e_nname, &dn );
446 scopeok = dn_match( &dn, &realbase );
449 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
450 scopeok = dnIsSuffix( &e->e_nname, &realbase );
457 /* check size limit */
458 if ( --slimit == -1 ) {
459 bdb_cache_return_entry_r (&bdb->bi_cache, e);
461 send_search_result( conn, op,
462 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
463 v2refs, NULL, nentries );
473 result = send_search_entry( be, conn, op,
474 e, attrs, attrsonly, NULL);
478 case 0: /* entry sent ok */
481 case 1: /* entry not sent */
483 case -1: /* connection closed */
484 bdb_cache_return_entry_r(&bdb->bi_cache, e);
492 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: %ld scope not okay\n", (long) id));
494 Debug( LDAP_DEBUG_TRACE,
495 "bdb_search: %ld scope not okay\n",
501 LDAP_LOG (( "search", LDAP_LEVEL_RESULTS,"bdb_search: %ld does match filter\n", (long) id));
503 Debug( LDAP_DEBUG_TRACE,
504 "bdb_search: %ld does match filter\n",
511 /* free reader lock */
512 bdb_cache_return_entry_r ( &bdb->bi_cache, e );
516 ldap_pvt_thread_yield();
518 send_search_result( conn, op,
519 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
520 NULL, NULL, v2refs, NULL, nentries );
526 /* free reader lock */
527 bdb_cache_return_entry_r ( &bdb->bi_cache, e );
530 if( v2refs ) ber_bvarray_free( v2refs );
531 if( realbase.bv_val ) ch_free( realbase.bv_val );
537 static int base_candidate(
543 LDAP_LOG (( "search", LDAP_LEVEL_ENTRY,"base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id));
545 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
546 e->e_dn, (long) e->e_id, 0);
554 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
555 * doesn't count, we're looking for explicit values. Also count depth
556 * of filter tree while we're at it.
558 static int oc_filter(
566 if( cur > *max ) *max = cur;
568 switch(f->f_choice) {
569 case LDAP_FILTER_EQUALITY:
570 case LDAP_FILTER_APPROX:
571 if (f->f_av_desc == slap_schema.si_ad_objectClass)
575 case LDAP_FILTER_SUBSTRINGS:
576 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
580 case LDAP_FILTER_AND:
583 for (f=f->f_and; f; f=f->f_next)
584 if ((rc = oc_filter(f, cur, max)))
593 static int search_candidates(
603 Filter f, scopef, rf, xf;
605 AttributeAssertion aa_ref;
606 #ifdef BDB_SUBENTRIES
608 AttributeAssertion aa_subentry;
612 AttributeAssertion aa_alias;
616 * This routine takes as input a filter (user-filter)
617 * and rewrites it as follows:
618 * (&(scope=DN)[(objectClass=subentry)]
619 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
623 LDAP_LOG (( "search", LDAP_LEVEL_ENTRY,"search_candidates: base=\"%s\" (0x%08lx) scope=%d\n", e->e_dn, (long) e->e_id, scope));
625 Debug(LDAP_DEBUG_TRACE,
626 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
627 e->e_dn, (long) e->e_id, scope );
631 xf.f_choice = LDAP_FILTER_OR;
634 /* If the user's filter doesn't mention objectClass, or if
635 * it just uses objectClass=*, these clauses are redundant.
637 if (oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
638 if( !get_manageDSAit(op) ) { /* match referrals */
639 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
640 rf.f_choice = LDAP_FILTER_EQUALITY;
642 rf.f_av_desc = slap_schema.si_ad_objectClass;
643 rf.f_av_value = bv_ref;
649 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
650 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
651 af.f_choice = LDAP_FILTER_EQUALITY;
652 af.f_ava = &aa_alias;
653 af.f_av_desc = slap_schema.si_ad_objectClass;
654 af.f_av_value = bv_alias;
659 /* We added one of these clauses, filter depth increased */
660 if( xf.f_or != filter ) depth++;
664 f.f_choice = LDAP_FILTER_AND;
666 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
667 ? SLAPD_FILTER_DN_SUBTREE
668 : SLAPD_FILTER_DN_ONE;
669 scopef.f_dn = &e->e_nname;
670 scopef.f_next = xf.f_or == filter ? filter : &xf ;
671 /* Filter depth increased again, adding scope clause */
674 #ifdef BDB_SUBENTRIES
675 if( get_subentries_visibility( op ) ) {
676 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
677 sf.f_choice = LDAP_FILTER_EQUALITY;
678 sf.f_ava = &aa_subentry;
679 sf.f_av_desc = slap_schema.si_ad_objectClass;
680 sf.f_av_value = bv_subentry;
681 sf.f_next = scopef.f_next;
686 /* Allocate IDL stack, plus 1 more for former tmp */
687 stack = malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
689 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
695 LDAP_LOG (( "search", LDAP_LEVEL_DETAIL1,"bdb_search_candidates: failed (rc=%d)\n", rc));
697 Debug(LDAP_DEBUG_TRACE,
698 "bdb_search_candidates: failed (rc=%d)\n",
704 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)));
706 Debug(LDAP_DEBUG_TRACE,
707 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
709 (long) BDB_IDL_FIRST(ids),
710 (long) BDB_IDL_LAST(ids) );