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