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 /* A queued result of a persistent search */
29 typedef struct syncres {
30 struct syncres *s_next;
36 /* Record of a persistent search */
37 typedef struct syncops {
38 struct syncops *s_next;
39 struct berval s_base; /* ndn of search base */
40 ID s_eid; /* entryID of search base */
41 Operation *s_op; /* search op */
43 int s_flags; /* search status */
44 struct syncres *s_res;
45 struct syncres *s_restail;
46 ldap_pvt_thread_mutex_t s_mutex;
51 #define PS_IS_REFRESHING 0x01
53 /* Record of which searches matched at premodify step */
54 typedef struct syncmatches {
55 struct syncmatches *sm_next;
59 typedef struct syncprov_info_t {
61 struct berval si_ctxcsn; /* ldapsync context */
62 int si_gotcsn; /* is our ctxcsn up to date? */
63 ldap_pvt_thread_mutex_t si_csn_mutex;
64 ldap_pvt_thread_mutex_t si_ops_mutex;
65 char si_ctxcsnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
68 typedef struct opcookie {
70 syncmatches *smatches;
71 struct berval sdn; /* DN of entry, for deletes */
73 struct berval suuid; /* UUID of entry */
74 struct berval sctxcsn;
75 int sreference; /* Is the entry a reference? */
78 typedef struct fbase_cookie {
79 struct berval *fdn; /* DN of a modified entry, for scope testing */
80 syncops *fss; /* persistent search we're testing against */
81 int fbase; /* if TRUE we found the search base and it's still valid */
82 int fscope; /* if TRUE then fdn is within the psearch scope */
85 static AttributeName csn_anlist[2];
86 static AttributeName uuid_anlist[2];
89 * finds the true DN of the base of a search (with alias dereferencing) and
90 * checks to make sure the base entry doesn't get replaced with a different
91 * entry (e.g., swapping trees via ModDN, or retargeting an alias). If a
92 * change is detected, any persistent search on this base must be terminated /
94 * On the first call, we just save the DN and entryID. On subsequent calls
95 * we compare the DN and entryID with the saved values.
98 findbase_cb( Operation *op, SlapReply *rs )
100 slap_callback *sc = op->o_callback;
102 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
103 fbase_cookie *fc = sc->sc_private;
105 /* If no entryID, we're looking for the first time.
106 * Just store whatever we got.
108 if ( fc->fss->s_eid == NOID ) {
110 fc->fss->s_eid = rs->sr_entry->e_id;
111 ber_dupbv( &fc->fss->s_base, &rs->sr_entry->e_nname );
113 } else if ( rs->sr_entry->e_id == fc->fss->s_eid &&
114 dn_match( &rs->sr_entry->e_nname, &fc->fss->s_base )) {
116 /* OK, the DN is the same and the entryID is the same. Now
117 * see if the fdn resides in the scope.
120 switch ( fc->fss->s_op->ors_scope ) {
121 case LDAP_SCOPE_BASE:
122 fc->fscope = dn_match( fc->fdn, &rs->sr_entry->e_nname );
124 case LDAP_SCOPE_ONELEVEL: {
126 dnParent( fc->fdn, &pdn );
127 fc->fscope = dn_match( &pdn, &rs->sr_entry->e_nname );
129 case LDAP_SCOPE_SUBTREE:
130 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname );
132 #ifdef LDAP_SCOPE_SUBORDINATE
133 case LDAP_SCOPE_SUBORDINATE:
134 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname ) &&
135 !dn_match( fc->fdn, &rs->sr_entry->e_nname );
145 syncprov_findbase( Operation *op, fbase_cookie *fc )
147 opcookie *opc = op->o_callback->sc_private;
148 slap_overinst *on = opc->son;
149 syncprov_info_t *si = on->on_bi.bi_private;
151 slap_callback cb = {0};
153 SlapReply frs = { REP_RESULT };
158 cb.sc_response = findbase_cb;
161 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync mode */
162 fop.o_callback = &cb;
163 fop.o_tag = LDAP_REQ_SEARCH;
164 fop.ors_scope = LDAP_SCOPE_BASE;
165 fop.ors_deref = fc->fss->s_op->ors_deref;
167 fop.ors_tlimit = SLAP_NO_LIMIT;
168 fop.ors_attrs = slap_anlist_no_attrs;
169 fop.ors_attrsonly = 1;
170 fop.ors_filter = fc->fss->s_op->ors_filter;
171 fop.ors_filterstr = fc->fss->s_op->ors_filterstr;
173 fop.o_req_ndn = fc->fss->s_op->o_req_ndn;
175 fop.o_bd->bd_info = on->on_info->oi_orig;
176 rc = fop.o_bd->be_search( &fop, &frs );
177 fop.o_bd->bd_info = (BackendInfo *)on;
179 if ( fc->fbase ) return LDAP_SUCCESS;
181 /* If entryID has changed, then the base of this search has
182 * changed. Invalidate the psearch.
184 return LDAP_NO_SUCH_OBJECT;
188 * This function has three different purposes, but they all use a search
189 * that filters on entryCSN so they're combined here.
190 * 1: when the current contextCSN is unknown (i.e., at server start time)
191 * and a syncrepl search has arrived with a cookie, we search for all entries
192 * with CSN >= the cookie CSN, and store the maximum as our contextCSN. Also,
193 * we expect to find the cookie CSN in the search results, and note if we did
194 * or not. If not, we assume the cookie is stale. (This may be too restrictive,
197 * 2: when the current contextCSN is known and we have a sync cookie, we search
198 * for one entry with CSN <= the cookie CSN. (Used to search for =.) If an
199 * entry is found, the cookie CSN is valid, otherwise it is stale. Case 1 is
200 * considered a special case of case 2, and both are generally called the
203 * 3: during a refresh phase, we search for all entries with CSN <= the cookie
204 * CSN, and generate Present records for them. We always collect this result
205 * in SyncID sets, even if there's only one match.
208 #define FIND_PRESENT 2
210 typedef struct fcsn_cookie {
211 struct berval maxcsn;
216 findcsn_cb( Operation *op, SlapReply *rs )
218 slap_callback *sc = op->o_callback;
220 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
221 /* If the private pointer is set, it points to an fcsn_cookie
222 * and we want to record the maxcsn and match state.
224 if ( sc->sc_private ) {
226 fcsn_cookie *fc = sc->sc_private;
227 syncrepl_state *srs = op->o_controls[sync_cid];
228 Attribute *a = attr_find(rs->sr_entry->e_attrs,
229 slap_schema.si_ad_entryCSN );
230 i = ber_bvcmp( &a->a_vals[0], srs->sr_state.ctxcsn );
231 if ( i == 0 ) fc->gotmatch = 1;
232 i = ber_bvcmp( &a->a_vals[0], &fc->maxcsn );
234 fc->maxcsn.bv_len = a->a_vals[0].bv_len;
235 strcpy(fc->maxcsn.bv_val, a->a_vals[0].bv_val );
238 /* Otherwise, if the private pointer is not set, we just
239 * want to know if any entry matched the filter.
241 sc->sc_private = (void *)1;
247 /* Build a list of entryUUIDs for sending in a SyncID set */
249 typedef struct fpres_cookie {
255 findpres_cb( Operation *op, SlapReply *rs )
257 slap_callback *sc = op->o_callback;
258 fpres_cookie *pc = sc->sc_private;
259 int ret = SLAP_CB_CONTINUE;
261 if ( rs->sr_type == REP_SEARCH ) {
262 ret = slap_build_syncUUID_set( op, &pc->uuids, rs->sr_entry );
266 if ( pc->num == SLAP_SYNCUUID_SET_SIZE ) {
267 rs->sr_rspoid = LDAP_SYNC_INFO;
268 ret = slap_send_syncinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
270 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
277 } else if ( rs->sr_type == REP_RESULT ) {
280 rs->sr_rspoid = LDAP_SYNC_INFO;
281 ret = slap_send_syncinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
283 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
293 syncprov_findcsn( Operation *op, int mode )
295 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
296 syncprov_info_t *si = on->on_bi.bi_private;
298 slap_callback cb = {0};
300 SlapReply frs = { REP_RESULT };
301 char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
302 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
305 AttributeAssertion eq;
308 fpres_cookie pcookie;
310 syncrepl_state *srs = op->o_controls[sync_cid];
312 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
317 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync_mode */
320 if ( mode == FIND_CSN ) {
321 if ( !si->si_gotcsn ) {
322 /* If we don't know the current ctxcsn, find it */
323 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
326 if ( !si->si_gotcsn ) {
327 cf.f_choice = LDAP_FILTER_GE;
328 fop.ors_attrsonly = 0;
329 fop.ors_attrs = csn_anlist;
330 fop.ors_slimit = SLAP_NO_LIMIT;
331 cb.sc_private = &fcookie;
332 fcookie.maxcsn.bv_val = cbuf;
333 fcookie.maxcsn.bv_len = 0;
334 fcookie.gotmatch = 0;
335 fbuf.bv_len = sprintf( buf, "(entryCSN>=%s)", srs->sr_state.ctxcsn->bv_val );
338 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
341 cf.f_choice = LDAP_FILTER_LE;
342 fop.ors_attrsonly = 1;
343 fop.ors_attrs = slap_anlist_no_attrs;
345 cb.sc_private = NULL;
346 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
348 cb.sc_response = findcsn_cb;
350 } else if ( mode == FIND_PRESENT ) {
351 cf.f_choice = LDAP_FILTER_LE;
352 fop.ors_attrsonly = 0;
353 fop.ors_attrs = uuid_anlist;
354 fop.ors_slimit = SLAP_NO_LIMIT;
355 /* We want pure entries, not referrals */
356 fop.o_managedsait = SLAP_CONTROL_CRITICAL;
357 cb.sc_private = &pcookie;
358 cb.sc_response = findpres_cb;
360 pcookie.uuids = NULL;
361 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
364 cf.f_av_desc = slap_schema.si_ad_entryCSN;
365 cf.f_av_value = *srs->sr_state.ctxcsn;
368 fop.o_callback = &cb;
369 fop.ors_tlimit = SLAP_NO_LIMIT;
370 fop.ors_filter = &cf;
371 fop.ors_filterstr = fbuf;
373 fop.o_bd->bd_info = on->on_info->oi_orig;
374 rc = fop.o_bd->be_search( &fop, &frs );
375 fop.o_bd->bd_info = (BackendInfo *)on;
377 if ( mode == FIND_CSN ) {
378 if ( !si->si_gotcsn ) {
379 strcpy(si->si_ctxcsnbuf, fcookie.maxcsn.bv_val);
380 si->si_ctxcsn.bv_len = fcookie.maxcsn.bv_len;
382 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
383 if ( fcookie.gotmatch ) return LDAP_SUCCESS;
386 if ( cb.sc_private ) return LDAP_SUCCESS;
388 } else if ( mode == FIND_PRESENT ) {
392 /* If matching CSN was not found, invalidate the context. */
393 return LDAP_NO_SUCH_OBJECT;
397 syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry *e, int mode )
399 slap_overinst *on = opc->son;
400 syncprov_info_t *si = on->on_bi.bi_private;
402 SlapReply rs = { REP_SEARCH };
403 LDAPControl *ctrls[2];
404 struct berval cookie;
406 Attribute a_uuid = {0};
407 Operation sop = *so->s_op;
409 syncrepl_state *srs = sop.o_controls[sync_cid];
413 sop.o_tmpmemctx = op->o_tmpmemctx;
416 slap_compose_sync_cookie( op, &cookie, &opc->sctxcsn,
417 srs->sr_state.sid, srs->sr_state.rid );
419 e_uuid.e_attrs = &a_uuid;
420 a_uuid.a_desc = slap_schema.si_ad_entryUUID;
421 a_uuid.a_nvals = &opc->suuid;
422 rs.sr_err = slap_build_sync_state_ctrl( &sop, &rs, &e_uuid,
423 mode, ctrls, 0, 1, &cookie );
429 if ( opc->sreference ) {
430 rs.sr_ref = get_entry_referrals( &sop, e );
431 send_search_reference( &sop, &rs );
432 ber_bvarray_free( rs.sr_ref );
436 case LDAP_SYNC_MODIFY:
437 rs.sr_attrs = sop.ors_attrs;
438 send_search_entry( &sop, &rs );
440 case LDAP_SYNC_DELETE:
441 e_uuid.e_attrs = NULL;
442 e_uuid.e_name = opc->sdn;
443 e_uuid.e_nname = opc->sndn;
444 rs.sr_entry = &e_uuid;
445 if ( opc->sreference ) {
450 send_search_reference( &sop, &rs );
452 send_search_entry( &sop, &rs );
458 free( rs.sr_ctrls[0] );
463 syncprov_matchops( Operation *op, opcookie *opc, int saveit )
465 slap_overinst *on = opc->son;
466 syncprov_info_t *si = on->on_bi.bi_private;
475 fc.fdn = &op->o_req_ndn;
477 if ( op->o_tag == LDAP_REQ_MODRDN && !saveit ) {
479 if ( op->orr_nnewSup ) pdn = *op->orr_nnewSup;
480 else dnParent( fc.fdn, &pdn );
481 build_new_dn( &newdn, &pdn, &op->orr_nnewrdn, op->o_tmpmemctx );
484 if ( op->o_tag != LDAP_REQ_ADD ) {
485 op->o_bd->bd_info = (BackendInfo *)on->on_info;
486 rc = be_entry_get_rw( op, fc.fdn, NULL, NULL, 0, &e );
487 op->o_bd->bd_info = (BackendInfo *)on;
494 ber_dupbv_x( &opc->sdn, &e->e_name, op->o_tmpmemctx );
495 ber_dupbv_x( &opc->sndn, &e->e_nname, op->o_tmpmemctx );
496 opc->sreference = is_entry_referral( e );
498 if ( saveit || op->o_tag == LDAP_REQ_ADD ) {
499 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
501 ber_dupbv_x( &opc->suuid, &a->a_nvals[0], op->o_tmpmemctx );
504 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
505 for (ss = si->si_ops; ss; ss=ss->s_next)
514 rc = syncprov_findbase( op, &fc );
515 if ( rc != LDAP_SUCCESS ) continue;
517 /* If we're sending results now, look for this op in old matches */
520 for ( sm=opc->smatches, old=(syncmatches *)&opc->smatches; sm;
521 old=sm, sm=sm->sm_next ) {
522 if ( sm->sm_op == ss ) {
524 old->sm_next = sm->sm_next;
525 op->o_tmpfree( sm, op->o_tmpmemctx );
531 /* check if current o_req_dn is in scope and matches filter */
532 if ( fc.fscope && test_filter( op, e, ss->s_filter ) ==
533 LDAP_COMPARE_TRUE ) {
535 sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
536 sm->sm_next = opc->smatches;
540 /* if found send UPDATE else send ADD */
541 syncprov_sendresp( op, opc, ss, e,
542 found ? LDAP_SYNC_MODIFY : LDAP_SYNC_ADD );
544 } else if ( !saveit && found ) {
546 syncprov_sendresp( op, opc, ss, NULL, LDAP_SYNC_DELETE );
549 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
550 if ( op->o_tag != LDAP_REQ_ADD ) {
551 op->o_bd->bd_info = (BackendInfo *)on->on_info;
552 be_entry_release_r( op, e );
553 op->o_bd->bd_info = (BackendInfo *)on;
558 syncprov_op_cleanup( Operation *op, SlapReply *rs )
560 slap_callback *cb = op->o_callback;
561 opcookie *opc = cb->sc_private;
562 syncmatches *sm, *snext;
564 for (sm = opc->smatches; sm; sm=snext) {
566 op->o_tmpfree( sm, op->o_tmpmemctx );
568 op->o_callback = cb->sc_next;
569 op->o_tmpfree(cb, op->o_tmpmemctx);
573 syncprov_op_response( Operation *op, SlapReply *rs )
575 opcookie *opc = op->o_callback->sc_private;
576 slap_overinst *on = opc->son;
577 syncprov_info_t *si = on->on_bi.bi_private;
580 if ( rs->sr_err == LDAP_SUCCESS )
582 struct berval maxcsn;
583 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
586 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
587 slap_get_commit_csn( op, &maxcsn );
588 if ( maxcsn.bv_val ) {
589 strcpy( cbuf, maxcsn.bv_val );
590 if ( ber_bvcmp( &maxcsn, &si->si_ctxcsn ) > 0 ) {
591 strcpy( si->si_ctxcsnbuf, cbuf );
592 si->si_ctxcsn.bv_len = maxcsn.bv_len;
596 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
598 opc->sctxcsn.bv_len = maxcsn.bv_len;
599 opc->sctxcsn.bv_val = cbuf;
604 case LDAP_REQ_MODIFY:
605 case LDAP_REQ_MODRDN:
606 case LDAP_REQ_EXTENDED:
607 syncprov_matchops( op, opc, 0 );
609 case LDAP_REQ_DELETE:
610 /* for each match in opc->smatches:
613 for ( sm = opc->smatches; sm; sm=sm->sm_next ) {
614 syncprov_sendresp( op, opc, sm->sm_op, NULL,
622 return SLAP_CB_CONTINUE;
627 syncprov_op_compare( Operation *op, SlapReply *rs )
629 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
630 syncprov_info_t *si = on->on_bi.bi_private;
631 int rc = SLAP_CB_CONTINUE;
633 if ( dn_match( &op->o_req_ndn, &si->si_e->e_nname ) )
637 ldap_pvt_thread_mutex_lock( &si->si_e_mutex );
639 if ( get_assert( op ) &&
640 ( test_filter( op, si->si_e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
642 rs->sr_err = LDAP_ASSERTION_FAILED;
646 rs->sr_err = access_allowed( op, si->si_e, op->oq_compare.rs_ava->aa_desc,
647 &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
648 if ( ! rs->sr_err ) {
649 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
653 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
655 for ( a = attr_find( si->si_e->e_attrs, op->oq_compare.rs_ava->aa_desc );
657 a = attr_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
659 rs->sr_err = LDAP_COMPARE_FALSE;
661 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
662 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
663 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
664 a->a_nvals, &op->oq_compare.rs_ava->aa_value, op->o_tmpmemctx ) == 0 )
666 rs->sr_err = LDAP_COMPARE_TRUE;
673 ldap_pvt_thread_mutex_unlock( &si->si_e_mutex );
675 send_ldap_result( op, rs );
677 if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
678 rs->sr_err = LDAP_SUCCESS;
683 return SLAP_CB_CONTINUE;
688 syncprov_op_mod( Operation *op, SlapReply *rs )
690 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
691 syncprov_info_t *si = on->on_bi.bi_private;
693 slap_callback *cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(opcookie), op->o_tmpmemctx);
694 opcookie *opc = (opcookie *)(cb+1);
696 cb->sc_response = syncprov_op_response;
697 cb->sc_cleanup = syncprov_op_cleanup;
698 cb->sc_private = opc;
699 cb->sc_next = op->o_callback;
702 if ( si->si_ops && op->o_tag != LDAP_REQ_ADD )
703 syncprov_matchops( op, opc, 1 );
705 return SLAP_CB_CONTINUE;
709 syncprov_op_extended( Operation *op, SlapReply *rs )
711 if ( exop_is_write( op ))
712 return syncprov_op_mod( op, rs );
714 return SLAP_CB_CONTINUE;
717 typedef struct searchstate {
718 slap_overinst *ss_on;
724 syncprov_search_cleanup( Operation *op, SlapReply *rs )
726 searchstate *ss = op->o_callback->sc_private;
727 if ( rs->sr_ctrls ) {
728 free( rs->sr_ctrls[0] );
729 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
732 op->o_sync_mode |= SLAP_SYNC_REFRESH_AND_PERSIST;
737 syncprov_search_response( Operation *op, SlapReply *rs )
739 searchstate *ss = op->o_callback->sc_private;
740 slap_overinst *on = ss->ss_on;
741 syncprov_info_t *si = on->on_bi.bi_private;
742 syncrepl_state *srs = op->o_controls[sync_cid];
744 if ( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF ) {
746 if ( srs->sr_state.ctxcsn ) {
747 Attribute *a = attr_find( rs->sr_entry->e_attrs,
748 slap_schema.si_ad_entryCSN );
749 /* Don't send the ctx entry twice */
750 if ( bvmatch( &a->a_nvals[0], srs->sr_state.ctxcsn ))
753 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
755 rs->sr_ctrls[1] = NULL;
756 rs->sr_err = slap_build_sync_state_ctrl( op, rs, rs->sr_entry,
757 LDAP_SYNC_ADD, rs->sr_ctrls, 0, 0, NULL );
758 } else if ( rs->sr_type == REP_RESULT && rs->sr_err == LDAP_SUCCESS ) {
759 struct berval cookie;
761 slap_compose_sync_cookie( op, &cookie,
762 &op->ors_filter->f_and->f_ava->aa_value,
763 srs->sr_state.sid, srs->sr_state.rid );
765 /* Is this a regular refresh? */
767 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
769 rs->sr_ctrls[1] = NULL;
770 rs->sr_err = slap_build_sync_done_ctrl( op, rs, rs->sr_ctrls,
771 0, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
773 /* It's RefreshAndPersist, transition to Persist phase */
774 rs->sr_rspoid = LDAP_SYNC_INFO;
775 slap_send_syncinfo( op, rs, rs->sr_nentries ?
776 LDAP_TAG_SYNC_REFRESH_PRESENT : LDAP_TAG_SYNC_REFRESH_DELETE,
777 &cookie, 1, NULL, 0 );
778 /* Flush any queued persist messages */
781 /* Turn off the refreshing flag */
782 ss->ss_so->s_flags ^= PS_IS_REFRESHING;
784 /* Detach this Op from frontend control */
792 return SLAP_CB_CONTINUE;
796 syncprov_op_search( Operation *op, SlapReply *rs )
798 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
799 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
801 int gotstate = 0, nochange = 0;
807 if ( !(op->o_sync_mode & SLAP_SYNC_REFRESH) ) return SLAP_CB_CONTINUE;
809 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
810 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "illegal value for derefAliases" );
814 srs = op->o_controls[sync_cid];
816 /* If this is a persistent search, set it up right away */
817 if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
827 so.s_flags = PS_IS_REFRESHING;
828 /* syncprov_findbase expects to be called as a callback... */
829 sc.sc_private = &opc;
832 op->o_callback = ≻
833 rs->sr_err = syncprov_findbase( op, &fc );
836 if ( rs->sr_err != LDAP_SUCCESS ) {
837 send_ldap_result( op, rs );
840 sop = ch_malloc( sizeof( syncops ));
842 ldap_pvt_thread_mutex_init( &sop->s_mutex );
843 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
844 sop->s_next = si->si_ops;
846 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
849 /* If we have a cookie, handle the PRESENT lookups
851 if ( srs->sr_state.ctxcsn ) {
852 /* Is the CSN in a valid format? */
853 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
854 send_ldap_error( op, rs, LDAP_OTHER, "invalid sync cookie" );
857 /* Is the CSN still present in the database? */
858 if ( syncprov_findcsn( op, FIND_CSN ) != LDAP_SUCCESS ) {
859 /* No, so a reload is required */
860 #if 0 /* the consumer doesn't seem to send this hint */
861 if ( op->o_sync_rhint == 0 ) {
862 send_ldap_error( op, rs, LDAP_SYNC_REFRESH_REQUIRED, "sync cookie is stale" );
868 /* If just Refreshing and nothing has changed, shortcut it */
869 if ( bvmatch( srs->sr_state.ctxcsn, &si->si_ctxcsn )) {
871 if ( !(op->o_sync_mode & SLAP_SYNC_PERSIST) ) {
872 LDAPControl *ctrls[2];
876 slap_build_sync_done_ctrl( op, rs, ctrls, 0, 0,
877 NULL, LDAP_SYNC_REFRESH_DELETES );
878 rs->sr_ctrls = ctrls;
879 rs->sr_err = LDAP_SUCCESS;
880 send_ldap_result( op, rs );
885 /* If context has changed, check for Present UUIDs */
886 if ( syncprov_findcsn( op, FIND_PRESENT ) != LDAP_SUCCESS ) {
887 send_ldap_result( op, rs );
893 /* If we didn't get a cookie and we don't know our contextcsn, try to
896 if ( !gotstate && !si->si_gotcsn ) {
897 struct berval bv = BER_BVC("1"), *old;
899 old = srs->sr_state.ctxcsn;
900 srs->sr_state.ctxcsn = &bv;
901 syncprov_findcsn( op, FIND_CSN );
902 srs->sr_state.ctxcsn = old;
905 /* Append CSN range to search filter, save original filter
906 * for persistent search evaluation
909 sop->s_filter = op->ors_filter;
912 fand = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
913 fand->f_choice = LDAP_FILTER_AND;
915 fava = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
916 fava->f_choice = LDAP_FILTER_LE;
917 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
918 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
919 ber_dupbv_x( &fava->f_ava->aa_value, &si->si_ctxcsn, op->o_tmpmemctx );
922 fava->f_next = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
924 fava->f_choice = LDAP_FILTER_GE;
925 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
926 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
927 ber_dupbv_x( &fava->f_ava->aa_value, srs->sr_state.ctxcsn, op->o_tmpmemctx );
929 fava->f_next = op->ors_filter;
930 op->ors_filter = fand;
931 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
934 /* Let our callback add needed info to returned entries */
935 cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(searchstate), op->o_tmpmemctx);
936 ss = (searchstate *)(cb+1);
940 cb->sc_response = syncprov_search_response;
941 cb->sc_cleanup = syncprov_search_cleanup;
943 cb->sc_next = op->o_callback;
946 /* FIXME: temporary hack to make sure back-bdb's native Psearch handling
947 * doesn't get invoked. We can skip this after the back-bdb code is
948 * removed, and also delete ss->ss_done.
950 op->o_sync_mode &= SLAP_CONTROL_MASK;
952 /* If this is a persistent search and no changes were reported during
953 * the refresh phase, just invoke the response callback to transition
954 * us into persist phase
957 rs->sr_err = LDAP_SUCCESS;
959 send_ldap_result( op, rs );
962 return SLAP_CB_CONTINUE;
974 slap_overinst *on = (slap_overinst *)be->bd_info;
975 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
978 if ( strcasecmp( argv[ 0 ], "syncprov-checkpoint" ) == 0 ) {
980 fprintf( stderr, "%s: line %d: wrong number of arguments in "
981 "\"syncprov-checkpoint <ops> <minutes>\"\n", fname, lineno );
984 si->si_chkops = atoi( argv[1] );
985 si->si_chktime = atoi( argv[2] ) * 60;
988 return SLAP_CONF_UNKNOWN;
992 return SLAP_CONF_UNKNOWN;
1000 slap_overinst *on = (slap_overinst *)be->bd_info;
1001 syncprov_info_t *si;
1003 si = ch_calloc(1, sizeof(syncprov_info_t));
1004 on->on_bi.bi_private = si;
1005 ldap_pvt_thread_mutex_init( &si->si_csn_mutex );
1006 ldap_pvt_thread_mutex_init( &si->si_ops_mutex );
1007 si->si_ctxcsn.bv_val = si->si_ctxcsnbuf;
1009 csn_anlist[0].an_desc = slap_schema.si_ad_entryCSN;
1010 csn_anlist[0].an_name = slap_schema.si_ad_entryCSN->ad_cname;
1012 uuid_anlist[0].an_desc = slap_schema.si_ad_entryUUID;
1013 uuid_anlist[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1015 sync_cid = slap_cids.sc_LDAPsync;
1021 syncprov_db_destroy(
1025 slap_overinst *on = (slap_overinst *)be->bd_info;
1026 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1029 ldap_pvt_thread_mutex_destroy( &si->si_ops_mutex );
1030 ldap_pvt_thread_mutex_destroy( &si->si_csn_mutex );
1037 /* This overlay is set up for dynamic loading via moduleload. For static
1038 * configuration, you'll need to arrange for the slap_overinst to be
1039 * initialized and registered by some other function inside slapd.
1042 static slap_overinst syncprov;
1047 syncprov.on_bi.bi_type = "syncprov";
1048 syncprov.on_bi.bi_db_init = syncprov_db_init;
1049 syncprov.on_bi.bi_db_config = syncprov_db_config;
1050 syncprov.on_bi.bi_db_destroy = syncprov_db_destroy;
1052 syncprov.on_bi.bi_op_add = syncprov_op_mod;
1054 syncprov.on_bi.bi_op_compare = syncprov_op_compare;
1056 syncprov.on_bi.bi_op_delete = syncprov_op_mod;
1057 syncprov.on_bi.bi_op_modify = syncprov_op_mod;
1058 syncprov.on_bi.bi_op_modrdn = syncprov_op_mod;
1059 syncprov.on_bi.bi_op_search = syncprov_op_search;
1060 syncprov.on_bi.bi_extended = syncprov_op_extended;
1063 syncprov.on_response = syncprov_response;
1066 return overlay_register( &syncprov );
1069 #if SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC
1071 init_module( int argc, char *argv[] )
1073 return syncprov_init();
1075 #endif /* SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC */
1077 #endif /* defined(SLAPD_OVER_SYNCPROV) */