When mixer is not available, recommend SDL2_mixer instead of SDL1.2 mixer
[freeciv.git] / scripts / setup_auth_server.sh
blobde9ef412ae96ced2b3b86326d68f5b20e2ea15e0
1 #!/bin/bash
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)
7 # any later version.
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
22 then
23 PROGRAM_NAME="$(basename $0)"
24 else
25 PROGRAM_NAME="setup_auth_server.sh"
27 PROGRAM_VERSION="0.10"
29 #############################################################################
31 # Function definitions
34 # Ask yes/no question
36 # $1 - Question
38 # 0 - Answer 'No'
39 # 1 - Answer 'Yes'
42 ask_yes_no() {
43 while /bin/true
45 ANSWER=""
47 echo -e $1
48 echo -n "(y/n) > "
50 read ANSWER
52 if test "x$ANSWER" = "xy" || test "x$ANSWER" = "xyes" ||
53 test "x$ANSWER" = "xY" || test "x$ANSWER" = "xYES" ||
54 test "x$ANSWER" = "xYes"
55 then
56 return 1
57 elif test "x$ANSWER" = "xn" || test "x$ANSWER" = "xno" ||
58 test "x$ANSWER" = "xN" || test "x$ANSWER" = "xNO" ||
59 test "x$ANSWER" = "xNo"
60 then
61 return 0
62 else
63 echo "Please answer 'y' or 'n'"
65 done
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.
77 make_query() {
78 declare -i MYSQL_RETURN
80 if test "x$MYSQL_SERVER" != "x"
81 then
82 MYSQL_SERVER_PARAM="-h$MYSQL_SERVER"
83 else
84 MYSQL_SERVER_PARAM=
86 if test "x$MYSQL_PORT" != "x"
87 then
88 MYSQL_PORT_PARAM="-P$MYSQL_PORT"
89 else
90 MYSQL_PORT_PARAM=
92 if test "x$MYSQL_DATABASE" != "x"
93 then
94 MYSQL_DATABASE_PARAM="-D$MYSQL_DATABASE"
95 else
96 MYSQL_DATABASE_PARAM=
98 if test "x$MYSQL_USER" != "x"
99 then
100 MYSQL_USER_PARAM="-u$MYSQL_USER"
101 else
102 MYSQL_USER_PARAM=
104 if test "x$MYSQL_PASSWORD" = "x*"
105 then
106 MYSQL_PASSWORD_PARAM="-p"
107 elif test "x$MYSQL_PASSWORD" != "x"
108 then
109 MYSQL_PASSWORD_PARAM="-p$MYSQL_PASSWORD"
110 else
111 MYSQL_PASSWORD_PARAM=
114 if test "x$1" != "x-"
115 then
116 echo "$1" | mysql $MYSQL_SERVER_PARAM $MYSQL_PORT_PARAM \
117 $MYSQL_USER_PARAM $MYSQL_PASSWORD_PARAM \
118 $MYSQL_DATABASE_PARAM
119 else
120 mysql $MYSQL_SERVER_PARAM $MYSQL_PORT_PARAM \
121 $MYSQL_USER_PARAM $MYSQL_PASSWORD_PARAM \
122 $MYSQL_DATABASE_PARAM
125 MYSQL_RETURN=$?
127 return $MYSQL_RETURN
130 print_usage() {
131 echo "Usage: $PROGRAM_NAME [-v|--version] [-h|--help]"
134 #############################################################################
136 # Script main
138 # Check for commandline parameters
140 if test "x$1" = "x-v" || test "x$1" = "x--version"
141 then
142 echo "$PROGRAM_NAME version $PROGRAM_VERSION"
144 # If we have several parameters fall through to usage, otherwise exit
145 if test "x$2" = "x"
146 then
147 exit 0
151 if test "x$1" != "x"
152 then
153 print_usage
155 if test "x$2" = "x"
156 then
157 if test "x$1" = "x-h" || test "x$1" = "x--help"
158 then
159 exit 0
163 exit 1
166 echo "This script helps in setup of Freeciv server with player authentication."
167 echo "Most users do not need player authentication."
168 echo
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?"
185 then
186 echo "Canceling before anything done"
187 exit
190 echo
191 echo "First we populate the player database on the MySQL server"
193 if ! which mysql >/dev/null
194 then
195 echo "mysql command not found. Aborting!"
196 exit 1
199 CONNECTED=no
200 MYSQL_SERVER="localhost"
201 MYSQL_PORT="3306"
202 MYSQL_USER="Freeciv"
203 MYSQL_PASSWORD=""
204 # We attempt connection without selecting database first
205 MYSQL_DATABASE=""
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)> "
213 read MYSQL_PORT_NEW
214 echo -n "username ($MYSQL_USER)> "
215 read MYSQL_USER_NEW
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"
223 then
224 MYSQL_SERVER="$MYSQL_SERVER_NEW"
226 if test "x$MYSQL_PORT_NEW" != "x"
227 then
228 MYSQL_PORT="$MYSQL_PORT_NEW"
230 if test "x$MYSQL_USER_NEW" != "x"
231 then
232 export MYSQL_USER="$MYSQL_USER_NEW"
234 if test "x$MYSQL_PASSWORD_NEW" != "x"
235 then
236 if test "x$MYSQL_PASSWORD_NEW" == "x-"
237 then
238 # No password
239 MYSQL_PASSWORD=""
240 else
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."
248 CONNECTED=yes
249 else
250 echo -e "\nConnection to MySQL server failed."
251 if ask_yes_no "\nRetry with changed parameters"
252 then
253 echo "Aborting!"
254 exit 1
257 done
259 echo "Next we select the database"
261 MYSQL_DATABASE="Freeciv"
262 DATABASE_SELECTED=no
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
268 # for this query
269 MYSQL_DATABASE_TMP="$MYSQL_DATABASE"
270 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
276 GREPRESULT=$?
278 # See if automatically proposed database is in the list
279 if test $GREPRESULT -eq 0
280 then
281 # Keep current default
282 MYSQL_DATABASE_TMP="$MYSQL_DATABASE_TMP"
283 else
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/^/ -/'
290 echo
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"
296 then
297 MYSQL_DATABASE="$MYSQL_DATABASE_NEW"
298 else
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
306 else
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?"
315 then
316 echo "Aborting!"
317 exit 1
320 done
322 # These are hardcoded here, and not prompted
323 TABLE_USER="auth"
324 TABLE_LOG="loginlog"
326 TABLELIST="$(make_query 'show tables' | grep -v '^Tables_in_')"
328 if test "x$TABLELIST" != "x"
329 then
330 echo "This database already contains some tables."
332 echo "$TABLELIST" | grep "$TABLE_USER" > /dev/null
333 GREPRESULT=$?
335 if test $GREPRESULT -eq 0
336 then
337 USER_TABLE_PRESENT=yes
338 else
339 USER_TABLE_PRESENT=no
342 echo "$TABLELIST" | grep "$TABLE_LOG" > /dev/null
343 GREPRESULT=$?
345 if test $GREPRESULT -eq 0
346 then
347 LOG_TABLE_PRESENT=yes
348 else
349 LOG_TABLE_PRESENT=no
352 if test $LOG_TABLE_PRESENT = yes ||
353 test $USER_TABLE_PRESENT = yes
354 then
355 echo "There are even tables with the names Freeciv would use."
356 if test $USER_TABLE_PRESENT = yes
357 then
358 echo " -$TABLE_USER"
360 if test $LOG_TABLE_PRESENT = yes
361 then
362 echo " -$TABLE_LOG"
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?"
373 then
374 echo "Can't continue because of conflicting tables."
375 exit 1
378 # Drop tables
379 if test $USER_TABLE_PRESENT = yes
380 then
381 if ! make_query "drop table $TABLE_USER"
382 then
383 echo "Dropping table $TABLE_USER failed!"
384 echo "Aborting!"
385 exit 1
388 if test $LOG_TABLE_PRESENT = yes
389 then
390 if ! make_query "drop table $TABLE_LOG"
391 then
392 echo "Dropping table $TABLE_LOG failed!"
393 echo "Aborting!"
394 exit 1
398 # Updated tablelist
399 TABLELIST="$(make_query 'show tables' | grep -v '^Tables_in_')"
400 if test "x$TABLELIST" != "x"
401 then
402 echo "After dropping Freeciv tables, others remain."
406 # Do we still have tables in that database?
407 if test "x$TABLELIST" != "x"
408 then
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?"
414 then
415 echo "Aborting!"
416 exit 1
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.
426 (echo \
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', \
437 PRIMARY KEY (id), \
438 UNIQUE KEY name (name) \
440 echo \
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', \
447 PRIMARY KEY (id) \
449 ) | make_query "-"
451 QUERYRESULT=$?
453 if test $QUERYRESULT -ne 0
454 then
455 echo "Creation of tables failed!"
456 echo "Aborting!"
457 exit 1
460 echo "Tables successfully created!"
462 CONFIG_FILE="./fc_auth.conf"
463 ACCEPTABLE_FILE=no
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) > "
470 read CONFIG_FILE_NEW
472 if test "x$CONFIG_FILE_NEW" != "x"
473 then
474 CONFIG_FILE="$CONFIG_FILE_NEW"
477 # Default is to test file creation
478 TRY_FILE=yes
479 if test -e "$CONFIG_FILE"
480 then
481 echo "$CONFIG_FILE already exists"
482 # Default is not to test overwriting
483 TRY_FILE=no
484 if test -d "$CONFIG_FILE"
485 then
486 echo "and it's a directory. Can't overwrite with file."
487 else
488 if ! ask_yes_no "\nOK to overwrite?"
489 then
490 TRY_FILE=yes
495 if test $TRY_FILE = "yes"
496 then
497 if touch "$CONFIG_FILE"
498 then
499 ACCEPTABLE_FILE=yes
500 else
501 echo "Can't write to that file"
502 if ask_yes_no "\nRetry with another file?"
503 then
504 echo "Aborting!"
505 exit 1
510 done
512 SAVE_PASSWORD=no
513 if test "x$MYSQL_PASSWORD" != "x" &&
514 test "x$MYSQL_PASSWORD" != "x-" &&
515 test "x$MYSQL_PASSWORD" != "x*"
516 then
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?"
526 then
527 SAVE_PASSWORD=yes
531 # Generate config file
533 echo "; Configuration file for Freeciv server player authentication"
534 echo "; Generated by \"$PROGRAM_NAME\" version $PROGRAM_VERSION"
535 echo
536 echo "[fcdb]"
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"
544 then
545 echo "password=\"$MYSQL_PASSWORD\""
546 else
547 echo ";password=\"\""
549 echo
550 echo "; What database to use in MySQL server"
551 echo "database=\"$MYSQL_DATABASE\""
552 echo
553 echo "; Table names"
554 echo "table_user=\"$TABLE_USER\""
555 echo "table_log=\"$TABLE_LOG\""
556 ) > $CONFIG_FILE
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"