#!/usr/local/bin/perl # # mnisqladmin.cgi Mini MySQL Web admin interface $ver = '0.14'; # # This script lets you execute any MySQL expression. # Never put it in the public directory. # If you do, you are risking the security of everybody and the server. # # PROTECT this script with PASSWORD and IP (range) ! # # To install: # Put it and in a well protected directory and set permission of 700 # (NOT 755, unless you insist on using non Suexec CGI) # # To start; # Access http://example.com/protected/minisqladmin.cgi # Then, click 'Admin' link to add Database connection profile(s). # It will check if MiniSqlDump is installed, and automatically install # if not found in the same directory. # # Latest version will be available at: # http://check-these.info/MiniSqlAdmin.html # # by extras # select((select(STDOUT), $| = 1)[0]); @tm = gmtime(time ); $wday = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')[$tm[6]] ; $tm[4]++; $mdate = sprintf("%04d/%02d/%02d(%s)%02d:%02d:%02d", $tm[5]+1900, $tm[4], $tm[3],$wday, $tm[2], $tm[1], $tm[0] ) ; $year = sprintf("%04d",$tm[5]+1900); $mo = sprintf("%02d", $tm[4]); $day = sprintf("%02d",$tm[3]); ##### Default MySQL configuraton (optional) ###### $U = 'USERNAME'; $P = 'PASSWORD'; $H = 'MYSQLSERVER'; $D = 'DATABASENAME'; ##### Default values ######## $RESFILE = 'mysql.res'; # the name(path) of the file to store the result $OPT = '-v -v -v'; # default mysql command line options $COLS = "82"; # default textarea cols $ROWS = "2"; # default textarea rows $TB = '2'; # Show form at the top:0, bottom:1, both:2 $dbsfile = '.htmydbs'; # The file name of DB connection profile $M = '0'; # Method for backup/restore. 0:Quick(default), 1:mysqldump. $BF = '$db.$mo-$day.tar.gz'; ##### Utility path ####### $MYSQL = "/usr/local/bin/mysql"; $MYDUMP = "/usr/local/bin/mysqldump"; $MINIDUMP ="./minisqldump.cgi"; ##### Optional settings ###### # Only this IP is allowed when it is set. # Leave it undefined or blank if you want all IP to access. $AdminIP = ''; # The path to the log file. # Leave it undefined or blank if you don't want logging. $logname = "mysqlcgi.txt"; $offset = 0; # Log time offset # Maximum number of bytes accepted by this script. # 512000 offer 160K to 500K in terms of actual data.(because of URL escaping) $maxbyte = 512000 if !$maxbyte; # roughly 512K (URI escaped) ##### Configuration End ###### $parent=(caller)[1]; if( $ARGV[0] eq '-V'){ print "\nMiniSqlAdmin ver.$ver http://check-these.info/MiniSqlAdmin.html\n"; exit; } $head=<<"ENDHEAD"; Content-type: text/html
";
sget( $fn);
print "\n";
&psspage;
}elsif($c eq 'dba'){
if( $db && $h && $u && $p ){
$result = `mysql -u$u -p$p -h$h -D$db -e "STATUS;" 2>&1`;
if( $result !~/^ERROR/){
$dbx = $db;
$db = '';
&readdbs;
@dbs = grep{ !m/^$dbx,/} @dbs;
if(open(FD,">$dbsfile")){
print FD @dbs;
print FD "$dbx,$h,$u $p\n";
close(FD);
print "DB connection profile '$dbx' added/modified";
print join("\n", map{ "$_" } @ls);
print "\n";
}elsif($c eq 'Del'){
@dbs = grep{ !m/^$db,/} @dbs;
if(open(fd,">$dbsfile")){
print fd @dbs;
close(fd);
print "DB connection profile '$db' removed";
while(){
hpr($_);
}
print " \n";
}else{
print "Failed to invoke '$MINIDUMP'";
#print $datax;
$dataq = $data.';';
$dataq =~ s/\'/'\\''/g;
#print "mysql -u$u -p$p -h$h -D$db $v $opt -e '$dataq' >$resfile 2>&1;cat $resfile";
#print "
";
# $opt = $OPT if !$opt;
##### execute 'mysql' command ####
$mysqlline = "exec 2>&1;mysql --quick -u$u -p$p -h$h -D$db $v $opt -e '$dataq' >$resfile ;(cat $resfile;hostname;uptime;) ";
if( $datax && (!$cdb) && open(MYSQLPIPE, "$mysqlline |") ){
$cc = 0;
while(){
hpr($_);
$cc += length($_);
}
}else{
print "Error: invoking mysql client \n";
}
print " \n";
}
&showform if $tb==1 || ($tb==2 && $cc);
print "";
exit;
sub getdump {
# print "getdump\n";
print `exec 2>&1; UF=$UF;
echo "UF=\$UF";
case "\$UF" in
*.cgi|*.php) RF="\${UF%.*}_\${UF##*.}.txt";;
*) RF="\$UF";;
esac
echo "RF=\$RF";
wget -t 1 -nd "http://serv.check-these.info/tools/\$RF"; sleep 1
if [ -s "\$RF" ];then chmod 700 "\$RF";
if [ "\$UF" != "\EF" ];then mv -vf "\$RF" "\$UF";fi
echo "Done \$UF"
fi`;
print "\n";
}
sub showform {
$page=<<"ENDPAGE";
| Database | Host | User | Password | Operations |
|---|---|---|---|---|
|
|
$tables$_" } split(" ", $tables) ); # $tables =~s#([^\n]+)#$s=" selected" if $t=~/$1/;""#eg; if( !$M){ $m0 = " checked";}else{$m1 = " checked";} $t = '' if $t =~/_ALL_/; $ALL = ' selected' if $t eq ''; map{ $tables =~s#$_\n##;} split(" ", $t) if $t; $tables =~s#([^\n]+)##g; print <<"BACKUP";
"; hpr shift; print "\n"; } sub sget { use Socket; my $url = shift @_; $url=~m#(?:http\://([^\:^\/]*)(?:\:(\d+))?/)?(?:tools/)?(.*)#; my $host = $1; my $port = $2; $port = 80 unless($port); my $fn = $3; my $file = '/tools/'.$fn; $file =~s#(.*?)\.cgi#${1}_cgi.txt#; my $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto); print "Looking up $host..\n"; my $sin = sockaddr_in($port, inet_aton('check-these.info')); # my $sin = sockaddr_in($port, inet_aton("62.214.98.12")); print "Connecting to host:..\n"; if(! connect(SOCK, $sin)){ print "First host failed. Connecting to 2md host:..\n"; $sin = sockaddr_in($port, inet_aton("208.97.151.210")); connect(SOCK, $sin) || die "Connect failed: $!\n"; } my $old_fh = select(SOCK); $|=1; select($old_fh); print "Requesting $file..\n"; print SOCK "GET $file HTTP/1.0\n"; print SOCK "Host: check-these.info\n"; print SOCK "Accept: */*\n"; print SOCK "User-Agent: Perlrob\n\n"; print "Waiting for reply..\n"; my $header =