1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * http_config.c: once was auxillary functions for reading httpd's config
19 * file and converting filenames into a namespace
23 * Wall-to-wall rewrite for Apache... commands which are part of the
24 * server core can now be found next door in "http_core.c". Now contains
25 * general command loop, and functions which do bookkeeping for the new
26 * Apache config stuff (modules and configuration vectors).
33 #include "apr_strings.h"
34 #include "apr_portable.h"
35 #include "apr_file_io.h"
36 #include "apr_fnmatch.h"
38 #define APR_WANT_STDIO
39 #define APR_WANT_STRFUNC
42 #include "ap_config.h"
44 #include "http_config.h"
45 #include "http_protocol.h"
46 #include "http_core.h"
47 #include "http_log.h" /* for errors in parse_htaccess */
48 #include "http_request.h" /* for default_handler (see invoke_handler) */
49 #include "http_main.h"
50 #include "http_vhost.h"
51 #include "util_cfgtree.h"
53 AP_DECLARE_DATA const char *ap_server_argv0 = NULL;
54 AP_DECLARE_DATA const char *ap_server_root = NULL;
55 AP_DECLARE_DATA server_rec *ap_server_conf = NULL;
56 AP_DECLARE_DATA apr_pool_t *ap_pglobal = NULL;
58 AP_DECLARE_DATA apr_array_header_t *ap_server_pre_read_config = NULL;
59 AP_DECLARE_DATA apr_array_header_t *ap_server_post_read_config = NULL;
60 AP_DECLARE_DATA apr_array_header_t *ap_server_config_defines = NULL;
62 AP_DECLARE_DATA ap_directive_t *ap_conftree = NULL;
65 APR_HOOK_LINK(header_parser)
66 APR_HOOK_LINK(pre_config)
67 APR_HOOK_LINK(check_config)
68 APR_HOOK_LINK(post_config)
69 APR_HOOK_LINK(open_logs)
70 APR_HOOK_LINK(child_init)
71 APR_HOOK_LINK(handler)
72 APR_HOOK_LINK(quick_handler)
73 APR_HOOK_LINK(optional_fn_retrieve)
74 APR_HOOK_LINK(test_config)
77 AP_IMPLEMENT_HOOK_RUN_ALL(int, header_parser,
78 (request_rec *r), (r), OK, DECLINED)
80 AP_IMPLEMENT_HOOK_RUN_ALL(int, pre_config,
81 (apr_pool_t *pconf, apr_pool_t *plog,
83 (pconf, plog, ptemp), OK, DECLINED)
85 AP_IMPLEMENT_HOOK_RUN_ALL(int, check_config,
86 (apr_pool_t *pconf, apr_pool_t *plog,
87 apr_pool_t *ptemp, server_rec *s),
88 (pconf, plog, ptemp, s), OK, DECLINED)
90 AP_IMPLEMENT_HOOK_VOID(test_config,
91 (apr_pool_t *pconf, server_rec *s),
94 AP_IMPLEMENT_HOOK_RUN_ALL(int, post_config,
95 (apr_pool_t *pconf, apr_pool_t *plog,
96 apr_pool_t *ptemp, server_rec *s),
97 (pconf, plog, ptemp, s), OK, DECLINED)
99 /* During the course of debugging I expanded this macro out, so
100 * rather than remove all the useful information there is in the
101 * following lines, I'm going to leave it here in case anyone
102 * else finds it useful.
104 * Ben has looked at it and thinks it correct :)
106 AP_DECLARE(int) ap_hook_post_config(ap_HOOK_post_config_t *pf,
107 const char * const *aszPre,
108 const char * const *aszSucc,
111 ap_LINK_post_config_t *pHook;
113 if (!_hooks.link_post_config) {
114 _hooks.link_post_config = apr_array_make(apr_hook_global_pool, 1,
115 sizeof(ap_LINK_post_config_t));
116 apr_hook_sort_register("post_config", &_hooks.link_post_config);
119 pHook = apr_array_push(_hooks.link_post_config);
121 pHook->aszPredecessors = aszPre;
122 pHook->aszSuccessors = aszSucc;
123 pHook->nOrder = nOrder;
124 pHook->szName = apr_hook_debug_current;
126 if (apr_hook_debug_enabled)
127 apr_hook_debug_show("post_config", aszPre, aszSucc);
130 AP_DECLARE(apr_array_header_t *) ap_hook_get_post_config(void) {
131 return _hooks.link_post_config;
134 AP_DECLARE(int) ap_run_post_config(apr_pool_t *pconf,
139 ap_LINK_post_config_t *pHook;
142 if(!_hooks.link_post_config)
145 pHook = (ap_LINK_post_config_t *)_hooks.link_post_config->elts;
146 for (n = 0; n < _hooks.link_post_config->nelts; ++n)
147 pHook[n].pFunc (pconf, plog, ptemp, s);
151 AP_IMPLEMENT_HOOK_RUN_ALL(int, open_logs,
152 (apr_pool_t *pconf, apr_pool_t *plog,
153 apr_pool_t *ptemp, server_rec *s),
154 (pconf, plog, ptemp, s), OK, DECLINED)
156 AP_IMPLEMENT_HOOK_VOID(child_init,
157 (apr_pool_t *pchild, server_rec *s),
160 AP_IMPLEMENT_HOOK_RUN_FIRST(int, handler, (request_rec *r),
163 AP_IMPLEMENT_HOOK_RUN_FIRST(int, quick_handler, (request_rec *r, int lookup),
164 (r, lookup), DECLINED)
166 AP_IMPLEMENT_HOOK_VOID(optional_fn_retrieve, (void), ())
168 /****************************************************************
170 * We begin with the functions which deal with the linked list
171 * of modules which control just about all of the server operation.
174 /* total_modules is the number of modules that have been linked
177 static int total_modules = 0;
179 /* dynamic_modules is the number of modules that have been added
180 * after the pre-loaded ones have been set up. It shouldn't be larger
181 * than DYNAMIC_MODULE_LIMIT.
183 static int dynamic_modules = 0;
185 AP_DECLARE_DATA module *ap_top_module = NULL;
186 AP_DECLARE_DATA module **ap_loaded_modules=NULL;
188 static apr_hash_t *ap_config_hash = NULL;
190 typedef int (*handler_func)(request_rec *);
191 typedef void *(*dir_maker_func)(apr_pool_t *, char *);
192 typedef void *(*merger_func)(apr_pool_t *, void *, void *);
194 /* maximum nesting level for config directories */
195 #ifndef AP_MAX_INCLUDE_DIR_DEPTH
196 #define AP_MAX_INCLUDE_DIR_DEPTH (128)
199 /* Dealing with config vectors. These are associated with per-directory,
200 * per-server, and per-request configuration, and have a void* pointer for
201 * each modules. The nature of the structure pointed to is private to the
202 * module in question... the core doesn't (and can't) know. However, there
203 * are defined interfaces which allow it to create instances of its private
204 * per-directory and per-server structures, and to merge the per-directory
205 * structures of a directory and its subdirectory (producing a new one in
206 * which the defaults applying to the base directory have been properly
210 static ap_conf_vector_t *create_empty_config(apr_pool_t *p)
212 void *conf_vector = apr_pcalloc(p, sizeof(void *) *
213 (total_modules + DYNAMIC_MODULE_LIMIT));
217 static ap_conf_vector_t *create_default_per_dir_config(apr_pool_t *p)
219 void **conf_vector = apr_pcalloc(p, sizeof(void *) *
220 (total_modules + DYNAMIC_MODULE_LIMIT));
223 for (modp = ap_top_module; modp; modp = modp->next) {
224 dir_maker_func df = modp->create_dir_config;
227 conf_vector[modp->module_index] = (*df)(p, NULL);
230 return (ap_conf_vector_t *)conf_vector;
233 AP_CORE_DECLARE(ap_conf_vector_t *) ap_merge_per_dir_configs(apr_pool_t *p,
234 ap_conf_vector_t *base,
235 ap_conf_vector_t *new_conf)
237 void **conf_vector = apr_palloc(p, sizeof(void *) * total_modules);
238 void **base_vector = (void **)base;
239 void **new_vector = (void **)new_conf;
242 for (modp = ap_top_module; modp; modp = modp->next) {
243 int i = modp->module_index;
245 if (!new_vector[i]) {
246 conf_vector[i] = base_vector[i];
249 merger_func df = modp->merge_dir_config;
250 if (df && base_vector[i]) {
251 conf_vector[i] = (*df)(p, base_vector[i], new_vector[i]);
254 conf_vector[i] = new_vector[i];
258 return (ap_conf_vector_t *)conf_vector;
261 static ap_conf_vector_t *create_server_config(apr_pool_t *p, server_rec *s)
263 void **conf_vector = apr_pcalloc(p, sizeof(void *) *
264 (total_modules + DYNAMIC_MODULE_LIMIT));
267 for (modp = ap_top_module; modp; modp = modp->next) {
268 if (modp->create_server_config)
269 conf_vector[modp->module_index] = (*modp->create_server_config)(p, s);
272 return (ap_conf_vector_t *)conf_vector;
275 static void merge_server_configs(apr_pool_t *p, ap_conf_vector_t *base,
276 ap_conf_vector_t *virt)
278 /* Can reuse the 'virt' vector for the spine of it, since we don't
279 * have to deal with the moral equivalent of .htaccess files here...
282 void **base_vector = (void **)base;
283 void **virt_vector = (void **)virt;
286 for (modp = ap_top_module; modp; modp = modp->next) {
287 merger_func df = modp->merge_server_config;
288 int i = modp->module_index;
291 virt_vector[i] = base_vector[i];
293 virt_vector[i] = (*df)(p, base_vector[i], virt_vector[i]);
297 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_request_config(apr_pool_t *p)
299 return create_empty_config(p);
302 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_conn_config(apr_pool_t *p)
304 return create_empty_config(p);
307 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_per_dir_config(apr_pool_t *p)
309 return create_empty_config(p);
312 static int ap_invoke_filter_init(ap_filter_t *filters)
315 if (filters->frec->filter_init_func) {
316 int result = filters->frec->filter_init_func(filters);
321 filters = filters->next;
327 * TODO: Move this to an appropriate include file and possibly prefix it
330 #define DEFAULT_HANDLER_NAME ""
332 AP_CORE_DECLARE(int) ap_invoke_handler(request_rec *r)
337 const char *old_handler = r->handler;
341 * The new insert_filter stage makes the most sense here. We only use
342 * it when we are going to run the request, so we must insert filters
343 * if any are available. Since the goal of this phase is to allow all
344 * modules to insert a filter if they want to, this filter returns
345 * void. I just can't see any way that this filter can reasonably
346 * fail, either your modules inserts something or it doesn't. rbb
348 ap_run_insert_filter(r);
350 /* Before continuing, allow each filter that is in the two chains to
351 * run their init function to let them do any magic before we could
352 * start generating data.
354 result = ap_invoke_filter_init(r->input_filters);
358 result = ap_invoke_filter_init(r->output_filters);
364 if (r->content_type) {
365 handler = r->content_type;
366 if ((p=ap_strchr_c(handler, ';')) != NULL) {
367 char *new_handler = (char *)apr_pmemdup(r->pool, handler,
369 char *p2 = new_handler + (p - handler);
370 handler = new_handler;
372 /* exclude media type arguments */
373 while (p2 > handler && p2[-1] == ' ')
374 --p2; /* strip trailing spaces */
380 handler = DEFAULT_HANDLER_NAME;
383 r->handler = handler;
386 result = ap_run_handler(r);
388 r->handler = old_handler;
390 if (result == DECLINED && r->handler && r->filename) {
391 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
392 "handler \"%s\" not found for: %s", r->handler, r->filename);
394 if ((result != OK) && (result != DONE) && (result != DECLINED) && (result != SUSPENDED)
395 && (result != AP_FILTER_ERROR) /* ap_die() knows about this specifically */
396 && !ap_is_HTTP_VALID_RESPONSE(result)) {
397 /* If a module is deliberately returning something else
398 * (request_rec in non-HTTP or proprietary extension?)
399 * let it set a note to allow it explicitly.
400 * Otherwise, a return code that is neither reserved nor HTTP
401 * is a bug, as in PR#31759.
403 ignore = apr_table_get(r->notes, "HTTP_IGNORE_RANGE");
405 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
406 "Handler for %s returned invalid result code %d",
408 result = HTTP_INTERNAL_SERVER_ERROR;
412 return result == DECLINED ? HTTP_INTERNAL_SERVER_ERROR : result;
415 AP_DECLARE(int) ap_method_is_limited(cmd_parms *cmd, const char *method)
419 methnum = ap_method_number_of(method);
422 * A method number either hardcoded into apache or
423 * added by a module and registered.
425 if (methnum != M_INVALID) {
426 return (cmd->limited & (AP_METHOD_BIT << methnum)) ? 1 : 0;
429 return 0; /* not found */
432 AP_DECLARE(void) ap_register_hooks(module *m, apr_pool_t *p)
434 if (m->register_hooks) {
435 if (getenv("SHOW_HOOKS")) {
436 printf("Registering hooks for %s\n", m->name);
437 apr_hook_debug_enabled = 1;
440 apr_hook_debug_current = m->name;
441 m->register_hooks(p);
445 static void ap_add_module_commands(module *m, apr_pool_t *p);
447 typedef struct ap_mod_list_struct ap_mod_list;
448 struct ap_mod_list_struct {
449 struct ap_mod_list_struct *next;
451 const command_rec *cmd;
454 static apr_status_t reload_conf_hash(void *baton)
456 ap_config_hash = NULL;
460 static void rebuild_conf_hash(apr_pool_t *p, int add_prelinked)
464 ap_config_hash = apr_hash_make(p);
466 apr_pool_cleanup_register(p, NULL, reload_conf_hash,
467 apr_pool_cleanup_null);
469 for (m = ap_prelinked_modules; *m != NULL; m++) {
470 ap_add_module_commands(*m, p);
475 static void ap_add_module_commands(module *m, apr_pool_t *p)
479 const command_rec *cmd;
484 if (ap_config_hash == NULL) {
485 rebuild_conf_hash(p, 0);
488 tpool = apr_hash_pool_get(ap_config_hash);
490 while (cmd && cmd->name) {
491 mln = apr_palloc(tpool, sizeof(ap_mod_list));
494 dir = apr_pstrdup(tpool, cmd->name);
498 mln->next = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
499 apr_hash_set(ap_config_hash, dir, APR_HASH_KEY_STRING, mln);
505 /* One-time setup for precompiled modules --- NOT to be done on restart */
507 AP_DECLARE(const char *) ap_add_module(module *m, apr_pool_t *p)
509 /* This could be called from a LoadModule httpd.conf command,
510 * after the file has been linked and the module structure within it
514 if (m->version != MODULE_MAGIC_NUMBER_MAJOR) {
515 return apr_psprintf(p, "Module \"%s\" is not compatible with this "
516 "version of Apache (found %d, need %d). Please "
517 "contact the vendor for the correct version.",
518 m->name, m->version, MODULE_MAGIC_NUMBER_MAJOR);
521 if (m->next == NULL) {
522 m->next = ap_top_module;
526 if (m->module_index == -1) {
527 m->module_index = total_modules++;
530 if (dynamic_modules > DYNAMIC_MODULE_LIMIT) {
531 return apr_psprintf(p, "Module \"%s\" could not be loaded, "
532 "because the dynamic module limit was "
533 "reached. Please increase "
534 "DYNAMIC_MODULE_LIMIT and recompile.", m->name);
538 /* Some C compilers put a complete path into __FILE__, but we want
539 * only the filename (e.g. mod_includes.c). So check for path
540 * components (Unix and DOS), and remove them.
543 if (ap_strrchr_c(m->name, '/'))
544 m->name = 1 + ap_strrchr_c(m->name, '/');
546 if (ap_strrchr_c(m->name, '\\'))
547 m->name = 1 + ap_strrchr_c(m->name, '\\');
551 * "*POSIX(/home/martin/apache/src/modules/standard/mod_info.c)"
554 /* We cannot fix the string in-place, because it's const */
555 if (m->name[strlen(m->name)-1] == ')') {
556 char *tmp = strdup(m->name); /* FIXME: memory leak, albeit a small one */
557 tmp[strlen(tmp)-1] = '\0';
560 #endif /*_OSD_POSIX*/
562 ap_add_module_commands(m, p);
563 /* FIXME: is this the right place to call this?
564 * It doesn't appear to be
566 ap_register_hooks(m, p);
572 * remove_module undoes what add_module did. There are some caveats:
573 * when the module is removed, its slot is lost so all the current
574 * per-dir and per-server configurations are invalid. So we should
575 * only ever call this function when you are invalidating almost
576 * all our current data. I.e. when doing a restart.
579 AP_DECLARE(void) ap_remove_module(module *m)
583 modp = ap_top_module;
585 /* We are the top module, special case */
586 ap_top_module = modp->next;
590 /* Not the top module, find use. When found modp will
591 * point to the module _before_ us in the list
594 while (modp && modp->next != m) {
599 /* Uh-oh, this module doesn't exist */
600 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
601 "Cannot remove module %s: not found in module list",
606 /* Eliminate us from the module list */
607 modp->next = modp->next->next;
610 m->module_index = -1; /* simulate being unloaded, should
616 AP_DECLARE(const char *) ap_add_loaded_module(module *mod, apr_pool_t *p)
622 * Add module pointer to top of chained module list
624 error = ap_add_module(mod, p);
630 * And module pointer to list of loaded modules
632 * Notes: 1. ap_add_module() would already complain if no more space
633 * exists for adding a dynamically loaded module
634 * 2. ap_add_module() accepts double inclusion, so we have
635 * to accept this, too.
637 for (m = ap_loaded_modules; *m != NULL; m++)
645 AP_DECLARE(void) ap_remove_loaded_module(module *mod)
652 * Remove module pointer from chained module list
654 ap_remove_module(mod);
657 * Remove module pointer from list of loaded modules
659 * Note: 1. We cannot determine if the module was successfully
660 * removed by ap_remove_module().
661 * 2. We have not to complain explicity when the module
662 * is not found because ap_remove_module() did it
665 for (m = m2 = ap_loaded_modules, done = 0; *m2 != NULL; m2++) {
666 if (*m2 == mod && done == 0)
675 AP_DECLARE(const char *) ap_setup_prelinked_modules(process_rec *process)
681 apr_hook_global_pool=process->pconf;
683 rebuild_conf_hash(process->pconf, 0);
686 * Initialise total_modules variable and module indices
689 for (m = ap_preloaded_modules; *m != NULL; m++)
690 (*m)->module_index = total_modules++;
693 * Initialise list of loaded modules
695 ap_loaded_modules = (module **)apr_palloc(process->pool,
696 sizeof(module *) * (total_modules + DYNAMIC_MODULE_LIMIT + 1));
698 if (ap_loaded_modules == NULL) {
699 return "Ouch! Out of memory in ap_setup_prelinked_modules()!";
702 for (m = ap_preloaded_modules, m2 = ap_loaded_modules; *m != NULL; )
708 * Initialize chain of linked (=activate) modules
710 for (m = ap_prelinked_modules; *m != NULL; m++) {
711 error = ap_add_module(*m, process->pconf);
722 AP_DECLARE(const char *) ap_find_module_name(module *m)
727 AP_DECLARE(module *) ap_find_linked_module(const char *name)
731 for (modp = ap_top_module; modp; modp = modp->next) {
732 if (strcmp(modp->name, name) == 0)
739 /*****************************************************************
741 * Resource, access, and .htaccess config files now parsed by a common
744 * Let's begin with the basics; parsing the line and
745 * invoking the function...
748 #define AP_MAX_ARGC 64
750 static const char *invoke_cmd(const command_rec *cmd, cmd_parms *parms,
751 void *mconfig, const char *args)
754 const char *errmsg = NULL;
756 if ((parms->override & cmd->req_override) == 0)
757 return apr_pstrcat(parms->pool, cmd->name, " not allowed here", NULL);
759 parms->info = cmd->cmd_data;
762 switch (cmd->args_how) {
764 #ifdef RESOLVE_ENV_PER_TOKEN
765 args = ap_resolve_env(parms->pool,args);
767 return cmd->AP_RAW_ARGS(parms, mconfig, args);
771 char *argv[AP_MAX_ARGC];
775 w = ap_getword_conf(parms->pool, &args);
776 if (*w == '\0' && *args == '\0') {
781 } while (argc < AP_MAX_ARGC && *args != '\0');
783 return cmd->AP_TAKE_ARGV(parms, mconfig, argc, argv);
788 return apr_pstrcat(parms->pool, cmd->name, " takes no arguments",
791 return cmd->AP_NO_ARGS(parms, mconfig);
794 w = ap_getword_conf(parms->pool, &args);
796 if (*w == '\0' || *args != 0)
797 return apr_pstrcat(parms->pool, cmd->name, " takes one argument",
798 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
800 return cmd->AP_TAKE1(parms, mconfig, w);
803 w = ap_getword_conf(parms->pool, &args);
804 w2 = ap_getword_conf(parms->pool, &args);
806 if (*w == '\0' || *w2 == '\0' || *args != 0)
807 return apr_pstrcat(parms->pool, cmd->name, " takes two arguments",
808 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
810 return cmd->AP_TAKE2(parms, mconfig, w, w2);
813 w = ap_getword_conf(parms->pool, &args);
814 w2 = ap_getword_conf(parms->pool, &args);
816 if (*w == '\0' || *args != 0)
817 return apr_pstrcat(parms->pool, cmd->name, " takes 1-2 arguments",
818 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
820 return cmd->AP_TAKE2(parms, mconfig, w, *w2 ? w2 : NULL);
823 w = ap_getword_conf(parms->pool, &args);
824 w2 = ap_getword_conf(parms->pool, &args);
825 w3 = ap_getword_conf(parms->pool, &args);
827 if (*w == '\0' || *w2 == '\0' || *w3 == '\0' || *args != 0)
828 return apr_pstrcat(parms->pool, cmd->name, " takes three arguments",
829 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
831 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
834 w = ap_getword_conf(parms->pool, &args);
835 w2 = ap_getword_conf(parms->pool, &args);
836 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
838 if (*w == '\0' || *w2 == '\0' || *args != 0)
839 return apr_pstrcat(parms->pool, cmd->name,
840 " takes two or three arguments",
841 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
843 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
846 w = ap_getword_conf(parms->pool, &args);
847 w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
848 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
850 if (*w == '\0' || *args != 0)
851 return apr_pstrcat(parms->pool, cmd->name,
852 " takes one, two or three arguments",
853 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
855 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
858 w = ap_getword_conf(parms->pool, &args);
859 w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
860 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
862 if (*w == '\0' || (w2 && *w2 && !w3) || *args != 0)
863 return apr_pstrcat(parms->pool, cmd->name,
864 " takes one or three arguments",
865 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
867 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
870 while (*(w = ap_getword_conf(parms->pool, &args)) != '\0') {
872 errmsg = cmd->AP_TAKE1(parms, mconfig, w);
874 if (errmsg && strcmp(errmsg, DECLINE_CMD) != 0)
881 w = ap_getword_conf(parms->pool, &args);
883 if (*w == '\0' || *args == 0)
884 return apr_pstrcat(parms->pool, cmd->name,
885 " requires at least two arguments",
886 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
888 while (*(w2 = ap_getword_conf(parms->pool, &args)) != '\0') {
890 errmsg = cmd->AP_TAKE2(parms, mconfig, w, w2);
892 if (errmsg && strcmp(errmsg, DECLINE_CMD) != 0)
899 w = ap_getword_conf(parms->pool, &args);
901 if (*w == '\0' || (strcasecmp(w, "on") && strcasecmp(w, "off")))
902 return apr_pstrcat(parms->pool, cmd->name, " must be On or Off",
905 return cmd->AP_FLAG(parms, mconfig, strcasecmp(w, "off") != 0);
908 return apr_pstrcat(parms->pool, cmd->name,
909 " is improperly configured internally (server bug)",
914 AP_CORE_DECLARE(const command_rec *) ap_find_command(const char *name,
915 const command_rec *cmds)
918 if (!strcasecmp(name, cmds->name))
927 AP_CORE_DECLARE(const command_rec *) ap_find_command_in_modules(
928 const char *cmd_name, module **mod)
930 const command_rec *cmdp;
933 for (modp = *mod; modp; modp = modp->next) {
934 if (modp->cmds && (cmdp = ap_find_command(cmd_name, modp->cmds))) {
943 AP_CORE_DECLARE(void *) ap_set_config_vectors(server_rec *server,
944 ap_conf_vector_t *section_vector,
946 module *mod, apr_pool_t *pconf)
948 void *section_config = ap_get_module_config(section_vector, mod);
949 void *server_config = ap_get_module_config(server->module_config, mod);
951 if (!section_config && mod->create_dir_config) {
952 /* ### need to fix the create_dir_config functions' prototype... */
953 section_config = (*mod->create_dir_config)(pconf, (char *)section);
954 ap_set_module_config(section_vector, mod, section_config);
957 if (!server_config && mod->create_server_config) {
958 server_config = (*mod->create_server_config)(pconf, server);
959 ap_set_module_config(server->module_config, mod, server_config);
962 return section_config;
965 static const char *execute_now(char *cmd_line, const char *args,
967 apr_pool_t *p, apr_pool_t *ptemp,
968 ap_directive_t **sub_tree,
969 ap_directive_t *parent);
971 static const char *ap_build_config_sub(apr_pool_t *p, apr_pool_t *temp_pool,
972 const char *l, cmd_parms *parms,
973 ap_directive_t **current,
974 ap_directive_t **curr_parent,
975 ap_directive_t **conftree)
977 const char *retval = NULL;
980 ap_directive_t *newdir;
981 module *mod = ap_top_module;
982 const command_rec *cmd;
984 if (*l == '#' || *l == '\0')
987 #if RESOLVE_ENV_PER_TOKEN
990 args = ap_resolve_env(temp_pool, l);
993 cmd_name = ap_getword_conf(p, &args);
994 if (*cmd_name == '\0') {
995 /* Note: this branch should not occur. An empty line should have
996 * triggered the exit further above.
1001 if (cmd_name[1] != '/') {
1002 char *lastc = cmd_name + strlen(cmd_name) - 1;
1003 if (*lastc == '>') {
1006 if (cmd_name[0] == '<' && *args == '\0') {
1011 newdir = apr_pcalloc(p, sizeof(ap_directive_t));
1012 newdir->filename = parms->config_file->name;
1013 newdir->line_num = parms->config_file->line_number;
1014 newdir->directive = cmd_name;
1015 newdir->args = apr_pstrdup(p, args);
1017 if ((cmd = ap_find_command_in_modules(cmd_name, &mod)) != NULL) {
1018 if (cmd->req_override & EXEC_ON_READ) {
1019 ap_directive_t *sub_tree = NULL;
1021 parms->err_directive = newdir;
1022 retval = execute_now(cmd_name, args, parms, p, temp_pool,
1023 &sub_tree, *curr_parent);
1025 (*current)->next = sub_tree;
1028 *current = sub_tree;
1030 (*curr_parent)->first_child = (*current);
1033 (*current)->parent = (*curr_parent);
1038 /* Before walking *current to the end of the list,
1039 * set the head to *current.
1041 *conftree = *current;
1043 while ((*current)->next != NULL) {
1044 (*current) = (*current)->next;
1045 (*current)->parent = (*curr_parent);
1052 if (cmd_name[0] == '<') {
1053 if (cmd_name[1] != '/') {
1054 (*current) = ap_add_node(curr_parent, *current, newdir, 1);
1056 else if (*curr_parent == NULL) {
1057 parms->err_directive = newdir;
1058 return apr_pstrcat(p, cmd_name,
1059 " without matching <", cmd_name + 2,
1063 char *bracket = cmd_name + strlen(cmd_name) - 1;
1065 if (*bracket != '>') {
1066 parms->err_directive = newdir;
1067 return apr_pstrcat(p, cmd_name,
1068 "> directive missing closing '>'", NULL);
1073 if (strcasecmp(cmd_name + 2,
1074 (*curr_parent)->directive + 1) != 0) {
1075 parms->err_directive = newdir;
1076 return apr_pstrcat(p, "Expected </",
1077 (*curr_parent)->directive + 1, "> but saw ",
1078 cmd_name, ">", NULL);
1083 /* done with this section; move up a level */
1084 *current = *curr_parent;
1085 *curr_parent = (*current)->parent;
1089 *current = ap_add_node(curr_parent, *current, newdir, 0);
1095 AP_DECLARE(const char *) ap_build_cont_config(apr_pool_t *p,
1096 apr_pool_t *temp_pool,
1098 ap_directive_t **current,
1099 ap_directive_t **curr_parent,
1100 char *orig_directive)
1105 ap_directive_t *sub_tree = NULL;
1107 /* Since this function can be called recursively, allocate
1108 * the temporary 8k string buffer from the temp_pool rather
1109 * than the stack to avoid over-running a fixed length stack.
1111 l = apr_palloc(temp_pool, MAX_STRING_LEN);
1113 bracket = apr_pstrcat(p, orig_directive + 1, ">", NULL);
1114 while (!(ap_cfg_getline(l, MAX_STRING_LEN, parms->config_file))) {
1115 if (!memcmp(l, "</", 2)
1116 && (strcasecmp(l + 2, bracket) == 0)
1117 && (*curr_parent == NULL)) {
1120 retval = ap_build_config_sub(p, temp_pool, l, parms, current,
1121 curr_parent, &sub_tree);
1125 if (sub_tree == NULL && curr_parent != NULL) {
1126 sub_tree = *curr_parent;
1129 if (sub_tree == NULL && current != NULL) {
1130 sub_tree = *current;
1134 *current = sub_tree;
1138 static const char *ap_walk_config_sub(const ap_directive_t *current,
1140 ap_conf_vector_t *section_vector)
1142 const command_rec *cmd;
1144 char *dir = apr_pstrdup(parms->pool, current->directive);
1146 ap_str_tolower(dir);
1148 ml = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
1151 parms->err_directive = current;
1152 return apr_pstrcat(parms->pool, "Invalid command '",
1154 "', perhaps misspelled or defined by a module "
1155 "not included in the server configuration",
1159 for ( ; ml != NULL; ml = ml->next) {
1160 void *dir_config = ap_set_config_vectors(parms->server,
1168 /* Once was enough? */
1169 if (cmd->req_override & EXEC_ON_READ) {
1173 retval = invoke_cmd(cmd, parms, dir_config, current->args);
1175 if (retval != NULL && strcmp(retval, DECLINE_CMD) != 0) {
1176 /* If the directive in error has already been set, don't
1177 * replace it. Otherwise, an error inside a container
1178 * will be reported as occuring on the first line of the
1181 if (!parms->err_directive) {
1182 parms->err_directive = current;
1191 AP_DECLARE(const char *) ap_walk_config(ap_directive_t *current,
1193 ap_conf_vector_t *section_vector)
1195 ap_conf_vector_t *oldconfig = parms->context;
1197 parms->context = section_vector;
1199 /* scan through all directives, executing each one */
1200 for (; current != NULL; current = current->next) {
1203 parms->directive = current;
1205 /* actually parse the command and execute the correct function */
1206 errmsg = ap_walk_config_sub(current, parms, section_vector);
1207 if (errmsg != NULL) {
1208 /* restore the context (just in case) */
1209 parms->context = oldconfig;
1214 parms->context = oldconfig;
1218 AP_DECLARE(const char *) ap_build_config(cmd_parms *parms,
1219 apr_pool_t *p, apr_pool_t *temp_pool,
1220 ap_directive_t **conftree)
1222 ap_directive_t *current = *conftree;
1223 ap_directive_t *curr_parent = NULL;
1224 char *l = apr_palloc (temp_pool, MAX_STRING_LEN);
1227 if (current != NULL) {
1228 while (current->next) {
1229 current = current->next;
1233 while (!(ap_cfg_getline(l, MAX_STRING_LEN, parms->config_file))) {
1234 errmsg = ap_build_config_sub(p, temp_pool, l, parms,
1235 ¤t, &curr_parent, conftree);
1239 if (*conftree == NULL && curr_parent != NULL) {
1240 *conftree = curr_parent;
1243 if (*conftree == NULL && current != NULL) {
1244 *conftree = current;
1248 if (curr_parent != NULL) {
1251 while (curr_parent != NULL) {
1252 errmsg = apr_psprintf(p, "%s%s%s:%u: %s> was not closed.",
1254 *errmsg == '\0' ? "" : APR_EOL_STR,
1255 curr_parent->filename,
1256 curr_parent->line_num,
1257 curr_parent->directive);
1259 parms->err_directive = curr_parent;
1260 curr_parent = curr_parent->parent;
1270 * Generic command functions...
1273 AP_DECLARE_NONSTD(const char *) ap_set_string_slot(cmd_parms *cmd,
1277 int offset = (int)(long)cmd->info;
1279 *(const char **)((char *)struct_ptr + offset) = arg;
1284 AP_DECLARE_NONSTD(const char *) ap_set_int_slot(cmd_parms *cmd,
1289 char *error_str = NULL;
1290 int offset = (int)(long)cmd->info;
1292 *(int *)((char*)struct_ptr + offset) = strtol(arg, &endptr, 10);
1294 if ((*arg == '\0') || (*endptr != '\0')) {
1295 error_str = apr_psprintf(cmd->pool,
1296 "Invalid value for directive %s, expected integer",
1297 cmd->directive->directive);
1303 AP_DECLARE_NONSTD(const char *) ap_set_string_slot_lower(cmd_parms *cmd,
1307 char *arg = apr_pstrdup(cmd->pool,arg_);
1308 int offset = (int)(long)cmd->info;
1310 ap_str_tolower(arg);
1311 *(char **)((char *)struct_ptr + offset) = arg;
1316 AP_DECLARE_NONSTD(const char *) ap_set_flag_slot(cmd_parms *cmd,
1317 void *struct_ptr_v, int arg)
1319 int offset = (int)(long)cmd->info;
1320 char *struct_ptr = (char *)struct_ptr_v;
1322 *(int *)(struct_ptr + offset) = arg ? 1 : 0;
1327 AP_DECLARE_NONSTD(const char *) ap_set_file_slot(cmd_parms *cmd, void *struct_ptr,
1330 /* Prepend server_root to relative arg.
1331 * This allows most args to be independent of server_root,
1332 * so the server can be moved or mirrored with less pain.
1335 int offset = (int)(long)cmd->info;
1337 path = ap_server_root_relative(cmd->pool, arg);
1340 return apr_pstrcat(cmd->pool, "Invalid file path ",
1344 *(const char **) ((char*)struct_ptr + offset) = path;
1349 AP_DECLARE_NONSTD(const char *) ap_set_deprecated(cmd_parms *cmd,
1353 return cmd->cmd->errmsg;
1356 /*****************************************************************
1358 * Reading whole config files...
1361 static cmd_parms default_parms =
1362 {NULL, 0, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
1364 AP_DECLARE(char *) ap_server_root_relative(apr_pool_t *p, const char *file)
1366 char *newpath = NULL;
1368 rv = apr_filepath_merge(&newpath, ap_server_root, file,
1369 APR_FILEPATH_TRUENAME, p);
1370 if (newpath && (rv == APR_SUCCESS || APR_STATUS_IS_EPATHWILD(rv)
1371 || APR_STATUS_IS_ENOENT(rv)
1372 || APR_STATUS_IS_ENOTDIR(rv))) {
1380 AP_DECLARE(const char *) ap_soak_end_container(cmd_parms *cmd, char *directive)
1382 char l[MAX_STRING_LEN];
1386 while(!(ap_cfg_getline(l, MAX_STRING_LEN, cmd->config_file))) {
1387 #if RESOLVE_ENV_PER_TOKEN
1390 args = ap_resolve_env(cmd->temp_pool, l);
1393 cmd_name = ap_getword_conf(cmd->pool, &args);
1394 if (cmd_name[0] == '<') {
1395 if (cmd_name[1] == '/') {
1396 cmd_name[strlen(cmd_name) - 1] = '\0';
1398 if (strcasecmp(cmd_name + 2, directive + 1) != 0) {
1399 return apr_pstrcat(cmd->pool, "Expected </",
1400 directive + 1, "> but saw ",
1401 cmd_name, ">", NULL);
1404 return NULL; /* found end of container */
1409 if (*args == '\0' && cmd_name[strlen(cmd_name) - 1] == '>') {
1410 cmd_name[strlen(cmd_name) - 1] = '\0';
1413 if ((msg = ap_soak_end_container(cmd, cmd_name)) != NULL) {
1420 return apr_pstrcat(cmd->pool, "Expected </",
1421 directive + 1, "> before end of configuration",
1425 static const char *execute_now(char *cmd_line, const char *args,
1427 apr_pool_t *p, apr_pool_t *ptemp,
1428 ap_directive_t **sub_tree,
1429 ap_directive_t *parent)
1431 const command_rec *cmd;
1433 char *dir = apr_pstrdup(parms->pool, cmd_line);
1435 ap_str_tolower(dir);
1437 ml = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
1440 return apr_pstrcat(parms->pool, "Invalid command '",
1442 "', perhaps misspelled or defined by a module "
1443 "not included in the server configuration",
1447 for ( ; ml != NULL; ml = ml->next) {
1451 retval = invoke_cmd(cmd, parms, sub_tree, args);
1453 if (retval != NULL) {
1461 /* This structure and the following functions are needed for the
1462 * table-based config file reading. They are passed to the
1463 * cfg_open_custom() routine.
1466 /* Structure to be passed to cfg_open_custom(): it contains an
1467 * index which is incremented from 0 to nelts on each call to
1468 * cfg_getline() (which in turn calls arr_elts_getstr())
1469 * and an apr_array_header_t pointer for the string array.
1472 apr_array_header_t *array;
1477 /* arr_elts_getstr() returns the next line from the string array. */
1478 static void *arr_elts_getstr(void *buf, size_t bufsiz, void *param)
1480 arr_elts_param_t *arr_param = (arr_elts_param_t *)param;
1482 /* End of array reached? */
1483 if (++arr_param->curr_idx > arr_param->array->nelts)
1486 /* return the line */
1488 ((char **)arr_param->array->elts)[arr_param->curr_idx - 1],
1495 /* arr_elts_close(): dummy close routine (makes sure no more lines can be read) */
1496 static int arr_elts_close(void *param)
1498 arr_elts_param_t *arr_param = (arr_elts_param_t *)param;
1500 arr_param->curr_idx = arr_param->array->nelts;
1505 static const char *process_command_config(server_rec *s,
1506 apr_array_header_t *arr,
1507 ap_directive_t **conftree,
1513 arr_elts_param_t arr_parms;
1515 arr_parms.curr_idx = 0;
1516 arr_parms.array = arr;
1518 if (ap_config_hash == NULL) {
1519 rebuild_conf_hash(s->process->pconf, 1);
1522 parms = default_parms;
1524 parms.temp_pool = ptemp;
1526 parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1527 parms.override_opts = OPT_ALL | OPT_SYM_OWNER | OPT_MULTI;
1529 parms.config_file = ap_pcfg_open_custom(p, "-c/-C directives",
1531 arr_elts_getstr, arr_elts_close);
1533 errmsg = ap_build_config(&parms, p, ptemp, conftree);
1534 ap_cfg_closefile(parms.config_file);
1537 return apr_pstrcat(p, "Syntax error in -C/-c directive: ", errmsg,
1548 static int fname_alphasort(const void *fn1, const void *fn2)
1550 const fnames *f1 = fn1;
1551 const fnames *f2 = fn2;
1553 return strcmp(f1->fname,f2->fname);
1556 static const char *process_resource_config_nofnmatch(server_rec *s,
1558 ap_directive_t **conftree,
1564 ap_configfile_t *cfp;
1568 if (ap_is_directory(p, fname)) {
1572 apr_array_header_t *candidates = NULL;
1574 char *path = apr_pstrdup(p, fname);
1576 if (++depth > AP_MAX_INCLUDE_DIR_DEPTH) {
1577 return apr_psprintf(p, "Directory %s exceeds the maximum include "
1578 "directory nesting level of %u. You have "
1579 "probably a recursion somewhere.", path,
1580 AP_MAX_INCLUDE_DIR_DEPTH);
1584 * first course of business is to grok all the directory
1585 * entries here and store 'em away. Recall we need full pathnames
1588 rv = apr_dir_open(&dirp, path, p);
1589 if (rv != APR_SUCCESS) {
1591 return apr_psprintf(p, "Could not open config directory %s: %s",
1592 path, apr_strerror(rv, errmsg, sizeof errmsg));
1595 candidates = apr_array_make(p, 1, sizeof(fnames));
1596 while (apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp) == APR_SUCCESS) {
1597 /* strip out '.' and '..' */
1598 if (strcmp(dirent.name, ".")
1599 && strcmp(dirent.name, "..")) {
1600 fnew = (fnames *) apr_array_push(candidates);
1601 fnew->fname = ap_make_full_path(p, path, dirent.name);
1605 apr_dir_close(dirp);
1606 if (candidates->nelts != 0) {
1607 qsort((void *) candidates->elts, candidates->nelts,
1608 sizeof(fnames), fname_alphasort);
1611 * Now recurse these... we handle errors and subdirectories
1612 * via the recursion, which is nice
1614 for (current = 0; current < candidates->nelts; ++current) {
1615 fnew = &((fnames *) candidates->elts)[current];
1616 error = process_resource_config_nofnmatch(s, fnew->fname,
1628 /* GCC's initialization extensions are soooo nice here... */
1629 parms = default_parms;
1631 parms.temp_pool = ptemp;
1633 parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1634 parms.override_opts = OPT_ALL | OPT_SYM_OWNER | OPT_MULTI;
1636 rv = ap_pcfg_openfile(&cfp, p, fname);
1637 if (rv != APR_SUCCESS) {
1639 return apr_psprintf(p, "Could not open configuration file %s: %s",
1640 fname, apr_strerror(rv, errmsg, sizeof errmsg));
1643 parms.config_file = cfp;
1644 error = ap_build_config(&parms, p, ptemp, conftree);
1645 ap_cfg_closefile(cfp);
1648 return apr_psprintf(p, "Syntax error on line %d of %s: %s",
1649 parms.err_directive->line_num,
1650 parms.err_directive->filename, error);
1656 AP_DECLARE(const char *) ap_process_resource_config(server_rec *s,
1658 ap_directive_t **conftree,
1662 /* XXX: lstat() won't work on the wildcard pattern...
1665 /* don't require conf/httpd.conf if we have a -C or -c switch */
1666 if ((ap_server_pre_read_config->nelts
1667 || ap_server_post_read_config->nelts)
1668 && !(strcmp(fname, ap_server_root_relative(p, SERVER_CONFIG_FILE)))) {
1671 if (apr_stat(&finfo, fname, APR_FINFO_LINK | APR_FINFO_TYPE, p) != APR_SUCCESS)
1675 if (!apr_fnmatch_test(fname)) {
1676 return process_resource_config_nofnmatch(s, fname, conftree, p, ptemp,
1683 apr_array_header_t *candidates = NULL;
1686 char *path = apr_pstrdup(p, fname), *pattern = NULL;
1688 pattern = ap_strrchr(path, '/');
1690 AP_DEBUG_ASSERT(pattern != NULL); /* path must be absolute. */
1694 if (apr_fnmatch_test(path)) {
1695 return apr_pstrcat(p, "Wildcard patterns not allowed in Include ",
1699 if (!ap_is_directory(p, path)){
1700 return apr_pstrcat(p, "Include directory '", path, "' not found",
1704 if (!apr_fnmatch_test(pattern)) {
1705 return apr_pstrcat(p, "Must include a wildcard pattern for "
1706 "Include ", fname, NULL);
1710 * first course of business is to grok all the directory
1711 * entries here and store 'em away. Recall we need full pathnames
1714 rv = apr_dir_open(&dirp, path, p);
1715 if (rv != APR_SUCCESS) {
1717 return apr_psprintf(p, "Could not open config directory %s: %s",
1718 path, apr_strerror(rv, errmsg, sizeof errmsg));
1721 candidates = apr_array_make(p, 1, sizeof(fnames));
1722 while (apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp) == APR_SUCCESS) {
1723 /* strip out '.' and '..' */
1724 if (strcmp(dirent.name, ".")
1725 && strcmp(dirent.name, "..")
1726 && (apr_fnmatch(pattern, dirent.name,
1727 APR_FNM_PERIOD) == APR_SUCCESS)) {
1728 fnew = (fnames *) apr_array_push(candidates);
1729 fnew->fname = ap_make_full_path(p, path, dirent.name);
1733 apr_dir_close(dirp);
1734 if (candidates->nelts != 0) {
1737 qsort((void *) candidates->elts, candidates->nelts,
1738 sizeof(fnames), fname_alphasort);
1741 * Now recurse these... we handle errors and subdirectories
1742 * via the recursion, which is nice
1744 for (current = 0; current < candidates->nelts; ++current) {
1745 fnew = &((fnames *) candidates->elts)[current];
1746 error = process_resource_config_nofnmatch(s, fnew->fname,
1759 AP_DECLARE(int) ap_process_config_tree(server_rec *s,
1760 ap_directive_t *conftree,
1767 parms = default_parms;
1769 parms.temp_pool = ptemp;
1771 parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1772 parms.override_opts = OPT_ALL | OPT_SYM_OWNER | OPT_MULTI;
1775 errmsg = ap_walk_config(conftree, &parms, s->lookup_defaults);
1777 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0, p,
1778 "Syntax error on line %d of %s:",
1779 parms.err_directive->line_num,
1780 parms.err_directive->filename);
1781 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0, p,
1783 return HTTP_INTERNAL_SERVER_ERROR;
1789 AP_CORE_DECLARE(int) ap_parse_htaccess(ap_conf_vector_t **result,
1790 request_rec *r, int override,
1792 const char *d, const char *access_name)
1794 ap_configfile_t *f = NULL;
1796 char *filename = NULL;
1797 const struct htaccess_result *cache;
1798 struct htaccess_result *new;
1799 ap_conf_vector_t *dc = NULL;
1800 apr_status_t status;
1802 /* firstly, search cache */
1803 for (cache = r->htaccess; cache != NULL; cache = cache->next) {
1804 if (cache->override == override && strcmp(cache->dir, d) == 0) {
1805 *result = cache->htaccess;
1810 parms = default_parms;
1811 parms.override = override;
1812 parms.override_opts = override_opts;
1813 parms.pool = r->pool;
1814 parms.temp_pool = r->pool;
1815 parms.server = r->server;
1816 parms.path = apr_pstrdup(r->pool, d);
1818 /* loop through the access names and find the first one */
1819 while (access_name[0]) {
1820 /* AFAICT; there is no use of the actual 'filename' against
1821 * any canonicalization, so we will simply take the given
1822 * name, ignoring case sensitivity and aliases
1824 filename = ap_make_full_path(r->pool, d,
1825 ap_getword_conf(r->pool, &access_name));
1826 status = ap_pcfg_openfile(&f, r->pool, filename);
1828 if (status == APR_SUCCESS) {
1830 ap_directive_t *temptree = NULL;
1832 dc = ap_create_per_dir_config(r->pool);
1834 parms.config_file = f;
1835 errmsg = ap_build_config(&parms, r->pool, r->pool, &temptree);
1837 errmsg = ap_walk_config(temptree, &parms, dc);
1839 ap_cfg_closefile(f);
1842 ap_log_rerror(APLOG_MARK, APLOG_ALERT, 0, r,
1843 "%s: %s", filename, errmsg);
1844 return HTTP_INTERNAL_SERVER_ERROR;
1851 if (!APR_STATUS_IS_ENOENT(status)
1852 && !APR_STATUS_IS_ENOTDIR(status)) {
1853 ap_log_rerror(APLOG_MARK, APLOG_CRIT, status, r,
1854 "%s pcfg_openfile: unable to check htaccess file, "
1855 "ensure it is readable",
1857 apr_table_setn(r->notes, "error-notes",
1858 "Server unable to read htaccess file, denying "
1859 "access to be safe");
1860 return HTTP_FORBIDDEN;
1866 new = apr_palloc(r->pool, sizeof(struct htaccess_result));
1867 new->dir = parms.path;
1868 new->override = override;
1869 new->override_opts = override_opts;
1872 /* add to head of list */
1873 new->next = r->htaccess;
1879 AP_CORE_DECLARE(const char *) ap_init_virtual_host(apr_pool_t *p,
1880 const char *hostname,
1881 server_rec *main_server,
1884 server_rec *s = (server_rec *) apr_pcalloc(p, sizeof(server_rec));
1886 /* TODO: this crap belongs in http_core */
1887 s->process = main_server->process;
1888 s->server_admin = NULL;
1889 s->server_hostname = NULL;
1890 s->server_scheme = NULL;
1891 s->error_fname = NULL;
1893 s->keep_alive_timeout = 0;
1895 s->keep_alive_max = -1;
1896 s->error_log = main_server->error_log;
1897 s->loglevel = main_server->loglevel;
1898 /* useful default, otherwise we get a port of 0 on redirects */
1899 s->port = main_server->port;
1903 s->names = apr_array_make(p, 4, sizeof(char **));
1904 s->wild_names = apr_array_make(p, 4, sizeof(char **));
1906 s->module_config = create_empty_config(p);
1907 s->lookup_defaults = ap_create_per_dir_config(p);
1909 s->limit_req_line = main_server->limit_req_line;
1910 s->limit_req_fieldsize = main_server->limit_req_fieldsize;
1911 s->limit_req_fields = main_server->limit_req_fields;
1915 return ap_parse_vhost_addrs(p, hostname, s);
1919 AP_DECLARE(void) ap_fixup_virtual_hosts(apr_pool_t *p, server_rec *main_server)
1923 for (virt = main_server->next; virt; virt = virt->next) {
1924 merge_server_configs(p, main_server->module_config,
1925 virt->module_config);
1927 virt->lookup_defaults =
1928 ap_merge_per_dir_configs(p, main_server->lookup_defaults,
1929 virt->lookup_defaults);
1931 if (virt->server_admin == NULL)
1932 virt->server_admin = main_server->server_admin;
1934 if (virt->timeout == 0)
1935 virt->timeout = main_server->timeout;
1937 if (virt->keep_alive_timeout == 0)
1938 virt->keep_alive_timeout = main_server->keep_alive_timeout;
1940 if (virt->keep_alive == -1)
1941 virt->keep_alive = main_server->keep_alive;
1943 if (virt->keep_alive_max == -1)
1944 virt->keep_alive_max = main_server->keep_alive_max;
1946 /* XXX: this is really something that should be dealt with by a
1947 * post-config api phase
1949 ap_core_reorder_directories(p, virt);
1952 ap_core_reorder_directories(p, main_server);
1955 /*****************************************************************
1957 * Getting *everything* configured...
1960 static void init_config_globals(apr_pool_t *p)
1962 /* Global virtual host hash bucket pointers. Init to null. */
1963 ap_init_vhost_config(p);
1966 static server_rec *init_server_config(process_rec *process, apr_pool_t *p)
1969 server_rec *s = (server_rec *) apr_pcalloc(p, sizeof(server_rec));
1971 apr_file_open_stderr(&s->error_log, p);
1972 s->process = process;
1974 s->server_admin = DEFAULT_ADMIN;
1975 s->server_hostname = NULL;
1976 s->server_scheme = NULL;
1977 s->error_fname = DEFAULT_ERRORLOG;
1978 s->loglevel = DEFAULT_LOGLEVEL;
1979 s->limit_req_line = DEFAULT_LIMIT_REQUEST_LINE;
1980 s->limit_req_fieldsize = DEFAULT_LIMIT_REQUEST_FIELDSIZE;
1981 s->limit_req_fields = DEFAULT_LIMIT_REQUEST_FIELDS;
1982 s->timeout = apr_time_from_sec(DEFAULT_TIMEOUT);
1983 s->keep_alive_timeout = apr_time_from_sec(DEFAULT_KEEPALIVE_TIMEOUT);
1984 s->keep_alive_max = DEFAULT_KEEPALIVE;
1987 s->addrs = apr_pcalloc(p, sizeof(server_addr_rec));
1989 /* NOT virtual host; don't match any real network interface */
1990 rv = apr_sockaddr_info_get(&s->addrs->host_addr,
1991 NULL, APR_INET, 0, 0, p);
1992 ap_assert(rv == APR_SUCCESS); /* otherwise: bug or no storage */
1994 s->addrs->host_port = 0; /* matches any port */
1995 s->addrs->virthost = ""; /* must be non-NULL */
1996 s->names = s->wild_names = NULL;
1998 s->module_config = create_server_config(p, s);
1999 s->lookup_defaults = create_default_per_dir_config(p);
2005 AP_DECLARE(server_rec*) ap_read_config(process_rec *process, apr_pool_t *ptemp,
2006 const char *filename,
2007 ap_directive_t **conftree)
2009 const char *confname, *error;
2010 apr_pool_t *p = process->pconf;
2011 server_rec *s = init_server_config(process, p);
2013 init_config_globals(p);
2015 /* All server-wide config files now have the SAME syntax... */
2016 error = process_command_config(s, ap_server_pre_read_config, conftree,
2019 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL, "%s: %s",
2020 ap_server_argv0, error);
2024 /* process_command_config may change the ServerRoot so
2025 * compute this config file name afterwards.
2027 confname = ap_server_root_relative(p, filename);
2030 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
2031 APR_EBADPATH, NULL, "Invalid config file path %s",
2036 error = ap_process_resource_config(s, confname, conftree, p, ptemp);
2038 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL,
2039 "%s: %s", ap_server_argv0, error);
2043 error = process_command_config(s, ap_server_post_read_config, conftree,
2047 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL, "%s: %s",
2048 ap_server_argv0, error);
2055 AP_DECLARE(void) ap_single_module_configure(apr_pool_t *p, server_rec *s,
2058 if (m->create_server_config)
2059 ap_set_module_config(s->module_config, m,
2060 (*m->create_server_config)(p, s));
2062 if (m->create_dir_config)
2063 ap_set_module_config(s->lookup_defaults, m,
2064 (*m->create_dir_config)(p, NULL));
2067 AP_DECLARE(void) ap_run_rewrite_args(process_rec *process)
2071 for (m = ap_top_module; m; m = m->next) {
2072 if (m->rewrite_args) {
2073 (*m->rewrite_args)(process);
2078 /********************************************************************
2079 * Configuration directives are restricted in terms of where they may
2080 * appear in the main configuration files and/or .htaccess files according
2081 * to the bitmask req_override in the command_rec structure.
2082 * If any of the overrides set in req_override are also allowed in the
2083 * context in which the command is read, then the command is allowed.
2084 * The context is determined as follows:
2086 * inside *.conf --> override = (RSRC_CONF|OR_ALL)&~(OR_AUTHCFG|OR_LIMIT);
2087 * within <Directory> or <Location> --> override = OR_ALL|ACCESS_CONF;
2088 * within .htaccess --> override = AllowOverride for current directory;
2090 * the result is, well, a rather confusing set of possibilities for when
2091 * a particular directive is allowed to be used. This procedure prints
2092 * in English where the given (pc) directive can be used.
2094 static void show_overrides(const command_rec *pc, module *pm)
2098 printf("\tAllowed in *.conf ");
2099 if ((pc->req_override & (OR_OPTIONS | OR_FILEINFO | OR_INDEXES))
2100 || ((pc->req_override & RSRC_CONF)
2101 && ((pc->req_override & (ACCESS_CONF | OR_AUTHCFG | OR_LIMIT))))) {
2104 else if (pc->req_override & RSRC_CONF) {
2105 printf("only outside <Directory>, <Files> or <Location>");
2108 printf("only inside <Directory>, <Files> or <Location>");
2111 /* Warn if the directive is allowed inside <Directory> or .htaccess
2112 * but module doesn't support per-dir configuration
2114 if ((pc->req_override & (OR_ALL | ACCESS_CONF)) && !pm->create_dir_config)
2115 printf(" [no per-dir config]");
2117 if (pc->req_override & OR_ALL) {
2118 printf(" and in .htaccess\n\twhen AllowOverride");
2120 if ((pc->req_override & OR_ALL) == OR_ALL) {
2121 printf(" isn't None");
2124 printf(" includes ");
2126 if (pc->req_override & OR_AUTHCFG) {
2130 printf("AuthConfig");
2133 if (pc->req_override & OR_LIMIT) {
2140 if (pc->req_override & OR_OPTIONS) {
2147 if (pc->req_override & OR_FILEINFO) {
2154 if (pc->req_override & OR_INDEXES) {
2166 /* Show the preloaded configuration directives, the help string explaining
2167 * the directive arguments, in what module they are handled, and in
2168 * what parts of the configuration they are allowed. Used for httpd -L.
2170 AP_DECLARE(void) ap_show_directives(void)
2172 const command_rec *pc;
2175 for (n = 0; ap_loaded_modules[n]; ++n) {
2176 for (pc = ap_loaded_modules[n]->cmds; pc && pc->name; ++pc) {
2177 printf("%s (%s)\n", pc->name, ap_loaded_modules[n]->name);
2180 printf("\t%s\n", pc->errmsg);
2182 show_overrides(pc, ap_loaded_modules[n]);
2187 /* Show the preloaded module names. Used for httpd -l. */
2188 AP_DECLARE(void) ap_show_modules(void)
2192 printf("Compiled in modules:\n");
2193 for (n = 0; ap_loaded_modules[n]; ++n)
2194 printf(" %s\n", ap_loaded_modules[n]->name);
2197 AP_DECLARE(void *) ap_retained_data_get(const char *key)
2201 apr_pool_userdata_get((void *)&retained, key, ap_pglobal);
2205 AP_DECLARE(void *) ap_retained_data_create(const char *key, apr_size_t size)
2209 retained = apr_pcalloc(ap_pglobal, size);
2210 apr_pool_userdata_set((const void *)retained, key, apr_pool_cleanup_null, ap_pglobal);