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