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