xref: /netbsd-src/etc/security (revision f5d3fbbc6ff4a77159fb268d247bd94cb7d7e332)
1#!/bin/sh -
2#
3#	$NetBSD: security,v 1.30 1997/10/08 16:13:44 mycroft 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.$$
29MPBYUID=secure4.$$
30MPBYPATH=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 > $MPBYUID
40awk -F: '{ print $1 " " $9 }' $MP | sort -k2 > $MPBYPATH
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	< $MPBYUID 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 $MPBYUID
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	> $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	> $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.
250if [ "$check_ftpusers" = YES ]; then
251	> $OUTPUT
252	list="uucp "`awk '$2 == 0 { print $1 }' $MPBYUID`
253	for i in $list; do
254		if /usr/libexec/ftpd -C $i ; then
255			printf "\t$i is not denied\n" >> $OUTPUT
256		fi
257	done
258	if [ -s $OUTPUT ]; then
259		printf "\nChecking the /etc/ftpusers configuration:\n"
260		cat $OUTPUT
261	fi
262fi
263
264# Uudecode should not be in the /etc/aliases file.
265if [ "$check_aliases" = YES ]; then
266	if egrep '^[^#]*(uudecode|decode).*\|' /etc/aliases; then
267		printf "\nEntry for uudecode in /etc/aliases file.\n"
268	fi
269fi
270
271# Files that should not have + signs.
272if [ "$check_rhosts" = YES ]; then
273	list="/etc/hosts.equiv /etc/hosts.lpd"
274	for f in $list ; do
275		if [ -f $f ] && egrep '\+' $f > /dev/null ; then
276			printf "\nPlus sign in $f file.\n"
277		fi
278	done
279
280	# Check for special users with .rhosts files.  Only root and toor should
281	# have .rhosts files.  Also, .rhosts files should not have plus signs.
282	awk -F: '$1 != "root" && $1 != "toor" && \
283		($3 < 100 || $1 == "ftp" || $1 == "uucp") \
284			{ print $1 " " $9 }' $MP |
285	sort -k2 |
286	while read uid homedir; do
287		if [ -f ${homedir}/.rhosts ] ; then
288			rhost=`ls -ldgT ${homedir}/.rhosts`
289			printf "$uid: $rhost\n"
290		fi
291	done > $OUTPUT
292	if [ -s $OUTPUT ] ; then
293		printf "\nChecking for special users with .rhosts files.\n"
294		cat $OUTPUT
295	fi
296
297	while read uid homedir; do
298		if [ -f ${homedir}/.rhosts ] && \
299		    egrep '\+' ${homedir}/.rhosts > /dev/null ; then
300			printf "$uid: + in .rhosts file.\n"
301		fi
302	done < $MPBYPATH > $OUTPUT
303	if [ -s $OUTPUT ] ; then
304		printf "\nChecking .rhosts files syntax.\n"
305		cat $OUTPUT
306	fi
307fi
308
309# Check home directories.  Directories should not be owned by someone else
310# or writeable.
311if [ "$check_homes" = YES ]; then
312	while read uid homedir; do
313		if [ -d ${homedir}/ ] ; then
314			file=`ls -ldgT ${homedir}`
315			printf "$uid $file\n"
316		fi
317	done < $MPBYPATH |
318	awk '$1 != $4 && $4 != "root" \
319		{ print "user " $1 " home directory is owned by " $4 }
320	     $2 ~ /^-....w/ \
321		{ print "user " $1 " home directory is group writeable" }
322	     $2 ~ /^-.......w/ \
323		{ print "user " $1 " home directory is other writeable" }' \
324	    > $OUTPUT
325	if [ -s $OUTPUT ] ; then
326		printf "\nChecking home directories.\n"
327		cat $OUTPUT
328	fi
329
330	# Files that should not be owned by someone else or readable.
331	list=".Xauthority .netrc"
332	while read uid homedir; do
333		for f in $list ; do
334			file=${homedir}/${f}
335			if [ -f $file ] ; then
336				printf "$uid $f `ls -ldgT $file`\n"
337			fi
338		done
339	done < $MPBYPATH |
340	awk '$1 != $5 && $5 != "root" \
341		{ print "user " $1 " " $2 " file is owned by " $5 }
342	     $3 ~ /^-...r/ \
343		{ print "user " $1 " " $2 " file is group readable" }
344	     $3 ~ /^-......r/ \
345		{ print "user " $1 " " $2 " file is other readable" }
346	     $3 ~ /^-....w/ \
347		{ print "user " $1 " " $2 " file is group writeable" }
348	     $3 ~ /^-.......w/ \
349		{ print "user " $1 " " $2 " file is other writeable" }' \
350	    > $OUTPUT
351
352	# Files that should not be owned by someone else or writeable.
353	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
354	      .cshrc .emacs .exrc .forward .history .klogin .login .logout \
355	      .profile .qmail .rc_history .rhosts .tcshrc .twmrc .xinitrc \
356	      .xsession"
357	while read uid homedir; do
358		for f in $list ; do
359			file=${homedir}/${f}
360			if [ -f $file ] ; then
361				printf "$uid $f `ls -ldgT $file`\n"
362			fi
363		done
364	done < $MPBYPATH |
365	awk '$1 != $5 && $5 != "root" \
366		{ print "user " $1 " " $2 " file is owned by " $5 }
367	     $3 ~ /^-....w/ \
368		{ print "user " $1 " " $2 " file is group writeable" }
369	     $3 ~ /^-.......w/ \
370		{ print "user " $1 " " $2 " file is other writeable" }' \
371	    >> $OUTPUT
372	if [ -s $OUTPUT ] ; then
373		printf "\nChecking dot files.\n"
374		cat $OUTPUT
375	fi
376fi
377
378# Mailboxes should be owned by user and unreadable.
379if [ "$check_varmail" = YES ]; then
380	ls -l /var/mail | sed 1d | \
381	awk '$3 != $9 \
382		{ print "user " $9 " mailbox is owned by " $3 }
383	     $1 != "-rw-------" \
384		{ print "user " $9 " mailbox is " $1 ", group " $4 }' > $OUTPUT
385	if [ -s $OUTPUT ] ; then
386		printf "\nChecking mailbox ownership.\n"
387		cat $OUTPUT
388	fi
389fi
390
391if [ "$check_nfs" = YES ]; then
392	if [ -f /etc/exports ]; then
393	    # File systems should not be globally exported.
394	    awk '{
395		# ignore comments and blank lines
396		if ($LINE ~ /^\#/ || $LINE ~ /^$/ )
397			next;
398
399		readonly = 0;
400		for (i = 2; i <= NF; ++i) {
401			if ($i ~ /-ro/)
402				readonly = 1;
403			else if ($i !~ /^-/)
404				next;
405		}
406		if (readonly)
407			print "File system " $1 " globally exported, read-only."
408		else
409			print "File system " $1 " globally exported, read-write."
410	    }' < /etc/exports > $OUTPUT
411	    if [ -s $OUTPUT ] ; then
412		printf "\nChecking for globally exported file systems.\n"
413		cat $OUTPUT
414	    fi
415	fi
416fi
417
418# Display any changes in setuid files and devices.
419if [ "$check_devices" = YES ]; then
420	> $ERR
421	(find / \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \
422			-o -fstype procfs \) -a -prune -o \
423	    \( \( -perm -u+s -a ! -type d \) -o \
424	       \( -perm -g+s -a ! -type d \) -o \
425	       -type b -o -type c \) -print0 | \
426	xargs -0 ls -ldgTq | sort +9 > $LIST) 2> $OUTPUT
427
428	# Display any errors that occurred during system file walk.
429	if [ -s $OUTPUT ] ; then
430		printf "Setuid/device find errors:\n" >> $ERR
431		cat $OUTPUT >> $ERR
432		printf "\n" >> $ERR
433	fi
434
435	# Display any changes in the setuid file list.
436	egrep -v '^[bc]' $LIST > $TMP1
437	if [ -s $TMP1 ] ; then
438		# Check to make sure uudecode isn't setuid.
439		if grep -w uudecode $TMP1 > /dev/null ; then
440			printf "\nUudecode is setuid.\n" >> $ERR
441		fi
442
443		CUR=/var/backups/setuid.current
444		BACK=/var/backups/setuid.backup
445
446		if [ -s $CUR ] ; then
447			if cmp -s $CUR $TMP1 ; then
448				:
449			else
450				> $TMP2
451				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
452				if [ -s $OUTPUT ] ; then
453					printf "Setuid additions:\n" >> $ERR
454					tee -a $TMP2 < $OUTPUT >> $ERR
455					printf "\n" >> $ERR
456				fi
457
458				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
459				if [ -s $OUTPUT ] ; then
460					printf "Setuid deletions:\n" >> $ERR
461					tee -a $TMP2 < $OUTPUT >> $ERR
462					printf "\n" >> $ERR
463				fi
464
465				sort -k10 $TMP2 $CUR $TMP1 | \
466				    sed -e 's/[	 ][	 ]*/ /g' | \
467				    uniq -u > $OUTPUT
468				if [ -s $OUTPUT ] ; then
469					printf "Setuid changes:\n" >> $ERR
470					column -t $OUTPUT >> $ERR
471					printf "\n" >> $ERR
472				fi
473
474				cp $CUR $BACK
475				cp $TMP1 $CUR
476			fi
477		else
478			printf "Setuid additions:\n" >> $ERR
479			column -t $TMP1 >> $ERR
480			printf "\n" >> $ERR
481			cp $TMP1 $CUR
482		fi
483	fi
484
485	# Check for block and character disk devices that are readable or
486	# writeable or not owned by root.operator.
487	>$TMP1
488	DISKLIST="acd ccd cd ch fd hk hp mcd md ra rb rd rl rx rz \
489	    sd se ss tz uk up vnd wd xd xy"
490#	DISKLIST="$DISKLIST ct mt st wt"
491	for i in $DISKLIST; do
492		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
493		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
494	done
495
496	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
497		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
498		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
499	if [ -s $OUTPUT ] ; then
500		printf "\nChecking disk ownership and permissions.\n" >> $ERR
501		cat $OUTPUT >> $ERR
502		printf "\n" >> $ERR
503	fi
504
505	# Display any changes in the device file list.
506	egrep '^[bc]' $LIST | sort -k11 > $TMP1
507	if [ -s $TMP1 ] ; then
508		CUR=/var/backups/device.current
509		BACK=/var/backups/device.backup
510
511		if [ -s $CUR ] ; then
512			if cmp -s $CUR $TMP1 ; then
513				:
514			else
515				> $TMP2
516				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
517				if [ -s $OUTPUT ] ; then
518					printf "Device additions:\n" >> $ERR
519					tee -a $TMP2 < $OUTPUT >> $ERR
520					printf "\n" >> $ERR
521				fi
522
523				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
524				if [ -s $OUTPUT ] ; then
525					printf "Device deletions:\n" >> $ERR
526					tee -a $TMP2 < $OUTPUT >> $ERR
527					printf "\n" >> $ERR
528				fi
529
530				# Report any block device change. Ignore
531				# character devices, only the name is
532				# significant.
533				cat $TMP2 $CUR $TMP1 | \
534				    sed -e '/^c/d' | \
535				    sort -k11 | \
536				    sed -e 's/[	 ][	 ]*/ /g' | \
537				    uniq -u > $OUTPUT
538				if [ -s $OUTPUT ] ; then
539					printf "Block device changes:\n" >> $ERR
540					column -t $OUTPUT >> $ERR
541					printf "\n" >> $ERR
542				fi
543
544				cp $CUR $BACK
545				cp $TMP1 $CUR
546			fi
547		else
548			printf "Device additions:\n" >> $ERR
549			column -t $TMP1 >> $ERR
550			printf "\n" >> $ERR
551			cp $TMP1 $CUR >> $ERR
552		fi
553	fi
554	if [ -s $ERR ] ; then
555		printf "\nChecking setuid files and devices:\n"
556		cat $ERR
557		printf "\n"
558	fi
559fi
560
561# Check special files.
562# Check system binaries.
563#
564# Create the mtree tree specifications using:
565#
566#	mtree -cx -pDIR -kcksum,gid,mode,nlink,size,link,time,uid > DIR.secure
567#	chown root.wheel DIR.secure
568#	chmod 600 DIR.secure
569#
570# Note, this is not complete protection against Trojan horsed binaries, as
571# the hacker can modify the tree specification to match the replaced binary.
572# For details on really protecting yourself against modified binaries, see
573# the mtree(8) manual page.
574if [ "$check_mtree" = YES ]; then
575	mtree -e -p / -f /etc/mtree/special > $OUTPUT
576	if [ -s $OUTPUT ]; then
577		printf "\nChecking special files and directories.\n"
578		cat $OUTPUT
579	fi
580
581	> $OUTPUT
582	for file in /etc/mtree/*.secure; do
583		[ $file = '/etc/mtree/*.secure' ] && continue
584		tree=`sed -n -e '3s/.* //p' -e 3q $file`
585		mtree -f $file -p $tree > $TMP1
586		if [ -s $TMP1 ]; then
587			printf "\nChecking $tree:\n" >> $OUTPUT
588			cat $TMP1 >> $OUTPUT
589		fi
590	done
591	if [ -s $OUTPUT ]; then
592		printf "\nChecking system binaries:\n"
593		cat $OUTPUT
594	fi
595fi
596
597# List of files that get backed up and checked for any modifications.  Each
598# file is expected to have two backups, /var/backups/file.{current,backup}.
599# Any changes cause the files to rotate.
600if [ "$check_changelist" = YES ] && [ -s /etc/changelist ] ; then
601	for file in `egrep -v "^#|$MP" /etc/changelist`; do
602		CUR=/var/backups/`basename $file`.current
603		BACK=/var/backups/`basename $file`.backup
604		if [ -f $file ]; then
605			if [ -f $CUR ] ; then
606				diff $CUR $file > $OUTPUT
607				if [ -s $OUTPUT ] ; then
608		printf "\n======\n%s diffs (OLD < > NEW)\n======\n" $file
609					cat $OUTPUT
610					mv -f $CUR $BACK
611					cp -p $file $CUR
612					chown root.wheel $CUR
613				fi
614			else
615		printf "\n======\n%s added\n======\n" $file
616				diff /dev/null $file
617				cp -p $file $CUR
618				chown root.wheel $CUR
619			fi
620		else
621			if [ -f $CUR ]; then
622		printf "\n======\n%s removed\n======\n" $file
623				diff $CUR /dev/null
624				mv -f $CUR $BACK
625			fi
626		fi
627	done
628fi
629