]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/search.c
ITS#7975 fix onelevel search
[openldap] / servers / slapd / back-mdb / search.c
1 /* search.c - search operation */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2014 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-mdb.h"
23 #include "idl.h"
24
25 static int base_candidate(
26         BackendDB       *be,
27         Entry   *e,
28         ID              *ids );
29
30 static int search_candidates(
31         Operation *op,
32         SlapReply *rs,
33         Entry *e,
34         IdScopes *isc,
35         MDB_cursor *mci,
36         ID      *ids,
37         ID *stack );
38
39 static int parse_paged_cookie( Operation *op, SlapReply *rs );
40
41 static void send_paged_response( 
42         Operation *op,
43         SlapReply *rs,
44         ID  *lastid,
45         int tentries );
46
47 /* Dereference aliases for a single alias entry. Return the final
48  * dereferenced entry on success, NULL on any failure.
49  */
50 static Entry * deref_base (
51         Operation *op,
52         SlapReply *rs,
53         Entry *e,
54         Entry **matched,
55         MDB_txn *txn,
56         ID      *tmp,
57         ID      *visited )
58 {
59         struct berval ndn;
60
61         rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
62         rs->sr_text = "maximum deref depth exceeded";
63
64         for (;;) {
65                 /* Remember the last entry we looked at, so we can
66                  * report broken links
67                  */
68                 *matched = e;
69
70                 if (MDB_IDL_N(tmp) >= op->o_bd->be_max_deref_depth) {
71                         e = NULL;
72                         break;
73                 }
74
75                 /* If this is part of a subtree or onelevel search,
76                  * have we seen this ID before? If so, quit.
77                  */
78                 if ( visited && mdb_idl_insert( visited, e->e_id ) ) {
79                         e = NULL;
80                         break;
81                 }
82
83                 /* If we've seen this ID during this deref iteration,
84                  * we've hit a loop.
85                  */
86                 if ( mdb_idl_insert( tmp, e->e_id ) ) {
87                         rs->sr_err = LDAP_ALIAS_PROBLEM;
88                         rs->sr_text = "circular alias";
89                         e = NULL;
90                         break;
91                 }
92
93                 /* If there was a problem getting the aliasedObjectName,
94                  * get_alias_dn will have set the error status.
95                  */
96                 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
97                         e = NULL;
98                         break;
99                 }
100
101                 rs->sr_err = mdb_dn2entry( op, txn, NULL, &ndn, &e, NULL, 0 );
102                 if (rs->sr_err) {
103                         rs->sr_err = LDAP_ALIAS_PROBLEM;
104                         rs->sr_text = "aliasedObject not found";
105                         break;
106                 }
107
108                 /* Free the previous entry, continue to work with the
109                  * one we just retrieved.
110                  */
111                 mdb_entry_return( op, *matched );
112
113                 /* We found a regular entry. Return this to the caller.
114                  */
115                 if (!is_entry_alias(e)) {
116                         rs->sr_err = LDAP_SUCCESS;
117                         rs->sr_text = NULL;
118                         break;
119                 }
120         }
121         return e;
122 }
123
124 /* Look for and dereference all aliases within the search scope.
125  * Requires "stack" to be able to hold 6 levels of DB_SIZE IDLs.
126  * Of course we're hardcoded to require a minimum of 8 UM_SIZE
127  * IDLs so this is never a problem.
128  */
129 static int search_aliases(
130         Operation *op,
131         SlapReply *rs,
132         ID e_id,
133         IdScopes *isc,
134         MDB_cursor *mci,
135         ID *stack )
136 {
137         ID *aliases, *curscop, *visited, *newsubs, *oldsubs, *tmp;
138         ID cursora, ida, cursoro, ido;
139         Entry *matched, *a;
140         struct berval bv_alias = BER_BVC( "alias" );
141         AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
142         Filter  af;
143         int first = 1;
144
145         aliases = stack;        /* IDL of all aliases in the database */
146         curscop = aliases + MDB_IDL_DB_SIZE;    /* Aliases in the current scope */
147         visited = curscop + MDB_IDL_DB_SIZE;    /* IDs we've seen in this search */
148         newsubs = visited + MDB_IDL_DB_SIZE;    /* New subtrees we've added */
149         oldsubs = newsubs + MDB_IDL_DB_SIZE;    /* Subtrees added previously */
150         tmp = oldsubs + MDB_IDL_DB_SIZE;        /* Scratch space for deref_base() */
151
152         af.f_choice = LDAP_FILTER_EQUALITY;
153         af.f_ava = &aa_alias;
154         af.f_av_desc = slap_schema.si_ad_objectClass;
155         af.f_av_value = bv_alias;
156         af.f_next = NULL;
157
158         /* Find all aliases in database */
159         MDB_IDL_ZERO( aliases );
160         rs->sr_err = mdb_filter_candidates( op, isc->mt, &af, aliases,
161                 curscop, visited );
162         if (rs->sr_err != LDAP_SUCCESS || MDB_IDL_IS_ZERO( aliases )) {
163                 return rs->sr_err;
164         }
165         oldsubs[0] = 1;
166         oldsubs[1] = e_id;
167
168         MDB_IDL_ZERO( visited );
169         MDB_IDL_ZERO( newsubs );
170
171         cursoro = 0;
172         ido = mdb_idl_first( oldsubs, &cursoro );
173
174         for (;;) {
175                 /* Set curscop to only the aliases in the current scope. Start with
176                  * all the aliases, then get the intersection with the scope.
177                  */
178                 rs->sr_err = mdb_idscope( op, isc->mt, e_id, aliases, curscop );
179
180                 /* Dereference all of the aliases in the current scope. */
181                 cursora = 0;
182                 for (ida = mdb_idl_first(curscop, &cursora); ida != NOID;
183                         ida = mdb_idl_next(curscop, &cursora))
184                 {
185                         rs->sr_err = mdb_id2entry(op, mci, ida, &a);
186                         if (rs->sr_err != LDAP_SUCCESS) {
187                                 continue;
188                         }
189
190                         /* This should only happen if the curscop IDL has maxed out and
191                          * turned into a range that spans IDs indiscriminately
192                          */
193                         if (!is_entry_alias(a)) {
194                                 mdb_entry_return(op, a);
195                                 continue;
196                         }
197
198                         /* Actually dereference the alias */
199                         MDB_IDL_ZERO(tmp);
200                         a = deref_base( op, rs, a, &matched, isc->mt,
201                                 tmp, visited );
202                         if (a) {
203                                 /* If the target was not already in our current scopes,
204                                  * make note of it in the newsubs list.
205                                  */
206                                 ID2 mid;
207                                 mid.mid = a->e_id;
208                                 mid.mval.mv_data = NULL;
209                                 if (op->ors_scope == LDAP_SCOPE_SUBTREE) {
210                                         isc->id = a->e_id;
211                                         /* if ID is a child of any of our current scopes,
212                                          * ignore it, it's already included.
213                                          */
214                                         if (mdb_idscopechk(op, isc))
215                                                 goto skip;
216                                 }
217                                 if (mdb_id2l_insert(isc->scopes, &mid) == 0) {
218                                         mdb_idl_insert(newsubs, a->e_id);
219                                 }
220 skip:                   mdb_entry_return( op, a );
221
222                         } else if (matched) {
223                                 /* Alias could not be dereferenced, or it deref'd to
224                                  * an ID we've already seen. Ignore it.
225                                  */
226                                 mdb_entry_return( op, matched );
227                                 rs->sr_text = NULL;
228                                 rs->sr_err = 0;
229                         }
230                 }
231                 /* If this is a OneLevel search, we're done; oldsubs only had one
232                  * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
233                  */
234                 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
235 nextido:
236                 ido = mdb_idl_next( oldsubs, &cursoro );
237                 
238                 /* If we're done processing the old scopes, did we add any new
239                  * scopes in this iteration? If so, go back and do those now.
240                  */
241                 if (ido == NOID) {
242                         if (MDB_IDL_IS_ZERO(newsubs)) break;
243                         MDB_IDL_CPY(oldsubs, newsubs);
244                         MDB_IDL_ZERO(newsubs);
245                         cursoro = 0;
246                         ido = mdb_idl_first( oldsubs, &cursoro );
247                 }
248
249                 /* Find the entry corresponding to the next scope. If it can't
250                  * be found, ignore it and move on. This should never happen;
251                  * we should never see the ID of an entry that doesn't exist.
252                  */
253                 {
254                         MDB_val edata;
255                         rs->sr_err = mdb_id2edata(op, mci, ido, &edata);
256                         if ( rs->sr_err != MDB_SUCCESS ) {
257                                 goto nextido;
258                         }
259                         e_id = ido;
260                 }
261         }
262         return rs->sr_err;
263 }
264
265 /* Get the next ID from the DB. Used if the candidate list is
266  * a range and simple iteration hits missing entryIDs
267  */
268 static int
269 mdb_get_nextid(MDB_cursor *mci, ID *cursor)
270 {
271         MDB_val key;
272         ID id;
273         int rc;
274
275         id = *cursor + 1;
276         key.mv_data = &id;
277         key.mv_size = sizeof(ID);
278         rc = mdb_cursor_get( mci, &key, NULL, MDB_SET_RANGE );
279         if ( rc )
280                 return rc;
281         memcpy( cursor, key.mv_data, sizeof(ID));
282         return 0;
283 }
284
285 static void scope_chunk_free( void *key, void *data )
286 {
287         ID2 *p1, *p2;
288         for (p1 = data; p1; p1 = p2) {
289                 p2 = p1[0].mval.mv_data;
290                 ber_memfree_x(p1, NULL);
291         }
292 }
293
294 static ID2 *scope_chunk_get( Operation *op )
295 {
296         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
297         ID2 *ret = NULL;
298
299         ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
300                         (void *)&ret, NULL );
301         if ( !ret ) {
302                 ret = ch_malloc( MDB_IDL_UM_SIZE * sizeof( ID2 ));
303         } else {
304                 void *r2 = ret[0].mval.mv_data;
305                 ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
306                         r2, scope_chunk_free, NULL, NULL );
307         }
308         return ret;
309 }
310
311 static void scope_chunk_ret( Operation *op, ID2 *scopes )
312 {
313         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
314         void *ret = NULL;
315
316         ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)scope_chunk_get,
317                         &ret, NULL );
318         scopes[0].mval.mv_data = ret;
319         ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)scope_chunk_get,
320                         (void *)scopes, scope_chunk_free, NULL, NULL );
321 }
322
323 static void *search_stack( Operation *op );
324
325 typedef struct ww_ctx {
326         MDB_txn *txn;
327         MDB_cursor *mcd;        /* if set, save cursor context */
328         ID key;
329         MDB_val data;
330         int flag;
331 } ww_ctx;
332
333 /* ITS#7904 if we get blocked while writing results to client,
334  * release the current reader txn and reacquire it after we
335  * unblock.
336  * Slight problem - if we're doing a scope-based walk (mdb_dn2id_walk)
337  * to return results, we need to remember the state of the mcd cursor.
338  * If the node that cursor was pointing to gets deleted while we're
339  * blocked, we may be unable to restore the cursor position. In that
340  * case return an LDAP_BUSY error - let the client know this search
341  * couldn't succeed, but might succeed on a retry.
342  */
343 static void
344 mdb_writewait( Operation *op, slap_callback *sc )
345 {
346         ww_ctx *ww = sc->sc_private;
347         if ( !ww->flag ) {
348                 if ( ww->mcd ) {
349                         MDB_val key, data;
350                         mdb_cursor_get( ww->mcd, &key, &data, MDB_GET_CURRENT );
351                         memcpy( &ww->key, key.mv_data, sizeof(ID) );
352                         ww->data.mv_size = data.mv_size;
353                         ww->data.mv_data = op->o_tmpalloc( data.mv_size, op->o_tmpmemctx );
354                         memcpy(ww->data.mv_data, data.mv_data, data.mv_size);
355                 }
356                 mdb_txn_reset( ww->txn );
357                 ww->flag = 1;
358         }
359 }
360
361 static int
362 mdb_waitfixup( Operation *op, ww_ctx *ww, MDB_cursor *mci, MDB_cursor *mcd )
363 {
364         int rc = 0;
365         ww->flag = 0;
366         mdb_txn_renew( ww->txn );
367         mdb_cursor_renew( ww->txn, mci );
368         mdb_cursor_renew( ww->txn, mcd );
369         if ( ww->mcd ) {
370                 MDB_val key, data;
371                 key.mv_size = sizeof(ID);
372                 key.mv_data = &ww->key;
373                 data = ww->data;
374                 rc = mdb_cursor_get( mcd, &key, &data, MDB_GET_BOTH );
375                 if ( rc == MDB_NOTFOUND ) {
376                         data = ww->data;
377                         rc = mdb_cursor_get( mcd, &key, &data, MDB_GET_BOTH_RANGE );
378                         /* the loop will skip this node using NEXT_DUP but we want it
379                          * sent, so go back one space first
380                          */
381                         if ( rc == MDB_SUCCESS )
382                                 mdb_cursor_get( mcd, &key, &data, MDB_PREV_DUP );
383                         else
384                                 rc = LDAP_BUSY;
385                 } else if ( rc ) {
386                         rc = LDAP_OTHER;
387                 }
388                 op->o_tmpfree( ww->data.mv_data, op->o_tmpmemctx );
389                 ww->data.mv_data = NULL;
390         }
391         return rc;
392 }
393
394 int
395 mdb_search( Operation *op, SlapReply *rs )
396 {
397         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
398         ID              id, cursor, nsubs, ncand, cscope;
399         ID              lastid = NOID;
400         ID              candidates[MDB_IDL_UM_SIZE];
401         ID              iscopes[MDB_IDL_DB_SIZE];
402         ID2             *scopes;
403         void    *stack;
404         Entry           *e = NULL, *base = NULL;
405         Entry           *matched = NULL;
406         AttributeName   *attrs;
407         slap_mask_t     mask;
408         time_t          stoptime;
409         int             manageDSAit;
410         int             tentries = 0;
411         IdScopes        isc;
412         MDB_cursor      *mci, *mcd;
413         ww_ctx wwctx;
414         slap_callback cb = { 0 };
415
416         mdb_op_info     opinfo = {{{0}}}, *moi = &opinfo;
417         MDB_txn                 *ltid = NULL;
418
419         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(mdb_search) "\n", 0, 0, 0);
420         attrs = op->oq_search.rs_attrs;
421
422         manageDSAit = get_manageDSAit( op );
423
424         rs->sr_err = mdb_opinfo_get( op, mdb, 1, &moi );
425         switch(rs->sr_err) {
426         case 0:
427                 break;
428         default:
429                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
430                 return rs->sr_err;
431         }
432
433         ltid = moi->moi_txn;
434
435         rs->sr_err = mdb_cursor_open( ltid, mdb->mi_id2entry, &mci );
436         if ( rs->sr_err ) {
437                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
438                 return rs->sr_err;
439         }
440
441         rs->sr_err = mdb_cursor_open( ltid, mdb->mi_dn2id, &mcd );
442         if ( rs->sr_err ) {
443                 mdb_cursor_close( mci );
444                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
445                 return rs->sr_err;
446         }
447
448         scopes = scope_chunk_get( op );
449         stack = search_stack( op );
450         isc.mt = ltid;
451         isc.mc = mcd;
452         isc.scopes = scopes;
453         isc.oscope = op->ors_scope;
454         isc.sctmp = stack;
455
456         if ( op->ors_deref & LDAP_DEREF_FINDING ) {
457                 MDB_IDL_ZERO(candidates);
458         }
459 dn2entry_retry:
460         /* get entry with reader lock */
461         rs->sr_err = mdb_dn2entry( op, ltid, mcd, &op->o_req_ndn, &e, &nsubs, 1 );
462
463         switch(rs->sr_err) {
464         case MDB_NOTFOUND:
465                 matched = e;
466                 e = NULL;
467                 break;
468         case 0:
469                 break;
470         case LDAP_BUSY:
471                 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
472                 goto done;
473         default:
474                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
475                 goto done;
476         }
477
478         if ( op->ors_deref & LDAP_DEREF_FINDING ) {
479                 if ( matched && is_entry_alias( matched )) {
480                         struct berval stub;
481
482                         stub.bv_val = op->o_req_ndn.bv_val;
483                         stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
484                         e = deref_base( op, rs, matched, &matched, ltid,
485                                 candidates, NULL );
486                         if ( e ) {
487                                 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
488                                         op->o_tmpmemctx );
489                                 mdb_entry_return(op, e);
490                                 matched = NULL;
491                                 goto dn2entry_retry;
492                         }
493                 } else if ( e && is_entry_alias( e )) {
494                         e = deref_base( op, rs, e, &matched, ltid,
495                                 candidates, NULL );
496                 }
497         }
498
499         if ( e == NULL ) {
500                 struct berval matched_dn = BER_BVNULL;
501
502                 if ( matched != NULL ) {
503                         BerVarray erefs = NULL;
504
505                         /* return referral only if "disclose"
506                          * is granted on the object */
507                         if ( ! access_allowed( op, matched,
508                                                 slap_schema.si_ad_entry,
509                                                 NULL, ACL_DISCLOSE, NULL ) )
510                         {
511                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
512
513                         } else {
514                                 ber_dupbv( &matched_dn, &matched->e_name );
515
516                                 erefs = is_entry_referral( matched )
517                                         ? get_entry_referrals( op, matched )
518                                         : NULL;
519                                 if ( rs->sr_err == MDB_NOTFOUND )
520                                         rs->sr_err = LDAP_REFERRAL;
521                                 rs->sr_matched = matched_dn.bv_val;
522                         }
523
524                         mdb_entry_return(op, matched);
525                         matched = NULL;
526
527                         if ( erefs ) {
528                                 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
529                                         &op->o_req_dn, op->oq_search.rs_scope );
530                                 ber_bvarray_free( erefs );
531                         }
532
533                 } else {
534                         rs->sr_ref = referral_rewrite( default_referral,
535                                 NULL, &op->o_req_dn, op->oq_search.rs_scope );
536                         rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
537                 }
538
539                 send_ldap_result( op, rs );
540
541                 if ( rs->sr_ref ) {
542                         ber_bvarray_free( rs->sr_ref );
543                         rs->sr_ref = NULL;
544                 }
545                 if ( !BER_BVISNULL( &matched_dn ) ) {
546                         ber_memfree( matched_dn.bv_val );
547                         rs->sr_matched = NULL;
548                 }
549                 goto done;
550         }
551
552         /* NOTE: __NEW__ "search" access is required
553          * on searchBase object */
554         if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
555                                 NULL, ACL_SEARCH, NULL, &mask ) )
556         {
557                 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
558                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
559                 } else {
560                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
561                 }
562
563                 mdb_entry_return( op,e);
564                 send_ldap_result( op, rs );
565                 goto done;
566         }
567
568         if ( !manageDSAit && is_entry_referral( e ) ) {
569                 /* entry is a referral */
570                 struct berval matched_dn = BER_BVNULL;
571                 BerVarray erefs = NULL;
572                 
573                 ber_dupbv( &matched_dn, &e->e_name );
574                 erefs = get_entry_referrals( op, e );
575
576                 rs->sr_err = LDAP_REFERRAL;
577
578                 mdb_entry_return( op, e );
579                 e = NULL;
580
581                 if ( erefs ) {
582                         rs->sr_ref = referral_rewrite( erefs, &matched_dn,
583                                 &op->o_req_dn, op->oq_search.rs_scope );
584                         ber_bvarray_free( erefs );
585
586                         if ( !rs->sr_ref ) {
587                                 rs->sr_text = "bad_referral object";
588                         }
589                 }
590
591                 Debug( LDAP_DEBUG_TRACE,
592                         LDAP_XSTRING(mdb_search) ": entry is referral\n",
593                         0, 0, 0 );
594
595                 rs->sr_matched = matched_dn.bv_val;
596                 send_ldap_result( op, rs );
597
598                 ber_bvarray_free( rs->sr_ref );
599                 rs->sr_ref = NULL;
600                 ber_memfree( matched_dn.bv_val );
601                 rs->sr_matched = NULL;
602                 goto done;
603         }
604
605         if ( get_assert( op ) &&
606                 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
607         {
608                 rs->sr_err = LDAP_ASSERTION_FAILED;
609                 mdb_entry_return( op,e);
610                 send_ldap_result( op, rs );
611                 goto done;
612         }
613
614         /* compute it anyway; root does not use it */
615         stoptime = op->o_time + op->ors_tlimit;
616
617         base = e;
618
619         e = NULL;
620
621         /* select candidates */
622         if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
623                 rs->sr_err = base_candidate( op->o_bd, base, candidates );
624                 scopes[0].mid = 0;
625                 ncand = 1;
626         } else {
627                 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
628                         size_t nkids;
629                         MDB_val key, data;
630                         key.mv_data = &base->e_id;
631                         key.mv_size = sizeof( ID );
632                         mdb_cursor_get( mcd, &key, &data, MDB_SET );
633                         mdb_cursor_count( mcd, &nkids );
634                         nsubs = nkids - 1;
635                 } else if ( !base->e_id ) {
636                         /* we don't maintain nsubs for entryID 0.
637                          * just grab entry count from id2entry stat
638                          */
639                         MDB_stat ms;
640                         mdb_stat( ltid, mdb->mi_id2entry, &ms );
641                         nsubs = ms.ms_entries;
642                 }
643                 MDB_IDL_ZERO( candidates );
644                 scopes[0].mid = 1;
645                 scopes[1].mid = base->e_id;
646                 scopes[1].mval.mv_data = NULL;
647                 rs->sr_err = search_candidates( op, rs, base,
648                         &isc, mci, candidates, stack );
649                 ncand = MDB_IDL_N( candidates );
650                 if ( !base->e_id || ncand == NOID ) {
651                         /* grab entry count from id2entry stat
652                          */
653                         MDB_stat ms;
654                         mdb_stat( ltid, mdb->mi_id2entry, &ms );
655                         if ( !base->e_id )
656                                 nsubs = ms.ms_entries;
657                         if ( ncand == NOID )
658                                 ncand = ms.ms_entries;
659                 }
660         }
661
662         /* start cursor at beginning of candidates.
663          */
664         cursor = 0;
665
666         if ( candidates[0] == 0 ) {
667                 Debug( LDAP_DEBUG_TRACE,
668                         LDAP_XSTRING(mdb_search) ": no candidates\n",
669                         0, 0, 0 );
670
671                 goto nochange;
672         }
673
674         /* if not root and candidates exceed to-be-checked entries, abort */
675         if ( op->ors_limit      /* isroot == FALSE */ &&
676                 op->ors_limit->lms_s_unchecked != -1 &&
677                 ncand > (unsigned) op->ors_limit->lms_s_unchecked )
678         {
679                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
680                 send_ldap_result( op, rs );
681                 rs->sr_err = LDAP_SUCCESS;
682                 goto done;
683         }
684
685         if ( op->ors_limit == NULL      /* isroot == TRUE */ ||
686                 !op->ors_limit->lms_s_pr_hide )
687         {
688                 tentries = ncand;
689         }
690
691         wwctx.flag = 0;
692         /* If we're running in our own read txn */
693         if (  moi == &opinfo ) {
694                 cb.sc_writewait = mdb_writewait;
695                 cb.sc_private = &wwctx;
696                 wwctx.txn = ltid;
697                 wwctx.mcd = NULL;
698                 cb.sc_next = op->o_callback;
699                 op->o_callback = &cb;
700         }
701
702         if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
703                 PagedResultsState *ps = op->o_pagedresults_state;
704                 /* deferred cookie parsing */
705                 rs->sr_err = parse_paged_cookie( op, rs );
706                 if ( rs->sr_err != LDAP_SUCCESS ) {
707                         send_ldap_result( op, rs );
708                         goto done;
709                 }
710
711                 cursor = (ID) ps->ps_cookie;
712                 if ( cursor && ps->ps_size == 0 ) {
713                         rs->sr_err = LDAP_SUCCESS;
714                         rs->sr_text = "search abandoned by pagedResult size=0";
715                         send_ldap_result( op, rs );
716                         goto done;
717                 }
718                 id = mdb_idl_first( candidates, &cursor );
719                 if ( id == NOID ) {
720                         Debug( LDAP_DEBUG_TRACE, 
721                                 LDAP_XSTRING(mdb_search)
722                                 ": no paged results candidates\n",
723                                 0, 0, 0 );
724                         send_paged_response( op, rs, &lastid, 0 );
725
726                         rs->sr_err = LDAP_OTHER;
727                         goto done;
728                 }
729                 if ( id == (ID)ps->ps_cookie )
730                         id = mdb_idl_next( candidates, &cursor );
731                 nsubs = ncand;  /* always bypass scope'd search */
732                 goto loop_begin;
733         }
734         if ( nsubs < ncand ) {
735                 int rc;
736                 /* Do scope-based search */
737
738                 /* if any alias scopes were set, save them */
739                 if (scopes[0].mid > 1) {
740                         cursor = 1;
741                         for (cscope = 1; cscope <= scopes[0].mid; cscope++) {
742                                 /* Ignore the original base */
743                                 if (scopes[cscope].mid == base->e_id)
744                                         continue;
745                                 iscopes[cursor++] = scopes[cscope].mid;
746                         }
747                         iscopes[0] = scopes[0].mid - 1;
748                 } else {
749                         iscopes[0] = 0;
750                 }
751
752                 wwctx.mcd = mcd;
753                 isc.id = base->e_id;
754                 isc.numrdns = 0;
755                 rc = mdb_dn2id_walk( op, &isc );
756                 if ( rc )
757                         id = NOID;
758                 else
759                         id = isc.id;
760                 cscope = 0;
761         } else {
762                 id = mdb_idl_first( candidates, &cursor );
763         }
764
765         while (id != NOID)
766         {
767                 int scopeok;
768                 MDB_val edata;
769
770 loop_begin:
771
772                 /* check for abandon */
773                 if ( op->o_abandon ) {
774                         rs->sr_err = SLAPD_ABANDON;
775                         send_ldap_result( op, rs );
776                         goto done;
777                 }
778
779                 /* mostly needed by internal searches,
780                  * e.g. related to syncrepl, for whom
781                  * abandon does not get set... */
782                 if ( slapd_shutdown ) {
783                         rs->sr_err = LDAP_UNAVAILABLE;
784                         send_ldap_disconnect( op, rs );
785                         goto done;
786                 }
787
788                 /* check time limit */
789                 if ( op->ors_tlimit != SLAP_NO_LIMIT
790                                 && slap_get_time() > stoptime )
791                 {
792                         rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
793                         rs->sr_ref = rs->sr_v2ref;
794                         send_ldap_result( op, rs );
795                         rs->sr_err = LDAP_SUCCESS;
796                         goto done;
797                 }
798
799
800                 if ( nsubs < ncand ) {
801                         unsigned i;
802                         /* Is this entry in the candidate list? */
803                         scopeok = 0;
804                         if (MDB_IDL_IS_RANGE( candidates )) {
805                                 if ( id >= MDB_IDL_RANGE_FIRST( candidates ) &&
806                                         id <= MDB_IDL_RANGE_LAST( candidates ))
807                                         scopeok = 1;
808                         } else {
809                                 i = mdb_idl_search( candidates, id );
810                                 if ( candidates[i] == id )
811                                         scopeok = 1;
812                         }
813                         if ( scopeok )
814                                 goto scopeok;
815                         goto loop_continue;
816                 }
817
818                 /* Does this candidate actually satisfy the search scope?
819                  */
820                 scopeok = 0;
821                 isc.numrdns = 0;
822                 switch( op->ors_scope ) {
823                 case LDAP_SCOPE_BASE:
824                         /* This is always true, yes? */
825                         if ( id == base->e_id ) scopeok = 1;
826                         break;
827
828 #ifdef LDAP_SCOPE_CHILDREN
829                 case LDAP_SCOPE_CHILDREN:
830                         if ( id == base->e_id ) break;
831                         /* Fall-thru */
832 #endif
833                 case LDAP_SCOPE_SUBTREE:
834                         if ( id == base->e_id ) {
835                                 scopeok = 1;
836                                 break;
837                         }
838                         /* Fall-thru */
839                 case LDAP_SCOPE_ONELEVEL:
840                         if ( id == base->e_id ) break;
841                         isc.id = id;
842                         isc.nscope = 0;
843                         rs->sr_err = mdb_idscopes( op, &isc );
844                         if ( rs->sr_err == MDB_SUCCESS ) {
845                                 if ( isc.nscope )
846                                         scopeok = 1;
847                         } else {
848                                 if ( rs->sr_err == MDB_NOTFOUND )
849                                         goto notfound;
850                         }
851                         break;
852                 }
853
854                 /* Not in scope, ignore it */
855                 if ( !scopeok )
856                 {
857                         Debug( LDAP_DEBUG_TRACE,
858                                 LDAP_XSTRING(mdb_search)
859                                 ": %ld scope not okay\n",
860                                 (long) id, 0, 0 );
861                         goto loop_continue;
862                 }
863
864 scopeok:
865                 if ( id == base->e_id ) {
866                         e = base;
867                 } else {
868
869                         /* get the entry */
870                         rs->sr_err = mdb_id2edata( op, mci, id, &edata );
871                         if ( rs->sr_err == MDB_NOTFOUND ) {
872 notfound:
873                                 if( nsubs < ncand )
874                                         goto loop_continue;
875
876                                 if( !MDB_IDL_IS_RANGE(candidates) ) {
877                                         /* only complain for non-range IDLs */
878                                         Debug( LDAP_DEBUG_TRACE,
879                                                 LDAP_XSTRING(mdb_search)
880                                                 ": candidate %ld not found\n",
881                                                 (long) id, 0, 0 );
882                                 } else {
883                                         /* get the next ID from the DB */
884                                         rs->sr_err = mdb_get_nextid( mci, &cursor );
885                                         if ( rs->sr_err == MDB_NOTFOUND ) {
886                                                 break;
887                                         }
888                                         if ( rs->sr_err ) {
889                                                 rs->sr_err = LDAP_OTHER;
890                                                 rs->sr_text = "internal error in get_nextid";
891                                                 send_ldap_result( op, rs );
892                                                 goto done;
893                                         }
894                                         cursor--;
895                                 }
896
897                                 goto loop_continue;
898                         } else if ( rs->sr_err ) {
899                                 rs->sr_err = LDAP_OTHER;
900                                 rs->sr_text = "internal error in mdb_id2edata";
901                                 send_ldap_result( op, rs );
902                                 goto done;
903                         }
904
905                         rs->sr_err = mdb_entry_decode( op, ltid, &edata, &e );
906                         if ( rs->sr_err ) {
907                                 rs->sr_err = LDAP_OTHER;
908                                 rs->sr_text = "internal error in mdb_entry_decode";
909                                 send_ldap_result( op, rs );
910                                 goto done;
911                         }
912                         e->e_id = id;
913                         e->e_name.bv_val = NULL;
914                         e->e_nname.bv_val = NULL;
915                 }
916
917                 if ( is_entry_subentry( e ) ) {
918                         if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
919                                 if(!get_subentries_visibility( op )) {
920                                         /* only subentries are visible */
921                                         goto loop_continue;
922                                 }
923
924                         } else if ( get_subentries( op ) &&
925                                 !get_subentries_visibility( op ))
926                         {
927                                 /* only subentries are visible */
928                                 goto loop_continue;
929                         }
930
931                 } else if ( get_subentries_visibility( op )) {
932                         /* only subentries are visible */
933                         goto loop_continue;
934                 }
935
936                 /* aliases were already dereferenced in candidate list */
937                 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
938                         /* but if the search base is an alias, and we didn't
939                          * deref it when finding, return it.
940                          */
941                         if ( is_entry_alias(e) &&
942                                 ((op->ors_deref & LDAP_DEREF_FINDING) || e != base ))
943                         {
944                                 goto loop_continue;
945                         }
946                 }
947
948                 if ( !manageDSAit && is_entry_glue( e )) {
949                         goto loop_continue;
950                 }
951
952                 if (e != base) {
953                         struct berval pdn, pndn;
954                         char *d, *n;
955                         int i;
956
957                         /* child of base, just append RDNs to base->e_name */
958                         if ( nsubs < ncand || isc.scopes[isc.nscope].mid == base->e_id ) {
959                                 pdn = base->e_name;
960                                 pndn = base->e_nname;
961                         } else {
962                                 mdb_id2name( op, ltid, &isc.mc, scopes[isc.nscope].mid, &pdn, &pndn );
963                         }
964                         e->e_name.bv_len = pdn.bv_len;
965                         e->e_nname.bv_len = pndn.bv_len;
966                         for (i=0; i<isc.numrdns; i++) {
967                                 e->e_name.bv_len += isc.rdns[i].bv_len + 1;
968                                 e->e_nname.bv_len += isc.nrdns[i].bv_len + 1;
969                         }
970                         e->e_name.bv_val = op->o_tmpalloc(e->e_name.bv_len + 1, op->o_tmpmemctx);
971                         e->e_nname.bv_val = op->o_tmpalloc(e->e_nname.bv_len + 1, op->o_tmpmemctx);
972                         d = e->e_name.bv_val;
973                         n = e->e_nname.bv_val;
974                         if (nsubs < ncand) {
975                                 /* RDNs are in top-down order */
976                                 for (i=isc.numrdns-1; i>=0; i--) {
977                                         memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
978                                         d += isc.rdns[i].bv_len;
979                                         *d++ = ',';
980                                         memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
981                                         n += isc.nrdns[i].bv_len;
982                                         *n++ = ',';
983                                 }
984                         } else {
985                                 /* RDNs are in bottom-up order */
986                                 for (i=0; i<isc.numrdns; i++) {
987                                         memcpy(d, isc.rdns[i].bv_val, isc.rdns[i].bv_len);
988                                         d += isc.rdns[i].bv_len;
989                                         *d++ = ',';
990                                         memcpy(n, isc.nrdns[i].bv_val, isc.nrdns[i].bv_len);
991                                         n += isc.nrdns[i].bv_len;
992                                         *n++ = ',';
993                                 }
994                         }
995
996                         if (pdn.bv_len) {
997                                 memcpy(d, pdn.bv_val, pdn.bv_len+1);
998                                 memcpy(n, pndn.bv_val, pndn.bv_len+1);
999                         } else {
1000                                 *--d = '\0';
1001                                 *--n = '\0';
1002                                 e->e_name.bv_len--;
1003                                 e->e_nname.bv_len--;
1004                         }
1005                         if (pndn.bv_val != base->e_nname.bv_val) {
1006                                 op->o_tmpfree(pndn.bv_val, op->o_tmpmemctx);
1007                                 op->o_tmpfree(pdn.bv_val, op->o_tmpmemctx);
1008                         }
1009                 }
1010
1011                 /*
1012                  * if it's a referral, add it to the list of referrals. only do
1013                  * this for non-base searches, and don't check the filter
1014                  * explicitly here since it's only a candidate anyway.
1015                  */
1016                 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
1017                         && is_entry_referral( e ) )
1018                 {
1019                         BerVarray erefs = get_entry_referrals( op, e );
1020                         rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1021                                 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1022                                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1023
1024                         rs->sr_entry = e;
1025                         rs->sr_flags = 0;
1026
1027                         send_search_reference( op, rs );
1028
1029                         if (e != base)
1030                                 mdb_entry_return( op, e );
1031                         rs->sr_entry = NULL;
1032                         e = NULL;
1033
1034                         ber_bvarray_free( rs->sr_ref );
1035                         ber_bvarray_free( erefs );
1036                         rs->sr_ref = NULL;
1037
1038                         if ( wwctx.flag ) {
1039                                 rs->sr_err = mdb_waitfixup( op, &wwctx, mci, mcd );
1040                                 if ( rs->sr_err ) {
1041                                         send_ldap_result( op, rs );
1042                                         goto done;
1043                                 }
1044                         }
1045
1046                         goto loop_continue;
1047                 }
1048
1049                 /* if it matches the filter and scope, send it */
1050                 rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
1051
1052                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1053                         /* check size limit */
1054                         if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
1055                                 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
1056                                         mdb_entry_return( op, e );
1057                                         e = NULL;
1058                                         send_paged_response( op, rs, &lastid, tentries );
1059                                         goto done;
1060                                 }
1061                                 lastid = id;
1062                         }
1063
1064                         if (e) {
1065                                 /* safe default */
1066                                 rs->sr_attrs = op->oq_search.rs_attrs;
1067                                 rs->sr_operational_attrs = NULL;
1068                                 rs->sr_ctrls = NULL;
1069                                 rs->sr_entry = e;
1070                                 RS_ASSERT( e->e_private != NULL );
1071                                 rs->sr_flags = 0;
1072                                 rs->sr_err = LDAP_SUCCESS;
1073                                 rs->sr_err = send_search_entry( op, rs );
1074                                 rs->sr_attrs = NULL;
1075                                 rs->sr_entry = NULL;
1076                                 if (e != base)
1077                                         mdb_entry_return( op, e );
1078                                 e = NULL;
1079
1080                                 switch ( rs->sr_err ) {
1081                                 case LDAP_SUCCESS:      /* entry sent ok */
1082                                         break;
1083                                 default:                /* entry not sent */
1084                                         break;
1085                                 case LDAP_BUSY:
1086                                         send_ldap_result( op, rs );
1087                                         goto done;
1088                                 case LDAP_UNAVAILABLE:
1089                                 case LDAP_SIZELIMIT_EXCEEDED:
1090                                         if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1091                                                 rs->sr_ref = rs->sr_v2ref;
1092                                                 send_ldap_result( op, rs );
1093                                                 rs->sr_err = LDAP_SUCCESS;
1094
1095                                         } else {
1096                                                 rs->sr_err = LDAP_OTHER;
1097                                         }
1098                                         goto done;
1099                                 }
1100                                 if ( wwctx.flag ) {
1101                                         rs->sr_err = mdb_waitfixup( op, &wwctx, mci, mcd );
1102                                         if ( rs->sr_err ) {
1103                                                 send_ldap_result( op, rs );
1104                                                 goto done;
1105                                         }
1106                                 }
1107                         }
1108
1109                 } else {
1110                         Debug( LDAP_DEBUG_TRACE,
1111                                 LDAP_XSTRING(mdb_search)
1112                                 ": %ld does not match filter\n",
1113                                 (long) id, 0, 0 );
1114                 }
1115
1116 loop_continue:
1117                 if( e != NULL ) {
1118                         if ( e != base )
1119                                 mdb_entry_return( op, e );
1120                         RS_ASSERT( rs->sr_entry == NULL );
1121                         e = NULL;
1122                         rs->sr_entry = NULL;
1123                 }
1124
1125                 if ( nsubs < ncand ) {
1126                         int rc = mdb_dn2id_walk( op, &isc );
1127                         if (rc) {
1128                                 id = NOID;
1129                                 /* We got to the end of a subtree. If there are any
1130                                  * alias scopes left, search them too.
1131                                  */
1132                                 while (iscopes[0] && cscope < iscopes[0]) {
1133                                         cscope++;
1134                                         isc.id = iscopes[cscope];
1135                                         if ( base )
1136                                                 mdb_entry_return( op, base );
1137                                         rs->sr_err = mdb_id2entry(op, mci, isc.id, &base);
1138                                         if ( !rs->sr_err ) {
1139                                                 mdb_id2name( op, ltid, &isc.mc, isc.id, &base->e_name, &base->e_nname );
1140                                                 isc.numrdns = 0;
1141                                                 if (isc.oscope == LDAP_SCOPE_ONELEVEL)
1142                                                         isc.oscope = LDAP_SCOPE_BASE;
1143                                                 rc = mdb_dn2id_walk( op, &isc );
1144                                                 if ( !rc ) {
1145                                                         id = isc.id;
1146                                                         break;
1147                                                 }
1148                                         }
1149                                 }
1150                         } else
1151                                 id = isc.id;
1152                 } else {
1153                         id = mdb_idl_next( candidates, &cursor );
1154                 }
1155         }
1156
1157 nochange:
1158         rs->sr_ctrls = NULL;
1159         rs->sr_ref = rs->sr_v2ref;
1160         rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1161         rs->sr_rspoid = NULL;
1162         if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
1163                 send_paged_response( op, rs, NULL, 0 );
1164         } else {
1165                 send_ldap_result( op, rs );
1166         }
1167
1168         rs->sr_err = LDAP_SUCCESS;
1169
1170 done:
1171         if ( cb.sc_private ) {
1172                 /* remove our writewait callback */
1173                 slap_callback **scp = &op->o_callback;
1174                 while ( *scp ) {
1175                         if ( *scp == &cb ) {
1176                                 *scp = cb.sc_next;
1177                                 cb.sc_private = NULL;
1178                                 break;
1179                         }
1180                 }
1181         }
1182         mdb_cursor_close( mcd );
1183         mdb_cursor_close( mci );
1184         if ( moi == &opinfo ) {
1185                 mdb_txn_reset( moi->moi_txn );
1186                 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
1187         } else {
1188                 moi->moi_ref--;
1189         }
1190         if( rs->sr_v2ref ) {
1191                 ber_bvarray_free( rs->sr_v2ref );
1192                 rs->sr_v2ref = NULL;
1193         }
1194         if (base)
1195                 mdb_entry_return( op, base );
1196         scope_chunk_ret( op, scopes );
1197
1198         return rs->sr_err;
1199 }
1200
1201
1202 static int base_candidate(
1203         BackendDB       *be,
1204         Entry   *e,
1205         ID              *ids )
1206 {
1207         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1208                 e->e_nname.bv_val, (long) e->e_id, 0);
1209
1210         ids[0] = 1;
1211         ids[1] = e->e_id;
1212         return 0;
1213 }
1214
1215 /* Look for "objectClass Present" in this filter.
1216  * Also count depth of filter tree while we're at it.
1217  */
1218 static int oc_filter(
1219         Filter *f,
1220         int cur,
1221         int *max )
1222 {
1223         int rc = 0;
1224
1225         assert( f != NULL );
1226
1227         if( cur > *max ) *max = cur;
1228
1229         switch( f->f_choice ) {
1230         case LDAP_FILTER_PRESENT:
1231                 if (f->f_desc == slap_schema.si_ad_objectClass) {
1232                         rc = 1;
1233                 }
1234                 break;
1235
1236         case LDAP_FILTER_AND:
1237         case LDAP_FILTER_OR:
1238                 cur++;
1239                 for ( f=f->f_and; f; f=f->f_next ) {
1240                         (void) oc_filter(f, cur, max);
1241                 }
1242                 break;
1243
1244         default:
1245                 break;
1246         }
1247         return rc;
1248 }
1249
1250 static void search_stack_free( void *key, void *data )
1251 {
1252         ber_memfree_x(data, NULL);
1253 }
1254
1255 static void *search_stack( Operation *op )
1256 {
1257         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1258         void *ret = NULL;
1259
1260         if ( op->o_threadctx ) {
1261                 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
1262                         &ret, NULL );
1263         } else {
1264                 ret = mdb->mi_search_stack;
1265         }
1266
1267         if ( !ret ) {
1268                 ret = ch_malloc( mdb->mi_search_stack_depth * MDB_IDL_UM_SIZE
1269                         * sizeof( ID ) );
1270                 if ( op->o_threadctx ) {
1271                         ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1272                                 ret, search_stack_free, NULL, NULL );
1273                 } else {
1274                         mdb->mi_search_stack = ret;
1275                 }
1276         }
1277         return ret;
1278 }
1279
1280 static int search_candidates(
1281         Operation *op,
1282         SlapReply *rs,
1283         Entry *e,
1284         IdScopes *isc,
1285         MDB_cursor *mci,
1286         ID      *ids,
1287         ID *stack )
1288 {
1289         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1290         int rc, depth = 1;
1291         Filter          *f, rf, xf, nf, sf;
1292         AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1293         AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1294
1295         /*
1296          * This routine takes as input a filter (user-filter)
1297          * and rewrites it as follows:
1298          *      (&(scope=DN)[(objectClass=subentry)]
1299          *              (|[(objectClass=referral)](user-filter))
1300          */
1301
1302         Debug(LDAP_DEBUG_TRACE,
1303                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1304                 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1305
1306         f = op->oq_search.rs_filter;
1307
1308         /* If the user's filter uses objectClass=*,
1309          * these clauses are redundant.
1310          */
1311         if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1312                 && !get_subentries_visibility(op)) {
1313                 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1314                         /* match referral objects */
1315                         struct berval bv_ref = BER_BVC( "referral" );
1316                         rf.f_choice = LDAP_FILTER_EQUALITY;
1317                         rf.f_ava = &aa_ref;
1318                         rf.f_av_desc = slap_schema.si_ad_objectClass;
1319                         rf.f_av_value = bv_ref;
1320                         rf.f_next = f;
1321                         xf.f_or = &rf;
1322                         xf.f_choice = LDAP_FILTER_OR;
1323                         xf.f_next = NULL;
1324                         f = &xf;
1325                         depth++;
1326                 }
1327         }
1328
1329         if( get_subentries_visibility( op ) ) {
1330                 struct berval bv_subentry = BER_BVC( "subentry" );
1331                 sf.f_choice = LDAP_FILTER_EQUALITY;
1332                 sf.f_ava = &aa_subentry;
1333                 sf.f_av_desc = slap_schema.si_ad_objectClass;
1334                 sf.f_av_value = bv_subentry;
1335                 sf.f_next = f;
1336                 nf.f_choice = LDAP_FILTER_AND;
1337                 nf.f_and = &sf;
1338                 nf.f_next = NULL;
1339                 f = &nf;
1340                 depth++;
1341         }
1342
1343         /* Allocate IDL stack, plus 1 more for former tmp */
1344         if ( depth+1 > mdb->mi_search_stack_depth ) {
1345                 stack = ch_malloc( (depth + 1) * MDB_IDL_UM_SIZE * sizeof( ID ) );
1346         }
1347
1348         if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1349                 rc = search_aliases( op, rs, e->e_id, isc, mci, stack );
1350         } else {
1351                 rc = LDAP_SUCCESS;
1352         }
1353
1354         if ( rc == LDAP_SUCCESS ) {
1355                 rc = mdb_filter_candidates( op, isc->mt, f, ids,
1356                         stack, stack+MDB_IDL_UM_SIZE );
1357         }
1358
1359         if ( depth+1 > mdb->mi_search_stack_depth ) {
1360                 ch_free( stack );
1361         }
1362
1363         if( rc ) {
1364                 Debug(LDAP_DEBUG_TRACE,
1365                         "mdb_search_candidates: failed (rc=%d)\n",
1366                         rc, NULL, NULL );
1367
1368         } else {
1369                 Debug(LDAP_DEBUG_TRACE,
1370                         "mdb_search_candidates: id=%ld first=%ld last=%ld\n",
1371                         (long) ids[0],
1372                         (long) MDB_IDL_FIRST(ids),
1373                         (long) MDB_IDL_LAST(ids) );
1374         }
1375
1376         return rc;
1377 }
1378
1379 static int
1380 parse_paged_cookie( Operation *op, SlapReply *rs )
1381 {
1382         int             rc = LDAP_SUCCESS;
1383         PagedResultsState *ps = op->o_pagedresults_state;
1384
1385         /* this function must be invoked only if the pagedResults
1386          * control has been detected, parsed and partially checked
1387          * by the frontend */
1388         assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1389
1390         /* cookie decoding/checks deferred to backend... */
1391         if ( ps->ps_cookieval.bv_len ) {
1392                 PagedResultsCookie reqcookie;
1393                 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1394                         /* bad cookie */
1395                         rs->sr_text = "paged results cookie is invalid";
1396                         rc = LDAP_PROTOCOL_ERROR;
1397                         goto done;
1398                 }
1399
1400                 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1401
1402                 if ( reqcookie > ps->ps_cookie ) {
1403                         /* bad cookie */
1404                         rs->sr_text = "paged results cookie is invalid";
1405                         rc = LDAP_PROTOCOL_ERROR;
1406                         goto done;
1407
1408                 } else if ( reqcookie < ps->ps_cookie ) {
1409                         rs->sr_text = "paged results cookie is invalid or old";
1410                         rc = LDAP_UNWILLING_TO_PERFORM;
1411                         goto done;
1412                 }
1413
1414         } else {
1415                 /* we're going to use ps_cookie */
1416                 op->o_conn->c_pagedresults_state.ps_cookie = 0;
1417         }
1418
1419 done:;
1420
1421         return rc;
1422 }
1423
1424 static void
1425 send_paged_response( 
1426         Operation       *op,
1427         SlapReply       *rs,
1428         ID              *lastid,
1429         int             tentries )
1430 {
1431         LDAPControl     *ctrls[2];
1432         BerElementBuffer berbuf;
1433         BerElement      *ber = (BerElement *)&berbuf;
1434         PagedResultsCookie respcookie;
1435         struct berval cookie;
1436
1437         Debug(LDAP_DEBUG_ARGS,
1438                 "send_paged_response: lastid=0x%08lx nentries=%d\n", 
1439                 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1440
1441         ctrls[1] = NULL;
1442
1443         ber_init2( ber, NULL, LBER_USE_DER );
1444
1445         if ( lastid ) {
1446                 respcookie = ( PagedResultsCookie )(*lastid);
1447                 cookie.bv_len = sizeof( respcookie );
1448                 cookie.bv_val = (char *)&respcookie;
1449
1450         } else {
1451                 respcookie = ( PagedResultsCookie )0;
1452                 BER_BVSTR( &cookie, "" );
1453         }
1454
1455         op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1456         op->o_conn->c_pagedresults_state.ps_count =
1457                 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1458                 rs->sr_nentries;
1459
1460         /* return size of 0 -- no estimate */
1461         ber_printf( ber, "{iO}", 0, &cookie ); 
1462
1463         ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1464         if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1465                 goto done;
1466         }
1467
1468         ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1469         ctrls[0]->ldctl_iscritical = 0;
1470
1471         slap_add_ctrls( op, rs, ctrls );
1472         rs->sr_err = LDAP_SUCCESS;
1473         send_ldap_result( op, rs );
1474
1475 done:
1476         (void) ber_free_buf( ber );
1477 }