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