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