]> Nishi Git Mirror - tewi.git/commitdiff
adding chroot
authorNishi <nishi@nishi.boats>
Wed, 25 Sep 2024 13:35:04 +0000 (13:35 +0000)
committerNishi <nishi@nishi.boats>
Wed, 25 Sep 2024 13:35:04 +0000 (13:35 +0000)
git-svn-id: file:///raid/svn-personal/tewi/trunk@161 8739d7e6-ffea-ec47-b151-bdff447c6205

README
Server/config.c
Server/server.c
Server/tw_version.h
Tool/genconf.c

diff --git a/README b/README
index 6ecc3c8ec3300c27e131d1c61c1ef4236edcffab..10ad07ee2b62d3457f3ce05dc752e6346eb081d5 100644 (file)
--- a/README
+++ b/README
@@ -1,5 +1,5 @@
 
-Tewi HTTPd version 1.07A
+Tewi HTTPd version 1.08
 
 Original by Nishi <nishi@nishi.boats>
 
index 5dfdb61dd05b11dd45c520752c2d6b47c8ac6852..a6a4d08f268e8ad19e3622811076b178fbb75119 100644 (file)
@@ -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);
index 361a6e527e35411fba77d67d199515867cc8935a..dea62ec24bb6b28c964698299d2d55793b35eb2f 100644 (file)
@@ -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);
                }
index e491efa90213b82ec32c91b47c800154d280fe87..ed6becdc603c0edd48e43e2a7b8da95cb6b83ea2 100644 (file)
@@ -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);
index f9d14a9167236c77c142fd259ae3e0a9b1e70ab0..450c7e0459e8f33cfbafb4f47771cfbd39f9c538 100644 (file)
@@ -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]);
 }