1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
60 * http_config.c: once was auxillary functions for reading httpd's config
61 * file and converting filenames into a namespace
65 * Wall-to-wall rewrite for Apache... commands which are part of the
66 * server core can now be found next door in "http_core.c". Now contains
67 * general command loop, and functions which do bookkeeping for the new
68 * Apache config stuff (modules and configuration vectors).
76 #include "ap_config.h"
77 #include "apr_portable.h"
78 #include "apr_file_io.h"
80 #include "http_config.h"
81 #include "http_core.h"
82 #include "http_log.h" /* for errors in parse_htaccess */
83 #include "http_request.h" /* for default_handler (see invoke_handler) */
84 #include "http_main.h"
85 #include "http_vhost.h"
88 HOOK_LINK(header_parser)
89 HOOK_LINK(post_config)
94 IMPLEMENT_HOOK_RUN_ALL(int,header_parser,(request_rec *r),(r),OK,DECLINED)
95 IMPLEMENT_HOOK_VOID(post_config,
96 (ap_context_t *pconf, ap_context_t *plog, ap_context_t *ptemp, server_rec *s),
98 IMPLEMENT_HOOK_VOID(open_logs,
99 (ap_context_t *pconf, ap_context_t *plog, ap_context_t *ptemp, server_rec *s),
100 (pconf,plog,ptemp,s))
101 IMPLEMENT_HOOK_VOID(child_init,(ap_context_t *pchild, server_rec *s),(pchild,s))
103 /****************************************************************
105 * We begin with the functions which deal with the linked list
106 * of modules which control just about all of the server operation.
109 /* total_modules is the number of modules that have been linked
112 static int total_modules = 0;
113 /* dynamic_modules is the number of modules that have been added
114 * after the pre-loaded ones have been set up. It shouldn't be larger
115 * than DYNAMIC_MODULE_LIMIT.
117 static int dynamic_modules = 0;
118 API_VAR_EXPORT module *top_module = NULL;
119 API_VAR_EXPORT module **ap_loaded_modules=NULL;
121 typedef int (*handler_func) (request_rec *);
122 typedef void *(*dir_maker_func) (ap_context_t *, char *);
123 typedef void *(*merger_func) (ap_context_t *, void *, void *);
125 /* Dealing with config vectors. These are associated with per-directory,
126 * per-server, and per-request configuration, and have a void* pointer for
127 * each modules. The nature of the structure pointed to is private to the
128 * module in question... the core doesn't (and can't) know. However, there
129 * are defined interfaces which allow it to create instances of its private
130 * per-directory and per-server structures, and to merge the per-directory
131 * structures of a directory and its subdirectory (producing a new one in
132 * which the defaults applying to the base directory have been properly
136 #ifndef ap_get_module_config
137 API_EXPORT(void *) ap_get_module_config(void *conf_vector, module *m)
139 void **confv = (void **) conf_vector;
140 return confv[m->module_index];
144 #ifndef ap_set_module_config
145 API_EXPORT(void) ap_set_module_config(void *conf_vector, module *m, void *val)
147 void **confv = (void **) conf_vector;
148 confv[m->module_index] = val;
152 static void *create_empty_config(ap_context_t *p)
154 void **conf_vector = (void **) ap_pcalloc(p, sizeof(void *) *
155 (total_modules + DYNAMIC_MODULE_LIMIT));
156 return (void *) conf_vector;
159 static void *create_default_per_dir_config(ap_context_t *p)
161 void **conf_vector = (void **) ap_pcalloc(p, sizeof(void *) * (total_modules + DYNAMIC_MODULE_LIMIT));
164 for (modp = top_module; modp; modp = modp->next) {
165 dir_maker_func df = modp->create_dir_config;
168 conf_vector[modp->module_index] = (*df) (p, NULL);
171 return (void *) conf_vector;
175 ap_merge_per_dir_configs(ap_context_t *p, void *base, void *new)
177 void **conf_vector = (void **) ap_palloc(p, sizeof(void *) * total_modules);
178 void **base_vector = (void **) base;
179 void **new_vector = (void **) new;
182 for (modp = top_module; modp; modp = modp->next) {
183 merger_func df = modp->merge_dir_config;
184 int i = modp->module_index;
186 if (df && new_vector[i])
187 conf_vector[i] = (*df) (p, base_vector[i], new_vector[i]);
189 conf_vector[i] = new_vector[i] ? new_vector[i] : base_vector[i];
192 return (void *) conf_vector;
195 static void *create_server_config(ap_context_t *p, server_rec *s)
197 void **conf_vector = (void **) ap_pcalloc(p, sizeof(void *) * (total_modules + DYNAMIC_MODULE_LIMIT));
200 for (modp = top_module; modp; modp = modp->next) {
201 if (modp->create_server_config)
202 conf_vector[modp->module_index] = (*modp->create_server_config) (p, s);
205 return (void *) conf_vector;
208 static void merge_server_configs(ap_context_t *p, void *base, void *virt)
210 /* Can reuse the 'virt' vector for the spine of it, since we don't
211 * have to deal with the moral equivalent of .htaccess files here...
214 void **base_vector = (void **) base;
215 void **virt_vector = (void **) virt;
218 for (modp = top_module; modp; modp = modp->next) {
219 merger_func df = modp->merge_server_config;
220 int i = modp->module_index;
223 virt_vector[i] = base_vector[i];
225 virt_vector[i] = (*df) (p, base_vector[i], virt_vector[i]);
229 void *ap_create_request_config(ap_context_t *p)
231 return create_empty_config(p);
234 void *ap_create_conn_config(ap_context_t *p)
236 return create_empty_config(p);
239 CORE_EXPORT(void *) ap_create_per_dir_config(ap_context_t *p)
241 return create_empty_config(p);
245 * For speed/efficiency we generate a compact list of all the handlers
246 * and wildcard handlers. This means we won't have to scan the entire
247 * module list looking for handlers... where we'll find a whole whack
255 static fast_handler_rec *handlers;
256 static fast_handler_rec *wildhandlers;
258 static void init_handlers(ap_context_t *p)
262 int nwildhandlers = 0;
263 const handler_rec *handp;
264 fast_handler_rec *ph, *pw;
267 for (modp = top_module; modp; modp = modp->next) {
270 for (handp = modp->handlers; handp->content_type; ++handp) {
271 if (strchr(handp->content_type, '*')) {
278 ph = handlers = ap_palloc(p, sizeof(*ph)*(nhandlers + 1));
279 pw = wildhandlers = ap_palloc(p, sizeof(*pw)*(nwildhandlers + 1));
280 for (modp = top_module; modp; modp = modp->next) {
283 for (handp = modp->handlers; handp->content_type; ++handp) {
284 if ((starp = strchr(handp->content_type, '*'))) {
285 pw->hr.content_type = handp->content_type;
286 pw->hr.handler = handp->handler;
287 pw->len = starp - handp->content_type;
290 ph->hr.content_type = handp->content_type;
291 ph->hr.handler = handp->handler;
292 ph->len = strlen(handp->content_type);
297 pw->hr.content_type = NULL;
298 pw->hr.handler = NULL;
299 ph->hr.content_type = NULL;
300 ph->hr.handler = NULL;
303 int ap_invoke_handler(request_rec *r)
305 fast_handler_rec *handp;
309 int result = HTTP_INTERNAL_SERVER_ERROR;
312 handler = r->handler;
313 handler_len = strlen(handler);
316 handler = r->content_type ? r->content_type : ap_default_type(r);
317 if ((p = strchr(handler, ';')) != NULL) { /* MIME type arguments */
318 while (p > handler && p[-1] == ' ')
319 --p; /* strip trailing spaces */
320 handler_len = p - handler;
323 handler_len = strlen(handler);
327 /* Pass one --- direct matches */
329 for (handp = handlers; handp->hr.content_type; ++handp) {
330 if (handler_len == handp->len
331 && !strncmp(handler, handp->hr.content_type, handler_len)) {
332 result = (*handp->hr.handler) (r);
334 if (result != DECLINED)
339 /* Pass two --- wildcard matches */
341 for (handp = wildhandlers; handp->hr.content_type; ++handp) {
342 if (handler_len >= handp->len
343 && !strncmp(handler, handp->hr.content_type, handp->len)) {
344 result = (*handp->hr.handler) (r);
346 if (result != DECLINED)
351 if (result == HTTP_INTERNAL_SERVER_ERROR && r->handler && r->filename) {
352 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_WARNING, 0, r,
353 "handler \"%s\" not found for: %s", r->handler, r->filename);
355 return HTTP_INTERNAL_SERVER_ERROR;
359 const char *g_szCurrentHookName;
361 void ap_register_hooks(module *m)
363 if(m->register_hooks)
365 if(getenv("SHOW_HOOKS"))
367 printf("Registering hooks for %s\n",m->name);
370 g_szCurrentHookName=m->name;
375 /* One-time setup for precompiled modules --- NOT to be done on restart */
377 API_EXPORT(void) ap_add_module(module *m)
379 /* This could be called from an AddModule httpd.conf command,
380 * after the file has been linked and the module structure within it
384 if (m->version != MODULE_MAGIC_NUMBER_MAJOR) {
385 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
386 "%s: module \"%s\" is not compatible with this "
387 "version of Apache.", ap_server_argv0, m->name);
388 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL, "Please contact the vendor for the correct version.");
392 if (m->next == NULL) {
393 m->next = top_module;
396 if (m->module_index == -1) {
397 m->module_index = total_modules++;
400 if (dynamic_modules > DYNAMIC_MODULE_LIMIT) {
401 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
402 "%s: module \"%s\" could not be loaded, because"
403 " the dynamic", ap_server_argv0, m->name);
404 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
405 "module limit was reached. Please increase "
406 "DYNAMIC_MODULE_LIMIT and recompile.");
411 /* Some C compilers put a complete path into __FILE__, but we want
412 * only the filename (e.g. mod_includes.c). So check for path
413 * components (Unix and DOS), and remove them.
416 if (strrchr(m->name, '/'))
417 m->name = 1 + strrchr(m->name, '/');
418 if (strrchr(m->name, '\\'))
419 m->name = 1 + strrchr(m->name, '\\');
421 #ifdef _OSD_POSIX /* __FILE__="*POSIX(/home/martin/apache/src/modules/standard/mod_info.c)" */
422 /* We cannot fix the string in-place, because it's const */
423 if (m->name[strlen(m->name)-1]==')') {
424 char *tmp = strdup(m->name); /* FIXME:memory leak, albeit a small one */
425 tmp[strlen(tmp)-1] = '\0';
428 #endif /*_OSD_POSIX*/
430 /* FIXME: is this the right place to call this? */
431 ap_register_hooks(m);
435 * remove_module undoes what add_module did. There are some caveats:
436 * when the module is removed, its slot is lost so all the current
437 * per-dir and per-server configurations are invalid. So we should
438 * only ever call this function when you are invalidating almost
439 * all our current data. I.e. when doing a restart.
442 API_EXPORT(void) ap_remove_module(module *m)
448 /* We are the top module, special case */
449 top_module = modp->next;
453 /* Not the top module, find use. When found modp will
454 * point to the module _before_ us in the list
457 while (modp && modp->next != m) {
461 /* Uh-oh, this module doesn't exist */
462 ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, NULL,
463 "Cannot remove module %s: not found in module list",
467 /* Eliminate us from the module list */
468 modp->next = modp->next->next;
471 m->module_index = -1; /* simulate being unloaded, should
477 API_EXPORT(void) ap_add_loaded_module(module *mod)
482 * Add module pointer to top of chained module list
487 * And module pointer to list of loaded modules
489 * Notes: 1. ap_add_module() would already complain if no more space
490 * exists for adding a dynamically loaded module
491 * 2. ap_add_module() accepts double inclusion, so we have
492 * to accept this, too.
494 for (m = ap_loaded_modules; *m != NULL; m++)
500 API_EXPORT(void) ap_remove_loaded_module(module *mod)
507 * Remove module pointer from chained module list
509 ap_remove_module(mod);
512 * Remove module pointer from list of loaded modules
514 * Note: 1. We cannot determine if the module was successfully
515 * removed by ap_remove_module().
516 * 2. We have not to complain explicity when the module
517 * is not found because ap_remove_module() did it
520 for (m = m2 = ap_loaded_modules, done = 0; *m2 != NULL; m2++) {
521 if (*m2 == mod && done == 0)
529 void ap_setup_prelinked_modules(process_rec *process)
535 * Initialise total_modules variable and module indices
538 for (m = ap_preloaded_modules; *m != NULL; m++)
539 (*m)->module_index = total_modules++;
542 * Initialise list of loaded modules
544 ap_loaded_modules = (module **)ap_palloc(process->pool,
545 sizeof(module *)*(total_modules+DYNAMIC_MODULE_LIMIT+1));
546 if (ap_loaded_modules == NULL) {
547 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
548 "Ouch! Out of memory in ap_setup_prelinked_modules()!");
550 for (m = ap_preloaded_modules, m2 = ap_loaded_modules; *m != NULL; )
555 * Initialize chain of linked (=activate) modules
557 for (m = ap_prelinked_modules; *m != NULL; m++)
563 API_EXPORT(const char *) ap_find_module_name(module *m)
568 API_EXPORT(module *) ap_find_linked_module(const char *name)
572 for (modp = top_module; modp; modp = modp->next) {
573 if (strcmp(modp->name, name) == 0)
579 /* Add a named module. Returns 1 if module found, 0 otherwise. */
580 API_EXPORT(int) ap_add_named_module(const char *name)
585 for (modp = ap_loaded_modules[i]; modp; modp = ap_loaded_modules[++i]) {
586 if (strcmp(modp->name, name) == 0) {
587 /* Only add modules that are not already enabled. */
588 if (modp->next == NULL) {
598 /* Clear the internal list of modules, in preparation for starting over. */
599 API_EXPORT(void) ap_clear_module_list()
601 module **m = &top_module;
605 next_m = &((*m)->next);
610 /* This is required; so we add it always. */
611 ap_add_named_module("http_core.c");
614 /*****************************************************************
616 * Resource, access, and .htaccess config files now parsed by a common
619 * Let's begin with the basics; parsing the line and
620 * invoking the function...
623 static const char *invoke_cmd(const command_rec *cmd, cmd_parms *parms,
624 void *mconfig, const char *args)
629 if ((parms->override & cmd->req_override) == 0)
630 return ap_pstrcat(parms->pool, cmd->name, " not allowed here", NULL);
632 parms->info = cmd->cmd_data;
635 switch (cmd->args_how) {
637 #ifdef RESOLVE_ENV_PER_TOKEN
638 args = ap_resolve_env(parms->pool,args);
640 return ((const char *(*)(cmd_parms *, void *, const char *))
641 (cmd->func)) (parms, mconfig, args);
645 return ap_pstrcat(parms->pool, cmd->name, " takes no arguments",
648 return ((const char *(*)(cmd_parms *, void *))
649 (cmd->func)) (parms, mconfig);
652 w = ap_getword_conf(parms->pool, &args);
654 if (*w == '\0' || *args != 0)
655 return ap_pstrcat(parms->pool, cmd->name, " takes one argument",
656 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
658 return ((const char *(*)(cmd_parms *, void *, const char *))
659 (cmd->func)) (parms, mconfig, w);
663 w = ap_getword_conf(parms->pool, &args);
664 w2 = ap_getword_conf(parms->pool, &args);
666 if (*w == '\0' || *w2 == '\0' || *args != 0)
667 return ap_pstrcat(parms->pool, cmd->name, " takes two arguments",
668 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
670 return ((const char *(*)(cmd_parms *, void *, const char *,
671 const char *)) (cmd->func)) (parms, mconfig, w, w2);
675 w = ap_getword_conf(parms->pool, &args);
676 w2 = ap_getword_conf(parms->pool, &args);
678 if (*w == '\0' || *args != 0)
679 return ap_pstrcat(parms->pool, cmd->name, " takes 1-2 arguments",
680 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
682 return ((const char *(*)(cmd_parms *, void *, const char *,
683 const char *)) (cmd->func)) (parms, mconfig, w,
688 w = ap_getword_conf(parms->pool, &args);
689 w2 = ap_getword_conf(parms->pool, &args);
690 w3 = ap_getword_conf(parms->pool, &args);
692 if (*w == '\0' || *w2 == '\0' || *w3 == '\0' || *args != 0)
693 return ap_pstrcat(parms->pool, cmd->name, " takes three arguments",
694 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
696 return ((const char *(*)(cmd_parms *, void *, const char *,
697 const char *, const char *)) (cmd->func)) (parms,
702 w = ap_getword_conf(parms->pool, &args);
703 w2 = ap_getword_conf(parms->pool, &args);
704 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
706 if (*w == '\0' || *w2 == '\0' || *args != 0)
707 return ap_pstrcat(parms->pool, cmd->name,
708 " takes two or three arguments",
709 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
711 return ((const char *(*)(cmd_parms *, void *, const char *,
712 const char *, const char *)) (cmd->func)) (parms,
717 w = ap_getword_conf(parms->pool, &args);
718 w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
719 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
721 if (*w == '\0' || *args != 0)
722 return ap_pstrcat(parms->pool, cmd->name,
723 " takes one, two or three arguments",
724 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
726 return ((const char *(*)(cmd_parms *, void *, const char *,
727 const char *, const char *)) (cmd->func)) (parms,
732 w = ap_getword_conf(parms->pool, &args);
733 w2 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
734 w3 = *args ? ap_getword_conf(parms->pool, &args) : NULL;
736 if (*w == '\0' || (*w2 && !w3) || *args != 0)
737 return ap_pstrcat(parms->pool, cmd->name,
738 " takes one or three arguments",
739 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
741 return ((const char *(*)(cmd_parms *, void *, const char *,
742 const char *, const char *)) (cmd->func)) (parms,
747 while (*(w = ap_getword_conf(parms->pool, &args)) != '\0')
748 if ((errmsg = ((const char *(*)(cmd_parms *, void *,
749 const char *)) (cmd->func)) (parms, mconfig, w)))
756 w = ap_getword_conf(parms->pool, &args);
758 if (*w == '\0' || *args == 0)
759 return ap_pstrcat(parms->pool, cmd->name,
760 " requires at least two arguments",
761 cmd->errmsg ? ", " : NULL, cmd->errmsg, NULL);
764 while (*(w2 = ap_getword_conf(parms->pool, &args)) != '\0')
765 if ((errmsg = ((const char *(*)(cmd_parms *, void *,
766 const char *, const char *)) (cmd->func)) (parms,
774 w = ap_getword_conf(parms->pool, &args);
776 if (*w == '\0' || (strcasecmp(w, "on") && strcasecmp(w, "off")))
777 return ap_pstrcat(parms->pool, cmd->name, " must be On or Off",
780 return ((const char *(*)(cmd_parms *, void *, int))
781 (cmd->func)) (parms, mconfig, strcasecmp(w, "off") != 0);
785 return ap_pstrcat(parms->pool, cmd->name,
786 " is improperly configured internally (server bug)",
791 CORE_EXPORT(const command_rec *) ap_find_command(const char *name, const command_rec *cmds)
794 if (!strcasecmp(name, cmds->name))
802 CORE_EXPORT(const command_rec *) ap_find_command_in_modules(const char *cmd_name, module **mod)
804 const command_rec *cmdp;
807 for (modp = *mod; modp; modp = modp->next)
808 if (modp->cmds && (cmdp = ap_find_command(cmd_name, modp->cmds))) {
816 CORE_EXPORT(void *) ap_set_config_vectors(cmd_parms *parms, void *config, module *mod)
818 void *mconfig = ap_get_module_config(config, mod);
819 void *sconfig = ap_get_module_config(parms->server->module_config, mod);
821 if (!mconfig && mod->create_dir_config) {
822 mconfig = (*mod->create_dir_config) (parms->pool, parms->path);
823 ap_set_module_config(config, mod, mconfig);
826 if (!sconfig && mod->create_server_config) {
827 sconfig = (*mod->create_server_config) (parms->pool, parms->server);
828 ap_set_module_config(parms->server->module_config, mod, sconfig);
834 CORE_EXPORT(const char *) ap_handle_command(cmd_parms *parms, void *config, const char *l)
837 const char *args, *cmd_name, *retval;
838 const command_rec *cmd;
839 module *mod = top_module;
841 if ((l[0] == '#') || (!l[0]))
844 #if RESOLVE_ENV_PER_TOKEN
847 args = ap_resolve_env(parms->temp_pool,l);
849 cmd_name = ap_getword_conf(parms->temp_pool, &args);
850 if (*cmd_name == '\0')
853 oldconfig = parms->context;
854 parms->context = config;
856 if (!(cmd = ap_find_command_in_modules(cmd_name, &mod))) {
858 return ap_pstrcat(parms->pool, "Invalid command '", cmd_name,
859 "', perhaps mis-spelled or defined by a module "
860 "not included in the server configuration", NULL);
863 void *mconfig = ap_set_config_vectors(parms,config, mod);
865 retval = invoke_cmd(cmd, parms, mconfig, args);
866 mod = mod->next; /* Next time around, skip this one */
868 } while (retval && !strcmp(retval, DECLINE_CMD));
869 parms->context = oldconfig;
874 API_EXPORT(const char *) ap_srm_command_loop(cmd_parms *parms, void *config)
876 char l[MAX_STRING_LEN];
878 while (!(ap_cfg_getline(l, MAX_STRING_LEN, parms->config_file))) {
879 const char *errmsg = ap_handle_command(parms, config, l);
889 * Generic command functions...
892 API_EXPORT_NONSTD(const char *) ap_set_string_slot(cmd_parms *cmd,
893 char *struct_ptr, char *arg)
895 /* This one's pretty generic... */
897 int offset = (int) (long) cmd->info;
898 *(char **) (struct_ptr + offset) = arg;
902 API_EXPORT_NONSTD(const char *) ap_set_string_slot_lower(cmd_parms *cmd,
903 char *struct_ptr, char *arg)
905 /* This one's pretty generic... */
907 int offset = (int) (long) cmd->info;
909 *(char **) (struct_ptr + offset) = arg;
913 API_EXPORT_NONSTD(const char *) ap_set_flag_slot(cmd_parms *cmd,
914 char *struct_ptr, int arg)
916 /* This one's pretty generic too... */
918 int offset = (int) (long) cmd->info;
919 *(int *) (struct_ptr + offset) = arg ? 1 : 0;
923 API_EXPORT_NONSTD(const char *) ap_set_file_slot(cmd_parms *cmd, char *struct_ptr, char *arg)
925 /* Prepend server_root to relative arg.
926 This allows .htaccess to be independent of server_root,
927 so the server can be moved or mirrored with less pain. */
929 int offset = (int) (long) cmd->info;
930 if (ap_os_is_path_absolute(arg))
933 p = ap_make_full_path(cmd->pool, ap_server_root, arg);
934 *(char **) (struct_ptr + offset) = p;
938 /*****************************************************************
940 * Reading whole config files...
943 static cmd_parms default_parms =
944 {NULL, 0, -1, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
946 API_EXPORT(const char *) ap_server_root_relative(ap_context_t *p, const char *file)
948 if(ap_os_is_path_absolute(file))
950 return ap_make_full_path(p, ap_server_root, file);
954 /* This structure and the following functions are needed for the
955 * table-based config file reading. They are passed to the
956 * cfg_open_custom() routine.
959 /* Structure to be passed to cfg_open_custom(): it contains an
960 * index which is incremented from 0 to nelts on each call to
961 * cfg_getline() (which in turn calls arr_elts_getstr())
962 * and an ap_array_header_t pointer for the string array.
965 ap_array_header_t *array;
970 /* arr_elts_getstr() returns the next line from the string array. */
971 static void *arr_elts_getstr(void *buf, size_t bufsiz, void *param)
973 arr_elts_param_t *arr_param = (arr_elts_param_t *) param;
975 /* End of array reached? */
976 if (++arr_param->curr_idx > arr_param->array->nelts)
979 /* return the line */
980 ap_cpystrn(buf, ((char **) arr_param->array->elts)[arr_param->curr_idx - 1], bufsiz);
986 /* arr_elts_close(): dummy close routine (makes sure no more lines can be read) */
987 static int arr_elts_close(void *param)
989 arr_elts_param_t *arr_param = (arr_elts_param_t *) param;
990 arr_param->curr_idx = arr_param->array->nelts;
994 static void process_command_config(server_rec *s, ap_array_header_t *arr, ap_context_t *p,
999 arr_elts_param_t arr_parms;
1001 arr_parms.curr_idx = 0;
1002 arr_parms.array = arr;
1004 parms = default_parms;
1006 parms.temp_pool = ptemp;
1008 parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1009 parms.config_file = ap_pcfg_open_custom(p, "-c/-C directives",
1011 arr_elts_getstr, arr_elts_close);
1013 errmsg = ap_srm_command_loop(&parms, s->lookup_defaults);
1016 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1017 "Syntax error in -C/-c directive:\n%s", errmsg);
1021 ap_cfg_closefile(parms.config_file);
1024 void ap_process_resource_config(server_rec *s, const char *fname, ap_context_t *p, ap_context_t *ptemp)
1030 fname = ap_server_root_relative(p, fname);
1032 /* don't require conf/httpd.conf if we have a -C or -c switch */
1033 if((ap_server_pre_read_config->nelts || ap_server_post_read_config->nelts) &&
1034 !(strcmp(fname, ap_server_root_relative(p, SERVER_CONFIG_FILE)))) {
1035 if (ap_stat(&finfo, fname, p) != APR_SUCCESS)
1039 /* GCC's initialization extensions are soooo nice here... */
1041 parms = default_parms;
1043 parms.temp_pool = ptemp;
1045 parms.override = (RSRC_CONF | OR_ALL) & ~(OR_AUTHCFG | OR_LIMIT);
1047 if (ap_pcfg_openfile(&parms.config_file, p, fname) != APR_SUCCESS) {
1048 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1049 "%s: could not open document config file %s",
1050 ap_server_argv0, fname);
1054 errmsg = ap_srm_command_loop(&parms, s->lookup_defaults);
1057 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1058 "Syntax error on line %d of %s:",
1059 parms.config_file->line_number, parms.config_file->name);
1060 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1065 ap_cfg_closefile(parms.config_file);
1069 int ap_parse_htaccess(void **result, request_rec *r, int override,
1070 const char *d, const char *access_name)
1072 configfile_t *f = NULL;
1075 char *filename = NULL;
1076 const struct htaccess_result *cache;
1077 struct htaccess_result *new;
1081 /* firstly, search cache */
1082 for (cache = r->htaccess; cache != NULL; cache = cache->next)
1083 if (cache->override == override && strcmp(cache->dir, d) == 0) {
1084 if (cache->htaccess != NULL)
1085 *result = cache->htaccess;
1089 parms = default_parms;
1090 parms.override = override;
1091 parms.pool = r->pool;
1092 parms.temp_pool = r->pool;
1093 parms.server = r->server;
1094 parms.path = ap_pstrdup(r->pool, d);
1096 /* loop through the access names and find the first one */
1098 while (access_name[0]) {
1099 filename = ap_make_full_path(r->pool, d,
1100 ap_getword_conf(r->pool, &access_name));
1101 status = ap_pcfg_openfile(&f, r->pool, filename);
1103 if (status == APR_SUCCESS) {
1105 dc = ap_create_per_dir_config(r->pool);
1107 parms.config_file = f;
1109 errmsg = ap_srm_command_loop(&parms, dc);
1111 ap_cfg_closefile(f);
1114 ap_log_rerror(APLOG_MARK, APLOG_ALERT|APLOG_NOERRNO, 0, r,
1115 "%s: %s", filename, errmsg);
1116 return HTTP_INTERNAL_SERVER_ERROR;
1121 else if (status != APR_ENOENT && status != APR_ENOTDIR) {
1122 ap_log_rerror(APLOG_MARK, APLOG_CRIT, errno, r,
1123 "%s pcfg_openfile: unable to check htaccess file, "
1124 "ensure it is readable",
1126 ap_table_setn(r->notes, "error-notes",
1127 "Server unable to read htaccess file, denying "
1128 "access to be safe");
1129 return HTTP_FORBIDDEN;
1134 new = ap_palloc(r->pool, sizeof(struct htaccess_result));
1135 new->dir = parms.path;
1136 new->override = override;
1138 /* add to head of list */
1139 new->next = r->htaccess;
1146 CORE_EXPORT(const char *) ap_init_virtual_host(ap_context_t *p, const char *hostname,
1147 server_rec *main_server, server_rec **ps)
1149 server_rec *s = (server_rec *) ap_pcalloc(p, sizeof(server_rec));
1151 #ifdef RLIMIT_NOFILE
1152 struct rlimit limits;
1154 getrlimit(RLIMIT_NOFILE, &limits);
1155 if (limits.rlim_cur < limits.rlim_max) {
1156 limits.rlim_cur += 2;
1157 if (setrlimit(RLIMIT_NOFILE, &limits) < 0) {
1158 perror("setrlimit(RLIMIT_NOFILE)");
1159 ap_log_error(APLOG_MARK, APLOG_STARTUP | APLOG_NOERRNO, 0, NULL,
1160 "Cannot exceed hard limit for open files");
1165 /* TODO: this crap belongs in http_core */
1166 s->process = main_server->process;
1167 s->server_admin = NULL;
1168 s->server_hostname = NULL;
1169 s->error_fname = NULL;
1171 s->keep_alive_timeout = 0;
1173 s->keep_alive_max = -1;
1174 s->error_log = main_server->error_log;
1175 s->loglevel = main_server->loglevel;
1176 /* useful default, otherwise we get a port of 0 on redirects */
1177 s->port = main_server->port;
1181 s->names = ap_make_array(p, 4, sizeof(char **));
1182 s->wild_names = ap_make_array(p, 4, sizeof(char **));
1184 s->module_config = create_empty_config(p);
1185 s->lookup_defaults = ap_create_per_dir_config(p);
1188 s->server_uid = ap_user_id;
1189 s->server_gid = ap_group_id;
1192 s->limit_req_line = main_server->limit_req_line;
1193 s->limit_req_fieldsize = main_server->limit_req_fieldsize;
1194 s->limit_req_fields = main_server->limit_req_fields;
1198 return ap_parse_vhost_addrs(p, hostname, s);
1202 static void fixup_virtual_hosts(ap_context_t *p, server_rec *main_server)
1206 for (virt = main_server->next; virt; virt = virt->next) {
1207 merge_server_configs(p, main_server->module_config,
1208 virt->module_config);
1210 virt->lookup_defaults =
1211 ap_merge_per_dir_configs(p, main_server->lookup_defaults,
1212 virt->lookup_defaults);
1214 if (virt->server_admin == NULL)
1215 virt->server_admin = main_server->server_admin;
1217 if (virt->timeout == 0)
1218 virt->timeout = main_server->timeout;
1220 if (virt->keep_alive_timeout == 0)
1221 virt->keep_alive_timeout = main_server->keep_alive_timeout;
1223 if (virt->keep_alive == -1)
1224 virt->keep_alive = main_server->keep_alive;
1226 if (virt->keep_alive_max == -1)
1227 virt->keep_alive_max = main_server->keep_alive_max;
1229 /* XXX: this is really something that should be dealt with by a
1230 * post-config api phase */
1231 ap_core_reorder_directories(p, virt);
1233 ap_core_reorder_directories(p, main_server);
1236 /*****************************************************************
1238 * Getting *everything* configured...
1241 static void init_config_globals(ap_context_t *p)
1243 /* Global virtual host hash bucket pointers. Init to null. */
1244 ap_init_vhost_config(p);
1247 static server_rec *init_server_config(process_rec *process, ap_context_t *p)
1249 server_rec *s = (server_rec *) ap_pcalloc(p, sizeof(server_rec));
1251 ap_open_stderr(&s->error_log, p);
1252 s->process = process;
1254 s->server_admin = DEFAULT_ADMIN;
1255 s->server_hostname = NULL;
1256 s->error_fname = DEFAULT_ERRORLOG;
1257 s->loglevel = DEFAULT_LOGLEVEL;
1258 s->limit_req_line = DEFAULT_LIMIT_REQUEST_LINE;
1259 s->limit_req_fieldsize = DEFAULT_LIMIT_REQUEST_FIELDSIZE;
1260 s->limit_req_fields = DEFAULT_LIMIT_REQUEST_FIELDS;
1261 s->timeout = DEFAULT_TIMEOUT;
1262 s->keep_alive_timeout = DEFAULT_KEEPALIVE_TIMEOUT;
1263 s->keep_alive_max = DEFAULT_KEEPALIVE;
1266 s->addrs = ap_pcalloc(p, sizeof(server_addr_rec));
1267 /* NOT virtual host; don't match any real network interface */
1268 s->addrs->host_addr.s_addr = htonl(INADDR_ANY);
1269 s->addrs->host_port = 0; /* matches any port */
1270 s->addrs->virthost = ""; /* must be non-NULL */
1271 s->names = s->wild_names = NULL;
1273 s->module_config = create_server_config(p, s);
1274 s->lookup_defaults = create_default_per_dir_config(p);
1280 server_rec *ap_read_config(process_rec *process, ap_context_t *ptemp, const char *confname)
1282 ap_context_t *p = process->pconf;
1283 server_rec *s = init_server_config(process, p);
1285 init_config_globals(p);
1287 /* All server-wide config files now have the SAME syntax... */
1289 process_command_config(s, ap_server_pre_read_config, p, ptemp);
1291 ap_process_resource_config(s, confname, p, ptemp);
1293 process_command_config(s, ap_server_post_read_config, p, ptemp);
1295 fixup_virtual_hosts(p, s);
1296 ap_fini_vhost_config(p, s);
1302 void ap_single_module_configure(ap_context_t *p, server_rec *s, module *m)
1304 if (m->create_server_config)
1305 ap_set_module_config(s->module_config, m,
1306 (*m->create_server_config)(p, s));
1307 if (m->create_dir_config)
1308 ap_set_module_config(s->lookup_defaults, m,
1309 (*m->create_dir_config)(p, NULL));
1312 void run_pre_config(ap_context_t *p, ap_context_t *plog, ap_context_t *ptemp)
1316 for (m = top_module; m; m = m->next)
1318 (*m->pre_config) (p, plog, ptemp);
1322 void ap_post_config_hook(ap_context_t *pconf, ap_context_t *plog, ap_context_t *ptemp, server_rec *s)
1324 ap_run_post_config(pconf,plog,ptemp,s);
1325 init_handlers(pconf);
1328 void ap_child_init_hook(ap_context_t *pchild, server_rec *s)
1330 /* TODO: uh this seems ugly, is there a better way? */
1331 /*ap_child_init_alloc(); PUT THIS BACK IN XXXXX */
1333 ap_run_child_init(pchild,s);
1336 /********************************************************************
1337 * Configuration directives are restricted in terms of where they may
1338 * appear in the main configuration files and/or .htaccess files according
1339 * to the bitmask req_override in the command_rec structure.
1340 * If any of the overrides set in req_override are also allowed in the
1341 * context in which the command is read, then the command is allowed.
1342 * The context is determined as follows:
1344 * inside *.conf --> override = (RSRC_CONF|OR_ALL)&~(OR_AUTHCFG|OR_LIMIT);
1345 * within <Directory> or <Location> --> override = OR_ALL|ACCESS_CONF;
1346 * within .htaccess --> override = AllowOverride for current directory;
1348 * the result is, well, a rather confusing set of possibilities for when
1349 * a particular directive is allowed to be used. This procedure prints
1350 * in English where the given (pc) directive can be used.
1352 static void show_overrides(const command_rec *pc, module *pm)
1356 printf("\tAllowed in *.conf ");
1357 if ((pc->req_override & (OR_OPTIONS | OR_FILEINFO | OR_INDEXES)) ||
1358 ((pc->req_override & RSRC_CONF) &&
1359 ((pc->req_override & (ACCESS_CONF | OR_AUTHCFG | OR_LIMIT)))))
1361 else if (pc->req_override & RSRC_CONF)
1362 printf("only outside <Directory>, <Files> or <Location>");
1364 printf("only inside <Directory>, <Files> or <Location>");
1366 /* Warn if the directive is allowed inside <Directory> or .htaccess
1367 * but module doesn't support per-dir configuration */
1369 if ((pc->req_override & (OR_ALL | ACCESS_CONF)) && !pm->create_dir_config)
1370 printf(" [no per-dir config]");
1372 if (pc->req_override & OR_ALL) {
1373 printf(" and in .htaccess\n\twhen AllowOverride");
1375 if ((pc->req_override & OR_ALL) == OR_ALL)
1376 printf(" isn't None");
1378 printf(" includes ");
1380 if (pc->req_override & OR_AUTHCFG) {
1383 printf("AuthConfig");
1385 if (pc->req_override & OR_LIMIT) {
1390 if (pc->req_override & OR_OPTIONS) {
1395 if (pc->req_override & OR_FILEINFO) {
1400 if (pc->req_override & OR_INDEXES) {
1410 /* Show the preloaded configuration directives, the help string explaining
1411 * the directive arguments, in what module they are handled, and in
1412 * what parts of the configuration they are allowed. Used for httpd -h.
1414 void ap_show_directives()
1416 const command_rec *pc;
1419 for (n = 0; ap_loaded_modules[n]; ++n)
1420 for (pc = ap_loaded_modules[n]->cmds; pc && pc->name; ++pc) {
1421 printf("%s (%s)\n", pc->name, ap_loaded_modules[n]->name);
1423 printf("\t%s\n", pc->errmsg);
1424 show_overrides(pc, ap_loaded_modules[n]);
1428 /* Show the preloaded module names. Used for httpd -l. */
1429 void ap_show_modules()
1433 printf("Compiled in modules:\n");
1434 for (n = 0; ap_loaded_modules[n]; ++n)
1435 printf(" %s\n", ap_loaded_modules[n]->name);