1 ===========================================================================
2 Authentication and Freeciv database support (fcdb)
3 ===========================================================================
5 The Freeciv server allows the authentication of users, although by default
6 it is not enabled, and anyone can connect with any name.
8 In order to support authentication, the Freeciv server needs access to a
9 database backend in which to store the credentials. To support different
10 database backends, the database access code is written in Lua using luasql.
11 In principle, luasql supports SQLite3, MySQL, and Postgres backends, and
12 the Freeciv server can be built with any or all of these; however, the
13 supplied scripts currently do not support Postgres out of the box.
15 As well as storing and retrieving usernames and passwords, the supplied
16 database access script logs the time and IP address of each attempted
17 login, although this information is not used by the Freeciv server itself.
19 To use the Freeciv database and authentication, the server has to be built
20 with the '--enable-fcdb' and '--enable-auth' options to 'configure'. It
21 must also be installed properly, as it searches for database.lua in the
22 install location; the server cannot simply be run from a build directory if
23 authentication is required.
25 ================================
27 ================================
29 The simplest setup is to use the SQLite backend, and this is probably the
30 best option for new deployments. In this setup, the authentication data is
31 stored in a simple file accessed directly by the Freeciv server; there is
32 no need for a separate database server process.
34 In order to use this, the server must have been built with
35 '--enable-fcdb=sqlite3'.
37 To set this up, first create a database configuration file called something
38 like fc_auth.conf, with the 'database' key specifying where the database
39 file is to live (of course it must be readable and writable by the Freeciv
44 database="/my/path/to/freeciv.sqlite"
46 If you have experimental xml registry backend built in to freeciv, above
47 configuration can also be given as
49 <?xml version="1.0" encoding="UTF-8"?>
50 <Freeciv options="+xml">
52 <database>"/my/path/to/freeciv.sqlite"</database>
53 <backend>"sqlite"</backend>
57 (For more information on the format of this file, see below. There are more
58 settings available, but this file is entirely sufficient for a SQLite
61 Now start the server with
62 freeciv-server --Database fc_auth.conf --auth --Newusers
64 The first time you do this, you need to create the database file and its
65 tables with the following server command:
66 /fcdb lua sqlite_createdb()
68 Now you can create some users by connecting with the client; due to the
69 --Newusers flag, when you connect with the client with a previously unknown
70 username, the server will prompt for a password and save the new account to
73 You may want to prepopulate the users table this way and then restart the
74 server without --Newusers for the actual game, or you can run the game with
77 --------------------------------
79 --------------------------------
81 SQLite supports working with a temporary database in memory which is never
82 written to disk. To do this, specify 'database=":memory:"' in the
83 configuration file. The database will last only for the lifetime of the
84 freeciv-server process; its contents will be lost if the server quits or
85 crashes (it is not saved in the saved game file). You'll probably need the
88 ================================
90 ================================
92 This setup uses a network connection to a separate MySQL database server.
93 MySQL was the only backend supported before Freeciv version 2.4, so is
94 supported for backward compatibility, but SQLite is probably a better
95 option for new deployments unless you have special requirements.
97 In order to use this, the server must have been built with
98 '--enable-fcdb=mysql'.
100 You can still use an existing pre-2.4 MySQL authentication database with
101 Freeciv 2.4 or later. Note however that the format of the configuration
102 file has changed slightly from that which used to be supplied to the --auth
104 - Section header is now '[fcdb]' instead of '[auth]'
105 - You should add 'backend="mysql"'
106 - the 'table' directive is now called 'table_user', and the default table
107 name is now 'fcdb_auth'; if your setup relied on the old default, you'll
108 need 'table_user="auth"'
109 - the 'login_table' directive is now called 'table_log', and the default
110 table name is now 'fcdb_log'; if your setup relied on the old default,
111 you'll need 'table_log="loginlog"'
113 If you want to use MySQL for a fresh userbase, the supplied
114 scripts/setup_auth_server.sh can be used to create database tables in an
115 existing MySQL database, and a suitable config file describing that
116 database, which can be passed to the server's '--Database' option.
118 Before running this script, you will need to have a MySQL database server
119 running, with a user account for the Freeciv server and a database to
120 contain the data (ideally empty). (The space required for a Freeciv user
121 database is very small.)
123 Having created the tables and config file, you should be able to invoke the
124 server in a similar way to SQLite.
126 ================================
128 ================================
130 The following server command-line options are relevant to authentication:
132 -D, --Database <conffile>
133 Specifies a configuration file describing how to connect to the
134 database. Without this, all authentication will fail.
136 Enable authentication. Without this, anyone will be able to connect
137 without authentication, and --Database has no effect.
139 Allow guests. These are usernames with names starting "guest". If
140 enabled, any number of guests may connect without accounts in the
141 database; if a guest name is already in use by a connection, a new
142 guest name is generated.
143 Once connected, guests have the same privileges as any other account.
144 If this option is not specified, accounts are required to connect,
145 and guest account names are forbidden.
147 Allow Freeciv clients to create new user accounts through the Freeciv
149 Without this, only accounts which already exist in the database can
150 connect (which might be desirable if you wants users to register via
151 a web front end, for instance).
153 ================================
154 Lua script database.lua
155 ================================
157 This script is responsible for checking usernames, fetching passwords, and
158 saving new users (if --Newusers is enabled). It encapsulates access to the
159 database backend, and hence the details of the table layout.
161 The script lives in data/database.lua in the source tree, and is installed
162 to 'sysconfdir'; depending on the options given to 'configure' at build
163 time, this may be a location like /usr/local/etc/freeciv/database.lua.
165 The supplied version supports basic authentication against a SQLite or
166 MySQL database; it supports configuration as shown in the following
167 example (for MySQL; SQLite does not need all of these options).
179 If that's sufficient for you, it's not necessary to read on.
181 Freeciv expects the following lua functions to be defined in database.lua:
183 -- try to load data for an existing user
184 -- return TRUE if user exists, FALSE otherwise
185 function user_load(conn)
186 -- save a new user to the database
187 function user_save(conn)
188 -- log the connection attempt (success is boolean)
189 function user_log(conn, success)
191 -- test and initialise the database connection
192 function database_init()
193 -- free the database connection
194 function database_free()
196 Where 'conn' is on object representing the connection to the client which
199 The return status of all of these functions should be one of
205 indicating an error, a positive or a negative result.
207 The following lua functions are provided by Freeciv:
209 -- return the client-specified username
210 auth.get_username(conn)
211 -- return the client IP address (string)
212 auth.get_ipaddr(conn)
213 -- tell the server (the MD5 hash of) the correct password to check against
214 -- for this connection (usually to be called by user_load())
215 -- returns whether this succeeded
216 auth.set_password(conn, password)
217 -- return (the MD5 hash of) the password for this connection (as specified
218 -- by the client in user_save(), or as previously set by set_password()).
219 auth.get_password(conn)
221 -- return a value from the --Database configuration file
224 'type' selects one of the entries in the configuration file by name (for
225 instance fcdb.option("backend")). For backward compatibility with Freeciv
226 2.4, definitions such as fcdb.param.BACKEND are provided for conventional
227 option names, but there's no need to use them in new code.
229 Freeciv also provides some of the same Lua functions that ruleset scripts
230 get -- log.*(), _(), etc -- but the script is executing in a separate
231 context from ruleset scripts, and does not have access to signals, game
234 ================================
236 ================================
238 * Move password comparison / policy to Lua script
239 <http://gna.org/patch/?3386>
240 * Give database.lua access to do more stuff <http://gna.org/bugs/?19729>
241 * Allow setting cmdlevel per-user, etc <http://gna.org/patch/?3388>
242 * Give database.lua access to game signals and events
243 <http://gna.org/patch/?3385>
244 * Lua script for postgres <http://gna.org/patch/?3387>
245 * Improve this README