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 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 /* grab giant lock for reading */
59 ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
61 if ( *nbase == '\0' ) {
62 /* DIT root special case */
63 static const Entry root = { NOID, "", "", NULL, NULL };
66 /* need normalized dn below */
67 realbase = ch_strdup( e->e_ndn );
69 candidates = search_candidates( be, e, filter,
70 scope, deref, manageDSAit );
74 } else if ( deref & LDAP_DEREF_FINDING ) {
75 /* deref dn and get entry with reader lock */
76 e = deref_dn_r( be, nbase, &err, &matched, &text );
78 if( err == LDAP_NO_SUCH_OBJECT ) err = LDAP_REFERRAL;
81 /* get entry with reader lock */
82 e = dn2entry_r( be, nbase, &matched );
83 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
88 char *matched_dn = NULL;
89 struct berval **refs = NULL;
91 if ( matched != NULL ) {
92 matched_dn = ch_strdup( matched->e_dn );
94 refs = is_entry_referral( matched )
95 ? get_entry_referrals( be, conn, op, matched )
98 cache_return_entry_r( &li->li_cache, matched );
101 refs = default_referral;
104 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
106 send_ldap_result( conn, op, err,
107 matched_dn, text, refs, NULL );
109 if( matched != NULL ) {
110 ber_bvecfree( refs );
117 if (!manageDSAit && is_entry_referral( e ) ) {
118 /* entry is a referral, don't allow add */
119 char *matched_dn = ch_strdup( e->e_dn );
120 struct berval **refs = get_entry_referrals( be,
123 cache_return_entry_r( &li->li_cache, e );
124 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
126 Debug( LDAP_DEBUG_TRACE,
127 "ldbm_search: entry is referral\n",
130 send_ldap_result( conn, op, LDAP_REFERRAL,
131 matched_dn, NULL, refs, NULL );
133 ber_bvecfree( refs );
139 if ( is_entry_alias( e ) ) {
141 deref = LDAP_DEREF_NEVER;
144 if ( scope == LDAP_SCOPE_BASE ) {
145 candidates = base_candidate( be, e );
148 candidates = search_candidates( be, e, filter,
149 scope, deref, manageDSAit );
152 /* need normalized dn below */
153 realbase = ch_strdup( e->e_ndn );
155 cache_return_entry_r( &li->li_cache, e );
158 if ( candidates == NULL ) {
160 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
163 send_search_result( conn, op,
165 NULL, NULL, NULL, NULL, 0 );
171 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
172 tlimit = -1; /* allow root to set no limit */
174 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
175 be->be_timelimit : tlimit;
176 stoptime = op->o_time + tlimit;
179 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
180 slimit = -1; /* allow root to set no limit */
182 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
183 be->be_sizelimit : slimit;
186 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
187 id = idl_nextid( candidates, &cursor ) )
191 /* check for abandon */
192 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
194 if ( op->o_abandon ) {
195 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
200 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
202 /* check time limit */
203 if ( tlimit != -1 && slap_get_time() > stoptime ) {
204 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
205 NULL, NULL, v2refs, NULL, nentries );
210 /* get the entry with reader lock */
211 e = id2entry_r( be, id );
214 Debug( LDAP_DEBUG_TRACE,
215 "ldbm_search: candidate %ld not found\n",
221 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
226 e = deref_entry_r( be, e, &err, &matched, &text );
233 if( e->e_id == id ) {
238 /* need to skip alias which deref into scope */
239 if( scope & LDAP_SCOPE_ONELEVEL ) {
240 char *pdn = dn_parent( NULL, e->e_ndn );
242 if( strcmp( pdn, realbase ) ) {
249 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
250 /* alias is within scope */
251 Debug( LDAP_DEBUG_TRACE,
252 "ldbm_search: \"%s\" in subtree\n",
261 * if it's a referral, add it to the list of referrals. only do
262 * this for non-base searches, and don't check the filter
263 * explicitly here since it's only a candidate anyway.
265 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
266 is_entry_referral( e ) )
268 struct berval **refs = get_entry_referrals(
271 send_search_reference( be, conn, op,
272 e, refs, scope, NULL, &v2refs );
274 ber_bvecfree( refs );
279 /* if it matches the filter and scope, send it */
280 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
284 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
285 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
286 (void) dn_normalize( dn );
287 scopeok = (dn == realbase)
289 : (strcmp( dn, realbase ) ? 0 : 1 );
293 scopeok = (realbase == NULL || *realbase == '\0');
296 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
297 dn = ch_strdup( e->e_ndn );
298 scopeok = dn_issuffix( dn, realbase );
306 /* check size limit */
307 if ( --slimit == -1 ) {
308 cache_return_entry_r( &li->li_cache, e );
309 send_search_result( conn, op,
310 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
311 v2refs, NULL, nentries );
317 int result = send_search_entry(be, conn, op,
318 e, attrs, attrsonly, NULL);
321 case 0: /* entry sent ok */
324 case 1: /* entry not sent */
326 case -1: /* connection closed */
327 cache_return_entry_r( &li->li_cache, e );
333 Debug( LDAP_DEBUG_TRACE,
334 "ldbm_search: candidate %ld scope not okay\n",
338 Debug( LDAP_DEBUG_TRACE,
339 "ldbm_search: candidate %ld does not match filter\n",
345 /* free reader lock */
346 cache_return_entry_r( &li->li_cache, e );
349 ldap_pvt_thread_yield();
351 send_search_result( conn, op,
352 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
353 NULL, NULL, v2refs, NULL, nentries );
358 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
360 if( candidates != NULL )
361 idl_free( candidates );
363 ber_bvecfree( v2refs );
364 if( realbase ) free( realbase );
376 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
379 idl = idl_alloc( 1 );
380 idl_insert( &idl, e->e_id, 1 );
394 ID_BLOCK *candidates;
395 Filter f, fand, rf, af, xf;
396 AttributeAssertion aa_ref, aa_alias;
397 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
398 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
400 Debug(LDAP_DEBUG_TRACE,
401 "search_candidates: base=\"%s\" s=%d d=%d\n",
402 e->e_ndn, scope, deref );
405 xf.f_choice = LDAP_FILTER_OR;
409 /* match referrals */
410 rf.f_choice = LDAP_FILTER_EQUALITY;
412 rf.f_av_desc = slap_schema.si_ad_objectClass;
413 rf.f_av_value = &bv_ref;
418 if( deref & LDAP_DEREF_SEARCHING ) {
420 af.f_choice = LDAP_FILTER_EQUALITY;
421 af.f_ava = &aa_alias;
422 af.f_av_desc = slap_schema.si_ad_objectClass;
423 af.f_av_value = &bv_alias;
429 f.f_choice = LDAP_FILTER_AND;
431 fand.f_choice = scope == LDAP_SCOPE_SUBTREE
432 ? SLAPD_FILTER_DN_SUBTREE
433 : SLAPD_FILTER_DN_ONE;
434 fand.f_dn = e->e_ndn;
435 fand.f_next = xf.f_or == filter ? filter : &xf ;
437 candidates = filter_candidates( be, &f );
439 return( candidates );