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 /* A received sync control */
57 typedef struct sync_control {
58 struct sync_cookie sr_state;
62 /* o_sync_mode uses data bits of o_sync */
63 #define o_sync o_ctrlflag[sync_cid]
64 #define o_sync_mode o_ctrlflag[sync_cid]
66 #define SLAP_SYNC_NONE (LDAP_SYNC_NONE<<SLAP_CONTROL_SHIFT)
67 #define SLAP_SYNC_REFRESH (LDAP_SYNC_REFRESH_ONLY<<SLAP_CONTROL_SHIFT)
68 #define SLAP_SYNC_PERSIST (LDAP_SYNC_RESERVED<<SLAP_CONTROL_SHIFT)
69 #define SLAP_SYNC_REFRESH_AND_PERSIST (LDAP_SYNC_REFRESH_AND_PERSIST<<SLAP_CONTROL_SHIFT)
71 #define PS_IS_REFRESHING 0x01
73 /* Record of which searches matched at premodify step */
74 typedef struct syncmatches {
75 struct syncmatches *sm_next;
79 typedef struct syncprov_info_t {
81 struct berval si_ctxcsn; /* ldapsync context */
82 int si_gotcsn; /* is our ctxcsn up to date? */
83 ldap_pvt_thread_mutex_t si_csn_mutex;
84 ldap_pvt_thread_mutex_t si_ops_mutex;
85 char si_ctxcsnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
88 typedef struct opcookie {
90 syncmatches *smatches;
91 struct berval sdn; /* DN of entry, for deletes */
93 struct berval suuid; /* UUID of entry */
94 struct berval sctxcsn;
95 int sreference; /* Is the entry a reference? */
98 typedef struct fbase_cookie {
99 struct berval *fdn; /* DN of a modified entry, for scope testing */
100 syncops *fss; /* persistent search we're testing against */
101 int fbase; /* if TRUE we found the search base and it's still valid */
102 int fscope; /* if TRUE then fdn is within the psearch scope */
105 static AttributeName csn_anlist[2];
106 static AttributeName uuid_anlist[2];
113 int entry_sync_state,
117 struct berval *cookie)
122 const char *text = NULL;
124 BerElementBuffer berbuf;
125 BerElement *ber = (BerElement *)&berbuf;
127 struct berval entryuuid_bv = BER_BVNULL;
129 ber_init2( ber, 0, LBER_USE_DER );
130 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
132 ctrls[num_ctrls] = slap_sl_malloc ( sizeof ( LDAPControl ), op->o_tmpmemctx );
134 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
135 AttributeDescription *desc = a->a_desc;
136 if ( desc == slap_schema.si_ad_entryUUID ) {
137 entryuuid_bv = a->a_nvals[0];
142 if ( send_cookie && cookie ) {
143 ber_printf( ber, "{eOON}",
144 entry_sync_state, &entryuuid_bv, cookie );
146 ber_printf( ber, "{eON}",
147 entry_sync_state, &entryuuid_bv );
150 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
151 ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
152 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
157 Debug( LDAP_DEBUG_TRACE,
158 "slap_build_sync_ctrl: ber_flatten2 failed\n",
160 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
174 struct berval *cookie,
178 BerElementBuffer berbuf;
179 BerElement *ber = (BerElement *)&berbuf;
181 ber_init2( ber, NULL, LBER_USE_DER );
182 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
184 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
186 ber_printf( ber, "{" );
187 if ( send_cookie && cookie ) {
188 ber_printf( ber, "O", cookie );
190 if ( refreshDeletes == LDAP_SYNC_REFRESH_DELETES ) {
191 ber_printf( ber, "b", refreshDeletes );
193 ber_printf( ber, "N}" );
195 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
196 ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
197 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
202 Debug( LDAP_DEBUG_TRACE,
203 "syncprov_done_ctrl: ber_flatten2 failed\n",
205 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
214 syncprov_state_ctrl_from_slog(
217 struct slog_entry *slog_e,
218 int entry_sync_state,
222 struct berval *cookie)
227 const char *text = NULL;
229 BerElementBuffer berbuf;
230 BerElement *ber = (BerElement *)&berbuf;
232 struct berval entryuuid_bv = BER_BVNULL;
234 ber_init2( ber, NULL, LBER_USE_DER );
235 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
237 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
239 entryuuid_bv = slog_e->sl_uuid;
241 if ( send_cookie && cookie ) {
242 ber_printf( ber, "{eOON}",
243 entry_sync_state, &entryuuid_bv, cookie );
245 ber_printf( ber, "{eON}",
246 entry_sync_state, &entryuuid_bv );
249 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
250 ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
251 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
256 Debug( LDAP_DEBUG_TRACE,
257 "slap_build_sync_ctrl: ber_flatten2 failed\n",
259 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
271 struct berval *cookie,
276 BerElementBuffer berbuf;
277 BerElement *ber = (BerElement *)&berbuf;
278 struct berval rspdata;
282 ber_init2( ber, NULL, LBER_USE_DER );
283 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
287 case LDAP_TAG_SYNC_NEW_COOKIE:
288 ber_printf( ber, "tO", type, cookie );
290 case LDAP_TAG_SYNC_REFRESH_DELETE:
291 case LDAP_TAG_SYNC_REFRESH_PRESENT:
292 ber_printf( ber, "t{", type );
294 ber_printf( ber, "O", cookie );
296 if ( refreshDone == 0 ) {
297 ber_printf( ber, "b", refreshDone );
299 ber_printf( ber, "N}" );
301 case LDAP_TAG_SYNC_ID_SET:
302 ber_printf( ber, "t{", type );
304 ber_printf( ber, "O", cookie );
306 if ( refreshDeletes == 1 ) {
307 ber_printf( ber, "b", refreshDeletes );
309 ber_printf( ber, "[W]", syncUUIDs );
310 ber_printf( ber, "N}" );
313 Debug( LDAP_DEBUG_TRACE,
314 "syncprov_sendinfo: invalid syncinfo type (%d)\n",
320 ret = ber_flatten2( ber, &rspdata, 0 );
323 Debug( LDAP_DEBUG_TRACE,
324 "syncprov_sendinfo: ber_flatten2 failed\n",
326 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
330 rs->sr_rspdata = &rspdata;
331 send_ldap_intermediate( op, rs );
332 rs->sr_rspdata = NULL;
337 /* syncprov_findbase:
338 * finds the true DN of the base of a search (with alias dereferencing) and
339 * checks to make sure the base entry doesn't get replaced with a different
340 * entry (e.g., swapping trees via ModDN, or retargeting an alias). If a
341 * change is detected, any persistent search on this base must be terminated /
343 * On the first call, we just save the DN and entryID. On subsequent calls
344 * we compare the DN and entryID with the saved values.
347 findbase_cb( Operation *op, SlapReply *rs )
349 slap_callback *sc = op->o_callback;
351 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
352 fbase_cookie *fc = sc->sc_private;
354 /* If no entryID, we're looking for the first time.
355 * Just store whatever we got.
357 if ( fc->fss->s_eid == NOID ) {
359 fc->fss->s_eid = rs->sr_entry->e_id;
360 ber_dupbv( &fc->fss->s_base, &rs->sr_entry->e_nname );
362 } else if ( rs->sr_entry->e_id == fc->fss->s_eid &&
363 dn_match( &rs->sr_entry->e_nname, &fc->fss->s_base )) {
365 /* OK, the DN is the same and the entryID is the same. Now
366 * see if the fdn resides in the scope.
369 switch ( fc->fss->s_op->ors_scope ) {
370 case LDAP_SCOPE_BASE:
371 fc->fscope = dn_match( fc->fdn, &rs->sr_entry->e_nname );
373 case LDAP_SCOPE_ONELEVEL: {
375 dnParent( fc->fdn, &pdn );
376 fc->fscope = dn_match( &pdn, &rs->sr_entry->e_nname );
378 case LDAP_SCOPE_SUBTREE:
379 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname );
381 #ifdef LDAP_SCOPE_SUBORDINATE
382 case LDAP_SCOPE_SUBORDINATE:
383 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname ) &&
384 !dn_match( fc->fdn, &rs->sr_entry->e_nname );
394 syncprov_findbase( Operation *op, fbase_cookie *fc )
396 opcookie *opc = op->o_callback->sc_private;
397 slap_overinst *on = opc->son;
398 syncprov_info_t *si = on->on_bi.bi_private;
400 slap_callback cb = {0};
402 SlapReply frs = { REP_RESULT };
407 cb.sc_response = findbase_cb;
410 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync mode */
411 fop.o_callback = &cb;
412 fop.o_tag = LDAP_REQ_SEARCH;
413 fop.ors_scope = LDAP_SCOPE_BASE;
414 fop.ors_deref = fc->fss->s_op->ors_deref;
416 fop.ors_tlimit = SLAP_NO_LIMIT;
417 fop.ors_attrs = slap_anlist_no_attrs;
418 fop.ors_attrsonly = 1;
419 fop.ors_filter = fc->fss->s_op->ors_filter;
420 fop.ors_filterstr = fc->fss->s_op->ors_filterstr;
422 fop.o_req_ndn = fc->fss->s_op->o_req_ndn;
424 fop.o_bd->bd_info = on->on_info->oi_orig;
425 rc = fop.o_bd->be_search( &fop, &frs );
426 fop.o_bd->bd_info = (BackendInfo *)on;
428 if ( fc->fbase ) return LDAP_SUCCESS;
430 /* If entryID has changed, then the base of this search has
431 * changed. Invalidate the psearch.
433 return LDAP_NO_SUCH_OBJECT;
437 * This function has three different purposes, but they all use a search
438 * that filters on entryCSN so they're combined here.
439 * 1: when the current contextCSN is unknown (i.e., at server start time)
440 * and a syncrepl search has arrived with a cookie, we search for all entries
441 * with CSN >= the cookie CSN, and store the maximum as our contextCSN. Also,
442 * we expect to find the cookie CSN in the search results, and note if we did
443 * or not. If not, we assume the cookie is stale. (This may be too restrictive,
446 * 2: when the current contextCSN is known and we have a sync cookie, we search
447 * for one entry with CSN <= the cookie CSN. (Used to search for =.) If an
448 * entry is found, the cookie CSN is valid, otherwise it is stale. Case 1 is
449 * considered a special case of case 2, and both are generally called the
452 * 3: during a refresh phase, we search for all entries with CSN <= the cookie
453 * CSN, and generate Present records for them. We always collect this result
454 * in SyncID sets, even if there's only one match.
457 #define FIND_PRESENT 2
459 typedef struct fcsn_cookie {
460 struct berval maxcsn;
465 findcsn_cb( Operation *op, SlapReply *rs )
467 slap_callback *sc = op->o_callback;
469 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
470 /* If the private pointer is set, it points to an fcsn_cookie
471 * and we want to record the maxcsn and match state.
473 if ( sc->sc_private ) {
475 fcsn_cookie *fc = sc->sc_private;
476 sync_control *srs = op->o_controls[sync_cid];
477 Attribute *a = attr_find(rs->sr_entry->e_attrs,
478 slap_schema.si_ad_entryCSN );
479 i = ber_bvcmp( &a->a_vals[0], srs->sr_state.ctxcsn );
480 if ( i == 0 ) fc->gotmatch = 1;
481 i = ber_bvcmp( &a->a_vals[0], &fc->maxcsn );
483 fc->maxcsn.bv_len = a->a_vals[0].bv_len;
484 strcpy(fc->maxcsn.bv_val, a->a_vals[0].bv_val );
487 /* Otherwise, if the private pointer is not set, we just
488 * want to know if any entry matched the filter.
490 sc->sc_private = (void *)1;
496 /* Build a list of entryUUIDs for sending in a SyncID set */
498 typedef struct fpres_cookie {
504 findpres_cb( Operation *op, SlapReply *rs )
506 slap_callback *sc = op->o_callback;
507 fpres_cookie *pc = sc->sc_private;
508 int ret = SLAP_CB_CONTINUE;
510 if ( rs->sr_type == REP_SEARCH ) {
511 ret = slap_build_syncUUID_set( op, &pc->uuids, rs->sr_entry );
515 if ( pc->num == SLAP_SYNCUUID_SET_SIZE ) {
516 rs->sr_rspoid = LDAP_SYNC_INFO;
517 ret = syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
519 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
526 } else if ( rs->sr_type == REP_RESULT ) {
529 rs->sr_rspoid = LDAP_SYNC_INFO;
530 ret = syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
532 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
542 syncprov_findcsn( Operation *op, int mode )
544 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
545 syncprov_info_t *si = on->on_bi.bi_private;
547 slap_callback cb = {0};
549 SlapReply frs = { REP_RESULT };
550 char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
551 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
554 AttributeAssertion eq;
557 fpres_cookie pcookie;
559 sync_control *srs = op->o_controls[sync_cid];
561 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
566 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync_mode */
569 if ( mode == FIND_CSN ) {
570 if ( !si->si_gotcsn ) {
571 /* If we don't know the current ctxcsn, find it */
572 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
575 if ( !si->si_gotcsn ) {
576 cf.f_choice = LDAP_FILTER_GE;
577 fop.ors_attrsonly = 0;
578 fop.ors_attrs = csn_anlist;
579 fop.ors_slimit = SLAP_NO_LIMIT;
580 cb.sc_private = &fcookie;
581 fcookie.maxcsn.bv_val = cbuf;
582 fcookie.maxcsn.bv_len = 0;
583 fcookie.gotmatch = 0;
584 fbuf.bv_len = sprintf( buf, "(entryCSN>=%s)", srs->sr_state.ctxcsn->bv_val );
587 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
590 cf.f_choice = LDAP_FILTER_LE;
591 fop.ors_attrsonly = 1;
592 fop.ors_attrs = slap_anlist_no_attrs;
594 cb.sc_private = NULL;
595 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
597 cb.sc_response = findcsn_cb;
599 } else if ( mode == FIND_PRESENT ) {
600 cf.f_choice = LDAP_FILTER_LE;
601 fop.ors_attrsonly = 0;
602 fop.ors_attrs = uuid_anlist;
603 fop.ors_slimit = SLAP_NO_LIMIT;
604 /* We want pure entries, not referrals */
605 fop.o_managedsait = SLAP_CONTROL_CRITICAL;
606 cb.sc_private = &pcookie;
607 cb.sc_response = findpres_cb;
609 pcookie.uuids = NULL;
610 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
613 cf.f_av_desc = slap_schema.si_ad_entryCSN;
614 cf.f_av_value = *srs->sr_state.ctxcsn;
617 fop.o_callback = &cb;
618 fop.ors_tlimit = SLAP_NO_LIMIT;
619 fop.ors_filter = &cf;
620 fop.ors_filterstr = fbuf;
622 fop.o_bd->bd_info = on->on_info->oi_orig;
623 rc = fop.o_bd->be_search( &fop, &frs );
624 fop.o_bd->bd_info = (BackendInfo *)on;
626 if ( mode == FIND_CSN ) {
627 if ( !si->si_gotcsn ) {
628 strcpy(si->si_ctxcsnbuf, fcookie.maxcsn.bv_val);
629 si->si_ctxcsn.bv_len = fcookie.maxcsn.bv_len;
631 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
632 if ( fcookie.gotmatch ) return LDAP_SUCCESS;
635 if ( cb.sc_private ) return LDAP_SUCCESS;
637 } else if ( mode == FIND_PRESENT ) {
641 /* If matching CSN was not found, invalidate the context. */
642 return LDAP_NO_SUCH_OBJECT;
645 /* Queue a persistent search response if still in Refresh stage */
647 syncprov_qresp( opcookie *opc, syncops *so, int mode )
651 sr = ch_malloc(sizeof(syncres) + opc->suuid.bv_len + 1 +
652 opc->sdn.bv_len + 1 + opc->sndn.bv_len + 1 + opc->sctxcsn.bv_len + 1 );
654 sr->s_dn.bv_val = (char *)(sr + 1);
656 sr->s_isreference = opc->sreference;
657 sr->s_ndn.bv_val = lutil_strcopy( sr->s_dn.bv_val, opc->sdn.bv_val );
658 *(sr->s_ndn.bv_val++) = '\0';
659 sr->s_uuid.bv_val = lutil_strcopy( sr->s_ndn.bv_val, opc->sndn.bv_val );
660 *(sr->s_uuid.bv_val++) = '\0';
661 sr->s_csn.bv_val = lutil_strcopy( sr->s_uuid.bv_val, opc->suuid.bv_val );
666 so->s_restail->s_next = sr;
669 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
673 /* Send a persistent search response */
675 syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry *e, int mode, int queue )
677 slap_overinst *on = opc->son;
678 syncprov_info_t *si = on->on_bi.bi_private;
680 SlapReply rs = { REP_SEARCH };
681 LDAPControl *ctrls[2];
682 struct berval cookie;
684 Attribute a_uuid = {0};
685 Operation sop = *so->s_op;
690 sop.o_tmpmemctx = op->o_tmpmemctx;
692 sop.o_controls = op->o_controls;
694 if ( queue && (so->s_flags & PS_IS_REFRESHING) ) {
695 ldap_pvt_thread_mutex_lock( &so->s_mutex );
696 if ( so->s_flags & PS_IS_REFRESHING )
697 return syncprov_qresp( opc, so, mode );
698 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
702 slap_compose_sync_cookie( op, &cookie, &opc->sctxcsn,
703 so->s_sid, so->s_rid );
705 e_uuid.e_attrs = &a_uuid;
706 a_uuid.a_desc = slap_schema.si_ad_entryUUID;
707 a_uuid.a_nvals = &opc->suuid;
708 rs.sr_err = syncprov_state_ctrl( &sop, &rs, &e_uuid,
709 mode, ctrls, 0, 1, &cookie );
715 if ( opc->sreference ) {
716 rs.sr_ref = get_entry_referrals( &sop, e );
717 send_search_reference( &sop, &rs );
718 ber_bvarray_free( rs.sr_ref );
722 case LDAP_SYNC_MODIFY:
723 rs.sr_attrs = sop.ors_attrs;
724 send_search_entry( &sop, &rs );
726 case LDAP_SYNC_DELETE:
727 e_uuid.e_attrs = NULL;
728 e_uuid.e_name = opc->sdn;
729 e_uuid.e_nname = opc->sndn;
730 rs.sr_entry = &e_uuid;
731 if ( opc->sreference ) {
736 send_search_reference( &sop, &rs );
738 send_search_entry( &sop, &rs );
744 free( rs.sr_ctrls[0] );
749 syncprov_matchops( Operation *op, opcookie *opc, int saveit )
751 slap_overinst *on = opc->son;
752 syncprov_info_t *si = on->on_bi.bi_private;
761 fc.fdn = &op->o_req_ndn;
763 if ( op->o_tag == LDAP_REQ_MODRDN && !saveit ) {
765 if ( op->orr_nnewSup ) pdn = *op->orr_nnewSup;
766 else dnParent( fc.fdn, &pdn );
767 build_new_dn( &newdn, &pdn, &op->orr_nnewrdn, op->o_tmpmemctx );
770 if ( op->o_tag != LDAP_REQ_ADD ) {
771 op->o_bd->bd_info = (BackendInfo *)on->on_info;
772 rc = be_entry_get_rw( op, fc.fdn, NULL, NULL, 0, &e );
773 op->o_bd->bd_info = (BackendInfo *)on;
780 ber_dupbv_x( &opc->sdn, &e->e_name, op->o_tmpmemctx );
781 ber_dupbv_x( &opc->sndn, &e->e_nname, op->o_tmpmemctx );
782 opc->sreference = is_entry_referral( e );
784 if ( saveit || op->o_tag == LDAP_REQ_ADD ) {
785 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
787 ber_dupbv_x( &opc->suuid, &a->a_nvals[0], op->o_tmpmemctx );
790 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
791 for (ss = si->si_ops; ss; ss=ss->s_next)
800 rc = syncprov_findbase( op, &fc );
801 if ( rc != LDAP_SUCCESS ) continue;
803 /* If we're sending results now, look for this op in old matches */
806 for ( sm=opc->smatches, old=(syncmatches *)&opc->smatches; sm;
807 old=sm, sm=sm->sm_next ) {
808 if ( sm->sm_op == ss ) {
810 old->sm_next = sm->sm_next;
811 op->o_tmpfree( sm, op->o_tmpmemctx );
817 /* check if current o_req_dn is in scope and matches filter */
818 if ( fc.fscope && test_filter( op, e, ss->s_op->ors_filter ) ==
819 LDAP_COMPARE_TRUE ) {
821 sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
822 sm->sm_next = opc->smatches;
826 /* if found send UPDATE else send ADD */
827 syncprov_sendresp( op, opc, ss, e,
828 found ? LDAP_SYNC_MODIFY : LDAP_SYNC_ADD, 1 );
830 } else if ( !saveit && found ) {
832 syncprov_sendresp( op, opc, ss, NULL, LDAP_SYNC_DELETE, 1 );
835 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
836 if ( op->o_tag != LDAP_REQ_ADD ) {
837 op->o_bd->bd_info = (BackendInfo *)on->on_info;
838 be_entry_release_r( op, e );
839 op->o_bd->bd_info = (BackendInfo *)on;
844 syncprov_op_cleanup( Operation *op, SlapReply *rs )
846 slap_callback *cb = op->o_callback;
847 opcookie *opc = cb->sc_private;
848 syncmatches *sm, *snext;
850 for (sm = opc->smatches; sm; sm=snext) {
852 op->o_tmpfree( sm, op->o_tmpmemctx );
854 op->o_callback = cb->sc_next;
855 op->o_tmpfree(cb, op->o_tmpmemctx);
859 syncprov_op_response( Operation *op, SlapReply *rs )
861 opcookie *opc = op->o_callback->sc_private;
862 slap_overinst *on = opc->son;
863 syncprov_info_t *si = on->on_bi.bi_private;
866 if ( rs->sr_err == LDAP_SUCCESS )
868 struct berval maxcsn;
869 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
872 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
873 slap_get_commit_csn( op, &maxcsn );
874 if ( maxcsn.bv_val ) {
875 strcpy( cbuf, maxcsn.bv_val );
876 if ( ber_bvcmp( &maxcsn, &si->si_ctxcsn ) > 0 ) {
877 strcpy( si->si_ctxcsnbuf, cbuf );
878 si->si_ctxcsn.bv_len = maxcsn.bv_len;
882 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
884 opc->sctxcsn.bv_len = maxcsn.bv_len;
885 opc->sctxcsn.bv_val = cbuf;
890 case LDAP_REQ_MODIFY:
891 case LDAP_REQ_MODRDN:
892 case LDAP_REQ_EXTENDED:
893 syncprov_matchops( op, opc, 0 );
895 case LDAP_REQ_DELETE:
896 /* for each match in opc->smatches:
899 for ( sm = opc->smatches; sm; sm=sm->sm_next ) {
900 syncprov_sendresp( op, opc, sm->sm_op, NULL,
901 LDAP_SYNC_DELETE, 1 );
908 return SLAP_CB_CONTINUE;
912 syncprov_free_syncop( syncops *so )
914 syncres *sr, *srnext;
916 filter_free( so->s_op->ors_filter );
918 ch_free( so->s_base.bv_val );
919 for ( sr=so->s_res; sr; sr=srnext ) {
923 ldap_pvt_thread_mutex_destroy( &so->s_mutex );
928 syncprov_op_abandon( Operation *op, SlapReply *rs )
930 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
931 syncprov_info_t *si = on->on_bi.bi_private;
932 syncops *so, *soprev;
934 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
935 for ( so=si->si_ops, soprev = (syncops *)&si->si_ops; so;
936 soprev=so, so=so->s_next ) {
937 if ( so->s_op->o_connid == op->o_connid &&
938 so->s_op->o_msgid == op->orn_msgid ) {
939 soprev->s_next = so->s_next;
943 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
945 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
946 op->o_conn->c_n_ops_executing--;
947 op->o_conn->c_n_ops_completed++;
948 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
949 /* Is this really a Cancel exop? */
950 if ( op->o_tag != LDAP_REQ_ABANDON ) {
951 rs->sr_err = LDAP_CANCELLED;
952 send_ldap_result( so->s_op, rs );
954 syncprov_free_syncop( so );
956 return SLAP_CB_CONTINUE;
961 syncprov_op_compare( Operation *op, SlapReply *rs )
963 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
964 syncprov_info_t *si = on->on_bi.bi_private;
965 int rc = SLAP_CB_CONTINUE;
967 if ( dn_match( &op->o_req_ndn, &si->si_e->e_nname ) )
971 ldap_pvt_thread_mutex_lock( &si->si_e_mutex );
973 if ( get_assert( op ) &&
974 ( test_filter( op, si->si_e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
976 rs->sr_err = LDAP_ASSERTION_FAILED;
980 rs->sr_err = access_allowed( op, si->si_e, op->oq_compare.rs_ava->aa_desc,
981 &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
982 if ( ! rs->sr_err ) {
983 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
987 rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
989 for ( a = attr_find( si->si_e->e_attrs, op->oq_compare.rs_ava->aa_desc );
991 a = attr_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
993 rs->sr_err = LDAP_COMPARE_FALSE;
995 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
996 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
997 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
998 a->a_nvals, &op->oq_compare.rs_ava->aa_value, op->o_tmpmemctx ) == 0 )
1000 rs->sr_err = LDAP_COMPARE_TRUE;
1007 ldap_pvt_thread_mutex_unlock( &si->si_e_mutex );
1009 send_ldap_result( op, rs );
1011 if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
1012 rs->sr_err = LDAP_SUCCESS;
1017 return SLAP_CB_CONTINUE;
1022 syncprov_op_mod( Operation *op, SlapReply *rs )
1024 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1025 syncprov_info_t *si = on->on_bi.bi_private;
1027 slap_callback *cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(opcookie), op->o_tmpmemctx);
1028 opcookie *opc = (opcookie *)(cb+1);
1030 cb->sc_response = syncprov_op_response;
1031 cb->sc_cleanup = syncprov_op_cleanup;
1032 cb->sc_private = opc;
1033 cb->sc_next = op->o_callback;
1034 op->o_callback = cb;
1036 if ( si->si_ops && op->o_tag != LDAP_REQ_ADD )
1037 syncprov_matchops( op, opc, 1 );
1039 return SLAP_CB_CONTINUE;
1043 syncprov_op_extended( Operation *op, SlapReply *rs )
1045 if ( exop_is_write( op ))
1046 return syncprov_op_mod( op, rs );
1048 return SLAP_CB_CONTINUE;
1051 typedef struct searchstate {
1052 slap_overinst *ss_on;
1057 syncprov_search_cleanup( Operation *op, SlapReply *rs )
1059 searchstate *ss = op->o_callback->sc_private;
1060 if ( rs->sr_ctrls ) {
1061 free( rs->sr_ctrls[0] );
1062 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
1068 syncprov_detach_op( Operation *op, syncops *so )
1075 /* count the search attrs */
1076 for (i=0; op->ors_attrs && op->ors_attrs[i].an_name.bv_val; i++) {
1077 alen += op->ors_attrs[i].an_name.bv_len + 1;
1079 /* Make a new copy of the operation */
1080 size = sizeof(Operation) + sizeof(Opheader) +
1081 (i ? ( (i+1) * sizeof(AttributeName) + alen) : 0) +
1082 op->o_req_dn.bv_len + 1 +
1083 op->o_req_ndn.bv_len + 1 +
1084 op->o_ndn.bv_len + 1 +
1085 so->s_filterstr.bv_len + 1;
1086 op2 = (Operation *)ch_malloc( size );
1088 op2->o_hdr = (Opheader *)(op2+1);
1089 *op2->o_hdr = *op->o_hdr;
1091 op2->ors_attrs = (AttributeName *)(op2->o_hdr + 1);
1092 ptr = (char *)(op2->ors_attrs+i+1);
1093 for (i=0; op->ors_attrs[i].an_name.bv_val; i++) {
1094 op2->ors_attrs[i] = op->ors_attrs[i];
1095 op2->ors_attrs[i].an_name.bv_val = ptr;
1096 ptr = lutil_strcopy( ptr, op->ors_attrs[i].an_name.bv_val ) + 1;
1098 op2->ors_attrs[i].an_name.bv_val = NULL;
1099 op2->ors_attrs[i].an_name.bv_len = 0;
1101 ptr = (char *)(op2->o_hdr + 1);
1103 op2->o_ndn.bv_val = ptr;
1104 ptr = lutil_strcopy(ptr, op->o_ndn.bv_val) + 1;
1105 op2->o_dn = op2->o_ndn;
1106 op2->o_req_dn.bv_val = ptr;
1107 ptr = lutil_strcopy(ptr, op->o_req_dn.bv_val) + 1;
1108 op2->o_req_ndn.bv_val = ptr;
1109 ptr = lutil_strcopy(ptr, op->o_req_ndn.bv_val) + 1;
1110 op2->ors_filterstr.bv_val = ptr;
1111 strcpy( ptr, so->s_filterstr.bv_val );
1112 op2->ors_filterstr.bv_len = so->s_filterstr.bv_len;
1113 op2->ors_filter = str2filter( ptr );
1114 op2->o_controls = NULL;
1115 op2->o_callback = NULL;
1118 /* Increment number of ops so that idletimeout ignores us */
1119 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1120 op->o_conn->c_n_ops_executing++;
1121 op->o_conn->c_n_ops_completed--;
1122 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1126 syncprov_search_response( Operation *op, SlapReply *rs )
1128 searchstate *ss = op->o_callback->sc_private;
1129 slap_overinst *on = ss->ss_on;
1130 syncprov_info_t *si = on->on_bi.bi_private;
1131 sync_control *srs = op->o_controls[sync_cid];
1133 if ( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF ) {
1135 if ( srs->sr_state.ctxcsn ) {
1136 Attribute *a = attr_find( rs->sr_entry->e_attrs,
1137 slap_schema.si_ad_entryCSN );
1138 /* Don't send the ctx entry twice */
1139 if ( bvmatch( &a->a_nvals[0], srs->sr_state.ctxcsn ))
1140 return LDAP_SUCCESS;
1142 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
1144 rs->sr_ctrls[1] = NULL;
1145 rs->sr_err = syncprov_state_ctrl( op, rs, rs->sr_entry,
1146 LDAP_SYNC_ADD, rs->sr_ctrls, 0, 0, NULL );
1147 } else if ( rs->sr_type == REP_RESULT && rs->sr_err == LDAP_SUCCESS ) {
1148 struct berval cookie;
1150 slap_compose_sync_cookie( op, &cookie,
1151 &op->ors_filter->f_and->f_ava->aa_value,
1152 srs->sr_state.sid, srs->sr_state.rid );
1154 /* Is this a regular refresh? */
1156 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
1158 rs->sr_ctrls[1] = NULL;
1159 rs->sr_err = syncprov_done_ctrl( op, rs, rs->sr_ctrls,
1160 0, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1163 /* It's RefreshAndPersist, transition to Persist phase */
1164 rs->sr_rspoid = LDAP_SYNC_INFO;
1165 syncprov_sendinfo( op, rs, rs->sr_nentries ?
1166 LDAP_TAG_SYNC_REFRESH_PRESENT : LDAP_TAG_SYNC_REFRESH_DELETE,
1167 &cookie, 1, NULL, 0 );
1168 /* Flush any queued persist messages */
1169 if ( ss->ss_so->s_res ) {
1170 syncres *sr, *srnext;
1175 ldap_pvt_thread_mutex_lock( &ss->ss_so->s_mutex );
1177 for (sr = ss->ss_so->s_res; sr; sr=srnext) {
1178 int rc = LDAP_SUCCESS;
1179 srnext = sr->s_next;
1181 opc.sndn = sr->s_ndn;
1182 opc.suuid = sr->s_uuid;
1183 opc.sctxcsn = sr->s_csn;
1184 opc.sreference = sr->s_isreference;
1187 if ( sr->s_mode != LDAP_SYNC_DELETE ) {
1188 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1189 rc = be_entry_get_rw( op, &opc.sndn, NULL, NULL, 0, &e );
1190 op->o_bd->bd_info = (BackendInfo *)on;
1192 if ( rc == LDAP_SUCCESS )
1193 syncprov_sendresp( op, &opc, ss->ss_so, e,
1197 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1198 be_entry_release_r( op, e );
1199 op->o_bd->bd_info = (BackendInfo *)on;
1203 ss->ss_so->s_res = NULL;
1204 ss->ss_so->s_restail = NULL;
1207 /* Turn off the refreshing flag */
1208 ss->ss_so->s_flags ^= PS_IS_REFRESHING;
1210 ldap_pvt_thread_mutex_unlock( &ss->ss_so->s_mutex );
1212 /* Detach this Op from frontend control */
1213 syncprov_detach_op( op, ss->ss_so );
1215 return LDAP_SUCCESS;
1219 return SLAP_CB_CONTINUE;
1223 syncprov_op_search( Operation *op, SlapReply *rs )
1225 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1226 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1228 int gotstate = 0, nochange = 0;
1229 Filter *fand, *fava;
1230 syncops *sop = NULL;
1234 if ( !(op->o_sync_mode & SLAP_SYNC_REFRESH) ) return SLAP_CB_CONTINUE;
1236 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1237 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "illegal value for derefAliases" );
1241 srs = op->o_controls[sync_cid];
1243 /* If this is a persistent search, set it up right away */
1244 if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
1254 so.s_flags = PS_IS_REFRESHING;
1255 /* syncprov_findbase expects to be called as a callback... */
1256 sc.sc_private = &opc;
1258 cb = op->o_callback;
1259 op->o_callback = ≻
1260 rs->sr_err = syncprov_findbase( op, &fc );
1261 op->o_callback = cb;
1263 if ( rs->sr_err != LDAP_SUCCESS ) {
1264 send_ldap_result( op, rs );
1267 sop = ch_malloc( sizeof( syncops ));
1269 ldap_pvt_thread_mutex_init( &sop->s_mutex );
1270 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
1271 sop->s_sid = srs->sr_state.sid;
1272 sop->s_rid = srs->sr_state.rid;
1273 sop->s_next = si->si_ops;
1275 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
1278 /* If we have a cookie, handle the PRESENT lookups
1280 if ( srs->sr_state.ctxcsn ) {
1281 /* Is the CSN in a valid format? */
1282 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
1283 send_ldap_error( op, rs, LDAP_OTHER, "invalid sync cookie" );
1286 /* Is the CSN still present in the database? */
1287 if ( syncprov_findcsn( op, FIND_CSN ) != LDAP_SUCCESS ) {
1288 /* No, so a reload is required */
1289 #if 0 /* the consumer doesn't seem to send this hint */
1290 if ( op->o_sync_rhint == 0 ) {
1291 send_ldap_error( op, rs, LDAP_SYNC_REFRESH_REQUIRED, "sync cookie is stale" );
1297 /* If just Refreshing and nothing has changed, shortcut it */
1298 if ( bvmatch( srs->sr_state.ctxcsn, &si->si_ctxcsn )) {
1300 if ( !(op->o_sync_mode & SLAP_SYNC_PERSIST) ) {
1301 LDAPControl *ctrls[2];
1305 syncprov_done_ctrl( op, rs, ctrls, 0, 0,
1306 NULL, LDAP_SYNC_REFRESH_DELETES );
1307 rs->sr_ctrls = ctrls;
1308 rs->sr_err = LDAP_SUCCESS;
1309 send_ldap_result( op, rs );
1314 /* If context has changed, check for Present UUIDs */
1315 if ( syncprov_findcsn( op, FIND_PRESENT ) != LDAP_SUCCESS ) {
1316 send_ldap_result( op, rs );
1322 /* If we didn't get a cookie and we don't know our contextcsn, try to
1325 if ( !gotstate && !si->si_gotcsn ) {
1326 struct berval bv = BER_BVC("1"), *old;
1328 old = srs->sr_state.ctxcsn;
1329 srs->sr_state.ctxcsn = &bv;
1330 syncprov_findcsn( op, FIND_CSN );
1331 srs->sr_state.ctxcsn = old;
1334 /* Append CSN range to search filter, save original filter
1335 * for persistent search evaluation
1338 sop->s_filterstr= op->ors_filterstr;
1341 fand = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1342 fand->f_choice = LDAP_FILTER_AND;
1343 fand->f_next = NULL;
1344 fava = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1345 fava->f_choice = LDAP_FILTER_LE;
1346 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
1347 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
1348 ber_dupbv_x( &fava->f_ava->aa_value, &si->si_ctxcsn, op->o_tmpmemctx );
1351 fava->f_next = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1352 fava = fava->f_next;
1353 fava->f_choice = LDAP_FILTER_GE;
1354 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
1355 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
1356 ber_dupbv_x( &fava->f_ava->aa_value, srs->sr_state.ctxcsn, op->o_tmpmemctx );
1358 fava->f_next = op->ors_filter;
1359 op->ors_filter = fand;
1360 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
1363 /* Let our callback add needed info to returned entries */
1364 cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(searchstate), op->o_tmpmemctx);
1365 ss = (searchstate *)(cb+1);
1368 cb->sc_response = syncprov_search_response;
1369 cb->sc_cleanup = syncprov_search_cleanup;
1370 cb->sc_private = ss;
1371 cb->sc_next = op->o_callback;
1372 op->o_callback = cb;
1374 op->o_sync_mode &= SLAP_CONTROL_MASK;
1376 /* If this is a persistent search and no changes were reported during
1377 * the refresh phase, just invoke the response callback to transition
1378 * us into persist phase
1381 rs->sr_err = LDAP_SUCCESS;
1382 rs->sr_nentries = 0;
1383 send_ldap_result( op, rs );
1386 return SLAP_CB_CONTINUE;
1398 slap_overinst *on = (slap_overinst *)be->bd_info;
1399 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1402 if ( strcasecmp( argv[ 0 ], "syncprov-checkpoint" ) == 0 ) {
1404 fprintf( stderr, "%s: line %d: wrong number of arguments in "
1405 "\"syncprov-checkpoint <ops> <minutes>\"\n", fname, lineno );
1408 si->si_chkops = atoi( argv[1] );
1409 si->si_chktime = atoi( argv[2] ) * 60;
1412 return SLAP_CONF_UNKNOWN;
1416 return SLAP_CONF_UNKNOWN;
1424 slap_overinst *on = (slap_overinst *)be->bd_info;
1425 syncprov_info_t *si;
1427 si = ch_calloc(1, sizeof(syncprov_info_t));
1428 on->on_bi.bi_private = si;
1429 ldap_pvt_thread_mutex_init( &si->si_csn_mutex );
1430 ldap_pvt_thread_mutex_init( &si->si_ops_mutex );
1431 si->si_ctxcsn.bv_val = si->si_ctxcsnbuf;
1433 csn_anlist[0].an_desc = slap_schema.si_ad_entryCSN;
1434 csn_anlist[0].an_name = slap_schema.si_ad_entryCSN->ad_cname;
1436 uuid_anlist[0].an_desc = slap_schema.si_ad_entryUUID;
1437 uuid_anlist[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1443 syncprov_db_destroy(
1447 slap_overinst *on = (slap_overinst *)be->bd_info;
1448 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1451 ldap_pvt_thread_mutex_destroy( &si->si_ops_mutex );
1452 ldap_pvt_thread_mutex_destroy( &si->si_csn_mutex );
1459 static int syncprov_parseCtrl (
1468 struct berval cookie = BER_BVNULL;
1472 if ( op->o_sync != SLAP_CONTROL_NONE ) {
1473 rs->sr_text = "Sync control specified multiple times";
1474 return LDAP_PROTOCOL_ERROR;
1477 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1478 rs->sr_text = "Sync control specified with pagedResults control";
1479 return LDAP_PROTOCOL_ERROR;
1482 if ( ctrl->ldctl_value.bv_len == 0 ) {
1483 rs->sr_text = "Sync control value is empty (or absent)";
1484 return LDAP_PROTOCOL_ERROR;
1487 /* Parse the control value
1488 * syncRequestValue ::= SEQUENCE {
1493 * refreshAndPersist (3)
1495 * cookie syncCookie OPTIONAL
1499 ber = ber_init( &ctrl->ldctl_value );
1501 rs->sr_text = "internal error";
1505 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1506 rs->sr_text = "Sync control : mode decoding error";
1507 return LDAP_PROTOCOL_ERROR;
1511 case LDAP_SYNC_REFRESH_ONLY:
1512 mode = SLAP_SYNC_REFRESH;
1514 case LDAP_SYNC_REFRESH_AND_PERSIST:
1515 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1518 rs->sr_text = "Sync control : unknown update mode";
1519 return LDAP_PROTOCOL_ERROR;
1522 tag = ber_peek_tag( ber, &len );
1524 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1525 if (( ber_scanf( ber, /*{*/ "o", &cookie )) == LBER_ERROR ) {
1526 rs->sr_text = "Sync control : cookie decoding error";
1527 return LDAP_PROTOCOL_ERROR;
1530 if ( tag == LDAP_TAG_RELOAD_HINT ) {
1531 if (( ber_scanf( ber, /*{*/ "b", &rhint )) == LBER_ERROR ) {
1532 rs->sr_text = "Sync control : rhint decoding error";
1533 return LDAP_PROTOCOL_ERROR;
1536 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1537 rs->sr_text = "Sync control : decoding error";
1538 return LDAP_PROTOCOL_ERROR;
1540 sr = op->o_tmpcalloc( 1, sizeof(struct sync_control), op->o_tmpmemctx );
1541 sr->sr_rhint = rhint;
1542 if (!BER_BVISNULL(&cookie)) {
1543 ber_bvarray_add( &sr->sr_state.octet_str, &cookie );
1544 slap_parse_sync_cookie( &sr->sr_state );
1547 op->o_controls[sync_cid] = sr;
1549 (void) ber_free( ber, 1 );
1551 op->o_sync = ctrl->ldctl_iscritical
1552 ? SLAP_CONTROL_CRITICAL
1553 : SLAP_CONTROL_NONCRITICAL;
1555 op->o_sync_mode |= mode; /* o_sync_mode shares o_sync */
1557 return LDAP_SUCCESS;
1560 /* This overlay is set up for dynamic loading via moduleload. For static
1561 * configuration, you'll need to arrange for the slap_overinst to be
1562 * initialized and registered by some other function inside slapd.
1565 static slap_overinst syncprov;
1572 rc = register_supported_control( LDAP_CONTROL_SYNC,
1573 SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
1574 syncprov_parseCtrl, &sync_cid );
1575 if ( rc != LDAP_SUCCESS ) {
1576 fprintf( stderr, "Failed to register control %d\n", rc );
1580 syncprov.on_bi.bi_type = "syncprov";
1581 syncprov.on_bi.bi_db_init = syncprov_db_init;
1582 syncprov.on_bi.bi_db_config = syncprov_db_config;
1583 syncprov.on_bi.bi_db_destroy = syncprov_db_destroy;
1585 syncprov.on_bi.bi_op_abandon = syncprov_op_abandon;
1586 syncprov.on_bi.bi_op_cancel = syncprov_op_abandon;
1588 syncprov.on_bi.bi_op_add = syncprov_op_mod;
1590 syncprov.on_bi.bi_op_compare = syncprov_op_compare;
1592 syncprov.on_bi.bi_op_delete = syncprov_op_mod;
1593 syncprov.on_bi.bi_op_modify = syncprov_op_mod;
1594 syncprov.on_bi.bi_op_modrdn = syncprov_op_mod;
1595 syncprov.on_bi.bi_op_search = syncprov_op_search;
1596 syncprov.on_bi.bi_extended = syncprov_op_extended;
1599 syncprov.on_response = syncprov_response;
1602 return overlay_register( &syncprov );
1605 #if SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC
1607 init_module( int argc, char *argv[] )
1609 return syncprov_init();
1611 #endif /* SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC */
1613 #endif /* defined(SLAPD_OVER_SYNCPROV) */