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,
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 );
57 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
58 "ldbm_back_search: enter\n" ));
60 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
64 if ( *nbase == '\0' ) {
65 /* DIT root special case */
66 static const Entry root = { NOID, "", "", NULL, NULL };
69 /* need normalized dn below */
70 realbase = ch_strdup( e->e_ndn );
72 candidates = search_candidates( be, e, filter,
73 scope, deref, manageDSAit );
77 } else if ( deref & LDAP_DEREF_FINDING ) {
78 /* deref dn and get entry with reader lock */
79 e = deref_dn_r( be, nbase, &err, &matched, &text );
81 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
84 /* get entry with reader lock */
85 e = dn2entry_r( be, nbase, &matched );
86 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
91 char *matched_dn = NULL;
92 struct berval **refs = NULL;
94 if ( matched != NULL ) {
95 matched_dn = ch_strdup( matched->e_dn );
97 refs = is_entry_referral( matched )
98 ? get_entry_referrals( be, conn, op, matched )
101 cache_return_entry_r( &li->li_cache, matched );
104 refs = default_referral;
107 send_ldap_result( conn, op, err,
108 matched_dn, text, refs, NULL );
110 if( matched != NULL ) {
111 ber_bvecfree( refs );
118 if (!manageDSAit && is_entry_referral( e ) ) {
119 /* entry is a referral, don't allow add */
120 char *matched_dn = ch_strdup( e->e_dn );
121 struct berval **refs = get_entry_referrals( be,
124 cache_return_entry_r( &li->li_cache, e );
127 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
128 "ldbm_search: entry (%s) is a referral.\n",
131 Debug( LDAP_DEBUG_TRACE,
132 "ldbm_search: entry is referral\n",
137 send_ldap_result( conn, op, LDAP_REFERRAL,
138 matched_dn, NULL, refs, NULL );
140 ber_bvecfree( refs );
146 if ( is_entry_alias( e ) ) {
148 deref = LDAP_DEREF_NEVER;
151 if ( scope == LDAP_SCOPE_BASE ) {
152 candidates = base_candidate( be, e );
155 candidates = search_candidates( be, e, filter,
156 scope, deref, manageDSAit );
159 /* need normalized dn below */
160 realbase = ch_strdup( e->e_ndn );
162 cache_return_entry_r( &li->li_cache, e );
165 if ( candidates == NULL ) {
168 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
169 "ldbm_search: no candidates\n" ));
171 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
176 send_search_result( conn, op,
178 NULL, NULL, NULL, NULL, 0 );
184 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
185 tlimit = -1; /* allow root to set no limit */
187 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
188 be->be_timelimit : tlimit;
189 stoptime = op->o_time + tlimit;
192 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
193 slimit = -1; /* allow root to set no limit */
195 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
196 be->be_sizelimit : slimit;
199 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
200 id = idl_nextid( candidates, &cursor ) )
204 /* check for abandon */
205 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
207 if ( op->o_abandon ) {
208 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
213 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
215 /* check time limit */
216 if ( tlimit != -1 && slap_get_time() > stoptime ) {
217 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
218 NULL, NULL, v2refs, NULL, nentries );
223 /* get the entry with reader lock */
224 e = id2entry_r( be, id );
228 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
229 "ldbm_search: candidate %ld not found.\n", id ));
231 Debug( LDAP_DEBUG_TRACE,
232 "ldbm_search: candidate %ld not found\n",
240 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
245 e = deref_entry_r( be, e, &err, &matched, &text );
252 if( e->e_id == id ) {
257 /* need to skip alias which deref into scope */
258 if( scope & LDAP_SCOPE_ONELEVEL ) {
259 char *pdn = dn_parent( NULL, e->e_ndn );
261 if( strcmp( pdn, realbase ) ) {
268 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
269 /* alias is within scope */
271 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
272 "ldbm_search: \"%s\" in subtree\n", e->e_dn ));
274 Debug( LDAP_DEBUG_TRACE,
275 "ldbm_search: \"%s\" in subtree\n",
286 * if it's a referral, add it to the list of referrals. only do
287 * this for non-base searches, and don't check the filter
288 * explicitly here since it's only a candidate anyway.
290 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
291 is_entry_referral( e ) )
293 struct berval **refs = get_entry_referrals(
296 send_search_reference( be, conn, op,
297 e, refs, scope, NULL, &v2refs );
299 ber_bvecfree( refs );
304 /* if it matches the filter and scope, send it */
305 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
309 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
310 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
311 (void) dn_normalize( dn );
312 scopeok = (dn == realbase)
314 : (strcmp( dn, realbase ) ? 0 : 1 );
318 scopeok = (realbase == NULL || *realbase == '\0');
321 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
322 dn = ch_strdup( e->e_ndn );
323 scopeok = dn_issuffix( dn, realbase );
331 /* check size limit */
332 if ( --slimit == -1 ) {
333 cache_return_entry_r( &li->li_cache, e );
334 send_search_result( conn, op,
335 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
336 v2refs, NULL, nentries );
342 int result = send_search_entry(be, conn, op,
343 e, attrs, attrsonly, NULL);
346 case 0: /* entry sent ok */
349 case 1: /* entry not sent */
351 case -1: /* connection closed */
352 cache_return_entry_r( &li->li_cache, e );
359 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
360 "ldbm_search: candidate %ld scope not okay\n", id ));
362 Debug( LDAP_DEBUG_TRACE,
363 "ldbm_search: candidate %ld scope not okay\n",
370 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
371 "ldbm_search: candidate %ld does not match filter\n", id ));
373 Debug( LDAP_DEBUG_TRACE,
374 "ldbm_search: candidate %ld does not match filter\n",
382 /* free reader lock */
383 cache_return_entry_r( &li->li_cache, e );
386 ldap_pvt_thread_yield();
388 send_search_result( conn, op,
389 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
390 NULL, NULL, v2refs, NULL, nentries );
395 if( candidates != NULL )
396 idl_free( candidates );
398 ber_bvecfree( v2refs );
399 if( realbase ) free( realbase );
412 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
413 "base_candidate: base (%s)\n", e->e_dn ));
415 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
420 idl = idl_alloc( 1 );
421 idl_insert( &idl, e->e_id, 1 );
435 ID_BLOCK *candidates;
436 Filter f, fand, rf, af, xf;
437 AttributeAssertion aa_ref, aa_alias;
438 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
439 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
442 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
443 "search_candidates: base (%s) scope %d deref %d\n",
444 e->e_ndn, scope, deref ));
446 Debug(LDAP_DEBUG_TRACE,
447 "search_candidates: base=\"%s\" s=%d d=%d\n",
448 e->e_ndn, scope, deref );
453 xf.f_choice = LDAP_FILTER_OR;
457 /* match referrals */
458 rf.f_choice = LDAP_FILTER_EQUALITY;
460 rf.f_av_desc = slap_schema.si_ad_objectClass;
461 rf.f_av_value = &bv_ref;
466 if( deref & LDAP_DEREF_SEARCHING ) {
468 af.f_choice = LDAP_FILTER_EQUALITY;
469 af.f_ava = &aa_alias;
470 af.f_av_desc = slap_schema.si_ad_objectClass;
471 af.f_av_value = &bv_alias;
477 f.f_choice = LDAP_FILTER_AND;
479 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
480 ? SLAPD_FILTER_DN_SUBTREE
481 : SLAPD_FILTER_DN_ONE;
482 fand.f_dn = e->e_ndn;
483 fand.f_next = xf.f_or == filter ? filter : &xf ;
485 candidates = filter_candidates( be, &f );
487 return( candidates );