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;
66 LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
68 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
72 manageDSAit = get_manageDSAit( op );
74 rc = LOCK_ID (bdb->bi_dbenv, &locker );
79 send_ldap_result( conn, op, rc=LDAP_OTHER,
80 NULL, "internal error", NULL, NULL );
84 if ( nbase->bv_len == 0 ) {
85 /* DIT root special case */
86 e = (Entry *) &slap_entry_root;
90 /* get entry with reader lock */
91 if ( deref & LDAP_DEREF_FINDING ) {
92 e = deref_dn_r( be, nbase-, &err, &matched, &text );
98 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
107 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
109 if (matched != NULL) {
110 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
112 send_ldap_result( conn, op, LDAP_BUSY,
113 NULL, "ldap server busy", NULL, NULL );
114 LOCK_ID_FREE (bdb->bi_dbenv, locker );
116 case DB_LOCK_DEADLOCK:
117 case DB_LOCK_NOTGRANTED:
121 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
123 if (matched != NULL) {
124 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
126 send_ldap_result( conn, op, rc=LDAP_OTHER,
127 NULL, "internal error", NULL, NULL );
128 LOCK_ID_FREE (bdb->bi_dbenv, locker );
133 struct berval matched_dn = { 0, NULL };
134 BerVarray refs = NULL;
136 if ( matched != NULL ) {
138 ber_dupbv( &matched_dn, &matched->e_name );
140 erefs = is_entry_referral( matched )
141 ? get_entry_referrals( be, conn, op, matched )
144 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
148 refs = referral_rewrite( erefs, &matched_dn,
150 ber_bvarray_free( erefs );
154 refs = referral_rewrite( default_referral,
158 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
159 matched_dn.bv_val, text, refs, NULL );
161 LOCK_ID_FREE (bdb->bi_dbenv, locker );
162 if ( refs ) ber_bvarray_free( refs );
163 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
167 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
168 /* entry is a referral, don't allow add */
169 struct berval matched_dn;
170 BerVarray erefs, refs;
172 ber_dupbv( &matched_dn, &e->e_name );
173 erefs = get_entry_referrals( be, conn, op, e );
176 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
180 refs = referral_rewrite( erefs, &matched_dn,
182 ber_bvarray_free( erefs );
186 LDAP_LOG ( OPERATION, RESULTS,
187 "bdb_search: entry is referral\n", 0, 0, 0 );
189 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
193 send_ldap_result( conn, op, LDAP_REFERRAL,
195 refs ? NULL : "bad referral object",
198 LOCK_ID_FREE (bdb->bi_dbenv, locker );
199 ber_bvarray_free( refs );
200 ber_memfree( matched_dn.bv_val );
204 /* if not root, get appropriate limits */
205 if ( be_isroot( be, &op->o_ndn ) ) {
208 ( void ) get_limits( be, &op->o_ndn, &limit );
211 /* The time/size limits come first because they require very little
212 * effort, so there's no chance the candidates are selected and then
213 * the request is not honored only because of time/size constraints */
215 /* if no time limit requested, use soft limit (unless root!) */
218 tlimit = -1; /* allow root to set no limit */
226 /* if no limit is required, use soft limit */
228 tlimit = limit->lms_t_soft;
230 /* if requested limit higher than hard limit, abort */
231 } else if ( tlimit > limit->lms_t_hard ) {
232 /* no hard limit means use soft instead */
233 if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
234 tlimit = limit->lms_t_soft;
236 /* positive hard limit means abort */
237 } else if ( limit->lms_t_hard > 0 ) {
238 send_search_result( conn, op,
239 LDAP_UNWILLING_TO_PERFORM,
240 NULL, NULL, NULL, NULL, 0 );
245 /* negative hard limit means no limit */
248 /* if no limit is required, use soft limit */
250 slimit = limit->lms_s_soft;
252 /* if requested limit higher than hard limit, abort */
253 } else if ( slimit > limit->lms_s_hard ) {
254 /* no hard limit means use soft instead */
255 if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
256 slimit = limit->lms_s_soft;
258 /* positive hard limit means abort */
259 } else if ( limit->lms_s_hard > 0 ) {
260 send_search_result( conn, op,
261 LDAP_UNWILLING_TO_PERFORM,
262 NULL, NULL, NULL, NULL, 0 );
267 /* negative hard limit means no limit */
271 /* compute it anyway; root does not use it */
272 stoptime = op->o_time + tlimit;
274 /* select candidates */
275 if ( scope == LDAP_SCOPE_BASE ) {
276 rc = base_candidate( be, e, candidates );
279 BDB_IDL_ALL( bdb, candidates );
280 rc = search_candidates( be, op, e, filter,
281 scope, deref, candidates );
284 /* need normalized dn below */
285 ber_dupbv( &realbase, &e->e_nname );
287 /* start cursor at base entry's id
288 * FIXME: hack to make "" base work
289 * FIXME: moddn needs to assign new ID for this to work
291 cursor = e->e_id == NOID ? 1 : e->e_id;
293 if ( e != &slap_entry_root ) {
294 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
298 if ( candidates[0] == 0 ) {
300 LDAP_LOG ( OPERATION, RESULTS, "bdb_search: no candidates\n", 0, 0, 0 );
302 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
306 send_search_result( conn, op,
308 NULL, NULL, NULL, NULL, 0 );
314 /* if not root and candidates exceed to-be-checked entries, abort */
315 if ( !isroot && limit->lms_s_unchecked != -1 ) {
316 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
317 send_search_result( conn, op,
318 LDAP_ADMINLIMIT_EXCEEDED,
319 NULL, NULL, NULL, NULL, 0 );
325 for ( id = bdb_idl_first( candidates, &cursor );
327 id = bdb_idl_next( candidates, &cursor ) )
331 /* check for abandon */
332 if ( op->o_abandon ) {
337 /* check time limit */
338 if ( tlimit != -1 && slap_get_time() > stoptime ) {
339 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
340 NULL, NULL, v2refs, NULL, nentries );
345 /* get the entry with reader lock */
346 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
348 if (rc == LDAP_BUSY) {
349 send_ldap_result( conn, op, rc=LDAP_BUSY,
350 NULL, "ldap server busy", NULL, NULL );
353 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
358 if( !BDB_IDL_IS_RANGE(candidates) ) {
359 /* only complain for non-range IDLs */
361 LDAP_LOG ( OPERATION, RESULTS,
362 "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
364 Debug( LDAP_DEBUG_TRACE,
365 "bdb_search: candidate %ld not found\n",
373 #ifdef BDB_SUBENTRIES
374 if ( is_entry_subentry( e ) ) {
375 if( scope != LDAP_SCOPE_BASE ) {
376 if(!get_subentries_visibility( op )) {
377 /* only subentries are visible */
381 } else if ( get_subentries( op ) &&
382 !get_subentries_visibility( op ))
384 /* only subentries are visible */
388 } else if ( get_subentries_visibility( op )) {
389 /* only subentries are visible */
395 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
400 e = deref_entry_r( be, e, &err, &matched, &text );
407 if( e->e_id == id ) {
412 /* need to skip alias which deref into scope */
413 if( scope & LDAP_SCOPE_ONELEVEL ) {
416 dnParent( &e->e_nname, &pdn ):
417 if ( ber_bvcmp( pdn, &realbase ) ) {
421 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
422 /* alias is within scope */
424 LDAP_LOG ( OPERATION, RESULTS,
425 "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
427 Debug( LDAP_DEBUG_TRACE,
428 "bdb_search: \"%s\" in subtree\n",
439 * if it's a referral, add it to the list of referrals. only do
440 * this for non-base searches, and don't check the filter
441 * explicitly here since it's only a candidate anyway.
443 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
444 is_entry_referral( e ) )
449 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
450 if ( !be_issuffix( be, &e->e_nname ) ) {
451 dnParent( &e->e_nname, &dn );
452 scopeok = dn_match( &dn, &realbase );
454 scopeok = (realbase.bv_len == 0);
457 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
458 scopeok = dnIsSuffix( &e->e_nname, &realbase );
465 BerVarray erefs = get_entry_referrals(
467 BerVarray refs = referral_rewrite( erefs,
469 scope == LDAP_SCOPE_SUBTREE
473 send_search_reference( be, conn, op,
474 e, refs, NULL, &v2refs );
476 ber_bvarray_free( refs );
480 LDAP_LOG(OPERATION, DETAIL2,
481 "bdb_search: candidate referral %ld scope not okay\n",
484 Debug( LDAP_DEBUG_TRACE,
485 "bdb_search: candidate referral %ld scope not okay\n",
493 /* if it matches the filter and scope, send it */
494 rc = test_filter( be, conn, op, e, filter );
495 if ( rc == LDAP_COMPARE_TRUE ) {
499 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
500 if ( be_issuffix( be, &e->e_nname ) ) {
501 scopeok = (realbase.bv_len == 0);
503 dnParent( &e->e_nname, &dn );
504 scopeok = dn_match( &dn, &realbase );
507 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
508 scopeok = dnIsSuffix( &e->e_nname, &realbase );
515 /* check size limit */
516 if ( --slimit == -1 ) {
517 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
519 send_search_result( conn, op,
520 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
521 v2refs, NULL, nentries );
531 result = send_search_entry( be, conn, op,
532 e, attrs, attrsonly, NULL);
536 case 0: /* entry sent ok */
539 case 1: /* entry not sent */
541 case -1: /* connection closed */
542 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
550 LDAP_LOG ( OPERATION, RESULTS,
551 "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
553 Debug( LDAP_DEBUG_TRACE,
554 "bdb_search: %ld scope not okay\n",
560 LDAP_LOG ( OPERATION, RESULTS,
561 "bdb_search: %ld does match filter\n", (long) id, 0, 0);
563 Debug( LDAP_DEBUG_TRACE,
564 "bdb_search: %ld does match filter\n",
571 /* free reader lock */
572 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e , &lock);
576 ldap_pvt_thread_yield();
578 send_search_result( conn, op,
579 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
580 NULL, NULL, v2refs, NULL, nentries );
586 /* free reader lock */
587 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
590 LOCK_ID_FREE (bdb->bi_dbenv, locker );
592 if( v2refs ) ber_bvarray_free( v2refs );
593 if( realbase.bv_val ) ch_free( realbase.bv_val );
599 static int base_candidate(
605 LDAP_LOG ( OPERATION, ENTRY,
606 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
608 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
609 e->e_dn, (long) e->e_id, 0);
617 /* Look for "objectClass Present" in this filter.
618 * Also count depth of filter tree while we're at it.
620 static int oc_filter(
628 if( cur > *max ) *max = cur;
630 switch(f->f_choice) {
631 case LDAP_FILTER_PRESENT:
632 if (f->f_desc == slap_schema.si_ad_objectClass)
636 case LDAP_FILTER_AND:
639 for (f=f->f_and; f; f=f->f_next)
640 rc |= oc_filter(f, cur, max);
648 static int search_candidates(
658 Filter f, scopef, rf, xf;
660 AttributeAssertion aa_ref;
661 #ifdef BDB_SUBENTRIES
663 AttributeAssertion aa_subentry;
667 AttributeAssertion aa_alias;
671 * This routine takes as input a filter (user-filter)
672 * and rewrites it as follows:
673 * (&(scope=DN)[(objectClass=subentry)]
674 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
678 LDAP_LOG ( OPERATION, ENTRY,
679 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
680 e->e_dn, (long) e->e_id, scope);
682 Debug(LDAP_DEBUG_TRACE,
683 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
684 e->e_dn, (long) e->e_id, scope );
688 xf.f_choice = LDAP_FILTER_OR;
691 /* If the user's filter uses objectClass=*,
692 * these clauses are redundant.
694 if (!oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
695 if( !get_manageDSAit(op) ) { /* match referrals */
696 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
697 rf.f_choice = LDAP_FILTER_EQUALITY;
699 rf.f_av_desc = slap_schema.si_ad_objectClass;
700 rf.f_av_value = bv_ref;
706 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
707 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
708 af.f_choice = LDAP_FILTER_EQUALITY;
709 af.f_ava = &aa_alias;
710 af.f_av_desc = slap_schema.si_ad_objectClass;
711 af.f_av_value = bv_alias;
716 /* We added one of these clauses, filter depth increased */
717 if( xf.f_or != filter ) depth++;
721 f.f_choice = LDAP_FILTER_AND;
723 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
724 ? SLAPD_FILTER_DN_SUBTREE
725 : SLAPD_FILTER_DN_ONE;
726 scopef.f_dn = &e->e_nname;
727 scopef.f_next = xf.f_or == filter ? filter : &xf ;
728 /* Filter depth increased again, adding scope clause */
731 #ifdef BDB_SUBENTRIES
732 if( get_subentries_visibility( op ) ) {
733 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
734 sf.f_choice = LDAP_FILTER_EQUALITY;
735 sf.f_ava = &aa_subentry;
736 sf.f_av_desc = slap_schema.si_ad_objectClass;
737 sf.f_av_value = bv_subentry;
738 sf.f_next = scopef.f_next;
743 /* Allocate IDL stack, plus 1 more for former tmp */
744 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
746 rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
752 LDAP_LOG ( OPERATION, DETAIL1,
753 "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0 );
755 Debug(LDAP_DEBUG_TRACE,
756 "bdb_search_candidates: failed (rc=%d)\n",
762 LDAP_LOG ( OPERATION, DETAIL1,
763 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
764 (long) ids[0], (long) BDB_IDL_FIRST(ids),
765 (long) BDB_IDL_LAST(ids));
767 Debug(LDAP_DEBUG_TRACE,
768 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
770 (long) BDB_IDL_FIRST(ids),
771 (long) BDB_IDL_LAST(ids) );