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_chkops; /* checkpointing info */
99 int si_numops; /* number of ops since last checkpoint */
100 time_t si_chklast; /* time of last checkpoint */
101 Avlnode *si_mods; /* entries being modified */
102 ldap_pvt_thread_mutex_t si_csn_mutex;
103 ldap_pvt_thread_mutex_t si_ops_mutex;
104 ldap_pvt_thread_mutex_t si_mods_mutex;
105 char si_ctxcsnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
108 typedef struct opcookie {
110 syncmatches *smatches;
111 struct berval sdn; /* DN of entry, for deletes */
113 struct berval suuid; /* UUID of entry */
114 struct berval sctxcsn;
115 int sreference; /* Is the entry a reference? */
118 typedef struct fbase_cookie {
119 struct berval *fdn; /* DN of a modified entry, for scope testing */
120 syncops *fss; /* persistent search we're testing against */
121 int fbase; /* if TRUE we found the search base and it's still valid */
122 int fscope; /* if TRUE then fdn is within the psearch scope */
125 static AttributeName csn_anlist[2];
126 static AttributeName uuid_anlist[2];
128 /* Build a LDAPsync intermediate state control */
134 int entry_sync_state,
138 struct berval *cookie)
143 const char *text = NULL;
145 BerElementBuffer berbuf;
146 BerElement *ber = (BerElement *)&berbuf;
148 struct berval entryuuid_bv = BER_BVNULL;
150 ber_init2( ber, 0, LBER_USE_DER );
151 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
153 ctrls[num_ctrls] = op->o_tmpalloc( sizeof ( LDAPControl ), op->o_tmpmemctx );
155 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
156 AttributeDescription *desc = a->a_desc;
157 if ( desc == slap_schema.si_ad_entryUUID ) {
158 entryuuid_bv = a->a_nvals[0];
163 if ( send_cookie && cookie ) {
164 ber_printf( ber, "{eOON}",
165 entry_sync_state, &entryuuid_bv, cookie );
167 ber_printf( ber, "{eON}",
168 entry_sync_state, &entryuuid_bv );
171 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
172 ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
173 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
178 Debug( LDAP_DEBUG_TRACE,
179 "slap_build_sync_ctrl: ber_flatten2 failed\n",
181 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
188 /* Build a LDAPsync final state control */
196 struct berval *cookie,
200 BerElementBuffer berbuf;
201 BerElement *ber = (BerElement *)&berbuf;
203 ber_init2( ber, NULL, LBER_USE_DER );
204 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
206 ctrls[num_ctrls] = op->o_tmpalloc( sizeof ( LDAPControl ), op->o_tmpmemctx );
208 ber_printf( ber, "{" );
209 if ( send_cookie && cookie ) {
210 ber_printf( ber, "O", cookie );
212 if ( refreshDeletes == LDAP_SYNC_REFRESH_DELETES ) {
213 ber_printf( ber, "b", refreshDeletes );
215 ber_printf( ber, "N}" );
217 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
218 ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
219 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
224 Debug( LDAP_DEBUG_TRACE,
225 "syncprov_done_ctrl: ber_flatten2 failed\n",
227 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
235 /* Generate state based on session log - not implemented yet */
237 syncprov_state_ctrl_from_slog(
240 struct slog_entry *slog_e,
241 int entry_sync_state,
245 struct berval *cookie)
250 const char *text = NULL;
252 BerElementBuffer berbuf;
253 BerElement *ber = (BerElement *)&berbuf;
255 struct berval entryuuid_bv = BER_BVNULL;
257 ber_init2( ber, NULL, LBER_USE_DER );
258 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
260 ctrls[num_ctrls] = ch_malloc ( sizeof ( LDAPControl ) );
262 entryuuid_bv = slog_e->sl_uuid;
264 if ( send_cookie && cookie ) {
265 ber_printf( ber, "{eOON}",
266 entry_sync_state, &entryuuid_bv, cookie );
268 ber_printf( ber, "{eON}",
269 entry_sync_state, &entryuuid_bv );
272 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
273 ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
274 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
279 Debug( LDAP_DEBUG_TRACE,
280 "slap_build_sync_ctrl: ber_flatten2 failed\n",
282 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
295 struct berval *cookie,
300 BerElementBuffer berbuf;
301 BerElement *ber = (BerElement *)&berbuf;
302 struct berval rspdata;
306 ber_init2( ber, NULL, LBER_USE_DER );
307 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
311 case LDAP_TAG_SYNC_NEW_COOKIE:
312 ber_printf( ber, "tO", type, cookie );
314 case LDAP_TAG_SYNC_REFRESH_DELETE:
315 case LDAP_TAG_SYNC_REFRESH_PRESENT:
316 ber_printf( ber, "t{", type );
318 ber_printf( ber, "O", cookie );
320 if ( refreshDone == 0 ) {
321 ber_printf( ber, "b", refreshDone );
323 ber_printf( ber, "N}" );
325 case LDAP_TAG_SYNC_ID_SET:
326 ber_printf( ber, "t{", type );
328 ber_printf( ber, "O", cookie );
330 if ( refreshDeletes == 1 ) {
331 ber_printf( ber, "b", refreshDeletes );
333 ber_printf( ber, "[W]", syncUUIDs );
334 ber_printf( ber, "N}" );
337 Debug( LDAP_DEBUG_TRACE,
338 "syncprov_sendinfo: invalid syncinfo type (%d)\n",
344 ret = ber_flatten2( ber, &rspdata, 0 );
347 Debug( LDAP_DEBUG_TRACE,
348 "syncprov_sendinfo: ber_flatten2 failed\n",
350 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
354 rs->sr_rspoid = LDAP_SYNC_INFO;
355 rs->sr_rspdata = &rspdata;
356 send_ldap_intermediate( op, rs );
357 rs->sr_rspdata = NULL;
363 /* Find a modtarget in an AVL tree */
365 sp_avl_cmp( const void *c1, const void *c2 )
367 const modtarget *m1, *m2;
371 rc = m1->mt_op->o_req_ndn.bv_len - m2->mt_op->o_req_ndn.bv_len;
374 return ber_bvcmp( &m1->mt_op->o_req_ndn, &m2->mt_op->o_req_ndn );
377 /* syncprov_findbase:
378 * finds the true DN of the base of a search (with alias dereferencing) and
379 * checks to make sure the base entry doesn't get replaced with a different
380 * entry (e.g., swapping trees via ModDN, or retargeting an alias). If a
381 * change is detected, any persistent search on this base must be terminated /
383 * On the first call, we just save the DN and entryID. On subsequent calls
384 * we compare the DN and entryID with the saved values.
387 findbase_cb( Operation *op, SlapReply *rs )
389 slap_callback *sc = op->o_callback;
391 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
392 fbase_cookie *fc = sc->sc_private;
394 /* If no entryID, we're looking for the first time.
395 * Just store whatever we got.
397 if ( fc->fss->s_eid == NOID ) {
399 fc->fss->s_eid = rs->sr_entry->e_id;
400 ber_dupbv( &fc->fss->s_base, &rs->sr_entry->e_nname );
402 } else if ( rs->sr_entry->e_id == fc->fss->s_eid &&
403 dn_match( &rs->sr_entry->e_nname, &fc->fss->s_base )) {
405 /* OK, the DN is the same and the entryID is the same. Now
406 * see if the fdn resides in the scope.
409 switch ( fc->fss->s_op->ors_scope ) {
410 case LDAP_SCOPE_BASE:
411 fc->fscope = dn_match( fc->fdn, &rs->sr_entry->e_nname );
413 case LDAP_SCOPE_ONELEVEL: {
415 dnParent( fc->fdn, &pdn );
416 fc->fscope = dn_match( &pdn, &rs->sr_entry->e_nname );
418 case LDAP_SCOPE_SUBTREE:
419 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname );
421 #ifdef LDAP_SCOPE_SUBORDINATE
422 case LDAP_SCOPE_SUBORDINATE:
423 fc->fscope = dnIsSuffix( fc->fdn, &rs->sr_entry->e_nname ) &&
424 !dn_match( fc->fdn, &rs->sr_entry->e_nname );
434 syncprov_findbase( Operation *op, fbase_cookie *fc )
436 opcookie *opc = op->o_callback->sc_private;
437 slap_overinst *on = opc->son;
438 syncprov_info_t *si = on->on_bi.bi_private;
440 slap_callback cb = {0};
442 SlapReply frs = { REP_RESULT };
447 cb.sc_response = findbase_cb;
450 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync mode */
451 fop.o_callback = &cb;
452 fop.o_tag = LDAP_REQ_SEARCH;
453 fop.ors_scope = LDAP_SCOPE_BASE;
454 fop.ors_deref = fc->fss->s_op->ors_deref;
456 fop.ors_tlimit = SLAP_NO_LIMIT;
457 fop.ors_attrs = slap_anlist_no_attrs;
458 fop.ors_attrsonly = 1;
459 fop.ors_filter = fc->fss->s_op->ors_filter;
460 fop.ors_filterstr = fc->fss->s_op->ors_filterstr;
462 fop.o_req_ndn = fc->fss->s_op->o_req_ndn;
464 fop.o_bd->bd_info = on->on_info->oi_orig;
465 rc = fop.o_bd->be_search( &fop, &frs );
466 fop.o_bd->bd_info = (BackendInfo *)on;
468 if ( fc->fbase ) return LDAP_SUCCESS;
470 /* If entryID has changed, then the base of this search has
471 * changed. Invalidate the psearch.
473 return LDAP_NO_SUCH_OBJECT;
477 * This function has two different purposes, but they both use a search
478 * that filters on entryCSN so they're combined here.
479 * 1: when the current contextCSN is known and we have a sync cookie, we search
480 * for one entry with CSN <= the cookie CSN. (Used to search for =.) If an
481 * entry is found, the cookie CSN is valid, otherwise it is stale.
483 * 2: during a refresh phase, we search for all entries with CSN <= the cookie
484 * CSN, and generate Present records for them. We always collect this result
485 * in SyncID sets, even if there's only one match.
488 #define FIND_PRESENT 2
491 findcsn_cb( Operation *op, SlapReply *rs )
493 slap_callback *sc = op->o_callback;
495 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
496 sc->sc_private = (void *)1;
501 /* Build a list of entryUUIDs for sending in a SyncID set */
503 typedef struct fpres_cookie {
509 findpres_cb( Operation *op, SlapReply *rs )
511 slap_callback *sc = op->o_callback;
512 fpres_cookie *pc = sc->sc_private;
513 int ret = SLAP_CB_CONTINUE;
515 if ( rs->sr_type == REP_SEARCH ) {
516 ret = slap_build_syncUUID_set( op, &pc->uuids, rs->sr_entry );
520 if ( pc->num == SLAP_SYNCUUID_SET_SIZE ) {
521 ret = syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
523 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
530 } else if ( rs->sr_type == REP_RESULT ) {
533 ret = syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
535 ber_bvarray_free_x( pc->uuids, op->o_tmpmemctx );
545 syncprov_findcsn( Operation *op, int mode )
547 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
548 syncprov_info_t *si = on->on_bi.bi_private;
550 slap_callback cb = {0};
552 SlapReply frs = { REP_RESULT };
553 char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
554 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
557 AttributeAssertion eq;
559 fpres_cookie pcookie;
561 sync_control *srs = op->o_controls[sync_cid];
563 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
568 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync_mode */
571 if ( mode == FIND_CSN ) {
572 fop.ors_attrsonly = 1;
573 fop.ors_attrs = slap_anlist_no_attrs;
575 cb.sc_private = NULL;
576 cb.sc_response = findcsn_cb;
578 } else if ( mode == FIND_PRESENT ) {
579 fop.ors_attrsonly = 0;
580 fop.ors_attrs = uuid_anlist;
581 fop.ors_slimit = SLAP_NO_LIMIT;
582 /* We want pure entries, not referrals */
583 fop.o_managedsait = SLAP_CONTROL_CRITICAL;
584 cb.sc_private = &pcookie;
585 cb.sc_response = findpres_cb;
587 pcookie.uuids = NULL;
589 cf.f_choice = LDAP_FILTER_LE;
591 cf.f_av_desc = slap_schema.si_ad_entryCSN;
592 cf.f_av_value = *srs->sr_state.ctxcsn;
594 fbuf.bv_len = sprintf( buf, "(entryCSN<=%s)",
595 srs->sr_state.ctxcsn->bv_val );
597 fop.o_callback = &cb;
598 fop.ors_tlimit = SLAP_NO_LIMIT;
599 fop.ors_filter = &cf;
600 fop.ors_filterstr = fbuf;
602 fop.o_bd->bd_info = on->on_info->oi_orig;
603 rc = fop.o_bd->be_search( &fop, &frs );
604 fop.o_bd->bd_info = (BackendInfo *)on;
606 if ( mode == FIND_CSN ) {
607 if ( cb.sc_private ) return LDAP_SUCCESS;
608 } else if ( mode == FIND_PRESENT ) {
612 /* If matching CSN was not found, invalidate the context. */
613 return LDAP_NO_SUCH_OBJECT;
616 /* Queue a persistent search response if still in Refresh stage */
618 syncprov_qresp( opcookie *opc, syncops *so, int mode )
622 sr = ch_malloc(sizeof(syncres) + opc->suuid.bv_len + 1 +
623 opc->sdn.bv_len + 1 + opc->sndn.bv_len + 1 + opc->sctxcsn.bv_len + 1 );
625 sr->s_dn.bv_val = (char *)(sr + 1);
627 sr->s_isreference = opc->sreference;
628 sr->s_ndn.bv_val = lutil_strcopy( sr->s_dn.bv_val, opc->sdn.bv_val );
629 *(sr->s_ndn.bv_val++) = '\0';
630 sr->s_uuid.bv_val = lutil_strcopy( sr->s_ndn.bv_val, opc->sndn.bv_val );
631 *(sr->s_uuid.bv_val++) = '\0';
632 sr->s_csn.bv_val = lutil_strcopy( sr->s_uuid.bv_val, opc->suuid.bv_val );
637 so->s_restail->s_next = sr;
640 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
644 /* Send a persistent search response */
646 syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, Entry *e, int mode, int queue )
648 slap_overinst *on = opc->son;
649 syncprov_info_t *si = on->on_bi.bi_private;
651 SlapReply rs = { REP_SEARCH };
652 LDAPControl *ctrls[2];
653 struct berval cookie;
655 Attribute a_uuid = {0};
656 Operation sop = *so->s_op;
661 sop.o_tmpmemctx = op->o_tmpmemctx;
663 sop.o_controls = op->o_controls;
665 if ( queue && (so->s_flags & PS_IS_REFRESHING) ) {
666 ldap_pvt_thread_mutex_lock( &so->s_mutex );
667 if ( so->s_flags & PS_IS_REFRESHING )
668 return syncprov_qresp( opc, so, mode );
669 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
673 slap_compose_sync_cookie( op, &cookie, &opc->sctxcsn,
674 so->s_sid, so->s_rid );
676 e_uuid.e_attrs = &a_uuid;
677 a_uuid.a_desc = slap_schema.si_ad_entryUUID;
678 a_uuid.a_nvals = &opc->suuid;
679 rs.sr_err = syncprov_state_ctrl( &sop, &rs, &e_uuid,
680 mode, ctrls, 0, 1, &cookie );
686 if ( opc->sreference ) {
687 rs.sr_ref = get_entry_referrals( &sop, e );
688 send_search_reference( &sop, &rs );
689 ber_bvarray_free( rs.sr_ref );
693 case LDAP_SYNC_MODIFY:
694 rs.sr_attrs = sop.ors_attrs;
695 send_search_entry( &sop, &rs );
697 case LDAP_SYNC_DELETE:
698 e_uuid.e_attrs = NULL;
699 e_uuid.e_name = opc->sdn;
700 e_uuid.e_nname = opc->sndn;
701 rs.sr_entry = &e_uuid;
702 if ( opc->sreference ) {
707 send_search_reference( &sop, &rs );
709 send_search_entry( &sop, &rs );
715 op->o_tmpfree( rs.sr_ctrls[0], op->o_tmpmemctx );
721 syncprov_free_syncop( syncops *so )
723 syncres *sr, *srnext;
725 ldap_pvt_thread_mutex_lock( &so->s_mutex );
727 if ( so->s_inuse > 0 ) {
728 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
731 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
732 filter_free( so->s_op->ors_filter );
734 ch_free( so->s_base.bv_val );
735 for ( sr=so->s_res; sr; sr=srnext ) {
739 ldap_pvt_thread_mutex_destroy( &so->s_mutex );
744 syncprov_drop_psearch( syncops *so )
746 ldap_pvt_thread_mutex_lock( &so->s_op->o_conn->c_mutex );
747 so->s_op->o_conn->c_n_ops_executing--;
748 so->s_op->o_conn->c_n_ops_completed++;
749 ldap_pvt_thread_mutex_unlock( &so->s_op->o_conn->c_mutex );
750 syncprov_free_syncop( so );
754 syncprov_op_abandon( Operation *op, SlapReply *rs )
756 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
757 syncprov_info_t *si = on->on_bi.bi_private;
758 syncops *so, *soprev;
760 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
761 for ( so=si->si_ops, soprev = (syncops *)&si->si_ops; so;
762 soprev=so, so=so->s_next ) {
763 if ( so->s_op->o_connid == op->o_connid &&
764 so->s_op->o_msgid == op->orn_msgid ) {
765 so->s_op->o_abandon = 1;
766 soprev->s_next = so->s_next;
770 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
772 /* Is this really a Cancel exop? */
773 if ( op->o_tag != LDAP_REQ_ABANDON ) {
774 rs->sr_err = LDAP_CANCELLED;
775 send_ldap_result( so->s_op, rs );
777 syncprov_drop_psearch( so );
779 return SLAP_CB_CONTINUE;
782 /* Find which persistent searches are affected by this operation */
784 syncprov_matchops( Operation *op, opcookie *opc, int saveit )
786 slap_overinst *on = opc->son;
787 syncprov_info_t *si = on->on_bi.bi_private;
790 syncops *ss, *sprev, *snext;
796 fc.fdn = &op->o_req_ndn;
798 if ( op->o_tag == LDAP_REQ_MODRDN && !saveit ) {
800 if ( op->orr_nnewSup ) pdn = *op->orr_nnewSup;
801 else dnParent( fc.fdn, &pdn );
802 build_new_dn( &newdn, &pdn, &op->orr_nnewrdn, op->o_tmpmemctx );
805 if ( op->o_tag != LDAP_REQ_ADD ) {
806 op->o_bd->bd_info = (BackendInfo *)on->on_info;
807 rc = be_entry_get_rw( op, fc.fdn, NULL, NULL, 0, &e );
808 op->o_bd->bd_info = (BackendInfo *)on;
815 ber_dupbv_x( &opc->sdn, &e->e_name, op->o_tmpmemctx );
816 ber_dupbv_x( &opc->sndn, &e->e_nname, op->o_tmpmemctx );
817 opc->sreference = is_entry_referral( e );
819 if ( saveit || op->o_tag == LDAP_REQ_ADD ) {
820 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
822 ber_dupbv_x( &opc->suuid, &a->a_nvals[0], op->o_tmpmemctx );
825 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
826 for (ss = si->si_ops, sprev = (syncops *)&si->si_ops; ss;
827 sprev = ss, ss=snext)
838 /* If the base of the search is missing, signal a refresh */
839 rc = syncprov_findbase( op, &fc );
840 if ( rc != LDAP_SUCCESS ) {
841 SlapReply rs = {REP_RESULT};
842 send_ldap_error( ss->s_op, &rs, LDAP_SYNC_REFRESH_REQUIRED,
843 "search base has changed" );
844 sprev->s_next = snext;
845 syncprov_drop_psearch( ss );
849 /* If we're sending results now, look for this op in old matches */
852 for ( sm=opc->smatches, old=(syncmatches *)&opc->smatches; sm;
853 old=sm, sm=sm->sm_next ) {
854 if ( sm->sm_op == ss ) {
856 old->sm_next = sm->sm_next;
857 op->o_tmpfree( sm, op->o_tmpmemctx );
863 /* check if current o_req_dn is in scope and matches filter */
864 if ( fc.fscope && test_filter( op, e, ss->s_op->ors_filter ) ==
865 LDAP_COMPARE_TRUE ) {
867 sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
868 sm->sm_next = opc->smatches;
873 /* if found send UPDATE else send ADD */
874 syncprov_sendresp( op, opc, ss, e,
875 found ? LDAP_SYNC_MODIFY : LDAP_SYNC_ADD, 1 );
877 } else if ( !saveit && found ) {
879 syncprov_sendresp( op, opc, ss, NULL, LDAP_SYNC_DELETE, 1 );
882 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
883 if ( op->o_tag != LDAP_REQ_ADD ) {
884 op->o_bd->bd_info = (BackendInfo *)on->on_info;
885 be_entry_release_r( op, e );
886 op->o_bd->bd_info = (BackendInfo *)on;
891 syncprov_op_cleanup( Operation *op, SlapReply *rs )
893 slap_callback *cb = op->o_callback;
894 opcookie *opc = cb->sc_private;
895 slap_overinst *on = opc->son;
896 syncprov_info_t *si = on->on_bi.bi_private;
897 syncmatches *sm, *snext;
898 modtarget *mt, mtdummy;
900 for (sm = opc->smatches; sm; sm=snext) {
902 syncprov_free_syncop( sm->sm_op );
903 op->o_tmpfree( sm, op->o_tmpmemctx );
906 /* Remove op from lock table */
908 ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
909 mt = avl_find( si->si_mods, &mtdummy, sp_avl_cmp );
910 ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
912 modinst *mi = mt->mt_mods;
914 /* If there are more, promote the next one */
915 ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
917 mt->mt_mods = mi->mi_next;
918 mt->mt_op = mt->mt_mods->mi_op;
919 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
921 ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
922 avl_delete( &si->si_mods, mt, sp_avl_cmp );
923 ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
924 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
925 ldap_pvt_thread_mutex_destroy( &mt->mt_mutex );
929 op->o_callback = cb->sc_next;
930 op->o_tmpfree(cb, op->o_tmpmemctx);
934 syncprov_checkpoint( Operation *op, SlapReply *rs, slap_overinst *on )
936 syncprov_info_t *si = on->on_bi.bi_private;
941 slap_callback cb = {0};
945 bv[0] = si->si_ctxcsn;
946 mod.sml_nvalues = NULL;
947 mod.sml_desc = slap_schema.si_ad_contextCSN;
948 mod.sml_op = LDAP_MOD_REPLACE;
951 cb.sc_response = slap_null_cb;
953 opm.o_tag = LDAP_REQ_MODIFY;
954 opm.o_callback = &cb;
955 opm.orm_modlist = &mod;
956 opm.o_req_dn = op->o_bd->be_suffix[0];
957 opm.o_req_ndn = op->o_bd->be_nsuffix[0];
958 orig = opm.o_bd->bd_info;
959 opm.o_bd->bd_info = on->on_info->oi_orig;
960 opm.o_bd->be_modify( &opm, rs );
964 syncprov_op_response( Operation *op, SlapReply *rs )
966 opcookie *opc = op->o_callback->sc_private;
967 slap_overinst *on = opc->son;
968 syncprov_info_t *si = on->on_bi.bi_private;
971 if ( rs->sr_err == LDAP_SUCCESS )
973 struct berval maxcsn;
974 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
976 /* Update our context CSN */
978 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
979 slap_get_commit_csn( op, &maxcsn );
980 if ( maxcsn.bv_val ) {
981 strcpy( cbuf, maxcsn.bv_val );
982 if ( ber_bvcmp( &maxcsn, &si->si_ctxcsn ) > 0 ) {
983 strcpy( si->si_ctxcsnbuf, cbuf );
984 si->si_ctxcsn.bv_len = maxcsn.bv_len;
989 if ( si->si_chkops || si->si_chktime ) {
991 if ( si->si_chkops && si->si_numops >= si->si_chkops ) {
995 if ( si->si_chktime &&
996 (op->o_time - si->si_chklast >= si->si_chktime )) {
998 si->si_chklast = op->o_time;
1001 syncprov_checkpoint( op, rs, on );
1004 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1006 opc->sctxcsn.bv_len = maxcsn.bv_len;
1007 opc->sctxcsn.bv_val = cbuf;
1009 /* Handle any persistent searches */
1013 case LDAP_REQ_MODIFY:
1014 case LDAP_REQ_MODRDN:
1015 case LDAP_REQ_EXTENDED:
1016 syncprov_matchops( op, opc, 0 );
1018 case LDAP_REQ_DELETE:
1019 /* for each match in opc->smatches:
1022 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
1023 for ( sm = opc->smatches; sm; sm=sm->sm_next ) {
1024 if ( sm->sm_op->s_op->o_abandon )
1026 syncprov_sendresp( op, opc, sm->sm_op, NULL,
1027 LDAP_SYNC_DELETE, 1 );
1029 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
1035 return SLAP_CB_CONTINUE;
1038 /* We don't use a subentry to store the context CSN any more.
1039 * We expose the current context CSN as an operational attribute
1040 * of the suffix entry.
1043 syncprov_op_compare( Operation *op, SlapReply *rs )
1045 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1046 syncprov_info_t *si = on->on_bi.bi_private;
1047 int rc = SLAP_CB_CONTINUE;
1049 if ( dn_match( &op->o_req_ndn, op->o_bd->be_nsuffix ) &&
1050 op->oq_compare.rs_ava->aa_desc == slap_schema.si_ad_contextCSN )
1054 struct berval bv[2];
1056 e.e_name = op->o_bd->be_suffix[0];
1057 e.e_nname = op->o_bd->be_nsuffix[0];
1059 bv[1].bv_val = NULL;
1060 bv[0] = si->si_ctxcsn;
1062 a.a_desc = slap_schema.si_ad_contextCSN;
1064 a.a_nvals = a.a_vals;
1066 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
1068 rs->sr_err = access_allowed( op, &e, op->oq_compare.rs_ava->aa_desc,
1069 &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
1070 if ( ! rs->sr_err ) {
1071 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1072 goto return_results;
1075 if ( get_assert( op ) &&
1076 ( test_filter( op, &e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
1078 rs->sr_err = LDAP_ASSERTION_FAILED;
1079 goto return_results;
1083 rs->sr_err = LDAP_COMPARE_FALSE;
1085 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
1086 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1087 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1088 a.a_nvals, &op->oq_compare.rs_ava->aa_value, op->o_tmpmemctx ) == 0 )
1090 rs->sr_err = LDAP_COMPARE_TRUE;
1095 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1097 send_ldap_result( op, rs );
1099 if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
1100 rs->sr_err = LDAP_SUCCESS;
1109 syncprov_op_mod( Operation *op, SlapReply *rs )
1111 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1112 syncprov_info_t *si = on->on_bi.bi_private;
1114 slap_callback *cb = op->o_tmpcalloc(1, sizeof(slap_callback)+
1116 (si->si_ops ? sizeof(modinst) : 0 ),
1118 opcookie *opc = (opcookie *)(cb+1);
1120 cb->sc_response = syncprov_op_response;
1121 cb->sc_cleanup = syncprov_op_cleanup;
1122 cb->sc_private = opc;
1123 cb->sc_next = op->o_callback;
1124 op->o_callback = cb;
1126 /* If there are active persistent searches, lock this operation.
1127 * See seqmod.c for the locking logic on its own.
1130 modtarget *mt, mtdummy;
1133 mi = (modinst *)(opc+1);
1136 /* See if we're already modifying this entry... */
1138 ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
1139 mt = avl_find( si->si_mods, &mtdummy, sp_avl_cmp );
1141 ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
1142 ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
1143 mt->mt_tail->mi_next = mi;
1145 /* wait for this op to get to head of list */
1146 while ( mt->mt_mods != mi ) {
1147 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
1148 ldap_pvt_thread_yield();
1149 ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
1152 /* Record that we're modifying this entry now */
1153 mt = malloc( sizeof(modtarget) );
1156 mt->mt_op = mi->mi_op;
1157 ldap_pvt_thread_mutex_init( &mt->mt_mutex );
1158 avl_insert( &si->si_mods, mt, sp_avl_cmp, avl_dup_error );
1159 ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
1162 if ( op->o_tag != LDAP_REQ_ADD )
1163 syncprov_matchops( op, opc, 1 );
1166 return SLAP_CB_CONTINUE;
1170 syncprov_op_extended( Operation *op, SlapReply *rs )
1172 if ( exop_is_write( op ))
1173 return syncprov_op_mod( op, rs );
1175 return SLAP_CB_CONTINUE;
1178 typedef struct searchstate {
1179 slap_overinst *ss_on;
1184 syncprov_search_cleanup( Operation *op, SlapReply *rs )
1187 if ( rs->sr_ctrls ) {
1188 free( rs->sr_ctrls[0] );
1189 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
1190 rs->sr_ctrls = NULL;
1197 syncprov_detach_op( Operation *op, syncops *so )
1204 /* count the search attrs */
1205 for (i=0; op->ors_attrs && op->ors_attrs[i].an_name.bv_val; i++) {
1206 alen += op->ors_attrs[i].an_name.bv_len + 1;
1208 /* Make a new copy of the operation */
1209 size = sizeof(Operation) + sizeof(Opheader) +
1210 (i ? ( (i+1) * sizeof(AttributeName) + alen) : 0) +
1211 op->o_req_dn.bv_len + 1 +
1212 op->o_req_ndn.bv_len + 1 +
1213 op->o_ndn.bv_len + 1 +
1214 so->s_filterstr.bv_len + 1;
1215 op2 = (Operation *)ch_malloc( size );
1217 op2->o_hdr = (Opheader *)(op2+1);
1218 *op2->o_hdr = *op->o_hdr;
1220 op2->ors_attrs = (AttributeName *)(op2->o_hdr + 1);
1221 ptr = (char *)(op2->ors_attrs+i+1);
1222 for (i=0; op->ors_attrs[i].an_name.bv_val; i++) {
1223 op2->ors_attrs[i] = op->ors_attrs[i];
1224 op2->ors_attrs[i].an_name.bv_val = ptr;
1225 ptr = lutil_strcopy( ptr, op->ors_attrs[i].an_name.bv_val ) + 1;
1227 op2->ors_attrs[i].an_name.bv_val = NULL;
1228 op2->ors_attrs[i].an_name.bv_len = 0;
1230 ptr = (char *)(op2->o_hdr + 1);
1232 op2->o_ndn.bv_val = ptr;
1233 ptr = lutil_strcopy(ptr, op->o_ndn.bv_val) + 1;
1234 op2->o_dn = op2->o_ndn;
1235 op2->o_req_dn.bv_val = ptr;
1236 ptr = lutil_strcopy(ptr, op->o_req_dn.bv_val) + 1;
1237 op2->o_req_ndn.bv_val = ptr;
1238 ptr = lutil_strcopy(ptr, op->o_req_ndn.bv_val) + 1;
1239 op2->ors_filterstr.bv_val = ptr;
1240 strcpy( ptr, so->s_filterstr.bv_val );
1241 op2->ors_filterstr.bv_len = so->s_filterstr.bv_len;
1242 op2->ors_filter = str2filter( ptr );
1243 op2->o_controls = NULL;
1244 op2->o_callback = NULL;
1247 /* Increment number of ops so that idletimeout ignores us */
1248 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
1249 op->o_conn->c_n_ops_executing++;
1250 op->o_conn->c_n_ops_completed--;
1251 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
1255 syncprov_search_response( Operation *op, SlapReply *rs )
1257 searchstate *ss = op->o_callback->sc_private;
1258 slap_overinst *on = ss->ss_on;
1259 syncprov_info_t *si = on->on_bi.bi_private;
1260 sync_control *srs = op->o_controls[sync_cid];
1262 if ( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF ) {
1264 if ( srs->sr_state.ctxcsn ) {
1265 Attribute *a = attr_find( rs->sr_entry->e_attrs,
1266 slap_schema.si_ad_entryCSN );
1267 /* Don't send the ctx entry twice */
1268 if ( bvmatch( &a->a_nvals[0], srs->sr_state.ctxcsn ))
1269 return LDAP_SUCCESS;
1271 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
1273 rs->sr_ctrls[1] = NULL;
1274 rs->sr_err = syncprov_state_ctrl( op, rs, rs->sr_entry,
1275 LDAP_SYNC_ADD, rs->sr_ctrls, 0, 0, NULL );
1276 } else if ( rs->sr_type == REP_RESULT && rs->sr_err == LDAP_SUCCESS ) {
1277 struct berval cookie;
1279 slap_compose_sync_cookie( op, &cookie,
1280 &op->ors_filter->f_and->f_ava->aa_value,
1281 srs->sr_state.sid, srs->sr_state.rid );
1283 /* Is this a regular refresh? */
1285 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
1287 rs->sr_ctrls[1] = NULL;
1288 rs->sr_err = syncprov_done_ctrl( op, rs, rs->sr_ctrls,
1289 0, 1, &cookie, LDAP_SYNC_REFRESH_PRESENTS );
1292 /* It's RefreshAndPersist, transition to Persist phase */
1293 syncprov_sendinfo( op, rs, rs->sr_nentries ?
1294 LDAP_TAG_SYNC_REFRESH_PRESENT : LDAP_TAG_SYNC_REFRESH_DELETE,
1295 &cookie, 1, NULL, 0 );
1296 /* Flush any queued persist messages */
1297 if ( ss->ss_so->s_res ) {
1298 syncres *sr, *srnext;
1303 ldap_pvt_thread_mutex_lock( &ss->ss_so->s_mutex );
1305 for (sr = ss->ss_so->s_res; sr; sr=srnext) {
1306 int rc = LDAP_SUCCESS;
1307 srnext = sr->s_next;
1309 opc.sndn = sr->s_ndn;
1310 opc.suuid = sr->s_uuid;
1311 opc.sctxcsn = sr->s_csn;
1312 opc.sreference = sr->s_isreference;
1315 if ( sr->s_mode != LDAP_SYNC_DELETE ) {
1316 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1317 rc = be_entry_get_rw( op, &opc.sndn, NULL, NULL, 0, &e );
1318 op->o_bd->bd_info = (BackendInfo *)on;
1320 if ( rc == LDAP_SUCCESS )
1321 syncprov_sendresp( op, &opc, ss->ss_so, e,
1325 op->o_bd->bd_info = (BackendInfo *)on->on_info;
1326 be_entry_release_r( op, e );
1327 op->o_bd->bd_info = (BackendInfo *)on;
1331 ss->ss_so->s_res = NULL;
1332 ss->ss_so->s_restail = NULL;
1335 /* Turn off the refreshing flag */
1336 ss->ss_so->s_flags ^= PS_IS_REFRESHING;
1338 ldap_pvt_thread_mutex_unlock( &ss->ss_so->s_mutex );
1340 /* Detach this Op from frontend control */
1341 syncprov_detach_op( op, ss->ss_so );
1343 return LDAP_SUCCESS;
1347 return SLAP_CB_CONTINUE;
1351 syncprov_op_search( Operation *op, SlapReply *rs )
1353 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1354 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1356 int gotstate = 0, nochange = 0;
1357 Filter *fand, *fava;
1358 syncops *sop = NULL;
1362 if ( !(op->o_sync_mode & SLAP_SYNC_REFRESH) ) return SLAP_CB_CONTINUE;
1364 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
1365 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "illegal value for derefAliases" );
1369 srs = op->o_controls[sync_cid];
1371 /* If this is a persistent search, set it up right away */
1372 if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
1382 so.s_flags = PS_IS_REFRESHING;
1383 /* syncprov_findbase expects to be called as a callback... */
1384 sc.sc_private = &opc;
1386 cb = op->o_callback;
1387 op->o_callback = ≻
1388 rs->sr_err = syncprov_findbase( op, &fc );
1389 op->o_callback = cb;
1391 if ( rs->sr_err != LDAP_SUCCESS ) {
1392 send_ldap_result( op, rs );
1395 sop = ch_malloc( sizeof( syncops ));
1397 ldap_pvt_thread_mutex_init( &sop->s_mutex );
1398 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
1399 sop->s_sid = srs->sr_state.sid;
1400 sop->s_rid = srs->sr_state.rid;
1401 sop->s_next = si->si_ops;
1404 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
1407 /* If we have a cookie, handle the PRESENT lookups
1409 if ( srs->sr_state.ctxcsn ) {
1410 /* Is the CSN in a valid format? */
1411 if ( srs->sr_state.ctxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE ) {
1412 send_ldap_error( op, rs, LDAP_OTHER, "invalid sync cookie" );
1415 /* Is the CSN still present in the database? */
1416 if ( syncprov_findcsn( op, FIND_CSN ) != LDAP_SUCCESS ) {
1417 /* No, so a reload is required */
1418 #if 0 /* the consumer doesn't seem to send this hint */
1419 if ( op->o_sync_rhint == 0 ) {
1420 send_ldap_error( op, rs, LDAP_SYNC_REFRESH_REQUIRED, "sync cookie is stale" );
1426 /* If just Refreshing and nothing has changed, shortcut it */
1427 if ( bvmatch( srs->sr_state.ctxcsn, &si->si_ctxcsn )) {
1429 if ( !(op->o_sync_mode & SLAP_SYNC_PERSIST) ) {
1430 LDAPControl *ctrls[2];
1434 syncprov_done_ctrl( op, rs, ctrls, 0, 0,
1435 NULL, LDAP_SYNC_REFRESH_DELETES );
1436 rs->sr_ctrls = ctrls;
1437 rs->sr_err = LDAP_SUCCESS;
1438 send_ldap_result( op, rs );
1439 rs->sr_ctrls = NULL;
1444 /* If context has changed, check for Present UUIDs */
1445 if ( syncprov_findcsn( op, FIND_PRESENT ) != LDAP_SUCCESS ) {
1446 send_ldap_result( op, rs );
1452 /* Append CSN range to search filter, save original filter
1453 * for persistent search evaluation
1456 sop->s_filterstr= op->ors_filterstr;
1459 fand = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1460 fand->f_choice = LDAP_FILTER_AND;
1461 fand->f_next = NULL;
1462 fava = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1463 fava->f_choice = LDAP_FILTER_LE;
1464 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
1465 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
1466 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
1467 ber_dupbv_x( &fava->f_ava->aa_value, &si->si_ctxcsn, op->o_tmpmemctx );
1468 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1471 fava->f_next = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
1472 fava = fava->f_next;
1473 fava->f_choice = LDAP_FILTER_GE;
1474 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
1475 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
1476 ber_dupbv_x( &fava->f_ava->aa_value, srs->sr_state.ctxcsn, op->o_tmpmemctx );
1478 fava->f_next = op->ors_filter;
1479 op->ors_filter = fand;
1480 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
1483 /* Let our callback add needed info to returned entries */
1484 cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(searchstate), op->o_tmpmemctx);
1485 ss = (searchstate *)(cb+1);
1488 cb->sc_response = syncprov_search_response;
1489 cb->sc_cleanup = syncprov_search_cleanup;
1490 cb->sc_private = ss;
1491 cb->sc_next = op->o_callback;
1492 op->o_callback = cb;
1494 #if 0 /* I don't think we need to shortcircuit back-bdb any more */
1495 op->o_sync_mode &= SLAP_CONTROL_MASK;
1498 /* If this is a persistent search and no changes were reported during
1499 * the refresh phase, just invoke the response callback to transition
1500 * us into persist phase
1503 rs->sr_err = LDAP_SUCCESS;
1504 rs->sr_nentries = 0;
1505 send_ldap_result( op, rs );
1508 return SLAP_CB_CONTINUE;
1512 syncprov_operational(
1516 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1517 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1519 if ( rs->sr_entry &&
1520 dn_match( &rs->sr_entry->e_nname, op->o_bd->be_nsuffix )) {
1522 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
1523 ad_inlist( slap_schema.si_ad_contextCSN, rs->sr_attrs )) {
1524 Attribute *a, **ap = NULL;
1527 for ( a=rs->sr_entry->e_attrs; a; a=a->a_next ) {
1528 if ( a->a_desc == slap_schema.si_ad_contextCSN )
1533 for ( ap = &rs->sr_operational_attrs; *ap; ap=&(*ap)->a_next );
1535 a = ch_malloc( sizeof(Attribute));
1536 a->a_desc = slap_schema.si_ad_contextCSN;
1537 a->a_vals = ch_malloc( 2 * sizeof(struct berval));
1538 a->a_vals[1].bv_val = NULL;
1539 a->a_nvals = a->a_vals;
1545 ldap_pvt_thread_mutex_lock( &si->si_csn_mutex );
1547 strcpy( a->a_vals[0].bv_val, si->si_ctxcsnbuf );
1549 ber_dupbv( &a->a_vals[0], &si->si_ctxcsn );
1551 ldap_pvt_thread_mutex_unlock( &si->si_csn_mutex );
1554 return LDAP_SUCCESS;
1566 slap_overinst *on = (slap_overinst *)be->bd_info;
1567 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1570 if ( strcasecmp( argv[ 0 ], "syncprov-checkpoint" ) == 0 ) {
1572 fprintf( stderr, "%s: line %d: wrong number of arguments in "
1573 "\"syncprov-checkpoint <ops> <minutes>\"\n", fname, lineno );
1576 si->si_chkops = atoi( argv[1] );
1577 si->si_chktime = atoi( argv[2] ) * 60;
1580 return SLAP_CONF_UNKNOWN;
1584 return SLAP_CONF_UNKNOWN;
1587 /* Cheating - we have no thread pool context for these functions,
1590 typedef struct thread_keys {
1593 ldap_pvt_thread_pool_keyfree_t *free;
1596 /* A fake thread context */
1597 static thread_keys thrctx[8];
1599 /* Read any existing contextCSN from the underlying db.
1600 * Then search for any entries newer than that. If no value exists,
1601 * just generate it. Cache whatever result.
1608 slap_overinst *on = (slap_overinst *) be->bd_info;
1609 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1611 char opbuf[OPERATION_BUFFER_SIZE];
1612 Operation *op = (Operation *)opbuf;
1617 memset(opbuf, 0, sizeof(opbuf));
1618 op->o_hdr = (Opheader *)(op+1);
1620 op->o_dn = be->be_rootdn;
1621 op->o_ndn = be->be_rootndn;
1622 op->o_threadctx = thrctx;
1623 op->o_tmpmfuncs = &ch_mfuncs;
1625 op->o_bd->bd_info = on->on_info->oi_orig;
1626 rc = be_entry_get_rw( op, be->be_nsuffix, NULL,
1627 slap_schema.si_ad_contextCSN, 0, &e );
1630 a = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
1632 si->si_ctxcsn.bv_len = a->a_nvals[0].bv_len;
1633 if ( si->si_ctxcsn.bv_len >= sizeof(si->si_ctxcsnbuf ))
1634 si->si_ctxcsn.bv_len = sizeof(si->si_ctxcsnbuf)-1;
1635 strncpy( si->si_ctxcsnbuf, a->a_nvals[0].bv_val,
1636 si->si_ctxcsn.bv_len );
1637 si->si_ctxcsnbuf[si->si_ctxcsn.bv_len] = '\0';
1639 be_entry_release_r( op, e );
1641 op->o_bd->bd_info = (BackendInfo *)on;
1645 /* Write the current contextCSN into the underlying db.
1652 slap_overinst *on = (slap_overinst *) be->bd_info;
1653 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1656 if ( si->si_numops ) {
1658 char opbuf[OPERATION_BUFFER_SIZE];
1659 Operation *op = (Operation *)opbuf;
1660 SlapReply rs = {REP_RESULT};
1662 connection_fake_init( &conn, op, thrctx );
1664 op->o_dn = be->be_rootdn;
1665 op->o_ndn = be->be_rootndn;
1666 syncprov_checkpoint( op, &rs, on );
1668 for ( i=0; thrctx[i].key; i++) {
1669 if ( thrctx[i].free )
1670 thrctx[i].free( thrctx[i].key, thrctx[i].data );
1681 slap_overinst *on = (slap_overinst *)be->bd_info;
1682 syncprov_info_t *si;
1684 si = ch_calloc(1, sizeof(syncprov_info_t));
1685 on->on_bi.bi_private = si;
1686 ldap_pvt_thread_mutex_init( &si->si_csn_mutex );
1687 ldap_pvt_thread_mutex_init( &si->si_ops_mutex );
1688 ldap_pvt_thread_mutex_init( &si->si_mods_mutex );
1689 si->si_ctxcsn.bv_val = si->si_ctxcsnbuf;
1691 csn_anlist[0].an_desc = slap_schema.si_ad_entryCSN;
1692 csn_anlist[0].an_name = slap_schema.si_ad_entryCSN->ad_cname;
1694 uuid_anlist[0].an_desc = slap_schema.si_ad_entryUUID;
1695 uuid_anlist[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1701 syncprov_db_destroy(
1705 slap_overinst *on = (slap_overinst *)be->bd_info;
1706 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1709 ldap_pvt_thread_mutex_destroy( &si->si_mods_mutex );
1710 ldap_pvt_thread_mutex_destroy( &si->si_ops_mutex );
1711 ldap_pvt_thread_mutex_destroy( &si->si_csn_mutex );
1718 static int syncprov_parseCtrl (
1727 struct berval cookie = BER_BVNULL;
1731 if ( op->o_sync != SLAP_CONTROL_NONE ) {
1732 rs->sr_text = "Sync control specified multiple times";
1733 return LDAP_PROTOCOL_ERROR;
1736 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
1737 rs->sr_text = "Sync control specified with pagedResults control";
1738 return LDAP_PROTOCOL_ERROR;
1741 if ( ctrl->ldctl_value.bv_len == 0 ) {
1742 rs->sr_text = "Sync control value is empty (or absent)";
1743 return LDAP_PROTOCOL_ERROR;
1746 /* Parse the control value
1747 * syncRequestValue ::= SEQUENCE {
1752 * refreshAndPersist (3)
1754 * cookie syncCookie OPTIONAL
1758 ber = ber_init( &ctrl->ldctl_value );
1760 rs->sr_text = "internal error";
1764 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
1765 rs->sr_text = "Sync control : mode decoding error";
1766 return LDAP_PROTOCOL_ERROR;
1770 case LDAP_SYNC_REFRESH_ONLY:
1771 mode = SLAP_SYNC_REFRESH;
1773 case LDAP_SYNC_REFRESH_AND_PERSIST:
1774 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
1777 rs->sr_text = "Sync control : unknown update mode";
1778 return LDAP_PROTOCOL_ERROR;
1781 tag = ber_peek_tag( ber, &len );
1783 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
1784 if (( ber_scanf( ber, /*{*/ "o", &cookie )) == LBER_ERROR ) {
1785 rs->sr_text = "Sync control : cookie decoding error";
1786 return LDAP_PROTOCOL_ERROR;
1789 if ( tag == LDAP_TAG_RELOAD_HINT ) {
1790 if (( ber_scanf( ber, /*{*/ "b", &rhint )) == LBER_ERROR ) {
1791 rs->sr_text = "Sync control : rhint decoding error";
1792 return LDAP_PROTOCOL_ERROR;
1795 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
1796 rs->sr_text = "Sync control : decoding error";
1797 return LDAP_PROTOCOL_ERROR;
1799 sr = op->o_tmpcalloc( 1, sizeof(struct sync_control), op->o_tmpmemctx );
1800 sr->sr_rhint = rhint;
1801 if (!BER_BVISNULL(&cookie)) {
1802 ber_bvarray_add( &sr->sr_state.octet_str, &cookie );
1803 slap_parse_sync_cookie( &sr->sr_state );
1806 op->o_controls[sync_cid] = sr;
1808 (void) ber_free( ber, 1 );
1810 op->o_sync = ctrl->ldctl_iscritical
1811 ? SLAP_CONTROL_CRITICAL
1812 : SLAP_CONTROL_NONCRITICAL;
1814 op->o_sync_mode |= mode; /* o_sync_mode shares o_sync */
1816 return LDAP_SUCCESS;
1819 /* This overlay is set up for dynamic loading via moduleload. For static
1820 * configuration, you'll need to arrange for the slap_overinst to be
1821 * initialized and registered by some other function inside slapd.
1824 static slap_overinst syncprov;
1831 rc = register_supported_control( LDAP_CONTROL_SYNC,
1832 SLAP_CTRL_HIDE|SLAP_CTRL_SEARCH, NULL,
1833 syncprov_parseCtrl, &sync_cid );
1834 if ( rc != LDAP_SUCCESS ) {
1835 fprintf( stderr, "Failed to register control %d\n", rc );
1839 syncprov.on_bi.bi_type = "syncprov";
1840 syncprov.on_bi.bi_db_init = syncprov_db_init;
1841 syncprov.on_bi.bi_db_config = syncprov_db_config;
1842 syncprov.on_bi.bi_db_destroy = syncprov_db_destroy;
1843 syncprov.on_bi.bi_db_open = syncprov_db_open;
1844 syncprov.on_bi.bi_db_close = syncprov_db_close;
1846 syncprov.on_bi.bi_op_abandon = syncprov_op_abandon;
1847 syncprov.on_bi.bi_op_cancel = syncprov_op_abandon;
1849 syncprov.on_bi.bi_op_add = syncprov_op_mod;
1850 syncprov.on_bi.bi_op_compare = syncprov_op_compare;
1851 syncprov.on_bi.bi_op_delete = syncprov_op_mod;
1852 syncprov.on_bi.bi_op_modify = syncprov_op_mod;
1853 syncprov.on_bi.bi_op_modrdn = syncprov_op_mod;
1854 syncprov.on_bi.bi_op_search = syncprov_op_search;
1855 syncprov.on_bi.bi_extended = syncprov_op_extended;
1856 syncprov.on_bi.bi_operational = syncprov_operational;
1859 syncprov.on_response = syncprov_response;
1862 return overlay_register( &syncprov );
1865 #if SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC
1867 init_module( int argc, char *argv[] )
1869 return syncprov_init();
1871 #endif /* SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC */
1873 #endif /* defined(SLAPD_OVER_SYNCPROV) */