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-2016 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;
114 time_t si_refreshBeg;
115 time_t si_refreshEnd;
116 OpExtra *si_refreshTxn;
121 int si_strict_refresh; /* stop listening during fallback refresh */
124 Avlnode *si_presentlist;
127 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
128 #ifdef ENABLE_REWRITE
129 struct rewrite_info *si_rewrite;
130 struct berval si_suffixm;
132 ldap_pvt_thread_mutex_t si_mutex;
135 static int syncuuid_cmp( const void *, const void * );
136 static int presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
137 static void presentlist_delete( Avlnode **av, struct berval *syncUUID );
138 static char *presentlist_find( Avlnode *av, struct berval *syncUUID );
139 static int presentlist_free( Avlnode *av );
140 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
141 static int syncrepl_message_to_op(
142 syncinfo_t *, Operation *, LDAPMessage * );
143 static int syncrepl_message_to_entry(
144 syncinfo_t *, Operation *, LDAPMessage *,
145 Modifications **, Entry **, int, struct berval* );
146 static int syncrepl_entry(
147 syncinfo_t *, Operation*, Entry*,
148 Modifications**,int, struct berval*,
149 struct berval *cookieCSN );
150 static int syncrepl_updateCookie(
151 syncinfo_t *, Operation *,
152 struct sync_cookie * );
153 static struct berval * slap_uuidstr_from_normalized(
154 struct berval *, struct berval *, void * );
155 static int syncrepl_add_glue_ancestors(
156 Operation* op, Entry *e );
158 /* delta-mmr overlay handler */
159 static int syncrepl_op_modify( Operation *op, SlapReply *rs );
161 /* callback functions */
162 static int dn_callback( Operation *, SlapReply * );
163 static int nonpresent_callback( Operation *, SlapReply * );
164 static int null_callback( Operation *, SlapReply * );
166 static AttributeDescription *sync_descs[4];
169 static AttributeDescription *ad_reqMod, *ad_reqDN;
171 typedef struct logschema {
173 struct berval ls_req;
174 struct berval ls_mod;
175 struct berval ls_newRdn;
176 struct berval ls_delRdn;
177 struct berval ls_newSup;
180 static logschema changelog_sc = {
182 BER_BVC("changeType"),
185 BER_BVC("deleteOldRDN"),
186 BER_BVC("newSuperior")
189 static logschema accesslog_sc = {
193 BER_BVC("reqNewRDN"),
194 BER_BVC("reqDeleteOldRDN"),
195 BER_BVC("reqNewSuperior")
199 syncrepl_state2str( int state )
202 case LDAP_SYNC_PRESENT:
208 case LDAP_SYNC_MODIFY:
211 case LDAP_SYNC_DELETE:
218 static slap_overinst syncrepl_ov;
221 init_syncrepl(syncinfo_t *si)
224 char **attrs, **exattrs;
226 if ( !syncrepl_ov.on_bi.bi_type ) {
227 syncrepl_ov.on_bi.bi_type = "syncrepl";
228 syncrepl_ov.on_bi.bi_op_modify = syncrepl_op_modify;
229 overlay_register( &syncrepl_ov );
232 /* delta-MMR needs the overlay, nothing else does.
233 * This must happen before accesslog overlay is configured.
235 if ( si->si_syncdata &&
236 !overlay_is_inst( si->si_be, syncrepl_ov.on_bi.bi_type )) {
237 overlay_config( si->si_be, syncrepl_ov.on_bi.bi_type, -1, NULL, NULL );
240 logschema *ls = &accesslog_sc;
242 slap_bv2ad( &ls->ls_mod, &ad_reqMod, &text );
243 slap_bv2ad( &ls->ls_dn, &ad_reqDN, &text );
247 if ( !sync_descs[0] ) {
248 sync_descs[0] = slap_schema.si_ad_objectClass;
249 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
250 sync_descs[2] = slap_schema.si_ad_entryCSN;
251 sync_descs[3] = NULL;
254 if ( si->si_allattrs && si->si_allopattrs )
257 attrs = anlist2attrs( si->si_anlist );
260 if ( si->si_allattrs ) {
263 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
264 for ( j = i; attrs[j] != NULL; j++ ) {
267 attrs[j] = attrs[j+1];
273 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
274 attrs[i] = ch_strdup("*");
277 } else if ( si->si_allopattrs ) {
280 if ( is_at_operational( at_find( attrs[i] ) ) ) {
281 for ( j = i; attrs[j] != NULL; j++ ) {
284 attrs[j] = attrs[j+1];
290 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
291 attrs[i] = ch_strdup("+");
295 for ( i = 0; sync_descs[i] != NULL; i++ ) {
298 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
299 for ( k = j; attrs[k] != NULL; k++ ) {
302 attrs[k] = attrs[k+1];
310 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
312 if ( si->si_allopattrs ) {
313 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
315 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
319 if ( si->si_allopattrs ) {
320 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
322 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
323 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
331 if ( si->si_allattrs == si->si_allopattrs ) {
332 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
333 attrs[i++] = ch_strdup( "*" );
334 attrs[i++] = ch_strdup( "+" );
335 } else if ( si->si_allattrs && !si->si_allopattrs ) {
336 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
337 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
338 attrs[i++] = ch_strdup( "*" );
339 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
340 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
342 } else if ( !si->si_allattrs && si->si_allopattrs ) {
343 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
344 attrs[i++] = ch_strdup( "+" );
345 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
350 si->si_attrs = attrs;
352 exattrs = anlist2attrs( si->si_exanlist );
355 for ( n = 0; exattrs[n] != NULL; n++ ) ;
357 for ( i = 0; sync_descs[i] != NULL; i++ ) {
359 while ( exattrs[j] != NULL ) {
360 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
361 ch_free( exattrs[j] );
362 for ( k = j; exattrs[k] != NULL; k++ ) {
363 exattrs[k] = exattrs[k+1];
371 for ( i = 0; exattrs[i] != NULL; i++ ) {
372 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
374 if ( ( oc = si->si_anlist[j].an_oc ) ) {
376 while ( oc->soc_required[k] ) {
377 if ( !strcmp( exattrs[i],
378 oc->soc_required[k]->sat_cname.bv_val ) ) {
379 ch_free( exattrs[i] );
380 for ( l = i; exattrs[l]; l++ ) {
381 exattrs[l] = exattrs[l+1];
391 for ( i = 0; exattrs[i] != NULL; i++ ) ;
394 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
397 si->si_exattrs = exattrs;
405 BerElementBuffer berbuf;
406 BerElement *ber = (BerElement *)&berbuf;
407 LDAPControl c[3], *ctrls[4];
411 char **attrs, *lattrs[8];
416 /* setup LDAP SYNC control */
417 ber_init2( ber, NULL, LBER_USE_DER );
418 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
420 /* If we're using a log but we have no state, then fallback to
421 * normal mode for a full refresh.
423 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
424 si->si_logstate = SYNCLOG_FALLBACK;
427 /* Use the log parameters if we're in log mode */
428 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
430 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
434 lattrs[0] = ls->ls_dn.bv_val;
435 lattrs[1] = ls->ls_req.bv_val;
436 lattrs[2] = ls->ls_mod.bv_val;
437 lattrs[3] = ls->ls_newRdn.bv_val;
438 lattrs[4] = ls->ls_delRdn.bv_val;
439 lattrs[5] = ls->ls_newSup.bv_val;
440 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
444 base = si->si_logbase.bv_val;
445 filter = si->si_logfilterstr.bv_val;
448 scope = LDAP_SCOPE_SUBTREE;
451 base = si->si_base.bv_val;
452 filter = si->si_filterstr.bv_val;
453 attrs = si->si_attrs;
454 attrsonly = si->si_attrsonly;
455 scope = si->si_scope;
457 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
458 si->si_type = LDAP_SYNC_REFRESH_ONLY;
460 si->si_type = si->si_ctype;
463 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
465 ber_printf( ber, "{eOb}",
466 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
468 ber_printf( ber, "{eb}",
469 abs(si->si_type), rhint );
472 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == -1 ) {
477 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
478 c[0].ldctl_iscritical = si->si_type < 0;
481 c[1].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
482 BER_BVZERO( &c[1].ldctl_value );
483 c[1].ldctl_iscritical = 1;
486 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
487 c[2].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
488 c[2].ldctl_value = si->si_bindconf.sb_authzId;
489 c[2].ldctl_iscritical = 1;
496 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
497 ctrls, NULL, NULL, si->si_slimit, &si->si_msgid );
510 SlapReply rs = {REP_SEARCH};
511 int i, j, changed = 0;
513 /* Look for contextCSN from syncprov overlay. If
514 * there's no overlay, this will be a no-op. That means
515 * this is a pure consumer, so local changes will not be
516 * allowed, and all changes will already be reflected in
519 a.a_desc = slap_schema.si_ad_contextCSN;
521 e.e_name = si->si_contextdn;
522 e.e_nname = si->si_contextdn;
523 at[0].an_name = a.a_desc->ad_cname;
524 at[0].an_desc = a.a_desc;
525 BER_BVZERO( &at[1].an_name );
527 rs.sr_flags = REP_ENTRY_MODIFIABLE;
529 op->o_req_dn = e.e_name;
530 op->o_req_ndn = e.e_nname;
532 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
533 i = backend_operational( op, &rs );
534 if ( i == LDAP_SUCCESS && a.a_nvals ) {
535 int num = a.a_numvals;
536 /* check for differences */
537 if ( num != si->si_cookieState->cs_num ) {
540 for ( i=0; i<num; i++ ) {
541 if ( ber_bvcmp( &a.a_nvals[i],
542 &si->si_cookieState->cs_vals[i] )) {
549 ber_bvarray_free( si->si_cookieState->cs_vals );
550 ch_free( si->si_cookieState->cs_sids );
551 si->si_cookieState->cs_num = num;
552 si->si_cookieState->cs_vals = a.a_nvals;
553 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_nvals,
555 si->si_cookieState->cs_age++;
557 ber_bvarray_free( a.a_nvals );
559 ber_bvarray_free( a.a_vals );
561 /* See if the cookieState has changed due to anything outside
562 * this particular consumer. That includes other consumers in
563 * the same context, or local changes detected above.
565 if ( si->si_cookieState->cs_num > 0 && si->si_cookieAge !=
566 si->si_cookieState->cs_age ) {
567 if ( !si->si_syncCookie.numcsns ) {
568 ber_bvarray_free( si->si_syncCookie.ctxcsn );
569 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
570 si->si_cookieState->cs_vals, NULL );
573 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
574 /* bogus, just dup everything */
575 if ( si->si_syncCookie.sids[i] == -1 ) {
576 ber_bvarray_free( si->si_syncCookie.ctxcsn );
577 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
578 si->si_cookieState->cs_vals, NULL );
582 for (j=0; j<si->si_cookieState->cs_num; j++) {
583 if ( si->si_syncCookie.sids[i] !=
584 si->si_cookieState->cs_sids[j] )
586 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
587 &si->si_cookieState->cs_vals[j] ))
589 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
590 &si->si_cookieState->cs_vals[j] );
598 si->si_cookieAge = si->si_cookieState->cs_age;
599 ch_free( si->si_syncCookie.octet_str.bv_val );
600 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
601 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
602 si->si_syncCookie.sid );
603 ch_free( si->si_syncCookie.sids );
604 slap_reparse_sync_cookie( &si->si_syncCookie, op->o_tmpmemctx );
606 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
616 int cmdline_cookie_found = 0;
618 struct sync_cookie *sc = NULL;
623 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
624 if ( rc != LDAP_SUCCESS ) {
627 op->o_protocol = LDAP_VERSION3;
629 /* Set SSF to strongest of TLS, SASL SSFs */
632 op->o_transport_ssf = 0;
634 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
635 == LDAP_SUCCESS && ssl != NULL )
637 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
639 #endif /* HAVE_TLS */
641 ber_len_t ssf; /* ITS#5403, 3864 LDAP_OPT_X_SASL_SSF probably ought
642 to use sasl_ssf_t but currently uses ber_len_t */
643 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &ssf )
645 op->o_sasl_ssf = ssf;
647 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
648 ? op->o_sasl_ssf : op->o_tls_ssf;
650 ldap_set_option( si->si_ld, LDAP_OPT_TIMELIMIT, &si->si_tlimit );
652 rc = LDAP_DEREF_NEVER; /* actually could allow DEREF_FINDING */
653 ldap_set_option( si->si_ld, LDAP_OPT_DEREF, &rc );
655 ldap_set_option( si->si_ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
657 si->si_syncCookie.rid = si->si_rid;
659 /* whenever there are multiple data sources possible, advertise sid */
660 si->si_syncCookie.sid = ( SLAP_MULTIMASTER( si->si_be ) || si->si_be != si->si_wbe ) ?
663 /* We've just started up, or the remote server hasn't sent us
664 * any meaningful state.
666 if ( !si->si_syncCookie.ctxcsn ) {
669 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
670 if ( si->si_rid == sc->rid ) {
671 cmdline_cookie_found = 1;
676 if ( cmdline_cookie_found ) {
677 /* cookie is supplied in the command line */
679 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
681 slap_sync_cookie_free( &si->si_syncCookie, 0 );
682 si->si_syncCookie.octet_str = sc->octet_str;
684 /* ctxcsn wasn't parsed yet, do it now */
685 slap_parse_sync_cookie( &si->si_syncCookie, NULL );
687 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
688 if ( !si->si_cookieState->cs_num ) {
689 /* get contextCSN shadow replica from database */
690 BerVarray csn = NULL;
691 void *ctx = op->o_tmpmemctx;
693 op->o_req_ndn = si->si_contextdn;
694 op->o_req_dn = op->o_req_ndn;
696 /* try to read stored contextCSN */
697 op->o_tmpmemctx = NULL;
698 backend_attribute( op, NULL, &op->o_req_ndn,
699 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
700 op->o_tmpmemctx = ctx;
702 si->si_cookieState->cs_vals = csn;
703 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
704 si->si_cookieState->cs_num = i;
705 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
706 slap_sort_csn_sids( csn, si->si_cookieState->cs_sids, i, NULL );
709 if ( si->si_cookieState->cs_num ) {
710 ber_bvarray_free( si->si_syncCookie.ctxcsn );
711 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
712 si->si_cookieState->cs_vals, NULL )) {
714 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
717 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
718 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
720 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
721 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
723 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
726 ch_free( si->si_syncCookie.octet_str.bv_val );
727 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
728 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
729 si->si_syncCookie.sid );
731 /* ITS#6367: recreate the cookie so it has our SID, not our peer's */
732 ch_free( si->si_syncCookie.octet_str.bv_val );
733 BER_BVZERO( &si->si_syncCookie.octet_str );
734 /* Look for contextCSN from syncprov overlay. */
735 check_syncprov( op, si );
736 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
737 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
738 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
739 si->si_syncCookie.sid );
742 si->si_refreshDone = 0;
743 si->si_refreshBeg = slap_get_time();
744 si->si_refreshCount = 0;
745 si->si_refreshTxn = NULL;
746 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s starting refresh\n",
747 si->si_ridtxt, 0, 0 );
749 rc = ldap_sync_search( si, op->o_tmpmemctx );
751 if( rc != LDAP_SUCCESS ) {
752 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
753 "ldap_search_ext: %s (%d)\n",
754 si->si_ridtxt, ldap_err2string( rc ), rc );
760 ldap_unbind_ext( si->si_ld, NULL, NULL );
769 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
776 if ( sc1->numcsns < sc2->numcsns ) {
777 *which = sc1->numcsns;
781 for (j=0; j<sc2->numcsns; j++) {
782 for (i=0; i<sc1->numcsns; i++) {
783 if ( sc1->sids[i] != sc2->sids[j] )
785 value_match( &match, slap_schema.si_ad_entryCSN,
786 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
787 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
788 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
795 if ( i == sc1->numcsns ) {
796 /* sc2 has a sid sc1 lacks */
804 #define SYNC_PAUSED -3
811 BerElementBuffer berbuf;
812 BerElement *ber = (BerElement *)&berbuf;
814 LDAPMessage *msg = NULL;
816 struct sync_cookie syncCookie = { NULL };
817 struct sync_cookie syncCookie_req = { NULL };
822 Modifications *modlist = NULL;
826 struct timeval *tout_p = NULL;
827 struct timeval tout = { 0, 0 };
829 int refreshDeletes = 0;
830 char empty[6] = "empty";
832 if ( slapd_shutdown ) {
837 ber_init2( ber, NULL, LBER_USE_DER );
838 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
840 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
842 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
844 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST && si->si_refreshDone ) {
850 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
851 tout_p, &msg ) ) > 0 )
853 int match, punlock, syncstate;
854 struct berval *retdata, syncUUID[2], cookie = BER_BVNULL;
856 LDAPControl **rctrls = NULL, *rctrlp = NULL;
863 if ( slapd_shutdown ) {
867 switch( ldap_msgtype( msg ) ) {
868 case LDAP_RES_SEARCH_ENTRY:
869 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
870 ldap_get_dn_ber( si->si_ld, msg, NULL, &bdn );
873 bdn.bv_len = sizeof(empty)-1;
875 /* we can't work without the control */
877 LDAPControl **next = NULL;
878 /* NOTE: make sure we use the right one;
879 * a better approach would be to run thru
880 * the whole list and take care of all */
881 /* NOTE: since we issue the search request,
882 * we should know what controls to expect,
883 * and there should be none apart from the
884 * sync-related control */
885 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
886 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
888 bdn.bv_val[bdn.bv_len] = '\0';
889 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
890 "got search entry with multiple "
891 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
892 ldap_controls_free( rctrls );
897 if ( rctrlp == NULL ) {
898 bdn.bv_val[bdn.bv_len] = '\0';
899 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
900 "got search entry without "
901 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
905 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
906 if ( ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID[0] )
908 bdn.bv_val[bdn.bv_len] = '\0';
909 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s malformed message (%s)\n",
910 si->si_ridtxt, bdn.bv_val, 0 );
911 ldap_controls_free( rctrls );
915 /* FIXME: what if syncUUID is NULL or empty?
916 * (happens with back-sql...) */
917 if ( syncUUID[0].bv_len != UUIDLEN ) {
918 bdn.bv_val[bdn.bv_len] = '\0';
919 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
920 "got empty or invalid syncUUID with LDAP_SYNC_%s (%s)\n",
922 syncrepl_state2str( syncstate ), bdn.bv_val );
923 ldap_controls_free( rctrls );
928 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
929 if ( ber_scanf( ber, /*"{"*/ "m}", &cookie ) != LBER_ERROR ) {
931 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
933 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
935 if ( !BER_BVISNULL( &cookie ) ) {
936 ch_free( syncCookie.octet_str.bv_val );
937 ber_dupbv( &syncCookie.octet_str, &cookie );
939 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
941 slap_parse_sync_cookie( &syncCookie, NULL );
942 if ( syncCookie.ctxcsn ) {
943 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
944 check_syncprov( op, si );
945 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
946 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
948 if ( sid < si->si_cookieState->cs_sids[i] )
950 if ( si->si_cookieState->cs_sids[i] == sid ) {
951 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
952 bdn.bv_val[bdn.bv_len] = '\0';
953 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s (%s)\n",
954 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
956 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
957 ldap_controls_free( rctrls );
965 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
967 /* check pending CSNs too */
968 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
969 if ( slapd_shutdown ) {
973 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
974 ldap_pvt_thread_yield();
977 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
978 if ( sid < si->si_cookieState->cs_psids[i] )
980 if ( si->si_cookieState->cs_psids[i] == sid ) {
981 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
982 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
983 bdn.bv_val[bdn.bv_len] = '\0';
984 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s (%s)\n",
985 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
986 ldap_controls_free( rctrls );
990 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
995 /* new SID, add it */
996 if ( i == si->si_cookieState->cs_pnum ||
997 sid != si->si_cookieState->cs_psids[i] ) {
998 slap_insert_csn_sids(
999 (struct sync_cookie *)&si->si_cookieState->cs_pvals,
1000 i, sid, syncCookie.ctxcsn );
1002 assert( punlock < 0 );
1004 } else if (si->si_too_old) {
1005 bdn.bv_val[bdn.bv_len] = '\0';
1006 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring (%s)\n",
1007 si->si_ridtxt, bdn.bv_val, 0 );
1008 ldap_controls_free( rctrls );
1012 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1017 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
1019 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
1022 rc = syncrepl_updateCookie( si, op, &syncCookie );
1023 } else switch ( rc ) {
1024 case LDAP_ALREADY_EXISTS:
1025 case LDAP_NO_SUCH_OBJECT:
1026 case LDAP_NO_SUCH_ATTRIBUTE:
1027 case LDAP_TYPE_OR_VALUE_EXISTS:
1028 case LDAP_NOT_ALLOWED_ON_NONLEAF:
1029 rc = LDAP_SYNC_REFRESH_REQUIRED;
1030 si->si_logstate = SYNCLOG_FALLBACK;
1031 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
1032 bdn.bv_val[bdn.bv_len] = '\0';
1033 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync on (%s), switching to REFRESH\n",
1034 si->si_ridtxt, bdn.bv_val, 0 );
1035 if (si->si_strict_refresh) {
1036 slap_suspend_listeners();
1043 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
1044 &modlist, &entry, syncstate, syncUUID ) ) == LDAP_SUCCESS )
1046 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
1047 syncstate, syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
1050 rc = syncrepl_updateCookie( si, op, &syncCookie );
1053 if ( punlock >= 0 ) {
1054 /* on failure, revert pending CSN */
1055 if ( rc != LDAP_SUCCESS ) {
1057 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
1058 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
1059 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
1060 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
1061 &si->si_cookieState->cs_vals[i] );
1065 if ( i == si->si_cookieState->cs_num )
1066 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
1067 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
1069 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
1071 ldap_controls_free( rctrls );
1073 slap_mods_free( modlist, 1 );
1079 case LDAP_RES_SEARCH_REFERENCE:
1080 Debug( LDAP_DEBUG_ANY,
1081 "do_syncrep2: %s reference received error\n",
1082 si->si_ridtxt, 0, 0 );
1085 case LDAP_RES_SEARCH_RESULT:
1086 Debug( LDAP_DEBUG_SYNC,
1087 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
1088 si->si_ridtxt, 0, 0 );
1089 err = LDAP_OTHER; /* FIXME check parse result properly */
1090 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
1092 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
1093 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
1094 /* map old result code to registered code */
1095 err = LDAP_SYNC_REFRESH_REQUIRED;
1098 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
1099 if ( si->si_logstate == SYNCLOG_LOGGING ) {
1100 si->si_logstate = SYNCLOG_FALLBACK;
1101 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync, switching to REFRESH\n",
1102 si->si_ridtxt, 0, 0 );
1103 if (si->si_strict_refresh) {
1104 slap_suspend_listeners();
1112 Debug( LDAP_DEBUG_ANY,
1113 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1114 si->si_ridtxt, err, ldap_err2string( err ) );
1117 LDAPControl **next = NULL;
1118 /* NOTE: make sure we use the right one;
1119 * a better approach would be to run thru
1120 * the whole list and take care of all */
1121 /* NOTE: since we issue the search request,
1122 * we should know what controls to expect,
1123 * and there should be none apart from the
1124 * sync-related control */
1125 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1126 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1128 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1129 "got search result with multiple "
1130 "Sync State control\n", si->si_ridtxt, 0, 0 );
1131 ldap_controls_free( rctrls );
1137 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1139 ber_scanf( ber, "{" /*"}"*/);
1140 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1141 ber_scanf( ber, "m", &cookie );
1143 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1145 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1147 if ( !BER_BVISNULL( &cookie ) ) {
1148 ch_free( syncCookie.octet_str.bv_val );
1149 ber_dupbv( &syncCookie.octet_str, &cookie);
1151 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1153 slap_parse_sync_cookie( &syncCookie, NULL );
1154 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1157 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1159 ber_scanf( ber, "b", &refreshDeletes );
1161 ber_scanf( ber, /*"{"*/ "}" );
1163 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1164 slap_sync_cookie_free( &syncCookie_req, 0 );
1165 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1167 if ( !syncCookie.ctxcsn ) {
1169 } else if ( !syncCookie_req.ctxcsn ) {
1173 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1176 ldap_controls_free( rctrls );
1178 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1179 /* FIXME : different error behaviors according to
1180 * 1) err code : LDAP_BUSY ...
1181 * 2) on err policy : stop service, stop sync, retry
1183 if ( refreshDeletes == 0 && match < 0 &&
1184 err == LDAP_SUCCESS &&
1185 syncCookie_req.numcsns == syncCookie.numcsns )
1187 syncrepl_del_nonpresent( op, si, NULL,
1189 } else if ( si->si_presentlist ) {
1190 presentlist_free( si->si_presentlist );
1191 si->si_presentlist = NULL;
1194 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1196 rc = syncrepl_updateCookie( si, op, &syncCookie );
1198 if ( si->si_refreshCount ) {
1199 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
1200 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
1201 si->si_refreshCount = 0;
1202 si->si_refreshTxn = NULL;
1204 si->si_refreshEnd = slap_get_time();
1205 if ( err == LDAP_SUCCESS
1206 && si->si_logstate == SYNCLOG_FALLBACK ) {
1207 si->si_logstate = SYNCLOG_LOGGING;
1208 rc = LDAP_SYNC_REFRESH_REQUIRED;
1209 slap_resume_listeners();
1215 case LDAP_RES_INTERMEDIATE:
1218 rc = ldap_parse_intermediate( si->si_ld, msg,
1219 &retoid, &retdata, NULL, 0 );
1220 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1221 ber_init2( ber, retdata, LBER_USE_DER );
1223 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1225 case LDAP_TAG_SYNC_NEW_COOKIE:
1226 Debug( LDAP_DEBUG_SYNC,
1227 "do_syncrep2: %s %s - %s\n",
1229 "LDAP_RES_INTERMEDIATE",
1231 ber_scanf( ber, "tm", &tag, &cookie );
1232 Debug( LDAP_DEBUG_SYNC,
1233 "do_syncrep2: %s NEW_COOKIE: %s\n",
1236 if ( !BER_BVISNULL( &cookie ) ) {
1237 ch_free( syncCookie.octet_str.bv_val );
1238 ber_dupbv( &syncCookie.octet_str, &cookie );
1240 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1241 slap_parse_sync_cookie( &syncCookie, NULL );
1242 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1245 case LDAP_TAG_SYNC_REFRESH_DELETE:
1246 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1247 Debug( LDAP_DEBUG_SYNC,
1248 "do_syncrep2: %s %s - %s\n",
1250 "LDAP_RES_INTERMEDIATE",
1251 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1252 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1253 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1254 si->si_refreshDelete = 1;
1256 si->si_refreshPresent = 1;
1258 ber_scanf( ber, "t{" /*"}"*/, &tag );
1259 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1261 ber_scanf( ber, "m", &cookie );
1263 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1265 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1267 if ( !BER_BVISNULL( &cookie ) ) {
1268 ch_free( syncCookie.octet_str.bv_val );
1269 ber_dupbv( &syncCookie.octet_str, &cookie );
1271 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1273 slap_parse_sync_cookie( &syncCookie, NULL );
1274 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1277 /* Defaults to TRUE */
1278 if ( ber_peek_tag( ber, &len ) ==
1279 LDAP_TAG_REFRESHDONE )
1281 ber_scanf( ber, "b", &si->si_refreshDone );
1284 si->si_refreshDone = 1;
1286 if ( si->si_refreshDone ) {
1287 if ( si->si_refreshCount ) {
1288 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
1289 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
1290 si->si_refreshCount = 0;
1291 si->si_refreshTxn = NULL;
1293 si->si_refreshEnd = slap_get_time();
1294 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s finished refresh\n",
1295 si->si_ridtxt, 0, 0 );
1297 ber_scanf( ber, /*"{"*/ "}" );
1298 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
1299 si->si_refreshDone )
1302 case LDAP_TAG_SYNC_ID_SET:
1303 Debug( LDAP_DEBUG_SYNC,
1304 "do_syncrep2: %s %s - %s\n",
1306 "LDAP_RES_INTERMEDIATE",
1308 ber_scanf( ber, "t{" /*"}"*/, &tag );
1309 if ( ber_peek_tag( ber, &len ) ==
1310 LDAP_TAG_SYNC_COOKIE )
1312 ber_scanf( ber, "m", &cookie );
1314 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1316 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1318 if ( !BER_BVISNULL( &cookie ) ) {
1319 ch_free( syncCookie.octet_str.bv_val );
1320 ber_dupbv( &syncCookie.octet_str, &cookie );
1322 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1324 slap_parse_sync_cookie( &syncCookie, NULL );
1325 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1326 compare_csns( &syncCookie_req, &syncCookie, &m );
1329 if ( ber_peek_tag( ber, &len ) ==
1330 LDAP_TAG_REFRESHDELETES )
1332 ber_scanf( ber, "b", &refreshDeletes );
1335 rc = ber_scanf( ber, "[W]", &syncUUIDs );
1336 ber_scanf( ber, /*"{"*/ "}" );
1337 if ( rc != LBER_ERROR ) {
1338 if ( refreshDeletes ) {
1339 syncrepl_del_nonpresent( op, si, syncUUIDs,
1341 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1344 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1345 (void)presentlist_insert( si, &syncUUIDs[i] );
1346 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1348 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1352 slap_sync_cookie_free( &syncCookie, 0 );
1355 Debug( LDAP_DEBUG_ANY,
1356 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1357 si->si_ridtxt, (long) si_tag, 0 );
1358 ldap_memfree( retoid );
1359 ber_bvfree( retdata );
1363 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1364 slap_sync_cookie_free( &syncCookie_req, 0 );
1365 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1367 if ( !syncCookie.ctxcsn ) {
1369 } else if ( !syncCookie_req.ctxcsn ) {
1373 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1377 if ( si->si_refreshPresent == 1 &&
1378 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1379 syncCookie_req.numcsns == syncCookie.numcsns ) {
1380 syncrepl_del_nonpresent( op, si, NULL,
1384 if ( syncCookie.ctxcsn )
1386 rc = syncrepl_updateCookie( si, op, &syncCookie);
1388 if ( si->si_presentlist ) {
1389 presentlist_free( si->si_presentlist );
1390 si->si_presentlist = NULL;
1394 ldap_memfree( retoid );
1395 ber_bvfree( retdata );
1401 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1402 "unknown intermediate response (%d)\n",
1403 si->si_ridtxt, rc, 0 );
1404 ldap_memfree( retoid );
1405 ber_bvfree( retdata );
1410 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1411 "unknown message (0x%02lx)\n",
1413 (unsigned long)ldap_msgtype( msg ), 0 );
1417 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1418 slap_sync_cookie_free( &syncCookie_req, 0 );
1419 syncCookie_req = syncCookie;
1420 memset( &syncCookie, 0, sizeof( syncCookie ));
1422 ldap_msgfree( msg );
1424 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1425 slap_sync_cookie_free( &syncCookie, 0 );
1426 slap_sync_cookie_free( &syncCookie_req, 0 );
1427 if ( si->si_refreshCount ) {
1428 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
1429 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
1430 si->si_refreshCount = 0;
1431 si->si_refreshTxn = NULL;
1439 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1444 if ( err != LDAP_SUCCESS ) {
1445 Debug( LDAP_DEBUG_ANY,
1446 "do_syncrep2: %s (%d) %s\n",
1447 si->si_ridtxt, err, ldap_err2string( err ) );
1450 slap_sync_cookie_free( &syncCookie, 0 );
1451 slap_sync_cookie_free( &syncCookie_req, 0 );
1453 if ( msg ) ldap_msgfree( msg );
1455 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1456 if ( si->si_conn ) {
1457 connection_client_stop( si->si_conn );
1460 ldap_unbind_ext( si->si_ld, NULL, NULL );
1472 struct re_s* rtask = arg;
1473 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1474 Connection conn = {0};
1475 OperationBuffer opbuf;
1477 int rc = LDAP_SUCCESS;
1480 int i, defer = 1, fail = 0, freeinfo = 0;
1485 if ( slapd_shutdown )
1488 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1490 /* Don't get stuck here while a pause is initiated */
1491 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1492 if ( slapd_shutdown )
1494 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1495 ldap_pvt_thread_yield();
1500 if ( si->si_ctype < 1 ) {
1504 switch( abs( si->si_type ) ) {
1505 case LDAP_SYNC_REFRESH_ONLY:
1506 case LDAP_SYNC_REFRESH_AND_PERSIST:
1509 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1513 if ( slapd_shutdown ) {
1515 if ( si->si_conn ) {
1516 connection_client_stop( si->si_conn );
1519 ldap_unbind_ext( si->si_ld, NULL, NULL );
1522 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1526 connection_fake_init( &conn, &opbuf, ctx );
1528 /* o_connids must be unique for slap_graduate_commit_csn */
1529 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1531 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1534 /* Coordinate contextCSN updates with any syncprov overlays
1535 * in use. This may be complicated by the use of the glue
1538 * Typically there is a single syncprov mastering the entire
1539 * glued tree. In that case, our contextCSN updates should
1540 * go to the master DB. But if there is no syncprov on the
1541 * master DB, then nothing special is needed here.
1543 * Alternatively, there may be individual syncprov overlays
1544 * on each glued branch. In that case, each syncprov only
1545 * knows about changes within its own branch. And so our
1546 * contextCSN updates should only go to the local DB.
1548 if ( !si->si_wbe ) {
1549 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1550 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1551 if ( overlay_is_inst( top_be, "syncprov" ))
1552 si->si_wbe = top_be;
1558 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1559 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1560 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1562 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1565 if ( !si->si_schemachecking )
1566 op->o_no_schema_check = 1;
1568 /* Establish session, do search */
1570 si->si_refreshDelete = 0;
1571 si->si_refreshPresent = 0;
1573 if ( si->si_presentlist ) {
1574 presentlist_free( si->si_presentlist );
1575 si->si_presentlist = NULL;
1578 /* use main DB when retrieving contextCSN */
1579 op->o_bd = si->si_wbe;
1580 op->o_dn = op->o_bd->be_rootdn;
1581 op->o_ndn = op->o_bd->be_rootndn;
1582 rc = do_syncrep1( op, si );
1586 /* Process results */
1587 if ( rc == LDAP_SUCCESS ) {
1588 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1590 /* use current DB */
1592 op->o_dn = op->o_bd->be_rootdn;
1593 op->o_ndn = op->o_bd->be_rootndn;
1594 rc = do_syncrep2( op, si );
1595 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1596 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
1597 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
1598 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
1599 si->si_syncCookie.sid );
1600 rc = ldap_sync_search( si, op->o_tmpmemctx );
1605 /* We got deleted while running on cn=config */
1606 if ( si->si_ctype < 1 ) {
1607 if ( si->si_ctype == -1 ) {
1616 if ( rc != SYNC_PAUSED ) {
1617 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1618 /* If we succeeded, enable the connection for further listening.
1619 * If we failed, tear down the connection and reschedule.
1621 if ( rc == LDAP_SUCCESS ) {
1622 if ( si->si_conn ) {
1623 connection_client_enable( si->si_conn );
1625 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1627 } else if ( si->si_conn ) {
1631 if ( rc == -2 ) rc = 0;
1636 /* At this point, we have 5 cases:
1637 * 1) for any hard failure, give up and remove this task
1638 * 2) for ServerDown, reschedule this task to run later
1639 * 3) for threadpool pause, reschedule to run immediately
1640 * 4) for Refresh and Success, reschedule to run
1641 * 5) for Persist and Success, reschedule to defer
1643 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1645 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1646 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1650 connection_client_stop( si->si_conn );
1654 if ( rc == SYNC_PAUSED ) {
1655 rtask->interval.tv_sec = 0;
1656 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1657 rtask->interval.tv_sec = si->si_interval;
1659 } else if ( rc == LDAP_SUCCESS ) {
1660 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1663 rtask->interval.tv_sec = si->si_interval;
1664 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1665 if ( si->si_retrynum ) {
1666 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1667 si->si_retrynum[i] = si->si_retrynum_init[i];
1669 si->si_retrynum[i] = RETRYNUM_TAIL;
1672 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1673 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1677 if ( si->si_ctype < 1
1678 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1680 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1683 fail = RETRYNUM_TAIL;
1684 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1685 if ( si->si_retrynum[i] > 0 )
1686 si->si_retrynum[i]--;
1687 fail = si->si_retrynum[i];
1688 rtask->interval.tv_sec = si->si_retryinterval[i];
1689 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1690 slap_wake_listener();
1694 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1695 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1698 if ( fail == RETRYNUM_TAIL ) {
1699 Debug( LDAP_DEBUG_ANY,
1700 "do_syncrepl: %s rc %d quitting\n",
1701 si->si_ridtxt, rc, 0 );
1702 } else if ( fail > 0 ) {
1703 Debug( LDAP_DEBUG_ANY,
1704 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1705 si->si_ridtxt, rc, fail );
1707 Debug( LDAP_DEBUG_ANY,
1708 "do_syncrepl: %s rc %d retrying\n",
1709 si->si_ridtxt, rc, 0 );
1713 /* Do final delete cleanup */
1715 syncinfo_free( si, 0 );
1720 #ifdef ENABLE_REWRITE
1722 syncrepl_rewrite_dn(
1725 struct berval *sdn )
1730 nul = dn->bv_val[dn->bv_len];
1731 dn->bv_val[dn->bv_len] = 0;
1732 rc = rewrite( si->si_rewrite, SUFFIXM_CTX, dn->bv_val, &sdn->bv_val );
1733 dn->bv_val[dn->bv_len] = nul;
1735 if ( sdn->bv_val == dn->bv_val )
1737 else if ( rc == REWRITE_REGEXEC_OK && sdn->bv_val )
1738 sdn->bv_len = strlen( sdn->bv_val );
1741 #define REWRITE_VAL(si, ad, bv, bv2) \
1742 BER_BVZERO( &bv2 ); \
1743 if ( si->si_rewrite && ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName) \
1744 syncrepl_rewrite_dn( si, &bv, &bv2); \
1745 if ( BER_BVISNULL( &bv2 )) \
1746 ber_dupbv( &bv2, &bv )
1747 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1748 BER_BVZERO( &bv2 ); \
1749 if (si->si_rewrite) \
1750 syncrepl_rewrite_dn(si, &bv, &bv2); \
1751 rc = dnPrettyNormal( NULL, bv2.bv_val ? &bv2 : &bv, &dn, &ndn, op->o_tmpmemctx ); \
1754 #define REWRITE_VAL(si, ad, bv, bv2) ber_dupbv(&bv2, &bv)
1755 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1756 rc = dnPrettyNormal( NULL, &bv, &dn, &ndn, op->o_tmpmemctx )
1760 static slap_verbmasks modops[] = {
1761 { BER_BVC("add"), LDAP_REQ_ADD },
1762 { BER_BVC("delete"), LDAP_REQ_DELETE },
1763 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1764 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1769 syncrepl_accesslog_mods(
1771 struct berval *vals,
1772 struct Modifications **modres
1777 AttributeDescription *ad;
1778 struct berval bv, bv2;
1780 Modifications *mod = NULL, *modlist = NULL, **modtail;
1785 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1789 colon = ber_bvchr( &bv, ':' );
1795 bv.bv_len = colon - bv.bv_val;
1796 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1798 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1799 "Invalid attribute %s, %s\n",
1800 si->si_ridtxt, bv.bv_val, text );
1801 slap_mods_free( modlist, 1 );
1807 /* Ignore dynamically generated attrs */
1808 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1812 /* Ignore excluded attrs */
1813 if ( ldap_charray_inlist( si->si_exattrs,
1814 ad->ad_type->sat_cname.bv_val ) )
1820 case '+': op = LDAP_MOD_ADD; break;
1821 case '-': op = LDAP_MOD_DELETE; break;
1822 case '=': op = LDAP_MOD_REPLACE; break;
1823 case '#': op = LDAP_MOD_INCREMENT; break;
1827 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1828 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1831 mod->sml_next = NULL;
1833 mod->sml_type = ad->ad_cname;
1834 mod->sml_values = NULL;
1835 mod->sml_nvalues = NULL;
1836 mod->sml_numvals = 0;
1839 modtail = &mod->sml_next;
1841 if ( colon[2] == ' ' ) {
1842 bv.bv_val = colon + 3;
1843 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1844 REWRITE_VAL( si, ad, bv, bv2 );
1845 ber_bvarray_add( &mod->sml_values, &bv2 );
1854 syncrepl_changelog_mods(
1856 struct berval *vals,
1857 struct Modifications **modres
1860 return -1; /* FIXME */
1863 typedef struct OpExtraSync {
1868 /* Copy the original modlist, split Replace ops into Delete/Add,
1869 * and drop mod opattrs since this modification is in the past.
1871 static Modifications *mods_dup( Operation *op, Modifications *modlist, int match )
1873 Modifications *mod, *modnew = NULL, *modtail = NULL;
1875 for ( ; modlist; modlist = modlist->sml_next ) {
1878 if ( modlist->sml_desc == slap_schema.si_ad_modifiersName ||
1879 modlist->sml_desc == slap_schema.si_ad_modifyTimestamp ||
1880 modlist->sml_desc == slap_schema.si_ad_entryCSN )
1882 if ( modlist->sml_op == LDAP_MOD_REPLACE ) {
1883 mod = op->o_tmpalloc( sizeof(Modifications), op->o_tmpmemctx );
1884 mod->sml_desc = modlist->sml_desc;
1885 mod->sml_values = NULL;
1886 mod->sml_nvalues = NULL;
1887 mod->sml_op = LDAP_MOD_DELETE;
1888 mod->sml_numvals = 0;
1893 modtail->sml_next = mod;
1897 if ( modlist->sml_numvals ) {
1898 size = (modlist->sml_numvals+1) * sizeof(struct berval);
1899 if ( modlist->sml_nvalues ) size *= 2;
1903 size += sizeof(Modifications);
1904 mod = op->o_tmpalloc( size, op->o_tmpmemctx );
1908 modtail->sml_next = mod;
1910 mod->sml_desc = modlist->sml_desc;
1911 mod->sml_numvals = modlist->sml_numvals;
1913 if ( modlist->sml_numvals ) {
1915 mod->sml_values = (BerVarray)(mod+1);
1916 for (i=0; i<mod->sml_numvals; i++)
1917 mod->sml_values[i] = modlist->sml_values[i];
1918 BER_BVZERO(&mod->sml_values[i]);
1919 if ( modlist->sml_nvalues ) {
1920 mod->sml_nvalues = mod->sml_values + mod->sml_numvals + 1;
1921 for (i=0; i<mod->sml_numvals; i++)
1922 mod->sml_nvalues[i] = modlist->sml_nvalues[i];
1923 BER_BVZERO(&mod->sml_nvalues[i]);
1925 mod->sml_nvalues = NULL;
1928 mod->sml_values = NULL;
1929 mod->sml_nvalues = NULL;
1931 if ( match < 0 && modlist->sml_op == LDAP_MOD_REPLACE )
1932 mod->sml_op = LDAP_MOD_ADD;
1934 mod->sml_op = modlist->sml_op;
1935 mod->sml_next = NULL;
1940 typedef struct resolve_ctxt {
1942 Modifications *rx_mods;
1946 compare_vals( Modifications *m1, Modifications *m2 )
1949 struct berval *bv1, *bv2;
1951 if ( m2->sml_nvalues ) {
1952 bv2 = m2->sml_nvalues;
1953 bv1 = m1->sml_nvalues;
1955 bv2 = m2->sml_values;
1956 bv1 = m1->sml_values;
1958 for ( j=0; j<m2->sml_numvals; j++ ) {
1959 for ( i=0; i<m1->sml_numvals; i++ ) {
1960 if ( !ber_bvcmp( &bv1[i], &bv2[j] )) {
1962 for ( k=i; k<m1->sml_numvals-1; k++ ) {
1963 m1->sml_values[k] = m1->sml_values[k+1];
1964 if ( m1->sml_nvalues )
1965 m1->sml_nvalues[k] = m1->sml_nvalues[k+1];
1967 BER_BVZERO(&m1->sml_values[k]);
1968 if ( m1->sml_nvalues ) {
1969 BER_BVZERO(&m1->sml_nvalues[k]);
1979 syncrepl_resolve_cb( Operation *op, SlapReply *rs )
1981 if ( rs->sr_type == REP_SEARCH ) {
1982 resolve_ctxt *rx = op->o_callback->sc_private;
1983 Attribute *a = attr_find( rs->sr_entry->e_attrs, ad_reqMod );
1985 Modifications *oldmods, *newmods, *m1, *m2, **prev;
1986 oldmods = rx->rx_mods;
1987 syncrepl_accesslog_mods( rx->rx_si, a->a_vals, &newmods );
1988 for ( m2 = newmods; m2; m2=m2->sml_next ) {
1989 for ( prev = &oldmods, m1 = *prev; m1; m1 = *prev ) {
1990 if ( m1->sml_desc != m2->sml_desc ) {
1991 prev = &m1->sml_next;
1994 if ( m2->sml_op == LDAP_MOD_DELETE ||
1995 m2->sml_op == LDAP_MOD_REPLACE ) {
1996 int numvals = m2->sml_numvals;
1997 if ( m2->sml_op == LDAP_MOD_REPLACE )
1999 /* New delete All cancels everything */
2000 if ( numvals == 0 ) {
2002 *prev = m1->sml_next;
2003 op->o_tmpfree( m1, op->o_tmpmemctx );
2006 if ( m1->sml_op == LDAP_MOD_DELETE ) {
2007 if ( m1->sml_numvals == 0 ) {
2008 /* turn this to SOFTDEL later */
2009 m1->sml_flags = SLAP_MOD_INTERNAL;
2011 compare_vals( m1, m2 );
2012 if ( !m1->sml_numvals )
2015 } else if ( m1->sml_op == LDAP_MOD_ADD ) {
2016 compare_vals( m1, m2 );
2017 if ( !m1->sml_numvals )
2022 if ( m2->sml_op == LDAP_MOD_ADD ||
2023 m2->sml_op == LDAP_MOD_REPLACE ) {
2024 if ( m1->sml_op == LDAP_MOD_DELETE ) {
2025 if ( !m1->sml_numvals ) goto drop;
2026 compare_vals( m1, m2 );
2027 if ( !m1->sml_numvals )
2030 if ( m2->sml_desc->ad_type->sat_atype.at_single_value )
2032 compare_vals( m1, m2 );
2033 if ( !m1->sml_numvals )
2036 prev = &m1->sml_next;
2039 slap_mods_free( newmods, 1 );
2040 rx->rx_mods = oldmods;
2043 return LDAP_SUCCESS;
2046 typedef struct modify_ctxt {
2047 Modifications *mx_orig;
2048 Modifications *mx_free;
2052 syncrepl_modify_cb( Operation *op, SlapReply *rs )
2054 slap_callback *sc = op->o_callback;
2055 modify_ctxt *mx = sc->sc_private;
2058 op->orm_no_opattrs = 0;
2059 op->orm_modlist = mx->mx_orig;
2060 for ( ml = mx->mx_free; ml; ml = mx->mx_free ) {
2061 mx->mx_free = ml->sml_next;
2062 op->o_tmpfree( ml, op->o_tmpmemctx );
2064 op->o_callback = sc->sc_next;
2065 op->o_tmpfree( sc, op->o_tmpmemctx );
2066 return SLAP_CB_CONTINUE;
2070 syncrepl_op_modify( Operation *op, SlapReply *rs )
2072 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2077 Modifications *mod, *newlist;
2079 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
2080 if ( oex->oe_key == (void *)syncrepl_message_to_op )
2084 return SLAP_CB_CONTINUE;
2086 si = ((OpExtraSync *)oex)->oe_si;
2088 /* Check if entryCSN in modlist is newer than entryCSN in entry.
2089 * We do it here because the op has been serialized by accesslog
2090 * by the time we get here. If the CSN is new enough, just do the
2091 * mod. If not, we need to resolve conflicts.
2094 for ( mod = op->orm_modlist; mod; mod=mod->sml_next ) {
2095 if ( mod->sml_desc == slap_schema.si_ad_entryCSN ) break;
2097 /* FIXME: what should we do if entryCSN is missing from the mod? */
2099 return SLAP_CB_CONTINUE;
2101 rc = overlay_entry_get_ov( op, &op->o_req_ndn, NULL, NULL, 0, &e, on );
2105 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
2106 value_match( &match, slap_schema.si_ad_entryCSN,
2107 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
2108 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2109 &mod->sml_nvalues[0], &a->a_nvals[0], &text );
2110 overlay_entry_release_ov( op, e, 0, on );
2112 /* equal? Should never happen */
2114 /* tell accesslog this was a failure */
2115 rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
2116 return LDAP_SUCCESS;
2119 /* mod is older: resolve conflicts...
2120 * 1. Save/copy original modlist. Split Replace to Del/Add.
2121 * 2. Find all mods to this reqDN newer than the mod stamp.
2122 * 3. Resolve any mods in this request that affect attributes
2123 * touched by newer mods.
2125 * delete all delete all drop
2126 * delete all delete X SOFTDEL
2127 * delete X delete all drop
2128 * delete X delete X drop
2129 * delete X delete Y OK
2130 * delete all add X drop
2131 * delete X add X drop
2133 * add X delete all drop
2134 * add X delete X drop
2136 * add X add Y if SV, drop else OK
2138 * 4. Swap original modlist back in response callback so
2139 * that accesslog logs the original mod.
2141 * Even if the mod is newer, other out-of-order changes may
2142 * have been committed, forcing us to tweak the modlist:
2143 * 1. Save/copy original modlist.
2144 * 2. Change deletes to soft deletes.
2145 * 3. Change Adds of single-valued attrs to Replace.
2148 newlist = mods_dup( op, op->orm_modlist, match );
2152 Operation op2 = *op;
2153 AttributeName an[2];
2159 SlapReply rs1 = {0};
2161 slap_callback cb = { NULL, syncrepl_resolve_cb, NULL, NULL };
2164 rx.rx_mods = newlist;
2165 cb.sc_private = ℞
2167 op2.o_tag = LDAP_REQ_SEARCH;
2168 op2.ors_scope = LDAP_SCOPE_SUBTREE;
2169 op2.ors_deref = LDAP_DEREF_NEVER;
2170 op2.o_req_dn = si->si_logbase;
2171 op2.o_req_ndn = si->si_logbase;
2172 op2.ors_tlimit = SLAP_NO_LIMIT;
2173 op2.ors_slimit = SLAP_NO_LIMIT;
2174 op2.ors_limit = NULL;
2175 memset( an, 0, sizeof(an));
2176 an[0].an_desc = ad_reqMod;
2177 an[0].an_name = ad_reqMod->ad_cname;
2179 op2.ors_attrsonly = 0;
2181 bv = mod->sml_nvalues[0];
2183 size = sizeof("(&(entryCSN>=)(reqDN=))");
2184 size += bv.bv_len + op->o_req_ndn.bv_len + si->si_logfilterstr.bv_len;
2185 op2.ors_filterstr.bv_val = op->o_tmpalloc( size, op->o_tmpmemctx );
2186 op2.ors_filterstr.bv_len = sprintf(op2.ors_filterstr.bv_val,
2187 "(&(entryCSN>=%s)(reqDN=%s)%s)",
2188 bv.bv_val, op->o_req_ndn.bv_val, si->si_logfilterstr.bv_val );
2189 op2.ors_filter = str2filter_x( op, op2.ors_filterstr.bv_val );
2191 op2.o_callback = &cb;
2192 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2193 op2.o_bd->be_search( &op2, &rs1 );
2194 newlist = rx.rx_mods;
2198 slap_callback *sc = op->o_tmpalloc( sizeof(slap_callback) +
2199 sizeof(modify_ctxt), op->o_tmpmemctx );
2200 modify_ctxt *mx = (modify_ctxt *)(sc+1);
2203 sc->sc_response = syncrepl_modify_cb;
2204 sc->sc_private = mx;
2205 sc->sc_next = op->o_callback;
2206 sc->sc_cleanup = NULL;
2207 op->o_callback = sc;
2208 op->orm_no_opattrs = 1;
2209 mx->mx_orig = op->orm_modlist;
2210 mx->mx_free = newlist;
2211 for ( ml = newlist; ml; ml=ml->sml_next ) {
2212 if ( ml->sml_flags == SLAP_MOD_INTERNAL ) {
2214 ml->sml_op = SLAP_MOD_SOFTDEL;
2216 else if ( ml->sml_op == LDAP_MOD_DELETE )
2217 ml->sml_op = SLAP_MOD_SOFTDEL;
2218 else if ( ml->sml_op == LDAP_MOD_ADD &&
2219 ml->sml_desc->ad_type->sat_atype.at_single_value )
2220 ml->sml_op = LDAP_MOD_REPLACE;
2222 op->orm_modlist = newlist;
2223 op->o_csn = mod->sml_nvalues[0];
2225 return SLAP_CB_CONTINUE;
2229 syncrepl_message_to_op(
2235 BerElement *ber = NULL;
2236 Modifications *modlist = NULL;
2238 SlapReply rs = { REP_RESULT };
2239 slap_callback cb = { NULL, null_callback, NULL, NULL };
2242 char txtbuf[SLAP_TEXT_BUFLEN];
2243 size_t textlen = sizeof txtbuf;
2245 struct berval bdn, dn = BER_BVNULL, ndn;
2246 struct berval bv, bv2, *bvals = NULL;
2247 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
2248 prdn = BER_BVNULL, nrdn = BER_BVNULL,
2249 psup = BER_BVNULL, nsup = BER_BVNULL;
2250 int rc, deleteOldRdn = 0, freeReqDn = 0;
2251 int do_graduate = 0;
2253 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2254 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2255 "Message type should be entry (%d)",
2256 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2260 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
2265 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2267 if ( rc != LDAP_SUCCESS ) {
2268 Debug( LDAP_DEBUG_ANY,
2269 "syncrepl_message_to_op: %s dn get failed (%d)",
2270 si->si_ridtxt, rc, 0 );
2274 op->o_tag = LBER_DEFAULT;
2275 op->o_bd = si->si_wbe;
2277 if ( BER_BVISEMPTY( &bdn )) {
2278 Debug( LDAP_DEBUG_ANY,
2279 "syncrepl_message_to_op: %s got empty dn",
2280 si->si_ridtxt, 0, 0 );
2284 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
2286 if ( bv.bv_val == NULL )
2289 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
2291 REWRITE_DN( si, bdn, bv2, dn, ndn );
2292 if ( rc != LDAP_SUCCESS ) {
2293 Debug( LDAP_DEBUG_ANY,
2294 "syncrepl_message_to_op: %s "
2295 "dn \"%s\" normalization failed (%d)",
2296 si->si_ridtxt, bdn.bv_val, rc );
2301 ber_dupbv( &op->o_req_dn, &dn );
2302 ber_dupbv( &op->o_req_ndn, &ndn );
2303 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2304 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2306 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
2307 int i = verb_to_mask( bvals[0].bv_val, modops );
2309 Debug( LDAP_DEBUG_ANY,
2310 "syncrepl_message_to_op: %s unknown op %s",
2311 si->si_ridtxt, bvals[0].bv_val, 0 );
2316 op->o_tag = modops[i].mask;
2317 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
2318 /* Parse attribute into modlist */
2319 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
2320 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
2322 rc = syncrepl_changelog_mods( si, bvals, &modlist );
2324 if ( rc ) goto done;
2325 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
2327 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
2328 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
2331 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
2333 } else if ( !ber_bvstrcasecmp( &bv,
2334 &slap_schema.si_ad_entryCSN->ad_cname ) )
2336 slap_queue_csn( op, bvals );
2342 /* If we didn't get a mod type or a target DN, bail out */
2343 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
2348 op->o_callback = &cb;
2349 slap_op_time( &op->o_time, &op->o_tincr );
2351 switch( op->o_tag ) {
2353 case LDAP_REQ_MODIFY:
2354 /* If we didn't get required data, bail */
2355 if ( !modlist ) goto done;
2357 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
2359 if ( rc != LDAP_SUCCESS ) {
2360 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2361 "mods check (%s)\n",
2362 si->si_ridtxt, text, 0 );
2366 if ( op->o_tag == LDAP_REQ_ADD ) {
2367 Entry *e = entry_alloc();
2369 op->ora_e->e_name = op->o_req_dn;
2370 op->ora_e->e_nname = op->o_req_ndn;
2372 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
2373 if( rc != LDAP_SUCCESS ) {
2374 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2375 "mods2entry (%s)\n",
2376 si->si_ridtxt, text, 0 );
2378 rc = op->o_bd->be_add( op, &rs );
2379 Debug( LDAP_DEBUG_SYNC,
2380 "syncrepl_message_to_op: %s be_add %s (%d)\n",
2381 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2384 if ( e == op->ora_e )
2385 be_entry_release_w( op, op->ora_e );
2388 op->orm_modlist = modlist;
2389 op->o_bd = si->si_wbe;
2390 /* delta-mmr needs additional checks in syncrepl_op_modify */
2391 if ( SLAP_MULTIMASTER( op->o_bd )) {
2392 oes.oe.oe_key = (void *)syncrepl_message_to_op;
2394 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &oes.oe, oe_next );
2396 rc = op->o_bd->be_modify( op, &rs );
2397 if ( SLAP_MULTIMASTER( op->o_bd )) {
2398 LDAP_SLIST_REMOVE( &op->o_extra, &oes.oe, OpExtra, oe_next );
2399 BER_BVZERO( &op->o_csn );
2401 modlist = op->orm_modlist;
2402 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2403 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
2404 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2405 op->o_bd = si->si_be;
2409 case LDAP_REQ_MODRDN:
2410 if ( BER_BVISNULL( &rdn ) ) goto done;
2412 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
2415 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
2418 if ( !BER_BVISNULL( &sup ) ) {
2419 REWRITE_DN( si, sup, bv2, psup, nsup );
2422 op->orr_newSup = &psup;
2423 op->orr_nnewSup = ⊅
2425 op->orr_newSup = NULL;
2426 op->orr_nnewSup = NULL;
2428 op->orr_newrdn = prdn;
2429 op->orr_nnewrdn = nrdn;
2430 op->orr_deleteoldrdn = deleteOldRdn;
2431 op->orr_modlist = NULL;
2432 if ( slap_modrdn2mods( op, &rs ) ) {
2436 /* Append modlist for operational attrs */
2440 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
2442 m->sml_next = modlist;
2445 rc = op->o_bd->be_modrdn( op, &rs );
2446 slap_mods_free( op->orr_modlist, 1 );
2447 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2448 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
2449 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2452 case LDAP_REQ_DELETE:
2453 rc = op->o_bd->be_delete( op, &rs );
2454 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2455 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
2456 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2462 slap_graduate_commit_csn( op );
2463 op->o_bd = si->si_be;
2464 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2465 BER_BVZERO( &op->o_csn );
2467 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
2469 if ( !BER_BVISNULL( &rdn ) ) {
2470 if ( !BER_BVISNULL( &nsup ) ) {
2471 ch_free( nsup.bv_val );
2473 if ( !BER_BVISNULL( &psup ) ) {
2474 ch_free( psup.bv_val );
2476 if ( !BER_BVISNULL( &nrdn ) ) {
2477 ch_free( nrdn.bv_val );
2479 if ( !BER_BVISNULL( &prdn ) ) {
2480 ch_free( prdn.bv_val );
2484 ch_free( op->o_req_ndn.bv_val );
2485 ch_free( op->o_req_dn.bv_val );
2492 syncrepl_message_to_entry(
2496 Modifications **modlist,
2499 struct berval *syncUUID
2503 BerElement *ber = NULL;
2506 Modifications **modtail = modlist;
2509 char txtbuf[SLAP_TEXT_BUFLEN];
2510 size_t textlen = sizeof txtbuf;
2512 struct berval bdn = BER_BVNULL, dn, ndn, bv2;
2517 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2518 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
2519 "Message type should be entry (%d)",
2520 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2524 op->o_tag = LDAP_REQ_ADD;
2526 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2527 if ( rc != LDAP_SUCCESS ) {
2528 Debug( LDAP_DEBUG_ANY,
2529 "syncrepl_message_to_entry: %s dn get failed (%d)",
2530 si->si_ridtxt, rc, 0 );
2534 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
2535 Debug( LDAP_DEBUG_ANY,
2536 "syncrepl_message_to_entry: %s got empty dn",
2537 si->si_ridtxt, 0, 0 );
2541 /* syncUUID[0] is normalized UUID received over the wire
2542 * syncUUID[1] is denormalized UUID, generated here
2544 (void)slap_uuidstr_from_normalized( &syncUUID[1], &syncUUID[0], op->o_tmpmemctx );
2545 Debug( LDAP_DEBUG_SYNC,
2546 "syncrepl_message_to_entry: %s DN: %s, UUID: %s\n",
2547 si->si_ridtxt, bdn.bv_val, syncUUID[1].bv_val );
2549 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
2550 /* NOTE: this could be done even before decoding the DN,
2551 * although encoding errors wouldn't be detected */
2556 if ( entry == NULL ) {
2560 REWRITE_DN( si, bdn, bv2, dn, ndn );
2561 if ( rc != LDAP_SUCCESS ) {
2562 /* One of the things that could happen is that the schema
2563 * is not lined-up; this could result in unknown attributes.
2564 * A value non conformant to the syntax should be unlikely,
2565 * except when replicating between different versions
2566 * of the software, or when syntax validation bugs are fixed
2568 Debug( LDAP_DEBUG_ANY,
2569 "syncrepl_message_to_entry: "
2570 "%s dn \"%s\" normalization failed (%d)",
2571 si->si_ridtxt, bdn.bv_val, rc );
2575 ber_dupbv( &op->o_req_dn, &dn );
2576 ber_dupbv( &op->o_req_ndn, &ndn );
2577 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2578 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2580 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
2583 e->e_name = op->o_req_dn;
2584 e->e_nname = op->o_req_ndn;
2586 while ( ber_remaining( ber ) ) {
2587 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
2588 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2593 /* Drop all updates to the contextCSN of the context entry
2596 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2597 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2598 ber_bvarray_free( tmp.sml_values );
2602 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2604 mod->sml_op = LDAP_MOD_REPLACE;
2606 mod->sml_next = NULL;
2607 mod->sml_desc = NULL;
2608 mod->sml_type = tmp.sml_type;
2609 mod->sml_values = tmp.sml_values;
2610 mod->sml_nvalues = NULL;
2611 mod->sml_numvals = 0; /* slap_mods_check will set this */
2613 #ifdef ENABLE_REWRITE
2614 if (si->si_rewrite) {
2615 AttributeDescription *ad = NULL;
2616 slap_bv2ad( &tmp.sml_type, &ad, &text );
2619 mod->sml_type = ad->ad_cname;
2620 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
2622 for ( i = 0; tmp.sml_values[i].bv_val; i++ ) {
2623 syncrepl_rewrite_dn( si, &tmp.sml_values[i], &bv2);
2624 if ( !BER_BVISNULL( &bv2 )) {
2625 ber_memfree( tmp.sml_values[i].bv_val );
2626 tmp.sml_values[i] = bv2;
2634 modtail = &mod->sml_next;
2637 if ( *modlist == NULL ) {
2638 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2639 si->si_ridtxt, 0, 0 );
2644 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2646 if ( rc != LDAP_SUCCESS ) {
2647 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2648 si->si_ridtxt, text, 0 );
2652 /* Strip out dynamically generated attrs */
2653 for ( modtail = modlist; *modtail ; ) {
2655 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2656 *modtail = mod->sml_next;
2657 slap_mod_free( &mod->sml_mod, 0 );
2660 modtail = &mod->sml_next;
2664 /* Strip out attrs in exattrs list */
2665 for ( modtail = modlist; *modtail ; ) {
2667 if ( ldap_charray_inlist( si->si_exattrs,
2668 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2670 *modtail = mod->sml_next;
2671 slap_mod_free( &mod->sml_mod, 0 );
2674 modtail = &mod->sml_next;
2678 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2679 if( rc != LDAP_SUCCESS ) {
2680 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2681 si->si_ridtxt, text, 0 );
2686 if ( rc != LDAP_SUCCESS ) {
2698 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2700 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2701 * entry if a previous refresh was interrupted before sending us a new
2702 * context state. We try to compare the new entry to the existing entry
2703 * and ignore the new entry if they are the same.
2705 * Also, we may get an update where the entryDN has changed, due to
2706 * a ModDn on the provider. We detect this as well, so we can issue
2707 * the corresponding operation locally.
2709 * In the case of a modify, we get a list of all the attributes
2710 * in the original entry. Rather than deleting the entry and re-adding it,
2711 * we issue a Modify request that deletes all the attributes and adds all
2712 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2715 * We otherwise distinguish ModDN from Modify; in the case of
2716 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2717 * operational attributes from the entry, and do a regular ModDN.
2719 typedef struct dninfo {
2723 struct berval nnewSup;
2724 int renamed; /* Was an existing entry renamed? */
2725 int delOldRDN; /* Was old RDN deleted? */
2726 Modifications **modlist; /* the modlist we received */
2727 Modifications *mods; /* the modlist we compared */
2728 int oldNcount; /* #values of old naming attr */
2729 AttributeDescription *oldDesc; /* for renames */
2730 AttributeDescription *newDesc; /* for renames */
2735 /* return 1 if inserted, 0 otherwise */
2739 struct berval *syncUUID )
2747 if ( !si->si_presentlist )
2748 si->si_presentlist = ch_calloc(65536, sizeof( Avlnode * ));
2750 av = (Avlnode **)si->si_presentlist;
2752 val = ch_malloc(UUIDLEN-2);
2753 memcpy(&s, syncUUID->bv_val, 2);
2754 memcpy(val, syncUUID->bv_val+2, UUIDLEN-2);
2756 if ( avl_insert( &av[s], val,
2757 syncuuid_cmp, avl_dup_error ) )
2763 val = ch_malloc(UUIDLEN);
2765 AC_MEMCPY( val, syncUUID->bv_val, UUIDLEN );
2767 if ( avl_insert( &si->si_presentlist, val,
2768 syncuuid_cmp, avl_dup_error ) )
2781 struct berval *val )
2784 Avlnode **a2 = (Avlnode **)av;
2790 memcpy(&s, val->bv_val, 2);
2791 return avl_find( a2[s], val->bv_val+2, syncuuid_cmp );
2793 return avl_find( av, val->bv_val, syncuuid_cmp );
2798 presentlist_free( Avlnode *av )
2801 Avlnode **a2 = (Avlnode **)av;
2805 for (i=0; i<65536; i++) {
2807 count += avl_free( a2[i], ch_free );
2813 return avl_free( av, ch_free );
2820 struct berval *val )
2823 Avlnode **a2 = *(Avlnode ***)av;
2826 memcpy(&s, val->bv_val, 2);
2827 avl_delete( &a2[s], val->bv_val+2, syncuuid_cmp );
2829 avl_delete( av, val->bv_val, syncuuid_cmp );
2838 Modifications** modlist,
2840 struct berval* syncUUID,
2841 struct berval* syncCSN )
2843 Backend *be = op->o_bd;
2844 slap_callback cb = { NULL, NULL, NULL, NULL };
2845 int syncuuid_inserted = 0;
2847 SlapReply rs_search = {REP_RESULT};
2849 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2850 int rc = LDAP_SUCCESS;
2852 struct berval pdn = BER_BVNULL;
2857 Debug( LDAP_DEBUG_SYNC,
2858 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2859 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2861 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2862 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2863 syncuuid_inserted = presentlist_insert( si, syncUUID );
2867 if ( syncstate == LDAP_SYNC_PRESENT ) {
2869 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2870 if ( entry == NULL ) {
2875 if ( syncstate != LDAP_SYNC_DELETE ) {
2876 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2879 /* add if missing */
2880 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2881 &syncUUID[1], syncUUID );
2883 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2884 /* replace only if necessary */
2885 if ( a->a_nvals != a->a_vals ) {
2886 ber_memfree( a->a_nvals[0].bv_val );
2887 ber_dupbv( &a->a_nvals[0], syncUUID );
2889 ber_memfree( a->a_vals[0].bv_val );
2890 ber_dupbv( &a->a_vals[0], &syncUUID[1] );
2894 f.f_choice = LDAP_FILTER_EQUALITY;
2896 ava.aa_desc = slap_schema.si_ad_entryUUID;
2897 ava.aa_value = *syncUUID;
2899 if ( syncuuid_inserted ) {
2900 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2901 si->si_ridtxt, syncUUID[1].bv_val, 0 );
2903 op->ors_filter = &f;
2905 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID[1].bv_len;
2906 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2907 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2908 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2909 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2910 syncUUID[1].bv_val, syncUUID[1].bv_len );
2911 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2912 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2914 op->o_tag = LDAP_REQ_SEARCH;
2915 op->ors_scope = LDAP_SCOPE_SUBTREE;
2916 op->ors_deref = LDAP_DEREF_NEVER;
2918 /* get the entry for this UUID */
2919 #ifdef ENABLE_REWRITE
2920 if ( si->si_rewrite ) {
2921 op->o_req_dn = si->si_suffixm;
2922 op->o_req_ndn = si->si_suffixm;
2926 op->o_req_dn = si->si_base;
2927 op->o_req_ndn = si->si_base;
2930 op->o_time = slap_get_time();
2931 op->ors_tlimit = SLAP_NO_LIMIT;
2933 op->ors_limit = NULL;
2935 op->ors_attrs = slap_anlist_all_attributes;
2936 op->ors_attrsonly = 0;
2938 /* set callback function */
2939 op->o_callback = &cb;
2940 cb.sc_response = dn_callback;
2941 cb.sc_private = &dni;
2942 dni.new_entry = entry;
2943 dni.modlist = modlist;
2945 rc = be->be_search( op, &rs_search );
2946 Debug( LDAP_DEBUG_SYNC,
2947 "syncrepl_entry: %s be_search (%d)\n",
2948 si->si_ridtxt, rc, 0 );
2950 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2951 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2954 cb.sc_response = null_callback;
2957 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2958 Debug( LDAP_DEBUG_SYNC,
2959 "syncrepl_entry: %s %s\n",
2960 si->si_ridtxt, entry->e_name.bv_val, 0 );
2962 Debug( LDAP_DEBUG_SYNC,
2963 "syncrepl_entry: %s %s\n",
2964 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2967 assert( BER_BVISNULL( &op->o_csn ) );
2969 slap_queue_csn( op, syncCSN );
2972 if ( !si->si_refreshDone ) {
2973 if ( si->si_lazyCommit )
2974 op->o_lazyCommit = SLAP_CONTROL_NONCRITICAL;
2975 if ( si->si_refreshCount == 500 ) {
2976 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
2977 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
2978 si->si_refreshCount = 0;
2979 si->si_refreshTxn = NULL;
2981 if ( op->o_bd->bd_info->bi_op_txn ) {
2982 if ( !si->si_refreshCount ) {
2983 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_BEGIN, &si->si_refreshTxn );
2985 si->si_refreshCount++;
2989 slap_op_time( &op->o_time, &op->o_tincr );
2990 switch ( syncstate ) {
2992 case LDAP_SYNC_MODIFY:
2993 if ( BER_BVISNULL( &op->o_csn ))
2996 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2998 /* FIXME: op->o_csn is assumed to be
2999 * on the thread's slab; this needs
3000 * to be cleared ASAP.
3002 op->o_csn = a->a_vals[0];
3007 if ( BER_BVISNULL( &dni.dn ) ) {
3008 SlapReply rs_add = {REP_RESULT};
3010 op->o_req_dn = entry->e_name;
3011 op->o_req_ndn = entry->e_nname;
3012 op->o_tag = LDAP_REQ_ADD;
3014 op->o_bd = si->si_wbe;
3016 rc = op->o_bd->be_add( op, &rs_add );
3017 Debug( LDAP_DEBUG_SYNC,
3018 "syncrepl_entry: %s be_add %s (%d)\n",
3019 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3020 switch ( rs_add.sr_err ) {
3022 if ( op->ora_e == entry ) {
3023 be_entry_release_w( op, entry );
3029 /* we assume that LDAP_NO_SUCH_OBJECT is returned
3030 * only if the suffix entry is not present.
3031 * This should not happen during Persist phase.
3033 case LDAP_NO_SUCH_OBJECT:
3034 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
3035 si->si_refreshDone ) {
3036 /* Something's wrong, start over */
3037 ber_bvarray_free( si->si_syncCookie.ctxcsn );
3038 si->si_syncCookie.ctxcsn = NULL;
3039 entry_free( entry );
3040 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3041 ber_bvarray_free( si->si_cookieState->cs_vals );
3042 ch_free( si->si_cookieState->cs_sids );
3043 si->si_cookieState->cs_vals = NULL;
3044 si->si_cookieState->cs_sids = 0;
3045 si->si_cookieState->cs_num = 0;
3046 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3047 return LDAP_NO_SUCH_OBJECT;
3049 rc = syncrepl_add_glue( op, entry );
3053 /* if an entry was added via syncrepl_add_glue(),
3054 * it likely has no entryUUID, so the previous
3055 * be_search() doesn't find it. In this case,
3056 * give syncrepl a chance to modify it. Also
3057 * allow for entries that were recreated with the
3058 * same DN but a different entryUUID.
3060 case LDAP_ALREADY_EXISTS:
3062 Operation op2 = *op;
3063 SlapReply rs2 = { REP_RESULT };
3064 slap_callback cb2 = { 0 };
3067 op2.o_tag = LDAP_REQ_SEARCH;
3068 op2.o_req_dn = entry->e_name;
3069 op2.o_req_ndn = entry->e_nname;
3070 op2.ors_scope = LDAP_SCOPE_BASE;
3071 op2.ors_deref = LDAP_DEREF_NEVER;
3072 op2.ors_attrs = slap_anlist_all_attributes;
3073 op2.ors_attrsonly = 0;
3074 op2.ors_limit = NULL;
3076 op2.ors_tlimit = SLAP_NO_LIMIT;
3078 f.f_choice = LDAP_FILTER_PRESENT;
3079 f.f_desc = slap_schema.si_ad_objectClass;
3080 op2.ors_filter = &f;
3081 op2.ors_filterstr = generic_filterstr;
3083 op2.o_callback = &cb2;
3084 cb2.sc_response = dn_callback;
3085 cb2.sc_private = &dni;
3087 rc = be->be_search( &op2, &rs2 );
3088 if ( rc ) goto done;
3091 slap_op_time( &op->o_time, &op->o_tincr );
3097 Debug( LDAP_DEBUG_ANY,
3098 "syncrepl_entry: %s be_add %s failed (%d)\n",
3099 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
3107 op->o_req_dn = dni.dn;
3108 op->o_req_ndn = dni.ndn;
3109 if ( dni.renamed ) {
3110 struct berval noldp, newp;
3111 Modifications *mod, **modtail, **ml, *m2;
3112 int i, got_replace = 0, just_rename = 0;
3113 SlapReply rs_modify = {REP_RESULT};
3115 op->o_tag = LDAP_REQ_MODRDN;
3116 dnRdn( &entry->e_name, &op->orr_newrdn );
3117 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
3119 if ( !BER_BVISNULL( &dni.nnewSup )) {
3120 dnParent( &entry->e_name, &newp );
3121 op->orr_newSup = &newp;
3122 op->orr_nnewSup = &dni.nnewSup;
3124 op->orr_newSup = NULL;
3125 op->orr_nnewSup = NULL;
3127 op->orr_deleteoldrdn = dni.delOldRDN;
3128 op->orr_modlist = NULL;
3129 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
3133 /* Drop the RDN-related mods from this op, because their
3134 * equivalents were just setup by slap_modrdn2mods.
3136 * If delOldRDN is TRUE then we should see a delete modop
3137 * for oldDesc. We might see a replace instead.
3138 * delete with no values: therefore newDesc != oldDesc.
3139 * if oldNcount == 1, then Drop this op.
3140 * delete with 1 value: can only be the oldRDN value. Drop op.
3141 * delete with N values: Drop oldRDN value, keep remainder.
3142 * replace with 1 value: if oldNcount == 1 and
3143 * newDesc == oldDesc, Drop this op.
3144 * Any other cases must be left intact.
3146 * We should also see an add modop for newDesc. (But not if
3147 * we got a replace modop due to delOldRDN.) If it has
3148 * multiple values, we'll have to drop the new RDN value.
3150 modtail = &op->orr_modlist;
3151 if ( dni.delOldRDN ) {
3152 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3153 if ( (*ml)->sml_desc == dni.oldDesc ) {
3155 if ( mod->sml_op == LDAP_MOD_REPLACE &&
3156 dni.oldDesc != dni.newDesc ) {
3157 /* This Replace is due to other Mods.
3162 if ( mod->sml_numvals <= 1 &&
3163 dni.oldNcount == 1 &&
3164 ( mod->sml_op == LDAP_MOD_DELETE ||
3165 mod->sml_op == LDAP_MOD_REPLACE )) {
3166 if ( mod->sml_op == LDAP_MOD_REPLACE )
3169 *ml = mod->sml_next;
3170 mod->sml_next = NULL;
3171 slap_mods_free( mod, 1 );
3174 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
3176 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3177 if ( m2->sml_desc == dni.oldDesc &&
3178 m2->sml_op == LDAP_MOD_DELETE ) break;
3180 for ( i=0; i<mod->sml_numvals; i++ ) {
3181 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3183 ch_free( mod->sml_values[i].bv_val );
3184 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3185 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3186 if ( mod->sml_nvalues ) {
3187 ch_free( mod->sml_nvalues[i].bv_val );
3188 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3189 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3194 if ( !mod->sml_numvals ) {
3196 *ml = mod->sml_next;
3197 mod->sml_next = NULL;
3198 slap_mods_free( mod, 1 );
3204 if ( !got_replace ) {
3205 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3206 if ( (*ml)->sml_desc == dni.newDesc ) {
3208 if ( mod->sml_op != LDAP_MOD_ADD )
3210 if ( mod->sml_numvals == 1 ) {
3212 *ml = mod->sml_next;
3213 mod->sml_next = NULL;
3214 slap_mods_free( mod, 1 );
3217 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3218 if ( m2->sml_desc == dni.oldDesc &&
3219 m2->sml_op == SLAP_MOD_SOFTADD ) break;
3221 for ( i=0; i<mod->sml_numvals; i++ ) {
3222 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3224 ch_free( mod->sml_values[i].bv_val );
3225 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3226 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3227 if ( mod->sml_nvalues ) {
3228 ch_free( mod->sml_nvalues[i].bv_val );
3229 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3230 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3240 /* RDNs must be NUL-terminated for back-ldap */
3241 noldp = op->orr_newrdn;
3242 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
3243 noldp = op->orr_nnewrdn;
3244 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
3246 /* Setup opattrs too */
3248 static AttributeDescription *nullattr = NULL;
3249 static AttributeDescription **const opattrs[] = {
3250 &slap_schema.si_ad_entryCSN,
3251 &slap_schema.si_ad_modifiersName,
3252 &slap_schema.si_ad_modifyTimestamp,
3255 AttributeDescription *opattr;
3259 /* pull mod off incoming modlist */
3260 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
3261 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
3263 if ( (*ml)->sml_desc == opattr ) {
3265 *ml = mod->sml_next;
3266 mod->sml_next = NULL;
3268 modtail = &mod->sml_next;
3273 /* If there are still Modifications left, put the opattrs
3274 * back, and let be_modify run. Otherwise, append the opattrs
3275 * to the orr_modlist.
3279 /* don't set a CSN for the rename op */
3281 slap_graduate_commit_csn( op );
3283 mod = op->orr_modlist;
3286 for ( ; mod->sml_next; mod=mod->sml_next );
3289 op->o_bd = si->si_wbe;
3291 rs_reinit( &rs_modify, REP_RESULT );
3292 rc = op->o_bd->be_modrdn( op, &rs_modify );
3294 /* NOTE: noSuchObject should result because the new superior
3295 * has not been added yet (ITS#6472) */
3296 if ( rc == LDAP_NO_SUCH_OBJECT && op->orr_nnewSup != NULL ) {
3297 Operation op2 = *op;
3298 rc = syncrepl_add_glue_ancestors( &op2, entry );
3299 if ( rc == LDAP_SUCCESS ) {
3304 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
3305 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
3307 slap_mods_free( op->orr_modlist, 1 );
3308 Debug( LDAP_DEBUG_SYNC,
3309 "syncrepl_entry: %s be_modrdn %s (%d)\n",
3310 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3312 /* Renamed entries may still have other mods so just fallthru */
3313 op->o_req_dn = entry->e_name;
3314 op->o_req_ndn = entry->e_nname;
3315 /* Use CSN on the modify */
3319 slap_queue_csn( op, syncCSN );
3322 SlapReply rs_modify = {REP_RESULT};
3324 op->o_tag = LDAP_REQ_MODIFY;
3325 op->orm_modlist = dni.mods;
3326 op->orm_no_opattrs = 1;
3327 op->o_bd = si->si_wbe;
3329 rc = op->o_bd->be_modify( op, &rs_modify );
3330 slap_mods_free( op->orm_modlist, 1 );
3331 op->orm_no_opattrs = 0;
3332 Debug( LDAP_DEBUG_SYNC,
3333 "syncrepl_entry: %s be_modify %s (%d)\n",
3334 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3335 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
3336 Debug( LDAP_DEBUG_ANY,
3337 "syncrepl_entry: %s be_modify failed (%d)\n",
3338 si->si_ridtxt, rs_modify.sr_err, 0 );
3342 } else if ( !dni.renamed ) {
3343 Debug( LDAP_DEBUG_SYNC,
3344 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
3345 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
3347 slap_graduate_commit_csn( op );
3352 case LDAP_SYNC_DELETE :
3353 if ( !BER_BVISNULL( &dni.dn ) ) {
3354 SlapReply rs_delete = {REP_RESULT};
3355 op->o_req_dn = dni.dn;
3356 op->o_req_ndn = dni.ndn;
3357 op->o_tag = LDAP_REQ_DELETE;
3358 op->o_bd = si->si_wbe;
3360 slap_queue_csn( op, si->si_syncCookie.ctxcsn );
3362 rc = op->o_bd->be_delete( op, &rs_delete );
3363 Debug( LDAP_DEBUG_SYNC,
3364 "syncrepl_entry: %s be_delete %s (%d)\n",
3365 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3366 if ( rc == LDAP_NO_SUCH_OBJECT )
3369 while ( rs_delete.sr_err == LDAP_SUCCESS
3370 && op->o_delete_glue_parent ) {
3371 op->o_delete_glue_parent = 0;
3372 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3373 slap_callback cb = { NULL };
3374 cb.sc_response = slap_null_cb;
3375 dnParent( &op->o_req_ndn, &pdn );
3377 op->o_req_ndn = pdn;
3378 op->o_callback = &cb;
3379 rs_reinit( &rs_delete, REP_RESULT );
3380 op->o_bd->be_delete( op, &rs_delete );
3391 Debug( LDAP_DEBUG_ANY,
3392 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
3397 slap_sl_free( syncUUID[1].bv_val, op->o_tmpmemctx );
3398 BER_BVZERO( &syncUUID[1] );
3399 if ( !BER_BVISNULL( &dni.ndn ) ) {
3400 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
3402 if ( !BER_BVISNULL( &dni.dn ) ) {
3403 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
3406 entry_free( entry );
3409 slap_graduate_commit_csn( op );
3411 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
3412 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3414 BER_BVZERO( &op->o_csn );
3418 static struct berval gcbva[] = {
3424 #define NP_DELETE_ONE 2
3427 syncrepl_del_nonpresent(
3431 struct sync_cookie *sc,
3434 Backend* be = op->o_bd;
3435 slap_callback cb = { NULL };
3436 struct nonpresent_entry *np_list, *np_prev;
3438 AttributeName an[2];
3440 struct berval pdn = BER_BVNULL;
3443 #ifdef ENABLE_REWRITE
3444 if ( si->si_rewrite ) {
3445 op->o_req_dn = si->si_suffixm;
3446 op->o_req_ndn = si->si_suffixm;
3450 op->o_req_dn = si->si_base;
3451 op->o_req_ndn = si->si_base;
3454 cb.sc_response = nonpresent_callback;
3457 op->o_callback = &cb;
3458 op->o_tag = LDAP_REQ_SEARCH;
3459 op->ors_scope = si->si_scope;
3460 op->ors_deref = LDAP_DEREF_NEVER;
3461 op->o_time = slap_get_time();
3462 op->ors_tlimit = SLAP_NO_LIMIT;
3467 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
3470 op->ors_attrsonly = 1;
3471 op->ors_attrs = slap_anlist_no_attrs;
3472 op->ors_limit = NULL;
3473 op->ors_filter = &uf;
3476 uf.f_av_desc = slap_schema.si_ad_entryUUID;
3478 uf.f_choice = LDAP_FILTER_EQUALITY;
3479 si->si_refreshDelete |= NP_DELETE_ONE;
3481 for (i=0; uuids[i].bv_val; i++) {
3482 SlapReply rs_search = {REP_RESULT};
3485 uf.f_av_value = uuids[i];
3486 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3487 rc = be->be_search( op, &rs_search );
3488 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3490 si->si_refreshDelete ^= NP_DELETE_ONE;
3494 AttributeAssertion mmaa;
3495 SlapReply rs_search = {REP_RESULT};
3497 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
3498 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
3499 an[0].an_desc = slap_schema.si_ad_entryUUID;
3501 op->ors_slimit = SLAP_NO_LIMIT;
3502 op->ors_tlimit = SLAP_NO_LIMIT;
3503 op->ors_limit = NULL;
3504 op->ors_attrsonly = 0;
3505 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
3506 /* In multimaster, updates can continue to arrive while
3507 * we're searching. Limit the search result to entries
3508 * older than our newest cookie CSN.
3510 if ( SLAP_MULTIMASTER( op->o_bd )) {
3515 f->f_choice = LDAP_FILTER_AND;
3516 f->f_next = op->ors_filter;
3520 f->f_choice = LDAP_FILTER_LE;
3522 f->f_av_desc = slap_schema.si_ad_entryCSN;
3524 BER_BVZERO( &f->f_av_value );
3525 for ( i=0; i<sc->numcsns; i++ ) {
3526 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
3527 f->f_av_value = sc->ctxcsn[i];
3529 of = op->ors_filter;
3530 op->ors_filter = mmf;
3531 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3534 op->ors_filterstr = si->si_filterstr;
3536 op->o_nocaching = 1;
3539 rc = be->be_search( op, &rs_search );
3540 if ( SLAP_MULTIMASTER( op->o_bd )) {
3541 op->ors_filter = of;
3543 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
3544 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
3545 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3550 op->o_nocaching = 0;
3552 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
3554 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
3555 csn = sc->ctxcsn[m];
3557 csn = si->si_syncCookie.ctxcsn[0];
3560 op->o_bd = si->si_wbe;
3561 slap_queue_csn( op, &csn );
3563 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
3564 while ( np_list != NULL ) {
3565 SlapReply rs_delete = {REP_RESULT};
3567 LDAP_LIST_REMOVE( np_list, npe_link );
3569 np_list = LDAP_LIST_NEXT( np_list, npe_link );
3570 op->o_tag = LDAP_REQ_DELETE;
3571 op->o_callback = &cb;
3572 cb.sc_response = null_callback;
3574 op->o_req_dn = *np_prev->npe_name;
3575 op->o_req_ndn = *np_prev->npe_nname;
3576 rc = op->o_bd->be_delete( op, &rs_delete );
3577 Debug( LDAP_DEBUG_SYNC,
3578 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
3579 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3581 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
3582 SlapReply rs_modify = {REP_RESULT};
3583 Modifications mod1, mod2;
3584 mod1.sml_op = LDAP_MOD_REPLACE;
3586 mod1.sml_desc = slap_schema.si_ad_objectClass;
3587 mod1.sml_type = mod1.sml_desc->ad_cname;
3588 mod1.sml_numvals = 2;
3589 mod1.sml_values = &gcbva[0];
3590 mod1.sml_nvalues = NULL;
3591 mod1.sml_next = &mod2;
3593 mod2.sml_op = LDAP_MOD_REPLACE;
3595 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
3596 mod2.sml_type = mod2.sml_desc->ad_cname;
3597 mod2.sml_numvals = 1;
3598 mod2.sml_values = &gcbva[1];
3599 mod2.sml_nvalues = NULL;
3600 mod2.sml_next = NULL;
3602 op->o_tag = LDAP_REQ_MODIFY;
3603 op->orm_modlist = &mod1;
3605 rc = op->o_bd->be_modify( op, &rs_modify );
3606 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
3609 while ( rs_delete.sr_err == LDAP_SUCCESS &&
3610 op->o_delete_glue_parent ) {
3611 op->o_delete_glue_parent = 0;
3612 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3613 slap_callback cb = { NULL };
3614 cb.sc_response = slap_null_cb;
3615 dnParent( &op->o_req_ndn, &pdn );
3617 op->o_req_ndn = pdn;
3618 op->o_callback = &cb;
3619 rs_reinit( &rs_delete, REP_RESULT );
3620 /* give it a root privil ? */
3621 op->o_bd->be_delete( op, &rs_delete );
3627 op->o_delete_glue_parent = 0;
3629 ber_bvfree( np_prev->npe_name );
3630 ber_bvfree( np_prev->npe_nname );
3633 if ( slapd_shutdown ) {
3638 slap_graduate_commit_csn( op );
3641 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3642 BER_BVZERO( &op->o_csn );
3649 syncrepl_add_glue_ancestors(
3653 Backend *be = op->o_bd;
3654 slap_callback cb = { NULL };
3656 int rc = LDAP_SUCCESS;
3659 struct berval dn = BER_BVNULL;
3660 struct berval ndn = BER_BVNULL;
3662 struct berval ptr, nptr;
3665 op->o_tag = LDAP_REQ_ADD;
3666 op->o_callback = &cb;
3667 cb.sc_response = null_callback;
3668 cb.sc_private = NULL;
3673 /* count RDNs in suffix */
3674 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3675 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3677 ptr.bv_len -= comma - ptr.bv_val;
3687 /* Start with BE suffix */
3689 for ( i = 0; i < suffrdns; i++ ) {
3690 comma = ber_bvrchr( &ptr, ',' );
3691 if ( comma != NULL ) {
3692 ptr.bv_len = comma - ptr.bv_val;
3699 if ( !BER_BVISEMPTY( &ptr ) ) {
3700 dn.bv_len -= ptr.bv_len + ( suffrdns != 0 );
3701 dn.bv_val += ptr.bv_len + ( suffrdns != 0 );
3704 /* the normalizedDNs are always the same length, no counting
3708 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3709 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3710 ndn.bv_len = be->be_nsuffix[0].bv_len;
3712 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3718 while ( ndn.bv_val > e->e_nname.bv_val ) {
3719 SlapReply rs_add = {REP_RESULT};
3721 glue = entry_alloc();
3722 ber_dupbv( &glue->e_name, &dn );
3723 ber_dupbv( &glue->e_nname, &ndn );
3725 a = attr_alloc( slap_schema.si_ad_objectClass );
3728 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3729 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3730 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3731 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3733 a->a_nvals = a->a_vals;
3735 a->a_next = glue->e_attrs;
3738 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3741 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3742 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3743 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3745 a->a_nvals = a->a_vals;
3747 a->a_next = glue->e_attrs;
3750 op->o_req_dn = glue->e_name;
3751 op->o_req_ndn = glue->e_nname;
3753 rc = be->be_add ( op, &rs_add );
3754 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3755 if ( op->ora_e == glue )
3756 be_entry_release_w( op, glue );
3758 /* incl. ALREADY EXIST */
3760 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3766 /* Move to next child */
3767 comma = ber_bvrchr( &ptr, ',' );
3768 if ( comma == NULL ) {
3771 ptr.bv_len = comma - ptr.bv_val;
3773 dn.bv_val = ++comma;
3774 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3776 comma = ber_bvrchr( &nptr, ',' );
3777 assert( comma != NULL );
3778 nptr.bv_len = comma - nptr.bv_val;
3780 ndn.bv_val = ++comma;
3781 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3792 slap_callback cb = { NULL };
3794 Backend *be = op->o_bd;
3795 SlapReply rs_add = {REP_RESULT};
3797 rc = syncrepl_add_glue_ancestors( op, e );
3800 case LDAP_ALREADY_EXISTS:
3807 op->o_tag = LDAP_REQ_ADD;
3808 op->o_callback = &cb;
3809 cb.sc_response = null_callback;
3810 cb.sc_private = NULL;
3812 op->o_req_dn = e->e_name;
3813 op->o_req_ndn = e->e_nname;
3815 rc = be->be_add ( op, &rs_add );
3816 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3817 if ( op->ora_e == e )
3818 be_entry_release_w( op, e );
3827 syncrepl_updateCookie(
3830 struct sync_cookie *syncCookie )
3832 Backend *be = op->o_bd;
3834 struct berval first = BER_BVNULL;
3835 struct sync_cookie sc;
3837 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3840 int rc, i, j, changed = 0;
3843 slap_callback cb = { NULL };
3844 SlapReply rs_modify = {REP_RESULT};
3846 mod.sml_op = LDAP_MOD_REPLACE;
3847 mod.sml_desc = slap_schema.si_ad_contextCSN;
3848 mod.sml_type = mod.sml_desc->ad_cname;
3849 mod.sml_flags = SLAP_MOD_INTERNAL;
3850 mod.sml_nvalues = NULL;
3851 mod.sml_next = NULL;
3853 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3856 for ( i=0; i<syncCookie->numcsns; i++ ) {
3857 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3859 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3860 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3864 /* clone the cookieState CSNs so we can Replace the whole thing */
3865 sc.numcsns = si->si_cookieState->cs_num;
3867 ber_bvarray_dup_x( &sc.ctxcsn, si->si_cookieState->cs_vals, NULL );
3868 sc.sids = ch_malloc( sc.numcsns * sizeof(int));
3869 for ( i=0; i<sc.numcsns; i++ )
3870 sc.sids[i] = si->si_cookieState->cs_sids[i];
3876 /* find any CSNs in the syncCookie that are newer than the cookieState */
3877 for ( i=0; i<syncCookie->numcsns; i++ ) {
3878 for ( j=0; j<sc.numcsns; j++ ) {
3879 if ( syncCookie->sids[i] < sc.sids[j] )
3881 if ( syncCookie->sids[i] != sc.sids[j] )
3883 len = syncCookie->ctxcsn[i].bv_len;
3884 if ( len > sc.ctxcsn[j].bv_len )
3885 len = sc.ctxcsn[j].bv_len;
3886 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3887 sc.ctxcsn[j].bv_val, len ) > 0 ) {
3888 ber_bvreplace( &sc.ctxcsn[j], &syncCookie->ctxcsn[i] );
3890 if ( BER_BVISNULL( &first ) ||
3891 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3892 first = syncCookie->ctxcsn[i];
3897 /* there was no match for this SID, it's a new CSN */
3898 if ( j == sc.numcsns ||
3899 syncCookie->sids[i] != sc.sids[j] ) {
3900 slap_insert_csn_sids( &sc, j, syncCookie->sids[i],
3901 &syncCookie->ctxcsn[i] );
3902 if ( BER_BVISNULL( &first ) ||
3903 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3904 first = syncCookie->ctxcsn[i];
3909 /* Should never happen, ITS#5065 */
3910 if ( BER_BVISNULL( &first ) || !changed ) {
3911 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3912 ber_bvarray_free( sc.ctxcsn );
3916 op->o_bd = si->si_wbe;
3917 slap_queue_csn( op, &first );
3919 op->o_tag = LDAP_REQ_MODIFY;
3921 cb.sc_response = null_callback;
3924 op->o_callback = &cb;
3925 op->o_req_dn = si->si_contextdn;
3926 op->o_req_ndn = si->si_contextdn;
3928 /* update contextCSN */
3929 op->o_dont_replicate = 1;
3931 mod.sml_numvals = sc.numcsns;
3932 mod.sml_values = sc.ctxcsn;
3934 op->orm_modlist = &mod;
3935 op->orm_no_opattrs = 1;
3936 rc = op->o_bd->be_modify( op, &rs_modify );
3938 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3939 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3941 char txtbuf[SLAP_TEXT_BUFLEN];
3942 size_t textlen = sizeof txtbuf;
3943 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3944 rs_reinit( &rs_modify, REP_RESULT );
3945 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3947 rc = op->o_bd->be_add( op, &rs_modify );
3948 if ( e == op->ora_e )
3949 be_entry_release_w( op, op->ora_e );
3952 op->orm_no_opattrs = 0;
3953 op->o_dont_replicate = 0;
3955 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3956 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3957 ber_bvarray_free( si->si_cookieState->cs_vals );
3958 ch_free( si->si_cookieState->cs_sids );
3959 si->si_cookieState->cs_vals = sc.ctxcsn;
3960 si->si_cookieState->cs_sids = sc.sids;
3961 si->si_cookieState->cs_num = sc.numcsns;
3963 /* Don't just dup the provider's cookie, recreate it */
3964 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
3965 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn, si->si_cookieState->cs_vals, NULL );
3966 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num * sizeof(int) );
3967 for ( i=0; i<si->si_cookieState->cs_num; i++ )
3968 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
3970 si->si_cookieState->cs_age++;
3971 si->si_cookieAge = si->si_cookieState->cs_age;
3973 Debug( LDAP_DEBUG_ANY,
3974 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3975 si->si_ridtxt, rs_modify.sr_err, 0 );
3977 ber_bvarray_free( sc.ctxcsn );
3981 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3982 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3986 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3989 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3990 BER_BVZERO( &op->o_csn );
3991 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3996 /* Compare the attribute from the old entry to the one in the new
3997 * entry. The Modifications from the new entry will either be left
3998 * in place, or changed to an Add or Delete as needed.
4001 attr_cmp( Operation *op, Attribute *old, Attribute *new,
4002 Modifications ***mret, Modifications ***mcur )
4005 Modifications *mod, **modtail;
4011 struct berval **adds, **dels;
4012 /* count old and new */
4013 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
4014 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
4016 /* there MUST be both old and new values */
4021 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
4022 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
4024 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
4025 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
4029 for ( i=0; i<o; i++ ) {
4030 for ( j=0; j<n; j++ ) {
4033 if ( bvmatch( dels[i], adds[j] ) ) {
4043 /* Don't delete/add an objectClass, always use the replace op.
4044 * Modify would fail if provider has replaced entry with a new,
4045 * and the new explicitly includes a superior of a class that was
4046 * only included implicitly in the old entry. Ref ITS#5517.
4048 * Also use replace op if attr has no equality matching rule.
4051 if ( ( nn || ( no > 0 && no < o ) ) &&
4052 ( old->a_desc == slap_schema.si_ad_objectClass ||
4053 !old->a_desc->ad_type->sat_equality ) )
4059 /* all old values were deleted, just use the replace op */
4063 /* delete some values */
4064 mod = ch_malloc( sizeof( Modifications ) );
4065 mod->sml_op = LDAP_MOD_DELETE;
4067 mod->sml_desc = old->a_desc;
4068 mod->sml_type = mod->sml_desc->ad_cname;
4069 mod->sml_numvals = no;
4070 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4071 if ( old->a_vals != old->a_nvals ) {
4072 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4074 mod->sml_nvalues = NULL;
4077 for ( i = 0; i < o; i++ ) {
4078 if ( !dels[i] ) continue;
4079 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
4080 if ( mod->sml_nvalues ) {
4081 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
4085 BER_BVZERO( &mod->sml_values[j] );
4086 if ( mod->sml_nvalues ) {
4087 BER_BVZERO( &mod->sml_nvalues[j] );
4090 modtail = &mod->sml_next;
4093 op->o_tmpfree( dels, op->o_tmpmemctx );
4094 /* some values were added */
4095 if ( nn && no < o ) {
4096 mod = ch_malloc( sizeof( Modifications ) );
4097 mod->sml_op = LDAP_MOD_ADD;
4099 mod->sml_desc = old->a_desc;
4100 mod->sml_type = mod->sml_desc->ad_cname;
4101 mod->sml_numvals = nn;
4102 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4103 if ( old->a_vals != old->a_nvals ) {
4104 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4106 mod->sml_nvalues = NULL;
4109 for ( i = 0; i < n; i++ ) {
4110 if ( !adds[i] ) continue;
4111 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
4112 if ( mod->sml_nvalues ) {
4113 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
4117 BER_BVZERO( &mod->sml_values[j] );
4118 if ( mod->sml_nvalues ) {
4119 BER_BVZERO( &mod->sml_nvalues[j] );
4122 modtail = &mod->sml_next;
4125 op->o_tmpfree( adds, op->o_tmpmemctx );
4127 /* new attr, just use the new mod */
4131 /* advance to next element */
4135 **mcur = mod->sml_next;
4137 modtail = &mod->sml_next;
4139 *mcur = &mod->sml_next;
4145 /* Generate a set of modifications to change the old entry into the
4146 * new one. On input ml is a list of modifications equivalent to
4147 * the new entry. It will be massaged and the result will be stored
4150 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
4151 Modifications **mods, Modifications **ml, int is_ctx)
4153 Modifications **modtail = mods;
4155 /* We assume that attributes are saved in the same order
4156 * in the remote and local databases. So if we walk through
4157 * the attributeDescriptions one by one they should match in
4158 * lock step. If not, look for an add or delete.
4160 while ( old && new )
4162 /* If we've seen this before, use its mod now */
4163 if ( new->a_flags & SLAP_ATTR_IXADD ) {
4164 attr_cmp( op, NULL, new, &modtail, &ml );
4168 /* Skip contextCSN */
4169 if ( is_ctx && old->a_desc ==
4170 slap_schema.si_ad_contextCSN ) {
4175 if ( old->a_desc != new->a_desc ) {
4179 /* If it's just been re-added later,
4180 * remember that we've seen it.
4182 tmp = attr_find( new, old->a_desc );
4184 tmp->a_flags |= SLAP_ATTR_IXADD;
4186 /* If it's a new attribute, pull it in.
4188 tmp = attr_find( old, new->a_desc );
4190 attr_cmp( op, NULL, new, &modtail, &ml );
4194 /* Delete old attr */
4195 mod = ch_malloc( sizeof( Modifications ) );
4196 mod->sml_op = LDAP_MOD_DELETE;
4198 mod->sml_desc = old->a_desc;
4199 mod->sml_type = mod->sml_desc->ad_cname;
4200 mod->sml_numvals = 0;
4201 mod->sml_values = NULL;
4202 mod->sml_nvalues = NULL;
4204 modtail = &mod->sml_next;
4209 /* kludge - always update modifiersName so that it
4210 * stays co-located with the other mod opattrs. But only
4211 * if we know there are other valid mods.
4213 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
4214 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
4215 attr_cmp( op, NULL, new, &modtail, &ml );
4217 attr_cmp( op, old, new, &modtail, &ml );
4230 dninfo *dni = op->o_callback->sc_private;
4232 if ( rs->sr_type == REP_SEARCH ) {
4233 if ( !BER_BVISNULL( &dni->dn ) ) {
4234 Debug( LDAP_DEBUG_ANY,
4235 "dn_callback : consistency error - "
4236 "entryUUID is not unique\n", 0, 0, 0 );
4238 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
4239 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
4240 /* If there is a new entry, see if it differs from the old.
4241 * We compare the non-normalized values so that cosmetic changes
4242 * in the provider are always propagated.
4244 if ( dni->new_entry ) {
4245 Attribute *old, *new;
4246 struct berval old_rdn, new_rdn;
4247 struct berval old_p, new_p;
4248 int is_ctx, new_sup = 0;
4250 /* If old entry is not a glue entry, make sure new entry
4251 * is actually newer than old entry
4253 if ( !is_entry_glue( rs->sr_entry )) {
4254 old = attr_find( rs->sr_entry->e_attrs,
4255 slap_schema.si_ad_entryCSN );
4256 new = attr_find( dni->new_entry->e_attrs,
4257 slap_schema.si_ad_entryCSN );
4260 ber_len_t len = old->a_vals[0].bv_len;
4261 if ( len > new->a_vals[0].bv_len )
4262 len = new->a_vals[0].bv_len;
4263 rc = memcmp( old->a_vals[0].bv_val,
4264 new->a_vals[0].bv_val, len );
4266 Debug( LDAP_DEBUG_SYNC,
4267 "dn_callback : new entry is older than ours "
4268 "%s ours %s, new %s\n",
4269 rs->sr_entry->e_name.bv_val,
4270 old->a_vals[0].bv_val,
4271 new->a_vals[0].bv_val );
4272 return LDAP_SUCCESS;
4273 } else if ( rc == 0 ) {
4274 Debug( LDAP_DEBUG_SYNC,
4275 "dn_callback : entries have identical CSN "
4277 rs->sr_entry->e_name.bv_val,
4278 old->a_vals[0].bv_val, 0 );
4279 return LDAP_SUCCESS;
4284 is_ctx = dn_match( &rs->sr_entry->e_nname,
4285 &op->o_bd->be_nsuffix[0] );
4287 /* Did the DN change?
4288 * case changes in the parent are ignored,
4289 * we only want to know if the RDN was
4292 dnRdn( &rs->sr_entry->e_name, &old_rdn );
4293 dnRdn( &dni->new_entry->e_name, &new_rdn );
4294 dnParent( &rs->sr_entry->e_nname, &old_p );
4295 dnParent( &dni->new_entry->e_nname, &new_p );
4297 new_sup = !dn_match( &old_p, &new_p );
4298 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
4300 struct berval oldRDN, oldVal;
4301 AttributeDescription *ad = NULL;
4307 dni->nnewSup = new_p;
4309 /* See if the oldRDN was deleted */
4310 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
4311 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
4312 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
4314 oldRDN.bv_len -= oldVal.bv_len + 1;
4315 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4317 for ( oldpos=0, a=rs->sr_entry->e_attrs;
4318 a && a->a_desc != ad; oldpos++, a=a->a_next );
4319 /* a should not be NULL but apparently it happens.
4322 dni->oldNcount = a ? a->a_numvals : 0;
4323 for ( newpos=0, a=dni->new_entry->e_attrs;
4324 a && a->a_desc != ad; newpos++, a=a->a_next );
4325 if ( !a || oldpos != newpos || attr_valfind( a,
4326 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
4327 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
4328 SLAP_MR_VALUE_OF_SYNTAX,
4329 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
4333 /* Get the newRDN's desc */
4334 dnRdn( &dni->new_entry->e_nname, &oldRDN );
4335 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
4336 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
4338 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4341 /* A ModDN has happened, but in Refresh mode other
4342 * changes may have occurred before we picked it up.
4343 * So fallthru to regular Modify processing.
4347 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
4348 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
4352 } else if ( rs->sr_type == REP_RESULT ) {
4353 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
4354 Debug( LDAP_DEBUG_ANY,
4355 "dn_callback : consistency error - "
4356 "entryUUID is not unique\n", 0, 0, 0 );
4360 return LDAP_SUCCESS;
4364 nonpresent_callback(
4368 syncinfo_t *si = op->o_callback->sc_private;
4371 char *present_uuid = NULL;
4372 struct nonpresent_entry *np_entry;
4374 if ( rs->sr_type == REP_RESULT ) {
4375 count = presentlist_free( si->si_presentlist );
4376 si->si_presentlist = NULL;
4378 } else if ( rs->sr_type == REP_SEARCH ) {
4379 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
4380 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
4383 present_uuid = presentlist_find( si->si_presentlist, &a->a_nvals[0] );
4386 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
4387 char buf[sizeof("rid=999 non")];
4389 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
4390 present_uuid ? "" : "non" );
4392 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
4393 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
4396 if ( a == NULL ) return 0;
4399 if ( present_uuid == NULL ) {
4400 np_entry = (struct nonpresent_entry *)
4401 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
4402 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
4403 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
4404 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
4407 presentlist_delete( &si->si_presentlist, &a->a_nvals[0] );
4408 ch_free( present_uuid );
4411 return LDAP_SUCCESS;
4419 if ( rs->sr_err != LDAP_SUCCESS &&
4420 rs->sr_err != LDAP_REFERRAL &&
4421 rs->sr_err != LDAP_ALREADY_EXISTS &&
4422 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
4423 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
4425 Debug( LDAP_DEBUG_ANY,
4426 "null_callback : error code 0x%x\n",
4429 return LDAP_SUCCESS;
4432 static struct berval *
4433 slap_uuidstr_from_normalized(
4434 struct berval* uuidstr,
4435 struct berval* normalized,
4440 unsigned char nibble;
4443 if ( normalized == NULL ) return NULL;
4444 if ( normalized->bv_len != 16 ) return NULL;
4449 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4450 if ( new == NULL ) {
4457 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4458 if ( new != uuidstr ) {
4459 slap_sl_free( new, ctx );
4464 for ( i = 0; i < 16; i++ ) {
4465 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
4466 new->bv_val[(i<<1)+d] = '-';
4470 nibble = (normalized->bv_val[i] >> 4) & 0xF;
4471 if ( nibble < 10 ) {
4472 new->bv_val[(i<<1)+d] = nibble + '0';
4474 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
4477 nibble = (normalized->bv_val[i]) & 0xF;
4478 if ( nibble < 10 ) {
4479 new->bv_val[(i<<1)+d+1] = nibble + '0';
4481 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
4485 new->bv_val[new->bv_len] = '\0';
4492 if ( normalized == NULL ) return NULL;
4493 if ( normalized->bv_len != 16 ) return NULL;
4499 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4500 if ( new == NULL ) {
4507 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4512 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
4513 normalized->bv_len, new->bv_val, new->bv_len + 1 );
4517 if ( new != NULL ) {
4518 if ( new->bv_val != NULL ) {
4519 slap_sl_free( new->bv_val, ctx );
4522 if ( new != uuidstr ) {
4523 slap_sl_free( new, ctx );
4536 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
4539 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN-2 ));
4541 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN ));
4546 syncinfo_free( syncinfo_t *sie, int free_all )
4548 syncinfo_t *si_next;
4550 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
4551 sie->si_ridtxt, 0, 0 );
4554 si_next = sie->si_next;
4557 if ( sie->si_conn ) {
4558 connection_client_stop( sie->si_conn );
4559 sie->si_conn = NULL;
4561 ldap_unbind_ext( sie->si_ld, NULL, NULL );
4565 struct re_s *re = sie->si_re;
4568 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4569 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
4570 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
4571 ldap_pvt_runqueue_remove( &slapd_rq, re );
4572 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4575 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
4577 bindconf_free( &sie->si_bindconf );
4579 if ( sie->si_filterstr.bv_val ) {
4580 ch_free( sie->si_filterstr.bv_val );
4582 if ( sie->si_filter ) {
4583 filter_free( sie->si_filter );
4585 if ( sie->si_logfilterstr.bv_val ) {
4586 ch_free( sie->si_logfilterstr.bv_val );
4588 if ( sie->si_base.bv_val ) {
4589 ch_free( sie->si_base.bv_val );
4591 if ( sie->si_logbase.bv_val ) {
4592 ch_free( sie->si_logbase.bv_val );
4594 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
4595 ch_free( sie->si_contextdn.bv_val );
4597 if ( sie->si_attrs ) {
4599 while ( sie->si_attrs[i] != NULL ) {
4600 ch_free( sie->si_attrs[i] );
4603 ch_free( sie->si_attrs );
4605 if ( sie->si_exattrs ) {
4607 while ( sie->si_exattrs[i] != NULL ) {
4608 ch_free( sie->si_exattrs[i] );
4611 ch_free( sie->si_exattrs );
4613 if ( sie->si_anlist ) {
4615 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
4616 ch_free( sie->si_anlist[i].an_name.bv_val );
4619 ch_free( sie->si_anlist );
4621 if ( sie->si_exanlist ) {
4623 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
4624 ch_free( sie->si_exanlist[i].an_name.bv_val );
4627 ch_free( sie->si_exanlist );
4629 if ( sie->si_retryinterval ) {
4630 ch_free( sie->si_retryinterval );
4632 if ( sie->si_retrynum ) {
4633 ch_free( sie->si_retrynum );
4635 if ( sie->si_retrynum_init ) {
4636 ch_free( sie->si_retrynum_init );
4638 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
4639 if ( sie->si_presentlist ) {
4640 presentlist_free( sie->si_presentlist );
4642 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
4643 struct nonpresent_entry* npe;
4644 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
4645 LDAP_LIST_REMOVE( npe, npe_link );
4646 if ( npe->npe_name ) {
4647 if ( npe->npe_name->bv_val ) {
4648 ch_free( npe->npe_name->bv_val );
4650 ch_free( npe->npe_name );
4652 if ( npe->npe_nname ) {
4653 if ( npe->npe_nname->bv_val ) {
4654 ch_free( npe->npe_nname->bv_val );
4656 ch_free( npe->npe_nname );
4660 if ( sie->si_cookieState ) {
4661 sie->si_cookieState->cs_ref--;
4662 if ( !sie->si_cookieState->cs_ref ) {
4663 ch_free( sie->si_cookieState->cs_sids );
4664 ber_bvarray_free( sie->si_cookieState->cs_vals );
4665 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
4666 ch_free( sie->si_cookieState->cs_psids );
4667 ber_bvarray_free( sie->si_cookieState->cs_pvals );
4668 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
4669 ch_free( sie->si_cookieState );
4672 #ifdef ENABLE_REWRITE
4673 if ( sie->si_rewrite )
4674 rewrite_info_delete( &sie->si_rewrite );
4675 if ( sie->si_suffixm.bv_val )
4676 ch_free( sie->si_suffixm.bv_val );
4680 } while ( free_all && si_next );
4683 #ifdef ENABLE_REWRITE
4685 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4687 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4688 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4689 char *argvRule[] = { "rewriteRule", NULL, NULL, ":", NULL };
4693 if ( si->si_rewrite )
4694 rewrite_info_delete( &si->si_rewrite );
4695 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4697 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4698 if ( rc != LDAP_SUCCESS )
4701 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4702 if ( rc != LDAP_SUCCESS )
4705 vnc = ch_malloc( si->si_base.bv_len + 6 );
4706 strcpy( vnc, "(.*)" );
4707 lutil_strcopy( lutil_strcopy( vnc+4, si->si_base.bv_val ), "$" );
4710 rnc = ch_malloc( si->si_suffixm.bv_len + 3 );
4711 strcpy( rnc, "%1" );
4712 strcpy( rnc+2, si->si_suffixm.bv_val );
4715 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4722 /* NOTE: used & documented in slapd.conf(5) */
4724 #define PROVIDERSTR "provider"
4725 #define SCHEMASTR "schemachecking"
4726 #define FILTERSTR "filter"
4727 #define SEARCHBASESTR "searchbase"
4728 #define SCOPESTR "scope"
4729 #define ATTRSONLYSTR "attrsonly"
4730 #define ATTRSSTR "attrs"
4731 #define TYPESTR "type"
4732 #define INTERVALSTR "interval"
4733 #define RETRYSTR "retry"
4734 #define SLIMITSTR "sizelimit"
4735 #define TLIMITSTR "timelimit"
4736 #define SYNCDATASTR "syncdata"
4737 #define LOGBASESTR "logbase"
4738 #define LOGFILTERSTR "logfilter"
4739 #define SUFFIXMSTR "suffixmassage"
4740 #define STRICT_REFRESH "strictrefresh"
4741 #define LAZY_COMMIT "lazycommit"
4743 /* FIXME: undocumented */
4744 #define EXATTRSSTR "exattrs"
4745 #define MANAGEDSAITSTR "manageDSAit"
4749 GOT_RID = 0x00000001U,
4750 GOT_PROVIDER = 0x00000002U,
4751 GOT_SCHEMACHECKING = 0x00000004U,
4752 GOT_FILTER = 0x00000008U,
4753 GOT_SEARCHBASE = 0x00000010U,
4754 GOT_SCOPE = 0x00000020U,
4755 GOT_ATTRSONLY = 0x00000040U,
4756 GOT_ATTRS = 0x00000080U,
4757 GOT_TYPE = 0x00000100U,
4758 GOT_INTERVAL = 0x00000200U,
4759 GOT_RETRY = 0x00000400U,
4760 GOT_SLIMIT = 0x00000800U,
4761 GOT_TLIMIT = 0x00001000U,
4762 GOT_SYNCDATA = 0x00002000U,
4763 GOT_LOGBASE = 0x00004000U,
4764 GOT_LOGFILTER = 0x00008000U,
4765 GOT_EXATTRS = 0x00010000U,
4766 GOT_MANAGEDSAIT = 0x00020000U,
4767 GOT_BINDCONF = 0x00040000U,
4768 GOT_SUFFIXM = 0x00080000U,
4771 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4774 static slap_verbmasks datamodes[] = {
4775 { BER_BVC("default"), SYNCDATA_DEFAULT },
4776 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4777 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4782 parse_syncrepl_retry(
4789 int use_default = 0;
4791 char *val = arg + STRLENOF( RETRYSTR "=" );
4792 if ( strcasecmp( val, "undefined" ) == 0 ) {
4797 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4798 retry_list[0] = NULL;
4800 slap_str2clist( &retry_list, val, " ,\t" );
4802 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4805 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4806 "Error: incomplete syncrepl retry list" );
4807 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4808 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4809 ch_free( retry_list[k] );
4811 ch_free( retry_list );
4814 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4815 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4816 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4817 for ( j = 0; j < n; j++ ) {
4819 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4820 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4821 "Error: invalid retry interval \"%s\" (#%d)",
4822 retry_list[j*2], j );
4823 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4824 /* do some cleanup */
4827 si->si_retryinterval[j] = (time_t)t;
4828 if ( *retry_list[j*2+1] == '+' ) {
4829 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4830 si->si_retrynum[j] = RETRYNUM_FOREVER;
4834 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4835 || si->si_retrynum_init[j] <= 0 )
4837 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4838 "Error: invalid initial retry number \"%s\" (#%d)",
4839 retry_list[j*2+1], j );
4840 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4841 /* do some cleanup */
4844 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4845 || si->si_retrynum[j] <= 0 )
4847 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4848 "Error: invalid retry number \"%s\" (#%d)",
4849 retry_list[j*2+1], j );
4850 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4851 /* do some cleanup */
4856 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4857 Debug( LDAP_DEBUG_CONFIG,
4858 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4862 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4863 si->si_retrynum[j] = RETRYNUM_TAIL;
4864 si->si_retryinterval[j] = 0;
4866 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4867 ch_free( retry_list[k] );
4869 ch_free( retry_list );
4870 if ( !use_default ) {
4871 si->si_got |= GOT_RETRY;
4878 parse_syncrepl_line(
4885 for ( i = 1; i < c->argc; i++ ) {
4886 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4887 STRLENOF( IDSTR "=" ) ) )
4890 /* '\0' string terminator accounts for '=' */
4891 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4892 if ( lutil_atoi( &tmp, val ) != 0 ) {
4893 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4894 "Error: parse_syncrepl_line: "
4895 "unable to parse syncrepl id \"%s\"", val );
4896 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4899 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4900 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4901 "Error: parse_syncrepl_line: "
4902 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4903 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4907 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4908 si->si_got |= GOT_RID;
4909 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4910 STRLENOF( PROVIDERSTR "=" ) ) )
4912 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4913 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4915 if ( ldap_is_ldaps_url( val ))
4916 si->si_bindconf.sb_tls_do_init = 1;
4918 si->si_got |= GOT_PROVIDER;
4919 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4920 STRLENOF( SCHEMASTR "=" ) ) )
4922 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4923 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4924 si->si_schemachecking = 1;
4925 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4926 si->si_schemachecking = 0;
4928 si->si_schemachecking = 1;
4930 si->si_got |= GOT_SCHEMACHECKING;
4931 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4932 STRLENOF( FILTERSTR "=" ) ) )
4934 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4935 if ( si->si_filterstr.bv_val )
4936 ch_free( si->si_filterstr.bv_val );
4937 ber_str2bv( val, 0, 1, &si->si_filterstr );
4938 si->si_got |= GOT_FILTER;
4939 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4940 STRLENOF( LOGFILTERSTR "=" ) ) )
4942 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4943 if ( si->si_logfilterstr.bv_val )
4944 ch_free( si->si_logfilterstr.bv_val );
4945 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4946 si->si_got |= GOT_LOGFILTER;
4947 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4948 STRLENOF( SEARCHBASESTR "=" ) ) )
4953 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4954 if ( si->si_base.bv_val ) {
4955 ch_free( si->si_base.bv_val );
4957 ber_str2bv( val, 0, 0, &bv );
4958 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4959 if ( rc != LDAP_SUCCESS ) {
4960 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4961 "Invalid base 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_SEARCHBASE;
4967 #ifdef ENABLE_REWRITE
4968 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
4969 STRLENOF( SUFFIXMSTR "=" ) ) )
4974 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
4975 if ( si->si_suffixm.bv_val ) {
4976 ch_free( si->si_suffixm.bv_val );
4978 ber_str2bv( val, 0, 0, &bv );
4979 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
4980 if ( rc != LDAP_SUCCESS ) {
4981 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4982 "Invalid massage DN \"%s\": %d (%s)",
4983 val, rc, ldap_err2string( rc ) );
4984 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4987 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
4988 ch_free( si->si_suffixm.bv_val );
4989 BER_BVZERO( &si->si_suffixm );
4990 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4991 "Massage DN \"%s\" is not within the database naming context",
4993 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4996 si->si_got |= GOT_SUFFIXM;
4998 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4999 STRLENOF( LOGBASESTR "=" ) ) )
5004 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
5005 if ( si->si_logbase.bv_val ) {
5006 ch_free( si->si_logbase.bv_val );
5008 ber_str2bv( val, 0, 0, &bv );
5009 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
5010 if ( rc != LDAP_SUCCESS ) {
5011 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5012 "Invalid logbase DN \"%s\": %d (%s)",
5013 val, rc, ldap_err2string( rc ) );
5014 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5017 si->si_got |= GOT_LOGBASE;
5018 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
5019 STRLENOF( SCOPESTR "=" ) ) )
5022 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
5023 j = ldap_pvt_str2scope( val );
5025 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5026 "Error: parse_syncrepl_line: "
5027 "unknown scope \"%s\"", val);
5028 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5032 si->si_got |= GOT_SCOPE;
5033 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
5034 STRLENOF( ATTRSONLYSTR ) ) )
5036 si->si_attrsonly = 1;
5037 si->si_got |= GOT_ATTRSONLY;
5038 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
5039 STRLENOF( ATTRSSTR "=" ) ) )
5041 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
5042 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
5044 attr_fname = ch_strdup( val + STRLENOF(":include:") );
5045 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
5046 if ( si->si_anlist == NULL ) {
5047 ch_free( attr_fname );
5050 si->si_anfile = attr_fname;
5052 char *str, *s, *next;
5053 const char *delimstr = " ,\t";
5054 str = ch_strdup( val );
5055 for ( s = ldap_pvt_strtok( str, delimstr, &next );
5057 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
5059 if ( strlen(s) == 1 && *s == '*' ) {
5060 si->si_allattrs = 1;
5061 val[ s - str ] = delimstr[0];
5063 if ( strlen(s) == 1 && *s == '+' ) {
5064 si->si_allopattrs = 1;
5065 val [ s - str ] = delimstr[0];
5069 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
5070 if ( si->si_anlist == NULL ) {
5074 si->si_got |= GOT_ATTRS;
5075 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
5076 STRLENOF( EXATTRSSTR "=" ) ) )
5078 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
5079 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
5081 attr_fname = ch_strdup( val + STRLENOF(":include:") );
5082 si->si_exanlist = file2anlist(
5083 si->si_exanlist, attr_fname, " ,\t" );
5084 if ( si->si_exanlist == NULL ) {
5085 ch_free( attr_fname );
5088 ch_free( attr_fname );
5090 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
5091 if ( si->si_exanlist == NULL ) {
5095 si->si_got |= GOT_EXATTRS;
5096 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
5097 STRLENOF( TYPESTR "=" ) ) )
5099 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
5100 if ( !strncasecmp( val, "refreshOnly",
5101 STRLENOF("refreshOnly") ) )
5103 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5104 } else if ( !strncasecmp( val, "refreshAndPersist",
5105 STRLENOF("refreshAndPersist") ) )
5107 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
5108 si->si_interval = 60;
5110 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5111 "Error: parse_syncrepl_line: "
5112 "unknown sync type \"%s\"", val);
5113 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5116 si->si_got |= GOT_TYPE;
5117 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
5118 STRLENOF( INTERVALSTR "=" ) ) )
5120 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
5121 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
5122 si->si_interval = 0;
5123 } else if ( strchr( val, ':' ) != NULL ) {
5124 char *next, *ptr = val;
5127 dd = strtol( ptr, &next, 10 );
5128 if ( next == ptr || next[0] != ':' || dd < 0 ) {
5129 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5130 "Error: parse_syncrepl_line: "
5131 "invalid interval \"%s\", unable to parse days", val );
5132 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5136 hh = strtol( ptr, &next, 10 );
5137 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
5138 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5139 "Error: parse_syncrepl_line: "
5140 "invalid interval \"%s\", unable to parse hours", val );
5141 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5145 mm = strtol( ptr, &next, 10 );
5146 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
5147 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5148 "Error: parse_syncrepl_line: "
5149 "invalid interval \"%s\", unable to parse minutes", val );
5150 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5154 ss = strtol( ptr, &next, 10 );
5155 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
5156 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5157 "Error: parse_syncrepl_line: "
5158 "invalid interval \"%s\", unable to parse seconds", val );
5159 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5162 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
5166 if ( lutil_parse_time( val, &t ) != 0 ) {
5167 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5168 "Error: parse_syncrepl_line: "
5169 "invalid interval \"%s\"", val );
5170 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5173 si->si_interval = (time_t)t;
5175 if ( si->si_interval < 0 ) {
5176 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5177 "Error: parse_syncrepl_line: "
5178 "invalid interval \"%ld\"",
5179 (long) si->si_interval);
5180 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5183 si->si_got |= GOT_INTERVAL;
5184 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
5185 STRLENOF( RETRYSTR "=" ) ) )
5187 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
5190 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
5191 STRLENOF( MANAGEDSAITSTR "=" ) ) )
5193 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
5194 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
5195 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
5197 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5198 "invalid manageDSAit value \"%s\".\n",
5200 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5203 si->si_got |= GOT_MANAGEDSAIT;
5204 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
5205 STRLENOF( SLIMITSTR "=") ) )
5207 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
5208 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5211 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
5212 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5213 "invalid size limit value \"%s\".\n",
5215 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5218 si->si_got |= GOT_SLIMIT;
5219 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
5220 STRLENOF( TLIMITSTR "=" ) ) )
5222 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
5223 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5226 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
5227 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5228 "invalid time limit value \"%s\".\n",
5230 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5233 si->si_got |= GOT_TLIMIT;
5234 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
5235 STRLENOF( SYNCDATASTR "=" ) ) )
5237 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
5238 si->si_syncdata = verb_to_mask( val, datamodes );
5239 si->si_got |= GOT_SYNCDATA;
5240 } else if ( !strncasecmp( c->argv[ i ], STRICT_REFRESH,
5241 STRLENOF( STRICT_REFRESH ) ) )
5243 si->si_strict_refresh = 1;
5244 } else if ( !strncasecmp( c->argv[ i ], LAZY_COMMIT,
5245 STRLENOF( LAZY_COMMIT ) ) )
5247 si->si_lazyCommit = 1;
5248 } else if ( !bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
5249 si->si_got |= GOT_BINDCONF;
5251 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5252 "Error: parse_syncrepl_line: "
5253 "unable to parse \"%s\"\n", c->argv[ i ] );
5254 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5259 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
5260 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5261 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
5262 si->si_got & GOT_RID ? "" : " "IDSTR,
5263 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
5264 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
5265 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5269 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
5270 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5271 "Base DN \"%s\" is not within the database naming context",
5272 si->si_base.bv_val );
5273 ch_free( si->si_base.bv_val );
5274 BER_BVZERO( &si->si_base );
5275 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5279 #ifdef ENABLE_REWRITE
5280 if ( si->si_got & GOT_SUFFIXM ) {
5281 if (config_suffixm( c, si )) {
5282 ch_free( si->si_suffixm.bv_val );
5283 BER_BVZERO( &si->si_suffixm );
5284 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5285 "Error configuring rewrite engine" );
5286 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5292 if ( !( si->si_got & GOT_RETRY ) ) {
5293 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
5294 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
5295 if ( si->si_retryinterval == NULL ) {
5296 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
5302 si->si_filter = str2filter( si->si_filterstr.bv_val );
5303 if ( si->si_filter == NULL ) {
5304 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
5305 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
5319 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
5320 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
5321 "operations required for syncrepl", c->be->be_type );
5322 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5325 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
5326 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
5327 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5330 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
5333 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
5337 si->si_bindconf.sb_tls = SB_TLS_OFF;
5338 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
5339 si->si_schemachecking = 0;
5340 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
5341 &si->si_filterstr );
5342 si->si_base.bv_val = NULL;
5343 si->si_scope = LDAP_SCOPE_SUBTREE;
5344 si->si_attrsonly = 0;
5345 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5346 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5347 si->si_attrs = NULL;
5348 si->si_allattrs = 0;
5349 si->si_allopattrs = 0;
5350 si->si_exattrs = NULL;
5351 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5352 si->si_interval = 86400;
5353 si->si_retryinterval = NULL;
5354 si->si_retrynum_init = NULL;
5355 si->si_retrynum = NULL;
5356 si->si_manageDSAit = 0;
5360 si->si_presentlist = NULL;
5361 LDAP_LIST_INIT( &si->si_nonpresentlist );
5362 ldap_pvt_thread_mutex_init( &si->si_mutex );
5364 rc = parse_syncrepl_line( c, si );
5369 /* Must be LDAPv3 because we need controls */
5370 switch ( si->si_bindconf.sb_version ) {
5372 /* not explicitly set */
5373 si->si_bindconf.sb_version = LDAP_VERSION3;
5376 /* explicitly set */
5379 Debug( LDAP_DEBUG_ANY,
5380 "version %d incompatible with syncrepl\n",
5381 si->si_bindconf.sb_version, 0, 0 );
5382 syncinfo_free( si, 0 );
5386 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
5387 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5388 "<%s> invalid URL", c->argv[0] );
5389 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
5390 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
5395 if ( slapMode & SLAP_SERVER_MODE ) {
5397 /* check if consumer points to current server and database.
5398 * If so, ignore this configuration.
5400 if ( !SLAP_DBHIDDEN( c->be ) ) {
5402 /* if searchbase doesn't match current DB suffix,
5403 * assume it's different
5405 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
5406 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
5411 /* if searchbase matches, see if URLs match */
5412 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
5418 init_syncrepl( si );
5419 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5420 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
5421 si->si_interval, do_syncrepl, si, "do_syncrepl",
5423 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5425 rc = config_sync_shadow( c ) ? -1 : 0;
5430 /* mirrormode still needs to see this flag in tool mode */
5431 rc = config_sync_shadow( c ) ? -1 : 0;
5433 ldap_free_urldesc( lud );
5437 /* Use main slapd defaults */
5438 bindconf_tls_defaults( &si->si_bindconf );
5441 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
5442 syncinfo_free( si, 0 );
5445 Debug( LDAP_DEBUG_CONFIG,
5446 "Config: ** successfully added syncrepl %s \"%s\"\n",
5448 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
5449 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
5450 if ( c->be->be_syncinfo ) {
5453 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
5455 /* add new syncrepl to end of list (same order as when deleting) */
5456 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
5459 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
5460 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
5461 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
5463 c->be->be_syncinfo = si;
5465 si->si_cookieState->cs_ref++;
5474 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
5476 struct berval bc, uri, bs;
5477 char buf[BUFSIZ*2], *ptr;
5480 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
5484 /* temporarily inhibit bindconf from printing URI */
5485 uri = si->si_bindconf.sb_uri;
5486 BER_BVZERO( &si->si_bindconf.sb_uri );
5487 si->si_bindconf.sb_version = 0;
5488 bindconf_unparse( &si->si_bindconf, &bc );
5489 si->si_bindconf.sb_uri = uri;
5490 si->si_bindconf.sb_version = LDAP_VERSION3;
5493 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
5494 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
5495 si->si_rid, si->si_bindconf.sb_uri.bv_val );
5496 if ( len >= sizeof( buf ) ) return;
5498 if ( !BER_BVISNULL( &bc ) ) {
5499 if ( WHATSLEFT <= bc.bv_len ) {
5503 ptr = lutil_strcopy( ptr, bc.bv_val );
5506 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
5507 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
5508 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
5509 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
5512 if ( !BER_BVISNULL( &si->si_base ) ) {
5513 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
5514 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
5515 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
5518 #ifdef ENABLE_REWRITE
5519 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
5520 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
5521 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
5522 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
5526 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
5527 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
5528 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
5529 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
5532 if ( !BER_BVISNULL( &si->si_logbase ) ) {
5533 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
5534 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
5535 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
5538 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
5539 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
5540 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
5541 ptr = lutil_strcopy( ptr, bs.bv_val );
5543 if ( si->si_attrsonly ) {
5544 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5545 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
5547 if ( si->si_anfile ) {
5548 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
5549 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
5550 ptr = lutil_strcopy( ptr, si->si_anfile );
5552 } else if ( si->si_allattrs || si->si_allopattrs ||
5553 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
5557 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5558 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
5560 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
5561 if ( ptr == NULL ) return;
5562 if ( si->si_allattrs ) {
5563 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
5564 if ( old != ptr ) *ptr++ = ',';
5567 if ( si->si_allopattrs ) {
5568 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
5569 if ( old != ptr ) *ptr++ = ',';
5574 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
5575 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
5576 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
5577 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
5578 if ( ptr == NULL ) return;
5580 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
5581 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
5582 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
5584 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
5585 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
5586 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
5587 "refreshAndPersist" : "refreshOnly" );
5589 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
5592 dd = si->si_interval;
5599 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
5600 INTERVALSTR, dd, hh, mm, ss );
5601 if ( len >= WHATSLEFT ) return;
5605 if ( si->si_got & GOT_RETRY ) {
5606 const char *space = "";
5607 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
5608 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
5609 for (i=0; si->si_retryinterval[i]; i++) {
5610 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
5611 (long) si->si_retryinterval[i] );
5613 if ( WHATSLEFT - 1 <= len ) return;
5615 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
5618 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
5619 if ( WHATSLEFT <= len ) return;
5623 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
5626 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
5629 if ( si->si_slimit ) {
5630 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
5631 if ( WHATSLEFT <= len ) return;
5635 if ( si->si_tlimit ) {
5636 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
5637 if ( WHATSLEFT <= len ) return;
5641 if ( si->si_syncdata ) {
5642 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
5643 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
5644 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
5645 ptr = lutil_strcopy( ptr, bc.bv_val );
5649 if ( si->si_lazyCommit ) {
5650 ptr = lutil_strcopy( ptr, " " LAZY_COMMIT );
5653 bc.bv_len = ptr - buf;
5655 ber_dupbv( bv, &bc );
5659 syncrepl_config( ConfigArgs *c )
5661 if (c->op == SLAP_CONFIG_EMIT) {
5662 if ( c->be->be_syncinfo ) {
5666 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
5667 syncrepl_unparse( si, &bv );
5668 ber_bvarray_add( &c->rvalue_vals, &bv );
5673 } else if ( c->op == LDAP_MOD_DELETE ) {
5675 if ( c->be->be_syncinfo ) {
5676 syncinfo_t *si, **sip;
5679 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
5681 if ( c->valx == -1 || i == c->valx ) {
5685 /* If the task is currently active, we have to leave
5686 * it running. It will exit on its own. This will only
5687 * happen when running on the cn=config DB.
5690 if ( si->si_be == c->be || ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
5693 /* There is no active thread, but we must still
5694 * ensure that no thread is (or will be) queued
5695 * while we removes the task.
5697 struct re_s *re = si->si_re;
5700 if ( si->si_conn ) {
5701 connection_client_stop( si->si_conn );
5705 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5706 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
5707 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5710 if ( ldap_pvt_thread_pool_retract( &connection_pool,
5711 re->routine, re ) > 0 )
5714 ldap_pvt_runqueue_remove( &slapd_rq, re );
5715 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5717 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5721 syncinfo_free( si, 0 );
5730 if ( !c->be->be_syncinfo ) {
5731 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5735 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5736 Debug(LDAP_DEBUG_ANY, "%s: "
5737 "syncrepl: database already shadowed.\n",
5741 return add_syncrepl( c );