]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/syncprov.c
3690d45a6c49aaf542052c69a92b6252f0afacfa
[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 = SLAP_CB_CONTINUE;
181
182         if ( rs->sr_type == REP_SEARCH ) {
183                 Debug(LDAP_DEBUG_TRACE, "present %s\n", rs->sr_entry->e_name.bv_val, 0, 0);
184                 ret = slap_build_syncUUID_set( op, &pc->uuids, rs->sr_entry );
185                 if ( ret > 0 ) {
186                         pc->num++;
187                         ret = LDAP_SUCCESS;
188                         if ( pc->num == SLAP_SYNCUUID_SET_SIZE ) {
189                                 rs->sr_rspoid = LDAP_SYNC_INFO;
190                                 ret = slap_send_syncinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
191                                         0, pc->uuids, 0 );
192                                 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
193                                 pc->uuids = NULL;
194                                 pc->num = 0;
195                         }
196                 } else {
197                         ret = LDAP_OTHER;
198                 }
199         } else if ( rs->sr_type == REP_RESULT ) {
200                 ret = rs->sr_err;
201                 if ( pc->num ) {
202                         rs->sr_rspoid = LDAP_SYNC_INFO;
203                         ret = slap_send_syncinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
204                                 0, pc->uuids, 0 );
205                         ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
206                         pc->uuids = NULL;
207                         pc->num = 0;
208                 }
209         }
210         return ret;
211 }
212
213
214 static int
215 syncprov_findcsn( Operation *op, int mode )
216 {
217         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
218         syncprov_info_t         *si = on->on_bi.bi_private;
219
220         slap_callback cb = {0};
221         Operation fop;
222         SlapReply frs = { REP_RESULT };
223         char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
224         char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
225         struct berval fbuf;
226         Filter cf;
227         AttributeAssertion eq;
228         int rc;
229         fcsn_cookie fcookie;
230         fpres_cookie pcookie;
231         int locked = 0;
232
233         if ( op->o_sync_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
234                 return LDAP_OTHER;
235         }
236
237         fop = *op;
238         fop.o_sync_mode = 0;
239
240         fbuf.bv_val = buf;
241         if ( mode == FIND_CSN ) {
242                 if ( !si->si_gotcsn ) {
243                         /* If we don't know the current ctxcsn, find it */
244                         ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
245                         locked = 1;
246                 }
247                 if ( !si->si_gotcsn ) {
248                         cf.f_choice = LDAP_FILTER_GE;
249                         fop.ors_attrsonly = 0;
250                         fop.ors_attrs = csn_anlist;
251                         fop.ors_slimit = SLAP_NO_LIMIT;
252                         cb.sc_private = &fcookie;
253                         fcookie.maxcsn.bv_val = cbuf;
254                         fcookie.maxcsn.bv_len = 0;
255                         fcookie.gotmatch = 0;
256                         fbuf.bv_len = sprintf( buf, "(entryCSN>=%s)", op->o_sync_state.ctxcsn->bv_val );
257                 } else {
258                         if ( locked ) {
259                                 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
260                                 locked = 1;
261                         }
262                         cf.f_choice = LDAP_FILTER_EQUALITY;
263                         fop.ors_attrsonly = 1;
264                         fop.ors_attrs = slap_anlist_no_attrs;
265                         fop.ors_slimit = 1;
266                         cb.sc_private = NULL;
267                         fbuf.bv_len = sprintf( buf, "(entryCSN=%s)", op->o_sync_state.ctxcsn->bv_val );
268                 }
269                 cb.sc_response = findcsn_cb;
270
271         } else if ( mode == FIND_PRESENT ) {
272                 cf.f_choice = LDAP_FILTER_LE;
273                 fop.ors_attrsonly = 0;
274                 fop.ors_attrs = uuid_anlist;
275                 fop.ors_slimit = SLAP_NO_LIMIT;
276                 cb.sc_private = &pcookie;
277                 cb.sc_response = findpres_cb;
278                 pcookie.num = 0;
279                 pcookie.uuids = NULL;
280                 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", op->o_sync_state.ctxcsn->bv_val );
281         }
282         cf.f_ava = &eq;
283         cf.f_av_desc = slap_schema.si_ad_entryCSN;
284         cf.f_av_value = *op->o_sync_state.ctxcsn;
285         cf.f_next = NULL;
286
287         fop.o_callback = &cb;
288         fop.ors_tlimit = SLAP_NO_LIMIT;
289         fop.ors_filter = &cf;
290         fop.ors_filterstr = fbuf;
291
292         fop.o_bd->bd_info = on->on_info->oi_orig;
293         rc = fop.o_bd->be_search( &fop, &frs );
294         fop.o_bd->bd_info = on;
295
296         if ( mode == FIND_CSN ) {
297                 if ( !si->si_gotcsn ) {
298                         ber_dupbv( &si->si_ctxcsn, &fcookie.maxcsn );
299                         si->si_gotcsn = 1;
300                         ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
301                         if ( fcookie.gotmatch ) return LDAP_SUCCESS;
302                         
303                 } else {
304                         if ( cb.sc_private ) return LDAP_SUCCESS;
305                 }
306         } else if ( mode == FIND_PRESENT ) {
307                 return LDAP_SUCCESS;
308         }
309
310         /* If matching CSN was not found, invalidate the context. */
311         return LDAP_NO_SUCH_OBJECT;
312 }
313
314 static void
315 syncprov_matchops( Operation *op, opcookie *opc, int saveit )
316 {
317         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
318         syncprov_info_t         *si = on->on_bi.bi_private;
319
320         findcookie fc;
321         syncops *ss;
322         Entry *e;
323         Attribute *a;
324         int rc;
325
326         fc.fdn = &op->o_req_ndn;
327         rc = be_entry_get_rw( op, fc.fdn, NULL, NULL, 0, &e );
328         if ( rc ) return;
329
330         if ( saveit ) {
331                 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
332                 if ( a )
333                         ber_dupbv_x( &opc->suuid, &a->a_vals[0], op->o_tmpmemctx );
334         }
335
336         ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
337         for (ss = si->si_ops; ss; ss=ss->s_next)
338         {
339                 syncmatches *sm;
340                 int found = 0;
341
342                 /* validate base */
343                 fc.fss = ss;
344                 fc.fbase = 0;
345                 fc.fsuffix = 0;
346                 rc = syncprov_findbase( op, ss, &fc );
347                 if ( rc != LDAP_SUCCESS ) continue;
348
349                 /* If we're sending results now, look for this op in old matches */
350                 if ( !saveit ) {
351                         syncmatches *old;
352                         for ( sm=opc->smatches, old=(syncmatches *)&opc->smatches; sm;
353                                 old=sm, sm=sm->sm_next ) {
354                                 if ( sm->sm_op == ss ) {
355                                         found = 1;
356                                         old->sm_next = sm->sm_next;
357                                         op->o_tmpfree( sm, op->o_tmpmemctx );
358                                         break;
359                                 }
360                         }
361                 }
362
363                 /* check if current o_req_dn is in scope and matches filter */
364                 if ( fc.fsuffix && test_filter( op, e, ss->s_op->ors_filter ) ==
365                         LDAP_COMPARE_TRUE ) {
366                         if ( saveit ) {
367                                 sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
368                                 sm->sm_next = opc->smatches;
369                                 sm->sm_op = ss;
370                                 opc->smatches = sm;
371                         } else {
372                                 /* if found send UPDATE else send ADD */
373                                 if ( found ) {
374                                 } else {
375                                 }
376                         }
377                 } else if ( !saveit && found ) {
378                         /* send DELETE */
379                 }
380         }
381         ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
382         be_entry_release_r( op, e );
383 }
384
385 static int
386 syncprov_op_cleanup( Operation *op, SlapReply *rs )
387 {
388         slap_callback *cb = op->o_callback;
389         opcookie *opc = (opcookie *)(cb+1);
390         syncmatches *sm, *snext;
391
392         for (sm = opc->smatches; sm; sm=snext) {
393                 snext = sm->sm_next;
394                 op->o_tmpfree( sm, op->o_tmpmemctx );
395         }
396         op->o_callback = cb->sc_next;
397         op->o_tmpfree(cb, op->o_tmpmemctx);
398 }
399
400 static int
401 syncprov_op_response( Operation *op, SlapReply *rs )
402 {
403         slap_callback *cb = op->o_callback;
404         opcookie *opc = (opcookie *)(cb+1);
405         slap_overinst *on = opc->son;
406         syncprov_info_t         *si = on->on_bi.bi_private;
407         syncmatches *sm;
408
409         if ( rs->sr_err == LDAP_SUCCESS )
410         {
411                 struct berval maxcsn;
412                 void *memctx = op->o_tmpmemctx;
413
414                 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
415                 op->o_tmpmemctx = NULL;
416                 slap_get_commit_csn( op, &maxcsn );
417                 op->o_tmpmemctx = memctx;
418                 if ( maxcsn.bv_val ) {
419                         free( si->si_ctxcsn.bv_val );
420                         si->si_ctxcsn = maxcsn;
421                         si->si_gotcsn = 1;
422                 }
423                 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
424
425                 if ( si->si_ops ) {
426                         switch(op->o_tag) {
427                         case LDAP_REQ_ADD:
428                         case LDAP_REQ_MODIFY:
429                         case LDAP_REQ_MODRDN:
430                         case LDAP_REQ_EXTENDED:
431                                 syncprov_matchops( op, opc, 0 );
432                                 break;
433                         case LDAP_REQ_DELETE:
434                                 /* for each match in opc->smatches:
435                                  *   send DELETE msg
436                                  */
437                                 for ( sm = opc->smatches; sm; sm=sm->sm_next ) {
438                                 }
439                                 break;
440                         }
441                 }
442
443         }
444         return SLAP_CB_CONTINUE;
445 }
446
447 #if 0
448 static int
449 syncprov_op_compare( Operation *op, SlapReply *rs )
450 {
451         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
452         syncprov_info_t         *si = on->on_bi.bi_private;
453         int rc = SLAP_CB_CONTINUE;
454
455         if ( dn_match( &op->o_req_ndn, &si->si_e->e_nname ) )
456         {
457                 Attribute *a;
458
459                 ldap_pvt_thread_mutex_lock( &si->si_e_mutex );
460
461                 if ( get_assert( op ) &&
462                         ( test_filter( op, si->si_e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
463                 {
464                         rs->sr_err = LDAP_ASSERTION_FAILED;
465                         goto return_results;
466                 }
467
468                 rs->sr_err = access_allowed( op, si->si_e, op->oq_compare.rs_ava->aa_desc,
469                         &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
470                 if ( ! rs->sr_err ) {
471                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
472                         goto return_results;
473                 }
474
475                 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
476
477                 for ( a = attr_find( si->si_e->e_attrs, op->oq_compare.rs_ava->aa_desc );
478                         a != NULL;
479                         a = attr_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
480                 {
481                         rs->sr_err = LDAP_COMPARE_FALSE;
482
483                         if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
484                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
485                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
486                                 a->a_nvals, &op->oq_compare.rs_ava->aa_value, op->o_tmpmemctx ) == 0 )
487                         {
488                                 rs->sr_err = LDAP_COMPARE_TRUE;
489                                 break;
490                         }
491                 }
492
493 return_results:;
494
495                 ldap_pvt_thread_mutex_unlock( &si->si_e_mutex );
496
497                 send_ldap_result( op, rs );
498
499                 if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
500                         rs->sr_err = LDAP_SUCCESS;
501                 }
502                 rc = rs->sr_err;
503         }
504
505         return SLAP_CB_CONTINUE;
506 }
507 #endif
508         
509 static int
510 syncprov_op_mod( Operation *op, SlapReply *rs )
511 {
512         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
513         syncprov_info_t         *si = on->on_bi.bi_private;
514
515         slap_callback *cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(opcookie), op->o_tmpmemctx);
516         opcookie *opc = (opcookie *)(cb+1);
517         opc->son = on;
518         cb->sc_response = syncprov_op_response;
519         cb->sc_cleanup = syncprov_op_cleanup;
520         cb->sc_private = opc;
521         cb->sc_next = op->o_callback;
522         op->o_callback = cb;
523
524         if ( si->si_ops && op->o_tag != LDAP_REQ_ADD )
525                 syncprov_matchops( op, opc, 1 );
526
527         return SLAP_CB_CONTINUE;
528 }
529
530 static int
531 syncprov_op_extended( Operation *op, SlapReply *rs )
532 {
533         if ( exop_is_write( op ))
534                 return syncprov_op_mod( op, rs );
535
536         return SLAP_CB_CONTINUE;
537 }
538
539 static int
540 syncprov_search_cleanup( Operation *op, SlapReply *rs )
541 {
542         if ( rs->sr_ctrls ) {
543                 free( rs->sr_ctrls[0] );
544                 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
545         }
546         return 0;
547 }
548
549 static int
550 syncprov_search_response( Operation *op, SlapReply *rs )
551 {
552         slap_callback *cb = op->o_callback;
553         slap_overinst *on = cb->sc_private;
554         syncprov_info_t         *si = on->on_bi.bi_private;
555
556         if ( rs->sr_type == REP_SEARCH ) {
557                 int i;
558                 if ( op->o_sync_state.ctxcsn ) {
559                         Attribute *a = attr_find( rs->sr_entry->e_attrs,
560                                 slap_schema.si_ad_entryCSN );
561                         /* Don't send the ctx entry twice */
562                         if ( bvmatch( &a->a_nvals[0], op->o_sync_state.ctxcsn ))
563                                 return LDAP_SUCCESS;
564                 }
565                 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
566                         op->o_tmpmemctx );
567                 rs->sr_ctrls[1] = NULL;
568                 rs->sr_err = slap_build_sync_state_ctrl( op, rs, rs->sr_entry,
569                         LDAP_SYNC_ADD, rs->sr_ctrls, 0, 0, NULL );
570         } else if (rs->sr_type == REP_RESULT ) {
571                 struct berval cookie;
572                 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
573                         op->o_tmpmemctx );
574                 rs->sr_ctrls[1] = NULL;
575                 slap_compose_sync_cookie( op, &cookie,
576                         &op->ors_filter->f_and->f_ava->aa_value,
577                         op->o_sync_state.sid, op->o_sync_state.rid );
578                 rs->sr_err = slap_build_sync_done_ctrl( op, rs, rs->sr_ctrls,
579                         0, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
580         }
581
582         return SLAP_CB_CONTINUE;
583 }
584
585 static int
586 syncprov_op_search( Operation *op, SlapReply *rs )
587 {
588         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
589         syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
590         slap_callback   *cb;
591         int gotstate = 0;
592         Filter *fand, *fava;
593
594         if ( !op->o_sync_mode ) return SLAP_CB_CONTINUE;
595
596         if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
597                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "illegal value for derefAliases" );
598                 return rs->sr_err;
599         }
600
601         /* If we have a cookie, handle the PRESENT lookups
602          */
603         if ( op->o_sync_state.ctxcsn ) {
604                 /* Is the CSN in a valid format? */
605                 if ( op->o_sync_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
606                         send_ldap_error( op, rs, LDAP_OTHER, "invalid sync cookie" );
607                         return rs->sr_err;
608                 }
609                 /* Is the CSN still present in the database? */
610                 if ( syncprov_findcsn( op, FIND_CSN ) != LDAP_SUCCESS ) {
611                         /* No, so a reload is required */
612 #if 0           /* the consumer doesn't seem to send this hint */
613                         if ( op->o_sync_rhint == 0 ) {
614                                 send_ldap_error( op, rs, LDAP_SYNC_REFRESH_REQUIRED, "sync cookie is stale" );
615                                 return rs->sr_err;
616                         }
617 #endif
618                 } else {
619                         /* Does it match the current ctxCSN? */
620                         if ( bvmatch( op->o_sync_state.ctxcsn, &si->si_ctxcsn )) {
621                                 LDAPControl     *ctrls[2];
622
623                                 ctrls[0] = NULL;
624                                 ctrls[1] = NULL;
625                                 slap_build_sync_done_ctrl( op, rs, ctrls, 0, 0,
626                                         NULL, LDAP_SYNC_REFRESH_DELETES );
627                                 rs->sr_err = LDAP_SUCCESS;
628                                 send_ldap_result( op, rs );
629                                 return rs->sr_err;
630                         }
631                         gotstate = 1;
632                         /* OK, let's send all the Present UUIDs */
633                         if ( syncprov_findcsn( op, FIND_PRESENT ) != LDAP_SUCCESS ) {
634                                 send_ldap_result( op, rs );
635                                 return rs->sr_err;
636                         }
637                 }
638         }
639
640         if ( !gotstate && !si->si_gotcsn ) {
641                 struct berval bv = BER_BVC("1"), *old;
642                 
643                 old = op->o_sync_state.ctxcsn;
644                 op->o_sync_state.ctxcsn = &bv;
645                 syncprov_findcsn( op, FIND_CSN );
646                 op->o_sync_state.ctxcsn = old;
647         }
648
649         /* Append CSN range to search filter */
650         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
651
652         fand = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
653         fand->f_choice = LDAP_FILTER_AND;
654         fand->f_next = NULL;
655         fava = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
656         fava->f_choice = LDAP_FILTER_LE;
657         fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
658         fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
659         ber_dupbv_x( &fava->f_ava->aa_value, &si->si_ctxcsn, op->o_tmpmemctx );
660         fand->f_and = fava;
661         if ( gotstate ) {
662                 fava->f_next = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
663                 fava = fava->f_next;
664                 fava->f_choice = LDAP_FILTER_GE;
665                 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
666                 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
667                 ber_dupbv_x( &fava->f_ava->aa_value, op->o_sync_state.ctxcsn, op->o_tmpmemctx );
668         }
669         fava->f_next = op->ors_filter;
670         op->ors_filter = fand;
671         filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
672
673         /* Let our callback add needed info to returned entries */
674         cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(opcookie), op->o_tmpmemctx);
675         cb->sc_response = syncprov_search_response;
676         cb->sc_cleanup = syncprov_search_cleanup;
677         cb->sc_private = on;
678         cb->sc_next = op->o_callback;
679         op->o_callback = cb;
680
681         return SLAP_CB_CONTINUE;
682 }
683
684 #if 0
685 static int
686 syncprov_response( Operation *op, SlapReply *rs )
687 {
688         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
689         syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
690
691         if ( rs->sr_err == LDAP_SUCCESS ) {
692                 if ( op->o_tag == LDAP_REQ_SEARCH ) {
693                         /* handle transition from refresh to persist */
694                         if ( op->o_sync_mode == SLAP_SYNC_REFRESH_AND_PERSIST ) {
695                         }
696
697                 /* If we're checkpointing */
698                 } else if ( si->si_chkops || si->si_chktime )) {
699                         int do_check = 0;
700
701                         switch ( op->o_tag ) {
702                         case LDAP_REQ_EXTENDED:
703                                 { int i, doit = 0;
704
705                                 /* if not PASSWD_MODIFY, break */
706                                 for ( i=0; write_exop[i]; i++ )
707                                 {
708                                         if ( !ber_bvcmp( write_exop[i], &op->oq_extended.rs_reqoid ))
709                                         {
710                                                 doit = 1;
711                                                 break;
712                                         }
713                                 }
714                                 if ( !doit ) break;
715                                 }
716                                 /* else fallthru */
717                         case LDAP_REQ_ADD:
718                         case LDAP_REQ_MODIFY:
719                         case LDAP_REQ_MODRDN:
720                         case LDAP_REQ_DELETE:
721                                 ldap_pvt_thread_mutex_lock( &si->si_chk_mutex );
722                                 if ( si->si_chkops )
723                                 {
724                                         si->si_numops++;
725                                         if ( si->si_numops >= si->si_chkops )
726                                         {
727                                                 do_check = 1;
728                                                 si->si_numops = 0;
729                                         }
730                                 }
731                                 if ( si->si_chktime )
732                                 {
733                                         if ( op->o_time - si->si_chklast >= si->si_chktime )
734                                         {
735                                                 do_check = 1;
736                                                 si->si_chklast = op->o_time;
737                                         }
738                                 }
739                                 ldap_pvt_thread_mutex_unlock( &si->si_chk_mutex );
740                                 if ( do_check )
741                                 {
742                                         /* write cn=ldapsync to underlying db */
743                                 }
744                                 break;
745                         }
746                 }
747         }
748         /* Release this DN */
749         if ( op->o_tag == LDAP_REQ_MODIFY ) {
750                 ldap_pvt_thread_mutex_lock( &si->si_mod_mutex );
751                 avl_delete( &si->si_mods, &op->o_req_ndn, dn_avl_cmp );
752                 ldap_pvt_thread_mutex_unlock( &si->si_mod_mutex );
753         }
754         return SLAP_CB_CONTINUE;
755 }
756 #endif
757
758 static int
759 syncprov_db_config(
760         BackendDB       *be,
761         const char      *fname,
762         int             lineno,
763         int             argc,
764         char    **argv
765 )
766 {
767         slap_overinst           *on = (slap_overinst *)be->bd_info;
768         syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
769
770 #if 0
771         if ( strcasecmp( argv[ 0 ], "syncprov-checkpoint" ) == 0 ) {
772                 if ( argc != 3 ) {
773                         fprintf( stderr, "%s: line %d: wrong number of arguments in "
774                                 "\"syncprov-checkpoint <ops> <minutes>\"\n", fname, lineno );
775                         return -1;
776                 }
777                 si->si_chkops = atoi( argv[1] );
778                 si->si_chktime = atoi( argv[2] ) * 60;
779
780         } else {
781                 return SLAP_CONF_UNKNOWN;
782         }
783 #endif
784
785         return SLAP_CONF_UNKNOWN;
786 }
787
788 static int
789 syncprov_db_init(
790         BackendDB *be
791 )
792 {
793         slap_overinst   *on = (slap_overinst *)be->bd_info;
794         syncprov_info_t *si;
795
796         si = ch_calloc(1, sizeof(syncprov_info_t));
797         on->on_bi.bi_private = si;
798         ldap_pvt_thread_mutex_init( &si->si_csn_mutex );
799         ldap_pvt_thread_mutex_init( &si->si_ops_mutex );
800
801         csn_anlist[0].an_desc = slap_schema.si_ad_entryCSN;
802         csn_anlist[0].an_name = slap_schema.si_ad_entryCSN->ad_cname;
803
804         uuid_anlist[0].an_desc = slap_schema.si_ad_entryUUID;
805         uuid_anlist[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
806
807         return 0;
808 }
809
810 static int
811 syncprov_db_destroy(
812         BackendDB *be
813 )
814 {
815         slap_overinst   *on = (slap_overinst *)be->bd_info;
816         syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
817
818         if ( si ) {
819                 ldap_pvt_thread_mutex_destroy( &si->si_ops_mutex );
820                 ldap_pvt_thread_mutex_destroy( &si->si_csn_mutex );
821                 ch_free( si );
822         }
823
824         return 0;
825 }
826
827 /* This overlay is set up for dynamic loading via moduleload. For static
828  * configuration, you'll need to arrange for the slap_overinst to be
829  * initialized and registered by some other function inside slapd.
830  */
831
832 static slap_overinst            syncprov;
833
834 int
835 syncprov_init()
836 {
837         syncprov.on_bi.bi_type = "syncprov";
838         syncprov.on_bi.bi_db_init = syncprov_db_init;
839         syncprov.on_bi.bi_db_config = syncprov_db_config;
840         syncprov.on_bi.bi_db_destroy = syncprov_db_destroy;
841
842         syncprov.on_bi.bi_op_add = syncprov_op_mod;
843 #if 0
844         syncprov.on_bi.bi_op_compare = syncprov_op_compare;
845 #endif
846         syncprov.on_bi.bi_op_delete = syncprov_op_mod;
847         syncprov.on_bi.bi_op_modify = syncprov_op_mod;
848         syncprov.on_bi.bi_op_modrdn = syncprov_op_mod;
849         syncprov.on_bi.bi_op_search = syncprov_op_search;
850         syncprov.on_bi.bi_extended = syncprov_op_extended;
851
852 #if 0
853         syncprov.on_response = syncprov_response;
854 #endif
855
856         return overlay_register( &syncprov );
857 }
858
859 #if SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC
860 int
861 init_module( int argc, char *argv[] )
862 {
863         return syncprov_init();
864 }
865 #endif /* SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC */
866
867 #endif /* defined(SLAPD_OVER_SYNCPROV) */