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 send_search_reference( op, rs );
373 ber_bvarray_free( rs->sr_ref );
377 Debug( LDAP_DEBUG_TRACE,
378 "ldbm_search: candidate referral %ld scope not okay\n",
385 if ( !manageDSAit && is_entry_glue( e )) {
389 /* if it matches the filter and scope, send it */
390 result = test_filter( op, e, op->ors_filter );
392 if ( result == LDAP_COMPARE_TRUE ) {
396 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
397 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
398 dnParent( &e->e_nname, &dn );
399 scopeok = dn_match( &dn, &realbase );
401 scopeok = (realbase.bv_len == 0);
404 } else if ( !scopeok &&
405 op->ors_scope == LDAP_SCOPE_SUBTREE )
407 scopeok = dnIsSuffix( &e->e_nname, &realbase );
409 #ifdef LDAP_SCOPE_SUBORDINATE
410 } else if ( !scopeok &&
411 op->ors_scope == LDAP_SCOPE_SUBORDINATE )
413 scopeok = !dn_match( &e->e_nname, &realbase )
414 && dnIsSuffix( &e->e_nname, &realbase );
422 /* check size limit */
423 if ( --op->ors_slimit == -1 ) {
424 cache_return_entry_r( &li->li_cache, e );
425 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
427 send_ldap_result( op, rs );
434 result = send_search_entry( op, rs );
437 case 0: /* entry sent ok */
439 case 1: /* entry not sent */
441 case -1: /* connection closed */
442 cache_return_entry_r( &li->li_cache, e );
449 Debug( LDAP_DEBUG_TRACE,
450 "ldbm_search: candidate entry %ld scope not okay\n",
455 Debug( LDAP_DEBUG_TRACE,
456 "ldbm_search: candidate entry %ld does not match filter\n",
462 /* free reader lock */
463 cache_return_entry_r( &li->li_cache, e );
466 ldap_pvt_thread_yield();
469 rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
470 rs->sr_ref = rs->sr_v2ref;
471 send_ldap_result( op, rs );
476 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
478 if( candidates != NULL )
479 idl_free( candidates );
481 if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
482 if( realbase.bv_val ) free( realbase.bv_val );
494 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
498 idl = idl_alloc( 1 );
499 idl_insert( &idl, e->e_id, 1 );
513 ID_BLOCK *candidates;
514 Filter f, fand, rf, af, xf;
515 AttributeAssertion aa_ref, aa_alias;
516 struct berval bv_ref = { sizeof("referral")-1, "referral" };
517 struct berval bv_alias = { sizeof("alias")-1, "alias" };
518 #ifdef LDBM_SUBENTRIES
520 AttributeAssertion aa_subentry;
523 Debug(LDAP_DEBUG_TRACE,
524 "search_candidates: base=\"%s\" s=%d d=%d\n",
525 e->e_ndn, scope, deref );
529 xf.f_choice = LDAP_FILTER_OR;
533 /* match referrals */
534 rf.f_choice = LDAP_FILTER_EQUALITY;
536 rf.f_av_desc = slap_schema.si_ad_objectClass;
537 rf.f_av_value = bv_ref;
542 if( deref & LDAP_DEREF_SEARCHING ) {
544 af.f_choice = LDAP_FILTER_EQUALITY;
545 af.f_ava = &aa_alias;
546 af.f_av_desc = slap_schema.si_ad_objectClass;
547 af.f_av_value = bv_alias;
553 f.f_choice = LDAP_FILTER_AND;
555 fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
556 ? SLAPD_FILTER_DN_ONE
557 : SLAPD_FILTER_DN_SUBTREE;
558 fand.f_dn = &e->e_nname;
559 fand.f_next = xf.f_or == filter ? filter : &xf ;
561 #ifdef LDBM_SUBENTRIES
562 if ( get_subentries_visibility( op )) {
563 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
564 sf.f_choice = LDAP_FILTER_EQUALITY;
565 sf.f_ava = &aa_subentry;
566 sf.f_av_desc = slap_schema.si_ad_objectClass;
567 sf.f_av_value = bv_subentry;
568 sf.f_next = fand.f_next;
573 candidates = filter_candidates( op, &f );
574 return( candidates );