1 /* search.c - ldbm backend search function */
4 * Copyright 1998-2000 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 const char *filterstr,
40 struct berval **attrs,
43 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
45 const char *text = NULL;
50 struct berval **v2refs = NULL;
51 Entry *matched = NULL;
52 char *realbase = 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);
68 if ( nbase->bv_len == 0 ) {
69 /* DIT root special case */
70 e = (Entry *) &slap_entry_root;
72 /* need normalized dn below */
73 realbase = ch_strdup( e->e_ndn );
75 candidates = search_candidates( be, e, filter,
76 scope, deref, manageDSAit );
80 } else if ( deref & LDAP_DEREF_FINDING ) {
81 /* deref dn and get entry with reader lock */
82 e = deref_dn_r( be, nbase->bv_val, &err, &matched, &text );
84 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
87 /* get entry with reader lock */
88 e = dn2entry_r( be, nbase->bv_val, &matched );
89 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
94 struct berval *matched_dn = NULL;
95 struct berval **refs = NULL;
97 if ( matched != NULL ) {
98 struct berval **erefs;
99 matched_dn = ber_bvdup( &matched->e_name );
101 erefs = is_entry_referral( matched )
102 ? get_entry_referrals( be, conn, op, matched )
105 cache_return_entry_r( &li->li_cache, matched );
108 refs = referral_rewrite( erefs, matched_dn,
111 ber_bvecfree( erefs );
115 refs = referral_rewrite( default_referral,
119 send_ldap_result( conn, op, err,
120 matched_dn->bv_val, text, refs, NULL );
122 ber_bvecfree( refs );
123 ber_bvfree( matched_dn );
127 if (!manageDSAit && is_entry_referral( e ) ) {
128 /* entry is a referral, don't allow add */
129 struct berval *matched_dn = ber_bvdup( &e->e_name );
130 struct berval **erefs = get_entry_referrals( be,
132 struct berval **refs = NULL;
134 cache_return_entry_r( &li->li_cache, e );
137 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
138 "ldbm_search: entry (%s) is a referral.\n",
141 Debug( LDAP_DEBUG_TRACE,
142 "ldbm_search: entry is referral\n",
147 refs = referral_rewrite( erefs, matched_dn,
150 ber_bvecfree( erefs );
154 send_ldap_result( conn, op, LDAP_REFERRAL,
155 matched_dn->bv_val, NULL, refs, NULL );
156 ber_bvecfree( refs );
159 send_ldap_result( conn, op, LDAP_OTHER,
161 "bad referral object", NULL, NULL );
164 ber_bvfree( matched_dn );
168 if ( is_entry_alias( e ) ) {
170 deref = LDAP_DEREF_NEVER;
173 if ( scope == LDAP_SCOPE_BASE ) {
174 cscope = LDAP_SCOPE_BASE;
175 candidates = base_candidate( be, e );
178 cscope = ( scope != LDAP_SCOPE_SUBTREE )
179 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
180 candidates = search_candidates( be, e, filter,
181 scope, deref, manageDSAit );
184 /* need normalized dn below */
185 realbase = ch_strdup( e->e_ndn );
187 cache_return_entry_r( &li->li_cache, e );
190 if ( candidates == NULL ) {
193 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
194 "ldbm_search: no candidates\n" ));
196 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
200 send_search_result( conn, op,
202 NULL, NULL, NULL, NULL, 0 );
208 /* if not root, get appropriate limits */
209 if ( be_isroot( be, &op->o_ndn ) ) {
212 ( void ) get_limits( be, &op->o_ndn, &limit );
215 /* if candidates exceed to-be-checked entries, abort */
216 if ( !isroot && limit->lms_s_unchecked != -1 ) {
217 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
218 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
219 NULL, NULL, NULL, NULL, 0 );
225 /* if root an no specific limit is required, allow unlimited search */
236 /* if no limit is required, use soft limit */
238 tlimit = limit->lms_t_soft;
240 /* if requested limit higher than hard limit, abort */
241 } else if ( tlimit > limit->lms_t_hard ) {
242 /* no hard limit means use soft instead */
243 if ( limit->lms_t_hard == 0 ) {
244 tlimit = limit->lms_t_soft;
246 /* positive hard limit means abort */
247 } else if ( limit->lms_t_hard > 0 ) {
248 send_search_result( conn, op,
249 LDAP_UNWILLING_TO_PERFORM,
250 NULL, NULL, NULL, NULL, 0 );
255 /* negative hard limit means no limit */
258 /* if no limit is required, use soft limit */
260 slimit = limit->lms_s_soft;
262 /* if requested limit higher than hard limit, abort */
263 } else if ( slimit > limit->lms_s_hard ) {
264 /* no hard limit means use soft instead */
265 if ( limit->lms_s_hard == 0 ) {
266 slimit = limit->lms_s_soft;
268 /* positive hard limit means abort */
269 } else if ( limit->lms_s_hard > 0 ) {
270 send_search_result( conn, op,
271 LDAP_UNWILLING_TO_PERFORM,
272 NULL, NULL, NULL, NULL, 0 );
277 /* negative hard limit means no limit */
281 /* compute it anyway; root does not use it */
282 stoptime = op->o_time + tlimit;
284 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
285 id = idl_nextid( candidates, &cursor ) )
289 /* check for abandon */
290 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
292 if ( op->o_abandon ) {
293 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
298 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
300 /* check time limit */
301 if ( tlimit != -1 && slap_get_time() > stoptime ) {
302 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
303 NULL, NULL, v2refs, NULL, nentries );
308 /* get the entry with reader lock */
309 e = id2entry_r( be, id );
313 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
314 "ldbm_search: candidate %ld not found.\n", id ));
316 Debug( LDAP_DEBUG_TRACE,
317 "ldbm_search: candidate %ld not found\n",
324 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
329 e = deref_entry_r( be, e, &err, &matched, &text );
336 if( e->e_id == id ) {
341 /* need to skip alias which deref into scope */
342 if( scope & LDAP_SCOPE_ONELEVEL ) {
343 char *pdn = dn_parent( NULL, e->e_ndn );
345 if( strcmp( pdn, realbase ) ) {
351 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
352 /* alias is within scope */
354 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
355 "ldbm_search: alias \"%s\" in subtree\n", e->e_dn ));
357 Debug( LDAP_DEBUG_TRACE,
358 "ldbm_search: alias \"%s\" in subtree\n",
369 * if it's a referral, add it to the list of referrals. only do
370 * this for non-base searches, and don't check the filter
371 * explicitly here since it's only a candidate anyway.
373 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
374 is_entry_referral( e ) )
379 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
380 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
381 scopeok = (dn == realbase)
383 : (strcmp( dn, realbase ) ? 0 : 1 );
385 scopeok = (realbase == NULL || *realbase == '\0');
388 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
389 dn = ch_strdup( e->e_ndn );
390 scopeok = dn_issuffix( dn, realbase );
398 struct berval **erefs = get_entry_referrals(
400 struct berval **refs = referral_rewrite( erefs,
402 scope == LDAP_SCOPE_SUBTREE
406 send_search_reference( be, conn, op,
407 e, refs, NULL, &v2refs );
409 ber_bvecfree( refs );
413 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
414 "ldbm_search: candidate referral %ld scope not okay\n",
417 Debug( LDAP_DEBUG_TRACE,
418 "ldbm_search: candidate referral %ld scope not okay\n",
426 /* if it matches the filter and scope, send it */
427 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
431 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
432 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
433 scopeok = (dn == realbase)
435 : (strcmp( dn, realbase ) ? 0 : 1 );
437 scopeok = (realbase == NULL || *realbase == '\0');
440 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
441 dn = ch_strdup( e->e_ndn );
442 scopeok = dn_issuffix( dn, 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 if( candidates != NULL )
515 idl_free( candidates );
517 ber_bvecfree( v2refs );
518 if( realbase ) free( realbase );
531 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
532 "base_candidate: base (%s)\n", e->e_dn ));
534 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
539 idl = idl_alloc( 1 );
540 idl_insert( &idl, e->e_id, 1 );
554 ID_BLOCK *candidates;
555 Filter f, fand, rf, af, xf;
556 AttributeAssertion aa_ref, aa_alias;
557 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
558 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
561 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
562 "search_candidates: base (%s) scope %d deref %d\n",
563 e->e_ndn, scope, deref ));
565 Debug(LDAP_DEBUG_TRACE,
566 "search_candidates: base=\"%s\" s=%d d=%d\n",
567 e->e_ndn, scope, deref );
572 xf.f_choice = LDAP_FILTER_OR;
576 /* match referrals */
577 rf.f_choice = LDAP_FILTER_EQUALITY;
579 rf.f_av_desc = slap_schema.si_ad_objectClass;
580 rf.f_av_value = &bv_ref;
585 if( deref & LDAP_DEREF_SEARCHING ) {
587 af.f_choice = LDAP_FILTER_EQUALITY;
588 af.f_ava = &aa_alias;
589 af.f_av_desc = slap_schema.si_ad_objectClass;
590 af.f_av_value = &bv_alias;
596 f.f_choice = LDAP_FILTER_AND;
598 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
599 ? SLAPD_FILTER_DN_SUBTREE
600 : SLAPD_FILTER_DN_ONE;
601 fand.f_dn = e->e_ndn;
602 fand.f_next = xf.f_or == filter ? filter : &xf ;
604 candidates = filter_candidates( be, &f );
606 return( candidates );