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