3 # Copyright (c) 2010-2012 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 [ "$_COMMON_SUBR" ] || . $BSDCFG_SHARE/common.subr || exit 1
33 [ "$_SYSRC_SUBR" ] || f_include $BSDCFG_SHARE/sysrc.subr
35 ############################################################ CONFIGURATION
42 ############################################################ GLOBALS
59 ############################################################ FUNCTIONS
61 # die [ $fmt [ $opts ... ]]
63 # Optionally print a message to stderr before exiting with failure status.
68 [ $# -gt 0 ] && shift 1
69 [ "$fmt" ] && f_err "$fmt\n" "$@"
76 # Prints a short syntax statement and exits.
80 f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm"
81 f_err "Try \`%s --help' for more information.\n" "$pgm"
87 # Prints a full syntax statement and exits.
91 local optfmt="\t%-11s%s\n"
92 local envfmt="\t%-17s%s\n"
94 f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm"
97 f_err "$optfmt" "-a" \
98 "Dump a list of all non-default configuration variables."
99 f_err "$optfmt" "-A" \
100 "Dump a list of all configuration variables (incl. defaults)."
101 f_err "$optfmt" "-d" \
102 "Print a description of the given variable."
103 f_err "$optfmt" "-D" \
104 "Show default value(s) only (this is the same as setting"
106 "RC_CONFS to NULL or passing \`-f' with a NULL file-argument)."
107 f_err "$optfmt" "-e" \
108 "Print query results as \`var=value' (useful for producing"
110 "output to be fed back in). Ignored if \`-n' is specified."
111 f_err "$optfmt" "-f file" \
112 "Operate on the specified file(s) instead of rc_conf_files."
114 "Can be specified multiple times for additional files."
115 f_err "$optfmt" "-F" \
116 "Show only the last rc.conf(5) file each directive is in."
117 f_err "$optfmt" "-h" \
118 "Print a short usage statement to stderr and exit."
119 f_err "$optfmt" "--help" \
120 "Print this message to stderr and exit."
121 f_err "$optfmt" "-i" \
122 "Ignore unknown variables."
123 f_err "$optfmt" "-j jail" \
124 "The jid or name of the jail to operate within (overrides"
126 "\`-R dir'; requires jexec(8))."
127 f_err "$optfmt" "-n" \
128 "Show only variable values, not their names."
129 f_err "$optfmt" "-N" \
130 "Show only variable names, not their values."
131 f_err "$optfmt" "-q" \
132 "Quiet. Ignore previous \`-v' and/or SYSRC_VERBOSE."
133 f_err "$optfmt" "-R dir" \
134 "Operate within the root directory \`dir' rather than \`/'."
135 f_err "$optfmt" "-v" \
136 "Verbose. Print the pathname of the specific rc.conf(5)"
138 "file where the directive was found."
139 f_err "$optfmt" "-x" \
140 "Remove variable(s) from specified file(s)."
143 f_err "ENVIRONMENT:\n"
144 f_err "$envfmt" "RC_CONFS" \
145 "Override default rc_conf_files (even if set to NULL)."
146 f_err "$envfmt" "RC_DEFAULTS" \
147 "Location of \`/etc/defaults/rc.conf' file."
148 f_err "$envfmt" "SYSRC_VERBOSE" \
149 "Default verbosity. Set to non-NULL to enable."
156 # Dump dependencies such as language-file variables and include files to stdout
157 # to be piped-into sh(1) running via jexec(8)/chroot(8). As a security measure,
158 # this prevents existing language files and library files from being loaded in
159 # the jail. This also relaxes the requirement to have these files in every jail
160 # before sysrc can be used on said jail.
165 # Indicate that we are jailed
167 echo export _SYSRC_JAILED=1
170 # Print i18n language variables (their current values are sanitized
171 # and re-printed for interpretation so that the i18n language files
172 # do not need to exist within the jail).
176 msg_cannot_create_permission_denied \
177 msg_permission_denied \
178 msg_previous_syntax_errors \
180 val=$( eval echo \"\$$var\" |
181 awk '{ gsub(/'\''/, "'\''\\'\'\''"); print }' )
186 # Print include dependencies
188 cat $BSDCFG_SHARE/common.subr
189 cat $BSDCFG_SHARE/sysrc.subr
192 ############################################################ MAIN SOURCE
195 # Perform sanity checks
197 [ $# -gt 0 ] || usage
200 # Check for `--help' command-line option
202 ( # Operate in sub-shell to protect $@ in parent
203 while [ $# -gt 0 ]; do
206 -[fRj]) # These flags take an argument
215 # Process command-line flags
217 while getopts aAdDef:Fhij:nNqR:vxX flag; do
219 a) SHOW_ALL=${SHOW_ALL:-1};;
224 f) RC_CONFS="$RC_CONFS${RC_CONFS:+ }$OPTARG";;
227 i) IGNORE_UNKNOWNS=1;;
228 j) [ "$OPTARG" ] || die \
229 "%s: Missing or null argument to \`-j' flag" "$pgm"
233 q) QUIET=1 SYSRC_VERBOSE=;;
234 R) [ "$OPTARG" ] || die \
235 "%s: Missing or null argument to \`-R' flag" "$pgm"
237 v) SYSRC_VERBOSE=1 QUIET=;;
238 x) DELETE=${DELETE:-1};;
243 shift $(( $OPTIND - 1 ))
246 # [More] Sanity checks (e.g., "sysrc --")
248 [ $# -eq 0 -a ! "$SHOW_ALL" ] && usage
251 # Taint-check all rc.conf(5) files
253 errmsg="$pgm: Exiting due to previous syntax errors"
254 if [ "${RC_CONFS+set}" ]; then
255 ( for i in $RC_CONFS; do
256 [ -e "$i" ] || continue
257 /bin/sh -n "$i" || exit $FAILURE
262 /bin/sh -n "$RC_DEFAULTS" || die "$errmsg"
264 for i in $rc_conf_files; do
265 [ -e "$i" ] || continue
266 /bin/sh -n "$i" || exit $FAILURE
273 # Process `-x' (and secret `-X') command-line options
275 errmsg="$pgm: \`-x' option incompatible with \`-a'/\`-A' options"
276 errmsg="$errmsg (use \`-X' to override)"
277 if [ "$DELETE" -a "$SHOW_ALL" ]; then
278 [ "$DELETE" = "2" ] || die "$errmsg"
282 # Process `-e', `-n', and `-N' command-line options
285 [ "$SHOW_EQUALS" ] && SEP='="'
286 [ "$SHOW_NAME" ] || SHOW_EQUALS=
287 [ "$SYSRC_VERBOSE" = "0" ] && SYSRC_VERBOSE=
288 if [ ! "$SHOW_VALUE" ]; then
294 # Process `-j jail' and `-R dir' command-line options
296 if [ "$JAIL" -o "$ROOTDIR" ]; then
298 # Reconstruct the arguments that we want to carry-over
303 $( [ "$DELETE" = "1" ] && echo \ -x )
304 $( [ "$DELETE" = "2" ] && echo \ -X )
305 $( [ "$SHOW_ALL" = "1" ] && echo \ -a )
306 $( [ "$SHOW_ALL" = "2" ] && echo \ -A )
309 ${IGNORE_UNKNOWNS:+-i}
310 $( [ "$SHOW_NAME" ] || echo \ -n )
311 $( [ "$SHOW_VALUE" ] || echo \ -N )
312 $( [ "$SHOW_FILE" ] && echo \ -F )
314 if [ "${RC_CONFS+set}" ]; then
315 args="$args -f '$RC_CONFS'"
322 # If both are supplied, `-j jail' supercedes `-R dir'
326 # Re-execute ourselves with sh(1) via jexec(8)
331 ) | env - RC_DEFAULTS="$RC_DEFAULTS" \
332 /usr/sbin/jexec "$JAIL" /bin/sh
334 elif [ "$ROOTDIR" ]; then
336 # Make sure that the root directory specified is not to any
339 # NOTE: To maintain backward compatibility with older jails on
340 # older systems, we will not perform this check if either the
341 # jls(1) or jexec(8) utilities are missing.
343 if f_have jexec && f_have jls; then
344 jid="`jls jid path | \
346 while read JID JROOT; do
347 [ "$JROOT" = "$ROOTDIR" ] || continue
353 # If multiple running jails match the specified root
354 # directory, exit with error.
356 if [ "$jid" -a "${jid%[$IFS]*}" != "$jid" ]; then
357 die "%s: %s: %s" "$pgm" "$ROOTDIR" \
358 "$( echo "Multiple jails claim this" \
359 "directory as their root." \
360 "(use \`-j jail' instead)" )"
364 # If only a single running jail matches the specified
365 # root directory, implicitly use `-j jail'.
369 # Re-execute outselves with sh(1) via jexec(8)
374 ) | env - RC_DEFAULTS="$RC_DEFAULTS" \
375 /usr/sbin/jexec "$jid" /bin/sh
379 # Otherwise, fall through and allow chroot(8)
383 # Re-execute ourselves with sh(1) via chroot(8)
388 ) | env - RC_DEFAULTS="$RC_DEFAULTS" \
389 /usr/sbin/chroot "$ROOTDIR" /bin/sh
395 # Process `-a' or `-A' command-line options
397 if [ "$SHOW_ALL" ]; then
399 # Get a list of variables that are currently set in the rc.conf(5)
400 # files (included `/etc/defaults/rc.conf') by performing a call to
401 # source_rc_confs() in a clean environment.
403 ( # Operate in a sub-shell to protect the parent environment
405 # Set which variables we want to preserve in the environment.
406 # Append the pipe-character (|) to the list of internal field
407 # separation (IFS) characters, allowing us to use the below
408 # list both as an extended grep (-E) pattern and argument list
409 # (required to first get f_clean_env() to preserve these in the
410 # environment and then later to prune them from the list of
411 # variables produced by set(1)).
414 EXCEPT="IFS|EXCEPT|PATH|RC_DEFAULTS|OPTIND|DESCRIBE|SEP"
415 EXCEPT="$EXCEPT|DELETE|SHOW_ALL|SHOW_EQUALS|SHOW_NAME"
416 EXCEPT="$EXCEPT|SHOW_VALUE|SHOW_FILE|SYSRC_VERBOSE|RC_CONFS"
417 EXCEPT="$EXCEPT|pgm|SUCCESS|FAILURE"
418 EXCEPT="$EXCEPT|f_sysrc_desc_awk|f_sysrc_delete_awk"
421 # Clean the environment (except for our required variables)
422 # and then source the required files.
424 f_clean_env --except $EXCEPT
425 if [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ]; then
429 # If passed `-a' (rather than `-A'), re-purge the
430 # environment, removing the rc.conf(5) defaults.
432 [ "$SHOW_ALL" = "1" ] \
433 && f_clean_env --except rc_conf_files $EXCEPT
436 # If `-f file' was passed, set $rc_conf_files to an
437 # explicit value, modifying the default behavior of
440 [ "${RC_CONFS+set}" ] && rc_conf_files="$RC_CONFS"
445 # If passed `-a' (rather than `-A'), remove
446 # `rc_conf_files' unless it was defined somewhere
447 # other than rc.conf(5) defaults.
449 [ "$SHOW_ALL" = "1" -a \
450 "$( f_sysrc_find rc_conf_files )" = "$RC_DEFAULTS" \
452 && unset rc_conf_files
456 awk -F= '/^[[:alpha:]_][[:alnum:]_]*=/ {print $1}' |
457 grep -Ev "^($EXCEPT)$"
460 # If enabled, describe rather than expand value
462 if [ "$DESCRIBE" ]; then
463 echo "$NAME: $( f_sysrc_desc "$NAME" )"
468 # If `-F' is passed, find it and move on
470 if [ "$SHOW_FILE" ]; then
471 [ "$SHOW_NAME" ] && echo -n "$NAME: "
477 # If `-X' is passed, delete the variables
479 if [ "$DELETE" = "2" ]; then
480 f_sysrc_delete "$NAME"
484 [ "$SYSRC_VERBOSE" ] && \
485 echo -n "$( f_sysrc_find "$NAME" ): "
488 # If `-N' is passed, simplify the output
490 if [ ! "$SHOW_VALUE" ]; then
495 echo "${SHOW_NAME:+$NAME$SEP}$(
496 f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
502 # Ignore the remainder of positional arguments.
508 # Process command-line arguments
510 while [ $# -gt 0 ]; do
514 echo "$NAME: $( f_sysrc_desc "$NAME" )"
519 # Like sysctl(8), if both `-d' AND "name=value" is passed,
520 # first describe, then attempt to set
523 if [ "$SYSRC_VERBOSE" ]; then
524 file=$( f_sysrc_find "$NAME" )
525 [ "$file" = "$RC_DEFAULTS" -o ! "$file" ] && \
526 file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
531 # If `-x' or `-X' is passed, delete the variable and ignore the
532 # desire to set some value
534 if [ "$DELETE" ]; then
535 f_sysrc_delete "$NAME"
541 # If `-N' is passed, simplify the output
543 if [ ! "$SHOW_VALUE" ]; then
545 f_sysrc_set "$NAME" "${1#*}"
547 if [ "$SHOW_FILE" ]; then
548 before=$( f_sysrc_find "$NAME" )
550 before=$( f_sysrc_get "$NAME" )
552 if f_sysrc_set "$NAME" "${1#*=}"; then
553 if [ "$SHOW_FILE" ]; then
554 after=$( f_sysrc_find "$NAME" )
555 echo -n "${SHOW_NAME:+$NAME$SEP}"
556 echo -n "$before${SHOW_EQUALS:+\"}"
559 after=$( f_sysrc_get "$NAME" )
560 echo -n "${SHOW_NAME:+$NAME$SEP}"
561 echo "$before -> $after"
567 if ! IGNORED="$( f_sysrc_get "$NAME?" )"; then
568 [ "$IGNORE_UNKNOWNS" ] \
569 || echo "$pgm: unknown variable '$NAME'"
575 # Like sysctl(8), when `-d' is passed,
576 # desribe it rather than expanding it
579 if [ "$DESCRIBE" ]; then
585 # If `-x' or `-X' is passed, delete the variable
587 if [ "$DELETE" ]; then
588 f_sysrc_delete "$NAME"
594 # If `-F' is passed, find it and move on
596 if [ "$SHOW_FILE" ]; then
597 [ "$SHOW_NAME" ] && echo -n "$NAME: "
603 [ "$SYSRC_VERBOSE" ] && \
604 echo -n "$( f_sysrc_find "$NAME" ): "
607 # If `-N' is passed, simplify the output
609 if [ ! "$SHOW_VALUE" ]; then
612 echo "${SHOW_NAME:+$NAME$SEP}$(
613 f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"