3 # Freeciv - Copyright (C) 2007 - Marko Lindqvist
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; either version 2, or (at your option)
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.
14 # Script to help setup of authentication enabled Freeciv server
15 # with MySQL. (For new deployments, consider using SQLite instead; it
16 # is much simpler and does not need this setup script.)
17 # See doc/README.fcdb.
18 # I know that this will not work with bare sh. Tested with bash,
19 # so I set it in use above.
21 if which basename >/dev
/null
23 PROGRAM_NAME
="$(basename $0)"
25 PROGRAM_NAME
="setup_auth_server.sh"
27 PROGRAM_VERSION
="0.10"
29 #############################################################################
31 # Function definitions
52 if test "x$ANSWER" = "xy" ||
test "x$ANSWER" = "xyes" ||
53 test "x$ANSWER" = "xY" ||
test "x$ANSWER" = "xYES" ||
54 test "x$ANSWER" = "xYes"
57 elif test "x$ANSWER" = "xn" ||
test "x$ANSWER" = "xno" ||
58 test "x$ANSWER" = "xN" ||
test "x$ANSWER" = "xNO" ||
59 test "x$ANSWER" = "xNo"
63 echo "Please answer 'y' or 'n'"
68 # Connects to Freeciv database on MySQL server and runs given query
69 # Determines connection parameters from environment variables.
70 # Special value "*" for MYSQL_PASSWORD causes mysql to prompt password.
72 # $1 - Query or "-" indicating query from stdin
74 # Output is what MySQL server shows. This function adds nothing.
78 declare -i MYSQL_RETURN
80 if test "x$MYSQL_SERVER" != "x"
82 MYSQL_SERVER_PARAM
="-h$MYSQL_SERVER"
86 if test "x$MYSQL_PORT" != "x"
88 MYSQL_PORT_PARAM
="-P$MYSQL_PORT"
92 if test "x$MYSQL_DATABASE" != "x"
94 MYSQL_DATABASE_PARAM
="-D$MYSQL_DATABASE"
98 if test "x$MYSQL_USER" != "x"
100 MYSQL_USER_PARAM
="-u$MYSQL_USER"
104 if test "x$MYSQL_PASSWORD" = "x*"
106 MYSQL_PASSWORD_PARAM
="-p"
107 elif test "x$MYSQL_PASSWORD" != "x"
109 MYSQL_PASSWORD_PARAM
="-p$MYSQL_PASSWORD"
111 MYSQL_PASSWORD_PARAM
=
114 if test "x$1" != "x-"
116 echo "$1" | mysql
$MYSQL_SERVER_PARAM $MYSQL_PORT_PARAM \
117 $MYSQL_USER_PARAM $MYSQL_PASSWORD_PARAM \
118 $MYSQL_DATABASE_PARAM
120 mysql
$MYSQL_SERVER_PARAM $MYSQL_PORT_PARAM \
121 $MYSQL_USER_PARAM $MYSQL_PASSWORD_PARAM \
122 $MYSQL_DATABASE_PARAM
131 echo "Usage: $PROGRAM_NAME [-v|--version] [-h|--help]"
134 #############################################################################
138 # Check for commandline parameters
140 if test "x$1" = "x-v" ||
test "x$1" = "x--version"
142 echo "$PROGRAM_NAME version $PROGRAM_VERSION"
144 # If we have several parameters fall through to usage, otherwise exit
157 if test "x$1" = "x-h" ||
test "x$1" = "x--help"
166 echo "This script helps in setup of Freeciv server with player authentication."
167 echo "Most users do not need player authentication."
169 echo "As set up by this script, authentication uses a MySQL database to store"
170 echo "player information."
171 echo "Before running this script you should have a MySQL server running"
172 echo "and that server should have:"
173 echo " - a user account to be used by freeciv-server"
174 echo " - an empty database, which this script will populate"
175 echo "The password for that account will be stored in a Freeciv server"
176 echo "config file, so you want to create special database user account just"
177 echo "for freeciv-server use."
178 echo "This script needs a MySQL user account that can create tables in to"
179 echo "that database. The generated config file for the Freeciv server will"
180 echo "contain the MySQL username used by this script, but it's easy to change"
181 echo "afterwards if you don't want use same account from this script and"
182 echo "later from the Freeciv server."
184 if ask_yes_no
"\nDo you want to continue with this script now?"
186 echo "Canceling before anything done"
191 echo "First we populate the player database on the MySQL server"
193 if ! which mysql
>/dev
/null
195 echo "mysql command not found. Aborting!"
200 MYSQL_SERVER
="localhost"
204 # We attempt connection without selecting database first
207 while test $CONNECTED = no
209 echo "Please answer questions determining how to contact MySQL server."
210 echo -n "server ($MYSQL_SERVER)> "
211 read MYSQL_SERVER_NEW
212 echo -n "port ($MYSQL_PORT)> "
214 echo -n "username ($MYSQL_USER)> "
216 echo "If password is not required, say \"-\"."
217 echo "If you want MySQL server to prompt it, say \"*\"."
218 echo "(you need to type actual password many times if you choose *)"
219 echo -n "password > "
220 read MYSQL_PASSWORD_NEW
222 if test "x$MYSQL_SERVER_NEW" != "x"
224 MYSQL_SERVER
="$MYSQL_SERVER_NEW"
226 if test "x$MYSQL_PORT_NEW" != "x"
228 MYSQL_PORT
="$MYSQL_PORT_NEW"
230 if test "x$MYSQL_USER_NEW" != "x"
232 export MYSQL_USER
="$MYSQL_USER_NEW"
234 if test "x$MYSQL_PASSWORD_NEW" != "x"
236 if test "x$MYSQL_PASSWORD_NEW" == "x-"
241 MYSQL_PASSWORD
="$MYSQL_PASSWORD_NEW"
245 # Just connect to server and exit if connect succeeded.
246 if make_query
"exit" ; then
247 echo "Connection test to MySQL server succeeded."
250 echo -e "\nConnection to MySQL server failed."
251 if ask_yes_no
"\nRetry with changed parameters"
259 echo "Next we select the database"
261 MYSQL_DATABASE
="Freeciv"
263 while test $DATABASE_SELECTED = no
265 echo "List of databases this user can see at MySQL server:"
267 # Make sure that make_query doesn't try to select any database
269 MYSQL_DATABASE_TMP
="$MYSQL_DATABASE"
272 # List Databases - remove header and internal database.
273 DBLIST
="$(make_query 'show databases' | grep -v '^Database$' | grep -v '^information_schema$')"
275 echo "$DBLIST" |
grep "$MYSQL_DATABASE_TMP" > /dev
/null
278 # See if automatically proposed database is in the list
279 if test $GREPRESULT -eq 0
281 # Keep current default
282 MYSQL_DATABASE_TMP
="$MYSQL_DATABASE_TMP"
284 # Select first one from the list
285 MYSQL_DATABASE_TMP
=$
(echo "$DBLIST" |
head -n 1)
288 # Start lines with " -"
289 echo "$DBLIST" |
sed 's/^/ -/'
291 echo "Please select which one to use."
292 echo -n "($MYSQL_DATABASE_TMP)> "
293 read MYSQL_DATABASE_NEW
295 if test "x$MYSQL_DATABASE_NEW" != "x"
297 MYSQL_DATABASE
="$MYSQL_DATABASE_NEW"
299 MYSQL_DATABASE
="$MYSQL_DATABASE_TMP"
302 # Try to connect using that database
303 if make_query
"exit" ; then
304 echo "Database successfully selected."
305 DATABASE_SELECTED
=yes
307 echo -e "\nCannot select that database."
309 # We could try to create database here.
310 # But we don't want anybody to think it's a
311 # good idea to use MySQL account with database
312 # creation permissions for Freeciv player authentication.
314 if ask_yes_no
"\nTry selecting some other database?"
322 # These are hardcoded here, and not prompted
326 TABLELIST
="$(make_query 'show tables' | grep -v '^Tables_in_')"
328 if test "x$TABLELIST" != "x"
330 echo "This database already contains some tables."
332 echo "$TABLELIST" |
grep "$TABLE_USER" > /dev
/null
335 if test $GREPRESULT -eq 0
337 USER_TABLE_PRESENT
=yes
339 USER_TABLE_PRESENT
=no
342 echo "$TABLELIST" |
grep "$TABLE_LOG" > /dev
/null
345 if test $GREPRESULT -eq 0
347 LOG_TABLE_PRESENT
=yes
352 if test $LOG_TABLE_PRESENT = yes ||
353 test $USER_TABLE_PRESENT = yes
355 echo "There are even tables with the names Freeciv would use."
356 if test $USER_TABLE_PRESENT = yes
360 if test $LOG_TABLE_PRESENT = yes
365 echo "Maybe you have already attempted to create Freeciv tables?"
366 echo "We can destroy existing tables and create new ones, if you really want."
367 echo "Just be sure that they indeed are Freeciv related tables AND if"
368 echo "they are Freeciv tables, they contain nothing important."
369 echo "If you have ever run Freeciv server with authentication, these"
370 echo "contain player accounts and login information."
372 if ask_yes_no
"\nDo you want tables destroyed, and DATA IN THEM PERMANENTLY LOST?"
374 echo "Can't continue because of conflicting tables."
379 if test $USER_TABLE_PRESENT = yes
381 if ! make_query
"drop table $TABLE_USER"
383 echo "Dropping table $TABLE_USER failed!"
388 if test $LOG_TABLE_PRESENT = yes
390 if ! make_query
"drop table $TABLE_LOG"
392 echo "Dropping table $TABLE_LOG failed!"
399 TABLELIST
="$(make_query 'show tables' | grep -v '^Tables_in_')"
400 if test "x$TABLELIST" != "x"
402 echo "After dropping Freeciv tables, others remain."
406 # Do we still have tables in that database?
407 if test "x$TABLELIST" != "x"
409 # Print them, each line starting with " -"
410 echo "$TABLELIST" |
sed 's/^/ -/'
411 echo "Table names do not conflict with tables Freeciv would use."
413 if ask_yes_no
"\nDo you really want to use this database for Freeciv\nplayer authentication?"
421 echo "Now we create the Freeciv tables."
423 # We have embedded table creation SQL to this script.
424 # Maybe we should read it from separate file in the future.
425 # The tables here are as the supplied data/database.lua expects to find them.
427 "CREATE TABLE $TABLE_USER ( \
428 id int(11) NOT NULL auto_increment, \
429 name varchar(48) default NULL, \
430 password varchar(32) default NULL, \
431 email varchar(128) default NULL, \
432 createtime int(11) default NULL, \
433 accesstime int(11) default NULL, \
434 address varchar(255) default NULL, \
435 createaddress varchar(255) default NULL, \
436 logincount int(11) default '0', \
438 UNIQUE KEY name (name) \
441 "CREATE TABLE $TABLE_LOG ( \
442 id int(11) NOT NULL auto_increment, \
443 name varchar(48) default NULL, \
444 logintime int(11) default NULL, \
445 address varchar(255) default NULL, \
446 succeed enum('S','F') default 'S', \
453 if test $QUERYRESULT -ne 0
455 echo "Creation of tables failed!"
460 echo "Tables successfully created!"
462 CONFIG_FILE
="./fc_auth.conf"
464 while test $ACCEPTABLE_FILE = no
466 echo "Give name for Freeciv server authentication config file we"
467 echo "are about to generate next."
469 echo -n "($CONFIG_FILE) > "
472 if test "x$CONFIG_FILE_NEW" != "x"
474 CONFIG_FILE
="$CONFIG_FILE_NEW"
477 # Default is to test file creation
479 if test -e "$CONFIG_FILE"
481 echo "$CONFIG_FILE already exists"
482 # Default is not to test overwriting
484 if test -d "$CONFIG_FILE"
486 echo "and it's a directory. Can't overwrite with file."
488 if ! ask_yes_no
"\nOK to overwrite?"
495 if test $TRY_FILE = "yes"
497 if touch "$CONFIG_FILE"
501 echo "Can't write to that file"
502 if ask_yes_no
"\nRetry with another file?"
513 if test "x$MYSQL_PASSWORD" != "x" &&
514 test "x$MYSQL_PASSWORD" != "x-" &&
515 test "x$MYSQL_PASSWORD" != "x*"
517 # User has given password for this script, should it also
518 # go to config script? If user has not given password even
519 # to this script, (s)he definitely does not want it saved.
520 echo "Freeciv server needs MySQL password from config file"
521 echo "in order to access database."
522 echo "It has to be added to config file before authentication"
523 echo "can be used. We can add it automatically now, or you"
524 echo "can add it manually later."
525 if ! ask_yes_no
"\nShould password be added to config file now?"
531 # Generate config file
533 echo "; Configuration file for Freeciv server player authentication"
534 echo "; Generated by \"$PROGRAM_NAME\" version $PROGRAM_VERSION"
537 echo "; Use MySQL server"
538 echo "backend=\"mysql\""
539 echo "; How to connect to MySQL server"
540 echo "host=\"$MYSQL_SERVER\""
541 echo "port=\"$MYSQL_PORT\" ; This is handled as string!"
542 echo "user=\"$MYSQL_USER\""
543 if test $SAVE_PASSWORD = "yes"
545 echo "password=\"$MYSQL_PASSWORD\""
547 echo ";password=\"\""
550 echo "; What database to use in MySQL server"
551 echo "database=\"$MYSQL_DATABASE\""
554 echo "table_user=\"$TABLE_USER\""
555 echo "table_log=\"$TABLE_LOG\""
558 echo "Config file generated."
559 echo "Auth server setup finished."
560 echo "To use the newly created database, run"
561 echo " freeciv-server --Database $CONFIG_FILE --auth"