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