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 );
56 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
58 if ( *nbase == '\0' ) {
59 /* DIT root special case */
60 static const Entry root = { NOID, "", "", NULL, NULL };
63 /* need normalized dn below */
64 realbase = ch_strdup( e->e_ndn );
66 candidates = search_candidates( be, e, filter,
67 scope, deref, manageDSAit );
71 } else if ( deref & LDAP_DEREF_FINDING ) {
72 /* deref dn and get entry with reader lock */
73 e = deref_dn_r( be, nbase, &err, &matched, &text );
76 /* get entry with reader lock */
77 e = dn2entry_r( be, nbase, &matched );
78 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
83 char *matched_dn = NULL;
84 struct berval **refs = NULL;
86 if ( matched != NULL ) {
87 matched_dn = ch_strdup( matched->e_dn );
89 refs = is_entry_referral( matched )
90 ? get_entry_referrals( be, conn, op, matched )
93 cache_return_entry_r( &li->li_cache, matched );
95 refs = default_referral;
98 send_ldap_result( conn, op, err,
99 matched_dn, text, refs, NULL );
101 if( matched != NULL ) {
102 ber_bvecfree( refs );
109 if (!manageDSAit && is_entry_referral( e ) ) {
110 /* entry is a referral, don't allow add */
111 char *matched_dn = ch_strdup( e->e_dn );
112 struct berval **refs = get_entry_referrals( be,
115 cache_return_entry_r( &li->li_cache, e );
117 Debug( LDAP_DEBUG_TRACE,
118 "ldbm_search: entry is referral\n",
121 send_ldap_result( conn, op, LDAP_REFERRAL,
122 matched_dn, NULL, refs, NULL );
124 ber_bvecfree( refs );
130 if ( is_entry_alias( e ) ) {
132 deref = LDAP_DEREF_NEVER;
135 if ( scope == LDAP_SCOPE_BASE ) {
136 candidates = base_candidate( be, e );
139 candidates = search_candidates( be, e, filter,
140 scope, deref, manageDSAit );
143 /* need normalized dn below */
144 realbase = ch_strdup( e->e_ndn );
146 cache_return_entry_r( &li->li_cache, e );
149 if ( candidates == NULL ) {
151 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
154 send_search_result( conn, op,
156 NULL, NULL, NULL, NULL, 0 );
162 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
163 tlimit = -1; /* allow root to set no limit */
165 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
166 be->be_timelimit : tlimit;
167 stoptime = op->o_time + tlimit;
170 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
171 slimit = -1; /* allow root to set no limit */
173 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
174 be->be_sizelimit : slimit;
177 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
178 id = idl_nextid( candidates, &cursor ) )
182 /* check for abandon */
183 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
185 if ( op->o_abandon ) {
186 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
191 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
193 /* check time limit */
194 if ( tlimit != -1 && slap_get_time() > stoptime ) {
195 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
196 NULL, NULL, v2refs, NULL, nentries );
201 /* get the entry with reader lock */
202 e = id2entry_r( be, id );
205 Debug( LDAP_DEBUG_TRACE,
206 "ldbm_search: candidate %ld not found\n",
212 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
217 e = deref_entry_r( be, e, &err, &matched, &text );
224 if( e->e_id == id ) {
229 /* need to skip alias which deref into scope */
230 if( scope & LDAP_SCOPE_ONELEVEL ) {
231 char *pdn = dn_parent( NULL, e->e_ndn );
233 if( strcmp( pdn, realbase ) ) {
240 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
241 /* alias is within scope */
242 Debug( LDAP_DEBUG_TRACE,
243 "ldbm_search: \"%s\" in subtree\n",
252 * if it's a referral, add it to the list of referrals. only do
253 * this for non-base searches, and don't check the filter
254 * explicitly here since it's only a candidate anyway.
256 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
257 is_entry_referral( e ) )
259 struct berval **refs = get_entry_referrals(
262 send_search_reference( be, conn, op,
263 e, refs, scope, NULL, &v2refs );
265 ber_bvecfree( refs );
270 /* if it matches the filter and scope, send it */
271 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
275 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
276 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
277 (void) dn_normalize( dn );
278 scopeok = (dn == realbase)
280 : (strcmp( dn, realbase ) ? 0 : 1 );
284 scopeok = (realbase == NULL || *realbase == '\0');
287 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
288 dn = ch_strdup( e->e_ndn );
289 scopeok = dn_issuffix( dn, realbase );
297 /* check size limit */
298 if ( --slimit == -1 ) {
299 cache_return_entry_r( &li->li_cache, e );
300 send_search_result( conn, op,
301 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
302 v2refs, NULL, nentries );
308 int result = send_search_entry(be, conn, op,
309 e, attrs, attrsonly, NULL);
312 case 0: /* entry sent ok */
315 case 1: /* entry not sent */
317 case -1: /* connection closed */
318 cache_return_entry_r( &li->li_cache, e );
324 Debug( LDAP_DEBUG_TRACE,
325 "ldbm_search: candidate %ld scope not okay\n",
329 Debug( LDAP_DEBUG_TRACE,
330 "ldbm_search: candidate %ld does not match filter\n",
336 /* free reader lock */
337 cache_return_entry_r( &li->li_cache, e );
340 ldap_pvt_thread_yield();
342 send_search_result( conn, op,
343 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
344 NULL, NULL, v2refs, NULL, nentries );
349 if( candidates != NULL )
350 idl_free( candidates );
352 ber_bvecfree( v2refs );
353 if( realbase ) free( realbase );
365 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
368 idl = idl_alloc( 1 );
369 idl_insert( &idl, e->e_id, 1 );
383 ID_BLOCK *candidates;
384 Filter f, fand, rf, af, xf;
385 AttributeAssertion aa_ref, aa_alias;
386 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
387 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
389 Debug(LDAP_DEBUG_TRACE,
390 "search_candidates: base=\"%s\" s=%d d=%d\n",
391 e->e_ndn, scope, deref );
394 xf.f_choice = LDAP_FILTER_OR;
398 /* match referrals */
399 rf.f_choice = LDAP_FILTER_EQUALITY;
401 rf.f_av_desc = slap_schema.si_ad_objectClass;
402 rf.f_av_value = &bv_ref;
407 if( deref & LDAP_DEREF_SEARCHING ) {
409 af.f_choice = LDAP_FILTER_EQUALITY;
410 af.f_ava = &aa_alias;
411 af.f_av_desc = slap_schema.si_ad_objectClass;
412 af.f_av_value = &bv_alias;
418 f.f_choice = LDAP_FILTER_AND;
420 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
421 ? SLAPD_FILTER_DN_SUBTREE
422 : SLAPD_FILTER_DN_ONE;
423 fand.f_dn = e->e_ndn;
424 fand.f_next = xf.f_or == filter ? filter : &xf ;
426 candidates = filter_candidates( be, &f );
428 return( candidates );