"Fossies" - the Fresh Open Source Software Archive

Member "mod_chroot-0.5/src/apache20/mod_chroot.c" (12 Jun 2005, 3002 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 <dlfcn.h>
    2 #include "ap_config.h"
    3 #include "httpd.h"
    4 #include "http_config.h"
    5 #include "http_core.h"
    6 #include "http_log.h"
    7 
    8 #include "apr.h"
    9 #include "apr_pools.h"
   10 
   11 #define MODULE_SIGNATURE "mod_chroot/0.4"
   12 module AP_MODULE_DECLARE_DATA chroot_module;
   13 
   14 typedef struct {
   15     char *chroot_dir;
   16 } chroot_srv_config;
   17 
   18 typedef struct {
   19     int initcount;
   20 } chroot_ctx;
   21 
   22 int chroot_init_now(server_rec *s) {
   23     apr_pool_t *pool=s->process->pool;
   24     chroot_ctx *c;
   25 
   26     apr_pool_userdata_get((void **)&c, "chroot_module_ctx", pool);
   27     if(c) {
   28         return (c->initcount++);
   29     }
   30     c = (chroot_ctx *)apr_palloc(pool, sizeof(*c));
   31     c->initcount=1;
   32     apr_pool_userdata_set(c, "chroot_module_ctx", apr_pool_cleanup_null,
   33             pool);
   34     return 0;
   35 }
   36 
   37 static void *chroot_create_srv_config(apr_pool_t *p, server_rec *s) {
   38     chroot_srv_config *cfg = apr_pcalloc(p, sizeof(chroot_srv_config));
   39     
   40     cfg->chroot_dir=NULL;
   41     return cfg;
   42 }
   43 
   44 static const char *cmd_chroot_dir(cmd_parms *cmd, void *dummy, const char *p1) {
   45     chroot_srv_config *cfg = (chroot_srv_config *)ap_get_module_config(cmd->server->module_config, &chroot_module);
   46     const char *err;
   47 
   48     if((err=ap_check_cmd_context(cmd, GLOBAL_ONLY))) return err;
   49     if(!ap_is_directory(cmd->pool, p1)) {
   50         return "Chroot to something that is not a directory";
   51     }
   52     cfg->chroot_dir=(char *)p1;
   53     return NULL;
   54 }
   55 
   56 
   57 static int chroot_init(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) {
   58 chroot_srv_config *cfg = (chroot_srv_config *)ap_get_module_config(s->module_config, &chroot_module);
   59 
   60     ap_add_version_component(p, MODULE_SIGNATURE);
   61     if(cfg->chroot_dir==NULL) return OK;
   62     if(chroot_init_now(s)==1) {
   63         if(chroot(cfg->chroot_dir)<0) {
   64             ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "could not chroot to %s.",cfg->chroot_dir);
   65             return !OK;
   66         }
   67         if(chdir("/")<0) {
   68             ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, "could not chdir to '/'.");
   69             return !OK;
   70         }
   71         ap_log_error(APLOG_MARK, APLOG_NOTICE|APLOG_NOERRNO, 0, s, "mod_chroot: changed root to %s.",cfg->chroot_dir);
   72         return OK;
   73     }
   74     else return OK;
   75 }
   76 
   77 static void register_hooks(apr_pool_t *p) {
   78         /* we want to be loaded last, except for mod_fcgid... and other ? */
   79         static const char * const chrootPost[] = { 
   80         "mod_fcgid.c", 
   81         "mod_cgid.c", 
   82         NULL 
   83     };
   84 
   85         ap_hook_post_config(chroot_init, NULL, chrootPost, APR_HOOK_REALLY_LAST);   
   86 }
   87 
   88 static const command_rec chroot_cmds[] = {
   89 
   90     AP_INIT_TAKE1 (
   91         "ChrootDir",
   92         cmd_chroot_dir,
   93         NULL,
   94         RSRC_CONF,
   95         "path to a new root directory"
   96     ),
   97     { NULL }
   98 };
   99 
  100 module AP_MODULE_DECLARE_DATA chroot_module = {
  101    STANDARD20_MODULE_STUFF,
  102    NULL,             /* create per-dir    config structures */
  103    NULL,                 /* merge  per-dir    config structures */
  104    chroot_create_srv_config,     /* create per-server config structures */
  105    NULL,                         /* merge  per-server config structures */
  106    chroot_cmds,                  /* table of config file commands       */
  107    register_hooks                /* register hooks                      */
  108 };
  109