2 /* syncprov.c - syncrepl provider */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2004-2009 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by Howard Chu for inclusion in
23 #ifdef SLAPD_OVER_SYNCPROV
25 #include <ac/string.h>
35 /* A modify request on a particular entry */
36 typedef struct modinst {
37 struct modinst *mi_next;
41 typedef struct modtarget {
42 struct modinst *mt_mods;
43 struct modinst *mt_tail;
45 ldap_pvt_thread_mutex_t mt_mutex;
48 /* A queued result of a persistent search */
49 typedef struct syncres {
50 struct syncres *s_next;
59 /* Record of a persistent search */
60 typedef struct syncops {
61 struct syncops *s_next;
62 struct berval s_base; /* ndn of search base */
63 ID s_eid; /* entryID of search base */
64 Operation *s_op; /* search op */
67 struct berval s_filterstr;
68 int s_flags; /* search status */
69 #define PS_IS_REFRESHING 0x01
70 #define PS_IS_DETACHED 0x02
71 #define PS_WROTE_BASE 0x04
72 #define PS_FIND_BASE 0x08
73 #define PS_FIX_FILTER 0x10
74 #define PS_TASK_QUEUED 0x20
76 int s_inuse; /* reference count */
77 struct syncres *s_res;
78 struct syncres *s_restail;
79 ldap_pvt_thread_mutex_t s_mutex;
82 /* A received sync control */
83 typedef struct sync_control {
84 struct sync_cookie sr_state;
88 #if 0 /* moved back to slap.h */
89 #define o_sync o_ctrlflag[slap_cids.sc_LDAPsync]
91 /* o_sync_mode uses data bits of o_sync */
92 #define o_sync_mode o_ctrlflag[slap_cids.sc_LDAPsync]
94 #define SLAP_SYNC_NONE (LDAP_SYNC_NONE<<SLAP_CONTROL_SHIFT)
95 #define SLAP_SYNC_REFRESH (LDAP_SYNC_REFRESH_ONLY<<SLAP_CONTROL_SHIFT)
96 #define SLAP_SYNC_PERSIST (LDAP_SYNC_RESERVED<<SLAP_CONTROL_SHIFT)
97 #define SLAP_SYNC_REFRESH_AND_PERSIST (LDAP_SYNC_REFRESH_AND_PERSIST<<SLAP_CONTROL_SHIFT)
99 /* Record of which searches matched at premodify step */
100 typedef struct syncmatches {
101 struct syncmatches *sm_next;
105 /* Session log data */
106 typedef struct slog_entry {
107 struct slog_entry *se_next;
108 struct berval se_uuid;
109 struct berval se_csn;
114 typedef struct sessionlog {
115 struct berval sl_mincsn;
120 ldap_pvt_thread_mutex_t sl_mutex;
123 /* The main state for this overlay */
124 typedef struct syncprov_info_t {
126 BerVarray si_ctxcsn; /* ldapsync context */
129 int si_chkops; /* checkpointing info */
131 int si_numops; /* number of ops since last checkpoint */
132 int si_nopres; /* Skip present phase */
133 int si_usehint; /* use reload hint */
134 time_t si_chklast; /* time of last checkpoint */
135 Avlnode *si_mods; /* entries being modified */
137 ldap_pvt_thread_rdwr_t si_csn_rwlock;
138 ldap_pvt_thread_mutex_t si_ops_mutex;
139 ldap_pvt_thread_mutex_t si_mods_mutex;
142 typedef struct opcookie {
144 syncmatches *smatches;
145 struct berval sdn; /* DN of entry, for deletes */
147 struct berval suuid; /* UUID of entry */
148 struct berval sctxcsn;
149 short osid; /* sid of op csn */
150 short rsid; /* sid of relay */
151 short sreference; /* Is the entry a reference? */
154 typedef struct fbase_cookie {
155 struct berval *fdn; /* DN of a modified entry, for scope testing */
156 syncops *fss; /* persistent search we're testing against */
157 int fbase; /* if TRUE we found the search base and it's still valid */
158 int fscope; /* if TRUE then fdn is within the psearch scope */
161 static AttributeName csn_anlist[3];
162 static AttributeName uuid_anlist[2];
164 /* Build a LDAPsync intermediate state control */
170 int entry_sync_state,
174 struct berval *cookie )
179 BerElementBuffer berbuf;
180 BerElement *ber = (BerElement *)&berbuf;
182 struct berval entryuuid_bv = BER_BVNULL;
184 ber_init2( ber, 0, LBER_USE_DER );
185 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
187 ctrls[num_ctrls] = op->o_tmpalloc( sizeof ( LDAPControl ), op->o_tmpmemctx );
189 for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
190 AttributeDescription *desc = a->a_desc;
191 if ( desc == slap_schema.si_ad_entryUUID ) {
192 entryuuid_bv = a->a_nvals[0];
197 /* FIXME: what if entryuuid is NULL or empty ? */
199 if ( send_cookie && cookie ) {
200 ber_printf( ber, "{eOON}",
201 entry_sync_state, &entryuuid_bv, cookie );
203 ber_printf( ber, "{eON}",
204 entry_sync_state, &entryuuid_bv );
207 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_STATE;
208 ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
209 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
214 Debug( LDAP_DEBUG_TRACE,
215 "slap_build_sync_ctrl: ber_flatten2 failed\n",
217 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
224 /* Build a LDAPsync final state control */
232 struct berval *cookie,
236 BerElementBuffer berbuf;
237 BerElement *ber = (BerElement *)&berbuf;
239 ber_init2( ber, NULL, LBER_USE_DER );
240 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
242 ctrls[num_ctrls] = op->o_tmpalloc( sizeof ( LDAPControl ), op->o_tmpmemctx );
244 ber_printf( ber, "{" );
245 if ( send_cookie && cookie ) {
246 ber_printf( ber, "O", cookie );
248 if ( refreshDeletes == LDAP_SYNC_REFRESH_DELETES ) {
249 ber_printf( ber, "b", refreshDeletes );
251 ber_printf( ber, "N}" );
253 ctrls[num_ctrls]->ldctl_oid = LDAP_CONTROL_SYNC_DONE;
254 ctrls[num_ctrls]->ldctl_iscritical = (op->o_sync == SLAP_CONTROL_CRITICAL);
255 ret = ber_flatten2( ber, &ctrls[num_ctrls]->ldctl_value, 1 );
260 Debug( LDAP_DEBUG_TRACE,
261 "syncprov_done_ctrl: ber_flatten2 failed\n",
263 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
275 struct berval *cookie,
280 BerElementBuffer berbuf;
281 BerElement *ber = (BerElement *)&berbuf;
282 struct berval rspdata;
286 ber_init2( ber, NULL, LBER_USE_DER );
287 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
291 case LDAP_TAG_SYNC_NEW_COOKIE:
292 ber_printf( ber, "tO", type, cookie );
294 case LDAP_TAG_SYNC_REFRESH_DELETE:
295 case LDAP_TAG_SYNC_REFRESH_PRESENT:
296 ber_printf( ber, "t{", type );
298 ber_printf( ber, "O", cookie );
300 if ( refreshDone == 0 ) {
301 ber_printf( ber, "b", refreshDone );
303 ber_printf( ber, "N}" );
305 case LDAP_TAG_SYNC_ID_SET:
306 ber_printf( ber, "t{", type );
308 ber_printf( ber, "O", cookie );
310 if ( refreshDeletes == 1 ) {
311 ber_printf( ber, "b", refreshDeletes );
313 ber_printf( ber, "[W]", syncUUIDs );
314 ber_printf( ber, "N}" );
317 Debug( LDAP_DEBUG_TRACE,
318 "syncprov_sendinfo: invalid syncinfo type (%d)\n",
324 ret = ber_flatten2( ber, &rspdata, 0 );
327 Debug( LDAP_DEBUG_TRACE,
328 "syncprov_sendinfo: ber_flatten2 failed\n",
330 send_ldap_error( op, rs, LDAP_OTHER, "internal error" );
334 rs->sr_rspoid = LDAP_SYNC_INFO;
335 rs->sr_rspdata = &rspdata;
336 send_ldap_intermediate( op, rs );
337 rs->sr_rspdata = NULL;
343 /* Find a modtarget in an AVL tree */
345 sp_avl_cmp( const void *c1, const void *c2 )
347 const modtarget *m1, *m2;
351 rc = m1->mt_op->o_req_ndn.bv_len - m2->mt_op->o_req_ndn.bv_len;
354 return ber_bvcmp( &m1->mt_op->o_req_ndn, &m2->mt_op->o_req_ndn );
357 /* syncprov_findbase:
358 * finds the true DN of the base of a search (with alias dereferencing) and
359 * checks to make sure the base entry doesn't get replaced with a different
360 * entry (e.g., swapping trees via ModDN, or retargeting an alias). If a
361 * change is detected, any persistent search on this base must be terminated /
363 * On the first call, we just save the DN and entryID. On subsequent calls
364 * we compare the DN and entryID with the saved values.
367 findbase_cb( Operation *op, SlapReply *rs )
369 slap_callback *sc = op->o_callback;
371 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
372 fbase_cookie *fc = sc->sc_private;
374 /* If no entryID, we're looking for the first time.
375 * Just store whatever we got.
377 if ( fc->fss->s_eid == NOID ) {
379 fc->fss->s_eid = rs->sr_entry->e_id;
380 ber_dupbv( &fc->fss->s_base, &rs->sr_entry->e_nname );
382 } else if ( rs->sr_entry->e_id == fc->fss->s_eid &&
383 dn_match( &rs->sr_entry->e_nname, &fc->fss->s_base )) {
385 /* OK, the DN is the same and the entryID is the same. */
389 if ( rs->sr_err != LDAP_SUCCESS ) {
390 Debug( LDAP_DEBUG_ANY, "findbase failed! %d\n", rs->sr_err,0,0 );
395 static Filter generic_filter = { LDAP_FILTER_PRESENT, { 0 }, NULL };
396 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
399 syncprov_findbase( Operation *op, fbase_cookie *fc )
401 /* Use basic parameters from syncrepl search, but use
402 * current op's threadctx / tmpmemctx
404 ldap_pvt_thread_mutex_lock( &fc->fss->s_mutex );
405 if ( fc->fss->s_flags & PS_FIND_BASE ) {
406 slap_callback cb = {0};
408 SlapReply frs = { REP_RESULT };
411 fc->fss->s_flags ^= PS_FIND_BASE;
412 ldap_pvt_thread_mutex_unlock( &fc->fss->s_mutex );
414 fop = *fc->fss->s_op;
416 fop.o_bd = fop.o_bd->bd_self;
417 fop.o_hdr = op->o_hdr;
418 fop.o_time = op->o_time;
419 fop.o_tincr = op->o_tincr;
421 cb.sc_response = findbase_cb;
424 fop.o_sync_mode = 0; /* turn off sync mode */
425 fop.o_managedsait = SLAP_CONTROL_CRITICAL;
426 fop.o_callback = &cb;
427 fop.o_tag = LDAP_REQ_SEARCH;
428 fop.ors_scope = LDAP_SCOPE_BASE;
429 fop.ors_limit = NULL;
431 fop.ors_tlimit = SLAP_NO_LIMIT;
432 fop.ors_attrs = slap_anlist_no_attrs;
433 fop.ors_attrsonly = 1;
434 fop.ors_filter = &generic_filter;
435 fop.ors_filterstr = generic_filterstr;
437 rc = fop.o_bd->be_search( &fop, &frs );
439 ldap_pvt_thread_mutex_unlock( &fc->fss->s_mutex );
443 /* After the first call, see if the fdn resides in the scope */
444 if ( fc->fbase == 1 ) {
445 switch ( fc->fss->s_op->ors_scope ) {
446 case LDAP_SCOPE_BASE:
447 fc->fscope = dn_match( fc->fdn, &fc->fss->s_base );
449 case LDAP_SCOPE_ONELEVEL: {
451 dnParent( fc->fdn, &pdn );
452 fc->fscope = dn_match( &pdn, &fc->fss->s_base );
454 case LDAP_SCOPE_SUBTREE:
455 fc->fscope = dnIsSuffix( fc->fdn, &fc->fss->s_base );
457 case LDAP_SCOPE_SUBORDINATE:
458 fc->fscope = dnIsSuffix( fc->fdn, &fc->fss->s_base ) &&
459 !dn_match( fc->fdn, &fc->fss->s_base );
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: at startup time, after a contextCSN has been read from the database,
477 * we search for all entries with CSN >= contextCSN in case the contextCSN
478 * was not checkpointed at the previous shutdown.
480 * 2: when the current contextCSN is known and we have a sync cookie, we search
481 * for one entry with CSN = the cookie CSN. If not found, try <= cookie CSN.
482 * If an entry is found, the cookie CSN is valid, otherwise it is stale.
484 * 3: during a refresh phase, we search for all entries with CSN <= the cookie
485 * CSN, and generate Present records for them. We always collect this result
486 * in SyncID sets, even if there's only one match.
488 typedef enum find_csn_t {
495 findmax_cb( Operation *op, SlapReply *rs )
497 if ( rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS ) {
498 struct berval *maxcsn = op->o_callback->sc_private;
499 Attribute *a = attr_find( rs->sr_entry->e_attrs,
500 slap_schema.si_ad_entryCSN );
502 if ( a && ber_bvcmp( &a->a_vals[0], maxcsn ) > 0 &&
503 slap_parse_csn_sid( &a->a_vals[0] ) == slap_serverID ) {
504 maxcsn->bv_len = a->a_vals[0].bv_len;
505 strcpy( maxcsn->bv_val, a->a_vals[0].bv_val );
512 findcsn_cb( Operation *op, SlapReply *rs )
514 slap_callback *sc = op->o_callback;
516 /* We just want to know that at least one exists, so it's OK if
517 * we exceed the unchecked limit.
519 if ( rs->sr_err == LDAP_ADMINLIMIT_EXCEEDED ||
520 (rs->sr_type == REP_SEARCH && rs->sr_err == LDAP_SUCCESS )) {
521 sc->sc_private = (void *)1;
526 /* Build a list of entryUUIDs for sending in a SyncID set */
530 typedef struct fpres_cookie {
537 findpres_cb( Operation *op, SlapReply *rs )
539 slap_callback *sc = op->o_callback;
540 fpres_cookie *pc = sc->sc_private;
542 int ret = SLAP_CB_CONTINUE;
544 switch ( rs->sr_type ) {
546 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
548 pc->uuids[pc->num].bv_val = pc->last;
549 AC_MEMCPY( pc->uuids[pc->num].bv_val, a->a_nvals[0].bv_val,
550 pc->uuids[pc->num].bv_len );
552 pc->last = pc->uuids[pc->num].bv_val;
553 pc->uuids[pc->num].bv_val = NULL;
556 if ( pc->num != SLAP_SYNCUUID_SET_SIZE )
562 ret = syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET, NULL,
564 pc->uuids[pc->num].bv_val = pc->last;
566 pc->last = pc->uuids[0].bv_val;
576 syncprov_findcsn( Operation *op, find_csn_t mode )
578 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
579 syncprov_info_t *si = on->on_bi.bi_private;
581 slap_callback cb = {0};
583 SlapReply frs = { REP_RESULT };
584 char buf[LDAP_LUTIL_CSNSTR_BUFSIZE + STRLENOF("(entryCSN<=)")];
585 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
586 struct berval maxcsn;
588 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
589 fpres_cookie pcookie;
590 sync_control *srs = NULL;
591 struct slap_limits_set fc_limits;
592 int i, rc = LDAP_SUCCESS, findcsn_retry = 1;
595 if ( mode != FIND_MAXCSN ) {
596 srs = op->o_controls[slap_cids.sc_LDAPsync];
600 fop.o_sync_mode &= SLAP_CONTROL_MASK; /* turn off sync_mode */
601 /* We want pure entries, not referrals */
602 fop.o_managedsait = SLAP_CONTROL_CRITICAL;
605 cf.f_av_desc = slap_schema.si_ad_entryCSN;
606 BER_BVZERO( &cf.f_av_value );
609 fop.o_callback = &cb;
610 fop.ors_limit = NULL;
611 fop.ors_tlimit = SLAP_NO_LIMIT;
612 fop.ors_filter = &cf;
613 fop.ors_filterstr.bv_val = buf;
618 cf.f_choice = LDAP_FILTER_GE;
619 /* If there are multiple CSNs, use the one with our serverID */
620 for ( i=0; i<si->si_numcsns; i++) {
621 if ( slap_serverID == si->si_sids[i] ) {
626 if ( i == si->si_numcsns ) {
627 /* No match: this is multimaster, and none of the content in the DB
628 * originated locally. Treat like no CSN.
630 return LDAP_NO_SUCH_OBJECT;
632 cf.f_av_value = si->si_ctxcsn[maxid];
633 fop.ors_filterstr.bv_len = snprintf( buf, sizeof( buf ),
634 "(entryCSN>=%s)", cf.f_av_value.bv_val );
635 if ( fop.ors_filterstr.bv_len >= sizeof( buf ) ) {
638 fop.ors_attrsonly = 0;
639 fop.ors_attrs = csn_anlist;
640 fop.ors_slimit = SLAP_NO_LIMIT;
641 cb.sc_private = &maxcsn;
642 cb.sc_response = findmax_cb;
643 strcpy( cbuf, cf.f_av_value.bv_val );
644 maxcsn.bv_val = cbuf;
645 maxcsn.bv_len = cf.f_av_value.bv_len;
648 if ( BER_BVISEMPTY( &cf.f_av_value )) {
649 cf.f_av_value = srs->sr_state.ctxcsn[0];
650 /* If there are multiple CSNs, use the smallest */
651 for ( i=1; i<srs->sr_state.numcsns; i++ ) {
652 if ( ber_bvcmp( &cf.f_av_value, &srs->sr_state.ctxcsn[i] )
654 cf.f_av_value = srs->sr_state.ctxcsn[i];
658 /* Look for exact match the first time */
659 if ( findcsn_retry ) {
660 cf.f_choice = LDAP_FILTER_EQUALITY;
661 fop.ors_filterstr.bv_len = snprintf( buf, sizeof( buf ),
662 "(entryCSN=%s)", cf.f_av_value.bv_val );
663 /* On retry, look for <= */
665 cf.f_choice = LDAP_FILTER_LE;
666 fop.ors_limit = &fc_limits;
667 memset( &fc_limits, 0, sizeof( fc_limits ));
668 fc_limits.lms_s_unchecked = 1;
669 fop.ors_filterstr.bv_len = snprintf( buf, sizeof( buf ),
670 "(entryCSN<=%s)", cf.f_av_value.bv_val );
672 if ( fop.ors_filterstr.bv_len >= sizeof( buf ) ) {
675 fop.ors_attrsonly = 1;
676 fop.ors_attrs = slap_anlist_no_attrs;
678 cb.sc_private = NULL;
679 cb.sc_response = findcsn_cb;
682 fop.ors_filter = op->ors_filter;
683 fop.ors_filterstr = op->ors_filterstr;
684 fop.ors_attrsonly = 0;
685 fop.ors_attrs = uuid_anlist;
686 fop.ors_slimit = SLAP_NO_LIMIT;
687 cb.sc_private = &pcookie;
688 cb.sc_response = findpres_cb;
691 /* preallocate storage for a full set */
692 pcookie.uuids = op->o_tmpalloc( (SLAP_SYNCUUID_SET_SIZE+1) *
693 sizeof(struct berval) + SLAP_SYNCUUID_SET_SIZE * UUID_LEN,
695 pcookie.last = (char *)(pcookie.uuids + SLAP_SYNCUUID_SET_SIZE+1);
696 pcookie.uuids[0].bv_val = pcookie.last;
697 pcookie.uuids[0].bv_len = UUID_LEN;
698 for (i=1; i<SLAP_SYNCUUID_SET_SIZE; i++) {
699 pcookie.uuids[i].bv_val = pcookie.uuids[i-1].bv_val + UUID_LEN;
700 pcookie.uuids[i].bv_len = UUID_LEN;
705 fop.o_bd->bd_info = (BackendInfo *)on->on_info;
706 fop.o_bd->be_search( &fop, &frs );
707 fop.o_bd->bd_info = (BackendInfo *)on;
711 if ( ber_bvcmp( &si->si_ctxcsn[maxid], &maxcsn )) {
713 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
714 assert( !syn->ssyn_validate( syn, &maxcsn ));
716 ber_bvreplace( &si->si_ctxcsn[maxid], &maxcsn );
717 si->si_numops++; /* ensure a checkpoint */
721 /* If matching CSN was not found, invalidate the context. */
722 if ( !cb.sc_private ) {
723 /* If we didn't find an exact match, then try for <= */
724 if ( findcsn_retry ) {
728 rc = LDAP_NO_SUCH_OBJECT;
732 op->o_tmpfree( pcookie.uuids, op->o_tmpmemctx );
740 syncprov_free_syncop( syncops *so )
742 syncres *sr, *srnext;
743 GroupAssertion *ga, *gnext;
745 ldap_pvt_thread_mutex_lock( &so->s_mutex );
746 if ( --so->s_inuse > 0 ) {
747 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
750 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
751 if ( so->s_flags & PS_IS_DETACHED ) {
752 filter_free( so->s_op->ors_filter );
753 for ( ga = so->s_op->o_groups; ga; ga=gnext ) {
759 ch_free( so->s_base.bv_val );
760 for ( sr=so->s_res; sr; sr=srnext ) {
764 ldap_pvt_thread_mutex_destroy( &so->s_mutex );
768 /* Send a persistent search response */
770 syncprov_sendresp( Operation *op, opcookie *opc, syncops *so,
771 Entry **e, int mode )
773 slap_overinst *on = opc->son;
775 SlapReply rs = { REP_SEARCH };
776 LDAPControl *ctrls[2];
777 struct berval cookie, csns[2];
779 Attribute a_uuid = {0};
781 if ( so->s_op->o_abandon )
782 return SLAPD_ABANDON;
785 csns[0] = opc->sctxcsn;
786 BER_BVZERO( &csns[1] );
787 slap_compose_sync_cookie( op, &cookie, csns, so->s_rid, slap_serverID ? slap_serverID : -1 );
790 if ( so->s_sid > 0 ) {
791 Debug( LDAP_DEBUG_SYNC, "syncprov_sendresp: to=%03x, cookie=%s\n",
792 so->s_sid, cookie.bv_val, 0 );
794 Debug( LDAP_DEBUG_SYNC, "syncprov_sendresp: cookie=%s\n",
795 cookie.bv_val, 0, 0 );
799 e_uuid.e_attrs = &a_uuid;
800 a_uuid.a_desc = slap_schema.si_ad_entryUUID;
801 a_uuid.a_nvals = &opc->suuid;
802 rs.sr_err = syncprov_state_ctrl( op, &rs, &e_uuid,
803 mode, ctrls, 0, 1, &cookie );
804 op->o_tmpfree( cookie.bv_val, op->o_tmpmemctx );
807 op->o_bd->bd_info = (BackendInfo *)on->on_info;
811 if ( rs.sr_entry->e_private )
812 rs.sr_flags = REP_ENTRY_MUSTRELEASE;
813 if ( opc->sreference && so->s_op->o_managedsait <= SLAP_CONTROL_IGNORED ) {
814 rs.sr_ref = get_entry_referrals( op, rs.sr_entry );
815 rs.sr_err = send_search_reference( op, &rs );
816 ber_bvarray_free( rs.sr_ref );
822 case LDAP_SYNC_MODIFY:
824 if ( rs.sr_entry->e_private )
825 rs.sr_flags = REP_ENTRY_MUSTRELEASE;
826 rs.sr_attrs = op->ors_attrs;
827 rs.sr_err = send_search_entry( op, &rs );
831 case LDAP_SYNC_DELETE:
832 e_uuid.e_attrs = NULL;
833 e_uuid.e_name = opc->sdn;
834 e_uuid.e_nname = opc->sndn;
835 rs.sr_entry = &e_uuid;
836 if ( opc->sreference && so->s_op->o_managedsait <= SLAP_CONTROL_IGNORED ) {
837 struct berval bv = BER_BVNULL;
839 rs.sr_err = send_search_reference( op, &rs );
841 rs.sr_err = send_search_entry( op, &rs );
847 /* In case someone else freed it already? */
849 op->o_tmpfree( rs.sr_ctrls[0], op->o_tmpmemctx );
857 syncprov_qstart( syncops *so );
859 /* Play back queued responses */
861 syncprov_qplay( Operation *op, syncops *so )
863 slap_overinst *on = LDAP_SLIST_FIRST(&so->s_op->o_extra)->oe_key;
872 ldap_pvt_thread_mutex_lock( &so->s_mutex );
875 so->s_res = sr->s_next;
877 so->s_restail = NULL;
878 /* Exit loop with mutex held */
879 if ( !sr || so->s_op->o_abandon )
881 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
883 if ( sr->s_mode == LDAP_SYNC_NEW_COOKIE ) {
884 SlapReply rs = { REP_INTERMEDIATE };
886 rc = syncprov_sendinfo( op, &rs, LDAP_TAG_SYNC_NEW_COOKIE,
887 &sr->s_csn, 0, NULL, 0 );
890 opc.sndn = sr->s_ndn;
891 opc.suuid = sr->s_uuid;
892 opc.sctxcsn = sr->s_csn;
893 opc.sreference = sr->s_isreference;
896 if ( sr->s_mode != LDAP_SYNC_DELETE ) {
897 rc = overlay_entry_get_ov( op, &opc.sndn, NULL, NULL, 0, &e, on );
899 Debug( LDAP_DEBUG_SYNC, "syncprov_qplay: failed to get %s, "
900 "error (%d), ignoring...\n", opc.sndn.bv_val, rc, 0 );
906 rc = syncprov_sendresp( op, &opc, so, &e, sr->s_mode );
909 overlay_entry_release_ov( op, e, 0, on );
915 /* Exit loop with mutex held */
916 ldap_pvt_thread_mutex_lock( &so->s_mutex );
920 /* We now only send one change at a time, to prevent one
921 * psearch from hogging all the CPU. Resubmit this task if
922 * there are more responses queued and no errors occurred.
925 if ( rc == 0 && so->s_res ) {
926 syncprov_qstart( so );
928 so->s_flags ^= PS_TASK_QUEUED;
931 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
935 /* task for playing back queued responses */
937 syncprov_qtask( void *ctx, void *arg )
940 OperationBuffer opbuf;
947 op->o_hdr = &opbuf.ob_hdr;
948 op->o_controls = opbuf.ob_controls;
949 memset( op->o_controls, 0, sizeof(opbuf.ob_controls) );
951 *op->o_hdr = *so->s_op->o_hdr;
953 op->o_tmpmemctx = slap_sl_mem_create(SLAP_SLAB_SIZE, SLAP_SLAB_STACK, ctx, 1);
954 op->o_tmpmfuncs = &slap_sl_mfuncs;
955 op->o_threadctx = ctx;
957 /* syncprov_qplay expects a fake db */
958 be = *so->s_op->o_bd;
959 be.be_flags |= SLAP_DBFLAG_OVERLAY;
961 LDAP_SLIST_FIRST(&op->o_extra) = NULL;
962 op->o_callback = NULL;
964 rc = syncprov_qplay( op, so );
966 /* decrement use count... */
967 syncprov_free_syncop( so );
972 /* Start the task to play back queued psearch responses */
974 syncprov_qstart( syncops *so )
976 so->s_flags |= PS_TASK_QUEUED;
978 ldap_pvt_thread_pool_submit( &connection_pool,
979 syncprov_qtask, so );
982 /* Queue a persistent search response */
984 syncprov_qresp( opcookie *opc, syncops *so, int mode )
988 struct berval cookie = opc->sctxcsn;
990 if ( mode == LDAP_SYNC_NEW_COOKIE ) {
991 syncprov_info_t *si = opc->son->on_bi.bi_private;
993 slap_compose_sync_cookie( NULL, &cookie, si->si_ctxcsn,
994 so->s_rid, slap_serverID ? slap_serverID : -1);
997 srsize = sizeof(syncres) + opc->suuid.bv_len + 1 +
998 opc->sdn.bv_len + 1 + opc->sndn.bv_len + 1;
1000 srsize += cookie.bv_len + 1;
1001 sr = ch_malloc( srsize );
1003 sr->s_dn.bv_val = (char *)(sr + 1);
1004 sr->s_dn.bv_len = opc->sdn.bv_len;
1006 sr->s_isreference = opc->sreference;
1007 sr->s_ndn.bv_val = lutil_strcopy( sr->s_dn.bv_val,
1008 opc->sdn.bv_val ) + 1;
1009 sr->s_ndn.bv_len = opc->sndn.bv_len;
1010 sr->s_uuid.bv_val = lutil_strcopy( sr->s_ndn.bv_val,
1011 opc->sndn.bv_val ) + 1;
1012 sr->s_uuid.bv_len = opc->suuid.bv_len;
1013 AC_MEMCPY( sr->s_uuid.bv_val, opc->suuid.bv_val, opc->suuid.bv_len );
1014 if ( cookie.bv_len ) {
1015 sr->s_csn.bv_val = sr->s_uuid.bv_val + sr->s_uuid.bv_len + 1;
1016 strcpy( sr->s_csn.bv_val, cookie.bv_val );
1018 sr->s_csn.bv_val = NULL;
1020 sr->s_csn.bv_len = cookie.bv_len;
1022 if ( mode == LDAP_SYNC_NEW_COOKIE && cookie.bv_val ) {
1023 ch_free( cookie.bv_val );
1026 ldap_pvt_thread_mutex_lock( &so->s_mutex );
1030 so->s_restail->s_next = sr;
1034 /* If the base of the psearch was modified, check it next time round */
1035 if ( so->s_flags & PS_WROTE_BASE ) {
1036 so->s_flags ^= PS_WROTE_BASE;
1037 so->s_flags |= PS_FIND_BASE;
1039 if (( so->s_flags & (PS_IS_DETACHED|PS_TASK_QUEUED)) == PS_IS_DETACHED ) {
1040 syncprov_qstart( so );
1042 ldap_pvt_thread_mutex_unlock( &so->s_mutex );
1043 return LDAP_SUCCESS;
1047 syncprov_drop_psearch( syncops *so, int lock )
1049 if ( so->s_flags & PS_IS_DETACHED ) {
1051 ldap_pvt_thread_mutex_lock( &so->s_op->o_conn->c_mutex );
1052 so->s_op->o_conn->c_n_ops_executing--;
1053 so->s_op->o_conn->c_n_ops_completed++;
1054 LDAP_STAILQ_REMOVE( &so->s_op->o_conn->c_ops, so->s_op, Operation,
1057 ldap_pvt_thread_mutex_unlock( &so->s_op->o_conn->c_mutex );
1059 syncprov_free_syncop( so );
1065 syncprov_ab_cleanup( Operation *op, SlapReply *rs )
1067 slap_callback *sc = op->o_callback;
1068 op->o_callback = sc->sc_next;
1069 syncprov_drop_psearch( op->o_callback->sc_private, 0 );
1070 op->o_tmpfree( sc, op->o_tmpmemctx );
1075 syncprov_op_abandon( Operation *op, SlapReply *rs )
1077 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1078 syncprov_info_t *si = on->on_bi.bi_private;
1079 syncops *so, *soprev;
1081 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
1082 for ( so=si->si_ops, soprev = (syncops *)&si->si_ops; so;
1083 soprev=so, so=so->s_next ) {
1084 if ( so->s_op->o_connid == op->o_connid &&
1085 so->s_op->o_msgid == op->orn_msgid ) {
1086 so->s_op->o_abandon = 1;
1087 soprev->s_next = so->s_next;
1091 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
1093 /* Is this really a Cancel exop? */
1094 if ( op->o_tag != LDAP_REQ_ABANDON ) {
1095 so->s_op->o_cancel = SLAP_CANCEL_ACK;
1096 rs->sr_err = LDAP_CANCELLED;
1097 send_ldap_result( so->s_op, rs );
1098 if ( so->s_flags & PS_IS_DETACHED ) {
1100 cb = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
1101 cb->sc_cleanup = syncprov_ab_cleanup;
1102 cb->sc_next = op->o_callback;
1103 cb->sc_private = so;
1104 return SLAP_CB_CONTINUE;
1107 syncprov_drop_psearch( so, 0 );
1109 return SLAP_CB_CONTINUE;
1112 /* Find which persistent searches are affected by this operation */
1114 syncprov_matchops( Operation *op, opcookie *opc, int saveit )
1116 slap_overinst *on = opc->son;
1117 syncprov_info_t *si = on->on_bi.bi_private;
1120 syncops *ss, *sprev, *snext;
1124 struct berval newdn;
1126 BackendDB *b0 = op->o_bd, db;
1128 fc.fdn = &op->o_req_ndn;
1129 /* compute new DN */
1130 if ( op->o_tag == LDAP_REQ_MODRDN && !saveit ) {
1132 if ( op->orr_nnewSup ) pdn = *op->orr_nnewSup;
1133 else dnParent( fc.fdn, &pdn );
1134 build_new_dn( &newdn, &pdn, &op->orr_nnewrdn, op->o_tmpmemctx );
1138 if ( op->o_tag != LDAP_REQ_ADD ) {
1139 if ( !SLAP_ISOVERLAY( op->o_bd )) {
1143 rc = overlay_entry_get_ov( op, fc.fdn, NULL, NULL, 0, &e, on );
1144 /* If we're sending responses now, make a copy and unlock the DB */
1145 if ( e && !saveit ) {
1146 Entry *e2 = entry_dup( e );
1147 overlay_entry_release_ov( op, e, 0, on );
1158 if ( saveit || op->o_tag == LDAP_REQ_ADD ) {
1159 ber_dupbv_x( &opc->sdn, &e->e_name, op->o_tmpmemctx );
1160 ber_dupbv_x( &opc->sndn, &e->e_nname, op->o_tmpmemctx );
1161 opc->sreference = is_entry_referral( e );
1162 a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
1164 ber_dupbv_x( &opc->suuid, &a->a_nvals[0], op->o_tmpmemctx );
1165 } else if ( op->o_tag == LDAP_REQ_MODRDN && !saveit ) {
1166 op->o_tmpfree( opc->sndn.bv_val, op->o_tmpmemctx );
1167 op->o_tmpfree( opc->sdn.bv_val, op->o_tmpmemctx );
1168 ber_dupbv_x( &opc->sdn, &e->e_name, op->o_tmpmemctx );
1169 ber_dupbv_x( &opc->sndn, &e->e_nname, op->o_tmpmemctx );
1172 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
1173 for (ss = si->si_ops, sprev = (syncops *)&si->si_ops; ss;
1174 sprev = ss, ss=snext)
1182 if ( ss->s_op->o_abandon )
1185 /* First time thru, check for possible skips */
1186 if ( saveit || op->o_tag == LDAP_REQ_ADD ) {
1188 /* Don't send ops back to the originator */
1189 if ( opc->osid > 0 && opc->osid == ss->s_sid ) {
1190 Debug( LDAP_DEBUG_SYNC, "syncprov_matchops: skipping original sid %03x\n",
1195 /* Don't send ops back to the messenger */
1196 if ( opc->rsid > 0 && opc->rsid == ss->s_sid ) {
1197 Debug( LDAP_DEBUG_SYNC, "syncprov_matchops: skipping relayed sid %03x\n",
1208 /* If the base of the search is missing, signal a refresh */
1209 rc = syncprov_findbase( op, &fc );
1210 if ( rc != LDAP_SUCCESS ) {
1211 SlapReply rs = {REP_RESULT};
1212 send_ldap_error( ss->s_op, &rs, LDAP_SYNC_REFRESH_REQUIRED,
1213 "search base has changed" );
1214 sprev->s_next = snext;
1215 syncprov_drop_psearch( ss, 1 );
1221 /* If we're sending results now, look for this op in old matches */
1225 /* Did we modify the search base? */
1226 if ( dn_match( &op->o_req_ndn, &ss->s_base )) {
1227 ldap_pvt_thread_mutex_lock( &ss->s_mutex );
1228 ss->s_flags |= PS_WROTE_BASE;
1229 ldap_pvt_thread_mutex_unlock( &ss->s_mutex );
1232 for ( sm=opc->smatches, old=(syncmatches *)&opc->smatches; sm;
1233 old=sm, sm=sm->sm_next ) {
1234 if ( sm->sm_op == ss ) {
1236 old->sm_next = sm->sm_next;
1237 op->o_tmpfree( sm, op->o_tmpmemctx );
1246 oh.oh_conn = ss->s_op->o_conn;
1247 oh.oh_connid = ss->s_op->o_connid;
1249 op2.o_extra = op->o_extra;
1250 rc = test_filter( &op2, e, ss->s_op->ors_filter );
1253 Debug( LDAP_DEBUG_TRACE, "syncprov_matchops: sid %03x fscope %d rc %d\n",
1254 ss->s_sid, fc.fscope, rc );
1256 /* check if current o_req_dn is in scope and matches filter */
1257 if ( fc.fscope && rc == LDAP_COMPARE_TRUE ) {
1259 sm = op->o_tmpalloc( sizeof(syncmatches), op->o_tmpmemctx );
1260 sm->sm_next = opc->smatches;
1262 ldap_pvt_thread_mutex_lock( &ss->s_mutex );
1264 ldap_pvt_thread_mutex_unlock( &ss->s_mutex );
1267 /* if found send UPDATE else send ADD */
1268 syncprov_qresp( opc, ss,
1269 found ? LDAP_SYNC_MODIFY : LDAP_SYNC_ADD );
1271 } else if ( !saveit && found ) {
1273 syncprov_qresp( opc, ss, LDAP_SYNC_DELETE );
1274 } else if ( !saveit ) {
1275 syncprov_qresp( opc, ss, LDAP_SYNC_NEW_COOKIE );
1277 if ( !saveit && found ) {
1278 /* Decrement s_inuse, was incremented when called
1279 * with saveit == TRUE
1281 syncprov_free_syncop( ss );
1284 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
1286 if ( op->o_tag != LDAP_REQ_ADD && e ) {
1287 if ( !SLAP_ISOVERLAY( op->o_bd )) {
1290 overlay_entry_release_ov( op, e, 0, on );
1294 op->o_tmpfree( fc.fdn->bv_val, op->o_tmpmemctx );
1300 syncprov_op_cleanup( Operation *op, SlapReply *rs )
1302 slap_callback *cb = op->o_callback;
1303 opcookie *opc = cb->sc_private;
1304 slap_overinst *on = opc->son;
1305 syncprov_info_t *si = on->on_bi.bi_private;
1306 syncmatches *sm, *snext;
1307 modtarget *mt, mtdummy;
1309 for (sm = opc->smatches; sm; sm=snext) {
1310 snext = sm->sm_next;
1311 syncprov_free_syncop( sm->sm_op );
1312 op->o_tmpfree( sm, op->o_tmpmemctx );
1315 /* Remove op from lock table */
1317 ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
1318 mt = avl_find( si->si_mods, &mtdummy, sp_avl_cmp );
1320 modinst *mi = mt->mt_mods;
1322 /* If there are more, promote the next one */
1323 ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
1324 if ( mi->mi_next ) {
1325 mt->mt_mods = mi->mi_next;
1326 mt->mt_op = mt->mt_mods->mi_op;
1327 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
1329 avl_delete( &si->si_mods, mt, sp_avl_cmp );
1330 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
1331 ldap_pvt_thread_mutex_destroy( &mt->mt_mutex );
1335 ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
1336 if ( !BER_BVISNULL( &opc->suuid ))
1337 op->o_tmpfree( opc->suuid.bv_val, op->o_tmpmemctx );
1338 if ( !BER_BVISNULL( &opc->sndn ))
1339 op->o_tmpfree( opc->sndn.bv_val, op->o_tmpmemctx );
1340 if ( !BER_BVISNULL( &opc->sdn ))
1341 op->o_tmpfree( opc->sdn.bv_val, op->o_tmpmemctx );
1342 op->o_callback = cb->sc_next;
1343 op->o_tmpfree(cb, op->o_tmpmemctx);
1349 syncprov_checkpoint( Operation *op, SlapReply *rs, slap_overinst *on )
1351 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
1354 SlapReply rsm = { 0 };
1355 slap_callback cb = {0};
1358 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
1361 for ( i=0; i<si->si_numcsns; i++ ) {
1362 assert( !syn->ssyn_validate( syn, si->si_ctxcsn+i ));
1365 mod.sml_numvals = si->si_numcsns;
1366 mod.sml_values = si->si_ctxcsn;
1367 mod.sml_nvalues = NULL;
1368 mod.sml_desc = slap_schema.si_ad_contextCSN;
1369 mod.sml_op = LDAP_MOD_REPLACE;
1370 mod.sml_flags = SLAP_MOD_INTERNAL;
1371 mod.sml_next = NULL;
1373 cb.sc_response = slap_null_cb;
1375 opm.o_tag = LDAP_REQ_MODIFY;
1376 opm.o_callback = &cb;
1377 opm.orm_modlist = &mod;
1378 opm.orm_no_opattrs = 1;
1379 if ( SLAP_GLUE_SUBORDINATE( op->o_bd )) {
1380 be = *on->on_info->oi_origdb;
1383 opm.o_req_dn = opm.o_bd->be_suffix[0];
1384 opm.o_req_ndn = opm.o_bd->be_nsuffix[0];
1385 opm.o_bd->bd_info = on->on_info->oi_orig;
1386 opm.o_managedsait = SLAP_CONTROL_NONCRITICAL;
1387 opm.o_no_schema_check = 1;
1388 opm.o_bd->be_modify( &opm, &rsm );
1389 if ( mod.sml_next != NULL ) {
1390 slap_mods_free( mod.sml_next, 1 );
1393 for ( i=0; i<si->si_numcsns; i++ ) {
1394 assert( !syn->ssyn_validate( syn, si->si_ctxcsn+i ));
1400 syncprov_add_slog( Operation *op )
1402 opcookie *opc = op->o_callback->sc_private;
1403 slap_overinst *on = opc->son;
1404 syncprov_info_t *si = on->on_bi.bi_private;
1410 /* Allocate a record. UUIDs are not NUL-terminated. */
1411 se = ch_malloc( sizeof( slog_entry ) + opc->suuid.bv_len +
1412 op->o_csn.bv_len + 1 );
1414 se->se_tag = op->o_tag;
1416 se->se_uuid.bv_val = (char *)(&se[1]);
1417 AC_MEMCPY( se->se_uuid.bv_val, opc->suuid.bv_val, opc->suuid.bv_len );
1418 se->se_uuid.bv_len = opc->suuid.bv_len;
1420 se->se_csn.bv_val = se->se_uuid.bv_val + opc->suuid.bv_len;
1421 AC_MEMCPY( se->se_csn.bv_val, op->o_csn.bv_val, op->o_csn.bv_len );
1422 se->se_csn.bv_val[op->o_csn.bv_len] = '\0';
1423 se->se_csn.bv_len = op->o_csn.bv_len;
1424 se->se_sid = slap_parse_csn_sid( &se->se_csn );
1426 ldap_pvt_thread_mutex_lock( &sl->sl_mutex );
1427 if ( sl->sl_head ) {
1428 sl->sl_tail->se_next = se;
1434 while ( sl->sl_num > sl->sl_size ) {
1436 sl->sl_head = se->se_next;
1437 strcpy( sl->sl_mincsn.bv_val, se->se_csn.bv_val );
1438 sl->sl_mincsn.bv_len = se->se_csn.bv_len;
1442 ldap_pvt_thread_mutex_unlock( &sl->sl_mutex );
1446 /* Just set a flag if we found the matching entry */
1448 playlog_cb( Operation *op, SlapReply *rs )
1450 if ( rs->sr_type == REP_SEARCH ) {
1451 op->o_callback->sc_private = (void *)1;
1456 /* enter with sl->sl_mutex locked, release before returning */
1458 syncprov_playlog( Operation *op, SlapReply *rs, sessionlog *sl,
1459 sync_control *srs, BerVarray ctxcsn, int numcsns, int *sids )
1461 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1463 int i, j, ndel, num, nmods, mmods;
1464 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
1466 struct berval delcsn[2];
1468 if ( !sl->sl_num ) {
1469 ldap_pvt_thread_mutex_unlock( &sl->sl_mutex );
1477 uuids = op->o_tmpalloc( (num+1) * sizeof( struct berval ) +
1478 num * UUID_LEN, op->o_tmpmemctx );
1479 uuids[0].bv_val = (char *)(uuids + num + 1);
1481 delcsn[0].bv_len = 0;
1482 delcsn[0].bv_val = cbuf;
1483 BER_BVZERO(&delcsn[1]);
1485 /* Make a copy of the relevant UUIDs. Put the Deletes up front
1486 * and everything else at the end. Do this first so we can
1487 * unlock the list mutex.
1489 Debug( LDAP_DEBUG_SYNC, "srs csn %s\n",
1490 srs->sr_state.ctxcsn[0].bv_val, 0, 0 );
1491 for ( se=sl->sl_head; se; se=se->se_next ) {
1493 Debug( LDAP_DEBUG_SYNC, "log csn %s\n", se->se_csn.bv_val, 0, 0 );
1495 for ( k=0; k<srs->sr_state.numcsns; k++ ) {
1496 if ( se->se_sid == srs->sr_state.sids[k] ) {
1497 ndel = ber_bvcmp( &se->se_csn, &srs->sr_state.ctxcsn[k] );
1502 Debug( LDAP_DEBUG_SYNC, "cmp %d, too old\n", ndel, 0, 0 );
1506 for ( k=0; k<numcsns; k++ ) {
1507 if ( se->se_sid == sids[k] ) {
1508 ndel = ber_bvcmp( &se->se_csn, &ctxcsn[k] );
1513 Debug( LDAP_DEBUG_SYNC, "cmp %d, too new\n", ndel, 0, 0 );
1516 if ( se->se_tag == LDAP_REQ_DELETE ) {
1519 AC_MEMCPY( cbuf, se->se_csn.bv_val, se->se_csn.bv_len );
1520 delcsn[0].bv_len = se->se_csn.bv_len;
1521 delcsn[0].bv_val[delcsn[0].bv_len] = '\0';
1526 uuids[j].bv_val = uuids[0].bv_val + (j * UUID_LEN);
1527 AC_MEMCPY(uuids[j].bv_val, se->se_uuid.bv_val, UUID_LEN);
1528 uuids[j].bv_len = UUID_LEN;
1530 ldap_pvt_thread_mutex_unlock( &sl->sl_mutex );
1534 /* Zero out unused slots */
1535 for ( i=ndel; i < num - nmods; i++ )
1536 uuids[i].bv_len = 0;
1538 /* Mods must be validated to see if they belong in this delete set.
1542 /* Strip any duplicates */
1543 for ( i=0; i<nmods; i++ ) {
1544 for ( j=0; j<ndel; j++ ) {
1545 if ( bvmatch( &uuids[j], &uuids[num - 1 - i] )) {
1546 uuids[num - 1 - i].bv_len = 0;
1551 if ( uuids[num - 1 - i].bv_len == 0 ) continue;
1552 for ( j=0; j<i; j++ ) {
1553 if ( bvmatch( &uuids[num - 1 - j], &uuids[num - 1 - i] )) {
1554 uuids[num - 1 - i].bv_len = 0;
1563 SlapReply frs = { REP_RESULT };
1566 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
1567 slap_callback cb = {0};
1571 fop.o_sync_mode = 0;
1572 fop.o_callback = &cb;
1573 fop.ors_limit = NULL;
1574 fop.ors_tlimit = SLAP_NO_LIMIT;
1575 fop.ors_attrs = slap_anlist_all_attributes;
1576 fop.ors_attrsonly = 0;
1577 fop.o_managedsait = SLAP_CONTROL_CRITICAL;
1579 af.f_choice = LDAP_FILTER_AND;
1582 mf.f_choice = LDAP_FILTER_EQUALITY;
1584 mf.f_av_desc = slap_schema.si_ad_entryUUID;
1585 mf.f_next = fop.ors_filter;
1587 fop.ors_filter = ⁡
1589 cb.sc_response = playlog_cb;
1590 fop.o_bd->bd_info = (BackendInfo *)on->on_info;
1592 for ( i=ndel; i<num; i++ ) {
1593 if ( uuids[i].bv_len == 0 ) continue;
1595 mf.f_av_value = uuids[i];
1596 cb.sc_private = NULL;
1598 frs.sr_nentries = 0;
1599 rc = fop.o_bd->be_search( &fop, &frs );
1601 /* If entry was not found, add to delete list */
1602 if ( !cb.sc_private ) {
1603 uuids[ndel++] = uuids[i];
1606 fop.o_bd->bd_info = (BackendInfo *)on;
1609 struct berval cookie;
1611 if ( delcsn[0].bv_len ) {
1612 slap_compose_sync_cookie( op, &cookie, delcsn, srs->sr_state.rid,
1613 slap_serverID ? slap_serverID : -1 );
1615 Debug( LDAP_DEBUG_SYNC, "syncprov_playlog: cookie=%s\n", cookie.bv_val, 0, 0 );
1618 uuids[ndel].bv_val = NULL;
1619 syncprov_sendinfo( op, rs, LDAP_TAG_SYNC_ID_SET,
1620 delcsn[0].bv_len ? &cookie : NULL, 0, uuids, 1 );
1621 if ( delcsn[0].bv_len ) {
1622 op->o_tmpfree( cookie.bv_val, op->o_tmpmemctx );
1625 op->o_tmpfree( uuids, op->o_tmpmemctx );
1629 syncprov_op_response( Operation *op, SlapReply *rs )
1631 opcookie *opc = op->o_callback->sc_private;
1632 slap_overinst *on = opc->son;
1633 syncprov_info_t *si = on->on_bi.bi_private;
1636 if ( rs->sr_err == LDAP_SUCCESS )
1638 struct berval maxcsn;
1639 char cbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
1640 int do_check = 0, have_psearches, foundit, csn_changed = 0;
1642 /* Update our context CSN */
1644 maxcsn.bv_val = cbuf;
1645 maxcsn.bv_len = sizeof(cbuf);
1646 ldap_pvt_thread_rdwr_wlock( &si->si_csn_rwlock );
1648 slap_get_commit_csn( op, &maxcsn, &foundit );
1649 if ( BER_BVISEMPTY( &maxcsn ) && SLAP_GLUE_SUBORDINATE( op->o_bd )) {
1650 /* syncrepl queues the CSN values in the db where
1651 * it is configured , not where the changes are made.
1652 * So look for a value in the glue db if we didn't
1653 * find any in this db.
1655 BackendDB *be = op->o_bd;
1656 op->o_bd = select_backend( &be->be_nsuffix[0], 1);
1657 maxcsn.bv_val = cbuf;
1658 maxcsn.bv_len = sizeof(cbuf);
1659 slap_get_commit_csn( op, &maxcsn, &foundit );
1662 if ( !BER_BVISEMPTY( &maxcsn ) ) {
1665 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
1666 assert( !syn->ssyn_validate( syn, &maxcsn ));
1668 sid = slap_parse_csn_sid( &maxcsn );
1669 for ( i=0; i<si->si_numcsns; i++ ) {
1670 if ( sid == si->si_sids[i] ) {
1671 if ( ber_bvcmp( &maxcsn, &si->si_ctxcsn[i] ) > 0 ) {
1672 ber_bvreplace( &si->si_ctxcsn[i], &maxcsn );
1678 /* It's a new SID for us */
1679 if ( i == si->si_numcsns ) {
1680 value_add_one( &si->si_ctxcsn, &maxcsn );
1683 si->si_sids = ch_realloc( si->si_sids, si->si_numcsns *
1685 si->si_sids[i] = sid;
1687 } else if ( !foundit ) {
1688 /* internal ops that aren't meant to be replicated */
1689 ldap_pvt_thread_rdwr_wunlock( &si->si_csn_rwlock );
1690 return SLAP_CB_CONTINUE;
1693 /* Don't do any processing for consumer contextCSN updates */
1694 if ( op->o_dont_replicate ) {
1695 ldap_pvt_thread_rdwr_wunlock( &si->si_csn_rwlock );
1696 return SLAP_CB_CONTINUE;
1700 if ( si->si_chkops || si->si_chktime ) {
1701 if ( si->si_chkops && si->si_numops >= si->si_chkops ) {
1705 if ( si->si_chktime &&
1706 (op->o_time - si->si_chklast >= si->si_chktime )) {
1707 if ( si->si_chklast ) {
1709 si->si_chklast = op->o_time;
1715 ldap_pvt_thread_rdwr_wunlock( &si->si_csn_rwlock );
1718 ldap_pvt_thread_rdwr_rlock( &si->si_csn_rwlock );
1719 syncprov_checkpoint( op, rs, on );
1720 ldap_pvt_thread_rdwr_runlock( &si->si_csn_rwlock );
1723 /* only update consumer ctx if this is a newer csn */
1724 if ( csn_changed ) {
1725 opc->sctxcsn = maxcsn;
1728 /* Handle any persistent searches */
1729 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
1730 have_psearches = ( si->si_ops != NULL );
1731 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
1732 if ( have_psearches ) {
1735 case LDAP_REQ_MODIFY:
1736 case LDAP_REQ_MODRDN:
1737 case LDAP_REQ_EXTENDED:
1738 syncprov_matchops( op, opc, 0 );
1740 case LDAP_REQ_DELETE:
1741 /* for each match in opc->smatches:
1744 for ( sm = opc->smatches; sm; sm=sm->sm_next ) {
1745 if ( sm->sm_op->s_op->o_abandon )
1747 syncprov_qresp( opc, sm->sm_op, LDAP_SYNC_DELETE );
1753 /* Add any log records */
1754 if ( si->si_logs && op->o_tag != LDAP_REQ_ADD ) {
1755 syncprov_add_slog( op );
1759 return SLAP_CB_CONTINUE;
1762 /* We don't use a subentry to store the context CSN any more.
1763 * We expose the current context CSN as an operational attribute
1764 * of the suffix entry.
1767 syncprov_op_compare( Operation *op, SlapReply *rs )
1769 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1770 syncprov_info_t *si = on->on_bi.bi_private;
1771 int rc = SLAP_CB_CONTINUE;
1773 if ( dn_match( &op->o_req_ndn, op->o_bd->be_nsuffix ) &&
1774 op->oq_compare.rs_ava->aa_desc == slap_schema.si_ad_contextCSN )
1779 e.e_name = op->o_bd->be_suffix[0];
1780 e.e_nname = op->o_bd->be_nsuffix[0];
1783 a.a_desc = slap_schema.si_ad_contextCSN;
1785 ldap_pvt_thread_rdwr_rlock( &si->si_csn_rwlock );
1787 a.a_vals = si->si_ctxcsn;
1788 a.a_nvals = a.a_vals;
1789 a.a_numvals = si->si_numcsns;
1791 rs->sr_err = access_allowed( op, &e, op->oq_compare.rs_ava->aa_desc,
1792 &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
1793 if ( ! rs->sr_err ) {
1794 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
1795 goto return_results;
1798 if ( get_assert( op ) &&
1799 ( test_filter( op, &e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
1801 rs->sr_err = LDAP_ASSERTION_FAILED;
1802 goto return_results;
1806 rs->sr_err = LDAP_COMPARE_FALSE;
1808 if ( attr_valfind( &a,
1809 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
1810 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
1811 &op->oq_compare.rs_ava->aa_value, NULL, op->o_tmpmemctx ) == 0 )
1813 rs->sr_err = LDAP_COMPARE_TRUE;
1818 ldap_pvt_thread_rdwr_runlock( &si->si_csn_rwlock );
1820 send_ldap_result( op, rs );
1822 if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
1823 rs->sr_err = LDAP_SUCCESS;
1832 syncprov_op_mod( Operation *op, SlapReply *rs )
1834 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1835 syncprov_info_t *si = on->on_bi.bi_private;
1838 int have_psearches, cbsize;
1840 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
1841 have_psearches = ( si->si_ops != NULL );
1842 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
1844 cbsize = sizeof(slap_callback) + sizeof(opcookie) +
1845 (have_psearches ? sizeof(modinst) : 0 );
1847 cb = op->o_tmpcalloc(1, cbsize, op->o_tmpmemctx);
1848 opc = (opcookie *)(cb+1);
1850 cb->sc_response = syncprov_op_response;
1851 cb->sc_cleanup = syncprov_op_cleanup;
1852 cb->sc_private = opc;
1853 cb->sc_next = op->o_callback;
1854 op->o_callback = cb;
1858 if ( op->o_csn.bv_val ) {
1859 opc->osid = slap_parse_csn_sid( &op->o_csn );
1861 if ( op->o_controls ) {
1862 struct sync_cookie *scook =
1863 op->o_controls[slap_cids.sc_LDAPsync];
1865 opc->rsid = scook->sid;
1868 /* If there are active persistent searches, lock this operation.
1869 * See seqmod.c for the locking logic on its own.
1871 if ( have_psearches ) {
1872 modtarget *mt, mtdummy;
1875 mi = (modinst *)(opc+1);
1878 /* See if we're already modifying this entry... */
1880 ldap_pvt_thread_mutex_lock( &si->si_mods_mutex );
1881 mt = avl_find( si->si_mods, &mtdummy, sp_avl_cmp );
1883 ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
1884 ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
1885 mt->mt_tail->mi_next = mi;
1887 /* wait for this op to get to head of list */
1888 while ( mt->mt_mods != mi ) {
1889 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
1890 /* FIXME: if dynamic config can delete overlays or
1891 * databases we'll have to check for cleanup here.
1892 * Currently it's not an issue because there are
1893 * no dynamic config deletes...
1895 if ( slapd_shutdown )
1896 return SLAPD_ABANDON;
1898 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1899 ldap_pvt_thread_yield();
1900 ldap_pvt_thread_mutex_lock( &mt->mt_mutex );
1902 /* clean up if the caller is giving up */
1903 if ( op->o_abandon ) {
1905 for ( m2 = mt->mt_mods; m2->mi_next != mi;
1907 m2->mi_next = mi->mi_next;
1908 if ( mt->mt_tail == mi ) mt->mt_tail = m2;
1909 op->o_tmpfree( cb, op->o_tmpmemctx );
1910 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
1911 return SLAPD_ABANDON;
1914 ldap_pvt_thread_mutex_unlock( &mt->mt_mutex );
1916 /* Record that we're modifying this entry now */
1917 mt = ch_malloc( sizeof(modtarget) );
1920 mt->mt_op = mi->mi_op;
1921 ldap_pvt_thread_mutex_init( &mt->mt_mutex );
1922 avl_insert( &si->si_mods, mt, sp_avl_cmp, avl_dup_error );
1923 ldap_pvt_thread_mutex_unlock( &si->si_mods_mutex );
1927 if (( have_psearches || si->si_logs ) && op->o_tag != LDAP_REQ_ADD )
1928 syncprov_matchops( op, opc, 1 );
1930 return SLAP_CB_CONTINUE;
1934 syncprov_op_extended( Operation *op, SlapReply *rs )
1936 if ( exop_is_write( op ))
1937 return syncprov_op_mod( op, rs );
1939 return SLAP_CB_CONTINUE;
1942 typedef struct searchstate {
1943 slap_overinst *ss_on;
1945 BerVarray ss_ctxcsn;
1948 #define SS_PRESENT 0x01
1949 #define SS_CHANGED 0x02
1954 syncprov_search_cleanup( Operation *op, SlapReply *rs )
1956 if ( rs->sr_ctrls ) {
1957 op->o_tmpfree( rs->sr_ctrls[0], op->o_tmpmemctx );
1958 op->o_tmpfree( rs->sr_ctrls, op->o_tmpmemctx );
1959 rs->sr_ctrls = NULL;
1964 typedef struct SyncOperationBuffer {
1968 AttributeName sob_extra; /* not always present */
1969 /* Further data allocated here */
1970 } SyncOperationBuffer;
1973 syncprov_detach_op( Operation *op, syncops *so, slap_overinst *on )
1975 SyncOperationBuffer *sopbuf2;
1980 GroupAssertion *g1, *g2;
1982 /* count the search attrs */
1983 for (i=0; op->ors_attrs && !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++) {
1984 alen += op->ors_attrs[i].an_name.bv_len + 1;
1986 /* Make a new copy of the operation */
1987 size = offsetof( SyncOperationBuffer, sob_extra ) +
1988 (i ? ( (i+1) * sizeof(AttributeName) + alen) : 0) +
1989 op->o_req_dn.bv_len + 1 +
1990 op->o_req_ndn.bv_len + 1 +
1991 op->o_ndn.bv_len + 1 +
1992 so->s_filterstr.bv_len + 1;
1993 sopbuf2 = ch_calloc( 1, size );
1994 op2 = &sopbuf2->sob_op;
1995 op2->o_hdr = &sopbuf2->sob_hdr;
1996 LDAP_SLIST_FIRST(&op2->o_extra) = &sopbuf2->sob_oe;
1998 /* Copy the fields we care about explicitly, leave the rest alone */
1999 *op2->o_hdr = *op->o_hdr;
2000 op2->o_tag = op->o_tag;
2001 op2->o_time = op->o_time;
2002 op2->o_bd = on->on_info->oi_origdb;
2003 op2->o_request = op->o_request;
2004 op2->o_managedsait = op->o_managedsait;
2005 LDAP_SLIST_FIRST(&op2->o_extra)->oe_key = on;
2006 LDAP_SLIST_NEXT(LDAP_SLIST_FIRST(&op2->o_extra), oe_next) = NULL;
2008 ptr = (char *) sopbuf2 + offsetof( SyncOperationBuffer, sob_extra );
2010 op2->ors_attrs = (AttributeName *) ptr;
2011 ptr = (char *) &op2->ors_attrs[i+1];
2012 for (i=0; !BER_BVISNULL( &op->ors_attrs[i].an_name ); i++) {
2013 op2->ors_attrs[i] = op->ors_attrs[i];
2014 op2->ors_attrs[i].an_name.bv_val = ptr;
2015 ptr = lutil_strcopy( ptr, op->ors_attrs[i].an_name.bv_val ) + 1;
2017 BER_BVZERO( &op2->ors_attrs[i].an_name );
2020 op2->o_authz = op->o_authz;
2021 op2->o_ndn.bv_val = ptr;
2022 ptr = lutil_strcopy(ptr, op->o_ndn.bv_val) + 1;
2023 op2->o_dn = op2->o_ndn;
2024 op2->o_req_dn.bv_len = op->o_req_dn.bv_len;
2025 op2->o_req_dn.bv_val = ptr;
2026 ptr = lutil_strcopy(ptr, op->o_req_dn.bv_val) + 1;
2027 op2->o_req_ndn.bv_len = op->o_req_ndn.bv_len;
2028 op2->o_req_ndn.bv_val = ptr;
2029 ptr = lutil_strcopy(ptr, op->o_req_ndn.bv_val) + 1;
2030 op2->ors_filterstr.bv_val = ptr;
2031 strcpy( ptr, so->s_filterstr.bv_val );
2032 op2->ors_filterstr.bv_len = so->s_filterstr.bv_len;
2034 /* Skip the AND/GE clause that we stuck on in front */
2035 if ( so->s_flags & PS_FIX_FILTER ) {
2036 op2->ors_filter = op->ors_filter->f_and->f_next;
2037 so->s_flags ^= PS_FIX_FILTER;
2039 op2->ors_filter = op->ors_filter;
2041 op2->ors_filter = filter_dup( op2->ors_filter, NULL );
2044 /* Copy any cached group ACLs individually */
2045 op2->o_groups = NULL;
2046 for ( g1=op->o_groups; g1; g1=g1->ga_next ) {
2047 g2 = ch_malloc( sizeof(GroupAssertion) + g1->ga_len );
2049 strcpy( g2->ga_ndn, g1->ga_ndn );
2050 g2->ga_next = op2->o_groups;
2053 /* Don't allow any further group caching */
2054 op2->o_do_not_cache = 1;
2056 /* Add op2 to conn so abandon will find us */
2057 op->o_conn->c_n_ops_executing++;
2058 op->o_conn->c_n_ops_completed--;
2059 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_ops, op2, o_next );
2060 so->s_flags |= PS_IS_DETACHED;
2062 /* Prevent anyone else from trying to send a result for this op */
2067 syncprov_search_response( Operation *op, SlapReply *rs )
2069 searchstate *ss = op->o_callback->sc_private;
2070 slap_overinst *on = ss->ss_on;
2071 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
2072 sync_control *srs = op->o_controls[slap_cids.sc_LDAPsync];
2074 if ( rs->sr_type == REP_SEARCH || rs->sr_type == REP_SEARCHREF ) {
2076 /* If we got a referral without a referral object, there's
2077 * something missing that we cannot replicate. Just ignore it.
2078 * The consumer will abort because we didn't send the expected
2081 if ( !rs->sr_entry ) {
2082 assert( rs->sr_entry != NULL );
2083 Debug( LDAP_DEBUG_ANY, "bogus referral in context\n",0,0,0 );
2084 return SLAP_CB_CONTINUE;
2086 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryCSN );
2087 if ( a == NULL && rs->sr_operational_attrs != NULL ) {
2088 a = attr_find( rs->sr_operational_attrs, slap_schema.si_ad_entryCSN );
2092 sid = slap_parse_csn_sid( &a->a_nvals[0] );
2094 /* Don't send changed entries back to the originator */
2095 if ( sid == srs->sr_state.sid && srs->sr_state.numcsns ) {
2096 Debug( LDAP_DEBUG_SYNC,
2097 "Entry %s changed by peer, ignored\n",
2098 rs->sr_entry->e_name.bv_val, 0, 0 );
2099 return LDAP_SUCCESS;
2102 /* If not a persistent search */
2104 /* Make sure entry is less than the snapshot'd contextCSN */
2105 for ( i=0; i<ss->ss_numcsns; i++ ) {
2106 if ( sid == ss->ss_sids[i] && ber_bvcmp( &a->a_nvals[0],
2107 &ss->ss_ctxcsn[i] ) > 0 ) {
2108 Debug( LDAP_DEBUG_SYNC,
2109 "Entry %s CSN %s greater than snapshot %s\n",
2110 rs->sr_entry->e_name.bv_val,
2111 a->a_nvals[0].bv_val,
2112 ss->ss_ctxcsn[i].bv_val );
2113 return LDAP_SUCCESS;
2118 /* Don't send old entries twice */
2119 if ( srs->sr_state.ctxcsn ) {
2120 for ( i=0; i<srs->sr_state.numcsns; i++ ) {
2121 if ( sid == srs->sr_state.sids[i] &&
2122 ber_bvcmp( &a->a_nvals[0],
2123 &srs->sr_state.ctxcsn[i] )<= 0 ) {
2124 Debug( LDAP_DEBUG_SYNC,
2125 "Entry %s CSN %s older or equal to ctx %s\n",
2126 rs->sr_entry->e_name.bv_val,
2127 a->a_nvals[0].bv_val,
2128 srs->sr_state.ctxcsn[i].bv_val );
2129 return LDAP_SUCCESS;
2134 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
2136 rs->sr_ctrls[1] = NULL;
2137 /* If we're in delta-sync mode, always send a cookie */
2138 if ( si->si_nopres && si->si_usehint && a ) {
2139 struct berval cookie;
2140 slap_compose_sync_cookie( op, &cookie, a->a_nvals, srs->sr_state.rid, slap_serverID ? slap_serverID : -1 );
2141 rs->sr_err = syncprov_state_ctrl( op, rs, rs->sr_entry,
2142 LDAP_SYNC_ADD, rs->sr_ctrls, 0, 1, &cookie );
2144 rs->sr_err = syncprov_state_ctrl( op, rs, rs->sr_entry,
2145 LDAP_SYNC_ADD, rs->sr_ctrls, 0, 0, NULL );
2147 } else if ( rs->sr_type == REP_RESULT && rs->sr_err == LDAP_SUCCESS ) {
2148 struct berval cookie;
2150 if ( ss->ss_flags & SS_CHANGED ) {
2151 slap_compose_sync_cookie( op, &cookie, ss->ss_ctxcsn,
2152 srs->sr_state.rid, slap_serverID ? slap_serverID : -1 );
2154 Debug( LDAP_DEBUG_SYNC, "syncprov_search_response: cookie=%s\n", cookie.bv_val, 0, 0 );
2157 /* Is this a regular refresh?
2158 * Note: refresh never gets here if there were no changes
2161 rs->sr_ctrls = op->o_tmpalloc( sizeof(LDAPControl *)*2,
2163 rs->sr_ctrls[1] = NULL;
2164 rs->sr_err = syncprov_done_ctrl( op, rs, rs->sr_ctrls,
2165 0, 1, &cookie, ( ss->ss_flags & SS_PRESENT ) ? LDAP_SYNC_REFRESH_PRESENTS :
2166 LDAP_SYNC_REFRESH_DELETES );
2167 op->o_tmpfree( cookie.bv_val, op->o_tmpmemctx );
2169 /* It's RefreshAndPersist, transition to Persist phase */
2170 syncprov_sendinfo( op, rs, ( ss->ss_flags & SS_PRESENT ) ?
2171 LDAP_TAG_SYNC_REFRESH_PRESENT : LDAP_TAG_SYNC_REFRESH_DELETE,
2172 ( ss->ss_flags & SS_CHANGED ) ? &cookie : NULL,
2174 if ( ss->ss_flags & SS_CHANGED )
2175 op->o_tmpfree( cookie.bv_val, op->o_tmpmemctx );
2177 /* Detach this Op from frontend control */
2178 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
2180 /* But not if this connection was closed along the way */
2181 if ( op->o_abandon ) {
2182 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
2183 /* syncprov_ab_cleanup will free this syncop */
2184 return SLAPD_ABANDON;
2187 ldap_pvt_thread_mutex_lock( &ss->ss_so->s_mutex );
2188 /* Turn off the refreshing flag */
2189 ss->ss_so->s_flags ^= PS_IS_REFRESHING;
2191 syncprov_detach_op( op, ss->ss_so, on );
2193 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
2195 /* If there are queued responses, fire them off */
2196 if ( ss->ss_so->s_res )
2197 syncprov_qstart( ss->ss_so );
2198 ldap_pvt_thread_mutex_unlock( &ss->ss_so->s_mutex );
2201 return LDAP_SUCCESS;
2205 return SLAP_CB_CONTINUE;
2209 syncprov_op_search( Operation *op, SlapReply *rs )
2211 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2212 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
2214 int gotstate = 0, changed = 0, do_present = 0;
2215 syncops *sop = NULL;
2219 int i, *sids, numcsns;
2220 struct berval mincsn;
2222 if ( !(op->o_sync_mode & SLAP_SYNC_REFRESH) ) return SLAP_CB_CONTINUE;
2224 if ( op->ors_deref & LDAP_DEREF_SEARCHING ) {
2225 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR, "illegal value for derefAliases" );
2229 srs = op->o_controls[slap_cids.sc_LDAPsync];
2231 /* If this is a persistent search, set it up right away */
2232 if ( op->o_sync_mode & SLAP_SYNC_PERSIST ) {
2242 so.s_flags = PS_IS_REFRESHING | PS_FIND_BASE;
2243 /* syncprov_findbase expects to be called as a callback... */
2244 sc.sc_private = &opc;
2246 ldap_pvt_thread_mutex_init( &so.s_mutex );
2247 cb = op->o_callback;
2248 op->o_callback = ≻
2249 rs->sr_err = syncprov_findbase( op, &fc );
2250 op->o_callback = cb;
2251 ldap_pvt_thread_mutex_destroy( &so.s_mutex );
2253 if ( rs->sr_err != LDAP_SUCCESS ) {
2254 send_ldap_result( op, rs );
2257 sop = ch_malloc( sizeof( syncops ));
2259 ldap_pvt_thread_mutex_init( &sop->s_mutex );
2260 sop->s_rid = srs->sr_state.rid;
2261 sop->s_sid = srs->sr_state.sid;
2264 ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
2265 sop->s_next = si->si_ops;
2267 ldap_pvt_thread_mutex_unlock( &si->si_ops_mutex );
2270 /* snapshot the ctxcsn */
2271 ldap_pvt_thread_rdwr_rlock( &si->si_csn_rwlock );
2272 numcsns = si->si_numcsns;
2274 ber_bvarray_dup_x( &ctxcsn, si->si_ctxcsn, op->o_tmpmemctx );
2275 sids = op->o_tmpalloc( numcsns * sizeof(int), op->o_tmpmemctx );
2276 for ( i=0; i<numcsns; i++ )
2277 sids[i] = si->si_sids[i];
2282 ldap_pvt_thread_rdwr_runlock( &si->si_csn_rwlock );
2284 /* If we have a cookie, handle the PRESENT lookups */
2285 if ( srs->sr_state.ctxcsn ) {
2289 /* If we don't have any CSN of our own yet, pretend nothing
2295 if ( !si->si_nopres )
2296 do_present = SS_PRESENT;
2298 /* If there are SIDs we don't recognize in the cookie, drop them */
2299 for (i=0; i<srs->sr_state.numcsns; ) {
2300 for (j=0; j<numcsns; j++) {
2301 if ( srs->sr_state.sids[i] == sids[j] ) {
2306 if ( j == numcsns ) {
2307 struct berval tmp = srs->sr_state.ctxcsn[i];
2308 j = srs->sr_state.numcsns - 1;
2309 srs->sr_state.ctxcsn[i] = srs->sr_state.ctxcsn[j];
2311 srs->sr_state.ctxcsn[j] = tmp;
2312 srs->sr_state.numcsns = j;
2313 srs->sr_state.sids[i] = srs->sr_state.sids[j];
2319 /* Find the smallest CSN */
2320 mincsn = srs->sr_state.ctxcsn[0];
2321 for ( i=1; i<srs->sr_state.numcsns; i++ ) {
2322 if ( ber_bvcmp( &mincsn, &srs->sr_state.ctxcsn[i] ) > 0 )
2323 mincsn = srs->sr_state.ctxcsn[i];
2326 /* If nothing has changed, shortcut it */
2327 if ( srs->sr_state.numcsns == numcsns ) {
2329 for ( i=0; i<srs->sr_state.numcsns; i++ ) {
2330 for ( j=0; j<numcsns; j++ ) {
2331 if ( srs->sr_state.sids[i] != sids[j] )
2333 if ( !bvmatch( &srs->sr_state.ctxcsn[i], &ctxcsn[j] ))
2334 changed = SS_CHANGED;
2342 no_change: if ( !(op->o_sync_mode & SLAP_SYNC_PERSIST) ) {
2343 LDAPControl *ctrls[2];
2347 syncprov_done_ctrl( op, rs, ctrls, 0, 0,
2348 NULL, LDAP_SYNC_REFRESH_DELETES );
2349 rs->sr_ctrls = ctrls;
2350 rs->sr_err = LDAP_SUCCESS;
2351 send_ldap_result( op, rs );
2352 rs->sr_ctrls = NULL;
2358 /* consumer doesn't have the right number of CSNs */
2359 changed = SS_CHANGED;
2361 /* Do we have a sessionlog for this search? */
2364 ldap_pvt_thread_mutex_lock( &sl->sl_mutex );
2365 /* Are there any log entries, and is the consumer state
2366 * present in the session log?
2368 if ( sl->sl_num > 0 && ber_bvcmp( &mincsn, &sl->sl_mincsn ) >= 0 ) {
2370 /* mutex is unlocked in playlog */
2371 syncprov_playlog( op, rs, sl, srs, ctxcsn, numcsns, sids );
2373 ldap_pvt_thread_mutex_unlock( &sl->sl_mutex );
2376 /* Is the CSN still present in the database? */
2377 if ( syncprov_findcsn( op, FIND_CSN ) != LDAP_SUCCESS ) {
2378 /* No, so a reload is required */
2379 /* the 2.2 consumer doesn't send this hint */
2380 if ( si->si_usehint && srs->sr_rhint == 0 ) {
2382 ber_bvarray_free_x( ctxcsn, op->o_tmpmemctx );
2384 op->o_tmpfree( sids, op->o_tmpmemctx );
2385 send_ldap_error( op, rs, LDAP_SYNC_REFRESH_REQUIRED, "sync cookie is stale" );
2388 if ( srs->sr_state.ctxcsn ) {
2389 ber_bvarray_free_x( srs->sr_state.ctxcsn, op->o_tmpmemctx );
2390 srs->sr_state.ctxcsn = NULL;
2392 if ( srs->sr_state.sids ) {
2393 slap_sl_free( srs->sr_state.sids, op->o_tmpmemctx );
2394 srs->sr_state.sids = NULL;
2396 srs->sr_state.numcsns = 0;
2399 /* If changed and doing Present lookup, send Present UUIDs */
2400 if ( do_present && syncprov_findcsn( op, FIND_PRESENT ) !=
2403 ber_bvarray_free_x( ctxcsn, op->o_tmpmemctx );
2405 op->o_tmpfree( sids, op->o_tmpmemctx );
2406 send_ldap_result( op, rs );
2411 /* No consumer state, assume something has changed */
2412 changed = SS_CHANGED;
2416 /* Append CSN range to search filter, save original filter
2417 * for persistent search evaluation
2420 sop->s_filterstr= op->ors_filterstr;
2423 /* If something changed, find the changes */
2424 if ( gotstate && changed ) {
2425 Filter *fand, *fava;
2427 fand = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
2428 fand->f_choice = LDAP_FILTER_AND;
2429 fand->f_next = NULL;
2430 fava = op->o_tmpalloc( sizeof(Filter), op->o_tmpmemctx );
2432 fava->f_choice = LDAP_FILTER_GE;
2433 fava->f_ava = op->o_tmpalloc( sizeof(AttributeAssertion), op->o_tmpmemctx );
2434 fava->f_ava->aa_desc = slap_schema.si_ad_entryCSN;
2435 #ifdef LDAP_COMP_MATCH
2436 fava->f_ava->aa_cf = NULL;
2438 ber_dupbv_x( &fava->f_ava->aa_value, &mincsn, op->o_tmpmemctx );
2439 fava->f_next = op->ors_filter;
2440 op->ors_filter = fand;
2441 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2443 sop->s_flags |= PS_FIX_FILTER;
2446 /* Let our callback add needed info to returned entries */
2447 cb = op->o_tmpcalloc(1, sizeof(slap_callback)+sizeof(searchstate), op->o_tmpmemctx);
2448 ss = (searchstate *)(cb+1);
2451 ss->ss_flags = do_present | changed;
2452 ss->ss_ctxcsn = ctxcsn;
2453 ss->ss_numcsns = numcsns;
2455 cb->sc_response = syncprov_search_response;
2456 cb->sc_cleanup = syncprov_search_cleanup;
2457 cb->sc_private = ss;
2458 cb->sc_next = op->o_callback;
2459 op->o_callback = cb;
2461 /* If this is a persistent search and no changes were reported during
2462 * the refresh phase, just invoke the response callback to transition
2463 * us into persist phase
2466 rs->sr_err = LDAP_SUCCESS;
2467 rs->sr_nentries = 0;
2468 send_ldap_result( op, rs );
2471 return SLAP_CB_CONTINUE;
2475 syncprov_operational(
2479 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2480 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
2482 /* This prevents generating unnecessarily; frontend will strip
2483 * any statically stored copy.
2485 if ( op->o_sync != SLAP_CONTROL_NONE )
2486 return SLAP_CB_CONTINUE;
2488 if ( rs->sr_entry &&
2489 dn_match( &rs->sr_entry->e_nname, op->o_bd->be_nsuffix )) {
2491 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
2492 ad_inlist( slap_schema.si_ad_contextCSN, rs->sr_attrs )) {
2493 Attribute *a, **ap = NULL;
2495 for ( a=rs->sr_entry->e_attrs; a; a=a->a_next ) {
2496 if ( a->a_desc == slap_schema.si_ad_contextCSN )
2500 ldap_pvt_thread_rdwr_rlock( &si->si_csn_rwlock );
2501 if ( si->si_ctxcsn ) {
2503 for ( ap = &rs->sr_operational_attrs; *ap;
2504 ap=&(*ap)->a_next );
2506 a = attr_alloc( slap_schema.si_ad_contextCSN );
2511 if ( !(rs->sr_flags & REP_ENTRY_MODIFIABLE) ) {
2512 Entry *e = entry_dup( rs->sr_entry );
2513 if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
2514 overlay_entry_release_ov( op, rs->sr_entry, 0, on );
2515 rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
2516 } else if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
2517 entry_free( rs->sr_entry );
2521 REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED;
2522 a = attr_find( rs->sr_entry->e_attrs,
2523 slap_schema.si_ad_contextCSN );
2525 if ( a->a_nvals != a->a_vals ) {
2526 ber_bvarray_free( a->a_nvals );
2529 ber_bvarray_free( a->a_vals );
2533 attr_valadd( a, si->si_ctxcsn, si->si_ctxcsn, si->si_numcsns );
2535 ldap_pvt_thread_rdwr_runlock( &si->si_csn_rwlock );
2538 return SLAP_CB_CONTINUE;
2548 static ConfigDriver sp_cf_gen;
2550 static ConfigTable spcfg[] = {
2551 { "syncprov-checkpoint", "ops> <minutes", 3, 3, 0, ARG_MAGIC|SP_CHKPT,
2552 sp_cf_gen, "( OLcfgOvAt:1.1 NAME 'olcSpCheckpoint' "
2553 "DESC 'ContextCSN checkpoint interval in ops and minutes' "
2554 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
2555 { "syncprov-sessionlog", "ops", 2, 2, 0, ARG_INT|ARG_MAGIC|SP_SESSL,
2556 sp_cf_gen, "( OLcfgOvAt:1.2 NAME 'olcSpSessionlog' "
2557 "DESC 'Session log size in ops' "
2558 "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
2559 { "syncprov-nopresent", NULL, 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|SP_NOPRES,
2560 sp_cf_gen, "( OLcfgOvAt:1.3 NAME 'olcSpNoPresent' "
2561 "DESC 'Omit Present phase processing' "
2562 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
2563 { "syncprov-reloadhint", NULL, 2, 2, 0, ARG_ON_OFF|ARG_MAGIC|SP_USEHINT,
2564 sp_cf_gen, "( OLcfgOvAt:1.4 NAME 'olcSpReloadHint' "
2565 "DESC 'Observe Reload Hint in Request control' "
2566 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
2567 { NULL, NULL, 0, 0, 0, ARG_IGNORED }
2570 static ConfigOCs spocs[] = {
2571 { "( OLcfgOvOc:1.1 "
2572 "NAME 'olcSyncProvConfig' "
2573 "DESC 'SyncRepl Provider configuration' "
2574 "SUP olcOverlayConfig "
2575 "MAY ( olcSpCheckpoint "
2576 "$ olcSpSessionlog "
2578 "$ olcSpReloadHint "
2580 Cft_Overlay, spcfg },
2585 sp_cf_gen(ConfigArgs *c)
2587 slap_overinst *on = (slap_overinst *)c->bi;
2588 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
2591 if ( c->op == SLAP_CONFIG_EMIT ) {
2592 switch ( c->type ) {
2594 if ( si->si_chkops || si->si_chktime ) {
2596 bv.bv_len = snprintf( c->cr_msg, sizeof( c->cr_msg ),
2597 "%d %d", si->si_chkops, si->si_chktime );
2598 if ( bv.bv_len >= sizeof( c->cr_msg ) ) {
2601 bv.bv_val = c->cr_msg;
2602 value_add_one( &c->rvalue_vals, &bv );
2609 if ( si->si_logs ) {
2610 c->value_int = si->si_logs->sl_size;
2616 if ( si->si_nopres ) {
2623 if ( si->si_usehint ) {
2631 } else if ( c->op == LDAP_MOD_DELETE ) {
2632 switch ( c->type ) {
2639 si->si_logs->sl_size = 0;
2641 rc = LDAP_NO_SUCH_ATTRIBUTE;
2644 if ( si->si_nopres )
2647 rc = LDAP_NO_SUCH_ATTRIBUTE;
2650 if ( si->si_usehint )
2653 rc = LDAP_NO_SUCH_ATTRIBUTE;
2658 switch ( c->type ) {
2660 if ( lutil_atoi( &si->si_chkops, c->argv[1] ) != 0 ) {
2661 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s unable to parse checkpoint ops # \"%s\"",
2662 c->argv[0], c->argv[1] );
2663 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
2664 "%s: %s\n", c->log, c->cr_msg, 0 );
2665 return ARG_BAD_CONF;
2667 if ( si->si_chkops <= 0 ) {
2668 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid checkpoint ops # \"%d\"",
2669 c->argv[0], si->si_chkops );
2670 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
2671 "%s: %s\n", c->log, c->cr_msg, 0 );
2672 return ARG_BAD_CONF;
2674 if ( lutil_atoi( &si->si_chktime, c->argv[2] ) != 0 ) {
2675 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s unable to parse checkpoint time \"%s\"",
2676 c->argv[0], c->argv[1] );
2677 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
2678 "%s: %s\n", c->log, c->cr_msg, 0 );
2679 return ARG_BAD_CONF;
2681 if ( si->si_chktime <= 0 ) {
2682 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid checkpoint time \"%d\"",
2683 c->argv[0], si->si_chkops );
2684 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
2685 "%s: %s\n", c->log, c->cr_msg, 0 );
2686 return ARG_BAD_CONF;
2688 si->si_chktime *= 60;
2692 int size = c->value_int;
2695 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s size %d is negative",
2697 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
2698 "%s: %s\n", c->log, c->cr_msg, 0 );
2699 return ARG_BAD_CONF;
2703 sl = ch_malloc( sizeof( sessionlog ) + LDAP_LUTIL_CSNSTR_BUFSIZE );
2704 sl->sl_mincsn.bv_val = (char *)(sl+1);
2705 sl->sl_mincsn.bv_len = 0;
2707 sl->sl_head = sl->sl_tail = NULL;
2708 ldap_pvt_thread_mutex_init( &sl->sl_mutex );
2715 si->si_nopres = c->value_int;
2718 si->si_usehint = c->value_int;
2724 /* ITS#3456 we cannot run this search on the main thread, must use a
2725 * child thread in order to insure we have a big enough stack.
2732 syncprov_findcsn( ptr, FIND_MAXCSN );
2736 /* Read any existing contextCSN from the underlying db.
2737 * Then search for any entries newer than that. If no value exists,
2738 * just generate it. Cache whatever result.
2746 slap_overinst *on = (slap_overinst *) be->bd_info;
2747 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
2749 Connection conn = { 0 };
2750 OperationBuffer opbuf;
2755 void *thrctx = NULL;
2757 if ( !SLAP_LASTMOD( be )) {
2758 Debug( LDAP_DEBUG_ANY,
2759 "syncprov_db_open: invalid config, lastmod must be enabled\n", 0, 0, 0 );
2763 if ( slapMode & SLAP_TOOL_MODE ) {
2767 rc = overlay_register_control( be, LDAP_CONTROL_SYNC );
2772 thrctx = ldap_pvt_thread_pool_context();
2773 connection_fake_init( &conn, &opbuf, thrctx );
2776 op->o_dn = be->be_rootdn;
2777 op->o_ndn = be->be_rootndn;
2779 rc = overlay_entry_get_ov( op, be->be_nsuffix, NULL,
2780 slap_schema.si_ad_contextCSN, 0, &e, on );
2783 ldap_pvt_thread_t tid;
2785 a = attr_find( e->e_attrs, slap_schema.si_ad_contextCSN );
2787 ber_bvarray_dup_x( &si->si_ctxcsn, a->a_vals, NULL );
2788 si->si_numcsns = a->a_numvals;
2789 si->si_sids = slap_parse_csn_sids( si->si_ctxcsn, a->a_numvals, NULL );
2791 overlay_entry_release_ov( op, e, 0, on );
2792 if ( si->si_ctxcsn && !SLAP_DBCLEAN( be )) {
2793 op->o_req_dn = be->be_suffix[0];
2794 op->o_req_ndn = be->be_nsuffix[0];
2795 op->ors_scope = LDAP_SCOPE_SUBTREE;
2796 ldap_pvt_thread_create( &tid, 0, syncprov_db_otask, op );
2797 ldap_pvt_thread_join( tid, NULL );
2801 /* Didn't find a contextCSN, should we generate one? */
2802 if ( !si->si_ctxcsn ) {
2803 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
2806 if ( SLAP_SYNC_SHADOW( op->o_bd )) {
2807 /* If we're also a consumer, then don't generate anything.
2808 * Wait for our provider to send it to us, or for a local
2809 * modify if we have multimaster.
2813 csn.bv_val = csnbuf;
2814 csn.bv_len = sizeof( csnbuf );
2815 slap_get_csn( op, &csn, 0 );
2816 value_add_one( &si->si_ctxcsn, &csn );
2818 si->si_sids = ch_malloc( sizeof(int) );
2819 si->si_sids[0] = slap_serverID;
2821 /* make sure we do a checkpoint on close */
2826 op->o_bd->bd_info = (BackendInfo *)on;
2830 /* Write the current contextCSN into the underlying db.
2838 slap_overinst *on = (slap_overinst *) be->bd_info;
2839 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
2841 if ( slapMode & SLAP_TOOL_MODE ) {
2844 if ( si->si_numops ) {
2845 Connection conn = {0};
2846 OperationBuffer opbuf;
2848 SlapReply rs = {REP_RESULT};
2851 thrctx = ldap_pvt_thread_pool_context();
2852 connection_fake_init( &conn, &opbuf, thrctx );
2855 op->o_dn = be->be_rootdn;
2856 op->o_ndn = be->be_rootndn;
2857 syncprov_checkpoint( op, &rs, on );
2869 slap_overinst *on = (slap_overinst *)be->bd_info;
2870 syncprov_info_t *si;
2872 if ( SLAP_ISGLOBALOVERLAY( be ) ) {
2873 Debug( LDAP_DEBUG_ANY,
2874 "syncprov must be instantiated within a database.\n",
2879 si = ch_calloc(1, sizeof(syncprov_info_t));
2880 on->on_bi.bi_private = si;
2881 ldap_pvt_thread_rdwr_init( &si->si_csn_rwlock );
2882 ldap_pvt_thread_mutex_init( &si->si_ops_mutex );
2883 ldap_pvt_thread_mutex_init( &si->si_mods_mutex );
2885 csn_anlist[0].an_desc = slap_schema.si_ad_entryCSN;
2886 csn_anlist[0].an_name = slap_schema.si_ad_entryCSN->ad_cname;
2887 csn_anlist[1].an_desc = slap_schema.si_ad_entryUUID;
2888 csn_anlist[1].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2890 uuid_anlist[0].an_desc = slap_schema.si_ad_entryUUID;
2891 uuid_anlist[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2897 syncprov_db_destroy(
2902 slap_overinst *on = (slap_overinst *)be->bd_info;
2903 syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
2906 if ( si->si_logs ) {
2907 slog_entry *se = si->si_logs->sl_head;
2910 slog_entry *se_next = se->se_next;
2915 ch_free( si->si_logs );
2917 if ( si->si_ctxcsn )
2918 ber_bvarray_free( si->si_ctxcsn );
2920 ch_free( si->si_sids );
2921 ldap_pvt_thread_mutex_destroy( &si->si_mods_mutex );
2922 ldap_pvt_thread_mutex_destroy( &si->si_ops_mutex );
2923 ldap_pvt_thread_rdwr_destroy( &si->si_csn_rwlock );
2930 static int syncprov_parseCtrl (
2936 BerElementBuffer berbuf;
2937 BerElement *ber = (BerElement *)&berbuf;
2940 struct berval cookie = BER_BVNULL;
2944 if ( op->o_sync != SLAP_CONTROL_NONE ) {
2945 rs->sr_text = "Sync control specified multiple times";
2946 return LDAP_PROTOCOL_ERROR;
2949 if ( op->o_pagedresults != SLAP_CONTROL_NONE ) {
2950 rs->sr_text = "Sync control specified with pagedResults control";
2951 return LDAP_PROTOCOL_ERROR;
2954 if ( BER_BVISNULL( &ctrl->ldctl_value ) ) {
2955 rs->sr_text = "Sync control value is absent";
2956 return LDAP_PROTOCOL_ERROR;
2959 if ( BER_BVISEMPTY( &ctrl->ldctl_value ) ) {
2960 rs->sr_text = "Sync control value is empty";
2961 return LDAP_PROTOCOL_ERROR;
2964 /* Parse the control value
2965 * syncRequestValue ::= SEQUENCE {
2970 * refreshAndPersist (3)
2972 * cookie syncCookie OPTIONAL
2976 ber_init2( ber, &ctrl->ldctl_value, 0 );
2978 if ( (tag = ber_scanf( ber, "{i" /*}*/, &mode )) == LBER_ERROR ) {
2979 rs->sr_text = "Sync control : mode decoding error";
2980 return LDAP_PROTOCOL_ERROR;
2984 case LDAP_SYNC_REFRESH_ONLY:
2985 mode = SLAP_SYNC_REFRESH;
2987 case LDAP_SYNC_REFRESH_AND_PERSIST:
2988 mode = SLAP_SYNC_REFRESH_AND_PERSIST;
2991 rs->sr_text = "Sync control : unknown update mode";
2992 return LDAP_PROTOCOL_ERROR;
2995 tag = ber_peek_tag( ber, &len );
2997 if ( tag == LDAP_TAG_SYNC_COOKIE ) {
2998 if (( ber_scanf( ber, /*{*/ "m", &cookie )) == LBER_ERROR ) {
2999 rs->sr_text = "Sync control : cookie decoding error";
3000 return LDAP_PROTOCOL_ERROR;
3002 tag = ber_peek_tag( ber, &len );
3004 if ( tag == LDAP_TAG_RELOAD_HINT ) {
3005 if (( ber_scanf( ber, /*{*/ "b", &rhint )) == LBER_ERROR ) {
3006 rs->sr_text = "Sync control : rhint decoding error";
3007 return LDAP_PROTOCOL_ERROR;
3010 if (( ber_scanf( ber, /*{*/ "}")) == LBER_ERROR ) {
3011 rs->sr_text = "Sync control : decoding error";
3012 return LDAP_PROTOCOL_ERROR;
3014 sr = op->o_tmpcalloc( 1, sizeof(struct sync_control), op->o_tmpmemctx );
3015 sr->sr_rhint = rhint;
3016 if (!BER_BVISNULL(&cookie)) {
3017 ber_dupbv_x( &sr->sr_state.octet_str, &cookie, op->o_tmpmemctx );
3018 /* If parse fails, pretend no cookie was sent */
3019 if ( slap_parse_sync_cookie( &sr->sr_state, op->o_tmpmemctx ) ||
3020 sr->sr_state.rid == -1 ) {
3021 if ( sr->sr_state.ctxcsn ) {
3022 ber_bvarray_free_x( sr->sr_state.ctxcsn, op->o_tmpmemctx );
3023 sr->sr_state.ctxcsn = NULL;
3025 sr->sr_state.numcsns = 0;
3029 op->o_controls[slap_cids.sc_LDAPsync] = sr;
3031 op->o_sync = ctrl->ldctl_iscritical
3032 ? SLAP_CONTROL_CRITICAL
3033 : SLAP_CONTROL_NONCRITICAL;
3035 op->o_sync_mode |= mode; /* o_sync_mode shares o_sync */
3037 return LDAP_SUCCESS;
3040 /* This overlay is set up for dynamic loading via moduleload. For static
3041 * configuration, you'll need to arrange for the slap_overinst to be
3042 * initialized and registered by some other function inside slapd.
3045 static slap_overinst syncprov;
3048 syncprov_initialize()
3052 rc = register_supported_control( LDAP_CONTROL_SYNC,
3053 SLAP_CTRL_SEARCH, NULL,
3054 syncprov_parseCtrl, &slap_cids.sc_LDAPsync );
3055 if ( rc != LDAP_SUCCESS ) {
3056 Debug( LDAP_DEBUG_ANY,
3057 "syncprov_init: Failed to register control %d\n", rc, 0, 0 );
3061 syncprov.on_bi.bi_type = "syncprov";
3062 syncprov.on_bi.bi_db_init = syncprov_db_init;
3063 syncprov.on_bi.bi_db_destroy = syncprov_db_destroy;
3064 syncprov.on_bi.bi_db_open = syncprov_db_open;
3065 syncprov.on_bi.bi_db_close = syncprov_db_close;
3067 syncprov.on_bi.bi_op_abandon = syncprov_op_abandon;
3068 syncprov.on_bi.bi_op_cancel = syncprov_op_abandon;
3070 syncprov.on_bi.bi_op_add = syncprov_op_mod;
3071 syncprov.on_bi.bi_op_compare = syncprov_op_compare;
3072 syncprov.on_bi.bi_op_delete = syncprov_op_mod;
3073 syncprov.on_bi.bi_op_modify = syncprov_op_mod;
3074 syncprov.on_bi.bi_op_modrdn = syncprov_op_mod;
3075 syncprov.on_bi.bi_op_search = syncprov_op_search;
3076 syncprov.on_bi.bi_extended = syncprov_op_extended;
3077 syncprov.on_bi.bi_operational = syncprov_operational;
3079 syncprov.on_bi.bi_cf_ocs = spocs;
3081 generic_filter.f_desc = slap_schema.si_ad_objectClass;
3083 rc = config_register_schema( spcfg, spocs );
3084 if ( rc ) return rc;
3086 return overlay_register( &syncprov );
3089 #if SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC
3091 init_module( int argc, char *argv[] )
3093 return syncprov_initialize();
3095 #endif /* SLAPD_OVER_SYNCPROV == SLAPD_MOD_DYNAMIC */
3097 #endif /* defined(SLAPD_OVER_SYNCPROV) */