2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * login.c: Allow the player to login
29 * Known contributors to this file:
32 * Markus Armbruster, 2004-2012
33 * Ron Koenderink, 2005-2009
40 #include "empthread.h"
50 #include "prototypes.h"
53 static int client_cmd(void);
54 static int coun_cmd(void);
55 static int kill_cmd(void);
56 static int options_cmd(void);
57 static int pass_cmd(void);
58 static int play_cmd(void);
59 static int quit_cmd(void);
60 static int sanc_cmd(void);
61 static int user_cmd(void);
63 static struct cmndstr login_coms[] = {
64 {"client client-id...", 0, client_cmd, 0, 0},
65 {"coun country", 0, coun_cmd, 0, 0},
66 {"kill", 0, kill_cmd, 0, 0},
67 {"options option=value...", 0, options_cmd, 0, 0},
68 {"pass password", 0, pass_cmd, 0, 0},
69 {"play [user [country [password]]]", 0, play_cmd, 0, 0},
70 {"quit", 0, quit_cmd, 0, 0},
71 {"sanc", 0, sanc_cmd, 0, 0},
72 {"user name", 0, user_cmd, 0, 0},
78 player_login(void *ud)
83 int res, ac, cmd, prev_state;
85 player->proc = empth_self();
87 pr_id(player, C_INIT, "Empire server ready\n");
90 deadline = player_io_deadline(player, 0);
91 if (io_outputwaiting(player->iop)) {
92 if (io_output(player->iop, deadline) <= 0)
96 if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
97 res = io_input(player->iop, deadline);
99 if (res == 0 && !io_eof(player->iop))
100 pr_id(player, C_DATA, "idle connection terminated\n");
106 ac = parse(buf, space, player->argp, NULL, NULL, NULL);
108 pr_id(player, C_BADCMD, "Can't parse command\n");
111 cmd = comtch(player->argp[0], login_coms, 0);
113 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
116 switch (login_coms[cmd].c_addr()) {
122 pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form);
128 prev_state = player->state;
129 player->state = PS_SHUTDOWN;
130 if (prev_state == PS_PLAYING)
131 empth_rwlock_unlock(shutdown_lock);
132 pr_id(player, C_EXIT, "so long...\n");
133 player_delete(player);
144 if (!player->argp[1])
148 end = player->client + sizeof(player->client) - 1;
149 for (i = 1; player->argp[i]; ++i) {
152 sz = strlen(player->argp[i]);
153 sz = MIN(sz, end - p);
154 memcpy(p, player->argp[i], sz);
158 pr_id(player, C_CMDOK, "talking to %s\n", player->client);
165 if (!player->argp[1])
167 strncpy(player->userid, player->argp[1], sizeof(player->userid) - 1);
168 player->userid[sizeof(player->userid) - 1] = '\0';
169 pr_id(player, C_CMDOK, "hello %s\n", player->userid);
181 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
185 snxtitem_all(&ni, EF_NATION);
186 while (nxtitem(&ni, &nat)) {
187 if (nat.nat_stat != STAT_SANCT)
190 pr_id(player, C_DATA,
191 "The following countries are still in sanctuary:\n");
194 pr_id(player, C_DATA, "%s\n", nat.nat_cnam);
197 pr_id(player, C_CMDOK, "There are no countries in sanctuary\n");
199 pr_id(player, C_CMDOK, "\n");
208 if (!player->argp[1])
210 if (natbyname(player->argp[1], &cnum) < 0) {
211 pr_id(player, C_CMDERR, "country %s does not exist\n", player->argp[1]);
215 player->authenticated = 0;
216 pr_id(player, C_CMDOK, "country name %s\n", player->argp[1]);
223 if (!player->argp[1])
225 if (player->cnum == NATID_BAD) {
226 pr_id(player, C_CMDERR, "need country first\n");
229 if (!natpass(player->cnum, player->argp[1])) {
230 pr_id(player, C_CMDERR, "password bad, logging entry\n");
231 logerror("%s tried country #%d with %s",
232 praddr(player), player->cnum, player->argp[1]);
235 player->authenticated = 1;
236 pr_id(player, C_CMDOK, "password ok\n");
237 logerror("%s using country #%d", praddr(player), player->cnum);
245 * The option mechanism allows arbitrary string values, but so far
246 * all options are flags in struct player. Should be easy to
247 * generalize if needed.
253 static struct logoptstr login_opts[] = {
254 { "utf-8", PF_UTF8 },
262 if (!player->argp[1]) {
263 for (i = 0; login_opts[i].name; ++i)
264 pr_id(player, C_DATA, "%s=%d\n",
266 (player->flags & login_opts[i].val) != 0);
267 pr_id(player, C_CMDOK, "\n");
271 for (ap = player->argp+1; *ap; ++ap) {
272 p = strchr(*ap, '=');
275 opt = stmtch(*ap, login_opts,
276 offsetof(struct logoptstr, name),
277 sizeof(struct logoptstr));
279 pr_id(player, C_BADCMD, "Option %s not found\n", *ap);
283 player->flags |= login_opts[opt].val;
285 player->flags &= ~login_opts[opt].val;
288 pr_id(player, C_CMDOK, "Accepted\n");
298 if (player->cnum == NATID_BAD || !player->authenticated) {
299 pr_id(player, C_CMDERR, "need country and password\n");
302 /* TODO strstr() cheesy, compare IP against IP/BITS ... */
303 np = getnatp(player->cnum);
304 if (np->nat_stat == STAT_GOD && *privip
305 && !strstr(privip, player->hostaddr)) {
306 logerror("Deity login from untrusted host attempted by %s",
308 logerror("To allow this, add %s to econfig key privip",
310 pr_id(player, C_EXIT,
311 "Deity login not allowed from this IP!"
312 " See log for help on how to allow it.\n");
321 struct player *other;
329 strncpy(player->userid, *ap, sizeof(player->userid) - 1);
330 player->userid[sizeof(player->userid) - 1] = '\0';
331 player->authenticated = 0;
334 if (natbyname(*ap, &cnum) < 0) {
335 pr_id(player, C_CMDERR, "country %s does not exist\n", *ap);
340 if (!natpass(cnum, *ap)) {
341 pr_id(player, C_CMDERR, "password bad, logging entry\n");
342 logerror("%s tried country #%d with %s",
343 praddr(player), cnum, *ap);
347 player->authenticated = 1;
351 other = getplayer(player->cnum);
353 natp = getnatp(player->cnum);
354 if (natp->nat_stat != STAT_VIS) {
355 pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
357 pr_id(player, C_EXIT, "country in use\n");
361 snprintf(buf, sizeof(buf), "Play#%d", player->cnum);
362 empth_set_name(empth_self(), buf);
363 logerror("%s logged in as country #%d", praddr(player), player->cnum);
364 pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
365 empth_rwlock_rdlock(shutdown_lock);
366 player->state = PS_PLAYING;
368 logerror("%s logged out, country #%d", praddr(player), player->cnum);
369 if (!io_eof(player->iop) && !io_error(player->iop))
370 io_set_eof(player->iop);
377 struct player *other;
381 other = getplayer(player->cnum);
383 pr_id(player, C_EXIT, "country not in use\n");
386 logerror("%s killed country #%d", praddr(player), player->cnum);
387 pr_flash(other, "Disconnected by %s\n", praddr(player));
388 io_set_eof(other->iop);
390 other->may_sleep = PLAYER_SLEEP_NEVER;
391 empth_wakeup(other->proc);
392 pr_id(player, C_EXIT, "terminated %s's connection\n", praddr(other));
399 io_set_eof(player->iop);