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-2017 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;
85 struct berval si_logfilterstr;
88 struct berval si_contextdn;
92 AttributeName *si_anlist;
93 AttributeName *si_exanlist;
98 int si_schemachecking;
99 int si_type; /* the active type */
100 int si_ctype; /* the configured type */
102 time_t *si_retryinterval;
103 int *si_retrynum_init;
105 struct sync_cookie si_syncCookie;
106 cookie_state *si_cookieState;
111 int si_refreshDelete;
112 int si_refreshPresent;
115 time_t si_refreshBeg;
116 time_t si_refreshEnd;
117 OpExtra *si_refreshTxn;
122 int si_strict_refresh; /* stop listening during fallback refresh */
125 Avlnode *si_presentlist;
128 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
129 #ifdef ENABLE_REWRITE
130 struct rewrite_info *si_rewrite;
131 struct berval si_suffixm;
133 ldap_pvt_thread_mutex_t si_mutex;
136 static int syncuuid_cmp( const void *, const void * );
137 static int presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
138 static void presentlist_delete( Avlnode **av, struct berval *syncUUID );
139 static char *presentlist_find( Avlnode *av, struct berval *syncUUID );
140 static int presentlist_free( Avlnode *av );
141 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
142 static int syncrepl_message_to_op(
143 syncinfo_t *, Operation *, LDAPMessage * );
144 static int syncrepl_message_to_entry(
145 syncinfo_t *, Operation *, LDAPMessage *,
146 Modifications **, Entry **, int, struct berval* );
147 static int syncrepl_entry(
148 syncinfo_t *, Operation*, Entry*,
149 Modifications**,int, struct berval*,
150 struct berval *cookieCSN );
151 static int syncrepl_updateCookie(
152 syncinfo_t *, Operation *,
153 struct sync_cookie * );
154 static struct berval * slap_uuidstr_from_normalized(
155 struct berval *, struct berval *, void * );
156 static int syncrepl_add_glue_ancestors(
157 Operation* op, Entry *e );
159 /* delta-mmr overlay handler */
160 static int syncrepl_op_modify( Operation *op, SlapReply *rs );
162 /* callback functions */
163 static int dn_callback( Operation *, SlapReply * );
164 static int nonpresent_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++ ) {
947 #ifdef CHATTY_SYNCLOG
948 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN for sid %d: %s\n",
949 si->si_ridtxt, i, si->si_cookieState->cs_vals[i].bv_val );
952 if ( sid < si->si_cookieState->cs_sids[i] )
954 if ( si->si_cookieState->cs_sids[i] == sid ) {
955 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
956 bdn.bv_val[bdn.bv_len] = '\0';
957 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s (%s)\n",
958 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
960 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
961 ldap_controls_free( rctrls );
969 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
971 /* check pending CSNs too */
972 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
973 if ( slapd_shutdown ) {
977 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
978 ldap_pvt_thread_yield();
981 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
982 if ( sid < si->si_cookieState->cs_psids[i] )
984 if ( si->si_cookieState->cs_psids[i] == sid ) {
985 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
986 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
987 bdn.bv_val[bdn.bv_len] = '\0';
988 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s (%s)\n",
989 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
990 ldap_controls_free( rctrls );
994 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
999 /* new SID, add it */
1000 if ( i == si->si_cookieState->cs_pnum ||
1001 sid != si->si_cookieState->cs_psids[i] ) {
1002 slap_insert_csn_sids(
1003 (struct sync_cookie *)&si->si_cookieState->cs_pvals,
1004 i, sid, syncCookie.ctxcsn );
1006 assert( punlock < 0 );
1008 } else if (si->si_too_old) {
1009 bdn.bv_val[bdn.bv_len] = '\0';
1010 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring (%s)\n",
1011 si->si_ridtxt, bdn.bv_val, 0 );
1012 ldap_controls_free( rctrls );
1016 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1021 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
1023 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
1026 rc = syncrepl_updateCookie( si, op, &syncCookie );
1027 } else switch ( rc ) {
1028 case LDAP_ALREADY_EXISTS:
1029 case LDAP_NO_SUCH_OBJECT:
1030 case LDAP_NO_SUCH_ATTRIBUTE:
1031 case LDAP_TYPE_OR_VALUE_EXISTS:
1032 case LDAP_NOT_ALLOWED_ON_NONLEAF:
1033 rc = LDAP_SYNC_REFRESH_REQUIRED;
1034 si->si_logstate = SYNCLOG_FALLBACK;
1035 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
1036 bdn.bv_val[bdn.bv_len] = '\0';
1037 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync on (%s), switching to REFRESH\n",
1038 si->si_ridtxt, bdn.bv_val, 0 );
1039 if (si->si_strict_refresh) {
1040 slap_suspend_listeners();
1047 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
1048 &modlist, &entry, syncstate, syncUUID ) ) == LDAP_SUCCESS )
1050 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
1051 syncstate, syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
1054 rc = syncrepl_updateCookie( si, op, &syncCookie );
1057 if ( punlock >= 0 ) {
1058 /* on failure, revert pending CSN */
1059 if ( rc != LDAP_SUCCESS ) {
1061 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
1062 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
1063 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
1064 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
1065 &si->si_cookieState->cs_vals[i] );
1069 if ( i == si->si_cookieState->cs_num )
1070 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
1071 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
1073 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
1075 ldap_controls_free( rctrls );
1077 slap_mods_free( modlist, 1 );
1083 case LDAP_RES_SEARCH_REFERENCE:
1084 Debug( LDAP_DEBUG_ANY,
1085 "do_syncrep2: %s reference received error\n",
1086 si->si_ridtxt, 0, 0 );
1089 case LDAP_RES_SEARCH_RESULT:
1090 Debug( LDAP_DEBUG_SYNC,
1091 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
1092 si->si_ridtxt, 0, 0 );
1093 err = LDAP_OTHER; /* FIXME check parse result properly */
1094 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
1096 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
1097 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
1098 /* map old result code to registered code */
1099 err = LDAP_SYNC_REFRESH_REQUIRED;
1102 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
1103 if ( si->si_logstate == SYNCLOG_LOGGING ) {
1104 si->si_logstate = SYNCLOG_FALLBACK;
1105 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync, switching to REFRESH\n",
1106 si->si_ridtxt, 0, 0 );
1107 if (si->si_strict_refresh) {
1108 slap_suspend_listeners();
1116 Debug( LDAP_DEBUG_ANY,
1117 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1118 si->si_ridtxt, err, ldap_err2string( err ) );
1121 LDAPControl **next = NULL;
1122 /* NOTE: make sure we use the right one;
1123 * a better approach would be to run thru
1124 * the whole list and take care of all */
1125 /* NOTE: since we issue the search request,
1126 * we should know what controls to expect,
1127 * and there should be none apart from the
1128 * sync-related control */
1129 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1130 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1132 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1133 "got search result with multiple "
1134 "Sync State control\n", si->si_ridtxt, 0, 0 );
1135 ldap_controls_free( rctrls );
1141 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1143 ber_scanf( ber, "{" /*"}"*/);
1144 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1145 ber_scanf( ber, "m", &cookie );
1147 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1149 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1151 if ( !BER_BVISNULL( &cookie ) ) {
1152 ch_free( syncCookie.octet_str.bv_val );
1153 ber_dupbv( &syncCookie.octet_str, &cookie);
1155 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1157 slap_parse_sync_cookie( &syncCookie, NULL );
1158 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1161 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1163 ber_scanf( ber, "b", &refreshDeletes );
1165 ber_scanf( ber, /*"{"*/ "}" );
1167 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1168 slap_sync_cookie_free( &syncCookie_req, 0 );
1169 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1171 if ( !syncCookie.ctxcsn ) {
1173 } else if ( !syncCookie_req.ctxcsn ) {
1177 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1180 ldap_controls_free( rctrls );
1182 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1183 /* FIXME : different error behaviors according to
1184 * 1) err code : LDAP_BUSY ...
1185 * 2) on err policy : stop service, stop sync, retry
1187 if ( refreshDeletes == 0 && match < 0 &&
1188 err == LDAP_SUCCESS &&
1189 syncCookie_req.numcsns == syncCookie.numcsns )
1191 syncrepl_del_nonpresent( op, si, NULL,
1193 } else if ( si->si_presentlist ) {
1194 presentlist_free( si->si_presentlist );
1195 si->si_presentlist = NULL;
1198 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1200 rc = syncrepl_updateCookie( si, op, &syncCookie );
1202 if ( si->si_refreshCount ) {
1203 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
1204 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
1205 si->si_refreshCount = 0;
1206 si->si_refreshTxn = NULL;
1208 si->si_refreshEnd = slap_get_time();
1209 if ( err == LDAP_SUCCESS
1210 && si->si_logstate == SYNCLOG_FALLBACK ) {
1211 si->si_logstate = SYNCLOG_LOGGING;
1212 rc = LDAP_SYNC_REFRESH_REQUIRED;
1213 slap_resume_listeners();
1219 case LDAP_RES_INTERMEDIATE:
1222 rc = ldap_parse_intermediate( si->si_ld, msg,
1223 &retoid, &retdata, NULL, 0 );
1224 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1225 ber_init2( ber, retdata, LBER_USE_DER );
1227 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1229 case LDAP_TAG_SYNC_NEW_COOKIE:
1230 Debug( LDAP_DEBUG_SYNC,
1231 "do_syncrep2: %s %s - %s\n",
1233 "LDAP_RES_INTERMEDIATE",
1235 ber_scanf( ber, "tm", &tag, &cookie );
1236 Debug( LDAP_DEBUG_SYNC,
1237 "do_syncrep2: %s NEW_COOKIE: %s\n",
1240 if ( !BER_BVISNULL( &cookie ) ) {
1241 ch_free( syncCookie.octet_str.bv_val );
1242 ber_dupbv( &syncCookie.octet_str, &cookie );
1244 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1245 slap_parse_sync_cookie( &syncCookie, NULL );
1246 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1249 case LDAP_TAG_SYNC_REFRESH_DELETE:
1250 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1251 Debug( LDAP_DEBUG_SYNC,
1252 "do_syncrep2: %s %s - %s\n",
1254 "LDAP_RES_INTERMEDIATE",
1255 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1256 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1257 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1258 si->si_refreshDelete = 1;
1260 si->si_refreshPresent = 1;
1262 ber_scanf( ber, "t{" /*"}"*/, &tag );
1263 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1265 ber_scanf( ber, "m", &cookie );
1267 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1269 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1271 if ( !BER_BVISNULL( &cookie ) ) {
1272 ch_free( syncCookie.octet_str.bv_val );
1273 ber_dupbv( &syncCookie.octet_str, &cookie );
1275 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1277 slap_parse_sync_cookie( &syncCookie, NULL );
1278 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1281 /* Defaults to TRUE */
1282 if ( ber_peek_tag( ber, &len ) ==
1283 LDAP_TAG_REFRESHDONE )
1285 ber_scanf( ber, "b", &si->si_refreshDone );
1288 si->si_refreshDone = 1;
1290 if ( si->si_refreshDone ) {
1291 if ( si->si_refreshCount ) {
1292 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
1293 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
1294 si->si_refreshCount = 0;
1295 si->si_refreshTxn = NULL;
1297 si->si_refreshEnd = slap_get_time();
1298 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s finished refresh\n",
1299 si->si_ridtxt, 0, 0 );
1301 ber_scanf( ber, /*"{"*/ "}" );
1302 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
1303 si->si_refreshDone )
1306 case LDAP_TAG_SYNC_ID_SET:
1307 Debug( LDAP_DEBUG_SYNC,
1308 "do_syncrep2: %s %s - %s\n",
1310 "LDAP_RES_INTERMEDIATE",
1312 ber_scanf( ber, "t{" /*"}"*/, &tag );
1313 if ( ber_peek_tag( ber, &len ) ==
1314 LDAP_TAG_SYNC_COOKIE )
1316 ber_scanf( ber, "m", &cookie );
1318 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1320 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1322 if ( !BER_BVISNULL( &cookie ) ) {
1323 ch_free( syncCookie.octet_str.bv_val );
1324 ber_dupbv( &syncCookie.octet_str, &cookie );
1326 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1328 slap_parse_sync_cookie( &syncCookie, NULL );
1329 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1330 compare_csns( &syncCookie_req, &syncCookie, &m );
1333 if ( ber_peek_tag( ber, &len ) ==
1334 LDAP_TAG_REFRESHDELETES )
1336 ber_scanf( ber, "b", &refreshDeletes );
1339 rc = ber_scanf( ber, "[W]", &syncUUIDs );
1340 ber_scanf( ber, /*"{"*/ "}" );
1341 if ( rc != LBER_ERROR ) {
1342 if ( refreshDeletes ) {
1343 syncrepl_del_nonpresent( op, si, syncUUIDs,
1345 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1348 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1349 (void)presentlist_insert( si, &syncUUIDs[i] );
1350 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1352 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1356 slap_sync_cookie_free( &syncCookie, 0 );
1359 Debug( LDAP_DEBUG_ANY,
1360 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1361 si->si_ridtxt, (long) si_tag, 0 );
1362 ldap_memfree( retoid );
1363 ber_bvfree( retdata );
1367 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1368 slap_sync_cookie_free( &syncCookie_req, 0 );
1369 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1371 if ( !syncCookie.ctxcsn ) {
1373 } else if ( !syncCookie_req.ctxcsn ) {
1377 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1381 if ( si->si_refreshPresent == 1 &&
1382 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1383 syncCookie_req.numcsns == syncCookie.numcsns ) {
1384 syncrepl_del_nonpresent( op, si, NULL,
1388 if ( syncCookie.ctxcsn )
1390 rc = syncrepl_updateCookie( si, op, &syncCookie);
1392 if ( si->si_presentlist ) {
1393 presentlist_free( si->si_presentlist );
1394 si->si_presentlist = NULL;
1398 ldap_memfree( retoid );
1399 ber_bvfree( retdata );
1405 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1406 "unknown intermediate response (%d)\n",
1407 si->si_ridtxt, rc, 0 );
1408 ldap_memfree( retoid );
1409 ber_bvfree( retdata );
1414 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1415 "unknown message (0x%02lx)\n",
1417 (unsigned long)ldap_msgtype( msg ), 0 );
1421 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1422 slap_sync_cookie_free( &syncCookie_req, 0 );
1423 syncCookie_req = syncCookie;
1424 memset( &syncCookie, 0, sizeof( syncCookie ));
1426 ldap_msgfree( msg );
1428 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1429 slap_sync_cookie_free( &syncCookie, 0 );
1430 slap_sync_cookie_free( &syncCookie_req, 0 );
1431 if ( si->si_refreshCount ) {
1432 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
1433 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
1434 si->si_refreshCount = 0;
1435 si->si_refreshTxn = NULL;
1443 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1448 if ( err != LDAP_SUCCESS ) {
1449 Debug( LDAP_DEBUG_ANY,
1450 "do_syncrep2: %s (%d) %s\n",
1451 si->si_ridtxt, err, ldap_err2string( err ) );
1454 slap_sync_cookie_free( &syncCookie, 0 );
1455 slap_sync_cookie_free( &syncCookie_req, 0 );
1457 if ( msg ) ldap_msgfree( msg );
1459 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1460 if ( si->si_conn ) {
1461 connection_client_stop( si->si_conn );
1464 ldap_unbind_ext( si->si_ld, NULL, NULL );
1476 struct re_s* rtask = arg;
1477 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1478 Connection conn = {0};
1479 OperationBuffer opbuf;
1481 int rc = LDAP_SUCCESS;
1484 int i, defer = 1, fail = 0, freeinfo = 0;
1489 if ( slapd_shutdown )
1492 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1494 /* Don't get stuck here while a pause is initiated */
1495 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1496 if ( slapd_shutdown )
1498 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1499 ldap_pvt_thread_yield();
1504 if ( si->si_ctype < 1 ) {
1508 switch( abs( si->si_type ) ) {
1509 case LDAP_SYNC_REFRESH_ONLY:
1510 case LDAP_SYNC_REFRESH_AND_PERSIST:
1513 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1517 if ( slapd_shutdown ) {
1519 if ( si->si_conn ) {
1520 connection_client_stop( si->si_conn );
1523 ldap_unbind_ext( si->si_ld, NULL, NULL );
1526 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1530 connection_fake_init( &conn, &opbuf, ctx );
1532 /* o_connids must be unique for slap_graduate_commit_csn */
1533 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1535 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1538 /* Coordinate contextCSN updates with any syncprov overlays
1539 * in use. This may be complicated by the use of the glue
1542 * Typically there is a single syncprov mastering the entire
1543 * glued tree. In that case, our contextCSN updates should
1544 * go to the master DB. But if there is no syncprov on the
1545 * master DB, then nothing special is needed here.
1547 * Alternatively, there may be individual syncprov overlays
1548 * on each glued branch. In that case, each syncprov only
1549 * knows about changes within its own branch. And so our
1550 * contextCSN updates should only go to the local DB.
1552 if ( !si->si_wbe ) {
1553 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1554 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1555 if ( overlay_is_inst( top_be, "syncprov" ))
1556 si->si_wbe = top_be;
1562 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1563 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1564 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1566 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1569 if ( !si->si_schemachecking )
1570 op->o_no_schema_check = 1;
1572 /* Establish session, do search */
1574 si->si_refreshDelete = 0;
1575 si->si_refreshPresent = 0;
1577 if ( si->si_presentlist ) {
1578 presentlist_free( si->si_presentlist );
1579 si->si_presentlist = NULL;
1582 /* use main DB when retrieving contextCSN */
1583 op->o_bd = si->si_wbe;
1584 op->o_dn = op->o_bd->be_rootdn;
1585 op->o_ndn = op->o_bd->be_rootndn;
1586 rc = do_syncrep1( op, si );
1590 /* Process results */
1591 if ( rc == LDAP_SUCCESS ) {
1592 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1594 /* use current DB */
1596 op->o_dn = op->o_bd->be_rootdn;
1597 op->o_ndn = op->o_bd->be_rootndn;
1598 rc = do_syncrep2( op, si );
1599 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1600 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
1601 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
1602 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
1603 si->si_syncCookie.sid );
1604 rc = ldap_sync_search( si, op->o_tmpmemctx );
1609 /* We got deleted while running on cn=config */
1610 if ( si->si_ctype < 1 ) {
1611 if ( si->si_ctype == -1 ) {
1620 if ( rc != SYNC_PAUSED ) {
1621 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1622 /* If we succeeded, enable the connection for further listening.
1623 * If we failed, tear down the connection and reschedule.
1625 if ( rc == LDAP_SUCCESS ) {
1626 if ( si->si_conn ) {
1627 connection_client_enable( si->si_conn );
1629 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1631 } else if ( si->si_conn ) {
1635 if ( rc == -2 ) rc = 0;
1640 /* At this point, we have 5 cases:
1641 * 1) for any hard failure, give up and remove this task
1642 * 2) for ServerDown, reschedule this task to run later
1643 * 3) for threadpool pause, reschedule to run immediately
1644 * 4) for Refresh and Success, reschedule to run
1645 * 5) for Persist and Success, reschedule to defer
1647 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1649 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1650 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1654 connection_client_stop( si->si_conn );
1658 if ( rc == SYNC_PAUSED ) {
1659 rtask->interval.tv_sec = 0;
1660 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1661 rtask->interval.tv_sec = si->si_interval;
1663 } else if ( rc == LDAP_SUCCESS ) {
1664 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1667 rtask->interval.tv_sec = si->si_interval;
1668 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1669 if ( si->si_retrynum ) {
1670 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1671 si->si_retrynum[i] = si->si_retrynum_init[i];
1673 si->si_retrynum[i] = RETRYNUM_TAIL;
1676 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1677 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1681 if ( si->si_ctype < 1
1682 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1684 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1687 fail = RETRYNUM_TAIL;
1688 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1689 if ( si->si_retrynum[i] > 0 )
1690 si->si_retrynum[i]--;
1691 fail = si->si_retrynum[i];
1692 rtask->interval.tv_sec = si->si_retryinterval[i];
1693 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1694 slap_wake_listener();
1698 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1699 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1702 if ( fail == RETRYNUM_TAIL ) {
1703 Debug( LDAP_DEBUG_ANY,
1704 "do_syncrepl: %s rc %d quitting\n",
1705 si->si_ridtxt, rc, 0 );
1706 } else if ( fail > 0 ) {
1707 Debug( LDAP_DEBUG_ANY,
1708 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1709 si->si_ridtxt, rc, fail );
1711 Debug( LDAP_DEBUG_ANY,
1712 "do_syncrepl: %s rc %d retrying\n",
1713 si->si_ridtxt, rc, 0 );
1717 /* Do final delete cleanup */
1719 syncinfo_free( si, 0 );
1724 #ifdef ENABLE_REWRITE
1726 syncrepl_rewrite_dn(
1729 struct berval *sdn )
1734 nul = dn->bv_val[dn->bv_len];
1735 dn->bv_val[dn->bv_len] = 0;
1736 rc = rewrite( si->si_rewrite, SUFFIXM_CTX, dn->bv_val, &sdn->bv_val );
1737 dn->bv_val[dn->bv_len] = nul;
1739 if ( sdn->bv_val == dn->bv_val )
1741 else if ( rc == REWRITE_REGEXEC_OK && sdn->bv_val )
1742 sdn->bv_len = strlen( sdn->bv_val );
1745 #define REWRITE_VAL(si, ad, bv, bv2) \
1746 BER_BVZERO( &bv2 ); \
1747 if ( si->si_rewrite && ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName) \
1748 syncrepl_rewrite_dn( si, &bv, &bv2); \
1749 if ( BER_BVISNULL( &bv2 )) \
1750 ber_dupbv( &bv2, &bv )
1751 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1752 BER_BVZERO( &bv2 ); \
1753 if (si->si_rewrite) \
1754 syncrepl_rewrite_dn(si, &bv, &bv2); \
1755 rc = dnPrettyNormal( NULL, bv2.bv_val ? &bv2 : &bv, &dn, &ndn, op->o_tmpmemctx ); \
1758 #define REWRITE_VAL(si, ad, bv, bv2) ber_dupbv(&bv2, &bv)
1759 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1760 rc = dnPrettyNormal( NULL, &bv, &dn, &ndn, op->o_tmpmemctx )
1764 static slap_verbmasks modops[] = {
1765 { BER_BVC("add"), LDAP_REQ_ADD },
1766 { BER_BVC("delete"), LDAP_REQ_DELETE },
1767 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1768 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1773 syncrepl_accesslog_mods(
1775 struct berval *vals,
1776 struct Modifications **modres
1781 AttributeDescription *ad;
1782 struct berval bv, bv2;
1784 Modifications *mod = NULL, *modlist = NULL, **modtail;
1789 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1793 colon = ber_bvchr( &bv, ':' );
1797 } else if ( colon == bv.bv_val ) {
1798 /* ITS#6545: An empty attribute signals that a new mod
1799 * is about to start */
1804 bv.bv_len = colon - bv.bv_val;
1805 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1807 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1808 "Invalid attribute %s, %s\n",
1809 si->si_ridtxt, bv.bv_val, text );
1810 slap_mods_free( modlist, 1 );
1816 /* Ignore dynamically generated attrs */
1817 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1821 /* Ignore excluded attrs */
1822 if ( ldap_charray_inlist( si->si_exattrs,
1823 ad->ad_type->sat_cname.bv_val ) )
1829 case '+': op = LDAP_MOD_ADD; break;
1830 case '-': op = LDAP_MOD_DELETE; break;
1831 case '=': op = LDAP_MOD_REPLACE; break;
1832 case '#': op = LDAP_MOD_INCREMENT; break;
1836 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1837 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1840 mod->sml_next = NULL;
1842 mod->sml_type = ad->ad_cname;
1843 mod->sml_values = NULL;
1844 mod->sml_nvalues = NULL;
1845 mod->sml_numvals = 0;
1848 modtail = &mod->sml_next;
1850 if ( colon[2] == ' ' ) {
1851 bv.bv_val = colon + 3;
1852 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1853 REWRITE_VAL( si, ad, bv, bv2 );
1854 ber_bvarray_add( &mod->sml_values, &bv2 );
1863 syncrepl_changelog_mods(
1865 struct berval *vals,
1866 struct Modifications **modres
1869 return -1; /* FIXME */
1872 typedef struct OpExtraSync {
1877 /* Copy the original modlist, split Replace ops into Delete/Add,
1878 * and drop mod opattrs since this modification is in the past.
1880 static Modifications *mods_dup( Operation *op, Modifications *modlist, int match )
1882 Modifications *mod, *modnew = NULL, *modtail = NULL;
1884 for ( ; modlist; modlist = modlist->sml_next ) {
1887 if ( modlist->sml_desc == slap_schema.si_ad_modifiersName ||
1888 modlist->sml_desc == slap_schema.si_ad_modifyTimestamp ||
1889 modlist->sml_desc == slap_schema.si_ad_entryCSN )
1891 if ( modlist->sml_op == LDAP_MOD_REPLACE ) {
1892 mod = op->o_tmpalloc( sizeof(Modifications), op->o_tmpmemctx );
1893 mod->sml_desc = modlist->sml_desc;
1894 mod->sml_values = NULL;
1895 mod->sml_nvalues = NULL;
1896 mod->sml_op = LDAP_MOD_DELETE;
1897 mod->sml_numvals = 0;
1902 modtail->sml_next = mod;
1906 if ( modlist->sml_numvals ) {
1907 size = (modlist->sml_numvals+1) * sizeof(struct berval);
1908 if ( modlist->sml_nvalues ) size *= 2;
1912 size += sizeof(Modifications);
1913 mod = op->o_tmpalloc( size, op->o_tmpmemctx );
1917 modtail->sml_next = mod;
1919 mod->sml_desc = modlist->sml_desc;
1920 mod->sml_numvals = modlist->sml_numvals;
1922 if ( modlist->sml_numvals ) {
1924 mod->sml_values = (BerVarray)(mod+1);
1925 for (i=0; i<mod->sml_numvals; i++)
1926 mod->sml_values[i] = modlist->sml_values[i];
1927 BER_BVZERO(&mod->sml_values[i]);
1928 if ( modlist->sml_nvalues ) {
1929 mod->sml_nvalues = mod->sml_values + mod->sml_numvals + 1;
1930 for (i=0; i<mod->sml_numvals; i++)
1931 mod->sml_nvalues[i] = modlist->sml_nvalues[i];
1932 BER_BVZERO(&mod->sml_nvalues[i]);
1934 mod->sml_nvalues = NULL;
1937 mod->sml_values = NULL;
1938 mod->sml_nvalues = NULL;
1940 if ( match < 0 && modlist->sml_op == LDAP_MOD_REPLACE )
1941 mod->sml_op = LDAP_MOD_ADD;
1943 mod->sml_op = modlist->sml_op;
1944 mod->sml_next = NULL;
1949 typedef struct resolve_ctxt {
1951 Modifications *rx_mods;
1955 compare_vals( Modifications *m1, Modifications *m2 )
1958 struct berval *bv1, *bv2;
1960 if ( m2->sml_nvalues ) {
1961 bv2 = m2->sml_nvalues;
1962 bv1 = m1->sml_nvalues;
1964 bv2 = m2->sml_values;
1965 bv1 = m1->sml_values;
1967 for ( j=0; j<m2->sml_numvals; j++ ) {
1968 for ( i=0; i<m1->sml_numvals; i++ ) {
1969 if ( !ber_bvcmp( &bv1[i], &bv2[j] )) {
1971 for ( k=i; k<m1->sml_numvals-1; k++ ) {
1972 m1->sml_values[k] = m1->sml_values[k+1];
1973 if ( m1->sml_nvalues )
1974 m1->sml_nvalues[k] = m1->sml_nvalues[k+1];
1976 BER_BVZERO(&m1->sml_values[k]);
1977 if ( m1->sml_nvalues ) {
1978 BER_BVZERO(&m1->sml_nvalues[k]);
1988 syncrepl_resolve_cb( Operation *op, SlapReply *rs )
1990 if ( rs->sr_type == REP_SEARCH ) {
1991 resolve_ctxt *rx = op->o_callback->sc_private;
1992 Attribute *a = attr_find( rs->sr_entry->e_attrs, ad_reqMod );
1994 Modifications *oldmods, *newmods, *m1, *m2, **prev;
1995 oldmods = rx->rx_mods;
1996 syncrepl_accesslog_mods( rx->rx_si, a->a_vals, &newmods );
1997 for ( m2 = newmods; m2; m2=m2->sml_next ) {
1998 for ( prev = &oldmods, m1 = *prev; m1; m1 = *prev ) {
1999 if ( m1->sml_desc != m2->sml_desc ) {
2000 prev = &m1->sml_next;
2003 if ( m2->sml_op == LDAP_MOD_DELETE ||
2004 m2->sml_op == LDAP_MOD_REPLACE ) {
2005 int numvals = m2->sml_numvals;
2006 if ( m2->sml_op == LDAP_MOD_REPLACE )
2008 /* New delete All cancels everything */
2009 if ( numvals == 0 ) {
2011 *prev = m1->sml_next;
2012 op->o_tmpfree( m1, op->o_tmpmemctx );
2015 if ( m1->sml_op == LDAP_MOD_DELETE ) {
2016 if ( m1->sml_numvals == 0 ) {
2017 /* turn this to SOFTDEL later */
2018 m1->sml_flags = SLAP_MOD_INTERNAL;
2020 compare_vals( m1, m2 );
2021 if ( !m1->sml_numvals )
2024 } else if ( m1->sml_op == LDAP_MOD_ADD ) {
2025 compare_vals( m1, m2 );
2026 if ( !m1->sml_numvals )
2031 if ( m2->sml_op == LDAP_MOD_ADD ||
2032 m2->sml_op == LDAP_MOD_REPLACE ) {
2033 if ( m1->sml_op == LDAP_MOD_DELETE ) {
2034 if ( !m1->sml_numvals ) goto drop;
2035 compare_vals( m1, m2 );
2036 if ( !m1->sml_numvals )
2039 if ( m2->sml_desc->ad_type->sat_atype.at_single_value )
2041 compare_vals( m1, m2 );
2042 if ( !m1->sml_numvals )
2045 prev = &m1->sml_next;
2048 slap_mods_free( newmods, 1 );
2049 rx->rx_mods = oldmods;
2052 return LDAP_SUCCESS;
2055 typedef struct modify_ctxt {
2056 Modifications *mx_orig;
2057 Modifications *mx_free;
2061 syncrepl_modify_cb( Operation *op, SlapReply *rs )
2063 slap_callback *sc = op->o_callback;
2064 modify_ctxt *mx = sc->sc_private;
2067 op->orm_no_opattrs = 0;
2068 op->orm_modlist = mx->mx_orig;
2069 for ( ml = mx->mx_free; ml; ml = mx->mx_free ) {
2070 mx->mx_free = ml->sml_next;
2071 op->o_tmpfree( ml, op->o_tmpmemctx );
2073 op->o_callback = sc->sc_next;
2074 op->o_tmpfree( sc, op->o_tmpmemctx );
2075 return SLAP_CB_CONTINUE;
2079 syncrepl_op_modify( Operation *op, SlapReply *rs )
2081 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2086 Modifications *mod, *newlist;
2088 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
2089 if ( oex->oe_key == (void *)syncrepl_message_to_op )
2093 return SLAP_CB_CONTINUE;
2095 si = ((OpExtraSync *)oex)->oe_si;
2097 /* Check if entryCSN in modlist is newer than entryCSN in entry.
2098 * We do it here because the op has been serialized by accesslog
2099 * by the time we get here. If the CSN is new enough, just do the
2100 * mod. If not, we need to resolve conflicts.
2103 for ( mod = op->orm_modlist; mod; mod=mod->sml_next ) {
2104 if ( mod->sml_desc == slap_schema.si_ad_entryCSN ) break;
2106 /* FIXME: what should we do if entryCSN is missing from the mod? */
2108 return SLAP_CB_CONTINUE;
2112 sid = slap_parse_csn_sid( &mod->sml_nvalues[0] );
2113 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
2114 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
2115 #ifdef CHATTY_SYNCLOG
2116 Debug( LDAP_DEBUG_SYNC, "syncrepl_op_modify: %s CSN for sid %d: %s\n",
2117 si->si_ridtxt, i, si->si_cookieState->cs_vals[i].bv_val );
2120 if ( sid < si->si_cookieState->cs_sids[i] )
2122 if ( si->si_cookieState->cs_sids[i] == sid ) {
2123 if ( ber_bvcmp( &mod->sml_nvalues[0], &si->si_cookieState->cs_vals[i] ) <= 0 ) {
2124 Debug( LDAP_DEBUG_SYNC, "syncrepl_op_modify: %s entryCSN too old, ignoring %s (%s)\n",
2125 si->si_ridtxt, mod->sml_nvalues[0].bv_val, op->o_req_dn.bv_val );
2126 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2127 slap_graduate_commit_csn( op );
2128 /* tell accesslog this was a failure */
2129 rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
2130 return LDAP_SUCCESS;
2134 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2137 rc = overlay_entry_get_ov( op, &op->o_req_ndn, NULL, NULL, 0, &e, on );
2141 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
2142 value_match( &match, slap_schema.si_ad_entryCSN,
2143 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
2144 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2145 &mod->sml_nvalues[0], &a->a_nvals[0], &text );
2146 overlay_entry_release_ov( op, e, 0, on );
2148 /* equal? Should never happen */
2150 slap_graduate_commit_csn( op );
2151 /* tell accesslog this was a failure */
2152 rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
2153 return LDAP_SUCCESS;
2156 /* mod is older: resolve conflicts...
2157 * 1. Save/copy original modlist. Split Replace to Del/Add.
2158 * 2. Find all mods to this reqDN newer than the mod stamp.
2159 * 3. Resolve any mods in this request that affect attributes
2160 * touched by newer mods.
2162 * delete all delete all drop
2163 * delete all delete X SOFTDEL
2164 * delete X delete all drop
2165 * delete X delete X drop
2166 * delete X delete Y OK
2167 * delete all add X drop
2168 * delete X add X drop
2170 * add X delete all drop
2171 * add X delete X drop
2173 * add X add Y if SV, drop else OK
2175 * 4. Swap original modlist back in response callback so
2176 * that accesslog logs the original mod.
2178 * Even if the mod is newer, other out-of-order changes may
2179 * have been committed, forcing us to tweak the modlist:
2180 * 1. Save/copy original modlist.
2181 * 2. Change deletes to soft deletes.
2182 * 3. Change Adds of single-valued attrs to Replace.
2185 newlist = mods_dup( op, op->orm_modlist, match );
2189 Operation op2 = *op;
2190 AttributeName an[2];
2196 SlapReply rs1 = {0};
2198 slap_callback cb = { NULL, syncrepl_resolve_cb, NULL, NULL };
2200 AttributeAssertion aa[2] = {0};
2203 rx.rx_mods = newlist;
2204 cb.sc_private = ℞
2206 op2.o_tag = LDAP_REQ_SEARCH;
2207 op2.ors_scope = LDAP_SCOPE_SUBTREE;
2208 op2.ors_deref = LDAP_DEREF_NEVER;
2209 op2.o_req_dn = si->si_logbase;
2210 op2.o_req_ndn = si->si_logbase;
2211 op2.ors_tlimit = SLAP_NO_LIMIT;
2212 op2.ors_slimit = SLAP_NO_LIMIT;
2213 op2.ors_limit = NULL;
2214 memset( an, 0, sizeof(an));
2215 an[0].an_desc = ad_reqMod;
2216 an[0].an_name = ad_reqMod->ad_cname;
2218 op2.ors_attrsonly = 0;
2220 bv = mod->sml_nvalues[0];
2222 size = sizeof("(&(entryCSN>=)(reqDN=))");
2223 size += bv.bv_len + op->o_req_ndn.bv_len + si->si_logfilterstr.bv_len;
2224 op2.ors_filterstr.bv_val = op->o_tmpalloc( size, op->o_tmpmemctx );
2225 op2.ors_filterstr.bv_len = sprintf(op2.ors_filterstr.bv_val,
2226 "(&(entryCSN>=%s)(reqDN=%s)%s)",
2227 bv.bv_val, op->o_req_ndn.bv_val, si->si_logfilterstr.bv_val );
2229 lf[0].f_choice = LDAP_FILTER_AND;
2231 lf[1].f_choice = LDAP_FILTER_GE;
2233 lf[1].f_av_desc = slap_schema.si_ad_entryCSN;
2234 lf[1].f_av_value = bv;
2235 lf[1].f_next = lf+2;
2236 lf[2].f_choice = LDAP_FILTER_EQUALITY;
2238 lf[2].f_av_desc = ad_reqDN;
2239 lf[2].f_av_value = op->o_req_ndn;
2240 lf[2].f_next = si->si_logfilter;
2242 op2.ors_filter = lf;
2244 op2.o_callback = &cb;
2245 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2246 op2.o_bd->be_search( &op2, &rs1 );
2247 newlist = rx.rx_mods;
2251 slap_callback *sc = op->o_tmpalloc( sizeof(slap_callback) +
2252 sizeof(modify_ctxt), op->o_tmpmemctx );
2253 modify_ctxt *mx = (modify_ctxt *)(sc+1);
2256 sc->sc_response = syncrepl_modify_cb;
2257 sc->sc_private = mx;
2258 sc->sc_next = op->o_callback;
2259 sc->sc_cleanup = NULL;
2260 sc->sc_writewait = NULL;
2261 op->o_callback = sc;
2262 op->orm_no_opattrs = 1;
2263 mx->mx_orig = op->orm_modlist;
2264 mx->mx_free = newlist;
2265 for ( ml = newlist; ml; ml=ml->sml_next ) {
2266 if ( ml->sml_flags == SLAP_MOD_INTERNAL ) {
2268 ml->sml_op = SLAP_MOD_SOFTDEL;
2270 else if ( ml->sml_op == LDAP_MOD_DELETE )
2271 ml->sml_op = SLAP_MOD_SOFTDEL;
2272 else if ( ml->sml_op == LDAP_MOD_ADD &&
2273 ml->sml_desc->ad_type->sat_atype.at_single_value )
2274 ml->sml_op = LDAP_MOD_REPLACE;
2276 op->orm_modlist = newlist;
2277 op->o_csn = mod->sml_nvalues[0];
2279 return SLAP_CB_CONTINUE;
2283 syncrepl_null_callback(
2287 /* If we're not the last callback in the chain, move to the end */
2288 if ( op->o_callback->sc_next ) {
2289 slap_callback **sc, *s1;
2290 s1 = op->o_callback;
2291 op->o_callback = op->o_callback->sc_next;
2292 for ( sc = &op->o_callback; *sc; sc = &(*sc)->sc_next ) ;
2295 return SLAP_CB_CONTINUE;
2297 if ( rs->sr_err != LDAP_SUCCESS &&
2298 rs->sr_err != LDAP_REFERRAL &&
2299 rs->sr_err != LDAP_ALREADY_EXISTS &&
2300 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2301 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2303 Debug( LDAP_DEBUG_ANY,
2304 "syncrepl_null_callback : error code 0x%x\n",
2307 return LDAP_SUCCESS;
2311 syncrepl_message_to_op(
2317 BerElement *ber = NULL;
2318 Modifications *modlist = NULL;
2320 SlapReply rs = { REP_RESULT };
2321 slap_callback cb = { NULL, syncrepl_null_callback, NULL, NULL };
2324 char txtbuf[SLAP_TEXT_BUFLEN];
2325 size_t textlen = sizeof txtbuf;
2327 struct berval bdn, dn = BER_BVNULL, ndn;
2328 struct berval bv, bv2, *bvals = NULL;
2329 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
2330 prdn = BER_BVNULL, nrdn = BER_BVNULL,
2331 psup = BER_BVNULL, nsup = BER_BVNULL;
2332 int rc, deleteOldRdn = 0, freeReqDn = 0;
2333 int do_graduate = 0;
2335 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2336 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2337 "Message type should be entry (%d)",
2338 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2342 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
2347 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2349 if ( rc != LDAP_SUCCESS ) {
2350 Debug( LDAP_DEBUG_ANY,
2351 "syncrepl_message_to_op: %s dn get failed (%d)",
2352 si->si_ridtxt, rc, 0 );
2356 op->o_tag = LBER_DEFAULT;
2357 op->o_bd = si->si_wbe;
2359 if ( BER_BVISEMPTY( &bdn )) {
2360 Debug( LDAP_DEBUG_ANY,
2361 "syncrepl_message_to_op: %s got empty dn",
2362 si->si_ridtxt, 0, 0 );
2366 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
2368 if ( bv.bv_val == NULL )
2371 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
2373 REWRITE_DN( si, bdn, bv2, dn, ndn );
2374 if ( rc != LDAP_SUCCESS ) {
2375 Debug( LDAP_DEBUG_ANY,
2376 "syncrepl_message_to_op: %s "
2377 "dn \"%s\" normalization failed (%d)",
2378 si->si_ridtxt, bdn.bv_val, rc );
2383 ber_dupbv( &op->o_req_dn, &dn );
2384 ber_dupbv( &op->o_req_ndn, &ndn );
2385 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2386 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2388 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
2389 int i = verb_to_mask( bvals[0].bv_val, modops );
2391 Debug( LDAP_DEBUG_ANY,
2392 "syncrepl_message_to_op: %s unknown op %s",
2393 si->si_ridtxt, bvals[0].bv_val, 0 );
2398 op->o_tag = modops[i].mask;
2399 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
2400 /* Parse attribute into modlist */
2401 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
2402 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
2404 rc = syncrepl_changelog_mods( si, bvals, &modlist );
2406 if ( rc ) goto done;
2407 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
2409 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
2410 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
2413 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
2415 } else if ( !ber_bvstrcasecmp( &bv,
2416 &slap_schema.si_ad_entryCSN->ad_cname ) )
2418 slap_queue_csn( op, bvals );
2424 /* If we didn't get a mod type or a target DN, bail out */
2425 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
2430 op->o_callback = &cb;
2431 slap_op_time( &op->o_time, &op->o_tincr );
2433 switch( op->o_tag ) {
2435 case LDAP_REQ_MODIFY:
2436 /* If we didn't get required data, bail */
2437 if ( !modlist ) goto done;
2439 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
2441 if ( rc != LDAP_SUCCESS ) {
2442 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2443 "mods check (%s)\n",
2444 si->si_ridtxt, text, 0 );
2448 if ( op->o_tag == LDAP_REQ_ADD ) {
2449 Entry *e = entry_alloc();
2451 op->ora_e->e_name = op->o_req_dn;
2452 op->ora_e->e_nname = op->o_req_ndn;
2454 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
2455 if( rc != LDAP_SUCCESS ) {
2456 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2457 "mods2entry (%s)\n",
2458 si->si_ridtxt, text, 0 );
2460 rc = op->o_bd->be_add( op, &rs );
2461 Debug( LDAP_DEBUG_SYNC,
2462 "syncrepl_message_to_op: %s be_add %s (%d)\n",
2463 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2466 if ( e == op->ora_e )
2467 be_entry_release_w( op, op->ora_e );
2470 op->orm_modlist = modlist;
2471 op->o_bd = si->si_wbe;
2472 /* delta-mmr needs additional checks in syncrepl_op_modify */
2473 if ( SLAP_MULTIMASTER( op->o_bd )) {
2474 oes.oe.oe_key = (void *)syncrepl_message_to_op;
2476 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &oes.oe, oe_next );
2478 rc = op->o_bd->be_modify( op, &rs );
2479 if ( SLAP_MULTIMASTER( op->o_bd )) {
2480 LDAP_SLIST_REMOVE( &op->o_extra, &oes.oe, OpExtra, oe_next );
2481 BER_BVZERO( &op->o_csn );
2483 modlist = op->orm_modlist;
2484 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2485 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
2486 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2487 op->o_bd = si->si_be;
2491 case LDAP_REQ_MODRDN:
2492 if ( BER_BVISNULL( &rdn ) ) goto done;
2494 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
2497 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
2500 if ( !BER_BVISNULL( &sup ) ) {
2501 REWRITE_DN( si, sup, bv2, psup, nsup );
2504 op->orr_newSup = &psup;
2505 op->orr_nnewSup = ⊅
2507 op->orr_newSup = NULL;
2508 op->orr_nnewSup = NULL;
2510 op->orr_newrdn = prdn;
2511 op->orr_nnewrdn = nrdn;
2512 op->orr_deleteoldrdn = deleteOldRdn;
2513 op->orr_modlist = NULL;
2514 if ( slap_modrdn2mods( op, &rs ) ) {
2518 /* Append modlist for operational attrs */
2522 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
2524 m->sml_next = modlist;
2527 rc = op->o_bd->be_modrdn( op, &rs );
2528 slap_mods_free( op->orr_modlist, 1 );
2529 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2530 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
2531 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2534 case LDAP_REQ_DELETE:
2535 rc = op->o_bd->be_delete( op, &rs );
2536 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2537 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
2538 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2544 slap_graduate_commit_csn( op );
2545 op->o_bd = si->si_be;
2546 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2547 BER_BVZERO( &op->o_csn );
2549 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
2551 if ( !BER_BVISNULL( &rdn ) ) {
2552 if ( !BER_BVISNULL( &nsup ) ) {
2553 ch_free( nsup.bv_val );
2555 if ( !BER_BVISNULL( &psup ) ) {
2556 ch_free( psup.bv_val );
2558 if ( !BER_BVISNULL( &nrdn ) ) {
2559 ch_free( nrdn.bv_val );
2561 if ( !BER_BVISNULL( &prdn ) ) {
2562 ch_free( prdn.bv_val );
2566 ch_free( op->o_req_ndn.bv_val );
2567 ch_free( op->o_req_dn.bv_val );
2574 syncrepl_message_to_entry(
2578 Modifications **modlist,
2581 struct berval *syncUUID
2585 BerElement *ber = NULL;
2588 Modifications **modtail = modlist;
2591 char txtbuf[SLAP_TEXT_BUFLEN];
2592 size_t textlen = sizeof txtbuf;
2594 struct berval bdn = BER_BVNULL, dn, ndn, bv2;
2599 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2600 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
2601 "Message type should be entry (%d)",
2602 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2606 op->o_tag = LDAP_REQ_ADD;
2608 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2609 if ( rc != LDAP_SUCCESS ) {
2610 Debug( LDAP_DEBUG_ANY,
2611 "syncrepl_message_to_entry: %s dn get failed (%d)",
2612 si->si_ridtxt, rc, 0 );
2616 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
2617 Debug( LDAP_DEBUG_ANY,
2618 "syncrepl_message_to_entry: %s got empty dn",
2619 si->si_ridtxt, 0, 0 );
2623 /* syncUUID[0] is normalized UUID received over the wire
2624 * syncUUID[1] is denormalized UUID, generated here
2626 (void)slap_uuidstr_from_normalized( &syncUUID[1], &syncUUID[0], op->o_tmpmemctx );
2627 Debug( LDAP_DEBUG_SYNC,
2628 "syncrepl_message_to_entry: %s DN: %s, UUID: %s\n",
2629 si->si_ridtxt, bdn.bv_val, syncUUID[1].bv_val );
2631 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
2632 /* NOTE: this could be done even before decoding the DN,
2633 * although encoding errors wouldn't be detected */
2638 if ( entry == NULL ) {
2642 REWRITE_DN( si, bdn, bv2, dn, ndn );
2643 if ( rc != LDAP_SUCCESS ) {
2644 /* One of the things that could happen is that the schema
2645 * is not lined-up; this could result in unknown attributes.
2646 * A value non conformant to the syntax should be unlikely,
2647 * except when replicating between different versions
2648 * of the software, or when syntax validation bugs are fixed
2650 Debug( LDAP_DEBUG_ANY,
2651 "syncrepl_message_to_entry: "
2652 "%s dn \"%s\" normalization failed (%d)",
2653 si->si_ridtxt, bdn.bv_val, rc );
2657 ber_dupbv( &op->o_req_dn, &dn );
2658 ber_dupbv( &op->o_req_ndn, &ndn );
2659 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2660 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2662 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
2665 e->e_name = op->o_req_dn;
2666 e->e_nname = op->o_req_ndn;
2668 while ( ber_remaining( ber ) ) {
2669 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
2670 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2675 /* Drop all updates to the contextCSN of the context entry
2678 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2679 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2680 ber_bvarray_free( tmp.sml_values );
2684 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2686 mod->sml_op = LDAP_MOD_REPLACE;
2688 mod->sml_next = NULL;
2689 mod->sml_desc = NULL;
2690 mod->sml_type = tmp.sml_type;
2691 mod->sml_values = tmp.sml_values;
2692 mod->sml_nvalues = NULL;
2693 mod->sml_numvals = 0; /* slap_mods_check will set this */
2695 #ifdef ENABLE_REWRITE
2696 if (si->si_rewrite) {
2697 AttributeDescription *ad = NULL;
2698 slap_bv2ad( &tmp.sml_type, &ad, &text );
2701 mod->sml_type = ad->ad_cname;
2702 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
2704 for ( i = 0; tmp.sml_values[i].bv_val; i++ ) {
2705 syncrepl_rewrite_dn( si, &tmp.sml_values[i], &bv2);
2706 if ( !BER_BVISNULL( &bv2 )) {
2707 ber_memfree( tmp.sml_values[i].bv_val );
2708 tmp.sml_values[i] = bv2;
2716 modtail = &mod->sml_next;
2719 if ( *modlist == NULL ) {
2720 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2721 si->si_ridtxt, 0, 0 );
2726 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2728 if ( rc != LDAP_SUCCESS ) {
2729 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2730 si->si_ridtxt, text, 0 );
2734 /* Strip out dynamically generated attrs */
2735 for ( modtail = modlist; *modtail ; ) {
2737 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2738 *modtail = mod->sml_next;
2739 slap_mod_free( &mod->sml_mod, 0 );
2742 modtail = &mod->sml_next;
2746 /* Strip out attrs in exattrs list */
2747 for ( modtail = modlist; *modtail ; ) {
2749 if ( ldap_charray_inlist( si->si_exattrs,
2750 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2752 *modtail = mod->sml_next;
2753 slap_mod_free( &mod->sml_mod, 0 );
2756 modtail = &mod->sml_next;
2760 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2761 if( rc != LDAP_SUCCESS ) {
2762 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2763 si->si_ridtxt, text, 0 );
2768 if ( rc != LDAP_SUCCESS ) {
2780 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2782 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2783 * entry if a previous refresh was interrupted before sending us a new
2784 * context state. We try to compare the new entry to the existing entry
2785 * and ignore the new entry if they are the same.
2787 * Also, we may get an update where the entryDN has changed, due to
2788 * a ModDn on the provider. We detect this as well, so we can issue
2789 * the corresponding operation locally.
2791 * In the case of a modify, we get a list of all the attributes
2792 * in the original entry. Rather than deleting the entry and re-adding it,
2793 * we issue a Modify request that deletes all the attributes and adds all
2794 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2797 * We otherwise distinguish ModDN from Modify; in the case of
2798 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2799 * operational attributes from the entry, and do a regular ModDN.
2801 typedef struct dninfo {
2805 struct berval nnewSup;
2806 int renamed; /* Was an existing entry renamed? */
2807 int delOldRDN; /* Was old RDN deleted? */
2808 Modifications **modlist; /* the modlist we received */
2809 Modifications *mods; /* the modlist we compared */
2810 int oldNcount; /* #values of old naming attr */
2811 AttributeDescription *oldDesc; /* for renames */
2812 AttributeDescription *newDesc; /* for renames */
2817 /* return 1 if inserted, 0 otherwise */
2821 struct berval *syncUUID )
2829 if ( !si->si_presentlist )
2830 si->si_presentlist = ch_calloc(65536, sizeof( Avlnode * ));
2832 av = (Avlnode **)si->si_presentlist;
2834 val = ch_malloc(UUIDLEN-2);
2835 memcpy(&s, syncUUID->bv_val, 2);
2836 memcpy(val, syncUUID->bv_val+2, UUIDLEN-2);
2838 if ( avl_insert( &av[s], val,
2839 syncuuid_cmp, avl_dup_error ) )
2845 val = ch_malloc(UUIDLEN);
2847 AC_MEMCPY( val, syncUUID->bv_val, UUIDLEN );
2849 if ( avl_insert( &si->si_presentlist, val,
2850 syncuuid_cmp, avl_dup_error ) )
2863 struct berval *val )
2866 Avlnode **a2 = (Avlnode **)av;
2872 memcpy(&s, val->bv_val, 2);
2873 return avl_find( a2[s], val->bv_val+2, syncuuid_cmp );
2875 return avl_find( av, val->bv_val, syncuuid_cmp );
2880 presentlist_free( Avlnode *av )
2883 Avlnode **a2 = (Avlnode **)av;
2887 for (i=0; i<65536; i++) {
2889 count += avl_free( a2[i], ch_free );
2895 return avl_free( av, ch_free );
2902 struct berval *val )
2905 Avlnode **a2 = *(Avlnode ***)av;
2908 memcpy(&s, val->bv_val, 2);
2909 avl_delete( &a2[s], val->bv_val+2, syncuuid_cmp );
2911 avl_delete( av, val->bv_val, syncuuid_cmp );
2920 Modifications** modlist,
2922 struct berval* syncUUID,
2923 struct berval* syncCSN )
2925 Backend *be = op->o_bd;
2926 slap_callback cb = { NULL, NULL, NULL, NULL };
2927 int syncuuid_inserted = 0;
2929 SlapReply rs_search = {REP_RESULT};
2931 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2932 int rc = LDAP_SUCCESS;
2934 struct berval pdn = BER_BVNULL;
2939 Debug( LDAP_DEBUG_SYNC,
2940 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2941 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2943 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2944 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2945 syncuuid_inserted = presentlist_insert( si, syncUUID );
2949 if ( syncstate == LDAP_SYNC_PRESENT ) {
2951 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2952 if ( entry == NULL ) {
2957 if ( syncstate != LDAP_SYNC_DELETE ) {
2958 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2961 /* add if missing */
2962 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2963 &syncUUID[1], syncUUID );
2965 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2966 /* replace only if necessary */
2967 if ( a->a_nvals != a->a_vals ) {
2968 ber_memfree( a->a_nvals[0].bv_val );
2969 ber_dupbv( &a->a_nvals[0], syncUUID );
2971 ber_memfree( a->a_vals[0].bv_val );
2972 ber_dupbv( &a->a_vals[0], &syncUUID[1] );
2976 f.f_choice = LDAP_FILTER_EQUALITY;
2978 ava.aa_desc = slap_schema.si_ad_entryUUID;
2979 ava.aa_value = *syncUUID;
2981 if ( syncuuid_inserted ) {
2982 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2983 si->si_ridtxt, syncUUID[1].bv_val, 0 );
2985 op->ors_filter = &f;
2987 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID[1].bv_len;
2988 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2989 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2990 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2991 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2992 syncUUID[1].bv_val, syncUUID[1].bv_len );
2993 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2994 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2996 op->o_tag = LDAP_REQ_SEARCH;
2997 op->ors_scope = LDAP_SCOPE_SUBTREE;
2998 op->ors_deref = LDAP_DEREF_NEVER;
3000 /* get the entry for this UUID */
3001 #ifdef ENABLE_REWRITE
3002 if ( si->si_rewrite ) {
3003 op->o_req_dn = si->si_suffixm;
3004 op->o_req_ndn = si->si_suffixm;
3008 op->o_req_dn = si->si_base;
3009 op->o_req_ndn = si->si_base;
3012 op->o_time = slap_get_time();
3013 op->ors_tlimit = SLAP_NO_LIMIT;
3015 op->ors_limit = NULL;
3017 op->ors_attrs = slap_anlist_all_attributes;
3018 op->ors_attrsonly = 0;
3020 /* set callback function */
3021 op->o_callback = &cb;
3022 cb.sc_response = dn_callback;
3023 cb.sc_private = &dni;
3024 dni.new_entry = entry;
3025 dni.modlist = modlist;
3027 rc = be->be_search( op, &rs_search );
3028 Debug( LDAP_DEBUG_SYNC,
3029 "syncrepl_entry: %s be_search (%d)\n",
3030 si->si_ridtxt, rc, 0 );
3032 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
3033 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3036 cb.sc_response = syncrepl_null_callback;
3039 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
3040 Debug( LDAP_DEBUG_SYNC,
3041 "syncrepl_entry: %s %s\n",
3042 si->si_ridtxt, entry->e_name.bv_val, 0 );
3044 Debug( LDAP_DEBUG_SYNC,
3045 "syncrepl_entry: %s %s\n",
3046 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
3049 assert( BER_BVISNULL( &op->o_csn ) );
3051 slap_queue_csn( op, syncCSN );
3054 if ( !si->si_refreshDone ) {
3055 if ( si->si_lazyCommit )
3056 op->o_lazyCommit = SLAP_CONTROL_NONCRITICAL;
3057 if ( si->si_refreshCount == 500 ) {
3058 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
3059 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
3060 si->si_refreshCount = 0;
3061 si->si_refreshTxn = NULL;
3063 if ( op->o_bd->bd_info->bi_op_txn ) {
3064 if ( !si->si_refreshCount ) {
3065 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_BEGIN, &si->si_refreshTxn );
3067 si->si_refreshCount++;
3071 slap_op_time( &op->o_time, &op->o_tincr );
3072 switch ( syncstate ) {
3074 case LDAP_SYNC_MODIFY:
3075 if ( BER_BVISNULL( &op->o_csn ))
3078 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
3080 /* FIXME: op->o_csn is assumed to be
3081 * on the thread's slab; this needs
3082 * to be cleared ASAP.
3084 op->o_csn = a->a_vals[0];
3089 if ( BER_BVISNULL( &dni.dn ) ) {
3090 SlapReply rs_add = {REP_RESULT};
3092 op->o_req_dn = entry->e_name;
3093 op->o_req_ndn = entry->e_nname;
3094 op->o_tag = LDAP_REQ_ADD;
3096 op->o_bd = si->si_wbe;
3098 rc = op->o_bd->be_add( op, &rs_add );
3099 Debug( LDAP_DEBUG_SYNC,
3100 "syncrepl_entry: %s be_add %s (%d)\n",
3101 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3102 switch ( rs_add.sr_err ) {
3104 if ( op->ora_e == entry ) {
3105 be_entry_release_w( op, entry );
3111 /* we assume that LDAP_NO_SUCH_OBJECT is returned
3112 * only if the suffix entry is not present.
3113 * This should not happen during Persist phase.
3115 case LDAP_NO_SUCH_OBJECT:
3116 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
3117 si->si_refreshDone ) {
3118 /* Something's wrong, start over */
3119 ber_bvarray_free( si->si_syncCookie.ctxcsn );
3120 si->si_syncCookie.ctxcsn = NULL;
3121 entry_free( entry );
3122 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3123 ber_bvarray_free( si->si_cookieState->cs_vals );
3124 ch_free( si->si_cookieState->cs_sids );
3125 si->si_cookieState->cs_vals = NULL;
3126 si->si_cookieState->cs_sids = 0;
3127 si->si_cookieState->cs_num = 0;
3128 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3129 return LDAP_NO_SUCH_OBJECT;
3131 rc = syncrepl_add_glue( op, entry );
3135 /* if an entry was added via syncrepl_add_glue(),
3136 * it likely has no entryUUID, so the previous
3137 * be_search() doesn't find it. In this case,
3138 * give syncrepl a chance to modify it. Also
3139 * allow for entries that were recreated with the
3140 * same DN but a different entryUUID.
3142 case LDAP_ALREADY_EXISTS:
3144 Operation op2 = *op;
3145 SlapReply rs2 = { REP_RESULT };
3146 slap_callback cb2 = { 0 };
3149 op2.o_tag = LDAP_REQ_SEARCH;
3150 op2.o_req_dn = entry->e_name;
3151 op2.o_req_ndn = entry->e_nname;
3152 op2.ors_scope = LDAP_SCOPE_BASE;
3153 op2.ors_deref = LDAP_DEREF_NEVER;
3154 op2.ors_attrs = slap_anlist_all_attributes;
3155 op2.ors_attrsonly = 0;
3156 op2.ors_limit = NULL;
3158 op2.ors_tlimit = SLAP_NO_LIMIT;
3160 f.f_choice = LDAP_FILTER_PRESENT;
3161 f.f_desc = slap_schema.si_ad_objectClass;
3162 op2.ors_filter = &f;
3163 op2.ors_filterstr = generic_filterstr;
3165 op2.o_callback = &cb2;
3166 cb2.sc_response = dn_callback;
3167 cb2.sc_private = &dni;
3169 rc = be->be_search( &op2, &rs2 );
3170 if ( rc ) goto done;
3173 slap_op_time( &op->o_time, &op->o_tincr );
3179 Debug( LDAP_DEBUG_ANY,
3180 "syncrepl_entry: %s be_add %s failed (%d)\n",
3181 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
3189 op->o_req_dn = dni.dn;
3190 op->o_req_ndn = dni.ndn;
3191 if ( dni.renamed ) {
3192 struct berval noldp, newp;
3193 Modifications *mod, **modtail, **ml, *m2;
3194 int i, got_replace = 0, just_rename = 0;
3195 SlapReply rs_modify = {REP_RESULT};
3197 op->o_tag = LDAP_REQ_MODRDN;
3198 dnRdn( &entry->e_name, &op->orr_newrdn );
3199 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
3201 if ( !BER_BVISNULL( &dni.nnewSup )) {
3202 dnParent( &entry->e_name, &newp );
3203 op->orr_newSup = &newp;
3204 op->orr_nnewSup = &dni.nnewSup;
3206 op->orr_newSup = NULL;
3207 op->orr_nnewSup = NULL;
3209 op->orr_deleteoldrdn = dni.delOldRDN;
3210 op->orr_modlist = NULL;
3211 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
3215 /* Drop the RDN-related mods from this op, because their
3216 * equivalents were just setup by slap_modrdn2mods.
3218 * If delOldRDN is TRUE then we should see a delete modop
3219 * for oldDesc. We might see a replace instead.
3220 * delete with no values: therefore newDesc != oldDesc.
3221 * if oldNcount == 1, then Drop this op.
3222 * delete with 1 value: can only be the oldRDN value. Drop op.
3223 * delete with N values: Drop oldRDN value, keep remainder.
3224 * replace with 1 value: if oldNcount == 1 and
3225 * newDesc == oldDesc, Drop this op.
3226 * Any other cases must be left intact.
3228 * We should also see an add modop for newDesc. (But not if
3229 * we got a replace modop due to delOldRDN.) If it has
3230 * multiple values, we'll have to drop the new RDN value.
3232 modtail = &op->orr_modlist;
3233 if ( dni.delOldRDN ) {
3234 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3235 if ( (*ml)->sml_desc == dni.oldDesc ) {
3237 if ( mod->sml_op == LDAP_MOD_REPLACE &&
3238 dni.oldDesc != dni.newDesc ) {
3239 /* This Replace is due to other Mods.
3244 if ( mod->sml_numvals <= 1 &&
3245 dni.oldNcount == 1 &&
3246 ( mod->sml_op == LDAP_MOD_DELETE ||
3247 mod->sml_op == LDAP_MOD_REPLACE )) {
3248 if ( mod->sml_op == LDAP_MOD_REPLACE )
3251 *ml = mod->sml_next;
3252 mod->sml_next = NULL;
3253 slap_mods_free( mod, 1 );
3256 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
3258 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3259 if ( m2->sml_desc == dni.oldDesc &&
3260 m2->sml_op == LDAP_MOD_DELETE ) break;
3262 for ( i=0; i<mod->sml_numvals; i++ ) {
3263 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3265 ch_free( mod->sml_values[i].bv_val );
3266 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3267 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3268 if ( mod->sml_nvalues ) {
3269 ch_free( mod->sml_nvalues[i].bv_val );
3270 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3271 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3276 if ( !mod->sml_numvals ) {
3278 *ml = mod->sml_next;
3279 mod->sml_next = NULL;
3280 slap_mods_free( mod, 1 );
3286 if ( !got_replace ) {
3287 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3288 if ( (*ml)->sml_desc == dni.newDesc ) {
3290 if ( mod->sml_op != LDAP_MOD_ADD )
3292 if ( mod->sml_numvals == 1 ) {
3294 *ml = mod->sml_next;
3295 mod->sml_next = NULL;
3296 slap_mods_free( mod, 1 );
3299 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3300 if ( m2->sml_desc == dni.oldDesc &&
3301 m2->sml_op == SLAP_MOD_SOFTADD ) break;
3303 for ( i=0; i<mod->sml_numvals; i++ ) {
3304 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3306 ch_free( mod->sml_values[i].bv_val );
3307 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3308 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3309 if ( mod->sml_nvalues ) {
3310 ch_free( mod->sml_nvalues[i].bv_val );
3311 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3312 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3322 /* RDNs must be NUL-terminated for back-ldap */
3323 noldp = op->orr_newrdn;
3324 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
3325 noldp = op->orr_nnewrdn;
3326 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
3328 /* Setup opattrs too */
3330 static AttributeDescription *nullattr = NULL;
3331 static AttributeDescription **const opattrs[] = {
3332 &slap_schema.si_ad_entryCSN,
3333 &slap_schema.si_ad_modifiersName,
3334 &slap_schema.si_ad_modifyTimestamp,
3337 AttributeDescription *opattr;
3341 /* pull mod off incoming modlist */
3342 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
3343 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
3345 if ( (*ml)->sml_desc == opattr ) {
3347 *ml = mod->sml_next;
3348 mod->sml_next = NULL;
3350 modtail = &mod->sml_next;
3355 /* If there are still Modifications left, put the opattrs
3356 * back, and let be_modify run. Otherwise, append the opattrs
3357 * to the orr_modlist.
3361 /* don't set a CSN for the rename op */
3363 slap_graduate_commit_csn( op );
3365 mod = op->orr_modlist;
3368 for ( ; mod->sml_next; mod=mod->sml_next );
3371 op->o_bd = si->si_wbe;
3373 rs_reinit( &rs_modify, REP_RESULT );
3374 rc = op->o_bd->be_modrdn( op, &rs_modify );
3376 /* NOTE: noSuchObject should result because the new superior
3377 * has not been added yet (ITS#6472) */
3378 if ( rc == LDAP_NO_SUCH_OBJECT && op->orr_nnewSup != NULL ) {
3379 Operation op2 = *op;
3380 rc = syncrepl_add_glue_ancestors( &op2, entry );
3381 if ( rc == LDAP_SUCCESS ) {
3386 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
3387 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
3389 slap_mods_free( op->orr_modlist, 1 );
3390 Debug( LDAP_DEBUG_SYNC,
3391 "syncrepl_entry: %s be_modrdn %s (%d)\n",
3392 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3394 /* Renamed entries may still have other mods so just fallthru */
3395 op->o_req_dn = entry->e_name;
3396 op->o_req_ndn = entry->e_nname;
3397 /* Use CSN on the modify */
3401 slap_queue_csn( op, syncCSN );
3404 SlapReply rs_modify = {REP_RESULT};
3406 op->o_tag = LDAP_REQ_MODIFY;
3407 op->orm_modlist = dni.mods;
3408 op->orm_no_opattrs = 1;
3409 op->o_bd = si->si_wbe;
3411 rc = op->o_bd->be_modify( op, &rs_modify );
3412 slap_mods_free( op->orm_modlist, 1 );
3413 op->orm_no_opattrs = 0;
3414 Debug( LDAP_DEBUG_SYNC,
3415 "syncrepl_entry: %s be_modify %s (%d)\n",
3416 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3417 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
3418 Debug( LDAP_DEBUG_ANY,
3419 "syncrepl_entry: %s be_modify failed (%d)\n",
3420 si->si_ridtxt, rs_modify.sr_err, 0 );
3424 } else if ( !dni.renamed ) {
3425 Debug( LDAP_DEBUG_SYNC,
3426 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
3427 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
3429 slap_graduate_commit_csn( op );
3434 case LDAP_SYNC_DELETE :
3435 if ( !BER_BVISNULL( &dni.dn ) ) {
3436 SlapReply rs_delete = {REP_RESULT};
3437 op->o_req_dn = dni.dn;
3438 op->o_req_ndn = dni.ndn;
3439 op->o_tag = LDAP_REQ_DELETE;
3440 op->o_bd = si->si_wbe;
3442 slap_queue_csn( op, si->si_syncCookie.ctxcsn );
3444 rc = op->o_bd->be_delete( op, &rs_delete );
3445 Debug( LDAP_DEBUG_SYNC,
3446 "syncrepl_entry: %s be_delete %s (%d)\n",
3447 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3448 if ( rc == LDAP_NO_SUCH_OBJECT )
3451 while ( rs_delete.sr_err == LDAP_SUCCESS
3452 && op->o_delete_glue_parent ) {
3453 op->o_delete_glue_parent = 0;
3454 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3455 slap_callback cb = { NULL };
3456 cb.sc_response = syncrepl_null_callback;
3457 dnParent( &op->o_req_ndn, &pdn );
3459 op->o_req_ndn = pdn;
3460 op->o_callback = &cb;
3461 rs_reinit( &rs_delete, REP_RESULT );
3462 op->o_bd->be_delete( op, &rs_delete );
3473 Debug( LDAP_DEBUG_ANY,
3474 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
3479 slap_sl_free( syncUUID[1].bv_val, op->o_tmpmemctx );
3480 BER_BVZERO( &syncUUID[1] );
3481 if ( !BER_BVISNULL( &dni.ndn ) ) {
3482 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
3484 if ( !BER_BVISNULL( &dni.dn ) ) {
3485 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
3488 entry_free( entry );
3491 slap_graduate_commit_csn( op );
3493 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
3494 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3496 BER_BVZERO( &op->o_csn );
3500 static struct berval gcbva[] = {
3506 #define NP_DELETE_ONE 2
3509 syncrepl_del_nonpresent(
3513 struct sync_cookie *sc,
3516 Backend* be = op->o_bd;
3517 slap_callback cb = { NULL };
3518 struct nonpresent_entry *np_list, *np_prev;
3520 AttributeName an[2];
3522 struct berval pdn = BER_BVNULL;
3525 #ifdef ENABLE_REWRITE
3526 if ( si->si_rewrite ) {
3527 op->o_req_dn = si->si_suffixm;
3528 op->o_req_ndn = si->si_suffixm;
3532 op->o_req_dn = si->si_base;
3533 op->o_req_ndn = si->si_base;
3536 cb.sc_response = nonpresent_callback;
3539 op->o_callback = &cb;
3540 op->o_tag = LDAP_REQ_SEARCH;
3541 op->ors_scope = si->si_scope;
3542 op->ors_deref = LDAP_DEREF_NEVER;
3543 op->o_time = slap_get_time();
3544 op->ors_tlimit = SLAP_NO_LIMIT;
3549 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
3552 op->ors_attrsonly = 1;
3553 op->ors_attrs = slap_anlist_no_attrs;
3554 op->ors_limit = NULL;
3555 op->ors_filter = &uf;
3558 uf.f_av_desc = slap_schema.si_ad_entryUUID;
3560 uf.f_choice = LDAP_FILTER_EQUALITY;
3561 si->si_refreshDelete |= NP_DELETE_ONE;
3563 for (i=0; uuids[i].bv_val; i++) {
3564 SlapReply rs_search = {REP_RESULT};
3567 uf.f_av_value = uuids[i];
3568 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3569 rc = be->be_search( op, &rs_search );
3570 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3572 si->si_refreshDelete ^= NP_DELETE_ONE;
3576 AttributeAssertion mmaa;
3577 SlapReply rs_search = {REP_RESULT};
3579 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
3580 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
3581 an[0].an_desc = slap_schema.si_ad_entryUUID;
3583 op->ors_slimit = SLAP_NO_LIMIT;
3584 op->ors_tlimit = SLAP_NO_LIMIT;
3585 op->ors_limit = NULL;
3586 op->ors_attrsonly = 0;
3587 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
3588 /* In multimaster, updates can continue to arrive while
3589 * we're searching. Limit the search result to entries
3590 * older than our newest cookie CSN.
3592 if ( SLAP_MULTIMASTER( op->o_bd )) {
3597 f->f_choice = LDAP_FILTER_AND;
3598 f->f_next = op->ors_filter;
3602 f->f_choice = LDAP_FILTER_LE;
3604 f->f_av_desc = slap_schema.si_ad_entryCSN;
3606 BER_BVZERO( &f->f_av_value );
3607 for ( i=0; i<sc->numcsns; i++ ) {
3608 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
3609 f->f_av_value = sc->ctxcsn[i];
3611 of = op->ors_filter;
3612 op->ors_filter = mmf;
3613 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3616 op->ors_filterstr = si->si_filterstr;
3618 op->o_nocaching = 1;
3621 rc = be->be_search( op, &rs_search );
3622 if ( SLAP_MULTIMASTER( op->o_bd )) {
3623 op->ors_filter = of;
3625 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
3626 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
3627 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3632 op->o_nocaching = 0;
3634 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
3636 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
3637 csn = sc->ctxcsn[m];
3639 csn = si->si_syncCookie.ctxcsn[0];
3642 op->o_bd = si->si_wbe;
3643 slap_queue_csn( op, &csn );
3645 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
3646 while ( np_list != NULL ) {
3647 SlapReply rs_delete = {REP_RESULT};
3649 LDAP_LIST_REMOVE( np_list, npe_link );
3651 np_list = LDAP_LIST_NEXT( np_list, npe_link );
3652 op->o_tag = LDAP_REQ_DELETE;
3653 op->o_callback = &cb;
3654 cb.sc_response = syncrepl_null_callback;
3656 op->o_req_dn = *np_prev->npe_name;
3657 op->o_req_ndn = *np_prev->npe_nname;
3658 rc = op->o_bd->be_delete( op, &rs_delete );
3659 Debug( LDAP_DEBUG_SYNC,
3660 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
3661 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3663 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
3664 SlapReply rs_modify = {REP_RESULT};
3665 Modifications mod1, mod2;
3666 mod1.sml_op = LDAP_MOD_REPLACE;
3668 mod1.sml_desc = slap_schema.si_ad_objectClass;
3669 mod1.sml_type = mod1.sml_desc->ad_cname;
3670 mod1.sml_numvals = 2;
3671 mod1.sml_values = &gcbva[0];
3672 mod1.sml_nvalues = NULL;
3673 mod1.sml_next = &mod2;
3675 mod2.sml_op = LDAP_MOD_REPLACE;
3677 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
3678 mod2.sml_type = mod2.sml_desc->ad_cname;
3679 mod2.sml_numvals = 1;
3680 mod2.sml_values = &gcbva[1];
3681 mod2.sml_nvalues = NULL;
3682 mod2.sml_next = NULL;
3684 op->o_tag = LDAP_REQ_MODIFY;
3685 op->orm_modlist = &mod1;
3687 rc = op->o_bd->be_modify( op, &rs_modify );
3688 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
3691 while ( rs_delete.sr_err == LDAP_SUCCESS &&
3692 op->o_delete_glue_parent ) {
3693 op->o_delete_glue_parent = 0;
3694 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3695 slap_callback cb = { NULL };
3696 cb.sc_response = syncrepl_null_callback;
3697 dnParent( &op->o_req_ndn, &pdn );
3699 op->o_req_ndn = pdn;
3700 op->o_callback = &cb;
3701 rs_reinit( &rs_delete, REP_RESULT );
3702 /* give it a root privil ? */
3703 op->o_bd->be_delete( op, &rs_delete );
3709 op->o_delete_glue_parent = 0;
3711 ber_bvfree( np_prev->npe_name );
3712 ber_bvfree( np_prev->npe_nname );
3715 if ( slapd_shutdown ) {
3720 slap_graduate_commit_csn( op );
3723 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3724 BER_BVZERO( &op->o_csn );
3731 syncrepl_add_glue_ancestors(
3735 Backend *be = op->o_bd;
3736 slap_callback cb = { NULL };
3738 int rc = LDAP_SUCCESS;
3741 struct berval dn = BER_BVNULL;
3742 struct berval ndn = BER_BVNULL;
3744 struct berval ptr, nptr;
3747 op->o_tag = LDAP_REQ_ADD;
3748 op->o_callback = &cb;
3749 cb.sc_response = syncrepl_null_callback;
3750 cb.sc_private = NULL;
3755 /* count RDNs in suffix */
3756 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3757 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3759 ptr.bv_len -= comma - ptr.bv_val;
3769 /* Start with BE suffix */
3771 for ( i = 0; i < suffrdns; i++ ) {
3772 comma = ber_bvrchr( &ptr, ',' );
3773 if ( comma != NULL ) {
3774 ptr.bv_len = comma - ptr.bv_val;
3781 if ( !BER_BVISEMPTY( &ptr ) ) {
3782 dn.bv_len -= ptr.bv_len + ( suffrdns != 0 );
3783 dn.bv_val += ptr.bv_len + ( suffrdns != 0 );
3786 /* the normalizedDNs are always the same length, no counting
3790 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3791 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3792 ndn.bv_len = be->be_nsuffix[0].bv_len;
3794 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3800 while ( ndn.bv_val > e->e_nname.bv_val ) {
3801 SlapReply rs_add = {REP_RESULT};
3803 glue = entry_alloc();
3804 ber_dupbv( &glue->e_name, &dn );
3805 ber_dupbv( &glue->e_nname, &ndn );
3807 a = attr_alloc( slap_schema.si_ad_objectClass );
3810 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3811 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3812 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3813 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3815 a->a_nvals = a->a_vals;
3817 a->a_next = glue->e_attrs;
3820 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3823 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3824 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3825 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3827 a->a_nvals = a->a_vals;
3829 a->a_next = glue->e_attrs;
3832 op->o_req_dn = glue->e_name;
3833 op->o_req_ndn = glue->e_nname;
3835 rc = be->be_add ( op, &rs_add );
3836 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3837 if ( op->ora_e == glue )
3838 be_entry_release_w( op, glue );
3840 /* incl. ALREADY EXIST */
3842 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3848 /* Move to next child */
3849 comma = ber_bvrchr( &ptr, ',' );
3850 if ( comma == NULL ) {
3853 ptr.bv_len = comma - ptr.bv_val;
3855 dn.bv_val = ++comma;
3856 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3858 comma = ber_bvrchr( &nptr, ',' );
3859 assert( comma != NULL );
3860 nptr.bv_len = comma - nptr.bv_val;
3862 ndn.bv_val = ++comma;
3863 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3874 slap_callback cb = { NULL };
3876 Backend *be = op->o_bd;
3877 SlapReply rs_add = {REP_RESULT};
3879 rc = syncrepl_add_glue_ancestors( op, e );
3882 case LDAP_ALREADY_EXISTS:
3889 op->o_tag = LDAP_REQ_ADD;
3890 op->o_callback = &cb;
3891 cb.sc_response = syncrepl_null_callback;
3892 cb.sc_private = NULL;
3894 op->o_req_dn = e->e_name;
3895 op->o_req_ndn = e->e_nname;
3897 rc = be->be_add ( op, &rs_add );
3898 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3899 if ( op->ora_e == e )
3900 be_entry_release_w( op, e );
3909 syncrepl_updateCookie(
3912 struct sync_cookie *syncCookie )
3914 Backend *be = op->o_bd;
3916 struct berval first = BER_BVNULL;
3917 struct sync_cookie sc;
3919 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3922 int rc, i, j, changed = 0;
3925 slap_callback cb = { NULL };
3926 SlapReply rs_modify = {REP_RESULT};
3928 mod.sml_op = LDAP_MOD_REPLACE;
3929 mod.sml_desc = slap_schema.si_ad_contextCSN;
3930 mod.sml_type = mod.sml_desc->ad_cname;
3931 mod.sml_flags = SLAP_MOD_INTERNAL;
3932 mod.sml_nvalues = NULL;
3933 mod.sml_next = NULL;
3935 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3938 for ( i=0; i<syncCookie->numcsns; i++ ) {
3939 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3941 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3942 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3946 /* clone the cookieState CSNs so we can Replace the whole thing */
3947 sc.numcsns = si->si_cookieState->cs_num;
3949 ber_bvarray_dup_x( &sc.ctxcsn, si->si_cookieState->cs_vals, NULL );
3950 sc.sids = ch_malloc( sc.numcsns * sizeof(int));
3951 for ( i=0; i<sc.numcsns; i++ )
3952 sc.sids[i] = si->si_cookieState->cs_sids[i];
3958 /* find any CSNs in the syncCookie that are newer than the cookieState */
3959 for ( i=0; i<syncCookie->numcsns; i++ ) {
3960 for ( j=0; j<sc.numcsns; j++ ) {
3961 if ( syncCookie->sids[i] < sc.sids[j] )
3963 if ( syncCookie->sids[i] != sc.sids[j] )
3965 len = syncCookie->ctxcsn[i].bv_len;
3966 if ( len > sc.ctxcsn[j].bv_len )
3967 len = sc.ctxcsn[j].bv_len;
3968 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3969 sc.ctxcsn[j].bv_val, len ) > 0 ) {
3970 ber_bvreplace( &sc.ctxcsn[j], &syncCookie->ctxcsn[i] );
3972 if ( BER_BVISNULL( &first ) ||
3973 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3974 first = syncCookie->ctxcsn[i];
3979 /* there was no match for this SID, it's a new CSN */
3980 if ( j == sc.numcsns ||
3981 syncCookie->sids[i] != sc.sids[j] ) {
3982 slap_insert_csn_sids( &sc, j, syncCookie->sids[i],
3983 &syncCookie->ctxcsn[i] );
3984 if ( BER_BVISNULL( &first ) ||
3985 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3986 first = syncCookie->ctxcsn[i];
3991 /* Should never happen, ITS#5065 */
3992 if ( BER_BVISNULL( &first ) || !changed ) {
3993 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3994 ber_bvarray_free( sc.ctxcsn );
3998 op->o_bd = si->si_wbe;
3999 slap_queue_csn( op, &first );
4001 op->o_tag = LDAP_REQ_MODIFY;
4003 cb.sc_response = syncrepl_null_callback;
4006 op->o_callback = &cb;
4007 op->o_req_dn = si->si_contextdn;
4008 op->o_req_ndn = si->si_contextdn;
4010 /* update contextCSN */
4011 op->o_dont_replicate = 1;
4013 mod.sml_numvals = sc.numcsns;
4014 mod.sml_values = sc.ctxcsn;
4016 op->orm_modlist = &mod;
4017 op->orm_no_opattrs = 1;
4018 rc = op->o_bd->be_modify( op, &rs_modify );
4020 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
4021 SLAP_SYNC_SUBENTRY( op->o_bd )) {
4023 char txtbuf[SLAP_TEXT_BUFLEN];
4024 size_t textlen = sizeof txtbuf;
4025 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
4026 rs_reinit( &rs_modify, REP_RESULT );
4027 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
4029 rc = op->o_bd->be_add( op, &rs_modify );
4030 if ( e == op->ora_e )
4031 be_entry_release_w( op, op->ora_e );
4034 op->orm_no_opattrs = 0;
4035 op->o_dont_replicate = 0;
4037 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
4038 slap_sync_cookie_free( &si->si_syncCookie, 0 );
4039 ber_bvarray_free( si->si_cookieState->cs_vals );
4040 ch_free( si->si_cookieState->cs_sids );
4041 si->si_cookieState->cs_vals = sc.ctxcsn;
4042 si->si_cookieState->cs_sids = sc.sids;
4043 si->si_cookieState->cs_num = sc.numcsns;
4045 /* Don't just dup the provider's cookie, recreate it */
4046 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
4047 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn, si->si_cookieState->cs_vals, NULL );
4048 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num * sizeof(int) );
4049 for ( i=0; i<si->si_cookieState->cs_num; i++ )
4050 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
4052 si->si_cookieState->cs_age++;
4053 si->si_cookieAge = si->si_cookieState->cs_age;
4055 Debug( LDAP_DEBUG_ANY,
4056 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
4057 si->si_ridtxt, rs_modify.sr_err, 0 );
4059 ber_bvarray_free( sc.ctxcsn );
4063 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
4064 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
4068 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
4071 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
4072 BER_BVZERO( &op->o_csn );
4073 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
4078 /* Compare the attribute from the old entry to the one in the new
4079 * entry. The Modifications from the new entry will either be left
4080 * in place, or changed to an Add or Delete as needed.
4083 attr_cmp( Operation *op, Attribute *old, Attribute *new,
4084 Modifications ***mret, Modifications ***mcur )
4087 Modifications *mod, **modtail;
4093 struct berval **adds, **dels;
4094 /* count old and new */
4095 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
4096 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
4098 /* there MUST be both old and new values */
4103 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
4104 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
4106 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
4107 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
4111 for ( i=0; i<o; i++ ) {
4112 for ( j=0; j<n; j++ ) {
4115 if ( bvmatch( dels[i], adds[j] ) ) {
4125 /* Don't delete/add an objectClass, always use the replace op.
4126 * Modify would fail if provider has replaced entry with a new,
4127 * and the new explicitly includes a superior of a class that was
4128 * only included implicitly in the old entry. Ref ITS#5517.
4130 * Also use replace op if attr has no equality matching rule.
4133 if ( ( nn || ( no > 0 && no < o ) ) &&
4134 ( old->a_desc == slap_schema.si_ad_objectClass ||
4135 !old->a_desc->ad_type->sat_equality ) )
4141 /* all old values were deleted, just use the replace op */
4145 /* delete some values */
4146 mod = ch_malloc( sizeof( Modifications ) );
4147 mod->sml_op = LDAP_MOD_DELETE;
4149 mod->sml_desc = old->a_desc;
4150 mod->sml_type = mod->sml_desc->ad_cname;
4151 mod->sml_numvals = no;
4152 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4153 if ( old->a_vals != old->a_nvals ) {
4154 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4156 mod->sml_nvalues = NULL;
4159 for ( i = 0; i < o; i++ ) {
4160 if ( !dels[i] ) continue;
4161 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
4162 if ( mod->sml_nvalues ) {
4163 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
4167 BER_BVZERO( &mod->sml_values[j] );
4168 if ( mod->sml_nvalues ) {
4169 BER_BVZERO( &mod->sml_nvalues[j] );
4172 modtail = &mod->sml_next;
4175 op->o_tmpfree( dels, op->o_tmpmemctx );
4176 /* some values were added */
4177 if ( nn && no < o ) {
4178 mod = ch_malloc( sizeof( Modifications ) );
4179 mod->sml_op = LDAP_MOD_ADD;
4181 mod->sml_desc = old->a_desc;
4182 mod->sml_type = mod->sml_desc->ad_cname;
4183 mod->sml_numvals = nn;
4184 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4185 if ( old->a_vals != old->a_nvals ) {
4186 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4188 mod->sml_nvalues = NULL;
4191 for ( i = 0; i < n; i++ ) {
4192 if ( !adds[i] ) continue;
4193 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
4194 if ( mod->sml_nvalues ) {
4195 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
4199 BER_BVZERO( &mod->sml_values[j] );
4200 if ( mod->sml_nvalues ) {
4201 BER_BVZERO( &mod->sml_nvalues[j] );
4204 modtail = &mod->sml_next;
4207 op->o_tmpfree( adds, op->o_tmpmemctx );
4209 /* new attr, just use the new mod */
4213 /* advance to next element */
4217 **mcur = mod->sml_next;
4219 modtail = &mod->sml_next;
4221 *mcur = &mod->sml_next;
4227 /* Generate a set of modifications to change the old entry into the
4228 * new one. On input ml is a list of modifications equivalent to
4229 * the new entry. It will be massaged and the result will be stored
4232 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
4233 Modifications **mods, Modifications **ml, int is_ctx)
4235 Modifications **modtail = mods;
4237 /* We assume that attributes are saved in the same order
4238 * in the remote and local databases. So if we walk through
4239 * the attributeDescriptions one by one they should match in
4240 * lock step. If not, look for an add or delete.
4242 while ( old && new )
4244 /* If we've seen this before, use its mod now */
4245 if ( new->a_flags & SLAP_ATTR_IXADD ) {
4246 attr_cmp( op, NULL, new, &modtail, &ml );
4250 /* Skip contextCSN */
4251 if ( is_ctx && old->a_desc ==
4252 slap_schema.si_ad_contextCSN ) {
4257 if ( old->a_desc != new->a_desc ) {
4261 /* If it's just been re-added later,
4262 * remember that we've seen it.
4264 tmp = attr_find( new, old->a_desc );
4266 tmp->a_flags |= SLAP_ATTR_IXADD;
4268 /* If it's a new attribute, pull it in.
4270 tmp = attr_find( old, new->a_desc );
4272 attr_cmp( op, NULL, new, &modtail, &ml );
4276 /* Delete old attr */
4277 mod = ch_malloc( sizeof( Modifications ) );
4278 mod->sml_op = LDAP_MOD_DELETE;
4280 mod->sml_desc = old->a_desc;
4281 mod->sml_type = mod->sml_desc->ad_cname;
4282 mod->sml_numvals = 0;
4283 mod->sml_values = NULL;
4284 mod->sml_nvalues = NULL;
4286 modtail = &mod->sml_next;
4291 /* kludge - always update modifiersName so that it
4292 * stays co-located with the other mod opattrs. But only
4293 * if we know there are other valid mods.
4295 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
4296 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
4297 attr_cmp( op, NULL, new, &modtail, &ml );
4299 attr_cmp( op, old, new, &modtail, &ml );
4312 dninfo *dni = op->o_callback->sc_private;
4314 if ( rs->sr_type == REP_SEARCH ) {
4315 if ( !BER_BVISNULL( &dni->dn ) ) {
4316 Debug( LDAP_DEBUG_ANY,
4317 "dn_callback : consistency error - "
4318 "entryUUID is not unique\n", 0, 0, 0 );
4320 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
4321 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
4322 /* If there is a new entry, see if it differs from the old.
4323 * We compare the non-normalized values so that cosmetic changes
4324 * in the provider are always propagated.
4326 if ( dni->new_entry ) {
4327 Attribute *old, *new;
4328 struct berval old_rdn, new_rdn;
4329 struct berval old_p, new_p;
4330 int is_ctx, new_sup = 0;
4332 /* If old entry is not a glue entry, make sure new entry
4333 * is actually newer than old entry
4335 if ( !is_entry_glue( rs->sr_entry )) {
4336 old = attr_find( rs->sr_entry->e_attrs,
4337 slap_schema.si_ad_entryCSN );
4338 new = attr_find( dni->new_entry->e_attrs,
4339 slap_schema.si_ad_entryCSN );
4342 ber_len_t len = old->a_vals[0].bv_len;
4343 if ( len > new->a_vals[0].bv_len )
4344 len = new->a_vals[0].bv_len;
4345 rc = memcmp( old->a_vals[0].bv_val,
4346 new->a_vals[0].bv_val, len );
4348 Debug( LDAP_DEBUG_SYNC,
4349 "dn_callback : new entry is older than ours "
4350 "%s ours %s, new %s\n",
4351 rs->sr_entry->e_name.bv_val,
4352 old->a_vals[0].bv_val,
4353 new->a_vals[0].bv_val );
4354 return LDAP_SUCCESS;
4355 } else if ( rc == 0 ) {
4356 Debug( LDAP_DEBUG_SYNC,
4357 "dn_callback : entries have identical CSN "
4359 rs->sr_entry->e_name.bv_val,
4360 old->a_vals[0].bv_val, 0 );
4361 return LDAP_SUCCESS;
4366 is_ctx = dn_match( &rs->sr_entry->e_nname,
4367 &op->o_bd->be_nsuffix[0] );
4369 /* Did the DN change?
4370 * case changes in the parent are ignored,
4371 * we only want to know if the RDN was
4374 dnRdn( &rs->sr_entry->e_name, &old_rdn );
4375 dnRdn( &dni->new_entry->e_name, &new_rdn );
4376 dnParent( &rs->sr_entry->e_nname, &old_p );
4377 dnParent( &dni->new_entry->e_nname, &new_p );
4379 new_sup = !dn_match( &old_p, &new_p );
4380 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
4382 struct berval oldRDN, oldVal;
4383 AttributeDescription *ad = NULL;
4389 dni->nnewSup = new_p;
4391 /* See if the oldRDN was deleted */
4392 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
4393 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
4394 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
4396 oldRDN.bv_len -= oldVal.bv_len + 1;
4397 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4399 for ( oldpos=0, a=rs->sr_entry->e_attrs;
4400 a && a->a_desc != ad; oldpos++, a=a->a_next );
4401 /* a should not be NULL but apparently it happens.
4404 dni->oldNcount = a ? a->a_numvals : 0;
4405 for ( newpos=0, a=dni->new_entry->e_attrs;
4406 a && a->a_desc != ad; newpos++, a=a->a_next );
4407 if ( !a || oldpos != newpos || attr_valfind( a,
4408 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
4409 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
4410 SLAP_MR_VALUE_OF_SYNTAX,
4411 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
4415 /* Get the newRDN's desc */
4416 dnRdn( &dni->new_entry->e_nname, &oldRDN );
4417 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
4418 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
4420 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4423 /* A ModDN has happened, but in Refresh mode other
4424 * changes may have occurred before we picked it up.
4425 * So fallthru to regular Modify processing.
4429 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
4430 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
4434 } else if ( rs->sr_type == REP_RESULT ) {
4435 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
4436 Debug( LDAP_DEBUG_ANY,
4437 "dn_callback : consistency error - "
4438 "entryUUID is not unique\n", 0, 0, 0 );
4442 return LDAP_SUCCESS;
4446 nonpresent_callback(
4450 syncinfo_t *si = op->o_callback->sc_private;
4453 char *present_uuid = NULL;
4454 struct nonpresent_entry *np_entry;
4456 if ( rs->sr_type == REP_RESULT ) {
4457 count = presentlist_free( si->si_presentlist );
4458 si->si_presentlist = NULL;
4460 } else if ( rs->sr_type == REP_SEARCH ) {
4461 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
4462 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
4465 present_uuid = presentlist_find( si->si_presentlist, &a->a_nvals[0] );
4468 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
4469 char buf[sizeof("rid=999 non")];
4471 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
4472 present_uuid ? "" : "non" );
4474 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
4475 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
4478 if ( a == NULL ) return 0;
4481 if ( present_uuid == NULL ) {
4482 np_entry = (struct nonpresent_entry *)
4483 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
4484 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
4485 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
4486 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
4489 presentlist_delete( &si->si_presentlist, &a->a_nvals[0] );
4490 ch_free( present_uuid );
4493 return LDAP_SUCCESS;
4496 static struct berval *
4497 slap_uuidstr_from_normalized(
4498 struct berval* uuidstr,
4499 struct berval* normalized,
4504 unsigned char nibble;
4507 if ( normalized == NULL ) return NULL;
4508 if ( normalized->bv_len != 16 ) return NULL;
4513 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4514 if ( new == NULL ) {
4521 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4522 if ( new != uuidstr ) {
4523 slap_sl_free( new, ctx );
4528 for ( i = 0; i < 16; i++ ) {
4529 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
4530 new->bv_val[(i<<1)+d] = '-';
4534 nibble = (normalized->bv_val[i] >> 4) & 0xF;
4535 if ( nibble < 10 ) {
4536 new->bv_val[(i<<1)+d] = nibble + '0';
4538 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
4541 nibble = (normalized->bv_val[i]) & 0xF;
4542 if ( nibble < 10 ) {
4543 new->bv_val[(i<<1)+d+1] = nibble + '0';
4545 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
4549 new->bv_val[new->bv_len] = '\0';
4556 if ( normalized == NULL ) return NULL;
4557 if ( normalized->bv_len != 16 ) return NULL;
4563 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4564 if ( new == NULL ) {
4571 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4576 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
4577 normalized->bv_len, new->bv_val, new->bv_len + 1 );
4581 if ( new != NULL ) {
4582 if ( new->bv_val != NULL ) {
4583 slap_sl_free( new->bv_val, ctx );
4586 if ( new != uuidstr ) {
4587 slap_sl_free( new, ctx );
4600 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
4603 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN-2 ));
4605 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN ));
4610 syncinfo_free( syncinfo_t *sie, int free_all )
4612 syncinfo_t *si_next;
4614 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
4615 sie->si_ridtxt, 0, 0 );
4618 si_next = sie->si_next;
4621 if ( sie->si_conn ) {
4622 connection_client_stop( sie->si_conn );
4623 sie->si_conn = NULL;
4625 ldap_unbind_ext( sie->si_ld, NULL, NULL );
4629 struct re_s *re = sie->si_re;
4632 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4633 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
4634 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
4635 ldap_pvt_runqueue_remove( &slapd_rq, re );
4636 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4639 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
4641 bindconf_free( &sie->si_bindconf );
4643 if ( sie->si_filterstr.bv_val ) {
4644 ch_free( sie->si_filterstr.bv_val );
4646 if ( sie->si_filter ) {
4647 filter_free( sie->si_filter );
4649 if ( sie->si_logfilterstr.bv_val ) {
4650 ch_free( sie->si_logfilterstr.bv_val );
4652 if ( sie->si_logfilter ) {
4653 filter_free( sie->si_logfilter );
4655 if ( sie->si_base.bv_val ) {
4656 ch_free( sie->si_base.bv_val );
4658 if ( sie->si_logbase.bv_val ) {
4659 ch_free( sie->si_logbase.bv_val );
4661 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
4662 ch_free( sie->si_contextdn.bv_val );
4664 if ( sie->si_attrs ) {
4666 while ( sie->si_attrs[i] != NULL ) {
4667 ch_free( sie->si_attrs[i] );
4670 ch_free( sie->si_attrs );
4672 if ( sie->si_exattrs ) {
4674 while ( sie->si_exattrs[i] != NULL ) {
4675 ch_free( sie->si_exattrs[i] );
4678 ch_free( sie->si_exattrs );
4680 if ( sie->si_anlist ) {
4682 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
4683 ch_free( sie->si_anlist[i].an_name.bv_val );
4686 ch_free( sie->si_anlist );
4688 if ( sie->si_exanlist ) {
4690 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
4691 ch_free( sie->si_exanlist[i].an_name.bv_val );
4694 ch_free( sie->si_exanlist );
4696 if ( sie->si_retryinterval ) {
4697 ch_free( sie->si_retryinterval );
4699 if ( sie->si_retrynum ) {
4700 ch_free( sie->si_retrynum );
4702 if ( sie->si_retrynum_init ) {
4703 ch_free( sie->si_retrynum_init );
4705 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
4706 if ( sie->si_presentlist ) {
4707 presentlist_free( sie->si_presentlist );
4709 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
4710 struct nonpresent_entry* npe;
4711 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
4712 LDAP_LIST_REMOVE( npe, npe_link );
4713 if ( npe->npe_name ) {
4714 if ( npe->npe_name->bv_val ) {
4715 ch_free( npe->npe_name->bv_val );
4717 ch_free( npe->npe_name );
4719 if ( npe->npe_nname ) {
4720 if ( npe->npe_nname->bv_val ) {
4721 ch_free( npe->npe_nname->bv_val );
4723 ch_free( npe->npe_nname );
4727 if ( sie->si_cookieState ) {
4728 sie->si_cookieState->cs_ref--;
4729 if ( !sie->si_cookieState->cs_ref ) {
4730 ch_free( sie->si_cookieState->cs_sids );
4731 ber_bvarray_free( sie->si_cookieState->cs_vals );
4732 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
4733 ch_free( sie->si_cookieState->cs_psids );
4734 ber_bvarray_free( sie->si_cookieState->cs_pvals );
4735 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
4736 ch_free( sie->si_cookieState );
4739 #ifdef ENABLE_REWRITE
4740 if ( sie->si_rewrite )
4741 rewrite_info_delete( &sie->si_rewrite );
4742 if ( sie->si_suffixm.bv_val )
4743 ch_free( sie->si_suffixm.bv_val );
4747 } while ( free_all && si_next );
4750 #ifdef ENABLE_REWRITE
4752 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4754 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4755 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4756 char *argvRule[] = { "rewriteRule", NULL, NULL, ":", NULL };
4760 if ( si->si_rewrite )
4761 rewrite_info_delete( &si->si_rewrite );
4762 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4764 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4765 if ( rc != LDAP_SUCCESS )
4768 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4769 if ( rc != LDAP_SUCCESS )
4772 vnc = ch_malloc( si->si_base.bv_len + 6 );
4773 strcpy( vnc, "(.*)" );
4774 lutil_strcopy( lutil_strcopy( vnc+4, si->si_base.bv_val ), "$" );
4777 rnc = ch_malloc( si->si_suffixm.bv_len + 3 );
4778 strcpy( rnc, "%1" );
4779 strcpy( rnc+2, si->si_suffixm.bv_val );
4782 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4789 /* NOTE: used & documented in slapd.conf(5) */
4791 #define PROVIDERSTR "provider"
4792 #define SCHEMASTR "schemachecking"
4793 #define FILTERSTR "filter"
4794 #define SEARCHBASESTR "searchbase"
4795 #define SCOPESTR "scope"
4796 #define ATTRSONLYSTR "attrsonly"
4797 #define ATTRSSTR "attrs"
4798 #define TYPESTR "type"
4799 #define INTERVALSTR "interval"
4800 #define RETRYSTR "retry"
4801 #define SLIMITSTR "sizelimit"
4802 #define TLIMITSTR "timelimit"
4803 #define SYNCDATASTR "syncdata"
4804 #define LOGBASESTR "logbase"
4805 #define LOGFILTERSTR "logfilter"
4806 #define SUFFIXMSTR "suffixmassage"
4807 #define STRICT_REFRESH "strictrefresh"
4808 #define LAZY_COMMIT "lazycommit"
4810 /* FIXME: undocumented */
4811 #define EXATTRSSTR "exattrs"
4812 #define MANAGEDSAITSTR "manageDSAit"
4816 GOT_RID = 0x00000001U,
4817 GOT_PROVIDER = 0x00000002U,
4818 GOT_SCHEMACHECKING = 0x00000004U,
4819 GOT_FILTER = 0x00000008U,
4820 GOT_SEARCHBASE = 0x00000010U,
4821 GOT_SCOPE = 0x00000020U,
4822 GOT_ATTRSONLY = 0x00000040U,
4823 GOT_ATTRS = 0x00000080U,
4824 GOT_TYPE = 0x00000100U,
4825 GOT_INTERVAL = 0x00000200U,
4826 GOT_RETRY = 0x00000400U,
4827 GOT_SLIMIT = 0x00000800U,
4828 GOT_TLIMIT = 0x00001000U,
4829 GOT_SYNCDATA = 0x00002000U,
4830 GOT_LOGBASE = 0x00004000U,
4831 GOT_LOGFILTER = 0x00008000U,
4832 GOT_EXATTRS = 0x00010000U,
4833 GOT_MANAGEDSAIT = 0x00020000U,
4834 GOT_BINDCONF = 0x00040000U,
4835 GOT_SUFFIXM = 0x00080000U,
4838 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4841 static slap_verbmasks datamodes[] = {
4842 { BER_BVC("default"), SYNCDATA_DEFAULT },
4843 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4844 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4849 parse_syncrepl_retry(
4856 int use_default = 0;
4858 char *val = arg + STRLENOF( RETRYSTR "=" );
4859 if ( strcasecmp( val, "undefined" ) == 0 ) {
4864 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4865 retry_list[0] = NULL;
4867 slap_str2clist( &retry_list, val, " ,\t" );
4869 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4872 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4873 "Error: incomplete syncrepl retry list" );
4874 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4875 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4876 ch_free( retry_list[k] );
4878 ch_free( retry_list );
4881 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4882 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4883 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4884 for ( j = 0; j < n; j++ ) {
4886 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4887 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4888 "Error: invalid retry interval \"%s\" (#%d)",
4889 retry_list[j*2], j );
4890 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4891 /* do some cleanup */
4894 si->si_retryinterval[j] = (time_t)t;
4895 if ( *retry_list[j*2+1] == '+' ) {
4896 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4897 si->si_retrynum[j] = RETRYNUM_FOREVER;
4901 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4902 || si->si_retrynum_init[j] <= 0 )
4904 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4905 "Error: invalid initial retry number \"%s\" (#%d)",
4906 retry_list[j*2+1], j );
4907 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4908 /* do some cleanup */
4911 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4912 || si->si_retrynum[j] <= 0 )
4914 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4915 "Error: invalid retry number \"%s\" (#%d)",
4916 retry_list[j*2+1], j );
4917 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4918 /* do some cleanup */
4923 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4924 Debug( LDAP_DEBUG_CONFIG,
4925 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4929 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4930 si->si_retrynum[j] = RETRYNUM_TAIL;
4931 si->si_retryinterval[j] = 0;
4933 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4934 ch_free( retry_list[k] );
4936 ch_free( retry_list );
4937 if ( !use_default ) {
4938 si->si_got |= GOT_RETRY;
4945 parse_syncrepl_line(
4952 for ( i = 1; i < c->argc; i++ ) {
4953 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4954 STRLENOF( IDSTR "=" ) ) )
4957 /* '\0' string terminator accounts for '=' */
4958 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4959 if ( lutil_atoi( &tmp, val ) != 0 ) {
4960 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4961 "Error: parse_syncrepl_line: "
4962 "unable to parse syncrepl id \"%s\"", val );
4963 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4966 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4967 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4968 "Error: parse_syncrepl_line: "
4969 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4970 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4974 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4975 si->si_got |= GOT_RID;
4976 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4977 STRLENOF( PROVIDERSTR "=" ) ) )
4979 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4980 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4982 if ( ldap_is_ldaps_url( val ))
4983 si->si_bindconf.sb_tls_do_init = 1;
4985 si->si_got |= GOT_PROVIDER;
4986 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4987 STRLENOF( SCHEMASTR "=" ) ) )
4989 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4990 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4991 si->si_schemachecking = 1;
4992 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4993 si->si_schemachecking = 0;
4995 si->si_schemachecking = 1;
4997 si->si_got |= GOT_SCHEMACHECKING;
4998 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4999 STRLENOF( FILTERSTR "=" ) ) )
5001 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
5002 if ( si->si_filterstr.bv_val )
5003 ch_free( si->si_filterstr.bv_val );
5004 ber_str2bv( val, 0, 1, &si->si_filterstr );
5005 si->si_got |= GOT_FILTER;
5006 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
5007 STRLENOF( LOGFILTERSTR "=" ) ) )
5009 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
5010 if ( si->si_logfilterstr.bv_val )
5011 ch_free( si->si_logfilterstr.bv_val );
5012 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
5013 si->si_got |= GOT_LOGFILTER;
5014 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
5015 STRLENOF( SEARCHBASESTR "=" ) ) )
5020 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
5021 if ( si->si_base.bv_val ) {
5022 ch_free( si->si_base.bv_val );
5024 ber_str2bv( val, 0, 0, &bv );
5025 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
5026 if ( rc != LDAP_SUCCESS ) {
5027 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5028 "Invalid base DN \"%s\": %d (%s)",
5029 val, rc, ldap_err2string( rc ) );
5030 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5033 si->si_got |= GOT_SEARCHBASE;
5034 #ifdef ENABLE_REWRITE
5035 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
5036 STRLENOF( SUFFIXMSTR "=" ) ) )
5041 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
5042 if ( si->si_suffixm.bv_val ) {
5043 ch_free( si->si_suffixm.bv_val );
5045 ber_str2bv( val, 0, 0, &bv );
5046 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
5047 if ( rc != LDAP_SUCCESS ) {
5048 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5049 "Invalid massage DN \"%s\": %d (%s)",
5050 val, rc, ldap_err2string( rc ) );
5051 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5054 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
5055 ch_free( si->si_suffixm.bv_val );
5056 BER_BVZERO( &si->si_suffixm );
5057 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5058 "Massage DN \"%s\" is not within the database naming context",
5060 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5063 si->si_got |= GOT_SUFFIXM;
5065 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
5066 STRLENOF( LOGBASESTR "=" ) ) )
5071 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
5072 if ( si->si_logbase.bv_val ) {
5073 ch_free( si->si_logbase.bv_val );
5075 ber_str2bv( val, 0, 0, &bv );
5076 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
5077 if ( rc != LDAP_SUCCESS ) {
5078 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5079 "Invalid logbase DN \"%s\": %d (%s)",
5080 val, rc, ldap_err2string( rc ) );
5081 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5084 si->si_got |= GOT_LOGBASE;
5085 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
5086 STRLENOF( SCOPESTR "=" ) ) )
5089 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
5090 j = ldap_pvt_str2scope( val );
5092 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5093 "Error: parse_syncrepl_line: "
5094 "unknown scope \"%s\"", val);
5095 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5099 si->si_got |= GOT_SCOPE;
5100 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
5101 STRLENOF( ATTRSONLYSTR ) ) )
5103 si->si_attrsonly = 1;
5104 si->si_got |= GOT_ATTRSONLY;
5105 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
5106 STRLENOF( ATTRSSTR "=" ) ) )
5108 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
5109 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
5111 attr_fname = ch_strdup( val + STRLENOF(":include:") );
5112 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
5113 if ( si->si_anlist == NULL ) {
5114 ch_free( attr_fname );
5117 si->si_anfile = attr_fname;
5119 char *str, *s, *next;
5120 const char *delimstr = " ,\t";
5121 str = ch_strdup( val );
5122 for ( s = ldap_pvt_strtok( str, delimstr, &next );
5124 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
5126 if ( strlen(s) == 1 && *s == '*' ) {
5127 si->si_allattrs = 1;
5128 val[ s - str ] = delimstr[0];
5130 if ( strlen(s) == 1 && *s == '+' ) {
5131 si->si_allopattrs = 1;
5132 val [ s - str ] = delimstr[0];
5136 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
5137 if ( si->si_anlist == NULL ) {
5141 si->si_got |= GOT_ATTRS;
5142 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
5143 STRLENOF( EXATTRSSTR "=" ) ) )
5145 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
5146 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
5148 attr_fname = ch_strdup( val + STRLENOF(":include:") );
5149 si->si_exanlist = file2anlist(
5150 si->si_exanlist, attr_fname, " ,\t" );
5151 if ( si->si_exanlist == NULL ) {
5152 ch_free( attr_fname );
5155 ch_free( attr_fname );
5157 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
5158 if ( si->si_exanlist == NULL ) {
5162 si->si_got |= GOT_EXATTRS;
5163 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
5164 STRLENOF( TYPESTR "=" ) ) )
5166 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
5167 if ( !strncasecmp( val, "refreshOnly",
5168 STRLENOF("refreshOnly") ) )
5170 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5171 } else if ( !strncasecmp( val, "refreshAndPersist",
5172 STRLENOF("refreshAndPersist") ) )
5174 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
5175 si->si_interval = 60;
5177 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5178 "Error: parse_syncrepl_line: "
5179 "unknown sync type \"%s\"", val);
5180 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5183 si->si_got |= GOT_TYPE;
5184 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
5185 STRLENOF( INTERVALSTR "=" ) ) )
5187 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
5188 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
5189 si->si_interval = 0;
5190 } else if ( strchr( val, ':' ) != NULL ) {
5191 char *next, *ptr = val;
5194 dd = strtol( ptr, &next, 10 );
5195 if ( next == ptr || next[0] != ':' || dd < 0 ) {
5196 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5197 "Error: parse_syncrepl_line: "
5198 "invalid interval \"%s\", unable to parse days", val );
5199 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5203 hh = strtol( ptr, &next, 10 );
5204 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
5205 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5206 "Error: parse_syncrepl_line: "
5207 "invalid interval \"%s\", unable to parse hours", val );
5208 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5212 mm = strtol( ptr, &next, 10 );
5213 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
5214 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5215 "Error: parse_syncrepl_line: "
5216 "invalid interval \"%s\", unable to parse minutes", val );
5217 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5221 ss = strtol( ptr, &next, 10 );
5222 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
5223 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5224 "Error: parse_syncrepl_line: "
5225 "invalid interval \"%s\", unable to parse seconds", val );
5226 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5229 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
5233 if ( lutil_parse_time( val, &t ) != 0 ) {
5234 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5235 "Error: parse_syncrepl_line: "
5236 "invalid interval \"%s\"", val );
5237 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5240 si->si_interval = (time_t)t;
5242 if ( si->si_interval < 0 ) {
5243 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5244 "Error: parse_syncrepl_line: "
5245 "invalid interval \"%ld\"",
5246 (long) si->si_interval);
5247 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5250 si->si_got |= GOT_INTERVAL;
5251 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
5252 STRLENOF( RETRYSTR "=" ) ) )
5254 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
5257 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
5258 STRLENOF( MANAGEDSAITSTR "=" ) ) )
5260 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
5261 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
5262 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
5264 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5265 "invalid manageDSAit value \"%s\".\n",
5267 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5270 si->si_got |= GOT_MANAGEDSAIT;
5271 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
5272 STRLENOF( SLIMITSTR "=") ) )
5274 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
5275 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5278 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
5279 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5280 "invalid size limit value \"%s\".\n",
5282 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5285 si->si_got |= GOT_SLIMIT;
5286 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
5287 STRLENOF( TLIMITSTR "=" ) ) )
5289 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
5290 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5293 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
5294 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5295 "invalid time limit value \"%s\".\n",
5297 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5300 si->si_got |= GOT_TLIMIT;
5301 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
5302 STRLENOF( SYNCDATASTR "=" ) ) )
5304 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
5305 si->si_syncdata = verb_to_mask( val, datamodes );
5306 si->si_got |= GOT_SYNCDATA;
5307 } else if ( !strncasecmp( c->argv[ i ], STRICT_REFRESH,
5308 STRLENOF( STRICT_REFRESH ) ) )
5310 si->si_strict_refresh = 1;
5311 } else if ( !strncasecmp( c->argv[ i ], LAZY_COMMIT,
5312 STRLENOF( LAZY_COMMIT ) ) )
5314 si->si_lazyCommit = 1;
5315 } else if ( !bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
5316 si->si_got |= GOT_BINDCONF;
5318 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5319 "Error: parse_syncrepl_line: "
5320 "unable to parse \"%s\"\n", c->argv[ i ] );
5321 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5326 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
5327 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5328 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
5329 si->si_got & GOT_RID ? "" : " "IDSTR,
5330 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
5331 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
5332 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5336 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
5337 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5338 "Base DN \"%s\" is not within the database naming context",
5339 si->si_base.bv_val );
5340 ch_free( si->si_base.bv_val );
5341 BER_BVZERO( &si->si_base );
5342 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5346 #ifdef ENABLE_REWRITE
5347 if ( si->si_got & GOT_SUFFIXM ) {
5348 if (config_suffixm( c, si )) {
5349 ch_free( si->si_suffixm.bv_val );
5350 BER_BVZERO( &si->si_suffixm );
5351 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5352 "Error configuring rewrite engine" );
5353 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5359 if ( !( si->si_got & GOT_RETRY ) ) {
5360 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
5361 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
5362 if ( si->si_retryinterval == NULL ) {
5363 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
5369 si->si_filter = str2filter( si->si_filterstr.bv_val );
5370 if ( si->si_filter == NULL ) {
5371 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
5372 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
5376 if ( si->si_got & GOT_LOGFILTER ) {
5377 si->si_logfilter = str2filter( si->si_logfilterstr.bv_val );
5378 if ( si->si_logfilter == NULL ) {
5379 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse logfilter=\"%s\"\n",
5380 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_logfilterstr.bv_val );
5395 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
5396 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
5397 "operations required for syncrepl", c->be->be_type );
5398 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5401 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
5402 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
5403 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5406 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
5409 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
5413 si->si_bindconf.sb_tls = SB_TLS_OFF;
5414 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
5415 si->si_schemachecking = 0;
5416 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
5417 &si->si_filterstr );
5418 si->si_base.bv_val = NULL;
5419 si->si_scope = LDAP_SCOPE_SUBTREE;
5420 si->si_attrsonly = 0;
5421 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5422 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5423 si->si_attrs = NULL;
5424 si->si_allattrs = 0;
5425 si->si_allopattrs = 0;
5426 si->si_exattrs = NULL;
5427 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5428 si->si_interval = 86400;
5429 si->si_retryinterval = NULL;
5430 si->si_retrynum_init = NULL;
5431 si->si_retrynum = NULL;
5432 si->si_manageDSAit = 0;
5436 si->si_presentlist = NULL;
5437 LDAP_LIST_INIT( &si->si_nonpresentlist );
5438 ldap_pvt_thread_mutex_init( &si->si_mutex );
5440 rc = parse_syncrepl_line( c, si );
5445 /* Must be LDAPv3 because we need controls */
5446 switch ( si->si_bindconf.sb_version ) {
5448 /* not explicitly set */
5449 si->si_bindconf.sb_version = LDAP_VERSION3;
5452 /* explicitly set */
5455 Debug( LDAP_DEBUG_ANY,
5456 "version %d incompatible with syncrepl\n",
5457 si->si_bindconf.sb_version, 0, 0 );
5458 syncinfo_free( si, 0 );
5462 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
5463 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5464 "<%s> invalid URL", c->argv[0] );
5465 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
5466 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
5471 if ( slapMode & SLAP_SERVER_MODE ) {
5473 /* check if consumer points to current server and database.
5474 * If so, ignore this configuration.
5476 if ( !SLAP_DBHIDDEN( c->be ) ) {
5478 /* if searchbase doesn't match current DB suffix,
5479 * assume it's different
5481 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
5482 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
5487 /* if searchbase matches, see if URLs match */
5488 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
5494 init_syncrepl( si );
5495 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5496 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
5497 si->si_interval, do_syncrepl, si, "do_syncrepl",
5499 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5501 rc = config_sync_shadow( c ) ? -1 : 0;
5506 /* mirrormode still needs to see this flag in tool mode */
5507 rc = config_sync_shadow( c ) ? -1 : 0;
5509 ldap_free_urldesc( lud );
5513 /* Use main slapd defaults */
5514 bindconf_tls_defaults( &si->si_bindconf );
5517 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
5518 syncinfo_free( si, 0 );
5521 Debug( LDAP_DEBUG_CONFIG,
5522 "Config: ** successfully added syncrepl %s \"%s\"\n",
5524 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
5525 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
5526 if ( c->be->be_syncinfo ) {
5529 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
5531 /* add new syncrepl to end of list (same order as when deleting) */
5532 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
5535 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
5536 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
5537 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
5539 c->be->be_syncinfo = si;
5541 si->si_cookieState->cs_ref++;
5550 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
5552 struct berval bc, uri, bs;
5553 char buf[BUFSIZ*2], *ptr;
5556 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
5560 /* temporarily inhibit bindconf from printing URI */
5561 uri = si->si_bindconf.sb_uri;
5562 BER_BVZERO( &si->si_bindconf.sb_uri );
5563 si->si_bindconf.sb_version = 0;
5564 bindconf_unparse( &si->si_bindconf, &bc );
5565 si->si_bindconf.sb_uri = uri;
5566 si->si_bindconf.sb_version = LDAP_VERSION3;
5569 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
5570 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
5571 si->si_rid, si->si_bindconf.sb_uri.bv_val );
5572 if ( len >= sizeof( buf ) ) return;
5574 if ( !BER_BVISNULL( &bc ) ) {
5575 if ( WHATSLEFT <= bc.bv_len ) {
5579 ptr = lutil_strcopy( ptr, bc.bv_val );
5582 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
5583 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
5584 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
5585 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
5588 if ( !BER_BVISNULL( &si->si_base ) ) {
5589 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
5590 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
5591 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
5594 #ifdef ENABLE_REWRITE
5595 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
5596 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
5597 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
5598 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
5602 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
5603 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
5604 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
5605 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
5608 if ( !BER_BVISNULL( &si->si_logbase ) ) {
5609 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
5610 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
5611 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
5614 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
5615 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
5616 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
5617 ptr = lutil_strcopy( ptr, bs.bv_val );
5619 if ( si->si_attrsonly ) {
5620 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5621 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
5623 if ( si->si_anfile ) {
5624 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
5625 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
5626 ptr = lutil_strcopy( ptr, si->si_anfile );
5628 } else if ( si->si_allattrs || si->si_allopattrs ||
5629 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
5633 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5634 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
5636 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
5637 if ( ptr == NULL ) return;
5638 if ( si->si_allattrs ) {
5639 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
5640 if ( old != ptr ) *ptr++ = ',';
5643 if ( si->si_allopattrs ) {
5644 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
5645 if ( old != ptr ) *ptr++ = ',';
5650 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
5651 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
5652 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
5653 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
5654 if ( ptr == NULL ) return;
5656 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
5657 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
5658 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
5660 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
5661 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
5662 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
5663 "refreshAndPersist" : "refreshOnly" );
5665 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
5668 dd = si->si_interval;
5675 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
5676 INTERVALSTR, dd, hh, mm, ss );
5677 if ( len >= WHATSLEFT ) return;
5681 if ( si->si_got & GOT_RETRY ) {
5682 const char *space = "";
5683 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
5684 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
5685 for (i=0; si->si_retryinterval[i]; i++) {
5686 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
5687 (long) si->si_retryinterval[i] );
5689 if ( WHATSLEFT - 1 <= len ) return;
5691 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
5694 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
5695 if ( WHATSLEFT <= len ) return;
5699 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
5702 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
5705 if ( si->si_slimit ) {
5706 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
5707 if ( WHATSLEFT <= len ) return;
5711 if ( si->si_tlimit ) {
5712 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
5713 if ( WHATSLEFT <= len ) return;
5717 if ( si->si_syncdata ) {
5718 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
5719 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
5720 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
5721 ptr = lutil_strcopy( ptr, bc.bv_val );
5725 if ( si->si_lazyCommit ) {
5726 ptr = lutil_strcopy( ptr, " " LAZY_COMMIT );
5729 bc.bv_len = ptr - buf;
5731 ber_dupbv( bv, &bc );
5735 syncrepl_config( ConfigArgs *c )
5737 if (c->op == SLAP_CONFIG_EMIT) {
5738 if ( c->be->be_syncinfo ) {
5742 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
5743 syncrepl_unparse( si, &bv );
5744 ber_bvarray_add( &c->rvalue_vals, &bv );
5749 } else if ( c->op == LDAP_MOD_DELETE ) {
5751 if ( c->be->be_syncinfo ) {
5752 syncinfo_t *si, **sip;
5755 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
5757 if ( c->valx == -1 || i == c->valx ) {
5761 /* If the task is currently active, we have to leave
5762 * it running. It will exit on its own. This will only
5763 * happen when running on the cn=config DB.
5766 if ( si->si_be == c->be || ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
5769 /* There is no active thread, but we must still
5770 * ensure that no thread is (or will be) queued
5771 * while we removes the task.
5773 struct re_s *re = si->si_re;
5776 if ( si->si_conn ) {
5777 connection_client_stop( si->si_conn );
5781 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5782 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
5783 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5786 if ( ldap_pvt_thread_pool_retract( re->pool_cookie ) > 0 )
5789 ldap_pvt_runqueue_remove( &slapd_rq, re );
5790 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5792 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5796 syncinfo_free( si, 0 );
5805 if ( !c->be->be_syncinfo ) {
5806 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5810 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5811 Debug(LDAP_DEBUG_ANY, "%s: "
5812 "syncrepl: database already shadowed.\n",
5816 return add_syncrepl( c );