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 );
50 #ifdef SLAP_ACL_HONOR_DISCLOSE
54 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
56 /* grab giant lock for reading */
57 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
59 if ( op->o_req_ndn.bv_len == 0 ) {
60 /* DIT root special case */
61 e = (Entry *) &slap_entry_root;
63 /* need normalized dn below */
64 ber_dupbv( &realbase, &e->e_nname );
66 candidates = search_candidates( op, e, op->ors_filter,
67 op->ors_scope, op->ors_deref,
68 manageDSAit || get_domainScope(op) );
72 } else if ( op->ors_deref & LDAP_DEREF_FINDING ) {
73 /* deref dn and get entry with reader lock */
74 e = deref_dn_r( op->o_bd, &op->o_req_ndn,
75 &rs->sr_err, &matched, &rs->sr_text );
77 if( rs->sr_err == LDAP_NO_SUCH_OBJECT ) rs->sr_err = LDAP_REFERRAL;
80 /* get entry with reader lock */
81 e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched );
82 rs->sr_err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
87 struct berval matched_dn = BER_BVNULL;
89 if ( matched != NULL ) {
90 BerVarray erefs = NULL;
92 #ifdef SLAP_ACL_HONOR_DISCLOSE
93 if ( ! access_allowed( op, matched,
94 slap_schema.si_ad_entry,
95 NULL, ACL_DISCLOSE, NULL ) )
97 rs->sr_err = LDAP_NO_SUCH_OBJECT;
100 #endif /* SLAP_ACL_HONOR_DISCLOSE */
102 ber_dupbv( &matched_dn, &matched->e_name );
104 erefs = is_entry_referral( matched )
105 ? get_entry_referrals( op, matched )
109 cache_return_entry_r( &li->li_cache, matched );
112 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
113 &op->o_req_dn, op->ors_scope );
115 ber_bvarray_free( erefs );
119 rs->sr_ref = referral_rewrite( default_referral,
120 NULL, &op->o_req_dn, op->ors_scope );
123 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
125 rs->sr_matched = matched_dn.bv_val;
126 send_ldap_result( op, rs );
128 ber_bvarray_free( rs->sr_ref );
129 ber_memfree( matched_dn.bv_val );
131 rs->sr_matched = NULL;
135 #ifdef SLAP_ACL_HONOR_DISCLOSE
136 /* NOTE: __NEW__ "search" access is required
137 * on searchBase object */
138 if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
139 NULL, ACL_SEARCH, NULL, &mask ) )
141 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
142 rs->sr_err = LDAP_NO_SUCH_OBJECT;
144 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
147 cache_return_entry_r( &li->li_cache, e );
148 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
150 send_ldap_result( op, rs );
153 #endif /* SLAP_ACL_HONOR_DISCLOSE */
155 if ( !manageDSAit && is_entry_referral( e ) ) {
156 /* entry is a referral, don't allow add */
157 struct berval matched_dn = BER_BVNULL;
158 BerVarray erefs = NULL;
161 rs->sr_err = LDAP_OTHER;
162 rs->sr_text = "bad referral object";
164 ber_dupbv( &matched_dn, &e->e_name );
165 erefs = get_entry_referrals( op, e );
167 cache_return_entry_r( &li->li_cache, e );
168 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
170 Debug( LDAP_DEBUG_TRACE,
171 "ldbm_search: entry is referral\n",
175 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
176 &op->o_req_dn, op->ors_scope );
178 ber_bvarray_free( erefs );
181 rs->sr_err = LDAP_REFERRAL;
186 rs->sr_matched = matched_dn.bv_val;
187 send_ldap_result( op, rs );
188 ber_bvarray_free( rs->sr_ref );
189 ber_memfree( matched_dn.bv_val );
191 rs->sr_matched = NULL;
195 if ( is_entry_alias( e ) ) {
197 op->ors_deref = LDAP_DEREF_NEVER;
200 if ( op->ors_scope == LDAP_SCOPE_BASE ) {
201 candidates = base_candidate( op->o_bd, e );
204 candidates = search_candidates( op, e, op->ors_filter,
205 op->ors_scope, op->ors_deref, manageDSAit );
208 /* need normalized dn below */
209 ber_dupbv( &realbase, &e->e_nname );
211 cache_return_entry_r( &li->li_cache, e );
214 if ( candidates == NULL ) {
216 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
219 rs->sr_err = LDAP_SUCCESS;
220 send_ldap_result( op, rs );
226 /* if candidates exceed to-be-checked entries, abort */
227 if ( op->ors_limit /* isroot == FALSE */
228 && op->ors_limit->lms_s_unchecked != -1
229 && ID_BLOCK_NIDS( candidates ) > (unsigned) op->ors_limit->lms_s_unchecked )
231 send_ldap_error( op, rs, LDAP_ADMINLIMIT_EXCEEDED, NULL );
236 /* compute it anyway; root does not use it */
237 stoptime = op->o_time + op->ors_tlimit;
238 rs->sr_attrs = op->ors_attrs;
240 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
241 id = idl_nextid( candidates, &cursor ) )
246 /* check for abandon */
247 if ( op->o_abandon ) {
252 /* check time limit */
253 if ( op->ors_tlimit != SLAP_NO_LIMIT
254 && slap_get_time() > stoptime )
256 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
257 send_ldap_result( op, rs );
262 /* get the entry with reader lock */
263 e = id2entry_r( op->o_bd, id );
266 Debug( LDAP_DEBUG_TRACE,
267 "ldbm_search: candidate %ld not found\n",
275 #ifdef LDBM_SUBENTRIES
276 if ( is_entry_subentry( e ) ) {
277 if( op->ors_scope != LDAP_SCOPE_BASE ) {
278 if(!get_subentries_visibility( op )) {
279 /* only subentries are visible */
282 } else if ( get_subentries( op ) &&
283 !get_subentries_visibility( op ))
285 /* only subentries are visible */
288 } else if ( get_subentries_visibility( op )) {
289 /* only subentries are visible */
294 if ( op->ors_deref & LDAP_DEREF_SEARCHING &&
295 is_entry_alias( e ) )
301 e = deref_entry_r( op->o_bd, e, &err, &matched, &text );
308 if( e->e_id == id ) {
313 /* need to skip alias which deref into scope */
314 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
316 dnParent( &e->e_nname, &pdn );
317 if ( ber_bvcmp( &pdn, &realbase ) ) {
321 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
322 /* alias is within scope */
323 Debug( LDAP_DEBUG_TRACE,
324 "ldbm_search: alias \"%s\" in subtree\n",
336 * If it's a referral, add it to the list of referrals.
337 * Only do this for non-base searches, and don't check
338 * the filter explicitly here since it's only a candidate
341 if ( !manageDSAit && op->ors_scope != LDAP_SCOPE_BASE &&
342 is_entry_referral( e ) )
347 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
348 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
349 dnParent( &e->e_nname, &dn );
350 scopeok = dn_match( &dn, &realbase );
352 scopeok = (realbase.bv_len == 0);
356 && op->ors_scope == LDAP_SCOPE_SUBTREE )
358 scopeok = dnIsSuffix( &e->e_nname, &realbase );
360 #ifdef LDAP_SCOPE_SUBORDINATE
362 && op->ors_scope == LDAP_SCOPE_SUBORDINATE )
364 scopeok = !dn_match( &e->e_nname, &realbase )
365 && dnIsSuffix( &e->e_nname, &realbase );
373 BerVarray erefs = get_entry_referrals( op, e );
374 rs->sr_ref = referral_rewrite( erefs,
376 op->ors_scope == LDAP_SCOPE_ONELEVEL
378 : LDAP_SCOPE_SUBTREE );
380 ber_bvarray_free( erefs );
382 send_search_reference( op, rs );
384 ber_bvarray_free( rs->sr_ref );
388 Debug( LDAP_DEBUG_TRACE,
389 "ldbm_search: candidate referral %ld scope not okay\n",
396 if ( !manageDSAit && is_entry_glue( e )) {
400 /* if it matches the filter and scope, send it */
401 result = test_filter( op, e, op->ors_filter );
403 if ( result == LDAP_COMPARE_TRUE ) {
407 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
408 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
409 dnParent( &e->e_nname, &dn );
410 scopeok = dn_match( &dn, &realbase );
412 scopeok = (realbase.bv_len == 0);
415 } else if ( !scopeok &&
416 op->ors_scope == LDAP_SCOPE_SUBTREE )
418 scopeok = dnIsSuffix( &e->e_nname, &realbase );
420 #ifdef LDAP_SCOPE_SUBORDINATE
421 } else if ( !scopeok &&
422 op->ors_scope == LDAP_SCOPE_SUBORDINATE )
424 scopeok = !dn_match( &e->e_nname, &realbase )
425 && dnIsSuffix( &e->e_nname, &realbase );
433 /* check size limit */
434 if ( --op->ors_slimit == -1 ) {
435 cache_return_entry_r( &li->li_cache, e );
436 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
438 send_ldap_result( op, rs );
445 result = send_search_entry( op, rs );
448 case 0: /* entry sent ok */
450 case 1: /* entry not sent */
452 case -1: /* connection closed */
453 cache_return_entry_r( &li->li_cache, e );
460 Debug( LDAP_DEBUG_TRACE,
461 "ldbm_search: candidate entry %ld scope not okay\n",
466 Debug( LDAP_DEBUG_TRACE,
467 "ldbm_search: candidate entry %ld does not match filter\n",
473 /* free reader lock */
474 cache_return_entry_r( &li->li_cache, e );
477 ldap_pvt_thread_yield();
480 rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
481 rs->sr_ref = rs->sr_v2ref;
482 send_ldap_result( op, rs );
487 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
489 if( candidates != NULL )
490 idl_free( candidates );
492 if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
493 if( realbase.bv_val ) free( realbase.bv_val );
505 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
509 idl = idl_alloc( 1 );
510 idl_insert( &idl, e->e_id, 1 );
524 ID_BLOCK *candidates;
525 Filter f, fand, rf, af, xf;
526 AttributeAssertion aa_ref, aa_alias;
527 struct berval bv_ref = { sizeof("referral")-1, "referral" };
528 struct berval bv_alias = { sizeof("alias")-1, "alias" };
529 #ifdef LDBM_SUBENTRIES
531 AttributeAssertion aa_subentry;
534 Debug(LDAP_DEBUG_TRACE,
535 "search_candidates: base=\"%s\" s=%d d=%d\n",
536 e->e_ndn, scope, deref );
540 xf.f_choice = LDAP_FILTER_OR;
544 /* match referrals */
545 rf.f_choice = LDAP_FILTER_EQUALITY;
547 rf.f_av_desc = slap_schema.si_ad_objectClass;
548 rf.f_av_value = bv_ref;
553 if( deref & LDAP_DEREF_SEARCHING ) {
555 af.f_choice = LDAP_FILTER_EQUALITY;
556 af.f_ava = &aa_alias;
557 af.f_av_desc = slap_schema.si_ad_objectClass;
558 af.f_av_value = bv_alias;
564 f.f_choice = LDAP_FILTER_AND;
566 fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
567 ? SLAPD_FILTER_DN_ONE
568 : SLAPD_FILTER_DN_SUBTREE;
569 fand.f_dn = &e->e_nname;
570 fand.f_next = xf.f_or == filter ? filter : &xf ;
572 #ifdef LDBM_SUBENTRIES
573 if ( get_subentries_visibility( op )) {
574 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
575 sf.f_choice = LDAP_FILTER_EQUALITY;
576 sf.f_ava = &aa_subentry;
577 sf.f_av_desc = slap_schema.si_ad_objectClass;
578 sf.f_av_value = bv_subentry;
579 sf.f_next = fand.f_next;
584 candidates = filter_candidates( op, &f );
585 return( candidates );