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 / ftp.chk < prev    next >
Text File  |  1992-03-10  |  8KB  |  283 lines

  1. :
  2. #
  3. #  Usage: ftp.chk [-a]
  4. #
  5. #   This shell script checks to see if you've set up (mainly anonymous)
  6. # ftp correctly.  The "-a" option forces a check on your anon-ftp setup
  7. # (without the flag, this will look in your /etc/passwd, to see if user
  8. # ftp exists, and proceed onwards anyway) without that, this script 
  9. # doesn't do a whole lot -- just check to see if your ftpusers file 
  10. # doesn't have any root accounts in it.  There seems to be some different
  11. # types of ftp's around; for instance, some allow "chmod" -- and if the home
  12. # dir is owned by "ftp", you're toast.  So I've tried to err on the side of
  13. # safety...
  14. #
  15. #   See the man page for a more detailed description, here's what this
  16. # checks for:
  17. #
  18. # - User ftp exists in the password file.
  19. # - root (or all root equivalents) are in ftpusers file.
  20. # - Home directory for ftp should exist, and not be /
  21. # - The ~ftp/etc/{passwd|group} should not be the same as the real ones.
  22. # - Various critical files/directories should exist, and have correct
  23. #   permissions and owners; variables "$primary" and "$secondary" can be set
  24. # to whomever you want owning the files:
  25. #
  26. #  File/Dir          Perms           Owner      Other
  27. #  =========         ======          ======     ======
  28. #  ~ftp              non-w.w.        root
  29. #           or
  30. #  ~ftp              555             ftp    if no chmod command exists
  31. #
  32. #     All of these are ftp owned iff no chmod exists...
  33. #
  34. #  ~ftp/bin          non-w.w.        root/ftp
  35. #           or
  36. #  ~ftp/bin          non-w. and ftp w. ftp
  37. #  ~ftp/bin/ls       111             root/ftp
  38. #  ~ftp/etc          non-w.w.        root
  39. #           or
  40. #  ~ftp/etc          non-w. & ftp w. ftp
  41. #  ~ftp/etc/passwd   non-w.w.        root/ftp   0 size or nonexistant
  42. #  ~ftp/etc/group    non-w.w.        root/ftp   0 size or nonexistant
  43. #  ~ftp/pub          non-w.w.        root/ftp
  44. #  ~ftp/incoming     world-writable  root/ftp   This can be set to "pub"
  45. #  ~ftp/.rhosts      non-w.w.        root       0 size, is optional
  46. #  ~ftp/*            non-w.w.                   other dirs/files in ~ftp
  47. #
  48.  
  49. #  If an argument is present, it should be an "a"
  50. TEST=/bin/test
  51. ECHO=/bin/echo
  52. if $TEST $# -gt 1 ; then
  53.     $ECHO Usage: $0 [-a]
  54.     exit 1
  55.     fi
  56. if $TEST $# -eq 1 ; then
  57.     if $TEST $1 = "-a" ; then
  58.             anonymous=yes
  59.     else
  60.         $ECHO Usage: $0 [-a]
  61.         exit 1
  62.         fi
  63.     fi
  64.  
  65. #   Primary and secondary owners of the ftp files/dirs; if you *don't* have
  66. # chmod, you can probably change the secondary owner to "ftp".  If you have
  67. # chmod in your ftp, definitely have secondary to some other account (root
  68. # is fine for this.)
  69. primary=root
  70. secondary=root
  71.  
  72. # some might have this as ftpd; is the account in /etc/passwd
  73. ftpuid=ftp
  74.  
  75. # Where is everyone?
  76. AWK=/bin/awk
  77. EGREP=/usr/bin/egrep
  78. LS=/bin/ls
  79. CMP=/bin/cmp
  80. RM=/bin/rm
  81. YPCAT=/usr/bin/ypcat
  82. CAT=/bin/cat
  83.  
  84. # system files
  85. ftpusers=/etc/ftpusers
  86. passwd=/etc/passwd
  87. group=/etc/group
  88.  
  89. #  A pox on YP/NIS, making life tougher for me :-)  Thanks to Rob Kolstad
  90. # for pointing this out -- you need to use ypcat to get the password file,
  91. # if you run yp:
  92.  
  93. # Scratch files for testing:
  94. yp_passwd="./p.$$"
  95. yp_group="./g.$$"
  96. all_passwds="./ap.$$"
  97.  
  98. # generic test to check for yp use?
  99. if $TEST -s $YPCAT ; then
  100.     $YPCAT passwd > $yp_passwd
  101.     if $TEST $? -eq 0 ; then
  102.         $YPCAT group > $yp_group
  103.         yp=true
  104.     else
  105.         yp=false
  106.         fi
  107.     fi
  108.  
  109. if $TEST "$yp" = "true" ; then
  110.     $CAT $yp_passwd $passwd > $all_passwds
  111.     passwd=$yp_passwd
  112.     group=$yp_group
  113. else
  114.     $CAT $passwd > $all_passwds
  115.     fi
  116.  
  117. #   ftp's files:
  118. ftproot=`$AWK -F: '/^'"$ftpuid"':/{print $6}' $passwd`
  119. #  just recheck that user ftp exists:
  120. ftpuid=`$AWK -F: '/^'"$ftpuid"':/{print $1}' $passwd`
  121.  
  122. #
  123. # If they have user $ftpuid in /etc/password, then anon-ftp is possible...
  124. #
  125. # Comment this (next three lines) out if you don't want this program to
  126. # automatically detect anon-ftp setup!
  127. if $TEST -n "$ftpuid" ; then
  128.     anonymous=yes
  129.     fi
  130.  
  131. ftprhosts=$ftproot/.rhosts
  132. ftpbin=$ftproot"/bin"
  133. ftpls=$ftpbin"/ls"
  134. ftpetc=$ftproot"/etc"
  135. ftppasswd=$ftpetc"/passwd"
  136. ftpgroup=$ftpetc"/group"
  137.  
  138. #   the pub/incoming stuff; by default, pub is *not* world writable, incoming
  139. # is; if you want pub to be world writable, just change incoming to "pub"
  140. incoming=incoming
  141. ftppub=$ftproot"/pub"
  142.  
  143. crit_files="$ftpgroup $ftppasswd $ftpls"
  144.  
  145. if $TEST -s "$ftpusers" ; then
  146.     # check to see if root (or root equivalents) is in ftpusers file
  147.     all_roots=`$AWK -F: '{if ($3==0 && length($2)==13) printf("%s ", $1)}' $all_passwds`
  148.     if $TEST -n "$all_roots" ; then
  149.         for i in $all_roots
  150.             do
  151.             if $TEST ! "`$EGREP '^'"$i"'$' $ftpusers`"
  152.                 then
  153.                 $ECHO Warning!  $i should be in $ftpusers!
  154.                 fi
  155.             done
  156.         fi
  157. else
  158.     $ECHO "Warning!  $ftpusers should exist!"
  159.     fi
  160.  
  161. #  do the anonymous ftp checking stuff now
  162. if $TEST -n "$anonymous" ; then
  163.  
  164.     #   if the user ftp doesn't exist, no-anon stuff....
  165.     if $TEST -z "$ftpuid" ; then
  166.         $ECHO Warning!  Need user $ftpuid for anonymous ftp to work!
  167.         $RM -f $yp_passwd $yp_group $all_passwds
  168.         exit 1
  169.         fi
  170.     #
  171.     #  ftp's home dir checking
  172.     if $TEST ! -d "$ftproot" -o -z "$ftproot"; then
  173.         $ECHO Warning!  Home directory for ftp doesn\'t exist!
  174.         $RM -f $yp_passwd $yp_group $all_passwds
  175.         exit 1
  176.         fi
  177.     if $TEST "$ftproot" = "/" ; then
  178.         $ECHO Warning!  $ftproot ftp\'s home directory should not be \"/\"!
  179.         fi
  180.     #
  181.     #  Don't want the passwd and group files to be the real ones!
  182.     if $TEST "$passwd" != "$ftppasswd" ; then
  183.         if $TEST "`$CMP $passwd $ftppasswd 2> /dev/null`" ; then
  184.             :
  185.         else $ECHO ftp-Warning!  $ftppasswd and $passwd are the same!
  186.             fi
  187.         fi
  188.     if $TEST "$group" != "$ftpgroup" ; then
  189.         if $TEST "`$CMP $group $ftpgroup 2> /dev/null`" ; then
  190.             :
  191.         else $ECHO ftp-Warning!  $ftpgroup and $group are the same!
  192.             fi
  193.         fi
  194.  
  195.     #   want to check all the critical files and directories for correct
  196.     # ownership.
  197.     #
  198.     #  This is what a "/bin/ls -l" of a file should look like:
  199.     # ---x--x--x  1 root        81920 Dec 31  1999 /bin/ls
  200.     #  So in awk, $3 is the owner, $1 is the permission.
  201.     #
  202.     #   some versions don't need much of anything... no etc directory or
  203.     # password/group files.
  204.     # crit_files=$ftpls
  205.     #   others need etc directory & password/group files.  Experiment.
  206.     crit_files=$crit_files" "$ftpbin" "$ftpetc
  207.     for i in $crit_files
  208.         do
  209.         if $TEST ! -f $i -a ! -d $i; then
  210.             $ECHO "ftp-Warning!  File $i is missing (anon-ftp setup)!"
  211.             fi
  212.  
  213.         owner=`$LS -Lld $i | $AWK '{print $3}'`
  214.         if $TEST "$owner" = "$primary" -o "$owner" = "$secondary" ; then
  215.             :
  216.         else
  217.             $ECHO ftp-Warning!  $i should be owned by $primary or $secondary!
  218.             fi
  219.         done
  220.  
  221.     #   ftproot is special; if owned by root; should be !world writable;
  222.     # if owned by ftp, should be mode 555
  223.     owner=`$LS -Lld $ftproot | $AWK '{print $3}'`
  224.     perms=`$LS -Lld $ftproot | $AWK '{print $1}'`
  225.     if $TEST "$owner" = "$primary" -o "$owner" = "$secondary" ; then
  226.         :
  227.     else
  228.         $ECHO ftp-Warning!  $ftproot should be owned by $primary or $secondary!
  229.     fi
  230.  
  231.     # ftp-root should not be world-writable:
  232.     ./is_able $ftproot w w
  233.  
  234.     # if ftp owns root-dir, then mode should be 555:
  235.     if $TEST "$owner" = "$ftpuid" -a "$perms" != "dr-xr-xr-x" ; then
  236.         $ECHO ftp-Warning!  $ftproot should be mode 555!
  237.         fi
  238.  
  239.     #
  240.     # check the .rhosts file:
  241.     if $TEST -f $ftprhosts ; then
  242.         if $TEST -s $ftprhosts ; then
  243.             $ECHO ftp-Warning!  $ftprhosts should be be empty!
  244.             fi
  245.         owner=`$LS -Lld $ftprhosts | $AWK '{print $3}'`
  246.         if $TEST "$owner" = "$primary" -o "$owner" = "$secondary" ; then
  247.             :
  248.         else
  249.             $ECHO ftp-Warning!  $ftprhosts should be owned by $primary or $secondary!
  250.             fi
  251.         fi
  252.  
  253.     #
  254.     # finally, some permissions of miscellaneous files:
  255.     perms=`$LS -Lld $ftpls | $AWK '{print $1}'`
  256.     if $TEST "$perms" != "---x--x--x" ; then
  257.         $ECHO ftp-Warning!  Incorrect permissions on \"ls\" in $ftpbin!
  258.         fi
  259.  
  260.     perms=`$LS -Lld $ftppasswd | $AWK '{print $1}'`
  261.     if $TEST "$perms" != "-r--r--r--" ; then
  262.         $ECHO ftp-Warning!  Incorrect permissions on \"passwd\" in $ftpetc!
  263.         fi
  264.  
  265.     perms=`$LS -Lld $ftpgroup | $AWK '{print $1}'`
  266.     if $TEST "$perms" != "-r--r--r--" ; then
  267.         $ECHO ftp-Warning!  Incorrect permissions on \"group\" in $ftpetc!
  268.         fi
  269.  
  270.     #   Finally, the ~ftp/{pub|incoming|whatever} stuff:
  271.     all_dirs=`$LS -Lal $ftproot | $AWK '{if (NF >= 8) print $NF}'`
  272.     for i in $all_dirs
  273.         do
  274.         if $TEST -n "`is_able $ftproot/$i w w`" -a $i != "$incoming" ; then
  275.             $ECHO Warning!  Anon-ftp directory $i is World Writable!
  276.             fi
  277.         done
  278.     fi
  279.  
  280. # get rid of any yp evidence
  281. $RM -f $yp_passwd $yp_group $all_passwds
  282. # end of script
  283.