sysrc revision 272191
1#!/bin/sh
2#-
3# Copyright (c) 2010-2014 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: stable/10/usr.sbin/sysrc/sysrc 272191 2014-09-26 22:54:10Z dteske $
28#
29############################################################ INCLUDES
30
31# Prevent `-d' from being interpreted as a debug flag by common.subr
32DEBUG_SELF_INITIALIZE=
33
34BSDCFG_SHARE="/usr/share/bsdconfig"
35[ "$_COMMON_SUBR" ] || . $BSDCFG_SHARE/common.subr || exit 1
36[ "$_SYSRC_SUBR"  ] || f_include $BSDCFG_SHARE/sysrc.subr
37
38############################################################ GLOBALS
39
40#
41# Version information
42#
43SYSRC_VERSION="6.1 Jul-18,2014"
44
45#
46# Options
47#
48CHECK_ONLY=
49DELETE=
50DESCRIBE=
51IGNORE_UNKNOWNS=
52JAIL=
53QUIET=
54ROOTDIR=
55SHOW_ALL=
56SHOW_EQUALS=
57SHOW_FILE=
58SHOW_NAME=1
59SHOW_VALUE=1
60SYSRC_VERBOSE=
61
62############################################################ FUNCTIONS
63
64# die [ $fmt [ $opts ... ]]
65#
66# Optionally print a message to stderr before exiting with failure status.
67#
68die()
69{
70	local fmt="$1"
71	[ $# -gt 0 ] && shift 1
72	[  "$fmt"  ] && f_err "$fmt\n" "$@"
73
74	exit $FAILURE
75}
76
77# usage
78#
79# Prints a short syntax statement and exits.
80#
81usage()
82{
83	f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm"
84	f_err "Try \`%s --help' for more information.\n" "$pgm"
85	die
86}
87
88# help
89#
90# Prints a full syntax statement and exits.
91#
92help()
93{
94	local optfmt="\t%-11s%s\n"
95	local envfmt="\t%-17s%s\n"
96
97	f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm"
98
99	f_err "OPTIONS:\n"
100	f_err "$optfmt" "-a" \
101	      "Dump a list of all non-default configuration variables."
102	f_err "$optfmt" "-A" \
103	      "Dump a list of all configuration variables (incl. defaults)."
104	f_err "$optfmt" "-c" \
105	      "Check. Return success if set or no changes, else error."
106	f_err "$optfmt" "-d" \
107	      "Print a description of the given variable."
108	f_err "$optfmt" "-D" \
109	      "Show default value(s) only (this is the same as setting"
110	f_err "$optfmt" "" \
111	      "RC_CONFS to NULL or passing \`-f' with a NULL file-argument)."
112	f_err "$optfmt" "-e" \
113	      "Print query results as \`var=value' (useful for producing"
114	f_err "$optfmt" "" \
115	      "output to be fed back in). Ignored if \`-n' is specified."
116	f_err "$optfmt" "-f file" \
117	      "Operate on the specified file(s) instead of rc_conf_files."
118	f_err "$optfmt" "" \
119	      "Can be specified multiple times for additional files."
120	f_err "$optfmt" "-F" \
121	      "Show only the last rc.conf(5) file each directive is in."
122	f_err "$optfmt" "-h" \
123	      "Print a short usage statement to stderr and exit."
124	f_err "$optfmt" "--help" \
125	      "Print this message to stderr and exit."
126	f_err "$optfmt" "-i" \
127	      "Ignore unknown variables."
128	f_err "$optfmt" "-j jail" \
129	      "The jid or name of the jail to operate within (overrides"
130	f_err "$optfmt" "" \
131	      "\`-R dir'; requires jexec(8))."
132	f_err "$optfmt" "-n" \
133	      "Show only variable values, not their names."
134	f_err "$optfmt" "-N" \
135	      "Show only variable names, not their values."
136	f_err "$optfmt" "-q" \
137	      "Quiet. Disable verbose and hide certain errors."
138	f_err "$optfmt" "-R dir" \
139	      "Operate within the root directory \`dir' rather than \`/'."
140	f_err "$optfmt" "-v" \
141	      "Verbose. Print the pathname of the specific rc.conf(5)"
142	f_err "$optfmt" "" \
143	      "file where the directive was found."
144	f_err "$optfmt" "--version" \
145	      "Print version information to stdout and exit."
146	f_err "$optfmt" "-x" \
147	      "Remove variable(s) from specified file(s)."
148	f_err "\n"
149
150	f_err "ENVIRONMENT:\n"
151	f_err "$envfmt" "RC_CONFS" \
152	      "Override default rc_conf_files (even if set to NULL)."
153	f_err "$envfmt" "RC_DEFAULTS" \
154	      "Location of \`/etc/defaults/rc.conf' file."
155
156	die
157}
158
159# jail_depend
160#
161# Dump dependencies such as language-file variables and include files to stdout
162# to be piped-into sh(1) running via jexec(8)/chroot(8). As a security measure,
163# this prevents existing language files and library files from being loaded in
164# the jail. This also relaxes the requirement to have these files in every jail
165# before sysrc can be used on said jail.
166#
167jail_depend()
168{
169	#
170	# Indicate that we are jailed
171	#
172	echo export _SYSRC_JAILED=1
173
174	#
175	# Print i18n language variables (their current values are sanitized
176	# and re-printed for interpretation so that the i18n language files
177	# do not need to exist within the jail).
178	#
179	local var val
180	for var in \
181		msg_cannot_create_permission_denied \
182		msg_permission_denied \
183		msg_previous_syntax_errors \
184	; do
185		val=$( eval echo \"\$$var\" |
186			awk '{ gsub(/'\''/, "'\''\\'\'\''"); print }' )
187		echo $var="'$val'"
188	done
189
190	#
191	# Print include dependencies
192	#
193	echo DEBUG_SELF_INITIALIZE=
194	cat $BSDCFG_SHARE/common.subr
195	cat $BSDCFG_SHARE/sysrc.subr
196}
197
198############################################################ MAIN SOURCE
199
200#
201# Perform sanity checks
202#
203[ $# -gt 0 ] || usage
204
205#
206# Check for `--help' and `--version' command-line option
207#
208( # Operate in sub-shell to protect $@ in parent
209	while [ $# -gt 0 ]; do
210		case "$1" in
211		--help) help ;;
212		--version) # see GLOBALS
213			echo "$SYSRC_VERSION"
214			exit 1 ;;
215		-[fRj]) # These flags take an argument
216			shift 1 ;;
217		esac
218		shift 1
219	done
220	exit 0
221) || die
222
223#
224# Process command-line flags
225#
226while getopts aAcdDef:Fhij:nNqR:vxX flag; do
227	case "$flag" in
228	a) SHOW_ALL=${SHOW_ALL:-1};;
229	A) SHOW_ALL=2;;
230	c) CHECK_ONLY=1;;
231	d) DESCRIBE=1;;
232	D) RC_CONFS=;;
233	e) SHOW_EQUALS=1;;
234	f) RC_CONFS="$RC_CONFS${RC_CONFS:+ }$OPTARG";;
235	F) SHOW_FILE=1;;
236	h) usage;;
237	i) IGNORE_UNKNOWNS=1;;
238	j) [ "$OPTARG" ] || die \
239	   	"%s: Missing or null argument to \`-j' flag" "$pgm"
240	   JAIL="$OPTARG";;
241	n) SHOW_NAME=;;
242	N) SHOW_VALUE=;;
243	q) QUIET=1 SYSRC_VERBOSE=;;
244	R) [ "$OPTARG" ] || die \
245	   	"%s: Missing or null argument to \`-R' flag" "$pgm"
246	   ROOTDIR="$OPTARG";;
247	v) SYSRC_VERBOSE=1 QUIET=;;
248	x) DELETE=${DELETE:-1};;
249	X) DELETE=2;;
250	\?) usage;;
251	esac
252done
253shift $(( $OPTIND - 1 ))
254
255#
256# [More] Sanity checks (e.g., "sysrc --")
257#
258[ $# -eq 0 -a ! "$SHOW_ALL" ] && usage
259
260#
261# Taint-check all rc.conf(5) files
262#
263errmsg="$pgm: Exiting due to previous syntax errors"
264if [ "${RC_CONFS+set}" ]; then
265	( for i in $RC_CONFS; do
266	  	[ -e "$i" ] || continue
267	  	/bin/sh -n "$i" || exit $FAILURE
268	  done
269	  exit $SUCCESS
270	) || die "$errmsg"
271else
272	/bin/sh -n "$RC_DEFAULTS" || die "$errmsg"
273	( . "$RC_DEFAULTS"
274	  for i in $rc_conf_files; do
275	  	[ -e "$i" ] || continue
276	  	/bin/sh -n "$i" || exit $FAILURE
277	  done
278	  exit $SUCCESS
279	) || die "$errmsg"
280fi
281
282#
283# Process `-x' (and secret `-X') command-line options
284#
285errmsg="$pgm: \`-x' option incompatible with \`-a'/\`-A' options"
286errmsg="$errmsg (use \`-X' to override)"
287if [ "$DELETE" -a "$SHOW_ALL" ]; then
288	[ "$DELETE" = "2" ] || die "$errmsg"
289fi
290
291#
292# Pre-flight for `-c' command-line option
293#
294[ "$CHECK_ONLY" -a "$SHOW_ALL" ] &&
295	die "$pgm: \`-c' option incompatible with \`-a'/\`-A' options"
296
297#
298# Process `-e', `-n', and `-N' command-line options
299#
300SEP=': '
301[ "$SHOW_FILE" ] && SHOW_EQUALS=
302[ "$SHOW_NAME" ] || SHOW_EQUALS=
303[ "$SYSRC_VERBOSE" = "0" ] && SYSRC_VERBOSE=
304if [ ! "$SHOW_VALUE" ]; then
305	SHOW_NAME=1
306	SHOW_EQUALS=
307fi
308[ "$SHOW_EQUALS" ] && SEP='="'
309
310#
311# Process `-j jail' and `-R dir' command-line options
312#
313if [ "$JAIL" -o "$ROOTDIR" ]; then
314	#
315	# Reconstruct the arguments that we want to carry-over
316	#
317	args="
318		${SYSRC_VERBOSE:+-v}
319		${QUIET:+-q}
320		$( [ "$DELETE" = "1" ] && echo \ -x )
321		$( [ "$DELETE" = "2" ] && echo \ -X )
322		$( [ "$SHOW_ALL" = "1" ] && echo \ -a )
323		$( [ "$SHOW_ALL" = "2" ] && echo \ -A )
324		${CHECK_ONLY:+-c}
325		${DESCRIBE:+-d}
326		${SHOW_EQUALS:+-e}
327		${IGNORE_UNKNOWNS:+-i}
328		$( [ "$SHOW_NAME"  ] || echo \ -n )
329		$( [ "$SHOW_VALUE" ] || echo \ -N )
330		$( [ "$SHOW_FILE"  ] && echo \ -F )
331	"
332	if [ "${RC_CONFS+set}" ]; then
333		args="$args -f '$RC_CONFS'"
334	fi
335	for arg in "$@"; do
336		args="$args '$arg'"
337	done
338
339	#
340	# If both are supplied, `-j jail' supercedes `-R dir'
341	#
342	if [ "$JAIL" ]; then
343		#
344		# Re-execute ourselves with sh(1) via jexec(8)
345		#
346		( echo set -- $args
347		  jail_depend
348		  cat $0
349		) | env - RC_DEFAULTS="$RC_DEFAULTS" \
350		    	/usr/sbin/jexec "$JAIL" /bin/sh
351		exit $?
352	elif [ "$ROOTDIR" ]; then
353		#
354		# Make sure that the root directory specified is not to any
355		# running jails.
356		#
357		# NOTE: To maintain backward compatibility with older jails on
358		# older systems, we will not perform this check if either the
359		# jls(1) or jexec(8) utilities are missing.
360		#
361		if f_have jexec && f_have jls; then
362			jid="`jls jid path | \
363			(
364				while read JID JROOT; do
365					[ "$JROOT" = "$ROOTDIR" ] || continue
366					echo $JID
367				done
368			)`"
369
370			#
371			# If multiple running jails match the specified root
372			# directory, exit with error.
373			#
374			if [ "$jid" -a "${jid%[$IFS]*}" != "$jid" ]; then
375				die "%s: %s: %s" "$pgm" "$ROOTDIR" \
376				    "$( echo "Multiple jails claim this" \
377				             "directory as their root." \
378				             "(use \`-j jail' instead)" )"
379			fi
380
381			#
382			# If only a single running jail matches the specified
383			# root directory, implicitly use `-j jail'.
384			#
385			if [ "$jid" ]; then
386				#
387				# Re-execute outselves with sh(1) via jexec(8)
388				#
389				( echo set -- $args
390				  jail_depend
391				  cat $0
392				) | env - RC_DEFAULTS="$RC_DEFAULTS" \
393					/usr/sbin/jexec "$jid" /bin/sh
394				exit $?
395			fi
396
397			# Otherwise, fall through and allow chroot(8)
398		fi
399
400		#
401		# Re-execute ourselves with sh(1) via chroot(8)
402		#
403		( echo set -- $args
404		  jail_depend
405		  cat $0
406		) | env - RC_DEFAULTS="$RC_DEFAULTS" \
407		    	/usr/sbin/chroot "$ROOTDIR" /bin/sh
408		exit $?
409	fi
410fi
411
412#
413# Process `-a' or `-A' command-line options
414#
415if [ "$SHOW_ALL" ]; then
416	#
417	# Get a list of variables that are currently set in the rc.conf(5)
418	# files (included `/etc/defaults/rc.conf') by performing a call to
419	# source_rc_confs() in a clean environment.
420	#
421	( # Operate in a sub-shell to protect the parent environment
422		#
423		# Set which variables we want to preserve in the environment.
424		# Append the pipe-character (|) to the list of internal field
425		# separation (IFS) characters, allowing us to use the below
426		# list both as an extended grep (-E) pattern and argument list
427		# (required to first get f_clean_env() to preserve these in the
428		# environment and then later to prune them from the list of
429		# variables produced by set(1)).
430		#
431		IFS="$IFS|"
432		EXCEPT="IFS|EXCEPT|PATH|RC_DEFAULTS|OPTIND|DESCRIBE|SEP"
433		EXCEPT="$EXCEPT|DELETE|SHOW_ALL|SHOW_EQUALS|SHOW_NAME"
434		EXCEPT="$EXCEPT|SHOW_VALUE|SHOW_FILE|SYSRC_VERBOSE|RC_CONFS"
435		EXCEPT="$EXCEPT|pgm|SUCCESS|FAILURE|CHECK_ONLY"
436		EXCEPT="$EXCEPT|f_sysrc_desc_awk|f_sysrc_delete_awk"
437
438		#
439		# Clean the environment (except for our required variables)
440		# and then source the required files.
441		#
442		f_clean_env --except $EXCEPT
443		if [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ]; then
444			. "$RC_DEFAULTS"
445
446			#
447			# If passed `-a' (rather than `-A'), re-purge the
448			# environment, removing the rc.conf(5) defaults.
449			#
450			[ "$SHOW_ALL" = "1" ] \
451				&& f_clean_env --except rc_conf_files $EXCEPT
452
453			#
454			# If `-f file' was passed, set $rc_conf_files to an
455			# explicit value, modifying the default behavior of
456			# source_rc_confs().
457			#
458			[ "${RC_CONFS+set}" ] && rc_conf_files="$RC_CONFS"
459
460			source_rc_confs
461
462			#
463			# If passed `-a' (rather than `-A'), remove
464			# `rc_conf_files' unless it was defined somewhere
465			# other than rc.conf(5) defaults.
466			#
467			[ "$SHOW_ALL" = "1" -a \
468			  "$( f_sysrc_find rc_conf_files )" = "$RC_DEFAULTS" \
469			] \
470			&& unset rc_conf_files
471		fi
472
473		for NAME in $( set |
474			awk -F= '/^[[:alpha:]_][[:alnum:]_]*=/ {print $1}' |
475			grep -Ev "^($EXCEPT)$"
476		); do
477			#
478			# If enabled, describe rather than expand value
479			#
480			if [ "$DESCRIBE" ]; then
481				echo "$NAME: $( f_sysrc_desc "$NAME" )"
482				continue
483			fi
484
485			#
486			# If `-F' is passed, find it and move on
487			#
488			if [ "$SHOW_FILE" ]; then
489				[ "$SHOW_NAME" ] && echo -n "$NAME: "
490				f_sysrc_find "$NAME"
491				continue
492			fi
493
494			#
495			# If `-X' is passed, delete the variables
496			#
497			if [ "$DELETE" = "2" ]; then
498				f_sysrc_delete "$NAME"
499				continue
500			fi
501
502			[ "$SYSRC_VERBOSE" ] && \
503				echo -n "$( f_sysrc_find "$NAME" ): "
504
505			#
506			# If `-N' is passed, simplify the output
507			#
508			if [ ! "$SHOW_VALUE" ]; then
509				echo "$NAME"
510				continue
511			fi
512
513			echo "${SHOW_NAME:+$NAME$SEP}$(
514			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
515
516		done
517	)
518
519	#
520	# Ignore the remainder of positional arguments.
521	#
522	exit $SUCCESS
523fi
524
525#
526# Process command-line arguments
527#
528status=$SUCCESS
529while [ $# -gt 0 ]; do
530	NAME="${1%%=*}"
531
532	[ "$DESCRIBE" ] && \
533		echo "$NAME: $( f_sysrc_desc "$NAME" )"
534
535	case "$1" in
536	*=*)
537		#
538		# Like sysctl(8), if both `-d' AND "name=value" is passed,
539		# first describe (done above), then attempt to set
540		#
541
542		# If verbose, prefix line with where the directive lives
543		if [ "$SYSRC_VERBOSE" -a ! "$CHECK_ONLY" ]; then
544			file=$( f_sysrc_find "$NAME" )
545			[ "$file" = "$RC_DEFAULTS" -o ! "$file" ] && \
546				file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
547			if [ "$SHOW_EQUALS" ]; then
548				echo -n ": $file; "
549			else
550				echo -n "$file: "
551			fi
552		fi
553
554		#
555		# If `-x' or `-X' is passed, delete the variable and ignore the
556		# desire to set some value
557		#
558		if [ "$DELETE" ]; then
559			f_sysrc_delete "$NAME" || status=$FAILURE
560			shift 1
561			continue
562		fi
563
564		#
565		# If `-c' is passed, simply compare and move on
566		#
567		if [ "$CHECK_ONLY" ]; then
568			if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
569				status=$FAILURE
570				[ "$SYSRC_VERBOSE" ] &&
571					echo "$NAME: not currently set"
572				shift 1
573				continue
574			fi
575			value=$( f_sysrc_get "$NAME" )
576			if [ "$value" != "${1#*=}" ]; then
577				status=$FAILURE
578				if [ "$SYSRC_VERBOSE" ]; then
579					echo -n "$( f_sysrc_find "$NAME" ): "
580					echo -n "$NAME: would change from "
581					echo "\`$value' to \`${1#*=}'"
582				fi
583			elif [ "$SYSRC_VERBOSE" ]; then
584				echo -n "$( f_sysrc_find "$NAME" ): "
585				echo "$NAME: already set to \`$value'"
586			fi
587			shift 1
588			continue
589		fi
590
591		#
592		# If `-N' is passed, simplify the output
593		#
594		if [ ! "$SHOW_VALUE" ]; then
595			echo "$NAME"
596			f_sysrc_set "$NAME" "${1#*}"
597		else
598			if [ "$SHOW_FILE" ]; then
599				before=$( f_sysrc_find "$NAME" )
600			else
601				before=$( f_sysrc_get "$NAME" )
602			fi
603			if f_sysrc_set "$NAME" "${1#*=}"; then
604				if [ "$SHOW_FILE" ]; then
605					after=$( f_sysrc_find "$NAME" )
606				else
607					after=$( f_sysrc_get "$NAME" )
608				fi
609				echo -n "${SHOW_NAME:+$NAME$SEP}"
610				echo -n "$before${SHOW_EQUALS:+\" #}"
611				echo -n " -> ${SHOW_EQUALS:+\"}$after"
612				echo "${SHOW_EQUALS:+\"}"
613			fi
614		fi
615		;;
616	*)
617		if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
618			[ "$IGNORE_UNKNOWNS" -o "$QUIET" ] ||
619				echo "$pgm: unknown variable '$NAME'"
620			shift 1
621			status=$FAILURE
622			continue
623		fi
624
625		# The above check told us what we needed for `-c'
626		if [ "$CHECK_ONLY" ]; then
627			shift 1
628			continue
629		fi
630
631		#
632		# Like sysctl(8), when `-d' is passed, desribe it
633		# (already done above) rather than expanding it
634		#
635
636		if [ "$DESCRIBE" ]; then
637			shift 1
638			continue
639		fi
640
641		#
642		# If `-x' or `-X' is passed, delete the variable
643		#
644		if [ "$DELETE" ]; then
645			f_sysrc_delete "$NAME" || status=$FAILURE
646			shift 1
647			continue
648		fi
649
650		#
651		# If `-F' is passed, find it and move on
652		#
653		if [ "$SHOW_FILE" ]; then
654			[ "$SHOW_NAME" ] && echo -n "$NAME: "
655			f_sysrc_find "$NAME"
656			shift 1
657			continue
658		fi
659
660		if [ "$SYSRC_VERBOSE" ]; then
661			if [ "$SHOW_EQUALS" ]; then
662				echo -n ": $( f_sysrc_find "$NAME" ); "
663			else
664				echo -n "$( f_sysrc_find "$NAME" ): "
665			fi
666		fi
667
668		#
669		# If `-N' is passed, simplify the output
670		#
671		if [ ! "$SHOW_VALUE" ]; then
672			echo "$NAME"
673		else
674			echo "${SHOW_NAME:+$NAME$SEP}$(
675			      f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
676		fi
677	esac
678	shift 1
679done
680
681exit $status # $SUCCESS unless error occurred with either `-c' or `-x'
682
683################################################################################
684# END
685################################################################################
686