1 /* search.c - ldbm backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 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 );
53 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_search: enter\n", 0, 0, 0 );
55 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
58 /* grab giant lock for reading */
59 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
61 if ( op->o_req_ndn.bv_len == 0 ) {
62 /* DIT root special case */
63 e = (Entry *) &slap_entry_root;
65 /* need normalized dn below */
66 ber_dupbv( &realbase, &e->e_nname );
68 candidates = search_candidates( op, e, op->ors_filter,
69 op->ors_scope, op->ors_deref,
70 manageDSAit || get_domainScope(op) );
74 } else if ( op->ors_deref & LDAP_DEREF_FINDING ) {
75 /* deref dn and get entry with reader lock */
76 e = deref_dn_r( op->o_bd, &op->o_req_ndn,
77 &rs->sr_err, &matched, &rs->sr_text );
79 if( rs->sr_err == LDAP_NO_SUCH_OBJECT ) rs->sr_err = LDAP_REFERRAL;
82 /* get entry with reader lock */
83 e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched );
84 rs->sr_err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
89 struct berval matched_dn = BER_BVNULL;
91 if ( matched != NULL ) {
93 ber_dupbv( &matched_dn, &matched->e_name );
95 erefs = is_entry_referral( matched )
96 ? get_entry_referrals( op, matched )
99 cache_return_entry_r( &li->li_cache, matched );
102 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
103 &op->o_req_dn, op->ors_scope );
105 ber_bvarray_free( erefs );
109 rs->sr_ref = referral_rewrite( default_referral,
110 NULL, &op->o_req_dn, op->ors_scope );
113 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
115 rs->sr_matched = matched_dn.bv_val;
116 send_ldap_result( op, rs );
118 ber_bvarray_free( rs->sr_ref );
119 ber_memfree( matched_dn.bv_val );
121 rs->sr_matched = NULL;
122 return LDAP_REFERRAL;
125 if (!manageDSAit && is_entry_referral( e ) ) {
126 /* entry is a referral, don't allow add */
127 struct berval matched_dn;
130 ber_dupbv( &matched_dn, &e->e_name );
131 erefs = get_entry_referrals( op, e );
134 cache_return_entry_r( &li->li_cache, e );
135 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
138 LDAP_LOG( BACK_LDBM, INFO,
139 "ldbm_search: entry (%s) is a referral.\n",
142 Debug( LDAP_DEBUG_TRACE,
143 "ldbm_search: entry is referral\n",
148 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
149 &op->o_req_dn, op->ors_scope );
151 ber_bvarray_free( erefs );
154 rs->sr_matched = matched_dn.bv_val;
156 rs->sr_err = LDAP_REFERRAL;
157 send_ldap_result( op, rs );
158 ber_bvarray_free( rs->sr_ref );
161 send_ldap_error( op, rs, LDAP_OTHER,
162 "bad referral object" );
165 ber_memfree( matched_dn.bv_val );
167 rs->sr_matched = NULL;
171 if ( is_entry_alias( e ) ) {
173 op->ors_deref = LDAP_DEREF_NEVER;
176 if ( op->ors_scope == LDAP_SCOPE_BASE ) {
177 candidates = base_candidate( op->o_bd, e );
180 candidates = search_candidates( op, e, op->ors_filter,
181 op->ors_scope, op->ors_deref, manageDSAit );
184 /* need normalized dn below */
185 ber_dupbv( &realbase, &e->e_nname );
187 cache_return_entry_r( &li->li_cache, e );
190 if ( candidates == NULL ) {
193 LDAP_LOG( BACK_LDBM, INFO,
194 "ldbm_search: no candidates\n" , 0, 0, 0);
196 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
200 rs->sr_err = LDAP_SUCCESS;
201 send_ldap_result( op, rs );
207 /* if candidates exceed to-be-checked entries, abort */
208 if ( op->ors_limit /* isroot == TRUE */
209 && op->ors_limit->lms_s_unchecked != -1
210 && ID_BLOCK_NIDS( candidates ) > (unsigned) op->ors_limit->lms_s_unchecked )
212 send_ldap_error( op, rs, LDAP_ADMINLIMIT_EXCEEDED, NULL );
217 /* compute it anyway; root does not use it */
218 stoptime = op->o_time + op->ors_tlimit;
219 rs->sr_attrs = op->ors_attrs;
221 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
222 id = idl_nextid( candidates, &cursor ) )
227 /* check for abandon */
228 if ( op->o_abandon ) {
233 /* check time limit */
234 if ( op->ors_tlimit != -1 && slap_get_time() > stoptime ) {
235 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
236 send_ldap_result( op, rs );
241 /* get the entry with reader lock */
242 e = id2entry_r( op->o_bd, id );
246 LDAP_LOG( BACK_LDBM, INFO,
247 "ldbm_search: candidate %ld not found.\n", id, 0, 0 );
249 Debug( LDAP_DEBUG_TRACE,
250 "ldbm_search: candidate %ld not found\n",
259 #ifdef LDBM_SUBENTRIES
260 if ( is_entry_subentry( e ) ) {
261 if( op->ors_scope != LDAP_SCOPE_BASE ) {
262 if(!get_subentries_visibility( op )) {
263 /* only subentries are visible */
266 } else if ( get_subentries( op ) &&
267 !get_subentries_visibility( op ))
269 /* only subentries are visible */
272 } else if ( get_subentries_visibility( op )) {
273 /* only subentries are visible */
278 if ( op->ors_deref & LDAP_DEREF_SEARCHING &&
279 is_entry_alias( e ) )
285 e = deref_entry_r( op->o_bd, e, &err, &matched, &text );
292 if( e->e_id == id ) {
297 /* need to skip alias which deref into scope */
298 if( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
300 dnParent( &e->e_nname, &pdn );
301 if ( ber_bvcmp( &pdn, &realbase ) ) {
305 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
306 /* alias is within scope */
308 LDAP_LOG( BACK_LDBM, DETAIL1,
309 "ldbm_search: alias \"%s\" in subtree\n",
312 Debug( LDAP_DEBUG_TRACE,
313 "ldbm_search: alias \"%s\" in subtree\n",
326 * If it's a referral, add it to the list of referrals.
327 * Only do this for non-base searches, and don't check
328 * the filter explicitly here since it's only a candidate
331 if ( !manageDSAit && op->ors_scope != LDAP_SCOPE_BASE &&
332 is_entry_referral( e ) )
337 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
338 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
339 dnParent( &e->e_nname, &dn );
340 scopeok = dn_match( &dn, &realbase );
342 scopeok = (realbase.bv_len == 0);
346 && op->ors_scope == LDAP_SCOPE_SUBTREE )
348 scopeok = dnIsSuffix( &e->e_nname, &realbase );
350 #ifdef LDAP_SCOPE_SUBORDINATE
352 && op->ors_scope == LDAP_SCOPE_SUBORDINATE )
354 scopeok = !dn_match( &e->e_nname, &realbase )
355 && dnIsSuffix( &e->e_nname, &realbase );
363 BerVarray erefs = get_entry_referrals( op, e );
364 rs->sr_ref = referral_rewrite( erefs,
366 op->ors_scope == LDAP_SCOPE_ONELEVEL
368 : LDAP_SCOPE_SUBTREE );
370 send_search_reference( op, rs );
372 ber_bvarray_free( rs->sr_ref );
377 LDAP_LOG( BACK_LDBM, DETAIL2,
378 "ldbm_search: candidate referral %ld scope not okay\n",
381 Debug( LDAP_DEBUG_TRACE,
382 "ldbm_search: candidate referral %ld scope not okay\n",
390 if ( !manageDSAit && is_entry_glue( e )) {
394 /* if it matches the filter and scope, send it */
395 result = test_filter( op, e, op->ors_filter );
397 if ( result == LDAP_COMPARE_TRUE ) {
401 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
402 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
403 dnParent( &e->e_nname, &dn );
404 scopeok = dn_match( &dn, &realbase );
406 scopeok = (realbase.bv_len == 0);
409 } else if ( !scopeok &&
410 op->ors_scope == LDAP_SCOPE_SUBTREE )
412 scopeok = dnIsSuffix( &e->e_nname, &realbase );
414 #ifdef LDAP_SCOPE_SUBORDINATE
415 } else if ( !scopeok &&
416 op->ors_scope == LDAP_SCOPE_SUBORDINATE )
418 scopeok = !dn_match( &e->e_nname, &realbase )
419 && dnIsSuffix( &e->e_nname, &realbase );
427 /* check size limit */
428 if ( --op->ors_slimit == -1 ) {
429 cache_return_entry_r( &li->li_cache, e );
430 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
432 send_ldap_result( op, rs );
439 result = send_search_entry( op, rs );
442 case 0: /* entry sent ok */
444 case 1: /* entry not sent */
446 case -1: /* connection closed */
447 cache_return_entry_r( &li->li_cache, e );
455 LDAP_LOG( BACK_LDBM, DETAIL2,
456 "ldbm_search: candidate entry %ld scope not okay\n",
459 Debug( LDAP_DEBUG_TRACE,
460 "ldbm_search: candidate entry %ld scope not okay\n",
467 LDAP_LOG( BACK_LDBM, DETAIL2,
468 "ldbm_search: candidate entry %ld does not match filter\n",
471 Debug( LDAP_DEBUG_TRACE,
472 "ldbm_search: candidate entry %ld does not match filter\n",
479 /* free reader lock */
480 cache_return_entry_r( &li->li_cache, e );
483 ldap_pvt_thread_yield();
486 rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
487 rs->sr_ref = rs->sr_v2ref;
488 send_ldap_result( op, rs );
493 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
495 if( candidates != NULL )
496 idl_free( candidates );
498 if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
499 if( realbase.bv_val ) free( realbase.bv_val );
512 LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
514 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
519 idl = idl_alloc( 1 );
520 idl_insert( &idl, e->e_id, 1 );
534 ID_BLOCK *candidates;
535 Filter f, fand, rf, af, xf;
536 AttributeAssertion aa_ref, aa_alias;
537 struct berval bv_ref = { sizeof("referral")-1, "referral" };
538 struct berval bv_alias = { sizeof("alias")-1, "alias" };
539 #ifdef LDBM_SUBENTRIES
541 AttributeAssertion aa_subentry;
545 LDAP_LOG( BACK_LDBM, DETAIL1,
546 "search_candidates: base (%s) scope %d deref %d\n",
547 e->e_ndn, scope, deref );
549 Debug(LDAP_DEBUG_TRACE,
550 "search_candidates: base=\"%s\" s=%d d=%d\n",
551 e->e_ndn, scope, deref );
556 xf.f_choice = LDAP_FILTER_OR;
560 /* match referrals */
561 rf.f_choice = LDAP_FILTER_EQUALITY;
563 rf.f_av_desc = slap_schema.si_ad_objectClass;
564 rf.f_av_value = bv_ref;
569 if( deref & LDAP_DEREF_SEARCHING ) {
571 af.f_choice = LDAP_FILTER_EQUALITY;
572 af.f_ava = &aa_alias;
573 af.f_av_desc = slap_schema.si_ad_objectClass;
574 af.f_av_value = bv_alias;
580 f.f_choice = LDAP_FILTER_AND;
582 fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
583 ? SLAPD_FILTER_DN_ONE
584 : SLAPD_FILTER_DN_SUBTREE;
585 fand.f_dn = &e->e_nname;
586 fand.f_next = xf.f_or == filter ? filter : &xf ;
588 #ifdef LDBM_SUBENTRIES
589 if ( get_subentries_visibility( op )) {
590 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
591 sf.f_choice = LDAP_FILTER_EQUALITY;
592 sf.f_ava = &aa_subentry;
593 sf.f_av_desc = slap_schema.si_ad_objectClass;
594 sf.f_av_value = bv_subentry;
595 sf.f_next = fand.f_next;
600 candidates = filter_candidates( op, &f );
601 return( candidates );