]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
merge with main branch
[openldap] / servers / slapd / back-ldbm / search.c
1 /* search.c - ldbm backend search function */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/string.h>
8 #include <ac/socket.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
13
14 extern time_t           currenttime;
15 extern pthread_mutex_t  currenttime_mutex;
16
17 extern IDList           *idl_alloc();
18 extern Attribute        *attr_find();
19 extern IDList           *filter_candidates();
20 extern char             *ch_realloc();
21 extern char             *dn_parent();
22
23 static IDList   *base_candidates();
24 static IDList   *onelevel_candidates();
25 static IDList   *subtree_candidates();
26
27 #define GRABSIZE        BUFSIZ
28
29 #define MAKE_SPACE( n ) { \
30         if ( rcur + n > rbuf + rmaxsize ) { \
31                 int     offset = rcur - rbuf; \
32                 rbuf =  ch_realloc( rbuf, rmaxsize + GRABSIZE ); \
33                 rmaxsize += GRABSIZE; \
34                 rcur = rbuf + offset; \
35         } \
36 }
37
38 int
39 ldbm_back_search(
40     Backend     *be,
41     Connection  *conn,
42     Operation   *op,
43     char        *base,
44     int         scope,
45     int         deref,
46     int         slimit,
47     int         tlimit,
48     Filter      *filter,
49     char        *filterstr,
50     char        **attrs,
51     int         attrsonly
52 )
53 {
54         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
55         int             err;
56         time_t          stoptime;
57         IDList          *candidates;
58         ID              id;
59         Entry           *e;
60         Attribute       *ref;
61         char            *matched = NULL;
62         int             rmaxsize, nrefs;
63         char            *rbuf, *rcur, *r;
64         int             nentries = 0;
65         char            *realBase;
66
67         Debug(LDAP_DEBUG_ARGS, "=> ldbm_back_search\n", 0, 0, 0);
68
69         if ( tlimit == 0 && be_isroot( be, op->o_dn ) ) {
70                 tlimit = -1;    /* allow root to set no limit */
71         } else {
72                 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
73                     be->be_timelimit : tlimit;
74                 stoptime = op->o_time + tlimit;
75         }
76         if ( slimit == 0 && be_isroot( be, op->o_dn ) ) {
77                 slimit = -1;    /* allow root to set no limit */
78         } else {
79                 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
80                     be->be_sizelimit : slimit;
81         }
82
83         /*
84          * check and apply aliasing where the dereferencing applies to
85          * the subordinates of the base
86          */
87         realBase = strdup (base);
88         switch ( deref ) {
89         case LDAP_DEREF_FINDING:
90         case LDAP_DEREF_ALWAYS:
91                 free (realBase);
92                 realBase = derefDN ( be, conn, op, base );
93                 break;
94         }
95
96         (void) dn_normalize (realBase);
97
98         Debug( LDAP_DEBUG_TRACE, "using base %s\n",
99                 realBase, 0, 0 );
100
101         switch ( scope ) {
102         case LDAP_SCOPE_BASE:
103                 candidates = base_candidates( be, conn, op, realBase, filter,
104                     attrs, attrsonly, &matched, &err );
105                 break;
106
107         case LDAP_SCOPE_ONELEVEL:
108                 candidates = onelevel_candidates( be, conn, op, realBase, filter,
109                     attrs, attrsonly, &matched, &err );
110                 break;
111
112         case LDAP_SCOPE_SUBTREE:
113                 candidates = subtree_candidates( be, conn, op, realBase, filter,
114                     attrs, attrsonly, &matched, NULL, &err, 1 );
115                 break;
116
117         default:
118                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, "",
119                     "Bad scope" );
120                 return( -1 );
121         }
122
123         /* null candidates means we could not find the base object */
124         if ( candidates == NULL ) {
125                 send_ldap_result( conn, op, err, matched, "" );
126                 if ( matched != NULL ) {
127                         free( matched );
128                 }
129                 return( -1 );
130         }
131
132         rmaxsize = 0;
133         nrefs = 0;
134         rbuf = rcur = NULL;
135         MAKE_SPACE( sizeof("Referral:") + 1 );
136         strcpy( rbuf, "Referral:" );
137         rcur = strchr( rbuf, '\0' );
138         for ( id = idl_firstid( candidates ); id != NOID;
139             id = idl_nextid( candidates, id ) ) {
140                 /* check for abandon */
141                 pthread_mutex_lock( &op->o_abandonmutex );
142                 if ( op->o_abandon ) {
143                         pthread_mutex_unlock( &op->o_abandonmutex );
144                         idl_free( candidates );
145                         free( rbuf );
146                         return( 0 );
147                 }
148                 pthread_mutex_unlock( &op->o_abandonmutex );
149
150                 /* check time limit */
151                 pthread_mutex_lock( &currenttime_mutex );
152                 time( &currenttime );
153                 if ( tlimit != -1 && currenttime > stoptime ) {
154                         pthread_mutex_unlock( &currenttime_mutex );
155                         send_ldap_search_result( conn, op,
156                             LDAP_TIMELIMIT_EXCEEDED, NULL, nrefs > 0 ? rbuf :
157                             NULL, nentries );
158                         idl_free( candidates );
159                         free( rbuf );
160                         return( 0 );
161                 }
162                 pthread_mutex_unlock( &currenttime_mutex );
163
164                 /* get the entry with reader lock */
165                 if ( (e = id2entry_r( be, id )) == NULL ) {
166                         Debug( LDAP_DEBUG_ARGS, "candidate %d not found\n", id, 0, 0 );
167                         continue;
168                 }
169
170                 /*
171                  * if it's a referral, add it to the list of referrals. only do
172                  * this for subtree searches, and don't check the filter explicitly
173                  * here since it's only a candidate anyway.
174                  */
175                 if ( e->e_dn != NULL && strncasecmp( e->e_dn, "ref=", 4 )
176                         == 0 && (ref = attr_find( e->e_attrs, "ref" )) != NULL &&
177                         scope == LDAP_SCOPE_SUBTREE )
178                 {
179                         int     i, len;
180
181                         if ( ref->a_vals == NULL ) {
182                                 Debug( LDAP_DEBUG_ANY, "null ref in (%s)\n", 
183                                         e->e_dn, 0, 0 );
184                         } else {
185                                 for ( i = 0; ref->a_vals[i] != NULL; i++ ) {
186                                         /* referral + newline + null */
187                                         MAKE_SPACE( ref->a_vals[i]->bv_len + 2 );
188                                         *rcur++ = '\n';
189                                         strncpy( rcur, ref->a_vals[i]->bv_val,
190                                                 ref->a_vals[i]->bv_len );
191                                         rcur = rcur + ref->a_vals[i]->bv_len;
192                                         *rcur = '\0';
193                                         nrefs++;
194                                 }
195                         }
196
197                 /* otherwise it's an entry - see if it matches the filter */
198                 } else {
199                         /* if it matches the filter and scope, send it */
200                         if ( test_filter( be, conn, op, e, filter ) == 0 ) {
201                                 int             scopeok;
202                                 char    *dn;
203
204                                 /* check scope */
205                                 scopeok = 1;
206                                 if ( scope == LDAP_SCOPE_ONELEVEL ) {
207                                         if ( (dn = dn_parent( be, e->e_dn )) != NULL ) {
208                                                 (void) dn_normalize( dn );
209                                                 scopeok = (dn == realBase) ? 1 : (! strcasecmp( dn, realBase ));
210                                         } else {
211                                                 scopeok = (realBase == NULL || *realBase == '\0');
212                                         }
213                                         free( dn );
214                                 } else if ( scope == LDAP_SCOPE_SUBTREE ) {
215                                         dn = strdup( e->e_dn );
216                                         (void) dn_normalize( dn );
217                                         scopeok = dn_issuffix( dn, realBase );
218                                         free( dn );
219                                 }
220
221                                 if ( scopeok ) {
222                                         /* check size limit */
223                                         if ( --slimit == -1 ) {
224                                                 cache_return_entry_r( &li->li_cache, e );
225                                                 send_ldap_search_result( conn, op,
226                                                         LDAP_SIZELIMIT_EXCEEDED, NULL,
227                                                         nrefs > 0 ? rbuf : NULL, nentries );
228                                                 idl_free( candidates );
229                                                 free( rbuf );
230                                                 return( 0 );
231                                         }
232
233                                         /*
234                                          * check and apply aliasing where the dereferencing applies to
235                                          * the subordinates of the base
236                                          */
237                                         switch ( deref ) {
238                                         case LDAP_DEREF_SEARCHING:
239                                         case LDAP_DEREF_ALWAYS:
240                                                 {
241                                                         Entry *newe = derefAlias_r( be, conn, op, e );
242                                                         cache_return_entry_r( &li->li_cache, e );
243                                                         e = newe;
244                                                 }
245                                                 break;
246                                         }
247
248                                         switch ( send_search_entry( be, conn, op, e,
249                                                 attrs, attrsonly ) ) {
250                                         case 0:         /* entry sent ok */
251                                                 nentries++;
252                                                 break;
253                                         case 1:         /* entry not sent */
254                                                 break;
255                                         case -1:        /* connection closed */
256                                                 cache_return_entry_r( &li->li_cache, e );
257                                                 idl_free( candidates );
258                                                 free( rbuf );
259                                                 return( 0 );
260                                         }
261                                 }
262                         }
263                 }
264
265                 if( e == NULL ) {
266                         /* free reader lock */
267                         cache_return_entry_r( &li->li_cache, e );
268                 }
269
270                 pthread_yield();
271         }
272         idl_free( candidates );
273         if ( nrefs > 0 ) {
274                 send_ldap_search_result( conn, op, LDAP_PARTIAL_RESULTS, NULL,
275                     rbuf, nentries );
276         } else {
277                 send_ldap_search_result( conn, op, LDAP_SUCCESS, NULL, NULL,
278                     nentries );
279         }
280         free( rbuf );
281
282         return( 0 );
283 }
284
285 static IDList *
286 base_candidates(
287     Backend     *be,
288     Connection  *conn,
289     Operation   *op,
290     char        *base,
291     Filter      *filter,
292     char        **attrs,
293     int         attrsonly,
294     char        **matched,
295     int         *err
296 )
297 {
298         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
299         int             rc;
300         ID              id;
301         IDList          *idl;
302         Entry           *e;
303
304         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: %s\n", base, 0, 0);
305
306         *err = LDAP_SUCCESS;
307
308         /* get entry with reader lock */
309         if ( (e = dn2entry_r( be, base, matched )) == NULL ) {
310                 *err = LDAP_NO_SUCH_OBJECT;
311                 return( NULL );
312         }
313
314         /* check for deleted */
315
316         idl = idl_alloc( 1 );
317         idl_insert( &idl, e->e_id, 1 );
318
319
320         /* free reader lock */
321         cache_return_entry_r( &li->li_cache, e );
322
323         return( idl );
324 }
325
326 static IDList *
327 onelevel_candidates(
328     Backend     *be,
329     Connection  *conn,
330     Operation   *op,
331     char        *base,
332     Filter      *filter,
333     char        **attrs,
334     int         attrsonly,
335     char        **matched,
336     int         *err
337 )
338 {
339         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
340         Entry           *e;
341         Filter          *f;
342         char            buf[20];
343         IDList          *candidates;
344
345         Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: %s\n", base, 0, 0);
346
347         *err = LDAP_SUCCESS;
348         e = NULL;
349         /* get the base object with reader lock */
350         if ( base != NULL && *base != '\0' &&
351                 (e = dn2entry_r( be, base, matched )) == NULL )
352         {
353                 *err = LDAP_NO_SUCH_OBJECT;
354                 return( NULL );
355         }
356
357         /*
358          * modify the filter to be something like this:
359          *
360          *      parent=baseobject & originalfilter
361          */
362
363         f = (Filter *) ch_malloc( sizeof(Filter) );
364         f->f_next = NULL;
365         f->f_choice = LDAP_FILTER_AND;
366         f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
367         f->f_and->f_choice = LDAP_FILTER_EQUALITY;
368         f->f_and->f_ava.ava_type = strdup( "id2children" );
369         sprintf( buf, "%d", e != NULL ? e->e_id : 0 );
370         f->f_and->f_ava.ava_value.bv_val = strdup( buf );
371         f->f_and->f_ava.ava_value.bv_len = strlen( buf );
372         f->f_and->f_next = filter;
373
374         /* from here, it's just like subtree_candidates */
375         candidates = subtree_candidates( be, conn, op, base, f, attrs,
376             attrsonly, matched, e, err, 0 );
377
378         /* free up just the filter stuff we allocated above */
379         f->f_and->f_next = NULL;
380         filter_free( f );
381
382         /* free entry and reader lock */
383         cache_return_entry_r( &li->li_cache, e );
384         return( candidates );
385 }
386
387 static IDList *
388 subtree_candidates(
389     Backend     *be,
390     Connection  *conn,
391     Operation   *op,
392     char        *base,
393     Filter      *filter,
394     char        **attrs,
395     int         attrsonly,
396     char        **matched,
397     Entry       *e,
398     int         *err,
399     int         lookupbase
400 )
401 {
402         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
403         Filter          *f;
404         IDList          *candidates;
405
406         Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: %s\n",
407                 base ? base : "NULL", 0, 0);
408
409         /*
410          * get the base object - unless we already have it (from one-level).
411          * also, unless this is a one-level search or a subtree search
412          * starting at the very top of our subtree, we need to modify the
413          * filter to be something like this:
414          *
415          *      dn=*baseobjectdn & (originalfilter | ref=*)
416          *
417          * the "objectclass=referral" part is used to select referrals to return
418          */
419
420         *err = LDAP_SUCCESS;
421         f = NULL;
422         if ( lookupbase ) {
423                 if ( base != NULL && *base != '\0' &&
424                         (e = dn2entry_r( be, base, matched )) == NULL )
425                 {
426                         *err = LDAP_NO_SUCH_OBJECT;
427                         return( NULL );
428                 }
429
430                 if (e) {
431                         cache_return_entry_r( &li->li_cache, e );
432                 }
433
434                 f = (Filter *) ch_malloc( sizeof(Filter) );
435                 f->f_next = NULL;
436                 f->f_choice = LDAP_FILTER_OR;
437                 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
438                 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
439                 f->f_or->f_avtype = strdup( "objectclass" );
440                 /* Patch to use normalized uppercase */
441                 f->f_or->f_avvalue.bv_val = strdup( "REFERRAL" );
442                 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
443                 f->f_or->f_next = filter;
444                 filter = f;
445
446                 if ( ! be_issuffix( be, base ) ) {
447                         f = (Filter *) ch_malloc( sizeof(Filter) );
448                         f->f_next = NULL;
449                         f->f_choice = LDAP_FILTER_AND;
450                         f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
451                         f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
452                         f->f_and->f_sub_type = strdup( "dn" );
453                         f->f_and->f_sub_initial = NULL;
454                         f->f_and->f_sub_any = NULL;
455                         f->f_and->f_sub_final = strdup( base );
456                         value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
457                         f->f_and->f_next = filter;
458                         filter = f;
459                 }
460         }
461
462         candidates = filter_candidates( be, filter );
463
464         /* free up just the parts we allocated above */
465         if ( f != NULL ) {
466                 f->f_and->f_next = NULL;
467                 filter_free( f );
468         }
469
470         return( candidates );
471 }