1 /* search.c - ldbm backend search function */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include <ac/string.h>
12 #include <ac/socket.h>
15 #include "back-ldbm.h"
16 #include "proto-back-ldbm.h"
18 static ID_BLOCK *base_candidate(
19 Backend *be, Entry *e );
21 static ID_BLOCK *search_candidates(
22 Backend *be, Entry *e, Filter *filter,
23 int scope, int deref, int manageDSAit );
42 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
49 struct berval **v2refs = NULL;
50 Entry *matched = NULL;
51 char *realbase = NULL;
53 int manageDSAit = get_manageDSAit( op );
55 Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
57 /* get entry with reader lock */
58 if ( deref & LDAP_DEREF_FINDING ) {
59 e = deref_dn_r( be, base, &err, &matched, &text );
62 e = dn2entry_r( be, base, &matched );
63 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
68 char *matched_dn = NULL;
69 struct berval **refs = NULL;
71 if ( matched != NULL ) {
72 matched_dn = ch_strdup( matched->e_dn );
74 refs = is_entry_referral( matched )
75 ? get_entry_referrals( be, conn, op, matched )
78 cache_return_entry_r( &li->li_cache, matched );
80 refs = default_referral;
83 send_ldap_result( conn, op, err,
84 matched_dn, text, refs, NULL );
86 if( matched != NULL ) {
94 if (!manageDSAit && is_entry_referral( e ) ) {
95 /* entry is a referral, don't allow add */
96 char *matched_dn = ch_strdup( e->e_dn );
97 struct berval **refs = get_entry_referrals( be,
100 cache_return_entry_r( &li->li_cache, e );
102 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
105 send_ldap_result( conn, op, LDAP_REFERRAL,
106 matched_dn, NULL, refs, NULL );
108 ber_bvecfree( refs );
114 if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
115 tlimit = -1; /* allow root to set no limit */
117 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
118 be->be_timelimit : tlimit;
119 stoptime = op->o_time + tlimit;
122 if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
123 slimit = -1; /* allow root to set no limit */
125 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
126 be->be_sizelimit : slimit;
129 if ( scope == LDAP_SCOPE_BASE) {
130 candidates = base_candidate( be, e );
133 candidates = search_candidates( be, e, filter,
134 scope, deref, manageDSAit );
137 /* need normalized dn below */
138 realbase = ch_strdup( e->e_ndn );
139 cache_return_entry_r( &li->li_cache, e );
141 if ( candidates == NULL ) {
143 Debug( LDAP_DEBUG_TRACE, "no candidates\n", 0,
146 send_search_result( conn, op,
148 NULL, NULL, NULL, NULL, 0 );
154 for ( id = idl_firstid( candidates, &cursor ); id != NOID;
155 id = idl_nextid( candidates, &cursor ) )
159 /* check for abandon */
160 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
162 if ( op->o_abandon ) {
163 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
168 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
170 /* check time limit */
171 if ( tlimit != -1 && slap_get_time() > stoptime ) {
172 send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
173 NULL, NULL, v2refs, NULL, nentries );
178 /* get the entry with reader lock */
179 e = id2entry_r( be, id );
182 Debug( LDAP_DEBUG_ARGS, "search: candidate %ld not found\n",
188 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
193 e = deref_entry_r( be, e, &err, &matched, &text );
200 if( e->e_id == id ) {
205 /* need to skip alias which deref into scope */
206 if( scope & LDAP_SCOPE_ONELEVEL ) {
207 char *pdn = dn_parent( NULL, e->e_ndn );
209 if( strcmp( pdn, realbase ) ) {
216 } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
217 /* alias is within scope */
218 Debug( LDAP_DEBUG_ARGS, "search: \"%s\" in subtree\n",
227 * if it's a referral, add it to the list of referrals. only do
228 * this for non-base searches, and don't check the filter
229 * explicitly here since it's only a candidate anyway.
231 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
232 is_entry_referral( e ) )
234 struct berval **refs = get_entry_referrals(
237 send_search_reference( be, conn, op,
238 e, refs, scope, NULL, &v2refs );
240 ber_bvecfree( refs );
245 /* if it matches the filter and scope, send it */
246 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
250 if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
251 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
252 (void) dn_normalize_case( dn );
253 scopeok = (dn == realbase)
255 : (strcmp( dn, realbase ) ? 0 : 1 );
259 scopeok = (realbase == NULL || *realbase == '\0');
262 } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
263 dn = ch_strdup( e->e_ndn );
264 scopeok = dn_issuffix( dn, realbase );
272 /* check size limit */
273 if ( --slimit == -1 ) {
274 cache_return_entry_r( &li->li_cache, e );
275 send_search_result( conn, op,
276 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
277 v2refs, NULL, nentries );
283 switch ( send_search_entry( be, conn, op, e,
284 attrs, attrsonly, NULL ) ) {
285 case 0: /* entry sent ok */
288 case 1: /* entry not sent */
290 case -1: /* connection closed */
291 cache_return_entry_r( &li->li_cache, e );
297 Debug( LDAP_DEBUG_TRACE, "candidate %ld scope not okay\n",
301 Debug( LDAP_DEBUG_TRACE, "candidate %ld does match filter\n",
307 /* free reader lock */
308 cache_return_entry_r( &li->li_cache, e );
311 ldap_pvt_thread_yield();
313 send_search_result( conn, op,
314 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
315 NULL, NULL, v2refs, NULL, nentries );
320 idl_free( candidates );
322 ber_bvecfree( v2refs );
323 if( realbase ) free( realbase );
336 Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
339 idl = idl_alloc( 1 );
340 idl_insert( &idl, e->e_id, 1 );
355 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
356 ID_BLOCK *candidates;
357 Filter *f, *rf, *af, *lf;
359 Debug(LDAP_DEBUG_TRACE, "search_candidates: base=\"%s\" s=%d d=%d\n",
360 e->e_ndn, scope, deref );
365 /* match referrals */
366 rf = (Filter *) ch_malloc( sizeof(Filter) );
368 rf->f_choice = LDAP_FILTER_OR;
369 rf->f_or = (Filter *) ch_malloc( sizeof(Filter) );
370 rf->f_or->f_choice = LDAP_FILTER_EQUALITY;
371 rf->f_or->f_avtype = ch_strdup( "objectclass" );
372 rf->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
373 rf->f_or->f_avvalue.bv_len = sizeof("REFERRAL")-1;
374 rf->f_or->f_next = filter;
381 if( deref & LDAP_DEREF_SEARCHING ) {
383 af = (Filter *) ch_malloc( sizeof(Filter) );
385 af->f_choice = LDAP_FILTER_OR;
386 af->f_or = (Filter *) ch_malloc( sizeof(Filter) );
387 af->f_or->f_choice = LDAP_FILTER_EQUALITY;
388 af->f_or->f_avtype = ch_strdup( "objectclass" );
389 af->f_or->f_avvalue.bv_val = ch_strdup( "ALIAS" );
390 af->f_or->f_avvalue.bv_len = sizeof("ALIAS")-1;
391 af->f_or->f_next = f;
397 if ( scope == LDAP_SCOPE_SUBTREE ) {
398 lf = (Filter *) ch_malloc( sizeof(Filter) );
400 lf->f_choice = LDAP_FILTER_AND;
401 lf->f_and = (Filter *) ch_malloc( sizeof(Filter) );
403 lf->f_and->f_choice = SLAPD_FILTER_DN_SUBTREE;
404 lf->f_and->f_dn = e->e_ndn;
406 lf->f_and->f_next = f;
409 } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
410 lf = (Filter *) ch_malloc( sizeof(Filter) );
412 lf->f_choice = LDAP_FILTER_AND;
413 lf->f_and = (Filter *) ch_malloc( sizeof(Filter) );
415 lf->f_and->f_choice = SLAPD_FILTER_DN_ONE;
416 lf->f_and->f_dn = e->e_ndn;
418 lf->f_and->f_next = f;
425 candidates = filter_candidates( be, f );
427 /* free up filter additions we allocated above */
434 af->f_or->f_next = NULL;
439 rf->f_or->f_next = NULL;
443 return( candidates );