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-2011
33 * Ron Koenderink, 2005-2009
40 #include "empthread.h"
50 #include "prototypes.h"
52 static int client_cmd(void);
53 static int coun_cmd(void);
54 static int kill_cmd(void);
55 static int options_cmd(void);
56 static int pass_cmd(void);
57 static int play_cmd(void);
58 static int quit_cmd(void);
59 static int sanc_cmd(void);
60 static int user_cmd(void);
62 static struct cmndstr login_coms[] = {
63 {"client client-id...", 0, client_cmd, 0, 0},
64 {"coun country", 0, coun_cmd, 0, 0},
65 {"kill", 0, kill_cmd, 0, 0},
66 {"options option=value...", 0, options_cmd, 0, 0},
67 {"pass password", 0, pass_cmd, 0, 0},
68 {"play [user [country [password]]]", 0, play_cmd, 0, 0},
69 {"quit", 0, quit_cmd, 0, 0},
70 {"sanc", 0, sanc_cmd, 0, 0},
71 {"user name", 0, user_cmd, 0, 0},
77 player_login(void *ud)
85 player->proc = empth_self();
87 pr_id(player, C_INIT, "Empire server ready\n");
90 io_output(player->iop, 1);
91 if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
92 res = io_input(player->iop, 1);
94 if (res == 0 && !io_eof(player->iop))
95 pr_id(player, C_DATA, "idle connection terminated\n");
101 ac = parse(buf, space, player->argp, NULL, NULL, NULL);
103 pr_id(player, C_BADCMD, "Can't parse command\n");
106 cmd = comtch(player->argp[0], login_coms, 0);
108 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
111 switch (login_coms[cmd].c_addr()) {
117 pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form);
123 player->state = PS_SHUTDOWN;
124 pr_id(player, C_EXIT, "so long...\n");
125 while (io_output(player->iop, 1) > 0) ;
126 player_delete(player);
137 if (!player->argp[1])
141 end = player->client + sizeof(player->client) - 1;
142 for (i = 1; player->argp[i]; ++i) {
145 sz = strlen(player->argp[i]);
146 sz = MIN(sz, end - p);
147 memcpy(p, player->argp[i], sz);
151 pr_id(player, C_CMDOK, "talking to %s\n", player->client);
158 if (!player->argp[1])
160 strncpy(player->userid, player->argp[1], sizeof(player->userid) - 1);
161 player->userid[sizeof(player->userid) - 1] = '\0';
162 pr_id(player, C_CMDOK, "hello %s\n", player->userid);
174 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
178 snxtitem_all(&ni, EF_NATION);
179 while (nxtitem(&ni, &nat)) {
180 if (nat.nat_stat != STAT_SANCT)
183 pr_id(player, C_DATA,
184 "The following countries are still in sanctuary:\n");
187 pr_id(player, C_DATA, "%s\n", nat.nat_cnam);
190 pr_id(player, C_CMDOK, "There are no countries in sanctuary\n");
192 pr_id(player, C_CMDOK, "\n");
201 if (!player->argp[1])
203 if (natbyname(player->argp[1], &cnum) < 0) {
204 pr_id(player, C_CMDERR, "country %s does not exist\n", player->argp[1]);
208 player->authenticated = 0;
209 pr_id(player, C_CMDOK, "country name %s\n", player->argp[1]);
216 if (!player->argp[1])
218 if (player->cnum == NATID_BAD) {
219 pr_id(player, C_CMDERR, "need country first\n");
222 if (!natpass(player->cnum, player->argp[1])) {
223 pr_id(player, C_CMDERR, "password bad, logging entry\n");
224 logerror("%s tried country #%d with %s",
225 praddr(player), player->cnum, player->argp[1]);
228 player->authenticated = 1;
229 pr_id(player, C_CMDOK, "password ok\n");
230 logerror("%s using country #%d", praddr(player), player->cnum);
238 * The option mechanism allows arbitrary string values, but so far
239 * all options are flags in struct player. Should be easy to
240 * generalize if needed.
246 static struct logoptstr login_opts[] = {
247 { "utf-8", PF_UTF8 },
255 if (!player->argp[1]) {
256 for (i = 0; login_opts[i].name; ++i)
257 pr_id(player, C_DATA, "%s=%d\n",
259 (player->flags & login_opts[i].val) != 0);
260 pr_id(player, C_CMDOK, "\n");
264 for (ap = player->argp+1; *ap; ++ap) {
265 p = strchr(*ap, '=');
268 opt = stmtch(*ap, login_opts,
269 offsetof(struct logoptstr, name),
270 sizeof(struct logoptstr));
272 pr_id(player, C_BADCMD, "Option %s not found\n", *ap);
276 player->flags |= login_opts[opt].val;
278 player->flags &= ~login_opts[opt].val;
281 pr_id(player, C_CMDOK, "Accepted\n");
291 if (player->cnum == NATID_BAD || !player->authenticated) {
292 pr_id(player, C_CMDERR, "need country and password\n");
295 /* TODO strstr() cheesy, compare IP against IP/BITS ... */
296 np = getnatp(player->cnum);
297 if (np->nat_stat == STAT_GOD && *privip
298 && !strstr(privip, player->hostaddr)) {
299 logerror("Deity login from untrusted host attempted by %s",
301 logerror("To allow this, add %s to econfig key privip",
303 pr_id(player, C_EXIT,
304 "Deity login not allowed from this IP!"
305 " See log for help on how to allow it.\n");
314 struct player *other;
322 strncpy(player->userid, *ap, sizeof(player->userid) - 1);
323 player->userid[sizeof(player->userid) - 1] = '\0';
324 player->authenticated = 0;
327 if (natbyname(*ap, &cnum) < 0) {
328 pr_id(player, C_CMDERR, "country %s does not exist\n", *ap);
333 if (!natpass(cnum, *ap)) {
334 pr_id(player, C_CMDERR, "password bad, logging entry\n");
335 logerror("%s tried country #%d with %s",
336 praddr(player), cnum, *ap);
340 player->authenticated = 1;
344 other = getplayer(player->cnum);
346 natp = getnatp(player->cnum);
347 if (natp->nat_stat != STAT_VIS) {
348 pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
350 pr_id(player, C_EXIT, "country in use\n");
354 snprintf(buf, sizeof(buf), "Play#%d", player->cnum);
355 empth_set_name(empth_self(), buf);
356 logerror("%s logged in as country #%d", praddr(player), player->cnum);
357 pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
358 player->state = PS_PLAYING;
360 logerror("%s logged out, country #%d", praddr(player), player->cnum);
361 if (CANT_HAPPEN(!io_eof(player->iop)))
362 io_set_eof(player->iop);
369 struct player *other;
373 other = getplayer(player->cnum);
375 pr_id(player, C_EXIT, "country not in use\n");
378 logerror("%s killed country #%d", praddr(player), player->cnum);
379 io_shutdown(other->iop, IO_READ | IO_WRITE);
380 pr_id(player, C_EXIT, "closed socket of offending job\n");
387 io_set_eof(player->iop);