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