]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/syncprov.c
Getting refresh working
[openldap] / servers / slapd / overlays / syncprov.c
1 /* syncprov.c - syncrepl provider */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by Howard Chu for inclusion in
17  * OpenLDAP Software.
18  */
19
20 #include "portable.h"
21
22 #define SLAPD_OVER_SYNCPROV     SLAPD_MOD_DYNAMIC
23
24 #ifdef SLAPD_OVER_SYNCPROV
25
26 #include <ac/string.h>
27 #include "lutil.h"
28 #include "slap.h"
29
30 /* Record of a persistent search */
31 typedef struct syncops {
32         struct syncops *s_next;
33         struct berval   s_base;         /* ndn of search base */
34         ID              s_eid;          /* entryID of search base */
35         Operation       *s_op;          /* search op */
36         int             s_flags;        /* search status */
37 } syncops;
38
39 #define PS_IS_REFRESHING        0x01
40
41 /* Record of which searches matched at premodify step */
42 typedef struct syncmatches {
43         struct syncmatches *sm_next;
44         syncops *sm_op;
45 } syncmatches;
46
47 typedef struct syncprov_info_t {
48         syncops         *si_ops;
49         struct berval   si_ctxcsn;      /* ldapsync context */
50         int             si_gotcsn;      /* is our ctxcsn up to date? */
51         ldap_pvt_thread_mutex_t si_csn_mutex;
52         ldap_pvt_thread_mutex_t si_ops_mutex;
53 } syncprov_info_t;
54
55 typedef struct opcookie {
56         slap_overinst *son;
57         syncmatches *smatches;
58         struct berval suuid;
59 } opcookie;
60
61 typedef struct findcookie {
62         struct berval *fdn;
63         syncops *fss;
64         int fbase;
65         int fsuffix;
66 } findcookie;
67
68 static AttributeName csn_anlist[2];
69 static AttributeName uuid_anlist[2];
70
71 static int
72 dn_avl_cmp( const void *c1, const void *c2 )
73 {
74         struct berval *bv1 = (struct berval *)c1;
75         struct berval *bv2 = (struct berval *)c2;
76         int rc = bv1->bv_len - bv2->bv_len;
77
78         if ( rc ) return rc;
79         return ber_bvcmp( bv1, bv2 );
80 }
81
82 static int
83 findbase_cb( Operation *op, SlapReply *rs )
84 {
85         slap_callback *sc = op->o_callback;
86
87         if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
88                 findcookie *fc = sc->sc_private;
89                 if ( rs->sr_entry->e_id == fc->fss->s_eid &&
90                         dn_match( &rs->sr_entry->e_nname, &fc->fss->s_base )) {
91                         fc->fbase = 1;
92                         fc->fsuffix = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname );
93                 }
94         }
95         return LDAP_SUCCESS;
96 }
97
98 static int
99 syncprov_findbase( Operation *op, syncops *ss, findcookie *fc )
100 {
101         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
102         syncprov_info_t         *si = on->on_bi.bi_private;
103
104         slap_callback cb = {0};
105         Operation fop;
106         SlapReply frs = { REP_RESULT };
107         int rc;
108
109         fop = *op;
110
111         cb.sc_response = findbase_cb;
112         cb.sc_private = fc;
113
114         fop.o_callback = &cb;
115         fop.o_tag = LDAP_REQ_SEARCH;
116         fop.ors_scope = LDAP_SCOPE_BASE;
117         fop.ors_deref = ss->s_op->ors_deref;
118         fop.ors_slimit = 1;
119         fop.ors_tlimit = SLAP_NO_LIMIT;
120         fop.ors_attrs = slap_anlist_no_attrs;
121         fop.ors_attrsonly = 1;
122         fop.ors_filter = ss->s_op->ors_filter;
123         fop.ors_filterstr = ss->s_op->ors_filterstr;
124
125         fop.o_req_ndn = ss->s_op->o_req_ndn;
126
127         rc = fop.o_bd->be_search( &fop, &frs );
128
129         if ( fc->fbase ) return LDAP_SUCCESS;
130
131         /* If entryID has changed, then the base of this search has
132          * changed. Invalidate the psearch.
133          */
134         return LDAP_NO_SUCH_OBJECT;
135 }
136
137 #define FIND_CSN        1
138 #define FIND_PRESENT    2
139
140 typedef struct fcsn_cookie {
141         struct berval maxcsn;
142         int gotmatch;
143 } fcsn_cookie;
144
145 static int
146 findcsn_cb( Operation *op, SlapReply *rs )
147 {
148         slap_callback *sc = op->o_callback;
149
150         if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
151                 if ( sc->sc_private ) {
152                         int i;
153                         fcsn_cookie *fc = sc->sc_private;
154                         Attribute *a = attr_find(rs->sr_entry->e_attrs,
155                                 slap_schema.si_ad_entryCSN );
156                         i = ber_bvcmp( &a->a_vals[0], op->o_sync_state.ctxcsn );
157                         if ( i == 0 ) fc->gotmatch = 1;
158                         i = ber_bvcmp( &a->a_vals[0], &fc->maxcsn );
159                         if ( i > 0 ) {
160                                 fc->maxcsn.bv_len = a->a_vals[0].bv_len;
161                                 strcpy(fc->maxcsn.bv_val, a->a_vals[0].bv_val );
162                         }
163                 } else {
164                         sc->sc_private = (void *)1;
165                 }
166         }
167         return LDAP_SUCCESS;
168 }
169
170 typedef struct fpres_cookie {
171         int num;
172         BerVarray uuids;
173 } fpres_cookie;
174
175 static int
176 findpres_cb( Operation *op, SlapReply *rs )
177 {
178         slap_callback *sc = op->o_callback;
179         fpres_cookie *pc = sc->sc_private;
180         int ret;
181
182         if ( rs->sr_type == REP_SEARCH ) {
183                 ret = slap_sync_build_syncUUID_set( op, &pc->uuids, rs->sr_entry );
184                 if ( ret > 0 ) {
185                         pc->num++;
186                         if ( pc->num == SLAP_SYNCUUID_SET_SIZE ) {
187                                 ret = slap_send_syncinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
188                                         0, pc->uuids, 0 );
189                                 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
190                                 pc->uuids = NULL;
191                                 pc->num = 0;
192                         }
193                 } else {
194                         ret = LDAP_OTHER;
195                 }
196         } else if ( rs->sr_type == REP_RESULT ) {
197                 ret = rs->sr_err;
198                 if ( pc->num ) {
199                         ret = slap_send_syncinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
200                                 0, pc->uuids, 0 );
201                         ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
202                         pc->uuids = NULL;
203                         pc->num = 0;
204                 }
205         }
206         return ret;
207 }
208
209
210 static int
211 syncprov_findcsn( Operation *op, int mode )
212 {
213         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
214         syncprov_info_t         *si = on->on_bi.bi_private;
215
216         slap_callback cb = {0};
217         Operation fop;
218         SlapReply frs = { REP_RESULT };
219         char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
220         char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
221         struct berval fbuf;
222         Filter cf;
223         AttributeAssertion eq;
224         int rc;
225         fcsn_cookie fcookie;
226         fpres_cookie pcookie;
227         int locked = 0;
228
229         if ( op->o_sync_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
230                 return LDAP_OTHER;
231         }
232
233         fop = *op;
234         fop.o_sync_mode = 0;
235
236         fbuf.bv_val = buf;
237         if ( mode == FIND_CSN ) {
238                 if ( !si->si_gotcsn ) {
239                         /* If we don't know the current ctxcsn, find it */
240                         ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
241                         locked = 1;
242                 }
243                 if ( !si->si_gotcsn ) {
244                         cf.f_choice = LDAP_FILTER_GE;
245                         fop.ors_attrsonly = 0;
246                         fop.ors_attrs = csn_anlist;
247                         fop.ors_slimit = SLAP_NO_LIMIT;
248                         cb.sc_private = &fcookie;
249                         fcookie.maxcsn.bv_val = cbuf;
250                         fcookie.maxcsn.bv_len = 0;
251                         fcookie.gotmatch = 0;
252                         fbuf.bv_len = sprintf( buf, "(entryCSN>=%s)", op->o_sync_state.ctxcsn->bv_val );
253                 } else {
254                         if ( locked ) {
255                                 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
256                                 locked = 1;
257                         }
258                         cf.f_choice = LDAP_FILTER_EQUALITY;
259                         fop.ors_attrsonly = 1;
260                         fop.ors_attrs = slap_anlist_no_attrs;
261                         fop.ors_slimit = 1;
262                         cb.sc_private = NULL;
263                         fbuf.bv_len = sprintf( buf, "(entryCSN=%s)", op->o_sync_state.ctxcsn->bv_val );
264                 }
265                 cb.sc_response = findcsn_cb;
266
267         } else if ( mode == FIND_PRESENT ) {
268                 cf.f_choice = LDAP_FILTER_LE;
269                 fop.ors_attrsonly = 0;
270                 fop.ors_attrs = uuid_anlist;
271                 fop.ors_slimit = SLAP_NO_LIMIT;
272                 cb.sc_private = &fcookie;
273                 cb.sc_response = findpres_cb;
274                 pcookie.num = 0;
275                 pcookie.uuids = NULL;
276                 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", op->o_sync_state.ctxcsn->bv_val );
277         }
278         cf.f_ava = &eq;
279         cf.f_av_desc = slap_schema.si_ad_entryCSN;
280         cf.f_av_value = *op->o_sync_state.ctxcsn;
281         cf.f_next = NULL;
282
283         fop.o_callback = &cb;
284         fop.ors_slimit = 1;
285         fop.ors_tlimit = SLAP_NO_LIMIT;
286         fop.ors_filter = &cf;
287         fop.ors_filterstr = fbuf;
288
289         fop.o_bd->bd_info = on->on_info->oi_orig;
290         rc = fop.o_bd->be_search( &fop, &frs );
291
292         if ( mode == FIND_CSN ) {
293                 if ( !si->si_gotcsn ) {
294                         ber_dupbv( &si->si_ctxcsn, &fcookie.maxcsn );
295                         si->si_gotcsn = 1;
296                         ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
297                         if ( fcookie.gotmatch ) return LDAP_SUCCESS;
298                         
299                 } else {
300                         if ( cb.sc_private ) return LDAP_SUCCESS;
301                 }
302         } else if ( mode == FIND_PRESENT ) {
303                 return LDAP_SUCCESS;
304         }
305
306         /* If matching CSN was not found, invalidate the context. */
307         return LDAP_NO_SUCH_OBJECT;
308 }
309
310 static void
311 syncprov_matchops( Operation *op, opcookie *opc, int saveit )
312 {
313         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
314         syncprov_info_t         *si = on->on_bi.bi_private;
315
316         findcookie fc;
317         syncops *ss;
318         Entry *e;
319         Attribute *a;
320         int rc;
321
322         fc.fdn = &op->o_req_ndn;
323         rc = be_entry_get_rw( op, fc.fdn, NULL, NULL, 0, &e );
324         if ( rc ) return;
325
326         if ( saveit ) {
327                 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
328                 if ( a )
329                         ber_dupbv_x( &opc->suuid, &a->a_vals[0], op->o_tmpmemctx );
330         }
331
332         ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
333         for (ss = si->si_ops; ss; ss=ss->s_next)
334         {
335                 syncmatches *sm;
336                 int found = 0;
337
338                 /* validate base */
339                 fc.fss = ss;
340                 fc.fbase = 0;
341                 fc.fsuffix = 0;
342                 rc = syncprov_findbase( op, ss, &fc );
343                 if ( rc != LDAP_SUCCESS ) continue;
344
345                 /* If we're sending results now, look for this op in old matches */
346                 if ( !saveit ) {
347                         syncmatches *old;
348                         for ( sm=opc->smatches, old=(syncmatches *)&opc->smatches; sm;
349                                 old=sm, sm=sm->sm_next ) {
350                                 if ( sm->sm_op == ss ) {
351                                         found = 1;
352                                         old->sm_next = sm->sm_next;
353                                         op->o_tmpfree( sm, op->o_tmpmemctx );
354                                         break;
355                                 }
356                         }
357                 }
358
359                 /* check if current o_req_dn is in scope and matches filter */
360                 if ( fc.fsuffix && test_filter( op, e, ss->s_op->ors_filter ) ==
361                         LDAP_COMPARE_TRUE ) {
362                         if ( saveit ) {
363                                 sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
364                                 sm->sm_next = opc->smatches;
365                                 sm->sm_op = ss;
366                                 opc->smatches = sm;
367                         } else {
368                                 /* if found send UPDATE else send ADD */
369                                 if ( found ) {
370                                 } else {
371                                 }
372                         }
373                 } else if ( !saveit && found ) {
374                         /* send DELETE */
375                 }
376         }
377         ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
378         be_entry_release_r( op, e );
379 }
380
381 static int
382 syncprov_op_response( Operation *op, SlapReply *rs )
383 {
384         slap_callback *cb = op->o_callback;
385         opcookie *opc = (opcookie *)(cb+1);
386         slap_overinst *on = opc->son;
387         syncprov_info_t         *si = on->on_bi.bi_private;
388         syncmatches *sm, *snext;
389
390         if ( rs->sr_err == LDAP_SUCCESS )
391         {
392                 struct berval maxcsn;
393                 void *memctx = op->o_tmpmemctx;
394
395                 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
396                 op->o_tmpmemctx = NULL;
397                 slap_get_commit_csn( op, &maxcsn );
398                 op->o_tmpmemctx = memctx;
399                 if ( maxcsn.bv_val ) {
400                         free( si->si_ctxcsn.bv_val );
401                         si->si_ctxcsn = maxcsn;
402                         si->si_gotcsn = 1;
403                 }
404                 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
405
406                 switch(op->o_tag) {
407                 case LDAP_REQ_ADD:
408                 case LDAP_REQ_MODIFY:
409                 case LDAP_REQ_MODRDN:
410                 case LDAP_REQ_EXTENDED:
411                         syncprov_matchops( op, opc, 0 );
412                         break;
413                 case LDAP_REQ_DELETE:
414                         /* for each match in opc->smatches:
415                          *   send DELETE msg
416                          */
417                         for ( sm = opc->smatches; sm; sm=sm->sm_next ) {
418                         }
419                         break;
420                 }
421
422         }
423         for (sm = opc->smatches; sm; sm=snext) {
424                 snext = sm->sm_next;
425                 op->o_tmpfree( sm, op->o_tmpmemctx );
426         }
427         op->o_callback = cb->sc_next;
428         op->o_tmpfree(cb, op->o_tmpmemctx);
429         return SLAP_CB_CONTINUE;
430 }
431
432 #if 0
433 static int
434 syncprov_op_compare( Operation *op, SlapReply *rs )
435 {
436         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
437         syncprov_info_t         *si = on->on_bi.bi_private;
438         int rc = SLAP_CB_CONTINUE;
439
440         if ( dn_match( &op->o_req_ndn, &si->si_e->e_nname ) )
441         {
442                 Attribute *a;
443
444                 ldap_pvt_thread_mutex_lock( &si->si_e_mutex );
445
446                 if ( get_assert( op ) &&
447                         ( test_filter( op, si->si_e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
448                 {
449                         rs->sr_err = LDAP_ASSERTION_FAILED;
450                         goto return_results;
451                 }
452
453                 rs->sr_err = access_allowed( op, si->si_e, op->oq_compare.rs_ava->aa_desc,
454                         &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
455                 if ( ! rs->sr_err ) {
456                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
457                         goto return_results;
458                 }
459
460                 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
461
462                 for ( a = attr_find( si->si_e->e_attrs, op->oq_compare.rs_ava->aa_desc );
463                         a != NULL;
464                         a = attr_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
465                 {
466                         rs->sr_err = LDAP_COMPARE_FALSE;
467
468                         if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
469                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
470                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
471                                 a->a_nvals, &op->oq_compare.rs_ava->aa_value, op->o_tmpmemctx ) == 0 )
472                         {
473                                 rs->sr_err = LDAP_COMPARE_TRUE;
474                                 break;
475                         }
476                 }
477
478 return_results:;
479
480                 ldap_pvt_thread_mutex_unlock( &si->si_e_mutex );
481
482                 send_ldap_result( op, rs );
483
484                 if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
485                         rs->sr_err = LDAP_SUCCESS;
486                 }
487                 rc = rs->sr_err;
488         }
489
490         return SLAP_CB_CONTINUE;
491 }
492 #endif
493         
494 static int
495 syncprov_op_mod( Operation *op, SlapReply *rs )
496 {
497         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
498         syncprov_info_t         *si = on->on_bi.bi_private;
499
500         if ( si->si_ops )
501         {
502                 slap_callback *cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(opcookie), op->o_tmpmemctx);
503                 opcookie *opc = (opcookie *)(cb+1);
504                 opc->son = on;
505                 cb->sc_response = syncprov_op_response;
506                 cb->sc_private = opc;
507                 cb->sc_next = op->o_callback;
508                 op->o_callback = cb;
509
510                 if ( op->o_tag != LDAP_REQ_ADD )
511                         syncprov_matchops( op, opc, 1 );
512         }
513
514         return SLAP_CB_CONTINUE;
515 }
516
517 static int
518 syncprov_op_extended( Operation *op, SlapReply *rs )
519 {
520         if ( exop_is_write( op ))
521                 return syncprov_op_mod( op, rs );
522
523         return SLAP_CB_CONTINUE;
524 }
525
526 static int
527 syncprov_search_cleanup( Operation *op, SlapReply *rs )
528 {
529         if ( rs->sr_ctrls ) {
530                 free( rs->sr_ctrls[0] );
531                 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
532         }
533         return 0;
534 }
535
536 static int
537 syncprov_search_response( Operation *op, SlapReply *rs )
538 {
539         slap_callback *cb = op->o_callback;
540         slap_overinst *on = cb->sc_private;
541         syncprov_info_t         *si = on->on_bi.bi_private;
542
543         if ( rs->sr_type == REP_SEARCH ) {
544                 int i;
545                 if ( op->o_sync_state.ctxcsn ) {
546                         Attribute *a = attr_find( rs->sr_entry->e_attrs,
547                                 slap_schema.si_ad_entryCSN );
548                         /* Don't send the ctx entry twice */
549                         if ( bvmatch( &a->a_nvals[0], op->o_sync_state.ctxcsn ))
550                                 return LDAP_SUCCESS;
551                 }
552                 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
553                         op->o_tmpmemctx );
554                 rs->sr_ctrls[1] = NULL;
555                 rs->sr_err = slap_build_sync_state_ctrl( op, rs, rs->sr_entry,
556                         LDAP_SYNC_ADD, rs->sr_ctrls, 0, 0, NULL );
557         } else if (rs->sr_type == REP_RESULT ) {
558                 struct berval cookie;
559                 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
560                         op->o_tmpmemctx );
561                 rs->sr_ctrls[1] = NULL;
562                 slap_compose_sync_cookie( op, &cookie,
563                         &op->ors_filter->f_and->f_ava->aa_value,
564                         op->o_sync_state.sid, op->o_sync_state.rid );
565                 rs->sr_err = slap_build_sync_done_ctrl( op, rs, rs->sr_ctrls,
566                         0, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
567         }
568
569         return SLAP_CB_CONTINUE;
570 }
571
572 static int
573 syncprov_op_search( Operation *op, SlapReply *rs )
574 {
575         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
576         syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
577         slap_callback   *cb;
578         int gotstate = 0;
579         Filter *fand, *fava;
580
581         if ( !op->o_sync_mode ) return SLAP_CB_CONTINUE;
582
583         if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
584                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "illegal value for derefAliases" );
585                 return rs->sr_err;
586         }
587
588         /* If we have a cookie, handle the PRESENT lookups
589          */
590         if ( op->o_sync_state.ctxcsn ) {
591                 /* Is the CSN in a valid format? */
592                 if ( op->o_sync_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
593                         send_ldap_error( op, rs, LDAP_OTHER, "invalid sync cookie" );
594                         return rs->sr_err;
595                 }
596                 /* Is the CSN still present in the database? */
597                 if ( syncprov_findcsn( op, FIND_CSN ) != LDAP_SUCCESS ) {
598                         /* No, so a reload is required */
599                         if ( op->o_sync_rhint == 0 ) {
600                                 send_ldap_error( op, rs, LDAP_SYNC_REFRESH_REQUIRED, "sync cookie is stale" );
601                                 return rs->sr_err;
602                         }
603                 } else {
604                         /* Does it match the current ctxCSN? */
605                         if ( bvmatch( op->o_sync_state.ctxcsn, &si->si_ctxcsn )) {
606                                 struct berval cookie;
607                                 LDAPControl     *ctrls[2];
608
609                                 ctrls[0] = NULL;
610                                 ctrls[1] = NULL;
611                                 slap_compose_sync_cookie( op, &cookie, op->o_sync_state.ctxcsn,
612                                         op->o_sync_state.sid, op->o_sync_state.rid );
613                                 slap_build_sync_done_ctrl( op, rs, ctrls, 0, 1,
614                                         &cookie, LDAP_SYNC_REFRESH_DELETES );
615                                 rs->sr_err = LDAP_SUCCESS;
616                                 send_ldap_result( op, rs );
617                                 free( cookie.bv_val );
618                                 return rs->sr_err;
619                         }
620                         gotstate = 1;
621                         /* OK, let's send all the Present UUIDs */
622                         if ( syncprov_findcsn( op, FIND_PRESENT ) != LDAP_SUCCESS ) {
623                                 send_ldap_result( op, rs );
624                                 return rs->sr_err;
625                         }
626                 }
627         }
628
629         if ( !gotstate && !si->si_gotcsn ) {
630                 struct berval bv = BER_BVC("1"), *old;
631                 
632                 old = op->o_sync_state.ctxcsn;
633                 op->o_sync_state.ctxcsn = &bv;
634                 syncprov_findcsn( op, FIND_CSN );
635                 op->o_sync_state.ctxcsn = old;
636         }
637
638         /* Append CSN range to search filter */
639         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
640
641         fand = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
642         fand->f_choice = LDAP_FILTER_AND;
643         fand->f_next = NULL;
644         fava = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
645         fava->f_choice = LDAP_FILTER_LE;
646         fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
647         fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
648         ber_dupbv_x( &fava->f_ava->aa_value, &si->si_ctxcsn, op->o_tmpmemctx );
649         fand->f_and = fava;
650         if ( gotstate ) {
651                 fava->f_next = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
652                 fava = fava->f_next;
653                 fava->f_choice = LDAP_FILTER_GE;
654                 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
655                 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
656                 ber_dupbv_x( &fava->f_ava->aa_value, op->o_sync_state.ctxcsn, op->o_tmpmemctx );
657         }
658         fava->f_next = op->ors_filter;
659         op->ors_filter = fand;
660         filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
661
662         /* Let our callback add needed info to returned entries */
663         cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(opcookie), op->o_tmpmemctx);
664         cb->sc_response = syncprov_search_response;
665         cb->sc_cleanup = syncprov_search_cleanup;
666         cb->sc_private = on;
667         cb->sc_next = op->o_callback;
668         op->o_callback = cb;
669
670         return SLAP_CB_CONTINUE;
671 }
672
673 #if 0
674 static int
675 syncprov_response( Operation *op, SlapReply *rs )
676 {
677         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
678         syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
679
680         if ( rs->sr_err == LDAP_SUCCESS ) {
681                 if ( op->o_tag == LDAP_REQ_SEARCH ) {
682                         /* handle transition from refresh to persist */
683                         if ( op->o_sync_mode == SLAP_SYNC_REFRESH_AND_PERSIST ) {
684                         }
685
686                 /* If we're checkpointing */
687                 } else if ( si->si_chkops || si->si_chktime )) {
688                         int do_check = 0;
689
690                         switch ( op->o_tag ) {
691                         case LDAP_REQ_EXTENDED:
692                                 { int i, doit = 0;
693
694                                 /* if not PASSWD_MODIFY, break */
695                                 for ( i=0; write_exop[i]; i++ )
696                                 {
697                                         if ( !ber_bvcmp( write_exop[i], &op->oq_extended.rs_reqoid ))
698                                         {
699                                                 doit = 1;
700                                                 break;
701                                         }
702                                 }
703                                 if ( !doit ) break;
704                                 }
705                                 /* else fallthru */
706                         case LDAP_REQ_ADD:
707                         case LDAP_REQ_MODIFY:
708                         case LDAP_REQ_MODRDN:
709                         case LDAP_REQ_DELETE:
710                                 ldap_pvt_thread_mutex_lock( &si->si_chk_mutex );
711                                 if ( si->si_chkops )
712                                 {
713                                         si->si_numops++;
714                                         if ( si->si_numops >= si->si_chkops )
715                                         {
716                                                 do_check = 1;
717                                                 si->si_numops = 0;
718                                         }
719                                 }
720                                 if ( si->si_chktime )
721                                 {
722                                         if ( op->o_time - si->si_chklast >= si->si_chktime )
723                                         {
724                                                 do_check = 1;
725                                                 si->si_chklast = op->o_time;
726                                         }
727                                 }
728                                 ldap_pvt_thread_mutex_unlock( &si->si_chk_mutex );
729                                 if ( do_check )
730                                 {
731                                         /* write cn=ldapsync to underlying db */
732                                 }
733                                 break;
734                         }
735                 }
736         }
737         /* Release this DN */
738         if ( op->o_tag == LDAP_REQ_MODIFY ) {
739                 ldap_pvt_thread_mutex_lock( &si->si_mod_mutex );
740                 avl_delete( &si->si_mods, &op->o_req_ndn, dn_avl_cmp );
741                 ldap_pvt_thread_mutex_unlock( &si->si_mod_mutex );
742         }
743         return SLAP_CB_CONTINUE;
744 }
745 #endif
746
747 static int
748 syncprov_db_config(
749         BackendDB       *be,
750         const char      *fname,
751         int             lineno,
752         int             argc,
753         char    **argv
754 )
755 {
756         slap_overinst           *on = (slap_overinst *)be->bd_info;
757         syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
758
759 #if 0
760         if ( strcasecmp( argv[ 0 ], "syncprov-checkpoint" ) == 0 ) {
761                 if ( argc != 3 ) {
762                         fprintf( stderr, "%s: line %d: wrong number of arguments in "
763                                 "\"syncprov-checkpoint <ops> <minutes>\"\n", fname, lineno );
764                         return -1;
765                 }
766                 si->si_chkops = atoi( argv[1] );
767                 si->si_chktime = atoi( argv[2] ) * 60;
768
769         } else {
770                 return SLAP_CONF_UNKNOWN;
771         }
772 #endif
773
774         return SLAP_CONF_UNKNOWN;
775 }
776
777 static int
778 syncprov_db_init(
779         BackendDB *be
780 )
781 {
782         slap_overinst   *on = (slap_overinst *)be->bd_info;
783         syncprov_info_t *si;
784
785         si = ch_calloc(1, sizeof(syncprov_info_t));
786         on->on_bi.bi_private = si;
787         ldap_pvt_thread_mutex_init( &si->si_csn_mutex );
788         ldap_pvt_thread_mutex_init( &si->si_ops_mutex );
789
790         csn_anlist[0].an_desc = slap_schema.si_ad_entryCSN;
791         csn_anlist[0].an_name = slap_schema.si_ad_entryCSN->ad_cname;
792
793         uuid_anlist[0].an_desc = slap_schema.si_ad_entryUUID;
794         uuid_anlist[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
795
796         return 0;
797 }
798
799 static int
800 syncprov_db_destroy(
801         BackendDB *be
802 )
803 {
804         slap_overinst   *on = (slap_overinst *)be->bd_info;
805         syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
806
807         if ( si ) {
808                 ldap_pvt_thread_mutex_destroy( &si->si_ops_mutex );
809                 ldap_pvt_thread_mutex_destroy( &si->si_csn_mutex );
810                 ch_free( si );
811         }
812
813         return 0;
814 }
815
816 /* This overlay is set up for dynamic loading via moduleload. For static
817  * configuration, you'll need to arrange for the slap_overinst to be
818  * initialized and registered by some other function inside slapd.
819  */
820
821 static slap_overinst            syncprov;
822
823 int
824 syncprov_init()
825 {
826         syncprov.on_bi.bi_type = "syncprov";
827         syncprov.on_bi.bi_db_init = syncprov_db_init;
828         syncprov.on_bi.bi_db_config = syncprov_db_config;
829         syncprov.on_bi.bi_db_destroy = syncprov_db_destroy;
830
831         syncprov.on_bi.bi_op_add = syncprov_op_mod;
832 #if 0
833         syncprov.on_bi.bi_op_compare = syncprov_op_compare;
834 #endif
835         syncprov.on_bi.bi_op_delete = syncprov_op_mod;
836         syncprov.on_bi.bi_op_modify = syncprov_op_mod;
837         syncprov.on_bi.bi_op_modrdn = syncprov_op_mod;
838         syncprov.on_bi.bi_op_search = syncprov_op_search;
839         syncprov.on_bi.bi_extended = syncprov_op_extended;
840
841 #if 0
842         syncprov.on_response = syncprov_response;
843 #endif
844
845         return overlay_register( &syncprov );
846 }
847
848 #if SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC
849 int
850 init_module( int argc, char *argv[] )
851 {
852         return syncprov_init();
853 }
854 #endif /* SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC */
855
856 #endif /* defined(SLAPD_OVER_SYNCPROV) */