#!/usr/bin/perl -w # ---------------------------------------------------------------------------- # Shell.pl v 1.3 Modified 11/10/00 # Copyright (c) 2000 Jason M. Hinkle. All rights reserved. This script is # free software; you may redistribute it and/or modify it under the same # terms as Perl itself. For information: http://www.verysimple.com/scripts/ # # LEGAL DISCLAIMER: This software is provided as-is. Use it at your own risk. # The author takes no responsibility for any damages or losses directly or # indirectly caused by this software. # ---------------------------------------------------------------------------- # re-route fatal errors $SIG{__DIE__} = \&CommandFailed; $SIG{__WARN__} = \&CommandFailed; #flush the buffer, require modules and create our objects $|++; use CGI; my ($objFormData) = new CGI; my ($VERSION) = "1.3"; my ($input) = ""; # check for form input if (defined($objFormData->param("command"))) { $input = $objFormData->param("command"); } # make it look nice &print_header(); &print_form($input); # execute the command if ($objFormData->param) { &execute_command($input); } &print_footer(); exit; #______________________________________________________________________________ sub print_form { my ($value) = shift || ""; print "This application executes commands on the server using\n"; print "the permission level of the web server account.\n"; print "See the ReadMe file about using this program on Windows servers.\n"; print "

\n"; print "

\n"; print "Shell Command:
\n"; print "\n"; print "\n"; print "
\n"; } #______________________________________________________________________________ sub print_header { print "Content-type: text/html\n\n"; print "\n"; print "\n"; print "VerySimple Shell Command Processor\n"; print "\n"; print "\n"; print "\n"; print "
\n"; print "VerySimple Shell Command Processor $VERSION\n"; print "
\n"; print "

\n"; } #______________________________________________________________________________ sub print_footer { print "


\n"; print "VerySimple Shell Command Processor: © 2000, VerySimple
\n"; print "A component of the VerySimple Developer Toolbox\n"; print "

\n"; print "\n"; print "\n"; print "\n"; } #______________________________________________________________________________ sub execute_command { my $command = shift || ""; if ($command eq "") { print "No command submitted\n"; return 0; } if (defined($ENV{'PATH_TRANSLATED'})) { execute_win_command($command); } else { execute_unix_command($command); } } #______________________________________________________________________________ sub execute_unix_command { my $command = shift || ""; # redirect error to stdout open (STDERR, ">>&STDOUT"); print "

\n"; print "Results:
\n"; print "
\n"; # attempt to get the username: @output = `whoami`; print "Username: " . $output[0] . "
\n"; print "Process Id: " . $$ . "
\n"; } #______________________________________________________________________________ sub execute_win_command { my $command = shift || ""; my ($exitcode) = 0; my ($wincompath) = $ENV{'COMSPEC'}; my ($wincom) = substr($wincompath,rindex($wincompath,"\\") + 1); # check that we were able to locate the command interpreter unless (-r $wincompath) { &CommandFailed("Win32 command interpreter could not be found."); exit; } # use the win32 libraries for windows servers. eval 'use Win32::Process'; eval 'use Win32'; print "
\n"; print "Results:
\n"; print "
\n"; print "Username: " . Win32::LoginName() . "
\n"; print "Command Interpreter: " . $wincompath . "
\n"; print "Process Id: " . $ProcessObj->GetProcessID() . "\n"; } #______________________________________________________________________________ sub ErrorReport{ print Win32::FormatMessage( Win32::GetLastError() ); } #______________________________________________________________________________ sub CurrentUrl { if (defined($ENV{'SCRIPT_NAME'})) { return $ENV{'SCRIPT_NAME'}; } else { return $ENV{REQUEST_URI}; } } #______________________________________________________________________________ sub CommandFailed { my ($error) = shift || "Unknown Error"; if (index($error,"NORMAL_PRIORITY_CLASS",1) > 0) { # ignore this warning from the Win32 module. } elsif (index($error,"No such file or directory at",1) > 0) { print "Command not found."; } else { print $error; } }