fix: pass the database password to mysql through MYSQL_PWD and quote the user properly in zmupdate refs #5686

This commit is contained in:
Jash committed 2026-11-13 13:29:20 +05:30
1 parent ec2587a0c3
commit eb16b9fc5b
1 file changed
+21 -12
+21 -12
View File
@@ -137,12 +137,8 @@ if ($updateDir) {
my $dbh = zmDbConnect(undef, { mysql_multi_statements=>1 } );
die "Unable to connect to db\n" if !$dbh;
#When putting the user and password on the command line, we will enclose in quotes, so escape any quotes.
$Config{ZM_DB_USER} = $dbUser;
$dbUser =~ s/'/\\'/g;
$Config{ZM_DB_PASS} = $dbPass;
# we escape dbpass with single quotes so that $ in the password has no effect, but dbpass could have a ' in it.
$dbPass =~ s/'/\\'/g;
if ( ! ($check || $freshen || $rename || $zoneFix || $migrateEvents || $version) ) {
if ( $Config{ZM_DYN_DB_VERSION} ) {
@@ -422,8 +418,7 @@ if ( $version ) {
if ($super) {
$command .= ' --defaults-file=/etc/mysql/debian.cnf';
} elsif ($dbUser) {
$command .= ' -u\''.$dbUser.'\'';
$command .= ' -p\''.$dbPass.'\'' if $dbPass;
$command .= ' -u'.shellQuote($dbUser);
}
if ( defined($portOrSocket) ) {
if ( $portOrSocket =~ /^\// ) {
@@ -439,8 +434,12 @@ if ( $version ) {
print("Creating backup to $backup. This may take several minutes.\n");
($command) = $command =~ /(.*)/; # detaint
print("Executing '$command'\n") if logDebugging();
my $output = qx($command);
my $status = $? >> 8;
my ( $output, $status );
{
local $ENV{MYSQL_PWD} = $dbPass if ( $dbUser && $dbPass && !$super );
$output = qx($command);
$status = $? >> 8;
}
if ( $status || logDebugging() ) {
chomp( $output );
print( "Output: $output\n" );
@@ -1016,6 +1015,13 @@ sub patchDB_using_do {
}
} # end sub patchDB_using_do
# Quote a value for a POSIX shell by wrapping it in single quotes.
sub shellQuote {
my $value = shift;
$value =~ s/'/'\\''/g;
return "'$value'";
}
sub patchDB {
my $dbh = shift;
my $version = shift;
@@ -1025,8 +1031,7 @@ sub patchDB {
if ($super) {
$command .= ' --defaults-file=/etc/mysql/debian.cnf';
} elsif ($dbUser) {
$command .= ' -u\''.$dbUser.'\'';
$command .= ' -p\''.$dbPass.'\'' if $dbPass;
$command .= ' -u'.shellQuote($dbUser);
}
if ( defined($portOrSocket) ) {
if ( $portOrSocket =~ /^\// ) {
@@ -1041,8 +1046,12 @@ sub patchDB {
print("Executing '$command'\n") if logDebugging();
($command) = $command =~ /(.*)/; # detaint
my $output = qx($command);
my $status = $? >> 8;
my ( $output, $status );
{
local $ENV{MYSQL_PWD} = $dbPass if ( $dbUser && $dbPass && !$super );
$output = qx($command);
$status = $? >> 8;
}
if ( $status || logDebugging() ) {
chomp($output);
print("Output: $output\n");