]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/search.c
ded1b67239c6574224094d2ca3e08eef80a83828
[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 /* For persistent searches, op is the currently executing operation,
365  * sop is the persistent search. For regular searches, sop = op.
366  */
367 int
368 bdb_do_search( Operation *op, SlapReply *rs, Operation *sop,
369         Entry *ps_e, int ps_type )
370 {
371         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
372         time_t          stoptime;
373         ID              id, cursor;
374         ID              candidates[BDB_IDL_UM_SIZE];
375         ID              scopes[BDB_IDL_DB_SIZE];
376         Entry           *e = NULL, base, e_root = {0};
377         Entry           *matched = NULL;
378         EntryInfo       *ei, ei_root = {0};
379         struct berval   realbase = BER_BVNULL;
380         int             manageDSAit;
381         int             tentries = 0;
382         ID              lastid = NOID;
383         AttributeName   *attrs;
384
385         Filter          contextcsnand, contextcsnle, cookief, csnfnot,
386                         csnfeq, csnfand, csnfge;
387         AttributeAssertion aa_ge, aa_eq, aa_le;
388         struct berval   *search_context_csn = NULL;
389         DB_LOCK         ctxcsn_lock;
390         LDAPControl     *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
391         int             num_ctrls = 0;
392         AttributeName   uuid_attr[2];
393         int             rc_sync = 0;
394         int             entry_sync_state = -1;
395         AttributeName   null_attr;
396         int             no_sync_state_change = 0;
397
398         u_int32_t       locker = 0;
399         DB_LOCK         lock;
400
401         Operation       *ps_list;
402         int                     sync_send_present_mode = 1;
403         int                     match;
404         MatchingRule *mr;
405         const char *text;
406         int                     slog_found = 0;
407
408         BerVarray       syncUUID_set = NULL;
409         int                     syncUUID_set_cnt = 0;
410
411 #ifdef NEW_LOGGING
412         LDAP_LOG( OPERATION, ENTRY, "bdb_search\n", 0, 0, 0 );
413 #else
414         Debug( LDAP_DEBUG_TRACE, "=> bdb_search\n",
415                 0, 0, 0);
416 #endif
417         attrs = sop->oq_search.rs_attrs;
418
419         if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST ) {
420                 struct slap_session_entry *sent;
421                 if ( sop->o_sync_state.sid >= 0 ) {
422                         LDAP_LIST_FOREACH( sent, &bdb->bi_session_list, se_link ) {
423                                 if ( sent->se_id == sop->o_sync_state.sid ) {
424                                         sop->o_sync_slog_size = sent->se_size;
425                                         break;
426                                 }
427                         }
428                 }
429         }
430
431         /* psearch needs to be registered before refresh begins */
432         /* psearch and refresh transmission is serialized in send_ldap_ber() */
433         if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
434                 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
435                 LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
436                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
437
438         } else if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH_AND_PERSIST
439                 && sop->o_sync_slog_size >= 0 )
440         {
441                 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
442                 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
443                         if ( ps_list->o_sync_slog_size >= 0 ) {
444                                 if ( ps_list->o_sync_state.sid == sop->o_sync_state.sid ) {
445                                         slog_found = 1;
446                                         break;
447                                 }
448                         }
449                 }
450
451                 if ( slog_found ) {
452                         if ( ps_list->o_sync_slog_omitcsn.bv_len != 0 ) {
453                                 mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
454                                 if ( sop->o_sync_state.ctxcsn &&
455                                         sop->o_sync_state.ctxcsn->bv_val != NULL )
456                                 {
457                                         value_match( &match, slap_schema.si_ad_entryCSN, mr,
458                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
459                                                 sop->o_sync_state.ctxcsn,
460                                                 &ps_list->o_sync_slog_omitcsn,
461                                                 &text );
462                                 } else {
463                                         match = -1;
464                                 }
465                                 if ( match >= 0 ) {
466                                         sync_send_present_mode = 0;
467                                 }
468                         } else {
469                                 sync_send_present_mode = 0;
470                         }
471                 } else if ( sop->o_sync_slog_size >= 0 ) {
472                         LDAP_LIST_INSERT_HEAD( &bdb->bi_psearch_list, sop, o_ps_link );
473                 } else {
474                         sop->o_sync_state.sid = -1;
475                 }
476                 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
477         }
478
479         null_attr.an_desc = NULL;
480         null_attr.an_oc = NULL;
481         null_attr.an_oc_exclude = 0;
482         null_attr.an_name.bv_len = 0;
483         null_attr.an_name.bv_val = NULL;
484
485         for( num_ctrls = 0; num_ctrls < SLAP_MAX_RESPONSE_CONTROLS; num_ctrls++ ) {
486                 ctrls[num_ctrls] = NULL;
487         }
488         num_ctrls = 0;
489
490         if ( IS_PSEARCH && IS_BDB_REPLACE(ps_type)) {
491                 attrs = uuid_attr;
492                 attrs[0].an_desc = NULL;
493                 attrs[0].an_oc = NULL;
494                 attrs[0].an_oc_exclude = 0;
495                 attrs[0].an_name.bv_len = 0;
496                 attrs[0].an_name.bv_val = NULL;
497         }
498
499         manageDSAit = get_manageDSAit( sop );
500
501         /* Sync control overrides manageDSAit */
502         if ( !IS_PSEARCH && sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
503                 if ( manageDSAit == SLAP_NO_CONTROL ) {
504                         manageDSAit = SLAP_CRITICAL_CONTROL;
505                 }
506         } else if ( IS_PSEARCH ) {
507                 if ( manageDSAit == SLAP_NO_CONTROL ) {
508                         manageDSAit = SLAP_CRITICAL_CONTROL;
509                 }
510         }
511
512         rs->sr_err = LOCK_ID( bdb->bi_dbenv, &locker );
513
514         switch(rs->sr_err) {
515         case 0:
516                 break;
517         default:
518                 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
519                 return rs->sr_err;
520         }
521
522         if ( sop->o_req_ndn.bv_len == 0 ) {
523                 /* DIT root special case */
524                 ei_root.bei_e = &e_root;
525                 ei_root.bei_parent = &ei_root;
526                 e_root.e_private = &ei_root;
527                 e_root.e_id = 1;
528                 ei = &ei_root;
529                 rs->sr_err = LDAP_SUCCESS;
530         } else {
531 dn2entry_retry:
532                 /* get entry with reader lock */
533                 rs->sr_err = bdb_dn2entry( op, NULL, &sop->o_req_ndn, &ei,
534                         1, locker, &lock );
535         }
536
537         switch(rs->sr_err) {
538         case DB_NOTFOUND:
539                 matched = ei->bei_e;
540                 break;
541         case 0:
542                 e = ei->bei_e;
543                 break;
544         case LDAP_BUSY:
545                 send_ldap_error( sop, rs, LDAP_BUSY, "ldap server busy" );
546                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
547                 return LDAP_BUSY;
548         case DB_LOCK_DEADLOCK:
549         case DB_LOCK_NOTGRANTED:
550                 goto dn2entry_retry;
551         default:
552                 send_ldap_error( sop, rs, LDAP_OTHER, "internal error" );
553                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
554                 return rs->sr_err;
555         }
556
557         if ( e && (op->ors_deref & LDAP_DEREF_FINDING) && is_entry_alias(e) ) {
558                 BDB_IDL_ZERO(candidates);
559                 e = deref_base( op, rs, e, &matched, locker, &lock,
560                         candidates, NULL );
561         }
562
563         if ( e == NULL ) {
564                 struct berval matched_dn = BER_BVNULL;
565
566                 if ( matched != NULL ) {
567                         BerVarray erefs;
568                         ber_dupbv( &matched_dn, &matched->e_name );
569
570                         erefs = is_entry_referral( matched )
571                                 ? get_entry_referrals( op, matched )
572                                 : NULL;
573
574                         bdb_cache_return_entry_r (bdb->bi_dbenv, &bdb->bi_cache,
575                                 matched, &lock);
576                         matched = NULL;
577
578                         if( erefs ) {
579                                 rs->sr_ref = referral_rewrite( erefs, &matched_dn,
580                                         &sop->o_req_dn, sop->oq_search.rs_scope );
581                                 ber_bvarray_free( erefs );
582                         }
583
584                 } else {
585                         rs->sr_ref = referral_rewrite( default_referral,
586                                 NULL, &sop->o_req_dn, sop->oq_search.rs_scope );
587                 }
588
589                 rs->sr_err = LDAP_REFERRAL;
590                 rs->sr_matched = matched_dn.bv_val;
591                 send_ldap_result( sop, rs );
592
593                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
594                 if ( rs->sr_ref ) {
595                         ber_bvarray_free( rs->sr_ref );
596                         rs->sr_ref = NULL;
597                 }
598                 if ( matched_dn.bv_val ) {
599                         ber_memfree( matched_dn.bv_val );
600                         rs->sr_matched = NULL;
601                 }
602                 return rs->sr_err;
603         }
604
605         if ( !manageDSAit && e != &e_root && is_entry_referral( e ) ) {
606                 /* entry is a referral, don't allow add */
607                 struct berval matched_dn;
608                 BerVarray erefs;
609                 
610                 ber_dupbv( &matched_dn, &e->e_name );
611                 erefs = get_entry_referrals( op, e );
612
613                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
614                 e = NULL;
615
616                 if( erefs ) {
617                         rs->sr_ref = referral_rewrite( erefs, &matched_dn,
618                                 &sop->o_req_dn, sop->oq_search.rs_scope );
619                         ber_bvarray_free( erefs );
620                 }
621
622 #ifdef NEW_LOGGING
623                 LDAP_LOG ( OPERATION, RESULTS, 
624                         "bdb_search: entry is referral\n", 0, 0, 0 );
625 #else
626                 Debug( LDAP_DEBUG_TRACE, "bdb_search: entry is referral\n",
627                         0, 0, 0 );
628 #endif
629
630                 if (!rs->sr_ref) rs->sr_text = "bad_referral object";
631                 rs->sr_err = LDAP_REFERRAL;
632                 rs->sr_matched = matched_dn.bv_val;
633                 send_ldap_result( sop, rs );
634
635                 LOCK_ID_FREE (bdb->bi_dbenv, locker );
636                 ber_bvarray_free( rs->sr_ref );
637                 rs->sr_ref = NULL;
638                 ber_memfree( matched_dn.bv_val );
639                 rs->sr_matched = NULL;
640                 return 1;
641         }
642
643         if ( get_assert( op ) &&
644                 ( test_filter( op, e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
645         {
646                 rs->sr_err = LDAP_ASSERTION_FAILED;
647                 send_ldap_result( sop, rs );
648                 return 1;
649         }
650
651         /* compute it anyway; root does not use it */
652         stoptime = op->o_time + sop->ors_tlimit;
653
654         /* need normalized dn below */
655         ber_dupbv( &realbase, &e->e_nname );
656
657         /* Copy info to base, must free entry before accessing the database
658          * in search_candidates, to avoid deadlocks.
659          */
660         base.e_private = e->e_private;
661         base.e_nname = realbase;
662         base.e_id = e->e_id;
663
664         if ( e != &e_root ) {
665                 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
666         }
667         e = NULL;
668
669         if ( !IS_PSEARCH ) {
670                 rs->sr_err = bdb_get_commit_csn( sop, rs, &search_context_csn,
671                         locker, &ctxcsn_lock );
672
673                 if ( rs->sr_err != LDAP_SUCCESS ) {
674                         send_ldap_error( sop, rs, rs->sr_err,
675                                 "error in csn management in search" );
676                         goto done;
677                 }
678
679                 if ( sop->o_sync_mode != SLAP_SYNC_NONE &&
680                         sop->o_sync_state.ctxcsn &&
681                         sop->o_sync_state.ctxcsn->bv_val &&
682                         ber_bvcmp( &sop->o_sync_state.ctxcsn[0], search_context_csn ) == 0 )
683                 {
684                         bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
685                         goto nochange;
686                 }
687         } else {
688                 search_context_csn = ber_dupbv( NULL, &op->o_sync_csn );        
689         }
690
691         /* select candidates */
692         if ( sop->oq_search.rs_scope == LDAP_SCOPE_BASE ) {
693                 rs->sr_err = base_candidate( op->o_bd, &base, candidates );
694
695         } else {
696                 BDB_IDL_ZERO( candidates );
697                 BDB_IDL_ZERO( scopes );
698                 rs->sr_err = search_candidates( op, sop, rs, &base,
699                         locker, candidates, scopes );
700         }
701
702         if ( !IS_PSEARCH && sop->o_sync_mode != SLAP_SYNC_NONE ) {
703                 bdb_cache_entry_db_unlock( bdb->bi_dbenv, &ctxcsn_lock );
704         }
705
706         /* start cursor at beginning of candidates.
707          */
708         cursor = 0;
709         if (IS_PSEARCH) {
710                 if ( !BDB_IDL_IS_RANGE( candidates ) ) {
711                         cursor = bdb_idl_search( candidates, ps_e->e_id );
712                         if ( candidates[cursor] != ps_e->e_id ) {
713                                 rs->sr_err = LDAP_SUCCESS;
714                                 goto done;
715                         }
716                 } else if ( ps_e->e_id < BDB_IDL_RANGE_FIRST( candidates ) ||
717                         ps_e->e_id > BDB_IDL_RANGE_LAST( candidates ))
718                 {
719                         rs->sr_err = LDAP_SUCCESS;
720                         goto done;
721                 }
722                 candidates[0] = 1;
723                 candidates[1] = ps_e->e_id;
724         }
725
726         if ( candidates[0] == 0 ) {
727 #ifdef NEW_LOGGING
728                 LDAP_LOG ( OPERATION, RESULTS,
729                         "bdb_search: no candidates\n", 0, 0, 0 );
730 #else
731                 Debug( LDAP_DEBUG_TRACE, "bdb_search: no candidates\n",
732                         0, 0, 0 );
733 #endif
734
735                 goto nochange;
736         }
737
738         /* if not root and candidates exceed to-be-checked entries, abort */
739         if ( sop->ors_limit     /* isroot == TRUE */ &&
740                 sop->ors_limit->lms_s_unchecked != -1 &&
741                 BDB_IDL_N(candidates) > (unsigned) sop->ors_limit->lms_s_unchecked )
742         {
743                 rs->sr_err = LDAP_ADMINLIMIT_EXCEEDED;
744                 send_ldap_result( sop, rs );
745                 rs->sr_err = LDAP_SUCCESS;
746                 goto done;
747         }
748
749         if ( sop->ors_limit == NULL     /* isroot == FALSE */ ||
750                 !sop->ors_limit->lms_s_pr_hide )
751         {
752                 tentries = BDB_IDL_N(candidates);
753         }
754
755         if ( get_pagedresults(sop) ) {
756                 if ( (ID)( sop->o_pagedresults_state.ps_cookie ) == 0 ) {
757                         id = bdb_idl_first( candidates, &cursor );
758
759                 } else {
760                         if ( sop->o_pagedresults_size == 0 ) {
761                                 rs->sr_err = LDAP_SUCCESS;
762                                 rs->sr_text = "search abandoned by pagedResult size=0";
763                                 send_ldap_result( sop, rs );
764                                 goto done;
765                         }
766                         for ( id = bdb_idl_first( candidates, &cursor );
767                                 id != NOID &&
768                                         id <= (ID)( sop->o_pagedresults_state.ps_cookie );
769                                 id = bdb_idl_next( candidates, &cursor ) )
770                         {
771                                 /* empty */;
772                         }
773                 }
774
775                 if ( cursor == NOID ) {
776 #ifdef NEW_LOGGING
777                         LDAP_LOG ( OPERATION, RESULTS, 
778                                 "bdb_search: no paged results candidates\n", 
779                                 0, 0, 0 );
780 #else
781                         Debug( LDAP_DEBUG_TRACE, 
782                                 "bdb_search: no paged results candidates\n",
783                                 0, 0, 0 );
784 #endif
785                         send_paged_response( sop, rs, lastid, 0 );
786
787                         rs->sr_err = LDAP_OTHER;
788                         goto done;
789                 }
790                 goto loop_begin;
791         }
792
793         if (( sop->o_sync_mode & SLAP_SYNC_REFRESH ) || IS_PSEARCH ) {
794                 int match;
795
796                 cookief.f_choice = LDAP_FILTER_AND;
797                 cookief.f_and = &csnfnot;
798                 cookief.f_next = NULL;
799
800                 csnfnot.f_choice = LDAP_FILTER_NOT;
801                 csnfnot.f_not = &csnfeq;
802                 csnfnot.f_next = &csnfand;
803
804                 csnfeq.f_choice = LDAP_FILTER_EQUALITY;
805                 csnfeq.f_ava = &aa_eq;
806                 csnfeq.f_av_desc = slap_schema.si_ad_entryCSN;
807                 if ( sop->o_sync_state.ctxcsn != NULL ) {
808                         csnfeq.f_av_value = *sop->o_sync_state.ctxcsn;
809                 } else {
810                         csnfeq.f_av_value = slap_empty_bv;
811                 }
812
813                 csnfand.f_choice = LDAP_FILTER_AND;
814                 csnfand.f_and = &csnfge;
815                 csnfand.f_next = NULL;
816
817                 csnfge.f_choice = LDAP_FILTER_GE;
818                 csnfge.f_ava = &aa_ge;
819                 csnfge.f_av_desc = slap_schema.si_ad_entryCSN;
820                 if ( sop->o_sync_state.ctxcsn != NULL ) {
821                         csnfge.f_av_value = *sop->o_sync_state.ctxcsn;
822                 } else {
823                         csnfge.f_av_value = slap_empty_bv;
824                 }
825
826                 if ( search_context_csn && !IS_PSEARCH ) {
827                         csnfge.f_next = &contextcsnand;
828
829                         contextcsnand.f_choice = LDAP_FILTER_AND;
830                         contextcsnand.f_and = &contextcsnle;
831                         contextcsnand.f_next = NULL;
832         
833                         contextcsnle.f_choice = LDAP_FILTER_LE;
834                         contextcsnle.f_ava = &aa_le;
835                         contextcsnle.f_av_desc = slap_schema.si_ad_entryCSN;
836                         contextcsnle.f_av_value = *search_context_csn;
837                         contextcsnle.f_next = sop->oq_search.rs_filter;
838
839                         mr = slap_schema.si_ad_entryCSN->ad_type->sat_ordering;
840                         if ( sop->o_sync_state.ctxcsn &&
841                                 sop->o_sync_state.ctxcsn->bv_val != NULL )
842                         {
843                                 value_match( &match, slap_schema.si_ad_entryCSN, mr,
844                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
845                                                 &sop->o_sync_state.ctxcsn[0], search_context_csn,
846                                                 &text );
847                         } else {
848                                 match = -1;
849                         }
850                         no_sync_state_change = ( match >= 0 );
851                 } else {
852                         csnfge.f_next = sop->oq_search.rs_filter;
853                 }
854         }
855
856         for ( id = bdb_idl_first( candidates, &cursor );
857                 id != NOID && !no_sync_state_change;
858                 id = bdb_idl_next( candidates, &cursor ) )
859         {
860                 int scopeok = 0;
861
862 loop_begin:
863                 /* check for abandon */
864                 if ( sop->o_abandon ) {
865                         if ( sop != op ) {
866                                 bdb_drop_psearch( sop, sop->o_msgid );
867                         }
868                         rs->sr_err = LDAP_SUCCESS;
869                         goto done;
870                 }
871
872                 if ( sop->o_cancel ) {
873                         assert( sop->o_cancel == SLAP_CANCEL_REQ );
874                         rs->sr_err = LDAP_CANCELLED;
875                         send_ldap_result( sop, rs );
876                         sop->o_cancel = SLAP_CANCEL_ACK;
877                         rs->sr_err = LDAP_SUCCESS;
878                         goto done;
879                 }
880
881                 /* check time limit */
882                 if ( sop->ors_tlimit != -1 && slap_get_time() > stoptime ) {
883                         rs->sr_err = LDAP_TIMELIMIT_EXCEEDED;
884                         rs->sr_ref = rs->sr_v2ref;
885                         send_ldap_result( sop, rs );
886                         rs->sr_err = LDAP_SUCCESS;
887                         goto done;
888                 }
889
890                 if (!IS_PSEARCH) {
891 id2entry_retry:
892                         /* get the entry with reader lock */
893                         ei = NULL;
894                         rs->sr_err = bdb_cache_find_id( op, NULL,
895                                 id, &ei, 0, locker, &lock );
896
897                         if (rs->sr_err == LDAP_BUSY) {
898                                 rs->sr_text = "ldap server busy";
899                                 send_ldap_result( sop, rs );
900                                 goto done;
901
902                         } else if ( rs->sr_err == DB_LOCK_DEADLOCK
903                                 || rs->sr_err == DB_LOCK_NOTGRANTED )
904                         {
905                                 goto id2entry_retry;    
906                         }
907
908                         if ( ei && rs->sr_err == LDAP_SUCCESS ) {
909                                 e = ei->bei_e;
910                         } else {
911                                 e = NULL;
912                         }
913
914                         if ( e == NULL ) {
915                                 if( !BDB_IDL_IS_RANGE(candidates) ) {
916                                         /* only complain for non-range IDLs */
917 #ifdef NEW_LOGGING
918                                         LDAP_LOG ( OPERATION, RESULTS,
919                                                 "bdb_search: candidate %ld not found\n",
920                                                 (long) id, 0, 0);
921 #else
922                                         Debug( LDAP_DEBUG_TRACE,
923                                                 "bdb_search: candidate %ld not found\n",
924                                                 (long) id, 0, 0 );
925 #endif
926                                 }
927
928                                 goto loop_continue;
929                         }
930                 } else {
931                         e = ps_e;
932                 }
933
934                 rs->sr_entry = e;
935
936 #ifdef BDB_SUBENTRIES
937                 /* FIXME: send all but syncrepl */
938 #if 0
939                 if ( !is_sync_protocol( sop ) )
940 #endif
941                 {
942                         if ( is_entry_subentry( e ) ) {
943                                 if( sop->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
944                                         if(!get_subentries_visibility( sop )) {
945                                                 /* only subentries are visible */
946                                                 goto loop_continue;
947                                         }
948
949                                 } else if ( get_subentries( sop ) &&
950                                         !get_subentries_visibility( sop ))
951                                 {
952                                         /* only subentries are visible */
953                                         goto loop_continue;
954                                 }
955
956                         } else if ( get_subentries_visibility( sop )) {
957                                 /* only subentries are visible */
958                                 goto loop_continue;
959                         }
960                 }
961 #endif /* BDB_SUBENTRIES */
962
963                 /* Does this candidate actually satisfy the search scope?
964                  *
965                  * Note that we don't lock access to the bei_parent pointer.
966                  * Since only leaf nodes can be deleted, the parent of any
967                  * node will always be a valid node. Also since we have
968                  * a Read lock on the data, it cannot be renamed out of the
969                  * scope while we are looking at it, and unless we're using
970                  * BDB_HIER, its parents cannot be moved either.
971                  */
972                 switch( sop->ors_scope ) {
973                 case LDAP_SCOPE_BASE:
974                         /* This is always true, yes? */
975                         if ( id == base.e_id ) scopeok = 1;
976                         break;
977
978                 case LDAP_SCOPE_ONELEVEL:
979                         if ( ei->bei_parent->bei_id == base.e_id ) scopeok = 1;
980                         break;
981
982 #ifdef LDAP_SCOPE_CHILDREN
983                 case LDAP_SCOPE_CHILDREN:
984                         if ( id == base.e_id ) break;
985                         /* Fall-thru */
986 #endif
987                 case LDAP_SCOPE_SUBTREE: {
988                         EntryInfo *tmp;
989                         for (tmp = BEI(e); tmp->bei_parent;
990                                  tmp = tmp->bei_parent ) {
991                                 if ( tmp->bei_id == base.e_id ) {
992                                         scopeok = 1;
993                                         break;
994                                 }
995                         }
996                         } break;
997                 }
998
999 #ifdef BDB_ALIASES
1000                 /* aliases were already dereferenced in candidate list */
1001                 if ( sop->ors_deref & LDAP_DEREF_SEARCHING ) {
1002                         /* but if the search base is an alias, and we didn't
1003                          * deref it when finding, return it.
1004                          */
1005                         if ( is_entry_alias(e) &&
1006                                 ((sop->ors_deref & LDAP_DEREF_FINDING) ||
1007                                         !bvmatch(&e->e_nname, &op->o_req_ndn)))
1008                         {
1009                                 goto loop_continue;
1010                         }
1011
1012                         /* scopes is only non-empty for onelevel or subtree */
1013                         if ( !scopeok && BDB_IDL_N(scopes) ) {
1014                                 unsigned x;
1015                                 if ( sop->ors_scope == LDAP_SCOPE_ONELEVEL ) {
1016                                         x = bdb_idl_search( scopes, e->e_id );
1017                                         if ( scopes[x] == e->e_id ) scopeok = 1;
1018                                 } else {
1019                                         /* subtree, walk up the tree */
1020                                         EntryInfo *tmp = BEI(e);
1021                                         for (;tmp->bei_parent; tmp=tmp->bei_parent) {
1022                                                 x = bdb_idl_search( scopes, tmp->bei_id );
1023                                                 if ( scopes[x] == tmp->bei_id ) {
1024                                                         scopeok = 1;
1025                                                         break;
1026                                                 }
1027                                         }
1028                                 }
1029                         }
1030                 }
1031 #endif
1032
1033                 /* Not in scope, ignore it */
1034                 if ( !scopeok ) {
1035 #ifdef NEW_LOGGING
1036                         LDAP_LOG ( OPERATION, RESULTS,
1037                                 "bdb_search: %ld scope not okay\n",
1038                                 (long) id, 0, 0);
1039 #else
1040                         Debug( LDAP_DEBUG_TRACE,
1041                                 "bdb_search: %ld scope not okay\n",
1042                                 (long) id, 0, 0 );
1043 #endif
1044                         goto loop_continue;
1045                 }
1046
1047                 /*
1048                  * if it's a referral, add it to the list of referrals. only do
1049                  * this for non-base searches, and don't check the filter
1050                  * explicitly here since it's only a candidate anyway.
1051                  */
1052                 if ( !manageDSAit && sop->oq_search.rs_scope != LDAP_SCOPE_BASE
1053                         && is_entry_referral( e ) )
1054                 {
1055                         BerVarray erefs = get_entry_referrals( sop, e );
1056                         rs->sr_ref = referral_rewrite( erefs, &e->e_name, NULL,
1057                                 sop->oq_search.rs_scope == LDAP_SCOPE_ONELEVEL
1058                                         ? LDAP_SCOPE_BASE : LDAP_SCOPE_SUBTREE );
1059
1060                         send_search_reference( sop, rs );
1061
1062                         ber_bvarray_free( rs->sr_ref );
1063                         ber_bvarray_free( erefs );
1064                         rs->sr_ref = NULL;
1065
1066                         goto loop_continue;
1067                 }
1068
1069                 if ( !manageDSAit && is_entry_glue( e )) {
1070                         goto loop_continue;
1071                 }
1072
1073                 /* if it matches the filter and scope, send it */
1074                 if (IS_PSEARCH) {
1075                         if (ps_type != LDAP_PSEARCH_BY_SCOPEOUT) {
1076                                 rs->sr_err = test_filter( sop, rs->sr_entry, &cookief );
1077                         } else {
1078                                 rs->sr_err = LDAP_COMPARE_TRUE;
1079                         }
1080
1081                 } else {
1082                         if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1083                                 rc_sync = test_filter( sop, rs->sr_entry, &cookief );
1084                                 rs->sr_err = test_filter( sop, rs->sr_entry, &contextcsnand );
1085                                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1086                                         if ( rc_sync == LDAP_COMPARE_TRUE ) {
1087                                                 if ( no_sync_state_change ) {
1088 #ifdef NEW_LOGGING
1089                                                         LDAP_LOG ( OPERATION, RESULTS,
1090                                                                 "bdb_search: error in context csn management\n",
1091                                                                 0, 0, 0 );
1092 #else
1093                                                         Debug( LDAP_DEBUG_TRACE,
1094                                                                 "bdb_search: error in context csn management\n",
1095                                                                 0, 0, 0 );
1096 #endif
1097                                                 }
1098                                                 entry_sync_state = LDAP_SYNC_ADD;
1099
1100                                         } else {
1101                                                 if ( no_sync_state_change ) {
1102                                                         goto loop_continue;
1103                                                 }
1104                                                 entry_sync_state = LDAP_SYNC_PRESENT;
1105                                         }
1106                                 }
1107
1108                         } else {
1109                                 rs->sr_err = test_filter( sop,
1110                                         rs->sr_entry, sop->oq_search.rs_filter );
1111                         }
1112                 }
1113
1114                 if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
1115                         /* check size limit */
1116                         if ( --sop->ors_slimit == -1 &&
1117                                 sop->o_sync_slog_size == -1 )
1118                         {
1119                                 if (!IS_PSEARCH) {
1120                                         bdb_cache_return_entry_r( bdb->bi_dbenv,
1121                                                 &bdb->bi_cache, e, &lock );
1122                                 }
1123                                 e = NULL;
1124                                 rs->sr_entry = NULL;
1125                                 rs->sr_err = LDAP_SIZELIMIT_EXCEEDED;
1126                                 rs->sr_ref = rs->sr_v2ref;
1127                                 send_ldap_result( sop, rs );
1128                                 rs->sr_err = LDAP_SUCCESS;
1129                                 goto done;
1130                         }
1131
1132                         if ( get_pagedresults(sop) ) {
1133                                 if ( rs->sr_nentries >= sop->o_pagedresults_size ) {
1134                                         send_paged_response( sop, rs, lastid, tentries );
1135                                         goto done;
1136                                 }
1137                                 lastid = id;
1138                         }
1139
1140                         if (e) {
1141                                 /* safe default */
1142                                 int result = -1;
1143                                 
1144                                 if (IS_PSEARCH) {
1145                                         int premodify_found = 0;
1146                                         int entry_sync_state;
1147
1148                                         if ( ps_type == LDAP_PSEARCH_BY_ADD ||
1149                                                  ps_type == LDAP_PSEARCH_BY_DELETE ||
1150                                                  ps_type == LDAP_PSEARCH_BY_MODIFY ||
1151                                                  ps_type == LDAP_PSEARCH_BY_SCOPEOUT )
1152                                         {
1153                                                 if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1154                                                         struct psid_entry* psid_e;
1155                                                         LDAP_LIST_FOREACH( psid_e,
1156                                                                 &op->o_pm_list, ps_link)
1157                                                         {
1158                                                                 if( psid_e->ps_op == sop ) {
1159                                                                         premodify_found = 1;
1160                                                                         LDAP_LIST_REMOVE(psid_e, ps_link);
1161                                                                         break;
1162                                                                 }
1163                                                         }
1164                                                         if (psid_e != NULL) free (psid_e);
1165                                                 }
1166
1167                                                 if ( ps_type == LDAP_PSEARCH_BY_ADD ) {
1168                                                         entry_sync_state = LDAP_SYNC_ADD;
1169                                                 } else if ( ps_type == LDAP_PSEARCH_BY_DELETE ) {
1170                                                         entry_sync_state = LDAP_SYNC_DELETE;
1171                                                 } else if ( ps_type == LDAP_PSEARCH_BY_MODIFY ) {
1172                                                         if ( premodify_found ) {
1173                                                                 entry_sync_state = LDAP_SYNC_MODIFY;
1174                                                         } else {
1175                                                                 entry_sync_state = LDAP_SYNC_ADD;
1176                                                         }
1177                                                 } else if ( ps_type == LDAP_PSEARCH_BY_SCOPEOUT ) {
1178                                                         entry_sync_state = LDAP_SYNC_DELETE;
1179                                                 } else {
1180                                                         rs->sr_err = LDAP_OTHER;
1181                                                         goto done;
1182                                                 }
1183
1184                                                 if ( sop->o_sync_slog_size != -1 ) {
1185                                                         if ( entry_sync_state == LDAP_SYNC_DELETE ) {
1186                                                                 result = slap_add_session_log( op, sop, e );
1187                                                         } else {
1188                                                                 result = 1;
1189                                                         }
1190                                                 } else {
1191                                                         struct berval cookie;
1192                                                         slap_compose_sync_cookie( sop, &cookie,
1193                                                                 search_context_csn,
1194                                                                 sop->o_sync_state.sid,
1195                                                                 sop->o_sync_state.rid );
1196                                                         rs->sr_err = slap_build_sync_state_ctrl( sop,
1197                                                                 rs, e, entry_sync_state, ctrls,
1198                                                                 num_ctrls++, 1, &cookie );
1199                                                         if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1200                                                         rs->sr_attrs = attrs;
1201                                                         rs->sr_ctrls = ctrls;
1202                                                         rs->sr_flags = 0;
1203                                                         result = send_search_entry( sop, rs );
1204                                                         if ( cookie.bv_val ) ch_free( cookie.bv_val );  
1205                                                         slap_sl_free(
1206                                                                 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1207                                                                 sop->o_tmpmemctx );
1208                                                         slap_sl_free( ctrls[--num_ctrls],
1209                                                                 sop->o_tmpmemctx );
1210                                                         ctrls[num_ctrls] = NULL;
1211                                                         rs->sr_ctrls = NULL;
1212                                                 }
1213
1214                                         } else if ( ps_type == LDAP_PSEARCH_BY_PREMODIFY ) {
1215                                                 struct psid_entry* psid_e;
1216                                                 psid_e = (struct psid_entry *) ch_calloc(1,
1217                                                         sizeof(struct psid_entry));
1218                                                 psid_e->ps_op = sop;
1219                                                 LDAP_LIST_INSERT_HEAD( &op->o_pm_list,
1220                                                         psid_e, ps_link );
1221
1222                                         } else {
1223 #ifdef NEW_LOGGING
1224                                                 LDAP_LOG ( OPERATION, RESULTS,
1225                                                         "bdb_search: invalid ps_type (%d) \n",
1226                                                         ps_type, 0, 0);
1227 #else
1228                                                 Debug( LDAP_DEBUG_TRACE,
1229                                                         "bdb_search: invalid ps_type (%d) \n",
1230                                                         ps_type, 0, 0);
1231 #endif
1232                                         }
1233
1234                                 } else {
1235                                         if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1236                                                 if ( rc_sync == LDAP_COMPARE_TRUE ) { /* ADD */
1237                                                         rs->sr_err = slap_build_sync_state_ctrl( sop,
1238                                                                 rs, e, entry_sync_state, ctrls,
1239                                                                 num_ctrls++, 0, NULL );
1240                                                         if ( rs->sr_err != LDAP_SUCCESS ) goto done;
1241                                                         rs->sr_ctrls = ctrls;
1242                                                         rs->sr_attrs = sop->oq_search.rs_attrs;
1243                                                         rs->sr_flags = 0;
1244                                                         result = send_search_entry( sop, rs );
1245                                                         slap_sl_free(
1246                                                                 ctrls[num_ctrls-1]->ldctl_value.bv_val,
1247                                                                 sop->o_tmpmemctx );
1248                                                         slap_sl_free( ctrls[--num_ctrls],
1249                                                                 sop->o_tmpmemctx );
1250                                                         ctrls[num_ctrls] = NULL;
1251                                                         rs->sr_ctrls = NULL;
1252
1253                                                 } else { /* PRESENT */
1254                                                         if ( sync_send_present_mode ) {
1255                                                                 result = slap_build_syncUUID_set( sop,
1256                                                                         &syncUUID_set, e );
1257                                                                 if ( result <= 0 ) {
1258                                                                         result = -1;    
1259                                                                 } else {
1260                                                                         syncUUID_set_cnt++;
1261                                                                         if ( syncUUID_set_cnt ==
1262                                                                                 SLAP_SYNCUUID_SET_SIZE )
1263                                                                         {
1264                                                                                 rs->sr_err = LDAP_SUCCESS;
1265                                                                                 rs->sr_rspoid = LDAP_SYNC_INFO;
1266                                                                                 rs->sr_ctrls = NULL;
1267                                                                                 result = slap_send_syncinfo( sop, rs,
1268                                                                                         LDAP_TAG_SYNC_ID_SET,
1269                                                                                         NULL, 0, syncUUID_set, 0 );
1270                                                                                 if ( result != LDAP_SUCCESS ) {
1271                                                                                         result = -1;
1272                                                                                 }
1273                                                                                 ber_bvarray_free_x( syncUUID_set,
1274                                                                                         sop->o_tmpmemctx );
1275                                                                                 syncUUID_set = NULL;
1276                                                                                 syncUUID_set_cnt = 0;
1277                                                                         }
1278                                                                 }
1279
1280                                                         } else {
1281                                                                 result = 1;
1282                                                         }
1283                                                 }
1284
1285                                         } else {
1286                                                 rs->sr_attrs = sop->oq_search.rs_attrs;
1287                                                 rs->sr_ctrls = NULL;
1288                                                 rs->sr_flags = 0;
1289                                                 rs->sr_err = LDAP_SUCCESS;
1290                                                 result = send_search_entry( sop, rs );
1291                                         }
1292                                 }
1293
1294                                 switch (result) {
1295                                 case 0:         /* entry sent ok */
1296                                         break;
1297                                 case 1:         /* entry not sent */
1298                                         break;
1299                                 case -1:        /* connection closed */
1300                                         if (!IS_PSEARCH) {
1301                                                 bdb_cache_return_entry_r(bdb->bi_dbenv,
1302                                                         &bdb->bi_cache, e, &lock);
1303                                         }
1304                                         e = NULL;
1305                                         rs->sr_entry = NULL;
1306                                         rs->sr_err = LDAP_OTHER;
1307                                         goto done;
1308                                 }
1309                         }
1310
1311                 } else {
1312 #ifdef NEW_LOGGING
1313                         LDAP_LOG ( OPERATION, RESULTS,
1314                                 "bdb_search: %ld does not match filter\n", (long) id, 0, 0);
1315 #else
1316                         Debug( LDAP_DEBUG_TRACE,
1317                                 "bdb_search: %ld does not match filter\n",
1318                                 (long) id, 0, 0 );
1319 #endif
1320                 }
1321
1322 loop_continue:
1323                 if( e != NULL ) {
1324                         /* free reader lock */
1325                         if (!IS_PSEARCH) {
1326                                 bdb_cache_return_entry_r( bdb->bi_dbenv,
1327                                         &bdb->bi_cache, e , &lock );
1328                                 if ( sop->o_nocaching ) {
1329                                         bdb_cache_delete_entry( bdb, ei, locker, &lock );
1330                                 }
1331                         }
1332                         e = NULL;
1333                         rs->sr_entry = NULL;
1334                 }
1335
1336                 ldap_pvt_thread_yield();
1337         }
1338
1339         if ( syncUUID_set_cnt > 0 ) {
1340                 rs->sr_err = LDAP_SUCCESS;
1341                 rs->sr_rspoid = LDAP_SYNC_INFO;
1342                 rs->sr_ctrls = NULL;
1343                 slap_send_syncinfo( sop, rs, LDAP_TAG_SYNC_ID_SET,
1344                         NULL, 0, syncUUID_set, 0 );
1345                 ber_bvarray_free_x( syncUUID_set, sop->o_tmpmemctx );
1346                 syncUUID_set_cnt = 0;
1347         }
1348
1349 nochange:
1350         if (!IS_PSEARCH) {
1351                 if ( sop->o_sync_mode & SLAP_SYNC_REFRESH ) {
1352                         if ( sop->o_sync_mode & SLAP_SYNC_PERSIST ) {
1353                                 struct berval cookie;
1354                                 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1355                                         sop->o_sync_state.sid, sop->o_sync_state.rid );
1356
1357                                 if ( sync_send_present_mode ) {
1358                                         rs->sr_err = LDAP_SUCCESS;
1359                                         rs->sr_rspoid = LDAP_SYNC_INFO;
1360                                         rs->sr_ctrls = NULL;
1361                                         slap_send_syncinfo( sop, rs,
1362                                                 LDAP_TAG_SYNC_REFRESH_PRESENT, &cookie, 1, NULL, 0 );
1363
1364                                 } else {
1365                                         if ( !no_sync_state_change ) {
1366                                                 int slog_found = 0;
1367                                                 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1368                                                 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1369                                                         o_ps_link )
1370                                                 {
1371                                                         if ( ps_list->o_sync_slog_size > 0 ) {
1372                                                                 if ( ps_list->o_sync_state.sid ==
1373                                                                         sop->o_sync_state.sid )
1374                                                                 {
1375                                                                         slog_found = 1;
1376                                                                         break;
1377                                                                 }
1378                                                         }
1379                                                 }
1380                 
1381                                                 if ( slog_found ) {
1382                                                         rs->sr_err = LDAP_SUCCESS;
1383                                                         rs->sr_rspoid = NULL;
1384                                                         rs->sr_ctrls = NULL;
1385                                                         slap_send_session_log( op, ps_list, rs );
1386                                                 }
1387                                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1388                                         }
1389
1390                                         rs->sr_err = LDAP_SUCCESS;
1391                                         rs->sr_rspoid = LDAP_SYNC_INFO;
1392                                         rs->sr_ctrls = NULL;
1393                                         slap_send_syncinfo( sop, rs,
1394                                                 LDAP_TAG_SYNC_REFRESH_DELETE, &cookie, 1, NULL, 0 );
1395                                 }
1396
1397                                 if ( cookie.bv_val ) ch_free( cookie.bv_val );
1398
1399                         } else {
1400                                 /* refreshOnly mode */
1401                                 struct berval cookie;
1402                                 slap_compose_sync_cookie( sop, &cookie, search_context_csn,
1403                                         sop->o_sync_state.sid, sop->o_sync_state.rid );
1404
1405                                 if ( sync_send_present_mode ) {
1406                                         slap_build_sync_done_ctrl( sop, rs, ctrls,
1407                                                 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1408
1409                                 } else {
1410                                         if ( !no_sync_state_change ) {
1411                                                 int slog_found = 0;
1412                                                 ldap_pvt_thread_rdwr_rlock( &bdb->bi_pslist_rwlock );
1413                                                 LDAP_LIST_FOREACH( ps_list, &bdb->bi_psearch_list,
1414                                                         o_ps_link )
1415                                                 {
1416                                                         if ( ps_list->o_sync_slog_size > 0 ) {
1417                                                                 if ( ps_list->o_sync_state.sid ==
1418                                                                                 sop->o_sync_state.sid ) {
1419                                                                         slog_found = 1;
1420                                                                         break;
1421                                                                 }
1422                                                         }
1423                                                 }
1424                 
1425                                                 if ( slog_found ) {
1426                                                         slap_send_session_log( op, ps_list, rs );
1427                                                 }
1428                                                 ldap_pvt_thread_rdwr_runlock( &bdb->bi_pslist_rwlock );
1429                                         }
1430
1431                                         slap_build_sync_done_ctrl( sop, rs, ctrls,
1432                                                 num_ctrls++, 1, &cookie, LDAP_SYNC_REFRESH_DELETES );
1433                                 }
1434
1435                                 rs->sr_ctrls = ctrls;
1436                                 rs->sr_ref = rs->sr_v2ref;
1437                                 rs->sr_err = (rs->sr_v2ref == NULL)
1438                                         ? LDAP_SUCCESS : LDAP_REFERRAL;
1439                                 rs->sr_rspoid = NULL;
1440                                 send_ldap_result( sop, rs );
1441                                 if ( ctrls[num_ctrls-1]->ldctl_value.bv_val != NULL ) {
1442                                         slap_sl_free( ctrls[num_ctrls-1]->ldctl_value.bv_val,
1443                                                 sop->o_tmpmemctx );
1444                                 }
1445                                 slap_sl_free( ctrls[--num_ctrls], sop->o_tmpmemctx );
1446                                 ctrls[num_ctrls] = NULL;
1447                                 if ( cookie.bv_val ) ch_free( cookie.bv_val );  
1448                         }
1449
1450                 } else {
1451                         rs->sr_ctrls = NULL;
1452                         rs->sr_ref = rs->sr_v2ref;
1453                         rs->sr_err = (rs->sr_v2ref == NULL) ? LDAP_SUCCESS : LDAP_REFERRAL;
1454                         rs->sr_rspoid = NULL;
1455                         send_ldap_result( sop, rs );
1456                 }
1457         }
1458
1459         rs->sr_err = LDAP_SUCCESS;
1460
1461 done:
1462         if( !IS_PSEARCH && e != NULL ) {
1463                 /* free reader lock */
1464                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
1465         }
1466
1467         LOCK_ID_FREE( bdb->bi_dbenv, locker );
1468
1469         ber_bvfree( search_context_csn );
1470
1471         if( rs->sr_v2ref ) {
1472                 ber_bvarray_free( rs->sr_v2ref );
1473                 rs->sr_v2ref = NULL;
1474         }
1475         if( realbase.bv_val ) ch_free( realbase.bv_val );
1476
1477         return rs->sr_err;
1478 }
1479
1480
1481 static int base_candidate(
1482         BackendDB       *be,
1483         Entry   *e,
1484         ID              *ids )
1485 {
1486 #ifdef NEW_LOGGING
1487         LDAP_LOG ( OPERATION, ENTRY,
1488                 "base_candidate: base: \"%s\" (0x%08lx)\n",
1489                 e->e_nname.bv_val, (long) e->e_id, 0);
1490 #else
1491         Debug(LDAP_DEBUG_ARGS, "base_candidates: base: \"%s\" (0x%08lx)\n",
1492                 e->e_nname.bv_val, (long) e->e_id, 0);
1493 #endif
1494
1495         ids[0] = 1;
1496         ids[1] = e->e_id;
1497         return 0;
1498 }
1499
1500 /* Look for "objectClass Present" in this filter.
1501  * Also count depth of filter tree while we're at it.
1502  */
1503 static int oc_filter(
1504         Filter *f,
1505         int cur,
1506         int *max )
1507 {
1508         int rc = 0;
1509
1510         assert( f );
1511
1512         if( cur > *max ) *max = cur;
1513
1514         switch( f->f_choice ) {
1515         case LDAP_FILTER_PRESENT:
1516                 if (f->f_desc == slap_schema.si_ad_objectClass) {
1517                         rc = 1;
1518                 }
1519                 break;
1520
1521         case LDAP_FILTER_AND:
1522         case LDAP_FILTER_OR:
1523                 cur++;
1524                 for ( f=f->f_and; f; f=f->f_next ) {
1525                         (void) oc_filter(f, cur, max);
1526                 }
1527                 break;
1528
1529         default:
1530                 break;
1531         }
1532         return rc;
1533 }
1534
1535 static void search_stack_free( void *key, void *data )
1536 {
1537         ber_memfree_x(data, NULL);
1538 }
1539
1540 static void *search_stack( Operation *op )
1541 {
1542         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1543         void *ret = NULL;
1544
1545         if ( op->o_threadctx ) {
1546                 ldap_pvt_thread_pool_getkey( op->o_threadctx, search_stack,
1547                         &ret, NULL );
1548         } else {
1549                 ret = bdb->bi_search_stack;
1550         }
1551
1552         if ( !ret ) {
1553                 ret = ch_malloc( bdb->bi_search_stack_depth * BDB_IDL_UM_SIZE
1554                         * sizeof( ID ) );
1555                 if ( op->o_threadctx ) {
1556                         ldap_pvt_thread_pool_setkey( op->o_threadctx, search_stack,
1557                                 ret, search_stack_free );
1558                 } else {
1559                         bdb->bi_search_stack = ret;
1560                 }
1561         }
1562         return ret;
1563 }
1564
1565 static int search_candidates(
1566         Operation *stackop,
1567         Operation *op,
1568         SlapReply *rs,
1569         Entry *e,
1570         u_int32_t locker,
1571         ID      *ids,
1572         ID      *scopes )
1573 {
1574         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
1575         int rc, depth = 1;
1576         Filter          f, rf, xf, nf;
1577         ID              *stack;
1578         AttributeAssertion aa_ref;
1579 #ifdef BDB_SUBENTRIES
1580         Filter  sf;
1581         AttributeAssertion aa_subentry;
1582 #endif
1583
1584         /*
1585          * This routine takes as input a filter (user-filter)
1586          * and rewrites it as follows:
1587          *      (&(scope=DN)[(objectClass=subentry)]
1588          *              (|[(objectClass=referral)(objectClass=alias)](user-filter))
1589          */
1590
1591 #ifdef NEW_LOGGING
1592         LDAP_LOG ( OPERATION, ENTRY,
1593                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n", 
1594                 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope);
1595 #else
1596         Debug(LDAP_DEBUG_TRACE,
1597                 "search_candidates: base=\"%s\" (0x%08lx) scope=%d\n",
1598                 e->e_nname.bv_val, (long) e->e_id, op->oq_search.rs_scope );
1599 #endif
1600
1601         xf.f_or = op->oq_search.rs_filter;
1602         xf.f_choice = LDAP_FILTER_OR;
1603         xf.f_next = NULL;
1604
1605         /* If the user's filter uses objectClass=*,
1606          * these clauses are redundant.
1607          */
1608         if (!oc_filter(op->oq_search.rs_filter, 1, &depth)
1609                 && !get_subentries_visibility(op)
1610                 && !is_sync_protocol(op) )
1611         {
1612                 if( !get_manageDSAit(op) && !get_domainScope(op) ) {
1613                         /* match referral objects */
1614                         struct berval bv_ref = { sizeof("referral")-1, "referral" };
1615                         rf.f_choice = LDAP_FILTER_EQUALITY;
1616                         rf.f_ava = &aa_ref;
1617                         rf.f_av_desc = slap_schema.si_ad_objectClass;
1618                         rf.f_av_value = bv_ref;
1619                         rf.f_next = xf.f_or;
1620                         xf.f_or = &rf;
1621                         depth++;
1622                 }
1623         }
1624
1625         f.f_next = NULL;
1626         f.f_choice = LDAP_FILTER_AND;
1627         f.f_and = &nf;
1628         /* Dummy; we compute scope separately now */
1629         nf.f_choice = SLAPD_FILTER_COMPUTED;
1630         nf.f_result = LDAP_SUCCESS;
1631         nf.f_next = ( xf.f_or == op->oq_search.rs_filter )
1632                 ? op->oq_search.rs_filter : &xf ;
1633         /* Filter depth increased again, adding dummy clause */
1634         depth++;
1635
1636 #ifdef BDB_SUBENTRIES
1637         if( get_subentries_visibility( op ) ) {
1638                 struct berval bv_subentry = { sizeof("SUBENTRY")-1, "SUBENTRY" };
1639                 sf.f_choice = LDAP_FILTER_EQUALITY;
1640                 sf.f_ava = &aa_subentry;
1641                 sf.f_av_desc = slap_schema.si_ad_objectClass;
1642                 sf.f_av_value = bv_subentry;
1643                 sf.f_next = nf.f_next;
1644                 nf.f_next = &sf;
1645         }
1646 #endif
1647
1648         /* Allocate IDL stack, plus 1 more for former tmp */
1649         if ( depth+1 > bdb->bi_search_stack_depth ) {
1650                 stack = ch_malloc( (depth + 1) * BDB_IDL_UM_SIZE * sizeof( ID ) );
1651         } else {
1652                 stack = search_stack( stackop );
1653         }
1654
1655         if( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1656                 rc = search_aliases( op, rs, e, locker, ids, scopes, stack );
1657         } else {
1658                 rc = bdb_dn2idl( op, e, ids, stack );
1659         }
1660
1661         if ( rc == LDAP_SUCCESS ) {
1662                 rc = bdb_filter_candidates( op, &f, ids,
1663                         stack, stack+BDB_IDL_UM_SIZE );
1664         }
1665
1666         if ( depth+1 > bdb->bi_search_stack_depth ) {
1667                 ch_free( stack );
1668         }
1669
1670         if( rc ) {
1671 #ifdef NEW_LOGGING
1672                 LDAP_LOG ( OPERATION, DETAIL1,
1673                         "bdb_search_candidates: failed (rc=%d)\n", rc, 0, 0  );
1674 #else
1675                 Debug(LDAP_DEBUG_TRACE,
1676                         "bdb_search_candidates: failed (rc=%d)\n",
1677                         rc, NULL, NULL );
1678 #endif
1679
1680         } else {
1681 #ifdef NEW_LOGGING
1682                 LDAP_LOG ( OPERATION, DETAIL1,
1683                         "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1684                         (long) ids[0], (long) BDB_IDL_FIRST(ids), 
1685                         (long) BDB_IDL_LAST(ids));
1686 #else
1687                 Debug(LDAP_DEBUG_TRACE,
1688                         "bdb_search_candidates: id=%ld first=%ld last=%ld\n",
1689                         (long) ids[0],
1690                         (long) BDB_IDL_FIRST(ids),
1691                         (long) BDB_IDL_LAST(ids) );
1692 #endif
1693         }
1694
1695         return rc;
1696 }
1697
1698 static void
1699 send_paged_response( 
1700         Operation       *op,
1701         SlapReply       *rs,
1702         ID              lastid,
1703         int             tentries )
1704 {
1705         LDAPControl     ctrl, *ctrls[2];
1706         BerElementBuffer berbuf;
1707         BerElement      *ber = (BerElement *)&berbuf;
1708         struct berval   cookie = BER_BVNULL;
1709         PagedResultsCookie respcookie;
1710
1711 #ifdef NEW_LOGGING
1712         LDAP_LOG ( OPERATION, ENTRY,
1713                 "send_paged_response: lastid: (0x%08lx) "
1714                 "nentries: (0x%081x)\n", 
1715                 lastid, rs->sr_nentries, NULL );
1716 #else
1717         Debug(LDAP_DEBUG_ARGS, "send_paged_response: lastid: (0x%08lx) "
1718                 "nentries: (0x%081x)\n", lastid, rs->sr_nentries, NULL );
1719 #endif
1720
1721         ctrl.ldctl_value.bv_val = NULL;
1722         ctrls[0] = &ctrl;
1723         ctrls[1] = NULL;
1724
1725         ber_init2( ber, NULL, LBER_USE_DER );
1726
1727         respcookie = ( PagedResultsCookie )lastid;
1728         op->o_conn->c_pagedresults_state.ps_cookie = respcookie;
1729         op->o_conn->c_pagedresults_state.ps_count =
1730                 op->o_pagedresults_state.ps_count + rs->sr_nentries;
1731         cookie.bv_len = sizeof( respcookie );
1732         cookie.bv_val = (char *)&respcookie;
1733
1734         /*
1735          * FIXME: we should consider sending an estimate of the entries
1736          * left, after appropriate security check is done
1737          */
1738         ber_printf( ber, "{iO}", tentries, &cookie ); 
1739
1740         if ( ber_flatten2( ber, &ctrls[0]->ldctl_value, 0 ) == -1 ) {
1741                 goto done;
1742         }
1743
1744         ctrls[0]->ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
1745         ctrls[0]->ldctl_iscritical = 0;
1746
1747         rs->sr_ctrls = ctrls;
1748         rs->sr_err = LDAP_SUCCESS;
1749         send_ldap_result( op, rs );
1750
1751 done:
1752         (void) ber_free_buf( ber );
1753 }
1754