From 8a1e34686743db71277f75d6f818f9fab19c7078 Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Wed, 24 Jun 2009 13:47:53 +0000 Subject: [PATCH] Run restores and imports as non-root user --- mysql/CHANGELOG | 2 ++ mysql/exec_file.cgi | 3 ++- mysql/import.cgi | 12 +++++++++--- mysql/lang/en | 1 + mysql/mysql-lib.pl | 6 +++++- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/mysql/CHANGELOG b/mysql/CHANGELOG index c791cc13d..78e308412 100644 --- a/mysql/CHANGELOG +++ b/mysql/CHANGELOG @@ -84,3 +84,5 @@ 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. MySQL stored procedures are now included in backups, where supported. +---- Changes since 1.480 ---- +Restores and imports from local files are now run as the Unix user configured for backups, rather than root. diff --git a/mysql/exec_file.cgi b/mysql/exec_file.cgi index b7f8b0af6..c49489fd4 100755 --- a/mysql/exec_file.cgi +++ b/mysql/exec_file.cgi @@ -64,7 +64,8 @@ while() { close(SQL); print "
";
-($ex, $out) = &execute_sql_file($in{'db'}, $file);
+($ex, $out) = &execute_sql_file($in{'db'}, $file,
+				undef, undef, $access{'buser'});
 print &html_escape($out);
 $got++ if ($out =~ /\S/);
 print "$text{'exec_noout'}\n" if (!$got);
diff --git a/mysql/import.cgi b/mysql/import.cgi
index 1b88e5b3b..d58c93b3d 100755
--- a/mysql/import.cgi
+++ b/mysql/import.cgi
@@ -29,10 +29,10 @@ else {
 	print &text('import_fileout', "$in{'file'}"),"

\n"; } -# Execute the import command .. +# Build the import command if ($in{'table'}) { $nfile = &transname("$in{'table'}.txt"); - system("cp $file $nfile"); + ©_source_dest($file, $nfile); unlink($file) if ($need_unlink); $file = $nfile; $need_unlink = 1; @@ -45,9 +45,15 @@ if ($in{'format'} == 0) { elsif ($in{'format'} == 1) { $format = "--fields-terminated-by ,"; } + +# Execute the import command .. print "

";
 &additional_log('exec', undef, "$config{'mysqlimport'} $authstr $delete $ignore $format $in{'db'} $file");
-&open_execute_command(SQL, "$config{'mysqlimport'} $authstr $delete $ignore $format ".quotemeta($in{'db'})." $file 2>&1", 1, 0);
+$cmd = "$config{'mysqlimport'} $authstr $delete $ignore $format ".quotemeta($in{'db'})." ".quotemeta($file);
+if ($access{'buser'} && $access{'buser'} ne 'root' && $< == 0) {
+	$cmd = &command_as_user($access{'buser'}, 0, $cmd);
+	}
+&open_execute_command(SQL, $cmd, 2, 0);
 while() {
 	print &html_escape($_);
 	$got++ if (/\S/);
diff --git a/mysql/lang/en b/mysql/lang/en
index cd2d8277e..87ba070b1 100644
--- a/mysql/lang/en
+++ b/mysql/lang/en
@@ -562,6 +562,7 @@ acl_edonly=Can only edit table data?
 acl_bnone=Disallow backups
 acl_indexes=Can view and manage indexes?
 acl_views=Can view and manage views?
+acl_files=Can execute SQL from local files?
 
 log_start=Started MySQL server
 log_stop=Stopped MySQL server
diff --git a/mysql/mysql-lib.pl b/mysql/mysql-lib.pl
index e062c1db6..cd8c68b64 100644
--- a/mysql/mysql-lib.pl
+++ b/mysql/mysql-lib.pl
@@ -891,7 +891,7 @@ if (@auto) {
 return @sql;
 }
 
-# execute_sql_file(database, file, [user, pass])
+# execute_sql_file(database, file, [user, pass], [unix-user])
 # Executes some file of SQL commands, and returns the exit status and output
 sub execute_sql_file
 {
@@ -902,6 +902,10 @@ local ($db, $file, $user, $pass) = @_;
 local $authstr = &make_authstr($user, $pass);
 local $cmd = "$config{'mysql'} $authstr -t ".quotemeta($db)." <".quotemeta($file);
 -r $file || return (1, "$file does not exist");
+if ($_[4] && $_[4] ne 'root' && $< == 0) {
+	# Restoring as a Unix user
+	$cmd = &command_as_user($_[4], 0, $cmd);
+	}
 local $out = &backquote_logged("$cmd 2>&1");
 local @rv = ($?, $? ? $out || "$cmd failed" : $out);
 &make_authstr();	# Put back old password environment variable