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