]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/syncprov.c
More syncprov fixes. test019 now passes.
[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 #ifdef SLAPD_OVER_SYNCPROV
23
24 #include <ac/string.h>
25 #include "lutil.h"
26 #include "slap.h"
27
28 /* Record of a persistent search */
29 typedef struct syncops {
30         struct syncops *s_next;
31         struct berval   s_base;         /* ndn of search base */
32         ID              s_eid;          /* entryID of search base */
33         Operation       *s_op;          /* search op */
34         Filter  *s_filter;
35         int             s_flags;        /* search status */
36 } syncops;
37
38 static int      sync_cid;
39
40 #define PS_IS_REFRESHING        0x01
41
42 /* Record of which searches matched at premodify step */
43 typedef struct syncmatches {
44         struct syncmatches *sm_next;
45         syncops *sm_op;
46 } syncmatches;
47
48 typedef struct syncprov_info_t {
49         syncops         *si_ops;
50         struct berval   si_ctxcsn;      /* ldapsync context */
51         int             si_gotcsn;      /* is our ctxcsn up to date? */
52         ldap_pvt_thread_mutex_t si_csn_mutex;
53         ldap_pvt_thread_mutex_t si_ops_mutex;
54         char            si_ctxcsnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
55 } syncprov_info_t;
56
57 typedef struct opcookie {
58         slap_overinst *son;
59         syncmatches *smatches;
60         struct berval sdn;      /* DN of entry, for deletes */
61         struct berval sndn;
62         struct berval suuid;    /* UUID of entry */
63         struct berval sctxcsn;
64         int sreference; /* Is the entry a reference? */
65 } opcookie;
66
67 typedef struct fbase_cookie {
68         struct berval *fdn;     /* DN of a modified entry, for scope testing */
69         syncops *fss;   /* persistent search we're testing against */
70         int fbase;      /* if TRUE we found the search base and it's still valid */
71         int fscope;     /* if TRUE then fdn is within the psearch scope */
72 } fbase_cookie;
73
74 static AttributeName csn_anlist[2];
75 static AttributeName uuid_anlist[2];
76
77 /* syncprov_findbase:
78  *   finds the true DN of the base of a search (with alias dereferencing) and
79  * checks to make sure the base entry doesn't get replaced with a different
80  * entry (e.g., swapping trees via ModDN, or retargeting an alias). If a
81  * change is detected, any persistent search on this base must be terminated /
82  * reloaded.
83  *   On the first call, we just save the DN and entryID. On subsequent calls
84  * we compare the DN and entryID with the saved values.
85  */
86 static int
87 findbase_cb( Operation *op, SlapReply *rs )
88 {
89         slap_callback *sc = op->o_callback;
90
91         if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
92                 fbase_cookie *fc = sc->sc_private;
93
94                 /* If no entryID, we're looking for the first time.
95                  * Just store whatever we got.
96                  */
97                 if ( fc->fss->s_eid == NOID ) {
98                         fc->fbase = 1;
99                         fc->fss->s_eid = rs->sr_entry->e_id;
100                         ber_dupbv( &fc->fss->s_base, &rs->sr_entry->e_nname );
101
102                 } else if ( rs->sr_entry->e_id == fc->fss->s_eid &&
103                         dn_match( &rs->sr_entry->e_nname, &fc->fss->s_base )) {
104
105                 /* OK, the DN is the same and the entryID is the same. Now
106                  * see if the fdn resides in the scope.
107                  */
108                         fc->fbase = 1;
109                         switch ( fc->fss->s_op->ors_scope ) {
110                         case LDAP_SCOPE_BASE:
111                                 fc->fscope = dn_match( fc->fdn, &rs->sr_entry->e_nname );
112                                 break;
113                         case LDAP_SCOPE_ONELEVEL: {
114                                 struct berval pdn;
115                                 dnParent( fc->fdn, &pdn );
116                                 fc->fscope = dn_match( &pdn, &rs->sr_entry->e_nname );
117                                 break; }
118                         case LDAP_SCOPE_SUBTREE:
119                                 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname );
120                                 break;
121 #ifdef LDAP_SCOPE_SUBORDINATE
122                         case LDAP_SCOPE_SUBORDINATE:
123                                 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname ) &&
124                                         !dn_match( fc->fdn, &rs->sr_entry->e_nname );
125                                 break;
126 #endif
127                         }
128                 }
129         }
130         return LDAP_SUCCESS;
131 }
132
133 static int
134 syncprov_findbase( Operation *op, fbase_cookie *fc )
135 {
136         opcookie *opc = op->o_callback->sc_private;
137         slap_overinst *on = opc->son;
138         syncprov_info_t         *si = on->on_bi.bi_private;
139
140         slap_callback cb = {0};
141         Operation fop;
142         SlapReply frs = { REP_RESULT };
143         int rc;
144
145         fop = *op;
146
147         cb.sc_response = findbase_cb;
148         cb.sc_private = fc;
149
150         fop.o_sync_mode &= SLAP_CONTROL_MASK;   /* turn off sync mode */
151         fop.o_callback = &cb;
152         fop.o_tag = LDAP_REQ_SEARCH;
153         fop.ors_scope = LDAP_SCOPE_BASE;
154         fop.ors_deref = fc->fss->s_op->ors_deref;
155         fop.ors_slimit = 1;
156         fop.ors_tlimit = SLAP_NO_LIMIT;
157         fop.ors_attrs = slap_anlist_no_attrs;
158         fop.ors_attrsonly = 1;
159         fop.ors_filter = fc->fss->s_op->ors_filter;
160         fop.ors_filterstr = fc->fss->s_op->ors_filterstr;
161
162         fop.o_req_ndn = fc->fss->s_op->o_req_ndn;
163
164         fop.o_bd->bd_info = on->on_info->oi_orig;
165         rc = fop.o_bd->be_search( &fop, &frs );
166         fop.o_bd->bd_info = (BackendInfo *)on;
167
168         if ( fc->fbase ) return LDAP_SUCCESS;
169
170         /* If entryID has changed, then the base of this search has
171          * changed. Invalidate the psearch.
172          */
173         return LDAP_NO_SUCH_OBJECT;
174 }
175
176 /* syncprov_findcsn:
177  *   This function has three different purposes, but they all use a search
178  * that filters on entryCSN so they're combined here.
179  * 1: when the current contextCSN is unknown (i.e., at server start time)
180  * and a syncrepl search has arrived with a cookie, we search for all entries
181  * with CSN >= the cookie CSN, and store the maximum as our contextCSN. Also,
182  * we expect to find the cookie CSN in the search results, and note if we did
183  * or not. If not, we assume the cookie is stale. (This may be too restrictive,
184  * notice case 2.)
185  *
186  * 2: when the current contextCSN is known and we have a sync cookie, we search
187  * for one entry with CSN <= the cookie CSN. (Used to search for =.) If an
188  * entry is found, the cookie CSN is valid, otherwise it is stale. Case 1 is
189  * considered a special case of case 2, and both are generally called the
190  * "find CSN" task.
191  *
192  * 3: during a refresh phase, we search for all entries with CSN <= the cookie
193  * CSN, and generate Present records for them. We always collect this result
194  * in SyncID sets, even if there's only one match.
195  */
196 #define FIND_CSN        1
197 #define FIND_PRESENT    2
198
199 typedef struct fcsn_cookie {
200         struct berval maxcsn;
201         int gotmatch;
202 } fcsn_cookie;
203
204 static int
205 findcsn_cb( Operation *op, SlapReply *rs )
206 {
207         slap_callback *sc = op->o_callback;
208
209         if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
210                 /* If the private pointer is set, it points to an fcsn_cookie
211                  * and we want to record the maxcsn and match state.
212                  */
213                 if ( sc->sc_private ) {
214                         int i;
215                         fcsn_cookie *fc = sc->sc_private;
216                         syncrepl_state *srs = op->o_controls[sync_cid];
217                         Attribute *a = attr_find(rs->sr_entry->e_attrs,
218                                 slap_schema.si_ad_entryCSN );
219                         i = ber_bvcmp( &a->a_vals[0], srs->sr_state.ctxcsn );
220                         if ( i == 0 ) fc->gotmatch = 1;
221                         i = ber_bvcmp( &a->a_vals[0], &fc->maxcsn );
222                         if ( i > 0 ) {
223                                 fc->maxcsn.bv_len = a->a_vals[0].bv_len;
224                                 strcpy(fc->maxcsn.bv_val, a->a_vals[0].bv_val );
225                         }
226                 } else {
227                 /* Otherwise, if the private pointer is not set, we just
228                  * want to know if any entry matched the filter.
229                  */
230                         sc->sc_private = (void *)1;
231                 }
232         }
233         return LDAP_SUCCESS;
234 }
235
236 /* Build a list of entryUUIDs for sending in a SyncID set */
237
238 typedef struct fpres_cookie {
239         int num;
240         BerVarray uuids;
241 } fpres_cookie;
242
243 static int
244 findpres_cb( Operation *op, SlapReply *rs )
245 {
246         slap_callback *sc = op->o_callback;
247         fpres_cookie *pc = sc->sc_private;
248         int ret = SLAP_CB_CONTINUE;
249
250         if ( rs->sr_type == REP_SEARCH ) {
251                 ret = slap_build_syncUUID_set( op, &pc->uuids, rs->sr_entry );
252                 if ( ret > 0 ) {
253                         pc->num++;
254                         ret = LDAP_SUCCESS;
255                         if ( pc->num == SLAP_SYNCUUID_SET_SIZE ) {
256                                 rs->sr_rspoid = LDAP_SYNC_INFO;
257                                 ret = slap_send_syncinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
258                                         0, pc->uuids, 0 );
259                                 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
260                                 pc->uuids = NULL;
261                                 pc->num = 0;
262                         }
263                 } else {
264                         ret = LDAP_OTHER;
265                 }
266         } else if ( rs->sr_type == REP_RESULT ) {
267                 ret = rs->sr_err;
268                 if ( pc->num ) {
269                         rs->sr_rspoid = LDAP_SYNC_INFO;
270                         ret = slap_send_syncinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
271                                 0, pc->uuids, 0 );
272                         ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
273                         pc->uuids = NULL;
274                         pc->num = 0;
275                 }
276         }
277         return ret;
278 }
279
280
281 static int
282 syncprov_findcsn( Operation *op, int mode )
283 {
284         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
285         syncprov_info_t         *si = on->on_bi.bi_private;
286
287         slap_callback cb = {0};
288         Operation fop;
289         SlapReply frs = { REP_RESULT };
290         char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
291         char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
292         struct berval fbuf;
293         Filter cf;
294         AttributeAssertion eq;
295         int rc;
296         fcsn_cookie fcookie;
297         fpres_cookie pcookie;
298         int locked = 0;
299         syncrepl_state *srs = op->o_controls[sync_cid];
300
301         if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
302                 return LDAP_OTHER;
303         }
304
305         fop = *op;
306         fop.o_sync_mode &= SLAP_CONTROL_MASK;   /* turn off sync_mode */
307
308         fbuf.bv_val = buf;
309         if ( mode == FIND_CSN ) {
310                 if ( !si->si_gotcsn ) {
311                         /* If we don't know the current ctxcsn, find it */
312                         ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
313                         locked = 1;
314                 }
315                 if ( !si->si_gotcsn ) {
316                         cf.f_choice = LDAP_FILTER_GE;
317                         fop.ors_attrsonly = 0;
318                         fop.ors_attrs = csn_anlist;
319                         fop.ors_slimit = SLAP_NO_LIMIT;
320                         cb.sc_private = &fcookie;
321                         fcookie.maxcsn.bv_val = cbuf;
322                         fcookie.maxcsn.bv_len = 0;
323                         fcookie.gotmatch = 0;
324                         fbuf.bv_len = sprintf( buf, "(entryCSN>=%s)", srs->sr_state.ctxcsn->bv_val );
325                 } else {
326                         if ( locked ) {
327                                 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
328                                 locked = 0;
329                         }
330                         cf.f_choice = LDAP_FILTER_LE;
331                         fop.ors_attrsonly = 1;
332                         fop.ors_attrs = slap_anlist_no_attrs;
333                         fop.ors_slimit = 1;
334                         cb.sc_private = NULL;
335                         fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
336                 }
337                 cb.sc_response = findcsn_cb;
338
339         } else if ( mode == FIND_PRESENT ) {
340                 cf.f_choice = LDAP_FILTER_LE;
341                 fop.ors_attrsonly = 0;
342                 fop.ors_attrs = uuid_anlist;
343                 fop.ors_slimit = SLAP_NO_LIMIT;
344                 /* We want pure entries, not referrals */
345                 fop.o_managedsait = SLAP_CONTROL_CRITICAL;
346                 cb.sc_private = &pcookie;
347                 cb.sc_response = findpres_cb;
348                 pcookie.num = 0;
349                 pcookie.uuids = NULL;
350                 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
351         }
352         cf.f_ava = &eq;
353         cf.f_av_desc = slap_schema.si_ad_entryCSN;
354         cf.f_av_value = *srs->sr_state.ctxcsn;
355         cf.f_next = NULL;
356
357         fop.o_callback = &cb;
358         fop.ors_tlimit = SLAP_NO_LIMIT;
359         fop.ors_filter = &cf;
360         fop.ors_filterstr = fbuf;
361
362         fop.o_bd->bd_info = on->on_info->oi_orig;
363         rc = fop.o_bd->be_search( &fop, &frs );
364         fop.o_bd->bd_info = (BackendInfo *)on;
365
366         if ( mode == FIND_CSN ) {
367                 if ( !si->si_gotcsn ) {
368                         ber_dupbv( &si->si_ctxcsn, &fcookie.maxcsn );
369                         si->si_gotcsn = 1;
370                         ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
371                         if ( fcookie.gotmatch ) return LDAP_SUCCESS;
372                         
373                 } else {
374                         if ( cb.sc_private ) return LDAP_SUCCESS;
375                 }
376         } else if ( mode == FIND_PRESENT ) {
377                 return LDAP_SUCCESS;
378         }
379
380         /* If matching CSN was not found, invalidate the context. */
381         return LDAP_NO_SUCH_OBJECT;
382 }
383
384 static int
385 syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry *e, int mode )
386 {
387         slap_overinst *on = opc->son;
388         syncprov_info_t *si = on->on_bi.bi_private;
389
390         SlapReply rs = { REP_SEARCH };
391         LDAPControl *ctrls[2];
392         struct berval cookie;
393         Entry e_uuid = {0};
394         Attribute a_uuid = {0};
395         Operation sop = *so->s_op;
396         Opheader ohdr;
397         syncrepl_state *srs = sop.o_controls[sync_cid];
398
399         ohdr = *sop.o_hdr;
400         sop.o_hdr = &ohdr;
401         sop.o_tmpmemctx = op->o_tmpmemctx;
402
403         ctrls[1] = NULL;
404         slap_compose_sync_cookie( op, &cookie, &opc->sctxcsn,
405                 srs->sr_state.sid, srs->sr_state.rid );
406
407         e_uuid.e_attrs = &a_uuid;
408         a_uuid.a_desc = slap_schema.si_ad_entryUUID;
409         a_uuid.a_nvals = &opc->suuid;
410         rs.sr_err = slap_build_sync_state_ctrl( &sop, &rs, &e_uuid,
411                 mode, ctrls, 0, 1, &cookie );
412
413         rs.sr_entry = e;
414         rs.sr_ctrls = ctrls;
415         switch( mode ) {
416         case LDAP_SYNC_ADD:
417                 if ( opc->sreference ) {
418                         rs.sr_ref = get_entry_referrals( &sop, e );
419                         send_search_reference( &sop, &rs );
420                         ber_bvarray_free( rs.sr_ref );
421                         break;
422                 }
423                 /* fallthru */
424         case LDAP_SYNC_MODIFY:
425                 rs.sr_attrs = sop.ors_attrs;
426                 send_search_entry( &sop, &rs );
427                 break;
428         case LDAP_SYNC_DELETE:
429                 e_uuid.e_attrs = NULL;
430                 e_uuid.e_name = opc->sdn;
431                 e_uuid.e_nname = opc->sndn;
432                 rs.sr_entry = &e_uuid;
433                 if ( opc->sreference ) {
434                         struct berval bv;
435                         bv.bv_val = NULL;
436                         bv.bv_len = 0;
437                         rs.sr_ref = &bv;
438                         send_search_reference( &sop, &rs );
439                 } else {
440                         send_search_entry( &sop, &rs );
441                 }
442                 break;
443         default:
444                 assert(0);
445         }
446         free( rs.sr_ctrls[0] );
447         return rs.sr_err;
448 }
449
450 static void
451 syncprov_matchops( Operation *op, opcookie *opc, int saveit )
452 {
453         slap_overinst *on = opc->son;
454         syncprov_info_t         *si = on->on_bi.bi_private;
455
456         fbase_cookie fc;
457         syncops *ss;
458         Entry *e;
459         Attribute *a;
460         int rc;
461         struct berval newdn;
462
463         fc.fdn = &op->o_req_ndn;
464         /* compute new DN */
465         if ( op->o_tag == LDAP_REQ_MODRDN && !saveit ) {
466                 struct berval pdn;
467                 if ( op->orr_nnewSup ) pdn = *op->orr_nnewSup;
468                 else dnParent( fc.fdn, &pdn );
469                 build_new_dn( &newdn, &pdn, &op->orr_nnewrdn, op->o_tmpmemctx );
470                 fc.fdn = &newdn;
471         }
472         if ( op->o_tag != LDAP_REQ_ADD ) {
473                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
474                 rc = be_entry_get_rw( op, fc.fdn, NULL, NULL, 0, &e );
475                 op->o_bd->bd_info = (BackendInfo *)on;
476                 if ( rc ) return;
477         } else {
478                 e = op->ora_e;
479         }
480
481         if ( saveit ) {
482                 ber_dupbv_x( &opc->sdn, &e->e_name, op->o_tmpmemctx );
483                 ber_dupbv_x( &opc->sndn, &e->e_nname, op->o_tmpmemctx );
484                 opc->sreference = is_entry_referral( e );
485         }
486         if ( saveit || op->o_tag == LDAP_REQ_ADD ) {
487                 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
488                 if ( a )
489                         ber_dupbv_x( &opc->suuid, &a->a_nvals[0], op->o_tmpmemctx );
490         }
491
492         ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
493         for (ss = si->si_ops; ss; ss=ss->s_next)
494         {
495                 syncmatches *sm;
496                 int found = 0;
497
498                 /* validate base */
499                 fc.fss = ss;
500                 fc.fbase = 0;
501                 fc.fscope = 0;
502                 rc = syncprov_findbase( op, &fc );
503                 if ( rc != LDAP_SUCCESS ) continue;
504
505                 /* If we're sending results now, look for this op in old matches */
506                 if ( !saveit ) {
507                         syncmatches *old;
508                         for ( sm=opc->smatches, old=(syncmatches *)&opc->smatches; sm;
509                                 old=sm, sm=sm->sm_next ) {
510                                 if ( sm->sm_op == ss ) {
511                                         found = 1;
512                                         old->sm_next = sm->sm_next;
513                                         op->o_tmpfree( sm, op->o_tmpmemctx );
514                                         break;
515                                 }
516                         }
517                 }
518
519                 /* check if current o_req_dn is in scope and matches filter */
520                 if ( fc.fscope && test_filter( op, e, ss->s_filter ) ==
521                         LDAP_COMPARE_TRUE ) {
522                         if ( saveit ) {
523                                 sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
524                                 sm->sm_next = opc->smatches;
525                                 sm->sm_op = ss;
526                                 opc->smatches = sm;
527                         } else {
528                                 /* if found send UPDATE else send ADD */
529                                 syncprov_sendresp( op, opc, ss, e,
530                                         found ?  LDAP_SYNC_MODIFY : LDAP_SYNC_ADD );
531                         }
532                 } else if ( !saveit && found ) {
533                         /* send DELETE */
534                         syncprov_sendresp( op, opc, ss, NULL, LDAP_SYNC_DELETE );
535                 }
536         }
537         ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
538         if ( op->o_tag != LDAP_REQ_ADD ) {
539                 op->o_bd->bd_info = (BackendInfo *)on->on_info;
540                 be_entry_release_r( op, e );
541                 op->o_bd->bd_info = (BackendInfo *)on;
542         }
543 }
544
545 static int
546 syncprov_op_cleanup( Operation *op, SlapReply *rs )
547 {
548         slap_callback *cb = op->o_callback;
549         opcookie *opc = cb->sc_private;
550         syncmatches *sm, *snext;
551
552         for (sm = opc->smatches; sm; sm=snext) {
553                 snext = sm->sm_next;
554                 op->o_tmpfree( sm, op->o_tmpmemctx );
555         }
556         op->o_callback = cb->sc_next;
557         op->o_tmpfree(cb, op->o_tmpmemctx);
558 }
559
560 static int
561 syncprov_op_response( Operation *op, SlapReply *rs )
562 {
563         opcookie *opc = op->o_callback->sc_private;
564         slap_overinst *on = opc->son;
565         syncprov_info_t         *si = on->on_bi.bi_private;
566         syncmatches *sm;
567
568         if ( rs->sr_err == LDAP_SUCCESS )
569         {
570                 struct berval maxcsn;
571                 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
572
573                 cbuf[0] = '\0';
574                 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
575                 slap_get_commit_csn( op, &maxcsn );
576                 if ( maxcsn.bv_val ) {
577                         strcpy( cbuf, maxcsn.bv_val );
578                         if ( ber_bvcmp( &maxcsn, &si->si_ctxcsn ) > 0 ) {
579                                 strcpy( si->si_ctxcsnbuf, cbuf );
580                                 si->si_ctxcsn.bv_len = maxcsn.bv_len;
581                         }
582                         si->si_gotcsn = 1;
583                 }
584                 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
585
586                 opc->sctxcsn.bv_len = maxcsn.bv_len;
587                 opc->sctxcsn.bv_val = cbuf;
588
589                 if ( si->si_ops ) {
590                         switch(op->o_tag) {
591                         case LDAP_REQ_ADD:
592                         case LDAP_REQ_MODIFY:
593                         case LDAP_REQ_MODRDN:
594                         case LDAP_REQ_EXTENDED:
595                                 syncprov_matchops( op, opc, 0 );
596                                 break;
597                         case LDAP_REQ_DELETE:
598                                 /* for each match in opc->smatches:
599                                  *   send DELETE msg
600                                  */
601                                 for ( sm = opc->smatches; sm; sm=sm->sm_next ) {
602                                         syncprov_sendresp( op, opc, sm->sm_op, NULL,
603                                                 LDAP_SYNC_DELETE );
604                                 }
605                                 break;
606                         }
607                 }
608
609         }
610         return SLAP_CB_CONTINUE;
611 }
612
613 #if 0
614 static int
615 syncprov_op_compare( Operation *op, SlapReply *rs )
616 {
617         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
618         syncprov_info_t         *si = on->on_bi.bi_private;
619         int rc = SLAP_CB_CONTINUE;
620
621         if ( dn_match( &op->o_req_ndn, &si->si_e->e_nname ) )
622         {
623                 Attribute *a;
624
625                 ldap_pvt_thread_mutex_lock( &si->si_e_mutex );
626
627                 if ( get_assert( op ) &&
628                         ( test_filter( op, si->si_e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
629                 {
630                         rs->sr_err = LDAP_ASSERTION_FAILED;
631                         goto return_results;
632                 }
633
634                 rs->sr_err = access_allowed( op, si->si_e, op->oq_compare.rs_ava->aa_desc,
635                         &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
636                 if ( ! rs->sr_err ) {
637                         rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
638                         goto return_results;
639                 }
640
641                 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
642
643                 for ( a = attr_find( si->si_e->e_attrs, op->oq_compare.rs_ava->aa_desc );
644                         a != NULL;
645                         a = attr_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
646                 {
647                         rs->sr_err = LDAP_COMPARE_FALSE;
648
649                         if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
650                                 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
651                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
652                                 a->a_nvals, &op->oq_compare.rs_ava->aa_value, op->o_tmpmemctx ) == 0 )
653                         {
654                                 rs->sr_err = LDAP_COMPARE_TRUE;
655                                 break;
656                         }
657                 }
658
659 return_results:;
660
661                 ldap_pvt_thread_mutex_unlock( &si->si_e_mutex );
662
663                 send_ldap_result( op, rs );
664
665                 if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
666                         rs->sr_err = LDAP_SUCCESS;
667                 }
668                 rc = rs->sr_err;
669         }
670
671         return SLAP_CB_CONTINUE;
672 }
673 #endif
674         
675 static int
676 syncprov_op_mod( Operation *op, SlapReply *rs )
677 {
678         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
679         syncprov_info_t         *si = on->on_bi.bi_private;
680
681         slap_callback *cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(opcookie), op->o_tmpmemctx);
682         opcookie *opc = (opcookie *)(cb+1);
683         opc->son = on;
684         cb->sc_response = syncprov_op_response;
685         cb->sc_cleanup = syncprov_op_cleanup;
686         cb->sc_private = opc;
687         cb->sc_next = op->o_callback;
688         op->o_callback = cb;
689
690         if ( si->si_ops && op->o_tag != LDAP_REQ_ADD )
691                 syncprov_matchops( op, opc, 1 );
692
693         return SLAP_CB_CONTINUE;
694 }
695
696 static int
697 syncprov_op_extended( Operation *op, SlapReply *rs )
698 {
699         if ( exop_is_write( op ))
700                 return syncprov_op_mod( op, rs );
701
702         return SLAP_CB_CONTINUE;
703 }
704
705 typedef struct searchstate {
706         slap_overinst *ss_on;
707         syncops *ss_so;
708         int ss_done;
709 } searchstate;
710
711 static int
712 syncprov_search_cleanup( Operation *op, SlapReply *rs )
713 {
714         searchstate *ss = op->o_callback->sc_private;
715         if ( rs->sr_ctrls ) {
716                 free( rs->sr_ctrls[0] );
717                 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
718         }
719         if ( ss->ss_done )
720                 op->o_sync_mode |= SLAP_SYNC_REFRESH_AND_PERSIST;
721         return 0;
722 }
723
724 static int
725 syncprov_search_response( Operation *op, SlapReply *rs )
726 {
727         searchstate *ss = op->o_callback->sc_private;
728         slap_overinst *on = ss->ss_on;
729         syncprov_info_t         *si = on->on_bi.bi_private;
730         syncrepl_state *srs = op->o_controls[sync_cid];
731
732         if ( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF ) {
733                 int i;
734                 struct berval cookie;
735
736                 Attribute *a = attr_find( rs->sr_entry->e_attrs,
737                         slap_schema.si_ad_entryCSN );
738
739                 if ( srs->sr_state.ctxcsn ) {
740                         /* Don't send the ctx entry twice */
741                         if ( bvmatch( &a->a_nvals[0], srs->sr_state.ctxcsn ))
742                                 return LDAP_SUCCESS;
743                 }
744                 slap_compose_sync_cookie( op, &cookie, a->a_nvals,
745                         srs->sr_state.sid, srs->sr_state.rid );
746
747                 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
748                         op->o_tmpmemctx );
749                 rs->sr_ctrls[1] = NULL;
750                 rs->sr_err = slap_build_sync_state_ctrl( op, rs, rs->sr_entry,
751                         LDAP_SYNC_ADD, rs->sr_ctrls, 0, 1, &cookie );
752         } else if ( rs->sr_type == REP_RESULT && rs->sr_err == LDAP_SUCCESS ) {
753                 struct berval cookie;
754
755                 slap_compose_sync_cookie( op, &cookie,
756                         &op->ors_filter->f_and->f_ava->aa_value,
757                         srs->sr_state.sid, srs->sr_state.rid );
758
759                 /* Is this a regular refresh? */
760                 if ( !ss->ss_so ) {
761                         rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
762                                 op->o_tmpmemctx );
763                         rs->sr_ctrls[1] = NULL;
764                         rs->sr_err = slap_build_sync_done_ctrl( op, rs, rs->sr_ctrls,
765                                 0, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
766                 } else {
767                 /* It's RefreshAndPersist, transition to Persist phase */
768                         rs->sr_rspoid = LDAP_SYNC_INFO;
769                         slap_send_syncinfo( op, rs, rs->sr_nentries ?
770                                 LDAP_TAG_SYNC_REFRESH_PRESENT : LDAP_TAG_SYNC_REFRESH_DELETE,
771                                 &cookie, 1, NULL, 0 );
772                         /* Flush any queued persist messages */
773                                 ;
774
775                         /* Turn off the refreshing flag */
776                                 ss->ss_so->s_flags ^= PS_IS_REFRESHING;
777
778                         /* Detach this Op from frontend control */
779                                 ss->ss_done = 1;
780                                 ;
781
782                         return LDAP_SUCCESS;
783                 }
784         }
785
786         return SLAP_CB_CONTINUE;
787 }
788
789 static int
790 syncprov_op_search( Operation *op, SlapReply *rs )
791 {
792         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
793         syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
794         slap_callback   *cb;
795         int gotstate = 0, nochange = 0;
796         Filter *fand, *fava;
797         syncops *sop = NULL;
798         searchstate *ss;
799         syncrepl_state *srs;
800
801         if ( !(op->o_sync_mode & SLAP_SYNC_REFRESH) ) return SLAP_CB_CONTINUE;
802
803         if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
804                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "illegal value for derefAliases" );
805                 return rs->sr_err;
806         }
807
808         srs = op->o_controls[sync_cid];
809
810         /* If this is a persistent search, set it up right away */
811         if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
812                 syncops so;
813                 fbase_cookie fc;
814                 opcookie opc;
815                 slap_callback sc;
816
817                 fc.fss = &so;
818                 fc.fbase = 0;
819                 so.s_eid = NOID;
820                 so.s_op = op;
821                 so.s_flags = PS_IS_REFRESHING;
822                 /* syncprov_findbase expects to be called as a callback... */
823                 sc.sc_private = &opc;
824                 opc.son = on;
825                 cb = op->o_callback;
826                 op->o_callback = &sc;
827                 rs->sr_err = syncprov_findbase( op, &fc );
828                 op->o_callback = cb;
829
830                 if ( rs->sr_err != LDAP_SUCCESS ) {
831                         send_ldap_result( op, rs );
832                         return rs->sr_err;
833                 }
834                 sop = ch_malloc( sizeof( syncops ));
835                 *sop = so;
836                 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
837                 sop->s_next = si->si_ops;
838                 si->si_ops = sop;
839                 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
840         }
841
842         /* If we have a cookie, handle the PRESENT lookups
843          */
844         if ( srs->sr_state.ctxcsn ) {
845                 /* Is the CSN in a valid format? */
846                 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
847                         send_ldap_error( op, rs, LDAP_OTHER, "invalid sync cookie" );
848                         return rs->sr_err;
849                 }
850                 /* Is the CSN still present in the database? */
851                 if ( syncprov_findcsn( op, FIND_CSN ) != LDAP_SUCCESS ) {
852                         /* No, so a reload is required */
853 #if 0           /* the consumer doesn't seem to send this hint */
854                         if ( op->o_sync_rhint == 0 ) {
855                                 send_ldap_error( op, rs, LDAP_SYNC_REFRESH_REQUIRED, "sync cookie is stale" );
856                                 return rs->sr_err;
857                         }
858 #endif
859                 } else {
860                         gotstate = 1;
861                         /* If just Refreshing and nothing has changed, shortcut it */
862                         if ( bvmatch( srs->sr_state.ctxcsn, &si->si_ctxcsn )) {
863                                 nochange = 1;
864                                 if ( !(op->o_sync_mode & SLAP_SYNC_PERSIST) ) {
865                                         LDAPControl     *ctrls[2];
866
867                                         ctrls[0] = NULL;
868                                         ctrls[1] = NULL;
869                                         slap_build_sync_done_ctrl( op, rs, ctrls, 0, 0,
870                                                 NULL, LDAP_SYNC_REFRESH_DELETES );
871                                         rs->sr_ctrls = ctrls;
872                                         rs->sr_err = LDAP_SUCCESS;
873                                         send_ldap_result( op, rs );
874                                         return rs->sr_err;
875                                 }
876                                 goto shortcut;
877                         } else 
878                         /* If context has changed, check for Present UUIDs */
879                         if ( syncprov_findcsn( op, FIND_PRESENT ) != LDAP_SUCCESS ) {
880                                 send_ldap_result( op, rs );
881                                 return rs->sr_err;
882                         }
883                 }
884         }
885
886         /* If we didn't get a cookie and we don't know our contextcsn, try to
887          * find it anyway.
888          */
889         if ( !gotstate && !si->si_gotcsn ) {
890                 struct berval bv = BER_BVC("1"), *old;
891                 
892                 old = srs->sr_state.ctxcsn;
893                 srs->sr_state.ctxcsn = &bv;
894                 syncprov_findcsn( op, FIND_CSN );
895                 srs->sr_state.ctxcsn = old;
896         }
897
898         /* Append CSN range to search filter, save original filter
899          * for persistent search evaluation
900          */
901         if ( sop ) {
902                 sop->s_filter = op->ors_filter;
903         }
904
905         fand = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
906         fand->f_choice = LDAP_FILTER_AND;
907         fand->f_next = NULL;
908         fava = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
909         fava->f_choice = LDAP_FILTER_LE;
910         fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
911         fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
912         ber_dupbv_x( &fava->f_ava->aa_value, &si->si_ctxcsn, op->o_tmpmemctx );
913         fand->f_and = fava;
914         if ( gotstate ) {
915                 fava->f_next = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
916                 fava = fava->f_next;
917                 fava->f_choice = LDAP_FILTER_GE;
918                 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
919                 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
920                 ber_dupbv_x( &fava->f_ava->aa_value, srs->sr_state.ctxcsn, op->o_tmpmemctx );
921         }
922         fava->f_next = op->ors_filter;
923         op->ors_filter = fand;
924         filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
925
926 shortcut:
927         /* Let our callback add needed info to returned entries */
928         cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(searchstate), op->o_tmpmemctx);
929         ss = (searchstate *)(cb+1);
930         ss->ss_on = on;
931         ss->ss_so = sop;
932         ss->ss_done = 0;
933         cb->sc_response = syncprov_search_response;
934         cb->sc_cleanup = syncprov_search_cleanup;
935         cb->sc_private = ss;
936         cb->sc_next = op->o_callback;
937         op->o_callback = cb;
938
939         /* FIXME: temporary hack to make sure back-bdb's native Psearch handling
940          * doesn't get invoked. We can skip this after the back-bdb code is
941          * removed, and also delete ss->ss_done.
942          */
943         op->o_sync_mode &= SLAP_CONTROL_MASK;
944
945         /* If this is a persistent search and no changes were reported during
946          * the refresh phase, just invoke the response callback to transition
947          * us into persist phase
948          */
949         if ( nochange ) {
950                 rs->sr_err = LDAP_SUCCESS;
951                 rs->sr_nentries = 0;
952                 send_ldap_result( op, rs );
953                 return rs->sr_err;
954         }
955         return SLAP_CB_CONTINUE;
956 }
957
958 static int
959 syncprov_db_config(
960         BackendDB       *be,
961         const char      *fname,
962         int             lineno,
963         int             argc,
964         char    **argv
965 )
966 {
967         slap_overinst           *on = (slap_overinst *)be->bd_info;
968         syncprov_info_t         *si = (syncprov_info_t *)on->on_bi.bi_private;
969
970 #if 0
971         if ( strcasecmp( argv[ 0 ], "syncprov-checkpoint" ) == 0 ) {
972                 if ( argc != 3 ) {
973                         fprintf( stderr, "%s: line %d: wrong number of arguments in "
974                                 "\"syncprov-checkpoint <ops> <minutes>\"\n", fname, lineno );
975                         return -1;
976                 }
977                 si->si_chkops = atoi( argv[1] );
978                 si->si_chktime = atoi( argv[2] ) * 60;
979
980         } else {
981                 return SLAP_CONF_UNKNOWN;
982         }
983 #endif
984
985         return SLAP_CONF_UNKNOWN;
986 }
987
988 static int
989 syncprov_db_init(
990         BackendDB *be
991 )
992 {
993         slap_overinst   *on = (slap_overinst *)be->bd_info;
994         syncprov_info_t *si;
995
996         si = ch_calloc(1, sizeof(syncprov_info_t));
997         on->on_bi.bi_private = si;
998         ldap_pvt_thread_mutex_init( &si->si_csn_mutex );
999         ldap_pvt_thread_mutex_init( &si->si_ops_mutex );
1000         si->si_ctxcsn.bv_val = si->si_ctxcsnbuf;
1001
1002         csn_anlist[0].an_desc = slap_schema.si_ad_entryCSN;
1003         csn_anlist[0].an_name = slap_schema.si_ad_entryCSN->ad_cname;
1004
1005         uuid_anlist[0].an_desc = slap_schema.si_ad_entryUUID;
1006         uuid_anlist[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1007
1008         sync_cid = slap_cids.sc_LDAPsync;
1009
1010         return 0;
1011 }
1012
1013 static int
1014 syncprov_db_destroy(
1015         BackendDB *be
1016 )
1017 {
1018         slap_overinst   *on = (slap_overinst *)be->bd_info;
1019         syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1020
1021         if ( si ) {
1022                 ldap_pvt_thread_mutex_destroy( &si->si_ops_mutex );
1023                 ldap_pvt_thread_mutex_destroy( &si->si_csn_mutex );
1024                 ch_free( si );
1025         }
1026
1027         return 0;
1028 }
1029
1030 /* This overlay is set up for dynamic loading via moduleload. For static
1031  * configuration, you'll need to arrange for the slap_overinst to be
1032  * initialized and registered by some other function inside slapd.
1033  */
1034
1035 static slap_overinst            syncprov;
1036
1037 int
1038 syncprov_init()
1039 {
1040         syncprov.on_bi.bi_type = "syncprov";
1041         syncprov.on_bi.bi_db_init = syncprov_db_init;
1042         syncprov.on_bi.bi_db_config = syncprov_db_config;
1043         syncprov.on_bi.bi_db_destroy = syncprov_db_destroy;
1044
1045         syncprov.on_bi.bi_op_add = syncprov_op_mod;
1046 #if 0
1047         syncprov.on_bi.bi_op_compare = syncprov_op_compare;
1048 #endif
1049         syncprov.on_bi.bi_op_delete = syncprov_op_mod;
1050         syncprov.on_bi.bi_op_modify = syncprov_op_mod;
1051         syncprov.on_bi.bi_op_modrdn = syncprov_op_mod;
1052         syncprov.on_bi.bi_op_search = syncprov_op_search;
1053         syncprov.on_bi.bi_extended = syncprov_op_extended;
1054
1055 #if 0
1056         syncprov.on_response = syncprov_response;
1057 #endif
1058
1059         return overlay_register( &syncprov );
1060 }
1061
1062 #if SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC
1063 int
1064 init_module( int argc, char *argv[] )
1065 {
1066         return syncprov_init();
1067 }
1068 #endif /* SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC */
1069
1070 #endif /* defined(SLAPD_OVER_SYNCPROV) */