[BACK]Return to security CVS log [TXT][DIR] Up to [local] / src / etc

Annotation of src/etc/security, Revision 1.68

1.1       deraadt     1: #!/bin/sh -
                      2: #
1.68    ! millert     3: #      $OpenBSD: security,v 1.67 2004/08/25 19:17:30 millert Exp $
1.13      millert     4: #      from: @(#)security      8.1 (Berkeley) 6/9/93
1.1       deraadt     5: #
1.13      millert     6:
1.55      millert     7: PATH=/bin:/usr/bin:/sbin:/usr/sbin
1.1       deraadt     8:
                      9: umask 077
                     10:
1.60      avsm       11: DIR=`mktemp -d /tmp/_secure.XXXXXXXXXX` || exit 1
1.7       deraadt    12: ERR=$DIR/_secure1
                     13: TMP1=$DIR/_secure2
                     14: TMP2=$DIR/_secure3
                     15: TMP3=$DIR/_secure4
                     16: LIST=$DIR/_secure5
                     17: OUTPUT=$DIR/_secure6
1.8       deraadt    18:
1.25      deraadt    19: trap 'rm -rf $DIR; exit 1' 0 1 2 3 13 15
1.1       deraadt    20:
                     21: # Check the master password file syntax.
                     22: MP=/etc/master.passwd
                     23: awk -F: '{
                     24:        if ($0 ~ /^[     ]*$/) {
                     25:                printf("Line %d is a blank line.\n", NR);
                     26:                next;
                     27:        }
                     28:        if (NF != 10)
1.32      espie      29:                printf("Line %d has the wrong number of fields:\n%s\n", NR, $0);
1.15      millert    30:        if ($1 ~ /^[+-]/)
1.2       deraadt    31:                next;
                     32:        if ($1 == "")
1.32      espie      33:                printf("Line %d has an empty login field:\n%s\n", NR, $0);
1.59      grange     34:        else if ($1 !~ /^[A-Za-z0-9_][A-Za-z0-9_\-\.]*\$?$/)
1.1       deraadt    35:                printf("Login %s has non-alphanumeric characters.\n", $1);
1.48      brad       36:        if (length($1) > 31)
                     37:                printf("Login %s has more than 31 characters.\n", $1);
1.1       deraadt    38:        if ($2 == "")
                     39:                printf("Login %s has no password.\n", $1);
1.23      deraadt    40:        if ($2 != "" && length($2) != 13 && ($10 ~ /.*sh$/ || $10 == "") &&
                     41:            ($2 !~ /^\$[0-9a-f]+\$/) && ($2 != "skey")) {
1.51      millert    42:                if (system("test -s /etc/skey/"$1"") == 0)
                     43:                        printf("Login %s is off but still has a valid shell and an entry in /etc/skey.\n", $1);
1.23      deraadt    44:                if (system("test -d "$9" -a ! -r "$9"") == 0)
1.40      hugh       45:                        printf("Login %s is off but still has valid shell and home directory is unreadable\n\t by root; cannot check for existence of alternate access files.\n", $1);
1.23      deraadt    46:                else if (system("for file in .ssh .rhosts .shosts .klogin; do if test -e "$9"/$file; then if ((ls -ld "$9"/$file | cut -b 2-10 | grep -q r) && (test ! -O "$9"/$file)) ; then exit 1; fi; fi; done"))
                     47:                         printf("Login %s is off but still has a valid shell and alternate access files in\n\t home directory are still readable.\n",$1);
                     48:        }
1.9       deraadt    49:        if ($3 == 0 && $1 != "root")
1.36      aaron      50:                printf("Login %s has a user ID of 0.\n", $1);
1.1       deraadt    51:        if ($3 < 0)
1.36      aaron      52:                printf("Login %s has a negative user ID.\n", $1);
1.1       deraadt    53:        if ($4 < 0)
1.36      aaron      54:                printf("Login %s has a negative group ID.\n", $1);
1.64      millert    55:        if (int($7) != 0 && system("test "$7" -lt `date +%s`") == 0)
1.53      pvalchev   56:                printf("Login %s has expired.\n", $1);
1.1       deraadt    57: }' < $MP > $OUTPUT
                     58: if [ -s $OUTPUT ] ; then
1.41      millert    59:        echo "\nChecking the ${MP} file:"
1.1       deraadt    60:        cat $OUTPUT
                     61: fi
                     62:
                     63: awk -F: '{ print $1 }' $MP | sort | uniq -d > $OUTPUT
                     64: if [ -s $OUTPUT ] ; then
1.41      millert    65:        echo "\n${MP} has duplicate user names."
1.1       deraadt    66:        column $OUTPUT
                     67: fi
                     68:
1.47      millert    69: awk -F: '/^[^\+]/ { print $1 " " $3 }' $MP | sort -n +1 | tee $TMP1 |
1.1       deraadt    70: uniq -d -f 1 | awk '{ print $2 }' > $TMP2
                     71: if [ -s $TMP2 ] ; then
