From a3476238d40dc2f53e5156d8e68abcc37d0fa162 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Thu, 22 Feb 2018 21:27:50 +1000 Subject: [PATCH] Fix tinys crashes. --- src/www.c | 32 +++++++-------- src/www_files.c | 107 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 118 insertions(+), 21 deletions(-) diff --git a/src/www.c b/src/www.c index dfcc3b5..11b3763 100644 --- a/src/www.c +++ b/src/www.c @@ -897,24 +897,24 @@ int www_handler(void * cls, struct MHD_Connection * connection, const char * url if (conf.file_directories[file_dir]->display_on_web) { // send file filename = www_files_get_from_area(file_dir, file_sub, filen); - mime = NULL; - // get mimetype - for (i=strlen(filename);i>0;--i) { - if (filename[i] == '.') { - mime = www_get_mime_type(&filename[i+1]); - break; - } - if (filename[i] == '/') { - mime = www_get_mime_type(NULL); - break; - } - } - - if (mime = NULL) { - mime = www_get_mime_type(NULL); - } free(filen); if (filename != NULL) { + mime = NULL; + // get mimetype + for (i=strlen(filename);i>0;--i) { + if (filename[i] == '.') { + mime = www_get_mime_type(&filename[i+1]); + break; + } + if (filename[i] == '/') { + mime = www_get_mime_type(NULL); + break; + } + } + + if (mime = NULL) { + mime = www_get_mime_type(NULL); + } if (stat(filename, &s) == 0 && S_ISREG(s.st_mode)) { fno = open(filename, O_RDONLY); if (fno != -1) { diff --git a/src/www_files.c b/src/www_files.c index f5fa0a3..a32e331 100644 --- a/src/www_files.c +++ b/src/www_files.c @@ -11,6 +11,57 @@ extern struct bbs_config conf; extern struct user_record *gUser; extern char * aha(char *input); +static char *www_decode(char *clean_url) { + char *url = (char *)malloc(strlen(clean_url) + 1); + int i; + int j = 0; + unsigned char c; + if (clean_url == NULL) { + free(url); + return NULL; + } + + for (i=0;i%s", conf.www_url, dir, sub, basename(filename), basename(filename)); + clean_url = www_encode(basename(filename)); + snprintf(buffer, 4096, "%s", conf.www_url, dir, sub, basename(clean_url), basename(filename)); + free(clean_url); free(filename); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; @@ -410,17 +464,60 @@ char *www_files_areas() { return page; } -char *www_files_get_from_area(int dir, int sub, char *file) { - char *sql = "SELECT filename FROM files WHERE approved=1 AND filename LIKE ?"; +char *www_files_get_from_area(int dir, int sub, char *clean_file) { + char *sql = "SELECT filename FROM files WHERE approved=1 AND filename LIKE ? ESCAPE \"^\""; char *filenamelike; sqlite3 *db; sqlite3_stmt *res; int rc; char buffer[PATH_MAX]; char *ret = NULL; + int i; + int extra = 0; + int j; + char *file; - filenamelike = (char *)malloc(strlen(file) + 3); - sprintf(filenamelike, "%%/%s", file); + file = www_decode(clean_file); + + for (i=0;ifile_subs[sub]->database);