]> git.pond.sub.org Git - empserver/blob - src/lib/player/login.c
(play_cmd): Log begin and end of playing session. Closes #911285.
[empserver] / src / lib / player / login.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2005, 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 the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23  *  related information and legal notices. It is expected that any future
24  *  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 <string.h>
36 #include "prototypes.h"
37 #include "misc.h"
38 #include "proto.h"
39 #include "player.h"
40 #include "com.h"
41 #include "empthread.h"
42 #include "empio.h"
43 #include "match.h"
44 #include "nsc.h"
45 #include "nat.h"
46 #include "optlist.h"
47 #include "file.h"
48 #include "subs.h"
49 #include "common.h"
50 #include "gen.h"
51
52 #ifdef aix
53 #include <unistd.h>
54 #endif /* aix */
55 #if !defined(_WIN32)
56 #include <netinet/in.h>
57 #endif
58 #include <errno.h>
59
60 extern struct cmndstr login_coms[];
61
62
63 /*ARGSUSED*/
64 void
65 player_login(void *ud)
66 {
67     char buf[128];
68     char space[128];
69     int ac;
70     int cmd;
71
72     player->proc = empth_self();
73
74     pr_id(player, C_INIT, "Empire server ready\n");
75
76     while (!io_eof(player->iop) && !io_error(player->iop)) {
77         io_output(player->iop, IO_WAIT);
78         if (io_gets(player->iop, buf, sizeof(buf)) < 0) {
79             io_input(player->iop, IO_WAIT);
80             continue;
81         }
82         ac = parse(buf, player->argp, NULL, space, NULL);
83         cmd = comtch(player->argp[0], login_coms, 0, 0);
84         if (cmd < 0) {
85             pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
86             continue;
87         }
88         switch (login_coms[cmd].c_addr()) {
89         case RET_OK:
90             break;
91         case RET_FAIL:
92             break;
93         case RET_SYN:
94             pr_id(player, C_BADCMD, "Usage %s\n", login_coms[cmd].c_form);
95             break;
96         default:
97             break;
98         }
99         if (player->state >= PS_SHUTDOWN)
100             break;
101     }
102     player->state = PS_SHUTDOWN;
103     if (!io_eof(player->iop)) {
104         pr_id(player, C_EXIT, "so long...\n");
105         io_noblocking(player->iop, 0);
106         while (io_output(player->iop, IO_WAIT) > 0) ;
107     }
108     player_delete(player);
109     empth_exit();
110     /*NOTREACHED*/
111 }
112
113 static int
114 client_cmd(void)
115 {
116     int i;
117
118     if (!player->argp[1])
119         return RET_SYN;
120
121     for (i = 1; player->argp[i]; ++i) {
122         if (i > 1)
123             strncat(player->client, " ", sizeof(player->client) - 1);
124         strncat(player->client, player->argp[i], sizeof(player->client) - 1);
125     }
126     player->client[sizeof(player->client) - 1] = '\0';
127     pr_id(player, C_CMDOK, "talking to %s\n", player->client);
128     return RET_OK;
129 }
130
131 static int
132 user_cmd(void)
133 {
134     if (!player->argp[1])
135         return RET_SYN;
136     strncpy(player->userid, player->argp[1], sizeof(player->userid) - 1);
137     player->userid[sizeof(player->userid) - 1] = '\0';
138     pr_id(player, C_CMDOK, "hello %s\n", player->userid);
139     return RET_OK;
140 }
141
142 static int
143 sanc_cmd(void)
144 {
145     struct nstr_item ni;
146     struct natstr nat;
147     int first = 1;
148
149     if (!opt_BLITZ) {
150         pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]);
151         return RET_FAIL;
152     }
153
154     snxtitem_all(&ni, EF_NATION);
155     while (nxtitem(&ni, &nat)) {
156         if (nat.nat_stat != (STAT_INUSE | STAT_SANCT))
157             continue;
158         if (first) {
159             pr_id(player, C_DATA,
160                   "The following countries are still in sanctuary:\n");
161             first = 0;
162         }
163         pr_id(player, C_DATA, "%s\n", nat.nat_cnam);
164     }
165     if (first)
166         pr_id(player, C_CMDOK, "There are no countries in sanctuary\n");
167     else
168         pr_id(player, C_CMDOK, "\n");
169     return RET_OK;
170 }
171
172 static int
173 coun_cmd(void)
174 {
175     natid cnum;
176
177     if (!player->argp[1])
178         return RET_SYN;
179     if (natbyname(player->argp[1], &cnum) < 0) {
180         pr_id(player, C_CMDERR, "country %s does not exist\n", player->argp[1]);
181         return RET_FAIL;
182     }
183     player->cnum = cnum;
184     player->validated = 0;
185     pr_id(player, C_CMDOK, "country name %s\n", player->argp[1]);
186     return 0;
187 }
188
189 static int
190 pass_cmd(void)
191 {
192     if (!player->argp[1])
193         return RET_SYN;
194     if (player->cnum == 255) {
195         pr_id(player, C_CMDERR, "need country first\n");
196         return RET_FAIL;
197     }
198     if (!natpass(player->cnum, player->argp[1])) {
199         pr_id(player, C_CMDERR, "password bad, logging entry\n");
200         logerror("%s tried country #%d with %s",
201                  praddr(player), player->cnum, player->argp[1]);
202         return RET_FAIL;
203     }
204     player->validated++;
205     pr_id(player, C_CMDOK, "password ok\n");
206     logerror("%s using country #%d", praddr(player), player->cnum);
207     return RET_OK;
208 }
209
210 static int
211 options_cmd(void)
212 {
213     /*
214      * The option mechanism allows arbitrary string values, but so far
215      * all options are flags in struct player.  Should be easy to
216      * generalize if needed.
217      */
218     struct logoptstr {
219         char *name;
220         int val;
221     };
222     static struct logoptstr login_opts[] = {
223         { "utf-8", PF_UTF8 }
224     };
225     char **ap;
226     char *p;
227     int opt;
228     unsigned i;
229
230     if (!player->argp[1]) {
231         for (i = 0; i < sizeof(login_opts) / sizeof(*login_opts); ++i)
232             pr_id(player, C_DATA, "%s=%d",
233                   login_opts[i].name,
234                   (player->flags & login_opts[i].val) != 0);
235         pr_id(player, C_CMDOK, "\n");
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 play_cmd(void)
262 {
263     struct player *other;
264     natid cnum;
265     struct natstr *natp;
266     char **ap;
267
268     ap = player->argp;
269     if (*++ap) {
270         strncpy(player->userid, *ap, sizeof(player->userid) - 1);
271         player->userid[sizeof(player->userid) - 1] = '\0';
272         player->validated = 0;
273     }
274     if (*++ap) {
275         if (natbyname(*ap, &cnum) < 0) {
276             pr_id(player, C_CMDERR, "country %s does not exist\n", *ap);
277             return RET_FAIL;
278         }
279     }
280     if (*++ap) {
281         if (!natpass(cnum, *ap)) {
282             pr_id(player, C_CMDERR, "password bad, logging entry\n");
283             logerror("%s tried country #%d with %s",
284                      praddr(player), cnum, *ap);
285             return RET_FAIL;
286         }
287         player->cnum = cnum;
288         player->validated++;
289     }
290     if (player->cnum == 255 || !player->validated) {
291         pr_id(player, C_CMDERR, "need country and password\n");
292         return RET_FAIL;
293     }
294     other = getplayer((natid)player->cnum);
295     if (other) {
296         natp = getnatp(player->cnum);
297         if (natp->nat_stat != VIS) {
298             pr_id(player, C_EXIT, "country in use by %s\n", praddr(other));
299         } else {
300             pr_id(player, C_EXIT, "country in use\n");
301         }
302         return RET_FAIL;
303     }
304     if (match_user(banfil, player)) {
305         logerror("Attempted login by BANNED host %s", praddr(player));
306         pr("Your login has been banned from this game\n");
307         io_shutdown(player->iop, IO_READ);
308         return RET_FAIL;
309     }
310     logerror("%s logged in as country #%d", praddr(player), player->cnum);
311     pr_id(player, C_INIT, "%d\n", CLIENTPROTO);
312     player_main(player);
313     logerror("%s logged out, country #%d", praddr(player), player->cnum);
314     player->state = PS_SHUTDOWN;
315     return RET_OK;
316 }
317
318 static int
319 kill_cmd(void)
320 {
321     struct player *other;
322
323     if (player->cnum == 255 || !player->validated) {
324         pr_id(player, C_CMDERR, "need country and password\n");
325         return RET_FAIL;
326     }
327     other = getplayer(player->cnum);
328     if (!other) {
329         pr_id(player, C_EXIT, "country not in use\n");
330         return RET_FAIL;
331     }
332     logerror("%s killed country #%d", praddr(player), player->cnum);
333     io_shutdown(other->iop, IO_READ | IO_WRITE);
334     pr_id(player, C_EXIT, "closed socket of offending job\n");
335     return RET_OK;
336 }
337
338 static int
339 quit_cmd(void)
340 {
341     pr_id(player, C_EXIT, "so long\n");
342     io_shutdown(player->iop, IO_READ);
343     return RET_OK;
344 }
345
346 struct cmndstr login_coms[] = {
347     {"client client-id...", 0, client_cmd, 0, 0},
348     {"coun country", 0, coun_cmd, 0, 0},
349     {"kill", 0, kill_cmd, 0, 0},
350     {"options option=value...", 0, options_cmd, 0, 0},
351     {"pass password", 0, pass_cmd, 0, 0},
352     {"play [user [country [password]]]", 0, play_cmd, 0, 0},
353     {"quit", 0, quit_cmd, 0, 0},
354     {"sanc", 0, sanc_cmd, 0, 0},
355     {"user name", 0, user_cmd, 0, 0},
356     {0, 0, 0, 0, 0}
357 };