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