EOForm
return 1;
}
# =====================================
# sub Print_Usage:
# print some information on STDOUT
# =====================================
sub Print_Usage {
Print_Error_Messages();
if ($MySQLaccess::CMD) { #command-line mode
Print_Options();
}
if ($MySQLaccess::CGI) { #CGI-BIN mode
Print_Form();
}
return 1;
}
# ======================================
# sub Print_Version:
# ======================================
sub Print_Version {
if ($MySQLaccess::CMD) {
print $MySQLaccess::INFO;
}
if ($MySQLaccess::CGI) {
print "\n";
print $MySQLaccess::INFO;
print "\n";
}
return 1;
}
# ======================================
# sub Print_Relnotes:
# ======================================
sub Print_Relnotes {
if ($MySQLaccess::CMD) {
print $MySQLaccess::RELEASE;
}
if ($MySQLaccess::CGI) {
print "\n";
print $MySQLaccess::RELEASE;
print "\n";
}
return 1;
}
# ======================================
# sub Print_Plans:
# ======================================
sub Print_Plans {
if ($MySQLaccess::CMD) {
print $MySQLaccess::TODO;
}
if ($MySQLaccess::CGI) {
print "\n";
print $MySQLaccess::TODO;
print "\n";
}
return 1;
}
# ======================================
# sub Print_HowTo:
# ======================================
sub Print_HowTo {
if ($MySQLaccess::CMD) {
print $MySQLaccess::HOWTO;
}
if ($MySQLaccess::CGI) {
print "\n";
print $MySQLaccess::HOWTO;
print "\n";
}
return 1;
}
# ======================================
# sub Print_Options:
# ======================================
sub Print_Options {
if ($MySQLaccess::CGI) { print "\n"; }
print $MySQLaccess::OPTIONS;
if ($MySQLaccess::CGI) { print "\n"; }
return 1;
}
# ======================================
# sub Print_Error_Access:
# ======================================
sub Print_Error_Access {
my ($error) = @_;
print "\n";
if ($MySQLaccess::CGI) { print "\n\n"; }
print $MESSAGES{$error};
if ($MySQLaccess::CGI) { print "\n\n"; }
print "\n";
return 1;
}
# ======================================
# sub Print_Error_Messages:
# ======================================
sub Print_Error_Messages {
# my ($error) = @_;
print "\n";
if ($MySQLaccess::CGI) { print "\n\n"; }
foreach $error (@MySQLaccess::Grant::Error) {
print $MESSAGES{$error};
print $MySQLaccess::CGI ? "
\n" : "\n";
}
if ($MySQLaccess::CGI) { print "\n\n"; }
print "\n";
return 1;
}
# ======================================
# sub Print_Message:
# ======================================
sub Print_Message {
my ($aref) = @_;
my @messages = @{$aref};
print "\n";
if ($MySQLaccess::CGI) { print "\n\n"; }
foreach $msg (@messages) {
print $msg;
print $MySQLaccess::CGI ? "
\n" : "\n";
}
if ($MySQLaccess::CGI) { print "\n\n"; }
print "\n";
return 1;
}
# ======================================
# sub Print_Edit:
# ======================================
sub Print_Edit {
print "\n";
if (!$MySQLaccess::CGI) {
print "Note: Editing the temporary tables is NOT supported in CMD-line mode!\n";
return 0;
}
print "\n"
."\n"
."\n";
return 1;
}
# ======================================
# sub Print_Access_rights:
# print the access-rights on STDOUT
# ======================================
sub Print_Access_rights {
my ($host,$user,$db,$refhash) = @_;
if (defined($MySQLaccess::Param{'brief'})) {
# if ($MySQLaccess::CGI) { print "\n"; }
Matrix_Report($host,$user,$db,$refhash);
# if ($MySQLaccess::CGI) { print "\n"; }
}
else {
Tabular_Report($host,$user,$db,$refhash);
$MySQLaccess::Report::separator = $MySQLaccess::CGI ? "
" : "-"x80;
}
return 1;
}
# ======================================
# sub Print_Diff_ACL:
# print the diff. in the grants before and after
# ======================================
sub Print_Diff_ACL {
my ($aref) = @_;
my @diffs = @{$aref};
my %block = ( '<' => 'Before',
'>' => 'After',
);
my %color = ( '<' => 'Green',
'>' => 'Red',
);
my $curblock = '';
# -----------------------------
# create column-headers
foreach $field (@MySQLaccess::Grant::Privileges) {
push(@headers,substr($field,0,4));
}
if ($MySQLaccess::CMD) {
print "\n";
print "Differences in access-rights BEFORE and AFTER changes in grant-tables\n";
# print "---------------------------------------------------------------------\n";
my $line1="";
my $line2="";
$line1 .= sprintf("| %-30s|",'Host,User,DB');
$line2 .= sprintf("+-%-30s+",'-' x 30);
foreach $header (@headers) {
$line1 .= sprintf("%-4s|",$header);
$line2 .= sprintf("%s+",'----');
}
print "$line2\n";
print "$line1\n";
print "$line2\n";
$format = "format STDOUT = \n"
. "^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< " . " @|||" x 10 ."\n"
. '$host_user_db,@priv' . "\n"
. ".\n";
#print $format;
eval $format;
}
if ($MySQLaccess::CGI) {
print "\n";
print "\n";
print "| ";
print "Differences in access-rights BEFORE "
."and '}>AFTER changes to grant-tables\n";
print " | ";
print "
\n";
print "\n";
$line1 .= sprintf("| %-20s | ",'Host, User, DB');
foreach $header (@headers) {
$line1 .= sprintf("%-4s | ",$header);
}
print "$line1
\n";
}
foreach $line (@diffs) {
$type = substr($line,0,1);
$line = substr($line,1);
($host,$user,$db,@priv) = split(/,/,$line);
if ($MySQLaccess::CMD) {
if ($type ne $curblock) {
$curblock = $type;
print $block{$curblock},":\n";
}
#print "$line\n";
write;
}
if ($MySQLaccess::CGI) {
if ($type ne $curblock) {
$curblock = $type;
print "| $block{$curblock} |
\n";
}
$line1="$host, $user, $db | ";
foreach $field (@priv) {
$line1 .= sprintf("%-4s | ",$field);
}
print "$line1
\n";
}
}
print "\n";
if ($MySQLaccess::CMD) {
print "---------------------------------------------------------------------\n";
}
if ($MySQLaccess::CGI) {
print "
";
}
return 1;
}
# ======================================
# sub Tabular_Report
# Tabular report,
# suitable for 1 triple (host,db,user)
# ======================================
sub Tabular_Report {
my ($host,$user,$db,$a) = @_;
my $column=2;
# -----------------------------
# separator
if ($MySQLaccess::Report::separator) { print "$MySQLaccess::Report::separator\n"; }
# -----------------------------
# print table of access-rights
my $rows = int(@MySQLaccess::Grant::Privileges/2); #round up
my @table=();
$j=0;
for $i (0 .. $rows-1) {
$table[$j]=$MySQLaccess::Grant::Privileges[$i];
$j = $j+2;
}
$j=1;
for $i ($rows .. $#MySQLaccess::Grant::Privileges) {
$table[$j]=$MySQLaccess::Grant::Privileges[$i];
$j = $j+2;
}
if ($MySQLaccess::CMD) {
print "\n";
print "Access-rights\n";
print "for USER '$user', from HOST '$host', to DB '$db'\n";
}
if ($MySQLaccess::CGI) {
print "\n";
print "\n";
}
if ($MySQLaccess::CGI) {
print "| ";
print "Access-rights\n";
print "for USER '$user', from HOST '$host', to DB '$db'\n";
print " | ";
print "
\n";
print "\n";
}
if ($MySQLaccess::CMD) {
print "\t+-----------------+---+\t+-----------------+---+";
}
foreach $field (@table) {
if ($MySQLaccess::CMD) {
if ($column==2) { print "\n\t"; $column=1;}
else { print "\t"; $column=2;}
printf "| %-15s | %s |",$field,$Answer{$a->{$field}};
}
if ($MySQLaccess::CGI) {
if ($column==2) { print "
\n\n"; $column=1;}
else { print " | "; $column=2;}
printf " %-15s | %s | \n",$field,$Answer{$a->{$field}};
}
}
print "\n";
if ($MySQLaccess::CMD) {
print "\t+-----------------+---+\t+-----------------+---+\n";
}
if ($MySQLaccess::CGI) {
print "
\n
";
}
# ---------------
# print notes:
foreach $note (@MySQLaccess::Grant::Notes) {
my $message = $MESSAGES{$note};
$message =~ s/\$user/$user/g;
$message =~ s/\$db/$db/g;
$message =~ s/\$host/$host/g;
$message =~ s/\$password/$password/g;
$PREFIX='NOTE';
if ($MySQLaccess::CMD) {
my @lines = split(/\n/,$message);
foreach $line (@lines) {
print "$PREFIX:\t $line\n";
$PREFIX=' ';
}
}
if ($MySQLaccess::CGI) {
print "$PREFIX: $message
\n";
}
}
# ---------------
# print warnings:
foreach $warning (@MySQLaccess::Grant::Warnings) {
my $message = $MESSAGES{$warning};
$message =~ s/\$user/$user/g;
$message =~ s/\$db/$db/g;
$message =~ s/\$host/$host/g;
$message =~ s/\$password/$password/g;
$PREFIX='BEWARE';
if ($MySQLaccess::CMD) {
my @lines = split(/\n/,$message);
foreach $line (@lines) {
print "$PREFIX:\t $line\n";
$PREFIX=' ';
}
}
if ($MySQLaccess::CGI) {
print "$PREFIX: $message
\n";
}
}
# ---------------
# print errors:
foreach $error (@MySQLaccess::Grant::Errors) {
my $message = $MESSAGES{$error};
$message =~ s/\$user/$user/g;
$message =~ s/\$db/$db/g;
$message =~ s/\$host/$host/g;
$message =~ s/\$password/$password/g;
$PREFIX='ERROR';
if ($MySQLaccess::CMD) {
my @lines = split(/\n/,$message);
foreach $line (@lines) {
print "$PREFIX:\t $line\n";
$PREFIX=' ';
}
}
if ($MySQLaccess::CGI) {
print "$PREFIX: $message
\n";
}
}
# ---------------
# inform if there are no rules ==> full access for everyone.
if ($MySQLaccess::Grant::full_access) { print "$MESSAGES{'full_access'}\n"; }
# ---------------
# print the rules used
print "\n";
if ($MySQLaccess::CMD) {
print "The following rules are used:\n";
foreach $field (sort(keys(%MySQLaccess::Grant::Rules))) {
my $rule = (defined($MESSAGES{$MySQLaccess::Grant::Rules{$field}}) ? $MESSAGES{$MySQLaccess::Grant::Rules{$field}} : $MySQLaccess::Grant::Rules{$field});
$rule =~ s/\t/','/g;
printf " %-5s : '%s'\n",$field,$rule;
}
}
if ($MySQLaccess::CGI) {
print "
\n";
print "\n";
print "| The following rules are used: |
\n";
foreach $field (sort(keys(%MySQLaccess::Grant::Rules))) {
my $rule = (defined($MESSAGES{$MySQLaccess::Grant::Rules{$field}}) ? $MESSAGES{$MySQLaccess::Grant::Rules{$field}} : $MySQLaccess::Grant::Rules{$field});
$rule =~ s/\t/','/g;
printf "| %-5s | '%s' |
\n",$field,$rule;
}
print "
\n";
}
return 1;
}
# ======================================
# sub Matrix_Report:
# single-line output foreach triple,
# no notes,warnings,...
# ======================================
sub Matrix_Report {
my ($host,$user,$db,$a) = @_;
my @headers = ();
if (! $headers) {
# -----------------------------
# create column-headers
foreach $field (@MySQLaccess::Grant::Privileges) {
push(@headers,substr($field,0,4));
}
# -----------------------------
# print column-headers
print "\n";
if ($MySQLaccess::CMD) {
my $line1="";
my $line2="";
foreach $header (@headers) {
$line1 .= sprintf("%-4s ",$header);
$line2 .= sprintf("%s ",'----');
}
$line1 .= sprintf("| %-20s",'Host,User,DB');
$line2 .= sprintf("+ %-20s",'-' x 20);
print "$line1\n";
print "$line2\n";
}
if ($MySQLaccess::CGI) {
print "\n";
my $line1="";
foreach $header (@headers) {
$line1 .= sprintf("| %-4s | ",$header);
}
$line1 .= sprintf("%-20s | ",'Host, User, DB');
print "$line1
\n";
}
# ----------------------------
# column-headers should only be
# printed once.
$MySQLaccess::Report::headers=1;
}
# ------------------------
# print access-information
if ($MySQLaccess::CMD) {
foreach $field (@MySQLaccess::Grant::Privileges) {
printf " %-2s ",$Answer{$a->{$field}};
}
printf "| %-20s",join(',',$host,$user,$db);
print "\n";
}
if ($MySQLaccess::CGI) {
print "";
foreach $field (@MySQLaccess::Grant::Privileges) {
printf "| %-2s | ",$Answer{$a->{$field}};
}
printf "%-20s | ",join(', ',$host,$user,$db);
print "
\n";
}
return 1;
}
# ======================================
# sub Raw_Report:
# single-line output foreach triple,
# no notes,warnings,...
# ======================================
sub Raw_Report {
my ($host,$user,$db,$a) = @_;
my @headers = ();
my $string = "";
# ------------------------
# print access-information
$string = "$host,$user,$db,";
foreach $field (@MySQLaccess::Grant::Privileges) {
$string .= $Answer{$a->{$field}} . ",";
}
return $string;
}
#######################################################################
package MySQLaccess::Wildcards;
BEGIN {
$DEBUG = 0;
$DEBUG = $MySQLaccess::DEBUG unless ($DEBUG);
}
# ############################################
# SQL, WILDCARDS and REGULAR EXPRESSIONS
# ============================================
# translage SQL-expressions to Reg-expressions
# ============================================
sub SQL2Reg {
my ($expr) = @_;
my $expr_o = $expr;
$expr =~ s/\./\\./g;
$expr =~ s/\\%/\002/g;
$expr =~ s/%/.*/g;
$expr =~ s/\002/%/g;
$expr =~ s/\\_/\002/g;
$expr =~ s/_/.+/g;
$expr =~ s/\002/_/g;
MySQLaccess::Debug::Print(2,"$expr_o --> $expr");
return $expr;
}
# translage WILDcards to Reg-expressions
# ============================================
sub Wild2Reg {
my ($expr) = @_;
my $expr_o = $expr;
$expr =~ s/\./\\./g;
$expr =~ s/\\\*/\002/g;
$expr =~ s/\*/.*/g;
$expr =~ s/\002/*/g;
$expr =~ s/\\\?/\002/g;
$expr =~ s/\?/.+/g;
$expr =~ s/\002/?/g;
MySQLaccess::Debug::Print(2,"$expr_o --> $expr");
return $expr;
}
# =============================================
# match a given string with a template
# =============================================
sub MatchTemplate {
my ($tpl,$string) = @_;
my $match=0;
if ($string=~ /^$tpl$/ or $tpl eq '') { $match=1; }
else { $match=0;}
MySQLaccess::Debug::Print(2,"($tpl,$string) --> $match");
return $match;
}
#######################################################################
package MySQLaccess::Host;
BEGIN {
$localhost = undef;
$DEBUG = 2;
$DEBUG = $MySQLaccess::DEBUG unless ($DEBUG);
}
# ======================================
# sub IP2Name
# return the Name with the corr. IP-nmbr
# (no aliases yet!!)
# ======================================
sub IP2Name {
my ($ip) = @_;
my $ip_o = $ip;
if ($ip !~ /([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/o) {
MySQLaccess::Debug::Print(3,"'$ip' is not an ip-number, returning IP=$ip");
return $ip;
}
MySQLaccess::Debug::Print(4,"IP=$ip split up => $1.$2.$3.$4");
$ip = pack "C4",$1,$2,$3,$4;
MySQLaccess::Debug::Print(4,"IP packed -> >>$ip<<\n");
my ($name,$aliases,$addrtype,$length,@addrs) = gethostbyaddr($ip, AF_INET);
MySQLaccess::Debug::Print(3,"IP=$ip_o => hostname=$name");
MySQLaccess::Debug::Print(4,"aliases=$aliases");
MySQLaccess::Debug::Print(4,"addrtype=$addrtype - length=$length");
return ($name || $ip);
#return ($name || undef);
}
# ======================================
# sub Name2IP
# return the IP-number of the host
# ======================================
sub Name2IP {
my ($name) = @_;
if ($name =~ /[%_]/) {
MySQLaccess::Debug::Print(3,"'$name' contains SQL-wildcards, returning name=$name");
return $name;
}
my ($_name,$aliases,$addrtype,$length,@addrs) = gethostbyname($name);
my ($a,$b,$c,$d) = unpack('C4',$addrs[0]);
my $ip = "$a.$b.$c.$d";
MySQLaccess::Debug::Print(3,"hostname=$name => IP=$ip");
MySQLaccess::Debug::Print(4,"aliases=$aliases");
MySQLaccess::Debug::Print(4,"addrtype=$addrtype - length=$length");
#if ($ip ne "") { return "$ip"; }
#else { return undef; }
return ($ip || $name);
}
# ========================================
# sub LocalHost
# some special action has to be taken for
# the localhost
# ========================================
sub LocalHost {
if (!defined($MySQLaccess::Host::localhost)) {
$MySQLaccess::Host::localhost = Sys::Hostname::hostname();
MySQLaccess::Debug::Print(3,"Setting package variable \$localhost=$MySQLaccess::Host::localhost");
}
my $host = $localhost;
MySQLaccess::Debug::Print(3,"localhost = $host");
return $host;
}
# ========================================
# check if the given hostname (or ip)
# corresponds with the localhost
# ========================================
sub Is_localhost {
my ($host_tpl) = @_;
my $isit = 0;
if (($MySQLaccess::host_name eq $localhost) or ($MySQLaccess::host_ip eq $local_ip)) {
MySQLaccess::Debug::Print(2,"Checking for localhost");
MySQLaccess::Debug::Print(3,"because ($MySQLaccess::host_name EQ $localhost) AND ($MySQLaccess::host_ip EQ $local_ip)");
$isit = ( 'localhost' =~ /$host_tpl/ ) ? 1 : 0;
MySQLaccess::Debug::Print(3," 'localhost' =?= $host_tpl -> $isit");
return $isit;
}
else {
MySQLaccess::Debug::Print(4,"Not checking for localhost");
MySQLaccess::Debug::Print(4,"because ($MySQLaccess::host_name != $localhost) AND ($MySQLaccess::host_ip != $local_ip)");
return 0;
}
}
# =========================================
# check if host (IP or name) can be matched
# on the template.
# =========================================
sub MatchTemplate {
my ($host,$tpl) = @_;
my $match = 0;
MySQLaccess::Debug::Print(1, "($host) =?= ($tpl)");
my $host_name = IP2Name($host);
my $host_ip = Name2IP($host);
MySQLaccess::Debug::Print(2, "name=$host_name ; ip=$host_ip");
$match = (MySQLaccess::Wildcards::MatchTemplate($tpl,$host_name) or
MySQLaccess::Wildcards::MatchTemplate($tpl,$host_ip));
MySQLaccess::Debug::Print(2, "($host_name,$host_ip) =?= ($tpl): $ncount");
return $match;
}
########################################################################
package MySQLaccess::Debug;
BEGIN {
my $dbg_file = "$MySQLaccess::script_log";
open(DEBUG,"> $dbg_file") or warn "Could not open outputfile $dbg_file for debugging-info\n";
select DEBUG;
$| = 1;
select STDOUT;
}
# =========================================
# Print debugging information on STDERR
# =========================================
sub Print {
my ($level,$mesg) = @_;
my ($pack,$file,$line,$subname,$hasargs,$wantarray) = caller(1);
my ($PACK) = split('::',$subname);
my $DEBUG = ${$PACK."::DEBUG"} ? ${$PACK."::DEBUG"} : $MySQLaccess::DEBUG ;
my ($sec,$min,$hour) = localtime();
print DEBUG "[$hour:$min:$sec $subname] $mesg\n" if ($DEBUG>=$level);
}