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 ) {
89 ber_dupbv( &matched_dn, &matched->e_name );
91 erefs = is_entry_referral( matched )
92 ? get_entry_referrals( op, matched )
95 cache_return_entry_r( &li->li_cache, matched );
98 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
99 &op->o_req_dn, op->ors_scope );
101 ber_bvarray_free( erefs );
105 rs->sr_ref = referral_rewrite( default_referral,
106 NULL, &op->o_req_dn, op->ors_scope );
109 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
111 rs->sr_matched = matched_dn.bv_val;
112 send_ldap_result( op, rs );
114 ber_bvarray_free( rs->sr_ref );
115 ber_memfree( matched_dn.bv_val );
117 rs->sr_matched = NULL;
118 return LDAP_REFERRAL;
121 if (!manageDSAit && is_entry_referral( e ) ) {
122 /* entry is a referral, don't allow add */
123 struct berval matched_dn;
126 ber_dupbv( &matched_dn, &e->e_name );
127 erefs = get_entry_referrals( op, e );
130 cache_return_entry_r( &li->li_cache, e );
131 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
133 Debug( LDAP_DEBUG_TRACE,
134 "ldbm_search: entry is referral\n",
138 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
139 &op->o_req_dn, op->ors_scope );
141 ber_bvarray_free( erefs );
144 rs->sr_matched = matched_dn.bv_val;
146 rs->sr_err = LDAP_REFERRAL;
147 send_ldap_result( op, rs );
148 ber_bvarray_free( rs->sr_ref );
151 send_ldap_error( op, rs, LDAP_OTHER,
152 "bad referral object" );
155 ber_memfree( matched_dn.bv_val );
157 rs->sr_matched = NULL;
161 if ( is_entry_alias( e ) ) {
163 op->ors_deref = LDAP_DEREF_NEVER;
166 if ( op->ors_scope == LDAP_SCOPE_BASE ) {
167 candidates = base_candidate( op->o_bd, e );
170 candidates = search_candidates( op, e, op->ors_filter,
171 op->ors_scope, op->ors_deref, manageDSAit );
174 /* need normalized dn below */
175 ber_dupbv( &realbase, &e->e_nname );
177 cache_return_entry_r( &li->li_cache, e );
180 if ( candidates == NULL ) {
182 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
185 rs->sr_err = LDAP_SUCCESS;
186 send_ldap_result( op, rs );
192 /* if candidates exceed to-be-checked entries, abort */
193 if ( op->ors_limit /* isroot == FALSE */
194 && op->ors_limit->lms_s_unchecked != -1
195 && ID_BLOCK_NIDS( candidates ) > (unsigned) op->ors_limit->lms_s_unchecked )
197 send_ldap_error( op, rs, LDAP_ADMINLIMIT_EXCEEDED, NULL );
202 /* compute it anyway; root does not use it */
203 stoptime = op->o_time + op->ors_tlimit;
204 rs->sr_attrs = op->ors_attrs;
206 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
207 id = idl_nextid( candidates, &cursor ) )
212 /* check for abandon */
213 if ( op->o_abandon ) {
218 /* check time limit */
219 if ( op->ors_tlimit != SLAP_NO_LIMIT
220 && slap_get_time() > stoptime )
222 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
223 send_ldap_result( op, rs );
228 /* get the entry with reader lock */
229 e = id2entry_r( op->o_bd, id );
232 Debug( LDAP_DEBUG_TRACE,
233 "ldbm_search: candidate %ld not found\n",
241 #ifdef LDBM_SUBENTRIES
242 if ( is_entry_subentry( e ) ) {
243 if( op->ors_scope != LDAP_SCOPE_BASE ) {
244 if(!get_subentries_visibility( op )) {
245 /* only subentries are visible */
248 } else if ( get_subentries( op ) &&
249 !get_subentries_visibility( op ))
251 /* only subentries are visible */
254 } else if ( get_subentries_visibility( op )) {
255 /* only subentries are visible */
260 if ( op->ors_deref & LDAP_DEREF_SEARCHING &&
261 is_entry_alias( e ) )
267 e = deref_entry_r( op->o_bd, e, &err, &matched, &text );
274 if( e->e_id == id ) {
279 /* need to skip alias which deref into scope */
280 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
282 dnParent( &e->e_nname, &pdn );
283 if ( ber_bvcmp( &pdn, &realbase ) ) {
287 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
288 /* alias is within scope */
289 Debug( LDAP_DEBUG_TRACE,
290 "ldbm_search: alias \"%s\" in subtree\n",
302 * If it's a referral, add it to the list of referrals.
303 * Only do this for non-base searches, and don't check
304 * the filter explicitly here since it's only a candidate
307 if ( !manageDSAit && op->ors_scope != LDAP_SCOPE_BASE &&
308 is_entry_referral( e ) )
313 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
314 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
315 dnParent( &e->e_nname, &dn );
316 scopeok = dn_match( &dn, &realbase );
318 scopeok = (realbase.bv_len == 0);
322 && op->ors_scope == LDAP_SCOPE_SUBTREE )
324 scopeok = dnIsSuffix( &e->e_nname, &realbase );
326 #ifdef LDAP_SCOPE_SUBORDINATE
328 && op->ors_scope == LDAP_SCOPE_SUBORDINATE )
330 scopeok = !dn_match( &e->e_nname, &realbase )
331 && dnIsSuffix( &e->e_nname, &realbase );
339 BerVarray erefs = get_entry_referrals( op, e );
340 rs->sr_ref = referral_rewrite( erefs,
342 op->ors_scope == LDAP_SCOPE_ONELEVEL
344 : LDAP_SCOPE_SUBTREE );
346 send_search_reference( op, rs );
348 ber_bvarray_free( rs->sr_ref );
352 Debug( LDAP_DEBUG_TRACE,
353 "ldbm_search: candidate referral %ld scope not okay\n",
360 if ( !manageDSAit && is_entry_glue( e )) {
364 /* if it matches the filter and scope, send it */
365 result = test_filter( op, e, op->ors_filter );
367 if ( result == LDAP_COMPARE_TRUE ) {
371 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
372 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
373 dnParent( &e->e_nname, &dn );
374 scopeok = dn_match( &dn, &realbase );
376 scopeok = (realbase.bv_len == 0);
379 } else if ( !scopeok &&
380 op->ors_scope == LDAP_SCOPE_SUBTREE )
382 scopeok = dnIsSuffix( &e->e_nname, &realbase );
384 #ifdef LDAP_SCOPE_SUBORDINATE
385 } else if ( !scopeok &&
386 op->ors_scope == LDAP_SCOPE_SUBORDINATE )
388 scopeok = !dn_match( &e->e_nname, &realbase )
389 && dnIsSuffix( &e->e_nname, &realbase );
397 /* check size limit */
398 if ( --op->ors_slimit == -1 ) {
399 cache_return_entry_r( &li->li_cache, e );
400 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
402 send_ldap_result( op, rs );
409 result = send_search_entry( op, rs );
412 case 0: /* entry sent ok */
414 case 1: /* entry not sent */
416 case -1: /* connection closed */
417 cache_return_entry_r( &li->li_cache, e );
424 Debug( LDAP_DEBUG_TRACE,
425 "ldbm_search: candidate entry %ld scope not okay\n",
430 Debug( LDAP_DEBUG_TRACE,
431 "ldbm_search: candidate entry %ld does not match filter\n",
437 /* free reader lock */
438 cache_return_entry_r( &li->li_cache, e );
441 ldap_pvt_thread_yield();
444 rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
445 rs->sr_ref = rs->sr_v2ref;
446 send_ldap_result( op, rs );
451 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
453 if( candidates != NULL )
454 idl_free( candidates );
456 if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
457 if( realbase.bv_val ) free( realbase.bv_val );
469 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
473 idl = idl_alloc( 1 );
474 idl_insert( &idl, e->e_id, 1 );
488 ID_BLOCK *candidates;
489 Filter f, fand, rf, af, xf;
490 AttributeAssertion aa_ref, aa_alias;
491 struct berval bv_ref = { sizeof("referral")-1, "referral" };
492 struct berval bv_alias = { sizeof("alias")-1, "alias" };
493 #ifdef LDBM_SUBENTRIES
495 AttributeAssertion aa_subentry;
498 Debug(LDAP_DEBUG_TRACE,
499 "search_candidates: base=\"%s\" s=%d d=%d\n",
500 e->e_ndn, scope, deref );
504 xf.f_choice = LDAP_FILTER_OR;
508 /* match referrals */
509 rf.f_choice = LDAP_FILTER_EQUALITY;
511 rf.f_av_desc = slap_schema.si_ad_objectClass;
512 rf.f_av_value = bv_ref;
517 if( deref & LDAP_DEREF_SEARCHING ) {
519 af.f_choice = LDAP_FILTER_EQUALITY;
520 af.f_ava = &aa_alias;
521 af.f_av_desc = slap_schema.si_ad_objectClass;
522 af.f_av_value = bv_alias;
528 f.f_choice = LDAP_FILTER_AND;
530 fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
531 ? SLAPD_FILTER_DN_ONE
532 : SLAPD_FILTER_DN_SUBTREE;
533 fand.f_dn = &e->e_nname;
534 fand.f_next = xf.f_or == filter ? filter : &xf ;
536 #ifdef LDBM_SUBENTRIES
537 if ( get_subentries_visibility( op )) {
538 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
539 sf.f_choice = LDAP_FILTER_EQUALITY;
540 sf.f_ava = &aa_subentry;
541 sf.f_av_desc = slap_schema.si_ad_objectClass;
542 sf.f_av_value = bv_subentry;
543 sf.f_next = fand.f_next;
548 candidates = filter_candidates( op, &f );
549 return( candidates );