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
44 #include "ap_config.h"
46 #include "http_config.h"
47 #include "http_protocol.h"
48 #include "http_core.h"
49 #include "http_log.h" /* for errors in parse_htaccess */
50 #include "http_request.h" /* for default_handler (see invoke_handler) */
51 #include "http_main.h"
52 #include "http_vhost.h"
53 #include "util_cfgtree.h"
57 AP_DECLARE_DATA const char *ap_server_argv0 = NULL;
59 AP_DECLARE_DATA const char *ap_server_root = NULL;
61 AP_DECLARE_DATA apr_array_header_t *ap_server_pre_read_config = NULL;
62 AP_DECLARE_DATA apr_array_header_t *ap_server_post_read_config = NULL;
63 AP_DECLARE_DATA apr_array_header_t *ap_server_config_defines = NULL;
65 AP_DECLARE_DATA ap_directive_t *ap_conftree = NULL;
68 APR_HOOK_LINK(header_parser)
69 APR_HOOK_LINK(pre_config)
70 APR_HOOK_LINK(check_config)
71 APR_HOOK_LINK(post_config)
72 APR_HOOK_LINK(open_logs)
73 APR_HOOK_LINK(child_init)
74 APR_HOOK_LINK(handler)
75 APR_HOOK_LINK(quick_handler)
76 APR_HOOK_LINK(optional_fn_retrieve)
77 APR_HOOK_LINK(test_config)
80 AP_IMPLEMENT_HOOK_RUN_ALL(int, header_parser,
81 (request_rec *r), (r), OK, DECLINED)
83 AP_IMPLEMENT_HOOK_RUN_ALL(int, pre_config,
84 (apr_pool_t *pconf, apr_pool_t *plog,
86 (pconf, plog, ptemp), OK, DECLINED)
88 AP_IMPLEMENT_HOOK_RUN_ALL(int, check_config,
89 (apr_pool_t *pconf, apr_pool_t *plog,
90 apr_pool_t *ptemp, server_rec *s),
91 (pconf, plog, ptemp, s), OK, DECLINED)
93 AP_IMPLEMENT_HOOK_VOID(test_config,
94 (apr_pool_t *pconf, server_rec *s),
97 AP_IMPLEMENT_HOOK_RUN_ALL(int, post_config,
98 (apr_pool_t *pconf, apr_pool_t *plog,
99 apr_pool_t *ptemp, server_rec *s),
100 (pconf, plog, ptemp, s), OK, DECLINED)
102 /* During the course of debugging I expanded this macro out, so
103 * rather than remove all the useful information there is in the
104 * following lines, I'm going to leave it here in case anyone
105 * else finds it useful.
107 * Ben has looked at it and thinks it correct :)
109 AP_DECLARE(int) ap_hook_post_config(ap_HOOK_post_config_t *pf,
110 const char * const *aszPre,
111 const char * const *aszSucc,
114 ap_LINK_post_config_t *pHook;
116 if (!_hooks.link_post_config) {
117 _hooks.link_post_config = apr_array_make(apr_hook_global_pool, 1,
118 sizeof(ap_LINK_post_config_t));
119 apr_hook_sort_register("post_config", &_hooks.link_post_config);
122 pHook = apr_array_push(_hooks.link_post_config);
124 pHook->aszPredecessors = aszPre;
125 pHook->aszSuccessors = aszSucc;
126 pHook->nOrder = nOrder;
127 pHook->szName = apr_hook_debug_current;
129 if (apr_hook_debug_enabled)
130 apr_hook_debug_show("post_config", aszPre, aszSucc);
133 AP_DECLARE(apr_array_header_t *) ap_hook_get_post_config(void) {
134 return _hooks.link_post_config;
137 AP_DECLARE(int) ap_run_post_config(apr_pool_t *pconf,
142 ap_LINK_post_config_t *pHook;
145 if(!_hooks.link_post_config)
148 pHook = (ap_LINK_post_config_t *)_hooks.link_post_config->elts;
149 for (n = 0; n < _hooks.link_post_config->nelts; ++n)
150 pHook[n].pFunc (pconf, plog, ptemp, s);
154 AP_IMPLEMENT_HOOK_RUN_ALL(int, open_logs,
155 (apr_pool_t *pconf, apr_pool_t *plog,
156 apr_pool_t *ptemp, server_rec *s),
157 (pconf, plog, ptemp, s), OK, DECLINED)
159 AP_IMPLEMENT_HOOK_VOID(child_init,
160 (apr_pool_t *pchild, server_rec *s),
163 AP_IMPLEMENT_HOOK_RUN_FIRST(int, handler, (request_rec *r),
166 AP_IMPLEMENT_HOOK_RUN_FIRST(int, quick_handler, (request_rec *r, int lookup),
167 (r, lookup), DECLINED)
169 AP_IMPLEMENT_HOOK_VOID(optional_fn_retrieve, (void), ())
171 /****************************************************************
173 * We begin with the functions which deal with the linked list
174 * of modules which control just about all of the server operation.
177 /* total_modules is the number of modules that have been linked
180 static int total_modules = 0;
182 /* dynamic_modules is the number of modules that have been added
183 * after the pre-loaded ones have been set up. It shouldn't be larger
184 * than DYNAMIC_MODULE_LIMIT.
186 static int dynamic_modules = 0;
188 AP_DECLARE_DATA module *ap_top_module = NULL;
189 AP_DECLARE_DATA module **ap_loaded_modules=NULL;
191 static apr_hash_t *ap_config_hash = NULL;
193 typedef int (*handler_func)(request_rec *);
194 typedef void *(*dir_maker_func)(apr_pool_t *, char *);
195 typedef void *(*merger_func)(apr_pool_t *, void *, void *);
197 /* maximum nesting level for config directories */
198 #ifndef AP_MAX_INCLUDE_DIR_DEPTH
199 #define AP_MAX_INCLUDE_DIR_DEPTH (128)
202 /* Dealing with config vectors. These are associated with per-directory,
203 * per-server, and per-request configuration, and have a void* pointer for
204 * each modules. The nature of the structure pointed to is private to the
205 * module in question... the core doesn't (and can't) know. However, there
206 * are defined interfaces which allow it to create instances of its private
207 * per-directory and per-server structures, and to merge the per-directory
208 * structures of a directory and its subdirectory (producing a new one in
209 * which the defaults applying to the base directory have been properly
213 static ap_conf_vector_t *create_empty_config(apr_pool_t *p)
215 void *conf_vector = apr_pcalloc(p, sizeof(void *) *
216 (total_modules + DYNAMIC_MODULE_LIMIT));
220 static ap_conf_vector_t *create_default_per_dir_config(apr_pool_t *p)
222 void **conf_vector = apr_pcalloc(p, sizeof(void *) *
223 (total_modules + DYNAMIC_MODULE_LIMIT));
226 for (modp = ap_top_module; modp; modp = modp->next) {
227 dir_maker_func df = modp->create_dir_config;
230 conf_vector[modp->module_index] = (*df)(p, NULL);
233 return (ap_conf_vector_t *)conf_vector;
236 AP_CORE_DECLARE(ap_conf_vector_t *) ap_merge_per_dir_configs(apr_pool_t *p,
237 ap_conf_vector_t *base,
238 ap_conf_vector_t *new_conf)
240 void **conf_vector = apr_palloc(p, sizeof(void *) * total_modules);
241 void **base_vector = (void **)base;
242 void **new_vector = (void **)new_conf;
245 for (modp = ap_top_module; modp; modp = modp->next) {
246 int i = modp->module_index;
248 if (!new_vector[i]) {
249 conf_vector[i] = base_vector[i];
252 merger_func df = modp->merge_dir_config;
253 if (df && base_vector[i]) {
254 conf_vector[i] = (*df)(p, base_vector[i], new_vector[i]);
257 conf_vector[i] = new_vector[i];
261 return (ap_conf_vector_t *)conf_vector;
264 static ap_conf_vector_t *create_server_config(apr_pool_t *p, server_rec *s)
266 void **conf_vector = apr_pcalloc(p, sizeof(void *) *
267 (total_modules + DYNAMIC_MODULE_LIMIT));
270 for (modp = ap_top_module; modp; modp = modp->next) {
271 if (modp->create_server_config)
272 conf_vector[modp->module_index] = (*modp->create_server_config)(p, s);
275 return (ap_conf_vector_t *)conf_vector;
278 static void merge_server_configs(apr_pool_t *p, ap_conf_vector_t *base,
279 ap_conf_vector_t *virt)
281 /* Can reuse the 'virt' vector for the spine of it, since we don't
282 * have to deal with the moral equivalent of .htaccess files here...
285 void **base_vector = (void **)base;
286 void **virt_vector = (void **)virt;
289 for (modp = ap_top_module; modp; modp = modp->next) {
290 merger_func df = modp->merge_server_config;
291 int i = modp->module_index;
294 virt_vector[i] = base_vector[i];
296 virt_vector[i] = (*df)(p, base_vector[i], virt_vector[i]);
300 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_request_config(apr_pool_t *p)
302 return create_empty_config(p);
305 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_conn_config(apr_pool_t *p)
307 return create_empty_config(p);
310 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_per_dir_config(apr_pool_t *p)
312 return create_empty_config(p);
315 static int ap_invoke_filter_init(ap_filter_t *filters)
318 if (filters->frec->filter_init_func) {
319 int result = filters->frec->filter_init_func(filters);
324 filters = filters->next;
329 AP_CORE_DECLARE(int) ap_invoke_handler(request_rec *r)
334 const char *old_handler = r->handler;
338 * The new insert_filter stage makes the most sense here. We only use
339 * it when we are going to run the request, so we must insert filters
340 * if any are available. Since the goal of this phase is to allow all
341 * modules to insert a filter if they want to, this filter returns
342 * void. I just can't see any way that this filter can reasonably
343 * fail, either your modules inserts something or it doesn't. rbb
345 ap_run_insert_filter(r);
347 /* Before continuing, allow each filter that is in the two chains to
348 * run their init function to let them do any magic before we could
349 * start generating data.
351 result = ap_invoke_filter_init(r->input_filters);
355 result = ap_invoke_filter_init(r->output_filters);
361 handler = r->content_type ? r->content_type : ap_default_type(r);
362 if ((p=ap_strchr_c(handler, ';')) != NULL) {
363 char *new_handler = (char *)apr_pmemdup(r->pool, handler,
365 char *p2 = new_handler + (p - handler);
366 handler = new_handler;
368 /* MIME type arguments */
369 while (p2 > handler && p2[-1] == ' ')
370 --p2; /* strip trailing spaces */
375 r->handler = handler;
378 result = ap_run_handler(r);
380 r->handler = old_handler;
382 if (result == DECLINED && r->handler && r->filename) {
383 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
384 "handler \"%s\" not found for: %s", r->handler, r->filename);
386 if ((result != OK) && (result != DONE) && (result != DECLINED)
387 && !ap_is_HTTP_VALID_RESPONSE(result)) {
388 /* If a module is deliberately returning something else
389 * (request_rec in non-HTTP or proprietary extension?)
390 * let it set a note to allow it explicitly.
391 * Otherwise, a return code that is neither reserved nor HTTP
392 * is a bug, as in PR#31759.
394 ignore = apr_table_get(r->notes, "HTTP_IGNORE_RANGE");
396 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
397 "Handler for %s returned invalid result code %d",
399 result = HTTP_INTERNAL_SERVER_ERROR;
403 return result == DECLINED ? HTTP_INTERNAL_SERVER_ERROR : result;
406 AP_DECLARE(int) ap_method_is_limited(cmd_parms *cmd, const char *method)
410 methnum = ap_method_number_of(method);
413 * A method number either hardcoded into apache or
414 * added by a module and registered.
416 if (methnum != M_INVALID) {
417 return (cmd->limited & (AP_METHOD_BIT << methnum)) ? 1 : 0;
420 return 0; /* not found */
423 AP_DECLARE(void) ap_register_hooks(module *m, apr_pool_t *p)
425 if (m->register_hooks) {
426 if (getenv("SHOW_HOOKS")) {
427 printf("Registering hooks for %s\n", m->name);
428 apr_hook_debug_enabled = 1;
431 apr_hook_debug_current = m->name;
432 m->register_hooks(p);
436 static void ap_add_module_commands(module *m, apr_pool_t *p);
438 typedef struct ap_mod_list_struct ap_mod_list;
439 struct ap_mod_list_struct {
440 struct ap_mod_list_struct *next;
442 const command_rec *cmd;
445 static apr_status_t reload_conf_hash(void *baton)
447 ap_config_hash = NULL;
451 static void rebuild_conf_hash(apr_pool_t *p, int add_prelinked)
455 ap_config_hash = apr_hash_make(p);
457 apr_pool_cleanup_register(p, NULL, reload_conf_hash,
458 apr_pool_cleanup_null);
460 for (m = ap_prelinked_modules; *m != NULL; m++) {
461 ap_add_module_commands(*m, p);
466 static void ap_add_module_commands(module *m, apr_pool_t *p)
470 const command_rec *cmd;
475 if (ap_config_hash == NULL) {
476 rebuild_conf_hash(p, 0);
479 tpool = apr_hash_pool_get(ap_config_hash);
481 while (cmd && cmd->name) {
482 mln = apr_palloc(tpool, sizeof(ap_mod_list));
485 dir = apr_pstrdup(tpool, cmd->name);
489 mln->next = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
490 apr_hash_set(ap_config_hash, dir, APR_HASH_KEY_STRING, mln);
496 /* One-time setup for precompiled modules --- NOT to be done on restart */
498 AP_DECLARE(const char *) ap_add_module(module *m, apr_pool_t *p)
500 /* This could be called from a LoadModule httpd.conf command,
501 * after the file has been linked and the module structure within it
505 if (m->version != MODULE_MAGIC_NUMBER_MAJOR) {
506 return apr_psprintf(p, "Module \"%s\" is not compatible with this "
507 "version of Apache (found %d, need %d). Please "
508 "contact the vendor for the correct version.",
509 m->name, m->version, MODULE_MAGIC_NUMBER_MAJOR);
512 if (m->next == NULL) {
513 m->next = ap_top_module;
517 if (m->module_index == -1) {
518 m->module_index = total_modules++;
521 if (dynamic_modules > DYNAMIC_MODULE_LIMIT) {
522 return apr_psprintf(p, "Module \"%s\" could not be loaded, "
523 "because the dynamic module limit was "
524 "reached. Please increase "
525 "DYNAMIC_MODULE_LIMIT and recompile.", m->name);
529 /* Some C compilers put a complete path into __FILE__, but we want
530 * only the filename (e.g. mod_includes.c). So check for path
531 * components (Unix and DOS), and remove them.
534 if (ap_strrchr_c(m->name, '/'))
535 m->name = 1 + ap_strrchr_c(m->name, '/');
537 if (ap_strrchr_c(m->name, '\\'))
538 m->name = 1 + ap_strrchr_c(m->name, '\\');
542 * "*POSIX(/home/martin/apache/src/modules/standard/mod_info.c)"
545 /* We cannot fix the string in-place, because it's const */
546 if (m->name[strlen(m->name)-1] == ')') {
547 char *tmp = strdup(m->name); /* FIXME: memory leak, albeit a small one */
548 tmp[strlen(tmp)-1] = '\0';
551 #endif /*_OSD_POSIX*/
553 ap_add_module_commands(m, p);
554 /* FIXME: is this the right place to call this?
555 * It doesn't appear to be
557 ap_register_hooks(m, p);
563 * remove_module undoes what add_module did. There are some caveats:
564 * when the module is removed, its slot is lost so all the current
565 * per-dir and per-server configurations are invalid. So we should
566 * only ever call this function when you are invalidating almost
567 * all our current data. I.e. when doing a restart.
570 AP_DECLARE(void) ap_remove_module(module *m)
574 modp = ap_top_module;
576 /* We are the top module, special case */
577 ap_top_module = modp->next;
581 /* Not the top module, find use. When found modp will
582 * point to the module _before_ us in the list
585 while (modp && modp->next != m) {
590 /* Uh-oh, this module doesn't exist */
591 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
592 "Cannot remove module %s: not found in module list",
597 /* Eliminate us from the module list */
598 modp->next = modp->next->next;
601 m->module_index = -1; /* simulate being unloaded, should
607 AP_DECLARE(const char *) ap_add_loaded_module(module *mod, apr_pool_t *p)
613 * Add module pointer to top of chained module list
615 error = ap_add_module(mod, p);
621 * And module pointer to list of loaded modules
623 * Notes: 1. ap_add_module() would already complain if no more space
624 * exists for adding a dynamically loaded module
625 * 2. ap_add_module() accepts double inclusion, so we have
626 * to accept this, too.
628 for (m = ap_loaded_modules; *m != NULL; m++)
636 AP_DECLARE(void) ap_remove_loaded_module(module *mod)
643 * Remove module pointer from chained module list
645 ap_remove_module(mod);
648 * Remove module pointer from list of loaded modules
650 * Note: 1. We cannot determine if the module was successfully
651 * removed by ap_remove_module().
652 * 2. We have not to complain explicity when the module
653 * is not found because ap_remove_module() did it
656 for (m = m2 = ap_loaded_modules, done = 0; *m2 != NULL; m2++) {
657 if (*m2 == mod && done == 0)
666 AP_DECLARE(const char *) ap_setup_prelinked_modules(process_rec *process)
672 apr_hook_global_pool=process->pconf;
674 rebuild_conf_hash(process->pconf, 0);
677 * Initialise total_modules variable and module indices
680 for (m = ap_preloaded_modules; *m != NULL; m++)
681 (*m)->module_index = total_modules++;
684 * Initialise list of loaded modules
686 ap_loaded_modules = (module **)apr_palloc(process->pool,
687 sizeof(module *) * (total_modules + DYNAMIC_MODULE_LIMIT + 1));
689 if (ap_loaded_modules == NULL) {
690 return "Ouch! Out of memory in ap_setup_prelinked_modules()!";
693 for (m = ap_preloaded_modules, m2 = ap_loaded_modules; *m != NULL; )
699 * Initialize chain of linked (=activate) modules
701 for (m = ap_prelinked_modules; *m != NULL; m++) {
702 error = ap_add_module(*m, process->pconf);
713 AP_DECLARE(const char *) ap_find_module_name(module *m)
718 AP_DECLARE(module *) ap_find_linked_module(const char *name)
722 for (modp = ap_top_module; modp; modp = modp->next) {
723 if (strcmp(modp->name, name) == 0)
730 /*****************************************************************
732 * Resource, access, and .htaccess config files now parsed by a common
735 * Let's begin with the basics; parsing the line and
736 * invoking the function...
739 #define AP_MAX_ARGC 64
741 static const char *invoke_cmd(const command_rec *cmd, cmd_parms *parms,
742 void *mconfig, const char *args)
745 const char *errmsg = NULL;
747 if ((parms->override & cmd->req_override) == 0)
748 return apr_pstrcat(parms->pool, cmd->name, " not allowed here", NULL);
750 parms->info = cmd->cmd_data;
753 switch (cmd->args_how) {
755 #ifdef RESOLVE_ENV_PER_TOKEN
756 args = ap_resolve_env(parms->pool,args);
758 return cmd->AP_RAW_ARGS(parms, mconfig, args);
762 char *argv[AP_MAX_ARGC];
766 w = ap_getword_conf(parms->pool, &args);
767 if (*w == '\0' && *args == '\0') {
772 } while (argc < AP_MAX_ARGC && *args != '\0');
774 return cmd->AP_TAKE_ARGV(parms, mconfig, argc, argv);
779 return apr_pstrcat(parms->pool, cmd->name, " takes no arguments",
782 return cmd->AP_NO_ARGS(parms, mconfig);
785 w = ap_getword_conf(parms->pool, &args);
787 if (*w == '\0' || *args != 0)
788 return apr_pstrcat(parms->pool, cmd->name, " takes one argument",
789 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
791 return cmd->AP_TAKE1(parms, mconfig, w);
794 w = ap_getword_conf(parms->pool, &args);
795 w2 = ap_getword_conf(parms->pool, &args);
797 if (*w == '\0' || *w2 == '\0' || *args != 0)
798 return apr_pstrcat(parms->pool, cmd->name, " takes two arguments",
799 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
801 return cmd->AP_TAKE2(parms, mconfig, w, w2);
804 w = ap_getword_conf(parms->pool, &args);
805 w2 = ap_getword_conf(parms->pool, &args);
807 if (*w == '\0' || *args != 0)
808 return apr_pstrcat(parms->pool, cmd->name, " takes 1-2 arguments",
809 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
811 return cmd->AP_TAKE2(parms, mconfig, w, *w2 ? w2 : NULL);
814 w = ap_getword_conf(parms->pool, &args);
815 w2 = ap_getword_conf(parms->pool, &args);
816 w3 = ap_getword_conf(parms->pool, &args);
818 if (*w == '\0' || *w2 == '\0' || *w3 == '\0' || *args != 0)
819 return apr_pstrcat(parms->pool, cmd->name, " takes three arguments",
820 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
822 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
825 w = ap_getword_conf(parms->pool, &args);
826 w2 = ap_getword_conf(parms->pool, &args);
827 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
829 if (*w == '\0' || *w2 == '\0' || *args != 0)
830 return apr_pstrcat(parms->pool, cmd->name,
831 " takes two or three arguments",
832 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
834 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
837 w = ap_getword_conf(parms->pool, &args);
838 w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
839 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
841 if (*w == '\0' || *args != 0)
842 return apr_pstrcat(parms->pool, cmd->name,
843 " takes one, two or three arguments",
844 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
846 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
849 w = ap_getword_conf(parms->pool, &args);
850 w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
851 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
853 if (*w == '\0' || (w2 && *w2 && !w3) || *args != 0)
854 return apr_pstrcat(parms->pool, cmd->name,
855 " takes one or three arguments",
856 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
858 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
861 while (*(w = ap_getword_conf(parms->pool, &args)) != '\0') {
863 errmsg = cmd->AP_TAKE1(parms, mconfig, w);
865 if (errmsg && strcmp(errmsg, DECLINE_CMD) != 0)
872 w = ap_getword_conf(parms->pool, &args);
874 if (*w == '\0' || *args == 0)
875 return apr_pstrcat(parms->pool, cmd->name,
876 " requires at least two arguments",
877 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
879 while (*(w2 = ap_getword_conf(parms->pool, &args)) != '\0') {
881 errmsg = cmd->AP_TAKE2(parms, mconfig, w, w2);
883 if (errmsg && strcmp(errmsg, DECLINE_CMD) != 0)
890 w = ap_getword_conf(parms->pool, &args);
892 if (*w == '\0' || (strcasecmp(w, "on") && strcasecmp(w, "off")))
893 return apr_pstrcat(parms->pool, cmd->name, " must be On or Off",
896 return cmd->AP_FLAG(parms, mconfig, strcasecmp(w, "off") != 0);
899 return apr_pstrcat(parms->pool, cmd->name,
900 " is improperly configured internally (server bug)",
905 AP_CORE_DECLARE(const command_rec *) ap_find_command(const char *name,
906 const command_rec *cmds)
909 if (!strcasecmp(name, cmds->name))
918 AP_CORE_DECLARE(const command_rec *) ap_find_command_in_modules(
919 const char *cmd_name, module **mod)
921 const command_rec *cmdp;
924 for (modp = *mod; modp; modp = modp->next) {
925 if (modp->cmds && (cmdp = ap_find_command(cmd_name, modp->cmds))) {
934 AP_CORE_DECLARE(void *) ap_set_config_vectors(server_rec *server,
935 ap_conf_vector_t *section_vector,
937 module *mod, apr_pool_t *pconf)
939 void *section_config = ap_get_module_config(section_vector, mod);
940 void *server_config = ap_get_module_config(server->module_config, mod);
942 if (!section_config && mod->create_dir_config) {
943 /* ### need to fix the create_dir_config functions' prototype... */
944 section_config = (*mod->create_dir_config)(pconf, (char *)section);
945 ap_set_module_config(section_vector, mod, section_config);
948 if (!server_config && mod->create_server_config) {
949 server_config = (*mod->create_server_config)(pconf, server);
950 ap_set_module_config(server->module_config, mod, server_config);
953 return section_config;
956 static const char *execute_now(char *cmd_line, const char *args,
958 apr_pool_t *p, apr_pool_t *ptemp,
959 ap_directive_t **sub_tree,
960 ap_directive_t *parent);
962 static const char *ap_build_config_sub(apr_pool_t *p, apr_pool_t *temp_pool,
963 const char *l, cmd_parms *parms,
964 ap_directive_t **current,
965 ap_directive_t **curr_parent,
966 ap_directive_t **conftree)
968 const char *retval = NULL;
971 ap_directive_t *newdir;
972 module *mod = ap_top_module;
973 const command_rec *cmd;
975 if (*l == '#' || *l == '\0')
978 #if RESOLVE_ENV_PER_TOKEN
981 args = ap_resolve_env(temp_pool, l);
984 cmd_name = ap_getword_conf(p, &args);
985 if (*cmd_name == '\0') {
986 /* Note: this branch should not occur. An empty line should have
987 * triggered the exit further above.
992 if (cmd_name[1] != '/') {
993 char *lastc = cmd_name + strlen(cmd_name) - 1;
997 if (cmd_name[0] == '<' && *args == '\0') {
1002 newdir = apr_pcalloc(p, sizeof(ap_directive_t));
1003 newdir->filename = parms->config_file->name;
1004 newdir->line_num = parms->config_file->line_number;
1005 newdir->directive = cmd_name;
1006 newdir->args = apr_pstrdup(p, args);
1008 if ((cmd = ap_find_command_in_modules(cmd_name, &mod)) != NULL) {
1009 if (cmd->req_override & EXEC_ON_READ) {
1010 ap_directive_t *sub_tree = NULL;
1012 parms->err_directive = newdir;
1013 retval = execute_now(cmd_name, args, parms, p, temp_pool,
1014 &sub_tree, *curr_parent);
1016 (*current)->next = sub_tree;
1019 *current = sub_tree;
1021 (*curr_parent)->first_child = (*current);
1024 (*current)->parent = (*curr_parent);
1029 /* Before walking *current to the end of the list,
1030 * set the head to *current.
1032 *conftree = *current;
1034 while ((*current)->next != NULL) {
1035 (*current) = (*current)->next;
1036 (*current)->parent = (*curr_parent);
1043 if (cmd_name[0] == '<') {
1044 if (cmd_name[1] != '/') {
1045 (*current) = ap_add_node(curr_parent, *current, newdir, 1);
1047 else if (*curr_parent == NULL) {
1048 parms->err_directive = newdir;
1049 return apr_pstrcat(p, cmd_name,
1050 " without matching <", cmd_name + 2,
1054 char *bracket = cmd_name + strlen(cmd_name) - 1;
1056 if (*bracket != '>') {
1057 parms->err_directive = newdir;
1058 return apr_pstrcat(p, cmd_name,
1059 "> directive missing closing '>'", NULL);
1064 if (strcasecmp(cmd_name + 2,
1065 (*curr_parent)->directive + 1) != 0) {
1066 parms->err_directive = newdir;
1067 return apr_pstrcat(p, "Expected </",
1068 (*curr_parent)->directive + 1, "> but saw ",
1069 cmd_name, ">", NULL);
1074 /* done with this section; move up a level */
1075 *current = *curr_parent;
1076 *curr_parent = (*current)->parent;
1080 *current = ap_add_node(curr_parent, *current, newdir, 0);
1086 AP_DECLARE(const char *) ap_build_cont_config(apr_pool_t *p,
1087 apr_pool_t *temp_pool,
1089 ap_directive_t **current,
1090 ap_directive_t **curr_parent,
1091 char *orig_directive)
1096 ap_directive_t *sub_tree = NULL;
1098 /* Since this function can be called recursively, allocate
1099 * the temporary 8k string buffer from the temp_pool rather
1100 * than the stack to avoid over-running a fixed length stack.
1102 l = apr_palloc(temp_pool, MAX_STRING_LEN);
1104 bracket = apr_pstrcat(p, orig_directive + 1, ">", NULL);
1105 while (!(ap_cfg_getline(l, MAX_STRING_LEN, parms->config_file))) {
1106 if (!memcmp(l, "</", 2)
1107 && (strcasecmp(l + 2, bracket) == 0)
1108 && (*curr_parent == NULL)) {
1111 retval = ap_build_config_sub(p, temp_pool, l, parms, current,
1112 curr_parent, &sub_tree);
1116 if (sub_tree == NULL && curr_parent != NULL) {
1117 sub_tree = *curr_parent;
1120 if (sub_tree == NULL && current != NULL) {
1121 sub_tree = *current;
1125 *current = sub_tree;
1129 static const char *ap_walk_config_sub(const ap_directive_t *current,
1131 ap_conf_vector_t *section_vector)
1133 const command_rec *cmd;
1135 char *dir = apr_pstrdup(parms->pool, current->directive);
1137 ap_str_tolower(dir);
1139 ml = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
1142 parms->err_directive = current;
1143 return apr_pstrcat(parms->pool, "Invalid command '",
1145 "', perhaps misspelled or defined by a module "
1146 "not included in the server configuration",
1150 for ( ; ml != NULL; ml = ml->next) {
1151 void *dir_config = ap_set_config_vectors(parms->server,
1159 /* Once was enough? */
1160 if (cmd->req_override & EXEC_ON_READ) {
1164 retval = invoke_cmd(cmd, parms, dir_config, current->args);
1166 if (retval != NULL && strcmp(retval, DECLINE_CMD) != 0) {
1167 /* If the directive in error has already been set, don't
1168 * replace it. Otherwise, an error inside a container
1169 * will be reported as occuring on the first line of the
1172 if (!parms->err_directive) {
1173 parms->err_directive = current;
1182 AP_DECLARE(const char *) ap_walk_config(ap_directive_t *current,
1184 ap_conf_vector_t *section_vector)
1186 ap_conf_vector_t *oldconfig = parms->context;
1188 parms->context = section_vector;
1190 /* scan through all directives, executing each one */
1191 for (; current != NULL; current = current->next) {
1194 parms->directive = current;
1196 /* actually parse the command and execute the correct function */
1197 errmsg = ap_walk_config_sub(current, parms, section_vector);
1198 if (errmsg != NULL) {
1199 /* restore the context (just in case) */
1200 parms->context = oldconfig;
1205 parms->context = oldconfig;
1209 AP_DECLARE(const char *) ap_build_config(cmd_parms *parms,
1210 apr_pool_t *p, apr_pool_t *temp_pool,
1211 ap_directive_t **conftree)
1213 ap_directive_t *current = *conftree;
1214 ap_directive_t *curr_parent = NULL;
1215 char *l = apr_palloc (temp_pool, MAX_STRING_LEN);
1218 if (current != NULL) {
1219 while (current->next) {
1220 current = current->next;
1224 while (!(ap_cfg_getline(l, MAX_STRING_LEN, parms->config_file))) {
1225 errmsg = ap_build_config_sub(p, temp_pool, l, parms,
1226 ¤t, &curr_parent, conftree);
1230 if (*conftree == NULL && curr_parent != NULL) {
1231 *conftree = curr_parent;
1234 if (*conftree == NULL && current != NULL) {
1235 *conftree = current;
1239 if (curr_parent != NULL) {
1242 while (curr_parent != NULL) {
1243 errmsg = apr_psprintf(p, "%s%s%s:%u: %s> was not closed.",
1245 *errmsg == '\0' ? "" : APR_EOL_STR,
1246 curr_parent->filename,
1247 curr_parent->line_num,
1248 curr_parent->directive);
1250 parms->err_directive = curr_parent;
1251 curr_parent = curr_parent->parent;
1261 * Generic command functions...
1264 AP_DECLARE_NONSTD(const char *) ap_set_string_slot(cmd_parms *cmd,
1268 int offset = (int)(long)cmd->info;
1270 *(const char **)((char *)struct_ptr + offset) = arg;
1275 AP_DECLARE_NONSTD(const char *) ap_set_int_slot(cmd_parms *cmd,
1280 char *error_str = NULL;
1281 int offset = (int)(long)cmd->info;
1283 *(int *)((char*)struct_ptr + offset) = strtol(arg, &endptr, 10);
1285 if ((*arg == '\0') || (*endptr != '\0')) {
1286 error_str = apr_psprintf(cmd->pool,
1287 "Invalid value for directive %s, expected integer",
1288 cmd->directive->directive);
1294 AP_DECLARE_NONSTD(const char *) ap_set_string_slot_lower(cmd_parms *cmd,
1298 char *arg = apr_pstrdup(cmd->pool,arg_);
1299 int offset = (int)(long)cmd->info;
1301 ap_str_tolower(arg);
1302 *(char **)((char *)struct_ptr + offset) = arg;
1307 AP_DECLARE_NONSTD(const char *) ap_set_flag_slot(cmd_parms *cmd,
1308 void *struct_ptr_v, int arg)
1310 int offset = (int)(long)cmd->info;
1311 char *struct_ptr = (char *)struct_ptr_v;
1313 *(int *)(struct_ptr + offset) = arg ? 1 : 0;
1318 AP_DECLARE_NONSTD(const char *) ap_set_file_slot(cmd_parms *cmd, void *struct_ptr,
1321 /* Prepend server_root to relative arg.
1322 * This allows most args to be independent of server_root,
1323 * so the server can be moved or mirrored with less pain.
1326 int offset = (int)(long)cmd->info;
1328 path = ap_server_root_relative(cmd->pool, arg);
1331 return apr_pstrcat(cmd->pool, "Invalid file path ",
1335 *(const char **) ((char*)struct_ptr + offset) = path;
1340 AP_DECLARE_NONSTD(const char *) ap_set_deprecated(cmd_parms *cmd,
1344 return cmd->cmd->errmsg;
1347 /*****************************************************************
1349 * Reading whole config files...
1352 static cmd_parms default_parms =
1353 {NULL, 0, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
1355 AP_DECLARE(char *) ap_server_root_relative(apr_pool_t *p, const char *file)
1357 char *newpath = NULL;
1359 rv = apr_filepath_merge(&newpath, ap_server_root, file,
1360 APR_FILEPATH_TRUENAME, p);
1361 if (newpath && (rv == APR_SUCCESS || APR_STATUS_IS_EPATHWILD(rv)
1362 || APR_STATUS_IS_ENOENT(rv)
1363 || APR_STATUS_IS_ENOTDIR(rv))) {
1371 AP_DECLARE(const char *) ap_soak_end_container(cmd_parms *cmd, char *directive)
1373 char l[MAX_STRING_LEN];
1377 while(!(ap_cfg_getline(l, MAX_STRING_LEN, cmd->config_file))) {
1378 #if RESOLVE_ENV_PER_TOKEN
1381 args = ap_resolve_env(cmd->temp_pool, l);
1384 cmd_name = ap_getword_conf(cmd->pool, &args);
1385 if (cmd_name[0] == '<') {
1386 if (cmd_name[1] == '/') {
1387 cmd_name[strlen(cmd_name) - 1] = '\0';
1389 if (strcasecmp(cmd_name + 2, directive + 1) != 0) {
1390 return apr_pstrcat(cmd->pool, "Expected </",
1391 directive + 1, "> but saw ",
1392 cmd_name, ">", NULL);
1395 return NULL; /* found end of container */
1400 if (*args == '\0' && cmd_name[strlen(cmd_name) - 1] == '>') {
1401 cmd_name[strlen(cmd_name) - 1] = '\0';
1404 if ((msg = ap_soak_end_container(cmd, cmd_name)) != NULL) {
1411 return apr_pstrcat(cmd->pool, "Expected </",
1412 directive + 1, "> before end of configuration",
1416 static const char *execute_now(char *cmd_line, const char *args,
1418 apr_pool_t *p, apr_pool_t *ptemp,
1419 ap_directive_t **sub_tree,
1420 ap_directive_t *parent)
1422 const command_rec *cmd;
1424 char *dir = apr_pstrdup(parms->pool, cmd_line);
1426 ap_str_tolower(dir);
1428 ml = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
1431 return apr_pstrcat(parms->pool, "Invalid command '",
1433 "', perhaps misspelled or defined by a module "
1434 "not included in the server configuration",
1438 for ( ; ml != NULL; ml = ml->next) {
1442 retval = invoke_cmd(cmd, parms, sub_tree, args);
1444 if (retval != NULL) {
1452 /* This structure and the following functions are needed for the
1453 * table-based config file reading. They are passed to the
1454 * cfg_open_custom() routine.
1457 /* Structure to be passed to cfg_open_custom(): it contains an
1458 * index which is incremented from 0 to nelts on each call to
1459 * cfg_getline() (which in turn calls arr_elts_getstr())
1460 * and an apr_array_header_t pointer for the string array.
1463 apr_array_header_t *array;
1468 /* arr_elts_getstr() returns the next line from the string array. */
1469 static void *arr_elts_getstr(void *buf, size_t bufsiz, void *param)
1471 arr_elts_param_t *arr_param = (arr_elts_param_t *)param;
1473 /* End of array reached? */
1474 if (++arr_param->curr_idx > arr_param->array->nelts)
1477 /* return the line */
1479 ((char **)arr_param->array->elts)[arr_param->curr_idx - 1],
1486 /* arr_elts_close(): dummy close routine (makes sure no more lines can be read) */
1487 static int arr_elts_close(void *param)
1489 arr_elts_param_t *arr_param = (arr_elts_param_t *)param;
1491 arr_param->curr_idx = arr_param->array->nelts;
1496 static const char *process_command_config(server_rec *s,
1497 apr_array_header_t *arr,
1498 ap_directive_t **conftree,
1504 arr_elts_param_t arr_parms;
1506 arr_parms.curr_idx = 0;
1507 arr_parms.array = arr;
1509 if (ap_config_hash == NULL) {
1510 rebuild_conf_hash(s->process->pconf, 1);
1513 parms = default_parms;
1515 parms.temp_pool = ptemp;
1517 parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1518 parms.override_opts = OPT_ALL | OPT_INCNOEXEC | OPT_SYM_OWNER | OPT_MULTI;
1520 parms.config_file = ap_pcfg_open_custom(p, "-c/-C directives",
1522 arr_elts_getstr, arr_elts_close);
1524 errmsg = ap_build_config(&parms, p, ptemp, conftree);
1525 ap_cfg_closefile(parms.config_file);
1528 return apr_pstrcat(p, "Syntax error in -C/-c directive: ", errmsg,
1539 static int fname_alphasort(const void *fn1, const void *fn2)
1541 const fnames *f1 = fn1;
1542 const fnames *f2 = fn2;
1544 return strcmp(f1->fname,f2->fname);
1547 static const char *process_resource_config_nofnmatch(server_rec *s,
1549 ap_directive_t **conftree,
1555 ap_configfile_t *cfp;
1559 if (ap_is_directory(p, fname)) {
1563 apr_array_header_t *candidates = NULL;
1565 char *path = apr_pstrdup(p, fname);
1567 if (++depth > AP_MAX_INCLUDE_DIR_DEPTH) {
1568 return apr_psprintf(p, "Directory %s exceeds the maximum include "
1569 "directory nesting level of %u. You have "
1570 "probably a recursion somewhere.", path,
1571 AP_MAX_INCLUDE_DIR_DEPTH);
1575 * first course of business is to grok all the directory
1576 * entries here and store 'em away. Recall we need full pathnames
1579 rv = apr_dir_open(&dirp, path, p);
1580 if (rv != APR_SUCCESS) {
1582 return apr_psprintf(p, "Could not open config directory %s: %s",
1583 path, apr_strerror(rv, errmsg, sizeof errmsg));
1586 candidates = apr_array_make(p, 1, sizeof(fnames));
1587 while (apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp) == APR_SUCCESS) {
1588 /* strip out '.' and '..' */
1589 if (strcmp(dirent.name, ".")
1590 && strcmp(dirent.name, "..")) {
1591 fnew = (fnames *) apr_array_push(candidates);
1592 fnew->fname = ap_make_full_path(p, path, dirent.name);
1596 apr_dir_close(dirp);
1597 if (candidates->nelts != 0) {
1598 qsort((void *) candidates->elts, candidates->nelts,
1599 sizeof(fnames), fname_alphasort);
1602 * Now recurse these... we handle errors and subdirectories
1603 * via the recursion, which is nice
1605 for (current = 0; current < candidates->nelts; ++current) {
1606 fnew = &((fnames *) candidates->elts)[current];
1607 error = process_resource_config_nofnmatch(s, fnew->fname,
1619 /* GCC's initialization extensions are soooo nice here... */
1620 parms = default_parms;
1622 parms.temp_pool = ptemp;
1624 parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1625 parms.override_opts = OPT_ALL | OPT_INCNOEXEC | OPT_SYM_OWNER | OPT_MULTI;
1627 rv = ap_pcfg_openfile(&cfp, p, fname);
1628 if (rv != APR_SUCCESS) {
1630 return apr_psprintf(p, "Could not open configuration file %s: %s",
1631 fname, apr_strerror(rv, errmsg, sizeof errmsg));
1634 parms.config_file = cfp;
1635 error = ap_build_config(&parms, p, ptemp, conftree);
1636 ap_cfg_closefile(cfp);
1639 return apr_psprintf(p, "Syntax error on line %d of %s: %s",
1640 parms.err_directive->line_num,
1641 parms.err_directive->filename, error);
1647 AP_DECLARE(const char *) ap_process_resource_config(server_rec *s,
1649 ap_directive_t **conftree,
1653 /* XXX: lstat() won't work on the wildcard pattern...
1656 /* don't require conf/httpd.conf if we have a -C or -c switch */
1657 if ((ap_server_pre_read_config->nelts
1658 || ap_server_post_read_config->nelts)
1659 && !(strcmp(fname, ap_server_root_relative(p, SERVER_CONFIG_FILE)))) {
1662 if (apr_stat(&finfo, fname, APR_FINFO_LINK | APR_FINFO_TYPE, p) != APR_SUCCESS)
1666 if (!apr_fnmatch_test(fname)) {
1667 return process_resource_config_nofnmatch(s, fname, conftree, p, ptemp,
1674 apr_array_header_t *candidates = NULL;
1677 char *path = apr_pstrdup(p, fname), *pattern = NULL;
1679 pattern = ap_strrchr(path, '/');
1681 AP_DEBUG_ASSERT(pattern != NULL); /* path must be absolute. */
1685 if (apr_fnmatch_test(path)) {
1686 return apr_pstrcat(p, "Wildcard patterns not allowed in Include ",
1690 if (!ap_is_directory(p, path)){
1691 return apr_pstrcat(p, "Include directory '", path, "' not found",
1695 if (!apr_fnmatch_test(pattern)) {
1696 return apr_pstrcat(p, "Must include a wildcard pattern for "
1697 "Include ", fname, NULL);
1701 * first course of business is to grok all the directory
1702 * entries here and store 'em away. Recall we need full pathnames
1705 rv = apr_dir_open(&dirp, path, p);
1706 if (rv != APR_SUCCESS) {
1708 return apr_psprintf(p, "Could not open config directory %s: %s",
1709 path, apr_strerror(rv, errmsg, sizeof errmsg));
1712 candidates = apr_array_make(p, 1, sizeof(fnames));
1713 while (apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp) == APR_SUCCESS) {
1714 /* strip out '.' and '..' */
1715 if (strcmp(dirent.name, ".")
1716 && strcmp(dirent.name, "..")
1717 && (apr_fnmatch(pattern, dirent.name,
1718 APR_FNM_PERIOD) == APR_SUCCESS)) {
1719 fnew = (fnames *) apr_array_push(candidates);
1720 fnew->fname = ap_make_full_path(p, path, dirent.name);
1724 apr_dir_close(dirp);
1725 if (candidates->nelts != 0) {
1728 qsort((void *) candidates->elts, candidates->nelts,
1729 sizeof(fnames), fname_alphasort);
1732 * Now recurse these... we handle errors and subdirectories
1733 * via the recursion, which is nice
1735 for (current = 0; current < candidates->nelts; ++current) {
1736 fnew = &((fnames *) candidates->elts)[current];
1737 error = process_resource_config_nofnmatch(s, fnew->fname,
1750 AP_DECLARE(int) ap_process_config_tree(server_rec *s,
1751 ap_directive_t *conftree,
1758 parms = default_parms;
1760 parms.temp_pool = ptemp;
1762 parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1763 parms.override_opts = OPT_ALL | OPT_INCNOEXEC | OPT_SYM_OWNER | OPT_MULTI;
1766 errmsg = ap_walk_config(conftree, &parms, s->lookup_defaults);
1768 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0, p,
1769 "Syntax error on line %d of %s:",
1770 parms.err_directive->line_num,
1771 parms.err_directive->filename);
1772 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0, p,
1774 return HTTP_INTERNAL_SERVER_ERROR;
1780 AP_CORE_DECLARE(int) ap_parse_htaccess(ap_conf_vector_t **result,
1781 request_rec *r, int override,
1783 const char *d, const char *access_name)
1785 ap_configfile_t *f = NULL;
1787 char *filename = NULL;
1788 const struct htaccess_result *cache;
1789 struct htaccess_result *new;
1790 ap_conf_vector_t *dc = NULL;
1791 apr_status_t status;
1793 /* firstly, search cache */
1794 for (cache = r->htaccess; cache != NULL; cache = cache->next) {
1795 if (cache->override == override && strcmp(cache->dir, d) == 0) {
1796 *result = cache->htaccess;
1801 parms = default_parms;
1802 parms.override = override;
1803 parms.override_opts = override_opts;
1804 parms.pool = r->pool;
1805 parms.temp_pool = r->pool;
1806 parms.server = r->server;
1807 parms.path = apr_pstrdup(r->pool, d);
1809 /* loop through the access names and find the first one */
1810 while (access_name[0]) {
1811 /* AFAICT; there is no use of the actual 'filename' against
1812 * any canonicalization, so we will simply take the given
1813 * name, ignoring case sensitivity and aliases
1815 filename = ap_make_full_path(r->pool, d,
1816 ap_getword_conf(r->pool, &access_name));
1817 status = ap_pcfg_openfile(&f, r->pool, filename);
1819 if (status == APR_SUCCESS) {
1821 ap_directive_t *temptree = NULL;
1823 dc = ap_create_per_dir_config(r->pool);
1825 parms.config_file = f;
1826 errmsg = ap_build_config(&parms, r->pool, r->pool, &temptree);
1828 errmsg = ap_walk_config(temptree, &parms, dc);
1830 ap_cfg_closefile(f);
1833 ap_log_rerror(APLOG_MARK, APLOG_ALERT, 0, r,
1834 "%s: %s", filename, errmsg);
1835 return HTTP_INTERNAL_SERVER_ERROR;
1842 if (!APR_STATUS_IS_ENOENT(status)
1843 && !APR_STATUS_IS_ENOTDIR(status)) {
1844 ap_log_rerror(APLOG_MARK, APLOG_CRIT, status, r,
1845 "%s pcfg_openfile: unable to check htaccess file, "
1846 "ensure it is readable",
1848 apr_table_setn(r->notes, "error-notes",
1849 "Server unable to read htaccess file, denying "
1850 "access to be safe");
1851 return HTTP_FORBIDDEN;
1857 new = apr_palloc(r->pool, sizeof(struct htaccess_result));
1858 new->dir = parms.path;
1859 new->override = override;
1860 new->override_opts = override_opts;
1863 /* add to head of list */
1864 new->next = r->htaccess;
1870 AP_CORE_DECLARE(const char *) ap_init_virtual_host(apr_pool_t *p,
1871 const char *hostname,
1872 server_rec *main_server,
1875 server_rec *s = (server_rec *) apr_pcalloc(p, sizeof(server_rec));
1877 /* TODO: this crap belongs in http_core */
1878 s->process = main_server->process;
1879 s->server_admin = NULL;
1880 s->server_hostname = NULL;
1881 s->server_scheme = NULL;
1882 s->error_fname = NULL;
1884 s->keep_alive_timeout = 0;
1886 s->keep_alive_max = -1;
1887 s->error_log = main_server->error_log;
1888 s->loglevel = main_server->loglevel;
1889 /* useful default, otherwise we get a port of 0 on redirects */
1890 s->port = main_server->port;
1894 s->names = apr_array_make(p, 4, sizeof(char **));
1895 s->wild_names = apr_array_make(p, 4, sizeof(char **));
1897 s->module_config = create_empty_config(p);
1898 s->lookup_defaults = ap_create_per_dir_config(p);
1900 s->limit_req_line = main_server->limit_req_line;
1901 s->limit_req_fieldsize = main_server->limit_req_fieldsize;
1902 s->limit_req_fields = main_server->limit_req_fields;
1906 return ap_parse_vhost_addrs(p, hostname, s);
1910 AP_DECLARE(void) ap_fixup_virtual_hosts(apr_pool_t *p, server_rec *main_server)
1914 for (virt = main_server->next; virt; virt = virt->next) {
1915 merge_server_configs(p, main_server->module_config,
1916 virt->module_config);
1918 virt->lookup_defaults =
1919 ap_merge_per_dir_configs(p, main_server->lookup_defaults,
1920 virt->lookup_defaults);
1922 if (virt->server_admin == NULL)
1923 virt->server_admin = main_server->server_admin;
1925 if (virt->timeout == 0)
1926 virt->timeout = main_server->timeout;
1928 if (virt->keep_alive_timeout == 0)
1929 virt->keep_alive_timeout = main_server->keep_alive_timeout;
1931 if (virt->keep_alive == -1)
1932 virt->keep_alive = main_server->keep_alive;
1934 if (virt->keep_alive_max == -1)
1935 virt->keep_alive_max = main_server->keep_alive_max;
1937 /* XXX: this is really something that should be dealt with by a
1938 * post-config api phase
1940 ap_core_reorder_directories(p, virt);
1943 ap_core_reorder_directories(p, main_server);
1946 /*****************************************************************
1948 * Getting *everything* configured...
1951 static void init_config_globals(apr_pool_t *p)
1953 /* Global virtual host hash bucket pointers. Init to null. */
1954 ap_init_vhost_config(p);
1957 static server_rec *init_server_config(process_rec *process, apr_pool_t *p)
1960 server_rec *s = (server_rec *) apr_pcalloc(p, sizeof(server_rec));
1962 apr_file_open_stderr(&s->error_log, p);
1963 s->process = process;
1965 s->server_admin = DEFAULT_ADMIN;
1966 s->server_hostname = NULL;
1967 s->server_scheme = NULL;
1968 s->error_fname = DEFAULT_ERRORLOG;
1969 s->loglevel = DEFAULT_LOGLEVEL;
1970 s->limit_req_line = DEFAULT_LIMIT_REQUEST_LINE;
1971 s->limit_req_fieldsize = DEFAULT_LIMIT_REQUEST_FIELDSIZE;
1972 s->limit_req_fields = DEFAULT_LIMIT_REQUEST_FIELDS;
1973 s->timeout = apr_time_from_sec(DEFAULT_TIMEOUT);
1974 s->keep_alive_timeout = apr_time_from_sec(DEFAULT_KEEPALIVE_TIMEOUT);
1975 s->keep_alive_max = DEFAULT_KEEPALIVE;
1978 s->addrs = apr_pcalloc(p, sizeof(server_addr_rec));
1980 /* NOT virtual host; don't match any real network interface */
1981 rv = apr_sockaddr_info_get(&s->addrs->host_addr,
1982 NULL, APR_INET, 0, 0, p);
1983 ap_assert(rv == APR_SUCCESS); /* otherwise: bug or no storage */
1985 s->addrs->host_port = 0; /* matches any port */
1986 s->addrs->virthost = ""; /* must be non-NULL */
1987 s->names = s->wild_names = NULL;
1989 s->module_config = create_server_config(p, s);
1990 s->lookup_defaults = create_default_per_dir_config(p);
1996 AP_DECLARE(server_rec*) ap_read_config(process_rec *process, apr_pool_t *ptemp,
1997 const char *filename,
1998 ap_directive_t **conftree)
2000 const char *confname, *error;
2001 apr_pool_t *p = process->pconf;
2002 server_rec *s = init_server_config(process, p);
2004 init_config_globals(p);
2006 /* All server-wide config files now have the SAME syntax... */
2007 error = process_command_config(s, ap_server_pre_read_config, conftree,
2010 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL, "%s: %s",
2011 ap_server_argv0, error);
2015 /* process_command_config may change the ServerRoot so
2016 * compute this config file name afterwards.
2018 confname = ap_server_root_relative(p, filename);
2021 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
2022 APR_EBADPATH, NULL, "Invalid config file path %s",
2027 error = ap_process_resource_config(s, confname, conftree, p, ptemp);
2029 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL,
2030 "%s: %s", ap_server_argv0, error);
2034 error = process_command_config(s, ap_server_post_read_config, conftree,
2038 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL, "%s: %s",
2039 ap_server_argv0, error);
2046 AP_DECLARE(void) ap_single_module_configure(apr_pool_t *p, server_rec *s,
2049 if (m->create_server_config)
2050 ap_set_module_config(s->module_config, m,
2051 (*m->create_server_config)(p, s));
2053 if (m->create_dir_config)
2054 ap_set_module_config(s->lookup_defaults, m,
2055 (*m->create_dir_config)(p, NULL));
2058 AP_DECLARE(void) ap_run_rewrite_args(process_rec *process)
2062 for (m = ap_top_module; m; m = m->next) {
2063 if (m->rewrite_args) {
2064 (*m->rewrite_args)(process);
2069 /********************************************************************
2070 * Configuration directives are restricted in terms of where they may
2071 * appear in the main configuration files and/or .htaccess files according
2072 * to the bitmask req_override in the command_rec structure.
2073 * If any of the overrides set in req_override are also allowed in the
2074 * context in which the command is read, then the command is allowed.
2075 * The context is determined as follows:
2077 * inside *.conf --> override = (RSRC_CONF|OR_ALL)&~(OR_AUTHCFG|OR_LIMIT);
2078 * within <Directory> or <Location> --> override = OR_ALL|ACCESS_CONF;
2079 * within .htaccess --> override = AllowOverride for current directory;
2081 * the result is, well, a rather confusing set of possibilities for when
2082 * a particular directive is allowed to be used. This procedure prints
2083 * in English where the given (pc) directive can be used.
2085 static void show_overrides(const command_rec *pc, module *pm)
2089 printf("\tAllowed in *.conf ");
2090 if ((pc->req_override & (OR_OPTIONS | OR_FILEINFO | OR_INDEXES))
2091 || ((pc->req_override & RSRC_CONF)
2092 && ((pc->req_override & (ACCESS_CONF | OR_AUTHCFG | OR_LIMIT))))) {
2095 else if (pc->req_override & RSRC_CONF) {
2096 printf("only outside <Directory>, <Files> or <Location>");
2099 printf("only inside <Directory>, <Files> or <Location>");
2102 /* Warn if the directive is allowed inside <Directory> or .htaccess
2103 * but module doesn't support per-dir configuration
2105 if ((pc->req_override & (OR_ALL | ACCESS_CONF)) && !pm->create_dir_config)
2106 printf(" [no per-dir config]");
2108 if (pc->req_override & OR_ALL) {
2109 printf(" and in .htaccess\n\twhen AllowOverride");
2111 if ((pc->req_override & OR_ALL) == OR_ALL) {
2112 printf(" isn't None");
2115 printf(" includes ");
2117 if (pc->req_override & OR_AUTHCFG) {
2121 printf("AuthConfig");
2124 if (pc->req_override & OR_LIMIT) {
2131 if (pc->req_override & OR_OPTIONS) {
2138 if (pc->req_override & OR_FILEINFO) {
2145 if (pc->req_override & OR_INDEXES) {
2157 /* Show the preloaded configuration directives, the help string explaining
2158 * the directive arguments, in what module they are handled, and in
2159 * what parts of the configuration they are allowed. Used for httpd -L.
2161 AP_DECLARE(void) ap_show_directives(void)
2163 const command_rec *pc;
2166 for (n = 0; ap_loaded_modules[n]; ++n) {
2167 for (pc = ap_loaded_modules[n]->cmds; pc && pc->name; ++pc) {
2168 printf("%s (%s)\n", pc->name, ap_loaded_modules[n]->name);
2171 printf("\t%s\n", pc->errmsg);
2173 show_overrides(pc, ap_loaded_modules[n]);
2178 /* Show the preloaded module names. Used for httpd -l. */
2179 AP_DECLARE(void) ap_show_modules(void)
2183 printf("Compiled in modules:\n");
2184 for (n = 0; ap_loaded_modules[n]; ++n)
2185 printf(" %s\n", ap_loaded_modules[n]->name);
2188 AP_DECLARE(const char *) ap_show_mpm(void)