From c882d77892f0bded38e55804de8a8954abeded52 Mon Sep 17 00:00:00 2001 From: Nishi Date: Wed, 25 Sep 2024 13:35:04 +0000 Subject: [PATCH] adding chroot git-svn-id: file:///raid/svn-personal/tewi/trunk@161 8739d7e6-ffea-ec47-b151-bdff447c6205 --- README | 2 +- Server/config.c | 21 +++++++++++++-------- Server/server.c | 22 ++++++++++++++++++++-- Server/tw_version.h | 2 +- Tool/genconf.c | 3 +-- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/README b/README index 6ecc3c8..10ad07e 100644 --- a/README +++ b/README @@ -1,5 +1,5 @@ -Tewi HTTPd version 1.07A +Tewi HTTPd version 1.08 Original by Nishi diff --git a/Server/config.c b/Server/config.c index 5dfdb61..a6a4d08 100644 --- a/Server/config.c +++ b/Server/config.c @@ -102,6 +102,9 @@ void tw_config_init(void) { config.server_admin = cm_strdup(SERVER_ADMIN); config.defined[0] = NULL; gethostname(config.hostname, 1024); +#ifdef HAS_CHROOT + tw_add_define("HAS_CHROOT"); +#endif } int tw_config_read(const char* path) { @@ -272,6 +275,16 @@ int tw_config_read(const char* path) { if(current->sslcert != NULL) free(current->sslcert); current->sslcert = cm_strdup(r[1]); } +#endif +#ifdef HAS_CHROOT + } else if(cm_strcaseequ(r[0], "ChrootDirectory")) { + if(r[1] == NULL) { + cm_log("Config", "Missing path at line %d", ln); + stop = 1; + } else { + if(current->chroot_path != NULL) free(current->chroot_path); + current->chroot_path = cm_strdup(r[1]); + } #endif } else if(cm_strcaseequ(r[0], "ForceLog")) { if(r[1] == NULL) { @@ -346,14 +359,6 @@ int tw_config_read(const char* path) { if(current->root != NULL) free(current->root); current->root = cm_strdup(strcmp(r[1], "/") == 0 ? "" : r[1]); } - } else if(cm_strcaseequ(r[0], "ServerRoot")) { - if(r[1] == NULL) { - cm_log("Config", "Missing path at line %d", ln); - stop = 1; - } else { - if(config.server_root != NULL) free(config.server_root); - config.server_root = cm_strdup(r[1]); - } } else if(cm_strcaseequ(r[0], "MIMEType")) { if(r[1] == NULL) { cm_log("Config", "Missing extension at line %d", ln); diff --git a/Server/server.c b/Server/server.c index 361a6e5..dea62ec 100644 --- a/Server/server.c +++ b/Server/server.c @@ -279,6 +279,8 @@ const char* tw_http_status(int code) { return "403 Forbidden"; } else if(code == 404) { return "404 Not Found"; + } else if(code == 500) { + return "500 Internal Server Error"; } else { return "400 Bad Request"; } @@ -507,6 +509,7 @@ int32_t tw_server_pass(void* ptr) { } } } + bool rej = false; cm_log("Server", "Host is %s", vhost); int port = s == NULL ? 80 : 443; char* host = cm_strdup(vhost); @@ -520,6 +523,20 @@ int32_t tw_server_pass(void* ptr) { name = host; cm_log("Server", "Hostname is `%s', port is `%d'", host, port); struct tw_config_entry* vhost_entry = tw_vhost_match(host, port); +#ifdef HAS_CHROOT + char* chrootpath = vhost_entry->chroot_path != NULL ? vhost_entry->chroot_path : config.root.chroot_path; + if(chrootpath != NULL) { + if(chdir(chrootpath) == 0) { + if(chroot(".") == 0) { + cm_log("Server", "Chroot successful"); + } + } else { + cm_log("Server", "chdir() failed, cannot chroot"); + tw_http_error(s, sock, 500, name, port, vhost_entry); + rej = true; + } + } +#endif for(i = 0; i < config.module_count; i++) { tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "mod_request"); if(mod_req != NULL) { @@ -544,7 +561,6 @@ int32_t tw_server_pass(void* ptr) { cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root); char* path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path); cm_log("Server", "Filesystem path is %s", path); - bool rej = false; #ifdef __MINGW32__ char* rpath = cm_strdup(path); for(i = strlen(rpath) - 1; i >= 0; i--) { @@ -776,7 +792,9 @@ int32_t tw_server_pass(void* ptr) { fclose(f); } } else { - tw_http_error(s, sock, 404, name, port, vhost_entry); + if(!rej) { + tw_http_error(s, sock, 404, name, port, vhost_entry); + } } free(path); } diff --git a/Server/tw_version.h b/Server/tw_version.h index e491efa..ed6becd 100644 --- a/Server/tw_version.h +++ b/Server/tw_version.h @@ -7,7 +7,7 @@ extern "C" { #endif -#define TW_VERSION "1.07A\0" +#define TW_VERSION "1.08\0" const char* tw_get_version(void); const char* tw_get_platform(void); diff --git a/Tool/genconf.c b/Tool/genconf.c index f9d14a9..450c7e0 100644 --- a/Tool/genconf.c +++ b/Tool/genconf.c @@ -42,8 +42,7 @@ int main(int argc, char** argv) { printf("Readme README\n"); printf("\n"); printf("DocumentRoot %s/www\n", argv[1]); - printf("\n"); printf("BeginDirectory %s/www\n", argv[1]); - printf("\tAllow all\n"); + printf(" Allow all\n"); printf("EndDirectory\n", argv[1]); } -- 2.45.2