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