]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/search.c
Backout the input exhaustion change, it loops. Still looking for
[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_candidate(
16         Backend *be, Entry *e );
17
18 static ID_BLOCK *search_candidates(
19         Backend *be, Entry *e, Filter *filter,
20         int scope, int deref, int manageDSAit );
21
22 static int
23 bdb2i_back_search_internal(
24     BackendDB   *be,
25     Connection  *conn,
26     Operation   *op,
27     char        *base,
28     int         scope,
29     int         deref,
30     int         slimit,
31     int         tlimit,
32     Filter      *filter,
33     char        *filterstr,
34     char        **attrs,
35     int         attrsonly
36 )
37 {
38         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
39         int             rc, err;
40         char *text;
41         time_t          stoptime;
42         ID_BLOCK                *candidates;
43         ID              id;
44         Entry           *e;
45         struct berval **v2refs = NULL;
46         Entry   *matched = NULL;
47         char    *realbase = NULL;
48         int             nentries = 0;
49         int             manageDSAit = get_manageDSAit( op );
50
51         Debug(LDAP_DEBUG_TRACE, "=> bdb2_back_search\n", 0, 0, 0);
52
53         /* get entry with reader lock */
54         if ( deref & LDAP_DEREF_FINDING ) {
55                 e = deref_dn_r( be, base, &err, &matched, &text );
56
57         } else {
58                 e = bdb2i_dn2entry_r( be, base, &matched );
59                 err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
60                 text = NULL;
61         }
62
63         if ( e == NULL ) {
64                 char *matched_dn = NULL;
65                 struct berval **refs = NULL;
66
67                 if ( matched != NULL ) {
68                         matched_dn = ch_strdup( matched->e_dn );
69
70                         refs = is_entry_referral( matched )
71                                 ? get_entry_referrals( be, conn, op, matched )
72                                 : NULL;
73
74                         bdb2i_cache_return_entry_r( &li->li_cache, matched );
75                 } else {
76                         refs = default_referral;
77                 }
78
79                 send_ldap_result( conn, op, err,
80                         matched_dn, text, refs, NULL );
81
82                 if( matched != NULL ) {
83                         ber_bvecfree( refs );
84                         free( matched_dn );
85                 }
86
87                 return 1;
88         }
89
90         if (!manageDSAit && is_entry_referral( e ) ) {
91                 /* entry is a referral, don't allow add */
92                 char *matched_dn = ch_strdup( e->e_dn );
93                 struct berval **refs = get_entry_referrals( be,
94                         conn, op, e );
95
96                 bdb2i_cache_return_entry_r( &li->li_cache, e );
97
98                 Debug( LDAP_DEBUG_TRACE, "entry is referral\n", 0,
99                     0, 0 );
100
101                 send_ldap_result( conn, op, LDAP_REFERRAL,
102                     matched_dn, NULL, refs, NULL );
103
104                 ber_bvecfree( refs );
105                 free( matched_dn );
106
107                 return 1;
108         }
109
110         if ( tlimit == 0 && be_isroot( be, op->o_ndn ) ) {
111                 tlimit = -1;    /* allow root to set no limit */
112         } else {
113                 tlimit = (tlimit > be->be_timelimit || tlimit < 1) ?
114                     be->be_timelimit : tlimit;
115                 stoptime = op->o_time + tlimit;
116         }
117
118         if ( slimit == 0 && be_isroot( be, op->o_ndn ) ) {
119                 slimit = -1;    /* allow root to set no limit */
120         } else {
121                 slimit = (slimit > be->be_sizelimit || slimit < 1) ?
122                     be->be_sizelimit : slimit;
123         }
124
125         if ( scope == LDAP_SCOPE_BASE) {
126                 candidates = base_candidate( be, e );
127
128         } else {
129                 candidates = search_candidates( be, e, filter,
130                     scope, deref, manageDSAit );
131         }
132
133         /* need normalized dn below */
134         realbase = ch_strdup( e->e_ndn );
135         bdb2i_cache_return_entry_r( &li->li_cache, e );
136
137         if ( candidates == NULL ) {
138                 /* no candidates */
139                 Debug( LDAP_DEBUG_TRACE, "no candidates\n", 0,
140                     0, 0 );
141
142                 send_search_result( conn, op,
143                         LDAP_SUCCESS,
144                         NULL, NULL, NULL, NULL, 0 );
145
146                 rc = 1;
147                 goto done;
148         }
149
150         for ( id = bdb2i_idl_firstid( candidates ); id != NOID;
151             id = bdb2i_idl_nextid( candidates, id ) )
152         {
153                 int             scopeok = 0;
154
155                 /* check for abandon */
156                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
157
158                 if ( op->o_abandon ) {
159                         ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
160                         rc = 0;
161                         goto done;
162                 }
163
164                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
165
166                 /* check time limit */
167                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
168                         send_search_result( conn, op, LDAP_TIMELIMIT_EXCEEDED,
169                                 NULL, NULL, v2refs, NULL, nentries );
170                         rc = 0;
171                         goto done;
172                 }
173
174                 /* get the entry with reader lock */
175                 e = bdb2i_id2entry_r( be, id );
176
177                 if ( e == NULL ) {
178                         Debug( LDAP_DEBUG_ARGS, "search: candidate %ld not found\n",
179                                 id, 0, 0 );
180
181                         goto loop_continue;
182                 }
183
184                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
185                         Entry *matched;
186                         int err;
187                         char *text;
188                         
189                         e = deref_entry_r( be, e, &err, &matched, &text );
190
191                         if( e == NULL ) {
192                                 e = matched;
193                                 goto loop_continue;
194                         }
195
196                         if( e->e_id == id ) {
197                                 /* circular loop */
198                                 goto loop_continue;
199                         }
200
201                         /* need to skip alias which deref into scope */
202                         if( scope & LDAP_SCOPE_ONELEVEL ) {
203                                 char *pdn = dn_parent( NULL, e->e_ndn );
204                                 if ( pdn != NULL ) {
205                                         if( strcmp( pdn, realbase ) ) {
206                                                 free( pdn );
207                                                 goto loop_continue;
208                                         }
209                                         free(pdn);
210                                 }
211
212                         } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
213                                 /* alias is within scope */
214                                 Debug( LDAP_DEBUG_ARGS, "search: \"%s\" in subtree\n",
215                                         e->e_dn, 0, 0 );
216                                 goto loop_continue;
217                         }
218
219                         scopeok = 1;
220                 }
221
222                 /*
223                  * if it's a referral, add it to the list of referrals. only do
224                  * this for non-base searches, and don't check the filter
225                  * explicitly here since it's only a candidate anyway.
226                  */
227                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
228                         is_entry_referral( e ) )
229                 {
230                         struct berval **refs = get_entry_referrals(
231                                 be, conn, op, e );
232
233                         send_search_reference( be, conn, op,
234                                 e, refs, scope, NULL, &v2refs );
235
236                         ber_bvecfree( refs );
237
238                         goto loop_continue;
239                 }
240
241                 /* if it matches the filter and scope, send it */
242                 if ( test_filter( be, conn, op, e, filter ) == 0 ) {
243                         char    *dn;
244
245                         /* check scope */
246                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
247                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
248                                         (void) dn_normalize_case( dn );
249                                         scopeok = (dn == realbase)
250                                                 ? 1
251                                                 : (strcmp( dn, realbase ) ? 0 : 1 );
252                                         free( dn );
253
254                                 } else {
255                                         scopeok = (realbase == NULL || *realbase == '\0');
256                                 }
257
258                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
259                                 dn = ch_strdup( e->e_ndn );
260                                 scopeok = dn_issuffix( dn, realbase );
261                                 free( dn );
262
263                         } else {
264                                 scopeok = 1;
265                         }
266
267                         if ( scopeok ) {
268                                 /* check size limit */
269                                 if ( --slimit == -1 ) {
270                                         bdb2i_cache_return_entry_r( &li->li_cache, e );
271                                         send_search_result( conn, op,
272                                                 LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
273                                                 v2refs, NULL, nentries );
274                                         rc = 0;
275                                         goto done;
276                                 }
277
278                                 if (e) {
279                                         switch ( send_search_entry( be, conn, op, e,
280                                                 attrs, attrsonly, 0, NULL ) ) {
281                                         case 0:         /* entry sent ok */
282                                                 nentries++;
283                                                 break;
284                                         case 1:         /* entry not sent */
285                                                 break;
286                                         case -1:        /* connection closed */
287                                                 bdb2i_cache_return_entry_r( &li->li_cache, e );
288                                                 rc = 0;
289                                                 goto done;
290                                         }
291                                 }
292                         } else {
293                                 Debug( LDAP_DEBUG_TRACE, "candidate %ld scope not okay\n",
294                                         id, 0, 0 );
295                         }
296                 } else {
297                         Debug( LDAP_DEBUG_TRACE, "candidate %ld does match filter\n",
298                                 id, 0, 0 );
299                 }
300
301 loop_continue:
302                 if( e != NULL ) {
303                         /* free reader lock */
304                         bdb2i_cache_return_entry_r( &li->li_cache, e );
305                 }
306
307                 ldap_pvt_thread_yield();
308         }
309         send_search_result( conn, op,
310                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
311                 NULL, NULL, v2refs, NULL, nentries );
312
313         rc = 0;
314
315 done:
316         bdb2i_idl_free( candidates );
317
318         ber_bvecfree( v2refs );
319         if( realbase ) free( realbase );
320
321         return rc;
322 }
323
324
325 int
326 bdb2_back_search(
327     BackendDB   *be,
328     Connection  *conn,
329     Operation   *op,
330     char        *base,
331     int         scope,
332     int         deref,
333     int         slimit,
334     int         tlimit,
335     Filter      *filter,
336     char        *filterstr,
337     char        **attrs,
338     int         attrsonly
339 )
340 {
341         DB_LOCK         lock;
342         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
343         struct timeval  time1;
344         int             ret;
345
346         bdb2i_start_timing( be->bd_info, &time1 );
347
348         if ( bdb2i_enter_backend_r( &lock ) != 0 ) {
349                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
350                         NULL, NULL, NULL, NULL );
351                 return( -1 );
352
353         }
354
355         ret = bdb2i_back_search_internal( be, conn, op, base, scope, deref,
356                                         slimit, tlimit, filter, filterstr, attrs, attrsonly );
357
358         (void) bdb2i_leave_backend_r( lock );
359         bdb2i_stop_timing( be->bd_info, time1, "SRCH", conn, op );
360
361         return( ret );
362 }
363
364
365 static ID_BLOCK *
366 base_candidate(
367     Backend     *be,
368         Entry   *e
369 )
370 {
371         ID_BLOCK                *idl;
372
373         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
374                 e->e_dn, 0, 0);
375
376         idl = bdb2i_idl_alloc( 1 );
377         bdb2i_idl_insert( &idl, e->e_id, 1 );
378
379         return( idl );
380 }
381
382 static ID_BLOCK *
383 search_candidates(
384     Backend     *be,
385     Entry       *e,
386     Filter      *filter,
387     int         scope,
388         int             deref,
389         int             manageDSAit
390 )
391 {
392         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
393         ID_BLOCK                *candidates;
394         Filter          *f, *rf, *af, *lf;
395
396         Debug(LDAP_DEBUG_TRACE, "search_candidates: base=\"%s\" s=%d d=%d\n",
397                 e->e_ndn, scope, deref );
398
399         f = NULL;
400
401         if( !manageDSAit ) {
402                 /* match referrals */
403                 rf = (Filter *) ch_malloc( sizeof(Filter) );
404                 rf->f_next = NULL;
405                 rf->f_choice = LDAP_FILTER_OR;
406                 rf->f_or = (Filter *) ch_malloc( sizeof(Filter) );
407                 rf->f_or->f_choice = LDAP_FILTER_EQUALITY;
408                 rf->f_or->f_avtype = ch_strdup( "objectclass" );
409                 rf->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
410                 rf->f_or->f_avvalue.bv_len = sizeof("REFERRAL")-1;
411                 rf->f_or->f_next = filter;
412                 f = rf;
413         } else {
414                 rf = NULL;
415                 f = filter;
416         }
417
418         if( deref & LDAP_DEREF_SEARCHING ) {
419                 /* match aliases */
420                 af = (Filter *) ch_malloc( sizeof(Filter) );
421                 af->f_next = NULL;
422                 af->f_choice = LDAP_FILTER_OR;
423                 af->f_or = (Filter *) ch_malloc( sizeof(Filter) );
424                 af->f_or->f_choice = LDAP_FILTER_EQUALITY;
425                 af->f_or->f_avtype = ch_strdup( "objectclass" );
426                 af->f_or->f_avvalue.bv_val = ch_strdup( "ALIAS" );
427                 af->f_or->f_avvalue.bv_len = sizeof("ALIAS")-1;
428                 af->f_or->f_next = f;
429                 f = af;
430         } else {
431                 af = NULL;
432         }
433
434         if ( scope == LDAP_SCOPE_SUBTREE ) {
435                 lf = (Filter *) ch_malloc( sizeof(Filter) );
436                 lf->f_next = NULL;
437                 lf->f_choice = LDAP_FILTER_AND;
438                 lf->f_and = (Filter *) ch_malloc( sizeof(Filter) );
439
440                 lf->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
441                 lf->f_and->f_sub_type = ch_strdup( "dn" );
442                 lf->f_and->f_sub_initial = NULL;
443                 lf->f_and->f_sub_any = NULL;
444                 lf->f_and->f_sub_final = ch_strdup( e->e_ndn );
445
446                 lf->f_and->f_next = f;
447                 f = lf;
448
449         } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
450                 char buf[16];
451
452                 lf = (Filter *) ch_malloc( sizeof(Filter) );
453                 lf->f_next = NULL;
454                 lf->f_choice = LDAP_FILTER_AND;
455                 lf->f_and = (Filter *) ch_malloc( sizeof(Filter) );
456
457                 lf->f_and->f_choice = LDAP_FILTER_EQUALITY;
458                 lf->f_and->f_ava.ava_type = ch_strdup( "id2children" );
459                 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
460                 lf->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
461                 lf->f_and->f_ava.ava_value.bv_len = strlen( buf );
462
463                 lf->f_and->f_next = f;
464                 f = lf;
465
466         } else {
467                 lf = NULL;
468         }
469
470         candidates = bdb2i_filter_candidates( be, f );
471
472         /* free up filter additions we allocated above */
473         if( lf != NULL ) {
474                 lf->f_and->f_next = NULL;
475                 filter_free( lf );
476         }
477
478         if( af != NULL ) {
479                 af->f_or->f_next = NULL;
480                 filter_free( af );
481         }
482
483         if( rf != NULL ) {
484                 rf->f_or->f_next = NULL;
485                 filter_free( rf );
486         }
487
488         return( candidates );
489 }