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