2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * login.c: Allow the player to login
30 * Known contributors to this file:
36 #include "prototypes.h"
41 #include "empthread.h"
56 #include <netinet/in.h>
60 extern struct cmndstr login_coms[];
65 player_login(void *ud)
72 player->proc = empth_self();
74 pr_id(player, C_INIT, "Empire server ready\n");
76 while (!io_eof(player->iop) && !io_error(player->iop)) {
77 io_output(player->iop, IO_WAIT);
78 if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
79 io_input(player->iop, IO_WAIT);
82 ac = parse(buf, player->argp, NULL, space, NULL);
83 cmd = comtch(player->argp[0], login_coms, 0, 0);
85 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
88 switch (login_coms[cmd].c_addr()) {
94 pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form);
99 if (player->state >= PS_SHUTDOWN)
102 player->state = PS_SHUTDOWN;
103 if (!io_eof(player->iop)) {
104 pr_player(player, C_EXIT, "so long...\n");
105 io_noblocking(player->iop, 0);
106 while (io_output(player->iop, IO_WAIT) > 0) ;
108 player_delete(player);
118 if (!player->argp[1])
121 for (i = 1; player->argp[i]; ++i) {
123 strncat(player->client, " ", sizeof(player->client) - 1);
124 strncat(player->client, player->argp[i], sizeof(player->client) - 1);
126 player->client[sizeof(player->client) - 1] = '\0';
127 pr_id(player, C_CMDOK, "talking to %s\n", player->client);
134 if (!player->argp[1])
136 strncpy(player->userid, player->argp[1], sizeof(player->userid) - 1);
137 player->userid[sizeof(player->userid) - 1] = '\0';
138 pr_id(player, C_CMDOK, "hello %s\n", player->userid);
150 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
154 snxtitem_all(&ni, EF_NATION);
155 while (nxtitem(&ni, (s_char *)&nat)) {
156 if (nat.nat_stat != (STAT_INUSE | STAT_SANCT))
159 pr_id(player, C_DATA,
160 "The following countries are still in sanctuary:\n");
163 pr_id(player, C_DATA, "%s\n", nat.nat_cnam);
166 pr_id(player, C_CMDOK, "There are no countries in sanctuary\n");
168 pr_id(player, C_CMDOK, "\n");
177 if (!player->argp[1])
179 if (natbyname(player->argp[1], &cnum) < 0) {
180 pr_id(player, C_CMDERR, "country %s does not exist\n", player->argp[1]);
184 player->validated = 0;
185 pr_id(player, C_CMDOK, "country name %s\n", player->argp[1]);
192 if (!player->argp[1])
194 if (player->cnum == 255) {
195 pr_id(player, C_CMDERR, "need country first\n");
198 if (!natpass(player->cnum, player->argp[1])) {
199 pr_id(player, C_CMDERR, "password bad, logging entry\n");
200 logerror("%s tried country #%d with %s",
201 praddr(player), player->cnum, player->argp[1]);
205 pr_id(player, C_CMDOK, "password ok\n");
206 logerror("%s using country #%d", praddr(player), player->cnum);
214 * The login option mechanism allows arbitrary string values, but
215 * so far all options are flags in struct player. Should be easy
216 * to generalize if needed.
222 static struct logoptstr login_opts[] = {
230 for (ap = player->argp+1; *ap; ++ap) {
231 p = strchr(*ap, '=');
234 opt = stmtch(*ap, login_opts,
235 offsetof(struct logoptstr, name),
236 sizeof(struct logoptstr));
238 pr_id(player, C_BADCMD, "Option %s not found\n", *ap);
242 player->flags |= login_opts[opt].val;
244 player->flags &= ~login_opts[opt].val;
247 for (i = 0; i < sizeof(login_opts) / sizeof(*login_opts); ++i)
248 pr_id(player, C_DATA, "%s=%d",
250 (player->flags & login_opts[i].val) != 0);
251 pr_id(player, C_CMDOK, "Options okay\n");
259 struct player *other;
266 strncpy(player->userid, *ap, sizeof(player->userid) - 1);
267 player->userid[sizeof(player->userid) - 1] = '\0';
268 player->validated = 0;
271 if (natbyname(*ap, &cnum) < 0) {
272 pr_id(player, C_CMDERR, "country %s does not exist\n", *ap);
277 if (!natpass(cnum, *ap)) {
278 pr_id(player, C_CMDERR, "password bad, logging entry\n");
279 logerror("%s tried country #%d with %s",
280 praddr(player), cnum, *ap);
286 if (player->cnum == 255 || !player->validated) {
287 pr_id(player, C_CMDERR, "need country and password\n");
290 if ((other = player_find_other(player, (natid)player->cnum)) != 0) {
291 natp = getnatp(player->cnum);
292 if (natp->nat_stat != VIS) {
293 pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
295 pr_id(player, C_EXIT, "country in use\n");
299 if (match_user(banfil, player)) {
300 logerror("Attempted login by BANNED host %s", praddr(player));
301 pr("Your login has been banned from this game\n");
302 io_shutdown(player->iop, IO_READ);
306 player->state = PS_SHUTDOWN;
313 struct player *other;
315 if (player->cnum == 255 || !player->validated) {
316 pr_id(player, C_CMDERR, "need country and password\n");
319 /* XXX find cnum other than us */
320 if ((other = player_find_other(player, player->cnum)) == 0) {
321 pr_id(player, C_EXIT, "country not in use\n");
324 io_shutdown(other->iop, IO_READ | IO_WRITE);
325 pr_id(player, C_EXIT, "closed socket of offending job\n");
335 if (player->cnum != 0 || !player->validated) {
336 pr_id(player, C_CMDERR, "Permission denied\n");
339 for (lp = player_next(0); lp != 0; lp = player_next(lp)) {
341 pr_id(player, C_DATA, "user@host\tcountry\tpid\n");
344 pr_id(player, C_DATA, "%s\t%d\n", praddr(lp), lp->cnum);
347 pr_id(player, C_DATA, "\n");
354 pr_id(player, C_EXIT, "so long\n");
355 io_shutdown(player->iop, IO_READ);
359 struct cmndstr login_coms[] = {
360 {"client clientname [version info]", 0, client_cmd, 0, 0},
361 {"coun countryname", 0, coun_cmd, 0, 0},
362 {"kill", 0, kill_cmd, 0, 0},
363 {"list", 0, list_cmd, 0, 0},
364 {"options", 0, options_cmd, 0, 0},
365 {"pass password", 0, pass_cmd, 0, 0},
366 {"play [user country pass]", 0, play_cmd, 0, 0},
367 {"quit", 0, quit_cmd, 0, 0},
368 {"sanc", 0, sanc_cmd, 0, 0},
369 {"user username", 0, user_cmd, 0, 0},