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