]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/search.c
a7b07367195079ad7ae16eb03848c32dbb2b6052
[openldap] / servers / slapd / back-bdb / search.c
1 /* search.c - search operation */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 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         Operation *op,
24         Entry *e,
25         Filter *filter,
26         int scope,
27         int deref,
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             rc;
48         const char *text = NULL;
49         time_t          stoptime;
50         ID              id, cursor;
51         ID              candidates[BDB_IDL_UM_SIZE];
52         Entry           *e = NULL;
53         BerVarray v2refs = NULL;
54         Entry   *matched = NULL;
55         struct berval   realbase = { 0, NULL };
56         int             nentries = 0;
57         int             manageDSAit;
58
59         struct slap_limits_set *limit = NULL;
60         int isroot = 0;
61
62         u_int32_t       locker;
63         DB_LOCK         lock;
64
65 #ifdef NEW_LOGGING
66         LDAP_LOG ( OPERATION, ENTRY, "bdb_back_search\n", 0, 0, 0 );
67 #else
68         Debug( LDAP_DEBUG_TRACE, "=> bdb_back_search\n",
69                 0, 0, 0);
70 #endif
71
72         manageDSAit = get_manageDSAit( op );
73
74         rc = LOCK_ID (bdb->bi_dbenv, &locker );
75         switch(rc) {
76         case 0:
77                 break;
78         default:
79                 send_ldap_result( conn, op, rc=LDAP_OTHER,
80                         NULL, "internal error", NULL, NULL );
81                 return rc;
82         }
83
84         if ( nbase->bv_len == 0 ) {
85                 /* DIT root special case */
86                 e = (Entry *) &slap_entry_root;
87                 rc = 0;
88         } else                                          
89 #ifdef BDB_ALIASES
90         /* get entry with reader lock */
91         if ( deref & LDAP_DEREF_FINDING ) {
92                 e = deref_dn_r( be, nbase-, &err, &matched, &text );
93
94         } else
95 #endif
96         {
97 dn2entry_retry:
98                 rc = bdb_dn2entry_r( be, NULL, nbase, &e, &matched, 0, locker, &lock );
99         }
100
101         switch(rc) {
102         case DB_NOTFOUND:
103         case 0:
104                 break;
105         case LDAP_BUSY:
106                 if (e != NULL) {
107                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
108                 }
109                 if (matched != NULL) {
110                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
111                 }
112                 send_ldap_result( conn, op, LDAP_BUSY,
113                         NULL, "ldap server busy", NULL, NULL );
114                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
115                 return LDAP_BUSY;
116         case DB_LOCK_DEADLOCK:
117         case DB_LOCK_NOTGRANTED:
118                 goto dn2entry_retry;
119         default:
120                 if (e != NULL) {
121                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
122                 }
123                 if (matched != NULL) {
124                         bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
125                 }
126                 send_ldap_result( conn, op, rc=LDAP_OTHER,
127                         NULL, "internal error", NULL, NULL );
128                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
129                 return rc;
130         }
131
132         if ( e == NULL ) {
133                 struct berval matched_dn = { 0, NULL };
134                 BerVarray refs = NULL;
135
136                 if ( matched != NULL ) {
137                         BerVarray erefs;
138                         ber_dupbv( &matched_dn, &matched->e_name );
139
140                         erefs = is_entry_referral( matched )
141                                 ? get_entry_referrals( be, conn, op, matched )
142                                 : NULL;
143
144                         bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, matched, &lock);
145                         matched = NULL;
146
147                         if( erefs ) {
148                                 refs = referral_rewrite( erefs, &matched_dn,
149                                         base, scope );
150                                 ber_bvarray_free( erefs );
151                         }
152
153                 } else {
154                         refs = referral_rewrite( default_referral,
155                                 NULL, base, scope );
156                 }
157
158                 send_ldap_result( conn, op,     rc=LDAP_REFERRAL ,
159                         matched_dn.bv_val, text, refs, NULL );
160
161                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
162                 if ( refs ) ber_bvarray_free( refs );
163                 if ( matched_dn.bv_val ) ber_memfree( matched_dn.bv_val );
164                 return rc;
165         }
166
167         if (!manageDSAit && e != &slap_entry_root && is_entry_referral( e ) ) {
168                 /* entry is a referral, don't allow add */
169                 struct berval matched_dn;
170                 BerVarray erefs, refs;
171                 
172                 ber_dupbv( &matched_dn, &e->e_name );
173                 erefs = get_entry_referrals( be, conn, op, e );
174                 refs = NULL;
175
176                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
177                 e = NULL;
178
179                 if( erefs ) {
180                         refs = referral_rewrite( erefs, &matched_dn,
181                                 base, scope );
182                         ber_bvarray_free( erefs );
183                 }
184
185 #ifdef NEW_LOGGING
186                 LDAP_LOG ( OPERATION, RESULTS, 
187                         "bdb_search: entry is referral\n", 0, 0, 0 );
188 #else
189                 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
190                         0, 0, 0 );
191 #endif
192
193                 send_ldap_result( conn, op, LDAP_REFERRAL,
194                         matched_dn.bv_val,
195                         refs ? NULL : "bad referral object",
196                         refs, NULL );
197
198                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
199                 ber_bvarray_free( refs );
200                 ber_memfree( matched_dn.bv_val );
201                 return 1;
202         }
203
204         /* if not root, get appropriate limits */
205         if ( be_isroot( be, &op->o_ndn ) ) {
206                 isroot = 1;
207         } else {
208                 ( void ) get_limits( be, &op->o_ndn, &limit );
209         }
210
211         /* The time/size limits come first because they require very little
212          * effort, so there's no chance the candidates are selected and then 
213          * the request is not honored only because of time/size constraints */
214
215         /* if no time limit requested, use soft limit (unless root!) */
216         if ( isroot ) {
217                 if ( tlimit == 0 ) {
218                         tlimit = -1;    /* allow root to set no limit */
219                 }
220
221                 if ( slimit == 0 ) {
222                         slimit = -1;
223                 }
224
225         } else {
226                 /* if no limit is required, use soft limit */
227                 if ( tlimit <= 0 ) {
228                         tlimit = limit->lms_t_soft;
229
230                 /* if requested limit higher than hard limit, abort */
231                 } else if ( tlimit > limit->lms_t_hard ) {
232                         /* no hard limit means use soft instead */
233                         if ( limit->lms_t_hard == 0 && tlimit > limit->lms_t_soft ) {
234                                 tlimit = limit->lms_t_soft;
235
236                         /* positive hard limit means abort */
237                         } else if ( limit->lms_t_hard > 0 ) {
238                                 send_search_result( conn, op, 
239                                                 LDAP_UNWILLING_TO_PERFORM,
240                                                 NULL, NULL, NULL, NULL, 0 );
241                                 rc = 0;
242                                 goto done;
243                         }
244                 
245                         /* negative hard limit means no limit */
246                 }
247                 
248                 /* if no limit is required, use soft limit */
249                 if ( slimit <= 0 ) {
250                         slimit = limit->lms_s_soft;
251
252                 /* if requested limit higher than hard limit, abort */
253                 } else if ( slimit > limit->lms_s_hard ) {
254                         /* no hard limit means use soft instead */
255                         if ( limit->lms_s_hard == 0 && slimit > limit->lms_s_soft ) {
256                                 slimit = limit->lms_s_soft;
257
258                         /* positive hard limit means abort */
259                         } else if ( limit->lms_s_hard > 0 ) {
260                                 send_search_result( conn, op, 
261                                                 LDAP_UNWILLING_TO_PERFORM,
262                                                 NULL, NULL, NULL, NULL, 0 );
263                                 rc = 0; 
264                                 goto done;
265                         }
266                         
267                         /* negative hard limit means no limit */
268                 }
269         }
270
271         /* compute it anyway; root does not use it */
272         stoptime = op->o_time + tlimit;
273
274         /* select candidates */
275         if ( scope == LDAP_SCOPE_BASE ) {
276                 rc = base_candidate( be, e, candidates );
277
278         } else {
279                 BDB_IDL_ALL( bdb, candidates );
280                 rc = search_candidates( be, op, e, filter,
281                         scope, deref, candidates );
282         }
283
284         /* need normalized dn below */
285         ber_dupbv( &realbase, &e->e_nname );
286
287         /* start cursor at base entry's id 
288          * FIXME: hack to make "" base work
289          * FIXME: moddn needs to assign new ID for this to work
290          */
291         cursor = e->e_id == NOID ? 1 : e->e_id;
292
293         if ( e != &slap_entry_root ) {
294                 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
295         }
296         e = NULL;
297
298         if ( candidates[0] == 0 ) {
299 #ifdef NEW_LOGGING
300         LDAP_LOG ( OPERATION, RESULTS, "bdb_search: no candidates\n", 0, 0, 0 );
301 #else
302                 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
303                         0, 0, 0 );
304 #endif
305
306                 send_search_result( conn, op,
307                         LDAP_SUCCESS,
308                         NULL, NULL, NULL, NULL, 0 );
309
310                 rc = 1;
311                 goto done;
312         }
313
314         /* if not root and candidates exceed to-be-checked entries, abort */
315         if ( !isroot && limit->lms_s_unchecked != -1 ) {
316                 if ( BDB_IDL_N(candidates) > (unsigned) limit->lms_s_unchecked ) {
317                         send_search_result( conn, op, 
318                                         LDAP_ADMINLIMIT_EXCEEDED,
319                                         NULL, NULL, NULL, NULL, 0 );
320                         rc = 1;
321                         goto done;
322                 }
323         }
324
325         for ( id = bdb_idl_first( candidates, &cursor );
326                 id != NOID;
327                 id = bdb_idl_next( candidates, &cursor ) )
328         {
329                 int             scopeok = 0;
330
331                 /* check for abandon */
332                 if ( op->o_abandon ) {
333                         rc = 0;
334                         goto done;
335                 }
336
337                 /* check time limit */
338                 if ( tlimit != -1 && slap_get_time() > stoptime ) {
339                         send_search_result( conn, op, rc = LDAP_TIMELIMIT_EXCEEDED,
340                                 NULL, NULL, v2refs, NULL, nentries );
341                         goto done;
342                 }
343
344 id2entry_retry:
345                 /* get the entry with reader lock */
346                 rc = bdb_id2entry_r( be, NULL, id, &e, locker, &lock );
347
348                 if (rc == LDAP_BUSY) {
349                         send_ldap_result( conn, op, rc=LDAP_BUSY,
350                                 NULL, "ldap server busy", NULL, NULL );
351                         goto done;
352
353                 } else if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED ) {
354                         goto id2entry_retry;    
355                 }
356
357                 if ( e == NULL ) {
358                         if( !BDB_IDL_IS_RANGE(candidates) ) {
359                                 /* only complain for non-range IDLs */
360 #ifdef NEW_LOGGING
361                                 LDAP_LOG ( OPERATION, RESULTS,
362                                         "bdb_search: candidate %ld not found\n", (long) id, 0, 0);
363 #else
364                                 Debug( LDAP_DEBUG_TRACE,
365                                         "bdb_search: candidate %ld not found\n",
366                                         (long) id, 0, 0 );
367 #endif
368                         }
369
370                         goto loop_continue;
371                 }
372
373 #ifdef BDB_SUBENTRIES
374                 if ( is_entry_subentry( e ) ) {
375                         if( scope != LDAP_SCOPE_BASE ) {
376                                 if(!get_subentries_visibility( op )) {
377                                         /* only subentries are visible */
378                                         goto loop_continue;
379                                 }
380
381                         } else if ( get_subentries( op ) &&
382                                 !get_subentries_visibility( op ))
383                         {
384                                 /* only subentries are visible */
385                                 goto loop_continue;
386                         }
387
388                 } else if ( get_subentries_visibility( op )) {
389                         /* only subentries are visible */
390                         goto loop_continue;
391                 }
392 #endif
393
394 #ifdef BDB_ALIASES
395                 if ( deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
396                         Entry *matched;
397                         int err;
398                         const char *text;
399                         
400                         e = deref_entry_r( be, e, &err, &matched, &text );
401
402                         if( e == NULL ) {
403                                 e = matched;
404                                 goto loop_continue;
405                         }
406
407                         if( e->e_id == id ) {
408                                 /* circular loop */
409                                 goto loop_continue;
410                         }
411
412                         /* need to skip alias which deref into scope */
413                         if( scope & LDAP_SCOPE_ONELEVEL ) {
414                                 struct berval   pdn;
415                                 
416                                 dnParent( &e->e_nname, &pdn ):
417                                 if ( ber_bvcmp( pdn, &realbase ) ) {
418                                         goto loop_continue;
419                                 }
420
421                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
422                                 /* alias is within scope */
423 #ifdef NEW_LOGGING
424                                 LDAP_LOG ( OPERATION, RESULTS,
425                                         "bdb_search: \"%s\" in subtree\n", e->edn, 0, 0);
426 #else
427                                 Debug( LDAP_DEBUG_TRACE,
428                                         "bdb_search: \"%s\" in subtree\n",
429                                         e->e_dn, 0, 0 );
430 #endif
431                                 goto loop_continue;
432                         }
433
434                         scopeok = 1;
435                 }
436 #endif
437
438                 /*
439                  * if it's a referral, add it to the list of referrals. only do
440                  * this for non-base searches, and don't check the filter
441                  * explicitly here since it's only a candidate anyway.
442                  */
443                 if ( !manageDSAit && scope != LDAP_SCOPE_BASE &&
444                         is_entry_referral( e ) )
445                 {
446                         struct berval   dn;
447
448                         /* check scope */
449                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
450                                 if ( !be_issuffix( be, &e->e_nname ) ) {
451                                         dnParent( &e->e_nname, &dn );
452                                         scopeok = dn_match( &dn, &realbase );
453                                 } else {
454                                         scopeok = (realbase.bv_len == 0);
455                                 }
456
457                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
458                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
459
460                         } else {
461                                 scopeok = 1;
462                         }
463
464                         if( scopeok ) {
465                                 BerVarray erefs = get_entry_referrals(
466                                         be, conn, op, e );
467                                 BerVarray refs = referral_rewrite( erefs,
468                                         &e->e_name, NULL,
469                                         scope == LDAP_SCOPE_SUBTREE
470                                                 ? LDAP_SCOPE_SUBTREE
471                                                 : LDAP_SCOPE_BASE );
472
473                                 send_search_reference( be, conn, op,
474                                         e, refs, NULL, &v2refs );
475
476                                 ber_bvarray_free( refs );
477
478                         } else {
479 #ifdef NEW_LOGGING
480                                 LDAP_LOG(OPERATION, DETAIL2, 
481                                         "bdb_search: candidate referral %ld scope not okay\n",
482                                         id, 0, 0 );
483 #else
484                                 Debug( LDAP_DEBUG_TRACE,
485                                         "bdb_search: candidate referral %ld scope not okay\n",
486                                         id, 0, 0 );
487 #endif
488                         }
489
490                         goto loop_continue;
491                 }
492
493                 /* if it matches the filter and scope, send it */
494                 rc = test_filter( be, conn, op, e, filter );
495                 if ( rc == LDAP_COMPARE_TRUE ) {
496                         struct berval   dn;
497
498                         /* check scope */
499                         if ( !scopeok && scope == LDAP_SCOPE_ONELEVEL ) {
500                                 if ( be_issuffix( be, &e->e_nname ) ) {
501                                         scopeok = (realbase.bv_len == 0);
502                                 } else {
503                                         dnParent( &e->e_nname, &dn );
504                                         scopeok = dn_match( &dn, &realbase );
505                                 }
506
507                         } else if ( !scopeok && scope == LDAP_SCOPE_SUBTREE ) {
508                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
509
510                         } else {
511                                 scopeok = 1;
512                         }
513
514                         if ( scopeok ) {
515                                 /* check size limit */
516                                 if ( --slimit == -1 ) {
517                                         bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
518                                         e = NULL;
519                                         send_search_result( conn, op,
520                                                 rc = LDAP_SIZELIMIT_EXCEEDED, NULL, NULL,
521                                                 v2refs, NULL, nentries );
522                                         goto done;
523                                 }
524
525                                 if (e) {
526                                         int result;
527                                         
528                                         if( op->o_noop ) {
529                                                 result = 0;
530                                         } else {
531                                                 result = send_search_entry( be, conn, op,
532                                                         e, attrs, attrsonly, NULL);
533                                         }
534
535                                         switch (result) {
536                                         case 0:         /* entry sent ok */
537                                                 nentries++;
538                                                 break;
539                                         case 1:         /* entry not sent */
540                                                 break;
541                                         case -1:        /* connection closed */
542                                                 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
543                                                 e = NULL;
544                                                 rc = LDAP_OTHER;
545                                                 goto done;
546                                         }
547                                 }
548                         } else {
549 #ifdef NEW_LOGGING
550                                 LDAP_LOG ( OPERATION, RESULTS,
551                                         "bdb_search: %ld scope not okay\n", (long) id, 0, 0);
552 #else
553                                 Debug( LDAP_DEBUG_TRACE,
554                                         "bdb_search: %ld scope not okay\n",
555                                         (long) id, 0, 0 );
556 #endif
557                         }
558                 } else {
559 #ifdef NEW_LOGGING
560                                 LDAP_LOG ( OPERATION, RESULTS,
561                                         "bdb_search: %ld does match filter\n", (long) id, 0, 0);
562 #else
563                         Debug( LDAP_DEBUG_TRACE,
564                                 "bdb_search: %ld does match filter\n",
565                                 (long) id, 0, 0 );
566 #endif
567                 }
568
569 loop_continue:
570                 if( e != NULL ) {
571                         /* free reader lock */
572                         bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e , &lock);
573                         e = NULL;
574                 }
575
576                 ldap_pvt_thread_yield();
577         }
578         send_search_result( conn, op,
579                 v2refs == NULL ? LDAP_SUCCESS : LDAP_REFERRAL,
580                 NULL, NULL, v2refs, NULL, nentries );
581
582         rc = 0;
583
584 done:
585         if( e != NULL ) {
586                 /* free reader lock */
587                 bdb_cache_return_entry_r ( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
588         }
589
590         LOCK_ID_FREE (bdb->bi_dbenv, locker );
591
592         if( v2refs ) ber_bvarray_free( v2refs );
593         if( realbase.bv_val ) ch_free( realbase.bv_val );
594
595         return rc;
596 }
597
598
599 static int base_candidate(
600         BackendDB       *be,
601         Entry   *e,
602         ID              *ids )
603 {
604 #ifdef NEW_LOGGING
605         LDAP_LOG ( OPERATION, ENTRY,
606                 "base_candidate: base: \"%s\" (0x%08lx)\n", e->e_dn, (long) e->e_id, 0);
607 #else
608         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
609                 e->e_dn, (long) e->e_id, 0);
610 #endif
611
612         ids[0] = 1;
613         ids[1] = e->e_id;
614         return 0;
615 }
616
617 /* Is "objectClass=xx" mentioned anywhere in this filter? Presence
618  * doesn't count, we're looking for explicit values. Also count depth
619  * of filter tree while we're at it.
620  */
621 static int oc_filter(
622         Filter *f,
623         int cur,
624         int *max
625 )
626 {
627         int rc = 0;
628
629         if( cur > *max ) *max = cur;
630
631         switch(f->f_choice) {
632         case LDAP_FILTER_EQUALITY:
633         case LDAP_FILTER_APPROX:
634                 if (f->f_av_desc == slap_schema.si_ad_objectClass)
635                         rc = 1;
636                 break;
637
638         case LDAP_FILTER_SUBSTRINGS:
639                 if (f->f_sub_desc == slap_schema.si_ad_objectClass)
640                         rc = 1;
641                 break;
642
643         case LDAP_FILTER_AND:
644         case LDAP_FILTER_OR:
645                 cur++;
646                 for (f=f->f_and; f; f=f->f_next)
647                         if ((rc = oc_filter(f, cur, max)))
648                                 break;
649                 break;
650         default:
651                 break;
652         }
653         return rc;
654 }
655
656 static int search_candidates(
657         BackendDB *be,
658         Operation *op,
659         Entry *e,
660         Filter *filter,
661         int scope,
662         int deref,
663         ID      *ids )
664 {
665         int rc, depth = 1;
666         Filter          f, scopef, rf, xf;
667         ID              *stack;
668         AttributeAssertion aa_ref;
669 #ifdef BDB_SUBENTRIES
670         Filter  sf;
671         AttributeAssertion aa_subentry;
672 #endif
673 #ifdef BDB_ALIASES
674         Filter  af;
675         AttributeAssertion aa_alias;
676 #endif
677
678         /*
679          * This routine takes as input a filter (user-filter)
680          * and rewrites it as follows:
681          *      (&(scope=DN)[(objectClass=subentry)]
682          *              (|[(objectClass=referral)(objectClass=alias)](user-filter))
683          */
684
685 #ifdef NEW_LOGGING
686         LDAP_LOG ( OPERATION, ENTRY,
687                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n", 
688                 e->e_dn, (long) e->e_id, scope);
689 #else
690         Debug(LDAP_DEBUG_TRACE,
691                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
692                 e->e_dn, (long) e->e_id, scope );
693 #endif
694
695         xf.f_or = filter;
696         xf.f_choice = LDAP_FILTER_OR;
697         xf.f_next = NULL;
698
699         /* If the user's filter doesn't mention objectClass, or if
700          * it just uses objectClass=*, these clauses are redundant.
701          */
702         if (oc_filter(filter, 1, &depth) && !get_subentries_visibility(op) ) {
703                 if( !get_manageDSAit(op) ) { /* match referrals */
704                         struct berval bv_ref = { sizeof("REFERRAL")-1, "REFERRAL" };
705                         rf.f_choice = LDAP_FILTER_EQUALITY;
706                         rf.f_ava = &aa_ref;
707                         rf.f_av_desc = slap_schema.si_ad_objectClass;
708                         rf.f_av_value = bv_ref;
709                         rf.f_next = xf.f_or;
710                         xf.f_or = &rf;
711                 }
712
713 #ifdef BDB_ALIASES
714                 if( deref & LDAP_DEREF_SEARCHING ) { /* match aliases */
715                         struct berval bv_alias = { sizeof("ALIAS")-1, "ALIAS" };
716                         af.f_choice = LDAP_FILTER_EQUALITY;
717                         af.f_ava = &aa_alias;
718                         af.f_av_desc = slap_schema.si_ad_objectClass;
719                         af.f_av_value = bv_alias;
720                         af.f_next = xf.f_or;
721                         xf.f_or = &af;
722                 }
723 #endif
724                 /* We added one of these clauses, filter depth increased */
725                 if( xf.f_or != filter ) depth++;
726         }
727
728         f.f_next = NULL;
729         f.f_choice = LDAP_FILTER_AND;
730         f.f_and = &scopef;
731         scopef.f_choice = scope == LDAP_SCOPE_SUBTREE
732                 ? SLAPD_FILTER_DN_SUBTREE
733                 : SLAPD_FILTER_DN_ONE;
734         scopef.f_dn = &e->e_nname;
735         scopef.f_next = xf.f_or == filter ? filter : &xf ;
736         /* Filter depth increased again, adding scope clause */
737         depth++;
738
739 #ifdef BDB_SUBENTRIES
740         if( get_subentries_visibility( op ) ) {
741                 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
742                 sf.f_choice = LDAP_FILTER_EQUALITY;
743                 sf.f_ava = &aa_subentry;
744                 sf.f_av_desc = slap_schema.si_ad_objectClass;
745                 sf.f_av_value = bv_subentry;
746                 sf.f_next = scopef.f_next;
747                 scopef.f_next = &sf;
748         }
749 #endif
750
751         /* Allocate IDL stack, plus 1 more for former tmp */
752         stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
753
754         rc = bdb_filter_candidates( be, &f, ids, stack, stack+BDB_IDL_UM_SIZE );
755
756         ch_free( stack );
757
758         if( rc ) {
759 #ifdef NEW_LOGGING
760                 LDAP_LOG ( OPERATION, DETAIL1,
761                         "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0  );
762 #else
763                 Debug(LDAP_DEBUG_TRACE,
764                         "bdb_search_candidates: failed (rc=%d)\n",
765                         rc, NULL, NULL );
766 #endif
767
768         } else {
769 #ifdef NEW_LOGGING
770                 LDAP_LOG ( OPERATION, DETAIL1,
771                         "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
772                         (long) ids[0], (long) BDB_IDL_FIRST(ids), 
773                         (long) BDB_IDL_LAST(ids));
774 #else
775                 Debug(LDAP_DEBUG_TRACE,
776                         "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
777                         (long) ids[0],
778                         (long) BDB_IDL_FIRST(ids),
779                         (long) BDB_IDL_LAST(ids) );
780 #endif
781         }
782
783         return rc;
784 }