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