1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
|
#!/usr/bin/env bash
# Freeciv - Copyright (C) 2007-2023 - Marko Lindqvist
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# Script to help setup of authentication enabled Freeciv server
# with MySQL. (For new deployments, consider using SQLite instead; it
# is much simpler and does not need this setup script.)
# See doc/README.fcdb.
# I know that this will not work with bare sh. Tested with bash,
# so I set it in use above.
if command -v basename >/dev/null
then
PROGRAM_NAME="$(basename $0)"
else
PROGRAM_NAME="setup_auth_server.sh"
fi
PROGRAM_VERSION="0.15"
#############################################################################
#
# Function definitions
#
# Ask yes/no question
#
# $1 - Question
#
# 0 - Answer 'No'
# 1 - Answer 'Yes'
#
ask_yes_no() {
while /bin/true
do
ANSWER=""
echo -e $1
echo -n "(y/n) > "
read ANSWER
if test "${ANSWER}" = "y" || test "${ANSWER}" = "yes" ||
test "${ANSWER}" = "Y" || test "${ANSWER}" = "YES" ||
test "${ANSWER}" = "Yes"
then
return 1
elif test "${ANSWER}" = "n" || test "${ANSWER}" = "no" ||
test "${ANSWER}" = "N" || test "${ANSWER}" = "NO" ||
test "${ANSWER}" = "No"
then
return 0
else
echo "Please answer 'y' or 'n'"
fi
done
}
# Connects to Freeciv database on MySQL server and runs given query
# Determines connection parameters from environment variables.
# Special value "*" for MYSQL_PASSWORD causes mysql to prompt password.
#
# $1 - Query or "-" indicating query from stdin
#
# Output is what MySQL server shows. This function adds nothing.
#
make_query() {
declare -i MYSQL_RETURN
if test "${MYSQL_SERVER}" != ""
then
MYSQL_SERVER_PARAM="-h${MYSQL_SERVER}"
else
MYSQL_SERVER_PARAM=
fi
if test "${MYSQL_PORT}" != ""
then
MYSQL_PORT_PARAM="-P${MYSQL_PORT}"
else
MYSQL_PORT_PARAM=
fi
if test "${MYSQL_DATABASE}" != ""
then
MYSQL_DATABASE_PARAM="-D${MYSQL_DATABASE}"
else
MYSQL_DATABASE_PARAM=
fi
if test "${MYSQL_USER}" != ""
then
MYSQL_USER_PARAM="-u${MYSQL_USER}"
else
MYSQL_USER_PARAM=
fi
if test "${MYSQL_PASSWORD}" = "*"
then
MYSQL_PASSWORD_PARAM="-p"
elif test "${MYSQL_PASSWORD}" != ""
then
MYSQL_PASSWORD_PARAM="-p${MYSQL_PASSWORD}"
else
MYSQL_PASSWORD_PARAM=
fi
if test "$1" != "-"
then
echo "$1" | mysql ${MYSQL_SERVER_PARAM} ${MYSQL_PORT_PARAM} \
${MYSQL_USER_PARAM} ${MYSQL_PASSWORD_PARAM} \
${MYSQL_DATABASE_PARAM}
else
mysql ${MYSQL_SERVER_PARAM} ${MYSQL_PORT_PARAM} \
${MYSQL_USER_PARAM} ${MYSQL_PASSWORD_PARAM} \
${MYSQL_DATABASE_PARAM}
fi
MYSQL_RETURN=$?
return ${MYSQL_RETURN}
}
print_usage() {
echo "Usage: ${PROGRAM_NAME} [-v|--version] [-h|--help]"
}
#############################################################################
#
# Script main
# Check for commandline parameters
if test "$1" = "-v" || test "$1" = "--version"
then
echo "${PROGRAM_NAME} version ${PROGRAM_VERSION}"
# If we have several parameters fall through to usage, otherwise exit
if test "$2" = ""
then
exit 0
fi
fi
if test "$1" != ""
then
print_usage
if test "$2" = ""
then
if test "$1" = "-h" || test "$1" = "--help"
then
exit 0
fi
fi
exit 1
fi
echo "This script helps in setup of Freeciv server with player authentication."
echo "Most users do not need player authentication."
echo
echo "As set up by this script, authentication uses a MySQL database to store"
echo "player information."
echo "Before running this script you should have a MySQL server running"
echo "and that server should have:"
echo " - a user account to be used by freeciv-server"
echo " - an empty database, which this script will populate"
echo "The password for that account will be stored in a Freeciv server"
echo "config file, so you want to create special database user account just"
echo "for freeciv-server use."
echo "This script needs a MySQL user account that can create tables in to"
echo "that database. The generated config file for the Freeciv server will"
echo "contain the MySQL username used by this script, but it's easy to change"
echo "afterwards if you don't want use same account from this script and"
echo "later from the Freeciv server."
if ask_yes_no "\nDo you want to continue with this script now?"
then
echo "Canceling before anything done"
exit
fi
echo
echo "First we populate the player database on the MySQL server"
if ! command -v mysql >/dev/null
then
echo "mysql command not found. Aborting!"
exit 1
fi
CONNECTED=no
MYSQL_SERVER="localhost"
MYSQL_PORT="3306"
MYSQL_USER="Freeciv"
MYSQL_PASSWORD=""
# We attempt connection without selecting database first
MYSQL_DATABASE=""
while test $CONNECTED = no
do
echo "Please answer questions determining how to contact MySQL server."
echo -n "server (${MYSQL_SERVER})> "
read MYSQL_SERVER_NEW
echo -n "port (${MYSQL_PORT})> "
read MYSQL_PORT_NEW
echo -n "username (${MYSQL_USER})> "
read MYSQL_USER_NEW
echo "If password is not required, say \"-\"."
echo "If you want MySQL server to prompt it, say \"*\"."
echo "(you need to type actual password many times if you choose *)"
echo -n "password > "
read MYSQL_PASSWORD_NEW
if test "${MYSQL_SERVER_NEW}" != ""
then
MYSQL_SERVER="${MYSQL_SERVER_NEW}"
fi
if test "${MYSQL_PORT_NEW}" != ""
then
MYSQL_PORT="${MYSQL_PORT_NEW}"
fi
if test "${MYSQL_USER_NEW}" != ""
then
export MYSQL_USER="${MYSQL_USER_NEW}"
fi
if test "${MYSQL_PASSWORD_NEW}" != ""
then
if test "${MYSQL_PASSWORD_NEW}" == "-"
then
# No password
MYSQL_PASSWORD=""
else
MYSQL_PASSWORD="$MYSQL_PASSWORD_NEW"
fi
fi
# Just connect to server and exit if connect succeeded.
if make_query "exit" ; then
echo "Connection test to MySQL server succeeded."
CONNECTED=yes
else
echo -e "\nConnection to MySQL server failed."
if ask_yes_no "\nRetry with changed parameters"
then
echo "Aborting!"
exit 1
fi
fi
done
echo "Next we select the database"
MYSQL_DATABASE="Freeciv"
DATABASE_SELECTED=no
while test $DATABASE_SELECTED = no
do
echo "List of databases this user can see at MySQL server:"
# Make sure that make_query doesn't try to select any database
# for this query
MYSQL_DATABASE_TMP="${MYSQL_DATABASE}"
MYSQL_DATABASE=""
# List Databases - remove header and internal database.
DBLIST="$(make_query 'show databases' | grep -v '^Database$' | grep -v '^information_schema$')"
echo "${DBLIST}" | grep "${MYSQL_DATABASE_TMP}" > /dev/null
GREPRESULT=$?
# See if automatically proposed database is in the list
if test $GREPRESULT -eq 0
then
# Keep current default
MYSQL_DATABASE_TMP="${MYSQL_DATABASE_TMP}"
else
# Select first one from the list
MYSQL_DATABASE_TMP=$(echo "${DBLIST}" | head -n 1)
fi
# Start lines with " -"
echo "${DBLIST}" | sed 's/^/ -/'
echo
echo "Please select which one to use."
echo -n "(${MYSQL_DATABASE_TMP})> "
read MYSQL_DATABASE_NEW
if test "${MYSQL_DATABASE_NEW}" != ""
then
MYSQL_DATABASE="${MYSQL_DATABASE_NEW}"
else
MYSQL_DATABASE="${MYSQL_DATABASE_TMP}"
fi
# Try to connect using that database
if make_query "exit" ; then
echo "Database successfully selected."
DATABASE_SELECTED=yes
else
echo -e "\nCannot select that database."
# We could try to create database here.
# But we don't want anybody to think it's a
# good idea to use MySQL account with database
# creation permissions for Freeciv player authentication.
if ask_yes_no "\nTry selecting some other database?"
then
echo "Aborting!"
exit 1
fi
fi
done
# These are hardcoded here, and not prompted
TABLE_USER="auth"
TABLE_LOG="loginlog"
TABLE_META="table_meta"
TABLELIST="$(make_query 'show tables' | grep -v '^Tables_in_')"
if test "${TABLELIST}" != ""
then
echo "This database already contains some tables."
echo "${TABLELIST}" | grep "${TABLE_USER}" > /dev/null
GREPRESULT=$?
if test $GREPRESULT -eq 0
then
USER_TABLE_PRESENT=yes
else
USER_TABLE_PRESENT=no
fi
echo "${TABLELIST}" | grep "${TABLE_LOG}" > /dev/null
GREPRESULT=$?
if test $GREPRESULT -eq 0
then
LOG_TABLE_PRESENT=yes
else
LOG_TABLE_PRESENT=no
fi
echo "${TABLELIST}" | grep "${TABLE_META}" > /dev/null
GREPRESULT=$?
if test $GREPRESULT -eq 0
then
META_TABLE_PRESENT=yes
else
META_TABLE_PRESENT=no
fi
if test "${LOG_TABLE_PRESENT}" = yes ||
test "${USER_TABLE_PRESENT}" = yes ||
test "${META_TABLE_PRESENT}" = yes
then
echo "There are even tables with the names Freeciv would use."
if test "${USER_TABLE_PRESENT}" = yes
then
echo " -${TABLE_USER}"
fi
if test "${LOG_TABLE_PRESENT}" = yes
then
echo " -${TABLE_LOG}"
fi
if test "${META_TABLE_PRESENT}" = yes
then
echo " -${TABLE_META}"
fi
echo "Maybe you have already attempted to create Freeciv tables?"
echo "We can destroy existing tables and create new ones, if you really want."
echo "Just be sure that they indeed are Freeciv related tables AND if"
echo "they are Freeciv tables, they contain nothing important."
echo "If you have ever run Freeciv server with authentication, these"
echo "contain player accounts and login information."
if ask_yes_no "\nDo you want tables destroyed, and DATA IN THEM PERMANENTLY LOST?"
then
echo "Can't continue because of conflicting tables."
exit 1
fi
# Drop tables
if test "${USER_TABLE_PRESENT}" = yes
then
if ! make_query "drop table ${TABLE_USER}"
then
echo "Dropping table ${TABLE_USER} failed!"
echo "Aborting!"
exit 1
fi
fi
if test "${LOG_TABLE_PRESENT}" = yes
then
if ! make_query "drop table ${TABLE_LOG}"
then
echo "Dropping table ${TABLE_LOG} failed!"
echo "Aborting!"
exit 1
fi
fi
if test "${META_TABLE_PRESENT}" = yes
then
if ! make_query "drop table ${TABLE_META}"
then
echo "Dropping table ${TABLE_META} failed!"
echo "Aborting!"
exit 1
fi
fi
# Updated tablelist
TABLELIST="$(make_query 'show tables' | grep -v '^Tables_in_')"
if test "${TABLELIST}" != ""
then
echo "After dropping Freeciv tables, others remain."
fi
fi
# Do we still have tables in that database?
if test "${TABLELIST}" != ""
then
# Print them, each line starting with " -"
echo "${TABLELIST}" | sed 's/^/ -/'
echo "Table names do not conflict with tables Freeciv would use."
if ask_yes_no "\nDo you really want to use this database for Freeciv\nplayer authentication?"
then
echo "Aborting!"
exit 1
fi
fi
fi
echo "Now we create the Freeciv tables."
# We have embedded table creation SQL to this script.
# Maybe we should read it from separate file in the future.
# The tables here are as the supplied data/database.lua expects to find them.
(echo \
"CREATE TABLE ${TABLE_META} ( \
capstr varchar(256) default NULL, \
gamecount int(11) default '0' \
);"
echo \
"CREATE TABLE ${TABLE_USER} ( \
id int(11) NOT NULL auto_increment, \
name varchar(48) default NULL, \
password varchar(32) default NULL, \
email varchar(128) default NULL, \
createtime int(11) default NULL, \
accesstime int(11) default NULL, \
address varchar(255) default NULL, \
createaddress varchar(255) default NULL, \
logincount int(11) default '0', \
PRIMARY KEY (id), \
UNIQUE KEY name (name) \
);"
echo \
"CREATE TABLE ${TABLE_LOG} ( \
id int(11) NOT NULL auto_increment, \
name varchar(48) default NULL, \
logintime int(11) default NULL, \
address varchar(255) default NULL, \
succeed enum('S','F') default 'S', \
PRIMARY KEY (id) \
);"
echo \
"INSERT INTO ${TABLE_META} VALUES ('+fcdb', 0);"
) | make_query "-"
QUERYRESULT=$?
if test $QUERYRESULT -ne 0
then
echo "Creation of tables failed!"
echo "Aborting!"
exit 1
fi
echo "Tables successfully created!"
CONFIG_FILE="./fc_auth.conf"
ACCEPTABLE_FILE=no
while test $ACCEPTABLE_FILE = no
do
echo "Give name for Freeciv server authentication config file we"
echo "are about to generate next."
echo -n "(${CONFIG_FILE}) > "
read CONFIG_FILE_NEW
if test "${CONFIG_FILE_NEW}" != ""
then
CONFIG_FILE="${CONFIG_FILE_NEW}"
fi
# Default is to test file creation
TRY_FILE=yes
if test -e "${CONFIG_FILE}"
then
echo "$CONFIG_FILE already exists"
# Default is not to test overwriting
TRY_FILE=no
if test -d "${CONFIG_FILE}"
then
echo "and it's a directory. Can't overwrite with file."
else
if ! ask_yes_no "\nOK to overwrite?"
then
TRY_FILE=yes
fi
fi
fi
if test $TRY_FILE = "yes"
then
if touch "${CONFIG_FILE}"
then
ACCEPTABLE_FILE=yes
else
echo "Can't write to that file"
if ask_yes_no "\nRetry with another file?"
then
echo "Aborting!"
exit 1
fi
fi
fi
done
SAVE_PASSWORD=no
if test "${MYSQL_PASSWORD}" != "" &&
test "${MYSQL_PASSWORD}" != "-" &&
test "${MYSQL_PASSWORD}" != "*"
then
# User has given password for this script, should it also
# go to config script? If user has not given password even
# to this script, they definitely do not want it saved.
echo "Freeciv server needs MySQL password from config file"
echo "in order to access database."
echo "It has to be added to config file before authentication"
echo "can be used. We can add it automatically now, or you"
echo "can add it manually later."
if ! ask_yes_no "\nShould password be added to config file now?"
then
SAVE_PASSWORD=yes
fi
fi
# Generate config file
(
echo "; Configuration file for Freeciv server player authentication"
echo "; Generated by \"$PROGRAM_NAME\" version $PROGRAM_VERSION"
echo
echo "[fcdb]"
echo "; Use MySQL server"
echo "backend=\"mysql\""
echo "; How to connect to MySQL server"
echo "host=\"$MYSQL_SERVER\""
echo "port=\"$MYSQL_PORT\" ; This is handled as string!"
echo "user=\"$MYSQL_USER\""
if test $SAVE_PASSWORD = "yes"
then
echo "password=\"$MYSQL_PASSWORD\""
else
echo ";password=\"\""
fi
echo
echo "; What database to use in MySQL server"
echo "database=\"$MYSQL_DATABASE\""
echo
echo "; Table names"
echo "table_user=\"${TABLE_USER}\""
echo "table_log=\"${TABLE_LOG}\""
echo "table_meta=\"${TABLE_META}\""
) > "${CONFIG_FILE}"
echo "Config file generated."
echo "Auth server setup finished."
echo "To use the newly created database, run"
echo " freeciv-server --Database \"${CONFIG_FILE}\" --auth"
|