2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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-2006
39 #include "prototypes.h"
44 #include "empthread.h"
56 #include <netinet/in.h>
60 static int client_cmd(void);
61 static int coun_cmd(void);
62 static int kill_cmd(void);
63 static int options_cmd(void);
64 static int pass_cmd(void);
65 static int play_cmd(void);
66 static int quit_cmd(void);
67 static int sanc_cmd(void);
68 static int user_cmd(void);
70 static struct cmndstr login_coms[] = {
71 {"client client-id...", 0, client_cmd, 0, 0},
72 {"coun country", 0, coun_cmd, 0, 0},
73 {"kill", 0, kill_cmd, 0, 0},
74 {"options option=value...", 0, options_cmd, 0, 0},
75 {"pass password", 0, pass_cmd, 0, 0},
76 {"play [user [country [password]]]", 0, play_cmd, 0, 0},
77 {"quit", 0, quit_cmd, 0, 0},
78 {"sanc", 0, sanc_cmd, 0, 0},
79 {"user name", 0, user_cmd, 0, 0},
85 player_login(void *ud)
92 player->proc = empth_self();
94 pr_id(player, C_INIT, "Empire server ready\n");
96 while (!io_eof(player->iop) && !io_error(player->iop)) {
97 io_output(player->iop, IO_WAIT);
98 if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
99 io_input(player->iop, IO_WAIT);
102 ac = parse(buf, player->argp, NULL, space, NULL);
103 cmd = comtch(player->argp[0], login_coms, 0);
105 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
108 switch (login_coms[cmd].c_addr()) {
114 pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form);
119 if (player->state >= PS_SHUTDOWN)
122 player->state = PS_SHUTDOWN;
123 if (!io_eof(player->iop)) {
124 pr_id(player, C_EXIT, "so long...\n");
125 io_noblocking(player->iop, 0);
126 while (io_output(player->iop, IO_WAIT) > 0) ;
128 player_delete(player);
138 if (!player->argp[1])
141 for (i = 1; player->argp[i]; ++i) {
143 strncat(player->client, " ", sizeof(player->client) - 1);
144 strncat(player->client, player->argp[i], sizeof(player->client) - 1);
146 player->client[sizeof(player->client) - 1] = '\0';
147 pr_id(player, C_CMDOK, "talking to %s\n", player->client);
154 if (!player->argp[1])
156 strncpy(player->userid, player->argp[1], sizeof(player->userid) - 1);
157 player->userid[sizeof(player->userid) - 1] = '\0';
158 pr_id(player, C_CMDOK, "hello %s\n", player->userid);
170 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
174 snxtitem_all(&ni, EF_NATION);
175 while (nxtitem(&ni, &nat)) {
176 if (nat.nat_stat != STAT_SANCT)
179 pr_id(player, C_DATA,
180 "The following countries are still in sanctuary:\n");
183 pr_id(player, C_DATA, "%s\n", nat.nat_cnam);
186 pr_id(player, C_CMDOK, "There are no countries in sanctuary\n");
188 pr_id(player, C_CMDOK, "\n");
197 if (!player->argp[1])
199 if (natbyname(player->argp[1], &cnum) < 0) {
200 pr_id(player, C_CMDERR, "country %s does not exist\n", player->argp[1]);
204 player->authenticated = 0;
205 pr_id(player, C_CMDOK, "country name %s\n", player->argp[1]);
212 if (!player->argp[1])
214 if (player->cnum == 255) {
215 pr_id(player, C_CMDERR, "need country first\n");
218 if (!natpass(player->cnum, player->argp[1])) {
219 pr_id(player, C_CMDERR, "password bad, logging entry\n");
220 logerror("%s tried country #%d with %s",
221 praddr(player), player->cnum, player->argp[1]);
224 player->authenticated = 1;
225 pr_id(player, C_CMDOK, "password ok\n");
226 logerror("%s using country #%d", praddr(player), player->cnum);
234 * The option mechanism allows arbitrary string values, but so far
235 * all options are flags in struct player. Should be easy to
236 * generalize if needed.
242 static struct logoptstr login_opts[] = {
250 if (!player->argp[1]) {
251 for (i = 0; i < sizeof(login_opts) / sizeof(*login_opts); ++i)
252 pr_id(player, C_DATA, "%s=%d\n",
254 (player->flags & login_opts[i].val) != 0);
255 pr_id(player, C_CMDOK, "\n");
259 for (ap = player->argp+1; *ap; ++ap) {
260 p = strchr(*ap, '=');
263 opt = stmtch(*ap, login_opts,
264 offsetof(struct logoptstr, name),
265 sizeof(struct logoptstr));
267 pr_id(player, C_BADCMD, "Option %s not found\n", *ap);
271 player->flags |= login_opts[opt].val;
273 player->flags &= ~login_opts[opt].val;
276 pr_id(player, C_CMDOK, "Accepted\n");
286 if (player->cnum == 255 || !player->authenticated) {
287 pr_id(player, C_CMDERR, "need country and password\n");
290 /* TODO strstr() cheesy, compare IP against IP/BITS ... */
291 np = getnatp(player->cnum);
292 if (np->nat_stat == STAT_GOD && *privip
293 && !strstr(privip, player->hostaddr)) {
294 logerror("Deity login from untrusted host attempted by %s",
296 logerror("To allow this, add %s to econfig key privip",
298 pr_id(player, C_EXIT,
299 "Deity login not allowed from this IP!"
300 " See log for help on how to allow it.\n");
309 struct player *other;
316 strncpy(player->userid, *ap, sizeof(player->userid) - 1);
317 player->userid[sizeof(player->userid) - 1] = '\0';
318 player->authenticated = 0;
321 if (natbyname(*ap, &cnum) < 0) {
322 pr_id(player, C_CMDERR, "country %s does not exist\n", *ap);
327 if (!natpass(cnum, *ap)) {
328 pr_id(player, C_CMDERR, "password bad, logging entry\n");
329 logerror("%s tried country #%d with %s",
330 praddr(player), cnum, *ap);
334 player->authenticated = 1;
338 other = getplayer((natid)player->cnum);
340 natp = getnatp(player->cnum);
341 if (natp->nat_stat != STAT_VIS) {
342 pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
344 pr_id(player, C_EXIT, "country in use\n");
348 logerror("%s logged in as country #%d", praddr(player), player->cnum);
349 pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
351 logerror("%s logged out, country #%d", praddr(player), player->cnum);
352 player->state = PS_SHUTDOWN;
359 struct player *other;
363 other = getplayer(player->cnum);
365 pr_id(player, C_EXIT, "country not in use\n");
368 logerror("%s killed country #%d", praddr(player), player->cnum);
369 io_shutdown(other->iop, IO_READ | IO_WRITE);
370 pr_id(player, C_EXIT, "closed socket of offending job\n");
377 pr_id(player, C_EXIT, "so long\n");
378 io_shutdown(player->iop, IO_READ);