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