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-2010 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 );
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 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
689 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
690 si->si_syncCookie.sid );
691 /* Look for contextCSN from syncprov overlay. */
692 check_syncprov( op, si );
695 si->si_refreshDone = 0;
697 rc = ldap_sync_search( si, op->o_tmpmemctx );
699 if( rc != LDAP_SUCCESS ) {
700 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
701 "ldap_search_ext: %s (%d)\n",
702 si->si_ridtxt, ldap_err2string( rc ), rc );
708 ldap_unbind_ext( si->si_ld, NULL, NULL );
717 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
724 if ( sc1->numcsns < sc2->numcsns ) {
725 *which = sc1->numcsns;
729 for (j=0; j<sc2->numcsns; j++) {
730 for (i=0; i<sc1->numcsns; i++) {
731 if ( sc1->sids[i] != sc2->sids[j] )
733 value_match( &match, slap_schema.si_ad_entryCSN,
734 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
735 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
736 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
743 if ( i == sc1->numcsns ) {
744 /* sc2 has a sid sc1 lacks */
752 #define SYNC_PAUSED -3
759 BerElementBuffer berbuf;
760 BerElement *ber = (BerElement *)&berbuf;
762 LDAPMessage *msg = NULL;
764 struct sync_cookie syncCookie = { NULL };
765 struct sync_cookie syncCookie_req = { NULL };
770 Modifications *modlist = NULL;
774 struct timeval *tout_p = NULL;
775 struct timeval tout = { 0, 0 };
777 int refreshDeletes = 0;
779 if ( slapd_shutdown ) {
784 ber_init2( ber, NULL, LBER_USE_DER );
785 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
787 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
789 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
791 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
797 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
798 tout_p, &msg ) ) > 0 )
800 int match, punlock, syncstate;
801 struct berval *retdata, syncUUID, cookie = BER_BVNULL;
803 LDAPControl **rctrls = NULL, *rctrlp = NULL;
809 if ( slapd_shutdown ) {
813 switch( ldap_msgtype( msg ) ) {
814 case LDAP_RES_SEARCH_ENTRY:
815 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
816 /* we can't work without the control */
818 LDAPControl **next = NULL;
819 /* NOTE: make sure we use the right one;
820 * a better approach would be to run thru
821 * the whole list and take care of all */
822 /* NOTE: since we issue the search request,
823 * we should know what controls to expect,
824 * and there should be none apart from the
825 * sync-related control */
826 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
827 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
829 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
830 "got search entry with multiple "
831 "Sync State control\n", si->si_ridtxt, 0, 0 );
832 ldap_controls_free( rctrls );
837 if ( rctrlp == NULL ) {
838 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
839 "got search entry without "
840 "Sync State control\n", si->si_ridtxt, 0, 0 );
844 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
845 if ( ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID )
847 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s malformed message",
848 si->si_ridtxt, 0, 0 );
849 ldap_controls_free( rctrls );
853 /* FIXME: what if syncUUID is NULL or empty?
854 * (happens with back-sql...) */
855 if ( BER_BVISEMPTY( &syncUUID ) ) {
856 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
857 "got empty syncUUID with LDAP_SYNC_%s\n",
859 syncrepl_state2str( syncstate ), 0 );
860 ldap_controls_free( rctrls );
865 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
866 ber_scanf( ber, /*"{"*/ "m}", &cookie );
868 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
870 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
872 if ( !BER_BVISNULL( &cookie ) ) {
873 ch_free( syncCookie.octet_str.bv_val );
874 ber_dupbv( &syncCookie.octet_str, &cookie );
876 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
878 slap_parse_sync_cookie( &syncCookie, NULL );
879 if ( syncCookie.ctxcsn ) {
880 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
881 check_syncprov( op, si );
882 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
883 if ( si->si_cookieState->cs_sids[i] == sid ) {
884 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
885 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s\n",
886 si->si_ridtxt, syncCookie.ctxcsn->bv_val, 0 );
887 ldap_controls_free( rctrls );
894 /* check pending CSNs too */
895 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
896 if ( slapd_shutdown ) {
900 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
901 ldap_pvt_thread_yield();
903 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
904 if ( si->si_cookieState->cs_psids[i] == sid ) {
905 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
906 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s\n",
907 si->si_ridtxt, syncCookie.ctxcsn->bv_val, 0 );
908 ldap_controls_free( rctrls );
910 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
913 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
918 /* new SID, add it */
919 if ( i == si->si_cookieState->cs_pnum ) {
920 value_add( &si->si_cookieState->cs_pvals, syncCookie.ctxcsn );
921 si->si_cookieState->cs_pnum++;
922 si->si_cookieState->cs_psids = ch_realloc( si->si_cookieState->cs_psids, si->si_cookieState->cs_pnum * sizeof(int));
923 si->si_cookieState->cs_psids[i] = sid;
925 assert( punlock < 0 );
928 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
932 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
934 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
937 rc = syncrepl_updateCookie( si, op, &syncCookie );
938 } else switch ( rc ) {
939 case LDAP_ALREADY_EXISTS:
940 case LDAP_NO_SUCH_OBJECT:
941 case LDAP_NO_SUCH_ATTRIBUTE:
942 case LDAP_TYPE_OR_VALUE_EXISTS:
943 rc = LDAP_SYNC_REFRESH_REQUIRED;
944 si->si_logstate = SYNCLOG_FALLBACK;
945 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
950 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
951 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
953 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
954 syncstate, &syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
957 rc = syncrepl_updateCookie( si, op, &syncCookie );
960 if ( punlock >= 0 ) {
961 /* on failure, revert pending CSN */
962 if ( rc != LDAP_SUCCESS ) {
964 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
965 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
966 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
967 &si->si_cookieState->cs_vals[i] );
971 if ( i == si->si_cookieState->cs_num )
972 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
974 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
976 ldap_controls_free( rctrls );
978 slap_mods_free( modlist, 1 );
984 case LDAP_RES_SEARCH_REFERENCE:
985 Debug( LDAP_DEBUG_ANY,
986 "do_syncrep2: %s reference received error\n",
987 si->si_ridtxt, 0, 0 );
990 case LDAP_RES_SEARCH_RESULT:
991 Debug( LDAP_DEBUG_SYNC,
992 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
993 si->si_ridtxt, 0, 0 );
994 err = LDAP_OTHER; /* FIXME check parse result properly */
995 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
997 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
998 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
999 /* map old result code to registered code */
1000 err = LDAP_SYNC_REFRESH_REQUIRED;
1003 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
1004 if ( si->si_logstate == SYNCLOG_LOGGING ) {
1005 si->si_logstate = SYNCLOG_FALLBACK;
1011 Debug( LDAP_DEBUG_ANY,
1012 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1013 si->si_ridtxt, err, ldap_err2string( err ) );
1016 LDAPControl **next = NULL;
1017 /* NOTE: make sure we use the right one;
1018 * a better approach would be to run thru
1019 * the whole list and take care of all */
1020 /* NOTE: since we issue the search request,
1021 * we should know what controls to expect,
1022 * and there should be none apart from the
1023 * sync-related control */
1024 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1025 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1027 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1028 "got search result with multiple "
1029 "Sync State control\n", si->si_ridtxt, 0, 0 );
1030 ldap_controls_free( rctrls );
1036 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1038 ber_scanf( ber, "{" /*"}"*/);
1039 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1040 ber_scanf( ber, "m", &cookie );
1042 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1044 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1046 if ( !BER_BVISNULL( &cookie ) ) {
1047 ch_free( syncCookie.octet_str.bv_val );
1048 ber_dupbv( &syncCookie.octet_str, &cookie);
1050 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1052 slap_parse_sync_cookie( &syncCookie, NULL );
1053 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1056 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1058 ber_scanf( ber, "b", &refreshDeletes );
1060 ber_scanf( ber, /*"{"*/ "}" );
1062 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1063 slap_sync_cookie_free( &syncCookie_req, 0 );
1064 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1066 if ( !syncCookie.ctxcsn ) {
1068 } else if ( !syncCookie_req.ctxcsn ) {
1072 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1075 ldap_controls_free( rctrls );
1077 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1078 /* FIXME : different error behaviors according to
1079 * 1) err code : LDAP_BUSY ...
1080 * 2) on err policy : stop service, stop sync, retry
1082 if ( refreshDeletes == 0 && match < 0 &&
1083 err == LDAP_SUCCESS &&
1084 syncCookie_req.numcsns == syncCookie.numcsns )
1086 syncrepl_del_nonpresent( op, si, NULL,
1089 avl_free( si->si_presentlist, ch_free );
1090 si->si_presentlist = NULL;
1093 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1095 rc = syncrepl_updateCookie( si, op, &syncCookie );
1097 if ( err == LDAP_SUCCESS
1098 && si->si_logstate == SYNCLOG_FALLBACK ) {
1099 si->si_logstate = SYNCLOG_LOGGING;
1100 rc = LDAP_SYNC_REFRESH_REQUIRED;
1106 case LDAP_RES_INTERMEDIATE:
1109 rc = ldap_parse_intermediate( si->si_ld, msg,
1110 &retoid, &retdata, NULL, 0 );
1111 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1112 ber_init2( ber, retdata, LBER_USE_DER );
1114 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1116 case LDAP_TAG_SYNC_NEW_COOKIE:
1117 Debug( LDAP_DEBUG_SYNC,
1118 "do_syncrep2: %s %s - %s\n",
1120 "LDAP_RES_INTERMEDIATE",
1122 ber_scanf( ber, "tm", &tag, &cookie );
1123 Debug( LDAP_DEBUG_SYNC,
1124 "do_syncrep2: %s NEW_COOKIE: %s\n",
1127 if ( !BER_BVISNULL( &cookie ) ) {
1128 ch_free( syncCookie.octet_str.bv_val );
1129 ber_dupbv( &syncCookie.octet_str, &cookie );
1131 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1132 slap_parse_sync_cookie( &syncCookie, NULL );
1133 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1136 case LDAP_TAG_SYNC_REFRESH_DELETE:
1137 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1138 Debug( LDAP_DEBUG_SYNC,
1139 "do_syncrep2: %s %s - %s\n",
1141 "LDAP_RES_INTERMEDIATE",
1142 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1143 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1144 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1145 si->si_refreshDelete = 1;
1147 si->si_refreshPresent = 1;
1149 ber_scanf( ber, "t{" /*"}"*/, &tag );
1150 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1152 ber_scanf( ber, "m", &cookie );
1154 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1156 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1158 if ( !BER_BVISNULL( &cookie ) ) {
1159 ch_free( syncCookie.octet_str.bv_val );
1160 ber_dupbv( &syncCookie.octet_str, &cookie );
1162 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1164 slap_parse_sync_cookie( &syncCookie, NULL );
1165 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1168 /* Defaults to TRUE */
1169 if ( ber_peek_tag( ber, &len ) ==
1170 LDAP_TAG_REFRESHDONE )
1172 ber_scanf( ber, "b", &si->si_refreshDone );
1175 si->si_refreshDone = 1;
1177 ber_scanf( ber, /*"{"*/ "}" );
1179 case LDAP_TAG_SYNC_ID_SET:
1180 Debug( LDAP_DEBUG_SYNC,
1181 "do_syncrep2: %s %s - %s\n",
1183 "LDAP_RES_INTERMEDIATE",
1185 ber_scanf( ber, "t{" /*"}"*/, &tag );
1186 if ( ber_peek_tag( ber, &len ) ==
1187 LDAP_TAG_SYNC_COOKIE )
1189 ber_scanf( ber, "m", &cookie );
1191 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1193 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1195 if ( !BER_BVISNULL( &cookie ) ) {
1196 ch_free( syncCookie.octet_str.bv_val );
1197 ber_dupbv( &syncCookie.octet_str, &cookie );
1199 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1201 slap_parse_sync_cookie( &syncCookie, NULL );
1202 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1203 compare_csns( &syncCookie_req, &syncCookie, &m );
1206 if ( ber_peek_tag( ber, &len ) ==
1207 LDAP_TAG_REFRESHDELETES )
1209 ber_scanf( ber, "b", &refreshDeletes );
1212 ber_scanf( ber, "[W]", &syncUUIDs );
1213 ber_scanf( ber, /*"{"*/ "}" );
1214 if ( refreshDeletes ) {
1215 syncrepl_del_nonpresent( op, si, syncUUIDs,
1217 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1220 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1221 (void)avl_presentlist_insert( si, &syncUUIDs[i] );
1222 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1224 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1226 slap_sync_cookie_free( &syncCookie, 0 );
1229 Debug( LDAP_DEBUG_ANY,
1230 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1231 si->si_ridtxt, (long) si_tag, 0 );
1232 ldap_memfree( retoid );
1233 ber_bvfree( retdata );
1237 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1238 slap_sync_cookie_free( &syncCookie_req, 0 );
1239 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1241 if ( !syncCookie.ctxcsn ) {
1243 } else if ( !syncCookie_req.ctxcsn ) {
1247 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1251 if ( si->si_refreshPresent == 1 &&
1252 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1253 syncCookie_req.numcsns == syncCookie.numcsns ) {
1254 syncrepl_del_nonpresent( op, si, NULL,
1258 if ( syncCookie.ctxcsn )
1260 rc = syncrepl_updateCookie( si, op, &syncCookie);
1264 ldap_memfree( retoid );
1265 ber_bvfree( retdata );
1268 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1269 "unknown intermediate response (%d)\n",
1270 si->si_ridtxt, rc, 0 );
1271 ldap_memfree( retoid );
1272 ber_bvfree( retdata );
1277 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1278 "unknown message (0x%02lx)\n",
1280 (unsigned long)ldap_msgtype( msg ), 0 );
1284 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1285 slap_sync_cookie_free( &syncCookie_req, 0 );
1286 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1287 slap_sync_cookie_free( &syncCookie, 0 );
1289 ldap_msgfree( msg );
1291 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1292 slap_sync_cookie_free( &syncCookie, 0 );
1293 slap_sync_cookie_free( &syncCookie_req, 0 );
1300 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1305 if ( err != LDAP_SUCCESS ) {
1306 Debug( LDAP_DEBUG_ANY,
1307 "do_syncrep2: %s (%d) %s\n",
1308 si->si_ridtxt, err, ldap_err2string( err ) );
1311 slap_sync_cookie_free( &syncCookie, 0 );
1312 slap_sync_cookie_free( &syncCookie_req, 0 );
1314 if ( msg ) ldap_msgfree( msg );
1316 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1317 if ( si->si_conn ) {
1318 connection_client_stop( si->si_conn );
1321 ldap_unbind_ext( si->si_ld, NULL, NULL );
1333 struct re_s* rtask = arg;
1334 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1335 Connection conn = {0};
1336 OperationBuffer opbuf;
1338 int rc = LDAP_SUCCESS;
1341 int i, defer = 1, fail = 0, freeinfo = 0;
1346 if ( slapd_shutdown )
1349 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1351 /* Don't get stuck here while a pause is initiated */
1352 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1353 if ( slapd_shutdown )
1355 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1356 ldap_pvt_thread_yield();
1359 if ( si->si_ctype < 1 ) {
1363 switch( abs( si->si_type ) ) {
1364 case LDAP_SYNC_REFRESH_ONLY:
1365 case LDAP_SYNC_REFRESH_AND_PERSIST:
1368 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1372 if ( slapd_shutdown ) {
1374 if ( si->si_conn ) {
1375 connection_client_stop( si->si_conn );
1378 ldap_unbind_ext( si->si_ld, NULL, NULL );
1381 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1385 connection_fake_init( &conn, &opbuf, ctx );
1387 /* o_connids must be unique for slap_graduate_commit_csn */
1388 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1390 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1393 /* Coordinate contextCSN updates with any syncprov overlays
1394 * in use. This may be complicated by the use of the glue
1397 * Typically there is a single syncprov mastering the entire
1398 * glued tree. In that case, our contextCSN updates should
1399 * go to the master DB. But if there is no syncprov on the
1400 * master DB, then nothing special is needed here.
1402 * Alternatively, there may be individual syncprov overlays
1403 * on each glued branch. In that case, each syncprov only
1404 * knows about changes within its own branch. And so our
1405 * contextCSN updates should only go to the local DB.
1407 if ( !si->si_wbe ) {
1408 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1409 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1410 if ( overlay_is_inst( top_be, "syncprov" ))
1411 si->si_wbe = top_be;
1417 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1418 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1419 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1421 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1424 if ( !si->si_schemachecking )
1425 op->o_no_schema_check = 1;
1427 /* Establish session, do search */
1429 si->si_refreshDelete = 0;
1430 si->si_refreshPresent = 0;
1432 if ( si->si_presentlist ) {
1433 avl_free( si->si_presentlist, ch_free );
1434 si->si_presentlist = NULL;
1437 /* use main DB when retrieving contextCSN */
1438 op->o_bd = si->si_wbe;
1439 op->o_dn = op->o_bd->be_rootdn;
1440 op->o_ndn = op->o_bd->be_rootndn;
1441 rc = do_syncrep1( op, si );
1445 /* Process results */
1446 if ( rc == LDAP_SUCCESS ) {
1447 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1449 /* use current DB */
1451 op->o_dn = op->o_bd->be_rootdn;
1452 op->o_ndn = op->o_bd->be_rootndn;
1453 rc = do_syncrep2( op, si );
1454 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1455 rc = ldap_sync_search( si, op->o_tmpmemctx );
1460 /* We got deleted while running on cn=config */
1461 if ( si->si_ctype < 1 ) {
1462 if ( si->si_ctype == -1 ) {
1471 if ( rc != SYNC_PAUSED ) {
1472 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1473 /* If we succeeded, enable the connection for further listening.
1474 * If we failed, tear down the connection and reschedule.
1476 if ( rc == LDAP_SUCCESS ) {
1477 if ( si->si_conn ) {
1478 connection_client_enable( si->si_conn );
1480 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1482 } else if ( si->si_conn ) {
1486 if ( rc == -2 ) rc = 0;
1491 /* At this point, we have 5 cases:
1492 * 1) for any hard failure, give up and remove this task
1493 * 2) for ServerDown, reschedule this task to run later
1494 * 3) for threadpool pause, reschedule to run immediately
1495 * 4) for Refresh and Success, reschedule to run
1496 * 5) for Persist and Success, reschedule to defer
1498 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1500 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1501 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1505 connection_client_stop( si->si_conn );
1509 if ( rc == SYNC_PAUSED ) {
1510 rtask->interval.tv_sec = 0;
1511 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1512 rtask->interval.tv_sec = si->si_interval;
1514 } else if ( rc == LDAP_SUCCESS ) {
1515 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1518 rtask->interval.tv_sec = si->si_interval;
1519 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1520 if ( si->si_retrynum ) {
1521 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1522 si->si_retrynum[i] = si->si_retrynum_init[i];
1524 si->si_retrynum[i] = RETRYNUM_TAIL;
1527 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1528 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1532 if ( si->si_ctype < 1
1533 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1535 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1538 fail = RETRYNUM_TAIL;
1539 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1540 if ( si->si_retrynum[i] > 0 )
1541 si->si_retrynum[i]--;
1542 fail = si->si_retrynum[i];
1543 rtask->interval.tv_sec = si->si_retryinterval[i];
1544 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1545 slap_wake_listener();
1549 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1550 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1553 if ( fail == RETRYNUM_TAIL ) {
1554 Debug( LDAP_DEBUG_ANY,
1555 "do_syncrepl: %s rc %d quitting\n",
1556 si->si_ridtxt, rc, 0 );
1557 } else if ( fail > 0 ) {
1558 Debug( LDAP_DEBUG_ANY,
1559 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1560 si->si_ridtxt, rc, fail );
1562 Debug( LDAP_DEBUG_ANY,
1563 "do_syncrepl: %s rc %d retrying\n",
1564 si->si_ridtxt, rc, 0 );
1568 /* Do final delete cleanup */
1570 syncinfo_free( si, 0 );
1575 #ifdef ENABLE_REWRITE
1577 syncrepl_rewrite_dn(
1580 struct berval *sdn )
1585 nul = dn->bv_val[dn->bv_len];
1586 dn->bv_val[dn->bv_len] = 0;
1587 rc = rewrite( si->si_rewrite, SUFFIXM_CTX, dn->bv_val, &sdn->bv_val );
1588 dn->bv_val[dn->bv_len] = nul;
1590 if ( sdn->bv_val == dn->bv_val )
1592 else if ( rc == REWRITE_REGEXEC_OK && sdn->bv_val )
1593 sdn->bv_len = strlen( sdn->bv_val );
1596 #define REWRITE_VAL(si, ad, bv, bv2) \
1597 BER_BVZERO( &bv2 ); \
1598 if ( si->si_rewrite && ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName) \
1599 syncrepl_rewrite_dn( si, &bv, &bv2); \
1600 if ( BER_BVISNULL( &bv2 )) \
1601 ber_dupbv( &bv2, &bv )
1602 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1603 BER_BVZERO( &bv2 ); \
1604 if (si->si_rewrite) \
1605 syncrepl_rewrite_dn(si, &bv, &bv2); \
1606 rc = dnPrettyNormal( NULL, bv2.bv_val ? &bv2 : &bv, &dn, &ndn, op->o_tmpmemctx ); \
1609 #define REWRITE_VAL(si, ad, bv, bv2) ber_dupbv(&bv2, &bv)
1610 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1611 rc = dnPrettyNormal( NULL, &bv, &dn, &ndn, op->o_tmpmemctx )
1615 static slap_verbmasks modops[] = {
1616 { BER_BVC("add"), LDAP_REQ_ADD },
1617 { BER_BVC("delete"), LDAP_REQ_DELETE },
1618 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1619 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1624 syncrepl_accesslog_mods(
1626 struct berval *vals,
1627 struct Modifications **modres
1632 AttributeDescription *ad;
1633 struct berval bv, bv2;
1635 Modifications *mod = NULL, *modlist = NULL, **modtail;
1640 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1644 colon = ber_bvchr( &bv, ':' );
1650 bv.bv_len = colon - bv.bv_val;
1651 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1653 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1654 "Invalid attribute %s, %s\n",
1655 si->si_ridtxt, bv.bv_val, text );
1656 slap_mods_free( modlist, 1 );
1662 /* Ignore dynamically generated attrs */
1663 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1667 /* Ignore excluded attrs */
1668 if ( ldap_charray_inlist( si->si_exattrs,
1669 ad->ad_type->sat_cname.bv_val ) )
1675 case '+': op = LDAP_MOD_ADD; break;
1676 case '-': op = LDAP_MOD_DELETE; break;
1677 case '=': op = LDAP_MOD_REPLACE; break;
1678 case '#': op = LDAP_MOD_INCREMENT; break;
1682 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1683 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1686 mod->sml_next = NULL;
1688 mod->sml_type = ad->ad_cname;
1689 mod->sml_values = NULL;
1690 mod->sml_nvalues = NULL;
1691 mod->sml_numvals = 0;
1694 modtail = &mod->sml_next;
1696 if ( colon[2] == ' ' ) {
1697 bv.bv_val = colon + 3;
1698 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1699 REWRITE_VAL( si, ad, bv, bv2 );
1700 ber_bvarray_add( &mod->sml_values, &bv2 );
1709 syncrepl_changelog_mods(
1711 struct berval *vals,
1712 struct Modifications **modres
1715 return -1; /* FIXME */
1719 syncrepl_message_to_op(
1725 BerElement *ber = NULL;
1726 Modifications *modlist = NULL;
1728 SlapReply rs = { REP_RESULT };
1729 slap_callback cb = { NULL, null_callback, NULL, NULL };
1732 char txtbuf[SLAP_TEXT_BUFLEN];
1733 size_t textlen = sizeof txtbuf;
1735 struct berval bdn, dn = BER_BVNULL, ndn;
1736 struct berval bv, bv2, *bvals = NULL;
1737 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1738 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1739 psup = BER_BVNULL, nsup = BER_BVNULL;
1740 int rc, deleteOldRdn = 0, freeReqDn = 0;
1741 int do_graduate = 0;
1743 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1744 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1745 "Message type should be entry (%d)",
1746 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1750 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1755 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1757 if ( rc != LDAP_SUCCESS ) {
1758 Debug( LDAP_DEBUG_ANY,
1759 "syncrepl_message_to_op: %s dn get failed (%d)",
1760 si->si_ridtxt, rc, 0 );
1764 op->o_tag = LBER_DEFAULT;
1765 op->o_bd = si->si_wbe;
1767 if ( BER_BVISEMPTY( &bdn )) {
1768 Debug( LDAP_DEBUG_ANY,
1769 "syncrepl_message_to_op: %s got empty dn",
1770 si->si_ridtxt, 0, 0 );
1774 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
1776 if ( bv.bv_val == NULL )
1779 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
1781 REWRITE_DN( si, bdn, bv2, dn, ndn );
1782 if ( rc != LDAP_SUCCESS ) {
1783 Debug( LDAP_DEBUG_ANY,
1784 "syncrepl_message_to_op: %s "
1785 "dn \"%s\" normalization failed (%d)",
1786 si->si_ridtxt, bdn.bv_val, rc );
1791 ber_dupbv( &op->o_req_dn, &dn );
1792 ber_dupbv( &op->o_req_ndn, &ndn );
1793 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1794 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1796 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
1797 int i = verb_to_mask( bvals[0].bv_val, modops );
1799 Debug( LDAP_DEBUG_ANY,
1800 "syncrepl_message_to_op: %s unknown op %s",
1801 si->si_ridtxt, bvals[0].bv_val, 0 );
1806 op->o_tag = modops[i].mask;
1807 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
1808 /* Parse attribute into modlist */
1809 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
1810 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
1812 rc = syncrepl_changelog_mods( si, bvals, &modlist );
1814 if ( rc ) goto done;
1815 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
1817 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
1818 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
1821 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
1823 } else if ( !ber_bvstrcasecmp( &bv,
1824 &slap_schema.si_ad_entryCSN->ad_cname ) )
1826 slap_queue_csn( op, bvals );
1832 /* If we didn't get a mod type or a target DN, bail out */
1833 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
1838 op->o_callback = &cb;
1839 slap_op_time( &op->o_time, &op->o_tincr );
1841 switch( op->o_tag ) {
1843 case LDAP_REQ_MODIFY:
1844 /* If we didn't get required data, bail */
1845 if ( !modlist ) goto done;
1847 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
1849 if ( rc != LDAP_SUCCESS ) {
1850 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1851 "mods check (%s)\n",
1852 si->si_ridtxt, text, 0 );
1856 if ( op->o_tag == LDAP_REQ_ADD ) {
1857 Entry *e = entry_alloc();
1859 op->ora_e->e_name = op->o_req_dn;
1860 op->ora_e->e_nname = op->o_req_ndn;
1862 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1863 if( rc != LDAP_SUCCESS ) {
1864 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1865 "mods2entry (%s)\n",
1866 si->si_ridtxt, text, 0 );
1868 rc = op->o_bd->be_add( op, &rs );
1869 Debug( LDAP_DEBUG_SYNC,
1870 "syncrepl_message_to_op: %s be_add %s (%d)\n",
1871 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1874 if ( e == op->ora_e )
1875 be_entry_release_w( op, op->ora_e );
1877 op->orm_modlist = modlist;
1878 op->o_bd = si->si_wbe;
1879 rc = op->o_bd->be_modify( op, &rs );
1880 modlist = op->orm_modlist;
1881 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1882 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
1883 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1884 op->o_bd = si->si_be;
1888 case LDAP_REQ_MODRDN:
1889 if ( BER_BVISNULL( &rdn ) ) goto done;
1891 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
1894 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
1897 if ( !BER_BVISNULL( &sup ) ) {
1898 REWRITE_DN( si, sup, bv2, psup, nsup );
1901 op->orr_newSup = &psup;
1902 op->orr_nnewSup = ⊅
1904 op->orr_newSup = NULL;
1905 op->orr_nnewSup = NULL;
1907 op->orr_newrdn = prdn;
1908 op->orr_nnewrdn = nrdn;
1909 op->orr_deleteoldrdn = deleteOldRdn;
1910 op->orr_modlist = NULL;
1911 if ( slap_modrdn2mods( op, &rs ) ) {
1915 /* Append modlist for operational attrs */
1919 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
1921 m->sml_next = modlist;
1924 rc = op->o_bd->be_modrdn( op, &rs );
1925 slap_mods_free( op->orr_modlist, 1 );
1926 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1927 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
1928 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1931 case LDAP_REQ_DELETE:
1932 rc = op->o_bd->be_delete( op, &rs );
1933 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1934 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
1935 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1941 slap_graduate_commit_csn( op );
1942 op->o_bd = si->si_be;
1943 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1944 BER_BVZERO( &op->o_csn );
1946 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1948 if ( !BER_BVISNULL( &rdn ) ) {
1949 if ( !BER_BVISNULL( &nsup ) ) {
1950 ch_free( nsup.bv_val );
1952 if ( !BER_BVISNULL( &psup ) ) {
1953 ch_free( psup.bv_val );
1955 if ( !BER_BVISNULL( &nrdn ) ) {
1956 ch_free( nrdn.bv_val );
1958 if ( !BER_BVISNULL( &prdn ) ) {
1959 ch_free( prdn.bv_val );
1963 ch_free( op->o_req_ndn.bv_val );
1964 ch_free( op->o_req_dn.bv_val );
1971 syncrepl_message_to_entry(
1975 Modifications **modlist,
1981 BerElement *ber = NULL;
1984 Modifications **modtail = modlist;
1987 char txtbuf[SLAP_TEXT_BUFLEN];
1988 size_t textlen = sizeof txtbuf;
1990 struct berval bdn = BER_BVNULL, dn, ndn, bv2;
1995 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1996 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
1997 "Message type should be entry (%d)",
1998 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2002 op->o_tag = LDAP_REQ_ADD;
2004 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2005 if ( rc != LDAP_SUCCESS ) {
2006 Debug( LDAP_DEBUG_ANY,
2007 "syncrepl_message_to_entry: %s dn get failed (%d)",
2008 si->si_ridtxt, rc, 0 );
2012 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
2013 Debug( LDAP_DEBUG_ANY,
2014 "syncrepl_message_to_entry: %s got empty dn",
2015 si->si_ridtxt, 0, 0 );
2019 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
2020 /* NOTE: this could be done even before decoding the DN,
2021 * although encoding errors wouldn't be detected */
2026 if ( entry == NULL ) {
2030 REWRITE_DN( si, bdn, bv2, dn, ndn );
2031 if ( rc != LDAP_SUCCESS ) {
2032 /* One of the things that could happen is that the schema
2033 * is not lined-up; this could result in unknown attributes.
2034 * A value non conformant to the syntax should be unlikely,
2035 * except when replicating between different versions
2036 * of the software, or when syntax validation bugs are fixed
2038 Debug( LDAP_DEBUG_ANY,
2039 "syncrepl_message_to_entry: "
2040 "%s dn \"%s\" normalization failed (%d)",
2041 si->si_ridtxt, bdn.bv_val, rc );
2045 ber_dupbv( &op->o_req_dn, &dn );
2046 ber_dupbv( &op->o_req_ndn, &ndn );
2047 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2048 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2050 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
2053 e->e_name = op->o_req_dn;
2054 e->e_nname = op->o_req_ndn;
2056 while ( ber_remaining( ber ) ) {
2057 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
2058 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2063 /* Drop all updates to the contextCSN of the context entry
2066 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2067 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2068 ber_bvarray_free( tmp.sml_values );
2072 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2074 mod->sml_op = LDAP_MOD_REPLACE;
2076 mod->sml_next = NULL;
2077 mod->sml_desc = NULL;
2078 mod->sml_type = tmp.sml_type;
2079 mod->sml_values = tmp.sml_values;
2080 mod->sml_nvalues = NULL;
2081 mod->sml_numvals = 0; /* slap_mods_check will set this */
2083 #ifdef ENABLE_REWRITE
2084 if (si->si_rewrite) {
2085 AttributeDescription *ad = NULL;
2086 slap_bv2ad( &tmp.sml_type, &ad, &text );
2089 mod->sml_type = ad->ad_cname;
2090 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
2092 for ( i = 0; tmp.sml_values[i].bv_val; i++ ) {
2093 syncrepl_rewrite_dn( si, &tmp.sml_values[i], &bv2);
2094 if ( !BER_BVISNULL( &bv2 )) {
2095 ber_memfree( tmp.sml_values[i].bv_val );
2096 tmp.sml_values[i] = bv2;
2104 modtail = &mod->sml_next;
2107 if ( *modlist == NULL ) {
2108 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2109 si->si_ridtxt, 0, 0 );
2114 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2116 if ( rc != LDAP_SUCCESS ) {
2117 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2118 si->si_ridtxt, text, 0 );
2122 /* Strip out dynamically generated attrs */
2123 for ( modtail = modlist; *modtail ; ) {
2125 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2126 *modtail = mod->sml_next;
2127 slap_mod_free( &mod->sml_mod, 0 );
2130 modtail = &mod->sml_next;
2134 /* Strip out attrs in exattrs list */
2135 for ( modtail = modlist; *modtail ; ) {
2137 if ( ldap_charray_inlist( si->si_exattrs,
2138 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2140 *modtail = mod->sml_next;
2141 slap_mod_free( &mod->sml_mod, 0 );
2144 modtail = &mod->sml_next;
2148 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2149 if( rc != LDAP_SUCCESS ) {
2150 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2151 si->si_ridtxt, text, 0 );
2156 if ( rc != LDAP_SUCCESS ) {
2168 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2170 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2171 * entry if a previous refresh was interrupted before sending us a new
2172 * context state. We try to compare the new entry to the existing entry
2173 * and ignore the new entry if they are the same.
2175 * Also, we may get an update where the entryDN has changed, due to
2176 * a ModDn on the provider. We detect this as well, so we can issue
2177 * the corresponding operation locally.
2179 * In the case of a modify, we get a list of all the attributes
2180 * in the original entry. Rather than deleting the entry and re-adding it,
2181 * we issue a Modify request that deletes all the attributes and adds all
2182 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2185 * We otherwise distinguish ModDN from Modify; in the case of
2186 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2187 * operational attributes from the entry, and do a regular ModDN.
2189 typedef struct dninfo {
2193 struct berval nnewSup;
2194 int renamed; /* Was an existing entry renamed? */
2195 int delOldRDN; /* Was old RDN deleted? */
2196 Modifications **modlist; /* the modlist we received */
2197 Modifications *mods; /* the modlist we compared */
2198 Attribute *oldNattr; /* old naming attr */
2199 AttributeDescription *oldDesc; /* for renames */
2200 AttributeDescription *newDesc; /* for renames */
2203 /* return 1 if inserted, 0 otherwise */
2205 avl_presentlist_insert(
2207 struct berval *syncUUID )
2209 struct berval *syncuuid_bv = ch_malloc( sizeof( struct berval ) + syncUUID->bv_len + 1 );
2211 syncuuid_bv->bv_len = syncUUID->bv_len;
2212 syncuuid_bv->bv_val = (char *)&syncuuid_bv[1];
2213 AC_MEMCPY( syncuuid_bv->bv_val, syncUUID->bv_val, syncUUID->bv_len );
2214 syncuuid_bv->bv_val[ syncuuid_bv->bv_len ] = '\0';
2216 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
2217 syncuuid_cmp, avl_dup_error ) )
2219 ch_free( syncuuid_bv );
2231 Modifications** modlist,
2233 struct berval* syncUUID,
2234 struct berval* syncCSN )
2236 Backend *be = op->o_bd;
2237 slap_callback cb = { NULL, NULL, NULL, NULL };
2238 int syncuuid_inserted = 0;
2239 struct berval syncUUID_strrep = BER_BVNULL;
2241 SlapReply rs_search = {REP_RESULT};
2242 SlapReply rs_delete = {REP_RESULT};
2243 SlapReply rs_add = {REP_RESULT};
2244 SlapReply rs_modify = {REP_RESULT};
2246 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2247 int rc = LDAP_SUCCESS;
2249 struct berval pdn = BER_BVNULL;
2254 Debug( LDAP_DEBUG_SYNC,
2255 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2256 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2258 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2259 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2260 syncuuid_inserted = avl_presentlist_insert( si, syncUUID );
2264 if ( syncstate == LDAP_SYNC_PRESENT ) {
2266 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2267 if ( entry == NULL ) {
2272 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
2273 if ( syncstate != LDAP_SYNC_DELETE ) {
2274 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2277 /* add if missing */
2278 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2279 &syncUUID_strrep, syncUUID );
2281 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2282 /* replace only if necessary */
2283 if ( a->a_nvals != a->a_vals ) {
2284 ber_memfree( a->a_nvals[0].bv_val );
2285 ber_dupbv( &a->a_nvals[0], syncUUID );
2287 ber_memfree( a->a_vals[0].bv_val );
2288 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
2292 f.f_choice = LDAP_FILTER_EQUALITY;
2294 ava.aa_desc = slap_schema.si_ad_entryUUID;
2295 ava.aa_value = *syncUUID;
2297 if ( syncuuid_inserted ) {
2298 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2299 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
2301 op->ors_filter = &f;
2303 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
2304 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2305 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2306 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2307 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2308 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
2309 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2310 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2312 op->o_tag = LDAP_REQ_SEARCH;
2313 op->ors_scope = LDAP_SCOPE_SUBTREE;
2314 op->ors_deref = LDAP_DEREF_NEVER;
2316 /* get the entry for this UUID */
2317 #ifdef ENABLE_REWRITE
2318 if ( si->si_rewrite ) {
2319 op->o_req_dn = si->si_suffixm;
2320 op->o_req_ndn = si->si_suffixm;
2324 op->o_req_dn = si->si_base;
2325 op->o_req_ndn = si->si_base;
2328 op->o_time = slap_get_time();
2329 op->ors_tlimit = SLAP_NO_LIMIT;
2331 op->ors_limit = NULL;
2333 op->ors_attrs = slap_anlist_all_attributes;
2334 op->ors_attrsonly = 0;
2336 /* set callback function */
2337 op->o_callback = &cb;
2338 cb.sc_response = dn_callback;
2339 cb.sc_private = &dni;
2340 dni.new_entry = entry;
2341 dni.modlist = modlist;
2343 rc = be->be_search( op, &rs_search );
2344 Debug( LDAP_DEBUG_SYNC,
2345 "syncrepl_entry: %s be_search (%d)\n",
2346 si->si_ridtxt, rc, 0 );
2348 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2349 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2352 cb.sc_response = null_callback;
2355 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2356 Debug( LDAP_DEBUG_SYNC,
2357 "syncrepl_entry: %s %s\n",
2358 si->si_ridtxt, entry->e_name.bv_val, 0 );
2360 Debug( LDAP_DEBUG_SYNC,
2361 "syncrepl_entry: %s %s\n",
2362 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2365 assert( BER_BVISNULL( &op->o_csn ) );
2367 slap_queue_csn( op, syncCSN );
2370 slap_op_time( &op->o_time, &op->o_tincr );
2371 switch ( syncstate ) {
2373 case LDAP_SYNC_MODIFY:
2374 if ( BER_BVISNULL( &op->o_csn ))
2377 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2379 /* FIXME: op->o_csn is assumed to be
2380 * on the thread's slab; this needs
2381 * to be cleared ASAP.
2382 * What happens if already present?
2384 assert( BER_BVISNULL( &op->o_csn ) );
2385 op->o_csn = a->a_vals[0];
2390 if ( BER_BVISNULL( &dni.dn ) ) {
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 = { 0 };
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;
2479 op->o_tag = LDAP_REQ_MODRDN;
2480 dnRdn( &entry->e_name, &op->orr_newrdn );
2481 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2483 if ( !BER_BVISNULL( &dni.nnewSup )) {
2484 dnParent( &entry->e_name, &newp );
2485 op->orr_newSup = &newp;
2486 op->orr_nnewSup = &dni.nnewSup;
2488 op->orr_newSup = NULL;
2489 op->orr_nnewSup = NULL;
2491 op->orr_deleteoldrdn = dni.delOldRDN;
2492 op->orr_modlist = NULL;
2493 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2497 /* Drop the RDN-related mods from this op, because their
2498 * equivalents were just setup by slap_modrdn2mods.
2500 * If delOldRDN is TRUE then we should see a delete modop
2501 * for oldDesc. We might see a replace instead.
2502 * delete with no values: therefore newDesc != oldDesc.
2503 * if oldNattr had only one value, then Drop this op.
2504 * delete with 1 value: can only be the oldRDN value. Drop op.
2505 * delete with N values: Drop oldRDN value, keep remainder.
2506 * replace with 1 value: if oldNattr had only one value and
2507 * newDesc == oldDesc, Drop this op.
2508 * Any other cases must be left intact.
2510 * We should also see an add modop for newDesc. (But not if
2511 * we got a replace modop due to delOldRDN.) If it has
2512 * multiple values, we'll have to drop the new RDN value.
2514 modtail = &op->orr_modlist;
2515 if ( dni.delOldRDN ) {
2516 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2517 if ( (*ml)->sml_desc == dni.oldDesc ) {
2519 if ( mod->sml_op == LDAP_MOD_REPLACE &&
2520 dni.oldDesc != dni.newDesc ) {
2521 /* This Replace is due to other Mods.
2526 if ( mod->sml_numvals <= 1 &&
2527 dni.oldNattr->a_numvals == 1 &&
2528 ( mod->sml_op == LDAP_MOD_DELETE ||
2529 mod->sml_op == LDAP_MOD_REPLACE )) {
2530 if ( mod->sml_op == LDAP_MOD_REPLACE )
2533 *ml = mod->sml_next;
2534 mod->sml_next = NULL;
2535 slap_mods_free( mod, 1 );
2538 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
2540 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2541 if ( m2->sml_desc == dni.oldDesc &&
2542 m2->sml_op == LDAP_MOD_DELETE ) break;
2544 for ( i=0; i<mod->sml_numvals; i++ ) {
2545 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2547 ch_free( mod->sml_values[i].bv_val );
2548 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2549 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2550 if ( mod->sml_nvalues ) {
2551 ch_free( mod->sml_nvalues[i].bv_val );
2552 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2553 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2558 if ( !mod->sml_numvals ) {
2560 *ml = mod->sml_next;
2561 mod->sml_next = NULL;
2562 slap_mods_free( mod, 1 );
2568 if ( !got_replace ) {
2569 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2570 if ( (*ml)->sml_desc == dni.newDesc ) {
2572 if ( mod->sml_op != LDAP_MOD_ADD )
2574 if ( mod->sml_numvals == 1 ) {
2576 *ml = mod->sml_next;
2577 mod->sml_next = NULL;
2578 slap_mods_free( mod, 1 );
2581 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2582 if ( m2->sml_desc == dni.oldDesc &&
2583 m2->sml_op == SLAP_MOD_SOFTADD ) break;
2585 for ( i=0; i<mod->sml_numvals; i++ ) {
2586 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2588 ch_free( mod->sml_values[i].bv_val );
2589 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2590 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2591 if ( mod->sml_nvalues ) {
2592 ch_free( mod->sml_nvalues[i].bv_val );
2593 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2594 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2604 /* RDNs must be NUL-terminated for back-ldap */
2605 noldp = op->orr_newrdn;
2606 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
2607 noldp = op->orr_nnewrdn;
2608 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
2610 /* Setup opattrs too */
2612 static AttributeDescription *nullattr = NULL;
2613 static AttributeDescription **const opattrs[] = {
2614 &slap_schema.si_ad_entryCSN,
2615 &slap_schema.si_ad_modifiersName,
2616 &slap_schema.si_ad_modifyTimestamp,
2619 AttributeDescription *opattr;
2623 /* pull mod off incoming modlist */
2624 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
2625 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
2627 if ( (*ml)->sml_desc == opattr ) {
2629 *ml = mod->sml_next;
2630 mod->sml_next = NULL;
2632 modtail = &mod->sml_next;
2637 /* If there are still Modifications left, put the opattrs
2638 * back, and let be_modify run. Otherwise, append the opattrs
2639 * to the orr_modlist.
2643 /* don't set a CSN for the rename op */
2645 slap_graduate_commit_csn( op );
2647 mod = op->orr_modlist;
2650 for ( ; mod->sml_next; mod=mod->sml_next );
2653 op->o_bd = si->si_wbe;
2655 rc = op->o_bd->be_modrdn( op, &rs_modify );
2657 /* NOTE: noSuchObject should result because the new superior
2658 * has not been added yet (ITS#6472) */
2659 if ( rc == LDAP_NO_SUCH_OBJECT && !BER_BVISNULL( op->orr_nnewSup )) {
2660 Operation op2 = *op;
2661 rc = syncrepl_add_glue_ancestors( &op2, entry );
2662 if ( rc == LDAP_SUCCESS ) {
2667 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
2668 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
2670 slap_mods_free( op->orr_modlist, 1 );
2671 Debug( LDAP_DEBUG_SYNC,
2672 "syncrepl_entry: %s be_modrdn %s (%d)\n",
2673 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2675 /* Renamed entries may still have other mods so just fallthru */
2676 op->o_req_dn = entry->e_name;
2677 op->o_req_ndn = entry->e_nname;
2678 /* Use CSN on the modify */
2682 slap_queue_csn( op, syncCSN );
2685 op->o_tag = LDAP_REQ_MODIFY;
2686 op->orm_modlist = dni.mods;
2687 op->orm_no_opattrs = 1;
2688 op->o_bd = si->si_wbe;
2690 rc = op->o_bd->be_modify( op, &rs_modify );
2691 slap_mods_free( op->orm_modlist, 1 );
2692 op->orm_no_opattrs = 0;
2693 Debug( LDAP_DEBUG_SYNC,
2694 "syncrepl_entry: %s be_modify %s (%d)\n",
2695 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2696 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2697 Debug( LDAP_DEBUG_ANY,
2698 "syncrepl_entry: %s be_modify failed (%d)\n",
2699 si->si_ridtxt, rs_modify.sr_err, 0 );
2703 } else if ( !dni.renamed ) {
2704 Debug( LDAP_DEBUG_SYNC,
2705 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
2706 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
2708 slap_graduate_commit_csn( op );
2713 case LDAP_SYNC_DELETE :
2714 if ( !BER_BVISNULL( &dni.dn ) ) {
2715 op->o_req_dn = dni.dn;
2716 op->o_req_ndn = dni.ndn;
2717 op->o_tag = LDAP_REQ_DELETE;
2718 op->o_bd = si->si_wbe;
2719 rc = op->o_bd->be_delete( op, &rs_delete );
2720 Debug( LDAP_DEBUG_SYNC,
2721 "syncrepl_entry: %s be_delete %s (%d)\n",
2722 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2724 while ( rs_delete.sr_err == LDAP_SUCCESS
2725 && op->o_delete_glue_parent ) {
2726 op->o_delete_glue_parent = 0;
2727 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2728 slap_callback cb = { NULL };
2729 cb.sc_response = slap_null_cb;
2730 dnParent( &op->o_req_ndn, &pdn );
2732 op->o_req_ndn = pdn;
2733 op->o_callback = &cb;
2734 op->o_bd->be_delete( op, &rs_delete );
2745 Debug( LDAP_DEBUG_ANY,
2746 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
2751 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2752 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2753 BER_BVZERO( &syncUUID_strrep );
2755 if ( !BER_BVISNULL( &dni.ndn ) ) {
2756 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2758 if ( !BER_BVISNULL( &dni.dn ) ) {
2759 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2762 entry_free( entry );
2765 slap_graduate_commit_csn( op );
2767 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
2768 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2770 BER_BVZERO( &op->o_csn );
2774 static struct berval gcbva[] = {
2780 #define NP_DELETE_ONE 2
2783 syncrepl_del_nonpresent(
2787 struct sync_cookie *sc,
2790 Backend* be = op->o_bd;
2791 slap_callback cb = { NULL };
2792 SlapReply rs_search = {REP_RESULT};
2793 SlapReply rs_delete = {REP_RESULT};
2794 SlapReply rs_modify = {REP_RESULT};
2795 struct nonpresent_entry *np_list, *np_prev;
2797 AttributeName an[2];
2799 struct berval pdn = BER_BVNULL;
2802 #ifdef ENABLE_REWRITE
2803 if ( si->si_rewrite ) {
2804 op->o_req_dn = si->si_suffixm;
2805 op->o_req_ndn = si->si_suffixm;
2809 op->o_req_dn = si->si_base;
2810 op->o_req_ndn = si->si_base;
2813 cb.sc_response = nonpresent_callback;
2816 op->o_callback = &cb;
2817 op->o_tag = LDAP_REQ_SEARCH;
2818 op->ors_scope = si->si_scope;
2819 op->ors_deref = LDAP_DEREF_NEVER;
2820 op->o_time = slap_get_time();
2821 op->ors_tlimit = SLAP_NO_LIMIT;
2826 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
2829 op->ors_attrsonly = 1;
2830 op->ors_attrs = slap_anlist_no_attrs;
2831 op->ors_limit = NULL;
2832 op->ors_filter = &uf;
2835 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2837 uf.f_choice = LDAP_FILTER_EQUALITY;
2838 si->si_refreshDelete |= NP_DELETE_ONE;
2840 for (i=0; uuids[i].bv_val; i++) {
2842 uf.f_av_value = uuids[i];
2843 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2844 rc = be->be_search( op, &rs_search );
2845 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2847 si->si_refreshDelete ^= NP_DELETE_ONE;
2851 AttributeAssertion mmaa;
2853 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2854 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2855 an[0].an_desc = slap_schema.si_ad_entryUUID;
2857 op->ors_slimit = SLAP_NO_LIMIT;
2858 op->ors_tlimit = SLAP_NO_LIMIT;
2859 op->ors_limit = NULL;
2860 op->ors_attrsonly = 0;
2861 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
2862 /* In multimaster, updates can continue to arrive while
2863 * we're searching. Limit the search result to entries
2864 * older than our newest cookie CSN.
2866 if ( SLAP_MULTIMASTER( op->o_bd )) {
2871 f->f_choice = LDAP_FILTER_AND;
2872 f->f_next = op->ors_filter;
2876 f->f_choice = LDAP_FILTER_LE;
2878 f->f_av_desc = slap_schema.si_ad_entryCSN;
2880 BER_BVZERO( &f->f_av_value );
2881 for ( i=0; i<sc->numcsns; i++ ) {
2882 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
2883 f->f_av_value = sc->ctxcsn[i];
2885 of = op->ors_filter;
2886 op->ors_filter = mmf;
2887 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2890 op->ors_filterstr = si->si_filterstr;
2892 op->o_nocaching = 1;
2895 rc = be->be_search( op, &rs_search );
2896 if ( SLAP_MULTIMASTER( op->o_bd )) {
2897 op->ors_filter = of;
2899 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
2900 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
2901 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2906 op->o_nocaching = 0;
2908 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2910 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
2911 csn = sc->ctxcsn[m];
2913 csn = si->si_syncCookie.ctxcsn[0];
2916 op->o_bd = si->si_wbe;
2917 slap_queue_csn( op, &csn );
2919 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2920 while ( np_list != NULL ) {
2921 LDAP_LIST_REMOVE( np_list, npe_link );
2923 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2924 op->o_tag = LDAP_REQ_DELETE;
2925 op->o_callback = &cb;
2926 cb.sc_response = null_callback;
2928 op->o_req_dn = *np_prev->npe_name;
2929 op->o_req_ndn = *np_prev->npe_nname;
2930 rc = op->o_bd->be_delete( op, &rs_delete );
2931 Debug( LDAP_DEBUG_SYNC,
2932 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
2933 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2935 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2936 Modifications mod1, mod2;
2937 mod1.sml_op = LDAP_MOD_REPLACE;
2939 mod1.sml_desc = slap_schema.si_ad_objectClass;
2940 mod1.sml_type = mod1.sml_desc->ad_cname;
2941 mod1.sml_numvals = 2;
2942 mod1.sml_values = &gcbva[0];
2943 mod1.sml_nvalues = NULL;
2944 mod1.sml_next = &mod2;
2946 mod2.sml_op = LDAP_MOD_REPLACE;
2948 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2949 mod2.sml_type = mod2.sml_desc->ad_cname;
2950 mod2.sml_numvals = 1;
2951 mod2.sml_values = &gcbva[1];
2952 mod2.sml_nvalues = NULL;
2953 mod2.sml_next = NULL;
2955 op->o_tag = LDAP_REQ_MODIFY;
2956 op->orm_modlist = &mod1;
2958 rc = op->o_bd->be_modify( op, &rs_modify );
2959 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2962 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2963 op->o_delete_glue_parent ) {
2964 op->o_delete_glue_parent = 0;
2965 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2966 slap_callback cb = { NULL };
2967 cb.sc_response = slap_null_cb;
2968 dnParent( &op->o_req_ndn, &pdn );
2970 op->o_req_ndn = pdn;
2971 op->o_callback = &cb;
2972 /* give it a root privil ? */
2973 op->o_bd->be_delete( op, &rs_delete );
2979 op->o_delete_glue_parent = 0;
2981 ber_bvfree( np_prev->npe_name );
2982 ber_bvfree( np_prev->npe_nname );
2985 if ( slapd_shutdown ) {
2990 slap_graduate_commit_csn( op );
2993 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2994 BER_BVZERO( &op->o_csn );
3001 syncrepl_add_glue_ancestors(
3005 Backend *be = op->o_bd;
3006 slap_callback cb = { NULL };
3008 int rc = LDAP_SUCCESS;
3011 struct berval dn = BER_BVNULL;
3012 struct berval ndn = BER_BVNULL;
3014 SlapReply rs_add = {REP_RESULT};
3015 struct berval ptr, nptr;
3018 op->o_tag = LDAP_REQ_ADD;
3019 op->o_callback = &cb;
3020 cb.sc_response = null_callback;
3021 cb.sc_private = NULL;
3026 /* count RDNs in suffix */
3027 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3028 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3030 ptr.bv_len -= comma - ptr.bv_val;
3040 /* Start with BE suffix */
3042 for ( i = 0; i < suffrdns; i++ ) {
3043 comma = ber_bvrchr( &ptr, ',' );
3044 if ( comma != NULL ) {
3045 ptr.bv_len = comma - ptr.bv_val;
3052 if ( !BER_BVISEMPTY( &ptr ) ) {
3053 dn.bv_len -= ptr.bv_len + 1;
3054 dn.bv_val += ptr.bv_len + 1;
3057 /* the normalizedDNs are always the same length, no counting
3061 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3062 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3063 ndn.bv_len = be->be_nsuffix[0].bv_len;
3065 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3071 while ( ndn.bv_val > e->e_nname.bv_val ) {
3072 glue = entry_alloc();
3073 ber_dupbv( &glue->e_name, &dn );
3074 ber_dupbv( &glue->e_nname, &ndn );
3076 a = attr_alloc( slap_schema.si_ad_objectClass );
3079 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3080 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3081 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3082 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3084 a->a_nvals = a->a_vals;
3086 a->a_next = glue->e_attrs;
3089 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3092 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3093 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3094 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3096 a->a_nvals = a->a_vals;
3098 a->a_next = glue->e_attrs;
3101 op->o_req_dn = glue->e_name;
3102 op->o_req_ndn = glue->e_nname;
3104 rc = be->be_add ( op, &rs_add );
3105 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3106 if ( op->ora_e == glue )
3107 be_entry_release_w( op, glue );
3109 /* incl. ALREADY EXIST */
3111 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3117 /* Move to next child */
3118 comma = ber_bvrchr( &ptr, ',' );
3119 if ( comma == NULL ) {
3122 ptr.bv_len = comma - ptr.bv_val;
3124 dn.bv_val = ++comma;
3125 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3127 comma = ber_bvrchr( &nptr, ',' );
3128 assert( comma != NULL );
3129 nptr.bv_len = comma - nptr.bv_val;
3131 ndn.bv_val = ++comma;
3132 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3143 slap_callback cb = { NULL };
3145 Backend *be = op->o_bd;
3146 SlapReply rs_add = {REP_RESULT};
3148 rc = syncrepl_add_glue_ancestors( op, e );
3151 case LDAP_ALREADY_EXISTS:
3158 op->o_tag = LDAP_REQ_ADD;
3159 op->o_callback = &cb;
3160 cb.sc_response = null_callback;
3161 cb.sc_private = NULL;
3163 op->o_req_dn = e->e_name;
3164 op->o_req_ndn = e->e_nname;
3166 rc = be->be_add ( op, &rs_add );
3167 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3168 if ( op->ora_e == e )
3169 be_entry_release_w( op, e );
3178 syncrepl_updateCookie(
3181 struct sync_cookie *syncCookie )
3183 Backend *be = op->o_bd;
3185 struct berval first = BER_BVNULL;
3187 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3190 int rc, i, j, changed = 0;
3193 slap_callback cb = { NULL };
3194 SlapReply rs_modify = {REP_RESULT};
3196 mod.sml_op = LDAP_MOD_REPLACE;
3197 mod.sml_desc = slap_schema.si_ad_contextCSN;
3198 mod.sml_type = mod.sml_desc->ad_cname;
3199 mod.sml_flags = SLAP_MOD_INTERNAL;
3200 mod.sml_nvalues = NULL;
3201 mod.sml_next = NULL;
3203 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3206 for ( i=0; i<syncCookie->numcsns; i++ ) {
3207 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3209 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3210 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3214 /* clone the cookieState CSNs so we can Replace the whole thing */
3215 mod.sml_numvals = si->si_cookieState->cs_num;
3216 mod.sml_values = op->o_tmpalloc(( mod.sml_numvals+1 )*sizeof(struct berval), op->o_tmpmemctx );
3217 for ( i=0; i<mod.sml_numvals; i++ )
3218 mod.sml_values[i] = si->si_cookieState->cs_vals[i];
3219 BER_BVZERO( &mod.sml_values[i] );
3221 /* find any CSNs in the syncCookie that are newer than the cookieState */
3222 for ( i=0; i<syncCookie->numcsns; i++ ) {
3223 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
3224 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
3226 len = syncCookie->ctxcsn[i].bv_len;
3227 if ( len > si->si_cookieState->cs_vals[j].bv_len )
3228 len = si->si_cookieState->cs_vals[j].bv_len;
3229 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3230 si->si_cookieState->cs_vals[j].bv_val, len ) > 0 ) {
3231 mod.sml_values[j] = syncCookie->ctxcsn[i];
3233 if ( BER_BVISNULL( &first ) ) {
3234 first = syncCookie->ctxcsn[i];
3236 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3238 first = syncCookie->ctxcsn[i];
3243 /* there was no match for this SID, it's a new CSN */
3244 if ( j == si->si_cookieState->cs_num ) {
3245 mod.sml_values = op->o_tmprealloc( mod.sml_values,
3246 ( mod.sml_numvals+2 )*sizeof(struct berval), op->o_tmpmemctx );
3247 mod.sml_values[mod.sml_numvals++] = syncCookie->ctxcsn[i];
3248 BER_BVZERO( &mod.sml_values[mod.sml_numvals] );
3249 if ( BER_BVISNULL( &first ) ) {
3250 first = syncCookie->ctxcsn[i];
3251 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3253 first = syncCookie->ctxcsn[i];
3258 /* Should never happen, ITS#5065 */
3259 if ( BER_BVISNULL( &first ) || !changed ) {
3260 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3261 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3264 op->o_bd = si->si_wbe;
3265 slap_queue_csn( op, &first );
3267 op->o_tag = LDAP_REQ_MODIFY;
3269 cb.sc_response = null_callback;
3272 op->o_callback = &cb;
3273 op->o_req_dn = si->si_contextdn;
3274 op->o_req_ndn = si->si_contextdn;
3276 /* update contextCSN */
3277 op->o_dont_replicate = 1;
3279 op->orm_modlist = &mod;
3280 op->orm_no_opattrs = 1;
3281 rc = op->o_bd->be_modify( op, &rs_modify );
3283 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3284 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3286 char txtbuf[SLAP_TEXT_BUFLEN];
3287 size_t textlen = sizeof txtbuf;
3288 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3289 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3291 rc = op->o_bd->be_add( op, &rs_modify );
3292 if ( e == op->ora_e )
3293 be_entry_release_w( op, op->ora_e );
3296 op->orm_no_opattrs = 0;
3297 op->o_dont_replicate = 0;
3299 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3300 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3301 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
3302 /* If we replaced any old values */
3303 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3304 if ( mod.sml_values[i].bv_val != si->si_cookieState->cs_vals[i].bv_val )
3305 ber_bvreplace( &si->si_cookieState->cs_vals[i],
3306 &mod.sml_values[i] );
3308 /* Handle any added values */
3309 if ( i < mod.sml_numvals ) {
3310 si->si_cookieState->cs_num = mod.sml_numvals;
3311 value_add( &si->si_cookieState->cs_vals, &mod.sml_values[i] );
3312 free( si->si_cookieState->cs_sids );
3313 si->si_cookieState->cs_sids = slap_parse_csn_sids(
3314 si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
3317 si->si_cookieState->cs_age++;
3318 si->si_cookieAge = si->si_cookieState->cs_age;
3320 Debug( LDAP_DEBUG_ANY,
3321 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3322 si->si_ridtxt, rs_modify.sr_err, 0 );
3324 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3327 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3328 BER_BVZERO( &op->o_csn );
3329 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3330 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3333 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3334 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3341 /* Compare the attribute from the old entry to the one in the new
3342 * entry. The Modifications from the new entry will either be left
3343 * in place, or changed to an Add or Delete as needed.
3346 attr_cmp( Operation *op, Attribute *old, Attribute *new,
3347 Modifications ***mret, Modifications ***mcur )
3350 Modifications *mod, **modtail;
3356 struct berval **adds, **dels;
3357 /* count old and new */
3358 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
3359 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
3361 /* there MUST be both old and new values */
3366 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
3367 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
3369 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
3370 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
3374 for ( i=0; i<o; i++ ) {
3375 for ( j=0; j<n; j++ ) {
3378 if ( bvmatch( dels[i], adds[j] ) ) {
3388 /* Don't delete/add an objectClass, always use the replace op.
3389 * Modify would fail if provider has replaced entry with a new,
3390 * and the new explicitly includes a superior of a class that was
3391 * only included implicitly in the old entry. Ref ITS#5517.
3393 * Also use replace op if attr has no equality matching rule.
3396 if ( ( nn || ( no > 0 && no < o ) ) &&
3397 ( old->a_desc == slap_schema.si_ad_objectClass ||
3398 !old->a_desc->ad_type->sat_equality ) )
3404 /* all old values were deleted, just use the replace op */
3408 /* delete some values */
3409 mod = ch_malloc( sizeof( Modifications ) );
3410 mod->sml_op = LDAP_MOD_DELETE;
3412 mod->sml_desc = old->a_desc;
3413 mod->sml_type = mod->sml_desc->ad_cname;
3414 mod->sml_numvals = no;
3415 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3416 if ( old->a_vals != old->a_nvals ) {
3417 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3419 mod->sml_nvalues = NULL;
3422 for ( i = 0; i < o; i++ ) {
3423 if ( !dels[i] ) continue;
3424 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
3425 if ( mod->sml_nvalues ) {
3426 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
3430 BER_BVZERO( &mod->sml_values[j] );
3431 if ( mod->sml_nvalues ) {
3432 BER_BVZERO( &mod->sml_nvalues[j] );
3435 modtail = &mod->sml_next;
3438 op->o_tmpfree( dels, op->o_tmpmemctx );
3439 /* some values were added */
3440 if ( nn && no < o ) {
3441 mod = ch_malloc( sizeof( Modifications ) );
3442 mod->sml_op = LDAP_MOD_ADD;
3444 mod->sml_desc = old->a_desc;
3445 mod->sml_type = mod->sml_desc->ad_cname;
3446 mod->sml_numvals = nn;
3447 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3448 if ( old->a_vals != old->a_nvals ) {
3449 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3451 mod->sml_nvalues = NULL;
3454 for ( i = 0; i < n; i++ ) {
3455 if ( !adds[i] ) continue;
3456 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
3457 if ( mod->sml_nvalues ) {
3458 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
3462 BER_BVZERO( &mod->sml_values[j] );
3463 if ( mod->sml_nvalues ) {
3464 BER_BVZERO( &mod->sml_nvalues[j] );
3467 modtail = &mod->sml_next;
3470 op->o_tmpfree( adds, op->o_tmpmemctx );
3472 /* new attr, just use the new mod */
3476 /* advance to next element */
3480 **mcur = mod->sml_next;
3482 modtail = &mod->sml_next;
3484 *mcur = &mod->sml_next;
3490 /* Generate a set of modifications to change the old entry into the
3491 * new one. On input ml is a list of modifications equivalent to
3492 * the new entry. It will be massaged and the result will be stored
3495 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
3496 Modifications **mods, Modifications **ml, int is_ctx)
3498 Modifications **modtail = mods;
3500 /* We assume that attributes are saved in the same order
3501 * in the remote and local databases. So if we walk through
3502 * the attributeDescriptions one by one they should match in
3503 * lock step. If not, look for an add or delete.
3505 while ( old && new )
3507 /* If we've seen this before, use its mod now */
3508 if ( new->a_flags & SLAP_ATTR_IXADD ) {
3509 attr_cmp( op, NULL, new, &modtail, &ml );
3513 /* Skip contextCSN */
3514 if ( is_ctx && old->a_desc ==
3515 slap_schema.si_ad_contextCSN ) {
3520 if ( old->a_desc != new->a_desc ) {
3524 /* If it's just been re-added later,
3525 * remember that we've seen it.
3527 tmp = attr_find( new, old->a_desc );
3529 tmp->a_flags |= SLAP_ATTR_IXADD;
3531 /* If it's a new attribute, pull it in.
3533 tmp = attr_find( old, new->a_desc );
3535 attr_cmp( op, NULL, new, &modtail, &ml );
3539 /* Delete old attr */
3540 mod = ch_malloc( sizeof( Modifications ) );
3541 mod->sml_op = LDAP_MOD_DELETE;
3543 mod->sml_desc = old->a_desc;
3544 mod->sml_type = mod->sml_desc->ad_cname;
3545 mod->sml_numvals = 0;
3546 mod->sml_values = NULL;
3547 mod->sml_nvalues = NULL;
3549 modtail = &mod->sml_next;
3554 /* kludge - always update modifiersName so that it
3555 * stays co-located with the other mod opattrs. But only
3556 * if we know there are other valid mods.
3558 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
3559 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
3560 attr_cmp( op, NULL, new, &modtail, &ml );
3562 attr_cmp( op, old, new, &modtail, &ml );
3575 dninfo *dni = op->o_callback->sc_private;
3577 if ( rs->sr_type == REP_SEARCH ) {
3578 if ( !BER_BVISNULL( &dni->dn ) ) {
3579 Debug( LDAP_DEBUG_ANY,
3580 "dn_callback : consistency error - "
3581 "entryUUID is not unique\n", 0, 0, 0 );
3583 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
3584 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
3585 /* If there is a new entry, see if it differs from the old.
3586 * We compare the non-normalized values so that cosmetic changes
3587 * in the provider are always propagated.
3589 if ( dni->new_entry ) {
3590 Attribute *old, *new;
3591 struct berval old_rdn, new_rdn;
3592 struct berval old_p, new_p;
3593 int is_ctx, new_sup = 0;
3595 /* If old entry is not a glue entry, make sure new entry
3596 * is actually newer than old entry
3598 if ( !is_entry_glue( rs->sr_entry )) {
3599 old = attr_find( rs->sr_entry->e_attrs,
3600 slap_schema.si_ad_entryCSN );
3601 new = attr_find( dni->new_entry->e_attrs,
3602 slap_schema.si_ad_entryCSN );
3605 ber_len_t len = old->a_vals[0].bv_len;
3606 if ( len > new->a_vals[0].bv_len )
3607 len = new->a_vals[0].bv_len;
3608 rc = memcmp( old->a_vals[0].bv_val,
3609 new->a_vals[0].bv_val, len );
3611 Debug( LDAP_DEBUG_SYNC,
3612 "dn_callback : new entry is older than ours "
3613 "%s ours %s, new %s\n",
3614 rs->sr_entry->e_name.bv_val,
3615 old->a_vals[0].bv_val,
3616 new->a_vals[0].bv_val );
3617 return LDAP_SUCCESS;
3618 } else if ( rc == 0 ) {
3619 Debug( LDAP_DEBUG_SYNC,
3620 "dn_callback : entries have identical CSN "
3622 rs->sr_entry->e_name.bv_val,
3623 old->a_vals[0].bv_val, 0 );
3624 return LDAP_SUCCESS;
3629 is_ctx = dn_match( &rs->sr_entry->e_nname,
3630 &op->o_bd->be_nsuffix[0] );
3632 /* Did the DN change?
3633 * case changes in the parent are ignored,
3634 * we only want to know if the RDN was
3637 dnRdn( &rs->sr_entry->e_name, &old_rdn );
3638 dnRdn( &dni->new_entry->e_name, &new_rdn );
3639 dnParent( &rs->sr_entry->e_nname, &old_p );
3640 dnParent( &dni->new_entry->e_nname, &new_p );
3642 new_sup = !dn_match( &old_p, &new_p );
3643 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
3645 struct berval oldRDN, oldVal;
3646 AttributeDescription *ad = NULL;
3652 dni->nnewSup = new_p;
3654 /* See if the oldRDN was deleted */
3655 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
3656 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
3657 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
3659 oldRDN.bv_len -= oldVal.bv_len + 1;
3660 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3662 for ( oldpos=0, a=rs->sr_entry->e_attrs;
3663 a && a->a_desc != ad; oldpos++, a=a->a_next );
3665 for ( newpos=0, a=dni->new_entry->e_attrs;
3666 a && a->a_desc != ad; newpos++, a=a->a_next );
3667 if ( !a || oldpos != newpos || attr_valfind( a,
3668 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
3669 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3670 SLAP_MR_VALUE_OF_SYNTAX,
3671 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
3675 /* Get the newRDN's desc */
3676 dnRdn( &dni->new_entry->e_nname, &oldRDN );
3677 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
3678 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
3680 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3683 /* A ModDN has happened, but in Refresh mode other
3684 * changes may have occurred before we picked it up.
3685 * So fallthru to regular Modify processing.
3689 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
3690 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
3694 } else if ( rs->sr_type == REP_RESULT ) {
3695 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
3696 Debug( LDAP_DEBUG_ANY,
3697 "dn_callback : consistency error - "
3698 "entryUUID is not unique\n", 0, 0, 0 );
3702 return LDAP_SUCCESS;
3706 nonpresent_callback(
3710 syncinfo_t *si = op->o_callback->sc_private;
3713 struct berval* present_uuid = NULL;
3714 struct nonpresent_entry *np_entry;
3716 if ( rs->sr_type == REP_RESULT ) {
3717 count = avl_free( si->si_presentlist, ch_free );
3718 si->si_presentlist = NULL;
3720 } else if ( rs->sr_type == REP_SEARCH ) {
3721 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
3722 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
3725 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
3729 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
3730 char buf[sizeof("rid=999 non")];
3732 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
3733 present_uuid ? "" : "non" );
3735 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
3736 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
3739 if ( a == NULL ) return 0;
3742 if ( present_uuid == NULL ) {
3743 np_entry = (struct nonpresent_entry *)
3744 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
3745 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
3746 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
3747 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
3750 avl_delete( &si->si_presentlist,
3751 &a->a_nvals[0], syncuuid_cmp );
3752 ch_free( present_uuid );
3755 return LDAP_SUCCESS;
3763 if ( rs->sr_err != LDAP_SUCCESS &&
3764 rs->sr_err != LDAP_REFERRAL &&
3765 rs->sr_err != LDAP_ALREADY_EXISTS &&
3766 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
3767 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
3769 Debug( LDAP_DEBUG_ANY,
3770 "null_callback : error code 0x%x\n",
3773 return LDAP_SUCCESS;
3776 static struct berval *
3777 slap_uuidstr_from_normalized(
3778 struct berval* uuidstr,
3779 struct berval* normalized,
3784 unsigned char nibble;
3787 if ( normalized == NULL ) return NULL;
3788 if ( normalized->bv_len != 16 ) return NULL;
3793 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3794 if ( new == NULL ) {
3801 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3802 if ( new != uuidstr ) {
3803 slap_sl_free( new, ctx );
3808 for ( i = 0; i < 16; i++ ) {
3809 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
3810 new->bv_val[(i<<1)+d] = '-';
3814 nibble = (normalized->bv_val[i] >> 4) & 0xF;
3815 if ( nibble < 10 ) {
3816 new->bv_val[(i<<1)+d] = nibble + '0';
3818 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
3821 nibble = (normalized->bv_val[i]) & 0xF;
3822 if ( nibble < 10 ) {
3823 new->bv_val[(i<<1)+d+1] = nibble + '0';
3825 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
3829 new->bv_val[new->bv_len] = '\0';
3836 if ( normalized == NULL ) return NULL;
3837 if ( normalized->bv_len != 16 ) return NULL;
3843 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3844 if ( new == NULL ) {
3851 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3856 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
3857 normalized->bv_len, new->bv_val, new->bv_len + 1 );
3861 if ( new != NULL ) {
3862 if ( new->bv_val != NULL ) {
3863 slap_sl_free( new->bv_val, ctx );
3866 if ( new != uuidstr ) {
3867 slap_sl_free( new, ctx );
3880 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
3882 const struct berval *uuid1 = v_uuid1;
3883 const struct berval *uuid2 = v_uuid2;
3884 int rc = uuid1->bv_len - uuid2->bv_len;
3885 if ( rc ) return rc;
3886 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
3890 syncinfo_free( syncinfo_t *sie, int free_all )
3892 syncinfo_t *si_next;
3894 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
3895 sie->si_ridtxt, 0, 0 );
3898 si_next = sie->si_next;
3901 if ( sie->si_conn ) {
3902 connection_client_stop( sie->si_conn );
3903 sie->si_conn = NULL;
3905 ldap_unbind_ext( sie->si_ld, NULL, NULL );
3909 struct re_s *re = sie->si_re;
3912 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3913 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
3914 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3915 ldap_pvt_runqueue_remove( &slapd_rq, re );
3916 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3919 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
3921 bindconf_free( &sie->si_bindconf );
3923 if ( sie->si_filterstr.bv_val ) {
3924 ch_free( sie->si_filterstr.bv_val );
3926 if ( sie->si_filter ) {
3927 filter_free( sie->si_filter );
3929 if ( sie->si_logfilterstr.bv_val ) {
3930 ch_free( sie->si_logfilterstr.bv_val );
3932 if ( sie->si_base.bv_val ) {
3933 ch_free( sie->si_base.bv_val );
3935 if ( sie->si_logbase.bv_val ) {
3936 ch_free( sie->si_logbase.bv_val );
3938 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
3939 ch_free( sie->si_contextdn.bv_val );
3941 if ( sie->si_attrs ) {
3943 while ( sie->si_attrs[i] != NULL ) {
3944 ch_free( sie->si_attrs[i] );
3947 ch_free( sie->si_attrs );
3949 if ( sie->si_exattrs ) {
3951 while ( sie->si_exattrs[i] != NULL ) {
3952 ch_free( sie->si_exattrs[i] );
3955 ch_free( sie->si_exattrs );
3957 if ( sie->si_anlist ) {
3959 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
3960 ch_free( sie->si_anlist[i].an_name.bv_val );
3963 ch_free( sie->si_anlist );
3965 if ( sie->si_exanlist ) {
3967 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
3968 ch_free( sie->si_exanlist[i].an_name.bv_val );
3971 ch_free( sie->si_exanlist );
3973 if ( sie->si_retryinterval ) {
3974 ch_free( sie->si_retryinterval );
3976 if ( sie->si_retrynum ) {
3977 ch_free( sie->si_retrynum );
3979 if ( sie->si_retrynum_init ) {
3980 ch_free( sie->si_retrynum_init );
3982 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
3983 if ( sie->si_presentlist ) {
3984 avl_free( sie->si_presentlist, ch_free );
3986 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
3987 struct nonpresent_entry* npe;
3988 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
3989 LDAP_LIST_REMOVE( npe, npe_link );
3990 if ( npe->npe_name ) {
3991 if ( npe->npe_name->bv_val ) {
3992 ch_free( npe->npe_name->bv_val );
3994 ch_free( npe->npe_name );
3996 if ( npe->npe_nname ) {
3997 if ( npe->npe_nname->bv_val ) {
3998 ch_free( npe->npe_nname->bv_val );
4000 ch_free( npe->npe_nname );
4004 if ( sie->si_cookieState ) {
4005 sie->si_cookieState->cs_ref--;
4006 if ( !sie->si_cookieState->cs_ref ) {
4007 ch_free( sie->si_cookieState->cs_sids );
4008 ber_bvarray_free( sie->si_cookieState->cs_vals );
4009 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
4010 ch_free( sie->si_cookieState->cs_psids );
4011 ber_bvarray_free( sie->si_cookieState->cs_pvals );
4012 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
4013 ch_free( sie->si_cookieState );
4016 #ifdef ENABLE_REWRITE
4017 if ( sie->si_rewrite )
4018 rewrite_info_delete( &sie->si_rewrite );
4019 if ( sie->si_suffixm.bv_val )
4020 ch_free( sie->si_suffixm.bv_val );
4024 } while ( free_all && si_next );
4027 #ifdef ENABLE_REWRITE
4029 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4031 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4032 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4033 char *argvRule[] = { "rewriteRule", NULL, NULL, ":", NULL };
4037 if ( si->si_rewrite )
4038 rewrite_info_delete( &si->si_rewrite );
4039 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4041 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4042 if ( rc != LDAP_SUCCESS )
4045 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4046 if ( rc != LDAP_SUCCESS )
4049 vnc = ch_malloc( si->si_base.bv_len + 6 );
4050 strcpy( vnc, "(.*)" );
4051 lutil_strcopy( lutil_strcopy( vnc+4, si->si_base.bv_val ), "$" );
4054 rnc = ch_malloc( si->si_suffixm.bv_len + 3 );
4055 strcpy( rnc, "%1" );
4056 strcpy( rnc+2, si->si_suffixm.bv_val );
4059 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4066 /* NOTE: used & documented in slapd.conf(5) */
4068 #define PROVIDERSTR "provider"
4069 #define SCHEMASTR "schemachecking"
4070 #define FILTERSTR "filter"
4071 #define SEARCHBASESTR "searchbase"
4072 #define SCOPESTR "scope"
4073 #define ATTRSONLYSTR "attrsonly"
4074 #define ATTRSSTR "attrs"
4075 #define TYPESTR "type"
4076 #define INTERVALSTR "interval"
4077 #define RETRYSTR "retry"
4078 #define SLIMITSTR "sizelimit"
4079 #define TLIMITSTR "timelimit"
4080 #define SYNCDATASTR "syncdata"
4081 #define LOGBASESTR "logbase"
4082 #define LOGFILTERSTR "logfilter"
4083 #define SUFFIXMSTR "suffixmassage"
4085 /* FIXME: undocumented */
4086 #define EXATTRSSTR "exattrs"
4087 #define MANAGEDSAITSTR "manageDSAit"
4091 GOT_RID = 0x00000001U,
4092 GOT_PROVIDER = 0x00000002U,
4093 GOT_SCHEMACHECKING = 0x00000004U,
4094 GOT_FILTER = 0x00000008U,
4095 GOT_SEARCHBASE = 0x00000010U,
4096 GOT_SCOPE = 0x00000020U,
4097 GOT_ATTRSONLY = 0x00000040U,
4098 GOT_ATTRS = 0x00000080U,
4099 GOT_TYPE = 0x00000100U,
4100 GOT_INTERVAL = 0x00000200U,
4101 GOT_RETRY = 0x00000400U,
4102 GOT_SLIMIT = 0x00000800U,
4103 GOT_TLIMIT = 0x00001000U,
4104 GOT_SYNCDATA = 0x00002000U,
4105 GOT_LOGBASE = 0x00004000U,
4106 GOT_LOGFILTER = 0x00008000U,
4107 GOT_EXATTRS = 0x00010000U,
4108 GOT_MANAGEDSAIT = 0x00020000U,
4109 GOT_BINDCONF = 0x00040000U,
4110 GOT_SUFFIXM = 0x00080000U,
4113 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4116 static slap_verbmasks datamodes[] = {
4117 { BER_BVC("default"), SYNCDATA_DEFAULT },
4118 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4119 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4124 parse_syncrepl_retry(
4131 int use_default = 0;
4133 char *val = arg + STRLENOF( RETRYSTR "=" );
4134 if ( strcasecmp( val, "undefined" ) == 0 ) {
4139 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4140 retry_list[0] = NULL;
4142 slap_str2clist( &retry_list, val, " ,\t" );
4144 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4147 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4148 "Error: incomplete syncrepl retry list" );
4149 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4150 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4151 ch_free( retry_list[k] );
4153 ch_free( retry_list );
4156 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4157 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4158 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4159 for ( j = 0; j < n; j++ ) {
4161 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4162 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4163 "Error: invalid retry interval \"%s\" (#%d)",
4164 retry_list[j*2], j );
4165 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4166 /* do some cleanup */
4169 si->si_retryinterval[j] = (time_t)t;
4170 if ( *retry_list[j*2+1] == '+' ) {
4171 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4172 si->si_retrynum[j] = RETRYNUM_FOREVER;
4176 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4177 || si->si_retrynum_init[j] <= 0 )
4179 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4180 "Error: invalid initial retry number \"%s\" (#%d)",
4181 retry_list[j*2+1], j );
4182 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4183 /* do some cleanup */
4186 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4187 || si->si_retrynum[j] <= 0 )
4189 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4190 "Error: invalid retry number \"%s\" (#%d)",
4191 retry_list[j*2+1], j );
4192 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4193 /* do some cleanup */
4198 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4199 Debug( LDAP_DEBUG_CONFIG,
4200 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4204 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4205 si->si_retrynum[j] = RETRYNUM_TAIL;
4206 si->si_retryinterval[j] = 0;
4208 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4209 ch_free( retry_list[k] );
4211 ch_free( retry_list );
4212 if ( !use_default ) {
4213 si->si_got |= GOT_RETRY;
4220 parse_syncrepl_line(
4227 for ( i = 1; i < c->argc; i++ ) {
4228 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4229 STRLENOF( IDSTR "=" ) ) )
4232 /* '\0' string terminator accounts for '=' */
4233 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4234 if ( lutil_atoi( &tmp, val ) != 0 ) {
4235 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4236 "Error: parse_syncrepl_line: "
4237 "unable to parse syncrepl id \"%s\"", val );
4238 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4241 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4242 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4243 "Error: parse_syncrepl_line: "
4244 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4245 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4249 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4250 si->si_got |= GOT_RID;
4251 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4252 STRLENOF( PROVIDERSTR "=" ) ) )
4254 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4255 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4257 if ( ldap_is_ldaps_url( val ))
4258 si->si_bindconf.sb_tls_do_init = 1;
4260 si->si_got |= GOT_PROVIDER;
4261 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4262 STRLENOF( SCHEMASTR "=" ) ) )
4264 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4265 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4266 si->si_schemachecking = 1;
4267 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4268 si->si_schemachecking = 0;
4270 si->si_schemachecking = 1;
4272 si->si_got |= GOT_SCHEMACHECKING;
4273 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4274 STRLENOF( FILTERSTR "=" ) ) )
4276 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4277 if ( si->si_filterstr.bv_val )
4278 ch_free( si->si_filterstr.bv_val );
4279 ber_str2bv( val, 0, 1, &si->si_filterstr );
4280 si->si_got |= GOT_FILTER;
4281 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4282 STRLENOF( LOGFILTERSTR "=" ) ) )
4284 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4285 if ( si->si_logfilterstr.bv_val )
4286 ch_free( si->si_logfilterstr.bv_val );
4287 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4288 si->si_got |= GOT_LOGFILTER;
4289 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4290 STRLENOF( SEARCHBASESTR "=" ) ) )
4295 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4296 if ( si->si_base.bv_val ) {
4297 ch_free( si->si_base.bv_val );
4299 ber_str2bv( val, 0, 0, &bv );
4300 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4301 if ( rc != LDAP_SUCCESS ) {
4302 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4303 "Invalid base DN \"%s\": %d (%s)",
4304 val, rc, ldap_err2string( rc ) );
4305 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4308 si->si_got |= GOT_SEARCHBASE;
4309 #ifdef ENABLE_REWRITE
4310 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
4311 STRLENOF( SUFFIXMSTR "=" ) ) )
4316 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
4317 if ( si->si_suffixm.bv_val ) {
4318 ch_free( si->si_suffixm.bv_val );
4320 ber_str2bv( val, 0, 0, &bv );
4321 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
4322 if ( rc != LDAP_SUCCESS ) {
4323 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4324 "Invalid massage DN \"%s\": %d (%s)",
4325 val, rc, ldap_err2string( rc ) );
4326 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4329 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
4330 ch_free( si->si_suffixm.bv_val );
4331 BER_BVZERO( &si->si_suffixm );
4332 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4333 "Massage DN \"%s\" is not within the database naming context",
4335 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4338 si->si_got |= GOT_SUFFIXM;
4340 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4341 STRLENOF( LOGBASESTR "=" ) ) )
4346 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
4347 if ( si->si_logbase.bv_val ) {
4348 ch_free( si->si_logbase.bv_val );
4350 ber_str2bv( val, 0, 0, &bv );
4351 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
4352 if ( rc != LDAP_SUCCESS ) {
4353 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4354 "Invalid logbase DN \"%s\": %d (%s)",
4355 val, rc, ldap_err2string( rc ) );
4356 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4359 si->si_got |= GOT_LOGBASE;
4360 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
4361 STRLENOF( SCOPESTR "=" ) ) )
4364 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
4365 j = ldap_pvt_str2scope( val );
4367 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4368 "Error: parse_syncrepl_line: "
4369 "unknown scope \"%s\"", val);
4370 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4374 si->si_got |= GOT_SCOPE;
4375 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
4376 STRLENOF( ATTRSONLYSTR ) ) )
4378 si->si_attrsonly = 1;
4379 si->si_got |= GOT_ATTRSONLY;
4380 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
4381 STRLENOF( ATTRSSTR "=" ) ) )
4383 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
4384 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4386 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4387 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
4388 if ( si->si_anlist == NULL ) {
4389 ch_free( attr_fname );
4392 si->si_anfile = attr_fname;
4394 char *str, *s, *next;
4395 const char *delimstr = " ,\t";
4396 str = ch_strdup( val );
4397 for ( s = ldap_pvt_strtok( str, delimstr, &next );
4399 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
4401 if ( strlen(s) == 1 && *s == '*' ) {
4402 si->si_allattrs = 1;
4403 val[ s - str ] = delimstr[0];
4405 if ( strlen(s) == 1 && *s == '+' ) {
4406 si->si_allopattrs = 1;
4407 val [ s - str ] = delimstr[0];
4411 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
4412 if ( si->si_anlist == NULL ) {
4416 si->si_got |= GOT_ATTRS;
4417 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
4418 STRLENOF( EXATTRSSTR "=" ) ) )
4420 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
4421 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4423 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4424 si->si_exanlist = file2anlist(
4425 si->si_exanlist, attr_fname, " ,\t" );
4426 if ( si->si_exanlist == NULL ) {
4427 ch_free( attr_fname );
4430 ch_free( attr_fname );
4432 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
4433 if ( si->si_exanlist == NULL ) {
4437 si->si_got |= GOT_EXATTRS;
4438 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
4439 STRLENOF( TYPESTR "=" ) ) )
4441 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
4442 if ( !strncasecmp( val, "refreshOnly",
4443 STRLENOF("refreshOnly") ) )
4445 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4446 } else if ( !strncasecmp( val, "refreshAndPersist",
4447 STRLENOF("refreshAndPersist") ) )
4449 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
4450 si->si_interval = 60;
4452 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4453 "Error: parse_syncrepl_line: "
4454 "unknown sync type \"%s\"", val);
4455 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4458 si->si_got |= GOT_TYPE;
4459 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
4460 STRLENOF( INTERVALSTR "=" ) ) )
4462 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
4463 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
4464 si->si_interval = 0;
4465 } else if ( strchr( val, ':' ) != NULL ) {
4466 char *next, *ptr = val;
4469 dd = strtol( ptr, &next, 10 );
4470 if ( next == ptr || next[0] != ':' || dd < 0 ) {
4471 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4472 "Error: parse_syncrepl_line: "
4473 "invalid interval \"%s\", unable to parse days", val );
4474 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4478 hh = strtol( ptr, &next, 10 );
4479 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
4480 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4481 "Error: parse_syncrepl_line: "
4482 "invalid interval \"%s\", unable to parse hours", val );
4483 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4487 mm = strtol( ptr, &next, 10 );
4488 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
4489 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4490 "Error: parse_syncrepl_line: "
4491 "invalid interval \"%s\", unable to parse minutes", val );
4492 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4496 ss = strtol( ptr, &next, 10 );
4497 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
4498 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4499 "Error: parse_syncrepl_line: "
4500 "invalid interval \"%s\", unable to parse seconds", val );
4501 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4504 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
4508 if ( lutil_parse_time( val, &t ) != 0 ) {
4509 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4510 "Error: parse_syncrepl_line: "
4511 "invalid interval \"%s\"", val );
4512 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4515 si->si_interval = (time_t)t;
4517 if ( si->si_interval < 0 ) {
4518 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4519 "Error: parse_syncrepl_line: "
4520 "invalid interval \"%ld\"",
4521 (long) si->si_interval);
4522 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4525 si->si_got |= GOT_INTERVAL;
4526 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
4527 STRLENOF( RETRYSTR "=" ) ) )
4529 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
4532 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
4533 STRLENOF( MANAGEDSAITSTR "=" ) ) )
4535 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
4536 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
4537 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
4539 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4540 "invalid manageDSAit value \"%s\".\n",
4542 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4545 si->si_got |= GOT_MANAGEDSAIT;
4546 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
4547 STRLENOF( SLIMITSTR "=") ) )
4549 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
4550 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4553 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
4554 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4555 "invalid size limit value \"%s\".\n",
4557 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4560 si->si_got |= GOT_SLIMIT;
4561 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
4562 STRLENOF( TLIMITSTR "=" ) ) )
4564 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
4565 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4568 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
4569 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4570 "invalid time limit value \"%s\".\n",
4572 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4575 si->si_got |= GOT_TLIMIT;
4576 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
4577 STRLENOF( SYNCDATASTR "=" ) ) )
4579 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
4580 si->si_syncdata = verb_to_mask( val, datamodes );
4581 si->si_got |= GOT_SYNCDATA;
4582 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
4583 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4584 "Error: parse_syncrepl_line: "
4585 "unable to parse \"%s\"\n", c->argv[ i ] );
4586 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4589 si->si_got |= GOT_BINDCONF;
4592 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
4593 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4594 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
4595 si->si_got & GOT_RID ? "" : " "IDSTR,
4596 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
4597 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
4598 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4602 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
4603 ch_free( si->si_base.bv_val );
4604 BER_BVZERO( &si->si_base );
4605 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4606 "Base DN \"%s\" is not within the database naming context",
4608 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4612 #ifdef ENABLE_REWRITE
4613 if ( si->si_got & GOT_SUFFIXM ) {
4614 if (config_suffixm( c, si )) {
4615 ch_free( si->si_suffixm.bv_val );
4616 BER_BVZERO( &si->si_suffixm );
4617 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4618 "Error configuring rewrite engine" );
4619 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4625 if ( !( si->si_got & GOT_RETRY ) ) {
4626 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
4627 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
4628 if ( si->si_retryinterval == NULL ) {
4629 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
4635 si->si_filter = str2filter( si->si_filterstr.bv_val );
4636 if ( si->si_filter == NULL ) {
4637 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
4638 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
4652 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
4653 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
4654 "operations required for syncrepl", c->be->be_type );
4655 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4658 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
4659 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
4660 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4663 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
4666 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
4670 si->si_bindconf.sb_tls = SB_TLS_OFF;
4671 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
4672 si->si_schemachecking = 0;
4673 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
4674 &si->si_filterstr );
4675 si->si_base.bv_val = NULL;
4676 si->si_scope = LDAP_SCOPE_SUBTREE;
4677 si->si_attrsonly = 0;
4678 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4679 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4680 si->si_attrs = NULL;
4681 si->si_allattrs = 0;
4682 si->si_allopattrs = 0;
4683 si->si_exattrs = NULL;
4684 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4685 si->si_interval = 86400;
4686 si->si_retryinterval = NULL;
4687 si->si_retrynum_init = NULL;
4688 si->si_retrynum = NULL;
4689 si->si_manageDSAit = 0;
4693 si->si_presentlist = NULL;
4694 LDAP_LIST_INIT( &si->si_nonpresentlist );
4695 ldap_pvt_thread_mutex_init( &si->si_mutex );
4697 rc = parse_syncrepl_line( c, si );
4702 /* Must be LDAPv3 because we need controls */
4703 switch ( si->si_bindconf.sb_version ) {
4705 /* not explicitly set */
4706 si->si_bindconf.sb_version = LDAP_VERSION3;
4709 /* explicitly set */
4712 Debug( LDAP_DEBUG_ANY,
4713 "version %d incompatible with syncrepl\n",
4714 si->si_bindconf.sb_version, 0, 0 );
4715 syncinfo_free( si, 0 );
4719 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
4720 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4721 "<%s> invalid URL", c->argv[0] );
4722 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
4723 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
4728 if ( slapMode & SLAP_SERVER_MODE ) {
4730 /* check if consumer points to current server and database.
4731 * If so, ignore this configuration.
4733 if ( !SLAP_DBHIDDEN( c->be ) ) {
4735 /* if searchbase doesn't match current DB suffix,
4736 * assume it's different
4738 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
4739 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
4744 /* if searchbase matches, see if URLs match */
4745 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
4751 init_syncrepl( si );
4752 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4753 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
4754 si->si_interval, do_syncrepl, si, "do_syncrepl",
4756 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4758 rc = config_sync_shadow( c ) ? -1 : 0;
4763 /* mirrormode still needs to see this flag in tool mode */
4764 rc = config_sync_shadow( c ) ? -1 : 0;
4766 ldap_free_urldesc( lud );
4770 /* Use main slapd defaults */
4771 bindconf_tls_defaults( &si->si_bindconf );
4774 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
4775 syncinfo_free( si, 0 );
4778 Debug( LDAP_DEBUG_CONFIG,
4779 "Config: ** successfully added syncrepl %s \"%s\"\n",
4781 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
4782 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
4783 if ( c->be->be_syncinfo ) {
4786 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
4788 /* add new syncrepl to end of list (same order as when deleting) */
4789 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
4792 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
4793 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
4794 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
4796 c->be->be_syncinfo = si;
4798 si->si_cookieState->cs_ref++;
4807 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
4809 struct berval bc, uri, bs;
4810 char buf[BUFSIZ*2], *ptr;
4813 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
4817 /* temporarily inhibit bindconf from printing URI */
4818 uri = si->si_bindconf.sb_uri;
4819 BER_BVZERO( &si->si_bindconf.sb_uri );
4820 si->si_bindconf.sb_version = 0;
4821 bindconf_unparse( &si->si_bindconf, &bc );
4822 si->si_bindconf.sb_uri = uri;
4823 si->si_bindconf.sb_version = LDAP_VERSION3;
4826 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
4827 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
4828 si->si_rid, si->si_bindconf.sb_uri.bv_val );
4829 if ( len >= sizeof( buf ) ) return;
4831 if ( !BER_BVISNULL( &bc ) ) {
4832 if ( WHATSLEFT <= bc.bv_len ) {
4836 ptr = lutil_strcopy( ptr, bc.bv_val );
4839 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
4840 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
4841 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
4842 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
4845 if ( !BER_BVISNULL( &si->si_base ) ) {
4846 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
4847 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
4848 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
4851 #ifdef ENABLE_REWRITE
4852 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
4853 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
4854 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
4855 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
4859 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
4860 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
4861 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
4862 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
4865 if ( !BER_BVISNULL( &si->si_logbase ) ) {
4866 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
4867 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
4868 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
4871 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
4872 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
4873 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
4874 ptr = lutil_strcopy( ptr, bs.bv_val );
4876 if ( si->si_attrsonly ) {
4877 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4878 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
4880 if ( si->si_anfile ) {
4881 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
4882 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
4883 ptr = lutil_strcopy( ptr, si->si_anfile );
4885 } else if ( si->si_allattrs || si->si_allopattrs ||
4886 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
4890 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4891 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
4893 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
4894 if ( ptr == NULL ) return;
4895 if ( si->si_allattrs ) {
4896 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
4897 if ( old != ptr ) *ptr++ = ',';
4900 if ( si->si_allopattrs ) {
4901 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
4902 if ( old != ptr ) *ptr++ = ',';
4907 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
4908 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
4909 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
4910 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
4911 if ( ptr == NULL ) return;
4913 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
4914 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
4915 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
4917 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
4918 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
4919 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
4920 "refreshAndPersist" : "refreshOnly" );
4922 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
4925 dd = si->si_interval;
4932 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
4933 INTERVALSTR, dd, hh, mm, ss );
4934 if ( len >= WHATSLEFT ) return;
4938 if ( si->si_got & GOT_RETRY ) {
4939 const char *space = "";
4940 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
4941 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
4942 for (i=0; si->si_retryinterval[i]; i++) {
4943 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
4944 (long) si->si_retryinterval[i] );
4946 if ( WHATSLEFT - 1 <= len ) return;
4948 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
4951 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
4952 if ( WHATSLEFT <= len ) return;
4956 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
4959 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
4962 if ( si->si_slimit ) {
4963 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
4964 if ( WHATSLEFT <= len ) return;
4968 if ( si->si_tlimit ) {
4969 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
4970 if ( WHATSLEFT <= len ) return;
4974 if ( si->si_syncdata ) {
4975 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
4976 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
4977 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
4978 ptr = lutil_strcopy( ptr, bc.bv_val );
4981 bc.bv_len = ptr - buf;
4983 ber_dupbv( bv, &bc );
4987 syncrepl_config( ConfigArgs *c )
4989 if (c->op == SLAP_CONFIG_EMIT) {
4990 if ( c->be->be_syncinfo ) {
4994 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
4995 syncrepl_unparse( si, &bv );
4996 ber_bvarray_add( &c->rvalue_vals, &bv );
5001 } else if ( c->op == LDAP_MOD_DELETE ) {
5003 if ( c->be->be_syncinfo ) {
5004 syncinfo_t *si, **sip;
5007 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
5009 if ( c->valx == -1 || i == c->valx ) {
5013 /* If the task is currently active, we have to leave
5014 * it running. It will exit on its own. This will only
5015 * happen when running on the cn=config DB.
5018 if ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
5021 /* There is no active thread, but we must still
5022 * ensure that no thread is (or will be) queued
5023 * while we removes the task.
5025 struct re_s *re = si->si_re;
5028 if ( si->si_conn ) {
5029 connection_client_stop( si->si_conn );
5033 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5034 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
5035 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5038 ldap_pvt_runqueue_remove( &slapd_rq, re );
5039 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5041 if ( ldap_pvt_thread_pool_retract( &connection_pool,
5042 re->routine, re ) > 0 )
5045 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5049 syncinfo_free( si, 0 );
5058 if ( !c->be->be_syncinfo ) {
5059 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5063 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5064 Debug(LDAP_DEBUG_ANY, "%s: "
5065 "syncrepl: database already shadowed.\n",
5069 return add_syncrepl( c );