From 6a7b61450c6db1a74e31fb7a52497be6fce50e2b Mon Sep 17 00:00:00 2001
From: Steve Langasek <steve.langasek@ubuntu.com>
Date: Mon, 15 Jul 2013 15:57:56 -0700
Subject: [PATCH] dpkg-maintscript-helper: when a conffile is no longer owned
 by the current (or named) package, don't move/remove it.  Closes: #716948.

---
 debian/changelog                   |  4 +++
 scripts/dpkg-maintscript-helper.sh | 66 +++++++++++++++++++++++++++++++++++---
 2 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index d3faa2c..0c3c037 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -132,6 +132,10 @@ dpkg (1.17.0) UNRELEASED; urgency=low
   * Fix typo in dpkg-source(1) man page French translation.
     Thanks to Cédric Boutillier <boutil@debian.org>. Closes: #708292
 
+  [ Steve Langasek ]
+  * dpkg-maintscript-helper: when a conffile is no longer owned by the
+    current (or named) package, don't move/remove it.  Closes: #716948.
+
  -- Guillem Jover <guillem@debian.org>  Fri, 03 Aug 2012 13:21:00 +0200
 
 dpkg (1.16.10) unstable; urgency=low
diff --git a/scripts/dpkg-maintscript-helper.sh b/scripts/dpkg-maintscript-helper.sh
index 6e2252e..c28b5be 100755
--- a/scripts/dpkg-maintscript-helper.sh
+++ b/scripts/dpkg-maintscript-helper.sh
@@ -61,7 +61,7 @@ rm_conffile() {
 	postinst)
 		if [ "$1" = "configure" ] && [ -n "$2" ] &&
 		   dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
-			finish_rm_conffile $CONFFILE
+			finish_rm_conffile "$CONFFILE"
 		fi
 		;;
 	postrm)
@@ -72,7 +72,7 @@ rm_conffile() {
 		if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
 		   [ -n "$2" ] &&
 		   dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
-			abort_rm_conffile "$CONFFILE"
+			abort_rm_conffile "$CONFFILE" "$PACKAGE"
 		fi
 		;;
 	*)
@@ -87,6 +87,17 @@ prepare_rm_conffile() {
 
 	[ -e "$CONFFILE" ] || return 0
 
+	owner=$(dpkg-query -S "$CONFFILE" | sed -e's/:[^:]*$//')
+	case $owner in
+		"$PACKAGE"|"$PACKAGE, *"|"* $PACKAGE, *"|"* $PACKAGE")
+			;;
+		*)
+			debug "Conffile '$CONFFILE' not owned by package " \
+			      "'$PACKAGE', skipping rm_conffile"
+			return 0
+			;;
+	esac
+
 	local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
 	local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \
 		sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
@@ -114,6 +125,18 @@ finish_rm_conffile() {
 
 abort_rm_conffile() {
 	local CONFFILE="$1"
+	local PACKAGE="$2"
+
+	owner=$(dpkg-query -S "$CONFFILE" | sed -e's/:[^:]*$//')
+	case $owner in
+		"$PACKAGE"|"$PACKAGE, *"|"* $PACKAGE, *"|"* $PACKAGE")
+			;;
+		*)
+			debug "Conffile '$CONFFILE' not owned by package " \
+			      "'$PACKAGE', skipping rm_conffile"
+			return 0
+			;;
+	esac
 
 	if [ -e "$CONFFILE.dpkg-remove" ]; then
 		echo "Reinstalling $CONFFILE that was moved away"
@@ -164,14 +187,14 @@ mv_conffile() {
 	postinst)
 		if [ "$1" = "configure" ] && [ -n "$2" ] &&
 		   dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
-			finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE"
+			finish_mv_conffile "$OLDCONFFILE" "$NEWCONFFILE" "$PACKAGE"
 		fi
 		;;
 	postrm)
 		if [ "$1" = "abort-install" -o "$1" = "abort-upgrade" ] &&
 		   [ -n "$2" ] &&
 		   dpkg --compare-versions "$2" le-nl "$LASTVERSION"; then
-			abort_mv_conffile "$OLDCONFFILE"
+			abort_mv_conffile "$OLDCONFFILE" "$PACKAGE"
 		fi
 		;;
 	*)
@@ -186,6 +209,17 @@ prepare_mv_conffile() {
 
 	[ -e "$CONFFILE" ] || return 0
 
+	owner=$(dpkg-query -S $CONFFILE | sed -e's/:[^:]*$//')
+	case $owner in
+		"$PACKAGE"|"$PACKAGE, *"|"* $PACKAGE, *"|"* $PACKAGE")
+			;;
+		*)
+			debug "Conffile '$CONFFILE' not owned by package " \
+			      "'$PACKAGE', skipping mv_conffile"
+			return 0
+			;;
+	esac
+
 	local md5sum="$(md5sum $CONFFILE | sed -e 's/ .*//')"
 	local old_md5sum="$(dpkg-query -W -f='${Conffiles}' $PACKAGE | \
 		sed -n -e "\' $CONFFILE ' { s/ obsolete$//; s/.* //; p }")"
@@ -197,11 +231,23 @@ prepare_mv_conffile() {
 finish_mv_conffile() {
 	local OLDCONFFILE="$1"
 	local NEWCONFFILE="$2"
+	local PACKAGE="$2"
 
 	rm -f $OLDCONFFILE.dpkg-remove
 
 	[ -e "$OLDCONFFILE" ] || return 0
 
+	owner=$(dpkg-query -S $OLDCONFFILE | sed -e's/:[^:]*$//')
+	case $owner in
+		"$PACKAGE"|"$PACKAGE, *"|"* $PACKAGE, *"|"* $PACKAGE")
+			;;
+		*)
+			debug "Conffile '$CONFFILE' not owned by package " \
+			      "'$PACKAGE', skipping mv_conffile"
+			return 0
+			;;
+	esac
+
 	echo "Preserving user changes to $NEWCONFFILE (renamed from $OLDCONFFILE)..."
 	mv -f "$NEWCONFFILE" "$NEWCONFFILE.dpkg-new"
 	mv -f "$OLDCONFFILE" "$NEWCONFFILE"
@@ -209,6 +255,18 @@ finish_mv_conffile() {
 
 abort_mv_conffile() {
 	local CONFFILE="$1"
+	local PACKAGE="$2"
+
+	owner=$(dpkg-query -S $CONFFILE | sed -e's/:[^:]*$//')
+	case $owner in
+		"$PACKAGE"|"$PACKAGE, *"|"* $PACKAGE, *"|"* $PACKAGE")
+			;;
+		*)
+			debug "Conffile '$CONFFILE' not owned by package " \
+			      "'$PACKAGE', skipping mv_conffile"
+			return 0
+			;;
+	esac
 
 	if [ -e "$CONFFILE.dpkg-remove" ]; then
 		echo "Reinstalling $CONFFILE that was moved away"
-- 
1.8.3.2

