security revision 1.84
11.1Scgd#!/bin/sh -
21.1Scgd#
31.84Sjhawk#	$NetBSD: security,v 1.84 2003/10/01 04:29:03 jhawk 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.83Sjhawk				diff ${diff_options} $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.83Sjhawk				diff ${diff_options} /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.83Sjhawk				diff ${diff_options} $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.81Sjhawk	checkyesno check_passwd_permit_star && permit_star=0 || permit_star=1
1781.81Sjhawk	awk -v "len=$max_loginlen" \
1791.81Sjhawk	    -v "nowarn_shells_list=$check_passwd_nowarn_shells" \
1801.81Sjhawk	    -v "nowarn_users_list=$check_passwd_nowarn_users" \
1811.81Sjhawk	    -v "permit_star=$permit_star" '
1821.25Slukem	BEGIN {
1831.25Slukem		while ( getline < "/etc/shells" > 0 ) {
1841.39Shubertf			if ($0 ~ /^\#/ || $0 ~ /^$/ )
1851.25Slukem				continue;
1861.25Slukem			shells[$1]++;
1871.25Slukem		}
1881.81Sjhawk		split(nowarn_shells_list, a);
1891.81Sjhawk		for (i in a) nowarn_shells[a[i]]++;
1901.81Sjhawk		split(nowarn_users_list, a);
1911.81Sjhawk		for (i in a) nowarn_users[a[i]]++;
1921.81Sjhawk		uid0_users_list="root toor"
1931.81Sjhawk		split(uid0_users_list, a);
1941.81Sjhawk		for (i in a) uid0_users[a[i]]++;
1951.25Slukem		FS=":";
1961.25Slukem	}
1971.25Slukem
1981.25Slukem	{
1991.15Smrg		if ($0 ~ /^[	 ]*$/) {
2001.25Slukem			printf "Line %d is a blank line.\n", NR;
2011.15Smrg			next;
2021.15Smrg		}
2031.34Sabs		if (NF != 10 && ($1 != "+" || NF != 1))
2041.25Slukem			printf "Line %d has the wrong number of fields.\n", NR;
2051.34Sabs		if ($1 == "+" )  {
2061.34Sabs			if (NF != 1 && $3 == 0)
2071.81Sjhawk			    printf "Line %d includes entries with uid 0.\n",
2081.81Sjhawk			        NR;
2091.34Sabs			next;
2101.34Sabs		}
2111.53Satatat		if ($1 !~ /^[A-Za-z0-9]([-A-Za-z0-9]*[A-Za-z0-9])*$/)
2121.25Slukem			printf "Login %s has non-alphanumeric characters.\n",
2131.25Slukem			    $1;
2141.34Sabs		if (length($1) > len)
2151.81Sjhawk			printf "Login %s has more than "len" characters.\n",
2161.81Sjhawk			    $1;
2171.81Sjhawk		if ($2 == "" && !nowarn_users[$1])
2181.81Sjhawk			    printf "Login %s has no password.\n", $1;
2191.81Sjhawk		if (!nowarn_shells[$10] && !nowarn_users[$1]) {
2201.81Sjhawk		    if (length($2) != 13 &&
2211.81Sjhawk		    	length($2) != 20 &&
2221.81Sjhawk		    	$2 !~ /^\$1/ &&
2231.81Sjhawk		    	$2 !~ /^\$2/ &&
2241.81Sjhawk		    	$2 != "" &&
2251.81Sjhawk			(permit_star || $2 != "*") &&
2261.81Sjhawk		    	$2 !~ /^\*[A-z-]+$/ &&
2271.81Sjhawk			$1 != "toor") {
2281.81Sjhawk		    	    if ($10 == "" || shells[$10])
2291.81Sjhawk				printf "Login %s is off but still has "\
2301.81Sjhawk				  "a valid shell (%s)\n", $1, $10;
2311.81Sjhawk		    } else if (! shells[$10])
2321.81Sjhawk		    	    printf "Login %s does not have a valid "\
2331.81Sjhawk			    "shell (%s)\n", $1, $10;
2341.81Sjhawk		}
2351.81Sjhawk		if ($3 == 0 && !uid0_users[$1] && !nowarn_users[$1])
2361.25Slukem			printf "Login %s has a user id of 0.\n", $1;
2371.15Smrg		if ($3 < 0)
2381.25Slukem			printf "Login %s has a negative user id.\n", $1;
2391.15Smrg		if ($4 < 0)
2401.25Slukem			printf "Login %s has a negative group id.\n", $1;
2411.15Smrg	}' < $MP > $OUTPUT
2421.15Smrg	if [ -s $OUTPUT ] ; then
2431.15Smrg		printf "\nChecking the $MP file:\n"
2441.15Smrg		cat $OUTPUT
2451.15Smrg	fi
2461.15Smrg
2471.15Smrg	awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
2481.15Smrg	if [ -s $OUTPUT ] ; then
2491.15Smrg		printf "\n$MP has duplicate user names.\n"
2501.15Smrg		column $OUTPUT
2511.15Smrg	fi
2521.15Smrg
2531.37Swrstuden# To not exclude 'toor', a standard duplicate root account, from the duplicate
2541.37Swrstuden# account test, uncomment the line below (without egrep in it)and comment
2551.37Swrstuden# out the line (with egrep in it) below it.
2561.37Swrstuden#
2571.37Swrstuden#	< $MPBYUID uniq -d -f 1 | awk '{ print $2 }' > $TMP2
2581.36Swrstuden	< $MPBYUID egrep -v '^toor ' | uniq -d -f 1 | awk '{ print $2 }' > $TMP2
2591.15Smrg	if [ -s $TMP2 ] ; then
2601.15Smrg		printf "\n$MP has duplicate user id's.\n"
2611.15Smrg		while read uid; do
2621.28Slukem			grep -w $uid $MPBYUID
2631.15Smrg		done < $TMP2 | column
2641.15Smrg	fi
2651.9Scgdfi
2661.9Scgd
2671.9Scgd# Check the group file syntax.
2681.32Slukem#
2691.31Slukemif checkyesno check_group; then
2701.15Smrg	GRP=/etc/group
2711.49Sjdolecek	awk -F: -v "len=$max_grouplen" '{
2721.15Smrg		if ($0 ~ /^[	 ]*$/) {
2731.25Slukem			printf "Line %d is a blank line.\n", NR;
2741.15Smrg			next;
2751.15Smrg		}
2761.34Sabs		if (NF != 4 && ($1 != "+" || NF != 1))
2771.25Slukem			printf "Line %d has the wrong number of fields.\n", NR;
2781.34Sabs		if ($1 == "+" )  {
2791.34Sabs			next;
2801.34Sabs		}
2811.53Satatat		if ($1 !~ /^[A-Za-z0-9]([-A-Za-z0-9]*[A-Za-z0-9])*$/)
2821.25Slukem			printf "Group %s has non-alphanumeric characters.\n",
2831.25Slukem			    $1;
2841.49Sjdolecek		if (length($1) > len)
2851.49Sjdolecek			printf "Group %s has more than "len" characters.\n", $1;
2861.15Smrg		if ($3 !~ /[0-9]*/)
2871.25Slukem			printf "Login %s has a negative group id.\n", $1;
2881.15Smrg	}' < $GRP > $OUTPUT
2891.15Smrg	if [ -s $OUTPUT ] ; then
2901.15Smrg		printf "\nChecking the $GRP file:\n"
2911.15Smrg		cat $OUTPUT
2921.15Smrg	fi
2931.15Smrg
2941.15Smrg	awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
2951.15Smrg	if [ -s $OUTPUT ] ; then
2961.15Smrg		printf "\n$GRP has duplicate group names.\n"
2971.15Smrg		column $OUTPUT
2981.15Smrg	fi
2991.9Scgdfi
3001.9Scgd
3011.9Scgd# Check for root paths, umask values in startup files.
3021.9Scgd# The check for the root paths is problematical -- it's likely to fail
3031.9Scgd# in other environments.  Once the shells have been modified to warn
3041.9Scgd# of '.' in the path, the path tests should go away.
3051.32Slukem#
3061.31Slukemif checkyesno check_rootdotfiles; then
3071.67Slukem	rhome=~root
3081.15Smrg	umaskset=no
3091.15Smrg	list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
3101.15Smrg	for i in $list ; do
3111.15Smrg		if [ -f $i ] ; then
3121.67Slukem			if egrep '^[ \t]*umask[ \t]+[0-7]+' $i > /dev/null ;
3131.67Slukem			then
3141.15Smrg				umaskset=yes
3151.15Smrg			fi
3161.63Slukem			# Double check the umask value itself; ensure that
3171.67Slukem			# both the group and other write bits are set.
3181.67Slukem			#
3191.45Ssommerfe			egrep '^[ \t]*umask[ \t]+[0-7]+' $i |
3201.63Slukem			awk '{
3211.67Slukem				if ($2 ~ /^.$/ || $2 ~! /[^2367].$/) {
3221.80Swiz					print "\tRoot umask is group writable"
3231.63Slukem				}
3241.67Slukem				if ($2 ~ /[^2367]$/) {
3251.80Swiz					print "\tRoot umask is other writable"
3261.63Slukem			    	}
3271.67Slukem			    }' | sort -u
3281.26Slukem			SAVE_PATH=$PATH
3291.26Slukem			unset PATH
3301.15Smrg			/bin/csh -f -s << end-of-csh > /dev/null 2>&1
3311.15Smrg				source $i
3321.15Smrg				/bin/ls -ldgT \$path > $TMP1
3331.9Scgdend-of-csh
3341.76Satatat			export PATH=$SAVE_PATH
3351.15Smrg			awk '{
3361.15Smrg				if ($10 ~ /^\.$/) {
3371.27Slukem					print "\tThe root path includes .";
3381.15Smrg					next;
3391.15Smrg				}
3401.15Smrg			     }
3411.15Smrg			     $1 ~ /^d....w/ \
3421.80Swiz		{ print "\tRoot path directory " $10 " is group writable." } \
3431.15Smrg			     $1 ~ /^d.......w/ \
3441.80Swiz		{ print "\tRoot path directory " $10 " is other writable." }' \
3451.67Slukem			< $TMP1
3461.15Smrg		fi
3471.67Slukem	done > $OUTPUT
3481.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
3491.27Slukem		printf "\nChecking root csh paths, umask values:\n$list\n\n"
3501.15Smrg		if [ -s $OUTPUT ]; then
3511.15Smrg			cat $OUTPUT
3521.15Smrg		fi
3531.15Smrg		if [ $umaskset = "no" ] ; then
3541.27Slukem		    printf "\tRoot csh startup files do not set the umask.\n"
3551.15Smrg		fi
3561.9Scgd	fi
3571.9Scgd
3581.15Smrg	umaskset=no
3591.23Slukem	list="/etc/profile ${rhome}/.profile"
3601.15Smrg	for i in $list; do
3611.15Smrg		if [ -f $i ] ; then
3621.15Smrg			if egrep umask $i > /dev/null ; then
3631.15Smrg				umaskset=yes
3641.15Smrg			fi
3651.15Smrg			egrep umask $i |
3661.67Slukem			awk '$2 ~ /^.$/ || $2 ~ /[^2367].$/ \
3671.80Swiz				{ print "\tRoot umask is group writable" } \
3681.67Slukem			     $2 ~ /[^2367]$/ \
3691.80Swiz				{ print "\tRoot umask is other writable" }'
3701.26Slukem			SAVE_PATH=$PATH
3711.26Slukem			unset PATH
3721.15Smrg			/bin/sh << end-of-sh > /dev/null 2>&1
3731.15Smrg				. $i
3741.26Slukem				list=\`echo \$PATH | /usr/bin/sed -e \
3751.26Slukem				    's/^:/.:/;s/:$/:./;s/::/:.:/g;s/:/ /g'\`
3761.15Smrg				/bin/ls -ldgT \$list > $TMP1
3771.9Scgdend-of-sh
3781.76Satatat			export PATH=$SAVE_PATH
3791.15Smrg			awk '{
3801.15Smrg				if ($10 ~ /^\.$/) {
3811.27Slukem					print "\tThe root path includes .";
3821.15Smrg					next;
3831.15Smrg				}
3841.15Smrg			     }
3851.15Smrg			     $1 ~ /^d....w/ \
3861.80Swiz		{ print "\tRoot path directory " $10 " is group writable." } \
3871.15Smrg			     $1 ~ /^d.......w/ \
3881.80Swiz		{ print "\tRoot path directory " $10 " is other writable." }' \
3891.67Slukem			< $TMP1
3901.9Scgd
3911.15Smrg		fi
3921.67Slukem	done > $OUTPUT
3931.15Smrg	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
3941.15Smrg		printf "\nChecking root sh paths, umask values:\n$list\n"
3951.15Smrg		if [ -s $OUTPUT ]; then
3961.15Smrg			cat $OUTPUT
3971.15Smrg		fi
3981.15Smrg		if [ $umaskset = "no" ] ; then
3991.27Slukem			printf "\tRoot sh startup files do not set the umask.\n"
4001.15Smrg		fi
4011.9Scgd	fi
4021.9Scgdfi
4031.9Scgd
4041.9Scgd# Root and uucp should both be in /etc/ftpusers.
4051.32Slukem#
4061.31Slukemif checkyesno check_ftpusers; then
4071.28Slukem	list="uucp "`awk '$2 == 0 { print $1 }' $MPBYUID`
4081.27Slukem	for i in $list; do
4091.29Slukem		if /usr/libexec/ftpd -C $i ; then
4101.67Slukem			printf "\t$i is not denied\n"
4111.27Slukem		fi
4121.67Slukem	done > $OUTPUT
4131.28Slukem	if [ -s $OUTPUT ]; then
4141.28Slukem		printf "\nChecking the /etc/ftpusers configuration:\n"
4151.28Slukem		cat $OUTPUT
4161.28Slukem	fi
4171.9Scgdfi
4181.9Scgd
4191.43Sitojun# Uudecode should not be in the /etc/mail/aliases file.
4201.32Slukem#
4211.31Slukemif checkyesno check_aliases; then
4221.43Sitojun	for f in /etc/mail/aliases /etc/aliases; do
4231.43Sitojun		if [ -f $f ] && egrep '^[^#]*(uudecode|decode).*\|' $f; then
4241.43Sitojun			printf "\nEntry for uudecode in $f file.\n"
4251.43Sitojun		fi
4261.43Sitojun	done
4271.9Scgdfi
4281.9Scgd
4291.9Scgd# Files that should not have + signs.
4301.32Slukem#
4311.31Slukemif checkyesno check_rhosts; then
4321.15Smrg	list="/etc/hosts.equiv /etc/hosts.lpd"
4331.15Smrg	for f in $list ; do
4341.15Smrg		if [ -f $f ] && egrep '\+' $f > /dev/null ; then
4351.15Smrg			printf "\nPlus sign in $f file.\n"
4361.15Smrg		fi
4371.15Smrg	done
4381.15Smrg
4391.15Smrg	# Check for special users with .rhosts files.  Only root and toor should
4401.16Smikel	# have .rhosts files.  Also, .rhosts files should not have plus signs.
4411.15Smrg	awk -F: '$1 != "root" && $1 != "toor" && \
4421.15Smrg		($3 < 100 || $1 == "ftp" || $1 == "uucp") \
4431.20Smycroft			{ print $1 " " $9 }' $MP |
4441.19Smycroft	sort -k2 |
4451.15Smrg	while read uid homedir; do
4461.15Smrg		if [ -f ${homedir}/.rhosts ] ; then
4471.15Smrg			rhost=`ls -ldgT ${homedir}/.rhosts`
4481.46Schristos			printf -- "$uid: $rhost\n"
4491.15Smrg		fi
4501.15Smrg	done > $OUTPUT
4511.15Smrg	if [ -s $OUTPUT ] ; then
4521.15Smrg		printf "\nChecking for special users with .rhosts files.\n"
4531.15Smrg		cat $OUTPUT
4541.15Smrg	fi
4551.15Smrg
4561.15Smrg	while read uid homedir; do
4571.35Sfair		if [ -f ${homedir}/.rhosts -a -r ${homedir}/.rhosts ] && \
4581.41Schristos		    cat -f ${homedir}/.rhosts | egrep '\+' > /dev/null ; then
4591.46Schristos			printf -- "$uid: + in .rhosts file.\n"
4601.15Smrg		fi
4611.29Slukem	done < $MPBYPATH > $OUTPUT
4621.15Smrg	if [ -s $OUTPUT ] ; then
4631.15Smrg		printf "\nChecking .rhosts files syntax.\n"
4641.15Smrg		cat $OUTPUT
4651.15Smrg	fi
4661.9Scgdfi
4671.9Scgd
4681.9Scgd# Check home directories.  Directories should not be owned by someone else
4691.80Swiz# or writable.
4701.32Slukem#
4711.31Slukemif checkyesno check_homes; then
4721.15Smrg	while read uid homedir; do
4731.15Smrg		if [ -d ${homedir}/ ] ; then
4741.15Smrg			file=`ls -ldgT ${homedir}`
4751.46Schristos			printf -- "$uid $file\n"
4761.9Scgd		fi
4771.29Slukem	done < $MPBYPATH |
4781.15Smrg	awk '$1 != $4 && $4 != "root" \
4791.15Smrg		{ print "user " $1 " home directory is owned by " $4 }
4801.15Smrg	     $2 ~ /^-....w/ \
4811.80Swiz		{ print "user " $1 " home directory is group writable" }
4821.15Smrg	     $2 ~ /^-.......w/ \
4831.80Swiz		{ print "user " $1 " home directory is other writable" }' \
4841.27Slukem	    > $OUTPUT
4851.15Smrg	if [ -s $OUTPUT ] ; then
4861.15Smrg		printf "\nChecking home directories.\n"
4871.15Smrg		cat $OUTPUT
4881.15Smrg	fi
4891.15Smrg
4901.15Smrg	# Files that should not be owned by someone else or readable.
4911.67Slukem	list=".Xauthority .netrc .ssh/id_dsa .ssh/id_rsa .ssh/identity"
4921.15Smrg	while read uid homedir; do
4931.15Smrg		for f in $list ; do
4941.15Smrg			file=${homedir}/${f}
4951.15Smrg			if [ -f $file ] ; then
4961.46Schristos				printf -- "$uid $f `ls -ldgT $file`\n"
4971.15Smrg			fi
4981.15Smrg		done
4991.29Slukem	done < $MPBYPATH |
5001.15Smrg	awk '$1 != $5 && $5 != "root" \
5011.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
5021.15Smrg	     $3 ~ /^-...r/ \
5031.15Smrg		{ print "user " $1 " " $2 " file is group readable" }
5041.15Smrg	     $3 ~ /^-......r/ \
5051.15Smrg		{ print "user " $1 " " $2 " file is other readable" }
5061.15Smrg	     $3 ~ /^-....w/ \
5071.80Swiz		{ print "user " $1 " " $2 " file is group writable" }
5081.15Smrg	     $3 ~ /^-.......w/ \
5091.80Swiz		{ print "user " $1 " " $2 " file is other writable" }' \
5101.27Slukem	    > $OUTPUT
5111.15Smrg
5121.80Swiz	# Files that should not be owned by someone else or writable.
5131.19Smycroft	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
5141.79Selric	      .cshrc .emacs .exrc .forward .history .k5login .klogin .login \
5151.79Selric	      .logout .profile .qmail .rc_history .rhosts .shosts ssh .tcshrc \
5161.79Selric	      .twmrc .xinitrc .xsession .ssh/authorized_keys \
5171.79Selric	      .ssh/authorized_keys2 .ssh/config .ssh/id_dsa.pub \
5181.79Selric	      .ssh/id_rsa.pub .ssh/identity.pub .ssh/known_hosts \
5191.79Selric	      .ssh/known_hosts2"
5201.15Smrg	while read uid homedir; do
5211.15Smrg		for f in $list ; do
5221.15Smrg			file=${homedir}/${f}
5231.15Smrg			if [ -f $file ] ; then
5241.46Schristos				printf -- "$uid $f `ls -ldgT $file`\n"
5251.15Smrg			fi
5261.15Smrg		done
5271.29Slukem	done < $MPBYPATH |
5281.15Smrg	awk '$1 != $5 && $5 != "root" \
5291.15Smrg		{ print "user " $1 " " $2 " file is owned by " $5 }
5301.15Smrg	     $3 ~ /^-....w/ \
5311.80Swiz		{ print "user " $1 " " $2 " file is group writable" }
5321.15Smrg	     $3 ~ /^-.......w/ \
5331.80Swiz		{ print "user " $1 " " $2 " file is other writable" }' \
5341.27Slukem	    >> $OUTPUT
5351.15Smrg	if [ -s $OUTPUT ] ; then
5361.15Smrg		printf "\nChecking dot files.\n"
5371.15Smrg		cat $OUTPUT
5381.15Smrg	fi
5391.9Scgdfi
5401.9Scgd
5411.9Scgd# Mailboxes should be owned by user and unreadable.
5421.32Slukem#
5431.31Slukemif checkyesno check_varmail; then
5441.63Slukem	ls -l /var/mail | \
5451.63Slukem	awk '	NR == 1 { next; }
5461.63Slukem	    	$3 != $9 {
5471.63Slukem			print "user " $9 " mailbox is owned by " $3
5481.63Slukem		}
5491.63Slukem		$1 != "-rw-------" {
5501.63Slukem			print "user " $9 " mailbox is " $1 ", group " $4
5511.63Slukem		}' > $OUTPUT
5521.15Smrg	if [ -s $OUTPUT ] ; then
5531.15Smrg		printf "\nChecking mailbox ownership.\n"
5541.15Smrg		cat $OUTPUT
5551.15Smrg	fi
5561.15Smrgfi
5571.15Smrg
5581.32Slukem# NFS exports shouldn't be globally exported
5591.32Slukem#
5601.32Slukemif checkyesno check_nfs && [ -f /etc/exports ]; then
5611.32Slukem	awk '{
5621.22Slukem		# ignore comments and blank lines
5631.39Shubertf		if ($0 ~ /^\#/ || $0 ~ /^$/ )
5641.22Slukem			next;
5651.22Slukem
5661.15Smrg		readonly = 0;
5671.15Smrg		for (i = 2; i <= NF; ++i) {
5681.15Smrg			if ($i ~ /-ro/)
5691.15Smrg				readonly = 1;
5701.15Smrg			else if ($i !~ /^-/)
5711.15Smrg				next;
5721.15Smrg		}
5731.15Smrg		if (readonly)
5741.15Smrg			print "File system " $1 " globally exported, read-only."
5751.15Smrg		else
5761.15Smrg			print "File system " $1 " globally exported, read-write."
5771.32Slukem	}' < /etc/exports > $OUTPUT
5781.32Slukem	if [ -s $OUTPUT ] ; then
5791.15Smrg		printf "\nChecking for globally exported file systems.\n"
5801.15Smrg		cat $OUTPUT
5811.15Smrg	fi
5821.9Scgdfi
5831.9Scgd
5841.9Scgd# Display any changes in setuid files and devices.
5851.32Slukem#
5861.31Slukemif checkyesno check_devices; then
5871.28Slukem	> $ERR
5881.15Smrg	(find / \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \
5891.74Slukem			-o -fstype null \
5901.15Smrg			-o -fstype procfs \) -a -prune -o \
5911.21Smycroft	    \( \( -perm -u+s -a ! -type d \) -o \
5921.21Smycroft	       \( -perm -g+s -a ! -type d \) -o \
5931.24Slukem	       -type b -o -type c \) -print0 | \
5941.24Slukem	xargs -0 ls -ldgTq | sort +9 > $LIST) 2> $OUTPUT
5951.15Smrg
5961.15Smrg	# Display any errors that occurred during system file walk.
5971.15Smrg	if [ -s $OUTPUT ] ; then
5981.28Slukem		printf "Setuid/device find errors:\n" >> $ERR
5991.28Slukem		cat $OUTPUT >> $ERR
6001.28Slukem		printf "\n" >> $ERR
6011.15Smrg	fi
6021.15Smrg
6031.15Smrg	# Display any changes in the setuid file list.
6041.15Smrg	egrep -v '^[bc]' $LIST > $TMP1
6051.15Smrg	if [ -s $TMP1 ] ; then
6061.15Smrg		# Check to make sure uudecode isn't setuid.
6071.15Smrg		if grep -w uudecode $TMP1 > /dev/null ; then
6081.28Slukem			printf "\nUudecode is setuid.\n" >> $ERR
6091.15Smrg		fi
6101.15Smrg
6111.67Slukem		file=$work_dir/setuid
6121.67Slukem		migrate_file "$backup_dir/setuid" "$file"
6131.67Slukem		CUR=${file}.current
6141.67Slukem		BACK=${file}.backup
6151.15Smrg		if [ -s $CUR ] ; then
6161.15Smrg			if cmp -s $CUR $TMP1 ; then
6171.15Smrg				:
6181.15Smrg			else
6191.15Smrg				> $TMP2
6201.15Smrg				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
6211.15Smrg				if [ -s $OUTPUT ] ; then
6221.28Slukem					printf "Setuid additions:\n" >> $ERR
6231.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6241.28Slukem					printf "\n" >> $ERR
6251.15Smrg				fi
6261.15Smrg
6271.15Smrg				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
6281.15Smrg				if [ -s $OUTPUT ] ; then
6291.28Slukem					printf "Setuid deletions:\n" >> $ERR
6301.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6311.28Slukem					printf "\n" >> $ERR
6321.15Smrg				fi
6331.15Smrg
6341.20Smycroft				sort -k10 $TMP2 $CUR $TMP1 | \
6351.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
6361.27Slukem				    uniq -u > $OUTPUT
6371.15Smrg				if [ -s $OUTPUT ] ; then
6381.28Slukem					printf "Setuid changes:\n" >> $ERR
6391.28Slukem					column -t $OUTPUT >> $ERR
6401.28Slukem					printf "\n" >> $ERR
6411.15Smrg				fi
6421.9Scgd
6431.52Satatat				backup_file update $TMP1 $CUR $BACK
6441.9Scgd			fi
6451.15Smrg		else
6461.28Slukem			printf "Setuid additions:\n" >> $ERR
6471.28Slukem			column -t $TMP1 >> $ERR
6481.28Slukem			printf "\n" >> $ERR
6491.52Satatat			backup_file add $TMP1 $CUR $BACK
6501.9Scgd		fi
6511.15Smrg	fi
6521.15Smrg
6531.27Slukem	# Check for block and character disk devices that are readable or
6541.80Swiz	# writable or not owned by root.operator.
6551.15Smrg	>$TMP1
6561.61Slukem	DISKLIST="ccd ch hk hp ld md ra raid rb rd rl rx \
6571.57Ssimonb	    sd se ss uk up vnd wd xd xy"
6581.27Slukem#	DISKLIST="$DISKLIST ct mt st wt"
6591.15Smrg	for i in $DISKLIST; do
6601.15Smrg		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
6611.15Smrg		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
6621.15Smrg	done
6631.15Smrg
6641.15Smrg	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
6651.25Slukem		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
6661.25Slukem		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
6671.15Smrg	if [ -s $OUTPUT ] ; then
6681.28Slukem		printf "\nChecking disk ownership and permissions.\n" >> $ERR
6691.28Slukem		cat $OUTPUT >> $ERR
6701.28Slukem		printf "\n" >> $ERR
6711.9Scgd	fi
6721.9Scgd
6731.15Smrg	# Display any changes in the device file list.
6741.20Smycroft	egrep '^[bc]' $LIST | sort -k11 > $TMP1
6751.15Smrg	if [ -s $TMP1 ] ; then
6761.67Slukem		file=$work_dir/device
6771.67Slukem		migrate_file "$backup_dir/device" "$file"
6781.67Slukem		CUR=${file}.current
6791.67Slukem		BACK=${file}.backup
6801.15Smrg
6811.15Smrg		if [ -s $CUR ] ; then
6821.15Smrg			if cmp -s $CUR $TMP1 ; then
6831.15Smrg				:
6841.15Smrg			else
6851.15Smrg				> $TMP2
6861.15Smrg				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
6871.15Smrg				if [ -s $OUTPUT ] ; then
6881.28Slukem					printf "Device additions:\n" >> $ERR
6891.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6901.28Slukem					printf "\n" >> $ERR
6911.15Smrg				fi
6921.15Smrg
6931.15Smrg				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
6941.15Smrg				if [ -s $OUTPUT ] ; then
6951.28Slukem					printf "Device deletions:\n" >> $ERR
6961.28Slukem					tee -a $TMP2 < $OUTPUT >> $ERR
6971.28Slukem					printf "\n" >> $ERR
6981.15Smrg				fi
6991.15Smrg
7001.27Slukem				# Report any block device change. Ignore
7011.27Slukem				# character devices, only the name is
7021.27Slukem				# significant.
7031.15Smrg				cat $TMP2 $CUR $TMP1 | \
7041.27Slukem				    sed -e '/^c/d' | \
7051.27Slukem				    sort -k11 | \
7061.27Slukem				    sed -e 's/[	 ][	 ]*/ /g' | \
7071.27Slukem				    uniq -u > $OUTPUT
7081.15Smrg				if [ -s $OUTPUT ] ; then
7091.28Slukem					printf "Block device changes:\n" >> $ERR
7101.28Slukem					column -t $OUTPUT >> $ERR
7111.28Slukem					printf "\n" >> $ERR
7121.15Smrg				fi
7131.9Scgd
7141.52Satatat				backup_file update $TMP1 $CUR $BACK
7151.9Scgd			fi
7161.15Smrg		else
7171.28Slukem			printf "Device additions:\n" >> $ERR
7181.28Slukem			column -t $TMP1 >> $ERR
7191.28Slukem			printf "\n" >> $ERR
7201.52Satatat			backup_file add $TMP1 $CUR $BACK >> $ERR
7211.9Scgd		fi
7221.28Slukem	fi
7231.28Slukem	if [ -s $ERR ] ; then
7241.28Slukem		printf "\nChecking setuid files and devices:\n"
7251.28Slukem		cat $ERR
7261.28Slukem		printf "\n"
7271.9Scgd	fi
7281.9Scgdfi
7291.9Scgd
7301.9Scgd# Check special files.
7311.9Scgd# Check system binaries.
7321.9Scgd#
7331.9Scgd# Create the mtree tree specifications using:
7341.67Slukem#	mtree -cx -pDIR -kmd5,uid,gid,mode,nlink,size,link,time > DIR.secure
7351.38Skleink#	chown root:wheel DIR.secure
7361.67Slukem#	chmod u+r,go= DIR.secure
7371.9Scgd#
7381.9Scgd# Note, this is not complete protection against Trojan horsed binaries, as
7391.9Scgd# the hacker can modify the tree specification to match the replaced binary.
7401.9Scgd# For details on really protecting yourself against modified binaries, see
7411.9Scgd# the mtree(8) manual page.
7421.32Slukem#
7431.31Slukemif checkyesno check_mtree; then
7441.82Sjhawk	if checkyesno check_mtree_follow_symlinks; then
7451.82Sjhawk		check_mtree_flags="-L"
7461.82Sjhawk	else
7471.82Sjhawk		check_mtree_flags=""
7481.82Sjhawk	fi
7491.67Slukem	for file in $special_files; do
7501.67Slukem		[ ! -s $file ] && continue
7511.82Sjhawk		mtree -e -l -p / $check_mtree_flags -f $file
7521.67Slukem	done > $OUTPUT
7531.15Smrg	if [ -s $OUTPUT ]; then
7541.9Scgd		printf "\nChecking special files and directories.\n"
7551.9Scgd		cat $OUTPUT
7561.9Scgd	fi
7571.9Scgd
7581.16Smikel	for file in /etc/mtree/*.secure; do
7591.16Smikel		[ $file = '/etc/mtree/*.secure' ] && continue
7601.9Scgd		tree=`sed -n -e '3s/.* //p' -e 3q $file`
7611.82Sjhawk		mtree $check_mtree_flags -f $file -p $tree > $TMP1
7621.9Scgd		if [ -s $TMP1 ]; then
7631.67Slukem			printf "\nChecking $tree:\n"
7641.67Slukem			cat $TMP1
7651.9Scgd		fi
7661.67Slukem	done > $OUTPUT
7671.15Smrg	if [ -s $OUTPUT ]; then
7681.9Scgd		printf "\nChecking system binaries:\n"
7691.9Scgd		cat $OUTPUT
7701.9Scgd	fi
7711.9Scgdfi
7721.9Scgd
7731.32Slukem# Backup disklabels of available disks
7741.32Slukem#
7751.32Slukemif checkyesno check_disklabels; then
7761.67Slukem		# migrate old disklabels
7771.67Slukem	for file in `ls -1d $backup_dir/$backup_dir/disklabel.* \
7781.67Slukem	    $backup_dir/disklabel.* 2>/dev/null`; do
7791.67Slukem		migrate_file "$file" "$work_dir/${file##*/}"
7801.67Slukem	done
7811.67Slukem
7821.67Slukem		# generate list of old disklabels & fdisks and remove them
7831.67Slukem	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* 2>/dev/null |
7841.52Satatat	    egrep -v '\.(backup|current)(,v)?$' > $LABELS
7851.32Slukem	xargs rm < $LABELS
7861.32Slukem
7871.67Slukem		# generate disklabels of all disks excluding:	cd fd md
7881.63Slukem	disks=`iostat -x | awk 'NR > 1 && $1 !~ /^[cfm]d/ { print $1; }'`
7891.32Slukem	for i in $disks; do
7901.67Slukem		disklabel $i > "$work_dir/disklabel.$i" 2>/dev/null
7911.32Slukem	done
7921.32Slukem
7931.67Slukem		# if fdisk is available, generate fdisks for:	ed ld sd wd
7941.67Slukem	if [ -x /sbin/fdisk ]; then
7951.67Slukem		disks=`iostat -x| awk 'NR > 1 && $1 ~ /^[elsw]d/ { print $1; }'`
7961.67Slukem		for i in $disks; do
7971.67Slukem			/sbin/fdisk $i > "$work_dir/fdisk.$i" 2>/dev/null
7981.67Slukem		done
7991.67Slukem	fi
8001.67Slukem
8011.67Slukem		# append list of new disklabels and fdisks
8021.67Slukem	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* 2>/dev/null |
8031.52Satatat	    egrep -v '\.(backup|current)(,v)?$' >> $LABELS
8041.62Satatat	CHANGELIST="$LABELS $CHANGELIST"
8051.62Satatatfi
8061.62Satatat
8071.62Satatat# Check for changes in the list of installed pkgs
8081.62Satatat#
8091.65Slukemif checkyesno check_pkgs && [ -d $pkgdb_dir ]; then
8101.67Slukem	pkgs=$work_dir/pkgs
8111.67Slukem	migrate_file "$backup_dir/pkgs" "$pkgs"
8121.65Slukem	(	cd $pkgdb_dir
8131.62Satatat		pkg_info | sort
8141.62Satatat		echo ""
8151.62Satatat		find . \( -name +REQUIRED_BY -o -name +CONTENTS \) -print0 |
8161.72Slukem			xargs -0 ls -ldgTq | sort -t. +1 | sed -e 's, \./, ,'
8171.62Satatat	 ) > $pkgs
8181.67Slukem	echo "$pkgs" > $PKGS
8191.62Satatat	CHANGELIST="$PKGS $CHANGELIST"
8201.32Slukemfi
8211.32Slukem
8221.67Slukem# List of files that get backed up and checked for any modifications.
8231.9Scgd# Any changes cause the files to rotate.
8241.32Slukem#
8251.67Slukemif checkyesno check_changelist ; then
8261.67Slukem	for file in $special_files; do
8271.67Slukem		[ ! -s $file ] && continue
8281.67Slukem		mtree -D -k type -f $file -E exclude |
8291.67Slukem		    sed '/^type=file/!d ; s/type=file \.//'
8301.67Slukem	done > $CHANGEFILES
8311.67Slukem
8321.75Slukem	(
8331.68Slukem		# Add other files which might dynamically exist:
8341.67Slukem		#	/etc/ifconfig.*
8351.67Slukem		#	/etc/raid*.conf
8361.68Slukem		#	/etc/rc.d/*
8371.67Slukem		#	/etc/rc.conf.d/*
8381.68Slukem		#
8391.75Slukem		echo "/etc/ifconfig.*"
8401.75Slukem		echo "/etc/raid*.conf"
8411.75Slukem		echo "/etc/rc.d/*"
8421.75Slukem		echo "/etc/rc.conf.d/*"
8431.67Slukem
8441.68Slukem		# Add /etc/changelist
8451.68Slukem		#
8461.75Slukem		if [ -s /etc/changelist ]; then
8471.75Slukem			grep -v '^#' /etc/changelist
8481.75Slukem		fi
8491.75Slukem	) | while read file; do
8501.75Slukem		case "$file" in
8511.75Slukem		*[\*\?\[]*)	# If changelist line is a glob ...
8521.75Slukem				# ... expand possible backup files
8531.75Slukem				#
8541.75Slukem			ls -1d $(echo $backup_dir/${file}.current) 2>/dev/null \
8551.75Slukem			    | sed "s,^$backup_dir/,, ; s,\.current$,,"
8561.75Slukem				
8571.75Slukem				# ... expand possible files
8581.75Slukem				#
8591.75Slukem			ls -1d $(echo $file) 2>/dev/null
8601.75Slukem			;;
8611.75Slukem		*)
8621.75Slukem				# Otherwise, just print the filename
8631.75Slukem			echo $file
8641.75Slukem			;;
8651.75Slukem		esac
8661.75Slukem	done >> $CHANGEFILES
8671.67Slukem	CHANGELIST="$CHANGEFILES $CHANGELIST"
8681.67Slukemfi
8691.67Slukem
8701.67Slukem# Special case backups, including the master password file and
8711.67Slukem# ssh private host keys. The normal backup mechanisms for
8721.67Slukem# $check_changelist (see below) also print out the actual file
8731.67Slukem# differences and we don't want to do that for these files
8741.67Slukem#
8751.67Slukemecho $MP > $TMP1			# always add /etc/master.passwd
8761.67Slukemfor file in $special_files; do
8771.67Slukem	[ ! -s $file ] && continue
8781.70Slukem	mtree -D -k type -f $file -I nodiff |
8791.67Slukem	    sed '/^type=file/!d ; s/type=file \.//'
8801.67Slukemdone >> $TMP1
8811.73Slukemgrep -v '^$' $TMP1 | sort -u > $TMP2
8821.68Slukem
8831.69Slukemwhile read file; do
8841.67Slukem	backup_and_diff "$file" no
8851.69Slukemdone < $TMP2
8861.67Slukem
8871.32Slukem
8881.32Slukemif [ -n "$CHANGELIST" ]; then
8891.73Slukem	grep -h -v '^$' $CHANGELIST | sort -u > $TMP1
8901.68Slukem	comm -23 $TMP1 $TMP2 | while read file; do
8911.67Slukem		backup_and_diff "$file" yes
8921.9Scgd	done
8931.44Sadfi
8941.44Sad
8951.44Sadif [ -f /etc/security.local ]; then
8961.84Sjhawk	. /etc/security.local > $OUTPUT
8971.84Sjhawk	if [ -s $OUTPUT ] ; then
8981.84Sjhawk		printf "\nRunning /etc/security.local:\n"
8991.84Sjhawk		cat $OUTPUT
9001.84Sjhawk	fi
9011.9Scgdfi
902