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