security revision 1.98 1 #!/bin/sh -
2 #
3 # $NetBSD: security,v 1.98 2006/05/25 02:38:10 lukem Exp $
4 # from: @(#)security 8.1 (Berkeley) 6/9/93
5 #
6
7 PATH=/sbin:/usr/sbin:/bin:/usr/bin
8
9 rcvar_manpage='security.conf(5)'
10
11 if [ -f /etc/rc.subr ]; then
12 . /etc/rc.subr
13 else
14 echo "Can't read /etc/rc.subr; aborting."
15 exit 1;
16 fi
17
18 umask 077
19 TZ=UTC; export TZ
20
21 if [ -s /etc/security.conf ]; then
22 . /etc/security.conf
23 fi
24
25 # Set reasonable defaults (if they're not set in security.conf)
26 #
27 backup_dir=${backup_dir:-/var/backups}
28 pkgdb_dir=${pkgdb_dir:-/var/db/pkg}
29 max_loginlen=${max_loginlen:-8}
30 max_grouplen=${max_grouplen:-8}
31
32 # Other configurable variables
33 #
34 special_files="/etc/mtree/special /etc/mtree/special.local"
35 MP=/etc/master.passwd
36 CHANGELIST=""
37 work_dir=$backup_dir/work
38
39 if [ ! -d "$work_dir" ]; then
40 mkdir -p "$work_dir"
41 fi
42
43 SECUREDIR=`mktemp -d /tmp/_securedir.XXXXXX` || exit 1
44
45 trap "/bin/rm -rf $SECUREDIR ; exit 0" EXIT INT QUIT PIPE
46
47 if ! cd "$SECUREDIR"; then
48 echo "Can not cd to $SECUREDIR".
49 exit 1
50 fi
51
52 ERR=err.$$
53 TMP1=tmp1.$$
54 TMP2=tmp2.$$
55 MPBYUID=mpbyuid.$$
56 MPBYPATH=mpbypath.$$
57 LIST=list.$$
58 OUTPUT=output.$$
59 LABELS=labels.$$
60 PKGS=pkgs.$$
61 CHANGEFILES=changefiles.$$
62 SPECIALSPEC=specialspec.$$
63
64
65 # migrate_file old new
66 # Determine if the "${old}" path name needs to be migrated to the
67 # "${new}" path. Also checks if "${old}.current" needs migrating,
68 # and if so, migrate it and possibly "${old}.current,v" and
69 # "${old}.backup".
70 #
71 migrate_file()
72 {
73 _old=$1
74 _new=$2
75 if [ -z "$_old" -o -z "$_new" ]; then
76 err 3 "USAGE: migrate_file old new"
77 fi
78 if [ ! -d "${_new%/*}" ]; then
79 mkdir -p "${_new%/*}"
80 fi
81 if [ -f "${_old}" -a ! -f "${_new}" ]; then
82 echo "==> migrating ${_old}"
83 echo " to ${_new}"
84 mv "${_old}" "${_new}"
85 fi
86 if [ -f "${_old}.current" -a ! -f "${_new}.current" ]; then
87 echo "==> migrating ${_old}.current"
88 echo " to ${_new}.current"
89 mv "${_old}.current" "${_new}.current"
90 if [ -f "${_old}.current,v" -a ! -f "${_new}.current,v" ]; then
91 echo "==> migrating ${_old}.current,v"
92 echo " to ${_new}.current,v"
93 mv "${_old}.current,v" "${_new}.current,v"
94 fi
95 if [ -f "${_old}.backup" -a ! -f "${_new}.backup" ]; then
96 echo "==> migrating ${_old}.backup"
97 echo " to ${_new}.backup"
98 mv "${_old}.backup" "${_new}.backup"
99 fi
100 fi
101 }
102
103
104 # backup_and_diff file printdiff
105 # Determine if file needs backing up, and if so, do it.
106 # If printdiff is yes, display the diffs, otherwise
107 # just print a message saying "[changes omitted]".
108 #
109 backup_and_diff()
110 {
111 _file=$1
112 _printdiff=$2
113 if [ -z "$_file" -o -z "$_printdiff" ]; then
114 err 3 "USAGE: backup_and_diff file printdiff"
115 fi
116 ! checkyesno _printdiff
117 _printdiff=$?
118
119 _old=$backup_dir/${_file##*/}
120 case "$_file" in
121 $work_dir/*)
122 _new=$_file
123 migrate_file "$backup_dir/$_old" "$_new"
124 migrate_file "$_old" "$_new"
125 ;;
126 *)
127 _new=$backup_dir/$_file
128 migrate_file "$_old" "$_new"
129 ;;
130 esac
131 CUR=${_new}.current
132 BACK=${_new}.backup
133 if [ -f $_file ]; then
134 if [ -f $CUR ] ; then
135 if [ "$_printdiff" -ne 0 ]; then
136 diff ${diff_options} $CUR $_file > $OUTPUT
137 else
138 if ! cmp -s $CUR $_file; then
139 echo "[changes omitted]"
140 fi > $OUTPUT
141 fi
142 if [ -s $OUTPUT ] ; then
143 printf \
144 "\n======\n%s diffs (OLD < > NEW)\n======\n" $_file
145 cat $OUTPUT
146 backup_file update $_file $CUR $BACK
147 fi
148 else
149 printf "\n======\n%s added\n======\n" $_file
150 if [ "$_printdiff" -ne 0 ]; then
151 diff ${diff_options} /dev/null $_file
152 else
153 echo "[changes omitted]"
154 fi
155 backup_file add $_file $CUR $BACK
156 fi
157 else
158 if [ -f $CUR ]; then
159 printf "\n======\n%s removed\n======\n" $_file
160 if [ "$_printdiff" -ne 0 ]; then
161 diff ${diff_options} $CUR /dev/null
162 else
163 echo "[changes omitted]"
164 fi
165 backup_file remove $_file $CUR $BACK
166 fi
167 fi
168 }
169
170
171 # These are used several times.
172 #
173 awk -F: '!/^\+/ { print $1 " " $3 }' $MP | sort -k2n > $MPBYUID
174 awk -F: '{ print $1 " " $9 }' $MP | sort -k2 > $MPBYPATH
175 for file in $special_files; do
176 [ -s $file ] && cat $file
177 done | mtree -CM -k all > $SPECIALSPEC || exit 1
178
179
180 # Check the master password file syntax.
181 #
182 if checkyesno check_passwd; then
183 # XXX: the sense of permit_star is reversed; the code works as
184 # implemented, but usage needs to be negated.
185 checkyesno check_passwd_permit_star && permit_star=0 || permit_star=1
186 checkyesno check_passwd_permit_nonalpha \
187 && permit_nonalpha=1 || permit_nonalpha=0
188
189 awk -v "len=$max_loginlen" \
190 -v "nowarn_shells_list=$check_passwd_nowarn_shells" \
191 -v "nowarn_users_list=$check_passwd_nowarn_users" \
192 -v "permit_star=$permit_star" \
193 -v "permit_nonalpha=$permit_nonalpha" \
194 '
195 BEGIN {
196 while ( getline < "/etc/shells" > 0 ) {
197 if ($0 ~ /^\#/ || $0 ~ /^$/ )
198 continue;
199 shells[$1]++;
200 }
201 split(nowarn_shells_list, a);
202 for (i in a) nowarn_shells[a[i]]++;
203 split(nowarn_users_list, a);
204 for (i in a) nowarn_users[a[i]]++;
205 uid0_users_list="root toor"
206 split(uid0_users_list, a);
207 for (i in a) uid0_users[a[i]]++;
208 FS=":";
209 }
210
211 {
212 if ($0 ~ /^[ ]*$/) {
213 printf "Line %d is a blank line.\n", NR;
214 next;
215 }
216 if (NF != 10 && ($1 != "+" || NF != 1))
217 printf "Line %d has the wrong number of fields.\n", NR;
218 if ($1 == "+" ) {
219 if (NF != 1 && $3 == 0)
220 printf "Line %d includes entries with uid 0.\n",
221 NR;
222 next;
223 }
224 if (!permit_nonalpha &&
225 $1 !~ /^[_A-Za-z0-9]([-A-Za-z0-9_.]*[A-Za-z0-9])*$/)
226 printf "Login %s has non-alphanumeric characters.\n",
227 $1;
228 if (length($1) > len)
229 printf "Login %s has more than "len" characters.\n",
230 $1;
231 if ($2 == "" && !nowarn_users[$1])
232 printf "Login %s has no password.\n", $1;
233 if (!nowarn_shells[$10] && !nowarn_users[$1]) {
234 if (length($2) != 13 &&
235 length($2) != 20 &&
236 $2 !~ /^\$1/ &&
237 $2 !~ /^\$2/ &&
238 $2 != "" &&
239 (permit_star || $2 != "*") &&
240 $2 !~ /^\*[A-z-]+$/ &&
241 $1 != "toor") {
242 if ($10 == "" || shells[$10])
243 printf "Login %s is off but still has "\
244 "a valid shell (%s)\n", $1, $10;
245 } else if (! shells[$10])
246 printf "Login %s does not have a valid "\
247 "shell (%s)\n", $1, $10;
248 }
249 if ($3 == 0 && !uid0_users[$1] && !nowarn_users[$1])
250 printf "Login %s has a user id of 0.\n", $1;
251 if ($3 < 0)
252 printf "Login %s has a negative user id.\n", $1;
253 if ($4 < 0)
254 printf "Login %s has a negative group id.\n", $1;
255 }' < $MP > $OUTPUT
256 if [ -s $OUTPUT ] ; then
257 printf "\nChecking the $MP file:\n"
258 cat $OUTPUT
259 fi
260
261 awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
262 if [ -s $OUTPUT ] ; then
263 printf "\n$MP has duplicate user names.\n"
264 column $OUTPUT
265 fi
266
267 # To not exclude 'toor', a standard duplicate root account, from the duplicate
268 # account test, uncomment the line below (without egrep in it)and comment
269 # out the line (with egrep in it) below it.
270 #
271 # < $MPBYUID uniq -d -f 1 | awk '{ print $2 }' > $TMP2
272 < $MPBYUID egrep -v '^toor ' | uniq -d -f 1 | awk '{ print $2 }' > $TMP2
273 if [ -s $TMP2 ] ; then
274 printf "\n$MP has duplicate user id's.\n"
275 while read uid; do
276 grep -w $uid $MPBYUID
277 done < $TMP2 | column
278 fi
279 fi
280
281 # Check the group file syntax.
282 #
283 if checkyesno check_group; then
284 GRP=/etc/group
285 awk -F: -v "len=$max_grouplen" '{
286 if ($0 ~ /^[ ]*$/) {
287 printf "Line %d is a blank line.\n", NR;
288 next;
289 }
290 if (NF != 4 && ($1 != "+" || NF != 1))
291 printf "Line %d has the wrong number of fields.\n", NR;
292 if ($1 == "+" ) {
293 next;
294 }
295 if ($1 !~ /^[_A-Za-z0-9]([-A-Za-z0-9_.]*[A-Za-z0-9])*$/)
296 printf "Group %s has non-alphanumeric characters.\n",
297 $1;
298 if (length($1) > len)
299 printf "Group %s has more than "len" characters.\n", $1;
300 if ($3 !~ /[0-9]*/)
301 printf "Login %s has a negative group id.\n", $1;
302 }' < $GRP > $OUTPUT
303 if [ -s $OUTPUT ] ; then
304 printf "\nChecking the $GRP file:\n"
305 cat $OUTPUT
306 fi
307
308 awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
309 if [ -s $OUTPUT ] ; then
310 printf "\n$GRP has duplicate group names.\n"
311 column $OUTPUT
312 fi
313 fi
314
315 # Check for root paths, umask values in startup files.
316 # The check for the root paths is problematical -- it's likely to fail
317 # in other environments. Once the shells have been modified to warn
318 # of '.' in the path, the path tests should go away.
319 #
320 if checkyesno check_rootdotfiles; then
321 rhome=~root
322 umaskset=no
323 list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
324 for i in $list ; do
325 if [ -f $i ] ; then
326 if egrep '^[ \t]*umask[ \t]+[0-7]+' $i > /dev/null ;
327 then
328 umaskset=yes
329 fi
330 # Double check the umask value itself; ensure that
331 # both the group and other write bits are set.
332 #
333 egrep '^[ \t]*umask[ \t]+[0-7]+' $i |
334 awk '{
335 if ($2 ~ /^.$/ || $2 ~! /[^2367].$/) {
336 print "\tRoot umask is group writable"
337 }
338 if ($2 ~ /[^2367]$/) {
339 print "\tRoot umask is other writable"
340 }
341 }' | sort -u
342 SAVE_PATH=$PATH
343 unset PATH
344 /bin/csh -f -s << end-of-csh > /dev/null 2>&1
345 source $i
346 /bin/ls -ldgT \$path > $TMP1
347 end-of-csh
348 export PATH=$SAVE_PATH
349 awk '{
350 if ($10 ~ /^\.$/) {
351 print "\tThe root path includes .";
352 next;
353 }
354 }
355 $1 ~ /^d....w/ \
356 { print "\tRoot path directory " $10 " is group writable." } \
357 $1 ~ /^d.......w/ \
358 { print "\tRoot path directory " $10 " is other writable." }' \
359 < $TMP1
360 fi
361 done > $OUTPUT
362 if [ $umaskset = "no" -o -s $OUTPUT ] ; then
363 printf "\nChecking root csh paths, umask values:\n$list\n\n"
364 if [ -s $OUTPUT ]; then
365 cat $OUTPUT
366 fi
367 if [ $umaskset = "no" ] ; then
368 printf "\tRoot csh startup files do not set the umask.\n"
369 fi
370 fi
371
372 umaskset=no
373 list="/etc/profile ${rhome}/.profile"
374 for i in $list; do
375 if [ -f $i ] ; then
376 if egrep umask $i > /dev/null ; then
377 umaskset=yes
378 fi
379 egrep umask $i |
380 awk '$2 ~ /^.$/ || $2 ~ /[^2367].$/ \
381 { print "\tRoot umask is group writable" } \
382 $2 ~ /[^2367]$/ \
383 { print "\tRoot umask is other writable" }'
384 SAVE_PATH=$PATH
385 unset PATH
386 /bin/sh << end-of-sh > /dev/null 2>&1
387 . $i
388 list=\`echo \$PATH | /usr/bin/sed -e \
389 's/^:/.:/;s/:$/:./;s/::/:.:/g;s/:/ /g'\`
390 /bin/ls -ldgT \$list > $TMP1
391 end-of-sh
392 export PATH=$SAVE_PATH
393 awk '{
394 if ($10 ~ /^\.$/) {
395 print "\tThe root path includes .";
396 next;
397 }
398 }
399 $1 ~ /^d....w/ \
400 { print "\tRoot path directory " $10 " is group writable." } \
401 $1 ~ /^d.......w/ \
402 { print "\tRoot path directory " $10 " is other writable." }' \
403 < $TMP1
404
405 fi
406 done > $OUTPUT
407 if [ $umaskset = "no" -o -s $OUTPUT ] ; then
408 printf "\nChecking root sh paths, umask values:\n$list\n"
409 if [ -s $OUTPUT ]; then
410 cat $OUTPUT
411 fi
412 if [ $umaskset = "no" ] ; then
413 printf "\tRoot sh startup files do not set the umask.\n"
414 fi
415 fi
416 fi
417
418 # Root and uucp should both be in /etc/ftpusers.
419 #
420 if checkyesno check_ftpusers; then
421 list="uucp "`awk '$2 == 0 { print $1 }' $MPBYUID`
422 for i in $list; do
423 if /usr/libexec/ftpd -C $i ; then
424 printf "\t$i is not denied\n"
425 fi
426 done > $OUTPUT
427 if [ -s $OUTPUT ]; then
428 printf "\nChecking the /etc/ftpusers configuration:\n"
429 cat $OUTPUT
430 fi
431 fi
432
433 # Uudecode should not be in the /etc/mail/aliases file.
434 #
435 if checkyesno check_aliases; then
436 for f in /etc/mail/aliases /etc/aliases; do
437 if [ -f $f ] && egrep '^[^#]*(uudecode|decode).*\|' $f; then
438 printf "\nEntry for uudecode in $f file.\n"
439 fi
440 done
441 fi
442
443 # Files that should not have + signs.
444 #
445 if checkyesno check_rhosts; then
446 list="/etc/hosts.equiv /etc/hosts.lpd"
447 for f in $list ; do
448 if [ -f $f ] && egrep '\+' $f > /dev/null ; then
449 printf "\nPlus sign in $f file.\n"
450 fi
451 done
452
453 # Check for special users with .rhosts files. Only root and toor should
454 # have .rhosts files. Also, .rhosts files should not have plus signs.
455 awk -F: '$1 != "root" && $1 != "toor" && \
456 ($3 < 100 || $1 == "ftp" || $1 == "uucp") \
457 { print $1 " " $9 }' $MP |
458 sort -k2 |
459 while read uid homedir; do
460 if [ -f ${homedir}/.rhosts ] ; then
461 rhost=`ls -ldgT ${homedir}/.rhosts`
462 printf -- "$uid: $rhost\n"
463 fi
464 done > $OUTPUT
465 if [ -s $OUTPUT ] ; then
466 printf "\nChecking for special users with .rhosts files.\n"
467 cat $OUTPUT
468 fi
469
470 while read uid homedir; do
471 if [ -f ${homedir}/.rhosts -a -r ${homedir}/.rhosts ] && \
472 cat -f ${homedir}/.rhosts | egrep '\+' > /dev/null ; then
473 printf -- "$uid: + in .rhosts file.\n"
474 fi
475 done < $MPBYPATH > $OUTPUT
476 if [ -s $OUTPUT ] ; then
477 printf "\nChecking .rhosts files syntax.\n"
478 cat $OUTPUT
479 fi
480 fi
481
482 # Check home directories. Directories should not be owned by someone else
483 # or writable.
484 #
485 if checkyesno check_homes; then
486 checkyesno check_homes_permit_usergroups && \
487 permit_usergroups=1 || permit_usergroups=0
488 while read uid homedir; do
489 if [ -d ${homedir}/ ] ; then
490 file=`ls -ldgT ${homedir}`
491 printf -- "$uid $file\n"
492 fi
493 done < $MPBYPATH |
494 awk -v "usergroups=$permit_usergroups" '
495 $1 != $4 && $4 != "root" \
496 { print "user " $1 " home directory is owned by " $4 }
497 $2 ~ /^-....w/ && (!usergroups || $5 != $1) \
498 { print "user " $1 " home directory is group writable" }
499 $2 ~ /^-.......w/ \
500 { print "user " $1 " home directory is other writable" }' \
501 > $OUTPUT
502 if [ -s $OUTPUT ] ; then
503 printf "\nChecking home directories.\n"
504 cat $OUTPUT
505 fi
506
507 # Files that should not be owned by someone else or readable.
508 list=".Xauthority .netrc .ssh/id_dsa .ssh/id_rsa .ssh/identity"
509 while read uid homedir; do
510 for f in $list ; do
511 file=${homedir}/${f}
512 if [ -f $file ] ; then
513 printf -- "$uid $f `ls -ldgT $file`\n"
514 fi
515 done
516 done < $MPBYPATH |
517 awk -v "usergroups=$permit_usergroups" '
518 $1 != $5 && $5 != "root" \
519 { print "user " $1 " " $2 " file is owned by " $5 }
520 $3 ~ /^-...r/ && (!usergroups || $6 != $1) \
521 { print "user " $1 " " $2 " file is group readable" }
522 $3 ~ /^-......r/ \
523 { print "user " $1 " " $2 " file is other readable" }
524 $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
525 { print "user " $1 " " $2 " file is group writable" }
526 $3 ~ /^-.......w/ \
527 { print "user " $1 " " $2 " file is other writable" }' \
528 > $OUTPUT
529
530 # Files that should not be owned by someone else or writable.
531 list=".bash_history .bash_login .bash_logout .bash_profile .bashrc \
532 .cshrc .emacs .exrc .forward .history .k5login .klogin .login \
533 .logout .profile .qmail .rc_history .rhosts .shosts ssh .tcshrc \
534 .twmrc .xinitrc .xsession .ssh/authorized_keys \
535 .ssh/authorized_keys2 .ssh/config .ssh/id_dsa.pub \
536 .ssh/id_rsa.pub .ssh/identity.pub .ssh/known_hosts \
537 .ssh/known_hosts2"
538 while read uid homedir; do
539 for f in $list ; do
540 file=${homedir}/${f}
541 if [ -f $file ] ; then
542 printf -- "$uid $f `ls -ldgT $file`\n"
543 fi
544 done
545 done < $MPBYPATH |
546 awk -v "usergroups=$permit_usergroups" '
547 $1 != $5 && $5 != "root" \
548 { print "user " $1 " " $2 " file is owned by " $5 }
549 $3 ~ /^-....w/ && (!usergroups || $6 != $1) \
550 { print "user " $1 " " $2 " file is group writable" }
551 $3 ~ /^-.......w/ \
552 { print "user " $1 " " $2 " file is other writable" }' \
553 >> $OUTPUT
554 if [ -s $OUTPUT ] ; then
555 printf "\nChecking dot files.\n"
556 cat $OUTPUT
557 fi
558 fi
559
560 # Mailboxes should be owned by user and unreadable.
561 #
562 if checkyesno check_varmail; then
563 ls -lA /var/mail | \
564 awk ' NR == 1 { next; }
565 $9 ~ /^\./ {next; }
566 $3 != $9 {
567 print "user " $9 " mailbox is owned by " $3
568 }
569 $1 != "-rw-------" {
570 print "user " $9 " mailbox is " $1 ", group " $4
571 }' > $OUTPUT
572 if [ -s $OUTPUT ] ; then
573 printf "\nChecking mailbox ownership.\n"
574 cat $OUTPUT
575 fi
576 fi
577
578 # NFS exports shouldn't be globally exported
579 #
580 if checkyesno check_nfs && [ -f /etc/exports ]; then
581 awk '{
582 # ignore comments and blank lines
583 if ($0 ~ /^\#/ || $0 ~ /^$/ )
584 next;
585
586 readonly = 0;
587 for (i = 2; i <= NF; ++i) {
588 if ($i ~ /-ro/)
589 readonly = 1;
590 else if ($i ~ /^-network=/)
591 next;
592 else if ($i !~ /^-/)
593 next;
594 }
595 if (readonly)
596 print "File system " $1 " globally exported, read-only."
597 else
598 print "File system " $1 " globally exported, read-write."
599 }' < /etc/exports > $OUTPUT
600 if [ -s $OUTPUT ] ; then
601 printf "\nChecking for globally exported file systems.\n"
602 cat $OUTPUT
603 fi
604 fi
605
606 # Display any changes in setuid files and devices.
607 #
608 if checkyesno check_devices; then
609 > $ERR
610 (
611
612 # Convert check_devices_ignore_fstypes="foo !bar bax"
613 # into "-fstype foo -o ! -fstype bar -o -fstype bax"
614 # and check_devices_ignore_paths="/foo !/bar /bax"
615 # into " -path /foo -o ! -path /bar -o -path /bax"
616 #
617 ignexpr=$(\
618 echo $check_devices_ignore_fstypes | \
619 sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -fstype \2/g' ; \
620 echo $check_devices_ignore_paths | \
621 sed -e's/\(!*\)\([^[:space:]]\{1,\}\)/-o \1 -path \2/g' \
622 )
623
624 # Massage the expression into ( $ignexpr ) -a -prune -o
625 if [ -n "${ignexpr}" ]; then
626 ignexpr=$(\
627 echo $ignexpr | \
628 sed -e 's/^-o /( /' \
629 -e 's/$/ ) -a -prune -o/' \
630 )
631 fi
632
633 find / $ignexpr \
634 \( \( -perm -u+s -a ! -type d \) -o \
635 \( -perm -g+s -a ! -type d \) -o \
636 -type b -o -type c \) -print0 | \
637 xargs -0 ls -ldgTq | sort +9 > $LIST
638
639 ) 2> $OUTPUT
640
641 # Display any errors that occurred during system file walk.
642 if [ -s $OUTPUT ] ; then
643 printf "Setuid/device find errors:\n" >> $ERR
644 cat $OUTPUT >> $ERR
645 printf "\n" >> $ERR
646 fi
647
648 # Display any changes in the setuid file list.
649 egrep -v '^[bc]' $LIST > $TMP1
650 if [ -s $TMP1 ] ; then
651 # Check to make sure uudecode isn't setuid.
652 if grep -w uudecode $TMP1 > /dev/null ; then
653 printf "\nUudecode is setuid.\n" >> $ERR
654 fi
655
656 file=$work_dir/setuid
657 migrate_file "$backup_dir/setuid" "$file"
658 CUR=${file}.current
659 BACK=${file}.backup
660 if [ -s $CUR ] ; then
661 if cmp -s $CUR $TMP1 ; then
662 :
663 else
664 > $TMP2
665 join -110 -210 -v2 $CUR $TMP1 > $OUTPUT
666 if [ -s $OUTPUT ] ; then
667 printf "Setuid additions:\n" >> $ERR
668 tee -a $TMP2 < $OUTPUT >> $ERR
669 printf "\n" >> $ERR
670 fi
671
672 join -110 -210 -v1 $CUR $TMP1 > $OUTPUT
673 if [ -s $OUTPUT ] ; then
674 printf "Setuid deletions:\n" >> $ERR
675 tee -a $TMP2 < $OUTPUT >> $ERR
676 printf "\n" >> $ERR
677 fi
678
679 sort -k10 $TMP2 $CUR $TMP1 | \
680 sed -e 's/[ ][ ]*/ /g' | \
681 uniq -u > $OUTPUT
682 if [ -s $OUTPUT ] ; then
683 printf "Setuid changes:\n" >> $ERR
684 column -t $OUTPUT >> $ERR
685 printf "\n" >> $ERR
686 fi
687
688 backup_file update $TMP1 $CUR $BACK
689 fi
690 else
691 printf "Setuid additions:\n" >> $ERR
692 column -t $TMP1 >> $ERR
693 printf "\n" >> $ERR
694 backup_file add $TMP1 $CUR $BACK
695 fi
696 fi
697
698 # Check for block and character disk devices that are readable or
699 # writable or not owned by root.operator.
700 >$TMP1
701 DISKLIST="ccd ch hk hp ld md ra raid rb rd rl rx \
702 sd se ss uk up vnd wd xd xy"
703 # DISKLIST="$DISKLIST ct mt st wt"
704 for i in $DISKLIST; do
705 egrep "^b.*/${i}[0-9][0-9]*[a-p]$" $LIST >> $TMP1
706 egrep "^c.*/r${i}[0-9][0-9]*[a-p]$" $LIST >> $TMP1
707 done
708
709 awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
710 { printf "Disk %s is user %s, group %s, permissions %s.\n", \
711 $11, $3, $4, $1; }' < $TMP1 > $OUTPUT
712 if [ -s $OUTPUT ] ; then
713 printf "\nChecking disk ownership and permissions.\n" >> $ERR
714 cat $OUTPUT >> $ERR
715 printf "\n" >> $ERR
716 fi
717
718 # Display any changes in the device file list.
719 egrep '^[bc]' $LIST | sort -k11 > $TMP1
720 if [ -s $TMP1 ] ; then
721 file=$work_dir/device
722 migrate_file "$backup_dir/device" "$file"
723 CUR=${file}.current
724 BACK=${file}.backup
725
726 if [ -s $CUR ] ; then
727 if cmp -s $CUR $TMP1 ; then
728 :
729 else
730 > $TMP2
731 join -111 -211 -v2 $CUR $TMP1 > $OUTPUT
732 if [ -s $OUTPUT ] ; then
733 printf "Device additions:\n" >> $ERR
734 tee -a $TMP2 < $OUTPUT >> $ERR
735 printf "\n" >> $ERR
736 fi
737
738 join -111 -211 -v1 $CUR $TMP1 > $OUTPUT
739 if [ -s $OUTPUT ] ; then
740 printf "Device deletions:\n" >> $ERR
741 tee -a $TMP2 < $OUTPUT >> $ERR
742 printf "\n" >> $ERR
743 fi
744
745 # Report any block device change. Ignore
746 # character devices, only the name is
747 # significant.
748 cat $TMP2 $CUR $TMP1 | \
749 sed -e '/^c/d' | \
750 sort -k11 | \
751 sed -e 's/[ ][ ]*/ /g' | \
752 uniq -u > $OUTPUT
753 if [ -s $OUTPUT ] ; then
754 printf "Block device changes:\n" >> $ERR
755 column -t $OUTPUT >> $ERR
756 printf "\n" >> $ERR
757 fi
758
759 backup_file update $TMP1 $CUR $BACK
760 fi
761 else
762 printf "Device additions:\n" >> $ERR
763 column -t $TMP1 >> $ERR
764 printf "\n" >> $ERR
765 backup_file add $TMP1 $CUR $BACK >> $ERR
766 fi
767 fi
768 if [ -s $ERR ] ; then
769 printf "\nChecking setuid files and devices:\n"
770 cat $ERR
771 printf "\n"
772 fi
773 fi
774
775 # Check special files.
776 # Check system binaries.
777 #
778 # Create the mtree tree specifications using:
779 # mtree -cx -pDIR -kmd5,uid,gid,mode,nlink,size,link,time > DIR.secure
780 # chown root:wheel DIR.secure
781 # chmod u+r,go= DIR.secure
782 #
783 # Note, this is not complete protection against Trojan horsed binaries, as
784 # the hacker can modify the tree specification to match the replaced binary.
785 # For details on really protecting yourself against modified binaries, see
786 # the mtree(8) manual page.
787 #
788 if checkyesno check_mtree; then
789 if checkyesno check_mtree_follow_symlinks; then
790 check_mtree_flags="-L"
791 else
792 check_mtree_flags=""
793 fi
794 mtree -e -l -p / $check_mtree_flags -f $SPECIALSPEC 3>&1 >$OUTPUT 2>&3 |
795 grep -v '^mtree: dev/tty: Device not configured$' >&2
796 if [ -s $OUTPUT ]; then
797 printf "\nChecking special files and directories.\n"
798 cat $OUTPUT
799 fi
800
801 for file in /etc/mtree/*.secure; do
802 [ $file = '/etc/mtree/*.secure' ] && continue
803 tree=`sed -n -e '3s/.* //p' -e 3q $file`
804 mtree $check_mtree_flags -f $file -p $tree > $TMP1
805 if [ -s $TMP1 ]; then
806 printf "\nChecking $tree:\n"
807 cat $TMP1
808 fi
809 done > $OUTPUT
810 if [ -s $OUTPUT ]; then
811 printf "\nChecking system binaries:\n"
812 cat $OUTPUT
813 fi
814 fi
815
816 # Backup disklabels of available disks
817 #
818 if checkyesno check_disklabels; then
819 # migrate old disklabels
820 for file in `ls -1d $backup_dir/$backup_dir/disklabel.* \
821 $backup_dir/disklabel.* 2>/dev/null`; do
822 migrate_file "$file" "$work_dir/${file##*/}"
823 done
824
825 # generate list of old disklabels & fdisks and remove them
826 ls -1d $work_dir/disklabel.* $work_dir/fdisk.* 2>/dev/null |
827 egrep -v '\.(backup|current)(,v)?$' > $LABELS
828 xargs rm < $LABELS
829
830 # generate disklabels of all disks excluding: cd fd md st
831 disks=`iostat -x | awk 'NR > 1 && $1 !~ /^[cfm]d|st|nfs/ { print $1; }'`
832 for i in $disks; do
833 disklabel $i > "$work_dir/disklabel.$i" 2>/dev/null
834 done
835
836 # if fdisk is available, generate fdisks for: ed ld sd wd
837 if [ -x /sbin/fdisk ]; then
838 disks=`iostat -x| awk 'NR > 1 && $1 ~ /^[elsw]d/ { print $1; }'`
839 for i in $disks; do
840 /sbin/fdisk $i > "$work_dir/fdisk.$i" 2>/dev/null
841 done
842 fi
843
844 # append list of new disklabels and fdisks
845 ls -1d $work_dir/disklabel.* $work_dir/fdisk.* 2>/dev/null |
846 egrep -v '\.(backup|current)(,v)?$' >> $LABELS
847 CHANGELIST="$LABELS $CHANGELIST"
848 fi
849
850 # Check for changes in the list of installed pkgs
851 #
852 if checkyesno check_pkgs && [ -d $pkgdb_dir ]; then
853 pkgs=$work_dir/pkgs
854 migrate_file "$backup_dir/pkgs" "$pkgs"
855 ( cd $pkgdb_dir
856 pkg_info | sort
857 echo ""
858 find . \( -name +REQUIRED_BY -o -name +CONTENTS \) -print0 |
859 xargs -0 ls -ldgTq | sort -t. +1 | sed -e 's, \./, ,'
860 ) > $pkgs
861 echo "$pkgs" > $PKGS
862 CHANGELIST="$PKGS $CHANGELIST"
863 fi
864
865 # List of files that get backed up and checked for any modifications.
866 # Any changes cause the files to rotate.
867 #
868 if checkyesno check_changelist ; then
869 mtree -D -k type -f $SPECIALSPEC -E exclude |
870 sed '/^type=file/!d ; s/type=file \.//' | unvis > $CHANGEFILES
871
872 (
873 # Add other files which might dynamically exist:
874 # /etc/ifconfig.*
875 # /etc/raid*.conf
876 # /etc/rc.d/*
877 # /etc/rc.conf.d/*
878 #
879 echo "/etc/ifconfig.*"
880 echo "/etc/raid*.conf"
881 echo "/etc/rc.d/*"
882 echo "/etc/rc.conf.d/*"
883
884 # Add /etc/changelist
885 #
886 if [ -s /etc/changelist ]; then
887 grep -v '^#' /etc/changelist
888 fi
889 ) | while read file; do
890 case "$file" in
891 *[\*\?\[]*) # If changelist line is a glob ...
892 # ... expand possible backup files
893 #
894 ls -1d $(echo $backup_dir/${file}.current) 2>/dev/null \
895 | sed "s,^$backup_dir/,, ; s,\.current$,,"
896
897 # ... expand possible files
898 #
899 ls -1d $(echo $file) 2>/dev/null
900 ;;
901 *)
902 # Otherwise, just print the filename
903 echo $file
904 ;;
905 esac
906 done >> $CHANGEFILES
907 CHANGELIST="$CHANGEFILES $CHANGELIST"
908 fi
909
910 # Special case backups, including the master password file and
911 # ssh private host keys. The normal backup mechanisms for
912 # $check_changelist (see below) also print out the actual file
913 # differences and we don't want to do that for these files
914 #
915 echo $MP > $TMP1 # always add /etc/master.passwd
916 mtree -D -k type -f $SPECIALSPEC -I nodiff |
917 sed '/^type=file/!d ; s/type=file \.//' | unvis >> $TMP1
918 grep -v '^$' $TMP1 | sort -u > $TMP2
919
920 while read file; do
921 backup_and_diff "$file" no
922 done < $TMP2
923
924
925 if [ -n "$CHANGELIST" ]; then
926 grep -h -v '^$' $CHANGELIST | sort -u > $TMP1
927 comm -23 $TMP1 $TMP2 | while read file; do
928 backup_and_diff "$file" yes
929 done
930 fi
931
932 if [ -f /etc/security.local ]; then
933 . /etc/security.local > $OUTPUT 2>&1
934 if [ -s $OUTPUT ] ; then
935 printf "\nRunning /etc/security.local:\n"
936 cat $OUTPUT
937 fi
938 fi
939