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

Annotation of src/etc/security, Revision 1.80

1.1       deraadt     1: #!/bin/sh -
                      2: #
1.80    ! sthen       3: #      $OpenBSD: security,v 1.79 2007/10/23 11:19:58 sthen 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 == "") &&
1.70      david      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.78      henning    72:        echo "\n${MP} has duplicate user IDs."
1.70      david      73:        while read uid; do
                     74:                grep -w $uid $TMP1
                     75:        done < $TMP2 | column
1.1       deraadt    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.75      david     139:                if egrep -aq '[[:space:]]*umask[[:space:]]' $i ; then
1.63      millert   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.70      david     168:        { print "Root path directory " $10 " is group writable." } \
1.1       deraadt   169:                     $1 ~ /^d.......w/ \
1.70      david     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.75      david     191:                if egrep -a umask $i > /dev/null ; then
1.1       deraadt   192:                        umaskset=yes
                    193:                fi
1.75      david     194:                egrep -a umask $i |
1.1       deraadt   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.70      david     221:        { print "Root path directory " $10 " is group writable." } \
1.1       deraadt   222:                     $1 ~ /^d.......w/ \
1.70      david     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
1.75      david     246:                egrep -a umask $i |
1.27      marc      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.75      david     251:                if egrep -a PATH= $i > /dev/null ; then
1.27      marc      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
1.80    ! sthen     301:
        !           302: # hostname.if files may contain secrets and should not be
        !           303: # world-readable.
        !           304:
        !           305: for f in /etc/hostname.* ; do
        !           306:        if [ "$(stat -f "%SLp" $f)" != "---" ]; then
        !           307:                echo "\n$f is world readable."
        !           308:        fi
        !           309: done
1.1       deraadt   310:
                    311: # Files that should not have + signs.
1.2       deraadt   312: list="/etc/hosts.equiv /etc/shosts.equiv /etc/hosts.lpd"
1.1       deraadt   313: for f in $list ; do
1.6       millert   314:        if [ -s $f ] ; then
1.2       deraadt   315:                awk '{
1.13      millert   316:                        if ($0 ~ /^\+@.*$/)
1.2       deraadt   317:                                next;
1.13      millert   318:                        if ($0 ~ /^\+.*$/)
1.2       deraadt   319:                                printf("\nPlus sign in %s file.\n", FILENAME);
                    320:                }' $f
1.1       deraadt   321:        fi
                    322: done
                    323:
1.13      millert   324: # Check for special users with .rhosts/.shosts files.  Only root
                    325: # should have .rhosts/.shosts files.  Also, .rhosts/.shosts
                    326: # files should not have plus signs.
1.14      millert   327: awk -F: '$1 != "root" && $1 !~ /^[+-]/ && \
1.1       deraadt   328:        ($3 < 100 || $1 == "ftp" || $1 == "uucp") \
                    329:                { print $1 " " $6 }' /etc/passwd |
                    330: while read uid homedir; do
1.2       deraadt   331:        for j in .rhosts .shosts; do
1.14      millert   332:                # Root owned .rhosts/.shosts files are ok.
1.15      millert   333:                if [ -s ${homedir}/$j -a ! -O ${homedir}/$j ] ; then
1.2       deraadt   334:                        rhost=`ls -ldgT ${homedir}/$j`
1.41      millert   335:                        echo "${uid}: ${rhost}"
1.2       deraadt   336:                fi
                    337:        done
1.1       deraadt   338: done > $OUTPUT
                    339: if [ -s $OUTPUT ] ; then
1.41      millert   340:        echo "\nChecking for special users with .rhosts/.shosts files."
1.1       deraadt   341:        cat $OUTPUT
                    342: fi
                    343:
1.14      millert   344: awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
1.1       deraadt   345: while read uid homedir; do
1.2       deraadt   346:        for j in .rhosts .shosts; do
1.13      millert   347:                if [ -s ${homedir}/$j ] ; then
1.2       deraadt   348:                        awk '{
                    349:                                if ($0 ~ /^+@.*$/ )
                    350:                                        next;
1.4       deraadt   351:                                if ($0 ~ /^\+[  ]*$/ )
1.2       deraadt   352:                                        printf("%s has + sign in it.\n",
1.13      millert   353:                                                FILENAME);
1.2       deraadt   354:                        }' ${homedir}/$j
                    355:                fi
                    356:        done
1.1       deraadt   357: done > $OUTPUT
                    358: if [ -s $OUTPUT ] ; then
