From a4cfffda63bee266a0f6ef2b2407b16a33896ff8 Mon Sep 17 00:00:00 2001 From: chris Date: Sat, 10 May 2014 17:48:09 +0000 Subject: [PATCH] Allow nginx to chroot to a directory other than /var/www ok henning@ --- usr.sbin/nginx/src/core/nginx.c | 2 +- usr.sbin/nginx/src/core/ngx_file.c | 9 +++++++-- usr.sbin/nginx/src/core/ngx_string.c | 18 +++++++++++++++--- usr.sbin/nginx/src/os/unix/ngx_process.h | 1 + 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/usr.sbin/nginx/src/core/nginx.c b/usr.sbin/nginx/src/core/nginx.c index 875fe07f06e..7f0011d879f 100644 --- a/usr.sbin/nginx/src/core/nginx.c +++ b/usr.sbin/nginx/src/core/nginx.c @@ -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; diff --git a/usr.sbin/nginx/src/core/ngx_file.c b/usr.sbin/nginx/src/core/ngx_file.c index 69d65d0fd2b..ce09bb6021f 100644 --- a/usr.sbin/nginx/src/core/ngx_file.c +++ b/usr.sbin/nginx/src/core/ngx_file.c @@ -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); diff --git a/usr.sbin/nginx/src/core/ngx_string.c b/usr.sbin/nginx/src/core/ngx_string.c index a42ee861f03..1d1989da24d 100644 --- a/usr.sbin/nginx/src/core/ngx_string.c +++ b/usr.sbin/nginx/src/core/ngx_string.c @@ -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; } diff --git a/usr.sbin/nginx/src/os/unix/ngx_process.h b/usr.sbin/nginx/src/os/unix/ngx_process.h index 7b5e8c0c25e..14f7b89cd95 100644 --- a/usr.sbin/nginx/src/os/unix/ngx_process.h +++ b/usr.sbin/nginx/src/os/unix/ngx_process.h @@ -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; -- 2.20.1