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