1 /* search.c - ldbm backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/string.h>
22 #include <ac/socket.h>
25 #include "back-ldbm.h"
26 #include "proto-back-ldbm.h"
28 static ID_BLOCK *base_candidate(
29 Backend *be, Entry *e );
31 static ID_BLOCK *search_candidates(
32 Operation *op, Entry *e, Filter *filter,
33 int scope, int deref, int manageDSAit );
41 struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
47 Entry *matched = NULL;
48 struct berval realbase = BER_BVNULL;
49 int manageDSAit = get_manageDSAit( op );
51 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
53 /* grab giant lock for reading */
54 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
56 if ( op->o_req_ndn.bv_len == 0 ) {
57 /* DIT root special case */
58 e = (Entry *) &slap_entry_root;
60 /* need normalized dn below */
61 ber_dupbv( &realbase, &e->e_nname );
63 candidates = search_candidates( op, e, op->ors_filter,
64 op->ors_scope, op->ors_deref,
65 manageDSAit || get_domainScope(op) );
69 } else if ( op->ors_deref & LDAP_DEREF_FINDING ) {
70 /* deref dn and get entry with reader lock */
71 e = deref_dn_r( op->o_bd, &op->o_req_ndn,
72 &rs->sr_err, &matched, &rs->sr_text );
74 if( rs->sr_err == LDAP_NO_SUCH_OBJECT ) rs->sr_err = LDAP_REFERRAL;
77 /* get entry with reader lock */
78 e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched );
79 rs->sr_err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
84 struct berval matched_dn = BER_BVNULL;
86 if ( matched != NULL ) {
87 BerVarray erefs = NULL;
89 #ifdef SLAP_ACL_HONOR_DISCLOSE
90 if ( ! access_allowed( op, matched,
91 slap_schema.si_ad_entry,
92 NULL, ACL_DISCLOSE, NULL ) )
94 rs->sr_err = LDAP_NO_SUCH_OBJECT;
97 #endif /* SLAP_ACL_HONOR_DISCLOSE */
99 ber_dupbv( &matched_dn, &matched->e_name );
101 erefs = is_entry_referral( matched )
102 ? get_entry_referrals( op, matched )
106 cache_return_entry_r( &li->li_cache, matched );
109 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
110 &op->o_req_dn, op->ors_scope );
112 ber_bvarray_free( erefs );
116 rs->sr_ref = referral_rewrite( default_referral,
117 NULL, &op->o_req_dn, op->ors_scope );
120 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
122 rs->sr_matched = matched_dn.bv_val;
123 send_ldap_result( op, rs );
125 ber_bvarray_free( rs->sr_ref );
126 ber_memfree( matched_dn.bv_val );
128 rs->sr_matched = NULL;
132 #ifdef SLAP_ACL_HONOR_DISCLOSE
133 if ( ! access_allowed( op, e, slap_schema.si_ad_entry,
134 NULL, ACL_DISCLOSE, NULL ) )
136 rs->sr_err = LDAP_NO_SUCH_OBJECT;
138 cache_return_entry_r( &li->li_cache, e );
139 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
141 send_ldap_result( op, rs );
144 #endif /* SLAP_ACL_HONOR_DISCLOSE */
146 if ( !manageDSAit && is_entry_referral( e ) ) {
147 /* entry is a referral, don't allow add */
148 struct berval matched_dn = BER_BVNULL;
149 BerVarray erefs = NULL;
152 rs->sr_err = LDAP_OTHER;
153 rs->sr_text = "bad referral object";
155 ber_dupbv( &matched_dn, &e->e_name );
156 erefs = get_entry_referrals( op, e );
158 cache_return_entry_r( &li->li_cache, e );
159 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
161 Debug( LDAP_DEBUG_TRACE,
162 "ldbm_search: entry is referral\n",
166 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
167 &op->o_req_dn, op->ors_scope );
169 ber_bvarray_free( erefs );
172 rs->sr_err = LDAP_REFERRAL;
177 rs->sr_matched = matched_dn.bv_val;
178 send_ldap_result( op, rs );
179 ber_bvarray_free( rs->sr_ref );
180 ber_memfree( matched_dn.bv_val );
182 rs->sr_matched = NULL;
186 if ( is_entry_alias( e ) ) {
188 op->ors_deref = LDAP_DEREF_NEVER;
191 if ( op->ors_scope == LDAP_SCOPE_BASE ) {
192 candidates = base_candidate( op->o_bd, e );
195 candidates = search_candidates( op, e, op->ors_filter,
196 op->ors_scope, op->ors_deref, manageDSAit );
199 /* need normalized dn below */
200 ber_dupbv( &realbase, &e->e_nname );
202 cache_return_entry_r( &li->li_cache, e );
205 if ( candidates == NULL ) {
207 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
210 rs->sr_err = LDAP_SUCCESS;
211 send_ldap_result( op, rs );
217 /* if candidates exceed to-be-checked entries, abort */
218 if ( op->ors_limit /* isroot == FALSE */
219 && op->ors_limit->lms_s_unchecked != -1
220 && ID_BLOCK_NIDS( candidates ) > (unsigned) op->ors_limit->lms_s_unchecked )
222 send_ldap_error( op, rs, LDAP_ADMINLIMIT_EXCEEDED, NULL );
227 /* compute it anyway; root does not use it */
228 stoptime = op->o_time + op->ors_tlimit;
229 rs->sr_attrs = op->ors_attrs;
231 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
232 id = idl_nextid( candidates, &cursor ) )
237 /* check for abandon */
238 if ( op->o_abandon ) {
243 /* check time limit */
244 if ( op->ors_tlimit != SLAP_NO_LIMIT
245 && slap_get_time() > stoptime )
247 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
248 send_ldap_result( op, rs );
253 /* get the entry with reader lock */
254 e = id2entry_r( op->o_bd, id );
257 Debug( LDAP_DEBUG_TRACE,
258 "ldbm_search: candidate %ld not found\n",
266 #ifdef LDBM_SUBENTRIES
267 if ( is_entry_subentry( e ) ) {
268 if( op->ors_scope != LDAP_SCOPE_BASE ) {
269 if(!get_subentries_visibility( op )) {
270 /* only subentries are visible */
273 } else if ( get_subentries( op ) &&
274 !get_subentries_visibility( op ))
276 /* only subentries are visible */
279 } else if ( get_subentries_visibility( op )) {
280 /* only subentries are visible */
285 if ( op->ors_deref & LDAP_DEREF_SEARCHING &&
286 is_entry_alias( e ) )
292 e = deref_entry_r( op->o_bd, e, &err, &matched, &text );
299 if( e->e_id == id ) {
304 /* need to skip alias which deref into scope */
305 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
307 dnParent( &e->e_nname, &pdn );
308 if ( ber_bvcmp( &pdn, &realbase ) ) {
312 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
313 /* alias is within scope */
314 Debug( LDAP_DEBUG_TRACE,
315 "ldbm_search: alias \"%s\" in subtree\n",
327 * If it's a referral, add it to the list of referrals.
328 * Only do this for non-base searches, and don't check
329 * the filter explicitly here since it's only a candidate
332 if ( !manageDSAit && op->ors_scope != LDAP_SCOPE_BASE &&
333 is_entry_referral( e ) )
338 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
339 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
340 dnParent( &e->e_nname, &dn );
341 scopeok = dn_match( &dn, &realbase );
343 scopeok = (realbase.bv_len == 0);
347 && op->ors_scope == LDAP_SCOPE_SUBTREE )
349 scopeok = dnIsSuffix( &e->e_nname, &realbase );
351 #ifdef LDAP_SCOPE_SUBORDINATE
353 && op->ors_scope == LDAP_SCOPE_SUBORDINATE )
355 scopeok = !dn_match( &e->e_nname, &realbase )
356 && dnIsSuffix( &e->e_nname, &realbase );
364 BerVarray erefs = get_entry_referrals( op, e );
365 rs->sr_ref = referral_rewrite( erefs,
367 op->ors_scope == LDAP_SCOPE_ONELEVEL
369 : LDAP_SCOPE_SUBTREE );
371 ber_bvarray_free( erefs );
373 send_search_reference( op, rs );
375 ber_bvarray_free( rs->sr_ref );
379 Debug( LDAP_DEBUG_TRACE,
380 "ldbm_search: candidate referral %ld scope not okay\n",
387 if ( !manageDSAit && is_entry_glue( e )) {
391 /* if it matches the filter and scope, send it */
392 result = test_filter( op, e, op->ors_filter );
394 if ( result == LDAP_COMPARE_TRUE ) {
398 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
399 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
400 dnParent( &e->e_nname, &dn );
401 scopeok = dn_match( &dn, &realbase );
403 scopeok = (realbase.bv_len == 0);
406 } else if ( !scopeok &&
407 op->ors_scope == LDAP_SCOPE_SUBTREE )
409 scopeok = dnIsSuffix( &e->e_nname, &realbase );
411 #ifdef LDAP_SCOPE_SUBORDINATE
412 } else if ( !scopeok &&
413 op->ors_scope == LDAP_SCOPE_SUBORDINATE )
415 scopeok = !dn_match( &e->e_nname, &realbase )
416 && dnIsSuffix( &e->e_nname, &realbase );
424 /* check size limit */
425 if ( --op->ors_slimit == -1 ) {
426 cache_return_entry_r( &li->li_cache, e );
427 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
429 send_ldap_result( op, rs );
436 result = send_search_entry( op, rs );
439 case 0: /* entry sent ok */
441 case 1: /* entry not sent */
443 case -1: /* connection closed */
444 cache_return_entry_r( &li->li_cache, e );
451 Debug( LDAP_DEBUG_TRACE,
452 "ldbm_search: candidate entry %ld scope not okay\n",
457 Debug( LDAP_DEBUG_TRACE,
458 "ldbm_search: candidate entry %ld does not match filter\n",
464 /* free reader lock */
465 cache_return_entry_r( &li->li_cache, e );
468 ldap_pvt_thread_yield();
471 rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
472 rs->sr_ref = rs->sr_v2ref;
473 send_ldap_result( op, rs );
478 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
480 if( candidates != NULL )
481 idl_free( candidates );
483 if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
484 if( realbase.bv_val ) free( realbase.bv_val );
496 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
500 idl = idl_alloc( 1 );
501 idl_insert( &idl, e->e_id, 1 );
515 ID_BLOCK *candidates;
516 Filter f, fand, rf, af, xf;
517 AttributeAssertion aa_ref, aa_alias;
518 struct berval bv_ref = { sizeof("referral")-1, "referral" };
519 struct berval bv_alias = { sizeof("alias")-1, "alias" };
520 #ifdef LDBM_SUBENTRIES
522 AttributeAssertion aa_subentry;
525 Debug(LDAP_DEBUG_TRACE,
526 "search_candidates: base=\"%s\" s=%d d=%d\n",
527 e->e_ndn, scope, deref );
531 xf.f_choice = LDAP_FILTER_OR;
535 /* match referrals */
536 rf.f_choice = LDAP_FILTER_EQUALITY;
538 rf.f_av_desc = slap_schema.si_ad_objectClass;
539 rf.f_av_value = bv_ref;
544 if( deref & LDAP_DEREF_SEARCHING ) {
546 af.f_choice = LDAP_FILTER_EQUALITY;
547 af.f_ava = &aa_alias;
548 af.f_av_desc = slap_schema.si_ad_objectClass;
549 af.f_av_value = bv_alias;
555 f.f_choice = LDAP_FILTER_AND;
557 fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
558 ? SLAPD_FILTER_DN_ONE
559 : SLAPD_FILTER_DN_SUBTREE;
560 fand.f_dn = &e->e_nname;
561 fand.f_next = xf.f_or == filter ? filter : &xf ;
563 #ifdef LDBM_SUBENTRIES
564 if ( get_subentries_visibility( op )) {
565 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
566 sf.f_choice = LDAP_FILTER_EQUALITY;
567 sf.f_ava = &aa_subentry;
568 sf.f_av_desc = slap_schema.si_ad_objectClass;
569 sf.f_av_value = bv_subentry;
570 sf.f_next = fand.f_next;
575 candidates = filter_candidates( op, &f );
576 return( candidates );