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