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;
43 const char *text = NULL;
48 Entry *matched = NULL;
49 struct berval realbase = BER_BVNULL;
50 int manageDSAit = get_manageDSAit( op );
52 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
54 /* grab giant lock for reading */
55 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
57 if ( op->o_req_ndn.bv_len == 0 ) {
58 /* DIT root special case */
59 e = (Entry *) &slap_entry_root;
61 /* need normalized dn below */
62 ber_dupbv( &realbase, &e->e_nname );
64 candidates = search_candidates( op, e, op->ors_filter,
65 op->ors_scope, op->ors_deref,
66 manageDSAit || get_domainScope(op) );
70 } else if ( op->ors_deref & LDAP_DEREF_FINDING ) {
71 /* deref dn and get entry with reader lock */
72 e = deref_dn_r( op->o_bd, &op->o_req_ndn,
73 &rs->sr_err, &matched, &rs->sr_text );
75 if( rs->sr_err == LDAP_NO_SUCH_OBJECT ) rs->sr_err = LDAP_REFERRAL;
78 /* get entry with reader lock */
79 e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched );
80 rs->sr_err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
85 struct berval matched_dn = BER_BVNULL;
87 if ( matched != NULL ) {
88 BerVarray erefs = NULL;
90 #ifdef SLAP_ACL_HONOR_DISCLOSE
91 if ( ! access_allowed( op, matched,
92 slap_schema.si_ad_entry,
93 NULL, ACL_DISCLOSE, NULL ) )
95 rs->sr_err = LDAP_NO_SUCH_OBJECT;
98 #endif /* SLAP_ACL_HONOR_DISCLOSE */
100 ber_dupbv( &matched_dn, &matched->e_name );
102 erefs = is_entry_referral( matched )
103 ? get_entry_referrals( op, matched )
107 cache_return_entry_r( &li->li_cache, matched );
110 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
111 &op->o_req_dn, op->ors_scope );
113 ber_bvarray_free( erefs );
117 rs->sr_ref = referral_rewrite( default_referral,
118 NULL, &op->o_req_dn, op->ors_scope );
121 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
123 rs->sr_matched = matched_dn.bv_val;
124 send_ldap_result( op, rs );
126 ber_bvarray_free( rs->sr_ref );
127 ber_memfree( matched_dn.bv_val );
129 rs->sr_matched = NULL;
133 #ifdef SLAP_ACL_HONOR_DISCLOSE
134 if ( ! access_allowed( op, e, slap_schema.si_ad_entry,
135 NULL, ACL_DISCLOSE, NULL ) )
137 rs->sr_err = LDAP_NO_SUCH_OBJECT;
139 cache_return_entry_r( &li->li_cache, e );
140 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
142 send_ldap_result( op, rs );
145 #endif /* SLAP_ACL_HONOR_DISCLOSE */
147 if ( !manageDSAit && is_entry_referral( e ) ) {
148 /* entry is a referral, don't allow add */
149 struct berval matched_dn = BER_BVNULL;
150 BerVarray erefs = NULL;
153 rs->sr_err = LDAP_OTHER;
154 rs->sr_text = "bad referral object";
156 ber_dupbv( &matched_dn, &e->e_name );
157 erefs = get_entry_referrals( op, e );
159 cache_return_entry_r( &li->li_cache, e );
160 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
162 Debug( LDAP_DEBUG_TRACE,
163 "ldbm_search: entry is referral\n",
167 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
168 &op->o_req_dn, op->ors_scope );
170 ber_bvarray_free( erefs );
173 rs->sr_err = LDAP_REFERRAL;
178 rs->sr_matched = matched_dn.bv_val;
179 send_ldap_result( op, rs );
180 ber_bvarray_free( rs->sr_ref );
181 ber_memfree( matched_dn.bv_val );
183 rs->sr_matched = NULL;
187 if ( is_entry_alias( e ) ) {
189 op->ors_deref = LDAP_DEREF_NEVER;
192 if ( op->ors_scope == LDAP_SCOPE_BASE ) {
193 candidates = base_candidate( op->o_bd, e );
196 candidates = search_candidates( op, e, op->ors_filter,
197 op->ors_scope, op->ors_deref, manageDSAit );
200 /* need normalized dn below */
201 ber_dupbv( &realbase, &e->e_nname );
203 cache_return_entry_r( &li->li_cache, e );
206 if ( candidates == NULL ) {
208 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
211 rs->sr_err = LDAP_SUCCESS;
212 send_ldap_result( op, rs );
218 /* if candidates exceed to-be-checked entries, abort */
219 if ( op->ors_limit /* isroot == FALSE */
220 && op->ors_limit->lms_s_unchecked != -1
221 && ID_BLOCK_NIDS( candidates ) > (unsigned) op->ors_limit->lms_s_unchecked )
223 send_ldap_error( op, rs, LDAP_ADMINLIMIT_EXCEEDED, NULL );
228 /* compute it anyway; root does not use it */
229 stoptime = op->o_time + op->ors_tlimit;
230 rs->sr_attrs = op->ors_attrs;
232 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
233 id = idl_nextid( candidates, &cursor ) )
238 /* check for abandon */
239 if ( op->o_abandon ) {
244 /* check time limit */
245 if ( op->ors_tlimit != SLAP_NO_LIMIT
246 && slap_get_time() > stoptime )
248 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
249 send_ldap_result( op, rs );
254 /* get the entry with reader lock */
255 e = id2entry_r( op->o_bd, id );
258 Debug( LDAP_DEBUG_TRACE,
259 "ldbm_search: candidate %ld not found\n",
267 #ifdef LDBM_SUBENTRIES
268 if ( is_entry_subentry( e ) ) {
269 if( op->ors_scope != LDAP_SCOPE_BASE ) {
270 if(!get_subentries_visibility( op )) {
271 /* only subentries are visible */
274 } else if ( get_subentries( op ) &&
275 !get_subentries_visibility( op ))
277 /* only subentries are visible */
280 } else if ( get_subentries_visibility( op )) {
281 /* only subentries are visible */
286 if ( op->ors_deref & LDAP_DEREF_SEARCHING &&
287 is_entry_alias( e ) )
293 e = deref_entry_r( op->o_bd, e, &err, &matched, &text );
300 if( e->e_id == id ) {
305 /* need to skip alias which deref into scope */
306 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
308 dnParent( &e->e_nname, &pdn );
309 if ( ber_bvcmp( &pdn, &realbase ) ) {
313 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
314 /* alias is within scope */
315 Debug( LDAP_DEBUG_TRACE,
316 "ldbm_search: alias \"%s\" in subtree\n",
328 * If it's a referral, add it to the list of referrals.
329 * Only do this for non-base searches, and don't check
330 * the filter explicitly here since it's only a candidate
333 if ( !manageDSAit && op->ors_scope != LDAP_SCOPE_BASE &&
334 is_entry_referral( e ) )
339 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
340 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
341 dnParent( &e->e_nname, &dn );
342 scopeok = dn_match( &dn, &realbase );
344 scopeok = (realbase.bv_len == 0);
348 && op->ors_scope == LDAP_SCOPE_SUBTREE )
350 scopeok = dnIsSuffix( &e->e_nname, &realbase );
352 #ifdef LDAP_SCOPE_SUBORDINATE
354 && op->ors_scope == LDAP_SCOPE_SUBORDINATE )
356 scopeok = !dn_match( &e->e_nname, &realbase )
357 && dnIsSuffix( &e->e_nname, &realbase );
365 BerVarray erefs = get_entry_referrals( op, e );
366 rs->sr_ref = referral_rewrite( erefs,
368 op->ors_scope == LDAP_SCOPE_ONELEVEL
370 : LDAP_SCOPE_SUBTREE );
372 send_search_reference( op, rs );
374 ber_bvarray_free( rs->sr_ref );
378 Debug( LDAP_DEBUG_TRACE,
379 "ldbm_search: candidate referral %ld scope not okay\n",
386 if ( !manageDSAit && is_entry_glue( e )) {
390 /* if it matches the filter and scope, send it */
391 result = test_filter( op, e, op->ors_filter );
393 if ( result == LDAP_COMPARE_TRUE ) {
397 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
398 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
399 dnParent( &e->e_nname, &dn );
400 scopeok = dn_match( &dn, &realbase );
402 scopeok = (realbase.bv_len == 0);
405 } else if ( !scopeok &&
406 op->ors_scope == LDAP_SCOPE_SUBTREE )
408 scopeok = dnIsSuffix( &e->e_nname, &realbase );
410 #ifdef LDAP_SCOPE_SUBORDINATE
411 } else if ( !scopeok &&
412 op->ors_scope == LDAP_SCOPE_SUBORDINATE )
414 scopeok = !dn_match( &e->e_nname, &realbase )
415 && dnIsSuffix( &e->e_nname, &realbase );
423 /* check size limit */
424 if ( --op->ors_slimit == -1 ) {
425 cache_return_entry_r( &li->li_cache, e );
426 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
428 send_ldap_result( op, rs );
435 result = send_search_entry( op, rs );
438 case 0: /* entry sent ok */
440 case 1: /* entry not sent */
442 case -1: /* connection closed */
443 cache_return_entry_r( &li->li_cache, e );
450 Debug( LDAP_DEBUG_TRACE,
451 "ldbm_search: candidate entry %ld scope not okay\n",
456 Debug( LDAP_DEBUG_TRACE,
457 "ldbm_search: candidate entry %ld does not match filter\n",
463 /* free reader lock */
464 cache_return_entry_r( &li->li_cache, e );
467 ldap_pvt_thread_yield();
470 rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
471 rs->sr_ref = rs->sr_v2ref;
472 send_ldap_result( op, rs );
477 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
479 if( candidates != NULL )
480 idl_free( candidates );
482 if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
483 if( realbase.bv_val ) free( realbase.bv_val );
495 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
499 idl = idl_alloc( 1 );
500 idl_insert( &idl, e->e_id, 1 );
514 ID_BLOCK *candidates;
515 Filter f, fand, rf, af, xf;
516 AttributeAssertion aa_ref, aa_alias;
517 struct berval bv_ref = { sizeof("referral")-1, "referral" };
518 struct berval bv_alias = { sizeof("alias")-1, "alias" };
519 #ifdef LDBM_SUBENTRIES
521 AttributeAssertion aa_subentry;
524 Debug(LDAP_DEBUG_TRACE,
525 "search_candidates: base=\"%s\" s=%d d=%d\n",
526 e->e_ndn, scope, deref );
530 xf.f_choice = LDAP_FILTER_OR;
534 /* match referrals */
535 rf.f_choice = LDAP_FILTER_EQUALITY;
537 rf.f_av_desc = slap_schema.si_ad_objectClass;
538 rf.f_av_value = bv_ref;
543 if( deref & LDAP_DEREF_SEARCHING ) {
545 af.f_choice = LDAP_FILTER_EQUALITY;
546 af.f_ava = &aa_alias;
547 af.f_av_desc = slap_schema.si_ad_objectClass;
548 af.f_av_value = bv_alias;
554 f.f_choice = LDAP_FILTER_AND;
556 fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
557 ? SLAPD_FILTER_DN_ONE
558 : SLAPD_FILTER_DN_SUBTREE;
559 fand.f_dn = &e->e_nname;
560 fand.f_next = xf.f_or == filter ? filter : &xf ;
562 #ifdef LDBM_SUBENTRIES
563 if ( get_subentries_visibility( op )) {
564 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
565 sf.f_choice = LDAP_FILTER_EQUALITY;
566 sf.f_ava = &aa_subentry;
567 sf.f_av_desc = slap_schema.si_ad_objectClass;
568 sf.f_av_value = bv_subentry;
569 sf.f_next = fand.f_next;
574 candidates = filter_candidates( op, &f );
575 return( candidates );