]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/search.c
e78d0627600102f59f28f3c518c8eb48c70133c4
[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         struct berval   *base,
36         struct berval   *nbase,
37         int             scope,
38         int             deref,
39         int             slimit,
40         int             tlimit,
41         Filter  *filter,
42         struct berval   *filterstr,
43         AttributeName   *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_UM_SIZE];
53         Entry           *e = NULL;
54         BVarray v2refs = NULL;
55         Entry   *matched = NULL;
56         struct berval   realbase = { 0, 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         if ( nbase->bv_len == 0 ) {
69                 /* DIT root special case */
70                 e = (Entry *) &slap_entry_root;
71                 rc = 0;
72         } else                                          
73 #ifdef BDB_ALIASES
74         /* get entry with reader lock */
75         if ( deref & LDAP_DEREF_FINDING ) {
76                 e = deref_dn_r( be, nbase-, &err, &matched, &text );
77
78         } else
79 #endif
80         {
81                 rc = bdb_dn2entry( be, NULL, nbase, &e, &matched, 0 );
82         }
83
84         switch(rc) {
85         case DB_NOTFOUND:
86         case 0:
87                 break;
88         default:
89                 send_ldap_result( conn, op, rc=LDAP_OTHER,
90                         NULL, "internal error", NULL, NULL );
91                 return rc;
92         }
93
94         if ( e == NULL ) {
95                 struct berval *matched_dn = NULL;
96                 BVarray refs = NULL;
97
98                 if ( matched != NULL ) {
99                         BVarray erefs;
100                         matched_dn = ber_bvdup( &matched->e_name );
101
102                         erefs = is_entry_referral( matched )
103                                 ? get_entry_referrals( be, conn, op, matched )
104                                 : NULL;
105
106                         bdb_entry_return( be, matched );
107                         matched = NULL;
108
109                         if( erefs ) {
110                                 refs = referral_rewrite( erefs, matched_dn,
111                                         base, scope );
112                                 bvarray_free( erefs );
113                         }
114
115                 } else {
116                         refs = referral_rewrite( default_referral,
117                                 NULL, base, scope );
118                 }
119
120                 send_ldap_result( conn, op,     rc=LDAP_REFERRAL ,
121                         matched_dn ? matched_dn->bv_val : NULL, text, refs, NULL );
122
123                 if ( refs ) bvarray_free( refs );
124                 if ( matched_dn ) ber_bvfree( matched_dn );
125                 return rc;
126         }
127
128         if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
129                 /* entry is a referral, don't allow add */
130                 struct berval *matched_dn = ber_bvdup( &e->e_name );
131                 BVarray erefs = get_entry_referrals( be, conn, op, e );
132                 BVarray refs = NULL;
133
134                 bdb_entry_return( be, e );
135                 e = NULL;
136
137                 if( erefs ) {
138                         refs = referral_rewrite( erefs, matched_dn,
139                                 base, scope );
140                         bvarray_free( erefs );
141                 }
142
143                 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
144                         0, 0, 0 );
145
146                 send_ldap_result( conn, op, LDAP_REFERRAL,
147                         matched_dn->bv_val,
148                         refs ? NULL : "bad referral object",
149                         refs, NULL );
150
151                 bvarray_free( refs );
152                 ber_bvfree( matched_dn );
153                 return 1;
154         }
155
156         /* if not root, get appropriate limits */
157         if ( be_isroot( be, &op->o_ndn ) ) {
158                 isroot = 1;
159         } else {
160                 ( void ) get_limits( be, &op->o_ndn, &limit );
161         }
162
163         /* The time/size limits come first because they require very little
164          * effort, so there's no chance the candidates are selected and then 
165          * the request is not honored only because of time/size constraints */
166
167         /* if no time limit requested, use soft limit (unless root!) */
168         if ( isroot ) {
169                 if ( tlimit == 0 ) {
170                         tlimit = -1;    /* allow root to set no limit */
171                 }
172
173                 if ( slimit == 0 ) {
174                         slimit = -1;
175                 }
176
177         } else {
178                 /* if no limit is required, use soft limit */
179                 if ( tlimit <= 0 ) {
180                         tlimit = limit->lms_t_soft;
181
182                 /* if requested limit higher than hard limit, abort */
183                 } else if ( tlimit > limit->lms_t_hard ) {
184                         /* no hard limit means use soft instead */
185                         if ( limit->lms_t_hard == 0 ) {
186                                 tlimit = limit->lms_t_soft;
187
188                         /* positive hard limit means abort */
189                         } else if ( limit->lms_t_hard > 0 ) {
190                                 send_search_result( conn, op, 
191                                                 LDAP_UNWILLING_TO_PERFORM,
192                                                 NULL, NULL, NULL, NULL, 0 );
193                                 rc = 0;
194                                 goto done;
195                         }
196                 
197                         /* negative hard limit means no limit */
198                 }
199                 
200                 /* if no limit is required, use soft limit */
201                 if ( slimit <= 0 ) {
202                         slimit = limit->lms_s_soft;
203
204                 /* if requested limit higher than hard limit, abort */
205                 } else if ( slimit > limit->lms_s_hard ) {
206                         /* no hard limit means use soft instead */
207                         if ( limit->lms_s_hard == 0 ) {
208                                 slimit = limit->lms_s_soft;
209
210                         /* positive hard limit means abort */
211                         } else if ( limit->lms_s_hard > 0 ) {
212                                 send_search_result( conn, op, 
213                                                 LDAP_UNWILLING_TO_PERFORM,
214                                                 NULL, NULL, NULL, NULL, 0 );
215                                 rc = 0; 
216                                 goto done;
217                         }
218                         
219                         /* negative hard limit means no limit */
220                 }
221         }
222
223         /* compute it anyway; root does not use it */
224         stoptime = op->o_time + tlimit;
225
226         /* select candidates */
227         if ( scope == LDAP_SCOPE_BASE ) {
228                 rc = base_candidate( be, e, candidates );
229
230         } else {
231                 BDB_IDL_ALL( bdb, candidates );
232                 rc = search_candidates( be, e, filter,
233                         scope, deref, manageDSAit, candidates );
234         }
235
236         /* need normalized dn below */
237         ber_dupbv( &realbase, &e->e_nname );
238
239         /* start cursor at base entry's id 
240          * FIXME: hack to make "" base work */
241         cursor = e->e_id == NOID ? 1 : e->e_id;
242
243         if ( e != &slap_entry_root ) {
244                 bdb_entry_return( be, e );
245         }
246         e = NULL;
247
248         if ( candidates[0] == 0 ) {
249                 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
250                         0, 0, 0 );
251
252                 send_search_result( conn, op,
253                         LDAP_SUCCESS,
254                         NULL, NULL, NULL, NULL, 0 );
255
256                 rc = 1;
257                 goto done;
258         }
259
260         /* if not root and candidates exceed to-be-checked entries, abort */
261         if ( !isroot && limit->lms_s_unchecked != -1 ) {
262                 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
263                         send_search_result( conn, op, 
264                                         LDAP_UNWILLING_TO_PERFORM,
265                                         NULL, NULL, NULL, NULL, 0 );
266                         rc = 1;
267                         goto done;
268                 }
269         }
270
271         for ( id = bdb_idl_first( candidates, &cursor );
272                 id != NOID;
273                 id = bdb_idl_next( candidates, &cursor ) )
274         {
275                 int             scopeok = 0;
276
277                 /* check for abandon */
278                 ldap_pvt_thread_mutex_lock( &op->o_abandonmutex );
279                 abandon = op->o_abandon;
280                 ldap_pvt_thread_mutex_unlock( &op->o_abandonmutex );
281
282                 if ( abandon ) {
283                         rc = 0;
284                         goto done;
285                 }
286
287                 /* check time limit */
288                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
289                         send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
290                                 NULL, NULL, v2refs, NULL, nentries );
291                         goto done;
292                 }
293
294                 /* get the entry with reader lock */
295                 rc = bdb_id2entry( be, NULL, id, &e );
296
297                 if ( e == NULL ) {
298                         if( !BDB_IDL_IS_RANGE(candidates) ) {
299                                 /* only complain for non-range IDLs */
300                                 Debug( LDAP_DEBUG_TRACE,
301                                         "bdb_search: candidate %ld not found\n",
302                                         (long) id, 0, 0 );
303                         }
304
305                         goto loop_continue;
306                 }
307
308 #ifdef BDB_ALIASES
309                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
310                         Entry *matched;
311                         int err;
312                         const char *text;
313                         
314                         e = deref_entry_r( be, e, &err, &matched, &text );
315
316                         if( e == NULL ) {
317                                 e = matched;
318                                 goto loop_continue;
319                         }
320
321                         if( e->e_id == id ) {
322                                 /* circular loop */
323                                 goto loop_continue;
324                         }
325
326                         /* need to skip alias which deref into scope */
327                         if( scope & LDAP_SCOPE_ONELEVEL ) {
328                                 char *pdn = dn_parent( NULL, e->e_ndn );
329                                 if ( pdn != NULL ) {
330                                         if( strcmp( pdn, realbase.bv_val ) ) {
331                                                 goto loop_continue;
332                                         }
333                                 }
334
335                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
336                                 /* alias is within scope */
337                                 Debug( LDAP_DEBUG_TRACE,
338                                         "bdb_search: \"%s\" in subtree\n",
339                                         e->e_dn, 0, 0 );
340                                 goto loop_continue;
341                         }
342
343                         scopeok = 1;
344                 }
345 #endif
346
347                 /*
348                  * if it's a referral, add it to the list of referrals. only do
349                  * this for non-base searches, and don't check the filter
350                  * explicitly here since it's only a candidate anyway.
351                  */
352                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
353                         is_entry_referral( e ) )
354                 {
355                         BVarray erefs = get_entry_referrals(
356                                 be, conn, op, e );
357                         BVarray refs = referral_rewrite( erefs,
358                                 &e->e_name, NULL,
359                                 scope == LDAP_SCOPE_SUBTREE 
360                                         ? LDAP_SCOPE_SUBTREE
361                                         : LDAP_SCOPE_BASE );
362
363                         send_search_reference( be, conn, op,
364                                 e, refs, NULL, &v2refs );
365
366                         bvarray_free( refs );
367
368                         goto loop_continue;
369                 }
370
371                 /* if it matches the filter and scope, send it */
372                 rc = test_filter( be, conn, op, e, filter );
373                 if ( rc == LDAP_COMPARE_TRUE ) {
374                         char    *dn;
375
376                         /* check scope */
377                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
378                                 if ( (dn = dn_parent( be, e->e_ndn )) != NULL ) {
379                                         scopeok = (dn == realbase.bv_val)
380                                                 ? 1
381                                                 : (strcmp( dn, realbase.bv_val ) ? 0 : 1 );
382                                 } else {
383                                         scopeok = (realbase.bv_len == 0);
384                                 }
385
386                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
387                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
388
389                         } else {
390                                 scopeok = 1;
391                         }
392
393                         if ( scopeok ) {
394                                 /* check size limit */
395                                 if ( --slimit == -1 ) {
396                                         bdb_entry_return( be, e );
397                                         e = NULL;
398                                         send_search_result( conn, op,
399                                                 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
400                                                 v2refs, NULL, nentries );
401                                         goto done;
402                                 }
403
404                                 if (e) {
405                                         int result = send_search_entry( be, conn, op,
406                                                 e, attrs, attrsonly, NULL);
407
408                                         switch (result) {
409                                         case 0:         /* entry sent ok */
410                                                 nentries++;
411                                                 break;
412                                         case 1:         /* entry not sent */
413                                                 break;
414                                         case -1:        /* connection closed */
415                                                 bdb_entry_return( be, e );
416                                                 e = NULL;
417                                                 rc = LDAP_OTHER;
418                                                 goto done;
419                                         }
420                                 }
421                         } else {
422                                 Debug( LDAP_DEBUG_TRACE,
423                                         "bdb_search: %ld scope not okay\n",
424                                         (long) id, 0, 0 );
425                         }
426                 } else {
427                         Debug( LDAP_DEBUG_TRACE,
428                                 "bdb_search: %ld does match filter\n",
429                                 (long) id, 0, 0 );
430                 }
431
432 loop_continue:
433                 if( e != NULL ) {
434                         /* free reader lock */
435                         bdb_entry_return( be, e );
436                 }
437
438                 ldap_pvt_thread_yield();
439         }
440         send_search_result( conn, op,
441                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
442                 NULL, NULL, v2refs, NULL, nentries );
443
444         rc = 0;
445
446 done:
447         if( v2refs ) bvarray_free( v2refs );
448         if( realbase.bv_val ) ch_free( realbase.bv_val );
449
450         return rc;
451 }
452
453
454 static int base_candidate(
455         BackendDB       *be,
456         Entry   *e,
457         ID              *ids )
458 {
459         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
460                 e->e_dn, (long) e->e_id, 0);
461
462         ids[0] = 1;
463         ids[1] = e->e_id;
464         return 0;
465 }
466
467 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
468  * doesn't count, we're looking for explicit values.
469  */
470 static int oc_filter(
471         Filter *f
472 )
473 {
474         int rc = 0;
475
476         switch(f->f_choice) {
477         case LDAP_FILTER_EQUALITY:
478         case LDAP_FILTER_APPROX:
479                 if (f->f_av_desc == slap_schema.si_ad_objectClass)
480                         rc = 1;
481                 break;
482
483         case LDAP_FILTER_SUBSTRINGS:
484                 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
485                         rc = 1;
486                 break;
487
488         case LDAP_FILTER_AND:
489         case LDAP_FILTER_OR:
490                 for (f=f->f_and; f; f=f->f_next)
491                         if ((rc = oc_filter(f)))
492                                 break;
493                 break;
494         default:
495                 break;
496         }
497         return rc;
498 }
499
500 static int search_candidates(
501         BackendDB *be,
502         Entry *e,
503         Filter *filter,
504         int scope,
505         int deref,
506         int manageDSAit,
507         ID      *ids )
508 {
509         int rc;
510         Filter          f, fand, rf, xf;
511         ID              tmp[BDB_IDL_UM_SIZE];
512         AttributeAssertion aa_ref;
513         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
514 #ifdef BDB_ALIASES
515         Filter  af;
516         AttributeAssertion aa_alias;
517 #endif
518
519         Debug(LDAP_DEBUG_TRACE,
520                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
521                 e->e_dn, (long) e->e_id, scope );
522
523         xf.f_or = filter;
524         xf.f_choice = LDAP_FILTER_OR;
525         xf.f_next = NULL;
526
527         /* If the user's filter doesn't mention objectClass, or if
528          * it just uses objectClass=*, these clauses are redundant.
529          */
530         if (oc_filter(filter)) {
531                 if( !manageDSAit ) { /* match referrals */
532                         struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
533                         rf.f_choice = LDAP_FILTER_EQUALITY;
534                         rf.f_ava = &aa_ref;
535                         rf.f_av_desc = slap_schema.si_ad_objectClass;
536                         rf.f_av_value = bv_ref;
537                         rf.f_next = xf.f_or;
538                         xf.f_or = &rf;
539                 }
540
541 #ifdef BDB_ALIASES
542                 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
543                         struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
544                         af.f_choice = LDAP_FILTER_EQUALITY;
545                         af.f_ava = &aa_alias;
546                         af.f_av_desc = slap_schema.si_ad_objectClass;
547                         af.f_av_value = bv_alias;
548                         af.f_next = xf.f_or;
549                         xf.f_or = &af;
550                 }
551 #endif
552         }
553
554         f.f_next = NULL;
555         f.f_choice = LDAP_FILTER_AND;
556         f.f_and = &fand;
557         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
558                 ? SLAPD_FILTER_DN_SUBTREE
559                 : SLAPD_FILTER_DN_ONE;
560         fand.f_dn = &e->e_nname;
561         fand.f_next = xf.f_or == filter ? filter : &xf ;
562
563
564 #ifdef BDB_FILTER_INDICES
565         rc = bdb_filter_candidates( be, &f, ids, tmp );
566 #else
567         /* FIXME: Original code:
568         BDB_IDL_ID( bdb, ids, e->e_id );
569         * this is a hack to make "" base work; when bdb_filter_candidates
570         * is used this should not be needed any more */
571         BDB_IDL_ID( bdb, ids, (e->e_id == NOID ? 1 : e->e_id) );
572         rc = 0;
573 #endif
574
575         Debug(LDAP_DEBUG_TRACE,
576                 "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
577                 (long) ids[0],
578                 (long) BDB_IDL_FIRST(ids),
579                 (long) BDB_IDL_LAST(ids) );
580
581         return rc;
582 }