1.41      millert    72:        echo "\n${MP} has duplicate user ID's."
1.1       deraadt    73:         while read uid; do
                     74:                 grep -w $uid $TMP1
                     75:         done < $TMP2 | column
                     76: fi
                     77:
                     78: # Backup the master password file; a special case, the normal backup
                     79: # mechanisms also print out file differences and we don't want to do
                     80: # that because this file has encrypted passwords in it.
1.2       deraadt    81: if [ ! -d /var/backups ] ; then
                     82:        mkdir /var/backups
1.31      deraadt    83:        chmod 700 /var/backups
1.2       deraadt    84: fi
1.1       deraadt    85: CUR=/var/backups/`basename $MP`.current
                     86: BACK=/var/backups/`basename $MP`.backup
                     87: if [ -s $CUR ] ; then
                     88:        if cmp -s $CUR $MP; then
                     89:                :
                     90:        else
                     91:                cp -p $CUR $BACK
                     92:                cp -p $MP $CUR
1.56      millert    93:                chown root:wheel $CUR
1.1       deraadt    94:        fi
                     95: else
                     96:        cp -p $MP $CUR
1.56      millert    97:        chown root:wheel $CUR
1.1       deraadt    98: fi
                     99:
                    100: # Check the group file syntax.
                    101: GRP=/etc/group
                    102: awk -F: '{
                    103:        if ($0 ~ /^[     ]*$/) {
                    104:                printf("Line %d is a blank line.\n", NR);
                    105:                next;
                    106:        }
1.2       deraadt   107:        if ($1 ~ /^[+-].*$/)
                    108:                next;
1.1       deraadt   109:        if (NF != 4)
1.32      espie     110:                printf("Line %d has the wrong number of fields:\n%s\n", NR, $0);
1.65      sturm     111:        if ($1 !~ /^[A-Za-z0-9_][A-Za-z0-9_\-\.]*$/)
1.1       deraadt   112:                printf("Group %s has non-alphanumeric characters.\n", $1);
1.48      brad      113:        if (length($1) > 31)
                    114:                printf("Group %s has more than 31 characters.\n", $1);
1.1       deraadt   115:        if ($3 !~ /[0-9]*/)
1.36      aaron     116:                printf("Login %s has a negative group ID.\n", $1);
1.1       deraadt   117: }' < $GRP > $OUTPUT
                    118: if [ -s $OUTPUT ] ; then
1.41      millert   119:        echo "\nChecking the ${GRP} file:"
1.1       deraadt   120:        cat $OUTPUT
                    121: fi
                    122:
                    123: awk -F: '{ print $1 }' $GRP | sort | uniq -d > $OUTPUT
                    124: if [ -s $OUTPUT ] ; then
1.41      millert   125:        echo "\n${GRP} has duplicate group names."
1.1       deraadt   126:        column $OUTPUT
                    127: fi
                    128:
                    129: # Check for root paths, umask values in startup files.
                    130: # The check for the root paths is problematical -- it's likely to fail
                    131: # in other environments.  Once the shells have been modified to warn
                    132: # of '.' in the path, the path tests should go away.
                    133: > $OUTPUT
                    134: rhome=/root
                    135: umaskset=no
                    136: list="/etc/csh.cshrc /etc/csh.login ${rhome}/.cshrc ${rhome}/.login"
                    137: for i in $list ; do
1.15      millert   138:        if [ -s $i ] ; then
1.63      millert   139:                if egrep -q '[[:space:]]*umask[[:space:]]' $i ; then
                    140:                        umaskset=yes
                    141:                fi
1.61      millert   142:                awk '{
                    143:                    if ($1 == "umask") {
1.62      millert   144:                         if ($2 % 100 / 10 ~ /^[0145]/)
1.61      millert   145:                            print "Root umask is group writable";
                    146:                         if ($2 % 10 ~ /^[0145]/)
                    147:                            print "Root umask is other writable";
                    148:                    }
1.63      millert   149:                }' < $i >> $OUTPUT
1.21      deraadt   150:                SAVE_PATH=$PATH
                    151:                unset PATH
1.1       deraadt   152:                /bin/csh -f -s << end-of-csh > /dev/null 2>&1
                    153:                        source $i
1.29      marc      154:                        if (\$?path) then
                    155:                                /bin/ls -ldgT \$path > $TMP1
                    156:                        else
                    157:                                cat /dev/null > $TMP1
                    158:                        endif
1.1       deraadt   159: end-of-csh
1.21      deraadt   160:                PATH=$SAVE_PATH
1.1       deraadt   161:                awk '{
                    162:                        if ($10 ~ /^\.$/) {
                    163:                                print "The root path includes .";
                    164:                                next;
                    165:                        }
                    166:                     }
                    167:                     $1 ~ /^d....w/ \
