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:
38 #include "prototypes.h"
43 #include "empthread.h"
55 #include <netinet/in.h>
59 extern struct cmndstr login_coms[];
64 player_login(void *ud)
71 player->proc = empth_self();
73 pr_id(player, C_INIT, "Empire server ready\n");
75 while (!io_eof(player->iop) && !io_error(player->iop)) {
76 io_output(player->iop, IO_WAIT);
77 if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
78 io_input(player->iop, IO_WAIT);
81 ac = parse(buf, player->argp, NULL, space, NULL);
82 cmd = comtch(player->argp[0], login_coms, 0);
84 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
87 switch (login_coms[cmd].c_addr()) {
93 pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form);
98 if (player->state >= PS_SHUTDOWN)
101 player->state = PS_SHUTDOWN;
102 if (!io_eof(player->iop)) {
103 pr_id(player, C_EXIT, "so long...\n");
104 io_noblocking(player->iop, 0);
105 while (io_output(player->iop, IO_WAIT) > 0) ;
107 player_delete(player);
117 if (!player->argp[1])
120 for (i = 1; player->argp[i]; ++i) {
122 strncat(player->client, " ", sizeof(player->client) - 1);
123 strncat(player->client, player->argp[i], sizeof(player->client) - 1);
125 player->client[sizeof(player->client) - 1] = '\0';
126 pr_id(player, C_CMDOK, "talking to %s\n", player->client);
133 if (!player->argp[1])
135 strncpy(player->userid, player->argp[1], sizeof(player->userid) - 1);
136 player->userid[sizeof(player->userid) - 1] = '\0';
137 pr_id(player, C_CMDOK, "hello %s\n", player->userid);
149 pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
153 snxtitem_all(&ni, EF_NATION);
154 while (nxtitem(&ni, &nat)) {
155 if (nat.nat_stat != STAT_SANCT)
158 pr_id(player, C_DATA,
159 "The following countries are still in sanctuary:\n");
162 pr_id(player, C_DATA, "%s\n", nat.nat_cnam);
165 pr_id(player, C_CMDOK, "There are no countries in sanctuary\n");
167 pr_id(player, C_CMDOK, "\n");
176 if (!player->argp[1])
178 if (natbyname(player->argp[1], &cnum) < 0) {
179 pr_id(player, C_CMDERR, "country %s does not exist\n", player->argp[1]);
183 player->validated = 0;
184 pr_id(player, C_CMDOK, "country name %s\n", player->argp[1]);
191 if (!player->argp[1])
193 if (player->cnum == 255) {
194 pr_id(player, C_CMDERR, "need country first\n");
197 if (!natpass(player->cnum, player->argp[1])) {
198 pr_id(player, C_CMDERR, "password bad, logging entry\n");
199 logerror("%s tried country #%d with %s",
200 praddr(player), player->cnum, player->argp[1]);
204 pr_id(player, C_CMDOK, "password ok\n");
205 logerror("%s using country #%d", praddr(player), player->cnum);
213 * The option mechanism allows arbitrary string values, but so far
214 * all options are flags in struct player. Should be easy to
215 * generalize if needed.
221 static struct logoptstr login_opts[] = {
229 if (!player->argp[1]) {
230 for (i = 0; i < sizeof(login_opts) / sizeof(*login_opts); ++i)
231 pr_id(player, C_DATA, "%s=%d\n",
233 (player->flags & login_opts[i].val) != 0);
234 pr_id(player, C_CMDOK, "\n");
238 for (ap = player->argp+1; *ap; ++ap) {
239 p = strchr(*ap, '=');
242 opt = stmtch(*ap, login_opts,
243 offsetof(struct logoptstr, name),
244 sizeof(struct logoptstr));
246 pr_id(player, C_BADCMD, "Option %s not found\n", *ap);
250 player->flags |= login_opts[opt].val;
252 player->flags &= ~login_opts[opt].val;
255 pr_id(player, C_CMDOK, "Accepted\n");
263 struct player *other;
270 strncpy(player->userid, *ap, sizeof(player->userid) - 1);
271 player->userid[sizeof(player->userid) - 1] = '\0';
272 player->validated = 0;
275 if (natbyname(*ap, &cnum) < 0) {
276 pr_id(player, C_CMDERR, "country %s does not exist\n", *ap);
281 if (!natpass(cnum, *ap)) {
282 pr_id(player, C_CMDERR, "password bad, logging entry\n");
283 logerror("%s tried country #%d with %s",
284 praddr(player), cnum, *ap);
290 if (player->cnum == 255 || !player->validated) {
291 pr_id(player, C_CMDERR, "need country and password\n");
294 other = getplayer((natid)player->cnum);
296 natp = getnatp(player->cnum);
297 if (natp->nat_stat != STAT_VIS) {
298 pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
300 pr_id(player, C_EXIT, "country in use\n");
304 if (match_user(banfil, player)) {
305 logerror("Attempted login by BANNED host %s", praddr(player));
306 pr("Your login has been banned from this game\n");
307 io_shutdown(player->iop, IO_READ);
310 logerror("%s logged in as country #%d", praddr(player), player->cnum);
311 pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
313 logerror("%s logged out, country #%d", praddr(player), player->cnum);
314 player->state = PS_SHUTDOWN;
321 struct player *other;
324 if (player->cnum == 255 || !player->validated) {
325 pr_id(player, C_CMDERR, "need country and password\n");
328 if (match_user(banfil, player)) {
329 logerror("Attempted login by BANNED host %s", praddr(player));
330 pr_id(player, C_EXIT, "Your login has been banned from this game\n");
331 io_shutdown(player->iop, IO_READ);
334 np = getnatp(player->cnum);
335 if (np->nat_stat == STAT_GOD && !match_user(authfil, player)) {
336 logerror("NON-AUTHed Login attempted by %s", praddr(player));
337 pr_id(player, C_EXIT, "You're not a deity!\n");
340 other = getplayer(player->cnum);
342 pr_id(player, C_EXIT, "country not in use\n");
345 logerror("%s killed country #%d", praddr(player), player->cnum);
346 io_shutdown(other->iop, IO_READ | IO_WRITE);
347 pr_id(player, C_EXIT, "closed socket of offending job\n");
354 pr_id(player, C_EXIT, "so long\n");
355 io_shutdown(player->iop, IO_READ);
359 struct cmndstr login_coms[] = {
360 {"client client-id...", 0, client_cmd, 0, 0},
361 {"coun country", 0, coun_cmd, 0, 0},
362 {"kill", 0, kill_cmd, 0, 0},
363 {"options option=value...", 0, options_cmd, 0, 0},
364 {"pass password", 0, pass_cmd, 0, 0},
365 {"play [user [country [password]]]", 0, play_cmd, 0, 0},
366 {"quit", 0, quit_cmd, 0, 0},
367 {"sanc", 0, sanc_cmd, 0, 0},
368 {"user name", 0, user_cmd, 0, 0},