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 base entry's id
299 * FIXME: hack to make "" base work
300 * FIXME: moddn needs to assign new ID for this to work
302 cursor = e->e_id == NOID ? 1 : e->e_id;
304 if ( e != &slap_entry_root ) {
305 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
309 if ( candidates[0] == 0 ) {
311 LDAP_LOG ( OPERATION, RESULTS, "bdb_search: no candidates\n", 0, 0, 0 );
313 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
317 send_search_result( conn, op,
319 NULL, NULL, NULL, NULL, 0 );
325 /* if not root and candidates exceed to-be-checked entries, abort */
326 if ( !isroot && limit->lms_s_unchecked != -1 ) {
327 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
328 send_search_result( conn, op,
329 LDAP_ADMINLIMIT_EXCEEDED,
330 NULL, NULL, NULL, NULL, 0 );
336 #ifdef SLAP_X_FILTER_HASSUBORDINATES
338 * is hasSubordinates used in the filter ?
339 * FIXME: we may compute this directly when parsing the filter
341 filter_hasSubordinates = filter_has_subordinates( filter );
342 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
344 #ifdef LDAP_CLIENT_UPDATE
345 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
346 lcupf.f_choice = LDAP_FILTER_AND;
347 lcupf.f_and = &csnfnot;
350 csnfnot.f_choice = LDAP_FILTER_NOT;
351 csnfnot.f_not = &csnfeq;
352 csnfnot.f_next = &csnfand;
354 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
355 csnfeq.f_ava = &aa_eq;
356 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
357 ber_dupbv(&csnfeq.f_av_value, op->o_clientupdate_state);
359 csnfand.f_choice = LDAP_FILTER_AND;
360 csnfand.f_and = &csnfge;
361 csnfand.f_next = NULL;
363 csnfge.f_choice = LDAP_FILTER_GE;
364 csnfge.f_ava = &aa_ge;
365 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
366 ber_dupbv(&csnfge.f_av_value, op->o_clientupdate_state);
367 csnfge.f_next = filter;
369 #endif /* LDAP_CLIENT_UPDATE */
371 for ( id = bdb_idl_first( candidates, &cursor );
373 id = bdb_idl_next( candidates, &cursor ) )
376 #ifdef SLAP_X_FILTER_HASSUBORDINATES
377 Attribute *hasSubordinates = NULL;
378 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
380 /* check for abandon */
381 if ( op->o_abandon ) {
386 /* check time limit */
387 if ( tlimit != -1 && slap_get_time() > stoptime ) {
388 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
389 NULL, NULL, v2refs, NULL, nentries );
394 /* get the entry with reader lock */
395 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
397 if (rc == LDAP_BUSY) {
398 send_ldap_result( conn, op, rc=LDAP_BUSY,
399 NULL, "ldap server busy", NULL, NULL );
402 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
407 if( !BDB_IDL_IS_RANGE(candidates) ) {
408 /* only complain for non-range IDLs */
410 LDAP_LOG ( OPERATION, RESULTS,
411 "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
413 Debug( LDAP_DEBUG_TRACE,
414 "bdb_search: candidate %ld not found\n",
422 #ifdef BDB_SUBENTRIES
423 if ( is_entry_subentry( e ) ) {
424 if( scope != LDAP_SCOPE_BASE ) {
425 if(!get_subentries_visibility( op )) {
426 /* only subentries are visible */
430 } else if ( get_subentries( op ) &&
431 !get_subentries_visibility( op ))
433 /* only subentries are visible */
437 } else if ( get_subentries_visibility( op )) {
438 /* only subentries are visible */
444 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
449 e = deref_entry_r( be, e, &err, &matched, &text );
456 if( e->e_id == id ) {
461 /* need to skip alias which deref into scope */
462 if( scope & LDAP_SCOPE_ONELEVEL ) {
465 dnParent( &e->e_nname, &pdn ):
466 if ( ber_bvcmp( pdn, &realbase ) ) {
470 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
471 /* alias is within scope */
473 LDAP_LOG ( OPERATION, RESULTS,
474 "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
476 Debug( LDAP_DEBUG_TRACE,
477 "bdb_search: \"%s\" in subtree\n",
488 * if it's a referral, add it to the list of referrals. only do
489 * this for non-base searches, and don't check the filter
490 * explicitly here since it's only a candidate anyway.
492 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
493 is_entry_referral( e ) )
498 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
499 if ( !be_issuffix( be, &e->e_nname ) ) {
500 dnParent( &e->e_nname, &dn );
501 scopeok = dn_match( &dn, &realbase );
503 scopeok = (realbase.bv_len == 0);
506 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
507 scopeok = dnIsSuffix( &e->e_nname, &realbase );
514 BerVarray erefs = get_entry_referrals(
516 BerVarray refs = referral_rewrite( erefs,
518 scope == LDAP_SCOPE_SUBTREE
522 send_search_reference( be, conn, op,
523 e, refs, NULL, &v2refs );
525 ber_bvarray_free( refs );
529 LDAP_LOG(OPERATION, DETAIL2,
530 "bdb_search: candidate referral %ld scope not okay\n",
533 Debug( LDAP_DEBUG_TRACE,
534 "bdb_search: candidate referral %ld scope not okay\n",
542 #ifdef SLAP_X_FILTER_HASSUBORDINATES
544 * if hasSubordinates is used in the filter,
545 * append it to the entry's attributes
547 if ( filter_hasSubordinates ) {
550 rc = bdb_hasSubordinates( be, conn, op, e, &hs);
551 if ( rc != LDAP_SUCCESS ) {
555 hasSubordinates = slap_operational_hasSubordinate(
556 hs == LDAP_COMPARE_TRUE );
558 if ( hasSubordinates == NULL ) {
562 hasSubordinates->a_next = e->e_attrs;
563 e->e_attrs = hasSubordinates;
565 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
567 /* if it matches the filter and scope, send it */
568 #ifdef LDAP_CLIENT_UPDATE
569 if ( op->o_clientupdate_type & SLAP_LCUP_SYNC ) {
570 rc = test_filter( be, conn, op, e, &lcupf );
572 #endif /* LDAP_CLIENT_UPDATE */
574 rc = test_filter( be, conn, op, e, filter );
577 #ifdef SLAP_X_FILTER_HASSUBORDINATES
578 if ( hasSubordinates ) {
580 * FIXME: this is fairly inefficient, because
581 * if hasSubordinates is among the required
582 * attrs, it will be added again later;
583 * maybe we should leave it and check
584 * check later if it's already present,
587 e->e_attrs = e->e_attrs->a_next;
588 attr_free( hasSubordinates );
590 #endif /* SLAP_X_FILTER_HASSUBORDINATES */
592 if ( rc == LDAP_COMPARE_TRUE ) {
596 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
597 if ( be_issuffix( be, &e->e_nname ) ) {
598 scopeok = (realbase.bv_len == 0);
600 dnParent( &e->e_nname, &dn );
601 scopeok = dn_match( &dn, &realbase );
604 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
605 scopeok = dnIsSuffix( &e->e_nname, &realbase );
612 /* check size limit */
613 if ( --slimit == -1 ) {
614 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
616 send_search_result( conn, op,
617 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
618 v2refs, NULL, nentries );
625 #if 0 /* noop is masked SLAP_CTRL_UPDATE */
630 result = send_search_entry( be, conn, op,
631 e, attrs, attrsonly, NULL);
638 case 0: /* entry sent ok */
641 case 1: /* entry not sent */
643 case -1: /* connection closed */
644 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
652 LDAP_LOG ( OPERATION, RESULTS,
653 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
655 Debug( LDAP_DEBUG_TRACE,
656 "bdb_search: %ld scope not okay\n",
662 LDAP_LOG ( OPERATION, RESULTS,
663 "bdb_search: %ld does match filter\n", (long) id, 0, 0);
665 Debug( LDAP_DEBUG_TRACE,
666 "bdb_search: %ld does match filter\n",
673 /* free reader lock */
674 bdb_cache_return_entry_r( bdb->bi_dbenv,
675 &bdb->bi_cache, e , &lock);
679 ldap_pvt_thread_yield();
682 send_search_result( conn, op,
683 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
684 NULL, NULL, v2refs, NULL, nentries );
690 /* free reader lock */
691 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
694 LOCK_ID_FREE (bdb->bi_dbenv, locker );
696 if( v2refs ) ber_bvarray_free( v2refs );
697 if( realbase.bv_val ) ch_free( realbase.bv_val );
703 static int base_candidate(
709 LDAP_LOG ( OPERATION, ENTRY,
710 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
712 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
713 e->e_dn, (long) e->e_id, 0);
721 /* Look for "objectClass Present" in this filter.
722 * Also count depth of filter tree while we're at it.
724 static int oc_filter(
732 if( cur > *max ) *max = cur;
734 switch(f->f_choice) {
735 case LDAP_FILTER_PRESENT:
736 if (f->f_desc == slap_schema.si_ad_objectClass)
740 case LDAP_FILTER_AND:
743 for (f=f->f_and; f; f=f->f_next)
744 rc |= oc_filter(f, cur, max);
752 static int search_candidates(
762 Filter f, scopef, rf, xf;
764 AttributeAssertion aa_ref;
765 #ifdef BDB_SUBENTRIES
767 AttributeAssertion aa_subentry;
771 AttributeAssertion aa_alias;
775 * This routine takes as input a filter (user-filter)
776 * and rewrites it as follows:
777 * (&(scope=DN)[(objectClass=subentry)]
778 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
782 LDAP_LOG ( OPERATION, ENTRY,
783 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
784 e->e_dn, (long) e->e_id, scope);
786 Debug(LDAP_DEBUG_TRACE,
787 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
788 e->e_dn, (long) e->e_id, scope );
792 xf.f_choice = LDAP_FILTER_OR;
795 /* If the user's filter uses objectClass=*,
796 * these clauses are redundant.
798 if (!oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
799 if( !get_manageDSAit(op) ) { /* match referrals */
800 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
801 rf.f_choice = LDAP_FILTER_EQUALITY;
803 rf.f_av_desc = slap_schema.si_ad_objectClass;
804 rf.f_av_value = bv_ref;
810 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
811 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
812 af.f_choice = LDAP_FILTER_EQUALITY;
813 af.f_ava = &aa_alias;
814 af.f_av_desc = slap_schema.si_ad_objectClass;
815 af.f_av_value = bv_alias;
820 /* We added one of these clauses, filter depth increased */
821 if( xf.f_or != filter ) depth++;
825 f.f_choice = LDAP_FILTER_AND;
827 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
828 ? SLAPD_FILTER_DN_SUBTREE
829 : SLAPD_FILTER_DN_ONE;
830 scopef.f_dn = &e->e_nname;
831 scopef.f_next = xf.f_or == filter ? filter : &xf ;
832 /* Filter depth increased again, adding scope clause */
835 #ifdef BDB_SUBENTRIES
836 if( get_subentries_visibility( op ) ) {
837 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
838 sf.f_choice = LDAP_FILTER_EQUALITY;
839 sf.f_ava = &aa_subentry;
840 sf.f_av_desc = slap_schema.si_ad_objectClass;
841 sf.f_av_value = bv_subentry;
842 sf.f_next = scopef.f_next;
847 /* Allocate IDL stack, plus 1 more for former tmp */
848 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
850 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
856 LDAP_LOG ( OPERATION, DETAIL1,
857 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
859 Debug(LDAP_DEBUG_TRACE,
860 "bdb_search_candidates: failed (rc=%d)\n",
866 LDAP_LOG ( OPERATION, DETAIL1,
867 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
868 (long) ids[0], (long) BDB_IDL_FIRST(ids),
869 (long) BDB_IDL_LAST(ids));
871 Debug(LDAP_DEBUG_TRACE,
872 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
874 (long) BDB_IDL_FIRST(ids),
875 (long) BDB_IDL_LAST(ids) );