Attempt to solve stuck nodes

This commit is contained in:
Andrew Pamment 2018-02-16 21:35:09 +10:00
parent 86025cfc5e
commit 263118a164
2 changed files with 121 additions and 62 deletions

167
src/bbs.c
View File

@ -256,12 +256,18 @@ void s_putchar(char c) {
size_t inc; size_t inc;
size_t ouc; size_t ouc;
size_t sz; size_t sz;
int ret;
if (!should_convert_utf8()) { if (!should_convert_utf8()) {
if (sshBBS) { if (sshBBS) {
putchar(c); putchar(c);
} else { } else {
write(gSocket, &c, 1); ret = send(gSocket, &c, 1, MSG_DONTWAIT);
if (ret == -1) {
if (errno == ECONNRESET) {
disconnect("Disconnected");
}
}
} }
} else { } else {
ic = iconv_open("UTF-8", "CP437"); ic = iconv_open("UTF-8", "CP437");
@ -277,7 +283,12 @@ void s_putchar(char c) {
if (sshBBS) { if (sshBBS) {
fprintf(stdout, "%s", ptr1); fprintf(stdout, "%s", ptr1);
} else { } else {
write(gSocket, ptr1, outbuf - ptr1); ret = send(gSocket, ptr1, outbuf - ptr1, MSG_DONTWAIT);
if (ret == -1) {
if (errno == ECONNRESET) {
disconnect("Disconnected");
}
}
} }
iconv_close(ic); iconv_close(ic);
free(ptr1); free(ptr1);
@ -294,11 +305,17 @@ void s_putstring(char *c) {
size_t sz; size_t sz;
char *ptr1; char *ptr1;
char *ptr2; char *ptr2;
int ret;
if (!should_convert_utf8()) { if (!should_convert_utf8()) {
if (sshBBS) { if (sshBBS) {
fprintf(stdout, "%s", c); fprintf(stdout, "%s", c);
} else { } else {
write(gSocket, c, strlen(c)); ret = send(gSocket, c, strlen(c), MSG_DONTWAIT);
if (ret == -1) {
if (errno == ECONNRESET) {
disconnect("Disconnected");
}
}
} }
} else { } else {
ic = iconv_open("UTF-8", "CP437"); ic = iconv_open("UTF-8", "CP437");
@ -313,7 +330,12 @@ void s_putstring(char *c) {
if (sshBBS) { if (sshBBS) {
fprintf(stdout, "%s", ptr1); fprintf(stdout, "%s", ptr1);
} else { } else {
write(gSocket, ptr1, outbuf - ptr1); ret = send(gSocket, ptr1, outbuf - ptr1, MSG_DONTWAIT);
if (ret == -1) {
if (errno == ECONNRESET) {
disconnect("Disconnected");
}
}
} }
iconv_close(ic); iconv_close(ic);
free(ptr1); free(ptr1);
@ -395,22 +417,25 @@ char s_getchar() {
char iac_binary_do[] = {IAC, IAC_DO, IAC_TRANSMIT_BINARY, '\0'}; char iac_binary_do[] = {IAC, IAC_DO, IAC_TRANSMIT_BINARY, '\0'};
char iac_binary_wont[] = {IAC, IAC_WONT, IAC_TRANSMIT_BINARY, '\0'}; char iac_binary_wont[] = {IAC, IAC_WONT, IAC_TRANSMIT_BINARY, '\0'};
char iac_binary_dont[] = {IAC, IAC_DONT, IAC_TRANSMIT_BINARY, '\0'}; char iac_binary_dont[] = {IAC, IAC_DONT, IAC_TRANSMIT_BINARY, '\0'};
int ret;
do { do {
if (sshBBS) { if (sshBBS) {
c = getchar(); c = getchar();
} else { } else {
do {
len = read(gSocket, &c, 1); len = read(gSocket, &c, 1);
} while (len == -1 && errno == EINTR);
if (len == 0) { if (len <= 0) {
disconnect("Socket Closed"); disconnect("Socket Closed");
} }
} }
if (!sshBBS) { if (!sshBBS) {
while (c == IAC) { while (c == IAC) {
do {
len = read(gSocket, &c, 1); len = read(gSocket, &c, 1);
} while (len == -1 && errno == EINTR);
if (len == 0) { if (len == 0) {
disconnect("Socket Closed"); disconnect("Socket Closed");
} else if (c == IAC) { } else if (c == IAC) {
@ -418,8 +443,10 @@ char s_getchar() {
return c; return c;
} }
if (c == IAC_WILL || c == IAC_WONT || c == IAC_DO || c == IAC_DONT) { if (c == IAC_WILL || c == IAC_WONT || c == IAC_DO || c == IAC_DONT) {
len = read(gSocket, &d, 1); do {
if (len == 0) { len = read(gSocket, &c, 1);
} while (len == -1 && errno == EINTR);
if (len <= 0) {
disconnect("Socket Closed"); disconnect("Socket Closed");
} }
@ -428,7 +455,12 @@ char s_getchar() {
if (d == 0) { if (d == 0) {
if (telnet_bin_mode != 1) { if (telnet_bin_mode != 1) {
telnet_bin_mode = 1; telnet_bin_mode = 1;
write(gSocket, iac_binary_do, 3); ret = send(gSocket, iac_binary_do, 3, MSG_DONTWAIT);
if (ret == -1) {
if (errno == ECONNRESET) {
disconnect("Disconnected");
}
}
} }
} }
break; break;
@ -436,7 +468,12 @@ char s_getchar() {
if (d == 0) { if (d == 0) {
if (telnet_bin_mode != 0) { if (telnet_bin_mode != 0) {
telnet_bin_mode = 0; telnet_bin_mode = 0;
write(gSocket, iac_binary_dont, 3); ret = send(gSocket, iac_binary_dont, 3, MSG_DONTWAIT);
if (ret == -1) {
if (errno == ECONNRESET) {
disconnect("Disconnected");
}
}
} }
} }
break; break;
@ -444,7 +481,13 @@ char s_getchar() {
if (d == 0) { if (d == 0) {
if (telnet_bin_mode != 1) { if (telnet_bin_mode != 1) {
telnet_bin_mode = 1; telnet_bin_mode = 1;
write(gSocket, iac_binary_will, 3); ret = send(gSocket, iac_binary_will, 3, MSG_DONTWAIT);
if (ret == -1) {
if (errno == ECONNRESET) {
disconnect("Disconnected");
}
}
} }
} }
break; break;
@ -452,22 +495,31 @@ char s_getchar() {
if (d == 0) { if (d == 0) {
if (telnet_bin_mode != 0) { if (telnet_bin_mode != 0) {
telnet_bin_mode = 0; telnet_bin_mode = 0;
write(gSocket, iac_binary_wont, 3); ret = send(gSocket, iac_binary_wont, 3, MSG_DONTWAIT);
if (ret == -1) {
if (errno == ECONNRESET) {
disconnect("Disconnected");
}
}
} }
} }
break; break;
} }
} else if (c == 250) { } else if (c == 250) {
do {
do { do {
len = read(gSocket, &c, 1); len = read(gSocket, &c, 1);
if (len == 0) { } while (len == -1 && errno == EINTR);
if (len <= 0) {
disconnect("Socket Closed"); disconnect("Socket Closed");
} }
} while(c != 240); } while(c != 240);
} }
do {
len = read(gSocket, &c, 1); len = read(gSocket, &c, 1);
if (len == 0) { } while (len == -1 && errno == EINTR);
if (len <= 0) {
disconnect("Socket Closed"); disconnect("Socket Closed");
} }
} }
@ -772,25 +824,27 @@ void runbbs_real(int socket, char *ip, int ssh) {
int do_internal_login = 0; int do_internal_login = 0;
int usernotfound; int usernotfound;
int tries; int tries;
int fno;
atexit(exit_bbs); atexit(exit_bbs);
ipaddress = ip; usertimeout = 10;
timeoutpaused = 0;
if (!ssh) {
gUser = NULL; memset (&sa, 0, sizeof (sa));
sshBBS = 0; sa.sa_handler = &timer_handler;
if (write(socket, iac_echo, 3) != 3) { sa.sa_flags = SA_RESTART;
dolog("Failed to send iac_echo"); sigaction (SIGALRM, &sa, 0);
exit(0);
} itime.it_interval.tv_sec = 60;
if (write(socket, iac_sga, 3) != 3) { itime.it_interval.tv_usec = 0;
dolog("Failed to send iac_sga"); itime.it_value.tv_sec = 60;
exit(0); itime.it_value.tv_usec = 0;
}
} else { setitimer (ITIMER_REAL, &itime, 0);
sshBBS = 1;
} ipaddress = ip;
st.sa_handler = sigterm_handler2; st.sa_handler = sigterm_handler2;
sigemptyset(&st.sa_mask); sigemptyset(&st.sa_mask);
@ -799,26 +853,45 @@ void runbbs_real(int socket, char *ip, int ssh) {
dolog("Failed to setup sigterm handler."); dolog("Failed to setup sigterm handler.");
exit(1); exit(1);
} }
if (sigaction(SIGPIPE, &st, NULL) == -1) {
dolog("Failed to setup sigpipe handler.");
exit(1);
}
gSocket = socket; gSocket = socket;
if (!ssh) {
gUser = NULL;
sshBBS = 0;
if (send(socket, iac_echo, 3, MSG_DONTWAIT) != 3) {
dolog("Failed to send iac_echo");
exit(0);
}
if (send(socket, iac_sga, 3, MSG_DONTWAIT) != 3) {
dolog("Failed to send iac_sga");
exit(0);
}
} else {
sshBBS = 1;
}
s_printf("Magicka BBS v%d.%d (%s), Loading...\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_STR); s_printf("Magicka BBS v%d.%d (%s), Loading...\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_STR);
// find out which node we are // find out which node we are
for (i=1;i<=conf.nodes;i++) { for (i=1;i<=conf.nodes;i++) {
sprintf(buffer, "%s/nodeinuse.%d", conf.bbs_path, i); sprintf(buffer, "%s/nodeinuse.%d", conf.bbs_path, i);
if (stat(buffer, &s) != 0) { if (stat(buffer, &s) != 0) {
mynode = i; mynode = i;
nodefile = fopen(buffer, "w"); fno = open(buffer, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (!nodefile) { if (fno == -1) {
dolog("Error opening nodefile!"); dolog("Error opening nodefile!");
close(socket); continue;
exit(1);
} }
fputs("UNKNOWN", nodefile); write(fno, "UNKNOWN", 7);
fclose(nodefile);
close(fno);
break; break;
} }
} }
@ -833,24 +906,10 @@ void runbbs_real(int socket, char *ip, int ssh) {
dolog("Incoming %s connection on node %d", (ssh ? "SSH" : "Telnet"), mynode); dolog("Incoming %s connection on node %d", (ssh ? "SSH" : "Telnet"), mynode);
usertimeout = 10;
timeoutpaused = 0;
tries = 0;
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;
sa.sa_flags = SA_RESTART;
sigaction (SIGALRM, &sa, 0);
itime.it_interval.tv_sec = 60;
itime.it_interval.tv_usec = 0;
itime.it_value.tv_sec = 60;
itime.it_value.tv_usec = 0;
setitimer (ITIMER_REAL, &itime, 0);
s_displayansi("issue"); s_displayansi("issue");
tries = 0;
if (!ssh) { if (!ssh) {
tryagain: tryagain:
s_printf(get_string(19)); s_printf(get_string(19));
@ -863,8 +922,8 @@ tryagain:
if (strcasecmp(buffer, "new") == 0) { if (strcasecmp(buffer, "new") == 0) {
usernotfound = 1; usernotfound = 1;
} else if (check_user(buffer)) { } else if (check_user(buffer)) {
usernotfound = 1;
s_printf(get_string(203)); s_printf(get_string(203));
goto tryagain;
} }
if (usernotfound) { if (usernotfound) {

View File

@ -583,7 +583,7 @@ int check_user(char *loginname) {
} }
struct user_record *new_user() { struct user_record *new_user() {
char buffer[256]; char buffer[PATH_MAX];
struct user_record *user; struct user_record *user;
int done = 0; int done = 0;
char c; char c;
@ -753,7 +753,7 @@ struct user_record *new_user() {
user->sec_level = conf.newuserlvl; user->sec_level = conf.newuserlvl;
user->bwavepktno = 0; user->bwavepktno = 0;
user->sec_info = (struct sec_level_t *)malloc(sizeof(struct sec_level_t)); user->sec_info = (struct sec_level_t *)malloc(sizeof(struct sec_level_t));
sprintf(buffer, "%s/config/s%d.ini", conf.bbs_path, user->sec_level); snprintf(buffer, PATH_MAX, "%s/config/s%d.ini", conf.bbs_path, user->sec_level);
if (ini_parse(buffer, secLevel, user->sec_info) <0) { if (ini_parse(buffer, secLevel, user->sec_info) <0) {
dolog("Unable to load sec Level ini (%s)!", buffer); dolog("Unable to load sec Level ini (%s)!", buffer);