Advertisement
Guest User

Untitled

a guest
Dec 12th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; version 2 of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17.  
  18.  
  19. if [ "$(id -u)" != "0" ]; then
  20. echo "This script must be run as root" 1>&2
  21. exit 1
  22. fi
  23.  
  24. cd /root
  25.  
  26. config=".my.cnf.$$"
  27. command=".mysql.$$"
  28. mysql_client=""
  29.  
  30. trap "interrupt" 1 2 3 6 15
  31.  
  32. rootpass=""
  33. echo_n=
  34. echo_c=
  35.  
  36. set_echo_compat() {
  37. case `echo "testing\c"`,`echo -n testing` in
  38. *c*,-n*) echo_n= echo_c= ;;
  39. *c*,*) echo_n=-n echo_c= ;;
  40. *) echo_n= echo_c='\c' ;;
  41. esac
  42. }
  43.  
  44. prepare() {
  45. touch $config $command
  46. chmod 600 $config $command
  47. }
  48.  
  49. find_mysql_client()
  50. {
  51. for n in ./bin/mysql mysql
  52. do
  53. $n --no-defaults --help > /dev/null 2>&1
  54. status=$?
  55. if test $status -eq 0
  56. then
  57. mysql_client=$n
  58. return
  59. fi
  60. done
  61. echo "Can't find a 'mysql' client in PATH or ./bin"
  62. exit 1
  63. }
  64.  
  65. do_query() {
  66. echo "$1" >$command
  67. #sed 's,^,> ,' < $command # Debugging
  68. $mysql_client --defaults-file=$config <$command
  69. return $?
  70. }
  71.  
  72. # Simple escape mechanism (\-escape any ' and \), suitable for two contexts:
  73. # - single-quoted SQL strings
  74. # - single-quoted option values on the right hand side of = in my.cnf
  75. #
  76. # These two contexts don't handle escapes identically. SQL strings allow
  77. # quoting any character (\C => C, for any C), but my.cnf parsing allows
  78. # quoting only \, ' or ". For example, password='a\b' quotes a 3-character
  79. # string in my.cnf, but a 2-character string in SQL.
  80. #
  81. # This simple escape works correctly in both places.
  82. basic_single_escape () {
  83. # The quoting on this sed command is a bit complex. Single-quoted strings
  84. # don't allow *any* escape mechanism, so they cannot contain a single
  85. # quote. The string sed gets (as argv[1]) is: s/\(['\]\)/\\\1/g
  86. #
  87. # Inside a character class, \ and ' are not special, so the ['\] character
  88. # class is balanced and contains two characters.
  89. echo "$1" | sed 's/\(['"'"'\]\)/\\\1/g'
  90. }
  91.  
  92. make_config() {
  93. echo "# mysql_secure_installation config file" >$config
  94. echo "[mysql]" >>$config
  95. echo "user=root" >>$config
  96. esc_pass=`basic_single_escape "$rootpass"`
  97. echo "password='$esc_pass'" >>$config
  98. #sed 's,^,> ,' < $config # Debugging
  99. }
  100.  
  101. get_root_password() {
  102. echo "Assuming that root password is stored where it use to be"
  103. password=$root_mysql_pass
  104.  
  105. if [ "x$password" = "x" ]; then
  106. hadpass=0
  107. else
  108. hadpass=1
  109. fi
  110. rootpass=$password
  111. make_config
  112. do_query ""
  113. status=$?
  114.  
  115. if [ $status -eq 1 ]; then
  116. echo "Fail, mysql does not allow passwordless connection"
  117. clean_and_exit
  118. fi
  119.  
  120. echo "OK, successfully injected root password, moving on..."
  121. echo
  122. }
  123.  
  124. set_root_password() {
  125.  
  126. password1=$(uuidgen -r)
  127.  
  128. if [ $? -ne 0 ]; then
  129. echo "Unable to find uuidgen to generate random password..."
  130. echo
  131. clean_and_exit
  132. fi
  133.  
  134. if [ "$password1" = "" ]; then
  135. echo "Sorry, you can't use an empty password here."
  136. echo
  137. clean_and_exit
  138. fi
  139.  
  140.  
  141. esc_pass=`basic_single_escape "$password1"`
  142. do_query "UPDATE mysql.user SET Password=PASSWORD('$esc_pass') WHERE User='root';"
  143. if [ $? -eq 0 ]; then
  144. echo "Password updated successfully!"
  145. echo "Reloading privilege tables.."
  146. reload_privilege_tables
  147. if [ $? -eq 1 ]; then
  148. clean_and_exit
  149. fi
  150. echo
  151. rootpass=$password1
  152. make_config
  153. else
  154. echo "Password update failed!"
  155. clean_and_exit
  156. fi
  157.  
  158. return 0
  159. }
  160.  
  161. remove_anonymous_users() {
  162. do_query "DELETE FROM mysql.user WHERE User='';"
  163. if [ $? -eq 0 ]; then
  164. echo " ... Success!"
  165. else
  166. echo " ... Failed!"
  167. clean_and_exit
  168. fi
  169.  
  170. return 0
  171. }
  172.  
  173. remove_remote_root() {
  174. do_query "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
  175. if [ $? -eq 0 ]; then
  176. echo " ... Success!"
  177. else
  178. echo " ... Failed!"
  179. fi
  180. }
  181.  
  182. remove_test_database() {
  183. echo " - Dropping test database..."
  184. do_query "DROP DATABASE test;"
  185. if [ $? -eq 0 ]; then
  186. echo " ... Success!"
  187. else
  188. echo " ... Failed! Not critical, keep moving..."
  189. fi
  190.  
  191. echo " - Removing privileges on test database..."
  192. do_query "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
  193. if [ $? -eq 0 ]; then
  194. echo " ... Success!"
  195. else
  196. echo " ... Failed! Not critical, keep moving..."
  197. fi
  198.  
  199. return 0
  200. }
  201.  
  202. reload_privilege_tables() {
  203. do_query "FLUSH PRIVILEGES;"
  204. if [ $? -eq 0 ]; then
  205. echo " ... Success!"
  206. return 0
  207. else
  208. echo " ... Failed!"
  209. return 1
  210. fi
  211. }
  212.  
  213. interrupt() {
  214. echo
  215. echo "Aborting!"
  216. echo
  217. cleanup
  218. stty echo
  219. exit 1
  220. }
  221.  
  222. cleanup() {
  223. echo "Cleaning up..."
  224. rm -f $config $command
  225. }
  226.  
  227. # Remove the files before exiting.
  228. clean_and_exit() {
  229. cleanup
  230. exit 1
  231. }
  232.  
  233. # The actual script starts here
  234.  
  235. prepare
  236. find_mysql_client
  237. set_echo_compat
  238.  
  239. echo
  240. echo
  241. echo
  242. echo
  243. echo "NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL"
  244. echo " SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!"
  245. echo
  246. echo
  247.  
  248. echo "In order to log into MySQL to secure it, we'll need the current"
  249. echo "password for the root user. If you've just installed MySQL, and"
  250. echo "you haven't set the root password yet, the password will be blank."
  251. echo
  252.  
  253. get_root_password
  254.  
  255.  
  256. #
  257. # Set the root password
  258. #
  259.  
  260. echo "Setting the root password ensures that nobody can log into the MySQL"
  261. echo "root user without the proper authorisation."
  262. echo
  263.  
  264. echo "Setting random root password"
  265. set_root_password
  266. echo
  267.  
  268.  
  269. #
  270. # Remove anonymous users
  271. #
  272.  
  273. echo "By default, a MySQL installation has an anonymous user, allowing anyone"
  274. echo "to log into MySQL without having to have a user account created for"
  275. echo "them. This is intended only for testing, and to make the installation"
  276. echo "go a bit smoother. You should remove them before moving into a"
  277. echo "production environment."
  278. echo
  279.  
  280. echo $echo_n "Removing anonymous users $echo_c"
  281. remove_anonymous_users
  282. echo
  283.  
  284.  
  285. #
  286. # Disallow remote root login
  287. #
  288.  
  289. echo "Normally, root should only be allowed to connect from 'localhost'. This"
  290. echo "ensures that someone cannot guess at the root password from the network."
  291. echo
  292.  
  293. echo $echo_n "Disallowing root login remotely $echo_c"
  294. remove_remote_root
  295. echo
  296.  
  297.  
  298. #
  299. # Remove test database
  300. #
  301.  
  302. echo "By default, MySQL comes with a database named 'test' that anyone can"
  303. echo "access. This is also intended only for testing, and should be removed"
  304. echo "before moving into a production environment."
  305. echo
  306.  
  307. remove_test_database
  308. echo
  309.  
  310. #
  311. # Reload privilege tables
  312. #
  313.  
  314. echo "Reloading the privilege tables will ensure that all changes made so far"
  315. echo "will take effect immediately."
  316. echo
  317.  
  318. echo $echo_n "Reloading privilege tables now $echo_c"
  319.  
  320. reload_privilege_tables
  321.  
  322. cleanup
  323. config='.my.cnf'
  324. make_config
  325.  
  326. echo
  327. echo
  328. echo
  329. echo "All done! Your MySQL installation should now be secure."
  330. echo
  331. echo "We have saved your random-generated root password to "
  332. echo "/root/.my.cnf"
  333. echo
  334. echo "Thanks for using MySQL!"
  335. echo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement