fixes for SSH

This commit is contained in:
Andrew Pamment 2016-12-11 22:14:36 +10:00
parent 24b1aa87cc
commit 8310fb4f93
4 changed files with 193 additions and 30 deletions

View File

@ -13,6 +13,9 @@ extern struct user_record *gUser;
extern int mynode;
extern char upload_filename[1024];
extern int sshBBS;
extern int bbs_stdin;
extern int bbs_stdout;
extern int bbs_stderr;
tLONG convertl(tLONG l) {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
@ -103,15 +106,30 @@ int bwave_scan_area(int confr, int area, int areano, int totmsgs, FILE *fti_file
break;
}
if (strcasecmp(msghs->msgs[k]->to, gUser->loginname) == 0) {
personal_msgs++;
if (msghs->msgs[k]->to != NULL) {
if (strcasecmp(msghs->msgs[k]->to, gUser->loginname) == 0) {
personal_msgs++;
}
}
memset(&fti, 0, sizeof(FTI_REC));
strncpy(fti.from, msghs->msgs[k]->from, 35);
strncpy(fti.to, msghs->msgs[k]->to, 35);
strncpy(fti.subject, msghs->msgs[k]->subject, 71);
if (msghs->msgs[k]->from != NULL) {
strncpy(fti.from, msghs->msgs[k]->from, 35);
} else {
sprintf(fti.from, "(Missing From)");
}
if (msghs->msgs[k]->to != NULL) {
strncpy(fti.to, msghs->msgs[k]->to, 35);
} else {
sprintf(fti.to, "(Missing To)");
}
if (msghs->msgs[k]->subject != NULL) {
strncpy(fti.subject, msghs->msgs[k]->subject, 71);
} else {
sprintf(fti.subject, "(Missing Subject)");
}
localtime_r((time_t *)&msghs->msgs[k]->msg_h->DateWritten, &timeStruct);
@ -187,6 +205,9 @@ void bwave_create_packet() {
int lasttot;
int bpos;
int last_ptr = 0;
int stout;
int stin;
int sterr;
struct termios oldit;
struct termios oldot;
@ -251,7 +272,9 @@ void bwave_create_packet() {
for (j=0;j<conf.mail_conferences[i]->mail_area_count;j++) {
if (conf.mail_conferences[i]->mail_areas[j]->read_sec_level <= gUser->sec_level && conf.mail_conferences[i]->mail_areas[j]->qwkname != NULL) {
lasttot = totmsgs;
printf("Scan Started\n");
totmsgs = bwave_scan_area(i, j, area_count+1, totmsgs, fti_file, mix_file, dat_file, &last_ptr);
printf("Scan complete\n");
s_printf(get_string(195), conf.mail_conferences[i]->name, conf.mail_conferences[i]->mail_areas[j]->name, totmsgs - lasttot);
if (lasttot == totmsgs) {
continue;
@ -354,8 +377,28 @@ void bwave_create_packet() {
buffer[bpos] = '\0';
}
}
if (sshBBS) {
stout = dup(STDOUT_FILENO);
stin = dup(STDIN_FILENO);
sterr = dup(STDERR_FILENO);
dup2(bbs_stdout, STDOUT_FILENO);
dup2(bbs_stderr, STDERR_FILENO);
dup2(bbs_stdin, STDIN_FILENO);
}
system(buffer);
if (sshBBS) {
dup2(stout, STDOUT_FILENO);
dup2(sterr, STDERR_FILENO);
dup2(stin, STDIN_FILENO);
close(stin);
close(stout);
close(sterr);
}
do_download(gUser, archive);
@ -563,6 +606,9 @@ void bwave_upload_reply() {
FILE *msg_file;
int sem_fd;
int msg_count;
int stout;
int stin;
int sterr;
msg_count = 0;
@ -604,8 +650,28 @@ void bwave_upload_reply() {
buffer[bpos] = '\0';
}
}
if (sshBBS) {
stout = dup(STDOUT_FILENO);
stin = dup(STDIN_FILENO);
sterr = dup(STDERR_FILENO);
dup2(bbs_stdout, STDOUT_FILENO);
dup2(bbs_stderr, STDERR_FILENO);
dup2(bbs_stdin, STDIN_FILENO);
}
system(buffer);
if (sshBBS) {
dup2(stout, STDOUT_FILENO);
dup2(sterr, STDERR_FILENO);
dup2(stin, STDIN_FILENO);
close(stin);
close(stout);
close(sterr);
}
unlink(upload_filename);
snprintf(buffer, 1024, "%s/node%d/bwave/%s.UPL", conf.bbs_path, mynode, conf.bwave_name);

53
doors.c
View File

@ -8,6 +8,7 @@
#include <signal.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#if defined(linux)
# include <pty.h>
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
@ -24,7 +25,7 @@ extern struct bbs_config conf;
extern int mynode;
extern int gSocket;
extern int sshBBS;
extern int bbs_stderr;
int running_door_pid = 0;
int running_door = 0;
@ -172,8 +173,12 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c
int i;
int gotiac;
int flush;
struct timeval thetimeout;
struct termios oldit;
struct termios oldot;
struct termios oldit2;
timeoutpaused = 1;
if (write_door32sys(user) != 0) {
@ -189,6 +194,11 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c
door_out = gSocket;
}
if (sshBBS && raw) {
ttySetRaw(STDIN_FILENO, &oldit);
ttySetRaw(STDOUT_FILENO, &oldot);
}
ws.ws_row = 24;
ws.ws_col = 80;
@ -203,8 +213,8 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c
exit(1);
}
if (raw) {
ttySetRaw(master, &oldit);
ttySetRaw(slave, &oldit);
ttySetRaw(master, &oldit2);
ttySetRaw(slave, &oldit2);
}
pid = fork();
if (pid < 0) {
@ -214,8 +224,12 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c
chdir(cwd);
}
close(master);
dup2(slave, 0);
dup2(slave, 1);
if (sshBBS) {
dup2(bbs_stderr, 2);
}
close(slave);
@ -241,6 +255,7 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c
thetimeout.tv_sec = 5;
thetimeout.tv_usec = 0;
ret = select(t, &fdset, NULL, NULL, &thetimeout);
if (ret > 0) {
if (FD_ISSET(door_in, &fdset)) {
@ -258,20 +273,24 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c
if (!running_door) {
continue;
}
if (c == 255) {
if (gotiac == 1) {
write(master, &c, 1);
gotiac = 0;
if (!sshBBS) {
if (c == 255) {
if (gotiac == 1) {
write(master, &c, 1);
gotiac = 0;
} else {
gotiac = 1;
}
} else {
gotiac = 1;
if (gotiac < 2 && gotiac != 0) {
gotiac++;
} else {
write(master, &c, 1);
gotiac = 0;
}
}
} else {
if (gotiac < 2 && gotiac != 0) {
gotiac++;
} else {
write(master, &c, 1);
gotiac = 0;
}
write(master, &c, 1);
}
} else if (FD_ISSET(master, &fdset)) {
len = read(master, &c, 1);
@ -279,7 +298,7 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c
close(master);
break;
}
if (c == 255) {
if (c == 255 && !sshBBS) {
write(door_out, &c, 1);
}
write(door_out, &c, 1);
@ -292,6 +311,10 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c
}
}
}
if (sshBBS && raw) {
tcsetattr(STDIN_FILENO, TCSANOW, &oldit);
tcsetattr(STDOUT_FILENO, TCSANOW, &oldot);
}
} else {
if (!sshBBS) {

32
files.c
View File

@ -9,6 +9,7 @@
#include <errno.h>
#include <termios.h>
#include <dirent.h>
#include <fcntl.h>
#include "Xmodem/zmodem.h"
#include "bbs.h"
#include "lua/lua.h"
@ -18,6 +19,9 @@ extern struct bbs_config conf;
extern int gSocket;
extern int sshBBS;
extern int mynode;
extern int bbs_stdin;
extern int bbs_stdout;
extern int bbs_stderr;
struct file_entry {
char *filename;
@ -229,6 +233,9 @@ char *get_file_id_diz(char *filename) {
int len;
int ext;
int arch;
int stout;
int stin;
int sterr;
ext = 0;
arch = -1;
@ -289,8 +296,29 @@ char *get_file_id_diz(char *filename) {
buffer[bpos] = '\0';
}
}
if (sshBBS) {
stout = dup(STDOUT_FILENO);
stin = dup(STDIN_FILENO);
sterr = dup(STDERR_FILENO);
dup2(bbs_stdout, STDOUT_FILENO);
dup2(bbs_stderr, STDERR_FILENO);
dup2(bbs_stdin, STDIN_FILENO);
}
system(buffer);
if (sshBBS) {
dup2(stout, STDOUT_FILENO);
dup2(sterr, STDERR_FILENO);
dup2(stin, STDIN_FILENO);
close(stin);
close(stout);
close(sterr);
}
snprintf(buffer, 1024, "%s/node%d/temp/FILE_ID.DIZ", conf.bbs_path, mynode);
if (stat(buffer, &s) != 0) {
snprintf(buffer, 1024, "%s/node%d/temp/file_id.diz", conf.bbs_path, mynode);
@ -502,10 +530,6 @@ int do_upload(struct user_record *user, char *final_path) {
}
arguments[bpos] = NULL;
for (i=0;i<bpos;i++) {
printf("\"%s\"\n", arguments[i]);
}
arguments[0] = upload_command;
snprintf(upload_path, 1024, "%s/node%d/upload/", conf.bbs_path, mynode);

52
main.c
View File

@ -39,6 +39,10 @@ int ssh_pid = -1;
int bbs_pid = 0;
int server_socket = -1;
int bbs_stdin;
int bbs_stdout;
int bbs_stderr;
#if defined(ENABLE_WWW)
struct MHD_Daemon *www_daemon;
#endif
@ -672,7 +676,14 @@ void serverssh(int port) {
short events;
ssh_message message;
struct termios tios;
struct ip_address_guard *ip_guard;
int i;
char buffer[1024];
FILE *fptr;
bbs_stdin = dup(STDIN_FILENO);
bbs_stdout = dup(STDOUT_FILENO);
bbs_stderr = dup(STDERR_FILENO);
err = ssh_init();
if (err == -1) {
@ -699,6 +710,45 @@ void serverssh(int port) {
while (1) {
if (ssh_bind_accept(p_ssh_bind, p_ssh_session) == SSH_OK) {
ip = ssh_getip(p_ssh_session);
if (conf.ipguard_enable) {
i = hashmap_get(ip_guard_map, ip, (void **)(&ip_guard));
if (i == MAP_MISSING) {
ip_guard = (struct ip_address_guard *)malloc(sizeof(struct ip_address_guard));
ip_guard->status = IP_STATUS_UNKNOWN;
ip_guard->last_connection = time(NULL);
ip_guard->connection_count = 1;
hashmap_put(ip_guard_map, strdup(ip), ip_guard);
} else if (i == MAP_OK) {
if (ip_guard->status == IP_STATUS_BLACKLISTED) {
free(ip);
ssh_disconnect(p_ssh_session);
continue;
} else if (ip_guard->status == IP_STATUS_UNKNOWN) {
if (ip_guard->last_connection + conf.ipguard_timeout > time(NULL)) {
ip_guard->connection_count++;
if (ip_guard->connection_count == conf.ipguard_tries) {
ip_guard->status = IP_STATUS_BLACKLISTED;
snprintf(buffer, 1024, "%s/blacklist.ip", conf.bbs_path);
fptr = fopen(buffer, "a");
fprintf(fptr, "%s\n", ip);
fclose(fptr);
free(ip);
ssh_disconnect(p_ssh_session);
continue;
}
} else {
ip_guard->connection_count = 0;
ip_guard->last_connection = time(NULL);
}
}
}
}
pid = fork();
if (pid == 0) {
if (ssh_handle_key_exchange(p_ssh_session)) {
@ -753,7 +803,7 @@ void serverssh(int port) {
exit(-1);
}
ip = ssh_getip(p_ssh_session);