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