2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program 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 2 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * login.c: Allow the player to login
30 * Known contributors to this file:
33 * Markus Armbruster, 2004-2008
40 #include "empthread.h"
49 #include "prototypes.h"
51 static int client_cmd(void);
52 static int coun_cmd(void);
53 static int kill_cmd(void);
54 static int options_cmd(void);
55 static int pass_cmd(void);
56 static int play_cmd(void);
57 static int quit_cmd(void);
58 static int sanc_cmd(void);
59 static int user_cmd(void);
61 static struct cmndstr login_coms[] = {
62 {"client client-id...", 0, client_cmd, 0, 0},
63 {"coun country", 0, coun_cmd, 0, 0},
64 {"kill", 0, kill_cmd, 0, 0},
65 {"options option=value...", 0, options_cmd, 0, 0},
66 {"pass password", 0, pass_cmd, 0, 0},
67 {"play [user [country [password]]]", 0, play_cmd, 0, 0},
68 {"quit", 0, quit_cmd, 0, 0},
69 {"sanc", 0, sanc_cmd, 0, 0},
70 {"user name", 0, user_cmd, 0, 0},
76 player_login(void *ud)
84 player->proc = empth_self();
86 pr_id(player, C_INIT, "Empire server ready\n");
88 while (player->state != PS_SHUTDOWN) {
89 io_output(player->iop, IO_WAIT);
90 if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
91 res = io_input(player->iop, IO_WAIT);
93 if (res == 0 && !io_eof(player->iop))
94 pr_id(player, C_DATA, "idle connection terminated\n");
99 ac = parse(buf, space, player->argp, NULL, NULL, NULL);
100 cmd = comtch(player->argp[0], login_coms, 0);
102 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
105 switch (login_coms[cmd].c_addr()) {
111 pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form);
117 player->state = PS_SHUTDOWN;
118 if (!io_eof(player->iop)) {
119 pr_id(player, C_EXIT, "so long...\n");
120 io_noblocking(player->iop, 0);
121 while (io_output(player->iop, IO_WAIT) > 0) ;
123 player_delete(player);
134 if (!player->argp[1])
138 end = player->client + sizeof(player->client) - 1;
139 for (i = 1; player->argp[i]; ++i) {
142 sz = strlen(player->argp[i]);
143 sz = MIN(sz, end - p);
144 memcpy(p, player->argp[i], sz);
148 pr_id(player, C_CMDOK, "talking to %s\n", player->client);
155 if (!player->argp[1])
157 strncpy(player->userid, player->argp[1], sizeof(player->userid) - 1);
158 player->userid[sizeof(player->userid) - 1] = '\0';
159 pr_id(player, C_CMDOK, "hello %s\n", player->userid);
171 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
175 snxtitem_all(&ni, EF_NATION);
176 while (nxtitem(&ni, &nat)) {
177 if (nat.nat_stat != STAT_SANCT)
180 pr_id(player, C_DATA,
181 "The following countries are still in sanctuary:\n");
184 pr_id(player, C_DATA, "%s\n", nat.nat_cnam);
187 pr_id(player, C_CMDOK, "There are no countries in sanctuary\n");
189 pr_id(player, C_CMDOK, "\n");
198 if (!player->argp[1])
200 if (natbyname(player->argp[1], &cnum) < 0) {
201 pr_id(player, C_CMDERR, "country %s does not exist\n", player->argp[1]);
205 player->authenticated = 0;
206 pr_id(player, C_CMDOK, "country name %s\n", player->argp[1]);
213 if (!player->argp[1])
215 if (player->cnum == NATID_BAD) {
216 pr_id(player, C_CMDERR, "need country first\n");
219 if (!natpass(player->cnum, player->argp[1])) {
220 pr_id(player, C_CMDERR, "password bad, logging entry\n");
221 logerror("%s tried country #%d with %s",
222 praddr(player), player->cnum, player->argp[1]);
225 player->authenticated = 1;
226 pr_id(player, C_CMDOK, "password ok\n");
227 logerror("%s using country #%d", praddr(player), player->cnum);
235 * The option mechanism allows arbitrary string values, but so far
236 * all options are flags in struct player. Should be easy to
237 * generalize if needed.
243 static struct logoptstr login_opts[] = {
244 { "utf-8", PF_UTF8 },
252 if (!player->argp[1]) {
253 for (i = 0; login_opts[i].name; ++i)
254 pr_id(player, C_DATA, "%s=%d\n",
256 (player->flags & login_opts[i].val) != 0);
257 pr_id(player, C_CMDOK, "\n");
261 for (ap = player->argp+1; *ap; ++ap) {
262 p = strchr(*ap, '=');
265 opt = stmtch(*ap, login_opts,
266 offsetof(struct logoptstr, name),
267 sizeof(struct logoptstr));
269 pr_id(player, C_BADCMD, "Option %s not found\n", *ap);
273 player->flags |= login_opts[opt].val;
275 player->flags &= ~login_opts[opt].val;
278 pr_id(player, C_CMDOK, "Accepted\n");
288 if (player->cnum == NATID_BAD || !player->authenticated) {
289 pr_id(player, C_CMDERR, "need country and password\n");
292 /* TODO strstr() cheesy, compare IP against IP/BITS ... */
293 np = getnatp(player->cnum);
294 if (np->nat_stat == STAT_GOD && *privip
295 && !strstr(privip, player->hostaddr)) {
296 logerror("Deity login from untrusted host attempted by %s",
298 logerror("To allow this, add %s to econfig key privip",
300 pr_id(player, C_EXIT,
301 "Deity login not allowed from this IP!"
302 " See log for help on how to allow it.\n");
311 struct player *other;
319 strncpy(player->userid, *ap, sizeof(player->userid) - 1);
320 player->userid[sizeof(player->userid) - 1] = '\0';
321 player->authenticated = 0;
324 if (natbyname(*ap, &cnum) < 0) {
325 pr_id(player, C_CMDERR, "country %s does not exist\n", *ap);
330 if (!natpass(cnum, *ap)) {
331 pr_id(player, C_CMDERR, "password bad, logging entry\n");
332 logerror("%s tried country #%d with %s",
333 praddr(player), cnum, *ap);
337 player->authenticated = 1;
341 other = getplayer((natid)player->cnum);
343 natp = getnatp(player->cnum);
344 if (natp->nat_stat != STAT_VIS) {
345 pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
347 pr_id(player, C_EXIT, "country in use\n");
351 snprintf(buf, sizeof(buf), "Play#%d", player->cnum);
352 empth_set_name(empth_self(), buf);
353 logerror("%s logged in as country #%d", praddr(player), player->cnum);
354 pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
356 logerror("%s logged out, country #%d", praddr(player), player->cnum);
357 player->state = PS_SHUTDOWN;
364 struct player *other;
368 other = getplayer(player->cnum);
370 pr_id(player, C_EXIT, "country not in use\n");
373 logerror("%s killed country #%d", praddr(player), player->cnum);
374 io_shutdown(other->iop, IO_READ | IO_WRITE);
375 pr_id(player, C_EXIT, "closed socket of offending job\n");
382 pr_id(player, C_EXIT, "so long\n");
383 io_shutdown(player->iop, IO_READ);