home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2002 November / SGI IRIX 6.5 Applications 2002 November.iso / dist / gateway.idb / usr / WebFace / Source / 20-NetworkServices / ftp / ftp-anonymous.frm.z / ftp-anonymous.frm
Encoding:
Text File  |  2002-06-12  |  8.1 KB  |  300 lines

  1. #!/usr/bin/perl5
  2. #
  3. # ftp-anonymous.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: ftp-anonymous.frm,v 1.41 1997/11/17 19:06:16 shotes Exp $
  21.  
  22. # flow chart on form validation:
  23. #
  24. # if (ftp user does not already exist) 
  25. #   if (home directory already exists) -> error
  26. #   else -> create directory and ftp user
  27. # else
  28. #   if (home directory exists and owned by ftp) -> check for nec. files
  29. #   elsif (home dir. exists and not owned by ftp) -> error
  30. #   else -> create home dir. and add nec. files
  31.  
  32. BEGIN { require "/usr/WebFace/lib/CGI.pm"; import CGI; }
  33. require "/usr/OnRamp/lib/OnRamp.pm";
  34. require "/usr/OnRamp/lib/java.pm";
  35.  
  36. $query = new CGI;
  37.  
  38. $help_page = "ftp-anonymous-help.html";
  39. $passwdfile = "/etc/passwd";
  40. $confI = "/etc/inetd.conf";
  41. $dummy = "/etc/passwd.tmp";
  42. $title = "Anonymous FTP Configuration";
  43.  
  44. if ($ENV{'HTTP_USER_AGENT'} =~ /Mozilla\/2/) { $br_index = 1; }
  45. else { $br_index = 0; }
  46.  
  47. $js = 
  48. "br_index = $br_index;
  49. $js_standard
  50. $js_error_box
  51. $js_help
  52. $js_filename
  53. function checkForm(form) {
  54.     if (form.eftp[br_index].checked) {
  55.         if (!testFilename(form.home, \"home directory name\")) return (false);
  56.     }
  57.     return (true);
  58. }";
  59.  
  60.  
  61. print $query->header;
  62. &js_title_block($title,$js);
  63.  
  64. $help = $document_root . $ENV{"SCRIPT_NAME"};
  65. $help =~ s/cgi$/hlp/;
  66. exec $help if ($query->param('help') eq "Help");
  67.  
  68. # if anonymous FTP user exists, we assume directory tree
  69. # also exists.
  70.  
  71. @getpass = getpwnam("ftp");
  72. if ($query->param('doit') eq 'Ok') {
  73.     if ($getpass[0]) {
  74.         $oldHome = $getpass[7];
  75.         $oldEftp = 'Yes';
  76.     }
  77.     if ($query->param('eftp') eq 'Yes') {
  78.         $home = $query->param('home');
  79.         &error("Invalid home directory.") if !$home;
  80.         &formValidation;
  81.         &checkInetdEntry;
  82.     
  83.         if (!$getpass[0] || $oldHome ne $home) { 
  84.             if ($getpass[0]) { &deleteOld; }
  85.             &add_password("ftp","*",500,25,"anon ftp",$home,"/bin/false"); 
  86.         }
  87.         if (-d $home) { &checkForFiles; }
  88.         else { &createHome; }
  89.         $message = "Anonymous FTP enabled.";
  90.     } else { &disable_ftp; }
  91. } elsif ($getpass[0]) {
  92.     $home = $getpass[7];
  93.     $eftp = 'Yes';
  94. } else {
  95.     $home = '/var/ftp';
  96.     $eftp = 'No';
  97. }
  98.  
  99. &generic;
  100.  
  101. print $query->end_html;
  102.  
  103. sub deleteOld {
  104.     open(IN,"< $passwdfile");
  105.     open(OUT,"> $dummy");
  106.     while(<IN>) {
  107.         @items = split(/:/);
  108.         if ($items[0] ne "ftp") { print OUT $_; }
  109.     }
  110.     close(IN);
  111.     close(OUT);
  112.     rename($dummy,$passwdfile);
  113. }        
  114.  
  115. sub checkInetdEntry {
  116.     $found = 0;
  117.     $doRename = 0;
  118.     open(IN,"< $confI");
  119.     open(OUT,"> $dummy");
  120.     while(<IN>) {
  121.         @items = split(/\s+/);
  122.         if (($items[0] eq "#" && $items[1] eq "ftp") || $items[0] eq "#ftp") {
  123.             $_ =~ /^\s*#\s*(.*)$/;
  124.             $line = $1;
  125.             print OUT "$line\n";
  126.             $found = 1;
  127.             $doRename = 1;
  128.         } elsif ($items[0] eq "ftp") {
  129.             print OUT $_;
  130.             $found = 1;
  131.         } else { print OUT $_; }
  132.     }
  133.     if (!$found) {
  134.         $doRename = 1;
  135.         print OUT "ftp\tstream\ttcp\tnowait\troot\t/user/etc/ftpd\tftpd\n";
  136.     }
  137.     close(IN);
  138.     close(OUT);
  139.     if ($doRename) { 
  140.         rename($dummy,$confI); 
  141.     system("/etc/killall", "-HUP", "inetd");
  142.     }
  143. }
  144.  
  145. sub formValidation {
  146.     if ($getpass[0]) {        # ftp user exists
  147.         if (-d $query->param('home')) {
  148.             my($dev,$ino,$mode,$nlink,$fuid,
  149.               $fgid,$rdev,$size,$atime,$mtime,
  150.               $ctime,$blksize,$blocks) = stat(_);
  151.  
  152.             my($name,$psswd,$uuid,$ugid,$quota,
  153.               $comment,$gcos,$dir,$shell) = getpwnam("ftp");
  154.  
  155.             &error("Home directory not owned by FTP") 
  156.               if $fuid != $uuid;
  157.         } elsif (-f $query->param('home')) {
  158.             &error("$home exists, but is not a directory.");
  159.         }
  160.     } else {                  #ftp user does not exist
  161.         if (-d $query->param('home')) {
  162.             &error("Home directory owned by other user.");
  163.         } elsif (-f $query->param('home')) {
  164.             &error("$home exists, but is not a directory.");
  165.         }
  166.     }
  167. }
  168.  
  169. sub error {
  170.     &error_block($_[0]);
  171.     &generic;
  172.     exit 0;
  173.  
  174. sub get_os_num {
  175.     open(IN, "/sbin/uname -r |");
  176.     $ret = <IN>; chop $ret;
  177.     close(IN);
  178.     $ret;
  179. }
  180.  
  181. sub createHome {
  182.     if (!$home) { return 0; }
  183.     # create home directory
  184.     system("/sbin/mkdir", "-p", $home);
  185.     system("/sbin/chown", "ftp.ftp", $home);
  186.  
  187.     $os_num = &get_os_num;
  188.  
  189.     # create subdirectories
  190.     mkdir("$home/bin", 0111);
  191.     mkdir("$home/pub", 0555);
  192.     mkdir("$home/etc", 0111);
  193.     mkdir("$home/lib", 0555);
  194.     mkdir("$home/dev", 0555);
  195.  
  196.     mkdir("$home/lib32", 0555) if ($os_num eq "6.4" || $os_num eq "6.5");
  197.  
  198.     system("/sbin/chown", "ftp.ftp", "$home/pub");
  199.   
  200.     system("/sbin/cp", "/bin/ls", "$home/bin"); 
  201.     chmod 0555, "$home/bin/ls";
  202.  
  203.     system("/sbin/cp", "/lib/rld", "$home/lib");
  204.     system("/sbin/cp", "/lib/libc.so.1", "$home/lib");
  205.     chmod 0555, "$home/lib/rld", "$home/lib/libc.so.1";
  206.  
  207.     if ($os_num eq "6.4" || $os_num eq "6.5") {
  208.     system("/sbin/cp", "/lib32/rld", "$home/lib32");
  209.     system("/sbin/cp", "/lib32/libc.so.1", "$home/lib32");
  210.     mkdir("$home/lib/iconv", 0555);
  211.     system("/sbin/cp", "/usr/lib/iconv/iconvtab", "$home/lib/iconv");
  212.     chmod 0555, "$home/lib/iconv/iconvtab";
  213.     }
  214.  
  215.     system("/sbin/mknod", "$home/dev/zero", "c", "37", "0");
  216. #   `mknod $home/dev/zero c 37 0`;
  217.     chmod 0444, "$home/dev/zero";
  218.  
  219.     $ftp_pass = $home . "/etc/passwd";
  220.     $ftp_grp = $home . "/etc/group";
  221.  
  222.     open(OUTP,"> $ftp_pass");
  223.     open(OUTG,"> $ftp_grp");
  224.  
  225.     my($name,$psswd,$uuid,$ugid,$quota,
  226.         $comment,$gcos,$dir,$shell) = getpwnam("ftp");
  227.     print OUTP "ftp:*:${uuid}:${ugid}:anon ftp:${dir}:${shell}\n";
  228.     print OUTG "ftp:*:${ugid}:\n";
  229.  
  230.     my($name,$psswd,$uuid,$ugid,$quota,
  231.         $comment,$gcos,$dir,$shell) = getpwnam("user");
  232.     print OUTG "user:*:${ugid}:\n";    
  233.  
  234.     my($name,$psswd,$uuid,$ugid,$quota,
  235.         $comment,$gcos,$dir,$shell) = getpwnam("root");
  236.     print OUTP "root:*:${uuid}:${ugid}::/:/bin/false\n";
  237.     print OUTG "sys:*:$ugid:\n";
  238.  
  239.     close(OUTP); 
  240.     close(OUTG);  
  241. }
  242.    
  243. sub checkForFiles { }  
  244.  
  245. sub disable_ftp {
  246.     $value = "";
  247.     &putpass;
  248.     $message = "Anonymous FTP disabled.";
  249. }
  250.  
  251. sub generic {
  252.     &header_block("Anonymous FTP");
  253.  
  254.     print "<i>$message</i>";
  255.  
  256.     print "<form name=\"StandardForm\" method=post onSubmit=\"return runSubmit()\">";
  257.  
  258.     print "<center><table cellpadding=5 width=450>\n";
  259.  
  260.     print "<tr><th align=left>Enable anonymous FTP:</th><th align=left>\n",
  261.       $query->radio_group(-name=>'eftp',
  262.       -values=>['Yes','No'], -default=>$eftp),
  263.       "</th></tr>\n";
  264.  
  265.     print "<tr><th align=left>Home directory for the FTP account:</th>\n",
  266.       "<th align=left>",
  267.       $query->textfield('home', $home),
  268.       "</th></tr>\n";
  269.  
  270.     print "</table></center><br>\n";
  271.  
  272.     print &js_buttons('doit','Ok','onClick="markOK()"','onClick="markOther()"',
  273.         "onClick=\"do_help('$help_page'); return (false)\"");
  274.  
  275.     print $query->endform;
  276.  
  277.     print $query->end_html;
  278. }
  279.  
  280. sub putpass {
  281.     $variable = "ftp";
  282.     local($len) = length($variable);
  283.  
  284.     open(IN,"< $passwdfile") || print "unable to read $passwdfile";
  285.     open(OUT,"> $dummy") || print "unable to write to $dummy";
  286.     $found = 0;
  287.  
  288.     while (<IN>) {
  289.         if ($variable eq substr($_,0,$len)) {
  290.             $found = 1;
  291.             print OUT $value;
  292.         } else { print OUT $_; }
  293.     }
  294.     if ($found == 0) { print OUT $value; }
  295.     close(OUT);
  296.     close(IN);
  297.     rename($dummy, $passwdfile) || print "unable to rename $dummy";
  298. }
  299.