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