Home | History | Annotate | Line # | Download | only in postinstall
postinstall.in revision 1.22
      1 #!/bin/sh
      2 #
      3 # $NetBSD: postinstall.in,v 1.22 2020/05/31 13:45:47 roy 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 		_files=$(find "${_dir}" \( \( ! -user "${_user}" \) -o \
    742 		                \( ! -group "${_grp}" \) \) )
    743 		_error=$?
    744 		if [ ! -z "$_files" ] || [ $_error != 0 ]; then
    745 			msg "${_dir} and contents not all owned by" \
    746 			    "${_user}:${_grp}"
    747 			return 1
    748 		else
    749 			return 0
    750 		fi
    751 	elif [ "${_op}" = "fix" ]; then
    752 		find "${_dir}" \( \( ! -user "${_user}" \) -o \
    753 		\( ! -group "${_grp}" \) \) \
    754 		-exec chown "${_user}:${_grp}" -- {} \;
    755 	fi
    756 }
    757 
    758 # get_makevar var [var ...]
    759 #	Retrieve the value of a user-settable system make variable
    760 get_makevar()
    761 {
    762 	$SOURCEMODE || err 3 "get_makevar must be used in source mode"
    763 	[ $# -eq 0 ] && err 3 "USAGE: get_makevar var [var ...]"
    764 
    765 	for _var in "$@"; do
    766 		_value="$(echo '.include <bsd.own.mk>' | \
    767 		    ${MAKE} -f - -V "\${${_var}}")"
    768 
    769 		eval ${_var}=\"\${_value}\"
    770 	done
    771 }
    772 
    773 # detect_x11
    774 #	Detect if X11 components should be analysed and set values of
    775 #	relevant variables.
    776 detect_x11()
    777 {
    778 	if $SOURCEMODE; then
    779 		get_makevar MKX11 X11ROOTDIR X11SRCDIR
    780 	else
    781 		if [ -f "${SRC_DIR}/etc/mtree/set.xetc" ]; then
    782 			MKX11=yes
    783 			X11ROOTDIR=/this/value/isnt/used/yet
    784 		else
    785 			MKX11=no
    786 			X11ROOTDIR=
    787 		fi
    788 		X11SRCDIR=/nonexistent/xsrc
    789 	fi
    790 }
    791 
    792 #
    793 #	find out where MAKEDEV lives, set MAKEDEV_DIR appropriately
    794 #
    795 find_makedev()
    796 {
    797 	if [ -e "${DEST_DIR}/dev/MAKEDEV" ]; then
    798 		MAKEDEV_DIR="${DEST_DIR}/dev"
    799 	elif [ -e "${DEST_DIR}/etc/MAKEDEV" ]; then
    800 		MAKEDEV_DIR="${DEST_DIR}/etc"
    801 	else
    802 		MAKEDEV_DIR="${DEST_DIR}/dev"
    803 	fi
    804 }
    805 
    806 
    807 #
    808 #	items
    809 #	-----
    810 #
    811 
    812 #
    813 #	Bluetooth
    814 #
    815 
    816 additem bluetooth "Bluetooth configuration is up to date"
    817 do_bluetooth()
    818 {
    819 	[ -n "$1" ] || err 3 "USAGE: do_bluetooth fix|check"
    820 	op="$1"
    821 	failed=0
    822 
    823 	populate_dir "${op}" true \
    824 		"${SRC_DIR}/etc/bluetooth" "${DEST_DIR}/etc/bluetooth" 644 \
    825 		hosts protocols btattach.conf btdevctl.conf
    826 	failed=$(( ${failed} + $? ))
    827 
    828 	move_file "${op}" "${DEST_DIR}/var/db/btdev.xml" \
    829 			"${DEST_DIR}/var/db/btdevctl.plist"
    830 	failed=$(( ${failed} + $? ))
    831 
    832 	notfixed=""
    833 	if [ "${op}" = "fix" ]; then
    834 		notfixed="${NOT_FIXED}"
    835 	fi
    836 	for _v in btattach btconfig btdevctl; do
    837 		if rcvar_is_enabled "${_v}"; then
    838 			msg \
    839     "${_v} is obsolete in rc.conf(5)${notfixed}: use bluetooth=YES"
    840 			failed=$(( ${failed} + 1 ))
    841 		fi
    842 	done
    843 
    844 	return ${failed}
    845 }
    846 
    847 #
    848 #	ddbonpanic
    849 #
    850 additem ddbonpanic "verify ddb.onpanic is configured in sysctl.conf"
    851 do_ddbonpanic()
    852 {
    853 	[ -n "$1" ] || err 3 "USAGE: do_ddbonpanic  fix|check"
    854 
    855 	if ${GREP} -E '^#*[[:space:]]*ddb\.onpanic[[:space:]]*\??=[[:space:]]*[[:digit:]]+' \
    856 		"${DEST_DIR}/etc/sysctl.conf" >/dev/null 2>&1
    857 	then
    858 		result=0
    859 	else
    860 		if [ "$1" = check ]; then
    861 			msg \
    862     "The ddb.onpanic behaviour is not explicitly specified in /etc/sysctl.conf"
    863 			result=1
    864 		else
    865 			echo >> "${DEST_DIR}/etc/sysctl.conf"
    866 			${SED} < "${SRC_DIR}/etc/sysctl.conf" \
    867 			   -e '/^ddb\.onpanic/q' | \
    868 			       ${SED} -e '1,/^$/d' >> \
    869 			    "${DEST_DIR}/etc/sysctl.conf"
    870 			result=$?
    871 		fi
    872 	fi
    873 	return ${result}
    874 }
    875 
    876 #
    877 #	defaults
    878 #
    879 additem defaults "/etc/defaults/ being up to date"
    880 do_defaults()
    881 {
    882 	[ -n "$1" ] || err 3 "USAGE: do_defaults  fix|check"
    883 	local op="$1"
    884 	local failed=0
    885 	local etcsets=$(getetcsets)
    886 
    887 	local rc_exclude_scripts=""
    888 	if $SOURCEMODE; then
    889 		# For most architectures rc.conf(5) should be the same as the
    890 		# one obtained from a source directory, except for the ones
    891 		# that have an append file for it.
    892 		local rc_conf_app="${SRC_DIR}/etc/etc.${MACHINE}/rc.conf.append"
    893 		if [ -f "${rc_conf_app}" ]; then
    894 			rc_exclude_scripts="rc.conf"
    895 
    896 			# Generate and compare the correct rc.conf(5) file
    897 			mkdir "${SCRATCHDIR}/defaults"
    898 
    899 			cat "${SRC_DIR}/etc/defaults/rc.conf" "${rc_conf_app}" \
    900 			    > "${SCRATCHDIR}/defaults/rc.conf"
    901 
    902 			compare_dir "${op}" "${SCRATCHDIR}/defaults" \
    903 			    "${DEST_DIR}/etc/defaults" \
    904 			    444 \
    905 			    "rc.conf"
    906 			failed=$(( ${failed} + $? ))
    907 		fi
    908 	fi
    909 
    910 	find_file_in_dirlist pf.boot.conf "pf.boot.conf" \
    911 	    "${SRC_DIR}/usr.sbin/pf/etc/defaults" "${SRC_DIR}/etc/defaults" \
    912 	    || return 1
    913 	# ${dir} is set by find_file_in_dirlist()
    914 	compare_dir "$op" "${dir}" "${DEST_DIR}/etc/defaults" 444 pf.boot.conf
    915 	failed=$(( ${failed} + $? ))
    916 
    917 	rc_exclude_scripts="${rc_exclude_scripts} pf.boot.conf"
    918 
    919 	local rc_default_conf_files="$(select_set_files /etc/defaults/ \
    920 	    "/etc/defaults/\([^[:space:]]*\.conf\)" ${etcsets} | \
    921 	    exclude ${rc_exclude_scripts})"
    922 	compare_dir "$op" "${SRC_DIR}/etc/defaults" "${DEST_DIR}/etc/defaults" \
    923 		444 \
    924 		${rc_default_conf_files}
    925 	failed=$(( ${failed} + $? ))
    926 
    927 
    928 	return ${failed}
    929 }
    930 
    931 #
    932 #	dhcpcd
    933 #
    934 additem dhcpcd "dhcpcd configuration is up to date"
    935 do_dhcpcd()
    936 {
    937 	[ -n "$1" ] || err 3 "USAGE: do_dhcpcd fix|check"
    938 	op="$1"
    939 	failed=0
    940 
    941 	find_file_in_dirlist dhcpcd.conf "dhcpcd.conf" \
    942 	    "${SRC_DIR}/external/bsd/dhcpcd/dist/src" \
    943 	    "${SRC_DIR}/etc" || return 1
    944 			# ${dir} is set by find_file_in_dirlist()
    945 	populate_dir "$op" true "${dir}" "${DEST_DIR}/etc" 644 dhcpcd.conf
    946 	failed=$(( ${failed} + $? ))
    947 
    948 	check_dir "${op}" "${DEST_DIR}/var/db/dhcpcd" 755
    949 	failed=$(( ${failed} + $? ))
    950 
    951 	move_file "${op}" \
    952 		"${DEST_DIR}/etc/dhcpcd.duid" \
    953 		"${DEST_DIR}/var/db/dhcpcd/duid"
    954 	failed=$(( ${failed} + $? ))
    955 
    956 	move_file "${op}" \
    957 		"${DEST_DIR}/etc/dhcpcd.secret" \
    958 		"${DEST_DIR}/var/db/dhcpcd/secret"
    959 	failed=$(( ${failed} + $? ))
    960 
    961 	move_file "${op}" \
    962 		"${DEST_DIR}/var/db/dhcpcd-rdm.monotonic" \
    963 		"${DEST_DIR}/var/db/dhcpcd/rdm_monotonic"
    964 	failed=$(( ${failed} + $? ))
    965 
    966 	for lease in "${DEST_DIR}/var/db/dhcpcd-"*.lease*; do
    967 		[ -f "${lease}" ] || continue
    968 		new_lease=$(basename "${lease}" | ${SED} -e 's/dhcpcd-//')
    969 		new_lease="${DEST_DIR}/var/db/dhcpcd/${new_lease}"
    970 		move_file "${op}" "${lease}" "${new_lease}"
    971 		failed=$(( ${failed} + $? ))
    972 	done
    973 
    974 	chroot_dir="${DEST_DIR}/var/chroot/dhcpcd"
    975 	move_file "${op}" \
    976 		"${chroot_dir}/var/db/dhcpcd/duid" \
    977 		"${DEST_DIR}/var/db/dhcpcd/duid"
    978 	failed=$(( ${failed} + $? ))
    979 
    980 	move_file "${op}" \
    981 		"${chroot_dir}/var/db/dhcpcd/secret" \
    982 		"${DEST_DIR}/var/db/dhcpcd/secret"
    983 	failed=$(( ${failed} + $? ))
    984 
    985 	move_file "${op}" \
    986 		"${chroot_dir}/var/db/dhcpcd/rdm_monotonic" \
    987 		"${DEST_DIR}/var/db/dhcpcd/rdm_monotonic"
    988 	failed=$(( ${failed} + $? ))
    989 
    990 	for lease in "${chroot_dir}/var/db/dhcpcd/"*.lease*; do
    991 		[ -f "${lease}" ] || continue
    992 		new_lease="${DEST_DIR}/var/db/dhcpcd/$(basename ${lease})"
    993 		move_file "${op}" "${lease}" "${new_lease}"
    994 		failed=$(( ${failed} + $? ))
    995 	done
    996 
    997 	# Ensure chroot is now empty
    998 	for dir in \
    999 		$(find ${chroot_dir} -type f) \
   1000 		$(find ${chroot_dir} -type d -mindepth 1 | sort -r)
   1001 	do
   1002 		echo "/var/chroot/dhcpcd${dir##${chroot_dir}}"
   1003 	done | obsolete_paths "${op}"
   1004 	failed=$(( ${failed} + $? ))
   1005 
   1006 	contents_owner "${op}" "${DEST_DIR}/var/db/dhcpcd" root wheel
   1007 	failed=$(( ${failed} + $? ))
   1008 
   1009 	return ${failed}
   1010 }
   1011 
   1012 #
   1013 #	dhcpcdrundir
   1014 #
   1015 additem dhcpcdrundir "accidentaly created /@RUNDIR@ does not exist"
   1016 do_dhcpcdrundir()
   1017 {
   1018 	[ -n "$1" ] || err 3 "USAGE: do_dhcpcdrundir fix|check"
   1019 	op="$1"
   1020 	failed=0
   1021 
   1022 	if [ -d "${DEST_DIR}/@RUNDIR@" ]; then
   1023 		if [ "${op}" = "check" ]; then
   1024 			msg "Remove eroneously created /@RUNDIR@"
   1025 			failed=1
   1026 		elif ! rm -r "${DEST_DIR}/@RUNDIR@"; then
   1027 			msg "Failed to remove ${DEST_DIR}/@RUNDIR@"
   1028 			failed=1
   1029 		else
   1030 			msg "Removed eroneously created ${DEST_DIR}/@RUNDIR@"
   1031 		fi
   1032 	fi
   1033 	return ${failed}
   1034 }
   1035 
   1036 #
   1037 #	envsys
   1038 #
   1039 additem envsys "envsys configuration is up to date"
   1040 do_envsys()
   1041 {
   1042 	[ -n "$1" ] || err 3 "USAGE: do_envsys fix|check"
   1043 	local op="$1"
   1044 	local failed=0
   1045 	local etcsets=$(getetcsets)
   1046 
   1047 	populate_dir "$op" true "${SRC_DIR}/etc" "${DEST_DIR}/etc" 644 \
   1048 		envsys.conf
   1049 	failed=$(( ${failed} + $? ))
   1050 
   1051 	local powerd_scripts="$(select_set_files /etc/powerd/scripts/ \
   1052 	    "/etc/powerd/scripts/\([^[:space:]/]*\)" ${etcsets})"
   1053 
   1054 	populate_dir "$op" true "${SRC_DIR}/etc/powerd/scripts" \
   1055 		"${DEST_DIR}/etc/powerd/scripts" \
   1056 		555 \
   1057 		${powerd_scripts}
   1058 	failed=$(( ${failed} + $? ))
   1059 
   1060 	return ${failed}
   1061 }
   1062 
   1063 #
   1064 #	autofs config files
   1065 #
   1066 additem autofsconfig "automounter configuration files"
   1067 do_autofsconfig()
   1068 {
   1069 	[ -n "$1" ] || err 3 "USAGE: do_autofsconfig fix|check"
   1070 	local autofs_files="
   1071 include_ldap
   1072 include_nis
   1073 special_hosts
   1074 special_media
   1075 special_noauto
   1076 special_null 
   1077 "
   1078 	op="$1"
   1079 	failed=0
   1080 	if [ "$op" = "fix" ]; then
   1081 		mkdir -p "${DEST_DIR}/etc/autofs"
   1082 	fi
   1083 	failed=$(( ${failed} + $? ))
   1084 	populate_dir "$op" false "${SRC_DIR}/etc" \
   1085 	    "${DEST_DIR}/etc" \
   1086 	    644 \
   1087 	    auto_master
   1088 	failed=$(( ${failed} + $? ))
   1089 	populate_dir "$op" false "${SRC_DIR}/etc/autofs" \
   1090 	    "${DEST_DIR}/etc/autofs" \
   1091 	    644 \
   1092 	    ${autofs_files}
   1093 	return ${failed}
   1094 }
   1095 
   1096 
   1097 #
   1098 #	X11 fontconfig
   1099 #
   1100 additem fontconfig "X11 font configuration is up to date"
   1101 do_fontconfig()
   1102 {
   1103 	[ -n "$1" ] || err 3 "USAGE: do_fontconfig fix|check"
   1104 	op="$1"
   1105 	failed=0
   1106 
   1107 	# First, check for updates we can handle.
   1108 	if ! $SOURCEMODE; then
   1109 		FONTCONFIG_DIR="${SRC_DIR}/etc/fonts/conf.avail"
   1110 	else
   1111 		FONTCONFIG_DIR="${XSRC_DIR}/external/mit/fontconfig/dist/conf.d"
   1112 	fi
   1113 
   1114 	if [ ! -d "${FONTCONFIG_DIR}" ]; then
   1115 		msg "${FONTCONFIG_DIR} is not a directory; skipping check"
   1116 		return 0
   1117 	fi
   1118 	local regular_fonts="
   1119 10-autohint.conf
   1120 10-no-sub-pixel.conf
   1121 10-scale-bitmap-fonts.conf
   1122 10-sub-pixel-bgr.conf
   1123 10-sub-pixel-rgb.conf
   1124 10-sub-pixel-vbgr.conf
   1125 10-sub-pixel-vrgb.conf
   1126 10-unhinted.conf
   1127 11-lcdfilter-default.conf
   1128 11-lcdfilter-legacy.conf
   1129 11-lcdfilter-light.conf
   1130 20-unhint-small-vera.conf
   1131 25-unhint-nonlatin.conf
   1132 30-metric-aliases.conf
   1133 40-nonlatin.conf
   1134 45-generic.conf
   1135 45-latin.conf
   1136 49-sansserif.conf
   1137 50-user.conf
   1138 51-local.conf
   1139 60-generic.conf
   1140 60-latin.conf
   1141 65-fonts-persian.conf
   1142 65-khmer.conf
   1143 65-nonlatin.conf
   1144 69-unifont.conf
   1145 70-no-bitmaps.conf
   1146 70-yes-bitmaps.conf
   1147 80-delicious.conf
   1148 90-synthetic.conf
   1149 "
   1150 	populate_dir "$op" false "${FONTCONFIG_DIR}" \
   1151 	    "${DEST_DIR}/etc/fonts/conf.avail" \
   1152 	    444 \
   1153 	    ${regular_fonts}
   1154 	failed=$(( ${failed} + $? ))
   1155 
   1156 	if ! $SOURCEMODE; then
   1157 		FONTS_DIR="${SRC_DIR}/etc/fonts"
   1158 	else
   1159 		FONTS_DIR="${SRC_DIR}/external/mit/xorg/lib/fontconfig/etc"
   1160 	fi
   1161 
   1162 	populate_dir "$op" false "${FONTS_DIR}" "${DEST_DIR}/etc/fonts" 444 \
   1163 		fonts.conf
   1164 	failed=$(( ${failed} + $? ))
   1165 
   1166 	# We can't modify conf.d easily; someone might have removed a file.
   1167 
   1168 	# Look for old files that need to be deleted.
   1169 	obsolete_fonts="
   1170 10-autohint.conf
   1171 10-no-sub-pixel.conf
   1172 10-sub-pixel-bgr.conf
   1173 10-sub-pixel-rgb.conf
   1174 10-sub-pixel-vbgr.conf
   1175 10-sub-pixel-vrgb.conf
   1176 10-unhinted.conf
   1177 25-unhint-nonlatin.conf
   1178 65-khmer.conf
   1179 70-no-bitmaps.conf
   1180 70-yes-bitmaps.conf
   1181 "
   1182 	failed_fonts=""
   1183 	for i in ${obsolete_fonts}; do
   1184 	    if [ -f "${DEST_DIR}/etc/fonts/conf.d/$i" ]; then
   1185 		    conf_d_failed=1
   1186 		    failed_fonts="$failed_fonts $i"
   1187 	    fi
   1188 	done
   1189 
   1190 	if [ -n "$failed_fonts" ]; then
   1191 		msg \
   1192     "Broken fontconfig configuration found; please delete these files:"
   1193 		msg "[$failed_fonts]"
   1194 		failed=$(( ${failed} + 1 ))
   1195 	fi
   1196 
   1197 	return ${failed}
   1198 }
   1199 
   1200 #
   1201 #	gid
   1202 #
   1203 additem gid "required groups in /etc/group"
   1204 do_gid()
   1205 {
   1206 	[ -n "$1" ] || err 3 "USAGE: do_gid  fix|check"
   1207 
   1208 	check_ids "$1" groups "${DEST_DIR}/etc/group" \
   1209 	    "${SRC_DIR}/etc/group" 14 \
   1210 	    named ntpd sshd SKIP _pflogd _rwhod staff _proxy _timedc \
   1211 	    _sdpd _httpd _mdnsd _tests _tcpdump _tss _gpio _rtadvd SKIP \
   1212 	    _unbound _nsd nvmm _dhcpcd
   1213 }
   1214 
   1215 #
   1216 #	gpio
   1217 #
   1218 additem gpio "gpio configuration is up to date"
   1219 do_gpio()
   1220 {
   1221 	[ -n "$1" ] || err 3 "USAGE: do_gpio fix|check"
   1222 	op="$1"
   1223 	failed=0
   1224 
   1225 	populate_dir "$op" true "${SRC_DIR}/etc" "${DEST_DIR}/etc" 644 \
   1226 		gpio.conf
   1227 	failed=$(( ${failed} + $? ))
   1228 
   1229 	return ${failed}
   1230 }
   1231 
   1232 #
   1233 #	hosts
   1234 #
   1235 additem hosts "/etc/hosts being up to date"
   1236 do_hosts()
   1237 {
   1238 	[ -n "$1" ] || err 3 "USAGE: do_hosts  fix|check"
   1239 
   1240 	modify_file "$1" "${DEST_DIR}/etc/hosts" "${SCRATCHDIR}/hosts" '
   1241 		/^(127\.0\.0\.1|::1)[ 	]+[^\.]*$/ {
   1242 			print $0, "localhost."
   1243 			next
   1244 		}
   1245 		{ print }
   1246 	'
   1247 	return $?
   1248 }
   1249 
   1250 #
   1251 #	iscsi
   1252 #
   1253 additem iscsi "/etc/iscsi is populated"
   1254 do_iscsi()
   1255 {
   1256 	[ -n "$1" ] || err 3 "USAGE: do_iscsi  fix|check"
   1257 
   1258 	populate_dir "${op}" true \
   1259 	    "${SRC_DIR}/etc/iscsi" "${DEST_DIR}/etc/iscsi" 600 auths
   1260 	populate_dir "${op}" true \
   1261 	    "${SRC_DIR}/etc/iscsi" "${DEST_DIR}/etc/iscsi" 644 targets
   1262 	return $?
   1263 }
   1264 
   1265 #
   1266 #	makedev
   1267 #
   1268 additem makedev "/dev/MAKEDEV being up to date"
   1269 do_makedev()
   1270 {
   1271 	[ -n "$1" ] || err 3 "USAGE: do_makedev   fix|check"
   1272 	failed=0
   1273 
   1274 	if [ -f "${SRC_DIR}/etc/MAKEDEV.tmpl" ]; then
   1275 			# generate MAKEDEV from source if source is available
   1276 		env MACHINE="${MACHINE}" \
   1277 		    MACHINE_ARCH="${MACHINE_ARCH}" \
   1278 		    NETBSDSRCDIR="${SRC_DIR}" \
   1279 		    ${AWK} -f "${SRC_DIR}/etc/MAKEDEV.awk" \
   1280 		    "${SRC_DIR}/etc/MAKEDEV.tmpl" > "${SCRATCHDIR}/MAKEDEV"
   1281 	fi
   1282 
   1283 	find_file_in_dirlist MAKEDEV "MAKEDEV" \
   1284 	    "${SCRATCHDIR}" "${SRC_DIR}/dev" \
   1285 	    || return 1
   1286 			# ${dir} is set by find_file_in_dirlist()
   1287 	find_makedev
   1288 	compare_dir "$1" "${dir}" "${MAKEDEV_DIR}" 555 MAKEDEV
   1289 	failed=$(( ${failed} + $? ))
   1290 
   1291 	find_file_in_dirlist MAKEDEV.local "MAKEDEV.local" \
   1292 	    "${SRC_DIR}/etc" "${SRC_DIR}/dev" \
   1293 	    || return 1
   1294 			# ${dir} is set by find_file_in_dirlist()
   1295 	compare_dir "$1" "${dir}" "${DEST_DIR}/dev" 555 MAKEDEV.local
   1296 	failed=$(( ${failed} + $? ))
   1297 
   1298 	return ${failed}
   1299 }
   1300 
   1301 #
   1302 #	motd
   1303 #
   1304 additem motd "contents of motd"
   1305 do_motd()
   1306 {
   1307 	[ -n "$1" ] || err 3 "USAGE: do_motd  fix|check"
   1308 
   1309 	if ${GREP} -i 'http://www.NetBSD.org/Misc/send-pr.html' \
   1310 		"${DEST_DIR}/etc/motd" >/dev/null 2>&1 \
   1311 	    || ${GREP} -i 'https*://www.NetBSD.org/support/send-pr.html' \
   1312 		"${DEST_DIR}/etc/motd" >/dev/null 2>&1
   1313 	then
   1314 		tmp1="$(mktemp /tmp/postinstall.motd.XXXXXXXX)"
   1315 		tmp2="$(mktemp /tmp/postinstall.motd.XXXXXXXX)"
   1316 		${SED} '1,2d' <"${SRC_DIR}/etc/motd" >"${tmp1}"
   1317 		${SED} '1,2d' <"${DEST_DIR}/etc/motd" >"${tmp2}"
   1318 
   1319 		if [ "$1" = check ]; then
   1320 			cmp -s "${tmp1}" "${tmp2}"
   1321 			result=$?
   1322 			if [ "${result}" -ne 0 ]; then
   1323 				msg \
   1324     "Bug reporting messages do not seem to match the installed release"
   1325 			fi
   1326 		else
   1327 			head -n 2 "${DEST_DIR}/etc/motd" >"${tmp1}"
   1328 			${SED} '1,2d' <"${SRC_DIR}/etc/motd" >>"${tmp1}"
   1329 			cp "${tmp1}" "${DEST_DIR}/etc/motd"
   1330 			result=0
   1331 		fi
   1332 
   1333 		rm -f "${tmp1}" "${tmp2}"
   1334 	else
   1335 		result=0
   1336 	fi
   1337 
   1338 	return ${result}
   1339 }
   1340 
   1341 #
   1342 #	mtree
   1343 #
   1344 additem mtree "/etc/mtree/ being up to date"
   1345 do_mtree()
   1346 {
   1347 	[ -n "$1" ] || err 3 "USAGE: do_mtree  fix|check"
   1348 	failed=0
   1349 
   1350 	compare_dir "$1" "${SRC_DIR}/etc/mtree" "${DEST_DIR}/etc/mtree" 444 special
   1351 	failed=$(( ${failed} + $? ))
   1352 
   1353 	if ! $SOURCEMODE; then
   1354 		MTREE_DIR="${SRC_DIR}/etc/mtree"
   1355 	else
   1356 		/bin/rm -rf "${SCRATCHDIR}/obj"
   1357 		mkdir "${SCRATCHDIR}/obj"
   1358 		${MAKE} -s -C "${SRC_DIR}/etc/mtree" TOOL_AWK="${AWK}" \
   1359 		    MAKEOBJDIR="${SCRATCHDIR}/obj" emit_dist_file > \
   1360 		    "${SCRATCHDIR}/NetBSD.dist"
   1361 		MTREE_DIR="${SCRATCHDIR}"
   1362 		/bin/rm -rf "${SCRATCHDIR}/obj"
   1363 	fi
   1364 	compare_dir "$1" "${MTREE_DIR}" "${DEST_DIR}/etc/mtree" 444 NetBSD.dist
   1365 	failed=$(( ${failed} + $? ))
   1366 
   1367 	return ${failed}
   1368 }
   1369 
   1370 #
   1371 #	named
   1372 #
   1373 additem named "named configuration update"
   1374 do_named()
   1375 {
   1376 	[ -n "$1" ] || err 3 "USAGE: do_named  fix|check"
   1377 	op="$1"
   1378 
   1379 	move_file "${op}" \
   1380 		"${DEST_DIR}/etc/namedb/named.conf" \
   1381 		"${DEST_DIR}/etc/named.conf"
   1382 
   1383 	compare_dir "${op}" "${SRC_DIR}/etc/namedb" "${DEST_DIR}/etc/namedb" \
   1384 		644 \
   1385 		root.cache
   1386 }
   1387 
   1388 #
   1389 #	pam
   1390 #
   1391 additem pam "/etc/pam.d is populated"
   1392 do_pam()
   1393 {
   1394 	[ -n "$1" ] || err 3 "USAGE: do_pam  fix|check"
   1395 	op="$1"
   1396 	failed=0
   1397 
   1398 	populate_dir "${op}" true "${SRC_DIR}/etc/pam.d" \
   1399 		"${DEST_DIR}/etc/pam.d" 644 \
   1400 		README cron display_manager ftpd gdm imap kde login other \
   1401 		passwd pop3 ppp racoon rexecd rsh sshd su system telnetd \
   1402 		xdm xserver
   1403 
   1404 	failed=$(( ${failed} + $? ))
   1405 
   1406 	return ${failed}
   1407 }
   1408 
   1409 #
   1410 #	periodic
   1411 #
   1412 additem periodic "/etc/{daily,weekly,monthly,security} being up to date"
   1413 do_periodic()
   1414 {
   1415 	[ -n "$1" ] || err 3 "USAGE: do_periodic  fix|check"
   1416 
   1417 	compare_dir "$1" "${SRC_DIR}/etc" "${DEST_DIR}/etc" 644 \
   1418 		daily weekly monthly security
   1419 }
   1420 
   1421 #
   1422 #	pf
   1423 #
   1424 additem pf "pf configuration being up to date"
   1425 do_pf()
   1426 {
   1427 	[ -n "$1" ] || err 3 "USAGE: do_pf  fix|check"
   1428 	op="$1"
   1429 	failed=0
   1430 
   1431 	find_file_in_dirlist pf.os "pf.os" \
   1432 	    "${SRC_DIR}/dist/pf/etc" "${SRC_DIR}/etc" \
   1433 	    || return 1
   1434 			# ${dir} is set by find_file_in_dirlist()
   1435 	populate_dir "${op}" true \
   1436 	    "${dir}" "${DEST_DIR}/etc" 644 \
   1437 	    pf.conf
   1438 	failed=$(( ${failed} + $? ))
   1439 
   1440 	compare_dir "${op}" "${dir}" "${DEST_DIR}/etc" 444 pf.os
   1441 	failed=$(( ${failed} + $? ))
   1442 
   1443 	return ${failed}
   1444 }
   1445 
   1446 #
   1447 #	pwd_mkdb
   1448 #
   1449 additem pwd_mkdb "passwd database version"
   1450 do_pwd_mkdb()
   1451 {
   1452 	[ -n "$1" ] || err 3 "USAGE: do_pwd_mkdb  fix|check"
   1453 	op="$1"
   1454 	failed=0
   1455 
   1456 	# XXX Ideally, we should figure out the endianness of the
   1457 	# target machine, and add "-E B"/"-E L" to the db(1) flags,
   1458 	# and "-B"/"-L" to the pwd_mkdb(8) flags if the target is not
   1459 	# the same as the host machine.  It probably doesn't matter,
   1460 	# because we don't expect "postinstall fix pwd_mkdb" to be
   1461 	# invoked during a cross build.
   1462 
   1463 	set -- $(${DB} -q -Sb -Ub -To -N hash "${DEST_DIR}/etc/pwd.db" \
   1464 		'VERSION\0')
   1465 	case "$2" in
   1466 	'\001\000\000\000') return 0 ;; # version 1, little-endian
   1467 	'\000\000\000\001') return 0 ;; # version 1, big-endian
   1468 	esac
   1469 
   1470 	if [ "${op}" = "check" ]; then
   1471 		msg "Update format of passwd database"
   1472 		failed=1
   1473 	elif ! ${PWD_MKDB} -V 1 -d "${DEST_DIR:-/}" \
   1474 			"${DEST_DIR}/etc/master.passwd";
   1475 	then
   1476 		msg "Can't update format of passwd database"
   1477 		failed=1
   1478 	else
   1479 		msg "Updated format of passwd database"
   1480 	fi
   1481 
   1482 	return ${failed}
   1483 }
   1484 
   1485 #
   1486 #	rc
   1487 #
   1488 
   1489 # There is no info in src/distrib or /etc/mtree which rc* files
   1490 # can be overwritten unconditionally on upgrade. See PR/54741.
   1491 rc_644_files="
   1492 rc
   1493 rc.subr
   1494 rc.shutdown
   1495 "
   1496 
   1497 rc_obsolete_vars="
   1498 amd amd_master
   1499 btcontrol btcontrol_devices
   1500 critical_filesystems critical_filesystems_beforenet
   1501 mountcritlocal mountcritremote
   1502 network ip6forwarding
   1503 network nfsiod_flags
   1504 sdpd sdpd_control
   1505 sdpd sdpd_groupname
   1506 sdpd sdpd_username
   1507 sysctl defcorename
   1508 "
   1509 
   1510 update_rc()
   1511 {
   1512 	local op=$1
   1513 	local dir=$2
   1514 	local name=$3
   1515 	local bindir=$4
   1516 	local rcdir=$5
   1517 
   1518 	if [ ! -x "${DEST_DIR}/${bindir}/${name}" ]; then
   1519 		return 0
   1520 	fi
   1521 
   1522 	if ! find_file_in_dirlist "${name}" "${name}" \
   1523 	    "${rcdir}" "${SRC_DIR}/etc/rc.d"; then
   1524 		return 1
   1525 	fi
   1526 	populate_dir "${op}" false "${dir}" "${DEST_DIR}/etc/rc.d" 555 "${name}"
   1527 	return $?
   1528 }
   1529 
   1530 # select non-obsolete files in a sets file
   1531 # $1: directory pattern
   1532 # $2: file pattern
   1533 # $3: filename
   1534 select_set_files()
   1535 {
   1536 	local qdir="$(echo $1 | ${SED} -e s@/@\\\\/@g -e s/\\./\\\\./g)"
   1537 	${SED} -n -e /obsolete/d \
   1538 	    -e "/^\.${qdir}/s@^.$2[[:space:]].*@\1@p" $3
   1539 }
   1540 
   1541 # select obsolete files in a sets file
   1542 # $1: directory pattern
   1543 # $2: file pattern
   1544 # $3: setname
   1545 select_obsolete_files()
   1546 {
   1547 	if $SOURCEMODE; then
   1548 		${SED} -n -e "/obsolete/s@\.$1$2[[:space:]].*@\1@p" \
   1549 		    ${SRC_DIR}/distrib/sets/lists/$3/mi
   1550 		return
   1551 	fi
   1552 
   1553 	# On upgrade builds we don't extract the "etc" set so we
   1554 	# try to use the source set instead. See PR/54730 for
   1555 	# ways to better handle this.
   1556 
   1557 	local obsolete_dir
   1558 
   1559 	if [ $3 = "etc" ] ;then
   1560 		obsolete_dir=${SRC_DIR}/var/db/obsolete
   1561 	else
   1562 		obsolete_dir=${DEST_DIR}/var/db/obsolete
   1563 	fi
   1564 	${SED} -n -e "s@\.$1$2\$@\1@p" "${obsolete_dir}/$3"
   1565 }
   1566 
   1567 getetcsets()
   1568 {
   1569 	if $SOURCEMODE; then
   1570 		echo "${SRC_DIR}/distrib/sets/lists/etc/mi"
   1571 	else
   1572 		echo "${SRC_DIR}/etc/mtree/set.etc"
   1573 	fi
   1574 }
   1575 
   1576 additem rc "/etc/rc* and /etc/rc.d/ being up to date"
   1577 do_rc()
   1578 {
   1579 	[ -n "$1" ] || err 3 "USAGE: do_rc  fix|check"
   1580 	local op="$1"
   1581 	local failed=0
   1582 	local generated_scripts=""
   1583 	local etcsets=$(getetcsets)
   1584 	if [ "${MKX11}" != "no" ]; then
   1585 		generated_scripts="${generated_scripts} xdm xfs"
   1586 	fi
   1587 
   1588 	# Directories of external programs that have rc files (in bsd)
   1589 	local rc_external_files="blacklist nsd unbound"
   1590 
   1591 	# rc* files in /etc/
   1592 	# XXX: at least rc.conf and rc.local shouldn't be updated. PR/54741
   1593 	#local rc_644_files="$(select_set_files /etc/rc \
   1594 	#    "/etc/\(rc[^[:space:]/]*\)" ${etcsets})"
   1595 
   1596 	# no-obsolete rc files in /etc/rc.d
   1597 	local rc_555_files="$(select_set_files /etc/rc.d/ \
   1598 	    "/etc/rc\.d/\([^[:space:]]*\)" ${etcsets} | \
   1599 	    exclude ${rc_external_files})"
   1600 
   1601 	# obsolete rc file in /etc/rc.d
   1602 	local rc_obsolete_files="$(select_obsolete_files /etc/rc.d/ \
   1603 	    "\([^[:space:]]*\)" etc)"
   1604 
   1605 	compare_dir "${op}" "${SRC_DIR}/etc" "${DEST_DIR}/etc" 644 \
   1606 		${rc_644_files}
   1607 	failed=$(( ${failed} + $? ))
   1608 
   1609 	local extra_scripts
   1610 	if ! $SOURCEMODE; then
   1611 		extra_scripts="${generated_scripts}"
   1612 	else
   1613 		extra_scripts=""
   1614 	fi
   1615 
   1616 	compare_dir "${op}" "${SRC_DIR}/etc/rc.d" "${DEST_DIR}/etc/rc.d" 555 \
   1617 		${rc_555_files} \
   1618 		${extra_scripts}
   1619 	failed=$(( ${failed} + $? ))
   1620 
   1621 	for i in ${rc_external_files}; do
   1622 	    local rc_file 
   1623 	    case $i in
   1624 	    *d) rc_file=${i};;
   1625 	    *)	rc_file=${i}d;;
   1626 	    esac
   1627 		
   1628 	    update_rc "${op}" "${dir}" ${rc_file} /sbin \
   1629 		"${SRC_DIR}/external/bsd/$i/etc/rc.d"
   1630 	    failed=$(( ${failed} + $? ))
   1631 	done
   1632 
   1633 	if $SOURCEMODE && [ -n "${generated_scripts}" ]; then
   1634 		# generate scripts
   1635 		mkdir "${SCRATCHDIR}/rc"
   1636 		for f in ${generated_scripts}; do
   1637 			${SED} -e "s,@X11ROOTDIR@,${X11ROOTDIR},g" \
   1638 			    < "${SRC_DIR}/etc/rc.d/${f}.in" \
   1639 			    > "${SCRATCHDIR}/rc/${f}"
   1640 		done
   1641 		compare_dir "${op}" "${SCRATCHDIR}/rc" \
   1642 		    "${DEST_DIR}/etc/rc.d" 555 \
   1643 		    ${generated_scripts}
   1644 		failed=$(( ${failed} + $? ))
   1645 	fi
   1646 
   1647 		# check for obsolete rc.d files
   1648 	for f in ${rc_obsolete_files}; do
   1649 		local fd="/etc/rc.d/${f}"
   1650 		[ -e "${DEST_DIR}${fd}" ] && echo "${fd}"
   1651 	done | obsolete_paths "${op}"
   1652 	failed=$(( ${failed} + $? ))
   1653 
   1654 		# check for obsolete rc.conf(5) variables
   1655 	set -- ${rc_obsolete_vars}
   1656 	while [ $# -gt 1 ]; do
   1657 		if rcconf_is_set "${op}" "$1" "$2" 1; then
   1658 			failed=1
   1659 		fi
   1660 		shift 2
   1661 	done
   1662 
   1663 	return ${failed}
   1664 }
   1665 
   1666 #
   1667 #	sendmail
   1668 #
   1669 adddisableditem sendmail "remove obsolete sendmail configuration files and scripts"
   1670 do_sendmail()
   1671 {
   1672 	[ -n "$1" ] || err 3 "USAGE: do_sendmail  fix|check"
   1673 	op="$1"
   1674 	failed=0
   1675 
   1676 	# Don't complain if the "sendmail" package is installed because the
   1677 	# files might still be in use.
   1678 	if /usr/sbin/pkg_info -qe sendmail >/dev/null 2>&1; then
   1679 		return 0
   1680 	fi
   1681 
   1682 	for f in /etc/mail/helpfile /etc/mail/local-host-names \
   1683 	    /etc/mail/sendmail.cf /etc/mail/submit.cf /etc/rc.d/sendmail \
   1684 	    /etc/rc.d/smmsp /usr/share/misc/sendmail.hf \
   1685 	    $( ( find "${DEST_DIR}/usr/share/sendmail" -type f ; \
   1686 	         find "${DEST_DIR}/usr/share/sendmail" -type d \
   1687 	       ) | unprefix "${DEST_DIR}" ) \
   1688 	    /var/log/sendmail.st \
   1689 	    /var/spool/clientmqueue \
   1690 	    /var/spool/mqueue
   1691 	do
   1692 		[ -e "${DEST_DIR}${f}" ] && echo "${f}"
   1693 	done | obsolete_paths "${op}"
   1694 	failed=$(( ${failed} + $? ))
   1695 
   1696 	return ${failed}
   1697 }
   1698 
   1699 #
   1700 #	mailerconf
   1701 #
   1702 adddisableditem mailerconf "update /etc/mailer.conf after sendmail removal"
   1703 do_mailerconf()
   1704 {
   1705 	[ -n "$1" ] || err 3 "USAGE: do_mailterconf  fix|check"
   1706 	op="$1"
   1707 
   1708 	failed=0
   1709 	mta_path="$(${AWK} '/^sendmail[ \t]/{print$2}' \
   1710 		"${DEST_DIR}/etc/mailer.conf")"
   1711 	old_sendmail_path="/usr/libexec/sendmail/sendmail"
   1712 	if [ "${mta_path}" = "${old_sendmail_path}" ]; then
   1713 	    if [ "$op" = check ]; then
   1714 		msg "mailer.conf points to obsolete ${old_sendmail_path}"
   1715 		failed=1;
   1716 	    else
   1717 		populate_dir "${op}" false \
   1718 		"${SRC_DIR}/etc" "${DEST_DIR}/etc" 644 mailer.conf
   1719 		failed=$?
   1720 	    fi
   1721 	fi
   1722 
   1723 	return ${failed}
   1724 }
   1725 
   1726 #
   1727 #	ssh
   1728 #
   1729 additem ssh "ssh configuration update"
   1730 do_ssh()
   1731 {
   1732 	[ -n "$1" ] || err 3 "USAGE: do_ssh  fix|check"
   1733 	op="$1"
   1734 
   1735 	failed=0
   1736 	_etcssh="${DEST_DIR}/etc/ssh"
   1737 	if ! check_dir "${op}" "${_etcssh}" 755; then
   1738 		failed=1
   1739 	fi
   1740 
   1741 	if [ ${failed} -eq 0 ]; then
   1742 		for f in \
   1743 			    ssh_known_hosts ssh_known_hosts2 \
   1744 			    ssh_host_dsa_key ssh_host_dsa_key.pub \
   1745 			    ssh_host_rsa_key ssh_host_rsa_key.pub \
   1746 			    ssh_host_key ssh_host_key.pub \
   1747 		    ; do
   1748 			if ! move_file "${op}" \
   1749 			    "${DEST_DIR}/etc/${f}" "${_etcssh}/${f}" ; then
   1750 				failed=1
   1751 			fi
   1752 		done
   1753 		for f in sshd.conf ssh.conf ; do
   1754 				# /etc/ssh/ssh{,d}.conf -> ssh{,d}_config
   1755 				#
   1756 			if ! move_file "${op}" \
   1757 			    "${_etcssh}/${f}" "${_etcssh}/${f%.conf}_config" ;
   1758 			then
   1759 				failed=1
   1760 			fi
   1761 				# /etc/ssh{,d}.conf -> /etc/ssh/ssh{,d}_config
   1762 				#
   1763 			if ! move_file "${op}" \
   1764 			    "${DEST_DIR}/etc/${f}" \
   1765 			    "${_etcssh}/${f%.conf}_config" ;
   1766 			then
   1767 				failed=1
   1768 			fi
   1769 		done
   1770 	fi
   1771 
   1772 	sshdconf=""
   1773 	for f in \
   1774 	    "${_etcssh}/sshd_config" \
   1775 	    "${_etcssh}/sshd.conf" \
   1776 	    "${DEST_DIR}/etc/sshd.conf" ; do
   1777 		if [ -f "${f}" ]; then
   1778 			sshdconf="${f}"
   1779 			break
   1780 		fi
   1781 	done
   1782 	if [ -n "${sshdconf}" ]; then
   1783 		modify_file "${op}" "${sshdconf}" "${SCRATCHDIR}/sshdconf" '
   1784 			/^[^#$]/ {
   1785 				kw = tolower($1)
   1786 				if (kw == "hostkey" &&
   1787 				    $2 ~ /^\/etc\/+ssh_host(_[dr]sa)?_key$/ ) {
   1788 					sub(/\/etc\/+/, "/etc/ssh/")
   1789 				}
   1790 				if (kw == "rhostsauthentication" ||
   1791 				    kw == "verifyreversemapping" ||
   1792 				    kw == "reversemappingcheck") {
   1793 					sub(/^/, "# DEPRECATED:\t")
   1794 				}
   1795 			}
   1796 			{ print }
   1797 		'
   1798 		failed=$(( ${failed} + $? ))
   1799 	fi
   1800 
   1801 	if ! find_file_in_dirlist moduli "moduli" \
   1802 	    "${SRC_DIR}/crypto/external/bsd/openssh/dist" "${SRC_DIR}/etc" ; then
   1803 		failed=1
   1804 			# ${dir} is set by find_file_in_dirlist()
   1805 	elif ! compare_dir "${op}" "${dir}" "${DEST_DIR}/etc" 444 moduli; then
   1806 		failed=1
   1807 	fi
   1808 
   1809 	if ! check_dir "${op}" "${DEST_DIR}/var/chroot/sshd" 755 ; then
   1810 		failed=1
   1811 	fi
   1812 
   1813 	if rcconf_is_set "${op}" sshd sshd_conf_dir 1; then
   1814 		failed=1
   1815 	fi
   1816 
   1817 	return ${failed}
   1818 }
   1819 
   1820 #
   1821 #	wscons
   1822 #
   1823 additem wscons "wscons configuration file update"
   1824 do_wscons()
   1825 {
   1826 	[ -n "$1" ] || err 3 "USAGE: do_wscons  fix|check"
   1827 	op="$1"
   1828 
   1829 	[ -f "${DEST_DIR}/etc/wscons.conf" ] || return 0
   1830 
   1831 	failed=0
   1832 	notfixed=""
   1833 	if [ "${op}" = "fix" ]; then
   1834 		notfixed="${NOT_FIXED}"
   1835 	fi
   1836 	while read _type _arg1 _rest; do
   1837 		if [ "${_type}" = "mux" -a "${_arg1}" = "1" ]; then
   1838 			msg \
   1839     "Obsolete wscons.conf(5) entry \""${_type} ${_arg1}"\" found.${notfixed}"
   1840 			failed=1
   1841 		fi
   1842 	done < "${DEST_DIR}/etc/wscons.conf"
   1843 
   1844 	return ${failed}
   1845 }
   1846 
   1847 #
   1848 #	X11
   1849 #
   1850 additem x11 "x11 configuration update"
   1851 do_x11()
   1852 {
   1853 	[ -n "$1" ] || err 3 "USAGE: do_x11  fix|check"
   1854 	op="$1"
   1855 
   1856 	failed=0
   1857 	_etcx11="${DEST_DIR}/etc/X11"
   1858 	if [ ! -d "${_etcx11}" ]; then
   1859 		msg "${_etcx11} is not a directory; skipping check"
   1860 		return 0
   1861 	fi
   1862 	if [ -d "${DEST_DIR}/usr/X11R6/." ]
   1863 	then
   1864 		_libx11="${DEST_DIR}/usr/X11R6/lib/X11"
   1865 		if [ ! -d "${_libx11}" ]; then
   1866 			msg "${_libx11} is not a directory; skipping check"
   1867 			return 0
   1868 		fi
   1869 	fi
   1870 
   1871 	_notfixed=""
   1872 	if [ "${op}" = "fix" ]; then
   1873 		_notfixed="${NOT_FIXED}"
   1874 	fi
   1875 
   1876 	for d in \
   1877 		    fs lbxproxy proxymngr rstart twm xdm xinit xserver xsm \
   1878 	    ; do
   1879 		sd="${_libx11}/${d}"
   1880 		ld="/etc/X11/${d}"
   1881 		td="${DEST_DIR}${ld}"
   1882 		if [ -h "${sd}" ]; then
   1883 			continue
   1884 		elif [ -d "${sd}" ]; then
   1885 			tdfiles="$(find "${td}" \! -type d)"
   1886 			if [ -n "${tdfiles}" ]; then
   1887 				msg "${sd} exists yet ${td} already" \
   1888 				    "contains files${_notfixed}"
   1889 			else
   1890 				msg "Migrate ${sd} to ${td}${_notfixed}"
   1891 			fi
   1892 			failed=1
   1893 		elif [ -e "${sd}" ]; then
   1894 			msg "Unexpected file ${sd}${_notfixed}"
   1895 			continue
   1896 		else
   1897 			continue
   1898 		fi
   1899 	done
   1900 
   1901 	# check if xdm resources have been updated
   1902 	if [ -r ${_etcx11}/xdm/Xresources ] && \
   1903 	    ! ${GREP} 'inpColor:' ${_etcx11}/xdm/Xresources > /dev/null; then
   1904 		msg "Update ${_etcx11}/xdm/Xresources${_notfixed}"
   1905 		failed=1
   1906 	fi
   1907 
   1908 	return ${failed}
   1909 }
   1910 
   1911 #
   1912 #	xkb
   1913 #
   1914 # /usr/X11R7/lib/X11/xkb/symbols/pc used to be a directory, but changed
   1915 # to a file on 2009-06-12.  Fixing this requires removing the directory
   1916 # (which we can do) and re-extracting the xbase set (which we can't do),
   1917 # or at least adding that one file (which we may be able to do if X11SRCDIR
   1918 # is available).
   1919 #
   1920 additem xkb "clean up for xkbdata to xkeyboard-config upgrade"
   1921 do_xkb()
   1922 {
   1923 	[ -n "$1" ] || err 3 "USAGE: do_xkb  fix|check"
   1924 	op="$1"
   1925 	failed=0
   1926 
   1927 	pcpath="/usr/X11R7/lib/X11/xkb/symbols/pc"
   1928 	pcsrcdir="${X11SRCDIR}/external/mit/xkeyboard-config/dist/symbols"
   1929 
   1930 	filemsg="\
   1931 ${pcpath} was a directory, should be a file.
   1932     To fix, extract the xbase set again."
   1933 
   1934 	_notfixed=""
   1935 	if [ "${op}" = "fix" ]; then
   1936 		_notfixed="${NOT_FIXED}"
   1937 	fi
   1938 
   1939 	if [ ! -d "${DEST_DIR}${pcpath}" ]; then
   1940 		return 0
   1941 	fi
   1942 
   1943 	# Delete obsolete files in the directory, and the directory
   1944 	# itself.  If the directory contains unexpected extra files
   1945 	# then it will not be deleted.
   1946 	( [ -f "${DEST_DIR}"/var/db/obsolete/xbase ] \
   1947 	    &&  ${SORT} -ru "${DEST_DIR}"/var/db/obsolete/xbase \
   1948 	    | ${GREP} -E "^\\.?${pcpath}/" ;
   1949 	    echo "${pcpath}" ) \
   1950 	| obsolete_paths "${op}"
   1951 	failed=$(( ${failed} + $? ))
   1952 
   1953 	# If the directory was removed above, then try to replace it with
   1954 	# a file.
   1955 	if [ -d "${DEST_DIR}${pcpath}" ]; then
   1956 		msg "${filemsg}${_notfixed}"
   1957 		failed=$(( ${failed} + 1 ))
   1958 	else
   1959 		if ! find_file_in_dirlist pc "${pcpath}" \
   1960 			"${pcsrcdir}" "${SRC_DIR}${pcpath%/*}"
   1961 		then
   1962 			msg "${filemsg}${_notfixed}"
   1963 			failed=$(( ${failed} + 1 ))
   1964 		else
   1965 			# ${dir} is set by find_file_in_dirlist()
   1966 			populate_dir "${op}" true \
   1967 				"${dir}" "${DEST_DIR}${pcpath%/*}" 444 \
   1968 				pc
   1969 			failed=$(( ${failed} + $? ))
   1970 		fi
   1971 	fi
   1972 
   1973 	return $failed
   1974 }
   1975 
   1976 #
   1977 #	uid
   1978 #
   1979 additem uid "required users in /etc/master.passwd"
   1980 do_uid()
   1981 {
   1982 	[ -n "$1" ] || err 3 "USAGE: do_uid  fix|check"
   1983 
   1984 	check_ids "$1" users "${DEST_DIR}/etc/master.passwd" \
   1985 	    "${SRC_DIR}/etc/master.passwd" 12 \
   1986 	    postfix SKIP named ntpd sshd SKIP _pflogd _rwhod SKIP _proxy \
   1987 	    _timedc _sdpd _httpd _mdnsd _tests _tcpdump _tss SKIP _rtadvd \
   1988 	    SKIP _unbound _nsd SKIP _dhcpcd
   1989 }
   1990 
   1991 
   1992 #
   1993 #	varrwho
   1994 #
   1995 additem varrwho "required ownership of files in /var/rwho"
   1996 do_varrwho()
   1997 {
   1998 	[ -n "$1" ] || err 3 "USAGE: do_varrwho  fix|check"
   1999 
   2000 	contents_owner "$1" "${DEST_DIR}/var/rwho" _rwhod _rwhod
   2001 }
   2002 
   2003 
   2004 #
   2005 #	tcpdumpchroot
   2006 #
   2007 additem tcpdumpchroot "remove /var/chroot/tcpdump/etc/protocols"
   2008 do_tcpdumpchroot()
   2009 {
   2010 	[ -n "$1" ] || err 3 "USAGE: do_tcpdumpchroot  fix|check"
   2011 
   2012 	failed=0;
   2013 	if [ -r "${DEST_DIR}/var/chroot/tcpdump/etc/protocols" ]; then
   2014 		if [ "$1" = "fix" ]; then
   2015 			rm "${DEST_DIR}/var/chroot/tcpdump/etc/protocols"
   2016 			failed=$(( ${failed} + $? ))
   2017 			rmdir "${DEST_DIR}/var/chroot/tcpdump/etc"
   2018 			failed=$(( ${failed} + $? ))
   2019 		else
   2020 			failed=1
   2021 		fi
   2022 	fi
   2023 	return ${failed}
   2024 }
   2025 
   2026 
   2027 #
   2028 #	atf
   2029 #
   2030 additem atf "install missing atf configuration files and validate them"
   2031 do_atf()
   2032 {
   2033 	[ -n "$1" ] || err 3 "USAGE: do_atf  fix|check"
   2034 	op="$1"
   2035 	failed=0
   2036 
   2037 	# Ensure atf configuration files are in place.
   2038 	if find_file_in_dirlist NetBSD.conf "NetBSD.conf" \
   2039 	    "${SRC_DIR}/external/bsd/atf/etc/atf" \
   2040 	    "${SRC_DIR}/etc/atf"; then
   2041 			# ${dir} is set by find_file_in_dirlist()
   2042 		populate_dir "${op}" true "${dir}" "${DEST_DIR}/etc/atf" 644 \
   2043 		    NetBSD.conf common.conf || failed=1
   2044 	else
   2045 		failed=1
   2046 	fi
   2047 	if find_file_in_dirlist atf-run.hooks "atf-run.hooks" \
   2048 	    "${SRC_DIR}/external/bsd/atf/dist/tools/sample" \
   2049 	    "${SRC_DIR}/etc/atf"; then
   2050 			# ${dir} is set by find_file_in_dirlist()
   2051 		populate_dir "${op}" true "${dir}" "${DEST_DIR}/etc/atf" 644 \
   2052 		    atf-run.hooks || failed=1
   2053 	else
   2054 		failed=1
   2055 	fi
   2056 
   2057 	# Validate the _atf to _tests user/group renaming.
   2058 	if [ -f "${DEST_DIR}/etc/atf/common.conf" ]; then
   2059 		handle_atf_user "${op}" || failed=1
   2060 	else
   2061 		failed=1
   2062 	fi
   2063 
   2064 	return ${failed}
   2065 }
   2066 
   2067 handle_atf_user()
   2068 {
   2069 	local op="$1"
   2070 	local failed=0
   2071 
   2072 	local conf="${DEST_DIR}/etc/atf/common.conf"
   2073 	if grep '[^#]*unprivileged-user[ \t]*=.*_atf' "${conf}" >/dev/null
   2074 	then
   2075 		if [ "$1" = "fix" ]; then
   2076 			${SED} -e \
   2077 			    "/[^#]*unprivileged-user[\ t]*=/s/_atf/_tests/" \
   2078 			    "${conf}" >"${conf}.new"
   2079 			failed=$(( ${failed} + $? ))
   2080 			mv "${conf}.new" "${conf}"
   2081 			failed=$(( ${failed} + $? ))
   2082 			msg "Set unprivileged-user=_tests in ${conf}"
   2083 		else
   2084 			msg "unprivileged-user=_atf in ${conf} should be" \
   2085 			    "unprivileged-user=_tests"
   2086 			failed=1
   2087 		fi
   2088 	fi
   2089 
   2090 	return ${failed}
   2091 }
   2092 
   2093 #
   2094 #	catpages
   2095 #
   2096 obsolete_catpages()
   2097 {
   2098 	basedir="$2"
   2099 	section="$3"
   2100 	mandir="${basedir}/man${section}"
   2101 	catdir="${basedir}/cat${section}"
   2102 	test -d "$mandir" || return 0
   2103 	test -d "$catdir" || return 0
   2104 	(cd "$mandir" && find . -type f) | {
   2105 	failed=0
   2106 	while read manpage; do
   2107 		manpage="${manpage#./}"
   2108 		case "$manpage" in
   2109 		*.Z)
   2110 			catname="$catdir/${manpage%.*.Z}.0"
   2111 			;;
   2112 		*.gz)
   2113 			catname="$catdir/${manpage%.*.gz}.0"
   2114 			;;
   2115 		*)
   2116 			catname="$catdir/${manpage%.*}.0"
   2117 			;;
   2118 		esac
   2119 		test -e "$catname" -a "$catname" -ot "$mandir/$manpage" || continue
   2120 		if [ "$1" = "fix" ]; then
   2121 			rm "$catname"
   2122 			failed=$(( ${failed} + $? ))
   2123 			msg "Removed obsolete cat page $catname"
   2124 		else
   2125 			msg "Obsolete cat page $catname"
   2126 			failed=1
   2127 		fi
   2128 	done
   2129 	exit $failed
   2130 	}
   2131 }
   2132 
   2133 additem catpages "remove outdated cat pages"
   2134 do_catpages()
   2135 {
   2136 	failed=0
   2137 	for manbase in /usr/share/man /usr/X11R6/man /usr/X11R7/man; do
   2138 		for sec in 1 2 3 4 5 6 7 8 9; do
   2139 			obsolete_catpages "$1" "${DEST_DIR}${manbase}" "${sec}"
   2140 			failed=$(( ${failed} + $? ))
   2141 			if [ "$1" = "fix" ]; then
   2142 				rmdir "${DEST_DIR}${manbase}/cat${sec}"/* \
   2143 					2>/dev/null
   2144 				rmdir "${DEST_DIR}${manbase}/cat${sec}" \
   2145 					2>/dev/null
   2146 			fi
   2147 		done
   2148 	done
   2149 	return $failed
   2150 }
   2151 
   2152 #
   2153 #	man.conf
   2154 #
   2155 additem manconf "check for a mandoc usage in /etc/man.conf"
   2156 do_manconf()
   2157 {
   2158 	[ -n "$1" ] || err 3 "USAGE: do_manconf  fix|check"
   2159 	op="$1"
   2160 	failed=0
   2161 
   2162 	[ -f "${DEST_DIR}/etc/man.conf" ] || return 0
   2163 	if ${GREP} -w "mandoc" "${DEST_DIR}/etc/man.conf" >/dev/null 2>&1;
   2164 	then
   2165 		failed=0;
   2166 	else
   2167 		failed=1
   2168 		notfixed=""
   2169 		if [ "${op}" = "fix" ]; then
   2170 			notfixed="${NOT_FIXED}"
   2171 		fi
   2172 		msg "The file /etc/man.conf has not been adapted to mandoc usage; you"
   2173 		msg "probably want to copy a new version over. ${notfixed}"
   2174 	fi
   2175 
   2176 	return ${failed}
   2177 }
   2178 
   2179 
   2180 #
   2181 #	ptyfsoldnodes
   2182 #
   2183 additem ptyfsoldnodes "remove legacy device nodes when using ptyfs"
   2184 do_ptyfsoldnodes()
   2185 {
   2186 	[ -n "$1" ] || err 3 "USAGE: do_ptyfsoldnodes  fix|check"
   2187 	_ptyfs_op="$1"
   2188 
   2189 	# Check whether ptyfs is in use
   2190 	failed=0;
   2191 	if ! ${GREP} -E "^ptyfs" "${DEST_DIR}/etc/fstab" > /dev/null; then
   2192 		msg "ptyfs is not in use"
   2193 		return 0
   2194 	fi
   2195 
   2196 	if [ ! -e "${DEST_DIR}/dev/pts" ]; then
   2197 		msg "ptyfs is not properly configured: missing /dev/pts"
   2198 		return 1
   2199 	fi
   2200 
   2201 	# Find the device major numbers for the pty master and slave
   2202 	# devices, by parsing the output from "MAKEDEV -s pty0".
   2203 	#
   2204 	# Output from MAKEDEV looks like this:
   2205 	# ./ttyp0 type=char device=netbsd,5,0 mode=666 gid=0 uid=0
   2206 	# ./ptyp0 type=char device=netbsd,6,0 mode=666 gid=0 uid=0
   2207 	#
   2208 	# Output from awk, used in the eval statement, looks like this:
   2209 	# maj_ptym=6; maj_ptys=5;
   2210 	#
   2211 	find_makedev
   2212 	eval "$(
   2213 	    ${HOST_SH} "${MAKEDEV_DIR}/MAKEDEV" -s pty0 2>/dev/null \
   2214 	    | ${AWK} '\
   2215 	    BEGIN { before_re = ".*device=[a-zA-Z]*,"; after_re = ",.*"; }
   2216 	    /ptyp0/ { maj_ptym = gensub(before_re, "", 1, $0);
   2217 		      maj_ptym = gensub(after_re, "", 1, maj_ptym); }
   2218 	    /ttyp0/ { maj_ptys = gensub(before_re, "", 1, $0);
   2219 		      maj_ptys = gensub(after_re, "", 1, maj_ptys); }
   2220 	    END { print "maj_ptym=" maj_ptym "; maj_ptys=" maj_ptys ";"; }
   2221 	    '
   2222 	    )"
   2223 	#msg "Major numbers are maj_ptym=${maj_ptym} maj_ptys=${maj_ptys}"
   2224 	if [ -z "$maj_ptym" ] || [ -z "$maj_ptys" ]; then
   2225 		msg "Cannot find device major numbers for pty master and slave"
   2226 		return 1
   2227 	fi
   2228 
   2229 	# look for /dev/[pt]ty[p-zP-T][0-9a-zA-Z], and check that they
   2230 	# have the expected device major numbers.  ttyv* is typically not a
   2231 	# pty device, but we check it anyway.
   2232 	#
   2233 	# The "for d1" loop is intended to avoid overflowing ARG_MAX;
   2234 	# otherwise we could have used a single glob pattern.
   2235 	#
   2236 	# If there are no files that match a particular pattern,
   2237 	# then stat prints something like:
   2238 	#    stat: /dev/[pt]tyx?: lstat: No such file or directory
   2239 	# and we ignore it.  XXX: We also ignore other error messages.
   2240 	#
   2241 	_ptyfs_tmp="$(mktemp /tmp/postinstall.ptyfs.XXXXXXXX)"
   2242 	for d1 in p q r s t u v w x y z P Q R S T; do
   2243 		${STAT} -f "%Hr %N" "${DEST_DIR}/dev/"[pt]ty${d1}? 2>&1
   2244 	done \
   2245 	| while read -r major node ; do
   2246 		case "$major" in
   2247 		${maj_ptym}|${maj_ptys}) echo "$node" ;;
   2248 		esac
   2249 	done >"${_ptyfs_tmp}"
   2250 
   2251 	_desc="legacy device node"
   2252 	while read node; do
   2253 		if [ "${_ptyfs_op}" = "check" ]; then
   2254 			msg "Remove ${_desc} ${node}"
   2255 			failed=1
   2256 		else # "fix"
   2257 			if rm "${node}"; then
   2258 				msg "Removed ${_desc} ${node}"
   2259 			else
   2260 				warn "Failed to remove ${_desc} ${node}"
   2261 				failed=1
   2262 			fi
   2263 		fi
   2264 	done < "${_ptyfs_tmp}"
   2265 	rm "${_ptyfs_tmp}"
   2266 
   2267 	return ${failed}
   2268 }
   2269 
   2270 
   2271 #
   2272 #	varshm
   2273 #
   2274 additem varshm "check for a tmpfs mounted on /var/shm"
   2275 do_varshm()
   2276 {
   2277 	[ -n "$1" ] || err 3 "USAGE: do_varshm  fix|check"
   2278 	op="$1"
   2279 	failed=0
   2280 
   2281 	[ -f "${DEST_DIR}/etc/fstab" ] || return 0
   2282 	if ${GREP} -E "^var_shm_symlink" "${DEST_DIR}/etc/rc.conf" >/dev/null 2>&1;
   2283 	then
   2284 		failed=0;
   2285 	elif ${GREP} -w "/var/shm" "${DEST_DIR}/etc/fstab" >/dev/null 2>&1;
   2286 	then
   2287 		failed=0;
   2288 	else
   2289 		if [ "${op}" = "check" ]; then
   2290 			failed=1
   2291 			msg "No /var/shm mount found in ${DEST_DIR}/etc/fstab"
   2292 		elif [ "${op}" = "fix" ]; then
   2293 			printf '\ntmpfs\t/var/shm\ttmpfs\trw,-m1777,-sram%%25\n' \
   2294 				>> "${DEST_DIR}/etc/fstab"
   2295 			msg "Added tmpfs with 25% ram limit as /var/shm"
   2296 
   2297 		fi
   2298 	fi
   2299 
   2300 	return ${failed}
   2301 }
   2302 
   2303 #
   2304 #	obsolete_stand
   2305 #
   2306 obsolete_stand_internal()
   2307 {
   2308 	local prefix="$1"
   2309 	shift
   2310 	[ -n "$1" ] || err 3 "USAGE: do_obsolete_stand  fix|check"
   2311 	local op="$1"
   2312 	local failed=0
   2313 
   2314 	for dir in \
   2315 	    ${prefix}/stand/${MACHINE} \
   2316 	    ${prefix}/stand/${MACHINE}-4xx \
   2317 	    ${prefix}/stand/${MACHINE}-booke \
   2318 	    ${prefix}/stand/${MACHINE}-xen \
   2319 	    ${prefix}/stand/${MACHINE}pae-xen
   2320 	do
   2321 		[ -d "${DESTDIR}${dir}" ] && obsolete_stand "${dir}"
   2322 	done | obsolete_paths "${op}"
   2323 	failed=$(( ${failed} + $? ))
   2324 
   2325 	return ${failed}
   2326 }
   2327 
   2328 adddisableditem obsolete_stand "remove obsolete files from /stand"
   2329 do_obsolete_stand()
   2330 {
   2331 	obsolete_stand_internal "" "$@"
   2332 	return $?
   2333 }
   2334 
   2335 adddisableditem obsolete_stand_debug "remove obsolete files from /usr/libdata/debug/stand"
   2336 do_obsolete_stand_debug()
   2337 {
   2338 	obsolete_stand_internal "/usr/libdata/debug" "$@"
   2339 	return $?
   2340 }
   2341 
   2342 listarchsubdirs() {
   2343 	if ! $SOURCEMODE; then
   2344 		echo "@ARCHSUBDIRS@"
   2345 	else
   2346 		${SED} -n -e '/ARCHDIR_SUBDIR/s/[[:space:]]//gp' \
   2347 		    ${SRC_DIR}/compat/archdirs.mk 
   2348 	fi
   2349 }
   2350 
   2351 
   2352 getarchsubdirs() {
   2353 	local m
   2354 	case ${MACHINE_ARCH} in
   2355 	*arm*|*aarch64*)	m=arm;;
   2356 	x86_64)			m=amd64;;
   2357 	*)			m=${MACHINE_ARCH};;
   2358 	esac
   2359 
   2360 	for i in $(listarchsubdirs); do
   2361 		echo $i
   2362 	done | ${SORT} -u | ${SED} -n -e "/=${m}/s@.*=${m}/\(.*\)@\1@p"
   2363 }
   2364 
   2365 getcompatlibdirs() {
   2366 	for i in $(getarchsubdirs); do
   2367 		if [ -d "${DEST_DIR}/usr/lib/$i" ]; then
   2368 			echo /usr/lib/$i
   2369 		fi
   2370 	done
   2371 }
   2372 
   2373 #
   2374 #	obsolete
   2375 #	(this item is last to allow other items to move obsolete files)
   2376 #
   2377 additem obsolete "remove obsolete file sets and minor libraries"
   2378 do_obsolete()
   2379 {
   2380 	[ -n "$1" ] || err 3 "USAGE: do_obsolete  fix|check"
   2381 	op="$1"
   2382 	failed=0
   2383 
   2384 	${SORT} -ru "${DEST_DIR}"/var/db/obsolete/* | obsolete_paths "${op}"
   2385 	failed=$(( ${failed} + $? ))
   2386 
   2387 	(
   2388 		obsolete_libs /lib
   2389 		obsolete_libs /usr/lib
   2390 		obsolete_libs /usr/lib/i18n
   2391 		obsolete_libs /usr/X11R6/lib
   2392 		obsolete_libs /usr/X11R7/lib
   2393 		for i in $(getcompatlibdirs); do
   2394 			obsolete_libs $i
   2395 		done
   2396 	) | obsolete_paths "${op}"
   2397 	failed=$(( ${failed} + $? ))
   2398 
   2399 	return ${failed}
   2400 }
   2401 
   2402 #
   2403 #	end of items
   2404 #	------------
   2405 #
   2406 
   2407 
   2408 usage()
   2409 {
   2410 	cat 1>&2 << _USAGE_
   2411 Usage: ${PROGNAME} [-s srcdir] [-x xsrcdir] [-d destdir] [-m mach] [-a arch] op [item [...]]
   2412 	Perform post-installation checks and/or fixes on a system's
   2413 	configuration files.
   2414 	If no items are provided, a default set of checks or fixes is applied.
   2415 
   2416 	Options:
   2417 	-s {srcdir|tgzfile|tempdir}
   2418 			Location of the source files.  This may be any
   2419 			of the following:
   2420 			* A directory that contains a NetBSD source tree;
   2421 			* A distribution set file such as "etc.tgz" or
   2422 			  "xetc.tgz".  Pass multiple -s options to specify
   2423 			  multiple such files;
   2424 			* A temporary directory in which one or both of
   2425 			  "etc.tgz" and "xetc.tgz" have been extracted.
   2426 							[${SRC_DIR:-/usr/src}]
   2427 	-x xsrcdir      Location of the X11 source files.  This must be
   2428 			a directory that contains a NetBSD xsrc tree.
   2429 							[${XSRC_DIR:-/usr/src/../xsrc}]
   2430 	-d destdir	Destination directory to check. [${DEST_DIR:-/}]
   2431 	-m mach		MACHINE.			[${MACHINE}]
   2432 	-a arch		MACHINE_ARCH.			[${MACHINE_ARCH}]
   2433 
   2434 	Operation may be one of:
   2435 		help	Display this help.
   2436 		list	List available items.
   2437 		check	Perform post-installation checks on items.
   2438 		diff [diff(1) options ...]
   2439 			Similar to 'check' but also output difference of files.
   2440 		fix	Apply fixes that 'check' determines need to be applied.
   2441 		usage	Display this usage.
   2442 _USAGE_
   2443 	exit 2
   2444 }
   2445 
   2446 
   2447 list()
   2448 {
   2449 	echo "Default set of items (to apply if no items are provided by user):"
   2450 	echo "  Item          Description"
   2451 	echo "  ----          -----------"
   2452 	for i in ${defaultitems}; do
   2453 		eval desc=\"\${desc_${i}}\"
   2454 		printf "  %-12s  %s\n" "${i}" "${desc}"
   2455 	done
   2456 	echo "Items disabled by default (must be requested explicitly):"
   2457 	echo "  Item          Description"
   2458 	echo "  ----          -----------"
   2459 	for i in ${otheritems}; do
   2460 		eval desc=\"\${desc_${i}}\"
   2461 		printf "  %-12s  %s\n" "${i}" "${desc}"
   2462 	done
   2463 
   2464 }
   2465 
   2466 
   2467 main()
   2468 {
   2469 	TGZLIST=		# quoted list list of tgz files
   2470 	SRC_ARGLIST=		# quoted list of one or more "-s" args
   2471 	SRC_DIR="${SRC_ARG}"	# set default value for early usage()
   2472 	XSRC_DIR="${SRC_ARG}/../xsrc"
   2473 	N_SRC_ARGS=0		# number of "-s" args
   2474 	TGZMODE=false		# true if "-s" specifies a tgz file
   2475 	DIRMODE=false		# true if "-s" specified a directory
   2476 	SOURCEMODE=false	# true if "-s" specified a source directory
   2477 
   2478 	case "$(uname -s)" in
   2479 	Darwin)
   2480 		# case sensitive match for case insensitive fs
   2481 		file_exists_exact=file_exists_exact
   2482 		;;
   2483 	*)
   2484 		file_exists_exact=:
   2485 		;;
   2486 	esac
   2487 
   2488 	while getopts s:x:d:m:a: ch; do
   2489 		case "${ch}" in
   2490 		s)
   2491 			qarg="$(shell_quote "${OPTARG}")"
   2492 			N_SRC_ARGS=$(( $N_SRC_ARGS + 1 ))
   2493 			SRC_ARGLIST="${SRC_ARGLIST}${SRC_ARGLIST:+ }-s ${qarg}"
   2494 			if [ -f "${OPTARG}" ]; then
   2495 				# arg refers to a *.tgz file.
   2496 				# This may happen twice, for both
   2497 				# etc.tgz and xetc.tgz, so we build up a
   2498 				# quoted list in TGZLIST.
   2499 				TGZMODE=true
   2500 				TGZLIST="${TGZLIST}${TGZLIST:+ }${qarg}"
   2501 				# Note that, when TGZMODE is true,
   2502 				# SRC_ARG is used only for printing
   2503 				# human-readable messages.
   2504 				SRC_ARG="${TGZLIST}"
   2505 			elif [ -d "${OPTARG}" ]; then
   2506 				# arg refers to a directory.
   2507 				# It might be a source directory, or a
   2508 				# directory where the sets have already
   2509 				# been extracted.
   2510 				DIRMODE=true
   2511 				SRC_ARG="${OPTARG}"
   2512 				if [ -f "${OPTARG}/etc/Makefile" ]; then
   2513 					SOURCEMODE=true
   2514 				fi
   2515 			else
   2516 				err 2 "Invalid argument for -s option"
   2517 			fi
   2518 			;;
   2519 		x)
   2520 			if [ -d "${OPTARG}" ]; then
   2521 				# arg refers to a directory.
   2522 				XSRC_DIR="${OPTARG}"
   2523 				XSRC_DIR_FIX="-x ${OPTARG} "
   2524 			else
   2525 				err 2 "Not a directory for -x option"
   2526 			fi
   2527 			;;
   2528 		d)
   2529 			DEST_DIR="${OPTARG}"
   2530 			;;
   2531 		m)
   2532 			MACHINE="${OPTARG}"
   2533 			;;
   2534 		a)
   2535 			MACHINE_ARCH="${OPTARG}"
   2536 			;;
   2537 		*)
   2538 			usage
   2539 			;;
   2540 		esac
   2541 	done
   2542 	shift $((${OPTIND} - 1))
   2543 	[ $# -gt 0 ] || usage
   2544 
   2545 	if [ "$N_SRC_ARGS" -gt 1 ] && $DIRMODE; then
   2546 		err 2 "Multiple -s args are allowed only with tgz files"
   2547 	fi
   2548 	if [ "$N_SRC_ARGS" -eq 0 ]; then
   2549 		# The default SRC_ARG was set elsewhere
   2550 		DIRMODE=true
   2551 		SOURCEMODE=true
   2552 		SRC_ARGLIST="-s $(shell_quote "${SRC_ARG}")"
   2553 	fi
   2554 
   2555 	#
   2556 	# If '-s' arg or args specified tgz files, extract them
   2557 	# to a scratch directory.
   2558 	#
   2559 	if $TGZMODE; then
   2560 		ETCTGZDIR="${SCRATCHDIR}/etc.tgz"
   2561 		echo "Note: Creating temporary directory ${ETCTGZDIR}"
   2562 		if ! mkdir "${ETCTGZDIR}"; then
   2563 			err 2 "Can't create ${ETCTGZDIR}"
   2564 		fi
   2565 		( # subshell to localise changes to "$@"
   2566 			eval "set -- ${TGZLIST}"
   2567 			for tgz in "$@"; do
   2568 				echo "Note: Extracting files from ${tgz}"
   2569 				cat "${tgz}" | (
   2570 					cd "${ETCTGZDIR}" &&
   2571 					tar -zxf -
   2572 				) || err 2 "Can't extract ${tgz}"
   2573 			done
   2574 		)
   2575 		SRC_DIR="${ETCTGZDIR}"
   2576 	else
   2577 		SRC_DIR="${SRC_ARG}"
   2578 	fi
   2579 
   2580 	[ -d "${SRC_DIR}" ]	|| err 2 "${SRC_DIR} is not a directory"
   2581 	[ -d "${DEST_DIR}" ]	|| err 2 "${DEST_DIR} is not a directory"
   2582 	[ -n "${MACHINE}" ]	|| err 2 "\${MACHINE} is not defined"
   2583 	[ -n "${MACHINE_ARCH}" ] || err 2 "\${MACHINE_ARCH} is not defined"
   2584 	if ! $SOURCEMODE && ! [ -f "${SRC_DIR}/etc/mtree/set.etc" ]; then
   2585 		err 2 "Files from the etc.tgz set are missing"
   2586 	fi
   2587 
   2588 		# If directories are /, clear them, so various messages
   2589 		# don't have leading "//".   However, this requires
   2590 		# the use of ${foo:-/} to display the variables.
   2591 		#
   2592 	[ "${SRC_DIR}" = "/" ]	&& SRC_DIR=""
   2593 	[ "${DEST_DIR}" = "/" ]	&& DEST_DIR=""
   2594 
   2595 	detect_x11
   2596 
   2597 	op="$1"
   2598 	shift
   2599 
   2600 	case "${op}" in
   2601 	diff)
   2602 		op=check
   2603 		DIFF_STYLE=n			# default style is RCS
   2604 		OPTIND=1
   2605 		while getopts bcenpuw ch; do
   2606 			case "${ch}" in
   2607 			c|e|n|u)
   2608 				if [ "${DIFF_STYLE}" != "n" -a \
   2609 				    "${DIFF_STYLE}" != "${ch}" ]; then
   2610 					err 2 "conflicting output style: ${ch}"
   2611 				fi
   2612 				DIFF_STYLE="${ch}"
   2613 				;;
   2614 			b|p|w)
   2615 				DIFF_OPT="${DIFF_OPT} -${ch}"
   2616 				;;
   2617 			*)
   2618 				err 2 "unknown diff option"
   2619 				;;
   2620 			esac
   2621 		done
   2622 		shift $((${OPTIND} - 1))
   2623 		;;
   2624 	esac
   2625 
   2626 	case "${op}" in
   2627 
   2628 	usage|help)
   2629 		usage
   2630 		;;
   2631 
   2632 	list)
   2633 		echo "Source directory: ${SRC_DIR:-/}"
   2634 		echo "Target directory: ${DEST_DIR:-/}"
   2635 		if $TGZMODE; then
   2636 			echo " (extracted from: ${SRC_ARG})"
   2637 		fi
   2638 		list
   2639 		;;
   2640 
   2641 	check|fix)
   2642 		todo="$*"
   2643 		: ${todo:="${defaultitems}"}
   2644 
   2645 		# ensure that all supplied items are valid
   2646 		#
   2647 		for i in ${todo}; do
   2648 			eval desc=\"\${desc_${i}}\"
   2649 			[ -n "${desc}" ] || err 2 "Unsupported ${op} '"${i}"'"
   2650 		done
   2651 
   2652 		# perform each check/fix
   2653 		#
   2654 		echo "Source directory: ${SRC_DIR:-/}"
   2655 		if $TGZMODE; then
   2656 			echo " (extracted from: ${SRC_ARG})"
   2657 		fi
   2658 		echo "Target directory: ${DEST_DIR:-/}"
   2659 		items_passed=
   2660 		items_failed=
   2661 		for i in ${todo}; do
   2662 			echo "${i} ${op}:"
   2663 			( eval do_${i} ${op} )
   2664 			if [ $? -eq 0 ]; then
   2665 				items_passed="${items_passed} ${i}"
   2666 			else
   2667 				items_failed="${items_failed} ${i}"
   2668 			fi
   2669 		done
   2670 
   2671 		if [ "${op}" = "check" ]; then
   2672 			plural="checks"
   2673 		else
   2674 			plural="fixes"
   2675 		fi
   2676 
   2677 		echo "${PROGNAME} ${plural} passed:${items_passed}"
   2678 		echo "${PROGNAME} ${plural} failed:${items_failed}"
   2679 		if [ -n "${items_failed}" ]; then
   2680 		    exitstatus=1;
   2681 		    if [ "${op}" = "check" ]; then
   2682 			[ "$MACHINE" = "$(uname -m)" ] && m= || m=" -m $MACHINE"
   2683 			cat <<_Fix_me_
   2684 To fix, run:
   2685     ${HOST_SH} ${0} ${SRC_ARGLIST} ${XSRC_DIR_FIX}-d ${DEST_DIR:-/}$m fix${items_failed}
   2686 Note that this may overwrite local changes.
   2687 _Fix_me_
   2688 		    fi
   2689 		fi
   2690 
   2691 		;;
   2692 
   2693 	*)
   2694 		warn "Unknown operation '"${op}"'"
   2695 		usage
   2696 		;;
   2697 
   2698 	esac
   2699 }
   2700 
   2701 if [ -n "$POSTINSTALL_FUNCTION" ]; then
   2702 	eval "$POSTINSTALL_FUNCTION"
   2703 	exit 0
   2704 fi
   2705 
   2706 # defaults
   2707 #
   2708 PROGNAME="${0##*/}"
   2709 SRC_ARG="/usr/src"
   2710 DEST_DIR="/"
   2711 : ${MACHINE:="$( uname -m )"}	# assume native build if $MACHINE is not set
   2712 : ${MACHINE_ARCH:="$( uname -p )"}# assume native build if not set
   2713 
   2714 DIFF_STYLE=
   2715 NOT_FIXED=" (FIX MANUALLY)"
   2716 SCRATCHDIR="$( mkdtemp )" || err 2 "Can't create scratch directory"
   2717 trap "/bin/rm -rf \"\${SCRATCHDIR}\" ; exit 0" 1 2 3 15	# HUP INT QUIT TERM
   2718 
   2719 umask 022
   2720 exec 3>/dev/null
   2721 exec 4>/dev/null
   2722 exitstatus=0
   2723 
   2724 main "$@"
   2725 /bin/rm -rf "${SCRATCHDIR}"
   2726 exit $exitstatus
   2727