This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
magicka/doors.c

436 lines
9.1 KiB
C
Raw Normal View History

#include <stdio.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
2016-12-11 12:14:36 +00:00
#include <fcntl.h>
2017-04-23 02:14:14 +00:00
#include <iconv.h>
#if defined(linux)
# include <pty.h>
2016-08-11 22:43:44 +00:00
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
# include <util.h>
#else
# include <libutil.h>
#endif
#include "bbs.h"
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
extern struct bbs_config conf;
extern int mynode;
2016-08-06 04:36:36 +00:00
extern int gSocket;
2016-08-07 03:47:21 +00:00
extern int sshBBS;
2016-12-11 12:14:36 +00:00
extern int bbs_stderr;
int running_door_pid = 0;
int running_door = 0;
extern int timeoutpaused;
void doorchld_handler(int s)
{
// waitpid() might overwrite errno, so we save and restore it:
while(waitpid(-1, NULL, WNOHANG) > 0);
running_door = 0;
}
2016-08-06 04:36:36 +00:00
int write_door32sys(struct user_record *user) {
struct stat s;
char buffer[256];
FILE *fptr;
char *ptr;
int i;
sprintf(buffer, "%s/node%d", conf.bbs_path, mynode);
if (stat(buffer, &s) != 0) {
mkdir(buffer, 0755);
}
sprintf(buffer, "%s/node%d/door32.sys", conf.bbs_path, mynode);
fptr = fopen(buffer, "w");
if (!fptr) {
dolog("Unable to open %s for writing!", buffer);
return 1;
}
2016-08-08 09:47:02 +00:00
fprintf(fptr, "2\r\n"); // telnet type
fprintf(fptr, "%d\r\n", gSocket); // socket
fprintf(fptr, "38400\r\n"); // baudrate
fprintf(fptr, "Magicka %d.%d\r\n", VERSION_MAJOR, VERSION_MINOR);
fprintf(fptr, "%d\r\n", user->id);
fprintf(fptr, "%s %s\r\n", user->firstname, user->lastname);
fprintf(fptr, "%s\r\n", user->loginname);
fprintf(fptr, "%d\r\n", user->sec_level);
fprintf(fptr, "%d\r\n", user->timeleft);
fprintf(fptr, "1\r\n"); // ansi emulation = 1
fprintf(fptr, "%d\r\n", mynode);
fclose(fptr);
// create dorinfo1.def
sprintf(buffer, "%s/node%d", conf.bbs_path, mynode);
if (stat(buffer, &s) != 0) {
mkdir(buffer, 0755);
}
sprintf(buffer, "%s/node%d/dorinfo1.def", conf.bbs_path, mynode);
fptr = fopen(buffer, "w");
if (!fptr) {
dolog("Unable to open %s for writing!", buffer);
return 1;
}
strcpy(buffer, conf.sysop_name);
ptr = NULL;
for (i=0;i<strlen(buffer);i++) {
if (buffer[i] == ' ') {
ptr = &buffer[i+1];
buffer[i] = '\0';
break;
}
}
2016-08-08 09:47:02 +00:00
fprintf(fptr, "%s\r\n", conf.bbs_name); // telnet type
fprintf(fptr, "%s\r\n", buffer);
if (ptr != NULL) {
2016-08-08 09:47:02 +00:00
fprintf(fptr, "%s\r\n", ptr);
} else {
2016-08-08 09:47:02 +00:00
fprintf(fptr, "\r\n");
}
2016-08-08 09:47:02 +00:00
fprintf(fptr, "COM1\r\n"); // com port
fprintf(fptr, "38400 BAUD,N,8,1\r\n");
fprintf(fptr, "0\r\n");
fprintf(fptr, "%s\r\n", user->firstname);
fprintf(fptr, "%s\r\n", user->lastname);
fprintf(fptr, "%s\r\n", user->location);
fprintf(fptr, "1\r\n");
2017-04-10 06:21:14 +00:00
fprintf(fptr, "%d\r\n", user->sec_level);
2016-08-08 09:47:02 +00:00
fprintf(fptr, "%d\r\n", user->timeleft);
fprintf(fptr, "-1\r\n");
2017-04-10 06:21:14 +00:00
fclose(fptr);
return 0;
}
2017-04-23 02:14:14 +00:00
void rundoor(struct user_record *user, char *cmd, int stdio, char *codepage) {
char *arguments[4];
int door_out;
char buffer[10];
if (sshBBS) {
door_out = STDOUT_FILENO;
} else {
door_out = gSocket;
}
arguments[0] = strdup(cmd);
sprintf(buffer, "%d", mynode);
arguments[1] = strdup(buffer);
sprintf(buffer, "%d", door_out);
arguments[2] = strdup(buffer);
arguments[3] = NULL;
2017-04-23 02:14:14 +00:00
runexternal(user, cmd, stdio, arguments, NULL, 0, codepage);
free(arguments[0]);
free(arguments[1]);
free(arguments[2]);
}
2017-04-23 02:14:14 +00:00
void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], char *cwd, int raw, char *codepage) {
char buffer[1024];
int pid;
int ret;
unsigned char c;
int len;
int master;
int slave;
fd_set fdset;
int t;
struct winsize ws;
struct sigaction sa;
int door_in;
int door_out;
int i;
int gotiac;
int flush;
2017-04-23 02:14:14 +00:00
iconv_t ic;
struct timeval thetimeout;
struct termios oldit;
2016-12-11 12:14:36 +00:00
struct termios oldot;
struct termios oldit2;
2017-03-26 10:25:07 +00:00
char inbuf[256];
2017-03-26 10:33:55 +00:00
char outbuf[512];
2017-03-26 10:25:07 +00:00
int h;
int g;
2017-04-23 02:14:14 +00:00
char *ptr1;
char *ptr2;
2017-04-23 03:07:30 +00:00
char *ptr2p;
2017-04-23 02:14:14 +00:00
size_t ouc;
size_t inc;
2017-04-23 03:25:49 +00:00
size_t sz;
2017-04-23 02:14:14 +00:00
timeoutpaused = 1;
2016-08-06 04:36:36 +00:00
if (write_door32sys(user) != 0) {
return;
}
if (stdio) {
if (sshBBS) {
door_in = STDIN_FILENO;
door_out = STDOUT_FILENO;
} else {
door_in = gSocket;
door_out = gSocket;
}
2016-12-11 13:17:43 +00:00
2016-12-11 12:14:36 +00:00
ws.ws_row = 24;
ws.ws_col = 80;
running_door = 1;
2016-12-11 13:17:43 +00:00
if (!sshBBS) {
if (openpty(&master, &slave, NULL, NULL, &ws) == 0) {
sa.sa_handler = doorchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_SIGINFO;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
2016-12-11 13:17:43 +00:00
if (raw) {
ttySetRaw(master, &oldit2);
ttySetRaw(slave, &oldit2);
2016-12-11 12:14:36 +00:00
}
2016-12-11 13:17:43 +00:00
pid = fork();
if (pid < 0) {
return;
} else if (pid == 0) {
if (cwd != NULL) {
chdir(cwd);
}
close(master);
2016-12-11 13:17:43 +00:00
dup2(slave, 0);
dup2(slave, 1);
close(slave);
2016-12-11 13:17:43 +00:00
setsid();
2016-12-11 13:17:43 +00:00
ioctl(0, TIOCSCTTY, 1);
execvp(cmd, argv);
} else {
running_door_pid = pid;
gotiac = 0;
flush = 0;
2016-12-11 13:17:43 +00:00
while(running_door || !flush) {
FD_ZERO(&fdset);
FD_SET(master, &fdset);
FD_SET(door_in, &fdset);
if (master > door_in) {
t = master + 1;
} else {
t = door_in + 1;
}
thetimeout.tv_sec = 5;
thetimeout.tv_usec = 0;
ret = select(t, &fdset, NULL, NULL, &thetimeout);
if (ret > 0) {
if (FD_ISSET(door_in, &fdset)) {
2017-03-26 10:25:07 +00:00
len = read(door_in, inbuf, 256);
2016-12-11 13:17:43 +00:00
if (len == 0) {
close(master);
disconnect("Socket Closed");
return;
}
2017-03-26 10:25:07 +00:00
g = 0;
for (h=0;h<len;h++) {
c = inbuf[h];
if (!raw) {
if (c == '\n' || c == '\0') {
continue;
}
}
if (!running_door) {
2016-12-11 13:17:43 +00:00
continue;
}
2017-03-26 10:25:07 +00:00
if (c == 255) {
if (gotiac == 1) {
outbuf[g++] = c;
2016-12-12 01:32:57 +00:00
gotiac = 0;
2017-03-26 10:25:07 +00:00
} else {
gotiac = 1;
2016-12-12 01:32:57 +00:00
}
2017-03-26 10:25:07 +00:00
} else {
if (gotiac == 1) {
if (c == 254 || c == 253 || c == 252 || c == 251) {
gotiac = 2;
} else if (c == 250) {
gotiac = 3;
} else {
gotiac = 0;
}
} else if (gotiac == 2) {
2016-12-12 01:32:57 +00:00
gotiac = 0;
2017-03-26 10:25:07 +00:00
} else if (gotiac == 3) {
if (c == 240) {
gotiac = 0;
}
} else {
outbuf[g++] = c;
2016-12-12 01:32:57 +00:00
}
2016-12-11 12:14:36 +00:00
}
}
2017-04-23 02:31:51 +00:00
if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && user->codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && user->codepage == 1)) {
2017-04-23 02:14:14 +00:00
write(master, outbuf, g);
} else {
2017-04-23 02:31:51 +00:00
if (user->codepage == 0) {
2017-04-23 03:16:34 +00:00
ic = iconv_open(codepage, "CP437");
2017-04-23 02:14:14 +00:00
} else {
2017-04-23 03:16:34 +00:00
ic = iconv_open(codepage, "UTF-8");
2017-04-23 02:14:14 +00:00
}
ptr1 = outbuf;
2017-04-23 03:33:09 +00:00
ptr2 = (char *)malloc((g + 1) * 4);
2017-04-23 03:07:30 +00:00
ptr2p = ptr2;
2017-04-23 03:33:09 +00:00
memset(ptr2, 0, (g + 1) * 4);
2017-04-23 02:14:14 +00:00
inc = g;
2017-04-23 03:33:09 +00:00
ouc = (g + 1) * 4;
2017-04-23 02:14:14 +00:00
iconv(ic, &ptr1, &inc, &ptr2, &ouc);
2017-04-23 03:09:24 +00:00
write(master, ptr2p, ptr2 - ptr2p);
2017-04-23 02:14:14 +00:00
2017-04-23 03:07:30 +00:00
free(ptr2p);
2017-04-23 02:14:14 +00:00
iconv_close(ic);
}
2016-12-11 13:17:43 +00:00
} else if (FD_ISSET(master, &fdset)) {
2017-03-26 10:25:07 +00:00
len = read(master, inbuf, 256);
2016-12-11 13:17:43 +00:00
if (len == 0) {
close(master);
break;
}
2017-03-26 10:33:55 +00:00
g = 0;
for (h=0;h<len;h++) {
2017-03-26 10:40:47 +00:00
c = inbuf[h];
2017-03-26 10:33:55 +00:00
if (c == 255) {
outbuf[g++] = c;
}
outbuf[g++] = c;
}
2017-04-23 02:31:51 +00:00
if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && user->codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && user->codepage == 1)) {
2017-04-23 02:14:14 +00:00
write(door_out, outbuf, g);
} else {
2017-04-23 02:31:51 +00:00
if (user->codepage == 0) {
2017-04-23 03:16:34 +00:00
ic = iconv_open("CP437", codepage);
2017-04-23 02:14:14 +00:00
} else {
2017-04-23 03:16:34 +00:00
ic = iconv_open("UTF-8", codepage);
2017-04-23 02:14:14 +00:00
}
ptr1 = outbuf;
2017-04-23 03:33:09 +00:00
ptr2 = (char *)malloc((g + 1) * 4);
2017-04-23 03:07:30 +00:00
ptr2p = ptr2;
2017-04-23 03:33:09 +00:00
memset(ptr2, 0, (g + 1) * 4);
2017-04-23 02:14:14 +00:00
inc = g;
2017-04-23 03:33:09 +00:00
ouc = (g + 1) * 4;
2017-04-23 02:14:14 +00:00
2017-04-23 03:25:49 +00:00
sz = iconv(ic, &ptr1, &inc, &ptr2, &ouc);
if (sz == -1) {
perror("Iconv");
}
2017-04-23 03:09:24 +00:00
write(door_out, ptr2p, ptr2 - ptr2p);
2017-04-23 02:14:14 +00:00
2017-04-23 03:07:30 +00:00
free(ptr2p);
2017-04-23 02:14:14 +00:00
iconv_close(ic);
}
}
2016-12-11 13:17:43 +00:00
} else {
if (!running_door) {
flush = 1;
}
}
}
2016-12-11 13:17:43 +00:00
}
}
} else {
if (raw) {
ttySetRaw(STDIN_FILENO, &oldit);
ttySetRaw(STDOUT_FILENO, &oldot);
}
sa.sa_handler = doorchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART | SA_SIGINFO;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
pid = fork();
if (pid < 0) {
return;
} else if (pid == 0) {
if (cwd != NULL) {
chdir(cwd);
}
dup2(bbs_stderr, 2);
execvp(cmd, argv);
} else {
while(running_door) {
sleep(1);
}
}
2016-12-11 13:17:43 +00:00
if (raw) {
tcsetattr(STDIN_FILENO, TCSANOW, &oldit);
tcsetattr(STDOUT_FILENO, TCSANOW, &oldot);
}
}
} else {
if (!sshBBS) {
snprintf(buffer, 1024, "%s", cmd);
2017-03-26 10:25:07 +00:00
for (i=1;argv[i] != NULL; i++) {
2017-03-26 02:40:42 +00:00
snprintf(&buffer[strlen(buffer)], 1024 - strlen(buffer), " %s", argv[i]);
}
if (cwd != NULL) {
chdir(cwd);
}
system(buffer);
if (cwd != NULL) {
chdir(conf.bbs_path);
}
} else {
s_printf(get_string(51));
}
}
timeoutpaused = 0;
2017-04-12 12:34:08 +00:00
}