From a42963a77df2dffa08afcb09d43cff48a68f4e6d Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Wed, 17 Aug 2016 19:26:01 +1000 Subject: [PATCH] Added email listing to www server --- Makefile.freebsd.WWW | 4 +- bbs.c | 3 +- bbs.h | 1 + email.c | 2 +- www.c | 118 +++++++++++++++++++++++++++++++++++++++++++ www/401.tpl | 2 + www_email.c | 107 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 233 insertions(+), 4 deletions(-) create mode 100644 www/401.tpl create mode 100644 www_email.c diff --git a/Makefile.freebsd.WWW b/Makefile.freebsd.WWW index 09a3abb..f6675a0 100644 --- a/Makefile.freebsd.WWW +++ b/Makefile.freebsd.WWW @@ -4,9 +4,9 @@ DEPS = bbs.h JAMLIB = jamlib/jamlib.a ZMODEM = Xmodem/libzmodem.a LUA = lua/liblua.a -MICROHTTPD=-lmicrohttpd +MICROHTTPD=-lmicrohttpd -lb64 -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 +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 www_email.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/bbs.c b/bbs.c index d64f53b..475d614 100644 --- a/bbs.c +++ b/bbs.c @@ -24,7 +24,7 @@ int sshBBS; int usertimeout; int timeoutpaused; -char *ipaddress; +char *ipaddress = NULL; void sigterm_handler2(int s) { @@ -512,6 +512,7 @@ void runbbs_real(int socket, char *ip, int ssh) { st.sa_handler = sigterm_handler2; sigemptyset(&st.sa_mask); + st.sa_flags = SA_SIGINFO; if (sigaction(SIGTERM, &st, NULL) == -1) { dolog("Failed to setup sigterm handler."); exit(1); diff --git a/bbs.h b/bbs.h index fccaf38..52e2205 100644 --- a/bbs.h +++ b/bbs.h @@ -205,6 +205,7 @@ extern void chomp(char *string); #if defined(ENABLE_WWW) extern void www_init(); 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); +extern char *www_email_summary(struct user_record *user); #endif #endif diff --git a/email.c b/email.c index 08826ff..7241c0f 100644 --- a/email.c +++ b/email.c @@ -296,7 +296,7 @@ void list_emails(struct user_record *user) { return; } - msgid = 0; + msgid = 0; s_printf(get_string(63)); while (sqlite3_step(res) == SQLITE_ROW) { from = strdup((char *)sqlite3_column_text(res, 0)); diff --git a/www.c b/www.c index 7cd2869..e9ae4e1 100644 --- a/www.c +++ b/www.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "bbs.h" extern struct bbs_config conf; @@ -72,6 +73,59 @@ char *www_get_mime_type(const char *extension) { return default_mime_type; } +int www_401(char *header, char *footer, struct MHD_Connection * connection) { + char buffer[4096]; + char *page; + struct stat s; + char *whole_page; + struct MHD_Response *response; + int ret; + FILE *fptr; + + snprintf(buffer, 4096, "%s/401.tpl", conf.www_path); + + page = NULL; + + if (stat(buffer, &s) == 0) { + page = (char *)malloc(s.st_size + 1); + if (page == NULL) { + return -1; + } + memset(page, 0, s.st_size + 1); + 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) { + return -1; + } + sprintf(page, "Missing Content"); + } + + whole_page = (char *)malloc(strlen(header) + strlen(page) + strlen(footer) + 1); + + sprintf(whole_page, "%s%s%s", header, page, footer); + + response = MHD_create_response_from_buffer (strlen(whole_page), (void*)whole_page, MHD_RESPMEM_PERSISTENT); + + MHD_add_response_header(response, "WWW-Authenticate", "Basic realm=\"BBS Area\""); + + ret = MHD_queue_response (connection, 401, response); + MHD_destroy_response (response); + free(whole_page); + free(page); + + return 0; +} + int www_404(char *header, char *footer, struct MHD_Connection * connection) { char buffer[4096]; char *page; @@ -174,6 +228,40 @@ int www_403(char *header, char *footer, struct MHD_Connection * connection) { return 0; } +struct user_record *www_auth_ok(struct MHD_Connection *connection, const char *url) { + char *user_password = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, "Authorization"); + base64_decodestate state; + char decoded_pass[34]; + int len; + char *username; + char *password; + int i; + + if (user_password == NULL) { + return NULL; + } + + if (strncasecmp(user_password, "basic ", 6) == 0) { + if (strlen(&user_password[6]) <= 48) { + base64_init_decodestate(&state); + len = base64_decode_block(&user_password[6], strlen(&user_password[6]), decoded_pass, &state); + decoded_pass[len] = '\0'; + + username = decoded_pass; + for (i=0;i401 - Not authorized +The page you are looking for can not be accessed. diff --git a/www_email.c b/www_email.c new file mode 100644 index 0000000..d47e843 --- /dev/null +++ b/www_email.c @@ -0,0 +1,107 @@ +#if defined(ENABLE_WWW) + +#include +#include +#include +#include + +#include "bbs.h" + + +extern struct bbs_config conf; + +char *www_email_summary(struct user_record *user) { + char *page; + int max_len; + int len; + char buffer[4096]; + sqlite3 *db; + sqlite3_stmt *res; + int rc; + char *email_summary_sql = "SELECT sender,subject,seen,date FROM email WHERE recipient LIKE ?"; + struct tm msg_date; + time_t date; + char *from; + char *subject; + int seen; + int msgid = 0; + char *err_msg = 0; + char *email_create_sql = "CREATE TABLE IF NOT EXISTS email (" + "id INTEGER PRIMARY KEY," + "sender TEXT COLLATE NOCASE," + "recipient TEXT COLLATE NOCASE," + "subject TEXT," + "body TEXT," + "date INTEGER," + "seen INTEGER);"; + + + page = (char *)malloc(4096); + max_len = 4096; + len = 0; + memset(page, 0, 4096); + + sprintf(buffer, "

Your Email

\n"); + if (len + strlen(buffer) > max_len - 1) { + max_len += 4096; + page = (char *)realloc(page, max_len); + } + strcat(page, buffer); + sprintf(buffer, "%s/email.sq3", conf.bbs_path); + + rc = sqlite3_open(buffer, &db); + if (rc != SQLITE_OK) { + sqlite3_close(db); + free(page); + return NULL; + } + + rc = sqlite3_exec(db, email_create_sql, 0, 0, &err_msg); + if (rc != SQLITE_OK ) { + sqlite3_free(err_msg); + sqlite3_close(db); + + return NULL; + } + + rc = sqlite3_prepare_v2(db, email_summary_sql, -1, &res, 0); + + if (rc == SQLITE_OK) { + sqlite3_bind_text(res, 1, user->loginname, -1, 0); + } else { + sqlite3_finalize(res); + sqlite3_close(db); + free(page); + return NULL; + } + + while (sqlite3_step(res) == SQLITE_ROW) { + from = strdup((char *)sqlite3_column_text(res, 0)); + subject = strdup((char *)sqlite3_column_text(res, 1)); + seen = sqlite3_column_int(res, 2); + date = (time_t)sqlite3_column_int(res, 3); + localtime_r(&date, &msg_date); + if (seen == 0) { + sprintf(buffer, "
%d
%s
%d:%2d %d-%d-%d
\n", msgid + 1, msgid + 1, subject, from, msg_date.tm_hour, msg_date.tm_min, msg_date.tm_mday, msg_date.tm_mon + 1, msg_date.tm_year - 100); + if (len + strlen(buffer) > max_len - 1) { + max_len += 4096; + page = (char *)realloc(page, max_len); + } + strcat(page, buffer); + } else { + sprintf(buffer, "
%d
%s
%d:%2d %d-%d-%d
\n", msgid + 1, msgid + 1, subject, from, msg_date.tm_hour, msg_date.tm_min, msg_date.tm_mday, msg_date.tm_mon + 1, msg_date.tm_year - 100); + if (len + strlen(buffer) > max_len - 1) { + max_len += 4096; + page = (char *)realloc(page, max_len); + } + strcat(page, buffer); + } + free(from); + free(subject); + } + sqlite3_finalize(res); + sqlite3_close(db); + return page; +} + +#endif