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