]> git.pond.sub.org Git - empserver/blobdiff - src/client/servcmd.c
Update copyright notice
[empserver] / src / client / servcmd.c
index aaab478a1f05177e9ca7ab1e511f9241c109f656..12017a19ee6e85fed8aa3a92e7e4b71c86af76e5 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  servercmd.c: Change the state depending on the command from the server.
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1989
  *     Steve McClure, 1998
  *     Ron Koenderink, 2005
- *     Markus Armbruster, 2005-2007
+ *     Markus Armbruster, 2005-2017
  */
 
 #include <config.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 #include "misc.h"
 #include "proto.h"
 #include "secure.h"
 
 int eight_bit_clean;
 FILE *auxfp;
+int restricted;
 
 static FILE *redir_fp;
-static FILE *pipe_fp;
+static int redir_is_pipe;
 static int executing;
-static size_t input_to_forget;
 
-static void prompt(int, char *, char *);
 static void doredir(char *p);
 static void dopipe(char *p);
-static void doexecute(char *p);
+static int doexecute(char *p);
 
-void
+int
 servercmd(int code, char *arg, int len)
 {
-    static int nmin, nbtu;
+    static int nmin, nbtu, fd;
     static char the_prompt[1024];
     static char teles[64];
 
     switch (code) {
     case C_PROMPT:
        if (sscanf(arg, "%d %d", &nmin, &nbtu) != 2) {
-           fprintf(stderr, "prompt: bad server prompt %s\n", arg);
+           fprintf(stderr, "Warning: server sent malformed prompt %s",
+                   arg);
        }
        snprintf(the_prompt, sizeof(the_prompt), "[%d:%d] Command : ",
                 nmin, nbtu);
+       if (redir_fp) {
+           if (redir_is_pipe)
+               (void)pclose(redir_fp);
+           else
+               (void)fclose(redir_fp);
+           redir_fp = NULL;
+       }
+       outch('\n');
        prompt(code, the_prompt, teles);
+       executing = 0;
        break;
     case C_FLUSH:
        snprintf(the_prompt, sizeof(the_prompt), "%.*s", len - 1, arg);
        prompt(code, the_prompt, teles);
        break;
     case C_EXECUTE:
-       doexecute(arg);
-       break;
+       fd = doexecute(arg);
+       if (fd >= 0)
+           executing = 1;
+       return fd;
     case C_EXIT:
        printf("Exit: %s", arg);
        if (auxfp)
@@ -93,9 +104,10 @@ servercmd(int code, char *arg, int len)
            fprintf(auxfp, "\n%s", arg);
        break;
     case C_INFORM:
-       if (*arg) {
-           snprintf(teles, sizeof(teles), "(%.*s )", len -1, arg);
-           if (!redir_fp && !pipe_fp) {
+       if (arg[0] != '\n') {
+           snprintf(teles, sizeof(teles), "(%.*s) ", len - 1, arg);
+           if (!redir_fp) {
+               outch('\n');
                putchar('\07');
                prompt(code, the_prompt, teles);
            }
@@ -112,35 +124,8 @@ servercmd(int code, char *arg, int len)
        assert(0);
        break;
     }
-}
-
-static void
-prompt(int code, char *prompt, char *teles)
-{
-    char *nl;
-
-    if (code == C_PROMPT) {
-       if (redir_fp) {
-           (void)fclose(redir_fp);
-           redir_fp = NULL;
-       } else if (pipe_fp) {
-           (void)pclose(pipe_fp);
-           pipe_fp = NULL;
-       }
-       executing = 0;
-       if (input_to_forget) {
-           forget_input(input_to_forget);
-           input_to_forget = 0;
-       }
-    }
 
-    nl = code == C_PROMPT || code == C_INFORM ? "\n" : "";
-    printf("%s%s%s", nl, teles, prompt);
-    fflush(stdout);
-    if (auxfp) {
-       fprintf(auxfp, "%s%s%s", nl, teles, prompt);
-       fflush(auxfp);
-    }
+    return 0;
 }
 
 static char *
@@ -155,42 +140,60 @@ fname(char *s)
 }
 
 static int
-redir_authorized(char *arg, char *attempt)
+common_authorized(char *arg, char *attempt)
 {
-    size_t seen = seen_input(arg);
+    if (restricted) {
+       fprintf(stderr, "Can't %s in restricted mode\n", attempt);
+       return 0;
+    }
 
     if (executing) {
-       fprintf(stderr, "Can't %s in a script\n", attempt);
+       fprintf(stderr, "Can't %s in a batch file\n", attempt);
+       return 0;
+    }
+    return 1;
+}
+
+static int
+redir_authorized(char *arg, char *attempt)
+{
+    if (redir_fp) {
+       fprintf(stderr, "Warning: dropped conflicting %s %s",
+               attempt, arg);
        return 0;
     }
 
-    if (!seen || (input_to_forget && input_to_forget != seen)) {
-       fprintf(stderr, "WARNING!  Server attempted to %s %s\n",
+    if (!seen_input(arg)) {
+       fprintf(stderr, "Warning: server attempted to %s %s",
                attempt, arg);
        return 0;
     }
-    input_to_forget = seen;
-    return 1;
+
+    return common_authorized(arg, attempt);
+}
+
+static int
+exec_authorized(char *arg)
+{
+    if (!seen_exec_input(arg)) {
+       fprintf(stderr,
+               "Warning: server attempted to execute batch file %s", arg);
+       return 0;
+    }
+
+    return common_authorized(arg, "execute batch file");
 }
 
-/*
- * opens redir_fp if successful
- */
 static void
 doredir(char *p)
 {
     int mode;
     int fd;
 
-    if (redir_fp) {
-       (void)fclose(redir_fp);
-       redir_fp = NULL;
-    }
-
     if (!redir_authorized(p, "redirect to file"))
        return;
     if (*p++ != '>') {
-       fprintf(stderr, "WARNING!  Weird redirection %s", p);
+       fprintf(stderr, "Warning: dropped weird redirection %s", p);
        return;
     }
 
@@ -210,6 +213,7 @@ doredir(char *p)
        return;
     }
 
+    redir_is_pipe = 0;
     fd = open(p, mode, 0666);
     redir_fp = fd < 0 ? NULL : fdopen(fd, "w");
     if (!redir_fp) {
@@ -218,16 +222,13 @@ doredir(char *p)
     }
 }
 
-/*
- * opens "pipe_fp" if successful
- */
 static void
 dopipe(char *p)
 {
     if (!redir_authorized(p, "pipe to shell command"))
        return;
     if (*p++ != '|') {
-       fprintf(stderr, "WARNING!  Weird pipe %s", p);
+       fprintf(stderr, "Warning: dropped weird pipe %s", p);
        return;
     }
 
@@ -237,38 +238,38 @@ dopipe(char *p)
        return;
     }
 
-    if ((pipe_fp = popen(p, "w")) == NULL) {
-       fprintf(stderr, "Can't redirect to pipe %s: %s\n",
-               p, strerror(errno));
+    /* strip newline */
+    p[strlen(p) - 1] = 0;
+
+    redir_is_pipe = 1;
+    errno = 0;
+    if ((redir_fp = popen(p, "w")) == NULL) {
+       fprintf(stderr, "Can't redirect to pipe %s%s%s\n",
+               p, errno ? ": " : "", errno ? strerror(errno) : "");
     }
 }
 
-static void
+static int
 doexecute(char *p)
 {
     int fd;
 
-    if (!redir_authorized(p, "execute script file")) {
-       send_eof++;
-       return;
-    }
+    if (!exec_authorized(p))
+       return -1;
 
     p = fname(p);
     if (*p == 0) {
        fprintf(stderr, "Need a file to execute\n");
-       send_eof++;
-       return;
+       return -1;
     }
 
     if ((fd = open(p, O_RDONLY)) < 0) {
-       fprintf(stderr, "Can't open execute file %s: %s\n",
+       fprintf(stderr, "Can't open batch file %s: %s\n",
                p, strerror(errno));
-       send_eof++;
-       return;
+       return -1;
     }
 
-    input_fd = fd;
-    executing = 1;
+    return fd;
 }
 
 void
@@ -278,8 +279,6 @@ outch(char c)
        putc(c, auxfp);
     if (redir_fp)
        putc(c, redir_fp);
-    else if (pipe_fp)
-       putc(c, pipe_fp);
     else if (eight_bit_clean) {
        if (c == 14)
            putso();