2016-04-07 04:45:05 +00:00
|
|
|
#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>
|
|
|
|
#if defined(linux)
|
|
|
|
# include <pty.h>
|
2016-08-11 22:43:44 +00:00
|
|
|
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
|
2016-04-07 04:45:05 +00:00
|
|
|
# include <util.h>
|
|
|
|
#else
|
|
|
|
# include <libutil.h>
|
|
|
|
#endif
|
|
|
|
#include "bbs.h"
|
2016-04-12 05:19:56 +00:00
|
|
|
#include "lua/lua.h"
|
|
|
|
#include "lua/lualib.h"
|
|
|
|
#include "lua/lauxlib.h"
|
2016-04-07 04:45:05 +00:00
|
|
|
|
|
|
|
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-04-07 04:45:05 +00:00
|
|
|
|
|
|
|
int running_door_pid = 0;
|
|
|
|
int running_door = 0;
|
|
|
|
|
2016-04-09 09:37:34 +00:00
|
|
|
extern int timeoutpaused;
|
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
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) {
|
2016-04-07 04:45:05 +00:00
|
|
|
struct stat s;
|
|
|
|
char buffer[256];
|
|
|
|
FILE *fptr;
|
|
|
|
char *ptr;
|
|
|
|
int i;
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
sprintf(buffer, "%s/node%d", conf.bbs_path, mynode);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
if (stat(buffer, &s) != 0) {
|
|
|
|
mkdir(buffer, 0755);
|
|
|
|
}
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
sprintf(buffer, "%s/node%d/door32.sys", conf.bbs_path, mynode);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
fptr = fopen(buffer, "w");
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
if (!fptr) {
|
2016-08-04 09:25:49 +00:00
|
|
|
dolog("Unable to open %s for writing!", buffer);
|
2016-04-07 04:45:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-08-04 09:25:49 +00:00
|
|
|
|
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);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
fclose(fptr);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
// create dorinfo1.def
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
sprintf(buffer, "%s/node%d", conf.bbs_path, mynode);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
if (stat(buffer, &s) != 0) {
|
|
|
|
mkdir(buffer, 0755);
|
|
|
|
}
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
sprintf(buffer, "%s/node%d/dorinfo1.def", conf.bbs_path, mynode);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
fptr = fopen(buffer, "w");
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
if (!fptr) {
|
2016-08-04 09:25:49 +00:00
|
|
|
dolog("Unable to open %s for writing!", buffer);
|
2016-04-07 04:45:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
strcpy(buffer, conf.sysop_name);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
ptr = NULL;
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
for (i=0;i<strlen(buffer);i++) {
|
|
|
|
if (buffer[i] == ' ') {
|
|
|
|
ptr = &buffer[i+1];
|
|
|
|
buffer[i] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-08-08 09:47:02 +00:00
|
|
|
fprintf(fptr, "%s\r\n", conf.bbs_name); // telnet type
|
|
|
|
fprintf(fptr, "%s\r\n", buffer);
|
2016-04-07 04:45:05 +00:00
|
|
|
if (ptr != NULL) {
|
2016-08-08 09:47:02 +00:00
|
|
|
fprintf(fptr, "%s\r\n", ptr);
|
2016-04-07 04:45:05 +00:00
|
|
|
} else {
|
2016-08-08 09:47:02 +00:00
|
|
|
fprintf(fptr, "\r\n");
|
2016-04-07 04:45:05 +00:00
|
|
|
}
|
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");
|
|
|
|
fprintf(fptr, "30\r\n");
|
|
|
|
fprintf(fptr, "%d\r\n", user->timeleft);
|
|
|
|
fprintf(fptr, "-1\r\n");
|
2016-04-07 04:45:05 +00:00
|
|
|
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
fclose(fptr);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-10 07:55:47 +00:00
|
|
|
|
|
|
|
|
2016-08-06 04:36:36 +00:00
|
|
|
void rundoor(struct user_record *user, char *cmd, int stdio) {
|
2016-04-07 04:45:05 +00:00
|
|
|
char *arguments[4];
|
2016-12-10 07:55:47 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
runexternal(user, cmd, stdio, arguments, NULL, 0);
|
|
|
|
|
|
|
|
free(arguments[0]);
|
|
|
|
free(arguments[1]);
|
|
|
|
free(arguments[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], char *cwd, int raw) {
|
|
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
int pid;
|
2016-04-07 04:45:05 +00:00
|
|
|
int ret;
|
2016-12-10 07:55:47 +00:00
|
|
|
unsigned char c;
|
2016-04-07 04:45:05 +00:00
|
|
|
int len;
|
|
|
|
int master;
|
|
|
|
int slave;
|
|
|
|
fd_set fdset;
|
|
|
|
int t;
|
|
|
|
struct winsize ws;
|
|
|
|
struct sigaction sa;
|
2016-08-07 03:45:52 +00:00
|
|
|
int door_in;
|
|
|
|
int door_out;
|
2016-12-10 07:55:47 +00:00
|
|
|
int i;
|
|
|
|
int gotiac;
|
|
|
|
int flush;
|
|
|
|
struct timeval thetimeout;
|
|
|
|
struct termios oldit;
|
2016-04-09 09:37:34 +00:00
|
|
|
timeoutpaused = 1;
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-08-06 04:36:36 +00:00
|
|
|
if (write_door32sys(user) != 0) {
|
2016-04-07 04:45:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
if (stdio) {
|
2016-08-07 03:45:52 +00:00
|
|
|
if (sshBBS) {
|
|
|
|
door_in = STDIN_FILENO;
|
|
|
|
door_out = STDOUT_FILENO;
|
|
|
|
} else {
|
|
|
|
door_in = gSocket;
|
|
|
|
door_out = gSocket;
|
|
|
|
}
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
ws.ws_row = 24;
|
|
|
|
ws.ws_col = 80;
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
running_door = 1;
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
if (openpty(&master, &slave, NULL, NULL, &ws) == 0) {
|
|
|
|
sa.sa_handler = doorchld_handler;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
2016-08-16 10:28:48 +00:00
|
|
|
sa.sa_flags = SA_RESTART | SA_SIGINFO;
|
2016-04-07 04:45:05 +00:00
|
|
|
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
|
|
|
|
perror("sigaction");
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-12-10 07:55:47 +00:00
|
|
|
if (raw) {
|
|
|
|
ttySetRaw(master, &oldit);
|
|
|
|
ttySetRaw(slave, &oldit);
|
|
|
|
}
|
2016-04-07 04:45:05 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
return;
|
|
|
|
} else if (pid == 0) {
|
2016-12-10 07:55:47 +00:00
|
|
|
if (cwd != NULL) {
|
|
|
|
chdir(cwd);
|
|
|
|
}
|
2016-04-07 04:45:05 +00:00
|
|
|
close(master);
|
|
|
|
dup2(slave, 0);
|
|
|
|
dup2(slave, 1);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
close(slave);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
setsid();
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
ioctl(0, TIOCSCTTY, 1);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-12-10 07:55:47 +00:00
|
|
|
execvp(cmd, argv);
|
2016-04-07 04:45:05 +00:00
|
|
|
} else {
|
|
|
|
running_door_pid = pid;
|
2016-12-10 07:55:47 +00:00
|
|
|
gotiac = 0;
|
|
|
|
flush = 0;
|
|
|
|
|
|
|
|
while(running_door || !flush) {
|
2016-04-07 04:45:05 +00:00
|
|
|
FD_ZERO(&fdset);
|
|
|
|
FD_SET(master, &fdset);
|
2016-08-07 03:45:52 +00:00
|
|
|
FD_SET(door_in, &fdset);
|
|
|
|
if (master > door_in) {
|
2016-04-07 04:45:05 +00:00
|
|
|
t = master + 1;
|
|
|
|
} else {
|
2016-08-07 03:45:52 +00:00
|
|
|
t = door_in + 1;
|
2016-04-07 04:45:05 +00:00
|
|
|
}
|
2016-12-10 07:55:47 +00:00
|
|
|
|
|
|
|
thetimeout.tv_sec = 5;
|
|
|
|
thetimeout.tv_usec = 0;
|
|
|
|
ret = select(t, &fdset, NULL, NULL, &thetimeout);
|
2016-04-07 04:45:05 +00:00
|
|
|
if (ret > 0) {
|
2016-08-07 03:45:52 +00:00
|
|
|
if (FD_ISSET(door_in, &fdset)) {
|
|
|
|
len = read(door_in, &c, 1);
|
2016-04-07 04:45:05 +00:00
|
|
|
if (len == 0) {
|
|
|
|
close(master);
|
2016-08-06 04:36:36 +00:00
|
|
|
disconnect("Socket Closed");
|
2016-04-07 04:45:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-12-10 07:55:47 +00:00
|
|
|
if (!raw) {
|
|
|
|
if (c == '\n' || c == '\0') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!running_door) {
|
2016-04-09 00:33:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-12-10 07:55:47 +00:00
|
|
|
if (c == 255) {
|
|
|
|
if (gotiac == 1) {
|
|
|
|
write(master, &c, 1);
|
|
|
|
gotiac = 0;
|
|
|
|
} else {
|
|
|
|
gotiac = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (gotiac < 2 && gotiac != 0) {
|
|
|
|
gotiac++;
|
|
|
|
} else {
|
|
|
|
write(master, &c, 1);
|
|
|
|
gotiac = 0;
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 04:45:05 +00:00
|
|
|
} else if (FD_ISSET(master, &fdset)) {
|
|
|
|
len = read(master, &c, 1);
|
|
|
|
if (len == 0) {
|
|
|
|
close(master);
|
|
|
|
break;
|
|
|
|
}
|
2016-12-10 07:55:47 +00:00
|
|
|
if (c == 255) {
|
|
|
|
write(door_out, &c, 1);
|
|
|
|
}
|
2016-08-07 03:45:52 +00:00
|
|
|
write(door_out, &c, 1);
|
2016-04-07 04:45:05 +00:00
|
|
|
}
|
2016-12-10 07:55:47 +00:00
|
|
|
} else {
|
|
|
|
if (!running_door) {
|
|
|
|
flush = 1;
|
|
|
|
}
|
2016-04-07 04:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-10 07:55:47 +00:00
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
} else {
|
2016-08-07 03:45:52 +00:00
|
|
|
if (!sshBBS) {
|
2016-12-10 07:55:47 +00:00
|
|
|
snprintf(buffer, 1024, "%s", cmd);
|
|
|
|
for (i=0;argv[i] != NULL; i++) {
|
|
|
|
snprintf(&buffer[strlen(buffer) - 1], 1024 - strlen(buffer), " %s", argv[i]);
|
|
|
|
}
|
|
|
|
if (cwd != NULL) {
|
|
|
|
chdir(cwd);
|
|
|
|
}
|
2016-08-07 03:45:52 +00:00
|
|
|
system(buffer);
|
2016-12-10 07:55:47 +00:00
|
|
|
if (cwd != NULL) {
|
|
|
|
chdir(conf.bbs_path);
|
|
|
|
}
|
2016-08-07 03:45:52 +00:00
|
|
|
} else {
|
2016-08-14 09:56:15 +00:00
|
|
|
s_printf(get_string(51));
|
2016-08-07 03:45:52 +00:00
|
|
|
}
|
2016-04-07 04:45:05 +00:00
|
|
|
}
|
2016-04-09 09:37:34 +00:00
|
|
|
timeoutpaused = 0;
|
2016-04-07 04:45:05 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 04:36:36 +00:00
|
|
|
int door_menu(struct user_record *user) {
|
2016-04-07 04:45:05 +00:00
|
|
|
int doquit = 0;
|
|
|
|
int dodoors = 0;
|
2016-04-12 05:19:56 +00:00
|
|
|
char buffer[256];
|
2016-04-07 04:45:05 +00:00
|
|
|
int i;
|
|
|
|
char c;
|
2016-04-12 05:19:56 +00:00
|
|
|
struct stat s;
|
|
|
|
int do_internal_menu = 0;
|
|
|
|
char *lRet;
|
|
|
|
lua_State *L;
|
|
|
|
int result;
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-04-12 05:19:56 +00:00
|
|
|
if (conf.script_path != NULL) {
|
|
|
|
sprintf(buffer, "%s/doors.lua", conf.script_path);
|
|
|
|
if (stat(buffer, &s) == 0) {
|
|
|
|
L = luaL_newstate();
|
|
|
|
luaL_openlibs(L);
|
|
|
|
lua_push_cfunctions(L);
|
|
|
|
luaL_loadfile(L, buffer);
|
|
|
|
do_internal_menu = 0;
|
|
|
|
result = lua_pcall(L, 0, 1, 0);
|
|
|
|
if (result) {
|
2016-08-04 09:25:49 +00:00
|
|
|
dolog("Failed to run script: %s", lua_tostring(L, -1));
|
2016-04-12 05:19:56 +00:00
|
|
|
do_internal_menu = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_internal_menu = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_internal_menu = 1;
|
|
|
|
}
|
|
|
|
|
2016-04-07 04:45:05 +00:00
|
|
|
while (!dodoors) {
|
2016-04-12 05:19:56 +00:00
|
|
|
if (do_internal_menu == 1) {
|
2016-08-06 04:36:36 +00:00
|
|
|
s_displayansi("doors");
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-08-14 09:56:15 +00:00
|
|
|
s_printf(get_string(52), user->timeleft);
|
2016-08-04 09:25:49 +00:00
|
|
|
|
2016-08-06 04:36:36 +00:00
|
|
|
c = s_getc();
|
2016-04-12 05:19:56 +00:00
|
|
|
} else {
|
|
|
|
lua_getglobal(L, "menu");
|
|
|
|
result = lua_pcall(L, 0, 1, 0);
|
|
|
|
if (result) {
|
2016-08-04 09:25:49 +00:00
|
|
|
dolog("Failed to run script: %s", lua_tostring(L, -1));
|
2016-04-12 05:19:56 +00:00
|
|
|
do_internal_menu = 1;
|
|
|
|
lua_close(L);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lRet = (char *)lua_tostring(L, -1);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
c = lRet[0];
|
|
|
|
}
|
2016-04-07 04:45:05 +00:00
|
|
|
switch(tolower(c)) {
|
|
|
|
case 'q':
|
|
|
|
dodoors = 1;
|
|
|
|
break;
|
|
|
|
case 'g':
|
2016-08-04 09:25:49 +00:00
|
|
|
{
|
2016-09-05 04:05:17 +00:00
|
|
|
doquit = do_logout();
|
|
|
|
dodoors = doquit;
|
2016-04-07 04:45:05 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
for (i=0;i<conf.door_count;i++) {
|
|
|
|
if (tolower(c) == tolower(conf.doors[i]->key)) {
|
2016-04-10 04:10:18 +00:00
|
|
|
dolog("%s is launched door %s, on node %d", user->loginname, conf.doors[i]->name, mynode);
|
2016-08-06 04:36:36 +00:00
|
|
|
rundoor(user, conf.doors[i]->command, conf.doors[i]->stdio);
|
2016-04-10 04:10:18 +00:00
|
|
|
dolog("%s is returned from door %s, on node %d", user->loginname, conf.doors[i]->name, mynode);
|
2016-04-07 04:45:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-12 05:19:56 +00:00
|
|
|
if (do_internal_menu == 0) {
|
|
|
|
lua_close(L);
|
|
|
|
}
|
2016-04-07 04:45:05 +00:00
|
|
|
return doquit;
|
|
|
|
}
|