config.c revision 9d0665da83d1e22c0ea0e5f6f940f70f75bf5237
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
*
* Portions of this software are based upon public domain software
* originally written at the National Center for Supercomputing Applications,
* University of Illinois, Urbana-Champaign.
*/
/*
* http_config.c: once was auxillary functions for reading httpd's config
* file and converting filenames into a namespace
*
* Rob McCool
*
* Wall-to-wall rewrite for Apache... commands which are part of the
* server core can now be found next door in "http_core.c". Now contains
* general command loop, and functions which do bookkeeping for the new
* Apache config stuff (modules and configuration vectors).
*
* rst
*
*/
#include "apr.h"
#include "apr_strings.h"
#include "apr_portable.h"
#include "apr_file_io.h"
#define APR_WANT_STDIO
#define APR_WANT_STRFUNC
#include "apr_want.h"
#define CORE_PRIVATE
#include "ap_config.h"
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "http_core.h"
#include "http_log.h" /* for errors in parse_htaccess */
#include "http_request.h" /* for default_handler (see invoke_handler) */
#include "http_main.h"
#include "http_vhost.h"
#include "util_cfgtree.h"
#include "mpm.h"
AP_DECLARE_DATA const char *ap_server_argv0;
AP_DECLARE_DATA const char *ap_server_root;
)
/* During the course of debugging I expanded this macro out, so
* rather than remove all the useful information there is in the
* following lines, I'm going to leave it here in case anyone
* else finds it useful.
*
* Ben has looked at it and thinks it correct :)
*
AP_DECLARE(int) ap_hook_post_config(ap_HOOK_post_config_t *pf,
const char * const *aszPre,
const char * const *aszSucc,
int nOrder)
{
ap_LINK_post_config_t *pHook;
if(!_hooks.link_post_config) {
_hooks.link_post_config=apr_array_make(apr_global_hook_pool,1,
sizeof(ap_LINK_post_config_t));
apr_hook_sort_register("post_config",&_hooks.link_post_config);
}
pHook=apr_array_push(_hooks.link_post_config);
pHook->pFunc=pf;
pHook->aszPredecessors=aszPre;
pHook->aszSuccessors=aszSucc;
pHook->nOrder=nOrder;
pHook->szName=apr_current_hooking_module;
if(apr_debug_module_hooks)
apr_show_hook("post_config",aszPre,aszSucc);
}
AP_DECLARE(apr_array_header_t *) ap_hook_get_post_config(void) {
return _hooks.link_post_config;
}
AP_DECLARE(int) ap_run_post_config (apr_pool_t *pconf,
apr_pool_t *plog,
apr_pool_t *ptemp,
server_rec *s)
{
ap_LINK_post_config_t *pHook;
int n;
if(!_hooks.link_post_config)
return;
pHook=(ap_LINK_post_config_t *)_hooks.link_post_config->elts;
for(n=0 ; n < _hooks.link_post_config->nelts ; ++n)
pHook[n].pFunc (pconf, plog, ptemp, s);
}
*/
(r),DECLINED)
(r),DECLINED)
AP_IMPLEMENT_HOOK_VOID(optional_fn_retrieve,(void),())
/****************************************************************
*
* We begin with the functions which deal with the linked list
* of modules which control just about all of the server operation.
*/
/* total_modules is the number of modules that have been linked
* into the server.
*/
static int total_modules = 0;
/* dynamic_modules is the number of modules that have been added
* after the pre-loaded ones have been set up. It shouldn't be larger
* than DYNAMIC_MODULE_LIMIT.
*/
static int dynamic_modules = 0;
typedef int (*handler_func) (request_rec *);
typedef void *(*dir_maker_func) (apr_pool_t *, char *);
typedef void *(*merger_func) (apr_pool_t *, void *, void *);
/* Dealing with config vectors. These are associated with per-directory,
* per-server, and per-request configuration, and have a void* pointer for
* each modules. The nature of the structure pointed to is private to the
* module in question... the core doesn't (and can't) know. However, there
* are defined interfaces which allow it to create instances of its private
* per-directory and per-server structures, and to merge the per-directory
* structures of a directory and its subdirectory (producing a new one in
* which the defaults applying to the base directory have been properly
* overridden).
*/
{
void *conf_vector = apr_pcalloc(p, sizeof(void *) *
return conf_vector;
}
{
void **conf_vector = apr_pcalloc(p, sizeof(void *) *
if (df)
}
return (ap_conf_vector_t *) conf_vector;
}
{
void **base_vector = (void **) base;
void **new_vector = (void **) new_conf;
int i = modp->module_index;
if (!new_vector[i]) {
conf_vector[i] = base_vector[i];
}
else {
if (df && base_vector[i]) {
}
else
conf_vector[i] = new_vector[i];
}
}
return (ap_conf_vector_t *) conf_vector;
}
{
void **conf_vector = apr_pcalloc(p, sizeof(void *) *
if (modp->create_server_config)
}
return (ap_conf_vector_t *) conf_vector;
}
{
/* Can reuse the 'virt' vector for the spine of it, since we don't
* have to deal with the moral equivalent of .htaccess files here...
*/
void **base_vector = (void **) base;
void **virt_vector = (void **) virt;
int i = modp->module_index;
if (!virt_vector[i])
virt_vector[i] = base_vector[i];
else if (df)
}
}
{
return create_empty_config(p);
}
{
return create_empty_config(p);
}
{
return create_empty_config(p);
}
{
const char *handler;
const char *p;
char *p2;
int result;
char hbuf[MAX_STRING_LEN];
const char *old_handler = r->handler;
if (!r->handler) {
/* MIME type arguments */
--p2; /* strip trailing spaces */
*p2='\0';
}
}
result = ap_run_handler(r);
r->handler = old_handler;
}
}
int methnum;
/*
* A method number either hardcoded into apache or
* added by a module and registered.
*/
}
return 0; /* not found */
}
{
if(m->register_hooks)
{
if(getenv("SHOW_HOOKS"))
{
}
m->register_hooks(p);
}
}
/* One-time setup for precompiled modules --- NOT to be done on restart */
{
/* This could be called from an AddModule httpd.conf command,
* after the file has been linked and the module structure within it
* teased out...
*/
if (m->version != MODULE_MAGIC_NUMBER_MAJOR) {
"%s: module \"%s\" is not compatible with this "
ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Please contact the vendor for the correct version.");
exit(1);
}
m->next = ap_top_module;
ap_top_module = m;
}
if (m->module_index == -1) {
m->module_index = total_modules++;
if (dynamic_modules > DYNAMIC_MODULE_LIMIT) {
"%s: module \"%s\" could not be loaded, because"
"module limit was reached. Please increase "
"DYNAMIC_MODULE_LIMIT and recompile.");
exit(1);
}
}
/* Some C compilers put a complete path into __FILE__, but we want
* only the filename (e.g. mod_includes.c). So check for path
* components (Unix and DOS), and remove them.
*/
#ifdef _OSD_POSIX /* __FILE__="*POSIX(/home/martin/apache/src/modules/standard/mod_info.c)" */
/* We cannot fix the string in-place, because it's const */
}
#endif /*_OSD_POSIX*/
/* FIXME: is this the right place to call this?
* It doesn't appear to be
*/
ap_register_hooks(m, p);
}
/*
* remove_module undoes what add_module did. There are some caveats:
* when the module is removed, its slot is lost so all the current
* per-dir and per-server configurations are invalid. So we should
* only ever call this function when you are invalidating almost
* all our current data. I.e. when doing a restart.
*/
{
if (modp == m) {
/* We are the top module, special case */
}
else {
/* Not the top module, find use. When found modp will
* point to the module _before_ us in the list
*/
}
if (!modp) {
/* Uh-oh, this module doesn't exist */
"Cannot remove module %s: not found in module list",
m->name);
return;
}
/* Eliminate us from the module list */
}
* be unnecessary */
}
{
module **m;
/*
* Add module pointer to top of chained module list
*/
ap_add_module(mod, p);
/*
* And module pointer to list of loaded modules
*
* Notes: 1. ap_add_module() would already complain if no more space
* exists for adding a dynamically loaded module
* 2. ap_add_module() accepts double inclusion, so we have
* to accept this, too.
*/
for (m = ap_loaded_modules; *m != NULL; m++)
;
*m++ = mod;
*m = NULL;
}
{
module **m;
int done;
/*
* Remove module pointer from chained module list
*/
/*
* Remove module pointer from list of loaded modules
*
* Note: 1. We cannot determine if the module was successfully
* removed by ap_remove_module().
* 2. We have not to complain explicity when the module
* is not found because ap_remove_module() did it
* for us already.
*/
done = 1;
else
*m++ = *m2;
}
*m = NULL;
}
{
module **m;
/*
* Initialise total_modules variable and module indices
*/
total_modules = 0;
for (m = ap_preloaded_modules; *m != NULL; m++)
(*m)->module_index = total_modules++;
/*
* Initialise list of loaded modules
*/
if (ap_loaded_modules == NULL) {
"Ouch! Out of memory in ap_setup_prelinked_modules()!");
}
*m2++ = *m++;
/*
* Initialize chain of linked (=activate) modules
*/
for (m = ap_prelinked_modules; *m != NULL; m++)
}
{
return m->name;
}
{
return modp;
}
return NULL;
}
/* Add a named module. Returns 1 if module found, 0 otherwise. */
{
int i = 0;
/* Only add modules that are not already enabled. */
ap_add_module(modp, p);
}
return 1;
}
}
return 0;
}
/*****************************************************************
*
* Resource, access, and .htaccess config files now parsed by a common
* command loop.
*
* Let's begin with the basics; parsing the line and
* invoking the function...
*/
{
const char *errmsg;
case RAW_ARGS:
#ifdef RESOLVE_ENV_PER_TOKEN
#endif
case NO_ARGS:
if (*args != 0)
NULL);
case TAKE1:
if (*w == '\0' || *args != 0)
case TAKE2:
case TAKE12:
if (*w == '\0' || *args != 0)
case TAKE3:
case TAKE23:
" takes two or three arguments",
case TAKE123:
if (*w == '\0' || *args != 0)
" takes one, two or three arguments",
case TAKE13:
" takes one or three arguments",
case ITERATE:
{
return errmsg;
}
return NULL;
case ITERATE2:
if (*w == '\0' || *args == 0)
" requires at least two arguments",
{
return errmsg;
}
return NULL;
case FLAG:
NULL);
default:
" is improperly configured internally (server bug)",
NULL);
}
}
{
return cmds;
else
++cmds;
return NULL;
}
{
const command_rec *cmdp;
return cmdp;
}
return NULL;
}
const char *section,
{
/* ### need to fix the create_dir_config functions' prototype... */
}
}
return section_config;
}
{
const char *args;
char *cmd_name;
const command_rec *cmd;
if (*l == '#' || *l == '\0')
return NULL;
args = l;
#else
#endif
if (*cmd_name == '\0') {
/* Note: this branch should not occur. An empty line should have
* triggered the exit further above.
*/
return NULL;
}
if (*lastc == '>') {
*lastc = '\0' ;
}
}
const char *retval;
&sub_tree, *curr_parent);
if (*current) {
}
else {
if (*curr_parent) {
}
if (*current) {
}
}
if (*current) {
if (!*conftree) {
/* Before walking *current to the end of the list,
* set the head to *current.
*/
}
}
}
return retval;
}
}
if (cmd_name[0] == '<') {
}
else if (*curr_parent == NULL) {
return apr_pstrcat(p, cmd_name,
" section", NULL);
}
else {
if (*bracket != '>') {
return apr_pstrcat(p, cmd_name,
"> directive missing closing '>'", NULL);
}
*bracket = '\0';
return apr_pstrcat(p, "Expected </",
}
*bracket = '>';
/* done with this section; move up a level */
*current = *curr_parent;
}
}
else {
}
return NULL;
}
char *orig_directive)
{
char l[MAX_STRING_LEN];
char *bracket;
const char *retval;
(*curr_parent == NULL)) {
break;
}
curr_parent, &sub_tree);
return retval;
sub_tree = *curr_parent;
}
}
}
return NULL;
}
{
while (1) {
const command_rec *cmd;
"', perhaps mis-spelled or defined by a module "
"not included in the server configuration",
NULL);
}
else {
mod,
const char *retval;
return NULL;
}
/* If the directive in error has already been set, don't
* replace it. Otherwise, an error inside a container
* will be reported as occuring on the first line of the
* container.
*/
if (!parms->err_directive) {
}
return retval;
}
}
}
/* NOTREACHED */
}
{
/* scan through all directives, executing each one */
const char *errmsg;
/* actually parse the command and execute the correct function */
/* restore the context (just in case) */
return errmsg;
}
}
return NULL;
}
{
char l[MAX_STRING_LEN];
const char *errmsg;
}
}
return errmsg;
*conftree = curr_parent;
}
}
}
if (curr_parent != NULL) {
errmsg = "";
while (curr_parent != NULL) {
}
return errmsg;
}
return NULL;
}
/*
* Generic command functions...
*/
void *struct_ptr,
const char *arg)
{
/* This one's pretty generic... */
return NULL;
}
void *struct_ptr,
const char *arg)
{
char *endptr;
"Invalid value for directive %s, expected integer",
}
return error_str;
}
void *struct_ptr,
const char *arg_)
{
/* This one's pretty generic... */
return NULL;
}
void *struct_ptr_v, int arg)
{
/* This one's pretty generic too... */
char *struct_ptr = (char *)struct_ptr_v;
return NULL;
}
const char *arg)
{
/* Prepend server_root to relative arg.
This allows most args to be independent of server_root,
so the server can be moved or mirrored with less pain. */
const char *p;
*(const char **) ((char*)struct_ptr + offset) = p;
return NULL;
}
void *struct_ptr,
const char *arg)
{
/* This one's really generic... */
}
/*****************************************************************
*
* Reading whole config files...
*/
static cmd_parms default_parms =
{
char *newpath;
APR_FILEPATH_TRUENAME, p) == APR_SUCCESS)
return newpath;
else
return NULL;
}
{
char l[MAX_STRING_LEN];
const char *args;
char *cmd_name;
args = l;
#else
#endif
if (cmd_name[0] == '<') {
}
return NULL; /* found end of container */
}
else {
const char *msg;
return msg;
}
}
}
}
NULL);
}
{
const command_rec *cmd;
"', perhaps mis-spelled or defined by a module "
"not included in the server configuration",
NULL);
}
else {
}
}
/* This structure and the following functions are needed for the
* table-based config file reading. They are passed to the
* cfg_open_custom() routine.
*/
/* Structure to be passed to cfg_open_custom(): it contains an
* index which is incremented from 0 to nelts on each call to
* cfg_getline() (which in turn calls arr_elts_getstr())
* and an apr_array_header_t pointer for the string array.
*/
typedef struct {
int curr_idx;
/* arr_elts_getstr() returns the next line from the string array. */
{
/* End of array reached? */
return NULL;
/* return the line */
return buf;
}
/* arr_elts_close(): dummy close routine (makes sure no more lines can be read) */
static int arr_elts_close(void *param)
{
return 0;
}
{
const char *errmsg;
if (errmsg) {
errmsg);
exit(1);
}
}
typedef struct {
char *fname;
} fnames;
{
}
{
const char *errmsg;
/* don't require conf/httpd.conf if we have a -C or -c switch */
return;
}
/*
* here we want to check if the candidate file is really a
* directory, and most definitely NOT a symlink (to prevent
* horrible loops). If so, let's recurse and toss it back
* into the function.
*/
int current;
char errmsg[120];
/*
* first course of business is to grok all the directory
* entries here and store 'em away. Recall we need full pathnames
* for this.
*/
if (rv != APR_SUCCESS) {
exit(1);
}
/* strip out '.' and '..' */
}
}
if (candidates->nelts != 0) {
sizeof(fnames), fname_alphasort);
/*
* Now recurse these... we handle errors and subdirectories
* via the recursion, which is nice
*/
}
}
return;
}
/* GCC's initialization extensions are soooo nice here... */
"%s: could not open document config file %s",
exit(1);
}
"Syntax error on line %d of %s:",
"%s", errmsg);
exit(1);
}
}
{
const char *errmsg;
if (errmsg) {
"Syntax error on line %d of %s:",
"%s", errmsg);
exit(1);
}
}
request_rec *r, int override,
const char *d, const char *access_name)
{
ap_configfile_t *f = NULL;
const struct htaccess_result *cache;
struct htaccess_result *new;
/* firstly, search cache */
return OK;
}
/* loop through the access names and find the first one */
while (access_name[0]) {
/* AFAICT; there is no use of the actual 'filename' against
* any canonicalization, so we will simply take the given
* name, ignoring case sensitivity and aliases
*/
if (status == APR_SUCCESS) {
const char *errmsg;
parms.config_file = f;
ap_cfg_closefile(f);
if (errmsg) {
return HTTP_INTERNAL_SERVER_ERROR;
}
break;
} else {
if (!APR_STATUS_IS_ENOENT(status)
&& !APR_STATUS_IS_ENOTDIR(status)) {
"%s pcfg_openfile: unable to check htaccess file, "
"ensure it is readable",
filename);
"Server unable to read htaccess file, denying "
"access to be safe");
return HTTP_FORBIDDEN;
}
}
}
/* cache it */
/* add to head of list */
return OK;
}
const char *hostname,
server_rec **ps)
{
/* TODO: this crap belongs in http_core */
s->server_admin = NULL;
s->server_hostname = NULL;
s->error_fname = NULL;
s->timeout = 0;
s->keep_alive_timeout = 0;
s->keep_alive = -1;
s->keep_alive_max = -1;
/* useful default, otherwise we get a port of 0 on redirects */
s->is_virtual = 1;
s->module_config = create_empty_config(p);
s->lookup_defaults = ap_create_per_dir_config(p);
*ps = s;
return ap_parse_vhost_addrs(p, hostname, s);
}
{
if (virt->keep_alive_timeout == 0)
/* XXX: this is really something that should be dealt with by a
* post-config api phase */
}
}
/*****************************************************************
*
* Getting *everything* configured...
*/
static void init_config_globals(apr_pool_t *p)
{
/* Global virtual host hash bucket pointers. Init to null. */
}
{
apr_file_open_stderr(&s->error_log, p);
s->port = 0;
s->server_admin = DEFAULT_ADMIN;
s->server_hostname = NULL;
s->error_fname = DEFAULT_ERRORLOG;
s->loglevel = DEFAULT_LOGLEVEL;
s->timeout = DEFAULT_TIMEOUT;
s->keep_alive = 1;
/* NOT virtual host; don't match any real network interface */
s->module_config = create_server_config(p, s);
return s;
}
const char *filename,
{
const char *confname;
/* All server-wide config files now have the SAME syntax... */
p, ptemp);
/* process_command_config may change the ServerRoot so
* compute this config file name afterwards.
*/
p, ptemp);
return s;
}
module *m)
{
if (m->create_server_config)
(*m->create_server_config)(p, s));
if (m->create_dir_config)
(*m->create_dir_config)(p, NULL));
}
{
module *m;
for (m = ap_top_module; m; m = m->next)
if (m->rewrite_args)
(*m->rewrite_args) (process);
}
/********************************************************************
* Configuration directives are restricted in terms of where they may
* to the bitmask req_override in the command_rec structure.
* If any of the overrides set in req_override are also allowed in the
* context in which the command is read, then the command is allowed.
* The context is determined as follows:
*
* inside *.conf --> override = (RSRC_CONF|OR_ALL)&~(OR_AUTHCFG|OR_LIMIT);
* within <Directory> or <Location> --> override = OR_ALL|ACCESS_CONF;
* within .htaccess --> override = AllowOverride for current directory;
*
* the result is, well, a rather confusing set of possibilities for when
* a particular directive is allowed to be used. This procedure prints
* in English where the given (pc) directive can be used.
*/
{
int n = 0;
printf("\tAllowed in *.conf ");
printf("anywhere");
printf("only outside <Directory>, <Files> or <Location>");
else
printf("only inside <Directory>, <Files> or <Location>");
/* Warn if the directive is allowed inside <Directory> or .htaccess
* but module doesn't support per-dir configuration */
printf(" [no per-dir config]");
printf(" and in .htaccess\n\twhen AllowOverride");
printf(" isn't None");
else {
printf(" includes ");
if (n++)
printf(" or ");
printf("AuthConfig");
}
if (n++)
printf(" or ");
printf("Limit");
}
if (n++)
printf(" or ");
printf("Options");
}
if (n++)
printf(" or ");
printf("FileInfo");
}
if (n++)
printf(" or ");
printf("Indexes");
}
}
}
printf("\n");
}
/* Show the preloaded configuration directives, the help string explaining
* the directive arguments, in what module they are handled, and in
* what parts of the configuration they are allowed. Used for httpd -L.
*/
AP_DECLARE(void) ap_show_directives(void)
{
const command_rec *pc;
int n;
for (n = 0; ap_loaded_modules[n]; ++n)
}
}
/* Show the preloaded module names. Used for httpd -l. */
AP_DECLARE(void) ap_show_modules(void)
{
int n;
printf("Compiled in modules:\n");
for (n = 0; ap_loaded_modules[n]; ++n)
}
AP_DECLARE(const char *) ap_show_mpm(void)
{
return MPM_NAME;
}