]> git.pond.sub.org Git - empserver/blob - src/lib/player/login.c
Replace the per-iop input_timeout by per-function timeouts
[empserver] / src / lib / player / login.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  login.c: Allow the player to login
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1994
31  *     Steve McClure, 2000
32  *     Markus Armbruster, 2004-2012
33  *     Ron Koenderink, 2005-2009
34  */
35
36 #include <config.h>
37
38 #include "com.h"
39 #include "empio.h"
40 #include "empthread.h"
41 #include "file.h"
42 #include "journal.h"
43 #include "match.h"
44 #include "misc.h"
45 #include "nat.h"
46 #include "nsc.h"
47 #include "optlist.h"
48 #include "player.h"
49 #include "proto.h"
50 #include "prototypes.h"
51
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);
61
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},
72     {NULL, 0, NULL, 0, 0}
73 };
74
75 /*ARGSUSED*/
76 void
77 player_login(void *ud)
78 {
79     struct timeval timeout;
80     char buf[128];
81     char space[128];
82     int ac;
83     int cmd;
84     int res;
85
86     player->proc = empth_self();
87
88     pr_id(player, C_INIT, "Empire server ready\n");
89
90     for (;;) {
91         io_output(player->iop, 1);
92         if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
93             timeout.tv_sec = minutes(max_idle);
94             timeout.tv_usec = 0;
95             res = io_input(player->iop, &timeout);
96             if (res <= 0) {
97                 if (res == 0 && !io_eof(player->iop))
98                     pr_id(player, C_DATA, "idle connection terminated\n");
99                 break;
100             }
101             continue;
102         }
103         journal_input(buf);
104         ac = parse(buf, space, player->argp, NULL, NULL, NULL);
105         if (ac <= 0) {
106             pr_id(player, C_BADCMD, "Can't parse command\n");
107             continue;
108         }
109         cmd = comtch(player->argp[0], login_coms, 0);
110         if (cmd < 0) {
111             pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
112             continue;
113         }
114         switch (login_coms[cmd].c_addr()) {
115         case RET_OK:
116             break;
117         case RET_FAIL:
118             break;
119         case RET_SYN:
120             pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form);
121             break;
122         default:
123             break;
124         }
125     }
126     player->state = PS_SHUTDOWN;
127     pr_id(player, C_EXIT, "so long...\n");
128     player_delete(player);
129     empth_exit();
130     /*NOTREACHED*/
131 }
132
133 static int
134 client_cmd(void)
135 {
136     int i, sz;
137     char *p, *end;
138
139     if (!player->argp[1])
140         return RET_SYN;
141
142     p = player->client;
143     end = player->client + sizeof(player->client) - 1;
144     for (i = 1; player->argp[i]; ++i) {
145         if (i > 1)
146             *p++ = ' ';
147         sz = strlen(player->argp[i]);
148         sz = MIN(sz, end - p);
149         memcpy(p, player->argp[i], sz);
150         p += sz;
151     }
152     *p = 0;
153     pr_id(player, C_CMDOK, "talking to %s\n", player->client);
154     return RET_OK;
155 }
156
157 static int
158 user_cmd(void)
159 {
160     if (!player->argp[1])
161         return RET_SYN;
162     strncpy(player->userid, player->argp[1], sizeof(player->userid) - 1);
163     player->userid[sizeof(player->userid) - 1] = '\0';
164     pr_id(player, C_CMDOK, "hello %s\n", player->userid);
165     return RET_OK;
166 }
167
168 static int
169 sanc_cmd(void)
170 {
171     struct nstr_item ni;
172     struct natstr nat;
173     int first = 1;
174
175     if (!opt_BLITZ) {
176         pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
177         return RET_FAIL;
178     }
179
180     snxtitem_all(&ni, EF_NATION);
181     while (nxtitem(&ni, &nat)) {
182         if (nat.nat_stat != STAT_SANCT)
183             continue;
184         if (first) {
185             pr_id(player, C_DATA,
186                   "The following countries are still in sanctuary:\n");
187             first = 0;
188         }
189         pr_id(player, C_DATA, "%s\n", nat.nat_cnam);
190     }
191     if (first)
192         pr_id(player, C_CMDOK, "There are no countries in sanctuary\n");
193     else
194         pr_id(player, C_CMDOK, "\n");
195     return RET_OK;
196 }
197
198 static int
199 coun_cmd(void)
200 {
201     natid cnum;
202
203     if (!player->argp[1])
204         return RET_SYN;
205     if (natbyname(player->argp[1], &cnum) < 0) {
206         pr_id(player, C_CMDERR, "country %s does not exist\n", player->argp[1]);
207         return RET_FAIL;
208     }
209     player->cnum = cnum;
210     player->authenticated = 0;
211     pr_id(player, C_CMDOK, "country name %s\n", player->argp[1]);
212     return 0;
213 }
214
215 static int
216 pass_cmd(void)
217 {
218     if (!player->argp[1])
219         return RET_SYN;
220     if (player->cnum == NATID_BAD) {
221         pr_id(player, C_CMDERR, "need country first\n");
222         return RET_FAIL;
223     }
224     if (!natpass(player->cnum, player->argp[1])) {
225         pr_id(player, C_CMDERR, "password bad, logging entry\n");
226         logerror("%s tried country #%d with %s",
227                  praddr(player), player->cnum, player->argp[1]);
228         return RET_FAIL;
229     }
230     player->authenticated = 1;
231     pr_id(player, C_CMDOK, "password ok\n");
232     logerror("%s using country #%d", praddr(player), player->cnum);
233     return RET_OK;
234 }
235
236 static int
237 options_cmd(void)
238 {
239     /*
240      * The option mechanism allows arbitrary string values, but so far
241      * all options are flags in struct player.  Should be easy to
242      * generalize if needed.
243      */
244     struct logoptstr {
245         char *name;
246         int val;
247     };
248     static struct logoptstr login_opts[] = {
249         { "utf-8", PF_UTF8 },
250         { NULL, 0 }
251     };
252     char **ap;
253     char *p;
254     int opt;
255     unsigned i;
256
257     if (!player->argp[1]) {
258         for (i = 0; login_opts[i].name; ++i)
259             pr_id(player, C_DATA, "%s=%d\n",
260                   login_opts[i].name,
261                   (player->flags & login_opts[i].val) != 0);
262         pr_id(player, C_CMDOK, "\n");
263         return RET_OK;
264     }
265
266     for (ap = player->argp+1; *ap; ++ap) {
267         p = strchr(*ap, '=');
268         if (p)
269             *p++ = 0;
270         opt = stmtch(*ap, login_opts,
271                      offsetof(struct logoptstr, name),
272                      sizeof(struct logoptstr));
273         if (opt < 0) {
274             pr_id(player, C_BADCMD, "Option %s not found\n", *ap);
275             return RET_FAIL;
276         }
277         if (!p || atoi(p))
278             player->flags |= login_opts[opt].val;
279         else
280             player->flags &= ~login_opts[opt].val;
281     }
282
283     pr_id(player, C_CMDOK, "Accepted\n");
284
285     return RET_OK;
286 }
287
288 static int
289 may_play(void)
290 {
291     struct natstr *np;
292
293     if (player->cnum == NATID_BAD || !player->authenticated) {
294         pr_id(player, C_CMDERR, "need country and password\n");
295         return 0;
296     }
297     /* TODO strstr() cheesy, compare IP against IP/BITS ... */
298     np = getnatp(player->cnum);
299     if (np->nat_stat == STAT_GOD && *privip
300         && !strstr(privip, player->hostaddr)) {
301         logerror("Deity login from untrusted host attempted by %s",
302                  praddr(player));
303         logerror("To allow this, add %s to econfig key privip",
304                  player->hostaddr);
305         pr_id(player, C_EXIT,
306               "Deity login not allowed from this IP!"
307               "  See log for help on how to allow it.\n");
308         return 0;
309     }
310     return 1;
311 }
312
313 static int
314 play_cmd(void)
315 {
316     struct player *other;
317     natid cnum;
318     struct natstr *natp;
319     char **ap;
320     char buf[128];
321
322     ap = player->argp;
323     if (*++ap) {
324         strncpy(player->userid, *ap, sizeof(player->userid) - 1);
325         player->userid[sizeof(player->userid) - 1] = '\0';
326         player->authenticated = 0;
327     }
328     if (*++ap) {
329         if (natbyname(*ap, &cnum) < 0) {
330             pr_id(player, C_CMDERR, "country %s does not exist\n", *ap);
331             return RET_FAIL;
332         }
333     }
334     if (*++ap) {
335         if (!natpass(cnum, *ap)) {
336             pr_id(player, C_CMDERR, "password bad, logging entry\n");
337             logerror("%s tried country #%d with %s",
338                      praddr(player), cnum, *ap);
339             return RET_FAIL;
340         }
341         player->cnum = cnum;
342         player->authenticated = 1;
343     }
344     if (!may_play())
345         return RET_FAIL;
346     other = getplayer(player->cnum);
347     if (other) {
348         natp = getnatp(player->cnum);
349         if (natp->nat_stat != STAT_VIS) {
350             pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
351         } else {
352             pr_id(player, C_EXIT, "country in use\n");
353         }
354         return RET_FAIL;
355     }
356     snprintf(buf, sizeof(buf), "Play#%d", player->cnum);
357     empth_set_name(empth_self(), buf);
358     logerror("%s logged in as country #%d", praddr(player), player->cnum);
359     pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
360     player->state = PS_PLAYING;
361     player_main(player);
362     logerror("%s logged out, country #%d", praddr(player), player->cnum);
363     if (!io_eof(player->iop) && !io_error(player->iop))
364         io_set_eof(player->iop);
365     return RET_OK;
366 }
367
368 static int
369 kill_cmd(void)
370 {
371     struct player *other;
372
373     if (!may_play())
374         return RET_FAIL;
375     other = getplayer(player->cnum);
376     if (!other) {
377         pr_id(player, C_EXIT, "country not in use\n");
378         return RET_FAIL;
379     }
380     logerror("%s killed country #%d", praddr(player), player->cnum);
381     io_shutdown(other->iop, IO_READ | IO_WRITE);
382     pr_id(player, C_EXIT, "closed socket of offending job\n");
383     return RET_OK;
384 }
385
386 static int
387 quit_cmd(void)
388 {
389     io_set_eof(player->iop);
390     return RET_OK;
391 }