1.41      millert   359:        echo "\nChecking .rhosts/.shosts files syntax."
1.1       deraadt   360:        cat $OUTPUT
                    361: fi
                    362:
                    363: # Check home directories.  Directories should not be owned by someone else
                    364: # or writeable.
1.14      millert   365: awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
1.1       deraadt   366: while read uid homedir; do
                    367:        if [ -d ${homedir}/ ] ; then
                    368:                file=`ls -ldgT ${homedir}`
1.41      millert   369:                echo "${uid} ${file}"
1.1       deraadt   370:        fi
                    371: done |
                    372: awk '$1 != $4 && $4 != "root" \
                    373:        { print "user " $1 " home directory is owned by " $4 }
                    374:      $2 ~ /^-....w/ \
1.54      henning   375:        { print "user " $1 " home directory is group writable" }
1.1       deraadt   376:      $2 ~ /^-.......w/ \
1.54      henning   377:        { print "user " $1 " home directory is other writable" }' > $OUTPUT
1.1       deraadt   378: if [ -s $OUTPUT ] ; then
1.41      millert   379:        echo "\nChecking home directories."
1.1       deraadt   380:        cat $OUTPUT
                    381: fi
                    382:
                    383: # Files that should not be owned by someone else or readable.
1.43      todd      384: list=".netrc .rhosts .gnupg/secring.gpg .gnupg/random_seed \
1.45      millert   385:        .pgp/secring.pgp .shosts .ssh/identity .ssh/id_dsa .ssh/id_rsa"
1.14      millert   386: awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
1.1       deraadt   387: while read uid homedir; do
                    388:        for f in $list ; do
                    389:                file=${homedir}/${f}
                    390:                if [ -f $file ] ; then
1.41      millert   391:                        echo "${uid} ${f} `ls -ldgT ${file}`"
1.1       deraadt   392:                fi
                    393:        done
                    394: done |
                    395: awk '$1 != $5 && $5 != "root" \
                    396:        { print "user " $1 " " $2 " file is owned by " $5 }
1.13      millert   397:      $3 ~ /^-...r/ \
                    398:        { print "user " $1 " " $2 " file is group readable" }
1.1       deraadt   399:      $3 ~ /^-......r/ \
                    400:        { print "user " $1 " " $2 " file is other readable" }
                    401:      $3 ~ /^-....w/ \
1.54      henning   402:        { print "user " $1 " " $2 " file is group writable" }
1.1       deraadt   403:      $3 ~ /^-.......w/ \
1.54      henning   404:        { print "user " $1 " " $2 " file is other writable" }' > $OUTPUT
1.1       deraadt   405:
                    406: # Files that should not be owned by someone else or writeable.
1.28      todd      407: list=".bashrc .bash_profile .bash_login .bash_logout .cshrc \
                    408:       .emacs .exrc .forward .fvwmrc .inputrc .klogin .kshrc .login \
                    409:       .logout .nexrc .profile .screenrc .ssh .ssh/config \
1.45      millert   410:       .ssh/authorized_keys .ssh/authorized_keys2 .ssh/environment \
                    411:       .ssh/known_hosts .ssh/rc .tcshrc .twmrc .xsession .xinitrc \
                    412:       .Xdefaults .Xauthority"
1.14      millert   413: awk -F: '/^[^+-]/ { print $1 " " $6 }' /etc/passwd | \
1.1       deraadt   414: while read uid homedir; do
                    415:        for f in $list ; do
                    416:                file=${homedir}/${f}
                    417:                if [ -f $file ] ; then
1.41      millert   418:                        echo "${uid} ${f} `ls -ldgT ${file}`"
1.1       deraadt   419:                fi
                    420:        done
                    421: done |
                    422: awk '$1 != $5 && $5 != "root" \
                    423:        { print "user " $1 " " $2 " file is owned by " $5 }
                    424:      $3 ~ /^-....w/ \
1.54      henning   425:        { print "user " $1 " " $2 " file is group writable" }
1.1       deraadt   426:      $3 ~ /^-.......w/ \
1.54      henning   427:        { print "user " $1 " " $2 " file is other writable" }' >> $OUTPUT
1.1       deraadt   428: if [ -s $OUTPUT ] ; then
1.41      millert   429:        echo "\nChecking dot files."
1.1       deraadt   430:        cat $OUTPUT
                    431: fi
                    432:
                    433: # Mailboxes should be owned by user and unreadable.
                    434: ls -l /var/mail | sed 1d | \
                    435: awk '$3 != $9 \
                    436:        { print "user " $9 " mailbox is owned by " $3 }
                    437:      $1 != "-rw-------" \
                    438:        { print "user " $9 " mailbox is " $1 ", group " $4 }' > $OUTPUT
                    439: if [ -s $OUTPUT ] ; then
