]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/syncprov.c
ITS#3549 acknowledge CANCEL requests
[openldap] / servers / slapd / overlays / syncprov.c
1 /* $OpenLDAP$ */
2 /* syncprov.c - syncrepl provider */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2004-2005 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 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Howard Chu for inclusion in
18  * OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #ifdef SLAPD_OVER_SYNCPROV
24
25 #include <ac/string.h>
26 #include "lutil.h"
27 #include "slap.h"
28
29 /* A modify request on a particular entry */
30 typedef struct modinst {
31         struct modinst *mi_next;
32         Operation *mi_op;
33 } modinst;
34
35 typedef struct modtarget {
36         struct modinst *mt_mods;
37         struct modinst *mt_tail;
38         Operation *mt_op;
39         ldap_pvt_thread_mutex_t mt_mutex;
40 } modtarget;
41
42 /* A queued result of a persistent search */
43 typedef struct syncres {
44         struct syncres *s_next;
45         struct berval s_dn;
46         struct berval s_ndn;
47         struct berval s_uuid;
48         struct berval s_csn;
49         char s_mode;
50         char s_isreference;
51 } syncres;
52
53 /* Record of a persistent search */
54 typedef struct syncops {
55         struct syncops *s_next;
56         struct berval   s_base;         /* ndn of search base */
57         ID              s_eid;          /* entryID of search base */
58         Operation       *s_op;          /* search op */
59         int             s_rid;
60         struct berval s_filterstr;
61         int             s_flags;        /* search status */
62         int             s_inuse;        /* reference count */
63         struct syncres *s_res;
64         struct syncres *s_restail;
65         ldap_pvt_thread_mutex_t s_mutex;
66 } syncops;
67
68 /* A received sync control */
69 typedef struct sync_control {
70         struct sync_cookie sr_state;
71         int sr_rhint;
72 } sync_control;
73
74 #if 0 /* moved back to slap.h */
75 #define o_sync  o_ctrlflag[slap_cids.sc_LDAPsync]
76 #endif
77 /* o_sync_mode uses data bits of o_sync */
78 #define o_sync_mode     o_ctrlflag[slap_cids.sc_LDAPsync]
79
80 #define SLAP_SYNC_NONE                                  (LDAP_SYNC_NONE<<SLAP_CONTROL_SHIFT)
81 #define SLAP_SYNC_REFRESH                               (LDAP_SYNC_REFRESH_ONLY<<SLAP_CONTROL_SHIFT)
82 #define SLAP_SYNC_PERSIST                               (LDAP_SYNC_RESERVED<<SLAP_CONTROL_SHIFT)
83 #define SLAP_SYNC_REFRESH_AND_PERSIST   (LDAP_SYNC_REFRESH_AND_PERSIST<<SLAP_CONTROL_SHIFT)
84
85 #define PS_IS_REFRESHING        0x01
86 #define PS_IS_DETACHED          0x02
87
88 /* Record of which searches matched at premodify step */
89 typedef struct syncmatches {
90         struct syncmatches *sm_next;
91         syncops *sm_op;
92 } syncmatches;
93
94 /* Session log data */
95 typedef struct slog_entry {
96         struct slog_entry *se_next;
97         struct berval se_uuid;
98         struct berval se_csn;
99         ber_tag_t       se_tag;
100 } slog_entry;
101
102 typedef struct sessionlog {
103         struct berval   sl_mincsn;
104         int             sl_num;
105         int             sl_size;
106         slog_entry *sl_head;
107         slog_entry *sl_tail;
108         ldap_pvt_thread_mutex_t sl_mutex;
109 } sessionlog;
110
111 /* The main state for this overlay */
112 typedef struct syncprov_info_t {
113         syncops         *si_ops;
114         struct berval   si_ctxcsn;      /* ldapsync context */
115         int             si_chkops;      /* checkpointing info */
116         int             si_chktime;
117         int             si_numops;      /* number of ops since last checkpoint */
118         time_t  si_chklast;     /* time of last checkpoint */
119         Avlnode *si_mods;       /* entries being modified */
120         sessionlog      *si_logs;
121         ldap_pvt_thread_mutex_t si_csn_mutex;
122         ldap_pvt_thread_mutex_t si_ops_mutex;
123         ldap_pvt_thread_mutex_t si_mods_mutex;
124         char            si_ctxcsnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
125 } syncprov_info_t;
126
127 typedef struct opcookie {
128         slap_overinst *son;
129         syncmatches *smatches;
130         struct berval sdn;      /* DN of entry, for deletes */
131         struct berval sndn;
132         struct berval suuid;    /* UUID of entry */
133         struct berval sctxcsn;
134         int sreference; /* Is the entry a reference? */
135 } opcookie;
136
137 typedef struct fbase_cookie {
138         struct berval *fdn;     /* DN of a modified entry, for scope testing */
139         syncops *fss;   /* persistent search we're testing against */
140         int fbase;      /* if TRUE we found the search base and it's still valid */
141         int fscope;     /* if TRUE then fdn is within the psearch scope */
142 } fbase_cookie;
143
144 static AttributeName csn_anlist[2];
145 static AttributeName uuid_anlist[2];
146
147 /* Build a LDAPsync intermediate state control */
148 static int
149 syncprov_state_ctrl(
150         Operation       *op,
151         SlapReply       *rs,
152         Entry           *e,
153         int             entry_sync_state,
154         LDAPControl     **ctrls,
155         int             num_ctrls,
156         int             send_cookie,
157         struct berval   *cookie )
158 {
159         Attribute* a;
160         int ret;
161         int res;
162         const char *text = NULL;
163
164         BerElementBuffer berbuf;
165         BerElement *ber = (BerElement *)&berbuf;
166
167         struct berval   entryuuid_bv = BER_BVNULL;
168
169         ber_init2( ber, 0, LBER_USE_DER );
170         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
171
172         ctrls[num_ctrls] = op->o_tmpalloc( sizeof ( LDAPControl ), op->o_tmpmemctx );
173
174         for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
175                 AttributeDescription *desc = a->a_desc;
176                 if ( desc == slap_schema.si_ad_entryUUID ) {
177                         entryuuid_bv = a->a_nvals[0];
178                         break;
179                 }
180         }
181
182         if ( send_cookie && cookie ) {
183                 ber_printf( ber, "{eOON}",
184                         entry_sync_state, &entryuuid_bv, cookie );
185         } else {
186                 ber_printf( ber, "{eON}",
187                         entry_sync_state, &entryuuid_bv );
188         }
189
190         ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
191         ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
192         ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
193
194         ber_free_buf( ber );
195
196         if ( ret < 0 ) {
197                 Debug( LDAP_DEBUG_TRACE,
198                         "slap_build_sync_ctrl: ber_flatten2 failed\n",
199                         0, 0, 0 );
200                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
201                 return ret;
202         }
203
204         return LDAP_SUCCESS;
205 }
206
207 /* Build a LDAPsync final state control */
208 static int
209 syncprov_done_ctrl(
210         Operation       *op,
211         SlapReply       *rs,
212         LDAPControl     **ctrls,
213         int                     num_ctrls,
214         int                     send_cookie,
215         struct berval *cookie,
216         int                     refreshDeletes )
217 {
218         int ret;
219         BerElementBuffer berbuf;
220         BerElement *ber = (BerElement *)&berbuf;
221
222         ber_init2( ber, NULL, LBER_USE_DER );
223         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
224
225         ctrls[num_ctrls] = op->o_tmpalloc( sizeof ( LDAPControl ), op->o_tmpmemctx );
226
227         ber_printf( ber, "{" );
228         if ( send_cookie && cookie ) {
229                 ber_printf( ber, "O", cookie );
230         }
231         if ( refreshDeletes == LDAP_SYNC_REFRESH_DELETES ) {
232                 ber_printf( ber, "b", refreshDeletes );
233         }
234         ber_printf( ber, "N}" );
235
236         ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
237         ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
238         ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
239
240         ber_free_buf( ber );
241
242         if ( ret < 0 ) {
243                 Debug( LDAP_DEBUG_TRACE,
244                         "syncprov_done_ctrl: ber_flatten2 failed\n",
245                         0, 0, 0 );
246                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
247                 return ret;
248         }
249
250         return LDAP_SUCCESS;
251 }
252
253 #if 0
254 /* Generate state based on session log - not implemented yet */
255 static int
256 syncprov_state_ctrl_from_slog(
257         Operation       *op,
258         SlapReply       *rs,
259         struct slog_entry *slog_e,
260         int                     entry_sync_state,
261         LDAPControl     **ctrls,
262         int                     num_ctrls,
263         int                     send_cookie,
264         struct berval   *cookie)
265 {
266         Attribute* a;
267         int ret;
268         int res;
269         const char *text = NULL;
270
271         BerElementBuffer berbuf;
272         BerElement *ber = (BerElement *)&berbuf;
273
274         struct berval entryuuid_bv      = BER_BVNULL;
275
276         ber_init2( ber, NULL, LBER_USE_DER );
277         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
278
279         ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
280
281         entryuuid_bv = slog_e->sl_uuid;
282
283         if ( send_cookie && cookie ) {
284                 ber_printf( ber, "{eOON}",
285                         entry_sync_state, &entryuuid_bv, cookie );
286         } else {
287                 ber_printf( ber, "{eON}",
288                         entry_sync_state, &entryuuid_bv );
289         }
290
291         ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
292         ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
293         ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
294
295         ber_free_buf( ber );
296
297         if ( ret < 0 ) {
298                 Debug( LDAP_DEBUG_TRACE,
299                         "slap_build_sync_ctrl: ber_flatten2 failed\n",
300                         0, 0, 0 );
301                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
302                 return ret;
303         }
304
305         return LDAP_SUCCESS;
306 }
307 #endif
308
309 static int
310 syncprov_sendinfo(
311         Operation       *op,
312         SlapReply       *rs,
313         int                     type,
314         struct berval *cookie,
315         int                     refreshDone,
316         BerVarray       syncUUIDs,
317         int                     refreshDeletes )
318 {
319         BerElementBuffer berbuf;
320         BerElement *ber = (BerElement *)&berbuf;
321         struct berval rspdata;
322
323         int ret;
324
325         ber_init2( ber, NULL, LBER_USE_DER );
326         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
327
328         if ( type ) {
329                 switch ( type ) {
330                 case LDAP_TAG_SYNC_NEW_COOKIE:
331                         ber_printf( ber, "tO", type, cookie );
332                         break;
333                 case LDAP_TAG_SYNC_REFRESH_DELETE:
334                 case LDAP_TAG_SYNC_REFRESH_PRESENT:
335                         ber_printf( ber, "t{", type );
336                         if ( cookie ) {
337                                 ber_printf( ber, "O", cookie );
338                         }
339                         if ( refreshDone == 0 ) {
340                                 ber_printf( ber, "b", refreshDone );
341                         }
342                         ber_printf( ber, "N}" );
343                         break;
344                 case LDAP_TAG_SYNC_ID_SET:
345                         ber_printf( ber, "t{", type );
346                         if ( cookie ) {
347                                 ber_printf( ber, "O", cookie );
348                         }
349                         if ( refreshDeletes == 1 ) {
350                                 ber_printf( ber, "b", refreshDeletes );
351                         }
352                         ber_printf( ber, "[W]", syncUUIDs );
353                         ber_printf( ber, "N}" );
354                         break;
355                 default:
356                         Debug( LDAP_DEBUG_TRACE,
357                                 "syncprov_sendinfo: invalid syncinfo type (%d)\n",
358                                 type, 0, 0 );
359                         return LDAP_OTHER;
360                 }
361         }
362
363         ret = ber_flatten2( ber, &rspdata, 0 );
364
365         if ( ret < 0 ) {
366                 Debug( LDAP_DEBUG_TRACE,
367                         "syncprov_sendinfo: ber_flatten2 failed\n",
368                         0, 0, 0 );
369                 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
370                 return ret;
371         }
372
373         rs->sr_rspoid = LDAP_SYNC_INFO;
374         rs->sr_rspdata = &rspdata;
375         send_ldap_intermediate( op, rs );
376         rs->sr_rspdata = NULL;
377         ber_free_buf( ber );
378
379         return LDAP_SUCCESS;
380 }
381
382 /* Find a modtarget in an AVL tree */
383 static int
384 sp_avl_cmp( const void *c1, const void *c2 )
385 {
386         const modtarget *m1, *m2;
387         int rc;
388
389         m1 = c1; m2 = c2;
390         rc = m1->mt_op->o_req_ndn.bv_len - m2->mt_op->o_req_ndn.bv_len;
391
392         if ( rc ) return rc;
393         return ber_bvcmp( &m1->mt_op->o_req_ndn, &m2->mt_op->o_req_ndn );
394 }
395
396 /* syncprov_findbase:
397  *   finds the true DN of the base of a search (with alias dereferencing) and
398  * checks to make sure the base entry doesn't get replaced with a different
399  * entry (e.g., swapping trees via ModDN, or retargeting an alias). If a
400  * change is detected, any persistent search on this base must be terminated /
401  * reloaded.
402  *   On the first call, we just save the DN and entryID. On subsequent calls
403  * we compare the DN and entryID with the saved values.
404  */
405 static int
406 findbase_cb( Operation *op, SlapReply *rs )
407 {
408         slap_callback *sc = op->o_callback;
409
410         if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
411                 fbase_cookie *fc = sc->sc_private;
412
413                 /* If no entryID, we're looking for the first time.
414                  * Just store whatever we got.
415                  */
416                 if ( fc->fss->s_eid == NOID ) {
417                         fc->fbase = 1;
418                         fc->fss->s_eid = rs->sr_entry->e_id;
419                         ber_dupbv( &fc->fss->s_base, &rs->sr_entry->e_nname );
420
421                 } else if ( rs->sr_entry->e_id == fc->fss->s_eid &&
422                         dn_match( &rs->sr_entry->e_nname, &fc->fss->s_base )) {
423
424                 /* OK, the DN is the same and the entryID is the same. Now
425                  * see if the fdn resides in the scope.
426                  */
427                         fc->fbase = 1;
428                         switch ( fc->fss->s_op->ors_scope ) {
429                         case LDAP_SCOPE_BASE:
430                                 fc->fscope = dn_match( fc->fdn, &rs->sr_entry->e_nname );
431                                 break;
432                         case LDAP_SCOPE_ONELEVEL: {
433                                 struct berval pdn;
434                                 dnParent( fc->fdn, &pdn );
435                                 fc->fscope = dn_match( &pdn, &rs->sr_entry->e_nname );
436                                 break; }
437                         case LDAP_SCOPE_SUBTREE:
438                                 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname );
439                                 break;
440 #ifdef LDAP_SCOPE_SUBORDINATE
441                         case LDAP_SCOPE_SUBORDINATE:
442                                 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname ) &&
443                                         !dn_match( fc->fdn, &rs->sr_entry->e_nname );
444                                 break;
445 #endif
446                         }
447                 }
448         }
449         if ( rs->sr_err != LDAP_SUCCESS ) {
450                 Debug( LDAP_DEBUG_ANY, "findbase failed! %d\n", rs->sr_err,0,0 );
451         }
452         return LDAP_SUCCESS;
453 }
454
455 static int
456 syncprov_findbase( Operation *op, fbase_cookie *fc )
457 {
458         opcookie *opc = op->o_callback->sc_private;
459         slap_overinst *on = opc->son;
460         syncprov_info_t         *si = on->on_bi.bi_private;
461
462         slap_callback cb = {0};
463         Operation fop;
464         SlapReply frs = { REP_RESULT };
465         int rc;
466
467         fop = *op;
468
469         cb.sc_response = findbase_cb;
470         cb.sc_private = fc;
471
472         fop.o_sync_mode &= SLAP_CONTROL_MASK;   /* turn off sync mode */
473         fop.o_callback = &cb;
474         fop.o_tag = LDAP_REQ_SEARCH;
475         fop.ors_scope = LDAP_SCOPE_BASE;
476         fop.ors_deref = fc->fss->s_op->ors_deref;
477         fop.ors_limit = NULL;
478         fop.ors_slimit = 1;
479         fop.ors_tlimit = SLAP_NO_LIMIT;
480         fop.ors_attrs = slap_anlist_no_attrs;
481         fop.ors_attrsonly = 1;
482         fop.ors_filter = fc->fss->s_op->ors_filter;
483         fop.ors_filterstr = fc->fss->s_op->ors_filterstr;
484
485         fop.o_req_ndn = fc->fss->s_op->o_req_ndn;
486
487         fop.o_bd->bd_info = on->on_info->oi_orig;
488         rc = fop.o_bd->be_search( &fop, &frs );
489         fop.o_bd->bd_info = (BackendInfo *)on;
490
491         if ( fc->fbase ) return LDAP_SUCCESS;
492
493         /* If entryID has changed, then the base of this search has
494          * changed. Invalidate the psearch.
495          */
496         return LDAP_NO_SUCH_OBJECT;
497 }
498
499 /* syncprov_findcsn:
500  *   This function has three different purposes, but they all use a search
501  * that filters on entryCSN so they're combined here.
502  * 1: at startup time, after a contextCSN has been read from the database,
503  * we search for all entries with CSN >= contextCSN in case the contextCSN
504  * was not checkpointed at the previous shutdown.
505  *
506  * 2: when the current contextCSN is known and we have a sync cookie, we search
507  * for one entry with CSN <= the cookie CSN. (Used to search for =.) If an
508  * entry is found, the cookie CSN is valid, otherwise it is stale.
509  *
510  * 3: during a refresh phase, we search for all entries with CSN <= the cookie
511  * CSN, and generate Present records for them. We always collect this result
512  * in SyncID sets, even if there's only one match.
513  */
514 #define FIND_MAXCSN     1
515 #define FIND_CSN        2
516 #define FIND_PRESENT    3
517
518 static int
519 findmax_cb( Operation *op, SlapReply *rs )
520 {
521         if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
522                 struct berval *maxcsn = op->o_callback->sc_private;
523                 Attribute *a = attr_find( rs->sr_entry->e_attrs,
524                         slap_schema.si_ad_entryCSN );
525
526                 if ( a && ber_bvcmp( &a->a_vals[0], maxcsn ) > 0 ) {
527                         maxcsn->bv_len = a->a_vals[0].bv_len;
528                         strcpy( maxcsn->bv_val, a->a_vals[0].bv_val );
529                 }
530         }
531         return LDAP_SUCCESS;
532 }
533
534 static int
535 findcsn_cb( Operation *op, SlapReply *rs )
536 {
537         slap_callback *sc = op->o_callback;
538
539         if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
540                 sc->sc_private = (void *)1;
541         }
542         return LDAP_SUCCESS;
543 }
544
545 /* Build a list of entryUUIDs for sending in a SyncID set */
546
547 #define UUID_LEN        16
548
549 typedef struct fpres_cookie {
550         int num;
551         BerVarray uuids;
552         char *last;
553 } fpres_cookie;
554
555 static int
556 findpres_cb( Operation *op, SlapReply *rs )
557 {
558         slap_callback *sc = op->o_callback;
559         fpres_cookie *pc = sc->sc_private;
560         Attribute *a;
561         int ret = SLAP_CB_CONTINUE;
562
563         switch ( rs->sr_type ) {
564         case REP_SEARCH:
565                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
566                 if ( a ) {
567                         pc->uuids[pc->num].bv_val = pc->last;
568                         AC_MEMCPY( pc->uuids[pc->num].bv_val, a->a_nvals[0].bv_val,
569                                 pc->uuids[pc->num].bv_len );
570                         pc->num++;
571                         pc->last = pc->uuids[pc->num].bv_val;
572                         pc->uuids[pc->num].bv_val = NULL;
573                 }
574                 ret = LDAP_SUCCESS;
575                 if ( pc->num != SLAP_SYNCUUID_SET_SIZE )
576                         break;
577                 /* FALLTHRU */
578         case REP_RESULT:
579                 ret = rs->sr_err;
580                 if ( pc->num ) {
581                         ret = syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
582                                 0, pc->uuids, 0 );
583                         pc->uuids[pc->num].bv_val = pc->last;
584                         pc->num = 0;
585                         pc->last = pc->uuids[0].bv_val;
586                 }
587                 break;
588         default:
589                 break;
590         }
591         return ret;
592 }
593
594 static int
595 syncprov_findcsn( Operation *op, int mode )
596 {
597         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
598         syncprov_info_t         *si = on->on_bi.bi_private;
599
600         slap_callback cb = {0};
601         Operation fop;
602         SlapReply frs = { REP_RESULT };
603         char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
604         char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
605         struct berval fbuf, maxcsn;
606         Filter cf, af;
607         AttributeAssertion eq;
608         int i, rc = LDAP_SUCCESS;
609         fpres_cookie pcookie;
610         sync_control *srs = NULL;
611
612         if ( mode != FIND_MAXCSN ) {
613                 srs = op->o_controls[slap_cids.sc_LDAPsync];
614
615                 if ( srs->sr_state.ctxcsn.bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
616                         return LDAP_OTHER;
617                 }
618         }
619
620         fop = *op;
621         fop.o_sync_mode &= SLAP_CONTROL_MASK;   /* turn off sync_mode */
622
623         fbuf.bv_val = buf;
624         cf.f_ava = &eq;
625         cf.f_av_desc = slap_schema.si_ad_entryCSN;
626         cf.f_next = NULL;
627
628         fop.o_callback = &cb;
629         fop.ors_limit = NULL;
630         fop.ors_tlimit = SLAP_NO_LIMIT;
631         fop.ors_filter = &cf;
632         fop.ors_filterstr = fbuf;
633
634         switch( mode ) {
635         case FIND_MAXCSN:
636                 cf.f_choice = LDAP_FILTER_GE;
637                 cf.f_av_value = si->si_ctxcsn;
638                 fbuf.bv_len = sprintf( buf, "(entryCSN>=%s)",
639                         cf.f_av_value.bv_val );
640                 fop.ors_attrsonly = 0;
641                 fop.ors_attrs = csn_anlist;
642                 fop.ors_slimit = SLAP_NO_LIMIT;
643                 cb.sc_private = &maxcsn;
644                 cb.sc_response = findmax_cb;
645                 strcpy( cbuf, si->si_ctxcsn.bv_val );
646                 maxcsn.bv_val = cbuf;
647                 maxcsn.bv_len = si->si_ctxcsn.bv_len;
648                 break;
649         case FIND_CSN:
650                 cf.f_choice = LDAP_FILTER_LE;
651                 cf.f_av_value = srs->sr_state.ctxcsn;
652                 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)",
653                         cf.f_av_value.bv_val );
654                 fop.ors_attrsonly = 1;
655                 fop.ors_attrs = slap_anlist_no_attrs;
656                 fop.ors_slimit = 1;
657                 cb.sc_private = NULL;
658                 cb.sc_response = findcsn_cb;
659                 break;
660         case FIND_PRESENT:
661                 af.f_choice = LDAP_FILTER_AND;
662                 af.f_next = NULL;
663                 af.f_and = &cf;
664                 cf.f_choice = LDAP_FILTER_LE;
665                 cf.f_av_value = srs->sr_state.ctxcsn;
666                 cf.f_next = op->ors_filter;
667                 fop.ors_filter = &af;
668                 filter2bv_x( &fop, fop.ors_filter, &fop.ors_filterstr );
669                 fop.ors_attrsonly = 0;
670                 fop.ors_attrs = uuid_anlist;
671                 fop.ors_slimit = SLAP_NO_LIMIT;
672                 /* We want pure entries, not referrals */
673                 fop.o_managedsait = SLAP_CONTROL_CRITICAL;
674                 cb.sc_private = &pcookie;
675                 cb.sc_response = findpres_cb;
676                 pcookie.num = 0;
677
678                 /* preallocate storage for a full set */
679                 pcookie.uuids = op->o_tmpalloc( (SLAP_SYNCUUID_SET_SIZE+1) *
680                         sizeof(struct berval) + SLAP_SYNCUUID_SET_SIZE * UUID_LEN,
681                         op->o_tmpmemctx );
682                 pcookie.last = (char *)(pcookie.uuids + SLAP_SYNCUUID_SET_SIZE+1);
683                 pcookie.uuids[0].bv_val = pcookie.last;
684                 pcookie.uuids[0].bv_len = UUID_LEN;
685                 for (i=1; i<SLAP_SYNCUUID_SET_SIZE; i++) {
686                         pcookie.uuids[i].bv_val = pcookie.uuids[i-1].bv_val + UUID_LEN;
687                         pcookie.uuids[i].bv_len = UUID_LEN;
688                 }
689                 break;
690         }
691
692         fop.o_bd->bd_info = on->on_info->oi_orig;
693         fop.o_bd->be_search( &fop, &frs );
694         fop.o_bd->bd_info = (BackendInfo *)on;
695
696         switch( mode ) {
697         case FIND_MAXCSN:
698                 strcpy( si->si_ctxcsnbuf, maxcsn.bv_val );
699                 si->si_ctxcsn.bv_len = maxcsn.bv_len;
700                 break;
701         case FIND_CSN:
702                 /* If matching CSN was not found, invalidate the context. */
703                 if ( !cb.sc_private ) rc = LDAP_NO_SUCH_OBJECT;
704                 break;
705         case FIND_PRESENT:
706                 op->o_tmpfree( pcookie.uuids, op->o_tmpmemctx );
707                 op->o_tmpfree( fop.ors_filterstr.bv_val, op->o_tmpmemctx );
708                 break;
709         }
710
711         return rc;
712 }
713
714 /* Queue a persistent search response if still in Refresh stage */
715 static int
716 syncprov_qresp( opcookie *opc, syncops *so, int mode )
717 {
718         syncres *sr;
719
720         sr = ch_malloc(sizeof(syncres) + opc->suuid.bv_len + 1 +
721                 opc->sdn.bv_len + 1 + opc->sndn.bv_len + 1 + opc->sctxcsn.bv_len + 1 );
722         sr->s_next = NULL;
723         sr->s_dn.bv_val = (char *)(sr + 1);
724         sr->s_mode = mode;
725         sr->s_isreference = opc->sreference;
726         sr->s_ndn.bv_val = lutil_strcopy( sr->s_dn.bv_val, opc->sdn.bv_val );
727         *(sr->s_ndn.bv_val++) = '\0';
728         sr->s_uuid.bv_val = lutil_strcopy( sr->s_ndn.bv_val, opc->sndn.bv_val );
729         *(sr->s_uuid.bv_val++) = '\0';
730         sr->s_csn.bv_val = lutil_strcopy( sr->s_uuid.bv_val, opc->suuid.bv_val );
731
732         if ( !so->s_res ) {
733                 so->s_res = sr;
734         } else {
735                 so->s_restail->s_next = sr;
736         }
737         so->s_restail = sr;
738         ldap_pvt_thread_mutex_unlock( &so->s_mutex );
739         return LDAP_SUCCESS;
740 }
741
742 /* Send a persistent search response */
743 static int
744 syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry *e, int mode, int queue )
745 {
746         slap_overinst *on = opc->son;
747         syncprov_info_t *si = on->on_bi.bi_private;
748
749         SlapReply rs = { REP_SEARCH };
750         LDAPControl *ctrls[2];
751         struct berval cookie;
752         Entry e_uuid = {0};
753         Attribute a_uuid = {0};
754         Operation sop = *so->s_op;
755         Opheader ohdr;
756
757         ohdr = *sop.o_hdr;
758         sop.o_hdr = &ohdr;
759         sop.o_tmpmemctx = op->o_tmpmemctx;
760         sop.o_bd = op->o_bd;
761         sop.o_controls = op->o_controls;
762
763         if ( queue && (so->s_flags & PS_IS_REFRESHING) ) {
764                 ldap_pvt_thread_mutex_lock( &so->s_mutex );
765                 if ( so->s_flags & PS_IS_REFRESHING )
766                         return syncprov_qresp( opc, so, mode );
767                 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
768         }
769
770         ctrls[1] = NULL;
771         slap_compose_sync_cookie( op, &cookie, &opc->sctxcsn, so->s_rid );
772
773         e_uuid.e_attrs = &a_uuid;
774         a_uuid.a_desc = slap_schema.si_ad_entryUUID;
775         a_uuid.a_nvals = &opc->suuid;
776         rs.sr_err = syncprov_state_ctrl( &sop, &rs, &e_uuid,
777                 mode, ctrls, 0, 1, &cookie );
778
779         rs.sr_entry = e;
780         rs.sr_ctrls = ctrls;
781         switch( mode ) {
782         case LDAP_SYNC_ADD:
783                 if ( opc->sreference ) {
784                         rs.sr_ref = get_entry_referrals( &sop, e );
785                         send_search_reference( &sop, &rs );
786                         ber_bvarray_free( rs.sr_ref );
787                         break;
788                 }
789                 /* fallthru */
790         case LDAP_SYNC_MODIFY:
791                 rs.sr_attrs = sop.ors_attrs;
792                 send_search_entry( &sop, &rs );
793                 break;
794         case LDAP_SYNC_DELETE:
795                 e_uuid.e_attrs = NULL;
796                 e_uuid.e_name = opc->sdn;
797                 e_uuid.e_nname = opc->sndn;
798                 rs.sr_entry = &e_uuid;
799                 if ( opc->sreference ) {
800                         struct berval bv = BER_BVNULL;
801                         rs.sr_ref = &bv;
802                         send_search_reference( &sop, &rs );
803                 } else {
804                         send_search_entry( &sop, &rs );
805                 }
806                 break;
807         default:
808                 assert(0);
809         }
810         op->o_tmpfree( rs.sr_ctrls[0], op->o_tmpmemctx );
811         rs.sr_ctrls = NULL;
812         return rs.sr_err;
813 }
814
815 static void
816 syncprov_free_syncop( syncops *so )
817 {
818         syncres *sr, *srnext;
819         GroupAssertion *ga, *gnext;
820
821         ldap_pvt_thread_mutex_lock( &so->s_mutex );
822         so->s_inuse--;
823         if ( so->s_inuse > 0 ) {
824                 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
825                 return;
826         }
827         ldap_pvt_thread_mutex_unlock( &so->s_mutex );
828         if ( so->s_flags & PS_IS_DETACHED ) {
829                 filter_free( so->s_op->ors_filter );
830                 for ( ga = so->s_op->o_groups; ga; ga=gnext ) {
831                         gnext = ga->ga_next;
832                         ch_free( ga );
833                 }
834                 ch_free( so->s_op );
835         }
836         ch_free( so->s_base.bv_val );
837         for ( sr=so->s_res; sr; sr=srnext ) {
838                 srnext = sr->s_next;
839                 ch_free( sr );
840         }
841         ldap_pvt_thread_mutex_destroy( &so->s_mutex );
842         ch_free( so );
843 }
844
845 static int
846 syncprov_drop_psearch( syncops *so, int lock )
847 {
848         if ( so->s_flags & PS_IS_DETACHED ) {
849                 if ( lock )
850                         ldap_pvt_thread_mutex_lock( &so->s_op->o_conn->c_mutex );
851                 so->s_op->o_conn->c_n_ops_executing--;
852                 so->s_op->o_conn->c_n_ops_completed++;
853                 LDAP_STAILQ_REMOVE( &so->s_op->o_conn->c_ops, so->s_op, slap_op,
854                         o_next );
855                 if ( lock )
856                         ldap_pvt_thread_mutex_unlock( &so->s_op->o_conn->c_mutex );
857         }
858         syncprov_free_syncop( so );
859
860         return 0;
861 }
862
863 static int
864 syncprov_op_abandon( Operation *op, SlapReply *rs )
865 {
866         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
867         syncprov_info_t         *si = on->on_bi.bi_private;
868         syncops *so, *soprev;
869
870         ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
871         for ( so=si->si_ops, soprev = (syncops *)&si->si_ops; so;
872                 soprev=so, so=so->s_next ) {
873                 if ( so->s_op->o_connid == op->o_connid &&
874                         so->s_op->o_msgid == op->orn_msgid ) {
875                                 so->s_op->o_abandon = 1;
876                                 soprev->s_next = so->s_next;
877                                 break;
878                 }
879         }
880         ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
881         if ( so ) {
882                 /* Is this really a Cancel exop? */
883                 if ( op->o_tag != LDAP_REQ_ABANDON ) {
884                         so->s_op->o_cancel = SLAP_CANCEL_ACK;
885                         rs->sr_err = LDAP_CANCELLED;
886                         send_ldap_result( so->s_op, rs );
887                         while ( so->s_op->o_cancel != SLAP_CANCEL_DONE ) {
888                                 ldap_pvt_thread_yield();
889                         }
890                 }
891                 syncprov_drop_psearch( so, 0 );
892         }
893         return SLAP_CB_CONTINUE;
894 }
895
896 /* Find which persistent searches are affected by this operation */
897 static void
898 syncprov_matchops( Operation *op, opcookie *opc, int saveit )
899 {
900         slap_overinst *on = opc->son;
901         syncprov_info_t         *si = on->on_bi.bi_private;
902
903         fbase_cookie fc;
904         syncops *ss, *sprev, *snext;
905         Entry *e;
906         Attribute *a;
907         int rc;
908         struct berval newdn;
909         int freefdn = 0;
910
911         fc.fdn = &op->o_req_ndn;
912         /* compute new DN */
913         if ( op->o_tag == LDAP_REQ_MODRDN && !saveit ) {
914                 struct berval pdn;
915                 if ( op->orr_nnewSup ) pdn = *op->orr_nnewSup;
916                 else dnParent( fc.fdn, &pdn );
917                 build_new_dn( &newdn, &pdn, &op->orr_nnewrdn, op->o_tmpmemctx );
918                 fc.fdn = &newdn;
919                 freefdn = 1;
920         }
921         if ( op->o_tag != LDAP_REQ_ADD ) {
922                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
923                 rc = be_entry_get_rw( op, fc.fdn, NULL, NULL, 0, &e );
924                 op->o_bd->bd_info = (BackendInfo *)on;
925                 if ( rc ) return;
926         } else {
927                 e = op->ora_e;
928         }
929
930         if ( saveit ) {
931                 ber_dupbv_x( &opc->sdn, &e->e_name, op->o_tmpmemctx );
932                 ber_dupbv_x( &opc->sndn, &e->e_nname, op->o_tmpmemctx );
933                 opc->sreference = is_entry_referral( e );
934         }
935         if ( saveit || op->o_tag == LDAP_REQ_ADD ) {
936                 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
937                 if ( a )
938                         ber_dupbv_x( &opc->suuid, &a->a_nvals[0], op->o_tmpmemctx );
939         }
940
941         ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
942         for (ss = si->si_ops, sprev = (syncops *)&si->si_ops; ss;
943                 sprev = ss, ss=snext)
944         {
945                 syncmatches *sm;
946                 int found = 0;
947
948                 snext = ss->s_next;
949                 /* validate base */
950                 fc.fss = ss;
951                 fc.fbase = 0;
952                 fc.fscope = 0;
953
954                 /* If the base of the search is missing, signal a refresh */
955                 rc = syncprov_findbase( op, &fc );
956                 if ( rc != LDAP_SUCCESS ) {
957                         SlapReply rs = {REP_RESULT};
958                         send_ldap_error( ss->s_op, &rs, LDAP_SYNC_REFRESH_REQUIRED,
959                                 "search base has changed" );
960                         sprev->s_next = snext;
961                         syncprov_drop_psearch( ss, 1 );
962                         continue;
963                 }
964
965                 /* If we're sending results now, look for this op in old matches */
966                 if ( !saveit ) {
967                         syncmatches *old;
968                         for ( sm=opc->smatches, old=(syncmatches *)&opc->smatches; sm;
969                                 old=sm, sm=sm->sm_next ) {
970                                 if ( sm->sm_op == ss ) {
971                                         found = 1;
972                                         old->sm_next = sm->sm_next;
973                                         op->o_tmpfree( sm, op->o_tmpmemctx );
974                                         break;
975                                 }
976                         }
977                 }
978
979                 /* check if current o_req_dn is in scope and matches filter */
980                 if ( fc.fscope && test_filter( op, e, ss->s_op->ors_filter ) ==
981                         LDAP_COMPARE_TRUE ) {
982                         if ( saveit ) {
983                                 sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
984                                 sm->sm_next = opc->smatches;
985                                 sm->sm_op = ss;
986                                 ss->s_inuse++;
987                                 opc->smatches = sm;
988                         } else {
989                                 /* if found send UPDATE else send ADD */
990                                 syncprov_sendresp( op, opc, ss, e,
991                                         found ? LDAP_SYNC_MODIFY : LDAP_SYNC_ADD, 1 );
992                         }
993                 } else if ( !saveit && found ) {
994                         /* send DELETE */
995                         syncprov_sendresp( op, opc, ss, NULL, LDAP_SYNC_DELETE, 1 );
996                 }
997         }
998         ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
999 done:
1000         if ( op->o_tag != LDAP_REQ_ADD ) {
1001                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1002                 be_entry_release_rw( op, e, 0 );
1003                 op->o_bd->bd_info = (BackendInfo *)on;
1004         }
1005         if ( freefdn ) {
1006                 op->o_tmpfree( fc.fdn->bv_val, op->o_tmpmemctx );
1007         }
1008 }
1009
1010 static int
1011 syncprov_op_cleanup( Operation *op, SlapReply *rs )
1012 {
1013         slap_callback *cb = op->o_callback;
1014         opcookie *opc = cb->sc_private;
1015         slap_overinst *on = opc->son;
1016         syncprov_info_t         *si = on->on_bi.bi_private;
1017         syncmatches *sm, *snext;
1018         modtarget *mt, mtdummy;
1019
1020         for (sm = opc->smatches; sm; sm=snext) {
1021                 snext = sm->sm_next;
1022                 syncprov_free_syncop( sm->sm_op );
1023                 op->o_tmpfree( sm, op->o_tmpmemctx );
1024         }
1025
1026         /* Remove op from lock table */
1027         mtdummy.mt_op = op;
1028         ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
1029         mt = avl_find( si->si_mods, &mtdummy, sp_avl_cmp );
1030         ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
1031         if ( mt ) {
1032                 modinst *mi = mt->mt_mods;
1033
1034                 /* If there are more, promote the next one */
1035                 ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
1036                 if ( mi->mi_next ) {
1037                         mt->mt_mods = mi->mi_next;
1038                         mt->mt_op = mt->mt_mods->mi_op;
1039                         ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
1040                 } else {
1041                         ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
1042                         avl_delete( &si->si_mods, mt, sp_avl_cmp );
1043                         ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
1044                         ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
1045                         ldap_pvt_thread_mutex_destroy( &mt->mt_mutex );
1046                         ch_free( mt );
1047                 }
1048         }
1049         if ( !BER_BVISNULL( &opc->suuid ))
1050                 op->o_tmpfree( opc->suuid.bv_val, op->o_tmpmemctx );
1051         if ( !BER_BVISNULL( &opc->sndn ))
1052                 op->o_tmpfree( opc->sndn.bv_val, op->o_tmpmemctx );
1053         if ( !BER_BVISNULL( &opc->sdn ))
1054                 op->o_tmpfree( opc->sdn.bv_val, op->o_tmpmemctx );
1055         op->o_callback = cb->sc_next;
1056         op->o_tmpfree(cb, op->o_tmpmemctx);
1057
1058         return 0;
1059 }
1060
1061 static void
1062 syncprov_checkpoint( Operation *op, SlapReply *rs, slap_overinst *on )
1063 {
1064         syncprov_info_t         *si = on->on_bi.bi_private;
1065         Modifications mod;
1066         Operation opm;
1067         struct berval bv[2];
1068         BackendInfo *orig;
1069         slap_callback cb = {0};
1070
1071         mod.sml_values = bv;
1072         bv[1].bv_val = NULL;
1073         bv[0] = si->si_ctxcsn;
1074         mod.sml_nvalues = NULL;
1075         mod.sml_desc = slap_schema.si_ad_contextCSN;
1076         mod.sml_op = LDAP_MOD_REPLACE;
1077         mod.sml_next = NULL;
1078
1079         cb.sc_response = slap_null_cb;
1080         opm = *op;
1081         opm.o_tag = LDAP_REQ_MODIFY;
1082         opm.o_callback = &cb;
1083         opm.orm_modlist = &mod;
1084         opm.o_req_dn = op->o_bd->be_suffix[0];
1085         opm.o_req_ndn = op->o_bd->be_nsuffix[0];
1086         orig = opm.o_bd->bd_info;
1087         opm.o_bd->bd_info = on->on_info->oi_orig;
1088         opm.o_bd->be_modify( &opm, rs );
1089 }
1090
1091 static void
1092 syncprov_add_slog( Operation *op, struct berval *csn )
1093 {
1094         opcookie *opc = op->o_callback->sc_private;
1095         slap_overinst *on = opc->son;
1096         syncprov_info_t         *si = on->on_bi.bi_private;
1097         sessionlog *sl;
1098         slog_entry *se;
1099
1100         sl = si->si_logs;
1101         {
1102                 /* Allocate a record. UUIDs are not NUL-terminated. */
1103                 se = ch_malloc( sizeof( slog_entry ) + opc->suuid.bv_len + 
1104                         csn->bv_len + 1 );
1105                 se->se_next = NULL;
1106                 se->se_tag = op->o_tag;
1107
1108                 se->se_uuid.bv_val = (char *)(se+1);
1109                 se->se_csn.bv_val = se->se_uuid.bv_val + opc->suuid.bv_len + 1;
1110                 AC_MEMCPY( se->se_uuid.bv_val, opc->suuid.bv_val, opc->suuid.bv_len );
1111                 se->se_uuid.bv_len = opc->suuid.bv_len;
1112
1113                 AC_MEMCPY( se->se_csn.bv_val, csn->bv_val, csn->bv_len );
1114                 se->se_csn.bv_val[csn->bv_len] = '\0';
1115                 se->se_csn.bv_len = csn->bv_len;
1116
1117                 ldap_pvt_thread_mutex_lock( &sl->sl_mutex );
1118                 if ( sl->sl_head ) {
1119                         sl->sl_tail->se_next = se;
1120                 } else {
1121                         sl->sl_head = se;
1122                 }
1123                 sl->sl_tail = se;
1124                 sl->sl_num++;
1125                 while ( sl->sl_num > sl->sl_size ) {
1126                         se = sl->sl_head;
1127                         sl->sl_head = se->se_next;
1128                         strcpy( sl->sl_mincsn.bv_val, se->se_csn.bv_val );
1129                         sl->sl_mincsn.bv_len = se->se_csn.bv_len;
1130                         ch_free( se );
1131                         sl->sl_num--;
1132                         if ( !sl->sl_head ) {
1133                                 sl->sl_tail = NULL;
1134                         }
1135                 }
1136                 ldap_pvt_thread_mutex_unlock( &sl->sl_mutex );
1137         }
1138 }
1139
1140 /* Just set a flag if we found the matching entry */
1141 static int
1142 playlog_cb( Operation *op, SlapReply *rs )
1143 {
1144         if ( rs->sr_type == REP_SEARCH ) {
1145                 op->o_callback->sc_private = (void *)1;
1146         }
1147         return rs->sr_err;
1148 }
1149
1150 /* enter with sl->sl_mutex locked, release before returning */
1151 static void
1152 syncprov_playlog( Operation *op, SlapReply *rs, sessionlog *sl,
1153         struct berval *oldcsn, struct berval *ctxcsn )
1154 {
1155         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
1156         syncprov_info_t         *si = on->on_bi.bi_private;
1157         slog_entry *se;
1158         int i, j, ndel, num, nmods, mmods;
1159         BerVarray uuids;
1160
1161         if ( !sl->sl_num ) {
1162                 ldap_pvt_thread_mutex_unlock( &sl->sl_mutex );
1163                 return;
1164         }
1165
1166         num = sl->sl_num;
1167         i = 0;
1168         nmods = 0;
1169
1170         uuids = op->o_tmpalloc( (num+1) * sizeof( struct berval ) +
1171                 num * UUID_LEN, op->o_tmpmemctx );
1172
1173         uuids[0].bv_val = (char *)(uuids + num + 1);
1174
1175         /* Make a copy of the relevant UUIDs. Put the Deletes up front
1176          * and everything else at the end. Do this first so we can
1177          * unlock the list mutex.
1178          */
1179         for ( se=sl->sl_head; se; se=se->se_next ) {
1180                 if ( ber_bvcmp( &se->se_csn, oldcsn ) < 0 ) continue;
1181                 if ( ber_bvcmp( &se->se_csn, ctxcsn ) > 0 ) break;
1182                 if ( se->se_tag == LDAP_REQ_DELETE ) {
1183                         j = i;
1184                         i++;
1185                 } else {
1186                         nmods++;
1187                         j = num - nmods;
1188                 }
1189                 uuids[j].bv_val = uuids[0].bv_val + (j * UUID_LEN);
1190                 AC_MEMCPY(uuids[j].bv_val, se->se_uuid.bv_val, UUID_LEN);
1191                 uuids[j].bv_len = UUID_LEN;
1192         }
1193         ldap_pvt_thread_mutex_unlock( &sl->sl_mutex );
1194
1195         ndel = i;
1196
1197         /* Zero out unused slots */
1198         for ( i=ndel; i < num - nmods; i++ )
1199                 uuids[i].bv_len = 0;
1200
1201         /* Mods must be validated to see if they belong in this delete set.
1202          */
1203
1204         mmods = nmods;
1205         /* Strip any duplicates */
1206         for ( i=0; i<nmods; i++ ) {
1207                 for ( j=0; j<ndel; j++ ) {
1208                         if ( bvmatch( &uuids[j], &uuids[num - 1 - i] )) {
1209                                 uuids[num - 1 - i].bv_len = 0;
1210                                 mmods --;
1211                                 break;
1212                         }
1213                 }
1214                 if ( uuids[num - 1 - i].bv_len == 0 ) continue;
1215                 for ( j=0; j<i; j++ ) {
1216                         if ( bvmatch( &uuids[num - 1 - j], &uuids[num - 1 - i] )) {
1217                                 uuids[num - 1 - i].bv_len = 0;
1218                                 mmods --;
1219                                 break;
1220                         }
1221                 }
1222         }
1223
1224         if ( mmods ) {
1225                 Operation fop;
1226                 SlapReply frs = { REP_RESULT };
1227                 int rc;
1228                 Filter mf, af;
1229                 AttributeAssertion eq;
1230                 slap_callback cb = {0};
1231
1232                 fop = *op;
1233
1234                 fop.o_sync_mode = 0;
1235                 fop.o_callback = &cb;
1236                 fop.ors_limit = NULL;
1237                 fop.ors_tlimit = SLAP_NO_LIMIT;
1238                 fop.ors_attrs = slap_anlist_all_attributes;
1239                 fop.ors_attrsonly = 0;
1240                 fop.o_managedsait = SLAP_CONTROL_CRITICAL;
1241
1242                 af.f_choice = LDAP_FILTER_AND;
1243                 af.f_next = NULL;
1244                 af.f_and = &mf;
1245                 mf.f_choice = LDAP_FILTER_EQUALITY;
1246                 mf.f_ava = &eq;
1247                 mf.f_av_desc = slap_schema.si_ad_entryUUID;
1248                 mf.f_next = fop.ors_filter;
1249
1250                 fop.ors_filter = &af;
1251
1252                 cb.sc_response = playlog_cb;
1253                 fop.o_bd->bd_info = on->on_info->oi_orig;
1254
1255                 for ( i=ndel; i<num; i++ ) {
1256                         if ( uuids[i].bv_len == 0 ) continue;
1257
1258                         mf.f_av_value = uuids[i];
1259                         cb.sc_private = NULL;
1260                         fop.ors_slimit = 1;
1261                         rc = fop.o_bd->be_search( &fop, &frs );
1262
1263                         /* If entry was not found, add to delete list */
1264                         if ( !cb.sc_private ) {
1265                                 uuids[ndel++] = uuids[i];
1266                         }
1267                 }
1268                 fop.o_bd->bd_info = (BackendInfo *)on;
1269         }
1270         if ( ndel ) {
1271                 uuids[ndel].bv_val = NULL;
1272                 syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL, 0, uuids, 1 );
1273         }
1274 }
1275
1276 static int
1277 syncprov_op_response( Operation *op, SlapReply *rs )
1278 {
1279         opcookie *opc = op->o_callback->sc_private;
1280         slap_overinst *on = opc->son;
1281         syncprov_info_t         *si = on->on_bi.bi_private;
1282         syncmatches *sm;
1283
1284         if ( rs->sr_err == LDAP_SUCCESS )
1285         {
1286                 struct berval maxcsn = BER_BVNULL, curcsn = BER_BVNULL;
1287                 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
1288
1289                 /* Update our context CSN */
1290                 cbuf[0] = '\0';
1291                 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
1292                 slap_get_commit_csn( op, &maxcsn, &curcsn );
1293                 if ( !BER_BVISNULL( &maxcsn ) ) {
1294                         strcpy( cbuf, maxcsn.bv_val );
1295                         if ( ber_bvcmp( &maxcsn, &si->si_ctxcsn ) > 0 ) {
1296                                 strcpy( si->si_ctxcsnbuf, cbuf );
1297                                 si->si_ctxcsn.bv_len = maxcsn.bv_len;
1298                         }
1299                 }
1300
1301                 /* Don't do any processing for consumer contextCSN updates */
1302                 if ( SLAP_SYNC_SHADOW( op->o_bd ) && 
1303                         op->o_msgid == SLAP_SYNC_UPDATE_MSGID ) {
1304                         ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1305                         return SLAP_CB_CONTINUE;
1306                 }
1307
1308                 si->si_numops++;
1309                 if ( si->si_chkops || si->si_chktime ) {
1310                         int do_check=0;
1311                         if ( si->si_chkops && si->si_numops >= si->si_chkops ) {
1312                                 do_check = 1;
1313                                 si->si_numops = 0;
1314                         }
1315                         if ( si->si_chktime &&
1316                                 (op->o_time - si->si_chklast >= si->si_chktime )) {
1317                                 do_check = 1;
1318                                 si->si_chklast = op->o_time;
1319                         }
1320                         if ( do_check ) {
1321                                 syncprov_checkpoint( op, rs, on );
1322                         }
1323                 }
1324                 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1325
1326                 opc->sctxcsn.bv_len = maxcsn.bv_len;
1327                 opc->sctxcsn.bv_val = cbuf;
1328
1329                 /* Handle any persistent searches */
1330                 if ( si->si_ops ) {
1331                         switch(op->o_tag) {
1332                         case LDAP_REQ_ADD:
1333                         case LDAP_REQ_MODIFY:
1334                         case LDAP_REQ_MODRDN:
1335                         case LDAP_REQ_EXTENDED:
1336                                 syncprov_matchops( op, opc, 0 );
1337                                 break;
1338                         case LDAP_REQ_DELETE:
1339                                 /* for each match in opc->smatches:
1340                                  *   send DELETE msg
1341                                  */
1342                                 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
1343                                 for ( sm = opc->smatches; sm; sm=sm->sm_next ) {
1344                                         if ( sm->sm_op->s_op->o_abandon )
1345                                                 continue;
1346                                         syncprov_sendresp( op, opc, sm->sm_op, NULL,
1347                                                 LDAP_SYNC_DELETE, 1 );
1348                                 }
1349                                 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
1350                                 break;
1351                         }
1352                 }
1353
1354                 /* Add any log records */
1355                 if ( si->si_logs && op->o_tag != LDAP_REQ_ADD ) {
1356                         syncprov_add_slog( op, &curcsn );
1357                 }
1358
1359         }
1360         return SLAP_CB_CONTINUE;
1361 }
1362
1363 /* We don't use a subentry to store the context CSN any more.
1364  * We expose the current context CSN as an operational attribute
1365  * of the suffix entry.
1366  */
1367 static int
1368 syncprov_op_compare( Operation *op, SlapReply *rs )
1369 {
1370         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
1371         syncprov_info_t         *si = on->on_bi.bi_private;
1372         int rc = SLAP_CB_CONTINUE;
1373
1374         if ( dn_match( &op->o_req_ndn, op->o_bd->be_nsuffix ) &&
1375                 op->oq_compare.rs_ava->aa_desc == slap_schema.si_ad_contextCSN )
1376         {
1377                 Entry e = {0};
1378                 Attribute a = {0};
1379                 struct berval bv[2];
1380
1381                 e.e_name = op->o_bd->be_suffix[0];
1382                 e.e_nname = op->o_bd->be_nsuffix[0];
1383
1384                 BER_BVZERO( &bv[1] );
1385                 bv[0] = si->si_ctxcsn;
1386
1387                 a.a_desc = slap_schema.si_ad_contextCSN;
1388                 a.a_vals = bv;
1389                 a.a_nvals = a.a_vals;
1390
1391                 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
1392
1393                 rs->sr_err = access_allowed( op, &e, op->oq_compare.rs_ava->aa_desc,
1394                         &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
1395                 if ( ! rs->sr_err ) {
1396                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1397                         goto return_results;
1398                 }
1399
1400                 if ( get_assert( op ) &&
1401                         ( test_filter( op, &e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
1402                 {
1403                         rs->sr_err = LDAP_ASSERTION_FAILED;
1404                         goto return_results;
1405                 }
1406
1407
1408                 rs->sr_err = LDAP_COMPARE_FALSE;
1409
1410                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
1411                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1412                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1413                                 a.a_nvals, &op->oq_compare.rs_ava->aa_value, op->o_tmpmemctx ) == 0 )
1414                 {
1415                         rs->sr_err = LDAP_COMPARE_TRUE;
1416                 }
1417
1418 return_results:;
1419
1420                 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1421
1422                 send_ldap_result( op, rs );
1423
1424                 if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
1425                         rs->sr_err = LDAP_SUCCESS;
1426                 }
1427                 rc = rs->sr_err;
1428         }
1429
1430         return rc;
1431 }
1432
1433 static int
1434 syncprov_op_mod( Operation *op, SlapReply *rs )
1435 {
1436         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
1437         syncprov_info_t         *si = on->on_bi.bi_private;
1438
1439         slap_callback *cb = op->o_tmpcalloc(1, sizeof(slap_callback)+
1440                 sizeof(opcookie) +
1441                 (si->si_ops ? sizeof(modinst) : 0 ),
1442                 op->o_tmpmemctx);
1443         opcookie *opc = (opcookie *)(cb+1);
1444         opc->son = on;
1445         cb->sc_response = syncprov_op_response;
1446         cb->sc_cleanup = syncprov_op_cleanup;
1447         cb->sc_private = opc;
1448         cb->sc_next = op->o_callback;
1449         op->o_callback = cb;
1450
1451         /* If there are active persistent searches, lock this operation.
1452          * See seqmod.c for the locking logic on its own.
1453          */
1454         if ( si->si_ops ) {
1455                 modtarget *mt, mtdummy;
1456                 modinst *mi;
1457
1458                 mi = (modinst *)(opc+1);
1459                 mi->mi_op = op;
1460
1461                 /* See if we're already modifying this entry... */
1462                 mtdummy.mt_op = op;
1463                 ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
1464                 mt = avl_find( si->si_mods, &mtdummy, sp_avl_cmp );
1465                 if ( mt ) {
1466                         ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
1467                         ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
1468                         mt->mt_tail->mi_next = mi;
1469                         mt->mt_tail = mi;
1470                         /* wait for this op to get to head of list */
1471                         while ( mt->mt_mods != mi ) {
1472                                 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
1473                                 ldap_pvt_thread_yield();
1474                                 ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
1475                         }
1476                         ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
1477                 } else {
1478                         /* Record that we're modifying this entry now */
1479                         mt = ch_malloc( sizeof(modtarget) );
1480                         mt->mt_mods = mi;
1481                         mt->mt_tail = mi;
1482                         mt->mt_op = mi->mi_op;
1483                         ldap_pvt_thread_mutex_init( &mt->mt_mutex );
1484                         avl_insert( &si->si_mods, mt, sp_avl_cmp, avl_dup_error );
1485                         ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
1486                 }
1487         }
1488
1489         if (( si->si_ops || si->si_logs ) && op->o_tag != LDAP_REQ_ADD )
1490                 syncprov_matchops( op, opc, 1 );
1491                 
1492
1493         return SLAP_CB_CONTINUE;
1494 }
1495
1496 static int
1497 syncprov_op_extended( Operation *op, SlapReply *rs )
1498 {
1499         if ( exop_is_write( op ))
1500                 return syncprov_op_mod( op, rs );
1501
1502         return SLAP_CB_CONTINUE;
1503 }
1504
1505 typedef struct searchstate {
1506         slap_overinst *ss_on;
1507         syncops *ss_so;
1508         int ss_present;
1509 } searchstate;
1510
1511 static int
1512 syncprov_search_cleanup( Operation *op, SlapReply *rs )
1513 {
1514         if ( rs->sr_ctrls ) {
1515                 op->o_tmpfree( rs->sr_ctrls[0], op->o_tmpmemctx );
1516                 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
1517                 rs->sr_ctrls = NULL;
1518         }
1519         return 0;
1520 }
1521
1522 static void
1523 syncprov_detach_op( Operation *op, syncops *so )
1524 {
1525         Operation *op2;
1526         int i, alen = 0;
1527         size_t size;
1528         char *ptr;
1529         GroupAssertion *g1, *g2;
1530
1531         /* count the search attrs */
1532         for (i=0; op->ors_attrs && !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++) {
1533                 alen += op->ors_attrs[i].an_name.bv_len + 1;
1534         }
1535         /* Make a new copy of the operation */
1536         size = sizeof(Operation) + sizeof(Opheader) +
1537                 (i ? ( (i+1) * sizeof(AttributeName) + alen) : 0) +
1538                 op->o_req_dn.bv_len + 1 +
1539                 op->o_req_ndn.bv_len + 1 +
1540                 op->o_ndn.bv_len + 1 +
1541                 so->s_filterstr.bv_len + 1;
1542         op2 = (Operation *)ch_calloc( 1, size );
1543         op2->o_hdr = (Opheader *)(op2+1);
1544
1545         /* Copy the fields we care about explicitly, leave the rest alone */
1546         *op2->o_hdr = *op->o_hdr;
1547         op2->o_tag = op->o_tag;
1548         op2->o_time = op->o_time;
1549         op2->o_bd = op->o_bd;
1550         op2->o_request = op->o_request;
1551
1552         if ( i ) {
1553                 op2->ors_attrs = (AttributeName *)(op2->o_hdr + 1);
1554                 ptr = (char *)(op2->ors_attrs+i+1);
1555                 for (i=0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++) {
1556                         op2->ors_attrs[i] = op->ors_attrs[i];
1557                         op2->ors_attrs[i].an_name.bv_val = ptr;
1558                         ptr = lutil_strcopy( ptr, op->ors_attrs[i].an_name.bv_val ) + 1;
1559                 }
1560                 BER_BVZERO( &op2->ors_attrs[i].an_name );
1561         } else {
1562                 ptr = (char *)(op2->o_hdr + 1);
1563         }
1564         op2->o_authz = op->o_authz;
1565         op2->o_ndn.bv_val = ptr;
1566         ptr = lutil_strcopy(ptr, op->o_ndn.bv_val) + 1;
1567         op2->o_dn = op2->o_ndn;
1568         op2->o_req_dn.bv_len = op->o_req_dn.bv_len;
1569         op2->o_req_dn.bv_val = ptr;
1570         ptr = lutil_strcopy(ptr, op->o_req_dn.bv_val) + 1;
1571         op2->o_req_ndn.bv_len = op->o_req_ndn.bv_len;
1572         op2->o_req_ndn.bv_val = ptr;
1573         ptr = lutil_strcopy(ptr, op->o_req_ndn.bv_val) + 1;
1574         op2->ors_filterstr.bv_val = ptr;
1575         strcpy( ptr, so->s_filterstr.bv_val );
1576         op2->ors_filterstr.bv_len = so->s_filterstr.bv_len;
1577         op2->ors_filter = str2filter( ptr );
1578         so->s_op = op2;
1579
1580         /* Copy any cached group ACLs individually */
1581         op2->o_groups = NULL;
1582         for ( g1=op->o_groups; g1; g1=g1->ga_next ) {
1583                 g2 = ch_malloc( sizeof(GroupAssertion) + g1->ga_len );
1584                 *g2 = *g1;
1585                 strcpy( g2->ga_ndn, g1->ga_ndn );
1586                 g2->ga_next = op2->o_groups;
1587                 op2->o_groups = g2;
1588         }
1589
1590         /* Add op2 to conn so abandon will find us */
1591         ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1592         op->o_conn->c_n_ops_executing++;
1593         op->o_conn->c_n_ops_completed--;
1594         LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op2, o_next );
1595         so->s_flags |= PS_IS_DETACHED;
1596         ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1597 }
1598
1599 static int
1600 syncprov_search_response( Operation *op, SlapReply *rs )
1601 {
1602         searchstate *ss = op->o_callback->sc_private;
1603         slap_overinst *on = ss->ss_on;
1604         syncprov_info_t         *si = on->on_bi.bi_private;
1605         sync_control *srs = op->o_controls[slap_cids.sc_LDAPsync];
1606
1607         if ( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF ) {
1608                 int i;
1609                 /* If we got a referral without a referral object, there's
1610                  * something missing that we cannot replicate. Just ignore it.
1611                  * The consumer will abort because we didn't send the expected
1612                  * control.
1613                  */
1614                 if ( !rs->sr_entry ) {
1615                         assert( rs->sr_entry );
1616                         Debug( LDAP_DEBUG_ANY, "bogus referral in context\n",0,0,0 );
1617                         return SLAP_CB_CONTINUE;
1618                 }
1619                 if ( !BER_BVISNULL( &srs->sr_state.ctxcsn )) {
1620                         Attribute *a = attr_find( rs->sr_entry->e_attrs,
1621                                 slap_schema.si_ad_entryCSN );
1622                         
1623                         /* Don't send the ctx entry twice */
1624                         if ( a && bvmatch( &a->a_nvals[0], &srs->sr_state.ctxcsn ) )
1625                                 return LDAP_SUCCESS;
1626                 }
1627                 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
1628                         op->o_tmpmemctx );
1629                 rs->sr_ctrls[1] = NULL;
1630                 rs->sr_err = syncprov_state_ctrl( op, rs, rs->sr_entry,
1631                         LDAP_SYNC_ADD, rs->sr_ctrls, 0, 0, NULL );
1632         } else if ( rs->sr_type == REP_RESULT && rs->sr_err == LDAP_SUCCESS ) {
1633                 struct berval cookie;
1634
1635                 slap_compose_sync_cookie( op, &cookie,
1636                         &op->ors_filter->f_and->f_ava->aa_value,
1637                         srs->sr_state.rid );
1638
1639                 /* Is this a regular refresh? */
1640                 if ( !ss->ss_so ) {
1641                         rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
1642                                 op->o_tmpmemctx );
1643                         rs->sr_ctrls[1] = NULL;
1644                         rs->sr_err = syncprov_done_ctrl( op, rs, rs->sr_ctrls,
1645                                 0, 1, &cookie, ss->ss_present ?  LDAP_SYNC_REFRESH_PRESENTS :
1646                                         LDAP_SYNC_REFRESH_DELETES );
1647                 } else {
1648                         int locked = 0;
1649                 /* It's RefreshAndPersist, transition to Persist phase */
1650                         syncprov_sendinfo( op, rs, ( ss->ss_present && rs->sr_nentries ) ?
1651                                 LDAP_TAG_SYNC_REFRESH_PRESENT : LDAP_TAG_SYNC_REFRESH_DELETE,
1652                                 &cookie, 1, NULL, 0 );
1653                         /* Flush any queued persist messages */
1654                         if ( ss->ss_so->s_res ) {
1655                                 syncres *sr, *srnext;
1656                                 Entry *e;
1657                                 opcookie opc;
1658
1659                                 opc.son = on;
1660                                 ldap_pvt_thread_mutex_lock( &ss->ss_so->s_mutex );
1661                                 locked = 1;
1662                                 for (sr = ss->ss_so->s_res; sr; sr=srnext) {
1663                                         int rc = LDAP_SUCCESS;
1664                                         srnext = sr->s_next;
1665                                         opc.sdn = sr->s_dn;
1666                                         opc.sndn = sr->s_ndn;
1667                                         opc.suuid = sr->s_uuid;
1668                                         opc.sctxcsn = sr->s_csn;
1669                                         opc.sreference = sr->s_isreference;
1670                                         e = NULL;
1671
1672                                         if ( sr->s_mode != LDAP_SYNC_DELETE ) {
1673                                                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1674                                                 rc = be_entry_get_rw( op, &opc.sndn, NULL, NULL, 0, &e );
1675                                                 op->o_bd->bd_info = (BackendInfo *)on;
1676                                         }
1677                                         if ( rc == LDAP_SUCCESS )
1678                                                 syncprov_sendresp( op, &opc, ss->ss_so, e,
1679                                                         sr->s_mode, 0 );
1680
1681                                         if ( e ) {
1682                                                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1683                                                 be_entry_release_rw( op, e, 0 );
1684                                                 op->o_bd->bd_info = (BackendInfo *)on;
1685                                         }
1686                                         ch_free( sr );
1687                                 }
1688                                 ss->ss_so->s_res = NULL;
1689                                 ss->ss_so->s_restail = NULL;
1690                         }
1691
1692                         /* Turn off the refreshing flag */
1693                         ss->ss_so->s_flags ^= PS_IS_REFRESHING;
1694                         if ( locked )
1695                                 ldap_pvt_thread_mutex_unlock( &ss->ss_so->s_mutex );
1696
1697                         /* Detach this Op from frontend control */
1698                         syncprov_detach_op( op, ss->ss_so );
1699
1700                         return LDAP_SUCCESS;
1701                 }
1702         }
1703
1704         return SLAP_CB_CONTINUE;
1705 }
1706
1707 static int
1708 syncprov_op_search( Operation *op, SlapReply *rs )
1709 {
1710         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
1711         syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
1712         slap_callback   *cb;
1713         int gotstate = 0, nochange = 0, do_present = 1;
1714         Filter *fand, *fava;
1715         syncops *sop = NULL;
1716         searchstate *ss;
1717         sync_control *srs;
1718         struct berval ctxcsn;
1719         char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
1720
1721         if ( !(op->o_sync_mode & SLAP_SYNC_REFRESH) ) return SLAP_CB_CONTINUE;
1722
1723         if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1724                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "illegal value for derefAliases" );
1725                 return rs->sr_err;
1726         }
1727
1728         srs = op->o_controls[slap_cids.sc_LDAPsync];
1729
1730         /* If this is a persistent search, set it up right away */
1731         if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
1732                 syncops so = {0};
1733                 fbase_cookie fc;
1734                 opcookie opc;
1735                 slap_callback sc;
1736
1737                 fc.fss = &so;
1738                 fc.fbase = 0;
1739                 so.s_eid = NOID;
1740                 so.s_op = op;
1741                 so.s_flags = PS_IS_REFRESHING;
1742                 /* syncprov_findbase expects to be called as a callback... */
1743                 sc.sc_private = &opc;
1744                 opc.son = on;
1745                 cb = op->o_callback;
1746                 op->o_callback = &sc;
1747                 rs->sr_err = syncprov_findbase( op, &fc );
1748                 op->o_callback = cb;
1749
1750                 if ( rs->sr_err != LDAP_SUCCESS ) {
1751                         send_ldap_result( op, rs );
1752                         return rs->sr_err;
1753                 }
1754                 sop = ch_malloc( sizeof( syncops ));
1755                 *sop = so;
1756                 ldap_pvt_thread_mutex_init( &sop->s_mutex );
1757                 sop->s_rid = srs->sr_state.rid;
1758                 sop->s_inuse = 1;
1759
1760                 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
1761                 sop->s_next = si->si_ops;
1762                 si->si_ops = sop;
1763                 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
1764         }
1765
1766         /* snapshot the ctxcsn */
1767         ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
1768         strcpy( csnbuf, si->si_ctxcsnbuf );
1769         ctxcsn.bv_len = si->si_ctxcsn.bv_len;
1770         ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1771         ctxcsn.bv_val = csnbuf;
1772         
1773         /* If we have a cookie, handle the PRESENT lookups */
1774         if ( !BER_BVISNULL( &srs->sr_state.ctxcsn )) {
1775                 sessionlog *sl;
1776
1777                 /* The cookie was validated when it was parsed, just use it */
1778
1779                 /* If just Refreshing and nothing has changed, shortcut it */
1780                 if ( bvmatch( &srs->sr_state.ctxcsn, &ctxcsn )) {
1781                         nochange = 1;
1782                         if ( !(op->o_sync_mode & SLAP_SYNC_PERSIST) ) {
1783                                 LDAPControl     *ctrls[2];
1784
1785                                 ctrls[0] = NULL;
1786                                 ctrls[1] = NULL;
1787                                 syncprov_done_ctrl( op, rs, ctrls, 0, 0,
1788                                         NULL, LDAP_SYNC_REFRESH_DELETES );
1789                                 rs->sr_ctrls = ctrls;
1790                                 rs->sr_err = LDAP_SUCCESS;
1791                                 send_ldap_result( op, rs );
1792                                 rs->sr_ctrls = NULL;
1793                                 return rs->sr_err;
1794                         }
1795                         goto shortcut;
1796                 }
1797                 /* Do we have a sessionlog for this search? */
1798                 sl=si->si_logs;
1799                 if ( sl ) {
1800                         ldap_pvt_thread_mutex_lock( &sl->sl_mutex );
1801                         if ( ber_bvcmp( &srs->sr_state.ctxcsn, &sl->sl_mincsn ) >= 0 ) {
1802                                 do_present = 0;
1803                                 /* mutex is unlocked in playlog */
1804                                 syncprov_playlog( op, rs, sl, &srs->sr_state.ctxcsn, &ctxcsn );
1805                         } else {
1806                                 ldap_pvt_thread_mutex_unlock( &sl->sl_mutex );
1807                         }
1808                 }
1809                 /* Is the CSN still present in the database? */
1810                 if ( syncprov_findcsn( op, FIND_CSN ) != LDAP_SUCCESS ) {
1811                         /* No, so a reload is required */
1812 #if 0           /* the consumer doesn't seem to send this hint */
1813                         if ( op->o_sync_rhint == 0 ) {
1814                                 send_ldap_error( op, rs, LDAP_SYNC_REFRESH_REQUIRED, "sync cookie is stale" );
1815                                 return rs->sr_err;
1816                         }
1817 #endif
1818                 } else {
1819                         gotstate = 1;
1820                         /* If changed and doing Present lookup, send Present UUIDs */
1821                         if ( do_present && syncprov_findcsn( op, FIND_PRESENT ) !=
1822                                 LDAP_SUCCESS ) {
1823                                 send_ldap_result( op, rs );
1824                                 return rs->sr_err;
1825                         }
1826                 }
1827         }
1828
1829 shortcut:
1830         /* Append CSN range to search filter, save original filter
1831          * for persistent search evaluation
1832          */
1833         if ( sop ) {
1834                 sop->s_filterstr= op->ors_filterstr;
1835         }
1836
1837         fand = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1838         fand->f_choice = LDAP_FILTER_AND;
1839         fand->f_next = NULL;
1840         fava = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1841         fava->f_choice = LDAP_FILTER_LE;
1842         fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
1843         fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
1844         ber_dupbv_x( &fava->f_ava->aa_value, &ctxcsn, op->o_tmpmemctx );
1845         fand->f_and = fava;
1846         if ( gotstate ) {
1847                 fava->f_next = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1848                 fava = fava->f_next;
1849                 fava->f_choice = LDAP_FILTER_GE;
1850                 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
1851                 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
1852                 ber_dupbv_x( &fava->f_ava->aa_value, &srs->sr_state.ctxcsn, op->o_tmpmemctx );
1853         }
1854         fava->f_next = op->ors_filter;
1855         op->ors_filter = fand;
1856         filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
1857
1858         /* Let our callback add needed info to returned entries */
1859         cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(searchstate), op->o_tmpmemctx);
1860         ss = (searchstate *)(cb+1);
1861         ss->ss_on = on;
1862         ss->ss_so = sop;
1863         ss->ss_present = do_present;
1864         cb->sc_response = syncprov_search_response;
1865         cb->sc_cleanup = syncprov_search_cleanup;
1866         cb->sc_private = ss;
1867         cb->sc_next = op->o_callback;
1868         op->o_callback = cb;
1869
1870 #if 0   /* I don't think we need to shortcircuit back-bdb any more */
1871         op->o_sync_mode &= SLAP_CONTROL_MASK;
1872 #endif
1873
1874         /* If this is a persistent search and no changes were reported during
1875          * the refresh phase, just invoke the response callback to transition
1876          * us into persist phase
1877          */
1878         if ( nochange ) {
1879                 rs->sr_err = LDAP_SUCCESS;
1880                 rs->sr_nentries = 0;
1881                 send_ldap_result( op, rs );
1882                 return rs->sr_err;
1883         }
1884         return SLAP_CB_CONTINUE;
1885 }
1886
1887 static int
1888 syncprov_operational(
1889         Operation *op,
1890         SlapReply *rs )
1891 {
1892         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
1893         syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
1894
1895         if ( rs->sr_entry &&
1896                 dn_match( &rs->sr_entry->e_nname, op->o_bd->be_nsuffix )) {
1897
1898                 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
1899                         ad_inlist( slap_schema.si_ad_contextCSN, rs->sr_attrs )) {
1900                         Attribute *a, **ap = NULL;
1901
1902                         for ( a=rs->sr_entry->e_attrs; a; a=a->a_next ) {
1903                                 if ( a->a_desc == slap_schema.si_ad_contextCSN )
1904                                         break;
1905                         }
1906
1907                         if ( !a ) {
1908                                 for ( ap = &rs->sr_operational_attrs; *ap; ap=&(*ap)->a_next );
1909
1910                                 a = ch_malloc( sizeof(Attribute));
1911                                 a->a_desc = slap_schema.si_ad_contextCSN;
1912                                 a->a_vals = ch_malloc( 2 * sizeof(struct berval));
1913                                 a->a_vals[1].bv_val = NULL;
1914                                 a->a_nvals = a->a_vals;
1915                                 a->a_next = NULL;
1916                                 a->a_flags = 0;
1917                                 *ap = a;
1918                         }
1919
1920                         ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
1921                         if ( !ap ) {
1922                                 strcpy( a->a_vals[0].bv_val, si->si_ctxcsnbuf );
1923                         } else {
1924                                 ber_dupbv( &a->a_vals[0], &si->si_ctxcsn );
1925                         }
1926                         ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1927                 }
1928         }
1929         return SLAP_CB_CONTINUE;
1930 }
1931
1932 static int
1933 syncprov_db_config(
1934         BackendDB       *be,
1935         const char      *fname,
1936         int             lineno,
1937         int             argc,
1938         char    **argv
1939 )
1940 {
1941         slap_overinst           *on = (slap_overinst *)be->bd_info;
1942         syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
1943
1944         if ( strcasecmp( argv[ 0 ], "syncprov-checkpoint" ) == 0 ) {
1945                 if ( argc != 3 ) {
1946                         fprintf( stderr, "%s: line %d: wrong number of arguments in "
1947                                 "\"syncprov-checkpoint <ops> <minutes>\"\n", fname, lineno );
1948                         return -1;
1949                 }
1950                 si->si_chkops = atoi( argv[1] );
1951                 si->si_chktime = atoi( argv[2] ) * 60;
1952                 return 0;
1953
1954         } else if ( strcasecmp( argv[0], "syncprov-sessionlog" ) == 0 ) {
1955                 sessionlog *sl;
1956                 int size;
1957                 if ( argc != 2 ) {
1958                         fprintf( stderr, "%s: line %d: wrong number of arguments in "
1959                                 "\"syncprov-sessionlog <size>\"\n", fname, lineno );
1960                         return -1;
1961                 }
1962                 size = atoi( argv[1] );
1963                 if ( size < 0 ) {
1964                         fprintf( stderr,
1965                                 "%s: line %d: session log size %d is negative\n",
1966                                 fname, lineno, size );
1967                         return -1;
1968                 }
1969                 sl = si->si_logs;
1970                 if ( !sl ) {
1971                         sl = ch_malloc( sizeof( sessionlog ) + LDAP_LUTIL_CSNSTR_BUFSIZE );
1972                         sl->sl_mincsn.bv_val = (char *)(sl+1);
1973                         sl->sl_mincsn.bv_len = 0;
1974                         sl->sl_num = 0;
1975                         sl->sl_head = sl->sl_tail = NULL;
1976                         ldap_pvt_thread_mutex_init( &sl->sl_mutex );
1977                         si->si_logs = sl;
1978                 }
1979                 sl->sl_size = size;
1980                 return 0;
1981         }
1982
1983         return SLAP_CONF_UNKNOWN;
1984 }
1985
1986 /* Cheating - we have no thread pool context for these functions,
1987  * so make one.
1988  */
1989 typedef struct thread_keys {
1990         void *key;
1991         void *data;
1992         ldap_pvt_thread_pool_keyfree_t *xfree;
1993 } thread_keys;
1994
1995 #define MAXKEYS 32
1996 /* A fake thread context */
1997 static thread_keys thrctx[MAXKEYS];
1998
1999 /* Read any existing contextCSN from the underlying db.
2000  * Then search for any entries newer than that. If no value exists,
2001  * just generate it. Cache whatever result.
2002  */
2003 static int
2004 syncprov_db_open(
2005     BackendDB *be
2006 )
2007 {
2008         slap_overinst   *on = (slap_overinst *) be->bd_info;
2009         syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
2010
2011         Connection conn;
2012         char opbuf[OPERATION_BUFFER_SIZE];
2013         char ctxcsnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
2014         Operation *op = (Operation *)opbuf;
2015         Entry *e;
2016         Attribute *a;
2017         int rc;
2018
2019         rc = overlay_register_control( be, LDAP_CONTROL_SYNC );
2020         if ( rc ) {
2021                 return rc;
2022         }
2023
2024         connection_fake_init( &conn, op, thrctx );
2025         op->o_bd = be;
2026         op->o_dn = be->be_rootdn;
2027         op->o_ndn = be->be_rootndn;
2028
2029         ctxcsnbuf[0] = '\0';
2030
2031         op->o_bd->bd_info = on->on_info->oi_orig;
2032         rc = be_entry_get_rw( op, be->be_nsuffix, NULL,
2033                 slap_schema.si_ad_contextCSN, 0, &e );
2034
2035         if ( e ) {
2036                 a = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
2037                 if ( a ) {
2038                         si->si_ctxcsn.bv_len = a->a_nvals[0].bv_len;
2039                         if ( si->si_ctxcsn.bv_len >= sizeof(si->si_ctxcsnbuf ))
2040                                 si->si_ctxcsn.bv_len = sizeof(si->si_ctxcsnbuf)-1;
2041                         strncpy( si->si_ctxcsnbuf, a->a_nvals[0].bv_val,
2042                                 si->si_ctxcsn.bv_len );
2043                         si->si_ctxcsnbuf[si->si_ctxcsn.bv_len] = '\0';
2044                         strcpy( ctxcsnbuf, si->si_ctxcsnbuf );
2045                 }
2046                 be_entry_release_rw( op, e, 0 );
2047                 op->o_bd->bd_info = (BackendInfo *)on;
2048                 op->o_req_dn = be->be_suffix[0];
2049                 op->o_req_ndn = be->be_nsuffix[0];
2050                 op->ors_scope = LDAP_SCOPE_SUBTREE;
2051                 syncprov_findcsn( op, FIND_MAXCSN );
2052         }
2053
2054         if ( BER_BVISEMPTY( &si->si_ctxcsn ) ) {
2055                 slap_get_csn( op, si->si_ctxcsnbuf, sizeof(si->si_ctxcsnbuf),
2056                                 &si->si_ctxcsn, 0 );
2057         }
2058
2059         /* If our ctxcsn is different from what was read from the root
2060          * entry, write the new value out.
2061          */
2062         if ( strcmp( si->si_ctxcsnbuf, ctxcsnbuf )) {
2063                 SlapReply rs = {REP_RESULT};
2064                 syncprov_checkpoint( op, &rs, on );
2065         }
2066
2067         op->o_bd->bd_info = (BackendInfo *)on;
2068         return 0;
2069 }
2070
2071 /* Write the current contextCSN into the underlying db.
2072  */
2073 static int
2074 syncprov_db_close(
2075     BackendDB *be
2076 )
2077 {
2078     slap_overinst   *on = (slap_overinst *) be->bd_info;
2079     syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
2080         int i;
2081
2082         if ( si->si_numops ) {
2083                 Connection conn;
2084                 char opbuf[OPERATION_BUFFER_SIZE];
2085                 Operation *op = (Operation *)opbuf;
2086                 SlapReply rs = {REP_RESULT};
2087
2088                 connection_fake_init( &conn, op, thrctx );
2089                 op->o_bd = be;
2090                 op->o_dn = be->be_rootdn;
2091                 op->o_ndn = be->be_rootndn;
2092                 syncprov_checkpoint( op, &rs, on );
2093         }
2094         for ( i=0; thrctx[i].key; i++) {
2095                 if ( thrctx[i].xfree )
2096                         thrctx[i].xfree( thrctx[i].key, thrctx[i].data );
2097                 thrctx[i].key = NULL;
2098         }
2099
2100     return 0;
2101 }
2102
2103 static int
2104 syncprov_db_init(
2105         BackendDB *be
2106 )
2107 {
2108         slap_overinst   *on = (slap_overinst *)be->bd_info;
2109         syncprov_info_t *si;
2110
2111         si = ch_calloc(1, sizeof(syncprov_info_t));
2112         on->on_bi.bi_private = si;
2113         ldap_pvt_thread_mutex_init( &si->si_csn_mutex );
2114         ldap_pvt_thread_mutex_init( &si->si_ops_mutex );
2115         ldap_pvt_thread_mutex_init( &si->si_mods_mutex );
2116         si->si_ctxcsn.bv_val = si->si_ctxcsnbuf;
2117
2118         csn_anlist[0].an_desc = slap_schema.si_ad_entryCSN;
2119         csn_anlist[0].an_name = slap_schema.si_ad_entryCSN->ad_cname;
2120
2121         uuid_anlist[0].an_desc = slap_schema.si_ad_entryUUID;
2122         uuid_anlist[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2123
2124         return 0;
2125 }
2126
2127 static int
2128 syncprov_db_destroy(
2129         BackendDB *be
2130 )
2131 {
2132         slap_overinst   *on = (slap_overinst *)be->bd_info;
2133         syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
2134
2135         if ( si ) {
2136                 ldap_pvt_thread_mutex_destroy( &si->si_mods_mutex );
2137                 ldap_pvt_thread_mutex_destroy( &si->si_ops_mutex );
2138                 ldap_pvt_thread_mutex_destroy( &si->si_csn_mutex );
2139                 ch_free( si );
2140         }
2141
2142         return 0;
2143 }
2144
2145 static int syncprov_parseCtrl (
2146         Operation *op,
2147         SlapReply *rs,
2148         LDAPControl *ctrl )
2149 {
2150         ber_tag_t tag;
2151         BerElement *ber;
2152         ber_int_t mode;
2153         ber_len_t len;
2154         struct berval cookie = BER_BVNULL;
2155         sync_control *sr;
2156         int rhint = 0;
2157
2158         if ( op->o_sync != SLAP_CONTROL_NONE ) {
2159                 rs->sr_text = "Sync control specified multiple times";
2160                 return LDAP_PROTOCOL_ERROR;
2161         }
2162
2163         if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
2164                 rs->sr_text = "Sync control specified with pagedResults control";
2165                 return LDAP_PROTOCOL_ERROR;
2166         }
2167
2168         if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
2169                 rs->sr_text = "Sync control value is empty (or absent)";
2170                 return LDAP_PROTOCOL_ERROR;
2171         }
2172
2173         /* Parse the control value
2174          *      syncRequestValue ::= SEQUENCE {
2175          *              mode   ENUMERATED {
2176          *                      -- 0 unused
2177          *                      refreshOnly             (1),
2178          *                      -- 2 reserved
2179          *                      refreshAndPersist       (3)
2180          *              },
2181          *              cookie  syncCookie OPTIONAL
2182          *      }
2183          */
2184
2185         ber = ber_init( &ctrl->ldctl_value );
2186         if( ber == NULL ) {
2187                 rs->sr_text = "internal error";
2188                 return LDAP_OTHER;
2189         }
2190
2191         if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
2192                 rs->sr_text = "Sync control : mode decoding error";
2193                 return LDAP_PROTOCOL_ERROR;
2194         }
2195
2196         switch( mode ) {
2197         case LDAP_SYNC_REFRESH_ONLY:
2198                 mode = SLAP_SYNC_REFRESH;
2199                 break;
2200         case LDAP_SYNC_REFRESH_AND_PERSIST:
2201                 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
2202                 break;
2203         default:
2204                 rs->sr_text = "Sync control : unknown update mode";
2205                 return LDAP_PROTOCOL_ERROR;
2206         }
2207
2208         tag = ber_peek_tag( ber, &len );
2209
2210         if ( tag == LDAP_TAG_SYNC_COOKIE ) {
2211                 if (( ber_scanf( ber, /*{*/ "o", &cookie )) == LBER_ERROR ) {
2212                         rs->sr_text = "Sync control : cookie decoding error";
2213                         return LDAP_PROTOCOL_ERROR;
2214                 }
2215         }
2216         if ( tag == LDAP_TAG_RELOAD_HINT ) {
2217                 if (( ber_scanf( ber, /*{*/ "b", &rhint )) == LBER_ERROR ) {
2218                         rs->sr_text = "Sync control : rhint decoding error";
2219                         return LDAP_PROTOCOL_ERROR;
2220                 }
2221         }
2222         if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
2223                         rs->sr_text = "Sync control : decoding error";
2224                         return LDAP_PROTOCOL_ERROR;
2225         }
2226         sr = op->o_tmpcalloc( 1, sizeof(struct sync_control), op->o_tmpmemctx );
2227         sr->sr_rhint = rhint;
2228         if (!BER_BVISNULL(&cookie)) {
2229                 ber_dupbv( &sr->sr_state.octet_str, &cookie );
2230                 slap_parse_sync_cookie( &sr->sr_state );
2231         }
2232
2233         op->o_controls[slap_cids.sc_LDAPsync] = sr;
2234
2235         (void) ber_free( ber, 1 );
2236
2237         op->o_sync = ctrl->ldctl_iscritical
2238                 ? SLAP_CONTROL_CRITICAL
2239                 : SLAP_CONTROL_NONCRITICAL;
2240
2241         op->o_sync_mode |= mode;        /* o_sync_mode shares o_sync */
2242
2243         return LDAP_SUCCESS;
2244 }
2245
2246 /* This overlay is set up for dynamic loading via moduleload. For static
2247  * configuration, you'll need to arrange for the slap_overinst to be
2248  * initialized and registered by some other function inside slapd.
2249  */
2250
2251 static slap_overinst            syncprov;
2252
2253 int
2254 syncprov_init()
2255 {
2256         int rc;
2257
2258         rc = register_supported_control( LDAP_CONTROL_SYNC,
2259                 SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
2260                 syncprov_parseCtrl, &slap_cids.sc_LDAPsync );
2261         if ( rc != LDAP_SUCCESS ) {
2262                 fprintf( stderr, "Failed to register control %d\n", rc );
2263                 return rc;
2264         }
2265
2266         syncprov.on_bi.bi_type = "syncprov";
2267         syncprov.on_bi.bi_db_init = syncprov_db_init;
2268         syncprov.on_bi.bi_db_config = syncprov_db_config;
2269         syncprov.on_bi.bi_db_destroy = syncprov_db_destroy;
2270         syncprov.on_bi.bi_db_open = syncprov_db_open;
2271         syncprov.on_bi.bi_db_close = syncprov_db_close;
2272
2273         syncprov.on_bi.bi_op_abandon = syncprov_op_abandon;
2274         syncprov.on_bi.bi_op_cancel = syncprov_op_abandon;
2275
2276         syncprov.on_bi.bi_op_add = syncprov_op_mod;
2277         syncprov.on_bi.bi_op_compare = syncprov_op_compare;
2278         syncprov.on_bi.bi_op_delete = syncprov_op_mod;
2279         syncprov.on_bi.bi_op_modify = syncprov_op_mod;
2280         syncprov.on_bi.bi_op_modrdn = syncprov_op_mod;
2281         syncprov.on_bi.bi_op_search = syncprov_op_search;
2282         syncprov.on_bi.bi_extended = syncprov_op_extended;
2283         syncprov.on_bi.bi_operational = syncprov_operational;
2284
2285         return overlay_register( &syncprov );
2286 }
2287
2288 #if SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC
2289 int
2290 init_module( int argc, char *argv[] )
2291 {
2292         return syncprov_init();
2293 }
2294 #endif /* SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC */
2295
2296 #endif /* defined(SLAPD_OVER_SYNCPROV) */