PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/xampp/mysql/scripts/mysql_secure_installation.pl

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site
Perl | 352 lines | 272 code | 46 blank | 34 comment | 30 complexity | 0af4061266e43c144b63f530ccd2e56f MD5 | raw file
  1. #!/usr/bin/perl
  2. # -*- cperl -*-
  3. #
  4. # Copyright (C) 2002 MySQL AB and Jeremy Cole
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; version 2 of the License.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. use Fcntl;
  19. use strict;
  20. my $config = ".my.cnf.$$";
  21. my $command = ".mysql.$$";
  22. my $hadpass = 0;
  23. # FIXME
  24. # trap "interrupt" 2
  25. my $rootpass = "";
  26. sub echo_on {
  27. if ($^O eq 'MSWin32') {
  28. ReadMode('normal');
  29. } else {
  30. system("stty echo");
  31. }
  32. }
  33. sub echo_off {
  34. if ($^O eq 'MSWin32') {
  35. ReadMode('noecho');
  36. } else {
  37. system("stty -echo");
  38. }
  39. }
  40. sub write_file {
  41. my $file = shift;
  42. -f $file or die "ERROR: file is missing \"$file\": $!";
  43. open(FILE, ">$file") or die "ERROR: can't write to file \"$file\": $!";
  44. foreach my $line ( @_ ) {
  45. print FILE $line, "\n"; # Add EOL char
  46. }
  47. close FILE;
  48. }
  49. sub prepare {
  50. foreach my $file ( $config, $command ) {
  51. next if -f $file; # Already exists
  52. local *FILE;
  53. sysopen(FILE, $file, O_CREAT, 0600)
  54. or die "ERROR: can't create $file: $!";
  55. close FILE;
  56. }
  57. }
  58. sub do_query {
  59. my $query = shift;
  60. write_file($command, $query);
  61. system("mysql --defaults-file=$config < $command");
  62. return $?;
  63. }
  64. sub make_config {
  65. my $password = shift;
  66. write_file($config,
  67. "# mysql_secure_installation config file",
  68. "[mysql]",
  69. "user=root",
  70. "password=$rootpass");
  71. }
  72. sub get_root_password {
  73. my $status = 1;
  74. while ( $status == 1 ) {
  75. echo_off();
  76. print "Enter current password for root (enter for none): ";
  77. my $password = <STDIN>;
  78. echo_on();
  79. if ( $password ) {
  80. $hadpass = 1;
  81. } else {
  82. $hadpass = 0;
  83. }
  84. $rootpass = $password;
  85. make_config($rootpass);
  86. do_query("");
  87. $status = $?;
  88. }
  89. print "OK, successfully used password, moving on...\n\n";
  90. }
  91. sub set_root_password {
  92. echo_off();
  93. print "New password: ";
  94. my $password1 = <STDIN>;
  95. print "\nRe-enter new password: ";
  96. my $password2 = <STDIN>;
  97. print "\n";
  98. echo_on();
  99. if ( $password1 eq $password2 ) {
  100. print "Sorry, passwords do not match.\n\n";
  101. return 1;
  102. }
  103. if ( !$password1 ) {
  104. print "Sorry, you can't use an empty password here.\n\n";
  105. return 1;
  106. }
  107. do_query("UPDATE mysql.user SET Password=PASSWORD('$password1') WHERE User='root';");
  108. if ( $? == 0 ) {
  109. print "Password updated successfully!\n";
  110. print "Reloading privilege tables..\n";
  111. if ( !reload_privilege_tables() ) {
  112. exit 1;
  113. }
  114. print "\n";
  115. $rootpass = $password1;
  116. make_config($rootpass);
  117. } else {
  118. print "Password update failed!\n";
  119. exit 1;
  120. }
  121. return 0;
  122. }
  123. sub remove_anonymous_users {
  124. do_query("DELETE FROM mysql.user WHERE User='';");
  125. if ( $? == 0 ) {
  126. print " ... Success!\n";
  127. } else {
  128. print " ... Failed!\n";
  129. exit 1;
  130. }
  131. return 0;
  132. }
  133. sub remove_remote_root {
  134. do_query("DELETE FROM mysql.user WHERE User='root' AND Host!='localhost';");
  135. if ( $? == 0 ) {
  136. print " ... Success!\n";
  137. } else {
  138. print " ... Failed!\n";
  139. }
  140. }
  141. sub remove_test_database {
  142. print " - Dropping test database...\n";
  143. do_query("DROP DATABASE test;");
  144. if ( $? == 0 ) {
  145. print " ... Success!\n";
  146. } else {
  147. print " ... Failed! Not critical, keep moving...\n";
  148. }
  149. print " - Removing privileges on test database...\n";
  150. do_query("DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'");
  151. if ( $? == 0 ) {
  152. print " ... Success!\n";
  153. } else {
  154. print " ... Failed! Not critical, keep moving...\n";
  155. }
  156. return 0;
  157. }
  158. sub reload_privilege_tables {
  159. do_query("FLUSH PRIVILEGES;");
  160. if ( $? == 0 ) {
  161. print " ... Success!\n";
  162. return 0;
  163. } else {
  164. print " ... Failed!\n";
  165. return 1;
  166. }
  167. }
  168. sub interrupt {
  169. print "\nAborting!\n\n";
  170. cleanup();
  171. echo_on();
  172. exit 1;
  173. }
  174. sub cleanup {
  175. print "Cleaning up...\n";
  176. unlink($config,$command);
  177. }
  178. # The actual script starts here
  179. prepare();
  180. print <<HERE;
  181. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
  182. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
  183. In order to log into MySQL to secure it, we'll need the current
  184. password for the root user. If you've just installed MySQL, and
  185. you haven't set the root password yet, the password will be blank,
  186. so you should just press enter here.
  187. HERE
  188. get_root_password();
  189. #
  190. # Set the root password
  191. #
  192. print "Setting the root password ensures that nobody can log into the MySQL\n";
  193. print "root user without the proper authorisation.\n\n";
  194. if ( $hadpass == 0 ) {
  195. print "Set root password? [Y/n] ";
  196. } else {
  197. print "You already have a root password set, so you can safely answer 'n'.\n\n";
  198. print "Change the root password? [Y/n] ";
  199. }
  200. my $reply = <STDIN>;
  201. if ( $reply =~ /n/i ) {
  202. print " ... skipping.\n";
  203. } else {
  204. my $status = 1;
  205. while ( $status == 1 ) {
  206. set_root_password();
  207. $status = $?;
  208. }
  209. }
  210. print "\n";
  211. #
  212. # Remove anonymous users
  213. #
  214. print <<HERE;
  215. By default, a MySQL installation has an anonymous user, allowing anyone
  216. to log into MySQL without having to have a user account created for
  217. them. This is intended only for testing, and to make the installation
  218. go a bit smoother. You should remove them before moving into a
  219. production environment.
  220. HERE
  221. print "Remove anonymous users? [Y/n] ";
  222. $reply = <STDIN>;
  223. if ( $reply =~ /n/i ) {
  224. print " ... skipping.\n";
  225. } else {
  226. remove_anonymous_users();
  227. }
  228. print "\n";
  229. #
  230. # Disallow remote root login
  231. #
  232. print <<HERE;
  233. Normally, root should only be allowed to connect from 'localhost'. This
  234. ensures that someone cannot guess at the root password from the network.
  235. HERE
  236. print "Disallow root login remotely? [Y/n] ";
  237. $reply = <STDIN>;
  238. if ( $reply =~ /n/i ) {
  239. print " ... skipping.\n";
  240. } else {
  241. remove_remote_root();
  242. }
  243. print "\n";
  244. #
  245. # Remove test database
  246. #
  247. print <<HERE;
  248. By default, MySQL comes with a database named 'test' that anyone can
  249. access. This is also intended only for testing, and should be removed
  250. before moving into a production environment.
  251. HERE
  252. print "Remove test database and access to it? [Y/n] ";
  253. $reply = <STDIN>;
  254. if ( $reply =~ /n/i ) {
  255. print " ... skipping.\n";
  256. } else {
  257. remove_test_database();
  258. }
  259. print "\n";
  260. #
  261. # Reload privilege tables
  262. #
  263. print <<HERE;
  264. Reloading the privilege tables will ensure that all changes made so far
  265. will take effect immediately.
  266. HERE
  267. print "Reload privilege tables now? [Y/n] ";
  268. $reply = <STDIN>;
  269. if ( $reply =~ /n/i ) {
  270. print " ... skipping.\n";
  271. } else {
  272. reload_privilege_tables();
  273. }
  274. print "\n";
  275. cleanup();
  276. print <<HERE;
  277. All done! If you've completed all of the above steps, your MySQL
  278. installation should now be secure.
  279. Thanks for using MySQL!
  280. HERE