3 # Copyright (c) 2010-2013 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 # Prevent `-d' from being interpreted as a debug flag by common.subr
32 DEBUG_SELF_INITIALIZE=
34 BSDCFG_SHARE="/usr/share/bsdconfig"
35 [ "$_COMMON_SUBR" ] || . $BSDCFG_SHARE/common.subr || exit 1
36 [ "$_SYSRC_SUBR" ] || f_include $BSDCFG_SHARE/sysrc.subr
38 ############################################################ GLOBALS
43 SYSRC_VERSION="6.0 Nov-07,2013"
62 ############################################################ FUNCTIONS
64 # die [ $fmt [ $opts ... ]]
66 # Optionally print a message to stderr before exiting with failure status.
71 [ $# -gt 0 ] && shift 1
72 [ "$fmt" ] && f_err "$fmt\n" "$@"
79 # Prints a short syntax statement and exits.
83 f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm"
84 f_err "Try \`%s --help' for more information.\n" "$pgm"
90 # Prints a full syntax statement and exits.
94 local optfmt="\t%-11s%s\n"
95 local envfmt="\t%-17s%s\n"
97 f_err "Usage: %s [OPTIONS] name[=value] ...\n" "$pgm"
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 no changes needed, 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"
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"
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."
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"
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. Ignore previous \`-v' and/or SYSRC_VERBOSE."
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)"
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)."
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 f_err "$envfmt" "SYSRC_VERBOSE" \
156 "Default verbosity. Set to non-NULL to enable."
163 # Dump dependencies such as language-file variables and include files to stdout
164 # to be piped-into sh(1) running via jexec(8)/chroot(8). As a security measure,
165 # this prevents existing language files and library files from being loaded in
166 # the jail. This also relaxes the requirement to have these files in every jail
167 # before sysrc can be used on said jail.
172 # Indicate that we are jailed
174 echo export _SYSRC_JAILED=1
177 # Print i18n language variables (their current values are sanitized
178 # and re-printed for interpretation so that the i18n language files
179 # do not need to exist within the jail).
183 msg_cannot_create_permission_denied \
184 msg_permission_denied \
185 msg_previous_syntax_errors \
187 val=$( eval echo \"\$$var\" |
188 awk '{ gsub(/'\''/, "'\''\\'\'\''"); print }' )
193 # Print include dependencies
195 echo DEBUG_SELF_INITIALIZE=
196 cat $BSDCFG_SHARE/common.subr
197 cat $BSDCFG_SHARE/sysrc.subr
200 ############################################################ MAIN SOURCE
203 # Perform sanity checks
205 [ $# -gt 0 ] || usage
208 # Check for `--help' and `--version' command-line option
210 ( # Operate in sub-shell to protect $@ in parent
211 while [ $# -gt 0 ]; do
214 --version) # see GLOBALS
215 echo "$SYSRC_VERSION"
217 -[fRj]) # These flags take an argument
226 # Process command-line flags
228 while getopts aAcdDef:Fhij:nNqR:vxX flag; do
230 a) SHOW_ALL=${SHOW_ALL:-1};;
236 f) RC_CONFS="$RC_CONFS${RC_CONFS:+ }$OPTARG";;
239 i) IGNORE_UNKNOWNS=1;;
240 j) [ "$OPTARG" ] || die \
241 "%s: Missing or null argument to \`-j' flag" "$pgm"
245 q) QUIET=1 SYSRC_VERBOSE=;;
246 R) [ "$OPTARG" ] || die \
247 "%s: Missing or null argument to \`-R' flag" "$pgm"
249 v) SYSRC_VERBOSE=1 QUIET=;;
250 x) DELETE=${DELETE:-1};;
255 shift $(( $OPTIND - 1 ))
258 # [More] Sanity checks (e.g., "sysrc --")
260 [ $# -eq 0 -a ! "$SHOW_ALL" ] && usage
263 # Taint-check all rc.conf(5) files
265 errmsg="$pgm: Exiting due to previous syntax errors"
266 if [ "${RC_CONFS+set}" ]; then
267 ( for i in $RC_CONFS; do
268 [ -e "$i" ] || continue
269 /bin/sh -n "$i" || exit $FAILURE
274 /bin/sh -n "$RC_DEFAULTS" || die "$errmsg"
276 for i in $rc_conf_files; do
277 [ -e "$i" ] || continue
278 /bin/sh -n "$i" || exit $FAILURE
285 # Process `-x' (and secret `-X') command-line options
287 errmsg="$pgm: \`-x' option incompatible with \`-a'/\`-A' options"
288 errmsg="$errmsg (use \`-X' to override)"
289 if [ "$DELETE" -a "$SHOW_ALL" ]; then
290 [ "$DELETE" = "2" ] || die "$errmsg"
294 # Pre-flight for `-c' command-line option
296 [ "$CHECK_ONLY" -a "$SHOW_ALL" ] &&
297 die "$pgm: \`-c' option incompatible with \`-a'/\`-A' options"
300 # Process `-e', `-n', and `-N' command-line options
303 [ "$SHOW_FILE" ] && SHOW_EQUALS=
304 [ "$SHOW_NAME" ] || SHOW_EQUALS=
305 [ "$SYSRC_VERBOSE" = "0" ] && SYSRC_VERBOSE=
306 if [ ! "$SHOW_VALUE" ]; then
310 [ "$SHOW_EQUALS" ] && SEP='="'
313 # Process `-j jail' and `-R dir' command-line options
315 if [ "$JAIL" -o "$ROOTDIR" ]; then
317 # Reconstruct the arguments that we want to carry-over
322 $( [ "$DELETE" = "1" ] && echo \ -x )
323 $( [ "$DELETE" = "2" ] && echo \ -X )
324 $( [ "$SHOW_ALL" = "1" ] && echo \ -a )
325 $( [ "$SHOW_ALL" = "2" ] && echo \ -A )
329 ${IGNORE_UNKNOWNS:+-i}
330 $( [ "$SHOW_NAME" ] || echo \ -n )
331 $( [ "$SHOW_VALUE" ] || echo \ -N )
332 $( [ "$SHOW_FILE" ] && echo \ -F )
334 if [ "${RC_CONFS+set}" ]; then
335 args="$args -f '$RC_CONFS'"
342 # If both are supplied, `-j jail' supercedes `-R dir'
346 # Re-execute ourselves with sh(1) via jexec(8)
351 ) | env - RC_DEFAULTS="$RC_DEFAULTS" \
352 /usr/sbin/jexec "$JAIL" /bin/sh
354 elif [ "$ROOTDIR" ]; then
356 # Make sure that the root directory specified is not to any
359 # NOTE: To maintain backward compatibility with older jails on
360 # older systems, we will not perform this check if either the
361 # jls(1) or jexec(8) utilities are missing.
363 if f_have jexec && f_have jls; then
364 jid="`jls jid path | \
366 while read JID JROOT; do
367 [ "$JROOT" = "$ROOTDIR" ] || continue
373 # If multiple running jails match the specified root
374 # directory, exit with error.
376 if [ "$jid" -a "${jid%[$IFS]*}" != "$jid" ]; then
377 die "%s: %s: %s" "$pgm" "$ROOTDIR" \
378 "$( echo "Multiple jails claim this" \
379 "directory as their root." \
380 "(use \`-j jail' instead)" )"
384 # If only a single running jail matches the specified
385 # root directory, implicitly use `-j jail'.
389 # Re-execute outselves with sh(1) via jexec(8)
394 ) | env - RC_DEFAULTS="$RC_DEFAULTS" \
395 /usr/sbin/jexec "$jid" /bin/sh
399 # Otherwise, fall through and allow chroot(8)
403 # Re-execute ourselves with sh(1) via chroot(8)
408 ) | env - RC_DEFAULTS="$RC_DEFAULTS" \
409 /usr/sbin/chroot "$ROOTDIR" /bin/sh
415 # Process `-a' or `-A' command-line options
417 if [ "$SHOW_ALL" ]; then
419 # Get a list of variables that are currently set in the rc.conf(5)
420 # files (included `/etc/defaults/rc.conf') by performing a call to
421 # source_rc_confs() in a clean environment.
423 ( # Operate in a sub-shell to protect the parent environment
425 # Set which variables we want to preserve in the environment.
426 # Append the pipe-character (|) to the list of internal field
427 # separation (IFS) characters, allowing us to use the below
428 # list both as an extended grep (-E) pattern and argument list
429 # (required to first get f_clean_env() to preserve these in the
430 # environment and then later to prune them from the list of
431 # variables produced by set(1)).
434 EXCEPT="IFS|EXCEPT|PATH|RC_DEFAULTS|OPTIND|DESCRIBE|SEP"
435 EXCEPT="$EXCEPT|DELETE|SHOW_ALL|SHOW_EQUALS|SHOW_NAME"
436 EXCEPT="$EXCEPT|SHOW_VALUE|SHOW_FILE|SYSRC_VERBOSE|RC_CONFS"
437 EXCEPT="$EXCEPT|pgm|SUCCESS|FAILURE|CHECK_ONLY"
438 EXCEPT="$EXCEPT|f_sysrc_desc_awk|f_sysrc_delete_awk"
441 # Clean the environment (except for our required variables)
442 # and then source the required files.
444 f_clean_env --except $EXCEPT
445 if [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ]; then
449 # If passed `-a' (rather than `-A'), re-purge the
450 # environment, removing the rc.conf(5) defaults.
452 [ "$SHOW_ALL" = "1" ] \
453 && f_clean_env --except rc_conf_files $EXCEPT
456 # If `-f file' was passed, set $rc_conf_files to an
457 # explicit value, modifying the default behavior of
460 [ "${RC_CONFS+set}" ] && rc_conf_files="$RC_CONFS"
465 # If passed `-a' (rather than `-A'), remove
466 # `rc_conf_files' unless it was defined somewhere
467 # other than rc.conf(5) defaults.
469 [ "$SHOW_ALL" = "1" -a \
470 "$( f_sysrc_find rc_conf_files )" = "$RC_DEFAULTS" \
472 && unset rc_conf_files
476 awk -F= '/^[[:alpha:]_][[:alnum:]_]*=/ {print $1}' |
477 grep -Ev "^($EXCEPT)$"
480 # If enabled, describe rather than expand value
482 if [ "$DESCRIBE" ]; then
483 echo "$NAME: $( f_sysrc_desc "$NAME" )"
488 # If `-F' is passed, find it and move on
490 if [ "$SHOW_FILE" ]; then
491 [ "$SHOW_NAME" ] && echo -n "$NAME: "
497 # If `-X' is passed, delete the variables
499 if [ "$DELETE" = "2" ]; then
500 f_sysrc_delete "$NAME"
504 [ "$SYSRC_VERBOSE" ] && \
505 echo -n "$( f_sysrc_find "$NAME" ): "
508 # If `-N' is passed, simplify the output
510 if [ ! "$SHOW_VALUE" ]; then
515 echo "${SHOW_NAME:+$NAME$SEP}$(
516 f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
522 # Ignore the remainder of positional arguments.
528 # Process command-line arguments
531 while [ $# -gt 0 ]; do
535 echo "$NAME: $( f_sysrc_desc "$NAME" )"
540 # Like sysctl(8), if both `-d' AND "name=value" is passed,
541 # first describe (done above), then attempt to set
544 # If verbose, prefix line with where the directive lives
545 if [ "$SYSRC_VERBOSE" -a ! "$CHECK_ONLY" ]; then
546 file=$( f_sysrc_find "$NAME" )
547 [ "$file" = "$RC_DEFAULTS" -o ! "$file" ] && \
548 file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
549 if [ "$SHOW_EQUALS" ]; then
557 # If `-x' or `-X' is passed, delete the variable and ignore the
558 # desire to set some value
560 if [ "$DELETE" ]; then
561 f_sysrc_delete "$NAME"
567 # If `-c' is passed, simply compare and move on
569 if [ "$CHECK_ONLY" ]; then
570 if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
573 value=$( f_sysrc_get "$NAME" )
574 [ "$value" = "${1#*=}" ] || costatus=$FAILURE
581 # If `-N' is passed, simplify the output
583 if [ ! "$SHOW_VALUE" ]; then
585 f_sysrc_set "$NAME" "${1#*}"
587 if [ "$SHOW_FILE" ]; then
588 before=$( f_sysrc_find "$NAME" )
590 before=$( f_sysrc_get "$NAME" )
592 if f_sysrc_set "$NAME" "${1#*=}"; then
593 if [ "$SHOW_FILE" ]; then
594 after=$( f_sysrc_find "$NAME" )
596 after=$( f_sysrc_get "$NAME" )
598 echo -n "${SHOW_NAME:+$NAME$SEP}"
599 echo -n "$before${SHOW_EQUALS:+\" #}"
600 echo -n " -> ${SHOW_EQUALS:+\"}$after"
601 echo "${SHOW_EQUALS:+\"}"
606 if ! IGNORED=$( f_sysrc_get "$NAME?" ); then
607 [ "$IGNORE_UNKNOWNS" ] ||
608 echo "$pgm: unknown variable '$NAME'"
614 # The above check told us what we needed for `-c'
615 if [ "$CHECK_ONLY" ]; then
621 # Like sysctl(8), when `-d' is passed, desribe it
622 # (already done above) rather than expanding it
625 if [ "$DESCRIBE" ]; then
631 # If `-x' or `-X' is passed, delete the variable
633 if [ "$DELETE" ]; then
634 f_sysrc_delete "$NAME"
640 # If `-F' is passed, find it and move on
642 if [ "$SHOW_FILE" ]; then
643 [ "$SHOW_NAME" ] && echo -n "$NAME: "
649 if [ "$SYSRC_VERBOSE" ]; then
650 if [ "$SHOW_EQUALS" ]; then
651 echo -n ": $( f_sysrc_find "$NAME" ); "
653 echo -n "$( f_sysrc_find "$NAME" ): "
658 # If `-N' is passed, simplify the output
660 if [ ! "$SHOW_VALUE" ]; then
663 echo "${SHOW_NAME:+$NAME$SEP}$(
664 f_sysrc_get "$NAME" )${SHOW_EQUALS:+\"}"
670 [ ! "$CHECK_ONLY" ] || exit $costatus
672 ################################################################################
674 ################################################################################