From 91985bc5adace75a938d51a97099c2a44cf5c661 Mon Sep 17 00:00:00 2001
From: Jessica Clarke <jrtc27@FreeBSD.org>
Date: Tue, 29 Nov 2022 02:56:25 +0000
Subject: [PATCH] bsdinstall: Fix issues parsing rc.conf.services on revisit

There are a few issues here, some of which are hiding others. The first
is that we don't use double quotes around the command substitution so
every word in the conf file is treated as a separate argument to eval,
resulting in spaces being used in place of newlines and thus comments in
the file commenting out the rest of the file, not just to the end of
their line. In particular, we insert one comment just before the dumpdev
entry (the final one in the file) and so we never see dumpdev as set,
and thus set a default value of on for the menu.

The second issue is that, for dumpdev, it takes a value of AUTO not YES
when set, but we don't replace this with on when eval'ing, so then end
up giving AUTO to bsddialog which is interpreted the same as off (which
seems to match GPL dialog). Thus handle AUTO like YES otherwise it will
always appear as unchecked on revisit.

The final issue is that our case-insensitive YES/NO (and now AUTO)
replacements have no word boundaries around them so match the middle of
words too. As it happens this doesn't matter in practice at the moment,
but it could in future; currently the only effect is that it rewrites
moused_nondefault_enable to moused_offndefault_enable, but since this
variable is never read, only written based on moused(_enable) this is
harmless, but we should fix it in case a service comes along in future
that does get affected by it.
---
 usr.sbin/bsdinstall/scripts/services | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/usr.sbin/bsdinstall/scripts/services b/usr.sbin/bsdinstall/scripts/services
index 44d7c5f138e6..294e044c74ae 100755
--- a/usr.sbin/bsdinstall/scripts/services
+++ b/usr.sbin/bsdinstall/scripts/services
@@ -32,8 +32,8 @@ BSDCFG_SHARE="/usr/share/bsdconfig"
 : ${BSDDIALOG_OK=0}
 
 if [ -f $BSDINSTALL_TMPETC/rc.conf.services ]; then
-	eval $( sed -e s/YES/on/i -e s/NO/off/i \
-		$BSDINSTALL_TMPETC/rc.conf.services )
+	eval "$( sed -E -e 's/\<(YES|AUTO)\>/on/i' -e 's/\<NO\>/off/i' \
+		$BSDINSTALL_TMPETC/rc.conf.services )"
 else
 	# Default service states. Everything is off if not enabled.
 	sshd_enable="on"