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 #ifdef LDAP_CLIENT_UPDATE
60 Filter lcupf, csnfnot, csnfeq, csnfand, csnfge;
61 AttributeAssertion aa_ge, aa_eq;
64 #endif /* LDAP_CLIENT_UPDATE */
66 struct slap_limits_set *limit = NULL;
69 #ifdef SLAP_X_FILTER_HASSUBORDINATES
70 int filter_hasSubordinates = 0;
71 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
77 LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
79 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
83 manageDSAit = get_manageDSAit( op );
85 rc = LOCK_ID (bdb->bi_dbenv, &locker );
90 send_ldap_result( conn, op, rc=LDAP_OTHER,
91 NULL, "internal error", NULL, NULL );
95 if ( nbase->bv_len == 0 ) {
96 /* DIT root special case */
97 e = (Entry *) &slap_entry_root;
101 /* get entry with reader lock */
102 if ( deref & LDAP_DEREF_FINDING ) {
103 e = deref_dn_r( be, nbase-, &err, &matched, &text );
109 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
118 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
120 if (matched != NULL) {
121 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
123 send_ldap_result( conn, op, LDAP_BUSY,
124 NULL, "ldap server busy", NULL, NULL );
125 LOCK_ID_FREE (bdb->bi_dbenv, locker );
127 case DB_LOCK_DEADLOCK:
128 case DB_LOCK_NOTGRANTED:
132 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
134 if (matched != NULL) {
135 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
137 send_ldap_result( conn, op, rc=LDAP_OTHER,
138 NULL, "internal error", NULL, NULL );
139 LOCK_ID_FREE (bdb->bi_dbenv, locker );
144 struct berval matched_dn = { 0, NULL };
145 BerVarray refs = NULL;
147 if ( matched != NULL ) {
149 ber_dupbv( &matched_dn, &matched->e_name );
151 erefs = is_entry_referral( matched )
152 ? get_entry_referrals( be, conn, op, matched )
155 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
159 refs = referral_rewrite( erefs, &matched_dn,
161 ber_bvarray_free( erefs );
165 refs = referral_rewrite( default_referral,
169 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
170 matched_dn.bv_val, text, refs, NULL );
172 LOCK_ID_FREE (bdb->bi_dbenv, locker );
173 if ( refs ) ber_bvarray_free( refs );
174 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
178 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
179 /* entry is a referral, don't allow add */
180 struct berval matched_dn;
181 BerVarray erefs, refs;
183 ber_dupbv( &matched_dn, &e->e_name );
184 erefs = get_entry_referrals( be, conn, op, e );
187 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
191 refs = referral_rewrite( erefs, &matched_dn,
193 ber_bvarray_free( erefs );
197 LDAP_LOG ( OPERATION, RESULTS,
198 "bdb_search: entry is referral\n", 0, 0, 0 );
200 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
204 send_ldap_result( conn, op, LDAP_REFERRAL,
206 refs ? NULL : "bad referral object",
209 LOCK_ID_FREE (bdb->bi_dbenv, locker );
210 ber_bvarray_free( refs );
211 ber_memfree( matched_dn.bv_val );
215 /* if not root, get appropriate limits */
216 if ( be_isroot( be, &op->o_ndn ) ) {
219 ( void ) get_limits( be, &op->o_ndn, &limit );
222 /* The time/size limits come first because they require very little
223 * effort, so there's no chance the candidates are selected and then
224 * the request is not honored only because of time/size constraints */
226 /* if no time limit requested, use soft limit (unless root!) */
229 tlimit = -1; /* allow root to set no limit */
237 /* if no limit is required, use soft limit */
239 tlimit = limit->lms_t_soft;
241 /* if requested limit higher than hard limit, abort */
242 } else if ( tlimit > limit->lms_t_hard ) {
243 /* no hard limit means use soft instead */
244 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
245 tlimit = limit->lms_t_soft;
247 /* positive hard limit means abort */
248 } else if ( limit->lms_t_hard > 0 ) {
249 send_search_result( conn, op,
250 LDAP_UNWILLING_TO_PERFORM,
251 NULL, NULL, NULL, NULL, 0 );
256 /* negative hard limit means no limit */
259 /* if no limit is required, use soft limit */
261 slimit = limit->lms_s_soft;
263 /* if requested limit higher than hard limit, abort */
264 } else if ( slimit > limit->lms_s_hard ) {
265 /* no hard limit means use soft instead */
266 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
267 slimit = limit->lms_s_soft;
269 /* positive hard limit means abort */
270 } else if ( limit->lms_s_hard > 0 ) {
271 send_search_result( conn, op,
272 LDAP_UNWILLING_TO_PERFORM,
273 NULL, NULL, NULL, NULL, 0 );
278 /* negative hard limit means no limit */
282 /* compute it anyway; root does not use it */
283 stoptime = op->o_time + tlimit;
285 /* select candidates */
286 if ( scope == LDAP_SCOPE_BASE ) {
287 rc = base_candidate( be, e, candidates );
290 BDB_IDL_ALL( bdb, candidates );
291 rc = search_candidates( be, op, e, filter,
292 scope, deref, candidates );
295 /* need normalized dn below */
296 ber_dupbv( &realbase, &e->e_nname );
298 /* start cursor at beginning of candidates.
302 if ( e != &slap_entry_root ) {
303 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
307 if ( candidates[0] == 0 ) {
309 LDAP_LOG ( OPERATION, RESULTS, "bdb_search: no candidates\n", 0, 0, 0 );
311 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
315 send_search_result( conn, op,
317 NULL, NULL, NULL, NULL, 0 );
323 /* if not root and candidates exceed to-be-checked entries, abort */
324 if ( !isroot && limit->lms_s_unchecked != -1 ) {
325 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
326 send_search_result( conn, op,
327 LDAP_ADMINLIMIT_EXCEEDED,
328 NULL, NULL, NULL, NULL, 0 );
334 #ifdef SLAP_X_FILTER_HASSUBORDINATES
336 * is hasSubordinates used in the filter ?
337 * FIXME: we may compute this directly when parsing the filter
339 filter_hasSubordinates = filter_has_subordinates( filter );
340 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
342 #ifdef LDAP_CLIENT_UPDATE
343 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
344 lcupf.f_choice = LDAP_FILTER_AND;
345 lcupf.f_and = &csnfnot;
348 csnfnot.f_choice = LDAP_FILTER_NOT;
349 csnfnot.f_not = &csnfeq;
350 csnfnot.f_next = &csnfand;
352 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
353 csnfeq.f_ava = &aa_eq;
354 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
355 ber_dupbv(&csnfeq.f_av_value, op->o_clientupdate_state);
357 csnfand.f_choice = LDAP_FILTER_AND;
358 csnfand.f_and = &csnfge;
359 csnfand.f_next = NULL;
361 csnfge.f_choice = LDAP_FILTER_GE;
362 csnfge.f_ava = &aa_ge;
363 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
364 ber_dupbv(&csnfge.f_av_value, op->o_clientupdate_state);
365 csnfge.f_next = filter;
367 #endif /* LDAP_CLIENT_UPDATE */
369 for ( id = bdb_idl_first( candidates, &cursor );
371 id = bdb_idl_next( candidates, &cursor ) )
374 #ifdef SLAP_X_FILTER_HASSUBORDINATES
375 Attribute *hasSubordinates = NULL;
376 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
378 /* check for abandon */
379 if ( op->o_abandon ) {
384 /* check time limit */
385 if ( tlimit != -1 && slap_get_time() > stoptime ) {
386 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
387 NULL, NULL, v2refs, NULL, nentries );
392 /* get the entry with reader lock */
393 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
395 if (rc == LDAP_BUSY) {
396 send_ldap_result( conn, op, rc=LDAP_BUSY,
397 NULL, "ldap server busy", NULL, NULL );
400 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
405 if( !BDB_IDL_IS_RANGE(candidates) ) {
406 /* only complain for non-range IDLs */
408 LDAP_LOG ( OPERATION, RESULTS,
409 "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
411 Debug( LDAP_DEBUG_TRACE,
412 "bdb_search: candidate %ld not found\n",
420 #ifdef BDB_SUBENTRIES
421 if ( is_entry_subentry( e ) ) {
422 if( scope != LDAP_SCOPE_BASE ) {
423 if(!get_subentries_visibility( op )) {
424 /* only subentries are visible */
428 } else if ( get_subentries( op ) &&
429 !get_subentries_visibility( op ))
431 /* only subentries are visible */
435 } else if ( get_subentries_visibility( op )) {
436 /* only subentries are visible */
442 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
447 e = deref_entry_r( be, e, &err, &matched, &text );
454 if( e->e_id == id ) {
459 /* need to skip alias which deref into scope */
460 if( scope & LDAP_SCOPE_ONELEVEL ) {
463 dnParent( &e->e_nname, &pdn ):
464 if ( ber_bvcmp( pdn, &realbase ) ) {
468 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
469 /* alias is within scope */
471 LDAP_LOG ( OPERATION, RESULTS,
472 "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
474 Debug( LDAP_DEBUG_TRACE,
475 "bdb_search: \"%s\" in subtree\n",
486 * if it's a referral, add it to the list of referrals. only do
487 * this for non-base searches, and don't check the filter
488 * explicitly here since it's only a candidate anyway.
490 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
491 is_entry_referral( e ) )
496 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
497 if ( !be_issuffix( be, &e->e_nname ) ) {
498 dnParent( &e->e_nname, &dn );
499 scopeok = dn_match( &dn, &realbase );
501 scopeok = (realbase.bv_len == 0);
504 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
505 scopeok = dnIsSuffix( &e->e_nname, &realbase );
512 BerVarray erefs = get_entry_referrals(
514 BerVarray refs = referral_rewrite( erefs,
516 scope == LDAP_SCOPE_SUBTREE
520 send_search_reference( be, conn, op,
521 e, refs, NULL, &v2refs );
523 ber_bvarray_free( refs );
527 LDAP_LOG(OPERATION, DETAIL2,
528 "bdb_search: candidate referral %ld scope not okay\n",
531 Debug( LDAP_DEBUG_TRACE,
532 "bdb_search: candidate referral %ld scope not okay\n",
540 #ifdef SLAP_X_FILTER_HASSUBORDINATES
542 * if hasSubordinates is used in the filter,
543 * append it to the entry's attributes
545 if ( filter_hasSubordinates ) {
548 rc = bdb_hasSubordinates( be, conn, op, e, &hs);
549 if ( rc != LDAP_SUCCESS ) {
553 hasSubordinates = slap_operational_hasSubordinate(
554 hs == LDAP_COMPARE_TRUE );
556 if ( hasSubordinates == NULL ) {
560 hasSubordinates->a_next = e->e_attrs;
561 e->e_attrs = hasSubordinates;
563 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
565 /* if it matches the filter and scope, send it */
566 #ifdef LDAP_CLIENT_UPDATE
567 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
568 rc = test_filter( be, conn, op, e, &lcupf );
570 #endif /* LDAP_CLIENT_UPDATE */
572 rc = test_filter( be, conn, op, e, filter );
575 #ifdef SLAP_X_FILTER_HASSUBORDINATES
576 if ( hasSubordinates ) {
578 * FIXME: this is fairly inefficient, because
579 * if hasSubordinates is among the required
580 * attrs, it will be added again later;
581 * maybe we should leave it and check
582 * check later if it's already present,
585 e->e_attrs = e->e_attrs->a_next;
586 attr_free( hasSubordinates );
588 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
590 if ( rc == LDAP_COMPARE_TRUE ) {
594 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
595 if ( be_issuffix( be, &e->e_nname ) ) {
596 scopeok = (realbase.bv_len == 0);
598 dnParent( &e->e_nname, &dn );
599 scopeok = dn_match( &dn, &realbase );
602 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
603 scopeok = dnIsSuffix( &e->e_nname, &realbase );
610 /* check size limit */
611 if ( --slimit == -1 ) {
612 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
614 send_search_result( conn, op,
615 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
616 v2refs, NULL, nentries );
623 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
628 result = send_search_entry( be, conn, op,
629 e, attrs, attrsonly, NULL);
636 case 0: /* entry sent ok */
639 case 1: /* entry not sent */
641 case -1: /* connection closed */
642 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
650 LDAP_LOG ( OPERATION, RESULTS,
651 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
653 Debug( LDAP_DEBUG_TRACE,
654 "bdb_search: %ld scope not okay\n",
660 LDAP_LOG ( OPERATION, RESULTS,
661 "bdb_search: %ld does match filter\n", (long) id, 0, 0);
663 Debug( LDAP_DEBUG_TRACE,
664 "bdb_search: %ld does match filter\n",
671 /* free reader lock */
672 bdb_cache_return_entry_r( bdb->bi_dbenv,
673 &bdb->bi_cache, e , &lock);
677 ldap_pvt_thread_yield();
680 send_search_result( conn, op,
681 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
682 NULL, NULL, v2refs, NULL, nentries );
688 /* free reader lock */
689 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
692 LOCK_ID_FREE (bdb->bi_dbenv, locker );
694 if( v2refs ) ber_bvarray_free( v2refs );
695 if( realbase.bv_val ) ch_free( realbase.bv_val );
701 static int base_candidate(
707 LDAP_LOG ( OPERATION, ENTRY,
708 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
710 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
711 e->e_dn, (long) e->e_id, 0);
719 /* Look for "objectClass Present" in this filter.
720 * Also count depth of filter tree while we're at it.
722 static int oc_filter(
730 if( cur > *max ) *max = cur;
732 switch(f->f_choice) {
733 case LDAP_FILTER_PRESENT:
734 if (f->f_desc == slap_schema.si_ad_objectClass)
738 case LDAP_FILTER_AND:
741 for (f=f->f_and; f; f=f->f_next)
742 rc |= oc_filter(f, cur, max);
750 static int search_candidates(
760 Filter f, scopef, rf, xf;
762 AttributeAssertion aa_ref;
763 #ifdef BDB_SUBENTRIES
765 AttributeAssertion aa_subentry;
769 AttributeAssertion aa_alias;
773 * This routine takes as input a filter (user-filter)
774 * and rewrites it as follows:
775 * (&(scope=DN)[(objectClass=subentry)]
776 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
780 LDAP_LOG ( OPERATION, ENTRY,
781 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
782 e->e_dn, (long) e->e_id, scope);
784 Debug(LDAP_DEBUG_TRACE,
785 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
786 e->e_dn, (long) e->e_id, scope );
790 xf.f_choice = LDAP_FILTER_OR;
793 /* If the user's filter uses objectClass=*,
794 * these clauses are redundant.
796 if (!oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
797 if( !get_manageDSAit(op) ) { /* match referrals */
798 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
799 rf.f_choice = LDAP_FILTER_EQUALITY;
801 rf.f_av_desc = slap_schema.si_ad_objectClass;
802 rf.f_av_value = bv_ref;
808 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
809 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
810 af.f_choice = LDAP_FILTER_EQUALITY;
811 af.f_ava = &aa_alias;
812 af.f_av_desc = slap_schema.si_ad_objectClass;
813 af.f_av_value = bv_alias;
818 /* We added one of these clauses, filter depth increased */
819 if( xf.f_or != filter ) depth++;
823 f.f_choice = LDAP_FILTER_AND;
825 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
826 ? SLAPD_FILTER_DN_SUBTREE
827 : SLAPD_FILTER_DN_ONE;
828 scopef.f_dn = &e->e_nname;
829 scopef.f_next = xf.f_or == filter ? filter : &xf ;
830 /* Filter depth increased again, adding scope clause */
833 #ifdef BDB_SUBENTRIES
834 if( get_subentries_visibility( op ) ) {
835 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
836 sf.f_choice = LDAP_FILTER_EQUALITY;
837 sf.f_ava = &aa_subentry;
838 sf.f_av_desc = slap_schema.si_ad_objectClass;
839 sf.f_av_value = bv_subentry;
840 sf.f_next = scopef.f_next;
845 /* Allocate IDL stack, plus 1 more for former tmp */
846 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
848 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
854 LDAP_LOG ( OPERATION, DETAIL1,
855 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
857 Debug(LDAP_DEBUG_TRACE,
858 "bdb_search_candidates: failed (rc=%d)\n",
864 LDAP_LOG ( OPERATION, DETAIL1,
865 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
866 (long) ids[0], (long) BDB_IDL_FIRST(ids),
867 (long) BDB_IDL_LAST(ids));
869 Debug(LDAP_DEBUG_TRACE,
870 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
872 (long) BDB_IDL_FIRST(ids),
873 (long) BDB_IDL_LAST(ids) );