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