security revision 1.29
11.1Scgd#!/bin/sh -
21.1Scgd#
31.29Slukem#	$NetBSD: security,v 1.29 1997/09/23 14:36:56 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.29SlukemMPBYPATH=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.29Slukemawk -F: '{ print $1 " " $9 }' $MP | sort -k2 > $MPBYPATH
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.17Smycroftif [ "$check_ftpusers" = YES ]; then
2511.28Slukem	> $OUTPUT
2521.28Slukem	list="uucp "`awk '$2 == 0 { print $1 }' $MPBYUID`
2531.27Slukem	for i in $list; do
2541.29Slukem		if /usr/libexec/ftpd -C $i ; then
2551.29Slukem			printf "\t$i is not denied\n" >> $OUTPUT
2561.27Slukem		fi
2571.27Slukem	done
2581.28Slukem	if [ -s $OUTPUT ]; then
2591.28Slukem		printf "\nChecking the /etc/ftpusers configuration:\n"
2601.28Slukem		cat $OUTPUT
2611.28Slukem	fi
2621.9Scgdfi
2631.9Scgd
2641.9Scgd# Uudecode should not be in the /etc/aliases file.
2651.17Smycroftif [ "$check_aliases" = YES ]; then
2661.18Smikel	if egrep '^[^#]*(uudecode|decode).*\|' /etc/aliases; then
2671.18Smikel		printf "\nEntry for uudecode in /etc/aliases file.\n"
2681.15Smrg	fi
2691.9Scgdfi
2701.9Scgd
2711.9Scgd# Files that should not have + signs.
2721.17Smycroftif [ "$check_rhosts" = YES ]; then
2731.15Smrg	list="/etc/hosts.equiv /etc/hosts.lpd"
2741.15Smrg	for f in $list ; do
2751.15Smrg		if [ -f $f ] && egrep '\+' $f > /dev/null ; then
2761.15Smrg			printf "\nPlus sign in $f file.\n"
2771.15Smrg		fi
2781.15Smrg	done
2791.15Smrg
2801.15Smrg	# Check for special users with .rhosts files.  Only root and toor should
2811.16Smikel	# have .rhosts files.  Also, .rhosts files should not have plus signs.
2821.15Smrg	awk -F: '$1 != "root" && $1 != "toor" && \
2831.15Smrg		($3 < 100 || $1 == "ftp" || $1 == "uucp") \
2841.20Smycroft			{ print $1 " " $9 }' $MP |
2851.19Smycroft	sort -k2 |
2861.15Smrg	while read uid homedir; do
2871.15Smrg		if [ -f ${homedir}/.rhosts ] ; then
2881.15Smrg			rhost=`ls -ldgT ${homedir}/.rhosts`
2891.15Smrg			printf "$uid: $rhost\n"
2901.15Smrg		fi
2911.15Smrg	done > $OUTPUT
2921.15Smrg	if [ -s $OUTPUT ] ; then
2931.15Smrg		printf "\nChecking for special users with .rhosts files.\n"
2941.15Smrg		cat $OUTPUT
2951.15Smrg	fi
2961.15Smrg
2971.15Smrg	while read uid homedir; do
2981.15Smrg		if [ -f ${homedir}/.rhosts ] && \
2991.15Smrg		    egrep '\+' ${homedir}/.rhosts > /dev/null ; then
3001.15Smrg			printf "$uid: + in .rhosts file.\n"
3011.15Smrg		fi
3021.29Slukem	done < $MPBYPATH > $OUTPUT
3031.15Smrg	if [ -s $OUTPUT ] ; then
3041.15Smrg		printf "\nChecking .rhosts files syntax.\n"
3051.15Smrg		cat $OUTPUT
3061.15Smrg	fi
3071.9Scgdfi
3081.9Scgd
3091.9Scgd# Check home directories.  Directories should not be owned by someone else
3101.9Scgd# or writeable.
3111.17Smycroftif [ "$check_homes" = YES ]; then
3121.15Smrg	while read uid homedir; do
3131.15Smrg		if [ -d ${homedir}/ ] ; then
3141.15Smrg			file=`ls -ldgT ${homedir}`
3151.15Smrg			printf "$uid $file\n"
3161.9Scgd		fi
3171.29Slukem	done < $MPBYPATH |
3181.15Smrg	awk '$1 != $4 && $4 != "root" \
3191.15Smrg		{ print "user " $1 " home directory is owned by " $4 }
3201.15Smrg	     $2 ~ /^-....w/ \
3211.15Smrg		{ print "user " $1 " home directory is group writeable" }
3221.15Smrg	     $2 ~ /^-.......w/ \
3231.27Slukem		{ print "user " $1 " home directory is other writeable" }' \
3241.27Slukem	    > $OUTPUT
3251.15Smrg	if [ -s $OUTPUT ] ; then
3261.15Smrg		printf "\nChecking home directories.\n"
3271.15Smrg		cat $OUTPUT
3281.15Smrg	fi
3291.15Smrg
3301.15Smrg	# Files that should not be owned by someone else or readable.
3311.27Slukem	list=".Xauthority .netrc"
3321.15Smrg	while read uid homedir; do
3331.15Smrg		for f in $list ; do
3341.15Smrg			file=${homedir}/${f}
3351.15Smrg			if [ -f $file ] ; then
3361.15Smrg				printf "$uid $f `ls -ldgT $file`\n"
3371.15Smrg			fi
3381.15Smrg		done
3391.29Slukem	done < $MPBYPATH |
3401.15Smrg	awk '$1 != $5 && $5 != "root" \
3411.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
3421.15Smrg	     $3 ~ /^-...r/ \
3431.15Smrg		{ print "user " $1 " " $2 " file is group readable" }
3441.15Smrg	     $3 ~ /^-......r/ \
3451.15Smrg		{ print "user " $1 " " $2 " file is other readable" }
3461.15Smrg	     $3 ~ /^-....w/ \
3471.15Smrg		{ print "user " $1 " " $2 " file is group writeable" }
3481.15Smrg	     $3 ~ /^-.......w/ \
3491.27Slukem		{ print "user " $1 " " $2 " file is other writeable" }' \
3501.27Slukem	    > $OUTPUT
3511.15Smrg
3521.15Smrg	# Files that should not be owned by someone else or writeable.
3531.19Smycroft	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
3541.19Smycroft	      .cshrc .emacs .exrc .forward .history .klogin .login .logout \
3551.27Slukem	      .profile .qmail .rc_history .rhosts .tcshrc .twmrc .xinitrc \
3561.27Slukem	      .xsession"
3571.15Smrg	while read uid homedir; do
3581.15Smrg		for f in $list ; do
3591.15Smrg			file=${homedir}/${f}
3601.15Smrg			if [ -f $file ] ; then
3611.15Smrg				printf "$uid $f `ls -ldgT $file`\n"
3621.15Smrg			fi
3631.15Smrg		done
3641.29Slukem	done < $MPBYPATH |
3651.15Smrg	awk '$1 != $5 && $5 != "root" \
3661.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
3671.15Smrg	     $3 ~ /^-....w/ \
3681.15Smrg		{ print "user " $1 " " $2 " file is group writeable" }
3691.15Smrg	     $3 ~ /^-.......w/ \
3701.27Slukem		{ print "user " $1 " " $2 " file is other writeable" }' \
3711.27Slukem	    >> $OUTPUT
3721.15Smrg	if [ -s $OUTPUT ] ; then
3731.15Smrg		printf "\nChecking dot files.\n"
3741.15Smrg		cat $OUTPUT
3751.15Smrg	fi
3761.9Scgdfi
3771.9Scgd
3781.9Scgd# Mailboxes should be owned by user and unreadable.
3791.17Smycroftif [ "$check_varmail" = YES ]; then
3801.15Smrg	ls -l /var/mail | sed 1d | \
3811.15Smrg	awk '$3 != $9 \
3821.15Smrg		{ print "user " $9 " mailbox is owned by " $3 }
3831.15Smrg	     $1 != "-rw-------" \
3841.15Smrg		{ print "user " $9 " mailbox is " $1 ", group " $4 }' > $OUTPUT
3851.15Smrg	if [ -s $OUTPUT ] ; then
3861.15Smrg		printf "\nChecking mailbox ownership.\n"
3871.15Smrg		cat $OUTPUT
3881.15Smrg	fi
3891.15Smrgfi
3901.15Smrg
3911.17Smycroftif [ "$check_nfs" = YES ]; then
3921.15Smrg	if [ -f /etc/exports ]; then
3931.15Smrg	    # File systems should not be globally exported.
3941.15Smrg	    awk '{
3951.22Slukem		# ignore comments and blank lines
3961.22Slukem		if ($LINE ~ /^\#/ || $LINE ~ /^$/ )
3971.22Slukem			next;
3981.22Slukem
3991.15Smrg		readonly = 0;
4001.15Smrg		for (i = 2; i <= NF; ++i) {
4011.15Smrg			if ($i ~ /-ro/)
4021.15Smrg				readonly = 1;
4031.15Smrg			else if ($i !~ /^-/)
4041.15Smrg				next;
4051.15Smrg		}
4061.15Smrg		if (readonly)
4071.15Smrg			print "File system " $1 " globally exported, read-only."
4081.15Smrg		else
4091.15Smrg			print "File system " $1 " globally exported, read-write."
4101.15Smrg	    }' < /etc/exports > $OUTPUT
4111.15Smrg	    if [ -s $OUTPUT ] ; then
4121.15Smrg		printf "\nChecking for globally exported file systems.\n"
4131.15Smrg		cat $OUTPUT
4141.15Smrg	    fi
4151.15Smrg	fi
4161.9Scgdfi
4171.9Scgd
4181.9Scgd# Display any changes in setuid files and devices.
4191.17Smycroftif [ "$check_devices" = YES ]; then
4201.28Slukem	> $ERR
4211.15Smrg	(find / \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \
4221.15Smrg			-o -fstype procfs \) -a -prune -o \
4231.21Smycroft	    \( \( -perm -u+s -a ! -type d \) -o \
4241.21Smycroft	       \( -perm -g+s -a ! -type d \) -o \
4251.24Slukem	       -type b -o -type c \) -print0 | \
4261.24Slukem	xargs -0 ls -ldgTq | sort +9 > $LIST) 2> $OUTPUT
4271.15Smrg
4281.15Smrg	# Display any errors that occurred during system file walk.
4291.15Smrg	if [ -s $OUTPUT ] ; then
4301.28Slukem		printf "Setuid/device find errors:\n" >> $ERR
4311.28Slukem		cat $OUTPUT >> $ERR
4321.28Slukem		printf "\n" >> $ERR
4331.15Smrg	fi
4341.15Smrg
4351.15Smrg	# Display any changes in the setuid file list.
4361.15Smrg	egrep -v '^[bc]' $LIST > $TMP1
4371.15Smrg	if [ -s $TMP1 ] ; then
4381.15Smrg		# Check to make sure uudecode isn't setuid.
4391.15Smrg		if grep -w uudecode $TMP1 > /dev/null ; then
4401.28Slukem			printf "\nUudecode is setuid.\n" >> $ERR
4411.15Smrg		fi
4421.15Smrg
4431.15Smrg		CUR=/var/backups/setuid.current
4441.15Smrg		BACK=/var/backups/setuid.backup
4451.9Scgd
4461.15Smrg		if [ -s $CUR ] ; then
4471.15Smrg			if cmp -s $CUR $TMP1 ; then
4481.15Smrg				:
4491.15Smrg			else
4501.15Smrg				> $TMP2
4511.15Smrg				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
4521.15Smrg				if [ -s $OUTPUT ] ; then
4531.28Slukem					printf "Setuid additions:\n" >> $ERR
4541.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
4551.28Slukem					printf "\n" >> $ERR
4561.15Smrg				fi
4571.15Smrg
4581.15Smrg				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
4591.15Smrg				if [ -s $OUTPUT ] ; then
4601.28Slukem					printf "Setuid deletions:\n" >> $ERR
4611.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
4621.28Slukem					printf "\n" >> $ERR
4631.15Smrg				fi
4641.15Smrg
4651.20Smycroft				sort -k10 $TMP2 $CUR $TMP1 | \
4661.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
4671.27Slukem				    uniq -u > $OUTPUT
4681.15Smrg				if [ -s $OUTPUT ] ; then
4691.28Slukem					printf "Setuid changes:\n" >> $ERR
4701.28Slukem					column -t $OUTPUT >> $ERR
4711.28Slukem					printf "\n" >> $ERR
4721.15Smrg				fi
4731.9Scgd
4741.15Smrg				cp $CUR $BACK
4751.15Smrg				cp $TMP1 $CUR
4761.9Scgd			fi
4771.15Smrg		else
4781.28Slukem			printf "Setuid additions:\n" >> $ERR
4791.28Slukem			column -t $TMP1 >> $ERR
4801.28Slukem			printf "\n" >> $ERR
4811.9Scgd			cp $TMP1 $CUR
4821.9Scgd		fi
4831.15Smrg	fi
4841.15Smrg
4851.27Slukem	# Check for block and character disk devices that are readable or
4861.27Slukem	# writeable or not owned by root.operator.
4871.15Smrg	>$TMP1
4881.27Slukem	DISKLIST="acd ccd cd ch fd hk hp mcd md ra rb rd rl rx rz \
4891.27Slukem	    sd se ss tz uk up vnd wd xd xy"
4901.27Slukem#	DISKLIST="$DISKLIST ct mt st wt"
4911.15Smrg	for i in $DISKLIST; do
4921.15Smrg		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
4931.15Smrg		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
4941.15Smrg	done
4951.15Smrg
4961.15Smrg	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
4971.25Slukem		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
4981.25Slukem		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
4991.15Smrg	if [ -s $OUTPUT ] ; then
5001.28Slukem		printf "\nChecking disk ownership and permissions.\n" >> $ERR
5011.28Slukem		cat $OUTPUT >> $ERR
5021.28Slukem		printf "\n" >> $ERR
5031.9Scgd	fi
5041.9Scgd
5051.15Smrg	# Display any changes in the device file list.
5061.20Smycroft	egrep '^[bc]' $LIST | sort -k11 > $TMP1
5071.15Smrg	if [ -s $TMP1 ] ; then
5081.15Smrg		CUR=/var/backups/device.current
5091.15Smrg		BACK=/var/backups/device.backup
5101.15Smrg
5111.15Smrg		if [ -s $CUR ] ; then
5121.15Smrg			if cmp -s $CUR $TMP1 ; then
5131.15Smrg				:
5141.15Smrg			else
5151.15Smrg				> $TMP2
5161.15Smrg				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
5171.15Smrg				if [ -s $OUTPUT ] ; then
5181.28Slukem					printf "Device additions:\n" >> $ERR
5191.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
5201.28Slukem					printf "\n" >> $ERR
5211.15Smrg				fi
5221.15Smrg
5231.15Smrg				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
5241.15Smrg				if [ -s $OUTPUT ] ; then
5251.28Slukem					printf "Device deletions:\n" >> $ERR
5261.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
5271.28Slukem					printf "\n" >> $ERR
5281.15Smrg				fi
5291.15Smrg
5301.27Slukem				# Report any block device change. Ignore
5311.27Slukem				# character devices, only the name is
5321.27Slukem				# significant.
5331.15Smrg				cat $TMP2 $CUR $TMP1 | \
5341.27Slukem				    sed -e '/^c/d' | \
5351.27Slukem				    sort -k11 | \
5361.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
5371.27Slukem				    uniq -u > $OUTPUT
5381.15Smrg				if [ -s $OUTPUT ] ; then
5391.28Slukem					printf "Block device changes:\n" >> $ERR
5401.28Slukem					column -t $OUTPUT >> $ERR
5411.28Slukem					printf "\n" >> $ERR
5421.15Smrg				fi
5431.9Scgd
5441.15Smrg				cp $CUR $BACK
5451.15Smrg				cp $TMP1 $CUR
5461.9Scgd			fi
5471.15Smrg		else
5481.28Slukem			printf "Device additions:\n" >> $ERR
5491.28Slukem			column -t $TMP1 >> $ERR
5501.28Slukem			printf "\n" >> $ERR
5511.28Slukem			cp $TMP1 $CUR >> $ERR
5521.9Scgd		fi
5531.28Slukem	fi
5541.28Slukem	if [ -s $ERR ] ; then
5551.28Slukem		printf "\nChecking setuid files and devices:\n"
5561.28Slukem		cat $ERR
5571.28Slukem		printf "\n"
5581.9Scgd	fi
5591.9Scgdfi
5601.9Scgd
5611.9Scgd# Check special files.
5621.9Scgd# Check system binaries.
5631.9Scgd#
5641.9Scgd# Create the mtree tree specifications using:
5651.9Scgd#
5661.9Scgd#	mtree -cx -pDIR -kcksum,gid,mode,nlink,size,link,time,uid > DIR.secure
5671.16Smikel#	chown root.wheel DIR.secure
5681.16Smikel#	chmod 600 DIR.secure
5691.9Scgd#
5701.9Scgd# Note, this is not complete protection against Trojan horsed binaries, as
5711.9Scgd# the hacker can modify the tree specification to match the replaced binary.
5721.9Scgd# For details on really protecting yourself against modified binaries, see
5731.9Scgd# the mtree(8) manual page.
5741.17Smycroftif [ "$check_mtree" = YES ]; then
5751.9Scgd	mtree -e -p / -f /etc/mtree/special > $OUTPUT
5761.15Smrg	if [ -s $OUTPUT ]; then
5771.9Scgd		printf "\nChecking special files and directories.\n"
5781.9Scgd		cat $OUTPUT
5791.9Scgd	fi
5801.9Scgd
5811.9Scgd	> $OUTPUT
5821.16Smikel	for file in /etc/mtree/*.secure; do
5831.16Smikel		[ $file = '/etc/mtree/*.secure' ] && continue
5841.9Scgd		tree=`sed -n -e '3s/.* //p' -e 3q $file`
5851.9Scgd		mtree -f $file -p $tree > $TMP1
5861.9Scgd		if [ -s $TMP1 ]; then
5871.9Scgd			printf "\nChecking $tree:\n" >> $OUTPUT
5881.9Scgd			cat $TMP1 >> $OUTPUT
5891.9Scgd		fi
5901.9Scgd	done
5911.15Smrg	if [ -s $OUTPUT ]; then
5921.9Scgd		printf "\nChecking system binaries:\n"
5931.9Scgd		cat $OUTPUT
5941.9Scgd	fi
5951.9Scgdfi
5961.9Scgd
5971.9Scgd# List of files that get backed up and checked for any modifications.  Each
5981.9Scgd# file is expected to have two backups, /var/backups/file.{current,backup}.
5991.9Scgd# Any changes cause the files to rotate.
6001.17Smycroftif [ "$check_changelist" = YES ] && [ -s /etc/changelist ] ; then
6011.27Slukem	for file in `egrep -v "^#|$MP" /etc/changelist`; do
6021.9Scgd		CUR=/var/backups/`basename $file`.current
6031.9Scgd		BACK=/var/backups/`basename $file`.backup
6041.9Scgd		if [ -s $file ]; then
6051.9Scgd			if [ -s $CUR ] ; then
6061.9Scgd				diff $CUR $file > $OUTPUT
6071.9Scgd				if [ -s $OUTPUT ] ; then
6081.9Scgd		printf "\n======\n%s diffs (OLD < > NEW)\n======\n" $file
6091.9Scgd					cat $OUTPUT
6101.9Scgd					cp -p $CUR $BACK
6111.9Scgd					cp -p $file $CUR
6121.9Scgd					chown root.wheel $CUR $BACK
6131.9Scgd				fi
6141.9Scgd			else
6151.9Scgd				cp -p $file $CUR
6161.9Scgd				chown root.wheel $CUR
6171.9Scgd			fi
6181.9Scgd		fi
6191.9Scgd	done
6201.9Scgdfi
621