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