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