Advertisement
Guest User

cgitelnet-mod.pl

a guest
Aug 27th, 2011
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 22.84 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #------------------------------------------------------------------------------
  3. # Copyright and Licence
  4. #------------------------------------------------------------------------------
  5. # CGI-Telnet Version 1.0 for NT and Unix : Run Commands on your Web Server
  6. #
  7. # Copyright (C) 2001 Rohitab Batra
  8. # Permission is granted to use, distribute and modify this script so long
  9. # as this copyright notice is left intact. If you make changes to the script
  10. # please document them and inform me. If you would like any changes to be made
  11. # in this script, you can e-mail me.
  12. #
  13. # Author: Rohitab Batra
  14. # Author e-mail: rohitab@rohitab.com
  15. # Author Homepage: http://www.rohitab.com/
  16. # Script Homepage: http://www.rohitab.com/cgiscripts/cgitelnet.html
  17. # Product Support: http://www.rohitab.com/support/
  18. # Discussion Forum: http://www.rohitab.com/discuss/
  19. # Mailing List: http://www.rohitab.com/mlist/
  20. #------------------------------------------------------------------------------
  21.  
  22. #------------------------------------------------------------------------------
  23. # Installation
  24. #------------------------------------------------------------------------------
  25. # To install this script
  26. #
  27. # 1. Modify the first line "#!/usr/bin/perl" to point to the correct path on
  28. #    your server. For most servers, you may not need to modify this.
  29. # 2. Change the password in the Configuration section below.
  30. # 3. If you're running the script under Windows NT, set $WinNT = 1 in the
  31. #    Configuration Section below.
  32. # 4. Upload the script to a directory on your server which has permissions to
  33. #    execute CGI scripts. This is usually cgi-bin. Make sure that you upload
  34. #    the script in ASCII mode.
  35. # 5. Change the permission (CHMOD) of the script to 755.
  36. # 6. Open the script in your web browser. If you uploaded the script in
  37. #    cgi-bin, this should be http://www.yourserver.com/cgi-bin/cgitelnet.pl
  38. # 7. Login using the password that you specified in Step 2.
  39. #------------------------------------------------------------------------------
  40.  
  41. #------------------------------------------------------------------------------
  42. # Configuration: You need to change only $Password and $WinNT. The other
  43. # values should work fine for most systems.
  44. #------------------------------------------------------------------------------
  45. $Password = "changeme";     # Change this. You will need to enter this
  46.                 # to login.
  47.  
  48. $WinNT = 0;         # You need to change the value of this to 1 if
  49.                 # you're running this script on a Windows NT
  50.                 # machine. If you're running it on Unix, you
  51.                 # can leave the value as it is.
  52.  
  53. $NTCmdSep = "&";        # This character is used to seperate 2 commands
  54.                 # in a command line on Windows NT.
  55.  
  56. $UnixCmdSep = ";";      # This character is used to seperate 2 commands
  57.                 # in a command line on Unix.
  58.  
  59. $CommandTimeoutDuration = 10;   # Time in seconds after commands will be killed
  60.                 # Don't set this to a very large value. This is
  61.                 # useful for commands that may hang or that
  62.                 # take very long to execute, like "find /".
  63.                 # This is valid only on Unix servers. It is
  64.                 # ignored on NT Servers.
  65.  
  66. $ShowDynamicOutput = 1;     # If this is 1, then data is sent to the
  67.                 # browser as soon as it is output, otherwise
  68.                 # it is buffered and send when the command
  69.                 # completes. This is useful for commands like
  70.                 # ping, so that you can see the output as it
  71.                 # is being generated.
  72.  
  73. # DON'T CHANGE ANYTHING BELOW THIS LINE UNLESS YOU KNOW WHAT YOU'RE DOING !!
  74.  
  75. $CmdSep = ($WinNT ? $NTCmdSep : $UnixCmdSep);
  76. $CmdPwd = ($WinNT ? "cd" : "pwd");
  77. $PathSep = ($WinNT ? "\\" : "/");
  78. $Redirector = ($WinNT ? " 2>&1 1>&2" : " 1>&1 2>&1");
  79.  
  80. #------------------------------------------------------------------------------
  81. # Reads the input sent by the browser and parses the input variables. It
  82. # parses GET, POST and multipart/form-data that is used for uploading files.
  83. # The filename is stored in $in{'f'} and the data is stored in $in{'filedata'}.
  84. # Other variables can be accessed using $in{'var'}, where var is the name of
  85. # the variable. Note: Most of the code in this function is taken from other CGI
  86. # scripts.
  87. #------------------------------------------------------------------------------
  88. sub ReadParse
  89. {
  90.     local (*in) = @_ if @_;
  91.     local ($i, $loc, $key, $val);
  92.    
  93.     $MultipartFormData = $ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/;
  94.  
  95.     if($ENV{'REQUEST_METHOD'} eq "GET")
  96.     {
  97.         $in = $ENV{'QUERY_STRING'};
  98.     }
  99.     elsif($ENV{'REQUEST_METHOD'} eq "POST")
  100.     {
  101.         binmode(STDIN) if $MultipartFormData & $WinNT;
  102.         read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
  103.     }
  104.  
  105.     # handle file upload data
  106.     if($ENV{'CONTENT_TYPE'} =~ /multipart\/form-data; boundary=(.+)$/)
  107.     {
  108.         $Boundary = '--'.$1; # please refer to RFC1867
  109.         @list = split(/$Boundary/, $in);
  110.         $HeaderBody = $list[1];
  111.         $HeaderBody =~ /\r\n\r\n|\n\n/;
  112.         $Header = $`;
  113.         $Body = $';
  114.         $Body =~ s/\r\n$//; # the last \r\n was put in by Netscape
  115.         $in{'filedata'} = $Body;
  116.         $Header =~ /filename=\"(.+)\"/;
  117.         $in{'f'} = $1;
  118.         $in{'f'} =~ s/\"//g;
  119.         $in{'f'} =~ s/\s//g;
  120.  
  121.         # parse trailer
  122.         for($i=2; $list[$i]; $i++)
  123.         {
  124.             $list[$i] =~ s/^.+name=$//;
  125.             $list[$i] =~ /\"(\w+)\"/;
  126.             $key = $1;
  127.             $val = $';
  128.             $val =~ s/(^(\r\n\r\n|\n\n))|(\r\n$|\n$)//g;
  129.             $val =~ s/%(..)/pack("c", hex($1))/ge;
  130.             $in{$key} = $val;
  131.         }
  132.     }
  133.     else # standard post data (url encoded, not multipart)
  134.     {
  135.         @in = split(/&/, $in);
  136.         foreach $i (0 .. $#in)
  137.         {
  138.             $in[$i] =~ s/\+/ /g;
  139.             ($key, $val) = split(/=/, $in[$i], 2);
  140.             $key =~ s/%(..)/pack("c", hex($1))/ge;
  141.             $val =~ s/%(..)/pack("c", hex($1))/ge;
  142.             $in{$key} .= "\0" if (defined($in{$key}));
  143.             $in{$key} .= $val;
  144.         }
  145.     }
  146. }
  147.  
  148. #------------------------------------------------------------------------------
  149. # Prints the HTML Page Header
  150. # Argument 1: Form item name to which focus should be set
  151. #------------------------------------------------------------------------------
  152. sub PrintPageHeader
  153. {
  154.     $EncodedCurrentDir = $CurrentDir;
  155.     $EncodedCurrentDir =~ s/([^a-zA-Z0-9])/'%'.unpack("H*",$1)/eg;
  156.     print "Content-type: text/html\n\n";
  157.     print <<END;
  158. <html>
  159. <head>
  160. <title>CGI-Telnet Version 1.0</title>
  161. $HtmlMetaHeader
  162. </head>
  163. <body onLoad="document.f.@_.focus()" bgcolor="#000000" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
  164. <table border="1" width="100%" cellspacing="0" cellpadding="2">
  165. <tr>
  166. <td bgcolor="#C2BFA5" bordercolor="#000080" align="center">
  167. <b><font color="#000080" size="2">#</font></b></td>
  168. <td bgcolor="#000080"><font face="Verdana" size="2" color="#FFFFFF"><b>CGI-Telnet Version 1.0 - Connected to $ServerName</b></font></td>
  169. </tr>
  170. <tr>
  171. <td colspan="2" bgcolor="#C2BFA5"><font face="Verdana" size="2">
  172. <a href="$ScriptLocation?a=logout">Disconnect</a> |
  173. <a href="http://www.rohitab.com/cgiscripts/cgitelnet.html">Help</a>
  174. </font></td>
  175. </tr>
  176. </table>
  177. <font color="#C0C0C0" size="3">
  178. END
  179. }
  180.  
  181. #------------------------------------------------------------------------------
  182. # Prints the Login Screen
  183. #------------------------------------------------------------------------------
  184. sub PrintLoginScreen
  185. {
  186.     $Message = q$<pre><font color="#669999"> _____  _____  _____          _____        _               _
  187. /  __ \|  __ \|_   _|        |_   _|      | |             | |
  188. | /  \/| |  \/  | |   ______   | |    ___ | | _ __    ___ | |_
  189. | |    | | __   | |  |______|  | |   / _ \| || '_ \  / _ \| __|
  190. | \__/\| |_\ \ _| |_           | |  |  __/| || | | ||  __/| |_
  191.  \____/ \____/ \___/           \_/   \___||_||_| |_| \___| \__| 1.0
  192.                                          
  193. </font><font color="#FF0000">                      ______             </font><font color="#AE8300">© 2001, Rohitab Batra</font><font color="#FF0000">
  194.                    .-&quot;      &quot;-.
  195.                   /            \
  196.                  |              |
  197.                  |,  .-.  .-.  ,|
  198.                  | )(_o/  \o_)( |
  199.                  |/     /\     \|
  200.        (@_       (_     ^^     _)
  201.   _     ) \</font><font color="#808080">_______</font><font color="#FF0000">\</font><font color="#808080">__</font><font color="#FF0000">|IIIIII|</font><font color="#808080">__</font><font color="#FF0000">/</font><font color="#808080">_______________________
  202. </font><font color="#FF0000"> (_)</font><font color="#808080">@8@8</font><font color="#FF0000">{}</font><font color="#808080">&lt;________</font><font color="#FF0000">|-\IIIIII/-|</font><font color="#808080">________________________&gt;</font><font color="#FF0000">
  203.         )_/        \          /
  204.        (@           `--------`
  205.              </font><font color="#AE8300">W A R N I N G: Private Server</font></pre>
  206. $;
  207. #'
  208.     print <<END;
  209. <code>
  210. Trying $ServerName...<br>
  211. Connected to $ServerName<br>
  212. Escape character is ^]
  213. <code>$Message
  214. END
  215. }
  216.  
  217. #------------------------------------------------------------------------------
  218. # Prints the message that informs the user of a failed login
  219. #------------------------------------------------------------------------------
  220. sub PrintLoginFailedMessage
  221. {
  222.     print <<END;
  223. <code>
  224. <br>login: admin<br>
  225. password:<br>
  226. Login incorrect<br><br>
  227. </code>
  228. END
  229. }
  230.  
  231. #------------------------------------------------------------------------------
  232. # Prints the HTML form for logging in
  233. #------------------------------------------------------------------------------
  234. sub PrintLoginForm
  235. {
  236.     print <<END;
  237. <code>
  238. <form name="f" method="POST" action="$ScriptLocation">
  239. <input type="hidden" name="a" value="login">
  240. login: admin<br>
  241. password:<input type="password" name="p">
  242. <input type="submit" value="Enter">
  243. </form>
  244. </code>
  245. END
  246. }
  247.  
  248. #------------------------------------------------------------------------------
  249. # Prints the footer for the HTML Page
  250. #------------------------------------------------------------------------------
  251. sub PrintPageFooter
  252. {
  253.     print "</font></body></html>";
  254. }
  255.  
  256. #------------------------------------------------------------------------------
  257. # Retreives the values of all cookies. The cookies can be accesses using the
  258. # variable $Cookies{''}
  259. #------------------------------------------------------------------------------
  260. sub GetCookies
  261. {
  262.     @httpcookies = split(/; /,$ENV{'HTTP_COOKIE'});
  263.     foreach $cookie(@httpcookies)
  264.     {
  265.         ($id, $val) = split(/=/, $cookie);
  266.         $Cookies{$id} = $val;
  267.     }
  268. }
  269.  
  270. #------------------------------------------------------------------------------
  271. # Prints the screen when the user logs out
  272. #------------------------------------------------------------------------------
  273. sub PrintLogoutScreen
  274. {
  275.     print "<code>Connection closed by foreign host.<br><br></code>";
  276. }
  277.  
  278. #------------------------------------------------------------------------------
  279. # Logs out the user and allows the user to login again
  280. #------------------------------------------------------------------------------
  281. sub PerformLogout
  282. {
  283.     print "Set-Cookie: SAVEDPWD=;\n"; # remove password cookie
  284.     &PrintPageHeader("p");
  285.     &PrintLogoutScreen;
  286.     &PrintLoginScreen;
  287.     &PrintLoginForm;
  288.     &PrintPageFooter;
  289. }
  290.  
  291. #------------------------------------------------------------------------------
  292. # This function is called to login the user. If the password matches, it
  293. # displays a page that allows the user to run commands. If the password doens't
  294. # match or if no password is entered, it displays a form that allows the user
  295. # to login
  296. #------------------------------------------------------------------------------
  297. sub PerformLogin
  298. {
  299.     if($LoginPassword eq $Password) # password matched
  300.     {
  301.         print "Set-Cookie: SAVEDPWD=$LoginPassword;\n";
  302.         &PrintPageHeader("c");
  303.         &PrintCommandLineInputForm;
  304.         &PrintPageFooter;
  305.     }
  306.     else # password didn't match
  307.     {
  308.         &PrintPageHeader("p");
  309.         &PrintLoginScreen;
  310.         if($LoginPassword ne "") # some password was entered
  311.         {
  312.             &PrintLoginFailedMessage;
  313.         }
  314.         &PrintLoginForm;
  315.         &PrintPageFooter;
  316.     }
  317. }
  318.  
  319. #------------------------------------------------------------------------------
  320. # Prints the HTML form that allows the user to enter commands
  321. #------------------------------------------------------------------------------
  322. sub PrintCommandLineInputForm
  323. {
  324.     #$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
  325.     $Prompt = $WinNT ? "$CurrentDir> " : "[Enter command]\$ ";
  326.     print <<END;
  327. <code>
  328. <form name="f" method="POST" action="$ScriptLocation">
  329. <input type="hidden" name="a" value="command">
  330. <input type="hidden" name="d" value="$CurrentDir">
  331. $Prompt
  332. <input type="text" name="c">
  333. <input type="submit" value="Enter">
  334. </form>
  335. </code>
  336. END
  337. }
  338.  
  339. #------------------------------------------------------------------------------
  340. # This function is called when the timeout for a command expires. We need to
  341. # terminate the script immediately. This function is valid only on Unix. It is
  342. # never called when the script is running on NT.
  343. #------------------------------------------------------------------------------
  344. sub CommandTimeout
  345. {
  346.     if(!$WinNT)
  347.     {
  348.         alarm(0);
  349.         print <<END;
  350. </xmp>
  351. <code>
  352. Command exceeded maximum time of $CommandTimeoutDuration second(s).
  353. <br>Killed it!
  354. <code>
  355. END
  356.         &PrintCommandLineInputForm;
  357.         &PrintPageFooter;
  358.         exit;
  359.     }
  360. }
  361.  
  362. #------------------------------------------------------------------------------
  363. # This function is called to execute commands. It displays the output of the
  364. # command and allows the user to enter another command. The change directory
  365. # command is handled differently. In this case, the new directory is stored in
  366. # an internal variable and is used each time a command has to be executed. The
  367. # output of the change directory command is not displayed to the users
  368. # therefore error messages cannot be displayed.
  369. #------------------------------------------------------------------------------
  370. sub ExecuteCommand
  371. {  
  372. # acceptable commands if, elseif, then final else to non-functional command
  373.     # PING
  374.     if($RunCommand =~ m/^\s*ping\s+(.+)/)
  375.     {
  376.         &PrintPageHeader("c");
  377.                 #$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
  378.                 $Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
  379.                 print "<code>$Prompt $RunCommand</code><xmp>";
  380.                 $Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
  381.                 if(!$WinNT)
  382.                 {
  383.                         $SIG{'ALRM'} = \&CommandTimeout;
  384.                         alarm($CommandTimeoutDuration);
  385.                 }
  386.                 if($ShowDynamicOutput) # show output as it is generated
  387.                 {
  388.                         $|=1;
  389.                         $Command .= " |";
  390.                         open(CommandOutput, $Command);
  391.                         while(<CommandOutput>)
  392.                         {
  393.                                 $_ =~ s/(\n|\r\n)$//;
  394.                                 print "$_\n";
  395.                         }
  396.                         $|=0;
  397.                 }
  398.                 else # show output after command completes
  399.                 {
  400.                         print `$Command`;
  401.                 }
  402.                 if(!$WinNT)
  403.                 {
  404.                         alarm(0);
  405.                 }
  406.                 print "</xmp>";
  407.  
  408.     }
  409.     # TELNET
  410.     elseif ($RunCommand =~ m/^\s*telnet\s+(.+)/)
  411.     {
  412.         &PrintPageHeader("c");
  413.         #$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
  414.         $Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
  415.         print "<code>$Prompt $RunCommand</code><xmp>";
  416.         $Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
  417.         if(!$WinNT)
  418.         {
  419.             $SIG{'ALRM'} = \&CommandTimeout;
  420.             alarm($CommandTimeoutDuration);
  421.         }
  422.         if($ShowDynamicOutput) # show output as it is generated
  423.         {
  424.             $|=1;
  425.             $Command .= " |";
  426.             open(CommandOutput, $Command);
  427.             while(<CommandOutput>)
  428.             {
  429.                 $_ =~ s/(\n|\r\n)$//;
  430.                 print "$_\n";
  431.             }
  432.             $|=0;
  433.         }
  434.         else # show output after command completes
  435.         {
  436.             print `$Command`;
  437.         }
  438.         if(!$WinNT)
  439.         {
  440.             alarm(0);
  441.         }
  442.         print "</xmp>";
  443.     }
  444.     else
  445.     {
  446.         # Print not an acceptable command
  447.                 # command is not displayed.
  448.  
  449.                 $Prompt = $WinNT ? "$OldDir> " : "Not an acceptable command ";
  450.         }
  451.     &PrintCommandLineInputForm;
  452.     &PrintPageFooter;
  453. }
  454.  
  455. #------------------------------------------------------------------------------
  456. # This function displays the page that contains a link which allows the user
  457. # to download the specified file. The page also contains a auto-refresh
  458. # feature that starts the download automatically.
  459. # Argument 1: Fully qualified filename of the file to be downloaded
  460. #------------------------------------------------------------------------------
  461. sub PrintDownloadLinkPage
  462. {
  463.     local($FileUrl) = @_;
  464.     if(-e $FileUrl) # if the file exists
  465.     {
  466.         # encode the file link so we can send it to the browser
  467.         $FileUrl =~ s/([^a-zA-Z0-9])/'%'.unpack("H*",$1)/eg;
  468.         $DownloadLink = "$ScriptLocation?a=download&f=$FileUrl&o=go";
  469.         $HtmlMetaHeader = "<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=$DownloadLink\">";
  470.         &PrintPageHeader("c");
  471.         print <<END;
  472. <code>
  473. Sending File $TransferFile...<br>
  474. If the download does not start automatically,
  475. <a href="$DownloadLink">Click Here</a>.
  476. </code>
  477. END
  478.         &PrintCommandLineInputForm;
  479.         &PrintPageFooter;
  480.     }
  481.     else # file doesn't exist
  482.     {
  483.         &PrintPageHeader("f");
  484.         print "<code>Failed to download $FileUrl: $!</code>";
  485.         &PrintFileDownloadForm;
  486.         &PrintPageFooter;
  487.     }
  488. }
  489.  
  490. #------------------------------------------------------------------------------
  491. # This function reads the specified file from the disk and sends it to the
  492. # browser, so that it can be downloaded by the user.
  493. # Argument 1: Fully qualified pathname of the file to be sent.
  494. #------------------------------------------------------------------------------
  495. sub SendFileToBrowser
  496. {
  497.     local($SendFile) = @_;
  498.     if(open(SENDFILE, $SendFile)) # file opened for reading
  499.     {
  500.         if($WinNT)
  501.         {
  502.             binmode(SENDFILE);
  503.             binmode(STDOUT);
  504.         }
  505.         $FileSize = (stat($SendFile))[7];
  506.         ($Filename = $SendFile) =~  m!([^/^\\]*)$!;
  507.         print "Content-Type: application/x-unknown\n";
  508.         print "Content-Length: $FileSize\n";
  509.         print "Content-Disposition: attachment; filename=$1\n\n";
  510.         print while(<SENDFILE>);
  511.         close(SENDFILE);
  512.     }
  513.     else # failed to open file
  514.     {
  515.         &PrintPageHeader("f");
  516.         print "<code>Failed to download $SendFile: $!</code>";
  517.         &PrintFileDownloadForm;
  518.         &PrintPageFooter;
  519.     }
  520. }
  521.  
  522.  
  523. #------------------------------------------------------------------------------
  524. # This function is called when the user downloads a file. It displays a message
  525. # to the user and provides a link through which the file can be downloaded.
  526. # This function is also called when the user clicks on that link. In this case,
  527. # the file is read and sent to the browser.
  528. #------------------------------------------------------------------------------
  529. sub BeginDownload
  530. {
  531.     # get fully qualified path of the file to be downloaded
  532.     if(($WinNT & ($TransferFile =~ m/^\\|^.:/)) |
  533.         (!$WinNT & ($TransferFile =~ m/^\//))) # path is absolute
  534.     {
  535.         $TargetFile = $TransferFile;
  536.     }
  537.     else # path is relative
  538.     {
  539.         chop($TargetFile) if($TargetFile = $CurrentDir) =~ m/[\\\/]$/;
  540.         $TargetFile .= $PathSep.$TransferFile;
  541.     }
  542.  
  543.     if($Options eq "go") # we have to send the file
  544.     {
  545.         &SendFileToBrowser($TargetFile);
  546.     }
  547.     else # we have to send only the link page
  548.     {
  549.         &PrintDownloadLinkPage($TargetFile);
  550.     }
  551. }
  552.  
  553. #------------------------------------------------------------------------------
  554. # This function is called when the user wants to upload a file. If the
  555. # file is not specified, it displays a form allowing the user to specify a
  556. # file, otherwise it starts the upload process.
  557. #------------------------------------------------------------------------------
  558. sub UploadFile
  559. {
  560.     # if no file is specified, print the upload form again
  561.     if($TransferFile eq "")
  562.     {
  563.         &PrintPageHeader("f");
  564.         &PrintFileUploadForm;
  565.         &PrintPageFooter;
  566.         return;
  567.     }
  568.     &PrintPageHeader("c");
  569.  
  570.     # start the uploading process
  571.     print "<code>Uploading $TransferFile to $CurrentDir...<br>";
  572.  
  573.     # get the fullly qualified pathname of the file to be created
  574.     chop($TargetName) if ($TargetName = $CurrentDir) =~ m/[\\\/]$/;
  575.     $TransferFile =~ m!([^/^\\]*)$!;
  576.     $TargetName .= $PathSep.$1;
  577.  
  578.     $TargetFileSize = length($in{'filedata'});
  579.     # if the file exists and we are not supposed to overwrite it
  580.     if(-e $TargetName && $Options ne "overwrite")
  581.     {
  582.         print "Failed: Destination file already exists.<br>";
  583.     }
  584.     else # file is not present
  585.     {
  586.         if(open(UPLOADFILE, ">$TargetName"))
  587.         {
  588.             binmode(UPLOADFILE) if $WinNT;
  589.             print UPLOADFILE $in{'filedata'};
  590.             close(UPLOADFILE);
  591.             print "Transfered $TargetFileSize Bytes.<br>";
  592.             print "File Path: $TargetName<br>";
  593.         }
  594.         else
  595.         {
  596.             print "Failed: $!<br>";
  597.         }
  598.     }
  599.     print "</code>";
  600.     &PrintCommandLineInputForm;
  601.     &PrintPageFooter;
  602. }
  603.  
  604. #------------------------------------------------------------------------------
  605. # This function is called when the user wants to download a file. If the
  606. # filename is not specified, it displays a form allowing the user to specify a
  607. # file, otherwise it displays a message to the user and provides a link
  608. # through  which the file can be downloaded.
  609. #------------------------------------------------------------------------------
  610. sub DownloadFile
  611. {
  612.     # if no file is specified, print the download form again
  613.     if($TransferFile eq "")
  614.     {
  615.         &PrintPageHeader("f");
  616.         &PrintFileDownloadForm;
  617.         &PrintPageFooter;
  618.         return;
  619.     }
  620.    
  621.     # get fully qualified path of the file to be downloaded
  622.     if(($WinNT & ($TransferFile =~ m/^\\|^.:/)) |
  623.         (!$WinNT & ($TransferFile =~ m/^\//))) # path is absolute
  624.     {
  625.         $TargetFile = $TransferFile;
  626.     }
  627.     else # path is relative
  628.     {
  629.         chop($TargetFile) if($TargetFile = $CurrentDir) =~ m/[\\\/]$/;
  630.         $TargetFile .= $PathSep.$TransferFile;
  631.     }
  632.  
  633.     if($Options eq "go") # we have to send the file
  634.     {
  635.         &SendFileToBrowser($TargetFile);
  636.     }
  637.     else # we have to send only the link page
  638.     {
  639.         &PrintDownloadLinkPage($TargetFile);
  640.     }
  641. }
  642.  
  643. #------------------------------------------------------------------------------
  644. # Main Program - Execution Starts Here
  645. #------------------------------------------------------------------------------
  646. &ReadParse;
  647. &GetCookies;
  648.  
  649. $ScriptLocation = $ENV{'SCRIPT_NAME'};
  650. $ServerName = $ENV{'SERVER_NAME'};
  651. $LoginPassword = $in{'p'};
  652. $RunCommand = $in{'c'};
  653. $TransferFile = $in{'f'};
  654. $Options = $in{'o'};
  655.  
  656. $Action = $in{'a'};
  657. $Action = "login" if($Action eq ""); # no action specified, use default
  658.  
  659. # get the directory in which the commands will be executed
  660. $CurrentDir = $in{'d'};
  661. chop($CurrentDir = `$CmdPwd`) if($CurrentDir eq "");
  662.  
  663. $LoggedIn = $Cookies{'SAVEDPWD'} eq $Password;
  664.  
  665. if($Action eq "login" || !$LoggedIn) # user needs/has to login
  666. {
  667.     &PerformLogin;
  668. }
  669. elsif($Action eq "command") # user wants to run a command
  670. {
  671.     &ExecuteCommand;
  672. }
  673. elsif($Action eq "upload") # user wants to upload a file
  674. {
  675.     &UploadFile;
  676. }
  677. elsif($Action eq "download") # user wants to download a file
  678. {
  679.     &DownloadFile;
  680. }
  681. elsif($Action eq "logout") # user wants to logout
  682. {
  683.     &PerformLogout;
  684. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement