Home | History | Annotate | Line # | Download | only in etc
      1 #!/bin/sh -
      2 #
      3 #	$NetBSD: security,v 1.132 2024/07/21 14:56:16 he Exp $
      4 #	from: @(#)security	8.1 (Berkeley) 6/9/93
      5 #
      6 
      7 PATH=/sbin:/usr/sbin:/bin:/usr/bin
      8 
      9 rcvar_manpage='security.conf(5)'
     10 
     11 if [ -f /etc/rc.subr ]; then
     12 	. /etc/rc.subr
     13 else
     14 	echo "Can't read /etc/rc.subr; aborting."
     15 	exit 1;
     16 fi
     17 
     18 umask 077
     19 TZ=UTC; export TZ
     20 
     21 if [ -s /etc/security.conf ]; then
     22 	. /etc/security.conf
     23 fi
     24 if [ -s /etc/pkgpath.conf ]; then
     25 	. /etc/pkgpath.conf
     26 fi
     27 
     28 # Set reasonable defaults (if they're not set in security.conf)
     29 #
     30 backup_dir=${backup_dir:-/var/backups}
     31 max_loginlen=${max_loginlen:-8}
     32 max_grouplen=${max_grouplen:-8}
     33 pkg_admin=${pkg_admin:-/usr/sbin/pkg_admin}
     34 pkg_info=${pkg_info:-/usr/sbin/pkg_info}
     35 
     36 # Other configurable variables
     37 #
     38 special_files="/etc/mtree/special /etc/mtree/special.local"
     39 MP=/etc/master.passwd
     40 CHANGELIST=""
     41 work_dir=$backup_dir/work
     42 
     43 if [ ! -d "$work_dir" ]; then
     44 	mkdir -p "$work_dir"
     45 fi
     46 
     47 SECUREDIR=$(mktemp -d -t _securedir) || exit 1
     48 
     49 trap "/bin/rm -rf $SECUREDIR ; exit 0" EXIT INT QUIT PIPE
     50 
     51 if ! cd "$SECUREDIR"; then
     52 	echo "Can not cd to $SECUREDIR".
     53 	exit 1
     54 fi
     55 
     56 ERR=err.$$
     57 TMP1=tmp1.$$
     58 TMP2=tmp2.$$
     59 MPBYUID=mpbyuid.$$
     60 MPBYPATH=mpbypath.$$
     61 LIST=list.$$
     62 OUTPUT=output.$$
     63 LABELS=labels.$$
     64 LVM_LABELS=lvm.$$
     65 PKGS=pkgs.$$
     66 CHANGEFILES=changefiles.$$
     67 SPECIALSPEC=specialspec.$$
     68 
     69 if [ -n "${pkgdb_dir}" ]; then
     70 	echo "WARNING: Setting pkgdb_dir in security.conf(5) is deprecated"
     71 	echo "WARNING: Please define PKG_DBDIR in pkg_install.conf(5) instead"
     72 	_compat_K_flag="-K ${pkgdb_dir}"
     73 fi
     74 
     75 have_pkgs() {
     76 	$pkg_info ${_compat_K_flag} -q -E '*'
     77 }
     78 
     79 # migrate_file old new
     80 #	Determine if the "${old}" path name needs to be migrated to the
     81 #	"${new}" path. Also checks if "${old}.current" needs migrating,
     82 #	and if so, migrate it and possibly "${old}.current,v" and
     83 #	"${old}.backup".
     84 #
     85 migrate_file()
     86 {
     87 	_old=$1
     88 	_new=$2
     89 	if [ -z "$_old" ] || [ -z "$_new" ]; then
     90 		err 3 "USAGE: migrate_file old new"
     91 	fi
     92 	if [ ! -d "${_new%/*}" ]; then
     93 		mkdir -p "${_new%/*}"
     94 	fi
     95 	if [ -f "${_old}" ] && ! [ -f "${_new}" ]; then
     96 		echo "==> migrating ${_old}"
     97 		echo "           to ${_new}"
     98 		mv "${_old}" "${_new}"
     99 	fi
    100 	if [ -f "${_old}.current" ] && ! [ -f "${_new}.current" ]; then
    101 		echo "==> migrating ${_old}.current"
    102 		echo "           to ${_new}.current"
    103 		mv "${_old}.current" "${_new}.current"
    104 		if [ -f "${_old}.current,v" ] &&
    105 		 ! [ -f "${_new}.current,v" ]; then
    106 			echo "==> migrating ${_old}.current,v"
    107 			echo "           to ${_new}.current,v"
    108 			mv "${_old}.current,v" "${_new}.current,v"
    109 		fi
    110 		if [ -f "${_old}.backup" ] && ! [ -f "${_new}.backup" ]; then
    111 			echo "==> migrating ${_old}.backup"
    112 			echo "           to ${_new}.backup"
    113 			mv "${_old}.backup" "${_new}.backup"
    114 		fi
    115 	fi
    116 }
    117 
    118 
    119 # backup_and_diff file printdiff
    120 #	Determine if file needs backing up, and if so, do it.
    121 #	If printdiff is yes, display the diffs, otherwise
    122 #	just print a message saying "[changes omitted]".
    123 #
    124 backup_and_diff()
    125 {
    126 	_file=$1
    127 	_printdiff=$2
    128 	if [ -z "$_file" ] || [ -z "$_printdiff" ]; then
    129 		err 3 "USAGE: backup_and_diff file printdiff"
    130 	fi
    131 	! checkyesno _printdiff
    132 	_printdiff=$?
    133 
    134 	_old=$backup_dir/${_file##*/}
    135 	case "$_file" in
    136 	$work_dir/*)
    137 		_new=$_file
    138 		migrate_file "$backup_dir/$_old" "$_new"
    139 		migrate_file "$_old" "$_new"
    140 		;;
    141 	*)
    142 		_new=$backup_dir/$_file
    143 		migrate_file "$_old" "$_new"
    144 		;;
    145 	esac
    146 	CUR=${_new}.current
    147 	BACK=${_new}.backup
    148 	if [ -f $_file ]; then
    149 		if [ -f $CUR ] ; then
    150 			if [ "$_printdiff" -ne 0 ]; then
    151 				diff ${diff_options} $CUR $_file > $OUTPUT
    152 			else
    153 				if ! cmp -s $CUR $_file; then
    154 					echo "[changes omitted]"
    155 				fi > $OUTPUT
    156 			fi
    157 			if [ -s $OUTPUT ] ; then
    158 				printf \
    159 			"\n======\n%s diffs (OLD < > NEW)\n======\n" $_file
    160 				cat $OUTPUT
    161 				backup_file update $_file $CUR $BACK
    162 			fi
    163 		else
    164 			printf "\n======\n%s added\n======\n" $_file
    165 			if [ "$_printdiff" -ne 0 ]; then
    166 				diff ${diff_options} /dev/null $_file
    167 			else
    168 				echo "[changes omitted]"
    169 			fi
    170 			backup_file add $_file $CUR $BACK
    171 		fi
    172 	else
    173 		if [ -f $CUR ]; then
    174 			printf "\n======\n%s removed\n======\n" $_file
    175 			if [ "$_printdiff" -ne 0 ]; then
    176 				diff ${diff_options} $CUR /dev/null
    177 			else
    178 				echo "[changes omitted]"
    179 			fi
    180 			backup_file remove $_file $CUR $BACK
    181 		fi
    182 	fi
    183 }
    184 
    185 
    186 # These are used several times.
    187 #
    188 awk -F: '!/^\+/ { print $1 " " $3 }' $MP | sort -k2n > $MPBYUID
    189 awk -F: '{ print $1 " " $9 }' $MP | sort -k2 > $MPBYPATH
    190 for file in $special_files; do
    191 	[ -s $file ] && cat $file
    192 done | mtree -CM -k all > $SPECIALSPEC || exit 1
    193 
    194 
    195 # Check for enough entropy.
    196 #
    197 if checkyesno check_entropy; then
    198 	if [ "$(sysctl -n kern.entropy.needed)" != 0 ]; then
    199 		printf '\n'
    200 		printf 'Entropy:\n'
    201 		printf 'System may need more entropy for cryptography.\n'
    202 		printf 'See the entropy(7) man page for details.\n'
    203 	fi
    204 fi
    205 
    206 
    207 # Check the master password file syntax.
    208 #
    209 if checkyesno check_passwd; then
    210 	# XXX: the sense of permit_star is reversed; the code works as
    211 	# implemented, but usage needs to be negated.
    212 	checkyesno check_passwd_permit_star && permit_star=0 || permit_star=1
    213 	checkyesno check_passwd_permit_nonalpha \
    214 		 && permit_nonalpha=1 || permit_nonalpha=0
    215 
    216 	awk -v "len=$max_loginlen" \
    217 	    -v "nowarn_shells_list=$check_passwd_nowarn_shells" \
    218 	    -v "nowarn_users_list=$check_passwd_nowarn_users" \
    219 	    -v "permit_star=$permit_star" \
    220 	    -v "permit_nonalpha=$permit_nonalpha" \
    221 	'
    222 	BEGIN {
    223 		while ( getline < "/etc/shells" > 0 ) {
    224 			if ($0 ~ /^\#/ || $0 ~ /^$/ )
    225 				continue;
    226 			shells[$1]++;
    227 		}
    228 		split(nowarn_shells_list, a);
    229 		for (i in a) nowarn_shells[a[i]]++;
    230 		split(nowarn_users_list, a);
    231 		for (i in a) nowarn_users[a[i]]++;
    232 		uid0_users_list="root toor"
    233 		split(uid0_users_list, a);
    234 		for (i in a) uid0_users[a[i]]++;
    235 		FS=":";
    236 	}
    237 
    238 	{
    239 		if ($0 ~ /^[	 ]*$/) {
    240 			printf "Line %d is a blank line.\n", NR;
    241 			next;
    242 		}
    243 
    244 		# NIS compat entry?
    245 		compatline = $1 ~ "^[\\+-]";
    246 		if (compatline) {
    247 			if ($1 == "+" && NF == 1) {
    248 				next;
    249 			}
    250 			sub("^.", "", $1);
    251 		}
    252 		if (NF != 10)
    253 			printf "Line %d has the wrong number of fields.\n", NR;
    254 		if (compatline)  {
    255 			if ($3 == 0)
    256 			    printf "Line %d includes entries with uid 0.\n",
    257 			        NR;
    258 			if ($1 == "")
    259 			    next;
    260 		}
    261 		if (!permit_nonalpha &&
    262 		    $1 !~ /^[_A-Za-z0-9]([-A-Za-z0-9_.]*[A-Za-z0-9])*$/)
    263 			printf "Login %s has non-alphanumeric characters.\n",
    264 			    $1;
    265 		if (length($1) > len)
    266 			printf "Login %s has more than "len" characters.\n",
    267 			    $1;
    268 		if ($2 == "" && !compatline && !nowarn_users[$1])
    269 			    printf "Login %s has no password.\n", $1;
    270 		if (!nowarn_shells[$10] && !nowarn_users[$1]) {
    271 		    if (length($2) != 13 &&
    272 		    	length($2) != 20 &&
    273 		    	$2 !~ /^\$1/ &&
    274 		    	$2 !~ /^\$2/ &&
    275 			$2 !~ /^\$sha1/ &&
    276 			$2 !~ /^\$argon2(i|d|id)/ &&
    277 		    	$2 != "" &&
    278 			(permit_star || $2 != "*") &&
    279 		    	$2 !~ /^\*[A-z-]+$/ &&
    280 			$1 != "toor") {
    281 		    	    if ($10 == "" || shells[$10])
    282 				printf "Login %s is off but still has "\
    283 				  "a valid shell (%s)\n", $1, $10;
    284 		    } else if (compatline && $10 == "") {
    285 			    # nothing
    286 		    } else if (! shells[$10])
    287 		    	    printf "Login %s does not have a valid "\
    288 			    "shell (%s)\n", $1, $10;
    289 		}
    290 		if ($3 == 0 && !uid0_users[$1] && !nowarn_users[$1])
    291 			printf "Login %s has a user id of 0.\n", $1;
    292 		if ($3 != "" && $3 < 0)
    293 			printf "Login %s has a negative user id.\n", $1;
    294 		if ($4 != "" && $4 < 0)
    295 			printf "Login %s has a negative group id.\n", $1;
    296 	}' < $MP > $OUTPUT
    297 	if [ -s $OUTPUT ] ; then
    298 		printf "\nChecking the $MP file:\n"
    299 		cat $OUTPUT
    300 	fi
    301 
    302 	awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
    303 	if [ -s $OUTPUT ] ; then
    304 		printf "\n$MP has duplicate user names.\n"
    305 		column $OUTPUT
    306 	fi
    307 
    308 	awk -v "permit_dups_list=$check_passwd_permit_dups" \
    309 	'
    310 	BEGIN {
    311 		split(permit_dups_list, a);
    312 		for (i in a) permit_dups[a[i]]++;
    313 	}
    314 	{
    315 		if (!permit_dups[$1])
    316 			print $2;
    317 	}' < $MPBYUID | uniq -d > $TMP2
    318 	if [ -s $TMP2 ] ; then
    319 		printf "\n$MP has duplicate user ids.\n"
    320 		while read uid; do
    321 			grep -w $uid $MPBYUID
    322 		done < $TMP2 | column
    323 	fi
    324 fi
    325 
    326 # Check the group file syntax.
    327 #
    328 if checkyesno check_group; then
    329 	GRP=/etc/group
    330 	awk -F: -v "len=$max_grouplen" '{
    331 		if ($0 ~ /^[	 ]*$/) {
    332 			printf "Line %d is a blank line.\n", NR;
    333 			next;
    334 		}
    335 		if (NF != 4 && ($1 != "+" || NF != 1))
    336 			printf "Line %d has the wrong number of fields.\n", NR;
    337 		if ($1 == "+" )  {
    338 			next;
    339 		}
    340 		if ($1 !~ /^[_A-Za-z0-9]([-A-Za-z0-9_.]*[A-Za-z0-9])*$/)
    341 			printf "Group %s has non-alphanumeric characters.\n",
    342 			    $1;
    343 		if (length($1) > len)
    344 			printf "Group %s has more than "len" characters.\n", $1;
    345 		if ($3 !~ /[0-9]*/)
    346 			printf "Login %s has a negative group id.\n", $1;
    347 	}' < $GRP > $OUTPUT
    348 	if [ -s $OUTPUT ] ; then
    349 		printf "\nChecking the $GRP file:\n"
    350 		cat $OUTPUT
    351 	fi
    352 
    353 	awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
    354 	dupgroups=""
    355 	for group in $(cat $OUTPUT) ; do
    356 		gcount=$(awk -F: "/$group/ { print \$1,\$3 }" $GRP |
    357 			sort -u | wc -l)
    358 		if [ $gcount -gt 1 ]; then
    359 			dupgroups="$dupgroups $group"
    360 		fi
    361 	done
    362 	if [ ! -z "$dupgroups" ] ; then
    363 		printf "\n$GRP has duplicate group names.\n"
    364 		printf "$dupgroups\n"
    365 	fi
    366 fi
    367 
    368 # Check for root paths, umask values in startup files.
    369 # The check for the root paths is problematical -- it's likely to fail
    370 # in other environments.  Once the shells have been modified to warn
    371 # of '.' in the path, the path tests should go away.
    372 #
    373 if checkyesno check_rootdotfiles; then
    374 	rhome=~root
    375 	umaskset=no
    376 	list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
    377 	for i in $list ; do
    378 		if [ -f $i ] ; then
    379 			if egrep '^[ \t]*umask[ \t]+[0-7]+' $i > /dev/null ;
    380 			then
    381 				umaskset=yes
    382 			fi
    383 			# Double check the umask value itself; ensure that
    384 			# both the group and other write bits are set.
    385 			#
    386 			egrep '^[ \t]*umask[ \t]+[0-7]+' $i |
    387 			awk '{
    388 				if ($2 ~ /^.$/ || $2 ~! /[^2367].$/) {
    389 					print "\tRoot umask is group writable"
    390 				}
    391 				if ($2 ~ /[^2367]$/) {
    392 					print "\tRoot umask is other writable"
    393 			    	}
    394 			    }' | sort -u
    395 			SAVE_PATH=$PATH
    396 			unset PATH
    397 			/bin/csh -f -s << end-of-csh > /dev/null 2>&1
    398 				source $i
    399 				/bin/ls -ldgT \$path > $TMP1
    400 end-of-csh
    401 			export PATH=$SAVE_PATH
    402 			awk '{
    403 				if ($10 ~ /^\.$/) {
    404 					print "\tThe root path includes .";
    405 					next;
    406 				}
    407 			     }
    408 			     $1 ~ /^d....w/ \
    409 		{ print "\tRoot path directory " $10 " is group writable." } \
    410 			     $1 ~ /^d.......w/ \
    411 		{ print "\tRoot path directory " $10 " is other writable." }' \
    412 			< $TMP1
    413 		fi
    414 	done > $OUTPUT
    415 	if [ $umaskset = no ] || [ -s $OUTPUT ] ; then
    416 		printf "\nChecking root csh paths, umask values:\n$list\n\n"
    417 		if [ -s $OUTPUT ]; then
    418 			cat $OUTPUT
    419 		fi
    420 		if [ $umaskset = no ] ; then
    421 		    printf "\tRoot csh startup files do not set the umask.\n"
    422 		fi
    423 	fi
    424 
    425 	umaskset=no
    426 	list="/etc/profile ${rhome}/.profile"
    427 	for i in $list; do
    428 		if [ -f $i ] ; then
    429 			if egrep umask $i > /dev/null ; then
    430 				umaskset=yes
    431 			fi
    432 			egrep umask $i |
    433 			awk '$2 ~ /^.$/ || $2 ~ /[^2367].$/ \
    434 				{ print "\tRoot umask is group writable" } \
    435 			     $2 ~ /[^2367]$/ \
    436 				{ print "\tRoot umask is other writable" }'
    437 			SAVE_PATH=$PATH
    438 			unset PATH
    439 			/bin/sh << end-of-sh > /dev/null 2>&1
    440 				. $i
    441 				list=\$(echo \$PATH | /usr/bin/sed -e \
    442 				    's/^:/.:/;s/:$/:./;s/::/:.:/g;s/:/ /g')
    443 				/bin/ls -ldgT \$list > $TMP1
    444 end-of-sh
    445 			export PATH=$SAVE_PATH
    446 			awk '{
    447 				if ($10 ~ /^\.$/) {
    448 					print "\tThe root path includes .";
    449 					next;
    450 				}
    451 			     }
    452 			     $1 ~ /^d....w/ \
    453 		{ print "\tRoot path directory " $10 " is group writable." } \
    454 			     $1 ~ /^d.......w/ \
    455 		{ print "\tRoot path directory " $10 " is other writable." }' \
    456 			< $TMP1
    457 
    458 		fi
    459 	done > $OUTPUT
    460 	if [ $umaskset = no ] || [ -s $OUTPUT ] ; then
    461 		printf "\nChecking root sh paths, umask values:\n$list\n"
    462 		if [ -s $OUTPUT ]; then
    463 			cat $OUTPUT
    464 		fi
    465 		if [ $umaskset = no ] ; then
    466 			printf "\tRoot sh startup files do not set the umask.\n"
    467 		fi
    468 	fi
    469 fi
    470 
    471 # Root and uucp should both be in /etc/ftpusers.
    472 #
    473 if checkyesno check_ftpusers; then
    474 	list="uucp "$(awk '$2 == 0 { print $1 }' $MPBYUID)
    475 	for i in $list; do
    476 		if /usr/libexec/ftpd -C $i ; then
    477 			printf "\t$i is not denied\n"
    478 		fi
    479 	done > $OUTPUT
    480 	if [ -s $OUTPUT ]; then
    481 		printf "\nChecking the /etc/ftpusers configuration:\n"
    482 		cat $OUTPUT
    483 	fi
    484 fi
    485 
    486 # Uudecode should not be in the /etc/mail/aliases file.
    487 #
    488 if checkyesno check_aliases; then
    489 	for f in /etc/mail/aliases /etc/aliases; do
    490 		if [ -f $f ] && egrep '^[^#]*(uudecode|decode).*\|' $f; then
    491 			printf "\nEntry for uudecode in $f file.\n"
    492 		fi
    493 	done
    494 fi
    495 
    496 # Files that should not have + signs.
    497 #
    498 if checkyesno check_rhosts; then
    499 	list="/etc/hosts.equiv /etc/hosts.lpd"
    500 	for f in $list ; do
    501 		if [ -f $f ] && egrep '\+' $f > /dev/null ; then
    502 			printf "\nPlus sign in $f file.\n"
    503 		fi
    504 	done
    505 
    506 	# Check for special users with .rhosts files.  Only root and toor should
    507 	# have .rhosts files.  Also, .rhosts files should not have plus signs.
    508 	awk -F: '$1 != "root" && $1 != "toor" && \
    509 		($3 < 100 || $1 == "ftp" || $1 == "uucp") \
    510 			{ print $1 " " $9 }' $MP |
    511 	sort -k2 |
    512 	while read uid homedir; do
    513 		if [ -f ${homedir}/.rhosts ] ; then
    514 			rhost=$(ls -ldgT ${homedir}/.rhosts)
    515 			printf -- "$uid: $rhost\n"
    516 		fi
    517 	done > $OUTPUT
    518 	if [ -s $OUTPUT ] ; then
    519 		printf "\nChecking for special users with .rhosts files.\n"
    520 		cat $OUTPUT
    521 	fi
    522 
    523 	while read uid homedir; do
    524 		if [ -f ${homedir}/.rhosts ] &&
    525 		   [ -r ${homedir}/.rhosts ] &&
    526 		   cat -f ${homedir}/.rhosts | egrep '\+' > /dev/null
    527 		then
    528 			printf -- "$uid: + in .rhosts file.\n"
    529 		fi
    530 	done < $MPBYPATH > $OUTPUT
    531 	if [ -s $OUTPUT ] ; then
    532 		printf "\nChecking .rhosts files syntax.\n"
    533 		cat $OUTPUT
    534 	fi
    535 fi
    536 
    537 # Check home directories.  Directories should not be owned by someone else
    538 # or writable.
    539 #
    540 if checkyesno check_homes; then
    541 	checkyesno check_homes_permit_usergroups && \
    542 		permit_usergroups=1 || permit_usergroups=0
    543 	while read uid homedir; do
    544 		if [ -d ${homedir}/ ] ; then
    545 			file=$(ls -ldgT ${homedir})
    546 			printf -- "$uid $file\n"
    547 		fi
    548 	done < $MPBYPATH |
    549 	awk -v "usergroups=$permit_usergroups" \
    550 	    -v "permit_owners_list=$check_homes_permit_other_owner"  '
    551 	     BEGIN {
    552 		split(permit_owners_list, a);
    553 		for (i in a) permit_owners[a[i]]++;
    554 	     }
    555 	     $1 != $4 && $4 != "root" && !permit_owners[$1] \
    556 		{ print "user " $1 " home directory is owned by " $4 }
    557 	     $2 ~ /^d....w/ && (!usergroups || $5 != $1) \
    558 		{ print "user " $1 " home directory is group writable" }
    559 	     $2 ~ /^d.......w/ \
    560 		{ print "user " $1 " home directory is other writable" }' \
    561 	    > $OUTPUT
    562 	if [ -s $OUTPUT ] ; then
    563 		printf "\nChecking home directories.\n"
    564 		cat $OUTPUT
    565 	fi
    566 
    567 	# Files that should not be owned by someone else or readable.
    568 	list=".Xauthority .netrc .ssh/id_dsa .ssh/id_rsa .ssh/identity"
    569 	while read uid homedir; do
    570 		for f in $list ; do
    571 			file=${homedir}/${f}
    572 			if [ -f $file ] ; then
    573 				printf -- "$uid $f $(ls -ldgT $file)\n"
    574 			fi
    575 		done
    576 	done < $MPBYPATH |
    577 	awk -v "usergroups=$permit_usergroups" \
    578 	    -v "permit_owners_list=$check_homes_permit_other_owner"  '
    579 	     BEGIN {
    580 		split(permit_owners_list, a);
    581 		for (i in a) permit_owners[a[i]]++;
    582 	     }
    583 	     $1 != $5 && $5 != "root" && !permit_owners[$1] \
    584 		{ print "user " $1 " " $2 " file is owned by " $5 }
    585 	     $3 ~ /^-...r/ && (!usergroups || $6 != $1) \
    586 		{ print "user " $1 " " $2 " file is group readable" }
    587 	     $3 ~ /^-......r/ \
    588 		{ print "user " $1 " " $2 " file is other readable" }
    589 	     $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
    590 		{ print "user " $1 " " $2 " file is group writable" }
    591 	     $3 ~ /^-.......w/ \
    592 		{ print "user " $1 " " $2 " file is other writable" }' \
    593 	    > $OUTPUT
    594 
    595 	# Files that should not be owned by someone else or writable.
    596 	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
    597 	      .cshrc .emacs .exrc .forward .history .k5login .klogin .login \
    598 	      .logout .profile .qmail .rc_history .rhosts .shosts ssh .tcshrc \
    599 	      .twmrc .xinitrc .xsession .ssh/authorized_keys \
    600 	      .ssh/authorized_keys2 .ssh/config .ssh/id_dsa.pub \
    601 	      .ssh/id_rsa.pub .ssh/identity.pub .ssh/known_hosts \
    602 	      .ssh/known_hosts2"
    603 	while read uid homedir; do
    604 		for f in $list ; do
    605 			file=${homedir}/${f}
    606 			if [ -f $file ] ; then
    607 				printf -- "$uid $f $(ls -ldgT $file)\n"
    608 			fi
    609 		done
    610 	done < $MPBYPATH |
    611 	awk -v "usergroups=$permit_usergroups" \
    612 	    -v "permit_owners_list=$check_homes_permit_other_owner"  '
    613 	     BEGIN {
    614 		split(permit_owners_list, a);
    615 		for (i in a) permit_owners[a[i]]++;
    616 	     }
    617 	     $1 != $5 && $5 != "root" && !permit_owners[$1] \
    618 		{ print "user " $1 " " $2 " file is owned by " $5 }
    619 	     $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
    620 		{ print "user " $1 " " $2 " file is group writable" }
    621 	     $3 ~ /^-.......w/ \
    622 		{ print "user " $1 " " $2 " file is other writable" }' \
    623 	    >> $OUTPUT
    624 	if [ -s $OUTPUT ] ; then
    625 		printf "\nChecking dot files.\n"
    626 		cat $OUTPUT
    627 	fi
    628 fi
    629 
    630 # Mailboxes should be owned by user and unreadable.
    631 #
    632 if checkyesno check_varmail; then
    633 	ls -lA /var/mail | \
    634 	awk '	NR == 1 { next; }
    635 		$9 ~ /^\./ {next; }
    636 	    	$3 != $9 {
    637 			print "user " $9 " mailbox is owned by " $3
    638 		}
    639 		$1 != "-rw-------" {
    640 			print "user " $9 " mailbox is " $1 ", group " $4
    641 		}' > $OUTPUT
    642 	if [ -s $OUTPUT ] ; then
    643 		printf "\nChecking mailbox ownership.\n"
    644 		cat $OUTPUT
    645 	fi
    646 fi
    647 
    648 # NFS exports shouldn't be globally exported
    649 #
    650 if checkyesno check_nfs && [ -f /etc/exports ]; then
    651 	awk '{
    652 		# ignore comments and blank lines
    653 		if ($0 ~ /^\#/ || $0 ~ /^$/ )
    654 			next;
    655 		# manage line continuation
    656 		while ($NF ~ /^\\$/) {
    657 			$NF = "";
    658 			line = $0 "";
    659 			getline;
    660 			$0 = line $0 "";
    661 		}
    662 
    663 		delete dir;
    664 		readonly = ndir = 0;
    665 		for (i = 1; i <= NF; ++i) {
    666 			if ($i ~ /^\//) dir[ndir++] = $i;
    667 			else if ($i ~ /^-/) {
    668 				if ($i ~ /^-(ro|o)$/) readonly = 1;
    669 				if ($i ~ /^-network/) next;
    670 			}
    671 			else next;
    672 		}
    673 		if (readonly)
    674 			for (item in dir)
    675 				rodir[nrodir++] = dir[item];
    676 		else
    677 			for (item in dir)
    678 				rwdir[nrwdir++] = dir[item];
    679 
    680 	}
    681 
    682 	END {
    683 		if (nrodir) {
    684 			printf("Globally exported file system%s, read-only:\n",
    685 				nrodir > 1 ? "s" : "");
    686 			for (item in rodir)
    687 				printf("\t%s\n", rodir[item]);
    688 		}
    689 		if (nrwdir) {
    690 			printf("Globally exported file system%s, read-write:\n",
    691 				nrwdir > 1 ? "s" : "");
    692 			for (item in rwdir)
    693 				printf("\t%s\n", rwdir[item]);
    694 		}
    695 	}' < /etc/exports > $OUTPUT
    696 	if [ -s $OUTPUT ] ; then
    697 		printf "\nChecking for globally exported file systems.\n"
    698 		cat $OUTPUT
    699 	fi
    700 fi
    701 
    702 # Display any changes in setuid files and devices.
    703 #
    704 if checkyesno check_devices; then
    705 	> $ERR
    706 	(
    707 
    708 	# Convert check_devices_ignore_fstypes="foo !bar bax"
    709 	#    into "-fstype foo -o ! -fstype bar -o -fstype bax"
    710 	# and check_devices_ignore_paths="/foo !/bar /bax"
    711 	#    into " -path /foo -o ! -path /bar -o -path /bax"
    712 	#
    713 	ignexpr=$(\
    714 	    echo $check_devices_ignore_fstypes | \
    715 		sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -fstype \2/g' ; \
    716 	    echo $check_devices_ignore_paths | \
    717 		sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -path \2/g' \
    718 	)
    719 
    720 	# Massage the expression into ( $ignexpr ) -a -prune -o
    721 	if [ -n "${ignexpr}" ]; then
    722 		ignexpr=$(\
    723 			echo $ignexpr | \
    724 			    sed -e 's/^-o /( /' \
    725 				-e 's/$/ ) -a -prune -o/' \
    726 		)
    727 	fi
    728 
    729 	find / $ignexpr \
    730 	    \( \( -perm -u+s -a ! -type d \) -o \
    731 	       \( -perm -g+s -a ! -type d \) -o \
    732 	       -type b -o -type c \) -print0 | \
    733 	xargs -0 ls -ldgTq | sort +9 > $LIST
    734 
    735 	) 2> $OUTPUT
    736 
    737 	# Display any errors that occurred during system file walk.
    738 	if [ -s $OUTPUT ] ; then
    739 		printf "Setuid/device find errors:\n" >> $ERR
    740 		cat $OUTPUT >> $ERR
    741 		printf "\n" >> $ERR
    742 	fi
    743 
    744 	# Display any changes in the setuid file list.
    745 	egrep -v '^[bc]' $LIST > $TMP1
    746 	if [ -s $TMP1 ] ; then
    747 		# Check to make sure uudecode isn't setuid.
    748 		if grep -w uudecode $TMP1 > /dev/null ; then
    749 			printf "\nUudecode is setuid.\n" >> $ERR
    750 		fi
    751 
    752 		file=$work_dir/setuid
    753 		migrate_file "$backup_dir/setuid" "$file"
    754 		CUR=${file}.current
    755 		BACK=${file}.backup
    756 		if [ -s $CUR ] ; then
    757 			if cmp -s $CUR $TMP1 ; then
    758 				:
    759 			else
    760 				> $TMP2
    761 				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
    762 				if [ -s $OUTPUT ] ; then
    763 					printf "Setuid additions:\n" >> $ERR
    764 					tee -a $TMP2 < $OUTPUT >> $ERR
    765 					printf "\n" >> $ERR
    766 				fi
    767 
    768 				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
    769 				if [ -s $OUTPUT ] ; then
    770 					printf "Setuid deletions:\n" >> $ERR
    771 					tee -a $TMP2 < $OUTPUT >> $ERR
    772 					printf "\n" >> $ERR
    773 				fi
    774 
    775 				sort -k10 $TMP2 $CUR $TMP1 | \
    776 				    sed -e 's/[	 ][	 ]*/ /g' | \
    777 				    uniq -u > $OUTPUT
    778 				if [ -s $OUTPUT ] ; then
    779 					printf "Setuid changes:\n" >> $ERR
    780 					column -t $OUTPUT >> $ERR
    781 					printf "\n" >> $ERR
    782 				fi
    783 
    784 				backup_file update $TMP1 $CUR $BACK
    785 			fi
    786 		else
    787 			printf "Setuid additions:\n" >> $ERR
    788 			column -t $TMP1 >> $ERR
    789 			printf "\n" >> $ERR
    790 			backup_file add $TMP1 $CUR $BACK
    791 		fi
    792 	fi
    793 
    794 	# Check for block and character disk devices that are readable or
    795 	# writable or not owned by root.operator.
    796 	>$TMP1
    797 	DISKLIST="ccd ch hk hp ld md ra raid rb rd rl rx \
    798 	    sd se ss uk up vnd wd xd xy"
    799 #	DISKLIST="$DISKLIST ct mt st wt"
    800 	for i in $DISKLIST; do
    801 		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
    802 		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
    803 	done
    804 
    805 	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
    806 		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
    807 		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
    808 	if [ -s $OUTPUT ] ; then
    809 		printf "\nChecking disk ownership and permissions.\n" >> $ERR
    810 		cat $OUTPUT >> $ERR
    811 		printf "\n" >> $ERR
    812 	fi
    813 
    814 	# Display any changes in the device file list.
    815 	egrep '^[bc]' $LIST | sort -k11 > $TMP1
    816 	if [ -s $TMP1 ] ; then
    817 		file=$work_dir/device
    818 		migrate_file "$backup_dir/device" "$file"
    819 		CUR=${file}.current
    820 		BACK=${file}.backup
    821 
    822 		if [ -s $CUR ] ; then
    823 			if cmp -s $CUR $TMP1 ; then
    824 				:
    825 			else
    826 				> $TMP2
    827 				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
    828 				if [ -s $OUTPUT ] ; then
    829 					printf "Device additions:\n" >> $ERR
    830 					tee -a $TMP2 < $OUTPUT >> $ERR
    831 					printf "\n" >> $ERR
    832 				fi
    833 
    834 				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
    835 				if [ -s $OUTPUT ] ; then
    836 					printf "Device deletions:\n" >> $ERR
    837 					tee -a $TMP2 < $OUTPUT >> $ERR
    838 					printf "\n" >> $ERR
    839 				fi
    840 
    841 				# Report any block device change. Ignore
    842 				# character devices, only the name is
    843 				# significant.
    844 				cat $TMP2 $CUR $TMP1 | \
    845 				    sed -e '/^c/d' | \
    846 				    sort -k11 | \
    847 				    sed -e 's/[	 ][	 ]*/ /g' | \
    848 				    uniq -u > $OUTPUT
    849 				if [ -s $OUTPUT ] ; then
    850 					printf "Block device changes:\n" >> $ERR
    851 					column -t $OUTPUT >> $ERR
    852 					printf "\n" >> $ERR
    853 				fi
    854 
    855 				backup_file update $TMP1 $CUR $BACK
    856 			fi
    857 		else
    858 			printf "Device additions:\n" >> $ERR
    859 			column -t $TMP1 >> $ERR
    860 			printf "\n" >> $ERR
    861 			backup_file add $TMP1 $CUR $BACK >> $ERR
    862 		fi
    863 	fi
    864 	if [ -s $ERR ] ; then
    865 		printf "\nChecking setuid files and devices:\n"
    866 		cat $ERR
    867 		printf "\n"
    868 	fi
    869 fi
    870 
    871 # Check special files.
    872 # Check system binaries.
    873 #
    874 # Create the mtree tree specifications using:
    875 #	mtree -cx -pDIR -kmd5,uid,gid,mode,nlink,size,link,time > DIR.secure
    876 #	chown root:wheel DIR.secure
    877 #	chmod u+r,go= DIR.secure
    878 #
    879 # Note, this is not complete protection against Trojan horsed binaries, as
    880 # the hacker can modify the tree specification to match the replaced binary.
    881 # For details on really protecting yourself against modified binaries, see
    882 # the mtree(8) manual page.
    883 #
    884 if checkyesno check_mtree; then
    885 	if checkyesno check_mtree_follow_symlinks; then
    886 		check_mtree_flags="-L"
    887 	else
    888 		check_mtree_flags=""
    889 	fi
    890 	mtree -e -l -p / $check_mtree_flags -f $SPECIALSPEC 3>&1 >$OUTPUT 2>&3 |
    891 		grep -v '^mtree: dev/tty: Device not configured$' >&2
    892 	if [ -s $OUTPUT ]; then
    893 		printf "\nChecking special files and directories.\n"
    894 		cat $OUTPUT
    895 	fi
    896 
    897 	for file in /etc/mtree/*.secure; do
    898 		[ $file = '/etc/mtree/*.secure' ] && continue
    899 		tree=$(sed -n -e '3s/.* //p' -e 3q $file)
    900 		mtree $check_mtree_flags -f $file -p $tree > $TMP1
    901 		if [ -s $TMP1 ]; then
    902 			printf "\nChecking $tree:\n"
    903 			cat $TMP1
    904 		fi
    905 	done > $OUTPUT
    906 	if [ -s $OUTPUT ]; then
    907 		printf "\nChecking system binaries:\n"
    908 		cat $OUTPUT
    909 	fi
    910 fi
    911 
    912 # Backup disklabels of available disks
    913 #
    914 if checkyesno check_disklabels; then
    915 		# migrate old disklabels
    916 	for file in $(ls -1d $backup_dir/$backup_dir/disklabel.* \
    917 	    $backup_dir/disklabel.* 2>/dev/null); do
    918 		migrate_file "$file" "$work_dir/${file##*/}"
    919 	done
    920 
    921 		# generate list of old disklabels, fdisks & wedges,
    922 		# and remove them
    923 	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* $work_dir/wedges.* \
    924 	    2>/dev/null |
    925 	    egrep -v '\.(backup|current)(,v)?$' > $LABELS
    926 	xargs rm < $LABELS
    927 
    928 	disks="$(/sbin/sysctl -n hw.iostatnames)"
    929 
    930 		# generate disklabels of all disks excluding:	cd fd md dk st
    931 		# nfs and "device" (the header of iostat)
    932 	for i in $disks; do
    933 		case $i in
    934 		[cfm]d[0-9]*|dk[0-9]*|st[0-9]*|nfs[0-9]*)
    935 			;;
    936 		*)
    937 			if disklabel $i > /dev/null 2>&1; then
    938 				disklabel $i > "$work_dir/disklabel.$i"
    939 			fi
    940 			;;
    941 		esac
    942 	done
    943 
    944 		# if fdisk is available, generate fdisks for:	ed ld sd wd
    945 	if [ -x /sbin/fdisk ]; then
    946 		for i in $disks; do
    947 			case $i in
    948 			[elsw]d[0-9]*)
    949 				/sbin/fdisk $i > "$work_dir/fdisk.$i" \
    950 				    2>/dev/null
    951 				;;
    952 			esac
    953 		done
    954 	fi
    955 
    956 		# if dkctl is available, generate dkctl listwedges
    957 		# for:	ed ld sd wd cgd ofdisk ra rl raid
    958 	if [ -x /sbin/dkctl ]; then
    959 		for i in $disks; do
    960 			case $i in
    961 			[elsw]d[0-9]*|cgd[0-9]*|ofdisk[0-9]*|r[al][0-9]*|raid[0-9]*)
    962 				if /sbin/dkctl $i listwedges |
    963 				     grep -qe '[0-9] wedges:'; then
    964 					/sbin/dkctl $i listwedges \
    965 					    > "$work_dir/wedges.$i" 2>/dev/null
    966 				fi
    967 				;;
    968 			esac
    969 		done
    970 	fi
    971 
    972 		# if raidctl is available, generate raidctls for:	raid
    973 	if [ -x /sbin/raidctl ]; then
    974 		disks=$(iostat -x | awk 'NR > 1 && $1 ~ /^raid/ { print $1; }')
    975 		for i in $disks; do
    976 			/sbin/raidctl -G $i > "$work_dir/raidconf.$i" \
    977 				2>/dev/null
    978 		done
    979 	fi
    980 
    981 		# append list of new disklabels, fdisks and wedges
    982 	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* $work_dir/wedges.* \
    983 	    $work_dir/raidconf.* 2>/dev/null |
    984 	    egrep -v '\.(backup|current)(,v)?$' >> $LABELS
    985 	CHANGELIST="$LABELS $CHANGELIST"
    986 fi
    987 
    988 if checkyesno check_lvm; then
    989 		# generate list of existing LVM elements Physical Volumes,
    990 		# Volume Groups and Logical Volumes.
    991 	if [ -x /sbin/lvm ]; then
    992 		lvm pvdisplay -m >"$work_dir/lvm.pv" 2>/dev/null
    993 		lvm vgdisplay -m >"$work_dir/lvm.vg" 2>/dev/null
    994 		lvm lvdisplay -m >"$work_dir/lvm.lv" 2>/dev/null
    995 	fi
    996 	ls -1d $work_dir/lvm.* 2>/dev/null |
    997 	    egrep -v '\.(backup|current)(,v)?$'>> $LVM_LABELS
    998 	CHANGELIST="$CHANGELIST $LVM_LABELS"
    999 fi
   1000 
   1001 # Check for changes in the list of installed pkgs
   1002 #
   1003 if checkyesno check_pkgs && have_pkgs; then
   1004 	pkgs=$work_dir/pkgs
   1005 	migrate_file "$backup_dir/pkgs" "$pkgs"
   1006 	pkg_dbdir=$(${pkg_admin} config-var PKG_DBDIR)
   1007 	: ${pkg_dbdir:=/usr/pkg/pkgdb}
   1008 	(	cd $pkg_dbdir
   1009 		$pkg_info | sort
   1010 		echo ""
   1011 		find . \( -name +REQUIRED_BY -o -name +CONTENTS \) -print0 |
   1012 			xargs -0 ls -ldgTq | sort -t. +1 | sed -e 's, \./, ,'
   1013 	 ) > $pkgs
   1014 	echo "$pkgs" > $PKGS
   1015 	CHANGELIST="$PKGS $CHANGELIST"
   1016 fi
   1017 
   1018 # List of files that get backed up and checked for any modifications.
   1019 # Any changes cause the files to rotate.
   1020 #
   1021 if checkyesno check_changelist ; then
   1022 	mtree -D -k type -f $SPECIALSPEC -E exclude |
   1023 	    sed '/^type=file/!d ; s/type=file \.//' | unvis > $CHANGEFILES
   1024 
   1025 	(
   1026 		# Add other files which might dynamically exist:
   1027 		#	/etc/ifconfig.*
   1028 		#	/etc/raid*.conf
   1029 		#	/etc/rc.d/*
   1030 		#	/etc/rc.conf.d/*
   1031 		#
   1032 		echo "/etc/ifconfig.*"
   1033 		echo "/etc/raid*.conf"
   1034 		echo "/etc/rc.d/*"
   1035 		echo "/etc/rc.conf.d/*"
   1036 		echo "/etc/lvm/backup/*"
   1037 		echo "/etc/lvm/archive/*"
   1038 
   1039 		# Add /etc/changelist
   1040 		#
   1041 		if [ -s /etc/changelist ]; then
   1042 			grep -v '^#' /etc/changelist
   1043 		fi
   1044 	) | while read file; do
   1045 		case "$file" in
   1046 		*[\*\?\[]*)	# If changelist line is a glob ...
   1047 				# ... expand possible backup files
   1048 				#
   1049 			ls -1d $backup_dir/${file}.current 2>/dev/null \
   1050 			    | sed "s,^$backup_dir/,, ; s,\.current$,,"
   1051 
   1052 				# ... expand possible files
   1053 				#
   1054 			ls -1d $file 2>/dev/null
   1055 			;;
   1056 		*)
   1057 				# Otherwise, just print the filename
   1058 			echo $file
   1059 			;;
   1060 		esac
   1061 	done >> $CHANGEFILES
   1062 	CHANGELIST="$CHANGEFILES $CHANGELIST"
   1063 fi
   1064 
   1065 # Save entropy to ${random_file} if defined, like
   1066 # /etc/rc.d/random_seed.
   1067 #
   1068 if [ -n "${random_file:-}" ]; then
   1069 	rndctl -S "$random_file"
   1070 fi
   1071 
   1072 # Special case backups, including the master password file and
   1073 # ssh private host keys. The normal backup mechanisms for
   1074 # $check_changelist (see below) also print out the actual file
   1075 # differences and we don't want to do that for these files
   1076 #
   1077 echo $MP > $TMP1			# always add /etc/master.passwd
   1078 mtree -D -k type -f $SPECIALSPEC -I nodiff |
   1079     sed '/^type=file/!d ; s/type=file \.//' | unvis >> $TMP1
   1080 grep -v '^$' $TMP1 | sort -u > $TMP2
   1081 
   1082 while read file; do
   1083 	backup_and_diff "$file" no
   1084 done < $TMP2
   1085 
   1086 
   1087 if [ -n "$CHANGELIST" ]; then
   1088 	grep -h -v '^$' $CHANGELIST | sort -u > $TMP1
   1089 	comm -23 $TMP1 $TMP2 | while read file; do
   1090 		backup_and_diff "$file" yes
   1091 	done
   1092 fi
   1093 
   1094 if have_pkgs; then
   1095 	if checkyesno check_pkg_vulnerabilities; then
   1096 		${pkg_admin} ${_compat_K_flag} audit >${OUTPUT} 2>&1
   1097 		if [ -s ${OUTPUT} ]; then
   1098 			printf "\nInstalled vulnerable packages:\n"
   1099 			cat ${OUTPUT}
   1100 		fi
   1101 	fi
   1102 
   1103 	if checkyesno check_pkg_signatures; then
   1104 		${pkg_admin} ${_compat_K_flag} check >${OUTPUT} 2>&1
   1105 		if [ $? -ne 0 ]; then
   1106 			printf "\nFiles with invalid signatures:\n"
   1107 			cat ${OUTPUT}
   1108 		fi
   1109 	fi
   1110 fi
   1111 
   1112 if [ -f /etc/security.local ]; then
   1113 	. /etc/security.local > $OUTPUT 2>&1
   1114 	if [ -s $OUTPUT ] ; then
   1115 		printf "\nRunning /etc/security.local:\n"
   1116 		cat $OUTPUT
   1117 	fi
   1118 fi
   1119