1.54      henning   168:         { print "Root path directory " $10 " is group writable." } \
1.1       deraadt   169:                     $1 ~ /^d.......w/ \
1.54      henning   170:         { print "Root path directory " $10 " is other writable." }' \
1.1       deraadt   171:                < $TMP1 >> $OUTPUT
                    172:        fi
                    173: done
                    174: if [ $umaskset = "no" -o -s $OUTPUT ] ; then
1.41      millert   175:        echo "\nChecking root csh paths, umask values:\n${list}"
1.13      millert   176:        if [ -s $OUTPUT ] ; then
1.1       deraadt   177:                cat $OUTPUT
                    178:        fi
                    179:        if [ $umaskset = "no" ] ; then
1.41      millert   180:                echo "\nRoot csh startup files do not set the umask."
1.1       deraadt   181:        fi
                    182: fi
                    183:
                    184: > $OUTPUT
1.29      marc      185: > $TMP2
1.1       deraadt   186: rhome=/root
                    187: umaskset=no
1.20      millert   188: list="/etc/profile ${rhome}/.profile"
1.1       deraadt   189: for i in $list; do
1.15      millert   190:        if [ -s $i ] ; then
1.1       deraadt   191:                if egrep umask $i > /dev/null ; then
                    192:                        umaskset=yes
                    193:                fi
                    194:                egrep umask $i |
                    195:                awk '$2 % 100 < 20 \
1.54      henning   196:                        { print "Root umask is group writable" } \
1.1       deraadt   197:                     $2 % 10 < 2 \
1.54      henning   198:                        { print "Root umask is other writable" }' >> $OUTPUT
1.21      deraadt   199:                SAVE_PATH=$PATH
1.29      marc      200:                SAVE_ENV=$ENV
                    201:                unset PATH ENV
1.1       deraadt   202:                /bin/sh << end-of-sh > /dev/null 2>&1
                    203:                        . $i
