From f214eb2fafc902342333c8dee412a45dc78e488f Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Tue, 16 Aug 2016 20:28:48 +1000 Subject: [PATCH] Started on a WWW server, not complete disabled by default --- Makefile.freebsd | 7 +- Makefile.freebsd.WWW | 19 ++++++ bbs.h | 12 +++- config_default/bbs.ini | 3 + doors.c | 2 +- lua/Makefile | 2 +- main.c | 44 +++++++++++-- www.c | 142 +++++++++++++++++++++++++++++++++++++++++ www/footer.tpl | 2 + www/header.tpl | 4 ++ www/index.tpl | 3 + 11 files changed, 229 insertions(+), 11 deletions(-) create mode 100644 Makefile.freebsd.WWW create mode 100644 www.c create mode 100644 www/footer.tpl create mode 100644 www/header.tpl create mode 100644 www/index.tpl diff --git a/Makefile.freebsd b/Makefile.freebsd index 36ceb6b..d1780d2 100644 --- a/Makefile.freebsd +++ b/Makefile.freebsd @@ -1,16 +1,17 @@ CC=cc -CFLAGS=-I/usr/local/include +CFLAGS=-I/usr/local/include DEPS = bbs.h JAMLIB = jamlib/jamlib.a ZMODEM = Xmodem/libzmodem.a LUA = lua/liblua.a +MICROHTTPD= -OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o +OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o www.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) magicka: $(OBJ) - $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) -lutil -lm -lssl -lcrypto -lssh + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) -lutil -lm -lssl -lcrypto -lssh $(MICROHTTPD) .PHONY: clean diff --git a/Makefile.freebsd.WWW b/Makefile.freebsd.WWW new file mode 100644 index 0000000..09a3abb --- /dev/null +++ b/Makefile.freebsd.WWW @@ -0,0 +1,19 @@ +CC=cc +CFLAGS=-I/usr/local/include -DENABLE_WWW=1 +DEPS = bbs.h +JAMLIB = jamlib/jamlib.a +ZMODEM = Xmodem/libzmodem.a +LUA = lua/liblua.a +MICROHTTPD=-lmicrohttpd + +OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o chat_system.o email.o files.o settings.o lua_glue.o strings.o www.o +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +magicka: $(OBJ) + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 $(JAMLIB) $(ZMODEM) $(LUA) -lutil -lm -lssl -lcrypto -lssh $(MICROHTTPD) + +.PHONY: clean + +clean: + rm -f $(OBJ) magicka diff --git a/bbs.h b/bbs.h index a739f5d..86471ac 100644 --- a/bbs.h +++ b/bbs.h @@ -2,6 +2,9 @@ #define __BBS_H__ #include +#if defined(ENABLE_WWW) +#include +#endif #include "lua/lua.h" #include "lua/lauxlib.h" @@ -91,7 +94,9 @@ struct bbs_config { char *netmail_sem; char *default_tagline; int telnet_port; - + int www_server; + int www_port; + char *www_path; int ssh_server; int ssh_port; char *ssh_dsa_key; @@ -195,4 +200,9 @@ extern void lua_push_cfunctions(lua_State *L); extern void load_strings(); extern char *get_string(int offset); + +#if defined(ENABLE_WWW) +extern int www_handler(void * cls, struct MHD_Connection * connection, const char * url, const char * method, const char * version, const char * upload_data, size_t * upload_data_size, void ** ptr); +#endif + #endif diff --git a/config_default/bbs.ini b/config_default/bbs.ini index b9ab499..2d0a972 100644 --- a/config_default/bbs.ini +++ b/config_default/bbs.ini @@ -12,12 +12,15 @@ External Editor cmd = /home/andrew/MagickaBBS/doors/oedit.sh External Editor stdio = false Automessage Write Level = 10 Fork = false +Enable WWW = false +WWW Port = 8080 Enable SSH = false SSH Port = 2024 SSH DSA Key = /home/andrew/MagickaBBS/keys/ssh_host_dsa_key SSH RSA Key = /home/andrew/MagickaBBS/keys/ssh_host_rsa_key [paths] +WWW Path = /home/andrew/MagickaBBS/www String File = /home/andrew/MagickaBBS/magicka.strings PID File = /home/andrew/MagickaBBS/magicka.pid ANSI Path = /home/andrew/MagickaBBS/ansis diff --git a/doors.c b/doors.c index 39ec7eb..85321dc 100644 --- a/doors.c +++ b/doors.c @@ -174,7 +174,7 @@ void rundoor(struct user_record *user, char *cmd, int stdio) { if (openpty(&master, &slave, NULL, NULL, &ws) == 0) { sa.sa_handler = doorchld_handler; sigemptyset(&sa.sa_mask); - sa.sa_flags = SA_RESTART; + sa.sa_flags = SA_RESTART | SA_SIGINFO; if (sigaction(SIGCHLD, &sa, NULL) == -1) { perror("sigaction"); exit(1); diff --git a/lua/Makefile b/lua/Makefile index 2715483..79f5ce8 100644 --- a/lua/Makefile +++ b/lua/Makefile @@ -105,7 +105,7 @@ minix: $(MAKE) $(ALL) CC="clang" SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-L/usr/pkg/lib -Wl,-E -lreadline" freebsd: - $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -lreadline" + $(MAKE) $(ALL) CC="cc" SYSCFLAGS="-DLUA_USE_LINUX -I/usr/local/include" SYSLIBS="-Wl,-E -L/usr/local/lib -lreadline" generic: $(ALL) diff --git a/main.c b/main.c index 7843f4f..699cc46 100644 --- a/main.c +++ b/main.c @@ -22,6 +22,9 @@ #else # include #endif +#if defined(ENABLE_WWW) +# include +#endif #include #include "bbs.h" #include "inih/ini.h" @@ -33,6 +36,10 @@ int ssh_pid = -1; int bbs_pid = 0; int server_socket = -1; +#if defined(ENABLE_WWW) +struct MHD_Daemon *www_daemon; +#endif + void sigterm_handler(int s) { if (ssh_pid != -1) { @@ -41,6 +48,11 @@ void sigterm_handler(int s) if (server_socket != -1) { close(server_socket); } +#if defined(ENABLE_WWW) + if (www_daemon != NULL) { + MHD_stop_daemon(www_daemon); + } +#endif remove(conf.pid_file); exit(0); } @@ -259,6 +271,14 @@ static int handler(void* user, const char* section, const char* name, } else { conf->ssh_server = 0; } + } else if (strcasecmp(name, "enable www") == 0) { + if (strcasecmp(value, "true") == 0) { + conf->www_server = 1; + } else { + conf->www_server = 0; + } + } else if (strcasecmp(name, "www port") == 0) { + conf->www_port = atoi(value); } else if (strcasecmp(name, "ssh port") == 0) { conf->ssh_port = atoi(value); } else if (strcasecmp(name, "ssh dsa key") == 0) { @@ -313,6 +333,8 @@ static int handler(void* user, const char* section, const char* name, conf->pid_file = strdup(value); } else if (strcasecmp(name, "string file") == 0) { conf->string_file = strdup(value); + } else if (strcasecmp(name, "www path") == 0) { + conf->www_path = strdup(value); } } else if (strcasecmp(section, "mail conferences") == 0) { if (conf->mail_conference_count == 0) { @@ -627,29 +649,34 @@ void server(int port) { int client_sock, c; int pid; struct sockaddr_in server, client; +#if defined(ENABLE_WWW) + www_daemon = NULL; +#endif sa.sa_handler = sigchld_handler; // reap all dead processes sigemptyset(&sa.sa_mask); - sa.sa_flags = SA_RESTART; + sa.sa_flags = SA_RESTART | SA_SIGINFO; if (sigaction(SIGCHLD, &sa, NULL) == -1) { + perror("sigaction - sigchld"); remove(conf.pid_file); - perror("sigaction"); exit(1); } st.sa_handler = sigterm_handler; sigemptyset(&st.sa_mask); + st.sa_flags = SA_SIGINFO; if (sigaction(SIGTERM, &st, NULL) == -1) { + perror("sigaction - sigterm"); remove(conf.pid_file); - perror("sigaction"); exit(1); } sq.sa_handler = sigterm_handler; sigemptyset(&sq.sa_mask); + sq.sa_flags = SA_SIGINFO; if (sigaction(SIGQUIT, &sq, NULL) == -1) { + perror("sigaction - sigquit"); remove(conf.pid_file); - perror("sigaction"); exit(1); } @@ -667,6 +694,12 @@ void server(int port) { } } +#if defined(ENABLE_WWW) + if (conf.www_server && conf.www_path != NULL) { + www_daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, conf.www_port, NULL, NULL, &www_handler, NULL, MHD_OPTION_END); + } +#endif + server_socket = socket(AF_INET, SOCK_STREAM, 0); if (server_socket == -1) { remove(conf.pid_file); @@ -741,7 +774,8 @@ int main(int argc, char **argv) { conf.netmail_sem = NULL; conf.telnet_port = 0; conf.string_file = NULL; - + conf.www_path = NULL; + // Load BBS data if (ini_parse(argv[1], handler, &conf) <0) { fprintf(stderr, "Unable to load configuration ini (%s)!\n", argv[1]); diff --git a/www.c b/www.c new file mode 100644 index 0000000..c468ba7 --- /dev/null +++ b/www.c @@ -0,0 +1,142 @@ +#if defined(ENABLE_WWW) + +#include +#include +#include +#include +#include "bbs.h" + +extern struct bbs_config conf; + +int www_handler(void * cls, struct MHD_Connection * connection, const char * url, const char * method, const char * version, const char * upload_data, size_t * upload_data_size, void ** ptr) { + struct MHD_Response *response; + + int ret; + char *page; + char buffer[4096]; + struct stat s; + char *header; + char *footer; + char *whole_page; + FILE *fptr; + + snprintf(buffer, 4096, "%s/header.tpl", conf.www_path); + + header = NULL; + + if (stat(buffer, &s) == 0) { + header = (char *)malloc(s.st_size + 1); + if (header == NULL) { + return MHD_NO; + } + fptr = fopen(buffer, "r"); + if (fptr) { + fread(header, s.st_size, 1, fptr); + fclose(fptr); + } else { + free(header); + header = NULL; + } + } + + if (header == NULL) { + header = (char *)malloc(strlen(conf.bbs_name) * 2 + 61); + if (header == NULL) { + return MHD_NO; + } + sprintf(header, "\n\n%s\n\n\n

