summaryrefslogtreecommitdiff
path: root/etc/security
blob: a849a0451187a88921b449e153b960efb1361281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
#!/bin/sh -
#
#	$NetBSD: security,v 1.45 2000/07/02 22:27:47 sommerfeld Exp $
#	from: @(#)security	8.1 (Berkeley) 6/9/93
#

PATH=/sbin:/usr/sbin:/bin:/usr/bin

if [ -f /etc/rc.subr ]; then
	. /etc/rc.subr
else
	echo "Can't read /etc/rc.subr; aborting."
	exit 1;
fi

umask 077

if [ -s /etc/security.conf ]; then
	. /etc/security.conf
fi

SECUREDIR=/tmp/_securedir.$$
if ! mkdir $SECUREDIR; then
	echo can not create $SECUREDIR.
	exit 1
fi

if ! cd $SECUREDIR; then
	echo can not chdir to $SECUREDIR.
	exit 1
fi

if [ -z "$max_loginlen" ];then
    max_loginlen=8
fi

ERR=secure1.$$
TMP1=secure2.$$
TMP2=secure3.$$
MPBYUID=secure4.$$
MPBYPATH=secure5.$$
LIST=secure6.$$
OUTPUT=secure7.$$
LABELS=secure8.$$

trap '/bin/rm -rf $SECUREDIR ; exit 0' 0 2 3

MP=/etc/master.passwd

# these is used several times.
awk -F: '!/^+/ { print $1 " " $3 }' $MP | sort -k2n > $MPBYUID
awk -F: '{ print $1 " " $9 }' $MP | sort -k2 > $MPBYPATH

# Check the master password file syntax.
#
if checkyesno check_passwd; then
	awk -v "len=$max_loginlen" '
	BEGIN {
		while ( getline < "/etc/shells" > 0 ) {
			if ($0 ~ /^\#/ || $0 ~ /^$/ )
				continue;
			shells[$1]++;
		}
		FS=":";
	}

	{
		if ($0 ~ /^[	 ]*$/) {
			printf "Line %d is a blank line.\n", NR;
			next;
		}
		if (NF != 10 && ($1 != "+" || NF != 1))
			printf "Line %d has the wrong number of fields.\n", NR;
		if ($1 == "+" )  {
			if (NF != 1 && $3 == 0)
			    printf "Line %d includes entries with uid 0.\n", NR;
			next;
		}
		if ($1 !~ /^[A-Za-z0-9]*$/)
			printf "Login %s has non-alphanumeric characters.\n",
			    $1;
		if (length($1) > len)
			printf "Login %s has more than "len" characters.\n", $1;
		if ($2 == "")
			printf "Login %s has no password.\n", $1;
		if (length($2) != 13 && length($2) != 20 && $2 != "") {
			if ($10 == "" || shells[$10])
		    printf "Login %s is off but still has a valid shell (%s)\n",
				    $1, $10;
		} else if (! shells[$10])
			printf "Login %s does not have a valid shell (%s)\n",
			    $1, $10;
		if ($3 == 0 && $1 != "root" && $1 != "toor")
			printf "Login %s has a user id of 0.\n", $1;
		if ($3 < 0)
			printf "Login %s has a negative user id.\n", $1;
		if ($4 < 0)
			printf "Login %s has a negative group id.\n", $1;
	}' < $MP > $OUTPUT
	if [ -s $OUTPUT ] ; then
		printf "\nChecking the $MP file:\n"
		cat $OUTPUT
	fi

	awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
	if [ -s $OUTPUT ] ; then
		printf "\n$MP has duplicate user names.\n"
		column $OUTPUT
	fi

# To not exclude 'toor', a standard duplicate root account, from the duplicate
# account test, uncomment the line below (without egrep in it)and comment
# out the line (with egrep in it) below it.
#
#	< $MPBYUID uniq -d -f 1 | awk '{ print $2 }' > $TMP2
	< $MPBYUID egrep -v '^toor ' | uniq -d -f 1 | awk '{ print $2 }' > $TMP2
	if [ -s $TMP2 ] ; then
		printf "\n$MP has duplicate user id's.\n"
		while read uid; do
			grep -w $uid $MPBYUID
		done < $TMP2 | column
	fi
fi

# Backup the master password file; a special case, the normal backup
# mechanisms also print out file differences and we don't want to do
# that because this file has encrypted passwords in it.
#
CUR=/var/backups/`basename $MP`.current
BACK=/var/backups/`basename $MP`.backup
if [ -s $CUR ] ; then
	if cmp -s $CUR $MP; then
		:
	else
		cp -p $CUR $BACK
		cp -p $MP $CUR
		chown root:wheel $CUR
	fi
else
	cp -p $MP $CUR
	chown root:wheel $CUR
fi

# Check the group file syntax.
#
if checkyesno check_group; then
	GRP=/etc/group
	awk -F: '{
		if ($0 ~ /^[	 ]*$/) {
			printf "Line %d is a blank line.\n", NR;
			next;
		}
		if (NF != 4 && ($1 != "+" || NF != 1))
			printf "Line %d has the wrong number of fields.\n", NR;
		if ($1 == "+" )  {
			next;
		}
		if ($1 !~ /^[A-za-z0-9]*$/)
			printf "Group %s has non-alphanumeric characters.\n",
			    $1;
		if (length($1) > 8)
			printf "Group %s has more than 8 characters.\n", $1;
		if ($3 !~ /[0-9]*/)
			printf "Login %s has a negative group id.\n", $1;
	}' < $GRP > $OUTPUT
	if [ -s $OUTPUT ] ; then
		printf "\nChecking the $GRP file:\n"
		cat $OUTPUT
	fi

	awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
	if [ -s $OUTPUT ] ; then
		printf "\n$GRP has duplicate group names.\n"
		column $OUTPUT
	fi
