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 modify request on a particular entry */
29 typedef struct modinst {
30 struct modinst *mi_next;
34 typedef struct modtarget {
35 struct modinst *mt_mods;
36 struct modinst *mt_tail;
38 ldap_pvt_thread_mutex_t mt_mutex;
41 /* A queued result of a persistent search */
42 typedef struct syncres {
43 struct syncres *s_next;
52 /* Record of a persistent search */
53 typedef struct syncops {
54 struct syncops *s_next;
55 struct berval s_base; /* ndn of search base */
56 ID s_eid; /* entryID of search base */
57 Operation *s_op; /* search op */
60 struct berval s_filterstr;
61 int s_flags; /* search status */
62 int s_inuse; /* reference count */
63 struct syncres *s_res;
64 struct syncres *s_restail;
65 ldap_pvt_thread_mutex_t s_mutex;
70 /* A received sync control */
71 typedef struct sync_control {
72 struct sync_cookie sr_state;
76 /* o_sync_mode uses data bits of o_sync */
77 #define o_sync o_ctrlflag[sync_cid]
78 #define o_sync_mode o_ctrlflag[sync_cid]
80 #define SLAP_SYNC_NONE (LDAP_SYNC_NONE<<SLAP_CONTROL_SHIFT)
81 #define SLAP_SYNC_REFRESH (LDAP_SYNC_REFRESH_ONLY<<SLAP_CONTROL_SHIFT)
82 #define SLAP_SYNC_PERSIST (LDAP_SYNC_RESERVED<<SLAP_CONTROL_SHIFT)
83 #define SLAP_SYNC_REFRESH_AND_PERSIST (LDAP_SYNC_REFRESH_AND_PERSIST<<SLAP_CONTROL_SHIFT)
85 #define PS_IS_REFRESHING 0x01
87 /* Record of which searches matched at premodify step */
88 typedef struct syncmatches {
89 struct syncmatches *sm_next;
93 /* The main state for this overlay */
94 typedef struct syncprov_info_t {
96 struct berval si_ctxcsn; /* ldapsync context */
97 int si_gotcsn; /* is our ctxcsn up to date? */
98 Avlnode *si_mods; /* entries being modified */
99 ldap_pvt_thread_mutex_t si_csn_mutex;
100 ldap_pvt_thread_mutex_t si_ops_mutex;
101 ldap_pvt_thread_mutex_t si_mods_mutex;
102 char si_ctxcsnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
105 typedef struct opcookie {
107 syncmatches *smatches;
108 struct berval sdn; /* DN of entry, for deletes */
110 struct berval suuid; /* UUID of entry */
111 struct berval sctxcsn;
112 int sreference; /* Is the entry a reference? */
115 typedef struct fbase_cookie {
116 struct berval *fdn; /* DN of a modified entry, for scope testing */
117 syncops *fss; /* persistent search we're testing against */
118 int fbase; /* if TRUE we found the search base and it's still valid */
119 int fscope; /* if TRUE then fdn is within the psearch scope */
122 static AttributeName csn_anlist[2];
123 static AttributeName uuid_anlist[2];
125 /* Build a LDAPsync intermediate state control */
131 int entry_sync_state,
135 struct berval *cookie)
140 const char *text = NULL;
142 BerElementBuffer berbuf;
143 BerElement *ber = (BerElement *)&berbuf;
145 struct berval entryuuid_bv = BER_BVNULL;
147 ber_init2( ber, 0, LBER_USE_DER );
148 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
150 ctrls[num_ctrls] = slap_sl_malloc ( sizeof ( LDAPControl ), op->o_tmpmemctx );
152 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
153 AttributeDescription *desc = a->a_desc;
154 if ( desc == slap_schema.si_ad_entryUUID ) {
155 entryuuid_bv = a->a_nvals[0];
160 if ( send_cookie && cookie ) {
161 ber_printf( ber, "{eOON}",
162 entry_sync_state, &entryuuid_bv, cookie );
164 ber_printf( ber, "{eON}",
165 entry_sync_state, &entryuuid_bv );
168 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
169 ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
170 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
175 Debug( LDAP_DEBUG_TRACE,
176 "slap_build_sync_ctrl: ber_flatten2 failed\n",
178 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
185 /* Build a LDAPsync final state control */
193 struct berval *cookie,
197 BerElementBuffer berbuf;
198 BerElement *ber = (BerElement *)&berbuf;
200 ber_init2( ber, NULL, LBER_USE_DER );
201 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
203 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
205 ber_printf( ber, "{" );
206 if ( send_cookie && cookie ) {
207 ber_printf( ber, "O", cookie );
209 if ( refreshDeletes == LDAP_SYNC_REFRESH_DELETES ) {
210 ber_printf( ber, "b", refreshDeletes );
212 ber_printf( ber, "N}" );
214 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
215 ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
216 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
221 Debug( LDAP_DEBUG_TRACE,
222 "syncprov_done_ctrl: ber_flatten2 failed\n",
224 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
232 /* Generate state based on session log - not implemented yet */
234 syncprov_state_ctrl_from_slog(
237 struct slog_entry *slog_e,
238 int entry_sync_state,
242 struct berval *cookie)
247 const char *text = NULL;
249 BerElementBuffer berbuf;
250 BerElement *ber = (BerElement *)&berbuf;
252 struct berval entryuuid_bv = BER_BVNULL;
254 ber_init2( ber, NULL, LBER_USE_DER );
255 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
257 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
259 entryuuid_bv = slog_e->sl_uuid;
261 if ( send_cookie && cookie ) {
262 ber_printf( ber, "{eOON}",
263 entry_sync_state, &entryuuid_bv, cookie );
265 ber_printf( ber, "{eON}",
266 entry_sync_state, &entryuuid_bv );
269 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
270 ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
271 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
276 Debug( LDAP_DEBUG_TRACE,
277 "slap_build_sync_ctrl: ber_flatten2 failed\n",
279 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
292 struct berval *cookie,
297 BerElementBuffer berbuf;
298 BerElement *ber = (BerElement *)&berbuf;
299 struct berval rspdata;
303 ber_init2( ber, NULL, LBER_USE_DER );
304 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
308 case LDAP_TAG_SYNC_NEW_COOKIE:
309 ber_printf( ber, "tO", type, cookie );
311 case LDAP_TAG_SYNC_REFRESH_DELETE:
312 case LDAP_TAG_SYNC_REFRESH_PRESENT:
313 ber_printf( ber, "t{", type );
315 ber_printf( ber, "O", cookie );
317 if ( refreshDone == 0 ) {
318 ber_printf( ber, "b", refreshDone );
320 ber_printf( ber, "N}" );
322 case LDAP_TAG_SYNC_ID_SET:
323 ber_printf( ber, "t{", type );
325 ber_printf( ber, "O", cookie );
327 if ( refreshDeletes == 1 ) {
328 ber_printf( ber, "b", refreshDeletes );
330 ber_printf( ber, "[W]", syncUUIDs );
331 ber_printf( ber, "N}" );
334 Debug( LDAP_DEBUG_TRACE,
335 "syncprov_sendinfo: invalid syncinfo type (%d)\n",
341 ret = ber_flatten2( ber, &rspdata, 0 );
344 Debug( LDAP_DEBUG_TRACE,
345 "syncprov_sendinfo: ber_flatten2 failed\n",
347 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
351 rs->sr_rspoid = LDAP_SYNC_INFO;
352 rs->sr_rspdata = &rspdata;
353 send_ldap_intermediate( op, rs );
354 rs->sr_rspdata = NULL;
360 /* Find a modtarget in an AVL tree */
362 sp_avl_cmp( const void *c1, const void *c2 )
364 const modtarget *m1, *m2;
368 rc = m1->mt_op->o_req_ndn.bv_len - m2->mt_op->o_req_ndn.bv_len;
371 return ber_bvcmp( &m1->mt_op->o_req_ndn, &m2->mt_op->o_req_ndn );
374 /* syncprov_findbase:
375 * finds the true DN of the base of a search (with alias dereferencing) and
376 * checks to make sure the base entry doesn't get replaced with a different
377 * entry (e.g., swapping trees via ModDN, or retargeting an alias). If a
378 * change is detected, any persistent search on this base must be terminated /
380 * On the first call, we just save the DN and entryID. On subsequent calls
381 * we compare the DN and entryID with the saved values.
384 findbase_cb( Operation *op, SlapReply *rs )
386 slap_callback *sc = op->o_callback;
388 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
389 fbase_cookie *fc = sc->sc_private;
391 /* If no entryID, we're looking for the first time.
392 * Just store whatever we got.
394 if ( fc->fss->s_eid == NOID ) {
396 fc->fss->s_eid = rs->sr_entry->e_id;
397 ber_dupbv( &fc->fss->s_base, &rs->sr_entry->e_nname );
399 } else if ( rs->sr_entry->e_id == fc->fss->s_eid &&
400 dn_match( &rs->sr_entry->e_nname, &fc->fss->s_base )) {
402 /* OK, the DN is the same and the entryID is the same. Now
403 * see if the fdn resides in the scope.
406 switch ( fc->fss->s_op->ors_scope ) {
407 case LDAP_SCOPE_BASE:
408 fc->fscope = dn_match( fc->fdn, &rs->sr_entry->e_nname );
410 case LDAP_SCOPE_ONELEVEL: {
412 dnParent( fc->fdn, &pdn );
413 fc->fscope = dn_match( &pdn, &rs->sr_entry->e_nname );
415 case LDAP_SCOPE_SUBTREE:
416 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname );
418 #ifdef LDAP_SCOPE_SUBORDINATE
419 case LDAP_SCOPE_SUBORDINATE:
420 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname ) &&
421 !dn_match( fc->fdn, &rs->sr_entry->e_nname );
431 syncprov_findbase( Operation *op, fbase_cookie *fc )
433 opcookie *opc = op->o_callback->sc_private;
434 slap_overinst *on = opc->son;
435 syncprov_info_t *si = on->on_bi.bi_private;
437 slap_callback cb = {0};
439 SlapReply frs = { REP_RESULT };
444 cb.sc_response = findbase_cb;
447 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync mode */
448 fop.o_callback = &cb;
449 fop.o_tag = LDAP_REQ_SEARCH;
450 fop.ors_scope = LDAP_SCOPE_BASE;
451 fop.ors_deref = fc->fss->s_op->ors_deref;
453 fop.ors_tlimit = SLAP_NO_LIMIT;
454 fop.ors_attrs = slap_anlist_no_attrs;
455 fop.ors_attrsonly = 1;
456 fop.ors_filter = fc->fss->s_op->ors_filter;
457 fop.ors_filterstr = fc->fss->s_op->ors_filterstr;
459 fop.o_req_ndn = fc->fss->s_op->o_req_ndn;
461 fop.o_bd->bd_info = on->on_info->oi_orig;
462 rc = fop.o_bd->be_search( &fop, &frs );
463 fop.o_bd->bd_info = (BackendInfo *)on;
465 if ( fc->fbase ) return LDAP_SUCCESS;
467 /* If entryID has changed, then the base of this search has
468 * changed. Invalidate the psearch.
470 return LDAP_NO_SUCH_OBJECT;
474 * This function has three different purposes, but they all use a search
475 * that filters on entryCSN so they're combined here.
476 * 1: when the current contextCSN is unknown (i.e., at server start time)
477 * and a syncrepl search has arrived with a cookie, we search for all entries
478 * with CSN >= the cookie CSN, and store the maximum as our contextCSN. Also,
479 * we expect to find the cookie CSN in the search results, and note if we did
480 * or not. If not, we assume the cookie is stale. (This may be too restrictive,
483 * 2: when the current contextCSN is known and we have a sync cookie, we search
484 * for one entry with CSN <= the cookie CSN. (Used to search for =.) If an
485 * entry is found, the cookie CSN is valid, otherwise it is stale. Case 1 is
486 * considered a special case of case 2, and both are generally called the
489 * 3: during a refresh phase, we search for all entries with CSN <= the cookie
490 * CSN, and generate Present records for them. We always collect this result
491 * in SyncID sets, even if there's only one match.
494 #define FIND_PRESENT 2
496 typedef struct fcsn_cookie {
497 struct berval maxcsn;
502 findcsn_cb( Operation *op, SlapReply *rs )
504 slap_callback *sc = op->o_callback;
506 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
507 /* If the private pointer is set, it points to an fcsn_cookie
508 * and we want to record the maxcsn and match state.
510 if ( sc->sc_private ) {
512 fcsn_cookie *fc = sc->sc_private;
513 sync_control *srs = op->o_controls[sync_cid];
514 Attribute *a = attr_find(rs->sr_entry->e_attrs,
515 slap_schema.si_ad_entryCSN );
516 i = ber_bvcmp( &a->a_vals[0], srs->sr_state.ctxcsn );
517 if ( i == 0 ) fc->gotmatch = 1;
518 i = ber_bvcmp( &a->a_vals[0], &fc->maxcsn );
520 fc->maxcsn.bv_len = a->a_vals[0].bv_len;
521 strcpy(fc->maxcsn.bv_val, a->a_vals[0].bv_val );
524 /* Otherwise, if the private pointer is not set, we just
525 * want to know if any entry matched the filter.
527 sc->sc_private = (void *)1;
533 /* Build a list of entryUUIDs for sending in a SyncID set */
535 typedef struct fpres_cookie {
541 findpres_cb( Operation *op, SlapReply *rs )
543 slap_callback *sc = op->o_callback;
544 fpres_cookie *pc = sc->sc_private;
545 int ret = SLAP_CB_CONTINUE;
547 if ( rs->sr_type == REP_SEARCH ) {
548 ret = slap_build_syncUUID_set( op, &pc->uuids, rs->sr_entry );
552 if ( pc->num == SLAP_SYNCUUID_SET_SIZE ) {
553 ret = syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
555 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
562 } else if ( rs->sr_type == REP_RESULT ) {
565 ret = syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
567 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
577 syncprov_findcsn( Operation *op, int mode )
579 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
580 syncprov_info_t *si = on->on_bi.bi_private;
582 slap_callback cb = {0};
584 SlapReply frs = { REP_RESULT };
585 char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
586 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
589 AttributeAssertion eq;
592 fpres_cookie pcookie;
594 sync_control *srs = op->o_controls[sync_cid];
596 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
601 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync_mode */
604 if ( mode == FIND_CSN ) {
605 if ( !si->si_gotcsn ) {
606 /* If we don't know the current ctxcsn, find it */
607 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
610 if ( !si->si_gotcsn ) {
611 cf.f_choice = LDAP_FILTER_GE;
612 fop.ors_attrsonly = 0;
613 fop.ors_attrs = csn_anlist;
614 fop.ors_slimit = SLAP_NO_LIMIT;
615 cb.sc_private = &fcookie;
616 fcookie.maxcsn.bv_val = cbuf;
617 fcookie.maxcsn.bv_len = 0;
618 fcookie.gotmatch = 0;
619 fbuf.bv_len = sprintf( buf, "(entryCSN>=%s)", srs->sr_state.ctxcsn->bv_val );
622 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
625 cf.f_choice = LDAP_FILTER_LE;
626 fop.ors_attrsonly = 1;
627 fop.ors_attrs = slap_anlist_no_attrs;
629 cb.sc_private = NULL;
630 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
632 cb.sc_response = findcsn_cb;
634 } else if ( mode == FIND_PRESENT ) {
635 cf.f_choice = LDAP_FILTER_LE;
636 fop.ors_attrsonly = 0;
637 fop.ors_attrs = uuid_anlist;
638 fop.ors_slimit = SLAP_NO_LIMIT;
639 /* We want pure entries, not referrals */
640 fop.o_managedsait = SLAP_CONTROL_CRITICAL;
641 cb.sc_private = &pcookie;
642 cb.sc_response = findpres_cb;
644 pcookie.uuids = NULL;
645 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)", srs->sr_state.ctxcsn->bv_val );
648 cf.f_av_desc = slap_schema.si_ad_entryCSN;
649 cf.f_av_value = *srs->sr_state.ctxcsn;
652 fop.o_callback = &cb;
653 fop.ors_tlimit = SLAP_NO_LIMIT;
654 fop.ors_filter = &cf;
655 fop.ors_filterstr = fbuf;
657 fop.o_bd->bd_info = on->on_info->oi_orig;
658 rc = fop.o_bd->be_search( &fop, &frs );
659 fop.o_bd->bd_info = (BackendInfo *)on;
661 if ( mode == FIND_CSN ) {
662 if ( !si->si_gotcsn ) {
663 strcpy(si->si_ctxcsnbuf, fcookie.maxcsn.bv_val);
664 si->si_ctxcsn.bv_len = fcookie.maxcsn.bv_len;
666 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
667 if ( fcookie.gotmatch ) return LDAP_SUCCESS;
670 if ( cb.sc_private ) return LDAP_SUCCESS;
672 } else if ( mode == FIND_PRESENT ) {
676 /* If matching CSN was not found, invalidate the context. */
677 return LDAP_NO_SUCH_OBJECT;
680 /* Queue a persistent search response if still in Refresh stage */
682 syncprov_qresp( opcookie *opc, syncops *so, int mode )
686 sr = ch_malloc(sizeof(syncres) + opc->suuid.bv_len + 1 +
687 opc->sdn.bv_len + 1 + opc->sndn.bv_len + 1 + opc->sctxcsn.bv_len + 1 );
689 sr->s_dn.bv_val = (char *)(sr + 1);
691 sr->s_isreference = opc->sreference;
692 sr->s_ndn.bv_val = lutil_strcopy( sr->s_dn.bv_val, opc->sdn.bv_val );
693 *(sr->s_ndn.bv_val++) = '\0';
694 sr->s_uuid.bv_val = lutil_strcopy( sr->s_ndn.bv_val, opc->sndn.bv_val );
695 *(sr->s_uuid.bv_val++) = '\0';
696 sr->s_csn.bv_val = lutil_strcopy( sr->s_uuid.bv_val, opc->suuid.bv_val );
701 so->s_restail->s_next = sr;
704 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
708 /* Send a persistent search response */
710 syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry *e, int mode, int queue )
712 slap_overinst *on = opc->son;
713 syncprov_info_t *si = on->on_bi.bi_private;
715 SlapReply rs = { REP_SEARCH };
716 LDAPControl *ctrls[2];
717 struct berval cookie;
719 Attribute a_uuid = {0};
720 Operation sop = *so->s_op;
725 sop.o_tmpmemctx = op->o_tmpmemctx;
727 sop.o_controls = op->o_controls;
729 if ( queue && (so->s_flags & PS_IS_REFRESHING) ) {
730 ldap_pvt_thread_mutex_lock( &so->s_mutex );
731 if ( so->s_flags & PS_IS_REFRESHING )
732 return syncprov_qresp( opc, so, mode );
733 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
737 slap_compose_sync_cookie( op, &cookie, &opc->sctxcsn,
738 so->s_sid, so->s_rid );
740 e_uuid.e_attrs = &a_uuid;
741 a_uuid.a_desc = slap_schema.si_ad_entryUUID;
742 a_uuid.a_nvals = &opc->suuid;
743 rs.sr_err = syncprov_state_ctrl( &sop, &rs, &e_uuid,
744 mode, ctrls, 0, 1, &cookie );
750 if ( opc->sreference ) {
751 rs.sr_ref = get_entry_referrals( &sop, e );
752 send_search_reference( &sop, &rs );
753 ber_bvarray_free( rs.sr_ref );
757 case LDAP_SYNC_MODIFY:
758 rs.sr_attrs = sop.ors_attrs;
759 send_search_entry( &sop, &rs );
761 case LDAP_SYNC_DELETE:
762 e_uuid.e_attrs = NULL;
763 e_uuid.e_name = opc->sdn;
764 e_uuid.e_nname = opc->sndn;
765 rs.sr_entry = &e_uuid;
766 if ( opc->sreference ) {
771 send_search_reference( &sop, &rs );
773 send_search_entry( &sop, &rs );
779 free( rs.sr_ctrls[0] );
784 syncprov_free_syncop( syncops *so )
786 syncres *sr, *srnext;
788 ldap_pvt_thread_mutex_lock( &so->s_mutex );
790 if ( so->s_inuse > 0 ) {
791 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
794 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
795 filter_free( so->s_op->ors_filter );
797 ch_free( so->s_base.bv_val );
798 for ( sr=so->s_res; sr; sr=srnext ) {
802 ldap_pvt_thread_mutex_destroy( &so->s_mutex );
807 syncprov_drop_psearch( syncops *so )
809 ldap_pvt_thread_mutex_lock( &so->s_op->o_conn->c_mutex );
810 so->s_op->o_conn->c_n_ops_executing--;
811 so->s_op->o_conn->c_n_ops_completed++;
812 ldap_pvt_thread_mutex_unlock( &so->s_op->o_conn->c_mutex );
813 syncprov_free_syncop( so );
817 syncprov_op_abandon( Operation *op, SlapReply *rs )
819 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
820 syncprov_info_t *si = on->on_bi.bi_private;
821 syncops *so, *soprev;
823 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
824 for ( so=si->si_ops, soprev = (syncops *)&si->si_ops; so;
825 soprev=so, so=so->s_next ) {
826 if ( so->s_op->o_connid == op->o_connid &&
827 so->s_op->o_msgid == op->orn_msgid ) {
828 so->s_op->o_abandon = 1;
829 soprev->s_next = so->s_next;
833 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
835 /* Is this really a Cancel exop? */
836 if ( op->o_tag != LDAP_REQ_ABANDON ) {
837 rs->sr_err = LDAP_CANCELLED;
838 send_ldap_result( so->s_op, rs );
840 syncprov_drop_psearch( so );
842 return SLAP_CB_CONTINUE;
845 /* Find which persistent searches are affected by this operation */
847 syncprov_matchops( Operation *op, opcookie *opc, int saveit )
849 slap_overinst *on = opc->son;
850 syncprov_info_t *si = on->on_bi.bi_private;
853 syncops *ss, *sprev, *snext;
859 fc.fdn = &op->o_req_ndn;
861 if ( op->o_tag == LDAP_REQ_MODRDN && !saveit ) {
863 if ( op->orr_nnewSup ) pdn = *op->orr_nnewSup;
864 else dnParent( fc.fdn, &pdn );
865 build_new_dn( &newdn, &pdn, &op->orr_nnewrdn, op->o_tmpmemctx );
868 if ( op->o_tag != LDAP_REQ_ADD ) {
869 op->o_bd->bd_info = (BackendInfo *)on->on_info;
870 rc = be_entry_get_rw( op, fc.fdn, NULL, NULL, 0, &e );
871 op->o_bd->bd_info = (BackendInfo *)on;
878 ber_dupbv_x( &opc->sdn, &e->e_name, op->o_tmpmemctx );
879 ber_dupbv_x( &opc->sndn, &e->e_nname, op->o_tmpmemctx );
880 opc->sreference = is_entry_referral( e );
882 if ( saveit || op->o_tag == LDAP_REQ_ADD ) {
883 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
885 ber_dupbv_x( &opc->suuid, &a->a_nvals[0], op->o_tmpmemctx );
888 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
889 for (ss = si->si_ops, sprev = (syncops *)&si->si_ops; ss;
890 sprev = ss, ss=snext)
901 /* If the base of the search is missing, signal a refresh */
902 rc = syncprov_findbase( op, &fc );
903 if ( rc != LDAP_SUCCESS ) {
904 SlapReply rs = {REP_RESULT};
905 send_ldap_error( ss->s_op, &rs, LDAP_SYNC_REFRESH_REQUIRED,
906 "search base has changed" );
907 sprev->s_next = snext;
908 syncprov_drop_psearch( ss );
912 /* If we're sending results now, look for this op in old matches */
915 for ( sm=opc->smatches, old=(syncmatches *)&opc->smatches; sm;
916 old=sm, sm=sm->sm_next ) {
917 if ( sm->sm_op == ss ) {
919 old->sm_next = sm->sm_next;
920 op->o_tmpfree( sm, op->o_tmpmemctx );
926 /* check if current o_req_dn is in scope and matches filter */
927 if ( fc.fscope && test_filter( op, e, ss->s_op->ors_filter ) ==
928 LDAP_COMPARE_TRUE ) {
930 sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
931 sm->sm_next = opc->smatches;
936 /* if found send UPDATE else send ADD */
937 syncprov_sendresp( op, opc, ss, e,
938 found ? LDAP_SYNC_MODIFY : LDAP_SYNC_ADD, 1 );
940 } else if ( !saveit && found ) {
942 syncprov_sendresp( op, opc, ss, NULL, LDAP_SYNC_DELETE, 1 );
945 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
946 if ( op->o_tag != LDAP_REQ_ADD ) {
947 op->o_bd->bd_info = (BackendInfo *)on->on_info;
948 be_entry_release_r( op, e );
949 op->o_bd->bd_info = (BackendInfo *)on;
954 syncprov_op_cleanup( Operation *op, SlapReply *rs )
956 slap_callback *cb = op->o_callback;
957 opcookie *opc = cb->sc_private;
958 slap_overinst *on = opc->son;
959 syncprov_info_t *si = on->on_bi.bi_private;
960 syncmatches *sm, *snext;
961 modtarget *mt, mtdummy;
963 for (sm = opc->smatches; sm; sm=snext) {
965 syncprov_free_syncop( sm->sm_op );
966 op->o_tmpfree( sm, op->o_tmpmemctx );
969 /* Remove op from lock table */
971 ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
972 mt = avl_find( si->si_mods, &mtdummy, sp_avl_cmp );
973 ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
975 modinst *mi = mt->mt_mods;
977 /* If there are more, promote the next one */
978 ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
980 mt->mt_mods = mi->mi_next;
981 mt->mt_op = mt->mt_mods->mi_op;
982 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
984 ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
985 avl_delete( &si->si_mods, mt, sp_avl_cmp );
986 ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
987 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
988 ldap_pvt_thread_mutex_destroy( &mt->mt_mutex );
992 op->o_callback = cb->sc_next;
993 op->o_tmpfree(cb, op->o_tmpmemctx);
997 syncprov_op_response( Operation *op, SlapReply *rs )
999 opcookie *opc = op->o_callback->sc_private;
1000 slap_overinst *on = opc->son;
1001 syncprov_info_t *si = on->on_bi.bi_private;
1004 if ( rs->sr_err == LDAP_SUCCESS )
1006 struct berval maxcsn;
1007 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
1009 /* Update our context CSN */
1011 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
1012 slap_get_commit_csn( op, &maxcsn );
1013 if ( maxcsn.bv_val ) {
1014 strcpy( cbuf, maxcsn.bv_val );
1015 if ( ber_bvcmp( &maxcsn, &si->si_ctxcsn ) > 0 ) {
1016 strcpy( si->si_ctxcsnbuf, cbuf );
1017 si->si_ctxcsn.bv_len = maxcsn.bv_len;
1021 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1023 opc->sctxcsn.bv_len = maxcsn.bv_len;
1024 opc->sctxcsn.bv_val = cbuf;
1026 /* Handle any persistent searches */
1030 case LDAP_REQ_MODIFY:
1031 case LDAP_REQ_MODRDN:
1032 case LDAP_REQ_EXTENDED:
1033 syncprov_matchops( op, opc, 0 );
1035 case LDAP_REQ_DELETE:
1036 /* for each match in opc->smatches:
1039 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
1040 for ( sm = opc->smatches; sm; sm=sm->sm_next ) {
1041 if ( sm->sm_op->s_op->o_abandon )
1043 syncprov_sendresp( op, opc, sm->sm_op, NULL,
1044 LDAP_SYNC_DELETE, 1 );
1046 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
1052 return SLAP_CB_CONTINUE;
1055 /* We don't use a subentry to store the context CSN any more.
1056 * We expose the current context CSN as an operational attribute
1057 * of the suffix entry.
1060 syncprov_op_compare( Operation *op, SlapReply *rs )
1062 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1063 syncprov_info_t *si = on->on_bi.bi_private;
1064 int rc = SLAP_CB_CONTINUE;
1066 if ( dn_match( &op->o_req_ndn, op->o_bd->be_nsuffix ) &&
1067 op->oq_compare.rs_ava->aa_desc == slap_schema.si_ad_contextCSN )
1071 struct berval bv[2];
1073 e.e_name = op->o_bd->be_suffix[0];
1074 e.e_nname = op->o_bd->be_nsuffix[0];
1076 bv[1].bv_val = NULL;
1077 bv[0] = si->si_ctxcsn;
1079 a.a_desc = slap_schema.si_ad_contextCSN;
1081 a.a_nvals = a.a_vals;
1083 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
1085 rs->sr_err = access_allowed( op, &e, op->oq_compare.rs_ava->aa_desc,
1086 &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
1087 if ( ! rs->sr_err ) {
1088 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1089 goto return_results;
1092 if ( get_assert( op ) &&
1093 ( test_filter( op, &e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
1095 rs->sr_err = LDAP_ASSERTION_FAILED;
1096 goto return_results;
1100 rs->sr_err = LDAP_COMPARE_FALSE;
1102 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
1103 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1104 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1105 a.a_nvals, &op->oq_compare.rs_ava->aa_value, op->o_tmpmemctx ) == 0 )
1107 rs->sr_err = LDAP_COMPARE_TRUE;
1112 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1114 send_ldap_result( op, rs );
1116 if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
1117 rs->sr_err = LDAP_SUCCESS;
1126 syncprov_op_mod( Operation *op, SlapReply *rs )
1128 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1129 syncprov_info_t *si = on->on_bi.bi_private;
1131 slap_callback *cb = op->o_tmpcalloc(1, sizeof(slap_callback)+
1133 (si->si_ops ? sizeof(modinst) : 0 ),
1135 opcookie *opc = (opcookie *)(cb+1);
1137 cb->sc_response = syncprov_op_response;
1138 cb->sc_cleanup = syncprov_op_cleanup;
1139 cb->sc_private = opc;
1140 cb->sc_next = op->o_callback;
1141 op->o_callback = cb;
1143 /* If there are active persistent searches, lock this operation.
1144 * See seqmod.c for the locking logic on its own.
1147 modtarget *mt, mtdummy;
1150 mi = (modinst *)(opc+1);
1153 /* See if we're already modifying this entry... */
1155 ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
1156 mt = avl_find( si->si_mods, &mtdummy, sp_avl_cmp );
1158 ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
1159 ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
1160 mt->mt_tail->mi_next = mi;
1162 /* wait for this op to get to head of list */
1163 while ( mt->mt_mods != mi ) {
1164 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
1165 ldap_pvt_thread_yield();
1166 ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
1169 /* Record that we're modifying this entry now */
1170 mt = malloc( sizeof(modtarget) );
1173 mt->mt_op = mi->mi_op;
1174 ldap_pvt_thread_mutex_init( &mt->mt_mutex );
1175 avl_insert( &si->si_mods, mt, sp_avl_cmp, avl_dup_error );
1176 ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
1179 if ( op->o_tag != LDAP_REQ_ADD )
1180 syncprov_matchops( op, opc, 1 );
1183 return SLAP_CB_CONTINUE;
1187 syncprov_op_extended( Operation *op, SlapReply *rs )
1189 if ( exop_is_write( op ))
1190 return syncprov_op_mod( op, rs );
1192 return SLAP_CB_CONTINUE;
1195 typedef struct searchstate {
1196 slap_overinst *ss_on;
1201 syncprov_search_cleanup( Operation *op, SlapReply *rs )
1203 searchstate *ss = op->o_callback->sc_private;
1204 if ( rs->sr_ctrls ) {
1205 free( rs->sr_ctrls[0] );
1206 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
1212 syncprov_detach_op( Operation *op, syncops *so )
1219 /* count the search attrs */
1220 for (i=0; op->ors_attrs && op->ors_attrs[i].an_name.bv_val; i++) {
1221 alen += op->ors_attrs[i].an_name.bv_len + 1;
1223 /* Make a new copy of the operation */
1224 size = sizeof(Operation) + sizeof(Opheader) +
1225 (i ? ( (i+1) * sizeof(AttributeName) + alen) : 0) +
1226 op->o_req_dn.bv_len + 1 +
1227 op->o_req_ndn.bv_len + 1 +
1228 op->o_ndn.bv_len + 1 +
1229 so->s_filterstr.bv_len + 1;
1230 op2 = (Operation *)ch_malloc( size );
1232 op2->o_hdr = (Opheader *)(op2+1);
1233 *op2->o_hdr = *op->o_hdr;
1235 op2->ors_attrs = (AttributeName *)(op2->o_hdr + 1);
1236 ptr = (char *)(op2->ors_attrs+i+1);
1237 for (i=0; op->ors_attrs[i].an_name.bv_val; i++) {
1238 op2->ors_attrs[i] = op->ors_attrs[i];
1239 op2->ors_attrs[i].an_name.bv_val = ptr;
1240 ptr = lutil_strcopy( ptr, op->ors_attrs[i].an_name.bv_val ) + 1;
1242 op2->ors_attrs[i].an_name.bv_val = NULL;
1243 op2->ors_attrs[i].an_name.bv_len = 0;
1245 ptr = (char *)(op2->o_hdr + 1);
1247 op2->o_ndn.bv_val = ptr;
1248 ptr = lutil_strcopy(ptr, op->o_ndn.bv_val) + 1;
1249 op2->o_dn = op2->o_ndn;
1250 op2->o_req_dn.bv_val = ptr;
1251 ptr = lutil_strcopy(ptr, op->o_req_dn.bv_val) + 1;
1252 op2->o_req_ndn.bv_val = ptr;
1253 ptr = lutil_strcopy(ptr, op->o_req_ndn.bv_val) + 1;
1254 op2->ors_filterstr.bv_val = ptr;
1255 strcpy( ptr, so->s_filterstr.bv_val );
1256 op2->ors_filterstr.bv_len = so->s_filterstr.bv_len;
1257 op2->ors_filter = str2filter( ptr );
1258 op2->o_controls = NULL;
1259 op2->o_callback = NULL;
1262 /* Increment number of ops so that idletimeout ignores us */
1263 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1264 op->o_conn->c_n_ops_executing++;
1265 op->o_conn->c_n_ops_completed--;
1266 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1270 syncprov_search_response( Operation *op, SlapReply *rs )
1272 searchstate *ss = op->o_callback->sc_private;
1273 slap_overinst *on = ss->ss_on;
1274 syncprov_info_t *si = on->on_bi.bi_private;
1275 sync_control *srs = op->o_controls[sync_cid];
1277 if ( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF ) {
1279 if ( srs->sr_state.ctxcsn ) {
1280 Attribute *a = attr_find( rs->sr_entry->e_attrs,
1281 slap_schema.si_ad_entryCSN );
1282 /* Don't send the ctx entry twice */
1283 if ( bvmatch( &a->a_nvals[0], srs->sr_state.ctxcsn ))
1284 return LDAP_SUCCESS;
1286 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
1288 rs->sr_ctrls[1] = NULL;
1289 rs->sr_err = syncprov_state_ctrl( op, rs, rs->sr_entry,
1290 LDAP_SYNC_ADD, rs->sr_ctrls, 0, 0, NULL );
1291 } else if ( rs->sr_type == REP_RESULT && rs->sr_err == LDAP_SUCCESS ) {
1292 struct berval cookie;
1294 slap_compose_sync_cookie( op, &cookie,
1295 &op->ors_filter->f_and->f_ava->aa_value,
1296 srs->sr_state.sid, srs->sr_state.rid );
1298 /* Is this a regular refresh? */
1300 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
1302 rs->sr_ctrls[1] = NULL;
1303 rs->sr_err = syncprov_done_ctrl( op, rs, rs->sr_ctrls,
1304 0, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1307 /* It's RefreshAndPersist, transition to Persist phase */
1308 syncprov_sendinfo( op, rs, rs->sr_nentries ?
1309 LDAP_TAG_SYNC_REFRESH_PRESENT : LDAP_TAG_SYNC_REFRESH_DELETE,
1310 &cookie, 1, NULL, 0 );
1311 /* Flush any queued persist messages */
1312 if ( ss->ss_so->s_res ) {
1313 syncres *sr, *srnext;
1318 ldap_pvt_thread_mutex_lock( &ss->ss_so->s_mutex );
1320 for (sr = ss->ss_so->s_res; sr; sr=srnext) {
1321 int rc = LDAP_SUCCESS;
1322 srnext = sr->s_next;
1324 opc.sndn = sr->s_ndn;
1325 opc.suuid = sr->s_uuid;
1326 opc.sctxcsn = sr->s_csn;
1327 opc.sreference = sr->s_isreference;
1330 if ( sr->s_mode != LDAP_SYNC_DELETE ) {
1331 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1332 rc = be_entry_get_rw( op, &opc.sndn, NULL, NULL, 0, &e );
1333 op->o_bd->bd_info = (BackendInfo *)on;
1335 if ( rc == LDAP_SUCCESS )
1336 syncprov_sendresp( op, &opc, ss->ss_so, e,
1340 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1341 be_entry_release_r( op, e );
1342 op->o_bd->bd_info = (BackendInfo *)on;
1346 ss->ss_so->s_res = NULL;
1347 ss->ss_so->s_restail = NULL;
1350 /* Turn off the refreshing flag */
1351 ss->ss_so->s_flags ^= PS_IS_REFRESHING;
1353 ldap_pvt_thread_mutex_unlock( &ss->ss_so->s_mutex );
1355 /* Detach this Op from frontend control */
1356 syncprov_detach_op( op, ss->ss_so );
1358 return LDAP_SUCCESS;
1362 return SLAP_CB_CONTINUE;
1366 syncprov_op_search( Operation *op, SlapReply *rs )
1368 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1369 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1371 int gotstate = 0, nochange = 0;
1372 Filter *fand, *fava;
1373 syncops *sop = NULL;
1377 if ( !(op->o_sync_mode & SLAP_SYNC_REFRESH) ) return SLAP_CB_CONTINUE;
1379 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1380 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "illegal value for derefAliases" );
1384 srs = op->o_controls[sync_cid];
1386 /* If this is a persistent search, set it up right away */
1387 if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
1397 so.s_flags = PS_IS_REFRESHING;
1398 /* syncprov_findbase expects to be called as a callback... */
1399 sc.sc_private = &opc;
1401 cb = op->o_callback;
1402 op->o_callback = ≻
1403 rs->sr_err = syncprov_findbase( op, &fc );
1404 op->o_callback = cb;
1406 if ( rs->sr_err != LDAP_SUCCESS ) {
1407 send_ldap_result( op, rs );
1410 sop = ch_malloc( sizeof( syncops ));
1412 ldap_pvt_thread_mutex_init( &sop->s_mutex );
1413 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
1414 sop->s_sid = srs->sr_state.sid;
1415 sop->s_rid = srs->sr_state.rid;
1416 sop->s_next = si->si_ops;
1419 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
1422 /* If we have a cookie, handle the PRESENT lookups
1424 if ( srs->sr_state.ctxcsn ) {
1425 /* Is the CSN in a valid format? */
1426 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
1427 send_ldap_error( op, rs, LDAP_OTHER, "invalid sync cookie" );
1430 /* Is the CSN still present in the database? */
1431 if ( syncprov_findcsn( op, FIND_CSN ) != LDAP_SUCCESS ) {
1432 /* No, so a reload is required */
1433 #if 0 /* the consumer doesn't seem to send this hint */
1434 if ( op->o_sync_rhint == 0 ) {
1435 send_ldap_error( op, rs, LDAP_SYNC_REFRESH_REQUIRED, "sync cookie is stale" );
1441 /* If just Refreshing and nothing has changed, shortcut it */
1442 if ( bvmatch( srs->sr_state.ctxcsn, &si->si_ctxcsn )) {
1444 if ( !(op->o_sync_mode & SLAP_SYNC_PERSIST) ) {
1445 LDAPControl *ctrls[2];
1449 syncprov_done_ctrl( op, rs, ctrls, 0, 0,
1450 NULL, LDAP_SYNC_REFRESH_DELETES );
1451 rs->sr_ctrls = ctrls;
1452 rs->sr_err = LDAP_SUCCESS;
1453 send_ldap_result( op, rs );
1458 /* If context has changed, check for Present UUIDs */
1459 if ( syncprov_findcsn( op, FIND_PRESENT ) != LDAP_SUCCESS ) {
1460 send_ldap_result( op, rs );
1466 /* If we didn't get a cookie and we don't know our contextcsn, try to
1469 if ( !gotstate && !si->si_gotcsn ) {
1470 struct berval bv = BER_BVC("1"), *old;
1472 old = srs->sr_state.ctxcsn;
1473 srs->sr_state.ctxcsn = &bv;
1474 syncprov_findcsn( op, FIND_CSN );
1475 srs->sr_state.ctxcsn = old;
1478 /* Append CSN range to search filter, save original filter
1479 * for persistent search evaluation
1482 sop->s_filterstr= op->ors_filterstr;
1485 fand = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1486 fand->f_choice = LDAP_FILTER_AND;
1487 fand->f_next = NULL;
1488 fava = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1489 fava->f_choice = LDAP_FILTER_LE;
1490 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
1491 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
1492 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
1493 ber_dupbv_x( &fava->f_ava->aa_value, &si->si_ctxcsn, op->o_tmpmemctx );
1494 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1497 fava->f_next = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1498 fava = fava->f_next;
1499 fava->f_choice = LDAP_FILTER_GE;
1500 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
1501 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
1502 ber_dupbv_x( &fava->f_ava->aa_value, srs->sr_state.ctxcsn, op->o_tmpmemctx );
1504 fava->f_next = op->ors_filter;
1505 op->ors_filter = fand;
1506 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
1509 /* Let our callback add needed info to returned entries */
1510 cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(searchstate), op->o_tmpmemctx);
1511 ss = (searchstate *)(cb+1);
1514 cb->sc_response = syncprov_search_response;
1515 cb->sc_cleanup = syncprov_search_cleanup;
1516 cb->sc_private = ss;
1517 cb->sc_next = op->o_callback;
1518 op->o_callback = cb;
1520 op->o_sync_mode &= SLAP_CONTROL_MASK;
1522 /* If this is a persistent search and no changes were reported during
1523 * the refresh phase, just invoke the response callback to transition
1524 * us into persist phase
1527 rs->sr_err = LDAP_SUCCESS;
1528 rs->sr_nentries = 0;
1529 send_ldap_result( op, rs );
1532 return SLAP_CB_CONTINUE;
1536 syncprov_operational(
1540 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1541 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1543 if ( rs->sr_entry &&
1544 dn_match( &rs->sr_entry->e_nname, op->o_bd->be_nsuffix )) {
1548 for ( ap = &rs->sr_operational_attrs; *ap; ap=&(*ap)->a_next ) ;
1550 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
1551 ad_inlist( slap_schema.si_ad_contextCSN, rs->sr_attrs )) {
1553 Attribute *a = ch_malloc( sizeof(Attribute));
1554 a->a_desc = slap_schema.si_ad_contextCSN;
1555 a->a_vals = ch_malloc( 2 * sizeof(struct berval));
1557 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
1558 ber_dupbv( &a->a_vals[0], &si->si_ctxcsn );
1559 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1561 a->a_vals[1].bv_val = NULL;
1562 a->a_nvals = a->a_vals;
1569 return LDAP_SUCCESS;
1581 slap_overinst *on = (slap_overinst *)be->bd_info;
1582 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1585 if ( strcasecmp( argv[ 0 ], "syncprov-checkpoint" ) == 0 ) {
1587 fprintf( stderr, "%s: line %d: wrong number of arguments in "
1588 "\"syncprov-checkpoint <ops> <minutes>\"\n", fname, lineno );
1591 si->si_chkops = atoi( argv[1] );
1592 si->si_chktime = atoi( argv[2] ) * 60;
1595 return SLAP_CONF_UNKNOWN;
1599 return SLAP_CONF_UNKNOWN;
1607 slap_overinst *on = (slap_overinst *)be->bd_info;
1608 syncprov_info_t *si;
1610 si = ch_calloc(1, sizeof(syncprov_info_t));
1611 on->on_bi.bi_private = si;
1612 ldap_pvt_thread_mutex_init( &si->si_csn_mutex );
1613 ldap_pvt_thread_mutex_init( &si->si_ops_mutex );
1614 si->si_ctxcsn.bv_val = si->si_ctxcsnbuf;
1616 csn_anlist[0].an_desc = slap_schema.si_ad_entryCSN;
1617 csn_anlist[0].an_name = slap_schema.si_ad_entryCSN->ad_cname;
1619 uuid_anlist[0].an_desc = slap_schema.si_ad_entryUUID;
1620 uuid_anlist[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1626 syncprov_db_destroy(
1630 slap_overinst *on = (slap_overinst *)be->bd_info;
1631 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1634 ldap_pvt_thread_mutex_destroy( &si->si_ops_mutex );
1635 ldap_pvt_thread_mutex_destroy( &si->si_csn_mutex );
1642 static int syncprov_parseCtrl (
1651 struct berval cookie = BER_BVNULL;
1655 if ( op->o_sync != SLAP_CONTROL_NONE ) {
1656 rs->sr_text = "Sync control specified multiple times";
1657 return LDAP_PROTOCOL_ERROR;
1660 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1661 rs->sr_text = "Sync control specified with pagedResults control";
1662 return LDAP_PROTOCOL_ERROR;
1665 if ( ctrl->ldctl_value.bv_len == 0 ) {
1666 rs->sr_text = "Sync control value is empty (or absent)";
1667 return LDAP_PROTOCOL_ERROR;
1670 /* Parse the control value
1671 * syncRequestValue ::= SEQUENCE {
1676 * refreshAndPersist (3)
1678 * cookie syncCookie OPTIONAL
1682 ber = ber_init( &ctrl->ldctl_value );
1684 rs->sr_text = "internal error";
1688 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1689 rs->sr_text = "Sync control : mode decoding error";
1690 return LDAP_PROTOCOL_ERROR;
1694 case LDAP_SYNC_REFRESH_ONLY:
1695 mode = SLAP_SYNC_REFRESH;
1697 case LDAP_SYNC_REFRESH_AND_PERSIST:
1698 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1701 rs->sr_text = "Sync control : unknown update mode";
1702 return LDAP_PROTOCOL_ERROR;
1705 tag = ber_peek_tag( ber, &len );
1707 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1708 if (( ber_scanf( ber, /*{*/ "o", &cookie )) == LBER_ERROR ) {
1709 rs->sr_text = "Sync control : cookie decoding error";
1710 return LDAP_PROTOCOL_ERROR;
1713 if ( tag == LDAP_TAG_RELOAD_HINT ) {
1714 if (( ber_scanf( ber, /*{*/ "b", &rhint )) == LBER_ERROR ) {
1715 rs->sr_text = "Sync control : rhint decoding error";
1716 return LDAP_PROTOCOL_ERROR;
1719 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1720 rs->sr_text = "Sync control : decoding error";
1721 return LDAP_PROTOCOL_ERROR;
1723 sr = op->o_tmpcalloc( 1, sizeof(struct sync_control), op->o_tmpmemctx );
1724 sr->sr_rhint = rhint;
1725 if (!BER_BVISNULL(&cookie)) {
1726 ber_bvarray_add( &sr->sr_state.octet_str, &cookie );
1727 slap_parse_sync_cookie( &sr->sr_state );
1730 op->o_controls[sync_cid] = sr;
1732 (void) ber_free( ber, 1 );
1734 op->o_sync = ctrl->ldctl_iscritical
1735 ? SLAP_CONTROL_CRITICAL
1736 : SLAP_CONTROL_NONCRITICAL;
1738 op->o_sync_mode |= mode; /* o_sync_mode shares o_sync */
1740 return LDAP_SUCCESS;
1743 /* This overlay is set up for dynamic loading via moduleload. For static
1744 * configuration, you'll need to arrange for the slap_overinst to be
1745 * initialized and registered by some other function inside slapd.
1748 static slap_overinst syncprov;
1755 rc = register_supported_control( LDAP_CONTROL_SYNC,
1756 SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
1757 syncprov_parseCtrl, &sync_cid );
1758 if ( rc != LDAP_SUCCESS ) {
1759 fprintf( stderr, "Failed to register control %d\n", rc );
1763 syncprov.on_bi.bi_type = "syncprov";
1764 syncprov.on_bi.bi_db_init = syncprov_db_init;
1765 syncprov.on_bi.bi_db_config = syncprov_db_config;
1766 syncprov.on_bi.bi_db_destroy = syncprov_db_destroy;
1768 syncprov.on_bi.bi_op_abandon = syncprov_op_abandon;
1769 syncprov.on_bi.bi_op_cancel = syncprov_op_abandon;
1771 syncprov.on_bi.bi_op_add = syncprov_op_mod;
1772 syncprov.on_bi.bi_op_compare = syncprov_op_compare;
1773 syncprov.on_bi.bi_op_delete = syncprov_op_mod;
1774 syncprov.on_bi.bi_op_modify = syncprov_op_mod;
1775 syncprov.on_bi.bi_op_modrdn = syncprov_op_mod;
1776 syncprov.on_bi.bi_op_search = syncprov_op_search;
1777 syncprov.on_bi.bi_extended = syncprov_op_extended;
1778 syncprov.on_bi.bi_operational = syncprov_operational;
1781 syncprov.on_response = syncprov_response;
1784 return overlay_register( &syncprov );
1787 #if SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC
1789 init_module( int argc, char *argv[] )
1791 return syncprov_init();
1793 #endif /* SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC */
1795 #endif /* defined(SLAPD_OVER_SYNCPROV) */