Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.31 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Copyright (c) 2002, 2012, Oracle and/or its affiliates.
  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. config=".my.cnf.$$"
  19. command=".mysql.$$"
  20.  
  21. trap "interrupt" 1 2 3 6 15
  22.  
  23. rootpass=""
  24. echo_n=
  25. echo_c=
  26. basedir=
  27. bindir=
  28.  
  29. parse_arg()
  30. {
  31. echo "$1" | sed -e 's/^[^=]*=//'
  32. }
  33.  
  34. parse_arguments()
  35. {
  36. # We only need to pass arguments through to the server if we don't
  37. # handle them here. So, we collect unrecognized options (passed on
  38. # the command line) into the args variable.
  39. pick_args=
  40. if test "$1" = PICK-ARGS-FROM-ARGV
  41. then
  42. pick_args=1
  43. shift
  44. fi
  45.  
  46. for arg
  47. do
  48. case "$arg" in
  49. --basedir=*) basedir=`parse_arg "$arg"` ;;
  50. --no-defaults|--defaults-file=*|--defaults-extra-file=*)
  51. defaults="$arg" ;;
  52. *)
  53. if test -n "$pick_args"
  54. then
  55. # This sed command makes sure that any special chars are quoted,
  56. # so the arg gets passed exactly to the server.
  57. # XXX: This is broken; true fix requires using eval and proper
  58. # quoting of every single arg ($basedir, $ldata, etc.)
  59. #args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
  60. args="$args $arg"
  61. fi
  62. ;;
  63. esac
  64. done
  65. }
  66.  
  67. # Try to find a specific file within --basedir which can either be a binary
  68. # release or installed source directory and return the path.
  69. find_in_basedir()
  70. {
  71. return_dir=0
  72. found=0
  73. case "$1" in
  74. --dir)
  75. return_dir=1; shift
  76. ;;
  77. esac
  78.  
  79. file=$1; shift
  80.  
  81. for dir in "$@"
  82. do
  83. if test -f "$basedir/$dir/$file"
  84. then
  85. found=1
  86. if test $return_dir -eq 1
  87. then
  88. echo "$basedir/$dir"
  89. else
  90. echo "$basedir/$dir/$file"
  91. fi
  92. break
  93. fi
  94. done
  95.  
  96. if test $found -eq 0
  97. then
  98. # Test if command is in PATH
  99. $file --no-defaults --version > /dev/null 2>&1
  100. status=$?
  101. if test $status -eq 0
  102. then
  103. echo $file
  104. fi
  105. fi
  106. }
  107.  
  108. cannot_find_file()
  109. {
  110. echo
  111. echo "FATAL ERROR: Could not find $1"
  112.  
  113. shift
  114. if test $# -ne 0
  115. then
  116. echo
  117. echo "The following directories were searched:"
  118. echo
  119. for dir in "$@"
  120. do
  121. echo " $dir"
  122. done
  123. fi
  124.  
  125. echo
  126. echo "If you compiled from source, you need to run 'make install' to"
  127. echo "copy the software into the correct location ready for operation."
  128. echo
  129. echo "If you are using a binary release, you must either be at the top"
  130. echo "level of the extracted archive, or pass the --basedir option"
  131. echo "pointing to that location."
  132. echo
  133. }
  134.  
  135. # Ok, let's go. We first need to parse arguments which are required by
  136. # my_print_defaults so that we can execute it first, then later re-parse
  137. # the command line to add any extra bits that we need.
  138. parse_arguments PICK-ARGS-FROM-ARGV "$@"
  139.  
  140. #
  141. # We can now find my_print_defaults. This script supports:
  142. #
  143. # --srcdir=path pointing to compiled source tree
  144. # --basedir=path pointing to installed binary location
  145. #
  146. # or default to compiled-in locations.
  147. #
  148.  
  149. if test -n "$basedir"
  150. then
  151. print_defaults=`find_in_basedir my_print_defaults bin extra`
  152. echo "print: $print_defaults"
  153. if test -z "$print_defaults"
  154. then
  155. cannot_find_file my_print_defaults $basedir/bin $basedir/extra
  156. exit 1
  157. fi
  158. else
  159. print_defaults="/usr/bin/my_print_defaults"
  160. fi
  161.  
  162. if test ! -x "$print_defaults"
  163. then
  164. cannot_find_file "$print_defaults"
  165. exit 1
  166. fi
  167.  
  168. # Now we can get arguments from the group [client] and [client-server]
  169. # in the my.cfg file, then re-run to merge with command line arguments.
  170. parse_arguments `$print_defaults $defaults client client-server client-mariadb`
  171. parse_arguments PICK-ARGS-FROM-ARGV "$@"
  172.  
  173. # Configure paths to support files
  174. if test -n "$basedir"
  175. then
  176. bindir="$basedir/bin"
  177. elif test -f "./bin/mysql"
  178. then
  179. bindir="./bin"
  180. else
  181. bindir="/usr/bin"
  182. fi
  183.  
  184. mysql_command=`find_in_basedir mysql $bindir`
  185. if test -z "$print_defaults"
  186. then
  187. cannot_find_file mysql $bindir
  188. exit 1
  189. fi
  190.  
  191. set_echo_compat() {
  192. case `echo "testing\c"`,`echo -n testing` in
  193. *c*,-n*) echo_n= echo_c= ;;
  194. *c*,*) echo_n=-n echo_c= ;;
  195. *) echo_n= echo_c='\c' ;;
  196. esac
  197. }
  198.  
  199. prepare() {
  200. touch $config $command
  201. chmod 600 $config $command
  202. }
  203.  
  204. do_query() {
  205. echo "$1" >$command
  206. #sed 's,^,> ,' < $command # Debugging
  207. $bindir/mysql --defaults-file=$config <$command
  208. return $?
  209. }
  210.  
  211. # Simple escape mechanism (\-escape any ' and \), suitable for two contexts:
  212. # - single-quoted SQL strings
  213. # - single-quoted option values on the right hand side of = in my.cnf
  214. #
  215. # These two contexts don't handle escapes identically. SQL strings allow
  216. # quoting any character (\C => C, for any C), but my.cnf parsing allows
  217. # quoting only \, ' or ". For example, password='a\b' quotes a 3-character
  218. # string in my.cnf, but a 2-character string in SQL.
  219. #
  220. # This simple escape works correctly in both places.
  221. basic_single_escape () {
  222. # The quoting on this sed command is a bit complex. Single-quoted strings
  223. # don't allow *any* escape mechanism, so they cannot contain a single
  224. # quote. The string sed gets (as argv[1]) is: s/\(['\]\)/\\\1/g
  225. #
  226. # Inside a character class, \ and ' are not special, so the ['\] character
  227. # class is balanced and contains two characters.
  228. echo "$1" | sed 's/\(['"'"'\]\)/\\\1/g'
  229. }
  230.  
  231. make_config() {
  232. echo "# mysql_secure_installation config file" >$config
  233. echo "[mysql]" >>$config
  234. echo "user=root" >>$config
  235. esc_pass=`basic_single_escape "$rootpass"`
  236. echo "password='$esc_pass'" >>$config
  237. #sed 's,^,> ,' < $config # Debugging
  238. }
  239.  
  240. get_root_password() {
  241. status=1
  242. while [ $status -eq 1 ]; do
  243. stty -echo
  244. echo $echo_n "Enter current password for root (enter for none): $echo_c"
  245. read password
  246. echo
  247. stty echo
  248. if [ "x$password" = "x" ]; then
  249. hadpass=0
  250. else
  251. hadpass=1
  252. fi
  253. rootpass=$password
  254. make_config
  255. do_query ""
  256. status=$?
  257. done
  258. echo "OK, successfully used password, moving on..."
  259. echo
  260. }
  261.  
  262. set_root_password() {
  263. stty -echo
  264. echo $echo_n "New password: $echo_c"
  265. read password1
  266. echo
  267. echo $echo_n "Re-enter new password: $echo_c"
  268. read password2
  269. echo
  270. stty echo
  271.  
  272. if [ "$password1" != "$password2" ]; then
  273. echo "Sorry, passwords do not match."
  274. echo
  275. return 1
  276. fi
  277.  
  278. if [ "$password1" = "" ]; then
  279. echo "Sorry, you can't use an empty password here."
  280. echo
  281. return 1
  282. fi
  283.  
  284. esc_pass=`basic_single_escape "$password1"`
  285. do_query "UPDATE mysql.user SET Password=PASSWORD('$esc_pass') WHERE User='root';"
  286. if [ $? -eq 0 ]; then
  287. echo "Password updated successfully!"
  288. echo "Reloading privilege tables.."
  289. reload_privilege_tables
  290. if [ $? -eq 1 ]; then
  291. clean_and_exit
  292. fi
  293. echo
  294. rootpass=$password1
  295. make_config
  296. else
  297. echo "Password update failed!"
  298. clean_and_exit
  299. fi
  300.  
  301. return 0
  302. }
  303.  
  304. remove_anonymous_users() {
  305. do_query "DELETE FROM mysql.user WHERE User='';"
  306. if [ $? -eq 0 ]; then
  307. echo " ... Success!"
  308. else
  309. echo " ... Failed!"
  310. clean_and_exit
  311. fi
  312.  
  313. return 0
  314. }
  315.  
  316. remove_remote_root() {
  317. do_query "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');"
  318. if [ $? -eq 0 ]; then
  319. echo " ... Success!"
  320. else
  321. echo " ... Failed!"
  322. fi
  323. }
  324.  
  325. remove_test_database() {
  326. echo " - Dropping test database..."
  327. do_query "DROP DATABASE test;"
  328. if [ $? -eq 0 ]; then
  329. echo " ... Success!"
  330. else
  331. echo " ... Failed! Not critical, keep moving..."
  332. fi
  333.  
  334. echo " - Removing privileges on test database..."
  335. do_query "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
  336. if [ $? -eq 0 ]; then
  337. echo " ... Success!"
  338. else
  339. echo " ... Failed! Not critical, keep moving..."
  340. fi
  341.  
  342. return 0
  343. }
  344.  
  345. reload_privilege_tables() {
  346. do_query "FLUSH PRIVILEGES;"
  347. if [ $? -eq 0 ]; then
  348. echo " ... Success!"
  349. return 0
  350. else
  351. echo " ... Failed!"
  352. return 1
  353. fi
  354. }
  355.  
  356. interrupt() {
  357. echo
  358. echo "Aborting!"
  359. echo
  360. cleanup
  361. stty echo
  362. exit 1
  363. }
  364.  
  365. cleanup() {
  366. echo "Cleaning up..."
  367. rm -f $config $command
  368. }
  369.  
  370. # Remove the files before exiting.
  371. clean_and_exit() {
  372. cleanup
  373. exit 1
  374. }
  375.  
  376. # The actual script starts here
  377.  
  378. prepare
  379. find_mysql_client
  380. set_echo_compat
  381.  
  382. echo
  383. echo "NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB"
  384. echo " SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!"
  385. echo
  386. echo "In order to log into MariaDB to secure it, we'll need the current"
  387. echo "password for the root user. If you've just installed MariaDB, and"
  388. echo "you haven't set the root password yet, the password will be blank,"
  389. echo "so you should just press enter here."
  390. echo
  391.  
  392. get_root_password
  393.  
  394.  
  395. #
  396. # Set the root password
  397. #
  398.  
  399. echo "Setting the root password ensures that nobody can log into the MariaDB"
  400. echo "root user without the proper authorisation."
  401. echo
  402.  
  403. if [ $hadpass -eq 0 ]; then
  404. echo $echo_n "Set root password? [Y/n] $echo_c"
  405. else
  406. echo "You already have a root password set, so you can safely answer 'n'."
  407. echo
  408. echo $echo_n "Change the root password? [Y/n] $echo_c"
  409. fi
  410.  
  411. read reply
  412. if [ "$reply" = "n" ]; then
  413. echo " ... skipping."
  414. else
  415. status=1
  416. while [ $status -eq 1 ]; do
  417. set_root_password
  418. status=$?
  419. done
  420. fi
  421. echo
  422.  
  423.  
  424. #
  425. # Remove anonymous users
  426. #
  427.  
  428. echo "By default, a MariaDB installation has an anonymous user, allowing anyone"
  429. echo "to log into MariaDB without having to have a user account created for"
  430. echo "them. This is intended only for testing, and to make the installation"
  431. echo "go a bit smoother. You should remove them before moving into a"
  432. echo "production environment."
  433. echo
  434.  
  435. echo $echo_n "Remove anonymous users? [Y/n] $echo_c"
  436.  
  437. read reply
  438. if [ "$reply" = "n" ]; then
  439. echo " ... skipping."
  440. else
  441. remove_anonymous_users
  442. fi
  443. echo
  444.  
  445.  
  446. #
  447. # Disallow remote root login
  448. #
  449.  
  450. echo "Normally, root should only be allowed to connect from 'localhost'. This"
  451. echo "ensures that someone cannot guess at the root password from the network."
  452. echo
  453.  
  454. echo $echo_n "Disallow root login remotely? [Y/n] $echo_c"
  455. read reply
  456. if [ "$reply" = "n" ]; then
  457. echo " ... skipping."
  458. else
  459. remove_remote_root
  460. fi
  461. echo
  462.  
  463.  
  464. #
  465. # Remove test database
  466. #
  467.  
  468. echo "By default, MariaDB comes with a database named 'test' that anyone can"
  469. echo "access. This is also intended only for testing, and should be removed"
  470. echo "before moving into a production environment."
  471. echo
  472.  
  473. echo $echo_n "Remove test database and access to it? [Y/n] $echo_c"
  474. read reply
  475. if [ "$reply" = "n" ]; then
  476. echo " ... skipping."
  477. else
  478. remove_test_database
  479. fi
  480. echo
  481.  
  482.  
  483. #
  484. # Reload privilege tables
  485. #
  486.  
  487. echo "Reloading the privilege tables will ensure that all changes made so far"
  488. echo "will take effect immediately."
  489. echo
  490.  
  491. echo $echo_n "Reload privilege tables now? [Y/n] $echo_c"
  492. read reply
  493. if [ "$reply" = "n" ]; then
  494. echo " ... skipping."
  495. else
  496. reload_privilege_tables
  497. fi
  498. echo
  499.  
  500. cleanup
  501.  
  502. echo
  503. echo "All done! If you've completed all of the above steps, your MariaDB"
  504. echo "installation should now be secure."
  505. echo
  506. echo "Thanks for using MariaDB!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement