1 /* search.c - ldbm backend search function */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2003 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 );
51 int cscope = LDAP_SCOPE_DEFAULT;
53 struct slap_limits_set *limit = NULL;
57 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_search: enter\n", 0, 0, 0 );
59 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
62 /* grab giant lock for reading */
63 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
65 if ( op->o_req_ndn.bv_len == 0 ) {
66 /* DIT root special case */
67 e = (Entry *) &slap_entry_root;
69 /* need normalized dn below */
70 ber_dupbv( &realbase, &e->e_nname );
72 candidates = search_candidates( op, e, op->oq_search.rs_filter,
73 op->oq_search.rs_scope, op->oq_search.rs_deref,
74 manageDSAit || get_domainScope(op) );
78 } else if ( op->oq_search.rs_deref & LDAP_DEREF_FINDING ) {
79 /* deref dn and get entry with reader lock */
80 e = deref_dn_r( op->o_bd, &op->o_req_ndn, &rs->sr_err, &matched, &rs->sr_text );
82 if( rs->sr_err == LDAP_NO_SUCH_OBJECT ) rs->sr_err = LDAP_REFERRAL;
85 /* get entry with reader lock */
86 e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched );
87 rs->sr_err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
92 struct berval matched_dn = { 0, NULL };
94 if ( matched != NULL ) {
96 ber_dupbv( &matched_dn, &matched->e_name );
98 erefs = is_entry_referral( matched )
99 ? get_entry_referrals( op, matched )
102 cache_return_entry_r( &li->li_cache, matched );
105 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
106 &op->o_req_dn, op->oq_search.rs_scope );
108 ber_bvarray_free( erefs );
112 rs->sr_ref = referral_rewrite( default_referral,
113 NULL, &op->o_req_dn, op->oq_search.rs_scope );
116 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
118 rs->sr_matched = matched_dn.bv_val;
119 send_ldap_result( op, rs );
121 ber_bvarray_free( rs->sr_ref );
122 ber_memfree( matched_dn.bv_val );
124 rs->sr_matched = NULL;
125 return LDAP_REFERRAL;
128 if (!manageDSAit && is_entry_referral( e ) ) {
129 /* entry is a referral, don't allow add */
130 struct berval matched_dn;
133 ber_dupbv( &matched_dn, &e->e_name );
134 erefs = get_entry_referrals( op, e );
137 cache_return_entry_r( &li->li_cache, e );
138 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
141 LDAP_LOG( BACK_LDBM, INFO,
142 "ldbm_search: entry (%s) is a referral.\n",
145 Debug( LDAP_DEBUG_TRACE,
146 "ldbm_search: entry is referral\n",
151 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
152 &op->o_req_dn, op->oq_search.rs_scope );
154 ber_bvarray_free( erefs );
157 rs->sr_matched = matched_dn.bv_val;
159 rs->sr_err = LDAP_REFERRAL;
160 send_ldap_result( op, rs );
161 ber_bvarray_free( rs->sr_ref );
164 send_ldap_error( op, rs, LDAP_OTHER,
165 "bad referral object" );
168 ber_memfree( matched_dn.bv_val );
170 rs->sr_matched = NULL;
174 if ( is_entry_alias( e ) ) {
176 op->oq_search.rs_deref = LDAP_DEREF_NEVER;
179 if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
180 cscope = LDAP_SCOPE_BASE;
181 candidates = base_candidate( op->o_bd, e );
184 cscope = ( op->oq_search.rs_scope != LDAP_SCOPE_SUBTREE )
185 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
186 candidates = search_candidates( op, e, op->oq_search.rs_filter,
187 op->oq_search.rs_scope, op->oq_search.rs_deref, manageDSAit );
190 /* need normalized dn below */
191 ber_dupbv( &realbase, &e->e_nname );
193 cache_return_entry_r( &li->li_cache, e );
196 if ( candidates == NULL ) {
199 LDAP_LOG( BACK_LDBM, INFO,
200 "ldbm_search: no candidates\n" , 0, 0, 0);
202 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
206 rs->sr_err = LDAP_SUCCESS;
207 send_ldap_result( op, rs );
213 /* if not root, get appropriate limits */
214 if ( be_isroot( op->o_bd, &op->o_ndn ) )
217 * FIXME: I'd consider this dangerous if someone
218 * uses isroot for anything but handling limits
222 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
225 /* if candidates exceed to-be-checked entries, abort */
226 if ( !isroot && limit->lms_s_unchecked != -1 ) {
227 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
228 send_ldap_error( op, rs, LDAP_ADMINLIMIT_EXCEEDED,
235 /* if root an no specific limit is required, allow unlimited search */
237 if ( op->oq_search.rs_tlimit == 0 ) {
238 op->oq_search.rs_tlimit = -1;
241 if ( op->oq_search.rs_slimit == 0 ) {
242 op->oq_search.rs_slimit = -1;
246 /* if no limit is required, use soft limit */
247 if ( op->oq_search.rs_tlimit <= 0 ) {
248 op->oq_search.rs_tlimit = limit->lms_t_soft;
250 /* if requested limit higher than hard limit, abort */
251 } else if ( op->oq_search.rs_tlimit > limit->lms_t_hard ) {
252 /* no hard limit means use soft instead */
253 if ( limit->lms_t_hard == 0
254 && limit->lms_t_soft > -1
255 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
256 op->oq_search.rs_tlimit = limit->lms_t_soft;
258 /* positive hard limit means abort */
259 } else if ( limit->lms_t_hard > 0 ) {
260 send_ldap_error( op, rs,
261 LDAP_ADMINLIMIT_EXCEEDED,
267 /* negative hard limit means no limit */
270 /* if no limit is required, use soft limit */
271 if ( op->oq_search.rs_slimit <= 0 ) {
272 op->oq_search.rs_slimit = limit->lms_s_soft;
274 /* if requested limit higher than hard limit, abort */
275 } else if ( op->oq_search.rs_slimit > limit->lms_s_hard ) {
276 /* no hard limit means use soft instead */
277 if ( limit->lms_s_hard == 0
278 && limit->lms_s_soft > -1
279 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
280 op->oq_search.rs_slimit = limit->lms_s_soft;
282 /* positive hard limit means abort */
283 } else if ( limit->lms_s_hard > 0 ) {
284 send_ldap_error( op, rs,
285 LDAP_ADMINLIMIT_EXCEEDED,
291 /* negative hard limit means no limit */
295 /* compute it anyway; root does not use it */
296 stoptime = op->o_time + op->oq_search.rs_tlimit;
297 rs->sr_attrs = op->oq_search.rs_attrs;
299 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
300 id = idl_nextid( candidates, &cursor ) )
305 /* check for abandon */
306 if ( op->o_abandon ) {
311 /* check time limit */
312 if ( op->oq_search.rs_tlimit != -1 && slap_get_time() > stoptime ) {
313 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
314 send_ldap_result( op, rs );
319 /* get the entry with reader lock */
320 e = id2entry_r( op->o_bd, id );
324 LDAP_LOG( BACK_LDBM, INFO,
325 "ldbm_search: candidate %ld not found.\n", id, 0, 0 );
327 Debug( LDAP_DEBUG_TRACE,
328 "ldbm_search: candidate %ld not found\n",
337 #ifdef LDBM_SUBENTRIES
338 if ( is_entry_subentry( e ) ) {
339 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
340 if(!get_subentries_visibility( op )) {
341 /* only subentries are visible */
344 } else if ( get_subentries( op ) &&
345 !get_subentries_visibility( op ))
347 /* only subentries are visible */
350 } else if ( get_subentries_visibility( op )) {
351 /* only subentries are visible */
356 if ( op->oq_search.rs_deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
361 e = deref_entry_r( op->o_bd, e, &err, &matched, &text );
368 if( e->e_id == id ) {
373 /* need to skip alias which deref into scope */
374 if( op->oq_search.rs_scope & LDAP_SCOPE_ONELEVEL ) {
376 dnParent( &e->e_nname, &pdn );
377 if ( ber_bvcmp( &pdn, &realbase ) ) {
381 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
382 /* alias is within scope */
384 LDAP_LOG( BACK_LDBM, DETAIL1,
385 "ldbm_search: alias \"%s\" in subtree\n", e->e_dn, 0, 0 );
387 Debug( LDAP_DEBUG_TRACE,
388 "ldbm_search: alias \"%s\" in subtree\n",
401 * if it's a referral, add it to the list of referrals. only do
402 * this for non-base searches, and don't check the filter
403 * explicitly here since it's only a candidate anyway.
405 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE &&
406 is_entry_referral( e ) )
411 if ( !scopeok && op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
412 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
413 dnParent( &e->e_nname, &dn );
414 scopeok = dn_match( &dn, &realbase );
416 scopeok = (realbase.bv_len == 0);
419 } else if ( !scopeok && op->oq_search.rs_scope == LDAP_SCOPE_SUBTREE ) {
420 scopeok = dnIsSuffix( &e->e_nname, &realbase );
427 BerVarray erefs = get_entry_referrals( op, e );
428 rs->sr_ref = referral_rewrite( erefs,
430 op->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
434 send_search_reference( op, rs );
436 ber_bvarray_free( rs->sr_ref );
441 LDAP_LOG( BACK_LDBM, DETAIL2,
442 "ldbm_search: candidate referral %ld scope not okay\n",
445 Debug( LDAP_DEBUG_TRACE,
446 "ldbm_search: candidate referral %ld scope not okay\n",
454 if ( !manageDSAit && is_entry_glue( e )) {
458 /* if it matches the filter and scope, send it */
459 result = test_filter( op, e, op->oq_search.rs_filter );
461 if ( result == LDAP_COMPARE_TRUE ) {
465 if ( !scopeok && op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
466 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
467 dnParent( &e->e_nname, &dn );
468 scopeok = dn_match( &dn, &realbase );
470 scopeok = (realbase.bv_len == 0);
473 } else if ( !scopeok && op->oq_search.rs_scope == LDAP_SCOPE_SUBTREE ) {
474 scopeok = dnIsSuffix( &e->e_nname, &realbase );
481 /* check size limit */
482 if ( --op->oq_search.rs_slimit == -1 ) {
483 cache_return_entry_r( &li->li_cache, e );
484 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
485 send_ldap_result( op, rs );
492 result = send_search_entry( op, rs );
495 case 0: /* entry sent ok */
497 case 1: /* entry not sent */
499 case -1: /* connection closed */
500 cache_return_entry_r( &li->li_cache, e );
507 LDAP_LOG( BACK_LDBM, DETAIL2,
508 "ldbm_search: candidate entry %ld scope not okay\n",
511 Debug( LDAP_DEBUG_TRACE,
512 "ldbm_search: candidate entry %ld scope not okay\n",
519 LDAP_LOG( BACK_LDBM, DETAIL2,
520 "ldbm_search: candidate entry %ld does not match filter\n",
523 Debug( LDAP_DEBUG_TRACE,
524 "ldbm_search: candidate entry %ld does not match filter\n",
531 /* free reader lock */
532 cache_return_entry_r( &li->li_cache, e );
535 ldap_pvt_thread_yield();
538 rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
539 rs->sr_ref = rs->sr_v2ref;
540 send_ldap_result( op, rs );
545 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
547 if( candidates != NULL )
548 idl_free( candidates );
550 if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
551 if( realbase.bv_val ) free( realbase.bv_val );
564 LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
566 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
571 idl = idl_alloc( 1 );
572 idl_insert( &idl, e->e_id, 1 );
586 ID_BLOCK *candidates;
587 Filter f, fand, rf, af, xf;
588 AttributeAssertion aa_ref, aa_alias;
589 struct berval bv_ref = { sizeof("referral")-1, "referral" };
590 struct berval bv_alias = { sizeof("alias")-1, "alias" };
591 #ifdef LDBM_SUBENTRIES
593 AttributeAssertion aa_subentry;
597 LDAP_LOG( BACK_LDBM, DETAIL1,
598 "search_candidates: base (%s) scope %d deref %d\n",
599 e->e_ndn, scope, deref );
601 Debug(LDAP_DEBUG_TRACE,
602 "search_candidates: base=\"%s\" s=%d d=%d\n",
603 e->e_ndn, scope, deref );
608 xf.f_choice = LDAP_FILTER_OR;
612 /* match referrals */
613 rf.f_choice = LDAP_FILTER_EQUALITY;
615 rf.f_av_desc = slap_schema.si_ad_objectClass;
616 rf.f_av_value = bv_ref;
621 if( deref & LDAP_DEREF_SEARCHING ) {
623 af.f_choice = LDAP_FILTER_EQUALITY;
624 af.f_ava = &aa_alias;
625 af.f_av_desc = slap_schema.si_ad_objectClass;
626 af.f_av_value = bv_alias;
632 f.f_choice = LDAP_FILTER_AND;
634 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
635 ? SLAPD_FILTER_DN_SUBTREE
636 : SLAPD_FILTER_DN_ONE;
637 fand.f_dn = &e->e_nname;
638 fand.f_next = xf.f_or == filter ? filter : &xf ;
640 #ifdef LDBM_SUBENTRIES
641 if ( get_subentries_visibility( op )) {
642 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
643 sf.f_choice = LDAP_FILTER_EQUALITY;
644 sf.f_ava = &aa_subentry;
645 sf.f_av_desc = slap_schema.si_ad_objectClass;
646 sf.f_av_value = bv_subentry;
647 sf.f_next = fand.f_next;
652 candidates = filter_candidates( op, &f );
654 return( candidates );