diff -r 5eb4d7541107 src/core/nginx.c
|
a
|
b
|
|
| 49 | 49 | offsetof(ngx_core_conf_t, master), |
| 50 | 50 | NULL }, |
| 51 | 51 | |
| | 52 | { ngx_string("chroot"), |
| | 53 | NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG, |
| | 54 | ngx_conf_set_flag_slot, |
| | 55 | 0, |
| | 56 | offsetof(ngx_core_conf_t, chroot), |
| | 57 | NULL }, |
| | 58 | |
| 52 | 59 | { ngx_string("timer_resolution"), |
| 53 | 60 | NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1, |
| 54 | 61 | ngx_conf_set_msec_slot, |
| … |
… |
|
| 909 | 916 | #endif |
| 910 | 917 | } |
| 911 | 918 | |
| | 919 | cycle->chroot.len = cycle->prefix.len; |
| | 920 | cycle->chroot.data = ngx_pstrdup(cycle->pool, &cycle->prefix); |
| | 921 | |
| 912 | 922 | if (ngx_conf_file) { |
| 913 | 923 | cycle->conf_file.len = ngx_strlen(ngx_conf_file); |
| 914 | 924 | cycle->conf_file.data = ngx_conf_file; |
| … |
… |
|
| 967 | 977 | |
| 968 | 978 | ccf->daemon = NGX_CONF_UNSET; |
| 969 | 979 | ccf->master = NGX_CONF_UNSET; |
| | 980 | ccf->chroot = NGX_CONF_UNSET; |
| 970 | 981 | ccf->timer_resolution = NGX_CONF_UNSET_MSEC; |
| 971 | 982 | |
| 972 | 983 | ccf->worker_processes = NGX_CONF_UNSET; |
| … |
… |
|
| 995 | 1006 | |
| 996 | 1007 | ngx_conf_init_value(ccf->daemon, 1); |
| 997 | 1008 | ngx_conf_init_value(ccf->master, 1); |
| | 1009 | ngx_conf_init_value(ccf->chroot, 0); |
| 998 | 1010 | ngx_conf_init_msec_value(ccf->timer_resolution, 0); |
| 999 | 1011 | |
| 1000 | 1012 | ngx_conf_init_value(ccf->worker_processes, 1); |
diff -r 5eb4d7541107 src/core/ngx_cycle.c
|
a
|
b
|
|
| 94 | 94 | return NULL; |
| 95 | 95 | } |
| 96 | 96 | |
| | 97 | if (old_cycle->chroot.data != NULL) { |
| | 98 | cycle->chroot.len = old_cycle->chroot.len; |
| | 99 | cycle->chroot.data = ngx_pstrdup(pool, &old_cycle->chroot); |
| | 100 | } |
| | 101 | |
| 97 | 102 | cycle->conf_file.len = old_cycle->conf_file.len; |
| 98 | 103 | cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1); |
| 99 | 104 | if (cycle->conf_file.data == NULL) { |
diff -r 5eb4d7541107 src/core/ngx_cycle.h
|
a
|
b
|
|
| 68 | 68 | ngx_str_t conf_file; |
| 69 | 69 | ngx_str_t conf_param; |
| 70 | 70 | ngx_str_t conf_prefix; |
| | 71 | ngx_str_t chroot; |
| 71 | 72 | ngx_str_t prefix; |
| 72 | 73 | ngx_str_t lock_file; |
| 73 | 74 | ngx_str_t hostname; |
| … |
… |
|
| 77 | 78 | typedef struct { |
| 78 | 79 | ngx_flag_t daemon; |
| 79 | 80 | ngx_flag_t master; |
| | 81 | ngx_flag_t chroot; |
| 80 | 82 | |
| 81 | 83 | ngx_msec_t timer_resolution; |
| 82 | 84 | |
diff -r 5eb4d7541107 src/http/ngx_http_core_module.c
|
a
|
b
|
|
| 4429 | 4429 | static char * |
| 4430 | 4430 | ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
| 4431 | 4431 | { |
| | 4432 | ngx_core_conf_t *ccf; |
| 4432 | 4433 | ngx_http_core_loc_conf_t *clcf = conf; |
| 4433 | 4434 | |
| 4434 | 4435 | ngx_str_t *value; |
| … |
… |
|
| 4437 | 4438 | ngx_http_script_compile_t sc; |
| 4438 | 4439 | |
| 4439 | 4440 | alias = (cmd->name.len == sizeof("alias") - 1) ? 1 : 0; |
| | 4441 | ccf = (ngx_core_conf_t *)ngx_get_conf(cf->cycle->conf_ctx, ngx_core_module); |
| 4440 | 4442 | |
| 4441 | 4443 | if (clcf->root.data) { |
| 4442 | 4444 | |
| … |
… |
|
| 4499 | 4501 | } |
| 4500 | 4502 | } |
| 4501 | 4503 | |
| | 4504 | if (ccf->chroot && clcf->root.len >= cf->cycle->chroot.len) { |
| | 4505 | u_char *chrootdir = cf->cycle->chroot.data; |
| | 4506 | chrootdir[cf->cycle->chroot.len] = '\0'; |
| | 4507 | |
| | 4508 | if (ngx_strnstr(clcf->root.data, (char *)chrootdir, |
| | 4509 | cf->cycle->chroot.len)) { |
| | 4510 | clcf->root.data += cf->cycle->chroot.len; |
| | 4511 | clcf->root.len -= cf->cycle->chroot.len; |
| | 4512 | } |
| | 4513 | } |
| | 4514 | |
| 4502 | 4515 | n = ngx_http_script_variables_count(&clcf->root); |
| 4503 | 4516 | |
| 4504 | 4517 | ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); |
diff -r 5eb4d7541107 src/os/unix/ngx_process_cycle.c
|
a
|
b
|
|
| 828 | 828 | } |
| 829 | 829 | |
| 830 | 830 | if (geteuid() == 0) { |
| | 831 | if (ccf->chroot) { |
| | 832 | u_char *chrootdir = cycle->chroot.data; |
| | 833 | chrootdir[cycle->chroot.len] = '\0'; |
| | 834 | |
| | 835 | if (chroot((const char *)chrootdir) == -1) { |
| | 836 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| | 837 | "chroot(%s) failed", chroot); |
| | 838 | exit(2); |
| | 839 | } |
| | 840 | |
| | 841 | if (chdir("/") == -1) { |
| | 842 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| | 843 | "chdir failed"); |
| | 844 | exit(2); |
| | 845 | } |
| | 846 | |
| | 847 | cycle->prefix.data[0] = '\0'; |
| | 848 | cycle->prefix.len = 0; |
| | 849 | |
| | 850 | ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "chrooted on %s", chrootdir); |
| | 851 | } |
| | 852 | |
| 831 | 853 | if (setgid(ccf->group) == -1) { |
| 832 | 854 | ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, |
| 833 | 855 | "setgid(%d) failed", ccf->group); |