home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / cops_104.zip / cops_104 / root.chk < prev    next >
Text File  |  1992-03-10  |  5KB  |  175 lines

  1. :
  2. #
  3. #  Usage: root.chk
  4. #
  5. #  This shell script checks pathnames inside root's startup files for 
  6. # writability, improper umask settings (world writable), non-root
  7. # entries in /.rhosts, and to ensure that root is in /etc/ftpusers.
  8. # Also check for a single "+" in /etc/hosts.equiv (world is trusted),
  9. # and that /bin, /etc and certain key files are root owned, so that you
  10. # can't, say, rcp from a host.equived machine and blow over the password
  11. # file... this may or may not be bad, decide for yourself.
  12. # Startup files are /.login /.cshrc /.profile
  13. #
  14. #  Mechanism:  These files contain paths and filenames that are stripped
  15. # out using "grep".  These strings are then processed by the "is_able"
  16. # program to see if they are world writable.  Strings of the form:
  17. #
  18. #    path=(/bin /usr/bin .)
  19. #        and
  20. #    PATH=/bin:/usr/bin:.:
  21. #
  22. # are checked (using grep) to ensure that "." is not in the path.  All
  23. # results are echoed to standard output.  In addition, some effort was
  24. # put into parsing out paths with multiple lines; e.g. ending in "\",
  25. # and continuing on the next line.
  26. #  For umask stuff, simply grep for umask in startup files, and check
  27. # umask value.  For /etc/ftpuser, simple grep to check if root is in
  28. # the file.  For /etc/hosts.equiv, just check to see if "+" is alone
  29. # on a line by awking it.
  30. #
  31. #
  32. AWK=/bin/awk
  33. SED=/bin/sed
  34. TEST=/bin/test
  35. ECHO=/bin/echo
  36. GREP=/bin/grep
  37. SORT=/usr/bin/sort
  38. EXPR=/bin/expr
  39. LS=/bin/ls
  40.  
  41. # root startup/important files
  42. csh=/.cshrc
  43. sh=/.profile
  44. rhosts=/.rhosts
  45. big_files="/.login /.cshrc /.profile"
  46.  
  47. # root should own *at least* these, + $big_files; you can check for all files
  48. # in /bin & /etc, or just the directories (the default.)
  49. # root_files="/bin /bin/* /etc /etc/* $big_files $rhosts"
  50. root_files="/dev /usr/etc /bin /etc $big_files $rhosts /etc/passwd /etc/group"
  51.  
  52. # misc important stuff
  53. ftp=/etc/ftpusers
  54. equiv=/etc/hosts.equiv
  55.  
  56. #   should't have anyone but root owning /bin or /etc files/directories
  57. # In case some of the critical files don't exist (/.rhost), toss away error
  58. # messages
  59. non_root=`$LS -ld $root_files | $AWK '{if ($3 != "root") print $NF}'`
  60. if $TEST -n "$non_root" ; then
  61.     $ECHO "Warning!  Root does not own the following file(s):"
  62.     $ECHO $non_root
  63.     fi
  64.  
  65. # parse into separate paths:
  66. for i in $big_files
  67.     do
  68.     if $TEST -s $i
  69.         then
  70.         ./chk_strings $i
  71.  
  72.         # check for umask stuff (thanks to Bruce Spence):
  73.         if umsk=`$GREP umask $i ` 2>/dev/null
  74.             then
  75.             mask=`$ECHO $umsk|$AWK '{if($2!=""){if(length($2)==1) print "00"$2; \
  76.                 else if (length($2)==2) print "0"$2; \
  77.                 else print $2} else print "000"}'`
  78. #            perm=`$EXPR substr $mask 3 1`
  79.             perm=`$ECHO $mask | $SED 's/[0-9][0-9]//'`
  80.             if $TEST "$perm" -lt 2 -o "$perm" = 4
  81.                 then
  82.                 if $TEST "$umsk"
  83.                     then
  84.                     $ECHO "Warning!  Root's umask set to $umsk in $i"
  85.                     fi
  86.                 fi
  87.             fi
  88.         fi
  89.     done
  90.  
  91. # check to see if root is in ftpusers file
  92. if $TEST -s $ftp
  93.     then
  94.     if $TEST ! "`$GREP "root" $ftp`"
  95.         then
  96.         $ECHO Warning!  $ftp exists and root is not in it
  97.         fi
  98.     fi
  99.  
  100. # check for a "+" in hosts.equiv.  Bad.  Bad dog.
  101. if $TEST -f $equiv ; then
  102.     $AWK '{if (NF==1 && $1=="+") printf("Warning!  A \"+\" entry in %s!\n", "'$equiv'")}' $equiv
  103.     fi
  104.  
  105. # check for non-root entries in /.rhosts
  106. #$AWK '{if ((NF==1&&!($1=="localhost" || $1=="root"))||(NR!=1&&$2!="root")) printf("Warning!  Non root entry in %s! %s\n", $rhosts, $0)}' $rhosts
  107.  
  108.  
  109. # checking paths...
  110. #
  111. #  For both the .profile and .cshrc, the methods are similar.  Awk for
  112. # lines with "path" or "PATH", rip out the guts, then check with is_writable 
  113. # Trying to pull out the multi line stuff was a pain...  no thanks to
  114. # Jay Batson for telling me this was broken :-)
  115. #
  116. {
  117. #
  118. # Get the root paths from $csh.
  119. if $TEST -f $csh; then
  120.     $AWK '{foo=substr($NF,1,length($NF)); \
  121.     if (bar && foo=="\\" )
  122.         foobar[i++] = $0; \
  123.     if (bar && foo==")") {
  124.         bar = 0; \
  125.         foobar[i++] = $0;}}
  126. /path/    { foobar[i++] = $0; \
  127.     foo=substr($NF,1,length($NF)); \
  128.     if (foo=="\\" )
  129.         bar = NR \
  130.     }
  131. END { for (j=0; j<=i; j++)
  132.     print foobar[j] } ' $csh |
  133.     $SED -e 's/#.*$//' -e 's/(//' -e 's/)//' -e 's/.*=//' |
  134.       $AWK '{for (i=1;i<=NF;i++) print $i}'
  135. fi
  136.  
  137. #
  138. # Get the root paths from $sh.
  139. if $TEST -f $sh; then
  140.     $AWK -F: '{foo=substr($NF,1,length($NF)); \
  141.     if (bar && foo=="\\" )
  142.         foobar[i++] = $0; \
  143.     if (bar) {
  144.         bar = 0; \
  145.         foobar[i++] = $0;}}
  146.     /PATH/    { foobar[i++] = $0; \
  147.         foo=substr($NF,1,length($NF)); \
  148.         if (foo=="\\" )
  149.             bar = NR \
  150.         }
  151.     END { for (j=0; j<=i; j++)
  152.         print foobar[j] }' $sh |
  153.     $SED -e 's/#.*$//' -e 's/^export.*$//' -e 's/PATH=//' -e 's/;.*$//' |
  154.      $AWK '{ split($0,temp,":"); for (i in temp) \
  155.              if (temp[i] == "") print "."; \
  156.              else print temp[i]}'
  157. fi
  158. } |
  159.  $SORT -u |
  160.   while read i
  161.   do
  162.     # check to see if "." is in path
  163.     if $TEST "." = "$i"
  164.     then
  165.         $ECHO "Warning!  \".\" (or current directory) is in roots path!"
  166.     fi
  167.  
  168.     if ./is_writable $i
  169.     then
  170.         $ECHO "Warning!  Directory $i is _World_ writable and in roots path!"
  171.     fi
  172.   done
  173.  
  174. # end of script
  175.