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 ldap_pvt_thread_cond_t cs_cond;
50 struct berval *cs_vals;
57 /* pending changes, not yet committed */
58 ldap_pvt_thread_mutex_t cs_pmutex;
59 struct berval *cs_pvals;
64 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
65 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
66 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
68 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
69 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
71 #define RETRYNUM_FOREVER (-1) /* retry forever */
72 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
73 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
74 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
76 typedef struct syncinfo_s {
77 struct syncinfo_s *si_next;
82 char si_ridtxt[ STRLENOF("rid=999") + 1 ];
83 slap_bindconf si_bindconf;
84 struct berval si_base;
85 struct berval si_logbase;
86 struct berval si_filterstr;
87 struct berval si_logfilterstr;
90 struct berval si_contextdn;
94 AttributeName *si_anlist;
95 AttributeName *si_exanlist;
100 int si_schemachecking;
101 int si_type; /* the active type */
102 int si_ctype; /* the configured type */
104 time_t *si_retryinterval;
105 int *si_retrynum_init;
107 struct sync_cookie si_syncCookie;
108 cookie_state *si_cookieState;
113 int si_refreshDelete;
114 int si_refreshPresent;
117 time_t si_refreshBeg;
118 time_t si_refreshEnd;
119 OpExtra *si_refreshTxn;
124 int si_strict_refresh; /* stop listening during fallback refresh */
127 Avlnode *si_presentlist;
130 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
131 #ifdef ENABLE_REWRITE
132 struct rewrite_info *si_rewrite;
133 struct berval si_suffixm;
135 ldap_pvt_thread_mutex_t si_mutex;
138 static int syncuuid_cmp( const void *, const void * );
139 static int presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
140 static void presentlist_delete( Avlnode **av, struct berval *syncUUID );
141 static char *presentlist_find( Avlnode *av, struct berval *syncUUID );
142 static int presentlist_free( Avlnode *av );
143 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
144 static int syncrepl_message_to_op(
145 syncinfo_t *, Operation *, LDAPMessage * );
146 static int syncrepl_message_to_entry(
147 syncinfo_t *, Operation *, LDAPMessage *,
148 Modifications **, Entry **, int, struct berval* );
149 static int syncrepl_entry(
150 syncinfo_t *, Operation*, Entry*,
151 Modifications**,int, struct berval*,
152 struct berval *cookieCSN );
153 static int syncrepl_updateCookie(
154 syncinfo_t *, Operation *,
155 struct sync_cookie *, int save );
156 static struct berval * slap_uuidstr_from_normalized(
157 struct berval *, struct berval *, void * );
158 static int syncrepl_add_glue_ancestors(
159 Operation* op, Entry *e );
161 /* delta-mmr overlay handler */
162 static int syncrepl_op_modify( Operation *op, SlapReply *rs );
164 /* callback functions */
165 static int dn_callback( Operation *, SlapReply * );
166 static int nonpresent_callback( Operation *, SlapReply * );
168 static AttributeDescription *sync_descs[4];
171 static AttributeDescription *ad_reqMod, *ad_reqDN;
173 typedef struct logschema {
175 struct berval ls_req;
176 struct berval ls_mod;
177 struct berval ls_newRdn;
178 struct berval ls_delRdn;
179 struct berval ls_newSup;
180 struct berval ls_controls;
183 static logschema changelog_sc = {
185 BER_BVC("changeType"),
188 BER_BVC("deleteOldRDN"),
189 BER_BVC("newSuperior"),
193 static logschema accesslog_sc = {
197 BER_BVC("reqNewRDN"),
198 BER_BVC("reqDeleteOldRDN"),
199 BER_BVC("reqNewSuperior"),
200 BER_BVC("reqControls")
204 syncrepl_state2str( int state )
207 case LDAP_SYNC_PRESENT:
213 case LDAP_SYNC_MODIFY:
216 case LDAP_SYNC_DELETE:
223 static slap_overinst syncrepl_ov;
226 init_syncrepl(syncinfo_t *si)
229 char **attrs, **exattrs;
231 if ( !syncrepl_ov.on_bi.bi_type ) {
232 syncrepl_ov.on_bi.bi_type = "syncrepl";
233 syncrepl_ov.on_bi.bi_op_modify = syncrepl_op_modify;
234 overlay_register( &syncrepl_ov );
237 /* delta-MMR needs the overlay, nothing else does.
238 * This must happen before accesslog overlay is configured.
240 if ( si->si_syncdata &&
241 !overlay_is_inst( si->si_be, syncrepl_ov.on_bi.bi_type )) {
242 overlay_config( si->si_be, syncrepl_ov.on_bi.bi_type, -1, NULL, NULL );
245 logschema *ls = &accesslog_sc;
247 slap_bv2ad( &ls->ls_mod, &ad_reqMod, &text );
248 slap_bv2ad( &ls->ls_dn, &ad_reqDN, &text );
252 if ( !sync_descs[0] ) {
253 sync_descs[0] = slap_schema.si_ad_objectClass;
254 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
255 sync_descs[2] = slap_schema.si_ad_entryCSN;
256 sync_descs[3] = NULL;
259 if ( si->si_allattrs && si->si_allopattrs )
262 attrs = anlist2attrs( si->si_anlist );
265 if ( si->si_allattrs ) {
268 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
269 for ( j = i; attrs[j] != NULL; j++ ) {
272 attrs[j] = attrs[j+1];
278 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
279 attrs[i] = ch_strdup("*");
282 } else if ( si->si_allopattrs ) {
285 if ( is_at_operational( at_find( attrs[i] ) ) ) {
286 for ( j = i; attrs[j] != NULL; j++ ) {
289 attrs[j] = attrs[j+1];
295 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
296 attrs[i] = ch_strdup("+");
300 for ( i = 0; sync_descs[i] != NULL; i++ ) {
303 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
304 for ( k = j; attrs[k] != NULL; k++ ) {
307 attrs[k] = attrs[k+1];
315 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
317 if ( si->si_allopattrs ) {
318 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
320 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
324 if ( si->si_allopattrs ) {
325 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
327 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
328 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
336 if ( si->si_allattrs == si->si_allopattrs ) {
337 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
338 attrs[i++] = ch_strdup( "*" );
339 attrs[i++] = ch_strdup( "+" );
340 } else if ( si->si_allattrs && !si->si_allopattrs ) {
341 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
342 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
343 attrs[i++] = ch_strdup( "*" );
344 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
345 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
347 } else if ( !si->si_allattrs && si->si_allopattrs ) {
348 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
349 attrs[i++] = ch_strdup( "+" );
350 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
355 si->si_attrs = attrs;
357 exattrs = anlist2attrs( si->si_exanlist );
360 for ( n = 0; exattrs[n] != NULL; n++ ) ;
362 for ( i = 0; sync_descs[i] != NULL; i++ ) {
364 while ( exattrs[j] != NULL ) {
365 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
366 ch_free( exattrs[j] );
367 for ( k = j; exattrs[k] != NULL; k++ ) {
368 exattrs[k] = exattrs[k+1];
376 for ( i = 0; exattrs[i] != NULL; i++ ) {
377 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
379 if ( ( oc = si->si_anlist[j].an_oc ) ) {
381 while ( oc->soc_required[k] ) {
382 if ( !strcmp( exattrs[i],
383 oc->soc_required[k]->sat_cname.bv_val ) ) {
384 ch_free( exattrs[i] );
385 for ( l = i; exattrs[l]; l++ ) {
386 exattrs[l] = exattrs[l+1];
396 for ( i = 0; exattrs[i] != NULL; i++ ) ;
399 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
402 si->si_exattrs = exattrs;
410 BerElementBuffer berbuf;
411 BerElement *ber = (BerElement *)&berbuf;
412 LDAPControl c[3], *ctrls[4];
416 char **attrs, *lattrs[9];
421 /* setup LDAP SYNC control */
422 ber_init2( ber, NULL, LBER_USE_DER );
423 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
425 /* If we're using a log but we have no state, then fallback to
426 * normal mode for a full refresh.
428 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
429 si->si_logstate = SYNCLOG_FALLBACK;
432 /* Use the log parameters if we're in log mode */
433 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
435 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
439 lattrs[0] = ls->ls_dn.bv_val;
440 lattrs[1] = ls->ls_req.bv_val;
441 lattrs[2] = ls->ls_mod.bv_val;
442 lattrs[3] = ls->ls_newRdn.bv_val;
443 lattrs[4] = ls->ls_delRdn.bv_val;
444 lattrs[5] = ls->ls_newSup.bv_val;
445 lattrs[6] = ls->ls_controls.bv_val;
446 lattrs[7] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
450 base = si->si_logbase.bv_val;
451 filter = si->si_logfilterstr.bv_val;
454 scope = LDAP_SCOPE_SUBTREE;
457 base = si->si_base.bv_val;
458 filter = si->si_filterstr.bv_val;
459 attrs = si->si_attrs;
460 attrsonly = si->si_attrsonly;
461 scope = si->si_scope;
463 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
464 si->si_type = LDAP_SYNC_REFRESH_ONLY;
466 si->si_type = si->si_ctype;
469 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
471 ber_printf( ber, "{eOb}",
472 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
474 ber_printf( ber, "{eb}",
475 abs(si->si_type), rhint );
478 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == -1 ) {
483 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
484 c[0].ldctl_iscritical = si->si_type < 0;
487 c[1].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
488 BER_BVZERO( &c[1].ldctl_value );
489 c[1].ldctl_iscritical = 1;
492 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
493 c[2].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
494 c[2].ldctl_value = si->si_bindconf.sb_authzId;
495 c[2].ldctl_iscritical = 1;
502 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
503 ctrls, NULL, NULL, si->si_slimit, &si->si_msgid );
516 SlapReply rs = {REP_SEARCH};
517 int i, j, changed = 0;
519 /* Look for contextCSN from syncprov overlay. If
520 * there's no overlay, this will be a no-op. That means
521 * this is a pure consumer, so local changes will not be
522 * allowed, and all changes will already be reflected in
525 a.a_desc = slap_schema.si_ad_contextCSN;
527 e.e_name = si->si_contextdn;
528 e.e_nname = si->si_contextdn;
529 at[0].an_name = a.a_desc->ad_cname;
530 at[0].an_desc = a.a_desc;
531 BER_BVZERO( &at[1].an_name );
533 rs.sr_flags = REP_ENTRY_MODIFIABLE;
535 op->o_req_dn = e.e_name;
536 op->o_req_ndn = e.e_nname;
538 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
539 i = backend_operational( op, &rs );
540 if ( i == LDAP_SUCCESS && a.a_nvals ) {
541 int num = a.a_numvals;
542 /* check for differences */
543 if ( num != si->si_cookieState->cs_num ) {
546 for ( i=0; i<num; i++ ) {
547 if ( ber_bvcmp( &a.a_nvals[i],
548 &si->si_cookieState->cs_vals[i] )) {
555 ber_bvarray_free( si->si_cookieState->cs_vals );
556 ch_free( si->si_cookieState->cs_sids );
557 si->si_cookieState->cs_num = num;
558 si->si_cookieState->cs_vals = a.a_nvals;
559 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_nvals,
561 si->si_cookieState->cs_age++;
563 ber_bvarray_free( a.a_nvals );
565 ber_bvarray_free( a.a_vals );
567 /* See if the cookieState has changed due to anything outside
568 * this particular consumer. That includes other consumers in
569 * the same context, or local changes detected above.
571 if ( si->si_cookieState->cs_num > 0 && si->si_cookieAge !=
572 si->si_cookieState->cs_age ) {
573 if ( !si->si_syncCookie.numcsns ) {
574 ber_bvarray_free( si->si_syncCookie.ctxcsn );
575 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
576 si->si_cookieState->cs_vals, NULL );
579 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
580 /* bogus, just dup everything */
581 if ( si->si_syncCookie.sids[i] == -1 ) {
582 ber_bvarray_free( si->si_syncCookie.ctxcsn );
583 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
584 si->si_cookieState->cs_vals, NULL );
588 for (j=0; j<si->si_cookieState->cs_num; j++) {
589 if ( si->si_syncCookie.sids[i] !=
590 si->si_cookieState->cs_sids[j] )
592 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
593 &si->si_cookieState->cs_vals[j] ))
595 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
596 &si->si_cookieState->cs_vals[j] );
604 si->si_cookieAge = si->si_cookieState->cs_age;
605 ch_free( si->si_syncCookie.octet_str.bv_val );
606 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
607 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
608 si->si_syncCookie.sid );
609 ch_free( si->si_syncCookie.sids );
610 slap_reparse_sync_cookie( &si->si_syncCookie, op->o_tmpmemctx );
612 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
622 int cmdline_cookie_found = 0;
624 struct sync_cookie *sc = NULL;
629 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
630 if ( rc != LDAP_SUCCESS ) {
633 op->o_protocol = LDAP_VERSION3;
635 /* Set SSF to strongest of TLS, SASL SSFs */
638 op->o_transport_ssf = 0;
640 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
641 == LDAP_SUCCESS && ssl != NULL )
643 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
645 #endif /* HAVE_TLS */
647 ber_len_t ssf; /* ITS#5403, 3864 LDAP_OPT_X_SASL_SSF probably ought
648 to use sasl_ssf_t but currently uses ber_len_t */
649 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &ssf )
651 op->o_sasl_ssf = ssf;
653 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
654 ? op->o_sasl_ssf : op->o_tls_ssf;
656 ldap_set_option( si->si_ld, LDAP_OPT_TIMELIMIT, &si->si_tlimit );
658 rc = LDAP_DEREF_NEVER; /* actually could allow DEREF_FINDING */
659 ldap_set_option( si->si_ld, LDAP_OPT_DEREF, &rc );
661 ldap_set_option( si->si_ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
663 si->si_syncCookie.rid = si->si_rid;
665 /* whenever there are multiple data sources possible, advertise sid */
666 si->si_syncCookie.sid = ( SLAP_MULTIMASTER( si->si_be ) || si->si_be != si->si_wbe ) ?
669 /* We've just started up, or the remote server hasn't sent us
670 * any meaningful state.
672 if ( !si->si_syncCookie.ctxcsn ) {
675 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
676 if ( si->si_rid == sc->rid ) {
677 cmdline_cookie_found = 1;
682 if ( cmdline_cookie_found ) {
683 /* cookie is supplied in the command line */
685 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
687 slap_sync_cookie_free( &si->si_syncCookie, 0 );
688 si->si_syncCookie.octet_str = sc->octet_str;
690 /* ctxcsn wasn't parsed yet, do it now */
691 slap_parse_sync_cookie( &si->si_syncCookie, NULL );
693 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
694 if ( !si->si_cookieState->cs_num ) {
695 /* get contextCSN shadow replica from database */
696 BerVarray csn = NULL;
697 void *ctx = op->o_tmpmemctx;
699 op->o_req_ndn = si->si_contextdn;
700 op->o_req_dn = op->o_req_ndn;
702 /* try to read stored contextCSN */
703 op->o_tmpmemctx = NULL;
704 backend_attribute( op, NULL, &op->o_req_ndn,
705 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
706 op->o_tmpmemctx = ctx;
708 si->si_cookieState->cs_vals = csn;
709 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
710 si->si_cookieState->cs_num = i;
711 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
712 slap_sort_csn_sids( csn, si->si_cookieState->cs_sids, i, NULL );
715 if ( si->si_cookieState->cs_num ) {
716 ber_bvarray_free( si->si_syncCookie.ctxcsn );
717 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
718 si->si_cookieState->cs_vals, NULL )) {
720 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
723 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
724 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
726 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
727 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
729 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
732 ch_free( si->si_syncCookie.octet_str.bv_val );
733 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
734 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
735 si->si_syncCookie.sid );
737 /* ITS#6367: recreate the cookie so it has our SID, not our peer's */
738 ch_free( si->si_syncCookie.octet_str.bv_val );
739 BER_BVZERO( &si->si_syncCookie.octet_str );
740 /* Look for contextCSN from syncprov overlay. */
741 check_syncprov( op, si );
742 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
743 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
744 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
745 si->si_syncCookie.sid );
748 si->si_refreshDone = 0;
749 si->si_refreshBeg = slap_get_time();
750 si->si_refreshCount = 0;
751 si->si_refreshTxn = NULL;
752 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s starting refresh\n",
753 si->si_ridtxt, 0, 0 );
755 rc = ldap_sync_search( si, op->o_tmpmemctx );
757 if( rc != LDAP_SUCCESS ) {
758 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
759 "ldap_search_ext: %s (%d)\n",
760 si->si_ridtxt, ldap_err2string( rc ), rc );
766 ldap_unbind_ext( si->si_ld, NULL, NULL );
775 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
782 if ( sc1->numcsns < sc2->numcsns ) {
783 *which = sc1->numcsns;
787 for (j=0; j<sc2->numcsns; j++) {
788 for (i=0; i<sc1->numcsns; i++) {
789 if ( sc1->sids[i] != sc2->sids[j] )
791 value_match( &match, slap_schema.si_ad_entryCSN,
792 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
793 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
794 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
801 if ( i == sc1->numcsns ) {
802 /* sc2 has a sid sc1 lacks */
810 #define SYNC_PAUSED -3
817 BerElementBuffer berbuf;
818 BerElement *ber = (BerElement *)&berbuf;
820 LDAPMessage *msg = NULL;
822 struct sync_cookie syncCookie = { NULL };
823 struct sync_cookie syncCookie_req = { NULL };
828 Modifications *modlist = NULL;
832 struct timeval *tout_p = NULL;
833 struct timeval tout = { 0, 0 };
835 int refreshDeletes = 0;
836 char empty[6] = "empty";
838 if ( slapd_shutdown ) {
843 ber_init2( ber, NULL, LBER_USE_DER );
844 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
846 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
848 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
850 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST && si->si_refreshDone ) {
856 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
857 tout_p, &msg ) ) > 0 )
859 int match, punlock, syncstate;
860 struct berval *retdata, syncUUID[2], cookie = BER_BVNULL;
862 LDAPControl **rctrls = NULL, *rctrlp = NULL;
869 if ( slapd_shutdown ) {
873 switch( ldap_msgtype( msg ) ) {
874 case LDAP_RES_SEARCH_ENTRY:
875 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
876 ldap_get_dn_ber( si->si_ld, msg, NULL, &bdn );
879 bdn.bv_len = sizeof(empty)-1;
881 /* we can't work without the control */
883 LDAPControl **next = NULL;
884 /* NOTE: make sure we use the right one;
885 * a better approach would be to run thru
886 * the whole list and take care of all */
887 /* NOTE: since we issue the search request,
888 * we should know what controls to expect,
889 * and there should be none apart from the
890 * sync-related control */
891 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
892 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
894 bdn.bv_val[bdn.bv_len] = '\0';
895 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
896 "got search entry with multiple "
897 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
898 ldap_controls_free( rctrls );
903 if ( rctrlp == NULL ) {
904 bdn.bv_val[bdn.bv_len] = '\0';
905 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
906 "got search entry without "
907 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
911 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
912 if ( ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID[0] )
914 bdn.bv_val[bdn.bv_len] = '\0';
915 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s malformed message (%s)\n",
916 si->si_ridtxt, bdn.bv_val, 0 );
917 ldap_controls_free( rctrls );
921 /* FIXME: what if syncUUID is NULL or empty?
922 * (happens with back-sql...) */
923 if ( syncUUID[0].bv_len != UUIDLEN ) {
924 bdn.bv_val[bdn.bv_len] = '\0';
925 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
926 "got empty or invalid syncUUID with LDAP_SYNC_%s (%s)\n",
928 syncrepl_state2str( syncstate ), bdn.bv_val );
929 ldap_controls_free( rctrls );
934 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
935 if ( ber_scanf( ber, /*"{"*/ "m}", &cookie ) != LBER_ERROR ) {
937 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
939 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
941 if ( !BER_BVISNULL( &cookie ) ) {
942 ch_free( syncCookie.octet_str.bv_val );
943 ber_dupbv( &syncCookie.octet_str, &cookie );
945 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
947 slap_parse_sync_cookie( &syncCookie, NULL );
948 if ( syncCookie.ctxcsn ) {
949 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
950 check_syncprov( op, si );
951 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
952 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
953 #ifdef CHATTY_SYNCLOG
954 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN for sid %d: %s\n",
955 si->si_ridtxt, i, si->si_cookieState->cs_vals[i].bv_val );
958 if ( sid < si->si_cookieState->cs_sids[i] )
960 if ( si->si_cookieState->cs_sids[i] == sid ) {
961 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
962 bdn.bv_val[bdn.bv_len] = '\0';
963 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s (%s)\n",
964 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
966 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
967 ldap_controls_free( rctrls );
975 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
977 /* check pending CSNs too */
978 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
979 if ( slapd_shutdown ) {
983 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
984 ldap_pvt_thread_yield();
987 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
988 if ( sid < si->si_cookieState->cs_psids[i] )
990 if ( si->si_cookieState->cs_psids[i] == sid ) {
991 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
992 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
993 bdn.bv_val[bdn.bv_len] = '\0';
994 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s (%s)\n",
995 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
996 ldap_controls_free( rctrls );
1000 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
1001 syncCookie.ctxcsn );
1005 /* new SID, add it */
1006 if ( i == si->si_cookieState->cs_pnum ||
1007 sid != si->si_cookieState->cs_psids[i] ) {
1008 slap_insert_csn_sids(
1009 (struct sync_cookie *)&si->si_cookieState->cs_pvals,
1010 i, sid, syncCookie.ctxcsn );
1012 assert( punlock < 0 );
1014 } else if (si->si_too_old) {
1015 bdn.bv_val[bdn.bv_len] = '\0';
1016 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring (%s)\n",
1017 si->si_ridtxt, bdn.bv_val, 0 );
1018 ldap_controls_free( rctrls );
1022 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1027 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
1029 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
1032 rc = syncrepl_updateCookie( si, op, &syncCookie, 0 );
1033 } else switch ( rc ) {
1034 case LDAP_ALREADY_EXISTS:
1035 case LDAP_NO_SUCH_OBJECT:
1036 case LDAP_NO_SUCH_ATTRIBUTE:
1037 case LDAP_TYPE_OR_VALUE_EXISTS:
1038 case LDAP_NOT_ALLOWED_ON_NONLEAF:
1039 rc = LDAP_SYNC_REFRESH_REQUIRED;
1040 si->si_logstate = SYNCLOG_FALLBACK;
1041 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
1042 bdn.bv_val[bdn.bv_len] = '\0';
1043 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync on (%s), switching to REFRESH\n",
1044 si->si_ridtxt, bdn.bv_val, 0 );
1045 if (si->si_strict_refresh) {
1046 slap_suspend_listeners();
1053 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
1054 &modlist, &entry, syncstate, syncUUID ) ) == LDAP_SUCCESS )
1056 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
1057 syncstate, syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
1060 rc = syncrepl_updateCookie( si, op, &syncCookie, 0 );
1063 if ( punlock >= 0 ) {
1064 /* on failure, revert pending CSN */
1065 if ( rc != LDAP_SUCCESS ) {
1067 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
1068 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
1069 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
1070 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
1071 &si->si_cookieState->cs_vals[i] );
1075 if ( i == si->si_cookieState->cs_num )
1076 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
1077 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
1079 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
1081 ldap_controls_free( rctrls );
1083 slap_mods_free( modlist, 1 );
1089 case LDAP_RES_SEARCH_REFERENCE:
1090 Debug( LDAP_DEBUG_ANY,
1091 "do_syncrep2: %s reference received error\n",
1092 si->si_ridtxt, 0, 0 );
1095 case LDAP_RES_SEARCH_RESULT:
1096 Debug( LDAP_DEBUG_SYNC,
1097 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
1098 si->si_ridtxt, 0, 0 );
1099 err = LDAP_OTHER; /* FIXME check parse result properly */
1100 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
1102 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
1103 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
1104 /* map old result code to registered code */
1105 err = LDAP_SYNC_REFRESH_REQUIRED;
1108 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
1109 if ( si->si_logstate == SYNCLOG_LOGGING ) {
1110 si->si_logstate = SYNCLOG_FALLBACK;
1111 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync, switching to REFRESH\n",
1112 si->si_ridtxt, 0, 0 );
1113 if (si->si_strict_refresh) {
1114 slap_suspend_listeners();
1122 Debug( LDAP_DEBUG_ANY,
1123 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1124 si->si_ridtxt, err, ldap_err2string( err ) );
1127 LDAPControl **next = NULL;
1128 /* NOTE: make sure we use the right one;
1129 * a better approach would be to run thru
1130 * the whole list and take care of all */
1131 /* NOTE: since we issue the search request,
1132 * we should know what controls to expect,
1133 * and there should be none apart from the
1134 * sync-related control */
1135 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1136 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1138 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1139 "got search result with multiple "
1140 "Sync State control\n", si->si_ridtxt, 0, 0 );
1141 ldap_controls_free( rctrls );
1147 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1149 ber_scanf( ber, "{" /*"}"*/);
1150 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1151 ber_scanf( ber, "m", &cookie );
1153 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1155 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1157 if ( !BER_BVISNULL( &cookie ) ) {
1158 ch_free( syncCookie.octet_str.bv_val );
1159 ber_dupbv( &syncCookie.octet_str, &cookie);
1161 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1163 slap_parse_sync_cookie( &syncCookie, NULL );
1164 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1167 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1169 ber_scanf( ber, "b", &refreshDeletes );
1171 ber_scanf( ber, /*"{"*/ "}" );
1173 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1174 slap_sync_cookie_free( &syncCookie_req, 0 );
1175 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1177 if ( !syncCookie.ctxcsn ) {
1179 } else if ( !syncCookie_req.ctxcsn ) {
1183 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1186 ldap_controls_free( rctrls );
1188 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1189 /* FIXME : different error behaviors according to
1190 * 1) err code : LDAP_BUSY ...
1191 * 2) on err policy : stop service, stop sync, retry
1193 if ( refreshDeletes == 0 && match < 0 &&
1194 err == LDAP_SUCCESS &&
1195 syncCookie_req.numcsns == syncCookie.numcsns )
1197 syncrepl_del_nonpresent( op, si, NULL,
1199 } else if ( si->si_presentlist ) {
1200 presentlist_free( si->si_presentlist );
1201 si->si_presentlist = NULL;
1204 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1206 rc = syncrepl_updateCookie( si, op, &syncCookie, 1 );
1208 if ( si->si_refreshCount ) {
1209 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
1210 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
1211 si->si_refreshCount = 0;
1212 si->si_refreshTxn = NULL;
1214 si->si_refreshEnd = slap_get_time();
1215 if ( err == LDAP_SUCCESS
1216 && si->si_logstate == SYNCLOG_FALLBACK ) {
1217 si->si_logstate = SYNCLOG_LOGGING;
1218 rc = LDAP_SYNC_REFRESH_REQUIRED;
1219 slap_resume_listeners();
1225 case LDAP_RES_INTERMEDIATE:
1228 rc = ldap_parse_intermediate( si->si_ld, msg,
1229 &retoid, &retdata, NULL, 0 );
1230 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1231 ber_init2( ber, retdata, LBER_USE_DER );
1233 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1235 case LDAP_TAG_SYNC_NEW_COOKIE:
1236 Debug( LDAP_DEBUG_SYNC,
1237 "do_syncrep2: %s %s - %s\n",
1239 "LDAP_RES_INTERMEDIATE",
1241 ber_scanf( ber, "tm", &tag, &cookie );
1242 Debug( LDAP_DEBUG_SYNC,
1243 "do_syncrep2: %s NEW_COOKIE: %s\n",
1246 if ( !BER_BVISNULL( &cookie ) ) {
1247 ch_free( syncCookie.octet_str.bv_val );
1248 ber_dupbv( &syncCookie.octet_str, &cookie );
1250 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1251 slap_parse_sync_cookie( &syncCookie, NULL );
1252 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1255 case LDAP_TAG_SYNC_REFRESH_DELETE:
1256 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1257 Debug( LDAP_DEBUG_SYNC,
1258 "do_syncrep2: %s %s - %s\n",
1260 "LDAP_RES_INTERMEDIATE",
1261 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1262 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1263 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1264 si->si_refreshDelete = 1;
1266 si->si_refreshPresent = 1;
1268 ber_scanf( ber, "t{" /*"}"*/, &tag );
1269 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1271 ber_scanf( ber, "m", &cookie );
1273 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1275 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1277 if ( !BER_BVISNULL( &cookie ) ) {
1278 ch_free( syncCookie.octet_str.bv_val );
1279 ber_dupbv( &syncCookie.octet_str, &cookie );
1281 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1283 slap_parse_sync_cookie( &syncCookie, NULL );
1284 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1287 /* Defaults to TRUE */
1288 if ( ber_peek_tag( ber, &len ) ==
1289 LDAP_TAG_REFRESHDONE )
1291 ber_scanf( ber, "b", &si->si_refreshDone );
1294 si->si_refreshDone = 1;
1296 if ( si->si_refreshDone ) {
1297 if ( si->si_refreshCount ) {
1298 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
1299 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
1300 si->si_refreshCount = 0;
1301 si->si_refreshTxn = NULL;
1303 si->si_refreshEnd = slap_get_time();
1304 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s finished refresh\n",
1305 si->si_ridtxt, 0, 0 );
1307 ber_scanf( ber, /*"{"*/ "}" );
1308 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
1309 si->si_refreshDone )
1312 case LDAP_TAG_SYNC_ID_SET:
1313 Debug( LDAP_DEBUG_SYNC,
1314 "do_syncrep2: %s %s - %s\n",
1316 "LDAP_RES_INTERMEDIATE",
1318 ber_scanf( ber, "t{" /*"}"*/, &tag );
1319 if ( ber_peek_tag( ber, &len ) ==
1320 LDAP_TAG_SYNC_COOKIE )
1322 ber_scanf( ber, "m", &cookie );
1324 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1326 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1328 if ( !BER_BVISNULL( &cookie ) ) {
1329 ch_free( syncCookie.octet_str.bv_val );
1330 ber_dupbv( &syncCookie.octet_str, &cookie );
1332 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1334 slap_parse_sync_cookie( &syncCookie, NULL );
1335 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1336 compare_csns( &syncCookie_req, &syncCookie, &m );
1339 if ( ber_peek_tag( ber, &len ) ==
1340 LDAP_TAG_REFRESHDELETES )
1342 ber_scanf( ber, "b", &refreshDeletes );
1345 rc = ber_scanf( ber, "[W]", &syncUUIDs );
1346 ber_scanf( ber, /*"{"*/ "}" );
1347 if ( rc != LBER_ERROR ) {
1348 if ( refreshDeletes ) {
1349 syncrepl_del_nonpresent( op, si, syncUUIDs,
1351 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1354 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1355 (void)presentlist_insert( si, &syncUUIDs[i] );
1356 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1358 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1362 slap_sync_cookie_free( &syncCookie, 0 );
1365 Debug( LDAP_DEBUG_ANY,
1366 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1367 si->si_ridtxt, (long) si_tag, 0 );
1368 ldap_memfree( retoid );
1369 ber_bvfree( retdata );
1373 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1374 slap_sync_cookie_free( &syncCookie_req, 0 );
1375 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1377 if ( !syncCookie.ctxcsn ) {
1379 } else if ( !syncCookie_req.ctxcsn ) {
1383 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1387 if ( si->si_refreshPresent == 1 &&
1388 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1389 syncCookie_req.numcsns == syncCookie.numcsns ) {
1390 syncrepl_del_nonpresent( op, si, NULL,
1394 if ( syncCookie.ctxcsn )
1396 rc = syncrepl_updateCookie( si, op, &syncCookie, 1 );
1398 if ( si->si_presentlist ) {
1399 presentlist_free( si->si_presentlist );
1400 si->si_presentlist = NULL;
1404 ldap_memfree( retoid );
1405 ber_bvfree( retdata );
1411 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1412 "unknown intermediate response (%d)\n",
1413 si->si_ridtxt, rc, 0 );
1414 ldap_memfree( retoid );
1415 ber_bvfree( retdata );
1420 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1421 "unknown message (0x%02lx)\n",
1423 (unsigned long)ldap_msgtype( msg ), 0 );
1427 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1428 slap_sync_cookie_free( &syncCookie_req, 0 );
1429 syncCookie_req = syncCookie;
1430 memset( &syncCookie, 0, sizeof( syncCookie ));
1432 ldap_msgfree( msg );
1434 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1435 slap_sync_cookie_free( &syncCookie, 0 );
1436 slap_sync_cookie_free( &syncCookie_req, 0 );
1437 if ( si->si_refreshCount ) {
1438 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
1439 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
1440 si->si_refreshCount = 0;
1441 si->si_refreshTxn = NULL;
1449 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1454 if ( err != LDAP_SUCCESS ) {
1455 Debug( LDAP_DEBUG_ANY,
1456 "do_syncrep2: %s (%d) %s\n",
1457 si->si_ridtxt, err, ldap_err2string( err ) );
1460 slap_sync_cookie_free( &syncCookie, 0 );
1461 slap_sync_cookie_free( &syncCookie_req, 0 );
1463 if ( msg ) ldap_msgfree( msg );
1465 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1466 if ( si->si_conn ) {
1467 connection_client_stop( si->si_conn );
1470 ldap_unbind_ext( si->si_ld, NULL, NULL );
1482 struct re_s* rtask = arg;
1483 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1484 Connection conn = {0};
1485 OperationBuffer opbuf;
1487 int rc = LDAP_SUCCESS;
1490 int i, defer = 1, fail = 0, freeinfo = 0;
1495 if ( slapd_shutdown )
1498 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1500 /* Don't get stuck here while a pause is initiated */
1501 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1502 if ( slapd_shutdown )
1504 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1505 ldap_pvt_thread_yield();
1510 if ( si->si_ctype < 1 ) {
1514 switch( abs( si->si_type ) ) {
1515 case LDAP_SYNC_REFRESH_ONLY:
1516 case LDAP_SYNC_REFRESH_AND_PERSIST:
1519 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1523 if ( slapd_shutdown ) {
1525 if ( si->si_conn ) {
1526 connection_client_stop( si->si_conn );
1529 ldap_unbind_ext( si->si_ld, NULL, NULL );
1532 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1536 connection_fake_init( &conn, &opbuf, ctx );
1538 /* o_connids must be unique for slap_graduate_commit_csn */
1539 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1541 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1544 /* Coordinate contextCSN updates with any syncprov overlays
1545 * in use. This may be complicated by the use of the glue
1548 * Typically there is a single syncprov mastering the entire
1549 * glued tree. In that case, our contextCSN updates should
1550 * go to the master DB. But if there is no syncprov on the
1551 * master DB, then nothing special is needed here.
1553 * Alternatively, there may be individual syncprov overlays
1554 * on each glued branch. In that case, each syncprov only
1555 * knows about changes within its own branch. And so our
1556 * contextCSN updates should only go to the local DB.
1558 if ( !si->si_wbe ) {
1559 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1560 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1561 if ( overlay_is_inst( top_be, "syncprov" ))
1562 si->si_wbe = top_be;
1568 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1569 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1570 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1572 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1575 if ( !si->si_schemachecking )
1576 op->o_no_schema_check = 1;
1578 /* Establish session, do search */
1580 si->si_refreshDelete = 0;
1581 si->si_refreshPresent = 0;
1583 if ( si->si_presentlist ) {
1584 presentlist_free( si->si_presentlist );
1585 si->si_presentlist = NULL;
1588 /* use main DB when retrieving contextCSN */
1589 op->o_bd = si->si_wbe;
1590 op->o_dn = op->o_bd->be_rootdn;
1591 op->o_ndn = op->o_bd->be_rootndn;
1592 rc = do_syncrep1( op, si );
1596 /* Process results */
1597 if ( rc == LDAP_SUCCESS ) {
1598 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1600 /* use current DB */
1602 op->o_dn = op->o_bd->be_rootdn;
1603 op->o_ndn = op->o_bd->be_rootndn;
1604 rc = do_syncrep2( op, si );
1605 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1606 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
1607 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
1608 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
1609 si->si_syncCookie.sid );
1610 rc = ldap_sync_search( si, op->o_tmpmemctx );
1615 /* We got deleted while running on cn=config */
1616 if ( si->si_ctype < 1 ) {
1617 if ( si->si_ctype == -1 ) {
1626 if ( rc != SYNC_PAUSED ) {
1627 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1628 /* If we succeeded, enable the connection for further listening.
1629 * If we failed, tear down the connection and reschedule.
1631 if ( rc == LDAP_SUCCESS ) {
1632 if ( si->si_conn ) {
1633 connection_client_enable( si->si_conn );
1635 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1637 } else if ( si->si_conn ) {
1641 if ( rc == -2 ) rc = 0;
1646 /* At this point, we have 5 cases:
1647 * 1) for any hard failure, give up and remove this task
1648 * 2) for ServerDown, reschedule this task to run later
1649 * 3) for threadpool pause, reschedule to run immediately
1650 * 4) for Refresh and Success, reschedule to run
1651 * 5) for Persist and Success, reschedule to defer
1653 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1655 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1656 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1660 connection_client_stop( si->si_conn );
1664 if ( rc == SYNC_PAUSED ) {
1665 rtask->interval.tv_sec = 0;
1666 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1667 rtask->interval.tv_sec = si->si_interval;
1669 } else if ( rc == LDAP_SUCCESS ) {
1670 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1673 rtask->interval.tv_sec = si->si_interval;
1674 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1675 if ( si->si_retrynum ) {
1676 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1677 si->si_retrynum[i] = si->si_retrynum_init[i];
1679 si->si_retrynum[i] = RETRYNUM_TAIL;
1682 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1683 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1687 if ( si->si_ctype < 1
1688 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1690 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1693 fail = RETRYNUM_TAIL;
1694 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1695 if ( si->si_retrynum[i] > 0 )
1696 si->si_retrynum[i]--;
1697 fail = si->si_retrynum[i];
1698 rtask->interval.tv_sec = si->si_retryinterval[i];
1699 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1700 slap_wake_listener();
1704 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1705 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1708 if ( fail == RETRYNUM_TAIL ) {
1709 Debug( LDAP_DEBUG_ANY,
1710 "do_syncrepl: %s rc %d quitting\n",
1711 si->si_ridtxt, rc, 0 );
1712 } else if ( fail > 0 ) {
1713 Debug( LDAP_DEBUG_ANY,
1714 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1715 si->si_ridtxt, rc, fail );
1717 Debug( LDAP_DEBUG_ANY,
1718 "do_syncrepl: %s rc %d retrying\n",
1719 si->si_ridtxt, rc, 0 );
1723 /* Do final delete cleanup */
1725 syncinfo_free( si, 0 );
1730 #ifdef ENABLE_REWRITE
1732 syncrepl_rewrite_dn(
1735 struct berval *sdn )
1740 nul = dn->bv_val[dn->bv_len];
1741 dn->bv_val[dn->bv_len] = 0;
1742 rc = rewrite( si->si_rewrite, SUFFIXM_CTX, dn->bv_val, &sdn->bv_val );
1743 dn->bv_val[dn->bv_len] = nul;
1745 if ( sdn->bv_val == dn->bv_val )
1747 else if ( rc == REWRITE_REGEXEC_OK && sdn->bv_val )
1748 sdn->bv_len = strlen( sdn->bv_val );
1751 #define REWRITE_VAL(si, ad, bv, bv2) \
1752 BER_BVZERO( &bv2 ); \
1753 if ( si->si_rewrite && ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName) \
1754 syncrepl_rewrite_dn( si, &bv, &bv2); \
1755 if ( BER_BVISNULL( &bv2 )) \
1756 ber_dupbv( &bv2, &bv )
1757 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1758 BER_BVZERO( &bv2 ); \
1759 if (si->si_rewrite) \
1760 syncrepl_rewrite_dn(si, &bv, &bv2); \
1761 rc = dnPrettyNormal( NULL, bv2.bv_val ? &bv2 : &bv, &dn, &ndn, op->o_tmpmemctx ); \
1764 #define REWRITE_VAL(si, ad, bv, bv2) ber_dupbv(&bv2, &bv)
1765 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1766 rc = dnPrettyNormal( NULL, &bv, &dn, &ndn, op->o_tmpmemctx )
1770 static slap_verbmasks modops[] = {
1771 { BER_BVC("add"), LDAP_REQ_ADD },
1772 { BER_BVC("delete"), LDAP_REQ_DELETE },
1773 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1774 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1779 syncrepl_accesslog_mods(
1781 struct berval *vals,
1782 struct Modifications **modres
1787 AttributeDescription *ad;
1788 struct berval bv, bv2;
1790 Modifications *mod = NULL, *modlist = NULL, **modtail;
1795 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1799 colon = ber_bvchr( &bv, ':' );
1803 } else if ( colon == bv.bv_val ) {
1804 /* ITS#6545: An empty attribute signals that a new mod
1805 * is about to start */
1810 bv.bv_len = colon - bv.bv_val;
1811 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1813 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1814 "Invalid attribute %s, %s\n",
1815 si->si_ridtxt, bv.bv_val, text );
1816 slap_mods_free( modlist, 1 );
1822 /* Ignore dynamically generated attrs */
1823 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1827 /* Ignore excluded attrs */
1828 if ( ldap_charray_inlist( si->si_exattrs,
1829 ad->ad_type->sat_cname.bv_val ) )
1835 case '+': op = LDAP_MOD_ADD; break;
1836 case '-': op = LDAP_MOD_DELETE; break;
1837 case '=': op = LDAP_MOD_REPLACE; break;
1838 case '#': op = LDAP_MOD_INCREMENT; break;
1842 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1843 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1846 mod->sml_next = NULL;
1848 mod->sml_type = ad->ad_cname;
1849 mod->sml_values = NULL;
1850 mod->sml_nvalues = NULL;
1851 mod->sml_numvals = 0;
1854 modtail = &mod->sml_next;
1856 if ( colon[2] == ' ' ) {
1857 bv.bv_val = colon + 3;
1858 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1859 REWRITE_VAL( si, ad, bv, bv2 );
1860 ber_bvarray_add( &mod->sml_values, &bv2 );
1869 syncrepl_changelog_mods(
1871 struct berval *vals,
1872 struct Modifications **modres
1875 return -1; /* FIXME */
1878 typedef struct OpExtraSync {
1883 /* Copy the original modlist, split Replace ops into Delete/Add,
1884 * and drop mod opattrs since this modification is in the past.
1886 static Modifications *mods_dup( Operation *op, Modifications *modlist, int match )
1888 Modifications *mod, *modnew = NULL, *modtail = NULL;
1890 for ( ; modlist; modlist = modlist->sml_next ) {
1893 if ( modlist->sml_desc == slap_schema.si_ad_modifiersName ||
1894 modlist->sml_desc == slap_schema.si_ad_modifyTimestamp ||
1895 modlist->sml_desc == slap_schema.si_ad_entryCSN )
1897 if ( modlist->sml_op == LDAP_MOD_REPLACE ) {
1898 mod = op->o_tmpalloc( sizeof(Modifications), op->o_tmpmemctx );
1899 mod->sml_desc = modlist->sml_desc;
1900 mod->sml_values = NULL;
1901 mod->sml_nvalues = NULL;
1902 mod->sml_op = LDAP_MOD_DELETE;
1903 mod->sml_numvals = 0;
1908 modtail->sml_next = mod;
1912 if ( modlist->sml_numvals ) {
1913 size = (modlist->sml_numvals+1) * sizeof(struct berval);
1914 if ( modlist->sml_nvalues ) size *= 2;
1918 size += sizeof(Modifications);
1919 mod = op->o_tmpalloc( size, op->o_tmpmemctx );
1923 modtail->sml_next = mod;
1925 mod->sml_desc = modlist->sml_desc;
1926 mod->sml_numvals = modlist->sml_numvals;
1928 if ( modlist->sml_numvals ) {
1930 mod->sml_values = (BerVarray)(mod+1);
1931 for (i=0; i<mod->sml_numvals; i++)
1932 mod->sml_values[i] = modlist->sml_values[i];
1933 BER_BVZERO(&mod->sml_values[i]);
1934 if ( modlist->sml_nvalues ) {
1935 mod->sml_nvalues = mod->sml_values + mod->sml_numvals + 1;
1936 for (i=0; i<mod->sml_numvals; i++)
1937 mod->sml_nvalues[i] = modlist->sml_nvalues[i];
1938 BER_BVZERO(&mod->sml_nvalues[i]);
1940 mod->sml_nvalues = NULL;
1943 mod->sml_values = NULL;
1944 mod->sml_nvalues = NULL;
1946 if ( match < 0 && modlist->sml_op == LDAP_MOD_REPLACE )
1947 mod->sml_op = LDAP_MOD_ADD;
1949 mod->sml_op = modlist->sml_op;
1950 mod->sml_next = NULL;
1955 typedef struct resolve_ctxt {
1957 Modifications *rx_mods;
1961 compare_vals( Modifications *m1, Modifications *m2 )
1964 struct berval *bv1, *bv2;
1966 if ( m2->sml_nvalues ) {
1967 bv2 = m2->sml_nvalues;
1968 bv1 = m1->sml_nvalues;
1970 bv2 = m2->sml_values;
1971 bv1 = m1->sml_values;
1973 for ( j=0; j<m2->sml_numvals; j++ ) {
1974 for ( i=0; i<m1->sml_numvals; i++ ) {
1975 if ( !ber_bvcmp( &bv1[i], &bv2[j] )) {
1977 for ( k=i; k<m1->sml_numvals-1; k++ ) {
1978 m1->sml_values[k] = m1->sml_values[k+1];
1979 if ( m1->sml_nvalues )
1980 m1->sml_nvalues[k] = m1->sml_nvalues[k+1];
1982 BER_BVZERO(&m1->sml_values[k]);
1983 if ( m1->sml_nvalues ) {
1984 BER_BVZERO(&m1->sml_nvalues[k]);
1994 syncrepl_resolve_cb( Operation *op, SlapReply *rs )
1996 if ( rs->sr_type == REP_SEARCH ) {
1997 resolve_ctxt *rx = op->o_callback->sc_private;
1998 Attribute *a = attr_find( rs->sr_entry->e_attrs, ad_reqMod );
2000 Modifications *oldmods, *newmods, *m1, *m2, **prev;
2001 oldmods = rx->rx_mods;
2002 syncrepl_accesslog_mods( rx->rx_si, a->a_vals, &newmods );
2003 for ( m2 = newmods; m2; m2=m2->sml_next ) {
2004 for ( prev = &oldmods, m1 = *prev; m1; m1 = *prev ) {
2005 if ( m1->sml_desc != m2->sml_desc ) {
2006 prev = &m1->sml_next;
2009 if ( m2->sml_op == LDAP_MOD_DELETE ||
2010 m2->sml_op == LDAP_MOD_REPLACE ) {
2011 int numvals = m2->sml_numvals;
2012 if ( m2->sml_op == LDAP_MOD_REPLACE )
2014 /* New delete All cancels everything */
2015 if ( numvals == 0 ) {
2017 *prev = m1->sml_next;
2018 op->o_tmpfree( m1, op->o_tmpmemctx );
2021 if ( m1->sml_op == LDAP_MOD_DELETE ) {
2022 if ( m1->sml_numvals == 0 ) {
2023 /* turn this to SOFTDEL later */
2024 m1->sml_flags = SLAP_MOD_INTERNAL;
2026 compare_vals( m1, m2 );
2027 if ( !m1->sml_numvals )
2030 } else if ( m1->sml_op == LDAP_MOD_ADD ) {
2031 compare_vals( m1, m2 );
2032 if ( !m1->sml_numvals )
2037 if ( m2->sml_op == LDAP_MOD_ADD ||
2038 m2->sml_op == LDAP_MOD_REPLACE ) {
2039 if ( m1->sml_op == LDAP_MOD_DELETE ) {
2040 if ( !m1->sml_numvals ) goto drop;
2041 compare_vals( m1, m2 );
2042 if ( !m1->sml_numvals )
2045 if ( m2->sml_desc->ad_type->sat_atype.at_single_value )
2047 compare_vals( m1, m2 );
2048 if ( !m1->sml_numvals )
2051 prev = &m1->sml_next;
2054 slap_mods_free( newmods, 1 );
2055 rx->rx_mods = oldmods;
2058 return LDAP_SUCCESS;
2061 typedef struct modify_ctxt {
2062 Modifications *mx_orig;
2063 Modifications *mx_free;
2067 syncrepl_modify_cb( Operation *op, SlapReply *rs )
2069 slap_callback *sc = op->o_callback;
2070 modify_ctxt *mx = sc->sc_private;
2073 op->orm_no_opattrs = 0;
2074 op->orm_modlist = mx->mx_orig;
2075 for ( ml = mx->mx_free; ml; ml = mx->mx_free ) {
2076 mx->mx_free = ml->sml_next;
2077 op->o_tmpfree( ml, op->o_tmpmemctx );
2079 op->o_callback = sc->sc_next;
2080 op->o_tmpfree( sc, op->o_tmpmemctx );
2081 return SLAP_CB_CONTINUE;
2085 syncrepl_op_modify( Operation *op, SlapReply *rs )
2087 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2092 Modifications *mod, *newlist;
2094 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
2095 if ( oex->oe_key == (void *)syncrepl_message_to_op )
2099 return SLAP_CB_CONTINUE;
2101 si = ((OpExtraSync *)oex)->oe_si;
2103 /* Check if entryCSN in modlist is newer than entryCSN in entry.
2104 * We do it here because the op has been serialized by accesslog
2105 * by the time we get here. If the CSN is new enough, just do the
2106 * mod. If not, we need to resolve conflicts.
2109 for ( mod = op->orm_modlist; mod; mod=mod->sml_next ) {
2110 if ( mod->sml_desc == slap_schema.si_ad_entryCSN ) break;
2112 /* FIXME: what should we do if entryCSN is missing from the mod? */
2114 return SLAP_CB_CONTINUE;
2118 sid = slap_parse_csn_sid( &mod->sml_nvalues[0] );
2119 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
2120 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
2121 #ifdef CHATTY_SYNCLOG
2122 Debug( LDAP_DEBUG_SYNC, "syncrepl_op_modify: %s CSN for sid %d: %s\n",
2123 si->si_ridtxt, i, si->si_cookieState->cs_vals[i].bv_val );
2126 if ( sid < si->si_cookieState->cs_sids[i] )
2128 if ( si->si_cookieState->cs_sids[i] == sid ) {
2129 if ( ber_bvcmp( &mod->sml_nvalues[0], &si->si_cookieState->cs_vals[i] ) <= 0 ) {
2130 Debug( LDAP_DEBUG_SYNC, "syncrepl_op_modify: %s entryCSN too old, ignoring %s (%s)\n",
2131 si->si_ridtxt, mod->sml_nvalues[0].bv_val, op->o_req_dn.bv_val );
2132 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2133 slap_graduate_commit_csn( op );
2134 /* tell accesslog this was a failure */
2135 rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
2136 return LDAP_SUCCESS;
2140 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2143 rc = overlay_entry_get_ov( op, &op->o_req_ndn, NULL, NULL, 0, &e, on );
2147 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
2148 value_match( &match, slap_schema.si_ad_entryCSN,
2149 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
2150 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2151 &mod->sml_nvalues[0], &a->a_nvals[0], &text );
2152 overlay_entry_release_ov( op, e, 0, on );
2154 /* equal? Should never happen */
2156 slap_graduate_commit_csn( op );
2157 /* tell accesslog this was a failure */
2158 rs->sr_err = LDAP_TYPE_OR_VALUE_EXISTS;
2159 return LDAP_SUCCESS;
2162 /* mod is older: resolve conflicts...
2163 * 1. Save/copy original modlist. Split Replace to Del/Add.
2164 * 2. Find all mods to this reqDN newer than the mod stamp.
2165 * 3. Resolve any mods in this request that affect attributes
2166 * touched by newer mods.
2168 * delete all delete all drop
2169 * delete all delete X SOFTDEL
2170 * delete X delete all drop
2171 * delete X delete X drop
2172 * delete X delete Y OK
2173 * delete all add X drop
2174 * delete X add X drop
2176 * add X delete all drop
2177 * add X delete X drop
2179 * add X add Y if SV, drop else OK
2181 * 4. Swap original modlist back in response callback so
2182 * that accesslog logs the original mod.
2184 * Even if the mod is newer, other out-of-order changes may
2185 * have been committed, forcing us to tweak the modlist:
2186 * 1. Save/copy original modlist.
2187 * 2. Change deletes to soft deletes.
2188 * 3. Change Adds of single-valued attrs to Replace.
2191 newlist = mods_dup( op, op->orm_modlist, match );
2195 Operation op2 = *op;
2196 AttributeName an[2];
2199 SlapReply rs1 = {0};
2201 slap_callback cb = { NULL, syncrepl_resolve_cb, NULL, NULL };
2203 AttributeAssertion aa[2] = {0};
2206 rx.rx_mods = newlist;
2207 cb.sc_private = ℞
2209 op2.o_tag = LDAP_REQ_SEARCH;
2210 op2.ors_scope = LDAP_SCOPE_SUBTREE;
2211 op2.ors_deref = LDAP_DEREF_NEVER;
2212 op2.o_req_dn = si->si_logbase;
2213 op2.o_req_ndn = si->si_logbase;
2214 op2.ors_tlimit = SLAP_NO_LIMIT;
2215 op2.ors_slimit = SLAP_NO_LIMIT;
2216 op2.ors_limit = NULL;
2217 memset( an, 0, sizeof(an));
2218 an[0].an_desc = ad_reqMod;
2219 an[0].an_name = ad_reqMod->ad_cname;
2221 op2.ors_attrsonly = 0;
2223 bv = mod->sml_nvalues[0];
2225 size = sizeof("(&(entryCSN>=)(reqDN=))");
2226 size += bv.bv_len + op->o_req_ndn.bv_len + si->si_logfilterstr.bv_len;
2227 op2.ors_filterstr.bv_val = op->o_tmpalloc( size, op->o_tmpmemctx );
2228 op2.ors_filterstr.bv_len = sprintf(op2.ors_filterstr.bv_val,
2229 "(&(entryCSN>=%s)(reqDN=%s)%s)",
2230 bv.bv_val, op->o_req_ndn.bv_val, si->si_logfilterstr.bv_val );
2232 lf[0].f_choice = LDAP_FILTER_AND;
2234 lf[1].f_choice = LDAP_FILTER_GE;
2236 lf[1].f_av_desc = slap_schema.si_ad_entryCSN;
2237 lf[1].f_av_value = bv;
2238 lf[1].f_next = lf+2;
2239 lf[2].f_choice = LDAP_FILTER_EQUALITY;
2241 lf[2].f_av_desc = ad_reqDN;
2242 lf[2].f_av_value = op->o_req_ndn;
2243 lf[2].f_next = si->si_logfilter;
2245 op2.ors_filter = lf;
2247 op2.o_callback = &cb;
2248 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2249 op2.o_bd->be_search( &op2, &rs1 );
2250 newlist = rx.rx_mods;
2254 slap_callback *sc = op->o_tmpalloc( sizeof(slap_callback) +
2255 sizeof(modify_ctxt), op->o_tmpmemctx );
2256 modify_ctxt *mx = (modify_ctxt *)(sc+1);
2259 sc->sc_response = syncrepl_modify_cb;
2260 sc->sc_private = mx;
2261 sc->sc_next = op->o_callback;
2262 sc->sc_cleanup = NULL;
2263 sc->sc_writewait = NULL;
2264 op->o_callback = sc;
2265 op->orm_no_opattrs = 1;
2266 mx->mx_orig = op->orm_modlist;
2267 mx->mx_free = newlist;
2268 for ( ml = newlist; ml; ml=ml->sml_next ) {
2269 if ( ml->sml_flags == SLAP_MOD_INTERNAL ) {
2271 ml->sml_op = SLAP_MOD_SOFTDEL;
2273 else if ( ml->sml_op == LDAP_MOD_DELETE )
2274 ml->sml_op = SLAP_MOD_SOFTDEL;
2275 else if ( ml->sml_op == LDAP_MOD_ADD &&
2276 ml->sml_desc->ad_type->sat_atype.at_single_value )
2277 ml->sml_op = LDAP_MOD_REPLACE;
2279 op->orm_modlist = newlist;
2280 op->o_csn = mod->sml_nvalues[0];
2282 return SLAP_CB_CONTINUE;
2286 syncrepl_null_callback(
2290 /* If we're not the last callback in the chain, move to the end */
2291 if ( op->o_callback->sc_next ) {
2292 slap_callback **sc, *s1;
2293 s1 = op->o_callback;
2294 op->o_callback = op->o_callback->sc_next;
2295 for ( sc = &op->o_callback; *sc; sc = &(*sc)->sc_next ) ;
2298 return SLAP_CB_CONTINUE;
2300 if ( rs->sr_err != LDAP_SUCCESS &&
2301 rs->sr_err != LDAP_REFERRAL &&
2302 rs->sr_err != LDAP_ALREADY_EXISTS &&
2303 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2304 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2306 Debug( LDAP_DEBUG_ANY,
2307 "syncrepl_null_callback : error code 0x%x\n",
2310 return LDAP_SUCCESS;
2314 syncrepl_message_to_op(
2320 BerElement *ber = NULL;
2321 Modifications *modlist = NULL;
2323 SlapReply rs = { REP_RESULT };
2324 slap_callback cb = { NULL, syncrepl_null_callback, NULL, NULL };
2327 char txtbuf[SLAP_TEXT_BUFLEN];
2328 size_t textlen = sizeof txtbuf;
2330 struct berval bdn, dn = BER_BVNULL, ndn;
2331 struct berval bv, bv2, *bvals = NULL;
2332 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
2333 prdn = BER_BVNULL, nrdn = BER_BVNULL,
2334 psup = BER_BVNULL, nsup = BER_BVNULL;
2335 int rc, deleteOldRdn = 0, freeReqDn = 0;
2336 int do_graduate = 0;
2338 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2339 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2340 "Message type should be entry (%d)",
2341 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2345 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
2350 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2352 if ( rc != LDAP_SUCCESS ) {
2353 Debug( LDAP_DEBUG_ANY,
2354 "syncrepl_message_to_op: %s dn get failed (%d)",
2355 si->si_ridtxt, rc, 0 );
2359 op->o_tag = LBER_DEFAULT;
2360 op->o_bd = si->si_wbe;
2362 if ( BER_BVISEMPTY( &bdn )) {
2363 Debug( LDAP_DEBUG_ANY,
2364 "syncrepl_message_to_op: %s got empty dn",
2365 si->si_ridtxt, 0, 0 );
2369 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
2371 if ( bv.bv_val == NULL )
2374 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
2376 REWRITE_DN( si, bdn, bv2, dn, ndn );
2377 if ( rc != LDAP_SUCCESS ) {
2378 Debug( LDAP_DEBUG_ANY,
2379 "syncrepl_message_to_op: %s "
2380 "dn \"%s\" normalization failed (%d)",
2381 si->si_ridtxt, bdn.bv_val, rc );
2386 ber_dupbv( &op->o_req_dn, &dn );
2387 ber_dupbv( &op->o_req_ndn, &ndn );
2388 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2389 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2391 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
2392 int i = verb_to_mask( bvals[0].bv_val, modops );
2394 Debug( LDAP_DEBUG_ANY,
2395 "syncrepl_message_to_op: %s unknown op %s",
2396 si->si_ridtxt, bvals[0].bv_val, 0 );
2401 op->o_tag = modops[i].mask;
2402 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
2403 /* Parse attribute into modlist */
2404 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
2405 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
2407 rc = syncrepl_changelog_mods( si, bvals, &modlist );
2409 if ( rc ) goto done;
2410 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
2412 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
2413 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
2416 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
2418 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_controls ) ) {
2420 struct berval rel_ctrl_bv;
2422 (void)ber_str2bv( "{" LDAP_CONTROL_RELAX, 0, 0, &rel_ctrl_bv );
2423 for ( i = 0; bvals[i].bv_val; i++ ) {
2424 struct berval cbv, tmp;
2426 ber_bvchr_post( &cbv, &bvals[i], '}' );
2427 ber_bvchr_post( &tmp, &cbv, '{' );
2428 ber_bvchr_pre( &cbv, &tmp, ' ' );
2429 if ( cbv.bv_len == tmp.bv_len ) /* control w/o value */
2430 ber_bvchr_pre( &cbv, &tmp, '}' );
2431 if ( !ber_bvcmp( &cbv, &rel_ctrl_bv ) )
2432 op->o_relax = SLAP_CONTROL_CRITICAL;
2434 } else if ( !ber_bvstrcasecmp( &bv,
2435 &slap_schema.si_ad_entryCSN->ad_cname ) )
2437 slap_queue_csn( op, bvals );
2443 /* If we didn't get a mod type or a target DN, bail out */
2444 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
2449 op->o_callback = &cb;
2450 slap_op_time( &op->o_time, &op->o_tincr );
2452 Debug( LDAP_DEBUG_SYNC, "syncrepl_message_to_op: %s tid %x\n",
2453 si->si_ridtxt, op->o_tid, 0 );
2455 switch( op->o_tag ) {
2457 case LDAP_REQ_MODIFY:
2458 /* If we didn't get required data, bail */
2459 if ( !modlist ) goto done;
2461 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
2463 if ( rc != LDAP_SUCCESS ) {
2464 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2465 "mods check (%s)\n",
2466 si->si_ridtxt, text, 0 );
2470 if ( op->o_tag == LDAP_REQ_ADD ) {
2471 Entry *e = entry_alloc();
2473 op->ora_e->e_name = op->o_req_dn;
2474 op->ora_e->e_nname = op->o_req_ndn;
2476 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
2477 if( rc != LDAP_SUCCESS ) {
2478 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2479 "mods2entry (%s)\n",
2480 si->si_ridtxt, text, 0 );
2482 rc = op->o_bd->be_add( op, &rs );
2483 Debug( LDAP_DEBUG_SYNC,
2484 "syncrepl_message_to_op: %s be_add %s (%d)\n",
2485 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2488 if ( e == op->ora_e )
2489 be_entry_release_w( op, op->ora_e );
2492 op->orm_modlist = modlist;
2493 op->o_bd = si->si_wbe;
2494 /* delta-mmr needs additional checks in syncrepl_op_modify */
2495 if ( SLAP_MULTIMASTER( op->o_bd )) {
2496 oes.oe.oe_key = (void *)syncrepl_message_to_op;
2498 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &oes.oe, oe_next );
2500 rc = op->o_bd->be_modify( op, &rs );
2501 if ( SLAP_MULTIMASTER( op->o_bd )) {
2502 LDAP_SLIST_REMOVE( &op->o_extra, &oes.oe, OpExtra, oe_next );
2503 BER_BVZERO( &op->o_csn );
2505 modlist = op->orm_modlist;
2506 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2507 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
2508 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2509 op->o_bd = si->si_be;
2513 case LDAP_REQ_MODRDN:
2514 if ( BER_BVISNULL( &rdn ) ) goto done;
2516 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
2519 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
2522 if ( !BER_BVISNULL( &sup ) ) {
2523 REWRITE_DN( si, sup, bv2, psup, nsup );
2526 op->orr_newSup = &psup;
2527 op->orr_nnewSup = ⊅
2529 op->orr_newSup = NULL;
2530 op->orr_nnewSup = NULL;
2532 op->orr_newrdn = prdn;
2533 op->orr_nnewrdn = nrdn;
2534 op->orr_deleteoldrdn = deleteOldRdn;
2535 op->orr_modlist = NULL;
2536 if ( slap_modrdn2mods( op, &rs ) ) {
2540 /* Append modlist for operational attrs */
2544 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
2546 m->sml_next = modlist;
2549 rc = op->o_bd->be_modrdn( op, &rs );
2550 slap_mods_free( op->orr_modlist, 1 );
2551 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2552 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
2553 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2556 case LDAP_REQ_DELETE:
2557 rc = op->o_bd->be_delete( op, &rs );
2558 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2559 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
2560 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2566 slap_graduate_commit_csn( op );
2567 op->o_bd = si->si_be;
2568 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2569 BER_BVZERO( &op->o_csn );
2571 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
2573 if ( !BER_BVISNULL( &rdn ) ) {
2574 if ( !BER_BVISNULL( &nsup ) ) {
2575 ch_free( nsup.bv_val );
2577 if ( !BER_BVISNULL( &psup ) ) {
2578 ch_free( psup.bv_val );
2580 if ( !BER_BVISNULL( &nrdn ) ) {
2581 ch_free( nrdn.bv_val );
2583 if ( !BER_BVISNULL( &prdn ) ) {
2584 ch_free( prdn.bv_val );
2588 ch_free( op->o_req_ndn.bv_val );
2589 ch_free( op->o_req_dn.bv_val );
2596 syncrepl_message_to_entry(
2600 Modifications **modlist,
2603 struct berval *syncUUID
2607 BerElement *ber = NULL;
2610 Modifications **modtail = modlist;
2613 char txtbuf[SLAP_TEXT_BUFLEN];
2614 size_t textlen = sizeof txtbuf;
2616 struct berval bdn = BER_BVNULL, dn, ndn, bv2;
2621 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2622 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
2623 "Message type should be entry (%d)",
2624 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2628 op->o_tag = LDAP_REQ_ADD;
2630 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2631 if ( rc != LDAP_SUCCESS ) {
2632 Debug( LDAP_DEBUG_ANY,
2633 "syncrepl_message_to_entry: %s dn get failed (%d)",
2634 si->si_ridtxt, rc, 0 );
2638 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
2639 Debug( LDAP_DEBUG_ANY,
2640 "syncrepl_message_to_entry: %s got empty dn",
2641 si->si_ridtxt, 0, 0 );
2645 /* syncUUID[0] is normalized UUID received over the wire
2646 * syncUUID[1] is denormalized UUID, generated here
2648 (void)slap_uuidstr_from_normalized( &syncUUID[1], &syncUUID[0], op->o_tmpmemctx );
2649 Debug( LDAP_DEBUG_SYNC,
2650 "syncrepl_message_to_entry: %s DN: %s, UUID: %s\n",
2651 si->si_ridtxt, bdn.bv_val, syncUUID[1].bv_val );
2653 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
2654 /* NOTE: this could be done even before decoding the DN,
2655 * although encoding errors wouldn't be detected */
2660 if ( entry == NULL ) {
2664 REWRITE_DN( si, bdn, bv2, dn, ndn );
2665 if ( rc != LDAP_SUCCESS ) {
2666 /* One of the things that could happen is that the schema
2667 * is not lined-up; this could result in unknown attributes.
2668 * A value non conformant to the syntax should be unlikely,
2669 * except when replicating between different versions
2670 * of the software, or when syntax validation bugs are fixed
2672 Debug( LDAP_DEBUG_ANY,
2673 "syncrepl_message_to_entry: "
2674 "%s dn \"%s\" normalization failed (%d)",
2675 si->si_ridtxt, bdn.bv_val, rc );
2679 ber_dupbv( &op->o_req_dn, &dn );
2680 ber_dupbv( &op->o_req_ndn, &ndn );
2681 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2682 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2684 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
2687 e->e_name = op->o_req_dn;
2688 e->e_nname = op->o_req_ndn;
2690 while ( ber_remaining( ber ) ) {
2691 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
2692 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2697 /* Drop all updates to the contextCSN of the context entry
2700 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2701 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2702 ber_bvarray_free( tmp.sml_values );
2706 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2708 mod->sml_op = LDAP_MOD_REPLACE;
2710 mod->sml_next = NULL;
2711 mod->sml_desc = NULL;
2712 mod->sml_type = tmp.sml_type;
2713 mod->sml_values = tmp.sml_values;
2714 mod->sml_nvalues = NULL;
2715 mod->sml_numvals = 0; /* slap_mods_check will set this */
2717 #ifdef ENABLE_REWRITE
2718 if (si->si_rewrite) {
2719 AttributeDescription *ad = NULL;
2720 slap_bv2ad( &tmp.sml_type, &ad, &text );
2723 mod->sml_type = ad->ad_cname;
2724 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
2726 for ( i = 0; tmp.sml_values[i].bv_val; i++ ) {
2727 syncrepl_rewrite_dn( si, &tmp.sml_values[i], &bv2);
2728 if ( !BER_BVISNULL( &bv2 )) {
2729 ber_memfree( tmp.sml_values[i].bv_val );
2730 tmp.sml_values[i] = bv2;
2738 modtail = &mod->sml_next;
2741 if ( *modlist == NULL ) {
2742 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2743 si->si_ridtxt, 0, 0 );
2748 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2750 if ( rc != LDAP_SUCCESS ) {
2751 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2752 si->si_ridtxt, text, 0 );
2756 /* Strip out dynamically generated attrs */
2757 for ( modtail = modlist; *modtail ; ) {
2759 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2760 *modtail = mod->sml_next;
2761 slap_mod_free( &mod->sml_mod, 0 );
2764 modtail = &mod->sml_next;
2768 /* Strip out attrs in exattrs list */
2769 for ( modtail = modlist; *modtail ; ) {
2771 if ( ldap_charray_inlist( si->si_exattrs,
2772 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2774 *modtail = mod->sml_next;
2775 slap_mod_free( &mod->sml_mod, 0 );
2778 modtail = &mod->sml_next;
2782 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2783 if( rc != LDAP_SUCCESS ) {
2784 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2785 si->si_ridtxt, text, 0 );
2790 if ( rc != LDAP_SUCCESS ) {
2802 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2804 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2805 * entry if a previous refresh was interrupted before sending us a new
2806 * context state. We try to compare the new entry to the existing entry
2807 * and ignore the new entry if they are the same.
2809 * Also, we may get an update where the entryDN has changed, due to
2810 * a ModDn on the provider. We detect this as well, so we can issue
2811 * the corresponding operation locally.
2813 * In the case of a modify, we get a list of all the attributes
2814 * in the original entry. Rather than deleting the entry and re-adding it,
2815 * we issue a Modify request that deletes all the attributes and adds all
2816 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2819 * We otherwise distinguish ModDN from Modify; in the case of
2820 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2821 * operational attributes from the entry, and do a regular ModDN.
2823 typedef struct dninfo {
2827 struct berval nnewSup;
2828 int renamed; /* Was an existing entry renamed? */
2829 int delOldRDN; /* Was old RDN deleted? */
2830 Modifications **modlist; /* the modlist we received */
2831 Modifications *mods; /* the modlist we compared */
2832 int oldNcount; /* #values of old naming attr */
2833 AttributeDescription *oldDesc; /* for renames */
2834 AttributeDescription *newDesc; /* for renames */
2839 /* return 1 if inserted, 0 otherwise */
2843 struct berval *syncUUID )
2851 if ( !si->si_presentlist )
2852 si->si_presentlist = ch_calloc(65536, sizeof( Avlnode * ));
2854 av = (Avlnode **)si->si_presentlist;
2856 val = ch_malloc(UUIDLEN-2);
2857 memcpy(&s, syncUUID->bv_val, 2);
2858 memcpy(val, syncUUID->bv_val+2, UUIDLEN-2);
2860 if ( avl_insert( &av[s], val,
2861 syncuuid_cmp, avl_dup_error ) )
2867 val = ch_malloc(UUIDLEN);
2869 AC_MEMCPY( val, syncUUID->bv_val, UUIDLEN );
2871 if ( avl_insert( &si->si_presentlist, val,
2872 syncuuid_cmp, avl_dup_error ) )
2885 struct berval *val )
2888 Avlnode **a2 = (Avlnode **)av;
2894 memcpy(&s, val->bv_val, 2);
2895 return avl_find( a2[s], val->bv_val+2, syncuuid_cmp );
2897 return avl_find( av, val->bv_val, syncuuid_cmp );
2902 presentlist_free( Avlnode *av )
2905 Avlnode **a2 = (Avlnode **)av;
2909 for (i=0; i<65536; i++) {
2911 count += avl_free( a2[i], ch_free );
2917 return avl_free( av, ch_free );
2924 struct berval *val )
2927 Avlnode **a2 = *(Avlnode ***)av;
2930 memcpy(&s, val->bv_val, 2);
2931 avl_delete( &a2[s], val->bv_val+2, syncuuid_cmp );
2933 avl_delete( av, val->bv_val, syncuuid_cmp );
2942 Modifications** modlist,
2944 struct berval* syncUUID,
2945 struct berval* syncCSN )
2947 Backend *be = op->o_bd;
2948 slap_callback cb = { NULL, NULL, NULL, NULL };
2949 int syncuuid_inserted = 0;
2951 SlapReply rs_search = {REP_RESULT};
2953 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2954 int rc = LDAP_SUCCESS;
2956 struct berval pdn = BER_BVNULL;
2961 Debug( LDAP_DEBUG_SYNC,
2962 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s) tid %x\n",
2963 si->si_ridtxt, syncrepl_state2str( syncstate ), op->o_tid );
2965 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2966 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2967 syncuuid_inserted = presentlist_insert( si, syncUUID );
2971 if ( syncstate == LDAP_SYNC_PRESENT ) {
2973 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2974 if ( entry == NULL ) {
2979 if ( syncstate != LDAP_SYNC_DELETE ) {
2980 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2983 /* add if missing */
2984 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2985 &syncUUID[1], syncUUID );
2987 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2988 /* replace only if necessary */
2989 if ( a->a_nvals != a->a_vals ) {
2990 ber_memfree( a->a_nvals[0].bv_val );
2991 ber_dupbv( &a->a_nvals[0], syncUUID );
2993 ber_memfree( a->a_vals[0].bv_val );
2994 ber_dupbv( &a->a_vals[0], &syncUUID[1] );
2998 f.f_choice = LDAP_FILTER_EQUALITY;
3000 ava.aa_desc = slap_schema.si_ad_entryUUID;
3001 ava.aa_value = *syncUUID;
3003 if ( syncuuid_inserted ) {
3004 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
3005 si->si_ridtxt, syncUUID[1].bv_val, 0 );
3007 op->ors_filter = &f;
3009 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID[1].bv_len;
3010 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
3011 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
3012 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
3013 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
3014 syncUUID[1].bv_val, syncUUID[1].bv_len );
3015 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
3016 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
3018 op->o_tag = LDAP_REQ_SEARCH;
3019 op->ors_scope = LDAP_SCOPE_SUBTREE;
3020 op->ors_deref = LDAP_DEREF_NEVER;
3022 /* get the entry for this UUID */
3023 #ifdef ENABLE_REWRITE
3024 if ( si->si_rewrite ) {
3025 op->o_req_dn = si->si_suffixm;
3026 op->o_req_ndn = si->si_suffixm;
3030 op->o_req_dn = si->si_base;
3031 op->o_req_ndn = si->si_base;
3034 op->o_time = slap_get_time();
3035 op->ors_tlimit = SLAP_NO_LIMIT;
3037 op->ors_limit = NULL;
3039 op->ors_attrs = slap_anlist_all_attributes;
3040 op->ors_attrsonly = 0;
3042 /* set callback function */
3043 op->o_callback = &cb;
3044 cb.sc_response = dn_callback;
3045 cb.sc_private = &dni;
3046 dni.new_entry = entry;
3047 dni.modlist = modlist;
3049 rc = be->be_search( op, &rs_search );
3050 Debug( LDAP_DEBUG_SYNC,
3051 "syncrepl_entry: %s be_search (%d)\n",
3052 si->si_ridtxt, rc, 0 );
3054 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
3055 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3058 cb.sc_response = syncrepl_null_callback;
3061 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
3062 Debug( LDAP_DEBUG_SYNC,
3063 "syncrepl_entry: %s %s\n",
3064 si->si_ridtxt, entry->e_name.bv_val, 0 );
3066 Debug( LDAP_DEBUG_SYNC,
3067 "syncrepl_entry: %s %s\n",
3068 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
3071 assert( BER_BVISNULL( &op->o_csn ) );
3073 slap_queue_csn( op, syncCSN );
3076 if ( !si->si_refreshDone ) {
3077 if ( si->si_lazyCommit )
3078 op->o_lazyCommit = SLAP_CONTROL_NONCRITICAL;
3079 if ( si->si_refreshCount == 500 ) {
3080 LDAP_SLIST_REMOVE( &op->o_extra, si->si_refreshTxn, OpExtra, oe_next );
3081 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_COMMIT, &si->si_refreshTxn );
3082 si->si_refreshCount = 0;
3083 si->si_refreshTxn = NULL;
3085 if ( op->o_bd->bd_info->bi_op_txn ) {
3086 if ( !si->si_refreshCount ) {
3087 op->o_bd->bd_info->bi_op_txn( op, SLAP_TXN_BEGIN, &si->si_refreshTxn );
3089 si->si_refreshCount++;
3093 slap_op_time( &op->o_time, &op->o_tincr );
3094 switch ( syncstate ) {
3096 case LDAP_SYNC_MODIFY:
3097 if ( BER_BVISNULL( &op->o_csn ))
3100 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
3102 /* FIXME: op->o_csn is assumed to be
3103 * on the thread's slab; this needs
3104 * to be cleared ASAP.
3106 op->o_csn = a->a_vals[0];
3111 if ( BER_BVISNULL( &dni.dn ) ) {
3112 SlapReply rs_add = {REP_RESULT};
3114 op->o_req_dn = entry->e_name;
3115 op->o_req_ndn = entry->e_nname;
3116 op->o_tag = LDAP_REQ_ADD;
3118 op->o_bd = si->si_wbe;
3120 rc = op->o_bd->be_add( op, &rs_add );
3121 Debug( LDAP_DEBUG_SYNC,
3122 "syncrepl_entry: %s be_add %s (%d)\n",
3123 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3124 switch ( rs_add.sr_err ) {
3126 if ( op->ora_e == entry ) {
3127 be_entry_release_w( op, entry );
3133 /* we assume that LDAP_NO_SUCH_OBJECT is returned
3134 * only if the suffix entry is not present.
3135 * This should not happen during Persist phase.
3137 case LDAP_NO_SUCH_OBJECT:
3138 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
3139 si->si_refreshDone ) {
3140 /* Something's wrong, start over */
3141 ber_bvarray_free( si->si_syncCookie.ctxcsn );
3142 si->si_syncCookie.ctxcsn = NULL;
3143 entry_free( entry );
3144 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3145 ber_bvarray_free( si->si_cookieState->cs_vals );
3146 ch_free( si->si_cookieState->cs_sids );
3147 si->si_cookieState->cs_vals = NULL;
3148 si->si_cookieState->cs_sids = 0;
3149 si->si_cookieState->cs_num = 0;
3150 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3151 return LDAP_NO_SUCH_OBJECT;
3153 rc = syncrepl_add_glue( op, entry );
3157 /* if an entry was added via syncrepl_add_glue(),
3158 * it likely has no entryUUID, so the previous
3159 * be_search() doesn't find it. In this case,
3160 * give syncrepl a chance to modify it. Also
3161 * allow for entries that were recreated with the
3162 * same DN but a different entryUUID.
3164 case LDAP_ALREADY_EXISTS:
3166 Operation op2 = *op;
3167 SlapReply rs2 = { REP_RESULT };
3168 slap_callback cb2 = { 0 };
3171 op2.o_tag = LDAP_REQ_SEARCH;
3172 op2.o_req_dn = entry->e_name;
3173 op2.o_req_ndn = entry->e_nname;
3174 op2.ors_scope = LDAP_SCOPE_BASE;
3175 op2.ors_deref = LDAP_DEREF_NEVER;
3176 op2.ors_attrs = slap_anlist_all_attributes;
3177 op2.ors_attrsonly = 0;
3178 op2.ors_limit = NULL;
3180 op2.ors_tlimit = SLAP_NO_LIMIT;
3182 f.f_choice = LDAP_FILTER_PRESENT;
3183 f.f_desc = slap_schema.si_ad_objectClass;
3184 op2.ors_filter = &f;
3185 op2.ors_filterstr = generic_filterstr;
3187 op2.o_callback = &cb2;
3188 cb2.sc_response = dn_callback;
3189 cb2.sc_private = &dni;
3191 rc = be->be_search( &op2, &rs2 );
3192 if ( rc ) goto done;
3195 slap_op_time( &op->o_time, &op->o_tincr );
3201 Debug( LDAP_DEBUG_ANY,
3202 "syncrepl_entry: %s be_add %s failed (%d)\n",
3203 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
3211 op->o_req_dn = dni.dn;
3212 op->o_req_ndn = dni.ndn;
3213 if ( dni.renamed ) {
3214 struct berval noldp, newp;
3215 Modifications *mod, **modtail, **ml, *m2;
3216 int i, got_replace = 0, just_rename = 0;
3217 SlapReply rs_modify = {REP_RESULT};
3219 op->o_tag = LDAP_REQ_MODRDN;
3220 dnRdn( &entry->e_name, &op->orr_newrdn );
3221 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
3223 if ( !BER_BVISNULL( &dni.nnewSup )) {
3224 dnParent( &entry->e_name, &newp );
3225 op->orr_newSup = &newp;
3226 op->orr_nnewSup = &dni.nnewSup;
3228 op->orr_newSup = NULL;
3229 op->orr_nnewSup = NULL;
3231 op->orr_deleteoldrdn = dni.delOldRDN;
3232 op->orr_modlist = NULL;
3233 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
3237 /* Drop the RDN-related mods from this op, because their
3238 * equivalents were just setup by slap_modrdn2mods.
3240 * If delOldRDN is TRUE then we should see a delete modop
3241 * for oldDesc. We might see a replace instead.
3242 * delete with no values: therefore newDesc != oldDesc.
3243 * if oldNcount == 1, then Drop this op.
3244 * delete with 1 value: can only be the oldRDN value. Drop op.
3245 * delete with N values: Drop oldRDN value, keep remainder.
3246 * replace with 1 value: if oldNcount == 1 and
3247 * newDesc == oldDesc, Drop this op.
3248 * Any other cases must be left intact.
3250 * We should also see an add modop for newDesc. (But not if
3251 * we got a replace modop due to delOldRDN.) If it has
3252 * multiple values, we'll have to drop the new RDN value.
3254 modtail = &op->orr_modlist;
3255 if ( dni.delOldRDN ) {
3256 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3257 if ( (*ml)->sml_desc == dni.oldDesc ) {
3259 if ( mod->sml_op == LDAP_MOD_REPLACE &&
3260 dni.oldDesc != dni.newDesc ) {
3261 /* This Replace is due to other Mods.
3266 if ( mod->sml_numvals <= 1 &&
3267 dni.oldNcount == 1 &&
3268 ( mod->sml_op == LDAP_MOD_DELETE ||
3269 mod->sml_op == LDAP_MOD_REPLACE )) {
3270 if ( mod->sml_op == LDAP_MOD_REPLACE )
3273 *ml = mod->sml_next;
3274 mod->sml_next = NULL;
3275 slap_mods_free( mod, 1 );
3278 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
3280 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3281 if ( m2->sml_desc == dni.oldDesc &&
3282 m2->sml_op == LDAP_MOD_DELETE ) break;
3284 for ( i=0; i<mod->sml_numvals; i++ ) {
3285 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3287 ch_free( mod->sml_values[i].bv_val );
3288 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3289 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3290 if ( mod->sml_nvalues ) {
3291 ch_free( mod->sml_nvalues[i].bv_val );
3292 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3293 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3298 if ( !mod->sml_numvals ) {
3300 *ml = mod->sml_next;
3301 mod->sml_next = NULL;
3302 slap_mods_free( mod, 1 );
3308 if ( !got_replace ) {
3309 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3310 if ( (*ml)->sml_desc == dni.newDesc ) {
3312 if ( mod->sml_op != LDAP_MOD_ADD )
3314 if ( mod->sml_numvals == 1 ) {
3316 *ml = mod->sml_next;
3317 mod->sml_next = NULL;
3318 slap_mods_free( mod, 1 );
3321 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3322 if ( m2->sml_desc == dni.oldDesc &&
3323 m2->sml_op == SLAP_MOD_SOFTADD ) break;
3325 for ( i=0; i<mod->sml_numvals; i++ ) {
3326 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3328 ch_free( mod->sml_values[i].bv_val );
3329 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3330 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3331 if ( mod->sml_nvalues ) {
3332 ch_free( mod->sml_nvalues[i].bv_val );
3333 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3334 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3344 /* RDNs must be NUL-terminated for back-ldap */
3345 noldp = op->orr_newrdn;
3346 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
3347 noldp = op->orr_nnewrdn;
3348 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
3350 /* Setup opattrs too */
3352 static AttributeDescription *nullattr = NULL;
3353 static AttributeDescription **const opattrs[] = {
3354 &slap_schema.si_ad_entryCSN,
3355 &slap_schema.si_ad_modifiersName,
3356 &slap_schema.si_ad_modifyTimestamp,
3359 AttributeDescription *opattr;
3363 /* pull mod off incoming modlist */
3364 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
3365 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
3367 if ( (*ml)->sml_desc == opattr ) {
3369 *ml = mod->sml_next;
3370 mod->sml_next = NULL;
3372 modtail = &mod->sml_next;
3377 /* If there are still Modifications left, put the opattrs
3378 * back, and let be_modify run. Otherwise, append the opattrs
3379 * to the orr_modlist.
3383 /* don't set a CSN for the rename op */
3385 slap_graduate_commit_csn( op );
3387 mod = op->orr_modlist;
3390 for ( ; mod->sml_next; mod=mod->sml_next );
3393 op->o_bd = si->si_wbe;
3395 rs_reinit( &rs_modify, REP_RESULT );
3396 rc = op->o_bd->be_modrdn( op, &rs_modify );
3398 /* NOTE: noSuchObject should result because the new superior
3399 * has not been added yet (ITS#6472) */
3400 if ( rc == LDAP_NO_SUCH_OBJECT && op->orr_nnewSup != NULL ) {
3401 Operation op2 = *op;
3402 rc = syncrepl_add_glue_ancestors( &op2, entry );
3403 if ( rc == LDAP_SUCCESS ) {
3408 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
3409 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
3411 slap_mods_free( op->orr_modlist, 1 );
3412 Debug( LDAP_DEBUG_SYNC,
3413 "syncrepl_entry: %s be_modrdn %s (%d)\n",
3414 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3416 /* Renamed entries may still have other mods so just fallthru */
3417 op->o_req_dn = entry->e_name;
3418 op->o_req_ndn = entry->e_nname;
3419 /* Use CSN on the modify */
3423 slap_queue_csn( op, syncCSN );
3426 SlapReply rs_modify = {REP_RESULT};
3428 op->o_tag = LDAP_REQ_MODIFY;
3429 op->orm_modlist = dni.mods;
3430 op->orm_no_opattrs = 1;
3431 op->o_bd = si->si_wbe;
3433 rc = op->o_bd->be_modify( op, &rs_modify );
3434 slap_mods_free( op->orm_modlist, 1 );
3435 op->orm_no_opattrs = 0;
3436 Debug( LDAP_DEBUG_SYNC,
3437 "syncrepl_entry: %s be_modify %s (%d)\n",
3438 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3439 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
3440 Debug( LDAP_DEBUG_ANY,
3441 "syncrepl_entry: %s be_modify failed (%d)\n",
3442 si->si_ridtxt, rs_modify.sr_err, 0 );
3446 } else if ( !dni.renamed ) {
3447 Debug( LDAP_DEBUG_SYNC,
3448 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
3449 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
3451 slap_graduate_commit_csn( op );
3456 case LDAP_SYNC_DELETE :
3457 if ( !BER_BVISNULL( &dni.dn ) ) {
3458 SlapReply rs_delete = {REP_RESULT};
3459 op->o_req_dn = dni.dn;
3460 op->o_req_ndn = dni.ndn;
3461 op->o_tag = LDAP_REQ_DELETE;
3462 op->o_bd = si->si_wbe;
3464 slap_queue_csn( op, si->si_syncCookie.ctxcsn );
3466 rc = op->o_bd->be_delete( op, &rs_delete );
3467 Debug( LDAP_DEBUG_SYNC,
3468 "syncrepl_entry: %s be_delete %s (%d)\n",
3469 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3470 if ( rc == LDAP_NO_SUCH_OBJECT )
3473 while ( rs_delete.sr_err == LDAP_SUCCESS
3474 && op->o_delete_glue_parent ) {
3475 op->o_delete_glue_parent = 0;
3476 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3477 slap_callback cb = { NULL };
3478 cb.sc_response = syncrepl_null_callback;
3479 dnParent( &op->o_req_ndn, &pdn );
3481 op->o_req_ndn = pdn;
3482 op->o_callback = &cb;
3483 rs_reinit( &rs_delete, REP_RESULT );
3484 op->o_bd->be_delete( op, &rs_delete );
3495 Debug( LDAP_DEBUG_ANY,
3496 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
3501 slap_sl_free( syncUUID[1].bv_val, op->o_tmpmemctx );
3502 BER_BVZERO( &syncUUID[1] );
3503 if ( !BER_BVISNULL( &dni.ndn ) ) {
3504 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
3506 if ( !BER_BVISNULL( &dni.dn ) ) {
3507 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
3510 entry_free( entry );
3513 slap_graduate_commit_csn( op );
3515 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
3516 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3518 BER_BVZERO( &op->o_csn );
3522 static struct berval gcbva[] = {
3528 #define NP_DELETE_ONE 2
3531 syncrepl_del_nonpresent(
3535 struct sync_cookie *sc,
3538 Backend* be = op->o_bd;
3539 slap_callback cb = { NULL };
3540 struct nonpresent_entry *np_list, *np_prev;
3542 AttributeName an[2];
3544 struct berval pdn = BER_BVNULL;
3547 #ifdef ENABLE_REWRITE
3548 if ( si->si_rewrite ) {
3549 op->o_req_dn = si->si_suffixm;
3550 op->o_req_ndn = si->si_suffixm;
3554 op->o_req_dn = si->si_base;
3555 op->o_req_ndn = si->si_base;
3558 cb.sc_response = nonpresent_callback;
3561 op->o_callback = &cb;
3562 op->o_tag = LDAP_REQ_SEARCH;
3563 op->ors_scope = si->si_scope;
3564 op->ors_deref = LDAP_DEREF_NEVER;
3565 op->o_time = slap_get_time();
3566 op->ors_tlimit = SLAP_NO_LIMIT;
3571 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
3574 op->ors_attrsonly = 1;
3575 op->ors_attrs = slap_anlist_no_attrs;
3576 op->ors_limit = NULL;
3577 op->ors_filter = &uf;
3580 uf.f_av_desc = slap_schema.si_ad_entryUUID;
3582 uf.f_choice = LDAP_FILTER_EQUALITY;
3583 si->si_refreshDelete |= NP_DELETE_ONE;
3585 for (i=0; uuids[i].bv_val; i++) {
3586 SlapReply rs_search = {REP_RESULT};
3589 uf.f_av_value = uuids[i];
3590 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3591 rc = be->be_search( op, &rs_search );
3592 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3594 si->si_refreshDelete ^= NP_DELETE_ONE;
3598 AttributeAssertion mmaa;
3599 SlapReply rs_search = {REP_RESULT};
3601 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
3602 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
3603 an[0].an_desc = slap_schema.si_ad_entryUUID;
3605 op->ors_slimit = SLAP_NO_LIMIT;
3606 op->ors_tlimit = SLAP_NO_LIMIT;
3607 op->ors_limit = NULL;
3608 op->ors_attrsonly = 0;
3609 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
3610 /* In multimaster, updates can continue to arrive while
3611 * we're searching. Limit the search result to entries
3612 * older than our newest cookie CSN.
3614 if ( SLAP_MULTIMASTER( op->o_bd )) {
3619 f->f_choice = LDAP_FILTER_AND;
3620 f->f_next = op->ors_filter;
3624 f->f_choice = LDAP_FILTER_LE;
3626 f->f_av_desc = slap_schema.si_ad_entryCSN;
3628 BER_BVZERO( &f->f_av_value );
3629 for ( i=0; i<sc->numcsns; i++ ) {
3630 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
3631 f->f_av_value = sc->ctxcsn[i];
3633 of = op->ors_filter;
3634 op->ors_filter = mmf;
3635 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3638 op->ors_filterstr = si->si_filterstr;
3640 op->o_nocaching = 1;
3643 rc = be->be_search( op, &rs_search );
3644 if ( SLAP_MULTIMASTER( op->o_bd )) {
3645 op->ors_filter = of;
3647 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
3648 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
3649 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3654 op->o_nocaching = 0;
3656 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
3658 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
3659 csn = sc->ctxcsn[m];
3661 csn = si->si_syncCookie.ctxcsn[0];
3664 op->o_bd = si->si_wbe;
3665 slap_queue_csn( op, &csn );
3667 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
3668 while ( np_list != NULL ) {
3669 SlapReply rs_delete = {REP_RESULT};
3671 LDAP_LIST_REMOVE( np_list, npe_link );
3673 np_list = LDAP_LIST_NEXT( np_list, npe_link );
3674 op->o_tag = LDAP_REQ_DELETE;
3675 op->o_callback = &cb;
3676 cb.sc_response = syncrepl_null_callback;
3678 op->o_req_dn = *np_prev->npe_name;
3679 op->o_req_ndn = *np_prev->npe_nname;
3680 rc = op->o_bd->be_delete( op, &rs_delete );
3681 Debug( LDAP_DEBUG_SYNC,
3682 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
3683 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3685 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
3686 SlapReply rs_modify = {REP_RESULT};
3687 Modifications mod1, mod2;
3688 mod1.sml_op = LDAP_MOD_REPLACE;
3690 mod1.sml_desc = slap_schema.si_ad_objectClass;
3691 mod1.sml_type = mod1.sml_desc->ad_cname;
3692 mod1.sml_numvals = 2;
3693 mod1.sml_values = &gcbva[0];
3694 mod1.sml_nvalues = NULL;
3695 mod1.sml_next = &mod2;
3697 mod2.sml_op = LDAP_MOD_REPLACE;
3699 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
3700 mod2.sml_type = mod2.sml_desc->ad_cname;
3701 mod2.sml_numvals = 1;
3702 mod2.sml_values = &gcbva[1];
3703 mod2.sml_nvalues = NULL;
3704 mod2.sml_next = NULL;
3706 op->o_tag = LDAP_REQ_MODIFY;
3707 op->orm_modlist = &mod1;
3709 rc = op->o_bd->be_modify( op, &rs_modify );
3710 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
3713 while ( rs_delete.sr_err == LDAP_SUCCESS &&
3714 op->o_delete_glue_parent ) {
3715 op->o_delete_glue_parent = 0;
3716 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3717 slap_callback cb = { NULL };
3718 cb.sc_response = syncrepl_null_callback;
3719 dnParent( &op->o_req_ndn, &pdn );
3721 op->o_req_ndn = pdn;
3722 op->o_callback = &cb;
3723 rs_reinit( &rs_delete, REP_RESULT );
3724 /* give it a root privil ? */
3725 op->o_bd->be_delete( op, &rs_delete );
3731 op->o_delete_glue_parent = 0;
3733 ber_bvfree( np_prev->npe_name );
3734 ber_bvfree( np_prev->npe_nname );
3737 if ( slapd_shutdown ) {
3742 slap_graduate_commit_csn( op );
3745 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3746 BER_BVZERO( &op->o_csn );
3753 syncrepl_add_glue_ancestors(
3757 Backend *be = op->o_bd;
3758 slap_callback cb = { NULL };
3760 int rc = LDAP_SUCCESS;
3763 struct berval dn = BER_BVNULL;
3764 struct berval ndn = BER_BVNULL;
3766 struct berval ptr, nptr;
3769 op->o_tag = LDAP_REQ_ADD;
3770 op->o_callback = &cb;
3771 cb.sc_response = syncrepl_null_callback;
3772 cb.sc_private = NULL;
3777 /* count RDNs in suffix */
3778 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3779 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3781 ptr.bv_len -= comma - ptr.bv_val;
3791 /* Start with BE suffix */
3793 for ( i = 0; i < suffrdns; i++ ) {
3794 comma = ber_bvrchr( &ptr, ',' );
3795 if ( comma != NULL ) {
3796 ptr.bv_len = comma - ptr.bv_val;
3803 if ( !BER_BVISEMPTY( &ptr ) ) {
3804 dn.bv_len -= ptr.bv_len + ( suffrdns != 0 );
3805 dn.bv_val += ptr.bv_len + ( suffrdns != 0 );
3808 /* the normalizedDNs are always the same length, no counting
3812 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3813 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3814 ndn.bv_len = be->be_nsuffix[0].bv_len;
3816 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3822 while ( ndn.bv_val > e->e_nname.bv_val ) {
3823 SlapReply rs_add = {REP_RESULT};
3825 glue = entry_alloc();
3826 ber_dupbv( &glue->e_name, &dn );
3827 ber_dupbv( &glue->e_nname, &ndn );
3829 a = attr_alloc( slap_schema.si_ad_objectClass );
3832 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3833 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3834 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3835 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3837 a->a_nvals = a->a_vals;
3839 a->a_next = glue->e_attrs;
3842 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3845 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3846 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3847 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3849 a->a_nvals = a->a_vals;
3851 a->a_next = glue->e_attrs;
3854 op->o_req_dn = glue->e_name;
3855 op->o_req_ndn = glue->e_nname;
3857 rc = be->be_add ( op, &rs_add );
3858 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3859 if ( op->ora_e == glue )
3860 be_entry_release_w( op, glue );
3862 /* incl. ALREADY EXIST */
3864 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3870 /* Move to next child */
3871 comma = ber_bvrchr( &ptr, ',' );
3872 if ( comma == NULL ) {
3875 ptr.bv_len = comma - ptr.bv_val;
3877 dn.bv_val = ++comma;
3878 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3880 comma = ber_bvrchr( &nptr, ',' );
3881 assert( comma != NULL );
3882 nptr.bv_len = comma - nptr.bv_val;
3884 ndn.bv_val = ++comma;
3885 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3896 slap_callback cb = { NULL };
3898 Backend *be = op->o_bd;
3899 SlapReply rs_add = {REP_RESULT};
3901 rc = syncrepl_add_glue_ancestors( op, e );
3904 case LDAP_ALREADY_EXISTS:
3911 op->o_tag = LDAP_REQ_ADD;
3912 op->o_callback = &cb;
3913 cb.sc_response = syncrepl_null_callback;
3914 cb.sc_private = NULL;
3916 op->o_req_dn = e->e_name;
3917 op->o_req_ndn = e->e_nname;
3919 rc = be->be_add ( op, &rs_add );
3920 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3921 if ( op->ora_e == e )
3922 be_entry_release_w( op, e );
3931 syncrepl_updateCookie(
3934 struct sync_cookie *syncCookie,
3937 Backend *be = op->o_bd;
3939 struct berval first = BER_BVNULL;
3940 struct sync_cookie sc;
3942 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3945 int rc, i, j, changed = 0;
3948 slap_callback cb = { NULL };
3949 SlapReply rs_modify = {REP_RESULT};
3951 mod.sml_op = LDAP_MOD_REPLACE;
3952 mod.sml_desc = slap_schema.si_ad_contextCSN;
3953 mod.sml_type = mod.sml_desc->ad_cname;
3954 mod.sml_flags = SLAP_MOD_INTERNAL;
3955 mod.sml_nvalues = NULL;
3956 mod.sml_next = NULL;
3958 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3959 while ( si->si_cookieState->cs_updating )
3960 ldap_pvt_thread_cond_wait( &si->si_cookieState->cs_cond, &si->si_cookieState->cs_mutex );
3963 for ( i=0; i<syncCookie->numcsns; i++ ) {
3964 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3966 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3967 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3971 /* clone the cookieState CSNs so we can Replace the whole thing */
3972 sc.numcsns = si->si_cookieState->cs_num;
3974 ber_bvarray_dup_x( &sc.ctxcsn, si->si_cookieState->cs_vals, NULL );
3975 sc.sids = ch_malloc( sc.numcsns * sizeof(int));
3976 for ( i=0; i<sc.numcsns; i++ )
3977 sc.sids[i] = si->si_cookieState->cs_sids[i];
3983 /* find any CSNs in the syncCookie that are newer than the cookieState */
3984 for ( i=0; i<syncCookie->numcsns; i++ ) {
3985 for ( j=0; j<sc.numcsns; j++ ) {
3986 if ( syncCookie->sids[i] < sc.sids[j] )
3988 if ( syncCookie->sids[i] != sc.sids[j] )
3990 len = syncCookie->ctxcsn[i].bv_len;
3991 if ( len > sc.ctxcsn[j].bv_len )
3992 len = sc.ctxcsn[j].bv_len;
3993 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3994 sc.ctxcsn[j].bv_val, len ) > 0 ) {
3995 ber_bvreplace( &sc.ctxcsn[j], &syncCookie->ctxcsn[i] );
3997 if ( BER_BVISNULL( &first ) ||
3998 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3999 first = syncCookie->ctxcsn[i];
4004 /* there was no match for this SID, it's a new CSN */
4005 if ( j == sc.numcsns ||
4006 syncCookie->sids[i] != sc.sids[j] ) {
4007 slap_insert_csn_sids( &sc, j, syncCookie->sids[i],
4008 &syncCookie->ctxcsn[i] );
4009 if ( BER_BVISNULL( &first ) ||
4010 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
4011 first = syncCookie->ctxcsn[i];
4016 /* Should never happen, ITS#5065 */
4017 if ( BER_BVISNULL( &first ) || !changed ) {
4018 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
4019 ber_bvarray_free( sc.ctxcsn );
4024 si->si_cookieState->cs_updating = 1;
4025 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
4027 op->o_bd = si->si_wbe;
4028 slap_queue_csn( op, &first );
4030 op->o_tag = LDAP_REQ_MODIFY;
4032 cb.sc_response = syncrepl_null_callback;
4035 op->o_callback = &cb;
4036 op->o_req_dn = si->si_contextdn;
4037 op->o_req_ndn = si->si_contextdn;
4039 /* update contextCSN */
4040 op->o_dont_replicate = !save;
4042 /* avoid timestamp collisions */
4044 slap_op_time( &op->o_time, &op->o_tincr );
4046 mod.sml_numvals = sc.numcsns;
4047 mod.sml_values = sc.ctxcsn;
4049 op->orm_modlist = &mod;
4050 op->orm_no_opattrs = 1;
4051 rc = op->o_bd->be_modify( op, &rs_modify );
4053 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
4054 SLAP_SYNC_SUBENTRY( op->o_bd )) {
4056 char txtbuf[SLAP_TEXT_BUFLEN];
4057 size_t textlen = sizeof txtbuf;
4058 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
4059 rs_reinit( &rs_modify, REP_RESULT );
4060 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
4061 slap_queue_csn( op, &first );
4062 op->o_tag = LDAP_REQ_ADD;
4064 rc = op->o_bd->be_add( op, &rs_modify );
4065 if ( e == op->ora_e )
4066 be_entry_release_w( op, op->ora_e );
4069 op->orm_no_opattrs = 0;
4070 op->o_dont_replicate = 0;
4071 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
4073 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
4074 slap_sync_cookie_free( &si->si_syncCookie, 0 );
4075 ber_bvarray_free( si->si_cookieState->cs_vals );
4076 ch_free( si->si_cookieState->cs_sids );
4077 si->si_cookieState->cs_vals = sc.ctxcsn;
4078 si->si_cookieState->cs_sids = sc.sids;
4079 si->si_cookieState->cs_num = sc.numcsns;
4081 /* Don't just dup the provider's cookie, recreate it */
4082 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
4083 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn, si->si_cookieState->cs_vals, NULL );
4084 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num * sizeof(int) );
4085 for ( i=0; i<si->si_cookieState->cs_num; i++ )
4086 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
4088 si->si_cookieState->cs_age++;
4089 si->si_cookieAge = si->si_cookieState->cs_age;
4091 Debug( LDAP_DEBUG_ANY,
4092 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
4093 si->si_ridtxt, rs_modify.sr_err, 0 );
4095 ber_bvarray_free( sc.ctxcsn );
4099 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
4100 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
4104 si->si_cookieState->cs_updating = 0;
4105 ldap_pvt_thread_cond_broadcast( &si->si_cookieState->cs_cond );
4106 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
4109 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
4110 BER_BVZERO( &op->o_csn );
4111 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
4116 /* Compare the attribute from the old entry to the one in the new
4117 * entry. The Modifications from the new entry will either be left
4118 * in place, or changed to an Add or Delete as needed.
4121 attr_cmp( Operation *op, Attribute *old, Attribute *new,
4122 Modifications ***mret, Modifications ***mcur )
4125 Modifications *mod, **modtail;
4131 struct berval **adds, **dels;
4132 /* count old and new */
4133 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
4134 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
4136 /* there MUST be both old and new values */
4141 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
4142 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
4144 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
4145 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
4149 for ( i=0; i<o; i++ ) {
4150 for ( j=0; j<n; j++ ) {
4153 if ( bvmatch( dels[i], adds[j] ) ) {
4163 /* Don't delete/add an objectClass, always use the replace op.
4164 * Modify would fail if provider has replaced entry with a new,
4165 * and the new explicitly includes a superior of a class that was
4166 * only included implicitly in the old entry. Ref ITS#5517.
4168 * Also use replace op if attr has no equality matching rule.
4171 if ( ( nn || ( no > 0 && no < o ) ) &&
4172 ( old->a_desc == slap_schema.si_ad_objectClass ||
4173 !old->a_desc->ad_type->sat_equality ) )
4179 /* all old values were deleted, just use the replace op */
4183 /* delete some values */
4184 mod = ch_malloc( sizeof( Modifications ) );
4185 mod->sml_op = LDAP_MOD_DELETE;
4187 mod->sml_desc = old->a_desc;
4188 mod->sml_type = mod->sml_desc->ad_cname;
4189 mod->sml_numvals = no;
4190 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4191 if ( old->a_vals != old->a_nvals ) {
4192 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
4194 mod->sml_nvalues = NULL;
4197 for ( i = 0; i < o; i++ ) {
4198 if ( !dels[i] ) continue;
4199 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
4200 if ( mod->sml_nvalues ) {
4201 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
4205 BER_BVZERO( &mod->sml_values[j] );
4206 if ( mod->sml_nvalues ) {
4207 BER_BVZERO( &mod->sml_nvalues[j] );
4210 modtail = &mod->sml_next;
4213 op->o_tmpfree( dels, op->o_tmpmemctx );
4214 /* some values were added */
4215 if ( nn && no < o ) {
4216 mod = ch_malloc( sizeof( Modifications ) );
4217 mod->sml_op = LDAP_MOD_ADD;
4219 mod->sml_desc = old->a_desc;
4220 mod->sml_type = mod->sml_desc->ad_cname;
4221 mod->sml_numvals = nn;
4222 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4223 if ( old->a_vals != old->a_nvals ) {
4224 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
4226 mod->sml_nvalues = NULL;
4229 for ( i = 0; i < n; i++ ) {
4230 if ( !adds[i] ) continue;
4231 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
4232 if ( mod->sml_nvalues ) {
4233 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
4237 BER_BVZERO( &mod->sml_values[j] );
4238 if ( mod->sml_nvalues ) {
4239 BER_BVZERO( &mod->sml_nvalues[j] );
4242 modtail = &mod->sml_next;
4245 op->o_tmpfree( adds, op->o_tmpmemctx );
4247 /* new attr, just use the new mod */
4251 /* advance to next element */
4255 **mcur = mod->sml_next;
4257 modtail = &mod->sml_next;
4259 *mcur = &mod->sml_next;
4265 /* Generate a set of modifications to change the old entry into the
4266 * new one. On input ml is a list of modifications equivalent to
4267 * the new entry. It will be massaged and the result will be stored
4270 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
4271 Modifications **mods, Modifications **ml, int is_ctx)
4273 Modifications **modtail = mods;
4275 /* We assume that attributes are saved in the same order
4276 * in the remote and local databases. So if we walk through
4277 * the attributeDescriptions one by one they should match in
4278 * lock step. If not, look for an add or delete.
4280 while ( old && new )
4282 /* If we've seen this before, use its mod now */
4283 if ( new->a_flags & SLAP_ATTR_IXADD ) {
4284 attr_cmp( op, NULL, new, &modtail, &ml );
4288 /* Skip contextCSN */
4289 if ( is_ctx && old->a_desc ==
4290 slap_schema.si_ad_contextCSN ) {
4295 if ( old->a_desc != new->a_desc ) {
4299 /* If it's just been re-added later,
4300 * remember that we've seen it.
4302 tmp = attr_find( new, old->a_desc );
4304 tmp->a_flags |= SLAP_ATTR_IXADD;
4306 /* If it's a new attribute, pull it in.
4308 tmp = attr_find( old, new->a_desc );
4310 attr_cmp( op, NULL, new, &modtail, &ml );
4314 /* Delete old attr */
4315 mod = ch_malloc( sizeof( Modifications ) );
4316 mod->sml_op = LDAP_MOD_DELETE;
4318 mod->sml_desc = old->a_desc;
4319 mod->sml_type = mod->sml_desc->ad_cname;
4320 mod->sml_numvals = 0;
4321 mod->sml_values = NULL;
4322 mod->sml_nvalues = NULL;
4324 modtail = &mod->sml_next;
4329 /* kludge - always update modifiersName so that it
4330 * stays co-located with the other mod opattrs. But only
4331 * if we know there are other valid mods.
4333 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
4334 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
4335 attr_cmp( op, NULL, new, &modtail, &ml );
4337 attr_cmp( op, old, new, &modtail, &ml );
4350 dninfo *dni = op->o_callback->sc_private;
4352 if ( rs->sr_type == REP_SEARCH ) {
4353 if ( !BER_BVISNULL( &dni->dn ) ) {
4354 Debug( LDAP_DEBUG_ANY,
4355 "dn_callback : consistency error - "
4356 "entryUUID is not unique\n", 0, 0, 0 );
4358 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
4359 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
4360 /* If there is a new entry, see if it differs from the old.
4361 * We compare the non-normalized values so that cosmetic changes
4362 * in the provider are always propagated.
4364 if ( dni->new_entry ) {
4365 Attribute *old, *new;
4366 struct berval old_rdn, new_rdn;
4367 struct berval old_p, new_p;
4368 int is_ctx, new_sup = 0;
4370 /* If old entry is not a glue entry, make sure new entry
4371 * is actually newer than old entry
4373 if ( !is_entry_glue( rs->sr_entry )) {
4374 old = attr_find( rs->sr_entry->e_attrs,
4375 slap_schema.si_ad_entryCSN );
4376 new = attr_find( dni->new_entry->e_attrs,
4377 slap_schema.si_ad_entryCSN );
4380 ber_len_t len = old->a_vals[0].bv_len;
4381 if ( len > new->a_vals[0].bv_len )
4382 len = new->a_vals[0].bv_len;
4383 rc = memcmp( old->a_vals[0].bv_val,
4384 new->a_vals[0].bv_val, len );
4386 Debug( LDAP_DEBUG_SYNC,
4387 "dn_callback : new entry is older than ours "
4388 "%s ours %s, new %s\n",
4389 rs->sr_entry->e_name.bv_val,
4390 old->a_vals[0].bv_val,
4391 new->a_vals[0].bv_val );
4392 return LDAP_SUCCESS;
4393 } else if ( rc == 0 ) {
4394 Debug( LDAP_DEBUG_SYNC,
4395 "dn_callback : entries have identical CSN "
4397 rs->sr_entry->e_name.bv_val,
4398 old->a_vals[0].bv_val, 0 );
4399 return LDAP_SUCCESS;
4404 is_ctx = dn_match( &rs->sr_entry->e_nname,
4405 &op->o_bd->be_nsuffix[0] );
4407 /* Did the DN change?
4408 * case changes in the parent are ignored,
4409 * we only want to know if the RDN was
4412 dnRdn( &rs->sr_entry->e_name, &old_rdn );
4413 dnRdn( &dni->new_entry->e_name, &new_rdn );
4414 dnParent( &rs->sr_entry->e_nname, &old_p );
4415 dnParent( &dni->new_entry->e_nname, &new_p );
4417 new_sup = !dn_match( &old_p, &new_p );
4418 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
4420 struct berval oldRDN, oldVal;
4421 AttributeDescription *ad = NULL;
4427 dni->nnewSup = new_p;
4429 /* See if the oldRDN was deleted */
4430 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
4431 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
4432 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
4434 oldRDN.bv_len -= oldVal.bv_len + 1;
4435 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4437 for ( oldpos=0, a=rs->sr_entry->e_attrs;
4438 a && a->a_desc != ad; oldpos++, a=a->a_next );
4439 /* a should not be NULL but apparently it happens.
4442 dni->oldNcount = a ? a->a_numvals : 0;
4443 for ( newpos=0, a=dni->new_entry->e_attrs;
4444 a && a->a_desc != ad; newpos++, a=a->a_next );
4445 if ( !a || oldpos != newpos || attr_valfind( a,
4446 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
4447 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
4448 SLAP_MR_VALUE_OF_SYNTAX,
4449 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
4453 /* Get the newRDN's desc */
4454 dnRdn( &dni->new_entry->e_nname, &oldRDN );
4455 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
4456 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
4458 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4461 /* A ModDN has happened, but in Refresh mode other
4462 * changes may have occurred before we picked it up.
4463 * So fallthru to regular Modify processing.
4467 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
4468 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
4472 } else if ( rs->sr_type == REP_RESULT ) {
4473 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
4474 Debug( LDAP_DEBUG_ANY,
4475 "dn_callback : consistency error - "
4476 "entryUUID is not unique\n", 0, 0, 0 );
4480 return LDAP_SUCCESS;
4484 nonpresent_callback(
4488 syncinfo_t *si = op->o_callback->sc_private;
4491 char *present_uuid = NULL;
4492 struct nonpresent_entry *np_entry;
4494 if ( rs->sr_type == REP_RESULT ) {
4495 count = presentlist_free( si->si_presentlist );
4496 si->si_presentlist = NULL;
4498 } else if ( rs->sr_type == REP_SEARCH ) {
4499 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
4500 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
4503 present_uuid = presentlist_find( si->si_presentlist, &a->a_nvals[0] );
4506 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
4507 char buf[sizeof("rid=999 non")];
4509 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
4510 present_uuid ? "" : "non" );
4512 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
4513 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
4516 if ( a == NULL ) return 0;
4519 if ( present_uuid == NULL ) {
4520 np_entry = (struct nonpresent_entry *)
4521 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
4522 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
4523 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
4524 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
4527 presentlist_delete( &si->si_presentlist, &a->a_nvals[0] );
4528 ch_free( present_uuid );
4531 return LDAP_SUCCESS;
4534 static struct berval *
4535 slap_uuidstr_from_normalized(
4536 struct berval* uuidstr,
4537 struct berval* normalized,
4542 unsigned char nibble;
4545 if ( normalized == NULL ) return NULL;
4546 if ( normalized->bv_len != 16 ) return NULL;
4551 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4552 if ( new == NULL ) {
4559 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4560 if ( new != uuidstr ) {
4561 slap_sl_free( new, ctx );
4566 for ( i = 0; i < 16; i++ ) {
4567 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
4568 new->bv_val[(i<<1)+d] = '-';
4572 nibble = (normalized->bv_val[i] >> 4) & 0xF;
4573 if ( nibble < 10 ) {
4574 new->bv_val[(i<<1)+d] = nibble + '0';
4576 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
4579 nibble = (normalized->bv_val[i]) & 0xF;
4580 if ( nibble < 10 ) {
4581 new->bv_val[(i<<1)+d+1] = nibble + '0';
4583 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
4587 new->bv_val[new->bv_len] = '\0';
4594 if ( normalized == NULL ) return NULL;
4595 if ( normalized->bv_len != 16 ) return NULL;
4601 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4602 if ( new == NULL ) {
4609 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4614 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
4615 normalized->bv_len, new->bv_val, new->bv_len + 1 );
4619 if ( new != NULL ) {
4620 if ( new->bv_val != NULL ) {
4621 slap_sl_free( new->bv_val, ctx );
4624 if ( new != uuidstr ) {
4625 slap_sl_free( new, ctx );
4638 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
4641 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN-2 ));
4643 return ( memcmp( v_uuid1, v_uuid2, UUIDLEN ));
4648 syncinfo_free( syncinfo_t *sie, int free_all )
4650 syncinfo_t *si_next;
4652 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
4653 sie->si_ridtxt, 0, 0 );
4656 si_next = sie->si_next;
4659 if ( sie->si_conn ) {
4660 connection_client_stop( sie->si_conn );
4661 sie->si_conn = NULL;
4663 ldap_unbind_ext( sie->si_ld, NULL, NULL );
4667 struct re_s *re = sie->si_re;
4670 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4671 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
4672 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
4673 ldap_pvt_runqueue_remove( &slapd_rq, re );
4674 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4677 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
4679 bindconf_free( &sie->si_bindconf );
4681 if ( sie->si_filterstr.bv_val ) {
4682 ch_free( sie->si_filterstr.bv_val );
4684 if ( sie->si_filter ) {
4685 filter_free( sie->si_filter );
4687 if ( sie->si_logfilterstr.bv_val ) {
4688 ch_free( sie->si_logfilterstr.bv_val );
4690 if ( sie->si_logfilter ) {
4691 filter_free( sie->si_logfilter );
4693 if ( sie->si_base.bv_val ) {
4694 ch_free( sie->si_base.bv_val );
4696 if ( sie->si_logbase.bv_val ) {
4697 ch_free( sie->si_logbase.bv_val );
4699 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
4700 ch_free( sie->si_contextdn.bv_val );
4702 if ( sie->si_attrs ) {
4704 while ( sie->si_attrs[i] != NULL ) {
4705 ch_free( sie->si_attrs[i] );
4708 ch_free( sie->si_attrs );
4710 if ( sie->si_exattrs ) {
4712 while ( sie->si_exattrs[i] != NULL ) {
4713 ch_free( sie->si_exattrs[i] );
4716 ch_free( sie->si_exattrs );
4718 if ( sie->si_anlist ) {
4720 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
4721 ch_free( sie->si_anlist[i].an_name.bv_val );
4724 ch_free( sie->si_anlist );
4726 if ( sie->si_exanlist ) {
4728 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
4729 ch_free( sie->si_exanlist[i].an_name.bv_val );
4732 ch_free( sie->si_exanlist );
4734 if ( sie->si_retryinterval ) {
4735 ch_free( sie->si_retryinterval );
4737 if ( sie->si_retrynum ) {
4738 ch_free( sie->si_retrynum );
4740 if ( sie->si_retrynum_init ) {
4741 ch_free( sie->si_retrynum_init );
4743 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
4744 if ( sie->si_presentlist ) {
4745 presentlist_free( sie->si_presentlist );
4747 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
4748 struct nonpresent_entry* npe;
4749 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
4750 LDAP_LIST_REMOVE( npe, npe_link );
4751 if ( npe->npe_name ) {
4752 if ( npe->npe_name->bv_val ) {
4753 ch_free( npe->npe_name->bv_val );
4755 ch_free( npe->npe_name );
4757 if ( npe->npe_nname ) {
4758 if ( npe->npe_nname->bv_val ) {
4759 ch_free( npe->npe_nname->bv_val );
4761 ch_free( npe->npe_nname );
4765 if ( sie->si_cookieState ) {
4766 sie->si_cookieState->cs_ref--;
4767 if ( !sie->si_cookieState->cs_ref ) {
4768 ch_free( sie->si_cookieState->cs_sids );
4769 ber_bvarray_free( sie->si_cookieState->cs_vals );
4770 ldap_pvt_thread_cond_destroy( &sie->si_cookieState->cs_cond );
4771 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
4772 ch_free( sie->si_cookieState->cs_psids );
4773 ber_bvarray_free( sie->si_cookieState->cs_pvals );
4774 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
4775 ch_free( sie->si_cookieState );
4778 #ifdef ENABLE_REWRITE
4779 if ( sie->si_rewrite )
4780 rewrite_info_delete( &sie->si_rewrite );
4781 if ( sie->si_suffixm.bv_val )
4782 ch_free( sie->si_suffixm.bv_val );
4786 } while ( free_all && si_next );
4789 #ifdef ENABLE_REWRITE
4791 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4793 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4794 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4795 char *argvRule[] = { "rewriteRule", NULL, NULL, ":", NULL };
4799 if ( si->si_rewrite )
4800 rewrite_info_delete( &si->si_rewrite );
4801 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4803 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4804 if ( rc != LDAP_SUCCESS )
4807 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4808 if ( rc != LDAP_SUCCESS )
4811 vnc = ch_malloc( si->si_base.bv_len + 6 );
4812 strcpy( vnc, "(.*)" );
4813 lutil_strcopy( lutil_strcopy( vnc+4, si->si_base.bv_val ), "$" );
4816 rnc = ch_malloc( si->si_suffixm.bv_len + 3 );
4817 strcpy( rnc, "%1" );
4818 strcpy( rnc+2, si->si_suffixm.bv_val );
4821 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4828 /* NOTE: used & documented in slapd.conf(5) */
4830 #define PROVIDERSTR "provider"
4831 #define SCHEMASTR "schemachecking"
4832 #define FILTERSTR "filter"
4833 #define SEARCHBASESTR "searchbase"
4834 #define SCOPESTR "scope"
4835 #define ATTRSONLYSTR "attrsonly"
4836 #define ATTRSSTR "attrs"
4837 #define TYPESTR "type"
4838 #define INTERVALSTR "interval"
4839 #define RETRYSTR "retry"
4840 #define SLIMITSTR "sizelimit"
4841 #define TLIMITSTR "timelimit"
4842 #define SYNCDATASTR "syncdata"
4843 #define LOGBASESTR "logbase"
4844 #define LOGFILTERSTR "logfilter"
4845 #define SUFFIXMSTR "suffixmassage"
4846 #define STRICT_REFRESH "strictrefresh"
4847 #define LAZY_COMMIT "lazycommit"
4849 /* FIXME: undocumented */
4850 #define EXATTRSSTR "exattrs"
4851 #define MANAGEDSAITSTR "manageDSAit"
4855 GOT_RID = 0x00000001U,
4856 GOT_PROVIDER = 0x00000002U,
4857 GOT_SCHEMACHECKING = 0x00000004U,
4858 GOT_FILTER = 0x00000008U,
4859 GOT_SEARCHBASE = 0x00000010U,
4860 GOT_SCOPE = 0x00000020U,
4861 GOT_ATTRSONLY = 0x00000040U,
4862 GOT_ATTRS = 0x00000080U,
4863 GOT_TYPE = 0x00000100U,
4864 GOT_INTERVAL = 0x00000200U,
4865 GOT_RETRY = 0x00000400U,
4866 GOT_SLIMIT = 0x00000800U,
4867 GOT_TLIMIT = 0x00001000U,
4868 GOT_SYNCDATA = 0x00002000U,
4869 GOT_LOGBASE = 0x00004000U,
4870 GOT_LOGFILTER = 0x00008000U,
4871 GOT_EXATTRS = 0x00010000U,
4872 GOT_MANAGEDSAIT = 0x00020000U,
4873 GOT_BINDCONF = 0x00040000U,
4874 GOT_SUFFIXM = 0x00080000U,
4877 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4880 static slap_verbmasks datamodes[] = {
4881 { BER_BVC("default"), SYNCDATA_DEFAULT },
4882 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4883 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4888 parse_syncrepl_retry(
4895 int use_default = 0;
4897 char *val = arg + STRLENOF( RETRYSTR "=" );
4898 if ( strcasecmp( val, "undefined" ) == 0 ) {
4903 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4904 retry_list[0] = NULL;
4906 slap_str2clist( &retry_list, val, " ,\t" );
4908 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4911 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4912 "Error: incomplete syncrepl retry list" );
4913 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4914 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4915 ch_free( retry_list[k] );
4917 ch_free( retry_list );
4920 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4921 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4922 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4923 for ( j = 0; j < n; j++ ) {
4925 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4926 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4927 "Error: invalid retry interval \"%s\" (#%d)",
4928 retry_list[j*2], j );
4929 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4930 /* do some cleanup */
4933 si->si_retryinterval[j] = (time_t)t;
4934 if ( *retry_list[j*2+1] == '+' ) {
4935 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4936 si->si_retrynum[j] = RETRYNUM_FOREVER;
4940 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4941 || si->si_retrynum_init[j] <= 0 )
4943 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4944 "Error: invalid initial retry number \"%s\" (#%d)",
4945 retry_list[j*2+1], j );
4946 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4947 /* do some cleanup */
4950 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4951 || si->si_retrynum[j] <= 0 )
4953 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4954 "Error: invalid retry number \"%s\" (#%d)",
4955 retry_list[j*2+1], j );
4956 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4957 /* do some cleanup */
4962 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4963 Debug( LDAP_DEBUG_CONFIG,
4964 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4968 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4969 si->si_retrynum[j] = RETRYNUM_TAIL;
4970 si->si_retryinterval[j] = 0;
4972 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4973 ch_free( retry_list[k] );
4975 ch_free( retry_list );
4976 if ( !use_default ) {
4977 si->si_got |= GOT_RETRY;
4984 parse_syncrepl_line(
4991 for ( i = 1; i < c->argc; i++ ) {
4992 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4993 STRLENOF( IDSTR "=" ) ) )
4996 /* '\0' string terminator accounts for '=' */
4997 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4998 if ( lutil_atoi( &tmp, val ) != 0 ) {
4999 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5000 "Error: parse_syncrepl_line: "
5001 "unable to parse syncrepl id \"%s\"", val );
5002 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5005 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
5006 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5007 "Error: parse_syncrepl_line: "
5008 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
5009 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5013 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
5014 si->si_got |= GOT_RID;
5015 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
5016 STRLENOF( PROVIDERSTR "=" ) ) )
5018 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
5019 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
5021 if ( ldap_is_ldaps_url( val ))
5022 si->si_bindconf.sb_tls_do_init = 1;
5024 si->si_got |= GOT_PROVIDER;
5025 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
5026 STRLENOF( SCHEMASTR "=" ) ) )
5028 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
5029 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
5030 si->si_schemachecking = 1;
5031 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
5032 si->si_schemachecking = 0;
5034 si->si_schemachecking = 1;
5036 si->si_got |= GOT_SCHEMACHECKING;
5037 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
5038 STRLENOF( FILTERSTR "=" ) ) )
5040 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
5041 if ( si->si_filterstr.bv_val )
5042 ch_free( si->si_filterstr.bv_val );
5043 ber_str2bv( val, 0, 1, &si->si_filterstr );
5044 si->si_got |= GOT_FILTER;
5045 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
5046 STRLENOF( LOGFILTERSTR "=" ) ) )
5048 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
5049 if ( si->si_logfilterstr.bv_val )
5050 ch_free( si->si_logfilterstr.bv_val );
5051 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
5052 si->si_got |= GOT_LOGFILTER;
5053 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
5054 STRLENOF( SEARCHBASESTR "=" ) ) )
5059 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
5060 if ( si->si_base.bv_val ) {
5061 ch_free( si->si_base.bv_val );
5063 ber_str2bv( val, 0, 0, &bv );
5064 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
5065 if ( rc != LDAP_SUCCESS ) {
5066 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5067 "Invalid base DN \"%s\": %d (%s)",
5068 val, rc, ldap_err2string( rc ) );
5069 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5072 si->si_got |= GOT_SEARCHBASE;
5073 #ifdef ENABLE_REWRITE
5074 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
5075 STRLENOF( SUFFIXMSTR "=" ) ) )
5080 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
5081 if ( si->si_suffixm.bv_val ) {
5082 ch_free( si->si_suffixm.bv_val );
5084 ber_str2bv( val, 0, 0, &bv );
5085 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
5086 if ( rc != LDAP_SUCCESS ) {
5087 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5088 "Invalid massage DN \"%s\": %d (%s)",
5089 val, rc, ldap_err2string( rc ) );
5090 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5093 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
5094 ch_free( si->si_suffixm.bv_val );
5095 BER_BVZERO( &si->si_suffixm );
5096 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5097 "Massage DN \"%s\" is not within the database naming context",
5099 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5102 si->si_got |= GOT_SUFFIXM;
5104 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
5105 STRLENOF( LOGBASESTR "=" ) ) )
5110 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
5111 if ( si->si_logbase.bv_val ) {
5112 ch_free( si->si_logbase.bv_val );
5114 ber_str2bv( val, 0, 0, &bv );
5115 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
5116 if ( rc != LDAP_SUCCESS ) {
5117 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5118 "Invalid logbase DN \"%s\": %d (%s)",
5119 val, rc, ldap_err2string( rc ) );
5120 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5123 si->si_got |= GOT_LOGBASE;
5124 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
5125 STRLENOF( SCOPESTR "=" ) ) )
5128 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
5129 j = ldap_pvt_str2scope( val );
5131 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5132 "Error: parse_syncrepl_line: "
5133 "unknown scope \"%s\"", val);
5134 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5138 si->si_got |= GOT_SCOPE;
5139 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
5140 STRLENOF( ATTRSONLYSTR ) ) )
5142 si->si_attrsonly = 1;
5143 si->si_got |= GOT_ATTRSONLY;
5144 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
5145 STRLENOF( ATTRSSTR "=" ) ) )
5147 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
5148 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
5150 attr_fname = ch_strdup( val + STRLENOF(":include:") );
5151 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
5152 if ( si->si_anlist == NULL ) {
5153 ch_free( attr_fname );
5156 si->si_anfile = attr_fname;
5158 char *str, *s, *next;
5159 const char *delimstr = " ,\t";
5160 str = ch_strdup( val );
5161 for ( s = ldap_pvt_strtok( str, delimstr, &next );
5163 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
5165 if ( strlen(s) == 1 && *s == '*' ) {
5166 si->si_allattrs = 1;
5167 val[ s - str ] = delimstr[0];
5169 if ( strlen(s) == 1 && *s == '+' ) {
5170 si->si_allopattrs = 1;
5171 val [ s - str ] = delimstr[0];
5175 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
5176 if ( si->si_anlist == NULL ) {
5180 si->si_got |= GOT_ATTRS;
5181 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
5182 STRLENOF( EXATTRSSTR "=" ) ) )
5184 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
5185 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
5187 attr_fname = ch_strdup( val + STRLENOF(":include:") );
5188 si->si_exanlist = file2anlist(
5189 si->si_exanlist, attr_fname, " ,\t" );
5190 if ( si->si_exanlist == NULL ) {
5191 ch_free( attr_fname );
5194 ch_free( attr_fname );
5196 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
5197 if ( si->si_exanlist == NULL ) {
5201 si->si_got |= GOT_EXATTRS;
5202 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
5203 STRLENOF( TYPESTR "=" ) ) )
5205 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
5206 if ( !strncasecmp( val, "refreshOnly",
5207 STRLENOF("refreshOnly") ) )
5209 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5210 } else if ( !strncasecmp( val, "refreshAndPersist",
5211 STRLENOF("refreshAndPersist") ) )
5213 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
5214 si->si_interval = 60;
5216 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5217 "Error: parse_syncrepl_line: "
5218 "unknown sync type \"%s\"", val);
5219 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5222 si->si_got |= GOT_TYPE;
5223 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
5224 STRLENOF( INTERVALSTR "=" ) ) )
5226 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
5227 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
5228 si->si_interval = 0;
5229 } else if ( strchr( val, ':' ) != NULL ) {
5230 char *next, *ptr = val;
5233 dd = strtol( ptr, &next, 10 );
5234 if ( next == ptr || next[0] != ':' || dd < 0 ) {
5235 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5236 "Error: parse_syncrepl_line: "
5237 "invalid interval \"%s\", unable to parse days", val );
5238 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5242 hh = strtol( ptr, &next, 10 );
5243 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
5244 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5245 "Error: parse_syncrepl_line: "
5246 "invalid interval \"%s\", unable to parse hours", val );
5247 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5251 mm = strtol( ptr, &next, 10 );
5252 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
5253 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5254 "Error: parse_syncrepl_line: "
5255 "invalid interval \"%s\", unable to parse minutes", val );
5256 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5260 ss = strtol( ptr, &next, 10 );
5261 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
5262 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5263 "Error: parse_syncrepl_line: "
5264 "invalid interval \"%s\", unable to parse seconds", val );
5265 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5268 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
5272 if ( lutil_parse_time( val, &t ) != 0 ) {
5273 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5274 "Error: parse_syncrepl_line: "
5275 "invalid interval \"%s\"", val );
5276 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5279 si->si_interval = (time_t)t;
5281 if ( si->si_interval < 0 ) {
5282 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5283 "Error: parse_syncrepl_line: "
5284 "invalid interval \"%ld\"",
5285 (long) si->si_interval);
5286 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5289 si->si_got |= GOT_INTERVAL;
5290 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
5291 STRLENOF( RETRYSTR "=" ) ) )
5293 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
5296 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
5297 STRLENOF( MANAGEDSAITSTR "=" ) ) )
5299 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
5300 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
5301 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
5303 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5304 "invalid manageDSAit value \"%s\".\n",
5306 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5309 si->si_got |= GOT_MANAGEDSAIT;
5310 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
5311 STRLENOF( SLIMITSTR "=") ) )
5313 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
5314 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5317 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
5318 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5319 "invalid size limit value \"%s\".\n",
5321 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5324 si->si_got |= GOT_SLIMIT;
5325 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
5326 STRLENOF( TLIMITSTR "=" ) ) )
5328 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
5329 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5332 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
5333 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5334 "invalid time limit value \"%s\".\n",
5336 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5339 si->si_got |= GOT_TLIMIT;
5340 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
5341 STRLENOF( SYNCDATASTR "=" ) ) )
5343 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
5344 si->si_syncdata = verb_to_mask( val, datamodes );
5345 si->si_got |= GOT_SYNCDATA;
5346 } else if ( !strncasecmp( c->argv[ i ], STRICT_REFRESH,
5347 STRLENOF( STRICT_REFRESH ) ) )
5349 si->si_strict_refresh = 1;
5350 } else if ( !strncasecmp( c->argv[ i ], LAZY_COMMIT,
5351 STRLENOF( LAZY_COMMIT ) ) )
5353 si->si_lazyCommit = 1;
5354 } else if ( !bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
5355 si->si_got |= GOT_BINDCONF;
5357 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5358 "Error: parse_syncrepl_line: "
5359 "unable to parse \"%s\"\n", c->argv[ i ] );
5360 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5365 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
5366 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5367 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
5368 si->si_got & GOT_RID ? "" : " "IDSTR,
5369 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
5370 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
5371 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5375 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
5376 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5377 "Base DN \"%s\" is not within the database naming context",
5378 si->si_base.bv_val );
5379 ch_free( si->si_base.bv_val );
5380 BER_BVZERO( &si->si_base );
5381 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5385 #ifdef ENABLE_REWRITE
5386 if ( si->si_got & GOT_SUFFIXM ) {
5387 if (config_suffixm( c, si )) {
5388 ch_free( si->si_suffixm.bv_val );
5389 BER_BVZERO( &si->si_suffixm );
5390 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5391 "Error configuring rewrite engine" );
5392 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5398 if ( !( si->si_got & GOT_RETRY ) ) {
5399 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
5400 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
5401 if ( si->si_retryinterval == NULL ) {
5402 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
5408 si->si_filter = str2filter( si->si_filterstr.bv_val );
5409 if ( si->si_filter == NULL ) {
5410 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
5411 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
5415 if ( si->si_got & GOT_LOGFILTER ) {
5416 si->si_logfilter = str2filter( si->si_logfilterstr.bv_val );
5417 if ( si->si_logfilter == NULL ) {
5418 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse logfilter=\"%s\"\n",
5419 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_logfilterstr.bv_val );
5434 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
5435 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
5436 "operations required for syncrepl", c->be->be_type );
5437 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5440 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
5441 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
5442 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5445 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
5448 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
5452 si->si_bindconf.sb_tls = SB_TLS_OFF;
5453 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
5454 si->si_schemachecking = 0;
5455 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
5456 &si->si_filterstr );
5457 si->si_base.bv_val = NULL;
5458 si->si_scope = LDAP_SCOPE_SUBTREE;
5459 si->si_attrsonly = 0;
5460 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5461 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5462 si->si_attrs = NULL;
5463 si->si_allattrs = 0;
5464 si->si_allopattrs = 0;
5465 si->si_exattrs = NULL;
5466 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5467 si->si_interval = 86400;
5468 si->si_retryinterval = NULL;
5469 si->si_retrynum_init = NULL;
5470 si->si_retrynum = NULL;
5471 si->si_manageDSAit = 0;
5475 si->si_presentlist = NULL;
5476 LDAP_LIST_INIT( &si->si_nonpresentlist );
5477 ldap_pvt_thread_mutex_init( &si->si_mutex );
5479 rc = parse_syncrepl_line( c, si );
5484 /* Must be LDAPv3 because we need controls */
5485 switch ( si->si_bindconf.sb_version ) {
5487 /* not explicitly set */
5488 si->si_bindconf.sb_version = LDAP_VERSION3;
5491 /* explicitly set */
5494 Debug( LDAP_DEBUG_ANY,
5495 "version %d incompatible with syncrepl\n",
5496 si->si_bindconf.sb_version, 0, 0 );
5497 syncinfo_free( si, 0 );
5501 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
5502 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5503 "<%s> invalid URL", c->argv[0] );
5504 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
5505 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
5510 if ( slapMode & SLAP_SERVER_MODE ) {
5512 /* check if consumer points to current server and database.
5513 * If so, ignore this configuration.
5515 if ( !SLAP_DBHIDDEN( c->be ) ) {
5517 /* if searchbase doesn't match current DB suffix,
5518 * assume it's different
5520 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
5521 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
5526 /* if searchbase matches, see if URLs match */
5527 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
5533 init_syncrepl( si );
5534 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5535 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
5536 si->si_interval, do_syncrepl, si, "do_syncrepl",
5538 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5540 rc = config_sync_shadow( c ) ? -1 : 0;
5545 /* mirrormode still needs to see this flag in tool mode */
5546 rc = config_sync_shadow( c ) ? -1 : 0;
5548 ldap_free_urldesc( lud );
5552 /* Use main slapd defaults */
5553 bindconf_tls_defaults( &si->si_bindconf );
5556 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
5557 syncinfo_free( si, 0 );
5560 Debug( LDAP_DEBUG_CONFIG,
5561 "Config: ** successfully added syncrepl %s \"%s\"\n",
5563 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
5564 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
5565 if ( c->be->be_syncinfo ) {
5568 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
5570 /* add new syncrepl to end of list (same order as when deleting) */
5571 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
5574 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
5575 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
5576 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
5577 ldap_pvt_thread_cond_init( &si->si_cookieState->cs_cond );
5579 c->be->be_syncinfo = si;
5581 si->si_cookieState->cs_ref++;
5590 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
5592 struct berval bc, uri, bs;
5593 char buf[BUFSIZ*2], *ptr;
5596 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
5600 /* temporarily inhibit bindconf from printing URI */
5601 uri = si->si_bindconf.sb_uri;
5602 BER_BVZERO( &si->si_bindconf.sb_uri );
5603 si->si_bindconf.sb_version = 0;
5604 bindconf_unparse( &si->si_bindconf, &bc );
5605 si->si_bindconf.sb_uri = uri;
5606 si->si_bindconf.sb_version = LDAP_VERSION3;
5609 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
5610 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
5611 si->si_rid, si->si_bindconf.sb_uri.bv_val );
5612 if ( len >= sizeof( buf ) ) return;
5614 if ( !BER_BVISNULL( &bc ) ) {
5615 if ( WHATSLEFT <= bc.bv_len ) {
5619 ptr = lutil_strcopy( ptr, bc.bv_val );
5622 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
5623 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
5624 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
5625 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
5628 if ( !BER_BVISNULL( &si->si_base ) ) {
5629 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
5630 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
5631 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
5634 #ifdef ENABLE_REWRITE
5635 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
5636 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
5637 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
5638 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
5642 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
5643 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
5644 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
5645 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
5648 if ( !BER_BVISNULL( &si->si_logbase ) ) {
5649 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
5650 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
5651 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
5654 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
5655 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
5656 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
5657 ptr = lutil_strcopy( ptr, bs.bv_val );
5659 if ( si->si_attrsonly ) {
5660 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5661 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
5663 if ( si->si_anfile ) {
5664 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
5665 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
5666 ptr = lutil_strcopy( ptr, si->si_anfile );
5668 } else if ( si->si_allattrs || si->si_allopattrs ||
5669 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
5673 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5674 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
5676 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
5677 if ( ptr == NULL ) return;
5678 if ( si->si_allattrs ) {
5679 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
5680 if ( old != ptr ) *ptr++ = ',';
5683 if ( si->si_allopattrs ) {
5684 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
5685 if ( old != ptr ) *ptr++ = ',';
5690 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
5691 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
5692 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
5693 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
5694 if ( ptr == NULL ) return;
5696 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
5697 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
5698 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
5700 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
5701 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
5702 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
5703 "refreshAndPersist" : "refreshOnly" );
5705 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
5708 dd = si->si_interval;
5715 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
5716 INTERVALSTR, dd, hh, mm, ss );
5717 if ( len >= WHATSLEFT ) return;
5721 if ( si->si_got & GOT_RETRY ) {
5722 const char *space = "";
5723 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
5724 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
5725 for (i=0; si->si_retryinterval[i]; i++) {
5726 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
5727 (long) si->si_retryinterval[i] );
5729 if ( WHATSLEFT - 1 <= len ) return;
5731 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
5734 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
5735 if ( WHATSLEFT <= len ) return;
5739 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
5742 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
5745 if ( si->si_slimit ) {
5746 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
5747 if ( WHATSLEFT <= len ) return;
5751 if ( si->si_tlimit ) {
5752 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
5753 if ( WHATSLEFT <= len ) return;
5757 if ( si->si_syncdata ) {
5758 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
5759 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
5760 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
5761 ptr = lutil_strcopy( ptr, bc.bv_val );
5765 if ( si->si_lazyCommit ) {
5766 ptr = lutil_strcopy( ptr, " " LAZY_COMMIT );
5769 bc.bv_len = ptr - buf;
5771 ber_dupbv( bv, &bc );
5775 syncrepl_config( ConfigArgs *c )
5777 if (c->op == SLAP_CONFIG_EMIT) {
5778 if ( c->be->be_syncinfo ) {
5782 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
5783 syncrepl_unparse( si, &bv );
5784 ber_bvarray_add( &c->rvalue_vals, &bv );
5789 } else if ( c->op == LDAP_MOD_DELETE ) {
5791 if ( c->be->be_syncinfo ) {
5792 syncinfo_t *si, **sip;
5795 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
5797 if ( c->valx == -1 || i == c->valx ) {
5801 /* If the task is currently active, we have to leave
5802 * it running. It will exit on its own. This will only
5803 * happen when running on the cn=config DB.
5806 if ( si->si_be == c->be || ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
5809 /* There is no active thread, but we must still
5810 * ensure that no thread is (or will be) queued
5811 * while we removes the task.
5813 struct re_s *re = si->si_re;
5816 if ( si->si_conn ) {
5817 connection_client_stop( si->si_conn );
5821 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5822 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
5823 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5826 if ( ldap_pvt_thread_pool_retract( re->pool_cookie ) > 0 )
5829 ldap_pvt_runqueue_remove( &slapd_rq, re );
5830 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5832 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5836 syncinfo_free( si, 0 );
5845 if ( !c->be->be_syncinfo ) {
5846 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5850 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5851 Debug(LDAP_DEBUG_ANY, "%s: "
5852 "syncrepl: database already shadowed.\n",
5856 return add_syncrepl( c );