1 /* search.c - ldbm backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2006 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 if ( is_entry_subentry( e ) ) {
276 if( op->ors_scope != LDAP_SCOPE_BASE ) {
277 if(!get_subentries_visibility( op )) {
278 /* only subentries are visible */
281 } else if ( get_subentries( op ) &&
282 !get_subentries_visibility( op ))
284 /* only subentries are visible */
287 } else if ( get_subentries_visibility( op )) {
288 /* only subentries are visible */
292 if ( op->ors_deref & LDAP_DEREF_SEARCHING &&
293 is_entry_alias( e ) )
299 e = deref_entry_r( op->o_bd, e, &err, &matched, &text );
306 if( e->e_id == id ) {
311 /* need to skip alias which deref into scope */
312 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
314 dnParent( &e->e_nname, &pdn );
315 if ( ber_bvcmp( &pdn, &realbase ) ) {
319 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
320 /* alias is within scope */
321 Debug( LDAP_DEBUG_TRACE,
322 "ldbm_search: alias \"%s\" in subtree\n",
334 * If it's a referral, add it to the list of referrals.
335 * Only do this for non-base searches, and don't check
336 * the filter explicitly here since it's only a candidate
339 if ( !manageDSAit && op->ors_scope != LDAP_SCOPE_BASE &&
340 is_entry_referral( e ) )
345 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
346 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
347 dnParent( &e->e_nname, &dn );
348 scopeok = dn_match( &dn, &realbase );
350 scopeok = (realbase.bv_len == 0);
354 && op->ors_scope == LDAP_SCOPE_SUBTREE )
356 scopeok = dnIsSuffix( &e->e_nname, &realbase );
358 #ifdef LDAP_SCOPE_SUBORDINATE
360 && op->ors_scope == LDAP_SCOPE_SUBORDINATE )
362 scopeok = !dn_match( &e->e_nname, &realbase )
363 && dnIsSuffix( &e->e_nname, &realbase );
371 BerVarray erefs = get_entry_referrals( op, e );
372 rs->sr_ref = referral_rewrite( erefs,
374 op->ors_scope == LDAP_SCOPE_ONELEVEL
376 : LDAP_SCOPE_SUBTREE );
378 ber_bvarray_free( erefs );
380 send_search_reference( op, rs );
382 ber_bvarray_free( rs->sr_ref );
386 Debug( LDAP_DEBUG_TRACE,
387 "ldbm_search: candidate referral %ld scope not okay\n",
394 if ( !manageDSAit && is_entry_glue( e )) {
398 /* if it matches the filter and scope, send it */
399 result = test_filter( op, e, op->ors_filter );
401 if ( result == LDAP_COMPARE_TRUE ) {
405 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
406 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
407 dnParent( &e->e_nname, &dn );
408 scopeok = dn_match( &dn, &realbase );
410 scopeok = (realbase.bv_len == 0);
413 } else if ( !scopeok &&
414 op->ors_scope == LDAP_SCOPE_SUBTREE )
416 scopeok = dnIsSuffix( &e->e_nname, &realbase );
418 } else if ( !scopeok &&
419 op->ors_scope == LDAP_SCOPE_SUBORDINATE )
421 scopeok = !dn_match( &e->e_nname, &realbase )
422 && dnIsSuffix( &e->e_nname, &realbase );
431 rs->sr_err = send_search_entry( op, rs );
433 switch ( rs->sr_err ) {
434 case LDAP_UNAVAILABLE: /* connection closed */
435 cache_return_entry_r( &li->li_cache, e );
438 case LDAP_SIZELIMIT_EXCEEDED:
439 cache_return_entry_r( &li->li_cache, e );
442 send_ldap_result( op, rs );
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" };
519 AttributeAssertion aa_subentry;
521 Debug(LDAP_DEBUG_TRACE,
522 "search_candidates: base=\"%s\" s=%d d=%d\n",
523 e->e_ndn, scope, deref );
527 xf.f_choice = LDAP_FILTER_OR;
531 /* match referrals */
532 rf.f_choice = LDAP_FILTER_EQUALITY;
534 rf.f_av_desc = slap_schema.si_ad_objectClass;
535 rf.f_av_value = bv_ref;
540 if( deref & LDAP_DEREF_SEARCHING ) {
542 af.f_choice = LDAP_FILTER_EQUALITY;
543 af.f_ava = &aa_alias;
544 af.f_av_desc = slap_schema.si_ad_objectClass;
545 af.f_av_value = bv_alias;
551 f.f_choice = LDAP_FILTER_AND;
553 fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
554 ? SLAPD_FILTER_DN_ONE
555 : SLAPD_FILTER_DN_SUBTREE;
556 fand.f_dn = &e->e_nname;
557 fand.f_next = xf.f_or == filter ? filter : &xf ;
559 if ( get_subentries_visibility( op )) {
560 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
561 sf.f_choice = LDAP_FILTER_EQUALITY;
562 sf.f_ava = &aa_subentry;
563 sf.f_av_desc = slap_schema.si_ad_objectClass;
564 sf.f_av_value = bv_subentry;
565 sf.f_next = fand.f_next;
569 candidates = filter_candidates( op, &f );
570 return( candidates );