1 /* syncprov.c - syncrepl provider */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
16 * This work was initially developed by Howard Chu for inclusion in
22 #ifdef SLAPD_OVER_SYNCPROV
24 #include <ac/string.h>
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 */
35 int s_flags; /* search status */
40 #define PS_IS_REFRESHING 0x01
42 /* Record of which searches matched at premodify step */
43 typedef struct syncmatches {
44 struct syncmatches *sm_next;
48 typedef struct syncprov_info_t {
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];
57 typedef struct opcookie {
59 syncmatches *smatches;
60 struct berval sdn; /* DN of entry, for deletes */
62 struct berval suuid; /* UUID of entry */
63 struct berval sctxcsn;
64 int sreference; /* Is the entry a reference? */
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 */
74 static AttributeName csn_anlist[2];
75 static AttributeName uuid_anlist[2];
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 /
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.
87 findbase_cb( Operation *op, SlapReply *rs )
89 slap_callback *sc = op->o_callback;
91 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
92 fbase_cookie *fc = sc->sc_private;
94 /* If no entryID, we're looking for the first time.
95 * Just store whatever we got.
97 if ( fc->fss->s_eid == NOID ) {
99 fc->fss->s_eid = rs->sr_entry->e_id;
100 ber_dupbv( &fc->fss->s_base, &rs->sr_entry->e_nname );
102 } else if ( rs->sr_entry->e_id == fc->fss->s_eid &&
103 dn_match( &rs->sr_entry->e_nname, &fc->fss->s_base )) {
105 /* OK, the DN is the same and the entryID is the same. Now
106 * see if the fdn resides in the scope.
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 );
113 case LDAP_SCOPE_ONELEVEL: {
115 dnParent( fc->fdn, &pdn );
116 fc->fscope = dn_match( &pdn, &rs->sr_entry->e_nname );
118 case LDAP_SCOPE_SUBTREE:
119 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname );
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 );
134 syncprov_findbase( Operation *op, fbase_cookie *fc )
136 opcookie *opc = op->o_callback->sc_private;
137 slap_overinst *on = opc->son;
138 syncprov_info_t *si = on->on_bi.bi_private;
140 slap_callback cb = {0};
142 SlapReply frs = { REP_RESULT };
147 cb.sc_response = findbase_cb;
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;
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;
162 fop.o_req_ndn = fc->fss->s_op->o_req_ndn;
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;
168 if ( fc->fbase ) return LDAP_SUCCESS;
170 /* If entryID has changed, then the base of this search has
171 * changed. Invalidate the psearch.
173 return LDAP_NO_SUCH_OBJECT;
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,
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
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.
197 #define FIND_PRESENT 2
199 typedef struct fcsn_cookie {
200 struct berval maxcsn;
205 findcsn_cb( Operation *op, SlapReply *rs )
207 slap_callback *sc = op->o_callback;
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.
213 if ( sc->sc_private ) {
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 );
223 fc->maxcsn.bv_len = a->a_vals[0].bv_len;
224 strcpy(fc->maxcsn.bv_val, a->a_vals[0].bv_val );
227 /* Otherwise, if the private pointer is not set, we just
228 * want to know if any entry matched the filter.
230 sc->sc_private = (void *)1;
236 /* Build a list of entryUUIDs for sending in a SyncID set */
238 typedef struct fpres_cookie {
244 findpres_cb( Operation *op, SlapReply *rs )
246 slap_callback *sc = op->o_callback;
247 fpres_cookie *pc = sc->sc_private;
248 int ret = SLAP_CB_CONTINUE;
250 if ( rs->sr_type == REP_SEARCH ) {
251 ret = slap_build_syncUUID_set( op, &pc->uuids, rs->sr_entry );
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,
259 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
266 } else if ( rs->sr_type == REP_RESULT ) {
269 rs->sr_rspoid = LDAP_SYNC_INFO;
270 ret = slap_send_syncinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
272 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
282 syncprov_findcsn( Operation *op, int mode )
284 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
285 syncprov_info_t *si = on->on_bi.bi_private;
287 slap_callback cb = {0};
289 SlapReply frs = { REP_RESULT };
290 char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
291 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
294 AttributeAssertion eq;
297 fpres_cookie pcookie;
299 syncrepl_state *srs = op->o_controls[sync_cid];
301 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
306 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync_mode */
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 );
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 );
327 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
330 cf.f_choice = LDAP_FILTER_LE;
331 fop.ors_attrsonly = 1;
332 fop.ors_attrs = slap_anlist_no_attrs;
334 cb.sc_private = NULL;
335 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
337 cb.sc_response = findcsn_cb;
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;
349 pcookie.uuids = NULL;
350 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
353 cf.f_av_desc = slap_schema.si_ad_entryCSN;
354 cf.f_av_value = *srs->sr_state.ctxcsn;
357 fop.o_callback = &cb;
358 fop.ors_tlimit = SLAP_NO_LIMIT;
359 fop.ors_filter = &cf;
360 fop.ors_filterstr = fbuf;
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;
366 if ( mode == FIND_CSN ) {
367 if ( !si->si_gotcsn ) {
368 ber_dupbv( &si->si_ctxcsn, &fcookie.maxcsn );
370 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
371 if ( fcookie.gotmatch ) return LDAP_SUCCESS;
374 if ( cb.sc_private ) return LDAP_SUCCESS;
376 } else if ( mode == FIND_PRESENT ) {
380 /* If matching CSN was not found, invalidate the context. */
381 return LDAP_NO_SUCH_OBJECT;
385 syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry *e, int mode )
387 slap_overinst *on = opc->son;
388 syncprov_info_t *si = on->on_bi.bi_private;
390 SlapReply rs = { REP_SEARCH };
391 LDAPControl *ctrls[2];
392 struct berval cookie;
394 Attribute a_uuid = {0};
395 Operation sop = *so->s_op;
397 syncrepl_state *srs = sop.o_controls[sync_cid];
401 sop.o_tmpmemctx = op->o_tmpmemctx;
404 slap_compose_sync_cookie( op, &cookie, &opc->sctxcsn,
405 srs->sr_state.sid, srs->sr_state.rid );
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 );
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 );
424 case LDAP_SYNC_MODIFY:
425 rs.sr_attrs = sop.ors_attrs;
426 send_search_entry( &sop, &rs );
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 ) {
438 send_search_reference( &sop, &rs );
440 send_search_entry( &sop, &rs );
446 free( rs.sr_ctrls[0] );
451 syncprov_matchops( Operation *op, opcookie *opc, int saveit )
453 slap_overinst *on = opc->son;
454 syncprov_info_t *si = on->on_bi.bi_private;
463 fc.fdn = &op->o_req_ndn;
465 if ( op->o_tag == LDAP_REQ_MODRDN && !saveit ) {
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 );
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;
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 );
486 if ( saveit || op->o_tag == LDAP_REQ_ADD ) {
487 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
489 ber_dupbv_x( &opc->suuid, &a->a_nvals[0], op->o_tmpmemctx );
492 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
493 for (ss = si->si_ops; ss; ss=ss->s_next)
502 rc = syncprov_findbase( op, &fc );
503 if ( rc != LDAP_SUCCESS ) continue;
505 /* If we're sending results now, look for this op in old matches */
508 for ( sm=opc->smatches, old=(syncmatches *)&opc->smatches; sm;
509 old=sm, sm=sm->sm_next ) {
510 if ( sm->sm_op == ss ) {
512 old->sm_next = sm->sm_next;
513 op->o_tmpfree( sm, op->o_tmpmemctx );
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 ) {
523 sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
524 sm->sm_next = opc->smatches;
528 /* if found send UPDATE else send ADD */
529 syncprov_sendresp( op, opc, ss, e,
530 found ? LDAP_SYNC_MODIFY : LDAP_SYNC_ADD );
532 } else if ( !saveit && found ) {
534 syncprov_sendresp( op, opc, ss, NULL, LDAP_SYNC_DELETE );
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;
546 syncprov_op_cleanup( Operation *op, SlapReply *rs )
548 slap_callback *cb = op->o_callback;
549 opcookie *opc = cb->sc_private;
550 syncmatches *sm, *snext;
552 for (sm = opc->smatches; sm; sm=snext) {
554 op->o_tmpfree( sm, op->o_tmpmemctx );
556 op->o_callback = cb->sc_next;
557 op->o_tmpfree(cb, op->o_tmpmemctx);
561 syncprov_op_response( Operation *op, SlapReply *rs )
563 opcookie *opc = op->o_callback->sc_private;
564 slap_overinst *on = opc->son;
565 syncprov_info_t *si = on->on_bi.bi_private;
568 if ( rs->sr_err == LDAP_SUCCESS )
570 struct berval maxcsn;
571 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
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;
584 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
586 opc->sctxcsn.bv_len = maxcsn.bv_len;
587 opc->sctxcsn.bv_val = cbuf;
592 case LDAP_REQ_MODIFY:
593 case LDAP_REQ_MODRDN:
594 case LDAP_REQ_EXTENDED:
595 syncprov_matchops( op, opc, 0 );
597 case LDAP_REQ_DELETE:
598 /* for each match in opc->smatches:
601 for ( sm = opc->smatches; sm; sm=sm->sm_next ) {
602 syncprov_sendresp( op, opc, sm->sm_op, NULL,
610 return SLAP_CB_CONTINUE;
615 syncprov_op_compare( Operation *op, SlapReply *rs )
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;
621 if ( dn_match( &op->o_req_ndn, &si->si_e->e_nname ) )
625 ldap_pvt_thread_mutex_lock( &si->si_e_mutex );
627 if ( get_assert( op ) &&
628 ( test_filter( op, si->si_e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
630 rs->sr_err = LDAP_ASSERTION_FAILED;
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;
641 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
643 for ( a = attr_find( si->si_e->e_attrs, op->oq_compare.rs_ava->aa_desc );
645 a = attr_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
647 rs->sr_err = LDAP_COMPARE_FALSE;
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 )
654 rs->sr_err = LDAP_COMPARE_TRUE;
661 ldap_pvt_thread_mutex_unlock( &si->si_e_mutex );
663 send_ldap_result( op, rs );
665 if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
666 rs->sr_err = LDAP_SUCCESS;
671 return SLAP_CB_CONTINUE;
676 syncprov_op_mod( Operation *op, SlapReply *rs )
678 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
679 syncprov_info_t *si = on->on_bi.bi_private;
681 slap_callback *cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(opcookie), op->o_tmpmemctx);
682 opcookie *opc = (opcookie *)(cb+1);
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;
690 if ( si->si_ops && op->o_tag != LDAP_REQ_ADD )
691 syncprov_matchops( op, opc, 1 );
693 return SLAP_CB_CONTINUE;
697 syncprov_op_extended( Operation *op, SlapReply *rs )
699 if ( exop_is_write( op ))
700 return syncprov_op_mod( op, rs );
702 return SLAP_CB_CONTINUE;
705 typedef struct searchstate {
706 slap_overinst *ss_on;
712 syncprov_search_cleanup( Operation *op, SlapReply *rs )
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 );
720 op->o_sync_mode |= SLAP_SYNC_REFRESH_AND_PERSIST;
725 syncprov_search_response( Operation *op, SlapReply *rs )
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];
732 if ( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF ) {
734 struct berval cookie;
736 Attribute *a = attr_find( rs->sr_entry->e_attrs,
737 slap_schema.si_ad_entryCSN );
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 ))
744 slap_compose_sync_cookie( op, &cookie, a->a_nvals,
745 srs->sr_state.sid, srs->sr_state.rid );
747 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
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;
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 );
759 /* Is this a regular refresh? */
761 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
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 );
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 */
775 /* Turn off the refreshing flag */
776 ss->ss_so->s_flags ^= PS_IS_REFRESHING;
778 /* Detach this Op from frontend control */
786 return SLAP_CB_CONTINUE;
790 syncprov_op_search( Operation *op, SlapReply *rs )
792 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
793 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
795 int gotstate = 0, nochange = 0;
801 if ( !(op->o_sync_mode & SLAP_SYNC_REFRESH) ) return SLAP_CB_CONTINUE;
803 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
804 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "illegal value for derefAliases" );
808 srs = op->o_controls[sync_cid];
810 /* If this is a persistent search, set it up right away */
811 if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
821 so.s_flags = PS_IS_REFRESHING;
822 /* syncprov_findbase expects to be called as a callback... */
823 sc.sc_private = &opc;
826 op->o_callback = ≻
827 rs->sr_err = syncprov_findbase( op, &fc );
830 if ( rs->sr_err != LDAP_SUCCESS ) {
831 send_ldap_result( op, rs );
834 sop = ch_malloc( sizeof( syncops ));
836 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
837 sop->s_next = si->si_ops;
839 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
842 /* If we have a cookie, handle the PRESENT lookups
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" );
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" );
861 /* If just Refreshing and nothing has changed, shortcut it */
862 if ( bvmatch( srs->sr_state.ctxcsn, &si->si_ctxcsn )) {
864 if ( !(op->o_sync_mode & SLAP_SYNC_PERSIST) ) {
865 LDAPControl *ctrls[2];
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 );
878 /* If context has changed, check for Present UUIDs */
879 if ( syncprov_findcsn( op, FIND_PRESENT ) != LDAP_SUCCESS ) {
880 send_ldap_result( op, rs );
886 /* If we didn't get a cookie and we don't know our contextcsn, try to
889 if ( !gotstate && !si->si_gotcsn ) {
890 struct berval bv = BER_BVC("1"), *old;
892 old = srs->sr_state.ctxcsn;
893 srs->sr_state.ctxcsn = &bv;
894 syncprov_findcsn( op, FIND_CSN );
895 srs->sr_state.ctxcsn = old;
898 /* Append CSN range to search filter, save original filter
899 * for persistent search evaluation
902 sop->s_filter = op->ors_filter;
905 fand = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
906 fand->f_choice = LDAP_FILTER_AND;
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 );
915 fava->f_next = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
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 );
922 fava->f_next = op->ors_filter;
923 op->ors_filter = fand;
924 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
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);
933 cb->sc_response = syncprov_search_response;
934 cb->sc_cleanup = syncprov_search_cleanup;
936 cb->sc_next = op->o_callback;
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.
943 op->o_sync_mode &= SLAP_CONTROL_MASK;
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
950 rs->sr_err = LDAP_SUCCESS;
952 send_ldap_result( op, rs );
955 return SLAP_CB_CONTINUE;
967 slap_overinst *on = (slap_overinst *)be->bd_info;
968 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
971 if ( strcasecmp( argv[ 0 ], "syncprov-checkpoint" ) == 0 ) {
973 fprintf( stderr, "%s: line %d: wrong number of arguments in "
974 "\"syncprov-checkpoint <ops> <minutes>\"\n", fname, lineno );
977 si->si_chkops = atoi( argv[1] );
978 si->si_chktime = atoi( argv[2] ) * 60;
981 return SLAP_CONF_UNKNOWN;
985 return SLAP_CONF_UNKNOWN;
993 slap_overinst *on = (slap_overinst *)be->bd_info;
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;
1002 csn_anlist[0].an_desc = slap_schema.si_ad_entryCSN;
1003 csn_anlist[0].an_name = slap_schema.si_ad_entryCSN->ad_cname;
1005 uuid_anlist[0].an_desc = slap_schema.si_ad_entryUUID;
1006 uuid_anlist[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1008 sync_cid = slap_cids.sc_LDAPsync;
1014 syncprov_db_destroy(
1018 slap_overinst *on = (slap_overinst *)be->bd_info;
1019 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1022 ldap_pvt_thread_mutex_destroy( &si->si_ops_mutex );
1023 ldap_pvt_thread_mutex_destroy( &si->si_csn_mutex );
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.
1035 static slap_overinst syncprov;
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;
1045 syncprov.on_bi.bi_op_add = syncprov_op_mod;
1047 syncprov.on_bi.bi_op_compare = syncprov_op_compare;
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;
1056 syncprov.on_response = syncprov_response;
1059 return overlay_register( &syncprov );
1062 #if SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC
1064 init_module( int argc, char *argv[] )
1066 return syncprov_init();
1068 #endif /* SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC */
1070 #endif /* defined(SLAPD_OVER_SYNCPROV) */