]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/search.c
ITS#6095 experimental fix: avoid purging other cases of in-use EntryInfos,
[openldap] / servers / slapd / back-bdb / 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-2009 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-bdb.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         DB_TXN *txn,
35         ID      *ids,
36         ID      *scopes );
37
38 static int parse_paged_cookie( Operation *op, SlapReply *rs );
39
40 static void send_paged_response( 
41         Operation *op,
42         SlapReply *rs,
43         ID  *lastid,
44         int tentries );
45
46 /* Dereference aliases for a single alias entry. Return the final
47  * dereferenced entry on success, NULL on any failure.
48  */
49 static Entry * deref_base (
50         Operation *op,
51         SlapReply *rs,
52         Entry *e,
53         Entry **matched,
54         DB_TXN *txn,
55         DB_LOCK *lock,
56         ID      *tmp,
57         ID      *visited )
58 {
59         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
60         struct berval ndn;
61         EntryInfo *ei;
62         DB_LOCK lockr;
63
64         rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
65         rs->sr_text = "maximum deref depth exceeded";
66
67         for (;;) {
68                 /* Remember the last entry we looked at, so we can
69                  * report broken links
70                  */
71                 *matched = e;
72
73                 if (BDB_IDL_N(tmp) >= op->o_bd->be_max_deref_depth) {
74                         e = NULL;
75                         break;
76                 }
77
78                 /* If this is part of a subtree or onelevel search,
79                  * have we seen this ID before? If so, quit.
80                  */
81                 if ( visited && bdb_idl_insert( visited, e->e_id ) ) {
82                         e = NULL;
83                         break;
84                 }
85
86                 /* If we've seen this ID during this deref iteration,
87                  * we've hit a loop.
88                  */
89                 if ( bdb_idl_insert( tmp, e->e_id ) ) {
90                         rs->sr_err = LDAP_ALIAS_PROBLEM;
91                         rs->sr_text = "circular alias";
92                         e = NULL;
93                         break;
94                 }
95
96                 /* If there was a problem getting the aliasedObjectName,
97                  * get_alias_dn will have set the error status.
98                  */
99                 if ( get_alias_dn(e, &ndn, &rs->sr_err, &rs->sr_text) ) {
100                         e = NULL;
101                         break;
102                 }
103
104                 rs->sr_err = bdb_dn2entry( op, txn, &ndn, &ei,
105                         0, &lockr );
106                 if ( rs->sr_err == DB_LOCK_DEADLOCK )
107                         return NULL;
108
109                 if ( ei ) {
110                         e = ei->bei_e;
111                 } else {
112                         e = NULL;
113                 }
114
115                 if (!e) {
116                         rs->sr_err = LDAP_ALIAS_PROBLEM;
117                         rs->sr_text = "aliasedObject not found";
118                         break;
119                 }
120
121                 /* Free the previous entry, continue to work with the
122                  * one we just retrieved.
123                  */
124                 bdb_cache_return_entry_r( bdb, *matched, lock);
125                 *lock = lockr;
126
127                 /* We found a regular entry. Return this to the caller. The
128                  * entry is still locked for Read.
129                  */
130                 if (!is_entry_alias(e)) {
131                         rs->sr_err = LDAP_SUCCESS;
132                         rs->sr_text = NULL;
133                         break;
134                 }
135         }
136         return e;
137 }
138
139 /* Look for and dereference all aliases within the search scope. Adds
140  * the dereferenced entries to the "ids" list. Requires "stack" to be
141  * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
142  * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
143  */
144 static int search_aliases(
145         Operation *op,
146         SlapReply *rs,
147         Entry *e,
148         DB_TXN *txn,
149         ID *ids,
150         ID *scopes,
151         ID *stack )
152 {
153         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
154         ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
155         ID cursora, ida, cursoro, ido, *subscop2;
156         Entry *matched, *a;
157         EntryInfo *ei;
158         struct berval bv_alias = BER_BVC( "alias" );
159         AttributeAssertion aa_alias = ATTRIBUTEASSERTION_INIT;
160         Filter  af;
161         DB_LOCK locka, lockr;
162         int first = 1;
163
164         aliases = stack;        /* IDL of all aliases in the database */
165         curscop = aliases + BDB_IDL_DB_SIZE;    /* Aliases in the current scope */
166         subscop = curscop + BDB_IDL_DB_SIZE;    /* The current scope */
167         visited = subscop + BDB_IDL_DB_SIZE;    /* IDs we've seen in this search */
168         newsubs = visited + BDB_IDL_DB_SIZE;    /* New subtrees we've added */
169         oldsubs = newsubs + BDB_IDL_DB_SIZE;    /* Subtrees added previously */
170         tmp = oldsubs + BDB_IDL_DB_SIZE;        /* Scratch space for deref_base() */
171
172         /* A copy of subscop, because subscop gets clobbered by
173          * the bdb_idl_union/intersection routines
174          */
175         subscop2 = tmp + BDB_IDL_DB_SIZE;
176
177         af.f_choice = LDAP_FILTER_EQUALITY;
178         af.f_ava = &aa_alias;
179         af.f_av_desc = slap_schema.si_ad_objectClass;
180         af.f_av_value = bv_alias;
181         af.f_next = NULL;
182
183         /* Find all aliases in database */
184         BDB_IDL_ZERO( aliases );
185         rs->sr_err = bdb_filter_candidates( op, txn, &af, aliases,
186                 curscop, visited );
187         if (rs->sr_err != LDAP_SUCCESS) {
188                 return rs->sr_err;
189         }
190         oldsubs[0] = 1;
191         oldsubs[1] = e->e_id;
192
193         BDB_IDL_ZERO( ids );
194         BDB_IDL_ZERO( visited );
195         BDB_IDL_ZERO( newsubs );
196
197         cursoro = 0;
198         ido = bdb_idl_first( oldsubs, &cursoro );
199
200         for (;;) {
201                 /* Set curscop to only the aliases in the current scope. Start with
202                  * all the aliases, obtain the IDL for the current scope, and then
203                  * get the intersection of these two IDLs. Add the current scope
204                  * to the cumulative list of candidates.
205                  */
206                 BDB_IDL_CPY( curscop, aliases );
207                 rs->sr_err = bdb_dn2idl( op, txn, &e->e_nname, BEI(e), subscop,
208                         subscop2+BDB_IDL_DB_SIZE );
209
210                 if (first) {
211                         first = 0;
212                 } else {
213                         bdb_cache_return_entry_r (bdb, e, &locka);
214                 }
215                 if ( rs->sr_err == DB_LOCK_DEADLOCK )
216                         return rs->sr_err;
217
218                 BDB_IDL_CPY(subscop2, subscop);
219                 rs->sr_err = bdb_idl_intersection(curscop, subscop);
220                 bdb_idl_union( ids, subscop2 );
221
222                 /* Dereference all of the aliases in the current scope. */
223                 cursora = 0;
224                 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
225                         ida = bdb_idl_next(curscop, &cursora))
226                 {
227                         ei = NULL;
228 retry1:
229                         rs->sr_err = bdb_cache_find_id(op, txn,
230                                 ida, &ei, 0, &lockr );
231                         if (rs->sr_err != LDAP_SUCCESS) {
232                                 if ( rs->sr_err == DB_LOCK_DEADLOCK )
233                                         return rs->sr_err;
234                                 if ( rs->sr_err == DB_LOCK_NOTGRANTED )
235                                         goto retry1;
236                                 continue;
237                         }
238                         a = ei->bei_e;
239
240                         /* This should only happen if the curscop IDL has maxed out and
241                          * turned into a range that spans IDs indiscriminately
242                          */
243                         if (!is_entry_alias(a)) {
244                                 bdb_cache_return_entry_r (bdb, a, &lockr);
245                                 continue;
246                         }
247
248                         /* Actually dereference the alias */
249                         BDB_IDL_ZERO(tmp);
250                         a = deref_base( op, rs, a, &matched, txn, &lockr,
251                                 tmp, visited );
252                         if (a) {
253                                 /* If the target was not already in our current candidates,
254                                  * make note of it in the newsubs list. Also
255                                  * set it in the scopes list so that bdb_search
256                                  * can check it.
257                                  */
258                                 if (bdb_idl_insert(ids, a->e_id) == 0) {
259                                         bdb_idl_insert(newsubs, a->e_id);
260                                         bdb_idl_insert(scopes, a->e_id);
261                                 }
262                                 bdb_cache_return_entry_r( bdb, a, &lockr);
263
264                         } else if ( rs->sr_err == DB_LOCK_DEADLOCK ) {
265                                 return rs->sr_err;
266                         } else if (matched) {
267                                 /* Alias could not be dereferenced, or it deref'd to
268                                  * an ID we've already seen. Ignore it.
269                                  */
270                                 bdb_cache_return_entry_r( bdb, matched, &lockr );
271                                 rs->sr_text = NULL;
272                         }
273                 }
274                 /* If this is a OneLevel search, we're done; oldsubs only had one
275                  * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
276                  */
277                 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
278 nextido:
279                 ido = bdb_idl_next( oldsubs, &cursoro );
280                 
281                 /* If we're done processing the old scopes, did we add any new
282                  * scopes in this iteration? If so, go back and do those now.
283                  */
284                 if (ido == NOID) {
285                         if (BDB_IDL_IS_ZERO(newsubs)) break;
286                         BDB_IDL_CPY(oldsubs, newsubs);
287                         BDB_IDL_ZERO(newsubs);
288                         cursoro = 0;
289                         ido = bdb_idl_first( oldsubs, &cursoro );
290                 }
291
292                 /* Find the entry corresponding to the next scope. If it can't
293                  * be found, ignore it and move on. This should never happen;
294                  * we should never see the ID of an entry that doesn't exist.
295                  * Set the name so that the scope's IDL can be retrieved.
296                  */
297                 ei = NULL;
298 sameido:
299                 rs->sr_err = bdb_cache_find_id(op, txn, ido, &ei,
300                         0, &locka );
301                 if ( rs->sr_err != LDAP_SUCCESS ) {
302                         if ( rs->sr_err == DB_LOCK_DEADLOCK )
303                                 return rs->sr_err;
304                         if ( rs->sr_err == DB_LOCK_NOTGRANTED )
305                                 goto sameido;
306                         goto nextido;
307                 }
308                 e = ei->bei_e;
309         }
310         return rs->sr_err;
311 }
312
313 int
314 bdb_search( Operation *op, SlapReply *rs )
315 {
316         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
317         ID              id, cursor;
318         ID              lastid = NOID;
319         ID              candidates[BDB_IDL_UM_SIZE];
320         ID              scopes[BDB_IDL_DB_SIZE];
321         Entry           *e = NULL, base, *e_root;
322         Entry           *matched = NULL;
323         EntryInfo       *ei;
324         AttributeName   *attrs;
325         struct berval   realbase = BER_BVNULL;
326         slap_mask_t     mask;
327         time_t          stoptime;
328         int             manageDSAit;
329         int             tentries = 0, nentries = 0;
330         int             idflag = 0;
331
332         DB_LOCK         lock;
333         struct  bdb_op_info     *opinfo = NULL;
334         DB_TXN                  *ltid = NULL;
335         OpExtra *oex;
336
337         Debug( LDAP_DEBUG_TRACE, "=> " LDAP_XSTRING(bdb_search) "\n", 0, 0, 0);
338         attrs = op->oq_search.rs_attrs;
339
340         LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
341                 if ( oex->oe_key == bdb )
342                         break;
343         }
344         opinfo = (struct bdb_op_info *) oex;
345
346         manageDSAit = get_manageDSAit( op );
347
348         if ( opinfo && opinfo->boi_txn ) {
349                 ltid = opinfo->boi_txn;
350         } else {
351                 rs->sr_err = bdb_reader_get( op, bdb->bi_dbenv, &ltid );
352
353                 switch(rs->sr_err) {
354                 case 0:
355                         break;
356                 default:
357                         send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
358                         return rs->sr_err;
359                 }
360         }
361
362         e_root = bdb->bi_cache.c_dntree.bei_e;
363         if ( op->o_req_ndn.bv_len == 0 ) {
364                 /* DIT root special case */
365                 ei = e_root->e_private;
366                 rs->sr_err = LDAP_SUCCESS;
367         } else {
368                 if ( op->ors_deref & LDAP_DEREF_FINDING ) {
369                         BDB_IDL_ZERO(candidates);
370                 }
371 dn2entry_retry:
372                 /* get entry with reader lock */
373                 rs->sr_err = bdb_dn2entry( op, ltid, &op->o_req_ndn, &ei,
374                         1, &lock );
375         }
376
377         switch(rs->sr_err) {
378         case DB_NOTFOUND:
379                 matched = ei->bei_e;
380                 break;
381         case 0:
382                 e = ei->bei_e;
383                 break;
384         case DB_LOCK_DEADLOCK:
385                 if ( !opinfo ) {
386                         ltid->flags &= ~TXN_DEADLOCK;
387                         goto dn2entry_retry;
388                 }
389                 opinfo->boi_err = rs->sr_err;
390                 /* FALLTHRU */
391         case LDAP_BUSY:
392                 send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
393                 return LDAP_BUSY;
394         case DB_LOCK_NOTGRANTED:
395                 goto dn2entry_retry;
396         default:
397                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
398                 return rs->sr_err;
399         }
400
401         if ( op->ors_deref & LDAP_DEREF_FINDING ) {
402                 if ( matched && is_entry_alias( matched )) {
403                         struct berval stub;
404
405                         stub.bv_val = op->o_req_ndn.bv_val;
406                         stub.bv_len = op->o_req_ndn.bv_len - matched->e_nname.bv_len - 1;
407                         e = deref_base( op, rs, matched, &matched, ltid, &lock,
408                                 candidates, NULL );
409                         if ( e ) {
410                                 build_new_dn( &op->o_req_ndn, &e->e_nname, &stub,
411                                         op->o_tmpmemctx );
412                                 bdb_cache_return_entry_r (bdb, e, &lock);
413                                 matched = NULL;
414                                 goto dn2entry_retry;
415                         }
416                 } else if ( e && is_entry_alias( e )) {
417                         e = deref_base( op, rs, e, &matched, ltid, &lock,
418                                 candidates, NULL );
419                 }
420         }
421
422         if ( e == NULL ) {
423                 struct berval matched_dn = BER_BVNULL;
424
425                 if ( matched != NULL ) {
426                         BerVarray erefs = NULL;
427
428                         /* return referral only if "disclose"
429                          * is granted on the object */
430                         if ( ! access_allowed( op, matched,
431                                                 slap_schema.si_ad_entry,
432                                                 NULL, ACL_DISCLOSE, NULL ) )
433                         {
434                                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
435
436                         } else {
437                                 ber_dupbv( &matched_dn, &matched->e_name );
438
439                                 erefs = is_entry_referral( matched )
440                                         ? get_entry_referrals( op, matched )
441                                         : NULL;
442                                 if ( rs->sr_err == DB_NOTFOUND )
443                                         rs->sr_err = LDAP_REFERRAL;
444                                 rs->sr_matched = matched_dn.bv_val;
445                         }
446
447 #ifdef SLAP_ZONE_ALLOC
448                         slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
449 #endif
450                         bdb_cache_return_entry_r (bdb, matched, &lock);
451                         matched = NULL;
452
453                         if ( erefs ) {
454                                 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
455                                         &op->o_req_dn, op->oq_search.rs_scope );
456                                 ber_bvarray_free( erefs );
457                         }
458
459                 } else {
460 #ifdef SLAP_ZONE_ALLOC
461                         slap_zn_runlock(bdb->bi_cache.c_zctx, matched);
462 #endif
463                         rs->sr_ref = referral_rewrite( default_referral,
464                                 NULL, &op->o_req_dn, op->oq_search.rs_scope );
465                         rs->sr_err = rs->sr_ref != NULL ? LDAP_REFERRAL : LDAP_NO_SUCH_OBJECT;
466                 }
467
468                 send_ldap_result( op, rs );
469
470                 if ( rs->sr_ref ) {
471                         ber_bvarray_free( rs->sr_ref );
472                         rs->sr_ref = NULL;
473                 }
474                 if ( !BER_BVISNULL( &matched_dn ) ) {
475                         ber_memfree( matched_dn.bv_val );
476                         rs->sr_matched = NULL;
477                 }
478                 return rs->sr_err;
479         }
480
481         /* NOTE: __NEW__ "search" access is required
482          * on searchBase object */
483         if ( ! access_allowed_mask( op, e, slap_schema.si_ad_entry,
484                                 NULL, ACL_SEARCH, NULL, &mask ) )
485         {
486                 if ( !ACL_GRANT( mask, ACL_DISCLOSE ) ) {
487                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
488                 } else {
489                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
490                 }
491
492 #ifdef SLAP_ZONE_ALLOC
493                 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
494 #endif
495                 if ( e != e_root ) {
496                         bdb_cache_return_entry_r(bdb, e, &lock);
497                 }
498                 send_ldap_result( op, rs );
499                 return rs->sr_err;
500         }
501
502         if ( !manageDSAit && e != e_root && is_entry_referral( e ) ) {
503                 /* entry is a referral, don't allow add */
504                 struct berval matched_dn = BER_BVNULL;
505                 BerVarray erefs = NULL;
506                 
507                 ber_dupbv( &matched_dn, &e->e_name );
508                 erefs = get_entry_referrals( op, e );
509
510                 rs->sr_err = LDAP_REFERRAL;
511
512 #ifdef SLAP_ZONE_ALLOC
513                 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
514 #endif
515                 bdb_cache_return_entry_r( bdb, e, &lock );
516                 e = NULL;
517
518                 if ( erefs ) {
519                         rs->sr_ref = referral_rewrite( erefs, &matched_dn,
520                                 &op->o_req_dn, op->oq_search.rs_scope );
521                         ber_bvarray_free( erefs );
522
523                         if ( !rs->sr_ref ) {
524                                 rs->sr_text = "bad_referral object";
525                         }
526                 }
527
528                 Debug( LDAP_DEBUG_TRACE,
529                         LDAP_XSTRING(bdb_search) ": entry is referral\n",
530                         0, 0, 0 );
531
532                 rs->sr_matched = matched_dn.bv_val;
533                 send_ldap_result( op, rs );
534
535                 ber_bvarray_free( rs->sr_ref );
536                 rs->sr_ref = NULL;
537                 ber_memfree( matched_dn.bv_val );
538                 rs->sr_matched = NULL;
539                 return 1;
540         }
541
542         if ( get_assert( op ) &&
543                 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
544         {
545                 rs->sr_err = LDAP_ASSERTION_FAILED;
546 #ifdef SLAP_ZONE_ALLOC
547                 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
548 #endif
549                 if ( e != e_root ) {
550                         bdb_cache_return_entry_r(bdb, e, &lock);
551                 }
552                 send_ldap_result( op, rs );
553                 return 1;
554         }
555
556         /* compute it anyway; root does not use it */
557         stoptime = op->o_time + op->ors_tlimit;
558
559         /* need normalized dn below */
560         ber_dupbv( &realbase, &e->e_nname );
561
562         /* Copy info to base, must free entry before accessing the database
563          * in search_candidates, to avoid deadlocks.
564          */
565         base.e_private = e->e_private;
566         base.e_nname = realbase;
567         base.e_id = e->e_id;
568
569 #ifdef SLAP_ZONE_ALLOC
570         slap_zn_runlock(bdb->bi_cache.c_zctx, e);
571 #endif
572
573         /* select candidates */
574         if ( op->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
575                 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
576
577         } else {
578 cand_retry:
579                 BDB_IDL_ZERO( candidates );
580                 BDB_IDL_ZERO( scopes );
581                 rs->sr_err = search_candidates( op, rs, &base,
582                         ltid, candidates, scopes );
583                 if ( rs->sr_err == DB_LOCK_DEADLOCK ) {
584                         if ( !opinfo ) {
585                                 ltid->flags &= ~TXN_DEADLOCK;
586                                 goto cand_retry;
587                         }
588                         opinfo->boi_err = rs->sr_err;
589                         send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
590                         return LDAP_BUSY;
591                 }
592         }
593
594         if ( e != e_root ) {
595                 bdb_cache_return_entry_r(bdb, e, &lock);
596         }
597         e = NULL;
598
599         /* start cursor at beginning of candidates.
600          */
601         cursor = 0;
602
603         if ( candidates[0] == 0 ) {
604                 Debug( LDAP_DEBUG_TRACE,
605                         LDAP_XSTRING(bdb_search) ": no candidates\n",
606                         0, 0, 0 );
607
608                 goto nochange;
609         }
610
611         /* if not root and candidates exceed to-be-checked entries, abort */
612         if ( op->ors_limit      /* isroot == FALSE */ &&
613                 op->ors_limit->lms_s_unchecked != -1 &&
614                 BDB_IDL_N(candidates) > (unsigned) op->ors_limit->lms_s_unchecked )
615         {
616                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
617                 send_ldap_result( op, rs );
618                 rs->sr_err = LDAP_SUCCESS;
619                 goto done;
620         }
621
622         if ( op->ors_limit == NULL      /* isroot == TRUE */ ||
623                 !op->ors_limit->lms_s_pr_hide )
624         {
625                 tentries = BDB_IDL_N(candidates);
626         }
627
628         if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
629                 PagedResultsState *ps = op->o_pagedresults_state;
630                 /* deferred cookie parsing */
631                 rs->sr_err = parse_paged_cookie( op, rs );
632                 if ( rs->sr_err != LDAP_SUCCESS ) {
633                         send_ldap_result( op, rs );
634                         goto done;
635                 }
636
637                 cursor = (ID) ps->ps_cookie;
638                 if ( cursor && ps->ps_size == 0 ) {
639                         rs->sr_err = LDAP_SUCCESS;
640                         rs->sr_text = "search abandoned by pagedResult size=0";
641                         send_ldap_result( op, rs );
642                         goto done;
643                 }
644                 id = bdb_idl_first( candidates, &cursor );
645                 if ( id == NOID ) {
646                         Debug( LDAP_DEBUG_TRACE, 
647                                 LDAP_XSTRING(bdb_search)
648                                 ": no paged results candidates\n",
649                                 0, 0, 0 );
650                         send_paged_response( op, rs, &lastid, 0 );
651
652                         rs->sr_err = LDAP_OTHER;
653                         goto done;
654                 }
655                 nentries = ps->ps_count;
656                 if ( id == (ID)ps->ps_cookie )
657                         id = bdb_idl_next( candidates, &cursor );
658                 goto loop_begin;
659         }
660
661         for ( id = bdb_idl_first( candidates, &cursor );
662                   id != NOID ; id = bdb_idl_next( candidates, &cursor ) )
663         {
664                 int scopeok;
665
666 loop_begin:
667
668                 /* check for abandon */
669                 if ( op->o_abandon ) {
670                         rs->sr_err = SLAPD_ABANDON;
671                         goto done;
672                 }
673
674                 /* mostly needed by internal searches,
675                  * e.g. related to syncrepl, for whom
676                  * abandon does not get set... */
677                 if ( slapd_shutdown ) {
678                         rs->sr_err = LDAP_UNAVAILABLE;
679                         send_ldap_disconnect( op, rs );
680                         goto done;
681                 }
682
683                 /* check time limit */
684                 if ( op->ors_tlimit != SLAP_NO_LIMIT
685                                 && slap_get_time() > stoptime )
686                 {
687                         rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
688                         rs->sr_ref = rs->sr_v2ref;
689                         send_ldap_result( op, rs );
690                         rs->sr_err = LDAP_SUCCESS;
691                         goto done;
692                 }
693
694                 /* If we inspect more entries than will
695                  * fit into the entry cache, stop caching
696                  * any subsequent entries
697                  */
698                 nentries++;
699                 if ( nentries > bdb->bi_cache.c_maxsize && !idflag ) {
700                         idflag = ID_NOCACHE;
701                 }
702
703 fetch_entry_retry:
704                 /* get the entry with reader lock */
705                 ei = NULL;
706                 rs->sr_err = bdb_cache_find_id( op, ltid,
707                         id, &ei, idflag, &lock );
708
709                 if (rs->sr_err == LDAP_BUSY) {
710                         rs->sr_text = "ldap server busy";
711                         send_ldap_result( op, rs );
712                         goto done;
713
714                 } else if ( rs->sr_err == DB_LOCK_DEADLOCK ) {
715                         if ( !opinfo ) {
716                                 ltid->flags &= ~TXN_DEADLOCK;
717                                 goto fetch_entry_retry;
718                         }
719                         opinfo->boi_err = rs->sr_err;
720                         send_ldap_error( op, rs, LDAP_BUSY, "ldap server busy" );
721                         goto done;
722
723                 } else if ( rs->sr_err == DB_LOCK_NOTGRANTED )
724                 {
725                         goto fetch_entry_retry;
726                 } else if ( rs->sr_err == LDAP_OTHER ) {
727                         rs->sr_text = "internal error";
728                         send_ldap_result( op, rs );
729                         goto done;
730                 }
731
732                 if ( ei && rs->sr_err == LDAP_SUCCESS ) {
733                         e = ei->bei_e;
734                 } else {
735                         e = NULL;
736                 }
737
738                 if ( e == NULL ) {
739                         if( !BDB_IDL_IS_RANGE(candidates) ) {
740                                 /* only complain for non-range IDLs */
741                                 Debug( LDAP_DEBUG_TRACE,
742                                         LDAP_XSTRING(bdb_search)
743                                         ": candidate %ld not found\n",
744                                         (long) id, 0, 0 );
745                         }
746
747                         goto loop_continue;
748                 }
749
750                 rs->sr_entry = e;
751
752                 if ( is_entry_subentry( e ) ) {
753                         if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
754                                 if(!get_subentries_visibility( op )) {
755                                         /* only subentries are visible */
756                                         goto loop_continue;
757                                 }
758
759                         } else if ( get_subentries( op ) &&
760                                 !get_subentries_visibility( op ))
761                         {
762                                 /* only subentries are visible */
763                                 goto loop_continue;
764                         }
765
766                 } else if ( get_subentries_visibility( op )) {
767                         /* only subentries are visible */
768                         goto loop_continue;
769                 }
770
771                 /* Does this candidate actually satisfy the search scope?
772                  *
773                  * Note that we don't lock access to the bei_parent pointer.
774                  * Since only leaf nodes can be deleted, the parent of any
775                  * node will always be a valid node. Also since we have
776                  * a Read lock on the data, it cannot be renamed out of the
777                  * scope while we are looking at it, and unless we're using
778                  * BDB_HIER, its parents cannot be moved either.
779                  */
780                 scopeok = 0;
781                 switch( op->ors_scope ) {
782                 case LDAP_SCOPE_BASE:
783                         /* This is always true, yes? */
784                         if ( id == base.e_id ) scopeok = 1;
785                         break;
786
787                 case LDAP_SCOPE_ONELEVEL:
788                         if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
789                         break;
790
791 #ifdef LDAP_SCOPE_CHILDREN
792                 case LDAP_SCOPE_CHILDREN:
793                         if ( id == base.e_id ) break;
794                         /* Fall-thru */
795 #endif
796                 case LDAP_SCOPE_SUBTREE: {
797                         EntryInfo *tmp;
798                         for ( tmp = BEI(e); tmp; tmp = tmp->bei_parent ) {
799                                 if ( tmp->bei_id == base.e_id ) {
800                                         scopeok = 1;
801                                         break;
802                                 }
803                         }
804                         } break;
805                 }
806
807                 /* aliases were already dereferenced in candidate list */
808                 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
809                         /* but if the search base is an alias, and we didn't
810                          * deref it when finding, return it.
811                          */
812                         if ( is_entry_alias(e) &&
813                                 ((op->ors_deref & LDAP_DEREF_FINDING) ||
814                                         !bvmatch(&e->e_nname, &op->o_req_ndn)))
815                         {
816                                 goto loop_continue;
817                         }
818
819                         /* scopes is only non-empty for onelevel or subtree */
820                         if ( !scopeok && BDB_IDL_N(scopes) ) {
821                                 unsigned x;
822                                 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) {
823                                         x = bdb_idl_search( scopes, e->e_id );
824                                         if ( scopes[x] == e->e_id ) scopeok = 1;
825                                 } else {
826                                         /* subtree, walk up the tree */
827                                         EntryInfo *tmp = BEI(e);
828                                         for (;tmp->bei_parent; tmp=tmp->bei_parent) {
829                                                 x = bdb_idl_search( scopes, tmp->bei_id );
830                                                 if ( scopes[x] == tmp->bei_id ) {
831                                                         scopeok = 1;
832                                                         break;
833                                                 }
834                                         }
835                                 }
836                         }
837                 }
838
839                 /* Not in scope, ignore it */
840                 if ( !scopeok )
841                 {
842                         Debug( LDAP_DEBUG_TRACE,
843                                 LDAP_XSTRING(bdb_search)
844                                 ": %ld scope not okay\n",
845                                 (long) id, 0, 0 );
846                         goto loop_continue;
847                 }
848
849                 /*
850                  * if it's a referral, add it to the list of referrals. only do
851                  * this for non-base searches, and don't check the filter
852                  * explicitly here since it's only a candidate anyway.
853                  */
854                 if ( !manageDSAit && op->oq_search.rs_scope != LDAP_SCOPE_BASE
855                         && is_entry_referral( e ) )
856                 {
857                         struct bdb_op_info bois;
858                         struct bdb_lock_info blis;
859                         BerVarray erefs = get_entry_referrals( op, e );
860                         rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
861                                 op->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
862                                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
863
864                         /* Must set lockinfo so that entry_release will work */
865                         if (!opinfo) {
866                                 bois.boi_oe.oe_key = bdb;
867                                 bois.boi_txn = NULL;
868                                 bois.boi_err = 0;
869                                 bois.boi_acl_cache = op->o_do_not_cache;
870                                 bois.boi_flag = BOI_DONTFREE;
871                                 bois.boi_locks = &blis;
872                                 blis.bli_next = NULL;
873                                 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &bois.boi_oe,
874                                         oe_next );
875                         } else {
876                                 blis.bli_next = opinfo->boi_locks;
877                                 opinfo->boi_locks = &blis;
878                         }
879                         blis.bli_id = e->e_id;
880                         blis.bli_lock = lock;
881                         blis.bli_flag = BLI_DONTFREE;
882
883                         rs->sr_flags = REP_ENTRY_MUSTRELEASE;
884
885                         send_search_reference( op, rs );
886
887                         if ( blis.bli_flag ) {
888 #ifdef SLAP_ZONE_ALLOC
889                                 slap_zn_runlock(bdb->bi_cache.c_zctx, e);
890 #endif
891                                 bdb_cache_return_entry_r(bdb, e, &lock);
892                                 if ( opinfo ) {
893                                         opinfo->boi_locks = blis.bli_next;
894                                 } else {
895                                         LDAP_SLIST_REMOVE( &op->o_extra, &bois.boi_oe,
896                                                 OpExtra, oe_next );
897                                 }
898                         }
899                         rs->sr_entry = NULL;
900                         e = NULL;
901
902                         ber_bvarray_free( rs->sr_ref );
903                         ber_bvarray_free( erefs );
904                         rs->sr_ref = NULL;
905
906                         goto loop_continue;
907                 }
908
909                 if ( !manageDSAit && is_entry_glue( e )) {
910                         goto loop_continue;
911                 }
912
913                 /* if it matches the filter and scope, send it */
914                 rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
915
916                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
917                         /* check size limit */
918                         if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
919                                 if ( rs->sr_nentries >= ((PagedResultsState *)op->o_pagedresults_state)->ps_size ) {
920 #ifdef SLAP_ZONE_ALLOC
921                                         slap_zn_runlock(bdb->bi_cache.c_zctx, e);
922 #endif
923                                         bdb_cache_return_entry_r( bdb, e, &lock );
924                                         e = NULL;
925                                         send_paged_response( op, rs, &lastid, tentries );
926                                         goto done;
927                                 }
928                                 lastid = id;
929                         }
930
931                         if (e) {
932                                 struct bdb_op_info bois;
933                                 struct bdb_lock_info blis;
934
935                                 /* Must set lockinfo so that entry_release will work */
936                                 if (!opinfo) {
937                                         bois.boi_oe.oe_key = bdb;
938                                         bois.boi_txn = NULL;
939                                         bois.boi_err = 0;
940                                         bois.boi_acl_cache = op->o_do_not_cache;
941                                         bois.boi_flag = BOI_DONTFREE;
942                                         bois.boi_locks = &blis;
943                                         blis.bli_next = NULL;
944                                         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &bois.boi_oe,
945                                                 oe_next );
946                                 } else {
947                                         blis.bli_next = opinfo->boi_locks;
948                                         opinfo->boi_locks = &blis;
949                                 }
950                                 blis.bli_id = e->e_id;
951                                 blis.bli_lock = lock;
952                                 blis.bli_flag = BLI_DONTFREE;
953
954                                 /* safe default */
955                                 rs->sr_attrs = op->oq_search.rs_attrs;
956                                 rs->sr_operational_attrs = NULL;
957                                 rs->sr_ctrls = NULL;
958                                 rs->sr_flags = REP_ENTRY_MUSTRELEASE;
959                                 rs->sr_err = LDAP_SUCCESS;
960                                 rs->sr_err = send_search_entry( op, rs );
961
962                                 /* send_search_entry will usually free it.
963                                  * an overlay might leave its own copy here;
964                                  * bli_flag will be 0 if lock was already released.
965                                  */
966                                 if ( blis.bli_flag ) {
967 #ifdef SLAP_ZONE_ALLOC
968                                         slap_zn_runlock(bdb->bi_cache.c_zctx, e);
969 #endif
970                                         bdb_cache_return_entry_r(bdb, e, &lock);
971                                         if ( opinfo ) {
972                                                 opinfo->boi_locks = blis.bli_next;
973                                         } else {
974                                                 LDAP_SLIST_REMOVE( &op->o_extra, &bois.boi_oe,
975                                                         OpExtra, oe_next );
976                                         }
977                                 }
978                                 rs->sr_entry = NULL;
979                                 e = NULL;
980
981                                 switch ( rs->sr_err ) {
982                                 case LDAP_SUCCESS:      /* entry sent ok */
983                                         break;
984                                 default:                /* entry not sent */
985                                         break;
986                                 case LDAP_UNAVAILABLE:
987                                 case LDAP_SIZELIMIT_EXCEEDED:
988                                         if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
989                                                 rs->sr_ref = rs->sr_v2ref;
990                                                 send_ldap_result( op, rs );
991                                                 rs->sr_err = LDAP_SUCCESS;
992
993                                         } else {
994                                                 rs->sr_err = LDAP_OTHER;
995                                         }
996                                         goto done;
997                                 }
998                         }
999
1000                 } else {
1001                         Debug( LDAP_DEBUG_TRACE,
1002                                 LDAP_XSTRING(bdb_search)
1003                                 ": %ld does not match filter\n",
1004                                 (long) id, 0, 0 );
1005                 }
1006
1007 loop_continue:
1008                 if( e != NULL ) {
1009                         /* free reader lock */
1010 #ifdef SLAP_ZONE_ALLOC
1011                         slap_zn_runlock(bdb->bi_cache.c_zctx, e);
1012 #endif
1013                         bdb_cache_return_entry_r( bdb, e , &lock );
1014                         e = NULL;
1015                         rs->sr_entry = NULL;
1016                 }
1017         }
1018
1019 nochange:
1020         rs->sr_ctrls = NULL;
1021         rs->sr_ref = rs->sr_v2ref;
1022         rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1023         rs->sr_rspoid = NULL;
1024         if ( get_pagedresults(op) > SLAP_CONTROL_IGNORED ) {
1025                 send_paged_response( op, rs, NULL, 0 );
1026         } else {
1027                 send_ldap_result( op, rs );
1028         }
1029
1030         rs->sr_err = LDAP_SUCCESS;
1031
1032 done:
1033         if( rs->sr_v2ref ) {
1034                 ber_bvarray_free( rs->sr_v2ref );
1035                 rs->sr_v2ref = NULL;
1036         }
1037         if( realbase.bv_val ) ch_free( realbase.bv_val );
1038
1039         return rs->sr_err;
1040 }
1041
1042
1043 static int base_candidate(
1044         BackendDB       *be,
1045         Entry   *e,
1046         ID              *ids )
1047 {
1048         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1049                 e->e_nname.bv_val, (long) e->e_id, 0);
1050
1051         ids[0] = 1;
1052         ids[1] = e->e_id;
1053         return 0;
1054 }
1055
1056 /* Look for "objectClass Present" in this filter.
1057  * Also count depth of filter tree while we're at it.
1058  */
1059 static int oc_filter(
1060         Filter *f,
1061         int cur,
1062         int *max )
1063 {
1064         int rc = 0;
1065
1066         assert( f != NULL );
1067
1068         if( cur > *max ) *max = cur;
1069
1070         switch( f->f_choice ) {
1071         case LDAP_FILTER_PRESENT:
1072                 if (f->f_desc == slap_schema.si_ad_objectClass) {
1073                         rc = 1;
1074                 }
1075                 break;
1076
1077         case LDAP_FILTER_AND:
1078         case LDAP_FILTER_OR:
1079                 cur++;
1080                 for ( f=f->f_and; f; f=f->f_next ) {
1081                         (void) oc_filter(f, cur, max);
1082                 }
1083                 break;
1084
1085         default:
1086                 break;
1087         }
1088         return rc;
1089 }
1090
1091 static void search_stack_free( void *key, void *data )
1092 {
1093         ber_memfree_x(data, NULL);
1094 }
1095
1096 static void *search_stack( Operation *op )
1097 {
1098         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1099         void *ret = NULL;
1100
1101         if ( op->o_threadctx ) {
1102                 ldap_pvt_thread_pool_getkey( op->o_threadctx, (void *)search_stack,
1103                         &ret, NULL );
1104         } else {
1105                 ret = bdb->bi_search_stack;
1106         }
1107
1108         if ( !ret ) {
1109                 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1110                         * sizeof( ID ) );
1111                 if ( op->o_threadctx ) {
1112                         ldap_pvt_thread_pool_setkey( op->o_threadctx, (void *)search_stack,
1113                                 ret, search_stack_free, NULL, NULL );
1114                 } else {
1115                         bdb->bi_search_stack = ret;
1116                 }
1117         }
1118         return ret;
1119 }
1120
1121 static int search_candidates(
1122         Operation *op,
1123         SlapReply *rs,
1124         Entry *e,
1125         DB_TXN *txn,
1126         ID      *ids,
1127         ID      *scopes )
1128 {
1129         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1130         int rc, depth = 1;
1131         Filter          f, rf, xf, nf;
1132         ID              *stack;
1133         AttributeAssertion aa_ref = ATTRIBUTEASSERTION_INIT;
1134         Filter  sf;
1135         AttributeAssertion aa_subentry = ATTRIBUTEASSERTION_INIT;
1136
1137         /*
1138          * This routine takes as input a filter (user-filter)
1139          * and rewrites it as follows:
1140          *      (&(scope=DN)[(objectClass=subentry)]
1141          *              (|[(objectClass=referral)(objectClass=alias)](user-filter))
1142          */
1143
1144         Debug(LDAP_DEBUG_TRACE,
1145                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1146                 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1147
1148         xf.f_or = op->oq_search.rs_filter;
1149         xf.f_choice = LDAP_FILTER_OR;
1150         xf.f_next = NULL;
1151
1152         /* If the user's filter uses objectClass=*,
1153          * these clauses are redundant.
1154          */
1155         if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1156                 && !get_subentries_visibility(op)) {
1157                 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1158                         /* match referral objects */
1159                         struct berval bv_ref = BER_BVC( "referral" );
1160                         rf.f_choice = LDAP_FILTER_EQUALITY;
1161                         rf.f_ava = &aa_ref;
1162                         rf.f_av_desc = slap_schema.si_ad_objectClass;
1163                         rf.f_av_value = bv_ref;
1164                         rf.f_next = xf.f_or;
1165                         xf.f_or = &rf;
1166                         depth++;
1167                 }
1168         }
1169
1170         f.f_next = NULL;
1171         f.f_choice = LDAP_FILTER_AND;
1172         f.f_and = &nf;
1173         /* Dummy; we compute scope separately now */
1174         nf.f_choice = SLAPD_FILTER_COMPUTED;
1175         nf.f_result = LDAP_SUCCESS;
1176         nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1177                 ? op->oq_search.rs_filter : &xf ;
1178         /* Filter depth increased again, adding dummy clause */
1179         depth++;
1180
1181         if( get_subentries_visibility( op ) ) {
1182                 struct berval bv_subentry = BER_BVC( "subentry" );
1183                 sf.f_choice = LDAP_FILTER_EQUALITY;
1184                 sf.f_ava = &aa_subentry;
1185                 sf.f_av_desc = slap_schema.si_ad_objectClass;
1186                 sf.f_av_value = bv_subentry;
1187                 sf.f_next = nf.f_next;
1188                 nf.f_next = &sf;
1189         }
1190
1191         /* Allocate IDL stack, plus 1 more for former tmp */
1192         if ( depth+1 > bdb->bi_search_stack_depth ) {
1193                 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1194         } else {
1195                 stack = search_stack( op );
1196         }
1197
1198         if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1199                 rc = search_aliases( op, rs, e, txn, ids, scopes, stack );
1200         } else {
1201                 rc = bdb_dn2idl( op, txn, &e->e_nname, BEI(e), ids, stack );
1202         }
1203
1204         if ( rc == LDAP_SUCCESS ) {
1205                 rc = bdb_filter_candidates( op, txn, &f, ids,
1206                         stack, stack+BDB_IDL_UM_SIZE );
1207         }
1208
1209         if ( depth+1 > bdb->bi_search_stack_depth ) {
1210                 ch_free( stack );
1211         }
1212
1213         if( rc ) {
1214                 Debug(LDAP_DEBUG_TRACE,
1215                         "bdb_search_candidates: failed (rc=%d)\n",
1216                         rc, NULL, NULL );
1217
1218         } else {
1219                 Debug(LDAP_DEBUG_TRACE,
1220                         "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1221                         (long) ids[0],
1222                         (long) BDB_IDL_FIRST(ids),
1223                         (long) BDB_IDL_LAST(ids) );
1224         }
1225
1226         return rc;
1227 }
1228
1229 static int
1230 parse_paged_cookie( Operation *op, SlapReply *rs )
1231 {
1232         int             rc = LDAP_SUCCESS;
1233         PagedResultsState *ps = op->o_pagedresults_state;
1234
1235         /* this function must be invoked only if the pagedResults
1236          * control has been detected, parsed and partially checked
1237          * by the frontend */
1238         assert( get_pagedresults( op ) > SLAP_CONTROL_IGNORED );
1239
1240         /* cookie decoding/checks deferred to backend... */
1241         if ( ps->ps_cookieval.bv_len ) {
1242                 PagedResultsCookie reqcookie;
1243                 if( ps->ps_cookieval.bv_len != sizeof( reqcookie ) ) {
1244                         /* bad cookie */
1245                         rs->sr_text = "paged results cookie is invalid";
1246                         rc = LDAP_PROTOCOL_ERROR;
1247                         goto done;
1248                 }
1249
1250                 AC_MEMCPY( &reqcookie, ps->ps_cookieval.bv_val, sizeof( reqcookie ));
1251
1252                 if ( reqcookie > ps->ps_cookie ) {
1253                         /* bad cookie */
1254                         rs->sr_text = "paged results cookie is invalid";
1255                         rc = LDAP_PROTOCOL_ERROR;
1256                         goto done;
1257
1258                 } else if ( reqcookie < ps->ps_cookie ) {
1259                         rs->sr_text = "paged results cookie is invalid or old";
1260                         rc = LDAP_UNWILLING_TO_PERFORM;
1261                         goto done;
1262                 }
1263
1264         }
1265
1266 done:;
1267
1268         return rc;
1269 }
1270
1271 static void
1272 send_paged_response( 
1273         Operation       *op,
1274         SlapReply       *rs,
1275         ID              *lastid,
1276         int             tentries )
1277 {
1278         LDAPControl     *ctrls[2];
1279         BerElementBuffer berbuf;
1280         BerElement      *ber = (BerElement *)&berbuf;
1281         PagedResultsCookie respcookie;
1282         struct berval cookie;
1283
1284         Debug(LDAP_DEBUG_ARGS,
1285                 "send_paged_response: lastid=0x%08lx nentries=%d\n", 
1286                 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1287
1288         ctrls[1] = NULL;
1289
1290         ber_init2( ber, NULL, LBER_USE_DER );
1291
1292         if ( lastid ) {
1293                 respcookie = ( PagedResultsCookie )(*lastid);
1294                 cookie.bv_len = sizeof( respcookie );
1295                 cookie.bv_val = (char *)&respcookie;
1296
1297         } else {
1298                 respcookie = ( PagedResultsCookie )0;
1299                 BER_BVSTR( &cookie, "" );
1300         }
1301
1302         op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1303         op->o_conn->c_pagedresults_state.ps_count =
1304                 ((PagedResultsState *)op->o_pagedresults_state)->ps_count +
1305                 rs->sr_nentries;
1306
1307         /* return size of 0 -- no estimate */
1308         ber_printf( ber, "{iO}", 0, &cookie ); 
1309
1310         ctrls[0] = op->o_tmpalloc( sizeof(LDAPControl), op->o_tmpmemctx );
1311         if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1312                 goto done;
1313         }
1314
1315         ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1316         ctrls[0]->ldctl_iscritical = 0;
1317
1318         slap_add_ctrls( op, rs, ctrls );
1319         rs->sr_err = LDAP_SUCCESS;
1320         send_ldap_result( op, rs );
1321
1322 done:
1323         (void) ber_free_buf( ber );
1324 }
1325