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 = { 0, NULL };
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 = { 0, NULL };
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 );
351 && op->ors_scope == LDAP_SCOPE_SUBORDINATE )
353 scopeok = !dn_match( &e->e_nname, &realbase )
354 && dnIsSuffix( &e->e_nname, &realbase );
361 BerVarray erefs = get_entry_referrals( op, e );
362 rs->sr_ref = referral_rewrite( erefs,
364 op->ors_scope == LDAP_SCOPE_ONELEVEL
366 : LDAP_SCOPE_SUBTREE );
368 send_search_reference( op, rs );
370 ber_bvarray_free( rs->sr_ref );
375 LDAP_LOG( BACK_LDBM, DETAIL2,
376 "ldbm_search: candidate referral %ld scope not okay\n",
379 Debug( LDAP_DEBUG_TRACE,
380 "ldbm_search: candidate referral %ld scope not okay\n",
388 if ( !manageDSAit && is_entry_glue( e )) {
392 /* if it matches the filter and scope, send it */
393 result = test_filter( op, e, op->ors_filter );
395 if ( result == LDAP_COMPARE_TRUE ) {
399 if ( !scopeok && op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
400 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
401 dnParent( &e->e_nname, &dn );
402 scopeok = dn_match( &dn, &realbase );
404 scopeok = (realbase.bv_len == 0);
407 } else if ( !scopeok &&
408 op->ors_scope == LDAP_SCOPE_SUBTREE )
410 scopeok = dnIsSuffix( &e->e_nname, &realbase );
412 } else if ( !scopeok &&
413 op->ors_scope == LDAP_SCOPE_SUBORDINATE )
415 scopeok = !dn_match( &e->e_nname, &realbase )
416 && 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;
427 send_ldap_result( op, rs );
434 result = send_search_entry( op, rs );
437 case 0: /* entry sent ok */
439 case 1: /* entry not sent */
441 case -1: /* connection closed */
442 cache_return_entry_r( &li->li_cache, e );
450 LDAP_LOG( BACK_LDBM, DETAIL2,
451 "ldbm_search: candidate entry %ld scope not okay\n",
454 Debug( LDAP_DEBUG_TRACE,
455 "ldbm_search: candidate entry %ld scope not okay\n",
462 LDAP_LOG( BACK_LDBM, DETAIL2,
463 "ldbm_search: candidate entry %ld does not match filter\n",
466 Debug( LDAP_DEBUG_TRACE,
467 "ldbm_search: candidate entry %ld does not match filter\n",
474 /* free reader lock */
475 cache_return_entry_r( &li->li_cache, e );
478 ldap_pvt_thread_yield();
481 rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
482 rs->sr_ref = rs->sr_v2ref;
483 send_ldap_result( op, rs );
488 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
490 if( candidates != NULL )
491 idl_free( candidates );
493 if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
494 if( realbase.bv_val ) free( realbase.bv_val );
507 LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
509 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
514 idl = idl_alloc( 1 );
515 idl_insert( &idl, e->e_id, 1 );
529 ID_BLOCK *candidates;
530 Filter f, fand, rf, af, xf;
531 AttributeAssertion aa_ref, aa_alias;
532 struct berval bv_ref = { sizeof("referral")-1, "referral" };
533 struct berval bv_alias = { sizeof("alias")-1, "alias" };
534 #ifdef LDBM_SUBENTRIES
536 AttributeAssertion aa_subentry;
540 LDAP_LOG( BACK_LDBM, DETAIL1,
541 "search_candidates: base (%s) scope %d deref %d\n",
542 e->e_ndn, scope, deref );
544 Debug(LDAP_DEBUG_TRACE,
545 "search_candidates: base=\"%s\" s=%d d=%d\n",
546 e->e_ndn, scope, deref );
551 xf.f_choice = LDAP_FILTER_OR;
555 /* match referrals */
556 rf.f_choice = LDAP_FILTER_EQUALITY;
558 rf.f_av_desc = slap_schema.si_ad_objectClass;
559 rf.f_av_value = bv_ref;
564 if( deref & LDAP_DEREF_SEARCHING ) {
566 af.f_choice = LDAP_FILTER_EQUALITY;
567 af.f_ava = &aa_alias;
568 af.f_av_desc = slap_schema.si_ad_objectClass;
569 af.f_av_value = bv_alias;
575 f.f_choice = LDAP_FILTER_AND;
577 fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
578 ? SLAPD_FILTER_DN_ONE
579 : SLAPD_FILTER_DN_SUBTREE;
580 fand.f_dn = &e->e_nname;
581 fand.f_next = xf.f_or == filter ? filter : &xf ;
583 #ifdef LDBM_SUBENTRIES
584 if ( get_subentries_visibility( op )) {
585 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
586 sf.f_choice = LDAP_FILTER_EQUALITY;
587 sf.f_ava = &aa_subentry;
588 sf.f_av_desc = slap_schema.si_ad_objectClass;
589 sf.f_av_value = bv_subentry;
590 sf.f_next = fand.f_next;
595 candidates = filter_candidates( op, &f );
596 return( candidates );