/*
* Empire - A multi-player, client/server Internet based war game.
- * Copyright (C) 1986-2015, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ * Copyright (C) 1986-2018, Dave Pare, Jeff Bailey, Thomas Ruschak,
* Ken Stevens, Steve McClure, Markus Armbruster
*
* Empire is free software: you can redistribute it and/or modify
* Dave Pare, 1989
* Steve McClure, 1998
* Ron Koenderink, 2005
- * Markus Armbruster, 2005-2015
+ * 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"
static int redir_is_pipe;
static int executing;
-static void prompt(int, char *, char *);
static void doredir(char *p);
static void dopipe(char *p);
static int doexecute(char *p);
-void
+int
servercmd(int code, char *arg, int len)
{
static int nmin, nbtu, fd;
(void)fclose(redir_fp);
redir_fp = NULL;
}
+ outch('\n');
prompt(code, the_prompt, teles);
executing = 0;
break;
break;
case C_EXECUTE:
fd = doexecute(arg);
- if (fd < 0)
- send_eof++;
- else {
- input_fd = fd;
+ if (fd >= 0)
executing = 1;
- }
- break;
+ return fd;
case C_EXIT:
printf("Exit: %s", arg);
if (auxfp)
if (arg[0] != '\n') {
snprintf(teles, sizeof(teles), "(%.*s) ", len - 1, arg);
if (!redir_fp) {
+ outch('\n');
putchar('\07');
prompt(code, the_prompt, teles);
}
assert(0);
break;
}
-}
-static void
-prompt(int code, char *prompt, char *teles)
-{
- char *nl;
-
- 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 *
}
static int
-redir_authorized(char *arg, char *attempt, int expected)
+common_authorized(char *arg, char *attempt)
+{
+ if (restricted) {
+ fprintf(stderr, "Can't %s in restricted mode\n", attempt);
+ return 0;
+ }
+
+ if (executing) {
+ fprintf(stderr, "Can't %s in a batch file\n", attempt);
+ return 0;
+ }
+ return 1;
+}
+
+static int
+redir_authorized(char *arg, char *attempt)
{
- if (!expected) {
+ if (redir_fp) {
fprintf(stderr, "Warning: dropped conflicting %s %s",
attempt, arg);
return 0;
return 0;
}
- if (restricted) {
- fprintf(stderr, "Can't %s in restricted mode\n", attempt);
- return 0;
- }
+ return common_authorized(arg, attempt);
+}
- if (executing) {
- fprintf(stderr, "Can't %s in a batch file\n", 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 1;
+
+ return common_authorized(arg, "execute batch file");
}
static void
int mode;
int fd;
- if (!redir_authorized(p, "redirect to file", !redir_fp))
+ if (!redir_authorized(p, "redirect to file"))
return;
if (*p++ != '>') {
fprintf(stderr, "Warning: dropped weird redirection %s", p);
static void
dopipe(char *p)
{
- if (!redir_authorized(p, "pipe to shell command", !redir_fp))
+ if (!redir_authorized(p, "pipe to shell command"))
return;
if (*p++ != '|') {
fprintf(stderr, "Warning: dropped weird pipe %s", p);
{
int fd;
- if (!redir_authorized(p, "execute batch file", 1))
+ if (!exec_authorized(p))
return -1;
p = fname(p);