git: d0a167652278 - main - sysrc: Support special characters in file names

From: Devin Teske <dteske_at_FreeBSD.org>
Date: Wed, 25 Nov 2026 21:51:29 UTC
The branch main has been updated by dteske:

URL: https://cgit.FreeBSD.org/src/commit/?id=d0a167652278ed81d259a1b77620b0c1cb9f72c8

commit d0a167652278ed81d259a1b77620b0c1cb9f72c8
Author:     Devin Teske <dteske@FreeBSD.org>
AuthorDate: 2026-11-25 21:46:30 +0000
Commit:     Devin Teske <dteske@FreeBSD.org>
CommitDate: 2026-11-25 21:47:38 +0000

    sysrc: Support special characters in file names
    
    Previously, passing -f "abc'123" (for example) would break sysrc.
    Support special characters such as single-quote, double-quote,
    dollar sign, backtick, asterisk, and more.
    
    It was previously impossible to handle file names with glob
    characters. If you absolutely need to use a glob to expand a list
    of potential files for `-f' you can still do it in two ways:
    
            sysrc -f "$( echo glob )" ...
            RC_CONFS=$( echo glob ) sysrc ...
    
    While here, clean up trailing whitespace on 3 lines.
    
    Reviewed by:    kfv
    Differential Revision:  https://reviews.freebsd.org/D61740
---
 usr.sbin/sysrc/sysrc      | 20 ++++++++++---
 usr.sbin/sysrc/sysrc.subr | 72 +++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 76 insertions(+), 16 deletions(-)

diff --git a/usr.sbin/sysrc/sysrc b/usr.sbin/sysrc/sysrc
index c5bb85be3be1..1e8cfb74bba8 100644
--- a/usr.sbin/sysrc/sysrc
+++ b/usr.sbin/sysrc/sysrc
@@ -16,7 +16,7 @@ pgm="${0##*/}" # Program basename
 #
 # Version information
 #
-SYSRC_VERSION="8.9 Nov-18,2026"
+SYSRC_VERSION="9.0 Nov-18,2026"
 
 #
 # Options
@@ -316,13 +316,14 @@ fi
 # Process `-L' flag
 #
 if [ "$LIST_SERVICE_CONFS" ]; then
