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