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