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 LDAPControl **rctrls = NULL;
761 BerElementBuffer berbuf;
762 BerElement *ber = (BerElement *)&berbuf;
764 LDAPMessage *msg = NULL;
767 struct berval *retdata = NULL;
772 struct berval syncUUID = BER_BVNULL;
773 struct sync_cookie syncCookie = { NULL };
774 struct sync_cookie syncCookie_req = { NULL };
775 struct berval cookie = BER_BVNULL;
781 Modifications *modlist = NULL;
783 int match, m, punlock = -1;
785 struct timeval *tout_p = NULL;
786 struct timeval tout = { 0, 0 };
788 int refreshDeletes = 0;
789 BerVarray syncUUIDs = NULL;
792 if ( slapd_shutdown ) {
797 ber_init2( ber, NULL, LBER_USE_DER );
798 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
800 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
802 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
804 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
810 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
811 tout_p, &msg ) ) > 0 )
813 LDAPControl *rctrlp = NULL;
815 if ( slapd_shutdown ) {
819 switch( ldap_msgtype( msg ) ) {
820 case LDAP_RES_SEARCH_ENTRY:
821 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
822 /* we can't work without the control */
824 LDAPControl **next = NULL;
825 /* NOTE: make sure we use the right one;
826 * a better approach would be to run thru
827 * the whole list and take care of all */
828 /* NOTE: since we issue the search request,
829 * we should know what controls to expect,
830 * and there should be none apart from the
831 * sync-related control */
832 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
833 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
835 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
836 "got search entry with multiple "
837 "Sync State control\n", si->si_ridtxt, 0, 0 );
838 ldap_controls_free( rctrls );
843 if ( rctrlp == NULL ) {
844 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
845 "got search entry without "
846 "Sync State control\n", si->si_ridtxt, 0, 0 );
850 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
851 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
852 /* FIXME: what if syncUUID is NULL or empty?
853 * (happens with back-sql...) */
854 if ( BER_BVISEMPTY( &syncUUID ) ) {
855 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
856 "got empty syncUUID with LDAP_SYNC_%s\n",
858 syncrepl_state2str( syncstate ), 0 );
859 ldap_controls_free( rctrls );
863 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
864 ber_scanf( ber, /*"{"*/ "m}", &cookie );
866 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
868 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
870 if ( !BER_BVISNULL( &cookie ) ) {
871 ch_free( syncCookie.octet_str.bv_val );
872 ber_dupbv( &syncCookie.octet_str, &cookie );
874 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
876 slap_parse_sync_cookie( &syncCookie, NULL );
877 if ( syncCookie.ctxcsn ) {
878 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
879 check_syncprov( op, si );
880 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
881 if ( si->si_cookieState->cs_sids[i] == sid ) {
882 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
883 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s\n",
884 si->si_ridtxt, syncCookie.ctxcsn->bv_val, 0 );
885 ldap_controls_free( rctrls );
892 /* check pending CSNs too */
893 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
894 if ( slapd_shutdown ) {
898 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
899 ldap_pvt_thread_yield();
901 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
902 if ( si->si_cookieState->cs_psids[i] == sid ) {
903 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
904 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s\n",
905 si->si_ridtxt, syncCookie.ctxcsn->bv_val, 0 );
906 ldap_controls_free( rctrls );
908 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
911 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
916 /* new SID, add it */
917 if ( i == si->si_cookieState->cs_pnum ) {
918 value_add( &si->si_cookieState->cs_pvals, syncCookie.ctxcsn );
919 si->si_cookieState->cs_pnum++;
920 si->si_cookieState->cs_psids = ch_realloc( si->si_cookieState->cs_psids, si->si_cookieState->cs_pnum * sizeof(int));
921 si->si_cookieState->cs_psids[i] = sid;
925 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
929 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
931 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
934 rc = syncrepl_updateCookie( si, op, &syncCookie );
935 } else switch ( rc ) {
936 case LDAP_ALREADY_EXISTS:
937 case LDAP_NO_SUCH_OBJECT:
938 case LDAP_NO_SUCH_ATTRIBUTE:
939 case LDAP_TYPE_OR_VALUE_EXISTS:
940 rc = LDAP_SYNC_REFRESH_REQUIRED;
941 si->si_logstate = SYNCLOG_FALLBACK;
942 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
947 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
948 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
950 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
951 syncstate, &syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
954 rc = syncrepl_updateCookie( si, op, &syncCookie );
957 if ( punlock >= 0 ) {
958 /* on failure, revert pending CSN */
959 if ( rc != LDAP_SUCCESS ) {
961 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
962 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
963 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
964 &si->si_cookieState->cs_vals[i] );
968 if ( i == si->si_cookieState->cs_num )
969 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
971 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
973 ldap_controls_free( rctrls );
975 slap_mods_free( modlist, 1 );
981 case LDAP_RES_SEARCH_REFERENCE:
982 Debug( LDAP_DEBUG_ANY,
983 "do_syncrep2: %s reference received error\n",
984 si->si_ridtxt, 0, 0 );
987 case LDAP_RES_SEARCH_RESULT:
988 Debug( LDAP_DEBUG_SYNC,
989 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
990 si->si_ridtxt, 0, 0 );
991 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
993 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
994 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
995 /* map old result code to registered code */
996 err = LDAP_SYNC_REFRESH_REQUIRED;
999 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
1000 if ( si->si_logstate == SYNCLOG_LOGGING ) {
1001 si->si_logstate = SYNCLOG_FALLBACK;
1007 Debug( LDAP_DEBUG_ANY,
1008 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1009 si->si_ridtxt, err, ldap_err2string( err ) );
1012 LDAPControl **next = NULL;
1013 /* NOTE: make sure we use the right one;
1014 * a better approach would be to run thru
1015 * the whole list and take care of all */
1016 /* NOTE: since we issue the search request,
1017 * we should know what controls to expect,
1018 * and there should be none apart from the
1019 * sync-related control */
1020 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1021 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1023 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1024 "got search result with multiple "
1025 "Sync State control\n", si->si_ridtxt, 0, 0 );
1026 ldap_controls_free( rctrls );
1032 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1034 ber_scanf( ber, "{" /*"}"*/);
1035 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1036 ber_scanf( ber, "m", &cookie );
1038 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1040 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1042 if ( !BER_BVISNULL( &cookie ) ) {
1043 ch_free( syncCookie.octet_str.bv_val );
1044 ber_dupbv( &syncCookie.octet_str, &cookie);
1046 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1048 slap_parse_sync_cookie( &syncCookie, NULL );
1049 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1052 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1054 ber_scanf( ber, "b", &refreshDeletes );
1056 ber_scanf( ber, /*"{"*/ "}" );
1058 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1059 slap_sync_cookie_free( &syncCookie_req, 0 );
1060 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1062 if ( !syncCookie.ctxcsn ) {
1064 } else if ( !syncCookie_req.ctxcsn ) {
1068 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1071 ldap_controls_free( rctrls );
1073 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1074 /* FIXME : different error behaviors according to
1075 * 1) err code : LDAP_BUSY ...
1076 * 2) on err policy : stop service, stop sync, retry
1078 if ( refreshDeletes == 0 && match < 0 &&
1079 err == LDAP_SUCCESS &&
1080 syncCookie_req.numcsns == syncCookie.numcsns )
1082 syncrepl_del_nonpresent( op, si, NULL,
1085 avl_free( si->si_presentlist, ch_free );
1086 si->si_presentlist = NULL;
1089 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1091 rc = syncrepl_updateCookie( si, op, &syncCookie );
1093 if ( err == LDAP_SUCCESS
1094 && si->si_logstate == SYNCLOG_FALLBACK ) {
1095 si->si_logstate = SYNCLOG_LOGGING;
1096 rc = LDAP_SYNC_REFRESH_REQUIRED;
1103 case LDAP_RES_INTERMEDIATE:
1104 rc = ldap_parse_intermediate( si->si_ld, msg,
1105 &retoid, &retdata, NULL, 0 );
1106 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1107 ber_init2( ber, retdata, LBER_USE_DER );
1109 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1111 case LDAP_TAG_SYNC_NEW_COOKIE:
1112 Debug( LDAP_DEBUG_SYNC,
1113 "do_syncrep2: %s %s - %s\n",
1115 "LDAP_RES_INTERMEDIATE",
1117 ber_scanf( ber, "tm", &tag, &cookie );
1118 Debug( LDAP_DEBUG_SYNC,
1119 "do_syncrep2: %s NEW_COOKIE: %s\n",
1122 if ( !BER_BVISNULL( &cookie ) ) {
1123 ch_free( syncCookie.octet_str.bv_val );
1124 ber_dupbv( &syncCookie.octet_str, &cookie );
1126 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1127 slap_parse_sync_cookie( &syncCookie, NULL );
1128 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1131 case LDAP_TAG_SYNC_REFRESH_DELETE:
1132 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1133 Debug( LDAP_DEBUG_SYNC,
1134 "do_syncrep2: %s %s - %s\n",
1136 "LDAP_RES_INTERMEDIATE",
1137 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1138 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1139 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1140 si->si_refreshDelete = 1;
1142 si->si_refreshPresent = 1;
1144 ber_scanf( ber, "t{" /*"}"*/, &tag );
1145 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1147 ber_scanf( ber, "m", &cookie );
1149 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1151 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1153 if ( !BER_BVISNULL( &cookie ) ) {
1154 ch_free( syncCookie.octet_str.bv_val );
1155 ber_dupbv( &syncCookie.octet_str, &cookie );
1157 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1159 slap_parse_sync_cookie( &syncCookie, NULL );
1160 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1163 /* Defaults to TRUE */
1164 if ( ber_peek_tag( ber, &len ) ==
1165 LDAP_TAG_REFRESHDONE )
1167 ber_scanf( ber, "b", &si->si_refreshDone );
1170 si->si_refreshDone = 1;
1172 ber_scanf( ber, /*"{"*/ "}" );
1174 case LDAP_TAG_SYNC_ID_SET:
1175 Debug( LDAP_DEBUG_SYNC,
1176 "do_syncrep2: %s %s - %s\n",
1178 "LDAP_RES_INTERMEDIATE",
1180 ber_scanf( ber, "t{" /*"}"*/, &tag );
1181 if ( ber_peek_tag( ber, &len ) ==
1182 LDAP_TAG_SYNC_COOKIE )
1184 ber_scanf( ber, "m", &cookie );
1186 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1188 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1190 if ( !BER_BVISNULL( &cookie ) ) {
1191 ch_free( syncCookie.octet_str.bv_val );
1192 ber_dupbv( &syncCookie.octet_str, &cookie );
1194 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1196 slap_parse_sync_cookie( &syncCookie, NULL );
1197 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1198 compare_csns( &syncCookie_req, &syncCookie, &m );
1201 if ( ber_peek_tag( ber, &len ) ==
1202 LDAP_TAG_REFRESHDELETES )
1204 ber_scanf( ber, "b", &refreshDeletes );
1206 ber_scanf( ber, "[W]", &syncUUIDs );
1207 ber_scanf( ber, /*"{"*/ "}" );
1208 if ( refreshDeletes ) {
1209 syncrepl_del_nonpresent( op, si, syncUUIDs,
1211 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1214 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1215 (void)avl_presentlist_insert( si, &syncUUIDs[i] );
1216 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1218 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1220 slap_sync_cookie_free( &syncCookie, 0 );
1223 Debug( LDAP_DEBUG_ANY,
1224 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1225 si->si_ridtxt, (long) si_tag, 0 );
1226 ldap_memfree( retoid );
1227 ber_bvfree( retdata );
1231 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1232 slap_sync_cookie_free( &syncCookie_req, 0 );
1233 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1235 if ( !syncCookie.ctxcsn ) {
1237 } else if ( !syncCookie_req.ctxcsn ) {
1241 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1245 if ( si->si_refreshPresent == 1 &&
1246 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1247 syncCookie_req.numcsns == syncCookie.numcsns ) {
1248 syncrepl_del_nonpresent( op, si, NULL,
1252 if ( syncCookie.ctxcsn )
1254 rc = syncrepl_updateCookie( si, op, &syncCookie);
1258 ldap_memfree( retoid );
1259 ber_bvfree( retdata );
1263 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1264 "unknown intermediate response (%d)\n",
1265 si->si_ridtxt, rc, 0 );
1266 ldap_memfree( retoid );
1267 ber_bvfree( retdata );
1273 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1274 "unknown message (0x%02lx)\n",
1276 (unsigned long)ldap_msgtype( msg ), 0 );
1280 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1281 slap_sync_cookie_free( &syncCookie_req, 0 );
1282 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1283 slap_sync_cookie_free( &syncCookie, 0 );
1285 ldap_msgfree( msg );
1287 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1288 slap_sync_cookie_free( &syncCookie, 0 );
1289 slap_sync_cookie_free( &syncCookie_req, 0 );
1295 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1300 if ( err != LDAP_SUCCESS ) {
1301 Debug( LDAP_DEBUG_ANY,
1302 "do_syncrep2: %s (%d) %s\n",
1303 si->si_ridtxt, err, ldap_err2string( err ) );
1306 slap_sync_cookie_free( &syncCookie, 0 );
1307 slap_sync_cookie_free( &syncCookie_req, 0 );
1309 if ( msg ) ldap_msgfree( msg );
1311 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1312 if ( si->si_conn ) {
1313 connection_client_stop( si->si_conn );
1316 ldap_unbind_ext( si->si_ld, NULL, NULL );
1328 struct re_s* rtask = arg;
1329 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1330 Connection conn = {0};
1331 OperationBuffer opbuf;
1333 int rc = LDAP_SUCCESS;
1336 int i, defer = 1, fail = 0, freeinfo = 0;
1341 if ( slapd_shutdown )
1344 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1346 /* Don't get stuck here while a pause is initiated */
1347 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1348 if ( slapd_shutdown )
1350 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1351 ldap_pvt_thread_yield();
1354 if ( si->si_ctype < 1 ) {
1358 switch( abs( si->si_type ) ) {
1359 case LDAP_SYNC_REFRESH_ONLY:
1360 case LDAP_SYNC_REFRESH_AND_PERSIST:
1363 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1367 if ( slapd_shutdown ) {
1369 if ( si->si_conn ) {
1370 connection_client_stop( si->si_conn );
1373 ldap_unbind_ext( si->si_ld, NULL, NULL );
1376 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1380 connection_fake_init( &conn, &opbuf, ctx );
1382 /* o_connids must be unique for slap_graduate_commit_csn */
1383 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1385 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1388 /* Coordinate contextCSN updates with any syncprov overlays
1389 * in use. This may be complicated by the use of the glue
1392 * Typically there is a single syncprov mastering the entire
1393 * glued tree. In that case, our contextCSN updates should
1394 * go to the master DB. But if there is no syncprov on the
1395 * master DB, then nothing special is needed here.
1397 * Alternatively, there may be individual syncprov overlays
1398 * on each glued branch. In that case, each syncprov only
1399 * knows about changes within its own branch. And so our
1400 * contextCSN updates should only go to the local DB.
1402 if ( !si->si_wbe ) {
1403 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1404 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1405 if ( overlay_is_inst( top_be, "syncprov" ))
1406 si->si_wbe = top_be;
1412 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1413 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1414 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1416 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1419 if ( !si->si_schemachecking )
1420 op->o_no_schema_check = 1;
1422 /* Establish session, do search */
1424 si->si_refreshDelete = 0;
1425 si->si_refreshPresent = 0;
1427 /* use main DB when retrieving contextCSN */
1428 op->o_bd = si->si_wbe;
1429 op->o_dn = op->o_bd->be_rootdn;
1430 op->o_ndn = op->o_bd->be_rootndn;
1431 rc = do_syncrep1( op, si );
1435 /* Process results */
1436 if ( rc == LDAP_SUCCESS ) {
1437 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1439 /* use current DB */
1441 op->o_dn = op->o_bd->be_rootdn;
1442 op->o_ndn = op->o_bd->be_rootndn;
1443 rc = do_syncrep2( op, si );
1444 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1445 rc = ldap_sync_search( si, op->o_tmpmemctx );
1450 /* We got deleted while running on cn=config */
1451 if ( si->si_ctype < 1 ) {
1452 if ( si->si_ctype == -1 ) {
1461 if ( rc != SYNC_PAUSED ) {
1462 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1463 /* If we succeeded, enable the connection for further listening.
1464 * If we failed, tear down the connection and reschedule.
1466 if ( rc == LDAP_SUCCESS ) {
1467 if ( si->si_conn ) {
1468 connection_client_enable( si->si_conn );
1470 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1472 } else if ( si->si_conn ) {
1476 if ( rc == -2 ) rc = 0;
1481 /* At this point, we have 5 cases:
1482 * 1) for any hard failure, give up and remove this task
1483 * 2) for ServerDown, reschedule this task to run later
1484 * 3) for threadpool pause, reschedule to run immediately
1485 * 4) for Refresh and Success, reschedule to run
1486 * 5) for Persist and Success, reschedule to defer
1488 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1490 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1491 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1495 connection_client_stop( si->si_conn );
1499 if ( rc == SYNC_PAUSED ) {
1500 rtask->interval.tv_sec = 0;
1501 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1502 rtask->interval.tv_sec = si->si_interval;
1504 } else if ( rc == LDAP_SUCCESS ) {
1505 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1508 rtask->interval.tv_sec = si->si_interval;
1509 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1510 if ( si->si_retrynum ) {
1511 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1512 si->si_retrynum[i] = si->si_retrynum_init[i];
1514 si->si_retrynum[i] = RETRYNUM_TAIL;
1517 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1518 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1522 if ( si->si_ctype < 1
1523 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1525 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1528 fail = RETRYNUM_TAIL;
1529 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1530 if ( si->si_retrynum[i] > 0 )
1531 si->si_retrynum[i]--;
1532 fail = si->si_retrynum[i];
1533 rtask->interval.tv_sec = si->si_retryinterval[i];
1534 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1535 slap_wake_listener();
1539 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1540 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1543 if ( fail == RETRYNUM_TAIL ) {
1544 Debug( LDAP_DEBUG_ANY,
1545 "do_syncrepl: %s rc %d quitting\n",
1546 si->si_ridtxt, rc, 0 );
1547 } else if ( fail > 0 ) {
1548 Debug( LDAP_DEBUG_ANY,
1549 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1550 si->si_ridtxt, rc, fail );
1552 Debug( LDAP_DEBUG_ANY,
1553 "do_syncrepl: %s rc %d retrying\n",
1554 si->si_ridtxt, rc, 0 );
1558 /* Do final delete cleanup */
1560 syncinfo_free( si, 0 );
1565 #ifdef ENABLE_REWRITE
1567 syncrepl_rewrite_dn(
1570 struct berval *sdn )
1575 nul = dn->bv_val[dn->bv_len];
1576 dn->bv_val[dn->bv_len] = 0;
1577 rc = rewrite( si->si_rewrite, SUFFIXM_CTX, dn->bv_val, &sdn->bv_val );
1578 dn->bv_val[dn->bv_len] = nul;
1580 if ( sdn->bv_val == dn->bv_val )
1582 else if ( rc == REWRITE_REGEXEC_OK && sdn->bv_val )
1583 sdn->bv_len = strlen( sdn->bv_val );
1586 #define REWRITE_VAL(si, ad, bv, bv2) \
1587 BER_BVZERO( &bv2 ); \
1588 if ( si->si_rewrite && ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName) \
1589 syncrepl_rewrite_dn( si, &bv, &bv2); \
1590 if ( BER_BVISNULL( &bv2 )) \
1591 ber_dupbv( &bv2, &bv )
1592 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1593 BER_BVZERO( &bv2 ); \
1594 if (si->si_rewrite) \
1595 syncrepl_rewrite_dn(si, &bv, &bv2); \
1596 rc = dnPrettyNormal( NULL, bv2.bv_val ? &bv2 : &bv, &dn, &ndn, op->o_tmpmemctx ); \
1599 #define REWRITE_VAL(si, ad, bv, bv2) ber_dupbv(&bv2, &bv)
1600 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1601 rc = dnPrettyNormal( NULL, &bv, &dn, &ndn, op->o_tmpmemctx )
1605 static slap_verbmasks modops[] = {
1606 { BER_BVC("add"), LDAP_REQ_ADD },
1607 { BER_BVC("delete"), LDAP_REQ_DELETE },
1608 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1609 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1614 syncrepl_accesslog_mods(
1616 struct berval *vals,
1617 struct Modifications **modres
1622 AttributeDescription *ad;
1623 struct berval bv, bv2;
1625 Modifications *mod = NULL, *modlist = NULL, **modtail;
1630 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1634 colon = ber_bvchr( &bv, ':' );
1640 bv.bv_len = colon - bv.bv_val;
1641 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1643 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1644 "Invalid attribute %s, %s\n",
1645 si->si_ridtxt, bv.bv_val, text );
1646 slap_mods_free( modlist, 1 );
1652 /* Ignore dynamically generated attrs */
1653 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1657 /* Ignore excluded attrs */
1658 if ( ldap_charray_inlist( si->si_exattrs,
1659 ad->ad_type->sat_cname.bv_val ) )
1665 case '+': op = LDAP_MOD_ADD; break;
1666 case '-': op = LDAP_MOD_DELETE; break;
1667 case '=': op = LDAP_MOD_REPLACE; break;
1668 case '#': op = LDAP_MOD_INCREMENT; break;
1672 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1673 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1676 mod->sml_next = NULL;
1678 mod->sml_type = ad->ad_cname;
1679 mod->sml_values = NULL;
1680 mod->sml_nvalues = NULL;
1681 mod->sml_numvals = 0;
1684 modtail = &mod->sml_next;
1686 if ( colon[2] == ' ' ) {
1687 bv.bv_val = colon + 3;
1688 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1689 REWRITE_VAL( si, ad, bv, bv2 );
1690 ber_bvarray_add( &mod->sml_values, &bv2 );
1699 syncrepl_changelog_mods(
1701 struct berval *vals,
1702 struct Modifications **modres
1705 return -1; /* FIXME */
1709 syncrepl_message_to_op(
1715 BerElement *ber = NULL;
1716 Modifications *modlist = NULL;
1718 SlapReply rs = { REP_RESULT };
1719 slap_callback cb = { NULL, null_callback, NULL, NULL };
1722 char txtbuf[SLAP_TEXT_BUFLEN];
1723 size_t textlen = sizeof txtbuf;
1725 struct berval bdn, dn = BER_BVNULL, ndn;
1726 struct berval bv, bv2, *bvals = NULL;
1727 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1728 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1729 psup = BER_BVNULL, nsup = BER_BVNULL;
1730 int rc, deleteOldRdn = 0, freeReqDn = 0;
1731 int do_graduate = 0;
1733 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1734 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1735 "Message type should be entry (%d)",
1736 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1740 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1745 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1747 if ( rc != LDAP_SUCCESS ) {
1748 Debug( LDAP_DEBUG_ANY,
1749 "syncrepl_message_to_op: %s dn get failed (%d)",
1750 si->si_ridtxt, rc, 0 );
1754 op->o_tag = LBER_DEFAULT;
1755 op->o_bd = si->si_wbe;
1757 if ( BER_BVISEMPTY( &bdn )) {
1758 Debug( LDAP_DEBUG_ANY,
1759 "syncrepl_message_to_op: %s got empty dn",
1760 si->si_ridtxt, 0, 0 );
1764 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
1766 if ( bv.bv_val == NULL )
1769 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
1771 REWRITE_DN( si, bdn, bv2, dn, ndn );
1772 if ( rc != LDAP_SUCCESS ) {
1773 Debug( LDAP_DEBUG_ANY,
1774 "syncrepl_message_to_op: %s "
1775 "dn \"%s\" normalization failed (%d)",
1776 si->si_ridtxt, bdn.bv_val, rc );
1781 ber_dupbv( &op->o_req_dn, &dn );
1782 ber_dupbv( &op->o_req_ndn, &ndn );
1783 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1784 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1786 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
1787 int i = verb_to_mask( bvals[0].bv_val, modops );
1789 Debug( LDAP_DEBUG_ANY,
1790 "syncrepl_message_to_op: %s unknown op %s",
1791 si->si_ridtxt, bvals[0].bv_val, 0 );
1796 op->o_tag = modops[i].mask;
1797 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
1798 /* Parse attribute into modlist */
1799 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
1800 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
1802 rc = syncrepl_changelog_mods( si, bvals, &modlist );
1804 if ( rc ) goto done;
1805 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
1807 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
1808 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
1811 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
1813 } else if ( !ber_bvstrcasecmp( &bv,
1814 &slap_schema.si_ad_entryCSN->ad_cname ) )
1816 slap_queue_csn( op, bvals );
1822 /* If we didn't get a mod type or a target DN, bail out */
1823 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
1828 op->o_callback = &cb;
1829 slap_op_time( &op->o_time, &op->o_tincr );
1831 switch( op->o_tag ) {
1833 case LDAP_REQ_MODIFY:
1834 /* If we didn't get required data, bail */
1835 if ( !modlist ) goto done;
1837 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
1839 if ( rc != LDAP_SUCCESS ) {
1840 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1841 "mods check (%s)\n",
1842 si->si_ridtxt, text, 0 );
1846 if ( op->o_tag == LDAP_REQ_ADD ) {
1847 Entry *e = entry_alloc();
1849 op->ora_e->e_name = op->o_req_dn;
1850 op->ora_e->e_nname = op->o_req_ndn;
1852 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1853 if( rc != LDAP_SUCCESS ) {
1854 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1855 "mods2entry (%s)\n",
1856 si->si_ridtxt, text, 0 );
1858 rc = op->o_bd->be_add( op, &rs );
1859 Debug( LDAP_DEBUG_SYNC,
1860 "syncrepl_message_to_op: %s be_add %s (%d)\n",
1861 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1864 if ( e == op->ora_e )
1865 be_entry_release_w( op, op->ora_e );
1867 op->orm_modlist = modlist;
1868 op->o_bd = si->si_wbe;
1869 rc = op->o_bd->be_modify( op, &rs );
1870 modlist = op->orm_modlist;
1871 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1872 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
1873 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1874 op->o_bd = si->si_be;
1878 case LDAP_REQ_MODRDN:
1879 if ( BER_BVISNULL( &rdn ) ) goto done;
1881 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
1884 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
1887 if ( !BER_BVISNULL( &sup ) ) {
1888 REWRITE_DN( si, sup, bv2, psup, nsup );
1891 op->orr_newSup = &psup;
1892 op->orr_nnewSup = ⊅
1894 op->orr_newSup = NULL;
1895 op->orr_nnewSup = NULL;
1897 op->orr_newrdn = prdn;
1898 op->orr_nnewrdn = nrdn;
1899 op->orr_deleteoldrdn = deleteOldRdn;
1900 op->orr_modlist = NULL;
1901 if ( slap_modrdn2mods( op, &rs ) ) {
1905 /* Append modlist for operational attrs */
1909 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
1911 m->sml_next = modlist;
1914 rc = op->o_bd->be_modrdn( op, &rs );
1915 slap_mods_free( op->orr_modlist, 1 );
1916 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1917 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
1918 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1921 case LDAP_REQ_DELETE:
1922 rc = op->o_bd->be_delete( op, &rs );
1923 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1924 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
1925 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1931 slap_graduate_commit_csn( op );
1932 op->o_bd = si->si_be;
1933 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1934 BER_BVZERO( &op->o_csn );
1936 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1938 if ( !BER_BVISNULL( &rdn ) ) {
1939 if ( !BER_BVISNULL( &nsup ) ) {
1940 ch_free( nsup.bv_val );
1942 if ( !BER_BVISNULL( &psup ) ) {
1943 ch_free( psup.bv_val );
1945 if ( !BER_BVISNULL( &nrdn ) ) {
1946 ch_free( nrdn.bv_val );
1948 if ( !BER_BVISNULL( &prdn ) ) {
1949 ch_free( prdn.bv_val );
1953 ch_free( op->o_req_ndn.bv_val );
1954 ch_free( op->o_req_dn.bv_val );
1961 syncrepl_message_to_entry(
1965 Modifications **modlist,
1971 BerElement *ber = NULL;
1974 Modifications **modtail = modlist;
1977 char txtbuf[SLAP_TEXT_BUFLEN];
1978 size_t textlen = sizeof txtbuf;
1980 struct berval bdn = BER_BVNULL, dn, ndn, bv2;
1985 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1986 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
1987 "Message type should be entry (%d)",
1988 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1992 op->o_tag = LDAP_REQ_ADD;
1994 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1995 if ( rc != LDAP_SUCCESS ) {
1996 Debug( LDAP_DEBUG_ANY,
1997 "syncrepl_message_to_entry: %s dn get failed (%d)",
1998 si->si_ridtxt, rc, 0 );
2002 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
2003 Debug( LDAP_DEBUG_ANY,
2004 "syncrepl_message_to_entry: %s got empty dn",
2005 si->si_ridtxt, 0, 0 );
2009 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
2010 /* NOTE: this could be done even before decoding the DN,
2011 * although encoding errors wouldn't be detected */
2016 if ( entry == NULL ) {
2020 REWRITE_DN( si, bdn, bv2, dn, ndn );
2021 if ( rc != LDAP_SUCCESS ) {
2022 /* One of the things that could happen is that the schema
2023 * is not lined-up; this could result in unknown attributes.
2024 * A value non conformant to the syntax should be unlikely,
2025 * except when replicating between different versions
2026 * of the software, or when syntax validation bugs are fixed
2028 Debug( LDAP_DEBUG_ANY,
2029 "syncrepl_message_to_entry: "
2030 "%s dn \"%s\" normalization failed (%d)",
2031 si->si_ridtxt, bdn.bv_val, rc );
2035 ber_dupbv( &op->o_req_dn, &dn );
2036 ber_dupbv( &op->o_req_ndn, &ndn );
2037 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2038 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2040 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
2043 e->e_name = op->o_req_dn;
2044 e->e_nname = op->o_req_ndn;
2046 while ( ber_remaining( ber ) ) {
2047 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
2048 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2053 /* Drop all updates to the contextCSN of the context entry
2056 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2057 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2058 ber_bvarray_free( tmp.sml_values );
2062 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2064 mod->sml_op = LDAP_MOD_REPLACE;
2066 mod->sml_next = NULL;
2067 mod->sml_desc = NULL;
2068 mod->sml_type = tmp.sml_type;
2069 mod->sml_values = tmp.sml_values;
2070 mod->sml_nvalues = NULL;
2071 mod->sml_numvals = 0; /* slap_mods_check will set this */
2073 #ifdef ENABLE_REWRITE
2074 if (si->si_rewrite) {
2075 AttributeDescription *ad = NULL;
2076 slap_bv2ad( &tmp.sml_type, &ad, &text );
2079 mod->sml_type = ad->ad_cname;
2080 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
2082 for ( i = 0; tmp.sml_values[i].bv_val; i++ ) {
2083 syncrepl_rewrite_dn( si, &tmp.sml_values[i], &bv2);
2084 if ( !BER_BVISNULL( &bv2 )) {
2085 ber_memfree( tmp.sml_values[i].bv_val );
2086 tmp.sml_values[i] = bv2;
2094 modtail = &mod->sml_next;
2097 if ( *modlist == NULL ) {
2098 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2099 si->si_ridtxt, 0, 0 );
2104 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2106 if ( rc != LDAP_SUCCESS ) {
2107 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2108 si->si_ridtxt, text, 0 );
2112 /* Strip out dynamically generated attrs */
2113 for ( modtail = modlist; *modtail ; ) {
2115 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2116 *modtail = mod->sml_next;
2117 slap_mod_free( &mod->sml_mod, 0 );
2120 modtail = &mod->sml_next;
2124 /* Strip out attrs in exattrs list */
2125 for ( modtail = modlist; *modtail ; ) {
2127 if ( ldap_charray_inlist( si->si_exattrs,
2128 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2130 *modtail = mod->sml_next;
2131 slap_mod_free( &mod->sml_mod, 0 );
2134 modtail = &mod->sml_next;
2138 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2139 if( rc != LDAP_SUCCESS ) {
2140 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2141 si->si_ridtxt, text, 0 );
2146 if ( rc != LDAP_SUCCESS ) {
2158 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2160 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2161 * entry if a previous refresh was interrupted before sending us a new
2162 * context state. We try to compare the new entry to the existing entry
2163 * and ignore the new entry if they are the same.
2165 * Also, we may get an update where the entryDN has changed, due to
2166 * a ModDn on the provider. We detect this as well, so we can issue
2167 * the corresponding operation locally.
2169 * In the case of a modify, we get a list of all the attributes
2170 * in the original entry. Rather than deleting the entry and re-adding it,
2171 * we issue a Modify request that deletes all the attributes and adds all
2172 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2175 * We otherwise distinguish ModDN from Modify; in the case of
2176 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2177 * operational attributes from the entry, and do a regular ModDN.
2179 typedef struct dninfo {
2183 struct berval nnewSup;
2184 int renamed; /* Was an existing entry renamed? */
2185 int delOldRDN; /* Was old RDN deleted? */
2186 Modifications **modlist; /* the modlist we received */
2187 Modifications *mods; /* the modlist we compared */
2188 Attribute *oldNattr; /* old naming attr */
2189 AttributeDescription *oldDesc; /* for renames */
2190 AttributeDescription *newDesc; /* for renames */
2193 /* return 1 if inserted, 0 otherwise */
2195 avl_presentlist_insert(
2197 struct berval *syncUUID )
2199 struct berval *syncuuid_bv = ch_malloc( sizeof( struct berval ) + syncUUID->bv_len + 1 );
2201 syncuuid_bv->bv_len = syncUUID->bv_len;
2202 syncuuid_bv->bv_val = (char *)&syncuuid_bv[1];
2203 AC_MEMCPY( syncuuid_bv->bv_val, syncUUID->bv_val, syncUUID->bv_len );
2204 syncuuid_bv->bv_val[ syncuuid_bv->bv_len ] = '\0';
2206 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
2207 syncuuid_cmp, avl_dup_error ) )
2209 ch_free( syncuuid_bv );
2221 Modifications** modlist,
2223 struct berval* syncUUID,
2224 struct berval* syncCSN )
2226 Backend *be = op->o_bd;
2227 slap_callback cb = { NULL, NULL, NULL, NULL };
2228 int syncuuid_inserted = 0;
2229 struct berval syncUUID_strrep = BER_BVNULL;
2231 SlapReply rs_search = {REP_RESULT};
2232 SlapReply rs_delete = {REP_RESULT};
2233 SlapReply rs_add = {REP_RESULT};
2234 SlapReply rs_modify = {REP_RESULT};
2236 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2237 int rc = LDAP_SUCCESS;
2239 struct berval pdn = BER_BVNULL;
2244 Debug( LDAP_DEBUG_SYNC,
2245 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2246 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2248 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2249 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2250 syncuuid_inserted = avl_presentlist_insert( si, syncUUID );
2254 if ( syncstate == LDAP_SYNC_PRESENT ) {
2256 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2257 if ( entry == NULL ) {
2262 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
2263 if ( syncstate != LDAP_SYNC_DELETE ) {
2264 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2267 /* add if missing */
2268 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2269 &syncUUID_strrep, syncUUID );
2271 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2272 /* replace only if necessary */
2273 if ( a->a_nvals != a->a_vals ) {
2274 ber_memfree( a->a_nvals[0].bv_val );
2275 ber_dupbv( &a->a_nvals[0], syncUUID );
2277 ber_memfree( a->a_vals[0].bv_val );
2278 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
2282 f.f_choice = LDAP_FILTER_EQUALITY;
2284 ava.aa_desc = slap_schema.si_ad_entryUUID;
2285 ava.aa_value = *syncUUID;
2287 if ( syncuuid_inserted ) {
2288 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2289 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
2291 op->ors_filter = &f;
2293 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
2294 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2295 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2296 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2297 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2298 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
2299 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2300 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2302 op->o_tag = LDAP_REQ_SEARCH;
2303 op->ors_scope = LDAP_SCOPE_SUBTREE;
2304 op->ors_deref = LDAP_DEREF_NEVER;
2306 /* get the entry for this UUID */
2307 op->o_req_dn = si->si_base;
2308 op->o_req_ndn = si->si_base;
2310 op->o_time = slap_get_time();
2311 op->ors_tlimit = SLAP_NO_LIMIT;
2313 op->ors_limit = NULL;
2315 op->ors_attrs = slap_anlist_all_attributes;
2316 op->ors_attrsonly = 0;
2318 /* set callback function */
2319 op->o_callback = &cb;
2320 cb.sc_response = dn_callback;
2321 cb.sc_private = &dni;
2322 dni.new_entry = entry;
2323 dni.modlist = modlist;
2325 rc = be->be_search( op, &rs_search );
2326 Debug( LDAP_DEBUG_SYNC,
2327 "syncrepl_entry: %s be_search (%d)\n",
2328 si->si_ridtxt, rc, 0 );
2330 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2331 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2334 cb.sc_response = null_callback;
2337 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2338 Debug( LDAP_DEBUG_SYNC,
2339 "syncrepl_entry: %s %s\n",
2340 si->si_ridtxt, entry->e_name.bv_val, 0 );
2342 Debug( LDAP_DEBUG_SYNC,
2343 "syncrepl_entry: %s %s\n",
2344 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2347 assert( BER_BVISNULL( &op->o_csn ) );
2349 slap_queue_csn( op, syncCSN );
2352 slap_op_time( &op->o_time, &op->o_tincr );
2353 switch ( syncstate ) {
2355 case LDAP_SYNC_MODIFY:
2356 if ( BER_BVISNULL( &op->o_csn ))
2359 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2361 /* FIXME: op->o_csn is assumed to be
2362 * on the thread's slab; this needs
2363 * to be cleared ASAP.
2364 * What happens if already present?
2366 assert( BER_BVISNULL( &op->o_csn ) );
2367 op->o_csn = a->a_vals[0];
2372 if ( BER_BVISNULL( &dni.dn ) ) {
2374 op->o_req_dn = entry->e_name;
2375 op->o_req_ndn = entry->e_nname;
2376 op->o_tag = LDAP_REQ_ADD;
2378 op->o_bd = si->si_wbe;
2380 rc = op->o_bd->be_add( op, &rs_add );
2381 Debug( LDAP_DEBUG_SYNC,
2382 "syncrepl_entry: %s be_add %s (%d)\n",
2383 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2384 switch ( rs_add.sr_err ) {
2386 if ( op->ora_e == entry ) {
2387 be_entry_release_w( op, entry );
2393 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2394 * only if the suffix entry is not present */
2395 case LDAP_NO_SUCH_OBJECT:
2396 rc = syncrepl_add_glue( op, entry );
2400 /* if an entry was added via syncrepl_add_glue(),
2401 * it likely has no entryUUID, so the previous
2402 * be_search() doesn't find it. In this case,
2403 * give syncrepl a chance to modify it. Also
2404 * allow for entries that were recreated with the
2405 * same DN but a different entryUUID.
2407 case LDAP_ALREADY_EXISTS:
2409 Operation op2 = *op;
2410 SlapReply rs2 = { 0 };
2411 slap_callback cb2 = { 0 };
2414 op2.o_tag = LDAP_REQ_SEARCH;
2415 op2.o_req_dn = entry->e_name;
2416 op2.o_req_ndn = entry->e_nname;
2417 op2.ors_scope = LDAP_SCOPE_BASE;
2418 op2.ors_deref = LDAP_DEREF_NEVER;
2419 op2.ors_attrs = slap_anlist_all_attributes;
2420 op2.ors_attrsonly = 0;
2421 op2.ors_limit = NULL;
2423 op2.ors_tlimit = SLAP_NO_LIMIT;
2425 f.f_choice = LDAP_FILTER_PRESENT;
2426 f.f_desc = slap_schema.si_ad_objectClass;
2427 op2.ors_filter = &f;
2428 op2.ors_filterstr = generic_filterstr;
2430 op2.o_callback = &cb2;
2431 cb2.sc_response = dn_callback;
2432 cb2.sc_private = &dni;
2434 rc = be->be_search( &op2, &rs2 );
2435 if ( rc ) goto done;
2438 slap_op_time( &op->o_time, &op->o_tincr );
2444 Debug( LDAP_DEBUG_ANY,
2445 "syncrepl_entry: %s be_add %s failed (%d)\n",
2446 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
2454 op->o_req_dn = dni.dn;
2455 op->o_req_ndn = dni.ndn;
2456 if ( dni.renamed ) {
2457 struct berval noldp, newp;
2458 Modifications *mod, **modtail, **ml, *m2;
2459 int i, got_replace = 0, just_rename = 0;
2461 op->o_tag = LDAP_REQ_MODRDN;
2462 dnRdn( &entry->e_name, &op->orr_newrdn );
2463 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2465 if ( !BER_BVISNULL( &dni.nnewSup )) {
2466 dnParent( &entry->e_name, &newp );
2467 op->orr_newSup = &newp;
2468 op->orr_nnewSup = &dni.nnewSup;
2470 op->orr_newSup = NULL;
2471 op->orr_nnewSup = NULL;
2473 op->orr_deleteoldrdn = dni.delOldRDN;
2474 op->orr_modlist = NULL;
2475 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2479 /* Drop the RDN-related mods from this op, because their
2480 * equivalents were just setup by slap_modrdn2mods.
2482 * If delOldRDN is TRUE then we should see a delete modop
2483 * for oldDesc. We might see a replace instead.
2484 * delete with no values: therefore newDesc != oldDesc.
2485 * if oldNattr had only one value, then Drop this op.
2486 * delete with 1 value: can only be the oldRDN value. Drop op.
2487 * delete with N values: Drop oldRDN value, keep remainder.
2488 * replace with 1 value: if oldNattr had only one value and
2489 * newDesc == oldDesc, Drop this op.
2490 * Any other cases must be left intact.
2492 * We should also see an add modop for newDesc. (But not if
2493 * we got a replace modop due to delOldRDN.) If it has
2494 * multiple values, we'll have to drop the new RDN value.
2496 modtail = &op->orr_modlist;
2497 if ( dni.delOldRDN ) {
2498 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2499 if ( (*ml)->sml_desc == dni.oldDesc ) {
2501 if ( mod->sml_op == LDAP_MOD_REPLACE &&
2502 dni.oldDesc != dni.newDesc ) {
2503 /* This Replace is due to other Mods.
2508 if ( mod->sml_numvals <= 1 &&
2509 dni.oldNattr->a_numvals == 1 &&
2510 ( mod->sml_op == LDAP_MOD_DELETE ||
2511 mod->sml_op == LDAP_MOD_REPLACE )) {
2512 if ( mod->sml_op == LDAP_MOD_REPLACE )
2515 *ml = mod->sml_next;
2516 mod->sml_next = NULL;
2517 slap_mods_free( mod, 1 );
2520 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
2522 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2523 if ( m2->sml_desc == dni.oldDesc &&
2524 m2->sml_op == LDAP_MOD_DELETE ) break;
2526 for ( i=0; i<mod->sml_numvals; i++ ) {
2527 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2529 ch_free( mod->sml_values[i].bv_val );
2530 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2531 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2532 if ( mod->sml_nvalues ) {
2533 ch_free( mod->sml_nvalues[i].bv_val );
2534 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2535 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2540 if ( !mod->sml_numvals ) {
2542 *ml = mod->sml_next;
2543 mod->sml_next = NULL;
2544 slap_mods_free( mod, 1 );
2550 if ( !got_replace ) {
2551 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2552 if ( (*ml)->sml_desc == dni.newDesc ) {
2554 if ( mod->sml_op != LDAP_MOD_ADD )
2556 if ( mod->sml_numvals == 1 ) {
2558 *ml = mod->sml_next;
2559 mod->sml_next = NULL;
2560 slap_mods_free( mod, 1 );
2563 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2564 if ( m2->sml_desc == dni.oldDesc &&
2565 m2->sml_op == SLAP_MOD_SOFTADD ) break;
2567 for ( i=0; i<mod->sml_numvals; i++ ) {
2568 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2570 ch_free( mod->sml_values[i].bv_val );
2571 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2572 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2573 if ( mod->sml_nvalues ) {
2574 ch_free( mod->sml_nvalues[i].bv_val );
2575 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2576 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2586 /* RDNs must be NUL-terminated for back-ldap */
2587 noldp = op->orr_newrdn;
2588 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
2589 noldp = op->orr_nnewrdn;
2590 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
2592 /* Setup opattrs too */
2594 static AttributeDescription *nullattr = NULL;
2595 static AttributeDescription **const opattrs[] = {
2596 &slap_schema.si_ad_entryCSN,
2597 &slap_schema.si_ad_modifiersName,
2598 &slap_schema.si_ad_modifyTimestamp,
2601 AttributeDescription *opattr;
2605 /* pull mod off incoming modlist */
2606 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
2607 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
2609 if ( (*ml)->sml_desc == opattr ) {
2611 *ml = mod->sml_next;
2612 mod->sml_next = NULL;
2614 modtail = &mod->sml_next;
2619 /* If there are still Modifications left, put the opattrs
2620 * back, and let be_modify run. Otherwise, append the opattrs
2621 * to the orr_modlist.
2625 /* don't set a CSN for the rename op */
2627 slap_graduate_commit_csn( op );
2629 mod = op->orr_modlist;
2632 for ( ; mod->sml_next; mod=mod->sml_next );
2635 op->o_bd = si->si_wbe;
2637 rc = op->o_bd->be_modrdn( op, &rs_modify );
2639 /* NOTE: noSuchObject should result because the new superior
2640 * has not been added yet (ITS#6472) */
2641 if ( rc == LDAP_NO_SUCH_OBJECT && !BER_BVISNULL( op->orr_nnewSup )) {
2642 Operation op2 = *op;
2643 rc = syncrepl_add_glue_ancestors( &op2, entry );
2644 if ( rc == LDAP_SUCCESS ) {
2649 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
2650 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
2652 slap_mods_free( op->orr_modlist, 1 );
2653 Debug( LDAP_DEBUG_SYNC,
2654 "syncrepl_entry: %s be_modrdn %s (%d)\n",
2655 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2657 /* Renamed entries may still have other mods so just fallthru */
2658 op->o_req_dn = entry->e_name;
2659 op->o_req_ndn = entry->e_nname;
2660 /* Use CSN on the modify */
2664 slap_queue_csn( op, syncCSN );
2667 op->o_tag = LDAP_REQ_MODIFY;
2668 op->orm_modlist = dni.mods;
2669 op->orm_no_opattrs = 1;
2670 op->o_bd = si->si_wbe;
2672 rc = op->o_bd->be_modify( op, &rs_modify );
2673 slap_mods_free( op->orm_modlist, 1 );
2674 op->orm_no_opattrs = 0;
2675 Debug( LDAP_DEBUG_SYNC,
2676 "syncrepl_entry: %s be_modify %s (%d)\n",
2677 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2678 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2679 Debug( LDAP_DEBUG_ANY,
2680 "syncrepl_entry: %s be_modify failed (%d)\n",
2681 si->si_ridtxt, rs_modify.sr_err, 0 );
2685 } else if ( !dni.renamed ) {
2686 Debug( LDAP_DEBUG_SYNC,
2687 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
2688 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
2690 slap_graduate_commit_csn( op );
2695 case LDAP_SYNC_DELETE :
2696 if ( !BER_BVISNULL( &dni.dn ) ) {
2697 op->o_req_dn = dni.dn;
2698 op->o_req_ndn = dni.ndn;
2699 op->o_tag = LDAP_REQ_DELETE;
2700 op->o_bd = si->si_wbe;
2701 rc = op->o_bd->be_delete( op, &rs_delete );
2702 Debug( LDAP_DEBUG_SYNC,
2703 "syncrepl_entry: %s be_delete %s (%d)\n",
2704 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2706 while ( rs_delete.sr_err == LDAP_SUCCESS
2707 && op->o_delete_glue_parent ) {
2708 op->o_delete_glue_parent = 0;
2709 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2710 slap_callback cb = { NULL };
2711 cb.sc_response = slap_null_cb;
2712 dnParent( &op->o_req_ndn, &pdn );
2714 op->o_req_ndn = pdn;
2715 op->o_callback = &cb;
2716 op->o_bd->be_delete( op, &rs_delete );
2727 Debug( LDAP_DEBUG_ANY,
2728 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
2733 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2734 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2735 BER_BVZERO( &syncUUID_strrep );
2737 if ( !BER_BVISNULL( &dni.ndn ) ) {
2738 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2740 if ( !BER_BVISNULL( &dni.dn ) ) {
2741 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2744 entry_free( entry );
2747 slap_graduate_commit_csn( op );
2749 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
2750 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2752 BER_BVZERO( &op->o_csn );
2756 static struct berval gcbva[] = {
2762 #define NP_DELETE_ONE 2
2765 syncrepl_del_nonpresent(
2769 struct sync_cookie *sc,
2772 Backend* be = op->o_bd;
2773 slap_callback cb = { NULL };
2774 SlapReply rs_search = {REP_RESULT};
2775 SlapReply rs_delete = {REP_RESULT};
2776 SlapReply rs_modify = {REP_RESULT};
2777 struct nonpresent_entry *np_list, *np_prev;
2779 AttributeName an[2];
2781 struct berval pdn = BER_BVNULL;
2784 op->o_req_dn = si->si_base;
2785 op->o_req_ndn = si->si_base;
2787 cb.sc_response = nonpresent_callback;
2790 op->o_callback = &cb;
2791 op->o_tag = LDAP_REQ_SEARCH;
2792 op->ors_scope = si->si_scope;
2793 op->ors_deref = LDAP_DEREF_NEVER;
2794 op->o_time = slap_get_time();
2795 op->ors_tlimit = SLAP_NO_LIMIT;
2800 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
2803 op->ors_attrsonly = 1;
2804 op->ors_attrs = slap_anlist_no_attrs;
2805 op->ors_limit = NULL;
2806 op->ors_filter = &uf;
2809 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2811 uf.f_choice = LDAP_FILTER_EQUALITY;
2812 si->si_refreshDelete |= NP_DELETE_ONE;
2814 for (i=0; uuids[i].bv_val; i++) {
2816 uf.f_av_value = uuids[i];
2817 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2818 rc = be->be_search( op, &rs_search );
2819 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2821 si->si_refreshDelete ^= NP_DELETE_ONE;
2825 AttributeAssertion mmaa;
2827 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2828 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2829 an[0].an_desc = slap_schema.si_ad_entryUUID;
2831 op->ors_slimit = SLAP_NO_LIMIT;
2832 op->ors_tlimit = SLAP_NO_LIMIT;
2833 op->ors_limit = NULL;
2834 op->ors_attrsonly = 0;
2835 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
2836 /* In multimaster, updates can continue to arrive while
2837 * we're searching. Limit the search result to entries
2838 * older than our newest cookie CSN.
2840 if ( SLAP_MULTIMASTER( op->o_bd )) {
2845 f->f_choice = LDAP_FILTER_AND;
2846 f->f_next = op->ors_filter;
2850 f->f_choice = LDAP_FILTER_LE;
2852 f->f_av_desc = slap_schema.si_ad_entryCSN;
2854 BER_BVZERO( &f->f_av_value );
2855 for ( i=0; i<sc->numcsns; i++ ) {
2856 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
2857 f->f_av_value = sc->ctxcsn[i];
2859 of = op->ors_filter;
2860 op->ors_filter = mmf;
2861 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2864 op->ors_filterstr = si->si_filterstr;
2866 op->o_nocaching = 1;
2869 rc = be->be_search( op, &rs_search );
2870 if ( SLAP_MULTIMASTER( op->o_bd )) {
2871 op->ors_filter = of;
2873 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
2874 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
2875 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2880 op->o_nocaching = 0;
2882 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2884 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
2885 csn = sc->ctxcsn[m];
2887 csn = si->si_syncCookie.ctxcsn[0];
2890 op->o_bd = si->si_wbe;
2891 slap_queue_csn( op, &csn );
2893 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2894 while ( np_list != NULL ) {
2895 LDAP_LIST_REMOVE( np_list, npe_link );
2897 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2898 op->o_tag = LDAP_REQ_DELETE;
2899 op->o_callback = &cb;
2900 cb.sc_response = null_callback;
2902 op->o_req_dn = *np_prev->npe_name;
2903 op->o_req_ndn = *np_prev->npe_nname;
2904 rc = op->o_bd->be_delete( op, &rs_delete );
2905 Debug( LDAP_DEBUG_SYNC,
2906 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
2907 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2909 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2910 Modifications mod1, mod2;
2911 mod1.sml_op = LDAP_MOD_REPLACE;
2913 mod1.sml_desc = slap_schema.si_ad_objectClass;
2914 mod1.sml_type = mod1.sml_desc->ad_cname;
2915 mod1.sml_numvals = 2;
2916 mod1.sml_values = &gcbva[0];
2917 mod1.sml_nvalues = NULL;
2918 mod1.sml_next = &mod2;
2920 mod2.sml_op = LDAP_MOD_REPLACE;
2922 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2923 mod2.sml_type = mod2.sml_desc->ad_cname;
2924 mod2.sml_numvals = 1;
2925 mod2.sml_values = &gcbva[1];
2926 mod2.sml_nvalues = NULL;
2927 mod2.sml_next = NULL;
2929 op->o_tag = LDAP_REQ_MODIFY;
2930 op->orm_modlist = &mod1;
2932 rc = op->o_bd->be_modify( op, &rs_modify );
2933 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2936 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2937 op->o_delete_glue_parent ) {
2938 op->o_delete_glue_parent = 0;
2939 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2940 slap_callback cb = { NULL };
2941 cb.sc_response = slap_null_cb;
2942 dnParent( &op->o_req_ndn, &pdn );
2944 op->o_req_ndn = pdn;
2945 op->o_callback = &cb;
2946 /* give it a root privil ? */
2947 op->o_bd->be_delete( op, &rs_delete );
2953 op->o_delete_glue_parent = 0;
2955 ber_bvfree( np_prev->npe_name );
2956 ber_bvfree( np_prev->npe_nname );
2959 if ( slapd_shutdown ) {
2964 slap_graduate_commit_csn( op );
2967 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2968 BER_BVZERO( &op->o_csn );
2975 syncrepl_add_glue_ancestors(
2979 Backend *be = op->o_bd;
2980 slap_callback cb = { NULL };
2985 struct berval dn = BER_BVNULL;
2986 struct berval ndn = BER_BVNULL;
2988 SlapReply rs_add = {REP_RESULT};
2989 struct berval ptr, nptr;
2992 op->o_tag = LDAP_REQ_ADD;
2993 op->o_callback = &cb;
2994 cb.sc_response = null_callback;
2995 cb.sc_private = NULL;
3000 /* count RDNs in suffix */
3001 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3002 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3004 ptr.bv_len -= comma - ptr.bv_val;
3014 /* Start with BE suffix */
3016 for ( i = 0; i < suffrdns; i++ ) {
3017 comma = ber_bvrchr( &ptr, ',' );
3018 if ( comma != NULL ) {
3019 ptr.bv_len = comma - ptr.bv_val;
3026 if ( !BER_BVISEMPTY( &ptr ) ) {
3027 dn.bv_len -= ptr.bv_len + 1;
3028 dn.bv_val += ptr.bv_len + 1;
3031 /* the normalizedDNs are always the same length, no counting
3035 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3036 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3037 ndn.bv_len = be->be_nsuffix[0].bv_len;
3039 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3045 while ( ndn.bv_val > e->e_nname.bv_val ) {
3046 glue = entry_alloc();
3047 ber_dupbv( &glue->e_name, &dn );
3048 ber_dupbv( &glue->e_nname, &ndn );
3050 a = attr_alloc( slap_schema.si_ad_objectClass );
3053 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3054 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3055 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3056 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3058 a->a_nvals = a->a_vals;
3060 a->a_next = glue->e_attrs;
3063 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3066 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3067 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3068 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3070 a->a_nvals = a->a_vals;
3072 a->a_next = glue->e_attrs;
3075 op->o_req_dn = glue->e_name;
3076 op->o_req_ndn = glue->e_nname;
3078 rc = be->be_add ( op, &rs_add );
3079 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3080 if ( op->ora_e == glue )
3081 be_entry_release_w( op, glue );
3083 /* incl. ALREADY EXIST */
3085 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3091 /* Move to next child */
3092 comma = ber_bvrchr( &ptr, ',' );
3093 if ( comma == NULL ) {
3096 ptr.bv_len = comma - ptr.bv_val;
3098 dn.bv_val = ++comma;
3099 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3101 comma = ber_bvrchr( &nptr, ',' );
3102 assert( comma != NULL );
3103 nptr.bv_len = comma - nptr.bv_val;
3105 ndn.bv_val = ++comma;
3106 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3117 slap_callback cb = { NULL };
3119 Backend *be = op->o_bd;
3120 SlapReply rs_add = {REP_RESULT};
3122 rc = syncrepl_add_glue_ancestors( op, e );
3125 case LDAP_ALREADY_EXISTS:
3132 op->o_tag = LDAP_REQ_ADD;
3133 op->o_callback = &cb;
3134 cb.sc_response = null_callback;
3135 cb.sc_private = NULL;
3137 op->o_req_dn = e->e_name;
3138 op->o_req_ndn = e->e_nname;
3140 rc = be->be_add ( op, &rs_add );
3141 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3142 if ( op->ora_e == e )
3143 be_entry_release_w( op, e );
3152 syncrepl_updateCookie(
3155 struct sync_cookie *syncCookie )
3157 Backend *be = op->o_bd;
3159 struct berval first = BER_BVNULL;
3161 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3164 int rc, i, j, changed = 0;
3167 slap_callback cb = { NULL };
3168 SlapReply rs_modify = {REP_RESULT};
3170 mod.sml_op = LDAP_MOD_REPLACE;
3171 mod.sml_desc = slap_schema.si_ad_contextCSN;
3172 mod.sml_type = mod.sml_desc->ad_cname;
3173 mod.sml_flags = SLAP_MOD_INTERNAL;
3174 mod.sml_nvalues = NULL;
3175 mod.sml_next = NULL;
3177 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3180 for ( i=0; i<syncCookie->numcsns; i++ ) {
3181 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3183 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3184 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3188 /* clone the cookieState CSNs so we can Replace the whole thing */
3189 mod.sml_numvals = si->si_cookieState->cs_num;
3190 mod.sml_values = op->o_tmpalloc(( mod.sml_numvals+1 )*sizeof(struct berval), op->o_tmpmemctx );
3191 for ( i=0; i<mod.sml_numvals; i++ )
3192 mod.sml_values[i] = si->si_cookieState->cs_vals[i];
3193 BER_BVZERO( &mod.sml_values[i] );
3195 /* find any CSNs in the syncCookie that are newer than the cookieState */
3196 for ( i=0; i<syncCookie->numcsns; i++ ) {
3197 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
3198 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
3200 len = syncCookie->ctxcsn[i].bv_len;
3201 if ( len > si->si_cookieState->cs_vals[j].bv_len )
3202 len = si->si_cookieState->cs_vals[j].bv_len;
3203 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3204 si->si_cookieState->cs_vals[j].bv_val, len ) > 0 ) {
3205 mod.sml_values[j] = syncCookie->ctxcsn[i];
3207 if ( BER_BVISNULL( &first ) ) {
3208 first = syncCookie->ctxcsn[i];
3210 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3212 first = syncCookie->ctxcsn[i];
3217 /* there was no match for this SID, it's a new CSN */
3218 if ( j == si->si_cookieState->cs_num ) {
3219 mod.sml_values = op->o_tmprealloc( mod.sml_values,
3220 ( mod.sml_numvals+2 )*sizeof(struct berval), op->o_tmpmemctx );
3221 mod.sml_values[mod.sml_numvals++] = syncCookie->ctxcsn[i];
3222 BER_BVZERO( &mod.sml_values[mod.sml_numvals] );
3223 if ( BER_BVISNULL( &first ) ) {
3224 first = syncCookie->ctxcsn[i];
3225 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3227 first = syncCookie->ctxcsn[i];
3232 /* Should never happen, ITS#5065 */
3233 if ( BER_BVISNULL( &first ) || !changed ) {
3234 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3235 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3238 op->o_bd = si->si_wbe;
3239 slap_queue_csn( op, &first );
3241 op->o_tag = LDAP_REQ_MODIFY;
3243 cb.sc_response = null_callback;
3246 op->o_callback = &cb;
3247 op->o_req_dn = si->si_contextdn;
3248 op->o_req_ndn = si->si_contextdn;
3250 /* update contextCSN */
3251 op->o_dont_replicate = 1;
3253 op->orm_modlist = &mod;
3254 op->orm_no_opattrs = 1;
3255 rc = op->o_bd->be_modify( op, &rs_modify );
3257 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3258 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3260 char txtbuf[SLAP_TEXT_BUFLEN];
3261 size_t textlen = sizeof txtbuf;
3262 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3263 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3265 rc = op->o_bd->be_add( op, &rs_modify );
3266 if ( e == op->ora_e )
3267 be_entry_release_w( op, op->ora_e );
3270 op->orm_no_opattrs = 0;
3271 op->o_dont_replicate = 0;
3273 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3274 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3275 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
3276 /* If we replaced any old values */
3277 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3278 if ( mod.sml_values[i].bv_val != si->si_cookieState->cs_vals[i].bv_val )
3279 ber_bvreplace( &si->si_cookieState->cs_vals[i],
3280 &mod.sml_values[i] );
3282 /* Handle any added values */
3283 if ( i < mod.sml_numvals ) {
3284 si->si_cookieState->cs_num = mod.sml_numvals;
3285 value_add( &si->si_cookieState->cs_vals, &mod.sml_values[i] );
3286 free( si->si_cookieState->cs_sids );
3287 si->si_cookieState->cs_sids = slap_parse_csn_sids(
3288 si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
3291 si->si_cookieState->cs_age++;
3292 si->si_cookieAge = si->si_cookieState->cs_age;
3294 Debug( LDAP_DEBUG_ANY,
3295 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3296 si->si_ridtxt, rs_modify.sr_err, 0 );
3298 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3301 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3302 BER_BVZERO( &op->o_csn );
3303 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3304 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3307 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3308 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3315 /* Compare the attribute from the old entry to the one in the new
3316 * entry. The Modifications from the new entry will either be left
3317 * in place, or changed to an Add or Delete as needed.
3320 attr_cmp( Operation *op, Attribute *old, Attribute *new,
3321 Modifications ***mret, Modifications ***mcur )
3324 Modifications *mod, **modtail;
3330 struct berval **adds, **dels;
3331 /* count old and new */
3332 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
3333 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
3335 /* there MUST be both old and new values */
3340 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
3341 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
3343 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
3344 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
3348 for ( i=0; i<o; i++ ) {
3349 for ( j=0; j<n; j++ ) {
3352 if ( bvmatch( dels[i], adds[j] ) ) {
3362 /* Don't delete/add an objectClass, always use the replace op.
3363 * Modify would fail if provider has replaced entry with a new,
3364 * and the new explicitly includes a superior of a class that was
3365 * only included implicitly in the old entry. Ref ITS#5517.
3367 * Also use replace op if attr has no equality matching rule.
3370 if ( ( nn || ( no > 0 && no < o ) ) &&
3371 ( old->a_desc == slap_schema.si_ad_objectClass ||
3372 !old->a_desc->ad_type->sat_equality ) )
3378 /* all old values were deleted, just use the replace op */
3382 /* delete some values */
3383 mod = ch_malloc( sizeof( Modifications ) );
3384 mod->sml_op = LDAP_MOD_DELETE;
3386 mod->sml_desc = old->a_desc;
3387 mod->sml_type = mod->sml_desc->ad_cname;
3388 mod->sml_numvals = no;
3389 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3390 if ( old->a_vals != old->a_nvals ) {
3391 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3393 mod->sml_nvalues = NULL;
3396 for ( i = 0; i < o; i++ ) {
3397 if ( !dels[i] ) continue;
3398 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
3399 if ( mod->sml_nvalues ) {
3400 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
3404 BER_BVZERO( &mod->sml_values[j] );
3405 if ( mod->sml_nvalues ) {
3406 BER_BVZERO( &mod->sml_nvalues[j] );
3409 modtail = &mod->sml_next;
3412 op->o_tmpfree( dels, op->o_tmpmemctx );
3413 /* some values were added */
3414 if ( nn && no < o ) {
3415 mod = ch_malloc( sizeof( Modifications ) );
3416 mod->sml_op = LDAP_MOD_ADD;
3418 mod->sml_desc = old->a_desc;
3419 mod->sml_type = mod->sml_desc->ad_cname;
3420 mod->sml_numvals = nn;
3421 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3422 if ( old->a_vals != old->a_nvals ) {
3423 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3425 mod->sml_nvalues = NULL;
3428 for ( i = 0; i < n; i++ ) {
3429 if ( !adds[i] ) continue;
3430 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
3431 if ( mod->sml_nvalues ) {
3432 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
3436 BER_BVZERO( &mod->sml_values[j] );
3437 if ( mod->sml_nvalues ) {
3438 BER_BVZERO( &mod->sml_nvalues[j] );
3441 modtail = &mod->sml_next;
3444 op->o_tmpfree( adds, op->o_tmpmemctx );
3446 /* new attr, just use the new mod */
3450 /* advance to next element */
3454 **mcur = mod->sml_next;
3456 modtail = &mod->sml_next;
3458 *mcur = &mod->sml_next;
3464 /* Generate a set of modifications to change the old entry into the
3465 * new one. On input ml is a list of modifications equivalent to
3466 * the new entry. It will be massaged and the result will be stored
3469 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
3470 Modifications **mods, Modifications **ml, int is_ctx)
3472 Modifications **modtail = mods;
3474 /* We assume that attributes are saved in the same order
3475 * in the remote and local databases. So if we walk through
3476 * the attributeDescriptions one by one they should match in
3477 * lock step. If not, look for an add or delete.
3479 while ( old && new )
3481 /* If we've seen this before, use its mod now */
3482 if ( new->a_flags & SLAP_ATTR_IXADD ) {
3483 attr_cmp( op, NULL, new, &modtail, &ml );
3487 /* Skip contextCSN */
3488 if ( is_ctx && old->a_desc ==
3489 slap_schema.si_ad_contextCSN ) {
3494 if ( old->a_desc != new->a_desc ) {
3498 /* If it's just been re-added later,
3499 * remember that we've seen it.
3501 tmp = attr_find( new, old->a_desc );
3503 tmp->a_flags |= SLAP_ATTR_IXADD;
3505 /* If it's a new attribute, pull it in.
3507 tmp = attr_find( old, new->a_desc );
3509 attr_cmp( op, NULL, new, &modtail, &ml );
3513 /* Delete old attr */
3514 mod = ch_malloc( sizeof( Modifications ) );
3515 mod->sml_op = LDAP_MOD_DELETE;
3517 mod->sml_desc = old->a_desc;
3518 mod->sml_type = mod->sml_desc->ad_cname;
3519 mod->sml_numvals = 0;
3520 mod->sml_values = NULL;
3521 mod->sml_nvalues = NULL;
3523 modtail = &mod->sml_next;
3528 /* kludge - always update modifiersName so that it
3529 * stays co-located with the other mod opattrs. But only
3530 * if we know there are other valid mods.
3532 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
3533 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
3534 attr_cmp( op, NULL, new, &modtail, &ml );
3536 attr_cmp( op, old, new, &modtail, &ml );
3549 dninfo *dni = op->o_callback->sc_private;
3551 if ( rs->sr_type == REP_SEARCH ) {
3552 if ( !BER_BVISNULL( &dni->dn ) ) {
3553 Debug( LDAP_DEBUG_ANY,
3554 "dn_callback : consistency error - "
3555 "entryUUID is not unique\n", 0, 0, 0 );
3557 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
3558 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
3559 /* If there is a new entry, see if it differs from the old.
3560 * We compare the non-normalized values so that cosmetic changes
3561 * in the provider are always propagated.
3563 if ( dni->new_entry ) {
3564 Modifications **modtail, **ml;
3565 Attribute *old, *new;
3566 struct berval old_rdn, new_rdn;
3567 struct berval old_p, new_p;
3568 int is_ctx, new_sup = 0;
3570 /* If old entry is not a glue entry, make sure new entry
3571 * is actually newer than old entry
3573 if ( !is_entry_glue( rs->sr_entry )) {
3574 old = attr_find( rs->sr_entry->e_attrs,
3575 slap_schema.si_ad_entryCSN );
3576 new = attr_find( dni->new_entry->e_attrs,
3577 slap_schema.si_ad_entryCSN );
3580 ber_len_t len = old->a_vals[0].bv_len;
3581 if ( len > new->a_vals[0].bv_len )
3582 len = new->a_vals[0].bv_len;
3583 rc = memcmp( old->a_vals[0].bv_val,
3584 new->a_vals[0].bv_val, len );
3586 Debug( LDAP_DEBUG_SYNC,
3587 "dn_callback : new entry is older than ours "
3588 "%s ours %s, new %s\n",
3589 rs->sr_entry->e_name.bv_val,
3590 old->a_vals[0].bv_val,
3591 new->a_vals[0].bv_val );
3592 return LDAP_SUCCESS;
3593 } else if ( rc == 0 ) {
3594 Debug( LDAP_DEBUG_SYNC,
3595 "dn_callback : entries have identical CSN "
3597 rs->sr_entry->e_name.bv_val,
3598 old->a_vals[0].bv_val, 0 );
3599 return LDAP_SUCCESS;
3604 is_ctx = dn_match( &rs->sr_entry->e_nname,
3605 &op->o_bd->be_nsuffix[0] );
3607 /* Did the DN change?
3608 * case changes in the parent are ignored,
3609 * we only want to know if the RDN was
3612 dnRdn( &rs->sr_entry->e_name, &old_rdn );
3613 dnRdn( &dni->new_entry->e_name, &new_rdn );
3614 dnParent( &rs->sr_entry->e_nname, &old_p );
3615 dnParent( &dni->new_entry->e_nname, &new_p );
3617 new_sup = !dn_match( &old_p, &new_p );
3618 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
3620 struct berval oldRDN, oldVal;
3621 AttributeDescription *ad = NULL;
3627 dni->nnewSup = new_p;
3629 /* See if the oldRDN was deleted */
3630 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
3631 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
3632 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
3634 oldRDN.bv_len -= oldVal.bv_len + 1;
3635 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3637 for ( oldpos=0, a=rs->sr_entry->e_attrs;
3638 a && a->a_desc != ad; oldpos++, a=a->a_next );
3640 for ( newpos=0, a=dni->new_entry->e_attrs;
3641 a && a->a_desc != ad; newpos++, a=a->a_next );
3642 if ( !a || oldpos != newpos || attr_valfind( a,
3643 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
3644 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3645 SLAP_MR_VALUE_OF_SYNTAX,
3646 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
3650 /* Get the newRDN's desc */
3651 dnRdn( &dni->new_entry->e_nname, &oldRDN );
3652 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
3653 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
3655 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3658 /* A ModDN has happened, but in Refresh mode other
3659 * changes may have occurred before we picked it up.
3660 * So fallthru to regular Modify processing.
3664 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
3665 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
3669 } else if ( rs->sr_type == REP_RESULT ) {
3670 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
3671 Debug( LDAP_DEBUG_ANY,
3672 "dn_callback : consistency error - "
3673 "entryUUID is not unique\n", 0, 0, 0 );
3677 return LDAP_SUCCESS;
3681 nonpresent_callback(
3685 syncinfo_t *si = op->o_callback->sc_private;
3688 struct berval* present_uuid = NULL;
3689 struct nonpresent_entry *np_entry;
3691 if ( rs->sr_type == REP_RESULT ) {
3692 count = avl_free( si->si_presentlist, ch_free );
3693 si->si_presentlist = NULL;
3695 } else if ( rs->sr_type == REP_SEARCH ) {
3696 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
3697 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
3700 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
3704 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
3705 char buf[sizeof("rid=999 non")];
3707 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
3708 present_uuid ? "" : "non" );
3710 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
3711 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
3714 if ( a == NULL ) return 0;
3717 if ( present_uuid == NULL ) {
3718 np_entry = (struct nonpresent_entry *)
3719 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
3720 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
3721 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
3722 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
3725 avl_delete( &si->si_presentlist,
3726 &a->a_nvals[0], syncuuid_cmp );
3727 ch_free( present_uuid );
3730 return LDAP_SUCCESS;
3738 if ( rs->sr_err != LDAP_SUCCESS &&
3739 rs->sr_err != LDAP_REFERRAL &&
3740 rs->sr_err != LDAP_ALREADY_EXISTS &&
3741 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
3742 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
3744 Debug( LDAP_DEBUG_ANY,
3745 "null_callback : error code 0x%x\n",
3748 return LDAP_SUCCESS;
3751 static struct berval *
3752 slap_uuidstr_from_normalized(
3753 struct berval* uuidstr,
3754 struct berval* normalized,
3759 unsigned char nibble;
3762 if ( normalized == NULL ) return NULL;
3763 if ( normalized->bv_len != 16 ) return NULL;
3768 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3769 if ( new == NULL ) {
3776 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3777 if ( new != uuidstr ) {
3778 slap_sl_free( new, ctx );
3783 for ( i = 0; i < 16; i++ ) {
3784 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
3785 new->bv_val[(i<<1)+d] = '-';
3789 nibble = (normalized->bv_val[i] >> 4) & 0xF;
3790 if ( nibble < 10 ) {
3791 new->bv_val[(i<<1)+d] = nibble + '0';
3793 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
3796 nibble = (normalized->bv_val[i]) & 0xF;
3797 if ( nibble < 10 ) {
3798 new->bv_val[(i<<1)+d+1] = nibble + '0';
3800 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
3804 new->bv_val[new->bv_len] = '\0';
3811 if ( normalized == NULL ) return NULL;
3812 if ( normalized->bv_len != 16 ) return NULL;
3818 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3819 if ( new == NULL ) {
3826 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3831 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
3832 normalized->bv_len, new->bv_val, new->bv_len + 1 );
3836 if ( new != NULL ) {
3837 if ( new->bv_val != NULL ) {
3838 slap_sl_free( new->bv_val, ctx );
3841 if ( new != uuidstr ) {
3842 slap_sl_free( new, ctx );
3855 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
3857 const struct berval *uuid1 = v_uuid1;
3858 const struct berval *uuid2 = v_uuid2;
3859 int rc = uuid1->bv_len - uuid2->bv_len;
3860 if ( rc ) return rc;
3861 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
3865 syncinfo_free( syncinfo_t *sie, int free_all )
3867 syncinfo_t *si_next;
3869 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
3870 sie->si_ridtxt, 0, 0 );
3873 si_next = sie->si_next;
3876 if ( sie->si_conn ) {
3877 connection_client_stop( sie->si_conn );
3878 sie->si_conn = NULL;
3880 ldap_unbind_ext( sie->si_ld, NULL, NULL );
3884 struct re_s *re = sie->si_re;
3887 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3888 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
3889 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3890 ldap_pvt_runqueue_remove( &slapd_rq, re );
3891 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3894 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
3896 bindconf_free( &sie->si_bindconf );
3898 if ( sie->si_filterstr.bv_val ) {
3899 ch_free( sie->si_filterstr.bv_val );
3901 if ( sie->si_filter ) {
3902 filter_free( sie->si_filter );
3904 if ( sie->si_logfilterstr.bv_val ) {
3905 ch_free( sie->si_logfilterstr.bv_val );
3907 if ( sie->si_base.bv_val ) {
3908 ch_free( sie->si_base.bv_val );
3910 if ( sie->si_logbase.bv_val ) {
3911 ch_free( sie->si_logbase.bv_val );
3913 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
3914 ch_free( sie->si_contextdn.bv_val );
3916 if ( sie->si_attrs ) {
3918 while ( sie->si_attrs[i] != NULL ) {
3919 ch_free( sie->si_attrs[i] );
3922 ch_free( sie->si_attrs );
3924 if ( sie->si_exattrs ) {
3926 while ( sie->si_exattrs[i] != NULL ) {
3927 ch_free( sie->si_exattrs[i] );
3930 ch_free( sie->si_exattrs );
3932 if ( sie->si_anlist ) {
3934 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
3935 ch_free( sie->si_anlist[i].an_name.bv_val );
3938 ch_free( sie->si_anlist );
3940 if ( sie->si_exanlist ) {
3942 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
3943 ch_free( sie->si_exanlist[i].an_name.bv_val );
3946 ch_free( sie->si_exanlist );
3948 if ( sie->si_retryinterval ) {
3949 ch_free( sie->si_retryinterval );
3951 if ( sie->si_retrynum ) {
3952 ch_free( sie->si_retrynum );
3954 if ( sie->si_retrynum_init ) {
3955 ch_free( sie->si_retrynum_init );
3957 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
3958 if ( sie->si_presentlist ) {
3959 avl_free( sie->si_presentlist, ch_free );
3961 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
3962 struct nonpresent_entry* npe;
3963 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
3964 LDAP_LIST_REMOVE( npe, npe_link );
3965 if ( npe->npe_name ) {
3966 if ( npe->npe_name->bv_val ) {
3967 ch_free( npe->npe_name->bv_val );
3969 ch_free( npe->npe_name );
3971 if ( npe->npe_nname ) {
3972 if ( npe->npe_nname->bv_val ) {
3973 ch_free( npe->npe_nname->bv_val );
3975 ch_free( npe->npe_nname );
3979 if ( sie->si_cookieState ) {
3980 sie->si_cookieState->cs_ref--;
3981 if ( !sie->si_cookieState->cs_ref ) {
3982 ch_free( sie->si_cookieState->cs_sids );
3983 ber_bvarray_free( sie->si_cookieState->cs_vals );
3984 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
3985 ch_free( sie->si_cookieState->cs_psids );
3986 ber_bvarray_free( sie->si_cookieState->cs_pvals );
3987 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
3988 ch_free( sie->si_cookieState );
3993 } while ( free_all && si_next );
3996 #ifdef ENABLE_REWRITE
3998 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4000 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4001 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4002 char *argvRule[] = { "rewriteRule", NULL, si->si_suffixm.bv_val, ":", NULL };
4006 if ( si->si_rewrite )
4007 rewrite_info_delete( &si->si_rewrite );
4008 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4010 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4011 if ( rc != LDAP_SUCCESS )
4014 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4015 if ( rc != LDAP_SUCCESS )
4018 vnc = ch_malloc( si->si_base.bv_len + 2 );
4019 lutil_strcopy( lutil_strcopy( vnc, si->si_base.bv_val ), "$" );
4022 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4028 /* NOTE: used & documented in slapd.conf(5) */
4030 #define PROVIDERSTR "provider"
4031 #define SCHEMASTR "schemachecking"
4032 #define FILTERSTR "filter"
4033 #define SEARCHBASESTR "searchbase"
4034 #define SCOPESTR "scope"
4035 #define ATTRSONLYSTR "attrsonly"
4036 #define ATTRSSTR "attrs"
4037 #define TYPESTR "type"
4038 #define INTERVALSTR "interval"
4039 #define RETRYSTR "retry"
4040 #define SLIMITSTR "sizelimit"
4041 #define TLIMITSTR "timelimit"
4042 #define SYNCDATASTR "syncdata"
4043 #define LOGBASESTR "logbase"
4044 #define LOGFILTERSTR "logfilter"
4045 #define SUFFIXMSTR "suffixmassage"
4047 /* FIXME: undocumented */
4048 #define EXATTRSSTR "exattrs"
4049 #define MANAGEDSAITSTR "manageDSAit"
4053 GOT_RID = 0x00000001U,
4054 GOT_PROVIDER = 0x00000002U,
4055 GOT_SCHEMACHECKING = 0x00000004U,
4056 GOT_FILTER = 0x00000008U,
4057 GOT_SEARCHBASE = 0x00000010U,
4058 GOT_SCOPE = 0x00000020U,
4059 GOT_ATTRSONLY = 0x00000040U,
4060 GOT_ATTRS = 0x00000080U,
4061 GOT_TYPE = 0x00000100U,
4062 GOT_INTERVAL = 0x00000200U,
4063 GOT_RETRY = 0x00000400U,
4064 GOT_SLIMIT = 0x00000800U,
4065 GOT_TLIMIT = 0x00001000U,
4066 GOT_SYNCDATA = 0x00002000U,
4067 GOT_LOGBASE = 0x00004000U,
4068 GOT_LOGFILTER = 0x00008000U,
4069 GOT_EXATTRS = 0x00010000U,
4070 GOT_MANAGEDSAIT = 0x00020000U,
4071 GOT_BINDCONF = 0x00040000U,
4072 GOT_SUFFIXM = 0x00080000U,
4075 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4078 static slap_verbmasks datamodes[] = {
4079 { BER_BVC("default"), SYNCDATA_DEFAULT },
4080 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4081 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4086 parse_syncrepl_retry(
4093 int use_default = 0;
4095 char *val = arg + STRLENOF( RETRYSTR "=" );
4096 if ( strcasecmp( val, "undefined" ) == 0 ) {
4101 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4102 retry_list[0] = NULL;
4104 slap_str2clist( &retry_list, val, " ,\t" );
4106 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4109 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4110 "Error: incomplete syncrepl retry list" );
4111 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4112 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4113 ch_free( retry_list[k] );
4115 ch_free( retry_list );
4118 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4119 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4120 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4121 for ( j = 0; j < n; j++ ) {
4123 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4124 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4125 "Error: invalid retry interval \"%s\" (#%d)",
4126 retry_list[j*2], j );
4127 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4128 /* do some cleanup */
4131 si->si_retryinterval[j] = (time_t)t;
4132 if ( *retry_list[j*2+1] == '+' ) {
4133 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4134 si->si_retrynum[j] = RETRYNUM_FOREVER;
4138 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4139 || si->si_retrynum_init[j] <= 0 )
4141 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4142 "Error: invalid initial retry number \"%s\" (#%d)",
4143 retry_list[j*2+1], j );
4144 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4145 /* do some cleanup */
4148 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4149 || si->si_retrynum[j] <= 0 )
4151 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4152 "Error: invalid retry number \"%s\" (#%d)",
4153 retry_list[j*2+1], j );
4154 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4155 /* do some cleanup */
4160 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4161 Debug( LDAP_DEBUG_CONFIG,
4162 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4166 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4167 si->si_retrynum[j] = RETRYNUM_TAIL;
4168 si->si_retryinterval[j] = 0;
4170 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4171 ch_free( retry_list[k] );
4173 ch_free( retry_list );
4174 if ( !use_default ) {
4175 si->si_got |= GOT_RETRY;
4182 parse_syncrepl_line(
4189 for ( i = 1; i < c->argc; i++ ) {
4190 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4191 STRLENOF( IDSTR "=" ) ) )
4194 /* '\0' string terminator accounts for '=' */
4195 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4196 if ( lutil_atoi( &tmp, val ) != 0 ) {
4197 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4198 "Error: parse_syncrepl_line: "
4199 "unable to parse syncrepl id \"%s\"", val );
4200 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4203 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4204 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4205 "Error: parse_syncrepl_line: "
4206 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4207 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4211 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4212 si->si_got |= GOT_RID;
4213 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4214 STRLENOF( PROVIDERSTR "=" ) ) )
4216 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4217 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4219 if ( ldap_is_ldaps_url( val ))
4220 si->si_bindconf.sb_tls_do_init = 1;
4222 si->si_got |= GOT_PROVIDER;
4223 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4224 STRLENOF( SCHEMASTR "=" ) ) )
4226 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4227 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4228 si->si_schemachecking = 1;
4229 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4230 si->si_schemachecking = 0;
4232 si->si_schemachecking = 1;
4234 si->si_got |= GOT_SCHEMACHECKING;
4235 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4236 STRLENOF( FILTERSTR "=" ) ) )
4238 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4239 if ( si->si_filterstr.bv_val )
4240 ch_free( si->si_filterstr.bv_val );
4241 ber_str2bv( val, 0, 1, &si->si_filterstr );
4242 si->si_got |= GOT_FILTER;
4243 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4244 STRLENOF( LOGFILTERSTR "=" ) ) )
4246 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4247 if ( si->si_logfilterstr.bv_val )
4248 ch_free( si->si_logfilterstr.bv_val );
4249 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4250 si->si_got |= GOT_LOGFILTER;
4251 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4252 STRLENOF( SEARCHBASESTR "=" ) ) )
4257 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4258 if ( si->si_base.bv_val ) {
4259 ch_free( si->si_base.bv_val );
4261 ber_str2bv( val, 0, 0, &bv );
4262 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4263 if ( rc != LDAP_SUCCESS ) {
4264 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4265 "Invalid base DN \"%s\": %d (%s)",
4266 val, rc, ldap_err2string( rc ) );
4267 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4270 si->si_got |= GOT_SEARCHBASE;
4271 #ifdef ENABLE_REWRITE
4272 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
4273 STRLENOF( SUFFIXMSTR "=" ) ) )
4278 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
4279 if ( si->si_suffixm.bv_val ) {
4280 ch_free( si->si_suffixm.bv_val );
4282 ber_str2bv( val, 0, 0, &bv );
4283 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
4284 if ( rc != LDAP_SUCCESS ) {
4285 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4286 "Invalid massage DN \"%s\": %d (%s)",
4287 val, rc, ldap_err2string( rc ) );
4288 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4291 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
4292 ch_free( si->si_suffixm.bv_val );
4293 BER_BVZERO( &si->si_suffixm );
4294 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4295 "Massage DN \"%s\" is not within the database naming context",
4297 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4300 si->si_got |= GOT_SUFFIXM;
4302 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4303 STRLENOF( LOGBASESTR "=" ) ) )
4308 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
4309 if ( si->si_logbase.bv_val ) {
4310 ch_free( si->si_logbase.bv_val );
4312 ber_str2bv( val, 0, 0, &bv );
4313 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
4314 if ( rc != LDAP_SUCCESS ) {
4315 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4316 "Invalid logbase DN \"%s\": %d (%s)",
4317 val, rc, ldap_err2string( rc ) );
4318 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4321 si->si_got |= GOT_LOGBASE;
4322 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
4323 STRLENOF( SCOPESTR "=" ) ) )
4326 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
4327 j = ldap_pvt_str2scope( val );
4329 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4330 "Error: parse_syncrepl_line: "
4331 "unknown scope \"%s\"", val);
4332 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4336 si->si_got |= GOT_SCOPE;
4337 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
4338 STRLENOF( ATTRSONLYSTR ) ) )
4340 si->si_attrsonly = 1;
4341 si->si_got |= GOT_ATTRSONLY;
4342 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
4343 STRLENOF( ATTRSSTR "=" ) ) )
4345 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
4346 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4348 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4349 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
4350 if ( si->si_anlist == NULL ) {
4351 ch_free( attr_fname );
4354 si->si_anfile = attr_fname;
4356 char *str, *s, *next;
4357 const char *delimstr = " ,\t";
4358 str = ch_strdup( val );
4359 for ( s = ldap_pvt_strtok( str, delimstr, &next );
4361 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
4363 if ( strlen(s) == 1 && *s == '*' ) {
4364 si->si_allattrs = 1;
4365 val[ s - str ] = delimstr[0];
4367 if ( strlen(s) == 1 && *s == '+' ) {
4368 si->si_allopattrs = 1;
4369 val [ s - str ] = delimstr[0];
4373 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
4374 if ( si->si_anlist == NULL ) {
4378 si->si_got |= GOT_ATTRS;
4379 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
4380 STRLENOF( EXATTRSSTR "=" ) ) )
4382 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
4383 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4385 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4386 si->si_exanlist = file2anlist(
4387 si->si_exanlist, attr_fname, " ,\t" );
4388 if ( si->si_exanlist == NULL ) {
4389 ch_free( attr_fname );
4392 ch_free( attr_fname );
4394 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
4395 if ( si->si_exanlist == NULL ) {
4399 si->si_got |= GOT_EXATTRS;
4400 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
4401 STRLENOF( TYPESTR "=" ) ) )
4403 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
4404 if ( !strncasecmp( val, "refreshOnly",
4405 STRLENOF("refreshOnly") ) )
4407 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4408 } else if ( !strncasecmp( val, "refreshAndPersist",
4409 STRLENOF("refreshAndPersist") ) )
4411 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
4412 si->si_interval = 60;
4414 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4415 "Error: parse_syncrepl_line: "
4416 "unknown sync type \"%s\"", val);
4417 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4420 si->si_got |= GOT_TYPE;
4421 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
4422 STRLENOF( INTERVALSTR "=" ) ) )
4424 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
4425 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
4426 si->si_interval = 0;
4427 } else if ( strchr( val, ':' ) != NULL ) {
4428 char *next, *ptr = val;
4431 dd = strtol( ptr, &next, 10 );
4432 if ( next == ptr || next[0] != ':' || dd < 0 ) {
4433 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4434 "Error: parse_syncrepl_line: "
4435 "invalid interval \"%s\", unable to parse days", val );
4436 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4440 hh = strtol( ptr, &next, 10 );
4441 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
4442 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4443 "Error: parse_syncrepl_line: "
4444 "invalid interval \"%s\", unable to parse hours", val );
4445 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4449 mm = strtol( ptr, &next, 10 );
4450 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
4451 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4452 "Error: parse_syncrepl_line: "
4453 "invalid interval \"%s\", unable to parse minutes", val );
4454 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4458 ss = strtol( ptr, &next, 10 );
4459 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
4460 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4461 "Error: parse_syncrepl_line: "
4462 "invalid interval \"%s\", unable to parse seconds", val );
4463 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4466 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
4470 if ( lutil_parse_time( val, &t ) != 0 ) {
4471 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4472 "Error: parse_syncrepl_line: "
4473 "invalid interval \"%s\"", val );
4474 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4477 si->si_interval = (time_t)t;
4479 if ( si->si_interval < 0 ) {
4480 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4481 "Error: parse_syncrepl_line: "
4482 "invalid interval \"%ld\"",
4483 (long) si->si_interval);
4484 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4487 si->si_got |= GOT_INTERVAL;
4488 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
4489 STRLENOF( RETRYSTR "=" ) ) )
4491 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
4494 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
4495 STRLENOF( MANAGEDSAITSTR "=" ) ) )
4497 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
4498 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
4499 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
4501 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4502 "invalid manageDSAit value \"%s\".\n",
4504 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4507 si->si_got |= GOT_MANAGEDSAIT;
4508 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
4509 STRLENOF( SLIMITSTR "=") ) )
4511 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
4512 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4515 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
4516 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4517 "invalid size limit value \"%s\".\n",
4519 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4522 si->si_got |= GOT_SLIMIT;
4523 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
4524 STRLENOF( TLIMITSTR "=" ) ) )
4526 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
4527 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4530 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
4531 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4532 "invalid time limit value \"%s\".\n",
4534 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4537 si->si_got |= GOT_TLIMIT;
4538 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
4539 STRLENOF( SYNCDATASTR "=" ) ) )
4541 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
4542 si->si_syncdata = verb_to_mask( val, datamodes );
4543 si->si_got |= GOT_SYNCDATA;
4544 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
4545 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4546 "Error: parse_syncrepl_line: "
4547 "unable to parse \"%s\"\n", c->argv[ i ] );
4548 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4551 si->si_got |= GOT_BINDCONF;
4554 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
4555 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4556 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
4557 si->si_got & GOT_RID ? "" : " "IDSTR,
4558 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
4559 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
4560 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4564 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
4565 ch_free( si->si_base.bv_val );
4566 BER_BVZERO( &si->si_base );
4567 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4568 "Base DN \"%s\" is not within the database naming context",
4570 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4574 #ifdef ENABLE_REWRITE
4575 if ( si->si_got & GOT_SUFFIXM ) {
4576 if (config_suffixm( c, si )) {
4577 ch_free( si->si_suffixm.bv_val );
4578 BER_BVZERO( &si->si_suffixm );
4579 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4580 "Error configuring rewrite engine" );
4581 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4587 if ( !( si->si_got & GOT_RETRY ) ) {
4588 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
4589 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
4590 if ( si->si_retryinterval == NULL ) {
4591 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
4597 si->si_filter = str2filter( si->si_filterstr.bv_val );
4598 if ( si->si_filter == NULL ) {
4599 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
4600 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
4614 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
4615 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
4616 "operations required for syncrepl", c->be->be_type );
4617 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4620 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
4621 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
4622 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4625 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
4628 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
4632 si->si_bindconf.sb_tls = SB_TLS_OFF;
4633 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
4634 si->si_schemachecking = 0;
4635 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
4636 &si->si_filterstr );
4637 si->si_base.bv_val = NULL;
4638 si->si_scope = LDAP_SCOPE_SUBTREE;
4639 si->si_attrsonly = 0;
4640 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4641 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4642 si->si_attrs = NULL;
4643 si->si_allattrs = 0;
4644 si->si_allopattrs = 0;
4645 si->si_exattrs = NULL;
4646 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4647 si->si_interval = 86400;
4648 si->si_retryinterval = NULL;
4649 si->si_retrynum_init = NULL;
4650 si->si_retrynum = NULL;
4651 si->si_manageDSAit = 0;
4655 si->si_presentlist = NULL;
4656 LDAP_LIST_INIT( &si->si_nonpresentlist );
4657 ldap_pvt_thread_mutex_init( &si->si_mutex );
4659 rc = parse_syncrepl_line( c, si );
4664 /* Must be LDAPv3 because we need controls */
4665 switch ( si->si_bindconf.sb_version ) {
4667 /* not explicitly set */
4668 si->si_bindconf.sb_version = LDAP_VERSION3;
4671 /* explicitly set */
4674 Debug( LDAP_DEBUG_ANY,
4675 "version %d incompatible with syncrepl\n",
4676 si->si_bindconf.sb_version, 0, 0 );
4677 syncinfo_free( si, 0 );
4681 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
4682 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4683 "<%s> invalid URL", c->argv[0] );
4684 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
4685 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
4690 if ( slapMode & SLAP_SERVER_MODE ) {
4692 /* check if consumer points to current server and database.
4693 * If so, ignore this configuration.
4695 if ( !SLAP_DBHIDDEN( c->be ) ) {
4697 /* if searchbase doesn't match current DB suffix,
4698 * assume it's different
4700 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
4701 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
4706 /* if searchbase matches, see if URLs match */
4707 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
4713 init_syncrepl( si );
4714 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4715 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
4716 si->si_interval, do_syncrepl, si, "do_syncrepl",
4718 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4720 rc = config_sync_shadow( c ) ? -1 : 0;
4725 /* mirrormode still needs to see this flag in tool mode */
4726 rc = config_sync_shadow( c ) ? -1 : 0;
4728 ldap_free_urldesc( lud );
4732 /* Use main slapd defaults */
4733 bindconf_tls_defaults( &si->si_bindconf );
4736 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
4737 syncinfo_free( si, 0 );
4740 Debug( LDAP_DEBUG_CONFIG,
4741 "Config: ** successfully added syncrepl %s \"%s\"\n",
4743 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
4744 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
4745 if ( c->be->be_syncinfo ) {
4748 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
4750 /* add new syncrepl to end of list (same order as when deleting) */
4751 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
4754 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
4755 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
4756 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
4758 c->be->be_syncinfo = si;
4760 si->si_cookieState->cs_ref++;
4769 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
4771 struct berval bc, uri, bs;
4772 char buf[BUFSIZ*2], *ptr;
4775 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
4779 /* temporarily inhibit bindconf from printing URI */
4780 uri = si->si_bindconf.sb_uri;
4781 BER_BVZERO( &si->si_bindconf.sb_uri );
4782 si->si_bindconf.sb_version = 0;
4783 bindconf_unparse( &si->si_bindconf, &bc );
4784 si->si_bindconf.sb_uri = uri;
4785 si->si_bindconf.sb_version = LDAP_VERSION3;
4788 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
4789 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
4790 si->si_rid, si->si_bindconf.sb_uri.bv_val );
4791 if ( len >= sizeof( buf ) ) return;
4793 if ( !BER_BVISNULL( &bc ) ) {
4794 if ( WHATSLEFT <= bc.bv_len ) {
4798 ptr = lutil_strcopy( ptr, bc.bv_val );
4801 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
4802 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
4803 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
4804 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
4807 if ( !BER_BVISNULL( &si->si_base ) ) {
4808 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
4809 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
4810 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
4813 #ifdef ENABLE_REWRITE
4814 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
4815 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
4816 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
4817 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
4821 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
4822 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
4823 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
4824 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
4827 if ( !BER_BVISNULL( &si->si_logbase ) ) {
4828 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
4829 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
4830 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
4833 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
4834 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
4835 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
4836 ptr = lutil_strcopy( ptr, bs.bv_val );
4838 if ( si->si_attrsonly ) {
4839 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4840 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
4842 if ( si->si_anfile ) {
4843 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
4844 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
4845 ptr = lutil_strcopy( ptr, si->si_anfile );
4847 } else if ( si->si_allattrs || si->si_allopattrs ||
4848 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
4852 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4853 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
4855 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
4856 if ( ptr == NULL ) return;
4857 if ( si->si_allattrs ) {
4858 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
4859 if ( old != ptr ) *ptr++ = ',';
4862 if ( si->si_allopattrs ) {
4863 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
4864 if ( old != ptr ) *ptr++ = ',';
4869 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
4870 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
4871 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
4872 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
4873 if ( ptr == NULL ) return;
4875 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
4876 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
4877 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
4879 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
4880 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
4881 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
4882 "refreshAndPersist" : "refreshOnly" );
4884 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
4887 dd = si->si_interval;
4894 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
4895 INTERVALSTR, dd, hh, mm, ss );
4896 if ( len >= WHATSLEFT ) return;
4900 if ( si->si_got & GOT_RETRY ) {
4901 const char *space = "";
4902 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
4903 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
4904 for (i=0; si->si_retryinterval[i]; i++) {
4905 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
4906 (long) si->si_retryinterval[i] );
4908 if ( WHATSLEFT - 1 <= len ) return;
4910 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
4913 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
4914 if ( WHATSLEFT <= len ) return;
4918 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
4921 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
4924 if ( si->si_slimit ) {
4925 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
4926 if ( WHATSLEFT <= len ) return;
4930 if ( si->si_tlimit ) {
4931 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
4932 if ( WHATSLEFT <= len ) return;
4936 if ( si->si_syncdata ) {
4937 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
4938 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
4939 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
4940 ptr = lutil_strcopy( ptr, bc.bv_val );
4943 bc.bv_len = ptr - buf;
4945 ber_dupbv( bv, &bc );
4949 syncrepl_config( ConfigArgs *c )
4951 if (c->op == SLAP_CONFIG_EMIT) {
4952 if ( c->be->be_syncinfo ) {
4956 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
4957 syncrepl_unparse( si, &bv );
4958 ber_bvarray_add( &c->rvalue_vals, &bv );
4963 } else if ( c->op == LDAP_MOD_DELETE ) {
4965 if ( c->be->be_syncinfo ) {
4966 syncinfo_t *si, **sip;
4969 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
4971 if ( c->valx == -1 || i == c->valx ) {
4975 /* If the task is currently active, we have to leave
4976 * it running. It will exit on its own. This will only
4977 * happen when running on the cn=config DB.
4980 if ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
4983 /* There is no active thread, but we must still
4984 * ensure that no thread is (or will be) queued
4985 * while we removes the task.
4987 struct re_s *re = si->si_re;
4990 if ( si->si_conn ) {
4991 connection_client_stop( si->si_conn );
4995 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4996 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
4997 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5000 ldap_pvt_runqueue_remove( &slapd_rq, re );
5001 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5003 if ( ldap_pvt_thread_pool_retract( &connection_pool,
5004 re->routine, re ) > 0 )
5007 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5011 syncinfo_free( si, 0 );
5020 if ( !c->be->be_syncinfo ) {
5021 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5025 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5026 Debug(LDAP_DEBUG_ANY, "%s: "
5027 "syncrepl: database already shadowed.\n",
5031 return add_syncrepl( c );