Allow nginx to chroot to a directory other than /var/www
authorchris <chris@openbsd.org>
Sat, 10 May 2014 17:48:09 +0000 (17:48 +0000)
committerchris <chris@openbsd.org>
Sat, 10 May 2014 17:48:09 +0000 (17:48 +0000)
ok henning@

usr.sbin/nginx/src/core/nginx.c
usr.sbin/nginx/src/core/ngx_file.c
usr.sbin/nginx/src/core/ngx_string.c
usr.sbin/nginx/src/os/unix/ngx_process.h

index 875fe07..7f0011d 100644 (file)
@@ -185,11 +185,11 @@ ngx_module_t  ngx_core_module = {
 
 
 ngx_uint_t          ngx_max_module;
+u_char            *ngx_prefix;
 
 static ngx_uint_t   ngx_show_help;
 static ngx_uint_t   ngx_show_version;
 static ngx_uint_t   ngx_show_configure;
-static u_char      *ngx_prefix;
 static u_char      *ngx_conf_file;
 static u_char      *ngx_conf_params;
 static char        *ngx_signal;
index 69d65d0..ce09bb6 100644 (file)
@@ -471,6 +471,7 @@ ngx_add_path(ngx_conf_t *cf, ngx_path_t **slot)
 ngx_int_t
 ngx_create_paths(ngx_cycle_t *cycle, ngx_uid_t user)
 {
+    u_char          *prefix;
     ngx_err_t         err;
     ngx_uint_t        i;
     ngx_path_t      **path;
@@ -479,9 +480,13 @@ ngx_create_paths(ngx_cycle_t *cycle, ngx_uid_t user)
     for (i = 0; i < cycle->paths.nelts; i++) {
 
         if (ngx_chrooted) {
-          if (chdir(NGX_PREFIX) == -1) {
+         if (ngx_prefix)
+           prefix = ngx_prefix;
+         else
+           prefix = NGX_PREFIX;
+          if (chdir(prefix) == -1) {
             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
-                          "chdir(\"%s\") failed", NGX_PREFIX);
+                          "chdir(\"%s\") failed", prefix);
             return NGX_ERROR;
           }
           ngx_strip_chroot(&path[i]->name);
index a42ee86..1d1989d 100644 (file)
@@ -1842,10 +1842,22 @@ ngx_memcpy(void *dst, const void *src, size_t n)
 void
 ngx_strip_chroot(ngx_str_t *path)
 {
-    if (!ngx_strncmp(path->data, NGX_PREFIX, strlen(NGX_PREFIX))) {
+    int plen;
+    u_char *prefix;
+
+    if (ngx_prefix)
+       prefix = ngx_prefix;
+    else
+       prefix = NGX_PREFIX;
+
+    if (prefix[strlen(prefix) - 1] == '/')
+       plen = strlen(prefix) - 1;
+    else
+       plen = strlen(prefix);
+
+    if (!ngx_strncmp(path->data, prefix, strlen(prefix))) {
         char *x, *buf = malloc(path->len);
-       x = ngx_cpystrn(buf, path->data + strlen(NGX_PREFIX) - 1,
-                       path->len);
+       x = ngx_cpystrn(buf, path->data + plen, path->len);
        path->len = (x - buf);
        path->data = buf;
     }
index 7b5e8c0..14f7b89 100644 (file)
@@ -77,6 +77,7 @@ void ngx_debug_point(void);
 extern int            ngx_argc;
 extern char         **ngx_argv;
 extern char         **ngx_os_argv;
+extern u_char       *ngx_prefix;
 
 extern ngx_pid_t      ngx_pid;
 extern ngx_socket_t   ngx_channel;