]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
8470483817bf0fbe20b69d80947e508cf47de4a9
[openldap] / servers / slapd / back-ldbm / search.c
1 /* search.c - ldbm backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
18
19 static ID_BLOCK *base_candidate(
20         Backend *be, Entry *e );
21
22 static ID_BLOCK *search_candidates(
23         Backend *be, Entry *e, Filter *filter,
24         int scope, int deref, int manageDSAit );
25
26
27 int
28 ldbm_back_search(
29     Backend     *be,
30     Connection  *conn,
31     Operation   *op,
32     const char  *base,
33     const char  *nbase,
34     int         scope,
35     int         deref,
36     int         slimit,
37     int         tlimit,
38     Filter      *filter,
39     const char  *filterstr,
40     char        **attrs,
41     int         attrsonly )
42 {
43         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
44         int             rc, err;
45         const char *text = NULL;
46         time_t          stoptime;
47         ID_BLOCK                *candidates;
48         ID              id, cursor;
49         Entry           *e;
50         struct berval **v2refs = NULL;
51         Entry   *matched = NULL;
52         char    *realbase = NULL;
53         int             nentries = 0;
54         int             manageDSAit = get_manageDSAit( op );
55
56         Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
57
58         /* get entry with reader lock */
59         if ( deref & LDAP_DEREF_FINDING ) {
60                 e = deref_dn_r( be, nbase, &err, &matched, &text );
61
62         } else {
63                 e = dn2entry_r( be, nbase, &matched );
64                 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
65                 text = NULL;
66         }
67
68         if ( e == NULL ) {
69                 char *matched_dn = NULL;
70                 struct berval **refs = NULL;
71
72                 if ( matched != NULL ) {
73                         matched_dn = ch_strdup( matched->e_dn );
74
75                         refs = is_entry_referral( matched )
76                                 ? get_entry_referrals( be, conn, op, matched )
77                                 : NULL;
78
79                         cache_return_entry_r( &li->li_cache, matched );
80                 } else {
81                         refs = default_referral;
82                 }
83
84                 send_ldap_result( conn, op, err,
85                         matched_dn, text, refs, NULL );
86
87                 if( matched != NULL ) {
88                         ber_bvecfree( refs );
89                         free( matched_dn );
90                 }
91
92                 return 1;
93         }
94
95         if (!manageDSAit && is_entry_referral( e ) ) {
96                 /* entry is a referral, don't allow add */
97                 char *matched_dn = ch_strdup( e->e_dn );
98                 struct berval **refs = get_entry_referrals( be,
99                         conn, op, e );
100
101                 cache_return_entry_r( &li->li_cache, e );
102
103                 Debug( LDAP_DEBUG_TRACE,
104                         "ldbm_search: entry is referral\n",
105                         0, 0, 0 );
106
107                 send_ldap_result( conn, op, LDAP_REFERRAL,
108                     matched_dn, NULL, refs, NULL );
109
110                 ber_bvecfree( refs );
111                 free( matched_dn );
112
113                 return 1;
114         }
115
116         if ( is_entry_alias( e ) ) {
117                 /* don't deref */
118                 deref = LDAP_DEREF_NEVER;
119         }
120
121         if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
122                 tlimit = -1;    /* allow root to set no limit */
123         } else {
124                 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
125                     be->be_timelimit : tlimit;
126                 stoptime = op->o_time + tlimit;
127         }
128
129         if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
130                 slimit = -1;    /* allow root to set no limit */
131         } else {
132                 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
133                     be->be_sizelimit : slimit;
134         }
135
136         if ( scope == LDAP_SCOPE_BASE ) {
137                 candidates = base_candidate( be, e );
138
139         } else {
140                 candidates = search_candidates( be, e, filter,
141                     scope, deref, manageDSAit );
142         }
143
144         /* need normalized dn below */
145         realbase = ch_strdup( e->e_ndn );
146
147         cache_return_entry_r( &li->li_cache, e );
148
149         if ( candidates == NULL ) {
150                 /* no candidates */
151                 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
152                         0, 0, 0 );
153
154                 send_search_result( conn, op,
155                         LDAP_SUCCESS,
156                         NULL, NULL, NULL, NULL, 0 );
157
158                 rc = 1;
159                 goto done;
160         }
161
162         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
163             id = idl_nextid( candidates, &cursor ) )
164         {
165                 int             scopeok = 0;
166
167                 /* check for abandon */
168                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
169
170                 if ( op->o_abandon ) {
171                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
172                         rc = 0;
173                         goto done;
174                 }
175
176                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
177
178                 /* check time limit */
179                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
180                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
181                                 NULL, NULL, v2refs, NULL, nentries );
182                         rc = 0;
183                         goto done;
184                 }
185
186                 /* get the entry with reader lock */
187                 e = id2entry_r( be, id );
188
189                 if ( e == NULL ) {
190                         Debug( LDAP_DEBUG_TRACE,
191                                 "ldbm_search: candidate %ld not found\n",
192                                 id, 0, 0 );
193
194                         goto loop_continue;
195                 }
196
197                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
198                         Entry *matched;
199                         int err;
200                         const char *text;
201                         
202                         e = deref_entry_r( be, e, &err, &matched, &text );
203
204                         if( e == NULL ) {
205                                 e = matched;
206                                 goto loop_continue;
207                         }
208
209                         if( e->e_id == id ) {
210                                 /* circular loop */
211                                 goto loop_continue;
212                         }
213
214                         /* need to skip alias which deref into scope */
215                         if( scope & LDAP_SCOPE_ONELEVEL ) {
216                                 char *pdn = dn_parent( NULL, e->e_ndn );
217                                 if ( pdn != NULL ) {
218                                         if( strcmp( pdn, realbase ) ) {
219                                                 free( pdn );
220                                                 goto loop_continue;
221                                         }
222                                         free(pdn);
223                                 }
224
225                         } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
226                                 /* alias is within scope */
227                                 Debug( LDAP_DEBUG_TRACE,
228                                         "ldbm_search: \"%s\" in subtree\n",
229                                         e->e_dn, 0, 0 );
230                                 goto loop_continue;
231                         }
232
233                         scopeok = 1;
234                 }
235
236                 /*
237                  * if it's a referral, add it to the list of referrals. only do
238                  * this for non-base searches, and don't check the filter
239                  * explicitly here since it's only a candidate anyway.
240                  */
241                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
242                         is_entry_referral( e ) )
243                 {
244                         struct berval **refs = get_entry_referrals(
245                                 be, conn, op, e );
246
247                         send_search_reference( be, conn, op,
248                                 e, refs, scope, NULL, &v2refs );
249
250                         ber_bvecfree( refs );
251
252                         goto loop_continue;
253                 }
254
255                 /* if it matches the filter and scope, send it */
256                 if ( test_filter( be, conn, op, e, filter ) == LDAP_COMPARE_TRUE ) {
257                         char    *dn;
258
259                         /* check scope */
260                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
261                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
262                                         (void) dn_normalize( dn );
263                                         scopeok = (dn == realbase)
264                                                 ? 1
265                                                 : (strcmp( dn, realbase ) ? 0 : 1 );
266                                         free( dn );
267
268                                 } else {
269                                         scopeok = (realbase == NULL || *realbase == '\0');
270                                 }
271
272                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
273                                 dn = ch_strdup( e->e_ndn );
274                                 scopeok = dn_issuffix( dn, realbase );
275                                 free( dn );
276
277                         } else {
278                                 scopeok = 1;
279                         }
280
281                         if ( scopeok ) {
282                                 /* check size limit */
283                                 if ( --slimit == -1 ) {
284                                         cache_return_entry_r( &li->li_cache, e );
285                                         send_search_result( conn, op,
286                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
287                                                 v2refs, NULL, nentries );
288                                         rc = 0;
289                                         goto done;
290                                 }
291
292                                 if (e) {
293                                         int result = send_search_entry(be, conn, op,
294                                                 e, attrs, attrsonly, NULL);
295
296                                         switch (result) {
297                                         case 0:         /* entry sent ok */
298                                                 nentries++;
299                                                 break;
300                                         case 1:         /* entry not sent */
301                                                 break;
302                                         case -1:        /* connection closed */
303                                                 cache_return_entry_r( &li->li_cache, e );
304                                                 rc = 0;
305                                                 goto done;
306                                         }
307                                 }
308                         } else {
309                                 Debug( LDAP_DEBUG_TRACE,
310                                         "ldbm_search: candidate %ld scope not okay\n",
311                                         id, 0, 0 );
312                         }
313                 } else {
314                         Debug( LDAP_DEBUG_TRACE,
315                                 "ldbm_search: candidate %ld does not match filter\n",
316                                 id, 0, 0 );
317                 }
318
319 loop_continue:
320                 if( e != NULL ) {
321                         /* free reader lock */
322                         cache_return_entry_r( &li->li_cache, e );
323                 }
324
325                 ldap_pvt_thread_yield();
326         }
327         send_search_result( conn, op,
328                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
329                 NULL, NULL, v2refs, NULL, nentries );
330
331         rc = 0;
332
333 done:
334         if( candidates != NULL )
335                 idl_free( candidates );
336
337         ber_bvecfree( v2refs );
338         if( realbase ) free( realbase );
339
340         return rc;
341 }
342
343 static ID_BLOCK *
344 base_candidate(
345     Backend     *be,
346         Entry   *e )
347 {
348         ID_BLOCK                *idl;
349
350         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
351                 e->e_dn, 0, 0);
352
353         idl = idl_alloc( 1 );
354         idl_insert( &idl, e->e_id, 1 );
355
356         return( idl );
357 }
358
359 static ID_BLOCK *
360 search_candidates(
361     Backend     *be,
362     Entry       *e,
363     Filter      *filter,
364     int         scope,
365         int             deref,
366         int             manageDSAit )
367 {
368         ID_BLOCK                *candidates;
369         Filter          f, fand, rf, af, xf;
370     AttributeAssertion aa_ref, aa_alias;
371         static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
372         static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
373
374         Debug(LDAP_DEBUG_TRACE,
375                 "search_candidates: base=\"%s\" s=%d d=%d\n",
376                 e->e_ndn, scope, deref );
377
378         xf.f_or = filter;
379         xf.f_choice = LDAP_FILTER_OR;
380         xf.f_next = NULL;
381
382         if( !manageDSAit ) {
383                 /* match referrals */
384                 rf.f_choice = LDAP_FILTER_EQUALITY;
385                 rf.f_ava = &aa_ref;
386                 rf.f_av_desc = slap_schema.si_ad_objectClass;
387                 rf.f_av_value = &bv_ref;
388                 rf.f_next = xf.f_or;
389                 xf.f_or = &rf;
390         }
391
392         if( deref & LDAP_DEREF_SEARCHING ) {
393                 /* match aliases */
394                 af.f_choice = LDAP_FILTER_EQUALITY;
395                 af.f_ava = &aa_alias;
396                 af.f_av_desc = slap_schema.si_ad_objectClass;
397                 af.f_av_value = &bv_alias;
398                 af.f_next = xf.f_or;
399                 xf.f_or = &af;
400         }
401
402         f.f_next = NULL;
403         f.f_choice = LDAP_FILTER_AND;
404         f.f_and = &fand;
405         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
406                 ? SLAPD_FILTER_DN_SUBTREE
407                 : SLAPD_FILTER_DN_ONE;
408         fand.f_dn = e->e_ndn;
409         fand.f_next = xf.f_or == filter ? filter : &xf ;
410
411         candidates = filter_candidates( be, &f );
412
413         return( candidates );
414 }