]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/search.c
BDB_INDEX code does no harm (but no good yet, not used by filters yet).
[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 "idl.h"
15 #include "external.h"
16
17 static int base_candidate(
18         BackendDB       *be,
19         Entry   *e,
20         ID              *ids );
21 static int search_candidates(
22         BackendDB *be,
23         Entry *e,
24         Filter *filter,
25         int scope,
26         int deref,
27         int manageDSAit,
28         ID      *ids );
29
30 int
31 bdb_search(
32         BackendDB       *be,
33         Connection      *conn,
34         Operation       *op,
35         const char      *base,
36         const char      *nbase,
37         int             scope,
38         int             deref,
39         int             slimit,
40         int             tlimit,
41         Filter  *filter,
42         const char      *filterstr,
43         char    **attrs,
44         int             attrsonly )
45 {
46         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
47         int              abandon;
48         int             rc;
49         const char *text = NULL;
50         time_t          stoptime;
51         ID              id, cursor;
52         ID              candidates[BDB_IDL_SIZE];
53         Entry           *e = NULL;
54         struct berval **v2refs = NULL;
55         Entry   *matched = NULL;
56         char    *realbase = NULL;
57         int             nentries = 0;
58         int             manageDSAit;
59
60         struct slap_limits_set *limit = NULL;
61         int isroot = 0;
62
63         Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
64                 0, 0, 0);
65
66         manageDSAit = get_manageDSAit( op );
67
68 #ifdef BDB_ALIASES
69         /* get entry with reader lock */
70         if ( deref & LDAP_DEREF_FINDING ) {
71                 e = deref_dn_r( be, nbase, &err, &matched, &text );
72
73         } else
74 #endif
75         {
76                 rc = bdb_dn2entry( be, NULL, nbase, &e, &matched, 0 );
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                         matched = NULL;
112                 }
113
114                 return rc;
115         }
116
117         if (!manageDSAit && is_entry_referral( e ) ) {
118                 /* entry is a referral, don't allow add */
119                 char *matched_dn = ch_strdup( e->e_dn );
120                 struct berval **refs = get_entry_referrals( be,
121                         conn, op, e );
122
123                 bdb_entry_return( be, e );
124                 e = NULL;
125
126                 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
127                         0, 0, 0 );
128
129                 send_ldap_result( conn, op, LDAP_REFERRAL,
130                         matched_dn, NULL, refs, NULL );
131
132                 ber_bvecfree( refs );
133                 free( matched_dn );
134
135                 return 1;
136         }
137
138         /* if not root, get appropriate limits */
139         if ( be_isroot( be, op->o_ndn ) ) {
140                 isroot = 1;
141         } else {
142                 ( void ) get_limits( be, op->o_ndn, &limit );
143         }
144
145         /* The time/size limits come first because they require very little
146          * effort, so there's no chance the candidates are selected and then 
147          * the request is not honored only because of time/size constraints */
148
149         /* if no time limit requested, use soft limit (unless root!) */
150         if ( tlimit <= 0 ) {
151                 if ( isroot ) {
152                         tlimit = -1;        /* allow root to set no limit */
153                 } else {
154                         tlimit = limit->lms_t_soft;
155                 }
156
157         /* if requested limit higher than hard limit, abort */
158         } else if ( tlimit > limit->lms_t_hard ) {
159                 /* no hard limit means use soft instead */
160                 if ( limit->lms_t_hard == 0 ) {
161                         tlimit = limit->lms_t_soft;
162
163                 /* positive hard limit means abort */
164                 } else if ( limit->lms_t_hard > 0 ) {
165                         send_search_result( conn, op, 
166                                         LDAP_UNWILLING_TO_PERFORM,
167                                         NULL, NULL, NULL, NULL, 0 );
168                         rc = 0;
169                         goto done;
170                 }
171                 
172                 /* negative hard limit means no limit */
173         }
174
175         /* compute it anyway; root does not use it */
176         stoptime = op->o_time + tlimit;
177         
178         /* if no size limit requested, use soft limit (unless root!) */
179         if ( slimit == 0 ) {
180                 if ( isroot ) {
181                         slimit = -1;        /* allow root to set no limit */
182                 } else {
183                         slimit = limit->lms_s_soft;
184                 }
185
186         /* if requested limit higher than hard limit, abort */
187         } else if ( slimit > limit->lms_s_hard ) {
188                 /* no hard limit means use soft instead */
189                 if ( limit->lms_s_hard == 0 ) {
190                         slimit = limit->lms_s_soft;
191
192                 /* positive hard limit means abort */
193                 } else if ( limit->lms_s_hard > 0 ) {
194                         send_search_result( conn, op, 
195                                         LDAP_UNWILLING_TO_PERFORM,
196                                         NULL, NULL, NULL, NULL, 0 );
197                         rc = 0; 
198                         goto done;
199                 }
200                 
201                 /* negative hard limit means no limit */
202         }
203
204         /* select candidates */
205         if ( scope == LDAP_SCOPE_BASE ) {
206                 rc = base_candidate( be, e, candidates );
207
208         } else {
209                 rc = search_candidates( be, e, filter,
210                         scope, deref, manageDSAit, candidates );
211         }
212
213         /* need normalized dn below */
214         realbase = ch_strdup( e->e_ndn );
215
216         /* start cursor at base entry's id */
217         cursor = e->e_id;
218
219         bdb_entry_return( be, e );
220         e = NULL;
221
222         if ( candidates[0] == 0 ) {
223                 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
224                         0, 0, 0 );
225
226                 send_search_result( conn, op,
227                         LDAP_SUCCESS,
228                         NULL, NULL, NULL, NULL, 0 );
229
230                 rc = 1;
231                 goto done;
232         }
233
234         /* if not root and candidates exceed to-be-checked entries, abort */
235         if ( !isroot && limit->lms_s_unchecked != -1 ) {
236                 if ( BDB_IDL_N(candidates) > limit->lms_s_unchecked ) {
237                         send_search_result( conn, op, 
238                                         LDAP_UNWILLING_TO_PERFORM,
239                                         NULL, NULL, NULL, NULL, 0 );
240                         rc = 1;
241                         goto done;
242                 }
243         }
244
245         for ( id = bdb_idl_first( candidates, &cursor );
246                 id != NOID;
247                 id = bdb_idl_next( candidates, &cursor ) )
248         {
249                 int             scopeok = 0;
250
251                 /* check for abandon */
252                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
253                 abandon = op->o_abandon;
254                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
255
256                 if ( abandon ) {
257                         rc = 0;
258                         goto done;
259                 }
260
261                 /* check time limit */
262                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
263                         send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
264                                 NULL, NULL, v2refs, NULL, nentries );
265                         goto done;
266                 }
267
268                 /* get the entry with reader lock */
269                 rc = bdb_id2entry( be, NULL, id, &e );
270
271                 if ( e == NULL ) {
272                         if( !BDB_IDL_IS_RANGE(candidates) ) {
273                                 /* only complain for non-range IDLs */
274                                 Debug( LDAP_DEBUG_TRACE,
275                                         "bdb_search: candidate %ld not found\n",
276                                         id, 0, 0 );
277                         }
278
279                         goto loop_continue;
280                 }
281
282 #ifdef BDB_ALIASES
283                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
284                         Entry *matched;
285                         int err;
286                         const char *text;
287                         
288                         e = deref_entry_r( be, e, &err, &matched, &text );
289
290                         if( e == NULL ) {
291                                 e = matched;
292                                 goto loop_continue;
293                         }
294
295                         if( e->e_id == id ) {
296                                 /* circular loop */
297                                 goto loop_continue;
298                         }
299
300                         /* need to skip alias which deref into scope */
301                         if( scope & LDAP_SCOPE_ONELEVEL ) {
302                                 char *pdn = dn_parent( NULL, e->e_ndn );
303                                 if ( pdn != NULL ) {
304                                         if( strcmp( pdn, realbase ) ) {
305                                                 free( pdn );
306                                                 goto loop_continue;
307                                         }
308                                         free(pdn);
309                                 }
310
311                         } else if ( dn_issuffix( e->e_ndn, realbase ) ) {
312                                 /* alias is within scope */
313                                 Debug( LDAP_DEBUG_TRACE,
314                                         "bdb_search: \"%s\" in subtree\n",
315                                         e->e_dn, 0, 0 );
316                                 goto loop_continue;
317                         }
318
319                         scopeok = 1;
320                 }
321 #endif
322
323                 /*
324                  * if it's a referral, add it to the list of referrals. only do
325                  * this for non-base searches, and don't check the filter
326                  * explicitly here since it's only a candidate anyway.
327                  */
328                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
329                         is_entry_referral( e ) )
330                 {
331                         struct berval **refs = get_entry_referrals(
332                                 be, conn, op, e );
333
334                         send_search_reference( be, conn, op,
335                                 e, refs, scope, NULL, &v2refs );
336
337                         ber_bvecfree( refs );
338
339                         goto loop_continue;
340                 }
341
342                 /* if it matches the filter and scope, send it */
343                 rc = test_filter( be, conn, op, e, filter );
344                 if ( rc == LDAP_COMPARE_TRUE ) {
345                         char    *dn;
346
347                         /* check scope */
348                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
349                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
350                                         (void) dn_normalize( dn );
351                                         scopeok = (dn == realbase)
352                                                 ? 1
353                                                 : (strcmp( dn, realbase ) ? 0 : 1 );
354                                         free( dn );
355
356                                 } else {
357                                         scopeok = (realbase == NULL || *realbase == '\0');
358                                 }
359
360                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
361                                 dn = ch_strdup( e->e_ndn );
362                                 scopeok = dn_issuffix( dn, realbase );
363                                 free( dn );
364
365                         } else {
366                                 scopeok = 1;
367                         }
368
369                         if ( scopeok ) {
370                                 /* check size limit */
371                                 if ( --slimit == -1 ) {
372                                         bdb_entry_return( be, e );
373                                         e = NULL;
374                                         send_search_result( conn, op,
375                                                 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
376                                                 v2refs, NULL, nentries );
377                                         goto done;
378                                 }
379
380                                 if (e) {
381                                         int result = send_search_entry( be, conn, op,
382                                                 e, attrs, attrsonly, NULL);
383
384                                         switch (result) {
385                                         case 0:         /* entry sent ok */
386                                                 nentries++;
387                                                 break;
388                                         case 1:         /* entry not sent */
389                                                 break;
390                                         case -1:        /* connection closed */
391                                                 bdb_entry_return( be, e );
392                                                 e = NULL;
393                                                 rc = LDAP_OTHER;
394                                                 goto done;
395                                         }
396                                 }
397                         } else {
398                                 Debug( LDAP_DEBUG_TRACE,
399                                         "bdb_search: %ld scope not okay\n",
400                                         id, 0, 0 );
401                         }
402                 } else {
403                         Debug( LDAP_DEBUG_TRACE,
404                                 "bdb_search: %ld does match filter\n",
405                                 id, 0, 0 );
406                 }
407
408 loop_continue:
409                 if( e != NULL ) {
410                         /* free reader lock */
411                         bdb_entry_return( be, e );
412                 }
413
414                 ldap_pvt_thread_yield();
415         }
416         send_search_result( conn, op,
417                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
418                 NULL, NULL, v2refs, NULL, nentries );
419
420         rc = 0;
421
422 done:
423         ber_bvecfree( v2refs );
424         if( realbase ) ch_free( realbase );
425
426         return rc;
427 }
428
429
430 static int base_candidate(
431         BackendDB       *be,
432         Entry   *e,
433         ID              *ids )
434 {
435         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
436                 e->e_dn, (long) e->e_id, 0);
437
438         ids[0] = 1;
439         ids[1] = e->e_id;
440         return 0;
441 }
442
443 static int search_candidates(
444         BackendDB *be,
445         Entry *e,
446         Filter *filter,
447         int scope,
448         int deref,
449         int manageDSAit,
450         ID      *ids )
451 {
452         int rc;
453         Filter          f, fand, rf, xf;
454         AttributeAssertion aa_ref;
455         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
456 #ifdef BDB_ALIASES
457         Filter  af;
458         AttributeAssertion aa_alias;
459 #endif
460
461         Debug(LDAP_DEBUG_TRACE,
462                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
463                 e->e_dn, (long) e->e_id, scope );
464
465         xf.f_or = filter;
466         xf.f_choice = LDAP_FILTER_OR;
467         xf.f_next = NULL;
468
469         if( !manageDSAit ) {
470                 /* match referrals */
471                 static struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
472                 rf.f_choice = LDAP_FILTER_EQUALITY;
473                 rf.f_ava = &aa_ref;
474                 rf.f_av_desc = slap_schema.si_ad_objectClass;
475                 rf.f_av_value = &bv_ref;
476                 rf.f_next = xf.f_or;
477                 xf.f_or = &rf;
478         }
479
480 #ifdef BDB_ALIASES
481         if( deref & LDAP_DEREF_SEARCHING ) {
482                 /* match aliases */
483                 static struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
484                 af.f_choice = LDAP_FILTER_EQUALITY;
485                 af.f_ava = &aa_alias;
486                 af.f_av_desc = slap_schema.si_ad_objectClass;
487                 af.f_av_value = &bv_alias;
488                 af.f_next = xf.f_or;
489                 xf.f_or = &af;
490         }
491 #endif
492
493         f.f_next = NULL;
494         f.f_choice = LDAP_FILTER_AND;
495         f.f_and = &fand;
496         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
497                 ? SLAPD_FILTER_DN_SUBTREE
498                 : SLAPD_FILTER_DN_ONE;
499         fand.f_dn = e->e_ndn;
500         fand.f_next = xf.f_or == filter ? filter : &xf ;
501
502
503 #ifdef BDB_FILTER_INDICES
504         {
505                 ID range[3];
506                 BDB_IDL_ID( bdb, range, e->e_id );
507                 rc = bdb_filter_candidates( be, range, &f, ids );
508         }
509 #else
510         BDB_IDL_ID( bdb, ids, e->e_id );
511         rc = 0;
512 #endif
513
514         Debug(LDAP_DEBUG_TRACE,
515                 "search_candidates: id=%ld first=%ld last=%ld\n",
516                 ids[0], ids[1],
517                 BDB_IDL_IS_RANGE( ids ) ? ids[2] : ids[ids[0]] );
518
519         return rc;
520 }