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