-	list= 
+	list=
 
 	#
 	# List rc_conf_files if no service names given
 	#
 	files=
 	[ $# -eq 0 ] && files=$( f_sysrc_get rc_conf_files )
+	set -f # noglob
 	for file in $files; do
 		if [ "$EXISTING_ONLY" ]; then
 			[ -e "$file" -a ! -d "$file" ] || continue
@@ -333,6 +334,7 @@ if [ "$LIST_SERVICE_CONFS" ]; then
 		list="$list $file"
 		[ "$VERBOSE$SHOW_EQUALS" ] || echo "$file"
 	done
+	set +f # glob
 	list="${list# }"
 	if [ $# -eq 0 ]; then
 		if [ "$VERBOSE" ]; then
@@ -349,6 +351,7 @@ if [ "$LIST_SERVICE_CONFS" ]; then
 	for service in ${*:-$( service -l )}; do
 		slist=
 		f_sysrc_service_configs $service files || retval=$? continue
+		set -f # noglob
 		for file in $files; do
 			if [ "$EXISTING_ONLY" ]; then
 				[ -e "$file" -a ! -d "$file" ] || continue
@@ -360,6 +363,7 @@ if [ "$LIST_SERVICE_CONFS" ]; then
 			fi
 			slist="$slist $file"
 		done
+		set +f # glob
 		slist="${slist# }"
 		if [ $# -gt 0 ]; then
 			[ "$slist" ] || retval=$?
@@ -371,9 +375,11 @@ if [ "$LIST_SERVICE_CONFS" ]; then
 			[ "$slist" ] && shell_fmt "$service=%s\n" "$slist"
 			continue
 		elif [ $# -eq 0 -o ! "$QUIET" ]; then
+			set -f # noglob
 			for item in $slist; do
 				echo "$item"
 			done
+			set +f # glob
 		fi
 		list="$list${slist:+ }$slist"
 	done
@@ -415,7 +421,7 @@ if [ "$EXISTING_ONLY" ]; then
 	# RC_CONFS to override the unpreened value. If RC_CONFS already has a
 	# value (`-D', `-f file', `-s name', or inherited from parent), use it.
 	# Otherwise, include filtered contents of rc_conf_files.
-	# 
+	#
 	RC_CONFS=$(
 		if [ "${RC_CONFS+set}" ]; then
 			set -- $RC_CONFS
@@ -442,9 +448,11 @@ if [ "$LIST_CONFS" ]; then
 	else
 		list=$( f_sysrc_get rc_conf_files )
 	fi
+	set -f # noglob
 	for item in $list; do
 		echo "$item"
 	done
+	set +f # glob
 	exit $SUCCESS
 fi
 
@@ -458,7 +466,8 @@ fi
 #
 errmsg="Exiting due to previous syntax errors"
 if [ "${RC_CONFS+set}" ]; then
-	( for i in $RC_CONFS; do
+	( set -f # noglob
+	  for i in $RC_CONFS; do
 	  	[ -e "$i" ] || continue
 	  	/bin/sh -n "$i" || exit $FAILURE
 	  done
@@ -467,6 +476,7 @@ if [ "${RC_CONFS+set}" ]; then
 else
 	/bin/sh -n "$RC_DEFAULTS" || die "$errmsg"
 	( . "$RC_DEFAULTS"
+	  set -f # noglob
 	  for i in $rc_conf_files; do
 	  	[ -e "$i" ] || continue
 	  	/bin/sh -n "$i" || exit $FAILURE
@@ -680,7 +690,9 @@ if [ "$SHOW_ALL" ]; then
 				rc_conf_files="${rc_conf_files% }"
 			fi
 
+			set -f # noglob
 			source_rc_confs
+			set +f # glob
 
 			#
 			# If passed `-a' (rather than `-A'), remove
diff --git a/usr.sbin/sysrc/sysrc.subr b/usr.sbin/sysrc/sysrc.subr
index 2e4ab79706f0..9ee396f96086 100644
--- a/usr.sbin/sysrc/sysrc.subr
+++ b/usr.sbin/sysrc/sysrc.subr
@@ -290,6 +290,7 @@ f_sysrc_get()
 			_rc_confs_set=1
 		fi
 
+		set -f # noglob
 		source_rc_confs > /dev/null 2>&1
 
 		#
@@ -832,6 +833,7 @@ f_sysrc_service_configs()
 {
 	local OPTIND=1 OPTARG __flag __append= __prepend=
 	local __local_startup __dir __spath __stype __names=
+	local __noglob=
 
 	while getopts ap __flag; do
 		case "$__flag" in
@@ -844,12 +846,18 @@ f_sysrc_service_configs()
 	[ $# -gt 0 ] || return $FAILURE
 	local __sname="$1" __var_to_set="$2"
 
+	case "$-" in
+	*f*) __noglob=1 ;;
+	esac
+
 	__local_startup=$( f_sysrc_get local_startup )
+	set -f # noglob
 	for __dir in /etc/rc.d $__local_startup; do
 		__spath="$__dir/$__sname"
 		[ -f "$__spath" -a -x "$__spath" ] || __spath= continue
 		break
 	done
+	[ "$__noglob" ] || set +f # glob
 	[ "$__spath" ] || return $FAILURE
 
 	__stype=$( file -b "$__spath" 2> /dev/null )
@@ -876,17 +884,21 @@ f_sysrc_service_configs()
 	esac
 
 	local __name __test_path __configs=
+	set -f # noglob
 	for __name in $__names; do
 		for __dir in /etc/rc.d $__local_startup; do
 			__test_path="${__dir%/rc.d}/rc.conf.d/$__name"
 			[ -d "$__test_path" ] ||
 				__configs="$__configs $__test_path" continue
+			set +f # glob
 			for __test_path in "$__test_path"/*; do
 				[ -f "$__test_path" ] || continue
 				__configs="$__configs $__test_path"
-			done	
+			done
+			set -f # noglob
 		done
 	done
+	[ "$__noglob" ] || set +f # glob
 	__configs="${__configs# }"
 
 	if [ "$__var_to_set" ]; then
@@ -961,6 +973,7 @@ f_sysrc_find()
 	local rc_conf_files="$( f_sysrc_get rc_conf_files )"
 	local conf_files=
 	local file
+	local noglob=
 
 	# Check parameters
 	case "$varname" in
@@ -973,6 +986,15 @@ f_sysrc_find()
 	#
 	[ "${RC_CONFS+set}" ] && rc_conf_files="$RC_CONFS"
 
+	#
+	# Determine if caller has set noglob before we disable filename
+	# globbing, so we can later return to the inherited state.
+	#
+	case "$-" in
+	*f*) noglob=1 ;;
+	esac
+	set -f # noglob
+
 	#
 	# Reverse the order of files in rc_conf_files (the boot process sources
 	# these in order, so we will search them in reverse-order to find the
@@ -996,10 +1018,12 @@ f_sysrc_find()
 		[ -f "$file" -a -r "$file" ] || continue
 		if grep -Eq "$regex" $file; then
 			echo $file
+			[ "$noglob" ] || set +f # glob
 			return $SUCCESS
 		fi
 	done
 
+	[ "$noglob" ] || set +f # glob
 	return $FAILURE # Not found
 }
 
@@ -1163,8 +1187,15 @@ f_sysrc_set()
 	#
 	if [ "$not_found" ]; then
 		# Add a newline if missing before appending to the file
-		[ ! -e "$file" ] || awk 'BEGIN { wc = 0 } NR == 1 {
-			(cmd = "wc -l " FILENAME) | getline
+		[ ! -e "$file" ] || awk '
+		BEGIN {
+			wc = 0
+			sq = "\047"
+		}
+		NR == 1 {
+			file = FILENAME
+			gsub(sq, sq "\\" sq sq, file)
+			(cmd = "wc -l " sq file sq) | getline
 			close(cmd)
 			wc = $1
 		} END { exit wc != NR }' "$file" ||
@@ -1198,8 +1229,9 @@ f_sysrc_set()
 	# temporary file over the destination, the destination will inherit the
 	# permissions from the temporary file).
 	#
-	local mode
-	f_eval_catch -dk mode $funcname stat 'stat -f "%%#Lp" "%s"' "$file" ||
+	local _file mode
+	f_shell_escape "$file" _file
+	f_eval_catch -dk mode $funcname stat "stat -f '%%#Lp' '%s'" "$_file" ||
 		mode=0644
 	f_eval_catch -d $funcname chmod 'chmod "%s" "%s"' "$mode" "$tmpfile"
 
@@ -1210,7 +1242,7 @@ f_sysrc_set()
 	#
 	local owner
 	f_eval_catch -dk owner $funcname stat \
-		'stat -f "%%u:%%g" "%s"' "$file" || owner="root:wheel"
+		"stat -f '%%u:%%g' '%s'" "$_file" || owner="root:wheel"
 	f_eval_catch -d $funcname chown 'chown "%s" "%s"' "$owner" "$tmpfile"
 
 	#
@@ -1245,7 +1277,7 @@ f_sysrc_set()
 	#
 	# Finally, move the temporary file into place.
 	#
-	f_eval_catch -de $funcname mv 'mv "%s" "%s"' "$tmpfile" "$file"
+	f_eval_catch -de $funcname mv "mv '%s' '%s'" "$tmpfile" "$_file"
 }
 
 # f_sysrc_delete $varname
@@ -1278,7 +1310,8 @@ f_sysrc_delete()
 {
 	local funcname=f_sysrc_delete
 	local varname="$1"
-	local file
+	local file _file
+	local noglob=
 
 	# Check arguments
 	[ "$varname" ] || return $FAILURE
@@ -1287,8 +1320,13 @@ f_sysrc_delete()
 	# Operate on each of the specified files
 	#
 	local tmpfile
+	case "$-" in
+	*f*) noglob=1 ;;
+	esac
+	set -f # noglob
 	for file in ${RC_CONFS-$( f_sysrc_get rc_conf_files )}; do
 		[ -e "$file" ] || continue
+		f_shell_escape "$file" _file
 
 		#
 		# Create a new temporary file to write to.
@@ -1297,6 +1335,7 @@ f_sysrc_delete()
 			'mktemp -t "%s"' "$pgm"
 		then
 			echo "$tmpfile" >&2
+			[ "$noglob" ] || set +f # glob
 			return $FAILURE
 		fi
 
@@ -1306,9 +1345,9 @@ f_sysrc_delete()
 		#
 		local mode owner
 		f_eval_catch -dk mode $funcname stat \
-			'stat -f "%%#Lp" "%s"' "$file" || mode=0644
+			"stat -f '%%#Lp' '%s'" "$_file" || mode=0644
 		f_eval_catch -dk owner $funcname stat \
-			'stat -f "%%u:%%g" "%s"' "$file" || owner="root:wheel"
+			"stat -f '%%u:%%g' '%s'" "$_file" || owner="root:wheel"
 		f_eval_catch -d $funcname chmod \
 			'chmod "%s" "%s"' "$mode" "$tmpfile"
 		f_eval_catch -d $funcname chown \
@@ -1334,6 +1373,7 @@ f_sysrc_delete()
 			f_err "$msg_previous_syntax_errors\n" \
 			      "$pgm" "$file"
 			rm -f "$tmpfile"
+			[ "$noglob" ] || set +f # glob
 			return $FAILURE
 		fi
 
@@ -1343,15 +1383,23 @@ f_sysrc_delete()
 		if [ ! -w "$file" ]; then
 			f_err "$msg_permission_denied\n" "$pgm" "$file"
 			rm -f "$tmpfile"
+			[ "$noglob" ] || set +f # glob
 			return $FAILURE
 		fi
 
 		#
 		# Finally, move the temporary file into place.
 		#
-		f_eval_catch -de $funcname mv \
-			'mv "%s" "%s"' "$tmpfile" "$file" || return $FAILURE
+		if ! f_eval_catch -de $funcname mv \
+			"mv '%s' '%s'" "$tmpfile" "$_file"
+		then
+			[ "$noglob" ] || set +f
+			return $FAILURE
+		fi
 	done
+
+	[ "$noglob" ] || set +f # glob
+	return $SUCCESS
 }
 
 ############################################################ MAIN