security revision 1.114
11.1Scgd#!/bin/sh -
21.1Scgd#
31.114Sspz#	$NetBSD: security,v 1.114 2013/11/06 19:30:20 spz Exp $
41.9Scgd#	from: @(#)security	8.1 (Berkeley) 6/9/93
51.1Scgd#
61.1Scgd
71.9ScgdPATH=/sbin:/usr/sbin:/bin:/usr/bin
81.1Scgd
91.89Sjmmvrcvar_manpage='security.conf(5)'
101.89Sjmmv
111.31Slukemif [ -f /etc/rc.subr ]; then
121.31Slukem	. /etc/rc.subr
131.31Slukemelse
141.31Slukem	echo "Can't read /etc/rc.subr; aborting."
151.31Slukem	exit 1;
161.31Slukemfi
171.31Slukem
181.9Scgdumask 077
191.64ScjsTZ=UTC; export TZ
201.1Scgd
211.15Smrgif [ -s /etc/security.conf ]; then
221.15Smrg	. /etc/security.conf
231.15Smrgfi
241.112Sagcif [ -s /etc/pkgpath.conf ]; then
251.112Sagc	. /etc/pkgpath.conf
261.112Sagcfi
271.15Smrg
281.67Slukem# Set reasonable defaults (if they're not set in security.conf)
291.67Slukem#
301.67Slukembackup_dir=${backup_dir:-/var/backups}
311.67Slukemmax_loginlen=${max_loginlen:-8}
321.67Slukemmax_grouplen=${max_grouplen:-8}
331.113Sprlw1pkg_admin=${pkg_admin:-/usr/sbin/pkg_admin}
341.104Sadrianppkg_info=${pkg_info:-/usr/sbin/pkg_info}
351.67Slukem
361.67Slukem# Other configurable variables
371.67Slukem#
381.67Slukemspecial_files="/etc/mtree/special /etc/mtree/special.local"
391.67SlukemMP=/etc/master.passwd
401.67SlukemCHANGELIST=""
411.67Slukemwork_dir=$backup_dir/work
421.67Slukem
431.67Slukemif [ ! -d "$work_dir" ]; then
441.67Slukem	mkdir -p "$work_dir"
451.67Slukemfi
461.67Slukem
471.102SmarttiSECUREDIR=$(mktemp -d -t _securedir) || exit 1
481.56Slukem
491.67Slukemtrap "/bin/rm -rf $SECUREDIR ; exit 0" EXIT INT QUIT PIPE
501.15Smrg
511.56Slukemif ! cd "$SECUREDIR"; then
521.56Slukem	echo "Can not cd to $SECUREDIR".
531.15Smrg	exit 1
541.15Smrgfi
551.15Smrg
561.91SlukemERR=err.$$
571.91SlukemTMP1=tmp1.$$
581.91SlukemTMP2=tmp2.$$
591.91SlukemMPBYUID=mpbyuid.$$
601.91SlukemMPBYPATH=mpbypath.$$
611.91SlukemLIST=list.$$
621.91SlukemOUTPUT=output.$$
631.91SlukemLABELS=labels.$$
641.106ShaadLVM_LABELS=lvm.$$
651.91SlukemPKGS=pkgs.$$
661.91SlukemCHANGEFILES=changefiles.$$
671.91SlukemSPECIALSPEC=specialspec.$$
681.67Slukem
691.108Sjmmvif [ -n "${pkgdb_dir}" ]; then
701.108Sjmmv    echo "WARNING: Setting pkgdb_dir in security.conf(5) is deprecated"
711.108Sjmmv    echo "WARNING: Please define PKG_DBDIR in pkg_install.conf(5) instead"
721.108Sjmmv    _compat_K_flag="-K ${pkgdb_dir}"
731.108Sjmmvfi
741.108Sjmmv
751.108Sjmmvhave_pkgs() {
761.108Sjmmv	$pkg_info ${_compat_K_flag} -q -E '*'
771.108Sjmmv}
781.108Sjmmv
791.67Slukem# migrate_file old new
801.67Slukem#	Determine if the "${old}" path name needs to be migrated to the
811.67Slukem#	"${new}" path. Also checks if "${old}.current" needs migrating,
821.67Slukem#	and if so, migrate it and possibly "${old}.current,v" and
831.67Slukem#	"${old}.backup".
841.67Slukem#
851.67Slukemmigrate_file()
861.67Slukem{
871.67Slukem	_old=$1
881.67Slukem	_new=$2
891.67Slukem	if [ -z "$_old" -o -z "$_new" ]; then
901.67Slukem		err 3 "USAGE: migrate_file old new"
911.67Slukem	fi
921.67Slukem	if [ ! -d "${_new%/*}" ]; then
931.67Slukem		mkdir -p "${_new%/*}"
941.67Slukem	fi
951.67Slukem	if [ -f "${_old}" -a ! -f "${_new}" ]; then
961.67Slukem		echo "==> migrating ${_old}"
971.67Slukem		echo "           to ${_new}"
981.67Slukem		mv "${_old}" "${_new}"
991.67Slukem	fi
1001.67Slukem	if [ -f "${_old}.current" -a ! -f "${_new}.current" ]; then
1011.67Slukem		echo "==> migrating ${_old}.current"
1021.67Slukem		echo "           to ${_new}.current"
1031.67Slukem		mv "${_old}.current" "${_new}.current"
1041.67Slukem		if [ -f "${_old}.current,v" -a ! -f "${_new}.current,v" ]; then
1051.67Slukem			echo "==> migrating ${_old}.current,v"
1061.67Slukem			echo "           to ${_new}.current,v"
1071.67Slukem			mv "${_old}.current,v" "${_new}.current,v"
1081.67Slukem		fi
1091.67Slukem		if [ -f "${_old}.backup" -a ! -f "${_new}.backup" ]; then
1101.67Slukem			echo "==> migrating ${_old}.backup"
1111.67Slukem			echo "           to ${_new}.backup"
1121.67Slukem			mv "${_old}.backup" "${_new}.backup"
1131.67Slukem		fi
1141.67Slukem	fi
1151.67Slukem}
1161.67Slukem
1171.67Slukem
1181.67Slukem# backup_and_diff file printdiff
1191.67Slukem#	Determine if file needs backing up, and if so, do it.
1201.67Slukem#	If printdiff is yes, display the diffs, otherwise 
1211.67Slukem#	just print a message saying "[changes omitted]".
1221.67Slukem#
1231.67Slukembackup_and_diff()
1241.67Slukem{
1251.67Slukem	_file=$1
1261.67Slukem	_printdiff=$2
1271.67Slukem	if [ -z "$_file" -o -z "$_printdiff" ]; then
1281.67Slukem		err 3 "USAGE: backup_and_diff file printdiff"
1291.67Slukem	fi
1301.67Slukem	! checkyesno _printdiff
1311.67Slukem	_printdiff=$?
1321.67Slukem
1331.67Slukem	_old=$backup_dir/${_file##*/}
1341.67Slukem	case "$_file" in
1351.67Slukem	$work_dir/*)
1361.67Slukem		_new=$_file
1371.67Slukem		migrate_file "$backup_dir/$_old" "$_new"
1381.67Slukem		migrate_file "$_old" "$_new"
1391.67Slukem		;;
1401.67Slukem	*)
1411.67Slukem		_new=$backup_dir/$_file
1421.67Slukem		migrate_file "$_old" "$_new"
1431.67Slukem		;;
1441.67Slukem	esac
1451.67Slukem	CUR=${_new}.current
1461.67Slukem	BACK=${_new}.backup
1471.67Slukem	if [ -f $_file ]; then
1481.67Slukem		if [ -f $CUR ] ; then
1491.67Slukem			if [ "$_printdiff" -ne 0 ]; then
1501.83Sjhawk				diff ${diff_options} $CUR $_file > $OUTPUT
1511.67Slukem			else
1521.67Slukem				if ! cmp -s $CUR $_file; then
1531.67Slukem					echo "[changes omitted]"
1541.67Slukem				fi > $OUTPUT
1551.67Slukem			fi
1561.67Slukem			if [ -s $OUTPUT ] ; then
1571.67Slukem				printf \
1581.67Slukem			"\n======\n%s diffs (OLD < > NEW)\n======\n" $_file
1591.67Slukem				cat $OUTPUT
1601.67Slukem				backup_file update $_file $CUR $BACK
1611.67Slukem			fi
1621.67Slukem		else
1631.67Slukem			printf "\n======\n%s added\n======\n" $_file
1641.67Slukem			if [ "$_printdiff" -ne 0 ]; then
1651.83Sjhawk				diff ${diff_options} /dev/null $_file
1661.67Slukem			else
1671.67Slukem				echo "[changes omitted]"
1681.67Slukem			fi
1691.67Slukem			backup_file add $_file $CUR $BACK
1701.67Slukem		fi
1711.67Slukem	else
1721.67Slukem		if [ -f $CUR ]; then
1731.67Slukem			printf "\n======\n%s removed\n======\n" $_file
1741.67Slukem			if [ "$_printdiff" -ne 0 ]; then
1751.83Sjhawk				diff ${diff_options} $CUR /dev/null
1761.67Slukem			else
1771.67Slukem				echo "[changes omitted]"
1781.67Slukem			fi
1791.67Slukem			backup_file remove $_file $CUR $BACK
1801.67Slukem		fi
1811.67Slukem	fi
1821.67Slukem}
1831.48Sabs
1841.9Scgd
1851.67Slukem# These are used several times.
1861.67Slukem#
1871.91Slukemawk -F: '!/^\+/ { print $1 " " $3 }' $MP | sort -k2n > $MPBYUID
1881.29Slukemawk -F: '{ print $1 " " $9 }' $MP | sort -k2 > $MPBYPATH
1891.91Slukemfor file in $special_files; do
1901.91Slukem	[ -s $file ] && cat $file
1911.91Slukemdone | mtree -CM -k all > $SPECIALSPEC || exit 1
1921.9Scgd
1931.67Slukem
1941.9Scgd# Check the master password file syntax.
1951.32Slukem#
1961.31Slukemif checkyesno check_passwd; then
1971.85Sjhawk        # XXX: the sense of permit_star is reversed; the code works as
1981.85Sjhawk        # implemented, but usage needs to be negated.
1991.81Sjhawk	checkyesno check_passwd_permit_star && permit_star=0 || permit_star=1
2001.94Sjdolecek	checkyesno check_passwd_permit_nonalpha \
2011.94Sjdolecek		 && permit_nonalpha=1 || permit_nonalpha=0
2021.94Sjdolecek
2031.81Sjhawk	awk -v "len=$max_loginlen" \
2041.81Sjhawk	    -v "nowarn_shells_list=$check_passwd_nowarn_shells" \
2051.81Sjhawk	    -v "nowarn_users_list=$check_passwd_nowarn_users" \
2061.94Sjdolecek	    -v "permit_star=$permit_star" \
2071.94Sjdolecek	    -v "permit_nonalpha=$permit_nonalpha" \
2081.94Sjdolecek	'
2091.25Slukem	BEGIN {
2101.25Slukem		while ( getline < "/etc/shells" > 0 ) {
2111.39Shubertf			if ($0 ~ /^\#/ || $0 ~ /^$/ )
2121.25Slukem				continue;
2131.25Slukem			shells[$1]++;
2141.25Slukem		}
2151.81Sjhawk		split(nowarn_shells_list, a);
2161.81Sjhawk		for (i in a) nowarn_shells[a[i]]++;
2171.81Sjhawk		split(nowarn_users_list, a);
2181.81Sjhawk		for (i in a) nowarn_users[a[i]]++;
2191.81Sjhawk		uid0_users_list="root toor"
2201.81Sjhawk		split(uid0_users_list, a);
2211.81Sjhawk		for (i in a) uid0_users[a[i]]++;
2221.25Slukem		FS=":";
2231.25Slukem	}
2241.25Slukem
2251.25Slukem	{
2261.15Smrg		if ($0 ~ /^[	 ]*$/) {
2271.25Slukem			printf "Line %d is a blank line.\n", NR;
2281.15Smrg			next;
2291.15Smrg		}
2301.105Sdholland
2311.105Sdholland		# NIS compat entry?
2321.105Sdholland		compatline = $1 ~ "^[\\+-]";
2331.105Sdholland		if (compatline) {
2341.105Sdholland			if ($1 == "+" && NF == 1) {
2351.105Sdholland				next;
2361.105Sdholland			}
2371.105Sdholland			sub("^.", "", $1);
2381.105Sdholland		}
2391.105Sdholland		if (NF != 10)
2401.25Slukem			printf "Line %d has the wrong number of fields.\n", NR;
2411.105Sdholland		if (compatline)  {
2421.105Sdholland			if ($3 == 0)
2431.81Sjhawk			    printf "Line %d includes entries with uid 0.\n",
2441.81Sjhawk			        NR;
2451.105Sdholland			if ($1 == "")
2461.105Sdholland			    next;
2471.34Sabs		}
2481.94Sjdolecek		if (!permit_nonalpha &&
2491.95Speter		    $1 !~ /^[_A-Za-z0-9]([-A-Za-z0-9_.]*[A-Za-z0-9])*$/)
2501.25Slukem			printf "Login %s has non-alphanumeric characters.\n",
2511.25Slukem			    $1;
2521.34Sabs		if (length($1) > len)
2531.81Sjhawk			printf "Login %s has more than "len" characters.\n",
2541.81Sjhawk			    $1;
2551.105Sdholland		if ($2 == "" && !compatline && !nowarn_users[$1])
2561.81Sjhawk			    printf "Login %s has no password.\n", $1;
2571.81Sjhawk		if (!nowarn_shells[$10] && !nowarn_users[$1]) {
2581.81Sjhawk		    if (length($2) != 13 &&
2591.81Sjhawk		    	length($2) != 20 &&
2601.81Sjhawk		    	$2 !~ /^\$1/ &&
2611.81Sjhawk		    	$2 !~ /^\$2/ &&
2621.99Sjmcneill			$2 !~ /^\$sha1/ &&
2631.81Sjhawk		    	$2 != "" &&
2641.81Sjhawk			(permit_star || $2 != "*") &&
2651.81Sjhawk		    	$2 !~ /^\*[A-z-]+$/ &&
2661.81Sjhawk			$1 != "toor") {
2671.81Sjhawk		    	    if ($10 == "" || shells[$10])
2681.81Sjhawk				printf "Login %s is off but still has "\
2691.81Sjhawk				  "a valid shell (%s)\n", $1, $10;
2701.105Sdholland		    } else if (compatline && $10 == "") {
2711.105Sdholland			    # nothing
2721.81Sjhawk		    } else if (! shells[$10])
2731.81Sjhawk		    	    printf "Login %s does not have a valid "\
2741.81Sjhawk			    "shell (%s)\n", $1, $10;
2751.81Sjhawk		}
2761.81Sjhawk		if ($3 == 0 && !uid0_users[$1] && !nowarn_users[$1])
2771.25Slukem			printf "Login %s has a user id of 0.\n", $1;
2781.105Sdholland		if ($3 != "" && $3 < 0)
2791.25Slukem			printf "Login %s has a negative user id.\n", $1;
2801.105Sdholland		if ($4 != "" && $4 < 0)
2811.25Slukem			printf "Login %s has a negative group id.\n", $1;
2821.15Smrg	}' < $MP > $OUTPUT
2831.15Smrg	if [ -s $OUTPUT ] ; then
2841.15Smrg		printf "\nChecking the $MP file:\n"
2851.15Smrg		cat $OUTPUT
2861.15Smrg	fi
2871.15Smrg
2881.15Smrg	awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
2891.15Smrg	if [ -s $OUTPUT ] ; then
2901.15Smrg		printf "\n$MP has duplicate user names.\n"
2911.15Smrg		column $OUTPUT
2921.15Smrg	fi
2931.15Smrg
2941.111Sspz	awk -v "permit_dups_list=$check_passwd_permit_dups" \
2951.111Sspz	'
2961.111Sspz	BEGIN {
2971.111Sspz		split(permit_dups_list, a);
2981.111Sspz		for (i in a) permit_dups[a[i]]++;
2991.111Sspz	}
3001.111Sspz	{
3011.111Sspz		if (!permit_dups[$1])
3021.111Sspz			print $2;
3031.111Sspz	}' < $MPBYUID | uniq -d > $TMP2
3041.15Smrg	if [ -s $TMP2 ] ; then
3051.111Sspz		printf "\n$MP has duplicate user ids.\n"
3061.15Smrg		while read uid; do
3071.28Slukem			grep -w $uid $MPBYUID
3081.15Smrg		done < $TMP2 | column
3091.15Smrg	fi
3101.9Scgdfi
3111.9Scgd
3121.9Scgd# Check the group file syntax.
3131.32Slukem#
3141.31Slukemif checkyesno check_group; then
3151.15Smrg	GRP=/etc/group
3161.49Sjdolecek	awk -F: -v "len=$max_grouplen" '{
3171.15Smrg		if ($0 ~ /^[	 ]*$/) {
3181.25Slukem			printf "Line %d is a blank line.\n", NR;
3191.15Smrg			next;
3201.15Smrg		}
3211.34Sabs		if (NF != 4 && ($1 != "+" || NF != 1))
3221.25Slukem			printf "Line %d has the wrong number of fields.\n", NR;
3231.34Sabs		if ($1 == "+" )  {
3241.34Sabs			next;
3251.34Sabs		}
3261.95Speter		if ($1 !~ /^[_A-Za-z0-9]([-A-Za-z0-9_.]*[A-Za-z0-9])*$/)
3271.25Slukem			printf "Group %s has non-alphanumeric characters.\n",
3281.25Slukem			    $1;
3291.49Sjdolecek		if (length($1) > len)
3301.49Sjdolecek			printf "Group %s has more than "len" characters.\n", $1;
3311.15Smrg		if ($3 !~ /[0-9]*/)
3321.25Slukem			printf "Login %s has a negative group id.\n", $1;
3331.15Smrg	}' < $GRP > $OUTPUT
3341.15Smrg	if [ -s $OUTPUT ] ; then
3351.15Smrg		printf "\nChecking the $GRP file:\n"
3361.15Smrg		cat $OUTPUT
3371.15Smrg	fi
3381.15Smrg
3391.15Smrg	awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
3401.114Sspz	dupgroups=""
3411.114Sspz	for group in $(cat $OUTPUT) ; do
3421.114Sspz		gcount=$(awk -F: "/$group/ { print \$1,\$3 }" $GRP | sort -u | wc -l)
3431.114Sspz		if [ $gcount -gt 1 ]; then
3441.114Sspz			dupgroups="$dupgroups $group"
3451.114Sspz		fi
3461.114Sspz	done
3471.114Sspz	if [ ! -z $dupgroups ] ; then
3481.15Smrg		printf "\n$GRP has duplicate group names.\n"
3491.114Sspz		printf "$dupgroups\n"
3501.15Smrg	fi
3511.9Scgdfi
3521.9Scgd
3531.9Scgd# Check for root paths, umask values in startup files.
3541.9Scgd# The check for the root paths is problematical -- it's likely to fail
3551.9Scgd# in other environments.  Once the shells have been modified to warn
3561.9Scgd# of '.' in the path, the path tests should go away.
3571.32Slukem#
3581.31Slukemif checkyesno check_rootdotfiles; then
3591.67Slukem	rhome=~root
3601.15Smrg	umaskset=no
3611.15Smrg	list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
3621.15Smrg	for i in $list ; do
3631.15Smrg		if [ -f $i ] ; then
3641.67Slukem			if egrep '^[ \t]*umask[ \t]+[0-7]+' $i > /dev/null ;
3651.67Slukem			then
3661.15Smrg				umaskset=yes
3671.15Smrg			fi
3681.63Slukem			# Double check the umask value itself; ensure that
3691.67Slukem			# both the group and other write bits are set.
3701.67Slukem			#
3711.45Ssommerfe			egrep '^[ \t]*umask[ \t]+[0-7]+' $i |
3721.63Slukem			awk '{
3731.67Slukem				if ($2 ~ /^.$/ || $2 ~! /[^2367].$/) {
3741.80Swiz					print "\tRoot umask is group writable"
3751.63Slukem				}
3761.67Slukem				if ($2 ~ /[^2367]$/) {
3771.80Swiz					print "\tRoot umask is other writable"
3781.63Slukem			    	}
3791.67Slukem			    }' | sort -u
3801.26Slukem			SAVE_PATH=$PATH
3811.26Slukem			unset PATH
3821.15Smrg			/bin/csh -f -s << end-of-csh > /dev/null 2>&1
3831.15Smrg				source $i
3841.15Smrg				/bin/ls -ldgT \$path > $TMP1
3851.9Scgdend-of-csh
3861.76Satatat			export PATH=$SAVE_PATH
3871.15Smrg			awk '{
3881.15Smrg				if ($10 ~ /^\.$/) {
3891.27Slukem					print "\tThe root path includes .";
3901.15Smrg					next;
3911.15Smrg				}
3921.15Smrg			     }
3931.15Smrg			     $1 ~ /^d....w/ \
3941.80Swiz		{ print "\tRoot path directory " $10 " is group writable." } \
3951.15Smrg			     $1 ~ /^d.......w/ \
3961.80Swiz		{ print "\tRoot path directory " $10 " is other writable." }' \
3971.67Slukem			< $TMP1
3981.15Smrg		fi
3991.67Slukem	done > $OUTPUT
4001.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
4011.27Slukem		printf "\nChecking root csh paths, umask values:\n$list\n\n"
4021.15Smrg		if [ -s $OUTPUT ]; then
4031.15Smrg			cat $OUTPUT
4041.15Smrg		fi
4051.15Smrg		if [ $umaskset = "no" ] ; then
4061.27Slukem		    printf "\tRoot csh startup files do not set the umask.\n"
4071.15Smrg		fi
4081.9Scgd	fi
4091.9Scgd
4101.15Smrg	umaskset=no
4111.23Slukem	list="/etc/profile ${rhome}/.profile"
4121.15Smrg	for i in $list; do
4131.15Smrg		if [ -f $i ] ; then
4141.15Smrg			if egrep umask $i > /dev/null ; then
4151.15Smrg				umaskset=yes
4161.15Smrg			fi
4171.15Smrg			egrep umask $i |
4181.67Slukem			awk '$2 ~ /^.$/ || $2 ~ /[^2367].$/ \
4191.80Swiz				{ print "\tRoot umask is group writable" } \
4201.67Slukem			     $2 ~ /[^2367]$/ \
4211.80Swiz				{ print "\tRoot umask is other writable" }'
4221.26Slukem			SAVE_PATH=$PATH
4231.26Slukem			unset PATH
4241.15Smrg			/bin/sh << end-of-sh > /dev/null 2>&1
4251.15Smrg				. $i
4261.110Schristos				list=\$(echo \$PATH | /usr/bin/sed -e \
4271.110Schristos				    's/^:/.:/;s/:$/:./;s/::/:.:/g;s/:/ /g')
4281.15Smrg				/bin/ls -ldgT \$list > $TMP1
4291.9Scgdend-of-sh
4301.76Satatat			export PATH=$SAVE_PATH
4311.15Smrg			awk '{
4321.15Smrg				if ($10 ~ /^\.$/) {
4331.27Slukem					print "\tThe root path includes .";
4341.15Smrg					next;
4351.15Smrg				}
4361.15Smrg			     }
4371.15Smrg			     $1 ~ /^d....w/ \
4381.80Swiz		{ print "\tRoot path directory " $10 " is group writable." } \
4391.15Smrg			     $1 ~ /^d.......w/ \
4401.80Swiz		{ print "\tRoot path directory " $10 " is other writable." }' \
4411.67Slukem			< $TMP1
4421.9Scgd
4431.15Smrg		fi
4441.67Slukem	done > $OUTPUT
4451.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
4461.15Smrg		printf "\nChecking root sh paths, umask values:\n$list\n"
4471.15Smrg		if [ -s $OUTPUT ]; then
4481.15Smrg			cat $OUTPUT
4491.15Smrg		fi
4501.15Smrg		if [ $umaskset = "no" ] ; then
4511.27Slukem			printf "\tRoot sh startup files do not set the umask.\n"
4521.15Smrg		fi
4531.9Scgd	fi
4541.9Scgdfi
4551.9Scgd
4561.9Scgd# Root and uucp should both be in /etc/ftpusers.
4571.32Slukem#
4581.31Slukemif checkyesno check_ftpusers; then
4591.109Schristos	list="uucp "$(awk '$2 == 0 { print $1 }' $MPBYUID)
4601.27Slukem	for i in $list; do
4611.29Slukem		if /usr/libexec/ftpd -C $i ; then
4621.67Slukem			printf "\t$i is not denied\n"
4631.27Slukem		fi
4641.67Slukem	done > $OUTPUT
4651.28Slukem	if [ -s $OUTPUT ]; then
4661.28Slukem		printf "\nChecking the /etc/ftpusers configuration:\n"
4671.28Slukem		cat $OUTPUT
4681.28Slukem	fi
4691.9Scgdfi
4701.9Scgd
4711.43Sitojun# Uudecode should not be in the /etc/mail/aliases file.
4721.32Slukem#
4731.31Slukemif checkyesno check_aliases; then
4741.43Sitojun	for f in /etc/mail/aliases /etc/aliases; do
4751.43Sitojun		if [ -f $f ] && egrep '^[^#]*(uudecode|decode).*\|' $f; then
4761.43Sitojun			printf "\nEntry for uudecode in $f file.\n"
4771.43Sitojun		fi
4781.43Sitojun	done
4791.9Scgdfi
4801.9Scgd
4811.9Scgd# Files that should not have + signs.
4821.32Slukem#
4831.31Slukemif checkyesno check_rhosts; then
4841.15Smrg	list="/etc/hosts.equiv /etc/hosts.lpd"
4851.15Smrg	for f in $list ; do
4861.15Smrg		if [ -f $f ] && egrep '\+' $f > /dev/null ; then
4871.15Smrg			printf "\nPlus sign in $f file.\n"
4881.15Smrg		fi
4891.15Smrg	done
4901.15Smrg
4911.15Smrg	# Check for special users with .rhosts files.  Only root and toor should
4921.16Smikel	# have .rhosts files.  Also, .rhosts files should not have plus signs.
4931.15Smrg	awk -F: '$1 != "root" && $1 != "toor" && \
4941.15Smrg		($3 < 100 || $1 == "ftp" || $1 == "uucp") \
4951.20Smycroft			{ print $1 " " $9 }' $MP |
4961.19Smycroft	sort -k2 |
4971.15Smrg	while read uid homedir; do
4981.15Smrg		if [ -f ${homedir}/.rhosts ] ; then
4991.109Schristos			rhost=$(ls -ldgT ${homedir}/.rhosts)
5001.46Schristos			printf -- "$uid: $rhost\n"
5011.15Smrg		fi
5021.15Smrg	done > $OUTPUT
5031.15Smrg	if [ -s $OUTPUT ] ; then
5041.15Smrg		printf "\nChecking for special users with .rhosts files.\n"
5051.15Smrg		cat $OUTPUT
5061.15Smrg	fi
5071.15Smrg
5081.15Smrg	while read uid homedir; do
5091.35Sfair		if [ -f ${homedir}/.rhosts -a -r ${homedir}/.rhosts ] && \
5101.41Schristos		    cat -f ${homedir}/.rhosts | egrep '\+' > /dev/null ; then
5111.46Schristos			printf -- "$uid: + in .rhosts file.\n"
5121.15Smrg		fi
5131.29Slukem	done < $MPBYPATH > $OUTPUT
5141.15Smrg	if [ -s $OUTPUT ] ; then
5151.15Smrg		printf "\nChecking .rhosts files syntax.\n"
5161.15Smrg		cat $OUTPUT
5171.15Smrg	fi
5181.9Scgdfi
5191.9Scgd
5201.9Scgd# Check home directories.  Directories should not be owned by someone else
5211.80Swiz# or writable.
5221.32Slukem#
5231.31Slukemif checkyesno check_homes; then
5241.85Sjhawk	checkyesno check_homes_permit_usergroups && \
5251.85Sjhawk		permit_usergroups=1 || permit_usergroups=0
5261.15Smrg	while read uid homedir; do
5271.15Smrg		if [ -d ${homedir}/ ] ; then
5281.109Schristos			file=$(ls -ldgT ${homedir})
5291.46Schristos			printf -- "$uid $file\n"
5301.9Scgd		fi
5311.29Slukem	done < $MPBYPATH |
5321.85Sjhawk	awk -v "usergroups=$permit_usergroups" '
5331.85Sjhawk	     $1 != $4 && $4 != "root" \
5341.15Smrg		{ print "user " $1 " home directory is owned by " $4 }
5351.101Sjnemeth	     $2 ~ /^d....w/ && (!usergroups || $5 != $1) \
5361.80Swiz		{ print "user " $1 " home directory is group writable" }
5371.101Sjnemeth	     $2 ~ /^d.......w/ \
5381.80Swiz		{ print "user " $1 " home directory is other writable" }' \
5391.27Slukem	    > $OUTPUT
5401.15Smrg	if [ -s $OUTPUT ] ; then
5411.15Smrg		printf "\nChecking home directories.\n"
5421.15Smrg		cat $OUTPUT
5431.15Smrg	fi
5441.15Smrg
5451.15Smrg	# Files that should not be owned by someone else or readable.
5461.67Slukem	list=".Xauthority .netrc .ssh/id_dsa .ssh/id_rsa .ssh/identity"
5471.15Smrg	while read uid homedir; do
5481.15Smrg		for f in $list ; do
5491.15Smrg			file=${homedir}/${f}
5501.15Smrg			if [ -f $file ] ; then
5511.109Schristos				printf -- "$uid $f $(ls -ldgT $file)\n"
5521.15Smrg			fi
5531.15Smrg		done
5541.29Slukem	done < $MPBYPATH |
5551.85Sjhawk	awk  -v "usergroups=$permit_usergroups" '
5561.85Sjhawk	     $1 != $5 && $5 != "root" \
5571.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
5581.85Sjhawk	     $3 ~ /^-...r/ && (!usergroups || $6 != $1) \
5591.15Smrg		{ print "user " $1 " " $2 " file is group readable" }
5601.15Smrg	     $3 ~ /^-......r/ \
5611.15Smrg		{ print "user " $1 " " $2 " file is other readable" }
5621.85Sjhawk	     $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
5631.80Swiz		{ print "user " $1 " " $2 " file is group writable" }
5641.15Smrg	     $3 ~ /^-.......w/ \
5651.80Swiz		{ print "user " $1 " " $2 " file is other writable" }' \
5661.27Slukem	    > $OUTPUT
5671.15Smrg
5681.80Swiz	# Files that should not be owned by someone else or writable.
5691.19Smycroft	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
5701.79Selric	      .cshrc .emacs .exrc .forward .history .k5login .klogin .login \
5711.79Selric	      .logout .profile .qmail .rc_history .rhosts .shosts ssh .tcshrc \
5721.79Selric	      .twmrc .xinitrc .xsession .ssh/authorized_keys \
5731.79Selric	      .ssh/authorized_keys2 .ssh/config .ssh/id_dsa.pub \
5741.79Selric	      .ssh/id_rsa.pub .ssh/identity.pub .ssh/known_hosts \
5751.79Selric	      .ssh/known_hosts2"
5761.15Smrg	while read uid homedir; do
5771.15Smrg		for f in $list ; do
5781.15Smrg			file=${homedir}/${f}
5791.15Smrg			if [ -f $file ] ; then
5801.109Schristos				printf -- "$uid $f $(ls -ldgT $file)\n"
5811.15Smrg			fi
5821.15Smrg		done
5831.29Slukem	done < $MPBYPATH |
5841.85Sjhawk	awk -v "usergroups=$permit_usergroups" '
5851.85Sjhawk	     $1 != $5 && $5 != "root" \
5861.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
5871.85Sjhawk	     $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
5881.80Swiz		{ print "user " $1 " " $2 " file is group writable" }
5891.15Smrg	     $3 ~ /^-.......w/ \
5901.80Swiz		{ print "user " $1 " " $2 " file is other writable" }' \
5911.27Slukem	    >> $OUTPUT
5921.15Smrg	if [ -s $OUTPUT ] ; then
5931.15Smrg		printf "\nChecking dot files.\n"
5941.15Smrg		cat $OUTPUT
5951.15Smrg	fi
5961.9Scgdfi
5971.9Scgd
5981.9Scgd# Mailboxes should be owned by user and unreadable.
5991.32Slukem#
6001.31Slukemif checkyesno check_varmail; then
6011.86Sjhawk	ls -lA /var/mail | \
6021.63Slukem	awk '	NR == 1 { next; }
6031.86Sjhawk		$9 ~ /^\./ {next; }
6041.63Slukem	    	$3 != $9 {
6051.63Slukem			print "user " $9 " mailbox is owned by " $3
6061.63Slukem		}
6071.63Slukem		$1 != "-rw-------" {
6081.63Slukem			print "user " $9 " mailbox is " $1 ", group " $4
6091.63Slukem		}' > $OUTPUT
6101.15Smrg	if [ -s $OUTPUT ] ; then
6111.15Smrg		printf "\nChecking mailbox ownership.\n"
6121.15Smrg		cat $OUTPUT
6131.15Smrg	fi
6141.15Smrgfi
6151.15Smrg
6161.32Slukem# NFS exports shouldn't be globally exported
6171.32Slukem#
6181.32Slukemif checkyesno check_nfs && [ -f /etc/exports ]; then
6191.32Slukem	awk '{
6201.22Slukem		# ignore comments and blank lines
6211.39Shubertf		if ($0 ~ /^\#/ || $0 ~ /^$/ )
6221.22Slukem			next;
6231.100Stron		# manage line continuation
6241.100Stron		while ($NF ~ /^\\$/) {
6251.100Stron			$NF = "";
6261.100Stron			line = $0 "";
6271.100Stron			getline;
6281.100Stron			$0 = line $0 "";
6291.100Stron		}
6301.22Slukem
6311.100Stron		delete dir;
6321.100Stron		readonly = ndir = 0;
6331.100Stron		for (i = 1; i <= NF; ++i) {
6341.100Stron			if ($i ~ /^\//) dir[ndir++] = $i;
6351.100Stron			else if ($i ~ /^-/) {
6361.100Stron				if ($i ~ /^-(ro|o)$/) readonly = 1;
6371.100Stron				if ($i ~ /^-network/) next;
6381.100Stron			}
6391.100Stron			else next;
6401.15Smrg		}
6411.15Smrg		if (readonly)
6421.100Stron			for (item in dir)
6431.100Stron				rodir[nrodir++] = dir[item];
6441.15Smrg		else
6451.100Stron			for (item in dir)
6461.100Stron				rwdir[nrwdir++] = dir[item];
6471.100Stron
6481.100Stron	}
6491.100Stron
6501.100Stron	END {
6511.100Stron		if (nrodir) {
6521.100Stron			printf("Globally exported file system%s, read-only:\n",
6531.100Stron				nrodir > 1 ? "s" : "");
6541.100Stron			for (item in rodir)
6551.100Stron				printf("\t%s\n", rodir[item]);
6561.100Stron		}
6571.100Stron		if (nrwdir) {
6581.100Stron			printf("Globally exported file system%s, read-write:\n",
6591.100Stron				nrwdir > 1 ? "s" : "");
6601.100Stron			for (item in rwdir)
6611.100Stron				printf("\t%s\n", rwdir[item]);
6621.100Stron		}
6631.32Slukem	}' < /etc/exports > $OUTPUT
6641.32Slukem	if [ -s $OUTPUT ] ; then
6651.15Smrg		printf "\nChecking for globally exported file systems.\n"
6661.15Smrg		cat $OUTPUT
6671.15Smrg	fi
6681.9Scgdfi
6691.9Scgd
6701.9Scgd# Display any changes in setuid files and devices.
6711.32Slukem#
6721.31Slukemif checkyesno check_devices; then
6731.28Slukem	> $ERR
6741.92Serh	(
6751.98Slukem
6761.98Slukem	# Convert check_devices_ignore_fstypes="foo !bar bax"
6771.98Slukem	#    into "-fstype foo -o ! -fstype bar -o -fstype bax"
6781.98Slukem	# and check_devices_ignore_paths="/foo !/bar /bax"
6791.98Slukem	#    into " -path /foo -o ! -path /bar -o -path /bax"
6801.98Slukem	#
6811.98Slukem	ignexpr=$(\
6821.98Slukem	    echo $check_devices_ignore_fstypes | \
6831.98Slukem		sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -fstype \2/g' ; \
6841.98Slukem	    echo $check_devices_ignore_paths | \
6851.98Slukem		sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -path \2/g' \
6861.98Slukem	)
6871.98Slukem
6881.98Slukem	# Massage the expression into ( $ignexpr ) -a -prune -o
6891.98Slukem	if [ -n "${ignexpr}" ]; then
6901.98Slukem		ignexpr=$(\
6911.98Slukem			echo $ignexpr | \
6921.98Slukem			    sed -e 's/^-o /( /' \
6931.98Slukem				-e 's/$/ ) -a -prune -o/' \
6941.98Slukem		)
6951.98Slukem	fi
6961.98Slukem
6971.98Slukem	find / $ignexpr \
6981.21Smycroft	    \( \( -perm -u+s -a ! -type d \) -o \
6991.21Smycroft	       \( -perm -g+s -a ! -type d \) -o \
7001.24Slukem	       -type b -o -type c \) -print0 | \
7011.98Slukem	xargs -0 ls -ldgTq | sort +9 > $LIST
7021.98Slukem
7031.98Slukem	) 2> $OUTPUT
7041.15Smrg
7051.15Smrg	# Display any errors that occurred during system file walk.
7061.15Smrg	if [ -s $OUTPUT ] ; then
7071.28Slukem		printf "Setuid/device find errors:\n" >> $ERR
7081.28Slukem		cat $OUTPUT >> $ERR
7091.28Slukem		printf "\n" >> $ERR
7101.15Smrg	fi
7111.15Smrg
7121.15Smrg	# Display any changes in the setuid file list.
7131.15Smrg	egrep -v '^[bc]' $LIST > $TMP1
7141.15Smrg	if [ -s $TMP1 ] ; then
7151.15Smrg		# Check to make sure uudecode isn't setuid.
7161.15Smrg		if grep -w uudecode $TMP1 > /dev/null ; then
7171.28Slukem			printf "\nUudecode is setuid.\n" >> $ERR
7181.15Smrg		fi
7191.15Smrg
7201.67Slukem		file=$work_dir/setuid
7211.67Slukem		migrate_file "$backup_dir/setuid" "$file"
7221.67Slukem		CUR=${file}.current
7231.67Slukem		BACK=${file}.backup
7241.15Smrg		if [ -s $CUR ] ; then
7251.15Smrg			if cmp -s $CUR $TMP1 ; then
7261.15Smrg				:
7271.15Smrg			else
7281.15Smrg				> $TMP2
7291.15Smrg				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
7301.15Smrg				if [ -s $OUTPUT ] ; then
7311.28Slukem					printf "Setuid additions:\n" >> $ERR
7321.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
7331.28Slukem					printf "\n" >> $ERR
7341.15Smrg				fi
7351.15Smrg
7361.15Smrg				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
7371.15Smrg				if [ -s $OUTPUT ] ; then
7381.28Slukem					printf "Setuid deletions:\n" >> $ERR
7391.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
7401.28Slukem					printf "\n" >> $ERR
7411.15Smrg				fi
7421.15Smrg
7431.20Smycroft				sort -k10 $TMP2 $CUR $TMP1 | \
7441.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
7451.27Slukem				    uniq -u > $OUTPUT
7461.15Smrg				if [ -s $OUTPUT ] ; then
7471.28Slukem					printf "Setuid changes:\n" >> $ERR
7481.28Slukem					column -t $OUTPUT >> $ERR
7491.28Slukem					printf "\n" >> $ERR
7501.15Smrg				fi
7511.9Scgd
7521.52Satatat				backup_file update $TMP1 $CUR $BACK
7531.9Scgd			fi
7541.15Smrg		else
7551.28Slukem			printf "Setuid additions:\n" >> $ERR
7561.28Slukem			column -t $TMP1 >> $ERR
7571.28Slukem			printf "\n" >> $ERR
7581.52Satatat			backup_file add $TMP1 $CUR $BACK
7591.9Scgd		fi
7601.15Smrg	fi
7611.15Smrg
7621.27Slukem	# Check for block and character disk devices that are readable or
7631.80Swiz	# writable or not owned by root.operator.
7641.15Smrg	>$TMP1
7651.61Slukem	DISKLIST="ccd ch hk hp ld md ra raid rb rd rl rx \
7661.57Ssimonb	    sd se ss uk up vnd wd xd xy"
7671.27Slukem#	DISKLIST="$DISKLIST ct mt st wt"
7681.15Smrg	for i in $DISKLIST; do
7691.15Smrg		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
7701.15Smrg		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
7711.15Smrg	done
7721.15Smrg
7731.15Smrg	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
7741.25Slukem		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
7751.25Slukem		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
7761.15Smrg	if [ -s $OUTPUT ] ; then
7771.28Slukem		printf "\nChecking disk ownership and permissions.\n" >> $ERR
7781.28Slukem		cat $OUTPUT >> $ERR
7791.28Slukem		printf "\n" >> $ERR
7801.9Scgd	fi
7811.9Scgd
7821.15Smrg	# Display any changes in the device file list.
7831.20Smycroft	egrep '^[bc]' $LIST | sort -k11 > $TMP1
7841.15Smrg	if [ -s $TMP1 ] ; then
7851.67Slukem		file=$work_dir/device
7861.67Slukem		migrate_file "$backup_dir/device" "$file"
7871.67Slukem		CUR=${file}.current
7881.67Slukem		BACK=${file}.backup
7891.15Smrg
7901.15Smrg		if [ -s $CUR ] ; then
7911.15Smrg			if cmp -s $CUR $TMP1 ; then
7921.15Smrg				:
7931.15Smrg			else
7941.15Smrg				> $TMP2
7951.15Smrg				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
7961.15Smrg				if [ -s $OUTPUT ] ; then
7971.28Slukem					printf "Device additions:\n" >> $ERR
7981.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
7991.28Slukem					printf "\n" >> $ERR
8001.15Smrg				fi
8011.15Smrg
8021.15Smrg				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
8031.15Smrg				if [ -s $OUTPUT ] ; then
8041.28Slukem					printf "Device deletions:\n" >> $ERR
8051.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
8061.28Slukem					printf "\n" >> $ERR
8071.15Smrg				fi
8081.15Smrg
8091.27Slukem				# Report any block device change. Ignore
8101.27Slukem				# character devices, only the name is
8111.27Slukem				# significant.
8121.15Smrg				cat $TMP2 $CUR $TMP1 | \
8131.27Slukem				    sed -e '/^c/d' | \
8141.27Slukem				    sort -k11 | \
8151.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
8161.27Slukem				    uniq -u > $OUTPUT
8171.15Smrg				if [ -s $OUTPUT ] ; then
8181.28Slukem					printf "Block device changes:\n" >> $ERR
8191.28Slukem					column -t $OUTPUT >> $ERR
8201.28Slukem					printf "\n" >> $ERR
8211.15Smrg				fi
8221.9Scgd
8231.52Satatat				backup_file update $TMP1 $CUR $BACK
8241.9Scgd			fi
8251.15Smrg		else
8261.28Slukem			printf "Device additions:\n" >> $ERR
8271.28Slukem			column -t $TMP1 >> $ERR
8281.28Slukem			printf "\n" >> $ERR
8291.52Satatat			backup_file add $TMP1 $CUR $BACK >> $ERR
8301.9Scgd		fi
8311.28Slukem	fi
8321.28Slukem	if [ -s $ERR ] ; then
8331.28Slukem		printf "\nChecking setuid files and devices:\n"
8341.28Slukem		cat $ERR
8351.28Slukem		printf "\n"
8361.9Scgd	fi
8371.9Scgdfi
8381.9Scgd
8391.9Scgd# Check special files.
8401.9Scgd# Check system binaries.
8411.9Scgd#
8421.9Scgd# Create the mtree tree specifications using:
8431.67Slukem#	mtree -cx -pDIR -kmd5,uid,gid,mode,nlink,size,link,time > DIR.secure
8441.38Skleink#	chown root:wheel DIR.secure
8451.67Slukem#	chmod u+r,go= DIR.secure
8461.9Scgd#
8471.9Scgd# Note, this is not complete protection against Trojan horsed binaries, as
8481.9Scgd# the hacker can modify the tree specification to match the replaced binary.
8491.9Scgd# For details on really protecting yourself against modified binaries, see
8501.9Scgd# the mtree(8) manual page.
8511.32Slukem#
8521.31Slukemif checkyesno check_mtree; then
8531.82Sjhawk	if checkyesno check_mtree_follow_symlinks; then
8541.82Sjhawk		check_mtree_flags="-L"
8551.82Sjhawk	else
8561.82Sjhawk		check_mtree_flags=""
8571.82Sjhawk	fi
8581.91Slukem	mtree -e -l -p / $check_mtree_flags -f $SPECIALSPEC 3>&1 >$OUTPUT 2>&3 |
8591.87Sjhawk		grep -v '^mtree: dev/tty: Device not configured$' >&2
8601.15Smrg	if [ -s $OUTPUT ]; then
8611.9Scgd		printf "\nChecking special files and directories.\n"
8621.9Scgd		cat $OUTPUT
8631.9Scgd	fi
8641.9Scgd
8651.16Smikel	for file in /etc/mtree/*.secure; do
8661.16Smikel		[ $file = '/etc/mtree/*.secure' ] && continue
8671.109Schristos		tree=$(sed -n -e '3s/.* //p' -e 3q $file)
8681.82Sjhawk		mtree $check_mtree_flags -f $file -p $tree > $TMP1
8691.9Scgd		if [ -s $TMP1 ]; then
8701.67Slukem			printf "\nChecking $tree:\n"
8711.67Slukem			cat $TMP1
8721.9Scgd		fi
8731.67Slukem	done > $OUTPUT
8741.15Smrg	if [ -s $OUTPUT ]; then
8751.9Scgd		printf "\nChecking system binaries:\n"
8761.9Scgd		cat $OUTPUT
8771.9Scgd	fi
8781.9Scgdfi
8791.9Scgd
8801.32Slukem# Backup disklabels of available disks
8811.32Slukem#
8821.32Slukemif checkyesno check_disklabels; then
8831.67Slukem		# migrate old disklabels
8841.109Schristos	for file in $(ls -1d $backup_dir/$backup_dir/disklabel.* \
8851.109Schristos	    $backup_dir/disklabel.* 2>/dev/null); do
8861.67Slukem		migrate_file "$file" "$work_dir/${file##*/}"
8871.67Slukem	done
8881.67Slukem
8891.103Stron		# generate list of old disklabels, fdisks & wedges and remove them
8901.103Stron	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* $work_dir/wedges.* 2>/dev/null |
8911.52Satatat	    egrep -v '\.(backup|current)(,v)?$' > $LABELS
8921.32Slukem	xargs rm < $LABELS
8931.32Slukem
8941.103Stron		# generate disklabels of all disks excluding:	cd dk fd md st
8951.109Schristos	disks=$(iostat -x | awk 'NR > 1 && $1 !~ /^[cfm]d|dk|st|nfs/ { print $1; }')
8961.32Slukem	for i in $disks; do
8971.67Slukem		disklabel $i > "$work_dir/disklabel.$i" 2>/dev/null
8981.32Slukem	done
8991.32Slukem
9001.67Slukem		# if fdisk is available, generate fdisks for:	ed ld sd wd
9011.67Slukem	if [ -x /sbin/fdisk ]; then
9021.109Schristos		disks=$(iostat -x | awk 'NR > 1 && $1 ~ /^[elsw]d/ { print $1; }')
9031.67Slukem		for i in $disks; do
9041.67Slukem			/sbin/fdisk $i > "$work_dir/fdisk.$i" 2>/dev/null
9051.67Slukem		done
9061.67Slukem	fi
9071.67Slukem
9081.103Stron		# if dkctl is available, generate dkctl listwedges for:	ed ld sd wd cgd ofdisk ra rl raid
9091.103Stron	if [ -x /sbin/dkctl ]; then
9101.109Schristos		disks=$(iostat -x | awk 'NR > 1 && $1 ~ /^[elsw]d|cgd|ofdisk|r[al]|raid/ { print $1; }')
9111.103Stron		for i in $disks; do
9121.103Stron			/sbin/dkctl $i listwedges > "$work_dir/wedges.$i" 2>/dev/null
9131.103Stron		done
9141.103Stron	fi
9151.103Stron
9161.103Stron		# append list of new disklabels, fdisks and wedges
9171.103Stron	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* $work_dir/wedges.* 2>/dev/null |
9181.52Satatat	    egrep -v '\.(backup|current)(,v)?$' >> $LABELS
9191.62Satatat	CHANGELIST="$LABELS $CHANGELIST"
9201.62Satatatfi
9211.62Satatat
9221.106Shaadif checkyesno check_lvm; then
9231.106Shaad    
9241.106Shaad    # generate list of existing LVM elements Physical Volumes, Volume Groups and Logical Volumes.
9251.106Shaadif [ -x /sbin/lvm ]; then
9261.106Shaad    lvm pvdisplay -m >"$work_dir/lvm.pv" 2>/dev/null
9271.106Shaad    lvm vgdisplay -m >"$work_dir/lvm.vg" 2>/dev/null
9281.106Shaad    lvm lvdisplay -m >"$work_dir/lvm.lv" 2>/dev/null
9291.106Shaadfi
9301.106Shaad    ls -1d $work_dir/lvm.* 2>/dev/null |
9311.106Shaad        egrep -v '\.(backup|current)(,v)?$'>> $LVM_LABELS 
9321.106Shaad    CHANGELIST="$CHANGELIST $LVM_LABELS"
9331.106Shaadfi
9341.106Shaad
9351.62Satatat# Check for changes in the list of installed pkgs
9361.62Satatat#
9371.108Sjmmvif checkyesno check_pkgs && have_pkgs; then
9381.67Slukem	pkgs=$work_dir/pkgs
9391.67Slukem	migrate_file "$backup_dir/pkgs" "$pkgs"
9401.112Sagc	pkg_dbdir=$(${pkg_admin} config-var PKG_DBDIR)
9411.108Sjmmv	: ${pkg_dbdir:=/var/db/pkg}
9421.108Sjmmv	(	cd $pkg_dbdir
9431.104Sadrianp		$pkg_info | sort
9441.62Satatat		echo ""
9451.62Satatat		find . \( -name +REQUIRED_BY -o -name +CONTENTS \) -print0 |
9461.72Slukem			xargs -0 ls -ldgTq | sort -t. +1 | sed -e 's, \./, ,'
9471.62Satatat	 ) > $pkgs
9481.67Slukem	echo "$pkgs" > $PKGS
9491.62Satatat	CHANGELIST="$PKGS $CHANGELIST"
9501.32Slukemfi
9511.32Slukem
9521.67Slukem# List of files that get backed up and checked for any modifications.
9531.9Scgd# Any changes cause the files to rotate.
9541.32Slukem#
9551.67Slukemif checkyesno check_changelist ; then
9561.91Slukem	mtree -D -k type -f $SPECIALSPEC -E exclude |
9571.91Slukem	    sed '/^type=file/!d ; s/type=file \.//' | unvis > $CHANGEFILES
9581.67Slukem
9591.75Slukem	(
9601.68Slukem		# Add other files which might dynamically exist:
9611.67Slukem		#	/etc/ifconfig.*
9621.67Slukem		#	/etc/raid*.conf
9631.68Slukem		#	/etc/rc.d/*
9641.67Slukem		#	/etc/rc.conf.d/*
9651.68Slukem		#
9661.75Slukem		echo "/etc/ifconfig.*"
9671.75Slukem		echo "/etc/raid*.conf"
9681.75Slukem		echo "/etc/rc.d/*"
9691.75Slukem		echo "/etc/rc.conf.d/*"
9701.106Shaad		echo "/etc/lvm/backup/*"
9711.106Shaad		echo "/etc/lvm/archive/*"
9721.67Slukem
9731.68Slukem		# Add /etc/changelist
9741.68Slukem		#
9751.75Slukem		if [ -s /etc/changelist ]; then
9761.75Slukem			grep -v '^#' /etc/changelist
9771.75Slukem		fi
9781.75Slukem	) | while read file; do
9791.75Slukem		case "$file" in
9801.75Slukem		*[\*\?\[]*)	# If changelist line is a glob ...
9811.75Slukem				# ... expand possible backup files
9821.75Slukem				#
9831.75Slukem			ls -1d $(echo $backup_dir/${file}.current) 2>/dev/null \
9841.75Slukem			    | sed "s,^$backup_dir/,, ; s,\.current$,,"
9851.75Slukem				
9861.75Slukem				# ... expand possible files
9871.75Slukem				#
9881.75Slukem			ls -1d $(echo $file) 2>/dev/null
9891.75Slukem			;;
9901.75Slukem		*)
9911.75Slukem				# Otherwise, just print the filename
9921.75Slukem			echo $file
9931.75Slukem			;;
9941.75Slukem		esac
9951.75Slukem	done >> $CHANGEFILES
9961.67Slukem	CHANGELIST="$CHANGEFILES $CHANGELIST"
9971.67Slukemfi
9981.67Slukem
9991.67Slukem# Special case backups, including the master password file and
10001.67Slukem# ssh private host keys. The normal backup mechanisms for
10011.67Slukem# $check_changelist (see below) also print out the actual file
10021.67Slukem# differences and we don't want to do that for these files
10031.67Slukem#
10041.67Slukemecho $MP > $TMP1			# always add /etc/master.passwd
10051.91Slukemmtree -D -k type -f $SPECIALSPEC -I nodiff |
10061.91Slukem    sed '/^type=file/!d ; s/type=file \.//' | unvis >> $TMP1
10071.73Slukemgrep -v '^$' $TMP1 | sort -u > $TMP2
10081.68Slukem
10091.69Slukemwhile read file; do
10101.67Slukem	backup_and_diff "$file" no
10111.69Slukemdone < $TMP2
10121.67Slukem
10131.32Slukem
10141.32Slukemif [ -n "$CHANGELIST" ]; then
10151.73Slukem	grep -h -v '^$' $CHANGELIST | sort -u > $TMP1
10161.68Slukem	comm -23 $TMP1 $TMP2 | while read file; do
10171.67Slukem		backup_and_diff "$file" yes
10181.9Scgd	done
10191.44Sadfi
10201.44Sad
10211.108Sjmmvif have_pkgs; then
10221.107Sjmmv	if checkyesno check_pkg_vulnerabilities; then
10231.112Sagc		${pkg_admin} ${_compat_K_flag} audit >${OUTPUT} 2>&1
10241.107Sjmmv		if [ -s ${OUTPUT} ]; then
10251.107Sjmmv			printf "\nInstalled vulnerable packages:\n"
10261.107Sjmmv			cat ${OUTPUT}
10271.107Sjmmv		fi
10281.107Sjmmv	fi
10291.107Sjmmv
10301.107Sjmmv	if checkyesno check_pkg_signatures; then
10311.112Sagc		${pkg_admin} ${_compat_K_flag} check >${OUTPUT} 2>&1
10321.107Sjmmv		if [ $? -ne 0 ]; then
10331.107Sjmmv			printf "\nFiles with invalid signatures:\n"
10341.107Sjmmv			cat ${OUTPUT}
10351.107Sjmmv		fi
10361.107Sjmmv	fi
10371.107Sjmmvfi
10381.107Sjmmv
10391.44Sadif [ -f /etc/security.local ]; then
10401.90Skim	. /etc/security.local > $OUTPUT 2>&1
10411.84Sjhawk	if [ -s $OUTPUT ] ; then
10421.84Sjhawk		printf "\nRunning /etc/security.local:\n"
10431.84Sjhawk		cat $OUTPUT
10441.84Sjhawk	fi
10451.9Scgdfi
1046