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