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