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