Think I may have fixed it!

This commit is contained in:
Andrew Pamment 2018-01-21 19:05:01 +10:00
parent aac4aaad00
commit 69897e7955
6 changed files with 157 additions and 71 deletions

2
.gitignore vendored
View File

@ -76,3 +76,5 @@ menus/logoff.mnu
menus/mail.mnu menus/mail.mnu
menus/main.mnu menus/main.mnu
.vscode/settings.json .vscode/settings.json
core
.vscode/launch.json

146
src/bbs.c
View File

@ -89,8 +89,8 @@ void broadcast(char *mess, ...) {
} }
} }
void dolog(char *fmt, ...) { void dolog_www(char *ipaddr, char *fmt, ...) {
char buffer[512]; char buffer[PATH_MAX];
struct tm time_now; struct tm time_now;
time_t timen; time_t timen;
FILE *logfptr; FILE *logfptr;
@ -102,14 +102,42 @@ void dolog(char *fmt, ...) {
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, PATH_MAX, "%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) {
return; return;
} }
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
vsnprintf(buffer, 512, fmt, ap); vsnprintf(buffer, PATH_MAX, fmt, 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, ipaddr, buffer);
fclose(logfptr);
}
void dolog(char *fmt, ...) {
char buffer[PATH_MAX];
struct tm time_now;
time_t timen;
FILE *logfptr;
int mypid = getpid();
if (conf.log_path == NULL) return;
timen = time(NULL);
localtime_r(&timen, &time_now);
snprintf(buffer, PATH_MAX, "%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");
if (!logfptr) {
return;
}
va_list ap;
va_start(ap, fmt);
vsnprintf(buffer, PATH_MAX, 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);
@ -1076,48 +1104,80 @@ int copy_file(char *src, char *dest) {
return 0; return 0;
} }
char *str_replace(char *orig, char *rep, char *with) { char *str_replace(const char *str, const char *from, const char *to) {
char *result; // the return string /* Adjust each of the below values to suit your needs. */
char *ins; // the next insert point
char *tmp; // varies
int len_rep; // length of rep (the string to remove)
int len_with; // length of with (the string to replace rep with)
int len_front; // distance between rep and end of last rep
int count; // number of replacements
// sanity checks and initialization /* Increment positions cache size initially by this number. */
if (!orig || !rep) size_t cache_sz_inc = 16;
return NULL; /* Thereafter, each time capacity needs to be increased,
len_rep = strlen(rep); * multiply the increment by this factor. */
if (len_rep == 0) const size_t cache_sz_inc_factor = 3;
return NULL; // empty rep causes infinite loop during count /* But never increment capacity by more than this number. */
if (!with) const size_t cache_sz_inc_max = 1048576;
with = "";
len_with = strlen(with);
// count the number of replacements needed char *pret, *ret = NULL;
ins = orig; const char *pstr2, *pstr = str;
for (count = 0; tmp = strstr(ins, rep); ++count) { size_t i, count = 0;
ins = tmp + len_rep; uintptr_t *pos_cache_tmp, *pos_cache = NULL;
size_t cache_sz = 0;
size_t cpylen, orglen, retlen, tolen, fromlen = strlen(from);
/* Find all matches and cache their positions. */
while ((pstr2 = strstr(pstr, from)) != NULL) {
count++;
/* Increase the cache size when necessary. */
if (cache_sz < count) {
cache_sz += cache_sz_inc;
pos_cache_tmp = realloc(pos_cache, sizeof(*pos_cache) * cache_sz);
if (pos_cache_tmp == NULL) {
goto end_repl_str;
} else pos_cache = pos_cache_tmp;
cache_sz_inc *= cache_sz_inc_factor;
if (cache_sz_inc > cache_sz_inc_max) {
cache_sz_inc = cache_sz_inc_max;
}
} }
tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1); pos_cache[count-1] = pstr2 - str;
pstr = pstr2 + fromlen;
if (!result)
return NULL;
// first time through the loop, all the variable are set correctly
// from here on,
// tmp points to the end of the result string
// ins points to the next occurrence of rep in orig
// orig points to the remainder of orig after "end of rep"
while (count--) {
ins = strstr(orig, rep);
len_front = ins - orig;
tmp = strncpy(tmp, orig, len_front) + len_front;
tmp = strcpy(tmp, with) + len_with;
orig += len_front + len_rep; // move to next "end of rep"
} }
strcpy(tmp, orig);
return result; orglen = pstr - str + strlen(pstr);
/* Allocate memory for the post-replacement string. */
if (count > 0) {
tolen = strlen(to);
retlen = orglen + (tolen - fromlen) * count;
} else retlen = orglen;
ret = malloc(retlen + 1);
if (ret == NULL) {
goto end_repl_str;
}
if (count == 0) {
/* If no matches, then just duplicate the string. */
strcpy(ret, str);
} else {
/* Otherwise, duplicate the string whilst performing
* the replacements using the position cache. */
pret = ret;
memcpy(pret, str, pos_cache[0]);
pret += pos_cache[0];
for (i = 0; i < count; i++) {
memcpy(pret, to, tolen);
pret += tolen;
pstr = str + pos_cache[i] + fromlen;
cpylen = (i == count-1 ? orglen : pos_cache[i+1]) - pos_cache[i] - fromlen;
memcpy(pret, pstr, cpylen);
pret += cpylen;
}
ret[retlen] = '\0';
}
end_repl_str:
/* Free the cache and return the post-replacement string,
* which will be NULL in the event of an error. */
free(pos_cache);
return ret;
} }

