]> git.pond.sub.org Git - empserver/blob - src/client/servcmd.c
Update copyright notice
[empserver] / src / client / servcmd.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, 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  *  servercmd.c: Change the state depending on the command from the server.
28  *
29  *  Known contributors to this file:
30  *     Dave Pare, 1989
31  *     Steve McClure, 1998
32  *     Ron Koenderink, 2005
33  *     Markus Armbruster, 2005-2010
34  */
35
36 #include <config.h>
37
38 #include <assert.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include "misc.h"
45 #include "proto.h"
46 #include "secure.h"
47
48 int eight_bit_clean;
49 FILE *auxfp;
50 int restricted;
51
52 static FILE *redir_fp;
53 static int redir_is_pipe;
54 static int executing;
55 static size_t input_to_forget;
56
57 static void prompt(int, char *, char *);
58 static void doredir(char *p);
59 static void dopipe(char *p);
60 static int doexecute(char *p);
61
62 void
63 servercmd(int code, char *arg, int len)
64 {
65     static int nmin, nbtu, fd;
66     static char the_prompt[1024];
67     static char teles[64];
68
69     switch (code) {
70     case C_PROMPT:
71         if (sscanf(arg, "%d %d", &nmin, &nbtu) != 2) {
72             fprintf(stderr, "prompt: bad server prompt %s\n", arg);
73         }
74         snprintf(the_prompt, sizeof(the_prompt), "[%d:%d] Command : ",
75                  nmin, nbtu);
76         if (redir_fp) {
77             if (redir_is_pipe)
78                 (void)pclose(redir_fp);
79             else
80                 (void)fclose(redir_fp);
81             redir_fp = NULL;
82         }
83         if (input_to_forget) {
84             forget_input(input_to_forget);
85             input_to_forget = 0;
86         }
87         prompt(code, the_prompt, teles);
88         executing = 0;
89         break;
90     case C_FLUSH:
91         snprintf(the_prompt, sizeof(the_prompt), "%.*s", len - 1, arg);
92         prompt(code, the_prompt, teles);
93         break;
94     case C_EXECUTE:
95         fd = doexecute(arg);
96         if (fd < 0)
97             send_eof++;
98         else {
99             input_fd = fd;
100             executing = 1;
101         }
102         break;
103     case C_EXIT:
104         printf("Exit: %s", arg);
105         if (auxfp)
106             fprintf(auxfp, "Exit: %s", arg);
107         break;
108     case C_FLASH:
109         printf("\n%s", arg);
110         if (auxfp)
111             fprintf(auxfp, "\n%s", arg);
112         break;
113     case C_INFORM:
114         if (arg[0] != '\n') {
115             snprintf(teles, sizeof(teles), "(%.*s) ", len - 1, arg);
116             if (!redir_fp) {
117                 putchar('\07');
118                 prompt(code, the_prompt, teles);
119             }
120         } else
121             teles[0] = 0;
122         break;
123     case C_PIPE:
124         dopipe(arg);
125         break;
126     case C_REDIR:
127         doredir(arg);
128         break;
129     default:
130         assert(0);
131         break;
132     }
133 }
134
135 static void
136 prompt(int code, char *prompt, char *teles)
137 {
138     char *nl;
139
140     nl = code == C_PROMPT || code == C_INFORM ? "\n" : "";
141     printf("%s%s%s", nl, teles, prompt);
142     fflush(stdout);
143     if (auxfp) {
144         fprintf(auxfp, "%s%s%s", nl, teles, prompt);
145         fflush(auxfp);
146     }
147 }
148
149 static char *
150 fname(char *s)
151 {
152     char *beg, *end;
153
154     for (beg = s; isspace(*(unsigned char *)beg); beg++) ;
155     for (end = beg; !isspace(*(unsigned char *)end); end++) ;
156     *end = 0;
157     return beg;
158 }
159
160 static int
161 redir_authorized(char *arg, char *attempt, int expected)
162 {
163     size_t seen = seen_input(arg);
164
165     if (restricted) {
166         fprintf(stderr, "Can't %s in restricted mode\n", attempt);
167         return 0;
168     }
169
170     if (executing) {
171         fprintf(stderr, "Can't %s in a batch file\n", attempt);
172         return 0;
173     }
174
175     if (!expected) {
176         fprintf(stderr, "WARNING!  Server attempted to %s unexpectedly\n",
177                 attempt);
178         return 0;
179     }
180
181     if (!seen || (input_to_forget && input_to_forget != seen)) {
182         fprintf(stderr, "WARNING!  Server attempted to %s %s\n",
183                 attempt, arg);
184         return 0;
185     }
186     input_to_forget = seen;
187     return 1;
188 }
189
190 static void
191 doredir(char *p)
192 {
193     int mode;
194     int fd;
195
196     if (!redir_authorized(p, "redirect to file", !redir_fp))
197         return;
198     if (*p++ != '>') {
199         fprintf(stderr, "WARNING!  Weird redirection %s", p);
200         return;
201     }
202
203     mode = O_WRONLY | O_CREAT;
204     if (*p == '>') {
205         mode |= O_APPEND;
206         p++;
207     } else if (*p == '!') {
208         mode |= O_TRUNC;
209         p++;
210     } else
211         mode |= O_EXCL;
212
213     p = fname(p);
214     if (*p == 0) {
215         fprintf(stderr, "Redirection lacks a file name\n");
216         return;
217     }
218
219     redir_is_pipe = 0;
220     fd = open(p, mode, 0666);
221     redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
222     if (!redir_fp) {
223         fprintf(stderr, "Can't redirect to %s: %s\n",
224                 p, strerror(errno));
225     }
226 }
227
228 static void
229 dopipe(char *p)
230 {
231     if (!redir_authorized(p, "pipe to shell command", !redir_fp))
232         return;
233     if (*p++ != '|') {
234         fprintf(stderr, "WARNING!  Weird pipe %s", p);
235         return;
236     }
237
238     for (; *p && isspace(*p); p++) ;
239     if (*p == 0) {
240         fprintf(stderr, "Redirection lacks a command\n");
241         return;
242     }
243
244     redir_is_pipe = 1;
245     if ((redir_fp = popen(p, "w")) == NULL) {
246         fprintf(stderr, "Can't redirect to pipe %s: %s\n",
247                 p, strerror(errno));
248     }
249 }
250
251 static int
252 doexecute(char *p)
253 {
254     int fd;
255
256     if (!redir_authorized(p, "execute batch file", 1))
257         return -1;
258
259     p = fname(p);
260     if (*p == 0) {
261         fprintf(stderr, "Need a file to execute\n");
262         return -1;
263     }
264
265     if ((fd = open(p, O_RDONLY)) < 0) {
266         fprintf(stderr, "Can't open execute file %s: %s\n",
267                 p, strerror(errno));
268         return -1;
269     }
270
271     return fd;
272 }
273
274 void
275 outch(char c)
276 {
277     if (auxfp)
278         putc(c, auxfp);
279     if (redir_fp)
280         putc(c, redir_fp);
281     else if (eight_bit_clean) {
282         if (c == 14)
283             putso();
284         else if (c == 15)
285             putse();
286         else
287             putchar(c);
288     } else if (c & 0x80) {
289         putso();
290         putchar(c & 0x7f);
291         putse();
292     } else
293         putchar(c);
294 }