%s


", conf.bbs_name, conf.bbs_name); + } + + snprintf(buffer, 4096, "%s/footer.tpl", conf.www_path); + + footer = NULL; + + if (stat(buffer, &s) == 0) { + footer = (char *)malloc(s.st_size + 1); + if (footer == NULL) { + free(header); + return MHD_NO; + } + fptr = fopen(buffer, "r"); + if (fptr) { + fread(footer, s.st_size, 1, fptr); + fclose(fptr); + } else { + free(footer); + footer = NULL; + } + } + + if (footer == NULL) { + footer = (char *)malloc(43); + if (footer == NULL) { + free(header); + return MHD_NO; + } + sprintf(footer, "
Powered by Magicka BBS"); + } + + if (strcmp(method, "GET") == 0) { + if (strcasecmp(url, "/") == 0) { + + snprintf(buffer, 4096, "%s/index.tpl", conf.www_path); + + page = NULL; + + if (stat(buffer, &s) == 0) { + page = (char *)malloc(s.st_size + 1); + if (page == NULL) { + free(header); + free(footer); + return MHD_NO; + } + fptr = fopen(buffer, "r"); + if (fptr) { + fread(page, s.st_size, 1, fptr); + fclose(fptr); + } else { + free(page); + page = NULL; + } + } + + if (page == NULL) { + page = (char *)malloc(16); + if (page == NULL) { + free(header); + free(footer); + return MHD_NO; + } + sprintf(page, "Missing Content"); + } + + whole_page = (char *)malloc(strlen(header) + strlen(page) + strlen(footer) + 1); + + sprintf(whole_page, "%s%s%s", header, page, footer); + } else if (strncasecmp(url, "/static/", 8) == 0) { + // sanatize path + + // load file + } else { + free(header); + free(footer); + return MHD_NO; + } + } else if (strcmp(method, "POST") == 0) { + free(header); + free(footer); + return MHD_NO; + } + + response = MHD_create_response_from_buffer (strlen (whole_page), (void*) whole_page, MHD_RESPMEM_PERSISTENT); + + ret = MHD_queue_response (connection, MHD_HTTP_OK, response); + MHD_destroy_response (response); + free(whole_page); + free(page); + free(header); + free(footer); + + return ret; +} +#endif diff --git a/www/footer.tpl b/www/footer.tpl new file mode 100644 index 0000000..e04310f --- /dev/null +++ b/www/footer.tpl @@ -0,0 +1,2 @@ + + diff --git a/www/header.tpl b/www/header.tpl new file mode 100644 index 0000000..ef464c5 --- /dev/null +++ b/www/header.tpl @@ -0,0 +1,4 @@ + + + + diff --git a/www/index.tpl b/www/index.tpl new file mode 100644 index 0000000..6128ddd --- /dev/null +++ b/www/index.tpl @@ -0,0 +1,3 @@ +

Welcome to another Magicka BBS!

+ +The sysop should customize this file with what he wants on the front page!