1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2015 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003-2008 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
28 #include "lutil_ldap.h"
36 #define SUFFIXM_CTX "<suffix massage>"
41 struct nonpresent_entry {
42 struct berval *npe_name;
43 struct berval *npe_nname;
44 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
47 typedef struct cookie_state {
48 ldap_pvt_thread_mutex_t cs_mutex;
49 struct berval *cs_vals;
55 /* pending changes, not yet committed */
56 ldap_pvt_thread_mutex_t cs_pmutex;
57 struct berval *cs_pvals;
62 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
63 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
64 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
66 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
67 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
69 #define RETRYNUM_FOREVER (-1) /* retry forever */
70 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
71 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
72 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
74 typedef struct syncinfo_s {
75 struct syncinfo_s *si_next;
80 char si_ridtxt[ STRLENOF("rid=999") + 1 ];
81 slap_bindconf si_bindconf;
82 struct berval si_base;
83 struct berval si_logbase;
84 struct berval si_filterstr;
86 struct berval si_logfilterstr;
87 struct berval si_contextdn;
91 AttributeName *si_anlist;
92 AttributeName *si_exanlist;
97 int si_schemachecking;
98 int si_type; /* the active type */
99 int si_ctype; /* the configured type */
101 time_t *si_retryinterval;
102 int *si_retrynum_init;
104 struct sync_cookie si_syncCookie;
105 cookie_state *si_cookieState;
110 int si_refreshDelete;
111 int si_refreshPresent;
117 int si_strict_refresh; /* stop listening during fallback refresh */
120 Avlnode *si_presentlist;
123 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
124 #ifdef ENABLE_REWRITE
125 struct rewrite_info *si_rewrite;
126 struct berval si_suffixm;
128 ldap_pvt_thread_mutex_t si_mutex;
131 static int syncuuid_cmp( const void *, const void * );
132 static int presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
133 static void presentlist_delete( Avlnode **av, struct berval *syncUUID );
134 static char *presentlist_find( Avlnode *av, struct berval *syncUUID );
135 static int presentlist_free( Avlnode *av );
136 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
137 static int syncrepl_message_to_op(
138 syncinfo_t *, Operation *, LDAPMessage * );
139 static int syncrepl_message_to_entry(
140 syncinfo_t *, Operation *, LDAPMessage *,
141 Modifications **, Entry **, int, struct berval* );
142 static int syncrepl_entry(
143 syncinfo_t *, Operation*, Entry*,
144 Modifications**,int, struct berval*,
145 struct berval *cookieCSN );
146 static int syncrepl_updateCookie(
147 syncinfo_t *, Operation *,
148 struct sync_cookie * );
149 static struct berval * slap_uuidstr_from_normalized(
150 struct berval *, struct berval *, void * );
151 static int syncrepl_add_glue_ancestors(
152 Operation* op, Entry *e );
154 /* delta-mmr overlay handler */
155 static int syncrepl_op_modify( Operation *op, SlapReply *rs );
157 /* callback functions */
158 static int dn_callback( Operation *, SlapReply * );
159 static int nonpresent_callback( Operation *, SlapReply * );
160 static int null_callback( Operation *, SlapReply * );
162 static AttributeDescription *sync_descs[4];
165 static AttributeDescription *ad_reqMod, *ad_reqDN;
167 typedef struct logschema {
169 struct berval ls_req;
170 struct berval ls_mod;
171 struct berval ls_newRdn;
172 struct berval ls_delRdn;
173 struct berval ls_newSup;
176 static logschema changelog_sc = {
178 BER_BVC("changeType"),
181 BER_BVC("deleteOldRDN"),
182 BER_BVC("newSuperior")
185 static logschema accesslog_sc = {
189 BER_BVC("reqNewRDN"),
190 BER_BVC("reqDeleteOldRDN"),
191 BER_BVC("reqNewSuperior")
195 syncrepl_state2str( int state )
198 case LDAP_SYNC_PRESENT:
204 case LDAP_SYNC_MODIFY:
207 case LDAP_SYNC_DELETE:
214 static slap_overinst syncrepl_ov;
217 init_syncrepl(syncinfo_t *si)
220 char **attrs, **exattrs;
222 if ( !syncrepl_ov.on_bi.bi_type ) {
223 syncrepl_ov.on_bi.bi_type = "syncrepl";
224 syncrepl_ov.on_bi.bi_op_modify = syncrepl_op_modify;
225 overlay_register( &syncrepl_ov );
228 /* delta-MMR needs the overlay, nothing else does.
229 * This must happen before accesslog overlay is configured.
231 if ( si->si_syncdata &&
232 !overlay_is_inst( si->si_be, syncrepl_ov.on_bi.bi_type )) {
233 overlay_config( si->si_be, syncrepl_ov.on_bi.bi_type, -1, NULL, NULL );
236 logschema *ls = &accesslog_sc;
238 slap_bv2ad( &ls->ls_mod, &ad_reqMod, &text );
239 slap_bv2ad( &ls->ls_dn, &ad_reqDN, &text );
243 if ( !sync_descs[0] ) {
244 sync_descs[0] = slap_schema.si_ad_objectClass;
245 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
246 sync_descs[2] = slap_schema.si_ad_entryCSN;
247 sync_descs[3] = NULL;
250 if ( si->si_allattrs && si->si_allopattrs )
253 attrs = anlist2attrs( si->si_anlist );
256 if ( si->si_allattrs ) {
259 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
260 for ( j = i; attrs[j] != NULL; j++ ) {
263 attrs[j] = attrs[j+1];
269 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
270 attrs[i] = ch_strdup("*");
273 } else if ( si->si_allopattrs ) {
276 if ( is_at_operational( at_find( attrs[i] ) ) ) {
277 for ( j = i; attrs[j] != NULL; j++ ) {
280 attrs[j] = attrs[j+1];
286 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
287 attrs[i] = ch_strdup("+");
291 for ( i = 0; sync_descs[i] != NULL; i++ ) {
294 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
295 for ( k = j; attrs[k] != NULL; k++ ) {
298 attrs[k] = attrs[k+1];
306 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
308 if ( si->si_allopattrs ) {
309 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
311 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
315 if ( si->si_allopattrs ) {
316 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
318 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
319 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
327 if ( si->si_allattrs == si->si_allopattrs ) {
328 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
329 attrs[i++] = ch_strdup( "*" );
330 attrs[i++] = ch_strdup( "+" );
331 } else if ( si->si_allattrs && !si->si_allopattrs ) {
332 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
333 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
334 attrs[i++] = ch_strdup( "*" );
335 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
336 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
338 } else if ( !si->si_allattrs && si->si_allopattrs ) {
339 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
340 attrs[i++] = ch_strdup( "+" );
341 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
346 si->si_attrs = attrs;
348 exattrs = anlist2attrs( si->si_exanlist );
351 for ( n = 0; exattrs[n] != NULL; n++ ) ;
353 for ( i = 0; sync_descs[i] != NULL; i++ ) {
355 while ( exattrs[j] != NULL ) {
356 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
357 ch_free( exattrs[j] );
358 for ( k = j; exattrs[k] != NULL; k++ ) {
359 exattrs[k] = exattrs[k+1];
367 for ( i = 0; exattrs[i] != NULL; i++ ) {
368 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
370 if ( ( oc = si->si_anlist[j].an_oc ) ) {
372 while ( oc->soc_required[k] ) {
373 if ( !strcmp( exattrs[i],
374 oc->soc_required[k]->sat_cname.bv_val ) ) {
375 ch_free( exattrs[i] );
376 for ( l = i; exattrs[l]; l++ ) {
377 exattrs[l] = exattrs[l+1];
387 for ( i = 0; exattrs[i] != NULL; i++ ) ;
390 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
393 si->si_exattrs = exattrs;
401 BerElementBuffer berbuf;
402 BerElement *ber = (BerElement *)&berbuf;
403 LDAPControl c[3], *ctrls[4];
407 char **attrs, *lattrs[8];
412 /* setup LDAP SYNC control */
413 ber_init2( ber, NULL, LBER_USE_DER );
414 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
416 /* If we're using a log but we have no state, then fallback to
417 * normal mode for a full refresh.
419 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
420 si->si_logstate = SYNCLOG_FALLBACK;
423 /* Use the log parameters if we're in log mode */
424 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
426 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
430 lattrs[0] = ls->ls_dn.bv_val;
431 lattrs[1] = ls->ls_req.bv_val;
432 lattrs[2] = ls->ls_mod.bv_val;
433 lattrs[3] = ls->ls_newRdn.bv_val;
434 lattrs[4] = ls->ls_delRdn.bv_val;
435 lattrs[5] = ls->ls_newSup.bv_val;
436 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
440 base = si->si_logbase.bv_val;
441 filter = si->si_logfilterstr.bv_val;
444 scope = LDAP_SCOPE_SUBTREE;
447 base = si->si_base.bv_val;
448 filter = si->si_filterstr.bv_val;
449 attrs = si->si_attrs;
450 attrsonly = si->si_attrsonly;
451 scope = si->si_scope;
453 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
454 si->si_type = LDAP_SYNC_REFRESH_ONLY;
456 si->si_type = si->si_ctype;
459 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
461 ber_printf( ber, "{eOb}",
462 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
464 ber_printf( ber, "{eb}",
465 abs(si->si_type), rhint );
468 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == -1 ) {
473 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
474 c[0].ldctl_iscritical = si->si_type < 0;
477 c[1].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
478 BER_BVZERO( &c[1].ldctl_value );
479 c[1].ldctl_iscritical = 1;
482 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
483 c[2].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
484 c[2].ldctl_value = si->si_bindconf.sb_authzId;
485 c[2].ldctl_iscritical = 1;
492 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
493 ctrls, NULL, NULL, si->si_slimit, &si->si_msgid );
506 SlapReply rs = {REP_SEARCH};
507 int i, j, changed = 0;
509 /* Look for contextCSN from syncprov overlay. If
510 * there's no overlay, this will be a no-op. That means
511 * this is a pure consumer, so local changes will not be
512 * allowed, and all changes will already be reflected in
515 a.a_desc = slap_schema.si_ad_contextCSN;
517 e.e_name = si->si_contextdn;
518 e.e_nname = si->si_contextdn;
519 at[0].an_name = a.a_desc->ad_cname;
520 at[0].an_desc = a.a_desc;
521 BER_BVZERO( &at[1].an_name );
523 rs.sr_flags = REP_ENTRY_MODIFIABLE;
525 op->o_req_dn = e.e_name;
526 op->o_req_ndn = e.e_nname;
528 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
529 i = backend_operational( op, &rs );
530 if ( i == LDAP_SUCCESS && a.a_nvals ) {
531 int num = a.a_numvals;
532 /* check for differences */
533 if ( num != si->si_cookieState->cs_num ) {
536 for ( i=0; i<num; i++ ) {
537 if ( ber_bvcmp( &a.a_nvals[i],
538 &si->si_cookieState->cs_vals[i] )) {
545 ber_bvarray_free( si->si_cookieState->cs_vals );
546 ch_free( si->si_cookieState->cs_sids );
547 si->si_cookieState->cs_num = num;
548 si->si_cookieState->cs_vals = a.a_nvals;
549 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_nvals,
551 si->si_cookieState->cs_age++;
553 ber_bvarray_free( a.a_nvals );
555 ber_bvarray_free( a.a_vals );
557 /* See if the cookieState has changed due to anything outside
558 * this particular consumer. That includes other consumers in
559 * the same context, or local changes detected above.
561 if ( si->si_cookieState->cs_num > 0 && si->si_cookieAge !=
562 si->si_cookieState->cs_age ) {
563 if ( !si->si_syncCookie.numcsns ) {
564 ber_bvarray_free( si->si_syncCookie.ctxcsn );
565 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
566 si->si_cookieState->cs_vals, NULL );
569 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
570 /* bogus, just dup everything */
571 if ( si->si_syncCookie.sids[i] == -1 ) {
572 ber_bvarray_free( si->si_syncCookie.ctxcsn );
573 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
574 si->si_cookieState->cs_vals, NULL );
578 for (j=0; j<si->si_cookieState->cs_num; j++) {
579 if ( si->si_syncCookie.sids[i] !=
580 si->si_cookieState->cs_sids[j] )
582 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
583 &si->si_cookieState->cs_vals[j] ))
585 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
586 &si->si_cookieState->cs_vals[j] );
594 si->si_cookieAge = si->si_cookieState->cs_age;
595 ch_free( si->si_syncCookie.octet_str.bv_val );
596 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
597 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
598 si->si_syncCookie.sid );
599 ch_free( si->si_syncCookie.sids );
600 slap_reparse_sync_cookie( &si->si_syncCookie, op->o_tmpmemctx );
602 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
612 int cmdline_cookie_found = 0;
614 struct sync_cookie *sc = NULL;
619 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
620 if ( rc != LDAP_SUCCESS ) {
623 op->o_protocol = LDAP_VERSION3;
625 /* Set SSF to strongest of TLS, SASL SSFs */
628 op->o_transport_ssf = 0;
630 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
631 == LDAP_SUCCESS && ssl != NULL )
633 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
635 #endif /* HAVE_TLS */
637 ber_len_t ssf; /* ITS#5403, 3864 LDAP_OPT_X_SASL_SSF probably ought
638 to use sasl_ssf_t but currently uses ber_len_t */
639 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &ssf )
641 op->o_sasl_ssf = ssf;
643 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
644 ? op->o_sasl_ssf : op->o_tls_ssf;
646 ldap_set_option( si->si_ld, LDAP_OPT_TIMELIMIT, &si->si_tlimit );
648 rc = LDAP_DEREF_NEVER; /* actually could allow DEREF_FINDING */
649 ldap_set_option( si->si_ld, LDAP_OPT_DEREF, &rc );
651 ldap_set_option( si->si_ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
653 si->si_syncCookie.rid = si->si_rid;
655 /* whenever there are multiple data sources possible, advertise sid */
656 si->si_syncCookie.sid = ( SLAP_MULTIMASTER( si->si_be ) || si->si_be != si->si_wbe ) ?
659 /* We've just started up, or the remote server hasn't sent us
660 * any meaningful state.
662 if ( !si->si_syncCookie.ctxcsn ) {
665 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
666 if ( si->si_rid == sc->rid ) {
667 cmdline_cookie_found = 1;
672 if ( cmdline_cookie_found ) {
673 /* cookie is supplied in the command line */
675 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
677 slap_sync_cookie_free( &si->si_syncCookie, 0 );
678 si->si_syncCookie.octet_str = sc->octet_str;
680 /* ctxcsn wasn't parsed yet, do it now */
681 slap_parse_sync_cookie( &si->si_syncCookie, NULL );
683 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
684 if ( !si->si_cookieState->cs_num ) {
685 /* get contextCSN shadow replica from database */
686 BerVarray csn = NULL;
687 void *ctx = op->o_tmpmemctx;
689 op->o_req_ndn = si->si_contextdn;
690 op->o_req_dn = op->o_req_ndn;
692 /* try to read stored contextCSN */
693 op->o_tmpmemctx = NULL;
694 backend_attribute( op, NULL, &op->o_req_ndn,
695 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
696 op->o_tmpmemctx = ctx;
698 si->si_cookieState->cs_vals = csn;
699 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
700 si->si_cookieState->cs_num = i;
701 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
702 slap_sort_csn_sids( csn, si->si_cookieState->cs_sids, i, NULL );
705 if ( si->si_cookieState->cs_num ) {
706 ber_bvarray_free( si->si_syncCookie.ctxcsn );
707 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
708 si->si_cookieState->cs_vals, NULL )) {
710 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
713 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
714 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
716 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
717 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
719 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
722 ch_free( si->si_syncCookie.octet_str.bv_val );
723 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
724 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
725 si->si_syncCookie.sid );
727 /* ITS#6367: recreate the cookie so it has our SID, not our peer's */
728 ch_free( si->si_syncCookie.octet_str.bv_val );
729 BER_BVZERO( &si->si_syncCookie.octet_str );
730 /* Look for contextCSN from syncprov overlay. */
731 check_syncprov( op, si );
732 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
733 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
734 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
735 si->si_syncCookie.sid );
738 si->si_refreshDone = 0;
740 rc = ldap_sync_search( si, op->o_tmpmemctx );
742 if( rc != LDAP_SUCCESS ) {
743 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
744 "ldap_search_ext: %s (%d)\n",
745 si->si_ridtxt, ldap_err2string( rc ), rc );
751 ldap_unbind_ext( si->si_ld, NULL, NULL );
760 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
767 if ( sc1->numcsns < sc2->numcsns ) {
768 *which = sc1->numcsns;
772 for (j=0; j<sc2->numcsns; j++) {
773 for (i=0; i<sc1->numcsns; i++) {
774 if ( sc1->sids[i] != sc2->sids[j] )
776 value_match( &match, slap_schema.si_ad_entryCSN,
777 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
778 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
779 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
786 if ( i == sc1->numcsns ) {
787 /* sc2 has a sid sc1 lacks */
795 #define SYNC_PAUSED -3
802 BerElementBuffer berbuf;
803 BerElement *ber = (BerElement *)&berbuf;
805 LDAPMessage *msg = NULL;
807 struct sync_cookie syncCookie = { NULL };
808 struct sync_cookie syncCookie_req = { NULL };
813 Modifications *modlist = NULL;
817 struct timeval *tout_p = NULL;
818 struct timeval tout = { 0, 0 };
820 int refreshDeletes = 0;
821 char empty[6] = "empty";
823 if ( slapd_shutdown ) {
828 ber_init2( ber, NULL, LBER_USE_DER );
829 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
831 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
833 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
835 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST && si->si_refreshDone ) {
841 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
842 tout_p, &msg ) ) > 0 )
844 int match, punlock, syncstate;
845 struct berval *retdata, syncUUID[2], cookie = BER_BVNULL;
847 LDAPControl **rctrls = NULL, *rctrlp = NULL;
854 if ( slapd_shutdown ) {
858 switch( ldap_msgtype( msg ) ) {
859 case LDAP_RES_SEARCH_ENTRY:
860 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
861 ldap_get_dn_ber( si->si_ld, msg, NULL, &bdn );
864 bdn.bv_len = sizeof(empty)-1;
866 /* we can't work without the control */
868 LDAPControl **next = NULL;
869 /* NOTE: make sure we use the right one;
870 * a better approach would be to run thru
871 * the whole list and take care of all */
872 /* NOTE: since we issue the search request,
873 * we should know what controls to expect,
874 * and there should be none apart from the
875 * sync-related control */
876 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
877 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
879 bdn.bv_val[bdn.bv_len] = '\0';
880 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
881 "got search entry with multiple "
882 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
883 ldap_controls_free( rctrls );
888 if ( rctrlp == NULL ) {
889 bdn.bv_val[bdn.bv_len] = '\0';
890 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
891 "got search entry without "
892 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
896 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
897 if ( ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID[0] )
899 bdn.bv_val[bdn.bv_len] = '\0';
900 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s malformed message (%s)\n",
901 si->si_ridtxt, bdn.bv_val, 0 );
902 ldap_controls_free( rctrls );
906 /* FIXME: what if syncUUID is NULL or empty?
907 * (happens with back-sql...) */
908 if ( syncUUID[0].bv_len != UUIDLEN ) {
909 bdn.bv_val[bdn.bv_len] = '\0';
910 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
911 "got empty or invalid syncUUID with LDAP_SYNC_%s (%s)\n",
913 syncrepl_state2str( syncstate ), bdn.bv_val );
914 ldap_controls_free( rctrls );
919 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
920 if ( ber_scanf( ber, /*"{"*/ "m}", &cookie ) != LBER_ERROR ) {
922 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
924 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
926 if ( !BER_BVISNULL( &cookie ) ) {
927 ch_free( syncCookie.octet_str.bv_val );
928 ber_dupbv( &syncCookie.octet_str, &cookie );
930 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
932 slap_parse_sync_cookie( &syncCookie, NULL );
933 if ( syncCookie.ctxcsn ) {
934 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
935 check_syncprov( op, si );
936 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
937 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
939 if ( sid < si->si_cookieState->cs_sids[i] )
941 if ( si->si_cookieState->cs_sids[i] == sid ) {
942 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
943 bdn.bv_val[bdn.bv_len] = '\0';
944 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s (%s)\n",
945 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
947 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
948 ldap_controls_free( rctrls );
956 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
958 /* check pending CSNs too */
959 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
960 if ( slapd_shutdown ) {
964 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
965 ldap_pvt_thread_yield();
968 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
969 if ( sid < si->si_cookieState->cs_psids[i] )
971 if ( si->si_cookieState->cs_psids[i] == sid ) {
972 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
973 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
974 bdn.bv_val[bdn.bv_len] = '\0';
975 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s (%s)\n",
976 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
977 ldap_controls_free( rctrls );
981 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
986 /* new SID, add it */
987 if ( i == si->si_cookieState->cs_pnum ||
988 sid != si->si_cookieState->cs_psids[i] ) {
989 slap_insert_csn_sids(
990 (struct sync_cookie *)&si->si_cookieState->cs_pvals,
991 i, sid, syncCookie.ctxcsn );
993 assert( punlock < 0 );
995 } else if (si->si_too_old) {
996 bdn.bv_val[bdn.bv_len] = '\0';
997 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring (%s)\n",
998 si->si_ridtxt, bdn.bv_val, 0 );
999 ldap_controls_free( rctrls );
1003 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1008 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
1010 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
1013 rc = syncrepl_updateCookie( si, op, &syncCookie );
1014 } else switch ( rc ) {
1015 case LDAP_ALREADY_EXISTS:
1016 case LDAP_NO_SUCH_OBJECT:
1017 case LDAP_NO_SUCH_ATTRIBUTE:
1018 case LDAP_TYPE_OR_VALUE_EXISTS:
1019 case LDAP_NOT_ALLOWED_ON_NONLEAF:
1020 rc = LDAP_SYNC_REFRESH_REQUIRED;
1021 si->si_logstate = SYNCLOG_FALLBACK;
1022 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
1023 bdn.bv_val[bdn.bv_len] = '\0';
1024 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync on (%s), switching to REFRESH\n",
1025 si->si_ridtxt, bdn.bv_val, 0 );
1026 if (si->si_strict_refresh) {
1027 slap_suspend_listeners();
1034 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
1035 &modlist, &entry, syncstate, syncUUID ) ) == LDAP_SUCCESS )
1037 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
1038 syncstate, syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
1041 rc = syncrepl_updateCookie( si, op, &syncCookie );
1044 if ( punlock >= 0 ) {
1045 /* on failure, revert pending CSN */
1046 if ( rc != LDAP_SUCCESS ) {
1048 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
1049 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
1050 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
1051 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
1052 &si->si_cookieState->cs_vals[i] );
1056 if ( i == si->si_cookieState->cs_num )
1057 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
1058 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
1060 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
1062 ldap_controls_free( rctrls );
1064 slap_mods_free( modlist, 1 );
1070 case LDAP_RES_SEARCH_REFERENCE:
1071 Debug( LDAP_DEBUG_ANY,
1072 "do_syncrep2: %s reference received error\n",
1073 si->si_ridtxt, 0, 0 );
1076 case LDAP_RES_SEARCH_RESULT:
1077 Debug( LDAP_DEBUG_SYNC,
1078 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
1079 si->si_ridtxt, 0, 0 );
1080 err = LDAP_OTHER; /* FIXME check parse result properly */
1081 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
1083 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
1084 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
1085 /* map old result code to registered code */
1086 err = LDAP_SYNC_REFRESH_REQUIRED;
1089 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
1090 if ( si->si_logstate == SYNCLOG_LOGGING ) {
1091 si->si_logstate = SYNCLOG_FALLBACK;
1092 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync, switching to REFRESH\n",
1093 si->si_ridtxt, 0, 0 );
1094 if (si->si_strict_refresh) {
1095 slap_suspend_listeners();
1103 Debug( LDAP_DEBUG_ANY,
1104 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1105 si->si_ridtxt, err, ldap_err2string( err ) );
1108 LDAPControl **next = NULL;
1109 /* NOTE: make sure we use the right one;
1110 * a better approach would be to run thru
1111 * the whole list and take care of all */
1112 /* NOTE: since we issue the search request,
1113 * we should know what controls to expect,
1114 * and there should be none apart from the
1115 * sync-related control */
1116 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1117 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1119 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1120 "got search result with multiple "
1121 "Sync State control\n", si->si_ridtxt, 0, 0 );
1122 ldap_controls_free( rctrls );
1128 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1130 ber_scanf( ber, "{" /*"}"*/);
1131 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1132 ber_scanf( ber, "m", &cookie );
1134 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1136 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1138 if ( !BER_BVISNULL( &cookie ) ) {
1139 ch_free( syncCookie.octet_str.bv_val );
1140 ber_dupbv( &syncCookie.octet_str, &cookie);
1142 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1144 slap_parse_sync_cookie( &syncCookie, NULL );
1145 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1148 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1150 ber_scanf( ber, "b", &refreshDeletes );
1152 ber_scanf( ber, /*"{"*/ "}" );
1154 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1155 slap_sync_cookie_free( &syncCookie_req, 0 );
1156 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1158 if ( !syncCookie.ctxcsn ) {
1160 } else if ( !syncCookie_req.ctxcsn ) {
1164 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1167 ldap_controls_free( rctrls );
1169 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1170 /* FIXME : different error behaviors according to
1171 * 1) err code : LDAP_BUSY ...
1172 * 2) on err policy : stop service, stop sync, retry
1174 if ( refreshDeletes == 0 && match < 0 &&
1175 err == LDAP_SUCCESS &&
1176 syncCookie_req.numcsns == syncCookie.numcsns )
1178 syncrepl_del_nonpresent( op, si, NULL,
1180 } else if ( si->si_presentlist ) {
1181 presentlist_free( si->si_presentlist );
1182 si->si_presentlist = NULL;
1185 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1187 rc = syncrepl_updateCookie( si, op, &syncCookie );
1189 if ( err == LDAP_SUCCESS
1190 && si->si_logstate == SYNCLOG_FALLBACK ) {
1191 si->si_logstate = SYNCLOG_LOGGING;
1192 rc = LDAP_SYNC_REFRESH_REQUIRED;
1193 slap_resume_listeners();
1199 case LDAP_RES_INTERMEDIATE:
1202 rc = ldap_parse_intermediate( si->si_ld, msg,
1203 &retoid, &retdata, NULL, 0 );
1204 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1205 ber_init2( ber, retdata, LBER_USE_DER );
1207 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1209 case LDAP_TAG_SYNC_NEW_COOKIE:
1210 Debug( LDAP_DEBUG_SYNC,
1211 "do_syncrep2: %s %s - %s\n",
1213 "LDAP_RES_INTERMEDIATE",
1215 ber_scanf( ber, "tm", &tag, &cookie );
1216 Debug( LDAP_DEBUG_SYNC,
1217 "do_syncrep2: %s NEW_COOKIE: %s\n",
1220 if ( !BER_BVISNULL( &cookie ) ) {
1221 ch_free( syncCookie.octet_str.bv_val );
1222 ber_dupbv( &syncCookie.octet_str, &cookie );
1224 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1225 slap_parse_sync_cookie( &syncCookie, NULL );
1226 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1229 case LDAP_TAG_SYNC_REFRESH_DELETE:
1230 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1231 Debug( LDAP_DEBUG_SYNC,
1232 "do_syncrep2: %s %s - %s\n",
1234 "LDAP_RES_INTERMEDIATE",
1235 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1236 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1237 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1238 si->si_refreshDelete = 1;
1240 si->si_refreshPresent = 1;
1242 ber_scanf( ber, "t{" /*"}"*/, &tag );
1243 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1245 ber_scanf( ber, "m", &cookie );
1247 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1249 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1251 if ( !BER_BVISNULL( &cookie ) ) {
1252 ch_free( syncCookie.octet_str.bv_val );
1253 ber_dupbv( &syncCookie.octet_str, &cookie );
1255 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1257 slap_parse_sync_cookie( &syncCookie, NULL );
1258 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1261 /* Defaults to TRUE */
1262 if ( ber_peek_tag( ber, &len ) ==
1263 LDAP_TAG_REFRESHDONE )
1265 ber_scanf( ber, "b", &si->si_refreshDone );
1268 si->si_refreshDone = 1;
1270 ber_scanf( ber, /*"{"*/ "}" );
1271 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
1272 si->si_refreshDone )
1275 case LDAP_TAG_SYNC_ID_SET:
1276 Debug( LDAP_DEBUG_SYNC,
1277 "do_syncrep2: %s %s - %s\n",
1279 "LDAP_RES_INTERMEDIATE",
1281 ber_scanf( ber, "t{" /*"}"*/, &tag );
1282 if ( ber_peek_tag( ber, &len ) ==
1283 LDAP_TAG_SYNC_COOKIE )
1285 ber_scanf( ber, "m", &cookie );
1287 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1289 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1291 if ( !BER_BVISNULL( &cookie ) ) {
1292 ch_free( syncCookie.octet_str.bv_val );
1293 ber_dupbv( &syncCookie.octet_str, &cookie );
1295 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1297 slap_parse_sync_cookie( &syncCookie, NULL );
1298 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1299 compare_csns( &syncCookie_req, &syncCookie, &m );
1302 if ( ber_peek_tag( ber, &len ) ==
1303 LDAP_TAG_REFRESHDELETES )
1305 ber_scanf( ber, "b", &refreshDeletes );
1308 rc = ber_scanf( ber, "[W]", &syncUUIDs );
1309 ber_scanf( ber, /*"{"*/ "}" );
1310 if ( rc != LBER_ERROR ) {
1311 if ( refreshDeletes ) {
1312 syncrepl_del_nonpresent( op, si, syncUUIDs,
1314 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1317 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1318 (void)presentlist_insert( si, &syncUUIDs[i] );
1319 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1321 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1325 slap_sync_cookie_free( &syncCookie, 0 );
1328 Debug( LDAP_DEBUG_ANY,
1329 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1330 si->si_ridtxt, (long) si_tag, 0 );
1331 ldap_memfree( retoid );
1332 ber_bvfree( retdata );
1336 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1337 slap_sync_cookie_free( &syncCookie_req, 0 );
1338 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1340 if ( !syncCookie.ctxcsn ) {
1342 } else if ( !syncCookie_req.ctxcsn ) {
1346 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1350 if ( si->si_refreshPresent == 1 &&
1351 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1352 syncCookie_req.numcsns == syncCookie.numcsns ) {
1353 syncrepl_del_nonpresent( op, si, NULL,
1357 if ( syncCookie.ctxcsn )
1359 rc = syncrepl_updateCookie( si, op, &syncCookie);
1361 if ( si->si_presentlist ) {
1362 presentlist_free( si->si_presentlist );
1363 si->si_presentlist = NULL;
1367 ldap_memfree( retoid );
1368 ber_bvfree( retdata );
1374 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1375 "unknown intermediate response (%d)\n",
1376 si->si_ridtxt, rc, 0 );
1377 ldap_memfree( retoid );
1378 ber_bvfree( retdata );
1383 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1384 "unknown message (0x%02lx)\n",
1386 (unsigned long)ldap_msgtype( msg ), 0 );
1390 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1391 slap_sync_cookie_free( &syncCookie_req, 0 );
1392 syncCookie_req = syncCookie;
1393 memset( &syncCookie, 0, sizeof( syncCookie ));
1395 ldap_msgfree( msg );
1397 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1398 slap_sync_cookie_free( &syncCookie, 0 );
1399 slap_sync_cookie_free( &syncCookie_req, 0 );
1406 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1411 if ( err != LDAP_SUCCESS ) {
1412 Debug( LDAP_DEBUG_ANY,
1413 "do_syncrep2: %s (%d) %s\n",
1414 si->si_ridtxt, err, ldap_err2string( err ) );
1417 slap_sync_cookie_free( &syncCookie, 0 );
1418 slap_sync_cookie_free( &syncCookie_req, 0 );
1420 if ( msg ) ldap_msgfree( msg );
1422 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1423 if ( si->si_conn ) {
1424 connection_client_stop( si->si_conn );
1427 ldap_unbind_ext( si->si_ld, NULL, NULL );
1439 struct re_s* rtask = arg;
1440 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1441 Connection conn = {0};
1442 OperationBuffer opbuf;
1444 int rc = LDAP_SUCCESS;
1447 int i, defer = 1, fail = 0, freeinfo = 0;
1452 if ( slapd_shutdown )
1455 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1457 /* Don't get stuck here while a pause is initiated */
1458 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1459 if ( slapd_shutdown )
1461 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1462 ldap_pvt_thread_yield();
1467 if ( si->si_ctype < 1 ) {
1471 switch( abs( si->si_type ) ) {
1472 case LDAP_SYNC_REFRESH_ONLY:
1473 case LDAP_SYNC_REFRESH_AND_PERSIST:
1476 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1480 if ( slapd_shutdown ) {
1482 if ( si->si_conn ) {
1483 connection_client_stop( si->si_conn );
1486 ldap_unbind_ext( si->si_ld, NULL, NULL );
1489 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1493 connection_fake_init( &conn, &opbuf, ctx );
1495 /* o_connids must be unique for slap_graduate_commit_csn */
1496 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1498 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1501 /* Coordinate contextCSN updates with any syncprov overlays
1502 * in use. This may be complicated by the use of the glue
1505 * Typically there is a single syncprov mastering the entire
1506 * glued tree. In that case, our contextCSN updates should
1507 * go to the master DB. But if there is no syncprov on the
1508 * master DB, then nothing special is needed here.
1510 * Alternatively, there may be individual syncprov overlays
1511 * on each glued branch. In that case, each syncprov only
1512 * knows about changes within its own branch. And so our
1513 * contextCSN updates should only go to the local DB.
1515 if ( !si->si_wbe ) {
1516 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1517 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1518 if ( overlay_is_inst( top_be, "syncprov" ))
1519 si->si_wbe = top_be;
1525 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1526 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1527 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1529 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1532 if ( !si->si_schemachecking )
1533 op->o_no_schema_check = 1;
1535 /* Establish session, do search */
1537 si->si_refreshDelete = 0;
1538 si->si_refreshPresent = 0;
1540 if ( si->si_presentlist ) {
1541 presentlist_free( si->si_presentlist );
1542 si->si_presentlist = NULL;
1545 /* use main DB when retrieving contextCSN */
1546 op->o_bd = si->si_wbe;
1547 op->o_dn = op->o_bd->be_rootdn;
1548 op->o_ndn = op->o_bd->be_rootndn;
1549 rc = do_syncrep1( op, si );
1553 /* Process results */
1554 if ( rc == LDAP_SUCCESS ) {
1555 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1557 /* use current DB */
1559 op->o_dn = op->o_bd->be_rootdn;
1560 op->o_ndn = op->o_bd->be_rootndn;
1561 rc = do_syncrep2( op, si );
1562 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1563 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
1564 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
1565 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
1566 si->si_syncCookie.sid );
1567 rc = ldap_sync_search( si, op->o_tmpmemctx );
1572 /* We got deleted while running on cn=config */
1573 if ( si->si_ctype < 1 ) {
1574 if ( si->si_ctype == -1 ) {
1583 if ( rc != SYNC_PAUSED ) {
1584 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1585 /* If we succeeded, enable the connection for further listening.
1586 * If we failed, tear down the connection and reschedule.
1588 if ( rc == LDAP_SUCCESS ) {
1589 if ( si->si_conn ) {
1590 connection_client_enable( si->si_conn );
1592 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1594 } else if ( si->si_conn ) {
1598 if ( rc == -2 ) rc = 0;
1603 /* At this point, we have 5 cases:
1604 * 1) for any hard failure, give up and remove this task
1605 * 2) for ServerDown, reschedule this task to run later
1606 * 3) for threadpool pause, reschedule to run immediately
1607 * 4) for Refresh and Success, reschedule to run
1608 * 5) for Persist and Success, reschedule to defer
1610 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1612 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1613 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1617 connection_client_stop( si->si_conn );
1621 if ( rc == SYNC_PAUSED ) {
1622 rtask->interval.tv_sec = 0;
1623 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1624 rtask->interval.tv_sec = si->si_interval;
1626 } else if ( rc == LDAP_SUCCESS ) {
1627 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1630 rtask->interval.tv_sec = si->si_interval;
1631 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1632 if ( si->si_retrynum ) {
1633 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1634 si->si_retrynum[i] = si->si_retrynum_init[i];
1636 si->si_retrynum[i] = RETRYNUM_TAIL;
1639 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1640 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1644 if ( si->si_ctype < 1
1645 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1647 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1650 fail = RETRYNUM_TAIL;
1651 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1652 if ( si->si_retrynum[i] > 0 )
1653 si->si_retrynum[i]--;
1654 fail = si->si_retrynum[i];
1655 rtask->interval.tv_sec = si->si_retryinterval[i];
1656 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1657 slap_wake_listener();
1661 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1662 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1665 if ( fail == RETRYNUM_TAIL ) {
1666 Debug( LDAP_DEBUG_ANY,
1667 "do_syncrepl: %s rc %d quitting\n",
1668 si->si_ridtxt, rc, 0 );
1669 } else if ( fail > 0 ) {
1670 Debug( LDAP_DEBUG_ANY,
1671 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1672 si->si_ridtxt, rc, fail );
1674 Debug( LDAP_DEBUG_ANY,
1675 "do_syncrepl: %s rc %d retrying\n",
1676 si->si_ridtxt, rc, 0 );
1680 /* Do final delete cleanup */
1682 syncinfo_free( si, 0 );
1687 #ifdef ENABLE_REWRITE
1689 syncrepl_rewrite_dn(
1692 struct berval *sdn )
1697 nul = dn->bv_val[dn->bv_len];
1698 dn->bv_val[dn->bv_len] = 0;
1699 rc = rewrite( si->si_rewrite, SUFFIXM_CTX, dn->bv_val, &sdn->bv_val );
1700 dn->bv_val[dn->bv_len] = nul;
1702 if ( sdn->bv_val == dn->bv_val )
1704 else if ( rc == REWRITE_REGEXEC_OK && sdn->bv_val )
1705 sdn->bv_len = strlen( sdn->bv_val );
1708 #define REWRITE_VAL(si, ad, bv, bv2) \
1709 BER_BVZERO( &bv2 ); \
1710 if ( si->si_rewrite && ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName) \
1711 syncrepl_rewrite_dn( si, &bv, &bv2); \
1712 if ( BER_BVISNULL( &bv2 )) \
1713 ber_dupbv( &bv2, &bv )
1714 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1715 BER_BVZERO( &bv2 ); \
1716 if (si->si_rewrite) \
1717 syncrepl_rewrite_dn(si, &bv, &bv2); \
1718 rc = dnPrettyNormal( NULL, bv2.bv_val ? &bv2 : &bv, &dn, &ndn, op->o_tmpmemctx ); \
1721 #define REWRITE_VAL(si, ad, bv, bv2) ber_dupbv(&bv2, &bv)
1722 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1723 rc = dnPrettyNormal( NULL, &bv, &dn, &ndn, op->o_tmpmemctx )
1727 static slap_verbmasks modops[] = {
1728 { BER_BVC("add"), LDAP_REQ_ADD },
1729 { BER_BVC("delete"), LDAP_REQ_DELETE },
1730 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1731 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1736 syncrepl_accesslog_mods(
1738 struct berval *vals,
1739 struct Modifications **modres
1744 AttributeDescription *ad;
1745 struct berval bv, bv2;
1747 Modifications *mod = NULL, *modlist = NULL, **modtail;
1752 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1756 colon = ber_bvchr( &bv, ':' );
1762 bv.bv_len = colon - bv.bv_val;
1763 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1765 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1766 "Invalid attribute %s, %s\n",
1767 si->si_ridtxt, bv.bv_val, text );
1768 slap_mods_free( modlist, 1 );
1774 /* Ignore dynamically generated attrs */
1775 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1779 /* Ignore excluded attrs */
1780 if ( ldap_charray_inlist( si->si_exattrs,
1781 ad->ad_type->sat_cname.bv_val ) )
1787 case '+': op = LDAP_MOD_ADD; break;
1788 case '-': op = LDAP_MOD_DELETE; break;
1789 case '=': op = LDAP_MOD_REPLACE; break;
1790 case '#': op = LDAP_MOD_INCREMENT; break;
1794 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1795 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1798 mod->sml_next = NULL;
1800 mod->sml_type = ad->ad_cname;
1801 mod->sml_values = NULL;
1802 mod->sml_nvalues = NULL;
1803 mod->sml_numvals = 0;
1806 modtail = &mod->sml_next;
1808 if ( colon[2] == ' ' ) {
1809 bv.bv_val = colon + 3;
1810 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1811 REWRITE_VAL( si, ad, bv, bv2 );
1812 ber_bvarray_add( &mod->sml_values, &bv2 );
1821 syncrepl_changelog_mods(
1823 struct berval *vals,
1824 struct Modifications **modres
1827 return -1; /* FIXME */
1830 typedef struct OpExtraSync {
1835 /* Copy the original modlist, split Replace ops into Delete/Add,
1836 * and drop mod opattrs since this modification is in the past.
1838 static Modifications *mods_dup( Operation *op, Modifications *modlist, int match )
1840 Modifications *mod, *modnew = NULL, *modtail = NULL;
1842 for ( ; modlist; modlist = modlist->sml_next ) {
1845 if ( modlist->sml_desc == slap_schema.si_ad_modifiersName ||
1846 modlist->sml_desc == slap_schema.si_ad_modifyTimestamp ||
1847 modlist->sml_desc == slap_schema.si_ad_entryCSN )
1849 if ( modlist->sml_op == LDAP_MOD_REPLACE ) {
1850 mod = op->o_tmpalloc( sizeof(Modifications), op->o_tmpmemctx );
1851 mod->sml_desc = modlist->sml_desc;
1852 mod->sml_values = NULL;
1853 mod->sml_nvalues = NULL;
1854 mod->sml_op = LDAP_MOD_DELETE;
1855 mod->sml_numvals = 0;
1860 modtail->sml_next = mod;
1864 if ( modlist->sml_numvals ) {
1865 size = (modlist->sml_numvals+1) * sizeof(struct berval);
1866 if ( modlist->sml_nvalues ) size *= 2;
1870 size += sizeof(Modifications);
1871 mod = op->o_tmpalloc( size, op->o_tmpmemctx );
1875 modtail->sml_next = mod;
1877 mod->sml_desc = modlist->sml_desc;
1878 mod->sml_numvals = modlist->sml_numvals;
1880 if ( modlist->sml_numvals ) {
1882 mod->sml_values = (BerVarray)(mod+1);
1883 for (i=0; i<mod->sml_numvals; i++)
1884 mod->sml_values[i] = modlist->sml_values[i];
1885 BER_BVZERO(&mod->sml_values[i]);
1886 if ( modlist->sml_nvalues ) {
1887 mod->sml_nvalues = mod->sml_values + mod->sml_numvals + 1;
1888 for (i=0; i<mod->sml_numvals; i++)
1889 mod->sml_nvalues[i] = modlist->sml_nvalues[i];
1890 BER_BVZERO(&mod->sml_nvalues[i]);
1892 mod->sml_nvalues = NULL;
1895 mod->sml_values = NULL;
1896 mod->sml_nvalues = NULL;
1898 if ( match < 0 && modlist->sml_op == LDAP_MOD_REPLACE )
1899 mod->sml_op = LDAP_MOD_ADD;
1901 mod->sml_op = modlist->sml_op;
1902 mod->sml_next = NULL;
1907 typedef struct resolve_ctxt {
1909 Modifications *rx_mods;
1913 compare_vals( Modifications *m1, Modifications *m2 )
1916 struct berval *bv1, *bv2;
1918 if ( m2->sml_nvalues ) {
1919 bv2 = m2->sml_nvalues;
1920 bv1 = m1->sml_nvalues;
1922 bv2 = m2->sml_values;
1923 bv1 = m1->sml_values;
1925 for ( j=0; j<m2->sml_numvals; j++ ) {
1926 for ( i=0; i<m1->sml_numvals; i++ ) {
1927 if ( !ber_bvcmp( &bv1[i], &bv2[j] )) {
1929 for ( k=i; k<m1->sml_numvals-1; k++ ) {
1930 m1->sml_values[k] = m1->sml_values[k+1];
1931 if ( m1->sml_nvalues )
1932 m1->sml_nvalues[k] = m1->sml_nvalues[k+1];
1934 BER_BVZERO(&m1->sml_values[k]);
1935 if ( m1->sml_nvalues ) {
1936 BER_BVZERO(&m1->sml_nvalues[k]);
1946 syncrepl_resolve_cb( Operation *op, SlapReply *rs )
1948 if ( rs->sr_type == REP_SEARCH ) {
1949 resolve_ctxt *rx = op->o_callback->sc_private;
1950 Attribute *a = attr_find( rs->sr_entry->e_attrs, ad_reqMod );
1952 Modifications *oldmods, *newmods, *m1, *m2, **prev;
1953 oldmods = rx->rx_mods;
1954 syncrepl_accesslog_mods( rx->rx_si, a->a_vals, &newmods );
1955 for ( m2 = newmods; m2; m2=m2->sml_next ) {
1956 for ( prev = &oldmods, m1 = *prev; m1; m1 = *prev ) {
1957 if ( m1->sml_desc != m2->sml_desc ) {
1958 prev = &m1->sml_next;
1961 if ( m2->sml_op == LDAP_MOD_DELETE ||
1962 m2->sml_op == LDAP_MOD_REPLACE ) {
1963 int numvals = m2->sml_numvals;
1964 if ( m2->sml_op == LDAP_MOD_REPLACE )
1966 /* New delete All cancels everything */
1967 if ( numvals == 0 ) {
1969 *prev = m1->sml_next;
1970 op->o_tmpfree( m1, op->o_tmpmemctx );
1973 if ( m1->sml_op == LDAP_MOD_DELETE ) {
1974 if ( m1->sml_numvals == 0 ) {
1975 /* turn this to SOFTDEL later */
1976 m1->sml_flags = SLAP_MOD_INTERNAL;
1978 compare_vals( m1, m2 );
1979 if ( !m1->sml_numvals )
1982 } else if ( m1->sml_op == LDAP_MOD_ADD ) {
1983 compare_vals( m1, m2 );
1984 if ( !m1->sml_numvals )
1989 if ( m2->sml_op == LDAP_MOD_ADD ||
1990 m2->sml_op == LDAP_MOD_REPLACE ) {
1991 if ( m1->sml_op == LDAP_MOD_DELETE ) {
1992 if ( !m1->sml_numvals ) goto drop;
1993 compare_vals( m1, m2 );
1994 if ( !m1->sml_numvals )
1997 if ( m2->sml_desc->ad_type->sat_atype.at_single_value )
1999 compare_vals( m1, m2 );
2000 if ( !m1->sml_numvals )
2003 prev = &m1->sml_next;
2006 slap_mods_free( newmods, 1 );
2007 rx->rx_mods = oldmods;
2010 return LDAP_SUCCESS;
2013 typedef struct modify_ctxt {
2014 Modifications *mx_orig;
2015 Modifications *mx_free;
2019 syncrepl_modify_cb( Operation *op, SlapReply *rs )
2021 slap_callback *sc = op->o_callback;
2022 modify_ctxt *mx = sc->sc_private;
2025 op->orm_no_opattrs = 0;
2026 op->orm_modlist = mx->mx_orig;
2027 for ( ml = mx->mx_free; ml; ml = mx->mx_free ) {
2028 mx->mx_free = ml->sml_next;
2029 op->o_tmpfree( ml, op->o_tmpmemctx );
2031 op->o_callback = sc->sc_next;
2032 op->o_tmpfree( sc, op->o_tmpmemctx );
2033 return SLAP_CB_CONTINUE;
2037 syncrepl_op_modify( Operation *op, SlapReply *rs )
2039 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2044 Modifications *mod, *newlist;
2046 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
2047 if ( oex->oe_key == (void *)syncrepl_message_to_op )
2051 return SLAP_CB_CONTINUE;
2053 si = ((OpExtraSync *)oex)->oe_si;
2055 /* Check if entryCSN in modlist is newer than entryCSN in entry.
2056 * We do it here because the op has been serialized by accesslog
2057 * by the time we get here. If the CSN is new enough, just do the
2058 * mod. If not, we need to resolve conflicts.
2061 for ( mod = op->orm_modlist; mod; mod=mod->sml_next ) {
2062 if ( mod->sml_desc == slap_schema.si_ad_entryCSN ) break;
2064 /* FIXME: what should we do if entryCSN is missing from the mod? */
2066 return SLAP_CB_CONTINUE;
2068 rc = overlay_entry_get_ov( op, &op->o_req_ndn, NULL, NULL, 0, &e, on );
2072 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
2073 value_match( &match, slap_schema.si_ad_entryCSN,
2074 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
2075 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2076 &mod->sml_nvalues[0], &a->a_nvals[0], &text );
2077 overlay_entry_release_ov( op, e, 0, on );
2079 /* equal? Should never happen */
2081 /* tell accesslog this was a failure */
2082 rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
2083 return LDAP_SUCCESS;
2086 /* mod is older: resolve conflicts...
2087 * 1. Save/copy original modlist. Split Replace to Del/Add.
2088 * 2. Find all mods to this reqDN newer than the mod stamp.
2089 * 3. Resolve any mods in this request that affect attributes
2090 * touched by newer mods.
2092 * delete all delete all drop
2093 * delete all delete X SOFTDEL
2094 * delete X delete all drop
2095 * delete X delete X drop
2096 * delete X delete Y OK
2097 * delete all add X drop
2098 * delete X add X drop
2100 * add X delete all drop
2101 * add X delete X drop
2103 * add X add Y if SV, drop else OK
2105 * 4. Swap original modlist back in response callback so
2106 * that accesslog logs the original mod.
2108 * Even if the mod is newer, other out-of-order changes may
2109 * have been committed, forcing us to tweak the modlist:
2110 * 1. Save/copy original modlist.
2111 * 2. Change deletes to soft deletes.
2112 * 3. Change Adds of single-valued attrs to Replace.
2115 newlist = mods_dup( op, op->orm_modlist, match );
2119 Operation op2 = *op;
2120 AttributeName an[2];
2126 SlapReply rs1 = {0};
2128 slap_callback cb = { NULL, syncrepl_resolve_cb, NULL, NULL };
2131 rx.rx_mods = newlist;
2132 cb.sc_private = ℞
2134 op2.o_tag = LDAP_REQ_SEARCH;
2135 op2.ors_scope = LDAP_SCOPE_SUBTREE;
2136 op2.ors_deref = LDAP_DEREF_NEVER;
2137 op2.o_req_dn = si->si_logbase;
2138 op2.o_req_ndn = si->si_logbase;
2139 op2.ors_tlimit = SLAP_NO_LIMIT;
2140 op2.ors_slimit = SLAP_NO_LIMIT;
2141 op2.ors_limit = NULL;
2142 memset( an, 0, sizeof(an));
2143 an[0].an_desc = ad_reqMod;
2144 an[0].an_name = ad_reqMod->ad_cname;
2146 op2.ors_attrsonly = 0;
2148 bv = mod->sml_nvalues[0];
2150 size = sizeof("(&(entryCSN>=)(reqDN=))");
2151 size += bv.bv_len + op->o_req_ndn.bv_len + si->si_logfilterstr.bv_len;
2152 op2.ors_filterstr.bv_val = op->o_tmpalloc( size, op->o_tmpmemctx );
2153 op2.ors_filterstr.bv_len = sprintf(op2.ors_filterstr.bv_val,
2154 "(&(entryCSN>=%s)(reqDN=%s)%s)",
2155 bv.bv_val, op->o_req_ndn.bv_val, si->si_logfilterstr.bv_val );
2156 op2.ors_filter = str2filter_x( op, op2.ors_filterstr.bv_val );
2158 op2.o_callback = &cb;
2159 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2160 op2.o_bd->be_search( &op2, &rs1 );
2161 newlist = rx.rx_mods;
2165 slap_callback *sc = op->o_tmpalloc( sizeof(slap_callback) +
2166 sizeof(modify_ctxt), op->o_tmpmemctx );
2167 modify_ctxt *mx = (modify_ctxt *)(sc+1);
2170 sc->sc_response = syncrepl_modify_cb;
2171 sc->sc_private = mx;
2172 sc->sc_next = op->o_callback;
2173 sc->sc_cleanup = NULL;
2174 op->o_callback = sc;
2175 op->orm_no_opattrs = 1;
2176 mx->mx_orig = op->orm_modlist;
2177 mx->mx_free = newlist;
2178 for ( ml = newlist; ml; ml=ml->sml_next ) {
2179 if ( ml->sml_flags == SLAP_MOD_INTERNAL ) {
2181 ml->sml_op = SLAP_MOD_SOFTDEL;
2183 else if ( ml->sml_op == LDAP_MOD_DELETE )
2184 ml->sml_op = SLAP_MOD_SOFTDEL;
2185 else if ( ml->sml_op == LDAP_MOD_ADD &&
2186 ml->sml_desc->ad_type->sat_atype.at_single_value )
2187 ml->sml_op = LDAP_MOD_REPLACE;
2189 op->orm_modlist = newlist;
2190 op->o_csn = mod->sml_nvalues[0];
2192 return SLAP_CB_CONTINUE;
2196 syncrepl_message_to_op(
2202 BerElement *ber = NULL;
2203 Modifications *modlist = NULL;
2205 SlapReply rs = { REP_RESULT };
2206 slap_callback cb = { NULL, null_callback, NULL, NULL };
2209 char txtbuf[SLAP_TEXT_BUFLEN];
2210 size_t textlen = sizeof txtbuf;
2212 struct berval bdn, dn = BER_BVNULL, ndn;
2213 struct berval bv, bv2, *bvals = NULL;
2214 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
2215 prdn = BER_BVNULL, nrdn = BER_BVNULL,
2216 psup = BER_BVNULL, nsup = BER_BVNULL;
2217 int rc, deleteOldRdn = 0, freeReqDn = 0;
2218 int do_graduate = 0;
2220 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2221 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2222 "Message type should be entry (%d)",
2223 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2227 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
2232 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2234 if ( rc != LDAP_SUCCESS ) {
2235 Debug( LDAP_DEBUG_ANY,
2236 "syncrepl_message_to_op: %s dn get failed (%d)",
2237 si->si_ridtxt, rc, 0 );
2241 op->o_tag = LBER_DEFAULT;
2242 op->o_bd = si->si_wbe;
2244 if ( BER_BVISEMPTY( &bdn )) {
2245 Debug( LDAP_DEBUG_ANY,
2246 "syncrepl_message_to_op: %s got empty dn",
2247 si->si_ridtxt, 0, 0 );
2251 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
2253 if ( bv.bv_val == NULL )
2256 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
2258 REWRITE_DN( si, bdn, bv2, dn, ndn );
2259 if ( rc != LDAP_SUCCESS ) {
2260 Debug( LDAP_DEBUG_ANY,
2261 "syncrepl_message_to_op: %s "
2262 "dn \"%s\" normalization failed (%d)",
2263 si->si_ridtxt, bdn.bv_val, rc );
2268 ber_dupbv( &op->o_req_dn, &dn );
2269 ber_dupbv( &op->o_req_ndn, &ndn );
2270 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2271 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2273 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
2274 int i = verb_to_mask( bvals[0].bv_val, modops );
2276 Debug( LDAP_DEBUG_ANY,
2277 "syncrepl_message_to_op: %s unknown op %s",
2278 si->si_ridtxt, bvals[0].bv_val, 0 );
2283 op->o_tag = modops[i].mask;
2284 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
2285 /* Parse attribute into modlist */
2286 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
2287 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
2289 rc = syncrepl_changelog_mods( si, bvals, &modlist );
2291 if ( rc ) goto done;
2292 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
2294 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
2295 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
2298 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
2300 } else if ( !ber_bvstrcasecmp( &bv,
2301 &slap_schema.si_ad_entryCSN->ad_cname ) )
2303 slap_queue_csn( op, bvals );
2309 /* If we didn't get a mod type or a target DN, bail out */
2310 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
2315 op->o_callback = &cb;
2316 slap_op_time( &op->o_time, &op->o_tincr );
2318 switch( op->o_tag ) {
2320 case LDAP_REQ_MODIFY:
2321 /* If we didn't get required data, bail */
2322 if ( !modlist ) goto done;
2324 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
2326 if ( rc != LDAP_SUCCESS ) {
2327 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2328 "mods check (%s)\n",
2329 si->si_ridtxt, text, 0 );
2333 if ( op->o_tag == LDAP_REQ_ADD ) {
2334 Entry *e = entry_alloc();
2336 op->ora_e->e_name = op->o_req_dn;
2337 op->ora_e->e_nname = op->o_req_ndn;
2339 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
2340 if( rc != LDAP_SUCCESS ) {
2341 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2342 "mods2entry (%s)\n",
2343 si->si_ridtxt, text, 0 );
2345 rc = op->o_bd->be_add( op, &rs );
2346 Debug( LDAP_DEBUG_SYNC,
2347 "syncrepl_message_to_op: %s be_add %s (%d)\n",
2348 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2351 if ( e == op->ora_e )
2352 be_entry_release_w( op, op->ora_e );
2355 op->orm_modlist = modlist;
2356 op->o_bd = si->si_wbe;
2357 /* delta-mmr needs additional checks in syncrepl_op_modify */
2358 if ( SLAP_MULTIMASTER( op->o_bd )) {
2359 oes.oe.oe_key = (void *)syncrepl_message_to_op;
2361 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &oes.oe, oe_next );
2363 rc = op->o_bd->be_modify( op, &rs );
2364 if ( SLAP_MULTIMASTER( op->o_bd )) {
2365 LDAP_SLIST_REMOVE( &op->o_extra, &oes.oe, OpExtra, oe_next );
2366 BER_BVZERO( &op->o_csn );
2368 modlist = op->orm_modlist;
2369 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2370 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
2371 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2372 op->o_bd = si->si_be;
2376 case LDAP_REQ_MODRDN:
2377 if ( BER_BVISNULL( &rdn ) ) goto done;
2379 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
2382 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
2385 if ( !BER_BVISNULL( &sup ) ) {
2386 REWRITE_DN( si, sup, bv2, psup, nsup );
2389 op->orr_newSup = &psup;
2390 op->orr_nnewSup = ⊅
2392 op->orr_newSup = NULL;
2393 op->orr_nnewSup = NULL;
2395 op->orr_newrdn = prdn;
2396 op->orr_nnewrdn = nrdn;
2397 op->orr_deleteoldrdn = deleteOldRdn;
2398 op->orr_modlist = NULL;
2399 if ( slap_modrdn2mods( op, &rs ) ) {
2403 /* Append modlist for operational attrs */
2407 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
2409 m->sml_next = modlist;
2412 rc = op->o_bd->be_modrdn( op, &rs );
2413 slap_mods_free( op->orr_modlist, 1 );
2414 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2415 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
2416 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2419 case LDAP_REQ_DELETE:
2420 rc = op->o_bd->be_delete( op, &rs );
2421 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2422 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
2423 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2429 slap_graduate_commit_csn( op );
2430 op->o_bd = si->si_be;
2431 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2432 BER_BVZERO( &op->o_csn );
2434 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
2436 if ( !BER_BVISNULL( &rdn ) ) {
2437 if ( !BER_BVISNULL( &nsup ) ) {
2438 ch_free( nsup.bv_val );
2440 if ( !BER_BVISNULL( &psup ) ) {
2441 ch_free( psup.bv_val );
2443 if ( !BER_BVISNULL( &nrdn ) ) {
2444 ch_free( nrdn.bv_val );
2446 if ( !BER_BVISNULL( &prdn ) ) {
2447 ch_free( prdn.bv_val );
2451 ch_free( op->o_req_ndn.bv_val );
2452 ch_free( op->o_req_dn.bv_val );
2459 syncrepl_message_to_entry(
2463 Modifications **modlist,
2466 struct berval *syncUUID
2470 BerElement *ber = NULL;
2473 Modifications **modtail = modlist;
2476 char txtbuf[SLAP_TEXT_BUFLEN];
2477 size_t textlen = sizeof txtbuf;
2479 struct berval bdn = BER_BVNULL, dn, ndn, bv2;
2484 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2485 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
2486 "Message type should be entry (%d)",
2487 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2491 op->o_tag = LDAP_REQ_ADD;
2493 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2494 if ( rc != LDAP_SUCCESS ) {
2495 Debug( LDAP_DEBUG_ANY,
2496 "syncrepl_message_to_entry: %s dn get failed (%d)",
2497 si->si_ridtxt, rc, 0 );
2501 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
2502 Debug( LDAP_DEBUG_ANY,
2503 "syncrepl_message_to_entry: %s got empty dn",
2504 si->si_ridtxt, 0, 0 );
2508 /* syncUUID[0] is normalized UUID received over the wire
2509 * syncUUID[1] is denormalized UUID, generated here
2511 (void)slap_uuidstr_from_normalized( &syncUUID[1], &syncUUID[0], op->o_tmpmemctx );
2512 Debug( LDAP_DEBUG_SYNC,
2513 "syncrepl_message_to_entry: %s DN: %s, UUID: %s\n",
2514 si->si_ridtxt, bdn.bv_val, syncUUID[1].bv_val );
2516 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
2517 /* NOTE: this could be done even before decoding the DN,
2518 * although encoding errors wouldn't be detected */
2523 if ( entry == NULL ) {
2527 REWRITE_DN( si, bdn, bv2, dn, ndn );
2528 if ( rc != LDAP_SUCCESS ) {
2529 /* One of the things that could happen is that the schema
2530 * is not lined-up; this could result in unknown attributes.
2531 * A value non conformant to the syntax should be unlikely,
2532 * except when replicating between different versions
2533 * of the software, or when syntax validation bugs are fixed
2535 Debug( LDAP_DEBUG_ANY,
2536 "syncrepl_message_to_entry: "
2537 "%s dn \"%s\" normalization failed (%d)",
2538 si->si_ridtxt, bdn.bv_val, rc );
2542 ber_dupbv( &op->o_req_dn, &dn );
2543 ber_dupbv( &op->o_req_ndn, &ndn );
2544 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2545 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2547 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
2550 e->e_name = op->o_req_dn;
2551 e->e_nname = op->o_req_ndn;
2553 while ( ber_remaining( ber ) ) {
2554 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
2555 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2560 /* Drop all updates to the contextCSN of the context entry
2563 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2564 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2565 ber_bvarray_free( tmp.sml_values );
2569 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2571 mod->sml_op = LDAP_MOD_REPLACE;
2573 mod->sml_next = NULL;
2574 mod->sml_desc = NULL;
2575 mod->sml_type = tmp.sml_type;
2576 mod->sml_values = tmp.sml_values;
2577 mod->sml_nvalues = NULL;
2578 mod->sml_numvals = 0; /* slap_mods_check will set this */
2580 #ifdef ENABLE_REWRITE
2581 if (si->si_rewrite) {
2582 AttributeDescription *ad = NULL;
2583 slap_bv2ad( &tmp.sml_type, &ad, &text );
2586 mod->sml_type = ad->ad_cname;
2587 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
2589 for ( i = 0; tmp.sml_values[i].bv_val; i++ ) {
2590 syncrepl_rewrite_dn( si, &tmp.sml_values[i], &bv2);
2591 if ( !BER_BVISNULL( &bv2 )) {
2592 ber_memfree( tmp.sml_values[i].bv_val );
2593 tmp.sml_values[i] = bv2;
2601 modtail = &mod->sml_next;
2604 if ( *modlist == NULL ) {
2605 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2606 si->si_ridtxt, 0, 0 );
2611 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2613 if ( rc != LDAP_SUCCESS ) {
2614 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2615 si->si_ridtxt, text, 0 );
2619 /* Strip out dynamically generated attrs */
2620 for ( modtail = modlist; *modtail ; ) {
2622 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2623 *modtail = mod->sml_next;
2624 slap_mod_free( &mod->sml_mod, 0 );
2627 modtail = &mod->sml_next;
2631 /* Strip out attrs in exattrs list */
2632 for ( modtail = modlist; *modtail ; ) {
2634 if ( ldap_charray_inlist( si->si_exattrs,
2635 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2637 *modtail = mod->sml_next;
2638 slap_mod_free( &mod->sml_mod, 0 );
2641 modtail = &mod->sml_next;
2645 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2646 if( rc != LDAP_SUCCESS ) {
2647 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2648 si->si_ridtxt, text, 0 );
2653 if ( rc != LDAP_SUCCESS ) {
2665 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2667 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2668 * entry if a previous refresh was interrupted before sending us a new
2669 * context state. We try to compare the new entry to the existing entry
2670 * and ignore the new entry if they are the same.
2672 * Also, we may get an update where the entryDN has changed, due to
2673 * a ModDn on the provider. We detect this as well, so we can issue
2674 * the corresponding operation locally.
2676 * In the case of a modify, we get a list of all the attributes
2677 * in the original entry. Rather than deleting the entry and re-adding it,
2678 * we issue a Modify request that deletes all the attributes and adds all
2679 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2682 * We otherwise distinguish ModDN from Modify; in the case of
2683 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2684 * operational attributes from the entry, and do a regular ModDN.
2686 typedef struct dninfo {
2690 struct berval nnewSup;
2691 int renamed; /* Was an existing entry renamed? */
2692 int delOldRDN; /* Was old RDN deleted? */
2693 Modifications **modlist; /* the modlist we received */
2694 Modifications *mods; /* the modlist we compared */
2695 int oldNcount; /* #values of old naming attr */
2696 AttributeDescription *oldDesc; /* for renames */
2697 AttributeDescription *newDesc; /* for renames */
2702 /* return 1 if inserted, 0 otherwise */
2706 struct berval *syncUUID )
2714 if ( !si->si_presentlist )
2715 si->si_presentlist = ch_calloc(65536, sizeof( Avlnode * ));
2717 av = (Avlnode **)si->si_presentlist;
2719 val = ch_malloc(UUIDLEN-2);
2720 memcpy(&s, syncUUID->bv_val, 2);
2721 memcpy(val, syncUUID->bv_val+2, UUIDLEN-2);
2723 if ( avl_insert( &av[s], val,
2724 syncuuid_cmp, avl_dup_error ) )
2730 val = ch_malloc(UUIDLEN);
2732 AC_MEMCPY( val, syncUUID->bv_val, UUIDLEN );
2734 if ( avl_insert( &si->si_presentlist, val,
2735 syncuuid_cmp, avl_dup_error ) )
2748 struct berval *val )
2751 Avlnode **a2 = (Avlnode **)av;
2754 memcpy(&s, val->bv_val, 2);
2755 return avl_find( a2[s], val->bv_val+2, syncuuid_cmp );
2757 return avl_find( av, val->bv_val, syncuuid_cmp );
2762 presentlist_free( Avlnode *av )
2765 Avlnode **a2 = (Avlnode **)av;
2769 for (i=0; i<65536; i++) {
2771 count += avl_free( a2[i], ch_free );
2777 return avl_free( av, ch_free );
2784 struct berval *val )
2787 Avlnode **a2 = *(Avlnode ***)av;
2790 memcpy(&s, val->bv_val, 2);
2791 avl_delete( &a2[s], val->bv_val+2, syncuuid_cmp );
2793 avl_delete( av, val->bv_val, syncuuid_cmp );
2802 Modifications** modlist,
2804 struct berval* syncUUID,
2805 struct berval* syncCSN )
2807 Backend *be = op->o_bd;
2808 slap_callback cb = { NULL, NULL, NULL, NULL };
2809 int syncuuid_inserted = 0;
2811 SlapReply rs_search = {REP_RESULT};
2813 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2814 int rc = LDAP_SUCCESS;
2816 struct berval pdn = BER_BVNULL;
2821 Debug( LDAP_DEBUG_SYNC,
2822 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2823 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2825 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2826 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2827 syncuuid_inserted = presentlist_insert( si, syncUUID );
2831 if ( syncstate == LDAP_SYNC_PRESENT ) {
2833 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2834 if ( entry == NULL ) {
2839 if ( syncstate != LDAP_SYNC_DELETE ) {
2840 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2843 /* add if missing */
2844 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2845 &syncUUID[1], syncUUID );
2847 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2848 /* replace only if necessary */
2849 if ( a->a_nvals != a->a_vals ) {
2850 ber_memfree( a->a_nvals[0].bv_val );
2851 ber_dupbv( &a->a_nvals[0], syncUUID );
2853 ber_memfree( a->a_vals[0].bv_val );
2854 ber_dupbv( &a->a_vals[0], &syncUUID[1] );
2858 f.f_choice = LDAP_FILTER_EQUALITY;
2860 ava.aa_desc = slap_schema.si_ad_entryUUID;
2861 ava.aa_value = *syncUUID;
2863 if ( syncuuid_inserted ) {
2864 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2865 si->si_ridtxt, syncUUID[1].bv_val, 0 );
2867 op->ors_filter = &f;
2869 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID[1].bv_len;
2870 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2871 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2872 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2873 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2874 syncUUID[1].bv_val, syncUUID[1].bv_len );
2875 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2876 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2878 op->o_tag = LDAP_REQ_SEARCH;
2879 op->ors_scope = LDAP_SCOPE_SUBTREE;
2880 op->ors_deref = LDAP_DEREF_NEVER;
2882 /* get the entry for this UUID */
2883 #ifdef ENABLE_REWRITE
2884 if ( si->si_rewrite ) {
2885 op->o_req_dn = si->si_suffixm;
2886 op->o_req_ndn = si->si_suffixm;
2890 op->o_req_dn = si->si_base;
2891 op->o_req_ndn = si->si_base;
2894 op->o_time = slap_get_time();
2895 op->ors_tlimit = SLAP_NO_LIMIT;
2897 op->ors_limit = NULL;
2899 op->ors_attrs = slap_anlist_all_attributes;
2900 op->ors_attrsonly = 0;
2902 /* set callback function */
2903 op->o_callback = &cb;
2904 cb.sc_response = dn_callback;
2905 cb.sc_private = &dni;
2906 dni.new_entry = entry;
2907 dni.modlist = modlist;
2909 rc = be->be_search( op, &rs_search );
2910 Debug( LDAP_DEBUG_SYNC,
2911 "syncrepl_entry: %s be_search (%d)\n",
2912 si->si_ridtxt, rc, 0 );
2914 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2915 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2918 cb.sc_response = null_callback;
2921 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2922 Debug( LDAP_DEBUG_SYNC,
2923 "syncrepl_entry: %s %s\n",
2924 si->si_ridtxt, entry->e_name.bv_val, 0 );
2926 Debug( LDAP_DEBUG_SYNC,
2927 "syncrepl_entry: %s %s\n",
2928 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2931 assert( BER_BVISNULL( &op->o_csn ) );
2933 slap_queue_csn( op, syncCSN );
2936 if ( !si->si_refreshDone && si->si_lazyCommit )
2937 op->o_lazyCommit = SLAP_CONTROL_NONCRITICAL;
2939 slap_op_time( &op->o_time, &op->o_tincr );
2940 switch ( syncstate ) {
2942 case LDAP_SYNC_MODIFY:
2943 if ( BER_BVISNULL( &op->o_csn ))
2946 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2948 /* FIXME: op->o_csn is assumed to be
2949 * on the thread's slab; this needs
2950 * to be cleared ASAP.
2952 op->o_csn = a->a_vals[0];
2957 if ( BER_BVISNULL( &dni.dn ) ) {
2958 SlapReply rs_add = {REP_RESULT};
2960 op->o_req_dn = entry->e_name;
2961 op->o_req_ndn = entry->e_nname;
2962 op->o_tag = LDAP_REQ_ADD;
2964 op->o_bd = si->si_wbe;
2966 rc = op->o_bd->be_add( op, &rs_add );
2967 Debug( LDAP_DEBUG_SYNC,
2968 "syncrepl_entry: %s be_add %s (%d)\n",
2969 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2970 switch ( rs_add.sr_err ) {
2972 if ( op->ora_e == entry ) {
2973 be_entry_release_w( op, entry );
2979 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2980 * only if the suffix entry is not present.
2981 * This should not happen during Persist phase.
2983 case LDAP_NO_SUCH_OBJECT:
2984 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
2985 si->si_refreshDone ) {
2986 /* Something's wrong, start over */
2987 ber_bvarray_free( si->si_syncCookie.ctxcsn );
2988 si->si_syncCookie.ctxcsn = NULL;
2989 entry_free( entry );
2990 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
2991 ber_bvarray_free( si->si_cookieState->cs_vals );
2992 ch_free( si->si_cookieState->cs_sids );
2993 si->si_cookieState->cs_vals = NULL;
2994 si->si_cookieState->cs_sids = 0;
2995 si->si_cookieState->cs_num = 0;
2996 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2997 return LDAP_NO_SUCH_OBJECT;
2999 rc = syncrepl_add_glue( op, entry );
3003 /* if an entry was added via syncrepl_add_glue(),
3004 * it likely has no entryUUID, so the previous
3005 * be_search() doesn't find it. In this case,
3006 * give syncrepl a chance to modify it. Also
3007 * allow for entries that were recreated with the
3008 * same DN but a different entryUUID.
3010 case LDAP_ALREADY_EXISTS:
3012 Operation op2 = *op;
3013 SlapReply rs2 = { REP_RESULT };
3014 slap_callback cb2 = { 0 };
3017 op2.o_tag = LDAP_REQ_SEARCH;
3018 op2.o_req_dn = entry->e_name;
3019 op2.o_req_ndn = entry->e_nname;
3020 op2.ors_scope = LDAP_SCOPE_BASE;
3021 op2.ors_deref = LDAP_DEREF_NEVER;
3022 op2.ors_attrs = slap_anlist_all_attributes;
3023 op2.ors_attrsonly = 0;
3024 op2.ors_limit = NULL;
3026 op2.ors_tlimit = SLAP_NO_LIMIT;
3028 f.f_choice = LDAP_FILTER_PRESENT;
3029 f.f_desc = slap_schema.si_ad_objectClass;
3030 op2.ors_filter = &f;
3031 op2.ors_filterstr = generic_filterstr;
3033 op2.o_callback = &cb2;
3034 cb2.sc_response = dn_callback;
3035 cb2.sc_private = &dni;
3037 rc = be->be_search( &op2, &rs2 );
3038 if ( rc ) goto done;
3041 slap_op_time( &op->o_time, &op->o_tincr );
3047 Debug( LDAP_DEBUG_ANY,
3048 "syncrepl_entry: %s be_add %s failed (%d)\n",
3049 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
3057 op->o_req_dn = dni.dn;
3058 op->o_req_ndn = dni.ndn;
3059 if ( dni.renamed ) {
3060 struct berval noldp, newp;
3061 Modifications *mod, **modtail, **ml, *m2;
3062 int i, got_replace = 0, just_rename = 0;
3063 SlapReply rs_modify = {REP_RESULT};
3065 op->o_tag = LDAP_REQ_MODRDN;
3066 dnRdn( &entry->e_name, &op->orr_newrdn );
3067 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
3069 if ( !BER_BVISNULL( &dni.nnewSup )) {
3070 dnParent( &entry->e_name, &newp );
3071 op->orr_newSup = &newp;
3072 op->orr_nnewSup = &dni.nnewSup;
3074 op->orr_newSup = NULL;
3075 op->orr_nnewSup = NULL;
3077 op->orr_deleteoldrdn = dni.delOldRDN;
3078 op->orr_modlist = NULL;
3079 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
3083 /* Drop the RDN-related mods from this op, because their
3084 * equivalents were just setup by slap_modrdn2mods.
3086 * If delOldRDN is TRUE then we should see a delete modop
3087 * for oldDesc. We might see a replace instead.
3088 * delete with no values: therefore newDesc != oldDesc.
3089 * if oldNcount == 1, then Drop this op.
3090 * delete with 1 value: can only be the oldRDN value. Drop op.
3091 * delete with N values: Drop oldRDN value, keep remainder.
3092 * replace with 1 value: if oldNcount == 1 and
3093 * newDesc == oldDesc, Drop this op.
3094 * Any other cases must be left intact.
3096 * We should also see an add modop for newDesc. (But not if
3097 * we got a replace modop due to delOldRDN.) If it has
3098 * multiple values, we'll have to drop the new RDN value.
3100 modtail = &op->orr_modlist;
3101 if ( dni.delOldRDN ) {
3102 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3103 if ( (*ml)->sml_desc == dni.oldDesc ) {
3105 if ( mod->sml_op == LDAP_MOD_REPLACE &&
3106 dni.oldDesc != dni.newDesc ) {
3107 /* This Replace is due to other Mods.
3112 if ( mod->sml_numvals <= 1 &&
3113 dni.oldNcount == 1 &&
3114 ( mod->sml_op == LDAP_MOD_DELETE ||
3115 mod->sml_op == LDAP_MOD_REPLACE )) {
3116 if ( mod->sml_op == LDAP_MOD_REPLACE )
3119 *ml = mod->sml_next;
3120 mod->sml_next = NULL;
3121 slap_mods_free( mod, 1 );
3124 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
3126 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3127 if ( m2->sml_desc == dni.oldDesc &&
3128 m2->sml_op == LDAP_MOD_DELETE ) break;
3130 for ( i=0; i<mod->sml_numvals; i++ ) {
3131 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3133 ch_free( mod->sml_values[i].bv_val );
3134 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3135 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3136 if ( mod->sml_nvalues ) {
3137 ch_free( mod->sml_nvalues[i].bv_val );
3138 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3139 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3144 if ( !mod->sml_numvals ) {
3146 *ml = mod->sml_next;
3147 mod->sml_next = NULL;
3148 slap_mods_free( mod, 1 );
3154 if ( !got_replace ) {
3155 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3156 if ( (*ml)->sml_desc == dni.newDesc ) {
3158 if ( mod->sml_op != LDAP_MOD_ADD )
3160 if ( mod->sml_numvals == 1 ) {
3162 *ml = mod->sml_next;
3163 mod->sml_next = NULL;
3164 slap_mods_free( mod, 1 );
3167 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3168 if ( m2->sml_desc == dni.oldDesc &&
3169 m2->sml_op == SLAP_MOD_SOFTADD ) break;
3171 for ( i=0; i<mod->sml_numvals; i++ ) {
3172 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3174 ch_free( mod->sml_values[i].bv_val );
3175 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3176 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3177 if ( mod->sml_nvalues ) {
3178 ch_free( mod->sml_nvalues[i].bv_val );
3179 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3180 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3190 /* RDNs must be NUL-terminated for back-ldap */
3191 noldp = op->orr_newrdn;
3192 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
3193 noldp = op->orr_nnewrdn;
3194 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
3196 /* Setup opattrs too */
3198 static AttributeDescription *nullattr = NULL;
3199 static AttributeDescription **const opattrs[] = {
3200 &slap_schema.si_ad_entryCSN,
3201 &slap_schema.si_ad_modifiersName,
3202 &slap_schema.si_ad_modifyTimestamp,
3205 AttributeDescription *opattr;
3209 /* pull mod off incoming modlist */
3210 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
3211 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
3213 if ( (*ml)->sml_desc == opattr ) {
3215 *ml = mod->sml_next;
3216 mod->sml_next = NULL;
3218 modtail = &mod->sml_next;
3223 /* If there are still Modifications left, put the opattrs
3224 * back, and let be_modify run. Otherwise, append the opattrs
3225 * to the orr_modlist.
3229 /* don't set a CSN for the rename op */
3231 slap_graduate_commit_csn( op );
3233 mod = op->orr_modlist;
3236 for ( ; mod->sml_next; mod=mod->sml_next );
3239 op->o_bd = si->si_wbe;
3241 rs_reinit( &rs_modify, REP_RESULT );
3242 rc = op->o_bd->be_modrdn( op, &rs_modify );
3244 /* NOTE: noSuchObject should result because the new superior
3245 * has not been added yet (ITS#6472) */
3246 if ( rc == LDAP_NO_SUCH_OBJECT && op->orr_nnewSup != NULL ) {
3247 Operation op2 = *op;
3248 rc = syncrepl_add_glue_ancestors( &op2, entry );
3249 if ( rc == LDAP_SUCCESS ) {
3254 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
3255 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
3257 slap_mods_free( op->orr_modlist, 1 );
3258 Debug( LDAP_DEBUG_SYNC,
3259 "syncrepl_entry: %s be_modrdn %s (%d)\n",
3260 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3262 /* Renamed entries may still have other mods so just fallthru */
3263 op->o_req_dn = entry->e_name;
3264 op->o_req_ndn = entry->e_nname;
3265 /* Use CSN on the modify */
3269 slap_queue_csn( op, syncCSN );
3272 SlapReply rs_modify = {REP_RESULT};
3274 op->o_tag = LDAP_REQ_MODIFY;
3275 op->orm_modlist = dni.mods;
3276 op->orm_no_opattrs = 1;
3277 op->o_bd = si->si_wbe;
3279 rc = op->o_bd->be_modify( op, &rs_modify );
3280 slap_mods_free( op->orm_modlist, 1 );
3281 op->orm_no_opattrs = 0;
3282 Debug( LDAP_DEBUG_SYNC,
3283 "syncrepl_entry: %s be_modify %s (%d)\n",
3284 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3285 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
3286 Debug( LDAP_DEBUG_ANY,
3287 "syncrepl_entry: %s be_modify failed (%d)\n",
3288 si->si_ridtxt, rs_modify.sr_err, 0 );
3292 } else if ( !dni.renamed ) {
3293 Debug( LDAP_DEBUG_SYNC,
3294 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
3295 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
3297 slap_graduate_commit_csn( op );
3302 case LDAP_SYNC_DELETE :
3303 if ( !BER_BVISNULL( &dni.dn ) ) {
3304 SlapReply rs_delete = {REP_RESULT};
3305 op->o_req_dn = dni.dn;
3306 op->o_req_ndn = dni.ndn;
3307 op->o_tag = LDAP_REQ_DELETE;
3308 op->o_bd = si->si_wbe;
3310 slap_queue_csn( op, si->si_syncCookie.ctxcsn );
3312 rc = op->o_bd->be_delete( op, &rs_delete );
3313 Debug( LDAP_DEBUG_SYNC,
3314 "syncrepl_entry: %s be_delete %s (%d)\n",
3315 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3316 if ( rc == LDAP_NO_SUCH_OBJECT )
3319 while ( rs_delete.sr_err == LDAP_SUCCESS
3320 && op->o_delete_glue_parent ) {
3321 op->o_delete_glue_parent = 0;
3322 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3323 slap_callback cb = { NULL };
3324 cb.sc_response = slap_null_cb;
3325 dnParent( &op->o_req_ndn, &pdn );
3327 op->o_req_ndn = pdn;
3328 op->o_callback = &cb;
3329 rs_reinit( &rs_delete, REP_RESULT );
3330 op->o_bd->be_delete( op, &rs_delete );
3341 Debug( LDAP_DEBUG_ANY,
3342 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
3347 slap_sl_free( syncUUID[1].bv_val, op->o_tmpmemctx );
3348 BER_BVZERO( &syncUUID[1] );
3349 if ( !BER_BVISNULL( &dni.ndn ) ) {
3350 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
3352 if ( !BER_BVISNULL( &dni.dn ) ) {
3353 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
3356 entry_free( entry );
3359 slap_graduate_commit_csn( op );
3361 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
3362 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3364 BER_BVZERO( &op->o_csn );
3368 static struct berval gcbva[] = {
3374 #define NP_DELETE_ONE 2
3377 syncrepl_del_nonpresent(
3381 struct sync_cookie *sc,
3384 Backend* be = op->o_bd;
3385 slap_callback cb = { NULL };
3386 struct nonpresent_entry *np_list, *np_prev;
3388 AttributeName an[2];
3390 struct berval pdn = BER_BVNULL;
3393 #ifdef ENABLE_REWRITE
3394 if ( si->si_rewrite ) {
3395 op->o_req_dn = si->si_suffixm;
3396 op->o_req_ndn = si->si_suffixm;
3400 op->o_req_dn = si->si_base;
3401 op->o_req_ndn = si->si_base;
3404 cb.sc_response = nonpresent_callback;
3407 op->o_callback = &cb;
3408 op->o_tag = LDAP_REQ_SEARCH;
3409 op->ors_scope = si->si_scope;
3410 op->ors_deref = LDAP_DEREF_NEVER;
3411 op->o_time = slap_get_time();
3412 op->ors_tlimit = SLAP_NO_LIMIT;
3417 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
3420 op->ors_attrsonly = 1;
3421 op->ors_attrs = slap_anlist_no_attrs;
3422 op->ors_limit = NULL;
3423 op->ors_filter = &uf;
3426 uf.f_av_desc = slap_schema.si_ad_entryUUID;
3428 uf.f_choice = LDAP_FILTER_EQUALITY;
3429 si->si_refreshDelete |= NP_DELETE_ONE;
3431 for (i=0; uuids[i].bv_val; i++) {
3432 SlapReply rs_search = {REP_RESULT};
3435 uf.f_av_value = uuids[i];
3436 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3437 rc = be->be_search( op, &rs_search );
3438 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3440 si->si_refreshDelete ^= NP_DELETE_ONE;
3444 AttributeAssertion mmaa;
3445 SlapReply rs_search = {REP_RESULT};
3447 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
3448 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
3449 an[0].an_desc = slap_schema.si_ad_entryUUID;
3451 op->ors_slimit = SLAP_NO_LIMIT;
3452 op->ors_tlimit = SLAP_NO_LIMIT;
3453 op->ors_limit = NULL;
3454 op->ors_attrsonly = 0;
3455 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
3456 /* In multimaster, updates can continue to arrive while
3457 * we're searching. Limit the search result to entries
3458 * older than our newest cookie CSN.
3460 if ( SLAP_MULTIMASTER( op->o_bd )) {
3465 f->f_choice = LDAP_FILTER_AND;
3466 f->f_next = op->ors_filter;
3470 f->f_choice = LDAP_FILTER_LE;
3472 f->f_av_desc = slap_schema.si_ad_entryCSN;
3474 BER_BVZERO( &f->f_av_value );
3475 for ( i=0; i<sc->numcsns; i++ ) {
3476 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
3477 f->f_av_value = sc->ctxcsn[i];
3479 of = op->ors_filter;
3480 op->ors_filter = mmf;
3481 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3484 op->ors_filterstr = si->si_filterstr;
3486 op->o_nocaching = 1;
3489 rc = be->be_search( op, &rs_search );
3490 if ( SLAP_MULTIMASTER( op->o_bd )) {
3491 op->ors_filter = of;
3493 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
3494 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
3495 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3500 op->o_nocaching = 0;
3502 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
3504 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
3505 csn = sc->ctxcsn[m];
3507 csn = si->si_syncCookie.ctxcsn[0];
3510 op->o_bd = si->si_wbe;
3511 slap_queue_csn( op, &csn );
3513 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
3514 while ( np_list != NULL ) {
3515 SlapReply rs_delete = {REP_RESULT};
3517 LDAP_LIST_REMOVE( np_list, npe_link );
3519 np_list = LDAP_LIST_NEXT( np_list, npe_link );
3520 op->o_tag = LDAP_REQ_DELETE;
3521 op->o_callback = &cb;
3522 cb.sc_response = null_callback;
3524 op->o_req_dn = *np_prev->npe_name;
3525 op->o_req_ndn = *np_prev->npe_nname;
3526 rc = op->o_bd->be_delete( op, &rs_delete );
3527 Debug( LDAP_DEBUG_SYNC,
3528 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
3529 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3531 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
3532 SlapReply rs_modify = {REP_RESULT};
3533 Modifications mod1, mod2;
3534 mod1.sml_op = LDAP_MOD_REPLACE;
3536 mod1.sml_desc = slap_schema.si_ad_objectClass;
3537 mod1.sml_type = mod1.sml_desc->ad_cname;
3538 mod1.sml_numvals = 2;
3539 mod1.sml_values = &gcbva[0];
3540 mod1.sml_nvalues = NULL;
3541 mod1.sml_next = &mod2;
3543 mod2.sml_op = LDAP_MOD_REPLACE;
3545 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
3546 mod2.sml_type = mod2.sml_desc->ad_cname;
3547 mod2.sml_numvals = 1;
3548 mod2.sml_values = &gcbva[1];
3549 mod2.sml_nvalues = NULL;
3550 mod2.sml_next = NULL;
3552 op->o_tag = LDAP_REQ_MODIFY;
3553 op->orm_modlist = &mod1;
3555 rc = op->o_bd->be_modify( op, &rs_modify );
3556 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
3559 while ( rs_delete.sr_err == LDAP_SUCCESS &&
3560 op->o_delete_glue_parent ) {
3561 op->o_delete_glue_parent = 0;
3562 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3563 slap_callback cb = { NULL };
3564 cb.sc_response = slap_null_cb;
3565 dnParent( &op->o_req_ndn, &pdn );
3567 op->o_req_ndn = pdn;
3568 op->o_callback = &cb;
3569 rs_reinit( &rs_delete, REP_RESULT );
3570 /* give it a root privil ? */
3571 op->o_bd->be_delete( op, &rs_delete );
3577 op->o_delete_glue_parent = 0;
3579 ber_bvfree( np_prev->npe_name );
3580 ber_bvfree( np_prev->npe_nname );
3583 if ( slapd_shutdown ) {
3588 slap_graduate_commit_csn( op );
3591 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3592 BER_BVZERO( &op->o_csn );
3599 syncrepl_add_glue_ancestors(
3603 Backend *be = op->o_bd;
3604 slap_callback cb = { NULL };
3606 int rc = LDAP_SUCCESS;
3609 struct berval dn = BER_BVNULL;
3610 struct berval ndn = BER_BVNULL;
3612 struct berval ptr, nptr;
3615 op->o_tag = LDAP_REQ_ADD;
3616 op->o_callback = &cb;
3617 cb.sc_response = null_callback;
3618 cb.sc_private = NULL;
3623 /* count RDNs in suffix */
3624 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3625 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3627 ptr.bv_len -= comma - ptr.bv_val;
3637 /* Start with BE suffix */
3639 for ( i = 0; i < suffrdns; i++ ) {
3640 comma = ber_bvrchr( &ptr, ',' );
3641 if ( comma != NULL ) {
3642 ptr.bv_len = comma - ptr.bv_val;
3649 if ( !BER_BVISEMPTY( &ptr ) ) {
3650 dn.bv_len -= ptr.bv_len + ( suffrdns != 0 );
3651 dn.bv_val += ptr.bv_len + ( suffrdns != 0 );
3654 /* the normalizedDNs are always the same length, no counting
3658 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3659 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3660 ndn.bv_len = be->be_nsuffix[0].bv_len;
3662 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3668 while ( ndn.bv_val > e->e_nname.bv_val ) {
3669 SlapReply rs_add = {REP_RESULT};
3671 glue = entry_alloc();
3672 ber_dupbv( &glue->e_name, &dn );
3673 ber_dupbv( &glue->e_nname, &ndn );
3675 a = attr_alloc( slap_schema.si_ad_objectClass );
3678 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3679 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3680 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3681 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3683 a->a_nvals = a->a_vals;
3685 a->a_next = glue->e_attrs;
3688 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3691 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3692 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3693 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3695 a->a_nvals = a->a_vals;
3697 a->a_next = glue->e_attrs;
3700 op->o_req_dn = glue->e_name;
3701 op->o_req_ndn = glue->e_nname;
3703 rc = be->be_add ( op, &rs_add );
3704 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3705 if ( op->ora_e == glue )
3706 be_entry_release_w( op, glue );
3708 /* incl. ALREADY EXIST */
3710 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3716 /* Move to next child */
3717 comma = ber_bvrchr( &ptr, ',' );
3718 if ( comma == NULL ) {
3721 ptr.bv_len = comma - ptr.bv_val;
3723 dn.bv_val = ++comma;
3724 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3726 comma = ber_bvrchr( &nptr, ',' );
3727 assert( comma != NULL );
3728 nptr.bv_len = comma - nptr.bv_val;
3730 ndn.bv_val = ++comma;
3731 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3742 slap_callback cb = { NULL };
3744 Backend *be = op->o_bd;
3745 SlapReply rs_add = {REP_RESULT};
3747 rc = syncrepl_add_glue_ancestors( op, e );
3750 case LDAP_ALREADY_EXISTS:
3757 op->o_tag = LDAP_REQ_ADD;
3758 op->o_callback = &cb;
3759 cb.sc_response = null_callback;
3760 cb.sc_private = NULL;
3762 op->o_req_dn = e->e_name;
3763 op->o_req_ndn = e->e_nname;
3765 rc = be->be_add ( op, &rs_add );
3766 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3767 if ( op->ora_e == e )
3768 be_entry_release_w( op, e );
3777 syncrepl_updateCookie(
3780 struct sync_cookie *syncCookie )
3782 Backend *be = op->o_bd;
3784 struct berval first = BER_BVNULL;
3785 struct sync_cookie sc;
3787 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3790 int rc, i, j, changed = 0;
3793 slap_callback cb = { NULL };
3794 SlapReply rs_modify = {REP_RESULT};
3796 mod.sml_op = LDAP_MOD_REPLACE;
3797 mod.sml_desc = slap_schema.si_ad_contextCSN;
3798 mod.sml_type = mod.sml_desc->ad_cname;
3799 mod.sml_flags = SLAP_MOD_INTERNAL;
3800 mod.sml_nvalues = NULL;
3801 mod.sml_next = NULL;
3803 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3806 for ( i=0; i<syncCookie->numcsns; i++ ) {
3807 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3809 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3810 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3814 /* clone the cookieState CSNs so we can Replace the whole thing */
3815 sc.numcsns = si->si_cookieState->cs_num;
3817 ber_bvarray_dup_x( &sc.ctxcsn, si->si_cookieState->cs_vals, NULL );
3818 sc.sids = ch_malloc( sc.numcsns * sizeof(int));
3819 for ( i=0; i<sc.numcsns; i++ )
3820 sc.sids[i] = si->si_cookieState->cs_sids[i];
3826 /* find any CSNs in the syncCookie that are newer than the cookieState */
3827 for ( i=0; i<syncCookie->numcsns; i++ ) {
3828 for ( j=0; j<sc.numcsns; j++ ) {
3829 if ( syncCookie->sids[i] < sc.sids[j] )
3831 if ( syncCookie->sids[i] != sc.sids[j] )
3833 len = syncCookie->ctxcsn[i].bv_len;
3834 if ( len > sc.ctxcsn[j].bv_len )
3835 len = sc.ctxcsn[j].bv_len;
3836 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3837 sc.ctxcsn[j].bv_val, len ) > 0 ) {
3838 ber_bvreplace( &sc.ctxcsn[j], &syncCookie->ctxcsn[i] );
3840 if ( BER_BVISNULL( &first ) ||
3841 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3842 first = syncCookie->ctxcsn[i];
3847 /* there was no match for this SID, it's a new CSN */
3848 if ( j == sc.numcsns ||
3849 syncCookie->sids[i] != sc.sids[j] ) {
3850 slap_insert_csn_sids( &sc, j, syncCookie->sids[i],
3851 &syncCookie->ctxcsn[i] );
3852 if ( BER_BVISNULL( &first ) ||
3853 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3854 first = syncCookie->ctxcsn[i];
3859 /* Should never happen, ITS#5065 */
3860 if ( BER_BVISNULL( &first ) || !changed ) {
3861 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3862 ber_bvarray_free( sc.ctxcsn );
3866 op->o_bd = si->si_wbe;
3867 slap_queue_csn( op, &first );
3869 op->o_tag = LDAP_REQ_MODIFY;
3871 cb.sc_response = null_callback;
3874 op->o_callback = &cb;
3875 op->o_req_dn = si->si_contextdn;
3876 op->o_req_ndn = si->si_contextdn;
3878 /* update contextCSN */
3879 op->o_dont_replicate = 1;
3881 mod.sml_numvals = sc.numcsns;
3882 mod.sml_values = sc.ctxcsn;
3884 op->orm_modlist = &mod;
3885 op->orm_no_opattrs = 1;
3886 rc = op->o_bd->be_modify( op, &rs_modify );
3888 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3889 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3891 char txtbuf[SLAP_TEXT_BUFLEN];
3892 size_t textlen = sizeof txtbuf;
3893 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3894 rs_reinit( &rs_modify, REP_RESULT );
3895 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3897 rc = op->o_bd->be_add( op, &rs_modify );
3898 if ( e == op->ora_e )
3899 be_entry_release_w( op, op->ora_e );
3902 op->orm_no_opattrs = 0;
3903 op->o_dont_replicate = 0;
3905 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3906 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3907 ber_bvarray_free( si->si_cookieState->cs_vals );
3908 ch_free( si->si_cookieState->cs_sids );
3909 si->si_cookieState->cs_vals = sc.ctxcsn;
3910 si->si_cookieState->cs_sids = sc.sids;
3911 si->si_cookieState->cs_num = sc.numcsns;
3913 /* Don't just dup the provider's cookie, recreate it */
3914 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
3915 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn, si->si_cookieState->cs_vals, NULL );
3916 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num * sizeof(int) );
3917 for ( i=0; i<si->si_cookieState->cs_num; i++ )
3918 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
3920 si->si_cookieState->cs_age++;
3921 si->si_cookieAge = si->si_cookieState->cs_age;
3923 Debug( LDAP_DEBUG_ANY,
3924 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3925 si->si_ridtxt, rs_modify.sr_err, 0 );
3927 ber_bvarray_free( sc.ctxcsn );
3929 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3932 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3933 BER_BVZERO( &op->o_csn );
3934 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3937 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3938 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3945 /* Compare the attribute from the old entry to the one in the new
3946 * entry. The Modifications from the new entry will either be left
3947 * in place, or changed to an Add or Delete as needed.
3950 attr_cmp( Operation *op, Attribute *old, Attribute *new,
3951 Modifications ***mret, Modifications ***mcur )
3954 Modifications *mod, **modtail;
3960 struct berval **adds, **dels;
3961 /* count old and new */
3962 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
3963 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
3965 /* there MUST be both old and new values */
3970 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
3971 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
3973 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
3974 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
3978 for ( i=0; i<o; i++ ) {
3979 for ( j=0; j<n; j++ ) {
3982 if ( bvmatch( dels[i], adds[j] ) ) {
3992 /* Don't delete/add an objectClass, always use the replace op.
3993 * Modify would fail if provider has replaced entry with a new,
3994 * and the new explicitly includes a superior of a class that was
3995 * only included implicitly in the old entry. Ref ITS#5517.
3997 * Also use replace op if attr has no equality matching rule.
4000 if ( ( nn || ( no > 0 && no < o ) ) &&
4001 ( old->a_desc == slap_schema.si_ad_objectClass ||
4002 !old->a_desc->ad_type->sat_equality ) )
4008 /* all old values were deleted, just use the replace op */
4012 /* delete some values */
4013 mod = ch_malloc( sizeof( Modifications ) );
4014 mod->sml_op = LDAP_MOD_DELETE;
4016 mod->sml_desc = old->a_desc;
4017 mod->sml_type = mod->sml_desc->ad_cname;
4018 mod->sml_numvals = no;
4019 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4020 if ( old->a_vals != old->a_nvals ) {
4021 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4023 mod->sml_nvalues = NULL;
4026 for ( i = 0; i < o; i++ ) {
4027 if ( !dels[i] ) continue;
4028 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
4029 if ( mod->sml_nvalues ) {
4030 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
4034 BER_BVZERO( &mod->sml_values[j] );
4035 if ( mod->sml_nvalues ) {
4036 BER_BVZERO( &mod->sml_nvalues[j] );
4039 modtail = &mod->sml_next;
4042 op->o_tmpfree( dels, op->o_tmpmemctx );
4043 /* some values were added */
4044 if ( nn && no < o ) {
4045 mod = ch_malloc( sizeof( Modifications ) );
4046 mod->sml_op = LDAP_MOD_ADD;
4048 mod->sml_desc = old->a_desc;
4049 mod->sml_type = mod->sml_desc->ad_cname;
4050 mod->sml_numvals = nn;
4051 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4052 if ( old->a_vals != old->a_nvals ) {
4053 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4055 mod->sml_nvalues = NULL;
4058 for ( i = 0; i < n; i++ ) {
4059 if ( !adds[i] ) continue;
4060 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
4061 if ( mod->sml_nvalues ) {
4062 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
4066 BER_BVZERO( &mod->sml_values[j] );
4067 if ( mod->sml_nvalues ) {
4068 BER_BVZERO( &mod->sml_nvalues[j] );
4071 modtail = &mod->sml_next;
4074 op->o_tmpfree( adds, op->o_tmpmemctx );
4076 /* new attr, just use the new mod */
4080 /* advance to next element */
4084 **mcur = mod->sml_next;
4086 modtail = &mod->sml_next;
4088 *mcur = &mod->sml_next;
4094 /* Generate a set of modifications to change the old entry into the
4095 * new one. On input ml is a list of modifications equivalent to
4096 * the new entry. It will be massaged and the result will be stored
4099 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
4100 Modifications **mods, Modifications **ml, int is_ctx)
4102 Modifications **modtail = mods;
4104 /* We assume that attributes are saved in the same order
4105 * in the remote and local databases. So if we walk through
4106 * the attributeDescriptions one by one they should match in
4107 * lock step. If not, look for an add or delete.
4109 while ( old && new )
4111 /* If we've seen this before, use its mod now */
4112 if ( new->a_flags & SLAP_ATTR_IXADD ) {
4113 attr_cmp( op, NULL, new, &modtail, &ml );
4117 /* Skip contextCSN */
4118 if ( is_ctx && old->a_desc ==
4119 slap_schema.si_ad_contextCSN ) {
4124 if ( old->a_desc != new->a_desc ) {
4128 /* If it's just been re-added later,
4129 * remember that we've seen it.
4131 tmp = attr_find( new, old->a_desc );
4133 tmp->a_flags |= SLAP_ATTR_IXADD;
4135 /* If it's a new attribute, pull it in.
4137 tmp = attr_find( old, new->a_desc );
4139 attr_cmp( op, NULL, new, &modtail, &ml );
4143 /* Delete old attr */
4144 mod = ch_malloc( sizeof( Modifications ) );
4145 mod->sml_op = LDAP_MOD_DELETE;
4147 mod->sml_desc = old->a_desc;
4148 mod->sml_type = mod->sml_desc->ad_cname;
4149 mod->sml_numvals = 0;
4150 mod->sml_values = NULL;
4151 mod->sml_nvalues = NULL;
4153 modtail = &mod->sml_next;
4158 /* kludge - always update modifiersName so that it
4159 * stays co-located with the other mod opattrs. But only
4160 * if we know there are other valid mods.
4162 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
4163 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
4164 attr_cmp( op, NULL, new, &modtail, &ml );
4166 attr_cmp( op, old, new, &modtail, &ml );
4179 dninfo *dni = op->o_callback->sc_private;
4181 if ( rs->sr_type == REP_SEARCH ) {
4182 if ( !BER_BVISNULL( &dni->dn ) ) {
4183 Debug( LDAP_DEBUG_ANY,
4184 "dn_callback : consistency error - "
4185 "entryUUID is not unique\n", 0, 0, 0 );
4187 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
4188 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
4189 /* If there is a new entry, see if it differs from the old.
4190 * We compare the non-normalized values so that cosmetic changes
4191 * in the provider are always propagated.
4193 if ( dni->new_entry ) {
4194 Attribute *old, *new;
4195 struct berval old_rdn, new_rdn;
4196 struct berval old_p, new_p;
4197 int is_ctx, new_sup = 0;
4199 /* If old entry is not a glue entry, make sure new entry
4200 * is actually newer than old entry
4202 if ( !is_entry_glue( rs->sr_entry )) {
4203 old = attr_find( rs->sr_entry->e_attrs,
4204 slap_schema.si_ad_entryCSN );
4205 new = attr_find( dni->new_entry->e_attrs,
4206 slap_schema.si_ad_entryCSN );
4209 ber_len_t len = old->a_vals[0].bv_len;
4210 if ( len > new->a_vals[0].bv_len )
4211 len = new->a_vals[0].bv_len;
4212 rc = memcmp( old->a_vals[0].bv_val,
4213 new->a_vals[0].bv_val, len );
4215 Debug( LDAP_DEBUG_SYNC,
4216 "dn_callback : new entry is older than ours "
4217 "%s ours %s, new %s\n",
4218 rs->sr_entry->e_name.bv_val,
4219 old->a_vals[0].bv_val,
4220 new->a_vals[0].bv_val );
4221 return LDAP_SUCCESS;
4222 } else if ( rc == 0 ) {
4223 Debug( LDAP_DEBUG_SYNC,
4224 "dn_callback : entries have identical CSN "
4226 rs->sr_entry->e_name.bv_val,
4227 old->a_vals[0].bv_val, 0 );
4228 return LDAP_SUCCESS;
4233 is_ctx = dn_match( &rs->sr_entry->e_nname,
4234 &op->o_bd->be_nsuffix[0] );
4236 /* Did the DN change?
4237 * case changes in the parent are ignored,
4238 * we only want to know if the RDN was
4241 dnRdn( &rs->sr_entry->e_name, &old_rdn );
4242 dnRdn( &dni->new_entry->e_name, &new_rdn );
4243 dnParent( &rs->sr_entry->e_nname, &old_p );
4244 dnParent( &dni->new_entry->e_nname, &new_p );
4246 new_sup = !dn_match( &old_p, &new_p );
4247 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
4249 struct berval oldRDN, oldVal;
4250 AttributeDescription *ad = NULL;
4256 dni->nnewSup = new_p;
4258 /* See if the oldRDN was deleted */
4259 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
4260 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
4261 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
4263 oldRDN.bv_len -= oldVal.bv_len + 1;
4264 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4266 for ( oldpos=0, a=rs->sr_entry->e_attrs;
4267 a && a->a_desc != ad; oldpos++, a=a->a_next );
4268 /* a should not be NULL but apparently it happens.
4271 dni->oldNcount = a ? a->a_numvals : 0;
4272 for ( newpos=0, a=dni->new_entry->e_attrs;
4273 a && a->a_desc != ad; newpos++, a=a->a_next );
4274 if ( !a || oldpos != newpos || attr_valfind( a,
4275 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
4276 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
4277 SLAP_MR_VALUE_OF_SYNTAX,
4278 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
4282 /* Get the newRDN's desc */
4283 dnRdn( &dni->new_entry->e_nname, &oldRDN );
4284 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
4285 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
4287 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4290 /* A ModDN has happened, but in Refresh mode other
4291 * changes may have occurred before we picked it up.
4292 * So fallthru to regular Modify processing.
4296 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
4297 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
4301 } else if ( rs->sr_type == REP_RESULT ) {
4302 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
4303 Debug( LDAP_DEBUG_ANY,
4304 "dn_callback : consistency error - "
4305 "entryUUID is not unique\n", 0, 0, 0 );
4309 return LDAP_SUCCESS;
4313 nonpresent_callback(
4317 syncinfo_t *si = op->o_callback->sc_private;
4320 char *present_uuid = NULL;
4321 struct nonpresent_entry *np_entry;
4323 if ( rs->sr_type == REP_RESULT ) {
4324 count = presentlist_free( si->si_presentlist );
4325 si->si_presentlist = NULL;
4327 } else if ( rs->sr_type == REP_SEARCH ) {
4328 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
4329 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
4332 present_uuid = presentlist_find( si->si_presentlist, &a->a_nvals[0] );
4335 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
4336 char buf[sizeof("rid=999 non")];
4338 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
4339 present_uuid ? "" : "non" );
4341 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
4342 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
4345 if ( a == NULL ) return 0;
4348 if ( present_uuid == NULL ) {
4349 np_entry = (struct nonpresent_entry *)
4350 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
4351 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
4352 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
4353 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
4356 presentlist_delete( &si->si_presentlist, &a->a_nvals[0] );
4357 ch_free( present_uuid );
4360 return LDAP_SUCCESS;
4368 if ( rs->sr_err != LDAP_SUCCESS &&
4369 rs->sr_err != LDAP_REFERRAL &&
4370 rs->sr_err != LDAP_ALREADY_EXISTS &&
4371 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
4372 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
4374 Debug( LDAP_DEBUG_ANY,
4375 "null_callback : error code 0x%x\n",
4378 return LDAP_SUCCESS;
4381 static struct berval *
4382 slap_uuidstr_from_normalized(
4383 struct berval* uuidstr,
4384 struct berval* normalized,
4389 unsigned char nibble;
4392 if ( normalized == NULL ) return NULL;
4393 if ( normalized->bv_len != 16 ) return NULL;
4398 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4399 if ( new == NULL ) {
4406 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4407 if ( new != uuidstr ) {
4408 slap_sl_free( new, ctx );
4413 for ( i = 0; i < 16; i++ ) {
4414 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
4415 new->bv_val[(i<<1)+d] = '-';
4419 nibble = (normalized->bv_val[i] >> 4) & 0xF;
4420 if ( nibble < 10 ) {
4421 new->bv_val[(i<<1)+d] = nibble + '0';
4423 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
4426 nibble = (normalized->bv_val[i]) & 0xF;
4427 if ( nibble < 10 ) {
4428 new->bv_val[(i<<1)+d+1] = nibble + '0';
4430 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
4434 new->bv_val[new->bv_len] = '\0';
4441 if ( normalized == NULL ) return NULL;
4442 if ( normalized->bv_len != 16 ) return NULL;
4448 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4449 if ( new == NULL ) {
4456 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4461 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
4462 normalized->bv_len, new->bv_val, new->bv_len + 1 );
4466 if ( new != NULL ) {
4467 if ( new->bv_val != NULL ) {
4468 slap_sl_free( new->bv_val, ctx );
4471 if ( new != uuidstr ) {
4472 slap_sl_free( new, ctx );
4485 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
4488 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN-2 ));
4490 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN ));
4495 syncinfo_free( syncinfo_t *sie, int free_all )
4497 syncinfo_t *si_next;
4499 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
4500 sie->si_ridtxt, 0, 0 );
4503 si_next = sie->si_next;
4506 if ( sie->si_conn ) {
4507 connection_client_stop( sie->si_conn );
4508 sie->si_conn = NULL;
4510 ldap_unbind_ext( sie->si_ld, NULL, NULL );
4514 struct re_s *re = sie->si_re;
4517 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4518 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
4519 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
4520 ldap_pvt_runqueue_remove( &slapd_rq, re );
4521 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4524 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
4526 bindconf_free( &sie->si_bindconf );
4528 if ( sie->si_filterstr.bv_val ) {
4529 ch_free( sie->si_filterstr.bv_val );
4531 if ( sie->si_filter ) {
4532 filter_free( sie->si_filter );
4534 if ( sie->si_logfilterstr.bv_val ) {
4535 ch_free( sie->si_logfilterstr.bv_val );
4537 if ( sie->si_base.bv_val ) {
4538 ch_free( sie->si_base.bv_val );
4540 if ( sie->si_logbase.bv_val ) {
4541 ch_free( sie->si_logbase.bv_val );
4543 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
4544 ch_free( sie->si_contextdn.bv_val );
4546 if ( sie->si_attrs ) {
4548 while ( sie->si_attrs[i] != NULL ) {
4549 ch_free( sie->si_attrs[i] );
4552 ch_free( sie->si_attrs );
4554 if ( sie->si_exattrs ) {
4556 while ( sie->si_exattrs[i] != NULL ) {
4557 ch_free( sie->si_exattrs[i] );
4560 ch_free( sie->si_exattrs );
4562 if ( sie->si_anlist ) {
4564 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
4565 ch_free( sie->si_anlist[i].an_name.bv_val );
4568 ch_free( sie->si_anlist );
4570 if ( sie->si_exanlist ) {
4572 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
4573 ch_free( sie->si_exanlist[i].an_name.bv_val );
4576 ch_free( sie->si_exanlist );
4578 if ( sie->si_retryinterval ) {
4579 ch_free( sie->si_retryinterval );
4581 if ( sie->si_retrynum ) {
4582 ch_free( sie->si_retrynum );
4584 if ( sie->si_retrynum_init ) {
4585 ch_free( sie->si_retrynum_init );
4587 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
4588 if ( sie->si_presentlist ) {
4589 presentlist_free( sie->si_presentlist );
4591 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
4592 struct nonpresent_entry* npe;
4593 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
4594 LDAP_LIST_REMOVE( npe, npe_link );
4595 if ( npe->npe_name ) {
4596 if ( npe->npe_name->bv_val ) {
4597 ch_free( npe->npe_name->bv_val );
4599 ch_free( npe->npe_name );
4601 if ( npe->npe_nname ) {
4602 if ( npe->npe_nname->bv_val ) {
4603 ch_free( npe->npe_nname->bv_val );
4605 ch_free( npe->npe_nname );
4609 if ( sie->si_cookieState ) {
4610 sie->si_cookieState->cs_ref--;
4611 if ( !sie->si_cookieState->cs_ref ) {
4612 ch_free( sie->si_cookieState->cs_sids );
4613 ber_bvarray_free( sie->si_cookieState->cs_vals );
4614 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
4615 ch_free( sie->si_cookieState->cs_psids );
4616 ber_bvarray_free( sie->si_cookieState->cs_pvals );
4617 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
4618 ch_free( sie->si_cookieState );
4621 #ifdef ENABLE_REWRITE
4622 if ( sie->si_rewrite )
4623 rewrite_info_delete( &sie->si_rewrite );
4624 if ( sie->si_suffixm.bv_val )
4625 ch_free( sie->si_suffixm.bv_val );
4629 } while ( free_all && si_next );
4632 #ifdef ENABLE_REWRITE
4634 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4636 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4637 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4638 char *argvRule[] = { "rewriteRule", NULL, NULL, ":", NULL };
4642 if ( si->si_rewrite )
4643 rewrite_info_delete( &si->si_rewrite );
4644 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4646 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4647 if ( rc != LDAP_SUCCESS )
4650 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4651 if ( rc != LDAP_SUCCESS )
4654 vnc = ch_malloc( si->si_base.bv_len + 6 );
4655 strcpy( vnc, "(.*)" );
4656 lutil_strcopy( lutil_strcopy( vnc+4, si->si_base.bv_val ), "$" );
4659 rnc = ch_malloc( si->si_suffixm.bv_len + 3 );
4660 strcpy( rnc, "%1" );
4661 strcpy( rnc+2, si->si_suffixm.bv_val );
4664 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4671 /* NOTE: used & documented in slapd.conf(5) */
4673 #define PROVIDERSTR "provider"
4674 #define SCHEMASTR "schemachecking"
4675 #define FILTERSTR "filter"
4676 #define SEARCHBASESTR "searchbase"
4677 #define SCOPESTR "scope"
4678 #define ATTRSONLYSTR "attrsonly"
4679 #define ATTRSSTR "attrs"
4680 #define TYPESTR "type"
4681 #define INTERVALSTR "interval"
4682 #define RETRYSTR "retry"
4683 #define SLIMITSTR "sizelimit"
4684 #define TLIMITSTR "timelimit"
4685 #define SYNCDATASTR "syncdata"
4686 #define LOGBASESTR "logbase"
4687 #define LOGFILTERSTR "logfilter"
4688 #define SUFFIXMSTR "suffixmassage"
4689 #define STRICT_REFRESH "strictrefresh"
4691 /* FIXME: undocumented */
4692 #define EXATTRSSTR "exattrs"
4693 #define MANAGEDSAITSTR "manageDSAit"
4694 #define LAZY_COMMIT "lazycommit"
4698 GOT_RID = 0x00000001U,
4699 GOT_PROVIDER = 0x00000002U,
4700 GOT_SCHEMACHECKING = 0x00000004U,
4701 GOT_FILTER = 0x00000008U,
4702 GOT_SEARCHBASE = 0x00000010U,
4703 GOT_SCOPE = 0x00000020U,
4704 GOT_ATTRSONLY = 0x00000040U,
4705 GOT_ATTRS = 0x00000080U,
4706 GOT_TYPE = 0x00000100U,
4707 GOT_INTERVAL = 0x00000200U,
4708 GOT_RETRY = 0x00000400U,
4709 GOT_SLIMIT = 0x00000800U,
4710 GOT_TLIMIT = 0x00001000U,
4711 GOT_SYNCDATA = 0x00002000U,
4712 GOT_LOGBASE = 0x00004000U,
4713 GOT_LOGFILTER = 0x00008000U,
4714 GOT_EXATTRS = 0x00010000U,
4715 GOT_MANAGEDSAIT = 0x00020000U,
4716 GOT_BINDCONF = 0x00040000U,
4717 GOT_SUFFIXM = 0x00080000U,
4720 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4723 static slap_verbmasks datamodes[] = {
4724 { BER_BVC("default"), SYNCDATA_DEFAULT },
4725 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4726 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4731 parse_syncrepl_retry(
4738 int use_default = 0;
4740 char *val = arg + STRLENOF( RETRYSTR "=" );
4741 if ( strcasecmp( val, "undefined" ) == 0 ) {
4746 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4747 retry_list[0] = NULL;
4749 slap_str2clist( &retry_list, val, " ,\t" );
4751 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4754 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4755 "Error: incomplete syncrepl retry list" );
4756 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4757 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4758 ch_free( retry_list[k] );
4760 ch_free( retry_list );
4763 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4764 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4765 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4766 for ( j = 0; j < n; j++ ) {
4768 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4769 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4770 "Error: invalid retry interval \"%s\" (#%d)",
4771 retry_list[j*2], j );
4772 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4773 /* do some cleanup */
4776 si->si_retryinterval[j] = (time_t)t;
4777 if ( *retry_list[j*2+1] == '+' ) {
4778 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4779 si->si_retrynum[j] = RETRYNUM_FOREVER;
4783 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4784 || si->si_retrynum_init[j] <= 0 )
4786 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4787 "Error: invalid initial retry number \"%s\" (#%d)",
4788 retry_list[j*2+1], j );
4789 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4790 /* do some cleanup */
4793 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4794 || si->si_retrynum[j] <= 0 )
4796 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4797 "Error: invalid retry number \"%s\" (#%d)",
4798 retry_list[j*2+1], j );
4799 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4800 /* do some cleanup */
4805 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4806 Debug( LDAP_DEBUG_CONFIG,
4807 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4811 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4812 si->si_retrynum[j] = RETRYNUM_TAIL;
4813 si->si_retryinterval[j] = 0;
4815 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4816 ch_free( retry_list[k] );
4818 ch_free( retry_list );
4819 if ( !use_default ) {
4820 si->si_got |= GOT_RETRY;
4827 parse_syncrepl_line(
4834 for ( i = 1; i < c->argc; i++ ) {
4835 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4836 STRLENOF( IDSTR "=" ) ) )
4839 /* '\0' string terminator accounts for '=' */
4840 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4841 if ( lutil_atoi( &tmp, val ) != 0 ) {
4842 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4843 "Error: parse_syncrepl_line: "
4844 "unable to parse syncrepl id \"%s\"", val );
4845 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4848 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4849 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4850 "Error: parse_syncrepl_line: "
4851 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4852 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4856 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4857 si->si_got |= GOT_RID;
4858 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4859 STRLENOF( PROVIDERSTR "=" ) ) )
4861 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4862 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4864 if ( ldap_is_ldaps_url( val ))
4865 si->si_bindconf.sb_tls_do_init = 1;
4867 si->si_got |= GOT_PROVIDER;
4868 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4869 STRLENOF( SCHEMASTR "=" ) ) )
4871 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4872 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4873 si->si_schemachecking = 1;
4874 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4875 si->si_schemachecking = 0;
4877 si->si_schemachecking = 1;
4879 si->si_got |= GOT_SCHEMACHECKING;
4880 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4881 STRLENOF( FILTERSTR "=" ) ) )
4883 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4884 if ( si->si_filterstr.bv_val )
4885 ch_free( si->si_filterstr.bv_val );
4886 ber_str2bv( val, 0, 1, &si->si_filterstr );
4887 si->si_got |= GOT_FILTER;
4888 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4889 STRLENOF( LOGFILTERSTR "=" ) ) )
4891 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4892 if ( si->si_logfilterstr.bv_val )
4893 ch_free( si->si_logfilterstr.bv_val );
4894 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4895 si->si_got |= GOT_LOGFILTER;
4896 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4897 STRLENOF( SEARCHBASESTR "=" ) ) )
4902 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4903 if ( si->si_base.bv_val ) {
4904 ch_free( si->si_base.bv_val );
4906 ber_str2bv( val, 0, 0, &bv );
4907 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4908 if ( rc != LDAP_SUCCESS ) {
4909 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4910 "Invalid base DN \"%s\": %d (%s)",
4911 val, rc, ldap_err2string( rc ) );
4912 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4915 si->si_got |= GOT_SEARCHBASE;
4916 #ifdef ENABLE_REWRITE
4917 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
4918 STRLENOF( SUFFIXMSTR "=" ) ) )
4923 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
4924 if ( si->si_suffixm.bv_val ) {
4925 ch_free( si->si_suffixm.bv_val );
4927 ber_str2bv( val, 0, 0, &bv );
4928 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
4929 if ( rc != LDAP_SUCCESS ) {
4930 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4931 "Invalid massage DN \"%s\": %d (%s)",
4932 val, rc, ldap_err2string( rc ) );
4933 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4936 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
4937 ch_free( si->si_suffixm.bv_val );
4938 BER_BVZERO( &si->si_suffixm );
4939 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4940 "Massage DN \"%s\" is not within the database naming context",
4942 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4945 si->si_got |= GOT_SUFFIXM;
4947 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4948 STRLENOF( LOGBASESTR "=" ) ) )
4953 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
4954 if ( si->si_logbase.bv_val ) {
4955 ch_free( si->si_logbase.bv_val );
4957 ber_str2bv( val, 0, 0, &bv );
4958 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
4959 if ( rc != LDAP_SUCCESS ) {
4960 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4961 "Invalid logbase DN \"%s\": %d (%s)",
4962 val, rc, ldap_err2string( rc ) );
4963 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4966 si->si_got |= GOT_LOGBASE;
4967 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
4968 STRLENOF( SCOPESTR "=" ) ) )
4971 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
4972 j = ldap_pvt_str2scope( val );
4974 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4975 "Error: parse_syncrepl_line: "
4976 "unknown scope \"%s\"", val);
4977 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4981 si->si_got |= GOT_SCOPE;
4982 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
4983 STRLENOF( ATTRSONLYSTR ) ) )
4985 si->si_attrsonly = 1;
4986 si->si_got |= GOT_ATTRSONLY;
4987 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
4988 STRLENOF( ATTRSSTR "=" ) ) )
4990 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
4991 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4993 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4994 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
4995 if ( si->si_anlist == NULL ) {
4996 ch_free( attr_fname );
4999 si->si_anfile = attr_fname;
5001 char *str, *s, *next;
5002 const char *delimstr = " ,\t";
5003 str = ch_strdup( val );
5004 for ( s = ldap_pvt_strtok( str, delimstr, &next );
5006 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
5008 if ( strlen(s) == 1 && *s == '*' ) {
5009 si->si_allattrs = 1;
5010 val[ s - str ] = delimstr[0];
5012 if ( strlen(s) == 1 && *s == '+' ) {
5013 si->si_allopattrs = 1;
5014 val [ s - str ] = delimstr[0];
5018 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
5019 if ( si->si_anlist == NULL ) {
5023 si->si_got |= GOT_ATTRS;
5024 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
5025 STRLENOF( EXATTRSSTR "=" ) ) )
5027 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
5028 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
5030 attr_fname = ch_strdup( val + STRLENOF(":include:") );
5031 si->si_exanlist = file2anlist(
5032 si->si_exanlist, attr_fname, " ,\t" );
5033 if ( si->si_exanlist == NULL ) {
5034 ch_free( attr_fname );
5037 ch_free( attr_fname );
5039 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
5040 if ( si->si_exanlist == NULL ) {
5044 si->si_got |= GOT_EXATTRS;
5045 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
5046 STRLENOF( TYPESTR "=" ) ) )
5048 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
5049 if ( !strncasecmp( val, "refreshOnly",
5050 STRLENOF("refreshOnly") ) )
5052 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5053 } else if ( !strncasecmp( val, "refreshAndPersist",
5054 STRLENOF("refreshAndPersist") ) )
5056 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
5057 si->si_interval = 60;
5059 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5060 "Error: parse_syncrepl_line: "
5061 "unknown sync type \"%s\"", val);
5062 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5065 si->si_got |= GOT_TYPE;
5066 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
5067 STRLENOF( INTERVALSTR "=" ) ) )
5069 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
5070 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
5071 si->si_interval = 0;
5072 } else if ( strchr( val, ':' ) != NULL ) {
5073 char *next, *ptr = val;
5076 dd = strtol( ptr, &next, 10 );
5077 if ( next == ptr || next[0] != ':' || dd < 0 ) {
5078 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5079 "Error: parse_syncrepl_line: "
5080 "invalid interval \"%s\", unable to parse days", val );
5081 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5085 hh = strtol( ptr, &next, 10 );
5086 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
5087 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5088 "Error: parse_syncrepl_line: "
5089 "invalid interval \"%s\", unable to parse hours", val );
5090 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5094 mm = strtol( ptr, &next, 10 );
5095 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
5096 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5097 "Error: parse_syncrepl_line: "
5098 "invalid interval \"%s\", unable to parse minutes", val );
5099 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5103 ss = strtol( ptr, &next, 10 );
5104 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
5105 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5106 "Error: parse_syncrepl_line: "
5107 "invalid interval \"%s\", unable to parse seconds", val );
5108 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5111 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
5115 if ( lutil_parse_time( val, &t ) != 0 ) {
5116 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5117 "Error: parse_syncrepl_line: "
5118 "invalid interval \"%s\"", val );
5119 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5122 si->si_interval = (time_t)t;
5124 if ( si->si_interval < 0 ) {
5125 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5126 "Error: parse_syncrepl_line: "
5127 "invalid interval \"%ld\"",
5128 (long) si->si_interval);
5129 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5132 si->si_got |= GOT_INTERVAL;
5133 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
5134 STRLENOF( RETRYSTR "=" ) ) )
5136 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
5139 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
5140 STRLENOF( MANAGEDSAITSTR "=" ) ) )
5142 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
5143 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
5144 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
5146 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5147 "invalid manageDSAit value \"%s\".\n",
5149 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5152 si->si_got |= GOT_MANAGEDSAIT;
5153 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
5154 STRLENOF( SLIMITSTR "=") ) )
5156 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
5157 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5160 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
5161 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5162 "invalid size limit value \"%s\".\n",
5164 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5167 si->si_got |= GOT_SLIMIT;
5168 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
5169 STRLENOF( TLIMITSTR "=" ) ) )
5171 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
5172 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5175 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
5176 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5177 "invalid time limit value \"%s\".\n",
5179 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5182 si->si_got |= GOT_TLIMIT;
5183 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
5184 STRLENOF( SYNCDATASTR "=" ) ) )
5186 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
5187 si->si_syncdata = verb_to_mask( val, datamodes );
5188 si->si_got |= GOT_SYNCDATA;
5189 } else if ( !strncasecmp( c->argv[ i ], STRICT_REFRESH,
5190 STRLENOF( STRICT_REFRESH ) ) )
5192 si->si_strict_refresh = 1;
5193 } else if ( !strncasecmp( c->argv[ i ], LAZY_COMMIT,
5194 STRLENOF( LAZY_COMMIT ) ) )
5196 si->si_lazyCommit = 1;
5197 } else if ( !bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
5198 si->si_got |= GOT_BINDCONF;
5200 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5201 "Error: parse_syncrepl_line: "
5202 "unable to parse \"%s\"\n", c->argv[ i ] );
5203 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5208 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
5209 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5210 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
5211 si->si_got & GOT_RID ? "" : " "IDSTR,
5212 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
5213 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
5214 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5218 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
5219 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5220 "Base DN \"%s\" is not within the database naming context",
5221 si->si_base.bv_val );
5222 ch_free( si->si_base.bv_val );
5223 BER_BVZERO( &si->si_base );
5224 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5228 #ifdef ENABLE_REWRITE
5229 if ( si->si_got & GOT_SUFFIXM ) {
5230 if (config_suffixm( c, si )) {
5231 ch_free( si->si_suffixm.bv_val );
5232 BER_BVZERO( &si->si_suffixm );
5233 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5234 "Error configuring rewrite engine" );
5235 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5241 if ( !( si->si_got & GOT_RETRY ) ) {
5242 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
5243 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
5244 if ( si->si_retryinterval == NULL ) {
5245 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
5251 si->si_filter = str2filter( si->si_filterstr.bv_val );
5252 if ( si->si_filter == NULL ) {
5253 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
5254 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
5268 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
5269 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
5270 "operations required for syncrepl", c->be->be_type );
5271 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5274 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
5275 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
5276 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5279 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
5282 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
5286 si->si_bindconf.sb_tls = SB_TLS_OFF;
5287 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
5288 si->si_schemachecking = 0;
5289 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
5290 &si->si_filterstr );
5291 si->si_base.bv_val = NULL;
5292 si->si_scope = LDAP_SCOPE_SUBTREE;
5293 si->si_attrsonly = 0;
5294 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5295 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5296 si->si_attrs = NULL;
5297 si->si_allattrs = 0;
5298 si->si_allopattrs = 0;
5299 si->si_exattrs = NULL;
5300 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5301 si->si_interval = 86400;
5302 si->si_retryinterval = NULL;
5303 si->si_retrynum_init = NULL;
5304 si->si_retrynum = NULL;
5305 si->si_manageDSAit = 0;
5309 si->si_presentlist = NULL;
5310 LDAP_LIST_INIT( &si->si_nonpresentlist );
5311 ldap_pvt_thread_mutex_init( &si->si_mutex );
5313 rc = parse_syncrepl_line( c, si );
5318 /* Must be LDAPv3 because we need controls */
5319 switch ( si->si_bindconf.sb_version ) {
5321 /* not explicitly set */
5322 si->si_bindconf.sb_version = LDAP_VERSION3;
5325 /* explicitly set */
5328 Debug( LDAP_DEBUG_ANY,
5329 "version %d incompatible with syncrepl\n",
5330 si->si_bindconf.sb_version, 0, 0 );
5331 syncinfo_free( si, 0 );
5335 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
5336 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5337 "<%s> invalid URL", c->argv[0] );
5338 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
5339 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
5344 if ( slapMode & SLAP_SERVER_MODE ) {
5346 /* check if consumer points to current server and database.
5347 * If so, ignore this configuration.
5349 if ( !SLAP_DBHIDDEN( c->be ) ) {
5351 /* if searchbase doesn't match current DB suffix,
5352 * assume it's different
5354 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
5355 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
5360 /* if searchbase matches, see if URLs match */
5361 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
5367 init_syncrepl( si );
5368 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5369 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
5370 si->si_interval, do_syncrepl, si, "do_syncrepl",
5372 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5374 rc = config_sync_shadow( c ) ? -1 : 0;
5379 /* mirrormode still needs to see this flag in tool mode */
5380 rc = config_sync_shadow( c ) ? -1 : 0;
5382 ldap_free_urldesc( lud );
5386 /* Use main slapd defaults */
5387 bindconf_tls_defaults( &si->si_bindconf );
5390 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
5391 syncinfo_free( si, 0 );
5394 Debug( LDAP_DEBUG_CONFIG,
5395 "Config: ** successfully added syncrepl %s \"%s\"\n",
5397 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
5398 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
5399 if ( c->be->be_syncinfo ) {
5402 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
5404 /* add new syncrepl to end of list (same order as when deleting) */
5405 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
5408 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
5409 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
5410 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
5412 c->be->be_syncinfo = si;
5414 si->si_cookieState->cs_ref++;
5423 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
5425 struct berval bc, uri, bs;
5426 char buf[BUFSIZ*2], *ptr;
5429 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
5433 /* temporarily inhibit bindconf from printing URI */
5434 uri = si->si_bindconf.sb_uri;
5435 BER_BVZERO( &si->si_bindconf.sb_uri );
5436 si->si_bindconf.sb_version = 0;
5437 bindconf_unparse( &si->si_bindconf, &bc );
5438 si->si_bindconf.sb_uri = uri;
5439 si->si_bindconf.sb_version = LDAP_VERSION3;
5442 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
5443 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
5444 si->si_rid, si->si_bindconf.sb_uri.bv_val );
5445 if ( len >= sizeof( buf ) ) return;
5447 if ( !BER_BVISNULL( &bc ) ) {
5448 if ( WHATSLEFT <= bc.bv_len ) {
5452 ptr = lutil_strcopy( ptr, bc.bv_val );
5455 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
5456 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
5457 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
5458 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
5461 if ( !BER_BVISNULL( &si->si_base ) ) {
5462 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
5463 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
5464 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
5467 #ifdef ENABLE_REWRITE
5468 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
5469 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
5470 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
5471 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
5475 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
5476 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
5477 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
5478 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
5481 if ( !BER_BVISNULL( &si->si_logbase ) ) {
5482 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
5483 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
5484 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
5487 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
5488 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
5489 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
5490 ptr = lutil_strcopy( ptr, bs.bv_val );
5492 if ( si->si_attrsonly ) {
5493 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5494 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
5496 if ( si->si_anfile ) {
5497 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
5498 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
5499 ptr = lutil_strcopy( ptr, si->si_anfile );
5501 } else if ( si->si_allattrs || si->si_allopattrs ||
5502 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
5506 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5507 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
5509 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
5510 if ( ptr == NULL ) return;
5511 if ( si->si_allattrs ) {
5512 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
5513 if ( old != ptr ) *ptr++ = ',';
5516 if ( si->si_allopattrs ) {
5517 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
5518 if ( old != ptr ) *ptr++ = ',';
5523 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
5524 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
5525 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
5526 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
5527 if ( ptr == NULL ) return;
5529 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
5530 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
5531 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
5533 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
5534 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
5535 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
5536 "refreshAndPersist" : "refreshOnly" );
5538 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
5541 dd = si->si_interval;
5548 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
5549 INTERVALSTR, dd, hh, mm, ss );
5550 if ( len >= WHATSLEFT ) return;
5554 if ( si->si_got & GOT_RETRY ) {
5555 const char *space = "";
5556 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
5557 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
5558 for (i=0; si->si_retryinterval[i]; i++) {
5559 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
5560 (long) si->si_retryinterval[i] );
5562 if ( WHATSLEFT - 1 <= len ) return;
5564 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
5567 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
5568 if ( WHATSLEFT <= len ) return;
5572 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
5575 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
5578 if ( si->si_slimit ) {
5579 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
5580 if ( WHATSLEFT <= len ) return;
5584 if ( si->si_tlimit ) {
5585 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
5586 if ( WHATSLEFT <= len ) return;
5590 if ( si->si_syncdata ) {
5591 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
5592 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
5593 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
5594 ptr = lutil_strcopy( ptr, bc.bv_val );
5598 if ( si->si_lazyCommit ) {
5599 ptr = lutil_strcopy( ptr, " " LAZY_COMMIT );
5602 bc.bv_len = ptr - buf;
5604 ber_dupbv( bv, &bc );
5608 syncrepl_config( ConfigArgs *c )
5610 if (c->op == SLAP_CONFIG_EMIT) {
5611 if ( c->be->be_syncinfo ) {
5615 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
5616 syncrepl_unparse( si, &bv );
5617 ber_bvarray_add( &c->rvalue_vals, &bv );
5622 } else if ( c->op == LDAP_MOD_DELETE ) {
5624 if ( c->be->be_syncinfo ) {
5625 syncinfo_t *si, **sip;
5628 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
5630 if ( c->valx == -1 || i == c->valx ) {
5634 /* If the task is currently active, we have to leave
5635 * it running. It will exit on its own. This will only
5636 * happen when running on the cn=config DB.
5639 if ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
5642 /* There is no active thread, but we must still
5643 * ensure that no thread is (or will be) queued
5644 * while we removes the task.
5646 struct re_s *re = si->si_re;
5649 if ( si->si_conn ) {
5650 connection_client_stop( si->si_conn );
5654 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5655 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
5656 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5659 if ( ldap_pvt_thread_pool_retract( &connection_pool,
5660 re->routine, re ) > 0 )
5663 ldap_pvt_runqueue_remove( &slapd_rq, re );
5664 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5666 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5670 syncinfo_free( si, 0 );
5679 if ( !c->be->be_syncinfo ) {
5680 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5684 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5685 Debug(LDAP_DEBUG_ANY, "%s: "
5686 "syncrepl: database already shadowed.\n",
5690 return add_syncrepl( c );