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