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