"Fossies" - the Fresh Open Source Software Archive

Member "mod_chroot-0.5/src/apache13/mod_chroot.c" (12 Jun 2005, 3974 Bytes) of package /linux/www/apache_httpd_modules/old/mod_chroot-0.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <stdlib.h>
    2 #include "httpd.h"
    3 #include "http_config.h"
    4 #include "http_core.h"
    5 #include "http_main.h"
    6 #include "http_log.h"
    7 
    8 #ifdef EAPI
    9 #include "ap_ctx.h"
   10 #include "http_conf_globals.h"
   11 #endif
   12 
   13 #define MODULE_SIGNATURE "mod_chroot/0.4"
   14 module MODULE_VAR_EXPORT chroot_module;
   15 
   16 typedef struct {
   17     char *chroot_dir;
   18 } chroot_srv_config;
   19 
   20 #ifdef EAPI
   21 typedef struct {
   22     int initcount;
   23 } chroot_ctx_rec;
   24 #endif
   25 
   26 int chroot_init_now() {
   27     
   28 #ifndef EAPI
   29     if(getenv("APACHE_MOD_CHROOT_INIT")!=NULL) {
   30         unsetenv("APACHE_MOD_CHROOT_INIT");
   31         return 1;
   32     } else {
   33         setenv("APACHE_MOD_CHROOT_INIT", "", 0);
   34         return 0;
   35     }
   36 #else
   37 chroot_ctx_rec *m;
   38 
   39     m=(chroot_ctx_rec *)ap_ctx_get(ap_global_ctx, "chroot_module");
   40     if(m->initcount==0) {
   41         m->initcount=1;
   42         return 0;
   43     } else return 1;
   44 #endif
   45 }
   46 
   47 void chroot_init_ctx() {
   48 #ifdef EAPI
   49 chroot_ctx_rec *m;
   50 pool *p;
   51 
   52     m=ap_ctx_get(ap_global_ctx, "chroot_module");
   53     if(m==NULL) {
   54         p=ap_make_sub_pool(NULL);
   55         m=ap_palloc(p, sizeof(chroot_ctx_rec));
   56         m->initcount=0;
   57         ap_ctx_set(ap_global_ctx, "chroot_module", m);
   58     }
   59     return;
   60 #else
   61     unsetenv("__APACHE_MOD_CHROOT_INIT");
   62 #endif
   63 }
   64     
   65 
   66 void chroot_init(server_rec *s, pool *p) {
   67 chroot_srv_config *cfg = (chroot_srv_config *)ap_get_module_config(s->module_config, &chroot_module);
   68 
   69     ap_add_version_component(MODULE_SIGNATURE);
   70     if(cfg->chroot_dir==NULL) return;
   71     chroot_init_ctx();
   72     if(chroot_init_now()) {
   73         if(chroot(cfg->chroot_dir)<0) {
   74          ap_log_error(APLOG_MARK, APLOG_ERR, s, "chroot to %s failed.",
   75             cfg->chroot_dir);
   76          return;
   77         }
   78         if(chdir("/")<0) {
   79          ap_log_error(APLOG_MARK, APLOG_ERR, s, "chdir to / failed.");
   80          return;
   81         }
   82 #ifndef EAPI
   83         ap_log_error(APLOG_MARK, APLOG_NOTICE | APLOG_NOERRNO, s,
   84         "mod_chroot: changed root to %s (environment magic).",
   85         cfg->chroot_dir);
   86 #else
   87         ap_log_error(APLOG_MARK, APLOG_NOTICE | APLOG_NOERRNO, s,
   88         "mod_chroot: changed root to %s (EAPI mode).",
   89         cfg->chroot_dir);
   90 #endif
   91     }
   92 }
   93 
   94 static void *chroot_create_srv_config(pool *p, server_rec *s) {
   95 chroot_srv_config *cfg = (chroot_srv_config *)ap_pcalloc(p, sizeof(chroot_srv_config));
   96 if(cfg==NULL) return NULL;
   97     chroot_init_ctx();
   98     cfg->chroot_dir=NULL;
   99     return cfg;
  100 }
  101 
  102 static const char *cmd_chroot_dir(cmd_parms *cmd, void *dummy, char *p1) {
  103         chroot_srv_config *cfg = (chroot_srv_config *)ap_get_module_config(cmd->server->module_config, &chroot_module);
  104             if(!ap_is_directory(p1))
  105             return "Chroot to something that is not a directory";
  106             cfg->chroot_dir = p1;
  107         return NULL;
  108 }
  109 
  110 
  111 static const command_rec chroot_cmds[] = {
  112     {
  113      "ChrootDir",
  114      cmd_chroot_dir,
  115      NULL,
  116      RSRC_CONF,
  117      TAKE1,
  118      "path to a new root directory"
  119     },
  120     { NULL }
  121 };
  122 
  123     
  124 module MODULE_VAR_EXPORT chroot_module = {
  125     STANDARD_MODULE_STUFF,
  126     chroot_init,
  127     NULL,           /* create per-dir    config structures */
  128     NULL,               /* merge  per-dir    config structures */
  129     chroot_create_srv_config,   /* create per-server config structures */
  130     NULL,                       /* merge  per-server config structures */
  131     chroot_cmds,                   /* table of config file commands       */
  132     NULL,                       /* [#8] MIME-typed-dispatched handlers */
  133     NULL,                       /* [#1] URI to filename translation    */
  134     NULL,                       /* [#4] validate user id from request  */
  135     NULL,                       /* [#5] check if the user is ok _here_ */
  136     NULL,                       /* [#3] check access by host address   */
  137     NULL,                       /* [#6] determine MIME type            */
  138     NULL,                   /* [#7] pre-run fixups                 */
  139     NULL,                       /* [#9] log a transaction              */
  140     NULL,                       /* [#2] header parser                  */
  141     NULL,                       /* child_init                          */
  142     NULL,                       /* child_exit                          */
  143     NULL                        /* [#0] post read-request              */
  144 };
  145