security revision 1.27
1#!/bin/sh -
2#
3#	$NetBSD: security,v 1.27 1997/08/22 09:40:17 lukem Exp $
4#	from: @(#)security	8.1 (Berkeley) 6/9/93
5#
6
7PATH=/sbin:/usr/sbin:/bin:/usr/bin
8
9umask 077
10
11if [ -s /etc/security.conf ]; then
12	. /etc/security.conf
13fi
14
15SECUREDIR=/tmp/_securedir.$$
16if ! mkdir $SECUREDIR; then
17	echo can not create $SECUREDIR.
18	exit 1
19fi
20
21if ! cd $SECUREDIR; then
22	echo can not chdir to $SECUREDIR.
23	exit 1
24fi
25
26ERR=secure1.$$
27TMP1=secure2.$$
28TMP2=secure3.$$
29MPUID=secure4.$$
30MPPATH=secure5.$$
31LIST=secure6.$$
32OUTPUT=secure7.$$
33
34trap '/bin/rm -rf $SECUREDIR ; exit 0' 0 2 3
35
36MP=/etc/master.passwd
37
38# these is used several times.
39awk -F: '{ print $1 " " $3 }' $MP | sort -k2n > $MPUID
40awk -F: '{ print $1 " " $9 }' $MP | sort -k2 > $MPPATH
41
42# Check the master password file syntax.
43if [ "$check_passwd" = YES ]; then
44	awk '
45	BEGIN {
46		while ( getline < "/etc/shells" > 0 ) {
47			if ($LINE ~ /^\#/ || $LINE ~ /^$/ )
48				continue;
49			shells[$1]++;
50		}
51		FS=":";
52	}
53
54	{
55		if ($0 ~ /^[	 ]*$/) {
56			printf "Line %d is a blank line.\n", NR;
57			next;
58		}
59		if (NF != 10)
60			printf "Line %d has the wrong number of fields.\n", NR;
61		if ($1 !~ /^[A-Za-z0-9]*$/)
62			printf "Login %s has non-alphanumeric characters.\n",
63			    $1;
64		if (length($1) > 8)
65			printf "Login %s has more than 8 characters.\n", $1;
66		if ($2 == "")
67			printf "Login %s has no password.\n", $1;
68		if (length($2) != 13 && $2 != "") {
69			if ($10 == "" || shells[$10])
70		    printf "Login %s is off but still has a valid shell (%s)\n",
71				    $1, $10;
72		} else if (! shells[$10])
73			printf "Login %s does not have a valid shell (%s)\n",
74			    $1, $10;
75		if ($3 == 0 && $1 != "root" && $1 != "toor")
76			printf "Login %s has a user id of 0.\n", $1;
77		if ($3 < 0)
78			printf "Login %s has a negative user id.\n", $1;
79		if ($4 < 0)
80			printf "Login %s has a negative group id.\n", $1;
81	}' < $MP > $OUTPUT
82	if [ -s $OUTPUT ] ; then
83		printf "\nChecking the $MP file:\n"
84		cat $OUTPUT
85	fi
86
87	awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
88	if [ -s $OUTPUT ] ; then
89		printf "\n$MP has duplicate user names.\n"
90		column $OUTPUT
91	fi
92
93	< $MPUID uniq -d -f 1 | awk '{ print $2 }' > $TMP2
94	if [ -s $TMP2 ] ; then
95		printf "\n$MP has duplicate user id's.\n"
96		while read uid; do
97			grep -w $uid $MPUID
98		done < $TMP2 | column
99	fi
100fi
101
102# Backup the master password file; a special case, the normal backup
103# mechanisms also print out file differences and we don't want to do
104# that because this file has encrypted passwords in it.
105CUR=/var/backups/`basename $MP`.current
106BACK=/var/backups/`basename $MP`.backup
107if [ -s $CUR ] ; then
108	if cmp -s $CUR $MP; then
109		:
110	else
111		cp -p $CUR $BACK
112		cp -p $MP $CUR
113		chown root.wheel $CUR
114	fi
115else
116	cp -p $MP $CUR
117	chown root.wheel $CUR
118fi
119
120# Check the group file syntax.
121if [ "$check_group" = YES ]; then
122	GRP=/etc/group
123	awk -F: '{
124		if ($0 ~ /^[	 ]*$/) {
125			printf "Line %d is a blank line.\n", NR;
126			next;
127		}
128		if (NF != 4)
129			printf "Line %d has the wrong number of fields.\n", NR;
130		if ($1 !~ /^[A-za-z0-9]*$/)
131			printf "Group %s has non-alphanumeric characters.\n",
132			    $1;
133		if (length($1) > 8)
134			printf "Group %s has more than 8 characters.\n", $1;
135		if ($3 !~ /[0-9]*/)
136			printf "Login %s has a negative group id.\n", $1;
137	}' < $GRP > $OUTPUT
138	if [ -s $OUTPUT ] ; then
139		printf "\nChecking the $GRP file:\n"
140		cat $OUTPUT
141	fi
142
143	awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
144	if [ -s $OUTPUT ] ; then
145		printf "\n$GRP has duplicate group names.\n"
146		column $OUTPUT
147	fi
148fi
149
150# Check for root paths, umask values in startup files.
151# The check for the root paths is problematical -- it's likely to fail
152# in other environments.  Once the shells have been modified to warn
153# of '.' in the path, the path tests should go away.
154if [ "$check_rootdotfiles" = YES ]; then
155	cp /dev/null $OUTPUT
156	rhome=`csh -fc "echo ~root"`
157	umaskset=no
158	list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
159	for i in $list ; do
160		if [ -f $i ] ; then
161			if egrep umask $i > /dev/null ; then
162				umaskset=yes
163			fi
164			egrep umask $i |
165			awk '$2 % 100 < 20 \
166				{ print "\tRoot umask is group writeable" }
167			     $2 % 10 < 2 \
168				{ print "\tRoot umask is other writeable" }' \
169			    >> $OUTPUT
170			SAVE_PATH=$PATH
171			unset PATH
172			/bin/csh -f -s << end-of-csh > /dev/null 2>&1
173				source $i
174				/bin/ls -ldgT \$path > $TMP1
175end-of-csh
176			PATH=$SAVE_PATH
177			awk '{
178				if ($10 ~ /^\.$/) {
179					print "\tThe root path includes .";
180					next;
181				}
182			     }
183			     $1 ~ /^d....w/ \
184		{ print "\tRoot path directory " $10 " is group writeable." } \
185			     $1 ~ /^d.......w/ \
186		{ print "\tRoot path directory " $10 " is other writeable." }' \
187			< $TMP1 >> $OUTPUT
188		fi
189	done
190	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
191		printf "\nChecking root csh paths, umask values:\n$list\n\n"
192		if [ -s $OUTPUT ]; then
193			cat $OUTPUT
194		fi
195		if [ $umaskset = "no" ] ; then
196		    printf "\tRoot csh startup files do not set the umask.\n"
197		fi
198	fi
199
200	cp /dev/null $OUTPUT
201	rhome=/root
202	umaskset=no
203	list="/etc/profile ${rhome}/.profile"
204	for i in $list; do
205		if [ -f $i ] ; then
206			if egrep umask $i > /dev/null ; then
207				umaskset=yes
208			fi
209			egrep umask $i |
210			awk '$2 % 100 < 20 \
211				{ print "\tRoot umask is group writeable" } \
212			     $2 % 10 < 2 \
213				{ print "\tRoot umask is other writeable" }' \
214			    >> $OUTPUT
215			SAVE_PATH=$PATH
216			unset PATH
217			/bin/sh << end-of-sh > /dev/null 2>&1
218				. $i
219				list=\`echo \$PATH | /usr/bin/sed -e \
220				    's/^:/.:/;s/:$/:./;s/::/:.:/g;s/:/ /g'\`
221				/bin/ls -ldgT \$list > $TMP1
222end-of-sh
223			PATH=$SAVE_PATH
224			awk '{
225				if ($10 ~ /^\.$/) {
226					print "\tThe root path includes .";
227					next;
228				}
229			     }
230			     $1 ~ /^d....w/ \
231		{ print "\tRoot path directory " $10 " is group writeable." } \
232			     $1 ~ /^d.......w/ \
233		{ print "\tRoot path directory " $10 " is other writeable." }' \
234			< $TMP1 >> $OUTPUT
235
236		fi
237	done
238	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
239		printf "\nChecking root sh paths, umask values:\n$list\n"
240		if [ -s $OUTPUT ]; then
241			cat $OUTPUT
242		fi
243		if [ $umaskset = "no" ] ; then
244			printf "\tRoot sh startup files do not set the umask.\n"
245		fi
246	fi
247fi
248
249# Root and uucp should both be in /etc/ftpusers.
250# XXX This should be updated to support the new format...
251if [ "$check_ftpusers" = YES ]; then
252	list="root uucp"
253	for i in $list; do
254		if ! egrep "^$i$" /etc/ftpusers > /dev/null ; then
255			printf "\n$i is not listed in /etc/ftpusers file.\n"
256		fi
257	done
258fi
259
260# Uudecode should not be in the /etc/aliases file.
261if [ "$check_aliases" = YES ]; then
262	if egrep '^[^#]*(uudecode|decode).*\|' /etc/aliases; then
263		printf "\nEntry for uudecode in /etc/aliases file.\n"
264	fi
265fi
266
267# Files that should not have + signs.
268if [ "$check_rhosts" = YES ]; then
269	list="/etc/hosts.equiv /etc/hosts.lpd"
270	for f in $list ; do
271		if [ -f $f ] && egrep '\+' $f > /dev/null ; then
272			printf "\nPlus sign in $f file.\n"
273		fi
274	done
275
276	# Check for special users with .rhosts files.  Only root and toor should
277	# have .rhosts files.  Also, .rhosts files should not have plus signs.
278	awk -F: '$1 != "root" && $1 != "toor" && \
279		($3 < 100 || $1 == "ftp" || $1 == "uucp") \
280			{ print $1 " " $9 }' $MP |
281	sort -k2 |
282	while read uid homedir; do
283		if [ -f ${homedir}/.rhosts ] ; then
284			rhost=`ls -ldgT ${homedir}/.rhosts`
285			printf "$uid: $rhost\n"
286		fi
287	done > $OUTPUT
288	if [ -s $OUTPUT ] ; then
289		printf "\nChecking for special users with .rhosts files.\n"
290		cat $OUTPUT
291	fi
292
293	while read uid homedir; do
294		if [ -f ${homedir}/.rhosts ] && \
295		    egrep '\+' ${homedir}/.rhosts > /dev/null ; then
296			printf "$uid: + in .rhosts file.\n"
297		fi
298	done < $MPPATH > $OUTPUT
299	if [ -s $OUTPUT ] ; then
300		printf "\nChecking .rhosts files syntax.\n"
301		cat $OUTPUT
302	fi
303fi
304
305# Check home directories.  Directories should not be owned by someone else
306# or writeable.
307if [ "$check_homes" = YES ]; then
308	while read uid homedir; do
309		if [ -d ${homedir}/ ] ; then
310			file=`ls -ldgT ${homedir}`
311			printf "$uid $file\n"
312		fi
313	done < $MPPATH |
314	awk '$1 != $4 && $4 != "root" \
315		{ print "user " $1 " home directory is owned by " $4 }
316	     $2 ~ /^-....w/ \
317		{ print "user " $1 " home directory is group writeable" }
318	     $2 ~ /^-.......w/ \
319		{ print "user " $1 " home directory is other writeable" }' \
320	    > $OUTPUT
321	if [ -s $OUTPUT ] ; then
322		printf "\nChecking home directories.\n"
323		cat $OUTPUT
324	fi
325
326	# Files that should not be owned by someone else or readable.
327	list=".Xauthority .netrc"
328	while read uid homedir; do
329		for f in $list ; do
330			file=${homedir}/${f}
331			if [ -f $file ] ; then
332				printf "$uid $f `ls -ldgT $file`\n"
333			fi
334		done
335	done < $MPPATH |
336	awk '$1 != $5 && $5 != "root" \
337		{ print "user " $1 " " $2 " file is owned by " $5 }
338	     $3 ~ /^-...r/ \
339		{ print "user " $1 " " $2 " file is group readable" }
340	     $3 ~ /^-......r/ \
341		{ print "user " $1 " " $2 " file is other readable" }
342	     $3 ~ /^-....w/ \
343		{ print "user " $1 " " $2 " file is group writeable" }
344	     $3 ~ /^-.......w/ \
345		{ print "user " $1 " " $2 " file is other writeable" }' \
346	    > $OUTPUT
347
348	# Files that should not be owned by someone else or writeable.
349	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
350	      .cshrc .emacs .exrc .forward .history .klogin .login .logout \
351	      .profile .qmail .rc_history .rhosts .tcshrc .twmrc .xinitrc \
352	      .xsession"
353	while read uid homedir; do
354		for f in $list ; do
355			file=${homedir}/${f}
356			if [ -f $file ] ; then
357				printf "$uid $f `ls -ldgT $file`\n"
358			fi
359		done
360	done < $MPPATH |
361	awk '$1 != $5 && $5 != "root" \
362		{ print "user " $1 " " $2 " file is owned by " $5 }
363	     $3 ~ /^-....w/ \
364		{ print "user " $1 " " $2 " file is group writeable" }
365	     $3 ~ /^-.......w/ \
366		{ print "user " $1 " " $2 " file is other writeable" }' \
367	    >> $OUTPUT
368	if [ -s $OUTPUT ] ; then
369		printf "\nChecking dot files.\n"
370		cat $OUTPUT
371	fi
372fi
373
374# Mailboxes should be owned by user and unreadable.
375if [ "$check_varmail" = YES ]; then
376	ls -l /var/mail | sed 1d | \
377	awk '$3 != $9 \
378		{ print "user " $9 " mailbox is owned by " $3 }
379	     $1 != "-rw-------" \
380		{ print "user " $9 " mailbox is " $1 ", group " $4 }' > $OUTPUT
381	if [ -s $OUTPUT ] ; then
382		printf "\nChecking mailbox ownership.\n"
383		cat $OUTPUT
384	fi
385fi
386
387if [ "$check_nfs" = YES ]; then
388	if [ -f /etc/exports ]; then
389	    # File systems should not be globally exported.
390	    awk '{
391		# ignore comments and blank lines
392		if ($LINE ~ /^\#/ || $LINE ~ /^$/ )
393			next;
394
395		readonly = 0;
396		for (i = 2; i <= NF; ++i) {
397			if ($i ~ /-ro/)
398				readonly = 1;
399			else if ($i !~ /^-/)
400				next;
401		}
402		if (readonly)
403			print "File system " $1 " globally exported, read-only."
404		else
405			print "File system " $1 " globally exported, read-write."
406	    }' < /etc/exports > $OUTPUT
407	    if [ -s $OUTPUT ] ; then
408		printf "\nChecking for globally exported file systems.\n"
409		cat $OUTPUT
410	    fi
411	fi
412fi
413
414# Display any changes in setuid files and devices.
415if [ "$check_devices" = YES ]; then
416	printf "\nChecking setuid files and devices:\n"
417	(find / \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \
418			-o -fstype procfs \) -a -prune -o \
419	    \( \( -perm -u+s -a ! -type d \) -o \
420	       \( -perm -g+s -a ! -type d \) -o \
421	       -type b -o -type c \) -print0 | \
422	xargs -0 ls -ldgTq | sort +9 > $LIST) 2> $OUTPUT
423
424	# Display any errors that occurred during system file walk.
425	if [ -s $OUTPUT ] ; then
426		printf "Setuid/device find errors:\n"
427		cat $OUTPUT
428		printf "\n"
429	fi
430
431	# Display any changes in the setuid file list.
432	egrep -v '^[bc]' $LIST > $TMP1
433	if [ -s $TMP1 ] ; then
434		# Check to make sure uudecode isn't setuid.
435		if grep -w uudecode $TMP1 > /dev/null ; then
436			printf "\nUudecode is setuid.\n"
437		fi
438
439		CUR=/var/backups/setuid.current
440		BACK=/var/backups/setuid.backup
441
442		if [ -s $CUR ] ; then
443			if cmp -s $CUR $TMP1 ; then
444				:
445			else
446				> $TMP2
447				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
448				if [ -s $OUTPUT ] ; then
449					printf "Setuid additions:\n"
450					tee -a $TMP2 < $OUTPUT
451					printf "\n"
452				fi
453
454				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
455				if [ -s $OUTPUT ] ; then
456					printf "Setuid deletions:\n"
457					tee -a $TMP2 < $OUTPUT
458					printf "\n"
459				fi
460
461				sort -k10 $TMP2 $CUR $TMP1 | \
462				    sed -e 's/[	 ][	 ]*/ /g' | \
463				    uniq -u > $OUTPUT
464				if [ -s $OUTPUT ] ; then
465					printf "Setuid changes:\n"
466					column -t $OUTPUT
467					printf "\n"
468				fi
469
470				cp $CUR $BACK
471				cp $TMP1 $CUR
472			fi
473		else
474			printf "Setuid additions:\n"
475			column -t $TMP1
476			printf "\n"
477			cp $TMP1 $CUR
478		fi
479	fi
480
481	# Check for block and character disk devices that are readable or
482	# writeable or not owned by root.operator.
483	>$TMP1
484	DISKLIST="acd ccd cd ch fd hk hp mcd md ra rb rd rl rx rz \
485	    sd se ss tz uk up vnd wd xd xy"
486#	DISKLIST="$DISKLIST ct mt st wt"
487	for i in $DISKLIST; do
488		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
489		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
490	done
491
492	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
493		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
494		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
495	if [ -s $OUTPUT ] ; then
496		printf "\nChecking disk ownership and permissions.\n"
497		cat $OUTPUT
498		printf "\n"
499	fi
500
501	# Display any changes in the device file list.
502	egrep '^[bc]' $LIST | sort -k11 > $TMP1
503	if [ -s $TMP1 ] ; then
504		CUR=/var/backups/device.current
505		BACK=/var/backups/device.backup
506
507		if [ -s $CUR ] ; then
508			if cmp -s $CUR $TMP1 ; then
509				:
510			else
511				> $TMP2
512				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
513				if [ -s $OUTPUT ] ; then
514					printf "Device additions:\n"
515					tee -a $TMP2 < $OUTPUT
516					printf "\n"
517				fi
518
519				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
520				if [ -s $OUTPUT ] ; then
521					printf "Device deletions:\n"
522					tee -a $TMP2 < $OUTPUT
523					printf "\n"
524				fi
525
526				# Report any block device change. Ignore
527				# character devices, only the name is
528				# significant.
529				cat $TMP2 $CUR $TMP1 | \
530				    sed -e '/^c/d' | \
531				    sort -k11 | \
532				    sed -e 's/[	 ][	 ]*/ /g' | \
533				    uniq -u > $OUTPUT
534				if [ -s $OUTPUT ] ; then
535					printf "Block device changes:\n"
536					column -t $OUTPUT
537					printf "\n"
538				fi
539
540				cp $CUR $BACK
541				cp $TMP1 $CUR
542			fi
543		else
544			printf "Device additions:\n"
545			column -t $TMP1
546			printf "\n"
547			cp $TMP1 $CUR
548		fi
549	fi
550fi
551
552# Check special files.
553# Check system binaries.
554#
555# Create the mtree tree specifications using:
556#
557#	mtree -cx -pDIR -kcksum,gid,mode,nlink,size,link,time,uid > DIR.secure
558#	chown root.wheel DIR.secure
559#	chmod 600 DIR.secure
560#
561# Note, this is not complete protection against Trojan horsed binaries, as
562# the hacker can modify the tree specification to match the replaced binary.
563# For details on really protecting yourself against modified binaries, see
564# the mtree(8) manual page.
565if [ "$check_mtree" = YES ]; then
566	mtree -e -p / -f /etc/mtree/special > $OUTPUT
567	if [ -s $OUTPUT ]; then
568		printf "\nChecking special files and directories.\n"
569		cat $OUTPUT
570	fi
571
572	> $OUTPUT
573	for file in /etc/mtree/*.secure; do
574		[ $file = '/etc/mtree/*.secure' ] && continue
575		tree=`sed -n -e '3s/.* //p' -e 3q $file`
576		mtree -f $file -p $tree > $TMP1
577		if [ -s $TMP1 ]; then
578			printf "\nChecking $tree:\n" >> $OUTPUT
579			cat $TMP1 >> $OUTPUT
580		fi
581	done
582	if [ -s $OUTPUT ]; then
583		printf "\nChecking system binaries:\n"
584		cat $OUTPUT
585	fi
586fi
587
588# List of files that get backed up and checked for any modifications.  Each
589# file is expected to have two backups, /var/backups/file.{current,backup}.
590# Any changes cause the files to rotate.
591if [ "$check_changelist" = YES ] && [ -s /etc/changelist ] ; then
592	for file in `egrep -v "^#|$MP" /etc/changelist`; do
593		CUR=/var/backups/`basename $file`.current
594		BACK=/var/backups/`basename $file`.backup
595		if [ -s $file ]; then
596			if [ -s $CUR ] ; then
597				diff $CUR $file > $OUTPUT
598				if [ -s $OUTPUT ] ; then
599		printf "\n======\n%s diffs (OLD < > NEW)\n======\n" $file
600					cat $OUTPUT
601					cp -p $CUR $BACK
602					cp -p $file $CUR
603					chown root.wheel $CUR $BACK
604				fi
605			else
606				cp -p $file $CUR
607				chown root.wheel $CUR
608			fi
609		fi
610	done
611fi
612