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