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