Ticket #805: nginx-chroot.diff

File nginx-chroot.diff, 4.8 KB (added by David CARLIER, 11 years ago)

new chroot mode

  • src/core/nginx.c

    diff -r 5eb4d7541107 src/core/nginx.c
    a b  
    4949      offsetof(ngx_core_conf_t, master),
    5050      NULL },
    5151
     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
    5259    { ngx_string("timer_resolution"),
    5360      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
    5461      ngx_conf_set_msec_slot,
     
    909916#endif
    910917    }
    911918
     919    cycle->chroot.len = cycle->prefix.len;
     920    cycle->chroot.data = ngx_pstrdup(cycle->pool, &cycle->prefix);
     921
    912922    if (ngx_conf_file) {
    913923        cycle->conf_file.len = ngx_strlen(ngx_conf_file);
    914924        cycle->conf_file.data = ngx_conf_file;
     
    967977
    968978    ccf->daemon = NGX_CONF_UNSET;
    969979    ccf->master = NGX_CONF_UNSET;
     980    ccf->chroot = NGX_CONF_UNSET;
    970981    ccf->timer_resolution = NGX_CONF_UNSET_MSEC;
    971982
    972983    ccf->worker_processes = NGX_CONF_UNSET;
     
    9951006
    9961007    ngx_conf_init_value(ccf->daemon, 1);
    9971008    ngx_conf_init_value(ccf->master, 1);
     1009    ngx_conf_init_value(ccf->chroot, 0);
    9981010    ngx_conf_init_msec_value(ccf->timer_resolution, 0);
    9991011
    10001012    ngx_conf_init_value(ccf->worker_processes, 1);
  • src/core/ngx_cycle.c

    diff -r 5eb4d7541107 src/core/ngx_cycle.c
    a b  
    9494        return NULL;
    9595    }
    9696
     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
    97102    cycle->conf_file.len = old_cycle->conf_file.len;
    98103    cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);
    99104    if (cycle->conf_file.data == NULL) {
  • src/core/ngx_cycle.h

    diff -r 5eb4d7541107 src/core/ngx_cycle.h
    a b  
    6868    ngx_str_t                 conf_file;
    6969    ngx_str_t                 conf_param;
    7070    ngx_str_t                 conf_prefix;
     71    ngx_str_t                 chroot;
    7172    ngx_str_t                 prefix;
    7273    ngx_str_t                 lock_file;
    7374    ngx_str_t                 hostname;
     
    7778typedef struct {
    7879     ngx_flag_t               daemon;
    7980     ngx_flag_t               master;
     81     ngx_flag_t               chroot;
    8082
    8183     ngx_msec_t               timer_resolution;
    8284
  • src/http/ngx_http_core_module.c

    diff -r 5eb4d7541107 src/http/ngx_http_core_module.c
    a b  
    44294429static char *
    44304430ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
    44314431{
     4432    ngx_core_conf_t          *ccf;
    44324433    ngx_http_core_loc_conf_t *clcf = conf;
    44334434
    44344435    ngx_str_t                  *value;
     
    44374438    ngx_http_script_compile_t   sc;
    44384439
    44394440    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);
    44404442
    44414443    if (clcf->root.data) {
    44424444
     
    44994501        }
    45004502    }
    45014503
     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
    45024515    n = ngx_http_script_variables_count(&clcf->root);
    45034516
    45044517    ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
  • src/os/unix/ngx_process_cycle.c

    diff -r 5eb4d7541107 src/os/unix/ngx_process_cycle.c
    a b  
    828828    }
    829829
    830830    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
    831853        if (setgid(ccf->group) == -1) {
    832854            ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
    833855                          "setgid(%d) failed", ccf->group);