]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
When recreating a database from an ldif file created by ldbmcat,
[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_candidate(
15         Backend *be, Entry *e );
16
17 static ID_BLOCK *search_candidates(
18         Backend *be, Entry *e, Filter *filter,
19         int scope, int deref, int manageDSAit );
20
21
22 int
23 ldbm_back_search(
24     Backend     *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, "=> ldbm_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 = 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                         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                 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         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 = idl_firstid( candidates ); id != NOID;
151             id = 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 = 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                                         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, 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                                                 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                         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         idl_free( candidates );
317
318         ber_bvecfree( v2refs );
319         if( realbase ) free( realbase );
320
321         return rc;
322 }
323
324 static ID_BLOCK *
325 base_candidate(
326     Backend     *be,
327         Entry   *e
328 )
329 {
330         ID_BLOCK                *idl;
331
332         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
333                 e->e_dn, 0, 0);
334
335         idl = idl_alloc( 1 );
336         idl_insert( &idl, e->e_id, 1 );
337
338         return( idl );
339 }
340
341 static ID_BLOCK *
342 search_candidates(
343     Backend     *be,
344     Entry       *e,
345     Filter      *filter,
346     int         scope,
347         int             deref,
348         int             manageDSAit
349 )
350 {
351         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
352         ID_BLOCK                *candidates;
353         Filter          *f, *rf, *af, *lf;
354
355         Debug(LDAP_DEBUG_TRACE, "search_candidates: base=\"%s\" s=%d d=%d\n",
356                 e->e_ndn, scope, deref );
357
358         f = NULL;
359
360         if( !manageDSAit ) {
361                 /* match referrals */
362                 rf = (Filter *) ch_malloc( sizeof(Filter) );
363                 rf->f_next = NULL;
364                 rf->f_choice = LDAP_FILTER_OR;
365                 rf->f_or = (Filter *) ch_malloc( sizeof(Filter) );
366                 rf->f_or->f_choice = LDAP_FILTER_EQUALITY;
367                 rf->f_or->f_avtype = ch_strdup( "objectclass" );
368                 rf->f_or->f_avvalue.bv_val = ch_strdup( "REFERRAL" );
369                 rf->f_or->f_avvalue.bv_len = sizeof("REFERRAL")-1;
370                 rf->f_or->f_next = filter;
371                 f = rf;
372         } else {
373                 rf = NULL;
374                 f = filter;
375         }
376
377         if( deref & LDAP_DEREF_SEARCHING ) {
378                 /* match aliases */
379                 af = (Filter *) ch_malloc( sizeof(Filter) );
380                 af->f_next = NULL;
381                 af->f_choice = LDAP_FILTER_OR;
382                 af->f_or = (Filter *) ch_malloc( sizeof(Filter) );
383                 af->f_or->f_choice = LDAP_FILTER_EQUALITY;
384                 af->f_or->f_avtype = ch_strdup( "objectclass" );
385                 af->f_or->f_avvalue.bv_val = ch_strdup( "ALIAS" );
386                 af->f_or->f_avvalue.bv_len = sizeof("ALIAS")-1;
387                 af->f_or->f_next = f;
388                 f = af;
389         } else {
390                 af = NULL;
391         }
392
393         if ( scope == LDAP_SCOPE_SUBTREE ) {
394                 lf = (Filter *) ch_malloc( sizeof(Filter) );
395                 lf->f_next = NULL;
396                 lf->f_choice = LDAP_FILTER_AND;
397                 lf->f_and = (Filter *) ch_malloc( sizeof(Filter) );
398
399                 lf->f_and->f_choice = LDAP_FILTER_SUBSTRINGS;
400                 lf->f_and->f_sub_type = ch_strdup( "dn" );
401                 lf->f_and->f_sub_initial = NULL;
402                 lf->f_and->f_sub_any = NULL;
403                 lf->f_and->f_sub_final = ch_strdup( e->e_ndn );
404
405                 lf->f_and->f_next = f;
406                 f = lf;
407
408         } else if ( scope == LDAP_SCOPE_ONELEVEL ) {
409                 char buf[16];
410
411                 lf = (Filter *) ch_malloc( sizeof(Filter) );
412                 lf->f_next = NULL;
413                 lf->f_choice = LDAP_FILTER_AND;
414                 lf->f_and = (Filter *) ch_malloc( sizeof(Filter) );
415
416                 lf->f_and->f_choice = LDAP_FILTER_EQUALITY;
417                 lf->f_and->f_ava.ava_type = ch_strdup( "id2children" );
418                 sprintf( buf, "%ld", e != NULL ? e->e_id : 0 );
419                 lf->f_and->f_ava.ava_value.bv_val = ch_strdup( buf );
420                 lf->f_and->f_ava.ava_value.bv_len = strlen( buf );
421
422                 lf->f_and->f_next = f;
423                 f = lf;
424
425         } else {
426                 lf = NULL;
427         }
428
429         candidates = filter_candidates( be, f );
430
431         /* free up filter additions we allocated above */
432         if( lf != NULL ) {
433                 lf->f_and->f_next = NULL;
434                 filter_free( lf );
435         }
436
437         if( af != NULL ) {
438                 af->f_or->f_next = NULL;
439                 filter_free( af );
440         }
441
442         if( rf != NULL ) {
443                 rf->f_or->f_next = NULL;
444                 filter_free( rf );
445         }
446
447         return( candidates );
448 }