1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2011 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003-2008 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
28 #include "lutil_ldap.h"
36 #define SUFFIXM_CTX "<suffix massage>"
39 struct nonpresent_entry {
40 struct berval *npe_name;
41 struct berval *npe_nname;
42 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
45 typedef struct cookie_state {
46 ldap_pvt_thread_mutex_t cs_mutex;
50 struct berval *cs_vals;
53 /* pending changes, not yet committed */
54 ldap_pvt_thread_mutex_t cs_pmutex;
56 struct berval *cs_pvals;
60 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
61 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
62 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
64 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
65 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
67 #define RETRYNUM_FOREVER (-1) /* retry forever */
68 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
69 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
70 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
72 typedef struct syncinfo_s {
73 struct syncinfo_s *si_next;
78 char si_ridtxt[ STRLENOF("rid=999") + 1 ];
79 slap_bindconf si_bindconf;
80 struct berval si_base;
81 struct berval si_logbase;
82 struct berval si_filterstr;
84 struct berval si_logfilterstr;
85 struct berval si_contextdn;
89 AttributeName *si_anlist;
90 AttributeName *si_exanlist;
95 int si_schemachecking;
96 int si_type; /* the active type */
97 int si_ctype; /* the configured type */
99 time_t *si_retryinterval;
100 int *si_retrynum_init;
102 struct sync_cookie si_syncCookie;
103 cookie_state *si_cookieState;
108 int si_refreshDelete;
109 int si_refreshPresent;
114 int si_strict_refresh; /* stop listening during fallback refresh */
116 Avlnode *si_presentlist;
119 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
120 #ifdef ENABLE_REWRITE
121 struct rewrite_info *si_rewrite;
122 struct berval si_suffixm;
124 ldap_pvt_thread_mutex_t si_mutex;
127 static int syncuuid_cmp( const void *, const void * );
128 static int avl_presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
129 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
130 static int syncrepl_message_to_op(
131 syncinfo_t *, Operation *, LDAPMessage * );
132 static int syncrepl_message_to_entry(
133 syncinfo_t *, Operation *, LDAPMessage *,
134 Modifications **, Entry **, int );
135 static int syncrepl_entry(
136 syncinfo_t *, Operation*, Entry*,
137 Modifications**,int, struct berval*,
138 struct berval *cookieCSN );
139 static int syncrepl_updateCookie(
140 syncinfo_t *, Operation *,
141 struct sync_cookie * );
142 static struct berval * slap_uuidstr_from_normalized(
143 struct berval *, struct berval *, void * );
144 static int syncrepl_add_glue_ancestors(
145 Operation* op, Entry *e );
147 /* 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 );
696 if ( si->si_cookieState->cs_num ) {
697 ber_bvarray_free( si->si_syncCookie.ctxcsn );
698 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
699 si->si_cookieState->cs_vals, NULL )) {
701 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
704 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
705 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
707 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
708 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
710 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
713 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
714 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
715 si->si_syncCookie.sid );
717 /* ITS#6367: recreate the cookie so it has our SID, not our peer's */
718 ch_free( si->si_syncCookie.octet_str.bv_val );
719 BER_BVZERO( &si->si_syncCookie.octet_str );
720 /* Look for contextCSN from syncprov overlay. */
721 check_syncprov( op, si );
722 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
723 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
724 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
725 si->si_syncCookie.sid );
728 si->si_refreshDone = 0;
730 rc = ldap_sync_search( si, op->o_tmpmemctx );
732 if( rc != LDAP_SUCCESS ) {
733 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
734 "ldap_search_ext: %s (%d)\n",
735 si->si_ridtxt, ldap_err2string( rc ), rc );
741 ldap_unbind_ext( si->si_ld, NULL, NULL );
750 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
757 if ( sc1->numcsns < sc2->numcsns ) {
758 *which = sc1->numcsns;
762 for (j=0; j<sc2->numcsns; j++) {
763 for (i=0; i<sc1->numcsns; i++) {
764 if ( sc1->sids[i] != sc2->sids[j] )
766 value_match( &match, slap_schema.si_ad_entryCSN,
767 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
768 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
769 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
776 if ( i == sc1->numcsns ) {
777 /* sc2 has a sid sc1 lacks */
785 #define SYNC_PAUSED -3
792 BerElementBuffer berbuf;
793 BerElement *ber = (BerElement *)&berbuf;
795 LDAPMessage *msg = NULL;
797 struct sync_cookie syncCookie = { NULL };
798 struct sync_cookie syncCookie_req = { NULL };
803 Modifications *modlist = NULL;
807 struct timeval *tout_p = NULL;
808 struct timeval tout = { 0, 0 };
810 int refreshDeletes = 0;
811 char empty[6] = "empty";
813 if ( slapd_shutdown ) {
818 ber_init2( ber, NULL, LBER_USE_DER );
819 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
821 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
823 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
825 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
831 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
832 tout_p, &msg ) ) > 0 )
834 int match, punlock, syncstate;
835 struct berval *retdata, syncUUID, cookie = BER_BVNULL;
837 LDAPControl **rctrls = NULL, *rctrlp = NULL;
844 if ( slapd_shutdown ) {
848 switch( ldap_msgtype( msg ) ) {
849 case LDAP_RES_SEARCH_ENTRY:
850 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
851 ldap_get_dn_ber( si->si_ld, msg, NULL, &bdn );
854 bdn.bv_len = sizeof(empty)-1;
856 /* we can't work without the control */
858 LDAPControl **next = NULL;
859 /* NOTE: make sure we use the right one;
860 * a better approach would be to run thru
861 * the whole list and take care of all */
862 /* NOTE: since we issue the search request,
863 * we should know what controls to expect,
864 * and there should be none apart from the
865 * sync-related control */
866 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
867 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
869 bdn.bv_val[bdn.bv_len] = '\0';
870 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
871 "got search entry with multiple "
872 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
873 ldap_controls_free( rctrls );
878 if ( rctrlp == NULL ) {
879 bdn.bv_val[bdn.bv_len] = '\0';
880 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
881 "got search entry without "
882 "Sync State control (%s)\n", si->si_ridtxt, bdn.bv_val, 0 );
886 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
887 if ( ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID )
889 bdn.bv_val[bdn.bv_len] = '\0';
890 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s malformed message (%s)\n",
891 si->si_ridtxt, bdn.bv_val, 0 );
892 ldap_controls_free( rctrls );
896 /* FIXME: what if syncUUID is NULL or empty?
897 * (happens with back-sql...) */
898 if ( BER_BVISEMPTY( &syncUUID ) ) {
899 bdn.bv_val[bdn.bv_len] = '\0';
900 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
901 "got empty syncUUID with LDAP_SYNC_%s (%s)\n",
903 syncrepl_state2str( syncstate ), bdn.bv_val );
904 ldap_controls_free( rctrls );
909 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
910 ber_scanf( ber, /*"{"*/ "m}", &cookie );
912 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
914 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
916 if ( !BER_BVISNULL( &cookie ) ) {
917 ch_free( syncCookie.octet_str.bv_val );
918 ber_dupbv( &syncCookie.octet_str, &cookie );
920 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
922 slap_parse_sync_cookie( &syncCookie, NULL );
923 if ( syncCookie.ctxcsn ) {
924 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
925 check_syncprov( op, si );
926 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
927 if ( si->si_cookieState->cs_sids[i] == sid ) {
928 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
929 bdn.bv_val[bdn.bv_len] = '\0';
930 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s (%s)\n",
931 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
932 ldap_controls_free( rctrls );
939 /* check pending CSNs too */
940 while ( ldap_pvt_thread_mutex_trylock( &si->si_cookieState->cs_pmutex )) {
941 if ( slapd_shutdown ) {
945 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
946 ldap_pvt_thread_yield();
948 for ( i =0; i<si->si_cookieState->cs_pnum; i++ ) {
949 if ( si->si_cookieState->cs_psids[i] == sid ) {
950 if ( ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_pvals[i] ) <= 0 ) {
951 bdn.bv_val[bdn.bv_len] = '\0';
952 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN pending, ignoring %s (%s)\n",
953 si->si_ridtxt, syncCookie.ctxcsn->bv_val, bdn.bv_val );
954 ldap_controls_free( rctrls );
956 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
959 ber_bvreplace( &si->si_cookieState->cs_pvals[i],
964 /* new SID, add it */
965 if ( i == si->si_cookieState->cs_pnum ) {
966 value_add( &si->si_cookieState->cs_pvals, syncCookie.ctxcsn );
967 si->si_cookieState->cs_pnum++;
968 si->si_cookieState->cs_psids = ch_realloc( si->si_cookieState->cs_psids, si->si_cookieState->cs_pnum * sizeof(int));
969 si->si_cookieState->cs_psids[i] = sid;
971 assert( punlock < 0 );
974 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
978 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
980 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
983 rc = syncrepl_updateCookie( si, op, &syncCookie );
984 } else switch ( rc ) {
985 case LDAP_ALREADY_EXISTS:
986 case LDAP_NO_SUCH_OBJECT:
987 case LDAP_NO_SUCH_ATTRIBUTE:
988 case LDAP_TYPE_OR_VALUE_EXISTS:
989 rc = LDAP_SYNC_REFRESH_REQUIRED;
990 si->si_logstate = SYNCLOG_FALLBACK;
991 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
992 bdn.bv_val[bdn.bv_len] = '\0';
993 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync on (%s), switching to REFRESH\n",
994 si->si_ridtxt, bdn.bv_val, 0 );
995 if (si->si_strict_refresh) {
996 slap_suspend_listeners();
1003 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
1004 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
1006 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
1007 syncstate, &syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
1010 rc = syncrepl_updateCookie( si, op, &syncCookie );
1013 if ( punlock >= 0 ) {
1014 /* on failure, revert pending CSN */
1015 if ( rc != LDAP_SUCCESS ) {
1017 for ( i = 0; i<si->si_cookieState->cs_num; i++ ) {
1018 if ( si->si_cookieState->cs_sids[i] == si->si_cookieState->cs_psids[punlock] ) {
1019 ber_bvreplace( &si->si_cookieState->cs_pvals[punlock],
1020 &si->si_cookieState->cs_vals[i] );
1024 if ( i == si->si_cookieState->cs_num )
1025 si->si_cookieState->cs_pvals[punlock].bv_val[0] = '\0';
1027 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_pmutex );
1029 ldap_controls_free( rctrls );
1031 slap_mods_free( modlist, 1 );
1037 case LDAP_RES_SEARCH_REFERENCE:
1038 Debug( LDAP_DEBUG_ANY,
1039 "do_syncrep2: %s reference received error\n",
1040 si->si_ridtxt, 0, 0 );
1043 case LDAP_RES_SEARCH_RESULT:
1044 Debug( LDAP_DEBUG_SYNC,
1045 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
1046 si->si_ridtxt, 0, 0 );
1047 err = LDAP_OTHER; /* FIXME check parse result properly */
1048 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
1050 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
1051 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
1052 /* map old result code to registered code */
1053 err = LDAP_SYNC_REFRESH_REQUIRED;
1056 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
1057 if ( si->si_logstate == SYNCLOG_LOGGING ) {
1058 si->si_logstate = SYNCLOG_FALLBACK;
1059 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s delta-sync lost sync, switching to REFRESH\n",
1060 si->si_ridtxt, 0, 0 );
1061 if (si->si_strict_refresh) {
1062 slap_suspend_listeners();
1070 Debug( LDAP_DEBUG_ANY,
1071 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
1072 si->si_ridtxt, err, ldap_err2string( err ) );
1075 LDAPControl **next = NULL;
1076 /* NOTE: make sure we use the right one;
1077 * a better approach would be to run thru
1078 * the whole list and take care of all */
1079 /* NOTE: since we issue the search request,
1080 * we should know what controls to expect,
1081 * and there should be none apart from the
1082 * sync-related control */
1083 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
1084 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
1086 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1087 "got search result with multiple "
1088 "Sync State control\n", si->si_ridtxt, 0, 0 );
1089 ldap_controls_free( rctrls );
1095 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
1097 ber_scanf( ber, "{" /*"}"*/);
1098 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
1099 ber_scanf( ber, "m", &cookie );
1101 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1103 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1105 if ( !BER_BVISNULL( &cookie ) ) {
1106 ch_free( syncCookie.octet_str.bv_val );
1107 ber_dupbv( &syncCookie.octet_str, &cookie);
1109 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1111 slap_parse_sync_cookie( &syncCookie, NULL );
1112 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1115 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
1117 ber_scanf( ber, "b", &refreshDeletes );
1119 ber_scanf( ber, /*"{"*/ "}" );
1121 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1122 slap_sync_cookie_free( &syncCookie_req, 0 );
1123 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1125 if ( !syncCookie.ctxcsn ) {
1127 } else if ( !syncCookie_req.ctxcsn ) {
1131 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1134 ldap_controls_free( rctrls );
1136 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1137 /* FIXME : different error behaviors according to
1138 * 1) err code : LDAP_BUSY ...
1139 * 2) on err policy : stop service, stop sync, retry
1141 if ( refreshDeletes == 0 && match < 0 &&
1142 err == LDAP_SUCCESS &&
1143 syncCookie_req.numcsns == syncCookie.numcsns )
1145 syncrepl_del_nonpresent( op, si, NULL,
1148 avl_free( si->si_presentlist, ch_free );
1149 si->si_presentlist = NULL;
1152 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1154 rc = syncrepl_updateCookie( si, op, &syncCookie );
1156 if ( err == LDAP_SUCCESS
1157 && si->si_logstate == SYNCLOG_FALLBACK ) {
1158 si->si_logstate = SYNCLOG_LOGGING;
1159 rc = LDAP_SYNC_REFRESH_REQUIRED;
1160 slap_resume_listeners();
1166 case LDAP_RES_INTERMEDIATE:
1169 rc = ldap_parse_intermediate( si->si_ld, msg,
1170 &retoid, &retdata, NULL, 0 );
1171 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1172 ber_init2( ber, retdata, LBER_USE_DER );
1174 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1176 case LDAP_TAG_SYNC_NEW_COOKIE:
1177 Debug( LDAP_DEBUG_SYNC,
1178 "do_syncrep2: %s %s - %s\n",
1180 "LDAP_RES_INTERMEDIATE",
1182 ber_scanf( ber, "tm", &tag, &cookie );
1183 Debug( LDAP_DEBUG_SYNC,
1184 "do_syncrep2: %s NEW_COOKIE: %s\n",
1187 if ( !BER_BVISNULL( &cookie ) ) {
1188 ch_free( syncCookie.octet_str.bv_val );
1189 ber_dupbv( &syncCookie.octet_str, &cookie );
1191 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1192 slap_parse_sync_cookie( &syncCookie, NULL );
1193 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1196 case LDAP_TAG_SYNC_REFRESH_DELETE:
1197 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1198 Debug( LDAP_DEBUG_SYNC,
1199 "do_syncrep2: %s %s - %s\n",
1201 "LDAP_RES_INTERMEDIATE",
1202 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1203 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1204 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1205 si->si_refreshDelete = 1;
1207 si->si_refreshPresent = 1;
1209 ber_scanf( ber, "t{" /*"}"*/, &tag );
1210 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1212 ber_scanf( ber, "m", &cookie );
1214 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1216 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1218 if ( !BER_BVISNULL( &cookie ) ) {
1219 ch_free( syncCookie.octet_str.bv_val );
1220 ber_dupbv( &syncCookie.octet_str, &cookie );
1222 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1224 slap_parse_sync_cookie( &syncCookie, NULL );
1225 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1228 /* Defaults to TRUE */
1229 if ( ber_peek_tag( ber, &len ) ==
1230 LDAP_TAG_REFRESHDONE )
1232 ber_scanf( ber, "b", &si->si_refreshDone );
1235 si->si_refreshDone = 1;
1237 ber_scanf( ber, /*"{"*/ "}" );
1239 case LDAP_TAG_SYNC_ID_SET:
1240 Debug( LDAP_DEBUG_SYNC,
1241 "do_syncrep2: %s %s - %s\n",
1243 "LDAP_RES_INTERMEDIATE",
1245 ber_scanf( ber, "t{" /*"}"*/, &tag );
1246 if ( ber_peek_tag( ber, &len ) ==
1247 LDAP_TAG_SYNC_COOKIE )
1249 ber_scanf( ber, "m", &cookie );
1251 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s cookie=%s\n",
1253 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0 );
1255 if ( !BER_BVISNULL( &cookie ) ) {
1256 ch_free( syncCookie.octet_str.bv_val );
1257 ber_dupbv( &syncCookie.octet_str, &cookie );
1259 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1261 slap_parse_sync_cookie( &syncCookie, NULL );
1262 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1263 compare_csns( &syncCookie_req, &syncCookie, &m );
1266 if ( ber_peek_tag( ber, &len ) ==
1267 LDAP_TAG_REFRESHDELETES )
1269 ber_scanf( ber, "b", &refreshDeletes );
1272 ber_scanf( ber, "[W]", &syncUUIDs );
1273 ber_scanf( ber, /*"{"*/ "}" );
1274 if ( refreshDeletes ) {
1275 syncrepl_del_nonpresent( op, si, syncUUIDs,
1277 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1280 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1281 (void)avl_presentlist_insert( si, &syncUUIDs[i] );
1282 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1284 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1286 slap_sync_cookie_free( &syncCookie, 0 );
1289 Debug( LDAP_DEBUG_ANY,
1290 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1291 si->si_ridtxt, (long) si_tag, 0 );
1292 ldap_memfree( retoid );
1293 ber_bvfree( retdata );
1297 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1298 slap_sync_cookie_free( &syncCookie_req, 0 );
1299 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1301 if ( !syncCookie.ctxcsn ) {
1303 } else if ( !syncCookie_req.ctxcsn ) {
1307 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1311 if ( si->si_refreshPresent == 1 &&
1312 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1313 syncCookie_req.numcsns == syncCookie.numcsns ) {
1314 syncrepl_del_nonpresent( op, si, NULL,
1318 if ( syncCookie.ctxcsn )
1320 rc = syncrepl_updateCookie( si, op, &syncCookie);
1324 ldap_memfree( retoid );
1325 ber_bvfree( retdata );
1328 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1329 "unknown intermediate response (%d)\n",
1330 si->si_ridtxt, rc, 0 );
1331 ldap_memfree( retoid );
1332 ber_bvfree( retdata );
1337 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1338 "unknown message (0x%02lx)\n",
1340 (unsigned long)ldap_msgtype( msg ), 0 );
1344 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1345 slap_sync_cookie_free( &syncCookie_req, 0 );
1346 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1347 slap_sync_cookie_free( &syncCookie, 0 );
1349 ldap_msgfree( msg );
1351 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1352 slap_sync_cookie_free( &syncCookie, 0 );
1353 slap_sync_cookie_free( &syncCookie_req, 0 );
1360 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1365 if ( err != LDAP_SUCCESS ) {
1366 Debug( LDAP_DEBUG_ANY,
1367 "do_syncrep2: %s (%d) %s\n",
1368 si->si_ridtxt, err, ldap_err2string( err ) );
1371 slap_sync_cookie_free( &syncCookie, 0 );
1372 slap_sync_cookie_free( &syncCookie_req, 0 );
1374 if ( msg ) ldap_msgfree( msg );
1376 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1377 if ( si->si_conn ) {
1378 connection_client_stop( si->si_conn );
1381 ldap_unbind_ext( si->si_ld, NULL, NULL );
1393 struct re_s* rtask = arg;
1394 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1395 Connection conn = {0};
1396 OperationBuffer opbuf;
1398 int rc = LDAP_SUCCESS;
1401 int i, defer = 1, fail = 0, freeinfo = 0;
1406 if ( slapd_shutdown )
1409 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1411 /* Don't get stuck here while a pause is initiated */
1412 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1413 if ( slapd_shutdown )
1415 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1416 ldap_pvt_thread_yield();
1419 if ( si->si_ctype < 1 ) {
1423 switch( abs( si->si_type ) ) {
1424 case LDAP_SYNC_REFRESH_ONLY:
1425 case LDAP_SYNC_REFRESH_AND_PERSIST:
1428 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1432 if ( slapd_shutdown ) {
1434 if ( si->si_conn ) {
1435 connection_client_stop( si->si_conn );
1438 ldap_unbind_ext( si->si_ld, NULL, NULL );
1441 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1445 connection_fake_init( &conn, &opbuf, ctx );
1447 /* o_connids must be unique for slap_graduate_commit_csn */
1448 op->o_connid = SLAPD_SYNC_RID2SYNCCONN(si->si_rid);
1450 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1453 /* Coordinate contextCSN updates with any syncprov overlays
1454 * in use. This may be complicated by the use of the glue
1457 * Typically there is a single syncprov mastering the entire
1458 * glued tree. In that case, our contextCSN updates should
1459 * go to the master DB. But if there is no syncprov on the
1460 * master DB, then nothing special is needed here.
1462 * Alternatively, there may be individual syncprov overlays
1463 * on each glued branch. In that case, each syncprov only
1464 * knows about changes within its own branch. And so our
1465 * contextCSN updates should only go to the local DB.
1467 if ( !si->si_wbe ) {
1468 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1469 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1470 if ( overlay_is_inst( top_be, "syncprov" ))
1471 si->si_wbe = top_be;
1477 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1478 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1479 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1481 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1484 if ( !si->si_schemachecking )
1485 op->o_no_schema_check = 1;
1487 /* Establish session, do search */
1489 si->si_refreshDelete = 0;
1490 si->si_refreshPresent = 0;
1492 if ( si->si_presentlist ) {
1493 avl_free( si->si_presentlist, ch_free );
1494 si->si_presentlist = NULL;
1497 /* use main DB when retrieving contextCSN */
1498 op->o_bd = si->si_wbe;
1499 op->o_dn = op->o_bd->be_rootdn;
1500 op->o_ndn = op->o_bd->be_rootndn;
1501 rc = do_syncrep1( op, si );
1505 /* Process results */
1506 if ( rc == LDAP_SUCCESS ) {
1507 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1509 /* use current DB */
1511 op->o_dn = op->o_bd->be_rootdn;
1512 op->o_ndn = op->o_bd->be_rootndn;
1513 rc = do_syncrep2( op, si );
1514 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1515 rc = ldap_sync_search( si, op->o_tmpmemctx );
1520 /* We got deleted while running on cn=config */
1521 if ( si->si_ctype < 1 ) {
1522 if ( si->si_ctype == -1 ) {
1531 if ( rc != SYNC_PAUSED ) {
1532 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1533 /* If we succeeded, enable the connection for further listening.
1534 * If we failed, tear down the connection and reschedule.
1536 if ( rc == LDAP_SUCCESS ) {
1537 if ( si->si_conn ) {
1538 connection_client_enable( si->si_conn );
1540 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1542 } else if ( si->si_conn ) {
1546 if ( rc == -2 ) rc = 0;
1551 /* At this point, we have 5 cases:
1552 * 1) for any hard failure, give up and remove this task
1553 * 2) for ServerDown, reschedule this task to run later
1554 * 3) for threadpool pause, reschedule to run immediately
1555 * 4) for Refresh and Success, reschedule to run
1556 * 5) for Persist and Success, reschedule to defer
1558 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1560 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1561 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1565 connection_client_stop( si->si_conn );
1569 if ( rc == SYNC_PAUSED ) {
1570 rtask->interval.tv_sec = 0;
1571 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1572 rtask->interval.tv_sec = si->si_interval;
1574 } else if ( rc == LDAP_SUCCESS ) {
1575 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1578 rtask->interval.tv_sec = si->si_interval;
1579 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1580 if ( si->si_retrynum ) {
1581 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1582 si->si_retrynum[i] = si->si_retrynum_init[i];
1584 si->si_retrynum[i] = RETRYNUM_TAIL;
1587 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1588 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1592 if ( si->si_ctype < 1
1593 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1595 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1598 fail = RETRYNUM_TAIL;
1599 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1600 if ( si->si_retrynum[i] > 0 )
1601 si->si_retrynum[i]--;
1602 fail = si->si_retrynum[i];
1603 rtask->interval.tv_sec = si->si_retryinterval[i];
1604 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1605 slap_wake_listener();
1609 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1610 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1613 if ( fail == RETRYNUM_TAIL ) {
1614 Debug( LDAP_DEBUG_ANY,
1615 "do_syncrepl: %s rc %d quitting\n",
1616 si->si_ridtxt, rc, 0 );
1617 } else if ( fail > 0 ) {
1618 Debug( LDAP_DEBUG_ANY,
1619 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1620 si->si_ridtxt, rc, fail );
1622 Debug( LDAP_DEBUG_ANY,
1623 "do_syncrepl: %s rc %d retrying\n",
1624 si->si_ridtxt, rc, 0 );
1628 /* Do final delete cleanup */
1630 syncinfo_free( si, 0 );
1635 #ifdef ENABLE_REWRITE
1637 syncrepl_rewrite_dn(
1640 struct berval *sdn )
1645 nul = dn->bv_val[dn->bv_len];
1646 dn->bv_val[dn->bv_len] = 0;
1647 rc = rewrite( si->si_rewrite, SUFFIXM_CTX, dn->bv_val, &sdn->bv_val );
1648 dn->bv_val[dn->bv_len] = nul;
1650 if ( sdn->bv_val == dn->bv_val )
1652 else if ( rc == REWRITE_REGEXEC_OK && sdn->bv_val )
1653 sdn->bv_len = strlen( sdn->bv_val );
1656 #define REWRITE_VAL(si, ad, bv, bv2) \
1657 BER_BVZERO( &bv2 ); \
1658 if ( si->si_rewrite && ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName) \
1659 syncrepl_rewrite_dn( si, &bv, &bv2); \
1660 if ( BER_BVISNULL( &bv2 )) \
1661 ber_dupbv( &bv2, &bv )
1662 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1663 BER_BVZERO( &bv2 ); \
1664 if (si->si_rewrite) \
1665 syncrepl_rewrite_dn(si, &bv, &bv2); \
1666 rc = dnPrettyNormal( NULL, bv2.bv_val ? &bv2 : &bv, &dn, &ndn, op->o_tmpmemctx ); \
1669 #define REWRITE_VAL(si, ad, bv, bv2) ber_dupbv(&bv2, &bv)
1670 #define REWRITE_DN(si, bv, bv2, dn, ndn) \
1671 rc = dnPrettyNormal( NULL, &bv, &dn, &ndn, op->o_tmpmemctx )
1675 static slap_verbmasks modops[] = {
1676 { BER_BVC("add"), LDAP_REQ_ADD },
1677 { BER_BVC("delete"), LDAP_REQ_DELETE },
1678 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1679 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1684 syncrepl_accesslog_mods(
1686 struct berval *vals,
1687 struct Modifications **modres
1692 AttributeDescription *ad;
1693 struct berval bv, bv2;
1695 Modifications *mod = NULL, *modlist = NULL, **modtail;
1700 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1704 colon = ber_bvchr( &bv, ':' );
1710 bv.bv_len = colon - bv.bv_val;
1711 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1713 Debug( LDAP_DEBUG_ANY, "syncrepl_accesslog_mods: %s "
1714 "Invalid attribute %s, %s\n",
1715 si->si_ridtxt, bv.bv_val, text );
1716 slap_mods_free( modlist, 1 );
1722 /* Ignore dynamically generated attrs */
1723 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1727 /* Ignore excluded attrs */
1728 if ( ldap_charray_inlist( si->si_exattrs,
1729 ad->ad_type->sat_cname.bv_val ) )
1735 case '+': op = LDAP_MOD_ADD; break;
1736 case '-': op = LDAP_MOD_DELETE; break;
1737 case '=': op = LDAP_MOD_REPLACE; break;
1738 case '#': op = LDAP_MOD_INCREMENT; break;
1742 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1743 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1746 mod->sml_next = NULL;
1748 mod->sml_type = ad->ad_cname;
1749 mod->sml_values = NULL;
1750 mod->sml_nvalues = NULL;
1751 mod->sml_numvals = 0;
1754 modtail = &mod->sml_next;
1756 if ( colon[2] == ' ' ) {
1757 bv.bv_val = colon + 3;
1758 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1759 REWRITE_VAL( si, ad, bv, bv2 );
1760 ber_bvarray_add( &mod->sml_values, &bv2 );
1769 syncrepl_changelog_mods(
1771 struct berval *vals,
1772 struct Modifications **modres
1775 return -1; /* FIXME */
1778 typedef struct OpExtraSync {
1783 /* Copy the original modlist, split Replace ops into Delete/Add,
1784 * and drop mod opattrs since this modification is in the past.
1786 static Modifications *mods_dup( Operation *op, Modifications *modlist )
1788 Modifications *mod, *modnew = NULL, *modtail = NULL;
1790 for ( ; modlist; modlist = modlist->sml_next ) {
1791 if ( modlist->sml_desc == slap_schema.si_ad_modifiersName ||
1792 modlist->sml_desc == slap_schema.si_ad_modifyTimestamp ||
1793 modlist->sml_desc == slap_schema.si_ad_entryCSN )
1795 if ( modlist->sml_op == LDAP_MOD_REPLACE ) {
1796 mod = op->o_tmpalloc( sizeof(Modifications), op->o_tmpmemctx );
1797 mod->sml_desc = modlist->sml_desc;
1798 mod->sml_values = NULL;
1799 mod->sml_nvalues = NULL;
1800 mod->sml_op = LDAP_MOD_DELETE;
1801 mod->sml_numvals = 0;
1806 modtail->sml_next = mod;
1809 if ( modlist->sml_numvals ) {
1810 size = (modlist->sml_numvals+1) * sizeof(struct berval);
1811 if ( modlist->sml_nvalues ) size *= 2;
1815 size += sizeof(Modifications);
1816 mod = op->o_tmpalloc( size, op->o_tmpmemctx );
1820 modtail->sml_next = mod;
1822 mod->sml_desc = modlist->sml_desc;
1823 mod->sml_numvals = modlist->sml_numvals;
1825 if ( modlist->sml_numvals ) {
1827 mod->sml_values = (BerVarray)(mod+1);
1828 for (i=0; i<mod->sml_numvals; i++)
1829 mod->sml_values[i] = modlist->sml_values[i];
1830 if ( modlist->sml_nvalues ) {
1831 mod->sml_nvalues = mod->sml_values + mod->sml_numvals + 1;
1832 for (i=0; i<mod->sml_numvals; i++)
1833 mod->sml_nvalues[i] = modlist->sml_nvalues[i];
1836 if ( modlist->sml_op == LDAP_MOD_REPLACE )
1837 mod->sml_op = LDAP_MOD_ADD;
1839 mod->sml_op = modlist->sml_op;
1840 mod->sml_next = NULL;
1845 typedef struct resolve_ctxt {
1847 Modifications *rx_mods;
1851 compare_vals( Modifications *m1, Modifications *m2 )
1854 struct berval *bv1, *bv2;
1856 if ( m2->sml_nvalues ) {
1857 bv2 = m2->sml_nvalues;
1858 bv1 = m1->sml_nvalues;
1860 bv2 = m2->sml_values;
1861 bv1 = m1->sml_values;
1863 for ( j=0; j<m2->sml_numvals; j++ ) {
1864 for ( i=0; i<m1->sml_numvals; i++ ) {
1865 if ( !ber_bvcmp( &bv1[i], &bv2[j] )) {
1867 for ( k=i; k<m1->sml_numvals-1; k++ ) {
1868 m1->sml_values[k] = m1->sml_values[k+1];
1869 if ( m1->sml_nvalues )
1870 m1->sml_nvalues[k] = m1->sml_nvalues[k+1];
1880 syncrepl_resolve_cb( Operation *op, SlapReply *rs )
1882 if ( rs->sr_type == REP_SEARCH ) {
1883 resolve_ctxt *rx = op->o_callback->sc_private;
1884 Attribute *a = attr_find( rs->sr_entry->e_attrs, ad_reqMod );
1886 Modifications *oldmods, *newmods, *m1, *m2, **prev;
1887 oldmods = rx->rx_mods;
1888 syncrepl_accesslog_mods( rx->rx_si, a->a_vals, &newmods );
1889 for ( m2 = newmods; m2; m2=m2->sml_next ) {
1890 for ( prev = &oldmods, m1 = *prev; m1; m1 = *prev ) {
1891 if ( m1->sml_desc != m2->sml_desc ) {
1892 prev = &m1->sml_next;
1895 if ( m2->sml_op == LDAP_MOD_DELETE ||
1896 m2->sml_op == LDAP_MOD_REPLACE ) {
1897 int numvals = m2->sml_numvals;
1898 if ( m2->sml_op == LDAP_MOD_REPLACE )
1900 /* New delete All cancels everything */
1901 if ( numvals == 0 ) {
1903 *prev = m1->sml_next;
1904 op->o_tmpfree( m1, op->o_tmpmemctx );
1907 if ( m1->sml_op == LDAP_MOD_DELETE ) {
1908 if ( m1->sml_numvals == 0 ) {
1909 /* turn this to SOFTDEL later */
1910 m1->sml_flags = SLAP_MOD_INTERNAL;
1912 compare_vals( m1, m2 );
1913 if ( !m1->sml_numvals )
1916 } else if ( m1->sml_op == LDAP_MOD_ADD ) {
1917 compare_vals( m1, m2 );
1918 if ( !m1->sml_numvals )
1923 if ( m2->sml_op == LDAP_MOD_ADD ||
1924 m2->sml_op == LDAP_MOD_REPLACE ) {
1925 if ( m1->sml_op == LDAP_MOD_DELETE ) {
1926 if ( !m1->sml_numvals ) goto drop;
1927 compare_vals( m1, m2 );
1928 if ( !m1->sml_numvals )
1931 if ( m2->sml_desc->ad_type->sat_atype.at_single_value )
1933 compare_vals( m1, m2 );
1934 if ( !m1->sml_numvals )
1937 prev = &m1->sml_next;
1940 slap_mods_free( newmods, 1 );
1943 return LDAP_SUCCESS;
1946 typedef struct modify_ctxt {
1947 Modifications *mx_orig;
1948 Modifications *mx_free;
1952 syncrepl_modify_cb( Operation *op, SlapReply *rs )
1954 slap_callback *sc = op->o_callback;
1955 modify_ctxt *mx = sc->sc_private;
1958 op->o_callback = sc->sc_next;
1959 op->o_tmpfree( sc, op->o_tmpmemctx );
1961 op->orm_no_opattrs = 0;
1962 op->orm_modlist = mx->mx_orig;
1963 for ( ml = mx->mx_free; ml; ml = mx->mx_free ) {
1964 mx->mx_free = ml->sml_next;
1965 op->o_tmpfree( ml, op->o_tmpmemctx );
1967 return SLAP_CB_CONTINUE;
1971 syncrepl_op_modify( Operation *op, SlapReply *rs )
1973 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1978 Modifications *mod, *newlist;
1980 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
1981 if ( oex->oe_key == (void *)syncrepl_message_to_op )
1985 return SLAP_CB_CONTINUE;
1987 si = ((OpExtraSync *)oex)->oe_si;
1989 /* Check if entryCSN in modlist is newer than entryCSN in entry.
1990 * We do it here because the op has been serialized by accesslog
1991 * by the time we get here. If the CSN is new enough, just do the
1992 * mod. If not, we need to resolve conflicts.
1995 for ( mod = op->orm_modlist; mod; mod=mod->sml_next ) {
1996 if ( mod->sml_desc == slap_schema.si_ad_entryCSN ) break;
1998 /* FIXME: what should we do if entryCSN is missing from the mod? */
2000 return SLAP_CB_CONTINUE;
2002 rc = overlay_entry_get_ov( op, &op->o_req_ndn, NULL, NULL, 0, &e, on );
2006 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
2007 value_match( &match, slap_schema.si_ad_entryCSN,
2008 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
2009 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
2010 &mod->sml_nvalues[0], &a->a_nvals[0], &text );
2011 overlay_entry_release_ov( op, e, 0, on );
2013 /* mod is newer, let it go */
2015 for ( mod = op->orm_modlist; mod; mod=mod->sml_next ) {
2016 if ( mod->sml_op == LDAP_MOD_DELETE )
2017 mod->sml_op = SLAP_MOD_SOFTDEL;
2019 return SLAP_CB_CONTINUE;
2021 /* equal? Should never happen */
2023 return LDAP_SUCCESS;
2025 /* mod is older: resolve conflicts...
2026 * 1. Save/copy original modlist. Split Replace to Del/Add.
2027 * 2. Find all mods to this reqDN newer than the mod stamp.
2028 * 3. Resolve any mods in this request that affect attributes
2029 * touched by newer mods.
2031 * delete all delete all drop
2032 * delete all delete X SOFTDEL
2033 * delete X delete all drop
2034 * delete X delete X drop
2035 * delete X delete Y OK
2036 * delete all add X drop
2037 * delete X add X drop
2039 * add X delete all drop
2040 * add X delete X drop
2042 * add X add Y if SV, drop else OK
2044 * 4. Swap original modlist back in response callback so
2045 * that accesslog logs the original mod.
2048 newlist = mods_dup( op, op->orm_modlist );
2051 Operation op2 = *op;
2052 AttributeName an[2];
2058 SlapReply rs1 = {0};
2059 resolve_ctxt rx = { si, newlist };
2061 op2.o_tag = LDAP_REQ_SEARCH;
2062 op2.ors_scope = LDAP_SCOPE_SUBTREE;
2063 op2.ors_deref = LDAP_DEREF_NEVER;
2064 op2.o_req_dn = si->si_logbase;
2065 op2.o_req_ndn = si->si_logbase;
2066 op2.ors_tlimit = SLAP_NO_LIMIT;
2067 op2.ors_slimit = SLAP_NO_LIMIT;
2068 op2.ors_limit = NULL;
2069 memset( an, 0, sizeof(an));
2070 an[0].an_desc = ad_reqMod;
2071 an[0].an_name = ad_reqMod->ad_cname;
2073 op2.ors_attrsonly = 0;
2075 bv = mod->sml_nvalues[0];
2077 size = sizeof("(&(entryCSN>=)(reqDN=))");
2078 size += bv.bv_len + op->o_req_ndn.bv_len + si->si_logfilterstr.bv_len;
2079 op2.ors_filterstr.bv_val = op->o_tmpalloc( size, op->o_tmpmemctx );
2080 op2.ors_filterstr.bv_len = sprintf(op2.ors_filterstr.bv_val,
2081 "(&(entryCSN>=%s)(reqDN=%s)%s)",
2082 bv.bv_val, op->o_req_ndn.bv_val, si->si_logfilterstr.bv_val );
2083 op2.ors_filter = str2filter_x( op, op2.ors_filterstr.bv_val );
2085 op2.o_callback = &cb;
2086 cb.sc_response = syncrepl_resolve_cb;
2087 cb.sc_private = ℞
2088 op2.o_bd = select_backend( &op2.o_req_ndn, 1 );
2089 op2.o_bd->be_search( &op2, &rs1 );
2090 newlist = rx.rx_mods;
2094 slap_callback *sc = op->o_tmpalloc( sizeof(slap_callback) +
2095 sizeof(modify_ctxt), op->o_tmpmemctx );
2096 modify_ctxt *mx = (modify_ctxt *)(sc+1);
2099 sc->sc_response = syncrepl_modify_cb;
2100 sc->sc_private = mx;
2101 sc->sc_next = op->o_callback;
2102 op->o_callback = sc;
2103 op->orm_no_opattrs = 1;
2104 mx->mx_orig = op->orm_modlist;
2105 mx->mx_free = newlist;
2106 for ( ml = newlist; ml; ml=ml->sml_next ) {
2107 if ( ml->sml_flags == SLAP_MOD_INTERNAL ) {
2109 ml->sml_op = SLAP_MOD_SOFTDEL;
2112 op->orm_modlist = newlist;
2113 op->o_csn = mod->sml_nvalues[0];
2115 return SLAP_CB_CONTINUE;
2119 syncrepl_message_to_op(
2125 BerElement *ber = NULL;
2126 Modifications *modlist = NULL;
2128 SlapReply rs = { REP_RESULT };
2129 slap_callback cb = { NULL, null_callback, NULL, NULL };
2132 char txtbuf[SLAP_TEXT_BUFLEN];
2133 size_t textlen = sizeof txtbuf;
2135 struct berval bdn, dn = BER_BVNULL, ndn;
2136 struct berval bv, bv2, *bvals = NULL;
2137 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
2138 prdn = BER_BVNULL, nrdn = BER_BVNULL,
2139 psup = BER_BVNULL, nsup = BER_BVNULL;
2140 int rc, deleteOldRdn = 0, freeReqDn = 0;
2141 int do_graduate = 0;
2143 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2144 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2145 "Message type should be entry (%d)",
2146 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2150 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
2155 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2157 if ( rc != LDAP_SUCCESS ) {
2158 Debug( LDAP_DEBUG_ANY,
2159 "syncrepl_message_to_op: %s dn get failed (%d)",
2160 si->si_ridtxt, rc, 0 );
2164 op->o_tag = LBER_DEFAULT;
2165 op->o_bd = si->si_wbe;
2167 if ( BER_BVISEMPTY( &bdn )) {
2168 Debug( LDAP_DEBUG_ANY,
2169 "syncrepl_message_to_op: %s got empty dn",
2170 si->si_ridtxt, 0, 0 );
2174 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
2176 if ( bv.bv_val == NULL )
2179 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
2181 REWRITE_DN( si, bdn, bv2, dn, ndn );
2182 if ( rc != LDAP_SUCCESS ) {
2183 Debug( LDAP_DEBUG_ANY,
2184 "syncrepl_message_to_op: %s "
2185 "dn \"%s\" normalization failed (%d)",
2186 si->si_ridtxt, bdn.bv_val, rc );
2191 ber_dupbv( &op->o_req_dn, &dn );
2192 ber_dupbv( &op->o_req_ndn, &ndn );
2193 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2194 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2196 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
2197 int i = verb_to_mask( bvals[0].bv_val, modops );
2199 Debug( LDAP_DEBUG_ANY,
2200 "syncrepl_message_to_op: %s unknown op %s",
2201 si->si_ridtxt, bvals[0].bv_val, 0 );
2206 op->o_tag = modops[i].mask;
2207 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
2208 /* Parse attribute into modlist */
2209 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
2210 rc = syncrepl_accesslog_mods( si, bvals, &modlist );
2212 rc = syncrepl_changelog_mods( si, bvals, &modlist );
2214 if ( rc ) goto done;
2215 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
2217 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
2218 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
2221 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
2223 } else if ( !ber_bvstrcasecmp( &bv,
2224 &slap_schema.si_ad_entryCSN->ad_cname ) )
2226 slap_queue_csn( op, bvals );
2232 /* If we didn't get a mod type or a target DN, bail out */
2233 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
2238 op->o_callback = &cb;
2239 slap_op_time( &op->o_time, &op->o_tincr );
2241 switch( op->o_tag ) {
2243 case LDAP_REQ_MODIFY:
2244 /* If we didn't get required data, bail */
2245 if ( !modlist ) goto done;
2247 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
2249 if ( rc != LDAP_SUCCESS ) {
2250 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2251 "mods check (%s)\n",
2252 si->si_ridtxt, text, 0 );
2256 if ( op->o_tag == LDAP_REQ_ADD ) {
2257 Entry *e = entry_alloc();
2259 op->ora_e->e_name = op->o_req_dn;
2260 op->ora_e->e_nname = op->o_req_ndn;
2262 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
2263 if( rc != LDAP_SUCCESS ) {
2264 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
2265 "mods2entry (%s)\n",
2266 si->si_ridtxt, text, 0 );
2268 rc = op->o_bd->be_add( op, &rs );
2269 Debug( LDAP_DEBUG_SYNC,
2270 "syncrepl_message_to_op: %s be_add %s (%d)\n",
2271 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2274 if ( e == op->ora_e )
2275 be_entry_release_w( op, op->ora_e );
2278 op->orm_modlist = modlist;
2279 op->o_bd = si->si_wbe;
2280 /* delta-mmr needs additional checks in syncrepl_op_modify */
2281 if ( SLAP_MULTIMASTER( op->o_bd )) {
2282 oes.oe.oe_key = (void *)syncrepl_message_to_op;
2284 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &oes.oe, oe_next );
2286 rc = op->o_bd->be_modify( op, &rs );
2287 if ( SLAP_MULTIMASTER( op->o_bd )) {
2288 LDAP_SLIST_REMOVE( &op->o_extra, &oes.oe, OpExtra, oe_next );
2290 modlist = op->orm_modlist;
2291 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2292 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
2293 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2294 op->o_bd = si->si_be;
2298 case LDAP_REQ_MODRDN:
2299 if ( BER_BVISNULL( &rdn ) ) goto done;
2301 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
2304 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
2307 if ( !BER_BVISNULL( &sup ) ) {
2308 REWRITE_DN( si, sup, bv2, psup, nsup );
2311 op->orr_newSup = &psup;
2312 op->orr_nnewSup = ⊅
2314 op->orr_newSup = NULL;
2315 op->orr_nnewSup = NULL;
2317 op->orr_newrdn = prdn;
2318 op->orr_nnewrdn = nrdn;
2319 op->orr_deleteoldrdn = deleteOldRdn;
2320 op->orr_modlist = NULL;
2321 if ( slap_modrdn2mods( op, &rs ) ) {
2325 /* Append modlist for operational attrs */
2329 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
2331 m->sml_next = modlist;
2334 rc = op->o_bd->be_modrdn( op, &rs );
2335 slap_mods_free( op->orr_modlist, 1 );
2336 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2337 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
2338 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2341 case LDAP_REQ_DELETE:
2342 rc = op->o_bd->be_delete( op, &rs );
2343 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
2344 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
2345 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2351 slap_graduate_commit_csn( op );
2352 op->o_bd = si->si_be;
2353 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2354 BER_BVZERO( &op->o_csn );
2356 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
2358 if ( !BER_BVISNULL( &rdn ) ) {
2359 if ( !BER_BVISNULL( &nsup ) ) {
2360 ch_free( nsup.bv_val );
2362 if ( !BER_BVISNULL( &psup ) ) {
2363 ch_free( psup.bv_val );
2365 if ( !BER_BVISNULL( &nrdn ) ) {
2366 ch_free( nrdn.bv_val );
2368 if ( !BER_BVISNULL( &prdn ) ) {
2369 ch_free( prdn.bv_val );
2373 ch_free( op->o_req_ndn.bv_val );
2374 ch_free( op->o_req_dn.bv_val );
2381 syncrepl_message_to_entry(
2385 Modifications **modlist,
2391 BerElement *ber = NULL;
2394 Modifications **modtail = modlist;
2397 char txtbuf[SLAP_TEXT_BUFLEN];
2398 size_t textlen = sizeof txtbuf;
2400 struct berval bdn = BER_BVNULL, dn, ndn, bv2;
2405 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
2406 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
2407 "Message type should be entry (%d)",
2408 si->si_ridtxt, ldap_msgtype( msg ), 0 );
2412 op->o_tag = LDAP_REQ_ADD;
2414 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
2415 if ( rc != LDAP_SUCCESS ) {
2416 Debug( LDAP_DEBUG_ANY,
2417 "syncrepl_message_to_entry: %s dn get failed (%d)",
2418 si->si_ridtxt, rc, 0 );
2422 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
2423 Debug( LDAP_DEBUG_ANY,
2424 "syncrepl_message_to_entry: %s got empty dn",
2425 si->si_ridtxt, 0, 0 );
2429 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
2430 /* NOTE: this could be done even before decoding the DN,
2431 * although encoding errors wouldn't be detected */
2436 if ( entry == NULL ) {
2440 REWRITE_DN( si, bdn, bv2, dn, ndn );
2441 if ( rc != LDAP_SUCCESS ) {
2442 /* One of the things that could happen is that the schema
2443 * is not lined-up; this could result in unknown attributes.
2444 * A value non conformant to the syntax should be unlikely,
2445 * except when replicating between different versions
2446 * of the software, or when syntax validation bugs are fixed
2448 Debug( LDAP_DEBUG_ANY,
2449 "syncrepl_message_to_entry: "
2450 "%s dn \"%s\" normalization failed (%d)",
2451 si->si_ridtxt, bdn.bv_val, rc );
2455 ber_dupbv( &op->o_req_dn, &dn );
2456 ber_dupbv( &op->o_req_ndn, &ndn );
2457 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
2458 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
2460 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
2463 e->e_name = op->o_req_dn;
2464 e->e_nname = op->o_req_ndn;
2466 while ( ber_remaining( ber ) ) {
2467 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
2468 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
2473 /* Drop all updates to the contextCSN of the context entry
2476 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
2477 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
2478 ber_bvarray_free( tmp.sml_values );
2482 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
2484 mod->sml_op = LDAP_MOD_REPLACE;
2486 mod->sml_next = NULL;
2487 mod->sml_desc = NULL;
2488 mod->sml_type = tmp.sml_type;
2489 mod->sml_values = tmp.sml_values;
2490 mod->sml_nvalues = NULL;
2491 mod->sml_numvals = 0; /* slap_mods_check will set this */
2493 #ifdef ENABLE_REWRITE
2494 if (si->si_rewrite) {
2495 AttributeDescription *ad = NULL;
2496 slap_bv2ad( &tmp.sml_type, &ad, &text );
2499 mod->sml_type = ad->ad_cname;
2500 if ( ad->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
2502 for ( i = 0; tmp.sml_values[i].bv_val; i++ ) {
2503 syncrepl_rewrite_dn( si, &tmp.sml_values[i], &bv2);
2504 if ( !BER_BVISNULL( &bv2 )) {
2505 ber_memfree( tmp.sml_values[i].bv_val );
2506 tmp.sml_values[i] = bv2;
2514 modtail = &mod->sml_next;
2517 if ( *modlist == NULL ) {
2518 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
2519 si->si_ridtxt, 0, 0 );
2524 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
2526 if ( rc != LDAP_SUCCESS ) {
2527 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
2528 si->si_ridtxt, text, 0 );
2532 /* Strip out dynamically generated attrs */
2533 for ( modtail = modlist; *modtail ; ) {
2535 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
2536 *modtail = mod->sml_next;
2537 slap_mod_free( &mod->sml_mod, 0 );
2540 modtail = &mod->sml_next;
2544 /* Strip out attrs in exattrs list */
2545 for ( modtail = modlist; *modtail ; ) {
2547 if ( ldap_charray_inlist( si->si_exattrs,
2548 mod->sml_desc->ad_type->sat_cname.bv_val ) )
2550 *modtail = mod->sml_next;
2551 slap_mod_free( &mod->sml_mod, 0 );
2554 modtail = &mod->sml_next;
2558 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
2559 if( rc != LDAP_SUCCESS ) {
2560 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
2561 si->si_ridtxt, text, 0 );
2566 if ( rc != LDAP_SUCCESS ) {
2578 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2580 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2581 * entry if a previous refresh was interrupted before sending us a new
2582 * context state. We try to compare the new entry to the existing entry
2583 * and ignore the new entry if they are the same.
2585 * Also, we may get an update where the entryDN has changed, due to
2586 * a ModDn on the provider. We detect this as well, so we can issue
2587 * the corresponding operation locally.
2589 * In the case of a modify, we get a list of all the attributes
2590 * in the original entry. Rather than deleting the entry and re-adding it,
2591 * we issue a Modify request that deletes all the attributes and adds all
2592 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2595 * We otherwise distinguish ModDN from Modify; in the case of
2596 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2597 * operational attributes from the entry, and do a regular ModDN.
2599 typedef struct dninfo {
2603 struct berval nnewSup;
2604 int renamed; /* Was an existing entry renamed? */
2605 int delOldRDN; /* Was old RDN deleted? */
2606 Modifications **modlist; /* the modlist we received */
2607 Modifications *mods; /* the modlist we compared */
2608 Attribute *oldNattr; /* old naming attr */
2609 AttributeDescription *oldDesc; /* for renames */
2610 AttributeDescription *newDesc; /* for renames */
2613 /* return 1 if inserted, 0 otherwise */
2615 avl_presentlist_insert(
2617 struct berval *syncUUID )
2619 struct berval *syncuuid_bv = ch_malloc( sizeof( struct berval ) + syncUUID->bv_len + 1 );
2621 syncuuid_bv->bv_len = syncUUID->bv_len;
2622 syncuuid_bv->bv_val = (char *)&syncuuid_bv[1];
2623 AC_MEMCPY( syncuuid_bv->bv_val, syncUUID->bv_val, syncUUID->bv_len );
2624 syncuuid_bv->bv_val[ syncuuid_bv->bv_len ] = '\0';
2626 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
2627 syncuuid_cmp, avl_dup_error ) )
2629 ch_free( syncuuid_bv );
2641 Modifications** modlist,
2643 struct berval* syncUUID,
2644 struct berval* syncCSN )
2646 Backend *be = op->o_bd;
2647 slap_callback cb = { NULL, NULL, NULL, NULL };
2648 int syncuuid_inserted = 0;
2649 struct berval syncUUID_strrep = BER_BVNULL;
2651 SlapReply rs_search = {REP_RESULT};
2653 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2654 int rc = LDAP_SUCCESS;
2656 struct berval pdn = BER_BVNULL;
2661 Debug( LDAP_DEBUG_SYNC,
2662 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2663 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2665 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2666 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2667 syncuuid_inserted = avl_presentlist_insert( si, syncUUID );
2671 if ( syncstate == LDAP_SYNC_PRESENT ) {
2673 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2674 if ( entry == NULL ) {
2679 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
2680 if ( syncstate != LDAP_SYNC_DELETE ) {
2681 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2684 /* add if missing */
2685 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2686 &syncUUID_strrep, syncUUID );
2688 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2689 /* replace only if necessary */
2690 if ( a->a_nvals != a->a_vals ) {
2691 ber_memfree( a->a_nvals[0].bv_val );
2692 ber_dupbv( &a->a_nvals[0], syncUUID );
2694 ber_memfree( a->a_vals[0].bv_val );
2695 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
2699 f.f_choice = LDAP_FILTER_EQUALITY;
2701 ava.aa_desc = slap_schema.si_ad_entryUUID;
2702 ava.aa_value = *syncUUID;
2704 if ( syncuuid_inserted ) {
2705 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2706 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
2708 op->ors_filter = &f;
2710 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
2711 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2712 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2713 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2714 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2715 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
2716 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2717 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2719 op->o_tag = LDAP_REQ_SEARCH;
2720 op->ors_scope = LDAP_SCOPE_SUBTREE;
2721 op->ors_deref = LDAP_DEREF_NEVER;
2723 /* get the entry for this UUID */
2724 #ifdef ENABLE_REWRITE
2725 if ( si->si_rewrite ) {
2726 op->o_req_dn = si->si_suffixm;
2727 op->o_req_ndn = si->si_suffixm;
2731 op->o_req_dn = si->si_base;
2732 op->o_req_ndn = si->si_base;
2735 op->o_time = slap_get_time();
2736 op->ors_tlimit = SLAP_NO_LIMIT;
2738 op->ors_limit = NULL;
2740 op->ors_attrs = slap_anlist_all_attributes;
2741 op->ors_attrsonly = 0;
2743 /* set callback function */
2744 op->o_callback = &cb;
2745 cb.sc_response = dn_callback;
2746 cb.sc_private = &dni;
2747 dni.new_entry = entry;
2748 dni.modlist = modlist;
2750 rc = be->be_search( op, &rs_search );
2751 Debug( LDAP_DEBUG_SYNC,
2752 "syncrepl_entry: %s be_search (%d)\n",
2753 si->si_ridtxt, rc, 0 );
2755 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2756 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2759 cb.sc_response = null_callback;
2762 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2763 Debug( LDAP_DEBUG_SYNC,
2764 "syncrepl_entry: %s %s\n",
2765 si->si_ridtxt, entry->e_name.bv_val, 0 );
2767 Debug( LDAP_DEBUG_SYNC,
2768 "syncrepl_entry: %s %s\n",
2769 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2772 assert( BER_BVISNULL( &op->o_csn ) );
2774 slap_queue_csn( op, syncCSN );
2777 slap_op_time( &op->o_time, &op->o_tincr );
2778 switch ( syncstate ) {
2780 case LDAP_SYNC_MODIFY:
2781 if ( BER_BVISNULL( &op->o_csn ))
2784 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2786 /* FIXME: op->o_csn is assumed to be
2787 * on the thread's slab; this needs
2788 * to be cleared ASAP.
2789 * What happens if already present?
2791 assert( BER_BVISNULL( &op->o_csn ) );
2792 op->o_csn = a->a_vals[0];
2797 if ( BER_BVISNULL( &dni.dn ) ) {
2798 SlapReply rs_add = {REP_RESULT};
2800 op->o_req_dn = entry->e_name;
2801 op->o_req_ndn = entry->e_nname;
2802 op->o_tag = LDAP_REQ_ADD;
2804 op->o_bd = si->si_wbe;
2806 rc = op->o_bd->be_add( op, &rs_add );
2807 Debug( LDAP_DEBUG_SYNC,
2808 "syncrepl_entry: %s be_add %s (%d)\n",
2809 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2810 switch ( rs_add.sr_err ) {
2812 if ( op->ora_e == entry ) {
2813 be_entry_release_w( op, entry );
2819 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2820 * only if the suffix entry is not present */
2821 case LDAP_NO_SUCH_OBJECT:
2822 rc = syncrepl_add_glue( op, entry );
2826 /* if an entry was added via syncrepl_add_glue(),
2827 * it likely has no entryUUID, so the previous
2828 * be_search() doesn't find it. In this case,
2829 * give syncrepl a chance to modify it. Also
2830 * allow for entries that were recreated with the
2831 * same DN but a different entryUUID.
2833 case LDAP_ALREADY_EXISTS:
2835 Operation op2 = *op;
2836 SlapReply rs2 = { REP_RESULT };
2837 slap_callback cb2 = { 0 };
2840 op2.o_tag = LDAP_REQ_SEARCH;
2841 op2.o_req_dn = entry->e_name;
2842 op2.o_req_ndn = entry->e_nname;
2843 op2.ors_scope = LDAP_SCOPE_BASE;
2844 op2.ors_deref = LDAP_DEREF_NEVER;
2845 op2.ors_attrs = slap_anlist_all_attributes;
2846 op2.ors_attrsonly = 0;
2847 op2.ors_limit = NULL;
2849 op2.ors_tlimit = SLAP_NO_LIMIT;
2851 f.f_choice = LDAP_FILTER_PRESENT;
2852 f.f_desc = slap_schema.si_ad_objectClass;
2853 op2.ors_filter = &f;
2854 op2.ors_filterstr = generic_filterstr;
2856 op2.o_callback = &cb2;
2857 cb2.sc_response = dn_callback;
2858 cb2.sc_private = &dni;
2860 rc = be->be_search( &op2, &rs2 );
2861 if ( rc ) goto done;
2864 slap_op_time( &op->o_time, &op->o_tincr );
2870 Debug( LDAP_DEBUG_ANY,
2871 "syncrepl_entry: %s be_add %s failed (%d)\n",
2872 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
2880 op->o_req_dn = dni.dn;
2881 op->o_req_ndn = dni.ndn;
2882 if ( dni.renamed ) {
2883 struct berval noldp, newp;
2884 Modifications *mod, **modtail, **ml, *m2;
2885 int i, got_replace = 0, just_rename = 0;
2886 SlapReply rs_modify = {REP_RESULT};
2888 op->o_tag = LDAP_REQ_MODRDN;
2889 dnRdn( &entry->e_name, &op->orr_newrdn );
2890 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2892 if ( !BER_BVISNULL( &dni.nnewSup )) {
2893 dnParent( &entry->e_name, &newp );
2894 op->orr_newSup = &newp;
2895 op->orr_nnewSup = &dni.nnewSup;
2897 op->orr_newSup = NULL;
2898 op->orr_nnewSup = NULL;
2900 op->orr_deleteoldrdn = dni.delOldRDN;
2901 op->orr_modlist = NULL;
2902 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2906 /* Drop the RDN-related mods from this op, because their
2907 * equivalents were just setup by slap_modrdn2mods.
2909 * If delOldRDN is TRUE then we should see a delete modop
2910 * for oldDesc. We might see a replace instead.
2911 * delete with no values: therefore newDesc != oldDesc.
2912 * if oldNattr had only one value, then Drop this op.
2913 * delete with 1 value: can only be the oldRDN value. Drop op.
2914 * delete with N values: Drop oldRDN value, keep remainder.
2915 * replace with 1 value: if oldNattr had only one value and
2916 * newDesc == oldDesc, Drop this op.
2917 * Any other cases must be left intact.
2919 * We should also see an add modop for newDesc. (But not if
2920 * we got a replace modop due to delOldRDN.) If it has
2921 * multiple values, we'll have to drop the new RDN value.
2923 modtail = &op->orr_modlist;
2924 if ( dni.delOldRDN ) {
2925 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2926 if ( (*ml)->sml_desc == dni.oldDesc ) {
2928 if ( mod->sml_op == LDAP_MOD_REPLACE &&
2929 dni.oldDesc != dni.newDesc ) {
2930 /* This Replace is due to other Mods.
2935 if ( mod->sml_numvals <= 1 &&
2936 dni.oldNattr->a_numvals == 1 &&
2937 ( mod->sml_op == LDAP_MOD_DELETE ||
2938 mod->sml_op == LDAP_MOD_REPLACE )) {
2939 if ( mod->sml_op == LDAP_MOD_REPLACE )
2942 *ml = mod->sml_next;
2943 mod->sml_next = NULL;
2944 slap_mods_free( mod, 1 );
2947 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
2949 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2950 if ( m2->sml_desc == dni.oldDesc &&
2951 m2->sml_op == LDAP_MOD_DELETE ) break;
2953 for ( i=0; i<mod->sml_numvals; i++ ) {
2954 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2956 ch_free( mod->sml_values[i].bv_val );
2957 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2958 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2959 if ( mod->sml_nvalues ) {
2960 ch_free( mod->sml_nvalues[i].bv_val );
2961 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2962 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2967 if ( !mod->sml_numvals ) {
2969 *ml = mod->sml_next;
2970 mod->sml_next = NULL;
2971 slap_mods_free( mod, 1 );
2977 if ( !got_replace ) {
2978 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2979 if ( (*ml)->sml_desc == dni.newDesc ) {
2981 if ( mod->sml_op != LDAP_MOD_ADD )
2983 if ( mod->sml_numvals == 1 ) {
2985 *ml = mod->sml_next;
2986 mod->sml_next = NULL;
2987 slap_mods_free( mod, 1 );
2990 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2991 if ( m2->sml_desc == dni.oldDesc &&
2992 m2->sml_op == SLAP_MOD_SOFTADD ) break;
2994 for ( i=0; i<mod->sml_numvals; i++ ) {
2995 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2997 ch_free( mod->sml_values[i].bv_val );
2998 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2999 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
3000 if ( mod->sml_nvalues ) {
3001 ch_free( mod->sml_nvalues[i].bv_val );
3002 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
3003 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
3013 /* RDNs must be NUL-terminated for back-ldap */
3014 noldp = op->orr_newrdn;
3015 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
3016 noldp = op->orr_nnewrdn;
3017 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
3019 /* Setup opattrs too */
3021 static AttributeDescription *nullattr = NULL;
3022 static AttributeDescription **const opattrs[] = {
3023 &slap_schema.si_ad_entryCSN,
3024 &slap_schema.si_ad_modifiersName,
3025 &slap_schema.si_ad_modifyTimestamp,
3028 AttributeDescription *opattr;
3032 /* pull mod off incoming modlist */
3033 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
3034 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
3036 if ( (*ml)->sml_desc == opattr ) {
3038 *ml = mod->sml_next;
3039 mod->sml_next = NULL;
3041 modtail = &mod->sml_next;
3046 /* If there are still Modifications left, put the opattrs
3047 * back, and let be_modify run. Otherwise, append the opattrs
3048 * to the orr_modlist.
3052 /* don't set a CSN for the rename op */
3054 slap_graduate_commit_csn( op );
3056 mod = op->orr_modlist;
3059 for ( ; mod->sml_next; mod=mod->sml_next );
3062 op->o_bd = si->si_wbe;
3064 rs_reinit( &rs_modify, REP_RESULT );
3065 rc = op->o_bd->be_modrdn( op, &rs_modify );
3067 /* NOTE: noSuchObject should result because the new superior
3068 * has not been added yet (ITS#6472) */
3069 if ( rc == LDAP_NO_SUCH_OBJECT && !BER_BVISNULL( op->orr_nnewSup )) {
3070 Operation op2 = *op;
3071 rc = syncrepl_add_glue_ancestors( &op2, entry );
3072 if ( rc == LDAP_SUCCESS ) {
3077 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
3078 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
3080 slap_mods_free( op->orr_modlist, 1 );
3081 Debug( LDAP_DEBUG_SYNC,
3082 "syncrepl_entry: %s be_modrdn %s (%d)\n",
3083 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3085 /* Renamed entries may still have other mods so just fallthru */
3086 op->o_req_dn = entry->e_name;
3087 op->o_req_ndn = entry->e_nname;
3088 /* Use CSN on the modify */
3092 slap_queue_csn( op, syncCSN );
3095 SlapReply rs_modify = {REP_RESULT};
3097 op->o_tag = LDAP_REQ_MODIFY;
3098 op->orm_modlist = dni.mods;
3099 op->orm_no_opattrs = 1;
3100 op->o_bd = si->si_wbe;
3102 rc = op->o_bd->be_modify( op, &rs_modify );
3103 slap_mods_free( op->orm_modlist, 1 );
3104 op->orm_no_opattrs = 0;
3105 Debug( LDAP_DEBUG_SYNC,
3106 "syncrepl_entry: %s be_modify %s (%d)\n",
3107 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3108 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
3109 Debug( LDAP_DEBUG_ANY,
3110 "syncrepl_entry: %s be_modify failed (%d)\n",
3111 si->si_ridtxt, rs_modify.sr_err, 0 );
3115 } else if ( !dni.renamed ) {
3116 Debug( LDAP_DEBUG_SYNC,
3117 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
3118 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
3120 slap_graduate_commit_csn( op );
3125 case LDAP_SYNC_DELETE :
3126 if ( !BER_BVISNULL( &dni.dn ) ) {
3127 SlapReply rs_delete = {REP_RESULT};
3128 op->o_req_dn = dni.dn;
3129 op->o_req_ndn = dni.ndn;
3130 op->o_tag = LDAP_REQ_DELETE;
3131 op->o_bd = si->si_wbe;
3132 rc = op->o_bd->be_delete( op, &rs_delete );
3133 Debug( LDAP_DEBUG_SYNC,
3134 "syncrepl_entry: %s be_delete %s (%d)\n",
3135 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3137 while ( rs_delete.sr_err == LDAP_SUCCESS
3138 && op->o_delete_glue_parent ) {
3139 op->o_delete_glue_parent = 0;
3140 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3141 slap_callback cb = { NULL };
3142 cb.sc_response = slap_null_cb;
3143 dnParent( &op->o_req_ndn, &pdn );
3145 op->o_req_ndn = pdn;
3146 op->o_callback = &cb;
3147 rs_reinit( &rs_delete, REP_RESULT );
3148 op->o_bd->be_delete( op, &rs_delete );
3159 Debug( LDAP_DEBUG_ANY,
3160 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
3165 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
3166 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
3167 BER_BVZERO( &syncUUID_strrep );
3169 if ( !BER_BVISNULL( &dni.ndn ) ) {
3170 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
3172 if ( !BER_BVISNULL( &dni.dn ) ) {
3173 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
3176 entry_free( entry );
3179 slap_graduate_commit_csn( op );
3181 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
3182 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3184 BER_BVZERO( &op->o_csn );
3188 static struct berval gcbva[] = {
3194 #define NP_DELETE_ONE 2
3197 syncrepl_del_nonpresent(
3201 struct sync_cookie *sc,
3204 Backend* be = op->o_bd;
3205 slap_callback cb = { NULL };
3206 struct nonpresent_entry *np_list, *np_prev;
3208 AttributeName an[2];
3210 struct berval pdn = BER_BVNULL;
3213 #ifdef ENABLE_REWRITE
3214 if ( si->si_rewrite ) {
3215 op->o_req_dn = si->si_suffixm;
3216 op->o_req_ndn = si->si_suffixm;
3220 op->o_req_dn = si->si_base;
3221 op->o_req_ndn = si->si_base;
3224 cb.sc_response = nonpresent_callback;
3227 op->o_callback = &cb;
3228 op->o_tag = LDAP_REQ_SEARCH;
3229 op->ors_scope = si->si_scope;
3230 op->ors_deref = LDAP_DEREF_NEVER;
3231 op->o_time = slap_get_time();
3232 op->ors_tlimit = SLAP_NO_LIMIT;
3237 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
3240 op->ors_attrsonly = 1;
3241 op->ors_attrs = slap_anlist_no_attrs;
3242 op->ors_limit = NULL;
3243 op->ors_filter = &uf;
3246 uf.f_av_desc = slap_schema.si_ad_entryUUID;
3248 uf.f_choice = LDAP_FILTER_EQUALITY;
3249 si->si_refreshDelete |= NP_DELETE_ONE;
3251 for (i=0; uuids[i].bv_val; i++) {
3252 SlapReply rs_search = {REP_RESULT};
3255 uf.f_av_value = uuids[i];
3256 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3257 rc = be->be_search( op, &rs_search );
3258 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3260 si->si_refreshDelete ^= NP_DELETE_ONE;
3264 AttributeAssertion mmaa;
3265 SlapReply rs_search = {REP_RESULT};
3267 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
3268 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
3269 an[0].an_desc = slap_schema.si_ad_entryUUID;
3271 op->ors_slimit = SLAP_NO_LIMIT;
3272 op->ors_tlimit = SLAP_NO_LIMIT;
3273 op->ors_limit = NULL;
3274 op->ors_attrsonly = 0;
3275 op->ors_filter = filter_dup( si->si_filter, op->o_tmpmemctx );
3276 /* In multimaster, updates can continue to arrive while
3277 * we're searching. Limit the search result to entries
3278 * older than our newest cookie CSN.
3280 if ( SLAP_MULTIMASTER( op->o_bd )) {
3285 f->f_choice = LDAP_FILTER_AND;
3286 f->f_next = op->ors_filter;
3290 f->f_choice = LDAP_FILTER_LE;
3292 f->f_av_desc = slap_schema.si_ad_entryCSN;
3294 BER_BVZERO( &f->f_av_value );
3295 for ( i=0; i<sc->numcsns; i++ ) {
3296 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
3297 f->f_av_value = sc->ctxcsn[i];
3299 of = op->ors_filter;
3300 op->ors_filter = mmf;
3301 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
3304 op->ors_filterstr = si->si_filterstr;
3306 op->o_nocaching = 1;
3309 rc = be->be_search( op, &rs_search );
3310 if ( SLAP_MULTIMASTER( op->o_bd )) {
3311 op->ors_filter = of;
3313 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
3314 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
3315 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
3320 op->o_nocaching = 0;
3322 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
3324 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
3325 csn = sc->ctxcsn[m];
3327 csn = si->si_syncCookie.ctxcsn[0];
3330 op->o_bd = si->si_wbe;
3331 slap_queue_csn( op, &csn );
3333 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
3334 while ( np_list != NULL ) {
3335 SlapReply rs_delete = {REP_RESULT};
3337 LDAP_LIST_REMOVE( np_list, npe_link );
3339 np_list = LDAP_LIST_NEXT( np_list, npe_link );
3340 op->o_tag = LDAP_REQ_DELETE;
3341 op->o_callback = &cb;
3342 cb.sc_response = null_callback;
3344 op->o_req_dn = *np_prev->npe_name;
3345 op->o_req_ndn = *np_prev->npe_nname;
3346 rc = op->o_bd->be_delete( op, &rs_delete );
3347 Debug( LDAP_DEBUG_SYNC,
3348 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
3349 si->si_ridtxt, op->o_req_dn.bv_val, rc );
3351 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
3352 SlapReply rs_modify = {REP_RESULT};
3353 Modifications mod1, mod2;
3354 mod1.sml_op = LDAP_MOD_REPLACE;
3356 mod1.sml_desc = slap_schema.si_ad_objectClass;
3357 mod1.sml_type = mod1.sml_desc->ad_cname;
3358 mod1.sml_numvals = 2;
3359 mod1.sml_values = &gcbva[0];
3360 mod1.sml_nvalues = NULL;
3361 mod1.sml_next = &mod2;
3363 mod2.sml_op = LDAP_MOD_REPLACE;
3365 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
3366 mod2.sml_type = mod2.sml_desc->ad_cname;
3367 mod2.sml_numvals = 1;
3368 mod2.sml_values = &gcbva[1];
3369 mod2.sml_nvalues = NULL;
3370 mod2.sml_next = NULL;
3372 op->o_tag = LDAP_REQ_MODIFY;
3373 op->orm_modlist = &mod1;
3375 rc = op->o_bd->be_modify( op, &rs_modify );
3376 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
3379 while ( rs_delete.sr_err == LDAP_SUCCESS &&
3380 op->o_delete_glue_parent ) {
3381 op->o_delete_glue_parent = 0;
3382 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
3383 slap_callback cb = { NULL };
3384 cb.sc_response = slap_null_cb;
3385 dnParent( &op->o_req_ndn, &pdn );
3387 op->o_req_ndn = pdn;
3388 op->o_callback = &cb;
3389 rs_reinit( &rs_delete, REP_RESULT );
3390 /* give it a root privil ? */
3391 op->o_bd->be_delete( op, &rs_delete );
3397 op->o_delete_glue_parent = 0;
3399 ber_bvfree( np_prev->npe_name );
3400 ber_bvfree( np_prev->npe_nname );
3403 if ( slapd_shutdown ) {
3408 slap_graduate_commit_csn( op );
3411 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3412 BER_BVZERO( &op->o_csn );
3419 syncrepl_add_glue_ancestors(
3423 Backend *be = op->o_bd;
3424 slap_callback cb = { NULL };
3426 int rc = LDAP_SUCCESS;
3429 struct berval dn = BER_BVNULL;
3430 struct berval ndn = BER_BVNULL;
3432 struct berval ptr, nptr;
3435 op->o_tag = LDAP_REQ_ADD;
3436 op->o_callback = &cb;
3437 cb.sc_response = null_callback;
3438 cb.sc_private = NULL;
3443 /* count RDNs in suffix */
3444 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
3445 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
3447 ptr.bv_len -= comma - ptr.bv_val;
3457 /* Start with BE suffix */
3459 for ( i = 0; i < suffrdns; i++ ) {
3460 comma = ber_bvrchr( &ptr, ',' );
3461 if ( comma != NULL ) {
3462 ptr.bv_len = comma - ptr.bv_val;
3469 if ( !BER_BVISEMPTY( &ptr ) ) {
3470 dn.bv_len -= ptr.bv_len + 1;
3471 dn.bv_val += ptr.bv_len + 1;
3474 /* the normalizedDNs are always the same length, no counting
3478 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
3479 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
3480 ndn.bv_len = be->be_nsuffix[0].bv_len;
3482 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
3488 while ( ndn.bv_val > e->e_nname.bv_val ) {
3489 SlapReply rs_add = {REP_RESULT};
3491 glue = entry_alloc();
3492 ber_dupbv( &glue->e_name, &dn );
3493 ber_dupbv( &glue->e_nname, &ndn );
3495 a = attr_alloc( slap_schema.si_ad_objectClass );
3498 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
3499 ber_dupbv( &a->a_vals[0], &gcbva[0] );
3500 ber_dupbv( &a->a_vals[1], &gcbva[1] );
3501 ber_dupbv( &a->a_vals[2], &gcbva[2] );
3503 a->a_nvals = a->a_vals;
3505 a->a_next = glue->e_attrs;
3508 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
3511 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
3512 ber_dupbv( &a->a_vals[0], &gcbva[1] );
3513 ber_dupbv( &a->a_vals[1], &gcbva[2] );
3515 a->a_nvals = a->a_vals;
3517 a->a_next = glue->e_attrs;
3520 op->o_req_dn = glue->e_name;
3521 op->o_req_ndn = glue->e_nname;
3523 rc = be->be_add ( op, &rs_add );
3524 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3525 if ( op->ora_e == glue )
3526 be_entry_release_w( op, glue );
3528 /* incl. ALREADY EXIST */
3530 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
3536 /* Move to next child */
3537 comma = ber_bvrchr( &ptr, ',' );
3538 if ( comma == NULL ) {
3541 ptr.bv_len = comma - ptr.bv_val;
3543 dn.bv_val = ++comma;
3544 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
3546 comma = ber_bvrchr( &nptr, ',' );
3547 assert( comma != NULL );
3548 nptr.bv_len = comma - nptr.bv_val;
3550 ndn.bv_val = ++comma;
3551 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
3562 slap_callback cb = { NULL };
3564 Backend *be = op->o_bd;
3565 SlapReply rs_add = {REP_RESULT};
3567 rc = syncrepl_add_glue_ancestors( op, e );
3570 case LDAP_ALREADY_EXISTS:
3577 op->o_tag = LDAP_REQ_ADD;
3578 op->o_callback = &cb;
3579 cb.sc_response = null_callback;
3580 cb.sc_private = NULL;
3582 op->o_req_dn = e->e_name;
3583 op->o_req_ndn = e->e_nname;
3585 rc = be->be_add ( op, &rs_add );
3586 if ( rs_add.sr_err == LDAP_SUCCESS ) {
3587 if ( op->ora_e == e )
3588 be_entry_release_w( op, e );
3597 syncrepl_updateCookie(
3600 struct sync_cookie *syncCookie )
3602 Backend *be = op->o_bd;
3604 struct berval first = BER_BVNULL;
3606 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
3609 int rc, i, j, changed = 0;
3612 slap_callback cb = { NULL };
3613 SlapReply rs_modify = {REP_RESULT};
3615 mod.sml_op = LDAP_MOD_REPLACE;
3616 mod.sml_desc = slap_schema.si_ad_contextCSN;
3617 mod.sml_type = mod.sml_desc->ad_cname;
3618 mod.sml_flags = SLAP_MOD_INTERNAL;
3619 mod.sml_nvalues = NULL;
3620 mod.sml_next = NULL;
3622 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
3625 for ( i=0; i<syncCookie->numcsns; i++ ) {
3626 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
3628 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3629 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3633 /* clone the cookieState CSNs so we can Replace the whole thing */
3634 mod.sml_numvals = si->si_cookieState->cs_num;
3635 mod.sml_values = op->o_tmpalloc(( mod.sml_numvals+1 )*sizeof(struct berval), op->o_tmpmemctx );
3636 for ( i=0; i<mod.sml_numvals; i++ )
3637 mod.sml_values[i] = si->si_cookieState->cs_vals[i];
3638 BER_BVZERO( &mod.sml_values[i] );
3640 /* find any CSNs in the syncCookie that are newer than the cookieState */
3641 for ( i=0; i<syncCookie->numcsns; i++ ) {
3642 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
3643 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
3645 len = syncCookie->ctxcsn[i].bv_len;
3646 if ( len > si->si_cookieState->cs_vals[j].bv_len )
3647 len = si->si_cookieState->cs_vals[j].bv_len;
3648 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3649 si->si_cookieState->cs_vals[j].bv_val, len ) > 0 ) {
3650 mod.sml_values[j] = syncCookie->ctxcsn[i];
3652 if ( BER_BVISNULL( &first ) ) {
3653 first = syncCookie->ctxcsn[i];
3655 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3657 first = syncCookie->ctxcsn[i];
3662 /* there was no match for this SID, it's a new CSN */
3663 if ( j == si->si_cookieState->cs_num ) {
3664 mod.sml_values = op->o_tmprealloc( mod.sml_values,
3665 ( mod.sml_numvals+2 )*sizeof(struct berval), op->o_tmpmemctx );
3666 mod.sml_values[mod.sml_numvals++] = syncCookie->ctxcsn[i];
3667 BER_BVZERO( &mod.sml_values[mod.sml_numvals] );
3668 if ( BER_BVISNULL( &first ) ) {
3669 first = syncCookie->ctxcsn[i];
3670 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3672 first = syncCookie->ctxcsn[i];
3677 /* Should never happen, ITS#5065 */
3678 if ( BER_BVISNULL( &first ) || !changed ) {
3679 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3680 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3683 op->o_bd = si->si_wbe;
3684 slap_queue_csn( op, &first );
3686 op->o_tag = LDAP_REQ_MODIFY;
3688 cb.sc_response = null_callback;
3691 op->o_callback = &cb;
3692 op->o_req_dn = si->si_contextdn;
3693 op->o_req_ndn = si->si_contextdn;
3695 /* update contextCSN */
3696 op->o_dont_replicate = 1;
3698 op->orm_modlist = &mod;
3699 op->orm_no_opattrs = 1;
3700 rc = op->o_bd->be_modify( op, &rs_modify );
3702 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3703 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3705 char txtbuf[SLAP_TEXT_BUFLEN];
3706 size_t textlen = sizeof txtbuf;
3707 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3708 rs_reinit( &rs_modify, REP_RESULT );
3709 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3711 rc = op->o_bd->be_add( op, &rs_modify );
3712 if ( e == op->ora_e )
3713 be_entry_release_w( op, op->ora_e );
3716 op->orm_no_opattrs = 0;
3717 op->o_dont_replicate = 0;
3719 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3720 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3721 /* If we replaced any old values */
3722 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3723 if ( mod.sml_values[i].bv_val != si->si_cookieState->cs_vals[i].bv_val )
3724 ber_bvreplace( &si->si_cookieState->cs_vals[i],
3725 &mod.sml_values[i] );
3727 /* Handle any added values */
3728 if ( i < mod.sml_numvals ) {
3729 si->si_cookieState->cs_num = mod.sml_numvals;
3730 value_add( &si->si_cookieState->cs_vals, &mod.sml_values[i] );
3731 free( si->si_cookieState->cs_sids );
3732 si->si_cookieState->cs_sids = slap_parse_csn_sids(
3733 si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
3736 /* Don't just dup the provider's cookie, recreate it */
3737 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
3738 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn, si->si_cookieState->cs_vals, NULL );
3739 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num * sizeof(int) );
3740 for ( i=0; i<si->si_cookieState->cs_num; i++ )
3741 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
3743 si->si_cookieState->cs_age++;
3744 si->si_cookieAge = si->si_cookieState->cs_age;
3746 Debug( LDAP_DEBUG_ANY,
3747 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3748 si->si_ridtxt, rs_modify.sr_err, 0 );
3750 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3753 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3754 BER_BVZERO( &op->o_csn );
3755 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3756 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3759 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3760 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3767 /* Compare the attribute from the old entry to the one in the new
3768 * entry. The Modifications from the new entry will either be left
3769 * in place, or changed to an Add or Delete as needed.
3772 attr_cmp( Operation *op, Attribute *old, Attribute *new,
3773 Modifications ***mret, Modifications ***mcur )
3776 Modifications *mod, **modtail;
3782 struct berval **adds, **dels;
3783 /* count old and new */
3784 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
3785 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
3787 /* there MUST be both old and new values */
3792 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
3793 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
3795 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
3796 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
3800 for ( i=0; i<o; i++ ) {
3801 for ( j=0; j<n; j++ ) {
3804 if ( bvmatch( dels[i], adds[j] ) ) {
3814 /* Don't delete/add an objectClass, always use the replace op.
3815 * Modify would fail if provider has replaced entry with a new,
3816 * and the new explicitly includes a superior of a class that was
3817 * only included implicitly in the old entry. Ref ITS#5517.
3819 * Also use replace op if attr has no equality matching rule.
3822 if ( ( nn || ( no > 0 && no < o ) ) &&
3823 ( old->a_desc == slap_schema.si_ad_objectClass ||
3824 !old->a_desc->ad_type->sat_equality ) )
3830 /* all old values were deleted, just use the replace op */
3834 /* delete some values */
3835 mod = ch_malloc( sizeof( Modifications ) );
3836 mod->sml_op = LDAP_MOD_DELETE;
3838 mod->sml_desc = old->a_desc;
3839 mod->sml_type = mod->sml_desc->ad_cname;
3840 mod->sml_numvals = no;
3841 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3842 if ( old->a_vals != old->a_nvals ) {
3843 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3845 mod->sml_nvalues = NULL;
3848 for ( i = 0; i < o; i++ ) {
3849 if ( !dels[i] ) continue;
3850 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
3851 if ( mod->sml_nvalues ) {
3852 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
3856 BER_BVZERO( &mod->sml_values[j] );
3857 if ( mod->sml_nvalues ) {
3858 BER_BVZERO( &mod->sml_nvalues[j] );
3861 modtail = &mod->sml_next;
3864 op->o_tmpfree( dels, op->o_tmpmemctx );
3865 /* some values were added */
3866 if ( nn && no < o ) {
3867 mod = ch_malloc( sizeof( Modifications ) );
3868 mod->sml_op = LDAP_MOD_ADD;
3870 mod->sml_desc = old->a_desc;
3871 mod->sml_type = mod->sml_desc->ad_cname;
3872 mod->sml_numvals = nn;
3873 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3874 if ( old->a_vals != old->a_nvals ) {
3875 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3877 mod->sml_nvalues = NULL;
3880 for ( i = 0; i < n; i++ ) {
3881 if ( !adds[i] ) continue;
3882 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
3883 if ( mod->sml_nvalues ) {
3884 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
3888 BER_BVZERO( &mod->sml_values[j] );
3889 if ( mod->sml_nvalues ) {
3890 BER_BVZERO( &mod->sml_nvalues[j] );
3893 modtail = &mod->sml_next;
3896 op->o_tmpfree( adds, op->o_tmpmemctx );
3898 /* new attr, just use the new mod */
3902 /* advance to next element */
3906 **mcur = mod->sml_next;
3908 modtail = &mod->sml_next;
3910 *mcur = &mod->sml_next;
3916 /* Generate a set of modifications to change the old entry into the
3917 * new one. On input ml is a list of modifications equivalent to
3918 * the new entry. It will be massaged and the result will be stored
3921 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
3922 Modifications **mods, Modifications **ml, int is_ctx)
3924 Modifications **modtail = mods;
3926 /* We assume that attributes are saved in the same order
3927 * in the remote and local databases. So if we walk through
3928 * the attributeDescriptions one by one they should match in
3929 * lock step. If not, look for an add or delete.
3931 while ( old && new )
3933 /* If we've seen this before, use its mod now */
3934 if ( new->a_flags & SLAP_ATTR_IXADD ) {
3935 attr_cmp( op, NULL, new, &modtail, &ml );
3939 /* Skip contextCSN */
3940 if ( is_ctx && old->a_desc ==
3941 slap_schema.si_ad_contextCSN ) {
3946 if ( old->a_desc != new->a_desc ) {
3950 /* If it's just been re-added later,
3951 * remember that we've seen it.
3953 tmp = attr_find( new, old->a_desc );
3955 tmp->a_flags |= SLAP_ATTR_IXADD;
3957 /* If it's a new attribute, pull it in.
3959 tmp = attr_find( old, new->a_desc );
3961 attr_cmp( op, NULL, new, &modtail, &ml );
3965 /* Delete old attr */
3966 mod = ch_malloc( sizeof( Modifications ) );
3967 mod->sml_op = LDAP_MOD_DELETE;
3969 mod->sml_desc = old->a_desc;
3970 mod->sml_type = mod->sml_desc->ad_cname;
3971 mod->sml_numvals = 0;
3972 mod->sml_values = NULL;
3973 mod->sml_nvalues = NULL;
3975 modtail = &mod->sml_next;
3980 /* kludge - always update modifiersName so that it
3981 * stays co-located with the other mod opattrs. But only
3982 * if we know there are other valid mods.
3984 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
3985 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
3986 attr_cmp( op, NULL, new, &modtail, &ml );
3988 attr_cmp( op, old, new, &modtail, &ml );
4001 dninfo *dni = op->o_callback->sc_private;
4003 if ( rs->sr_type == REP_SEARCH ) {
4004 if ( !BER_BVISNULL( &dni->dn ) ) {
4005 Debug( LDAP_DEBUG_ANY,
4006 "dn_callback : consistency error - "
4007 "entryUUID is not unique\n", 0, 0, 0 );
4009 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
4010 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
4011 /* If there is a new entry, see if it differs from the old.
4012 * We compare the non-normalized values so that cosmetic changes
4013 * in the provider are always propagated.
4015 if ( dni->new_entry ) {
4016 Attribute *old, *new;
4017 struct berval old_rdn, new_rdn;
4018 struct berval old_p, new_p;
4019 int is_ctx, new_sup = 0;
4021 /* If old entry is not a glue entry, make sure new entry
4022 * is actually newer than old entry
4024 if ( !is_entry_glue( rs->sr_entry )) {
4025 old = attr_find( rs->sr_entry->e_attrs,
4026 slap_schema.si_ad_entryCSN );
4027 new = attr_find( dni->new_entry->e_attrs,
4028 slap_schema.si_ad_entryCSN );
4031 ber_len_t len = old->a_vals[0].bv_len;
4032 if ( len > new->a_vals[0].bv_len )
4033 len = new->a_vals[0].bv_len;
4034 rc = memcmp( old->a_vals[0].bv_val,
4035 new->a_vals[0].bv_val, len );
4037 Debug( LDAP_DEBUG_SYNC,
4038 "dn_callback : new entry is older than ours "
4039 "%s ours %s, new %s\n",
4040 rs->sr_entry->e_name.bv_val,
4041 old->a_vals[0].bv_val,
4042 new->a_vals[0].bv_val );
4043 return LDAP_SUCCESS;
4044 } else if ( rc == 0 ) {
4045 Debug( LDAP_DEBUG_SYNC,
4046 "dn_callback : entries have identical CSN "
4048 rs->sr_entry->e_name.bv_val,
4049 old->a_vals[0].bv_val, 0 );
4050 return LDAP_SUCCESS;
4055 is_ctx = dn_match( &rs->sr_entry->e_nname,
4056 &op->o_bd->be_nsuffix[0] );
4058 /* Did the DN change?
4059 * case changes in the parent are ignored,
4060 * we only want to know if the RDN was
4063 dnRdn( &rs->sr_entry->e_name, &old_rdn );
4064 dnRdn( &dni->new_entry->e_name, &new_rdn );
4065 dnParent( &rs->sr_entry->e_nname, &old_p );
4066 dnParent( &dni->new_entry->e_nname, &new_p );
4068 new_sup = !dn_match( &old_p, &new_p );
4069 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
4071 struct berval oldRDN, oldVal;
4072 AttributeDescription *ad = NULL;
4078 dni->nnewSup = new_p;
4080 /* See if the oldRDN was deleted */
4081 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
4082 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
4083 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
4085 oldRDN.bv_len -= oldVal.bv_len + 1;
4086 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4088 for ( oldpos=0, a=rs->sr_entry->e_attrs;
4089 a && a->a_desc != ad; oldpos++, a=a->a_next );
4091 for ( newpos=0, a=dni->new_entry->e_attrs;
4092 a && a->a_desc != ad; newpos++, a=a->a_next );
4093 if ( !a || oldpos != newpos || attr_valfind( a,
4094 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
4095 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
4096 SLAP_MR_VALUE_OF_SYNTAX,
4097 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
4101 /* Get the newRDN's desc */
4102 dnRdn( &dni->new_entry->e_nname, &oldRDN );
4103 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
4104 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
4106 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
4109 /* A ModDN has happened, but in Refresh mode other
4110 * changes may have occurred before we picked it up.
4111 * So fallthru to regular Modify processing.
4115 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
4116 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
4120 } else if ( rs->sr_type == REP_RESULT ) {
4121 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
4122 Debug( LDAP_DEBUG_ANY,
4123 "dn_callback : consistency error - "
4124 "entryUUID is not unique\n", 0, 0, 0 );
4128 return LDAP_SUCCESS;
4132 nonpresent_callback(
4136 syncinfo_t *si = op->o_callback->sc_private;
4139 struct berval* present_uuid = NULL;
4140 struct nonpresent_entry *np_entry;
4142 if ( rs->sr_type == REP_RESULT ) {
4143 count = avl_free( si->si_presentlist, ch_free );
4144 si->si_presentlist = NULL;
4146 } else if ( rs->sr_type == REP_SEARCH ) {
4147 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
4148 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
4151 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
4155 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
4156 char buf[sizeof("rid=999 non")];
4158 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
4159 present_uuid ? "" : "non" );
4161 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
4162 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
4165 if ( a == NULL ) return 0;
4168 if ( present_uuid == NULL ) {
4169 np_entry = (struct nonpresent_entry *)
4170 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
4171 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
4172 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
4173 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
4176 avl_delete( &si->si_presentlist,
4177 &a->a_nvals[0], syncuuid_cmp );
4178 ch_free( present_uuid );
4181 return LDAP_SUCCESS;
4189 if ( rs->sr_err != LDAP_SUCCESS &&
4190 rs->sr_err != LDAP_REFERRAL &&
4191 rs->sr_err != LDAP_ALREADY_EXISTS &&
4192 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
4193 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
4195 Debug( LDAP_DEBUG_ANY,
4196 "null_callback : error code 0x%x\n",
4199 return LDAP_SUCCESS;
4202 static struct berval *
4203 slap_uuidstr_from_normalized(
4204 struct berval* uuidstr,
4205 struct berval* normalized,
4210 unsigned char nibble;
4213 if ( normalized == NULL ) return NULL;
4214 if ( normalized->bv_len != 16 ) return NULL;
4219 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4220 if ( new == NULL ) {
4227 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4228 if ( new != uuidstr ) {
4229 slap_sl_free( new, ctx );
4234 for ( i = 0; i < 16; i++ ) {
4235 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
4236 new->bv_val[(i<<1)+d] = '-';
4240 nibble = (normalized->bv_val[i] >> 4) & 0xF;
4241 if ( nibble < 10 ) {
4242 new->bv_val[(i<<1)+d] = nibble + '0';
4244 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
4247 nibble = (normalized->bv_val[i]) & 0xF;
4248 if ( nibble < 10 ) {
4249 new->bv_val[(i<<1)+d+1] = nibble + '0';
4251 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
4255 new->bv_val[new->bv_len] = '\0';
4262 if ( normalized == NULL ) return NULL;
4263 if ( normalized->bv_len != 16 ) return NULL;
4269 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
4270 if ( new == NULL ) {
4277 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
4282 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
4283 normalized->bv_len, new->bv_val, new->bv_len + 1 );
4287 if ( new != NULL ) {
4288 if ( new->bv_val != NULL ) {
4289 slap_sl_free( new->bv_val, ctx );
4292 if ( new != uuidstr ) {
4293 slap_sl_free( new, ctx );
4306 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
4308 const struct berval *uuid1 = v_uuid1;
4309 const struct berval *uuid2 = v_uuid2;
4310 int rc = uuid1->bv_len - uuid2->bv_len;
4311 if ( rc ) return rc;
4312 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
4316 syncinfo_free( syncinfo_t *sie, int free_all )
4318 syncinfo_t *si_next;
4320 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
4321 sie->si_ridtxt, 0, 0 );
4324 si_next = sie->si_next;
4327 if ( sie->si_conn ) {
4328 connection_client_stop( sie->si_conn );
4329 sie->si_conn = NULL;
4331 ldap_unbind_ext( sie->si_ld, NULL, NULL );
4335 struct re_s *re = sie->si_re;
4338 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4339 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
4340 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
4341 ldap_pvt_runqueue_remove( &slapd_rq, re );
4342 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4345 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
4347 bindconf_free( &sie->si_bindconf );
4349 if ( sie->si_filterstr.bv_val ) {
4350 ch_free( sie->si_filterstr.bv_val );
4352 if ( sie->si_filter ) {
4353 filter_free( sie->si_filter );
4355 if ( sie->si_logfilterstr.bv_val ) {
4356 ch_free( sie->si_logfilterstr.bv_val );
4358 if ( sie->si_base.bv_val ) {
4359 ch_free( sie->si_base.bv_val );
4361 if ( sie->si_logbase.bv_val ) {
4362 ch_free( sie->si_logbase.bv_val );
4364 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
4365 ch_free( sie->si_contextdn.bv_val );
4367 if ( sie->si_attrs ) {
4369 while ( sie->si_attrs[i] != NULL ) {
4370 ch_free( sie->si_attrs[i] );
4373 ch_free( sie->si_attrs );
4375 if ( sie->si_exattrs ) {
4377 while ( sie->si_exattrs[i] != NULL ) {
4378 ch_free( sie->si_exattrs[i] );
4381 ch_free( sie->si_exattrs );
4383 if ( sie->si_anlist ) {
4385 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
4386 ch_free( sie->si_anlist[i].an_name.bv_val );
4389 ch_free( sie->si_anlist );
4391 if ( sie->si_exanlist ) {
4393 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
4394 ch_free( sie->si_exanlist[i].an_name.bv_val );
4397 ch_free( sie->si_exanlist );
4399 if ( sie->si_retryinterval ) {
4400 ch_free( sie->si_retryinterval );
4402 if ( sie->si_retrynum ) {
4403 ch_free( sie->si_retrynum );
4405 if ( sie->si_retrynum_init ) {
4406 ch_free( sie->si_retrynum_init );
4408 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
4409 if ( sie->si_presentlist ) {
4410 avl_free( sie->si_presentlist, ch_free );
4412 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
4413 struct nonpresent_entry* npe;
4414 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
4415 LDAP_LIST_REMOVE( npe, npe_link );
4416 if ( npe->npe_name ) {
4417 if ( npe->npe_name->bv_val ) {
4418 ch_free( npe->npe_name->bv_val );
4420 ch_free( npe->npe_name );
4422 if ( npe->npe_nname ) {
4423 if ( npe->npe_nname->bv_val ) {
4424 ch_free( npe->npe_nname->bv_val );
4426 ch_free( npe->npe_nname );
4430 if ( sie->si_cookieState ) {
4431 sie->si_cookieState->cs_ref--;
4432 if ( !sie->si_cookieState->cs_ref ) {
4433 ch_free( sie->si_cookieState->cs_sids );
4434 ber_bvarray_free( sie->si_cookieState->cs_vals );
4435 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
4436 ch_free( sie->si_cookieState->cs_psids );
4437 ber_bvarray_free( sie->si_cookieState->cs_pvals );
4438 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_pmutex );
4439 ch_free( sie->si_cookieState );
4442 #ifdef ENABLE_REWRITE
4443 if ( sie->si_rewrite )
4444 rewrite_info_delete( &sie->si_rewrite );
4445 if ( sie->si_suffixm.bv_val )
4446 ch_free( sie->si_suffixm.bv_val );
4450 } while ( free_all && si_next );
4453 #ifdef ENABLE_REWRITE
4455 config_suffixm( ConfigArgs *c, syncinfo_t *si )
4457 char *argvEngine[] = { "rewriteEngine", "on", NULL };
4458 char *argvContext[] = { "rewriteContext", SUFFIXM_CTX, NULL };
4459 char *argvRule[] = { "rewriteRule", NULL, NULL, ":", NULL };
4463 if ( si->si_rewrite )
4464 rewrite_info_delete( &si->si_rewrite );
4465 si->si_rewrite = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
4467 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvEngine );
4468 if ( rc != LDAP_SUCCESS )
4471 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 2, argvContext );
4472 if ( rc != LDAP_SUCCESS )
4475 vnc = ch_malloc( si->si_base.bv_len + 6 );
4476 strcpy( vnc, "(.*)" );
4477 lutil_strcopy( lutil_strcopy( vnc+4, si->si_base.bv_val ), "$" );
4480 rnc = ch_malloc( si->si_suffixm.bv_len + 3 );
4481 strcpy( rnc, "%1" );
4482 strcpy( rnc+2, si->si_suffixm.bv_val );
4485 rc = rewrite_parse( si->si_rewrite, c->fname, c->lineno, 4, argvRule );
4492 /* NOTE: used & documented in slapd.conf(5) */
4494 #define PROVIDERSTR "provider"
4495 #define SCHEMASTR "schemachecking"
4496 #define FILTERSTR "filter"
4497 #define SEARCHBASESTR "searchbase"
4498 #define SCOPESTR "scope"
4499 #define ATTRSONLYSTR "attrsonly"
4500 #define ATTRSSTR "attrs"
4501 #define TYPESTR "type"
4502 #define INTERVALSTR "interval"
4503 #define RETRYSTR "retry"
4504 #define SLIMITSTR "sizelimit"
4505 #define TLIMITSTR "timelimit"
4506 #define SYNCDATASTR "syncdata"
4507 #define LOGBASESTR "logbase"
4508 #define LOGFILTERSTR "logfilter"
4509 #define SUFFIXMSTR "suffixmassage"
4510 #define STRICT_REFRESH "strictrefresh"
4512 /* FIXME: undocumented */
4513 #define EXATTRSSTR "exattrs"
4514 #define MANAGEDSAITSTR "manageDSAit"
4518 GOT_RID = 0x00000001U,
4519 GOT_PROVIDER = 0x00000002U,
4520 GOT_SCHEMACHECKING = 0x00000004U,
4521 GOT_FILTER = 0x00000008U,
4522 GOT_SEARCHBASE = 0x00000010U,
4523 GOT_SCOPE = 0x00000020U,
4524 GOT_ATTRSONLY = 0x00000040U,
4525 GOT_ATTRS = 0x00000080U,
4526 GOT_TYPE = 0x00000100U,
4527 GOT_INTERVAL = 0x00000200U,
4528 GOT_RETRY = 0x00000400U,
4529 GOT_SLIMIT = 0x00000800U,
4530 GOT_TLIMIT = 0x00001000U,
4531 GOT_SYNCDATA = 0x00002000U,
4532 GOT_LOGBASE = 0x00004000U,
4533 GOT_LOGFILTER = 0x00008000U,
4534 GOT_EXATTRS = 0x00010000U,
4535 GOT_MANAGEDSAIT = 0x00020000U,
4536 GOT_BINDCONF = 0x00040000U,
4537 GOT_SUFFIXM = 0x00080000U,
4540 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
4543 static slap_verbmasks datamodes[] = {
4544 { BER_BVC("default"), SYNCDATA_DEFAULT },
4545 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
4546 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
4551 parse_syncrepl_retry(
4558 int use_default = 0;
4560 char *val = arg + STRLENOF( RETRYSTR "=" );
4561 if ( strcasecmp( val, "undefined" ) == 0 ) {
4566 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
4567 retry_list[0] = NULL;
4569 slap_str2clist( &retry_list, val, " ,\t" );
4571 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
4574 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4575 "Error: incomplete syncrepl retry list" );
4576 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4577 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4578 ch_free( retry_list[k] );
4580 ch_free( retry_list );
4583 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
4584 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
4585 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
4586 for ( j = 0; j < n; j++ ) {
4588 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
4589 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4590 "Error: invalid retry interval \"%s\" (#%d)",
4591 retry_list[j*2], j );
4592 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4593 /* do some cleanup */
4596 si->si_retryinterval[j] = (time_t)t;
4597 if ( *retry_list[j*2+1] == '+' ) {
4598 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
4599 si->si_retrynum[j] = RETRYNUM_FOREVER;
4603 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
4604 || si->si_retrynum_init[j] <= 0 )
4606 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4607 "Error: invalid initial retry number \"%s\" (#%d)",
4608 retry_list[j*2+1], j );
4609 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4610 /* do some cleanup */
4613 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
4614 || si->si_retrynum[j] <= 0 )
4616 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4617 "Error: invalid retry number \"%s\" (#%d)",
4618 retry_list[j*2+1], j );
4619 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4620 /* do some cleanup */
4625 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
4626 Debug( LDAP_DEBUG_CONFIG,
4627 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
4631 si->si_retrynum_init[j] = RETRYNUM_TAIL;
4632 si->si_retrynum[j] = RETRYNUM_TAIL;
4633 si->si_retryinterval[j] = 0;
4635 for ( k = 0; retry_list && retry_list[k]; k++ ) {
4636 ch_free( retry_list[k] );
4638 ch_free( retry_list );
4639 if ( !use_default ) {
4640 si->si_got |= GOT_RETRY;
4647 parse_syncrepl_line(
4654 for ( i = 1; i < c->argc; i++ ) {
4655 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
4656 STRLENOF( IDSTR "=" ) ) )
4659 /* '\0' string terminator accounts for '=' */
4660 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
4661 if ( lutil_atoi( &tmp, val ) != 0 ) {
4662 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4663 "Error: parse_syncrepl_line: "
4664 "unable to parse syncrepl id \"%s\"", val );
4665 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4668 if ( tmp > SLAP_SYNC_RID_MAX || tmp < 0 ) {
4669 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4670 "Error: parse_syncrepl_line: "
4671 "syncrepl id %d is out of range [0..%d]", tmp, SLAP_SYNC_RID_MAX );
4672 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4676 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
4677 si->si_got |= GOT_RID;
4678 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
4679 STRLENOF( PROVIDERSTR "=" ) ) )
4681 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
4682 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
4684 if ( ldap_is_ldaps_url( val ))
4685 si->si_bindconf.sb_tls_do_init = 1;
4687 si->si_got |= GOT_PROVIDER;
4688 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
4689 STRLENOF( SCHEMASTR "=" ) ) )
4691 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
4692 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
4693 si->si_schemachecking = 1;
4694 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
4695 si->si_schemachecking = 0;
4697 si->si_schemachecking = 1;
4699 si->si_got |= GOT_SCHEMACHECKING;
4700 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4701 STRLENOF( FILTERSTR "=" ) ) )
4703 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4704 if ( si->si_filterstr.bv_val )
4705 ch_free( si->si_filterstr.bv_val );
4706 ber_str2bv( val, 0, 1, &si->si_filterstr );
4707 si->si_got |= GOT_FILTER;
4708 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4709 STRLENOF( LOGFILTERSTR "=" ) ) )
4711 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4712 if ( si->si_logfilterstr.bv_val )
4713 ch_free( si->si_logfilterstr.bv_val );
4714 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4715 si->si_got |= GOT_LOGFILTER;
4716 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4717 STRLENOF( SEARCHBASESTR "=" ) ) )
4722 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4723 if ( si->si_base.bv_val ) {
4724 ch_free( si->si_base.bv_val );
4726 ber_str2bv( val, 0, 0, &bv );
4727 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4728 if ( rc != LDAP_SUCCESS ) {
4729 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4730 "Invalid base DN \"%s\": %d (%s)",
4731 val, rc, ldap_err2string( rc ) );
4732 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4735 si->si_got |= GOT_SEARCHBASE;
4736 #ifdef ENABLE_REWRITE
4737 } else if ( !strncasecmp( c->argv[ i ], SUFFIXMSTR "=",
4738 STRLENOF( SUFFIXMSTR "=" ) ) )
4743 val = c->argv[ i ] + STRLENOF( SUFFIXMSTR "=" );
4744 if ( si->si_suffixm.bv_val ) {
4745 ch_free( si->si_suffixm.bv_val );
4747 ber_str2bv( val, 0, 0, &bv );
4748 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_suffixm, NULL );
4749 if ( rc != LDAP_SUCCESS ) {
4750 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4751 "Invalid massage DN \"%s\": %d (%s)",
4752 val, rc, ldap_err2string( rc ) );
4753 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4756 if ( !be_issubordinate( c->be, &si->si_suffixm )) {
4757 ch_free( si->si_suffixm.bv_val );
4758 BER_BVZERO( &si->si_suffixm );
4759 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4760 "Massage DN \"%s\" is not within the database naming context",
4762 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4765 si->si_got |= GOT_SUFFIXM;
4767 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4768 STRLENOF( LOGBASESTR "=" ) ) )
4773 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
4774 if ( si->si_logbase.bv_val ) {
4775 ch_free( si->si_logbase.bv_val );
4777 ber_str2bv( val, 0, 0, &bv );
4778 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
4779 if ( rc != LDAP_SUCCESS ) {
4780 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4781 "Invalid logbase DN \"%s\": %d (%s)",
4782 val, rc, ldap_err2string( rc ) );
4783 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4786 si->si_got |= GOT_LOGBASE;
4787 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
4788 STRLENOF( SCOPESTR "=" ) ) )
4791 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
4792 j = ldap_pvt_str2scope( val );
4794 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4795 "Error: parse_syncrepl_line: "
4796 "unknown scope \"%s\"", val);
4797 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4801 si->si_got |= GOT_SCOPE;
4802 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
4803 STRLENOF( ATTRSONLYSTR ) ) )
4805 si->si_attrsonly = 1;
4806 si->si_got |= GOT_ATTRSONLY;
4807 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
4808 STRLENOF( ATTRSSTR "=" ) ) )
4810 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
4811 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4813 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4814 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
4815 if ( si->si_anlist == NULL ) {
4816 ch_free( attr_fname );
4819 si->si_anfile = attr_fname;
4821 char *str, *s, *next;
4822 const char *delimstr = " ,\t";
4823 str = ch_strdup( val );
4824 for ( s = ldap_pvt_strtok( str, delimstr, &next );
4826 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
4828 if ( strlen(s) == 1 && *s == '*' ) {
4829 si->si_allattrs = 1;
4830 val[ s - str ] = delimstr[0];
4832 if ( strlen(s) == 1 && *s == '+' ) {
4833 si->si_allopattrs = 1;
4834 val [ s - str ] = delimstr[0];
4838 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
4839 if ( si->si_anlist == NULL ) {
4843 si->si_got |= GOT_ATTRS;
4844 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
4845 STRLENOF( EXATTRSSTR "=" ) ) )
4847 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
4848 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4850 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4851 si->si_exanlist = file2anlist(
4852 si->si_exanlist, attr_fname, " ,\t" );
4853 if ( si->si_exanlist == NULL ) {
4854 ch_free( attr_fname );
4857 ch_free( attr_fname );
4859 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
4860 if ( si->si_exanlist == NULL ) {
4864 si->si_got |= GOT_EXATTRS;
4865 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
4866 STRLENOF( TYPESTR "=" ) ) )
4868 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
4869 if ( !strncasecmp( val, "refreshOnly",
4870 STRLENOF("refreshOnly") ) )
4872 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4873 } else if ( !strncasecmp( val, "refreshAndPersist",
4874 STRLENOF("refreshAndPersist") ) )
4876 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
4877 si->si_interval = 60;
4879 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4880 "Error: parse_syncrepl_line: "
4881 "unknown sync type \"%s\"", val);
4882 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4885 si->si_got |= GOT_TYPE;
4886 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
4887 STRLENOF( INTERVALSTR "=" ) ) )
4889 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
4890 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
4891 si->si_interval = 0;
4892 } else if ( strchr( val, ':' ) != NULL ) {
4893 char *next, *ptr = val;
4896 dd = strtol( ptr, &next, 10 );
4897 if ( next == ptr || next[0] != ':' || dd < 0 ) {
4898 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4899 "Error: parse_syncrepl_line: "
4900 "invalid interval \"%s\", unable to parse days", val );
4901 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4905 hh = strtol( ptr, &next, 10 );
4906 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
4907 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4908 "Error: parse_syncrepl_line: "
4909 "invalid interval \"%s\", unable to parse hours", val );
4910 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4914 mm = strtol( ptr, &next, 10 );
4915 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
4916 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4917 "Error: parse_syncrepl_line: "
4918 "invalid interval \"%s\", unable to parse minutes", val );
4919 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4923 ss = strtol( ptr, &next, 10 );
4924 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
4925 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4926 "Error: parse_syncrepl_line: "
4927 "invalid interval \"%s\", unable to parse seconds", val );
4928 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4931 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
4935 if ( lutil_parse_time( val, &t ) != 0 ) {
4936 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4937 "Error: parse_syncrepl_line: "
4938 "invalid interval \"%s\"", val );
4939 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4942 si->si_interval = (time_t)t;
4944 if ( si->si_interval < 0 ) {
4945 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4946 "Error: parse_syncrepl_line: "
4947 "invalid interval \"%ld\"",
4948 (long) si->si_interval);
4949 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4952 si->si_got |= GOT_INTERVAL;
4953 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
4954 STRLENOF( RETRYSTR "=" ) ) )
4956 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
4959 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
4960 STRLENOF( MANAGEDSAITSTR "=" ) ) )
4962 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
4963 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
4964 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
4966 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4967 "invalid manageDSAit value \"%s\".\n",
4969 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4972 si->si_got |= GOT_MANAGEDSAIT;
4973 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
4974 STRLENOF( SLIMITSTR "=") ) )
4976 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
4977 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4980 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
4981 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4982 "invalid size limit value \"%s\".\n",
4984 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4987 si->si_got |= GOT_SLIMIT;
4988 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
4989 STRLENOF( TLIMITSTR "=" ) ) )
4991 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
4992 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4995 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
4996 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4997 "invalid time limit value \"%s\".\n",
4999 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5002 si->si_got |= GOT_TLIMIT;
5003 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
5004 STRLENOF( SYNCDATASTR "=" ) ) )
5006 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
5007 si->si_syncdata = verb_to_mask( val, datamodes );
5008 si->si_got |= GOT_SYNCDATA;
5009 } else if ( !strncasecmp( c->argv[ i ], STRICT_REFRESH,
5010 STRLENOF( STRICT_REFRESH ) ) )
5012 si->si_strict_refresh = 1;
5013 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
5014 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5015 "Error: parse_syncrepl_line: "
5016 "unable to parse \"%s\"\n", c->argv[ i ] );
5017 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5020 si->si_got |= GOT_BINDCONF;
5023 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
5024 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5025 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
5026 si->si_got & GOT_RID ? "" : " "IDSTR,
5027 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
5028 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
5029 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5033 if ( !be_issubordinate( c->be, &si->si_base ) && !( si->si_got & GOT_SUFFIXM )) {
5034 ch_free( si->si_base.bv_val );
5035 BER_BVZERO( &si->si_base );
5036 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5037 "Base DN \"%s\" is not within the database naming context",
5039 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5043 #ifdef ENABLE_REWRITE
5044 if ( si->si_got & GOT_SUFFIXM ) {
5045 if (config_suffixm( c, si )) {
5046 ch_free( si->si_suffixm.bv_val );
5047 BER_BVZERO( &si->si_suffixm );
5048 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5049 "Error configuring rewrite engine" );
5050 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
5056 if ( !( si->si_got & GOT_RETRY ) ) {
5057 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
5058 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
5059 if ( si->si_retryinterval == NULL ) {
5060 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
5066 si->si_filter = str2filter( si->si_filterstr.bv_val );
5067 if ( si->si_filter == NULL ) {
5068 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": unable to parse filter=\"%s\"\n",
5069 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", si->si_filterstr.bv_val );
5083 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
5084 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
5085 "operations required for syncrepl", c->be->be_type );
5086 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5089 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
5090 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
5091 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
5094 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
5097 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
5101 si->si_bindconf.sb_tls = SB_TLS_OFF;
5102 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
5103 si->si_schemachecking = 0;
5104 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
5105 &si->si_filterstr );
5106 si->si_base.bv_val = NULL;
5107 si->si_scope = LDAP_SCOPE_SUBTREE;
5108 si->si_attrsonly = 0;
5109 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5110 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
5111 si->si_attrs = NULL;
5112 si->si_allattrs = 0;
5113 si->si_allopattrs = 0;
5114 si->si_exattrs = NULL;
5115 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
5116 si->si_interval = 86400;
5117 si->si_retryinterval = NULL;
5118 si->si_retrynum_init = NULL;
5119 si->si_retrynum = NULL;
5120 si->si_manageDSAit = 0;
5124 si->si_presentlist = NULL;
5125 LDAP_LIST_INIT( &si->si_nonpresentlist );
5126 ldap_pvt_thread_mutex_init( &si->si_mutex );
5128 rc = parse_syncrepl_line( c, si );
5133 /* Must be LDAPv3 because we need controls */
5134 switch ( si->si_bindconf.sb_version ) {
5136 /* not explicitly set */
5137 si->si_bindconf.sb_version = LDAP_VERSION3;
5140 /* explicitly set */
5143 Debug( LDAP_DEBUG_ANY,
5144 "version %d incompatible with syncrepl\n",
5145 si->si_bindconf.sb_version, 0, 0 );
5146 syncinfo_free( si, 0 );
5150 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
5151 snprintf( c->cr_msg, sizeof( c->cr_msg ),
5152 "<%s> invalid URL", c->argv[0] );
5153 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
5154 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
5159 if ( slapMode & SLAP_SERVER_MODE ) {
5161 /* check if consumer points to current server and database.
5162 * If so, ignore this configuration.
5164 if ( !SLAP_DBHIDDEN( c->be ) ) {
5166 /* if searchbase doesn't match current DB suffix,
5167 * assume it's different
5169 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
5170 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
5175 /* if searchbase matches, see if URLs match */
5176 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
5182 init_syncrepl( si );
5183 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5184 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
5185 si->si_interval, do_syncrepl, si, "do_syncrepl",
5187 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5189 rc = config_sync_shadow( c ) ? -1 : 0;
5194 /* mirrormode still needs to see this flag in tool mode */
5195 rc = config_sync_shadow( c ) ? -1 : 0;
5197 ldap_free_urldesc( lud );
5201 /* Use main slapd defaults */
5202 bindconf_tls_defaults( &si->si_bindconf );
5205 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
5206 syncinfo_free( si, 0 );
5209 Debug( LDAP_DEBUG_CONFIG,
5210 "Config: ** successfully added syncrepl %s \"%s\"\n",
5212 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
5213 "(null)" : si->si_bindconf.sb_uri.bv_val, 0 );
5214 if ( c->be->be_syncinfo ) {
5217 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
5219 /* add new syncrepl to end of list (same order as when deleting) */
5220 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
5223 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
5224 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
5225 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_pmutex );
5227 c->be->be_syncinfo = si;
5229 si->si_cookieState->cs_ref++;
5238 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
5240 struct berval bc, uri, bs;
5241 char buf[BUFSIZ*2], *ptr;
5244 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
5248 /* temporarily inhibit bindconf from printing URI */
5249 uri = si->si_bindconf.sb_uri;
5250 BER_BVZERO( &si->si_bindconf.sb_uri );
5251 si->si_bindconf.sb_version = 0;
5252 bindconf_unparse( &si->si_bindconf, &bc );
5253 si->si_bindconf.sb_uri = uri;
5254 si->si_bindconf.sb_version = LDAP_VERSION3;
5257 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_RID_MAX );
5258 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
5259 si->si_rid, si->si_bindconf.sb_uri.bv_val );
5260 if ( len >= sizeof( buf ) ) return;
5262 if ( !BER_BVISNULL( &bc ) ) {
5263 if ( WHATSLEFT <= bc.bv_len ) {
5267 ptr = lutil_strcopy( ptr, bc.bv_val );
5270 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
5271 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
5272 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
5273 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
5276 if ( !BER_BVISNULL( &si->si_base ) ) {
5277 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
5278 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
5279 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
5282 #ifdef ENABLE_REWRITE
5283 if ( !BER_BVISNULL( &si->si_suffixm ) ) {
5284 if ( WHATSLEFT <= STRLENOF( " " SUFFIXMSTR "=\"" "\"" ) + si->si_suffixm.bv_len ) return;
5285 ptr = lutil_strcopy( ptr, " " SUFFIXMSTR "=\"" );
5286 ptr = lutil_strcopy( ptr, si->si_suffixm.bv_val );
5290 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
5291 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
5292 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
5293 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
5296 if ( !BER_BVISNULL( &si->si_logbase ) ) {
5297 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
5298 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
5299 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
5302 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
5303 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
5304 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
5305 ptr = lutil_strcopy( ptr, bs.bv_val );
5307 if ( si->si_attrsonly ) {
5308 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5309 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
5311 if ( si->si_anfile ) {
5312 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
5313 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
5314 ptr = lutil_strcopy( ptr, si->si_anfile );
5316 } else if ( si->si_allattrs || si->si_allopattrs ||
5317 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
5321 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
5322 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
5324 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
5325 if ( ptr == NULL ) return;
5326 if ( si->si_allattrs ) {
5327 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
5328 if ( old != ptr ) *ptr++ = ',';
5331 if ( si->si_allopattrs ) {
5332 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
5333 if ( old != ptr ) *ptr++ = ',';
5338 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
5339 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
5340 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
5341 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
5342 if ( ptr == NULL ) return;
5344 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
5345 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
5346 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
5348 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
5349 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
5350 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
5351 "refreshAndPersist" : "refreshOnly" );
5353 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
5356 dd = si->si_interval;
5363 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
5364 INTERVALSTR, dd, hh, mm, ss );
5365 if ( len >= WHATSLEFT ) return;
5369 if ( si->si_got & GOT_RETRY ) {
5370 const char *space = "";
5371 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
5372 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
5373 for (i=0; si->si_retryinterval[i]; i++) {
5374 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
5375 (long) si->si_retryinterval[i] );
5377 if ( WHATSLEFT - 1 <= len ) return;
5379 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
5382 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
5383 if ( WHATSLEFT <= len ) return;
5387 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
5390 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
5393 if ( si->si_slimit ) {
5394 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
5395 if ( WHATSLEFT <= len ) return;
5399 if ( si->si_tlimit ) {
5400 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
5401 if ( WHATSLEFT <= len ) return;
5405 if ( si->si_syncdata ) {
5406 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
5407 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
5408 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
5409 ptr = lutil_strcopy( ptr, bc.bv_val );
5412 bc.bv_len = ptr - buf;
5414 ber_dupbv( bv, &bc );
5418 syncrepl_config( ConfigArgs *c )
5420 if (c->op == SLAP_CONFIG_EMIT) {
5421 if ( c->be->be_syncinfo ) {
5425 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
5426 syncrepl_unparse( si, &bv );
5427 ber_bvarray_add( &c->rvalue_vals, &bv );
5432 } else if ( c->op == LDAP_MOD_DELETE ) {
5434 if ( c->be->be_syncinfo ) {
5435 syncinfo_t *si, **sip;
5438 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
5440 if ( c->valx == -1 || i == c->valx ) {
5444 /* If the task is currently active, we have to leave
5445 * it running. It will exit on its own. This will only
5446 * happen when running on the cn=config DB.
5449 if ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
5452 /* There is no active thread, but we must still
5453 * ensure that no thread is (or will be) queued
5454 * while we removes the task.
5456 struct re_s *re = si->si_re;
5459 if ( si->si_conn ) {
5460 connection_client_stop( si->si_conn );
5464 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
5465 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) ) {
5466 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
5469 ldap_pvt_runqueue_remove( &slapd_rq, re );
5470 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
5472 if ( ldap_pvt_thread_pool_retract( &connection_pool,
5473 re->routine, re ) > 0 )
5476 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
5480 syncinfo_free( si, 0 );
5489 if ( !c->be->be_syncinfo ) {
5490 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
5494 if ( SLAP_SLURP_SHADOW( c->be ) ) {
5495 Debug(LDAP_DEBUG_ANY, "%s: "
5496 "syncrepl: database already shadowed.\n",
5500 return add_syncrepl( c );