fi

# Check for root paths, umask values in startup files.
# The check for the root paths is problematical -- it's likely to fail
# in other environments.  Once the shells have been modified to warn
# of '.' in the path, the path tests should go away.
#
if checkyesno check_rootdotfiles; then
	> $OUTPUT
	rhome=`csh -fc "echo ~root"`
	umaskset=no
	list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
	for i in $list ; do
		if [ -f $i ] ; then
			if egrep '^[ \t]*umask[ \t]+[0-7]+' $i > /dev/null ; then
				umaskset=yes
			fi
			# double check the umask value itself; ensure that both the
			# 020 and 002 bits are set.
			# we handle this in decimal initially to extract the digits,
			# and then extract the `2' bit of each digit.
			# this is made especially painful because 
			# bitwise operations were left out of awk.
			egrep '^[ \t]*umask[ \t]+[0-7]+' $i |
			awk '{ g= ($2 % 100) - ($2 % 10);
			       g /= 10;
			       g = g % 4;
			       g -= g % 2;
			       if (g != 2) { print "\tRoot umask is group writeable" }
			       o = ($2 % 10);
			       o = o % 4;
			       o -= o % 2;
			       if (o != 2) { print "\tRoot umask is other writeable" } }' |
			    sort -u >> $OUTPUT
			SAVE_PATH=$PATH
			unset PATH
			/bin/csh -f -s << end-of-csh > /dev/null 2>&1
				source $i
				/bin/ls -ldgT \$path > $TMP1
