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 );
52 struct slap_limits_set *limit = NULL;
56 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_search: enter\n", 0, 0, 0 );
58 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
61 /* grab giant lock for reading */
62 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
64 if ( op->o_req_ndn.bv_len == 0 ) {
65 /* DIT root special case */
66 e = (Entry *) &slap_entry_root;
68 /* need normalized dn below */
69 ber_dupbv( &realbase, &e->e_nname );
71 candidates = search_candidates( op, e, op->oq_search.rs_filter,
72 op->oq_search.rs_scope, op->oq_search.rs_deref,
73 manageDSAit || get_domainScope(op) );
77 } else if ( op->oq_search.rs_deref & LDAP_DEREF_FINDING ) {
78 /* deref dn and get entry with reader lock */
79 e = deref_dn_r( op->o_bd, &op->o_req_ndn,
80 &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 candidates = base_candidate( op->o_bd, e );
183 candidates = search_candidates( op, e, op->oq_search.rs_filter,
184 op->oq_search.rs_scope, op->oq_search.rs_deref, manageDSAit );
187 /* need normalized dn below */
188 ber_dupbv( &realbase, &e->e_nname );
190 cache_return_entry_r( &li->li_cache, e );
193 if ( candidates == NULL ) {
196 LDAP_LOG( BACK_LDBM, INFO,
197 "ldbm_search: no candidates\n" , 0, 0, 0);
199 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
203 rs->sr_err = LDAP_SUCCESS;
204 send_ldap_result( op, rs );
210 /* if not root, get appropriate limits */
211 if ( be_isroot( op->o_bd, &op->o_ndn ) )
214 * FIXME: I'd consider this dangerous if someone
215 * uses isroot for anything but handling limits
219 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
222 /* if candidates exceed to-be-checked entries, abort */
223 if ( !isroot && limit->lms_s_unchecked != -1 ) {
224 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
225 send_ldap_error( op, rs, LDAP_ADMINLIMIT_EXCEEDED,
232 /* if root an no specific limit is required, allow unlimited search */
234 if ( op->oq_search.rs_tlimit == 0 ) {
235 op->oq_search.rs_tlimit = -1;
238 if ( op->oq_search.rs_slimit == 0 ) {
239 op->oq_search.rs_slimit = -1;
243 /* if no limit is required, use soft limit */
244 if ( op->oq_search.rs_tlimit <= 0 ) {
245 op->oq_search.rs_tlimit = limit->lms_t_soft;
247 /* if requested limit higher than hard limit, abort */
248 } else if ( op->oq_search.rs_tlimit > limit->lms_t_hard ) {
249 /* no hard limit means use soft instead */
250 if ( limit->lms_t_hard == 0
251 && limit->lms_t_soft > -1
252 && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
253 op->oq_search.rs_tlimit = limit->lms_t_soft;
255 /* positive hard limit means abort */
256 } else if ( limit->lms_t_hard > 0 ) {
257 send_ldap_error( op, rs,
258 LDAP_ADMINLIMIT_EXCEEDED,
264 /* negative hard limit means no limit */
267 /* if no limit is required, use soft limit */
268 if ( op->oq_search.rs_slimit <= 0 ) {
269 op->oq_search.rs_slimit = limit->lms_s_soft;
271 /* if requested limit higher than hard limit, abort */
272 } else if ( op->oq_search.rs_slimit > limit->lms_s_hard ) {
273 /* no hard limit means use soft instead */
274 if ( limit->lms_s_hard == 0
275 && limit->lms_s_soft > -1
276 && op->oq_search.rs_slimit > limit->lms_s_soft ) {
277 op->oq_search.rs_slimit = limit->lms_s_soft;
279 /* positive hard limit means abort */
280 } else if ( limit->lms_s_hard > 0 ) {
281 send_ldap_error( op, rs,
282 LDAP_ADMINLIMIT_EXCEEDED,
288 /* negative hard limit means no limit */
292 /* compute it anyway; root does not use it */
293 stoptime = op->o_time + op->oq_search.rs_tlimit;
294 rs->sr_attrs = op->oq_search.rs_attrs;
296 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
297 id = idl_nextid( candidates, &cursor ) )
302 /* check for abandon */
303 if ( op->o_abandon ) {
308 /* check time limit */
309 if ( op->oq_search.rs_tlimit != -1 && slap_get_time() > stoptime ) {
310 rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
311 send_ldap_result( op, rs );
316 /* get the entry with reader lock */
317 e = id2entry_r( op->o_bd, id );
321 LDAP_LOG( BACK_LDBM, INFO,
322 "ldbm_search: candidate %ld not found.\n", id, 0, 0 );
324 Debug( LDAP_DEBUG_TRACE,
325 "ldbm_search: candidate %ld not found\n",
334 #ifdef LDBM_SUBENTRIES
335 if ( is_entry_subentry( e ) ) {
336 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
337 if(!get_subentries_visibility( op )) {
338 /* only subentries are visible */
341 } else if ( get_subentries( op ) &&
342 !get_subentries_visibility( op ))
344 /* only subentries are visible */
347 } else if ( get_subentries_visibility( op )) {
348 /* only subentries are visible */
353 if ( op->oq_search.rs_deref & LDAP_DEREF_SEARCHING &&
354 is_entry_alias( e ) )
360 e = deref_entry_r( op->o_bd, e, &err, &matched, &text );
367 if( e->e_id == id ) {
372 /* need to skip alias which deref into scope */
373 if( op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
375 dnParent( &e->e_nname, &pdn );
376 if ( ber_bvcmp( &pdn, &realbase ) ) {
380 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
381 /* alias is within scope */
383 LDAP_LOG( BACK_LDBM, DETAIL1,
384 "ldbm_search: alias \"%s\" in subtree\n",
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.
402 * Only do this for non-base searches, and don't check
403 * the filter explicitly here since it's only a candidate
406 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE &&
407 is_entry_referral( e ) )
412 if ( !scopeok && op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
413 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
414 dnParent( &e->e_nname, &dn );
415 scopeok = dn_match( &dn, &realbase );
417 scopeok = (realbase.bv_len == 0);
421 && op->oq_search.rs_scope == LDAP_SCOPE_SUBTREE )
423 scopeok = dnIsSuffix( &e->e_nname, &realbase );
426 && op->oq_search.rs_scope == LDAP_SCOPE_SUBORDINATE )
428 scopeok = !dn_match( &e->e_nname, &realbase )
429 && dnIsSuffix( &e->e_nname, &realbase );
436 BerVarray erefs = get_entry_referrals( op, e );
437 rs->sr_ref = referral_rewrite( erefs,
439 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
441 : LDAP_SCOPE_SUBTREE );
443 send_search_reference( op, rs );
445 ber_bvarray_free( rs->sr_ref );
450 LDAP_LOG( BACK_LDBM, DETAIL2,
451 "ldbm_search: candidate referral %ld scope not okay\n",
454 Debug( LDAP_DEBUG_TRACE,
455 "ldbm_search: candidate referral %ld scope not okay\n",
463 if ( !manageDSAit && is_entry_glue( e )) {
467 /* if it matches the filter and scope, send it */
468 result = test_filter( op, e, op->oq_search.rs_filter );
470 if ( result == LDAP_COMPARE_TRUE ) {
474 if ( !scopeok && op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
475 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
476 dnParent( &e->e_nname, &dn );
477 scopeok = dn_match( &dn, &realbase );
479 scopeok = (realbase.bv_len == 0);
482 } else if ( !scopeok &&
483 op->oq_search.rs_scope == LDAP_SCOPE_SUBTREE )
485 scopeok = dnIsSuffix( &e->e_nname, &realbase );
487 } else if ( !scopeok &&
488 op->oq_search.rs_scope == LDAP_SCOPE_SUBORDINATE )
490 scopeok = !dn_match( &e->e_nname, &realbase )
491 && dnIsSuffix( &e->e_nname, &realbase );
498 /* check size limit */
499 if ( --op->oq_search.rs_slimit == -1 ) {
500 cache_return_entry_r( &li->li_cache, e );
501 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
502 send_ldap_result( op, rs );
508 result = send_search_entry( op, rs );
511 case 0: /* entry sent ok */
513 case 1: /* entry not sent */
515 case -1: /* connection closed */
516 cache_return_entry_r( &li->li_cache, e );
524 LDAP_LOG( BACK_LDBM, DETAIL2,
525 "ldbm_search: candidate entry %ld scope not okay\n",
528 Debug( LDAP_DEBUG_TRACE,
529 "ldbm_search: candidate entry %ld scope not okay\n",
536 LDAP_LOG( BACK_LDBM, DETAIL2,
537 "ldbm_search: candidate entry %ld does not match filter\n",
540 Debug( LDAP_DEBUG_TRACE,
541 "ldbm_search: candidate entry %ld does not match filter\n",
548 /* free reader lock */
549 cache_return_entry_r( &li->li_cache, e );
552 ldap_pvt_thread_yield();
555 rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
556 rs->sr_ref = rs->sr_v2ref;
557 send_ldap_result( op, rs );
562 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
564 if( candidates != NULL )
565 idl_free( candidates );
567 if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
568 if( realbase.bv_val ) free( realbase.bv_val );
581 LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
583 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
588 idl = idl_alloc( 1 );
589 idl_insert( &idl, e->e_id, 1 );
603 ID_BLOCK *candidates;
604 Filter f, fand, rf, af, xf;
605 AttributeAssertion aa_ref, aa_alias;
606 struct berval bv_ref = { sizeof("referral")-1, "referral" };
607 struct berval bv_alias = { sizeof("alias")-1, "alias" };
608 #ifdef LDBM_SUBENTRIES
610 AttributeAssertion aa_subentry;
614 LDAP_LOG( BACK_LDBM, DETAIL1,
615 "search_candidates: base (%s) scope %d deref %d\n",
616 e->e_ndn, scope, deref );
618 Debug(LDAP_DEBUG_TRACE,
619 "search_candidates: base=\"%s\" s=%d d=%d\n",
620 e->e_ndn, scope, deref );
625 xf.f_choice = LDAP_FILTER_OR;
629 /* match referrals */
630 rf.f_choice = LDAP_FILTER_EQUALITY;
632 rf.f_av_desc = slap_schema.si_ad_objectClass;
633 rf.f_av_value = bv_ref;
638 if( deref & LDAP_DEREF_SEARCHING ) {
640 af.f_choice = LDAP_FILTER_EQUALITY;
641 af.f_ava = &aa_alias;
642 af.f_av_desc = slap_schema.si_ad_objectClass;
643 af.f_av_value = bv_alias;
649 f.f_choice = LDAP_FILTER_AND;
651 fand.f_choice = scope == LDAP_SCOPE_ONELEVEL
652 ? SLAPD_FILTER_DN_ONE
653 : SLAPD_FILTER_DN_SUBTREE;
654 fand.f_dn = &e->e_nname;
655 fand.f_next = xf.f_or == filter ? filter : &xf ;
657 #ifdef LDBM_SUBENTRIES
658 if ( get_subentries_visibility( op )) {
659 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
660 sf.f_choice = LDAP_FILTER_EQUALITY;
661 sf.f_ava = &aa_subentry;
662 sf.f_av_desc = slap_schema.si_ad_objectClass;
663 sf.f_av_value = bv_subentry;
664 sf.f_next = fand.f_next;
669 candidates = filter_candidates( op, &f );
670 return( candidates );