security revision 1.28
11.1Scgd#!/bin/sh -
21.1Scgd#
31.28Slukem#	$NetBSD: security,v 1.28 1997/09/18 05:16:19 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.9Scgdumask 077
101.1Scgd
111.15Smrgif [ -s /etc/security.conf ]; then
121.15Smrg	. /etc/security.conf
131.15Smrgfi
141.15Smrg
151.15SmrgSECUREDIR=/tmp/_securedir.$$
161.15Smrgif ! mkdir $SECUREDIR; then
171.15Smrg	echo can not create $SECUREDIR.
181.15Smrg	exit 1
191.15Smrgfi
201.15Smrg
211.15Smrgif ! cd $SECUREDIR; then
221.15Smrg	echo can not chdir to $SECUREDIR.
231.15Smrg	exit 1
241.15Smrgfi
251.15Smrg
261.15SmrgERR=secure1.$$
271.15SmrgTMP1=secure2.$$
281.15SmrgTMP2=secure3.$$
291.28SlukemMPBYUID=secure4.$$
301.27SlukemMPPATH=secure5.$$
311.27SlukemLIST=secure6.$$
321.27SlukemOUTPUT=secure7.$$
331.15Smrg
341.27Slukemtrap '/bin/rm -rf $SECUREDIR ; exit 0' 0 2 3
351.15Smrg
361.15SmrgMP=/etc/master.passwd
371.9Scgd
381.27Slukem# these is used several times.
391.28Slukemawk -F: '{ print $1 " " $3 }' $MP | sort -k2n > $MPBYUID
401.27Slukemawk -F: '{ print $1 " " $9 }' $MP | sort -k2 > $MPPATH
411.9Scgd
421.9Scgd# Check the master password file syntax.
431.17Smycroftif [ "$check_passwd" = YES ]; then
441.25Slukem	awk '
451.25Slukem	BEGIN {
461.25Slukem		while ( getline < "/etc/shells" > 0 ) {
471.25Slukem			if ($LINE ~ /^\#/ || $LINE ~ /^$/ )
481.25Slukem				continue;
491.25Slukem			shells[$1]++;
501.25Slukem		}
511.25Slukem		FS=":";
521.25Slukem	}
531.25Slukem
541.25Slukem	{
551.15Smrg		if ($0 ~ /^[	 ]*$/) {
561.25Slukem			printf "Line %d is a blank line.\n", NR;
571.15Smrg			next;
581.15Smrg		}
591.15Smrg		if (NF != 10)
601.25Slukem			printf "Line %d has the wrong number of fields.\n", NR;
611.15Smrg		if ($1 !~ /^[A-Za-z0-9]*$/)
621.25Slukem			printf "Login %s has non-alphanumeric characters.\n",
631.25Slukem			    $1;
641.15Smrg		if (length($1) > 8)
651.25Slukem			printf "Login %s has more than 8 characters.\n", $1;
661.15Smrg		if ($2 == "")
671.25Slukem			printf "Login %s has no password.\n", $1;
681.25Slukem		if (length($2) != 13 && $2 != "") {
691.25Slukem			if ($10 == "" || shells[$10])
701.27Slukem		    printf "Login %s is off but still has a valid shell (%s)\n",
711.25Slukem				    $1, $10;
721.25Slukem		} else if (! shells[$10])
731.25Slukem			printf "Login %s does not have a valid shell (%s)\n",
741.25Slukem			    $1, $10;
751.15Smrg		if ($3 == 0 && $1 != "root" && $1 != "toor")
761.25Slukem			printf "Login %s has a user id of 0.\n", $1;
771.15Smrg		if ($3 < 0)
781.25Slukem			printf "Login %s has a negative user id.\n", $1;
791.15Smrg		if ($4 < 0)
801.25Slukem			printf "Login %s has a negative group id.\n", $1;
811.15Smrg	}' < $MP > $OUTPUT
821.15Smrg	if [ -s $OUTPUT ] ; then
831.15Smrg		printf "\nChecking the $MP file:\n"
841.15Smrg		cat $OUTPUT
851.15Smrg	fi
861.15Smrg
871.15Smrg	awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
881.15Smrg	if [ -s $OUTPUT ] ; then
891.15Smrg		printf "\n$MP has duplicate user names.\n"
901.15Smrg		column $OUTPUT
911.15Smrg	fi
921.15Smrg
931.28Slukem	< $MPBYUID uniq -d -f 1 | awk '{ print $2 }' > $TMP2
941.15Smrg	if [ -s $TMP2 ] ; then
951.15Smrg		printf "\n$MP has duplicate user id's.\n"
961.15Smrg		while read uid; do
971.28Slukem			grep -w $uid $MPBYUID
981.15Smrg		done < $TMP2 | column
991.15Smrg	fi
1001.9Scgdfi
1011.9Scgd
1021.9Scgd# Backup the master password file; a special case, the normal backup
1031.9Scgd# mechanisms also print out file differences and we don't want to do
1041.9Scgd# that because this file has encrypted passwords in it.
1051.9ScgdCUR=/var/backups/`basename $MP`.current
1061.9ScgdBACK=/var/backups/`basename $MP`.backup
1071.9Scgdif [ -s $CUR ] ; then
1081.9Scgd	if cmp -s $CUR $MP; then
1091.9Scgd		:
1101.9Scgd	else
1111.9Scgd		cp -p $CUR $BACK
1121.9Scgd		cp -p $MP $CUR
1131.9Scgd		chown root.wheel $CUR
1141.9Scgd	fi
1151.9Scgdelse
1161.9Scgd	cp -p $MP $CUR
1171.9Scgd	chown root.wheel $CUR
1181.9Scgdfi
1191.9Scgd
1201.9Scgd# Check the group file syntax.
1211.17Smycroftif [ "$check_group" = YES ]; then
1221.15Smrg	GRP=/etc/group
1231.15Smrg	awk -F: '{
1241.15Smrg		if ($0 ~ /^[	 ]*$/) {
1251.25Slukem			printf "Line %d is a blank line.\n", NR;
1261.15Smrg			next;
1271.15Smrg		}
1281.15Smrg		if (NF != 4)
1291.25Slukem			printf "Line %d has the wrong number of fields.\n", NR;
1301.15Smrg		if ($1 !~ /^[A-za-z0-9]*$/)
1311.25Slukem			printf "Group %s has non-alphanumeric characters.\n",
1321.25Slukem			    $1;
1331.15Smrg		if (length($1) > 8)
1341.25Slukem			printf "Group %s has more than 8 characters.\n", $1;
1351.15Smrg		if ($3 !~ /[0-9]*/)
1361.25Slukem			printf "Login %s has a negative group id.\n", $1;
1371.15Smrg	}' < $GRP > $OUTPUT
1381.15Smrg	if [ -s $OUTPUT ] ; then
1391.15Smrg		printf "\nChecking the $GRP file:\n"
1401.15Smrg		cat $OUTPUT
1411.15Smrg	fi
1421.15Smrg
1431.15Smrg	awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
1441.15Smrg	if [ -s $OUTPUT ] ; then
1451.15Smrg		printf "\n$GRP has duplicate group names.\n"
1461.15Smrg		column $OUTPUT
1471.15Smrg	fi
1481.9Scgdfi
1491.9Scgd
1501.9Scgd# Check for root paths, umask values in startup files.
1511.9Scgd# The check for the root paths is problematical -- it's likely to fail
1521.9Scgd# in other environments.  Once the shells have been modified to warn
1531.9Scgd# of '.' in the path, the path tests should go away.
1541.17Smycroftif [ "$check_rootdotfiles" = YES ]; then
1551.28Slukem	> $OUTPUT
1561.15Smrg	rhome=`csh -fc "echo ~root"`
1571.15Smrg	umaskset=no
1581.15Smrg	list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
1591.15Smrg	for i in $list ; do
1601.15Smrg		if [ -f $i ] ; then
1611.15Smrg			if egrep umask $i > /dev/null ; then
1621.15Smrg				umaskset=yes
1631.15Smrg			fi
1641.15Smrg			egrep umask $i |
1651.15Smrg			awk '$2 % 100 < 20 \
1661.27Slukem				{ print "\tRoot umask is group writeable" }
1671.15Smrg			     $2 % 10 < 2 \
1681.27Slukem				{ print "\tRoot umask is other writeable" }' \
1691.27Slukem			    >> $OUTPUT
1701.26Slukem			SAVE_PATH=$PATH
1711.26Slukem			unset PATH
1721.15Smrg			/bin/csh -f -s << end-of-csh > /dev/null 2>&1
1731.15Smrg				source $i
1741.15Smrg				/bin/ls -ldgT \$path > $TMP1
1751.9Scgdend-of-csh
1761.26Slukem			PATH=$SAVE_PATH
1771.15Smrg			awk '{
1781.15Smrg				if ($10 ~ /^\.$/) {
1791.27Slukem					print "\tThe root path includes .";
1801.15Smrg					next;
1811.15Smrg				}
1821.15Smrg			     }
1831.15Smrg			     $1 ~ /^d....w/ \
1841.27Slukem		{ print "\tRoot path directory " $10 " is group writeable." } \
1851.15Smrg			     $1 ~ /^d.......w/ \
1861.27Slukem		{ print "\tRoot path directory " $10 " is other writeable." }' \
1871.15Smrg			< $TMP1 >> $OUTPUT
1881.15Smrg		fi
1891.15Smrg	done
1901.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
1911.27Slukem		printf "\nChecking root csh paths, umask values:\n$list\n\n"
1921.15Smrg		if [ -s $OUTPUT ]; then
1931.15Smrg			cat $OUTPUT
1941.15Smrg		fi
1951.15Smrg		if [ $umaskset = "no" ] ; then
1961.27Slukem		    printf "\tRoot csh startup files do not set the umask.\n"
1971.15Smrg		fi
1981.9Scgd	fi
1991.9Scgd
2001.28Slukem	> $OUTPUT
2011.15Smrg	rhome=/root
2021.15Smrg	umaskset=no
2031.23Slukem	list="/etc/profile ${rhome}/.profile"
2041.15Smrg	for i in $list; do
2051.15Smrg		if [ -f $i ] ; then
2061.15Smrg			if egrep umask $i > /dev/null ; then
2071.15Smrg				umaskset=yes
2081.15Smrg			fi
2091.15Smrg			egrep umask $i |
2101.15Smrg			awk '$2 % 100 < 20 \
2111.27Slukem				{ print "\tRoot umask is group writeable" } \
2121.15Smrg			     $2 % 10 < 2 \
2131.27Slukem				{ print "\tRoot umask is other writeable" }' \
2141.27Slukem			    >> $OUTPUT
2151.26Slukem			SAVE_PATH=$PATH
2161.26Slukem			unset PATH
2171.15Smrg			/bin/sh << end-of-sh > /dev/null 2>&1
2181.15Smrg				. $i
2191.26Slukem				list=\`echo \$PATH | /usr/bin/sed -e \
2201.26Slukem				    's/^:/.:/;s/:$/:./;s/::/:.:/g;s/:/ /g'\`
2211.15Smrg				/bin/ls -ldgT \$list > $TMP1
2221.9Scgdend-of-sh
2231.26Slukem			PATH=$SAVE_PATH
2241.15Smrg			awk '{
2251.15Smrg				if ($10 ~ /^\.$/) {
2261.27Slukem					print "\tThe root path includes .";
2271.15Smrg					next;
2281.15Smrg				}
2291.15Smrg			     }
2301.15Smrg			     $1 ~ /^d....w/ \
2311.27Slukem		{ print "\tRoot path directory " $10 " is group writeable." } \
2321.15Smrg			     $1 ~ /^d.......w/ \
2331.27Slukem		{ print "\tRoot path directory " $10 " is other writeable." }' \
2341.15Smrg			< $TMP1 >> $OUTPUT
2351.9Scgd
2361.15Smrg		fi
2371.15Smrg	done
2381.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
2391.15Smrg		printf "\nChecking root sh paths, umask values:\n$list\n"
2401.15Smrg		if [ -s $OUTPUT ]; then
2411.15Smrg			cat $OUTPUT
2421.15Smrg		fi
2431.15Smrg		if [ $umaskset = "no" ] ; then
2441.27Slukem			printf "\tRoot sh startup files do not set the umask.\n"
2451.15Smrg		fi
2461.9Scgd	fi
2471.9Scgdfi
2481.9Scgd
2491.9Scgd# Root and uucp should both be in /etc/ftpusers.
2501.27Slukem# XXX This should be updated to support the new format...
2511.17Smycroftif [ "$check_ftpusers" = YES ]; then
2521.28Slukem	> $OUTPUT
2531.28Slukem	list="uucp "`awk '$2 == 0 { print $1 }' $MPBYUID`
2541.27Slukem	for i in $list; do
2551.27Slukem		if ! egrep "^$i$" /etc/ftpusers > /dev/null ; then
2561.28Slukem			printf "\t$i is not present\n" \ >> $OUTPUT
2571.27Slukem		fi
2581.27Slukem	done
2591.28Slukem	if [ -s $OUTPUT ]; then
2601.28Slukem		printf "\nChecking the /etc/ftpusers configuration:\n"
2611.28Slukem		cat $OUTPUT
2621.28Slukem	fi
2631.9Scgdfi
2641.9Scgd
2651.9Scgd# Uudecode should not be in the /etc/aliases file.
2661.17Smycroftif [ "$check_aliases" = YES ]; then
2671.18Smikel	if egrep '^[^#]*(uudecode|decode).*\|' /etc/aliases; then
2681.18Smikel		printf "\nEntry for uudecode in /etc/aliases file.\n"
2691.15Smrg	fi
2701.9Scgdfi
2711.9Scgd
2721.9Scgd# Files that should not have + signs.
2731.17Smycroftif [ "$check_rhosts" = YES ]; then
2741.15Smrg	list="/etc/hosts.equiv /etc/hosts.lpd"
2751.15Smrg	for f in $list ; do
2761.15Smrg		if [ -f $f ] && egrep '\+' $f > /dev/null ; then
2771.15Smrg			printf "\nPlus sign in $f file.\n"
2781.15Smrg		fi
2791.15Smrg	done
2801.15Smrg
2811.15Smrg	# Check for special users with .rhosts files.  Only root and toor should
2821.16Smikel	# have .rhosts files.  Also, .rhosts files should not have plus signs.
2831.15Smrg	awk -F: '$1 != "root" && $1 != "toor" && \
2841.15Smrg		($3 < 100 || $1 == "ftp" || $1 == "uucp") \
2851.20Smycroft			{ print $1 " " $9 }' $MP |
2861.19Smycroft	sort -k2 |
2871.15Smrg	while read uid homedir; do
2881.15Smrg		if [ -f ${homedir}/.rhosts ] ; then
2891.15Smrg			rhost=`ls -ldgT ${homedir}/.rhosts`
2901.15Smrg			printf "$uid: $rhost\n"
2911.15Smrg		fi
2921.15Smrg	done > $OUTPUT
2931.15Smrg	if [ -s $OUTPUT ] ; then
2941.15Smrg		printf "\nChecking for special users with .rhosts files.\n"
2951.15Smrg		cat $OUTPUT
2961.15Smrg	fi
2971.15Smrg
2981.15Smrg	while read uid homedir; do
2991.15Smrg		if [ -f ${homedir}/.rhosts ] && \
3001.15Smrg		    egrep '\+' ${homedir}/.rhosts > /dev/null ; then
3011.15Smrg			printf "$uid: + in .rhosts file.\n"
3021.15Smrg		fi
3031.27Slukem	done < $MPPATH > $OUTPUT
3041.15Smrg	if [ -s $OUTPUT ] ; then
3051.15Smrg		printf "\nChecking .rhosts files syntax.\n"
3061.15Smrg		cat $OUTPUT
3071.15Smrg	fi
3081.9Scgdfi
3091.9Scgd
3101.9Scgd# Check home directories.  Directories should not be owned by someone else
3111.9Scgd# or writeable.
3121.17Smycroftif [ "$check_homes" = YES ]; then
3131.15Smrg	while read uid homedir; do
3141.15Smrg		if [ -d ${homedir}/ ] ; then
3151.15Smrg			file=`ls -ldgT ${homedir}`
3161.15Smrg			printf "$uid $file\n"
3171.9Scgd		fi
3181.27Slukem	done < $MPPATH |
3191.15Smrg	awk '$1 != $4 && $4 != "root" \
3201.15Smrg		{ print "user " $1 " home directory is owned by " $4 }
3211.15Smrg	     $2 ~ /^-....w/ \
3221.15Smrg		{ print "user " $1 " home directory is group writeable" }
3231.15Smrg	     $2 ~ /^-.......w/ \
3241.27Slukem		{ print "user " $1 " home directory is other writeable" }' \
3251.27Slukem	    > $OUTPUT
3261.15Smrg	if [ -s $OUTPUT ] ; then
3271.15Smrg		printf "\nChecking home directories.\n"
3281.15Smrg		cat $OUTPUT
3291.15Smrg	fi
3301.15Smrg
3311.15Smrg	# Files that should not be owned by someone else or readable.
3321.27Slukem	list=".Xauthority .netrc"
3331.15Smrg	while read uid homedir; do
3341.15Smrg		for f in $list ; do
3351.15Smrg			file=${homedir}/${f}
3361.15Smrg			if [ -f $file ] ; then
3371.15Smrg				printf "$uid $f `ls -ldgT $file`\n"
3381.15Smrg			fi
3391.15Smrg		done
3401.27Slukem	done < $MPPATH |
3411.15Smrg	awk '$1 != $5 && $5 != "root" \
3421.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
3431.15Smrg	     $3 ~ /^-...r/ \
3441.15Smrg		{ print "user " $1 " " $2 " file is group readable" }
3451.15Smrg	     $3 ~ /^-......r/ \
3461.15Smrg		{ print "user " $1 " " $2 " file is other readable" }
3471.15Smrg	     $3 ~ /^-....w/ \
3481.15Smrg		{ print "user " $1 " " $2 " file is group writeable" }
3491.15Smrg	     $3 ~ /^-.......w/ \
3501.27Slukem		{ print "user " $1 " " $2 " file is other writeable" }' \
3511.27Slukem	    > $OUTPUT
3521.15Smrg
3531.15Smrg	# Files that should not be owned by someone else or writeable.
3541.19Smycroft	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
3551.19Smycroft	      .cshrc .emacs .exrc .forward .history .klogin .login .logout \
3561.27Slukem	      .profile .qmail .rc_history .rhosts .tcshrc .twmrc .xinitrc \
3571.27Slukem	      .xsession"
3581.15Smrg	while read uid homedir; do
3591.15Smrg		for f in $list ; do
3601.15Smrg			file=${homedir}/${f}
3611.15Smrg			if [ -f $file ] ; then
3621.15Smrg				printf "$uid $f `ls -ldgT $file`\n"
3631.15Smrg			fi
3641.15Smrg		done
3651.27Slukem	done < $MPPATH |
3661.15Smrg	awk '$1 != $5 && $5 != "root" \
3671.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
3681.15Smrg	     $3 ~ /^-....w/ \
3691.15Smrg		{ print "user " $1 " " $2 " file is group writeable" }
3701.15Smrg	     $3 ~ /^-.......w/ \
3711.27Slukem		{ print "user " $1 " " $2 " file is other writeable" }' \
3721.27Slukem	    >> $OUTPUT
3731.15Smrg	if [ -s $OUTPUT ] ; then
3741.15Smrg		printf "\nChecking dot files.\n"
3751.15Smrg		cat $OUTPUT
3761.15Smrg	fi
3771.9Scgdfi
3781.9Scgd
3791.9Scgd# Mailboxes should be owned by user and unreadable.
3801.17Smycroftif [ "$check_varmail" = YES ]; then
3811.15Smrg	ls -l /var/mail | sed 1d | \
3821.15Smrg	awk '$3 != $9 \
3831.15Smrg		{ print "user " $9 " mailbox is owned by " $3 }
3841.15Smrg	     $1 != "-rw-------" \
3851.15Smrg		{ print "user " $9 " mailbox is " $1 ", group " $4 }' > $OUTPUT
3861.15Smrg	if [ -s $OUTPUT ] ; then
3871.15Smrg		printf "\nChecking mailbox ownership.\n"
3881.15Smrg		cat $OUTPUT
3891.15Smrg	fi
3901.15Smrgfi
3911.15Smrg
3921.17Smycroftif [ "$check_nfs" = YES ]; then
3931.15Smrg	if [ -f /etc/exports ]; then
3941.15Smrg	    # File systems should not be globally exported.
3951.15Smrg	    awk '{
3961.22Slukem		# ignore comments and blank lines
3971.22Slukem		if ($LINE ~ /^\#/ || $LINE ~ /^$/ )
3981.22Slukem			next;
3991.22Slukem
4001.15Smrg		readonly = 0;
4011.15Smrg		for (i = 2; i <= NF; ++i) {
4021.15Smrg			if ($i ~ /-ro/)
4031.15Smrg				readonly = 1;
4041.15Smrg			else if ($i !~ /^-/)
4051.15Smrg				next;
4061.15Smrg		}
4071.15Smrg		if (readonly)
4081.15Smrg			print "File system " $1 " globally exported, read-only."
4091.15Smrg		else
4101.15Smrg			print "File system " $1 " globally exported, read-write."
4111.15Smrg	    }' < /etc/exports > $OUTPUT
4121.15Smrg	    if [ -s $OUTPUT ] ; then
4131.15Smrg		printf "\nChecking for globally exported file systems.\n"
4141.15Smrg		cat $OUTPUT
4151.15Smrg	    fi
4161.15Smrg	fi
4171.9Scgdfi
4181.9Scgd
4191.9Scgd# Display any changes in setuid files and devices.
4201.17Smycroftif [ "$check_devices" = YES ]; then
4211.28Slukem	> $ERR
4221.15Smrg	(find / \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \
4231.15Smrg			-o -fstype procfs \) -a -prune -o \
4241.21Smycroft	    \( \( -perm -u+s -a ! -type d \) -o \
4251.21Smycroft	       \( -perm -g+s -a ! -type d \) -o \
4261.24Slukem	       -type b -o -type c \) -print0 | \
4271.24Slukem	xargs -0 ls -ldgTq | sort +9 > $LIST) 2> $OUTPUT
4281.15Smrg
4291.15Smrg	# Display any errors that occurred during system file walk.
4301.15Smrg	if [ -s $OUTPUT ] ; then
4311.28Slukem		printf "Setuid/device find errors:\n" >> $ERR
4321.28Slukem		cat $OUTPUT >> $ERR
4331.28Slukem		printf "\n" >> $ERR
4341.15Smrg	fi
4351.15Smrg
4361.15Smrg	# Display any changes in the setuid file list.
4371.15Smrg	egrep -v '^[bc]' $LIST > $TMP1
4381.15Smrg	if [ -s $TMP1 ] ; then
4391.15Smrg		# Check to make sure uudecode isn't setuid.
4401.15Smrg		if grep -w uudecode $TMP1 > /dev/null ; then
4411.28Slukem			printf "\nUudecode is setuid.\n" >> $ERR
4421.15Smrg		fi
4431.15Smrg
4441.15Smrg		CUR=/var/backups/setuid.current
4451.15Smrg		BACK=/var/backups/setuid.backup
4461.9Scgd
4471.15Smrg		if [ -s $CUR ] ; then
4481.15Smrg			if cmp -s $CUR $TMP1 ; then
4491.15Smrg				:
4501.15Smrg			else
4511.15Smrg				> $TMP2
4521.15Smrg				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
4531.15Smrg				if [ -s $OUTPUT ] ; then
4541.28Slukem					printf "Setuid additions:\n" >> $ERR
4551.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
4561.28Slukem					printf "\n" >> $ERR
4571.15Smrg				fi
4581.15Smrg
4591.15Smrg				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
4601.15Smrg				if [ -s $OUTPUT ] ; then
4611.28Slukem					printf "Setuid deletions:\n" >> $ERR
4621.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
4631.28Slukem					printf "\n" >> $ERR
4641.15Smrg				fi
4651.15Smrg
4661.20Smycroft				sort -k10 $TMP2 $CUR $TMP1 | \
4671.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
4681.27Slukem				    uniq -u > $OUTPUT
4691.15Smrg				if [ -s $OUTPUT ] ; then
4701.28Slukem					printf "Setuid changes:\n" >> $ERR
4711.28Slukem					column -t $OUTPUT >> $ERR
4721.28Slukem					printf "\n" >> $ERR
4731.15Smrg				fi
4741.9Scgd
4751.15Smrg				cp $CUR $BACK
4761.15Smrg				cp $TMP1 $CUR
4771.9Scgd			fi
4781.15Smrg		else
4791.28Slukem			printf "Setuid additions:\n" >> $ERR
4801.28Slukem			column -t $TMP1 >> $ERR
4811.28Slukem			printf "\n" >> $ERR
4821.9Scgd			cp $TMP1 $CUR
4831.9Scgd		fi
4841.15Smrg	fi
4851.15Smrg
4861.27Slukem	# Check for block and character disk devices that are readable or
4871.27Slukem	# writeable or not owned by root.operator.
4881.15Smrg	>$TMP1
4891.27Slukem	DISKLIST="acd ccd cd ch fd hk hp mcd md ra rb rd rl rx rz \
4901.27Slukem	    sd se ss tz uk up vnd wd xd xy"
4911.27Slukem#	DISKLIST="$DISKLIST ct mt st wt"
4921.15Smrg	for i in $DISKLIST; do
4931.15Smrg		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
4941.15Smrg		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
4951.15Smrg	done
4961.15Smrg
4971.15Smrg	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
4981.25Slukem		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
4991.25Slukem		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
5001.15Smrg	if [ -s $OUTPUT ] ; then
5011.28Slukem		printf "\nChecking disk ownership and permissions.\n" >> $ERR
5021.28Slukem		cat $OUTPUT >> $ERR
5031.28Slukem		printf "\n" >> $ERR
5041.9Scgd	fi
5051.9Scgd
5061.15Smrg	# Display any changes in the device file list.
5071.20Smycroft	egrep '^[bc]' $LIST | sort -k11 > $TMP1
5081.15Smrg	if [ -s $TMP1 ] ; then
5091.15Smrg		CUR=/var/backups/device.current
5101.15Smrg		BACK=/var/backups/device.backup
5111.15Smrg
5121.15Smrg		if [ -s $CUR ] ; then
5131.15Smrg			if cmp -s $CUR $TMP1 ; then
5141.15Smrg				:
5151.15Smrg			else
5161.15Smrg				> $TMP2
5171.15Smrg				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
5181.15Smrg				if [ -s $OUTPUT ] ; then
5191.28Slukem					printf "Device additions:\n" >> $ERR
5201.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
5211.28Slukem					printf "\n" >> $ERR
5221.15Smrg				fi
5231.15Smrg
5241.15Smrg				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
5251.15Smrg				if [ -s $OUTPUT ] ; then
5261.28Slukem					printf "Device deletions:\n" >> $ERR
5271.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
5281.28Slukem					printf "\n" >> $ERR
5291.15Smrg				fi
5301.15Smrg
5311.27Slukem				# Report any block device change. Ignore
5321.27Slukem				# character devices, only the name is
5331.27Slukem				# significant.
5341.15Smrg				cat $TMP2 $CUR $TMP1 | \
5351.27Slukem				    sed -e '/^c/d' | \
5361.27Slukem				    sort -k11 | \
5371.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
5381.27Slukem				    uniq -u > $OUTPUT
5391.15Smrg				if [ -s $OUTPUT ] ; then
5401.28Slukem					printf "Block device changes:\n" >> $ERR
5411.28Slukem					column -t $OUTPUT >> $ERR
5421.28Slukem					printf "\n" >> $ERR
5431.15Smrg				fi
5441.9Scgd
5451.15Smrg				cp $CUR $BACK
5461.15Smrg				cp $TMP1 $CUR
5471.9Scgd			fi
5481.15Smrg		else
5491.28Slukem			printf "Device additions:\n" >> $ERR
5501.28Slukem			column -t $TMP1 >> $ERR
5511.28Slukem			printf "\n" >> $ERR
5521.28Slukem			cp $TMP1 $CUR >> $ERR
5531.9Scgd		fi
5541.28Slukem	fi
5551.28Slukem	if [ -s $ERR ] ; then
5561.28Slukem		printf "\nChecking setuid files and devices:\n"
5571.28Slukem		cat $ERR
5581.28Slukem		printf "\n"
5591.9Scgd	fi
5601.9Scgdfi
5611.9Scgd
5621.9Scgd# Check special files.
5631.9Scgd# Check system binaries.
5641.9Scgd#
5651.9Scgd# Create the mtree tree specifications using:
5661.9Scgd#
5671.9Scgd#	mtree -cx -pDIR -kcksum,gid,mode,nlink,size,link,time,uid > DIR.secure
5681.16Smikel#	chown root.wheel DIR.secure
5691.16Smikel#	chmod 600 DIR.secure
5701.9Scgd#
5711.9Scgd# Note, this is not complete protection against Trojan horsed binaries, as
5721.9Scgd# the hacker can modify the tree specification to match the replaced binary.
5731.9Scgd# For details on really protecting yourself against modified binaries, see
5741.9Scgd# the mtree(8) manual page.
5751.17Smycroftif [ "$check_mtree" = YES ]; then
5761.9Scgd	mtree -e -p / -f /etc/mtree/special > $OUTPUT
5771.15Smrg	if [ -s $OUTPUT ]; then
5781.9Scgd		printf "\nChecking special files and directories.\n"
5791.9Scgd		cat $OUTPUT
5801.9Scgd	fi
5811.9Scgd
5821.9Scgd	> $OUTPUT
5831.16Smikel	for file in /etc/mtree/*.secure; do
5841.16Smikel		[ $file = '/etc/mtree/*.secure' ] && continue
5851.9Scgd		tree=`sed -n -e '3s/.* //p' -e 3q $file`
5861.9Scgd		mtree -f $file -p $tree > $TMP1
5871.9Scgd		if [ -s $TMP1 ]; then
5881.9Scgd			printf "\nChecking $tree:\n" >> $OUTPUT
5891.9Scgd			cat $TMP1 >> $OUTPUT
5901.9Scgd		fi
5911.9Scgd	done
5921.15Smrg	if [ -s $OUTPUT ]; then
5931.9Scgd		printf "\nChecking system binaries:\n"
5941.9Scgd		cat $OUTPUT
5951.9Scgd	fi
5961.9Scgdfi
5971.9Scgd
5981.9Scgd# List of files that get backed up and checked for any modifications.  Each
5991.9Scgd# file is expected to have two backups, /var/backups/file.{current,backup}.
6001.9Scgd# Any changes cause the files to rotate.
6011.17Smycroftif [ "$check_changelist" = YES ] && [ -s /etc/changelist ] ; then
6021.27Slukem	for file in `egrep -v "^#|$MP" /etc/changelist`; do
6031.9Scgd		CUR=/var/backups/`basename $file`.current
6041.9Scgd		BACK=/var/backups/`basename $file`.backup
6051.9Scgd		if [ -s $file ]; then
6061.9Scgd			if [ -s $CUR ] ; then
6071.9Scgd				diff $CUR $file > $OUTPUT
6081.9Scgd				if [ -s $OUTPUT ] ; then
6091.9Scgd		printf "\n======\n%s diffs (OLD < > NEW)\n======\n" $file
6101.9Scgd					cat $OUTPUT
6111.9Scgd					cp -p $CUR $BACK
6121.9Scgd					cp -p $file $CUR
6131.9Scgd					chown root.wheel $CUR $BACK
6141.9Scgd				fi
6151.9Scgd			else
6161.9Scgd				cp -p $file $CUR
6171.9Scgd				chown root.wheel $CUR
6181.9Scgd			fi
6191.9Scgd		fi
6201.9Scgd	done
6211.9Scgdfi
622