switch to a 60 bit hash
[httpd-crcsyncproxy.git] / server / config.c
blob564cf6d5efd1cbb4344307cc403fe1b9f59f0f50
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
21 * Rob McCool
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).
28 * rst
32 #include "apr.h"
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
40 #include "apr_want.h"
42 #include "ap_config.h"
43 #include "httpd.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"
52 #include "mpm.h"
55 AP_DECLARE_DATA const char *ap_server_argv0 = NULL;
57 AP_DECLARE_DATA const char *ap_server_root = NULL;
59 AP_DECLARE_DATA apr_array_header_t *ap_server_pre_read_config = NULL;
60 AP_DECLARE_DATA apr_array_header_t *ap_server_post_read_config = NULL;
61 AP_DECLARE_DATA apr_array_header_t *ap_server_config_defines = NULL;
63 AP_DECLARE_DATA ap_directive_t *ap_conftree = NULL;
65 APR_HOOK_STRUCT(
66 APR_HOOK_LINK(header_parser)
67 APR_HOOK_LINK(pre_config)
68 APR_HOOK_LINK(check_config)
69 APR_HOOK_LINK(post_config)
70 APR_HOOK_LINK(open_logs)
71 APR_HOOK_LINK(child_init)
72 APR_HOOK_LINK(handler)
73 APR_HOOK_LINK(quick_handler)
74 APR_HOOK_LINK(optional_fn_retrieve)
75 APR_HOOK_LINK(test_config)
78 AP_IMPLEMENT_HOOK_RUN_ALL(int, header_parser,
79 (request_rec *r), (r), OK, DECLINED)
81 AP_IMPLEMENT_HOOK_RUN_ALL(int, pre_config,
82 (apr_pool_t *pconf, apr_pool_t *plog,
83 apr_pool_t *ptemp),
84 (pconf, plog, ptemp), OK, DECLINED)
86 AP_IMPLEMENT_HOOK_RUN_ALL(int, check_config,
87 (apr_pool_t *pconf, apr_pool_t *plog,
88 apr_pool_t *ptemp, server_rec *s),
89 (pconf, plog, ptemp, s), OK, DECLINED)
91 AP_IMPLEMENT_HOOK_VOID(test_config,
92 (apr_pool_t *pconf, server_rec *s),
93 (pconf, s))
95 AP_IMPLEMENT_HOOK_RUN_ALL(int, post_config,
96 (apr_pool_t *pconf, apr_pool_t *plog,
97 apr_pool_t *ptemp, server_rec *s),
98 (pconf, plog, ptemp, s), OK, DECLINED)
100 /* During the course of debugging I expanded this macro out, so
101 * rather than remove all the useful information there is in the
102 * following lines, I'm going to leave it here in case anyone
103 * else finds it useful.
105 * Ben has looked at it and thinks it correct :)
107 AP_DECLARE(int) ap_hook_post_config(ap_HOOK_post_config_t *pf,
108 const char * const *aszPre,
109 const char * const *aszSucc,
110 int nOrder)
112 ap_LINK_post_config_t *pHook;
114 if (!_hooks.link_post_config) {
115 _hooks.link_post_config = apr_array_make(apr_hook_global_pool, 1,
116 sizeof(ap_LINK_post_config_t));
117 apr_hook_sort_register("post_config", &_hooks.link_post_config);
120 pHook = apr_array_push(_hooks.link_post_config);
121 pHook->pFunc = pf;
122 pHook->aszPredecessors = aszPre;
123 pHook->aszSuccessors = aszSucc;
124 pHook->nOrder = nOrder;
125 pHook->szName = apr_hook_debug_current;
127 if (apr_hook_debug_enabled)
128 apr_hook_debug_show("post_config", aszPre, aszSucc);
131 AP_DECLARE(apr_array_header_t *) ap_hook_get_post_config(void) {
132 return _hooks.link_post_config;
135 AP_DECLARE(int) ap_run_post_config(apr_pool_t *pconf,
136 apr_pool_t *plog,
137 apr_pool_t *ptemp,
138 server_rec *s)
140 ap_LINK_post_config_t *pHook;
141 int n;
143 if(!_hooks.link_post_config)
144 return;
146 pHook = (ap_LINK_post_config_t *)_hooks.link_post_config->elts;
147 for (n = 0; n < _hooks.link_post_config->nelts; ++n)
148 pHook[n].pFunc (pconf, plog, ptemp, s);
152 AP_IMPLEMENT_HOOK_RUN_ALL(int, open_logs,
153 (apr_pool_t *pconf, apr_pool_t *plog,
154 apr_pool_t *ptemp, server_rec *s),
155 (pconf, plog, ptemp, s), OK, DECLINED)
157 AP_IMPLEMENT_HOOK_VOID(child_init,
158 (apr_pool_t *pchild, server_rec *s),
159 (pchild, s))
161 AP_IMPLEMENT_HOOK_RUN_FIRST(int, handler, (request_rec *r),
162 (r), DECLINED)
164 AP_IMPLEMENT_HOOK_RUN_FIRST(int, quick_handler, (request_rec *r, int lookup),
165 (r, lookup), DECLINED)
167 AP_IMPLEMENT_HOOK_VOID(optional_fn_retrieve, (void), ())
169 /****************************************************************
171 * We begin with the functions which deal with the linked list
172 * of modules which control just about all of the server operation.
175 /* total_modules is the number of modules that have been linked
176 * into the server.
178 static int total_modules = 0;
180 /* dynamic_modules is the number of modules that have been added
181 * after the pre-loaded ones have been set up. It shouldn't be larger
182 * than DYNAMIC_MODULE_LIMIT.
184 static int dynamic_modules = 0;
186 AP_DECLARE_DATA module *ap_top_module = NULL;
187 AP_DECLARE_DATA module **ap_loaded_modules=NULL;
189 static apr_hash_t *ap_config_hash = NULL;
191 typedef int (*handler_func)(request_rec *);
192 typedef void *(*dir_maker_func)(apr_pool_t *, char *);
193 typedef void *(*merger_func)(apr_pool_t *, void *, void *);
195 /* maximum nesting level for config directories */
196 #ifndef AP_MAX_INCLUDE_DIR_DEPTH
197 #define AP_MAX_INCLUDE_DIR_DEPTH (128)
198 #endif
200 /* Dealing with config vectors. These are associated with per-directory,
201 * per-server, and per-request configuration, and have a void* pointer for
202 * each modules. The nature of the structure pointed to is private to the
203 * module in question... the core doesn't (and can't) know. However, there
204 * are defined interfaces which allow it to create instances of its private
205 * per-directory and per-server structures, and to merge the per-directory
206 * structures of a directory and its subdirectory (producing a new one in
207 * which the defaults applying to the base directory have been properly
208 * overridden).
211 static ap_conf_vector_t *create_empty_config(apr_pool_t *p)
213 void *conf_vector = apr_pcalloc(p, sizeof(void *) *
214 (total_modules + DYNAMIC_MODULE_LIMIT));
215 return conf_vector;
218 static ap_conf_vector_t *create_default_per_dir_config(apr_pool_t *p)
220 void **conf_vector = apr_pcalloc(p, sizeof(void *) *
221 (total_modules + DYNAMIC_MODULE_LIMIT));
222 module *modp;
224 for (modp = ap_top_module; modp; modp = modp->next) {
225 dir_maker_func df = modp->create_dir_config;
227 if (df)
228 conf_vector[modp->module_index] = (*df)(p, NULL);
231 return (ap_conf_vector_t *)conf_vector;
234 AP_CORE_DECLARE(ap_conf_vector_t *) ap_merge_per_dir_configs(apr_pool_t *p,
235 ap_conf_vector_t *base,
236 ap_conf_vector_t *new_conf)
238 void **conf_vector = apr_palloc(p, sizeof(void *) * total_modules);
239 void **base_vector = (void **)base;
240 void **new_vector = (void **)new_conf;
241 module *modp;
243 for (modp = ap_top_module; modp; modp = modp->next) {
244 int i = modp->module_index;
246 if (!new_vector[i]) {
247 conf_vector[i] = base_vector[i];
249 else {
250 merger_func df = modp->merge_dir_config;
251 if (df && base_vector[i]) {
252 conf_vector[i] = (*df)(p, base_vector[i], new_vector[i]);
254 else
255 conf_vector[i] = new_vector[i];
259 return (ap_conf_vector_t *)conf_vector;
262 static ap_conf_vector_t *create_server_config(apr_pool_t *p, server_rec *s)
264 void **conf_vector = apr_pcalloc(p, sizeof(void *) *
265 (total_modules + DYNAMIC_MODULE_LIMIT));
266 module *modp;
268 for (modp = ap_top_module; modp; modp = modp->next) {
269 if (modp->create_server_config)
270 conf_vector[modp->module_index] = (*modp->create_server_config)(p, s);
273 return (ap_conf_vector_t *)conf_vector;
276 static void merge_server_configs(apr_pool_t *p, ap_conf_vector_t *base,
277 ap_conf_vector_t *virt)
279 /* Can reuse the 'virt' vector for the spine of it, since we don't
280 * have to deal with the moral equivalent of .htaccess files here...
283 void **base_vector = (void **)base;
284 void **virt_vector = (void **)virt;
285 module *modp;
287 for (modp = ap_top_module; modp; modp = modp->next) {
288 merger_func df = modp->merge_server_config;
289 int i = modp->module_index;
291 if (!virt_vector[i])
292 virt_vector[i] = base_vector[i];
293 else if (df)
294 virt_vector[i] = (*df)(p, base_vector[i], virt_vector[i]);
298 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_request_config(apr_pool_t *p)
300 return create_empty_config(p);
303 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_conn_config(apr_pool_t *p)
305 return create_empty_config(p);
308 AP_CORE_DECLARE(ap_conf_vector_t *) ap_create_per_dir_config(apr_pool_t *p)
310 return create_empty_config(p);
313 static int ap_invoke_filter_init(ap_filter_t *filters)
315 while (filters) {
316 if (filters->frec->filter_init_func) {
317 int result = filters->frec->filter_init_func(filters);
318 if (result != OK) {
319 return result;
322 filters = filters->next;
324 return OK;
328 * TODO: Move this to an appropriate include file and possibly prefix it
329 * with AP_.
331 #define DEFAULT_HANDLER_NAME ""
333 AP_CORE_DECLARE(int) ap_invoke_handler(request_rec *r)
335 const char *handler;
336 const char *p;
337 int result;
338 const char *old_handler = r->handler;
339 const char *ignore;
342 * The new insert_filter stage makes the most sense here. We only use
343 * it when we are going to run the request, so we must insert filters
344 * if any are available. Since the goal of this phase is to allow all
345 * modules to insert a filter if they want to, this filter returns
346 * void. I just can't see any way that this filter can reasonably
347 * fail, either your modules inserts something or it doesn't. rbb
349 ap_run_insert_filter(r);
351 /* Before continuing, allow each filter that is in the two chains to
352 * run their init function to let them do any magic before we could
353 * start generating data.
355 result = ap_invoke_filter_init(r->input_filters);
356 if (result != OK) {
357 return result;
359 result = ap_invoke_filter_init(r->output_filters);
360 if (result != OK) {
361 return result;
364 if (!r->handler) {
365 if (r->content_type) {
366 handler = r->content_type;
367 if ((p=ap_strchr_c(handler, ';')) != NULL) {
368 char *new_handler = (char *)apr_pmemdup(r->pool, handler,
369 p - handler + 1);
370 char *p2 = new_handler + (p - handler);
371 handler = new_handler;
373 /* exclude media type arguments */
374 while (p2 > handler && p2[-1] == ' ')
375 --p2; /* strip trailing spaces */
377 *p2='\0';
380 else {
381 handler = DEFAULT_HANDLER_NAME;
384 r->handler = handler;
387 result = ap_run_handler(r);
389 r->handler = old_handler;
391 if (result == DECLINED && r->handler && r->filename) {
392 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
393 "handler \"%s\" not found for: %s", r->handler, r->filename);
395 if ((result != OK) && (result != DONE) && (result != DECLINED) && (result != SUSPENDED)
396 && (result != AP_FILTER_ERROR) /* ap_die() knows about this specifically */
397 && !ap_is_HTTP_VALID_RESPONSE(result)) {
398 /* If a module is deliberately returning something else
399 * (request_rec in non-HTTP or proprietary extension?)
400 * let it set a note to allow it explicitly.
401 * Otherwise, a return code that is neither reserved nor HTTP
402 * is a bug, as in PR#31759.
404 ignore = apr_table_get(r->notes, "HTTP_IGNORE_RANGE");
405 if (!ignore) {
406 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
407 "Handler for %s returned invalid result code %d",
408 r->handler, result);
409 result = HTTP_INTERNAL_SERVER_ERROR;
413 return result == DECLINED ? HTTP_INTERNAL_SERVER_ERROR : result;
416 AP_DECLARE(int) ap_method_is_limited(cmd_parms *cmd, const char *method)
418 int methnum;
420 methnum = ap_method_number_of(method);
423 * A method number either hardcoded into apache or
424 * added by a module and registered.
426 if (methnum != M_INVALID) {
427 return (cmd->limited & (AP_METHOD_BIT << methnum)) ? 1 : 0;
430 return 0; /* not found */
433 AP_DECLARE(void) ap_register_hooks(module *m, apr_pool_t *p)
435 if (m->register_hooks) {
436 if (getenv("SHOW_HOOKS")) {
437 printf("Registering hooks for %s\n", m->name);
438 apr_hook_debug_enabled = 1;
441 apr_hook_debug_current = m->name;
442 m->register_hooks(p);
446 static void ap_add_module_commands(module *m, apr_pool_t *p);
448 typedef struct ap_mod_list_struct ap_mod_list;
449 struct ap_mod_list_struct {
450 struct ap_mod_list_struct *next;
451 module *m;
452 const command_rec *cmd;
455 static apr_status_t reload_conf_hash(void *baton)
457 ap_config_hash = NULL;
458 return APR_SUCCESS;
461 static void rebuild_conf_hash(apr_pool_t *p, int add_prelinked)
463 module **m;
465 ap_config_hash = apr_hash_make(p);
467 apr_pool_cleanup_register(p, NULL, reload_conf_hash,
468 apr_pool_cleanup_null);
469 if (add_prelinked) {
470 for (m = ap_prelinked_modules; *m != NULL; m++) {
471 ap_add_module_commands(*m, p);
476 static void ap_add_module_commands(module *m, apr_pool_t *p)
478 apr_pool_t *tpool;
479 ap_mod_list *mln;
480 const command_rec *cmd;
481 char *dir;
483 cmd = m->cmds;
485 if (ap_config_hash == NULL) {
486 rebuild_conf_hash(p, 0);
489 tpool = apr_hash_pool_get(ap_config_hash);
491 while (cmd && cmd->name) {
492 mln = apr_palloc(tpool, sizeof(ap_mod_list));
493 mln->cmd = cmd;
494 mln->m = m;
495 dir = apr_pstrdup(tpool, cmd->name);
497 ap_str_tolower(dir);
499 mln->next = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
500 apr_hash_set(ap_config_hash, dir, APR_HASH_KEY_STRING, mln);
501 ++cmd;
506 /* One-time setup for precompiled modules --- NOT to be done on restart */
508 AP_DECLARE(const char *) ap_add_module(module *m, apr_pool_t *p)
510 /* This could be called from a LoadModule httpd.conf command,
511 * after the file has been linked and the module structure within it
512 * teased out...
515 if (m->version != MODULE_MAGIC_NUMBER_MAJOR) {
516 return apr_psprintf(p, "Module \"%s\" is not compatible with this "
517 "version of Apache (found %d, need %d). Please "
518 "contact the vendor for the correct version.",
519 m->name, m->version, MODULE_MAGIC_NUMBER_MAJOR);
522 if (m->next == NULL) {
523 m->next = ap_top_module;
524 ap_top_module = m;
527 if (m->module_index == -1) {
528 m->module_index = total_modules++;
529 dynamic_modules++;
531 if (dynamic_modules > DYNAMIC_MODULE_LIMIT) {
532 return apr_psprintf(p, "Module \"%s\" could not be loaded, "
533 "because the dynamic module limit was "
534 "reached. Please increase "
535 "DYNAMIC_MODULE_LIMIT and recompile.", m->name);
539 /* Some C compilers put a complete path into __FILE__, but we want
540 * only the filename (e.g. mod_includes.c). So check for path
541 * components (Unix and DOS), and remove them.
544 if (ap_strrchr_c(m->name, '/'))
545 m->name = 1 + ap_strrchr_c(m->name, '/');
547 if (ap_strrchr_c(m->name, '\\'))
548 m->name = 1 + ap_strrchr_c(m->name, '\\');
550 #ifdef _OSD_POSIX
551 /* __FILE__ =
552 * "*POSIX(/home/martin/apache/src/modules/standard/mod_info.c)"
555 /* We cannot fix the string in-place, because it's const */
556 if (m->name[strlen(m->name)-1] == ')') {
557 char *tmp = strdup(m->name); /* FIXME: memory leak, albeit a small one */
558 tmp[strlen(tmp)-1] = '\0';
559 m->name = tmp;
561 #endif /*_OSD_POSIX*/
563 ap_add_module_commands(m, p);
564 /* FIXME: is this the right place to call this?
565 * It doesn't appear to be
567 ap_register_hooks(m, p);
569 return NULL;
573 * remove_module undoes what add_module did. There are some caveats:
574 * when the module is removed, its slot is lost so all the current
575 * per-dir and per-server configurations are invalid. So we should
576 * only ever call this function when you are invalidating almost
577 * all our current data. I.e. when doing a restart.
580 AP_DECLARE(void) ap_remove_module(module *m)
582 module *modp;
584 modp = ap_top_module;
585 if (modp == m) {
586 /* We are the top module, special case */
587 ap_top_module = modp->next;
588 m->next = NULL;
590 else {
591 /* Not the top module, find use. When found modp will
592 * point to the module _before_ us in the list
595 while (modp && modp->next != m) {
596 modp = modp->next;
599 if (!modp) {
600 /* Uh-oh, this module doesn't exist */
601 ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
602 "Cannot remove module %s: not found in module list",
603 m->name);
604 return;
607 /* Eliminate us from the module list */
608 modp->next = modp->next->next;
611 m->module_index = -1; /* simulate being unloaded, should
612 * be unnecessary */
613 dynamic_modules--;
614 total_modules--;
617 AP_DECLARE(const char *) ap_add_loaded_module(module *mod, apr_pool_t *p)
619 module **m;
620 const char *error;
623 * Add module pointer to top of chained module list
625 error = ap_add_module(mod, p);
626 if (error) {
627 return error;
631 * And module pointer to list of loaded modules
633 * Notes: 1. ap_add_module() would already complain if no more space
634 * exists for adding a dynamically loaded module
635 * 2. ap_add_module() accepts double inclusion, so we have
636 * to accept this, too.
638 for (m = ap_loaded_modules; *m != NULL; m++)
640 *m++ = mod;
641 *m = NULL;
643 return NULL;
646 AP_DECLARE(void) ap_remove_loaded_module(module *mod)
648 module **m;
649 module **m2;
650 int done;
653 * Remove module pointer from chained module list
655 ap_remove_module(mod);
658 * Remove module pointer from list of loaded modules
660 * Note: 1. We cannot determine if the module was successfully
661 * removed by ap_remove_module().
662 * 2. We have not to complain explicity when the module
663 * is not found because ap_remove_module() did it
664 * for us already.
666 for (m = m2 = ap_loaded_modules, done = 0; *m2 != NULL; m2++) {
667 if (*m2 == mod && done == 0)
668 done = 1;
669 else
670 *m++ = *m2;
673 *m = NULL;
676 AP_DECLARE(const char *) ap_setup_prelinked_modules(process_rec *process)
678 module **m;
679 module **m2;
680 const char *error;
682 apr_hook_global_pool=process->pconf;
684 rebuild_conf_hash(process->pconf, 0);
687 * Initialise total_modules variable and module indices
689 total_modules = 0;
690 for (m = ap_preloaded_modules; *m != NULL; m++)
691 (*m)->module_index = total_modules++;
694 * Initialise list of loaded modules
696 ap_loaded_modules = (module **)apr_palloc(process->pool,
697 sizeof(module *) * (total_modules + DYNAMIC_MODULE_LIMIT + 1));
699 if (ap_loaded_modules == NULL) {
700 return "Ouch! Out of memory in ap_setup_prelinked_modules()!";
703 for (m = ap_preloaded_modules, m2 = ap_loaded_modules; *m != NULL; )
704 *m2++ = *m++;
706 *m2 = NULL;
709 * Initialize chain of linked (=activate) modules
711 for (m = ap_prelinked_modules; *m != NULL; m++) {
712 error = ap_add_module(*m, process->pconf);
713 if (error) {
714 return error;
718 apr_hook_sort_all();
720 return NULL;
723 AP_DECLARE(const char *) ap_find_module_name(module *m)
725 return m->name;
728 AP_DECLARE(module *) ap_find_linked_module(const char *name)
730 module *modp;
732 for (modp = ap_top_module; modp; modp = modp->next) {
733 if (strcmp(modp->name, name) == 0)
734 return modp;
737 return NULL;
740 /*****************************************************************
742 * Resource, access, and .htaccess config files now parsed by a common
743 * command loop.
745 * Let's begin with the basics; parsing the line and
746 * invoking the function...
749 #define AP_MAX_ARGC 64
751 static const char *invoke_cmd(const command_rec *cmd, cmd_parms *parms,
752 void *mconfig, const char *args)
754 char *w, *w2, *w3;
755 const char *errmsg = NULL;
757 if ((parms->override & cmd->req_override) == 0)
758 return apr_pstrcat(parms->pool, cmd->name, " not allowed here", NULL);
760 parms->info = cmd->cmd_data;
761 parms->cmd = cmd;
763 switch (cmd->args_how) {
764 case RAW_ARGS:
765 #ifdef RESOLVE_ENV_PER_TOKEN
766 args = ap_resolve_env(parms->pool,args);
767 #endif
768 return cmd->AP_RAW_ARGS(parms, mconfig, args);
770 case TAKE_ARGV:
772 char *argv[AP_MAX_ARGC];
773 int argc = 0;
775 do {
776 w = ap_getword_conf(parms->pool, &args);
777 if (*w == '\0' && *args == '\0') {
778 break;
780 argv[argc] = w;
781 argc++;
782 } while (argc < AP_MAX_ARGC && *args != '\0');
784 return cmd->AP_TAKE_ARGV(parms, mconfig, argc, argv);
787 case NO_ARGS:
788 if (*args != 0)
789 return apr_pstrcat(parms->pool, cmd->name, " takes no arguments",
790 NULL);
792 return cmd->AP_NO_ARGS(parms, mconfig);
794 case TAKE1:
795 w = ap_getword_conf(parms->pool, &args);
797 if (*w == '\0' || *args != 0)
798 return apr_pstrcat(parms->pool, cmd->name, " takes one argument",
799 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
801 return cmd->AP_TAKE1(parms, mconfig, w);
803 case TAKE2:
804 w = ap_getword_conf(parms->pool, &args);
805 w2 = ap_getword_conf(parms->pool, &args);
807 if (*w == '\0' || *w2 == '\0' || *args != 0)
808 return apr_pstrcat(parms->pool, cmd->name, " takes two arguments",
809 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
811 return cmd->AP_TAKE2(parms, mconfig, w, w2);
813 case TAKE12:
814 w = ap_getword_conf(parms->pool, &args);
815 w2 = ap_getword_conf(parms->pool, &args);
817 if (*w == '\0' || *args != 0)
818 return apr_pstrcat(parms->pool, cmd->name, " takes 1-2 arguments",
819 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
821 return cmd->AP_TAKE2(parms, mconfig, w, *w2 ? w2 : NULL);
823 case TAKE3:
824 w = ap_getword_conf(parms->pool, &args);
825 w2 = ap_getword_conf(parms->pool, &args);
826 w3 = ap_getword_conf(parms->pool, &args);
828 if (*w == '\0' || *w2 == '\0' || *w3 == '\0' || *args != 0)
829 return apr_pstrcat(parms->pool, cmd->name, " takes three arguments",
830 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
832 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
834 case TAKE23:
835 w = ap_getword_conf(parms->pool, &args);
836 w2 = ap_getword_conf(parms->pool, &args);
837 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
839 if (*w == '\0' || *w2 == '\0' || *args != 0)
840 return apr_pstrcat(parms->pool, cmd->name,
841 " takes two or three arguments",
842 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
844 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
846 case TAKE123:
847 w = ap_getword_conf(parms->pool, &args);
848 w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
849 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
851 if (*w == '\0' || *args != 0)
852 return apr_pstrcat(parms->pool, cmd->name,
853 " takes one, two or three arguments",
854 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
856 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
858 case TAKE13:
859 w = ap_getword_conf(parms->pool, &args);
860 w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
861 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
863 if (*w == '\0' || (w2 && *w2 && !w3) || *args != 0)
864 return apr_pstrcat(parms->pool, cmd->name,
865 " takes one or three arguments",
866 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
868 return cmd->AP_TAKE3(parms, mconfig, w, w2, w3);
870 case ITERATE:
871 while (*(w = ap_getword_conf(parms->pool, &args)) != '\0') {
873 errmsg = cmd->AP_TAKE1(parms, mconfig, w);
875 if (errmsg && strcmp(errmsg, DECLINE_CMD) != 0)
876 return errmsg;
879 return errmsg;
881 case ITERATE2:
882 w = ap_getword_conf(parms->pool, &args);
884 if (*w == '\0' || *args == 0)
885 return apr_pstrcat(parms->pool, cmd->name,
886 " requires at least two arguments",
887 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
889 while (*(w2 = ap_getword_conf(parms->pool, &args)) != '\0') {
891 errmsg = cmd->AP_TAKE2(parms, mconfig, w, w2);
893 if (errmsg && strcmp(errmsg, DECLINE_CMD) != 0)
894 return errmsg;
897 return errmsg;
899 case FLAG:
900 w = ap_getword_conf(parms->pool, &args);
902 if (*w == '\0' || (strcasecmp(w, "on") && strcasecmp(w, "off")))
903 return apr_pstrcat(parms->pool, cmd->name, " must be On or Off",
904 NULL);
906 return cmd->AP_FLAG(parms, mconfig, strcasecmp(w, "off") != 0);
908 default:
909 return apr_pstrcat(parms->pool, cmd->name,
910 " is improperly configured internally (server bug)",
911 NULL);
915 AP_CORE_DECLARE(const command_rec *) ap_find_command(const char *name,
916 const command_rec *cmds)
918 while (cmds->name) {
919 if (!strcasecmp(name, cmds->name))
920 return cmds;
922 ++cmds;
925 return NULL;
928 AP_CORE_DECLARE(const command_rec *) ap_find_command_in_modules(
929 const char *cmd_name, module **mod)
931 const command_rec *cmdp;
932 module *modp;
934 for (modp = *mod; modp; modp = modp->next) {
935 if (modp->cmds && (cmdp = ap_find_command(cmd_name, modp->cmds))) {
936 *mod = modp;
937 return cmdp;
941 return NULL;
944 AP_CORE_DECLARE(void *) ap_set_config_vectors(server_rec *server,
945 ap_conf_vector_t *section_vector,
946 const char *section,
947 module *mod, apr_pool_t *pconf)
949 void *section_config = ap_get_module_config(section_vector, mod);
950 void *server_config = ap_get_module_config(server->module_config, mod);
952 if (!section_config && mod->create_dir_config) {
953 /* ### need to fix the create_dir_config functions' prototype... */
954 section_config = (*mod->create_dir_config)(pconf, (char *)section);
955 ap_set_module_config(section_vector, mod, section_config);
958 if (!server_config && mod->create_server_config) {
959 server_config = (*mod->create_server_config)(pconf, server);
960 ap_set_module_config(server->module_config, mod, server_config);
963 return section_config;
966 static const char *execute_now(char *cmd_line, const char *args,
967 cmd_parms *parms,
968 apr_pool_t *p, apr_pool_t *ptemp,
969 ap_directive_t **sub_tree,
970 ap_directive_t *parent);
972 static const char *ap_build_config_sub(apr_pool_t *p, apr_pool_t *temp_pool,
973 const char *l, cmd_parms *parms,
974 ap_directive_t **current,
975 ap_directive_t **curr_parent,
976 ap_directive_t **conftree)
978 const char *retval = NULL;
979 const char *args;
980 char *cmd_name;
981 ap_directive_t *newdir;
982 module *mod = ap_top_module;
983 const command_rec *cmd;
985 if (*l == '#' || *l == '\0')
986 return NULL;
988 #if RESOLVE_ENV_PER_TOKEN
989 args = l;
990 #else
991 args = ap_resolve_env(temp_pool, l);
992 #endif
994 cmd_name = ap_getword_conf(p, &args);
995 if (*cmd_name == '\0') {
996 /* Note: this branch should not occur. An empty line should have
997 * triggered the exit further above.
999 return NULL;
1002 if (cmd_name[1] != '/') {
1003 char *lastc = cmd_name + strlen(cmd_name) - 1;
1004 if (*lastc == '>') {
1005 *lastc = '\0' ;
1007 if (cmd_name[0] == '<' && *args == '\0') {
1008 args = ">";
1012 newdir = apr_pcalloc(p, sizeof(ap_directive_t));
1013 newdir->filename = parms->config_file->name;
1014 newdir->line_num = parms->config_file->line_number;
1015 newdir->directive = cmd_name;
1016 newdir->args = apr_pstrdup(p, args);
1018 if ((cmd = ap_find_command_in_modules(cmd_name, &mod)) != NULL) {
1019 if (cmd->req_override & EXEC_ON_READ) {
1020 ap_directive_t *sub_tree = NULL;
1022 parms->err_directive = newdir;
1023 retval = execute_now(cmd_name, args, parms, p, temp_pool,
1024 &sub_tree, *curr_parent);
1025 if (*current) {
1026 (*current)->next = sub_tree;
1028 else {
1029 *current = sub_tree;
1030 if (*curr_parent) {
1031 (*curr_parent)->first_child = (*current);
1033 if (*current) {
1034 (*current)->parent = (*curr_parent);
1037 if (*current) {
1038 if (!*conftree) {
1039 /* Before walking *current to the end of the list,
1040 * set the head to *current.
1042 *conftree = *current;
1044 while ((*current)->next != NULL) {
1045 (*current) = (*current)->next;
1046 (*current)->parent = (*curr_parent);
1049 return retval;
1053 if (cmd_name[0] == '<') {
1054 if (cmd_name[1] != '/') {
1055 (*current) = ap_add_node(curr_parent, *current, newdir, 1);
1057 else if (*curr_parent == NULL) {
1058 parms->err_directive = newdir;
1059 return apr_pstrcat(p, cmd_name,
1060 " without matching <", cmd_name + 2,
1061 " section", NULL);
1063 else {
1064 char *bracket = cmd_name + strlen(cmd_name) - 1;
1066 if (*bracket != '>') {
1067 parms->err_directive = newdir;
1068 return apr_pstrcat(p, cmd_name,
1069 "> directive missing closing '>'", NULL);
1072 *bracket = '\0';
1074 if (strcasecmp(cmd_name + 2,
1075 (*curr_parent)->directive + 1) != 0) {
1076 parms->err_directive = newdir;
1077 return apr_pstrcat(p, "Expected </",
1078 (*curr_parent)->directive + 1, "> but saw ",
1079 cmd_name, ">", NULL);
1082 *bracket = '>';
1084 /* done with this section; move up a level */
1085 *current = *curr_parent;
1086 *curr_parent = (*current)->parent;
1089 else {
1090 *current = ap_add_node(curr_parent, *current, newdir, 0);
1093 return retval;
1096 AP_DECLARE(const char *) ap_build_cont_config(apr_pool_t *p,
1097 apr_pool_t *temp_pool,
1098 cmd_parms *parms,
1099 ap_directive_t **current,
1100 ap_directive_t **curr_parent,
1101 char *orig_directive)
1103 char *l;
1104 char *bracket;
1105 const char *retval;
1106 ap_directive_t *sub_tree = NULL;
1108 /* Since this function can be called recursively, allocate
1109 * the temporary 8k string buffer from the temp_pool rather
1110 * than the stack to avoid over-running a fixed length stack.
1112 l = apr_palloc(temp_pool, MAX_STRING_LEN);
1114 bracket = apr_pstrcat(p, orig_directive + 1, ">", NULL);
1115 while (!(ap_cfg_getline(l, MAX_STRING_LEN, parms->config_file))) {
1116 if (!memcmp(l, "</", 2)
1117 && (strcasecmp(l + 2, bracket) == 0)
1118 && (*curr_parent == NULL)) {
1119 break;
1121 retval = ap_build_config_sub(p, temp_pool, l, parms, current,
1122 curr_parent, &sub_tree);
1123 if (retval != NULL)
1124 return retval;
1126 if (sub_tree == NULL && curr_parent != NULL) {
1127 sub_tree = *curr_parent;
1130 if (sub_tree == NULL && current != NULL) {
1131 sub_tree = *current;
1135 *current = sub_tree;
1136 return NULL;
1139 static const char *ap_walk_config_sub(const ap_directive_t *current,
1140 cmd_parms *parms,
1141 ap_conf_vector_t *section_vector)
1143 const command_rec *cmd;
1144 ap_mod_list *ml;
1145 char *dir = apr_pstrdup(parms->pool, current->directive);
1147 ap_str_tolower(dir);
1149 ml = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
1151 if (ml == NULL) {
1152 parms->err_directive = current;
1153 return apr_pstrcat(parms->pool, "Invalid command '",
1154 current->directive,
1155 "', perhaps misspelled or defined by a module "
1156 "not included in the server configuration",
1157 NULL);
1160 for ( ; ml != NULL; ml = ml->next) {
1161 void *dir_config = ap_set_config_vectors(parms->server,
1162 section_vector,
1163 parms->path,
1164 ml->m,
1165 parms->pool);
1166 const char *retval;
1167 cmd = ml->cmd;
1169 /* Once was enough? */
1170 if (cmd->req_override & EXEC_ON_READ) {
1171 continue;
1174 retval = invoke_cmd(cmd, parms, dir_config, current->args);
1176 if (retval != NULL && strcmp(retval, DECLINE_CMD) != 0) {
1177 /* If the directive in error has already been set, don't
1178 * replace it. Otherwise, an error inside a container
1179 * will be reported as occuring on the first line of the
1180 * container.
1182 if (!parms->err_directive) {
1183 parms->err_directive = current;
1185 return retval;
1189 return NULL;
1192 AP_DECLARE(const char *) ap_walk_config(ap_directive_t *current,
1193 cmd_parms *parms,
1194 ap_conf_vector_t *section_vector)
1196 ap_conf_vector_t *oldconfig = parms->context;
1198 parms->context = section_vector;
1200 /* scan through all directives, executing each one */
1201 for (; current != NULL; current = current->next) {
1202 const char *errmsg;
1204 parms->directive = current;
1206 /* actually parse the command and execute the correct function */
1207 errmsg = ap_walk_config_sub(current, parms, section_vector);
1208 if (errmsg != NULL) {
1209 /* restore the context (just in case) */
1210 parms->context = oldconfig;
1211 return errmsg;
1215 parms->context = oldconfig;
1216 return NULL;
1219 AP_DECLARE(const char *) ap_build_config(cmd_parms *parms,
1220 apr_pool_t *p, apr_pool_t *temp_pool,
1221 ap_directive_t **conftree)
1223 ap_directive_t *current = *conftree;
1224 ap_directive_t *curr_parent = NULL;
1225 char *l = apr_palloc (temp_pool, MAX_STRING_LEN);
1226 const char *errmsg;
1228 if (current != NULL) {
1229 while (current->next) {
1230 current = current->next;
1234 while (!(ap_cfg_getline(l, MAX_STRING_LEN, parms->config_file))) {
1235 errmsg = ap_build_config_sub(p, temp_pool, l, parms,
1236 &current, &curr_parent, conftree);
1237 if (errmsg != NULL)
1238 return errmsg;
1240 if (*conftree == NULL && curr_parent != NULL) {
1241 *conftree = curr_parent;
1244 if (*conftree == NULL && current != NULL) {
1245 *conftree = current;
1249 if (curr_parent != NULL) {
1250 errmsg = "";
1252 while (curr_parent != NULL) {
1253 errmsg = apr_psprintf(p, "%s%s%s:%u: %s> was not closed.",
1254 errmsg,
1255 *errmsg == '\0' ? "" : APR_EOL_STR,
1256 curr_parent->filename,
1257 curr_parent->line_num,
1258 curr_parent->directive);
1260 parms->err_directive = curr_parent;
1261 curr_parent = curr_parent->parent;
1264 return errmsg;
1267 return NULL;
1271 * Generic command functions...
1274 AP_DECLARE_NONSTD(const char *) ap_set_string_slot(cmd_parms *cmd,
1275 void *struct_ptr,
1276 const char *arg)
1278 int offset = (int)(long)cmd->info;
1280 *(const char **)((char *)struct_ptr + offset) = arg;
1282 return NULL;
1285 AP_DECLARE_NONSTD(const char *) ap_set_int_slot(cmd_parms *cmd,
1286 void *struct_ptr,
1287 const char *arg)
1289 char *endptr;
1290 char *error_str = NULL;
1291 int offset = (int)(long)cmd->info;
1293 *(int *)((char*)struct_ptr + offset) = strtol(arg, &endptr, 10);
1295 if ((*arg == '\0') || (*endptr != '\0')) {
1296 error_str = apr_psprintf(cmd->pool,
1297 "Invalid value for directive %s, expected integer",
1298 cmd->directive->directive);
1301 return error_str;
1304 AP_DECLARE_NONSTD(const char *) ap_set_string_slot_lower(cmd_parms *cmd,
1305 void *struct_ptr,
1306 const char *arg_)
1308 char *arg = apr_pstrdup(cmd->pool,arg_);
1309 int offset = (int)(long)cmd->info;
1311 ap_str_tolower(arg);
1312 *(char **)((char *)struct_ptr + offset) = arg;
1314 return NULL;
1317 AP_DECLARE_NONSTD(const char *) ap_set_flag_slot(cmd_parms *cmd,
1318 void *struct_ptr_v, int arg)
1320 int offset = (int)(long)cmd->info;
1321 char *struct_ptr = (char *)struct_ptr_v;
1323 *(int *)(struct_ptr + offset) = arg ? 1 : 0;
1325 return NULL;
1328 AP_DECLARE_NONSTD(const char *) ap_set_file_slot(cmd_parms *cmd, void *struct_ptr,
1329 const char *arg)
1331 /* Prepend server_root to relative arg.
1332 * This allows most args to be independent of server_root,
1333 * so the server can be moved or mirrored with less pain.
1335 const char *path;
1336 int offset = (int)(long)cmd->info;
1338 path = ap_server_root_relative(cmd->pool, arg);
1340 if (!path) {
1341 return apr_pstrcat(cmd->pool, "Invalid file path ",
1342 arg, NULL);
1345 *(const char **) ((char*)struct_ptr + offset) = path;
1347 return NULL;
1350 AP_DECLARE_NONSTD(const char *) ap_set_deprecated(cmd_parms *cmd,
1351 void *struct_ptr,
1352 const char *arg)
1354 return cmd->cmd->errmsg;
1357 /*****************************************************************
1359 * Reading whole config files...
1362 static cmd_parms default_parms =
1363 {NULL, 0, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
1365 AP_DECLARE(char *) ap_server_root_relative(apr_pool_t *p, const char *file)
1367 char *newpath = NULL;
1368 apr_status_t rv;
1369 rv = apr_filepath_merge(&newpath, ap_server_root, file,
1370 APR_FILEPATH_TRUENAME, p);
1371 if (newpath && (rv == APR_SUCCESS || APR_STATUS_IS_EPATHWILD(rv)
1372 || APR_STATUS_IS_ENOENT(rv)
1373 || APR_STATUS_IS_ENOTDIR(rv))) {
1374 return newpath;
1376 else {
1377 return NULL;
1381 AP_DECLARE(const char *) ap_soak_end_container(cmd_parms *cmd, char *directive)
1383 char l[MAX_STRING_LEN];
1384 const char *args;
1385 char *cmd_name;
1387 while(!(ap_cfg_getline(l, MAX_STRING_LEN, cmd->config_file))) {
1388 #if RESOLVE_ENV_PER_TOKEN
1389 args = l;
1390 #else
1391 args = ap_resolve_env(cmd->temp_pool, l);
1392 #endif
1394 cmd_name = ap_getword_conf(cmd->pool, &args);
1395 if (cmd_name[0] == '<') {
1396 if (cmd_name[1] == '/') {
1397 cmd_name[strlen(cmd_name) - 1] = '\0';
1399 if (strcasecmp(cmd_name + 2, directive + 1) != 0) {
1400 return apr_pstrcat(cmd->pool, "Expected </",
1401 directive + 1, "> but saw ",
1402 cmd_name, ">", NULL);
1405 return NULL; /* found end of container */
1407 else {
1408 const char *msg;
1410 if (*args == '\0' && cmd_name[strlen(cmd_name) - 1] == '>') {
1411 cmd_name[strlen(cmd_name) - 1] = '\0';
1414 if ((msg = ap_soak_end_container(cmd, cmd_name)) != NULL) {
1415 return msg;
1421 return apr_pstrcat(cmd->pool, "Expected </",
1422 directive + 1, "> before end of configuration",
1423 NULL);
1426 static const char *execute_now(char *cmd_line, const char *args,
1427 cmd_parms *parms,
1428 apr_pool_t *p, apr_pool_t *ptemp,
1429 ap_directive_t **sub_tree,
1430 ap_directive_t *parent)
1432 const command_rec *cmd;
1433 ap_mod_list *ml;
1434 char *dir = apr_pstrdup(parms->pool, cmd_line);
1436 ap_str_tolower(dir);
1438 ml = apr_hash_get(ap_config_hash, dir, APR_HASH_KEY_STRING);
1440 if (ml == NULL) {
1441 return apr_pstrcat(parms->pool, "Invalid command '",
1442 cmd_line,
1443 "', perhaps misspelled or defined by a module "
1444 "not included in the server configuration",
1445 NULL);
1448 for ( ; ml != NULL; ml = ml->next) {
1449 const char *retval;
1450 cmd = ml->cmd;
1452 retval = invoke_cmd(cmd, parms, sub_tree, args);
1454 if (retval != NULL) {
1455 return retval;
1459 return NULL;
1462 /* This structure and the following functions are needed for the
1463 * table-based config file reading. They are passed to the
1464 * cfg_open_custom() routine.
1467 /* Structure to be passed to cfg_open_custom(): it contains an
1468 * index which is incremented from 0 to nelts on each call to
1469 * cfg_getline() (which in turn calls arr_elts_getstr())
1470 * and an apr_array_header_t pointer for the string array.
1472 typedef struct {
1473 apr_array_header_t *array;
1474 int curr_idx;
1475 } arr_elts_param_t;
1478 /* arr_elts_getstr() returns the next line from the string array. */
1479 static void *arr_elts_getstr(void *buf, size_t bufsiz, void *param)
1481 arr_elts_param_t *arr_param = (arr_elts_param_t *)param;
1483 /* End of array reached? */
1484 if (++arr_param->curr_idx > arr_param->array->nelts)
1485 return NULL;
1487 /* return the line */
1488 apr_cpystrn(buf,
1489 ((char **)arr_param->array->elts)[arr_param->curr_idx - 1],
1490 bufsiz);
1492 return buf;
1496 /* arr_elts_close(): dummy close routine (makes sure no more lines can be read) */
1497 static int arr_elts_close(void *param)
1499 arr_elts_param_t *arr_param = (arr_elts_param_t *)param;
1501 arr_param->curr_idx = arr_param->array->nelts;
1503 return 0;
1506 static const char *process_command_config(server_rec *s,
1507 apr_array_header_t *arr,
1508 ap_directive_t **conftree,
1509 apr_pool_t *p,
1510 apr_pool_t *ptemp)
1512 const char *errmsg;
1513 cmd_parms parms;
1514 arr_elts_param_t arr_parms;
1516 arr_parms.curr_idx = 0;
1517 arr_parms.array = arr;
1519 if (ap_config_hash == NULL) {
1520 rebuild_conf_hash(s->process->pconf, 1);
1523 parms = default_parms;
1524 parms.pool = p;
1525 parms.temp_pool = ptemp;
1526 parms.server = s;
1527 parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1528 parms.override_opts = OPT_ALL | OPT_INCNOEXEC | OPT_SYM_OWNER | OPT_MULTI;
1530 parms.config_file = ap_pcfg_open_custom(p, "-c/-C directives",
1531 &arr_parms, NULL,
1532 arr_elts_getstr, arr_elts_close);
1534 errmsg = ap_build_config(&parms, p, ptemp, conftree);
1535 ap_cfg_closefile(parms.config_file);
1537 if (errmsg) {
1538 return apr_pstrcat(p, "Syntax error in -C/-c directive: ", errmsg,
1539 NULL);
1542 return NULL;
1545 typedef struct {
1546 char *fname;
1547 } fnames;
1549 static int fname_alphasort(const void *fn1, const void *fn2)
1551 const fnames *f1 = fn1;
1552 const fnames *f2 = fn2;
1554 return strcmp(f1->fname,f2->fname);
1557 static const char *process_resource_config_nofnmatch(server_rec *s,
1558 const char *fname,
1559 ap_directive_t **conftree,
1560 apr_pool_t *p,
1561 apr_pool_t *ptemp,
1562 unsigned depth)
1564 cmd_parms parms;
1565 ap_configfile_t *cfp;
1566 const char *error;
1567 apr_status_t rv;
1569 if (ap_is_directory(p, fname)) {
1570 apr_dir_t *dirp;
1571 apr_finfo_t dirent;
1572 int current;
1573 apr_array_header_t *candidates = NULL;
1574 fnames *fnew;
1575 char *path = apr_pstrdup(p, fname);
1577 if (++depth > AP_MAX_INCLUDE_DIR_DEPTH) {
1578 return apr_psprintf(p, "Directory %s exceeds the maximum include "
1579 "directory nesting level of %u. You have "
1580 "probably a recursion somewhere.", path,
1581 AP_MAX_INCLUDE_DIR_DEPTH);
1585 * first course of business is to grok all the directory
1586 * entries here and store 'em away. Recall we need full pathnames
1587 * for this.
1589 rv = apr_dir_open(&dirp, path, p);
1590 if (rv != APR_SUCCESS) {
1591 char errmsg[120];
1592 return apr_psprintf(p, "Could not open config directory %s: %s",
1593 path, apr_strerror(rv, errmsg, sizeof errmsg));
1596 candidates = apr_array_make(p, 1, sizeof(fnames));
1597 while (apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp) == APR_SUCCESS) {
1598 /* strip out '.' and '..' */
1599 if (strcmp(dirent.name, ".")
1600 && strcmp(dirent.name, "..")) {
1601 fnew = (fnames *) apr_array_push(candidates);
1602 fnew->fname = ap_make_full_path(p, path, dirent.name);
1606 apr_dir_close(dirp);
1607 if (candidates->nelts != 0) {
1608 qsort((void *) candidates->elts, candidates->nelts,
1609 sizeof(fnames), fname_alphasort);
1612 * Now recurse these... we handle errors and subdirectories
1613 * via the recursion, which is nice
1615 for (current = 0; current < candidates->nelts; ++current) {
1616 fnew = &((fnames *) candidates->elts)[current];
1617 error = process_resource_config_nofnmatch(s, fnew->fname,
1618 conftree, p, ptemp,
1619 depth);
1620 if (error) {
1621 return error;
1626 return NULL;
1629 /* GCC's initialization extensions are soooo nice here... */
1630 parms = default_parms;
1631 parms.pool = p;
1632 parms.temp_pool = ptemp;
1633 parms.server = s;
1634 parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1635 parms.override_opts = OPT_ALL | OPT_INCNOEXEC | OPT_SYM_OWNER | OPT_MULTI;
1637 rv = ap_pcfg_openfile(&cfp, p, fname);
1638 if (rv != APR_SUCCESS) {
1639 char errmsg[120];
1640 return apr_psprintf(p, "Could not open configuration file %s: %s",
1641 fname, apr_strerror(rv, errmsg, sizeof errmsg));
1644 parms.config_file = cfp;
1645 error = ap_build_config(&parms, p, ptemp, conftree);
1646 ap_cfg_closefile(cfp);
1648 if (error) {
1649 return apr_psprintf(p, "Syntax error on line %d of %s: %s",
1650 parms.err_directive->line_num,
1651 parms.err_directive->filename, error);
1654 return NULL;
1657 AP_DECLARE(const char *) ap_process_resource_config(server_rec *s,
1658 const char *fname,
1659 ap_directive_t **conftree,
1660 apr_pool_t *p,
1661 apr_pool_t *ptemp)
1663 /* XXX: lstat() won't work on the wildcard pattern...
1666 /* don't require conf/httpd.conf if we have a -C or -c switch */
1667 if ((ap_server_pre_read_config->nelts
1668 || ap_server_post_read_config->nelts)
1669 && !(strcmp(fname, ap_server_root_relative(p, SERVER_CONFIG_FILE)))) {
1670 apr_finfo_t finfo;
1672 if (apr_stat(&finfo, fname, APR_FINFO_LINK | APR_FINFO_TYPE, p) != APR_SUCCESS)
1673 return NULL;
1676 if (!apr_fnmatch_test(fname)) {
1677 return process_resource_config_nofnmatch(s, fname, conftree, p, ptemp,
1680 else {
1681 apr_dir_t *dirp;
1682 apr_finfo_t dirent;
1683 int current;
1684 apr_array_header_t *candidates = NULL;
1685 fnames *fnew;
1686 apr_status_t rv;
1687 char *path = apr_pstrdup(p, fname), *pattern = NULL;
1689 pattern = ap_strrchr(path, '/');
1691 AP_DEBUG_ASSERT(pattern != NULL); /* path must be absolute. */
1693 *pattern++ = '\0';
1695 if (apr_fnmatch_test(path)) {
1696 return apr_pstrcat(p, "Wildcard patterns not allowed in Include ",
1697 fname, NULL);
1700 if (!ap_is_directory(p, path)){
1701 return apr_pstrcat(p, "Include directory '", path, "' not found",
1702 NULL);
1705 if (!apr_fnmatch_test(pattern)) {
1706 return apr_pstrcat(p, "Must include a wildcard pattern for "
1707 "Include ", fname, NULL);
1711 * first course of business is to grok all the directory
1712 * entries here and store 'em away. Recall we need full pathnames
1713 * for this.
1715 rv = apr_dir_open(&dirp, path, p);
1716 if (rv != APR_SUCCESS) {
1717 char errmsg[120];
1718 return apr_psprintf(p, "Could not open config directory %s: %s",
1719 path, apr_strerror(rv, errmsg, sizeof errmsg));
1722 candidates = apr_array_make(p, 1, sizeof(fnames));
1723 while (apr_dir_read(&dirent, APR_FINFO_DIRENT, dirp) == APR_SUCCESS) {
1724 /* strip out '.' and '..' */
1725 if (strcmp(dirent.name, ".")
1726 && strcmp(dirent.name, "..")
1727 && (apr_fnmatch(pattern, dirent.name,
1728 APR_FNM_PERIOD) == APR_SUCCESS)) {
1729 fnew = (fnames *) apr_array_push(candidates);
1730 fnew->fname = ap_make_full_path(p, path, dirent.name);
1734 apr_dir_close(dirp);
1735 if (candidates->nelts != 0) {
1736 const char *error;
1738 qsort((void *) candidates->elts, candidates->nelts,
1739 sizeof(fnames), fname_alphasort);
1742 * Now recurse these... we handle errors and subdirectories
1743 * via the recursion, which is nice
1745 for (current = 0; current < candidates->nelts; ++current) {
1746 fnew = &((fnames *) candidates->elts)[current];
1747 error = process_resource_config_nofnmatch(s, fnew->fname,
1748 conftree, p,
1749 ptemp, 0);
1750 if (error) {
1751 return error;
1757 return NULL;
1760 AP_DECLARE(int) ap_process_config_tree(server_rec *s,
1761 ap_directive_t *conftree,
1762 apr_pool_t *p,
1763 apr_pool_t *ptemp)
1765 const char *errmsg;
1766 cmd_parms parms;
1768 parms = default_parms;
1769 parms.pool = p;
1770 parms.temp_pool = ptemp;
1771 parms.server = s;
1772 parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1773 parms.override_opts = OPT_ALL | OPT_INCNOEXEC | OPT_SYM_OWNER | OPT_MULTI;
1774 parms.limited = -1;
1776 errmsg = ap_walk_config(conftree, &parms, s->lookup_defaults);
1777 if (errmsg) {
1778 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0, p,
1779 "Syntax error on line %d of %s:",
1780 parms.err_directive->line_num,
1781 parms.err_directive->filename);
1782 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0, p,
1783 "%s", errmsg);
1784 return HTTP_INTERNAL_SERVER_ERROR;
1787 return OK;
1790 AP_CORE_DECLARE(int) ap_parse_htaccess(ap_conf_vector_t **result,
1791 request_rec *r, int override,
1792 int override_opts,
1793 const char *d, const char *access_name)
1795 ap_configfile_t *f = NULL;
1796 cmd_parms parms;
1797 char *filename = NULL;
1798 const struct htaccess_result *cache;
1799 struct htaccess_result *new;
1800 ap_conf_vector_t *dc = NULL;
1801 apr_status_t status;
1803 /* firstly, search cache */
1804 for (cache = r->htaccess; cache != NULL; cache = cache->next) {
1805 if (cache->override == override && strcmp(cache->dir, d) == 0) {
1806 *result = cache->htaccess;
1807 return OK;
1811 parms = default_parms;
1812 parms.override = override;
1813 parms.override_opts = override_opts;
1814 parms.pool = r->pool;
1815 parms.temp_pool = r->pool;
1816 parms.server = r->server;
1817 parms.path = apr_pstrdup(r->pool, d);
1819 /* loop through the access names and find the first one */
1820 while (access_name[0]) {
1821 /* AFAICT; there is no use of the actual 'filename' against
1822 * any canonicalization, so we will simply take the given
1823 * name, ignoring case sensitivity and aliases
1825 filename = ap_make_full_path(r->pool, d,
1826 ap_getword_conf(r->pool, &access_name));
1827 status = ap_pcfg_openfile(&f, r->pool, filename);
1829 if (status == APR_SUCCESS) {
1830 const char *errmsg;
1831 ap_directive_t *temptree = NULL;
1833 dc = ap_create_per_dir_config(r->pool);
1835 parms.config_file = f;
1836 errmsg = ap_build_config(&parms, r->pool, r->pool, &temptree);
1837 if (errmsg == NULL)
1838 errmsg = ap_walk_config(temptree, &parms, dc);
1840 ap_cfg_closefile(f);
1842 if (errmsg) {
1843 ap_log_rerror(APLOG_MARK, APLOG_ALERT, 0, r,
1844 "%s: %s", filename, errmsg);
1845 return HTTP_INTERNAL_SERVER_ERROR;
1848 *result = dc;
1849 break;
1851 else {
1852 if (!APR_STATUS_IS_ENOENT(status)
1853 && !APR_STATUS_IS_ENOTDIR(status)) {
1854 ap_log_rerror(APLOG_MARK, APLOG_CRIT, status, r,
1855 "%s pcfg_openfile: unable to check htaccess file, "
1856 "ensure it is readable",
1857 filename);
1858 apr_table_setn(r->notes, "error-notes",
1859 "Server unable to read htaccess file, denying "
1860 "access to be safe");
1861 return HTTP_FORBIDDEN;
1866 /* cache it */
1867 new = apr_palloc(r->pool, sizeof(struct htaccess_result));
1868 new->dir = parms.path;
1869 new->override = override;
1870 new->override_opts = override_opts;
1871 new->htaccess = dc;
1873 /* add to head of list */
1874 new->next = r->htaccess;
1875 r->htaccess = new;
1877 return OK;
1880 AP_CORE_DECLARE(const char *) ap_init_virtual_host(apr_pool_t *p,
1881 const char *hostname,
1882 server_rec *main_server,
1883 server_rec **ps)
1885 server_rec *s = (server_rec *) apr_pcalloc(p, sizeof(server_rec));
1887 /* TODO: this crap belongs in http_core */
1888 s->process = main_server->process;
1889 s->server_admin = NULL;
1890 s->server_hostname = NULL;
1891 s->server_scheme = NULL;
1892 s->error_fname = NULL;
1893 s->timeout = 0;
1894 s->keep_alive_timeout = 0;
1895 s->keep_alive = -1;
1896 s->keep_alive_max = -1;
1897 s->error_log = main_server->error_log;
1898 s->loglevel = main_server->loglevel;
1899 /* useful default, otherwise we get a port of 0 on redirects */
1900 s->port = main_server->port;
1901 s->next = NULL;
1903 s->is_virtual = 1;
1904 s->names = apr_array_make(p, 4, sizeof(char **));
1905 s->wild_names = apr_array_make(p, 4, sizeof(char **));
1907 s->module_config = create_empty_config(p);
1908 s->lookup_defaults = ap_create_per_dir_config(p);
1910 s->limit_req_line = main_server->limit_req_line;
1911 s->limit_req_fieldsize = main_server->limit_req_fieldsize;
1912 s->limit_req_fields = main_server->limit_req_fields;
1914 *ps = s;
1916 return ap_parse_vhost_addrs(p, hostname, s);
1920 AP_DECLARE(void) ap_fixup_virtual_hosts(apr_pool_t *p, server_rec *main_server)
1922 server_rec *virt;
1924 for (virt = main_server->next; virt; virt = virt->next) {
1925 merge_server_configs(p, main_server->module_config,
1926 virt->module_config);
1928 virt->lookup_defaults =
1929 ap_merge_per_dir_configs(p, main_server->lookup_defaults,
1930 virt->lookup_defaults);
1932 if (virt->server_admin == NULL)
1933 virt->server_admin = main_server->server_admin;
1935 if (virt->timeout == 0)
1936 virt->timeout = main_server->timeout;
1938 if (virt->keep_alive_timeout == 0)
1939 virt->keep_alive_timeout = main_server->keep_alive_timeout;
1941 if (virt->keep_alive == -1)
1942 virt->keep_alive = main_server->keep_alive;
1944 if (virt->keep_alive_max == -1)
1945 virt->keep_alive_max = main_server->keep_alive_max;
1947 /* XXX: this is really something that should be dealt with by a
1948 * post-config api phase
1950 ap_core_reorder_directories(p, virt);
1953 ap_core_reorder_directories(p, main_server);
1956 /*****************************************************************
1958 * Getting *everything* configured...
1961 static void init_config_globals(apr_pool_t *p)
1963 /* Global virtual host hash bucket pointers. Init to null. */
1964 ap_init_vhost_config(p);
1967 static server_rec *init_server_config(process_rec *process, apr_pool_t *p)
1969 apr_status_t rv;
1970 server_rec *s = (server_rec *) apr_pcalloc(p, sizeof(server_rec));
1972 apr_file_open_stderr(&s->error_log, p);
1973 s->process = process;
1974 s->port = 0;
1975 s->server_admin = DEFAULT_ADMIN;
1976 s->server_hostname = NULL;
1977 s->server_scheme = NULL;
1978 s->error_fname = DEFAULT_ERRORLOG;
1979 s->loglevel = DEFAULT_LOGLEVEL;
1980 s->limit_req_line = DEFAULT_LIMIT_REQUEST_LINE;
1981 s->limit_req_fieldsize = DEFAULT_LIMIT_REQUEST_FIELDSIZE;
1982 s->limit_req_fields = DEFAULT_LIMIT_REQUEST_FIELDS;
1983 s->timeout = apr_time_from_sec(DEFAULT_TIMEOUT);
1984 s->keep_alive_timeout = apr_time_from_sec(DEFAULT_KEEPALIVE_TIMEOUT);
1985 s->keep_alive_max = DEFAULT_KEEPALIVE;
1986 s->keep_alive = 1;
1987 s->next = NULL;
1988 s->addrs = apr_pcalloc(p, sizeof(server_addr_rec));
1990 /* NOT virtual host; don't match any real network interface */
1991 rv = apr_sockaddr_info_get(&s->addrs->host_addr,
1992 NULL, APR_INET, 0, 0, p);
1993 ap_assert(rv == APR_SUCCESS); /* otherwise: bug or no storage */
1995 s->addrs->host_port = 0; /* matches any port */
1996 s->addrs->virthost = ""; /* must be non-NULL */
1997 s->names = s->wild_names = NULL;
1999 s->module_config = create_server_config(p, s);
2000 s->lookup_defaults = create_default_per_dir_config(p);
2002 return s;
2006 AP_DECLARE(server_rec*) ap_read_config(process_rec *process, apr_pool_t *ptemp,
2007 const char *filename,
2008 ap_directive_t **conftree)
2010 const char *confname, *error;
2011 apr_pool_t *p = process->pconf;
2012 server_rec *s = init_server_config(process, p);
2014 init_config_globals(p);
2016 /* All server-wide config files now have the SAME syntax... */
2017 error = process_command_config(s, ap_server_pre_read_config, conftree,
2018 p, ptemp);
2019 if (error) {
2020 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL, "%s: %s",
2021 ap_server_argv0, error);
2022 return NULL;
2025 /* process_command_config may change the ServerRoot so
2026 * compute this config file name afterwards.
2028 confname = ap_server_root_relative(p, filename);
2030 if (!confname) {
2031 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT,
2032 APR_EBADPATH, NULL, "Invalid config file path %s",
2033 filename);
2034 return NULL;
2037 error = ap_process_resource_config(s, confname, conftree, p, ptemp);
2038 if (error) {
2039 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL,
2040 "%s: %s", ap_server_argv0, error);
2041 return NULL;
2044 error = process_command_config(s, ap_server_post_read_config, conftree,
2045 p, ptemp);
2047 if (error) {
2048 ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_CRIT, 0, NULL, "%s: %s",
2049 ap_server_argv0, error);
2050 return NULL;
2053 return s;
2056 AP_DECLARE(void) ap_single_module_configure(apr_pool_t *p, server_rec *s,
2057 module *m)
2059 if (m->create_server_config)
2060 ap_set_module_config(s->module_config, m,
2061 (*m->create_server_config)(p, s));
2063 if (m->create_dir_config)
2064 ap_set_module_config(s->lookup_defaults, m,
2065 (*m->create_dir_config)(p, NULL));
2068 AP_DECLARE(void) ap_run_rewrite_args(process_rec *process)
2070 module *m;
2072 for (m = ap_top_module; m; m = m->next) {
2073 if (m->rewrite_args) {
2074 (*m->rewrite_args)(process);
2079 /********************************************************************
2080 * Configuration directives are restricted in terms of where they may
2081 * appear in the main configuration files and/or .htaccess files according
2082 * to the bitmask req_override in the command_rec structure.
2083 * If any of the overrides set in req_override are also allowed in the
2084 * context in which the command is read, then the command is allowed.
2085 * The context is determined as follows:
2087 * inside *.conf --> override = (RSRC_CONF|OR_ALL)&~(OR_AUTHCFG|OR_LIMIT);
2088 * within <Directory> or <Location> --> override = OR_ALL|ACCESS_CONF;
2089 * within .htaccess --> override = AllowOverride for current directory;
2091 * the result is, well, a rather confusing set of possibilities for when
2092 * a particular directive is allowed to be used. This procedure prints
2093 * in English where the given (pc) directive can be used.
2095 static void show_overrides(const command_rec *pc, module *pm)
2097 int n = 0;
2099 printf("\tAllowed in *.conf ");
2100 if ((pc->req_override & (OR_OPTIONS | OR_FILEINFO | OR_INDEXES))
2101 || ((pc->req_override & RSRC_CONF)
2102 && ((pc->req_override & (ACCESS_CONF | OR_AUTHCFG | OR_LIMIT))))) {
2103 printf("anywhere");
2105 else if (pc->req_override & RSRC_CONF) {
2106 printf("only outside <Directory>, <Files> or <Location>");
2108 else {
2109 printf("only inside <Directory>, <Files> or <Location>");
2112 /* Warn if the directive is allowed inside <Directory> or .htaccess
2113 * but module doesn't support per-dir configuration
2115 if ((pc->req_override & (OR_ALL | ACCESS_CONF)) && !pm->create_dir_config)
2116 printf(" [no per-dir config]");
2118 if (pc->req_override & OR_ALL) {
2119 printf(" and in .htaccess\n\twhen AllowOverride");
2121 if ((pc->req_override & OR_ALL) == OR_ALL) {
2122 printf(" isn't None");
2124 else {
2125 printf(" includes ");
2127 if (pc->req_override & OR_AUTHCFG) {
2128 if (n++)
2129 printf(" or ");
2131 printf("AuthConfig");
2134 if (pc->req_override & OR_LIMIT) {
2135 if (n++)
2136 printf(" or ");
2138 printf("Limit");
2141 if (pc->req_override & OR_OPTIONS) {
2142 if (n++)
2143 printf(" or ");
2145 printf("Options");
2148 if (pc->req_override & OR_FILEINFO) {
2149 if (n++)
2150 printf(" or ");
2152 printf("FileInfo");
2155 if (pc->req_override & OR_INDEXES) {
2156 if (n++)
2157 printf(" or ");
2159 printf("Indexes");
2164 printf("\n");
2167 /* Show the preloaded configuration directives, the help string explaining
2168 * the directive arguments, in what module they are handled, and in
2169 * what parts of the configuration they are allowed. Used for httpd -L.
2171 AP_DECLARE(void) ap_show_directives(void)
2173 const command_rec *pc;
2174 int n;
2176 for (n = 0; ap_loaded_modules[n]; ++n) {
2177 for (pc = ap_loaded_modules[n]->cmds; pc && pc->name; ++pc) {
2178 printf("%s (%s)\n", pc->name, ap_loaded_modules[n]->name);
2180 if (pc->errmsg)
2181 printf("\t%s\n", pc->errmsg);
2183 show_overrides(pc, ap_loaded_modules[n]);
2188 /* Show the preloaded module names. Used for httpd -l. */
2189 AP_DECLARE(void) ap_show_modules(void)
2191 int n;
2193 printf("Compiled in modules:\n");
2194 for (n = 0; ap_loaded_modules[n]; ++n)
2195 printf(" %s\n", ap_loaded_modules[n]->name);
2198 AP_DECLARE(const char *) ap_show_mpm(void)
2200 return MPM_NAME;