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

  1. #!/bin/sh -- need to mention perl here to avoid recursion
  2. 'true' || eval 'exec perl -S $0 $argv:q';
  3. eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
  4. & eval 'exec /usr/bin/perl -S $0 $argv:q'
  5.         if 0;
  6.  
  7. #  Usage: ftp.chk
  8. #
  9. #   This shell script checks to see if you've set up (mainly anonymous)
  10. # ftp correctly.  There seems to be some different types of ftp's 
  11. # around; for instance, some allow "chmod" -- and if the home dir is 
  12. # 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. #  ~ftp/bin/ls       111             root/ftp
  36. #  ~ftp/etc          non-w.w.        root/ftp
  37. #  ~ftp/etc/passwd   non-w.w.        root/ftp   0 size or nonexistant
  38. #  ~ftp/etc/group    non-w.w.        root/ftp   0 size or nonexistant
  39. #  ~ftp/pub          non-w.w.        root/ftp
  40. #  ~ftp/incoming     world-writable  root/ftp   This can be set to "pub"
  41. #  ~ftp/.rhosts      non-w.w.        root       0 size, is optional
  42. #  ~ftp/*            non-w.w.                   other dirs/files in ~ftp
  43. #
  44. #
  45.  
  46. require 'is_able.pl';
  47. require 'file_mode.pl';
  48. require 'glob.pl';
  49. require 'fgrep.pl';
  50. require 'pass.cache.pl';
  51. require 'file_owner.pl';
  52. require 'pathconf.pl';
  53.  
  54. $CMP="/bin/cmp" unless defined $CMP;
  55.  
  56. package ftp;
  57.  
  58. #   Primary and secondary owners of the ftp files/dirs; if you *don't* have
  59. # chmod, you can probably change the secondary owner to "ftp".  If you have
  60. # chmod in your ftp, definitely have secondary to some other account (root
  61. # is fine for this.)
  62. $primary = "root" unless defined $primary;
  63. $secondary = "ftp" unless defined $secondary;
  64.  
  65. # some might have this as ftpd; is the account in /etc/passwd
  66. $ftpuid = "ftp";
  67.  
  68. # system files
  69. $ftpusers = "/etc/ftpusers";
  70. $passwd = $'PASSWD || "/etc/passwd";
  71. $group = $'GROUP || "/etc/group";
  72.  
  73. #   ftp's home:
  74. $ftproot = &'uname2dir($ftpuid);
  75. $anonymous = $ftproot ne '';
  76.  
  77. $ftprhosts = "$ftproot/.rhosts";
  78. $ftpbin = "$ftproot/bin";
  79. $ftpls = "$ftpbin/ls";
  80. $ftpetc = "$ftproot/etc";
  81. $ftppasswd = "$ftpetc/passwd";
  82. $ftpgroup = "$ftpetc/group";
  83.  
  84. $W = 'Warning!  ' unless defined $W;
  85.  
  86. #   the pub/incoming stuff; by default, pub is *not* world writable, incoming
  87. # is; if you want pub to be world writable, just change incoming to "pub"
  88. $incoming = "pub";
  89.  
  90. @crit_files=($ftpgroup,
  91.          $ftppasswd,
  92.          $ftpls);
  93.  
  94. if (-s $ftpusers) {
  95.     # check to see if root (or root equivalents) is in ftpusers file
  96.     @all_roots = split(" ", $'uid2names{0});
  97.     for $i (@all_roots) {
  98.     if (length($user2passwd{$i}) == 13 && ! &'fgrep($ftpusers, "^$i$")) {
  99.         print "Warning!  $i should be in $ftpusers!\n";
  100.     }
  101.     }
  102. }
  103.  
  104. #  do the anonymous ftp checking stuff now?
  105. die unless $anonymous;
  106.  
  107. #   if the user ftp doesn't exist, no-anon stuff....
  108. # if $TEST -z $ftproot -a "$anonymous" = "yes" ; then
  109.  
  110. die "${W}Need user $ftpuid for anonymous ftp to work!\n" if ($ftpuid eq "");
  111.  
  112. #   if the user ftp doesn't exist, no-anon stuff....
  113. if (! -d $ftproot || $ftproot eq "") {
  114.     die "${W}Home directory for ftp doesn\'t exist!\n";
  115. }
  116. if ($ftproot eq "/") {
  117.     print qq:${W}$ftproot ftp's home directory should not be "/"!\n:;
  118. }
  119.  
  120. #   want to check all the critical files and directories for correct
  121. # ownership.  Some versions of ftp don't need much of anything... no 
  122. # etc directory or password/group files.
  123. #   others need etc directory & password/group files.  Experiment.
  124. #
  125. push(@crit_files, $ftpbin, $ftpetc);
  126. for $i (@crit_files) {
  127.     $owner = &'Owner($i);
  128.  
  129.     if ($owner eq 'BOGUS') {
  130.     print "${W}Critical anon-ftp file $i is missing!\n";
  131.     next;
  132.     }
  133.  
  134.     $owner = $'uid2names{$owner};
  135.  
  136.     if ($owner !~ /\b$primary\b|\b$secondary\b/) {
  137.        print "${W}$i should be owned by $primary or $secondary, not $owner!\n";
  138.     }
  139. }
  140.  
  141. #  Don't want the passwd and group files to be the real ones!
  142. if (&'Owner($ftppasswd) ne 'BOGUS' &&
  143.     $passwd ne $ftppasswd && 
  144.     ! system "$CMP -s $passwd $ftppasswd") 
  145. {
  146.     print "${W}$ftppasswd and $passwd are the same!\n";
  147. }
  148.  
  149. if (&'Owner($ftpgroup) ne 'BOGUS' &&
  150.     $group ne $ftpgroup && 
  151.     ! system "$CMP -s $passwd $ftpgroup") 
  152. {
  153.     print "${W}$ftpgroup and $group are the same!\n";
  154. }
  155.  
  156. #   ftproot is special; if owned by root; should be !world writable;
  157. # if owned by ftp, should be mode 555
  158.  
  159. if (&'Owner($ftproot) ne 'BOGUS') {
  160.     $owner = $'uid2names{&'Owner($ftproot)};
  161.     $perms=&'Mode($ftproot);
  162.     if ($owner !~ /\b$primary\b|\b$secondary\b/) {
  163.     print "${W}$ftproot should be owned by $primary or $secondary, not $owner!\n";
  164.     }
  165.  
  166.     # ftp-root should not be world-writable:
  167.     &'is_able($ftproot, "w", "w");
  168.  
  169.     # if ftp owns root-dir, then mode should be 555:
  170.     if ($owner eq $ftpuid && $perms ne 00555) {
  171.     print "${W}$ftproot should be mode 555!\n";
  172.     }
  173. }
  174.  
  175. #
  176. # check the .rhosts file:
  177. if (-f $ftprhosts) {
  178.     if (-s $ftprhosts) {
  179.     print "${W}$ftprhosts should be be empty!\n";
  180.     }
  181.     $owner=$'uid2names{&'Owner($ftprhosts)};
  182.     if ($owner ne $primary && $owner ne $secondary) {
  183.     print "${W}$ftprhosts should be owned by $primary or $secondary!\n";
  184.     }
  185. }
  186.  
  187. # finally, some permissions of miscellaneous files:
  188. if (($perms=&'Mode($ftpls)) & 0666) {
  189.     printf "${W}Incorrect permissions (%04o) on $ftpls!\n", $perms;
  190. }
  191.  
  192. if (($perms=&'Mode($ftppasswd)) & 0333) {
  193.     printf "${W}Incorrect permissions (%04o) on $ftppasswd!\n", $perms;
  194. }
  195.  
  196.  
  197. if (($perms=&'Mode($ftpgroup)) & 0333) {
  198.     printf "${W}Incorrect permissions (%04o) on $ftpgroup!\n", $perms;
  199. }
  200.  
  201. #   Finally, the ~ftp/{pub|incoming|whatever} stuff:
  202. opendir(FTPDIR, $ftproot) || die "can't opendir $ftproot: $!\n";
  203.  
  204. @all_dirs=grep(-d, readdir(FTPDIR));
  205.  
  206. local($is_able'silent) = 1;  
  207. for $i (@all_dirs) {
  208.     if ($i ne $incoming && &'is_able($ftproot . "/$i", "w", "w")) {
  209.     print "${W}Anon-ftp directory $i is World Writable!\n";
  210.     }
  211. }
  212.  
  213. 1;
  214. # end of script
  215.