1.41      millert   440:        echo "\nChecking mailbox ownership."
1.1       deraadt   441:        cat $OUTPUT
                    442: fi
                    443:
1.13      millert   444: # File systems should not be globally exported.
                    445: if [ -s /etc/exports ] ; then
                    446:        awk '{
1.19      flipk     447:                if (($1 ~ /^#/) || ($1 ~ /^$/))
1.1       deraadt   448:                        next;
1.13      millert   449:                readonly = 0;
                    450:                for (i = 2; i <= NF; ++i) {
1.19      flipk     451:                        if ($i ~ /^-ro$/)
1.13      millert   452:                                readonly = 1;
1.71      otto      453:                        else if ($i !~ /^-/ || $i ~ /^-network/)
1.13      millert   454:                                next;
                    455:                }
                    456:                if (readonly)
                    457:                        print "File system " $1 " globally exported, read-only."
                    458:                else
                    459:                        print "File system " $1 " globally exported, read-write."
                    460:        }' < /etc/exports > $OUTPUT
                    461:        if [ -s $OUTPUT ] ; then
1.41      millert   462:                echo "\nChecking for globally exported file systems."
1.13      millert   463:                cat $OUTPUT
                    464:        fi
1.1       deraadt   465: fi
                    466:
1.5       deraadt   467: # Display any changes in setuid/setgid files and devices.
                    468: pending="\nChecking setuid/setgid files and devices:\n"
1.74      pedro     469: (find / \( ! -fstype local \
1.72      deraadt   470:        -o -fstype procfs -o -fstype afs -o -fstype xfs \) -a -prune -o \
1.13      millert   471:        -type f -a \( -perm -u+s -o -perm -g+s \) -print0 -o \
1.30      millert   472:        ! -type d -a ! -type f -a ! -type l -a ! -type s -a ! -type p \
                    473:        -print0 | xargs -0 ls -ldgT | sort +9 > $LIST) 2> $OUTPUT
1.1       deraadt   474:
                    475: # Display any errors that occurred during system file walk.
                    476: if [ -s $OUTPUT ] ; then
1.41      millert   477:        echo "${pending}Setuid/device find errors:"
1.2       deraadt   478:        pending=
1.1       deraadt   479:        cat $OUTPUT
1.41      millert   480:        echo ""
1.1       deraadt   481: fi
                    482:
1.12      millert   483: # Display any changes in the setuid/setgid file list.
1.66      otto      484: FIELDS1=1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,0
                    485: FIELDS2=2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,0
                    486: egrep -av '^[bc]' $LIST | join -o $FIELDS2 -110 -210 -v2 /dev/null - > $TMP1
1.1       deraadt   487: if [ -s $TMP1 ] ; then
                    488:        # Check to make sure uudecode isn't setuid.
1.66      otto      489:        if grep -aw uudecode $TMP1 > /dev/null ; then
1.41      millert   490:                echo "${pending}\nUudecode is setuid."
1.2       deraadt   491:                pending=
1.1       deraadt   492:        fi
                    493:
                    494:        CUR=/var/backups/setuid.current
                    495:        BACK=/var/backups/setuid.backup
                    496:
                    497:        if [ -s $CUR ] ; then
                    498:                if cmp -s $CUR $TMP1 ; then
                    499:                        :
                    500:                else
                    501:                        > $TMP2
1.66      otto      502:                        join -o $FIELDS2 -110 -210 -v2 $CUR $TMP1 > $OUTPUT
1.1       deraadt   503:                        if [ -s $OUTPUT ] ; then
1.41      millert   504:                                echo "${pending}Setuid additions:"
1.2       deraadt   505:                                pending=
1.66      otto      506:                                tee -a $TMP2 < $OUTPUT | column -t
1.41      millert   507:                                echo ""
1.1       deraadt   508:                        fi
                    509:
1.66      otto      510:                        join -o $FIELDS1 -110 -210 -v1 $CUR $TMP1 > $OUTPUT
1.1       deraadt   511:                        if [ -s $OUTPUT ] ; then
1.41      millert   512:                                echo "${pending}Setuid deletions:"
1.2       deraadt   513:                                pending=
1.66      otto      514:                                tee -a $TMP2 < $OUTPUT | column -t
1.41      millert   515:                                echo ""
1.1       deraadt   516:                        fi
                    517:
1.13      millert   518:                        sort +9 $TMP2 $CUR $TMP1 | \
1.1       deraadt   519:                            sed -e 's/[  ][      ]*/ /g' | uniq -u > $OUTPUT
                    520:                        if [ -s $OUTPUT ] ; then
1.41      millert   521:                                echo "${pending}Setuid changes:"
1.2       deraadt   522:                                pending=
1.1       deraadt   523:                                column -t $OUTPUT
1.41      millert   524:                                echo ""
1.1       deraadt   525:                        fi
                    526:
                    527:                        cp $CUR $BACK
                    528:                        cp $TMP1 $CUR
                    529:                fi
                    530:        else
1.41      millert   531:                echo "${pending}Setuid additions:"
1.2       deraadt   532:                pending=
1.1       deraadt   533:                column -t $TMP1
1.41      millert   534:                echo ""
1.1       deraadt   535:                cp $TMP1 $CUR
                    536:        fi
                    537: fi
                    538:
                    539: # Check for block and character disk devices that are readable or writeable
                    540: # or not owned by root.operator.
                    541: >$TMP1
1.13      millert   542: DISKLIST="ccd dk fd hd hk hp jb kra ra rb rd rl rx rz sd up vnd wd xd"
1.1       deraadt   543: for i in $DISKLIST; do
1.33      millert   544:        egrep "^b.*/${i}[0-9][0-9]*[B-H]?[a-p]$"  $LIST >> $TMP1
                    545:        egrep "^c.*/r${i}[0-9][0-9]*[B-H]?[a-p]$"  $LIST >> $TMP1
1.1       deraadt   546: done
                    547:
                    548: awk '$3 != "root" || $4 != "operator" || $1 !~ /.rw-r-----/ \
                    549:        { printf("Disk %s is user %s, group %s, permissions %s.\n", \
                    550:            $11, $3, $4, $1); }' < $TMP1 > $OUTPUT
                    551: if [ -s $OUTPUT ] ; then
1.41      millert   552:        echo "\nChecking disk ownership and permissions."
1.1       deraadt   553:        cat $OUTPUT
1.41      millert   554:        echo ""
1.1       deraadt   555: fi
                    556:
1.66      otto      557: FIELDS1=1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.10,0
                    558: 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   559: # Display any changes in the device file list.
1.66      otto      560: egrep -a '^[bc]' $LIST | sort +10 | \
                    561:     join -o $FIELDS2 -111 -211 -v2 /dev/null - > $TMP1
1.1       deraadt   562: if [ -s $TMP1 ] ; then
                    563:        CUR=/var/backups/device.current
                    564:        BACK=/var/backups/device.backup
                    565:
                    566:        if [ -s $CUR ] ; then
                    567:                if cmp -s $CUR $TMP1 ; then
                    568:                        :
                    569:                else
                    570:                        > $TMP2
1.66      otto      571:                        join -o $FIELDS2 -111 -211 -v2 $CUR $TMP1 > $OUTPUT
1.1       deraadt   572:                        if [ -s $OUTPUT ] ; then
1.41      millert   573:                                echo "Device additions:"
1.66      otto      574:                                tee -a $TMP2 < $OUTPUT | column -t
1.41      millert   575:                                echo ""
1.1       deraadt   576:                        fi
                    577:
1.66      otto      578:                        join -o $FIELDS1 -111 -211 -v1 $CUR $TMP1 > $OUTPUT
1.1       deraadt   579:                        if [ -s $OUTPUT ] ; then
1.41      millert   580:                                echo "Device deletions:"
1.66      otto      581:                                tee -a $TMP2 < $OUTPUT | column -t
1.41      millert   582:                                echo ""
1.1       deraadt   583:                        fi
                    584:
                    585:                        # Report any block device change.  Ignore character
                    586:                        # devices, only the name is significant.
                    587:                        cat $TMP2 $CUR $TMP1 | \
                    588:                        sed -e '/^c/d' | \
                    589:                        sort +10 | \
                    590:                        sed -e 's/[      ][      ]*/ /g' | \
                    591:                        uniq -u > $OUTPUT
                    592:                        if [ -s $OUTPUT ] ; then
1.41      millert   593:                                echo "Block device changes:"
1.1       deraadt   594:                                column -t $OUTPUT
1.41      millert   595:                                echo ""
1.1       deraadt   596:                        fi
                    597:
                    598:                        cp $CUR $BACK
                    599:                        cp $TMP1 $CUR
                    600:                fi
                    601:        else
1.41      millert   602:                echo "Device additions:"
1.1       deraadt   603:                column -t $TMP1
1.41      millert   604:                echo ""
1.1       deraadt   605:                cp $TMP1 $CUR
                    606:        fi
                    607: fi
                    608:
                    609: # Check special files.
                    610: # Check system binaries.
                    611: #
                    612: # Create the mtree tree specifications using:
                    613: #
1.69      jmc       614: #      mtree -cx -p DIR -K md5digest,type >/etc/mtree/DIR.secure
                    615: #      chown root:wheel /etc/mtree/DIR.secure
                    616: #      chmod 600 /etc/mtree/DIR.secure
1.1       deraadt   617: #
                    618: # Note, this is not complete protection against Trojan horsed binaries, as
                    619: # the hacker can modify the tree specification to match the replaced binary.
                    620: # For details on really protecting yourself against modified binaries, see
                    621: # the mtree(8) manual page.
1.13      millert   622: if [ -d /etc/mtree ] ; then
1.2       deraadt   623:        cd /etc/mtree
1.49      jakob     624:        mtree -e -l -p / -f /etc/mtree/special > $OUTPUT
1.1       deraadt   625:        if [ -s $OUTPUT ] ; then
1.41      millert   626:                echo "\nChecking special files and directories."
                    627:                echo "Output format is:\n\tfilename:"
                    628:                echo "\t\tcriteria (shouldbe, reallyis)"
1.1       deraadt   629:                cat $OUTPUT
                    630:        fi
                    631:
                    632:        > $OUTPUT
                    633:        for file in *.secure; do
1.2       deraadt   634:                [ $file = '*.secure' ] && continue
1.1       deraadt   635:                tree=`sed -n -e '3s/.* //p' -e 3q $file`
                    636:                mtree -f $file -p $tree > $TMP1
1.13      millert   637:                if [ -s $TMP1 ] ; then
1.41      millert   638:                        echo "\nChecking ${tree}:" >> $OUTPUT
1.1       deraadt   639:                        cat $TMP1 >> $OUTPUT
                    640:                fi
                    641:        done
                    642:        if [ -s $OUTPUT ] ; then
1.41      millert   643:                echo "\nChecking system binaries:"
1.1       deraadt   644:                cat $OUTPUT
                    645:        fi
1.2       deraadt   646: else
                    647:        echo /etc/mtree is missing
1.1       deraadt   648: fi
                    649:
                    650: # List of files that get backed up and checked for any modifications.  Each
                    651: # file is expected to have two backups, /var/backups/file.{current,backup}.
                    652: # Any changes cause the files to rotate.
1.37      todd      653: _fnchg() {
                    654:        echo "$1" | sed 's/^\///;s/\//_/g'
                    655: }
1.1       deraadt   656: if [ -s /etc/changelist ] ; then
1.46      millert   657:        for file in `egrep -v "^(#|\+|$MP)" /etc/changelist`; do
1.37      todd      658:                CUR=/var/backups/$(_fnchg  "$file").current
                    659:                BACK=/var/backups/$(_fnchg "$file").backup
                    660:                if [ -s $file -a ! -d $file ] ; then
1.1       deraadt   661:                        if [ -s $CUR ] ; then
1.76      otto      662:                                diff -ua $CUR $file > $OUTPUT
1.1       deraadt   663:                                if [ -s $OUTPUT ] ; then
1.42      marc      664:                echo "\n======\n${file} diffs (-OLD  +NEW)\n======"
1.1       deraadt   665:                                        cat $OUTPUT
                    666:                                        cp -p $CUR $BACK
                    667:                                        cp -p $file $CUR
1.56      millert   668:                                        chown root:wheel $CUR $BACK
1.1       deraadt   669:                                fi
                    670:                        else
1.77      dlg       671:                echo "\n======\n${file} diffs (-OLD  +NEW)\n======"
                    672:                                diff -u /dev/null $file
1.1       deraadt   673:                                cp -p $file $CUR
1.56      millert   674:                                chown root:wheel $CUR
1.46      millert   675:                        fi
                    676:                fi
1.77      dlg       677:                if [ ! -s $file -a -s $CUR ]; then
                    678:                echo "\n======\n${file} diffs (-OLD  +NEW)\n======"
                    679:                        diff -u $CUR /dev/null
                    680:                        cp -p $CUR $BACK
                    681:                        rm -f $CUR
                    682:                        chown root:wheel $BACK
                    683:                fi
1.46      millert   684:        done
                    685:        for file in `egrep "^\+" /etc/changelist`; do
                    686:                file="${file#+}"
                    687:                CUR=/var/backups/$(_fnchg  "$file").current.md5
                    688:                BACK=/var/backups/$(_fnchg "$file").backup.md5
                    689:                if [ -s $file -a ! -d $file ] ; then
                    690:                        MD5_NEW=`md5 $file | sed 's/^.* //'`
                    691:                        if [ -s $CUR ] ; then
                    692:                                MD5_OLD="`cat $CUR`"
                    693:                                if [ "$MD5_NEW" != "$MD5_OLD" ]; then
                    694:                echo "\n======\n${file} MD5 checksums\n======"
                    695:                                        echo "OLD: $MD5_OLD"
                    696:                                        echo "NEW: $MD5_NEW"
                    697:                                        cp -p $CUR $BACK
                    698:                                        echo $MD5_NEW > $CUR
1.56      millert   699:                                        chown root:wheel $CUR $BACK
1.46      millert   700:                                        chmod 600 $CUR
                    701:                                fi
                    702:                        else
1.77      dlg       703:                        echo "\n======\n${file} new MD5 checksum\n======"
                    704:                                echo "NEW: $MD5_NEW"
1.46      millert   705:                                echo $MD5_NEW > $CUR
1.56      millert   706:                                chown root:wheel $CUR
1.46      millert   707:                                chmod 600 $CUR
1.1       deraadt   708:                        fi
1.77      dlg       709:                fi
                    710:                if [ ! -s $file -a -s $CUR ]; then
                    711:                        MD5_OLD="`cat $CUR`"
                    712:                echo "\n======\n${file} removed MD5 checksum\n======"
                    713:                        echo "OLD: $MD5_OLD"
                    714:                        cp -p $CUR $BACK
                    715:                        rm $CUR
                    716:                        chown root:wheel $BACK
1.1       deraadt   717:                fi
                    718:        done
                    719: fi
1.67      millert   720:
                    721: # Make backups of the labels for any mounted disks and produce diffs
                    722: # when they change.
                    723: for d in `df -ln | sed -n 's:^/dev/\([a-z]*[0-9]*\)[a-p].*$:\1:p' | sort -u`; do
                    724:        file=/var/backups/disklabel.$d
                    725:        CUR=$file.current
                    726:        BACK=$file.backup
1.68      millert   727:        if disklabel $d > $file 2>&1 ; then
1.67      millert   728:                if [ -s $CUR ] ; then
                    729:                        diff -u $CUR $file > $OUTPUT
                    730:                        if [ -s $OUTPUT ] ; then
                    731:        echo "\n======\n${d} diffs (-OLD  +NEW)\n======"
                    732:                                cat $OUTPUT
                    733:                                cp -p $CUR $BACK
                    734:                                cp -p $file $CUR
                    735:                                chown root:wheel $CUR $BACK
                    736:                        fi
                    737:                else
                    738:                        cp -p $file $CUR
                    739:                        chown root:wheel $CUR
                    740:                fi
                    741:        fi
                    742:        rm -f $file
                    743: done
1.79      sthen     744:
                    745: # Backup the list of installed packages and produce diffs when it changes.
                    746: file=/var/backups/pkglist
                    747: CUR=$file.current
                    748: BACK=$file.backup
                    749: if pkg_info > $file 2>&1 ; then
                    750:        if [ -s $CUR ] ; then
                    751:                diff -u $CUR $file > $OUTPUT
                    752:                if [ -s $OUTPUT ] ; then
                    753:                        echo "\n======\nInstalled package changes (-OLD  +NEW)\n======"
                    754:                        cat $OUTPUT
                    755:                        cp -p $CUR $BACK
                    756:                        cp -p $file $CUR
                    757:                        chown root:wheel $CUR $BACK
                    758:                fi
                    759:        else
                    760:                cp -p $file $CUR
                    761:                chown root:wheel $CUR
                    762:        fi
                    763: fi
                    764: rm -f $file