date and time created 92/05/01 17:01:20 by bostic
[unix-history] / usr / src / etc / security
CommitLineData
525ea79f
KB
1#!/bin/sh -
2#
8c93a907 3# @(#)security 5.23 (Berkeley) %G%
525ea79f 4#
07576f30 5
fd70cf43 6PATH=/sbin:/usr/sbin:/bin:/usr/bin
525ea79f 7
e9a107b1 8umask 077
383148a3 9
40835542
KB
10ERR=/tmp/_secure1.$$
11TMP1=/tmp/_secure2.$$
12TMP2=/tmp/_secure3.$$
e9a107b1
KB
13TMP3=/tmp/_secure4.$$
14LIST=/tmp/_secure5.$$
15OUTPUT=/tmp/_secure6.$$
16
17trap 'rm -f $ERR $TMP1 $TMP2 $TMP3 $LIST $OUTPUT' 0
18
28225475 19# Check the master password file syntax.
e9a107b1
KB
20MP=/etc/master.passwd
21awk -F: '{
22 if ($0 ~ /^[ ]*$/) {
23 printf("Line %d is a blank line.\n", NR);
24 next;
25 }
26 if (NF != 10)
27 printf("Line %d has the wrong number of fields.\n", NR);
d4409bb4 28 if ($1 !~ /^[A-Za-z0-9]*$/)
e9a107b1
KB
29 printf("Login %s has non-numeric characters.\n", $1);
30 if (length($1) > 8)
31 printf("Login %s has more than 8 characters.\n", $1);
32 if ($2 == "")
33 printf("Login %s has no password.\n", $1);
34 if (length($2) != 13 && ($10 ~ /.*sh$/ || $10 == ""))
35 printf("Login %s is off but still has a valid shell.\n", $1);
36 if ($3 == 0 && $1 != "root" && $1 != "toor")
37 printf("Login %s has a user id of 0.\n", $1);
38 if ($3 < 0)
39 printf("Login %s has a negative user id.\n", $1);
40 if ($4 < 0)
d4409bb4 41 printf("Login %s has a negative group id.\n", $1);
e9a107b1
KB
42}' < $MP > $OUTPUT
43if [ -s $OUTPUT ] ; then
44 printf "\nChecking the $MP file:\n"
45 cat $OUTPUT
46fi
fd70cf43 47
e9a107b1
KB
48awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
49if [ -s $OUTPUT ] ; then
50 printf "\n$MP has duplicate user names.\n"
51 column $OUTPUT
52fi
fd70cf43 53
e9a107b1
KB
54awk -F: '{ print $1 " " $3 }' $MP | sort -n +1 | tee $TMP1 |
55uniq -d -f 1 | awk '{ print $2 }' > $TMP2
56if [ -s $TMP2 ] ; then
57 printf "\n$MP has duplicate user id's.\n"
58 while read uid; do
59 grep -w $uid $TMP1
60 done < $TMP2 | column
61fi
8e55538e 62
8c93a907
KB
63# Backup the master password file; a special case, the normal backup
64# mechanisms also print out file differences and we don't want to do
65# that because this file has encrypted passwords in it.
66CUR=/var/backups/`basename $MP`.current
67BACK=/var/backups/`basename $MP`.backup
68if [ -s $CUR ] ; then
69 if cmp -s $CUR $MP; then
70 :
71 else
72 cp -p $CUR $BACK
73 cp -p $MP $CUR
74 chown root.wheel $CUR
75 fi
76else
77 cp -p $MP $CUR
78 chown root.wheel $CUR
79fi
80
e9a107b1
KB
81# Check the group file syntax.
82GRP=/etc/group
83awk -F: '{
84 if ($0 ~ /^[ ]*$/) {
85 printf("Line %d is a blank line.\n", NR);
86 next;
87 }
88 if (NF != 4)
89 printf("Line %d has the wrong number of fields.\n", NR);
76c990fe 90 if ($1 !~ /^[A-za-z0-9]*$/)
e9a107b1
KB
91 printf("Group %s has non-numeric characters.\n", $1);
92 if (length($1) > 8)
93 printf("Group %s has more than 8 characters.\n", $1);
94 if ($3 !~ /[0-9]*/)
d4409bb4 95 printf("Login %s has a negative group id.\n", $1);
e9a107b1
KB
96}' < $GRP > $OUTPUT
97if [ -s $OUTPUT ] ; then
98 printf "\nChecking the $GRP file:\n"
99 cat $OUTPUT
100fi
48e0a733 101
e9a107b1
KB
102awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
103if [ -s $OUTPUT ] ; then
104 printf "\n$GRP has duplicate group names.\n"
105 column $OUTPUT
106fi
48e0a733 107
e9a107b1 108# Check for root paths, umask values in startup files.
28225475
KB
109# The check for the root paths is problematical -- it's likely to fail
110# in other environments. Once the shells have been modified to warn
111# of '.' in the path, the path tests should go away.
e9a107b1
KB
112> $OUTPUT
113rhome=/root
114umaskset=no
115list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
116for i in $list ; do
117 if [ -f $i ] ; then
118 if egrep umask $i > /dev/null ; then
119 umaskset=yes
48e0a733 120 fi
e9a107b1
KB
121 egrep umask $i |
122 awk '$2 % 100 < 20 \
123 { print "Root umask is group writeable" }
124 $2 % 10 < 2 \
125 { print "Root umask is other writeable" }' >> $OUTPUT
126 /bin/csh -f -s << end-of-csh > /dev/null 2>&1
127 unset path
128 source $i
129 /bin/ls -ldgT \$path > $TMP1
130end-of-csh
131 awk '{
76c990fe 132 if ($10 ~ /^\.$/) {
e9a107b1
KB
133 print "The root path includes .";
134 next;
135 }
136 }
137 $1 ~ /^d....w/ \
138 { print "Root path directory " $10 " is group writeable." } \
139 $1 ~ /^d.......w/ \
140 { print "Root path directory " $10 " is other writeable." }' \
141 < $TMP1 >> $OUTPUT
07576f30 142 fi
e9a107b1
KB
143done
144if [ $umaskset = "no" -o -s $OUTPUT ] ; then
145 printf "\nChecking root csh paths, umask values:\n$list\n"
146 if [ -s $OUTPUT ]; then
147 cat $OUTPUT
48e0a733 148 fi
e9a107b1
KB
149 if [ $umaskset = "no" ] ; then
150 printf "\nRoot csh startup files do not set the umask.\n"
48e0a733 151 fi
e9a107b1
KB
152fi
153
154> $OUTPUT
155rhome=/root
156umaskset=no
157list="${rhome}/.profile"
158for i in $list; do
159 if [ -f $i ] ; then
160 if egrep umask $i > /dev/null ; then
161 umaskset=yes
162 fi
163 egrep umask $i |
164 awk '$2 % 100 < 20 \
165 { print "Root umask is group writeable" } \
166 $2 % 10 < 2 \
167 { print "Root umask is other writeable" }' >> $OUTPUT
168 /bin/sh << end-of-sh > /dev/null 2>&1
169 PATH=
170 . $i
171 list=\`echo \$PATH | /usr/bin/sed -e 's/:/ /g'\`
172 /bin/ls -ldgT \$list > $TMP1
173end-of-sh
174 awk '{
76c990fe 175 if ($10 ~ /^\.$/) {
e9a107b1
KB
176 print "The root path includes .";
177 next;
178 }
179 }
180 $1 ~ /^d....w/ \
181 { print "Root path directory " $10 " is group writeable." } \
182 $1 ~ /^d.......w/ \
183 { print "Root path directory " $10 " is other writeable." }' \
184 < $TMP1 >> $OUTPUT
48e0a733 185
48e0a733 186 fi
e9a107b1
KB
187done
188if [ $umaskset = "no" -o -s $OUTPUT ] ; then
189 printf "\nChecking root sh paths, umask values:\n$list\n"
190 if [ -s $OUTPUT ]; then
191 cat $OUTPUT
48e0a733 192 fi
e9a107b1
KB
193 if [ $umaskset = "no" ] ; then
194 printf "\nRoot sh startup files do not set the umask.\n"
48e0a733 195 fi
e9a107b1
KB
196fi
197
198# Root and uucp should both be in /etc/ftpusers.
199if egrep root /etc/ftpusers > /dev/null ; then
200 :
201else
202 printf "\nRoot not listed in /etc/ftpusers file.\n"
203fi
204if egrep uucp /etc/ftpusers > /dev/null ; then
205 :
206else
207 printf "\nUucp not listed in /etc/ftpusers file.\n"
208fi
209
28225475 210# Uudecode should not be in the /etc/aliases file.
e9a107b1
KB
211if grep -w uudecode /etc/aliases; then
212 printf "\nThere is an entry for uudecode in the /etc/aliases file.\n"
213fi
214
28225475 215# There should be no plus signs in /etc/hosts.equiv.
e9a107b1
KB
216if egrep '\+|^$' /etc/hosts.equiv > /dev/null ; then
217 printf "\nEmpty line or + in /etc/hosts.equiv file.\n"
218fi
219
220# Check for special users with .rhosts files. Only root and toor should
221# have a .rhosts files. Also, .rhosts files should not have blank lines
222# or plus signs.
223awk -F: '$1 != "root" && $1 != "toor" && \
224 ($3 < 100 || $1 == "ftp" || $1 == "uucp") \
225 { print $1 " " $6 }' /etc/passwd |
226while read uid homedir; do
227 if [ -f ${homedir}/.rhosts ] ; then
228 rhost=`ls -ldgT ${homedir}/.rhosts`
229 printf "$uid: $rhost\n"
48e0a733 230 fi
e9a107b1
KB
231done > $OUTPUT
232if [ -s $OUTPUT ] ; then
233 printf "\nChecking for special users with .rhosts files.\n"
234 cat $OUTPUT
235fi
48e0a733 236
e9a107b1
KB
237awk -F: '{ print $1 " " $6 }' /etc/passwd | \
238while read uid homedir; do
239 if [ -f ${homedir}/.rhosts ] && \
240 egrep '\+|^$' ${homedir}/.rhosts > /dev/null ; then
241 printf "$uid: empty line or + in .rhosts file.\n"
242 fi
243done > $OUTPUT
244if [ -s $OUTPUT ] ; then
245 printf "\nChecking .rhosts files syntax.\n"
246 cat $OUTPUT
247fi
48e0a733 248
28225475
KB
249# Check home directories. Directories should not be owned by someone else
250# or writeable.
e9a107b1
KB
251awk -F: '{ print $1 " " $6 }' /etc/passwd | \
252while read uid homedir; do
253 if [ -d ${homedir}/ ] ; then
254 file=`ls -ldgT ${homedir}`
255 printf "$uid $file\n"
256 fi
257done |
258awk '$1 != $4 && $4 != "root" \
259 { print "user " $1 " home directory is owned by " $4 }
260 $2 ~ /^-....w/ \
261 { print "user " $1 " home directory is group writeable" }
262 $2 ~ /^-.......w/ \
263 { print "user " $1 " home directory is other writeable" }' > $OUTPUT
264if [ -s $OUTPUT ] ; then
265 printf "\nChecking home directories.\n"
266 cat $OUTPUT
267fi
48e0a733 268
e9a107b1
KB
269# Files that should not be owned by someone else or readable.
270list=".netrc .rhosts"
271awk -F: '{ print $1 " " $6 }' /etc/passwd | \
272while read uid homedir; do
273 for f in $list ; do
274 file=${homedir}/${f}
275 if [ -f $file ] ; then
276 printf "$uid $f `ls -ldgT $file`\n"
48e0a733 277 fi
e9a107b1
KB
278 done
279done |
280awk '$1 != $5 && $5 != "root" \
281 { print "user " $1 " " $2 " file is owned by " $5 }
282 $3 ~ /^-...r/ \
283 { print "user " $1 " " $2 " file is group readable" }
284 $3 ~ /^-......r/ \
285 { print "user " $1 " " $2 " file is other readable" }
286 $3 ~ /^-....w/ \
287 { print "user " $1 " " $2 " file is group writeable" }
288 $3 ~ /^-.......w/ \
289 { print "user " $1 " " $2 " file is other writeable" }' > $OUTPUT
48e0a733 290
e9a107b1
KB
291# Files that should not be owned by someone else or writeable.
292list=".bashrc .cshrc .emacsrc .exrc .forward .klogin .login .logout \
293 .profile .tcshrc"
294awk -F: '{ print $1 " " $6 }' /etc/passwd | \
295while read uid homedir; do
296 for f in $list ; do
297 file=${homedir}/${f}
298 if [ -f $file ] ; then
299 printf "$uid $f `ls -ldgT $file`\n"
48e0a733 300 fi
e9a107b1
KB
301 done
302done |
303awk '$1 != $5 && $5 != "root" \
304 { print "user " $1 " " $2 " file is owned by " $5 }
305 $3 ~ /^-....w/ \
306 { print "user " $1 " " $2 " file is group writeable" }
307 $3 ~ /^-.......w/ \
308 { print "user " $1 " " $2 " file is other writeable" }' >> $OUTPUT
309if [ -s $OUTPUT ] ; then
310 printf "\nChecking dot files.\n"
311 cat $OUTPUT
312fi
313
28225475 314# Mailboxes should be owned by user and unreadable.
e9a107b1
KB
315ls -l /var/mail | sed 1d | \
316awk '$3 != $9 \
317 { print "user " $9 " mailbox is owned by " $3 }
318 $1 != "-rw-------" \
319 { print "user " $9 " mailbox is " $1 ", group " $4 }' > $OUTPUT
320if [ -s $OUTPUT ] ; then
321 printf "\nChecking mailbox ownership.\n"
322 cat $OUTPUT
323fi
8e55538e 324
28225475 325# File systems should not be globally exported.
07576f30
KB
326awk '{
327 readonly = 0;
328 for (i = 2; i <= NF; ++i) {
329 if ($i ~ /-ro/)
330 readonly = 1;
331 else if ($i !~ /^-/)
332 next;
333 }
334 if (readonly)
335 print "File system " $1 " globally exported, read-only."
336 else
337 print "File system " $1 " globally exported, read-write."
e9a107b1
KB
338}' < /etc/exports > $OUTPUT
339if [ -s $OUTPUT ] ; then
340 printf "\nChecking for globally exported file systems.\n"
341 cat $OUTPUT
342fi
07576f30 343
28225475 344# Display any changes in setuid files and devices.
e9a107b1
KB
345printf "\nChecking setuid files and devices:\n"
346(find / ! -fstype local -a -prune -o \
347 \( -perm -u+s -o -perm -g+s -o ! -type d -a ! -type f -a ! -type l -a \
348 ! -type s \) | \
349sort | sed -e 's/^/ls -ldgT /' | sh > $LIST) 2> $OUTPUT
383148a3 350
40835542 351# Display any errors that occurred during system file walk.
e9a107b1
KB
352if [ -s $OUTPUT ] ; then
353 printf "Setuid/device find errors:\n"
354 cat $OUTPUT
355 printf "\n"
383148a3
KB
356fi
357
40835542 358# Display any changes in the setuid file list.
e9a107b1 359egrep -v '^[bc]' $LIST > $TMP1
e9a107b1
KB
360if [ -s $TMP1 ] ; then
361 # Check to make sure uudecode isn't setuid.
362 if grep -w uudecode $TMP1 > /dev/null ; then
363 printf "\nUudecode is setuid.\n"
364 fi
365
366 CUR=/var/backups/setuid.current
367 BACK=/var/backups/setuid.backup
383148a3 368
40835542 369 if [ -s $CUR ] ; then
e9a107b1 370 if cmp -s $CUR $TMP1 ; then
8e55538e
KB
371 :
372 else
e9a107b1
KB
373 > $TMP2
374 join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
375 if [ -s $OUTPUT ] ; then
376 printf "Setuid additions:\n"
377 tee -a $TMP2 < $OUTPUT
378 printf "\n"
8e55538e
KB
379 fi
380
e9a107b1
KB
381 join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
382 if [ -s $OUTPUT ] ; then
383 printf "Setuid deletions:\n"
384 tee -a $TMP2 < $OUTPUT
385 printf "\n"
8e55538e
KB
386 fi
387
e9a107b1
KB
388 sort +9 $TMP2 $CUR $TMP1 | \
389 sed -e 's/[ ][ ]*/ /g' | uniq -u > $OUTPUT
390 if [ -s $OUTPUT ] ; then
391 printf "Setuid changes:\n"
392 column -t $OUTPUT
393 printf "\n"
8e55538e
KB
394 fi
395
e9a107b1
KB
396 cp $CUR $BACK
397 cp $TMP1 $CUR
8e55538e
KB
398 fi
399 else
e9a107b1 400 printf "Setuid additions:\n"
d0381ed9 401 column -t $TMP1
e9a107b1
KB
402 printf "\n"
403 cp $TMP1 $CUR
8e55538e 404 fi
525ea79f 405fi
525ea79f 406
28225475 407# Check for block and character disk devices that are readable or writeable
d0381ed9 408# or not owned by root.operator.
e9a107b1
KB
409egrep '^b' $LIST > $TMP1
410egrep '^c.*/rdk[0-9][0-9]*[a-h]$' $LIST >> $TMP1
411egrep '^c.*/rfd[0-9][0-9]*[a-h]$' $LIST >> $TMP1
412egrep '^c.*/rhd[0-9][0-9]*[a-h]$' $LIST >> $TMP1
413egrep '^c.*/rhk[0-9][0-9]*[a-h]$' $LIST >> $TMP1
414egrep '^c.*/rhp[0-9][0-9]*[a-h]$' $LIST >> $TMP1
415egrep '^c.*/rjb[0-9][0-9]*[a-h]$' $LIST >> $TMP1
416egrep '^c.*/rkra[0-9][0-9]*[a-h]$' $LIST >> $TMP1
417egrep '^c.*/rra[0-9][0-9]*[a-h]$' $LIST >> $TMP1
418egrep '^c.*/rrb[0-9][0-9]*[a-h]$' $LIST >> $TMP1
419egrep '^c.*/rrd[0-9][0-9]*[a-h]$' $LIST >> $TMP1
420egrep '^c.*/rrl[0-9][0-9]*[a-h]$' $LIST >> $TMP1
421egrep '^c.*/rrx[0-9][0-9]*[a-h]$' $LIST >> $TMP1
422egrep '^c.*/rrz[0-9][0-9]*[a-h]$' $LIST >> $TMP1
423egrep '^c.*/rsd[0-9][0-9]*[a-h]$' $LIST >> $TMP1
424egrep '^c.*/rup[0-9][0-9]*[a-h]$' $LIST >> $TMP1
425egrep '^c.*/rwd[0-9][0-9]*[a-h]$' $LIST >> $TMP1
426
d0381ed9
KB
427awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
428 { printf("Disk %s is user %s, group %s, permissions %s.\n", \
429 $11, $3, $4, $1); }' < $TMP1 > $OUTPUT
e9a107b1
KB
430if [ -s $OUTPUT ] ; then
431 printf "\nChecking disk ownership and permissions.\n"
432 cat $OUTPUT
433 printf "\n"
434fi
435
40835542 436# Display any changes in the device file list.
28225475 437egrep '^[bc]' $LIST | sort +10 > $TMP1
e9a107b1
KB
438if [ -s $TMP1 ] ; then
439 CUR=/var/backups/device.current
440 BACK=/var/backups/device.backup
40835542
KB
441
442 if [ -s $CUR ] ; then
e9a107b1 443 if cmp -s $CUR $TMP1 ; then
40835542
KB
444 :
445 else
e9a107b1
KB
446 > $TMP2
447 join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
448 if [ -s $OUTPUT ] ; then
449 printf "Device additions:\n"
450 tee -a $TMP2 < $OUTPUT
451 printf "\n"
40835542
KB
452 fi
453
e9a107b1
KB
454 join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
455 if [ -s $OUTPUT ] ; then
456 printf "Device deletions:\n"
457 tee -a $TMP2 < $OUTPUT
458 printf "\n"
40835542
KB
459 fi
460
d0381ed9
KB
461 # Report any block device change. Ignore character
462 # devices, only the name is significant.
463 cat $TMP2 $CUR $TMP1 | \
464 sed -e '/^c/d' | \
465 sort +10 | \
e9a107b1 466 sed -e 's/[ ][ ]*/ /g' | \
d0381ed9 467 uniq -u > $OUTPUT
e9a107b1 468 if [ -s $OUTPUT ] ; then
d0381ed9 469 printf "Block device changes:\n"
e9a107b1
KB
470 column -t $OUTPUT
471 printf "\n"
40835542
KB
472 fi
473
e9a107b1
KB
474 cp $CUR $BACK
475 cp $TMP1 $CUR
40835542
KB
476 fi
477 else
e9a107b1 478 printf "Device additions:\n"
d0381ed9 479 column -t $TMP1
e9a107b1
KB
480 printf "\n"
481 cp $TMP1 $CUR
40835542
KB
482 fi
483fi
40835542 484
08a2d709
KB
485# Check special files.
486# Check system binaries.
487#
fd70cf43
KB
488# Create the mtree tree specifications using:
489#
e9a107b1
KB
490# mtree -cx -pDIR -kcksum,gid,mode,nlink,size,link,time,uid > DIR.secure
491# chown root.wheel DIR.SECURE
492# chmod 600 DIR.SECURE
fd70cf43
KB
493#
494# Note, this is not complete protection against Trojan horsed binaries, as
495# the hacker can modify the tree specification to match the replaced binary.
496# For details on really protecting yourself against modified binaries, see
497# the mtree(8) manual page.
fd70cf43 498if cd /etc/mtree; then
e9a107b1
KB
499 mtree -e -p / -f /etc/mtree/special > $OUTPUT
500 if [ -s $OUTPUT ] ; then
501 printf "\nChecking special files and directories.\n"
502 cat $OUTPUT
503 fi
504
505 > $OUTPUT
fd70cf43
KB
506 for file in *.secure; do
507 tree=`sed -n -e '3s/.* //p' -e 3q $file`
e9a107b1
KB
508 mtree -f $file -p $tree > $TMP1
509 if [ -s $TMP1 ]; then
510 printf "\nChecking $tree:\n" >> $OUTPUT
511 cat $TMP1 >> $OUTPUT
512 fi
fd70cf43 513 done
e9a107b1
KB
514 if [ -s $OUTPUT ] ; then
515 printf "\nChecking system binaries:\n"
516 cat $OUTPUT
517 fi
fd70cf43 518fi
8c93a907
KB
519
520# List of files that get backed up and checked for any modifications. Each
521# file is expected to have two backups, /var/backups/file.{current,backup}.
522# Any changes cause the files to rotate.
523if [ -s /etc/changelist ] ; then
524 for file in `cat /etc/changelist`; do
525 CUR=/var/backups/`basename $file`.current
526 BACK=/var/backups/`basename $file`.backup
527 if [ -s $file ]; then
528 if [ -s $CUR ] ; then
529 diff $CUR $file > $OUTPUT
530 if [ -s $OUTPUT ] ; then
531 printf "\n======\n%s diffs (OLD < > NEW)\n======\n" $file
532 cat $OUTPUT
533 cp -p $CUR $BACK
534 cp -p $file $CUR
535 chown root.wheel $CUR $BACK
536 fi
537 else
538 cp -p $file $CUR
539 chown root.wheel $CUR
540 fi
541 fi
542 done
543fi