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, &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 ? matched_dn->bv_val : NULL,
123 ber_bvecfree( refs );
124 ber_bvfree( matched_dn );
128 if (!manageDSAit && is_entry_referral( e ) ) {
129 /* entry is a referral, don't allow add */
130 struct berval *matched_dn = ber_bvdup( &e->e_name );
131 struct berval **erefs = get_entry_referrals( be,
133 struct berval **refs = NULL;
135 cache_return_entry_r( &li->li_cache, e );
138 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
139 "ldbm_search: entry (%s) is a referral.\n",
142 Debug( LDAP_DEBUG_TRACE,
143 "ldbm_search: entry is referral\n",
148 refs = referral_rewrite( erefs, matched_dn,
151 ber_bvecfree( erefs );
155 send_ldap_result( conn, op, LDAP_REFERRAL,
156 matched_dn->bv_val, NULL, refs, NULL );
157 ber_bvecfree( refs );
160 send_ldap_result( conn, op, LDAP_OTHER,
162 "bad referral object", NULL, NULL );
165 ber_bvfree( matched_dn );
169 if ( is_entry_alias( e ) ) {
171 deref = LDAP_DEREF_NEVER;
174 if ( scope == LDAP_SCOPE_BASE ) {
175 cscope = LDAP_SCOPE_BASE;
176 candidates = base_candidate( be, e );
179 cscope = ( scope != LDAP_SCOPE_SUBTREE )
180 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
181 candidates = search_candidates( be, e, filter,
182 scope, deref, manageDSAit );
185 /* need normalized dn below */
186 realbase = ch_strdup( e->e_ndn );
188 cache_return_entry_r( &li->li_cache, e );
191 if ( candidates == NULL ) {
194 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
195 "ldbm_search: no candidates\n" ));
197 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
201 send_search_result( conn, op,
203 NULL, NULL, NULL, NULL, 0 );
209 /* if not root, get appropriate limits */
210 if ( be_isroot( be, &op->o_ndn ) ) {
213 ( void ) get_limits( be, &op->o_ndn, &limit );
216 /* if candidates exceed to-be-checked entries, abort */
217 if ( !isroot && limit->lms_s_unchecked != -1 ) {
218 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
219 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
220 NULL, NULL, NULL, NULL, 0 );
226 /* if root an no specific limit is required, allow unlimited search */
237 /* if no limit is required, use soft limit */
239 tlimit = limit->lms_t_soft;
241 /* if requested limit higher than hard limit, abort */
242 } else if ( tlimit > limit->lms_t_hard ) {
243 /* no hard limit means use soft instead */
244 if ( limit->lms_t_hard == 0 ) {
245 tlimit = limit->lms_t_soft;
247 /* positive hard limit means abort */
248 } else if ( limit->lms_t_hard > 0 ) {
249 send_search_result( conn, op,
250 LDAP_UNWILLING_TO_PERFORM,
251 NULL, NULL, NULL, NULL, 0 );
256 /* negative hard limit means no limit */
259 /* if no limit is required, use soft limit */
261 slimit = limit->lms_s_soft;
263 /* if requested limit higher than hard limit, abort */
264 } else if ( slimit > limit->lms_s_hard ) {
265 /* no hard limit means use soft instead */
266 if ( limit->lms_s_hard == 0 ) {
267 slimit = limit->lms_s_soft;
269 /* positive hard limit means abort */
270 } else if ( limit->lms_s_hard > 0 ) {
271 send_search_result( conn, op,
272 LDAP_UNWILLING_TO_PERFORM,
273 NULL, NULL, NULL, NULL, 0 );
278 /* negative hard limit means no limit */
282 /* compute it anyway; root does not use it */
283 stoptime = op->o_time + tlimit;
285 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
286 id = idl_nextid( candidates, &cursor ) )
290 /* check for abandon */
291 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
293 if ( op->o_abandon ) {
294 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
299 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
301 /* check time limit */
302 if ( tlimit != -1 && slap_get_time() > stoptime ) {
303 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
304 NULL, NULL, v2refs, NULL, nentries );
309 /* get the entry with reader lock */
310 e = id2entry_r( be, id );
314 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
315 "ldbm_search: candidate %ld not found.\n", id ));
317 Debug( LDAP_DEBUG_TRACE,
318 "ldbm_search: candidate %ld not found\n",
325 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
330 e = deref_entry_r( be, e, &err, &matched, &text );
337 if( e->e_id == id ) {
342 /* need to skip alias which deref into scope */
343 if( scope & LDAP_SCOPE_ONELEVEL ) {
344 char *pdn = dn_parent( NULL, e->e_ndn );
346 if( strcmp( pdn, realbase ) ) {
352 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
353 /* alias is within scope */
355 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
356 "ldbm_search: alias \"%s\" in subtree\n", e->e_dn ));
358 Debug( LDAP_DEBUG_TRACE,
359 "ldbm_search: alias \"%s\" in subtree\n",
370 * if it's a referral, add it to the list of referrals. only do
371 * this for non-base searches, and don't check the filter
372 * explicitly here since it's only a candidate anyway.
374 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
375 is_entry_referral( e ) )
380 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
381 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
382 scopeok = (dn == realbase)
384 : (strcmp( dn, realbase ) ? 0 : 1 );
386 scopeok = (realbase == NULL || *realbase == '\0');
389 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
390 dn = ch_strdup( e->e_ndn );
391 scopeok = dn_issuffix( dn, realbase );
399 struct berval **erefs = get_entry_referrals(
401 struct berval **refs = referral_rewrite( erefs,
403 scope == LDAP_SCOPE_SUBTREE
407 send_search_reference( be, conn, op,
408 e, refs, NULL, &v2refs );
410 ber_bvecfree( refs );
414 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
415 "ldbm_search: candidate referral %ld scope not okay\n",
418 Debug( LDAP_DEBUG_TRACE,
419 "ldbm_search: candidate referral %ld scope not okay\n",
427 /* if it matches the filter and scope, send it */
428 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
432 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
433 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
434 scopeok = (dn == realbase)
436 : (strcmp( dn, realbase ) ? 0 : 1 );
438 scopeok = (realbase == NULL || *realbase == '\0');
441 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
442 dn = ch_strdup( e->e_ndn );
443 scopeok = dn_issuffix( dn, realbase );
451 /* check size limit */
452 if ( --slimit == -1 ) {
453 cache_return_entry_r( &li->li_cache, e );
454 send_search_result( conn, op,
455 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
456 v2refs, NULL, nentries );
462 int result = send_search_entry(be, conn, op,
463 e, attrs, attrsonly, NULL);
466 case 0: /* entry sent ok */
469 case 1: /* entry not sent */
471 case -1: /* connection closed */
472 cache_return_entry_r( &li->li_cache, e );
479 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
480 "ldbm_search: candidate entry %ld scope not okay\n", id ));
482 Debug( LDAP_DEBUG_TRACE,
483 "ldbm_search: candidate entry %ld scope not okay\n",
490 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
491 "ldbm_search: candidate entry %ld does not match filter\n", id ));
493 Debug( LDAP_DEBUG_TRACE,
494 "ldbm_search: candidate entry %ld does not match filter\n",
501 /* free reader lock */
502 cache_return_entry_r( &li->li_cache, e );
505 ldap_pvt_thread_yield();
508 send_search_result( conn, op,
509 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
510 NULL, NULL, v2refs, NULL, nentries );
515 if( candidates != NULL )
516 idl_free( candidates );
518 ber_bvecfree( v2refs );
519 if( realbase ) free( realbase );
532 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
533 "base_candidate: base (%s)\n", e->e_dn ));
535 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
540 idl = idl_alloc( 1 );
541 idl_insert( &idl, e->e_id, 1 );
555 ID_BLOCK *candidates;
556 Filter f, fand, rf, af, xf;
557 AttributeAssertion aa_ref, aa_alias;
558 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
559 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
562 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
563 "search_candidates: base (%s) scope %d deref %d\n",
564 e->e_ndn, scope, deref ));
566 Debug(LDAP_DEBUG_TRACE,
567 "search_candidates: base=\"%s\" s=%d d=%d\n",
568 e->e_ndn, scope, deref );
573 xf.f_choice = LDAP_FILTER_OR;
577 /* match referrals */
578 rf.f_choice = LDAP_FILTER_EQUALITY;
580 rf.f_av_desc = slap_schema.si_ad_objectClass;
581 rf.f_av_value = &bv_ref;
586 if( deref & LDAP_DEREF_SEARCHING ) {
588 af.f_choice = LDAP_FILTER_EQUALITY;
589 af.f_ava = &aa_alias;
590 af.f_av_desc = slap_schema.si_ad_objectClass;
591 af.f_av_value = &bv_alias;
597 f.f_choice = LDAP_FILTER_AND;
599 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
600 ? SLAPD_FILTER_DN_SUBTREE
601 : SLAPD_FILTER_DN_ONE;
602 fand.f_dn = &e->e_nname;
603 fand.f_next = xf.f_or == filter ? filter : &xf ;
605 candidates = filter_candidates( be, &f );
607 return( candidates );