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