security revision 1.79
11.1Scgd#!/bin/sh -
21.1Scgd#
31.79Selric#	$NetBSD: security,v 1.79 2002/08/20 07:53:51 elric 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.78Sitojun		    $2 !~ /^\$1/ &&
2091.78Sitojun		    $2 !~ /^\$2/ &&
2101.59Sperry		    $2 != "" &&
2111.59Sperry		    $2 !~ /^\*[A-z-]+$/ &&
2121.59Sperry		    $1 != "toor") {
2131.25Slukem			if ($10 == "" || shells[$10])
2141.27Slukem		    printf "Login %s is off but still has a valid shell (%s)\n",
2151.25Slukem				    $1, $10;
2161.25Slukem		} else if (! shells[$10])
2171.25Slukem			printf "Login %s does not have a valid shell (%s)\n",
2181.25Slukem			    $1, $10;
2191.15Smrg		if ($3 == 0 && $1 != "root" && $1 != "toor")
2201.25Slukem			printf "Login %s has a user id of 0.\n", $1;
2211.15Smrg		if ($3 < 0)
2221.25Slukem			printf "Login %s has a negative user id.\n", $1;
2231.15Smrg		if ($4 < 0)
2241.25Slukem			printf "Login %s has a negative group id.\n", $1;
2251.15Smrg	}' < $MP > $OUTPUT
2261.15Smrg	if [ -s $OUTPUT ] ; then
2271.15Smrg		printf "\nChecking the $MP file:\n"
2281.15Smrg		cat $OUTPUT
2291.15Smrg	fi
2301.15Smrg
2311.15Smrg	awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
2321.15Smrg	if [ -s $OUTPUT ] ; then
2331.15Smrg		printf "\n$MP has duplicate user names.\n"
2341.15Smrg		column $OUTPUT
2351.15Smrg	fi
2361.15Smrg
2371.37Swrstuden# To not exclude 'toor', a standard duplicate root account, from the duplicate
2381.37Swrstuden# account test, uncomment the line below (without egrep in it)and comment
2391.37Swrstuden# out the line (with egrep in it) below it.
2401.37Swrstuden#
2411.37Swrstuden#	< $MPBYUID uniq -d -f 1 | awk '{ print $2 }' > $TMP2
2421.36Swrstuden	< $MPBYUID egrep -v '^toor ' | uniq -d -f 1 | awk '{ print $2 }' > $TMP2
2431.15Smrg	if [ -s $TMP2 ] ; then
2441.15Smrg		printf "\n$MP has duplicate user id's.\n"
2451.15Smrg		while read uid; do
2461.28Slukem			grep -w $uid $MPBYUID
2471.15Smrg		done < $TMP2 | column
2481.15Smrg	fi
2491.9Scgdfi
2501.9Scgd
2511.9Scgd# Check the group file syntax.
2521.32Slukem#
2531.31Slukemif checkyesno check_group; then
2541.15Smrg	GRP=/etc/group
2551.49Sjdolecek	awk -F: -v "len=$max_grouplen" '{
2561.15Smrg		if ($0 ~ /^[	 ]*$/) {
2571.25Slukem			printf "Line %d is a blank line.\n", NR;
2581.15Smrg			next;
2591.15Smrg		}
2601.34Sabs		if (NF != 4 && ($1 != "+" || NF != 1))
2611.25Slukem			printf "Line %d has the wrong number of fields.\n", NR;
2621.34Sabs		if ($1 == "+" )  {
2631.34Sabs			next;
2641.34Sabs		}
2651.53Satatat		if ($1 !~ /^[A-Za-z0-9]([-A-Za-z0-9]*[A-Za-z0-9])*$/)
2661.25Slukem			printf "Group %s has non-alphanumeric characters.\n",
2671.25Slukem			    $1;
2681.49Sjdolecek		if (length($1) > len)
2691.49Sjdolecek			printf "Group %s has more than "len" characters.\n", $1;
2701.15Smrg		if ($3 !~ /[0-9]*/)
2711.25Slukem			printf "Login %s has a negative group id.\n", $1;
2721.15Smrg	}' < $GRP > $OUTPUT
2731.15Smrg	if [ -s $OUTPUT ] ; then
2741.15Smrg		printf "\nChecking the $GRP file:\n"
2751.15Smrg		cat $OUTPUT
2761.15Smrg	fi
2771.15Smrg
2781.15Smrg	awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
2791.15Smrg	if [ -s $OUTPUT ] ; then
2801.15Smrg		printf "\n$GRP has duplicate group names.\n"
2811.15Smrg		column $OUTPUT
2821.15Smrg	fi
2831.9Scgdfi
2841.9Scgd
2851.9Scgd# Check for root paths, umask values in startup files.
2861.9Scgd# The check for the root paths is problematical -- it's likely to fail
2871.9Scgd# in other environments.  Once the shells have been modified to warn
2881.9Scgd# of '.' in the path, the path tests should go away.
2891.32Slukem#
2901.31Slukemif checkyesno check_rootdotfiles; then
2911.67Slukem	rhome=~root
2921.15Smrg	umaskset=no
2931.15Smrg	list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
2941.15Smrg	for i in $list ; do
2951.15Smrg		if [ -f $i ] ; then
2961.67Slukem			if egrep '^[ \t]*umask[ \t]+[0-7]+' $i > /dev/null ;
2971.67Slukem			then
2981.15Smrg				umaskset=yes
2991.15Smrg			fi
3001.63Slukem			# Double check the umask value itself; ensure that
3011.67Slukem			# both the group and other write bits are set.
3021.67Slukem			#
3031.45Ssommerfe			egrep '^[ \t]*umask[ \t]+[0-7]+' $i |
3041.63Slukem			awk '{
3051.67Slukem				if ($2 ~ /^.$/ || $2 ~! /[^2367].$/) {
3061.63Slukem					print "\tRoot umask is group writeable"
3071.63Slukem				}
3081.67Slukem				if ($2 ~ /[^2367]$/) {
3091.63Slukem					print "\tRoot umask is other writeable"
3101.63Slukem			    	}
3111.67Slukem			    }' | sort -u
3121.26Slukem			SAVE_PATH=$PATH
3131.26Slukem			unset PATH
3141.15Smrg			/bin/csh -f -s << end-of-csh > /dev/null 2>&1
3151.15Smrg				source $i
3161.15Smrg				/bin/ls -ldgT \$path > $TMP1
3171.9Scgdend-of-csh
3181.76Satatat			export PATH=$SAVE_PATH
3191.15Smrg			awk '{
3201.15Smrg				if ($10 ~ /^\.$/) {
3211.27Slukem					print "\tThe root path includes .";
3221.15Smrg					next;
3231.15Smrg				}
3241.15Smrg			     }
3251.15Smrg			     $1 ~ /^d....w/ \
3261.27Slukem		{ print "\tRoot path directory " $10 " is group writeable." } \
3271.15Smrg			     $1 ~ /^d.......w/ \
3281.27Slukem		{ print "\tRoot path directory " $10 " is other writeable." }' \
3291.67Slukem			< $TMP1
3301.15Smrg		fi
3311.67Slukem	done > $OUTPUT
3321.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
3331.27Slukem		printf "\nChecking root csh paths, umask values:\n$list\n\n"
3341.15Smrg		if [ -s $OUTPUT ]; then
3351.15Smrg			cat $OUTPUT
3361.15Smrg		fi
3371.15Smrg		if [ $umaskset = "no" ] ; then
3381.27Slukem		    printf "\tRoot csh startup files do not set the umask.\n"
3391.15Smrg		fi
3401.9Scgd	fi
3411.9Scgd
3421.15Smrg	umaskset=no
3431.23Slukem	list="/etc/profile ${rhome}/.profile"
3441.15Smrg	for i in $list; do
3451.15Smrg		if [ -f $i ] ; then
3461.15Smrg			if egrep umask $i > /dev/null ; then
3471.15Smrg				umaskset=yes
3481.15Smrg			fi
3491.15Smrg			egrep umask $i |
3501.67Slukem			awk '$2 ~ /^.$/ || $2 ~ /[^2367].$/ \
3511.27Slukem				{ print "\tRoot umask is group writeable" } \
3521.67Slukem			     $2 ~ /[^2367]$/ \
3531.67Slukem				{ print "\tRoot umask is other writeable" }'
3541.26Slukem			SAVE_PATH=$PATH
3551.26Slukem			unset PATH
3561.15Smrg			/bin/sh << end-of-sh > /dev/null 2>&1
3571.15Smrg				. $i
3581.26Slukem				list=\`echo \$PATH | /usr/bin/sed -e \
3591.26Slukem				    's/^:/.:/;s/:$/:./;s/::/:.:/g;s/:/ /g'\`
3601.15Smrg				/bin/ls -ldgT \$list > $TMP1
3611.9Scgdend-of-sh
3621.76Satatat			export PATH=$SAVE_PATH
3631.15Smrg			awk '{
3641.15Smrg				if ($10 ~ /^\.$/) {
3651.27Slukem					print "\tThe root path includes .";
3661.15Smrg					next;
3671.15Smrg				}
3681.15Smrg			     }
3691.15Smrg			     $1 ~ /^d....w/ \
3701.27Slukem		{ print "\tRoot path directory " $10 " is group writeable." } \
3711.15Smrg			     $1 ~ /^d.......w/ \
3721.27Slukem		{ print "\tRoot path directory " $10 " is other writeable." }' \
3731.67Slukem			< $TMP1
3741.9Scgd
3751.15Smrg		fi
3761.67Slukem	done > $OUTPUT
3771.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
3781.15Smrg		printf "\nChecking root sh paths, umask values:\n$list\n"
3791.15Smrg		if [ -s $OUTPUT ]; then
3801.15Smrg			cat $OUTPUT
3811.15Smrg		fi
3821.15Smrg		if [ $umaskset = "no" ] ; then
3831.27Slukem			printf "\tRoot sh startup files do not set the umask.\n"
3841.15Smrg		fi
3851.9Scgd	fi
3861.9Scgdfi
3871.9Scgd
3881.9Scgd# Root and uucp should both be in /etc/ftpusers.
3891.32Slukem#
3901.31Slukemif checkyesno check_ftpusers; then
3911.28Slukem	list="uucp "`awk '$2 == 0 { print $1 }' $MPBYUID`
3921.27Slukem	for i in $list; do
3931.29Slukem		if /usr/libexec/ftpd -C $i ; then
3941.67Slukem			printf "\t$i is not denied\n"
3951.27Slukem		fi
3961.67Slukem	done > $OUTPUT
3971.28Slukem	if [ -s $OUTPUT ]; then
3981.28Slukem		printf "\nChecking the /etc/ftpusers configuration:\n"
3991.28Slukem		cat $OUTPUT
4001.28Slukem	fi
4011.9Scgdfi
4021.9Scgd
4031.43Sitojun# Uudecode should not be in the /etc/mail/aliases file.
4041.32Slukem#
4051.31Slukemif checkyesno check_aliases; then
4061.43Sitojun	for f in /etc/mail/aliases /etc/aliases; do
4071.43Sitojun		if [ -f $f ] && egrep '^[^#]*(uudecode|decode).*\|' $f; then
4081.43Sitojun			printf "\nEntry for uudecode in $f file.\n"
4091.43Sitojun		fi
4101.43Sitojun	done
4111.9Scgdfi
4121.9Scgd
4131.9Scgd# Files that should not have + signs.
4141.32Slukem#
4151.31Slukemif checkyesno check_rhosts; then
4161.15Smrg	list="/etc/hosts.equiv /etc/hosts.lpd"
4171.15Smrg	for f in $list ; do
4181.15Smrg		if [ -f $f ] && egrep '\+' $f > /dev/null ; then
4191.15Smrg			printf "\nPlus sign in $f file.\n"
4201.15Smrg		fi
4211.15Smrg	done
4221.15Smrg
4231.15Smrg	# Check for special users with .rhosts files.  Only root and toor should
4241.16Smikel	# have .rhosts files.  Also, .rhosts files should not have plus signs.
4251.15Smrg	awk -F: '$1 != "root" && $1 != "toor" && \
4261.15Smrg		($3 < 100 || $1 == "ftp" || $1 == "uucp") \
4271.20Smycroft			{ print $1 " " $9 }' $MP |
4281.19Smycroft	sort -k2 |
4291.15Smrg	while read uid homedir; do
4301.15Smrg		if [ -f ${homedir}/.rhosts ] ; then
4311.15Smrg			rhost=`ls -ldgT ${homedir}/.rhosts`
4321.46Schristos			printf -- "$uid: $rhost\n"
4331.15Smrg		fi
4341.15Smrg	done > $OUTPUT
4351.15Smrg	if [ -s $OUTPUT ] ; then
4361.15Smrg		printf "\nChecking for special users with .rhosts files.\n"
4371.15Smrg		cat $OUTPUT
4381.15Smrg	fi
4391.15Smrg
4401.15Smrg	while read uid homedir; do
4411.35Sfair		if [ -f ${homedir}/.rhosts -a -r ${homedir}/.rhosts ] && \
4421.41Schristos		    cat -f ${homedir}/.rhosts | egrep '\+' > /dev/null ; then
4431.46Schristos			printf -- "$uid: + in .rhosts file.\n"
4441.15Smrg		fi
4451.29Slukem	done < $MPBYPATH > $OUTPUT
4461.15Smrg	if [ -s $OUTPUT ] ; then
4471.15Smrg		printf "\nChecking .rhosts files syntax.\n"
4481.15Smrg		cat $OUTPUT
4491.15Smrg	fi
4501.9Scgdfi
4511.9Scgd
4521.9Scgd# Check home directories.  Directories should not be owned by someone else
4531.9Scgd# or writeable.
4541.32Slukem#
4551.31Slukemif checkyesno check_homes; then
4561.15Smrg	while read uid homedir; do
4571.15Smrg		if [ -d ${homedir}/ ] ; then
4581.15Smrg			file=`ls -ldgT ${homedir}`
4591.46Schristos			printf -- "$uid $file\n"
4601.9Scgd		fi
4611.29Slukem	done < $MPBYPATH |
4621.15Smrg	awk '$1 != $4 && $4 != "root" \
4631.15Smrg		{ print "user " $1 " home directory is owned by " $4 }
4641.15Smrg	     $2 ~ /^-....w/ \
4651.15Smrg		{ print "user " $1 " home directory is group writeable" }
4661.15Smrg	     $2 ~ /^-.......w/ \
4671.27Slukem		{ print "user " $1 " home directory is other writeable" }' \
4681.27Slukem	    > $OUTPUT
4691.15Smrg	if [ -s $OUTPUT ] ; then
4701.15Smrg		printf "\nChecking home directories.\n"
4711.15Smrg		cat $OUTPUT
4721.15Smrg	fi
4731.15Smrg
4741.15Smrg	# Files that should not be owned by someone else or readable.
4751.67Slukem	list=".Xauthority .netrc .ssh/id_dsa .ssh/id_rsa .ssh/identity"
4761.15Smrg	while read uid homedir; do
4771.15Smrg		for f in $list ; do
4781.15Smrg			file=${homedir}/${f}
4791.15Smrg			if [ -f $file ] ; then
4801.46Schristos				printf -- "$uid $f `ls -ldgT $file`\n"
4811.15Smrg			fi
4821.15Smrg		done
4831.29Slukem	done < $MPBYPATH |
4841.15Smrg	awk '$1 != $5 && $5 != "root" \
4851.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
4861.15Smrg	     $3 ~ /^-...r/ \
4871.15Smrg		{ print "user " $1 " " $2 " file is group readable" }
4881.15Smrg	     $3 ~ /^-......r/ \
4891.15Smrg		{ print "user " $1 " " $2 " file is other readable" }
4901.15Smrg	     $3 ~ /^-....w/ \
4911.15Smrg		{ print "user " $1 " " $2 " file is group writeable" }
4921.15Smrg	     $3 ~ /^-.......w/ \
4931.27Slukem		{ print "user " $1 " " $2 " file is other writeable" }' \
4941.27Slukem	    > $OUTPUT
4951.15Smrg
4961.15Smrg	# Files that should not be owned by someone else or writeable.
4971.19Smycroft	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
4981.79Selric	      .cshrc .emacs .exrc .forward .history .k5login .klogin .login \
4991.79Selric	      .logout .profile .qmail .rc_history .rhosts .shosts ssh .tcshrc \
5001.79Selric	      .twmrc .xinitrc .xsession .ssh/authorized_keys \
5011.79Selric	      .ssh/authorized_keys2 .ssh/config .ssh/id_dsa.pub \
5021.79Selric	      .ssh/id_rsa.pub .ssh/identity.pub .ssh/known_hosts \
5031.79Selric	      .ssh/known_hosts2"
5041.15Smrg	while read uid homedir; do
5051.15Smrg		for f in $list ; do
5061.15Smrg			file=${homedir}/${f}
5071.15Smrg			if [ -f $file ] ; then
5081.46Schristos				printf -- "$uid $f `ls -ldgT $file`\n"
5091.15Smrg			fi
5101.15Smrg		done
5111.29Slukem	done < $MPBYPATH |
5121.15Smrg	awk '$1 != $5 && $5 != "root" \
5131.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
5141.15Smrg	     $3 ~ /^-....w/ \
5151.15Smrg		{ print "user " $1 " " $2 " file is group writeable" }
5161.15Smrg	     $3 ~ /^-.......w/ \
5171.27Slukem		{ print "user " $1 " " $2 " file is other writeable" }' \
5181.27Slukem	    >> $OUTPUT
5191.15Smrg	if [ -s $OUTPUT ] ; then
5201.15Smrg		printf "\nChecking dot files.\n"
5211.15Smrg		cat $OUTPUT
5221.15Smrg	fi
5231.9Scgdfi
5241.9Scgd
5251.9Scgd# Mailboxes should be owned by user and unreadable.
5261.32Slukem#
5271.31Slukemif checkyesno check_varmail; then
5281.63Slukem	ls -l /var/mail | \
5291.63Slukem	awk '	NR == 1 { next; }
5301.63Slukem	    	$3 != $9 {
5311.63Slukem			print "user " $9 " mailbox is owned by " $3
5321.63Slukem		}
5331.63Slukem		$1 != "-rw-------" {
5341.63Slukem			print "user " $9 " mailbox is " $1 ", group " $4
5351.63Slukem		}' > $OUTPUT
5361.15Smrg	if [ -s $OUTPUT ] ; then
5371.15Smrg		printf "\nChecking mailbox ownership.\n"
5381.15Smrg		cat $OUTPUT
5391.15Smrg	fi
5401.15Smrgfi
5411.15Smrg
5421.32Slukem# NFS exports shouldn't be globally exported
5431.32Slukem#
5441.32Slukemif checkyesno check_nfs && [ -f /etc/exports ]; then
5451.32Slukem	awk '{
5461.22Slukem		# ignore comments and blank lines
5471.39Shubertf		if ($0 ~ /^\#/ || $0 ~ /^$/ )
5481.22Slukem			next;
5491.22Slukem
5501.15Smrg		readonly = 0;
5511.15Smrg		for (i = 2; i <= NF; ++i) {
5521.15Smrg			if ($i ~ /-ro/)
5531.15Smrg				readonly = 1;
5541.15Smrg			else if ($i !~ /^-/)
5551.15Smrg				next;
5561.15Smrg		}
5571.15Smrg		if (readonly)
5581.15Smrg			print "File system " $1 " globally exported, read-only."
5591.15Smrg		else
5601.15Smrg			print "File system " $1 " globally exported, read-write."
5611.32Slukem	}' < /etc/exports > $OUTPUT
5621.32Slukem	if [ -s $OUTPUT ] ; then
5631.15Smrg		printf "\nChecking for globally exported file systems.\n"
5641.15Smrg		cat $OUTPUT
5651.15Smrg	fi
5661.9Scgdfi
5671.9Scgd
5681.9Scgd# Display any changes in setuid files and devices.
5691.32Slukem#
5701.31Slukemif checkyesno check_devices; then
5711.28Slukem	> $ERR
5721.15Smrg	(find / \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \
5731.74Slukem			-o -fstype null \
5741.15Smrg			-o -fstype procfs \) -a -prune -o \
5751.21Smycroft	    \( \( -perm -u+s -a ! -type d \) -o \
5761.21Smycroft	       \( -perm -g+s -a ! -type d \) -o \
5771.24Slukem	       -type b -o -type c \) -print0 | \
5781.24Slukem	xargs -0 ls -ldgTq | sort +9 > $LIST) 2> $OUTPUT
5791.15Smrg
5801.15Smrg	# Display any errors that occurred during system file walk.
5811.15Smrg	if [ -s $OUTPUT ] ; then
5821.28Slukem		printf "Setuid/device find errors:\n" >> $ERR
5831.28Slukem		cat $OUTPUT >> $ERR
5841.28Slukem		printf "\n" >> $ERR
5851.15Smrg	fi
5861.15Smrg
5871.15Smrg	# Display any changes in the setuid file list.
5881.15Smrg	egrep -v '^[bc]' $LIST > $TMP1
5891.15Smrg	if [ -s $TMP1 ] ; then
5901.15Smrg		# Check to make sure uudecode isn't setuid.
5911.15Smrg		if grep -w uudecode $TMP1 > /dev/null ; then
5921.28Slukem			printf "\nUudecode is setuid.\n" >> $ERR
5931.15Smrg		fi
5941.15Smrg
5951.67Slukem		file=$work_dir/setuid
5961.67Slukem		migrate_file "$backup_dir/setuid" "$file"
5971.67Slukem		CUR=${file}.current
5981.67Slukem		BACK=${file}.backup
5991.15Smrg		if [ -s $CUR ] ; then
6001.15Smrg			if cmp -s $CUR $TMP1 ; then
6011.15Smrg				:
6021.15Smrg			else
6031.15Smrg				> $TMP2
6041.15Smrg				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
6051.15Smrg				if [ -s $OUTPUT ] ; then
6061.28Slukem					printf "Setuid additions:\n" >> $ERR
6071.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6081.28Slukem					printf "\n" >> $ERR
6091.15Smrg				fi
6101.15Smrg
6111.15Smrg				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
6121.15Smrg				if [ -s $OUTPUT ] ; then
6131.28Slukem					printf "Setuid deletions:\n" >> $ERR
6141.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6151.28Slukem					printf "\n" >> $ERR
6161.15Smrg				fi
6171.15Smrg
6181.20Smycroft				sort -k10 $TMP2 $CUR $TMP1 | \
6191.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
6201.27Slukem				    uniq -u > $OUTPUT
6211.15Smrg				if [ -s $OUTPUT ] ; then
6221.28Slukem					printf "Setuid changes:\n" >> $ERR
6231.28Slukem					column -t $OUTPUT >> $ERR
6241.28Slukem					printf "\n" >> $ERR
6251.15Smrg				fi
6261.9Scgd
6271.52Satatat				backup_file update $TMP1 $CUR $BACK
6281.9Scgd			fi
6291.15Smrg		else
6301.28Slukem			printf "Setuid additions:\n" >> $ERR
6311.28Slukem			column -t $TMP1 >> $ERR
6321.28Slukem			printf "\n" >> $ERR
6331.52Satatat			backup_file add $TMP1 $CUR $BACK
6341.9Scgd		fi
6351.15Smrg	fi
6361.15Smrg
6371.27Slukem	# Check for block and character disk devices that are readable or
6381.27Slukem	# writeable or not owned by root.operator.
6391.15Smrg	>$TMP1
6401.61Slukem	DISKLIST="ccd ch hk hp ld md ra raid rb rd rl rx \
6411.57Ssimonb	    sd se ss uk up vnd wd xd xy"
6421.27Slukem#	DISKLIST="$DISKLIST ct mt st wt"
6431.15Smrg	for i in $DISKLIST; do
6441.15Smrg		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
6451.15Smrg		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
6461.15Smrg	done
6471.15Smrg
6481.15Smrg	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
6491.25Slukem		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
6501.25Slukem		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
6511.15Smrg	if [ -s $OUTPUT ] ; then
6521.28Slukem		printf "\nChecking disk ownership and permissions.\n" >> $ERR
6531.28Slukem		cat $OUTPUT >> $ERR
6541.28Slukem		printf "\n" >> $ERR
6551.9Scgd	fi
6561.9Scgd
6571.15Smrg	# Display any changes in the device file list.
6581.20Smycroft	egrep '^[bc]' $LIST | sort -k11 > $TMP1
6591.15Smrg	if [ -s $TMP1 ] ; then
6601.67Slukem		file=$work_dir/device
6611.67Slukem		migrate_file "$backup_dir/device" "$file"
6621.67Slukem		CUR=${file}.current
6631.67Slukem		BACK=${file}.backup
6641.15Smrg
6651.15Smrg		if [ -s $CUR ] ; then
6661.15Smrg			if cmp -s $CUR $TMP1 ; then
6671.15Smrg				:
6681.15Smrg			else
6691.15Smrg				> $TMP2
6701.15Smrg				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
6711.15Smrg				if [ -s $OUTPUT ] ; then
6721.28Slukem					printf "Device additions:\n" >> $ERR
6731.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6741.28Slukem					printf "\n" >> $ERR
6751.15Smrg				fi
6761.15Smrg
6771.15Smrg				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
6781.15Smrg				if [ -s $OUTPUT ] ; then
6791.28Slukem					printf "Device deletions:\n" >> $ERR
6801.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6811.28Slukem					printf "\n" >> $ERR
6821.15Smrg				fi
6831.15Smrg
6841.27Slukem				# Report any block device change. Ignore
6851.27Slukem				# character devices, only the name is
6861.27Slukem				# significant.
6871.15Smrg				cat $TMP2 $CUR $TMP1 | \
6881.27Slukem				    sed -e '/^c/d' | \
6891.27Slukem				    sort -k11 | \
6901.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
6911.27Slukem				    uniq -u > $OUTPUT
6921.15Smrg				if [ -s $OUTPUT ] ; then
6931.28Slukem					printf "Block device changes:\n" >> $ERR
6941.28Slukem					column -t $OUTPUT >> $ERR
6951.28Slukem					printf "\n" >> $ERR
6961.15Smrg				fi
6971.9Scgd
6981.52Satatat				backup_file update $TMP1 $CUR $BACK
6991.9Scgd			fi
7001.15Smrg		else
7011.28Slukem			printf "Device additions:\n" >> $ERR
7021.28Slukem			column -t $TMP1 >> $ERR
7031.28Slukem			printf "\n" >> $ERR
7041.52Satatat			backup_file add $TMP1 $CUR $BACK >> $ERR
7051.9Scgd		fi
7061.28Slukem	fi
7071.28Slukem	if [ -s $ERR ] ; then
7081.28Slukem		printf "\nChecking setuid files and devices:\n"
7091.28Slukem		cat $ERR
7101.28Slukem		printf "\n"
7111.9Scgd	fi
7121.9Scgdfi
7131.9Scgd
7141.9Scgd# Check special files.
7151.9Scgd# Check system binaries.
7161.9Scgd#
7171.9Scgd# Create the mtree tree specifications using:
7181.67Slukem#	mtree -cx -pDIR -kmd5,uid,gid,mode,nlink,size,link,time > DIR.secure
7191.38Skleink#	chown root:wheel DIR.secure
7201.67Slukem#	chmod u+r,go= DIR.secure
7211.9Scgd#
7221.9Scgd# Note, this is not complete protection against Trojan horsed binaries, as
7231.9Scgd# the hacker can modify the tree specification to match the replaced binary.
7241.9Scgd# For details on really protecting yourself against modified binaries, see
7251.9Scgd# the mtree(8) manual page.
7261.32Slukem#
7271.31Slukemif checkyesno check_mtree; then
7281.67Slukem	for file in $special_files; do
7291.67Slukem		[ ! -s $file ] && continue
7301.67Slukem		mtree -e -l -p / -f $file
7311.67Slukem	done > $OUTPUT
7321.15Smrg	if [ -s $OUTPUT ]; then
7331.9Scgd		printf "\nChecking special files and directories.\n"
7341.9Scgd		cat $OUTPUT
7351.9Scgd	fi
7361.9Scgd
7371.16Smikel	for file in /etc/mtree/*.secure; do
7381.16Smikel		[ $file = '/etc/mtree/*.secure' ] && continue
7391.9Scgd		tree=`sed -n -e '3s/.* //p' -e 3q $file`
7401.9Scgd		mtree -f $file -p $tree > $TMP1
7411.9Scgd		if [ -s $TMP1 ]; then
7421.67Slukem			printf "\nChecking $tree:\n"
7431.67Slukem			cat $TMP1
7441.9Scgd		fi
7451.67Slukem	done > $OUTPUT
7461.15Smrg	if [ -s $OUTPUT ]; then
7471.9Scgd		printf "\nChecking system binaries:\n"
7481.9Scgd		cat $OUTPUT
7491.9Scgd	fi
7501.9Scgdfi
7511.9Scgd
7521.32Slukem# Backup disklabels of available disks
7531.32Slukem#
7541.32Slukemif checkyesno check_disklabels; then
7551.67Slukem		# migrate old disklabels
7561.67Slukem	for file in `ls -1d $backup_dir/$backup_dir/disklabel.* \
7571.67Slukem	    $backup_dir/disklabel.* 2>/dev/null`; do
7581.67Slukem		migrate_file "$file" "$work_dir/${file##*/}"
7591.67Slukem	done
7601.67Slukem
7611.67Slukem		# generate list of old disklabels & fdisks and remove them
7621.67Slukem	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* 2>/dev/null |
7631.52Satatat	    egrep -v '\.(backup|current)(,v)?$' > $LABELS
7641.32Slukem	xargs rm < $LABELS
7651.32Slukem
7661.67Slukem		# generate disklabels of all disks excluding:	cd fd md
7671.63Slukem	disks=`iostat -x | awk 'NR > 1 && $1 !~ /^[cfm]d/ { print $1; }'`
7681.32Slukem	for i in $disks; do
7691.67Slukem		disklabel $i > "$work_dir/disklabel.$i" 2>/dev/null
7701.32Slukem	done
7711.32Slukem
7721.67Slukem		# if fdisk is available, generate fdisks for:	ed ld sd wd
7731.67Slukem	if [ -x /sbin/fdisk ]; then
7741.67Slukem		disks=`iostat -x| awk 'NR > 1 && $1 ~ /^[elsw]d/ { print $1; }'`
7751.67Slukem		for i in $disks; do
7761.67Slukem			/sbin/fdisk $i > "$work_dir/fdisk.$i" 2>/dev/null
7771.67Slukem		done
7781.67Slukem	fi
7791.67Slukem
7801.67Slukem		# append list of new disklabels and fdisks
7811.67Slukem	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* 2>/dev/null |
7821.52Satatat	    egrep -v '\.(backup|current)(,v)?$' >> $LABELS
7831.62Satatat	CHANGELIST="$LABELS $CHANGELIST"
7841.62Satatatfi
7851.62Satatat
7861.62Satatat# Check for changes in the list of installed pkgs
7871.62Satatat#
7881.65Slukemif checkyesno check_pkgs && [ -d $pkgdb_dir ]; then
7891.67Slukem	pkgs=$work_dir/pkgs
7901.67Slukem	migrate_file "$backup_dir/pkgs" "$pkgs"
7911.65Slukem	(	cd $pkgdb_dir
7921.62Satatat		pkg_info | sort
7931.62Satatat		echo ""
7941.62Satatat		find . \( -name +REQUIRED_BY -o -name +CONTENTS \) -print0 |
7951.72Slukem			xargs -0 ls -ldgTq | sort -t. +1 | sed -e 's, \./, ,'
7961.62Satatat	 ) > $pkgs
7971.67Slukem	echo "$pkgs" > $PKGS
7981.62Satatat	CHANGELIST="$PKGS $CHANGELIST"
7991.32Slukemfi
8001.32Slukem
8011.67Slukem# List of files that get backed up and checked for any modifications.
8021.9Scgd# Any changes cause the files to rotate.
8031.32Slukem#
8041.67Slukemif checkyesno check_changelist ; then
8051.67Slukem	for file in $special_files; do
8061.67Slukem		[ ! -s $file ] && continue
8071.67Slukem		mtree -D -k type -f $file -E exclude |
8081.67Slukem		    sed '/^type=file/!d ; s/type=file \.//'
8091.67Slukem	done > $CHANGEFILES
8101.67Slukem
8111.75Slukem	(
8121.68Slukem		# Add other files which might dynamically exist:
8131.67Slukem		#	/etc/ifconfig.*
8141.67Slukem		#	/etc/raid*.conf
8151.68Slukem		#	/etc/rc.d/*
8161.67Slukem		#	/etc/rc.conf.d/*
8171.68Slukem		#
8181.75Slukem		echo "/etc/ifconfig.*"
8191.75Slukem		echo "/etc/raid*.conf"
8201.75Slukem		echo "/etc/rc.d/*"
8211.75Slukem		echo "/etc/rc.conf.d/*"
8221.67Slukem
8231.68Slukem		# Add /etc/changelist
8241.68Slukem		#
8251.75Slukem		if [ -s /etc/changelist ]; then
8261.75Slukem			grep -v '^#' /etc/changelist
8271.75Slukem		fi
8281.75Slukem	) | while read file; do
8291.75Slukem		case "$file" in
8301.75Slukem		*[\*\?\[]*)	# If changelist line is a glob ...
8311.75Slukem				# ... expand possible backup files
8321.75Slukem				#
8331.75Slukem			ls -1d $(echo $backup_dir/${file}.current) 2>/dev/null \
8341.75Slukem			    | sed "s,^$backup_dir/,, ; s,\.current$,,"
8351.75Slukem				
8361.75Slukem				# ... expand possible files
8371.75Slukem				#
8381.75Slukem			ls -1d $(echo $file) 2>/dev/null
8391.75Slukem			;;
8401.75Slukem		*)
8411.75Slukem				# Otherwise, just print the filename
8421.75Slukem			echo $file
8431.75Slukem			;;
8441.75Slukem		esac
8451.75Slukem	done >> $CHANGEFILES
8461.67Slukem	CHANGELIST="$CHANGEFILES $CHANGELIST"
8471.67Slukemfi
8481.67Slukem
8491.67Slukem# Special case backups, including the master password file and
8501.67Slukem# ssh private host keys. The normal backup mechanisms for
8511.67Slukem# $check_changelist (see below) also print out the actual file
8521.67Slukem# differences and we don't want to do that for these files
8531.67Slukem#
8541.67Slukemecho $MP > $TMP1			# always add /etc/master.passwd
8551.67Slukemfor file in $special_files; do
8561.67Slukem	[ ! -s $file ] && continue
8571.70Slukem	mtree -D -k type -f $file -I nodiff |
8581.67Slukem	    sed '/^type=file/!d ; s/type=file \.//'
8591.67Slukemdone >> $TMP1
8601.73Slukemgrep -v '^$' $TMP1 | sort -u > $TMP2
8611.68Slukem
8621.69Slukemwhile read file; do
8631.67Slukem	backup_and_diff "$file" no
8641.69Slukemdone < $TMP2
8651.67Slukem
8661.32Slukem
8671.32Slukemif [ -n "$CHANGELIST" ]; then
8681.73Slukem	grep -h -v '^$' $CHANGELIST | sort -u > $TMP1
8691.68Slukem	comm -23 $TMP1 $TMP2 | while read file; do
8701.67Slukem		backup_and_diff "$file" yes
8711.9Scgd	done
8721.44Sadfi
8731.44Sad
8741.44Sadif [ -f /etc/security.local ]; then
8751.44Sad	echo ""
8761.44Sad	echo "Running /etc/security.local:"
8771.44Sad	. /etc/security.local
8781.9Scgdfi
879