security revision 1.64
11.1Scgd#!/bin/sh -
21.1Scgd#
31.64Scjs#	$NetBSD: security,v 1.64 2001/10/03 07:04:32 cjs 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.56SlukemSECUREDIR=`mktemp -d /tmp/_securedir.XXXXXX` || exit 1
241.56Slukem
251.56Slukemtrap "/bin/rm -rf $SECUREDIR ; exit 0" EXIT INT QUIT
261.15Smrg
271.56Slukemif ! cd "$SECUREDIR"; then
281.56Slukem	echo "Can not cd to $SECUREDIR".
291.15Smrg	exit 1
301.15Smrgfi
311.15Smrg
321.34Sabsif [ -z "$max_loginlen" ];then
331.56Slukem	max_loginlen=8
341.34Sabsfi
351.34Sabs
361.49Sjdolecekif [ -z "$max_grouplen" ]; then
371.56Slukem	max_grouplen=8
381.49Sjdolecekfi
391.49Sjdolecek
401.15SmrgERR=secure1.$$
411.15SmrgTMP1=secure2.$$
421.15SmrgTMP2=secure3.$$
431.28SlukemMPBYUID=secure4.$$
441.29SlukemMPBYPATH=secure5.$$
451.27SlukemLIST=secure6.$$
461.27SlukemOUTPUT=secure7.$$
471.32SlukemLABELS=secure8.$$
481.62SatatatPKGS=secure9.$$
491.15Smrg
501.48Sabs# Handle backup_dir not being set in .conf file
511.48Sabsbackup_dir=${backup_dir:-/var/backups}
521.62SatatatCHANGELIST=""
531.62Satatatpkg_dbdir=${pkg_dbdir:-/var/db/pkg}
541.48Sabs
551.15SmrgMP=/etc/master.passwd
561.9Scgd
571.27Slukem# these is used several times.
581.34Sabsawk -F: '!/^+/ { print $1 " " $3 }' $MP | sort -k2n > $MPBYUID
591.29Slukemawk -F: '{ print $1 " " $9 }' $MP | sort -k2 > $MPBYPATH
601.9Scgd
611.9Scgd# Check the master password file syntax.
621.32Slukem#
631.31Slukemif checkyesno check_passwd; then
641.34Sabs	awk -v "len=$max_loginlen" '
651.25Slukem	BEGIN {
661.25Slukem		while ( getline < "/etc/shells" > 0 ) {
671.39Shubertf			if ($0 ~ /^\#/ || $0 ~ /^$/ )
681.25Slukem				continue;
691.25Slukem			shells[$1]++;
701.25Slukem		}
711.25Slukem		FS=":";
721.25Slukem	}
731.25Slukem
741.25Slukem	{
751.15Smrg		if ($0 ~ /^[	 ]*$/) {
761.25Slukem			printf "Line %d is a blank line.\n", NR;
771.15Smrg			next;
781.15Smrg		}
791.34Sabs		if (NF != 10 && ($1 != "+" || NF != 1))
801.25Slukem			printf "Line %d has the wrong number of fields.\n", NR;
811.34Sabs		if ($1 == "+" )  {
821.34Sabs			if (NF != 1 && $3 == 0)
831.34Sabs			    printf "Line %d includes entries with uid 0.\n", NR;
841.34Sabs			next;
851.34Sabs		}
861.53Satatat		if ($1 !~ /^[A-Za-z0-9]([-A-Za-z0-9]*[A-Za-z0-9])*$/)
871.25Slukem			printf "Login %s has non-alphanumeric characters.\n",
881.25Slukem			    $1;
891.34Sabs		if (length($1) > len)
901.34Sabs			printf "Login %s has more than "len" characters.\n", $1;
911.15Smrg		if ($2 == "")
921.25Slukem			printf "Login %s has no password.\n", $1;
931.59Sperry		if (length($2) != 13 && 
941.59Sperry		    length($2) != 20 && 
951.59Sperry		    length($2) != 34 &&
961.59Sperry		    $2 != "" &&
971.59Sperry		    $2 !~ /^\*[A-z-]+$/ &&
981.59Sperry		    $1 != "toor") {
991.25Slukem			if ($10 == "" || shells[$10])
1001.27Slukem		    printf "Login %s is off but still has a valid shell (%s)\n",
1011.25Slukem				    $1, $10;
1021.25Slukem		} else if (! shells[$10])
1031.25Slukem			printf "Login %s does not have a valid shell (%s)\n",
1041.25Slukem			    $1, $10;
1051.15Smrg		if ($3 == 0 && $1 != "root" && $1 != "toor")
1061.25Slukem			printf "Login %s has a user id of 0.\n", $1;
1071.15Smrg		if ($3 < 0)
1081.25Slukem			printf "Login %s has a negative user id.\n", $1;
1091.15Smrg		if ($4 < 0)
1101.25Slukem			printf "Login %s has a negative group id.\n", $1;
1111.15Smrg	}' < $MP > $OUTPUT
1121.15Smrg	if [ -s $OUTPUT ] ; then
1131.15Smrg		printf "\nChecking the $MP file:\n"
1141.15Smrg		cat $OUTPUT
1151.15Smrg	fi
1161.15Smrg
1171.15Smrg	awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
1181.15Smrg	if [ -s $OUTPUT ] ; then
1191.15Smrg		printf "\n$MP has duplicate user names.\n"
1201.15Smrg		column $OUTPUT
1211.15Smrg	fi
1221.15Smrg
1231.37Swrstuden# To not exclude 'toor', a standard duplicate root account, from the duplicate
1241.37Swrstuden# account test, uncomment the line below (without egrep in it)and comment
1251.37Swrstuden# out the line (with egrep in it) below it.
1261.37Swrstuden#
1271.37Swrstuden#	< $MPBYUID uniq -d -f 1 | awk '{ print $2 }' > $TMP2
1281.36Swrstuden	< $MPBYUID egrep -v '^toor ' | uniq -d -f 1 | awk '{ print $2 }' > $TMP2
1291.15Smrg	if [ -s $TMP2 ] ; then
1301.15Smrg		printf "\n$MP has duplicate user id's.\n"
1311.15Smrg		while read uid; do
1321.28Slukem			grep -w $uid $MPBYUID
1331.15Smrg		done < $TMP2 | column
1341.15Smrg	fi
1351.9Scgdfi
1361.9Scgd
1371.9Scgd# Backup the master password file; a special case, the normal backup
1381.9Scgd# mechanisms also print out file differences and we don't want to do
1391.9Scgd# that because this file has encrypted passwords in it.
1401.32Slukem#
1411.48SabsCUR=$backup_dir/${MP##*/}.current
1421.48SabsBACK=$backup_dir/${MP##*/}.backup
1431.9Scgdif [ -s $CUR ] ; then
1441.9Scgd	if cmp -s $CUR $MP; then
1451.9Scgd		:
1461.9Scgd	else
1471.52Satatat		backup_file update $MP $CUR $BACK
1481.9Scgd	fi
1491.9Scgdelse
1501.52Satatat	backup_file add $MP $CUR $BACK
1511.9Scgdfi
1521.9Scgd
1531.9Scgd# Check the group file syntax.
1541.32Slukem#
1551.31Slukemif checkyesno check_group; then
1561.15Smrg	GRP=/etc/group
1571.49Sjdolecek	awk -F: -v "len=$max_grouplen" '{
1581.15Smrg		if ($0 ~ /^[	 ]*$/) {
1591.25Slukem			printf "Line %d is a blank line.\n", NR;
1601.15Smrg			next;
1611.15Smrg		}
1621.34Sabs		if (NF != 4 && ($1 != "+" || NF != 1))
1631.25Slukem			printf "Line %d has the wrong number of fields.\n", NR;
1641.34Sabs		if ($1 == "+" )  {
1651.34Sabs			next;
1661.34Sabs		}
1671.53Satatat		if ($1 !~ /^[A-Za-z0-9]([-A-Za-z0-9]*[A-Za-z0-9])*$/)
1681.25Slukem			printf "Group %s has non-alphanumeric characters.\n",
1691.25Slukem			    $1;
1701.49Sjdolecek		if (length($1) > len)
1711.49Sjdolecek			printf "Group %s has more than "len" characters.\n", $1;
1721.15Smrg		if ($3 !~ /[0-9]*/)
1731.25Slukem			printf "Login %s has a negative group id.\n", $1;
1741.15Smrg	}' < $GRP > $OUTPUT
1751.15Smrg	if [ -s $OUTPUT ] ; then
1761.15Smrg		printf "\nChecking the $GRP file:\n"
1771.15Smrg		cat $OUTPUT
1781.15Smrg	fi
1791.15Smrg
1801.15Smrg	awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
1811.15Smrg	if [ -s $OUTPUT ] ; then
1821.15Smrg		printf "\n$GRP has duplicate group names.\n"
1831.15Smrg		column $OUTPUT
1841.15Smrg	fi
1851.9Scgdfi
1861.9Scgd
1871.9Scgd# Check for root paths, umask values in startup files.
1881.9Scgd# The check for the root paths is problematical -- it's likely to fail
1891.9Scgd# in other environments.  Once the shells have been modified to warn
1901.9Scgd# of '.' in the path, the path tests should go away.
1911.32Slukem#
1921.31Slukemif checkyesno check_rootdotfiles; then
1931.28Slukem	> $OUTPUT
1941.15Smrg	rhome=`csh -fc "echo ~root"`
1951.15Smrg	umaskset=no
1961.15Smrg	list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
1971.15Smrg	for i in $list ; do
1981.15Smrg		if [ -f $i ] ; then
1991.45Ssommerfe			if egrep '^[ \t]*umask[ \t]+[0-7]+' $i > /dev/null ; then
2001.15Smrg				umaskset=yes
2011.15Smrg			fi
2021.63Slukem			# Double check the umask value itself; ensure that
2031.63Slukem			# both the 020 and 002 bits are set.
2041.63Slukem			# We handle this in decimal initially to extract the
2051.63Slukem			# digits, and then extract the `2' bit of each digit.
2061.63Slukem			# This is made especially painful because 
2071.45Ssommerfe			# bitwise operations were left out of awk.
2081.45Ssommerfe			egrep '^[ \t]*umask[ \t]+[0-7]+' $i |
2091.63Slukem			awk '{
2101.63Slukem				g= ($2 % 100) - ($2 % 10);
2111.63Slukem				g /= 10;
2121.63Slukem				g = g % 4;
2131.63Slukem				g -= g % 2;
2141.63Slukem				if (g != 2) {
2151.63Slukem					print "\tRoot umask is group writeable"
2161.63Slukem				}
2171.63Slukem				o = ($2 % 10);
2181.63Slukem				o = o % 4;
2191.63Slukem				o -= o % 2;
2201.63Slukem				if (o != 2) {
2211.63Slukem					print "\tRoot umask is other writeable"
2221.63Slukem			    	}
2231.63Slukem			    }' | sort -u >> $OUTPUT
2241.26Slukem			SAVE_PATH=$PATH
2251.26Slukem			unset PATH
2261.15Smrg			/bin/csh -f -s << end-of-csh > /dev/null 2>&1
2271.15Smrg				source $i
2281.15Smrg				/bin/ls -ldgT \$path > $TMP1
2291.9Scgdend-of-csh
2301.26Slukem			PATH=$SAVE_PATH
2311.15Smrg			awk '{
2321.15Smrg				if ($10 ~ /^\.$/) {
2331.27Slukem					print "\tThe root path includes .";
2341.15Smrg					next;
2351.15Smrg				}
2361.15Smrg			     }
2371.15Smrg			     $1 ~ /^d....w/ \
2381.27Slukem		{ print "\tRoot path directory " $10 " is group writeable." } \
2391.15Smrg			     $1 ~ /^d.......w/ \
2401.27Slukem		{ print "\tRoot path directory " $10 " is other writeable." }' \
2411.15Smrg			< $TMP1 >> $OUTPUT
2421.15Smrg		fi
2431.15Smrg	done
2441.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
2451.27Slukem		printf "\nChecking root csh paths, umask values:\n$list\n\n"
2461.15Smrg		if [ -s $OUTPUT ]; then
2471.15Smrg			cat $OUTPUT
2481.15Smrg		fi
2491.15Smrg		if [ $umaskset = "no" ] ; then
2501.27Slukem		    printf "\tRoot csh startup files do not set the umask.\n"
2511.15Smrg		fi
2521.9Scgd	fi
2531.9Scgd
2541.28Slukem	> $OUTPUT
2551.15Smrg	rhome=/root
2561.15Smrg	umaskset=no
2571.23Slukem	list="/etc/profile ${rhome}/.profile"
2581.15Smrg	for i in $list; do
2591.15Smrg		if [ -f $i ] ; then
2601.15Smrg			if egrep umask $i > /dev/null ; then
2611.15Smrg				umaskset=yes
2621.15Smrg			fi
2631.15Smrg			egrep umask $i |
2641.15Smrg			awk '$2 % 100 < 20 \
2651.27Slukem				{ print "\tRoot umask is group writeable" } \
2661.15Smrg			     $2 % 10 < 2 \
2671.27Slukem				{ print "\tRoot umask is other writeable" }' \
2681.27Slukem			    >> $OUTPUT
2691.26Slukem			SAVE_PATH=$PATH
2701.26Slukem			unset PATH
2711.15Smrg			/bin/sh << end-of-sh > /dev/null 2>&1
2721.15Smrg				. $i
2731.26Slukem				list=\`echo \$PATH | /usr/bin/sed -e \
2741.26Slukem				    's/^:/.:/;s/:$/:./;s/::/:.:/g;s/:/ /g'\`
2751.15Smrg				/bin/ls -ldgT \$list > $TMP1
2761.9Scgdend-of-sh
2771.26Slukem			PATH=$SAVE_PATH
2781.15Smrg			awk '{
2791.15Smrg				if ($10 ~ /^\.$/) {
2801.27Slukem					print "\tThe root path includes .";
2811.15Smrg					next;
2821.15Smrg				}
2831.15Smrg			     }
2841.15Smrg			     $1 ~ /^d....w/ \
2851.27Slukem		{ print "\tRoot path directory " $10 " is group writeable." } \
2861.15Smrg			     $1 ~ /^d.......w/ \
2871.27Slukem		{ print "\tRoot path directory " $10 " is other writeable." }' \
2881.15Smrg			< $TMP1 >> $OUTPUT
2891.9Scgd
2901.15Smrg		fi
2911.15Smrg	done
2921.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
2931.15Smrg		printf "\nChecking root sh paths, umask values:\n$list\n"
2941.15Smrg		if [ -s $OUTPUT ]; then
2951.15Smrg			cat $OUTPUT
2961.15Smrg		fi
2971.15Smrg		if [ $umaskset = "no" ] ; then
2981.27Slukem			printf "\tRoot sh startup files do not set the umask.\n"
2991.15Smrg		fi
3001.9Scgd	fi
3011.9Scgdfi
3021.9Scgd
3031.9Scgd# Root and uucp should both be in /etc/ftpusers.
3041.32Slukem#
3051.31Slukemif checkyesno check_ftpusers; then
3061.28Slukem	> $OUTPUT
3071.28Slukem	list="uucp "`awk '$2 == 0 { print $1 }' $MPBYUID`
3081.27Slukem	for i in $list; do
3091.29Slukem		if /usr/libexec/ftpd -C $i ; then
3101.29Slukem			printf "\t$i is not denied\n" >> $OUTPUT
3111.27Slukem		fi
3121.27Slukem	done
3131.28Slukem	if [ -s $OUTPUT ]; then
3141.28Slukem		printf "\nChecking the /etc/ftpusers configuration:\n"
3151.28Slukem		cat $OUTPUT
3161.28Slukem	fi
3171.9Scgdfi
3181.9Scgd
3191.43Sitojun# Uudecode should not be in the /etc/mail/aliases file.
3201.32Slukem#
3211.31Slukemif checkyesno check_aliases; then
3221.43Sitojun	for f in /etc/mail/aliases /etc/aliases; do
3231.43Sitojun		if [ -f $f ] && egrep '^[^#]*(uudecode|decode).*\|' $f; then
3241.43Sitojun			printf "\nEntry for uudecode in $f file.\n"
3251.43Sitojun		fi
3261.43Sitojun	done
3271.9Scgdfi
3281.9Scgd
3291.9Scgd# Files that should not have + signs.
3301.32Slukem#
3311.31Slukemif checkyesno check_rhosts; then
3321.15Smrg	list="/etc/hosts.equiv /etc/hosts.lpd"
3331.15Smrg	for f in $list ; do
3341.15Smrg		if [ -f $f ] && egrep '\+' $f > /dev/null ; then
3351.15Smrg			printf "\nPlus sign in $f file.\n"
3361.15Smrg		fi
3371.15Smrg	done
3381.15Smrg
3391.15Smrg	# Check for special users with .rhosts files.  Only root and toor should
3401.16Smikel	# have .rhosts files.  Also, .rhosts files should not have plus signs.
3411.15Smrg	awk -F: '$1 != "root" && $1 != "toor" && \
3421.15Smrg		($3 < 100 || $1 == "ftp" || $1 == "uucp") \
3431.20Smycroft			{ print $1 " " $9 }' $MP |
3441.19Smycroft	sort -k2 |
3451.15Smrg	while read uid homedir; do
3461.15Smrg		if [ -f ${homedir}/.rhosts ] ; then
3471.15Smrg			rhost=`ls -ldgT ${homedir}/.rhosts`
3481.46Schristos			printf -- "$uid: $rhost\n"
3491.15Smrg		fi
3501.15Smrg	done > $OUTPUT
3511.15Smrg	if [ -s $OUTPUT ] ; then
3521.15Smrg		printf "\nChecking for special users with .rhosts files.\n"
3531.15Smrg		cat $OUTPUT
3541.15Smrg	fi
3551.15Smrg
3561.15Smrg	while read uid homedir; do
3571.35Sfair		if [ -f ${homedir}/.rhosts -a -r ${homedir}/.rhosts ] && \
3581.41Schristos		    cat -f ${homedir}/.rhosts | egrep '\+' > /dev/null ; then
3591.46Schristos			printf -- "$uid: + in .rhosts file.\n"
3601.15Smrg		fi
3611.29Slukem	done < $MPBYPATH > $OUTPUT
3621.15Smrg	if [ -s $OUTPUT ] ; then
3631.15Smrg		printf "\nChecking .rhosts files syntax.\n"
3641.15Smrg		cat $OUTPUT
3651.15Smrg	fi
3661.9Scgdfi
3671.9Scgd
3681.9Scgd# Check home directories.  Directories should not be owned by someone else
3691.9Scgd# or writeable.
3701.32Slukem#
3711.31Slukemif checkyesno check_homes; then
3721.15Smrg	while read uid homedir; do
3731.15Smrg		if [ -d ${homedir}/ ] ; then
3741.15Smrg			file=`ls -ldgT ${homedir}`
3751.46Schristos			printf -- "$uid $file\n"
3761.9Scgd		fi
3771.29Slukem	done < $MPBYPATH |
3781.15Smrg	awk '$1 != $4 && $4 != "root" \
3791.15Smrg		{ print "user " $1 " home directory is owned by " $4 }
3801.15Smrg	     $2 ~ /^-....w/ \
3811.15Smrg		{ print "user " $1 " home directory is group writeable" }
3821.15Smrg	     $2 ~ /^-.......w/ \
3831.27Slukem		{ print "user " $1 " home directory is other writeable" }' \
3841.27Slukem	    > $OUTPUT
3851.15Smrg	if [ -s $OUTPUT ] ; then
3861.15Smrg		printf "\nChecking home directories.\n"
3871.15Smrg		cat $OUTPUT
3881.15Smrg	fi
3891.15Smrg
3901.15Smrg	# Files that should not be owned by someone else or readable.
3911.27Slukem	list=".Xauthority .netrc"
3921.15Smrg	while read uid homedir; do
3931.15Smrg		for f in $list ; do
3941.15Smrg			file=${homedir}/${f}
3951.15Smrg			if [ -f $file ] ; then
3961.46Schristos				printf -- "$uid $f `ls -ldgT $file`\n"
3971.15Smrg			fi
3981.15Smrg		done
3991.29Slukem	done < $MPBYPATH |
4001.15Smrg	awk '$1 != $5 && $5 != "root" \
4011.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
4021.15Smrg	     $3 ~ /^-...r/ \
4031.15Smrg		{ print "user " $1 " " $2 " file is group readable" }
4041.15Smrg	     $3 ~ /^-......r/ \
4051.15Smrg		{ print "user " $1 " " $2 " file is other readable" }
4061.15Smrg	     $3 ~ /^-....w/ \
4071.15Smrg		{ print "user " $1 " " $2 " file is group writeable" }
4081.15Smrg	     $3 ~ /^-.......w/ \
4091.27Slukem		{ print "user " $1 " " $2 " file is other writeable" }' \
4101.27Slukem	    > $OUTPUT
4111.15Smrg
4121.15Smrg	# Files that should not be owned by someone else or writeable.
4131.19Smycroft	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
4141.19Smycroft	      .cshrc .emacs .exrc .forward .history .klogin .login .logout \
4151.27Slukem	      .profile .qmail .rc_history .rhosts .tcshrc .twmrc .xinitrc \
4161.27Slukem	      .xsession"
4171.15Smrg	while read uid homedir; do
4181.15Smrg		for f in $list ; do
4191.15Smrg			file=${homedir}/${f}
4201.15Smrg			if [ -f $file ] ; then
4211.46Schristos				printf -- "$uid $f `ls -ldgT $file`\n"
4221.15Smrg			fi
4231.15Smrg		done
4241.29Slukem	done < $MPBYPATH |
4251.15Smrg	awk '$1 != $5 && $5 != "root" \
4261.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
4271.15Smrg	     $3 ~ /^-....w/ \
4281.15Smrg		{ print "user " $1 " " $2 " file is group writeable" }
4291.15Smrg	     $3 ~ /^-.......w/ \
4301.27Slukem		{ print "user " $1 " " $2 " file is other writeable" }' \
4311.27Slukem	    >> $OUTPUT
4321.15Smrg	if [ -s $OUTPUT ] ; then
4331.15Smrg		printf "\nChecking dot files.\n"
4341.15Smrg		cat $OUTPUT
4351.15Smrg	fi
4361.9Scgdfi
4371.9Scgd
4381.9Scgd# Mailboxes should be owned by user and unreadable.
4391.32Slukem#
4401.31Slukemif checkyesno check_varmail; then
4411.63Slukem	ls -l /var/mail | \
4421.63Slukem	awk '	NR == 1 { next; }
4431.63Slukem	    	$3 != $9 {
4441.63Slukem			print "user " $9 " mailbox is owned by " $3
4451.63Slukem		}
4461.63Slukem		$1 != "-rw-------" {
4471.63Slukem			print "user " $9 " mailbox is " $1 ", group " $4
4481.63Slukem		}' > $OUTPUT
4491.15Smrg	if [ -s $OUTPUT ] ; then
4501.15Smrg		printf "\nChecking mailbox ownership.\n"
4511.15Smrg		cat $OUTPUT
4521.15Smrg	fi
4531.15Smrgfi
4541.15Smrg
4551.32Slukem# NFS exports shouldn't be globally exported
4561.32Slukem#
4571.32Slukemif checkyesno check_nfs && [ -f /etc/exports ]; then
4581.32Slukem	awk '{
4591.22Slukem		# ignore comments and blank lines
4601.39Shubertf		if ($0 ~ /^\#/ || $0 ~ /^$/ )
4611.22Slukem			next;
4621.22Slukem
4631.15Smrg		readonly = 0;
4641.15Smrg		for (i = 2; i <= NF; ++i) {
4651.15Smrg			if ($i ~ /-ro/)
4661.15Smrg				readonly = 1;
4671.15Smrg			else if ($i !~ /^-/)
4681.15Smrg				next;
4691.15Smrg		}
4701.15Smrg		if (readonly)
4711.15Smrg			print "File system " $1 " globally exported, read-only."
4721.15Smrg		else
4731.15Smrg			print "File system " $1 " globally exported, read-write."
4741.32Slukem	}' < /etc/exports > $OUTPUT
4751.32Slukem	if [ -s $OUTPUT ] ; then
4761.15Smrg		printf "\nChecking for globally exported file systems.\n"
4771.15Smrg		cat $OUTPUT
4781.15Smrg	fi
4791.9Scgdfi
4801.9Scgd
4811.9Scgd# Display any changes in setuid files and devices.
4821.32Slukem#
4831.31Slukemif checkyesno check_devices; then
4841.28Slukem	> $ERR
4851.15Smrg	(find / \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \
4861.15Smrg			-o -fstype procfs \) -a -prune -o \
4871.21Smycroft	    \( \( -perm -u+s -a ! -type d \) -o \
4881.21Smycroft	       \( -perm -g+s -a ! -type d \) -o \
4891.24Slukem	       -type b -o -type c \) -print0 | \
4901.24Slukem	xargs -0 ls -ldgTq | sort +9 > $LIST) 2> $OUTPUT
4911.15Smrg
4921.15Smrg	# Display any errors that occurred during system file walk.
4931.15Smrg	if [ -s $OUTPUT ] ; then
4941.28Slukem		printf "Setuid/device find errors:\n" >> $ERR
4951.28Slukem		cat $OUTPUT >> $ERR
4961.28Slukem		printf "\n" >> $ERR
4971.15Smrg	fi
4981.15Smrg
4991.15Smrg	# Display any changes in the setuid file list.
5001.15Smrg	egrep -v '^[bc]' $LIST > $TMP1
5011.15Smrg	if [ -s $TMP1 ] ; then
5021.15Smrg		# Check to make sure uudecode isn't setuid.
5031.15Smrg		if grep -w uudecode $TMP1 > /dev/null ; then
5041.28Slukem			printf "\nUudecode is setuid.\n" >> $ERR
5051.15Smrg		fi
5061.15Smrg
5071.48Sabs		CUR=$backup_dir/setuid.current
5081.48Sabs		BACK=$backup_dir/setuid.backup
5091.9Scgd
5101.15Smrg		if [ -s $CUR ] ; then
5111.15Smrg			if cmp -s $CUR $TMP1 ; then
5121.15Smrg				:
5131.15Smrg			else
5141.15Smrg				> $TMP2
5151.15Smrg				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
5161.15Smrg				if [ -s $OUTPUT ] ; then
5171.28Slukem					printf "Setuid additions:\n" >> $ERR
5181.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
5191.28Slukem					printf "\n" >> $ERR
5201.15Smrg				fi
5211.15Smrg
5221.15Smrg				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
5231.15Smrg				if [ -s $OUTPUT ] ; then
5241.28Slukem					printf "Setuid deletions:\n" >> $ERR
5251.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
5261.28Slukem					printf "\n" >> $ERR
5271.15Smrg				fi
5281.15Smrg
5291.20Smycroft				sort -k10 $TMP2 $CUR $TMP1 | \
5301.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
5311.27Slukem				    uniq -u > $OUTPUT
5321.15Smrg				if [ -s $OUTPUT ] ; then
5331.28Slukem					printf "Setuid changes:\n" >> $ERR
5341.28Slukem					column -t $OUTPUT >> $ERR
5351.28Slukem					printf "\n" >> $ERR
5361.15Smrg				fi
5371.9Scgd
5381.52Satatat				backup_file update $TMP1 $CUR $BACK
5391.9Scgd			fi
5401.15Smrg		else
5411.28Slukem			printf "Setuid additions:\n" >> $ERR
5421.28Slukem			column -t $TMP1 >> $ERR
5431.28Slukem			printf "\n" >> $ERR
5441.52Satatat			backup_file add $TMP1 $CUR $BACK
5451.9Scgd		fi
5461.15Smrg	fi
5471.15Smrg
5481.27Slukem	# Check for block and character disk devices that are readable or
5491.27Slukem	# writeable or not owned by root.operator.
5501.15Smrg	>$TMP1
5511.61Slukem	DISKLIST="ccd ch hk hp ld md ra raid rb rd rl rx \
5521.57Ssimonb	    sd se ss uk up vnd wd xd xy"
5531.27Slukem#	DISKLIST="$DISKLIST ct mt st wt"
5541.15Smrg	for i in $DISKLIST; do
5551.15Smrg		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
5561.15Smrg		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
5571.15Smrg	done
5581.15Smrg
5591.15Smrg	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
5601.25Slukem		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
5611.25Slukem		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
5621.15Smrg	if [ -s $OUTPUT ] ; then
5631.28Slukem		printf "\nChecking disk ownership and permissions.\n" >> $ERR
5641.28Slukem		cat $OUTPUT >> $ERR
5651.28Slukem		printf "\n" >> $ERR
5661.9Scgd	fi
5671.9Scgd
5681.15Smrg	# Display any changes in the device file list.
5691.20Smycroft	egrep '^[bc]' $LIST | sort -k11 > $TMP1
5701.15Smrg	if [ -s $TMP1 ] ; then
5711.48Sabs		CUR=$backup_dir/device.current
5721.48Sabs		BACK=$backup_dir/device.backup
5731.15Smrg
5741.15Smrg		if [ -s $CUR ] ; then
5751.15Smrg			if cmp -s $CUR $TMP1 ; then
5761.15Smrg				:
5771.15Smrg			else
5781.15Smrg				> $TMP2
5791.15Smrg				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
5801.15Smrg				if [ -s $OUTPUT ] ; then
5811.28Slukem					printf "Device additions:\n" >> $ERR
5821.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
5831.28Slukem					printf "\n" >> $ERR
5841.15Smrg				fi
5851.15Smrg
5861.15Smrg				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
5871.15Smrg				if [ -s $OUTPUT ] ; then
5881.28Slukem					printf "Device deletions:\n" >> $ERR
5891.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
5901.28Slukem					printf "\n" >> $ERR
5911.15Smrg				fi
5921.15Smrg
5931.27Slukem				# Report any block device change. Ignore
5941.27Slukem				# character devices, only the name is
5951.27Slukem				# significant.
5961.15Smrg				cat $TMP2 $CUR $TMP1 | \
5971.27Slukem				    sed -e '/^c/d' | \
5981.27Slukem				    sort -k11 | \
5991.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
6001.27Slukem				    uniq -u > $OUTPUT
6011.15Smrg				if [ -s $OUTPUT ] ; then
6021.28Slukem					printf "Block device changes:\n" >> $ERR
6031.28Slukem					column -t $OUTPUT >> $ERR
6041.28Slukem					printf "\n" >> $ERR
6051.15Smrg				fi
6061.9Scgd
6071.52Satatat				backup_file update $TMP1 $CUR $BACK
6081.9Scgd			fi
6091.15Smrg		else
6101.28Slukem			printf "Device additions:\n" >> $ERR
6111.28Slukem			column -t $TMP1 >> $ERR
6121.28Slukem			printf "\n" >> $ERR
6131.52Satatat			backup_file add $TMP1 $CUR $BACK >> $ERR
6141.9Scgd		fi
6151.28Slukem	fi
6161.28Slukem	if [ -s $ERR ] ; then
6171.28Slukem		printf "\nChecking setuid files and devices:\n"
6181.28Slukem		cat $ERR
6191.28Slukem		printf "\n"
6201.9Scgd	fi
6211.9Scgdfi
6221.9Scgd
6231.9Scgd# Check special files.
6241.9Scgd# Check system binaries.
6251.9Scgd#
6261.9Scgd# Create the mtree tree specifications using:
6271.9Scgd#
6281.9Scgd#	mtree -cx -pDIR -kcksum,gid,mode,nlink,size,link,time,uid > DIR.secure
6291.38Skleink#	chown root:wheel DIR.secure
6301.16Smikel#	chmod 600 DIR.secure
6311.9Scgd#
6321.9Scgd# Note, this is not complete protection against Trojan horsed binaries, as
6331.9Scgd# the hacker can modify the tree specification to match the replaced binary.
6341.9Scgd# For details on really protecting yourself against modified binaries, see
6351.9Scgd# the mtree(8) manual page.
6361.32Slukem#
6371.31Slukemif checkyesno check_mtree; then
6381.58Sperry	mtree -e -l -p / -f /etc/mtree/special > $OUTPUT
6391.15Smrg	if [ -s $OUTPUT ]; then
6401.9Scgd		printf "\nChecking special files and directories.\n"
6411.9Scgd		cat $OUTPUT
6421.9Scgd	fi
6431.9Scgd
6441.9Scgd	> $OUTPUT
6451.16Smikel	for file in /etc/mtree/*.secure; do
6461.16Smikel		[ $file = '/etc/mtree/*.secure' ] && continue
6471.9Scgd		tree=`sed -n -e '3s/.* //p' -e 3q $file`
6481.9Scgd		mtree -f $file -p $tree > $TMP1
6491.9Scgd		if [ -s $TMP1 ]; then
6501.9Scgd			printf "\nChecking $tree:\n" >> $OUTPUT
6511.9Scgd			cat $TMP1 >> $OUTPUT
6521.9Scgd		fi
6531.9Scgd	done
6541.15Smrg	if [ -s $OUTPUT ]; then
6551.9Scgd		printf "\nChecking system binaries:\n"
6561.9Scgd		cat $OUTPUT
6571.9Scgd	fi
6581.9Scgdfi
6591.9Scgd
6601.32Slukem# Backup disklabels of available disks
6611.32Slukem#
6621.32Slukemif checkyesno check_disklabels; then
6631.32Slukem		# generate list of old disklabels and remove them
6641.48Sabs	ls -1d $backup_dir/disklabel.* 2>/dev/null |
6651.52Satatat	    egrep -v '\.(backup|current)(,v)?$' > $LABELS
6661.32Slukem	xargs rm < $LABELS
6671.32Slukem
6681.63Slukem	disks=`iostat -x | awk 'NR > 1 && $1 !~ /^[cfm]d/ { print $1; }'`
6691.32Slukem	for i in $disks; do
6701.48Sabs		dlf="$backup_dir/disklabel.$i"
6711.32Slukem		disklabel $i > $dlf 2>/dev/null
6721.32Slukem	done
6731.32Slukem
6741.32Slukem		# append list of new disklabels, sort list
6751.48Sabs	ls -1d $backup_dir/disklabel.* 2>/dev/null |
6761.52Satatat	    egrep -v '\.(backup|current)(,v)?$' >> $LABELS
6771.32Slukem	sort -u -o $LABELS $LABELS
6781.62Satatat	CHANGELIST="$LABELS $CHANGELIST"
6791.62Satatatfi
6801.62Satatat
6811.62Satatat# Check for changes in the list of installed pkgs
6821.62Satatat#
6831.62Satatatif checkyesno check_pkgs && [ -d $pkg_dbdir ]; then
6841.62Satatat	pkgs=$backup_dir/pkgs
6851.62Satatat	(	cd $pkg_dbdir
6861.62Satatat		pkg_info | sort
6871.62Satatat		echo ""
6881.62Satatat		find . \( -name +REQUIRED_BY -o -name +CONTENTS \) -print0 |
6891.62Satatat			xargs -0 ls -l | sort -t. +1 | sed -e 's, \./, ,'
6901.62Satatat	 ) > $pkgs
6911.62Satatat	echo $pkgs > $PKGS
6921.62Satatat	CHANGELIST="$PKGS $CHANGELIST"
6931.32Slukemfi
6941.32Slukem
6951.9Scgd# List of files that get backed up and checked for any modifications.  Each
6961.48Sabs# file is expected to have two backups, $backup_dir/file.{current,backup}.
6971.9Scgd# Any changes cause the files to rotate.
6981.32Slukem#
6991.31Slukemif checkyesno check_changelist && [ -s /etc/changelist ] ; then
7001.32Slukem	CHANGELIST="/etc/changelist $CHANGELIST"
7011.32Slukemfi
7021.32Slukem
7031.32Slukemif [ -n "$CHANGELIST" ]; then
7041.32Slukem	for file in `egrep -hv "^#|$MP" $CHANGELIST`; do
7051.54Satatat		# old changelist backup names
7061.54Satatat		OCUR=$backup_dir/${file##*/}.current
7071.54Satatat		OBACK=$backup_dir/${file##*/}.backup
7081.54Satatat		# new changelist backup names
7091.54Satatat		CUR=$backup_dir$file.current
7101.54Satatat		BACK=$backup_dir$file.backup
7111.54Satatat		# roll over old backups
7121.54Satatat		if [ ! -d ${CUR%/*} ]; then
7131.54Satatat			mkdir -p ${CUR%/*}
7141.54Satatat		fi
7151.54Satatat		if [ -f $OCUR -a ! -f $CUR ]; then
7161.54Satatat			mv $OCUR $CUR
7171.54Satatat		fi
7181.54Satatat		if [ -f $OCUR,v -a ! -f $CUR,v ]; then
7191.54Satatat			mv $OCUR,v $CUR,v
7201.54Satatat		fi
7211.54Satatat		if [ -f $OBACK -a ! -f $BACK ]; then
7221.54Satatat			mv $OBACK $BACK
7231.54Satatat		fi
7241.54Satatat		# and down to work
7251.30Smycroft		if [ -f $file ]; then
7261.30Smycroft			if [ -f $CUR ] ; then
7271.9Scgd				diff $CUR $file > $OUTPUT
7281.9Scgd				if [ -s $OUTPUT ] ; then
7291.9Scgd		printf "\n======\n%s diffs (OLD < > NEW)\n======\n" $file
7301.9Scgd					cat $OUTPUT
7311.52Satatat					backup_file update $file $CUR $BACK
7321.9Scgd				fi
7331.9Scgd			else
7341.30Smycroft		printf "\n======\n%s added\n======\n" $file
7351.30Smycroft				diff /dev/null $file
7361.52Satatat				backup_file add $file $CUR $BACK
7371.30Smycroft			fi
7381.30Smycroft		else
7391.30Smycroft			if [ -f $CUR ]; then
7401.30Smycroft		printf "\n======\n%s removed\n======\n" $file
7411.30Smycroft				diff $CUR /dev/null
7421.52Satatat				backup_file remove $file $CUR $BACK
7431.9Scgd			fi
7441.9Scgd		fi
7451.9Scgd	done
7461.44Sadfi
7471.44Sad
7481.44Sadif [ -f /etc/security.local ]; then
7491.44Sad	echo ""
7501.44Sad	echo "Running /etc/security.local:"
7511.44Sad	. /etc/security.local
7521.9Scgdfi
753