security revision 1.115
11.1Scgd#!/bin/sh -
21.1Scgd#
31.114Sspz#	$NetBSD: security,v 1.115 2013/11/06 19:37:05 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.115Sspz	awk -v "usergroups=$permit_usergroups" \
5331.115Sspz            -v "permit_owners_list=$check_homes_permit_other_owner"  '
5341.115Sspz	     BEGIN {
5351.115Sspz		split(permit_owners_list, a);
5361.115Sspz		for (i in a) permit_owners[a[i]]++;
5371.115Sspz	     }
5381.115Sspz	     $1 != $4 && $4 != "root" && !permit_owners[$1] \
5391.15Smrg		{ print "user " $1 " home directory is owned by " $4 }
5401.101Sjnemeth	     $2 ~ /^d....w/ && (!usergroups || $5 != $1) \
5411.80Swiz		{ print "user " $1 " home directory is group writable" }
5421.101Sjnemeth	     $2 ~ /^d.......w/ \
5431.80Swiz		{ print "user " $1 " home directory is other writable" }' \
5441.27Slukem	    > $OUTPUT
5451.15Smrg	if [ -s $OUTPUT ] ; then
5461.15Smrg		printf "\nChecking home directories.\n"
5471.15Smrg		cat $OUTPUT
5481.15Smrg	fi
5491.15Smrg
5501.15Smrg	# Files that should not be owned by someone else or readable.
5511.67Slukem	list=".Xauthority .netrc .ssh/id_dsa .ssh/id_rsa .ssh/identity"
5521.15Smrg	while read uid homedir; do
5531.15Smrg		for f in $list ; do
5541.15Smrg			file=${homedir}/${f}
5551.15Smrg			if [ -f $file ] ; then
5561.109Schristos				printf -- "$uid $f $(ls -ldgT $file)\n"
5571.15Smrg			fi
5581.15Smrg		done
5591.29Slukem	done < $MPBYPATH |
5601.115Sspz	awk -v "usergroups=$permit_usergroups" \
5611.115Sspz            -v "permit_owners_list=$check_homes_permit_other_owner"  '
5621.115Sspz	     BEGIN {
5631.115Sspz		split(permit_owners_list, a);
5641.115Sspz		for (i in a) permit_owners[a[i]]++;
5651.115Sspz	     }
5661.115Sspz	     $1 != $5 && $5 != "root" && !permit_owners[$1] \
5671.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
5681.85Sjhawk	     $3 ~ /^-...r/ && (!usergroups || $6 != $1) \
5691.15Smrg		{ print "user " $1 " " $2 " file is group readable" }
5701.15Smrg	     $3 ~ /^-......r/ \
5711.15Smrg		{ print "user " $1 " " $2 " file is other readable" }
5721.85Sjhawk	     $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
5731.80Swiz		{ print "user " $1 " " $2 " file is group writable" }
5741.15Smrg	     $3 ~ /^-.......w/ \
5751.80Swiz		{ print "user " $1 " " $2 " file is other writable" }' \
5761.27Slukem	    > $OUTPUT
5771.15Smrg
5781.80Swiz	# Files that should not be owned by someone else or writable.
5791.19Smycroft	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
5801.79Selric	      .cshrc .emacs .exrc .forward .history .k5login .klogin .login \
5811.79Selric	      .logout .profile .qmail .rc_history .rhosts .shosts ssh .tcshrc \
5821.79Selric	      .twmrc .xinitrc .xsession .ssh/authorized_keys \
5831.79Selric	      .ssh/authorized_keys2 .ssh/config .ssh/id_dsa.pub \
5841.79Selric	      .ssh/id_rsa.pub .ssh/identity.pub .ssh/known_hosts \
5851.79Selric	      .ssh/known_hosts2"
5861.15Smrg	while read uid homedir; do
5871.15Smrg		for f in $list ; do
5881.15Smrg			file=${homedir}/${f}
5891.15Smrg			if [ -f $file ] ; then
5901.109Schristos				printf -- "$uid $f $(ls -ldgT $file)\n"
5911.15Smrg			fi
5921.15Smrg		done
5931.29Slukem	done < $MPBYPATH |
5941.115Sspz	awk -v "usergroups=$permit_usergroups" \
5951.115Sspz            -v "permit_owners_list=$check_homes_permit_other_owner"  '
5961.115Sspz	     BEGIN {
5971.115Sspz		split(permit_owners_list, a);
5981.115Sspz		for (i in a) permit_owners[a[i]]++;
5991.115Sspz	     }
6001.115Sspz	     $1 != $5 && $5 != "root" && !permit_owners[$1] \
6011.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
6021.85Sjhawk	     $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
6031.80Swiz		{ print "user " $1 " " $2 " file is group writable" }
6041.15Smrg	     $3 ~ /^-.......w/ \
6051.80Swiz		{ print "user " $1 " " $2 " file is other writable" }' \
6061.27Slukem	    >> $OUTPUT
6071.15Smrg	if [ -s $OUTPUT ] ; then
6081.15Smrg		printf "\nChecking dot files.\n"
6091.15Smrg		cat $OUTPUT
6101.15Smrg	fi
6111.9Scgdfi
6121.9Scgd
6131.9Scgd# Mailboxes should be owned by user and unreadable.
6141.32Slukem#
6151.31Slukemif checkyesno check_varmail; then
6161.86Sjhawk	ls -lA /var/mail | \
6171.63Slukem	awk '	NR == 1 { next; }
6181.86Sjhawk		$9 ~ /^\./ {next; }
6191.63Slukem	    	$3 != $9 {
6201.63Slukem			print "user " $9 " mailbox is owned by " $3
6211.63Slukem		}
6221.63Slukem		$1 != "-rw-------" {
6231.63Slukem			print "user " $9 " mailbox is " $1 ", group " $4
6241.63Slukem		}' > $OUTPUT
6251.15Smrg	if [ -s $OUTPUT ] ; then
6261.15Smrg		printf "\nChecking mailbox ownership.\n"
6271.15Smrg		cat $OUTPUT
6281.15Smrg	fi
6291.15Smrgfi
6301.15Smrg
6311.32Slukem# NFS exports shouldn't be globally exported
6321.32Slukem#
6331.32Slukemif checkyesno check_nfs && [ -f /etc/exports ]; then
6341.32Slukem	awk '{
6351.22Slukem		# ignore comments and blank lines
6361.39Shubertf		if ($0 ~ /^\#/ || $0 ~ /^$/ )
6371.22Slukem			next;
6381.100Stron		# manage line continuation
6391.100Stron		while ($NF ~ /^\\$/) {
6401.100Stron			$NF = "";
6411.100Stron			line = $0 "";
6421.100Stron			getline;
6431.100Stron			$0 = line $0 "";
6441.100Stron		}
6451.22Slukem
6461.100Stron		delete dir;
6471.100Stron		readonly = ndir = 0;
6481.100Stron		for (i = 1; i <= NF; ++i) {
6491.100Stron			if ($i ~ /^\//) dir[ndir++] = $i;
6501.100Stron			else if ($i ~ /^-/) {
6511.100Stron				if ($i ~ /^-(ro|o)$/) readonly = 1;
6521.100Stron				if ($i ~ /^-network/) next;
6531.100Stron			}
6541.100Stron			else next;
6551.15Smrg		}
6561.15Smrg		if (readonly)
6571.100Stron			for (item in dir)
6581.100Stron				rodir[nrodir++] = dir[item];
6591.15Smrg		else
6601.100Stron			for (item in dir)
6611.100Stron				rwdir[nrwdir++] = dir[item];
6621.100Stron
6631.100Stron	}
6641.100Stron
6651.100Stron	END {
6661.100Stron		if (nrodir) {
6671.100Stron			printf("Globally exported file system%s, read-only:\n",
6681.100Stron				nrodir > 1 ? "s" : "");
6691.100Stron			for (item in rodir)
6701.100Stron				printf("\t%s\n", rodir[item]);
6711.100Stron		}
6721.100Stron		if (nrwdir) {
6731.100Stron			printf("Globally exported file system%s, read-write:\n",
6741.100Stron				nrwdir > 1 ? "s" : "");
6751.100Stron			for (item in rwdir)
6761.100Stron				printf("\t%s\n", rwdir[item]);
6771.100Stron		}
6781.32Slukem	}' < /etc/exports > $OUTPUT
6791.32Slukem	if [ -s $OUTPUT ] ; then
6801.15Smrg		printf "\nChecking for globally exported file systems.\n"
6811.15Smrg		cat $OUTPUT
6821.15Smrg	fi
6831.9Scgdfi
6841.9Scgd
6851.9Scgd# Display any changes in setuid files and devices.
6861.32Slukem#
6871.31Slukemif checkyesno check_devices; then
6881.28Slukem	> $ERR
6891.92Serh	(
6901.98Slukem
6911.98Slukem	# Convert check_devices_ignore_fstypes="foo !bar bax"
6921.98Slukem	#    into "-fstype foo -o ! -fstype bar -o -fstype bax"
6931.98Slukem	# and check_devices_ignore_paths="/foo !/bar /bax"
6941.98Slukem	#    into " -path /foo -o ! -path /bar -o -path /bax"
6951.98Slukem	#
6961.98Slukem	ignexpr=$(\
6971.98Slukem	    echo $check_devices_ignore_fstypes | \
6981.98Slukem		sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -fstype \2/g' ; \
6991.98Slukem	    echo $check_devices_ignore_paths | \
7001.98Slukem		sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -path \2/g' \
7011.98Slukem	)
7021.98Slukem
7031.98Slukem	# Massage the expression into ( $ignexpr ) -a -prune -o
7041.98Slukem	if [ -n "${ignexpr}" ]; then
7051.98Slukem		ignexpr=$(\
7061.98Slukem			echo $ignexpr | \
7071.98Slukem			    sed -e 's/^-o /( /' \
7081.98Slukem				-e 's/$/ ) -a -prune -o/' \
7091.98Slukem		)
7101.98Slukem	fi
7111.98Slukem
7121.98Slukem	find / $ignexpr \
7131.21Smycroft	    \( \( -perm -u+s -a ! -type d \) -o \
7141.21Smycroft	       \( -perm -g+s -a ! -type d \) -o \
7151.24Slukem	       -type b -o -type c \) -print0 | \
7161.98Slukem	xargs -0 ls -ldgTq | sort +9 > $LIST
7171.98Slukem
7181.98Slukem	) 2> $OUTPUT
7191.15Smrg
7201.15Smrg	# Display any errors that occurred during system file walk.
7211.15Smrg	if [ -s $OUTPUT ] ; then
7221.28Slukem		printf "Setuid/device find errors:\n" >> $ERR
7231.28Slukem		cat $OUTPUT >> $ERR
7241.28Slukem		printf "\n" >> $ERR
7251.15Smrg	fi
7261.15Smrg
7271.15Smrg	# Display any changes in the setuid file list.
7281.15Smrg	egrep -v '^[bc]' $LIST > $TMP1
7291.15Smrg	if [ -s $TMP1 ] ; then
7301.15Smrg		# Check to make sure uudecode isn't setuid.
7311.15Smrg		if grep -w uudecode $TMP1 > /dev/null ; then
7321.28Slukem			printf "\nUudecode is setuid.\n" >> $ERR
7331.15Smrg		fi
7341.15Smrg
7351.67Slukem		file=$work_dir/setuid
7361.67Slukem		migrate_file "$backup_dir/setuid" "$file"
7371.67Slukem		CUR=${file}.current
7381.67Slukem		BACK=${file}.backup
7391.15Smrg		if [ -s $CUR ] ; then
7401.15Smrg			if cmp -s $CUR $TMP1 ; then
7411.15Smrg				:
7421.15Smrg			else
7431.15Smrg				> $TMP2
7441.15Smrg				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
7451.15Smrg				if [ -s $OUTPUT ] ; then
7461.28Slukem					printf "Setuid additions:\n" >> $ERR
7471.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
7481.28Slukem					printf "\n" >> $ERR
7491.15Smrg				fi
7501.15Smrg
7511.15Smrg				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
7521.15Smrg				if [ -s $OUTPUT ] ; then
7531.28Slukem					printf "Setuid deletions:\n" >> $ERR
7541.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
7551.28Slukem					printf "\n" >> $ERR
7561.15Smrg				fi
7571.15Smrg
7581.20Smycroft				sort -k10 $TMP2 $CUR $TMP1 | \
7591.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
7601.27Slukem				    uniq -u > $OUTPUT
7611.15Smrg				if [ -s $OUTPUT ] ; then
7621.28Slukem					printf "Setuid changes:\n" >> $ERR
7631.28Slukem					column -t $OUTPUT >> $ERR
7641.28Slukem					printf "\n" >> $ERR
7651.15Smrg				fi
7661.9Scgd
7671.52Satatat				backup_file update $TMP1 $CUR $BACK
7681.9Scgd			fi
7691.15Smrg		else
7701.28Slukem			printf "Setuid additions:\n" >> $ERR
7711.28Slukem			column -t $TMP1 >> $ERR
7721.28Slukem			printf "\n" >> $ERR
7731.52Satatat			backup_file add $TMP1 $CUR $BACK
7741.9Scgd		fi
7751.15Smrg	fi
7761.15Smrg
7771.27Slukem	# Check for block and character disk devices that are readable or
7781.80Swiz	# writable or not owned by root.operator.
7791.15Smrg	>$TMP1
7801.61Slukem	DISKLIST="ccd ch hk hp ld md ra raid rb rd rl rx \
7811.57Ssimonb	    sd se ss uk up vnd wd xd xy"
7821.27Slukem#	DISKLIST="$DISKLIST ct mt st wt"
7831.15Smrg	for i in $DISKLIST; do
7841.15Smrg		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
7851.15Smrg		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
7861.15Smrg	done
7871.15Smrg
7881.15Smrg	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
7891.25Slukem		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
7901.25Slukem		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
7911.15Smrg	if [ -s $OUTPUT ] ; then
7921.28Slukem		printf "\nChecking disk ownership and permissions.\n" >> $ERR
7931.28Slukem		cat $OUTPUT >> $ERR
7941.28Slukem		printf "\n" >> $ERR
7951.9Scgd	fi
7961.9Scgd
7971.15Smrg	# Display any changes in the device file list.
7981.20Smycroft	egrep '^[bc]' $LIST | sort -k11 > $TMP1
7991.15Smrg	if [ -s $TMP1 ] ; then
8001.67Slukem		file=$work_dir/device
8011.67Slukem		migrate_file "$backup_dir/device" "$file"
8021.67Slukem		CUR=${file}.current
8031.67Slukem		BACK=${file}.backup
8041.15Smrg
8051.15Smrg		if [ -s $CUR ] ; then
8061.15Smrg			if cmp -s $CUR $TMP1 ; then
8071.15Smrg				:
8081.15Smrg			else
8091.15Smrg				> $TMP2
8101.15Smrg				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
8111.15Smrg				if [ -s $OUTPUT ] ; then
8121.28Slukem					printf "Device additions:\n" >> $ERR
8131.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
8141.28Slukem					printf "\n" >> $ERR
8151.15Smrg				fi
8161.15Smrg
8171.15Smrg				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
8181.15Smrg				if [ -s $OUTPUT ] ; then
8191.28Slukem					printf "Device deletions:\n" >> $ERR
8201.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
8211.28Slukem					printf "\n" >> $ERR
8221.15Smrg				fi
8231.15Smrg
8241.27Slukem				# Report any block device change. Ignore
8251.27Slukem				# character devices, only the name is
8261.27Slukem				# significant.
8271.15Smrg				cat $TMP2 $CUR $TMP1 | \
8281.27Slukem				    sed -e '/^c/d' | \
8291.27Slukem				    sort -k11 | \
8301.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
8311.27Slukem				    uniq -u > $OUTPUT
8321.15Smrg				if [ -s $OUTPUT ] ; then
8331.28Slukem					printf "Block device changes:\n" >> $ERR
8341.28Slukem					column -t $OUTPUT >> $ERR
8351.28Slukem					printf "\n" >> $ERR
8361.15Smrg				fi
8371.9Scgd
8381.52Satatat				backup_file update $TMP1 $CUR $BACK
8391.9Scgd			fi
8401.15Smrg		else
8411.28Slukem			printf "Device additions:\n" >> $ERR
8421.28Slukem			column -t $TMP1 >> $ERR
8431.28Slukem			printf "\n" >> $ERR
8441.52Satatat			backup_file add $TMP1 $CUR $BACK >> $ERR
8451.9Scgd		fi
8461.28Slukem	fi
8471.28Slukem	if [ -s $ERR ] ; then
8481.28Slukem		printf "\nChecking setuid files and devices:\n"
8491.28Slukem		cat $ERR
8501.28Slukem		printf "\n"
8511.9Scgd	fi
8521.9Scgdfi
8531.9Scgd
8541.9Scgd# Check special files.
8551.9Scgd# Check system binaries.
8561.9Scgd#
8571.9Scgd# Create the mtree tree specifications using:
8581.67Slukem#	mtree -cx -pDIR -kmd5,uid,gid,mode,nlink,size,link,time > DIR.secure
8591.38Skleink#	chown root:wheel DIR.secure
8601.67Slukem#	chmod u+r,go= DIR.secure
8611.9Scgd#
8621.9Scgd# Note, this is not complete protection against Trojan horsed binaries, as
8631.9Scgd# the hacker can modify the tree specification to match the replaced binary.
8641.9Scgd# For details on really protecting yourself against modified binaries, see
8651.9Scgd# the mtree(8) manual page.
8661.32Slukem#
8671.31Slukemif checkyesno check_mtree; then
8681.82Sjhawk	if checkyesno check_mtree_follow_symlinks; then
8691.82Sjhawk		check_mtree_flags="-L"
8701.82Sjhawk	else
8711.82Sjhawk		check_mtree_flags=""
8721.82Sjhawk	fi
8731.91Slukem	mtree -e -l -p / $check_mtree_flags -f $SPECIALSPEC 3>&1 >$OUTPUT 2>&3 |
8741.87Sjhawk		grep -v '^mtree: dev/tty: Device not configured$' >&2
8751.15Smrg	if [ -s $OUTPUT ]; then
8761.9Scgd		printf "\nChecking special files and directories.\n"
8771.9Scgd		cat $OUTPUT
8781.9Scgd	fi
8791.9Scgd
8801.16Smikel	for file in /etc/mtree/*.secure; do
8811.16Smikel		[ $file = '/etc/mtree/*.secure' ] && continue
8821.109Schristos		tree=$(sed -n -e '3s/.* //p' -e 3q $file)
8831.82Sjhawk		mtree $check_mtree_flags -f $file -p $tree > $TMP1
8841.9Scgd		if [ -s $TMP1 ]; then
8851.67Slukem			printf "\nChecking $tree:\n"
8861.67Slukem			cat $TMP1
8871.9Scgd		fi
8881.67Slukem	done > $OUTPUT
8891.15Smrg	if [ -s $OUTPUT ]; then
8901.9Scgd		printf "\nChecking system binaries:\n"
8911.9Scgd		cat $OUTPUT
8921.9Scgd	fi
8931.9Scgdfi
8941.9Scgd
8951.32Slukem# Backup disklabels of available disks
8961.32Slukem#
8971.32Slukemif checkyesno check_disklabels; then
8981.67Slukem		# migrate old disklabels
8991.109Schristos	for file in $(ls -1d $backup_dir/$backup_dir/disklabel.* \
9001.109Schristos	    $backup_dir/disklabel.* 2>/dev/null); do
9011.67Slukem		migrate_file "$file" "$work_dir/${file##*/}"
9021.67Slukem	done
9031.67Slukem
9041.103Stron		# generate list of old disklabels, fdisks & wedges and remove them
9051.103Stron	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* $work_dir/wedges.* 2>/dev/null |
9061.52Satatat	    egrep -v '\.(backup|current)(,v)?$' > $LABELS
9071.32Slukem	xargs rm < $LABELS
9081.32Slukem
9091.103Stron		# generate disklabels of all disks excluding:	cd dk fd md st
9101.109Schristos	disks=$(iostat -x | awk 'NR > 1 && $1 !~ /^[cfm]d|dk|st|nfs/ { print $1; }')
9111.32Slukem	for i in $disks; do
9121.67Slukem		disklabel $i > "$work_dir/disklabel.$i" 2>/dev/null
9131.32Slukem	done
9141.32Slukem
9151.67Slukem		# if fdisk is available, generate fdisks for:	ed ld sd wd
9161.67Slukem	if [ -x /sbin/fdisk ]; then
9171.109Schristos		disks=$(iostat -x | awk 'NR > 1 && $1 ~ /^[elsw]d/ { print $1; }')
9181.67Slukem		for i in $disks; do
9191.67Slukem			/sbin/fdisk $i > "$work_dir/fdisk.$i" 2>/dev/null
9201.67Slukem		done
9211.67Slukem	fi
9221.67Slukem
9231.103Stron		# if dkctl is available, generate dkctl listwedges for:	ed ld sd wd cgd ofdisk ra rl raid
9241.103Stron	if [ -x /sbin/dkctl ]; then
9251.109Schristos		disks=$(iostat -x | awk 'NR > 1 && $1 ~ /^[elsw]d|cgd|ofdisk|r[al]|raid/ { print $1; }')
9261.103Stron		for i in $disks; do
9271.103Stron			/sbin/dkctl $i listwedges > "$work_dir/wedges.$i" 2>/dev/null
9281.103Stron		done
9291.103Stron	fi
9301.103Stron
9311.103Stron		# append list of new disklabels, fdisks and wedges
9321.103Stron	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* $work_dir/wedges.* 2>/dev/null |
9331.52Satatat	    egrep -v '\.(backup|current)(,v)?$' >> $LABELS
9341.62Satatat	CHANGELIST="$LABELS $CHANGELIST"
9351.62Satatatfi
9361.62Satatat
9371.106Shaadif checkyesno check_lvm; then
9381.106Shaad    
9391.106Shaad    # generate list of existing LVM elements Physical Volumes, Volume Groups and Logical Volumes.
9401.106Shaadif [ -x /sbin/lvm ]; then
9411.106Shaad    lvm pvdisplay -m >"$work_dir/lvm.pv" 2>/dev/null
9421.106Shaad    lvm vgdisplay -m >"$work_dir/lvm.vg" 2>/dev/null
9431.106Shaad    lvm lvdisplay -m >"$work_dir/lvm.lv" 2>/dev/null
9441.106Shaadfi
9451.106Shaad    ls -1d $work_dir/lvm.* 2>/dev/null |
9461.106Shaad        egrep -v '\.(backup|current)(,v)?$'>> $LVM_LABELS 
9471.106Shaad    CHANGELIST="$CHANGELIST $LVM_LABELS"
9481.106Shaadfi
9491.106Shaad
9501.62Satatat# Check for changes in the list of installed pkgs
9511.62Satatat#
9521.108Sjmmvif checkyesno check_pkgs && have_pkgs; then
9531.67Slukem	pkgs=$work_dir/pkgs
9541.67Slukem	migrate_file "$backup_dir/pkgs" "$pkgs"
9551.112Sagc	pkg_dbdir=$(${pkg_admin} config-var PKG_DBDIR)
9561.108Sjmmv	: ${pkg_dbdir:=/var/db/pkg}
9571.108Sjmmv	(	cd $pkg_dbdir
9581.104Sadrianp		$pkg_info | sort
9591.62Satatat		echo ""
9601.62Satatat		find . \( -name +REQUIRED_BY -o -name +CONTENTS \) -print0 |
9611.72Slukem			xargs -0 ls -ldgTq | sort -t. +1 | sed -e 's, \./, ,'
9621.62Satatat	 ) > $pkgs
9631.67Slukem	echo "$pkgs" > $PKGS
9641.62Satatat	CHANGELIST="$PKGS $CHANGELIST"
9651.32Slukemfi
9661.32Slukem
9671.67Slukem# List of files that get backed up and checked for any modifications.
9681.9Scgd# Any changes cause the files to rotate.
9691.32Slukem#
9701.67Slukemif checkyesno check_changelist ; then
9711.91Slukem	mtree -D -k type -f $SPECIALSPEC -E exclude |
9721.91Slukem	    sed '/^type=file/!d ; s/type=file \.//' | unvis > $CHANGEFILES
9731.67Slukem
9741.75Slukem	(
9751.68Slukem		# Add other files which might dynamically exist:
9761.67Slukem		#	/etc/ifconfig.*
9771.67Slukem		#	/etc/raid*.conf
9781.68Slukem		#	/etc/rc.d/*
9791.67Slukem		#	/etc/rc.conf.d/*
9801.68Slukem		#
9811.75Slukem		echo "/etc/ifconfig.*"
9821.75Slukem		echo "/etc/raid*.conf"
9831.75Slukem		echo "/etc/rc.d/*"
9841.75Slukem		echo "/etc/rc.conf.d/*"
9851.106Shaad		echo "/etc/lvm/backup/*"
9861.106Shaad		echo "/etc/lvm/archive/*"
9871.67Slukem
9881.68Slukem		# Add /etc/changelist
9891.68Slukem		#
9901.75Slukem		if [ -s /etc/changelist ]; then
9911.75Slukem			grep -v '^#' /etc/changelist
9921.75Slukem		fi
9931.75Slukem	) | while read file; do
9941.75Slukem		case "$file" in
9951.75Slukem		*[\*\?\[]*)	# If changelist line is a glob ...
9961.75Slukem				# ... expand possible backup files
9971.75Slukem				#
9981.75Slukem			ls -1d $(echo $backup_dir/${file}.current) 2>/dev/null \
9991.75Slukem			    | sed "s,^$backup_dir/,, ; s,\.current$,,"
10001.75Slukem				
10011.75Slukem				# ... expand possible files
10021.75Slukem				#
10031.75Slukem			ls -1d $(echo $file) 2>/dev/null
10041.75Slukem			;;
10051.75Slukem		*)
10061.75Slukem				# Otherwise, just print the filename
10071.75Slukem			echo $file
10081.75Slukem			;;
10091.75Slukem		esac
10101.75Slukem	done >> $CHANGEFILES
10111.67Slukem	CHANGELIST="$CHANGEFILES $CHANGELIST"
10121.67Slukemfi
10131.67Slukem
10141.67Slukem# Special case backups, including the master password file and
10151.67Slukem# ssh private host keys. The normal backup mechanisms for
10161.67Slukem# $check_changelist (see below) also print out the actual file
10171.67Slukem# differences and we don't want to do that for these files
10181.67Slukem#
10191.67Slukemecho $MP > $TMP1			# always add /etc/master.passwd
10201.91Slukemmtree -D -k type -f $SPECIALSPEC -I nodiff |
10211.91Slukem    sed '/^type=file/!d ; s/type=file \.//' | unvis >> $TMP1
10221.73Slukemgrep -v '^$' $TMP1 | sort -u > $TMP2
10231.68Slukem
10241.69Slukemwhile read file; do
10251.67Slukem	backup_and_diff "$file" no
10261.69Slukemdone < $TMP2
10271.67Slukem
10281.32Slukem
10291.32Slukemif [ -n "$CHANGELIST" ]; then
10301.73Slukem	grep -h -v '^$' $CHANGELIST | sort -u > $TMP1
10311.68Slukem	comm -23 $TMP1 $TMP2 | while read file; do
10321.67Slukem		backup_and_diff "$file" yes
10331.9Scgd	done
10341.44Sadfi
10351.44Sad
10361.108Sjmmvif have_pkgs; then
10371.107Sjmmv	if checkyesno check_pkg_vulnerabilities; then
10381.112Sagc		${pkg_admin} ${_compat_K_flag} audit >${OUTPUT} 2>&1
10391.107Sjmmv		if [ -s ${OUTPUT} ]; then
10401.107Sjmmv			printf "\nInstalled vulnerable packages:\n"
10411.107Sjmmv			cat ${OUTPUT}
10421.107Sjmmv		fi
10431.107Sjmmv	fi
10441.107Sjmmv
10451.107Sjmmv	if checkyesno check_pkg_signatures; then
10461.112Sagc		${pkg_admin} ${_compat_K_flag} check >${OUTPUT} 2>&1
10471.107Sjmmv		if [ $? -ne 0 ]; then
10481.107Sjmmv			printf "\nFiles with invalid signatures:\n"
10491.107Sjmmv			cat ${OUTPUT}
10501.107Sjmmv		fi
10511.107Sjmmv	fi
10521.107Sjmmvfi
10531.107Sjmmv
10541.44Sadif [ -f /etc/security.local ]; then
10551.90Skim	. /etc/security.local > $OUTPUT 2>&1
10561.84Sjhawk	if [ -s $OUTPUT ] ; then
10571.84Sjhawk		printf "\nRunning /etc/security.local:\n"
10581.84Sjhawk		cat $OUTPUT
10591.84Sjhawk	fi
10601.9Scgdfi
1061