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;
115 Avlnode *si_presentlist;
118 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
119 #ifdef ENABLE_REWRITE
120 struct rewrite_info *si_rewrite;
121 struct berval si_suffixm;
123 ldap_pvt_thread_mutex_t si_mutex;
126 static int syncuuid_cmp( const void *, const void * );
127 static int avl_presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
128 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
129 static int syncrepl_message_to_op(
130 syncinfo_t *, Operation *, LDAPMessage * );
131 static int syncrepl_message_to_entry(
132 syncinfo_t *, Operation *, LDAPMessage *,
133 Modifications **, Entry **, int );
134 static int syncrepl_entry(
135 syncinfo_t *, Operation*, Entry*,
136 Modifications**,int, struct berval*,
137 struct berval *cookieCSN );
138 static int syncrepl_updateCookie(
139 syncinfo_t *, Operation *,
140 struct sync_cookie * );
141 static struct berval * slap_uuidstr_from_normalized(
142 struct berval *, struct berval *, void * );
143 static int syncrepl_add_glue_ancestors(
144 Operation* op, Entry *e );
146 /* callback functions */
147 static int dn_callback( Operation *, SlapReply * );
148 static int nonpresent_callback( Operation *, SlapReply * );
149 static int null_callback( Operation *, SlapReply * );
151 static AttributeDescription *sync_descs[4];
154 syncrepl_state2str( int state )
157 case LDAP_SYNC_PRESENT:
163 case LDAP_SYNC_MODIFY:
166 case LDAP_SYNC_DELETE:
174 init_syncrepl(syncinfo_t *si)
177 char **attrs, **exattrs;
179 if ( !sync_descs[0] ) {
180 sync_descs[0] = slap_schema.si_ad_objectClass;
181 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
182 sync_descs[2] = slap_schema.si_ad_entryCSN;
183 sync_descs[3] = NULL;
186 if ( si->si_allattrs && si->si_allopattrs )
189 attrs = anlist2attrs( si->si_anlist );
192 if ( si->si_allattrs ) {
195 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
196 for ( j = i; attrs[j] != NULL; j++ ) {
199 attrs[j] = attrs[j+1];
205 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
206 attrs[i] = ch_strdup("*");
209 } else if ( si->si_allopattrs ) {
212 if ( is_at_operational( at_find( attrs[i] ) ) ) {
213 for ( j = i; attrs[j] != NULL; j++ ) {
216 attrs[j] = attrs[j+1];
222 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
223 attrs[i] = ch_strdup("+");
227 for ( i = 0; sync_descs[i] != NULL; i++ ) {
230 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
231 for ( k = j; attrs[k] != NULL; k++ ) {
234 attrs[k] = attrs[k+1];
242 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
244 if ( si->si_allopattrs ) {
245 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
247 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
251 if ( si->si_allopattrs ) {
252 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
254 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
255 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
263 if ( si->si_allattrs == si->si_allopattrs ) {
264 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
265 attrs[i++] = ch_strdup( "*" );
266 attrs[i++] = ch_strdup( "+" );
267 } else if ( si->si_allattrs && !si->si_allopattrs ) {
268 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
269 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
270 attrs[i++] = ch_strdup( "*" );
271 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
272 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
274 } else if ( !si->si_allattrs && si->si_allopattrs ) {
275 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
276 attrs[i++] = ch_strdup( "+" );
277 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
282 si->si_attrs = attrs;
284 exattrs = anlist2attrs( si->si_exanlist );
287 for ( n = 0; exattrs[n] != NULL; n++ ) ;
289 for ( i = 0; sync_descs[i] != NULL; i++ ) {
291 while ( exattrs[j] != NULL ) {
292 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
293 ch_free( exattrs[j] );
294 for ( k = j; exattrs[k] != NULL; k++ ) {
295 exattrs[k] = exattrs[k+1];
303 for ( i = 0; exattrs[i] != NULL; i++ ) {
304 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
306 if ( ( oc = si->si_anlist[j].an_oc ) ) {
308 while ( oc->soc_required[k] ) {
309 if ( !strcmp( exattrs[i],
310 oc->soc_required[k]->sat_cname.bv_val ) ) {
311 ch_free( exattrs[i] );
312 for ( l = i; exattrs[l]; l++ ) {
313 exattrs[l] = exattrs[l+1];
323 for ( i = 0; exattrs[i] != NULL; i++ ) ;
326 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
329 si->si_exattrs = exattrs;
332 typedef struct logschema {
334 struct berval ls_req;
335 struct berval ls_mod;
336 struct berval ls_newRdn;
337 struct berval ls_delRdn;
338 struct berval ls_newSup;
341 static logschema changelog_sc = {
343 BER_BVC("changeType"),
346 BER_BVC("deleteOldRDN"),
347 BER_BVC("newSuperior")
350 static logschema accesslog_sc = {
354 BER_BVC("reqNewRDN"),
355 BER_BVC("reqDeleteOldRDN"),
356 BER_BVC("reqNewSuperior")
364 BerElementBuffer berbuf;
365 BerElement *ber = (BerElement *)&berbuf;
366 LDAPControl c[3], *ctrls[4];
370 char **attrs, *lattrs[8];
375 /* setup LDAP SYNC control */
376 ber_init2( ber, NULL, LBER_USE_DER );
377 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
379 /* If we're using a log but we have no state, then fallback to
380 * normal mode for a full refresh.
382 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
383 si->si_logstate = SYNCLOG_FALLBACK;
386 /* Use the log parameters if we're in log mode */
387 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
389 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
393 lattrs[0] = ls->ls_dn.bv_val;
394 lattrs[1] = ls->ls_req.bv_val;
395 lattrs[2] = ls->ls_mod.bv_val;
396 lattrs[3] = ls->ls_newRdn.bv_val;
397 lattrs[4] = ls->ls_delRdn.bv_val;
398 lattrs[5] = ls->ls_newSup.bv_val;
399 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
403 base = si->si_logbase.bv_val;
404 filter = si->si_logfilterstr.bv_val;
407 scope = LDAP_SCOPE_SUBTREE;
410 base = si->si_base.bv_val;
411 filter = si->si_filterstr.bv_val;
412 attrs = si->si_attrs;
413 attrsonly = si->si_attrsonly;
414 scope = si->si_scope;
416 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
417 si->si_type = LDAP_SYNC_REFRESH_ONLY;
419 si->si_type = si->si_ctype;
422 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
424 ber_printf( ber, "{eOb}",
425 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
427 ber_printf( ber, "{eb}",
428 abs(si->si_type), rhint );
431 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == -1 ) {
436 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
437 c[0].ldctl_iscritical = si->si_type < 0;
440 c[1].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
441 BER_BVZERO( &c[1].ldctl_value );
442 c[1].ldctl_iscritical = 1;
445 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
446 c[2].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
447 c[2].ldctl_value = si->si_bindconf.sb_authzId;
448 c[2].ldctl_iscritical = 1;
455 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
456 ctrls, NULL, NULL, si->si_slimit, &si->si_msgid );
469 SlapReply rs = {REP_SEARCH};
470 int i, j, changed = 0;
472 /* Look for contextCSN from syncprov overlay. If
473 * there's no overlay, this will be a no-op. That means
474 * this is a pure consumer, so local changes will not be
475 * allowed, and all changes will already be reflected in
478 a.a_desc = slap_schema.si_ad_contextCSN;
480 e.e_name = si->si_contextdn;
481 e.e_nname = si->si_contextdn;
482 at[0].an_name = a.a_desc->ad_cname;
483 at[0].an_desc = a.a_desc;
484 BER_BVZERO( &at[1].an_name );
486 rs.sr_flags = REP_ENTRY_MODIFIABLE;
488 op->o_req_dn = e.e_name;
489 op->o_req_ndn = e.e_nname;
491 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
492 i = backend_operational( op, &rs );
493 if ( i == LDAP_SUCCESS && a.a_nvals ) {
494 int num = a.a_numvals;
495 /* check for differences */
496 if ( num != si->si_cookieState->cs_num ) {
499 for ( i=0; i<num; i++ ) {
500 if ( ber_bvcmp( &a.a_nvals[i],
501 &si->si_cookieState->cs_vals[i] )) {
508 ber_bvarray_free( si->si_cookieState->cs_vals );
509 ch_free( si->si_cookieState->cs_sids );
510 si->si_cookieState->cs_num = num;
511 si->si_cookieState->cs_vals = a.a_nvals;
512 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_nvals,
514 si->si_cookieState->cs_age++;
516 ber_bvarray_free( a.a_nvals );
518 ber_bvarray_free( a.a_vals );
520 /* See if the cookieState has changed due to anything outside
521 * this particular consumer. That includes other consumers in
522 * the same context, or local changes detected above.
524 if ( si->si_cookieState->cs_num > 0 && si->si_cookieAge !=
525 si->si_cookieState->cs_age ) {
526 if ( !si->si_syncCookie.numcsns ) {
527 ber_bvarray_free( si->si_syncCookie.ctxcsn );
528 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
529 si->si_cookieState->cs_vals, NULL );
532 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
533 /* bogus, just dup everything */
534 if ( si->si_syncCookie.sids[i] == -1 ) {
535 ber_bvarray_free( si->si_syncCookie.ctxcsn );
536 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
537 si->si_cookieState->cs_vals, NULL );
541 for (j=0; j<si->si_cookieState->cs_num; j++) {
542 if ( si->si_syncCookie.sids[i] !=
543 si->si_cookieState->cs_sids[j] )
545 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
546 &si->si_cookieState->cs_vals[j] ))
548 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
549 &si->si_cookieState->cs_vals[j] );
557 si->si_cookieAge = si->si_cookieState->cs_age;
558 ch_free( si->si_syncCookie.octet_str.bv_val );
559 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
560 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
561 si->si_syncCookie.sid );
563 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
573 int cmdline_cookie_found = 0;
575 struct sync_cookie *sc = NULL;
580 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
581 if ( rc != LDAP_SUCCESS ) {
584 op->o_protocol = LDAP_VERSION3;
586 /* Set SSF to strongest of TLS, SASL SSFs */
589 op->o_transport_ssf = 0;
591 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
592 == LDAP_SUCCESS && ssl != NULL )
594 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
596 #endif /* HAVE_TLS */
598 ber_len_t ssf; /* ITS#5403, 3864 LDAP_OPT_X_SASL_SSF probably ought
599 to use sasl_ssf_t but currently uses ber_len_t */
600 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &ssf )
602 op->o_sasl_ssf = ssf;
604 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
605 ? op->o_sasl_ssf : op->o_tls_ssf;
607 ldap_set_option( si->si_ld, LDAP_OPT_TIMELIMIT, &si->si_tlimit );
609 rc = LDAP_DEREF_NEVER; /* actually could allow DEREF_FINDING */
610 ldap_set_option( si->si_ld, LDAP_OPT_DEREF, &rc );
612 ldap_set_option( si->si_ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
614 si->si_syncCookie.rid = si->si_rid;
616 /* whenever there are multiple data sources possible, advertise sid */
617 si->si_syncCookie.sid = ( SLAP_MULTIMASTER( si->si_be ) || si->si_be != si->si_wbe ) ?
620 /* We've just started up, or the remote server hasn't sent us
621 * any meaningful state.
623 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ) ) {
626 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
627 if ( si->si_rid == sc->rid ) {
628 cmdline_cookie_found = 1;
633 if ( cmdline_cookie_found ) {
634 /* cookie is supplied in the command line */
636 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
638 /* ctxcsn wasn't parsed yet, do it now */
639 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
640 slap_sync_cookie_free( &si->si_syncCookie, 0 );
641 slap_dup_sync_cookie( &si->si_syncCookie, sc );
642 slap_sync_cookie_free( sc, 1 );
644 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
645 if ( !si->si_cookieState->cs_num ) {
646 /* get contextCSN shadow replica from database */
647 BerVarray csn = NULL;
648 void *ctx = op->o_tmpmemctx;
650 op->o_req_ndn = si->si_contextdn;
651 op->o_req_dn = op->o_req_ndn;
653 /* try to read stored contextCSN */
654 op->o_tmpmemctx = NULL;
655 backend_attribute( op, NULL, &op->o_req_ndn,
656 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
657 op->o_tmpmemctx = ctx;
659 si->si_cookieState->cs_vals = csn;
660 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
661 si->si_cookieState->cs_num = i;
662 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
665 if ( si->si_cookieState->cs_num ) {
666 ber_bvarray_free( si->si_syncCookie.ctxcsn );
667 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
668 si->si_cookieState->cs_vals, NULL )) {
670 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
673 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
674 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
676 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
677 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
679 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
682 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
683 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
684 si->si_syncCookie.sid );
686 /* ITS#6367: recreate the cookie so it has our SID, not our peer's */
687 ch_free( si->si_syncCookie.octet_str.bv_val );
688 BER_BVZERO( &si->si_syncCookie.octet_str );
689 /* Look for contextCSN from syncprov overlay. */
690 check_syncprov( op, si );
691 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
692 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
693 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
694 si->si_syncCookie.sid );
697 si->si_refreshDone = 0;
699 rc = ldap_sync_search( si, op->o_tmpmemctx );
701 if( rc != LDAP_SUCCESS ) {
702 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
703 "ldap_search_ext: %s (%d)\n",
704 si->si_ridtxt, ldap_err2string( rc ), rc );
710 ldap_unbind_ext( si->si_ld, NULL, NULL );
719 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
726 if ( sc1->numcsns < sc2->numcsns ) {
727 *which = sc1->numcsns;
731 for (j=0; j<sc2->numcsns; j++) {
732 for (i=0; i<sc1->numcsns; i++) {
733 if ( sc1->sids[i] != sc2->sids[j] )
735 value_match( &match, slap_schema.si_ad_entryCSN,
736 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
737 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
738 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
745 if ( i == sc1->numcsns ) {
746 /* sc2 has a sid sc1 lacks */
754 #define SYNC_PAUSED -3
761 BerElementBuffer berbuf;
762 BerElement *ber = (BerElement *)&berbuf;
764 LDAPMessage *msg = NULL;
766 struct sync_cookie syncCookie = { NULL };
767 struct sync_cookie syncCookie_req = { NULL };
772 Modifications *modlist = NULL;
776 struct timeval *tout_p = NULL;
777 struct timeval tout = { 0, 0 };
779 int refreshDeletes = 0;
781 if ( slapd_shutdown ) {
786 ber_init2( ber, NULL, LBER_USE_DER );
787 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
789 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
791 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
793 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
799 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
800 tout_p, &msg ) ) > 0 )
802 int match, punlock, syncstate;
803 struct berval *retdata, syncUUID, cookie = BER_BVNULL;
805 LDAPControl **rctrls = NULL, *rctrlp = NULL;
811 if ( slapd_shutdown ) {
815 switch( ldap_msgtype( msg ) ) {
816 case LDAP_RES_SEARCH_ENTRY:
817 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
818 /* we can't work without the control */
820 LDAPControl **next = NULL;
821 /* NOTE: make sure we use the right one;
822 * a better approach would be to run thru
823 * the whole list and take care of all */
824 /* NOTE: since we issue the search request,
825 * we should know what controls to expect,
826 * and there should be none apart from the
827 * sync-related control */
828 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
829 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
831 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
832 "got search entry with multiple "
833 "Sync State control\n", si->si_ridtxt, 0, 0 );
834 ldap_controls_free( rctrls );
839 if ( rctrlp == NULL ) {
840 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
841 "got search entry without "
842 "Sync State control\n", si->si_ridtxt, 0, 0 );
846 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
847 if ( ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID )
849 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s malformed message",
850 si->si_ridtxt, 0, 0 );
851 ldap_controls_free( rctrls );
855 /* FIXME: what if syncUUID is NULL or empty?
856 * (happens with back-sql...) */
857 if ( BER_BVISEMPTY( &syncUUID ) ) {
858 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
859 "got empty syncUUID with LDAP_SYNC_%s\n",
861 syncrepl_state2str( syncstate ), 0 );
862 ldap_controls_free( rctrls );
867 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
868 ber_scanf( ber, /*"{"*/ "m}", &cookie );
870 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
872 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
874 if ( !BER_BVISNULL( &cookie ) ) {
875 ch_free( syncCookie.octet_str.bv_val );
876 ber_dupbv( &syncCookie.octet_str, &cookie );
878 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
880 slap_parse_sync_cookie( &syncCookie, NULL );
881 if ( syncCookie.ctxcsn ) {
882 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
883 check_syncprov( op, si );
884 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
885 if ( si->si_cookieState->cs_sids[i] == sid ) {
886 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
887 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s\n",
888 si->si_ridtxt, syncCookie.ctxcsn->bv_val, 0 );
889 ldap_controls_free( rctrls );
896 /* check pending CSNs too */
897 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
898 if ( slapd_shutdown ) {
902 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
903 ldap_pvt_thread_yield();
905 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
906 if ( si->si_cookieState->cs_psids[i] == sid ) {
907 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
908 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s\n",
909 si->si_ridtxt, syncCookie.ctxcsn->bv_val, 0 );
910 ldap_controls_free( rctrls );
912 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
915 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
920 /* new SID, add it */
921 if ( i == si->si_cookieState->cs_pnum ) {
922 value_add( &si->si_cookieState->cs_pvals, syncCookie.ctxcsn );
923 si->si_cookieState->cs_pnum++;
924 si->si_cookieState->cs_psids = ch_realloc( si->si_cookieState->cs_psids, si->si_cookieState->cs_pnum * sizeof(int));
925 si->si_cookieState->cs_psids[i] = sid;
927 assert( punlock < 0 );
930 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
934 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
936 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
939 rc = syncrepl_updateCookie( si, op, &syncCookie );
940 } else switch ( rc ) {
941 case LDAP_ALREADY_EXISTS:
942 case LDAP_NO_SUCH_OBJECT:
943 case LDAP_NO_SUCH_ATTRIBUTE:
944 case LDAP_TYPE_OR_VALUE_EXISTS:
945 rc = LDAP_SYNC_REFRESH_REQUIRED;
946 si->si_logstate = SYNCLOG_FALLBACK;
947 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
952 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
953 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
955 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
956 syncstate, &syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
959 rc = syncrepl_updateCookie( si, op, &syncCookie );
962 if ( punlock >= 0 ) {
963 /* on failure, revert pending CSN */
964 if ( rc != LDAP_SUCCESS ) {
966 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
967 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
968 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
969 &si->si_cookieState->cs_vals[i] );
973 if ( i == si->si_cookieState->cs_num )
974 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
976 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
978 ldap_controls_free( rctrls );
980 slap_mods_free( modlist, 1 );
986 case LDAP_RES_SEARCH_REFERENCE:
987 Debug( LDAP_DEBUG_ANY,
988 "do_syncrep2: %s reference received error\n",
989 si->si_ridtxt, 0, 0 );
992 case LDAP_RES_SEARCH_RESULT:
993 Debug( LDAP_DEBUG_SYNC,
994 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
995 si->si_ridtxt, 0, 0 );
996 err = LDAP_OTHER; /* FIXME check parse result properly */
997 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
999 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
1000 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
1001 /* map old result code to registered code */
1002 err = LDAP_SYNC_REFRESH_REQUIRED;
1005 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
1006 if ( si->si_logstate == SYNCLOG_LOGGING ) {
1007 si->si_logstate = SYNCLOG_FALLBACK;
1013 Debug( LDAP_DEBUG_ANY,
1014 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1015 si->si_ridtxt, err, ldap_err2string( err ) );
1018 LDAPControl **next = NULL;
1019 /* NOTE: make sure we use the right one;
1020 * a better approach would be to run thru
1021 * the whole list and take care of all */
1022 /* NOTE: since we issue the search request,
1023 * we should know what controls to expect,
1024 * and there should be none apart from the
1025 * sync-related control */
1026 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1027 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1029 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1030 "got search result with multiple "
1031 "Sync State control\n", si->si_ridtxt, 0, 0 );
1032 ldap_controls_free( rctrls );
1038 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1040 ber_scanf( ber, "{" /*"}"*/);
1041 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1042 ber_scanf( ber, "m", &cookie );
1044 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1046 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1048 if ( !BER_BVISNULL( &cookie ) ) {
1049 ch_free( syncCookie.octet_str.bv_val );
1050 ber_dupbv( &syncCookie.octet_str, &cookie);
1052 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1054 slap_parse_sync_cookie( &syncCookie, NULL );
1055 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1058 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1060 ber_scanf( ber, "b", &refreshDeletes );
1062 ber_scanf( ber, /*"{"*/ "}" );
1064 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1065 slap_sync_cookie_free( &syncCookie_req, 0 );
1066 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1068 if ( !syncCookie.ctxcsn ) {
1070 } else if ( !syncCookie_req.ctxcsn ) {
1074 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1077 ldap_controls_free( rctrls );
1079 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1080 /* FIXME : different error behaviors according to
1081 * 1) err code : LDAP_BUSY ...
1082 * 2) on err policy : stop service, stop sync, retry
1084 if ( refreshDeletes == 0 && match < 0 &&
1085 err == LDAP_SUCCESS &&
1086 syncCookie_req.numcsns == syncCookie.numcsns )
1088 syncrepl_del_nonpresent( op, si, NULL,
1091 avl_free( si->si_presentlist, ch_free );
1092 si->si_presentlist = NULL;
1095 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1097 rc = syncrepl_updateCookie( si, op, &syncCookie );
1099 if ( err == LDAP_SUCCESS
1100 && si->si_logstate == SYNCLOG_FALLBACK ) {
1101 si->si_logstate = SYNCLOG_LOGGING;
1102 rc = LDAP_SYNC_REFRESH_REQUIRED;
1108 case LDAP_RES_INTERMEDIATE:
1111 rc = ldap_parse_intermediate( si->si_ld, msg,
1112 &retoid, &retdata, NULL, 0 );
1113 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1114 ber_init2( ber, retdata, LBER_USE_DER );
1116 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1118 case LDAP_TAG_SYNC_NEW_COOKIE:
1119 Debug( LDAP_DEBUG_SYNC,
1120 "do_syncrep2: %s %s - %s\n",
1122 "LDAP_RES_INTERMEDIATE",
1124 ber_scanf( ber, "tm", &tag, &cookie );
1125 Debug( LDAP_DEBUG_SYNC,
1126 "do_syncrep2: %s NEW_COOKIE: %s\n",
1129 if ( !BER_BVISNULL( &cookie ) ) {
1130 ch_free( syncCookie.octet_str.bv_val );
1131 ber_dupbv( &syncCookie.octet_str, &cookie );
1133 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1134 slap_parse_sync_cookie( &syncCookie, NULL );
1135 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1138 case LDAP_TAG_SYNC_REFRESH_DELETE:
1139 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1140 Debug( LDAP_DEBUG_SYNC,
1141 "do_syncrep2: %s %s - %s\n",
1143 "LDAP_RES_INTERMEDIATE",
1144 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1145 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1146 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1147 si->si_refreshDelete = 1;
1149 si->si_refreshPresent = 1;
1151 ber_scanf( ber, "t{" /*"}"*/, &tag );
1152 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1154 ber_scanf( ber, "m", &cookie );
1156 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1158 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1160 if ( !BER_BVISNULL( &cookie ) ) {
1161 ch_free( syncCookie.octet_str.bv_val );
1162 ber_dupbv( &syncCookie.octet_str, &cookie );
1164 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1166 slap_parse_sync_cookie( &syncCookie, NULL );
1167 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1170 /* Defaults to TRUE */
1171 if ( ber_peek_tag( ber, &len ) ==
1172 LDAP_TAG_REFRESHDONE )
1174 ber_scanf( ber, "b", &si->si_refreshDone );
1177 si->si_refreshDone = 1;
1179 ber_scanf( ber, /*"{"*/ "}" );
1181 case LDAP_TAG_SYNC_ID_SET:
1182 Debug( LDAP_DEBUG_SYNC,
1183 "do_syncrep2: %s %s - %s\n",
1185 "LDAP_RES_INTERMEDIATE",
1187 ber_scanf( ber, "t{" /*"}"*/, &tag );
1188 if ( ber_peek_tag( ber, &len ) ==
1189 LDAP_TAG_SYNC_COOKIE )
1191 ber_scanf( ber, "m", &cookie );
1193 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1195 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1197 if ( !BER_BVISNULL( &cookie ) ) {
1198 ch_free( syncCookie.octet_str.bv_val );
1199 ber_dupbv( &syncCookie.octet_str, &cookie );
1201 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1203 slap_parse_sync_cookie( &syncCookie, NULL );
1204 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1205 compare_csns( &syncCookie_req, &syncCookie, &m );
1208 if ( ber_peek_tag( ber, &len ) ==
1209 LDAP_TAG_REFRESHDELETES )
1211 ber_scanf( ber, "b", &refreshDeletes );
1214 ber_scanf( ber, "[W]", &syncUUIDs );
1215 ber_scanf( ber, /*"{"*/ "}" );
1216 if ( refreshDeletes ) {
1217 syncrepl_del_nonpresent( op, si, syncUUIDs,
1219 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1222 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1223 (void)avl_presentlist_insert( si, &syncUUIDs[i] );
1224 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1226 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1228 slap_sync_cookie_free( &syncCookie, 0 );
1231 Debug( LDAP_DEBUG_ANY,
1232 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1233 si->si_ridtxt, (long) si_tag, 0 );
1234 ldap_memfree( retoid );
1235 ber_bvfree( retdata );
1239 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1240 slap_sync_cookie_free( &syncCookie_req, 0 );
1241 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1243 if ( !syncCookie.ctxcsn ) {
1245 } else if ( !syncCookie_req.ctxcsn ) {
1249 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1253 if ( si->si_refreshPresent == 1 &&
1254 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1255 syncCookie_req.numcsns == syncCookie.numcsns ) {
1256 syncrepl_del_nonpresent( op, si, NULL,
1260 if ( syncCookie.ctxcsn )
1262 rc = syncrepl_updateCookie( si, op, &syncCookie);
1266 ldap_memfree( retoid );
1267 ber_bvfree( retdata );
1270 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1271 "unknown intermediate response (%d)\n",
1272 si->si_ridtxt, rc, 0 );
1273 ldap_memfree( retoid );
1274 ber_bvfree( retdata );
1279 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1280 "unknown message (0x%02lx)\n",
1282 (unsigned long)ldap_msgtype( msg ), 0 );
1286 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1287 slap_sync_cookie_free( &syncCookie_req, 0 );
1288 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1289 slap_sync_cookie_free( &syncCookie, 0 );
1291 ldap_msgfree( msg );
1293 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1294 slap_sync_cookie_free( &syncCookie, 0 );
1295 slap_sync_cookie_free( &syncCookie_req, 0 );
1302 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1307 if ( err != LDAP_SUCCESS ) {
1308 Debug( LDAP_DEBUG_ANY,
1309 "do_syncrep2: %s (%d) %s\n",
1310 si->si_ridtxt, err, ldap_err2string( err ) );
1313 slap_sync_cookie_free( &syncCookie, 0 );
1314 slap_sync_cookie_free( &syncCookie_req, 0 );
1316 if ( msg ) ldap_msgfree( msg );
1318 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1319 if ( si->si_conn ) {
1320 connection_client_stop( si->si_conn );
1323 ldap_unbind_ext( si->si_ld, NULL, NULL );
1335 struct re_s* rtask = arg;
1336 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1337 Connection conn = {0};
1338 OperationBuffer opbuf;
1340 int rc = LDAP_SUCCESS;
1343 int i, defer = 1, fail = 0, freeinfo = 0;
1348 if ( slapd_shutdown )
1351 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1353 /* Don't get stuck here while a pause is initiated */
1354 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1355 if ( slapd_shutdown )
1357 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1358 ldap_pvt_thread_yield();
1361 if ( si->si_ctype < 1 ) {
1365 switch( abs( si->si_type ) ) {
1366 case LDAP_SYNC_REFRESH_ONLY:
1367 case LDAP_SYNC_REFRESH_AND_PERSIST:
1370 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1374 if ( slapd_shutdown ) {
1376 if ( si->si_conn ) {
1377 connection_client_stop( si->si_conn );
1380 ldap_unbind_ext( si->si_ld, NULL, NULL );
1383 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1387 connection_fake_init( &conn, &opbuf, ctx );
1389 /* o_connids must be unique for slap_graduate_commit_csn */
1390 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1392 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1395 /* Coordinate contextCSN updates with any syncprov overlays
1396 * in use. This may be complicated by the use of the glue
1399 * Typically there is a single syncprov mastering the entire
1400 * glued tree. In that case, our contextCSN updates should
1401 * go to the master DB. But if there is no syncprov on the
1402 * master DB, then nothing special is needed here.
1404 * Alternatively, there may be individual syncprov overlays
1405 * on each glued branch. In that case, each syncprov only
1406 * knows about changes within its own branch. And so our
1407 * contextCSN updates should only go to the local DB.
1409 if ( !si->si_wbe ) {
1410 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1411 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1412 if ( overlay_is_inst( top_be, "syncprov" ))
1413 si->si_wbe = top_be;
1419 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1420 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1421 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1423 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1426 if ( !si->si_schemachecking )
1427 op->o_no_schema_check = 1;
1429 /* Establish session, do search */
1431 si->si_refreshDelete = 0;
1432 si->si_refreshPresent = 0;
1434 if ( si->si_presentlist ) {
1435 avl_free( si->si_presentlist, ch_free );
1436 si->si_presentlist = NULL;
1439 /* use main DB when retrieving contextCSN */
1440 op->o_bd = si->si_wbe;
1441 op->o_dn = op->o_bd->be_rootdn;
1442 op->o_ndn = op->o_bd->be_rootndn;
1443 rc = do_syncrep1( op, si );
1447 /* Process results */
1448 if ( rc == LDAP_SUCCESS ) {
1449 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1451 /* use current DB */
1453 op->o_dn = op->o_bd->be_rootdn;
1454 op->o_ndn = op->o_bd->be_rootndn;
1455 rc = do_syncrep2( op, si );
1456 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1457 rc = ldap_sync_search( si, op->o_tmpmemctx );
1462 /* We got deleted while running on cn=config */
1463 if ( si->si_ctype < 1 ) {
1464 if ( si->si_ctype == -1 ) {
1473 if ( rc != SYNC_PAUSED ) {
1474 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1475 /* If we succeeded, enable the connection for further listening.
1476 * If we failed, tear down the connection and reschedule.
1478 if ( rc == LDAP_SUCCESS ) {
1479 if ( si->si_conn ) {
1480 connection_client_enable( si->si_conn );
1482 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1484 } else if ( si->si_conn ) {
1488 if ( rc == -2 ) rc = 0;
1493 /* At this point, we have 5 cases:
1494 * 1) for any hard failure, give up and remove this task
1495 * 2) for ServerDown, reschedule this task to run later
1496 * 3) for threadpool pause, reschedule to run immediately
1497 * 4) for Refresh and Success, reschedule to run
1498 * 5) for Persist and Success, reschedule to defer
1500 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1502 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1503 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1507 connection_client_stop( si->si_conn );
1511 if ( rc == SYNC_PAUSED ) {
1512 rtask->interval.tv_sec = 0;
1513 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1514 rtask->interval.tv_sec = si->si_interval;
1516 } else if ( rc == LDAP_SUCCESS ) {
1517 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1520 rtask->interval.tv_sec = si->si_interval;
1521 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1522 if ( si->si_retrynum ) {
1523 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1524 si->si_retrynum[i] = si->si_retrynum_init[i];
1526 si->si_retrynum[i] = RETRYNUM_TAIL;
1529 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1530 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1534 if ( si->si_ctype < 1
1535 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1537 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1540 fail = RETRYNUM_TAIL;
1541 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1542 if ( si->si_retrynum[i] > 0 )
1543 si->si_retrynum[i]--;
1544 fail = si->si_retrynum[i];
1545 rtask->interval.tv_sec = si->si_retryinterval[i];
1546 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1547 slap_wake_listener();
1551 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1552 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1555 if ( fail == RETRYNUM_TAIL ) {
1556 Debug( LDAP_DEBUG_ANY,
1557 "do_syncrepl: %s rc %d quitting\n",
1558 si->si_ridtxt, rc, 0 );
1559 } else if ( fail > 0 ) {
1560 Debug( LDAP_DEBUG_ANY,
1561 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1562 si->si_ridtxt, rc, fail );
1564 Debug( LDAP_DEBUG_ANY,
1565 "do_syncrepl: %s rc %d retrying\n",
1566 si->si_ridtxt, rc, 0 );
1570 /* Do final delete cleanup */
1572 syncinfo_free( si, 0 );
1577 #ifdef ENABLE_REWRITE
1579 syncrepl_rewrite_dn(
1582 struct berval *sdn )
1587 nul = dn->bv_val[dn->bv_len];
1588 dn->bv_val[dn->bv_len] = 0;
1589 rc = rewrite( si->si_rewrite, SUFFIXM_CTX, dn->bv_val, &sdn->bv_val );
1590 dn->bv_val[dn->bv_len] = nul;
1592 if ( sdn->bv_val == dn->bv_val )
1594 else if ( rc == REWRITE_REGEXEC_OK && sdn->bv_val )
1595 sdn->bv_len = strlen( sdn->bv_val );
1598 #define REWRITE_VAL(si, ad, bv, bv2) \
1599 BER_BVZERO( &bv2 ); \
1600 if ( si->si_rewrite && ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName) \
1601 syncrepl_rewrite_dn( si, &bv, &bv2); \
1602 if ( BER_BVISNULL( &bv2 )) \
1603 ber_dupbv( &bv2, &bv )
1604 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1605 BER_BVZERO( &bv2 ); \
1606 if (si->si_rewrite) \
1607 syncrepl_rewrite_dn(si, &bv, &bv2); \
1608 rc = dnPrettyNormal( NULL, bv2.bv_val ? &bv2 : &bv, &dn, &ndn, op->o_tmpmemctx ); \
1611 #define REWRITE_VAL(si, ad, bv, bv2) ber_dupbv(&bv2, &bv)
1612 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1613 rc = dnPrettyNormal( NULL, &bv, &dn, &ndn, op->o_tmpmemctx )
1617 static slap_verbmasks modops[] = {
1618 { BER_BVC("add"), LDAP_REQ_ADD },
1619 { BER_BVC("delete"), LDAP_REQ_DELETE },
1620 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1621 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1626 syncrepl_accesslog_mods(
1628 struct berval *vals,
1629 struct Modifications **modres
1634 AttributeDescription *ad;
1635 struct berval bv, bv2;
1637 Modifications *mod = NULL, *modlist = NULL, **modtail;
1642 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1646 colon = ber_bvchr( &bv, ':' );
1652 bv.bv_len = colon - bv.bv_val;
1653 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1655 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1656 "Invalid attribute %s, %s\n",
1657 si->si_ridtxt, bv.bv_val, text );
1658 slap_mods_free( modlist, 1 );
1664 /* Ignore dynamically generated attrs */
1665 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1669 /* Ignore excluded attrs */
1670 if ( ldap_charray_inlist( si->si_exattrs,
1671 ad->ad_type->sat_cname.bv_val ) )
1677 case '+': op = LDAP_MOD_ADD; break;
1678 case '-': op = LDAP_MOD_DELETE; break;
1679 case '=': op = LDAP_MOD_REPLACE; break;
1680 case '#': op = LDAP_MOD_INCREMENT; break;
1684 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1685 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1688 mod->sml_next = NULL;
1690 mod->sml_type = ad->ad_cname;
1691 mod->sml_values = NULL;
1692 mod->sml_nvalues = NULL;
1693 mod->sml_numvals = 0;
1696 modtail = &mod->sml_next;
1698 if ( colon[2] == ' ' ) {
1699 bv.bv_val = colon + 3;
1700 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1701 REWRITE_VAL( si, ad, bv, bv2 );
1702 ber_bvarray_add( &mod->sml_values, &bv2 );
1711 syncrepl_changelog_mods(
1713 struct berval *vals,
1714 struct Modifications **modres
1717 return -1; /* FIXME */
1721 syncrepl_message_to_op(
1727 BerElement *ber = NULL;
1728 Modifications *modlist = NULL;
1730 SlapReply rs = { REP_RESULT };
1731 slap_callback cb = { NULL, null_callback, NULL, NULL };
1734 char txtbuf[SLAP_TEXT_BUFLEN];
1735 size_t textlen = sizeof txtbuf;
1737 struct berval bdn, dn = BER_BVNULL, ndn;
1738 struct berval bv, bv2, *bvals = NULL;
1739 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1740 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1741 psup = BER_BVNULL, nsup = BER_BVNULL;
1742 int rc, deleteOldRdn = 0, freeReqDn = 0;
1743 int do_graduate = 0;
1745 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1746 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1747 "Message type should be entry (%d)",
1748 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1752 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1757 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1759 if ( rc != LDAP_SUCCESS ) {
1760 Debug( LDAP_DEBUG_ANY,
1761 "syncrepl_message_to_op: %s dn get failed (%d)",
1762 si->si_ridtxt, rc, 0 );
1766 op->o_tag = LBER_DEFAULT;
1767 op->o_bd = si->si_wbe;
1769 if ( BER_BVISEMPTY( &bdn )) {
1770 Debug( LDAP_DEBUG_ANY,
1771 "syncrepl_message_to_op: %s got empty dn",
1772 si->si_ridtxt, 0, 0 );
1776 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
1778 if ( bv.bv_val == NULL )
1781 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
1783 REWRITE_DN( si, bdn, bv2, dn, ndn );
1784 if ( rc != LDAP_SUCCESS ) {
1785 Debug( LDAP_DEBUG_ANY,
1786 "syncrepl_message_to_op: %s "
1787 "dn \"%s\" normalization failed (%d)",
1788 si->si_ridtxt, bdn.bv_val, rc );
1793 ber_dupbv( &op->o_req_dn, &dn );
1794 ber_dupbv( &op->o_req_ndn, &ndn );
1795 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1796 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1798 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
1799 int i = verb_to_mask( bvals[0].bv_val, modops );
1801 Debug( LDAP_DEBUG_ANY,
1802 "syncrepl_message_to_op: %s unknown op %s",
1803 si->si_ridtxt, bvals[0].bv_val, 0 );
1808 op->o_tag = modops[i].mask;
1809 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
1810 /* Parse attribute into modlist */
1811 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
1812 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
1814 rc = syncrepl_changelog_mods( si, bvals, &modlist );
1816 if ( rc ) goto done;
1817 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
1819 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
1820 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
1823 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
1825 } else if ( !ber_bvstrcasecmp( &bv,
1826 &slap_schema.si_ad_entryCSN->ad_cname ) )
1828 slap_queue_csn( op, bvals );
1834 /* If we didn't get a mod type or a target DN, bail out */
1835 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
1840 op->o_callback = &cb;
1841 slap_op_time( &op->o_time, &op->o_tincr );
1843 switch( op->o_tag ) {
1845 case LDAP_REQ_MODIFY:
1846 /* If we didn't get required data, bail */
1847 if ( !modlist ) goto done;
1849 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
1851 if ( rc != LDAP_SUCCESS ) {
1852 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1853 "mods check (%s)\n",
1854 si->si_ridtxt, text, 0 );
1858 if ( op->o_tag == LDAP_REQ_ADD ) {
1859 Entry *e = entry_alloc();
1861 op->ora_e->e_name = op->o_req_dn;
1862 op->ora_e->e_nname = op->o_req_ndn;
1864 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1865 if( rc != LDAP_SUCCESS ) {
1866 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1867 "mods2entry (%s)\n",
1868 si->si_ridtxt, text, 0 );
1870 rc = op->o_bd->be_add( op, &rs );
1871 Debug( LDAP_DEBUG_SYNC,
1872 "syncrepl_message_to_op: %s be_add %s (%d)\n",
1873 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1876 if ( e == op->ora_e )
1877 be_entry_release_w( op, op->ora_e );
1879 op->orm_modlist = modlist;
1880 op->o_bd = si->si_wbe;
1881 rc = op->o_bd->be_modify( op, &rs );
1882 modlist = op->orm_modlist;
1883 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1884 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
1885 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1886 op->o_bd = si->si_be;
1890 case LDAP_REQ_MODRDN:
1891 if ( BER_BVISNULL( &rdn ) ) goto done;
1893 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
1896 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
1899 if ( !BER_BVISNULL( &sup ) ) {
1900 REWRITE_DN( si, sup, bv2, psup, nsup );
1903 op->orr_newSup = &psup;
1904 op->orr_nnewSup = ⊅
1906 op->orr_newSup = NULL;
1907 op->orr_nnewSup = NULL;
1909 op->orr_newrdn = prdn;
1910 op->orr_nnewrdn = nrdn;
1911 op->orr_deleteoldrdn = deleteOldRdn;
1912 op->orr_modlist = NULL;
1913 if ( slap_modrdn2mods( op, &rs ) ) {
1917 /* Append modlist for operational attrs */
1921 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
1923 m->sml_next = modlist;
1926 rc = op->o_bd->be_modrdn( op, &rs );
1927 slap_mods_free( op->orr_modlist, 1 );
1928 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1929 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
1930 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1933 case LDAP_REQ_DELETE:
1934 rc = op->o_bd->be_delete( op, &rs );
1935 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1936 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
1937 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1943 slap_graduate_commit_csn( op );
1944 op->o_bd = si->si_be;
1945 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1946 BER_BVZERO( &op->o_csn );
1948 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1950 if ( !BER_BVISNULL( &rdn ) ) {
1951 if ( !BER_BVISNULL( &nsup ) ) {
1952 ch_free( nsup.bv_val );
1954 if ( !BER_BVISNULL( &psup ) ) {
1955 ch_free( psup.bv_val );
1957 if ( !BER_BVISNULL( &nrdn ) ) {
1958 ch_free( nrdn.bv_val );
1960 if ( !BER_BVISNULL( &prdn ) ) {
1961 ch_free( prdn.bv_val );
1965 ch_free( op->o_req_ndn.bv_val );
1966 ch_free( op->o_req_dn.bv_val );
1973 syncrepl_message_to_entry(
1977 Modifications **modlist,
1983 BerElement *ber = NULL;
1986 Modifications **modtail = modlist;
1989 char txtbuf[SLAP_TEXT_BUFLEN];
1990 size_t textlen = sizeof txtbuf;
1992 struct berval bdn = BER_BVNULL, dn, ndn, bv2;
1997 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1998 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
1999 "Message type should be entry (%d)",
2000 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2004 op->o_tag = LDAP_REQ_ADD;
2006 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2007 if ( rc != LDAP_SUCCESS ) {
2008 Debug( LDAP_DEBUG_ANY,
2009 "syncrepl_message_to_entry: %s dn get failed (%d)",
2010 si->si_ridtxt, rc, 0 );
2014 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
2015 Debug( LDAP_DEBUG_ANY,
2016 "syncrepl_message_to_entry: %s got empty dn",
2017 si->si_ridtxt, 0, 0 );
2021 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
2022 /* NOTE: this could be done even before decoding the DN,
2023 * although encoding errors wouldn't be detected */
2028 if ( entry == NULL ) {
2032 REWRITE_DN( si, bdn, bv2, dn, ndn );
2033 if ( rc != LDAP_SUCCESS ) {
2034 /* One of the things that could happen is that the schema
2035 * is not lined-up; this could result in unknown attributes.
2036 * A value non conformant to the syntax should be unlikely,
2037 * except when replicating between different versions
2038 * of the software, or when syntax validation bugs are fixed
2040 Debug( LDAP_DEBUG_ANY,
2041 "syncrepl_message_to_entry: "
2042 "%s dn \"%s\" normalization failed (%d)",
2043 si->si_ridtxt, bdn.bv_val, rc );
2047 ber_dupbv( &op->o_req_dn, &dn );
2048 ber_dupbv( &op->o_req_ndn, &ndn );
2049 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2050 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2052 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
2055 e->e_name = op->o_req_dn;
2056 e->e_nname = op->o_req_ndn;
2058 while ( ber_remaining( ber ) ) {
2059 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
2060 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2065 /* Drop all updates to the contextCSN of the context entry
2068 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2069 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2070 ber_bvarray_free( tmp.sml_values );
2074 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2076 mod->sml_op = LDAP_MOD_REPLACE;
2078 mod->sml_next = NULL;
2079 mod->sml_desc = NULL;
2080 mod->sml_type = tmp.sml_type;
2081 mod->sml_values = tmp.sml_values;
2082 mod->sml_nvalues = NULL;
2083 mod->sml_numvals = 0; /* slap_mods_check will set this */
2085 #ifdef ENABLE_REWRITE
2086 if (si->si_rewrite) {
2087 AttributeDescription *ad = NULL;
2088 slap_bv2ad( &tmp.sml_type, &ad, &text );
2091 mod->sml_type = ad->ad_cname;
2092 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
2094 for ( i = 0; tmp.sml_values[i].bv_val; i++ ) {
2095 syncrepl_rewrite_dn( si, &tmp.sml_values[i], &bv2);
2096 if ( !BER_BVISNULL( &bv2 )) {
2097 ber_memfree( tmp.sml_values[i].bv_val );
2098 tmp.sml_values[i] = bv2;
2106 modtail = &mod->sml_next;
2109 if ( *modlist == NULL ) {
2110 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2111 si->si_ridtxt, 0, 0 );
2116 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2118 if ( rc != LDAP_SUCCESS ) {
2119 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2120 si->si_ridtxt, text, 0 );
2124 /* Strip out dynamically generated attrs */
2125 for ( modtail = modlist; *modtail ; ) {
2127 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2128 *modtail = mod->sml_next;
2129 slap_mod_free( &mod->sml_mod, 0 );
2132 modtail = &mod->sml_next;
2136 /* Strip out attrs in exattrs list */
2137 for ( modtail = modlist; *modtail ; ) {
2139 if ( ldap_charray_inlist( si->si_exattrs,
2140 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2142 *modtail = mod->sml_next;
2143 slap_mod_free( &mod->sml_mod, 0 );
2146 modtail = &mod->sml_next;
2150 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2151 if( rc != LDAP_SUCCESS ) {
2152 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2153 si->si_ridtxt, text, 0 );
2158 if ( rc != LDAP_SUCCESS ) {
2170 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2172 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2173 * entry if a previous refresh was interrupted before sending us a new
2174 * context state. We try to compare the new entry to the existing entry
2175 * and ignore the new entry if they are the same.
2177 * Also, we may get an update where the entryDN has changed, due to
2178 * a ModDn on the provider. We detect this as well, so we can issue
2179 * the corresponding operation locally.
2181 * In the case of a modify, we get a list of all the attributes
2182 * in the original entry. Rather than deleting the entry and re-adding it,
2183 * we issue a Modify request that deletes all the attributes and adds all
2184 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2187 * We otherwise distinguish ModDN from Modify; in the case of
2188 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2189 * operational attributes from the entry, and do a regular ModDN.
2191 typedef struct dninfo {
2195 struct berval nnewSup;
2196 int renamed; /* Was an existing entry renamed? */
2197 int delOldRDN; /* Was old RDN deleted? */
2198 Modifications **modlist; /* the modlist we received */
2199 Modifications *mods; /* the modlist we compared */
2200 Attribute *oldNattr; /* old naming attr */
2201 AttributeDescription *oldDesc; /* for renames */
2202 AttributeDescription *newDesc; /* for renames */
2205 /* return 1 if inserted, 0 otherwise */
2207 avl_presentlist_insert(
2209 struct berval *syncUUID )
2211 struct berval *syncuuid_bv = ch_malloc( sizeof( struct berval ) + syncUUID->bv_len + 1 );
2213 syncuuid_bv->bv_len = syncUUID->bv_len;
2214 syncuuid_bv->bv_val = (char *)&syncuuid_bv[1];
2215 AC_MEMCPY( syncuuid_bv->bv_val, syncUUID->bv_val, syncUUID->bv_len );
2216 syncuuid_bv->bv_val[ syncuuid_bv->bv_len ] = '\0';
2218 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
2219 syncuuid_cmp, avl_dup_error ) )
2221 ch_free( syncuuid_bv );
2233 Modifications** modlist,
2235 struct berval* syncUUID,
2236 struct berval* syncCSN )
2238 Backend *be = op->o_bd;
2239 slap_callback cb = { NULL, NULL, NULL, NULL };
2240 int syncuuid_inserted = 0;
2241 struct berval syncUUID_strrep = BER_BVNULL;
2243 SlapReply rs_search = {REP_RESULT};
2245 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2246 int rc = LDAP_SUCCESS;
2248 struct berval pdn = BER_BVNULL;
2253 Debug( LDAP_DEBUG_SYNC,
2254 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2255 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2257 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2258 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2259 syncuuid_inserted = avl_presentlist_insert( si, syncUUID );
2263 if ( syncstate == LDAP_SYNC_PRESENT ) {
2265 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2266 if ( entry == NULL ) {
2271 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
2272 if ( syncstate != LDAP_SYNC_DELETE ) {
2273 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2276 /* add if missing */
2277 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2278 &syncUUID_strrep, syncUUID );
2280 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2281 /* replace only if necessary */
2282 if ( a->a_nvals != a->a_vals ) {
2283 ber_memfree( a->a_nvals[0].bv_val );
2284 ber_dupbv( &a->a_nvals[0], syncUUID );
2286 ber_memfree( a->a_vals[0].bv_val );
2287 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
2291 f.f_choice = LDAP_FILTER_EQUALITY;
2293 ava.aa_desc = slap_schema.si_ad_entryUUID;
2294 ava.aa_value = *syncUUID;
2296 if ( syncuuid_inserted ) {
2297 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2298 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
2300 op->ors_filter = &f;
2302 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
2303 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2304 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2305 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2306 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2307 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
2308 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2309 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2311 op->o_tag = LDAP_REQ_SEARCH;
2312 op->ors_scope = LDAP_SCOPE_SUBTREE;
2313 op->ors_deref = LDAP_DEREF_NEVER;
2315 /* get the entry for this UUID */
2316 #ifdef ENABLE_REWRITE
2317 if ( si->si_rewrite ) {
2318 op->o_req_dn = si->si_suffixm;
2319 op->o_req_ndn = si->si_suffixm;
2323 op->o_req_dn = si->si_base;
2324 op->o_req_ndn = si->si_base;
2327 op->o_time = slap_get_time();
2328 op->ors_tlimit = SLAP_NO_LIMIT;
2330 op->ors_limit = NULL;
2332 op->ors_attrs = slap_anlist_all_attributes;
2333 op->ors_attrsonly = 0;
2335 /* set callback function */
2336 op->o_callback = &cb;
2337 cb.sc_response = dn_callback;
2338 cb.sc_private = &dni;
2339 dni.new_entry = entry;
2340 dni.modlist = modlist;
2342 rc = be->be_search( op, &rs_search );
2343 Debug( LDAP_DEBUG_SYNC,
2344 "syncrepl_entry: %s be_search (%d)\n",
2345 si->si_ridtxt, rc, 0 );
2347 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2348 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2351 cb.sc_response = null_callback;
2354 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2355 Debug( LDAP_DEBUG_SYNC,
2356 "syncrepl_entry: %s %s\n",
2357 si->si_ridtxt, entry->e_name.bv_val, 0 );
2359 Debug( LDAP_DEBUG_SYNC,
2360 "syncrepl_entry: %s %s\n",
2361 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2364 assert( BER_BVISNULL( &op->o_csn ) );
2366 slap_queue_csn( op, syncCSN );
2369 slap_op_time( &op->o_time, &op->o_tincr );
2370 switch ( syncstate ) {
2372 case LDAP_SYNC_MODIFY:
2373 if ( BER_BVISNULL( &op->o_csn ))
2376 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2378 /* FIXME: op->o_csn is assumed to be
2379 * on the thread's slab; this needs
2380 * to be cleared ASAP.
2381 * What happens if already present?
2383 assert( BER_BVISNULL( &op->o_csn ) );
2384 op->o_csn = a->a_vals[0];
2389 if ( BER_BVISNULL( &dni.dn ) ) {
2390 SlapReply rs_add = {REP_RESULT};
2392 op->o_req_dn = entry->e_name;
2393 op->o_req_ndn = entry->e_nname;
2394 op->o_tag = LDAP_REQ_ADD;
2396 op->o_bd = si->si_wbe;
2398 rc = op->o_bd->be_add( op, &rs_add );
2399 Debug( LDAP_DEBUG_SYNC,
2400 "syncrepl_entry: %s be_add %s (%d)\n",
2401 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2402 switch ( rs_add.sr_err ) {
2404 if ( op->ora_e == entry ) {
2405 be_entry_release_w( op, entry );
2411 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2412 * only if the suffix entry is not present */
2413 case LDAP_NO_SUCH_OBJECT:
2414 rc = syncrepl_add_glue( op, entry );
2418 /* if an entry was added via syncrepl_add_glue(),
2419 * it likely has no entryUUID, so the previous
2420 * be_search() doesn't find it. In this case,
2421 * give syncrepl a chance to modify it. Also
2422 * allow for entries that were recreated with the
2423 * same DN but a different entryUUID.
2425 case LDAP_ALREADY_EXISTS:
2427 Operation op2 = *op;
2428 SlapReply rs2 = { REP_RESULT };
2429 slap_callback cb2 = { 0 };
2432 op2.o_tag = LDAP_REQ_SEARCH;
2433 op2.o_req_dn = entry->e_name;
2434 op2.o_req_ndn = entry->e_nname;
2435 op2.ors_scope = LDAP_SCOPE_BASE;
2436 op2.ors_deref = LDAP_DEREF_NEVER;
2437 op2.ors_attrs = slap_anlist_all_attributes;
2438 op2.ors_attrsonly = 0;
2439 op2.ors_limit = NULL;
2441 op2.ors_tlimit = SLAP_NO_LIMIT;
2443 f.f_choice = LDAP_FILTER_PRESENT;
2444 f.f_desc = slap_schema.si_ad_objectClass;
2445 op2.ors_filter = &f;
2446 op2.ors_filterstr = generic_filterstr;
2448 op2.o_callback = &cb2;
2449 cb2.sc_response = dn_callback;
2450 cb2.sc_private = &dni;
2452 rc = be->be_search( &op2, &rs2 );
2453 if ( rc ) goto done;
2456 slap_op_time( &op->o_time, &op->o_tincr );
2462 Debug( LDAP_DEBUG_ANY,
2463 "syncrepl_entry: %s be_add %s failed (%d)\n",
2464 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
2472 op->o_req_dn = dni.dn;
2473 op->o_req_ndn = dni.ndn;
2474 if ( dni.renamed ) {
2475 struct berval noldp, newp;
2476 Modifications *mod, **modtail, **ml, *m2;
2477 int i, got_replace = 0, just_rename = 0;
2478 SlapReply rs_modify = {REP_RESULT};
2480 op->o_tag = LDAP_REQ_MODRDN;
2481 dnRdn( &entry->e_name, &op->orr_newrdn );
2482 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2484 if ( !BER_BVISNULL( &dni.nnewSup )) {
2485 dnParent( &entry->e_name, &newp );
2486 op->orr_newSup = &newp;
2487 op->orr_nnewSup = &dni.nnewSup;
2489 op->orr_newSup = NULL;
2490 op->orr_nnewSup = NULL;
2492 op->orr_deleteoldrdn = dni.delOldRDN;
2493 op->orr_modlist = NULL;
2494 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2498 /* Drop the RDN-related mods from this op, because their
2499 * equivalents were just setup by slap_modrdn2mods.
2501 * If delOldRDN is TRUE then we should see a delete modop
2502 * for oldDesc. We might see a replace instead.
2503 * delete with no values: therefore newDesc != oldDesc.
2504 * if oldNattr had only one value, then Drop this op.
2505 * delete with 1 value: can only be the oldRDN value. Drop op.
2506 * delete with N values: Drop oldRDN value, keep remainder.
2507 * replace with 1 value: if oldNattr had only one value and
2508 * newDesc == oldDesc, Drop this op.
2509 * Any other cases must be left intact.
2511 * We should also see an add modop for newDesc. (But not if
2512 * we got a replace modop due to delOldRDN.) If it has
2513 * multiple values, we'll have to drop the new RDN value.
2515 modtail = &op->orr_modlist;
2516 if ( dni.delOldRDN ) {
2517 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2518 if ( (*ml)->sml_desc == dni.oldDesc ) {
2520 if ( mod->sml_op == LDAP_MOD_REPLACE &&
2521 dni.oldDesc != dni.newDesc ) {
2522 /* This Replace is due to other Mods.
2527 if ( mod->sml_numvals <= 1 &&
2528 dni.oldNattr->a_numvals == 1 &&
2529 ( mod->sml_op == LDAP_MOD_DELETE ||
2530 mod->sml_op == LDAP_MOD_REPLACE )) {
2531 if ( mod->sml_op == LDAP_MOD_REPLACE )
2534 *ml = mod->sml_next;
2535 mod->sml_next = NULL;
2536 slap_mods_free( mod, 1 );
2539 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
2541 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2542 if ( m2->sml_desc == dni.oldDesc &&
2543 m2->sml_op == LDAP_MOD_DELETE ) break;
2545 for ( i=0; i<mod->sml_numvals; i++ ) {
2546 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2548 ch_free( mod->sml_values[i].bv_val );
2549 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2550 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2551 if ( mod->sml_nvalues ) {
2552 ch_free( mod->sml_nvalues[i].bv_val );
2553 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2554 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2559 if ( !mod->sml_numvals ) {
2561 *ml = mod->sml_next;
2562 mod->sml_next = NULL;
2563 slap_mods_free( mod, 1 );
2569 if ( !got_replace ) {
2570 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2571 if ( (*ml)->sml_desc == dni.newDesc ) {
2573 if ( mod->sml_op != LDAP_MOD_ADD )
2575 if ( mod->sml_numvals == 1 ) {
2577 *ml = mod->sml_next;
2578 mod->sml_next = NULL;
2579 slap_mods_free( mod, 1 );
2582 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2583 if ( m2->sml_desc == dni.oldDesc &&
2584 m2->sml_op == SLAP_MOD_SOFTADD ) break;
2586 for ( i=0; i<mod->sml_numvals; i++ ) {
2587 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2589 ch_free( mod->sml_values[i].bv_val );
2590 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2591 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2592 if ( mod->sml_nvalues ) {
2593 ch_free( mod->sml_nvalues[i].bv_val );
2594 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2595 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2605 /* RDNs must be NUL-terminated for back-ldap */
2606 noldp = op->orr_newrdn;
2607 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
2608 noldp = op->orr_nnewrdn;
2609 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
2611 /* Setup opattrs too */
2613 static AttributeDescription *nullattr = NULL;
2614 static AttributeDescription **const opattrs[] = {
2615 &slap_schema.si_ad_entryCSN,
2616 &slap_schema.si_ad_modifiersName,
2617 &slap_schema.si_ad_modifyTimestamp,
2620 AttributeDescription *opattr;
2624 /* pull mod off incoming modlist */
2625 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
2626 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
2628 if ( (*ml)->sml_desc == opattr ) {
2630 *ml = mod->sml_next;
2631 mod->sml_next = NULL;
2633 modtail = &mod->sml_next;
2638 /* If there are still Modifications left, put the opattrs
2639 * back, and let be_modify run. Otherwise, append the opattrs
2640 * to the orr_modlist.
2644 /* don't set a CSN for the rename op */
2646 slap_graduate_commit_csn( op );
2648 mod = op->orr_modlist;
2651 for ( ; mod->sml_next; mod=mod->sml_next );
2654 op->o_bd = si->si_wbe;
2656 rs_reinit( &rs_modify, REP_RESULT );
2657 rc = op->o_bd->be_modrdn( op, &rs_modify );
2659 /* NOTE: noSuchObject should result because the new superior
2660 * has not been added yet (ITS#6472) */
2661 if ( rc == LDAP_NO_SUCH_OBJECT && !BER_BVISNULL( op->orr_nnewSup )) {
2662 Operation op2 = *op;
2663 rc = syncrepl_add_glue_ancestors( &op2, entry );
2664 if ( rc == LDAP_SUCCESS ) {
2669 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
2670 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
2672 slap_mods_free( op->orr_modlist, 1 );
2673 Debug( LDAP_DEBUG_SYNC,
2674 "syncrepl_entry: %s be_modrdn %s (%d)\n",
2675 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2677 /* Renamed entries may still have other mods so just fallthru */
2678 op->o_req_dn = entry->e_name;
2679 op->o_req_ndn = entry->e_nname;
2680 /* Use CSN on the modify */
2684 slap_queue_csn( op, syncCSN );
2687 SlapReply rs_modify = {REP_RESULT};
2689 op->o_tag = LDAP_REQ_MODIFY;
2690 op->orm_modlist = dni.mods;
2691 op->orm_no_opattrs = 1;
2692 op->o_bd = si->si_wbe;
2694 rc = op->o_bd->be_modify( op, &rs_modify );
2695 slap_mods_free( op->orm_modlist, 1 );
2696 op->orm_no_opattrs = 0;
2697 Debug( LDAP_DEBUG_SYNC,
2698 "syncrepl_entry: %s be_modify %s (%d)\n",
2699 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2700 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2701 Debug( LDAP_DEBUG_ANY,
2702 "syncrepl_entry: %s be_modify failed (%d)\n",
2703 si->si_ridtxt, rs_modify.sr_err, 0 );
2707 } else if ( !dni.renamed ) {
2708 Debug( LDAP_DEBUG_SYNC,
2709 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
2710 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
2712 slap_graduate_commit_csn( op );
2717 case LDAP_SYNC_DELETE :
2718 if ( !BER_BVISNULL( &dni.dn ) ) {
2719 SlapReply rs_delete = {REP_RESULT};
2720 op->o_req_dn = dni.dn;
2721 op->o_req_ndn = dni.ndn;
2722 op->o_tag = LDAP_REQ_DELETE;
2723 op->o_bd = si->si_wbe;
2724 rc = op->o_bd->be_delete( op, &rs_delete );
2725 Debug( LDAP_DEBUG_SYNC,
2726 "syncrepl_entry: %s be_delete %s (%d)\n",
2727 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2729 while ( rs_delete.sr_err == LDAP_SUCCESS
2730 && op->o_delete_glue_parent ) {
2731 op->o_delete_glue_parent = 0;
2732 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2733 slap_callback cb = { NULL };
2734 cb.sc_response = slap_null_cb;
2735 dnParent( &op->o_req_ndn, &pdn );
2737 op->o_req_ndn = pdn;
2738 op->o_callback = &cb;
2739 rs_reinit( &rs_delete, REP_RESULT );
2740 op->o_bd->be_delete( op, &rs_delete );
2751 Debug( LDAP_DEBUG_ANY,
2752 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
2757 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2758 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2759 BER_BVZERO( &syncUUID_strrep );
2761 if ( !BER_BVISNULL( &dni.ndn ) ) {
2762 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2764 if ( !BER_BVISNULL( &dni.dn ) ) {
2765 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2768 entry_free( entry );
2771 slap_graduate_commit_csn( op );
2773 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
2774 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2776 BER_BVZERO( &op->o_csn );
2780 static struct berval gcbva[] = {
2786 #define NP_DELETE_ONE 2
2789 syncrepl_del_nonpresent(
2793 struct sync_cookie *sc,
2796 Backend* be = op->o_bd;
2797 slap_callback cb = { NULL };
2798 struct nonpresent_entry *np_list, *np_prev;
2800 AttributeName an[2];
2802 struct berval pdn = BER_BVNULL;
2805 #ifdef ENABLE_REWRITE
2806 if ( si->si_rewrite ) {
2807 op->o_req_dn = si->si_suffixm;
2808 op->o_req_ndn = si->si_suffixm;
2812 op->o_req_dn = si->si_base;
2813 op->o_req_ndn = si->si_base;
2816 cb.sc_response = nonpresent_callback;
2819 op->o_callback = &cb;
2820 op->o_tag = LDAP_REQ_SEARCH;
2821 op->ors_scope = si->si_scope;
2822 op->ors_deref = LDAP_DEREF_NEVER;
2823 op->o_time = slap_get_time();
2824 op->ors_tlimit = SLAP_NO_LIMIT;
2829 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
2832 op->ors_attrsonly = 1;
2833 op->ors_attrs = slap_anlist_no_attrs;
2834 op->ors_limit = NULL;
2835 op->ors_filter = &uf;
2838 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2840 uf.f_choice = LDAP_FILTER_EQUALITY;
2841 si->si_refreshDelete |= NP_DELETE_ONE;
2843 for (i=0; uuids[i].bv_val; i++) {
2844 SlapReply rs_search = {REP_RESULT};
2847 uf.f_av_value = uuids[i];
2848 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2849 rc = be->be_search( op, &rs_search );
2850 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2852 si->si_refreshDelete ^= NP_DELETE_ONE;
2856 AttributeAssertion mmaa;
2857 SlapReply rs_search = {REP_RESULT};
2859 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2860 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2861 an[0].an_desc = slap_schema.si_ad_entryUUID;
2863 op->ors_slimit = SLAP_NO_LIMIT;
2864 op->ors_tlimit = SLAP_NO_LIMIT;
2865 op->ors_limit = NULL;
2866 op->ors_attrsonly = 0;
2867 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
2868 /* In multimaster, updates can continue to arrive while
2869 * we're searching. Limit the search result to entries
2870 * older than our newest cookie CSN.
2872 if ( SLAP_MULTIMASTER( op->o_bd )) {
2877 f->f_choice = LDAP_FILTER_AND;
2878 f->f_next = op->ors_filter;
2882 f->f_choice = LDAP_FILTER_LE;
2884 f->f_av_desc = slap_schema.si_ad_entryCSN;
2886 BER_BVZERO( &f->f_av_value );
2887 for ( i=0; i<sc->numcsns; i++ ) {
2888 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
2889 f->f_av_value = sc->ctxcsn[i];
2891 of = op->ors_filter;
2892 op->ors_filter = mmf;
2893 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2896 op->ors_filterstr = si->si_filterstr;
2898 op->o_nocaching = 1;
2901 rc = be->be_search( op, &rs_search );
2902 if ( SLAP_MULTIMASTER( op->o_bd )) {
2903 op->ors_filter = of;
2905 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
2906 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
2907 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2912 op->o_nocaching = 0;
2914 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2916 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
2917 csn = sc->ctxcsn[m];
2919 csn = si->si_syncCookie.ctxcsn[0];
2922 op->o_bd = si->si_wbe;
2923 slap_queue_csn( op, &csn );
2925 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2926 while ( np_list != NULL ) {
2927 SlapReply rs_delete = {REP_RESULT};
2929 LDAP_LIST_REMOVE( np_list, npe_link );
2931 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2932 op->o_tag = LDAP_REQ_DELETE;
2933 op->o_callback = &cb;
2934 cb.sc_response = null_callback;
2936 op->o_req_dn = *np_prev->npe_name;
2937 op->o_req_ndn = *np_prev->npe_nname;
2938 rc = op->o_bd->be_delete( op, &rs_delete );
2939 Debug( LDAP_DEBUG_SYNC,
2940 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
2941 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2943 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2944 SlapReply rs_modify = {REP_RESULT};
2945 Modifications mod1, mod2;
2946 mod1.sml_op = LDAP_MOD_REPLACE;
2948 mod1.sml_desc = slap_schema.si_ad_objectClass;
2949 mod1.sml_type = mod1.sml_desc->ad_cname;
2950 mod1.sml_numvals = 2;
2951 mod1.sml_values = &gcbva[0];
2952 mod1.sml_nvalues = NULL;
2953 mod1.sml_next = &mod2;
2955 mod2.sml_op = LDAP_MOD_REPLACE;
2957 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2958 mod2.sml_type = mod2.sml_desc->ad_cname;
2959 mod2.sml_numvals = 1;
2960 mod2.sml_values = &gcbva[1];
2961 mod2.sml_nvalues = NULL;
2962 mod2.sml_next = NULL;
2964 op->o_tag = LDAP_REQ_MODIFY;
2965 op->orm_modlist = &mod1;
2967 rc = op->o_bd->be_modify( op, &rs_modify );
2968 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2971 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2972 op->o_delete_glue_parent ) {
2973 op->o_delete_glue_parent = 0;
2974 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2975 slap_callback cb = { NULL };
2976 cb.sc_response = slap_null_cb;
2977 dnParent( &op->o_req_ndn, &pdn );
2979 op->o_req_ndn = pdn;
2980 op->o_callback = &cb;
2981 rs_reinit( &rs_delete, REP_RESULT );
2982 /* give it a root privil ? */
2983 op->o_bd->be_delete( op, &rs_delete );
2989 op->o_delete_glue_parent = 0;
2991 ber_bvfree( np_prev->npe_name );
2992 ber_bvfree( np_prev->npe_nname );
2995 if ( slapd_shutdown ) {
3000 slap_graduate_commit_csn( op );
3003 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3004 BER_BVZERO( &op->o_csn );
3011 syncrepl_add_glue_ancestors(
3015 Backend *be = op->o_bd;
3016 slap_callback cb = { NULL };
3018 int rc = LDAP_SUCCESS;
3021 struct berval dn = BER_BVNULL;
3022 struct berval ndn = BER_BVNULL;
3024 struct berval ptr, nptr;
3027 op->o_tag = LDAP_REQ_ADD;
3028 op->o_callback = &cb;
3029 cb.sc_response = null_callback;
3030 cb.sc_private = NULL;
3035 /* count RDNs in suffix */
3036 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3037 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3039 ptr.bv_len -= comma - ptr.bv_val;
3049 /* Start with BE suffix */
3051 for ( i = 0; i < suffrdns; i++ ) {
3052 comma = ber_bvrchr( &ptr, ',' );
3053 if ( comma != NULL ) {
3054 ptr.bv_len = comma - ptr.bv_val;
3061 if ( !BER_BVISEMPTY( &ptr ) ) {
3062 dn.bv_len -= ptr.bv_len + 1;
3063 dn.bv_val += ptr.bv_len + 1;
3066 /* the normalizedDNs are always the same length, no counting
3070 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3071 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3072 ndn.bv_len = be->be_nsuffix[0].bv_len;
3074 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3080 while ( ndn.bv_val > e->e_nname.bv_val ) {
3081 SlapReply rs_add = {REP_RESULT};
3083 glue = entry_alloc();
3084 ber_dupbv( &glue->e_name, &dn );
3085 ber_dupbv( &glue->e_nname, &ndn );
3087 a = attr_alloc( slap_schema.si_ad_objectClass );
3090 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3091 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3092 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3093 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3095 a->a_nvals = a->a_vals;
3097 a->a_next = glue->e_attrs;
3100 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3103 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3104 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3105 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3107 a->a_nvals = a->a_vals;
3109 a->a_next = glue->e_attrs;
3112 op->o_req_dn = glue->e_name;
3113 op->o_req_ndn = glue->e_nname;
3115 rc = be->be_add ( op, &rs_add );
3116 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3117 if ( op->ora_e == glue )
3118 be_entry_release_w( op, glue );
3120 /* incl. ALREADY EXIST */
3122 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3128 /* Move to next child */
3129 comma = ber_bvrchr( &ptr, ',' );
3130 if ( comma == NULL ) {
3133 ptr.bv_len = comma - ptr.bv_val;
3135 dn.bv_val = ++comma;
3136 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3138 comma = ber_bvrchr( &nptr, ',' );
3139 assert( comma != NULL );
3140 nptr.bv_len = comma - nptr.bv_val;
3142 ndn.bv_val = ++comma;
3143 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3154 slap_callback cb = { NULL };
3156 Backend *be = op->o_bd;
3157 SlapReply rs_add = {REP_RESULT};
3159 rc = syncrepl_add_glue_ancestors( op, e );
3162 case LDAP_ALREADY_EXISTS:
3169 op->o_tag = LDAP_REQ_ADD;
3170 op->o_callback = &cb;
3171 cb.sc_response = null_callback;
3172 cb.sc_private = NULL;
3174 op->o_req_dn = e->e_name;
3175 op->o_req_ndn = e->e_nname;
3177 rc = be->be_add ( op, &rs_add );
3178 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3179 if ( op->ora_e == e )
3180 be_entry_release_w( op, e );
3189 syncrepl_updateCookie(
3192 struct sync_cookie *syncCookie )
3194 Backend *be = op->o_bd;
3196 struct berval first = BER_BVNULL;
3198 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3201 int rc, i, j, changed = 0;
3204 slap_callback cb = { NULL };
3205 SlapReply rs_modify = {REP_RESULT};
3207 mod.sml_op = LDAP_MOD_REPLACE;
3208 mod.sml_desc = slap_schema.si_ad_contextCSN;
3209 mod.sml_type = mod.sml_desc->ad_cname;
3210 mod.sml_flags = SLAP_MOD_INTERNAL;
3211 mod.sml_nvalues = NULL;
3212 mod.sml_next = NULL;
3214 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3217 for ( i=0; i<syncCookie->numcsns; i++ ) {
3218 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3220 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3221 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3225 /* clone the cookieState CSNs so we can Replace the whole thing */
3226 mod.sml_numvals = si->si_cookieState->cs_num;
3227 mod.sml_values = op->o_tmpalloc(( mod.sml_numvals+1 )*sizeof(struct berval), op->o_tmpmemctx );
3228 for ( i=0; i<mod.sml_numvals; i++ )
3229 mod.sml_values[i] = si->si_cookieState->cs_vals[i];
3230 BER_BVZERO( &mod.sml_values[i] );
3232 /* find any CSNs in the syncCookie that are newer than the cookieState */
3233 for ( i=0; i<syncCookie->numcsns; i++ ) {
3234 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
3235 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
3237 len = syncCookie->ctxcsn[i].bv_len;
3238 if ( len > si->si_cookieState->cs_vals[j].bv_len )
3239 len = si->si_cookieState->cs_vals[j].bv_len;
3240 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3241 si->si_cookieState->cs_vals[j].bv_val, len ) > 0 ) {
3242 mod.sml_values[j] = syncCookie->ctxcsn[i];
3244 if ( BER_BVISNULL( &first ) ) {
3245 first = syncCookie->ctxcsn[i];
3247 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3249 first = syncCookie->ctxcsn[i];
3254 /* there was no match for this SID, it's a new CSN */
3255 if ( j == si->si_cookieState->cs_num ) {
3256 mod.sml_values = op->o_tmprealloc( mod.sml_values,
3257 ( mod.sml_numvals+2 )*sizeof(struct berval), op->o_tmpmemctx );
3258 mod.sml_values[mod.sml_numvals++] = syncCookie->ctxcsn[i];
3259 BER_BVZERO( &mod.sml_values[mod.sml_numvals] );
3260 if ( BER_BVISNULL( &first ) ) {
3261 first = syncCookie->ctxcsn[i];
3262 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3264 first = syncCookie->ctxcsn[i];
3269 /* Should never happen, ITS#5065 */
3270 if ( BER_BVISNULL( &first ) || !changed ) {
3271 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3272 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3275 op->o_bd = si->si_wbe;
3276 slap_queue_csn( op, &first );
3278 op->o_tag = LDAP_REQ_MODIFY;
3280 cb.sc_response = null_callback;
3283 op->o_callback = &cb;
3284 op->o_req_dn = si->si_contextdn;
3285 op->o_req_ndn = si->si_contextdn;
3287 /* update contextCSN */
3288 op->o_dont_replicate = 1;
3290 op->orm_modlist = &mod;
3291 op->orm_no_opattrs = 1;
3292 rc = op->o_bd->be_modify( op, &rs_modify );
3294 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3295 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3297 char txtbuf[SLAP_TEXT_BUFLEN];
3298 size_t textlen = sizeof txtbuf;
3299 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3300 rs_reinit( &rs_modify, REP_RESULT );
3301 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3303 rc = op->o_bd->be_add( op, &rs_modify );
3304 if ( e == op->ora_e )
3305 be_entry_release_w( op, op->ora_e );
3308 op->orm_no_opattrs = 0;
3309 op->o_dont_replicate = 0;
3311 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3312 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3313 /* If we replaced any old values */
3314 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3315 if ( mod.sml_values[i].bv_val != si->si_cookieState->cs_vals[i].bv_val )
3316 ber_bvreplace( &si->si_cookieState->cs_vals[i],
3317 &mod.sml_values[i] );
3319 /* Handle any added values */
3320 if ( i < mod.sml_numvals ) {
3321 si->si_cookieState->cs_num = mod.sml_numvals;
3322 value_add( &si->si_cookieState->cs_vals, &mod.sml_values[i] );
3323 free( si->si_cookieState->cs_sids );
3324 si->si_cookieState->cs_sids = slap_parse_csn_sids(
3325 si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
3328 /* Don't just dup the provider's cookie, recreate it */
3329 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
3330 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn, si->si_cookieState->cs_vals, NULL );
3331 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num * sizeof(int) );
3332 for ( i=0; i<si->si_cookieState->cs_num; i++ )
3333 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
3335 si->si_cookieState->cs_age++;
3336 si->si_cookieAge = si->si_cookieState->cs_age;
3338 Debug( LDAP_DEBUG_ANY,
3339 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3340 si->si_ridtxt, rs_modify.sr_err, 0 );
3342 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3345 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3346 BER_BVZERO( &op->o_csn );
3347 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3348 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3351 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3352 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3359 /* Compare the attribute from the old entry to the one in the new
3360 * entry. The Modifications from the new entry will either be left
3361 * in place, or changed to an Add or Delete as needed.
3364 attr_cmp( Operation *op, Attribute *old, Attribute *new,
3365 Modifications ***mret, Modifications ***mcur )
3368 Modifications *mod, **modtail;
3374 struct berval **adds, **dels;
3375 /* count old and new */
3376 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
3377 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
3379 /* there MUST be both old and new values */
3384 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
3385 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
3387 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
3388 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
3392 for ( i=0; i<o; i++ ) {
3393 for ( j=0; j<n; j++ ) {
3396 if ( bvmatch( dels[i], adds[j] ) ) {
3406 /* Don't delete/add an objectClass, always use the replace op.
3407 * Modify would fail if provider has replaced entry with a new,
3408 * and the new explicitly includes a superior of a class that was
3409 * only included implicitly in the old entry. Ref ITS#5517.
3411 * Also use replace op if attr has no equality matching rule.
3414 if ( ( nn || ( no > 0 && no < o ) ) &&
3415 ( old->a_desc == slap_schema.si_ad_objectClass ||
3416 !old->a_desc->ad_type->sat_equality ) )
3422 /* all old values were deleted, just use the replace op */
3426 /* delete some values */
3427 mod = ch_malloc( sizeof( Modifications ) );
3428 mod->sml_op = LDAP_MOD_DELETE;
3430 mod->sml_desc = old->a_desc;
3431 mod->sml_type = mod->sml_desc->ad_cname;
3432 mod->sml_numvals = no;
3433 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3434 if ( old->a_vals != old->a_nvals ) {
3435 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3437 mod->sml_nvalues = NULL;
3440 for ( i = 0; i < o; i++ ) {
3441 if ( !dels[i] ) continue;
3442 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
3443 if ( mod->sml_nvalues ) {
3444 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
3448 BER_BVZERO( &mod->sml_values[j] );
3449 if ( mod->sml_nvalues ) {
3450 BER_BVZERO( &mod->sml_nvalues[j] );
3453 modtail = &mod->sml_next;
3456 op->o_tmpfree( dels, op->o_tmpmemctx );
3457 /* some values were added */
3458 if ( nn && no < o ) {
3459 mod = ch_malloc( sizeof( Modifications ) );
3460 mod->sml_op = LDAP_MOD_ADD;
3462 mod->sml_desc = old->a_desc;
3463 mod->sml_type = mod->sml_desc->ad_cname;
3464 mod->sml_numvals = nn;
3465 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3466 if ( old->a_vals != old->a_nvals ) {
3467 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3469 mod->sml_nvalues = NULL;
3472 for ( i = 0; i < n; i++ ) {
3473 if ( !adds[i] ) continue;
3474 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
3475 if ( mod->sml_nvalues ) {
3476 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
3480 BER_BVZERO( &mod->sml_values[j] );
3481 if ( mod->sml_nvalues ) {
3482 BER_BVZERO( &mod->sml_nvalues[j] );
3485 modtail = &mod->sml_next;
3488 op->o_tmpfree( adds, op->o_tmpmemctx );
3490 /* new attr, just use the new mod */
3494 /* advance to next element */
3498 **mcur = mod->sml_next;
3500 modtail = &mod->sml_next;
3502 *mcur = &mod->sml_next;
3508 /* Generate a set of modifications to change the old entry into the
3509 * new one. On input ml is a list of modifications equivalent to
3510 * the new entry. It will be massaged and the result will be stored
3513 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
3514 Modifications **mods, Modifications **ml, int is_ctx)
3516 Modifications **modtail = mods;
3518 /* We assume that attributes are saved in the same order
3519 * in the remote and local databases. So if we walk through
3520 * the attributeDescriptions one by one they should match in
3521 * lock step. If not, look for an add or delete.
3523 while ( old && new )
3525 /* If we've seen this before, use its mod now */
3526 if ( new->a_flags & SLAP_ATTR_IXADD ) {
3527 attr_cmp( op, NULL, new, &modtail, &ml );
3531 /* Skip contextCSN */
3532 if ( is_ctx && old->a_desc ==
3533 slap_schema.si_ad_contextCSN ) {
3538 if ( old->a_desc != new->a_desc ) {
3542 /* If it's just been re-added later,
3543 * remember that we've seen it.
3545 tmp = attr_find( new, old->a_desc );
3547 tmp->a_flags |= SLAP_ATTR_IXADD;
3549 /* If it's a new attribute, pull it in.
3551 tmp = attr_find( old, new->a_desc );
3553 attr_cmp( op, NULL, new, &modtail, &ml );
3557 /* Delete old attr */
3558 mod = ch_malloc( sizeof( Modifications ) );
3559 mod->sml_op = LDAP_MOD_DELETE;
3561 mod->sml_desc = old->a_desc;
3562 mod->sml_type = mod->sml_desc->ad_cname;
3563 mod->sml_numvals = 0;
3564 mod->sml_values = NULL;
3565 mod->sml_nvalues = NULL;
3567 modtail = &mod->sml_next;
3572 /* kludge - always update modifiersName so that it
3573 * stays co-located with the other mod opattrs. But only
3574 * if we know there are other valid mods.
3576 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
3577 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
3578 attr_cmp( op, NULL, new, &modtail, &ml );
3580 attr_cmp( op, old, new, &modtail, &ml );
3593 dninfo *dni = op->o_callback->sc_private;
3595 if ( rs->sr_type == REP_SEARCH ) {
3596 if ( !BER_BVISNULL( &dni->dn ) ) {
3597 Debug( LDAP_DEBUG_ANY,
3598 "dn_callback : consistency error - "
3599 "entryUUID is not unique\n", 0, 0, 0 );
3601 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
3602 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
3603 /* If there is a new entry, see if it differs from the old.
3604 * We compare the non-normalized values so that cosmetic changes
3605 * in the provider are always propagated.
3607 if ( dni->new_entry ) {
3608 Attribute *old, *new;
3609 struct berval old_rdn, new_rdn;
3610 struct berval old_p, new_p;
3611 int is_ctx, new_sup = 0;
3613 /* If old entry is not a glue entry, make sure new entry
3614 * is actually newer than old entry
3616 if ( !is_entry_glue( rs->sr_entry )) {
3617 old = attr_find( rs->sr_entry->e_attrs,
3618 slap_schema.si_ad_entryCSN );
3619 new = attr_find( dni->new_entry->e_attrs,
3620 slap_schema.si_ad_entryCSN );
3623 ber_len_t len = old->a_vals[0].bv_len;
3624 if ( len > new->a_vals[0].bv_len )
3625 len = new->a_vals[0].bv_len;
3626 rc = memcmp( old->a_vals[0].bv_val,
3627 new->a_vals[0].bv_val, len );
3629 Debug( LDAP_DEBUG_SYNC,
3630 "dn_callback : new entry is older than ours "
3631 "%s ours %s, new %s\n",
3632 rs->sr_entry->e_name.bv_val,
3633 old->a_vals[0].bv_val,
3634 new->a_vals[0].bv_val );
3635 return LDAP_SUCCESS;
3636 } else if ( rc == 0 ) {
3637 Debug( LDAP_DEBUG_SYNC,
3638 "dn_callback : entries have identical CSN "
3640 rs->sr_entry->e_name.bv_val,
3641 old->a_vals[0].bv_val, 0 );
3642 return LDAP_SUCCESS;
3647 is_ctx = dn_match( &rs->sr_entry->e_nname,
3648 &op->o_bd->be_nsuffix[0] );
3650 /* Did the DN change?
3651 * case changes in the parent are ignored,
3652 * we only want to know if the RDN was
3655 dnRdn( &rs->sr_entry->e_name, &old_rdn );
3656 dnRdn( &dni->new_entry->e_name, &new_rdn );
3657 dnParent( &rs->sr_entry->e_nname, &old_p );
3658 dnParent( &dni->new_entry->e_nname, &new_p );
3660 new_sup = !dn_match( &old_p, &new_p );
3661 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
3663 struct berval oldRDN, oldVal;
3664 AttributeDescription *ad = NULL;
3670 dni->nnewSup = new_p;
3672 /* See if the oldRDN was deleted */
3673 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
3674 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
3675 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
3677 oldRDN.bv_len -= oldVal.bv_len + 1;
3678 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3680 for ( oldpos=0, a=rs->sr_entry->e_attrs;
3681 a && a->a_desc != ad; oldpos++, a=a->a_next );
3683 for ( newpos=0, a=dni->new_entry->e_attrs;
3684 a && a->a_desc != ad; newpos++, a=a->a_next );
3685 if ( !a || oldpos != newpos || attr_valfind( a,
3686 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
3687 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3688 SLAP_MR_VALUE_OF_SYNTAX,
3689 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
3693 /* Get the newRDN's desc */
3694 dnRdn( &dni->new_entry->e_nname, &oldRDN );
3695 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
3696 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
3698 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3701 /* A ModDN has happened, but in Refresh mode other
3702 * changes may have occurred before we picked it up.
3703 * So fallthru to regular Modify processing.
3707 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
3708 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
3712 } else if ( rs->sr_type == REP_RESULT ) {
3713 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
3714 Debug( LDAP_DEBUG_ANY,
3715 "dn_callback : consistency error - "
3716 "entryUUID is not unique\n", 0, 0, 0 );
3720 return LDAP_SUCCESS;
3724 nonpresent_callback(
3728 syncinfo_t *si = op->o_callback->sc_private;
3731 struct berval* present_uuid = NULL;
3732 struct nonpresent_entry *np_entry;
3734 if ( rs->sr_type == REP_RESULT ) {
3735 count = avl_free( si->si_presentlist, ch_free );
3736 si->si_presentlist = NULL;
3738 } else if ( rs->sr_type == REP_SEARCH ) {
3739 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
3740 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
3743 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
3747 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
3748 char buf[sizeof("rid=999 non")];
3750 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
3751 present_uuid ? "" : "non" );
3753 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
3754 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
3757 if ( a == NULL ) return 0;
3760 if ( present_uuid == NULL ) {
3761 np_entry = (struct nonpresent_entry *)
3762 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
3763 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
3764 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
3765 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
3768 avl_delete( &si->si_presentlist,
3769 &a->a_nvals[0], syncuuid_cmp );
3770 ch_free( present_uuid );
3773 return LDAP_SUCCESS;
3781 if ( rs->sr_err != LDAP_SUCCESS &&
3782 rs->sr_err != LDAP_REFERRAL &&
3783 rs->sr_err != LDAP_ALREADY_EXISTS &&
3784 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
3785 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
3787 Debug( LDAP_DEBUG_ANY,
3788 "null_callback : error code 0x%x\n",
3791 return LDAP_SUCCESS;
3794 static struct berval *
3795 slap_uuidstr_from_normalized(
3796 struct berval* uuidstr,
3797 struct berval* normalized,
3802 unsigned char nibble;
3805 if ( normalized == NULL ) return NULL;
3806 if ( normalized->bv_len != 16 ) return NULL;
3811 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3812 if ( new == NULL ) {
3819 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3820 if ( new != uuidstr ) {
3821 slap_sl_free( new, ctx );
3826 for ( i = 0; i < 16; i++ ) {
3827 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
3828 new->bv_val[(i<<1)+d] = '-';
3832 nibble = (normalized->bv_val[i] >> 4) & 0xF;
3833 if ( nibble < 10 ) {
3834 new->bv_val[(i<<1)+d] = nibble + '0';
3836 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
3839 nibble = (normalized->bv_val[i]) & 0xF;
3840 if ( nibble < 10 ) {
3841 new->bv_val[(i<<1)+d+1] = nibble + '0';
3843 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
3847 new->bv_val[new->bv_len] = '\0';
3854 if ( normalized == NULL ) return NULL;
3855 if ( normalized->bv_len != 16 ) return NULL;
3861 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3862 if ( new == NULL ) {
3869 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3874 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
3875 normalized->bv_len, new->bv_val, new->bv_len + 1 );
3879 if ( new != NULL ) {
3880 if ( new->bv_val != NULL ) {
3881 slap_sl_free( new->bv_val, ctx );
3884 if ( new != uuidstr ) {
3885 slap_sl_free( new, ctx );
3898 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
3900 const struct berval *uuid1 = v_uuid1;
3901 const struct berval *uuid2 = v_uuid2;
3902 int rc = uuid1->bv_len - uuid2->bv_len;
3903 if ( rc ) return rc;
3904 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
3908 syncinfo_free( syncinfo_t *sie, int free_all )
3910 syncinfo_t *si_next;
3912 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
3913 sie->si_ridtxt, 0, 0 );
3916 si_next = sie->si_next;
3919 if ( sie->si_conn ) {
3920 connection_client_stop( sie->si_conn );
3921 sie->si_conn = NULL;
3923 ldap_unbind_ext( sie->si_ld, NULL, NULL );
3927 struct re_s *re = sie->si_re;
3930 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3931 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
3932 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3933 ldap_pvt_runqueue_remove( &slapd_rq, re );
3934 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3937 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
3939 bindconf_free( &sie->si_bindconf );
3941 if ( sie->si_filterstr.bv_val ) {
3942 ch_free( sie->si_filterstr.bv_val );
3944 if ( sie->si_filter ) {
3945 filter_free( sie->si_filter );
3947 if ( sie->si_logfilterstr.bv_val ) {
3948 ch_free( sie->si_logfilterstr.bv_val );
3950 if ( sie->si_base.bv_val ) {
3951 ch_free( sie->si_base.bv_val );
3953 if ( sie->si_logbase.bv_val ) {
3954 ch_free( sie->si_logbase.bv_val );
3956 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
3957 ch_free( sie->si_contextdn.bv_val );
3959 if ( sie->si_attrs ) {
3961 while ( sie->si_attrs[i] != NULL ) {
3962 ch_free( sie->si_attrs[i] );
3965 ch_free( sie->si_attrs );
3967 if ( sie->si_exattrs ) {
3969 while ( sie->si_exattrs[i] != NULL ) {
3970 ch_free( sie->si_exattrs[i] );
3973 ch_free( sie->si_exattrs );
3975 if ( sie->si_anlist ) {
3977 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
3978 ch_free( sie->si_anlist[i].an_name.bv_val );
3981 ch_free( sie->si_anlist );
3983 if ( sie->si_exanlist ) {
3985 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
3986 ch_free( sie->si_exanlist[i].an_name.bv_val );
3989 ch_free( sie->si_exanlist );
3991 if ( sie->si_retryinterval ) {
3992 ch_free( sie->si_retryinterval );
3994 if ( sie->si_retrynum ) {
3995 ch_free( sie->si_retrynum );
3997 if ( sie->si_retrynum_init ) {
3998 ch_free( sie->si_retrynum_init );
4000 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
4001 if ( sie->si_presentlist ) {
4002 avl_free( sie->si_presentlist, ch_free );
4004 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
4005 struct nonpresent_entry* npe;
4006 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
4007 LDAP_LIST_REMOVE( npe, npe_link );
4008 if ( npe->npe_name ) {
4009 if ( npe->npe_name->bv_val ) {
4010 ch_free( npe->npe_name->bv_val );
4012 ch_free( npe->npe_name );
4014 if ( npe->npe_nname ) {
4015 if ( npe->npe_nname->bv_val ) {
4016 ch_free( npe->npe_nname->bv_val );
4018 ch_free( npe->npe_nname );
4022 if ( sie->si_cookieState ) {
4023 sie->si_cookieState->cs_ref--;
4024 if ( !sie->si_cookieState->cs_ref ) {
4025 ch_free( sie->si_cookieState->cs_sids );
4026 ber_bvarray_free( sie->si_cookieState->cs_vals );
4027 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
4028 ch_free( sie->si_cookieState->cs_psids );
4029 ber_bvarray_free( sie->si_cookieState->cs_pvals );
4030 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
4031 ch_free( sie->si_cookieState );
4034 #ifdef ENABLE_REWRITE
4035 if ( sie->si_rewrite )
4036 rewrite_info_delete( &sie->si_rewrite );
4037 if ( sie->si_suffixm.bv_val )
4038 ch_free( sie->si_suffixm.bv_val );
4042 } while ( free_all && si_next );
4045 #ifdef ENABLE_REWRITE
4047 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4049 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4050 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4051 char *argvRule[] = { "rewriteRule", NULL, NULL, ":", NULL };
4055 if ( si->si_rewrite )
4056 rewrite_info_delete( &si->si_rewrite );
4057 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4059 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4060 if ( rc != LDAP_SUCCESS )
4063 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4064 if ( rc != LDAP_SUCCESS )
4067 vnc = ch_malloc( si->si_base.bv_len + 6 );
4068 strcpy( vnc, "(.*)" );
4069 lutil_strcopy( lutil_strcopy( vnc+4, si->si_base.bv_val ), "$" );
4072 rnc = ch_malloc( si->si_suffixm.bv_len + 3 );
4073 strcpy( rnc, "%1" );
4074 strcpy( rnc+2, si->si_suffixm.bv_val );
4077 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4084 /* NOTE: used & documented in slapd.conf(5) */
4086 #define PROVIDERSTR "provider"
4087 #define SCHEMASTR "schemachecking"
4088 #define FILTERSTR "filter"
4089 #define SEARCHBASESTR "searchbase"
4090 #define SCOPESTR "scope"
4091 #define ATTRSONLYSTR "attrsonly"
4092 #define ATTRSSTR "attrs"
4093 #define TYPESTR "type"
4094 #define INTERVALSTR "interval"
4095 #define RETRYSTR "retry"
4096 #define SLIMITSTR "sizelimit"
4097 #define TLIMITSTR "timelimit"
4098 #define SYNCDATASTR "syncdata"
4099 #define LOGBASESTR "logbase"
4100 #define LOGFILTERSTR "logfilter"
4101 #define SUFFIXMSTR "suffixmassage"
4103 /* FIXME: undocumented */
4104 #define EXATTRSSTR "exattrs"
4105 #define MANAGEDSAITSTR "manageDSAit"
4109 GOT_RID = 0x00000001U,
4110 GOT_PROVIDER = 0x00000002U,
4111 GOT_SCHEMACHECKING = 0x00000004U,
4112 GOT_FILTER = 0x00000008U,
4113 GOT_SEARCHBASE = 0x00000010U,
4114 GOT_SCOPE = 0x00000020U,
4115 GOT_ATTRSONLY = 0x00000040U,
4116 GOT_ATTRS = 0x00000080U,
4117 GOT_TYPE = 0x00000100U,
4118 GOT_INTERVAL = 0x00000200U,
4119 GOT_RETRY = 0x00000400U,
4120 GOT_SLIMIT = 0x00000800U,
4121 GOT_TLIMIT = 0x00001000U,
4122 GOT_SYNCDATA = 0x00002000U,
4123 GOT_LOGBASE = 0x00004000U,
4124 GOT_LOGFILTER = 0x00008000U,
4125 GOT_EXATTRS = 0x00010000U,
4126 GOT_MANAGEDSAIT = 0x00020000U,
4127 GOT_BINDCONF = 0x00040000U,
4128 GOT_SUFFIXM = 0x00080000U,
4131 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4134 static slap_verbmasks datamodes[] = {
4135 { BER_BVC("default"), SYNCDATA_DEFAULT },
4136 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4137 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4142 parse_syncrepl_retry(
4149 int use_default = 0;
4151 char *val = arg + STRLENOF( RETRYSTR "=" );
4152 if ( strcasecmp( val, "undefined" ) == 0 ) {
4157 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4158 retry_list[0] = NULL;
4160 slap_str2clist( &retry_list, val, " ,\t" );
4162 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4165 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4166 "Error: incomplete syncrepl retry list" );
4167 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4168 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4169 ch_free( retry_list[k] );
4171 ch_free( retry_list );
4174 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4175 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4176 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4177 for ( j = 0; j < n; j++ ) {
4179 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4180 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4181 "Error: invalid retry interval \"%s\" (#%d)",
4182 retry_list[j*2], j );
4183 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4184 /* do some cleanup */
4187 si->si_retryinterval[j] = (time_t)t;
4188 if ( *retry_list[j*2+1] == '+' ) {
4189 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4190 si->si_retrynum[j] = RETRYNUM_FOREVER;
4194 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4195 || si->si_retrynum_init[j] <= 0 )
4197 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4198 "Error: invalid initial retry number \"%s\" (#%d)",
4199 retry_list[j*2+1], j );
4200 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4201 /* do some cleanup */
4204 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4205 || si->si_retrynum[j] <= 0 )
4207 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4208 "Error: invalid retry number \"%s\" (#%d)",
4209 retry_list[j*2+1], j );
4210 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4211 /* do some cleanup */
4216 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4217 Debug( LDAP_DEBUG_CONFIG,
4218 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4222 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4223 si->si_retrynum[j] = RETRYNUM_TAIL;
4224 si->si_retryinterval[j] = 0;
4226 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4227 ch_free( retry_list[k] );
4229 ch_free( retry_list );
4230 if ( !use_default ) {
4231 si->si_got |= GOT_RETRY;
4238 parse_syncrepl_line(
4245 for ( i = 1; i < c->argc; i++ ) {
4246 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4247 STRLENOF( IDSTR "=" ) ) )
4250 /* '\0' string terminator accounts for '=' */
4251 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4252 if ( lutil_atoi( &tmp, val ) != 0 ) {
4253 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4254 "Error: parse_syncrepl_line: "
4255 "unable to parse syncrepl id \"%s\"", val );
4256 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4259 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4260 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4261 "Error: parse_syncrepl_line: "
4262 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4263 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4267 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4268 si->si_got |= GOT_RID;
4269 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4270 STRLENOF( PROVIDERSTR "=" ) ) )
4272 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4273 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4275 if ( ldap_is_ldaps_url( val ))
4276 si->si_bindconf.sb_tls_do_init = 1;
4278 si->si_got |= GOT_PROVIDER;
4279 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4280 STRLENOF( SCHEMASTR "=" ) ) )
4282 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4283 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4284 si->si_schemachecking = 1;
4285 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4286 si->si_schemachecking = 0;
4288 si->si_schemachecking = 1;
4290 si->si_got |= GOT_SCHEMACHECKING;
4291 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4292 STRLENOF( FILTERSTR "=" ) ) )
4294 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4295 if ( si->si_filterstr.bv_val )
4296 ch_free( si->si_filterstr.bv_val );
4297 ber_str2bv( val, 0, 1, &si->si_filterstr );
4298 si->si_got |= GOT_FILTER;
4299 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4300 STRLENOF( LOGFILTERSTR "=" ) ) )
4302 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4303 if ( si->si_logfilterstr.bv_val )
4304 ch_free( si->si_logfilterstr.bv_val );
4305 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4306 si->si_got |= GOT_LOGFILTER;
4307 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4308 STRLENOF( SEARCHBASESTR "=" ) ) )
4313 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4314 if ( si->si_base.bv_val ) {
4315 ch_free( si->si_base.bv_val );
4317 ber_str2bv( val, 0, 0, &bv );
4318 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4319 if ( rc != LDAP_SUCCESS ) {
4320 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4321 "Invalid base DN \"%s\": %d (%s)",
4322 val, rc, ldap_err2string( rc ) );
4323 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4326 si->si_got |= GOT_SEARCHBASE;
4327 #ifdef ENABLE_REWRITE
4328 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
4329 STRLENOF( SUFFIXMSTR "=" ) ) )
4334 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
4335 if ( si->si_suffixm.bv_val ) {
4336 ch_free( si->si_suffixm.bv_val );
4338 ber_str2bv( val, 0, 0, &bv );
4339 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
4340 if ( rc != LDAP_SUCCESS ) {
4341 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4342 "Invalid massage DN \"%s\": %d (%s)",
4343 val, rc, ldap_err2string( rc ) );
4344 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4347 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
4348 ch_free( si->si_suffixm.bv_val );
4349 BER_BVZERO( &si->si_suffixm );
4350 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4351 "Massage DN \"%s\" is not within the database naming context",
4353 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4356 si->si_got |= GOT_SUFFIXM;
4358 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4359 STRLENOF( LOGBASESTR "=" ) ) )
4364 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
4365 if ( si->si_logbase.bv_val ) {
4366 ch_free( si->si_logbase.bv_val );
4368 ber_str2bv( val, 0, 0, &bv );
4369 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
4370 if ( rc != LDAP_SUCCESS ) {
4371 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4372 "Invalid logbase DN \"%s\": %d (%s)",
4373 val, rc, ldap_err2string( rc ) );
4374 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4377 si->si_got |= GOT_LOGBASE;
4378 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
4379 STRLENOF( SCOPESTR "=" ) ) )
4382 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
4383 j = ldap_pvt_str2scope( val );
4385 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4386 "Error: parse_syncrepl_line: "
4387 "unknown scope \"%s\"", val);
4388 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4392 si->si_got |= GOT_SCOPE;
4393 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
4394 STRLENOF( ATTRSONLYSTR ) ) )
4396 si->si_attrsonly = 1;
4397 si->si_got |= GOT_ATTRSONLY;
4398 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
4399 STRLENOF( ATTRSSTR "=" ) ) )
4401 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
4402 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4404 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4405 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
4406 if ( si->si_anlist == NULL ) {
4407 ch_free( attr_fname );
4410 si->si_anfile = attr_fname;
4412 char *str, *s, *next;
4413 const char *delimstr = " ,\t";
4414 str = ch_strdup( val );
4415 for ( s = ldap_pvt_strtok( str, delimstr, &next );
4417 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
4419 if ( strlen(s) == 1 && *s == '*' ) {
4420 si->si_allattrs = 1;
4421 val[ s - str ] = delimstr[0];
4423 if ( strlen(s) == 1 && *s == '+' ) {
4424 si->si_allopattrs = 1;
4425 val [ s - str ] = delimstr[0];
4429 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
4430 if ( si->si_anlist == NULL ) {
4434 si->si_got |= GOT_ATTRS;
4435 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
4436 STRLENOF( EXATTRSSTR "=" ) ) )
4438 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
4439 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4441 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4442 si->si_exanlist = file2anlist(
4443 si->si_exanlist, attr_fname, " ,\t" );
4444 if ( si->si_exanlist == NULL ) {
4445 ch_free( attr_fname );
4448 ch_free( attr_fname );
4450 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
4451 if ( si->si_exanlist == NULL ) {
4455 si->si_got |= GOT_EXATTRS;
4456 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
4457 STRLENOF( TYPESTR "=" ) ) )
4459 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
4460 if ( !strncasecmp( val, "refreshOnly",
4461 STRLENOF("refreshOnly") ) )
4463 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4464 } else if ( !strncasecmp( val, "refreshAndPersist",
4465 STRLENOF("refreshAndPersist") ) )
4467 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
4468 si->si_interval = 60;
4470 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4471 "Error: parse_syncrepl_line: "
4472 "unknown sync type \"%s\"", val);
4473 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4476 si->si_got |= GOT_TYPE;
4477 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
4478 STRLENOF( INTERVALSTR "=" ) ) )
4480 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
4481 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
4482 si->si_interval = 0;
4483 } else if ( strchr( val, ':' ) != NULL ) {
4484 char *next, *ptr = val;
4487 dd = strtol( ptr, &next, 10 );
4488 if ( next == ptr || next[0] != ':' || dd < 0 ) {
4489 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4490 "Error: parse_syncrepl_line: "
4491 "invalid interval \"%s\", unable to parse days", val );
4492 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4496 hh = strtol( ptr, &next, 10 );
4497 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
4498 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4499 "Error: parse_syncrepl_line: "
4500 "invalid interval \"%s\", unable to parse hours", val );
4501 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4505 mm = strtol( ptr, &next, 10 );
4506 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
4507 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4508 "Error: parse_syncrepl_line: "
4509 "invalid interval \"%s\", unable to parse minutes", val );
4510 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4514 ss = strtol( ptr, &next, 10 );
4515 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
4516 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4517 "Error: parse_syncrepl_line: "
4518 "invalid interval \"%s\", unable to parse seconds", val );
4519 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4522 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
4526 if ( lutil_parse_time( val, &t ) != 0 ) {
4527 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4528 "Error: parse_syncrepl_line: "
4529 "invalid interval \"%s\"", val );
4530 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4533 si->si_interval = (time_t)t;
4535 if ( si->si_interval < 0 ) {
4536 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4537 "Error: parse_syncrepl_line: "
4538 "invalid interval \"%ld\"",
4539 (long) si->si_interval);
4540 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4543 si->si_got |= GOT_INTERVAL;
4544 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
4545 STRLENOF( RETRYSTR "=" ) ) )
4547 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
4550 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
4551 STRLENOF( MANAGEDSAITSTR "=" ) ) )
4553 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
4554 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
4555 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
4557 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4558 "invalid manageDSAit value \"%s\".\n",
4560 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4563 si->si_got |= GOT_MANAGEDSAIT;
4564 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
4565 STRLENOF( SLIMITSTR "=") ) )
4567 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
4568 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4571 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
4572 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4573 "invalid size limit value \"%s\".\n",
4575 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4578 si->si_got |= GOT_SLIMIT;
4579 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
4580 STRLENOF( TLIMITSTR "=" ) ) )
4582 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
4583 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4586 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
4587 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4588 "invalid time limit value \"%s\".\n",
4590 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4593 si->si_got |= GOT_TLIMIT;
4594 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
4595 STRLENOF( SYNCDATASTR "=" ) ) )
4597 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
4598 si->si_syncdata = verb_to_mask( val, datamodes );
4599 si->si_got |= GOT_SYNCDATA;
4600 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
4601 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4602 "Error: parse_syncrepl_line: "
4603 "unable to parse \"%s\"\n", c->argv[ i ] );
4604 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4607 si->si_got |= GOT_BINDCONF;
4610 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
4611 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4612 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
4613 si->si_got & GOT_RID ? "" : " "IDSTR,
4614 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
4615 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
4616 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4620 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
4621 ch_free( si->si_base.bv_val );
4622 BER_BVZERO( &si->si_base );
4623 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4624 "Base DN \"%s\" is not within the database naming context",
4626 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4630 #ifdef ENABLE_REWRITE
4631 if ( si->si_got & GOT_SUFFIXM ) {
4632 if (config_suffixm( c, si )) {
4633 ch_free( si->si_suffixm.bv_val );
4634 BER_BVZERO( &si->si_suffixm );
4635 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4636 "Error configuring rewrite engine" );
4637 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4643 if ( !( si->si_got & GOT_RETRY ) ) {
4644 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
4645 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
4646 if ( si->si_retryinterval == NULL ) {
4647 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
4653 si->si_filter = str2filter( si->si_filterstr.bv_val );
4654 if ( si->si_filter == NULL ) {
4655 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
4656 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
4670 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
4671 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
4672 "operations required for syncrepl", c->be->be_type );
4673 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4676 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
4677 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
4678 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4681 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
4684 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
4688 si->si_bindconf.sb_tls = SB_TLS_OFF;
4689 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
4690 si->si_schemachecking = 0;
4691 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
4692 &si->si_filterstr );
4693 si->si_base.bv_val = NULL;
4694 si->si_scope = LDAP_SCOPE_SUBTREE;
4695 si->si_attrsonly = 0;
4696 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4697 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4698 si->si_attrs = NULL;
4699 si->si_allattrs = 0;
4700 si->si_allopattrs = 0;
4701 si->si_exattrs = NULL;
4702 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4703 si->si_interval = 86400;
4704 si->si_retryinterval = NULL;
4705 si->si_retrynum_init = NULL;
4706 si->si_retrynum = NULL;
4707 si->si_manageDSAit = 0;
4711 si->si_presentlist = NULL;
4712 LDAP_LIST_INIT( &si->si_nonpresentlist );
4713 ldap_pvt_thread_mutex_init( &si->si_mutex );
4715 rc = parse_syncrepl_line( c, si );
4720 /* Must be LDAPv3 because we need controls */
4721 switch ( si->si_bindconf.sb_version ) {
4723 /* not explicitly set */
4724 si->si_bindconf.sb_version = LDAP_VERSION3;
4727 /* explicitly set */
4730 Debug( LDAP_DEBUG_ANY,
4731 "version %d incompatible with syncrepl\n",
4732 si->si_bindconf.sb_version, 0, 0 );
4733 syncinfo_free( si, 0 );
4737 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
4738 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4739 "<%s> invalid URL", c->argv[0] );
4740 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
4741 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
4746 if ( slapMode & SLAP_SERVER_MODE ) {
4748 /* check if consumer points to current server and database.
4749 * If so, ignore this configuration.
4751 if ( !SLAP_DBHIDDEN( c->be ) ) {
4753 /* if searchbase doesn't match current DB suffix,
4754 * assume it's different
4756 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
4757 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
4762 /* if searchbase matches, see if URLs match */
4763 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
4769 init_syncrepl( si );
4770 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4771 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
4772 si->si_interval, do_syncrepl, si, "do_syncrepl",
4774 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4776 rc = config_sync_shadow( c ) ? -1 : 0;
4781 /* mirrormode still needs to see this flag in tool mode */
4782 rc = config_sync_shadow( c ) ? -1 : 0;
4784 ldap_free_urldesc( lud );
4788 /* Use main slapd defaults */
4789 bindconf_tls_defaults( &si->si_bindconf );
4792 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
4793 syncinfo_free( si, 0 );
4796 Debug( LDAP_DEBUG_CONFIG,
4797 "Config: ** successfully added syncrepl %s \"%s\"\n",
4799 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
4800 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
4801 if ( c->be->be_syncinfo ) {
4804 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
4806 /* add new syncrepl to end of list (same order as when deleting) */
4807 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
4810 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
4811 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
4812 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
4814 c->be->be_syncinfo = si;
4816 si->si_cookieState->cs_ref++;
4825 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
4827 struct berval bc, uri, bs;
4828 char buf[BUFSIZ*2], *ptr;
4831 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
4835 /* temporarily inhibit bindconf from printing URI */
4836 uri = si->si_bindconf.sb_uri;
4837 BER_BVZERO( &si->si_bindconf.sb_uri );
4838 si->si_bindconf.sb_version = 0;
4839 bindconf_unparse( &si->si_bindconf, &bc );
4840 si->si_bindconf.sb_uri = uri;
4841 si->si_bindconf.sb_version = LDAP_VERSION3;
4844 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
4845 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
4846 si->si_rid, si->si_bindconf.sb_uri.bv_val );
4847 if ( len >= sizeof( buf ) ) return;
4849 if ( !BER_BVISNULL( &bc ) ) {
4850 if ( WHATSLEFT <= bc.bv_len ) {
4854 ptr = lutil_strcopy( ptr, bc.bv_val );
4857 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
4858 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
4859 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
4860 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
4863 if ( !BER_BVISNULL( &si->si_base ) ) {
4864 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
4865 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
4866 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
4869 #ifdef ENABLE_REWRITE
4870 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
4871 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
4872 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
4873 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
4877 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
4878 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
4879 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
4880 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
4883 if ( !BER_BVISNULL( &si->si_logbase ) ) {
4884 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
4885 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
4886 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
4889 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
4890 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
4891 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
4892 ptr = lutil_strcopy( ptr, bs.bv_val );
4894 if ( si->si_attrsonly ) {
4895 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4896 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
4898 if ( si->si_anfile ) {
4899 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
4900 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
4901 ptr = lutil_strcopy( ptr, si->si_anfile );
4903 } else if ( si->si_allattrs || si->si_allopattrs ||
4904 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
4908 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4909 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
4911 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
4912 if ( ptr == NULL ) return;
4913 if ( si->si_allattrs ) {
4914 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
4915 if ( old != ptr ) *ptr++ = ',';
4918 if ( si->si_allopattrs ) {
4919 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
4920 if ( old != ptr ) *ptr++ = ',';
4925 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
4926 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
4927 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
4928 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
4929 if ( ptr == NULL ) return;
4931 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
4932 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
4933 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
4935 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
4936 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
4937 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
4938 "refreshAndPersist" : "refreshOnly" );
4940 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
4943 dd = si->si_interval;
4950 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
4951 INTERVALSTR, dd, hh, mm, ss );
4952 if ( len >= WHATSLEFT ) return;
4956 if ( si->si_got & GOT_RETRY ) {
4957 const char *space = "";
4958 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
4959 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
4960 for (i=0; si->si_retryinterval[i]; i++) {
4961 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
4962 (long) si->si_retryinterval[i] );
4964 if ( WHATSLEFT - 1 <= len ) return;
4966 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
4969 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
4970 if ( WHATSLEFT <= len ) return;
4974 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
4977 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
4980 if ( si->si_slimit ) {
4981 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
4982 if ( WHATSLEFT <= len ) return;
4986 if ( si->si_tlimit ) {
4987 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
4988 if ( WHATSLEFT <= len ) return;
4992 if ( si->si_syncdata ) {
4993 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
4994 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
4995 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
4996 ptr = lutil_strcopy( ptr, bc.bv_val );
4999 bc.bv_len = ptr - buf;
5001 ber_dupbv( bv, &bc );
5005 syncrepl_config( ConfigArgs *c )
5007 if (c->op == SLAP_CONFIG_EMIT) {
5008 if ( c->be->be_syncinfo ) {
5012 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
5013 syncrepl_unparse( si, &bv );
5014 ber_bvarray_add( &c->rvalue_vals, &bv );
5019 } else if ( c->op == LDAP_MOD_DELETE ) {
5021 if ( c->be->be_syncinfo ) {
5022 syncinfo_t *si, **sip;
5025 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
5027 if ( c->valx == -1 || i == c->valx ) {
5031 /* If the task is currently active, we have to leave
5032 * it running. It will exit on its own. This will only
5033 * happen when running on the cn=config DB.
5036 if ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
5039 /* There is no active thread, but we must still
5040 * ensure that no thread is (or will be) queued
5041 * while we removes the task.
5043 struct re_s *re = si->si_re;
5046 if ( si->si_conn ) {
5047 connection_client_stop( si->si_conn );
5051 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5052 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
5053 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5056 ldap_pvt_runqueue_remove( &slapd_rq, re );
5057 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5059 if ( ldap_pvt_thread_pool_retract( &connection_pool,
5060 re->routine, re ) > 0 )
5063 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5067 syncinfo_free( si, 0 );
5076 if ( !c->be->be_syncinfo ) {
5077 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5081 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5082 Debug(LDAP_DEBUG_ANY, "%s: "
5083 "syncrepl: database already shadowed.\n",
5087 return add_syncrepl( c );