]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - usr.sbin/bsdconfig/share/sysrc.subr
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / usr.sbin / bsdconfig / share / sysrc.subr
1 if [ ! "$_SYSRC_SUBR" ]; then _SYSRC_SUBR=1
2 #
3 # Copyright (c) 2006-2015 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$
28 #
29 ############################################################ INCLUDES
30
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 [ "$_COMMON_SUBR" ] || . $BSDCFG_SHARE/common.subr || exit 1
33
34 BSDCFG_LIBE="/usr/libexec/bsdconfig"
35 if [ ! "$_SYSRC_JAILED" ]; then
36         f_dprintf "%s: loading includes..." sysrc.subr
37         f_include_lang $BSDCFG_LIBE/include/messages.subr
38 fi
39
40 ############################################################ CONFIGURATION
41
42 #
43 # Standard pathnames (inherit values from shell if available)
44 #
45 : ${RC_DEFAULTS:="/etc/defaults/rc.conf"}
46
47 ############################################################ GLOBALS
48
49 #
50 # Global exit status variables
51 #
52 SUCCESS=0
53 FAILURE=1
54
55 #
56 # Valid characters that can appear in an sh(1) variable name
57 #
58 # Please note that the character ranges A-Z and a-z should be avoided because
59 # these can include accent characters (which are not valid in a variable name).
60 # For example, A-Z matches any character that sorts after A but before Z,
61 # including A and Z. Although ASCII order would make more sense, that is not
62 # how it works.
63 #
64 VALID_VARNAME_CHARS="0-9ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_"
65
66 ############################################################ FUNCTIONS
67
68 # f_clean_env [ --except $varname ... ]
69 #
70 # Unset all environment variables in the current scope. An optional list of
71 # arguments can be passed, indicating which variables to avoid unsetting; the
72 # `--except' is required to enable the exclusion-list as the remainder of
73 # positional arguments.
74 #
75 # Be careful not to call this in a shell that you still expect to perform
76 # $PATH expansion in, because this will blow $PATH away. This is best used
77 # within a sub-shell block "(...)" or "$(...)" or "`...`".
78 #
79 f_clean_env()
80 {
81         local var arg except=
82
83         #
84         # Should we process an exclusion-list?
85         #
86         if [ "$1" = "--except" ]; then
87                 except=1
88                 shift 1
89         fi
90
91         #
92         # Loop over a list of variable names from set(1) built-in.
93         #
94         for var in $( set | awk -F= \
95                 '/^[[:alpha:]_][[:alnum:]_]*=/ {print $1}' \
96                 | grep -v '^except$'
97         ); do
98                 #
99                 # In POSIX bourne-shell, attempting to unset(1) OPTIND results
100                 # in "unset: Illegal number:" and causes abrupt termination.
101                 #
102                 [ "$var" = OPTIND ] && continue
103
104                 #
105                 # Process the exclusion-list?
106                 #
107                 if [ "$except" ]; then
108                         for arg in "$@" ""; do
109                                 [ "$var" = "$arg" ] && break
110                         done
111                         [ "$arg" ] && continue
112                 fi
113
114                 unset "$var"
115         done
116 }
117
118 # f_sysrc_get $varname
119 #
120 # Get a system configuration setting from the collection of system-
121 # configuration files (in order: /etc/defaults/rc.conf /etc/rc.conf and
122 # /etc/rc.conf.local)
123 #
124 # NOTE: Additional shell parameter-expansion formats are supported. For
125 # example, passing an argument of "hostname%%.*" (properly quoted) will
126 # return the hostname up to (but not including) the first `.' (see sh(1),
127 # "Parameter Expansion" for more information on additional formats).
128 #
129 f_sysrc_get()
130 {
131         # Sanity check
132         [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ] || return $FAILURE
133
134         # Taint-check variable name
135         case "$1" in
136         [0-9]*)
137                 # Don't expand possible positional parameters
138                 return $FAILURE ;;
139         *)
140                 [ "$1" ] || return $FAILURE
141         esac
142
143         ( # Execute within sub-shell to protect parent environment
144
145                 #
146                 # Clear the environment of all variables, preventing the
147                 # expansion of normals such as `PS1', `TERM', etc.
148                 #
149                 f_clean_env --except IFS RC_CONFS RC_DEFAULTS
150
151                 . "$RC_DEFAULTS" > /dev/null 2>&1
152
153                 unset RC_DEFAULTS
154                         # no longer needed
155
156                 #
157                 # If the query is for `rc_conf_files' then store the value that
158                 # we inherited from sourcing RC_DEFAULTS (above) so that we may
159                 # conditionally restore this value after source_rc_confs in the
160                 # event that RC_CONFS does not customize the value.
161                 #
162                 if [ "$1" = "rc_conf_files" ]; then
163                         _rc_conf_files="$rc_conf_files"
164                 fi
165
166                 #
167                 # If RC_CONFS is defined, set $rc_conf_files to an explicit
168                 # value, modifying the default behavior of source_rc_confs().
169                 #
170                 if [ "${RC_CONFS+set}" ]; then
171                         rc_conf_files="$RC_CONFS"
172                         _rc_confs_set=1
173                 fi
174
175                 source_rc_confs > /dev/null 2>&1
176
177                 #
178                 # If the query was for `rc_conf_files' AND after calling
179                 # source_rc_confs the value has not changed, then we should
180                 # restore the value to the one inherited from RC_DEFAULTS
181                 # before performing the final query (preventing us from
182                 # returning what was set via RC_CONFS when the intent was
183                 # instead to query the value from the file(s) specified).
184                 #
185                 if [ "$1" = "rc_conf_files" -a \
186                      "$_rc_confs_set" -a \
187                      "$rc_conf_files" = "$RC_CONFS" \
188                 ]; then
189                         rc_conf_files="$_rc_conf_files"
190                         unset _rc_conf_files
191                         unset _rc_confs_set
192                 fi
193
194                 unset RC_CONFS
195                         # no longer needed
196
197                 #
198                 # This must be the last functional line for both the sub-shell
199                 # and the function to preserve the return status from formats
200                 # such as "${varname?}" and "${varname:?}" (see "Parameter
201                 # Expansion" in sh(1) for more information).
202                 #
203                 eval echo '"${'"$1"'}"' 2> /dev/null
204         )
205 }
206
207 # f_sysrc_service_configs [-a|-p] $name [$var_to_set]
208 #
209 # Get a list of optional `rc.conf.d' entries sourced by system `rc.d' script
210 # $name (see rc.subr(8) for additional information on `rc.conf.d'). If $name
211 # exists in `/etc/rc.d' or $local_startup directories and is an rc(8) script
212 # the result is a space separated list of `rc.conf.d' entries sourced by the
213 # $name `rc.d' script. Otherwise, if $name exists as a binary `rc.d' script,
214 # the result is ``/etc/rc.conf.d/$name /usr/local/etc/rc.conf.d/$name''. The
215 # result is NULL if $name does not exist.
216 #
217 # If $var_to_set is missing or NULL, output is to standard out. Returns success
218 # if $name was found, failure otherwise.
219 #
220 # If `-a' flag is given and $var_to_set is non-NULL, append result to value of
221 # $var_to_set rather than overwriting current contents.
222 #
223 # If `-p' flag is given and $var_to_set is non-NULL, prepend result to value of
224 # $var_to_set rather than overwriting current contents.
225 #
226 # NB: The `-a' and `-p' option flags are mutually exclusive.
227 #
228 f_sysrc_service_configs()
229 {
230         local OPTIND=1 OPTARG __flag __append= __prepend=
231         local __local_startup __dir __spath __stype __names=
232
233         while getopts ap __flag; do
234                 case "$__flag" in
235                 a) __append=1 __prepend= ;;
236                 p) __prepend=1 __append= ;;
237                 esac
238         done
239         shift $(( $OPTIND - 1 ))
240
241         [ $# -gt 0 ] || return $FAILURE
242         local __sname="$1" __var_to_set="$2"
243
244         __local_startup=$( f_sysrc_get local_startup )
245         for __dir in /etc/rc.d $__local_startup; do
246                 __spath="$__dir/$__sname"
247                 [ -f "$__spath" -a -x "$__spath" ] || __spath= continue
248                 break
249         done
250         [ "$__spath" ] || return $FAILURE
251
252         __stype=$( file -b "$__spath" 2> /dev/null )
253         case "$__stype" in
254         *"shell script"*)
255                 __names=$( exec 9<&1 1>&- 2>&-
256                         last_name=
257                         print_name() {
258                                 local name="$1"
259                                 [ "$name" = "$last_name" ] && return
260                                 echo "$name" >&9
261                                 last_name="$name"
262                         }
263                         eval "$( awk '{
264                                 gsub(/load_rc_config /, "print_name ")
265                                 gsub(/run_rc_command /, ": ")
266                                 print
267                         }' "$__spath" )"
268                 ) ;;
269         *)
270                 __names="$__sname"
271         esac
272
273         local __name __test_path __configs=
274         for __name in $__names; do
275                 for __dir in /etc/rc.d $__local_startup; do
276                         __test_path="${__dir%/rc.d}/rc.conf.d/$__name"
277                         [ -d "$__test_path" ] ||
278                                 __configs="$__configs $__test_path" continue
279                         for __test_path in "$__test_path"/*; do
280                                 [ -f "$__test_path" ] || continue
281                                 __configs="$__configs $__test_path"
282                         done    
283                 done
284         done
285         __configs="${__configs# }"
286
287         if [ "$__var_to_set" ]; then
288                 local __cur=
289                 [ "$__append" -o "$__prepend" ] &&
290                         f_getvar "$__var_to_set" __cur
291                 [ "$__append"  ] && __configs="$__cur{$__cur:+ }$__configs"
292                 [ "$__prepend" ] && __configs="$__configs${__cur:+ }$__cur"
293                 setvar "$__var_to_set" "$__configs"
294         else
295                 echo "$__configs"
296         fi
297
298         return $SUCCESS
299 }
300
301 # f_sysrc_get_default $varname
302 #
303 # Get a system configuration default setting from the default rc.conf(5) file
304 # (or whatever RC_DEFAULTS points at).
305 #
306 f_sysrc_get_default()
307 {
308         # Sanity check
309         [ -f "$RC_DEFAULTS" -a -r "$RC_DEFAULTS" ] || return $FAILURE
310
311         # Taint-check variable name
312         case "$1" in
313         [0-9]*)
314                 # Don't expand possible positional parameters
315                 return $FAILURE ;;
316         *)
317                 [ "$1" ] || return $FAILURE
318         esac
319
320         ( # Execute within sub-shell to protect parent environment
321
322                 #
323                 # Clear the environment of all variables, preventing the
324                 # expansion of normals such as `PS1', `TERM', etc.
325                 #
326                 f_clean_env --except RC_DEFAULTS
327
328                 . "$RC_DEFAULTS" > /dev/null 2>&1
329
330                 unset RC_DEFAULTS
331                         # no longer needed
332
333                 #
334                 # This must be the last functional line for both the sub-shell
335                 # and the function to preserve the return status from formats
336                 # such as "${varname?}" and "${varname:?}" (see "Parameter
337                 # Expansion" in sh(1) for more information).
338                 #
339                 eval echo '"${'"$1"'}"' 2> /dev/null
340         )
341 }
342
343 # f_sysrc_find $varname
344 #
345 # Find which file holds the effective last-assignment to a given variable
346 # within the rc.conf(5) file(s).
347 #
348 # If the variable is found in any of the rc.conf(5) files, the function prints
349 # the filename it was found in and then returns success. Otherwise output is
350 # NULL and the function returns with error status.
351 #
352 f_sysrc_find()
353 {
354         local varname="${1%%[!$VALID_VARNAME_CHARS]*}"
355         local regex="^[[:space:]]*$varname="
356         local rc_conf_files="$( f_sysrc_get rc_conf_files )"
357         local conf_files=
358         local file
359
360         # Check parameters
361         case "$varname" in
362         ""|[0-9]*) return $FAILURE
363         esac
364
365         #
366         # If RC_CONFS is defined, set $rc_conf_files to an explicit
367         # value, modifying the default behavior of source_rc_confs().
368         #
369         [ "${RC_CONFS+set}" ] && rc_conf_files="$RC_CONFS"
370
371         #
372         # Reverse the order of files in rc_conf_files (the boot process sources
373         # these in order, so we will search them in reverse-order to find the
374         # last-assignment -- the one that ultimately effects the environment).
375         #
376         for file in $rc_conf_files; do
377                 conf_files="$file${conf_files:+ }$conf_files"
378         done
379
380         #
381         # Append the defaults file (since directives in the defaults file
382         # indeed affect the boot process, we'll want to know when a directive
383         # is found there).
384         #
385         conf_files="$conf_files${conf_files:+ }$RC_DEFAULTS"
386
387         #
388         # Find which file matches assignment to the given variable name.
389         #
390         for file in $conf_files; do
391                 [ -f "$file" -a -r "$file" ] || continue
392                 if grep -Eq "$regex" $file; then
393                         echo $file
394                         return $SUCCESS
395                 fi
396         done
397
398         return $FAILURE # Not found
399 }
400
401 # f_sysrc_desc $varname
402 #
403 # Attempts to return the comments associated with varname from the rc.conf(5)
404 # defaults file `/etc/defaults/rc.conf' (or whatever RC_DEFAULTS points to).
405 #
406 # Multi-line comments are joined together. Results are NULL if no description
407 # could be found.
408 #
409 # This function is a two-parter. Below is the awk(1) portion of the function,
410 # afterward is the sh(1) function which utilizes the below awk script.
411 #
412 f_sysrc_desc_awk='
413 # Variables that should be defined on the invocation line:
414 #       -v varname="varname"
415 #
416 BEGIN {
417         regex = "^[[:space:]]*"varname"="
418         found = 0
419         buffer = ""
420 }
421 {
422         if ( ! found )
423         {
424                 if ( ! match($0, regex) ) next
425
426                 found = 1
427                 sub(/^[^#]*(#[[:space:]]*)?/, "")
428                 buffer = $0
429                 next
430         }
431
432         if ( !/^[[:space:]]*#/ ||
433               /^[[:space:]]*[[:alpha:]_][[:alnum:]_]*=/ ||
434               /^[[:space:]]*#[[:alpha:]_][[:alnum:]_]*=/ ||
435               /^[[:space:]]*$/ ) exit
436
437         sub(/(.*#)*[[:space:]]*/, "")
438         buffer = buffer" "$0
439 }
440 END {
441         # Clean up the buffer
442         sub(/^[[:space:]]*/, "", buffer)
443         sub(/[[:space:]]*$/, "", buffer)
444
445         print buffer
446         exit ! found
447 }
448 '
449 f_sysrc_desc()
450 {
451         awk -v varname="$1" "$f_sysrc_desc_awk" < "$RC_DEFAULTS"
452 }
453
454 # f_sysrc_set $varname $new_value
455 #
456 # Change a setting in the system configuration files (edits the files in-place
457 # to change the value in the last assignment to the variable). If the variable
458 # does not appear in the source file, it is appended to the end of the primary
459 # system configuration file `/etc/rc.conf'.
460 #
461 # This function is a two-parter. Below is the awk(1) portion of the function,
462 # afterward is the sh(1) function which utilizes the below awk script.
463 #
464 f_sysrc_set_awk='
465 # Variables that should be defined on the invocation line:
466 #       -v varname="varname"
467 #       -v new_value="new_value"
468 #
469 BEGIN {
470         regex = "^[[:space:]]*"varname"="
471         found = retval = 0
472 }
473 {
474         # If already found... just spew
475         if ( found ) { print; next }
476
477         # Does this line match an assignment to our variable?
478         if ( ! match($0, regex) ) { print; next }
479
480         # Save important match information
481         found = 1
482         matchlen = RSTART + RLENGTH - 1
483
484         # Store the value text for later munging
485         value = substr($0, matchlen + 1, length($0) - matchlen)
486
487         # Store the first character of the value
488         t1 = t2 = substr(value, 0, 1)
489
490         # Assignment w/ back-ticks, expression, or misc.
491         # We ignore these since we did not generate them
492         #
493         if ( t1 ~ /[`$\\]/ ) { retval = 1; print; next }
494
495         # Assignment w/ single-quoted value
496         else if ( t1 == "'\''" ) {
497                 sub(/^'\''[^'\'']*/, "", value)
498                 if ( length(value) == 0 ) t2 = ""
499                 sub(/^'\''/, "", value)
500         }
501
502         # Assignment w/ double-quoted value
503         else if ( t1 == "\"" ) {
504                 sub(/^"(.*\\\\+")*[^"]*/, "", value)
505                 if ( length(value) == 0 ) t2 = ""
506                 sub(/^"/, "", value)
507         }
508
509         # Assignment w/ non-quoted value
510         else if ( t1 ~ /[^[:space:];]/ ) {
511                 t1 = t2 = "\""
512                 sub(/^[^[:space:]]*/, "", value)
513         }
514
515         # Null-assignment
516         else if ( t1 ~ /[[:space:];]/ ) { t1 = t2 = "\"" }
517
518         printf "%s%c%s%c%s\n", substr($0, 0, matchlen), \
519                 t1, new_value, t2, value
520 }
521 END { exit retval }
522 '
523 f_sysrc_set()
524 {
525         local funcname=f_sysrc_set
526         local varname="$1" new_value="$2"
527
528         # Check arguments
529         [ "$varname" ] || return $FAILURE
530
531         #
532         # Find which rc.conf(5) file contains the last-assignment
533         #
534         local not_found=
535         local file="$( f_sysrc_find "$varname" )"
536         if [ "$file" = "$RC_DEFAULTS" -o ! "$file" ]; then
537                 #
538                 # We either got a null response (not found) or the variable
539                 # was only found in the rc.conf(5) defaults. In either case,
540                 # let's instead modify the first file from $rc_conf_files.
541                 #
542
543                 not_found=1
544
545                 #
546                 # If RC_CONFS is defined, use $RC_CONFS
547                 # rather than $rc_conf_files.
548                 #
549                 if [ "${RC_CONFS+set}" ]; then
550                         file="${RC_CONFS%%[$IFS]*}"
551                 else
552                         file=$( f_sysrc_get 'rc_conf_files%%[$IFS]*' )
553                 fi
554         fi
555
556         #
557         # If not found, append new value to last file and return.
558         #
559         if [ "$not_found" ]; then
560                 echo "$varname=\"$new_value\"" >> "$file"
561                 return $?
562         fi
563
564         #
565         # Perform sanity checks.
566         #
567         if [ ! -w "$file" ]; then
568                 f_err "$msg_cannot_create_permission_denied\n" \
569                       "$pgm" "$file"
570                 return $FAILURE
571         fi
572
573         #
574         # Create a new temporary file to write to.
575         #
576         local tmpfile
577         if ! f_eval_catch -dk tmpfile $funcname mktemp 'mktemp -t "%s"' "$pgm"
578         then
579                 echo "$tmpfile" >&2
580                 return $FAILURE
581         fi
582
583         #
584         # Fixup permissions (else we're in for a surprise, as mktemp(1) creates
585         # the temporary file with 0600 permissions, and if we simply mv(1) the
586         # temporary file over the destination, the destination will inherit the
587         # permissions from the temporary file).
588         #
589         local mode
590         f_eval_catch -dk mode $funcname stat 'stat -f "%%#Lp" "%s"' "$file" ||
591                 mode=0644
592         f_eval_catch -d $funcname chmod 'chmod "%s" "%s"' "$mode" "$tmpfile"
593
594         #
595         # Fixup ownership. The destination file _is_ writable (we tested
596         # earlier above). However, this will fail if we don't have sufficient
597         # permissions (so we throw stderr into the bit-bucket).
598         #
599         local owner
600         f_eval_catch -dk owner $funcname stat \
601                 'stat -f "%%u:%%g" "%s"' "$file" || owner="root:wheel"
602         f_eval_catch -d $funcname chown 'chown "%s" "%s"' "$owner" "$tmpfile"
603
604         #
605         # Operate on the matching file, replacing only the last occurrence.
606         #
607         local new_contents retval
608         new_contents=$( tail -r $file 2> /dev/null )
609         new_contents=$( echo "$new_contents" | awk -v varname="$varname" \
610                 -v new_value="$new_value" "$f_sysrc_set_awk" )
611         retval=$?
612
613         #
614         # Write the temporary file contents.
615         #
616         echo "$new_contents" | tail -r > "$tmpfile" || return $FAILURE
617         if [ $retval -ne $SUCCESS ]; then
618                 echo "$varname=\"$new_value\"" >> "$tmpfile"
619         fi
620
621         #
622         # Taint-check our results.
623         #
624         if ! f_eval_catch -d $funcname sh '/bin/sh -n "%s"' "$tmpfile"; then
625                 f_err "$msg_previous_syntax_errors\n" "$pgm" "$file"
626                 rm -f "$tmpfile"
627                 return $FAILURE
628         fi
629
630         #
631         # Finally, move the temporary file into place.
632         #
633         f_eval_catch -de $funcname mv 'mv "%s" "%s"' "$tmpfile" "$file"
634 }
635
636 # f_sysrc_delete $varname
637 #
638 # Remove a setting from the system configuration files (edits files in-place).
639 # Deletes all assignments to the given variable in all config files. If the
640 # `-f file' option is passed, the removal is restricted to only those files
641 # specified, otherwise the system collection of rc_conf_files is used.
642 #
643 # This function is a two-parter. Below is the awk(1) portion of the function,
644 # afterward is the sh(1) function which utilizes the below awk script.
645 #
646 f_sysrc_delete_awk='
647 # Variables that should be defined on the invocation line:
648 #       -v varname="varname"
649 #
650 BEGIN {
651         regex = "^[[:space:]]*"varname"="
652         found = 0
653 }
654 {
655         if ( $0 ~ regex )
656                 found = 1
657         else
658                 print
659 }
660 END { exit ! found }
661 '
662 f_sysrc_delete()
663 {
664         local funcname=f_sysrc_delete
665         local varname="$1"
666         local file
667
668         # Check arguments
669         [ "$varname" ] || return $FAILURE
670
671         #
672         # Operate on each of the specified files
673         #
674         local tmpfile
675         for file in ${RC_CONFS-$( f_sysrc_get rc_conf_files )}; do
676                 [ -e "$file" ] || continue
677
678                 #
679                 # Create a new temporary file to write to.
680                 #
681                 if ! f_eval_catch -dk tmpfile $funcname mktemp \
682                         'mktemp -t "%s"' "$pgm"
683                 then
684                         echo "$tmpfile" >&2
685                         return $FAILURE
686                 fi
687
688                 #
689                 # Fixup permissions and ownership (mktemp(1) defaults to 0600
690                 # permissions) to instead match the destination file.
691                 #
692                 local mode owner
693                 f_eval_catch -dk mode $funcname stat \
694                         'stat -f "%%#Lp" "%s"' "$file" || mode=0644
695                 f_eval_catch -dk owner $funcname stat \
696                         'stat -f "%%u:%%g" "%s"' "$file" || owner="root:wheel"
697                 f_eval_catch -d $funcname chmod \
698                         'chmod "%s" "%s"' "$mode" "$tmpfile"
699                 f_eval_catch -d $funcname chown \
700                         'chown "%s" "%s"' "$owner" "$tmpfile"
701
702                 #
703                 # Operate on the file, removing all occurrences, saving the
704                 # output in our temporary file.
705                 #
706                 awk -v varname="$varname" "$f_sysrc_delete_awk" "$file" \
707                         > "$tmpfile"
708                 if [ $? -ne $SUCCESS ]; then
709                         # The file didn't contain any assignments
710                         rm -f "$tmpfile"
711                         continue
712                 fi
713
714                 #
715                 # Taint-check our results.
716                 #
717                 if ! f_eval_catch -d $funcname sh '/bin/sh -n "%s"' "$tmpfile"
718                 then
719                         f_err "$msg_previous_syntax_errors\n" \
720                               "$pgm" "$file"
721                         rm -f "$tmpfile"
722                         return $FAILURE
723                 fi
724
725                 #
726                 # Perform sanity checks
727                 #
728                 if [ ! -w "$file" ]; then
729                         f_err "$msg_permission_denied\n" "$pgm" "$file"
730                         rm -f "$tmpfile"
731                         return $FAILURE
732                 fi
733
734                 #
735                 # Finally, move the temporary file into place.
736                 #
737                 f_eval_catch -de $funcname mv \
738                         'mv "%s" "%s"' "$tmpfile" "$file" || return $FAILURE
739         done
740 }
741
742 ############################################################ MAIN
743
744 f_dprintf "%s: Successfully loaded." sysrc.subr
745
746 fi # ! $_SYSRC_SUBR