1 /* search.c - ldbm backend search function */
4 * Copyright 1998-2002 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;
57 struct slap_limits_set *limit = NULL;
61 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
62 "ldbm_back_search: enter\n" ));
64 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
67 /* grab giant lock for reading */
68 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
70 if ( nbase->bv_len == 0 ) {
71 /* DIT root special case */
72 e = (Entry *) &slap_entry_root;
74 /* need normalized dn below */
75 ber_dupbv( &realbase, &e->e_nname );
77 candidates = search_candidates( be, e, filter,
78 scope, deref, manageDSAit );
82 } else if ( deref & LDAP_DEREF_FINDING ) {
83 /* deref dn and get entry with reader lock */
84 e = deref_dn_r( be, nbase, &err, &matched, &text );
86 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
89 /* get entry with reader lock */
90 e = dn2entry_r( be, nbase, &matched );
91 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
96 struct berval matched_dn = { 0, NULL };
97 BerVarray refs = NULL;
99 if ( matched != NULL ) {
101 ber_dupbv( &matched_dn, &matched->e_name );
103 erefs = is_entry_referral( matched )
104 ? get_entry_referrals( be, conn, op, matched )
107 cache_return_entry_r( &li->li_cache, matched );
110 refs = referral_rewrite( erefs, &matched_dn,
113 ber_bvarray_free( erefs );
117 refs = referral_rewrite( default_referral,
121 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
123 send_ldap_result( conn, op, err, matched_dn.bv_val,
126 ber_bvarray_free( refs );
127 ber_memfree( matched_dn.bv_val );
131 if (!manageDSAit && is_entry_referral( e ) ) {
132 /* entry is a referral, don't allow add */
133 struct berval matched_dn;
137 ber_dupbv( &matched_dn, &e->e_name );
138 erefs = get_entry_referrals( be, conn, op, e );
141 cache_return_entry_r( &li->li_cache, e );
142 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
145 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
146 "ldbm_search: entry (%s) is a referral.\n",
149 Debug( LDAP_DEBUG_TRACE,
150 "ldbm_search: entry is referral\n",
155 refs = referral_rewrite( erefs, &matched_dn,
158 ber_bvarray_free( erefs );
162 send_ldap_result( conn, op, LDAP_REFERRAL,
163 matched_dn.bv_val, NULL, refs, NULL );
164 ber_bvarray_free( refs );
167 send_ldap_result( conn, op, LDAP_OTHER,
169 "bad referral object", NULL, NULL );
172 ber_memfree( matched_dn.bv_val );
176 if ( is_entry_alias( e ) ) {
178 deref = LDAP_DEREF_NEVER;
181 if ( scope == LDAP_SCOPE_BASE ) {
182 cscope = LDAP_SCOPE_BASE;
183 candidates = base_candidate( be, e );
186 cscope = ( scope != LDAP_SCOPE_SUBTREE )
187 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
188 candidates = search_candidates( be, e, filter,
189 scope, deref, manageDSAit );
192 /* need normalized dn below */
193 ber_dupbv( &realbase, &e->e_nname );
195 cache_return_entry_r( &li->li_cache, e );
198 if ( candidates == NULL ) {
201 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
202 "ldbm_search: no candidates\n" ));
204 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
208 send_search_result( conn, op,
210 NULL, NULL, NULL, NULL, 0 );
216 /* if not root, get appropriate limits */
217 if ( be_isroot( be, &op->o_ndn ) ) {
220 ( void ) get_limits( be, &op->o_ndn, &limit );
223 /* if candidates exceed to-be-checked entries, abort */
224 if ( !isroot && limit->lms_s_unchecked != -1 ) {
225 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
226 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
227 NULL, NULL, NULL, NULL, 0 );
233 /* if root an no specific limit is required, allow unlimited search */
244 /* if no limit is required, use soft limit */
246 tlimit = limit->lms_t_soft;
248 /* if requested limit higher than hard limit, abort */
249 } else if ( tlimit > limit->lms_t_hard ) {
250 /* no hard limit means use soft instead */
251 if ( limit->lms_t_hard == 0 ) {
252 tlimit = limit->lms_t_soft;
254 /* positive hard limit means abort */
255 } else if ( limit->lms_t_hard > 0 ) {
256 send_search_result( conn, op,
257 LDAP_UNWILLING_TO_PERFORM,
258 NULL, NULL, NULL, NULL, 0 );
263 /* negative hard limit means no limit */
266 /* if no limit is required, use soft limit */
268 slimit = limit->lms_s_soft;
270 /* if requested limit higher than hard limit, abort */
271 } else if ( slimit > limit->lms_s_hard ) {
272 /* no hard limit means use soft instead */
273 if ( limit->lms_s_hard == 0 ) {
274 slimit = limit->lms_s_soft;
276 /* positive hard limit means abort */
277 } else if ( limit->lms_s_hard > 0 ) {
278 send_search_result( conn, op,
279 LDAP_UNWILLING_TO_PERFORM,
280 NULL, NULL, NULL, NULL, 0 );
285 /* negative hard limit means no limit */
289 /* compute it anyway; root does not use it */
290 stoptime = op->o_time + tlimit;
292 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
293 id = idl_nextid( candidates, &cursor ) )
297 /* check for abandon */
298 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
300 if ( op->o_abandon ) {
301 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
306 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
308 /* check time limit */
309 if ( tlimit != -1 && slap_get_time() > stoptime ) {
310 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
311 NULL, NULL, v2refs, NULL, nentries );
316 /* get the entry with reader lock */
317 e = id2entry_r( be, id );
321 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
322 "ldbm_search: candidate %ld not found.\n", id ));
324 Debug( LDAP_DEBUG_TRACE,
325 "ldbm_search: candidate %ld not found\n",
332 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
337 e = deref_entry_r( be, e, &err, &matched, &text );
344 if( e->e_id == id ) {
349 /* need to skip alias which deref into scope */
350 if( scope & LDAP_SCOPE_ONELEVEL ) {
352 dnParent( &e->e_nname, &pdn );
353 if ( ber_bvcmp( &pdn, &realbase ) ) {
357 } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
358 /* alias is within scope */
360 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
361 "ldbm_search: alias \"%s\" in subtree\n", e->e_dn ));
363 Debug( LDAP_DEBUG_TRACE,
364 "ldbm_search: alias \"%s\" in subtree\n",
375 * if it's a referral, add it to the list of referrals. only do
376 * this for non-base searches, and don't check the filter
377 * explicitly here since it's only a candidate anyway.
379 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
380 is_entry_referral( e ) )
385 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
386 if ( !be_issuffix( be, &e->e_nname ) ) {
387 dnParent( &e->e_nname, &dn );
388 scopeok = dn_match( &dn, &realbase );
390 scopeok = (realbase.bv_len == 0);
393 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
394 scopeok = dnIsSuffix( &e->e_nname, &realbase );
401 BerVarray erefs = get_entry_referrals(
403 BerVarray refs = referral_rewrite( erefs,
405 scope == LDAP_SCOPE_SUBTREE
409 send_search_reference( be, conn, op,
410 e, refs, NULL, &v2refs );
412 ber_bvarray_free( refs );
416 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
417 "ldbm_search: candidate referral %ld scope not okay\n",
420 Debug( LDAP_DEBUG_TRACE,
421 "ldbm_search: candidate referral %ld scope not okay\n",
429 /* if it matches the filter and scope, send it */
430 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
434 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
435 if ( !be_issuffix( be, &e->e_nname ) ) {
436 dnParent( &e->e_nname, &dn );
437 scopeok = dn_match( &dn, &realbase );
439 scopeok = (realbase.bv_len == 0);
442 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
443 scopeok = dnIsSuffix( &e->e_nname, &realbase );
450 /* check size limit */
451 if ( --slimit == -1 ) {
452 cache_return_entry_r( &li->li_cache, e );
453 send_search_result( conn, op,
454 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
455 v2refs, NULL, nentries );
461 int result = send_search_entry(be, conn, op,
462 e, attrs, attrsonly, NULL);
465 case 0: /* entry sent ok */
468 case 1: /* entry not sent */
470 case -1: /* connection closed */
471 cache_return_entry_r( &li->li_cache, e );
478 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
479 "ldbm_search: candidate entry %ld scope not okay\n", id ));
481 Debug( LDAP_DEBUG_TRACE,
482 "ldbm_search: candidate entry %ld scope not okay\n",
489 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
490 "ldbm_search: candidate entry %ld does not match filter\n", id ));
492 Debug( LDAP_DEBUG_TRACE,
493 "ldbm_search: candidate entry %ld does not match filter\n",
500 /* free reader lock */
501 cache_return_entry_r( &li->li_cache, e );
504 ldap_pvt_thread_yield();
507 send_search_result( conn, op,
508 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
509 NULL, NULL, v2refs, NULL, nentries );
514 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
516 if( candidates != NULL )
517 idl_free( candidates );
519 if( v2refs ) ber_bvarray_free( v2refs );
520 if( realbase.bv_val ) free( realbase.bv_val );
533 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
534 "base_candidate: base (%s)\n", e->e_dn ));
536 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
541 idl = idl_alloc( 1 );
542 idl_insert( &idl, e->e_id, 1 );
556 ID_BLOCK *candidates;
557 Filter f, fand, rf, af, xf;
558 AttributeAssertion aa_ref, aa_alias;
559 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
560 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
563 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
564 "search_candidates: base (%s) scope %d deref %d\n",
565 e->e_ndn, scope, deref ));
567 Debug(LDAP_DEBUG_TRACE,
568 "search_candidates: base=\"%s\" s=%d d=%d\n",
569 e->e_ndn, scope, deref );
574 xf.f_choice = LDAP_FILTER_OR;
578 /* match referrals */
579 rf.f_choice = LDAP_FILTER_EQUALITY;
581 rf.f_av_desc = slap_schema.si_ad_objectClass;
582 rf.f_av_value = bv_ref;
587 if( deref & LDAP_DEREF_SEARCHING ) {
589 af.f_choice = LDAP_FILTER_EQUALITY;
590 af.f_ava = &aa_alias;
591 af.f_av_desc = slap_schema.si_ad_objectClass;
592 af.f_av_value = bv_alias;
598 f.f_choice = LDAP_FILTER_AND;
600 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
601 ? SLAPD_FILTER_DN_SUBTREE
602 : SLAPD_FILTER_DN_ONE;
603 fand.f_dn = &e->e_nname;
604 fand.f_next = xf.f_or == filter ? filter : &xf ;
606 candidates = filter_candidates( be, &f );
608 return( candidates );