postinstall.in revision 1.8 1 #!/bin/sh
2 #
3 # $NetBSD: postinstall.in,v 1.8 2019/10/30 20:24:44 prlw1 Exp $
4 #
5 # Copyright (c) 2002-2015 The NetBSD Foundation, Inc.
6 # All rights reserved.
7 #
8 # This code is derived from software contributed to The NetBSD Foundation
9 # by Luke Mewburn.
10 #
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
13 # are met:
14 # 1. Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 #
32 # postinstall
33 # Check for or fix configuration changes that occur
34 # over time as NetBSD evolves.
35 #
36
37 #
38 # XXX BE SURE TO USE ${DEST_DIR} PREFIX BEFORE ALL REAL FILE OPERATIONS XXX
39 #
40
41 #
42 # checks to add:
43 # - sysctl(8) renames (net.inet6.ip6.bindv6only -> net.inet6.ip6.v6only)
44 # - de* -> tlp* migration (/etc/ifconfig.de*, $ifconfig_de*, ...) ?
45 # - support quiet/verbose mode ?
46 # - differentiate between failures caused by missing source
47 # and real failures
48 # - install moduli into usr/share/examples/ssh and use from there?
49 # - differentiate between "needs fix" versus "can't fix" issues
50 #
51
52 # This script is executed as part of a cross build. Allow the build
53 # environment to override the locations of some tools.
54 : ${AWK:=awk}
55 : ${DB:=db}
56 : ${GREP:=grep}
57 : ${HOST_SH:=sh}
58 : ${MAKE:=make}
59 : ${PWD_MKDB:=/usr/sbin/pwd_mkdb}
60 : ${SED:=sed}
61 : ${SORT:=sort}
62 : ${STAT:=stat}
63
64 #
65 # helper functions
66 #
67
68 err()
69 {
70 exitval=$1
71 shift
72 echo 1>&2 "${PROGNAME}: $*"
73 if [ -n "${SCRATCHDIR}" ]; then
74 /bin/rm -rf "${SCRATCHDIR}"
75 fi
76 exit ${exitval}
77 }
78
79 warn()
80 {
81 echo 1>&2 "${PROGNAME}: $*"
82 }
83
84 msg()
85 {
86 echo " $*"
87 }
88
89 mkdtemp()
90 {
91 # Make sure we don't loop forever if mkdir will always fail.
92 [ -d /tmp ] || err 2 /tmp is not a directory
93 [ -w /tmp ] || err 2 /tmp is not writable
94
95 _base="/tmp/_postinstall.$$"
96 _serial=0
97
98 while true; do
99 _dir="${_base}.${_serial}"
100 mkdir -m 0700 "${_dir}" && break
101 _serial=$((${_serial} + 1))
102 done
103 echo "${_dir}"
104 }
105
106 # Quote args to make them safe in the shell.
107 # Usage: quotedlist="$(shell_quote args...)"
108 #
109 # After building up a quoted list, use it by evaling it inside
110 # double quotes, like this:
111 # eval "set -- $quotedlist"
112 # or like this:
113 # eval "\$command $quotedlist \$filename"
114 #
115 shell_quote()
116 {(
117 local result=''
118 local arg qarg
119 LC_COLLATE=C ; export LC_COLLATE # so [a-zA-Z0-9] works in ASCII
120 for arg in "$@" ; do
121 case "${arg}" in
122 '')
123 qarg="''"
124 ;;
125 *[!-./a-zA-Z0-9]*)
126 # Convert each embedded ' to '\'',
127 # then insert ' at the beginning of the first line,
128 # and append ' at the end of the last line.
129 # Finally, elide unnecessary '' pairs at the
130 # beginning and end of the result and as part of
131 # '\'''\'' sequences that result from multiple
132 # adjacent quotes in he input.
133 qarg="$(printf "%s\n" "$arg" | \
134 ${SED:-sed} -e "s/'/'\\\\''/g" \
135 -e "1s/^/'/" -e "\$s/\$/'/" \
136 -e "1s/^''//" -e "\$s/''\$//" \
137 -e "s/'''/'/g"
138 )"
139 ;;
140 *)
141 # Arg is not the empty string, and does not contain
142 # any unsafe characters. Leave it unchanged for
143 # readability.
144 qarg="${arg}"
145 ;;
146 esac
147 result="${result}${result:+ }${qarg}"
148 done
149 printf "%s\n" "$result"
150 )}
151
152 # Convert arg $1 to a basic regular expression (as in sed)
153 # that will match the arg. This works by inserting backslashes
154 # before characters that are special in basic regular expressions.
155 # It also inserts backslashes before the extra characters specified
156 # in $2 (which defaults to "/,").
157 # XXX: Does not handle embedded newlines.
158 # Usage: regex="$(bre_quote "${string}")"
159 bre_quote()
160 {
161 local arg="$1"
162 local extra="${2-/,}"
163 printf "%s\n" "${arg}" | ${SED} -e 's/[][^$.*\\'"${extra}"']/\\&/g'
164 }
165
166 # unprefix dir
167 # Remove any dir prefix from a list of paths on stdin,
168 # and write the result to stdout. Useful for converting
169 # from ${DEST_DIR}/path to /path.
170 #
171 unprefix()
172 {
173 [ $# -eq 1 ] || err 3 "USAGE: unprefix dir"
174 local prefix="${1%/}"
175 prefix="$(bre_quote "${prefix}")"
176
177 ${SED} -e "s,^${prefix}/,/,"
178 }
179
180 # additem item description
181 # Add item to list of supported items to check/fix,
182 # which are checked/fixed by default if no item is requested by user.
183 #
184 additem()
185 {
186 [ $# -eq 2 ] || err 3 "USAGE: additem item description"
187 defaultitems="${defaultitems}${defaultitems:+ }$1"
188 eval desc_$1=\"\$2\"
189 }
190
191 # adddisableditem item description
192 # Add item to list of supported items to check/fix,
193 # but execute the item only if the user asks for it explicitly.
194 #
195 adddisableditem()
196 {
197 [ $# -eq 2 ] || err 3 "USAGE: adddisableditem item description"
198 otheritems="${otheritems}${otheritems:+ }$1"
199 eval desc_$1=\"\$2\"
200 }
201
202 # checkdir op dir mode
203 # Ensure dir exists, and if not, create it with the appropriate mode.
204 # Returns 0 if ok, 1 otherwise.
205 #
206 check_dir()
207 {
208 [ $# -eq 3 ] || err 3 "USAGE: check_dir op dir mode"
209 _cdop="$1"
210 _cddir="$2"
211 _cdmode="$3"
212 [ -d "${_cddir}" ] && return 0
213 if [ "${_cdop}" = "check" ]; then
214 msg "${_cddir} is not a directory"
215 return 1
216 elif ! mkdir -m "${_cdmode}" "${_cddir}" ; then
217 msg "Can't create missing ${_cddir}"
218 return 1
219 else
220 msg "Missing ${_cddir} created"
221 fi
222 return 0
223 }
224
225 # check_ids op type file srcfile start id [...]
226 # Check if file of type "users" or "groups" contains the relevant IDs.
227 # Use srcfile as a reference for the expected contents.
228 # The specified "id" names should be given in numerical order,
229 # with the first name corresponding to numerical value "start",
230 # and with the special name "SKIP" being used to mark gaps in the
231 # sequence.
232 # Returns 0 if ok, 1 otherwise.
233 #
234 check_ids()
235 {
236 [ $# -ge 6 ] || err 3 "USAGE: checks_ids op type file start srcfile id [...]"
237 _op="$1"
238 _type="$2"
239 _file="$3"
240 _srcfile="$4"
241 _start="$5"
242 shift 5
243 #_ids="$@"
244
245 if [ ! -f "${_file}" ]; then
246 msg "${_file} doesn't exist; can't check for missing ${_type}"
247 return 1
248 fi
249 if [ ! -r "${_file}" ]; then
250 msg "${_file} is not readable; can't check for missing ${_type}"
251 return 1
252 fi
253 _notfixed=""
254 if [ "${_op}" = "fix" ]; then
255 _notfixed="${NOT_FIXED}"
256 fi
257 _missing="$(${AWK} -v start=$_start -F: '
258 BEGIN {
259 for (x = 1; x < ARGC; x++) {
260 if (ARGV[x] == "SKIP")
261 continue;
262 idlist[ARGV[x]]++;
263 value[ARGV[x]] = start + x - 1;
264 }
265 ARGC=1
266 }
267 {
268 found[$1]++
269 number[$1] = $3
270 }
271 END {
272 for (id in idlist) {
273 if (!(id in found))
274 printf("%s (missing)\n", id)
275 else if (number[id] != value[id])
276 printf("%s (%d != %d)\n", id,
277 number[id], value[id])
278 start++;
279 }
280 }
281 ' "$@" < "${_file}")" || return 1
282 if [ -n "${_missing}" ]; then
283 msg "Error ${_type}${_notfixed}:" $(echo ${_missing})
284 msg "Use the following as a template:"
285 set -- ${_missing}
286 while [ $# -gt 0 ]
287 do
288 ${GREP} -E "^${1}:" ${_srcfile}
289 shift 2
290 done
291 msg "and adjust if necessary."
292 return 1
293 fi
294 return 0
295 }
296
297 # populate_dir op onlynew src dest mode file [file ...]
298 # Perform op ("check" or "fix") on files in src/ against dest/
299 # If op = "check" display missing or changed files, optionally with diffs.
300 # If op != "check" copies any missing or changed files.
301 # If onlynew evaluates to true, changed files are ignored.
302 # Returns 0 if ok, 1 otherwise.
303 #
304 populate_dir()
305 {
306 [ $# -ge 5 ] || err 3 "USAGE: populate_dir op onlynew src dest mode file [...]"
307 _op="$1"
308 _onlynew="$2"
309 _src="$3"
310 _dest="$4"
311 _mode="$5"
312 shift 5
313 #_files="$@"
314
315 if [ ! -d "${_src}" ]; then
316 msg "${_src} is not a directory; skipping check"
317 return 1
318 fi
319 check_dir "${_op}" "${_dest}" 755 || return 1
320
321 _cmpdir_rv=0
322 for f in "$@"; do
323 fs="${_src}/${f}"
324 fd="${_dest}/${f}"
325 _error=""
326 if [ ! -f "${fd}" ]; then
327 _error="${fd} does not exist"
328 elif ! cmp -s "${fs}" "${fd}" ; then
329 if $_onlynew; then # leave existing ${fd} alone
330 continue;
331 fi
332 _error="${fs} != ${fd}"
333 else
334 continue
335 fi
336 if [ "${_op}" = "check" ]; then
337 msg "${_error}"
338 if [ -n "${DIFF_STYLE}" -a -f "${fd}" ]; then
339 diff -${DIFF_STYLE} ${DIFF_OPT} "${fd}" "${fs}"
340 fi
341 _cmpdir_rv=1
342 elif ! rm -f "${fd}" ||
343 ! cp -f "${fs}" "${fd}"; then
344 msg "Can't copy ${fs} to ${fd}"
345 _cmpdir_rv=1
346 elif ! chmod "${_mode}" "${fd}"; then
347 msg "Can't change mode of ${fd} to ${_mode}"
348 _cmpdir_rv=1
349 else
350 msg "Copied ${fs} to ${fd}"
351 fi
352 done
353 return ${_cmpdir_rv}
354 }
355
356 # compare_dir op src dest mode file [file ...]
357 # Perform op ("check" or "fix") on files in src/ against dest/
358 # If op = "check" display missing or changed files, optionally with diffs.
359 # If op != "check" copies any missing or changed files.
360 # Returns 0 if ok, 1 otherwise.
361 #
362 compare_dir()
363 {
364 [ $# -ge 4 ] || err 3 "USAGE: compare_dir op src dest mode file [...]"
365 _op="$1"
366 _src="$2"
367 _dest="$3"
368 _mode="$4"
369 shift 4
370 #_files="$@"
371
372 populate_dir "$_op" false "$_src" "$_dest" "$_mode" "$@"
373 }
374
375 # move_file op src dest --
376 # Check (op == "check") or move (op != "check") from src to dest.
377 # Returns 0 if ok, 1 otherwise.
378 #
379 move_file()
380 {
381 [ $# -eq 3 ] || err 3 "USAGE: move_file op src dest"
382 _fm_op="$1"
383 _fm_src="$2"
384 _fm_dest="$3"
385
386 if [ -f "${_fm_src}" -a ! -f "${_fm_dest}" ]; then
387 if [ "${_fm_op}" = "check" ]; then
388 msg "Move ${_fm_src} to ${_fm_dest}"
389 return 1
390 fi
391 if ! mv "${_fm_src}" "${_fm_dest}"; then
392 msg "Can't move ${_fm_src} to ${_fm_dest}"
393 return 1
394 fi
395 msg "Moved ${_fm_src} to ${_fm_dest}"
396 fi
397 return 0
398 }
399
400 # rcconf_is_set op name var [verbose] --
401 # Load the rcconf for name, and check if obsolete rc.conf(5) variable
402 # var is defined or not.
403 # Returns 0 if defined (even to ""), otherwise 1.
404 # If verbose != "", print an obsolete warning if the var is defined.
405 #
406 rcconf_is_set()
407 {
408 [ $# -ge 3 ] || err 3 "USAGE: rcconf_is_set op name var [verbose]"
409 _rcis_op="$1"
410 _rcis_name="$2"
411 _rcis_var="$3"
412 _rcis_verbose="$4"
413 _rcis_notfixed=""
414 if [ "${_rcis_op}" = "fix" ]; then
415 _rcis_notfixed="${NOT_FIXED}"
416 fi
417 (
418 for f in \
419 "${DEST_DIR}/etc/rc.conf" \
420 "${DEST_DIR}/etc/rc.conf.d/${_rcis_name}"; do
421 [ -f "${f}" ] && . "${f}"
422 done
423 eval echo -n \"\${${_rcis_var}}\" 1>&3
424 if eval "[ -n \"\${${_rcis_var}}\" \
425 -o \"\${${_rcis_var}-UNSET}\" != \"UNSET\" ]"; then
426 if [ -n "${_rcis_verbose}" ]; then
427 msg \
428 "Obsolete rc.conf(5) variable '\$${_rcis_var}' found.${_rcis_notfixed}"
429 fi
430 exit 0
431 else
432 exit 1
433 fi
434 )
435 }
436
437 # rcvar_is_enabled var
438 # Check if rcvar is enabled
439 #
440 rcvar_is_enabled()
441 {
442 [ $# -eq 1 ] || err 3 "USAGE: rcvar_is_enabled var"
443 _rcie_var="$1"
444 (
445 [ -f "${DEST_DIR}/etc/rc.conf" ] && . "${DEST_DIR}/etc/rc.conf"
446 eval _rcie_val=\"\${${_rcie_var}}\"
447 case $_rcie_val in
448 # "yes", "true", "on", or "1"
449 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
450 exit 0
451 ;;
452
453 *)
454 exit 1
455 ;;
456 esac
457 )
458 }
459
460 # find_file_in_dirlist() file message dir1 [...] --
461 # Find which directory file is in, and sets ${dir} to match.
462 # Returns 0 if matched, otherwise 1 (and sets ${dir} to "").
463 #
464 # Generally, check the directory for the "checking from source" case,
465 # and then the directory for the "checking from extracted etc.tgz" case.
466 #
467 find_file_in_dirlist()
468 {
469 [ $# -ge 3 ] || err 3 "USAGE: find_file_in_dirlist file msg dir1 [...]"
470
471 _file="$1" ; shift
472 _msg="$1" ; shift
473 _dir1st= # first dir in list
474 for dir in "$@"; do
475 : ${_dir1st:="${dir}"}
476 if [ -f "${dir}/${_file}" ]; then
477 if [ "${_dir1st}" != "${dir}" ]; then
478 msg \
479 "(Checking for ${_msg} from ${dir} instead of ${_dir1st})"
480 fi
481 return 0
482 fi
483 done
484 msg "Can't find source directory for ${_msg}"
485 return 1
486 }
487
488 # file_exists_exact path
489 # Returns true if a file exists in the ${DEST_DIR} whose name
490 # is exactly ${path}, interpreted in a case-sensitive way
491 # even if the underlying file system is case-insensitive.
492 #
493 # The path must begin with '/' or './', and is interpreted as
494 # being relative to ${DEST_DIR}.
495 #
496 file_exists_exact()
497 {
498 [ -n "$1" ] || err 3 "USAGE: file_exists_exact path"
499 _path="${1#.}"
500 [ -h "${DEST_DIR}${_path}" ] || \
501 [ -e "${DEST_DIR}${_path}" ] || return 1
502 while [ "${_path}" != "/" -a "${_path}" != "." ] ; do
503 _dirname="$(dirname "${_path}" 2>/dev/null)"
504 _basename="$(basename "${_path}" 2>/dev/null)"
505 ls -fa "${DEST_DIR}${_dirname}" 2> /dev/null \
506 | ${GREP} -F -x "${_basename}" >/dev/null \
507 || return 1
508 _path="${_dirname}"
509 done
510 return 0
511 }
512
513 # obsolete_paths op
514 # Obsolete the list of paths provided on stdin.
515 # Each path should start with '/' or './', and
516 # will be interpreted relative to ${DEST_DIR}.
517 #
518 obsolete_paths()
519 {
520 [ -n "$1" ] || err 3 "USAGE: obsolete_paths fix|check"
521 op="$1"
522
523 failed=0
524 while read ofile; do
525 if ! ${file_exists_exact} "${ofile}"; then
526 continue
527 fi
528 ofile="${DEST_DIR}${ofile#.}"
529 cmd="rm"
530 ftype="file"
531 if [ -h "${ofile}" ]; then
532 ftype="link"
533 elif [ -d "${ofile}" ]; then
534 ftype="directory"
535 cmd="rmdir"
536 elif [ ! -e "${ofile}" ]; then
537 continue
538 fi
539 if [ "${op}" = "check" ]; then
540 msg "Remove obsolete ${ftype} ${ofile}"
541 failed=1
542 elif ! eval "${cmd} \"\${ofile}\""; then
543 msg "Can't remove obsolete ${ftype} ${ofile}"
544 failed=1
545 else
546 msg "Removed obsolete ${ftype} ${ofile}"
547 fi
548 done
549 return ${failed}
550 }
551
552 # obsolete_libs dir
553 # Display the minor/teeny shared libraries in dir that are considered
554 # to be obsolete.
555 #
556 # The implementation supports removing obsolete major libraries
557 # if the awk variable AllLibs is set, although there is no way to
558 # enable that in the enclosing shell function as this time.
559 #
560 obsolete_libs()
561 {
562 [ $# -eq 1 ] || err 3 "USAGE: obsolete_libs dir"
563 dir="$1"
564
565 _obsolete_libs "${dir}"
566 _obsolete_libs "/usr/libdata/debug/${dir}"
567 }
568
569 exclude()
570 {
571 local dollar
572 case "$1" in
573 -t)
574 dollar='$'
575 shift
576 ;;
577 *)
578 dollar=
579 ;;
580 esac
581 if [ -z "$*" ]; then
582 cat
583 else
584 eval ${GREP} -v -E "'(^$(echo $* | \
585 ${SED} -e s/\\./\\\\./g -e 's/ /'${dollar}'|^/'g)${dollar})'"
586 fi
587 }
588
589 #
590 # find all the target symlinks of shared libaries and exclude them
591 # from consideration for removal
592 #
593 exclude_libs() {
594 local target="$(ls -l -d lib*.so.* 2> /dev/null \
595 | ${AWK} '{ print $11; }' \
596 | ${SED} -e 's@.*/@@' | ${SORT} -u)"
597 exclude -t ${target}
598 }
599
600 _obsolete_libs()
601 {
602 dir="$1"
603
604 (
605
606 if [ ! -e "${DEST_DIR}/${dir}" ]
607 then
608 return 0
609 fi
610
611 cd "${DEST_DIR}/${dir}" || err 2 "can't cd to ${DEST_DIR}/${dir}"
612 echo lib*.so.* \
613 | tr ' ' '\n' \
614 | ${AWK} -v LibDir="${dir}/" '
615 #{
616
617 function digit(v, c, n) { return (n <= c) ? v[n] : 0 }
618
619 function checklib(results, line, regex) {
620 if (! match(line, regex))
621 return
622 lib = substr(line, RSTART, RLENGTH)
623 rev = substr($0, RLENGTH+1)
624 if (! (lib in results)) {
625 results[lib] = rev
626 return
627 }
628 orevc = split(results[lib], orev, ".")
629 nrevc = split(rev, nrev, ".")
630 maxc = (orevc > nrevc) ? orevc : nrevc
631 for (i = 1; i <= maxc; i++) {
632 res = digit(orev, orevc, i) - digit(nrev, nrevc, i)
633 if (res < 0) {
634 print LibDir lib results[lib]
635 results[lib] = rev
636 return
637 } else if (res > 0) {
638 print LibDir lib rev
639 return
640 }
641 }
642 }
643
644 /^lib.*\.so\.[0-9]+\.[0-9]+(\.[0-9]+)?(\.debug)?$/ {
645 if (AllLibs)
646 checklib(minor, $0, "^lib.*\\.so\\.")
647 else
648 checklib(found, $0, "^lib.*\\.so\\.[0-9]+\\.")
649 }
650
651 /^lib.*\.so\.[0-9]+$/ {
652 if (AllLibs)
653 checklib(major, $0, "^lib.*\\.so\\.")
654 }
655
656 #}' | exclude_libs
657
658 )
659 }
660
661 # obsolete_stand dir
662 # Prints the names of all obsolete files and subdirs below the
663 # provided dir. dir should be something like /stand/${MACHINE}.
664 # The input dir and all output paths are interpreted
665 # relative to ${DEST_DIR}.
666 #
667 # Assumes that the numerically largest subdir is current, and all
668 # others are obsolete.
669 #
670 obsolete_stand()
671 {
672 [ $# -eq 1 ] || err 3 "USAGE: obsolete_stand dir"
673 local dir="$1"
674 local subdir
675
676 if ! [ -d "${DEST_DIR}${dir}" ]; then
677 msg "${DEST_DIR}${dir} doesn't exist; can't check for obsolete files"
678 return 1
679 fi
680
681 ( cd "${DEST_DIR}${dir}" && ls -1d [0-9]*[0-9]/. ) \
682 | ${GREP} -v '[^0-9./]' \
683 | sort -t. -r -n -k1,1 -k2,2 -k3,3 \
684 | tail -n +2 \
685 | while read subdir ; do
686 subdir="${subdir%/.}"
687 find "${DEST_DIR}${dir}/${subdir}" -depth -print
688 done \
689 | unprefix "${DEST_DIR}"
690 }
691
692 # modify_file op srcfile scratchfile awkprog
693 # Apply awkprog to srcfile sending output to scratchfile, and
694 # if appropriate replace srcfile with scratchfile.
695 #
696 modify_file()
697 {
698 [ $# -eq 4 ] || err 3 "USAGE: modify_file op file scratch awkprog"
699
700 _mfop="$1"
701 _mffile="$2"
702 _mfscratch="$3"
703 _mfprog="$4"
704 _mffailed=0
705
706 ${AWK} "${_mfprog}" < "${_mffile}" > "${_mfscratch}"
707 if ! cmp -s "${_mffile}" "${_mfscratch}"; then
708 diff "${_mffile}" "${_mfscratch}" > "${_mfscratch}.diffs"
709 if [ "${_mfop}" = "check" ]; then
710 msg "${_mffile} needs the following changes:"
711 _mffailed=1
712 elif ! rm -f "${_mffile}" ||
713 ! cp -f "${_mfscratch}" "${_mffile}"; then
714 msg "${_mffile} changes not applied:"
715 _mffailed=1
716 else
717 msg "${_mffile} changes applied:"
718 fi
719 while read _line; do
720 msg " ${_line}"
721 done < "${_mfscratch}.diffs"
722 fi
723 return ${_mffailed}
724 }
725
726
727 # contents_owner op directory user group
728 # Make sure directory and contents are owned (and group-owned)
729 # as specified.
730 #
731 contents_owner()
732 {
733 [ $# -eq 4 ] || err 3 "USAGE: contents_owner op dir user group"
734
735 _op="$1"
736 _dir="$2"
737 _user="$3"
738 _grp="$4"
739
740 if [ "${_op}" = "check" ]; then
741 if [ ! -z "`find "${_dir}" \( ! -user "${_user}" \) -o \
742 \( ! -group "${_grp}" \)`" ]; then
743 msg \
744 "${_dir} and contents not all owned by ${_user}:${_grp}"
745 return 1
746 else
747 return 0
748 fi
749 elif [ "${_op}" = "fix" ]; then
750 find "${_dir}" \( \( ! -user "${_user}" \) -o \
751 \( ! -group "${_grp}" \) \) -a -print0 \
752 | xargs -0 chown "${_user}:${_grp}"
753 fi
754 }
755
756 # get_makevar var [var ...]
757 # Retrieve the value of a user-settable system make variable
758 get_makevar()
759 {
760 $SOURCEMODE || err 3 "get_makevar must be used in source mode"
761 [ $# -eq 0 ] && err 3 "USAGE: get_makevar var [var ...]"
762
763 for _var in "$@"; do
764 _value="$(echo '.include <bsd.own.mk>' | \
765 ${MAKE} -f - -V "\${${_var}}")"
766
767 eval ${_var}=\"\${_value}\"
768 done
769 }
770
771 # detect_x11
772 # Detect if X11 components should be analysed and set values of
773 # relevant variables.
774 detect_x11()
775 {
776 if $SOURCEMODE; then
777 get_makevar MKX11 X11ROOTDIR X11SRCDIR
778 else
779 if [ -f "${SRC_DIR}/etc/mtree/set.xetc" ]; then
780 MKX11=yes
781 X11ROOTDIR=/this/value/isnt/used/yet
782 else
783 MKX11=no
784 X11ROOTDIR=
785 fi
786 X11SRCDIR=/nonexistent/xsrc
787 fi
788 }
789
790 #
791 # find out where MAKEDEV lives, set MAKEDEV_DIR appropriately
792 #
793 find_makedev()
794 {
795 if [ -e "${DEST_DIR}/dev/MAKEDEV" ]; then
796 MAKEDEV_DIR="${DEST_DIR}/dev"
797 elif [ -e "${DEST_DIR}/etc/MAKEDEV" ]; then
798 MAKEDEV_DIR="${DEST_DIR}/etc"
799 else
800 MAKEDEV_DIR="${DEST_DIR}/dev"
801 fi
802 }
803
804
805 #
806 # items
807 # -----
808 #
809
810 #
811 # Bluetooth
812 #
813
814 additem bluetooth "Bluetooth configuration is up to date"
815 do_bluetooth()
816 {
817 [ -n "$1" ] || err 3 "USAGE: do_bluetooth fix|check"
818 op="$1"
819 failed=0
820
821 populate_dir "${op}" true \
822 "${SRC_DIR}/etc/bluetooth" "${DEST_DIR}/etc/bluetooth" 644 \
823 hosts protocols btattach.conf btdevctl.conf
824 failed=$(( ${failed} + $? ))
825
826 move_file "${op}" "${DEST_DIR}/var/db/btdev.xml" \
827 "${DEST_DIR}/var/db/btdevctl.plist"
828 failed=$(( ${failed} + $? ))
829
830 notfixed=""
831 if [ "${op}" = "fix" ]; then
832 notfixed="${NOT_FIXED}"
833 fi
834 for _v in btattach btconfig btdevctl; do
835 if rcvar_is_enabled "${_v}"; then
836 msg \
837 "${_v} is obsolete in rc.conf(5)${notfixed}: use bluetooth=YES"
838 failed=$(( ${failed} + 1 ))
839 fi
840 done
841
842 return ${failed}
843 }
844
845 #
846 # ddbonpanic
847 #
848 additem ddbonpanic "verify ddb.onpanic is configured in sysctl.conf"
849 do_ddbonpanic()
850 {
851 [ -n "$1" ] || err 3 "USAGE: do_ddbonpanic fix|check"
852
853 if ${GREP} -E '^#*[[:space:]]*ddb\.onpanic[[:space:]]*\??=[[:space:]]*[[:digit:]]+' \
854 "${DEST_DIR}/etc/sysctl.conf" >/dev/null 2>&1
855 then
856 result=0
857 else
858 if [ "$1" = check ]; then
859 msg \
860 "The ddb.onpanic behaviour is not explicitly specified in /etc/sysctl.conf"
861 result=1
862 else
863 echo >> "${DEST_DIR}/etc/sysctl.conf"
864 ${SED} < "${SRC_DIR}/etc/sysctl.conf" \
865 -e '/^ddb\.onpanic/q' | \
866 ${SED} -e '1,/^$/d' >> \
867 "${DEST_DIR}/etc/sysctl.conf"
868 result=$?
869 fi
870 fi
871 return ${result}
872 }
873
874 #
875 # defaults
876 #
877 additem defaults "/etc/defaults/ being up to date"
878 do_defaults()
879 {
880 [ -n "$1" ] || err 3 "USAGE: do_defaults fix|check"
881 local op="$1"
882 local failed=0
883 local etcsets=$(getetcsets)
884
885 local rc_exclude_scripts=""
886 if $SOURCEMODE; then
887 # For most architectures rc.conf(5) should be the same as the
888 # one obtained from a source directory, except for the ones
889 # that have an append file for it.
890 local rc_conf_app="${SRC_DIR}/etc/etc.${MACHINE}/rc.conf.append"
891 if [ -f "${rc_conf_app}" ]; then
892 rc_exclude_scripts="rc.conf"
893
894 # Generate and compare the correct rc.conf(5) file
895 mkdir "${SCRATCHDIR}/defaults"
896
897 cat "${SRC_DIR}/etc/defaults/rc.conf" "${rc_conf_app}" \
898 > "${SCRATCHDIR}/defaults/rc.conf"
899
900 compare_dir "${op}" "${SCRATCHDIR}/defaults" \
901 "${DEST_DIR}/etc/defaults" \
902 444 \
903 "rc.conf"
904 failed=$(( ${failed} + $? ))
905 fi
906 fi
907
908 find_file_in_dirlist pf.boot.conf "pf.boot.conf" \
909 "${SRC_DIR}/usr.sbin/pf/etc/defaults" "${SRC_DIR}/etc/defaults" \
910 || return 1
911 # ${dir} is set by find_file_in_dirlist()
912 compare_dir "$op" "${dir}" "${DEST_DIR}/etc/defaults" 444 pf.boot.conf
913 failed=$(( ${failed} + $? ))
914
915 rc_exclude_scripts="${rc_exclude_scripts} pf.boot.conf"
916
917 local rc_default_conf_files="$(select_set_files /etc/defaults/ \
918 "/etc/defaults/\([^[:space:]]*\.conf\)" ${etcsets} | \
919 exclude ${rc_exclude_scripts})"
920 compare_dir "$op" "${SRC_DIR}/etc/defaults" "${DEST_DIR}/etc/defaults" \
921 444 \
922 ${rc_default_conf_files}
923 failed=$(( ${failed} + $? ))
924
925
926 return ${failed}
927 }
928
929 #
930 # dhcpcd
931 #
932 additem dhcpcd "dhcpcd configuration is up to date"
933 do_dhcpcd()
934 {
935 [ -n "$1" ] || err 3 "USAGE: do_dhcpcd fix|check"
936 op="$1"
937 failed=0
938
939 find_file_in_dirlist dhcpcd.conf "dhcpcd.conf" \
940 "${SRC_DIR}/external/bsd/dhcpcd/dist/src" \
941 "${SRC_DIR}/etc" || return 1
942 # ${dir} is set by find_file_in_dirlist()
943 populate_dir "$op" true "${dir}" "${DEST_DIR}/etc" 644 dhcpcd.conf
944 failed=$(( ${failed} + $? ))
945
946 check_dir "${op}" "${DEST_DIR}/var/db/dhcpcd" 755
947 failed=$(( ${failed} + $? ))
948
949 move_file "${op}" \
950 "${DEST_DIR}/etc/dhcpcd.duid" \
951 "${DEST_DIR}/var/db/dhcpcd/duid"
952 failed=$(( ${failed} + $? ))
953
954 move_file "${op}" \
955 "${DEST_DIR}/etc/dhcpcd.secret" \
956 "${DEST_DIR}/var/db/dhcpcd/secret"
957 failed=$(( ${failed} + $? ))
958
959 move_file "${op}" \
960 "${DEST_DIR}/var/db/dhcpcd-rdm.monotonic" \
961 "${DEST_DIR}/var/db/dhcpcd/rdm_monotonic"
962 failed=$(( ${failed} + $? ))
963
964 for lease in "${DEST_DIR}/var/db/dhcpcd-"*.lease*; do
965 [ -f "${lease}" ] || continue
966 new_lease=$(basename "${lease}" | ${SED} -e 's/dhcpcd-//')
967 new_lease="${DEST_DIR}/var/db/dhcpcd/${new_lease}"
968 move_file "${op}" "${lease}" "${new_lease}"
969 failed=$(( ${failed} + $? ))
970 done
971
972 return ${failed}
973 }
974
975 #
976 # dhcpcdrundir
977 #
978 additem dhcpcdrundir "accidentaly created /@RUNDIR@ does not exist"
979 do_dhcpcdrundir()
980 {
981 [ -n "$1" ] || err 3 "USAGE: do_dhcpcdrundir fix|check"
982 op="$1"
983 failed=0
984
985 if [ -d "${DEST_DIR}/@RUNDIR@" ]; then
986 if [ "${op}" = "check" ]; then
987 msg "Remove eroneously created /@RUNDIR@"
988 failed=1
989 elif ! rm -r "${DEST_DIR}/@RUNDIR@"; then
990 msg "Failed to remove ${DEST_DIR}/@RUNDIR@"
991 failed=1
992 else
993 msg "Removed eroneously created ${DEST_DIR}/@RUNDIR@"
994 fi
995 fi
996 return ${failed}
997 }
998
999 #
1000 # envsys
1001 #
1002 additem envsys "envsys configuration is up to date"
1003 do_envsys()
1004 {
1005 [ -n "$1" ] || err 3 "USAGE: do_envsys fix|check"
1006 local op="$1"
1007 local failed=0
1008 local etcsets=$(getetcsets)
1009
1010 populate_dir "$op" true "${SRC_DIR}/etc" "${DEST_DIR}/etc" 644 \
1011 envsys.conf
1012 failed=$(( ${failed} + $? ))
1013
1014 local powerd_scripts="$(select_set_files /etc/powerd/scripts/ \
1015 "/etc/powerd/scripts/\([^[:space:]/]*\)" ${etcsets})"
1016
1017 populate_dir "$op" true "${SRC_DIR}/etc/powerd/scripts" \
1018 "${DEST_DIR}/etc/powerd/scripts" \
1019 555 \
1020 ${powerd_scripts}
1021 failed=$(( ${failed} + $? ))
1022
1023 return ${failed}
1024 }
1025
1026 #
1027 # X11 fontconfig
1028 #
1029 additem fontconfig "X11 font configuration is up to date"
1030 do_fontconfig()
1031 {
1032 [ -n "$1" ] || err 3 "USAGE: do_fontconfig fix|check"
1033 op="$1"
1034 failed=0
1035
1036 # First, check for updates we can handle.
1037 if ! $SOURCEMODE; then
1038 FONTCONFIG_DIR="${SRC_DIR}/etc/fonts/conf.avail"
1039 else
1040 FONTCONFIG_DIR="${XSRC_DIR}/external/mit/fontconfig/dist/conf.d"
1041 fi
1042
1043 if [ ! -d "${FONTCONFIG_DIR}" ]; then
1044 msg "${FONTCONFIG_DIR} is not a directory; skipping check"
1045 return 0
1046 fi
1047 local regular_fonts="
1048 10-autohint.conf
1049 10-no-sub-pixel.conf
1050 10-scale-bitmap-fonts.conf
1051 10-sub-pixel-bgr.conf
1052 10-sub-pixel-rgb.conf
1053 10-sub-pixel-vbgr.conf
1054 10-sub-pixel-vrgb.conf
1055 10-unhinted.conf
1056 11-lcdfilter-default.conf
1057 11-lcdfilter-legacy.conf
1058 11-lcdfilter-light.conf
1059 20-unhint-small-vera.conf
1060 25-unhint-nonlatin.conf
1061 30-metric-aliases.conf
1062 40-nonlatin.conf
1063 45-generic.conf
1064 45-latin.conf
1065 49-sansserif.conf
1066 50-user.conf
1067 51-local.conf
1068 60-generic.conf
1069 60-latin.conf
1070 65-fonts-persian.conf
1071 65-khmer.conf
1072 65-nonlatin.conf
1073 69-unifont.conf
1074 70-no-bitmaps.conf
1075 70-yes-bitmaps.conf
1076 80-delicious.conf
1077 90-synthetic.conf
1078 "
1079 populate_dir "$op" false "${FONTCONFIG_DIR}" \
1080 "${DEST_DIR}/etc/fonts/conf.avail" \
1081 444 \
1082 ${regular_fonts}
1083 failed=$(( ${failed} + $? ))
1084
1085 if ! $SOURCEMODE; then
1086 FONTS_DIR="${SRC_DIR}/etc/fonts"
1087 else
1088 FONTS_DIR="${SRC_DIR}/external/mit/xorg/lib/fontconfig/etc"
1089 fi
1090
1091 populate_dir "$op" false "${FONTS_DIR}" "${DEST_DIR}/etc/fonts" 444 \
1092 fonts.conf
1093 failed=$(( ${failed} + $? ))
1094
1095 # We can't modify conf.d easily; someone might have removed a file.
1096
1097 # Look for old files that need to be deleted.
1098 obsolete_fonts="
1099 10-autohint.conf
1100 10-no-sub-pixel.conf
1101 10-sub-pixel-bgr.conf
1102 10-sub-pixel-rgb.conf
1103 10-sub-pixel-vbgr.conf
1104 10-sub-pixel-vrgb.conf
1105 10-unhinted.conf
1106 25-unhint-nonlatin.conf
1107 65-khmer.conf
1108 70-no-bitmaps.conf
1109 70-yes-bitmaps.conf
1110 "
1111 failed_fonts=""
1112 for i in ${obsolete_fonts}; do
1113 if [ -f "${DEST_DIR}/etc/fonts/conf.d/$i" ]; then
1114 conf_d_failed=1
1115 failed_fonts="$failed_fonts $i"
1116 fi
1117 done
1118
1119 if [ -n "$failed_fonts" ]; then
1120 msg \
1121 "Broken fontconfig configuration found; please delete these files:"
1122 msg "[$failed_fonts]"
1123 failed=$(( ${failed} + 1 ))
1124 fi
1125
1126 return ${failed}
1127 }
1128
1129 #
1130 # gid
1131 #
1132 additem gid "required groups in /etc/group"
1133 do_gid()
1134 {
1135 [ -n "$1" ] || err 3 "USAGE: do_gid fix|check"
1136
1137 check_ids "$1" groups "${DEST_DIR}/etc/group" \
1138 "${SRC_DIR}/etc/group" 14 \
1139 named ntpd sshd SKIP _pflogd _rwhod staff _proxy _timedc \
1140 _sdpd _httpd _mdnsd _tests _tcpdump _tss _gpio _rtadvd SKIP \
1141 _unbound _nsd nvmm
1142 }
1143
1144 #
1145 # gpio
1146 #
1147 additem gpio "gpio configuration is up to date"
1148 do_gpio()
1149 {
1150 [ -n "$1" ] || err 3 "USAGE: do_gpio fix|check"
1151 op="$1"
1152 failed=0
1153
1154 populate_dir "$op" true "${SRC_DIR}/etc" "${DEST_DIR}/etc" 644 \
1155 gpio.conf
1156 failed=$(( ${failed} + $? ))
1157
1158 return ${failed}
1159 }
1160
1161 #
1162 # hosts
1163 #
1164 additem hosts "/etc/hosts being up to date"
1165 do_hosts()
1166 {
1167 [ -n "$1" ] || err 3 "USAGE: do_hosts fix|check"
1168
1169 modify_file "$1" "${DEST_DIR}/etc/hosts" "${SCRATCHDIR}/hosts" '
1170 /^(127\.0\.0\.1|::1)[ ]+[^\.]*$/ {
1171 print $0, "localhost."
1172 next
1173 }
1174 { print }
1175 '
1176 return $?
1177 }
1178
1179 #
1180 # iscsi
1181 #
1182 additem iscsi "/etc/iscsi is populated"
1183 do_iscsi()
1184 {
1185 [ -n "$1" ] || err 3 "USAGE: do_iscsi fix|check"
1186
1187 populate_dir "${op}" true \
1188 "${SRC_DIR}/etc/iscsi" "${DEST_DIR}/etc/iscsi" 600 auths
1189 populate_dir "${op}" true \
1190 "${SRC_DIR}/etc/iscsi" "${DEST_DIR}/etc/iscsi" 644 targets
1191 return $?
1192 }
1193
1194 #
1195 # makedev
1196 #
1197 additem makedev "/dev/MAKEDEV being up to date"
1198 do_makedev()
1199 {
1200 [ -n "$1" ] || err 3 "USAGE: do_makedev fix|check"
1201 failed=0
1202
1203 if [ -f "${SRC_DIR}/etc/MAKEDEV.tmpl" ]; then
1204 # generate MAKEDEV from source if source is available
1205 env MACHINE="${MACHINE}" \
1206 MACHINE_ARCH="${MACHINE_ARCH}" \
1207 NETBSDSRCDIR="${SRC_DIR}" \
1208 ${AWK} -f "${SRC_DIR}/etc/MAKEDEV.awk" \
1209 "${SRC_DIR}/etc/MAKEDEV.tmpl" > "${SCRATCHDIR}/MAKEDEV"
1210 fi
1211
1212 find_file_in_dirlist MAKEDEV "MAKEDEV" \
1213 "${SCRATCHDIR}" "${SRC_DIR}/dev" \
1214 || return 1
1215 # ${dir} is set by find_file_in_dirlist()
1216 find_makedev
1217 compare_dir "$1" "${dir}" "${MAKEDEV_DIR}" 555 MAKEDEV
1218 failed=$(( ${failed} + $? ))
1219
1220 find_file_in_dirlist MAKEDEV.local "MAKEDEV.local" \
1221 "${SRC_DIR}/etc" "${SRC_DIR}/dev" \
1222 || return 1
1223 # ${dir} is set by find_file_in_dirlist()
1224 compare_dir "$1" "${dir}" "${DEST_DIR}/dev" 555 MAKEDEV.local
1225 failed=$(( ${failed} + $? ))
1226
1227 return ${failed}
1228 }
1229
1230 #
1231 # motd
1232 #
1233 additem motd "contents of motd"
1234 do_motd()
1235 {
1236 [ -n "$1" ] || err 3 "USAGE: do_motd fix|check"
1237
1238 if ${GREP} -i 'http://www.NetBSD.org/Misc/send-pr.html' \
1239 "${DEST_DIR}/etc/motd" >/dev/null 2>&1 \
1240 || ${GREP} -i 'http://www.NetBSD.org/support/send-pr.html' \
1241 "${DEST_DIR}/etc/motd" >/dev/null 2>&1
1242 then
1243 tmp1="$(mktemp /tmp/postinstall.motd.XXXXXXXX)"
1244 tmp2="$(mktemp /tmp/postinstall.motd.XXXXXXXX)"
1245 ${SED} '1,2d' <"${SRC_DIR}/etc/motd" >"${tmp1}"
1246 ${SED} '1,2d' <"${DEST_DIR}/etc/motd" >"${tmp2}"
1247
1248 if [ "$1" = check ]; then
1249 cmp -s "${tmp1}" "${tmp2}"
1250 result=$?
1251 if [ "${result}" -ne 0 ]; then
1252 msg \
1253 "Bug reporting messages do not seem to match the installed release"
1254 fi
1255 else
1256 head -n 2 "${DEST_DIR}/etc/motd" >"${tmp1}"
1257 ${SED} '1,2d' <"${SRC_DIR}/etc/motd" >>"${tmp1}"
1258 cp "${tmp1}" "${DEST_DIR}/etc/motd"
1259 result=0
1260 fi
1261
1262 rm -f "${tmp1}" "${tmp2}"
1263 else
1264 result=0
1265 fi
1266
1267 return ${result}
1268 }
1269
1270 #
1271 # mtree
1272 #
1273 additem mtree "/etc/mtree/ being up to date"
1274 do_mtree()
1275 {
1276 [ -n "$1" ] || err 3 "USAGE: do_mtree fix|check"
1277 failed=0
1278
1279 compare_dir "$1" "${SRC_DIR}/etc/mtree" "${DEST_DIR}/etc/mtree" 444 special
1280 failed=$(( ${failed} + $? ))
1281
1282 if ! $SOURCEMODE; then
1283 MTREE_DIR="${SRC_DIR}/etc/mtree"
1284 else
1285 /bin/rm -rf "${SCRATCHDIR}/obj"
1286 mkdir "${SCRATCHDIR}/obj"
1287 ${MAKE} -s -C "${SRC_DIR}/etc/mtree" TOOL_AWK="${AWK}" \
1288 MAKEOBJDIR="${SCRATCHDIR}/obj" emit_dist_file > \
1289 "${SCRATCHDIR}/NetBSD.dist"
1290 MTREE_DIR="${SCRATCHDIR}"
1291 /bin/rm -rf "${SCRATCHDIR}/obj"
1292 fi
1293 compare_dir "$1" "${MTREE_DIR}" "${DEST_DIR}/etc/mtree" 444 NetBSD.dist
1294 failed=$(( ${failed} + $? ))
1295
1296 return ${failed}
1297 }
1298
1299 #
1300 # named
1301 #
1302 additem named "named configuration update"
1303 do_named()
1304 {
1305 [ -n "$1" ] || err 3 "USAGE: do_named fix|check"
1306 op="$1"
1307
1308 move_file "${op}" \
1309 "${DEST_DIR}/etc/namedb/named.conf" \
1310 "${DEST_DIR}/etc/named.conf"
1311
1312 compare_dir "${op}" "${SRC_DIR}/etc/namedb" "${DEST_DIR}/etc/namedb" \
1313 644 \
1314 root.cache
1315 }
1316
1317 #
1318 # pam
1319 #
1320 additem pam "/etc/pam.d is populated"
1321 do_pam()
1322 {
1323 [ -n "$1" ] || err 3 "USAGE: do_pam fix|check"
1324 op="$1"
1325 failed=0
1326
1327 populate_dir "${op}" true "${SRC_DIR}/etc/pam.d" \
1328 "${DEST_DIR}/etc/pam.d" 644 \
1329 README cron display_manager ftpd gdm imap kde login other \
1330 passwd pop3 ppp racoon rexecd rsh sshd su system telnetd \
1331 xdm xserver
1332
1333 failed=$(( ${failed} + $? ))
1334
1335 return ${failed}
1336 }
1337
1338 #
1339 # periodic
1340 #
1341 additem periodic "/etc/{daily,weekly,monthly,security} being up to date"
1342 do_periodic()
1343 {
1344 [ -n "$1" ] || err 3 "USAGE: do_periodic fix|check"
1345
1346 compare_dir "$1" "${SRC_DIR}/etc" "${DEST_DIR}/etc" 644 \
1347 daily weekly monthly security
1348 }
1349
1350 #
1351 # pf
1352 #
1353 additem pf "pf configuration being up to date"
1354 do_pf()
1355 {
1356 [ -n "$1" ] || err 3 "USAGE: do_pf fix|check"
1357 op="$1"
1358 failed=0
1359
1360 find_file_in_dirlist pf.os "pf.os" \
1361 "${SRC_DIR}/dist/pf/etc" "${SRC_DIR}/etc" \
1362 || return 1
1363 # ${dir} is set by find_file_in_dirlist()
1364 populate_dir "${op}" true \
1365 "${dir}" "${DEST_DIR}/etc" 644 \
1366 pf.conf
1367 failed=$(( ${failed} + $? ))
1368
1369 compare_dir "${op}" "${dir}" "${DEST_DIR}/etc" 444 pf.os
1370 failed=$(( ${failed} + $? ))
1371
1372 return ${failed}
1373 }
1374
1375 #
1376 # pwd_mkdb
1377 #
1378 additem pwd_mkdb "passwd database version"
1379 do_pwd_mkdb()
1380 {
1381 [ -n "$1" ] || err 3 "USAGE: do_pwd_mkdb fix|check"
1382 op="$1"
1383 failed=0
1384
1385 # XXX Ideally, we should figure out the endianness of the
1386 # target machine, and add "-E B"/"-E L" to the db(1) flags,
1387 # and "-B"/"-L" to the pwd_mkdb(8) flags if the target is not
1388 # the same as the host machine. It probably doesn't matter,
1389 # because we don't expect "postinstall fix pwd_mkdb" to be
1390 # invoked during a cross build.
1391
1392 set -- $(${DB} -q -Sb -Ub -To -N hash "${DEST_DIR}/etc/pwd.db" \
1393 'VERSION\0')
1394 case "$2" in
1395 '\001\000\000\000') return 0 ;; # version 1, little-endian
1396 '\000\000\000\001') return 0 ;; # version 1, big-endian
1397 esac
1398
1399 if [ "${op}" = "check" ]; then
1400 msg "Update format of passwd database"
1401 failed=1
1402 elif ! ${PWD_MKDB} -V 1 -d "${DEST_DIR:-/}" \
1403 "${DEST_DIR}/etc/master.passwd";
1404 then
1405 msg "Can't update format of passwd database"
1406 failed=1
1407 else
1408 msg "Updated format of passwd database"
1409 fi
1410
1411 return ${failed}
1412 }
1413
1414 #
1415 # rc
1416 #
1417
1418 rc_obsolete_vars="
1419 amd amd_master
1420 btcontrol btcontrol_devices
1421 critical_filesystems critical_filesystems_beforenet
1422 mountcritlocal mountcritremote
1423 network ip6forwarding
1424 network nfsiod_flags
1425 sdpd sdpd_control
1426 sdpd sdpd_groupname
1427 sdpd sdpd_username
1428 sysctl defcorename
1429 "
1430
1431 update_rc()
1432 {
1433 local op=$1
1434 local dir=$2
1435 local name=$3
1436 local bindir=$4
1437 local rcdir=$5
1438
1439 if [ ! -x "${DEST_DIR}/${bindir}/${name}" ]; then
1440 return 0
1441 fi
1442
1443 if ! find_file_in_dirlist "${name}" "${name}" \
1444 "${rcdir}" "${SRC_DIR}/etc/rc.d"; then
1445 return 1
1446 fi
1447 populate_dir "${op}" false "${dir}" "${DEST_DIR}/etc/rc.d" 555 "${name}"
1448 return $?
1449 }
1450
1451 # select non-obsolete files in a sets file
1452 # $1: directory pattern
1453 # $2: file pattern
1454 # $3: filename
1455 select_set_files()
1456 {
1457 local qdir="$(echo $1 | ${SED} -e s@/@\\\\/@g -e s/\\./\\\\./g)"
1458 ${SED} -n -e /obsolete/d \
1459 -e "/^\.${qdir}/s@^.$2[[:space:]].*@\1@p" $3
1460 }
1461
1462 # select obsolete files in a sets file
1463 # $1: directory pattern
1464 # $2: file pattern
1465 # $3: filename
1466 select_obsolete_files()
1467 {
1468 ${SED} -n -e "/obsolete/s@\.$1$2[[:space:]].*@\1@p" $3
1469 }
1470
1471
1472 getetcsets()
1473 {
1474 if $SOURCEMODE; then
1475 echo "${SRC_DIR}/distrib/sets/lists/etc/mi"
1476 else
1477 echo "${SRC_DIR}/etc/mtree/set.etc"
1478 fi
1479 }
1480
1481 additem rc "/etc/rc* and /etc/rc.d/ being up to date"
1482 do_rc()
1483 {
1484 [ -n "$1" ] || err 3 "USAGE: do_rc fix|check"
1485 local op="$1"
1486 local failed=0
1487 local generated_scripts=""
1488 local etcsets=$(getetcsets)
1489 if [ "${MKX11}" != "no" ]; then
1490 generated_scripts="${generated_scripts} xdm xfs"
1491 fi
1492
1493 # Directories of external programs that have rc files (in bsd)
1494 local rc_external_files="blacklist nsd unbound"
1495
1496 # rc* files in /etc/
1497 local rc_444_files="$(select_set_files /etc/rc \
1498 "/etc/\(rc[^[:space:]/]*\)" ${etcsets})"
1499
1500 # no-obsolete rc files in /etc/rc.d
1501 local rc_555_files="$(select_set_files /etc/rc.d/ \
1502 "/etc/rc\.d/\([^[:space:]]*\)" ${etcsets} | \
1503 exclude ${rc_external_files})"
1504
1505 # obsolete rc file in /etc/rc.d
1506 local rc_obsolete_files="$(select_obsolete_files /etc/rc.d/ \
1507 "\([^[:space:]]*\)" ${etcsets})"
1508
1509 compare_dir "${op}" "${SRC_DIR}/etc" "${DEST_DIR}/etc" 644 \
1510 ${rc_644_files}
1511 failed=$(( ${failed} + $? ))
1512
1513 local extra_scripts
1514 if ! $SOURCEMODE; then
1515 extra_scripts="${generated_scripts}"
1516 else
1517 extra_scripts=""
1518 fi
1519
1520 compare_dir "${op}" "${SRC_DIR}/etc/rc.d" "${DEST_DIR}/etc/rc.d" 555 \
1521 ${rc_555_files} \
1522 ${extra_scripts}
1523 failed=$(( ${failed} + $? ))
1524
1525 for i in ${rc_external_files}; do
1526 local rc_file
1527 case $i in
1528 *d) rc_file=${i};;
1529 *) rc_file=${i}d;;
1530 esac
1531
1532 update_rc "${op}" "${dir}" ${rc_file} /sbin \
1533 "${SRC_DIR}/external/bsd/$i/etc/rc.d"
1534 failed=$(( ${failed} + $? ))
1535 done
1536
1537 if $SOURCEMODE && [ -n "${generated_scripts}" ]; then
1538 # generate scripts
1539 mkdir "${SCRATCHDIR}/rc"
1540 for f in ${generated_scripts}; do
1541 ${SED} -e "s,@X11ROOTDIR@,${X11ROOTDIR},g" \
1542 < "${SRC_DIR}/etc/rc.d/${f}.in" \
1543 > "${SCRATCHDIR}/rc/${f}"
1544 done
1545 compare_dir "${op}" "${SCRATCHDIR}/rc" \
1546 "${DEST_DIR}/etc/rc.d" 555 \
1547 ${generated_scripts}
1548 failed=$(( ${failed} + $? ))
1549 fi
1550
1551 # check for obsolete rc.d files
1552 for f in ${rc_obsolete_files}; do
1553 local fd="/etc/rc.d/${f}"
1554 [ -e "${DEST_DIR}${fd}" ] && echo "${fd}"
1555 done | obsolete_paths "${op}"
1556 failed=$(( ${failed} + $? ))
1557
1558 # check for obsolete rc.conf(5) variables
1559 set -- ${rc_obsolete_vars}
1560 while [ $# -gt 1 ]; do
1561 if rcconf_is_set "${op}" "$1" "$2" 1; then
1562 failed=1
1563 fi
1564 shift 2
1565 done
1566
1567 return ${failed}
1568 }
1569
1570 #
1571 # sendmail
1572 #
1573 adddisableditem sendmail "remove obsolete sendmail configuration files and scripts"
1574 do_sendmail()
1575 {
1576 [ -n "$1" ] || err 3 "USAGE: do_sendmail fix|check"
1577 op="$1"
1578 failed=0
1579
1580 # Don't complain if the "sendmail" package is installed because the
1581 # files might still be in use.
1582 if /usr/sbin/pkg_info -qe sendmail >/dev/null 2>&1; then
1583 return 0
1584 fi
1585
1586 for f in /etc/mail/helpfile /etc/mail/local-host-names \
1587 /etc/mail/sendmail.cf /etc/mail/submit.cf /etc/rc.d/sendmail \
1588 /etc/rc.d/smmsp /usr/share/misc/sendmail.hf \
1589 $( ( find "${DEST_DIR}/usr/share/sendmail" -type f ; \
1590 find "${DEST_DIR}/usr/share/sendmail" -type d \
1591 ) | unprefix "${DEST_DIR}" ) \
1592 /var/log/sendmail.st \
1593 /var/spool/clientmqueue \
1594 /var/spool/mqueue
1595 do
1596 [ -e "${DEST_DIR}${f}" ] && echo "${f}"
1597 done | obsolete_paths "${op}"
1598 failed=$(( ${failed} + $? ))
1599
1600 return ${failed}
1601 }
1602
1603 #
1604 # mailerconf
1605 #
1606 adddisableditem mailerconf "update /etc/mailer.conf after sendmail removal"
1607 do_mailerconf()
1608 {
1609 [ -n "$1" ] || err 3 "USAGE: do_mailterconf fix|check"
1610 op="$1"
1611
1612 failed=0
1613 mta_path="$(${AWK} '/^sendmail[ \t]/{print$2}' \
1614 "${DEST_DIR}/etc/mailer.conf")"
1615 old_sendmail_path="/usr/libexec/sendmail/sendmail"
1616 if [ "${mta_path}" = "${old_sendmail_path}" ]; then
1617 if [ "$op" = check ]; then
1618 msg "mailer.conf points to obsolete ${old_sendmail_path}"
1619 failed=1;
1620 else
1621 populate_dir "${op}" false \
1622 "${SRC_DIR}/etc" "${DEST_DIR}/etc" 644 mailer.conf
1623 failed=$?
1624 fi
1625 fi
1626
1627 return ${failed}
1628 }
1629
1630 #
1631 # ssh
1632 #
1633 additem ssh "ssh configuration update"
1634 do_ssh()
1635 {
1636 [ -n "$1" ] || err 3 "USAGE: do_ssh fix|check"
1637 op="$1"
1638
1639 failed=0
1640 _etcssh="${DEST_DIR}/etc/ssh"
1641 if ! check_dir "${op}" "${_etcssh}" 755; then
1642 failed=1
1643 fi
1644
1645 if [ ${failed} -eq 0 ]; then
1646 for f in \
1647 ssh_known_hosts ssh_known_hosts2 \
1648 ssh_host_dsa_key ssh_host_dsa_key.pub \
1649 ssh_host_rsa_key ssh_host_rsa_key.pub \
1650 ssh_host_key ssh_host_key.pub \
1651 ; do
1652 if ! move_file "${op}" \
1653 "${DEST_DIR}/etc/${f}" "${_etcssh}/${f}" ; then
1654 failed=1
1655 fi
1656 done
1657 for f in sshd.conf ssh.conf ; do
1658 # /etc/ssh/ssh{,d}.conf -> ssh{,d}_config
1659 #
1660 if ! move_file "${op}" \
1661 "${_etcssh}/${f}" "${_etcssh}/${f%.conf}_config" ;
1662 then
1663 failed=1
1664 fi
1665 # /etc/ssh{,d}.conf -> /etc/ssh/ssh{,d}_config
1666 #
1667 if ! move_file "${op}" \
1668 "${DEST_DIR}/etc/${f}" \
1669 "${_etcssh}/${f%.conf}_config" ;
1670 then
1671 failed=1
1672 fi
1673 done
1674 fi
1675
1676 sshdconf=""
1677 for f in \
1678 "${_etcssh}/sshd_config" \
1679 "${_etcssh}/sshd.conf" \
1680 "${DEST_DIR}/etc/sshd.conf" ; do
1681 if [ -f "${f}" ]; then
1682 sshdconf="${f}"
1683 break
1684 fi
1685 done
1686 if [ -n "${sshdconf}" ]; then
1687 modify_file "${op}" "${sshdconf}" "${SCRATCHDIR}/sshdconf" '
1688 /^[^#$]/ {
1689 kw = tolower($1)
1690 if (kw == "hostkey" &&
1691 $2 ~ /^\/etc\/+ssh_host(_[dr]sa)?_key$/ ) {
1692 sub(/\/etc\/+/, "/etc/ssh/")
1693 }
1694 if (kw == "rhostsauthentication" ||
1695 kw == "verifyreversemapping" ||
1696 kw == "reversemappingcheck") {
1697 sub(/^/, "# DEPRECATED:\t")
1698 }
1699 }
1700 { print }
1701 '
1702 failed=$(( ${failed} + $? ))
1703 fi
1704
1705 if ! find_file_in_dirlist moduli "moduli" \
1706 "${SRC_DIR}/crypto/external/bsd/openssh/dist" "${SRC_DIR}/etc" ; then
1707 failed=1
1708 # ${dir} is set by find_file_in_dirlist()
1709 elif ! compare_dir "${op}" "${dir}" "${DEST_DIR}/etc" 444 moduli; then
1710 failed=1
1711 fi
1712
1713 if ! check_dir "${op}" "${DEST_DIR}/var/chroot/sshd" 755 ; then
1714 failed=1
1715 fi
1716
1717 if rcconf_is_set "${op}" sshd sshd_conf_dir 1; then
1718 failed=1
1719 fi
1720
1721 return ${failed}
1722 }
1723
1724 #
1725 # wscons
1726 #
1727 additem wscons "wscons configuration file update"
1728 do_wscons()
1729 {
1730 [ -n "$1" ] || err 3 "USAGE: do_wscons fix|check"
1731 op="$1"
1732
1733 [ -f "${DEST_DIR}/etc/wscons.conf" ] || return 0
1734
1735 failed=0
1736 notfixed=""
1737 if [ "${op}" = "fix" ]; then
1738 notfixed="${NOT_FIXED}"
1739 fi
1740 while read _type _arg1 _rest; do
1741 if [ "${_type}" = "mux" -a "${_arg1}" = "1" ]; then
1742 msg \
1743 "Obsolete wscons.conf(5) entry \""${_type} ${_arg1}"\" found.${notfixed}"
1744 failed=1
1745 fi
1746 done < "${DEST_DIR}/etc/wscons.conf"
1747
1748 return ${failed}
1749 }
1750
1751 #
1752 # X11
1753 #
1754 additem x11 "x11 configuration update"
1755 do_x11()
1756 {
1757 [ -n "$1" ] || err 3 "USAGE: do_x11 fix|check"
1758 op="$1"
1759
1760 failed=0
1761 _etcx11="${DEST_DIR}/etc/X11"
1762 if [ ! -d "${_etcx11}" ]; then
1763 msg "${_etcx11} is not a directory; skipping check"
1764 return 0
1765 fi
1766 if [ -d "${DEST_DIR}/usr/X11R6/." ]
1767 then
1768 _libx11="${DEST_DIR}/usr/X11R6/lib/X11"
1769 if [ ! -d "${_libx11}" ]; then
1770 msg "${_libx11} is not a directory; skipping check"
1771 return 0
1772 fi
1773 fi
1774
1775 _notfixed=""
1776 if [ "${op}" = "fix" ]; then
1777 _notfixed="${NOT_FIXED}"
1778 fi
1779
1780 for d in \
1781 fs lbxproxy proxymngr rstart twm xdm xinit xserver xsm \
1782 ; do
1783 sd="${_libx11}/${d}"
1784 ld="/etc/X11/${d}"
1785 td="${DEST_DIR}${ld}"
1786 if [ -h "${sd}" ]; then
1787 continue
1788 elif [ -d "${sd}" ]; then
1789 tdfiles="$(find "${td}" \! -type d)"
1790 if [ -n "${tdfiles}" ]; then
1791 msg "${sd} exists yet ${td} already" \
1792 "contains files${_notfixed}"
1793 else
1794 msg "Migrate ${sd} to ${td}${_notfixed}"
1795 fi
1796 failed=1
1797 elif [ -e "${sd}" ]; then
1798 msg "Unexpected file ${sd}${_notfixed}"
1799 continue
1800 else
1801 continue
1802 fi
1803 done
1804
1805 # check if xdm resources have been updated
1806 if [ -r ${_etcx11}/xdm/Xresources ] && \
1807 ! ${GREP} 'inpColor:' ${_etcx11}/xdm/Xresources > /dev/null; then
1808 msg "Update ${_etcx11}/xdm/Xresources${_notfixed}"
1809 failed=1
1810 fi
1811
1812 return ${failed}
1813 }
1814
1815 #
1816 # xkb
1817 #
1818 # /usr/X11R7/lib/X11/xkb/symbols/pc used to be a directory, but changed
1819 # to a file on 2009-06-12. Fixing this requires removing the directory
1820 # (which we can do) and re-extracting the xbase set (which we can't do),
1821 # or at least adding that one file (which we may be able to do if X11SRCDIR
1822 # is available).
1823 #
1824 additem xkb "clean up for xkbdata to xkeyboard-config upgrade"
1825 do_xkb()
1826 {
1827 [ -n "$1" ] || err 3 "USAGE: do_xkb fix|check"
1828 op="$1"
1829 failed=0
1830
1831 pcpath="/usr/X11R7/lib/X11/xkb/symbols/pc"
1832 pcsrcdir="${X11SRCDIR}/external/mit/xkeyboard-config/dist/symbols"
1833
1834 filemsg="\
1835 ${pcpath} was a directory, should be a file.
1836 To fix, extract the xbase set again."
1837
1838 _notfixed=""
1839 if [ "${op}" = "fix" ]; then
1840 _notfixed="${NOT_FIXED}"
1841 fi
1842
1843 if [ ! -d "${DEST_DIR}${pcpath}" ]; then
1844 return 0
1845 fi
1846
1847 # Delete obsolete files in the directory, and the directory
1848 # itself. If the directory contains unexpected extra files
1849 # then it will not be deleted.
1850 ( [ -f "${DEST_DIR}"/var/db/obsolete/xbase ] \
1851 && ${SORT} -ru "${DEST_DIR}"/var/db/obsolete/xbase \
1852 | ${GREP} -E "^\\.?${pcpath}/" ;
1853 echo "${pcpath}" ) \
1854 | obsolete_paths "${op}"
1855 failed=$(( ${failed} + $? ))
1856
1857 # If the directory was removed above, then try to replace it with
1858 # a file.
1859 if [ -d "${DEST_DIR}${pcpath}" ]; then
1860 msg "${filemsg}${_notfixed}"
1861 failed=$(( ${failed} + 1 ))
1862 else
1863 if ! find_file_in_dirlist pc "${pcpath}" \
1864 "${pcsrcdir}" "${SRC_DIR}${pcpath%/*}"
1865 then
1866 msg "${filemsg}${_notfixed}"
1867 failed=$(( ${failed} + 1 ))
1868 else
1869 # ${dir} is set by find_file_in_dirlist()
1870 populate_dir "${op}" true \
1871 "${dir}" "${DEST_DIR}${pcpath%/*}" 444 \
1872 pc
1873 failed=$(( ${failed} + $? ))
1874 fi
1875 fi
1876
1877 return $failed
1878 }
1879
1880 #
1881 # uid
1882 #
1883 additem uid "required users in /etc/master.passwd"
1884 do_uid()
1885 {
1886 [ -n "$1" ] || err 3 "USAGE: do_uid fix|check"
1887
1888 check_ids "$1" users "${DEST_DIR}/etc/master.passwd" \
1889 "${SRC_DIR}/etc/master.passwd" 12 \
1890 postfix SKIP named ntpd sshd SKIP _pflogd _rwhod SKIP _proxy \
1891 _timedc _sdpd _httpd _mdnsd _tests _tcpdump _tss SKIP _rtadvd \
1892 SKIP _unbound _nsd
1893 }
1894
1895
1896 #
1897 # varrwho
1898 #
1899 additem varrwho "required ownership of files in /var/rwho"
1900 do_varrwho()
1901 {
1902 [ -n "$1" ] || err 3 "USAGE: do_varrwho fix|check"
1903
1904 contents_owner "$1" "${DEST_DIR}/var/rwho" _rwhod _rwhod
1905 }
1906
1907
1908 #
1909 # tcpdumpchroot
1910 #
1911 additem tcpdumpchroot "remove /var/chroot/tcpdump/etc/protocols"
1912 do_tcpdumpchroot()
1913 {
1914 [ -n "$1" ] || err 3 "USAGE: do_tcpdumpchroot fix|check"
1915
1916 failed=0;
1917 if [ -r "${DEST_DIR}/var/chroot/tcpdump/etc/protocols" ]; then
1918 if [ "$1" = "fix" ]; then
1919 rm "${DEST_DIR}/var/chroot/tcpdump/etc/protocols"
1920 failed=$(( ${failed} + $? ))
1921 rmdir "${DEST_DIR}/var/chroot/tcpdump/etc"
1922 failed=$(( ${failed} + $? ))
1923 else
1924 failed=1
1925 fi
1926 fi
1927 return ${failed}
1928 }
1929
1930
1931 #
1932 # atf
1933 #
1934 additem atf "install missing atf configuration files and validate them"
1935 do_atf()
1936 {
1937 [ -n "$1" ] || err 3 "USAGE: do_atf fix|check"
1938 op="$1"
1939 failed=0
1940
1941 # Ensure atf configuration files are in place.
1942 if find_file_in_dirlist NetBSD.conf "NetBSD.conf" \
1943 "${SRC_DIR}/external/bsd/atf/etc/atf" \
1944 "${SRC_DIR}/etc/atf"; then
1945 # ${dir} is set by find_file_in_dirlist()
1946 populate_dir "${op}" true "${dir}" "${DEST_DIR}/etc/atf" 644 \
1947 NetBSD.conf common.conf || failed=1
1948 else
1949 failed=1
1950 fi
1951 if find_file_in_dirlist atf-run.hooks "atf-run.hooks" \
1952 "${SRC_DIR}/external/bsd/atf/dist/tools/sample" \
1953 "${SRC_DIR}/etc/atf"; then
1954 # ${dir} is set by find_file_in_dirlist()
1955 populate_dir "${op}" true "${dir}" "${DEST_DIR}/etc/atf" 644 \
1956 atf-run.hooks || failed=1
1957 else
1958 failed=1
1959 fi
1960
1961 # Validate the _atf to _tests user/group renaming.
1962 if [ -f "${DEST_DIR}/etc/atf/common.conf" ]; then
1963 handle_atf_user "${op}" || failed=1
1964 else
1965 failed=1
1966 fi
1967
1968 return ${failed}
1969 }
1970
1971 handle_atf_user()
1972 {
1973 local op="$1"
1974 local failed=0
1975
1976 local conf="${DEST_DIR}/etc/atf/common.conf"
1977 if grep '[^#]*unprivileged-user[ \t]*=.*_atf' "${conf}" >/dev/null
1978 then
1979 if [ "$1" = "fix" ]; then
1980 ${SED} -e \
1981 "/[^#]*unprivileged-user[\ t]*=/s/_atf/_tests/" \
1982 "${conf}" >"${conf}.new"
1983 failed=$(( ${failed} + $? ))
1984 mv "${conf}.new" "${conf}"
1985 failed=$(( ${failed} + $? ))
1986 msg "Set unprivileged-user=_tests in ${conf}"
1987 else
1988 msg "unprivileged-user=_atf in ${conf} should be" \
1989 "unprivileged-user=_tests"
1990 failed=1
1991 fi
1992 fi
1993
1994 return ${failed}
1995 }
1996
1997 #
1998 # catpages
1999 #
2000 obsolete_catpages()
2001 {
2002 basedir="$2"
2003 section="$3"
2004 mandir="${basedir}/man${section}"
2005 catdir="${basedir}/cat${section}"
2006 test -d "$mandir" || return 0
2007 test -d "$catdir" || return 0
2008 (cd "$mandir" && find . -type f) | {
2009 failed=0
2010 while read manpage; do
2011 manpage="${manpage#./}"
2012 case "$manpage" in
2013 *.Z)
2014 catname="$catdir/${manpage%.*.Z}.0"
2015 ;;
2016 *.gz)
2017 catname="$catdir/${manpage%.*.gz}.0"
2018 ;;
2019 *)
2020 catname="$catdir/${manpage%.*}.0"
2021 ;;
2022 esac
2023 test -e "$catname" -a "$catname" -ot "$mandir/$manpage" || continue
2024 if [ "$1" = "fix" ]; then
2025 rm "$catname"
2026 failed=$(( ${failed} + $? ))
2027 msg "Removed obsolete cat page $catname"
2028 else
2029 msg "Obsolete cat page $catname"
2030 failed=1
2031 fi
2032 done
2033 exit $failed
2034 }
2035 }
2036
2037 additem catpages "remove outdated cat pages"
2038 do_catpages()
2039 {
2040 failed=0
2041 for manbase in /usr/share/man /usr/X11R6/man /usr/X11R7/man; do
2042 for sec in 1 2 3 4 5 6 7 8 9; do
2043 obsolete_catpages "$1" "${DEST_DIR}${manbase}" "${sec}"
2044 failed=$(( ${failed} + $? ))
2045 if [ "$1" = "fix" ]; then
2046 rmdir "${DEST_DIR}${manbase}/cat${sec}"/* \
2047 2>/dev/null
2048 rmdir "${DEST_DIR}${manbase}/cat${sec}" \
2049 2>/dev/null
2050 fi
2051 done
2052 done
2053 return $failed
2054 }
2055
2056 #
2057 # man.conf
2058 #
2059 additem manconf "check for a mandoc usage in /etc/man.conf"
2060 do_manconf()
2061 {
2062 [ -n "$1" ] || err 3 "USAGE: do_manconf fix|check"
2063 op="$1"
2064 failed=0
2065
2066 [ -f "${DEST_DIR}/etc/man.conf" ] || return 0
2067 if ${GREP} -w "mandoc" "${DEST_DIR}/etc/man.conf" >/dev/null 2>&1;
2068 then
2069 failed=0;
2070 else
2071 failed=1
2072 notfixed=""
2073 if [ "${op}" = "fix" ]; then
2074 notfixed="${NOT_FIXED}"
2075 fi
2076 msg "The file /etc/man.conf has not been adapted to mandoc usage; you"
2077 msg "probably want to copy a new version over. ${notfixed}"
2078 fi
2079
2080 return ${failed}
2081 }
2082
2083
2084 #
2085 # ptyfsoldnodes
2086 #
2087 additem ptyfsoldnodes "remove legacy device nodes when using ptyfs"
2088 do_ptyfsoldnodes()
2089 {
2090 [ -n "$1" ] || err 3 "USAGE: do_ptyfsoldnodes fix|check"
2091 _ptyfs_op="$1"
2092
2093 # Check whether ptyfs is in use
2094 failed=0;
2095 if ! ${GREP} -E "^ptyfs" "${DEST_DIR}/etc/fstab" > /dev/null; then
2096 msg "ptyfs is not in use"
2097 return 0
2098 fi
2099
2100 if [ ! -e "${DEST_DIR}/dev/pts" ]; then
2101 msg "ptyfs is not properly configured: missing /dev/pts"
2102 return 1
2103 fi
2104
2105 # Find the device major numbers for the pty master and slave
2106 # devices, by parsing the output from "MAKEDEV -s pty0".
2107 #
2108 # Output from MAKEDEV looks like this:
2109 # ./ttyp0 type=char device=netbsd,5,0 mode=666 gid=0 uid=0
2110 # ./ptyp0 type=char device=netbsd,6,0 mode=666 gid=0 uid=0
2111 #
2112 # Output from awk, used in the eval statement, looks like this:
2113 # maj_ptym=6; maj_ptys=5;
2114 #
2115 find_makedev
2116 eval "$(
2117 ${HOST_SH} "${MAKEDEV_DIR}/MAKEDEV" -s pty0 2>/dev/null \
2118 | ${AWK} '\
2119 BEGIN { before_re = ".*device=[a-zA-Z]*,"; after_re = ",.*"; }
2120 /ptyp0/ { maj_ptym = gensub(before_re, "", 1, $0);
2121 maj_ptym = gensub(after_re, "", 1, maj_ptym); }
2122 /ttyp0/ { maj_ptys = gensub(before_re, "", 1, $0);
2123 maj_ptys = gensub(after_re, "", 1, maj_ptys); }
2124 END { print "maj_ptym=" maj_ptym "; maj_ptys=" maj_ptys ";"; }
2125 '
2126 )"
2127 #msg "Major numbers are maj_ptym=${maj_ptym} maj_ptys=${maj_ptys}"
2128 if [ -z "$maj_ptym" ] || [ -z "$maj_ptys" ]; then
2129 msg "Cannot find device major numbers for pty master and slave"
2130 return 1
2131 fi
2132
2133 # look for /dev/[pt]ty[p-zP-T][0-9a-zA-Z], and check that they
2134 # have the expected device major numbers. ttyv* is typically not a
2135 # pty device, but we check it anyway.
2136 #
2137 # The "for d1" loop is intended to avoid overflowing ARG_MAX;
2138 # otherwise we could have used a single glob pattern.
2139 #
2140 # If there are no files that match a particular pattern,
2141 # then stat prints something like:
2142 # stat: /dev/[pt]tyx?: lstat: No such file or directory
2143 # and we ignore it. XXX: We also ignore other error messages.
2144 #
2145 _ptyfs_tmp="$(mktemp /tmp/postinstall.ptyfs.XXXXXXXX)"
2146 for d1 in p q r s t u v w x y z P Q R S T; do
2147 ${STAT} -f "%Hr %N" "${DEST_DIR}/dev/"[pt]ty${d1}? 2>&1
2148 done \
2149 | while read -r major node ; do
2150 case "$major" in
2151 ${maj_ptym}|${maj_ptys}) echo "$node" ;;
2152 esac
2153 done >"${_ptyfs_tmp}"
2154
2155 _desc="legacy device node"
2156 while read node; do
2157 if [ "${_ptyfs_op}" = "check" ]; then
2158 msg "Remove ${_desc} ${node}"
2159 failed=1
2160 else # "fix"
2161 if rm "${node}"; then
2162 msg "Removed ${_desc} ${node}"
2163 else
2164 warn "Failed to remove ${_desc} ${node}"
2165 failed=1
2166 fi
2167 fi
2168 done < "${_ptyfs_tmp}"
2169 rm "${_ptyfs_tmp}"
2170
2171 return ${failed}
2172 }
2173
2174
2175 #
2176 # varshm
2177 #
2178 additem varshm "check for a tmpfs mounted on /var/shm"
2179 do_varshm()
2180 {
2181 [ -n "$1" ] || err 3 "USAGE: do_varshm fix|check"
2182 op="$1"
2183 failed=0
2184
2185 [ -f "${DEST_DIR}/etc/fstab" ] || return 0
2186 if ${GREP} -E "^var_shm_symlink" "${DEST_DIR}/etc/rc.conf" >/dev/null 2>&1;
2187 then
2188 failed=0;
2189 elif ${GREP} -w "/var/shm" "${DEST_DIR}/etc/fstab" >/dev/null 2>&1;
2190 then
2191 failed=0;
2192 else
2193 if [ "${op}" = "check" ]; then
2194 failed=1
2195 msg "No /var/shm mount found in ${DEST_DIR}/etc/fstab"
2196 elif [ "${op}" = "fix" ]; then
2197 printf '\ntmpfs\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n' \
2198 >> "${DEST_DIR}/etc/fstab"
2199 msg "Added tmpfs with 25% ram limit as /var/shm"
2200
2201 fi
2202 fi
2203
2204 return ${failed}
2205 }
2206
2207 #
2208 # obsolete_stand
2209 #
2210 adddisableditem obsolete_stand "remove obsolete files from /stand"
2211 do_obsolete_stand()
2212 {
2213 [ -n "$1" ] || err 3 "USAGE: do_obsolete_stnd fix|check"
2214 op="$1"
2215 failed=0
2216
2217 for dir in \
2218 /stand/${MACHINE} \
2219 /stand/${MACHINE}-4xx \
2220 /stand/${MACHINE}-booke \
2221 /stand/${MACHINE}-xen \
2222 /stand/${MACHINE}pae-xen
2223 do
2224 [ -d "${DESTDIR}${dir}" ] && obsolete_stand "${dir}"
2225 done | obsolete_paths "${op}"
2226 failed=$(( ${failed} + $? ))
2227
2228 return ${failed}
2229 }
2230
2231 listarchsubdirs() {
2232 if ! $SOURCEMODE; then
2233 echo "@ARCHSUBDIRS@"
2234 else
2235 ${SED} -n -e '/ARCHDIR_SUBDIR/s/[[:space:]]//gp' \
2236 ${SRC_DIR}/compat/archdirs.mk
2237 fi
2238 }
2239
2240
2241 getarchsubdirs() {
2242 local m
2243 case ${MACHINE_ARCH} in
2244 *arm*|*aarch64*) m=arm;;
2245 x86_64) m=amd64;;
2246 *) m=${MACHINE_ARCH};;
2247 esac
2248
2249 for i in $(listarchsubdirs); do
2250 echo $i
2251 done | ${SORT} -u | ${SED} -n -e "/=${m}/s@.*=${m}/\(.*\)@\1@p"
2252 }
2253
2254 getcompatlibdirs() {
2255 for i in $(getarchsubdirs); do
2256 echo $i 1>&2
2257 if [ -d "${DEST_DIR}/usr/lib/$i" ]; then
2258 echo /usr/lib/$i
2259 fi
2260 done
2261 }
2262
2263 #
2264 # obsolete
2265 # (this item is last to allow other items to move obsolete files)
2266 #
2267 additem obsolete "remove obsolete file sets and minor libraries"
2268 do_obsolete()
2269 {
2270 [ -n "$1" ] || err 3 "USAGE: do_obsolete fix|check"
2271 op="$1"
2272 failed=0
2273
2274 ${SORT} -ru "${DEST_DIR}"/var/db/obsolete/* | obsolete_paths "${op}"
2275 failed=$(( ${failed} + $? ))
2276
2277 (
2278 obsolete_libs /lib
2279 obsolete_libs /usr/lib
2280 obsolete_libs /usr/lib/i18n
2281 obsolete_libs /usr/X11R6/lib
2282 obsolete_libs /usr/X11R7/lib
2283 for i in $(getcompatlibdirs); do
2284 obsolete_libs $i
2285 done
2286 ) | obsolete_paths "${op}"
2287 failed=$(( ${failed} + $? ))
2288
2289 return ${failed}
2290 }
2291
2292 #
2293 # end of items
2294 # ------------
2295 #
2296
2297
2298 usage()
2299 {
2300 cat 1>&2 << _USAGE_
2301 Usage: ${PROGNAME} [-s srcdir] [-x xsrcdir] [-d destdir] [-m mach] [-a arch] op [item [...]]
2302 Perform post-installation checks and/or fixes on a system's
2303 configuration files.
2304 If no items are provided, a default set of checks or fixes is applied.
2305
2306 Options:
2307 -s {srcdir|tgzfile|tempdir}
2308 Location of the source files. This may be any
2309 of the following:
2310 * A directory that contains a NetBSD source tree;
2311 * A distribution set file such as "etc.tgz" or
2312 "xetc.tgz". Pass multiple -s options to specify
2313 multiple such files;
2314 * A temporary directory in which one or both of
2315 "etc.tgz" and "xetc.tgz" have been extracted.
2316 [${SRC_DIR:-/usr/src}]
2317 -x xsrcdir Location of the X11 source files. This must be
2318 a directory that contains a NetBSD xsrc tree.
2319 [${XSRC_DIR:-/usr/src/../xsrc}]
2320 -d destdir Destination directory to check. [${DEST_DIR:-/}]
2321 -m mach MACHINE. [${MACHINE}]
2322 -a arch MACHINE_ARCH. [${MACHINE_ARCH}]
2323
2324 Operation may be one of:
2325 help Display this help.
2326 list List available items.
2327 check Perform post-installation checks on items.
2328 diff [diff(1) options ...]
2329 Similar to 'check' but also output difference of files.
2330 fix Apply fixes that 'check' determines need to be applied.
2331 usage Display this usage.
2332 _USAGE_
2333 exit 2
2334 }
2335
2336
2337 list()
2338 {
2339 echo "Default set of items (to apply if no items are provided by user):"
2340 echo " Item Description"
2341 echo " ---- -----------"
2342 for i in ${defaultitems}; do
2343 eval desc=\"\${desc_${i}}\"
2344 printf " %-12s %s\n" "${i}" "${desc}"
2345 done
2346 echo "Items disabled by default (must be requested explicitly):"
2347 echo " Item Description"
2348 echo " ---- -----------"
2349 for i in ${otheritems}; do
2350 eval desc=\"\${desc_${i}}\"
2351 printf " %-12s %s\n" "${i}" "${desc}"
2352 done
2353
2354 }
2355
2356
2357 main()
2358 {
2359 TGZLIST= # quoted list list of tgz files
2360 SRC_ARGLIST= # quoted list of one or more "-s" args
2361 SRC_DIR="${SRC_ARG}" # set default value for early usage()
2362 XSRC_DIR="${SRC_ARG}/../xsrc"
2363 N_SRC_ARGS=0 # number of "-s" args
2364 TGZMODE=false # true if "-s" specifies a tgz file
2365 DIRMODE=false # true if "-s" specified a directory
2366 SOURCEMODE=false # true if "-s" specified a source directory
2367
2368 case "$(uname -s)" in
2369 Darwin)
2370 # case sensitive match for case insensitive fs
2371 file_exists_exact=file_exists_exact
2372 ;;
2373 *)
2374 file_exists_exact=:
2375 ;;
2376 esac
2377
2378 while getopts s:x:d:m:a: ch; do
2379 case "${ch}" in
2380 s)
2381 qarg="$(shell_quote "${OPTARG}")"
2382 N_SRC_ARGS=$(( $N_SRC_ARGS + 1 ))
2383 SRC_ARGLIST="${SRC_ARGLIST}${SRC_ARGLIST:+ }-s ${qarg}"
2384 if [ -f "${OPTARG}" ]; then
2385 # arg refers to a *.tgz file.
2386 # This may happen twice, for both
2387 # etc.tgz and xetc.tgz, so we build up a
2388 # quoted list in TGZLIST.
2389 TGZMODE=true
2390 TGZLIST="${TGZLIST}${TGZLIST:+ }${qarg}"
2391 # Note that, when TGZMODE is true,
2392 # SRC_ARG is used only for printing
2393 # human-readable messages.
2394 SRC_ARG="${TGZLIST}"
2395 elif [ -d "${OPTARG}" ]; then
2396 # arg refers to a directory.
2397 # It might be a source directory, or a
2398 # directory where the sets have already
2399 # been extracted.
2400 DIRMODE=true
2401 SRC_ARG="${OPTARG}"
2402 if [ -f "${OPTARG}/etc/Makefile" ]; then
2403 SOURCEMODE=true
2404 fi
2405 else
2406 err 2 "Invalid argument for -s option"
2407 fi
2408 ;;
2409 x)
2410 if [ -d "${OPTARG}" ]; then
2411 # arg refers to a directory.
2412 XSRC_DIR="${OPTARG}"
2413 XSRC_DIR_FIX="-x ${OPTARG} "
2414 else
2415 err 2 "Not a directory for -x option"
2416 fi
2417 ;;
2418 d)
2419 DEST_DIR="${OPTARG}"
2420 ;;
2421 m)
2422 MACHINE="${OPTARG}"
2423 ;;
2424 a)
2425 MACHINE_ARCH="${OPTARG}"
2426 ;;
2427 *)
2428 usage
2429 ;;
2430 esac
2431 done
2432 shift $((${OPTIND} - 1))
2433 [ $# -gt 0 ] || usage
2434
2435 if [ "$N_SRC_ARGS" -gt 1 ] && $DIRMODE; then
2436 err 2 "Multiple -s args are allowed only with tgz files"
2437 fi
2438 if [ "$N_SRC_ARGS" -eq 0 ]; then
2439 # The default SRC_ARG was set elsewhere
2440 DIRMODE=true
2441 SOURCEMODE=true
2442 SRC_ARGLIST="-s $(shell_quote "${SRC_ARG}")"
2443 fi
2444
2445 #
2446 # If '-s' arg or args specified tgz files, extract them
2447 # to a scratch directory.
2448 #
2449 if $TGZMODE; then
2450 ETCTGZDIR="${SCRATCHDIR}/etc.tgz"
2451 echo "Note: Creating temporary directory ${ETCTGZDIR}"
2452 if ! mkdir "${ETCTGZDIR}"; then
2453 err 2 "Can't create ${ETCTGZDIR}"
2454 fi
2455 ( # subshell to localise changes to "$@"
2456 eval "set -- ${TGZLIST}"
2457 for tgz in "$@"; do
2458 echo "Note: Extracting files from ${tgz}"
2459 cat "${tgz}" | (
2460 cd "${ETCTGZDIR}" &&
2461 tar -zxf -
2462 ) || err 2 "Can't extract ${tgz}"
2463 done
2464 )
2465 SRC_DIR="${ETCTGZDIR}"
2466 else
2467 SRC_DIR="${SRC_ARG}"
2468 fi
2469
2470 [ -d "${SRC_DIR}" ] || err 2 "${SRC_DIR} is not a directory"
2471 [ -d "${DEST_DIR}" ] || err 2 "${DEST_DIR} is not a directory"
2472 [ -n "${MACHINE}" ] || err 2 "\${MACHINE} is not defined"
2473 [ -n "${MACHINE_ARCH}" ] || err 2 "\${MACHINE_ARCH} is not defined"
2474 if ! $SOURCEMODE && ! [ -f "${SRC_DIR}/etc/mtree/set.etc" ]; then
2475 err 2 "Files from the etc.tgz set are missing"
2476 fi
2477
2478 # If directories are /, clear them, so various messages
2479 # don't have leading "//". However, this requires
2480 # the use of ${foo:-/} to display the variables.
2481 #
2482 [ "${SRC_DIR}" = "/" ] && SRC_DIR=""
2483 [ "${DEST_DIR}" = "/" ] && DEST_DIR=""
2484
2485 detect_x11
2486
2487 op="$1"
2488 shift
2489
2490 case "${op}" in
2491 diff)
2492 op=check
2493 DIFF_STYLE=n # default style is RCS
2494 OPTIND=1
2495 while getopts bcenpuw ch; do
2496 case "${ch}" in
2497 c|e|n|u)
2498 if [ "${DIFF_STYLE}" != "n" -a \
2499 "${DIFF_STYLE}" != "${ch}" ]; then
2500 err 2 "conflicting output style: ${ch}"
2501 fi
2502 DIFF_STYLE="${ch}"
2503 ;;
2504 b|p|w)
2505 DIFF_OPT="${DIFF_OPT} -${ch}"
2506 ;;
2507 *)
2508 err 2 "unknown diff option"
2509 ;;
2510 esac
2511 done
2512 shift $((${OPTIND} - 1))
2513 ;;
2514 esac
2515
2516 case "${op}" in
2517
2518 usage|help)
2519 usage
2520 ;;
2521
2522 list)
2523 echo "Source directory: ${SRC_DIR:-/}"
2524 echo "Target directory: ${DEST_DIR:-/}"
2525 if $TGZMODE; then
2526 echo " (extracted from: ${SRC_ARG})"
2527 fi
2528 list
2529 ;;
2530
2531 check|fix)
2532 todo="$*"
2533 : ${todo:="${defaultitems}"}
2534
2535 # ensure that all supplied items are valid
2536 #
2537 for i in ${todo}; do
2538 eval desc=\"\${desc_${i}}\"
2539 [ -n "${desc}" ] || err 2 "Unsupported ${op} '"${i}"'"
2540 done
2541
2542 # perform each check/fix
2543 #
2544 echo "Source directory: ${SRC_DIR:-/}"
2545 if $TGZMODE; then
2546 echo " (extracted from: ${SRC_ARG})"
2547 fi
2548 echo "Target directory: ${DEST_DIR:-/}"
2549 items_passed=
2550 items_failed=
2551 for i in ${todo}; do
2552 echo "${i} ${op}:"
2553 ( eval do_${i} ${op} )
2554 if [ $? -eq 0 ]; then
2555 items_passed="${items_passed} ${i}"
2556 else
2557 items_failed="${items_failed} ${i}"
2558 fi
2559 done
2560
2561 if [ "${op}" = "check" ]; then
2562 plural="checks"
2563 else
2564 plural="fixes"
2565 fi
2566
2567 echo "${PROGNAME} ${plural} passed:${items_passed}"
2568 echo "${PROGNAME} ${plural} failed:${items_failed}"
2569 if [ -n "${items_failed}" ]; then
2570 exitstatus=1;
2571 if [ "${op}" = "check" ]; then
2572 [ "$MACHINE" = "$(uname -m)" ] && m= || m=" -m $MACHINE"
2573 cat <<_Fix_me_
2574 To fix, run:
2575 ${HOST_SH} ${0} ${SRC_ARGLIST} ${XSRC_DIR_FIX}-d ${DEST_DIR:-/}$m fix${items_failed}
2576 Note that this may overwrite local changes.
2577 _Fix_me_
2578 fi
2579 fi
2580
2581 ;;
2582
2583 *)
2584 warn "Unknown operation '"${op}"'"
2585 usage
2586 ;;
2587
2588 esac
2589 }
2590
2591 if [ -n "$POSTINSTALL_FUNCTION" ]; then
2592 eval "$POSTINSTALL_FUNCTION"
2593 exit 0
2594 fi
2595
2596 # defaults
2597 #
2598 PROGNAME="${0##*/}"
2599 SRC_ARG="/usr/src"
2600 DEST_DIR="/"
2601 : ${MACHINE:="$( uname -m )"} # assume native build if $MACHINE is not set
2602 : ${MACHINE_ARCH:="$( uname -p )"}# assume native build if not set
2603
2604 DIFF_STYLE=
2605 NOT_FIXED=" (FIX MANUALLY)"
2606 SCRATCHDIR="$( mkdtemp )" || err 2 "Can't create scratch directory"
2607 trap "/bin/rm -rf \"\${SCRATCHDIR}\" ; exit 0" 1 2 3 15 # HUP INT QUIT TERM
2608
2609 umask 022
2610 exec 3>/dev/null
2611 exec 4>/dev/null
2612 exitstatus=0
2613
2614 main "$@"
2615 /bin/rm -rf "${SCRATCHDIR}"
2616 exit $exitstatus
2617