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