1 /* search.c - ldbm backend search function */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/string.h>
13 #include <ac/socket.h>
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
19 static ID_BLOCK *base_candidate(
20 Backend *be, Entry *e );
22 static ID_BLOCK *search_candidates(
23 Backend *be, Entry *e, Filter *filter,
24 int scope, int deref, int manageDSAit );
39 struct berval *filterstr,
43 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
45 const char *text = NULL;
50 BerVarray v2refs = NULL;
51 Entry *matched = NULL;
52 struct berval realbase = { 0, NULL };
54 int manageDSAit = get_manageDSAit( op );
55 int cscope = LDAP_SCOPE_DEFAULT;
58 Entry cache_base_entry;
59 #endif /* LDAP_CACHING */
61 struct slap_limits_set *limit = NULL;
65 LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_search: enter\n", 0, 0, 0 );
67 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
70 /* grab giant lock for reading */
71 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
74 if ( nbase->bv_len == 0 ) {
75 /* DIT root special case */
76 e = (Entry *) &slap_entry_root;
78 /* need normalized dn below */
79 ber_dupbv( &realbase, &e->e_nname );
81 #else /* LDAP_CACHING */
82 if ( op->o_caching_on || nbase->bv_len == 0 ) {
83 if (nbase->bv_len == 0) {
84 e = (Entry *) &slap_entry_root;
85 /* need normalized dn below */
86 ber_dupbv( &realbase, &e->e_nname );
88 if ((scope == LDAP_SCOPE_BASE)
89 && (e = dn2entry_r( be, nbase, &matched )))
91 candidates = base_candidate(be,e);
92 cache_return_entry_r( &li->li_cache, e );
95 cache_base_entry.e_nname = *nbase;
96 e = &cache_base_entry;
98 #endif /* LDAP_CACHING */
100 candidates = search_candidates( be, e, filter,
102 manageDSAit || get_domainScope(op) );
106 } else if ( deref & LDAP_DEREF_FINDING ) {
107 /* deref dn and get entry with reader lock */
108 e = deref_dn_r( be, nbase, &err, &matched, &text );
110 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
113 /* get entry with reader lock */
114 e = dn2entry_r( be, nbase, &matched );
115 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
120 struct berval matched_dn = { 0, NULL };
121 BerVarray refs = NULL;
123 if ( matched != NULL ) {
125 ber_dupbv( &matched_dn, &matched->e_name );
127 erefs = is_entry_referral( matched )
128 ? get_entry_referrals( be, conn, op, matched )
131 cache_return_entry_r( &li->li_cache, matched );
134 refs = referral_rewrite( erefs, &matched_dn,
137 ber_bvarray_free( erefs );
141 refs = referral_rewrite( default_referral,
145 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
147 send_ldap_result( conn, op, err, matched_dn.bv_val,
150 ber_bvarray_free( refs );
151 ber_memfree( matched_dn.bv_val );
155 if (!manageDSAit && is_entry_referral( e ) ) {
156 /* entry is a referral, don't allow add */
157 struct berval matched_dn;
161 ber_dupbv( &matched_dn, &e->e_name );
162 erefs = get_entry_referrals( be, conn, op, e );
165 cache_return_entry_r( &li->li_cache, e );
166 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
169 LDAP_LOG( BACK_LDBM, INFO,
170 "ldbm_search: entry (%s) is a referral.\n",
173 Debug( LDAP_DEBUG_TRACE,
174 "ldbm_search: entry is referral\n",
179 refs = referral_rewrite( erefs, &matched_dn,
182 ber_bvarray_free( erefs );
186 send_ldap_result( conn, op, LDAP_REFERRAL,
187 matched_dn.bv_val, NULL, refs, NULL );
188 ber_bvarray_free( refs );
191 send_ldap_result( conn, op, LDAP_OTHER,
193 "bad referral object", NULL, NULL );
196 ber_memfree( matched_dn.bv_val );
200 if ( is_entry_alias( e ) ) {
202 deref = LDAP_DEREF_NEVER;
205 if ( scope == LDAP_SCOPE_BASE ) {
206 cscope = LDAP_SCOPE_BASE;
207 candidates = base_candidate( be, e );
210 cscope = ( scope != LDAP_SCOPE_SUBTREE )
211 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
212 candidates = search_candidates( be, e, filter,
213 scope, deref, manageDSAit );
216 /* need normalized dn below */
217 ber_dupbv( &realbase, &e->e_nname );
219 cache_return_entry_r( &li->li_cache, e );
222 if ( candidates == NULL ) {
225 LDAP_LOG( BACK_LDBM, INFO,
226 "ldbm_search: no candidates\n" , 0, 0, 0);
228 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
232 if ( op->o_caching_on ) {
233 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
235 #endif /* LDAP_CACHING */
237 send_search_result( conn, op,
239 NULL, NULL, NULL, NULL, 0 );
242 if ( op->o_caching_on ) {
243 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
245 #endif /* LDAP_CACHING */
251 /* if not root, get appropriate limits */
253 if ( be_isroot( be, &op->o_ndn ) )
254 #else /* LDAP_CACHING */
255 if ( op->o_caching_on || be_isroot( be, &op->o_ndn ) )
256 #endif /* LDAP_CACHING */
259 * FIXME: I'd consider this dangerous if someone
260 * uses isroot for anything but handling limits
264 ( void ) get_limits( be, &op->o_ndn, &limit );
267 /* if candidates exceed to-be-checked entries, abort */
268 if ( !isroot && limit->lms_s_unchecked != -1 ) {
269 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
270 send_search_result( conn, op, LDAP_ADMINLIMIT_EXCEEDED,
271 NULL, NULL, NULL, NULL, 0 );
277 /* if root an no specific limit is required, allow unlimited search */
288 /* if no limit is required, use soft limit */
290 tlimit = limit->lms_t_soft;
292 /* if requested limit higher than hard limit, abort */
293 } else if ( tlimit > limit->lms_t_hard ) {
294 /* no hard limit means use soft instead */
295 if ( limit->lms_t_hard == 0
296 && limit->lms_t_soft > -1
297 && tlimit > limit->lms_t_soft ) {
298 tlimit = limit->lms_t_soft;
300 /* positive hard limit means abort */
301 } else if ( limit->lms_t_hard > 0 ) {
302 send_search_result( conn, op,
303 LDAP_ADMINLIMIT_EXCEEDED,
304 NULL, NULL, NULL, NULL, 0 );
309 /* negative hard limit means no limit */
312 /* if no limit is required, use soft limit */
314 slimit = limit->lms_s_soft;
316 /* if requested limit higher than hard limit, abort */
317 } else if ( slimit > limit->lms_s_hard ) {
318 /* no hard limit means use soft instead */
319 if ( limit->lms_s_hard == 0
320 && limit->lms_s_soft > -1
321 && slimit > limit->lms_s_soft ) {
322 slimit = limit->lms_s_soft;
324 /* positive hard limit means abort */
325 } else if ( limit->lms_s_hard > 0 ) {
326 send_search_result( conn, op,
327 LDAP_ADMINLIMIT_EXCEEDED,
328 NULL, NULL, NULL, NULL, 0 );
333 /* negative hard limit means no limit */
337 /* compute it anyway; root does not use it */
338 stoptime = op->o_time + tlimit;
340 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
341 id = idl_nextid( candidates, &cursor ) )
346 /* check for abandon */
347 if ( op->o_abandon ) {
352 /* check time limit */
353 if ( tlimit != -1 && slap_get_time() > stoptime ) {
354 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
355 NULL, NULL, v2refs, NULL, nentries );
360 /* get the entry with reader lock */
361 e = id2entry_r( be, id );
365 LDAP_LOG( BACK_LDBM, INFO,
366 "ldbm_search: candidate %ld not found.\n", id, 0, 0 );
368 Debug( LDAP_DEBUG_TRACE,
369 "ldbm_search: candidate %ld not found\n",
377 if ( !op->o_caching_on ) {
378 #endif /* LDAP_CACHING */
380 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
385 e = deref_entry_r( be, e, &err, &matched, &text );
392 if( e->e_id == id ) {
397 /* need to skip alias which deref into scope */
398 if( scope & LDAP_SCOPE_ONELEVEL ) {
400 dnParent( &e->e_nname, &pdn );
401 if ( ber_bvcmp( &pdn, &realbase ) ) {
405 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
406 /* alias is within scope */
408 LDAP_LOG( BACK_LDBM, DETAIL1,
409 "ldbm_search: alias \"%s\" in subtree\n", e->e_dn, 0, 0 );
411 Debug( LDAP_DEBUG_TRACE,
412 "ldbm_search: alias \"%s\" in subtree\n",
423 * if it's a referral, add it to the list of referrals. only do
424 * this for non-base searches, and don't check the filter
425 * explicitly here since it's only a candidate anyway.
427 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
428 is_entry_referral( e ) )
433 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
434 if ( !be_issuffix( be, &e->e_nname ) ) {
435 dnParent( &e->e_nname, &dn );
436 scopeok = dn_match( &dn, &realbase );
438 scopeok = (realbase.bv_len == 0);
441 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
442 scopeok = dnIsSuffix( &e->e_nname, &realbase );
449 BerVarray erefs = get_entry_referrals(
451 BerVarray refs = referral_rewrite( erefs,
453 scope == LDAP_SCOPE_SUBTREE
457 send_search_reference( be, conn, op,
458 e, refs, NULL, &v2refs );
460 ber_bvarray_free( refs );
464 LDAP_LOG( BACK_LDBM, DETAIL2,
465 "ldbm_search: candidate referral %ld scope not okay\n",
468 Debug( LDAP_DEBUG_TRACE,
469 "ldbm_search: candidate referral %ld scope not okay\n",
479 #endif /* LDAP_CACHING */
481 /* if it matches the filter and scope, send it */
482 result = test_filter( be, conn, op, e, filter );
484 if ( result == LDAP_COMPARE_TRUE ) {
488 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
489 if ( !be_issuffix( be, &e->e_nname ) ) {
490 dnParent( &e->e_nname, &dn );
491 scopeok = dn_match( &dn, &realbase );
493 scopeok = (realbase.bv_len == 0);
496 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
497 scopeok = dnIsSuffix( &e->e_nname, &realbase );
504 /* check size limit */
505 if ( --slimit == -1 ) {
506 cache_return_entry_r( &li->li_cache, e );
507 send_search_result( conn, op,
508 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
509 v2refs, NULL, nentries );
517 if ( op->o_caching_on ) {
518 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
519 cache_return_entry_r( &li->li_cache, e );
521 #endif /* LDAP_CACHING */
523 result = send_search_entry(be, conn, op,
524 e, attrs, attrsonly, NULL);
527 if ( op->o_caching_on ) {
528 ldap_pvt_thread_rdwr_rlock( &li->li_giant_rwlock );
530 #endif /* LDAP_CACHING */
534 case 0: /* entry sent ok */
537 case 1: /* entry not sent */
539 case -1: /* connection closed */
540 cache_return_entry_r( &li->li_cache, e );
547 LDAP_LOG( BACK_LDBM, DETAIL2,
548 "ldbm_search: candidate entry %ld scope not okay\n",
551 Debug( LDAP_DEBUG_TRACE,
552 "ldbm_search: candidate entry %ld scope not okay\n",
559 LDAP_LOG( BACK_LDBM, DETAIL2,
560 "ldbm_search: candidate entry %ld does not match filter\n",
563 Debug( LDAP_DEBUG_TRACE,
564 "ldbm_search: candidate entry %ld does not match filter\n",
571 /* free reader lock */
573 cache_return_entry_r( &li->li_cache, e );
574 #else /* LDAP_CACHING */
575 if ( !op->o_caching_on ) {
576 cache_return_entry_r( &li->li_cache, e );
578 #endif /* LDAP_CACHING */
581 ldap_pvt_thread_yield();
584 send_search_result( conn, op,
585 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
586 NULL, NULL, v2refs, NULL, nentries );
591 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
593 if( candidates != NULL )
594 idl_free( candidates );
596 if( v2refs ) ber_bvarray_free( v2refs );
597 if( realbase.bv_val ) free( realbase.bv_val );
610 LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
612 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
617 idl = idl_alloc( 1 );
618 idl_insert( &idl, e->e_id, 1 );
632 ID_BLOCK *candidates;
633 Filter f, fand, rf, af, xf;
634 AttributeAssertion aa_ref, aa_alias;
635 struct berval bv_ref = { sizeof("referral")-1, "referral" };
636 struct berval bv_alias = { sizeof("alias")-1, "alias" };
639 LDAP_LOG( BACK_LDBM, DETAIL1,
640 "search_candidates: base (%s) scope %d deref %d\n",
641 e->e_ndn, scope, deref );
643 Debug(LDAP_DEBUG_TRACE,
644 "search_candidates: base=\"%s\" s=%d d=%d\n",
645 e->e_ndn, scope, deref );
650 xf.f_choice = LDAP_FILTER_OR;
654 /* match referrals */
655 rf.f_choice = LDAP_FILTER_EQUALITY;
657 rf.f_av_desc = slap_schema.si_ad_objectClass;
658 rf.f_av_value = bv_ref;
663 if( deref & LDAP_DEREF_SEARCHING ) {
665 af.f_choice = LDAP_FILTER_EQUALITY;
666 af.f_ava = &aa_alias;
667 af.f_av_desc = slap_schema.si_ad_objectClass;
668 af.f_av_value = bv_alias;
674 f.f_choice = LDAP_FILTER_AND;
676 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
677 ? SLAPD_FILTER_DN_SUBTREE
678 : SLAPD_FILTER_DN_ONE;
679 fand.f_dn = &e->e_nname;
680 fand.f_next = xf.f_or == filter ? filter : &xf ;
682 candidates = filter_candidates( be, &f );
684 return( candidates );