security revision 1.73
11.1Scgd#!/bin/sh -
21.1Scgd#
31.73Slukem#	$NetBSD: security,v 1.73 2001/11/09 09:01:20 lukem 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.31Slukemif [ -f /etc/rc.subr ]; then
101.31Slukem	. /etc/rc.subr
111.31Slukemelse
121.31Slukem	echo "Can't read /etc/rc.subr; aborting."
131.31Slukem	exit 1;
141.31Slukemfi
151.31Slukem
161.9Scgdumask 077
171.64ScjsTZ=UTC; export TZ
181.1Scgd
191.15Smrgif [ -s /etc/security.conf ]; then
201.15Smrg	. /etc/security.conf
211.15Smrgfi
221.15Smrg
231.67Slukem# Set reasonable defaults (if they're not set in security.conf)
241.67Slukem#
251.67Slukembackup_dir=${backup_dir:-/var/backups}
261.67Slukempkgdb_dir=${pkgdb_dir:-/var/db/pkg}
271.67Slukemmax_loginlen=${max_loginlen:-8}
281.67Slukemmax_grouplen=${max_grouplen:-8}
291.67Slukem
301.67Slukem# Other configurable variables
311.67Slukem#
321.67Slukemspecial_files="/etc/mtree/special /etc/mtree/special.local"
331.67SlukemMP=/etc/master.passwd
341.67SlukemCHANGELIST=""
351.67Slukemwork_dir=$backup_dir/work
361.67Slukem
371.67Slukemif [ ! -d "$work_dir" ]; then
381.67Slukem	mkdir -p "$work_dir"
391.67Slukemfi
401.67Slukem
411.56SlukemSECUREDIR=`mktemp -d /tmp/_securedir.XXXXXX` || exit 1
421.56Slukem
431.67Slukemtrap "/bin/rm -rf $SECUREDIR ; exit 0" EXIT INT QUIT PIPE
441.15Smrg
451.56Slukemif ! cd "$SECUREDIR"; then
461.56Slukem	echo "Can not cd to $SECUREDIR".
471.15Smrg	exit 1
481.15Smrgfi
491.15Smrg
501.15SmrgERR=secure1.$$
511.15SmrgTMP1=secure2.$$
521.15SmrgTMP2=secure3.$$
531.28SlukemMPBYUID=secure4.$$
541.29SlukemMPBYPATH=secure5.$$
551.27SlukemLIST=secure6.$$
561.27SlukemOUTPUT=secure7.$$
571.32SlukemLABELS=secure8.$$
581.62SatatatPKGS=secure9.$$
591.67SlukemCHANGEFILES=secure10.$$
601.67Slukem
611.15Smrg
621.67Slukem# migrate_file old new
631.67Slukem#	Determine if the "${old}" path name needs to be migrated to the
641.67Slukem#	"${new}" path. Also checks if "${old}.current" needs migrating,
651.67Slukem#	and if so, migrate it and possibly "${old}.current,v" and
661.67Slukem#	"${old}.backup".
671.67Slukem#
681.67Slukemmigrate_file()
691.67Slukem{
701.67Slukem	_old=$1
711.67Slukem	_new=$2
721.67Slukem	if [ -z "$_old" -o -z "$_new" ]; then
731.67Slukem		err 3 "USAGE: migrate_file old new"
741.67Slukem	fi
751.67Slukem	if [ ! -d "${_new%/*}" ]; then
761.67Slukem		mkdir -p "${_new%/*}"
771.67Slukem	fi
781.67Slukem	if [ -f "${_old}" -a ! -f "${_new}" ]; then
791.67Slukem		echo "==> migrating ${_old}"
801.67Slukem		echo "           to ${_new}"
811.67Slukem		mv "${_old}" "${_new}"
821.67Slukem	fi
831.67Slukem	if [ -f "${_old}.current" -a ! -f "${_new}.current" ]; then
841.67Slukem		echo "==> migrating ${_old}.current"
851.67Slukem		echo "           to ${_new}.current"
861.67Slukem		mv "${_old}.current" "${_new}.current"
871.67Slukem		if [ -f "${_old}.current,v" -a ! -f "${_new}.current,v" ]; then
881.67Slukem			echo "==> migrating ${_old}.current,v"
891.67Slukem			echo "           to ${_new}.current,v"
901.67Slukem			mv "${_old}.current,v" "${_new}.current,v"
911.67Slukem		fi
921.67Slukem		if [ -f "${_old}.backup" -a ! -f "${_new}.backup" ]; then
931.67Slukem			echo "==> migrating ${_old}.backup"
941.67Slukem			echo "           to ${_new}.backup"
951.67Slukem			mv "${_old}.backup" "${_new}.backup"
961.67Slukem		fi
971.67Slukem	fi
981.67Slukem}
991.67Slukem
1001.67Slukem
1011.67Slukem# backup_and_diff file printdiff
1021.67Slukem#	Determine if file needs backing up, and if so, do it.
1031.67Slukem#	If printdiff is yes, display the diffs, otherwise 
1041.67Slukem#	just print a message saying "[changes omitted]".
1051.67Slukem#
1061.67Slukembackup_and_diff()
1071.67Slukem{
1081.67Slukem	_file=$1
1091.67Slukem	_printdiff=$2
1101.67Slukem	if [ -z "$_file" -o -z "$_printdiff" ]; then
1111.67Slukem		err 3 "USAGE: backup_and_diff file printdiff"
1121.67Slukem	fi
1131.67Slukem	! checkyesno _printdiff
1141.67Slukem	_printdiff=$?
1151.67Slukem
1161.67Slukem	_old=$backup_dir/${_file##*/}
1171.67Slukem	case "$_file" in
1181.67Slukem	$work_dir/*)
1191.67Slukem		_new=$_file
1201.67Slukem		migrate_file "$backup_dir/$_old" "$_new"
1211.67Slukem		migrate_file "$_old" "$_new"
1221.67Slukem		;;
1231.67Slukem	*)
1241.67Slukem		_new=$backup_dir/$_file
1251.67Slukem		migrate_file "$_old" "$_new"
1261.67Slukem		;;
1271.67Slukem	esac
1281.67Slukem	CUR=${_new}.current
1291.67Slukem	BACK=${_new}.backup
1301.67Slukem	if [ -f $_file ]; then
1311.67Slukem		if [ -f $CUR ] ; then
1321.67Slukem			if [ "$_printdiff" -ne 0 ]; then
1331.67Slukem				diff $CUR $_file > $OUTPUT
1341.67Slukem			else
1351.67Slukem				if ! cmp -s $CUR $_file; then
1361.67Slukem					echo "[changes omitted]"
1371.67Slukem				fi > $OUTPUT
1381.67Slukem			fi
1391.67Slukem			if [ -s $OUTPUT ] ; then
1401.67Slukem				printf \
1411.67Slukem			"\n======\n%s diffs (OLD < > NEW)\n======\n" $_file
1421.67Slukem				cat $OUTPUT
1431.67Slukem				backup_file update $_file $CUR $BACK
1441.67Slukem			fi
1451.67Slukem		else
1461.67Slukem			printf "\n======\n%s added\n======\n" $_file
1471.67Slukem			if [ "$_printdiff" -ne 0 ]; then
1481.67Slukem				diff /dev/null $_file
1491.67Slukem			else
1501.67Slukem				echo "[changes omitted]"
1511.67Slukem			fi
1521.67Slukem			backup_file add $_file $CUR $BACK
1531.67Slukem		fi
1541.67Slukem	else
1551.67Slukem		if [ -f $CUR ]; then
1561.67Slukem			printf "\n======\n%s removed\n======\n" $_file
1571.67Slukem			if [ "$_printdiff" -ne 0 ]; then
1581.67Slukem				diff $CUR /dev/null
1591.67Slukem			else
1601.67Slukem				echo "[changes omitted]"
1611.67Slukem			fi
1621.67Slukem			backup_file remove $_file $CUR $BACK
1631.67Slukem		fi
1641.67Slukem	fi
1651.67Slukem}
1661.48Sabs
1671.9Scgd
1681.67Slukem# These are used several times.
1691.67Slukem#
1701.34Sabsawk -F: '!/^+/ { print $1 " " $3 }' $MP | sort -k2n > $MPBYUID
1711.29Slukemawk -F: '{ print $1 " " $9 }' $MP | sort -k2 > $MPBYPATH
1721.9Scgd
1731.67Slukem
1741.9Scgd# Check the master password file syntax.
1751.32Slukem#
1761.31Slukemif checkyesno check_passwd; then
1771.34Sabs	awk -v "len=$max_loginlen" '
1781.25Slukem	BEGIN {
1791.25Slukem		while ( getline < "/etc/shells" > 0 ) {
1801.39Shubertf			if ($0 ~ /^\#/ || $0 ~ /^$/ )
1811.25Slukem				continue;
1821.25Slukem			shells[$1]++;
1831.25Slukem		}
1841.25Slukem		FS=":";
1851.25Slukem	}
1861.25Slukem
1871.25Slukem	{
1881.15Smrg		if ($0 ~ /^[	 ]*$/) {
1891.25Slukem			printf "Line %d is a blank line.\n", NR;
1901.15Smrg			next;
1911.15Smrg		}
1921.34Sabs		if (NF != 10 && ($1 != "+" || NF != 1))
1931.25Slukem			printf "Line %d has the wrong number of fields.\n", NR;
1941.34Sabs		if ($1 == "+" )  {
1951.34Sabs			if (NF != 1 && $3 == 0)
1961.34Sabs			    printf "Line %d includes entries with uid 0.\n", NR;
1971.34Sabs			next;
1981.34Sabs		}
1991.53Satatat		if ($1 !~ /^[A-Za-z0-9]([-A-Za-z0-9]*[A-Za-z0-9])*$/)
2001.25Slukem			printf "Login %s has non-alphanumeric characters.\n",
2011.25Slukem			    $1;
2021.34Sabs		if (length($1) > len)
2031.34Sabs			printf "Login %s has more than "len" characters.\n", $1;
2041.15Smrg		if ($2 == "")
2051.25Slukem			printf "Login %s has no password.\n", $1;
2061.66Slukem		if (length($2) != 13 &&
2071.66Slukem		    length($2) != 20 &&
2081.59Sperry		    length($2) != 34 &&
2091.59Sperry		    $2 != "" &&
2101.59Sperry		    $2 !~ /^\*[A-z-]+$/ &&
2111.59Sperry		    $1 != "toor") {
2121.25Slukem			if ($10 == "" || shells[$10])
2131.27Slukem		    printf "Login %s is off but still has a valid shell (%s)\n",
2141.25Slukem				    $1, $10;
2151.25Slukem		} else if (! shells[$10])
2161.25Slukem			printf "Login %s does not have a valid shell (%s)\n",
2171.25Slukem			    $1, $10;
2181.15Smrg		if ($3 == 0 && $1 != "root" && $1 != "toor")
2191.25Slukem			printf "Login %s has a user id of 0.\n", $1;
2201.15Smrg		if ($3 < 0)
2211.25Slukem			printf "Login %s has a negative user id.\n", $1;
2221.15Smrg		if ($4 < 0)
2231.25Slukem			printf "Login %s has a negative group id.\n", $1;
2241.15Smrg	}' < $MP > $OUTPUT
2251.15Smrg	if [ -s $OUTPUT ] ; then
2261.15Smrg		printf "\nChecking the $MP file:\n"
2271.15Smrg		cat $OUTPUT
2281.15Smrg	fi
2291.15Smrg
2301.15Smrg	awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
2311.15Smrg	if [ -s $OUTPUT ] ; then
2321.15Smrg		printf "\n$MP has duplicate user names.\n"
2331.15Smrg		column $OUTPUT
2341.15Smrg	fi
2351.15Smrg
2361.37Swrstuden# To not exclude 'toor', a standard duplicate root account, from the duplicate
2371.37Swrstuden# account test, uncomment the line below (without egrep in it)and comment
2381.37Swrstuden# out the line (with egrep in it) below it.
2391.37Swrstuden#
2401.37Swrstuden#	< $MPBYUID uniq -d -f 1 | awk '{ print $2 }' > $TMP2
2411.36Swrstuden	< $MPBYUID egrep -v '^toor ' | uniq -d -f 1 | awk '{ print $2 }' > $TMP2
2421.15Smrg	if [ -s $TMP2 ] ; then
2431.15Smrg		printf "\n$MP has duplicate user id's.\n"
2441.15Smrg		while read uid; do
2451.28Slukem			grep -w $uid $MPBYUID
2461.15Smrg		done < $TMP2 | column
2471.15Smrg	fi
2481.9Scgdfi
2491.9Scgd
2501.9Scgd# Check the group file syntax.
2511.32Slukem#
2521.31Slukemif checkyesno check_group; then
2531.15Smrg	GRP=/etc/group
2541.49Sjdolecek	awk -F: -v "len=$max_grouplen" '{
2551.15Smrg		if ($0 ~ /^[	 ]*$/) {
2561.25Slukem			printf "Line %d is a blank line.\n", NR;
2571.15Smrg			next;
2581.15Smrg		}
2591.34Sabs		if (NF != 4 && ($1 != "+" || NF != 1))
2601.25Slukem			printf "Line %d has the wrong number of fields.\n", NR;
2611.34Sabs		if ($1 == "+" )  {
2621.34Sabs			next;
2631.34Sabs		}
2641.53Satatat		if ($1 !~ /^[A-Za-z0-9]([-A-Za-z0-9]*[A-Za-z0-9])*$/)
2651.25Slukem			printf "Group %s has non-alphanumeric characters.\n",
2661.25Slukem			    $1;
2671.49Sjdolecek		if (length($1) > len)
2681.49Sjdolecek			printf "Group %s has more than "len" characters.\n", $1;
2691.15Smrg		if ($3 !~ /[0-9]*/)
2701.25Slukem			printf "Login %s has a negative group id.\n", $1;
2711.15Smrg	}' < $GRP > $OUTPUT
2721.15Smrg	if [ -s $OUTPUT ] ; then
2731.15Smrg		printf "\nChecking the $GRP file:\n"
2741.15Smrg		cat $OUTPUT
2751.15Smrg	fi
2761.15Smrg
2771.15Smrg	awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
2781.15Smrg	if [ -s $OUTPUT ] ; then
2791.15Smrg		printf "\n$GRP has duplicate group names.\n"
2801.15Smrg		column $OUTPUT
2811.15Smrg	fi
2821.9Scgdfi
2831.9Scgd
2841.9Scgd# Check for root paths, umask values in startup files.
2851.9Scgd# The check for the root paths is problematical -- it's likely to fail
2861.9Scgd# in other environments.  Once the shells have been modified to warn
2871.9Scgd# of '.' in the path, the path tests should go away.
2881.32Slukem#
2891.31Slukemif checkyesno check_rootdotfiles; then
2901.67Slukem	rhome=~root
2911.15Smrg	umaskset=no
2921.15Smrg	list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
2931.15Smrg	for i in $list ; do
2941.15Smrg		if [ -f $i ] ; then
2951.67Slukem			if egrep '^[ \t]*umask[ \t]+[0-7]+' $i > /dev/null ;
2961.67Slukem			then
2971.15Smrg				umaskset=yes
2981.15Smrg			fi
2991.63Slukem			# Double check the umask value itself; ensure that
3001.67Slukem			# both the group and other write bits are set.
3011.67Slukem			#
3021.45Ssommerfe			egrep '^[ \t]*umask[ \t]+[0-7]+' $i |
3031.63Slukem			awk '{
3041.67Slukem				if ($2 ~ /^.$/ || $2 ~! /[^2367].$/) {
3051.63Slukem					print "\tRoot umask is group writeable"
3061.63Slukem				}
3071.67Slukem				if ($2 ~ /[^2367]$/) {
3081.63Slukem					print "\tRoot umask is other writeable"
3091.63Slukem			    	}
3101.67Slukem			    }' | sort -u
3111.26Slukem			SAVE_PATH=$PATH
3121.26Slukem			unset PATH
3131.15Smrg			/bin/csh -f -s << end-of-csh > /dev/null 2>&1
3141.15Smrg				source $i
3151.15Smrg				/bin/ls -ldgT \$path > $TMP1
3161.9Scgdend-of-csh
3171.26Slukem			PATH=$SAVE_PATH
3181.15Smrg			awk '{
3191.15Smrg				if ($10 ~ /^\.$/) {
3201.27Slukem					print "\tThe root path includes .";
3211.15Smrg					next;
3221.15Smrg				}
3231.15Smrg			     }
3241.15Smrg			     $1 ~ /^d....w/ \
3251.27Slukem		{ print "\tRoot path directory " $10 " is group writeable." } \
3261.15Smrg			     $1 ~ /^d.......w/ \
3271.27Slukem		{ print "\tRoot path directory " $10 " is other writeable." }' \
3281.67Slukem			< $TMP1
3291.15Smrg		fi
3301.67Slukem	done > $OUTPUT
3311.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
3321.27Slukem		printf "\nChecking root csh paths, umask values:\n$list\n\n"
3331.15Smrg		if [ -s $OUTPUT ]; then
3341.15Smrg			cat $OUTPUT
3351.15Smrg		fi
3361.15Smrg		if [ $umaskset = "no" ] ; then
3371.27Slukem		    printf "\tRoot csh startup files do not set the umask.\n"
3381.15Smrg		fi
3391.9Scgd	fi
3401.9Scgd
3411.15Smrg	umaskset=no
3421.23Slukem	list="/etc/profile ${rhome}/.profile"
3431.15Smrg	for i in $list; do
3441.15Smrg		if [ -f $i ] ; then
3451.15Smrg			if egrep umask $i > /dev/null ; then
3461.15Smrg				umaskset=yes
3471.15Smrg			fi
3481.15Smrg			egrep umask $i |
3491.67Slukem			awk '$2 ~ /^.$/ || $2 ~ /[^2367].$/ \
3501.27Slukem				{ print "\tRoot umask is group writeable" } \
3511.67Slukem			     $2 ~ /[^2367]$/ \
3521.67Slukem				{ print "\tRoot umask is other writeable" }'
3531.26Slukem			SAVE_PATH=$PATH
3541.26Slukem			unset PATH
3551.15Smrg			/bin/sh << end-of-sh > /dev/null 2>&1
3561.15Smrg				. $i
3571.26Slukem				list=\`echo \$PATH | /usr/bin/sed -e \
3581.26Slukem				    's/^:/.:/;s/:$/:./;s/::/:.:/g;s/:/ /g'\`
3591.15Smrg				/bin/ls -ldgT \$list > $TMP1
3601.9Scgdend-of-sh
3611.26Slukem			PATH=$SAVE_PATH
3621.15Smrg			awk '{
3631.15Smrg				if ($10 ~ /^\.$/) {
3641.27Slukem					print "\tThe root path includes .";
3651.15Smrg					next;
3661.15Smrg				}
3671.15Smrg			     }
3681.15Smrg			     $1 ~ /^d....w/ \
3691.27Slukem		{ print "\tRoot path directory " $10 " is group writeable." } \
3701.15Smrg			     $1 ~ /^d.......w/ \
3711.27Slukem		{ print "\tRoot path directory " $10 " is other writeable." }' \
3721.67Slukem			< $TMP1
3731.9Scgd
3741.15Smrg		fi
3751.67Slukem	done > $OUTPUT
3761.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
3771.15Smrg		printf "\nChecking root sh paths, umask values:\n$list\n"
3781.15Smrg		if [ -s $OUTPUT ]; then
3791.15Smrg			cat $OUTPUT
3801.15Smrg		fi
3811.15Smrg		if [ $umaskset = "no" ] ; then
3821.27Slukem			printf "\tRoot sh startup files do not set the umask.\n"
3831.15Smrg		fi
3841.9Scgd	fi
3851.9Scgdfi
3861.9Scgd
3871.9Scgd# Root and uucp should both be in /etc/ftpusers.
3881.32Slukem#
3891.31Slukemif checkyesno check_ftpusers; then
3901.28Slukem	list="uucp "`awk '$2 == 0 { print $1 }' $MPBYUID`
3911.27Slukem	for i in $list; do
3921.29Slukem		if /usr/libexec/ftpd -C $i ; then
3931.67Slukem			printf "\t$i is not denied\n"
3941.27Slukem		fi
3951.67Slukem	done > $OUTPUT
3961.28Slukem	if [ -s $OUTPUT ]; then
3971.28Slukem		printf "\nChecking the /etc/ftpusers configuration:\n"
3981.28Slukem		cat $OUTPUT
3991.28Slukem	fi
4001.9Scgdfi
4011.9Scgd
4021.43Sitojun# Uudecode should not be in the /etc/mail/aliases file.
4031.32Slukem#
4041.31Slukemif checkyesno check_aliases; then
4051.43Sitojun	for f in /etc/mail/aliases /etc/aliases; do
4061.43Sitojun		if [ -f $f ] && egrep '^[^#]*(uudecode|decode).*\|' $f; then
4071.43Sitojun			printf "\nEntry for uudecode in $f file.\n"
4081.43Sitojun		fi
4091.43Sitojun	done
4101.9Scgdfi
4111.9Scgd
4121.9Scgd# Files that should not have + signs.
4131.32Slukem#
4141.31Slukemif checkyesno check_rhosts; then
4151.15Smrg	list="/etc/hosts.equiv /etc/hosts.lpd"
4161.15Smrg	for f in $list ; do
4171.15Smrg		if [ -f $f ] && egrep '\+' $f > /dev/null ; then
4181.15Smrg			printf "\nPlus sign in $f file.\n"
4191.15Smrg		fi
4201.15Smrg	done
4211.15Smrg
4221.15Smrg	# Check for special users with .rhosts files.  Only root and toor should
4231.16Smikel	# have .rhosts files.  Also, .rhosts files should not have plus signs.
4241.15Smrg	awk -F: '$1 != "root" && $1 != "toor" && \
4251.15Smrg		($3 < 100 || $1 == "ftp" || $1 == "uucp") \
4261.20Smycroft			{ print $1 " " $9 }' $MP |
4271.19Smycroft	sort -k2 |
4281.15Smrg	while read uid homedir; do
4291.15Smrg		if [ -f ${homedir}/.rhosts ] ; then
4301.15Smrg			rhost=`ls -ldgT ${homedir}/.rhosts`
4311.46Schristos			printf -- "$uid: $rhost\n"
4321.15Smrg		fi
4331.15Smrg	done > $OUTPUT
4341.15Smrg	if [ -s $OUTPUT ] ; then
4351.15Smrg		printf "\nChecking for special users with .rhosts files.\n"
4361.15Smrg		cat $OUTPUT
4371.15Smrg	fi
4381.15Smrg
4391.15Smrg	while read uid homedir; do
4401.35Sfair		if [ -f ${homedir}/.rhosts -a -r ${homedir}/.rhosts ] && \
4411.41Schristos		    cat -f ${homedir}/.rhosts | egrep '\+' > /dev/null ; then
4421.46Schristos			printf -- "$uid: + in .rhosts file.\n"
4431.15Smrg		fi
4441.29Slukem	done < $MPBYPATH > $OUTPUT
4451.15Smrg	if [ -s $OUTPUT ] ; then
4461.15Smrg		printf "\nChecking .rhosts files syntax.\n"
4471.15Smrg		cat $OUTPUT
4481.15Smrg	fi
4491.9Scgdfi
4501.9Scgd
4511.9Scgd# Check home directories.  Directories should not be owned by someone else
4521.9Scgd# or writeable.
4531.32Slukem#
4541.31Slukemif checkyesno check_homes; then
4551.15Smrg	while read uid homedir; do
4561.15Smrg		if [ -d ${homedir}/ ] ; then
4571.15Smrg			file=`ls -ldgT ${homedir}`
4581.46Schristos			printf -- "$uid $file\n"
4591.9Scgd		fi
4601.29Slukem	done < $MPBYPATH |
4611.15Smrg	awk '$1 != $4 && $4 != "root" \
4621.15Smrg		{ print "user " $1 " home directory is owned by " $4 }
4631.15Smrg	     $2 ~ /^-....w/ \
4641.15Smrg		{ print "user " $1 " home directory is group writeable" }
4651.15Smrg	     $2 ~ /^-.......w/ \
4661.27Slukem		{ print "user " $1 " home directory is other writeable" }' \
4671.27Slukem	    > $OUTPUT
4681.15Smrg	if [ -s $OUTPUT ] ; then
4691.15Smrg		printf "\nChecking home directories.\n"
4701.15Smrg		cat $OUTPUT
4711.15Smrg	fi
4721.15Smrg
4731.15Smrg	# Files that should not be owned by someone else or readable.
4741.67Slukem	list=".Xauthority .netrc .ssh/id_dsa .ssh/id_rsa .ssh/identity"
4751.15Smrg	while read uid homedir; do
4761.15Smrg		for f in $list ; do
4771.15Smrg			file=${homedir}/${f}
4781.15Smrg			if [ -f $file ] ; then
4791.46Schristos				printf -- "$uid $f `ls -ldgT $file`\n"
4801.15Smrg			fi
4811.15Smrg		done
4821.29Slukem	done < $MPBYPATH |
4831.15Smrg	awk '$1 != $5 && $5 != "root" \
4841.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
4851.15Smrg	     $3 ~ /^-...r/ \
4861.15Smrg		{ print "user " $1 " " $2 " file is group readable" }
4871.15Smrg	     $3 ~ /^-......r/ \
4881.15Smrg		{ print "user " $1 " " $2 " file is other readable" }
4891.15Smrg	     $3 ~ /^-....w/ \
4901.15Smrg		{ print "user " $1 " " $2 " file is group writeable" }
4911.15Smrg	     $3 ~ /^-.......w/ \
4921.27Slukem		{ print "user " $1 " " $2 " file is other writeable" }' \
4931.27Slukem	    > $OUTPUT
4941.15Smrg
4951.15Smrg	# Files that should not be owned by someone else or writeable.
4961.19Smycroft	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
4971.19Smycroft	      .cshrc .emacs .exrc .forward .history .klogin .login .logout \
4981.67Slukem	      .profile .qmail .rc_history .rhosts .shosts ssh .tcshrc .twmrc \
4991.67Slukem	      .xinitrc .xsession .ssh/authorized_keys .ssh/authorized_keys2 \
5001.67Slukem	      .ssh/config .ssh/id_dsa.pub .ssh/id_rsa.pub .ssh/identity.pub \
5011.67Slukem	      .ssh/known_hosts .ssh/known_hosts2"
5021.15Smrg	while read uid homedir; do
5031.15Smrg		for f in $list ; do
5041.15Smrg			file=${homedir}/${f}
5051.15Smrg			if [ -f $file ] ; then
5061.46Schristos				printf -- "$uid $f `ls -ldgT $file`\n"
5071.15Smrg			fi
5081.15Smrg		done
5091.29Slukem	done < $MPBYPATH |
5101.15Smrg	awk '$1 != $5 && $5 != "root" \
5111.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
5121.15Smrg	     $3 ~ /^-....w/ \
5131.15Smrg		{ print "user " $1 " " $2 " file is group writeable" }
5141.15Smrg	     $3 ~ /^-.......w/ \
5151.27Slukem		{ print "user " $1 " " $2 " file is other writeable" }' \
5161.27Slukem	    >> $OUTPUT
5171.15Smrg	if [ -s $OUTPUT ] ; then
5181.15Smrg		printf "\nChecking dot files.\n"
5191.15Smrg		cat $OUTPUT
5201.15Smrg	fi
5211.9Scgdfi
5221.9Scgd
5231.9Scgd# Mailboxes should be owned by user and unreadable.
5241.32Slukem#
5251.31Slukemif checkyesno check_varmail; then
5261.63Slukem	ls -l /var/mail | \
5271.63Slukem	awk '	NR == 1 { next; }
5281.63Slukem	    	$3 != $9 {
5291.63Slukem			print "user " $9 " mailbox is owned by " $3
5301.63Slukem		}
5311.63Slukem		$1 != "-rw-------" {
5321.63Slukem			print "user " $9 " mailbox is " $1 ", group " $4
5331.63Slukem		}' > $OUTPUT
5341.15Smrg	if [ -s $OUTPUT ] ; then
5351.15Smrg		printf "\nChecking mailbox ownership.\n"
5361.15Smrg		cat $OUTPUT
5371.15Smrg	fi
5381.15Smrgfi
5391.15Smrg
5401.32Slukem# NFS exports shouldn't be globally exported
5411.32Slukem#
5421.32Slukemif checkyesno check_nfs && [ -f /etc/exports ]; then
5431.32Slukem	awk '{
5441.22Slukem		# ignore comments and blank lines
5451.39Shubertf		if ($0 ~ /^\#/ || $0 ~ /^$/ )
5461.22Slukem			next;
5471.22Slukem
5481.15Smrg		readonly = 0;
5491.15Smrg		for (i = 2; i <= NF; ++i) {
5501.15Smrg			if ($i ~ /-ro/)
5511.15Smrg				readonly = 1;
5521.15Smrg			else if ($i !~ /^-/)
5531.15Smrg				next;
5541.15Smrg		}
5551.15Smrg		if (readonly)
5561.15Smrg			print "File system " $1 " globally exported, read-only."
5571.15Smrg		else
5581.15Smrg			print "File system " $1 " globally exported, read-write."
5591.32Slukem	}' < /etc/exports > $OUTPUT
5601.32Slukem	if [ -s $OUTPUT ] ; then
5611.15Smrg		printf "\nChecking for globally exported file systems.\n"
5621.15Smrg		cat $OUTPUT
5631.15Smrg	fi
5641.9Scgdfi
5651.9Scgd
5661.9Scgd# Display any changes in setuid files and devices.
5671.32Slukem#
5681.31Slukemif checkyesno check_devices; then
5691.28Slukem	> $ERR
5701.15Smrg	(find / \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \
5711.15Smrg			-o -fstype procfs \) -a -prune -o \
5721.21Smycroft	    \( \( -perm -u+s -a ! -type d \) -o \
5731.21Smycroft	       \( -perm -g+s -a ! -type d \) -o \
5741.24Slukem	       -type b -o -type c \) -print0 | \
5751.24Slukem	xargs -0 ls -ldgTq | sort +9 > $LIST) 2> $OUTPUT
5761.15Smrg
5771.15Smrg	# Display any errors that occurred during system file walk.
5781.15Smrg	if [ -s $OUTPUT ] ; then
5791.28Slukem		printf "Setuid/device find errors:\n" >> $ERR
5801.28Slukem		cat $OUTPUT >> $ERR
5811.28Slukem		printf "\n" >> $ERR
5821.15Smrg	fi
5831.15Smrg
5841.15Smrg	# Display any changes in the setuid file list.
5851.15Smrg	egrep -v '^[bc]' $LIST > $TMP1
5861.15Smrg	if [ -s $TMP1 ] ; then
5871.15Smrg		# Check to make sure uudecode isn't setuid.
5881.15Smrg		if grep -w uudecode $TMP1 > /dev/null ; then
5891.28Slukem			printf "\nUudecode is setuid.\n" >> $ERR
5901.15Smrg		fi
5911.15Smrg
5921.67Slukem		file=$work_dir/setuid
5931.67Slukem		migrate_file "$backup_dir/setuid" "$file"
5941.67Slukem		CUR=${file}.current
5951.67Slukem		BACK=${file}.backup
5961.15Smrg		if [ -s $CUR ] ; then
5971.15Smrg			if cmp -s $CUR $TMP1 ; then
5981.15Smrg				:
5991.15Smrg			else
6001.15Smrg				> $TMP2
6011.15Smrg				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
6021.15Smrg				if [ -s $OUTPUT ] ; then
6031.28Slukem					printf "Setuid additions:\n" >> $ERR
6041.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6051.28Slukem					printf "\n" >> $ERR
6061.15Smrg				fi
6071.15Smrg
6081.15Smrg				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
6091.15Smrg				if [ -s $OUTPUT ] ; then
6101.28Slukem					printf "Setuid deletions:\n" >> $ERR
6111.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6121.28Slukem					printf "\n" >> $ERR
6131.15Smrg				fi
6141.15Smrg
6151.20Smycroft				sort -k10 $TMP2 $CUR $TMP1 | \
6161.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
6171.27Slukem				    uniq -u > $OUTPUT
6181.15Smrg				if [ -s $OUTPUT ] ; then
6191.28Slukem					printf "Setuid changes:\n" >> $ERR
6201.28Slukem					column -t $OUTPUT >> $ERR
6211.28Slukem					printf "\n" >> $ERR
6221.15Smrg				fi
6231.9Scgd
6241.52Satatat				backup_file update $TMP1 $CUR $BACK
6251.9Scgd			fi
6261.15Smrg		else
6271.28Slukem			printf "Setuid additions:\n" >> $ERR
6281.28Slukem			column -t $TMP1 >> $ERR
6291.28Slukem			printf "\n" >> $ERR
6301.52Satatat			backup_file add $TMP1 $CUR $BACK
6311.9Scgd		fi
6321.15Smrg	fi
6331.15Smrg
6341.27Slukem	# Check for block and character disk devices that are readable or
6351.27Slukem	# writeable or not owned by root.operator.
6361.15Smrg	>$TMP1
6371.61Slukem	DISKLIST="ccd ch hk hp ld md ra raid rb rd rl rx \
6381.57Ssimonb	    sd se ss uk up vnd wd xd xy"
6391.27Slukem#	DISKLIST="$DISKLIST ct mt st wt"
6401.15Smrg	for i in $DISKLIST; do
6411.15Smrg		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
6421.15Smrg		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
6431.15Smrg	done
6441.15Smrg
6451.15Smrg	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
6461.25Slukem		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
6471.25Slukem		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
6481.15Smrg	if [ -s $OUTPUT ] ; then
6491.28Slukem		printf "\nChecking disk ownership and permissions.\n" >> $ERR
6501.28Slukem		cat $OUTPUT >> $ERR
6511.28Slukem		printf "\n" >> $ERR
6521.9Scgd	fi
6531.9Scgd
6541.15Smrg	# Display any changes in the device file list.
6551.20Smycroft	egrep '^[bc]' $LIST | sort -k11 > $TMP1
6561.15Smrg	if [ -s $TMP1 ] ; then
6571.67Slukem		file=$work_dir/device
6581.67Slukem		migrate_file "$backup_dir/device" "$file"
6591.67Slukem		CUR=${file}.current
6601.67Slukem		BACK=${file}.backup
6611.15Smrg
6621.15Smrg		if [ -s $CUR ] ; then
6631.15Smrg			if cmp -s $CUR $TMP1 ; then
6641.15Smrg				:
6651.15Smrg			else
6661.15Smrg				> $TMP2
6671.15Smrg				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
6681.15Smrg				if [ -s $OUTPUT ] ; then
6691.28Slukem					printf "Device additions:\n" >> $ERR
6701.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6711.28Slukem					printf "\n" >> $ERR
6721.15Smrg				fi
6731.15Smrg
6741.15Smrg				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
6751.15Smrg				if [ -s $OUTPUT ] ; then
6761.28Slukem					printf "Device deletions:\n" >> $ERR
6771.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6781.28Slukem					printf "\n" >> $ERR
6791.15Smrg				fi
6801.15Smrg
6811.27Slukem				# Report any block device change. Ignore
6821.27Slukem				# character devices, only the name is
6831.27Slukem				# significant.
6841.15Smrg				cat $TMP2 $CUR $TMP1 | \
6851.27Slukem				    sed -e '/^c/d' | \
6861.27Slukem				    sort -k11 | \
6871.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
6881.27Slukem				    uniq -u > $OUTPUT
6891.15Smrg				if [ -s $OUTPUT ] ; then
6901.28Slukem					printf "Block device changes:\n" >> $ERR
6911.28Slukem					column -t $OUTPUT >> $ERR
6921.28Slukem					printf "\n" >> $ERR
6931.15Smrg				fi
6941.9Scgd
6951.52Satatat				backup_file update $TMP1 $CUR $BACK
6961.9Scgd			fi
6971.15Smrg		else
6981.28Slukem			printf "Device additions:\n" >> $ERR
6991.28Slukem			column -t $TMP1 >> $ERR
7001.28Slukem			printf "\n" >> $ERR
7011.52Satatat			backup_file add $TMP1 $CUR $BACK >> $ERR
7021.9Scgd		fi
7031.28Slukem	fi
7041.28Slukem	if [ -s $ERR ] ; then
7051.28Slukem		printf "\nChecking setuid files and devices:\n"
7061.28Slukem		cat $ERR
7071.28Slukem		printf "\n"
7081.9Scgd	fi
7091.9Scgdfi
7101.9Scgd
7111.9Scgd# Check special files.
7121.9Scgd# Check system binaries.
7131.9Scgd#
7141.9Scgd# Create the mtree tree specifications using:
7151.67Slukem#	mtree -cx -pDIR -kmd5,uid,gid,mode,nlink,size,link,time > DIR.secure
7161.38Skleink#	chown root:wheel DIR.secure
7171.67Slukem#	chmod u+r,go= DIR.secure
7181.9Scgd#
7191.9Scgd# Note, this is not complete protection against Trojan horsed binaries, as
7201.9Scgd# the hacker can modify the tree specification to match the replaced binary.
7211.9Scgd# For details on really protecting yourself against modified binaries, see
7221.9Scgd# the mtree(8) manual page.
7231.32Slukem#
7241.31Slukemif checkyesno check_mtree; then
7251.67Slukem	for file in $special_files; do
7261.67Slukem		[ ! -s $file ] && continue
7271.67Slukem		mtree -e -l -p / -f $file
7281.67Slukem	done > $OUTPUT
7291.15Smrg	if [ -s $OUTPUT ]; then
7301.9Scgd		printf "\nChecking special files and directories.\n"
7311.9Scgd		cat $OUTPUT
7321.9Scgd	fi
7331.9Scgd
7341.16Smikel	for file in /etc/mtree/*.secure; do
7351.16Smikel		[ $file = '/etc/mtree/*.secure' ] && continue
7361.9Scgd		tree=`sed -n -e '3s/.* //p' -e 3q $file`
7371.9Scgd		mtree -f $file -p $tree > $TMP1
7381.9Scgd		if [ -s $TMP1 ]; then
7391.67Slukem			printf "\nChecking $tree:\n"
7401.67Slukem			cat $TMP1
7411.9Scgd		fi
7421.67Slukem	done > $OUTPUT
7431.15Smrg	if [ -s $OUTPUT ]; then
7441.9Scgd		printf "\nChecking system binaries:\n"
7451.9Scgd		cat $OUTPUT
7461.9Scgd	fi
7471.9Scgdfi
7481.9Scgd
7491.32Slukem# Backup disklabels of available disks
7501.32Slukem#
7511.32Slukemif checkyesno check_disklabels; then
7521.67Slukem		# migrate old disklabels
7531.67Slukem	for file in `ls -1d $backup_dir/$backup_dir/disklabel.* \
7541.67Slukem	    $backup_dir/disklabel.* 2>/dev/null`; do
7551.67Slukem		migrate_file "$file" "$work_dir/${file##*/}"
7561.67Slukem	done
7571.67Slukem
7581.67Slukem		# generate list of old disklabels & fdisks and remove them
7591.67Slukem	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* 2>/dev/null |
7601.52Satatat	    egrep -v '\.(backup|current)(,v)?$' > $LABELS
7611.32Slukem	xargs rm < $LABELS
7621.32Slukem
7631.67Slukem		# generate disklabels of all disks excluding:	cd fd md
7641.63Slukem	disks=`iostat -x | awk 'NR > 1 && $1 !~ /^[cfm]d/ { print $1; }'`
7651.32Slukem	for i in $disks; do
7661.67Slukem		disklabel $i > "$work_dir/disklabel.$i" 2>/dev/null
7671.32Slukem	done
7681.32Slukem
7691.67Slukem		# if fdisk is available, generate fdisks for:	ed ld sd wd
7701.67Slukem	if [ -x /sbin/fdisk ]; then
7711.67Slukem		disks=`iostat -x| awk 'NR > 1 && $1 ~ /^[elsw]d/ { print $1; }'`
7721.67Slukem		for i in $disks; do
7731.67Slukem			/sbin/fdisk $i > "$work_dir/fdisk.$i" 2>/dev/null
7741.67Slukem		done
7751.67Slukem	fi
7761.67Slukem
7771.67Slukem		# append list of new disklabels and fdisks
7781.67Slukem	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* 2>/dev/null |
7791.52Satatat	    egrep -v '\.(backup|current)(,v)?$' >> $LABELS
7801.62Satatat	CHANGELIST="$LABELS $CHANGELIST"
7811.62Satatatfi
7821.62Satatat
7831.62Satatat# Check for changes in the list of installed pkgs
7841.62Satatat#
7851.65Slukemif checkyesno check_pkgs && [ -d $pkgdb_dir ]; then
7861.67Slukem	pkgs=$work_dir/pkgs
7871.67Slukem	migrate_file "$backup_dir/pkgs" "$pkgs"
7881.65Slukem	(	cd $pkgdb_dir
7891.62Satatat		pkg_info | sort
7901.62Satatat		echo ""
7911.62Satatat		find . \( -name +REQUIRED_BY -o -name +CONTENTS \) -print0 |
7921.72Slukem			xargs -0 ls -ldgTq | sort -t. +1 | sed -e 's, \./, ,'
7931.62Satatat	 ) > $pkgs
7941.67Slukem	echo "$pkgs" > $PKGS
7951.62Satatat	CHANGELIST="$PKGS $CHANGELIST"
7961.32Slukemfi
7971.32Slukem
7981.67Slukem# List of files that get backed up and checked for any modifications.
7991.9Scgd# Any changes cause the files to rotate.
8001.32Slukem#
8011.67Slukemif checkyesno check_changelist ; then
8021.67Slukem	for file in $special_files; do
8031.67Slukem		[ ! -s $file ] && continue
8041.67Slukem		mtree -D -k type -f $file -E exclude |
8051.67Slukem		    sed '/^type=file/!d ; s/type=file \.//'
8061.67Slukem	done > $CHANGEFILES
8071.67Slukem
8081.68Slukem		# Add other files which might dynamically exist:
8091.67Slukem		#	/etc/ifconfig.*
8101.67Slukem		#	/etc/raid*.conf
8111.68Slukem		#	/etc/rc.d/*
8121.67Slukem		#	/etc/rc.conf.d/*
8131.68Slukem		#
8141.68Slukem	ls -1d	$backup_dir/etc/ifconfig.*.current	\
8151.68Slukem		$backup_dir/etc/raid*.conf.current	\
8161.68Slukem		$backup_dir/etc/rc.d/*.current		\
8171.68Slukem		$backup_dir/etc/rc.conf.d/*.current	\
8181.68Slukem	    2>/dev/null |
8191.67Slukem	    sed "s,^$backup_dir/,/, ; s,\.current$,," >> $CHANGEFILES
8201.68Slukem	ls -1d	/etc/ifconfig.*		\
8211.68Slukem		/etc/raid*.conf		\
8221.68Slukem		/etc/rc.d/*		\
8231.68Slukem		/etc/rc.conf.d/*	\
8241.67Slukem	    2>/dev/null >> $CHANGEFILES
8251.67Slukem
8261.68Slukem		# Add /etc/changelist
8271.68Slukem		#
8281.68Slukem	if [ -s /etc/changelist ]; then
8291.68Slukem		grep -v '^#' /etc/changelist >> $CHANGEFILES
8301.68Slukem	fi
8311.68Slukem
8321.67Slukem	CHANGELIST="$CHANGEFILES $CHANGELIST"
8331.67Slukemfi
8341.67Slukem
8351.67Slukem# Special case backups, including the master password file and
8361.67Slukem# ssh private host keys. The normal backup mechanisms for
8371.67Slukem# $check_changelist (see below) also print out the actual file
8381.67Slukem# differences and we don't want to do that for these files
8391.67Slukem#
8401.67Slukemecho $MP > $TMP1			# always add /etc/master.passwd
8411.67Slukemfor file in $special_files; do
8421.67Slukem	[ ! -s $file ] && continue
8431.70Slukem	mtree -D -k type -f $file -I nodiff |
8441.67Slukem	    sed '/^type=file/!d ; s/type=file \.//'
8451.67Slukemdone >> $TMP1
8461.73Slukemgrep -v '^$' $TMP1 | sort -u > $TMP2
8471.68Slukem
8481.69Slukemwhile read file; do
8491.67Slukem	backup_and_diff "$file" no
8501.69Slukemdone < $TMP2
8511.67Slukem
8521.32Slukem
8531.32Slukemif [ -n "$CHANGELIST" ]; then
8541.73Slukem	grep -h -v '^$' $CHANGELIST | sort -u > $TMP1
8551.68Slukem	comm -23 $TMP1 $TMP2 | while read file; do
8561.67Slukem		backup_and_diff "$file" yes
8571.9Scgd	done
8581.44Sadfi
8591.44Sad
8601.44Sadif [ -f /etc/security.local ]; then
8611.44Sad	echo ""
8621.44Sad	echo "Running /etc/security.local:"
8631.44Sad	. /etc/security.local
8641.9Scgdfi
865