You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
6.4 KiB

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