]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
2858e249610486af8ab6af19568ee75175f87bd5
[openldap] / servers / slapd / back-ldbm / search.c
1 /* search.c - ldbm backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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     char        *base,
33     int         scope,
34     int         deref,
35     int         slimit,
36     int         tlimit,
37     Filter      *filter,
38     char        *filterstr,
39     char        **attrs,
40     int         attrsonly
41 )
42 {
43         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
44         int             rc, err;
45         char *text;
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, base, &err, &matched, &text );
61
62         } else {
63                 e = dn2entry_r( be, base, &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, "entry is referral\n", 0,
104                     0, 0 );
105
106                 send_ldap_result( conn, op, LDAP_REFERRAL,
107                     matched_dn, NULL, refs, NULL );
108
109                 ber_bvecfree( refs );
110                 free( matched_dn );
111
112                 return 1;
113         }
114
115         if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
116                 tlimit = -1;    /* allow root to set no limit */
117         } else {
118                 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
119                     be->be_timelimit : tlimit;
120                 stoptime = op->o_time + tlimit;
121         }
122
123         if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
124                 slimit = -1;    /* allow root to set no limit */
125         } else {
126                 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
127                     be->be_sizelimit : slimit;
128         }
129
130         if ( scope == LDAP_SCOPE_BASE) {
131                 candidates = base_candidate( be, e );
132
133         } else {
134                 candidates = search_candidates( be, e, filter,
135                     scope, deref, manageDSAit );
136         }
137
138         /* need normalized dn below */
139         realbase = ch_strdup( e->e_ndn );
140         cache_return_entry_r( &li->li_cache, e );
141
142         if ( candidates == NULL ) {
143                 /* no candidates */
144                 Debug( LDAP_DEBUG_TRACE, "no candidates\n", 0,
145                     0, 0 );
146
147                 send_search_result( conn, op,
148                         LDAP_SUCCESS,
149                         NULL, NULL, NULL, NULL, 0 );
150
151                 rc = 1;
152                 goto done;
153         }
154
155         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
156             id = idl_nextid( candidates, &cursor ) )
157         {
158                 int             scopeok = 0;
159
160                 /* check for abandon */
161                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
162
163                 if ( op->o_abandon ) {
164                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
165                         rc = 0;
166                         goto done;
167                 }
168
169                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
170
171                 /* check time limit */
172                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
173                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
174                                 NULL, NULL, v2refs, NULL, nentries );
175                         rc = 0;
176                         goto done;
177                 }
178
179                 /* get the entry with reader lock */
180                 e = id2entry_r( be, id );
181
182                 if ( e == NULL ) {
183                         Debug( LDAP_DEBUG_ARGS, "search: candidate %ld not found\n",
184                                 id, 0, 0 );
185
186                         goto loop_continue;
187                 }
188
189                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
190                         Entry *matched;
191                         int err;
192                         char *text;
193                         
194                         e = deref_entry_r( be, e, &err, &matched, &text );
195
196                         if( e == NULL ) {
197                                 e = matched;
198                                 goto loop_continue;
199                         }
200
201                         if( e->e_id == id ) {
202                                 /* circular loop */
203                                 goto loop_continue;
204                         }
205
206                         /* need to skip alias which deref into scope */
207                         if( scope & LDAP_SCOPE_ONELEVEL ) {
208                                 char *pdn = dn_parent( NULL, e->e_ndn );
209                                 if ( pdn != NULL ) {
210                                         if( strcmp( pdn, realbase ) ) {
211                                                 free( pdn );
212                                                 goto loop_continue;
213                                         }
214                                         free(pdn);
215                                 }
216
217                         } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
218                                 /* alias is within scope */
219                                 Debug( LDAP_DEBUG_ARGS, "search: \"%s\" in subtree\n",
220                                         e->e_dn, 0, 0 );
221                                 goto loop_continue;
222                         }
223
224                         scopeok = 1;
225                 }
226
227                 /*
228                  * if it's a referral, add it to the list of referrals. only do
229                  * this for non-base searches, and don't check the filter
230                  * explicitly here since it's only a candidate anyway.
231                  */
232                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
233                         is_entry_referral( e ) )
234                 {
235                         struct berval **refs = get_entry_referrals(
236                                 be, conn, op, e );
237
238                         send_search_reference( be, conn, op,
239                                 e, refs, scope, NULL, &v2refs );
240
241                         ber_bvecfree( refs );
242
243                         goto loop_continue;
244                 }
245
246                 /* if it matches the filter and scope, send it */
247                 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
248                         char    *dn;
249
250                         /* check scope */
251                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
252                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
253                                         (void) dn_normalize_case( dn );
254                                         scopeok = (dn == realbase)
255                                                 ? 1
256                                                 : (strcmp( dn, realbase ) ? 0 : 1 );
257                                         free( dn );
258
259                                 } else {
260                                         scopeok = (realbase == NULL || *realbase == '\0');
261                                 }
262
263                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
264                                 dn = ch_strdup( e->e_ndn );
265                                 scopeok = dn_issuffix( dn, realbase );
266                                 free( dn );
267
268                         } else {
269                                 scopeok = 1;
270                         }
271
272                         if ( scopeok ) {
273                                 /* check size limit */
274                                 if ( --slimit == -1 ) {
275                                         cache_return_entry_r( &li->li_cache, e );
276                                         send_search_result( conn, op,
277                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
278                                                 v2refs, NULL, nentries );
279                                         rc = 0;
280                                         goto done;
281                                 }
282
283                                 if (e) {
284                                         switch ( send_search_entry( be, conn, op, e,
285                                                 attrs, attrsonly, NULL ) ) {
286                                         case 0:         /* entry sent ok */
287                                                 nentries++;
288                                                 break;
289                                         case 1:         /* entry not sent */
290                                                 break;
291                                         case -1:        /* connection closed */
292                                                 cache_return_entry_r( &li->li_cache, e );
293                                                 rc = 0;
294                                                 goto done;
295                                         }
296                                 }
297                         } else {
298                                 Debug( LDAP_DEBUG_TRACE, "candidate %ld scope not okay\n",
299                                         id, 0, 0 );
300                         }
301                 } else {
302                         Debug( LDAP_DEBUG_TRACE, "candidate %ld does match filter\n",
303                                 id, 0, 0 );
304                 }
305
306 loop_continue:
307                 if( e != NULL ) {
308                         /* free reader lock */
309                         cache_return_entry_r( &li->li_cache, e );
310                 }
311
312                 ldap_pvt_thread_yield();
313         }
314         send_search_result( conn, op,
315                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
316                 NULL, NULL, v2refs, NULL, nentries );
317
318         rc = 0;
319
320 done:
321         if( candidates != NULL )
322                 idl_free( candidates );
323
324         ber_bvecfree( v2refs );
325         if( realbase ) free( realbase );
326
327         return rc;
328 }
329
330 static ID_BLOCK *
331 base_candidate(
332     Backend     *be,
333         Entry   *e
334 )
335 {
336         ID_BLOCK                *idl;
337
338         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
339                 e->e_dn, 0, 0);
340
341         idl = idl_alloc( 1 );
342         idl_insert( &idl, e->e_id, 1 );
343
344         return( idl );
345 }
346
347 static ID_BLOCK *
348 search_candidates(
349     Backend     *be,
350     Entry       *e,
351     Filter      *filter,
352     int         scope,
353         int             deref,
354         int             manageDSAit
355 )
356 {
357         ID_BLOCK                *candidates;
358         Filter          *f, *rf, *af, *lf;
359
360         Debug(LDAP_DEBUG_TRACE, "search_candidates: base=\"%s\" s=%d d=%d\n",
361                 e->e_ndn, scope, deref );
362
363         f = NULL;
364
365         if( !manageDSAit ) {
366                 /* match referrals */
367                 rf = (Filter *) ch_malloc( sizeof(Filter) );
368                 rf->f_next = NULL;
369                 rf->f_choice = LDAP_FILTER_OR;
370                 rf->f_or = (Filter *) ch_malloc( sizeof(Filter) );
371                 rf->f_or->f_choice = LDAP_FILTER_EQUALITY;
372                 rf->f_or->f_avtype = ch_strdup( "objectclass" );
373                 rf->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
374                 rf->f_or->f_avvalue.bv_len = sizeof("REFERRAL")-1;
375                 rf->f_or->f_next = filter;
376                 f = rf;
377         } else {
378                 rf = NULL;
379                 f = filter;
380         }
381
382         if( deref & LDAP_DEREF_SEARCHING ) {
383                 /* match aliases */
384                 af = (Filter *) ch_malloc( sizeof(Filter) );
385                 af->f_next = NULL;
386                 af->f_choice = LDAP_FILTER_OR;
387                 af->f_or = (Filter *) ch_malloc( sizeof(Filter) );
388                 af->f_or->f_choice = LDAP_FILTER_EQUALITY;
389                 af->f_or->f_avtype = ch_strdup( "objectclass" );
390                 af->f_or->f_avvalue.bv_val = ch_strdup( "ALIAS" );
391                 af->f_or->f_avvalue.bv_len = sizeof("ALIAS")-1;
392                 af->f_or->f_next = f;
393                 f = af;
394         } else {
395                 af = NULL;
396         }
397
398         if ( scope == LDAP_SCOPE_SUBTREE ) {
399                 lf = (Filter *) ch_malloc( sizeof(Filter) );
400                 lf->f_next = NULL;
401                 lf->f_choice = LDAP_FILTER_AND;
402                 lf->f_and = (Filter *) ch_malloc( sizeof(Filter) );
403
404                 lf->f_and->f_choice = SLAPD_FILTER_DN_SUBTREE;
405                 lf->f_and->f_dn = e->e_ndn;
406
407                 lf->f_and->f_next = f;
408                 f = lf;
409
410         } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
411                 lf = (Filter *) ch_malloc( sizeof(Filter) );
412                 lf->f_next = NULL;
413                 lf->f_choice = LDAP_FILTER_AND;
414                 lf->f_and = (Filter *) ch_malloc( sizeof(Filter) );
415
416                 lf->f_and->f_choice = SLAPD_FILTER_DN_ONE;
417                 lf->f_and->f_dn = e->e_ndn;
418
419                 lf->f_and->f_next = f;
420                 f = lf;
421
422         } else {
423                 lf = NULL;
424         }
425
426         candidates = filter_candidates( be, f );
427
428         /* free up filter additions we allocated above */
429         if( lf != NULL ) {
430                 free( lf->f_and );
431                 free( lf );
432         }
433
434         if( af != NULL ) {
435                 af->f_or->f_next = NULL;
436                 filter_free( af );
437         }
438
439         if( rf != NULL ) {
440                 rf->f_or->f_next = NULL;
441                 filter_free( rf );
442         }
443
444         return( candidates );
445 }