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