View File

@ -229,12 +229,13 @@ struct msg_headers {
int msg_count; int msg_count;
}; };
extern char *str_replace(char *orig, char *rep, char *with); extern char *str_replace(const char *orig, const char *rep, const char *with);
extern int copy_file(char *src, char *dest); extern int copy_file(char *src, char *dest);
extern int recursive_delete(const char *dir); extern int recursive_delete(const char *dir);
extern void automessage_write(struct user_record *user); extern void automessage_write(struct user_record *user);
extern void automessage_display(); extern void automessage_display();
extern void dolog(char *fmt, ...); extern void dolog(char *fmt, ...);
extern void dolog_www(char *ipaddr, char *fmt, ...);
extern void runbbs_ssh(char *ipaddress); extern void runbbs_ssh(char *ipaddress);
extern void runbbs(int sock, char *ipaddress); extern void runbbs(int sock, char *ipaddress);
extern struct fido_addr *parse_fido_addr(const char *str); extern struct fido_addr *parse_fido_addr(const char *str);

View File

@ -41,16 +41,17 @@ struct connection_info_s {
void *www_logger(void * cls, const char * uri, struct MHD_Connection *con) { void *www_logger(void * cls, const char * uri, struct MHD_Connection *con) {
struct sockaddr *so = (struct sockaddr *)MHD_get_connection_info(con, MHD_CONNECTION_INFO_CLIENT_ADDRESS)->client_addr; struct sockaddr *so = (struct sockaddr *)MHD_get_connection_info(con, MHD_CONNECTION_INFO_CLIENT_ADDRESS)->client_addr;
char *ipaddr;
if (so->sa_family == AF_INET) { if (so->sa_family == AF_INET) {
ipaddress = (char *)malloc(INET_ADDRSTRLEN + 1); ipaddr = (char *)malloc(INET_ADDRSTRLEN + 1);
inet_ntop(AF_INET, &((struct sockaddr_in *)so)->sin_addr, ipaddress, INET_ADDRSTRLEN); inet_ntop(AF_INET, &((struct sockaddr_in *)so)->sin_addr, ipaddr, INET_ADDRSTRLEN);
} else if (so->sa_family == AF_INET6) { } else if (so->sa_family == AF_INET6) {
ipaddress = (char *)malloc(INET6_ADDRSTRLEN + 1); ipaddr = (char *)malloc(INET6_ADDRSTRLEN + 1);
inet_ntop(AF_INET6, &((struct sockaddr_in6 *)so)->sin6_addr, ipaddress, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &((struct sockaddr_in6 *)so)->sin6_addr, ipaddr, INET6_ADDRSTRLEN);
} }
dolog("%s", uri); dolog_www(ipaddr, "%s", uri);
free(ipaddress); free(ipaddr);
ipaddress = NULL;
return NULL; return NULL;
} }
@ -133,18 +134,18 @@ static int iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char
void www_init() { void www_init() {
FILE *fptr; FILE *fptr;
char buffer[4096]; char buffer[PATH_MAX];
int i; int i;
mime_types_count = 0; mime_types_count = 0;
sprintf(buffer, "%s/mime.types", conf.www_path); snprintf(buffer, PATH_MAX, "%s/mime.types", conf.www_path);
fptr = fopen(buffer, "r"); fptr = fopen(buffer, "r");
if (!fptr) { if (!fptr) {
return; return;
} }
fgets(buffer, 4096, fptr); fgets(buffer, 256, fptr);
while (!feof(fptr)) { while (!feof(fptr)) {
chomp(buffer); chomp(buffer);
@ -167,7 +168,7 @@ void www_init() {
} }
} }
fgets(buffer, 4096, fptr); fgets(buffer, 256, fptr);
} }
fclose(fptr); fclose(fptr);
@ -177,6 +178,9 @@ char *www_get_mime_type(const char *extension) {
int i; int i;
static char default_mime_type[] = "application/octet-stream"; static char default_mime_type[] = "application/octet-stream";
if (extension == NULL) {
return default_mime_type;
}
for (i=0;i<mime_types_count;i++) { for (i=0;i<mime_types_count;i++) {
if (strcasecmp(extension, mime_types[i]->ext) == 0) { if (strcasecmp(extension, mime_types[i]->ext) == 0) {
@ -187,7 +191,7 @@ char *www_get_mime_type(const char *extension) {
} }
int www_401(char *header, char *footer, struct MHD_Connection * connection) { int www_401(char *header, char *footer, struct MHD_Connection * connection) {
char buffer[4096]; char buffer[PATH_MAX];
char *page, *page_tmp; char *page, *page_tmp;
struct stat s; struct stat s;
char *whole_page; char *whole_page;
@ -195,7 +199,7 @@ int www_401(char *header, char *footer, struct MHD_Connection * connection) {
int ret; int ret;
FILE *fptr; FILE *fptr;
snprintf(buffer, 4096, "%s/401.tpl", conf.www_path); snprintf(buffer, PATH_MAX, "%s/401.tpl", conf.www_path);
page_tmp = NULL; page_tmp = NULL;
@ -242,7 +246,7 @@ int www_401(char *header, char *footer, struct MHD_Connection * connection) {
} }
int www_404(char *header, char *footer, struct MHD_Connection * connection) { int www_404(char *header, char *footer, struct MHD_Connection * connection) {
char buffer[4096]; char buffer[PATH_MAX];
char *page, *page_tmp; char *page, *page_tmp;
struct stat s; struct stat s;
char *whole_page; char *whole_page;
@ -250,7 +254,7 @@ int www_404(char *header, char *footer, struct MHD_Connection * connection) {
int ret; int ret;
FILE *fptr; FILE *fptr;
snprintf(buffer, 4096, "%s/404.tpl", conf.www_path); snprintf(buffer, PATH_MAX, "%s/404.tpl", conf.www_path);
page_tmp = NULL; page_tmp = NULL;
@ -295,7 +299,7 @@ int www_404(char *header, char *footer, struct MHD_Connection * connection) {
} }
int www_403(char *header, char *footer, struct MHD_Connection * connection) { int www_403(char *header, char *footer, struct MHD_Connection * connection) {
char buffer[4096]; char buffer[PATH_MAX];
char *page, *page_tmp; char *page, *page_tmp;
struct stat s; struct stat s;
char *whole_page; char *whole_page;
@ -303,7 +307,7 @@ int www_403(char *header, char *footer, struct MHD_Connection * connection) {
int ret; int ret;
FILE *fptr; FILE *fptr;
snprintf(buffer, 4096, "%s/403.tpl", conf.www_path); snprintf(buffer, PATH_MAX, "%s/403.tpl", conf.www_path);
page_tmp = NULL; page_tmp = NULL;
@ -393,7 +397,7 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url
int ret; int ret;
char *page, *page_tmp; char *page, *page_tmp;
char buffer[4096]; char buffer[PATH_MAX];
struct stat s; struct stat s;
char *header, *header_tmp; char *header, *header_tmp;
char *footer, *footer_tmp; char *footer, *footer_tmp;
@ -411,6 +415,7 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url
const char *val; const char *val;
int skip; int skip;
char *replyid; char *replyid;
// char *static_buffer;
if (strcmp(method, "GET") == 0) { if (strcmp(method, "GET") == 0) {
if (*ptr == NULL) { if (*ptr == NULL) {
@ -446,7 +451,7 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url
con_inf = *ptr; con_inf = *ptr;
snprintf(buffer, 4096, "%s/header.tpl", conf.www_path); snprintf(buffer, PATH_MAX, "%s/header.tpl", conf.www_path);
header_tmp = NULL; header_tmp = NULL;
@ -477,9 +482,9 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url
header = str_replace(header_tmp, "@@WWW_URL@@", conf.www_url); header = str_replace(header_tmp, "@@WWW_URL@@", conf.www_url);
free(header_tmp); free(header_tmp);
snprintf(buffer, 4096, "%s/footer.tpl", conf.www_path); snprintf(buffer, PATH_MAX, "%s/footer.tpl", conf.www_path);
footer = NULL; footer_tmp = NULL;
if (stat(buffer, &s) == 0) { if (stat(buffer, &s) == 0) {
footer_tmp = (char *)malloc(s.st_size + 1); footer_tmp = (char *)malloc(s.st_size + 1);
@ -513,7 +518,7 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url
if (strcmp(method, "GET") == 0) { if (strcmp(method, "GET") == 0) {
if (strcasecmp(url, "/") == 0) { if (strcasecmp(url, "/") == 0) {
snprintf(buffer, 4096, "%s/index.tpl", conf.www_path); snprintf(buffer, PATH_MAX, "%s/index.tpl", conf.www_path);
page_tmp = NULL; page_tmp = NULL;
@ -760,22 +765,40 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url
} else if (strncasecmp(url, "/static/", 8) == 0) { } else if (strncasecmp(url, "/static/", 8) == 0) {
// sanatize path // sanatize path
if (strstr(url, "/..") != NULL) { if (strstr(url, "/..") != NULL) {
free(header);
free(footer);
return MHD_NO; return MHD_NO;
} }
mime = NULL;
// get mimetype // get mimetype
for (i=strlen(url);i>0;--i) { for (i=strlen(url);i>0;--i) {
if (url[i] == '.') { if (url[i] == '.') {
mime = www_get_mime_type(&url[i+1]); mime = www_get_mime_type(&url[i+1]);
break; break;
} }
if (url[i] == '/') {
mime = www_get_mime_type(NULL);
break;
} }
}
if (mime = NULL) {
mime = www_get_mime_type(NULL);
}
// load file // load file
sprintf(buffer, "%s%s", conf.www_path, url); sprintf(buffer, "%s%s", conf.www_path, url);
if (stat(buffer, &s) == 0 && S_ISREG(s.st_mode)) { if (stat(buffer, &s) == 0 && S_ISREG(s.st_mode)) {
fno = open(buffer, O_RDONLY); fno = open(buffer, O_RDONLY);
if (fno != -1) { if (fno != -1) {
//static_buffer = (char *)malloc(s.st_size + 1);
//read(fno, static_buffer, s.st_size);
response = MHD_create_response_from_fd(s.st_size, fno); response = MHD_create_response_from_fd(s.st_size, fno);
//response = MHD_create_response_from_buffer (s.st_size, (void*) static_buffer, MHD_RESPMEM_MUST_FREE);
MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mime); MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mime);
ret = MHD_queue_response (connection, MHD_HTTP_OK, response); ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
MHD_destroy_response (response); MHD_destroy_response (response);

View File

@ -566,7 +566,7 @@ char *www_email_summary(struct user_record *user) {
strcat(page, buffer); strcat(page, buffer);
len += strlen(buffer); len += strlen(buffer);
sprintf(buffer, "<div class=\"button\"><a href=\"%semail/new\">New Email</a></div>\n", config.www_url); sprintf(buffer, "<div class=\"button\"><a href=\"%semail/new\">New Email</a></div>\n", conf.www_url);
if (len + strlen(buffer) > max_len - 1) { if (len + strlen(buffer) > max_len - 1) {
max_len += 4096; max_len += 4096;
page = (char *)realloc(page, max_len); page = (char *)realloc(page, max_len);