xref: /netbsd-src/etc/security (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1#!/bin/sh -
2#
3#	$NetBSD: security,v 1.115 2013/11/06 19:37:05 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            -v "permit_owners_list=$check_homes_permit_other_owner"  '
534	     BEGIN {
535		split(permit_owners_list, a);
536		for (i in a) permit_owners[a[i]]++;
537	     }
538	     $1 != $4 && $4 != "root" && !permit_owners[$1] \
539		{ print "user " $1 " home directory is owned by " $4 }
540	     $2 ~ /^d....w/ && (!usergroups || $5 != $1) \
541		{ print "user " $1 " home directory is group writable" }
542	     $2 ~ /^d.......w/ \
543		{ print "user " $1 " home directory is other writable" }' \
544	    > $OUTPUT
545	if [ -s $OUTPUT ] ; then
546		printf "\nChecking home directories.\n"
547		cat $OUTPUT
548	fi
549
550	# Files that should not be owned by someone else or readable.
551	list=".Xauthority .netrc .ssh/id_dsa .ssh/id_rsa .ssh/identity"
552	while read uid homedir; do
553		for f in $list ; do
554			file=${homedir}/${f}
555			if [ -f $file ] ; then
556				printf -- "$uid $f $(ls -ldgT $file)\n"
557			fi
558		done
559	done < $MPBYPATH |
560	awk -v "usergroups=$permit_usergroups" \
561            -v "permit_owners_list=$check_homes_permit_other_owner"  '
562	     BEGIN {
563		split(permit_owners_list, a);
564		for (i in a) permit_owners[a[i]]++;
565	     }
566	     $1 != $5 && $5 != "root" && !permit_owners[$1] \
567		{ print "user " $1 " " $2 " file is owned by " $5 }
568	     $3 ~ /^-...r/ && (!usergroups || $6 != $1) \
569		{ print "user " $1 " " $2 " file is group readable" }
570	     $3 ~ /^-......r/ \
571		{ print "user " $1 " " $2 " file is other readable" }
572	     $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
573		{ print "user " $1 " " $2 " file is group writable" }
574	     $3 ~ /^-.......w/ \
575		{ print "user " $1 " " $2 " file is other writable" }' \
576	    > $OUTPUT
577
578	# Files that should not be owned by someone else or writable.
579	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
580	      .cshrc .emacs .exrc .forward .history .k5login .klogin .login \
581	      .logout .profile .qmail .rc_history .rhosts .shosts ssh .tcshrc \
582	      .twmrc .xinitrc .xsession .ssh/authorized_keys \
583	      .ssh/authorized_keys2 .ssh/config .ssh/id_dsa.pub \
584	      .ssh/id_rsa.pub .ssh/identity.pub .ssh/known_hosts \
585	      .ssh/known_hosts2"
586	while read uid homedir; do
587		for f in $list ; do
588			file=${homedir}/${f}
589			if [ -f $file ] ; then
590				printf -- "$uid $f $(ls -ldgT $file)\n"
591			fi
592		done
593	done < $MPBYPATH |
594	awk -v "usergroups=$permit_usergroups" \
595            -v "permit_owners_list=$check_homes_permit_other_owner"  '
596	     BEGIN {
597		split(permit_owners_list, a);
598		for (i in a) permit_owners[a[i]]++;
599	     }
600	     $1 != $5 && $5 != "root" && !permit_owners[$1] \
601		{ print "user " $1 " " $2 " file is owned by " $5 }
602	     $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
603		{ print "user " $1 " " $2 " file is group writable" }
604	     $3 ~ /^-.......w/ \
605		{ print "user " $1 " " $2 " file is other writable" }' \
606	    >> $OUTPUT
607	if [ -s $OUTPUT ] ; then
608		printf "\nChecking dot files.\n"
609		cat $OUTPUT
610	fi
611fi
612
613# Mailboxes should be owned by user and unreadable.
614#
615if checkyesno check_varmail; then
616	ls -lA /var/mail | \
617	awk '	NR == 1 { next; }
618		$9 ~ /^\./ {next; }
619	    	$3 != $9 {
620			print "user " $9 " mailbox is owned by " $3
621		}
622		$1 != "-rw-------" {
623			print "user " $9 " mailbox is " $1 ", group " $4
624		}' > $OUTPUT
625	if [ -s $OUTPUT ] ; then
626		printf "\nChecking mailbox ownership.\n"
627		cat $OUTPUT
628	fi
629fi
630
631# NFS exports shouldn't be globally exported
632#
633if checkyesno check_nfs && [ -f /etc/exports ]; then
634	awk '{
635		# ignore comments and blank lines
636		if ($0 ~ /^\#/ || $0 ~ /^$/ )
637			next;
638		# manage line continuation
639		while ($NF ~ /^\\$/) {
640			$NF = "";
641			line = $0 "";
642			getline;
643			$0 = line $0 "";
644		}
645
646		delete dir;
647		readonly = ndir = 0;
648		for (i = 1; i <= NF; ++i) {
649			if ($i ~ /^\//) dir[ndir++] = $i;
650			else if ($i ~ /^-/) {
651				if ($i ~ /^-(ro|o)$/) readonly = 1;
652				if ($i ~ /^-network/) next;
653			}
654			else next;
655		}
656		if (readonly)
657			for (item in dir)
658				rodir[nrodir++] = dir[item];
659		else
660			for (item in dir)
661				rwdir[nrwdir++] = dir[item];
662
663	}
664
665	END {
666		if (nrodir) {
667			printf("Globally exported file system%s, read-only:\n",
668				nrodir > 1 ? "s" : "");
669			for (item in rodir)
670				printf("\t%s\n", rodir[item]);
671		}
672		if (nrwdir) {
673			printf("Globally exported file system%s, read-write:\n",
674				nrwdir > 1 ? "s" : "");
675			for (item in rwdir)
676				printf("\t%s\n", rwdir[item]);
677		}
678	}' < /etc/exports > $OUTPUT
679	if [ -s $OUTPUT ] ; then
680		printf "\nChecking for globally exported file systems.\n"
681		cat $OUTPUT
682	fi
683fi
684
685# Display any changes in setuid files and devices.
686#
687if checkyesno check_devices; then
688	> $ERR
689	(
690
691	# Convert check_devices_ignore_fstypes="foo !bar bax"
692	#    into "-fstype foo -o ! -fstype bar -o -fstype bax"
693	# and check_devices_ignore_paths="/foo !/bar /bax"
694	#    into " -path /foo -o ! -path /bar -o -path /bax"
695	#
696	ignexpr=$(\
697	    echo $check_devices_ignore_fstypes | \
698		sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -fstype \2/g' ; \
699	    echo $check_devices_ignore_paths | \
700		sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -path \2/g' \
701	)
702
703	# Massage the expression into ( $ignexpr ) -a -prune -o
704	if [ -n "${ignexpr}" ]; then
705		ignexpr=$(\
706			echo $ignexpr | \
707			    sed -e 's/^-o /( /' \
708				-e 's/$/ ) -a -prune -o/' \
709		)
710	fi
711
712	find / $ignexpr \
713	    \( \( -perm -u+s -a ! -type d \) -o \
714	       \( -perm -g+s -a ! -type d \) -o \
715	       -type b -o -type c \) -print0 | \
716	xargs -0 ls -ldgTq | sort +9 > $LIST
717
718	) 2> $OUTPUT
719
720	# Display any errors that occurred during system file walk.
721	if [ -s $OUTPUT ] ; then
722		printf "Setuid/device find errors:\n" >> $ERR
723		cat $OUTPUT >> $ERR
724		printf "\n" >> $ERR
725	fi
726
727	# Display any changes in the setuid file list.
728	egrep -v '^[bc]' $LIST > $TMP1
729	if [ -s $TMP1 ] ; then
730		# Check to make sure uudecode isn't setuid.
731		if grep -w uudecode $TMP1 > /dev/null ; then
732			printf "\nUudecode is setuid.\n" >> $ERR
733		fi
734
735		file=$work_dir/setuid
736		migrate_file "$backup_dir/setuid" "$file"
737		CUR=${file}.current
738		BACK=${file}.backup
739		if [ -s $CUR ] ; then
740			if cmp -s $CUR $TMP1 ; then
741				:
742			else
743				> $TMP2
744				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
745				if [ -s $OUTPUT ] ; then
746					printf "Setuid additions:\n" >> $ERR
747					tee -a $TMP2 < $OUTPUT >> $ERR
748					printf "\n" >> $ERR
749				fi
750
751				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
752				if [ -s $OUTPUT ] ; then
753					printf "Setuid deletions:\n" >> $ERR
754					tee -a $TMP2 < $OUTPUT >> $ERR
755					printf "\n" >> $ERR
756				fi
757
758				sort -k10 $TMP2 $CUR $TMP1 | \
759				    sed -e 's/[	 ][	 ]*/ /g' | \
760				    uniq -u > $OUTPUT
761				if [ -s $OUTPUT ] ; then
762					printf "Setuid changes:\n" >> $ERR
763					column -t $OUTPUT >> $ERR
764					printf "\n" >> $ERR
765				fi
766
767				backup_file update $TMP1 $CUR $BACK
768			fi
769		else
770			printf "Setuid additions:\n" >> $ERR
771			column -t $TMP1 >> $ERR
772			printf "\n" >> $ERR
773			backup_file add $TMP1 $CUR $BACK
774		fi
775	fi
776
777	# Check for block and character disk devices that are readable or
778	# writable or not owned by root.operator.
779	>$TMP1
780	DISKLIST="ccd ch hk hp ld md ra raid rb rd rl rx \
781	    sd se ss uk up vnd wd xd xy"
782#	DISKLIST="$DISKLIST ct mt st wt"
783	for i in $DISKLIST; do
784		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
785		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
786	done
787
788	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
789		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
790		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
791	if [ -s $OUTPUT ] ; then
792		printf "\nChecking disk ownership and permissions.\n" >> $ERR
793		cat $OUTPUT >> $ERR
794		printf "\n" >> $ERR
795	fi
796
797	# Display any changes in the device file list.
798	egrep '^[bc]' $LIST | sort -k11 > $TMP1
799	if [ -s $TMP1 ] ; then
800		file=$work_dir/device
801		migrate_file "$backup_dir/device" "$file"
802		CUR=${file}.current
803		BACK=${file}.backup
804
805		if [ -s $CUR ] ; then
806			if cmp -s $CUR $TMP1 ; then
807				:
808			else
809				> $TMP2
810				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
811				if [ -s $OUTPUT ] ; then
812					printf "Device additions:\n" >> $ERR
813					tee -a $TMP2 < $OUTPUT >> $ERR
814					printf "\n" >> $ERR
815				fi
816
817				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
818				if [ -s $OUTPUT ] ; then
819					printf "Device deletions:\n" >> $ERR
820					tee -a $TMP2 < $OUTPUT >> $ERR
821					printf "\n" >> $ERR
822				fi
823
824				# Report any block device change. Ignore
825				# character devices, only the name is
826				# significant.
827				cat $TMP2 $CUR $TMP1 | \
828				    sed -e '/^c/d' | \
829				    sort -k11 | \
830				    sed -e 's/[	 ][	 ]*/ /g' | \
831				    uniq -u > $OUTPUT
832				if [ -s $OUTPUT ] ; then
833					printf "Block device changes:\n" >> $ERR
834					column -t $OUTPUT >> $ERR
835					printf "\n" >> $ERR
836				fi
837
838				backup_file update $TMP1 $CUR $BACK
839			fi
840		else
841			printf "Device additions:\n" >> $ERR
842			column -t $TMP1 >> $ERR
843			printf "\n" >> $ERR
844			backup_file add $TMP1 $CUR $BACK >> $ERR
845		fi
846	fi
847	if [ -s $ERR ] ; then
848		printf "\nChecking setuid files and devices:\n"
849		cat $ERR
850		printf "\n"
851	fi
852fi
853
854# Check special files.
855# Check system binaries.
856#
857# Create the mtree tree specifications using:
858#	mtree -cx -pDIR -kmd5,uid,gid,mode,nlink,size,link,time > DIR.secure
859#	chown root:wheel DIR.secure
860#	chmod u+r,go= DIR.secure
861#
862# Note, this is not complete protection against Trojan horsed binaries, as
863# the hacker can modify the tree specification to match the replaced binary.
864# For details on really protecting yourself against modified binaries, see
865# the mtree(8) manual page.
866#
867if checkyesno check_mtree; then
868	if checkyesno check_mtree_follow_symlinks; then
869		check_mtree_flags="-L"
870	else
871		check_mtree_flags=""
872	fi
873	mtree -e -l -p / $check_mtree_flags -f $SPECIALSPEC 3>&1 >$OUTPUT 2>&3 |
874		grep -v '^mtree: dev/tty: Device not configured$' >&2
875	if [ -s $OUTPUT ]; then
876		printf "\nChecking special files and directories.\n"
877		cat $OUTPUT
878	fi
879
880	for file in /etc/mtree/*.secure; do
881		[ $file = '/etc/mtree/*.secure' ] && continue
882		tree=$(sed -n -e '3s/.* //p' -e 3q $file)
883		mtree $check_mtree_flags -f $file -p $tree > $TMP1
884		if [ -s $TMP1 ]; then
885			printf "\nChecking $tree:\n"
886			cat $TMP1
887		fi
888	done > $OUTPUT
889	if [ -s $OUTPUT ]; then
890		printf "\nChecking system binaries:\n"
891		cat $OUTPUT
892	fi
893fi
894
895# Backup disklabels of available disks
896#
897if checkyesno check_disklabels; then
898		# migrate old disklabels
899	for file in $(ls -1d $backup_dir/$backup_dir/disklabel.* \
900	    $backup_dir/disklabel.* 2>/dev/null); do
901		migrate_file "$file" "$work_dir/${file##*/}"
902	done
903
904		# generate list of old disklabels, fdisks & wedges and remove them
905	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* $work_dir/wedges.* 2>/dev/null |
906	    egrep -v '\.(backup|current)(,v)?$' > $LABELS
907	xargs rm < $LABELS
908
909		# generate disklabels of all disks excluding:	cd dk fd md st
910	disks=$(iostat -x | awk 'NR > 1 && $1 !~ /^[cfm]d|dk|st|nfs/ { print $1; }')
911	for i in $disks; do
912		disklabel $i > "$work_dir/disklabel.$i" 2>/dev/null
913	done
914
915		# if fdisk is available, generate fdisks for:	ed ld sd wd
916	if [ -x /sbin/fdisk ]; then
917		disks=$(iostat -x | awk 'NR > 1 && $1 ~ /^[elsw]d/ { print $1; }')
918		for i in $disks; do
919			/sbin/fdisk $i > "$work_dir/fdisk.$i" 2>/dev/null
920		done
921	fi
922
923		# if dkctl is available, generate dkctl listwedges for:	ed ld sd wd cgd ofdisk ra rl raid
924	if [ -x /sbin/dkctl ]; then
925		disks=$(iostat -x | awk 'NR > 1 && $1 ~ /^[elsw]d|cgd|ofdisk|r[al]|raid/ { print $1; }')
926		for i in $disks; do
927			/sbin/dkctl $i listwedges > "$work_dir/wedges.$i" 2>/dev/null
928		done
929	fi
930
931		# append list of new disklabels, fdisks and wedges
932	ls -1d $work_dir/disklabel.* $work_dir/fdisk.* $work_dir/wedges.* 2>/dev/null |
933	    egrep -v '\.(backup|current)(,v)?$' >> $LABELS
934	CHANGELIST="$LABELS $CHANGELIST"
935fi
936
937if checkyesno check_lvm; then
938    
939    # generate list of existing LVM elements Physical Volumes, Volume Groups and Logical Volumes.
940if [ -x /sbin/lvm ]; then
941    lvm pvdisplay -m >"$work_dir/lvm.pv" 2>/dev/null
942    lvm vgdisplay -m >"$work_dir/lvm.vg" 2>/dev/null
943    lvm lvdisplay -m >"$work_dir/lvm.lv" 2>/dev/null
944fi
945    ls -1d $work_dir/lvm.* 2>/dev/null |
946        egrep -v '\.(backup|current)(,v)?$'>> $LVM_LABELS 
947    CHANGELIST="$CHANGELIST $LVM_LABELS"
948fi
949
950# Check for changes in the list of installed pkgs
951#
952if checkyesno check_pkgs && have_pkgs; then
953	pkgs=$work_dir/pkgs
954	migrate_file "$backup_dir/pkgs" "$pkgs"
955	pkg_dbdir=$(${pkg_admin} config-var PKG_DBDIR)
956	: ${pkg_dbdir:=/var/db/pkg}
957	(	cd $pkg_dbdir
958		$pkg_info | sort
959		echo ""
960		find . \( -name +REQUIRED_BY -o -name +CONTENTS \) -print0 |
961			xargs -0 ls -ldgTq | sort -t. +1 | sed -e 's, \./, ,'
962	 ) > $pkgs
963	echo "$pkgs" > $PKGS
964	CHANGELIST="$PKGS $CHANGELIST"
965fi
966
967# List of files that get backed up and checked for any modifications.
968# Any changes cause the files to rotate.
969#
970if checkyesno check_changelist ; then
971	mtree -D -k type -f $SPECIALSPEC -E exclude |
972	    sed '/^type=file/!d ; s/type=file \.//' | unvis > $CHANGEFILES
973
974	(
975		# Add other files which might dynamically exist:
976		#	/etc/ifconfig.*
977		#	/etc/raid*.conf
978		#	/etc/rc.d/*
979		#	/etc/rc.conf.d/*
980		#
981		echo "/etc/ifconfig.*"
982		echo "/etc/raid*.conf"
983		echo "/etc/rc.d/*"
984		echo "/etc/rc.conf.d/*"
985		echo "/etc/lvm/backup/*"
986		echo "/etc/lvm/archive/*"
987
988		# Add /etc/changelist
989		#
990		if [ -s /etc/changelist ]; then
991			grep -v '^#' /etc/changelist
992		fi
993	) | while read file; do
994		case "$file" in
995		*[\*\?\[]*)	# If changelist line is a glob ...
996				# ... expand possible backup files
997				#
998			ls -1d $(echo $backup_dir/${file}.current) 2>/dev/null \
999			    | sed "s,^$backup_dir/,, ; s,\.current$,,"
1000				
1001				# ... expand possible files
1002				#
1003			ls -1d $(echo $file) 2>/dev/null
1004			;;
1005		*)
1006				# Otherwise, just print the filename
1007			echo $file
1008			;;
1009		esac
1010	done >> $CHANGEFILES
1011	CHANGELIST="$CHANGEFILES $CHANGELIST"
1012fi
1013
1014# Special case backups, including the master password file and
1015# ssh private host keys. The normal backup mechanisms for
1016# $check_changelist (see below) also print out the actual file
1017# differences and we don't want to do that for these files
1018#
1019echo $MP > $TMP1			# always add /etc/master.passwd
1020mtree -D -k type -f $SPECIALSPEC -I nodiff |
1021    sed '/^type=file/!d ; s/type=file \.//' | unvis >> $TMP1
1022grep -v '^$' $TMP1 | sort -u > $TMP2
1023
1024while read file; do
1025	backup_and_diff "$file" no
1026done < $TMP2
1027
1028
1029if [ -n "$CHANGELIST" ]; then
1030	grep -h -v '^$' $CHANGELIST | sort -u > $TMP1
1031	comm -23 $TMP1 $TMP2 | while read file; do
1032		backup_and_diff "$file" yes
1033	done
1034fi
1035
1036if have_pkgs; then
1037	if checkyesno check_pkg_vulnerabilities; then
1038		${pkg_admin} ${_compat_K_flag} audit >${OUTPUT} 2>&1
1039		if [ -s ${OUTPUT} ]; then
1040			printf "\nInstalled vulnerable packages:\n"
1041			cat ${OUTPUT}
1042		fi
1043	fi
1044
1045	if checkyesno check_pkg_signatures; then
1046		${pkg_admin} ${_compat_K_flag} check >${OUTPUT} 2>&1
1047		if [ $? -ne 0 ]; then
1048			printf "\nFiles with invalid signatures:\n"
1049			cat ${OUTPUT}
1050		fi
1051	fi
1052fi
1053
1054if [ -f /etc/security.local ]; then
1055	. /etc/security.local > $OUTPUT 2>&1
1056	if [ -s $OUTPUT ] ; then
1057		printf "\nRunning /etc/security.local:\n"
1058		cat $OUTPUT
1059	fi
1060fi
1061