1.29      marc      204:                        if [ X"\$PATH" != "X" ]; then
                    205:                                list=\`echo \$PATH | /usr/bin/sed -e 's/:/ /g'\`
                    206:                                /bin/ls -ldgT \$list > $TMP1
                    207:                        else
                    208:                                > $TMP1
                    209:                        fi
                    210:                        echo \$ENV >> $TMP2
1.1       deraadt   211: end-of-sh
1.21      deraadt   212:                PATH=$SAVE_PATH
1.29      marc      213:                ENV=$SAVE_ENV
1.1       deraadt   214:                awk '{
                    215:                        if ($10 ~ /^\.$/) {
                    216:                                print "The root path includes .";
                    217:                                next;
                    218:                        }
                    219:                     }
                    220:                     $1 ~ /^d....w/ \
1.54      henning   221:         { print "Root path directory " $10 " is group writable." } \
1.1       deraadt   222:                     $1 ~ /^d.......w/ \
1.54      henning   223:         { print "Root path directory " $10 " is other writable." }' \
1.1       deraadt   224:                < $TMP1 >> $OUTPUT
                    225:
                    226:        fi
                    227: done
                    228: if [ $umaskset = "no" -o -s $OUTPUT ] ; then
1.41      millert   229:        echo "\nChecking root sh paths, umask values:\n${list}"
1.13      millert   230:        if [ -s $OUTPUT ] ; then
1.1       deraadt   231:                cat $OUTPUT
                    232:        fi
                    233:        if [ $umaskset = "no" ] ; then
1.41      millert   234:                echo "\nRoot sh startup files do not set the umask."
1.1       deraadt   235:        fi
                    236: fi
                    237:
1.27      marc      238: # A good .kshrc will not have a umask or path, that being set in .profile
                    239: # check anyway.
                    240: > $OUTPUT
                    241: rhome=/root
1.29      marc      242: list="/etc/ksh.kshrc `cat $TMP2`"
                    243: (cd $rhome
                    244:  for i in $list; do
1.27      marc      245:        if [ -s $i ] ; then
                    246:                egrep umask $i |
                    247:                awk '$2 % 100 < 20 \
1.54      henning   248:                        { print "Root umask is group writable" } \
1.27      marc      249:                     $2 % 10 < 2 \
1.54      henning   250:                        { print "Root umask is other writable" }' >> $OUTPUT
1.27      marc      251:                if egrep PATH= $i > /dev/null ; then
                    252:                        SAVE_PATH=$PATH
                    253:                        unset PATH
                    254:                        /bin/ksh << end-of-sh > /dev/null 2>&1
                    255:                                . $i
1.29      marc      256:                                if [ X"\$PATH" != "X" ]; then
                    257:                                        list=\`echo \$PATH | /usr/bin/sed -e 's/:/ /g'\`
                    258:                                        /bin/ls -ldgT \$list > $TMP1
                    259:                                else
                    260:                                        > $TMP1
                    261:                                fi
1.27      marc      262: end-of-sh
                    263:                        PATH=$SAVE_PATH
                    264:                        awk '{
                    265:                                if ($10 ~ /^\.$/) {
                    266:                                        print "The root path includes .";
                    267:                                        next;
                    268:                                }
                    269:                            }
                    270:                            $1 ~ /^d....w/ \
1.54      henning   271:                { print "Root path directory " $10 " is group writable." } \
1.27      marc      272:                            $1 ~ /^d.......w/ \
1.54      henning   273:                { print "Root path directory " $10 " is other writable." }' \
1.27      marc      274:                        < $TMP1 >> $OUTPUT
                    275:                fi
                    276:
                    277:        fi
1.29      marc      278:  done
                    279: )
1.27      marc      280: if [ -s $OUTPUT ] ; then
1.41      millert   281:        echo "\nChecking root ksh paths, umask values:\n${list}"
1.27      marc      282:        cat $OUTPUT
                    283: fi
                    284:
1.1       deraadt   285: # Root and uucp should both be in /etc/ftpusers.
                    286: if egrep root /etc/ftpusers > /dev/null ; then
                    287:        :
                    288: else
1.41      millert   289:        echo "\nRoot not listed in /etc/ftpusers file."
1.1       deraadt   290: fi
                    291: if egrep uucp /etc/ftpusers > /dev/null ; then
                    292:        :
                    293: else
1.41      millert   294:        echo "\nUucp not listed in /etc/ftpusers file."
1.1       deraadt   295: fi
                    296:
1.35      millert   297: # Uudecode should not be in the /etc/mail/aliases file.
                    298: if egrep 'uudecode|decode' /etc/mail/aliases; then
1.41      millert   299:        echo "\nThere is an entry for uudecode in the /etc/mail/aliases file."
1.1       deraadt   300: fi
                    301:
                    302: # Files that should not have + signs.
1.2       deraadt   303: list="/etc/hosts.equiv /etc/shosts.equiv /etc/hosts.lpd"
1.1       deraadt   304: for f in $list ; do
1.6       millert   305:        if [ -s $f ] ; then
1.2       deraadt   306:                awk '{
1.13      millert   307:                        if ($0 ~ /^\+@.*$/)
1.2       deraadt   308:                                next;
1.13      millert   309:                        if ($0 ~ /^\+.*$/)
1.2       deraadt   310:                                printf("\nPlus sign in %s file.\n", FILENAME);
                    311:                }' $f
1.1       deraadt   312:        fi
                    313: done
                    314:
1.13      millert   315: # Check for special users with .rhosts/.shosts files.  Only root
                    316: # should have .rhosts/.shosts files.  Also, .rhosts/.shosts
                    317: # files should not have plus signs.
1.14      millert   318: awk -F: '$1 != "root" && $1 !~ /^[+-]/ && \
1.1       deraadt   319:        ($3 < 100 || $1 == "ftp" || $1 == "uucp") \
                    320:                { print $1 " " $6 }' /etc/passwd |
                    321: while read uid homedir; do
1.2       deraadt   322:        for j in .rhosts .shosts; do
1.14      millert   323:                # Root owned .rhosts/.shosts files are ok.
1.15      millert   324:                if [ -s ${homedir}/$j -a ! -O ${homedir}/$j ] ; then
1.2       deraadt   325:                        rhost=`ls -ldgT ${homedir}/$j`
1.41      millert   326:                        echo "${uid}: ${rhost}"
1.2       deraadt   327:                fi
                    328:        done
1.1       deraadt   329: done > $OUTPUT
                    330: if [ -s $OUTPUT ] ; then
1.41      millert   331:        echo "\nChecking for special users with .rhosts/.shosts files."
1.1       deraadt   332:        cat $OUTPUT
                    333: fi
                    334:
1.14      millert   335: awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
1.1       deraadt   336: while read uid homedir; do
1.2       deraadt   337:        for j in .rhosts .shosts; do
1.13      millert   338:                if [ -s ${homedir}/$j ] ; then
1.2       deraadt   339:                        awk '{
                    340:                                if ($0 ~ /^+@.*$/ )
                    341:                                        next;
1.4       deraadt   342:                                if ($0 ~ /^\+[  ]*$/ )
1.2       deraadt   343:                                        printf("%s has + sign in it.\n",
1.13      millert   344:                                                FILENAME);
1.2       deraadt   345:                        }' ${homedir}/$j
                    346:                fi
                    347:        done
1.1       deraadt   348: done > $OUTPUT
                    349: if [ -s $OUTPUT ] ; then
1.41      millert   350:        echo "\nChecking .rhosts/.shosts files syntax."
1.1       deraadt   351:        cat $OUTPUT
                    352: fi
                    353:
                    354: # Check home directories.  Directories should not be owned by someone else
                    355: # or writeable.
1.14      millert   356: awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
1.1       deraadt   357: while read uid homedir; do
                    358:        if [ -d ${homedir}/ ] ; then
                    359:                file=`ls -ldgT ${homedir}`
1.41      millert   360:                echo "${uid} ${file}"
1.1       deraadt   361:        fi
                    362: done |
                    363: awk '$1 != $4 && $4 != "root" \
                    364:        { print "user " $1 " home directory is owned by " $4 }
                    365:      $2 ~ /^-....w/ \
1.54      henning   366:        { print "user " $1 " home directory is group writable" }
1.1       deraadt   367:      $2 ~ /^-.......w/ \
1.54      henning   368:        { print "user " $1 " home directory is other writable" }' > $OUTPUT
1.1       deraadt   369: if [ -s $OUTPUT ] ; then
1.41      millert   370:        echo "\nChecking home directories."
1.1       deraadt   371:        cat $OUTPUT
                    372: fi
                    373:
                    374: # Files that should not be owned by someone else or readable.
1.43      todd      375: list=".netrc .rhosts .gnupg/secring.gpg .gnupg/random_seed \
1.45      millert   376:        .pgp/secring.pgp .shosts .ssh/identity .ssh/id_dsa .ssh/id_rsa"
1.14      millert   377: awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
1.1       deraadt   378: while read uid homedir; do
                    379:        for f in $list ; do
                    380:                file=${homedir}/${f}
                    381:                if [ -f $file ] ; then
1.41      millert   382:                        echo "${uid} ${f} `ls -ldgT ${file}`"
1.1       deraadt   383:                fi
                    384:        done
                    385: done |
                    386: awk '$1 != $5 && $5 != "root" \
                    387:        { print "user " $1 " " $2 " file is owned by " $5 }
1.13      millert   388:      $3 ~ /^-...r/ \
                    389:        { print "user " $1 " " $2 " file is group readable" }
1.1       deraadt   390:      $3 ~ /^-......r/ \
                    391:        { print "user " $1 " " $2 " file is other readable" }
                    392:      $3 ~ /^-....w/ \
1.54      henning   393:        { print "user " $1 " " $2 " file is group writable" }
1.1       deraadt   394:      $3 ~ /^-.......w/ \
1.54      henning   395:        { print "user " $1 " " $2 " file is other writable" }' > $OUTPUT
1.1       deraadt   396:
                    397: # Files that should not be owned by someone else or writeable.
1.28      todd      398: list=".bashrc .bash_profile .bash_login .bash_logout .cshrc \
                    399:       .emacs .exrc .forward .fvwmrc .inputrc .klogin .kshrc .login \
                    400:       .logout .nexrc .profile .screenrc .ssh .ssh/config \
1.45      millert   401:       .ssh/authorized_keys .ssh/authorized_keys2 .ssh/environment \
                    402:       .ssh/known_hosts .ssh/rc .tcshrc .twmrc .xsession .xinitrc \
                    403:       .Xdefaults .Xauthority"
1.14      millert   404: awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
1.1       deraadt   405: while read uid homedir; do
                    406:        for f in $list ; do
                    407:                file=${homedir}/${f}
                    408:                if [ -f $file ] ; then
1.41      millert   409:                        echo "${uid} ${f} `ls -ldgT ${file}`"
1.1       deraadt   410:                fi
                    411:        done
                    412: done |
                    413: awk '$1 != $5 && $5 != "root" \
                    414:        { print "user " $1 " " $2 " file is owned by " $5 }
                    415:      $3 ~ /^-....w/ \
1.54      henning   416:        { print "user " $1 " " $2 " file is group writable" }
1.1       deraadt   417:      $3 ~ /^-.......w/ \
1.54      henning   418:        { print "user " $1 " " $2 " file is other writable" }' >> $OUTPUT
1.1       deraadt   419: if [ -s $OUTPUT ] ; then
1.41      millert   420:        echo "\nChecking dot files."
1.1       deraadt   421:        cat $OUTPUT
                    422: fi
                    423:
                    424: # Mailboxes should be owned by user and unreadable.
                    425: ls -l /var/mail | sed 1d | \
                    426: awk '$3 != $9 \
                    427:        { print "user " $9 " mailbox is owned by " $3 }
                    428:      $1 != "-rw-------" \
                    429:        { print "user " $9 " mailbox is " $1 ", group " $4 }' > $OUTPUT
                    430: if [ -s $OUTPUT ] ; then
1.41      millert   431:        echo "\nChecking mailbox ownership."
1.1       deraadt   432:        cat $OUTPUT
                    433: fi
                    434:
1.13      millert   435: # File systems should not be globally exported.
                    436: if [ -s /etc/exports ] ; then
                    437:        awk '{
1.19      flipk     438:                if (($1 ~ /^#/) || ($1 ~ /^$/))
1.1       deraadt   439:                        next;
1.13      millert   440:                readonly = 0;
                    441:                for (i = 2; i <= NF; ++i) {
1.19      flipk     442:                        if ($i ~ /^-ro$/)
1.13      millert   443:                                readonly = 1;
                    444:                        else if ($i !~ /^-/)
                    445:                                next;
                    446:                }
                    447:                if (readonly)
                    448:                        print "File system " $1 " globally exported, read-only."
                    449:                else
                    450:                        print "File system " $1 " globally exported, read-write."
                    451:        }' < /etc/exports > $OUTPUT
                    452:        if [ -s $OUTPUT ] ; then
1.41      millert   453:                echo "\nChecking for globally exported file systems."
1.13      millert   454:                cat $OUTPUT
                    455:        fi
1.1       deraadt   456: fi
                    457:
1.5       deraadt   458: # Display any changes in setuid/setgid files and devices.
                    459: pending="\nChecking setuid/setgid files and devices:\n"
1.1       deraadt   460: (find / \( ! -fstype local -o -fstype fdesc -o -fstype kernfs \
1.13      millert   461:        -o -fstype procfs \) -a -prune -o \
                    462:        -type f -a \( -perm -u+s -o -perm -g+s \) -print0 -o \
1.30      millert   463:        ! -type d -a ! -type f -a ! -type l -a ! -type s -a ! -type p \
                    464:        -print0 | xargs -0 ls -ldgT | sort +9 > $LIST) 2> $OUTPUT
1.1       deraadt   465:
                    466: # Display any errors that occurred during system file walk.
                    467: if [ -s $OUTPUT ] ; then
1.41      millert   468:        echo "${pending}Setuid/device find errors:"
1.2       deraadt   469:        pending=
1.1       deraadt   470:        cat $OUTPUT
1.41      millert   471:        echo ""
1.1       deraadt   472: fi
                    473:
1.12      millert   474: # Display any changes in the setuid/setgid file list.
1.66      otto      475: FIELDS1=1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,0
                    476: FIELDS2=2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,0
                    477: egrep -av '^[bc]' $LIST | join -o $FIELDS2 -110 -210 -v2 /dev/null - > $TMP1
1.1       deraadt   478: if [ -s $TMP1 ] ; then
                    479:        # Check to make sure uudecode isn't setuid.
1.66      otto      480:        if grep -aw uudecode $TMP1 > /dev/null ; then
1.41      millert   481:                echo "${pending}\nUudecode is setuid."
1.2       deraadt   482:                pending=
1.1       deraadt   483:        fi
                    484:
                    485:        CUR=/var/backups/setuid.current
                    486:        BACK=/var/backups/setuid.backup
                    487:
                    488:        if [ -s $CUR ] ; then
                    489:                if cmp -s $CUR $TMP1 ; then
                    490:                        :
                    491:                else
                    492:                        > $TMP2
1.66      otto      493:                        join -o $FIELDS2 -110 -210 -v2 $CUR $TMP1 > $OUTPUT
1.1       deraadt   494:                        if [ -s $OUTPUT ] ; then
1.41      millert   495:                                echo "${pending}Setuid additions:"
1.2       deraadt   496:                                pending=
1.66      otto      497:                                tee -a $TMP2 < $OUTPUT | column -t
1.41      millert   498:                                echo ""
1.1       deraadt   499:                        fi
                    500:
1.66      otto      501:                        join -o $FIELDS1 -110 -210 -v1 $CUR $TMP1 > $OUTPUT
1.1       deraadt   502:                        if [ -s $OUTPUT ] ; then
1.41      millert   503:                                echo "${pending}Setuid deletions:"
1.2       deraadt   504:                                pending=
1.66      otto      505:                                tee -a $TMP2 < $OUTPUT | column -t
1.41      millert   506:                                echo ""
1.1       deraadt   507:                        fi
                    508:
1.13      millert   509:                        sort +9 $TMP2 $CUR $TMP1 | \
1.1       deraadt   510:                            sed -e 's/[  ][      ]*/ /g' | uniq -u > $OUTPUT
                    511:                        if [ -s $OUTPUT ] ; then
1.41      millert   512:                                echo "${pending}Setuid changes:"
1.2       deraadt   513:                                pending=
1.1       deraadt   514:                                column -t $OUTPUT
1.41      millert   515:                                echo ""
1.1       deraadt   516:                        fi
                    517:
                    518:                        cp $CUR $BACK
                    519:                        cp $TMP1 $CUR
                    520:                fi
                    521:        else
1.41      millert   522:                echo "${pending}Setuid additions:"
1.2       deraadt   523:                pending=
1.1       deraadt   524:                column -t $TMP1
1.41      millert   525:                echo ""
1.1       deraadt   526:                cp $TMP1 $CUR
                    527:        fi
                    528: fi
                    529:
                    530: # Check for block and character disk devices that are readable or writeable
                    531: # or not owned by root.operator.
                    532: >$TMP1
1.13      millert   533: DISKLIST="ccd dk fd hd hk hp jb kra ra rb rd rl rx rz sd up vnd wd xd"
1.1       deraadt   534: for i in $DISKLIST; do
1.33      millert   535:        egrep "^b.*/${i}[0-9][0-9]*[B-H]?[a-p]$"  $LIST >> $TMP1
                    536:        egrep "^c.*/r${i}[0-9][0-9]*[B-H]?[a-p]$"  $LIST >> $TMP1
1.1       deraadt   537: done
                    538:
                    539: awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
                    540:        { printf("Disk %s is user %s, group %s, permissions %s.\n", \
                    541:            $11, $3, $4, $1); }' < $TMP1 > $OUTPUT
                    542: if [ -s $OUTPUT ] ; then
1.41      millert   543:        echo "\nChecking disk ownership and permissions."
1.1       deraadt   544:        cat $OUTPUT
1.41      millert   545:        echo ""
1.1       deraadt   546: fi
                    547:
1.66      otto      548: FIELDS1=1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.10,0
                    549: FIELDS2=2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,2.10,0
1.1       deraadt   550: # Display any changes in the device file list.
1.66      otto      551: egrep -a '^[bc]' $LIST | sort +10 | \
                    552:     join -o $FIELDS2 -111 -211 -v2 /dev/null - > $TMP1
1.1       deraadt   553: if [ -s $TMP1 ] ; then
                    554:        CUR=/var/backups/device.current
                    555:        BACK=/var/backups/device.backup
                    556:
                    557:        if [ -s $CUR ] ; then
                    558:                if cmp -s $CUR $TMP1 ; then
                    559:                        :
                    560:                else
                    561:                        > $TMP2
1.66      otto      562:                        join -o $FIELDS2 -111 -211 -v2 $CUR $TMP1 > $OUTPUT
1.1       deraadt   563:                        if [ -s $OUTPUT ] ; then
1.41      millert   564:                                echo "Device additions:"
1.66      otto      565:                                tee -a $TMP2 < $OUTPUT | column -t
1.41      millert   566:                                echo ""
1.1       deraadt   567:                        fi
                    568:
1.66      otto      569:                        join -o $FIELDS1 -111 -211 -v1 $CUR $TMP1 > $OUTPUT
1.1       deraadt   570:                        if [ -s $OUTPUT ] ; then
1.41      millert   571:                                echo "Device deletions:"
1.66      otto      572:                                tee -a $TMP2 < $OUTPUT | column -t
1.41      millert   573:                                echo ""
1.1       deraadt   574:                        fi
                    575:
                    576:                        # Report any block device change.  Ignore character
                    577:                        # devices, only the name is significant.
                    578:                        cat $TMP2 $CUR $TMP1 | \
                    579:                        sed -e '/^c/d' | \
                    580:                        sort +10 | \
                    581:                        sed -e 's/[      ][      ]*/ /g' | \
                    582:                        uniq -u > $OUTPUT
                    583:                        if [ -s $OUTPUT ] ; then
1.41      millert   584:                                echo "Block device changes:"
1.1       deraadt   585:                                column -t $OUTPUT
1.41      millert   586:                                echo ""
1.1       deraadt   587:                        fi
                    588:
                    589:                        cp $CUR $BACK
                    590:                        cp $TMP1 $CUR
                    591:                fi
                    592:        else
1.41      millert   593:                echo "Device additions:"
1.1       deraadt   594:                column -t $TMP1
1.41      millert   595:                echo ""
1.1       deraadt   596:                cp $TMP1 $CUR
                    597:        fi
                    598: fi
                    599:
                    600: # Check special files.
                    601: # Check system binaries.
                    602: #
                    603: # Create the mtree tree specifications using:
                    604: #
                    605: #      mtree -cx -pDIR -kcksum,gid,mode,nlink,size,link,time,uid > DIR.secure
1.56      millert   606: #      chown root:wheel DIR.secure
1.2       deraadt   607: #      chmod 600 DIR.secure
1.1       deraadt   608: #
                    609: # Note, this is not complete protection against Trojan horsed binaries, as
                    610: # the hacker can modify the tree specification to match the replaced binary.
                    611: # For details on really protecting yourself against modified binaries, see
                    612: # the mtree(8) manual page.
1.13      millert   613: if [ -d /etc/mtree ] ; then
1.2       deraadt   614:        cd /etc/mtree
1.49      jakob     615:        mtree -e -l -p / -f /etc/mtree/special > $OUTPUT
1.1       deraadt   616:        if [ -s $OUTPUT ] ; then
1.41      millert   617:                echo "\nChecking special files and directories."
                    618:                echo "Output format is:\n\tfilename:"
                    619:                echo "\t\tcriteria (shouldbe, reallyis)"
1.1       deraadt   620:                cat $OUTPUT
                    621:        fi
                    622:
                    623:        > $OUTPUT
                    624:        for file in *.secure; do
1.2       deraadt   625:                [ $file = '*.secure' ] && continue
1.1       deraadt   626:                tree=`sed -n -e '3s/.* //p' -e 3q $file`
                    627:                mtree -f $file -p $tree > $TMP1
1.13      millert   628:                if [ -s $TMP1 ] ; then
1.41      millert   629:                        echo "\nChecking ${tree}:" >> $OUTPUT
1.1       deraadt   630:                        cat $TMP1 >> $OUTPUT
                    631:                fi
                    632:        done
                    633:        if [ -s $OUTPUT ] ; then
1.41      millert   634:                echo "\nChecking system binaries:"
1.1       deraadt   635:                cat $OUTPUT
                    636:        fi
1.2       deraadt   637: else
                    638:        echo /etc/mtree is missing
1.1       deraadt   639: fi
                    640:
                    641: # List of files that get backed up and checked for any modifications.  Each
                    642: # file is expected to have two backups, /var/backups/file.{current,backup}.
                    643: # Any changes cause the files to rotate.
1.37      todd      644: _fnchg() {
                    645:        echo "$1" | sed 's/^\///;s/\//_/g'
                    646: }
1.1       deraadt   647: if [ -s /etc/changelist ] ; then
1.46      millert   648:        for file in `egrep -v "^(#|\+|$MP)" /etc/changelist`; do
1.37      todd      649:                CUR=/var/backups/$(_fnchg  "$file").current
                    650:                BACK=/var/backups/$(_fnchg "$file").backup
                    651:                if [ -s $file -a ! -d $file ] ; then
1.1       deraadt   652:                        if [ -s $CUR ] ; then
1.42      marc      653:                                diff -u $CUR $file > $OUTPUT
1.1       deraadt   654:                                if [ -s $OUTPUT ] ; then
1.42      marc      655:                echo "\n======\n${file} diffs (-OLD  +NEW)\n======"
1.1       deraadt   656:                                        cat $OUTPUT
                    657:                                        cp -p $CUR $BACK
                    658:                                        cp -p $file $CUR
1.56      millert   659:                                        chown root:wheel $CUR $BACK
1.1       deraadt   660:                                fi
                    661:                        else
                    662:                                cp -p $file $CUR
1.56      millert   663:                                chown root:wheel $CUR
1.46      millert   664:                        fi
                    665:                fi
                    666:        done
                    667:        for file in `egrep "^\+" /etc/changelist`; do
                    668:                file="${file#+}"
                    669:                CUR=/var/backups/$(_fnchg  "$file").current.md5
                    670:                BACK=/var/backups/$(_fnchg "$file").backup.md5
                    671:                if [ -s $file -a ! -d $file ] ; then
                    672:                        MD5_NEW=`md5 $file | sed 's/^.* //'`
                    673:                        if [ -s $CUR ] ; then
                    674:                                MD5_OLD="`cat $CUR`"
                    675:                                if [ "$MD5_NEW" != "$MD5_OLD" ]; then
                    676:                echo "\n======\n${file} MD5 checksums\n======"
                    677:                                        echo "OLD: $MD5_OLD"
                    678:                                        echo "NEW: $MD5_NEW"
                    679:                                        cp -p $CUR $BACK
                    680:                                        echo $MD5_NEW > $CUR
1.56      millert   681:                                        chown root:wheel $CUR $BACK
1.46      millert   682:                                        chmod 600 $CUR
                    683:                                fi
                    684:                        else
                    685:                                echo $MD5_NEW > $CUR
1.56      millert   686:                                chown root:wheel $CUR
1.46      millert   687:                                chmod 600 $CUR
1.1       deraadt   688:                        fi
                    689:                fi
                    690:        done
                    691: fi
1.67      millert   692:
                    693: # Make backups of the labels for any mounted disks and produce diffs
                    694: # when they change.
                    695: for d in `df -ln | sed -n 's:^/dev/\([a-z]*[0-9]*\)[a-p].*$:\1:p' | sort -u`; do
                    696:        file=/var/backups/disklabel.$d
                    697:        CUR=$file.current
                    698:        BACK=$file.backup
1.68    ! millert   699:        if disklabel $d > $file 2>&1 ; then
1.67      millert   700:                if [ -s $CUR ] ; then
                    701:                        diff -u $CUR $file > $OUTPUT
                    702:                        if [ -s $OUTPUT ] ; then
                    703:        echo "\n======\n${d} diffs (-OLD  +NEW)\n======"
                    704:                                cat $OUTPUT
                    705:                                cp -p $CUR $BACK
                    706:                                cp -p $file $CUR
                    707:                                chown root:wheel $CUR $BACK
                    708:                        fi
                    709:                else
                    710:                        cp -p $file $CUR
                    711:                        chown root:wheel $CUR
                    712:                fi
                    713:        fi
                    714:        rm -f $file
                    715: done