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