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 );
82 /* get entry with reader lock */
83 e = dn2entry_r( be, nbase, &matched );
84 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
89 char *matched_dn = NULL;
90 struct berval **refs = NULL;
92 if ( matched != NULL ) {
93 matched_dn = ch_strdup( matched->e_dn );
95 refs = is_entry_referral( matched )
96 ? get_entry_referrals( be, conn, op, matched )
99 cache_return_entry_r( &li->li_cache, matched );
101 refs = default_referral;
104 send_ldap_result( conn, op, err,
105 matched_dn, text, refs, NULL );
107 if( matched != NULL ) {
108 ber_bvecfree( refs );
115 if (!manageDSAit && is_entry_referral( e ) ) {
116 /* entry is a referral, don't allow add */
117 char *matched_dn = ch_strdup( e->e_dn );
118 struct berval **refs = get_entry_referrals( be,
121 cache_return_entry_r( &li->li_cache, e );
124 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
125 "ldbm_search: entry (%s) is a referral.\n",
128 Debug( LDAP_DEBUG_TRACE,
129 "ldbm_search: entry is referral\n",
134 send_ldap_result( conn, op, LDAP_REFERRAL,
135 matched_dn, NULL, refs, NULL );
137 ber_bvecfree( refs );
143 if ( is_entry_alias( e ) ) {
145 deref = LDAP_DEREF_NEVER;
148 if ( scope == LDAP_SCOPE_BASE ) {
149 candidates = base_candidate( be, e );
152 candidates = search_candidates( be, e, filter,
153 scope, deref, manageDSAit );
156 /* need normalized dn below */
157 realbase = ch_strdup( e->e_ndn );
159 cache_return_entry_r( &li->li_cache, e );
162 if ( candidates == NULL ) {
165 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
166 "ldbm_search: no candidates\n" ));
168 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
173 send_search_result( conn, op,
175 NULL, NULL, NULL, NULL, 0 );
181 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
182 tlimit = -1; /* allow root to set no limit */
184 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
185 be->be_timelimit : tlimit;
186 stoptime = op->o_time + tlimit;
189 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
190 slimit = -1; /* allow root to set no limit */
192 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
193 be->be_sizelimit : slimit;
196 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
197 id = idl_nextid( candidates, &cursor ) )
201 /* check for abandon */
202 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
204 if ( op->o_abandon ) {
205 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
210 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
212 /* check time limit */
213 if ( tlimit != -1 && slap_get_time() > stoptime ) {
214 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
215 NULL, NULL, v2refs, NULL, nentries );
220 /* get the entry with reader lock */
221 e = id2entry_r( be, id );
225 LDAP_LOG(( "backend", LDAP_LEVEL_INFO,
226 "ldbm_search: candidate %ld not found.\n", id ));
228 Debug( LDAP_DEBUG_TRACE,
229 "ldbm_search: candidate %ld not found\n",
237 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
242 e = deref_entry_r( be, e, &err, &matched, &text );
249 if( e->e_id == id ) {
254 /* need to skip alias which deref into scope */
255 if( scope & LDAP_SCOPE_ONELEVEL ) {
256 char *pdn = dn_parent( NULL, e->e_ndn );
258 if( strcmp( pdn, realbase ) ) {
265 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
266 /* alias is within scope */
268 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
269 "ldbm_search: \"%s\" in subtree\n", e->e_dn ));
271 Debug( LDAP_DEBUG_TRACE,
272 "ldbm_search: \"%s\" in subtree\n",
283 * if it's a referral, add it to the list of referrals. only do
284 * this for non-base searches, and don't check the filter
285 * explicitly here since it's only a candidate anyway.
287 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
288 is_entry_referral( e ) )
290 struct berval **refs = get_entry_referrals(
293 send_search_reference( be, conn, op,
294 e, refs, scope, NULL, &v2refs );
296 ber_bvecfree( refs );
301 /* if it matches the filter and scope, send it */
302 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
306 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
307 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
308 (void) dn_normalize( dn );
309 scopeok = (dn == realbase)
311 : (strcmp( dn, realbase ) ? 0 : 1 );
315 scopeok = (realbase == NULL || *realbase == '\0');
318 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
319 dn = ch_strdup( e->e_ndn );
320 scopeok = dn_issuffix( dn, realbase );
328 /* check size limit */
329 if ( --slimit == -1 ) {
330 cache_return_entry_r( &li->li_cache, e );
331 send_search_result( conn, op,
332 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
333 v2refs, NULL, nentries );
339 int result = send_search_entry(be, conn, op,
340 e, attrs, attrsonly, NULL);
343 case 0: /* entry sent ok */
346 case 1: /* entry not sent */
348 case -1: /* connection closed */
349 cache_return_entry_r( &li->li_cache, e );
356 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
357 "ldbm_search: candidate %ld scope not okay\n", id ));
359 Debug( LDAP_DEBUG_TRACE,
360 "ldbm_search: candidate %ld scope not okay\n",
367 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL2,
368 "ldbm_search: candidate %ld does not match filter\n", id ));
370 Debug( LDAP_DEBUG_TRACE,
371 "ldbm_search: candidate %ld does not match filter\n",
379 /* free reader lock */
380 cache_return_entry_r( &li->li_cache, e );
383 ldap_pvt_thread_yield();
385 send_search_result( conn, op,
386 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
387 NULL, NULL, v2refs, NULL, nentries );
392 if( candidates != NULL )
393 idl_free( candidates );
395 ber_bvecfree( v2refs );
396 if( realbase ) free( realbase );
409 LDAP_LOG(( "backend", LDAP_LEVEL_ENTRY,
410 "base_candidate: base (%s)\n", e->e_dn ));
412 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
417 idl = idl_alloc( 1 );
418 idl_insert( &idl, e->e_id, 1 );
432 ID_BLOCK *candidates;
433 Filter f, fand, rf, af, xf;
434 AttributeAssertion aa_ref, aa_alias;
435 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
436 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
439 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
440 "search_candidates: base (%s) scope %d deref %d\n",
441 e->e_ndn, scope, deref ));
443 Debug(LDAP_DEBUG_TRACE,
444 "search_candidates: base=\"%s\" s=%d d=%d\n",
445 e->e_ndn, scope, deref );
450 xf.f_choice = LDAP_FILTER_OR;
454 /* match referrals */
455 rf.f_choice = LDAP_FILTER_EQUALITY;
457 rf.f_av_desc = slap_schema.si_ad_objectClass;
458 rf.f_av_value = &bv_ref;
463 if( deref & LDAP_DEREF_SEARCHING ) {
465 af.f_choice = LDAP_FILTER_EQUALITY;
466 af.f_ava = &aa_alias;
467 af.f_av_desc = slap_schema.si_ad_objectClass;
468 af.f_av_value = &bv_alias;
474 f.f_choice = LDAP_FILTER_AND;
476 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
477 ? SLAPD_FILTER_DN_SUBTREE
478 : SLAPD_FILTER_DN_ONE;
479 fand.f_dn = e->e_ndn;
480 fand.f_next = xf.f_or == filter ? filter : &xf ;
482 candidates = filter_candidates( be, &f );
484 return( candidates );