2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
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.
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.
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/>.
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.
27 * servercmd.c: Change the state depending on the command from the server.
29 * Known contributors to this file:
32 * Ron Koenderink, 2005
33 * Markus Armbruster, 2005-2017
49 #ifdef HAVE_LIBREADLINE
50 # if defined(HAVE_READLINE_READLINE_H)
51 # include <readline/readline.h>
52 # elif defined(HAVE_READLINE_H)
53 # include <readline.h>
54 # endif /* defined(HAVE_READLINE_H) */
55 #endif /* HAVE_LIBREADLINE */
61 static FILE *redir_fp;
62 static int redir_is_pipe;
65 static void prompt(int, char *, char *);
66 static void doredir(char *p);
67 static void dopipe(char *p);
68 static int doexecute(char *p);
71 servercmd(int code, char *arg, int len)
73 static int nmin, nbtu, fd;
74 static char the_prompt[1024];
75 static char teles[64];
79 if (sscanf(arg, "%d %d", &nmin, &nbtu) != 2) {
80 fprintf(stderr, "Warning: server sent malformed prompt %s",
83 snprintf(the_prompt, sizeof(the_prompt), "[%d:%d] Command : ",
87 (void)pclose(redir_fp);
89 (void)fclose(redir_fp);
93 prompt(code, the_prompt, teles);
97 snprintf(the_prompt, sizeof(the_prompt), "%.*s", len - 1, arg);
98 prompt(code, the_prompt, teles);
106 printf("Exit: %s", arg);
108 fprintf(auxfp, "Exit: %s", arg);
113 fprintf(auxfp, "\n%s", arg);
116 if (arg[0] != '\n') {
117 snprintf(teles, sizeof(teles), "(%.*s) ", len - 1, arg);
121 prompt(code, the_prompt, teles);
141 prompt(int code, char *prompt, char *teles)
145 snprintf(pr, sizeof(pr), "%s%s", teles, prompt);
146 #ifdef HAVE_LIBREADLINE
148 rl_forced_update_display();
149 #else /* !HAVE_LIBREADLINE */
152 #endif /* !HAVE_LIBREADLINE */
154 fprintf(auxfp, "%s%s", teles, prompt);
164 for (beg = s; isspace(*(unsigned char *)beg); beg++) ;
165 for (end = beg; !isspace(*(unsigned char *)end); end++) ;
171 common_authorized(char *arg, char *attempt)
174 fprintf(stderr, "Can't %s in restricted mode\n", attempt);
179 fprintf(stderr, "Can't %s in a batch file\n", attempt);
186 redir_authorized(char *arg, char *attempt)
189 fprintf(stderr, "Warning: dropped conflicting %s %s",
194 if (!seen_input(arg)) {
195 fprintf(stderr, "Warning: server attempted to %s %s",
200 return common_authorized(arg, attempt);
204 exec_authorized(char *arg)
206 if (!seen_exec_input(arg)) {
208 "Warning: server attempted to execute batch file %s", arg);
212 return common_authorized(arg, "execute batch file");
221 if (!redir_authorized(p, "redirect to file"))
224 fprintf(stderr, "Warning: dropped weird redirection %s", p);
228 mode = O_WRONLY | O_CREAT;
232 } else if (*p == '!') {
240 fprintf(stderr, "Redirection lacks a file name\n");
245 fd = open(p, mode, 0666);
246 redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
248 fprintf(stderr, "Can't redirect to %s: %s\n",
256 if (!redir_authorized(p, "pipe to shell command"))
259 fprintf(stderr, "Warning: dropped weird pipe %s", p);
263 for (; *p && isspace(*p); p++) ;
265 fprintf(stderr, "Redirection lacks a command\n");
270 p[strlen(p) - 1] = 0;
274 if ((redir_fp = popen(p, "w")) == NULL) {
275 fprintf(stderr, "Can't redirect to pipe %s%s%s\n",
276 p, errno ? ": " : "", errno ? strerror(errno) : "");
285 if (!exec_authorized(p))
290 fprintf(stderr, "Need a file to execute\n");
294 if ((fd = open(p, O_RDONLY)) < 0) {
295 fprintf(stderr, "Can't open batch file %s: %s\n",
310 else if (eight_bit_clean) {
317 } else if (c & 0x80) {