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 / rc.chk < prev    next >
Text File  |  1992-03-10  |  3KB  |  126 lines

  1. :
  2. #
  3. #  Usage: rc.chk
  4. #
  5. #  This checks pathnames and files inside the shell script files /etc/rc*
  6. # for writability.
  7. #
  8. #  Mechanism:  The commands inside the files /etc/rc* are executed when
  9. # the machine is booted.  This shell script greps for commands/paths that
  10. # are of these forms:
  11. #
  12. #    /path/command            # or whatever
  13. #        or
  14. #    PATH=:/bin:/usr/bin:.        # or whatever
  15. #        or
  16. #    MYVAR=`/path/command`        # or whatever
  17. #    
  18. #  It then takes each potential problem-string and uses the program
  19. # "is_writable" to determine if it is world writable.  All results are
  20. # echoed to standard output.
  21. #
  22. # 12 Apr 90, Mark Plumbly made it ignore lines starting with rm -f
  23. # (popular in rc files) and fixed my code so it would ignore everything
  24. # after a ">".
  25. #
  26. SED=/bin/sed
  27. CAT=/bin/cat
  28. RM=/bin/rm
  29. AWK=/bin/awk
  30. LS=/bin/ls
  31. TEST=/bin/test
  32. EGREP=/usr/bin/egrep
  33. ECHO=/bin/echo
  34. SORT=/usr/bin/sort
  35. FIND=/bin/find
  36.  
  37. # temp file for stuff:
  38. FOO_RC="./rc.foo.$$"
  39. FOO_RC2="./rc.foo2.$$"
  40.  
  41. # CHANGE THIS LINE OR PUT IN FILE NAMES IF/AS NEEDED!
  42. #    (for example: init_files="/etc/rc /etc/rc.local")
  43. #
  44. # init_files=`$LS /etc/*rc /etc/rc* /etc/rc*.d/* /etc/shutdown.d/* /etc/inittab | $SORT -u`
  45.  
  46. potential_files="/etc/*rc /etc/rc*"
  47. if $TEST -d /etc/shutdown.d ; then
  48.     potential_files=$potential_files" /etc/shutdown.d"
  49.     fi
  50. if $TEST -f /etc/inittab ; then
  51.     potential_files=$potential_files" /etc/inittab"
  52.     fi
  53. init_files=`$FIND $potential_files -print | $SORT -u`
  54.  
  55. #
  56. #  This should get all paths in /etc/rc* files; at least two types here.
  57. # First type starts with a "/", the second is either in the form :
  58. #
  59. #    PATH=:/bin:/usr/bin:.        # or whatever
  60. # or
  61. #    MYVAR=`/bin/echo "hello"`    # or whatever
  62. #
  63. #   Notice also I strip out any references to /tmp, /usr/tmp,
  64. # /dev/*ty's, and /dev/null.
  65. #
  66. # 12 Apr mdp:     Modified to remove "> file" as well as ">file"
  67. #        and remove "rm -f file" (this removes a few bogus ones).
  68. #        (i.e. things which are written to or removed only are ignored).
  69. #
  70.  
  71. #  You can try this, or use the old method...
  72. # for file in $init_files
  73. #      do
  74. #     if $TEST -s $file ; then
  75. #          ./chk_strings $file
  76. #         fi
  77. #       done
  78. # exit
  79.  
  80. for file in $init_files
  81.     do
  82.     if $TEST -f "$file" ; then
  83.         $AWK '{ if (substr($1,1,1)== "#") next; \
  84.         for (i=1;i<=NF;i++) \
  85.             { first=substr($i,1,1);        \
  86.                 if (first==">"||first=="#"||first=="$")    \
  87.                     break;            \
  88.                 else if ($i == "rm")        \
  89.                     break;            \
  90.                 else if (first == "/")        \
  91.                     print "\"'$file'\"", $i;\
  92.                 }                \
  93.             }' $file |
  94.         $SED -e s/\"//g -e s/\'//g -e s/\`//g -e s/\;// |
  95.         $EGREP -v "/dev/.*ty|/tmp|/usr/tmp|/dev/null"
  96.         fi
  97.         done | sort -u >> $FOO_RC2
  98.  
  99. #
  100. #  Ok -- $FOO_RC has a format like thus:
  101. # /etc/rc.local /bin/foofile
  102. #
  103. #  We want to kill off all dups in the second field:
  104. $AWK '{dup[$2] = $1}
  105.     END { for (i in dup) print dup[i], i;}' $FOO_RC2 | $SORT > $FOO_RC
  106.  
  107. #  First, get the ones starting with "/":
  108. #
  109. #   DANGER!  DANGER!  DANGER Will Robinson! Awk runs out of room ("bails
  110. # out") if too many files are here....
  111. # for i in `$CAT $FOO_RC`
  112. cat $FOO_RC | while read i
  113.     do
  114.     target=`$ECHO $i | $SED 's/.* //'`
  115.     if $TEST -f "$target" ; then
  116.         blame=`$ECHO $i | $SED 's/ .*$//'`
  117.         if ./is_writable $target
  118.             then
  119.             $ECHO "Warning!  File $target (in $blame) is _World_ writable!"
  120.             fi
  121.         fi
  122.     done
  123.  
  124. $RM -f $FOO_RC $FOO_RC2
  125. # end of script
  126.