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-2011 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>"
39 struct nonpresent_entry {
40 struct berval *npe_name;
41 struct berval *npe_nname;
42 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
45 typedef struct cookie_state {
46 ldap_pvt_thread_mutex_t cs_mutex;
50 struct berval *cs_vals;
53 /* pending changes, not yet committed */
54 ldap_pvt_thread_mutex_t cs_pmutex;
56 struct berval *cs_pvals;
60 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
61 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
62 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
64 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
65 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
67 #define RETRYNUM_FOREVER (-1) /* retry forever */
68 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
69 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
70 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
72 typedef struct syncinfo_s {
73 struct syncinfo_s *si_next;
78 char si_ridtxt[ STRLENOF("rid=999") + 1 ];
79 slap_bindconf si_bindconf;
80 struct berval si_base;
81 struct berval si_logbase;
82 struct berval si_filterstr;
84 struct berval si_logfilterstr;
85 struct berval si_contextdn;
89 AttributeName *si_anlist;
90 AttributeName *si_exanlist;
95 int si_schemachecking;
96 int si_type; /* the active type */
97 int si_ctype; /* the configured type */
99 time_t *si_retryinterval;
100 int *si_retrynum_init;
102 struct sync_cookie si_syncCookie;
103 cookie_state *si_cookieState;
108 int si_refreshDelete;
109 int si_refreshPresent;
114 int si_strict_refresh; /* stop listening during fallback refresh */
116 Avlnode *si_presentlist;
119 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
120 #ifdef ENABLE_REWRITE
121 struct rewrite_info *si_rewrite;
122 struct berval si_suffixm;
124 ldap_pvt_thread_mutex_t si_mutex;
127 static int syncuuid_cmp( const void *, const void * );
128 static int avl_presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
129 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
130 static int syncrepl_message_to_op(
131 syncinfo_t *, Operation *, LDAPMessage * );
132 static int syncrepl_message_to_entry(
133 syncinfo_t *, Operation *, LDAPMessage *,
134 Modifications **, Entry **, int );
135 static int syncrepl_entry(
136 syncinfo_t *, Operation*, Entry*,
137 Modifications**,int, struct berval*,
138 struct berval *cookieCSN );
139 static int syncrepl_updateCookie(
140 syncinfo_t *, Operation *,
141 struct sync_cookie * );
142 static struct berval * slap_uuidstr_from_normalized(
143 struct berval *, struct berval *, void * );
144 static int syncrepl_add_glue_ancestors(
145 Operation* op, Entry *e );
147 /* callback functions */
148 static int dn_callback( Operation *, SlapReply * );
149 static int nonpresent_callback( Operation *, SlapReply * );
150 static int null_callback( Operation *, SlapReply * );
152 static AttributeDescription *sync_descs[4];
155 syncrepl_state2str( int state )
158 case LDAP_SYNC_PRESENT:
164 case LDAP_SYNC_MODIFY:
167 case LDAP_SYNC_DELETE:
175 init_syncrepl(syncinfo_t *si)
178 char **attrs, **exattrs;
180 if ( !sync_descs[0] ) {
181 sync_descs[0] = slap_schema.si_ad_objectClass;
182 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
183 sync_descs[2] = slap_schema.si_ad_entryCSN;
184 sync_descs[3] = NULL;
187 if ( si->si_allattrs && si->si_allopattrs )
190 attrs = anlist2attrs( si->si_anlist );
193 if ( si->si_allattrs ) {
196 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
197 for ( j = i; attrs[j] != NULL; j++ ) {
200 attrs[j] = attrs[j+1];
206 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
207 attrs[i] = ch_strdup("*");
210 } else if ( si->si_allopattrs ) {
213 if ( is_at_operational( at_find( attrs[i] ) ) ) {
214 for ( j = i; attrs[j] != NULL; j++ ) {
217 attrs[j] = attrs[j+1];
223 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
224 attrs[i] = ch_strdup("+");
228 for ( i = 0; sync_descs[i] != NULL; i++ ) {
231 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
232 for ( k = j; attrs[k] != NULL; k++ ) {
235 attrs[k] = attrs[k+1];
243 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
245 if ( si->si_allopattrs ) {
246 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
248 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
252 if ( si->si_allopattrs ) {
253 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
255 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
256 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
264 if ( si->si_allattrs == si->si_allopattrs ) {
265 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
266 attrs[i++] = ch_strdup( "*" );
267 attrs[i++] = ch_strdup( "+" );
268 } else if ( si->si_allattrs && !si->si_allopattrs ) {
269 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
270 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
271 attrs[i++] = ch_strdup( "*" );
272 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
273 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
275 } else if ( !si->si_allattrs && si->si_allopattrs ) {
276 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
277 attrs[i++] = ch_strdup( "+" );
278 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
283 si->si_attrs = attrs;
285 exattrs = anlist2attrs( si->si_exanlist );
288 for ( n = 0; exattrs[n] != NULL; n++ ) ;
290 for ( i = 0; sync_descs[i] != NULL; i++ ) {
292 while ( exattrs[j] != NULL ) {
293 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
294 ch_free( exattrs[j] );
295 for ( k = j; exattrs[k] != NULL; k++ ) {
296 exattrs[k] = exattrs[k+1];
304 for ( i = 0; exattrs[i] != NULL; i++ ) {
305 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
307 if ( ( oc = si->si_anlist[j].an_oc ) ) {
309 while ( oc->soc_required[k] ) {
310 if ( !strcmp( exattrs[i],
311 oc->soc_required[k]->sat_cname.bv_val ) ) {
312 ch_free( exattrs[i] );
313 for ( l = i; exattrs[l]; l++ ) {
314 exattrs[l] = exattrs[l+1];
324 for ( i = 0; exattrs[i] != NULL; i++ ) ;
327 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
330 si->si_exattrs = exattrs;
333 typedef struct logschema {
335 struct berval ls_req;
336 struct berval ls_mod;
337 struct berval ls_newRdn;
338 struct berval ls_delRdn;
339 struct berval ls_newSup;
342 static logschema changelog_sc = {
344 BER_BVC("changeType"),
347 BER_BVC("deleteOldRDN"),
348 BER_BVC("newSuperior")
351 static logschema accesslog_sc = {
355 BER_BVC("reqNewRDN"),
356 BER_BVC("reqDeleteOldRDN"),
357 BER_BVC("reqNewSuperior")
365 BerElementBuffer berbuf;
366 BerElement *ber = (BerElement *)&berbuf;
367 LDAPControl c[3], *ctrls[4];
371 char **attrs, *lattrs[8];
376 /* setup LDAP SYNC control */
377 ber_init2( ber, NULL, LBER_USE_DER );
378 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
380 /* If we're using a log but we have no state, then fallback to
381 * normal mode for a full refresh.
383 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
384 si->si_logstate = SYNCLOG_FALLBACK;
387 /* Use the log parameters if we're in log mode */
388 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
390 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
394 lattrs[0] = ls->ls_dn.bv_val;
395 lattrs[1] = ls->ls_req.bv_val;
396 lattrs[2] = ls->ls_mod.bv_val;
397 lattrs[3] = ls->ls_newRdn.bv_val;
398 lattrs[4] = ls->ls_delRdn.bv_val;
399 lattrs[5] = ls->ls_newSup.bv_val;
400 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
404 base = si->si_logbase.bv_val;
405 filter = si->si_logfilterstr.bv_val;
408 scope = LDAP_SCOPE_SUBTREE;
411 base = si->si_base.bv_val;
412 filter = si->si_filterstr.bv_val;
413 attrs = si->si_attrs;
414 attrsonly = si->si_attrsonly;
415 scope = si->si_scope;
417 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
418 si->si_type = LDAP_SYNC_REFRESH_ONLY;
420 si->si_type = si->si_ctype;
423 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
425 ber_printf( ber, "{eOb}",
426 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
428 ber_printf( ber, "{eb}",
429 abs(si->si_type), rhint );
432 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == -1 ) {
437 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
438 c[0].ldctl_iscritical = si->si_type < 0;
441 c[1].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
442 BER_BVZERO( &c[1].ldctl_value );
443 c[1].ldctl_iscritical = 1;
446 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
447 c[2].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
448 c[2].ldctl_value = si->si_bindconf.sb_authzId;
449 c[2].ldctl_iscritical = 1;
456 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
457 ctrls, NULL, NULL, si->si_slimit, &si->si_msgid );
470 SlapReply rs = {REP_SEARCH};
471 int i, j, changed = 0;
473 /* Look for contextCSN from syncprov overlay. If
474 * there's no overlay, this will be a no-op. That means
475 * this is a pure consumer, so local changes will not be
476 * allowed, and all changes will already be reflected in
479 a.a_desc = slap_schema.si_ad_contextCSN;
481 e.e_name = si->si_contextdn;
482 e.e_nname = si->si_contextdn;
483 at[0].an_name = a.a_desc->ad_cname;
484 at[0].an_desc = a.a_desc;
485 BER_BVZERO( &at[1].an_name );
487 rs.sr_flags = REP_ENTRY_MODIFIABLE;
489 op->o_req_dn = e.e_name;
490 op->o_req_ndn = e.e_nname;
492 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
493 i = backend_operational( op, &rs );
494 if ( i == LDAP_SUCCESS && a.a_nvals ) {
495 int num = a.a_numvals;
496 /* check for differences */
497 if ( num != si->si_cookieState->cs_num ) {
500 for ( i=0; i<num; i++ ) {
501 if ( ber_bvcmp( &a.a_nvals[i],
502 &si->si_cookieState->cs_vals[i] )) {
509 ber_bvarray_free( si->si_cookieState->cs_vals );
510 ch_free( si->si_cookieState->cs_sids );
511 si->si_cookieState->cs_num = num;
512 si->si_cookieState->cs_vals = a.a_nvals;
513 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_nvals,
515 si->si_cookieState->cs_age++;
517 ber_bvarray_free( a.a_nvals );
519 ber_bvarray_free( a.a_vals );
521 /* See if the cookieState has changed due to anything outside
522 * this particular consumer. That includes other consumers in
523 * the same context, or local changes detected above.
525 if ( si->si_cookieState->cs_num > 0 && si->si_cookieAge !=
526 si->si_cookieState->cs_age ) {
527 if ( !si->si_syncCookie.numcsns ) {
528 ber_bvarray_free( si->si_syncCookie.ctxcsn );
529 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
530 si->si_cookieState->cs_vals, NULL );
533 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
534 /* bogus, just dup everything */
535 if ( si->si_syncCookie.sids[i] == -1 ) {
536 ber_bvarray_free( si->si_syncCookie.ctxcsn );
537 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
538 si->si_cookieState->cs_vals, NULL );
542 for (j=0; j<si->si_cookieState->cs_num; j++) {
543 if ( si->si_syncCookie.sids[i] !=
544 si->si_cookieState->cs_sids[j] )
546 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
547 &si->si_cookieState->cs_vals[j] ))
549 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
550 &si->si_cookieState->cs_vals[j] );
558 si->si_cookieAge = si->si_cookieState->cs_age;
559 ch_free( si->si_syncCookie.octet_str.bv_val );
560 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
561 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
562 si->si_syncCookie.sid );
564 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
574 int cmdline_cookie_found = 0;
576 struct sync_cookie *sc = NULL;
581 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
582 if ( rc != LDAP_SUCCESS ) {
585 op->o_protocol = LDAP_VERSION3;
587 /* Set SSF to strongest of TLS, SASL SSFs */
590 op->o_transport_ssf = 0;
592 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
593 == LDAP_SUCCESS && ssl != NULL )
595 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
597 #endif /* HAVE_TLS */
599 ber_len_t ssf; /* ITS#5403, 3864 LDAP_OPT_X_SASL_SSF probably ought
600 to use sasl_ssf_t but currently uses ber_len_t */
601 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &ssf )
603 op->o_sasl_ssf = ssf;
605 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
606 ? op->o_sasl_ssf : op->o_tls_ssf;
608 ldap_set_option( si->si_ld, LDAP_OPT_TIMELIMIT, &si->si_tlimit );
610 rc = LDAP_DEREF_NEVER; /* actually could allow DEREF_FINDING */
611 ldap_set_option( si->si_ld, LDAP_OPT_DEREF, &rc );
613 ldap_set_option( si->si_ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
615 si->si_syncCookie.rid = si->si_rid;
617 /* whenever there are multiple data sources possible, advertise sid */
618 si->si_syncCookie.sid = ( SLAP_MULTIMASTER( si->si_be ) || si->si_be != si->si_wbe ) ?
621 /* We've just started up, or the remote server hasn't sent us
622 * any meaningful state.
624 if ( !si->si_syncCookie.ctxcsn ) {
627 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
628 if ( si->si_rid == sc->rid ) {
629 cmdline_cookie_found = 1;
634 if ( cmdline_cookie_found ) {
635 /* cookie is supplied in the command line */
637 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
639 /* ctxcsn wasn't parsed yet, do it now */
640 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
641 slap_sync_cookie_free( &si->si_syncCookie, 0 );
642 slap_dup_sync_cookie( &si->si_syncCookie, sc );
643 slap_sync_cookie_free( sc, 1 );
645 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
646 if ( !si->si_cookieState->cs_num ) {
647 /* get contextCSN shadow replica from database */
648 BerVarray csn = NULL;
649 void *ctx = op->o_tmpmemctx;
651 op->o_req_ndn = si->si_contextdn;
652 op->o_req_dn = op->o_req_ndn;
654 /* try to read stored contextCSN */
655 op->o_tmpmemctx = NULL;
656 backend_attribute( op, NULL, &op->o_req_ndn,
657 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
658 op->o_tmpmemctx = ctx;
660 si->si_cookieState->cs_vals = csn;
661 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
662 si->si_cookieState->cs_num = i;
663 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
666 if ( si->si_cookieState->cs_num ) {
667 ber_bvarray_free( si->si_syncCookie.ctxcsn );
668 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
669 si->si_cookieState->cs_vals, NULL )) {
671 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
674 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
675 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
677 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
678 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
680 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
683 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
684 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
685 si->si_syncCookie.sid );
687 /* ITS#6367: recreate the cookie so it has our SID, not our peer's */
688 ch_free( si->si_syncCookie.octet_str.bv_val );
689 BER_BVZERO( &si->si_syncCookie.octet_str );
690 /* Look for contextCSN from syncprov overlay. */
691 check_syncprov( op, si );
692 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
693 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
694 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
695 si->si_syncCookie.sid );
698 si->si_refreshDone = 0;
700 rc = ldap_sync_search( si, op->o_tmpmemctx );
702 if( rc != LDAP_SUCCESS ) {
703 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
704 "ldap_search_ext: %s (%d)\n",
705 si->si_ridtxt, ldap_err2string( rc ), rc );
711 ldap_unbind_ext( si->si_ld, NULL, NULL );
720 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
727 if ( sc1->numcsns < sc2->numcsns ) {
728 *which = sc1->numcsns;
732 for (j=0; j<sc2->numcsns; j++) {
733 for (i=0; i<sc1->numcsns; i++) {
734 if ( sc1->sids[i] != sc2->sids[j] )
736 value_match( &match, slap_schema.si_ad_entryCSN,
737 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
738 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
739 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
746 if ( i == sc1->numcsns ) {
747 /* sc2 has a sid sc1 lacks */
755 #define SYNC_PAUSED -3
762 BerElementBuffer berbuf;
763 BerElement *ber = (BerElement *)&berbuf;
765 LDAPMessage *msg = NULL;
767 struct sync_cookie syncCookie = { NULL };
768 struct sync_cookie syncCookie_req = { NULL };
773 Modifications *modlist = NULL;
777 struct timeval *tout_p = NULL;
778 struct timeval tout = { 0, 0 };
780 int refreshDeletes = 0;
781 char empty[6] = "empty";
783 if ( slapd_shutdown ) {
788 ber_init2( ber, NULL, LBER_USE_DER );
789 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
791 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
793 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
795 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
801 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
802 tout_p, &msg ) ) > 0 )
804 int match, punlock, syncstate;
805 struct berval *retdata, syncUUID, cookie = BER_BVNULL;
807 LDAPControl **rctrls = NULL, *rctrlp = NULL;
814 if ( slapd_shutdown ) {
818 switch( ldap_msgtype( msg ) ) {
819 case LDAP_RES_SEARCH_ENTRY:
820 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
821 ldap_get_dn_ber( si->si_ld, msg, NULL, &bdn );
824 bdn.bv_len = sizeof(empty)-1;
826 /* we can't work without the control */
828 LDAPControl **next = NULL;
829 /* NOTE: make sure we use the right one;
830 * a better approach would be to run thru
831 * the whole list and take care of all */
832 /* NOTE: since we issue the search request,
833 * we should know what controls to expect,
834 * and there should be none apart from the
835 * sync-related control */
836 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
837 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
839 bdn.bv_val[bdn.bv_len] = '\0';
840 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
841 "got search entry with multiple "
842 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
843 ldap_controls_free( rctrls );
848 if ( rctrlp == NULL ) {
849 bdn.bv_val[bdn.bv_len] = '\0';
850 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
851 "got search entry without "
852 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
856 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
857 if ( ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID )
859 bdn.bv_val[bdn.bv_len] = '\0';
860 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s malformed message (%s)\n",
861 si->si_ridtxt, bdn.bv_val, 0 );
862 ldap_controls_free( rctrls );
866 /* FIXME: what if syncUUID is NULL or empty?
867 * (happens with back-sql...) */
868 if ( BER_BVISEMPTY( &syncUUID ) ) {
869 bdn.bv_val[bdn.bv_len] = '\0';
870 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
871 "got empty syncUUID with LDAP_SYNC_%s (%s)\n",
873 syncrepl_state2str( syncstate ), bdn.bv_val );
874 ldap_controls_free( rctrls );
879 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
880 ber_scanf( ber, /*"{"*/ "m}", &cookie );
882 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
884 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
886 if ( !BER_BVISNULL( &cookie ) ) {
887 ch_free( syncCookie.octet_str.bv_val );
888 ber_dupbv( &syncCookie.octet_str, &cookie );
890 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
892 slap_parse_sync_cookie( &syncCookie, NULL );
893 if ( syncCookie.ctxcsn ) {
894 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
895 check_syncprov( op, si );
896 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
897 if ( si->si_cookieState->cs_sids[i] == sid ) {
898 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
899 bdn.bv_val[bdn.bv_len] = '\0';
900 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s (%s)\n",
901 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
902 ldap_controls_free( rctrls );
909 /* check pending CSNs too */
910 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
911 if ( slapd_shutdown ) {
915 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
916 ldap_pvt_thread_yield();
918 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
919 if ( si->si_cookieState->cs_psids[i] == sid ) {
920 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
921 bdn.bv_val[bdn.bv_len] = '\0';
922 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s (%s)\n",
923 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
924 ldap_controls_free( rctrls );
926 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
929 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
934 /* new SID, add it */
935 if ( i == si->si_cookieState->cs_pnum ) {
936 value_add( &si->si_cookieState->cs_pvals, syncCookie.ctxcsn );
937 si->si_cookieState->cs_pnum++;
938 si->si_cookieState->cs_psids = ch_realloc( si->si_cookieState->cs_psids, si->si_cookieState->cs_pnum * sizeof(int));
939 si->si_cookieState->cs_psids[i] = sid;
941 assert( punlock < 0 );
944 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
948 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
950 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
953 rc = syncrepl_updateCookie( si, op, &syncCookie );
954 } else switch ( rc ) {
955 case LDAP_ALREADY_EXISTS:
956 case LDAP_NO_SUCH_OBJECT:
957 case LDAP_NO_SUCH_ATTRIBUTE:
958 case LDAP_TYPE_OR_VALUE_EXISTS:
959 rc = LDAP_SYNC_REFRESH_REQUIRED;
960 si->si_logstate = SYNCLOG_FALLBACK;
961 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
962 bdn.bv_val[bdn.bv_len] = '\0';
963 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync on (%s), switching to REFRESH\n",
964 si->si_ridtxt, bdn.bv_val, 0 );
965 if (si->si_strict_refresh) {
966 slap_suspend_listeners();
973 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
974 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
976 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
977 syncstate, &syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
980 rc = syncrepl_updateCookie( si, op, &syncCookie );
983 if ( punlock >= 0 ) {
984 /* on failure, revert pending CSN */
985 if ( rc != LDAP_SUCCESS ) {
987 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
988 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
989 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
990 &si->si_cookieState->cs_vals[i] );
994 if ( i == si->si_cookieState->cs_num )
995 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
997 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
999 ldap_controls_free( rctrls );
1001 slap_mods_free( modlist, 1 );
1007 case LDAP_RES_SEARCH_REFERENCE:
1008 Debug( LDAP_DEBUG_ANY,
1009 "do_syncrep2: %s reference received error\n",
1010 si->si_ridtxt, 0, 0 );
1013 case LDAP_RES_SEARCH_RESULT:
1014 Debug( LDAP_DEBUG_SYNC,
1015 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
1016 si->si_ridtxt, 0, 0 );
1017 err = LDAP_OTHER; /* FIXME check parse result properly */
1018 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
1020 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
1021 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
1022 /* map old result code to registered code */
1023 err = LDAP_SYNC_REFRESH_REQUIRED;
1026 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
1027 if ( si->si_logstate == SYNCLOG_LOGGING ) {
1028 si->si_logstate = SYNCLOG_FALLBACK;
1029 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync, switching to REFRESH\n",
1030 si->si_ridtxt, 0, 0 );
1031 if (si->si_strict_refresh) {
1032 slap_suspend_listeners();
1040 Debug( LDAP_DEBUG_ANY,
1041 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1042 si->si_ridtxt, err, ldap_err2string( err ) );
1045 LDAPControl **next = NULL;
1046 /* NOTE: make sure we use the right one;
1047 * a better approach would be to run thru
1048 * the whole list and take care of all */
1049 /* NOTE: since we issue the search request,
1050 * we should know what controls to expect,
1051 * and there should be none apart from the
1052 * sync-related control */
1053 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1054 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1056 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1057 "got search result with multiple "
1058 "Sync State control\n", si->si_ridtxt, 0, 0 );
1059 ldap_controls_free( rctrls );
1065 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1067 ber_scanf( ber, "{" /*"}"*/);
1068 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1069 ber_scanf( ber, "m", &cookie );
1071 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1073 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1075 if ( !BER_BVISNULL( &cookie ) ) {
1076 ch_free( syncCookie.octet_str.bv_val );
1077 ber_dupbv( &syncCookie.octet_str, &cookie);
1079 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1081 slap_parse_sync_cookie( &syncCookie, NULL );
1082 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1085 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1087 ber_scanf( ber, "b", &refreshDeletes );
1089 ber_scanf( ber, /*"{"*/ "}" );
1091 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1092 slap_sync_cookie_free( &syncCookie_req, 0 );
1093 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1095 if ( !syncCookie.ctxcsn ) {
1097 } else if ( !syncCookie_req.ctxcsn ) {
1101 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1104 ldap_controls_free( rctrls );
1106 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1107 /* FIXME : different error behaviors according to
1108 * 1) err code : LDAP_BUSY ...
1109 * 2) on err policy : stop service, stop sync, retry
1111 if ( refreshDeletes == 0 && match < 0 &&
1112 err == LDAP_SUCCESS &&
1113 syncCookie_req.numcsns == syncCookie.numcsns )
1115 syncrepl_del_nonpresent( op, si, NULL,
1118 avl_free( si->si_presentlist, ch_free );
1119 si->si_presentlist = NULL;
1122 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1124 rc = syncrepl_updateCookie( si, op, &syncCookie );
1126 if ( err == LDAP_SUCCESS
1127 && si->si_logstate == SYNCLOG_FALLBACK ) {
1128 si->si_logstate = SYNCLOG_LOGGING;
1129 rc = LDAP_SYNC_REFRESH_REQUIRED;
1130 slap_resume_listeners();
1136 case LDAP_RES_INTERMEDIATE:
1139 rc = ldap_parse_intermediate( si->si_ld, msg,
1140 &retoid, &retdata, NULL, 0 );
1141 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1142 ber_init2( ber, retdata, LBER_USE_DER );
1144 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1146 case LDAP_TAG_SYNC_NEW_COOKIE:
1147 Debug( LDAP_DEBUG_SYNC,
1148 "do_syncrep2: %s %s - %s\n",
1150 "LDAP_RES_INTERMEDIATE",
1152 ber_scanf( ber, "tm", &tag, &cookie );
1153 Debug( LDAP_DEBUG_SYNC,
1154 "do_syncrep2: %s NEW_COOKIE: %s\n",
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 ) ) {
1162 slap_parse_sync_cookie( &syncCookie, NULL );
1163 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1166 case LDAP_TAG_SYNC_REFRESH_DELETE:
1167 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1168 Debug( LDAP_DEBUG_SYNC,
1169 "do_syncrep2: %s %s - %s\n",
1171 "LDAP_RES_INTERMEDIATE",
1172 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1173 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1174 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1175 si->si_refreshDelete = 1;
1177 si->si_refreshPresent = 1;
1179 ber_scanf( ber, "t{" /*"}"*/, &tag );
1180 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1182 ber_scanf( ber, "m", &cookie );
1184 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1186 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1188 if ( !BER_BVISNULL( &cookie ) ) {
1189 ch_free( syncCookie.octet_str.bv_val );
1190 ber_dupbv( &syncCookie.octet_str, &cookie );
1192 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1194 slap_parse_sync_cookie( &syncCookie, NULL );
1195 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1198 /* Defaults to TRUE */
1199 if ( ber_peek_tag( ber, &len ) ==
1200 LDAP_TAG_REFRESHDONE )
1202 ber_scanf( ber, "b", &si->si_refreshDone );
1205 si->si_refreshDone = 1;
1207 ber_scanf( ber, /*"{"*/ "}" );
1209 case LDAP_TAG_SYNC_ID_SET:
1210 Debug( LDAP_DEBUG_SYNC,
1211 "do_syncrep2: %s %s - %s\n",
1213 "LDAP_RES_INTERMEDIATE",
1215 ber_scanf( ber, "t{" /*"}"*/, &tag );
1216 if ( ber_peek_tag( ber, &len ) ==
1217 LDAP_TAG_SYNC_COOKIE )
1219 ber_scanf( ber, "m", &cookie );
1221 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1223 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1225 if ( !BER_BVISNULL( &cookie ) ) {
1226 ch_free( syncCookie.octet_str.bv_val );
1227 ber_dupbv( &syncCookie.octet_str, &cookie );
1229 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1231 slap_parse_sync_cookie( &syncCookie, NULL );
1232 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1233 compare_csns( &syncCookie_req, &syncCookie, &m );
1236 if ( ber_peek_tag( ber, &len ) ==
1237 LDAP_TAG_REFRESHDELETES )
1239 ber_scanf( ber, "b", &refreshDeletes );
1242 ber_scanf( ber, "[W]", &syncUUIDs );
1243 ber_scanf( ber, /*"{"*/ "}" );
1244 if ( refreshDeletes ) {
1245 syncrepl_del_nonpresent( op, si, syncUUIDs,
1247 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1250 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1251 (void)avl_presentlist_insert( si, &syncUUIDs[i] );
1252 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1254 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1256 slap_sync_cookie_free( &syncCookie, 0 );
1259 Debug( LDAP_DEBUG_ANY,
1260 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1261 si->si_ridtxt, (long) si_tag, 0 );
1262 ldap_memfree( retoid );
1263 ber_bvfree( retdata );
1267 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1268 slap_sync_cookie_free( &syncCookie_req, 0 );
1269 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1271 if ( !syncCookie.ctxcsn ) {
1273 } else if ( !syncCookie_req.ctxcsn ) {
1277 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1281 if ( si->si_refreshPresent == 1 &&
1282 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1283 syncCookie_req.numcsns == syncCookie.numcsns ) {
1284 syncrepl_del_nonpresent( op, si, NULL,
1288 if ( syncCookie.ctxcsn )
1290 rc = syncrepl_updateCookie( si, op, &syncCookie);
1294 ldap_memfree( retoid );
1295 ber_bvfree( retdata );
1298 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1299 "unknown intermediate response (%d)\n",
1300 si->si_ridtxt, rc, 0 );
1301 ldap_memfree( retoid );
1302 ber_bvfree( retdata );
1307 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1308 "unknown message (0x%02lx)\n",
1310 (unsigned long)ldap_msgtype( msg ), 0 );
1314 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1315 slap_sync_cookie_free( &syncCookie_req, 0 );
1316 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1317 slap_sync_cookie_free( &syncCookie, 0 );
1319 ldap_msgfree( msg );
1321 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1322 slap_sync_cookie_free( &syncCookie, 0 );
1323 slap_sync_cookie_free( &syncCookie_req, 0 );
1330 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1335 if ( err != LDAP_SUCCESS ) {
1336 Debug( LDAP_DEBUG_ANY,
1337 "do_syncrep2: %s (%d) %s\n",
1338 si->si_ridtxt, err, ldap_err2string( err ) );
1341 slap_sync_cookie_free( &syncCookie, 0 );
1342 slap_sync_cookie_free( &syncCookie_req, 0 );
1344 if ( msg ) ldap_msgfree( msg );
1346 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1347 if ( si->si_conn ) {
1348 connection_client_stop( si->si_conn );
1351 ldap_unbind_ext( si->si_ld, NULL, NULL );
1363 struct re_s* rtask = arg;
1364 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1365 Connection conn = {0};
1366 OperationBuffer opbuf;
1368 int rc = LDAP_SUCCESS;
1371 int i, defer = 1, fail = 0, freeinfo = 0;
1376 if ( slapd_shutdown )
1379 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1381 /* Don't get stuck here while a pause is initiated */
1382 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1383 if ( slapd_shutdown )
1385 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1386 ldap_pvt_thread_yield();
1389 if ( si->si_ctype < 1 ) {
1393 switch( abs( si->si_type ) ) {
1394 case LDAP_SYNC_REFRESH_ONLY:
1395 case LDAP_SYNC_REFRESH_AND_PERSIST:
1398 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1402 if ( slapd_shutdown ) {
1404 if ( si->si_conn ) {
1405 connection_client_stop( si->si_conn );
1408 ldap_unbind_ext( si->si_ld, NULL, NULL );
1411 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1415 connection_fake_init( &conn, &opbuf, ctx );
1417 /* o_connids must be unique for slap_graduate_commit_csn */
1418 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1420 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1423 /* Coordinate contextCSN updates with any syncprov overlays
1424 * in use. This may be complicated by the use of the glue
1427 * Typically there is a single syncprov mastering the entire
1428 * glued tree. In that case, our contextCSN updates should
1429 * go to the master DB. But if there is no syncprov on the
1430 * master DB, then nothing special is needed here.
1432 * Alternatively, there may be individual syncprov overlays
1433 * on each glued branch. In that case, each syncprov only
1434 * knows about changes within its own branch. And so our
1435 * contextCSN updates should only go to the local DB.
1437 if ( !si->si_wbe ) {
1438 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1439 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1440 if ( overlay_is_inst( top_be, "syncprov" ))
1441 si->si_wbe = top_be;
1447 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1448 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1449 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1451 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1454 if ( !si->si_schemachecking )
1455 op->o_no_schema_check = 1;
1457 /* Establish session, do search */
1459 si->si_refreshDelete = 0;
1460 si->si_refreshPresent = 0;
1462 if ( si->si_presentlist ) {
1463 avl_free( si->si_presentlist, ch_free );
1464 si->si_presentlist = NULL;
1467 /* use main DB when retrieving contextCSN */
1468 op->o_bd = si->si_wbe;
1469 op->o_dn = op->o_bd->be_rootdn;
1470 op->o_ndn = op->o_bd->be_rootndn;
1471 rc = do_syncrep1( op, si );
1475 /* Process results */
1476 if ( rc == LDAP_SUCCESS ) {
1477 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1479 /* use current DB */
1481 op->o_dn = op->o_bd->be_rootdn;
1482 op->o_ndn = op->o_bd->be_rootndn;
1483 rc = do_syncrep2( op, si );
1484 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1485 rc = ldap_sync_search( si, op->o_tmpmemctx );
1490 /* We got deleted while running on cn=config */
1491 if ( si->si_ctype < 1 ) {
1492 if ( si->si_ctype == -1 ) {
1501 if ( rc != SYNC_PAUSED ) {
1502 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1503 /* If we succeeded, enable the connection for further listening.
1504 * If we failed, tear down the connection and reschedule.
1506 if ( rc == LDAP_SUCCESS ) {
1507 if ( si->si_conn ) {
1508 connection_client_enable( si->si_conn );
1510 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1512 } else if ( si->si_conn ) {
1516 if ( rc == -2 ) rc = 0;
1521 /* At this point, we have 5 cases:
1522 * 1) for any hard failure, give up and remove this task
1523 * 2) for ServerDown, reschedule this task to run later
1524 * 3) for threadpool pause, reschedule to run immediately
1525 * 4) for Refresh and Success, reschedule to run
1526 * 5) for Persist and Success, reschedule to defer
1528 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1530 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1531 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1535 connection_client_stop( si->si_conn );
1539 if ( rc == SYNC_PAUSED ) {
1540 rtask->interval.tv_sec = 0;
1541 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1542 rtask->interval.tv_sec = si->si_interval;
1544 } else if ( rc == LDAP_SUCCESS ) {
1545 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1548 rtask->interval.tv_sec = si->si_interval;
1549 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1550 if ( si->si_retrynum ) {
1551 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1552 si->si_retrynum[i] = si->si_retrynum_init[i];
1554 si->si_retrynum[i] = RETRYNUM_TAIL;
1557 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1558 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1562 if ( si->si_ctype < 1
1563 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1565 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1568 fail = RETRYNUM_TAIL;
1569 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1570 if ( si->si_retrynum[i] > 0 )
1571 si->si_retrynum[i]--;
1572 fail = si->si_retrynum[i];
1573 rtask->interval.tv_sec = si->si_retryinterval[i];
1574 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1575 slap_wake_listener();
1579 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1580 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1583 if ( fail == RETRYNUM_TAIL ) {
1584 Debug( LDAP_DEBUG_ANY,
1585 "do_syncrepl: %s rc %d quitting\n",
1586 si->si_ridtxt, rc, 0 );
1587 } else if ( fail > 0 ) {
1588 Debug( LDAP_DEBUG_ANY,
1589 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1590 si->si_ridtxt, rc, fail );
1592 Debug( LDAP_DEBUG_ANY,
1593 "do_syncrepl: %s rc %d retrying\n",
1594 si->si_ridtxt, rc, 0 );
1598 /* Do final delete cleanup */
1600 syncinfo_free( si, 0 );
1605 #ifdef ENABLE_REWRITE
1607 syncrepl_rewrite_dn(
1610 struct berval *sdn )
1615 nul = dn->bv_val[dn->bv_len];
1616 dn->bv_val[dn->bv_len] = 0;
1617 rc = rewrite( si->si_rewrite, SUFFIXM_CTX, dn->bv_val, &sdn->bv_val );
1618 dn->bv_val[dn->bv_len] = nul;
1620 if ( sdn->bv_val == dn->bv_val )
1622 else if ( rc == REWRITE_REGEXEC_OK && sdn->bv_val )
1623 sdn->bv_len = strlen( sdn->bv_val );
1626 #define REWRITE_VAL(si, ad, bv, bv2) \
1627 BER_BVZERO( &bv2 ); \
1628 if ( si->si_rewrite && ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName) \
1629 syncrepl_rewrite_dn( si, &bv, &bv2); \
1630 if ( BER_BVISNULL( &bv2 )) \
1631 ber_dupbv( &bv2, &bv )
1632 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1633 BER_BVZERO( &bv2 ); \
1634 if (si->si_rewrite) \
1635 syncrepl_rewrite_dn(si, &bv, &bv2); \
1636 rc = dnPrettyNormal( NULL, bv2.bv_val ? &bv2 : &bv, &dn, &ndn, op->o_tmpmemctx ); \
1639 #define REWRITE_VAL(si, ad, bv, bv2) ber_dupbv(&bv2, &bv)
1640 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1641 rc = dnPrettyNormal( NULL, &bv, &dn, &ndn, op->o_tmpmemctx )
1645 static slap_verbmasks modops[] = {
1646 { BER_BVC("add"), LDAP_REQ_ADD },
1647 { BER_BVC("delete"), LDAP_REQ_DELETE },
1648 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1649 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1654 syncrepl_accesslog_mods(
1656 struct berval *vals,
1657 struct Modifications **modres
1662 AttributeDescription *ad;
1663 struct berval bv, bv2;
1665 Modifications *mod = NULL, *modlist = NULL, **modtail;
1670 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1674 colon = ber_bvchr( &bv, ':' );
1680 bv.bv_len = colon - bv.bv_val;
1681 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1683 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1684 "Invalid attribute %s, %s\n",
1685 si->si_ridtxt, bv.bv_val, text );
1686 slap_mods_free( modlist, 1 );
1692 /* Ignore dynamically generated attrs */
1693 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1697 /* Ignore excluded attrs */
1698 if ( ldap_charray_inlist( si->si_exattrs,
1699 ad->ad_type->sat_cname.bv_val ) )
1705 case '+': op = LDAP_MOD_ADD; break;
1706 case '-': op = LDAP_MOD_DELETE; break;
1707 case '=': op = LDAP_MOD_REPLACE; break;
1708 case '#': op = LDAP_MOD_INCREMENT; break;
1712 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1713 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1716 mod->sml_next = NULL;
1718 mod->sml_type = ad->ad_cname;
1719 mod->sml_values = NULL;
1720 mod->sml_nvalues = NULL;
1721 mod->sml_numvals = 0;
1724 modtail = &mod->sml_next;
1726 if ( colon[2] == ' ' ) {
1727 bv.bv_val = colon + 3;
1728 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1729 REWRITE_VAL( si, ad, bv, bv2 );
1730 ber_bvarray_add( &mod->sml_values, &bv2 );
1739 syncrepl_changelog_mods(
1741 struct berval *vals,
1742 struct Modifications **modres
1745 return -1; /* FIXME */
1749 syncrepl_message_to_op(
1755 BerElement *ber = NULL;
1756 Modifications *modlist = NULL;
1758 SlapReply rs = { REP_RESULT };
1759 slap_callback cb = { NULL, null_callback, NULL, NULL };
1762 char txtbuf[SLAP_TEXT_BUFLEN];
1763 size_t textlen = sizeof txtbuf;
1765 struct berval bdn, dn = BER_BVNULL, ndn;
1766 struct berval bv, bv2, *bvals = NULL;
1767 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1768 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1769 psup = BER_BVNULL, nsup = BER_BVNULL;
1770 int rc, deleteOldRdn = 0, freeReqDn = 0;
1771 int do_graduate = 0;
1773 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1774 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1775 "Message type should be entry (%d)",
1776 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1780 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1785 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1787 if ( rc != LDAP_SUCCESS ) {
1788 Debug( LDAP_DEBUG_ANY,
1789 "syncrepl_message_to_op: %s dn get failed (%d)",
1790 si->si_ridtxt, rc, 0 );
1794 op->o_tag = LBER_DEFAULT;
1795 op->o_bd = si->si_wbe;
1797 if ( BER_BVISEMPTY( &bdn )) {
1798 Debug( LDAP_DEBUG_ANY,
1799 "syncrepl_message_to_op: %s got empty dn",
1800 si->si_ridtxt, 0, 0 );
1804 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
1806 if ( bv.bv_val == NULL )
1809 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
1811 REWRITE_DN( si, bdn, bv2, dn, ndn );
1812 if ( rc != LDAP_SUCCESS ) {
1813 Debug( LDAP_DEBUG_ANY,
1814 "syncrepl_message_to_op: %s "
1815 "dn \"%s\" normalization failed (%d)",
1816 si->si_ridtxt, bdn.bv_val, rc );
1821 ber_dupbv( &op->o_req_dn, &dn );
1822 ber_dupbv( &op->o_req_ndn, &ndn );
1823 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1824 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1826 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
1827 int i = verb_to_mask( bvals[0].bv_val, modops );
1829 Debug( LDAP_DEBUG_ANY,
1830 "syncrepl_message_to_op: %s unknown op %s",
1831 si->si_ridtxt, bvals[0].bv_val, 0 );
1836 op->o_tag = modops[i].mask;
1837 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
1838 /* Parse attribute into modlist */
1839 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
1840 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
1842 rc = syncrepl_changelog_mods( si, bvals, &modlist );
1844 if ( rc ) goto done;
1845 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
1847 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
1848 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
1851 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
1853 } else if ( !ber_bvstrcasecmp( &bv,
1854 &slap_schema.si_ad_entryCSN->ad_cname ) )
1856 slap_queue_csn( op, bvals );
1862 /* If we didn't get a mod type or a target DN, bail out */
1863 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
1868 op->o_callback = &cb;
1869 slap_op_time( &op->o_time, &op->o_tincr );
1871 switch( op->o_tag ) {
1873 case LDAP_REQ_MODIFY:
1874 /* If we didn't get required data, bail */
1875 if ( !modlist ) goto done;
1877 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
1879 if ( rc != LDAP_SUCCESS ) {
1880 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1881 "mods check (%s)\n",
1882 si->si_ridtxt, text, 0 );
1886 if ( op->o_tag == LDAP_REQ_ADD ) {
1887 Entry *e = entry_alloc();
1889 op->ora_e->e_name = op->o_req_dn;
1890 op->ora_e->e_nname = op->o_req_ndn;
1892 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1893 if( rc != LDAP_SUCCESS ) {
1894 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1895 "mods2entry (%s)\n",
1896 si->si_ridtxt, text, 0 );
1898 rc = op->o_bd->be_add( op, &rs );
1899 Debug( LDAP_DEBUG_SYNC,
1900 "syncrepl_message_to_op: %s be_add %s (%d)\n",
1901 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1904 if ( e == op->ora_e )
1905 be_entry_release_w( op, op->ora_e );
1907 op->orm_modlist = modlist;
1908 op->o_bd = si->si_wbe;
1909 rc = op->o_bd->be_modify( op, &rs );
1910 modlist = op->orm_modlist;
1911 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1912 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
1913 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1914 op->o_bd = si->si_be;
1918 case LDAP_REQ_MODRDN:
1919 if ( BER_BVISNULL( &rdn ) ) goto done;
1921 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
1924 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
1927 if ( !BER_BVISNULL( &sup ) ) {
1928 REWRITE_DN( si, sup, bv2, psup, nsup );
1931 op->orr_newSup = &psup;
1932 op->orr_nnewSup = ⊅
1934 op->orr_newSup = NULL;
1935 op->orr_nnewSup = NULL;
1937 op->orr_newrdn = prdn;
1938 op->orr_nnewrdn = nrdn;
1939 op->orr_deleteoldrdn = deleteOldRdn;
1940 op->orr_modlist = NULL;
1941 if ( slap_modrdn2mods( op, &rs ) ) {
1945 /* Append modlist for operational attrs */
1949 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
1951 m->sml_next = modlist;
1954 rc = op->o_bd->be_modrdn( op, &rs );
1955 slap_mods_free( op->orr_modlist, 1 );
1956 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1957 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
1958 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1961 case LDAP_REQ_DELETE:
1962 rc = op->o_bd->be_delete( op, &rs );
1963 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1964 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
1965 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1971 slap_graduate_commit_csn( op );
1972 op->o_bd = si->si_be;
1973 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1974 BER_BVZERO( &op->o_csn );
1976 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1978 if ( !BER_BVISNULL( &rdn ) ) {
1979 if ( !BER_BVISNULL( &nsup ) ) {
1980 ch_free( nsup.bv_val );
1982 if ( !BER_BVISNULL( &psup ) ) {
1983 ch_free( psup.bv_val );
1985 if ( !BER_BVISNULL( &nrdn ) ) {
1986 ch_free( nrdn.bv_val );
1988 if ( !BER_BVISNULL( &prdn ) ) {
1989 ch_free( prdn.bv_val );
1993 ch_free( op->o_req_ndn.bv_val );
1994 ch_free( op->o_req_dn.bv_val );
2001 syncrepl_message_to_entry(
2005 Modifications **modlist,
2011 BerElement *ber = NULL;
2014 Modifications **modtail = modlist;
2017 char txtbuf[SLAP_TEXT_BUFLEN];
2018 size_t textlen = sizeof txtbuf;
2020 struct berval bdn = BER_BVNULL, dn, ndn, bv2;
2025 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2026 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
2027 "Message type should be entry (%d)",
2028 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2032 op->o_tag = LDAP_REQ_ADD;
2034 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2035 if ( rc != LDAP_SUCCESS ) {
2036 Debug( LDAP_DEBUG_ANY,
2037 "syncrepl_message_to_entry: %s dn get failed (%d)",
2038 si->si_ridtxt, rc, 0 );
2042 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
2043 Debug( LDAP_DEBUG_ANY,
2044 "syncrepl_message_to_entry: %s got empty dn",
2045 si->si_ridtxt, 0, 0 );
2049 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
2050 /* NOTE: this could be done even before decoding the DN,
2051 * although encoding errors wouldn't be detected */
2056 if ( entry == NULL ) {
2060 REWRITE_DN( si, bdn, bv2, dn, ndn );
2061 if ( rc != LDAP_SUCCESS ) {
2062 /* One of the things that could happen is that the schema
2063 * is not lined-up; this could result in unknown attributes.
2064 * A value non conformant to the syntax should be unlikely,
2065 * except when replicating between different versions
2066 * of the software, or when syntax validation bugs are fixed
2068 Debug( LDAP_DEBUG_ANY,
2069 "syncrepl_message_to_entry: "
2070 "%s dn \"%s\" normalization failed (%d)",
2071 si->si_ridtxt, bdn.bv_val, rc );
2075 ber_dupbv( &op->o_req_dn, &dn );
2076 ber_dupbv( &op->o_req_ndn, &ndn );
2077 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2078 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2080 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
2083 e->e_name = op->o_req_dn;
2084 e->e_nname = op->o_req_ndn;
2086 while ( ber_remaining( ber ) ) {
2087 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
2088 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2093 /* Drop all updates to the contextCSN of the context entry
2096 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2097 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2098 ber_bvarray_free( tmp.sml_values );
2102 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2104 mod->sml_op = LDAP_MOD_REPLACE;
2106 mod->sml_next = NULL;
2107 mod->sml_desc = NULL;
2108 mod->sml_type = tmp.sml_type;
2109 mod->sml_values = tmp.sml_values;
2110 mod->sml_nvalues = NULL;
2111 mod->sml_numvals = 0; /* slap_mods_check will set this */
2113 #ifdef ENABLE_REWRITE
2114 if (si->si_rewrite) {
2115 AttributeDescription *ad = NULL;
2116 slap_bv2ad( &tmp.sml_type, &ad, &text );
2119 mod->sml_type = ad->ad_cname;
2120 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
2122 for ( i = 0; tmp.sml_values[i].bv_val; i++ ) {
2123 syncrepl_rewrite_dn( si, &tmp.sml_values[i], &bv2);
2124 if ( !BER_BVISNULL( &bv2 )) {
2125 ber_memfree( tmp.sml_values[i].bv_val );
2126 tmp.sml_values[i] = bv2;
2134 modtail = &mod->sml_next;
2137 if ( *modlist == NULL ) {
2138 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2139 si->si_ridtxt, 0, 0 );
2144 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2146 if ( rc != LDAP_SUCCESS ) {
2147 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2148 si->si_ridtxt, text, 0 );
2152 /* Strip out dynamically generated attrs */
2153 for ( modtail = modlist; *modtail ; ) {
2155 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2156 *modtail = mod->sml_next;
2157 slap_mod_free( &mod->sml_mod, 0 );
2160 modtail = &mod->sml_next;
2164 /* Strip out attrs in exattrs list */
2165 for ( modtail = modlist; *modtail ; ) {
2167 if ( ldap_charray_inlist( si->si_exattrs,
2168 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2170 *modtail = mod->sml_next;
2171 slap_mod_free( &mod->sml_mod, 0 );
2174 modtail = &mod->sml_next;
2178 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2179 if( rc != LDAP_SUCCESS ) {
2180 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2181 si->si_ridtxt, text, 0 );
2186 if ( rc != LDAP_SUCCESS ) {
2198 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2200 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2201 * entry if a previous refresh was interrupted before sending us a new
2202 * context state. We try to compare the new entry to the existing entry
2203 * and ignore the new entry if they are the same.
2205 * Also, we may get an update where the entryDN has changed, due to
2206 * a ModDn on the provider. We detect this as well, so we can issue
2207 * the corresponding operation locally.
2209 * In the case of a modify, we get a list of all the attributes
2210 * in the original entry. Rather than deleting the entry and re-adding it,
2211 * we issue a Modify request that deletes all the attributes and adds all
2212 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2215 * We otherwise distinguish ModDN from Modify; in the case of
2216 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2217 * operational attributes from the entry, and do a regular ModDN.
2219 typedef struct dninfo {
2223 struct berval nnewSup;
2224 int renamed; /* Was an existing entry renamed? */
2225 int delOldRDN; /* Was old RDN deleted? */
2226 Modifications **modlist; /* the modlist we received */
2227 Modifications *mods; /* the modlist we compared */
2228 Attribute *oldNattr; /* old naming attr */
2229 AttributeDescription *oldDesc; /* for renames */
2230 AttributeDescription *newDesc; /* for renames */
2233 /* return 1 if inserted, 0 otherwise */
2235 avl_presentlist_insert(
2237 struct berval *syncUUID )
2239 struct berval *syncuuid_bv = ch_malloc( sizeof( struct berval ) + syncUUID->bv_len + 1 );
2241 syncuuid_bv->bv_len = syncUUID->bv_len;
2242 syncuuid_bv->bv_val = (char *)&syncuuid_bv[1];
2243 AC_MEMCPY( syncuuid_bv->bv_val, syncUUID->bv_val, syncUUID->bv_len );
2244 syncuuid_bv->bv_val[ syncuuid_bv->bv_len ] = '\0';
2246 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
2247 syncuuid_cmp, avl_dup_error ) )
2249 ch_free( syncuuid_bv );
2261 Modifications** modlist,
2263 struct berval* syncUUID,
2264 struct berval* syncCSN )
2266 Backend *be = op->o_bd;
2267 slap_callback cb = { NULL, NULL, NULL, NULL };
2268 int syncuuid_inserted = 0;
2269 struct berval syncUUID_strrep = BER_BVNULL;
2271 SlapReply rs_search = {REP_RESULT};
2273 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2274 int rc = LDAP_SUCCESS;
2276 struct berval pdn = BER_BVNULL;
2281 Debug( LDAP_DEBUG_SYNC,
2282 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2283 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2285 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2286 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2287 syncuuid_inserted = avl_presentlist_insert( si, syncUUID );
2291 if ( syncstate == LDAP_SYNC_PRESENT ) {
2293 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2294 if ( entry == NULL ) {
2299 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
2300 if ( syncstate != LDAP_SYNC_DELETE ) {
2301 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2304 /* add if missing */
2305 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2306 &syncUUID_strrep, syncUUID );
2308 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2309 /* replace only if necessary */
2310 if ( a->a_nvals != a->a_vals ) {
2311 ber_memfree( a->a_nvals[0].bv_val );
2312 ber_dupbv( &a->a_nvals[0], syncUUID );
2314 ber_memfree( a->a_vals[0].bv_val );
2315 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
2319 f.f_choice = LDAP_FILTER_EQUALITY;
2321 ava.aa_desc = slap_schema.si_ad_entryUUID;
2322 ava.aa_value = *syncUUID;
2324 if ( syncuuid_inserted ) {
2325 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2326 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
2328 op->ors_filter = &f;
2330 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
2331 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2332 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2333 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2334 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2335 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
2336 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2337 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2339 op->o_tag = LDAP_REQ_SEARCH;
2340 op->ors_scope = LDAP_SCOPE_SUBTREE;
2341 op->ors_deref = LDAP_DEREF_NEVER;
2343 /* get the entry for this UUID */
2344 #ifdef ENABLE_REWRITE
2345 if ( si->si_rewrite ) {
2346 op->o_req_dn = si->si_suffixm;
2347 op->o_req_ndn = si->si_suffixm;
2351 op->o_req_dn = si->si_base;
2352 op->o_req_ndn = si->si_base;
2355 op->o_time = slap_get_time();
2356 op->ors_tlimit = SLAP_NO_LIMIT;
2358 op->ors_limit = NULL;
2360 op->ors_attrs = slap_anlist_all_attributes;
2361 op->ors_attrsonly = 0;
2363 /* set callback function */
2364 op->o_callback = &cb;
2365 cb.sc_response = dn_callback;
2366 cb.sc_private = &dni;
2367 dni.new_entry = entry;
2368 dni.modlist = modlist;
2370 rc = be->be_search( op, &rs_search );
2371 Debug( LDAP_DEBUG_SYNC,
2372 "syncrepl_entry: %s be_search (%d)\n",
2373 si->si_ridtxt, rc, 0 );
2375 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2376 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2379 cb.sc_response = null_callback;
2382 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2383 Debug( LDAP_DEBUG_SYNC,
2384 "syncrepl_entry: %s %s\n",
2385 si->si_ridtxt, entry->e_name.bv_val, 0 );
2387 Debug( LDAP_DEBUG_SYNC,
2388 "syncrepl_entry: %s %s\n",
2389 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2392 assert( BER_BVISNULL( &op->o_csn ) );
2394 slap_queue_csn( op, syncCSN );
2397 slap_op_time( &op->o_time, &op->o_tincr );
2398 switch ( syncstate ) {
2400 case LDAP_SYNC_MODIFY:
2401 if ( BER_BVISNULL( &op->o_csn ))
2404 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2406 /* FIXME: op->o_csn is assumed to be
2407 * on the thread's slab; this needs
2408 * to be cleared ASAP.
2409 * What happens if already present?
2411 assert( BER_BVISNULL( &op->o_csn ) );
2412 op->o_csn = a->a_vals[0];
2417 if ( BER_BVISNULL( &dni.dn ) ) {
2418 SlapReply rs_add = {REP_RESULT};
2420 op->o_req_dn = entry->e_name;
2421 op->o_req_ndn = entry->e_nname;
2422 op->o_tag = LDAP_REQ_ADD;
2424 op->o_bd = si->si_wbe;
2426 rc = op->o_bd->be_add( op, &rs_add );
2427 Debug( LDAP_DEBUG_SYNC,
2428 "syncrepl_entry: %s be_add %s (%d)\n",
2429 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2430 switch ( rs_add.sr_err ) {
2432 if ( op->ora_e == entry ) {
2433 be_entry_release_w( op, entry );
2439 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2440 * only if the suffix entry is not present */
2441 case LDAP_NO_SUCH_OBJECT:
2442 rc = syncrepl_add_glue( op, entry );
2446 /* if an entry was added via syncrepl_add_glue(),
2447 * it likely has no entryUUID, so the previous
2448 * be_search() doesn't find it. In this case,
2449 * give syncrepl a chance to modify it. Also
2450 * allow for entries that were recreated with the
2451 * same DN but a different entryUUID.
2453 case LDAP_ALREADY_EXISTS:
2455 Operation op2 = *op;
2456 SlapReply rs2 = { REP_RESULT };
2457 slap_callback cb2 = { 0 };
2460 op2.o_tag = LDAP_REQ_SEARCH;
2461 op2.o_req_dn = entry->e_name;
2462 op2.o_req_ndn = entry->e_nname;
2463 op2.ors_scope = LDAP_SCOPE_BASE;
2464 op2.ors_deref = LDAP_DEREF_NEVER;
2465 op2.ors_attrs = slap_anlist_all_attributes;
2466 op2.ors_attrsonly = 0;
2467 op2.ors_limit = NULL;
2469 op2.ors_tlimit = SLAP_NO_LIMIT;
2471 f.f_choice = LDAP_FILTER_PRESENT;
2472 f.f_desc = slap_schema.si_ad_objectClass;
2473 op2.ors_filter = &f;
2474 op2.ors_filterstr = generic_filterstr;
2476 op2.o_callback = &cb2;
2477 cb2.sc_response = dn_callback;
2478 cb2.sc_private = &dni;
2480 rc = be->be_search( &op2, &rs2 );
2481 if ( rc ) goto done;
2484 slap_op_time( &op->o_time, &op->o_tincr );
2490 Debug( LDAP_DEBUG_ANY,
2491 "syncrepl_entry: %s be_add %s failed (%d)\n",
2492 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
2500 op->o_req_dn = dni.dn;
2501 op->o_req_ndn = dni.ndn;
2502 if ( dni.renamed ) {
2503 struct berval noldp, newp;
2504 Modifications *mod, **modtail, **ml, *m2;
2505 int i, got_replace = 0, just_rename = 0;
2506 SlapReply rs_modify = {REP_RESULT};
2508 op->o_tag = LDAP_REQ_MODRDN;
2509 dnRdn( &entry->e_name, &op->orr_newrdn );
2510 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2512 if ( !BER_BVISNULL( &dni.nnewSup )) {
2513 dnParent( &entry->e_name, &newp );
2514 op->orr_newSup = &newp;
2515 op->orr_nnewSup = &dni.nnewSup;
2517 op->orr_newSup = NULL;
2518 op->orr_nnewSup = NULL;
2520 op->orr_deleteoldrdn = dni.delOldRDN;
2521 op->orr_modlist = NULL;
2522 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2526 /* Drop the RDN-related mods from this op, because their
2527 * equivalents were just setup by slap_modrdn2mods.
2529 * If delOldRDN is TRUE then we should see a delete modop
2530 * for oldDesc. We might see a replace instead.
2531 * delete with no values: therefore newDesc != oldDesc.
2532 * if oldNattr had only one value, then Drop this op.
2533 * delete with 1 value: can only be the oldRDN value. Drop op.
2534 * delete with N values: Drop oldRDN value, keep remainder.
2535 * replace with 1 value: if oldNattr had only one value and
2536 * newDesc == oldDesc, Drop this op.
2537 * Any other cases must be left intact.
2539 * We should also see an add modop for newDesc. (But not if
2540 * we got a replace modop due to delOldRDN.) If it has
2541 * multiple values, we'll have to drop the new RDN value.
2543 modtail = &op->orr_modlist;
2544 if ( dni.delOldRDN ) {
2545 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2546 if ( (*ml)->sml_desc == dni.oldDesc ) {
2548 if ( mod->sml_op == LDAP_MOD_REPLACE &&
2549 dni.oldDesc != dni.newDesc ) {
2550 /* This Replace is due to other Mods.
2555 if ( mod->sml_numvals <= 1 &&
2556 dni.oldNattr->a_numvals == 1 &&
2557 ( mod->sml_op == LDAP_MOD_DELETE ||
2558 mod->sml_op == LDAP_MOD_REPLACE )) {
2559 if ( mod->sml_op == LDAP_MOD_REPLACE )
2562 *ml = mod->sml_next;
2563 mod->sml_next = NULL;
2564 slap_mods_free( mod, 1 );
2567 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
2569 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2570 if ( m2->sml_desc == dni.oldDesc &&
2571 m2->sml_op == LDAP_MOD_DELETE ) break;
2573 for ( i=0; i<mod->sml_numvals; i++ ) {
2574 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2576 ch_free( mod->sml_values[i].bv_val );
2577 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2578 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2579 if ( mod->sml_nvalues ) {
2580 ch_free( mod->sml_nvalues[i].bv_val );
2581 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2582 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2587 if ( !mod->sml_numvals ) {
2589 *ml = mod->sml_next;
2590 mod->sml_next = NULL;
2591 slap_mods_free( mod, 1 );
2597 if ( !got_replace ) {
2598 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2599 if ( (*ml)->sml_desc == dni.newDesc ) {
2601 if ( mod->sml_op != LDAP_MOD_ADD )
2603 if ( mod->sml_numvals == 1 ) {
2605 *ml = mod->sml_next;
2606 mod->sml_next = NULL;
2607 slap_mods_free( mod, 1 );
2610 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2611 if ( m2->sml_desc == dni.oldDesc &&
2612 m2->sml_op == SLAP_MOD_SOFTADD ) break;
2614 for ( i=0; i<mod->sml_numvals; i++ ) {
2615 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2617 ch_free( mod->sml_values[i].bv_val );
2618 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2619 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2620 if ( mod->sml_nvalues ) {
2621 ch_free( mod->sml_nvalues[i].bv_val );
2622 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2623 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2633 /* RDNs must be NUL-terminated for back-ldap */
2634 noldp = op->orr_newrdn;
2635 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
2636 noldp = op->orr_nnewrdn;
2637 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
2639 /* Setup opattrs too */
2641 static AttributeDescription *nullattr = NULL;
2642 static AttributeDescription **const opattrs[] = {
2643 &slap_schema.si_ad_entryCSN,
2644 &slap_schema.si_ad_modifiersName,
2645 &slap_schema.si_ad_modifyTimestamp,
2648 AttributeDescription *opattr;
2652 /* pull mod off incoming modlist */
2653 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
2654 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
2656 if ( (*ml)->sml_desc == opattr ) {
2658 *ml = mod->sml_next;
2659 mod->sml_next = NULL;
2661 modtail = &mod->sml_next;
2666 /* If there are still Modifications left, put the opattrs
2667 * back, and let be_modify run. Otherwise, append the opattrs
2668 * to the orr_modlist.
2672 /* don't set a CSN for the rename op */
2674 slap_graduate_commit_csn( op );
2676 mod = op->orr_modlist;
2679 for ( ; mod->sml_next; mod=mod->sml_next );
2682 op->o_bd = si->si_wbe;
2684 rs_reinit( &rs_modify, REP_RESULT );
2685 rc = op->o_bd->be_modrdn( op, &rs_modify );
2687 /* NOTE: noSuchObject should result because the new superior
2688 * has not been added yet (ITS#6472) */
2689 if ( rc == LDAP_NO_SUCH_OBJECT && !BER_BVISNULL( op->orr_nnewSup )) {
2690 Operation op2 = *op;
2691 rc = syncrepl_add_glue_ancestors( &op2, entry );
2692 if ( rc == LDAP_SUCCESS ) {
2697 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
2698 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
2700 slap_mods_free( op->orr_modlist, 1 );
2701 Debug( LDAP_DEBUG_SYNC,
2702 "syncrepl_entry: %s be_modrdn %s (%d)\n",
2703 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2705 /* Renamed entries may still have other mods so just fallthru */
2706 op->o_req_dn = entry->e_name;
2707 op->o_req_ndn = entry->e_nname;
2708 /* Use CSN on the modify */
2712 slap_queue_csn( op, syncCSN );
2715 SlapReply rs_modify = {REP_RESULT};
2717 op->o_tag = LDAP_REQ_MODIFY;
2718 op->orm_modlist = dni.mods;
2719 op->orm_no_opattrs = 1;
2720 op->o_bd = si->si_wbe;
2722 rc = op->o_bd->be_modify( op, &rs_modify );
2723 slap_mods_free( op->orm_modlist, 1 );
2724 op->orm_no_opattrs = 0;
2725 Debug( LDAP_DEBUG_SYNC,
2726 "syncrepl_entry: %s be_modify %s (%d)\n",
2727 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2728 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2729 Debug( LDAP_DEBUG_ANY,
2730 "syncrepl_entry: %s be_modify failed (%d)\n",
2731 si->si_ridtxt, rs_modify.sr_err, 0 );
2735 } else if ( !dni.renamed ) {
2736 Debug( LDAP_DEBUG_SYNC,
2737 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
2738 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
2740 slap_graduate_commit_csn( op );
2745 case LDAP_SYNC_DELETE :
2746 if ( !BER_BVISNULL( &dni.dn ) ) {
2747 SlapReply rs_delete = {REP_RESULT};
2748 op->o_req_dn = dni.dn;
2749 op->o_req_ndn = dni.ndn;
2750 op->o_tag = LDAP_REQ_DELETE;
2751 op->o_bd = si->si_wbe;
2752 rc = op->o_bd->be_delete( op, &rs_delete );
2753 Debug( LDAP_DEBUG_SYNC,
2754 "syncrepl_entry: %s be_delete %s (%d)\n",
2755 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2757 while ( rs_delete.sr_err == LDAP_SUCCESS
2758 && op->o_delete_glue_parent ) {
2759 op->o_delete_glue_parent = 0;
2760 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2761 slap_callback cb = { NULL };
2762 cb.sc_response = slap_null_cb;
2763 dnParent( &op->o_req_ndn, &pdn );
2765 op->o_req_ndn = pdn;
2766 op->o_callback = &cb;
2767 rs_reinit( &rs_delete, REP_RESULT );
2768 op->o_bd->be_delete( op, &rs_delete );
2779 Debug( LDAP_DEBUG_ANY,
2780 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
2785 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2786 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2787 BER_BVZERO( &syncUUID_strrep );
2789 if ( !BER_BVISNULL( &dni.ndn ) ) {
2790 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2792 if ( !BER_BVISNULL( &dni.dn ) ) {
2793 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2796 entry_free( entry );
2799 slap_graduate_commit_csn( op );
2801 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
2802 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2804 BER_BVZERO( &op->o_csn );
2808 static struct berval gcbva[] = {
2814 #define NP_DELETE_ONE 2
2817 syncrepl_del_nonpresent(
2821 struct sync_cookie *sc,
2824 Backend* be = op->o_bd;
2825 slap_callback cb = { NULL };
2826 struct nonpresent_entry *np_list, *np_prev;
2828 AttributeName an[2];
2830 struct berval pdn = BER_BVNULL;
2833 #ifdef ENABLE_REWRITE
2834 if ( si->si_rewrite ) {
2835 op->o_req_dn = si->si_suffixm;
2836 op->o_req_ndn = si->si_suffixm;
2840 op->o_req_dn = si->si_base;
2841 op->o_req_ndn = si->si_base;
2844 cb.sc_response = nonpresent_callback;
2847 op->o_callback = &cb;
2848 op->o_tag = LDAP_REQ_SEARCH;
2849 op->ors_scope = si->si_scope;
2850 op->ors_deref = LDAP_DEREF_NEVER;
2851 op->o_time = slap_get_time();
2852 op->ors_tlimit = SLAP_NO_LIMIT;
2857 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
2860 op->ors_attrsonly = 1;
2861 op->ors_attrs = slap_anlist_no_attrs;
2862 op->ors_limit = NULL;
2863 op->ors_filter = &uf;
2866 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2868 uf.f_choice = LDAP_FILTER_EQUALITY;
2869 si->si_refreshDelete |= NP_DELETE_ONE;
2871 for (i=0; uuids[i].bv_val; i++) {
2872 SlapReply rs_search = {REP_RESULT};
2875 uf.f_av_value = uuids[i];
2876 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2877 rc = be->be_search( op, &rs_search );
2878 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2880 si->si_refreshDelete ^= NP_DELETE_ONE;
2884 AttributeAssertion mmaa;
2885 SlapReply rs_search = {REP_RESULT};
2887 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2888 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2889 an[0].an_desc = slap_schema.si_ad_entryUUID;
2891 op->ors_slimit = SLAP_NO_LIMIT;
2892 op->ors_tlimit = SLAP_NO_LIMIT;
2893 op->ors_limit = NULL;
2894 op->ors_attrsonly = 0;
2895 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
2896 /* In multimaster, updates can continue to arrive while
2897 * we're searching. Limit the search result to entries
2898 * older than our newest cookie CSN.
2900 if ( SLAP_MULTIMASTER( op->o_bd )) {
2905 f->f_choice = LDAP_FILTER_AND;
2906 f->f_next = op->ors_filter;
2910 f->f_choice = LDAP_FILTER_LE;
2912 f->f_av_desc = slap_schema.si_ad_entryCSN;
2914 BER_BVZERO( &f->f_av_value );
2915 for ( i=0; i<sc->numcsns; i++ ) {
2916 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
2917 f->f_av_value = sc->ctxcsn[i];
2919 of = op->ors_filter;
2920 op->ors_filter = mmf;
2921 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2924 op->ors_filterstr = si->si_filterstr;
2926 op->o_nocaching = 1;
2929 rc = be->be_search( op, &rs_search );
2930 if ( SLAP_MULTIMASTER( op->o_bd )) {
2931 op->ors_filter = of;
2933 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
2934 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
2935 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2940 op->o_nocaching = 0;
2942 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2944 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
2945 csn = sc->ctxcsn[m];
2947 csn = si->si_syncCookie.ctxcsn[0];
2950 op->o_bd = si->si_wbe;
2951 slap_queue_csn( op, &csn );
2953 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2954 while ( np_list != NULL ) {
2955 SlapReply rs_delete = {REP_RESULT};
2957 LDAP_LIST_REMOVE( np_list, npe_link );
2959 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2960 op->o_tag = LDAP_REQ_DELETE;
2961 op->o_callback = &cb;
2962 cb.sc_response = null_callback;
2964 op->o_req_dn = *np_prev->npe_name;
2965 op->o_req_ndn = *np_prev->npe_nname;
2966 rc = op->o_bd->be_delete( op, &rs_delete );
2967 Debug( LDAP_DEBUG_SYNC,
2968 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
2969 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2971 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2972 SlapReply rs_modify = {REP_RESULT};
2973 Modifications mod1, mod2;
2974 mod1.sml_op = LDAP_MOD_REPLACE;
2976 mod1.sml_desc = slap_schema.si_ad_objectClass;
2977 mod1.sml_type = mod1.sml_desc->ad_cname;
2978 mod1.sml_numvals = 2;
2979 mod1.sml_values = &gcbva[0];
2980 mod1.sml_nvalues = NULL;
2981 mod1.sml_next = &mod2;
2983 mod2.sml_op = LDAP_MOD_REPLACE;
2985 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2986 mod2.sml_type = mod2.sml_desc->ad_cname;
2987 mod2.sml_numvals = 1;
2988 mod2.sml_values = &gcbva[1];
2989 mod2.sml_nvalues = NULL;
2990 mod2.sml_next = NULL;
2992 op->o_tag = LDAP_REQ_MODIFY;
2993 op->orm_modlist = &mod1;
2995 rc = op->o_bd->be_modify( op, &rs_modify );
2996 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2999 while ( rs_delete.sr_err == LDAP_SUCCESS &&
3000 op->o_delete_glue_parent ) {
3001 op->o_delete_glue_parent = 0;
3002 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3003 slap_callback cb = { NULL };
3004 cb.sc_response = slap_null_cb;
3005 dnParent( &op->o_req_ndn, &pdn );
3007 op->o_req_ndn = pdn;
3008 op->o_callback = &cb;
3009 rs_reinit( &rs_delete, REP_RESULT );
3010 /* give it a root privil ? */
3011 op->o_bd->be_delete( op, &rs_delete );
3017 op->o_delete_glue_parent = 0;
3019 ber_bvfree( np_prev->npe_name );
3020 ber_bvfree( np_prev->npe_nname );
3023 if ( slapd_shutdown ) {
3028 slap_graduate_commit_csn( op );
3031 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3032 BER_BVZERO( &op->o_csn );
3039 syncrepl_add_glue_ancestors(
3043 Backend *be = op->o_bd;
3044 slap_callback cb = { NULL };
3046 int rc = LDAP_SUCCESS;
3049 struct berval dn = BER_BVNULL;
3050 struct berval ndn = BER_BVNULL;
3052 struct berval ptr, nptr;
3055 op->o_tag = LDAP_REQ_ADD;
3056 op->o_callback = &cb;
3057 cb.sc_response = null_callback;
3058 cb.sc_private = NULL;
3063 /* count RDNs in suffix */
3064 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3065 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3067 ptr.bv_len -= comma - ptr.bv_val;
3077 /* Start with BE suffix */
3079 for ( i = 0; i < suffrdns; i++ ) {
3080 comma = ber_bvrchr( &ptr, ',' );
3081 if ( comma != NULL ) {
3082 ptr.bv_len = comma - ptr.bv_val;
3089 if ( !BER_BVISEMPTY( &ptr ) ) {
3090 dn.bv_len -= ptr.bv_len + 1;
3091 dn.bv_val += ptr.bv_len + 1;
3094 /* the normalizedDNs are always the same length, no counting
3098 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3099 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3100 ndn.bv_len = be->be_nsuffix[0].bv_len;
3102 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3108 while ( ndn.bv_val > e->e_nname.bv_val ) {
3109 SlapReply rs_add = {REP_RESULT};
3111 glue = entry_alloc();
3112 ber_dupbv( &glue->e_name, &dn );
3113 ber_dupbv( &glue->e_nname, &ndn );
3115 a = attr_alloc( slap_schema.si_ad_objectClass );
3118 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3119 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3120 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3121 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3123 a->a_nvals = a->a_vals;
3125 a->a_next = glue->e_attrs;
3128 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3131 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3132 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3133 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3135 a->a_nvals = a->a_vals;
3137 a->a_next = glue->e_attrs;
3140 op->o_req_dn = glue->e_name;
3141 op->o_req_ndn = glue->e_nname;
3143 rc = be->be_add ( op, &rs_add );
3144 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3145 if ( op->ora_e == glue )
3146 be_entry_release_w( op, glue );
3148 /* incl. ALREADY EXIST */
3150 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3156 /* Move to next child */
3157 comma = ber_bvrchr( &ptr, ',' );
3158 if ( comma == NULL ) {
3161 ptr.bv_len = comma - ptr.bv_val;
3163 dn.bv_val = ++comma;
3164 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3166 comma = ber_bvrchr( &nptr, ',' );
3167 assert( comma != NULL );
3168 nptr.bv_len = comma - nptr.bv_val;
3170 ndn.bv_val = ++comma;
3171 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3182 slap_callback cb = { NULL };
3184 Backend *be = op->o_bd;
3185 SlapReply rs_add = {REP_RESULT};
3187 rc = syncrepl_add_glue_ancestors( op, e );
3190 case LDAP_ALREADY_EXISTS:
3197 op->o_tag = LDAP_REQ_ADD;
3198 op->o_callback = &cb;
3199 cb.sc_response = null_callback;
3200 cb.sc_private = NULL;
3202 op->o_req_dn = e->e_name;
3203 op->o_req_ndn = e->e_nname;
3205 rc = be->be_add ( op, &rs_add );
3206 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3207 if ( op->ora_e == e )
3208 be_entry_release_w( op, e );
3217 syncrepl_updateCookie(
3220 struct sync_cookie *syncCookie )
3222 Backend *be = op->o_bd;
3224 struct berval first = BER_BVNULL;
3226 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3229 int rc, i, j, changed = 0;
3232 slap_callback cb = { NULL };
3233 SlapReply rs_modify = {REP_RESULT};
3235 mod.sml_op = LDAP_MOD_REPLACE;
3236 mod.sml_desc = slap_schema.si_ad_contextCSN;
3237 mod.sml_type = mod.sml_desc->ad_cname;
3238 mod.sml_flags = SLAP_MOD_INTERNAL;
3239 mod.sml_nvalues = NULL;
3240 mod.sml_next = NULL;
3242 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3245 for ( i=0; i<syncCookie->numcsns; i++ ) {
3246 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3248 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3249 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3253 /* clone the cookieState CSNs so we can Replace the whole thing */
3254 mod.sml_numvals = si->si_cookieState->cs_num;
3255 mod.sml_values = op->o_tmpalloc(( mod.sml_numvals+1 )*sizeof(struct berval), op->o_tmpmemctx );
3256 for ( i=0; i<mod.sml_numvals; i++ )
3257 mod.sml_values[i] = si->si_cookieState->cs_vals[i];
3258 BER_BVZERO( &mod.sml_values[i] );
3260 /* find any CSNs in the syncCookie that are newer than the cookieState */
3261 for ( i=0; i<syncCookie->numcsns; i++ ) {
3262 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
3263 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
3265 len = syncCookie->ctxcsn[i].bv_len;
3266 if ( len > si->si_cookieState->cs_vals[j].bv_len )
3267 len = si->si_cookieState->cs_vals[j].bv_len;
3268 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3269 si->si_cookieState->cs_vals[j].bv_val, len ) > 0 ) {
3270 mod.sml_values[j] = syncCookie->ctxcsn[i];
3272 if ( BER_BVISNULL( &first ) ) {
3273 first = syncCookie->ctxcsn[i];
3275 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3277 first = syncCookie->ctxcsn[i];
3282 /* there was no match for this SID, it's a new CSN */
3283 if ( j == si->si_cookieState->cs_num ) {
3284 mod.sml_values = op->o_tmprealloc( mod.sml_values,
3285 ( mod.sml_numvals+2 )*sizeof(struct berval), op->o_tmpmemctx );
3286 mod.sml_values[mod.sml_numvals++] = syncCookie->ctxcsn[i];
3287 BER_BVZERO( &mod.sml_values[mod.sml_numvals] );
3288 if ( BER_BVISNULL( &first ) ) {
3289 first = syncCookie->ctxcsn[i];
3290 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3292 first = syncCookie->ctxcsn[i];
3297 /* Should never happen, ITS#5065 */
3298 if ( BER_BVISNULL( &first ) || !changed ) {
3299 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3300 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3303 op->o_bd = si->si_wbe;
3304 slap_queue_csn( op, &first );
3306 op->o_tag = LDAP_REQ_MODIFY;
3308 cb.sc_response = null_callback;
3311 op->o_callback = &cb;
3312 op->o_req_dn = si->si_contextdn;
3313 op->o_req_ndn = si->si_contextdn;
3315 /* update contextCSN */
3316 op->o_dont_replicate = 1;
3318 op->orm_modlist = &mod;
3319 op->orm_no_opattrs = 1;
3320 rc = op->o_bd->be_modify( op, &rs_modify );
3322 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3323 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3325 char txtbuf[SLAP_TEXT_BUFLEN];
3326 size_t textlen = sizeof txtbuf;
3327 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3328 rs_reinit( &rs_modify, REP_RESULT );
3329 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3331 rc = op->o_bd->be_add( op, &rs_modify );
3332 if ( e == op->ora_e )
3333 be_entry_release_w( op, op->ora_e );
3336 op->orm_no_opattrs = 0;
3337 op->o_dont_replicate = 0;
3339 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3340 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3341 /* If we replaced any old values */
3342 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3343 if ( mod.sml_values[i].bv_val != si->si_cookieState->cs_vals[i].bv_val )
3344 ber_bvreplace( &si->si_cookieState->cs_vals[i],
3345 &mod.sml_values[i] );
3347 /* Handle any added values */
3348 if ( i < mod.sml_numvals ) {
3349 si->si_cookieState->cs_num = mod.sml_numvals;
3350 value_add( &si->si_cookieState->cs_vals, &mod.sml_values[i] );
3351 free( si->si_cookieState->cs_sids );
3352 si->si_cookieState->cs_sids = slap_parse_csn_sids(
3353 si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
3356 /* Don't just dup the provider's cookie, recreate it */
3357 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
3358 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn, si->si_cookieState->cs_vals, NULL );
3359 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num * sizeof(int) );
3360 for ( i=0; i<si->si_cookieState->cs_num; i++ )
3361 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
3363 si->si_cookieState->cs_age++;
3364 si->si_cookieAge = si->si_cookieState->cs_age;
3366 Debug( LDAP_DEBUG_ANY,
3367 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3368 si->si_ridtxt, rs_modify.sr_err, 0 );
3370 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3373 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3374 BER_BVZERO( &op->o_csn );
3375 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3376 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3379 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3380 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3387 /* Compare the attribute from the old entry to the one in the new
3388 * entry. The Modifications from the new entry will either be left
3389 * in place, or changed to an Add or Delete as needed.
3392 attr_cmp( Operation *op, Attribute *old, Attribute *new,
3393 Modifications ***mret, Modifications ***mcur )
3396 Modifications *mod, **modtail;
3402 struct berval **adds, **dels;
3403 /* count old and new */
3404 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
3405 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
3407 /* there MUST be both old and new values */
3412 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
3413 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
3415 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
3416 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
3420 for ( i=0; i<o; i++ ) {
3421 for ( j=0; j<n; j++ ) {
3424 if ( bvmatch( dels[i], adds[j] ) ) {
3434 /* Don't delete/add an objectClass, always use the replace op.
3435 * Modify would fail if provider has replaced entry with a new,
3436 * and the new explicitly includes a superior of a class that was
3437 * only included implicitly in the old entry. Ref ITS#5517.
3439 * Also use replace op if attr has no equality matching rule.
3442 if ( ( nn || ( no > 0 && no < o ) ) &&
3443 ( old->a_desc == slap_schema.si_ad_objectClass ||
3444 !old->a_desc->ad_type->sat_equality ) )
3450 /* all old values were deleted, just use the replace op */
3454 /* delete some values */
3455 mod = ch_malloc( sizeof( Modifications ) );
3456 mod->sml_op = LDAP_MOD_DELETE;
3458 mod->sml_desc = old->a_desc;
3459 mod->sml_type = mod->sml_desc->ad_cname;
3460 mod->sml_numvals = no;
3461 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3462 if ( old->a_vals != old->a_nvals ) {
3463 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3465 mod->sml_nvalues = NULL;
3468 for ( i = 0; i < o; i++ ) {
3469 if ( !dels[i] ) continue;
3470 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
3471 if ( mod->sml_nvalues ) {
3472 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
3476 BER_BVZERO( &mod->sml_values[j] );
3477 if ( mod->sml_nvalues ) {
3478 BER_BVZERO( &mod->sml_nvalues[j] );
3481 modtail = &mod->sml_next;
3484 op->o_tmpfree( dels, op->o_tmpmemctx );
3485 /* some values were added */
3486 if ( nn && no < o ) {
3487 mod = ch_malloc( sizeof( Modifications ) );
3488 mod->sml_op = LDAP_MOD_ADD;
3490 mod->sml_desc = old->a_desc;
3491 mod->sml_type = mod->sml_desc->ad_cname;
3492 mod->sml_numvals = nn;
3493 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3494 if ( old->a_vals != old->a_nvals ) {
3495 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3497 mod->sml_nvalues = NULL;
3500 for ( i = 0; i < n; i++ ) {
3501 if ( !adds[i] ) continue;
3502 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
3503 if ( mod->sml_nvalues ) {
3504 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
3508 BER_BVZERO( &mod->sml_values[j] );
3509 if ( mod->sml_nvalues ) {
3510 BER_BVZERO( &mod->sml_nvalues[j] );
3513 modtail = &mod->sml_next;
3516 op->o_tmpfree( adds, op->o_tmpmemctx );
3518 /* new attr, just use the new mod */
3522 /* advance to next element */
3526 **mcur = mod->sml_next;
3528 modtail = &mod->sml_next;
3530 *mcur = &mod->sml_next;
3536 /* Generate a set of modifications to change the old entry into the
3537 * new one. On input ml is a list of modifications equivalent to
3538 * the new entry. It will be massaged and the result will be stored
3541 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
3542 Modifications **mods, Modifications **ml, int is_ctx)
3544 Modifications **modtail = mods;
3546 /* We assume that attributes are saved in the same order
3547 * in the remote and local databases. So if we walk through
3548 * the attributeDescriptions one by one they should match in
3549 * lock step. If not, look for an add or delete.
3551 while ( old && new )
3553 /* If we've seen this before, use its mod now */
3554 if ( new->a_flags & SLAP_ATTR_IXADD ) {
3555 attr_cmp( op, NULL, new, &modtail, &ml );
3559 /* Skip contextCSN */
3560 if ( is_ctx && old->a_desc ==
3561 slap_schema.si_ad_contextCSN ) {
3566 if ( old->a_desc != new->a_desc ) {
3570 /* If it's just been re-added later,
3571 * remember that we've seen it.
3573 tmp = attr_find( new, old->a_desc );
3575 tmp->a_flags |= SLAP_ATTR_IXADD;
3577 /* If it's a new attribute, pull it in.
3579 tmp = attr_find( old, new->a_desc );
3581 attr_cmp( op, NULL, new, &modtail, &ml );
3585 /* Delete old attr */
3586 mod = ch_malloc( sizeof( Modifications ) );
3587 mod->sml_op = LDAP_MOD_DELETE;
3589 mod->sml_desc = old->a_desc;
3590 mod->sml_type = mod->sml_desc->ad_cname;
3591 mod->sml_numvals = 0;
3592 mod->sml_values = NULL;
3593 mod->sml_nvalues = NULL;
3595 modtail = &mod->sml_next;
3600 /* kludge - always update modifiersName so that it
3601 * stays co-located with the other mod opattrs. But only
3602 * if we know there are other valid mods.
3604 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
3605 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
3606 attr_cmp( op, NULL, new, &modtail, &ml );
3608 attr_cmp( op, old, new, &modtail, &ml );
3621 dninfo *dni = op->o_callback->sc_private;
3623 if ( rs->sr_type == REP_SEARCH ) {
3624 if ( !BER_BVISNULL( &dni->dn ) ) {
3625 Debug( LDAP_DEBUG_ANY,
3626 "dn_callback : consistency error - "
3627 "entryUUID is not unique\n", 0, 0, 0 );
3629 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
3630 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
3631 /* If there is a new entry, see if it differs from the old.
3632 * We compare the non-normalized values so that cosmetic changes
3633 * in the provider are always propagated.
3635 if ( dni->new_entry ) {
3636 Attribute *old, *new;
3637 struct berval old_rdn, new_rdn;
3638 struct berval old_p, new_p;
3639 int is_ctx, new_sup = 0;
3641 /* If old entry is not a glue entry, make sure new entry
3642 * is actually newer than old entry
3644 if ( !is_entry_glue( rs->sr_entry )) {
3645 old = attr_find( rs->sr_entry->e_attrs,
3646 slap_schema.si_ad_entryCSN );
3647 new = attr_find( dni->new_entry->e_attrs,
3648 slap_schema.si_ad_entryCSN );
3651 ber_len_t len = old->a_vals[0].bv_len;
3652 if ( len > new->a_vals[0].bv_len )
3653 len = new->a_vals[0].bv_len;
3654 rc = memcmp( old->a_vals[0].bv_val,
3655 new->a_vals[0].bv_val, len );
3657 Debug( LDAP_DEBUG_SYNC,
3658 "dn_callback : new entry is older than ours "
3659 "%s ours %s, new %s\n",
3660 rs->sr_entry->e_name.bv_val,
3661 old->a_vals[0].bv_val,
3662 new->a_vals[0].bv_val );
3663 return LDAP_SUCCESS;
3664 } else if ( rc == 0 ) {
3665 Debug( LDAP_DEBUG_SYNC,
3666 "dn_callback : entries have identical CSN "
3668 rs->sr_entry->e_name.bv_val,
3669 old->a_vals[0].bv_val, 0 );
3670 return LDAP_SUCCESS;
3675 is_ctx = dn_match( &rs->sr_entry->e_nname,
3676 &op->o_bd->be_nsuffix[0] );
3678 /* Did the DN change?
3679 * case changes in the parent are ignored,
3680 * we only want to know if the RDN was
3683 dnRdn( &rs->sr_entry->e_name, &old_rdn );
3684 dnRdn( &dni->new_entry->e_name, &new_rdn );
3685 dnParent( &rs->sr_entry->e_nname, &old_p );
3686 dnParent( &dni->new_entry->e_nname, &new_p );
3688 new_sup = !dn_match( &old_p, &new_p );
3689 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
3691 struct berval oldRDN, oldVal;
3692 AttributeDescription *ad = NULL;
3698 dni->nnewSup = new_p;
3700 /* See if the oldRDN was deleted */
3701 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
3702 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
3703 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
3705 oldRDN.bv_len -= oldVal.bv_len + 1;
3706 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3708 for ( oldpos=0, a=rs->sr_entry->e_attrs;
3709 a && a->a_desc != ad; oldpos++, a=a->a_next );
3711 for ( newpos=0, a=dni->new_entry->e_attrs;
3712 a && a->a_desc != ad; newpos++, a=a->a_next );
3713 if ( !a || oldpos != newpos || attr_valfind( a,
3714 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
3715 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3716 SLAP_MR_VALUE_OF_SYNTAX,
3717 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
3721 /* Get the newRDN's desc */
3722 dnRdn( &dni->new_entry->e_nname, &oldRDN );
3723 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
3724 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
3726 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3729 /* A ModDN has happened, but in Refresh mode other
3730 * changes may have occurred before we picked it up.
3731 * So fallthru to regular Modify processing.
3735 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
3736 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
3740 } else if ( rs->sr_type == REP_RESULT ) {
3741 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
3742 Debug( LDAP_DEBUG_ANY,
3743 "dn_callback : consistency error - "
3744 "entryUUID is not unique\n", 0, 0, 0 );
3748 return LDAP_SUCCESS;
3752 nonpresent_callback(
3756 syncinfo_t *si = op->o_callback->sc_private;
3759 struct berval* present_uuid = NULL;
3760 struct nonpresent_entry *np_entry;
3762 if ( rs->sr_type == REP_RESULT ) {
3763 count = avl_free( si->si_presentlist, ch_free );
3764 si->si_presentlist = NULL;
3766 } else if ( rs->sr_type == REP_SEARCH ) {
3767 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
3768 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
3771 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
3775 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
3776 char buf[sizeof("rid=999 non")];
3778 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
3779 present_uuid ? "" : "non" );
3781 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
3782 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
3785 if ( a == NULL ) return 0;
3788 if ( present_uuid == NULL ) {
3789 np_entry = (struct nonpresent_entry *)
3790 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
3791 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
3792 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
3793 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
3796 avl_delete( &si->si_presentlist,
3797 &a->a_nvals[0], syncuuid_cmp );
3798 ch_free( present_uuid );
3801 return LDAP_SUCCESS;
3809 if ( rs->sr_err != LDAP_SUCCESS &&
3810 rs->sr_err != LDAP_REFERRAL &&
3811 rs->sr_err != LDAP_ALREADY_EXISTS &&
3812 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
3813 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
3815 Debug( LDAP_DEBUG_ANY,
3816 "null_callback : error code 0x%x\n",
3819 return LDAP_SUCCESS;
3822 static struct berval *
3823 slap_uuidstr_from_normalized(
3824 struct berval* uuidstr,
3825 struct berval* normalized,
3830 unsigned char nibble;
3833 if ( normalized == NULL ) return NULL;
3834 if ( normalized->bv_len != 16 ) return NULL;
3839 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3840 if ( new == NULL ) {
3847 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3848 if ( new != uuidstr ) {
3849 slap_sl_free( new, ctx );
3854 for ( i = 0; i < 16; i++ ) {
3855 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
3856 new->bv_val[(i<<1)+d] = '-';
3860 nibble = (normalized->bv_val[i] >> 4) & 0xF;
3861 if ( nibble < 10 ) {
3862 new->bv_val[(i<<1)+d] = nibble + '0';
3864 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
3867 nibble = (normalized->bv_val[i]) & 0xF;
3868 if ( nibble < 10 ) {
3869 new->bv_val[(i<<1)+d+1] = nibble + '0';
3871 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
3875 new->bv_val[new->bv_len] = '\0';
3882 if ( normalized == NULL ) return NULL;
3883 if ( normalized->bv_len != 16 ) return NULL;
3889 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3890 if ( new == NULL ) {
3897 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3902 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
3903 normalized->bv_len, new->bv_val, new->bv_len + 1 );
3907 if ( new != NULL ) {
3908 if ( new->bv_val != NULL ) {
3909 slap_sl_free( new->bv_val, ctx );
3912 if ( new != uuidstr ) {
3913 slap_sl_free( new, ctx );
3926 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
3928 const struct berval *uuid1 = v_uuid1;
3929 const struct berval *uuid2 = v_uuid2;
3930 int rc = uuid1->bv_len - uuid2->bv_len;
3931 if ( rc ) return rc;
3932 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
3936 syncinfo_free( syncinfo_t *sie, int free_all )
3938 syncinfo_t *si_next;
3940 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
3941 sie->si_ridtxt, 0, 0 );
3944 si_next = sie->si_next;
3947 if ( sie->si_conn ) {
3948 connection_client_stop( sie->si_conn );
3949 sie->si_conn = NULL;
3951 ldap_unbind_ext( sie->si_ld, NULL, NULL );
3955 struct re_s *re = sie->si_re;
3958 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3959 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
3960 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3961 ldap_pvt_runqueue_remove( &slapd_rq, re );
3962 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3965 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
3967 bindconf_free( &sie->si_bindconf );
3969 if ( sie->si_filterstr.bv_val ) {
3970 ch_free( sie->si_filterstr.bv_val );
3972 if ( sie->si_filter ) {
3973 filter_free( sie->si_filter );
3975 if ( sie->si_logfilterstr.bv_val ) {
3976 ch_free( sie->si_logfilterstr.bv_val );
3978 if ( sie->si_base.bv_val ) {
3979 ch_free( sie->si_base.bv_val );
3981 if ( sie->si_logbase.bv_val ) {
3982 ch_free( sie->si_logbase.bv_val );
3984 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
3985 ch_free( sie->si_contextdn.bv_val );
3987 if ( sie->si_attrs ) {
3989 while ( sie->si_attrs[i] != NULL ) {
3990 ch_free( sie->si_attrs[i] );
3993 ch_free( sie->si_attrs );
3995 if ( sie->si_exattrs ) {
3997 while ( sie->si_exattrs[i] != NULL ) {
3998 ch_free( sie->si_exattrs[i] );
4001 ch_free( sie->si_exattrs );
4003 if ( sie->si_anlist ) {
4005 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
4006 ch_free( sie->si_anlist[i].an_name.bv_val );
4009 ch_free( sie->si_anlist );
4011 if ( sie->si_exanlist ) {
4013 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
4014 ch_free( sie->si_exanlist[i].an_name.bv_val );
4017 ch_free( sie->si_exanlist );
4019 if ( sie->si_retryinterval ) {
4020 ch_free( sie->si_retryinterval );
4022 if ( sie->si_retrynum ) {
4023 ch_free( sie->si_retrynum );
4025 if ( sie->si_retrynum_init ) {
4026 ch_free( sie->si_retrynum_init );
4028 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
4029 if ( sie->si_presentlist ) {
4030 avl_free( sie->si_presentlist, ch_free );
4032 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
4033 struct nonpresent_entry* npe;
4034 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
4035 LDAP_LIST_REMOVE( npe, npe_link );
4036 if ( npe->npe_name ) {
4037 if ( npe->npe_name->bv_val ) {
4038 ch_free( npe->npe_name->bv_val );
4040 ch_free( npe->npe_name );
4042 if ( npe->npe_nname ) {
4043 if ( npe->npe_nname->bv_val ) {
4044 ch_free( npe->npe_nname->bv_val );
4046 ch_free( npe->npe_nname );
4050 if ( sie->si_cookieState ) {
4051 sie->si_cookieState->cs_ref--;
4052 if ( !sie->si_cookieState->cs_ref ) {
4053 ch_free( sie->si_cookieState->cs_sids );
4054 ber_bvarray_free( sie->si_cookieState->cs_vals );
4055 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
4056 ch_free( sie->si_cookieState->cs_psids );
4057 ber_bvarray_free( sie->si_cookieState->cs_pvals );
4058 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
4059 ch_free( sie->si_cookieState );
4062 #ifdef ENABLE_REWRITE
4063 if ( sie->si_rewrite )
4064 rewrite_info_delete( &sie->si_rewrite );
4065 if ( sie->si_suffixm.bv_val )
4066 ch_free( sie->si_suffixm.bv_val );
4070 } while ( free_all && si_next );
4073 #ifdef ENABLE_REWRITE
4075 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4077 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4078 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4079 char *argvRule[] = { "rewriteRule", NULL, NULL, ":", NULL };
4083 if ( si->si_rewrite )
4084 rewrite_info_delete( &si->si_rewrite );
4085 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4087 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4088 if ( rc != LDAP_SUCCESS )
4091 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4092 if ( rc != LDAP_SUCCESS )
4095 vnc = ch_malloc( si->si_base.bv_len + 6 );
4096 strcpy( vnc, "(.*)" );
4097 lutil_strcopy( lutil_strcopy( vnc+4, si->si_base.bv_val ), "$" );
4100 rnc = ch_malloc( si->si_suffixm.bv_len + 3 );
4101 strcpy( rnc, "%1" );
4102 strcpy( rnc+2, si->si_suffixm.bv_val );
4105 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4112 /* NOTE: used & documented in slapd.conf(5) */
4114 #define PROVIDERSTR "provider"
4115 #define SCHEMASTR "schemachecking"
4116 #define FILTERSTR "filter"
4117 #define SEARCHBASESTR "searchbase"
4118 #define SCOPESTR "scope"
4119 #define ATTRSONLYSTR "attrsonly"
4120 #define ATTRSSTR "attrs"
4121 #define TYPESTR "type"
4122 #define INTERVALSTR "interval"
4123 #define RETRYSTR "retry"
4124 #define SLIMITSTR "sizelimit"
4125 #define TLIMITSTR "timelimit"
4126 #define SYNCDATASTR "syncdata"
4127 #define LOGBASESTR "logbase"
4128 #define LOGFILTERSTR "logfilter"
4129 #define SUFFIXMSTR "suffixmassage"
4130 #define STRICT_REFRESH "strictrefresh"
4132 /* FIXME: undocumented */
4133 #define EXATTRSSTR "exattrs"
4134 #define MANAGEDSAITSTR "manageDSAit"
4138 GOT_RID = 0x00000001U,
4139 GOT_PROVIDER = 0x00000002U,
4140 GOT_SCHEMACHECKING = 0x00000004U,
4141 GOT_FILTER = 0x00000008U,
4142 GOT_SEARCHBASE = 0x00000010U,
4143 GOT_SCOPE = 0x00000020U,
4144 GOT_ATTRSONLY = 0x00000040U,
4145 GOT_ATTRS = 0x00000080U,
4146 GOT_TYPE = 0x00000100U,
4147 GOT_INTERVAL = 0x00000200U,
4148 GOT_RETRY = 0x00000400U,
4149 GOT_SLIMIT = 0x00000800U,
4150 GOT_TLIMIT = 0x00001000U,
4151 GOT_SYNCDATA = 0x00002000U,
4152 GOT_LOGBASE = 0x00004000U,
4153 GOT_LOGFILTER = 0x00008000U,
4154 GOT_EXATTRS = 0x00010000U,
4155 GOT_MANAGEDSAIT = 0x00020000U,
4156 GOT_BINDCONF = 0x00040000U,
4157 GOT_SUFFIXM = 0x00080000U,
4160 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4163 static slap_verbmasks datamodes[] = {
4164 { BER_BVC("default"), SYNCDATA_DEFAULT },
4165 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4166 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4171 parse_syncrepl_retry(
4178 int use_default = 0;
4180 char *val = arg + STRLENOF( RETRYSTR "=" );
4181 if ( strcasecmp( val, "undefined" ) == 0 ) {
4186 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4187 retry_list[0] = NULL;
4189 slap_str2clist( &retry_list, val, " ,\t" );
4191 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4194 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4195 "Error: incomplete syncrepl retry list" );
4196 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4197 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4198 ch_free( retry_list[k] );
4200 ch_free( retry_list );
4203 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4204 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4205 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4206 for ( j = 0; j < n; j++ ) {
4208 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4209 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4210 "Error: invalid retry interval \"%s\" (#%d)",
4211 retry_list[j*2], j );
4212 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4213 /* do some cleanup */
4216 si->si_retryinterval[j] = (time_t)t;
4217 if ( *retry_list[j*2+1] == '+' ) {
4218 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4219 si->si_retrynum[j] = RETRYNUM_FOREVER;
4223 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4224 || si->si_retrynum_init[j] <= 0 )
4226 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4227 "Error: invalid initial retry number \"%s\" (#%d)",
4228 retry_list[j*2+1], j );
4229 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4230 /* do some cleanup */
4233 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4234 || si->si_retrynum[j] <= 0 )
4236 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4237 "Error: invalid retry number \"%s\" (#%d)",
4238 retry_list[j*2+1], j );
4239 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4240 /* do some cleanup */
4245 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4246 Debug( LDAP_DEBUG_CONFIG,
4247 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4251 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4252 si->si_retrynum[j] = RETRYNUM_TAIL;
4253 si->si_retryinterval[j] = 0;
4255 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4256 ch_free( retry_list[k] );
4258 ch_free( retry_list );
4259 if ( !use_default ) {
4260 si->si_got |= GOT_RETRY;
4267 parse_syncrepl_line(
4274 for ( i = 1; i < c->argc; i++ ) {
4275 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4276 STRLENOF( IDSTR "=" ) ) )
4279 /* '\0' string terminator accounts for '=' */
4280 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4281 if ( lutil_atoi( &tmp, val ) != 0 ) {
4282 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4283 "Error: parse_syncrepl_line: "
4284 "unable to parse syncrepl id \"%s\"", val );
4285 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4288 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4289 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4290 "Error: parse_syncrepl_line: "
4291 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4292 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4296 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4297 si->si_got |= GOT_RID;
4298 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4299 STRLENOF( PROVIDERSTR "=" ) ) )
4301 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4302 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4304 if ( ldap_is_ldaps_url( val ))
4305 si->si_bindconf.sb_tls_do_init = 1;
4307 si->si_got |= GOT_PROVIDER;
4308 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4309 STRLENOF( SCHEMASTR "=" ) ) )
4311 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4312 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4313 si->si_schemachecking = 1;
4314 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4315 si->si_schemachecking = 0;
4317 si->si_schemachecking = 1;
4319 si->si_got |= GOT_SCHEMACHECKING;
4320 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4321 STRLENOF( FILTERSTR "=" ) ) )
4323 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4324 if ( si->si_filterstr.bv_val )
4325 ch_free( si->si_filterstr.bv_val );
4326 ber_str2bv( val, 0, 1, &si->si_filterstr );
4327 si->si_got |= GOT_FILTER;
4328 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4329 STRLENOF( LOGFILTERSTR "=" ) ) )
4331 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4332 if ( si->si_logfilterstr.bv_val )
4333 ch_free( si->si_logfilterstr.bv_val );
4334 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4335 si->si_got |= GOT_LOGFILTER;
4336 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4337 STRLENOF( SEARCHBASESTR "=" ) ) )
4342 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4343 if ( si->si_base.bv_val ) {
4344 ch_free( si->si_base.bv_val );
4346 ber_str2bv( val, 0, 0, &bv );
4347 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4348 if ( rc != LDAP_SUCCESS ) {
4349 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4350 "Invalid base DN \"%s\": %d (%s)",
4351 val, rc, ldap_err2string( rc ) );
4352 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4355 si->si_got |= GOT_SEARCHBASE;
4356 #ifdef ENABLE_REWRITE
4357 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
4358 STRLENOF( SUFFIXMSTR "=" ) ) )
4363 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
4364 if ( si->si_suffixm.bv_val ) {
4365 ch_free( si->si_suffixm.bv_val );
4367 ber_str2bv( val, 0, 0, &bv );
4368 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
4369 if ( rc != LDAP_SUCCESS ) {
4370 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4371 "Invalid massage DN \"%s\": %d (%s)",
4372 val, rc, ldap_err2string( rc ) );
4373 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4376 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
4377 ch_free( si->si_suffixm.bv_val );
4378 BER_BVZERO( &si->si_suffixm );
4379 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4380 "Massage DN \"%s\" is not within the database naming context",
4382 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4385 si->si_got |= GOT_SUFFIXM;
4387 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4388 STRLENOF( LOGBASESTR "=" ) ) )
4393 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
4394 if ( si->si_logbase.bv_val ) {
4395 ch_free( si->si_logbase.bv_val );
4397 ber_str2bv( val, 0, 0, &bv );
4398 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
4399 if ( rc != LDAP_SUCCESS ) {
4400 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4401 "Invalid logbase DN \"%s\": %d (%s)",
4402 val, rc, ldap_err2string( rc ) );
4403 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4406 si->si_got |= GOT_LOGBASE;
4407 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
4408 STRLENOF( SCOPESTR "=" ) ) )
4411 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
4412 j = ldap_pvt_str2scope( val );
4414 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4415 "Error: parse_syncrepl_line: "
4416 "unknown scope \"%s\"", val);
4417 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4421 si->si_got |= GOT_SCOPE;
4422 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
4423 STRLENOF( ATTRSONLYSTR ) ) )
4425 si->si_attrsonly = 1;
4426 si->si_got |= GOT_ATTRSONLY;
4427 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
4428 STRLENOF( ATTRSSTR "=" ) ) )
4430 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
4431 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4433 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4434 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
4435 if ( si->si_anlist == NULL ) {
4436 ch_free( attr_fname );
4439 si->si_anfile = attr_fname;
4441 char *str, *s, *next;
4442 const char *delimstr = " ,\t";
4443 str = ch_strdup( val );
4444 for ( s = ldap_pvt_strtok( str, delimstr, &next );
4446 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
4448 if ( strlen(s) == 1 && *s == '*' ) {
4449 si->si_allattrs = 1;
4450 val[ s - str ] = delimstr[0];
4452 if ( strlen(s) == 1 && *s == '+' ) {
4453 si->si_allopattrs = 1;
4454 val [ s - str ] = delimstr[0];
4458 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
4459 if ( si->si_anlist == NULL ) {
4463 si->si_got |= GOT_ATTRS;
4464 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
4465 STRLENOF( EXATTRSSTR "=" ) ) )
4467 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
4468 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4470 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4471 si->si_exanlist = file2anlist(
4472 si->si_exanlist, attr_fname, " ,\t" );
4473 if ( si->si_exanlist == NULL ) {
4474 ch_free( attr_fname );
4477 ch_free( attr_fname );
4479 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
4480 if ( si->si_exanlist == NULL ) {
4484 si->si_got |= GOT_EXATTRS;
4485 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
4486 STRLENOF( TYPESTR "=" ) ) )
4488 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
4489 if ( !strncasecmp( val, "refreshOnly",
4490 STRLENOF("refreshOnly") ) )
4492 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4493 } else if ( !strncasecmp( val, "refreshAndPersist",
4494 STRLENOF("refreshAndPersist") ) )
4496 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
4497 si->si_interval = 60;
4499 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4500 "Error: parse_syncrepl_line: "
4501 "unknown sync type \"%s\"", val);
4502 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4505 si->si_got |= GOT_TYPE;
4506 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
4507 STRLENOF( INTERVALSTR "=" ) ) )
4509 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
4510 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
4511 si->si_interval = 0;
4512 } else if ( strchr( val, ':' ) != NULL ) {
4513 char *next, *ptr = val;
4516 dd = strtol( ptr, &next, 10 );
4517 if ( next == ptr || next[0] != ':' || dd < 0 ) {
4518 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4519 "Error: parse_syncrepl_line: "
4520 "invalid interval \"%s\", unable to parse days", val );
4521 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4525 hh = strtol( ptr, &next, 10 );
4526 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
4527 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4528 "Error: parse_syncrepl_line: "
4529 "invalid interval \"%s\", unable to parse hours", val );
4530 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4534 mm = strtol( ptr, &next, 10 );
4535 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
4536 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4537 "Error: parse_syncrepl_line: "
4538 "invalid interval \"%s\", unable to parse minutes", val );
4539 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4543 ss = strtol( ptr, &next, 10 );
4544 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
4545 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4546 "Error: parse_syncrepl_line: "
4547 "invalid interval \"%s\", unable to parse seconds", val );
4548 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4551 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
4555 if ( lutil_parse_time( val, &t ) != 0 ) {
4556 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4557 "Error: parse_syncrepl_line: "
4558 "invalid interval \"%s\"", val );
4559 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4562 si->si_interval = (time_t)t;
4564 if ( si->si_interval < 0 ) {
4565 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4566 "Error: parse_syncrepl_line: "
4567 "invalid interval \"%ld\"",
4568 (long) si->si_interval);
4569 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4572 si->si_got |= GOT_INTERVAL;
4573 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
4574 STRLENOF( RETRYSTR "=" ) ) )
4576 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
4579 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
4580 STRLENOF( MANAGEDSAITSTR "=" ) ) )
4582 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
4583 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
4584 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
4586 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4587 "invalid manageDSAit value \"%s\".\n",
4589 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4592 si->si_got |= GOT_MANAGEDSAIT;
4593 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
4594 STRLENOF( SLIMITSTR "=") ) )
4596 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
4597 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4600 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
4601 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4602 "invalid size limit value \"%s\".\n",
4604 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4607 si->si_got |= GOT_SLIMIT;
4608 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
4609 STRLENOF( TLIMITSTR "=" ) ) )
4611 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
4612 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4615 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
4616 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4617 "invalid time limit value \"%s\".\n",
4619 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4622 si->si_got |= GOT_TLIMIT;
4623 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
4624 STRLENOF( SYNCDATASTR "=" ) ) )
4626 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
4627 si->si_syncdata = verb_to_mask( val, datamodes );
4628 si->si_got |= GOT_SYNCDATA;
4629 } else if ( !strncasecmp( c->argv[ i ], STRICT_REFRESH,
4630 STRLENOF( STRICT_REFRESH ) ) )
4632 si->si_strict_refresh = 1;
4633 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
4634 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4635 "Error: parse_syncrepl_line: "
4636 "unable to parse \"%s\"\n", c->argv[ i ] );
4637 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4640 si->si_got |= GOT_BINDCONF;
4643 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
4644 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4645 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
4646 si->si_got & GOT_RID ? "" : " "IDSTR,
4647 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
4648 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
4649 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4653 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
4654 ch_free( si->si_base.bv_val );
4655 BER_BVZERO( &si->si_base );
4656 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4657 "Base DN \"%s\" is not within the database naming context",
4659 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4663 #ifdef ENABLE_REWRITE
4664 if ( si->si_got & GOT_SUFFIXM ) {
4665 if (config_suffixm( c, si )) {
4666 ch_free( si->si_suffixm.bv_val );
4667 BER_BVZERO( &si->si_suffixm );
4668 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4669 "Error configuring rewrite engine" );
4670 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4676 if ( !( si->si_got & GOT_RETRY ) ) {
4677 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
4678 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
4679 if ( si->si_retryinterval == NULL ) {
4680 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
4686 si->si_filter = str2filter( si->si_filterstr.bv_val );
4687 if ( si->si_filter == NULL ) {
4688 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
4689 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
4703 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
4704 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
4705 "operations required for syncrepl", c->be->be_type );
4706 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4709 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
4710 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
4711 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4714 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
4717 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
4721 si->si_bindconf.sb_tls = SB_TLS_OFF;
4722 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
4723 si->si_schemachecking = 0;
4724 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
4725 &si->si_filterstr );
4726 si->si_base.bv_val = NULL;
4727 si->si_scope = LDAP_SCOPE_SUBTREE;
4728 si->si_attrsonly = 0;
4729 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4730 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4731 si->si_attrs = NULL;
4732 si->si_allattrs = 0;
4733 si->si_allopattrs = 0;
4734 si->si_exattrs = NULL;
4735 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4736 si->si_interval = 86400;
4737 si->si_retryinterval = NULL;
4738 si->si_retrynum_init = NULL;
4739 si->si_retrynum = NULL;
4740 si->si_manageDSAit = 0;
4744 si->si_presentlist = NULL;
4745 LDAP_LIST_INIT( &si->si_nonpresentlist );
4746 ldap_pvt_thread_mutex_init( &si->si_mutex );
4748 rc = parse_syncrepl_line( c, si );
4753 /* Must be LDAPv3 because we need controls */
4754 switch ( si->si_bindconf.sb_version ) {
4756 /* not explicitly set */
4757 si->si_bindconf.sb_version = LDAP_VERSION3;
4760 /* explicitly set */
4763 Debug( LDAP_DEBUG_ANY,
4764 "version %d incompatible with syncrepl\n",
4765 si->si_bindconf.sb_version, 0, 0 );
4766 syncinfo_free( si, 0 );
4770 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
4771 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4772 "<%s> invalid URL", c->argv[0] );
4773 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
4774 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
4779 if ( slapMode & SLAP_SERVER_MODE ) {
4781 /* check if consumer points to current server and database.
4782 * If so, ignore this configuration.
4784 if ( !SLAP_DBHIDDEN( c->be ) ) {
4786 /* if searchbase doesn't match current DB suffix,
4787 * assume it's different
4789 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
4790 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
4795 /* if searchbase matches, see if URLs match */
4796 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
4802 init_syncrepl( si );
4803 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4804 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
4805 si->si_interval, do_syncrepl, si, "do_syncrepl",
4807 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4809 rc = config_sync_shadow( c ) ? -1 : 0;
4814 /* mirrormode still needs to see this flag in tool mode */
4815 rc = config_sync_shadow( c ) ? -1 : 0;
4817 ldap_free_urldesc( lud );
4821 /* Use main slapd defaults */
4822 bindconf_tls_defaults( &si->si_bindconf );
4825 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
4826 syncinfo_free( si, 0 );
4829 Debug( LDAP_DEBUG_CONFIG,
4830 "Config: ** successfully added syncrepl %s \"%s\"\n",
4832 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
4833 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
4834 if ( c->be->be_syncinfo ) {
4837 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
4839 /* add new syncrepl to end of list (same order as when deleting) */
4840 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
4843 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
4844 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
4845 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
4847 c->be->be_syncinfo = si;
4849 si->si_cookieState->cs_ref++;
4858 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
4860 struct berval bc, uri, bs;
4861 char buf[BUFSIZ*2], *ptr;
4864 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
4868 /* temporarily inhibit bindconf from printing URI */
4869 uri = si->si_bindconf.sb_uri;
4870 BER_BVZERO( &si->si_bindconf.sb_uri );
4871 si->si_bindconf.sb_version = 0;
4872 bindconf_unparse( &si->si_bindconf, &bc );
4873 si->si_bindconf.sb_uri = uri;
4874 si->si_bindconf.sb_version = LDAP_VERSION3;
4877 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
4878 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
4879 si->si_rid, si->si_bindconf.sb_uri.bv_val );
4880 if ( len >= sizeof( buf ) ) return;
4882 if ( !BER_BVISNULL( &bc ) ) {
4883 if ( WHATSLEFT <= bc.bv_len ) {
4887 ptr = lutil_strcopy( ptr, bc.bv_val );
4890 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
4891 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
4892 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
4893 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
4896 if ( !BER_BVISNULL( &si->si_base ) ) {
4897 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
4898 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
4899 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
4902 #ifdef ENABLE_REWRITE
4903 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
4904 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
4905 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
4906 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
4910 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
4911 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
4912 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
4913 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
4916 if ( !BER_BVISNULL( &si->si_logbase ) ) {
4917 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
4918 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
4919 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
4922 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
4923 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
4924 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
4925 ptr = lutil_strcopy( ptr, bs.bv_val );
4927 if ( si->si_attrsonly ) {
4928 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4929 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
4931 if ( si->si_anfile ) {
4932 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
4933 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
4934 ptr = lutil_strcopy( ptr, si->si_anfile );
4936 } else if ( si->si_allattrs || si->si_allopattrs ||
4937 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
4941 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4942 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
4944 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
4945 if ( ptr == NULL ) return;
4946 if ( si->si_allattrs ) {
4947 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
4948 if ( old != ptr ) *ptr++ = ',';
4951 if ( si->si_allopattrs ) {
4952 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
4953 if ( old != ptr ) *ptr++ = ',';
4958 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
4959 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
4960 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
4961 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
4962 if ( ptr == NULL ) return;
4964 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
4965 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
4966 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
4968 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
4969 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
4970 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
4971 "refreshAndPersist" : "refreshOnly" );
4973 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
4976 dd = si->si_interval;
4983 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
4984 INTERVALSTR, dd, hh, mm, ss );
4985 if ( len >= WHATSLEFT ) return;
4989 if ( si->si_got & GOT_RETRY ) {
4990 const char *space = "";
4991 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
4992 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
4993 for (i=0; si->si_retryinterval[i]; i++) {
4994 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
4995 (long) si->si_retryinterval[i] );
4997 if ( WHATSLEFT - 1 <= len ) return;
4999 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
5002 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
5003 if ( WHATSLEFT <= len ) return;
5007 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
5010 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
5013 if ( si->si_slimit ) {
5014 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
5015 if ( WHATSLEFT <= len ) return;
5019 if ( si->si_tlimit ) {
5020 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
5021 if ( WHATSLEFT <= len ) return;
5025 if ( si->si_syncdata ) {
5026 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
5027 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
5028 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
5029 ptr = lutil_strcopy( ptr, bc.bv_val );
5032 bc.bv_len = ptr - buf;
5034 ber_dupbv( bv, &bc );
5038 syncrepl_config( ConfigArgs *c )
5040 if (c->op == SLAP_CONFIG_EMIT) {
5041 if ( c->be->be_syncinfo ) {
5045 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
5046 syncrepl_unparse( si, &bv );
5047 ber_bvarray_add( &c->rvalue_vals, &bv );
5052 } else if ( c->op == LDAP_MOD_DELETE ) {
5054 if ( c->be->be_syncinfo ) {
5055 syncinfo_t *si, **sip;
5058 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
5060 if ( c->valx == -1 || i == c->valx ) {
5064 /* If the task is currently active, we have to leave
5065 * it running. It will exit on its own. This will only
5066 * happen when running on the cn=config DB.
5069 if ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
5072 /* There is no active thread, but we must still
5073 * ensure that no thread is (or will be) queued
5074 * while we removes the task.
5076 struct re_s *re = si->si_re;
5079 if ( si->si_conn ) {
5080 connection_client_stop( si->si_conn );
5084 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5085 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
5086 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5089 ldap_pvt_runqueue_remove( &slapd_rq, re );
5090 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5092 if ( ldap_pvt_thread_pool_retract( &connection_pool,
5093 re->routine, re ) > 0 )
5096 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5100 syncinfo_free( si, 0 );
5109 if ( !c->be->be_syncinfo ) {
5110 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5114 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5115 Debug(LDAP_DEBUG_ANY, "%s: "
5116 "syncrepl: database already shadowed.\n",
5120 return add_syncrepl( c );