]> git.pond.sub.org Git - empserver/blob - src/client/servcmd.c
(doexecute) [_WIN32]: Open the exec file in text mode.
[empserver] / src / client / servcmd.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  *  servercmd.c: Change the state depending on the command from the server.
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1989
32  *     Steve McClure, 1998
33  */
34
35 #include "misc.h"
36 #include "proto.h"
37 #include "queue.h"
38 #include "ioqueue.h"
39 #include "tags.h"
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <fcntl.h>
46 #if !defined(_WIN32)
47 #include <unistd.h>
48 #else
49 #include <io.h>
50 #endif
51
52 char num_teles[64];
53 static char the_prompt[1024];
54 static int mode;
55 static int nbtu;
56 static int nmin;
57 FILE *redir_fp;
58 FILE *pipe_fp;
59 int exec_fd;
60
61 static void prompt(FILE *auxfi);
62 static void doredir(char *p);
63 static void dopipe(char *p);
64 static void doexecute(char *p, FILE *auxfi);
65 static void output(int code, char *buf, FILE *auxfi);
66 static void screen(char *buf);
67
68 void
69 servercmd(struct ioqueue *ioq, FILE *auxfi)
70 {
71     char buf[1024];
72     char *p;
73     int code;
74
75     while (ioq_gets(ioq, buf, sizeof(buf))) {
76         p = buf;
77         while (*p && !isspace(*p))
78             p++;
79         *p++ = 0;
80         if (isalpha(*buf))
81             code = 10 + (*buf - 'a');
82         else
83             code = *buf - '0';
84         switch (code) {
85         case C_PROMPT:
86             if (sscanf(p, "%d %d", &nmin, &nbtu) != 2) {
87                 fprintf(stderr, "prompt: bad server prompt %s\n", p);
88             }
89             mode = code;
90             sprintf(the_prompt, "[%d:%d] Command : ", nmin, nbtu);
91             prompt(auxfi);
92             break;
93         case C_REDIR:
94             doredir(p);
95             break;
96         case C_PIPE:
97             dopipe(p);
98             break;
99         case C_FLUSH:
100             mode = code;
101             sprintf(the_prompt, "%s", p);
102             prompt(auxfi);
103             break;
104         case C_EXECUTE:
105             doexecute(p, auxfi);
106             break;
107         case C_INFORM:
108             if (*p) {
109                 p[strlen(p) - 1] = '\0';
110                 sprintf(num_teles, "(%s) ", p + 1);
111                 if (!redir_fp && !pipe_fp && !exec_fd) {
112                     putchar('\07');
113                     prompt(NULL);
114                 }
115             } else
116                 *num_teles = '\0';
117             break;
118         default:
119             output(code, p, auxfi);
120             break;
121         }
122     }
123 }
124
125 static void
126 prompt(FILE *auxfi)
127 {
128     if (mode == C_PROMPT) {
129         if (redir_fp) {
130             (void)fclose(redir_fp);
131             redir_fp = NULL;
132         } else if (pipe_fp) {
133             (void)pclose(pipe_fp);
134             pipe_fp = NULL;
135         } else if (exec_fd > 0) {
136             close(exec_fd);
137             close(0);
138             exec_fd = -1;
139             open("/dev/tty", O_RDONLY, 0);
140         }
141     }
142     if (mode == C_PROMPT)
143         printf("\n");
144     printf("%s%s", num_teles, the_prompt);
145     (void)fflush(stdout);
146     if (auxfi) {
147         fprintf(auxfi, "\n%s%s", num_teles, the_prompt);
148         (void)fflush(auxfi);
149     }
150 }
151
152 /*
153  * opens redir_fp if successful
154  */
155 static void
156 doredir(char *p)
157 {
158     char *how;
159     char *name;
160     char *tag;
161     int mode;
162     int fd;
163
164     if (redir_fp) {
165         (void)fclose(redir_fp);
166         redir_fp = NULL;
167     }
168     how = p++;
169     if (*p && ((*p == '>') || (*p == '!')))
170         p++;
171     tag = gettag(p);
172     while (*p && isspace(*p))
173         p++;
174     name = p;
175     while (*p && !isspace(*p))
176         p++;
177     *p = 0;
178     if (tag == NULL) {
179         fprintf(stderr, "WARNING!  Server redirected output to file %s\n",
180                 name);
181         return;
182     }
183     mode = O_WRONLY | O_CREAT;
184     if (how[1] == '>')
185         mode |= O_APPEND;
186     else if (how[1] == '!')
187         mode |= O_TRUNC;
188     else
189         mode |= O_EXCL;
190     if (*name == 0) {
191         fprintf(stderr, "Null file name after redirect\n");
192         free(tag);
193         return;
194     }
195     if ((fd = open(name, mode, 0600)) < 0) {
196         fprintf(stderr, "Redirect open failed\n");
197         perror(name);
198     } else {
199         redir_fp = fdopen(fd, "w");
200     }
201     free(tag);
202 }
203
204 /*
205  * opens "pipe_fp" if successful
206  */
207 static void
208 dopipe(char *p)
209 {
210     char *tag;
211
212     if (*p == '|')
213         p++;
214     tag = gettag(p);
215     while (*p && isspace(*p))
216         p++;
217     if (tag == NULL) {
218         fprintf(stderr, "WARNING!  Server attempted to run: %s\n", p);
219         return;
220     }
221     if (*p == 0) {
222         fprintf(stderr, "Null program name after redirect\n");
223         free(tag);
224         return;
225     }
226     if ((pipe_fp = popen(p, "w")) == NULL) {
227         fprintf(stderr, "Pipe open failed\n");
228         perror(p);
229     }
230     free(tag);
231 }
232
233 static void
234 doexecute(char *p, FILE *auxfi)
235 {
236     int fd;
237     char *tag;
238
239     tag = gettag(p);
240     while (*p && isspace(*p))
241         p++;
242     if (tag == NULL) {
243         fprintf(stderr,
244                 "WARNING!  Server attempted unauthorized read of file %s\n",
245                 p);
246         return;
247     }
248     if (p == NULL) {
249         fprintf(stderr, "Null file to execute\n");
250         free(tag);
251         return;
252     }
253     if ((fd = open(p, O_RDONLY, 0)) < 0) {
254         fprintf(stderr, "Can't open execute file\n");
255         perror(p);
256         free(tag);
257         return;
258     }
259     /* copies 4k at a time to the socket */
260     while (termio(fd, sock, auxfi))     /*do copy */
261         ;
262     /* Some platforms don't send the eof (cntl-D) at the end of
263        copying a file.  If emp_client hangs at the end of an
264        execute, include the following line and notify wolfpack
265        of the platform you are using.
266        sendeof(sock);
267      */
268     close(fd);
269     free(tag);
270 }
271
272 static void
273 output(int code, char *buf, FILE *auxfi)
274 {
275     switch (code) {
276     case C_NOECHO:
277         /* not implemented; serve doesn't send it */
278         break;
279     case C_FLUSH:
280         (void)fflush(stdout);
281         if (auxfi)
282             (void)fflush(auxfi);
283         break;
284     case C_ABORT:
285         printf("Aborted\n");
286         if (auxfi)
287             fprintf(auxfi, "Aborted\n");
288         break;
289     case C_CMDERR:
290     case C_BADCMD:
291         printf("Error; ");
292         if (auxfi)
293             fprintf(auxfi, "Error; ");
294         break;
295     case C_EXIT:
296         printf("Exit: ");
297         if (auxfi)
298             fprintf(auxfi, "Exit: ");
299         break;
300     case C_FLASH:
301         printf("\n");
302         break;
303     default:
304         break;
305     }
306     if (auxfi) {
307         fprintf(auxfi, "%s", buf);
308         if (code == C_FLUSH)
309             (void)fflush(auxfi);
310         else
311             (void)putc('\n', auxfi);
312     }
313
314     if (redir_fp)
315         fprintf(redir_fp, "%s\n", buf);
316     else if (pipe_fp)
317         fprintf(pipe_fp, "%s\n", buf);
318     else {
319         screen(buf);
320         if (code == C_FLUSH)
321             (void)fflush(stdout);
322         else
323             (void)putc('\n', stdout);
324     }
325 }
326
327 static void
328 screen(char *buf)
329 {
330     char c;
331
332     while ((c = *buf++)) {
333         if (eight_bit_clean) {
334             if (c == 14)
335                 putso();
336             else if (c == 15)
337                 putse();
338             else
339                 putchar(c);
340         } else if (c & 0x80) {
341             putso();
342             putchar(c & 0x7f);
343             putse();
344         } else
345             putchar(c);
346     }
347 }