From 2dd6aaeda9479f7c610fe7c9508205e28f2186ff Mon Sep 17 00:00:00 2001
From: Quanah Gibson-Mount <quanah@openldap.org>
Date: Thu, 27 Jan 2011 21:08:52 +0000
Subject: [PATCH] ITS#6807

---
 CHANGES                  |  1 +
 servers/slapd/syncrepl.c | 16 ++++++++++++----
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/CHANGES b/CHANGES
index 7d61377f2a..688e60ae05 100644
--- a/CHANGES
+++ b/CHANGES
@@ -61,6 +61,7 @@ OpenLDAP 2.4.24 Engineering
 	Fixed slapd syncrepl reuse of presence list (ITS#6707)
 	Fixed slapd syncrepl uninitialized return code (ITS#6719)
 	Fixed slapd syncrepl variable initialization (ITS#6739)
+	Fixed slapd syncrepl refresh to use complete cookie (ITS#6807)
 	Fixed slapd-bdb hasSubordinates generation (ITS#6712)
 	Fixed slapd-bdb entry cache delete failure (ITS#6577)
 	Fixed slapd-bdb entry cache leak on multi-core systems (ITS#6660)
diff --git a/servers/slapd/syncrepl.c b/servers/slapd/syncrepl.c
index 4dae14820d..b2aeb3d985 100644
--- a/servers/slapd/syncrepl.c
+++ b/servers/slapd/syncrepl.c
@@ -685,11 +685,13 @@ do_syncrep1(
 	} else {
 		/* ITS#6367: recreate the cookie so it has our SID, not our peer's */
 		ch_free( si->si_syncCookie.octet_str.bv_val );
-		slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
-			si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
-			si->si_syncCookie.sid );
+		BER_BVZERO( &si->si_syncCookie.octet_str );
 		/* Look for contextCSN from syncprov overlay. */
 		check_syncprov( op, si );
+		if ( BER_BVISNULL( &si->si_syncCookie.octet_str ))
+			slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
+				si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
+				si->si_syncCookie.sid );
 	}
 
 	si->si_refreshDone = 0;
@@ -3308,7 +3310,6 @@ syncrepl_updateCookie(
 
 	if ( rs_modify.sr_err == LDAP_SUCCESS ) {
 		slap_sync_cookie_free( &si->si_syncCookie, 0 );
-		slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
 		/* If we replaced any old values */
 		for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
 			if ( mod.sml_values[i].bv_val != si->si_cookieState->cs_vals[i].bv_val )
@@ -3324,6 +3325,13 @@ syncrepl_updateCookie(
 				si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
 		}
 
+		/* Don't just dup the provider's cookie, recreate it */
+		si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
+		ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn, si->si_cookieState->cs_vals, NULL );
+		si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num * sizeof(int) );
+		for ( i=0; i<si->si_cookieState->cs_num; i++ )
+			si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
+
 		si->si_cookieState->cs_age++;
 		si->si_cookieAge = si->si_cookieState->cs_age;
 	} else {
-- 
GitLab