]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/search.c
Add support for unsolicited notifications.
[openldap] / servers / slapd / back-bdb2 / search.c
1 /* search.c - bdb2 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 #include <ac/time.h>
10
11 #include "slap.h"
12 #include "back-bdb2.h"
13 #include "proto-back-bdb2.h"
14
15 static ID_BLOCK *base_candidates(BackendDB *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
16 static ID_BLOCK *onelevel_candidates(BackendDB *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
17 static ID_BLOCK *subtree_candidates(BackendDB *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, Entry *e, int *err, int lookupbase);
18
19 #define GRABSIZE        BUFSIZ
20
21 #define MAKE_SPACE( n ) { \
22         if ( rcur + (n) > rbuf + rmaxsize ) { \
23                 int     offset = rcur - rbuf; \
24                 rbuf =  ch_realloc( rbuf, rmaxsize + GRABSIZE ); \
25                 rmaxsize += GRABSIZE; \
26                 rcur = rbuf + offset; \
27         } \
28 }
29
30 static int
31 bdb2i_back_search_internal(
32     BackendDB   *be,
33     Connection  *conn,
34     Operation   *op,
35     char        *base,
36     int         scope,
37     int         deref,
38     int         slimit,
39     int         tlimit,
40     Filter      *filter,
41     char        *filterstr,
42     char        **attrs,
43     int         attrsonly
44 )
45 {
46         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
47         int             err;
48         time_t          stoptime;
49         ID_BLOCK                *candidates;
50         ID              id;
51         Entry           *e;
52         Attribute       *ref;
53         char            *matched = NULL;
54         int             rmaxsize, nrefs;
55         char            *rbuf, *rcur;
56         int             nentries = 0;
57         char            *realBase;
58
59         Debug(LDAP_DEBUG_ARGS, "=> bdb2i_back_search\n", 0, 0, 0);
60
61         if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
62                 tlimit = -1;    /* allow root to set no limit */
63         } else {
64                 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
65                     be->be_timelimit : tlimit;
66                 stoptime = op->o_time + tlimit;
67         }
68         if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
69                 slimit = -1;    /* allow root to set no limit */
70         } else {
71                 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
72                     be->be_sizelimit : slimit;
73         }
74
75         /*
76          * check and apply aliasing where the dereferencing applies to
77          * the subordinates of the base
78          */
79
80         switch ( deref ) {
81         case LDAP_DEREF_FINDING:
82         case LDAP_DEREF_ALWAYS:
83                 realBase = bdb2i_derefDN ( be, conn, op, base );
84                 break;
85         default:
86                 realBase = ch_strdup(base);
87         }
88
89         (void) dn_normalize_case( realBase );
90
91         Debug( LDAP_DEBUG_TRACE, "using base \"%s\"\n",
92                 realBase, 0, 0 );
93
94         switch ( scope ) {
95         case LDAP_SCOPE_BASE:
96                 candidates = base_candidates( be, conn, op, realBase, filter,
97                     attrs, attrsonly, &matched, &err );
98                 break;
99
100         case LDAP_SCOPE_ONELEVEL:
101                 candidates = onelevel_candidates( be, conn, op, realBase, filter,
102                     attrs, attrsonly, &matched, &err );
103                 break;
104
105         case LDAP_SCOPE_SUBTREE:
106                 candidates = subtree_candidates( be, conn, op, realBase, filter,
107                     attrs, attrsonly, &matched, NULL, &err, 1 );
108                 break;
109
110         default:
111                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR, "",
112                     "Bad scope" );
113                 if( realBase != NULL) {
114                         free( realBase );
115                 }
116                 return( -1 );
117         }
118
119         /* null candidates means we could not find the base object */
120         if ( candidates == NULL ) {
121                 send_ldap_result( conn, op, err, matched, "" );
122                 if ( matched != NULL ) {
123                         free( matched );
124                 }
125                 if( realBase != NULL) {
126                         free( realBase );
127                 }
128                 return( -1 );
129         }
130
131         if ( matched != NULL ) {
132                 free( matched );
133         }
134
135         rmaxsize = 0;
136         nrefs = 0;
137         rbuf = rcur = NULL;
138         MAKE_SPACE( sizeof("Referral:") + 1 );
139         strcpy( rbuf, "Referral:" );
140         rcur = strchr( rbuf, '\0' );
141         for ( id = bdb2i_idl_firstid( candidates ); id != NOID;
142             id = bdb2i_idl_nextid( candidates, id ) ) {
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                         bdb2i_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                         bdb2i_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 = bdb2i_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                                                 bdb2i_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                                                 bdb2i_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 = bdb2i_derefAlias_r( be, conn, op, e );
254                                                         if ( newe == NULL ) { /* problem with the alias */
255                                                                 bdb2i_cache_return_entry_r( &li->li_cache, e );
256                                                                 e = NULL;
257                                                         }
258                                                         else if ( newe != e ) { /* reassign e */
259                                                                 bdb2i_cache_return_entry_r( &li->li_cache, e );
260                                                                 e = newe;
261                                                         }
262                                                 }
263                                                 break;
264                                         }
265
266                                         if (e) {
267                                                 switch ( send_search_entry( be, conn, op, e,
268                                                         attrs, attrsonly, 0 ) ) {
269                                                 case 0:         /* entry sent ok */
270                                                         nentries++;
271                                                         break;
272                                                 case 1:         /* entry not sent */
273                                                         break;
274                                                 case -1:        /* connection closed */
275                                                         bdb2i_cache_return_entry_r( &li->li_cache, e );
276                                                         bdb2i_idl_free( candidates );
277                                                         free( rbuf );
278
279                                                         if( realBase != NULL) {
280                                                                 free( realBase );
281                                                         }
282                                                         return( 0 );
283                                                 }
284                                         }
285                                 }
286                         }
287                 }
288
289                 if( e != NULL ) {
290                         /* free reader lock */
291                         bdb2i_cache_return_entry_r( &li->li_cache, e );
292                 }
293
294                 ldap_pvt_thread_yield();
295         }
296         bdb2i_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
314 int
315 bdb2_back_search(
316     BackendDB   *be,
317     Connection  *conn,
318     Operation   *op,
319     char        *base,
320     int         scope,
321     int         deref,
322     int         slimit,
323     int         tlimit,
324     Filter      *filter,
325     char        *filterstr,
326     char        **attrs,
327     int         attrsonly
328 )
329 {
330         DB_LOCK         lock;
331         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
332         struct timeval  time1;
333         int             ret;
334
335         bdb2i_start_timing( be->bd_info, &time1 );
336
337         if ( bdb2i_enter_backend_r( &lock ) != 0 ) {
338
339                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
340                 return( -1 );
341
342         }
343
344         ret = bdb2i_back_search_internal( be, conn, op, base, scope, deref,
345                                         slimit, tlimit, filter, filterstr, attrs, attrsonly );
346
347         (void) bdb2i_leave_backend_r( lock );
348         bdb2i_stop_timing( be->bd_info, time1, "SRCH", conn, op );
349
350         return( ret );
351 }
352
353
354 static ID_BLOCK *
355 base_candidates(
356     BackendDB   *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         ID_BLOCK                *idl;
369         Entry           *e;
370
371         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n", base, 0, 0);
372
373         *err = LDAP_SUCCESS;
374
375         /* get entry with reader lock */
376         if ( (e = bdb2i_dn2entry_r( be, base, matched )) == NULL ) {
377                 *err = LDAP_NO_SUCH_OBJECT;
378                 return( NULL );
379         }
380
381         /* check for deleted */
382
383         idl = bdb2i_idl_alloc( 1 );
384         bdb2i_idl_insert( &idl, e->e_id, 1 );
385
386
387         /* free reader lock */
388         bdb2i_cache_return_entry_r( &li->li_cache, e );
389
390         return( idl );
391 }
392
393 static ID_BLOCK *
394 onelevel_candidates(
395     BackendDB   *be,
396     Connection  *conn,
397     Operation   *op,
398     char        *base,
399     Filter      *filter,
400     char        **attrs,
401     int         attrsonly,
402     char        **matched,
403     int         *err
404 )
405 {
406         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
407         Entry           *e = NULL;
408         Filter          *f;
409         char            buf[20];
410         ID_BLOCK                *candidates;
411
412         Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: \"%s\"\n", base, 0, 0);
413
414         *err = LDAP_SUCCESS;
415
416         /* get the base object with reader lock */
417         if ( base != NULL && *base != '\0' &&
418                 (e = bdb2i_dn2entry_r( be, base, matched )) == NULL )
419         {
420                 *err = LDAP_NO_SUCH_OBJECT;
421                 return( NULL );
422         }
423
424         /*
425          * modify the filter to be something like this:
426          *
427          *      parent=baseobject & originalfilter
428          */
429
430         f = (Filter *) ch_malloc( sizeof(Filter) );
431         f->f_next = NULL;
432         f->f_choice = LDAP_FILTER_AND;
433         f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
434         f->f_and->f_choice = LDAP_FILTER_EQUALITY;
435         f->f_and->f_ava.ava_type = ch_strdup( "id2children" );
436         sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
437         f->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
438         f->f_and->f_ava.ava_value.bv_len = strlen( buf );
439         f->f_and->f_next = filter;
440
441         /* from here, it's just like subtree_candidates */
442         candidates = subtree_candidates( be, conn, op, base, f, attrs,
443             attrsonly, matched, e, err, 0 );
444
445         /* free up just the filter stuff we allocated above */
446         f->f_and->f_next = NULL;
447         filter_free( f );
448
449         /* free entry and reader lock */
450         if( e != NULL ) {
451                 bdb2i_cache_return_entry_r( &li->li_cache, e );
452         }
453         return( candidates );
454 }
455
456 static ID_BLOCK *
457 subtree_candidates(
458     BackendDB   *be,
459     Connection  *conn,
460     Operation   *op,
461     char        *base,
462     Filter      *filter,
463     char        **attrs,
464     int         attrsonly,
465     char        **matched,
466     Entry       *e,
467     int         *err,
468     int         lookupbase
469 )
470 {
471         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
472         Filter          *f, **filterarg_ptr;
473         ID_BLOCK                *candidates;
474
475         Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: \"%s\" %s\n",
476                 base ? base : "NULL", lookupbase ? "lookupbase" : "", 0);
477
478         /*
479          * get the base object - unless we already have it (from one-level).
480          * also, unless this is a one-level search or a subtree search
481          * starting at the very top of our subtree, we need to modify the
482          * filter to be something like this:
483          *
484          *      dn=*baseobjectdn & (originalfilter | ref=*)
485          *
486          * the "objectclass=referral" part is used to select referrals to return
487          */
488
489         *err = LDAP_SUCCESS;
490         f = NULL;
491         if ( lookupbase ) {
492                 e = NULL;
493
494                 if ( base != NULL && *base != '\0' &&
495                         (e = bdb2i_dn2entry_r( be, base, matched )) == NULL )
496                 {
497                         *err = LDAP_NO_SUCH_OBJECT;
498                         return( NULL );
499                 }
500
501                 if (e) {
502                         bdb2i_cache_return_entry_r( &li->li_cache, e );
503                 }
504
505                 f = (Filter *) ch_malloc( sizeof(Filter) );
506                 f->f_next = NULL;
507                 f->f_choice = LDAP_FILTER_OR;
508                 f->f_or = (Filter *) ch_malloc( sizeof(Filter) );
509                 f->f_or->f_choice = LDAP_FILTER_EQUALITY;
510                 f->f_or->f_avtype = ch_strdup( "objectclass" );
511                 /* Patch to use normalized uppercase */
512                 f->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
513                 f->f_or->f_avvalue.bv_len = strlen( "REFERRAL" );
514                 filterarg_ptr = &f->f_or->f_next;
515                 *filterarg_ptr = filter;
516                 filter = f;
517
518                 if ( ! be_issuffix( be, base ) ) {
519                         f = (Filter *) ch_malloc( sizeof(Filter) );
520                         f->f_next = NULL;
521                         f->f_choice = LDAP_FILTER_AND;
522                         f->f_and = (Filter *) ch_malloc( sizeof(Filter) );
523                         f->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
524                         f->f_and->f_sub_type = ch_strdup( "dn" );
525                         f->f_and->f_sub_initial = NULL;
526                         f->f_and->f_sub_any = NULL;
527                         f->f_and->f_sub_final = ch_strdup( base );
528                         value_normalize( f->f_and->f_sub_final, SYNTAX_CIS );
529                         f->f_and->f_next = filter;
530                         filter = f;
531                 }
532         }
533
534         candidates = bdb2i_filter_candidates( be, filter );
535
536         /* free up just the parts we allocated above */
537         if ( f != NULL ) {
538                 *filterarg_ptr = NULL;
539                 filter_free( f );
540         }
541
542         return( candidates );
543 }