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;
39 /* Record of a persistent search */
40 typedef struct syncops {
41 struct syncops *s_next;
42 struct berval s_base; /* ndn of search base */
43 ID s_eid; /* entryID of search base */
44 Operation *s_op; /* search op */
47 struct berval s_filterstr;
48 int s_flags; /* search status */
49 struct syncres *s_res;
50 struct syncres *s_restail;
51 ldap_pvt_thread_mutex_t s_mutex;
56 #define PS_IS_REFRESHING 0x01
58 /* Record of which searches matched at premodify step */
59 typedef struct syncmatches {
60 struct syncmatches *sm_next;
64 typedef struct syncprov_info_t {
66 struct berval si_ctxcsn; /* ldapsync context */
67 int si_gotcsn; /* is our ctxcsn up to date? */
68 ldap_pvt_thread_mutex_t si_csn_mutex;
69 ldap_pvt_thread_mutex_t si_ops_mutex;
70 char si_ctxcsnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
73 typedef struct opcookie {
75 syncmatches *smatches;
76 struct berval sdn; /* DN of entry, for deletes */
78 struct berval suuid; /* UUID of entry */
79 struct berval sctxcsn;
80 int sreference; /* Is the entry a reference? */
83 typedef struct fbase_cookie {
84 struct berval *fdn; /* DN of a modified entry, for scope testing */
85 syncops *fss; /* persistent search we're testing against */
86 int fbase; /* if TRUE we found the search base and it's still valid */
87 int fscope; /* if TRUE then fdn is within the psearch scope */
90 static AttributeName csn_anlist[2];
91 static AttributeName uuid_anlist[2];
94 * finds the true DN of the base of a search (with alias dereferencing) and
95 * checks to make sure the base entry doesn't get replaced with a different
96 * entry (e.g., swapping trees via ModDN, or retargeting an alias). If a
97 * change is detected, any persistent search on this base must be terminated /
99 * On the first call, we just save the DN and entryID. On subsequent calls
100 * we compare the DN and entryID with the saved values.
103 findbase_cb( Operation *op, SlapReply *rs )
105 slap_callback *sc = op->o_callback;
107 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
108 fbase_cookie *fc = sc->sc_private;
110 /* If no entryID, we're looking for the first time.
111 * Just store whatever we got.
113 if ( fc->fss->s_eid == NOID ) {
115 fc->fss->s_eid = rs->sr_entry->e_id;
116 ber_dupbv( &fc->fss->s_base, &rs->sr_entry->e_nname );
118 } else if ( rs->sr_entry->e_id == fc->fss->s_eid &&
119 dn_match( &rs->sr_entry->e_nname, &fc->fss->s_base )) {
121 /* OK, the DN is the same and the entryID is the same. Now
122 * see if the fdn resides in the scope.
125 switch ( fc->fss->s_op->ors_scope ) {
126 case LDAP_SCOPE_BASE:
127 fc->fscope = dn_match( fc->fdn, &rs->sr_entry->e_nname );
129 case LDAP_SCOPE_ONELEVEL: {
131 dnParent( fc->fdn, &pdn );
132 fc->fscope = dn_match( &pdn, &rs->sr_entry->e_nname );
134 case LDAP_SCOPE_SUBTREE:
135 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname );
137 #ifdef LDAP_SCOPE_SUBORDINATE
138 case LDAP_SCOPE_SUBORDINATE:
139 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname ) &&
140 !dn_match( fc->fdn, &rs->sr_entry->e_nname );
150 syncprov_findbase( Operation *op, fbase_cookie *fc )
152 opcookie *opc = op->o_callback->sc_private;
153 slap_overinst *on = opc->son;
154 syncprov_info_t *si = on->on_bi.bi_private;
156 slap_callback cb = {0};
158 SlapReply frs = { REP_RESULT };
163 cb.sc_response = findbase_cb;
166 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync mode */
167 fop.o_callback = &cb;
168 fop.o_tag = LDAP_REQ_SEARCH;
169 fop.ors_scope = LDAP_SCOPE_BASE;
170 fop.ors_deref = fc->fss->s_op->ors_deref;
172 fop.ors_tlimit = SLAP_NO_LIMIT;
173 fop.ors_attrs = slap_anlist_no_attrs;
174 fop.ors_attrsonly = 1;
175 fop.ors_filter = fc->fss->s_op->ors_filter;
176 fop.ors_filterstr = fc->fss->s_op->ors_filterstr;
178 fop.o_req_ndn = fc->fss->s_op->o_req_ndn;
180 fop.o_bd->bd_info = on->on_info->oi_orig;
181 rc = fop.o_bd->be_search( &fop, &frs );
182 fop.o_bd->bd_info = (BackendInfo *)on;
184 if ( fc->fbase ) return LDAP_SUCCESS;
186 /* If entryID has changed, then the base of this search has
187 * changed. Invalidate the psearch.
189 return LDAP_NO_SUCH_OBJECT;
193 * This function has three different purposes, but they all use a search
194 * that filters on entryCSN so they're combined here.
195 * 1: when the current contextCSN is unknown (i.e., at server start time)
196 * and a syncrepl search has arrived with a cookie, we search for all entries
197 * with CSN >= the cookie CSN, and store the maximum as our contextCSN. Also,
198 * we expect to find the cookie CSN in the search results, and note if we did
199 * or not. If not, we assume the cookie is stale. (This may be too restrictive,
202 * 2: when the current contextCSN is known and we have a sync cookie, we search
203 * for one entry with CSN <= the cookie CSN. (Used to search for =.) If an
204 * entry is found, the cookie CSN is valid, otherwise it is stale. Case 1 is
205 * considered a special case of case 2, and both are generally called the
208 * 3: during a refresh phase, we search for all entries with CSN <= the cookie
209 * CSN, and generate Present records for them. We always collect this result
210 * in SyncID sets, even if there's only one match.
213 #define FIND_PRESENT 2
215 typedef struct fcsn_cookie {
216 struct berval maxcsn;
221 findcsn_cb( Operation *op, SlapReply *rs )
223 slap_callback *sc = op->o_callback;
225 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
226 /* If the private pointer is set, it points to an fcsn_cookie
227 * and we want to record the maxcsn and match state.
229 if ( sc->sc_private ) {
231 fcsn_cookie *fc = sc->sc_private;
232 syncrepl_state *srs = op->o_controls[sync_cid];
233 Attribute *a = attr_find(rs->sr_entry->e_attrs,
234 slap_schema.si_ad_entryCSN );
235 i = ber_bvcmp( &a->a_vals[0], srs->sr_state.ctxcsn );
236 if ( i == 0 ) fc->gotmatch = 1;
237 i = ber_bvcmp( &a->a_vals[0], &fc->maxcsn );
239 fc->maxcsn.bv_len = a->a_vals[0].bv_len;
240 strcpy(fc->maxcsn.bv_val, a->a_vals[0].bv_val );
243 /* Otherwise, if the private pointer is not set, we just
244 * want to know if any entry matched the filter.
246 sc->sc_private = (void *)1;
252 /* Build a list of entryUUIDs for sending in a SyncID set */
254 typedef struct fpres_cookie {
260 findpres_cb( Operation *op, SlapReply *rs )
262 slap_callback *sc = op->o_callback;
263 fpres_cookie *pc = sc->sc_private;
264 int ret = SLAP_CB_CONTINUE;
266 if ( rs->sr_type == REP_SEARCH ) {
267 ret = slap_build_syncUUID_set( op, &pc->uuids, rs->sr_entry );
271 if ( pc->num == SLAP_SYNCUUID_SET_SIZE ) {
272 rs->sr_rspoid = LDAP_SYNC_INFO;
273 ret = slap_send_syncinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
275 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
282 } else if ( rs->sr_type == REP_RESULT ) {
285 rs->sr_rspoid = LDAP_SYNC_INFO;
286 ret = slap_send_syncinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
288 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
298 syncprov_findcsn( Operation *op, int mode )
300 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
301 syncprov_info_t *si = on->on_bi.bi_private;
303 slap_callback cb = {0};
305 SlapReply frs = { REP_RESULT };
306 char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
307 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
310 AttributeAssertion eq;
313 fpres_cookie pcookie;
315 syncrepl_state *srs = op->o_controls[sync_cid];
317 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
322 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync_mode */
325 if ( mode == FIND_CSN ) {
326 if ( !si->si_gotcsn ) {
327 /* If we don't know the current ctxcsn, find it */
328 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
331 if ( !si->si_gotcsn ) {
332 cf.f_choice = LDAP_FILTER_GE;
333 fop.ors_attrsonly = 0;
334 fop.ors_attrs = csn_anlist;
335 fop.ors_slimit = SLAP_NO_LIMIT;
336 cb.sc_private = &fcookie;
337 fcookie.maxcsn.bv_val = cbuf;
338 fcookie.maxcsn.bv_len = 0;
339 fcookie.gotmatch = 0;
340 fbuf.bv_len = sprintf( buf, "(entryCSN>=%s)", srs->sr_state.ctxcsn->bv_val );
343 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
346 cf.f_choice = LDAP_FILTER_LE;
347 fop.ors_attrsonly = 1;
348 fop.ors_attrs = slap_anlist_no_attrs;
350 cb.sc_private = NULL;
351 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
353 cb.sc_response = findcsn_cb;
355 } else if ( mode == FIND_PRESENT ) {
356 cf.f_choice = LDAP_FILTER_LE;
357 fop.ors_attrsonly = 0;
358 fop.ors_attrs = uuid_anlist;
359 fop.ors_slimit = SLAP_NO_LIMIT;
360 /* We want pure entries, not referrals */
361 fop.o_managedsait = SLAP_CONTROL_CRITICAL;
362 cb.sc_private = &pcookie;
363 cb.sc_response = findpres_cb;
365 pcookie.uuids = NULL;
366 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
369 cf.f_av_desc = slap_schema.si_ad_entryCSN;
370 cf.f_av_value = *srs->sr_state.ctxcsn;
373 fop.o_callback = &cb;
374 fop.ors_tlimit = SLAP_NO_LIMIT;
375 fop.ors_filter = &cf;
376 fop.ors_filterstr = fbuf;
378 fop.o_bd->bd_info = on->on_info->oi_orig;
379 rc = fop.o_bd->be_search( &fop, &frs );
380 fop.o_bd->bd_info = (BackendInfo *)on;
382 if ( mode == FIND_CSN ) {
383 if ( !si->si_gotcsn ) {
384 strcpy(si->si_ctxcsnbuf, fcookie.maxcsn.bv_val);
385 si->si_ctxcsn.bv_len = fcookie.maxcsn.bv_len;
387 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
388 if ( fcookie.gotmatch ) return LDAP_SUCCESS;
391 if ( cb.sc_private ) return LDAP_SUCCESS;
393 } else if ( mode == FIND_PRESENT ) {
397 /* If matching CSN was not found, invalidate the context. */
398 return LDAP_NO_SUCH_OBJECT;
401 /* Queue a persistent search response if still in Refresh stage */
403 syncprov_qresp( opcookie *opc, syncops *so, int mode )
407 sr = ch_malloc(sizeof(syncres) + opc->suuid.bv_len + 1 +
408 opc->sdn.bv_len + 1 + opc->sndn.bv_len + 1 + opc->sctxcsn.bv_len + 1 );
410 sr->s_dn.bv_val = (char *)(sr + 1);
412 sr->s_isreference = opc->sreference;
413 sr->s_ndn.bv_val = lutil_strcopy( sr->s_dn.bv_val, opc->sdn.bv_val );
414 *(sr->s_ndn.bv_val++) = '\0';
415 sr->s_uuid.bv_val = lutil_strcopy( sr->s_ndn.bv_val, opc->sndn.bv_val );
416 *(sr->s_uuid.bv_val++) = '\0';
417 sr->s_csn.bv_val = lutil_strcopy( sr->s_uuid.bv_val, opc->suuid.bv_val );
422 so->s_restail->s_next = sr;
425 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
429 /* Send a persistent search response */
431 syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry *e, int mode, int queue )
433 slap_overinst *on = opc->son;
434 syncprov_info_t *si = on->on_bi.bi_private;
436 SlapReply rs = { REP_SEARCH };
437 LDAPControl *ctrls[2];
438 struct berval cookie;
440 Attribute a_uuid = {0};
441 Operation sop = *so->s_op;
446 sop.o_tmpmemctx = op->o_tmpmemctx;
448 sop.o_controls = op->o_controls;
450 if ( queue && (so->s_flags & PS_IS_REFRESHING) ) {
451 ldap_pvt_thread_mutex_lock( &so->s_mutex );
452 if ( so->s_flags & PS_IS_REFRESHING )
453 return syncprov_qresp( opc, so, mode );
454 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
458 slap_compose_sync_cookie( op, &cookie, &opc->sctxcsn,
459 so->s_sid, so->s_rid );
461 e_uuid.e_attrs = &a_uuid;
462 a_uuid.a_desc = slap_schema.si_ad_entryUUID;
463 a_uuid.a_nvals = &opc->suuid;
464 rs.sr_err = slap_build_sync_state_ctrl( &sop, &rs, &e_uuid,
465 mode, ctrls, 0, 1, &cookie );
471 if ( opc->sreference ) {
472 rs.sr_ref = get_entry_referrals( &sop, e );
473 send_search_reference( &sop, &rs );
474 ber_bvarray_free( rs.sr_ref );
478 case LDAP_SYNC_MODIFY:
479 rs.sr_attrs = sop.ors_attrs;
480 send_search_entry( &sop, &rs );
482 case LDAP_SYNC_DELETE:
483 e_uuid.e_attrs = NULL;
484 e_uuid.e_name = opc->sdn;
485 e_uuid.e_nname = opc->sndn;
486 rs.sr_entry = &e_uuid;
487 if ( opc->sreference ) {
492 send_search_reference( &sop, &rs );
494 send_search_entry( &sop, &rs );
500 free( rs.sr_ctrls[0] );
505 syncprov_matchops( Operation *op, opcookie *opc, int saveit )
507 slap_overinst *on = opc->son;
508 syncprov_info_t *si = on->on_bi.bi_private;
517 fc.fdn = &op->o_req_ndn;
519 if ( op->o_tag == LDAP_REQ_MODRDN && !saveit ) {
521 if ( op->orr_nnewSup ) pdn = *op->orr_nnewSup;
522 else dnParent( fc.fdn, &pdn );
523 build_new_dn( &newdn, &pdn, &op->orr_nnewrdn, op->o_tmpmemctx );
526 if ( op->o_tag != LDAP_REQ_ADD ) {
527 op->o_bd->bd_info = (BackendInfo *)on->on_info;
528 rc = be_entry_get_rw( op, fc.fdn, NULL, NULL, 0, &e );
529 op->o_bd->bd_info = (BackendInfo *)on;
536 ber_dupbv_x( &opc->sdn, &e->e_name, op->o_tmpmemctx );
537 ber_dupbv_x( &opc->sndn, &e->e_nname, op->o_tmpmemctx );
538 opc->sreference = is_entry_referral( e );
540 if ( saveit || op->o_tag == LDAP_REQ_ADD ) {
541 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
543 ber_dupbv_x( &opc->suuid, &a->a_nvals[0], op->o_tmpmemctx );
546 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
547 for (ss = si->si_ops; ss; ss=ss->s_next)
556 rc = syncprov_findbase( op, &fc );
557 if ( rc != LDAP_SUCCESS ) continue;
559 /* If we're sending results now, look for this op in old matches */
562 for ( sm=opc->smatches, old=(syncmatches *)&opc->smatches; sm;
563 old=sm, sm=sm->sm_next ) {
564 if ( sm->sm_op == ss ) {
566 old->sm_next = sm->sm_next;
567 op->o_tmpfree( sm, op->o_tmpmemctx );
573 /* check if current o_req_dn is in scope and matches filter */
574 if ( fc.fscope && test_filter( op, e, ss->s_op->ors_filter ) ==
575 LDAP_COMPARE_TRUE ) {
577 sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
578 sm->sm_next = opc->smatches;
582 /* if found send UPDATE else send ADD */
583 syncprov_sendresp( op, opc, ss, e,
584 found ? LDAP_SYNC_MODIFY : LDAP_SYNC_ADD, 1 );
586 } else if ( !saveit && found ) {
588 syncprov_sendresp( op, opc, ss, NULL, LDAP_SYNC_DELETE, 1 );
591 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
592 if ( op->o_tag != LDAP_REQ_ADD ) {
593 op->o_bd->bd_info = (BackendInfo *)on->on_info;
594 be_entry_release_r( op, e );
595 op->o_bd->bd_info = (BackendInfo *)on;
600 syncprov_op_cleanup( Operation *op, SlapReply *rs )
602 slap_callback *cb = op->o_callback;
603 opcookie *opc = cb->sc_private;
604 syncmatches *sm, *snext;
606 for (sm = opc->smatches; sm; sm=snext) {
608 op->o_tmpfree( sm, op->o_tmpmemctx );
610 op->o_callback = cb->sc_next;
611 op->o_tmpfree(cb, op->o_tmpmemctx);
615 syncprov_op_response( Operation *op, SlapReply *rs )
617 opcookie *opc = op->o_callback->sc_private;
618 slap_overinst *on = opc->son;
619 syncprov_info_t *si = on->on_bi.bi_private;
622 if ( rs->sr_err == LDAP_SUCCESS )
624 struct berval maxcsn;
625 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
628 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
629 slap_get_commit_csn( op, &maxcsn );
630 if ( maxcsn.bv_val ) {
631 strcpy( cbuf, maxcsn.bv_val );
632 if ( ber_bvcmp( &maxcsn, &si->si_ctxcsn ) > 0 ) {
633 strcpy( si->si_ctxcsnbuf, cbuf );
634 si->si_ctxcsn.bv_len = maxcsn.bv_len;
638 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
640 opc->sctxcsn.bv_len = maxcsn.bv_len;
641 opc->sctxcsn.bv_val = cbuf;
646 case LDAP_REQ_MODIFY:
647 case LDAP_REQ_MODRDN:
648 case LDAP_REQ_EXTENDED:
649 syncprov_matchops( op, opc, 0 );
651 case LDAP_REQ_DELETE:
652 /* for each match in opc->smatches:
655 for ( sm = opc->smatches; sm; sm=sm->sm_next ) {
656 syncprov_sendresp( op, opc, sm->sm_op, NULL,
657 LDAP_SYNC_DELETE, 1 );
664 return SLAP_CB_CONTINUE;
669 syncprov_op_compare( Operation *op, SlapReply *rs )
671 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
672 syncprov_info_t *si = on->on_bi.bi_private;
673 int rc = SLAP_CB_CONTINUE;
675 if ( dn_match( &op->o_req_ndn, &si->si_e->e_nname ) )
679 ldap_pvt_thread_mutex_lock( &si->si_e_mutex );
681 if ( get_assert( op ) &&
682 ( test_filter( op, si->si_e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
684 rs->sr_err = LDAP_ASSERTION_FAILED;
688 rs->sr_err = access_allowed( op, si->si_e, op->oq_compare.rs_ava->aa_desc,
689 &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
690 if ( ! rs->sr_err ) {
691 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
695 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
697 for ( a = attr_find( si->si_e->e_attrs, op->oq_compare.rs_ava->aa_desc );
699 a = attr_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
701 rs->sr_err = LDAP_COMPARE_FALSE;
703 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
704 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
705 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
706 a->a_nvals, &op->oq_compare.rs_ava->aa_value, op->o_tmpmemctx ) == 0 )
708 rs->sr_err = LDAP_COMPARE_TRUE;
715 ldap_pvt_thread_mutex_unlock( &si->si_e_mutex );
717 send_ldap_result( op, rs );
719 if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
720 rs->sr_err = LDAP_SUCCESS;
725 return SLAP_CB_CONTINUE;
730 syncprov_op_mod( Operation *op, SlapReply *rs )
732 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
733 syncprov_info_t *si = on->on_bi.bi_private;
735 slap_callback *cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(opcookie), op->o_tmpmemctx);
736 opcookie *opc = (opcookie *)(cb+1);
738 cb->sc_response = syncprov_op_response;
739 cb->sc_cleanup = syncprov_op_cleanup;
740 cb->sc_private = opc;
741 cb->sc_next = op->o_callback;
744 if ( si->si_ops && op->o_tag != LDAP_REQ_ADD )
745 syncprov_matchops( op, opc, 1 );
747 return SLAP_CB_CONTINUE;
751 syncprov_op_extended( Operation *op, SlapReply *rs )
753 if ( exop_is_write( op ))
754 return syncprov_op_mod( op, rs );
756 return SLAP_CB_CONTINUE;
759 typedef struct searchstate {
760 slap_overinst *ss_on;
765 syncprov_search_cleanup( Operation *op, SlapReply *rs )
767 searchstate *ss = op->o_callback->sc_private;
768 if ( rs->sr_ctrls ) {
769 free( rs->sr_ctrls[0] );
770 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
776 syncprov_detach_op( Operation *op, syncops *so )
783 /* count the search attrs */
784 for (i=0; op->ors_attrs && op->ors_attrs[i].an_name.bv_val; i++) {
785 alen += op->ors_attrs[i].an_name.bv_len + 1;
787 /* Make a new copy of the operation */
788 size = sizeof(Operation) + sizeof(Opheader) +
789 (i ? ( (i+1) * sizeof(AttributeName) + alen) : 0) +
790 op->o_req_dn.bv_len + 1 +
791 op->o_req_ndn.bv_len + 1 +
792 op->o_ndn.bv_len + 1 +
793 so->s_filterstr.bv_len + 1;
794 op2 = (Operation *)ch_malloc( size );
796 op2->o_hdr = (Opheader *)(op2+1);
797 *op2->o_hdr = *op->o_hdr;
799 op2->ors_attrs = (AttributeName *)(op2->o_hdr + 1);
800 ptr = (char *)(op2->ors_attrs+i+1);
801 for (i=0; op->ors_attrs[i].an_name.bv_val; i++) {
802 op2->ors_attrs[i] = op->ors_attrs[i];
803 op2->ors_attrs[i].an_name.bv_val = ptr;
804 ptr = lutil_strcopy( ptr, op->ors_attrs[i].an_name.bv_val ) + 1;
807 ptr = (char *)(op2->o_hdr + 1);
809 op2->o_ndn.bv_val = ptr;
810 ptr = lutil_strcopy(ptr, op->o_ndn.bv_val) + 1;
811 op2->o_dn = op2->o_ndn;
812 op2->o_req_dn.bv_val = ptr;
813 ptr = lutil_strcopy(ptr, op->o_req_dn.bv_val) + 1;
814 op2->o_req_ndn.bv_val = ptr;
815 ptr = lutil_strcopy(ptr, op->o_req_ndn.bv_val) + 1;
816 op2->ors_filterstr.bv_val = ptr;
817 strcpy( ptr, so->s_filterstr.bv_val );
818 op2->ors_filterstr.bv_len = so->s_filterstr.bv_len;
819 op2->ors_filter = str2filter( ptr );
820 op2->o_controls = NULL;
821 op2->o_callback = NULL;
824 /* Increment number of ops so that idletimeout ignores us */
825 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
826 op->o_conn->c_n_ops_executing++;
827 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
831 syncprov_search_response( Operation *op, SlapReply *rs )
833 searchstate *ss = op->o_callback->sc_private;
834 slap_overinst *on = ss->ss_on;
835 syncprov_info_t *si = on->on_bi.bi_private;
836 syncrepl_state *srs = op->o_controls[sync_cid];
838 if ( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF ) {
840 if ( srs->sr_state.ctxcsn ) {
841 Attribute *a = attr_find( rs->sr_entry->e_attrs,
842 slap_schema.si_ad_entryCSN );
843 /* Don't send the ctx entry twice */
844 if ( bvmatch( &a->a_nvals[0], srs->sr_state.ctxcsn ))
847 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
849 rs->sr_ctrls[1] = NULL;
850 rs->sr_err = slap_build_sync_state_ctrl( op, rs, rs->sr_entry,
851 LDAP_SYNC_ADD, rs->sr_ctrls, 0, 0, NULL );
852 } else if ( rs->sr_type == REP_RESULT && rs->sr_err == LDAP_SUCCESS ) {
853 struct berval cookie;
855 slap_compose_sync_cookie( op, &cookie,
856 &op->ors_filter->f_and->f_ava->aa_value,
857 srs->sr_state.sid, srs->sr_state.rid );
859 /* Is this a regular refresh? */
861 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
863 rs->sr_ctrls[1] = NULL;
864 rs->sr_err = slap_build_sync_done_ctrl( op, rs, rs->sr_ctrls,
865 0, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
868 /* It's RefreshAndPersist, transition to Persist phase */
869 rs->sr_rspoid = LDAP_SYNC_INFO;
870 slap_send_syncinfo( op, rs, rs->sr_nentries ?
871 LDAP_TAG_SYNC_REFRESH_PRESENT : LDAP_TAG_SYNC_REFRESH_DELETE,
872 &cookie, 1, NULL, 0 );
873 /* Flush any queued persist messages */
874 if ( ss->ss_so->s_res ) {
875 syncres *sr, *srnext;
880 ldap_pvt_thread_mutex_lock( &ss->ss_so->s_mutex );
882 for (sr = ss->ss_so->s_res; sr; sr=srnext) {
883 int rc = LDAP_SUCCESS;
886 opc.sndn = sr->s_ndn;
887 opc.suuid = sr->s_uuid;
888 opc.sctxcsn = sr->s_csn;
889 opc.sreference = sr->s_isreference;
892 if ( sr->s_mode != LDAP_SYNC_DELETE ) {
893 op->o_bd->bd_info = (BackendInfo *)on->on_info;
894 rc = be_entry_get_rw( op, &opc.sndn, NULL, NULL, 0, &e );
895 op->o_bd->bd_info = (BackendInfo *)on;
897 if ( rc == LDAP_SUCCESS )
898 syncprov_sendresp( op, &opc, ss->ss_so, e,
902 op->o_bd->bd_info = (BackendInfo *)on->on_info;
903 be_entry_release_r( op, e );
904 op->o_bd->bd_info = (BackendInfo *)on;
908 ss->ss_so->s_res = NULL;
909 ss->ss_so->s_restail = NULL;
912 /* Turn off the refreshing flag */
913 ss->ss_so->s_flags ^= PS_IS_REFRESHING;
915 ldap_pvt_thread_mutex_unlock( &ss->ss_so->s_mutex );
917 /* Detach this Op from frontend control */
918 syncprov_detach_op( op, ss->ss_so );
924 return SLAP_CB_CONTINUE;
928 syncprov_op_search( Operation *op, SlapReply *rs )
930 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
931 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
933 int gotstate = 0, nochange = 0;
939 if ( !(op->o_sync_mode & SLAP_SYNC_REFRESH) ) return SLAP_CB_CONTINUE;
941 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
942 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "illegal value for derefAliases" );
946 srs = op->o_controls[sync_cid];
948 /* If this is a persistent search, set it up right away */
949 if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
959 so.s_flags = PS_IS_REFRESHING;
960 /* syncprov_findbase expects to be called as a callback... */
961 sc.sc_private = &opc;
964 op->o_callback = ≻
965 rs->sr_err = syncprov_findbase( op, &fc );
968 if ( rs->sr_err != LDAP_SUCCESS ) {
969 send_ldap_result( op, rs );
972 sop = ch_malloc( sizeof( syncops ));
974 ldap_pvt_thread_mutex_init( &sop->s_mutex );
975 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
976 sop->s_sid = srs->sr_state.sid;
977 sop->s_rid = srs->sr_state.rid;
978 sop->s_next = si->si_ops;
980 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
983 /* If we have a cookie, handle the PRESENT lookups
985 if ( srs->sr_state.ctxcsn ) {
986 /* Is the CSN in a valid format? */
987 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
988 send_ldap_error( op, rs, LDAP_OTHER, "invalid sync cookie" );
991 /* Is the CSN still present in the database? */
992 if ( syncprov_findcsn( op, FIND_CSN ) != LDAP_SUCCESS ) {
993 /* No, so a reload is required */
994 #if 0 /* the consumer doesn't seem to send this hint */
995 if ( op->o_sync_rhint == 0 ) {
996 send_ldap_error( op, rs, LDAP_SYNC_REFRESH_REQUIRED, "sync cookie is stale" );
1002 /* If just Refreshing and nothing has changed, shortcut it */
1003 if ( bvmatch( srs->sr_state.ctxcsn, &si->si_ctxcsn )) {
1005 if ( !(op->o_sync_mode & SLAP_SYNC_PERSIST) ) {
1006 LDAPControl *ctrls[2];
1010 slap_build_sync_done_ctrl( op, rs, ctrls, 0, 0,
1011 NULL, LDAP_SYNC_REFRESH_DELETES );
1012 rs->sr_ctrls = ctrls;
1013 rs->sr_err = LDAP_SUCCESS;
1014 send_ldap_result( op, rs );
1019 /* If context has changed, check for Present UUIDs */
1020 if ( syncprov_findcsn( op, FIND_PRESENT ) != LDAP_SUCCESS ) {
1021 send_ldap_result( op, rs );
1027 /* If we didn't get a cookie and we don't know our contextcsn, try to
1030 if ( !gotstate && !si->si_gotcsn ) {
1031 struct berval bv = BER_BVC("1"), *old;
1033 old = srs->sr_state.ctxcsn;
1034 srs->sr_state.ctxcsn = &bv;
1035 syncprov_findcsn( op, FIND_CSN );
1036 srs->sr_state.ctxcsn = old;
1039 /* Append CSN range to search filter, save original filter
1040 * for persistent search evaluation
1043 sop->s_filterstr= op->ors_filterstr;
1046 fand = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1047 fand->f_choice = LDAP_FILTER_AND;
1048 fand->f_next = NULL;
1049 fava = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1050 fava->f_choice = LDAP_FILTER_LE;
1051 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
1052 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
1053 ber_dupbv_x( &fava->f_ava->aa_value, &si->si_ctxcsn, op->o_tmpmemctx );
1056 fava->f_next = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1057 fava = fava->f_next;
1058 fava->f_choice = LDAP_FILTER_GE;
1059 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
1060 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
1061 ber_dupbv_x( &fava->f_ava->aa_value, srs->sr_state.ctxcsn, op->o_tmpmemctx );
1063 fava->f_next = op->ors_filter;
1064 op->ors_filter = fand;
1065 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
1068 /* Let our callback add needed info to returned entries */
1069 cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(searchstate), op->o_tmpmemctx);
1070 ss = (searchstate *)(cb+1);
1073 cb->sc_response = syncprov_search_response;
1074 cb->sc_cleanup = syncprov_search_cleanup;
1075 cb->sc_private = ss;
1076 cb->sc_next = op->o_callback;
1077 op->o_callback = cb;
1079 op->o_sync_mode &= SLAP_CONTROL_MASK;
1081 /* If this is a persistent search and no changes were reported during
1082 * the refresh phase, just invoke the response callback to transition
1083 * us into persist phase
1086 rs->sr_err = LDAP_SUCCESS;
1087 rs->sr_nentries = 0;
1088 send_ldap_result( op, rs );
1091 return SLAP_CB_CONTINUE;
1103 slap_overinst *on = (slap_overinst *)be->bd_info;
1104 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1107 if ( strcasecmp( argv[ 0 ], "syncprov-checkpoint" ) == 0 ) {
1109 fprintf( stderr, "%s: line %d: wrong number of arguments in "
1110 "\"syncprov-checkpoint <ops> <minutes>\"\n", fname, lineno );
1113 si->si_chkops = atoi( argv[1] );
1114 si->si_chktime = atoi( argv[2] ) * 60;
1117 return SLAP_CONF_UNKNOWN;
1121 return SLAP_CONF_UNKNOWN;
1129 slap_overinst *on = (slap_overinst *)be->bd_info;
1130 syncprov_info_t *si;
1132 si = ch_calloc(1, sizeof(syncprov_info_t));
1133 on->on_bi.bi_private = si;
1134 ldap_pvt_thread_mutex_init( &si->si_csn_mutex );
1135 ldap_pvt_thread_mutex_init( &si->si_ops_mutex );
1136 si->si_ctxcsn.bv_val = si->si_ctxcsnbuf;
1138 csn_anlist[0].an_desc = slap_schema.si_ad_entryCSN;
1139 csn_anlist[0].an_name = slap_schema.si_ad_entryCSN->ad_cname;
1141 uuid_anlist[0].an_desc = slap_schema.si_ad_entryUUID;
1142 uuid_anlist[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1144 sync_cid = slap_cids.sc_LDAPsync;
1150 syncprov_db_destroy(
1154 slap_overinst *on = (slap_overinst *)be->bd_info;
1155 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1158 ldap_pvt_thread_mutex_destroy( &si->si_ops_mutex );
1159 ldap_pvt_thread_mutex_destroy( &si->si_csn_mutex );
1166 /* This overlay is set up for dynamic loading via moduleload. For static
1167 * configuration, you'll need to arrange for the slap_overinst to be
1168 * initialized and registered by some other function inside slapd.
1171 static slap_overinst syncprov;
1176 syncprov.on_bi.bi_type = "syncprov";
1177 syncprov.on_bi.bi_db_init = syncprov_db_init;
1178 syncprov.on_bi.bi_db_config = syncprov_db_config;
1179 syncprov.on_bi.bi_db_destroy = syncprov_db_destroy;
1181 syncprov.on_bi.bi_op_add = syncprov_op_mod;
1183 syncprov.on_bi.bi_op_compare = syncprov_op_compare;
1185 syncprov.on_bi.bi_op_delete = syncprov_op_mod;
1186 syncprov.on_bi.bi_op_modify = syncprov_op_mod;
1187 syncprov.on_bi.bi_op_modrdn = syncprov_op_mod;
1188 syncprov.on_bi.bi_op_search = syncprov_op_search;
1189 syncprov.on_bi.bi_extended = syncprov_op_extended;
1192 syncprov.on_response = syncprov_response;
1195 return overlay_register( &syncprov );
1198 #if SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC
1200 init_module( int argc, char *argv[] )
1202 return syncprov_init();
1204 #endif /* SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC */
1206 #endif /* defined(SLAPD_OVER_SYNCPROV) */