end-of-csh
			PATH=$SAVE_PATH
			awk '{
				if ($10 ~ /^\.$/) {
					print "\tThe root path includes .";
					next;
				}
			     }
			     $1 ~ /^d....w/ \
		{ print "\tRoot path directory " $10 " is group writeable." } \
			     $1 ~ /^d.......w/ \
		{ print "\tRoot path directory " $10 " is other writeable." }' \
			< $TMP1 >> $OUTPUT
		fi
	done
	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
		printf "\nChecking root csh paths, umask values:\n$list\n\n"
		if [ -s $OUTPUT ]; then
			cat $OUTPUT
		fi
		if [ $umaskset = "no" ] ; then
		    printf "\tRoot csh startup files do not set the umask.\n"
		fi
	fi

	> $OUTPUT
	rhome=/root
	umaskset=no
	list="/etc/profile ${rhome}/.profile"
	for i in $list; do
		if [ -f $i ] ; then
			if egrep umask $i > /dev/null ; then
				umaskset=yes
			fi
			egrep umask $i |
			awk '$2 % 100 < 20 \
				{ print "\tRoot umask is group writeable" } \
			     $2 % 10 < 2 \
				{ print "\tRoot umask is other writeable" }' \
			    >> $OUTPUT
			SAVE_PATH=$PATH
			unset PATH
			/bin/sh << end-of-sh > /dev/null 2>&1
				. $i
				list=\`echo \$PATH | /usr/bin/sed -e \
				    's/^:/.:/;s/:$/:./;s/::/:.:/g;s/:/ /g'\`
				/bin/ls -ldgT \$list > $TMP1
end-of-sh
			PATH=$SAVE_PATH
			awk '{
				if ($10 ~ /^\.$/) {
					print "\tThe root path includes .";
					next;
				}
			     }
			     $1 ~ /^d....w/ \
		{ print "\tRoot path directory " $10 " is group writeable." } \
			     $1 ~ /^d.......w/ \
		{ print "\tRoot path directory " $10 " is other writeable." }' \
			< $TMP1 >> $OUTPUT

		fi
	done
	if [ $umaskset = "no" -o -s $OUTPUT ] ; then
		printf "\nChecking root sh paths, umask values:\n$list\n"
		if [ -s $OUTPUT ]; then
			cat $OUTPUT
		fi
		if [ $umaskset = "no" ] ; then
			printf "\tRoot sh startup files do not set the umask.\n"
		fi
	fi
fi

# Root and uucp should both be in /etc/ftpusers.
#
if checkyesno check_ftpusers; then
	> $OUTPUT
	list="uucp "`awk '$2 == 0 { print $1 }' $MPBYUID`
	for i in $list; do
		if /usr/libexec/ftpd -C $i ; then
			printf "\t$i is not denied\n" >> $OUTPUT
		fi
	done
	if [ -s $OUTPUT ]; then
		printf "\nChecking the /etc/ftpusers configuration:\n"
		cat $OUTPUT
	fi
fi

# Uudecode should not be in the /etc/mail/aliases file.
#
if checkyesno check_aliases; then
	for f in /etc/mail/aliases /etc/aliases; do
		if [ -f $f ] && egrep '^[^#]*(uudecode|decode).*\|' $f; then
			printf "\nEntry for uudecode in $f file.\n"
		fi
	done
fi

# Files that should not have + signs.
#
if checkyesno check_rhosts; then
	list="/etc/hosts.equiv /etc/hosts.lpd"
	for f in $list ; do
		if [ -f $f ] && egrep '\+' $f > /dev/null ; then
			printf "\nPlus sign in $f file.\n"
		fi
	done

	# Check for special users with .rhosts files.  Only root and toor should
	# have .rhosts files.  Also, .rhosts files should not have plus signs.
	awk -F: '$1 != "root" && $1 != "toor" && \
		($3 < 100 || $1 == "ftp" || $1 == "uucp") \
			{ print $1 " " $9 }' $MP |
	sort -k2 |
	while read uid homedir; do
		if [ -f ${homedir}/.rhosts ] ; then
			rhost=`ls -ldgT ${homedir}/.rhosts`
			printf "$uid: $rhost\n"
		fi
	done > $OUTPUT
	if [ -s $OUTPUT ] ; then
		printf "\nChecking for special users with .rhosts files.\n"
		cat $OUTPUT
	fi

	while read uid homedir; do
		if [ -f ${homedir}/.rhosts -a -r ${homedir}/.rhosts ] && \
		    cat -f ${homedir}/.rhosts | egrep '\+' > /dev/null ; then
			printf "$uid: + in .rhosts file.\n"
		fi
	done < $MPBYPATH > $OUTPUT
	if [ -s $OUTPUT ] ; then
		printf "\nChecking .rhosts files syntax.\n"
		cat $OUTPUT
	fi
fi

# Check home directories.  Directories should not be owned by someone else
# or writeable.
#
if checkyesno check_homes; then
	while read uid homedir; do
		if [ -d ${homedir}/ ] ; then
			file=`ls -ldgT ${homedir}`
			printf "$uid $file\n"
		fi
	done < $MPBYPATH |
	awk '$1 != $4 && $4 != "root" \
		{ print "user " $1 " home directory is owned by " $4 }
	     $2 ~ /^-....w/ \
		{ print "user " $1 " home directory is group writeable" }
	     $2 ~ /^-.......w/ \
		{ print "user " $1 " home directory is other writeable" }' \
	    > $OUTPUT
	if [ -s $OUTPUT ] ; then
		printf "\nChecking home directories.\n"
		cat $OUTPUT
	fi

	# Files that should not be owned by someone else or readable.
	list=".Xauthority .netrc"
	while read uid homedir; do
		for f in $list ; do
			file=${homedir}/${f}
			if [ -f $file ] ; then
				printf "$uid $f `ls -ldgT $file`\n"
			fi
		done
	done < $MPBYPATH |
	awk '$1 != $5 && $5 != "root" \
		{ print "user " $1 " " $2 " file is owned by " $5 }
	     $3 ~ /^-...r/ \
		{ print "user " $1 " " $2 " file is group readable" }
	     $3 ~ /^-......r/ \
		{ print "user " $1 " " $2 " file is other readable" }
	     $3 ~ /^-....w/ \
		{ print "user " $1 " " $2 " file is group writeable" }
	     $3 ~ /^-.......w/ \
		{ print "user " $1 " " $2 " file is other writeable" }' \
	    > $OUTPUT

	# Files that should not be owned by someone else or writeable.
	list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
	      .cshrc .emacs .exrc .forward .history .klogin .login .logout \
	      .profile .qmail .rc_history .rhosts .tcshrc .twmrc .xinitrc \
	      .xsession"
	while read uid homedir; do
		for f in $list ; do
			file=${homedir}/${f}
			if [ -f $file ] ; then
				printf "$uid $f `ls -ldgT $file`\n"
			fi
		done
	done < $MPBYPATH |
	awk '$1 != $5 && $5 != "root" \
		{ print "user " $1 " " $2 " file is owned by " $5 }
	     $3 ~ /^-....w/ \
		{ print "user " $1 " " $2 " file is group writeable" }
	     $3 ~ /^-.......w/ \
		{ print "user " $1 " " $2 " file is other writeable" }' \
	    >> $OUTPUT
	if [ -s $OUTPUT ] ; then
		printf "\nChecking dot files.\n"
		cat $OUTPUT
	fi
fi

# Mailboxes should be owned by user and unreadable.
#
if checkyesno check_varmail; then
	ls -l /var/mail | sed 1d | \
	awk '$3 != $9 \
		{ print "user " $9 " mailbox is owned by " $3 }
	     $1 != "-rw-------" \
		{ print "user " $9 " mailbox is " $1 ", group " $4 }' > $OUTPUT
	if [ -s $OUTPUT ] ; then
		printf "\nChecking mailbox ownership.\n"
		cat $OUTPUT
	fi
fi

# NFS exports shouldn't be globally exported
#
if checkyesno check_nfs && [ -f /etc/exports ]; then
	awk '{
		# ignore comments and blank lines
		if ($0 ~ /^\#/ || $0 ~ /^$/ )
			next;

		readonly = 0;
		for (i = 2; i <= NF; ++i) {
			if ($i ~ /-ro/)
				readonly = 1;
			else if ($i !~ /^-/)
				next;
		}
		if (readonly)
			print "File system " $1 " globally exported, read-only."
		else
			print "File system " $1 " globally exported, read-write."
	}' < /etc/exports > $OUTPUT
	if [ -s $OUTPUT ] ; then
		printf "\nChecking for globally exported file systems.\n"
		cat $OUTPUT
	fi
fi

# Display any changes in setuid files and devices.
#
if checkyesno check_devices; then
	> $ERR
	(find / \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \
			-o -fstype procfs \) -a -prune -o \
	    \( \( -perm -u+s -a ! -type d \) -o \
	       \( -perm -g+s -a ! -type d \) -o \
	       -type b -o -type c \) -print0 | \
	xargs -0 ls -ldgTq | sort +9 > $LIST) 2> $OUTPUT

	# Display any errors that occurred during system file walk.
	if [ -s $OUTPUT ] ; then
		printf "Setuid/device find errors:\n" >> $ERR
		cat $OUTPUT >> $ERR
		printf "\n" >> $ERR
	fi

	# Display any changes in the setuid file list.
	egrep -v '^[bc]' $LIST > $TMP1
	if [ -s $TMP1 ] ; then
		# Check to make sure uudecode isn't setuid.
		if grep -w uudecode $TMP1 > /dev/null ; then
			printf "\nUudecode is setuid.\n" >> $ERR
		fi

		CUR=/var/backups/setuid.current
		BACK=/var/backups/setuid.backup

		if [ -s $CUR ] ; then
			if cmp -s $CUR $TMP1 ; then
				:
			else
				> $TMP2
				join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
				if [ -s $OUTPUT ] ; then
					printf "Setuid additions:\n" >> $ERR
					tee -a $TMP2 < $OUTPUT >> $ERR
					printf "\n" >> $ERR
				fi

				join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
				if [ -s $OUTPUT ] ; then
					printf "Setuid deletions:\n" >> $ERR
					tee -a $TMP2 < $OUTPUT >> $ERR
					printf "\n" >> $ERR
				fi

				sort -k10 $TMP2 $CUR $TMP1 | \
				    sed -e 's/[	 ][	 ]*/ /g' | \
				    uniq -u > $OUTPUT
				if [ -s $OUTPUT ] ; then
					printf "Setuid changes:\n" >> $ERR
					column -t $OUTPUT >> $ERR
					printf "\n" >> $ERR
				fi

				cp $CUR $BACK
				cp $TMP1 $CUR
			fi
		else
			printf "Setuid additions:\n" >> $ERR
			column -t $TMP1 >> $ERR
			printf "\n" >> $ERR
			cp $TMP1 $CUR
		fi
	fi

	# Check for block and character disk devices that are readable or
	# writeable or not owned by root.operator.
	>$TMP1
	DISKLIST="acd ccd cd ch fd hk hp mcd md ra rb rd rl rx rz \
	    sd se ss tz uk up vnd wd xd xy"
#	DISKLIST="$DISKLIST ct mt st wt"
	for i in $DISKLIST; do
		egrep "^b.*/${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
		egrep "^c.*/r${i}[0-9][0-9]*[a-p]$"  $LIST >> $TMP1
	done

	awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
		{ printf "Disk %s is user %s, group %s, permissions %s.\n", \
		    $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
	if [ -s $OUTPUT ] ; then
		printf "\nChecking disk ownership and permissions.\n" >> $ERR
		cat $OUTPUT >> $ERR
		printf "\n" >> $ERR
	fi

	# Display any changes in the device file list.
	egrep '^[bc]' $LIST | sort -k11 > $TMP1
	if [ -s $TMP1 ] ; then
		CUR=/var/backups/device.current
		BACK=/var/backups/device.backup

		if [ -s $CUR ] ; then
			if cmp -s $CUR $TMP1 ; then
				:
			else
				> $TMP2
				join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
				if [ -s $OUTPUT ] ; then
					printf "Device additions:\n" >> $ERR
					tee -a $TMP2 < $OUTPUT >> $ERR
					printf "\n" >> $ERR
				fi

				join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
				if [ -s $OUTPUT ] ; then
					printf "Device deletions:\n" >> $ERR
					tee -a $TMP2 < $OUTPUT >> $ERR
					printf "\n" >> $ERR
				fi

				# Report any block device change. Ignore
				# character devices, only the name is
				# significant.
				cat $TMP2 $CUR $TMP1 | \
				    sed -e '/^c/d' | \
				    sort -k11 | \
				    sed -e 's/[	 ][	 ]*/ /g' | \
				    uniq -u > $OUTPUT
				if [ -s $OUTPUT ] ; then
					printf "Block device changes:\n" >> $ERR
					column -t $OUTPUT >> $ERR
					printf "\n" >> $ERR
				fi

				cp $CUR $BACK
				cp $TMP1 $CUR
			fi
		else
			printf "Device additions:\n" >> $ERR
			column -t $TMP1 >> $ERR
			printf "\n" >> $ERR
			cp $TMP1 $CUR >> $ERR
		fi
	fi
	if [ -s $ERR ] ; then
		printf "\nChecking setuid files and devices:\n"
		cat $ERR
		printf "\n"
	fi
fi

# Check special files.
# Check system binaries.
#
# Create the mtree tree specifications using:
#
#	mtree -cx -pDIR -kcksum,gid,mode,nlink,size,link,time,uid > DIR.secure
#	chown root:wheel DIR.secure
#	chmod 600 DIR.secure
#
# Note, this is not complete protection against Trojan horsed binaries, as
# the hacker can modify the tree specification to match the replaced binary.
# For details on really protecting yourself against modified binaries, see
# the mtree(8) manual page.
#
if checkyesno check_mtree; then
	mtree -e -p / -f /etc/mtree/special > $OUTPUT
	if [ -s $OUTPUT ]; then
		printf "\nChecking special files and directories.\n"
		cat $OUTPUT
	fi

	> $OUTPUT
	for file in /etc/mtree/*.secure; do
		[ $file = '/etc/mtree/*.secure' ] && continue
		tree=`sed -n -e '3s/.* //p' -e 3q $file`
		mtree -f $file -p $tree > $TMP1
		if [ -s $TMP1 ]; then
			printf "\nChecking $tree:\n" >> $OUTPUT
			cat $TMP1 >> $OUTPUT
		fi
	done
	if [ -s $OUTPUT ]; then
		printf "\nChecking system binaries:\n"
		cat $OUTPUT
	fi
fi

CHANGELIST=""

# Backup disklabels of available disks
#
if checkyesno check_disklabels; then
		# generate list of old disklabels and remove them
	ls -1d /var/backups/disklabel.* 2>/dev/null |
	    egrep -v '\.(backup|current)$' > $LABELS
	xargs rm < $LABELS

	disks=`iostat -x | sed 1d | awk '$1 !~ /^[cfm]d/ { print $1; }'`
	for i in $disks; do
		dlf="/var/backups/disklabel.$i"
		disklabel $i > $dlf 2>/dev/null
	done

		# append list of new disklabels, sort list
	ls -1d /var/backups/disklabel.* 2>/dev/null |
	    egrep -v '\.(backup|current)$' >> $LABELS
	sort -u -o $LABELS $LABELS
	CHANGELIST=$LABELS
fi

# List of files that get backed up and checked for any modifications.  Each
# file is expected to have two backups, /var/backups/file.{current,backup}.
# Any changes cause the files to rotate.
#
if checkyesno check_changelist && [ -s /etc/changelist ] ; then
	CHANGELIST="/etc/changelist $CHANGELIST"
fi

if [ -n "$CHANGELIST" ]; then
	for file in `egrep -hv "^#|$MP" $CHANGELIST`; do
		CUR=/var/backups/`basename $file`.current
		BACK=/var/backups/`basename $file`.backup
		if [ -f $file ]; then
			if [ -f $CUR ] ; then
				diff $CUR $file > $OUTPUT
				if [ -s $OUTPUT ] ; then
		printf "\n======\n%s diffs (OLD < > NEW)\n======\n" $file
					cat $OUTPUT
					mv -f $CUR $BACK
					cp -p $file $CUR
					chown root:wheel $CUR
				fi
			else
		printf "\n======\n%s added\n======\n" $file
				diff /dev/null $file
				cp -p $file $CUR
				chown root:wheel $CUR
			fi
		else
			if [ -f $CUR ]; then
		printf "\n======\n%s removed\n======\n" $file
				diff $CUR /dev/null
				mv -f $CUR $BACK
			fi
		fi
	done
fi

# run skeyaudit to inform users of ready to expire S/Keys
#
if checkyesno run_skeyaudit; then
	skeyaudit
fi

if [ -f /etc/security.local ]; then
	echo ""
	echo "Running /etc/security.local:"
	. /etc/security.local
fi