]> git.pond.sub.org Git - empserver/blob - src/lib/player/login.c
(may_play): Clarify deity login rejection message.
[empserver] / src / lib / player / login.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  login.c: Allow the player to login
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1994
32  *     Steve McClure, 2000
33  */
34
35 #include <config.h>
36
37 #include <string.h>
38 #include "prototypes.h"
39 #include "misc.h"
40 #include "proto.h"
41 #include "player.h"
42 #include "com.h"
43 #include "empthread.h"
44 #include "empio.h"
45 #include "match.h"
46 #include "nsc.h"
47 #include "nat.h"
48 #include "optlist.h"
49 #include "file.h"
50 #include "subs.h"
51 #include "common.h"
52 #include "gen.h"
53
54 #if !defined(_WIN32)
55 #include <netinet/in.h>
56 #endif
57 #include <errno.h>
58
59 extern struct cmndstr login_coms[];
60
61
62 /*ARGSUSED*/
63 void
64 player_login(void *ud)
65 {
66     char buf[128];
67     char space[128];
68     int ac;
69     int cmd;
70
71     player->proc = empth_self();
72
73     pr_id(player, C_INIT, "Empire server ready\n");
74
75     while (!io_eof(player->iop) && !io_error(player->iop)) {
76         io_output(player->iop, IO_WAIT);
77         if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
78             io_input(player->iop, IO_WAIT);
79             continue;
80         }
81         ac = parse(buf, player->argp, NULL, space, NULL);
82         cmd = comtch(player->argp[0], login_coms, 0);
83         if (cmd < 0) {
84             pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
85             continue;
86         }
87         switch (login_coms[cmd].c_addr()) {
88         case RET_OK:
89             break;
90         case RET_FAIL:
91             break;
92         case RET_SYN:
93             pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form);
94             break;
95         default:
96             break;
97         }
98         if (player->state >= PS_SHUTDOWN)
99             break;
100     }
101     player->state = PS_SHUTDOWN;
102     if (!io_eof(player->iop)) {
103         pr_id(player, C_EXIT, "so long...\n");
104         io_noblocking(player->iop, 0);
105         while (io_output(player->iop, IO_WAIT) > 0) ;
106     }
107     player_delete(player);
108     empth_exit();
109     /*NOTREACHED*/
110 }
111
112 static int
113 client_cmd(void)
114 {
115     int i;
116
117     if (!player->argp[1])
118         return RET_SYN;
119
120     for (i = 1; player->argp[i]; ++i) {
121         if (i > 1)
122             strncat(player->client, " ", sizeof(player->client) - 1);
123         strncat(player->client, player->argp[i], sizeof(player->client) - 1);
124     }
125     player->client[sizeof(player->client) - 1] = '\0';
126     pr_id(player, C_CMDOK, "talking to %s\n", player->client);
127     return RET_OK;
128 }
129
130 static int
131 user_cmd(void)
132 {
133     if (!player->argp[1])
134         return RET_SYN;
135     strncpy(player->userid, player->argp[1], sizeof(player->userid) - 1);
136     player->userid[sizeof(player->userid) - 1] = '\0';
137     pr_id(player, C_CMDOK, "hello %s\n", player->userid);
138     return RET_OK;
139 }
140
141 static int
142 sanc_cmd(void)
143 {
144     struct nstr_item ni;
145     struct natstr nat;
146     int first = 1;
147
148     if (!opt_BLITZ) {
149         pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
150         return RET_FAIL;
151     }
152
153     snxtitem_all(&ni, EF_NATION);
154     while (nxtitem(&ni, &nat)) {
155         if (nat.nat_stat != STAT_SANCT)
156             continue;
157         if (first) {
158             pr_id(player, C_DATA,
159                   "The following countries are still in sanctuary:\n");
160             first = 0;
161         }
162         pr_id(player, C_DATA, "%s\n", nat.nat_cnam);
163     }
164     if (first)
165         pr_id(player, C_CMDOK, "There are no countries in sanctuary\n");
166     else
167         pr_id(player, C_CMDOK, "\n");
168     return RET_OK;
169 }
170
171 static int
172 coun_cmd(void)
173 {
174     natid cnum;
175
176     if (!player->argp[1])
177         return RET_SYN;
178     if (natbyname(player->argp[1], &cnum) < 0) {
179         pr_id(player, C_CMDERR, "country %s does not exist\n", player->argp[1]);
180         return RET_FAIL;
181     }
182     player->cnum = cnum;
183     player->authenticated = 0;
184     pr_id(player, C_CMDOK, "country name %s\n", player->argp[1]);
185     return 0;
186 }
187
188 static int
189 pass_cmd(void)
190 {
191     if (!player->argp[1])
192         return RET_SYN;
193     if (player->cnum == 255) {
194         pr_id(player, C_CMDERR, "need country first\n");
195         return RET_FAIL;
196     }
197     if (!natpass(player->cnum, player->argp[1])) {
198         pr_id(player, C_CMDERR, "password bad, logging entry\n");
199         logerror("%s tried country #%d with %s",
200                  praddr(player), player->cnum, player->argp[1]);
201         return RET_FAIL;
202     }
203     player->authenticated = 1;
204     pr_id(player, C_CMDOK, "password ok\n");
205     logerror("%s using country #%d", praddr(player), player->cnum);
206     return RET_OK;
207 }
208
209 static int
210 options_cmd(void)
211 {
212     /*
213      * The option mechanism allows arbitrary string values, but so far
214      * all options are flags in struct player.  Should be easy to
215      * generalize if needed.
216      */
217     struct logoptstr {
218         char *name;
219         int val;
220     };
221     static struct logoptstr login_opts[] = {
222         { "utf-8", PF_UTF8 }
223     };
224     char **ap;
225     char *p;
226     int opt;
227     unsigned i;
228
229     if (!player->argp[1]) {
230         for (i = 0; i < sizeof(login_opts) / sizeof(*login_opts); ++i)
231             pr_id(player, C_DATA, "%s=%d\n",
232                   login_opts[i].name,
233                   (player->flags & login_opts[i].val) != 0);
234         pr_id(player, C_CMDOK, "\n");
235         return RET_OK;
236     }
237
238     for (ap = player->argp+1; *ap; ++ap) {
239         p = strchr(*ap, '=');
240         if (p)
241             *p++ = 0;
242         opt = stmtch(*ap, login_opts,
243                      offsetof(struct logoptstr, name),
244                      sizeof(struct logoptstr));
245         if (opt < 0) {
246             pr_id(player, C_BADCMD, "Option %s not found\n", *ap);
247             return RET_FAIL;
248         }
249         if (!p || atoi(p))
250             player->flags |= login_opts[opt].val;
251         else
252             player->flags &= ~login_opts[opt].val;
253     }
254
255     pr_id(player, C_CMDOK, "Accepted\n");
256
257     return RET_OK;
258 }
259
260 static int
261 may_play(void)
262 {
263     struct natstr *np;
264
265     if (player->cnum == 255 || !player->authenticated) {
266         pr_id(player, C_CMDERR, "need country and password\n");
267         return 0;
268     }
269     /* TODO strstr() cheesy, compare IP against IP/BITS ... */
270     np = getnatp(player->cnum);
271     if (np->nat_stat == STAT_GOD && *privip
272         && !strstr(privip, player->hostaddr)) {
273         logerror("Deity login from untrusted host attempted by %s",
274                  praddr(player));
275         pr_id(player, C_EXIT,
276               "Deity login only from IPs in econfig key privip!\n");
277         return 0;
278     }
279     return 1;
280 }
281
282 static int
283 play_cmd(void)
284 {
285     struct player *other;
286     natid cnum;
287     struct natstr *natp;
288     char **ap;
289
290     ap = player->argp;
291     if (*++ap) {
292         strncpy(player->userid, *ap, sizeof(player->userid) - 1);
293         player->userid[sizeof(player->userid) - 1] = '\0';
294         player->authenticated = 0;
295     }
296     if (*++ap) {
297         if (natbyname(*ap, &cnum) < 0) {
298             pr_id(player, C_CMDERR, "country %s does not exist\n", *ap);
299             return RET_FAIL;
300         }
301     }
302     if (*++ap) {
303         if (!natpass(cnum, *ap)) {
304             pr_id(player, C_CMDERR, "password bad, logging entry\n");
305             logerror("%s tried country #%d with %s",
306                      praddr(player), cnum, *ap);
307             return RET_FAIL;
308         }
309         player->cnum = cnum;
310         player->authenticated = 1;
311     }
312     if (!may_play())
313         return RET_FAIL;
314     other = getplayer((natid)player->cnum);
315     if (other) {
316         natp = getnatp(player->cnum);
317         if (natp->nat_stat != STAT_VIS) {
318             pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
319         } else {
320             pr_id(player, C_EXIT, "country in use\n");
321         }
322         return RET_FAIL;
323     }
324     logerror("%s logged in as country #%d", praddr(player), player->cnum);
325     pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
326     player_main(player);
327     logerror("%s logged out, country #%d", praddr(player), player->cnum);
328     player->state = PS_SHUTDOWN;
329     return RET_OK;
330 }
331
332 static int
333 kill_cmd(void)
334 {
335     struct player *other;
336
337     if (!may_play())
338         return RET_FAIL;
339     other = getplayer(player->cnum);
340     if (!other) {
341         pr_id(player, C_EXIT, "country not in use\n");
342         return RET_FAIL;
343     }
344     logerror("%s killed country #%d", praddr(player), player->cnum);
345     io_shutdown(other->iop, IO_READ | IO_WRITE);
346     pr_id(player, C_EXIT, "closed socket of offending job\n");
347     return RET_OK;
348 }
349
350 static int
351 quit_cmd(void)
352 {
353     pr_id(player, C_EXIT, "so long\n");
354     io_shutdown(player->iop, IO_READ);
355     return RET_OK;
356 }
357
358 struct cmndstr login_coms[] = {
359     {"client client-id...", 0, client_cmd, 0, 0},
360     {"coun country", 0, coun_cmd, 0, 0},
361     {"kill", 0, kill_cmd, 0, 0},
362     {"options option=value...", 0, options_cmd, 0, 0},
363     {"pass password", 0, pass_cmd, 0, 0},
364     {"play [user [country [password]]]", 0, play_cmd, 0, 0},
365     {"quit", 0, quit_cmd, 0, 0},
366     {"sanc", 0, sanc_cmd, 0, 0},
367     {"user name", 0, user_cmd, 0, 0},
368     {0, 0, 0, 0, 0}
369 };