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