Detect and override password in /root/.my.cnf

This commit is contained in:
Jamie Cameron 2009-04-03 20:52:41 +00:00
parent bbf395b891
commit 31ceb6ba83
2 changed files with 66 additions and 33 deletions

View File

@ -81,3 +81,5 @@ The password is passed to all MySQL commands using the MYSQL_PWD environment var
---- Changes since 1.450 ---- ---- Changes since 1.450 ----
Use DBI for listing databases and tables, to reduce dependency on the un-reliable MYSQL_PWD environment variable. Use DBI for listing databases and tables, to reduce dependency on the un-reliable MYSQL_PWD environment variable.
Fixed the input for setting the default value for fields. Fixed the input for setting the default value for fields.
---- Changes since 1.470 ----
Added code to detect a password in /root/.my.cnf which overrides the MYSQL_PWD variable, and thus causes login failures.

View File

@ -648,7 +648,22 @@ else {
# Returns 1 if passing the password via an environment variable is supported # Returns 1 if passing the password via an environment variable is supported
sub supports_env_pass sub supports_env_pass
{ {
return $mysql_version >= 4.1 && !$config{'nopwd'}; if ($mysql_version >= 4.1 && !$config{'nopwd'}) {
# Theortically possible .. but don't do this if ~/.my.cnf contains
# a [client] block with password= in it
my @uinfo = getpwuid($<);
foreach my $cf ($config{'my_cnf'}, "$uinfo[7]/.my.cnf",
"$ENV{'HOME'}/.my.cnf") {
next if (!$cf || !-r $cf);
local @cf = &parse_mysql_config($cf);
local $client = &find("client", \@cf);
next if (!$client);
local $password = &find("password", $client->{'members'});
return 0 if ($password ne '');
}
return 1;
}
return 0;
} }
# working_env_pass() # working_env_pass()
@ -945,10 +960,25 @@ return $config{'host'} eq '' || $config{'host'} eq 'localhost' ||
sub get_mysql_config sub get_mysql_config
{ {
if (!defined(@mysql_config_cache)) { if (!defined(@mysql_config_cache)) {
local $sect; if (!-r $config{'my_cnf'}) {
local $lnum = 0; return undef;
open(CONF, $config{'my_cnf'}) || return undef; }
while(<CONF>) { @mysql_config_cache = &parse_mysql_config($config{'my_cnf'});
}
return \@mysql_config_cache;
}
# parse_mysql_config(file)
# Reads one MySQL config file
sub parse_mysql_config
{
local ($file) = @_;
local @rv;
local $sect;
local $lnum = 0;
local $lref = &read_file_lines($file, 1);
local $_;
foreach (@$lref) {
s/\r|\n//g; s/\r|\n//g;
s/#.*$//; s/#.*$//;
s/\s+$//; s/\s+$//;
@ -956,15 +986,17 @@ if (!defined(@mysql_config_cache)) {
# Start of a section # Start of a section
$sect = { 'name' => $1, $sect = { 'name' => $1,
'members' => [ ], 'members' => [ ],
'file' => $file,
'line' => $lnum, 'line' => $lnum,
'eline' => $lnum }; 'eline' => $lnum };
push(@mysql_config_cache, $sect); push(@rv, $sect);
} }
elsif (/^\s*(\S+)\s*=\s*(.*)$/ && $sect) { elsif (/^\s*(\S+)\s*=\s*(.*)$/ && $sect) {
# Variable in a section # Variable in a section
push(@{$sect->{'members'}}, push(@{$sect->{'members'}},
{ 'name' => $1, { 'name' => $1,
'value' => $2, 'value' => $2,
'file' => $file,
'line' => $lnum }); 'line' => $lnum });
$sect->{'eline'} = $lnum; $sect->{'eline'} = $lnum;
} }
@ -972,14 +1004,13 @@ if (!defined(@mysql_config_cache)) {
# Single directive in a section # Single directive in a section
push(@{$sect->{'members'}}, push(@{$sect->{'members'}},
{ 'name' => $1, { 'name' => $1,
'file' => $file,
'line' => $lnum }); 'line' => $lnum });
$sect->{'eline'} = $lnum; $sect->{'eline'} = $lnum;
} }
$lnum++; $lnum++;
} }
close(CONF); return @rv;
}
return \@mysql_config_cache;
} }
# find(name, &conf) # find(name, &conf)