1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2011 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003-2008 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
28 #include "lutil_ldap.h"
36 #define SUFFIXM_CTX "<suffix massage>"
39 struct nonpresent_entry {
40 struct berval *npe_name;
41 struct berval *npe_nname;
42 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
45 typedef struct cookie_state {
46 ldap_pvt_thread_mutex_t cs_mutex;
47 struct berval *cs_vals;
53 /* pending changes, not yet committed */
54 ldap_pvt_thread_mutex_t cs_pmutex;
55 struct berval *cs_pvals;
60 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
61 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
62 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
64 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
65 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
67 #define RETRYNUM_FOREVER (-1) /* retry forever */
68 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
69 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
70 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
72 typedef struct syncinfo_s {
73 struct syncinfo_s *si_next;
78 char si_ridtxt[ STRLENOF("rid=999") + 1 ];
79 slap_bindconf si_bindconf;
80 struct berval si_base;
81 struct berval si_logbase;
82 struct berval si_filterstr;
84 struct berval si_logfilterstr;
85 struct berval si_contextdn;
89 AttributeName *si_anlist;
90 AttributeName *si_exanlist;
95 int si_schemachecking;
96 int si_type; /* the active type */
97 int si_ctype; /* the configured type */
99 time_t *si_retryinterval;
100 int *si_retrynum_init;
102 struct sync_cookie si_syncCookie;
103 cookie_state *si_cookieState;
108 int si_refreshDelete;
109 int si_refreshPresent;
114 int si_strict_refresh; /* stop listening during fallback refresh */
116 Avlnode *si_presentlist;
119 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
120 #ifdef ENABLE_REWRITE
121 struct rewrite_info *si_rewrite;
122 struct berval si_suffixm;
124 ldap_pvt_thread_mutex_t si_mutex;
127 static int syncuuid_cmp( const void *, const void * );
128 static int avl_presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
129 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
130 static int syncrepl_message_to_op(
131 syncinfo_t *, Operation *, LDAPMessage * );
132 static int syncrepl_message_to_entry(
133 syncinfo_t *, Operation *, LDAPMessage *,
134 Modifications **, Entry **, int );
135 static int syncrepl_entry(
136 syncinfo_t *, Operation*, Entry*,
137 Modifications**,int, struct berval*,
138 struct berval *cookieCSN );
139 static int syncrepl_updateCookie(
140 syncinfo_t *, Operation *,
141 struct sync_cookie * );
142 static struct berval * slap_uuidstr_from_normalized(
143 struct berval *, struct berval *, void * );
144 static int syncrepl_add_glue_ancestors(
145 Operation* op, Entry *e );
147 /* delta-mmr overlay handler */
148 static int syncrepl_op_modify( Operation *op, SlapReply *rs );
150 /* callback functions */
151 static int dn_callback( Operation *, SlapReply * );
152 static int nonpresent_callback( Operation *, SlapReply * );
153 static int null_callback( Operation *, SlapReply * );
155 static AttributeDescription *sync_descs[4];
158 static AttributeDescription *ad_reqMod, *ad_reqDN;
160 typedef struct logschema {
162 struct berval ls_req;
163 struct berval ls_mod;
164 struct berval ls_newRdn;
165 struct berval ls_delRdn;
166 struct berval ls_newSup;
169 static logschema changelog_sc = {
171 BER_BVC("changeType"),
174 BER_BVC("deleteOldRDN"),
175 BER_BVC("newSuperior")
178 static logschema accesslog_sc = {
182 BER_BVC("reqNewRDN"),
183 BER_BVC("reqDeleteOldRDN"),
184 BER_BVC("reqNewSuperior")
188 syncrepl_state2str( int state )
191 case LDAP_SYNC_PRESENT:
197 case LDAP_SYNC_MODIFY:
200 case LDAP_SYNC_DELETE:
207 static slap_overinst syncrepl_ov;
210 init_syncrepl(syncinfo_t *si)
213 char **attrs, **exattrs;
215 if ( !syncrepl_ov.on_bi.bi_type ) {
216 syncrepl_ov.on_bi.bi_type = "syncrepl";
217 syncrepl_ov.on_bi.bi_op_modify = syncrepl_op_modify;
218 overlay_register( &syncrepl_ov );
221 /* delta-MMR needs the overlay, nothing else does.
222 * This must happen before accesslog overlay is configured.
224 if ( si->si_syncdata &&
225 !overlay_is_inst( si->si_be, syncrepl_ov.on_bi.bi_type )) {
226 overlay_config( si->si_be, syncrepl_ov.on_bi.bi_type, -1, NULL, NULL );
229 logschema *ls = &accesslog_sc;
231 slap_bv2ad( &ls->ls_mod, &ad_reqMod, &text );
232 slap_bv2ad( &ls->ls_dn, &ad_reqDN, &text );
236 if ( !sync_descs[0] ) {
237 sync_descs[0] = slap_schema.si_ad_objectClass;
238 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
239 sync_descs[2] = slap_schema.si_ad_entryCSN;
240 sync_descs[3] = NULL;
243 if ( si->si_allattrs && si->si_allopattrs )
246 attrs = anlist2attrs( si->si_anlist );
249 if ( si->si_allattrs ) {
252 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
253 for ( j = i; attrs[j] != NULL; j++ ) {
256 attrs[j] = attrs[j+1];
262 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
263 attrs[i] = ch_strdup("*");
266 } else if ( si->si_allopattrs ) {
269 if ( is_at_operational( at_find( attrs[i] ) ) ) {
270 for ( j = i; attrs[j] != NULL; j++ ) {
273 attrs[j] = attrs[j+1];
279 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
280 attrs[i] = ch_strdup("+");
284 for ( i = 0; sync_descs[i] != NULL; i++ ) {
287 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
288 for ( k = j; attrs[k] != NULL; k++ ) {
291 attrs[k] = attrs[k+1];
299 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
301 if ( si->si_allopattrs ) {
302 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
304 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
308 if ( si->si_allopattrs ) {
309 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
311 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
312 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
320 if ( si->si_allattrs == si->si_allopattrs ) {
321 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
322 attrs[i++] = ch_strdup( "*" );
323 attrs[i++] = ch_strdup( "+" );
324 } else if ( si->si_allattrs && !si->si_allopattrs ) {
325 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
326 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
327 attrs[i++] = ch_strdup( "*" );
328 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
329 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
331 } else if ( !si->si_allattrs && si->si_allopattrs ) {
332 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
333 attrs[i++] = ch_strdup( "+" );
334 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
339 si->si_attrs = attrs;
341 exattrs = anlist2attrs( si->si_exanlist );
344 for ( n = 0; exattrs[n] != NULL; n++ ) ;
346 for ( i = 0; sync_descs[i] != NULL; i++ ) {
348 while ( exattrs[j] != NULL ) {
349 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
350 ch_free( exattrs[j] );
351 for ( k = j; exattrs[k] != NULL; k++ ) {
352 exattrs[k] = exattrs[k+1];
360 for ( i = 0; exattrs[i] != NULL; i++ ) {
361 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
363 if ( ( oc = si->si_anlist[j].an_oc ) ) {
365 while ( oc->soc_required[k] ) {
366 if ( !strcmp( exattrs[i],
367 oc->soc_required[k]->sat_cname.bv_val ) ) {
368 ch_free( exattrs[i] );
369 for ( l = i; exattrs[l]; l++ ) {
370 exattrs[l] = exattrs[l+1];
380 for ( i = 0; exattrs[i] != NULL; i++ ) ;
383 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
386 si->si_exattrs = exattrs;
394 BerElementBuffer berbuf;
395 BerElement *ber = (BerElement *)&berbuf;
396 LDAPControl c[3], *ctrls[4];
400 char **attrs, *lattrs[8];
405 /* setup LDAP SYNC control */
406 ber_init2( ber, NULL, LBER_USE_DER );
407 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
409 /* If we're using a log but we have no state, then fallback to
410 * normal mode for a full refresh.
412 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
413 si->si_logstate = SYNCLOG_FALLBACK;
416 /* Use the log parameters if we're in log mode */
417 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
419 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
423 lattrs[0] = ls->ls_dn.bv_val;
424 lattrs[1] = ls->ls_req.bv_val;
425 lattrs[2] = ls->ls_mod.bv_val;
426 lattrs[3] = ls->ls_newRdn.bv_val;
427 lattrs[4] = ls->ls_delRdn.bv_val;
428 lattrs[5] = ls->ls_newSup.bv_val;
429 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
433 base = si->si_logbase.bv_val;
434 filter = si->si_logfilterstr.bv_val;
437 scope = LDAP_SCOPE_SUBTREE;
440 base = si->si_base.bv_val;
441 filter = si->si_filterstr.bv_val;
442 attrs = si->si_attrs;
443 attrsonly = si->si_attrsonly;
444 scope = si->si_scope;
446 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
447 si->si_type = LDAP_SYNC_REFRESH_ONLY;
449 si->si_type = si->si_ctype;
452 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
454 ber_printf( ber, "{eOb}",
455 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
457 ber_printf( ber, "{eb}",
458 abs(si->si_type), rhint );
461 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == -1 ) {
466 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
467 c[0].ldctl_iscritical = si->si_type < 0;
470 c[1].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
471 BER_BVZERO( &c[1].ldctl_value );
472 c[1].ldctl_iscritical = 1;
475 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
476 c[2].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
477 c[2].ldctl_value = si->si_bindconf.sb_authzId;
478 c[2].ldctl_iscritical = 1;
485 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
486 ctrls, NULL, NULL, si->si_slimit, &si->si_msgid );
499 SlapReply rs = {REP_SEARCH};
500 int i, j, changed = 0;
502 /* Look for contextCSN from syncprov overlay. If
503 * there's no overlay, this will be a no-op. That means
504 * this is a pure consumer, so local changes will not be
505 * allowed, and all changes will already be reflected in
508 a.a_desc = slap_schema.si_ad_contextCSN;
510 e.e_name = si->si_contextdn;
511 e.e_nname = si->si_contextdn;
512 at[0].an_name = a.a_desc->ad_cname;
513 at[0].an_desc = a.a_desc;
514 BER_BVZERO( &at[1].an_name );
516 rs.sr_flags = REP_ENTRY_MODIFIABLE;
518 op->o_req_dn = e.e_name;
519 op->o_req_ndn = e.e_nname;
521 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
522 i = backend_operational( op, &rs );
523 if ( i == LDAP_SUCCESS && a.a_nvals ) {
524 int num = a.a_numvals;
525 /* check for differences */
526 if ( num != si->si_cookieState->cs_num ) {
529 for ( i=0; i<num; i++ ) {
530 if ( ber_bvcmp( &a.a_nvals[i],
531 &si->si_cookieState->cs_vals[i] )) {
538 ber_bvarray_free( si->si_cookieState->cs_vals );
539 ch_free( si->si_cookieState->cs_sids );
540 si->si_cookieState->cs_num = num;
541 si->si_cookieState->cs_vals = a.a_nvals;
542 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_nvals,
544 si->si_cookieState->cs_age++;
546 ber_bvarray_free( a.a_nvals );
548 ber_bvarray_free( a.a_vals );
550 /* See if the cookieState has changed due to anything outside
551 * this particular consumer. That includes other consumers in
552 * the same context, or local changes detected above.
554 if ( si->si_cookieState->cs_num > 0 && si->si_cookieAge !=
555 si->si_cookieState->cs_age ) {
556 if ( !si->si_syncCookie.numcsns ) {
557 ber_bvarray_free( si->si_syncCookie.ctxcsn );
558 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
559 si->si_cookieState->cs_vals, NULL );
562 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
563 /* bogus, just dup everything */
564 if ( si->si_syncCookie.sids[i] == -1 ) {
565 ber_bvarray_free( si->si_syncCookie.ctxcsn );
566 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
567 si->si_cookieState->cs_vals, NULL );
571 for (j=0; j<si->si_cookieState->cs_num; j++) {
572 if ( si->si_syncCookie.sids[i] !=
573 si->si_cookieState->cs_sids[j] )
575 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
576 &si->si_cookieState->cs_vals[j] ))
578 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
579 &si->si_cookieState->cs_vals[j] );
587 si->si_cookieAge = si->si_cookieState->cs_age;
588 ch_free( si->si_syncCookie.octet_str.bv_val );
589 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
590 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
591 si->si_syncCookie.sid );
592 slap_parse_sync_cookie( &si->si_syncCookie, NULL );
594 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
604 int cmdline_cookie_found = 0;
606 struct sync_cookie *sc = NULL;
611 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
612 if ( rc != LDAP_SUCCESS ) {
615 op->o_protocol = LDAP_VERSION3;
617 /* Set SSF to strongest of TLS, SASL SSFs */
620 op->o_transport_ssf = 0;
622 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
623 == LDAP_SUCCESS && ssl != NULL )
625 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
627 #endif /* HAVE_TLS */
629 ber_len_t ssf; /* ITS#5403, 3864 LDAP_OPT_X_SASL_SSF probably ought
630 to use sasl_ssf_t but currently uses ber_len_t */
631 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &ssf )
633 op->o_sasl_ssf = ssf;
635 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
636 ? op->o_sasl_ssf : op->o_tls_ssf;
638 ldap_set_option( si->si_ld, LDAP_OPT_TIMELIMIT, &si->si_tlimit );
640 rc = LDAP_DEREF_NEVER; /* actually could allow DEREF_FINDING */
641 ldap_set_option( si->si_ld, LDAP_OPT_DEREF, &rc );
643 ldap_set_option( si->si_ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
645 si->si_syncCookie.rid = si->si_rid;
647 /* whenever there are multiple data sources possible, advertise sid */
648 si->si_syncCookie.sid = ( SLAP_MULTIMASTER( si->si_be ) || si->si_be != si->si_wbe ) ?
651 /* We've just started up, or the remote server hasn't sent us
652 * any meaningful state.
654 if ( !si->si_syncCookie.ctxcsn ) {
657 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
658 if ( si->si_rid == sc->rid ) {
659 cmdline_cookie_found = 1;
664 if ( cmdline_cookie_found ) {
665 /* cookie is supplied in the command line */
667 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
669 /* ctxcsn wasn't parsed yet, do it now */
670 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
671 slap_sync_cookie_free( &si->si_syncCookie, 0 );
672 slap_dup_sync_cookie( &si->si_syncCookie, sc );
673 slap_sync_cookie_free( sc, 1 );
675 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
676 if ( !si->si_cookieState->cs_num ) {
677 /* get contextCSN shadow replica from database */
678 BerVarray csn = NULL;
679 void *ctx = op->o_tmpmemctx;
681 op->o_req_ndn = si->si_contextdn;
682 op->o_req_dn = op->o_req_ndn;
684 /* try to read stored contextCSN */
685 op->o_tmpmemctx = NULL;
686 backend_attribute( op, NULL, &op->o_req_ndn,
687 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
688 op->o_tmpmemctx = ctx;
690 si->si_cookieState->cs_vals = csn;
691 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
692 si->si_cookieState->cs_num = i;
693 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
694 slap_sort_csn_sids( csn, si->si_cookieState->cs_sids, i, NULL );
697 if ( si->si_cookieState->cs_num ) {
698 ber_bvarray_free( si->si_syncCookie.ctxcsn );
699 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
700 si->si_cookieState->cs_vals, NULL )) {
702 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
705 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
706 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
708 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
709 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
711 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
714 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
715 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
716 si->si_syncCookie.sid );
718 /* ITS#6367: recreate the cookie so it has our SID, not our peer's */
719 ch_free( si->si_syncCookie.octet_str.bv_val );
720 BER_BVZERO( &si->si_syncCookie.octet_str );
721 /* Look for contextCSN from syncprov overlay. */
722 check_syncprov( op, si );
723 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
724 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
725 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
726 si->si_syncCookie.sid );
729 si->si_refreshDone = 0;
731 rc = ldap_sync_search( si, op->o_tmpmemctx );
733 if( rc != LDAP_SUCCESS ) {
734 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
735 "ldap_search_ext: %s (%d)\n",
736 si->si_ridtxt, ldap_err2string( rc ), rc );
742 ldap_unbind_ext( si->si_ld, NULL, NULL );
751 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
758 if ( sc1->numcsns < sc2->numcsns ) {
759 *which = sc1->numcsns;
763 for (j=0; j<sc2->numcsns; j++) {
764 for (i=0; i<sc1->numcsns; i++) {
765 if ( sc1->sids[i] != sc2->sids[j] )
767 value_match( &match, slap_schema.si_ad_entryCSN,
768 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
769 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
770 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
777 if ( i == sc1->numcsns ) {
778 /* sc2 has a sid sc1 lacks */
786 #define SYNC_PAUSED -3
793 BerElementBuffer berbuf;
794 BerElement *ber = (BerElement *)&berbuf;
796 LDAPMessage *msg = NULL;
798 struct sync_cookie syncCookie = { NULL };
799 struct sync_cookie syncCookie_req = { NULL };
804 Modifications *modlist = NULL;
808 struct timeval *tout_p = NULL;
809 struct timeval tout = { 0, 0 };
811 int refreshDeletes = 0;
812 char empty[6] = "empty";
814 if ( slapd_shutdown ) {
819 ber_init2( ber, NULL, LBER_USE_DER );
820 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
822 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
824 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
826 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST && si->si_refreshDone ) {
832 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
833 tout_p, &msg ) ) > 0 )
835 int match, punlock, syncstate;
836 struct berval *retdata, syncUUID, cookie = BER_BVNULL;
838 LDAPControl **rctrls = NULL, *rctrlp = NULL;
845 if ( slapd_shutdown ) {
849 switch( ldap_msgtype( msg ) ) {
850 case LDAP_RES_SEARCH_ENTRY:
851 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
852 ldap_get_dn_ber( si->si_ld, msg, NULL, &bdn );
855 bdn.bv_len = sizeof(empty)-1;
857 /* we can't work without the control */
859 LDAPControl **next = NULL;
860 /* NOTE: make sure we use the right one;
861 * a better approach would be to run thru
862 * the whole list and take care of all */
863 /* NOTE: since we issue the search request,
864 * we should know what controls to expect,
865 * and there should be none apart from the
866 * sync-related control */
867 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
868 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
870 bdn.bv_val[bdn.bv_len] = '\0';
871 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
872 "got search entry with multiple "
873 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
874 ldap_controls_free( rctrls );
879 if ( rctrlp == NULL ) {
880 bdn.bv_val[bdn.bv_len] = '\0';
881 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
882 "got search entry without "
883 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
887 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
888 if ( ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID )
890 bdn.bv_val[bdn.bv_len] = '\0';
891 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s malformed message (%s)\n",
892 si->si_ridtxt, bdn.bv_val, 0 );
893 ldap_controls_free( rctrls );
897 /* FIXME: what if syncUUID is NULL or empty?
898 * (happens with back-sql...) */
899 if ( BER_BVISEMPTY( &syncUUID ) ) {
900 bdn.bv_val[bdn.bv_len] = '\0';
901 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
902 "got empty syncUUID with LDAP_SYNC_%s (%s)\n",
904 syncrepl_state2str( syncstate ), bdn.bv_val );
905 ldap_controls_free( rctrls );
910 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
911 ber_scanf( ber, /*"{"*/ "m}", &cookie );
913 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
915 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
917 if ( !BER_BVISNULL( &cookie ) ) {
918 ch_free( syncCookie.octet_str.bv_val );
919 ber_dupbv( &syncCookie.octet_str, &cookie );
921 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
923 slap_parse_sync_cookie( &syncCookie, NULL );
924 if ( syncCookie.ctxcsn ) {
925 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
926 check_syncprov( op, si );
927 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
929 if ( sid < si->si_cookieState->cs_sids[i] )
931 if ( si->si_cookieState->cs_sids[i] == sid ) {
932 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
933 bdn.bv_val[bdn.bv_len] = '\0';
934 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s (%s)\n",
935 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
936 ldap_controls_free( rctrls );
943 /* check pending CSNs too */
944 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
945 if ( slapd_shutdown ) {
949 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
950 ldap_pvt_thread_yield();
952 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
953 if ( sid < si->si_cookieState->cs_psids[i] )
955 if ( si->si_cookieState->cs_psids[i] == sid ) {
956 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
957 bdn.bv_val[bdn.bv_len] = '\0';
958 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s (%s)\n",
959 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
960 ldap_controls_free( rctrls );
962 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
965 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
970 /* new SID, add it */
971 if ( i == si->si_cookieState->cs_pnum ||
972 sid != si->si_cookieState->cs_psids[i] ) {
973 slap_insert_csn_sids(
974 (struct sync_cookie *)&si->si_cookieState->cs_pvals,
975 i, sid, syncCookie.ctxcsn );
977 assert( punlock < 0 );
980 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
984 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
986 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
989 rc = syncrepl_updateCookie( si, op, &syncCookie );
990 } else switch ( rc ) {
991 case LDAP_ALREADY_EXISTS:
992 case LDAP_NO_SUCH_OBJECT:
993 case LDAP_NO_SUCH_ATTRIBUTE:
994 case LDAP_TYPE_OR_VALUE_EXISTS:
995 rc = LDAP_SYNC_REFRESH_REQUIRED;
996 si->si_logstate = SYNCLOG_FALLBACK;
997 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
998 bdn.bv_val[bdn.bv_len] = '\0';
999 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync on (%s), switching to REFRESH\n",
1000 si->si_ridtxt, bdn.bv_val, 0 );
1001 if (si->si_strict_refresh) {
1002 slap_suspend_listeners();
1009 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
1010 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
1012 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
1013 syncstate, &syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
1016 rc = syncrepl_updateCookie( si, op, &syncCookie );
1019 if ( punlock >= 0 ) {
1020 /* on failure, revert pending CSN */
1021 if ( rc != LDAP_SUCCESS ) {
1023 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
1024 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
1025 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
1026 &si->si_cookieState->cs_vals[i] );
1030 if ( i == si->si_cookieState->cs_num )
1031 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
1033 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
1035 ldap_controls_free( rctrls );
1037 slap_mods_free( modlist, 1 );
1043 case LDAP_RES_SEARCH_REFERENCE:
1044 Debug( LDAP_DEBUG_ANY,
1045 "do_syncrep2: %s reference received error\n",
1046 si->si_ridtxt, 0, 0 );
1049 case LDAP_RES_SEARCH_RESULT:
1050 Debug( LDAP_DEBUG_SYNC,
1051 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
1052 si->si_ridtxt, 0, 0 );
1053 err = LDAP_OTHER; /* FIXME check parse result properly */
1054 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
1056 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
1057 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
1058 /* map old result code to registered code */
1059 err = LDAP_SYNC_REFRESH_REQUIRED;
1062 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
1063 if ( si->si_logstate == SYNCLOG_LOGGING ) {
1064 si->si_logstate = SYNCLOG_FALLBACK;
1065 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync, switching to REFRESH\n",
1066 si->si_ridtxt, 0, 0 );
1067 if (si->si_strict_refresh) {
1068 slap_suspend_listeners();
1076 Debug( LDAP_DEBUG_ANY,
1077 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1078 si->si_ridtxt, err, ldap_err2string( err ) );
1081 LDAPControl **next = NULL;
1082 /* NOTE: make sure we use the right one;
1083 * a better approach would be to run thru
1084 * the whole list and take care of all */
1085 /* NOTE: since we issue the search request,
1086 * we should know what controls to expect,
1087 * and there should be none apart from the
1088 * sync-related control */
1089 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1090 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1092 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1093 "got search result with multiple "
1094 "Sync State control\n", si->si_ridtxt, 0, 0 );
1095 ldap_controls_free( rctrls );
1101 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1103 ber_scanf( ber, "{" /*"}"*/);
1104 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1105 ber_scanf( ber, "m", &cookie );
1107 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1109 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1111 if ( !BER_BVISNULL( &cookie ) ) {
1112 ch_free( syncCookie.octet_str.bv_val );
1113 ber_dupbv( &syncCookie.octet_str, &cookie);
1115 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1117 slap_parse_sync_cookie( &syncCookie, NULL );
1118 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1121 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1123 ber_scanf( ber, "b", &refreshDeletes );
1125 ber_scanf( ber, /*"{"*/ "}" );
1127 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1128 slap_sync_cookie_free( &syncCookie_req, 0 );
1129 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1131 if ( !syncCookie.ctxcsn ) {
1133 } else if ( !syncCookie_req.ctxcsn ) {
1137 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1140 ldap_controls_free( rctrls );
1142 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1143 /* FIXME : different error behaviors according to
1144 * 1) err code : LDAP_BUSY ...
1145 * 2) on err policy : stop service, stop sync, retry
1147 if ( refreshDeletes == 0 && match < 0 &&
1148 err == LDAP_SUCCESS &&
1149 syncCookie_req.numcsns == syncCookie.numcsns )
1151 syncrepl_del_nonpresent( op, si, NULL,
1154 avl_free( si->si_presentlist, ch_free );
1155 si->si_presentlist = NULL;
1158 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1160 rc = syncrepl_updateCookie( si, op, &syncCookie );
1162 if ( err == LDAP_SUCCESS
1163 && si->si_logstate == SYNCLOG_FALLBACK ) {
1164 si->si_logstate = SYNCLOG_LOGGING;
1165 rc = LDAP_SYNC_REFRESH_REQUIRED;
1166 slap_resume_listeners();
1172 case LDAP_RES_INTERMEDIATE:
1175 rc = ldap_parse_intermediate( si->si_ld, msg,
1176 &retoid, &retdata, NULL, 0 );
1177 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1178 ber_init2( ber, retdata, LBER_USE_DER );
1180 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1182 case LDAP_TAG_SYNC_NEW_COOKIE:
1183 Debug( LDAP_DEBUG_SYNC,
1184 "do_syncrep2: %s %s - %s\n",
1186 "LDAP_RES_INTERMEDIATE",
1188 ber_scanf( ber, "tm", &tag, &cookie );
1189 Debug( LDAP_DEBUG_SYNC,
1190 "do_syncrep2: %s NEW_COOKIE: %s\n",
1193 if ( !BER_BVISNULL( &cookie ) ) {
1194 ch_free( syncCookie.octet_str.bv_val );
1195 ber_dupbv( &syncCookie.octet_str, &cookie );
1197 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1198 slap_parse_sync_cookie( &syncCookie, NULL );
1199 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1202 case LDAP_TAG_SYNC_REFRESH_DELETE:
1203 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1204 Debug( LDAP_DEBUG_SYNC,
1205 "do_syncrep2: %s %s - %s\n",
1207 "LDAP_RES_INTERMEDIATE",
1208 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1209 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1210 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1211 si->si_refreshDelete = 1;
1213 si->si_refreshPresent = 1;
1215 ber_scanf( ber, "t{" /*"}"*/, &tag );
1216 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1218 ber_scanf( ber, "m", &cookie );
1220 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1222 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1224 if ( !BER_BVISNULL( &cookie ) ) {
1225 ch_free( syncCookie.octet_str.bv_val );
1226 ber_dupbv( &syncCookie.octet_str, &cookie );
1228 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1230 slap_parse_sync_cookie( &syncCookie, NULL );
1231 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1234 /* Defaults to TRUE */
1235 if ( ber_peek_tag( ber, &len ) ==
1236 LDAP_TAG_REFRESHDONE )
1238 ber_scanf( ber, "b", &si->si_refreshDone );
1241 si->si_refreshDone = 1;
1243 ber_scanf( ber, /*"{"*/ "}" );
1244 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST &&
1245 si->si_refreshDone )
1248 case LDAP_TAG_SYNC_ID_SET:
1249 Debug( LDAP_DEBUG_SYNC,
1250 "do_syncrep2: %s %s - %s\n",
1252 "LDAP_RES_INTERMEDIATE",
1254 ber_scanf( ber, "t{" /*"}"*/, &tag );
1255 if ( ber_peek_tag( ber, &len ) ==
1256 LDAP_TAG_SYNC_COOKIE )
1258 ber_scanf( ber, "m", &cookie );
1260 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1262 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1264 if ( !BER_BVISNULL( &cookie ) ) {
1265 ch_free( syncCookie.octet_str.bv_val );
1266 ber_dupbv( &syncCookie.octet_str, &cookie );
1268 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1270 slap_parse_sync_cookie( &syncCookie, NULL );
1271 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1272 compare_csns( &syncCookie_req, &syncCookie, &m );
1275 if ( ber_peek_tag( ber, &len ) ==
1276 LDAP_TAG_REFRESHDELETES )
1278 ber_scanf( ber, "b", &refreshDeletes );
1281 ber_scanf( ber, "[W]", &syncUUIDs );
1282 ber_scanf( ber, /*"{"*/ "}" );
1283 if ( refreshDeletes ) {
1284 syncrepl_del_nonpresent( op, si, syncUUIDs,
1286 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1289 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1290 (void)avl_presentlist_insert( si, &syncUUIDs[i] );
1291 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1293 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1295 slap_sync_cookie_free( &syncCookie, 0 );
1298 Debug( LDAP_DEBUG_ANY,
1299 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1300 si->si_ridtxt, (long) si_tag, 0 );
1301 ldap_memfree( retoid );
1302 ber_bvfree( retdata );
1306 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1307 slap_sync_cookie_free( &syncCookie_req, 0 );
1308 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1310 if ( !syncCookie.ctxcsn ) {
1312 } else if ( !syncCookie_req.ctxcsn ) {
1316 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1320 if ( si->si_refreshPresent == 1 &&
1321 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1322 syncCookie_req.numcsns == syncCookie.numcsns ) {
1323 syncrepl_del_nonpresent( op, si, NULL,
1327 if ( syncCookie.ctxcsn )
1329 rc = syncrepl_updateCookie( si, op, &syncCookie);
1333 ldap_memfree( retoid );
1334 ber_bvfree( retdata );
1337 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1338 "unknown intermediate response (%d)\n",
1339 si->si_ridtxt, rc, 0 );
1340 ldap_memfree( retoid );
1341 ber_bvfree( retdata );
1346 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1347 "unknown message (0x%02lx)\n",
1349 (unsigned long)ldap_msgtype( msg ), 0 );
1353 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1354 slap_sync_cookie_free( &syncCookie_req, 0 );
1355 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1356 slap_sync_cookie_free( &syncCookie, 0 );
1358 ldap_msgfree( msg );
1360 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1361 slap_sync_cookie_free( &syncCookie, 0 );
1362 slap_sync_cookie_free( &syncCookie_req, 0 );
1369 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1374 if ( err != LDAP_SUCCESS ) {
1375 Debug( LDAP_DEBUG_ANY,
1376 "do_syncrep2: %s (%d) %s\n",
1377 si->si_ridtxt, err, ldap_err2string( err ) );
1380 slap_sync_cookie_free( &syncCookie, 0 );
1381 slap_sync_cookie_free( &syncCookie_req, 0 );
1383 if ( msg ) ldap_msgfree( msg );
1385 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1386 if ( si->si_conn ) {
1387 connection_client_stop( si->si_conn );
1390 ldap_unbind_ext( si->si_ld, NULL, NULL );
1402 struct re_s* rtask = arg;
1403 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1404 Connection conn = {0};
1405 OperationBuffer opbuf;
1407 int rc = LDAP_SUCCESS;
1410 int i, defer = 1, fail = 0, freeinfo = 0;
1415 if ( slapd_shutdown )
1418 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1420 /* Don't get stuck here while a pause is initiated */
1421 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1422 if ( slapd_shutdown )
1424 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1425 ldap_pvt_thread_yield();
1428 if ( si->si_ctype < 1 ) {
1432 switch( abs( si->si_type ) ) {
1433 case LDAP_SYNC_REFRESH_ONLY:
1434 case LDAP_SYNC_REFRESH_AND_PERSIST:
1437 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1441 if ( slapd_shutdown ) {
1443 if ( si->si_conn ) {
1444 connection_client_stop( si->si_conn );
1447 ldap_unbind_ext( si->si_ld, NULL, NULL );
1450 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1454 connection_fake_init( &conn, &opbuf, ctx );
1456 /* o_connids must be unique for slap_graduate_commit_csn */
1457 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1459 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1462 /* Coordinate contextCSN updates with any syncprov overlays
1463 * in use. This may be complicated by the use of the glue
1466 * Typically there is a single syncprov mastering the entire
1467 * glued tree. In that case, our contextCSN updates should
1468 * go to the master DB. But if there is no syncprov on the
1469 * master DB, then nothing special is needed here.
1471 * Alternatively, there may be individual syncprov overlays
1472 * on each glued branch. In that case, each syncprov only
1473 * knows about changes within its own branch. And so our
1474 * contextCSN updates should only go to the local DB.
1476 if ( !si->si_wbe ) {
1477 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1478 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1479 if ( overlay_is_inst( top_be, "syncprov" ))
1480 si->si_wbe = top_be;
1486 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1487 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1488 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1490 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1493 if ( !si->si_schemachecking )
1494 op->o_no_schema_check = 1;
1496 /* Establish session, do search */
1498 si->si_refreshDelete = 0;
1499 si->si_refreshPresent = 0;
1501 if ( si->si_presentlist ) {
1502 avl_free( si->si_presentlist, ch_free );
1503 si->si_presentlist = NULL;
1506 /* use main DB when retrieving contextCSN */
1507 op->o_bd = si->si_wbe;
1508 op->o_dn = op->o_bd->be_rootdn;
1509 op->o_ndn = op->o_bd->be_rootndn;
1510 rc = do_syncrep1( op, si );
1514 /* Process results */
1515 if ( rc == LDAP_SUCCESS ) {
1516 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1518 /* use current DB */
1520 op->o_dn = op->o_bd->be_rootdn;
1521 op->o_ndn = op->o_bd->be_rootndn;
1522 rc = do_syncrep2( op, si );
1523 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1524 rc = ldap_sync_search( si, op->o_tmpmemctx );
1529 /* We got deleted while running on cn=config */
1530 if ( si->si_ctype < 1 ) {
1531 if ( si->si_ctype == -1 ) {
1540 if ( rc != SYNC_PAUSED ) {
1541 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1542 /* If we succeeded, enable the connection for further listening.
1543 * If we failed, tear down the connection and reschedule.
1545 if ( rc == LDAP_SUCCESS ) {
1546 if ( si->si_conn ) {
1547 connection_client_enable( si->si_conn );
1549 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1551 } else if ( si->si_conn ) {
1555 if ( rc == -2 ) rc = 0;
1560 /* At this point, we have 5 cases:
1561 * 1) for any hard failure, give up and remove this task
1562 * 2) for ServerDown, reschedule this task to run later
1563 * 3) for threadpool pause, reschedule to run immediately
1564 * 4) for Refresh and Success, reschedule to run
1565 * 5) for Persist and Success, reschedule to defer
1567 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1569 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1570 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1574 connection_client_stop( si->si_conn );
1578 if ( rc == SYNC_PAUSED ) {
1579 rtask->interval.tv_sec = 0;
1580 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1581 rtask->interval.tv_sec = si->si_interval;
1583 } else if ( rc == LDAP_SUCCESS ) {
1584 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1587 rtask->interval.tv_sec = si->si_interval;
1588 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1589 if ( si->si_retrynum ) {
1590 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1591 si->si_retrynum[i] = si->si_retrynum_init[i];
1593 si->si_retrynum[i] = RETRYNUM_TAIL;
1596 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1597 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1601 if ( si->si_ctype < 1
1602 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1604 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1607 fail = RETRYNUM_TAIL;
1608 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1609 if ( si->si_retrynum[i] > 0 )
1610 si->si_retrynum[i]--;
1611 fail = si->si_retrynum[i];
1612 rtask->interval.tv_sec = si->si_retryinterval[i];
1613 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1614 slap_wake_listener();
1618 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1619 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1622 if ( fail == RETRYNUM_TAIL ) {
1623 Debug( LDAP_DEBUG_ANY,
1624 "do_syncrepl: %s rc %d quitting\n",
1625 si->si_ridtxt, rc, 0 );
1626 } else if ( fail > 0 ) {
1627 Debug( LDAP_DEBUG_ANY,
1628 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1629 si->si_ridtxt, rc, fail );
1631 Debug( LDAP_DEBUG_ANY,
1632 "do_syncrepl: %s rc %d retrying\n",
1633 si->si_ridtxt, rc, 0 );
1637 /* Do final delete cleanup */
1639 syncinfo_free( si, 0 );
1644 #ifdef ENABLE_REWRITE
1646 syncrepl_rewrite_dn(
1649 struct berval *sdn )
1654 nul = dn->bv_val[dn->bv_len];
1655 dn->bv_val[dn->bv_len] = 0;
1656 rc = rewrite( si->si_rewrite, SUFFIXM_CTX, dn->bv_val, &sdn->bv_val );
1657 dn->bv_val[dn->bv_len] = nul;
1659 if ( sdn->bv_val == dn->bv_val )
1661 else if ( rc == REWRITE_REGEXEC_OK && sdn->bv_val )
1662 sdn->bv_len = strlen( sdn->bv_val );
1665 #define REWRITE_VAL(si, ad, bv, bv2) \
1666 BER_BVZERO( &bv2 ); \
1667 if ( si->si_rewrite && ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName) \
1668 syncrepl_rewrite_dn( si, &bv, &bv2); \
1669 if ( BER_BVISNULL( &bv2 )) \
1670 ber_dupbv( &bv2, &bv )
1671 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1672 BER_BVZERO( &bv2 ); \
1673 if (si->si_rewrite) \
1674 syncrepl_rewrite_dn(si, &bv, &bv2); \
1675 rc = dnPrettyNormal( NULL, bv2.bv_val ? &bv2 : &bv, &dn, &ndn, op->o_tmpmemctx ); \
1678 #define REWRITE_VAL(si, ad, bv, bv2) ber_dupbv(&bv2, &bv)
1679 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1680 rc = dnPrettyNormal( NULL, &bv, &dn, &ndn, op->o_tmpmemctx )
1684 static slap_verbmasks modops[] = {
1685 { BER_BVC("add"), LDAP_REQ_ADD },
1686 { BER_BVC("delete"), LDAP_REQ_DELETE },
1687 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1688 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1693 syncrepl_accesslog_mods(
1695 struct berval *vals,
1696 struct Modifications **modres
1701 AttributeDescription *ad;
1702 struct berval bv, bv2;
1704 Modifications *mod = NULL, *modlist = NULL, **modtail;
1709 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1713 colon = ber_bvchr( &bv, ':' );
1719 bv.bv_len = colon - bv.bv_val;
1720 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1722 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1723 "Invalid attribute %s, %s\n",
1724 si->si_ridtxt, bv.bv_val, text );
1725 slap_mods_free( modlist, 1 );
1731 /* Ignore dynamically generated attrs */
1732 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1736 /* Ignore excluded attrs */
1737 if ( ldap_charray_inlist( si->si_exattrs,
1738 ad->ad_type->sat_cname.bv_val ) )
1744 case '+': op = LDAP_MOD_ADD; break;
1745 case '-': op = LDAP_MOD_DELETE; break;
1746 case '=': op = LDAP_MOD_REPLACE; break;
1747 case '#': op = LDAP_MOD_INCREMENT; break;
1751 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1752 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1755 mod->sml_next = NULL;
1757 mod->sml_type = ad->ad_cname;
1758 mod->sml_values = NULL;
1759 mod->sml_nvalues = NULL;
1760 mod->sml_numvals = 0;
1763 modtail = &mod->sml_next;
1765 if ( colon[2] == ' ' ) {
1766 bv.bv_val = colon + 3;
1767 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1768 REWRITE_VAL( si, ad, bv, bv2 );
1769 ber_bvarray_add( &mod->sml_values, &bv2 );
1778 syncrepl_changelog_mods(
1780 struct berval *vals,
1781 struct Modifications **modres
1784 return -1; /* FIXME */
1787 typedef struct OpExtraSync {
1792 /* Copy the original modlist, split Replace ops into Delete/Add,
1793 * and drop mod opattrs since this modification is in the past.
1795 static Modifications *mods_dup( Operation *op, Modifications *modlist, int match )
1797 Modifications *mod, *modnew = NULL, *modtail = NULL;
1799 for ( ; modlist; modlist = modlist->sml_next ) {
1802 if ( modlist->sml_desc == slap_schema.si_ad_modifiersName ||
1803 modlist->sml_desc == slap_schema.si_ad_modifyTimestamp ||
1804 modlist->sml_desc == slap_schema.si_ad_entryCSN )
1806 if ( modlist->sml_op == LDAP_MOD_REPLACE ) {
1807 mod = op->o_tmpalloc( sizeof(Modifications), op->o_tmpmemctx );
1808 mod->sml_desc = modlist->sml_desc;
1809 mod->sml_values = NULL;
1810 mod->sml_nvalues = NULL;
1811 mod->sml_op = LDAP_MOD_DELETE;
1812 mod->sml_numvals = 0;
1817 modtail->sml_next = mod;
1821 if ( modlist->sml_numvals ) {
1822 size = (modlist->sml_numvals+1) * sizeof(struct berval);
1823 if ( modlist->sml_nvalues ) size *= 2;
1827 size += sizeof(Modifications);
1828 mod = op->o_tmpalloc( size, op->o_tmpmemctx );
1832 modtail->sml_next = mod;
1834 mod->sml_desc = modlist->sml_desc;
1835 mod->sml_numvals = modlist->sml_numvals;
1837 if ( modlist->sml_numvals ) {
1839 mod->sml_values = (BerVarray)(mod+1);
1840 for (i=0; i<mod->sml_numvals; i++)
1841 mod->sml_values[i] = modlist->sml_values[i];
1842 BER_BVZERO(&mod->sml_values[i]);
1843 if ( modlist->sml_nvalues ) {
1844 mod->sml_nvalues = mod->sml_values + mod->sml_numvals + 1;
1845 for (i=0; i<mod->sml_numvals; i++)
1846 mod->sml_nvalues[i] = modlist->sml_nvalues[i];
1847 BER_BVZERO(&mod->sml_nvalues[i]);
1849 mod->sml_nvalues = NULL;
1852 mod->sml_values = NULL;
1853 mod->sml_nvalues = NULL;
1855 if ( match < 0 && modlist->sml_op == LDAP_MOD_REPLACE )
1856 mod->sml_op = LDAP_MOD_ADD;
1858 mod->sml_op = modlist->sml_op;
1859 mod->sml_next = NULL;
1864 typedef struct resolve_ctxt {
1866 Modifications *rx_mods;
1870 compare_vals( Modifications *m1, Modifications *m2 )
1873 struct berval *bv1, *bv2;
1875 if ( m2->sml_nvalues ) {
1876 bv2 = m2->sml_nvalues;
1877 bv1 = m1->sml_nvalues;
1879 bv2 = m2->sml_values;
1880 bv1 = m1->sml_values;
1882 for ( j=0; j<m2->sml_numvals; j++ ) {
1883 for ( i=0; i<m1->sml_numvals; i++ ) {
1884 if ( !ber_bvcmp( &bv1[i], &bv2[j] )) {
1886 for ( k=i; k<m1->sml_numvals-1; k++ ) {
1887 m1->sml_values[k] = m1->sml_values[k+1];
1888 if ( m1->sml_nvalues )
1889 m1->sml_nvalues[k] = m1->sml_nvalues[k+1];
1891 BER_BVZERO(&m1->sml_values[k]);
1892 if ( m1->sml_nvalues ) {
1893 BER_BVZERO(&m1->sml_nvalues[k]);
1903 syncrepl_resolve_cb( Operation *op, SlapReply *rs )
1905 if ( rs->sr_type == REP_SEARCH ) {
1906 resolve_ctxt *rx = op->o_callback->sc_private;
1907 Attribute *a = attr_find( rs->sr_entry->e_attrs, ad_reqMod );
1909 Modifications *oldmods, *newmods, *m1, *m2, **prev;
1910 oldmods = rx->rx_mods;
1911 syncrepl_accesslog_mods( rx->rx_si, a->a_vals, &newmods );
1912 for ( m2 = newmods; m2; m2=m2->sml_next ) {
1913 for ( prev = &oldmods, m1 = *prev; m1; m1 = *prev ) {
1914 if ( m1->sml_desc != m2->sml_desc ) {
1915 prev = &m1->sml_next;
1918 if ( m2->sml_op == LDAP_MOD_DELETE ||
1919 m2->sml_op == LDAP_MOD_REPLACE ) {
1920 int numvals = m2->sml_numvals;
1921 if ( m2->sml_op == LDAP_MOD_REPLACE )
1923 /* New delete All cancels everything */
1924 if ( numvals == 0 ) {
1926 *prev = m1->sml_next;
1927 op->o_tmpfree( m1, op->o_tmpmemctx );
1930 if ( m1->sml_op == LDAP_MOD_DELETE ) {
1931 if ( m1->sml_numvals == 0 ) {
1932 /* turn this to SOFTDEL later */
1933 m1->sml_flags = SLAP_MOD_INTERNAL;
1935 compare_vals( m1, m2 );
1936 if ( !m1->sml_numvals )
1939 } else if ( m1->sml_op == LDAP_MOD_ADD ) {
1940 compare_vals( m1, m2 );
1941 if ( !m1->sml_numvals )
1946 if ( m2->sml_op == LDAP_MOD_ADD ||
1947 m2->sml_op == LDAP_MOD_REPLACE ) {
1948 if ( m1->sml_op == LDAP_MOD_DELETE ) {
1949 if ( !m1->sml_numvals ) goto drop;
1950 compare_vals( m1, m2 );
1951 if ( !m1->sml_numvals )
1954 if ( m2->sml_desc->ad_type->sat_atype.at_single_value )
1956 compare_vals( m1, m2 );
1957 if ( !m1->sml_numvals )
1960 prev = &m1->sml_next;
1963 slap_mods_free( newmods, 1 );
1966 return LDAP_SUCCESS;
1969 typedef struct modify_ctxt {
1970 Modifications *mx_orig;
1971 Modifications *mx_free;
1975 syncrepl_modify_cb( Operation *op, SlapReply *rs )
1977 slap_callback *sc = op->o_callback;
1978 modify_ctxt *mx = sc->sc_private;
1981 op->orm_no_opattrs = 0;
1982 op->orm_modlist = mx->mx_orig;
1983 for ( ml = mx->mx_free; ml; ml = mx->mx_free ) {
1984 mx->mx_free = ml->sml_next;
1985 op->o_tmpfree( ml, op->o_tmpmemctx );
1987 op->o_callback = sc->sc_next;
1988 op->o_tmpfree( sc, op->o_tmpmemctx );
1989 return SLAP_CB_CONTINUE;
1993 syncrepl_op_modify( Operation *op, SlapReply *rs )
1995 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2000 Modifications *mod, *newlist;
2002 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
2003 if ( oex->oe_key == (void *)syncrepl_message_to_op )
2007 return SLAP_CB_CONTINUE;
2009 si = ((OpExtraSync *)oex)->oe_si;
2011 /* Check if entryCSN in modlist is newer than entryCSN in entry.
2012 * We do it here because the op has been serialized by accesslog
2013 * by the time we get here. If the CSN is new enough, just do the
2014 * mod. If not, we need to resolve conflicts.
2017 for ( mod = op->orm_modlist; mod; mod=mod->sml_next ) {
2018 if ( mod->sml_desc == slap_schema.si_ad_entryCSN ) break;
2020 /* FIXME: what should we do if entryCSN is missing from the mod? */
2022 return SLAP_CB_CONTINUE;
2024 rc = overlay_entry_get_ov( op, &op->o_req_ndn, NULL, NULL, 0, &e, on );
2028 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
2029 value_match( &match, slap_schema.si_ad_entryCSN,
2030 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
2031 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2032 &mod->sml_nvalues[0], &a->a_nvals[0], &text );
2033 overlay_entry_release_ov( op, e, 0, on );
2035 /* equal? Should never happen */
2037 return LDAP_SUCCESS;
2039 /* mod is older: resolve conflicts...
2040 * 1. Save/copy original modlist. Split Replace to Del/Add.
2041 * 2. Find all mods to this reqDN newer than the mod stamp.
2042 * 3. Resolve any mods in this request that affect attributes
2043 * touched by newer mods.
2045 * delete all delete all drop
2046 * delete all delete X SOFTDEL
2047 * delete X delete all drop
2048 * delete X delete X drop
2049 * delete X delete Y OK
2050 * delete all add X drop
2051 * delete X add X drop
2053 * add X delete all drop
2054 * add X delete X drop
2056 * add X add Y if SV, drop else OK
2058 * 4. Swap original modlist back in response callback so
2059 * that accesslog logs the original mod.
2061 * Even if the mod is newer, other out-of-order changes may
2062 * have been committed, forcing us to tweak the modlist:
2063 * 1. Save/copy original modlist.
2064 * 2. Change deletes to soft deletes.
2065 * 3. Change Adds of single-valued attrs to Replace.
2068 newlist = mods_dup( op, op->orm_modlist, match );
2072 Operation op2 = *op;
2073 AttributeName an[2];
2079 SlapReply rs1 = {0};
2080 resolve_ctxt rx = { si, newlist };
2081 slap_callback cb = { NULL, syncrepl_resolve_cb, NULL, &rx };
2083 op2.o_tag = LDAP_REQ_SEARCH;
2084 op2.ors_scope = LDAP_SCOPE_SUBTREE;
2085 op2.ors_deref = LDAP_DEREF_NEVER;
2086 op2.o_req_dn = si->si_logbase;
2087 op2.o_req_ndn = si->si_logbase;
2088 op2.ors_tlimit = SLAP_NO_LIMIT;
2089 op2.ors_slimit = SLAP_NO_LIMIT;
2090 op2.ors_limit = NULL;
2091 memset( an, 0, sizeof(an));
2092 an[0].an_desc = ad_reqMod;
2093 an[0].an_name = ad_reqMod->ad_cname;
2095 op2.ors_attrsonly = 0;
2097 bv = mod->sml_nvalues[0];
2099 size = sizeof("(&(entryCSN>=)(reqDN=))");
2100 size += bv.bv_len + op->o_req_ndn.bv_len + si->si_logfilterstr.bv_len;
2101 op2.ors_filterstr.bv_val = op->o_tmpalloc( size, op->o_tmpmemctx );
2102 op2.ors_filterstr.bv_len = sprintf(op2.ors_filterstr.bv_val,
2103 "(&(entryCSN>=%s)(reqDN=%s)%s)",
2104 bv.bv_val, op->o_req_ndn.bv_val, si->si_logfilterstr.bv_val );
2105 op2.ors_filter = str2filter_x( op, op2.ors_filterstr.bv_val );
2107 op2.o_callback = &cb;
2108 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2109 op2.o_bd->be_search( &op2, &rs1 );
2110 newlist = rx.rx_mods;
2114 slap_callback *sc = op->o_tmpalloc( sizeof(slap_callback) +
2115 sizeof(modify_ctxt), op->o_tmpmemctx );
2116 modify_ctxt *mx = (modify_ctxt *)(sc+1);
2119 sc->sc_response = syncrepl_modify_cb;
2120 sc->sc_private = mx;
2121 sc->sc_next = op->o_callback;
2122 sc->sc_cleanup = NULL;
2123 op->o_callback = sc;
2124 op->orm_no_opattrs = 1;
2125 mx->mx_orig = op->orm_modlist;
2126 mx->mx_free = newlist;
2127 for ( ml = newlist; ml; ml=ml->sml_next ) {
2128 if ( ml->sml_flags == SLAP_MOD_INTERNAL ) {
2130 ml->sml_op = SLAP_MOD_SOFTDEL;
2132 else if ( ml->sml_op == LDAP_MOD_DELETE )
2133 ml->sml_op = SLAP_MOD_SOFTDEL;
2134 else if ( ml->sml_op == LDAP_MOD_ADD &&
2135 ml->sml_desc->ad_type->sat_atype.at_single_value )
2136 ml->sml_op = LDAP_MOD_REPLACE;
2138 op->orm_modlist = newlist;
2139 op->o_csn = mod->sml_nvalues[0];
2141 return SLAP_CB_CONTINUE;
2145 syncrepl_message_to_op(
2151 BerElement *ber = NULL;
2152 Modifications *modlist = NULL;
2154 SlapReply rs = { REP_RESULT };
2155 slap_callback cb = { NULL, null_callback, NULL, NULL };
2158 char txtbuf[SLAP_TEXT_BUFLEN];
2159 size_t textlen = sizeof txtbuf;
2161 struct berval bdn, dn = BER_BVNULL, ndn;
2162 struct berval bv, bv2, *bvals = NULL;
2163 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
2164 prdn = BER_BVNULL, nrdn = BER_BVNULL,
2165 psup = BER_BVNULL, nsup = BER_BVNULL;
2166 int rc, deleteOldRdn = 0, freeReqDn = 0;
2167 int do_graduate = 0;
2169 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2170 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2171 "Message type should be entry (%d)",
2172 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2176 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
2181 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2183 if ( rc != LDAP_SUCCESS ) {
2184 Debug( LDAP_DEBUG_ANY,
2185 "syncrepl_message_to_op: %s dn get failed (%d)",
2186 si->si_ridtxt, rc, 0 );
2190 op->o_tag = LBER_DEFAULT;
2191 op->o_bd = si->si_wbe;
2193 if ( BER_BVISEMPTY( &bdn )) {
2194 Debug( LDAP_DEBUG_ANY,
2195 "syncrepl_message_to_op: %s got empty dn",
2196 si->si_ridtxt, 0, 0 );
2200 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
2202 if ( bv.bv_val == NULL )
2205 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
2207 REWRITE_DN( si, bdn, bv2, dn, ndn );
2208 if ( rc != LDAP_SUCCESS ) {
2209 Debug( LDAP_DEBUG_ANY,
2210 "syncrepl_message_to_op: %s "
2211 "dn \"%s\" normalization failed (%d)",
2212 si->si_ridtxt, bdn.bv_val, rc );
2217 ber_dupbv( &op->o_req_dn, &dn );
2218 ber_dupbv( &op->o_req_ndn, &ndn );
2219 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2220 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2222 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
2223 int i = verb_to_mask( bvals[0].bv_val, modops );
2225 Debug( LDAP_DEBUG_ANY,
2226 "syncrepl_message_to_op: %s unknown op %s",
2227 si->si_ridtxt, bvals[0].bv_val, 0 );
2232 op->o_tag = modops[i].mask;
2233 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
2234 /* Parse attribute into modlist */
2235 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
2236 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
2238 rc = syncrepl_changelog_mods( si, bvals, &modlist );
2240 if ( rc ) goto done;
2241 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
2243 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
2244 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
2247 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
2249 } else if ( !ber_bvstrcasecmp( &bv,
2250 &slap_schema.si_ad_entryCSN->ad_cname ) )
2252 slap_queue_csn( op, bvals );
2258 /* If we didn't get a mod type or a target DN, bail out */
2259 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
2264 op->o_callback = &cb;
2265 slap_op_time( &op->o_time, &op->o_tincr );
2267 switch( op->o_tag ) {
2269 case LDAP_REQ_MODIFY:
2270 /* If we didn't get required data, bail */
2271 if ( !modlist ) goto done;
2273 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
2275 if ( rc != LDAP_SUCCESS ) {
2276 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2277 "mods check (%s)\n",
2278 si->si_ridtxt, text, 0 );
2282 if ( op->o_tag == LDAP_REQ_ADD ) {
2283 Entry *e = entry_alloc();
2285 op->ora_e->e_name = op->o_req_dn;
2286 op->ora_e->e_nname = op->o_req_ndn;
2288 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
2289 if( rc != LDAP_SUCCESS ) {
2290 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2291 "mods2entry (%s)\n",
2292 si->si_ridtxt, text, 0 );
2294 rc = op->o_bd->be_add( op, &rs );
2295 Debug( LDAP_DEBUG_SYNC,
2296 "syncrepl_message_to_op: %s be_add %s (%d)\n",
2297 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2300 if ( e == op->ora_e )
2301 be_entry_release_w( op, op->ora_e );
2304 op->orm_modlist = modlist;
2305 op->o_bd = si->si_wbe;
2306 /* delta-mmr needs additional checks in syncrepl_op_modify */
2307 if ( SLAP_MULTIMASTER( op->o_bd )) {
2308 oes.oe.oe_key = (void *)syncrepl_message_to_op;
2310 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &oes.oe, oe_next );
2312 rc = op->o_bd->be_modify( op, &rs );
2313 if ( SLAP_MULTIMASTER( op->o_bd )) {
2314 LDAP_SLIST_REMOVE( &op->o_extra, &oes.oe, OpExtra, oe_next );
2315 BER_BVZERO( &op->o_csn );
2317 modlist = op->orm_modlist;
2318 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2319 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
2320 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2321 op->o_bd = si->si_be;
2325 case LDAP_REQ_MODRDN:
2326 if ( BER_BVISNULL( &rdn ) ) goto done;
2328 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
2331 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
2334 if ( !BER_BVISNULL( &sup ) ) {
2335 REWRITE_DN( si, sup, bv2, psup, nsup );
2338 op->orr_newSup = &psup;
2339 op->orr_nnewSup = ⊅
2341 op->orr_newSup = NULL;
2342 op->orr_nnewSup = NULL;
2344 op->orr_newrdn = prdn;
2345 op->orr_nnewrdn = nrdn;
2346 op->orr_deleteoldrdn = deleteOldRdn;
2347 op->orr_modlist = NULL;
2348 if ( slap_modrdn2mods( op, &rs ) ) {
2352 /* Append modlist for operational attrs */
2356 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
2358 m->sml_next = modlist;
2361 rc = op->o_bd->be_modrdn( op, &rs );
2362 slap_mods_free( op->orr_modlist, 1 );
2363 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2364 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
2365 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2368 case LDAP_REQ_DELETE:
2369 rc = op->o_bd->be_delete( op, &rs );
2370 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2371 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
2372 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2378 slap_graduate_commit_csn( op );
2379 op->o_bd = si->si_be;
2380 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2381 BER_BVZERO( &op->o_csn );
2383 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
2385 if ( !BER_BVISNULL( &rdn ) ) {
2386 if ( !BER_BVISNULL( &nsup ) ) {
2387 ch_free( nsup.bv_val );
2389 if ( !BER_BVISNULL( &psup ) ) {
2390 ch_free( psup.bv_val );
2392 if ( !BER_BVISNULL( &nrdn ) ) {
2393 ch_free( nrdn.bv_val );
2395 if ( !BER_BVISNULL( &prdn ) ) {
2396 ch_free( prdn.bv_val );
2400 ch_free( op->o_req_ndn.bv_val );
2401 ch_free( op->o_req_dn.bv_val );
2408 syncrepl_message_to_entry(
2412 Modifications **modlist,
2418 BerElement *ber = NULL;
2421 Modifications **modtail = modlist;
2424 char txtbuf[SLAP_TEXT_BUFLEN];
2425 size_t textlen = sizeof txtbuf;
2427 struct berval bdn = BER_BVNULL, dn, ndn, bv2;
2432 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2433 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
2434 "Message type should be entry (%d)",
2435 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2439 op->o_tag = LDAP_REQ_ADD;
2441 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2442 if ( rc != LDAP_SUCCESS ) {
2443 Debug( LDAP_DEBUG_ANY,
2444 "syncrepl_message_to_entry: %s dn get failed (%d)",
2445 si->si_ridtxt, rc, 0 );
2449 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
2450 Debug( LDAP_DEBUG_ANY,
2451 "syncrepl_message_to_entry: %s got empty dn",
2452 si->si_ridtxt, 0, 0 );
2456 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
2457 /* NOTE: this could be done even before decoding the DN,
2458 * although encoding errors wouldn't be detected */
2463 if ( entry == NULL ) {
2467 REWRITE_DN( si, bdn, bv2, dn, ndn );
2468 if ( rc != LDAP_SUCCESS ) {
2469 /* One of the things that could happen is that the schema
2470 * is not lined-up; this could result in unknown attributes.
2471 * A value non conformant to the syntax should be unlikely,
2472 * except when replicating between different versions
2473 * of the software, or when syntax validation bugs are fixed
2475 Debug( LDAP_DEBUG_ANY,
2476 "syncrepl_message_to_entry: "
2477 "%s dn \"%s\" normalization failed (%d)",
2478 si->si_ridtxt, bdn.bv_val, rc );
2482 ber_dupbv( &op->o_req_dn, &dn );
2483 ber_dupbv( &op->o_req_ndn, &ndn );
2484 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2485 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2487 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
2490 e->e_name = op->o_req_dn;
2491 e->e_nname = op->o_req_ndn;
2493 while ( ber_remaining( ber ) ) {
2494 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
2495 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2500 /* Drop all updates to the contextCSN of the context entry
2503 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2504 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2505 ber_bvarray_free( tmp.sml_values );
2509 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2511 mod->sml_op = LDAP_MOD_REPLACE;
2513 mod->sml_next = NULL;
2514 mod->sml_desc = NULL;
2515 mod->sml_type = tmp.sml_type;
2516 mod->sml_values = tmp.sml_values;
2517 mod->sml_nvalues = NULL;
2518 mod->sml_numvals = 0; /* slap_mods_check will set this */
2520 #ifdef ENABLE_REWRITE
2521 if (si->si_rewrite) {
2522 AttributeDescription *ad = NULL;
2523 slap_bv2ad( &tmp.sml_type, &ad, &text );
2526 mod->sml_type = ad->ad_cname;
2527 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
2529 for ( i = 0; tmp.sml_values[i].bv_val; i++ ) {
2530 syncrepl_rewrite_dn( si, &tmp.sml_values[i], &bv2);
2531 if ( !BER_BVISNULL( &bv2 )) {
2532 ber_memfree( tmp.sml_values[i].bv_val );
2533 tmp.sml_values[i] = bv2;
2541 modtail = &mod->sml_next;
2544 if ( *modlist == NULL ) {
2545 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2546 si->si_ridtxt, 0, 0 );
2551 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2553 if ( rc != LDAP_SUCCESS ) {
2554 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2555 si->si_ridtxt, text, 0 );
2559 /* Strip out dynamically generated attrs */
2560 for ( modtail = modlist; *modtail ; ) {
2562 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2563 *modtail = mod->sml_next;
2564 slap_mod_free( &mod->sml_mod, 0 );
2567 modtail = &mod->sml_next;
2571 /* Strip out attrs in exattrs list */
2572 for ( modtail = modlist; *modtail ; ) {
2574 if ( ldap_charray_inlist( si->si_exattrs,
2575 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2577 *modtail = mod->sml_next;
2578 slap_mod_free( &mod->sml_mod, 0 );
2581 modtail = &mod->sml_next;
2585 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2586 if( rc != LDAP_SUCCESS ) {
2587 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2588 si->si_ridtxt, text, 0 );
2593 if ( rc != LDAP_SUCCESS ) {
2605 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2607 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2608 * entry if a previous refresh was interrupted before sending us a new
2609 * context state. We try to compare the new entry to the existing entry
2610 * and ignore the new entry if they are the same.
2612 * Also, we may get an update where the entryDN has changed, due to
2613 * a ModDn on the provider. We detect this as well, so we can issue
2614 * the corresponding operation locally.
2616 * In the case of a modify, we get a list of all the attributes
2617 * in the original entry. Rather than deleting the entry and re-adding it,
2618 * we issue a Modify request that deletes all the attributes and adds all
2619 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2622 * We otherwise distinguish ModDN from Modify; in the case of
2623 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2624 * operational attributes from the entry, and do a regular ModDN.
2626 typedef struct dninfo {
2630 struct berval nnewSup;
2631 int renamed; /* Was an existing entry renamed? */
2632 int delOldRDN; /* Was old RDN deleted? */
2633 Modifications **modlist; /* the modlist we received */
2634 Modifications *mods; /* the modlist we compared */
2635 Attribute *oldNattr; /* old naming attr */
2636 AttributeDescription *oldDesc; /* for renames */
2637 AttributeDescription *newDesc; /* for renames */
2640 /* return 1 if inserted, 0 otherwise */
2642 avl_presentlist_insert(
2644 struct berval *syncUUID )
2646 struct berval *syncuuid_bv = ch_malloc( sizeof( struct berval ) + syncUUID->bv_len + 1 );
2648 syncuuid_bv->bv_len = syncUUID->bv_len;
2649 syncuuid_bv->bv_val = (char *)&syncuuid_bv[1];
2650 AC_MEMCPY( syncuuid_bv->bv_val, syncUUID->bv_val, syncUUID->bv_len );
2651 syncuuid_bv->bv_val[ syncuuid_bv->bv_len ] = '\0';
2653 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
2654 syncuuid_cmp, avl_dup_error ) )
2656 ch_free( syncuuid_bv );
2668 Modifications** modlist,
2670 struct berval* syncUUID,
2671 struct berval* syncCSN )
2673 Backend *be = op->o_bd;
2674 slap_callback cb = { NULL, NULL, NULL, NULL };
2675 int syncuuid_inserted = 0;
2676 struct berval syncUUID_strrep = BER_BVNULL;
2678 SlapReply rs_search = {REP_RESULT};
2680 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2681 int rc = LDAP_SUCCESS;
2683 struct berval pdn = BER_BVNULL;
2688 Debug( LDAP_DEBUG_SYNC,
2689 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2690 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2692 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2693 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2694 syncuuid_inserted = avl_presentlist_insert( si, syncUUID );
2698 if ( syncstate == LDAP_SYNC_PRESENT ) {
2700 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2701 if ( entry == NULL ) {
2706 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
2707 if ( syncstate != LDAP_SYNC_DELETE ) {
2708 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2711 /* add if missing */
2712 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2713 &syncUUID_strrep, syncUUID );
2715 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2716 /* replace only if necessary */
2717 if ( a->a_nvals != a->a_vals ) {
2718 ber_memfree( a->a_nvals[0].bv_val );
2719 ber_dupbv( &a->a_nvals[0], syncUUID );
2721 ber_memfree( a->a_vals[0].bv_val );
2722 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
2726 f.f_choice = LDAP_FILTER_EQUALITY;
2728 ava.aa_desc = slap_schema.si_ad_entryUUID;
2729 ava.aa_value = *syncUUID;
2731 if ( syncuuid_inserted ) {
2732 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2733 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
2735 op->ors_filter = &f;
2737 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
2738 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2739 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2740 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2741 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2742 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
2743 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2744 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2746 op->o_tag = LDAP_REQ_SEARCH;
2747 op->ors_scope = LDAP_SCOPE_SUBTREE;
2748 op->ors_deref = LDAP_DEREF_NEVER;
2750 /* get the entry for this UUID */
2751 #ifdef ENABLE_REWRITE
2752 if ( si->si_rewrite ) {
2753 op->o_req_dn = si->si_suffixm;
2754 op->o_req_ndn = si->si_suffixm;
2758 op->o_req_dn = si->si_base;
2759 op->o_req_ndn = si->si_base;
2762 op->o_time = slap_get_time();
2763 op->ors_tlimit = SLAP_NO_LIMIT;
2765 op->ors_limit = NULL;
2767 op->ors_attrs = slap_anlist_all_attributes;
2768 op->ors_attrsonly = 0;
2770 /* set callback function */
2771 op->o_callback = &cb;
2772 cb.sc_response = dn_callback;
2773 cb.sc_private = &dni;
2774 dni.new_entry = entry;
2775 dni.modlist = modlist;
2777 rc = be->be_search( op, &rs_search );
2778 Debug( LDAP_DEBUG_SYNC,
2779 "syncrepl_entry: %s be_search (%d)\n",
2780 si->si_ridtxt, rc, 0 );
2782 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2783 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2786 cb.sc_response = null_callback;
2789 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2790 Debug( LDAP_DEBUG_SYNC,
2791 "syncrepl_entry: %s %s\n",
2792 si->si_ridtxt, entry->e_name.bv_val, 0 );
2794 Debug( LDAP_DEBUG_SYNC,
2795 "syncrepl_entry: %s %s\n",
2796 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2799 assert( BER_BVISNULL( &op->o_csn ) );
2801 slap_queue_csn( op, syncCSN );
2804 slap_op_time( &op->o_time, &op->o_tincr );
2805 switch ( syncstate ) {
2807 case LDAP_SYNC_MODIFY:
2808 if ( BER_BVISNULL( &op->o_csn ))
2811 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2813 /* FIXME: op->o_csn is assumed to be
2814 * on the thread's slab; this needs
2815 * to be cleared ASAP.
2816 * What happens if already present?
2818 assert( BER_BVISNULL( &op->o_csn ) );
2819 op->o_csn = a->a_vals[0];
2824 if ( BER_BVISNULL( &dni.dn ) ) {
2825 SlapReply rs_add = {REP_RESULT};
2827 op->o_req_dn = entry->e_name;
2828 op->o_req_ndn = entry->e_nname;
2829 op->o_tag = LDAP_REQ_ADD;
2831 op->o_bd = si->si_wbe;
2833 rc = op->o_bd->be_add( op, &rs_add );
2834 Debug( LDAP_DEBUG_SYNC,
2835 "syncrepl_entry: %s be_add %s (%d)\n",
2836 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2837 switch ( rs_add.sr_err ) {
2839 if ( op->ora_e == entry ) {
2840 be_entry_release_w( op, entry );
2846 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2847 * only if the suffix entry is not present */
2848 case LDAP_NO_SUCH_OBJECT:
2849 rc = syncrepl_add_glue( op, entry );
2853 /* if an entry was added via syncrepl_add_glue(),
2854 * it likely has no entryUUID, so the previous
2855 * be_search() doesn't find it. In this case,
2856 * give syncrepl a chance to modify it. Also
2857 * allow for entries that were recreated with the
2858 * same DN but a different entryUUID.
2860 case LDAP_ALREADY_EXISTS:
2862 Operation op2 = *op;
2863 SlapReply rs2 = { REP_RESULT };
2864 slap_callback cb2 = { 0 };
2867 op2.o_tag = LDAP_REQ_SEARCH;
2868 op2.o_req_dn = entry->e_name;
2869 op2.o_req_ndn = entry->e_nname;
2870 op2.ors_scope = LDAP_SCOPE_BASE;
2871 op2.ors_deref = LDAP_DEREF_NEVER;
2872 op2.ors_attrs = slap_anlist_all_attributes;
2873 op2.ors_attrsonly = 0;
2874 op2.ors_limit = NULL;
2876 op2.ors_tlimit = SLAP_NO_LIMIT;
2878 f.f_choice = LDAP_FILTER_PRESENT;
2879 f.f_desc = slap_schema.si_ad_objectClass;
2880 op2.ors_filter = &f;
2881 op2.ors_filterstr = generic_filterstr;
2883 op2.o_callback = &cb2;
2884 cb2.sc_response = dn_callback;
2885 cb2.sc_private = &dni;
2887 rc = be->be_search( &op2, &rs2 );
2888 if ( rc ) goto done;
2891 slap_op_time( &op->o_time, &op->o_tincr );
2897 Debug( LDAP_DEBUG_ANY,
2898 "syncrepl_entry: %s be_add %s failed (%d)\n",
2899 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
2907 op->o_req_dn = dni.dn;
2908 op->o_req_ndn = dni.ndn;
2909 if ( dni.renamed ) {
2910 struct berval noldp, newp;
2911 Modifications *mod, **modtail, **ml, *m2;
2912 int i, got_replace = 0, just_rename = 0;
2913 SlapReply rs_modify = {REP_RESULT};
2915 op->o_tag = LDAP_REQ_MODRDN;
2916 dnRdn( &entry->e_name, &op->orr_newrdn );
2917 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2919 if ( !BER_BVISNULL( &dni.nnewSup )) {
2920 dnParent( &entry->e_name, &newp );
2921 op->orr_newSup = &newp;
2922 op->orr_nnewSup = &dni.nnewSup;
2924 op->orr_newSup = NULL;
2925 op->orr_nnewSup = NULL;
2927 op->orr_deleteoldrdn = dni.delOldRDN;
2928 op->orr_modlist = NULL;
2929 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2933 /* Drop the RDN-related mods from this op, because their
2934 * equivalents were just setup by slap_modrdn2mods.
2936 * If delOldRDN is TRUE then we should see a delete modop
2937 * for oldDesc. We might see a replace instead.
2938 * delete with no values: therefore newDesc != oldDesc.
2939 * if oldNattr had only one value, then Drop this op.
2940 * delete with 1 value: can only be the oldRDN value. Drop op.
2941 * delete with N values: Drop oldRDN value, keep remainder.
2942 * replace with 1 value: if oldNattr had only one value and
2943 * newDesc == oldDesc, Drop this op.
2944 * Any other cases must be left intact.
2946 * We should also see an add modop for newDesc. (But not if
2947 * we got a replace modop due to delOldRDN.) If it has
2948 * multiple values, we'll have to drop the new RDN value.
2950 modtail = &op->orr_modlist;
2951 if ( dni.delOldRDN ) {
2952 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2953 if ( (*ml)->sml_desc == dni.oldDesc ) {
2955 if ( mod->sml_op == LDAP_MOD_REPLACE &&
2956 dni.oldDesc != dni.newDesc ) {
2957 /* This Replace is due to other Mods.
2962 if ( mod->sml_numvals <= 1 &&
2963 dni.oldNattr->a_numvals == 1 &&
2964 ( mod->sml_op == LDAP_MOD_DELETE ||
2965 mod->sml_op == LDAP_MOD_REPLACE )) {
2966 if ( mod->sml_op == LDAP_MOD_REPLACE )
2969 *ml = mod->sml_next;
2970 mod->sml_next = NULL;
2971 slap_mods_free( mod, 1 );
2974 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
2976 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2977 if ( m2->sml_desc == dni.oldDesc &&
2978 m2->sml_op == LDAP_MOD_DELETE ) break;
2980 for ( i=0; i<mod->sml_numvals; i++ ) {
2981 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2983 ch_free( mod->sml_values[i].bv_val );
2984 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2985 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2986 if ( mod->sml_nvalues ) {
2987 ch_free( mod->sml_nvalues[i].bv_val );
2988 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2989 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2994 if ( !mod->sml_numvals ) {
2996 *ml = mod->sml_next;
2997 mod->sml_next = NULL;
2998 slap_mods_free( mod, 1 );
3004 if ( !got_replace ) {
3005 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
3006 if ( (*ml)->sml_desc == dni.newDesc ) {
3008 if ( mod->sml_op != LDAP_MOD_ADD )
3010 if ( mod->sml_numvals == 1 ) {
3012 *ml = mod->sml_next;
3013 mod->sml_next = NULL;
3014 slap_mods_free( mod, 1 );
3017 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
3018 if ( m2->sml_desc == dni.oldDesc &&
3019 m2->sml_op == SLAP_MOD_SOFTADD ) break;
3021 for ( i=0; i<mod->sml_numvals; i++ ) {
3022 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
3024 ch_free( mod->sml_values[i].bv_val );
3025 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
3026 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3027 if ( mod->sml_nvalues ) {
3028 ch_free( mod->sml_nvalues[i].bv_val );
3029 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3030 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3040 /* RDNs must be NUL-terminated for back-ldap */
3041 noldp = op->orr_newrdn;
3042 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
3043 noldp = op->orr_nnewrdn;
3044 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
3046 /* Setup opattrs too */
3048 static AttributeDescription *nullattr = NULL;
3049 static AttributeDescription **const opattrs[] = {
3050 &slap_schema.si_ad_entryCSN,
3051 &slap_schema.si_ad_modifiersName,
3052 &slap_schema.si_ad_modifyTimestamp,
3055 AttributeDescription *opattr;
3059 /* pull mod off incoming modlist */
3060 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
3061 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
3063 if ( (*ml)->sml_desc == opattr ) {
3065 *ml = mod->sml_next;
3066 mod->sml_next = NULL;
3068 modtail = &mod->sml_next;
3073 /* If there are still Modifications left, put the opattrs
3074 * back, and let be_modify run. Otherwise, append the opattrs
3075 * to the orr_modlist.
3079 /* don't set a CSN for the rename op */
3081 slap_graduate_commit_csn( op );
3083 mod = op->orr_modlist;
3086 for ( ; mod->sml_next; mod=mod->sml_next );
3089 op->o_bd = si->si_wbe;
3091 rs_reinit( &rs_modify, REP_RESULT );
3092 rc = op->o_bd->be_modrdn( op, &rs_modify );
3094 /* NOTE: noSuchObject should result because the new superior
3095 * has not been added yet (ITS#6472) */
3096 if ( rc == LDAP_NO_SUCH_OBJECT && op->orr_nnewSup != NULL ) {
3097 Operation op2 = *op;
3098 rc = syncrepl_add_glue_ancestors( &op2, entry );
3099 if ( rc == LDAP_SUCCESS ) {
3104 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
3105 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
3107 slap_mods_free( op->orr_modlist, 1 );
3108 Debug( LDAP_DEBUG_SYNC,
3109 "syncrepl_entry: %s be_modrdn %s (%d)\n",
3110 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3112 /* Renamed entries may still have other mods so just fallthru */
3113 op->o_req_dn = entry->e_name;
3114 op->o_req_ndn = entry->e_nname;
3115 /* Use CSN on the modify */
3119 slap_queue_csn( op, syncCSN );
3122 SlapReply rs_modify = {REP_RESULT};
3124 op->o_tag = LDAP_REQ_MODIFY;
3125 op->orm_modlist = dni.mods;
3126 op->orm_no_opattrs = 1;
3127 op->o_bd = si->si_wbe;
3129 rc = op->o_bd->be_modify( op, &rs_modify );
3130 slap_mods_free( op->orm_modlist, 1 );
3131 op->orm_no_opattrs = 0;
3132 Debug( LDAP_DEBUG_SYNC,
3133 "syncrepl_entry: %s be_modify %s (%d)\n",
3134 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3135 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
3136 Debug( LDAP_DEBUG_ANY,
3137 "syncrepl_entry: %s be_modify failed (%d)\n",
3138 si->si_ridtxt, rs_modify.sr_err, 0 );
3142 } else if ( !dni.renamed ) {
3143 Debug( LDAP_DEBUG_SYNC,
3144 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
3145 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
3147 slap_graduate_commit_csn( op );
3152 case LDAP_SYNC_DELETE :
3153 if ( !BER_BVISNULL( &dni.dn ) ) {
3154 SlapReply rs_delete = {REP_RESULT};
3155 op->o_req_dn = dni.dn;
3156 op->o_req_ndn = dni.ndn;
3157 op->o_tag = LDAP_REQ_DELETE;
3158 op->o_bd = si->si_wbe;
3159 rc = op->o_bd->be_delete( op, &rs_delete );
3160 Debug( LDAP_DEBUG_SYNC,
3161 "syncrepl_entry: %s be_delete %s (%d)\n",
3162 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3164 while ( rs_delete.sr_err == LDAP_SUCCESS
3165 && op->o_delete_glue_parent ) {
3166 op->o_delete_glue_parent = 0;
3167 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3168 slap_callback cb = { NULL };
3169 cb.sc_response = slap_null_cb;
3170 dnParent( &op->o_req_ndn, &pdn );
3172 op->o_req_ndn = pdn;
3173 op->o_callback = &cb;
3174 rs_reinit( &rs_delete, REP_RESULT );
3175 op->o_bd->be_delete( op, &rs_delete );
3186 Debug( LDAP_DEBUG_ANY,
3187 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
3192 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
3193 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
3194 BER_BVZERO( &syncUUID_strrep );
3196 if ( !BER_BVISNULL( &dni.ndn ) ) {
3197 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
3199 if ( !BER_BVISNULL( &dni.dn ) ) {
3200 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
3203 entry_free( entry );
3206 slap_graduate_commit_csn( op );
3208 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
3209 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3211 BER_BVZERO( &op->o_csn );
3215 static struct berval gcbva[] = {
3221 #define NP_DELETE_ONE 2
3224 syncrepl_del_nonpresent(
3228 struct sync_cookie *sc,
3231 Backend* be = op->o_bd;
3232 slap_callback cb = { NULL };
3233 struct nonpresent_entry *np_list, *np_prev;
3235 AttributeName an[2];
3237 struct berval pdn = BER_BVNULL;
3240 #ifdef ENABLE_REWRITE
3241 if ( si->si_rewrite ) {
3242 op->o_req_dn = si->si_suffixm;
3243 op->o_req_ndn = si->si_suffixm;
3247 op->o_req_dn = si->si_base;
3248 op->o_req_ndn = si->si_base;
3251 cb.sc_response = nonpresent_callback;
3254 op->o_callback = &cb;
3255 op->o_tag = LDAP_REQ_SEARCH;
3256 op->ors_scope = si->si_scope;
3257 op->ors_deref = LDAP_DEREF_NEVER;
3258 op->o_time = slap_get_time();
3259 op->ors_tlimit = SLAP_NO_LIMIT;
3264 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
3267 op->ors_attrsonly = 1;
3268 op->ors_attrs = slap_anlist_no_attrs;
3269 op->ors_limit = NULL;
3270 op->ors_filter = &uf;
3273 uf.f_av_desc = slap_schema.si_ad_entryUUID;
3275 uf.f_choice = LDAP_FILTER_EQUALITY;
3276 si->si_refreshDelete |= NP_DELETE_ONE;
3278 for (i=0; uuids[i].bv_val; i++) {
3279 SlapReply rs_search = {REP_RESULT};
3282 uf.f_av_value = uuids[i];
3283 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3284 rc = be->be_search( op, &rs_search );
3285 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3287 si->si_refreshDelete ^= NP_DELETE_ONE;
3291 AttributeAssertion mmaa;
3292 SlapReply rs_search = {REP_RESULT};
3294 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
3295 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
3296 an[0].an_desc = slap_schema.si_ad_entryUUID;
3298 op->ors_slimit = SLAP_NO_LIMIT;
3299 op->ors_tlimit = SLAP_NO_LIMIT;
3300 op->ors_limit = NULL;
3301 op->ors_attrsonly = 0;
3302 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
3303 /* In multimaster, updates can continue to arrive while
3304 * we're searching. Limit the search result to entries
3305 * older than our newest cookie CSN.
3307 if ( SLAP_MULTIMASTER( op->o_bd )) {
3312 f->f_choice = LDAP_FILTER_AND;
3313 f->f_next = op->ors_filter;
3317 f->f_choice = LDAP_FILTER_LE;
3319 f->f_av_desc = slap_schema.si_ad_entryCSN;
3321 BER_BVZERO( &f->f_av_value );
3322 for ( i=0; i<sc->numcsns; i++ ) {
3323 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
3324 f->f_av_value = sc->ctxcsn[i];
3326 of = op->ors_filter;
3327 op->ors_filter = mmf;
3328 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3331 op->ors_filterstr = si->si_filterstr;
3333 op->o_nocaching = 1;
3336 rc = be->be_search( op, &rs_search );
3337 if ( SLAP_MULTIMASTER( op->o_bd )) {
3338 op->ors_filter = of;
3340 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
3341 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
3342 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3347 op->o_nocaching = 0;
3349 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
3351 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
3352 csn = sc->ctxcsn[m];
3354 csn = si->si_syncCookie.ctxcsn[0];
3357 op->o_bd = si->si_wbe;
3358 slap_queue_csn( op, &csn );
3360 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
3361 while ( np_list != NULL ) {
3362 SlapReply rs_delete = {REP_RESULT};
3364 LDAP_LIST_REMOVE( np_list, npe_link );
3366 np_list = LDAP_LIST_NEXT( np_list, npe_link );
3367 op->o_tag = LDAP_REQ_DELETE;
3368 op->o_callback = &cb;
3369 cb.sc_response = null_callback;
3371 op->o_req_dn = *np_prev->npe_name;
3372 op->o_req_ndn = *np_prev->npe_nname;
3373 rc = op->o_bd->be_delete( op, &rs_delete );
3374 Debug( LDAP_DEBUG_SYNC,
3375 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
3376 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3378 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
3379 SlapReply rs_modify = {REP_RESULT};
3380 Modifications mod1, mod2;
3381 mod1.sml_op = LDAP_MOD_REPLACE;
3383 mod1.sml_desc = slap_schema.si_ad_objectClass;
3384 mod1.sml_type = mod1.sml_desc->ad_cname;
3385 mod1.sml_numvals = 2;
3386 mod1.sml_values = &gcbva[0];
3387 mod1.sml_nvalues = NULL;
3388 mod1.sml_next = &mod2;
3390 mod2.sml_op = LDAP_MOD_REPLACE;
3392 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
3393 mod2.sml_type = mod2.sml_desc->ad_cname;
3394 mod2.sml_numvals = 1;
3395 mod2.sml_values = &gcbva[1];
3396 mod2.sml_nvalues = NULL;
3397 mod2.sml_next = NULL;
3399 op->o_tag = LDAP_REQ_MODIFY;
3400 op->orm_modlist = &mod1;
3402 rc = op->o_bd->be_modify( op, &rs_modify );
3403 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
3406 while ( rs_delete.sr_err == LDAP_SUCCESS &&
3407 op->o_delete_glue_parent ) {
3408 op->o_delete_glue_parent = 0;
3409 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3410 slap_callback cb = { NULL };
3411 cb.sc_response = slap_null_cb;
3412 dnParent( &op->o_req_ndn, &pdn );
3414 op->o_req_ndn = pdn;
3415 op->o_callback = &cb;
3416 rs_reinit( &rs_delete, REP_RESULT );
3417 /* give it a root privil ? */
3418 op->o_bd->be_delete( op, &rs_delete );
3424 op->o_delete_glue_parent = 0;
3426 ber_bvfree( np_prev->npe_name );
3427 ber_bvfree( np_prev->npe_nname );
3430 if ( slapd_shutdown ) {
3435 slap_graduate_commit_csn( op );
3438 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3439 BER_BVZERO( &op->o_csn );
3446 syncrepl_add_glue_ancestors(
3450 Backend *be = op->o_bd;
3451 slap_callback cb = { NULL };
3453 int rc = LDAP_SUCCESS;
3456 struct berval dn = BER_BVNULL;
3457 struct berval ndn = BER_BVNULL;
3459 struct berval ptr, nptr;
3462 op->o_tag = LDAP_REQ_ADD;
3463 op->o_callback = &cb;
3464 cb.sc_response = null_callback;
3465 cb.sc_private = NULL;
3470 /* count RDNs in suffix */
3471 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3472 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3474 ptr.bv_len -= comma - ptr.bv_val;
3484 /* Start with BE suffix */
3486 for ( i = 0; i < suffrdns; i++ ) {
3487 comma = ber_bvrchr( &ptr, ',' );
3488 if ( comma != NULL ) {
3489 ptr.bv_len = comma - ptr.bv_val;
3496 if ( !BER_BVISEMPTY( &ptr ) ) {
3497 dn.bv_len -= ptr.bv_len + 1;
3498 dn.bv_val += ptr.bv_len + 1;
3501 /* the normalizedDNs are always the same length, no counting
3505 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3506 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3507 ndn.bv_len = be->be_nsuffix[0].bv_len;
3509 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3515 while ( ndn.bv_val > e->e_nname.bv_val ) {
3516 SlapReply rs_add = {REP_RESULT};
3518 glue = entry_alloc();
3519 ber_dupbv( &glue->e_name, &dn );
3520 ber_dupbv( &glue->e_nname, &ndn );
3522 a = attr_alloc( slap_schema.si_ad_objectClass );
3525 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3526 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3527 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3528 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3530 a->a_nvals = a->a_vals;
3532 a->a_next = glue->e_attrs;
3535 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3538 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3539 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3540 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3542 a->a_nvals = a->a_vals;
3544 a->a_next = glue->e_attrs;
3547 op->o_req_dn = glue->e_name;
3548 op->o_req_ndn = glue->e_nname;
3550 rc = be->be_add ( op, &rs_add );
3551 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3552 if ( op->ora_e == glue )
3553 be_entry_release_w( op, glue );
3555 /* incl. ALREADY EXIST */
3557 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3563 /* Move to next child */
3564 comma = ber_bvrchr( &ptr, ',' );
3565 if ( comma == NULL ) {
3568 ptr.bv_len = comma - ptr.bv_val;
3570 dn.bv_val = ++comma;
3571 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3573 comma = ber_bvrchr( &nptr, ',' );
3574 assert( comma != NULL );
3575 nptr.bv_len = comma - nptr.bv_val;
3577 ndn.bv_val = ++comma;
3578 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3589 slap_callback cb = { NULL };
3591 Backend *be = op->o_bd;
3592 SlapReply rs_add = {REP_RESULT};
3594 rc = syncrepl_add_glue_ancestors( op, e );
3597 case LDAP_ALREADY_EXISTS:
3604 op->o_tag = LDAP_REQ_ADD;
3605 op->o_callback = &cb;
3606 cb.sc_response = null_callback;
3607 cb.sc_private = NULL;
3609 op->o_req_dn = e->e_name;
3610 op->o_req_ndn = e->e_nname;
3612 rc = be->be_add ( op, &rs_add );
3613 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3614 if ( op->ora_e == e )
3615 be_entry_release_w( op, e );
3624 syncrepl_updateCookie(
3627 struct sync_cookie *syncCookie )
3629 Backend *be = op->o_bd;
3631 struct berval first = BER_BVNULL;
3632 struct sync_cookie sc;
3634 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3637 int rc, i, j, changed = 0;
3640 slap_callback cb = { NULL };
3641 SlapReply rs_modify = {REP_RESULT};
3643 mod.sml_op = LDAP_MOD_REPLACE;
3644 mod.sml_desc = slap_schema.si_ad_contextCSN;
3645 mod.sml_type = mod.sml_desc->ad_cname;
3646 mod.sml_flags = SLAP_MOD_INTERNAL;
3647 mod.sml_nvalues = NULL;
3648 mod.sml_next = NULL;
3650 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3653 for ( i=0; i<syncCookie->numcsns; i++ ) {
3654 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3656 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3657 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3661 /* clone the cookieState CSNs so we can Replace the whole thing */
3662 sc.numcsns = si->si_cookieState->cs_num;
3664 ber_bvarray_dup_x( &sc.ctxcsn, si->si_cookieState->cs_vals, NULL );
3665 sc.sids = ch_malloc( sc.numcsns * sizeof(int));
3666 for ( i=0; i<sc.numcsns; i++ )
3667 sc.sids[i] = si->si_cookieState->cs_sids[i];
3673 /* find any CSNs in the syncCookie that are newer than the cookieState */
3674 for ( i=0; i<syncCookie->numcsns; i++ ) {
3675 for ( j=0; j<sc.numcsns; j++ ) {
3676 if ( syncCookie->sids[i] < sc.sids[j] )
3678 if ( syncCookie->sids[i] != sc.sids[j] )
3680 len = syncCookie->ctxcsn[i].bv_len;
3681 if ( len > sc.ctxcsn[j].bv_len )
3682 len = sc.ctxcsn[j].bv_len;
3683 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3684 sc.ctxcsn[j].bv_val, len ) > 0 ) {
3685 ber_bvreplace( &sc.ctxcsn[j], &syncCookie->ctxcsn[i] );
3687 if ( BER_BVISNULL( &first ) ||
3688 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3689 first = syncCookie->ctxcsn[i];
3694 /* there was no match for this SID, it's a new CSN */
3695 if ( j == sc.numcsns ||
3696 syncCookie->sids[i] != sc.sids[j] ) {
3697 slap_insert_csn_sids( &sc, j, syncCookie->sids[i],
3698 &syncCookie->ctxcsn[i] );
3699 if ( BER_BVISNULL( &first ) ||
3700 memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 ) {
3701 first = syncCookie->ctxcsn[i];
3706 /* Should never happen, ITS#5065 */
3707 if ( BER_BVISNULL( &first ) || !changed ) {
3708 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3709 ber_bvarray_free( sc.ctxcsn );
3713 op->o_bd = si->si_wbe;
3714 slap_queue_csn( op, &first );
3716 op->o_tag = LDAP_REQ_MODIFY;
3718 cb.sc_response = null_callback;
3721 op->o_callback = &cb;
3722 op->o_req_dn = si->si_contextdn;
3723 op->o_req_ndn = si->si_contextdn;
3725 /* update contextCSN */
3726 op->o_dont_replicate = 1;
3728 mod.sml_numvals = sc.numcsns;
3729 mod.sml_values = sc.ctxcsn;
3731 op->orm_modlist = &mod;
3732 op->orm_no_opattrs = 1;
3733 rc = op->o_bd->be_modify( op, &rs_modify );
3735 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3736 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3738 char txtbuf[SLAP_TEXT_BUFLEN];
3739 size_t textlen = sizeof txtbuf;
3740 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3741 rs_reinit( &rs_modify, REP_RESULT );
3742 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3744 rc = op->o_bd->be_add( op, &rs_modify );
3745 if ( e == op->ora_e )
3746 be_entry_release_w( op, op->ora_e );
3749 op->orm_no_opattrs = 0;
3750 op->o_dont_replicate = 0;
3752 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3753 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3754 ber_bvarray_free( si->si_cookieState->cs_vals );
3755 ch_free( si->si_cookieState->cs_sids );
3756 si->si_cookieState->cs_vals = sc.ctxcsn;
3757 si->si_cookieState->cs_sids = sc.sids;
3758 si->si_cookieState->cs_num = sc.numcsns;
3760 /* Don't just dup the provider's cookie, recreate it */
3761 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
3762 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn, si->si_cookieState->cs_vals, NULL );
3763 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num * sizeof(int) );
3764 for ( i=0; i<si->si_cookieState->cs_num; i++ )
3765 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
3767 si->si_cookieState->cs_age++;
3768 si->si_cookieAge = si->si_cookieState->cs_age;
3770 Debug( LDAP_DEBUG_ANY,
3771 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3772 si->si_ridtxt, rs_modify.sr_err, 0 );
3774 ber_bvarray_free( sc.ctxcsn );
3776 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3779 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3780 BER_BVZERO( &op->o_csn );
3781 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3784 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3785 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3792 /* Compare the attribute from the old entry to the one in the new
3793 * entry. The Modifications from the new entry will either be left
3794 * in place, or changed to an Add or Delete as needed.
3797 attr_cmp( Operation *op, Attribute *old, Attribute *new,
3798 Modifications ***mret, Modifications ***mcur )
3801 Modifications *mod, **modtail;
3807 struct berval **adds, **dels;
3808 /* count old and new */
3809 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
3810 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
3812 /* there MUST be both old and new values */
3817 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
3818 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
3820 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
3821 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
3825 for ( i=0; i<o; i++ ) {
3826 for ( j=0; j<n; j++ ) {
3829 if ( bvmatch( dels[i], adds[j] ) ) {
3839 /* Don't delete/add an objectClass, always use the replace op.
3840 * Modify would fail if provider has replaced entry with a new,
3841 * and the new explicitly includes a superior of a class that was
3842 * only included implicitly in the old entry. Ref ITS#5517.
3844 * Also use replace op if attr has no equality matching rule.
3847 if ( ( nn || ( no > 0 && no < o ) ) &&
3848 ( old->a_desc == slap_schema.si_ad_objectClass ||
3849 !old->a_desc->ad_type->sat_equality ) )
3855 /* all old values were deleted, just use the replace op */
3859 /* delete some values */
3860 mod = ch_malloc( sizeof( Modifications ) );
3861 mod->sml_op = LDAP_MOD_DELETE;
3863 mod->sml_desc = old->a_desc;
3864 mod->sml_type = mod->sml_desc->ad_cname;
3865 mod->sml_numvals = no;
3866 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3867 if ( old->a_vals != old->a_nvals ) {
3868 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3870 mod->sml_nvalues = NULL;
3873 for ( i = 0; i < o; i++ ) {
3874 if ( !dels[i] ) continue;
3875 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
3876 if ( mod->sml_nvalues ) {
3877 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
3881 BER_BVZERO( &mod->sml_values[j] );
3882 if ( mod->sml_nvalues ) {
3883 BER_BVZERO( &mod->sml_nvalues[j] );
3886 modtail = &mod->sml_next;
3889 op->o_tmpfree( dels, op->o_tmpmemctx );
3890 /* some values were added */
3891 if ( nn && no < o ) {
3892 mod = ch_malloc( sizeof( Modifications ) );
3893 mod->sml_op = LDAP_MOD_ADD;
3895 mod->sml_desc = old->a_desc;
3896 mod->sml_type = mod->sml_desc->ad_cname;
3897 mod->sml_numvals = nn;
3898 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3899 if ( old->a_vals != old->a_nvals ) {
3900 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3902 mod->sml_nvalues = NULL;
3905 for ( i = 0; i < n; i++ ) {
3906 if ( !adds[i] ) continue;
3907 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
3908 if ( mod->sml_nvalues ) {
3909 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
3913 BER_BVZERO( &mod->sml_values[j] );
3914 if ( mod->sml_nvalues ) {
3915 BER_BVZERO( &mod->sml_nvalues[j] );
3918 modtail = &mod->sml_next;
3921 op->o_tmpfree( adds, op->o_tmpmemctx );
3923 /* new attr, just use the new mod */
3927 /* advance to next element */
3931 **mcur = mod->sml_next;
3933 modtail = &mod->sml_next;
3935 *mcur = &mod->sml_next;
3941 /* Generate a set of modifications to change the old entry into the
3942 * new one. On input ml is a list of modifications equivalent to
3943 * the new entry. It will be massaged and the result will be stored
3946 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
3947 Modifications **mods, Modifications **ml, int is_ctx)
3949 Modifications **modtail = mods;
3951 /* We assume that attributes are saved in the same order
3952 * in the remote and local databases. So if we walk through
3953 * the attributeDescriptions one by one they should match in
3954 * lock step. If not, look for an add or delete.
3956 while ( old && new )
3958 /* If we've seen this before, use its mod now */
3959 if ( new->a_flags & SLAP_ATTR_IXADD ) {
3960 attr_cmp( op, NULL, new, &modtail, &ml );
3964 /* Skip contextCSN */
3965 if ( is_ctx && old->a_desc ==
3966 slap_schema.si_ad_contextCSN ) {
3971 if ( old->a_desc != new->a_desc ) {
3975 /* If it's just been re-added later,
3976 * remember that we've seen it.
3978 tmp = attr_find( new, old->a_desc );
3980 tmp->a_flags |= SLAP_ATTR_IXADD;
3982 /* If it's a new attribute, pull it in.
3984 tmp = attr_find( old, new->a_desc );
3986 attr_cmp( op, NULL, new, &modtail, &ml );
3990 /* Delete old attr */
3991 mod = ch_malloc( sizeof( Modifications ) );
3992 mod->sml_op = LDAP_MOD_DELETE;
3994 mod->sml_desc = old->a_desc;
3995 mod->sml_type = mod->sml_desc->ad_cname;
3996 mod->sml_numvals = 0;
3997 mod->sml_values = NULL;
3998 mod->sml_nvalues = NULL;
4000 modtail = &mod->sml_next;
4005 /* kludge - always update modifiersName so that it
4006 * stays co-located with the other mod opattrs. But only
4007 * if we know there are other valid mods.
4009 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
4010 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
4011 attr_cmp( op, NULL, new, &modtail, &ml );
4013 attr_cmp( op, old, new, &modtail, &ml );
4026 dninfo *dni = op->o_callback->sc_private;
4028 if ( rs->sr_type == REP_SEARCH ) {
4029 if ( !BER_BVISNULL( &dni->dn ) ) {
4030 Debug( LDAP_DEBUG_ANY,
4031 "dn_callback : consistency error - "
4032 "entryUUID is not unique\n", 0, 0, 0 );
4034 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
4035 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
4036 /* If there is a new entry, see if it differs from the old.
4037 * We compare the non-normalized values so that cosmetic changes
4038 * in the provider are always propagated.
4040 if ( dni->new_entry ) {
4041 Attribute *old, *new;
4042 struct berval old_rdn, new_rdn;
4043 struct berval old_p, new_p;
4044 int is_ctx, new_sup = 0;
4046 /* If old entry is not a glue entry, make sure new entry
4047 * is actually newer than old entry
4049 if ( !is_entry_glue( rs->sr_entry )) {
4050 old = attr_find( rs->sr_entry->e_attrs,
4051 slap_schema.si_ad_entryCSN );
4052 new = attr_find( dni->new_entry->e_attrs,
4053 slap_schema.si_ad_entryCSN );
4056 ber_len_t len = old->a_vals[0].bv_len;
4057 if ( len > new->a_vals[0].bv_len )
4058 len = new->a_vals[0].bv_len;
4059 rc = memcmp( old->a_vals[0].bv_val,
4060 new->a_vals[0].bv_val, len );
4062 Debug( LDAP_DEBUG_SYNC,
4063 "dn_callback : new entry is older than ours "
4064 "%s ours %s, new %s\n",
4065 rs->sr_entry->e_name.bv_val,
4066 old->a_vals[0].bv_val,
4067 new->a_vals[0].bv_val );
4068 return LDAP_SUCCESS;
4069 } else if ( rc == 0 ) {
4070 Debug( LDAP_DEBUG_SYNC,
4071 "dn_callback : entries have identical CSN "
4073 rs->sr_entry->e_name.bv_val,
4074 old->a_vals[0].bv_val, 0 );
4075 return LDAP_SUCCESS;
4080 is_ctx = dn_match( &rs->sr_entry->e_nname,
4081 &op->o_bd->be_nsuffix[0] );
4083 /* Did the DN change?
4084 * case changes in the parent are ignored,
4085 * we only want to know if the RDN was
4088 dnRdn( &rs->sr_entry->e_name, &old_rdn );
4089 dnRdn( &dni->new_entry->e_name, &new_rdn );
4090 dnParent( &rs->sr_entry->e_nname, &old_p );
4091 dnParent( &dni->new_entry->e_nname, &new_p );
4093 new_sup = !dn_match( &old_p, &new_p );
4094 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
4096 struct berval oldRDN, oldVal;
4097 AttributeDescription *ad = NULL;
4103 dni->nnewSup = new_p;
4105 /* See if the oldRDN was deleted */
4106 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
4107 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
4108 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
4110 oldRDN.bv_len -= oldVal.bv_len + 1;
4111 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4113 for ( oldpos=0, a=rs->sr_entry->e_attrs;
4114 a && a->a_desc != ad; oldpos++, a=a->a_next );
4116 for ( newpos=0, a=dni->new_entry->e_attrs;
4117 a && a->a_desc != ad; newpos++, a=a->a_next );
4118 if ( !a || oldpos != newpos || attr_valfind( a,
4119 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
4120 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
4121 SLAP_MR_VALUE_OF_SYNTAX,
4122 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
4126 /* Get the newRDN's desc */
4127 dnRdn( &dni->new_entry->e_nname, &oldRDN );
4128 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
4129 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
4131 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4134 /* A ModDN has happened, but in Refresh mode other
4135 * changes may have occurred before we picked it up.
4136 * So fallthru to regular Modify processing.
4140 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
4141 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
4145 } else if ( rs->sr_type == REP_RESULT ) {
4146 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
4147 Debug( LDAP_DEBUG_ANY,
4148 "dn_callback : consistency error - "
4149 "entryUUID is not unique\n", 0, 0, 0 );
4153 return LDAP_SUCCESS;
4157 nonpresent_callback(
4161 syncinfo_t *si = op->o_callback->sc_private;
4164 struct berval* present_uuid = NULL;
4165 struct nonpresent_entry *np_entry;
4167 if ( rs->sr_type == REP_RESULT ) {
4168 count = avl_free( si->si_presentlist, ch_free );
4169 si->si_presentlist = NULL;
4171 } else if ( rs->sr_type == REP_SEARCH ) {
4172 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
4173 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
4176 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
4180 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
4181 char buf[sizeof("rid=999 non")];
4183 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
4184 present_uuid ? "" : "non" );
4186 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
4187 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
4190 if ( a == NULL ) return 0;
4193 if ( present_uuid == NULL ) {
4194 np_entry = (struct nonpresent_entry *)
4195 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
4196 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
4197 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
4198 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
4201 avl_delete( &si->si_presentlist,
4202 &a->a_nvals[0], syncuuid_cmp );
4203 ch_free( present_uuid );
4206 return LDAP_SUCCESS;
4214 if ( rs->sr_err != LDAP_SUCCESS &&
4215 rs->sr_err != LDAP_REFERRAL &&
4216 rs->sr_err != LDAP_ALREADY_EXISTS &&
4217 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
4218 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
4220 Debug( LDAP_DEBUG_ANY,
4221 "null_callback : error code 0x%x\n",
4224 return LDAP_SUCCESS;
4227 static struct berval *
4228 slap_uuidstr_from_normalized(
4229 struct berval* uuidstr,
4230 struct berval* normalized,
4235 unsigned char nibble;
4238 if ( normalized == NULL ) return NULL;
4239 if ( normalized->bv_len != 16 ) return NULL;
4244 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4245 if ( new == NULL ) {
4252 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4253 if ( new != uuidstr ) {
4254 slap_sl_free( new, ctx );
4259 for ( i = 0; i < 16; i++ ) {
4260 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
4261 new->bv_val[(i<<1)+d] = '-';
4265 nibble = (normalized->bv_val[i] >> 4) & 0xF;
4266 if ( nibble < 10 ) {
4267 new->bv_val[(i<<1)+d] = nibble + '0';
4269 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
4272 nibble = (normalized->bv_val[i]) & 0xF;
4273 if ( nibble < 10 ) {
4274 new->bv_val[(i<<1)+d+1] = nibble + '0';
4276 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
4280 new->bv_val[new->bv_len] = '\0';
4287 if ( normalized == NULL ) return NULL;
4288 if ( normalized->bv_len != 16 ) return NULL;
4294 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4295 if ( new == NULL ) {
4302 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4307 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
4308 normalized->bv_len, new->bv_val, new->bv_len + 1 );
4312 if ( new != NULL ) {
4313 if ( new->bv_val != NULL ) {
4314 slap_sl_free( new->bv_val, ctx );
4317 if ( new != uuidstr ) {
4318 slap_sl_free( new, ctx );
4331 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
4333 const struct berval *uuid1 = v_uuid1;
4334 const struct berval *uuid2 = v_uuid2;
4335 int rc = uuid1->bv_len - uuid2->bv_len;
4336 if ( rc ) return rc;
4337 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
4341 syncinfo_free( syncinfo_t *sie, int free_all )
4343 syncinfo_t *si_next;
4345 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
4346 sie->si_ridtxt, 0, 0 );
4349 si_next = sie->si_next;
4352 if ( sie->si_conn ) {
4353 connection_client_stop( sie->si_conn );
4354 sie->si_conn = NULL;
4356 ldap_unbind_ext( sie->si_ld, NULL, NULL );
4360 struct re_s *re = sie->si_re;
4363 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4364 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
4365 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
4366 ldap_pvt_runqueue_remove( &slapd_rq, re );
4367 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4370 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
4372 bindconf_free( &sie->si_bindconf );
4374 if ( sie->si_filterstr.bv_val ) {
4375 ch_free( sie->si_filterstr.bv_val );
4377 if ( sie->si_filter ) {
4378 filter_free( sie->si_filter );
4380 if ( sie->si_logfilterstr.bv_val ) {
4381 ch_free( sie->si_logfilterstr.bv_val );
4383 if ( sie->si_base.bv_val ) {
4384 ch_free( sie->si_base.bv_val );
4386 if ( sie->si_logbase.bv_val ) {
4387 ch_free( sie->si_logbase.bv_val );
4389 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
4390 ch_free( sie->si_contextdn.bv_val );
4392 if ( sie->si_attrs ) {
4394 while ( sie->si_attrs[i] != NULL ) {
4395 ch_free( sie->si_attrs[i] );
4398 ch_free( sie->si_attrs );
4400 if ( sie->si_exattrs ) {
4402 while ( sie->si_exattrs[i] != NULL ) {
4403 ch_free( sie->si_exattrs[i] );
4406 ch_free( sie->si_exattrs );
4408 if ( sie->si_anlist ) {
4410 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
4411 ch_free( sie->si_anlist[i].an_name.bv_val );
4414 ch_free( sie->si_anlist );
4416 if ( sie->si_exanlist ) {
4418 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
4419 ch_free( sie->si_exanlist[i].an_name.bv_val );
4422 ch_free( sie->si_exanlist );
4424 if ( sie->si_retryinterval ) {
4425 ch_free( sie->si_retryinterval );
4427 if ( sie->si_retrynum ) {
4428 ch_free( sie->si_retrynum );
4430 if ( sie->si_retrynum_init ) {
4431 ch_free( sie->si_retrynum_init );
4433 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
4434 if ( sie->si_presentlist ) {
4435 avl_free( sie->si_presentlist, ch_free );
4437 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
4438 struct nonpresent_entry* npe;
4439 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
4440 LDAP_LIST_REMOVE( npe, npe_link );
4441 if ( npe->npe_name ) {
4442 if ( npe->npe_name->bv_val ) {
4443 ch_free( npe->npe_name->bv_val );
4445 ch_free( npe->npe_name );
4447 if ( npe->npe_nname ) {
4448 if ( npe->npe_nname->bv_val ) {
4449 ch_free( npe->npe_nname->bv_val );
4451 ch_free( npe->npe_nname );
4455 if ( sie->si_cookieState ) {
4456 sie->si_cookieState->cs_ref--;
4457 if ( !sie->si_cookieState->cs_ref ) {
4458 ch_free( sie->si_cookieState->cs_sids );
4459 ber_bvarray_free( sie->si_cookieState->cs_vals );
4460 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
4461 ch_free( sie->si_cookieState->cs_psids );
4462 ber_bvarray_free( sie->si_cookieState->cs_pvals );
4463 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
4464 ch_free( sie->si_cookieState );
4467 #ifdef ENABLE_REWRITE
4468 if ( sie->si_rewrite )
4469 rewrite_info_delete( &sie->si_rewrite );
4470 if ( sie->si_suffixm.bv_val )
4471 ch_free( sie->si_suffixm.bv_val );
4475 } while ( free_all && si_next );
4478 #ifdef ENABLE_REWRITE
4480 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4482 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4483 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4484 char *argvRule[] = { "rewriteRule", NULL, NULL, ":", NULL };
4488 if ( si->si_rewrite )
4489 rewrite_info_delete( &si->si_rewrite );
4490 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4492 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4493 if ( rc != LDAP_SUCCESS )
4496 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4497 if ( rc != LDAP_SUCCESS )
4500 vnc = ch_malloc( si->si_base.bv_len + 6 );
4501 strcpy( vnc, "(.*)" );
4502 lutil_strcopy( lutil_strcopy( vnc+4, si->si_base.bv_val ), "$" );
4505 rnc = ch_malloc( si->si_suffixm.bv_len + 3 );
4506 strcpy( rnc, "%1" );
4507 strcpy( rnc+2, si->si_suffixm.bv_val );
4510 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4517 /* NOTE: used & documented in slapd.conf(5) */
4519 #define PROVIDERSTR "provider"
4520 #define SCHEMASTR "schemachecking"
4521 #define FILTERSTR "filter"
4522 #define SEARCHBASESTR "searchbase"
4523 #define SCOPESTR "scope"
4524 #define ATTRSONLYSTR "attrsonly"
4525 #define ATTRSSTR "attrs"
4526 #define TYPESTR "type"
4527 #define INTERVALSTR "interval"
4528 #define RETRYSTR "retry"
4529 #define SLIMITSTR "sizelimit"
4530 #define TLIMITSTR "timelimit"
4531 #define SYNCDATASTR "syncdata"
4532 #define LOGBASESTR "logbase"
4533 #define LOGFILTERSTR "logfilter"
4534 #define SUFFIXMSTR "suffixmassage"
4535 #define STRICT_REFRESH "strictrefresh"
4537 /* FIXME: undocumented */
4538 #define EXATTRSSTR "exattrs"
4539 #define MANAGEDSAITSTR "manageDSAit"
4543 GOT_RID = 0x00000001U,
4544 GOT_PROVIDER = 0x00000002U,
4545 GOT_SCHEMACHECKING = 0x00000004U,
4546 GOT_FILTER = 0x00000008U,
4547 GOT_SEARCHBASE = 0x00000010U,
4548 GOT_SCOPE = 0x00000020U,
4549 GOT_ATTRSONLY = 0x00000040U,
4550 GOT_ATTRS = 0x00000080U,
4551 GOT_TYPE = 0x00000100U,
4552 GOT_INTERVAL = 0x00000200U,
4553 GOT_RETRY = 0x00000400U,
4554 GOT_SLIMIT = 0x00000800U,
4555 GOT_TLIMIT = 0x00001000U,
4556 GOT_SYNCDATA = 0x00002000U,
4557 GOT_LOGBASE = 0x00004000U,
4558 GOT_LOGFILTER = 0x00008000U,
4559 GOT_EXATTRS = 0x00010000U,
4560 GOT_MANAGEDSAIT = 0x00020000U,
4561 GOT_BINDCONF = 0x00040000U,
4562 GOT_SUFFIXM = 0x00080000U,
4565 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4568 static slap_verbmasks datamodes[] = {
4569 { BER_BVC("default"), SYNCDATA_DEFAULT },
4570 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4571 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4576 parse_syncrepl_retry(
4583 int use_default = 0;
4585 char *val = arg + STRLENOF( RETRYSTR "=" );
4586 if ( strcasecmp( val, "undefined" ) == 0 ) {
4591 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4592 retry_list[0] = NULL;
4594 slap_str2clist( &retry_list, val, " ,\t" );
4596 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4599 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4600 "Error: incomplete syncrepl retry list" );
4601 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4602 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4603 ch_free( retry_list[k] );
4605 ch_free( retry_list );
4608 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4609 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4610 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4611 for ( j = 0; j < n; j++ ) {
4613 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4614 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4615 "Error: invalid retry interval \"%s\" (#%d)",
4616 retry_list[j*2], j );
4617 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4618 /* do some cleanup */
4621 si->si_retryinterval[j] = (time_t)t;
4622 if ( *retry_list[j*2+1] == '+' ) {
4623 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4624 si->si_retrynum[j] = RETRYNUM_FOREVER;
4628 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4629 || si->si_retrynum_init[j] <= 0 )
4631 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4632 "Error: invalid initial retry number \"%s\" (#%d)",
4633 retry_list[j*2+1], j );
4634 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4635 /* do some cleanup */
4638 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4639 || si->si_retrynum[j] <= 0 )
4641 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4642 "Error: invalid retry number \"%s\" (#%d)",
4643 retry_list[j*2+1], j );
4644 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4645 /* do some cleanup */
4650 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4651 Debug( LDAP_DEBUG_CONFIG,
4652 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4656 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4657 si->si_retrynum[j] = RETRYNUM_TAIL;
4658 si->si_retryinterval[j] = 0;
4660 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4661 ch_free( retry_list[k] );
4663 ch_free( retry_list );
4664 if ( !use_default ) {
4665 si->si_got |= GOT_RETRY;
4672 parse_syncrepl_line(
4679 for ( i = 1; i < c->argc; i++ ) {
4680 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4681 STRLENOF( IDSTR "=" ) ) )
4684 /* '\0' string terminator accounts for '=' */
4685 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4686 if ( lutil_atoi( &tmp, val ) != 0 ) {
4687 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4688 "Error: parse_syncrepl_line: "
4689 "unable to parse syncrepl id \"%s\"", val );
4690 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4693 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4694 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4695 "Error: parse_syncrepl_line: "
4696 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4697 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4701 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4702 si->si_got |= GOT_RID;
4703 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4704 STRLENOF( PROVIDERSTR "=" ) ) )
4706 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4707 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4709 if ( ldap_is_ldaps_url( val ))
4710 si->si_bindconf.sb_tls_do_init = 1;
4712 si->si_got |= GOT_PROVIDER;
4713 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4714 STRLENOF( SCHEMASTR "=" ) ) )
4716 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4717 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4718 si->si_schemachecking = 1;
4719 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4720 si->si_schemachecking = 0;
4722 si->si_schemachecking = 1;
4724 si->si_got |= GOT_SCHEMACHECKING;
4725 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4726 STRLENOF( FILTERSTR "=" ) ) )
4728 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4729 if ( si->si_filterstr.bv_val )
4730 ch_free( si->si_filterstr.bv_val );
4731 ber_str2bv( val, 0, 1, &si->si_filterstr );
4732 si->si_got |= GOT_FILTER;
4733 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4734 STRLENOF( LOGFILTERSTR "=" ) ) )
4736 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4737 if ( si->si_logfilterstr.bv_val )
4738 ch_free( si->si_logfilterstr.bv_val );
4739 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4740 si->si_got |= GOT_LOGFILTER;
4741 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4742 STRLENOF( SEARCHBASESTR "=" ) ) )
4747 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4748 if ( si->si_base.bv_val ) {
4749 ch_free( si->si_base.bv_val );
4751 ber_str2bv( val, 0, 0, &bv );
4752 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4753 if ( rc != LDAP_SUCCESS ) {
4754 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4755 "Invalid base DN \"%s\": %d (%s)",
4756 val, rc, ldap_err2string( rc ) );
4757 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4760 si->si_got |= GOT_SEARCHBASE;
4761 #ifdef ENABLE_REWRITE
4762 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
4763 STRLENOF( SUFFIXMSTR "=" ) ) )
4768 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
4769 if ( si->si_suffixm.bv_val ) {
4770 ch_free( si->si_suffixm.bv_val );
4772 ber_str2bv( val, 0, 0, &bv );
4773 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
4774 if ( rc != LDAP_SUCCESS ) {
4775 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4776 "Invalid massage DN \"%s\": %d (%s)",
4777 val, rc, ldap_err2string( rc ) );
4778 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4781 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
4782 ch_free( si->si_suffixm.bv_val );
4783 BER_BVZERO( &si->si_suffixm );
4784 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4785 "Massage DN \"%s\" is not within the database naming context",
4787 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4790 si->si_got |= GOT_SUFFIXM;
4792 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4793 STRLENOF( LOGBASESTR "=" ) ) )
4798 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
4799 if ( si->si_logbase.bv_val ) {
4800 ch_free( si->si_logbase.bv_val );
4802 ber_str2bv( val, 0, 0, &bv );
4803 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
4804 if ( rc != LDAP_SUCCESS ) {
4805 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4806 "Invalid logbase DN \"%s\": %d (%s)",
4807 val, rc, ldap_err2string( rc ) );
4808 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4811 si->si_got |= GOT_LOGBASE;
4812 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
4813 STRLENOF( SCOPESTR "=" ) ) )
4816 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
4817 j = ldap_pvt_str2scope( val );
4819 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4820 "Error: parse_syncrepl_line: "
4821 "unknown scope \"%s\"", val);
4822 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4826 si->si_got |= GOT_SCOPE;
4827 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
4828 STRLENOF( ATTRSONLYSTR ) ) )
4830 si->si_attrsonly = 1;
4831 si->si_got |= GOT_ATTRSONLY;
4832 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
4833 STRLENOF( ATTRSSTR "=" ) ) )
4835 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
4836 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4838 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4839 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
4840 if ( si->si_anlist == NULL ) {
4841 ch_free( attr_fname );
4844 si->si_anfile = attr_fname;
4846 char *str, *s, *next;
4847 const char *delimstr = " ,\t";
4848 str = ch_strdup( val );
4849 for ( s = ldap_pvt_strtok( str, delimstr, &next );
4851 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
4853 if ( strlen(s) == 1 && *s == '*' ) {
4854 si->si_allattrs = 1;
4855 val[ s - str ] = delimstr[0];
4857 if ( strlen(s) == 1 && *s == '+' ) {
4858 si->si_allopattrs = 1;
4859 val [ s - str ] = delimstr[0];
4863 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
4864 if ( si->si_anlist == NULL ) {
4868 si->si_got |= GOT_ATTRS;
4869 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
4870 STRLENOF( EXATTRSSTR "=" ) ) )
4872 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
4873 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4875 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4876 si->si_exanlist = file2anlist(
4877 si->si_exanlist, attr_fname, " ,\t" );
4878 if ( si->si_exanlist == NULL ) {
4879 ch_free( attr_fname );
4882 ch_free( attr_fname );
4884 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
4885 if ( si->si_exanlist == NULL ) {
4889 si->si_got |= GOT_EXATTRS;
4890 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
4891 STRLENOF( TYPESTR "=" ) ) )
4893 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
4894 if ( !strncasecmp( val, "refreshOnly",
4895 STRLENOF("refreshOnly") ) )
4897 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4898 } else if ( !strncasecmp( val, "refreshAndPersist",
4899 STRLENOF("refreshAndPersist") ) )
4901 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
4902 si->si_interval = 60;
4904 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4905 "Error: parse_syncrepl_line: "
4906 "unknown sync type \"%s\"", val);
4907 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4910 si->si_got |= GOT_TYPE;
4911 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
4912 STRLENOF( INTERVALSTR "=" ) ) )
4914 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
4915 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
4916 si->si_interval = 0;
4917 } else if ( strchr( val, ':' ) != NULL ) {
4918 char *next, *ptr = val;
4921 dd = strtol( ptr, &next, 10 );
4922 if ( next == ptr || next[0] != ':' || dd < 0 ) {
4923 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4924 "Error: parse_syncrepl_line: "
4925 "invalid interval \"%s\", unable to parse days", val );
4926 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4930 hh = strtol( ptr, &next, 10 );
4931 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
4932 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4933 "Error: parse_syncrepl_line: "
4934 "invalid interval \"%s\", unable to parse hours", val );
4935 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4939 mm = strtol( ptr, &next, 10 );
4940 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
4941 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4942 "Error: parse_syncrepl_line: "
4943 "invalid interval \"%s\", unable to parse minutes", val );
4944 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4948 ss = strtol( ptr, &next, 10 );
4949 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
4950 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4951 "Error: parse_syncrepl_line: "
4952 "invalid interval \"%s\", unable to parse seconds", val );
4953 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4956 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
4960 if ( lutil_parse_time( val, &t ) != 0 ) {
4961 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4962 "Error: parse_syncrepl_line: "
4963 "invalid interval \"%s\"", val );
4964 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4967 si->si_interval = (time_t)t;
4969 if ( si->si_interval < 0 ) {
4970 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4971 "Error: parse_syncrepl_line: "
4972 "invalid interval \"%ld\"",
4973 (long) si->si_interval);
4974 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4977 si->si_got |= GOT_INTERVAL;
4978 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
4979 STRLENOF( RETRYSTR "=" ) ) )
4981 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
4984 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
4985 STRLENOF( MANAGEDSAITSTR "=" ) ) )
4987 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
4988 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
4989 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
4991 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4992 "invalid manageDSAit value \"%s\".\n",
4994 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4997 si->si_got |= GOT_MANAGEDSAIT;
4998 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
4999 STRLENOF( SLIMITSTR "=") ) )
5001 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
5002 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5005 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
5006 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5007 "invalid size limit value \"%s\".\n",
5009 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5012 si->si_got |= GOT_SLIMIT;
5013 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
5014 STRLENOF( TLIMITSTR "=" ) ) )
5016 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
5017 if ( strcasecmp( val, "unlimited" ) == 0 ) {
5020 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
5021 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5022 "invalid time limit value \"%s\".\n",
5024 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5027 si->si_got |= GOT_TLIMIT;
5028 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
5029 STRLENOF( SYNCDATASTR "=" ) ) )
5031 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
5032 si->si_syncdata = verb_to_mask( val, datamodes );
5033 si->si_got |= GOT_SYNCDATA;
5034 } else if ( !strncasecmp( c->argv[ i ], STRICT_REFRESH,
5035 STRLENOF( STRICT_REFRESH ) ) )
5037 si->si_strict_refresh = 1;
5038 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
5039 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5040 "Error: parse_syncrepl_line: "
5041 "unable to parse \"%s\"\n", c->argv[ i ] );
5042 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5045 si->si_got |= GOT_BINDCONF;
5048 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
5049 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5050 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
5051 si->si_got & GOT_RID ? "" : " "IDSTR,
5052 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
5053 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
5054 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5058 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
5059 ch_free( si->si_base.bv_val );
5060 BER_BVZERO( &si->si_base );
5061 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5062 "Base DN \"%s\" is not within the database naming context",
5064 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5068 #ifdef ENABLE_REWRITE
5069 if ( si->si_got & GOT_SUFFIXM ) {
5070 if (config_suffixm( c, si )) {
5071 ch_free( si->si_suffixm.bv_val );
5072 BER_BVZERO( &si->si_suffixm );
5073 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5074 "Error configuring rewrite engine" );
5075 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5081 if ( !( si->si_got & GOT_RETRY ) ) {
5082 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
5083 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
5084 if ( si->si_retryinterval == NULL ) {
5085 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
5091 si->si_filter = str2filter( si->si_filterstr.bv_val );
5092 if ( si->si_filter == NULL ) {
5093 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
5094 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
5108 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
5109 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
5110 "operations required for syncrepl", c->be->be_type );
5111 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5114 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
5115 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
5116 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5119 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
5122 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
5126 si->si_bindconf.sb_tls = SB_TLS_OFF;
5127 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
5128 si->si_schemachecking = 0;
5129 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
5130 &si->si_filterstr );
5131 si->si_base.bv_val = NULL;
5132 si->si_scope = LDAP_SCOPE_SUBTREE;
5133 si->si_attrsonly = 0;
5134 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5135 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5136 si->si_attrs = NULL;
5137 si->si_allattrs = 0;
5138 si->si_allopattrs = 0;
5139 si->si_exattrs = NULL;
5140 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5141 si->si_interval = 86400;
5142 si->si_retryinterval = NULL;
5143 si->si_retrynum_init = NULL;
5144 si->si_retrynum = NULL;
5145 si->si_manageDSAit = 0;
5149 si->si_presentlist = NULL;
5150 LDAP_LIST_INIT( &si->si_nonpresentlist );
5151 ldap_pvt_thread_mutex_init( &si->si_mutex );
5153 rc = parse_syncrepl_line( c, si );
5158 /* Must be LDAPv3 because we need controls */
5159 switch ( si->si_bindconf.sb_version ) {
5161 /* not explicitly set */
5162 si->si_bindconf.sb_version = LDAP_VERSION3;
5165 /* explicitly set */
5168 Debug( LDAP_DEBUG_ANY,
5169 "version %d incompatible with syncrepl\n",
5170 si->si_bindconf.sb_version, 0, 0 );
5171 syncinfo_free( si, 0 );
5175 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
5176 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5177 "<%s> invalid URL", c->argv[0] );
5178 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
5179 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
5184 if ( slapMode & SLAP_SERVER_MODE ) {
5186 /* check if consumer points to current server and database.
5187 * If so, ignore this configuration.
5189 if ( !SLAP_DBHIDDEN( c->be ) ) {
5191 /* if searchbase doesn't match current DB suffix,
5192 * assume it's different
5194 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
5195 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
5200 /* if searchbase matches, see if URLs match */
5201 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
5207 init_syncrepl( si );
5208 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5209 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
5210 si->si_interval, do_syncrepl, si, "do_syncrepl",
5212 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5214 rc = config_sync_shadow( c ) ? -1 : 0;
5219 /* mirrormode still needs to see this flag in tool mode */
5220 rc = config_sync_shadow( c ) ? -1 : 0;
5222 ldap_free_urldesc( lud );
5226 /* Use main slapd defaults */
5227 bindconf_tls_defaults( &si->si_bindconf );
5230 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
5231 syncinfo_free( si, 0 );
5234 Debug( LDAP_DEBUG_CONFIG,
5235 "Config: ** successfully added syncrepl %s \"%s\"\n",
5237 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
5238 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
5239 if ( c->be->be_syncinfo ) {
5242 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
5244 /* add new syncrepl to end of list (same order as when deleting) */
5245 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
5248 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
5249 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
5250 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
5252 c->be->be_syncinfo = si;
5254 si->si_cookieState->cs_ref++;
5263 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
5265 struct berval bc, uri, bs;
5266 char buf[BUFSIZ*2], *ptr;
5269 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
5273 /* temporarily inhibit bindconf from printing URI */
5274 uri = si->si_bindconf.sb_uri;
5275 BER_BVZERO( &si->si_bindconf.sb_uri );
5276 si->si_bindconf.sb_version = 0;
5277 bindconf_unparse( &si->si_bindconf, &bc );
5278 si->si_bindconf.sb_uri = uri;
5279 si->si_bindconf.sb_version = LDAP_VERSION3;
5282 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
5283 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
5284 si->si_rid, si->si_bindconf.sb_uri.bv_val );
5285 if ( len >= sizeof( buf ) ) return;
5287 if ( !BER_BVISNULL( &bc ) ) {
5288 if ( WHATSLEFT <= bc.bv_len ) {
5292 ptr = lutil_strcopy( ptr, bc.bv_val );
5295 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
5296 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
5297 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
5298 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
5301 if ( !BER_BVISNULL( &si->si_base ) ) {
5302 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
5303 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
5304 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
5307 #ifdef ENABLE_REWRITE
5308 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
5309 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
5310 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
5311 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
5315 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
5316 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
5317 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
5318 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
5321 if ( !BER_BVISNULL( &si->si_logbase ) ) {
5322 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
5323 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
5324 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
5327 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
5328 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
5329 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
5330 ptr = lutil_strcopy( ptr, bs.bv_val );
5332 if ( si->si_attrsonly ) {
5333 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5334 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
5336 if ( si->si_anfile ) {
5337 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
5338 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
5339 ptr = lutil_strcopy( ptr, si->si_anfile );
5341 } else if ( si->si_allattrs || si->si_allopattrs ||
5342 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
5346 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5347 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
5349 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
5350 if ( ptr == NULL ) return;
5351 if ( si->si_allattrs ) {
5352 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
5353 if ( old != ptr ) *ptr++ = ',';
5356 if ( si->si_allopattrs ) {
5357 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
5358 if ( old != ptr ) *ptr++ = ',';
5363 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
5364 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
5365 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
5366 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
5367 if ( ptr == NULL ) return;
5369 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
5370 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
5371 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
5373 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
5374 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
5375 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
5376 "refreshAndPersist" : "refreshOnly" );
5378 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
5381 dd = si->si_interval;
5388 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
5389 INTERVALSTR, dd, hh, mm, ss );
5390 if ( len >= WHATSLEFT ) return;
5394 if ( si->si_got & GOT_RETRY ) {
5395 const char *space = "";
5396 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
5397 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
5398 for (i=0; si->si_retryinterval[i]; i++) {
5399 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
5400 (long) si->si_retryinterval[i] );
5402 if ( WHATSLEFT - 1 <= len ) return;
5404 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
5407 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
5408 if ( WHATSLEFT <= len ) return;
5412 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
5415 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
5418 if ( si->si_slimit ) {
5419 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
5420 if ( WHATSLEFT <= len ) return;
5424 if ( si->si_tlimit ) {
5425 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
5426 if ( WHATSLEFT <= len ) return;
5430 if ( si->si_syncdata ) {
5431 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
5432 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
5433 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
5434 ptr = lutil_strcopy( ptr, bc.bv_val );
5437 bc.bv_len = ptr - buf;
5439 ber_dupbv( bv, &bc );
5443 syncrepl_config( ConfigArgs *c )
5445 if (c->op == SLAP_CONFIG_EMIT) {
5446 if ( c->be->be_syncinfo ) {
5450 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
5451 syncrepl_unparse( si, &bv );
5452 ber_bvarray_add( &c->rvalue_vals, &bv );
5457 } else if ( c->op == LDAP_MOD_DELETE ) {
5459 if ( c->be->be_syncinfo ) {
5460 syncinfo_t *si, **sip;
5463 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
5465 if ( c->valx == -1 || i == c->valx ) {
5469 /* If the task is currently active, we have to leave
5470 * it running. It will exit on its own. This will only
5471 * happen when running on the cn=config DB.
5474 if ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
5477 /* There is no active thread, but we must still
5478 * ensure that no thread is (or will be) queued
5479 * while we removes the task.
5481 struct re_s *re = si->si_re;
5484 if ( si->si_conn ) {
5485 connection_client_stop( si->si_conn );
5489 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5490 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
5491 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5494 ldap_pvt_runqueue_remove( &slapd_rq, re );
5495 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5497 if ( ldap_pvt_thread_pool_retract( &connection_pool,
5498 re->routine, re ) > 0 )
5501 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5505 syncinfo_free( si, 0 );
5514 if ( !c->be->be_syncinfo ) {
5515 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5519 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5520 Debug(LDAP_DEBUG_ANY, "%s: "
5521 "syncrepl: database already shadowed.\n",
5525 return add_syncrepl( c );