]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/search.c
add paged results support to back-ldap
[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-2004 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 #include "external.h"
25
26 static int base_candidate(
27         BackendDB       *be,
28         Entry   *e,
29         ID              *ids );
30
31 static int search_candidates(
32         Operation *stackop,     /* op with the current threadctx/slab cache */
33         Operation *sop,         /* search op */
34         SlapReply *rs,
35         Entry *e,
36         u_int32_t locker,
37         ID      *ids,
38         ID      *scopes );
39
40 static int parse_paged_cookie( Operation *op, SlapReply *rs );
41
42 static void send_paged_response( 
43         Operation *op,
44         SlapReply *rs,
45         ID  *lastid,
46         int tentries );
47
48 /* Dereference aliases for a single alias entry. Return the final
49  * dereferenced entry on success, NULL on any failure.
50  */
51 static Entry * deref_base (
52         Operation *op,
53         SlapReply *rs,
54         Entry *e,
55         Entry **matched,
56         u_int32_t locker,
57         DB_LOCK *lock,
58         ID      *tmp,
59         ID      *visited )
60 {
61         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
62         struct berval ndn;
63         EntryInfo *ei;
64         DB_LOCK lockr;
65
66         rs->sr_err = LDAP_ALIAS_DEREF_PROBLEM;
67         rs->sr_text = "maximum deref depth exceeded";
68
69         while (BDB_IDL_N(tmp) < op->o_bd->be_max_deref_depth) {
70                 /* Remember the last entry we looked at, so we can
71                  * report broken links
72                  */
73                 *matched = e;
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 && bdb_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 ( bdb_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 = bdb_dn2entry( op, NULL, &ndn, &ei,
102                         0, locker, &lockr );
103
104                 if ( ei ) {
105                         e = ei->bei_e;
106                 } else {
107                         e = NULL;
108                 }
109
110                 if (!e) {
111                         rs->sr_err = LDAP_ALIAS_PROBLEM;
112                         rs->sr_text = "aliasedObject not found";
113                         break;
114                 }
115
116                 /* Free the previous entry, continue to work with the
117                  * one we just retrieved.
118                  */
119                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
120                         *matched, lock);
121                 *lock = lockr;
122
123                 /* We found a regular entry. Return this to the caller. The
124                  * entry is still locked for Read.
125                  */
126                 if (!is_entry_alias(e)) {
127                         rs->sr_err = LDAP_SUCCESS;
128                         rs->sr_text = NULL;
129                         break;
130                 }
131         }
132         return e;
133 }
134
135 /* Look for and dereference all aliases within the search scope. Adds
136  * the dereferenced entries to the "ids" list. Requires "stack" to be
137  * able to hold 8 levels of DB_SIZE IDLs. Of course we're hardcoded to
138  * require a minimum of 8 UM_SIZE IDLs so this is never a problem.
139  */
140 static int search_aliases(
141         Operation *op,
142         SlapReply *rs,
143         Entry *e,
144         u_int32_t locker,
145         ID *ids,
146         ID *scopes,
147         ID *stack )
148 {
149         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
150         ID *aliases, *curscop, *subscop, *visited, *newsubs, *oldsubs, *tmp;
151         ID cursora, ida, cursoro, ido, *subscop2;
152         Entry *matched, *a;
153         EntryInfo *ei;
154         struct berval bv_alias = BER_BVC( "alias" );
155         AttributeAssertion aa_alias;
156         Filter  af;
157         DB_LOCK locka, lockr;
158         int first = 1;
159
160         aliases = stack;        /* IDL of all aliases in the database */
161         curscop = aliases + BDB_IDL_DB_SIZE;    /* Aliases in the current scope */
162         subscop = curscop + BDB_IDL_DB_SIZE;    /* The current scope */
163         visited = subscop + BDB_IDL_DB_SIZE;    /* IDs we've seen in this search */
164         newsubs = visited + BDB_IDL_DB_SIZE;    /* New subtrees we've added */
165         oldsubs = newsubs + BDB_IDL_DB_SIZE;    /* Subtrees added previously */
166         tmp = oldsubs + BDB_IDL_DB_SIZE;        /* Scratch space for deref_base() */
167
168         /* A copy of subscop, because subscop gets clobbered by
169          * the bdb_idl_union/intersection routines
170          */
171         subscop2 = tmp + BDB_IDL_DB_SIZE;
172
173         af.f_choice = LDAP_FILTER_EQUALITY;
174         af.f_ava = &aa_alias;
175         af.f_av_desc = slap_schema.si_ad_objectClass;
176         af.f_av_value = bv_alias;
177         af.f_next = NULL;
178
179         /* Find all aliases in database */
180         BDB_IDL_ZERO( aliases );
181         rs->sr_err = bdb_filter_candidates( op, &af, aliases,
182                 curscop, visited );
183         if (rs->sr_err != LDAP_SUCCESS) {
184                 return rs->sr_err;
185         }
186         oldsubs[0] = 1;
187         oldsubs[1] = e->e_id;
188
189         BDB_IDL_ZERO( ids );
190         BDB_IDL_ZERO( visited );
191         BDB_IDL_ZERO( newsubs );
192
193         cursoro = 0;
194         ido = bdb_idl_first( oldsubs, &cursoro );
195
196         for (;;) {
197                 /* Set curscop to only the aliases in the current scope. Start with
198                  * all the aliases, obtain the IDL for the current scope, and then
199                  * get the intersection of these two IDLs. Add the current scope
200                  * to the cumulative list of candidates.
201                  */
202                 BDB_IDL_CPY( curscop, aliases );
203                 rs->sr_err = bdb_dn2idl( op, e, subscop,
204                         subscop2+BDB_IDL_DB_SIZE );
205                 if (first) {
206                         first = 0;
207                 } else {
208                         bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache, e, &locka);
209                 }
210                 BDB_IDL_CPY(subscop2, subscop);
211                 rs->sr_err = bdb_idl_intersection(curscop, subscop);
212                 bdb_idl_union( ids, subscop2 );
213
214                 /* Dereference all of the aliases in the current scope. */
215                 cursora = 0;
216                 for (ida = bdb_idl_first(curscop, &cursora); ida != NOID;
217                         ida = bdb_idl_next(curscop, &cursora))
218                 {
219                         ei = NULL;
220 retry1:
221                         rs->sr_err = bdb_cache_find_id(op, NULL,
222                                 ida, &ei, 0, locker, &lockr );
223                         if (rs->sr_err != LDAP_SUCCESS) {
224                                 if ( rs->sr_err == DB_LOCK_DEADLOCK ||
225                                         rs->sr_err == DB_LOCK_NOTGRANTED ) goto retry1;
226                                 continue;
227                         }
228                         a = ei->bei_e;
229
230                         /* This should only happen if the curscop IDL has maxed out and
231                          * turned into a range that spans IDs indiscriminately
232                          */
233                         if (!is_entry_alias(a)) {
234                                 bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
235                                         a, &lockr);
236                                 continue;
237                         }
238
239                         /* Actually dereference the alias */
240                         BDB_IDL_ZERO(tmp);
241                         a = deref_base( op, rs, a, &matched, locker, &lockr,
242                                 tmp, visited );
243                         if (a) {
244                                 /* If the target was not already in our current candidates,
245                                  * make note of it in the newsubs list. Also
246                                  * set it in the scopes list so that bdb_search
247                                  * can check it.
248                                  */
249                                 if (bdb_idl_insert(ids, a->e_id) == 0) {
250                                         bdb_idl_insert(newsubs, a->e_id);
251                                         bdb_idl_insert(scopes, a->e_id);
252                                 }
253                                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
254                                         a, &lockr);
255
256                         } else if (matched) {
257                                 /* Alias could not be dereferenced, or it deref'd to
258                                  * an ID we've already seen. Ignore it.
259                                  */
260                                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache,
261                                         matched, &lockr );
262                                 rs->sr_text = NULL;
263                         }
264                 }
265                 /* If this is a OneLevel search, we're done; oldsubs only had one
266                  * ID in it. For a Subtree search, oldsubs may be a list of scope IDs.
267                  */
268                 if ( op->ors_scope == LDAP_SCOPE_ONELEVEL ) break;
269 nextido:
270                 ido = bdb_idl_next( oldsubs, &cursoro );
271                 
272                 /* If we're done processing the old scopes, did we add any new
273                  * scopes in this iteration? If so, go back and do those now.
274                  */
275                 if (ido == NOID) {
276                         if (BDB_IDL_IS_ZERO(newsubs)) break;
277                         BDB_IDL_CPY(oldsubs, newsubs);
278                         BDB_IDL_ZERO(newsubs);
279                         cursoro = 0;
280                         ido = bdb_idl_first( oldsubs, &cursoro );
281                 }
282
283                 /* Find the entry corresponding to the next scope. If it can't
284                  * be found, ignore it and move on. This should never happen;
285                  * we should never see the ID of an entry that doesn't exist.
286                  * Set the name so that the scope's IDL can be retrieved.
287                  */
288                 ei = NULL;
289 sameido:
290                 rs->sr_err = bdb_cache_find_id(op, NULL, ido, &ei,
291                         0, locker, &locka );
292                 if ( rs->sr_err != LDAP_SUCCESS ) {
293                         if ( rs->sr_err == DB_LOCK_DEADLOCK ||
294                                 rs->sr_err == DB_LOCK_NOTGRANTED )
295                                 goto sameido;
296                         goto nextido;
297                 }
298                 e = ei->bei_e;
299         }
300         return rs->sr_err;
301 }
302
303 #define is_sync_protocol(op)    \
304         ((op)->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST)
305
306 #define IS_BDB_REPLACE(type) (( type == LDAP_PSEARCH_BY_DELETE ) || \
307         ( type == LDAP_PSEARCH_BY_SCOPEOUT ))
308 #define IS_PSEARCH (op != sop)
309
310 static Operation *
311 bdb_drop_psearch( Operation *op, ber_int_t msgid )
312 {
313         Operation       *ps_list;
314         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
315
316         LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
317                 if ( ps_list->o_connid == op->o_connid ) {
318                         if ( ps_list->o_msgid == msgid ) {
319                                 ps_list->o_abandon = 1;
320                                 LDAP_LIST_REMOVE( ps_list, o_ps_link );
321                                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
322                                 LDAP_STAILQ_REMOVE( &op->o_conn->c_ops, ps_list,
323                                         slap_op, o_next );
324                                 LDAP_STAILQ_NEXT( ps_list, o_next ) = NULL;
325                                 op->o_conn->c_n_ops_executing--;
326                                 op->o_conn->c_n_ops_completed++;
327                                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
328                                 return ps_list;
329                         }
330                 }
331         }
332
333         return NULL;
334 }
335
336 int
337 bdb_abandon( Operation *op, SlapReply *rs )
338 {
339         Operation       *ps;
340
341         ps = bdb_drop_psearch( op, op->oq_abandon.rs_msgid );
342         if ( ps ) {
343                 if ( ps->o_tmpmemctx ) {
344                         slap_sl_mem_destroy( NULL, ps->o_tmpmemctx );
345                 }
346                 slap_op_free ( ps );
347                 return LDAP_SUCCESS;
348         }
349         return LDAP_UNAVAILABLE;
350 }
351
352 int
353 bdb_cancel( Operation *op, SlapReply *rs )
354 {
355         Operation       *ps;
356
357         ps = bdb_drop_psearch( op, op->oq_cancel.rs_msgid );
358         if ( ps ) {
359                 rs->sr_err = LDAP_CANCELLED;
360                 send_ldap_result( ps, rs );
361                 if ( ps->o_tmpmemctx ) {
362                         slap_sl_mem_destroy( NULL, ps->o_tmpmemctx );
363                 }
364                 slap_op_free ( ps );
365                 return LDAP_SUCCESS;
366         }
367         return LDAP_UNAVAILABLE;
368 }
369
370 int bdb_search( Operation *op, SlapReply *rs )
371 {
372         return bdb_do_search( op, rs, op, NULL, 0 );
373 }
374
375 int bdb_psearch( Operation *op, SlapReply *rs, Operation *sop,
376         Entry *ps_e, int ps_type )
377 {
378         int     rc;
379
380         sop->o_private = op->o_private;
381         rc = bdb_do_search( op, rs, sop, ps_e, ps_type );
382         sop->o_private = NULL;
383
384         return rc;
385 }
386
387 /* For persistent searches, op is the currently executing operation,
388  * sop is the persistent search. For regular searches, sop = op.
389  */
390 int
391 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
392         Entry *ps_e, int ps_type )
393 {
394         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
395         time_t          stoptime;
396         ID              id, cursor;
397         ID              candidates[BDB_IDL_UM_SIZE];
398         ID              scopes[BDB_IDL_DB_SIZE];
399         Entry           *e = NULL, base, e_root = {0};
400         Entry           *matched = NULL;
401         EntryInfo       *ei, ei_root = {0};
402         struct berval   realbase = BER_BVNULL;
403         int             manageDSAit;
404         int             tentries = 0;
405         ID              lastid = NOID;
406         AttributeName   *attrs;
407
408         Filter          contextcsnand, contextcsnle, cookief, csnfnot,
409                         csnfeq, csnfand, csnfge;
410         AttributeAssertion aa_ge, aa_eq, aa_le;
411         struct berval   *search_context_csn = NULL;
412         DB_LOCK         ctxcsn_lock;
413         LDAPControl     *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
414         int             num_ctrls = 0;
415         AttributeName   uuid_attr[2];
416         int             rc_sync = 0;
417         int             entry_sync_state = -1;
418         AttributeName   null_attr;
419         int             no_sync_state_change = 0;
420
421         u_int32_t       locker = 0;
422         DB_LOCK         lock;
423
424         Operation       *ps_list;
425         int                     sync_send_present_mode = 1;
426         int                     match;
427         MatchingRule *mr;
428         const char *text;
429         int                     slog_found = 0;
430
431         BerVarray       syncUUID_set = NULL;
432         int                     syncUUID_set_cnt = 0;
433
434         struct  bdb_op_info     *opinfo = NULL;
435         DB_TXN                  *ltid = NULL;
436
437 #ifdef NEW_LOGGING
438         LDAP_LOG( OPERATION, ENTRY, "bdb_search\n", 0, 0, 0 );
439 #else
440         Debug( LDAP_DEBUG_TRACE, "=> bdb_search\n", 0, 0, 0);
441 #endif
442         attrs = sop->oq_search.rs_attrs;
443
444         opinfo = (struct bdb_op_info *) op->o_private;
445
446         if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
447                 struct slap_session_entry *sent;
448                 if ( sop->o_sync_state.sid >= 0 ) {
449                         LDAP_LIST_FOREACH( sent, &bdb->bi_session_list, se_link ) {
450                                 if ( sent->se_id == sop->o_sync_state.sid ) {
451                                         sop->o_sync_slog_size = sent->se_size;
452                                         break;
453                                 }
454                         }
455                 }
456         }
457
458         /* psearch needs to be registered before refresh begins */
459         /* psearch and refresh transmission is serialized in send_ldap_ber() */
460         if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
461                 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
462                 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
463                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
464
465         } else if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST
466                 && sop->o_sync_slog_size >= 0 )
467         {
468                 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
469                 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
470                         if ( ps_list->o_sync_slog_size >= 0 ) {
471                                 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
472                                         slog_found = 1;
473                                         break;
474                                 }
475                         }
476                 }
477
478                 if ( slog_found ) {
479                         if ( ps_list->o_sync_slog_omitcsn.bv_len != 0 ) {
480                                 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
481                                 if ( sop->o_sync_state.ctxcsn &&
482                                         sop->o_sync_state.ctxcsn->bv_val != NULL )
483                                 {
484                                         value_match( &match, slap_schema.si_ad_entryCSN, mr,
485                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
486                                                 sop->o_sync_state.ctxcsn,
487                                                 &ps_list->o_sync_slog_omitcsn,
488                                                 &text );
489                                 } else {
490                                         match = -1;
491                                 }
492                                 if ( match >= 0 ) {
493                                         sync_send_present_mode = 0;
494                                 }
495                         } else {
496                                 sync_send_present_mode = 0;
497                         }
498                 } else if ( sop->o_sync_slog_size >= 0 ) {
499                         LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
500                 } else {
501                         sop->o_sync_state.sid = -1;
502                 }
503                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
504         }
505
506         null_attr.an_desc = NULL;
507         null_attr.an_oc = NULL;
508         null_attr.an_oc_exclude = 0;
509         BER_BVZERO( &null_attr.an_name );
510
511         for( num_ctrls = 0; num_ctrls < SLAP_MAX_RESPONSE_CONTROLS; num_ctrls++ ) {
512                 ctrls[num_ctrls] = NULL;
513         }
514         num_ctrls = 0;
515
516         if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
517                 attrs = uuid_attr;
518                 attrs[0].an_desc = NULL;
519                 attrs[0].an_oc = NULL;
520                 attrs[0].an_oc_exclude = 0;
521                 BER_BVZERO( &attrs[0].an_name );
522         }
523
524         manageDSAit = get_manageDSAit( sop );
525
526         /* Sync control overrides manageDSAit */
527         if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
528                 if ( manageDSAit == SLAP_NO_CONTROL ) {
529                         manageDSAit = SLAP_CRITICAL_CONTROL;
530                 }
531         } else if ( IS_PSEARCH ) {
532                 if ( manageDSAit == SLAP_NO_CONTROL ) {
533                         manageDSAit = SLAP_CRITICAL_CONTROL;
534                 }
535         }
536
537         if ( opinfo ) {
538                 ltid = opinfo->boi_txn;
539                 locker = TXN_ID( ltid );
540         } else {
541                 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
542
543                 switch(rs->sr_err) {
544                 case 0:
545                         break;
546                 default:
547                         send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
548                         return rs->sr_err;
549                 }
550         }
551
552         if ( sop->o_req_ndn.bv_len == 0 ) {
553                 /* DIT root special case */
554                 ei_root.bei_e = &e_root;
555                 ei_root.bei_parent = &ei_root;
556                 e_root.e_private = &ei_root;
557                 e_root.e_id = 0;
558                 BER_BVSTR( &e_root.e_nname, "" );
559                 BER_BVSTR( &e_root.e_name, "" );
560                 ei = &ei_root;
561                 rs->sr_err = LDAP_SUCCESS;
562         } else {
563 dn2entry_retry:
564                 /* get entry with reader lock */
565                 rs->sr_err = bdb_dn2entry( op, ltid, &sop->o_req_ndn, &ei,
566                         1, locker, &lock );
567         }
568
569         switch(rs->sr_err) {
570         case DB_NOTFOUND:
571                 matched = ei->bei_e;
572                 break;
573         case 0:
574                 e = ei->bei_e;
575                 break;
576         case LDAP_BUSY:
577                 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
578                 if ( !opinfo )
579                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
580                 return LDAP_BUSY;
581         case DB_LOCK_DEADLOCK:
582         case DB_LOCK_NOTGRANTED:
583                 goto dn2entry_retry;
584         default:
585                 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
586                 if ( !opinfo )
587                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
588                 return rs->sr_err;
589         }
590
591         if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
592                 BDB_IDL_ZERO(candidates);
593                 e = deref_base( op, rs, e, &matched, locker, &lock,
594                         candidates, NULL );
595         }
596
597         if ( e == NULL ) {
598                 struct berval matched_dn = BER_BVNULL;
599
600                 if ( matched != NULL ) {
601                         BerVarray erefs;
602                         ber_dupbv( &matched_dn, &matched->e_name );
603
604                         erefs = is_entry_referral( matched )
605                                 ? get_entry_referrals( op, matched )
606                                 : NULL;
607
608                         bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
609                                 matched, &lock);
610                         matched = NULL;
611
612                         if( erefs ) {
613                                 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
614                                         &sop->o_req_dn, sop->oq_search.rs_scope );
615                                 ber_bvarray_free( erefs );
616                         }
617
618                 } else {
619                         rs->sr_ref = referral_rewrite( default_referral,
620                                 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
621                 }
622
623                 rs->sr_err = LDAP_REFERRAL;
624                 rs->sr_matched = matched_dn.bv_val;
625                 send_ldap_result( sop, rs );
626
627                 if ( !opinfo )
628                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
629                 if ( rs->sr_ref ) {
630                         ber_bvarray_free( rs->sr_ref );
631                         rs->sr_ref = NULL;
632                 }
633                 if ( matched_dn.bv_val ) {
634                         ber_memfree( matched_dn.bv_val );
635                         rs->sr_matched = NULL;
636                 }
637                 return rs->sr_err;
638         }
639
640         if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
641                 /* entry is a referral, don't allow add */
642                 struct berval matched_dn;
643                 BerVarray erefs;
644                 
645                 ber_dupbv( &matched_dn, &e->e_name );
646                 erefs = get_entry_referrals( op, e );
647
648                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
649                 e = NULL;
650
651                 if( erefs ) {
652                         rs->sr_ref = referral_rewrite( erefs, &matched_dn,
653                                 &sop->o_req_dn, sop->oq_search.rs_scope );
654                         ber_bvarray_free( erefs );
655                 }
656
657 #ifdef NEW_LOGGING
658                 LDAP_LOG ( OPERATION, RESULTS, 
659                         "bdb_search: entry is referral\n", 0, 0, 0 );
660 #else
661                 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
662                         0, 0, 0 );
663 #endif
664
665                 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
666                 rs->sr_err = LDAP_REFERRAL;
667                 rs->sr_matched = matched_dn.bv_val;
668                 send_ldap_result( sop, rs );
669
670                 if ( !opinfo )
671                         LOCK_ID_FREE (bdb->bi_dbenv, locker );
672                 ber_bvarray_free( rs->sr_ref );
673                 rs->sr_ref = NULL;
674                 ber_memfree( matched_dn.bv_val );
675                 rs->sr_matched = NULL;
676                 return 1;
677         }
678
679         if ( get_assert( op ) &&
680                 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
681         {
682                 rs->sr_err = LDAP_ASSERTION_FAILED;
683                 send_ldap_result( sop, rs );
684                 return 1;
685         }
686
687         /* compute it anyway; root does not use it */
688         stoptime = op->o_time + sop->ors_tlimit;
689
690         /* need normalized dn below */
691         ber_dupbv( &realbase, &e->e_nname );
692
693         /* Copy info to base, must free entry before accessing the database
694          * in search_candidates, to avoid deadlocks.
695          */
696         base.e_private = e->e_private;
697         base.e_nname = realbase;
698         base.e_id = e->e_id;
699
700         if ( e != &e_root ) {
701                 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
702         }
703         e = NULL;
704
705         if ( !IS_PSEARCH ) {
706                 rs->sr_err = bdb_get_commit_csn( sop, rs, &search_context_csn,
707                         locker, &ctxcsn_lock );
708
709                 if ( rs->sr_err != LDAP_SUCCESS ) {
710                         send_ldap_error( sop, rs, rs->sr_err,
711                                 "error in csn management in search" );
712                         goto done;
713                 }
714
715                 if ( sop->o_sync_mode != SLAP_SYNC_NONE &&
716                         sop->o_sync_state.ctxcsn &&
717                         sop->o_sync_state.ctxcsn->bv_val &&
718                         ber_bvcmp( &sop->o_sync_state.ctxcsn[0], search_context_csn ) == 0 )
719                 {
720                         bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
721                         goto nochange;
722                 }
723         } else {
724                 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );        
725         }
726
727         /* select candidates */
728         if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
729                 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
730
731         } else {
732                 BDB_IDL_ZERO( candidates );
733                 BDB_IDL_ZERO( scopes );
734                 rs->sr_err = search_candidates( op, sop, rs, &base,
735                         locker, candidates, scopes );
736         }
737
738         if ( !IS_PSEARCH && sop->o_sync_mode != SLAP_SYNC_NONE ) {
739                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
740         }
741
742         /* start cursor at beginning of candidates.
743          */
744         cursor = 0;
745         if (IS_PSEARCH) {
746                 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
747                         cursor = bdb_idl_search( candidates, ps_e->e_id );
748                         if ( candidates[cursor] != ps_e->e_id ) {
749                                 rs->sr_err = LDAP_SUCCESS;
750                                 goto done;
751                         }
752                 } else if ( ps_e->e_id < BDB_IDL_RANGE_FIRST( candidates ) ||
753                         ps_e->e_id > BDB_IDL_RANGE_LAST( candidates ))
754                 {
755                         rs->sr_err = LDAP_SUCCESS;
756                         goto done;
757                 }
758                 candidates[0] = 1;
759                 candidates[1] = ps_e->e_id;
760         }
761
762         if ( candidates[0] == 0 ) {
763 #ifdef NEW_LOGGING
764                 LDAP_LOG ( OPERATION, RESULTS,
765                         "bdb_search: no candidates\n", 0, 0, 0 );
766 #else
767                 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
768                         0, 0, 0 );
769 #endif
770
771                 goto nochange;
772         }
773
774         /* if not root and candidates exceed to-be-checked entries, abort */
775         if ( sop->ors_limit     /* isroot == FALSE */ &&
776                 sop->ors_limit->lms_s_unchecked != -1 &&
777                 BDB_IDL_N(candidates) > (unsigned) sop->ors_limit->lms_s_unchecked )
778         {
779                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
780                 send_ldap_result( sop, rs );
781                 rs->sr_err = LDAP_SUCCESS;
782                 goto done;
783         }
784
785         if ( sop->ors_limit == NULL     /* isroot == TRUE */ ||
786                 !sop->ors_limit->lms_s_pr_hide )
787         {
788                 tentries = BDB_IDL_N(candidates);
789         }
790
791         if ( get_pagedresults( sop ) > SLAP_NO_CONTROL ) {
792                 /* deferred cookie parsing */
793                 rs->sr_err = parse_paged_cookie( sop, rs );
794                 if ( rs->sr_err != LDAP_SUCCESS ) {
795                         send_ldap_result( sop, rs );
796                         goto done;
797                 }
798
799                 if ( (ID)( sop->o_pagedresults_state.ps_cookie ) == 0 ) {
800                         id = bdb_idl_first( candidates, &cursor );
801
802                 } else {
803                         if ( sop->o_pagedresults_size == 0 ) {
804                                 rs->sr_err = LDAP_SUCCESS;
805                                 rs->sr_text = "search abandoned by pagedResult size=0";
806                                 send_ldap_result( sop, rs );
807                                 goto done;
808                         }
809                         for ( id = bdb_idl_first( candidates, &cursor );
810                                 id != NOID &&
811                                         id <= (ID)( sop->o_pagedresults_state.ps_cookie );
812                                 id = bdb_idl_next( candidates, &cursor ) )
813                         {
814                                 /* empty */;
815                         }
816                 }
817
818                 if ( cursor == NOID ) {
819 #ifdef NEW_LOGGING
820                         LDAP_LOG ( OPERATION, RESULTS, 
821                                 "bdb_search: no paged results candidates\n", 
822                                 0, 0, 0 );
823 #else
824                         Debug( LDAP_DEBUG_TRACE, 
825                                 "bdb_search: no paged results candidates\n",
826                                 0, 0, 0 );
827 #endif
828                         send_paged_response( sop, rs, &lastid, 0 );
829
830                         rs->sr_err = LDAP_OTHER;
831                         goto done;
832                 }
833                 goto loop_begin;
834         }
835
836         if (( sop->o_sync_mode & SLAP_SYNC_REFRESH ) || IS_PSEARCH ) {
837                 int match;
838
839                 cookief.f_choice = LDAP_FILTER_AND;
840                 cookief.f_and = &csnfnot;
841                 cookief.f_next = NULL;
842
843                 csnfnot.f_choice = LDAP_FILTER_NOT;
844                 csnfnot.f_not = &csnfeq;
845                 csnfnot.f_next = &csnfand;
846
847                 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
848                 csnfeq.f_ava = &aa_eq;
849                 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
850                 if ( sop->o_sync_state.ctxcsn != NULL ) {
851                         csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
852                 } else {
853                         csnfeq.f_av_value = slap_empty_bv;
854                 }
855
856                 csnfand.f_choice = LDAP_FILTER_AND;
857                 csnfand.f_and = &csnfge;
858                 csnfand.f_next = NULL;
859
860                 csnfge.f_choice = LDAP_FILTER_GE;
861                 csnfge.f_ava = &aa_ge;
862                 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
863                 if ( sop->o_sync_state.ctxcsn != NULL ) {
864                         csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
865                 } else {
866                         csnfge.f_av_value = slap_empty_bv;
867                 }
868
869                 if ( search_context_csn && !IS_PSEARCH ) {
870                         csnfge.f_next = &contextcsnand;
871
872                         contextcsnand.f_choice = LDAP_FILTER_AND;
873                         contextcsnand.f_and = &contextcsnle;
874                         contextcsnand.f_next = NULL;
875         
876                         contextcsnle.f_choice = LDAP_FILTER_LE;
877                         contextcsnle.f_ava = &aa_le;
878                         contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
879                         contextcsnle.f_av_value = *search_context_csn;
880                         contextcsnle.f_next = sop->oq_search.rs_filter;
881
882                         mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
883                         if ( sop->o_sync_state.ctxcsn &&
884                                 sop->o_sync_state.ctxcsn->bv_val != NULL )
885                         {
886                                 value_match( &match, slap_schema.si_ad_entryCSN, mr,
887                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
888                                                 &sop->o_sync_state.ctxcsn[0], search_context_csn,
889                                                 &text );
890                         } else {
891                                 match = -1;
892                         }
893                         no_sync_state_change = ( match >= 0 );
894                 } else {
895                         csnfge.f_next = sop->oq_search.rs_filter;
896                 }
897         }
898
899         for ( id = bdb_idl_first( candidates, &cursor );
900                 id != NOID && !no_sync_state_change;
901                 id = bdb_idl_next( candidates, &cursor ) )
902         {
903                 int scopeok = 0;
904
905 loop_begin:
906                 /* check for abandon */
907                 if ( sop->o_abandon ) {
908                         if ( sop != op ) {
909                                 bdb_drop_psearch( sop, sop->o_msgid );
910                         }
911                         rs->sr_err = LDAP_SUCCESS;
912                         goto done;
913                 }
914
915                 if ( sop->o_cancel ) {
916                         assert( sop->o_cancel == SLAP_CANCEL_REQ );
917                         rs->sr_err = LDAP_CANCELLED;
918                         send_ldap_result( sop, rs );
919                         sop->o_cancel = SLAP_CANCEL_ACK;
920                         rs->sr_err = LDAP_SUCCESS;
921                         goto done;
922                 }
923
924                 /* check time limit */
925                 if ( sop->ors_tlimit != SLAP_NO_LIMIT
926                                 && slap_get_time() > stoptime )
927                 {
928                         rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
929                         rs->sr_ref = rs->sr_v2ref;
930                         send_ldap_result( sop, rs );
931                         rs->sr_err = LDAP_SUCCESS;
932                         goto done;
933                 }
934
935                 if (!IS_PSEARCH) {
936 id2entry_retry:
937                         /* get the entry with reader lock */
938                         ei = NULL;
939                         rs->sr_err = bdb_cache_find_id( op, ltid,
940                                 id, &ei, 0, locker, &lock );
941
942                         if (rs->sr_err == LDAP_BUSY) {
943                                 rs->sr_text = "ldap server busy";
944                                 send_ldap_result( sop, rs );
945                                 goto done;
946
947                         } else if ( rs->sr_err == DB_LOCK_DEADLOCK
948                                 || rs->sr_err == DB_LOCK_NOTGRANTED )
949                         {
950                                 goto id2entry_retry;    
951                         }
952
953                         if ( ei && rs->sr_err == LDAP_SUCCESS ) {
954                                 e = ei->bei_e;
955                         } else {
956                                 e = NULL;
957                         }
958
959                         if ( e == NULL ) {
960                                 if( !BDB_IDL_IS_RANGE(candidates) ) {
961                                         /* only complain for non-range IDLs */
962 #ifdef NEW_LOGGING
963                                         LDAP_LOG ( OPERATION, RESULTS,
964                                                 "bdb_search: candidate %ld not found\n",
965                                                 (long) id, 0, 0);
966 #else
967                                         Debug( LDAP_DEBUG_TRACE,
968                                                 "bdb_search: candidate %ld not found\n",
969                                                 (long) id, 0, 0 );
970 #endif
971                                 }
972
973                                 goto loop_continue;
974                         }
975                 } else {
976                         e = ps_e;
977                 }
978
979                 rs->sr_entry = e;
980
981 #ifdef BDB_SUBENTRIES
982                 /* FIXME: send all but syncrepl */
983 #if 0
984                 if ( !is_sync_protocol( sop ) )
985 #endif
986                 {
987                         if ( is_entry_subentry( e ) ) {
988                                 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
989                                         if(!get_subentries_visibility( sop )) {
990                                                 /* only subentries are visible */
991                                                 goto loop_continue;
992                                         }
993
994                                 } else if ( get_subentries( sop ) &&
995                                         !get_subentries_visibility( sop ))
996                                 {
997                                         /* only subentries are visible */
998                                         goto loop_continue;
999                                 }
1000
1001                         } else if ( get_subentries_visibility( sop )) {
1002                                 /* only subentries are visible */
1003                                 goto loop_continue;
1004                         }
1005                 }
1006 #endif /* BDB_SUBENTRIES */
1007
1008                 /* Does this candidate actually satisfy the search scope?
1009                  *
1010                  * Note that we don't lock access to the bei_parent pointer.
1011                  * Since only leaf nodes can be deleted, the parent of any
1012                  * node will always be a valid node. Also since we have
1013                  * a Read lock on the data, it cannot be renamed out of the
1014                  * scope while we are looking at it, and unless we're using
1015                  * BDB_HIER, its parents cannot be moved either.
1016                  */
1017                 switch( sop->ors_scope ) {
1018                 case LDAP_SCOPE_BASE:
1019                         /* This is always true, yes? */
1020                         if ( id == base.e_id ) scopeok = 1;
1021                         break;
1022
1023                 case LDAP_SCOPE_ONELEVEL:
1024                         if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
1025                         break;
1026
1027 #ifdef LDAP_SCOPE_CHILDREN
1028                 case LDAP_SCOPE_CHILDREN:
1029                         if ( id == base.e_id ) break;
1030                         /* Fall-thru */
1031 #endif
1032                 case LDAP_SCOPE_SUBTREE: {
1033                         EntryInfo *tmp;
1034                         for (tmp = BEI(e); tmp;
1035                                  tmp = tmp->bei_parent ) {
1036                                 if ( tmp->bei_id == base.e_id ) {
1037                                         scopeok = 1;
1038                                         break;
1039                                 }
1040                         }
1041                         } break;
1042                 }
1043
1044 #ifdef BDB_ALIASES
1045                 /* aliases were already dereferenced in candidate list */
1046                 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1047                         /* but if the search base is an alias, and we didn't
1048                          * deref it when finding, return it.
1049                          */
1050                         if ( is_entry_alias(e) &&
1051                                 ((sop->ors_deref & LDAP_DEREF_FINDING) ||
1052                                         !bvmatch(&e->e_nname, &op->o_req_ndn)))
1053                         {
1054                                 goto loop_continue;
1055                         }
1056
1057                         /* scopes is only non-empty for onelevel or subtree */
1058                         if ( !scopeok && BDB_IDL_N(scopes) ) {
1059                                 unsigned x;
1060                                 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1061                                         x = bdb_idl_search( scopes, e->e_id );
1062                                         if ( scopes[x] == e->e_id ) scopeok = 1;
1063                                 } else {
1064                                         /* subtree, walk up the tree */
1065                                         EntryInfo *tmp = BEI(e);
1066                                         for (;tmp->bei_parent; tmp=tmp->bei_parent) {
1067                                                 x = bdb_idl_search( scopes, tmp->bei_id );
1068                                                 if ( scopes[x] == tmp->bei_id ) {
1069                                                         scopeok = 1;
1070                                                         break;
1071                                                 }
1072                                         }
1073                                 }
1074                         }
1075                 }
1076 #endif
1077
1078                 /* Not in scope, ignore it */
1079                 if ( !scopeok ) {
1080 #ifdef NEW_LOGGING
1081                         LDAP_LOG ( OPERATION, RESULTS,
1082                                 "bdb_search: %ld scope not okay\n",
1083                                 (long) id, 0, 0);
1084 #else
1085                         Debug( LDAP_DEBUG_TRACE,
1086                                 "bdb_search: %ld scope not okay\n",
1087                                 (long) id, 0, 0 );
1088 #endif
1089                         goto loop_continue;
1090                 }
1091
1092                 /*
1093                  * if it's a referral, add it to the list of referrals. only do
1094                  * this for non-base searches, and don't check the filter
1095                  * explicitly here since it's only a candidate anyway.
1096                  */
1097                 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1098                         && is_entry_referral( e ) )
1099                 {
1100                         BerVarray erefs = get_entry_referrals( sop, e );
1101                         rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1102                                 sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1103                                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1104
1105                         send_search_reference( sop, rs );
1106
1107                         ber_bvarray_free( rs->sr_ref );
1108                         ber_bvarray_free( erefs );
1109                         rs->sr_ref = NULL;
1110
1111                         goto loop_continue;
1112                 }
1113
1114                 if ( !manageDSAit && is_entry_glue( e )) {
1115                         goto loop_continue;
1116                 }
1117
1118                 /* if it matches the filter and scope, send it */
1119                 if (IS_PSEARCH) {
1120                         if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1121                                 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1122                         } else {
1123                                 rs->sr_err = LDAP_COMPARE_TRUE;
1124                         }
1125
1126                 } else {
1127                         if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1128                                 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1129                                 rs->sr_err = test_filter( sop, rs->sr_entry, &contextcsnand );
1130                                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1131                                         if ( rc_sync == LDAP_COMPARE_TRUE ) {
1132                                                 if ( no_sync_state_change ) {
1133 #ifdef NEW_LOGGING
1134                                                         LDAP_LOG ( OPERATION, RESULTS,
1135                                                                 "bdb_search: error in context csn management\n",
1136                                                                 0, 0, 0 );
1137 #else
1138                                                         Debug( LDAP_DEBUG_TRACE,
1139                                                                 "bdb_search: error in context csn management\n",
1140                                                                 0, 0, 0 );
1141 #endif
1142                                                 }
1143                                                 entry_sync_state = LDAP_SYNC_ADD;
1144
1145                                         } else {
1146                                                 if ( no_sync_state_change ) {
1147                                                         goto loop_continue;
1148                                                 }
1149                                                 entry_sync_state = LDAP_SYNC_PRESENT;
1150                                         }
1151                                 }
1152
1153                         } else {
1154                                 rs->sr_err = test_filter( sop,
1155                                         rs->sr_entry, sop->oq_search.rs_filter );
1156                         }
1157                 }
1158
1159                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1160                         /* check size limit */
1161                         if ( --sop->ors_slimit == -1 &&
1162                                 sop->o_sync_slog_size == -1 )
1163                         {
1164                                 if (!IS_PSEARCH) {
1165                                         bdb_cache_return_entry_r( bdb->bi_dbenv,
1166                                                 &bdb->bi_cache, e, &lock );
1167                                 }
1168                                 e = NULL;
1169                                 rs->sr_entry = NULL;
1170                                 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1171                                 rs->sr_ref = rs->sr_v2ref;
1172                                 send_ldap_result( sop, rs );
1173                                 rs->sr_err = LDAP_SUCCESS;
1174                                 goto done;
1175                         }
1176
1177                         if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1178                                 if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
1179                                         send_paged_response( sop, rs, &lastid, tentries );
1180                                         goto done;
1181                                 }
1182                                 lastid = id;
1183                         }
1184
1185                         if (e) {
1186                                 /* safe default */
1187                                 int result = -1;
1188                                 
1189                                 if (IS_PSEARCH) {
1190                                         int premodify_found = 0;
1191                                         int entry_sync_state;
1192
1193                                         if ( ps_type == LDAP_PSEARCH_BY_ADD ||
1194                                                  ps_type == LDAP_PSEARCH_BY_DELETE ||
1195                                                  ps_type == LDAP_PSEARCH_BY_MODIFY ||
1196                                                  ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1197                                         {
1198                                                 if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1199                                                         struct psid_entry* psid_e;
1200                                                         LDAP_LIST_FOREACH( psid_e,
1201                                                                 &op->o_pm_list, ps_link)
1202                                                         {
1203                                                                 if( psid_e->ps_op == sop ) {
1204                                                                         premodify_found = 1;
1205                                                                         LDAP_LIST_REMOVE(psid_e, ps_link);
1206                                                                         break;
1207                                                                 }
1208                                                         }
1209                                                         if (psid_e != NULL) free (psid_e);
1210                                                 }
1211
1212                                                 if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1213                                                         entry_sync_state = LDAP_SYNC_ADD;
1214                                                 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1215                                                         entry_sync_state = LDAP_SYNC_DELETE;
1216                                                 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1217                                                         if ( premodify_found ) {
1218                                                                 entry_sync_state = LDAP_SYNC_MODIFY;
1219                                                         } else {
1220                                                                 entry_sync_state = LDAP_SYNC_ADD;
1221                                                         }
1222                                                 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1223                                                         entry_sync_state = LDAP_SYNC_DELETE;
1224                                                 } else {
1225                                                         rs->sr_err = LDAP_OTHER;
1226                                                         goto done;
1227                                                 }
1228
1229                                                 if ( sop->o_sync_slog_size != -1 ) {
1230                                                         if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1231                                                                 result = slap_add_session_log( op, sop, e );
1232                                                         } else {
1233                                                                 result = 1;
1234                                                         }
1235                                                 } else {
1236                                                         struct berval cookie;
1237                                                         slap_compose_sync_cookie( sop, &cookie,
1238                                                                 search_context_csn,
1239                                                                 sop->o_sync_state.sid,
1240                                                                 sop->o_sync_state.rid );
1241                                                         rs->sr_err = slap_build_sync_state_ctrl( sop,
1242                                                                 rs, e, entry_sync_state, ctrls,
1243                                                                 num_ctrls++, 1, &cookie );
1244                                                         if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1245                                                         rs->sr_attrs = attrs;
1246                                                         rs->sr_operational_attrs = NULL;
1247                                                         rs->sr_ctrls = ctrls;
1248                                                         rs->sr_flags = 0;
1249                                                         result = send_search_entry( sop, rs );
1250                                                         if ( cookie.bv_val ) ch_free( cookie.bv_val );  
1251                                                         slap_sl_free(
1252                                                                 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1253                                                                 sop->o_tmpmemctx );
1254                                                         slap_sl_free( ctrls[--num_ctrls],
1255                                                                 sop->o_tmpmemctx );
1256                                                         ctrls[num_ctrls] = NULL;
1257                                                         rs->sr_ctrls = NULL;
1258                                                 }
1259
1260                                         } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1261                                                 struct psid_entry* psid_e;
1262                                                 psid_e = (struct psid_entry *) ch_calloc(1,
1263                                                         sizeof(struct psid_entry));
1264                                                 psid_e->ps_op = sop;
1265                                                 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1266                                                         psid_e, ps_link );
1267
1268                                         } else {
1269 #ifdef NEW_LOGGING
1270                                                 LDAP_LOG ( OPERATION, RESULTS,
1271                                                         "bdb_search: invalid ps_type (%d) \n",
1272                                                         ps_type, 0, 0);
1273 #else
1274                                                 Debug( LDAP_DEBUG_TRACE,
1275                                                         "bdb_search: invalid ps_type (%d) \n",
1276                                                         ps_type, 0, 0);
1277 #endif
1278                                         }
1279
1280                                 } else {
1281                                         if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1282                                                 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1283                                                         rs->sr_err = slap_build_sync_state_ctrl( sop,
1284                                                                 rs, e, entry_sync_state, ctrls,
1285                                                                 num_ctrls++, 0, NULL );
1286                                                         if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1287                                                         rs->sr_ctrls = ctrls;
1288                                                         rs->sr_attrs = sop->oq_search.rs_attrs;
1289                                                         rs->sr_operational_attrs = NULL;
1290                                                         rs->sr_flags = 0;
1291                                                         result = send_search_entry( sop, rs );
1292                                                         slap_sl_free(
1293                                                                 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1294                                                                 sop->o_tmpmemctx );
1295                                                         slap_sl_free( ctrls[--num_ctrls],
1296                                                                 sop->o_tmpmemctx );
1297                                                         ctrls[num_ctrls] = NULL;
1298                                                         rs->sr_ctrls = NULL;
1299
1300                                                 } else { /* PRESENT */
1301                                                         if ( sync_send_present_mode ) {
1302                                                                 result = slap_build_syncUUID_set( sop,
1303                                                                         &syncUUID_set, e );
1304                                                                 if ( result <= 0 ) {
1305                                                                         result = -1;    
1306                                                                 } else {
1307                                                                         syncUUID_set_cnt++;
1308                                                                         if ( syncUUID_set_cnt ==
1309                                                                                 SLAP_SYNCUUID_SET_SIZE )
1310                                                                         {
1311                                                                                 rs->sr_err = LDAP_SUCCESS;
1312                                                                                 rs->sr_rspoid = LDAP_SYNC_INFO;
1313                                                                                 rs->sr_ctrls = NULL;
1314                                                                                 result = slap_send_syncinfo( sop, rs,
1315                                                                                         LDAP_TAG_SYNC_ID_SET,
1316                                                                                         NULL, 0, syncUUID_set, 0 );
1317                                                                                 if ( result != LDAP_SUCCESS ) {
1318                                                                                         result = -1;
1319                                                                                 }
1320                                                                                 ber_bvarray_free_x( syncUUID_set,
1321                                                                                         sop->o_tmpmemctx );
1322                                                                                 syncUUID_set = NULL;
1323                                                                                 syncUUID_set_cnt = 0;
1324                                                                         }
1325                                                                 }
1326
1327                                                         } else {
1328                                                                 result = 1;
1329                                                         }
1330                                                 }
1331
1332                                         } else {
1333                                                 rs->sr_attrs = sop->oq_search.rs_attrs;
1334                                                 rs->sr_operational_attrs = NULL;
1335                                                 rs->sr_ctrls = NULL;
1336                                                 rs->sr_flags = 0;
1337                                                 rs->sr_err = LDAP_SUCCESS;
1338                                                 result = send_search_entry( sop, rs );
1339                                         }
1340                                 }
1341
1342                                 switch (result) {
1343                                 case 0:         /* entry sent ok */
1344                                         break;
1345                                 case 1:         /* entry not sent */
1346                                         break;
1347                                 case -1:        /* connection closed */
1348                                         if (!IS_PSEARCH) {
1349                                                 bdb_cache_return_entry_r(bdb->bi_dbenv,
1350                                                         &bdb->bi_cache, e, &lock);
1351                                         }
1352                                         e = NULL;
1353                                         rs->sr_entry = NULL;
1354                                         rs->sr_err = LDAP_OTHER;
1355                                         goto done;
1356                                 }
1357                         }
1358
1359                 } else {
1360 #ifdef NEW_LOGGING
1361                         LDAP_LOG ( OPERATION, RESULTS,
1362                                 "bdb_search: %ld does not match filter\n", (long) id, 0, 0);
1363 #else
1364                         Debug( LDAP_DEBUG_TRACE,
1365                                 "bdb_search: %ld does not match filter\n",
1366                                 (long) id, 0, 0 );
1367 #endif
1368                 }
1369
1370 loop_continue:
1371                 if( e != NULL ) {
1372                         /* free reader lock */
1373                         if (!IS_PSEARCH) {
1374                                 bdb_cache_return_entry_r( bdb->bi_dbenv,
1375                                         &bdb->bi_cache, e , &lock );
1376                                 if ( sop->o_nocaching ) {
1377                                         bdb_cache_delete_entry( bdb, ei, locker, &lock );
1378                                 }
1379                         }
1380                         e = NULL;
1381                         rs->sr_entry = NULL;
1382                 }
1383
1384                 ldap_pvt_thread_yield();
1385         }
1386
1387         if ( syncUUID_set_cnt > 0 ) {
1388                 rs->sr_err = LDAP_SUCCESS;
1389                 rs->sr_rspoid = LDAP_SYNC_INFO;
1390                 rs->sr_ctrls = NULL;
1391                 slap_send_syncinfo( sop, rs, LDAP_TAG_SYNC_ID_SET,
1392                         NULL, 0, syncUUID_set, 0 );
1393                 ber_bvarray_free_x( syncUUID_set, sop->o_tmpmemctx );
1394                 syncUUID_set_cnt = 0;
1395         }
1396
1397 nochange:
1398         if (!IS_PSEARCH) {
1399                 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1400                         if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1401                                 struct berval cookie;
1402                                 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1403                                         sop->o_sync_state.sid, sop->o_sync_state.rid );
1404
1405                                 if ( sync_send_present_mode ) {
1406                                         rs->sr_err = LDAP_SUCCESS;
1407                                         rs->sr_rspoid = LDAP_SYNC_INFO;
1408                                         rs->sr_ctrls = NULL;
1409                                         slap_send_syncinfo( sop, rs,
1410                                                 LDAP_TAG_SYNC_REFRESH_PRESENT, &cookie, 1, NULL, 0 );
1411
1412                                 } else {
1413                                         if ( !no_sync_state_change ) {
1414                                                 int slog_found = 0;
1415                                                 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1416                                                 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1417                                                         o_ps_link )
1418                                                 {
1419                                                         if ( ps_list->o_sync_slog_size > 0 ) {
1420                                                                 if ( ps_list->o_sync_state.sid ==
1421                                                                         sop->o_sync_state.sid )
1422                                                                 {
1423                                                                         slog_found = 1;
1424                                                                         break;
1425                                                                 }
1426                                                         }
1427                                                 }
1428                 
1429                                                 if ( slog_found ) {
1430                                                         rs->sr_err = LDAP_SUCCESS;
1431                                                         rs->sr_rspoid = NULL;
1432                                                         rs->sr_ctrls = NULL;
1433                                                         slap_send_session_log( op, ps_list, rs );
1434                                                 }
1435                                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1436                                         }
1437
1438                                         rs->sr_err = LDAP_SUCCESS;
1439                                         rs->sr_rspoid = LDAP_SYNC_INFO;
1440                                         rs->sr_ctrls = NULL;
1441                                         slap_send_syncinfo( sop, rs,
1442                                                 LDAP_TAG_SYNC_REFRESH_DELETE, &cookie, 1, NULL, 0 );
1443                                 }
1444
1445                                 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1446
1447                         } else {
1448                                 /* refreshOnly mode */
1449                                 struct berval cookie;
1450                                 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1451                                         sop->o_sync_state.sid, sop->o_sync_state.rid );
1452
1453                                 if ( sync_send_present_mode ) {
1454                                         slap_build_sync_done_ctrl( sop, rs, ctrls,
1455                                                 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1456
1457                                 } else {
1458                                         if ( !no_sync_state_change ) {
1459                                                 int slog_found = 0;
1460                                                 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1461                                                 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1462                                                         o_ps_link )
1463                                                 {
1464                                                         if ( ps_list->o_sync_slog_size > 0 ) {
1465                                                                 if ( ps_list->o_sync_state.sid ==
1466                                                                                 sop->o_sync_state.sid ) {
1467                                                                         slog_found = 1;
1468                                                                         break;
1469                                                                 }
1470                                                         }
1471                                                 }
1472                 
1473                                                 if ( slog_found ) {
1474                                                         slap_send_session_log( op, ps_list, rs );
1475                                                 }
1476                                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1477                                         }
1478
1479                                         slap_build_sync_done_ctrl( sop, rs, ctrls,
1480                                                 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_DELETES );
1481                                 }
1482
1483                                 rs->sr_ctrls = ctrls;
1484                                 rs->sr_ref = rs->sr_v2ref;
1485                                 rs->sr_err = (rs->sr_v2ref == NULL)
1486                                         ? LDAP_SUCCESS : LDAP_REFERRAL;
1487                                 rs->sr_rspoid = NULL;
1488                                 send_ldap_result( sop, rs );
1489                                 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1490                                         slap_sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1491                                                 sop->o_tmpmemctx );
1492                                 }
1493                                 slap_sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1494                                 ctrls[num_ctrls] = NULL;
1495                                 if ( cookie.bv_val ) ch_free( cookie.bv_val );  
1496                         }
1497
1498                 } else {
1499                         rs->sr_ctrls = NULL;
1500                         rs->sr_ref = rs->sr_v2ref;
1501                         rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1502                         rs->sr_rspoid = NULL;
1503                         if ( get_pagedresults(sop) > SLAP_NO_CONTROL ) {
1504                                 send_paged_response( sop, rs, NULL, 0 );
1505                         } else {
1506                                 send_ldap_result( sop, rs );
1507                         }
1508                 }
1509         }
1510
1511         rs->sr_err = LDAP_SUCCESS;
1512
1513 done:
1514         if( !IS_PSEARCH && e != NULL ) {
1515                 /* free reader lock */
1516                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1517         }
1518
1519         if ( !opinfo )
1520                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
1521
1522         ber_bvfree( search_context_csn );
1523
1524         if( rs->sr_v2ref ) {
1525                 ber_bvarray_free( rs->sr_v2ref );
1526                 rs->sr_v2ref = NULL;
1527         }
1528         if( realbase.bv_val ) ch_free( realbase.bv_val );
1529
1530         return rs->sr_err;
1531 }
1532
1533
1534 static int base_candidate(
1535         BackendDB       *be,
1536         Entry   *e,
1537         ID              *ids )
1538 {
1539 #ifdef NEW_LOGGING
1540         LDAP_LOG ( OPERATION, ENTRY,
1541                 "base_candidate: base: \"%s\" (0x%08lx)\n",
1542                 e->e_nname.bv_val, (long) e->e_id, 0);
1543 #else
1544         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1545                 e->e_nname.bv_val, (long) e->e_id, 0);
1546 #endif
1547
1548         ids[0] = 1;
1549         ids[1] = e->e_id;
1550         return 0;
1551 }
1552
1553 /* Look for "objectClass Present" in this filter.
1554  * Also count depth of filter tree while we're at it.
1555  */
1556 static int oc_filter(
1557         Filter *f,
1558         int cur,
1559         int *max )
1560 {
1561         int rc = 0;
1562
1563         assert( f );
1564
1565         if( cur > *max ) *max = cur;
1566
1567         switch( f->f_choice ) {
1568         case LDAP_FILTER_PRESENT:
1569                 if (f->f_desc == slap_schema.si_ad_objectClass) {
1570                         rc = 1;
1571                 }
1572                 break;
1573
1574         case LDAP_FILTER_AND:
1575         case LDAP_FILTER_OR:
1576                 cur++;
1577                 for ( f=f->f_and; f; f=f->f_next ) {
1578                         (void) oc_filter(f, cur, max);
1579                 }
1580                 break;
1581
1582         default:
1583                 break;
1584         }
1585         return rc;
1586 }
1587
1588 static void search_stack_free( void *key, void *data )
1589 {
1590         ber_memfree_x(data, NULL);
1591 }
1592
1593 static void *search_stack( Operation *op )
1594 {
1595         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1596         void *ret = NULL;
1597
1598         if ( op->o_threadctx ) {
1599                 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1600                         &ret, NULL );
1601         } else {
1602                 ret = bdb->bi_search_stack;
1603         }
1604
1605         if ( !ret ) {
1606                 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1607                         * sizeof( ID ) );
1608                 if ( op->o_threadctx ) {
1609                         ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1610                                 ret, search_stack_free );
1611                 } else {
1612                         bdb->bi_search_stack = ret;
1613                 }
1614         }
1615         return ret;
1616 }
1617
1618 static int search_candidates(
1619         Operation *stackop,
1620         Operation *op,
1621         SlapReply *rs,
1622         Entry *e,
1623         u_int32_t locker,
1624         ID      *ids,
1625         ID      *scopes )
1626 {
1627         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1628         int rc, depth = 1;
1629         Filter          f, rf, xf, nf;
1630         ID              *stack;
1631         AttributeAssertion aa_ref;
1632 #ifdef BDB_SUBENTRIES
1633         Filter  sf;
1634         AttributeAssertion aa_subentry;
1635 #endif
1636
1637         /*
1638          * This routine takes as input a filter (user-filter)
1639          * and rewrites it as follows:
1640          *      (&(scope=DN)[(objectClass=subentry)]
1641          *              (|[(objectClass=referral)(objectClass=alias)](user-filter))
1642          */
1643
1644 #ifdef NEW_LOGGING
1645         LDAP_LOG ( OPERATION, ENTRY,
1646                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n", 
1647                 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope);
1648 #else
1649         Debug(LDAP_DEBUG_TRACE,
1650                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1651                 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1652 #endif
1653
1654         xf.f_or = op->oq_search.rs_filter;
1655         xf.f_choice = LDAP_FILTER_OR;
1656         xf.f_next = NULL;
1657
1658         /* If the user's filter uses objectClass=*,
1659          * these clauses are redundant.
1660          */
1661         if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1662                 && !get_subentries_visibility(op)
1663                 && !is_sync_protocol(op) )
1664         {
1665                 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1666                         /* match referral objects */
1667                         struct berval bv_ref = BER_BVC( "referral" );
1668                         rf.f_choice = LDAP_FILTER_EQUALITY;
1669                         rf.f_ava = &aa_ref;
1670                         rf.f_av_desc = slap_schema.si_ad_objectClass;
1671                         rf.f_av_value = bv_ref;
1672                         rf.f_next = xf.f_or;
1673                         xf.f_or = &rf;
1674                         depth++;
1675                 }
1676         }
1677
1678         f.f_next = NULL;
1679         f.f_choice = LDAP_FILTER_AND;
1680         f.f_and = &nf;
1681         /* Dummy; we compute scope separately now */
1682         nf.f_choice = SLAPD_FILTER_COMPUTED;
1683         nf.f_result = LDAP_SUCCESS;
1684         nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1685                 ? op->oq_search.rs_filter : &xf ;
1686         /* Filter depth increased again, adding dummy clause */
1687         depth++;
1688
1689 #ifdef BDB_SUBENTRIES
1690         if( get_subentries_visibility( op ) ) {
1691                 struct berval bv_subentry = BER_BVC( "SUBENTRY" );
1692                 sf.f_choice = LDAP_FILTER_EQUALITY;
1693                 sf.f_ava = &aa_subentry;
1694                 sf.f_av_desc = slap_schema.si_ad_objectClass;
1695                 sf.f_av_value = bv_subentry;
1696                 sf.f_next = nf.f_next;
1697                 nf.f_next = &sf;
1698         }
1699 #endif
1700
1701         /* Allocate IDL stack, plus 1 more for former tmp */
1702         if ( depth+1 > bdb->bi_search_stack_depth ) {
1703                 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1704         } else {
1705                 stack = search_stack( stackop );
1706         }
1707
1708         if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1709                 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1710         } else {
1711                 rc = bdb_dn2idl( op, e, ids, stack );
1712         }
1713
1714         if ( rc == LDAP_SUCCESS ) {
1715                 rc = bdb_filter_candidates( op, &f, ids,
1716                         stack, stack+BDB_IDL_UM_SIZE );
1717         }
1718
1719         if ( depth+1 > bdb->bi_search_stack_depth ) {
1720                 ch_free( stack );
1721         }
1722
1723         if( rc ) {
1724 #ifdef NEW_LOGGING
1725                 LDAP_LOG ( OPERATION, DETAIL1,
1726                         "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0  );
1727 #else
1728                 Debug(LDAP_DEBUG_TRACE,
1729                         "bdb_search_candidates: failed (rc=%d)\n",
1730                         rc, NULL, NULL );
1731 #endif
1732
1733         } else {
1734 #ifdef NEW_LOGGING
1735                 LDAP_LOG ( OPERATION, DETAIL1,
1736                         "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1737                         (long) ids[0], (long) BDB_IDL_FIRST(ids), 
1738                         (long) BDB_IDL_LAST(ids));
1739 #else
1740                 Debug(LDAP_DEBUG_TRACE,
1741                         "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1742                         (long) ids[0],
1743                         (long) BDB_IDL_FIRST(ids),
1744                         (long) BDB_IDL_LAST(ids) );
1745 #endif
1746         }
1747
1748         return rc;
1749 }
1750
1751 static int
1752 parse_paged_cookie( Operation *op, SlapReply *rs )
1753 {
1754         LDAPControl     **c;
1755         int             rc = LDAP_SUCCESS;
1756         ber_tag_t       tag;
1757         ber_int_t       size;
1758         BerElement      *ber;
1759         struct berval   cookie = BER_BVNULL;
1760
1761         /* this function must be invoked only if the pagedResults
1762          * control has been detected, parsed and partially checked
1763          * by the frontend */
1764         assert( get_pagedresults( op ) > SLAP_NO_CONTROL );
1765
1766         /* look for the appropriate ctrl structure */
1767         for ( c = op->o_ctrls; c[0] != NULL; c++ ) {
1768                 if ( strcmp( c[0]->ldctl_oid, LDAP_CONTROL_PAGEDRESULTS ) == 0 )
1769                 {
1770                         break;
1771                 }
1772         }
1773
1774         if ( c[0] == NULL ) {
1775                 rs->sr_text = "missing pagedResults control";
1776                 return LDAP_PROTOCOL_ERROR;
1777         }
1778
1779         /* Already tested by frontend */
1780         assert( c[0]->ldctl_value.bv_len > 0 );
1781 #if 0
1782         if ( c[0]->ldctl_value.bv_len == 0 ) {
1783                 rs->sr_text = "paged results control value is empty (or absent)";
1784                 return LDAP_PROTOCOL_ERROR;
1785         }
1786 #endif
1787
1788         /* Parse the control value
1789          *      realSearchControlValue ::= SEQUENCE {
1790          *              size    INTEGER (0..maxInt),
1791          *                              -- requested page size from client
1792          *                              -- result set size estimate from server
1793          *              cookie  OCTET STRING
1794          * }
1795          */
1796         ber = ber_init( &c[0]->ldctl_value );
1797         if ( ber == NULL ) {
1798                 rs->sr_text = "internal error";
1799                 return LDAP_OTHER;
1800         }
1801
1802         tag = ber_scanf( ber, "{im}", &size, &cookie );
1803
1804         /* Already tested by frontend */
1805         assert( tag != LBER_ERROR );
1806 #if 0
1807         if ( tag == LBER_ERROR ) {
1808                 rs->sr_text = "paged results control could not be decoded";
1809                 rc = LDAP_PROTOCOL_ERROR;
1810                 goto done;
1811         }
1812 #endif
1813
1814         /* Already tested by frontend */
1815         assert( size >= 0 );
1816 #if 0
1817         if ( size < 0 ) {
1818                 rs->sr_text = "paged results control size invalid";
1819                 rc = LDAP_PROTOCOL_ERROR;
1820                 goto done;
1821         }
1822 #endif
1823
1824         /* cookie decoding/checks deferred to backend... */
1825         if ( cookie.bv_len ) {
1826                 PagedResultsCookie reqcookie;
1827                 if( cookie.bv_len != sizeof( reqcookie ) ) {
1828                         /* bad cookie */
1829                         rs->sr_text = "paged results cookie is invalid";
1830                         rc = LDAP_PROTOCOL_ERROR;
1831                         goto done;
1832                 }
1833
1834                 AC_MEMCPY( &reqcookie, cookie.bv_val, sizeof( reqcookie ));
1835
1836                 if ( reqcookie > op->o_pagedresults_state.ps_cookie ) {
1837                         /* bad cookie */
1838                         rs->sr_text = "paged results cookie is invalid";
1839                         rc = LDAP_PROTOCOL_ERROR;
1840                         goto done;
1841
1842                 } else if ( reqcookie < op->o_pagedresults_state.ps_cookie ) {
1843                         rs->sr_text = "paged results cookie is invalid or old";
1844                         rc = LDAP_UNWILLING_TO_PERFORM;
1845                         goto done;
1846                 }
1847
1848         } else {
1849                 /* Initial request.  Initialize state. */
1850 #if 0
1851                 if ( op->o_conn->c_pagedresults_state.ps_cookie != 0 ) {
1852                         /* There's another pagedResults control on the
1853                          * same connection; reject new pagedResults controls 
1854                          * (allowed by RFC2696) */
1855                         rs->sr_text = "paged results cookie unavailable; try later";
1856                         rc = LDAP_UNWILLING_TO_PERFORM;
1857                         goto done;
1858                 }
1859 #endif
1860                 op->o_pagedresults_state.ps_cookie = 0;
1861                 op->o_pagedresults_state.ps_count = 0;
1862         }
1863
1864 done:;
1865         (void)ber_free( ber, 1 );
1866
1867         return rc;
1868 }
1869
1870 static void
1871 send_paged_response( 
1872         Operation       *op,
1873         SlapReply       *rs,
1874         ID              *lastid,
1875         int             tentries )
1876 {
1877         LDAPControl     ctrl, *ctrls[2];
1878         BerElementBuffer berbuf;
1879         BerElement      *ber = (BerElement *)&berbuf;
1880         PagedResultsCookie respcookie;
1881         struct berval cookie;
1882
1883 #ifdef NEW_LOGGING
1884         LDAP_LOG ( OPERATION, ENTRY,
1885                 "send_paged_response: lastid=0x%08lx nentries=%d\n", 
1886                 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1887 #else
1888         Debug(LDAP_DEBUG_ARGS,
1889                 "send_paged_response: lastid=0x%08lx nentries=%d\n", 
1890                 lastid ? *lastid : 0, rs->sr_nentries, NULL );
1891 #endif
1892
1893         BER_BVZERO( &ctrl.ldctl_value );
1894         ctrls[0] = &ctrl;
1895         ctrls[1] = NULL;
1896
1897         ber_init2( ber, NULL, LBER_USE_DER );
1898
1899         if ( lastid ) {
1900                 respcookie = ( PagedResultsCookie )(*lastid);
1901                 cookie.bv_len = sizeof( respcookie );
1902                 cookie.bv_val = (char *)&respcookie;
1903
1904         } else {
1905                 respcookie = ( PagedResultsCookie )0;
1906                 BER_BVSTR( &cookie, "" );
1907         }
1908
1909         op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1910         op->o_conn->c_pagedresults_state.ps_count =
1911                 op->o_pagedresults_state.ps_count + rs->sr_nentries;
1912
1913         /* return size of 0 -- no estimate */
1914         ber_printf( ber, "{iO}", 0, &cookie ); 
1915
1916         if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1917                 goto done;
1918         }
1919
1920         ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1921         ctrls[0]->ldctl_iscritical = 0;
1922
1923         rs->sr_ctrls = ctrls;
1924         rs->sr_err = LDAP_SUCCESS;
1925         send_ldap_result( op, rs );
1926         rs->sr_ctrls = NULL;
1927
1928 done:
1929         (void) ber_free_buf( ber );
1930 }
1931