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);
68 if ( nbase->bv_len == 0 ) {
69 /* DIT root special case */
70 e = (Entry *) &slap_entry_root;
72 /* need normalized dn below */
73 ber_dupbv( &realbase, &e->e_nname );
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, &matched );
89 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
94 struct berval matched_dn = { 0, NULL };
95 BerVarray refs = NULL;
97 if ( matched != NULL ) {
99 ber_dupbv( &matched_dn, &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_bvarray_free( erefs );
115 refs = referral_rewrite( default_referral,
119 send_ldap_result( conn, op, err, matched_dn.bv_val,
122 ber_bvarray_free( refs );
123 ber_memfree( matched_dn.bv_val );
127 if (!manageDSAit && is_entry_referral( e ) ) {
128 /* entry is a referral, don't allow add */
129 struct berval matched_dn;
133 ber_dupbv( &matched_dn, &e->e_name );
134 erefs = get_entry_referrals( be, conn, op, e );
137 cache_return_entry_r( &li->li_cache, e );
140 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
141 "ldbm_search: entry (%s) is a referral.\n",
144 Debug( LDAP_DEBUG_TRACE,
145 "ldbm_search: entry is referral\n",
150 refs = referral_rewrite( erefs, &matched_dn,
153 ber_bvarray_free( erefs );
157 send_ldap_result( conn, op, LDAP_REFERRAL,
158 matched_dn.bv_val, NULL, refs, NULL );
159 ber_bvarray_free( refs );
162 send_ldap_result( conn, op, LDAP_OTHER,
164 "bad referral object", NULL, NULL );
167 ber_memfree( matched_dn.bv_val );
171 if ( is_entry_alias( e ) ) {
173 deref = LDAP_DEREF_NEVER;
176 if ( scope == LDAP_SCOPE_BASE ) {
177 cscope = LDAP_SCOPE_BASE;
178 candidates = base_candidate( be, e );
181 cscope = ( scope != LDAP_SCOPE_SUBTREE )
182 ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
183 candidates = search_candidates( be, e, filter,
184 scope, 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(( "backend", LDAP_LEVEL_INFO,
197 "ldbm_search: no candidates\n" ));
199 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
203 send_search_result( conn, op,
205 NULL, NULL, NULL, NULL, 0 );
211 /* if not root, get appropriate limits */
212 if ( be_isroot( be, &op->o_ndn ) ) {
215 ( void ) get_limits( be, &op->o_ndn, &limit );
218 /* if candidates exceed to-be-checked entries, abort */
219 if ( !isroot && limit->lms_s_unchecked != -1 ) {
220 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
221 send_search_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
222 NULL, NULL, NULL, NULL, 0 );
228 /* if root an no specific limit is required, allow unlimited search */
239 /* if no limit is required, use soft limit */
241 tlimit = limit->lms_t_soft;
243 /* if requested limit higher than hard limit, abort */
244 } else if ( tlimit > limit->lms_t_hard ) {
245 /* no hard limit means use soft instead */
246 if ( limit->lms_t_hard == 0 ) {
247 tlimit = limit->lms_t_soft;
249 /* positive hard limit means abort */
250 } else if ( limit->lms_t_hard > 0 ) {
251 send_search_result( conn, op,
252 LDAP_UNWILLING_TO_PERFORM,
253 NULL, NULL, NULL, NULL, 0 );
258 /* negative hard limit means no limit */
261 /* if no limit is required, use soft limit */
263 slimit = limit->lms_s_soft;
265 /* if requested limit higher than hard limit, abort */
266 } else if ( slimit > limit->lms_s_hard ) {
267 /* no hard limit means use soft instead */
268 if ( limit->lms_s_hard == 0 ) {
269 slimit = limit->lms_s_soft;
271 /* positive hard limit means abort */
272 } else if ( limit->lms_s_hard > 0 ) {
273 send_search_result( conn, op,
274 LDAP_UNWILLING_TO_PERFORM,
275 NULL, NULL, NULL, NULL, 0 );
280 /* negative hard limit means no limit */
284 /* compute it anyway; root does not use it */
285 stoptime = op->o_time + tlimit;
287 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
288 id = idl_nextid( candidates, &cursor ) )
292 /* check for abandon */
293 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
295 if ( op->o_abandon ) {
296 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
301 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
303 /* check time limit */
304 if ( tlimit != -1 && slap_get_time() > stoptime ) {
305 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
306 NULL, NULL, v2refs, NULL, nentries );
311 /* get the entry with reader lock */
312 e = id2entry_r( be, id );
316 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
317 "ldbm_search: candidate %ld not found.\n", id ));
319 Debug( LDAP_DEBUG_TRACE,
320 "ldbm_search: candidate %ld not found\n",
327 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
332 e = deref_entry_r( be, e, &err, &matched, &text );
339 if( e->e_id == id ) {
344 /* need to skip alias which deref into scope */
345 if( scope & LDAP_SCOPE_ONELEVEL ) {
347 dnParent( &e->e_nname, &pdn );
348 if ( ber_bvcmp( &pdn, &realbase ) ) {
352 } else if ( dnIsSuffix( &e->e_nname, &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 ( !be_issuffix( be, &e->e_nname ) ) {
382 dnParent( &e->e_nname, &dn );
383 scopeok = dn_match( &dn, &realbase );
385 scopeok = (realbase.bv_len == 0);
388 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
389 scopeok = dnIsSuffix( &e->e_nname, &realbase );
396 BerVarray erefs = get_entry_referrals(
398 BerVarray refs = referral_rewrite( erefs,
400 scope == LDAP_SCOPE_SUBTREE
404 send_search_reference( be, conn, op,
405 e, refs, NULL, &v2refs );
407 ber_bvarray_free( refs );
411 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
412 "ldbm_search: candidate referral %ld scope not okay\n",
415 Debug( LDAP_DEBUG_TRACE,
416 "ldbm_search: candidate referral %ld scope not okay\n",
424 /* if it matches the filter and scope, send it */
425 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
429 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
430 if ( !be_issuffix( be, &e->e_nname ) ) {
431 dnParent( &e->e_nname, &dn );
432 scopeok = dn_match( &dn, &realbase );
434 scopeok = (realbase.bv_len == 0);
437 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
438 scopeok = dnIsSuffix( &e->e_nname, &realbase );
445 /* check size limit */
446 if ( --slimit == -1 ) {
447 cache_return_entry_r( &li->li_cache, e );
448 send_search_result( conn, op,
449 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
450 v2refs, NULL, nentries );
456 int result = send_search_entry(be, conn, op,
457 e, attrs, attrsonly, NULL);
460 case 0: /* entry sent ok */
463 case 1: /* entry not sent */
465 case -1: /* connection closed */
466 cache_return_entry_r( &li->li_cache, e );
473 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
474 "ldbm_search: candidate entry %ld scope not okay\n", id ));
476 Debug( LDAP_DEBUG_TRACE,
477 "ldbm_search: candidate entry %ld scope not okay\n",
484 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
485 "ldbm_search: candidate entry %ld does not match filter\n", id ));
487 Debug( LDAP_DEBUG_TRACE,
488 "ldbm_search: candidate entry %ld does not match filter\n",
495 /* free reader lock */
496 cache_return_entry_r( &li->li_cache, e );
499 ldap_pvt_thread_yield();
502 send_search_result( conn, op,
503 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
504 NULL, NULL, v2refs, NULL, nentries );
509 if( candidates != NULL )
510 idl_free( candidates );
512 if( v2refs ) ber_bvarray_free( v2refs );
513 if( realbase.bv_val ) free( realbase.bv_val );
526 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
527 "base_candidate: base (%s)\n", e->e_dn ));
529 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
534 idl = idl_alloc( 1 );
535 idl_insert( &idl, e->e_id, 1 );
549 ID_BLOCK *candidates;
550 Filter f, fand, rf, af, xf;
551 AttributeAssertion aa_ref, aa_alias;
552 struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
553 struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
556 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
557 "search_candidates: base (%s) scope %d deref %d\n",
558 e->e_ndn, scope, deref ));
560 Debug(LDAP_DEBUG_TRACE,
561 "search_candidates: base=\"%s\" s=%d d=%d\n",
562 e->e_ndn, scope, deref );
567 xf.f_choice = LDAP_FILTER_OR;
571 /* match referrals */
572 rf.f_choice = LDAP_FILTER_EQUALITY;
574 rf.f_av_desc = slap_schema.si_ad_objectClass;
575 rf.f_av_value = bv_ref;
580 if( deref & LDAP_DEREF_SEARCHING ) {
582 af.f_choice = LDAP_FILTER_EQUALITY;
583 af.f_ava = &aa_alias;
584 af.f_av_desc = slap_schema.si_ad_objectClass;
585 af.f_av_value = bv_alias;
591 f.f_choice = LDAP_FILTER_AND;
593 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
594 ? SLAPD_FILTER_DN_SUBTREE
595 : SLAPD_FILTER_DN_ONE;
596 fand.f_dn = &e->e_nname;
597 fand.f_next = xf.f_or == filter ? filter : &xf ;
599 candidates = filter_candidates( be, &f );
601 return( candidates );