replace sleep with waitpid

This commit is contained in:
Andrew Pamment 2016-08-10 11:22:55 +10:00
parent ddd874c4b8
commit b84d9af16a
2 changed files with 353 additions and 352 deletions

702
bbs.c
View File

@ -10,62 +10,62 @@
#include <sys/utsname.h> #include <sys/utsname.h>
#include <sys/time.h> #include <sys/time.h>
#include <stdarg.h> #include <stdarg.h>
#include "bbs.h" #include "bbs.h"
#include "lua/lua.h" #include "lua/lua.h"
#include "lua/lualib.h" #include "lua/lualib.h"
#include "lua/lauxlib.h" #include "lua/lauxlib.h"
int mynode = 0; int mynode = 0;
struct bbs_config conf; struct bbs_config conf;
struct user_record *gUser; struct user_record *gUser;
int gSocket; int gSocket;
int sshBBS; int sshBBS;
int usertimeout; int usertimeout;
int timeoutpaused; int timeoutpaused;
char *ipaddress; char *ipaddress;
void sigterm_handler2(int s) void sigterm_handler2(int s)
{ {
if (mynode != 0) { if (mynode != 0) {
disconnect("Terminated."); disconnect("Terminated.");
} }
dolog("Terminated..."); dolog("Terminated...");
exit(0); exit(0);
} }
void sigint_handler(int s) void sigint_handler(int s)
{ {
// do nothing... // do nothing...
} }
void dolog(char *fmt, ...) { void dolog(char *fmt, ...) {
char buffer[512]; char buffer[512];
struct tm time_now; struct tm time_now;
time_t timen; time_t timen;
FILE *logfptr; FILE *logfptr;
int mypid = getpid(); int mypid = getpid();
if (conf.log_path == NULL) return; if (conf.log_path == NULL) return;
timen = time(NULL); timen = time(NULL);
localtime_r(&timen, &time_now); localtime_r(&timen, &time_now);
snprintf(buffer, 512, "%s/%04d%02d%02d.log", conf.log_path, time_now.tm_year + 1900, time_now.tm_mon + 1, time_now.tm_mday); snprintf(buffer, 512, "%s/%04d%02d%02d.log", conf.log_path, time_now.tm_year + 1900, time_now.tm_mon + 1, time_now.tm_mday);
logfptr = fopen(buffer, "a"); logfptr = fopen(buffer, "a");
if (!logfptr) { if (!logfptr) {
dolog("Error opening log file!"); dolog("Error opening log file!");
return; return;
} }
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vsnprintf(buffer, 512, fmt, ap); vsnprintf(buffer, 512, fmt, ap);
va_end(ap); va_end(ap);
fprintf(logfptr, "%02d:%02d:%02d [%d][%s] %s\n", time_now.tm_hour, time_now.tm_min, time_now.tm_sec, mypid, ipaddress, buffer); fprintf(logfptr, "%02d:%02d:%02d [%d][%s] %s\n", time_now.tm_hour, time_now.tm_min, time_now.tm_sec, mypid, ipaddress, buffer);
fclose(logfptr); fclose(logfptr);
} }
@ -73,12 +73,12 @@ struct fido_addr *parse_fido_addr(const char *str) {
struct fido_addr *ret = (struct fido_addr *)malloc(sizeof(struct fido_addr)); struct fido_addr *ret = (struct fido_addr *)malloc(sizeof(struct fido_addr));
int c; int c;
int state = 0; int state = 0;
ret->zone = 0; ret->zone = 0;
ret->net = 0; ret->net = 0;
ret->node = 0; ret->node = 0;
ret->point = 0; ret->point = 0;
for (c=0;c<strlen(str);c++) { for (c=0;c<strlen(str);c++) {
switch(str[c]) { switch(str[c]) {
case ':': case ':':
@ -130,200 +130,200 @@ void timer_handler(int signum) {
if (signum == SIGALRM) { if (signum == SIGALRM) {
if (gUser != NULL) { if (gUser != NULL) {
gUser->timeleft--; gUser->timeleft--;
if (gUser->timeleft <= 0) {
s_printf("\r\n\r\nSorry, you're out of time today..\r\n");
disconnect("Out of Time");
}
} if (gUser->timeleft <= 0) {
s_printf("\r\n\r\nSorry, you're out of time today..\r\n");
disconnect("Out of Time");
}
}
if (timeoutpaused == 0) { if (timeoutpaused == 0) {
usertimeout--; usertimeout--;
} }
if (usertimeout <= 0) { if (usertimeout <= 0) {
s_printf("\r\n\r\nTimeout waiting for input..\r\n"); s_printf("\r\n\r\nTimeout waiting for input..\r\n");
disconnect("Timeout"); disconnect("Timeout");
} }
} }
} }
void s_printf(char *fmt, ...) { void s_printf(char *fmt, ...) {
char buffer[512]; char buffer[512];
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vsnprintf(buffer, 512, fmt, ap); vsnprintf(buffer, 512, fmt, ap);
va_end(ap); va_end(ap);
s_putstring(buffer); s_putstring(buffer);
}
void s_putchar(char c) {
if (sshBBS) {
putchar(c);
} else {
write(gSocket, &c, 1);
}
} }
void s_putstring(char *c) { void s_putchar(char c) {
if (sshBBS) { if (sshBBS) {
printf("%s", c); putchar(c);
} else { } else {
write(gSocket, c, strlen(c)); write(gSocket, &c, 1);
} }
} }
void s_displayansi_p(char *file) { void s_putstring(char *c) {
if (sshBBS) {
printf("%s", c);
} else {
write(gSocket, c, strlen(c));
}
}
void s_displayansi_p(char *file) {
FILE *fptr; FILE *fptr;
char c; char c;
fptr = fopen(file, "r"); fptr = fopen(file, "r");
if (!fptr) { if (!fptr) {
return; return;
} }
c = fgetc(fptr); c = fgetc(fptr);
while (!feof(fptr) && c != 0x1a) { while (!feof(fptr) && c != 0x1a) {
s_putchar(c); s_putchar(c);
c = fgetc(fptr); c = fgetc(fptr);
} }
fclose(fptr); fclose(fptr);
} }
void s_displayansi(char *file) { void s_displayansi(char *file) {
FILE *fptr; FILE *fptr;
char c; char c;
char buffer[256]; char buffer[256];
sprintf(buffer, "%s/%s.ans", conf.ansi_path, file); sprintf(buffer, "%s/%s.ans", conf.ansi_path, file);
fptr = fopen(buffer, "r"); fptr = fopen(buffer, "r");
if (!fptr) { if (!fptr) {
return; return;
} }
c = fgetc(fptr); c = fgetc(fptr);
while (!feof(fptr) && c != 0x1a) { while (!feof(fptr) && c != 0x1a) {
s_putchar(c); s_putchar(c);
c = fgetc(fptr); c = fgetc(fptr);
} }
fclose(fptr); fclose(fptr);
} }
char s_getchar() { char s_getchar() {
unsigned char c; unsigned char c;
int len; int len;
do { do {
if (sshBBS) { if (sshBBS) {
c = getchar(); c = getchar();
} else { } else {
len = read(gSocket, &c, 1); len = read(gSocket, &c, 1);
if (len == 0) { if (len == 0) {
disconnect("Socket Closed"); disconnect("Socket Closed");
} }
} }
if (!sshBBS) { if (!sshBBS) {
while (c == 255) { while (c == 255) {
len = read(gSocket, &c, 1); len = read(gSocket, &c, 1);
if (len == 0) { if (len == 0) {
disconnect("Socket Closed"); disconnect("Socket Closed");
} else if (c == 255) { } else if (c == 255) {
usertimeout = 10; usertimeout = 10;
return c; return c;
} }
len = read(gSocket, &c, 1); len = read(gSocket, &c, 1);
if (len == 0) { if (len == 0) {
disconnect("Socket Closed"); disconnect("Socket Closed");
} }
len = read(gSocket, &c, 1); len = read(gSocket, &c, 1);
if (len == 0) { if (len == 0) {
disconnect("Socket Closed"); disconnect("Socket Closed");
} }
} }
} }
} while (c == '\n'); } while (c == '\n');
usertimeout = 10; usertimeout = 10;
return (char)c; return (char)c;
} }
char s_getc() { char s_getc() {
char c = s_getchar(); char c = s_getchar();
s_putchar(c); s_putchar(c);
return (char)c; return (char)c;
} }
void s_readstring(char *buffer, int max) { void s_readstring(char *buffer, int max) {
int i; int i;
char c; char c;
memset(buffer, 0, max); memset(buffer, 0, max);
for (i=0;i<max;i++) { for (i=0;i<max;i++) {
c = s_getchar(); c = s_getchar();
if ((c == '\b' || c == 127) && i > 0) { if ((c == '\b' || c == 127) && i > 0) {
buffer[i-1] = '\0'; buffer[i-1] = '\0';
i -= 2; i -= 2;
s_printf("\e[D \e[D"); s_printf("\e[D \e[D");
continue; continue;
} else if (c == '\b' || c == 127) { } else if (c == '\b' || c == 127) {
i -= 1; i -= 1;
continue; continue;
} }
if (c == '\n' || c == '\r') { if (c == '\n' || c == '\r') {
return; return;
} }
s_putchar(c); s_putchar(c);
buffer[i] = c; buffer[i] = c;
buffer[i+1] = '\0'; buffer[i+1] = '\0';
} }
} }
void s_readpass(char *buffer, int max) { void s_readpass(char *buffer, int max) {
int i; int i;
char c; char c;
for (i=0;i<max;i++) { for (i=0;i<max;i++) {
c = s_getchar(); c = s_getchar();
if ((c == '\b' || c == 127) && i > 0) { if ((c == '\b' || c == 127) && i > 0) {
buffer[i-1] = '\0'; buffer[i-1] = '\0';
i-=2; i-=2;
s_printf("\e[D \e[D"); s_printf("\e[D \e[D");
continue; continue;
} else if (c == '\b' || c == 127) { } else if (c == '\b' || c == 127) {
i -= 1; i -= 1;
continue; continue;
} }
if (c == '\n' || c == '\r') { if (c == '\n' || c == '\r') {
return; return;
} }
s_putchar('*'); s_putchar('*');
buffer[i] = c; buffer[i] = c;
buffer[i+1] = '\0'; buffer[i+1] = '\0';
} }
} }
void disconnect(char *calledby) { void disconnect(char *calledby) {
char buffer[256]; char buffer[256];
if (gUser != NULL) { if (gUser != NULL) {
save_user(gUser); save_user(gUser);
} }
dolog("Node %d disconnected (%s)", mynode, calledby); dolog("Node %d disconnected (%s)", mynode, calledby);
sprintf(buffer, "%s/nodeinuse.%d", conf.bbs_path, mynode); sprintf(buffer, "%s/nodeinuse.%d", conf.bbs_path, mynode);
remove(buffer); remove(buffer);
if (!sshBBS) { if (!sshBBS) {
close(gSocket); close(gSocket);
} }
exit(0); exit(0);
} }
@ -332,7 +332,7 @@ void record_last10_callers(struct user_record *user) {
struct last10_callers callers[10]; struct last10_callers callers[10];
int i,j; int i,j;
FILE *fptr = fopen("last10.dat", "rb"); FILE *fptr = fopen("last10.dat", "rb");
if (fptr != NULL) { if (fptr != NULL) {
for (i=0;i<10;i++) { for (i=0;i<10;i++) {
@ -344,13 +344,13 @@ void record_last10_callers(struct user_record *user) {
} else { } else {
i = 0; i = 0;
} }
if (strcasecmp(conf.sysop_name, user->loginname) != 0 ) { if (strcasecmp(conf.sysop_name, user->loginname) != 0 ) {
memset(&new_entry, 0, sizeof(struct last10_callers)); memset(&new_entry, 0, sizeof(struct last10_callers));
strcpy(new_entry.name, user->loginname); strcpy(new_entry.name, user->loginname);
strcpy(new_entry.location, user->location); strcpy(new_entry.location, user->location);
new_entry.time = time(NULL); new_entry.time = time(NULL);
if (i == 10) { if (i == 10) {
j = 1; j = 1;
} else { } else {
@ -365,99 +365,99 @@ void record_last10_callers(struct user_record *user) {
} }
} }
void display_last10_callers(struct user_record *user) { void display_last10_callers(struct user_record *user) {
struct last10_callers callers[10]; struct last10_callers callers[10];
int i,z; int i,z;
struct tm l10_time; struct tm l10_time;
FILE *fptr = fopen("last10.dat", "rb"); FILE *fptr = fopen("last10.dat", "rb");
s_printf("\r\n\e[1;37mLast 10 callers:\r\n"); s_printf("\r\n\e[1;37mLast 10 callers:\r\n");
s_printf("\e[1;30m-------------------------------------------------------------------------------\r\n"); s_printf("\e[1;30m-------------------------------------------------------------------------------\r\n");
if (fptr != NULL) { if (fptr != NULL) {
for (i=0;i<10;i++) { for (i=0;i<10;i++) {
if (fread(&callers[i], sizeof(struct last10_callers), 1, fptr) < 1) { if (fread(&callers[i], sizeof(struct last10_callers), 1, fptr) < 1) {
break; break;
} }
} }
fclose(fptr); fclose(fptr);
} else { } else {
i = 0; i = 0;
} }
for (z=0;z<i;z++) { for (z=0;z<i;z++) {
localtime_r(&callers[z].time, &l10_time); localtime_r(&callers[z].time, &l10_time);
s_printf("\e[1;37m%-16s \e[1;36m%-32s \e[1;32m%02d:%02d %02d-%02d-%02d\e[0m\r\n", callers[z].name, callers[z].location, l10_time.tm_hour, l10_time.tm_min, l10_time.tm_mday, l10_time.tm_mon + 1, l10_time.tm_year - 100); s_printf("\e[1;37m%-16s \e[1;36m%-32s \e[1;32m%02d:%02d %02d-%02d-%02d\e[0m\r\n", callers[z].name, callers[z].location, l10_time.tm_hour, l10_time.tm_min, l10_time.tm_mday, l10_time.tm_mon + 1, l10_time.tm_year - 100);
} }
s_printf("\e[1;30m-------------------------------------------------------------------------------\e[0m\r\n"); s_printf("\e[1;30m-------------------------------------------------------------------------------\e[0m\r\n");
s_printf("Press any key to continue...\r\n"); s_printf("Press any key to continue...\r\n");
s_getc(); s_getc();
} }
void display_info() { void display_info() {
struct utsname name; struct utsname name;
uname(&name); uname(&name);
s_printf("\r\n\r\n\e[1;37mSystem Information\r\n"); s_printf("\r\n\r\n\e[1;37mSystem Information\r\n");
s_printf("\e[1;30m----------------------------------------------\r\n"); s_printf("\e[1;30m----------------------------------------------\r\n");
s_printf("\e[1;32mBBS Name : \e[1;37m%s\r\n", conf.bbs_name); s_printf("\e[1;32mBBS Name : \e[1;37m%s\r\n", conf.bbs_name);
s_printf("\e[1;32mSysOp Name : \e[1;37m%s\r\n", conf.sysop_name); s_printf("\e[1;32mSysOp Name : \e[1;37m%s\r\n", conf.sysop_name);
s_printf("\e[1;32mNode : \e[1;37m%d\r\n", mynode); s_printf("\e[1;32mNode : \e[1;37m%d\r\n", mynode);
s_printf("\e[1;32mBBS Version : \e[1;37mMagicka %d.%d (%s)\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_STR); s_printf("\e[1;32mBBS Version : \e[1;37mMagicka %d.%d (%s)\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_STR);
s_printf("\e[1;32mSystem : \e[1;37m%s (%s)\r\n", name.sysname, name.machine); s_printf("\e[1;32mSystem : \e[1;37m%s (%s)\r\n", name.sysname, name.machine);
s_printf("\e[1;30m----------------------------------------------\e[0m\r\n"); s_printf("\e[1;30m----------------------------------------------\e[0m\r\n");
s_printf("Press any key to continue...\r\n"); s_printf("Press any key to continue...\r\n");
s_getc(); s_getc();
} }
void automessage_write(struct user_record *user) { void automessage_write(struct user_record *user) {
FILE *fptr; FILE *fptr;
char automsg[450]; char automsg[450];
char buffer[76]; char buffer[76];
int i; int i;
struct tm timenow; struct tm timenow;
time_t timen; time_t timen;
memset(automsg, 0, 450); memset(automsg, 0, 450);
memset(buffer, 0, 76); memset(buffer, 0, 76);
if (user->sec_level >= conf.automsgwritelvl) { if (user->sec_level >= conf.automsgwritelvl) {
timen = time(NULL); timen = time(NULL);
localtime_r(&timen, &timenow); localtime_r(&timen, &timenow);
sprintf(automsg, "Automessage Posted by %s @ %s", user->loginname, asctime(&timenow)); sprintf(automsg, "Automessage Posted by %s @ %s", user->loginname, asctime(&timenow));
automsg[strlen(automsg) - 1] = '\r'; automsg[strlen(automsg) - 1] = '\r';
automsg[strlen(automsg)] = '\n'; automsg[strlen(automsg)] = '\n';
s_printf("\r\nEnter your message (4 lines):\r\n"); s_printf("\r\nEnter your message (4 lines):\r\n");
for (i=0;i<4;i++) { for (i=0;i<4;i++) {
s_printf("\r\n%d: ", i); s_printf("\r\n%d: ", i);
s_readstring(buffer, 75); s_readstring(buffer, 75);
strcat(automsg, buffer); strcat(automsg, buffer);
strcat(automsg, "\r\n"); strcat(automsg, "\r\n");
} }
fptr = fopen("automessage.txt", "w"); fptr = fopen("automessage.txt", "w");
if (fptr) { if (fptr) {
fwrite(automsg, strlen(automsg), 1, fptr); fwrite(automsg, strlen(automsg), 1, fptr);
fclose(fptr); fclose(fptr);
} else { } else {
dolog("Unable to open automessage.txt for writing"); dolog("Unable to open automessage.txt for writing");
} }
} }
} }
void automessage_display() { void automessage_display() {
struct stat s; struct stat s;
FILE *fptr; FILE *fptr;
char buffer[90]; char buffer[90];
int i; int i;
s_printf("\r\n\r\n"); s_printf("\r\n\r\n");
if (stat("automessage.txt", &s) == 0) { if (stat("automessage.txt", &s) == 0) {
fptr = fopen("automessage.txt", "r"); fptr = fopen("automessage.txt", "r");
if (fptr) { if (fptr) {
@ -466,21 +466,21 @@ void automessage_display() {
fgets(buffer, 88, fptr); fgets(buffer, 88, fptr);
buffer[strlen(buffer) - 1] = '\r'; buffer[strlen(buffer) - 1] = '\r';
buffer[strlen(buffer)] = '\n'; buffer[strlen(buffer)] = '\n';
s_printf(buffer); s_printf(buffer);
} }
fclose(fptr); fclose(fptr);
} else { } else {
dolog("Error opening automessage.txt"); dolog("Error opening automessage.txt");
} }
} else { } else {
s_printf("No automessage!\r\n"); s_printf("No automessage!\r\n");
} }
s_printf("\e[0mPress any key to continue...\r\n"); s_printf("\e[0mPress any key to continue...\r\n");
s_getc(); s_getc();
} }
void runbbs_real(int socket, char *ip, int ssh) { void runbbs_real(int socket, char *ip, int ssh) {
char buffer[256]; char buffer[256];
char password[17]; char password[17];
@ -494,172 +494,172 @@ void runbbs_real(int socket, char *ip, int ssh) {
struct tm oldtime; struct tm oldtime;
time_t now; time_t now;
struct itimerval itime; struct itimerval itime;
struct sigaction sa; struct sigaction sa;
struct sigaction st; struct sigaction st;
lua_State *L; lua_State *L;
int do_internal_login = 0; int do_internal_login = 0;
ipaddress = ip;
if (!ssh) {
write(socket, iac_echo, 3);
write(socket, iac_sga, 3);
gUser = NULL;
sshBBS = 0;
} else {
sshBBS = 1;
}
st.sa_handler = sigterm_handler2;
sigemptyset(&st.sa_mask);
if (sigaction(SIGTERM, &st, NULL) == -1) {
dolog("Failed to setup sigterm handler.");
exit(1);
}
gSocket = socket;
s_printf("Magicka BBS v%d.%d (%s), Loading...\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_STR); ipaddress = ip;
// find out which node we are if (!ssh) {
for (i=1;i<=conf.nodes;i++) { write(socket, iac_echo, 3);
write(socket, iac_sga, 3);
gUser = NULL;
sshBBS = 0;
} else {
sshBBS = 1;
}
st.sa_handler = sigterm_handler2;
sigemptyset(&st.sa_mask);
if (sigaction(SIGTERM, &st, NULL) == -1) {
dolog("Failed to setup sigterm handler.");
exit(1);
}
gSocket = socket;
s_printf("Magicka BBS v%d.%d (%s), Loading...\r\n", VERSION_MAJOR, VERSION_MINOR, VERSION_STR);
// find out which node we are
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"); nodefile = fopen(buffer, "w");
if (!nodefile) { if (!nodefile) {
dolog("Error opening nodefile!"); dolog("Error opening nodefile!");
close(socket); close(socket);
exit(1); exit(1);
} }
fputs("UNKNOWN", nodefile); fputs("UNKNOWN", nodefile);
fclose(nodefile); fclose(nodefile);
break; break;
} }
} }
if (mynode == 0) { if (mynode == 0) {
s_printf("Sorry, all nodes are in use. Please try later\r\n"); s_printf("Sorry, all nodes are in use. Please try later\r\n");
if (!ssh) { if (!ssh) {
close(socket); close(socket);
} }
exit(1); exit(1);
} }
usertimeout = 10; usertimeout = 10;
timeoutpaused = 0; timeoutpaused = 0;
memset (&sa, 0, sizeof (sa)); memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler; sa.sa_handler = &timer_handler;
sa.sa_flags = SA_RESTART; sa.sa_flags = SA_RESTART;
sigaction (SIGALRM, &sa, 0); sigaction (SIGALRM, &sa, 0);
itime.it_interval.tv_sec = 60; itime.it_interval.tv_sec = 60;
itime.it_interval.tv_usec = 0; itime.it_interval.tv_usec = 0;
itime.it_value.tv_sec = 60; itime.it_value.tv_sec = 60;
itime.it_value.tv_usec = 0; itime.it_value.tv_usec = 0;
setitimer (ITIMER_REAL, &itime, 0);
s_displayansi("issue");
if (!ssh) {
s_printf("\e[0mEnter your Login Name or NEW to create an account\r\n");
s_printf("Login:> ");
s_readstring(buffer, 25);
if (strcasecmp(buffer, "new") == 0) { setitimer (ITIMER_REAL, &itime, 0);
user = new_user();
gUser = user; s_displayansi("issue");
} else {
s_printf("\r\nPassword:> "); if (!ssh) {
s_readpass(password, 16); s_printf("\e[0mEnter your Login Name or NEW to create an account\r\n");
user = check_user_pass(buffer, password); s_printf("Login:> ");
if (user == NULL) {
s_printf("\r\nIncorrect Login.\r\n"); s_readstring(buffer, 25);
disconnect("Incorrect Login");
} if (strcasecmp(buffer, "new") == 0) {
user = new_user();
gUser = user; gUser = user;
} else {
for (i=1;i<=conf.nodes;i++) { s_printf("\r\nPassword:> ");
sprintf(buffer, "%s/nodeinuse.%d", conf.bbs_path, i); s_readpass(password, 16);
if (stat(buffer, &s) == 0) { user = check_user_pass(buffer, password);
nodefile = fopen(buffer, "r"); if (user == NULL) {
if (!nodefile) { s_printf("\r\nIncorrect Login.\r\n");
dolog("Error opening nodefile!"); disconnect("Incorrect Login");
disconnect("Error opening nodefile!"); }
}
fgets(buffer, 256, nodefile); gUser = user;
if (strcasecmp(user->loginname, buffer) == 0) { for (i=1;i<=conf.nodes;i++) {
fclose(nodefile); sprintf(buffer, "%s/nodeinuse.%d", conf.bbs_path, i);
s_printf("\r\nYou are already logged in.\r\n"); if (stat(buffer, &s) == 0) {
disconnect("Already Logged in"); nodefile = fopen(buffer, "r");
} if (!nodefile) {
fclose(nodefile); dolog("Error opening nodefile!");
} disconnect("Error opening nodefile!");
} }
} fgets(buffer, 256, nodefile);
} else {
if (gUser != NULL) { if (strcasecmp(user->loginname, buffer) == 0) {
user = gUser; fclose(nodefile);
s_printf("\e[0mWelcome back %s. Press enter to log in...\r\n", gUser->loginname); s_printf("\r\nYou are already logged in.\r\n");
s_getc(); disconnect("Already Logged in");
for (i=1;i<=conf.nodes;i++) { }
sprintf(buffer, "%s/nodeinuse.%d", conf.bbs_path, i); fclose(nodefile);
if (stat(buffer, &s) == 0) { }
nodefile = fopen(buffer, "r"); }
if (!nodefile) { }
dolog("Error opening nodefile!"); } else {
disconnect("Error opening nodefile!"); if (gUser != NULL) {
} user = gUser;
fgets(buffer, 256, nodefile); s_printf("\e[0mWelcome back %s. Press enter to log in...\r\n", gUser->loginname);
s_getc();
if (strcasecmp(user->loginname, buffer) == 0) { for (i=1;i<=conf.nodes;i++) {
fclose(nodefile); sprintf(buffer, "%s/nodeinuse.%d", conf.bbs_path, i);
s_printf("\r\nYou are already logged in.\r\n"); if (stat(buffer, &s) == 0) {
disconnect("Already Logged in"); nodefile = fopen(buffer, "r");
} if (!nodefile) {
fclose(nodefile); dolog("Error opening nodefile!");
} disconnect("Error opening nodefile!");
} }
} else { fgets(buffer, 256, nodefile);
s_printf("\e[0mWelcome to %s! Press enter to create an account...\r\n", conf.bbs_name);
s_getc(); if (strcasecmp(user->loginname, buffer) == 0) {
gUser = new_user(); fclose(nodefile);
user = gUser; s_printf("\r\nYou are already logged in.\r\n");
} disconnect("Already Logged in");
} }
fclose(nodefile);
}
}
} else {
s_printf("\e[0mWelcome to %s! Press enter to create an account...\r\n", conf.bbs_name);
s_getc();
gUser = new_user();
user = gUser;
}
}
sprintf(buffer, "%s/nodeinuse.%d", conf.bbs_path, mynode); sprintf(buffer, "%s/nodeinuse.%d", conf.bbs_path, mynode);
nodefile = fopen(buffer, "w"); nodefile = fopen(buffer, "w");
if (!nodefile) { if (!nodefile) {
dolog("Error opening nodefile!"); dolog("Error opening nodefile!");
close(socket); close(socket);
exit(1); exit(1);
} }
fputs(user->loginname, nodefile); fputs(user->loginname, nodefile);
fclose(nodefile); fclose(nodefile);
// do post-login // do post-login
dolog("%s logged in, on node %d", user->loginname, mynode); dolog("%s logged in, on node %d", user->loginname, mynode);
// check time left // check time left
now = time(NULL); now = time(NULL);
localtime_r(&now, &thetime); localtime_r(&now, &thetime);
localtime_r(&user->laston, &oldtime); localtime_r(&user->laston, &oldtime);
if (thetime.tm_mday != oldtime.tm_mday || thetime.tm_mon != oldtime.tm_mon || thetime.tm_year != oldtime.tm_year) { if (thetime.tm_mday != oldtime.tm_mday || thetime.tm_mon != oldtime.tm_mon || thetime.tm_year != oldtime.tm_year) {
user->timeleft = user->sec_info->timeperday; user->timeleft = user->sec_info->timeperday;
user->laston = now; user->laston = now;
save_user(user); save_user(user);
} }
user->timeson++; user->timeson++;
@ -683,57 +683,57 @@ void runbbs_real(int socket, char *ip, int ssh) {
// bulletins // bulletins
i = 0; i = 0;
sprintf(buffer, "%s/bulletin%d.ans", conf.ansi_path, i); sprintf(buffer, "%s/bulletin%d.ans", conf.ansi_path, i);
while (stat(buffer, &s) == 0) { while (stat(buffer, &s) == 0) {
sprintf(buffer, "bulletin%d", i); sprintf(buffer, "bulletin%d", i);
s_displayansi(buffer); s_displayansi(buffer);
s_printf("\e[0mPress any key to continue...\r\n"); s_printf("\e[0mPress any key to continue...\r\n");
s_getc(); s_getc();
i++; i++;
sprintf(buffer, "%s/bulletin%d.ans", conf.ansi_path, i); sprintf(buffer, "%s/bulletin%d.ans", conf.ansi_path, i);
} }
// external login cmd // external login cmd
// display info // display info
display_info(); display_info();
display_last10_callers(user); display_last10_callers(user);
// check email // check email
i = mail_getemailcount(user); i = mail_getemailcount(user);
if (i > 0) { if (i > 0) {
s_printf("\r\nYou have %d e-mail(s) in your inbox.\r\n", i); s_printf("\r\nYou have %d e-mail(s) in your inbox.\r\n", i);
} else { } else {
s_printf("\r\nYou have no e-mail.\r\n"); s_printf("\r\nYou have no e-mail.\r\n");
} }
mail_scan(user); mail_scan(user);
automessage_display(); automessage_display();
} }
record_last10_callers(user); record_last10_callers(user);
// main menu // main menu
main_menu(user); main_menu(user);
s_displayansi("goodbye"); s_displayansi("goodbye");
dolog("%s is logging out, on node %d", user->loginname, mynode); dolog("%s is logging out, on node %d", user->loginname, mynode);
disconnect("Log out"); disconnect("Log out");
}
void runbbs(int socket, char *ip) {
runbbs_real(socket, ip, 0);
}
void runbbs_ssh(char *ip) {
struct sigaction si;
setbuf(stdin, NULL);
setbuf(stdout, NULL);
si.sa_handler = sigint_handler;
sigemptyset(&si.sa_mask);
if (sigaction(SIGINT, &si, NULL) == -1) {
dolog("Failed to setup sigint handler.");
exit(1);
}
runbbs_real(-1, ip, 1);
} }
void runbbs(int socket, char *ip) {
runbbs_real(socket, ip, 0);
}
void runbbs_ssh(char *ip) {
struct sigaction si;
setbuf(stdin, NULL);
setbuf(stdout, NULL);
si.sa_handler = sigint_handler;
sigemptyset(&si.sa_mask);
if (sigaction(SIGINT, &si, NULL) == -1) {
dolog("Failed to setup sigint handler.");
exit(1);
}
runbbs_real(-1, ip, 1);
}

3
main.c
View File

@ -448,10 +448,11 @@ static int ssh_copy_chan_to_fd(ssh_session session,
static void ssh_chan_close(ssh_session session, ssh_channel channel, void *userdata) { static void ssh_chan_close(ssh_session session, ssh_channel channel, void *userdata) {
int fd = *(int*)userdata; int fd = *(int*)userdata;
int status;
(void)session; (void)session;
(void)channel; (void)channel;
kill(bbs_pid, SIGTERM); kill(bbs_pid, SIGTERM);
sleep(10); waitpid(bbs_pid, &status, 0);
close(fd); close(fd);
} }