]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/search.c
52461e5b95f044221eeed5150482abf4bdfcb976
[openldap] / servers / slapd / back-ldbm / search.c
1 /* search.c - ldbm backend search function */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
18
19 static ID_BLOCK *base_candidate(
20         Backend *be, Entry *e );
21
22 static ID_BLOCK *search_candidates(
23         Operation *op, Entry *e, Filter *filter,
24         int scope, int deref, int manageDSAit );
25
26
27 int
28 ldbm_back_search(
29     Operation   *op,
30     SlapReply   *rs )
31 {
32         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
33         int             rc, err;
34         const char *text = NULL;
35         time_t          stoptime;
36         ID_BLOCK                *candidates;
37         ID              id, cursor;
38         Entry           *e;
39         Entry   *matched = NULL;
40         struct berval   realbase = { 0, NULL };
41         int             manageDSAit = get_manageDSAit( op );
42         int             cscope = LDAP_SCOPE_DEFAULT;
43
44 #ifdef LDAP_CACHING
45         Entry           cache_base_entry; 
46 #endif /* LDAP_CACHING */
47
48         struct slap_limits_set *limit = NULL;
49         int isroot = 0;
50                 
51 #ifdef NEW_LOGGING
52         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_search: enter\n", 0, 0, 0 );
53 #else
54         Debug(LDAP_DEBUG_TRACE, "=> ldbm_back_search\n", 0, 0, 0);
55 #endif
56
57         /* grab giant lock for reading */
58         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
59
60 #ifndef LDAP_CACHING
61         if ( op->o_req_ndn.bv_len == 0 ) {
62                 /* DIT root special case */
63                 e = (Entry *) &slap_entry_root;
64
65                 /* need normalized dn below */
66                 ber_dupbv( &realbase, &e->e_nname );
67
68 #else /* LDAP_CACHING */
69         if ( op->o_caching_on || op->o_req_ndn.bv_len == 0 ) {
70                 if (op->o_req_ndn.bv_len == 0) {
71                     e = (Entry *) &slap_entry_root;
72                     /* need normalized dn below */
73                     ber_dupbv( &realbase, &e->e_nname );
74                 } else {
75                         if ((op->oq_search.rs_scope == LDAP_SCOPE_BASE) 
76                                         && (e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched )))
77                         {
78                                 candidates = base_candidate(op->o_bd, e);
79                                 cache_return_entry_r( &li->li_cache, e );
80                                 goto searchit;
81                         }
82                         cache_base_entry.e_nname = op->o_req_ndn;
83                         e = &cache_base_entry;
84                 }
85 #endif /* LDAP_CACHING */
86
87                 candidates = search_candidates( op, e, op->oq_search.rs_filter,
88                                 op->oq_search.rs_scope, op->oq_search.rs_deref,
89                                 manageDSAit || get_domainScope(op) );
90
91                 goto searchit;
92                 
93         } else if ( op->oq_search.rs_deref & LDAP_DEREF_FINDING ) {
94                 /* deref dn and get entry with reader lock */
95                 e = deref_dn_r( op->o_bd, &op->o_req_ndn, &rs->sr_err, &matched, &rs->sr_text );
96
97                 if( rs->sr_err == LDAP_NO_SUCH_OBJECT ) rs->sr_err = LDAP_REFERRAL;
98
99         } else {
100                 /* get entry with reader lock */
101                 e = dn2entry_r( op->o_bd, &op->o_req_ndn, &matched );
102                 rs->sr_err = e != NULL ? LDAP_SUCCESS : LDAP_REFERRAL;
103                 rs->sr_text = NULL;
104         }
105
106         if ( e == NULL ) {
107                 struct berval matched_dn = { 0, NULL };
108
109                 if ( matched != NULL ) {
110                         BerVarray erefs;
111                         ber_dupbv( &matched_dn, &matched->e_name );
112
113                         erefs = is_entry_referral( matched )
114                                 ? get_entry_referrals( op, matched )
115                                 : NULL;
116
117                         cache_return_entry_r( &li->li_cache, matched );
118
119                         if( erefs ) {
120                                 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
121                                         &op->o_req_dn, op->oq_search.rs_scope );
122
123                                 ber_bvarray_free( erefs );
124                         }
125
126                 } else {
127                         rs->sr_ref = referral_rewrite( default_referral,
128                                 NULL, &op->o_req_dn, op->oq_search.rs_scope );
129                 }
130
131                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
132
133                 rs->sr_matched = matched_dn.bv_val;
134                 send_ldap_result( op, rs );
135
136                 ber_bvarray_free( rs->sr_ref );
137                 ber_memfree( matched_dn.bv_val );
138 #ifdef LDAP_SYNCREPL
139                 return LDAP_REFERRAL;
140 #else
141                 return 1;
142 #endif
143         }
144
145         if (!manageDSAit && is_entry_referral( e ) ) {
146                 /* entry is a referral, don't allow add */
147                 struct berval matched_dn;
148                 BerVarray erefs;
149
150                 ber_dupbv( &matched_dn, &e->e_name );
151                 erefs = get_entry_referrals( op, e );
152                 rs->sr_ref = NULL;
153
154                 cache_return_entry_r( &li->li_cache, e );
155                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
156
157 #ifdef NEW_LOGGING
158                 LDAP_LOG( BACK_LDBM, INFO,
159                         "ldbm_search: entry (%s) is a referral.\n",
160                         e->e_dn, 0, 0 );
161 #else
162                 Debug( LDAP_DEBUG_TRACE,
163                         "ldbm_search: entry is referral\n",
164                         0, 0, 0 );
165 #endif
166
167                 if( erefs ) {
168                         rs->sr_ref = referral_rewrite( erefs, &matched_dn,
169                                 &op->o_req_dn, op->oq_search.rs_scope );
170
171                         ber_bvarray_free( erefs );
172                 }
173
174                 rs->sr_matched = matched_dn.bv_val;
175                 if( rs->sr_ref ) {
176                         rs->sr_err = LDAP_REFERRAL;
177                         send_ldap_result( op, rs );
178                         ber_bvarray_free( rs->sr_ref );
179
180                 } else {
181                         send_ldap_error( op, rs, LDAP_OTHER,
182                         "bad referral object" );
183                 }
184
185                 ber_memfree( matched_dn.bv_val );
186 #ifdef LDAP_SYNCREPL
187                 return LDAP_OTHER;
188 #else
189                 return 1;
190 #endif
191         }
192
193         if ( is_entry_alias( e ) ) {
194                 /* don't deref */
195                 op->oq_search.rs_deref = LDAP_DEREF_NEVER;
196         }
197
198         if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
199                 cscope = LDAP_SCOPE_BASE;
200                 candidates = base_candidate( op->o_bd, e );
201
202         } else {
203                 cscope = ( op->oq_search.rs_scope != LDAP_SCOPE_SUBTREE )
204                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE;
205                 candidates = search_candidates( op, e, op->oq_search.rs_filter,
206                     op->oq_search.rs_scope, op->oq_search.rs_deref, manageDSAit );
207         }
208
209         /* need normalized dn below */
210         ber_dupbv( &realbase, &e->e_nname );
211
212         cache_return_entry_r( &li->li_cache, e );
213
214 searchit:
215         if ( candidates == NULL ) {
216                 /* no candidates */
217 #ifdef NEW_LOGGING
218                 LDAP_LOG( BACK_LDBM, INFO,
219                         "ldbm_search: no candidates\n" , 0, 0, 0);
220 #else
221                 Debug( LDAP_DEBUG_TRACE, "ldbm_search: no candidates\n",
222                         0, 0, 0 );
223 #endif
224 #ifdef LDAP_CACHING
225                 if ( op->o_caching_on ) {
226                         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
227                 }
228 #endif /* LDAP_CACHING */
229
230                 rs->sr_err = LDAP_SUCCESS;
231                 send_ldap_result( op, rs );
232
233 #ifdef LDAP_CACHING
234                 if ( op->o_caching_on ) {
235                         ldap_pvt_thread_rdwr_rlock(&li->li_giant_rwlock);
236                 }
237 #endif /* LDAP_CACHING */
238
239 #ifdef LDAP_SYNCREPL
240                 rc = LDAP_OTHER;
241 #else
242                 rc = 1;
243 #endif
244                 goto done;
245         }
246
247         /* if not root, get appropriate limits */
248 #ifndef LDAP_CACHING
249         if ( be_isroot( op->o_bd, &op->o_ndn ) )
250 #else /* LDAP_CACHING */
251         if ( op->o_caching_on || be_isroot( op->o_bd, &op->o_ndn ) )
252 #endif /* LDAP_CACHING */
253         {
254                 /*
255                  * FIXME: I'd consider this dangerous if someone
256                  * uses isroot for anything but handling limits
257                  */
258                 isroot = 1;
259         } else {
260                 ( void ) get_limits( op->o_bd, &op->o_ndn, &limit );
261         }
262
263         /* if candidates exceed to-be-checked entries, abort */
264         if ( !isroot && limit->lms_s_unchecked != -1 ) {
265                 if ( ID_BLOCK_NIDS( candidates ) > (unsigned) limit->lms_s_unchecked ) {
266                         send_ldap_error( op, rs, LDAP_ADMINLIMIT_EXCEEDED,
267                                         NULL );
268                         rc = 0;
269                         goto done;
270                 }
271         }
272         
273         /* if root an no specific limit is required, allow unlimited search */
274         if ( isroot ) {
275                 if ( op->oq_search.rs_tlimit == 0 ) {
276                         op->oq_search.rs_tlimit = -1;
277                 }
278
279                 if ( op->oq_search.rs_slimit == 0 ) {
280                         op->oq_search.rs_slimit = -1;
281                 }
282
283         } else {
284                 /* if no limit is required, use soft limit */
285                 if ( op->oq_search.rs_tlimit <= 0 ) {
286                         op->oq_search.rs_tlimit = limit->lms_t_soft;
287                 
288                 /* if requested limit higher than hard limit, abort */
289                 } else if ( op->oq_search.rs_tlimit > limit->lms_t_hard ) {
290                         /* no hard limit means use soft instead */
291                         if ( limit->lms_t_hard == 0
292                                         && limit->lms_t_soft > -1
293                                         && op->oq_search.rs_tlimit > limit->lms_t_soft ) {
294                                 op->oq_search.rs_tlimit = limit->lms_t_soft;
295                         
296                         /* positive hard limit means abort */
297                         } else if ( limit->lms_t_hard > 0 ) {
298                                 send_ldap_error( op, rs,
299                                                 LDAP_ADMINLIMIT_EXCEEDED,
300                                                 NULL );
301                                 rc = 0; 
302                                 goto done;
303                         }
304
305                         /* negative hard limit means no limit */
306                 }
307
308                 /* if no limit is required, use soft limit */
309                 if ( op->oq_search.rs_slimit <= 0 ) {
310                         op->oq_search.rs_slimit = limit->lms_s_soft;
311
312                 /* if requested limit higher than hard limit, abort */
313                 } else if ( op->oq_search.rs_slimit > limit->lms_s_hard ) {
314                         /* no hard limit means use soft instead */
315                         if ( limit->lms_s_hard == 0
316                                         && limit->lms_s_soft > -1
317                                         && op->oq_search.rs_slimit > limit->lms_s_soft ) {
318                                 op->oq_search.rs_slimit = limit->lms_s_soft;
319
320                         /* positive hard limit means abort */
321                         } else if ( limit->lms_s_hard > 0 ) {
322                                 send_ldap_error( op, rs,
323                                                 LDAP_ADMINLIMIT_EXCEEDED,
324                                                 NULL );
325                                 rc = 0;
326                                 goto done;
327                         }
328
329                         /* negative hard limit means no limit */
330                 }
331         }
332
333         /* compute it anyway; root does not use it */
334         stoptime = op->o_time + op->oq_search.rs_tlimit;
335         rs->sr_attrs = op->oq_search.rs_attrs;
336
337         for ( id = idl_firstid( candidates, &cursor ); id != NOID;
338             id = idl_nextid( candidates, &cursor ) )
339         {
340                 int scopeok = 0;
341                 int result = 0;
342
343                 /* check for abandon */
344                 if ( op->o_abandon ) {
345                         rc = 0;
346                         goto done;
347                 }
348
349                 /* check time limit */
350                 if ( op->oq_search.rs_tlimit != -1 && slap_get_time() > stoptime ) {
351                         rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
352                         send_ldap_result( op, rs );
353                         rc = 0;
354                         goto done;
355                 }
356
357                 /* get the entry with reader lock */
358                 e = id2entry_r( op->o_bd, id );
359
360                 if ( e == NULL ) {
361 #ifdef NEW_LOGGING
362                         LDAP_LOG( BACK_LDBM, INFO,
363                                 "ldbm_search: candidate %ld not found.\n", id, 0, 0 );
364 #else
365                         Debug( LDAP_DEBUG_TRACE,
366                                 "ldbm_search: candidate %ld not found\n",
367                                 id, 0, 0 );
368 #endif
369
370                         goto loop_continue;
371                 }
372
373                 rs->sr_entry = e;
374
375 #ifdef LDBM_SUBENTRIES
376         if ( is_entry_subentry( e ) ) {
377                 if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
378                         if(!get_subentries_visibility( op )) {
379                                 /* only subentries are visible */
380                                 goto loop_continue;
381                         }
382                 } else if ( get_subentries( op ) &&
383                         !get_subentries_visibility( op ))
384                 {
385                         /* only subentries are visible */
386                         goto loop_continue;
387                 }
388         } else if ( get_subentries_visibility( op )) {
389                 /* only subentries are visible */
390                 goto loop_continue;
391         }
392 #endif
393
394 #ifdef LDAP_CACHING
395                 if ( !op->o_caching_on ) {
396 #endif /* LDAP_CACHING */
397
398                 if ( op->oq_search.rs_deref & LDAP_DEREF_SEARCHING && is_entry_alias( e ) ) {
399                         Entry *matched;
400                         int err;
401                         const char *text;
402                         
403                         e = deref_entry_r( op->o_bd, e, &err, &matched, &text );
404
405                         if( e == NULL ) {
406                                 e = matched;
407                                 goto loop_continue;
408                         }
409
410                         if( e->e_id == id ) {
411                                 /* circular loop */
412                                 goto loop_continue;
413                         }
414
415                         /* need to skip alias which deref into scope */
416                         if( op->oq_search.rs_scope & LDAP_SCOPE_ONELEVEL ) {
417                                 struct berval pdn;
418                                 dnParent( &e->e_nname, &pdn );
419                                 if ( ber_bvcmp( &pdn, &realbase ) ) {
420                                         goto loop_continue;
421                                 }
422
423                         } else if ( dnIsSuffix( &e->e_nname, &realbase ) ) {
424                                 /* alias is within scope */
425 #ifdef NEW_LOGGING
426                                 LDAP_LOG( BACK_LDBM, DETAIL1,
427                                         "ldbm_search: alias \"%s\" in subtree\n", e->e_dn, 0, 0 );
428 #else
429                                 Debug( LDAP_DEBUG_TRACE,
430                                         "ldbm_search: alias \"%s\" in subtree\n",
431                                         e->e_dn, 0, 0 );
432 #endif
433
434                                 goto loop_continue;
435                         }
436
437                         rs->sr_entry = e;
438
439                         scopeok = 1;
440                 }
441
442                 /*
443                  * if it's a referral, add it to the list of referrals. only do
444                  * this for non-base searches, and don't check the filter
445                  * explicitly here since it's only a candidate anyway.
446                  */
447                 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE &&
448                         is_entry_referral( e ) )
449                 {
450                         struct berval   dn;
451
452                         /* check scope */
453                         if ( !scopeok && op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
454                                 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
455                                         dnParent( &e->e_nname, &dn );
456                                         scopeok = dn_match( &dn, &realbase );
457                                 } else {
458                                         scopeok = (realbase.bv_len == 0);
459                                 }
460
461                         } else if ( !scopeok && op->oq_search.rs_scope == LDAP_SCOPE_SUBTREE ) {
462                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
463
464                         } else {
465                                 scopeok = 1;
466                         }
467
468                         if( scopeok ) {
469                                 BerVarray erefs = get_entry_referrals( op, e );
470                                 rs->sr_ref = referral_rewrite( erefs,
471                                         &e->e_name, NULL,
472                                         op->oq_search.rs_scope == LDAP_SCOPE_SUBTREE
473                                                 ? LDAP_SCOPE_SUBTREE
474                                                 : LDAP_SCOPE_BASE );
475
476                                 send_search_reference( op, rs );
477
478                                 ber_bvarray_free( rs->sr_ref );
479                                 rs->sr_ref = NULL;
480
481                         } else {
482 #ifdef NEW_LOGGING
483                                 LDAP_LOG( BACK_LDBM, DETAIL2,
484                                         "ldbm_search: candidate referral %ld scope not okay\n",
485                                         id, 0, 0 );
486 #else
487                                 Debug( LDAP_DEBUG_TRACE,
488                                         "ldbm_search: candidate referral %ld scope not okay\n",
489                                         id, 0, 0 );
490 #endif
491                         }
492
493                         goto loop_continue;
494                 }
495
496 #ifdef LDAP_CACHING
497                 }
498 #endif /* LDAP_CACHING */
499
500 #ifdef LDAP_SYNCREPL
501                 if ( !manageDSAit && is_entry_glue( e )) {
502                         goto loop_continue;
503                 }
504 #endif
505
506                 /* if it matches the filter and scope, send it */
507                 result = test_filter( op, e, op->oq_search.rs_filter );
508
509                 if ( result == LDAP_COMPARE_TRUE ) {
510                         struct berval   dn;
511
512                         /* check scope */
513                         if ( !scopeok && op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL ) {
514                                 if ( !be_issuffix( op->o_bd, &e->e_nname ) ) {
515                                         dnParent( &e->e_nname, &dn );
516                                         scopeok = dn_match( &dn, &realbase );
517                                 } else {
518                                         scopeok = (realbase.bv_len == 0);
519                                 }
520
521                         } else if ( !scopeok && op->oq_search.rs_scope == LDAP_SCOPE_SUBTREE ) {
522                                 scopeok = dnIsSuffix( &e->e_nname, &realbase );
523
524                         } else {
525                                 scopeok = 1;
526                         }
527
528                         if ( scopeok ) {
529                                 /* check size limit */
530                                 if ( --op->oq_search.rs_slimit == -1 ) {
531                                         cache_return_entry_r( &li->li_cache, e );
532                                         rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
533                                         send_ldap_result( op, rs );
534                                         rc = 0;
535                                         goto done;
536                                 }
537
538                                 if (e) {
539
540 #ifdef LDAP_CACHING
541                                         if ( op->o_caching_on ) {
542                                                 ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
543                                                 cache_return_entry_r( &li->li_cache, e );
544                                         }
545 #endif /* LDAP_CACHING */
546
547                                         result = send_search_entry( op, rs );
548
549 #ifdef LDAP_CACHING
550                                         if ( op->o_caching_on ) {
551                                                 ldap_pvt_thread_rdwr_rlock( &li->li_giant_rwlock );
552                                         }
553 #endif /* LDAP_CACHING */
554
555
556                                         switch (result) {
557                                         case 0:         /* entry sent ok */
558                                                 break;
559                                         case 1:         /* entry not sent */
560                                                 break;
561                                         case -1:        /* connection closed */
562                                                 cache_return_entry_r( &li->li_cache, e );
563                                                 rc = 0;
564                                                 goto done;
565                                         }
566                                 }
567                         } else {
568 #ifdef NEW_LOGGING
569                                 LDAP_LOG( BACK_LDBM, DETAIL2,
570                                         "ldbm_search: candidate entry %ld scope not okay\n", 
571                                         id, 0, 0 );
572 #else
573                                 Debug( LDAP_DEBUG_TRACE,
574                                         "ldbm_search: candidate entry %ld scope not okay\n",
575                                         id, 0, 0 );
576 #endif
577                         }
578
579                 } else {
580 #ifdef NEW_LOGGING
581                         LDAP_LOG( BACK_LDBM, DETAIL2,
582                                 "ldbm_search: candidate entry %ld does not match filter\n", 
583                                 id, 0, 0 );
584 #else
585                         Debug( LDAP_DEBUG_TRACE,
586                                 "ldbm_search: candidate entry %ld does not match filter\n",
587                                 id, 0, 0 );
588 #endif
589                 }
590
591 loop_continue:
592                 if( e != NULL ) {
593                         /* free reader lock */
594 #ifndef LDAP_CACHING
595                         cache_return_entry_r( &li->li_cache, e );
596 #else /* LDAP_CACHING */
597                         if ( !op->o_caching_on ) {
598                                 cache_return_entry_r( &li->li_cache, e );
599                         }
600 #endif /* LDAP_CACHING */
601                 }
602
603                 ldap_pvt_thread_yield();
604         }
605
606         rs->sr_err = rs->sr_v2ref ? LDAP_REFERRAL : LDAP_SUCCESS;
607         rs->sr_ref = rs->sr_v2ref;
608         send_ldap_result( op, rs );
609
610         rc = 0;
611
612 done:
613         ldap_pvt_thread_rdwr_runlock(&li->li_giant_rwlock);
614
615         if( candidates != NULL )
616                 idl_free( candidates );
617
618         if( rs->sr_v2ref ) ber_bvarray_free( rs->sr_v2ref );
619         if( realbase.bv_val ) free( realbase.bv_val );
620
621         return rc;
622 }
623
624 static ID_BLOCK *
625 base_candidate(
626     Backend     *be,
627         Entry   *e )
628 {
629         ID_BLOCK                *idl;
630
631 #ifdef NEW_LOGGING
632         LDAP_LOG( BACK_LDBM, ENTRY, "base_candidate: base (%s)\n", e->e_dn, 0, 0 );
633 #else
634         Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n",
635                 e->e_dn, 0, 0);
636 #endif
637
638
639         idl = idl_alloc( 1 );
640         idl_insert( &idl, e->e_id, 1 );
641
642         return( idl );
643 }
644
645 static ID_BLOCK *
646 search_candidates(
647     Operation   *op,
648     Entry       *e,
649     Filter      *filter,
650     int         scope,
651         int             deref,
652         int             manageDSAit )
653 {
654         ID_BLOCK                *candidates;
655         Filter          f, fand, rf, af, xf;
656     AttributeAssertion aa_ref, aa_alias;
657         struct berval bv_ref = { sizeof("referral")-1, "referral" };
658         struct berval bv_alias = { sizeof("alias")-1, "alias" };
659 #ifdef LDBM_SUBENTRIES
660         Filter  sf;
661         AttributeAssertion aa_subentry;
662 #endif
663
664 #ifdef NEW_LOGGING
665         LDAP_LOG( BACK_LDBM, DETAIL1,
666                    "search_candidates: base (%s) scope %d deref %d\n",
667                    e->e_ndn, scope, deref );
668 #else
669         Debug(LDAP_DEBUG_TRACE,
670                 "search_candidates: base=\"%s\" s=%d d=%d\n",
671                 e->e_ndn, scope, deref );
672 #endif
673
674
675         xf.f_or = filter;
676         xf.f_choice = LDAP_FILTER_OR;
677         xf.f_next = NULL;
678
679         if( !manageDSAit ) {
680                 /* match referrals */
681                 rf.f_choice = LDAP_FILTER_EQUALITY;
682                 rf.f_ava = &aa_ref;
683                 rf.f_av_desc = slap_schema.si_ad_objectClass;
684                 rf.f_av_value = bv_ref;
685                 rf.f_next = xf.f_or;
686                 xf.f_or = &rf;
687         }
688
689         if( deref & LDAP_DEREF_SEARCHING ) {
690                 /* match aliases */
691                 af.f_choice = LDAP_FILTER_EQUALITY;
692                 af.f_ava = &aa_alias;
693                 af.f_av_desc = slap_schema.si_ad_objectClass;
694                 af.f_av_value = bv_alias;
695                 af.f_next = xf.f_or;
696                 xf.f_or = &af;
697         }
698
699         f.f_next = NULL;
700         f.f_choice = LDAP_FILTER_AND;
701         f.f_and = &fand;
702         fand.f_choice = scope == LDAP_SCOPE_SUBTREE
703                 ? SLAPD_FILTER_DN_SUBTREE
704                 : SLAPD_FILTER_DN_ONE;
705         fand.f_dn = &e->e_nname;
706         fand.f_next = xf.f_or == filter ? filter : &xf ;
707
708 #ifdef LDBM_SUBENTRIES
709         if ( get_subentries_visibility( op )) {
710                 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
711                 sf.f_choice = LDAP_FILTER_EQUALITY;
712                 sf.f_ava = &aa_subentry;
713                 sf.f_av_desc = slap_schema.si_ad_objectClass;
714                 sf.f_av_value = bv_subentry;
715                 sf.f_next = fand.f_next;
716                 fand.f_next = &sf;
717         }
718 #endif
719
720         candidates = filter_candidates( op, &f );
721
722         return( candidates );
723 }