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;
63 Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
66 manageDSAit = get_manageDSAit( op );
68 if ( nbase->bv_len == 0 ) {
69 /* DIT root special case */
70 e = (Entry *) &slap_entry_root;
74 /* get entry with reader lock */
75 if ( deref & LDAP_DEREF_FINDING ) {
76 e = deref_dn_r( be, nbase-, &err, &matched, &text );
81 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0 );
90 bdb_cache_return_entry_w(&bdb->bi_cache, e);
92 if (matched != NULL) {
93 bdb_cache_return_entry_r(&bdb->bi_cache, matched);
95 send_ldap_result( conn, op, rc=LDAP_OTHER,
96 NULL, "internal error", NULL, NULL );
101 struct berval matched_dn = { 0, NULL };
102 BerVarray refs = NULL;
104 if ( matched != NULL ) {
107 ber_dupbv( &matched_dn, &matched->e_name );
109 erefs = is_entry_referral( matched )
110 ? get_entry_referrals( be, conn, op, matched )
113 bdb_cache_return_entry_r (&bdb->bi_cache, matched);
117 refs = referral_rewrite( erefs, &matched_dn,
119 ber_bvarray_free( erefs );
123 refs = referral_rewrite( default_referral,
127 send_ldap_result( conn, op, rc=LDAP_REFERRAL ,
128 matched_dn.bv_val, text, refs, NULL );
130 if ( refs ) ber_bvarray_free( refs );
131 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
135 if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
136 /* entry is a referral, don't allow add */
137 struct berval matched_dn;
138 BerVarray erefs, refs;
140 ber_dupbv( &matched_dn, &e->e_name );
141 erefs = get_entry_referrals( be, conn, op, e );
144 bdb_cache_return_entry_r( &bdb->bi_cache, e );
148 refs = referral_rewrite( erefs, &matched_dn,
150 ber_bvarray_free( erefs );
153 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
156 send_ldap_result( conn, op, LDAP_REFERRAL,
158 refs ? NULL : "bad referral object",
161 ber_bvarray_free( refs );
162 ber_memfree( matched_dn.bv_val );
166 /* if not root, get appropriate limits */
167 if ( be_isroot( be, &op->o_ndn ) ) {
170 ( void ) get_limits( be, &op->o_ndn, &limit );
173 /* The time/size limits come first because they require very little
174 * effort, so there's no chance the candidates are selected and then
175 * the request is not honored only because of time/size constraints */
177 /* if no time limit requested, use soft limit (unless root!) */
180 tlimit = -1; /* allow root to set no limit */
188 /* if no limit is required, use soft limit */
190 tlimit = limit->lms_t_soft;
192 /* if requested limit higher than hard limit, abort */
193 } else if ( tlimit > limit->lms_t_hard ) {
194 /* no hard limit means use soft instead */
195 if ( limit->lms_t_hard == 0 ) {
196 tlimit = limit->lms_t_soft;
198 /* positive hard limit means abort */
199 } else if ( limit->lms_t_hard > 0 ) {
200 send_search_result( conn, op,
201 LDAP_UNWILLING_TO_PERFORM,
202 NULL, NULL, NULL, NULL, 0 );
207 /* negative hard limit means no limit */
210 /* if no limit is required, use soft limit */
212 slimit = limit->lms_s_soft;
214 /* if requested limit higher than hard limit, abort */
215 } else if ( slimit > limit->lms_s_hard ) {
216 /* no hard limit means use soft instead */
217 if ( limit->lms_s_hard == 0 ) {
218 slimit = limit->lms_s_soft;
220 /* positive hard limit means abort */
221 } else if ( limit->lms_s_hard > 0 ) {
222 send_search_result( conn, op,
223 LDAP_UNWILLING_TO_PERFORM,
224 NULL, NULL, NULL, NULL, 0 );
229 /* negative hard limit means no limit */
233 /* compute it anyway; root does not use it */
234 stoptime = op->o_time + tlimit;
236 /* select candidates */
237 if ( scope == LDAP_SCOPE_BASE ) {
238 rc = base_candidate( be, e, candidates );
241 BDB_IDL_ALL( bdb, candidates );
242 rc = search_candidates( be, op, e, filter,
243 scope, deref, candidates );
246 /* need normalized dn below */
247 ber_dupbv( &realbase, &e->e_nname );
249 /* start cursor at base entry's id
250 * FIXME: hack to make "" base work */
251 cursor = e->e_id == NOID ? 1 : e->e_id;
253 if ( e != &slap_entry_root ) {
254 bdb_cache_return_entry_r(&bdb->bi_cache, e);
258 if ( candidates[0] == 0 ) {
259 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
262 send_search_result( conn, op,
264 NULL, NULL, NULL, NULL, 0 );
270 /* if not root and candidates exceed to-be-checked entries, abort */
271 if ( !isroot && limit->lms_s_unchecked != -1 ) {
272 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
273 send_search_result( conn, op,
274 LDAP_UNWILLING_TO_PERFORM,
275 NULL, NULL, NULL, NULL, 0 );
281 for ( id = bdb_idl_first( candidates, &cursor );
283 id = bdb_idl_next( candidates, &cursor ) )
287 /* check for abandon */
288 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
289 abandon = op->o_abandon;
290 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
297 /* check time limit */
298 if ( tlimit != -1 && slap_get_time() > stoptime ) {
299 send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
300 NULL, NULL, v2refs, NULL, nentries );
304 /* get the entry with reader lock */
305 rc = bdb_id2entry_r( be, NULL, id, &e );
308 if( !BDB_IDL_IS_RANGE(candidates) ) {
309 /* only complain for non-range IDLs */
310 Debug( LDAP_DEBUG_TRACE,
311 "bdb_search: candidate %ld not found\n",
318 if ( is_entry_subentry( e ) ) {
319 if( scope != LDAP_SCOPE_BASE ) {
320 if(!get_subentries_visibility( op )) {
321 /* only subentries are visible */
324 } else if ( get_subentries( op ) &&
325 !get_subentries_visibility( op ))
327 /* only subentries are visible */
330 } else if ( get_subentries_visibility( op )) {
331 /* only subentries are visible */
336 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
341 e = deref_entry_r( be, e, &err, &matched, &text );
348 if( e->e_id == id ) {
353 /* need to skip alias which deref into scope */
354 if( scope & LDAP_SCOPE_ONELEVEL ) {
358 if ( dnParent( e->e_nname.bv_val, &pdn ) == LDAP_SUCCESS ) {
359 plen = e->e_nname.bv_len - ( pdn - e->e_nname.bv_val );
360 if ( plen != realbase.bv_len || strcmp( pdn, realbase.bv_val ) ) {
365 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
366 /* alias is within scope */
367 Debug( LDAP_DEBUG_TRACE,
368 "bdb_search: \"%s\" in subtree\n",
378 * if it's a referral, add it to the list of referrals. only do
379 * this for non-base searches, and don't check the filter
380 * explicitly here since it's only a candidate anyway.
382 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
383 is_entry_referral( e ) )
385 BerVarray erefs = get_entry_referrals(
387 BerVarray refs = referral_rewrite( erefs,
389 scope == LDAP_SCOPE_SUBTREE
393 send_search_reference( be, conn, op,
394 e, refs, NULL, &v2refs );
396 ber_bvarray_free( refs );
401 /* if it matches the filter and scope, send it */
402 rc = test_filter( be, conn, op, e, filter );
403 if ( rc == LDAP_COMPARE_TRUE ) {
407 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
408 if ( be_issuffix( be, e->e_nname.bv_val ) ) {
409 scopeok = (realbase.bv_len == 0);
411 dnParent( e->e_nname.bv_val, (const char **)&dn );
412 scopeok = (dn == realbase.bv_val)
414 : (strcmp( dn, realbase.bv_val ) ? 0 : 1 );
417 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
418 scopeok = dnIsSuffix( &e->e_nname, &realbase );
425 /* check size limit */
426 if ( --slimit == -1 ) {
427 bdb_cache_return_entry_r (&bdb->bi_cache, e);
429 send_search_result( conn, op,
430 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
431 v2refs, NULL, nentries );
436 int result = send_search_entry( be, conn, op,
437 e, attrs, attrsonly, NULL);
440 case 0: /* entry sent ok */
443 case 1: /* entry not sent */
445 case -1: /* connection closed */
446 bdb_cache_return_entry_r(&bdb->bi_cache, e);
453 Debug( LDAP_DEBUG_TRACE,
454 "bdb_search: %ld scope not okay\n",
458 Debug( LDAP_DEBUG_TRACE,
459 "bdb_search: %ld does match filter\n",
465 /* free reader lock */
466 bdb_cache_return_entry_r ( &bdb->bi_cache, e );
470 ldap_pvt_thread_yield();
472 send_search_result( conn, op,
473 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
474 NULL, NULL, v2refs, NULL, nentries );
480 /* free reader lock */
481 bdb_cache_return_entry_r ( &bdb->bi_cache, e );
484 if( v2refs ) ber_bvarray_free( v2refs );
485 if( realbase.bv_val ) ch_free( realbase.bv_val );
491 static int base_candidate(
496 Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
497 e->e_dn, (long) e->e_id, 0);
504 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
505 * doesn't count, we're looking for explicit values.
507 static int oc_filter(
513 switch(f->f_choice) {
514 case LDAP_FILTER_EQUALITY:
515 case LDAP_FILTER_APPROX:
516 if (f->f_av_desc == slap_schema.si_ad_objectClass)
520 case LDAP_FILTER_SUBSTRINGS:
521 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
525 case LDAP_FILTER_AND:
527 for (f=f->f_and; f; f=f->f_next)
528 if ((rc = oc_filter(f)))
537 static int search_candidates(
547 Filter f, scopef, sf, rf, xf;
548 ID tmp[BDB_IDL_UM_SIZE];
549 AttributeAssertion aa_ref;
550 AttributeAssertion aa_subentry;
551 struct bdb_info *bdb = (struct bdb_info *) be->be_private;
554 AttributeAssertion aa_alias;
558 * This routine takes as input a filter (user-filter)
559 * and rewrites it as follows:
560 * (&(scope=DN)[(objectClass=subentry)]
561 * (|[(objectClass=referral)(objectClass=alias)](user-filter))
564 Debug(LDAP_DEBUG_TRACE,
565 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
566 e->e_dn, (long) e->e_id, scope );
569 xf.f_choice = LDAP_FILTER_OR;
572 /* If the user's filter doesn't mention objectClass, or if
573 * it just uses objectClass=*, these clauses are redundant.
575 if (oc_filter(filter) && !get_subentries_visibility(op) ) {
576 if( !get_manageDSAit(op) ) { /* match referrals */
577 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
578 rf.f_choice = LDAP_FILTER_EQUALITY;
580 rf.f_av_desc = slap_schema.si_ad_objectClass;
581 rf.f_av_value = bv_ref;
587 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
588 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
589 af.f_choice = LDAP_FILTER_EQUALITY;
590 af.f_ava = &aa_alias;
591 af.f_av_desc = slap_schema.si_ad_objectClass;
592 af.f_av_value = bv_alias;
600 f.f_choice = LDAP_FILTER_AND;
602 scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
603 ? SLAPD_FILTER_DN_SUBTREE
604 : SLAPD_FILTER_DN_ONE;
605 scopef.f_dn = &e->e_nname;
606 scopef.f_next = xf.f_or == filter ? filter : &xf ;
608 if( get_subentries_visibility( op ) ) {
609 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
610 sf.f_choice = LDAP_FILTER_EQUALITY;
611 sf.f_ava = &aa_subentry;
612 sf.f_av_desc = slap_schema.si_ad_objectClass;
613 sf.f_av_value = bv_subentry;
614 sf.f_next = scopef.f_next;
618 #ifdef BDB_FILTER_INDICES
619 rc = bdb_filter_candidates( be, &f, ids, tmp );
621 /* FIXME: Original code:
622 BDB_IDL_ID( bdb, ids, e->e_id );
623 * this is a hack to make "" base work; when bdb_filter_candidates
624 * is used this should not be needed any more */
625 BDB_IDL_ID( bdb, ids, (e->e_id == NOID ? 1 : e->e_id) );
629 Debug(LDAP_DEBUG_TRACE,
630 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
632 (long) BDB_IDL_FIRST(ids),
633 (long) BDB_IDL_LAST(ids) );