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 #include "apr_strings.h"
20 #include "apr_fnmatch.h"
22 #include "apr_thread_proc.h" /* for RLIMIT stuff */
23 #include "apr_hooks.h"
25 #define APR_WANT_IOVEC
26 #define APR_WANT_STRFUNC
27 #define APR_WANT_MEMFUNC
30 #include "ap_config.h"
32 #include "http_config.h"
33 #include "http_core.h"
34 #include "http_protocol.h" /* For index_of_response(). Grump. */
35 #include "http_request.h"
36 #include "http_vhost.h"
37 #include "http_main.h" /* For the default_handler below... */
40 #include "http_connection.h"
41 #include "apr_buckets.h"
42 #include "util_filter.h"
43 #include "util_ebcdic.h"
44 #include "util_mutex.h"
45 #include "mpm_common.h"
46 #include "scoreboard.h"
48 #include "mod_proxy.h"
49 #include "ap_listen.h"
51 #include "mod_so.h" /* for ap_find_loaded_module_symbol */
53 #if defined(RLIMIT_CPU) || defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS) || defined (RLIMIT_NPROC)
57 /* LimitRequestBody handling */
58 #define AP_LIMIT_REQ_BODY_UNSET ((apr_off_t) -1)
59 #define AP_DEFAULT_LIMIT_REQ_BODY ((apr_off_t) 0)
61 /* LimitXMLRequestBody handling */
62 #define AP_LIMIT_UNSET ((long) -1)
63 #define AP_DEFAULT_LIMIT_XML_BODY ((size_t)1000000)
65 #define AP_MIN_SENDFILE_BYTES (256)
67 /* maximum include nesting level */
68 #ifndef AP_MAX_INCLUDE_DEPTH
69 #define AP_MAX_INCLUDE_DEPTH (128)
73 APR_HOOK_LINK(get_mgmt_items)
76 AP_IMPLEMENT_HOOK_RUN_ALL(int, get_mgmt_items,
77 (apr_pool_t *p, const char *val, apr_hash_t *ht),
78 (p, val, ht), OK, DECLINED)
80 /* Server core module... This module provides support for really basic
81 * server operations, including options and commands which control the
82 * operation of other modules. Consider this the bureaucracy module.
84 * The core module also defines handlers, etc., to handle just enough
85 * to allow a server with the core module ONLY to actually serve documents.
87 * This file could almost be mod_core.c, except for the stuff which affects
88 * the http_conf_globals.
91 /* Handles for core filters */
92 AP_DECLARE_DATA ap_filter_rec_t *ap_subreq_core_filter_handle;
93 AP_DECLARE_DATA ap_filter_rec_t *ap_core_output_filter_handle;
94 AP_DECLARE_DATA ap_filter_rec_t *ap_content_length_filter_handle;
95 AP_DECLARE_DATA ap_filter_rec_t *ap_core_input_filter_handle;
97 /* magic pointer for ErrorDocument xxx "default" */
98 static char errordocument_default;
100 static void *create_core_dir_config(apr_pool_t *a, char *dir)
102 core_dir_config *conf;
104 conf = (core_dir_config *)apr_pcalloc(a, sizeof(core_dir_config));
106 /* conf->r and conf->d[_*] are initialized by dirsection() or left NULL */
108 conf->opts = dir ? OPT_UNSET : OPT_UNSET|OPT_ALL;
109 conf->opts_add = conf->opts_remove = OPT_NONE;
110 conf->override = dir ? OR_UNSET : OR_UNSET|OR_ALL;
111 conf->override_opts = OPT_UNSET | OPT_ALL | OPT_SYM_OWNER | OPT_MULTI;
113 conf->content_md5 = 2;
114 conf->accept_path_info = 3;
116 conf->use_canonical_name = USE_CANONICAL_NAME_UNSET;
117 conf->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_UNSET;
119 conf->hostname_lookups = HOSTNAME_LOOKUP_UNSET;
122 conf->limit_cpu = NULL;
124 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
125 conf->limit_mem = NULL;
128 conf->limit_nproc = NULL;
131 conf->limit_req_body = AP_LIMIT_REQ_BODY_UNSET;
132 conf->limit_xml_body = AP_LIMIT_UNSET;
133 conf->sec_file = apr_array_make(a, 2, sizeof(ap_conf_vector_t *));
135 conf->server_signature = srv_sig_unset;
137 conf->add_default_charset = ADD_DEFAULT_CHARSET_UNSET;
138 conf->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
140 /* Overriding all negotiation
142 conf->mime_type = NULL;
143 conf->handler = NULL;
144 conf->output_filters = NULL;
145 conf->input_filters = NULL;
148 * Flag for use of inodes in ETags.
150 conf->etag_bits = ETAG_UNSET;
151 conf->etag_add = ETAG_UNSET;
152 conf->etag_remove = ETAG_UNSET;
154 conf->enable_mmap = ENABLE_MMAP_UNSET;
155 conf->enable_sendfile = ENABLE_SENDFILE_UNSET;
156 conf->allow_encoded_slashes = 0;
162 * Overlay one hash table of ct_output_filters onto another
164 static void *merge_ct_filters(apr_pool_t *p,
167 const void *overlay_val,
168 const void *base_val,
171 ap_filter_rec_t *cur;
172 const ap_filter_rec_t *overlay_info = (const ap_filter_rec_t *)overlay_val;
173 const ap_filter_rec_t *base_info = (const ap_filter_rec_t *)base_val;
177 while (overlay_info) {
178 ap_filter_rec_t *new;
180 new = apr_pcalloc(p, sizeof(ap_filter_rec_t));
181 new->name = apr_pstrdup(p, overlay_info->name);
184 overlay_info = overlay_info->next;
191 /* We can't have dups. */
194 if (!strcasecmp(base_info->name, f->name)) {
203 f = apr_pcalloc(p, sizeof(ap_filter_rec_t));
204 f->name = apr_pstrdup(p, base_info->name);
209 base_info = base_info->next;
215 static void *merge_core_dir_configs(apr_pool_t *a, void *basev, void *newv)
217 core_dir_config *base = (core_dir_config *)basev;
218 core_dir_config *new = (core_dir_config *)newv;
219 core_dir_config *conf;
222 /* Create this conf by duplicating the base, replacing elements
223 * (or creating copies for merging) where new-> values exist.
225 conf = (core_dir_config *)apr_pmemdup(a, base, sizeof(core_dir_config));
228 conf->d_is_fnmatch = new->d_is_fnmatch;
229 conf->d_components = new->d_components;
231 conf->condition = new->condition;
233 if (new->opts & OPT_UNSET) {
234 /* there was no explicit setting of new->opts, so we merge
235 * preserve the invariant (opts_add & opts_remove) == 0
237 conf->opts_add = (conf->opts_add & ~new->opts_remove) | new->opts_add;
238 conf->opts_remove = (conf->opts_remove & ~new->opts_add)
240 conf->opts = (conf->opts & ~conf->opts_remove) | conf->opts_add;
242 /* If Includes was enabled with exec in the base config, but
243 * was enabled without exec in the new config, then disable
244 * exec in the merged set. */
245 if (((base->opts & (OPT_INCLUDES|OPT_INC_WITH_EXEC))
246 == (OPT_INCLUDES|OPT_INC_WITH_EXEC))
247 && ((new->opts & (OPT_INCLUDES|OPT_INC_WITH_EXEC))
249 conf->opts &= ~OPT_INC_WITH_EXEC;
253 /* otherwise we just copy, because an explicit opts setting
254 * overrides all earlier +/- modifiers
256 conf->opts = new->opts;
257 conf->opts_add = new->opts_add;
258 conf->opts_remove = new->opts_remove;
261 if (!(new->override & OR_UNSET)) {
262 conf->override = new->override;
265 if (!(new->override_opts & OPT_UNSET)) {
266 conf->override_opts = new->override_opts;
269 if (conf->response_code_strings == NULL) {
270 conf->response_code_strings = new->response_code_strings;
272 else if (new->response_code_strings != NULL) {
273 /* If we merge, the merge-result must have it's own array
275 conf->response_code_strings = apr_pmemdup(a,
276 base->response_code_strings,
277 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
279 for (i = 0; i < RESPONSE_CODES; ++i) {
280 if (new->response_code_strings[i] != NULL) {
281 conf->response_code_strings[i] = new->response_code_strings[i];
285 /* Otherwise we simply use the base->response_code_strings array
288 if (new->hostname_lookups != HOSTNAME_LOOKUP_UNSET) {
289 conf->hostname_lookups = new->hostname_lookups;
292 if ((new->content_md5 & 2) == 0) {
293 conf->content_md5 = new->content_md5;
296 if (new->accept_path_info != 3) {
297 conf->accept_path_info = new->accept_path_info;
300 if (new->use_canonical_name != USE_CANONICAL_NAME_UNSET) {
301 conf->use_canonical_name = new->use_canonical_name;
304 if (new->use_canonical_phys_port != USE_CANONICAL_PHYS_PORT_UNSET) {
305 conf->use_canonical_phys_port = new->use_canonical_phys_port;
309 if (new->limit_cpu) {
310 conf->limit_cpu = new->limit_cpu;
314 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
315 if (new->limit_mem) {
316 conf->limit_mem = new->limit_mem;
321 if (new->limit_nproc) {
322 conf->limit_nproc = new->limit_nproc;
326 if (new->limit_req_body != AP_LIMIT_REQ_BODY_UNSET) {
327 conf->limit_req_body = new->limit_req_body;
330 if (new->limit_xml_body != AP_LIMIT_UNSET)
331 conf->limit_xml_body = new->limit_xml_body;
333 conf->limit_xml_body = base->limit_xml_body;
335 if (!conf->sec_file) {
336 conf->sec_file = new->sec_file;
338 else if (new->sec_file) {
339 /* If we merge, the merge-result must have it's own array
341 conf->sec_file = apr_array_append(a, base->sec_file, new->sec_file);
343 /* Otherwise we simply use the base->sec_file array
346 if (new->server_signature != srv_sig_unset) {
347 conf->server_signature = new->server_signature;
350 if (new->add_default_charset != ADD_DEFAULT_CHARSET_UNSET) {
351 conf->add_default_charset = new->add_default_charset;
352 conf->add_default_charset_name = new->add_default_charset_name;
355 /* Overriding all negotiation
357 if (new->mime_type) {
358 conf->mime_type = new->mime_type;
362 conf->handler = new->handler;
365 if (new->output_filters) {
366 conf->output_filters = new->output_filters;
369 if (new->input_filters) {
370 conf->input_filters = new->input_filters;
373 if (conf->ct_output_filters && new->ct_output_filters) {
374 conf->ct_output_filters = apr_hash_merge(a,
375 new->ct_output_filters,
376 conf->ct_output_filters,
380 else if (new->ct_output_filters) {
381 conf->ct_output_filters = apr_hash_copy(a, new->ct_output_filters);
383 else if (conf->ct_output_filters) {
384 /* That memcpy above isn't enough. */
385 conf->ct_output_filters = apr_hash_copy(a, base->ct_output_filters);
389 * Now merge the setting of the FileETag directive.
391 if (new->etag_bits == ETAG_UNSET) {
393 (conf->etag_add & (~ new->etag_remove)) | new->etag_add;
395 (conf->etag_remove & (~ new->etag_add)) | new->etag_remove;
397 (conf->etag_bits & (~ conf->etag_remove)) | conf->etag_add;
400 conf->etag_bits = new->etag_bits;
401 conf->etag_add = new->etag_add;
402 conf->etag_remove = new->etag_remove;
405 if (conf->etag_bits != ETAG_NONE) {
406 conf->etag_bits &= (~ ETAG_NONE);
409 if (new->enable_mmap != ENABLE_MMAP_UNSET) {
410 conf->enable_mmap = new->enable_mmap;
413 if (new->enable_sendfile != ENABLE_SENDFILE_UNSET) {
414 conf->enable_sendfile = new->enable_sendfile;
417 conf->allow_encoded_slashes = new->allow_encoded_slashes;
422 #if APR_HAS_SO_ACCEPTFILTER
423 #ifndef ACCEPT_FILTER_NAME
424 #define ACCEPT_FILTER_NAME "httpready"
425 #ifdef __FreeBSD_version
426 #if __FreeBSD_version < 411000 /* httpready broken before 4.1.1 */
427 #undef ACCEPT_FILTER_NAME
428 #define ACCEPT_FILTER_NAME "dataready"
434 static void *create_core_server_config(apr_pool_t *a, server_rec *s)
436 core_server_config *conf;
437 int is_virtual = s->is_virtual;
439 conf = (core_server_config *)apr_pcalloc(a, sizeof(core_server_config));
441 /* global-default / global-only settings */
444 conf->ap_document_root = DOCUMENT_LOCATION;
445 conf->access_name = DEFAULT_ACCESS_FNAME;
447 /* A mapping only makes sense in the global context */
448 conf->accf_map = apr_table_make(a, 5);
449 #if APR_HAS_SO_ACCEPTFILTER
450 apr_table_setn(conf->accf_map, "http", ACCEPT_FILTER_NAME);
451 apr_table_setn(conf->accf_map, "https", "dataready");
453 apr_table_setn(conf->accf_map, "http", "data");
454 apr_table_setn(conf->accf_map, "https", "data");
457 /* pcalloc'ed - we have NULL's/0's
458 else ** is_virtual ** {
459 conf->ap_document_root = NULL;
460 conf->access_name = NULL;
461 conf->accf_map = NULL;
465 /* initialization, no special case for global context */
467 conf->sec_dir = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
468 conf->sec_url = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
470 /* pcalloc'ed - we have NULL's/0's
471 conf->gprof_dir = NULL;
473 ** recursion stopper; 0 == unset
474 conf->redirect_limit = 0;
475 conf->subreq_limit = 0;
477 conf->protocol = NULL;
480 conf->trace_enable = AP_TRACE_UNSET;
485 static void *merge_core_server_configs(apr_pool_t *p, void *basev, void *virtv)
487 core_server_config *base = (core_server_config *)basev;
488 core_server_config *virt = (core_server_config *)virtv;
489 core_server_config *conf = (core_server_config *)
490 apr_pmemdup(p, base, sizeof(core_server_config));
492 if (virt->ap_document_root)
493 conf->ap_document_root = virt->ap_document_root;
495 if (virt->access_name)
496 conf->access_name = virt->access_name;
498 /* XXX optimize to keep base->sec_ pointers if virt->sec_ array is empty */
499 conf->sec_dir = apr_array_append(p, base->sec_dir, virt->sec_dir);
500 conf->sec_url = apr_array_append(p, base->sec_url, virt->sec_url);
502 if (virt->redirect_limit)
503 conf->redirect_limit = virt->redirect_limit;
505 if (virt->subreq_limit)
506 conf->subreq_limit = virt->subreq_limit;
508 if (virt->trace_enable != AP_TRACE_UNSET)
509 conf->trace_enable = virt->trace_enable;
511 /* no action for virt->accf_map, not allowed per-vhost */
514 conf->protocol = virt->protocol;
517 conf->gprof_dir = virt->gprof_dir;
522 /* Add per-directory configuration entry (for <directory> section);
523 * these are part of the core server config.
526 AP_CORE_DECLARE(void) ap_add_per_dir_conf(server_rec *s, void *dir_config)
528 core_server_config *sconf = ap_get_module_config(s->module_config,
530 void **new_space = (void **)apr_array_push(sconf->sec_dir);
532 *new_space = dir_config;
535 AP_CORE_DECLARE(void) ap_add_per_url_conf(server_rec *s, void *url_config)
537 core_server_config *sconf = ap_get_module_config(s->module_config,
539 void **new_space = (void **)apr_array_push(sconf->sec_url);
541 *new_space = url_config;
544 AP_CORE_DECLARE(void) ap_add_file_conf(core_dir_config *conf, void *url_config)
546 void **new_space = (void **)apr_array_push(conf->sec_file);
548 *new_space = url_config;
551 /* We need to do a stable sort, qsort isn't stable. So to make it stable
552 * we'll be maintaining the original index into the list, and using it
553 * as the minor key during sorting. The major key is the number of
554 * components (where the root component is zero).
556 struct reorder_sort_rec {
557 ap_conf_vector_t *elt;
561 static int reorder_sorter(const void *va, const void *vb)
563 const struct reorder_sort_rec *a = va;
564 const struct reorder_sort_rec *b = vb;
565 core_dir_config *core_a;
566 core_dir_config *core_b;
568 core_a = ap_get_module_config(a->elt, &core_module);
569 core_b = ap_get_module_config(b->elt, &core_module);
571 /* a regex always sorts after a non-regex
573 if (!core_a->r && core_b->r) {
576 else if (core_a->r && !core_b->r) {
580 /* we always sort next by the number of components
582 if (core_a->d_components < core_b->d_components) {
585 else if (core_a->d_components > core_b->d_components) {
589 /* They have the same number of components, we now have to compare
590 * the minor key to maintain the original order (from the config.)
592 return a->orig_index - b->orig_index;
595 void ap_core_reorder_directories(apr_pool_t *p, server_rec *s)
597 core_server_config *sconf;
598 apr_array_header_t *sec_dir;
599 struct reorder_sort_rec *sortbin;
601 ap_conf_vector_t **elts;
605 sconf = ap_get_module_config(s->module_config, &core_module);
606 sec_dir = sconf->sec_dir;
607 nelts = sec_dir->nelts;
608 elts = (ap_conf_vector_t **)sec_dir->elts;
611 /* simple case of already being sorted... */
612 /* We're not checking this condition to be fast... we're checking
613 * it to avoid trying to palloc zero bytes, which can trigger some
614 * memory debuggers to barf
619 /* we have to allocate tmp space to do a stable sort */
620 apr_pool_create(&tmp, p);
621 sortbin = apr_palloc(tmp, sec_dir->nelts * sizeof(*sortbin));
622 for (i = 0; i < nelts; ++i) {
623 sortbin[i].orig_index = i;
624 sortbin[i].elt = elts[i];
627 qsort(sortbin, nelts, sizeof(*sortbin), reorder_sorter);
629 /* and now copy back to the original array */
630 for (i = 0; i < nelts; ++i) {
631 elts[i] = sortbin[i].elt;
634 apr_pool_destroy(tmp);
637 /*****************************************************************
639 * There are some elements of the core config structures in which
640 * other modules have a legitimate interest (this is ugly, but necessary
641 * to preserve NCSA back-compatibility). So, we have a bunch of accessors
645 AP_DECLARE(int) ap_allow_options(request_rec *r)
647 core_dir_config *conf =
648 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
653 AP_DECLARE(int) ap_allow_overrides(request_rec *r)
655 core_dir_config *conf;
656 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
659 return conf->override;
663 * Optional function coming from mod_authn_core, used for
664 * retrieving the type of autorization
666 static APR_OPTIONAL_FN_TYPE(authn_ap_auth_type) *authn_ap_auth_type;
668 AP_DECLARE(const char *) ap_auth_type(request_rec *r)
670 if (authn_ap_auth_type) {
671 return authn_ap_auth_type(r);
677 * Optional function coming from mod_authn_core, used for
678 * retrieving the authorization realm
680 static APR_OPTIONAL_FN_TYPE(authn_ap_auth_name) *authn_ap_auth_name;
682 AP_DECLARE(const char *) ap_auth_name(request_rec *r)
684 if (authn_ap_auth_name) {
685 return authn_ap_auth_name(r);
691 * Optional function coming from mod_access_compat, used to determine how
692 access control interacts with authentication/authorization
694 static APR_OPTIONAL_FN_TYPE(access_compat_ap_satisfies) *access_compat_ap_satisfies;
696 AP_DECLARE(int) ap_satisfies(request_rec *r)
698 if (access_compat_ap_satisfies) {
699 return access_compat_ap_satisfies(r);
701 return SATISFY_NOSPEC;
704 AP_DECLARE(const char *) ap_document_root(request_rec *r) /* Don't use this! */
706 core_server_config *conf;
708 conf = (core_server_config *)ap_get_module_config(r->server->module_config,
711 return conf->ap_document_root;
714 /* Should probably just get rid of this... the only code that cares is
715 * part of the core anyway (and in fact, it isn't publicised to other
719 char *ap_response_code_string(request_rec *r, int error_index)
721 core_dir_config *dirconf;
722 core_request_config *reqconf;
724 /* check for string registered via ap_custom_response() first */
725 reqconf = (core_request_config *)ap_get_module_config(r->request_config,
727 if (reqconf->response_code_strings != NULL &&
728 reqconf->response_code_strings[error_index] != NULL) {
729 return reqconf->response_code_strings[error_index];
732 /* check for string specified via ErrorDocument */
733 dirconf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
736 if (dirconf->response_code_strings == NULL) {
740 if (dirconf->response_code_strings[error_index] == &errordocument_default) {
744 return dirconf->response_code_strings[error_index];
748 /* Code from Harald Hanche-Olsen <hanche@imf.unit.no> */
749 static APR_INLINE void do_double_reverse (conn_rec *conn)
754 if (conn->double_reverse) {
759 if (conn->remote_host == NULL || conn->remote_host[0] == '\0') {
760 /* single reverse failed, so don't bother */
761 conn->double_reverse = -1;
765 rv = apr_sockaddr_info_get(&sa, conn->remote_host, APR_UNSPEC, 0, 0, conn->pool);
766 if (rv == APR_SUCCESS) {
768 if (apr_sockaddr_equal(sa, conn->remote_addr)) {
769 conn->double_reverse = 1;
777 conn->double_reverse = -1;
780 AP_DECLARE(const char *) ap_get_remote_host(conn_rec *conn, void *dir_config,
781 int type, int *str_is_ip)
783 int hostname_lookups;
784 int ignored_str_is_ip;
786 if (!str_is_ip) { /* caller doesn't want to know */
787 str_is_ip = &ignored_str_is_ip;
791 /* If we haven't checked the host name, and we want to */
794 ((core_dir_config *)ap_get_module_config(dir_config, &core_module))
797 if (hostname_lookups == HOSTNAME_LOOKUP_UNSET) {
798 hostname_lookups = HOSTNAME_LOOKUP_OFF;
803 hostname_lookups = HOSTNAME_LOOKUP_OFF;
806 if (type != REMOTE_NOLOOKUP
807 && conn->remote_host == NULL
808 && (type == REMOTE_DOUBLE_REV
809 || hostname_lookups != HOSTNAME_LOOKUP_OFF)) {
811 if (apr_getnameinfo(&conn->remote_host, conn->remote_addr, 0)
813 ap_str_tolower(conn->remote_host);
815 if (hostname_lookups == HOSTNAME_LOOKUP_DOUBLE) {
816 do_double_reverse(conn);
817 if (conn->double_reverse != 1) {
818 conn->remote_host = NULL;
823 /* if failed, set it to the NULL string to indicate error */
824 if (conn->remote_host == NULL) {
825 conn->remote_host = "";
829 if (type == REMOTE_DOUBLE_REV) {
830 do_double_reverse(conn);
831 if (conn->double_reverse == -1) {
837 * Return the desired information; either the remote DNS name, if found,
838 * or either NULL (if the hostname was requested) or the IP address
839 * (if any identifier was requested).
841 if (conn->remote_host != NULL && conn->remote_host[0] != '\0') {
842 return conn->remote_host;
845 if (type == REMOTE_HOST || type == REMOTE_DOUBLE_REV) {
850 return conn->remote_ip;
856 * Optional function coming from mod_ident, used for looking up ident user
858 static APR_OPTIONAL_FN_TYPE(ap_ident_lookup) *ident_lookup;
860 AP_DECLARE(const char *) ap_get_remote_logname(request_rec *r)
862 if (r->connection->remote_logname != NULL) {
863 return r->connection->remote_logname;
867 return ident_lookup(r);
873 /* There are two options regarding what the "name" of a server is. The
874 * "canonical" name as defined by ServerName and Port, or the "client's
875 * name" as supplied by a possible Host: header or full URI.
877 * The DNS option to UseCanonicalName causes this routine to do a
878 * reverse lookup on the local IP address of the connection and use
879 * that for the ServerName. This makes its value more reliable while
880 * at the same time allowing Demon's magic virtual hosting to work.
881 * The assumption is that DNS lookups are sufficiently quick...
884 AP_DECLARE(const char *) ap_get_server_name(request_rec *r)
886 conn_rec *conn = r->connection;
890 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
893 switch (d->use_canonical_name) {
894 case USE_CANONICAL_NAME_ON:
895 retval = r->server->server_hostname;
897 case USE_CANONICAL_NAME_DNS:
898 if (conn->local_host == NULL) {
899 if (apr_getnameinfo(&conn->local_host,
900 conn->local_addr, 0) != APR_SUCCESS)
901 conn->local_host = apr_pstrdup(conn->pool,
902 r->server->server_hostname);
904 ap_str_tolower(conn->local_host);
907 retval = conn->local_host;
909 case USE_CANONICAL_NAME_OFF:
910 case USE_CANONICAL_NAME_UNSET:
911 retval = r->hostname ? r->hostname : r->server->server_hostname;
914 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
915 "ap_get_server_name: Invalid UCN Option somehow");
916 retval = "localhost";
923 * Get the current server name from the request for the purposes
924 * of using in a URL. If the server name is an IPv6 literal
925 * address, it will be returned in URL format (e.g., "[fe80::1]").
927 static const char *get_server_name_for_url(request_rec *r)
929 const char *plain_server_name = ap_get_server_name(r);
932 if (ap_strchr_c(plain_server_name, ':')) { /* IPv6 literal? */
933 return apr_psprintf(r->pool, "[%s]", plain_server_name);
936 return plain_server_name;
939 AP_DECLARE(apr_port_t) ap_get_server_port(const request_rec *r)
943 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
945 switch (d->use_canonical_name) {
946 case USE_CANONICAL_NAME_OFF:
947 case USE_CANONICAL_NAME_DNS:
948 case USE_CANONICAL_NAME_UNSET:
949 if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
950 port = r->parsed_uri.port_str ? r->parsed_uri.port :
951 r->connection->local_addr->port ? r->connection->local_addr->port :
952 r->server->port ? r->server->port :
954 else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
955 port = r->parsed_uri.port_str ? r->parsed_uri.port :
956 r->server->port ? r->server->port :
959 case USE_CANONICAL_NAME_ON:
960 /* With UseCanonicalName on (and in all versions prior to 1.3)
961 * Apache will use the hostname and port specified in the
962 * ServerName directive to construct a canonical name for the
963 * server. (If no port was specified in the ServerName
964 * directive, Apache uses the port supplied by the client if
965 * any is supplied, and finally the default port for the protocol
968 if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
969 port = r->server->port ? r->server->port :
970 r->connection->local_addr->port ? r->connection->local_addr->port :
972 else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
973 port = r->server->port ? r->server->port :
977 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
978 "ap_get_server_port: Invalid UCN Option somehow");
979 port = ap_default_port(r);
986 AP_DECLARE(char *) ap_construct_url(apr_pool_t *p, const char *uri,
989 unsigned port = ap_get_server_port(r);
990 const char *host = get_server_name_for_url(r);
992 if (ap_is_default_port(port, r)) {
993 return apr_pstrcat(p, ap_http_scheme(r), "://", host, uri, NULL);
996 return apr_psprintf(p, "%s://%s:%u%s", ap_http_scheme(r), host, port, uri);
999 AP_DECLARE(apr_off_t) ap_get_limit_req_body(const request_rec *r)
1001 core_dir_config *d =
1002 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
1004 if (d->limit_req_body == AP_LIMIT_REQ_BODY_UNSET) {
1005 return AP_DEFAULT_LIMIT_REQ_BODY;
1008 return d->limit_req_body;
1012 /*****************************************************************
1014 * Commands... this module handles almost all of the NCSA httpd.conf
1015 * commands, but most of the old srm.conf is in the the modules.
1019 /* returns a parent if it matches the given directive */
1020 static const ap_directive_t * find_parent(const ap_directive_t *dirp,
1023 while (dirp->parent != NULL) {
1024 dirp = dirp->parent;
1026 /* ### it would be nice to have atom-ized directives */
1027 if (strcasecmp(dirp->directive, what) == 0)
1034 AP_DECLARE(const char *) ap_check_cmd_context(cmd_parms *cmd,
1037 const char *gt = (cmd->cmd->name[0] == '<'
1038 && cmd->cmd->name[strlen(cmd->cmd->name)-1] != '>')
1040 const ap_directive_t *found;
1042 if ((forbidden & NOT_IN_VIRTUALHOST) && cmd->server->is_virtual) {
1043 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1044 " cannot occur within <VirtualHost> section", NULL);
1047 if ((forbidden & (NOT_IN_LIMIT | NOT_IN_DIR_LOC_FILE))
1048 && cmd->limited != -1) {
1049 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1050 " cannot occur within <Limit> or <LimitExcept> "
1054 if ((forbidden & NOT_IN_DIR_LOC_FILE) == NOT_IN_DIR_LOC_FILE) {
1055 if (cmd->path != NULL) {
1056 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1057 " cannot occur within <Directory/Location/Files> "
1060 if (cmd->cmd->req_override & EXEC_ON_READ) {
1061 /* EXEC_ON_READ must be NOT_IN_DIR_LOC_FILE, if not, it will
1062 * (deliberately) segfault below in the individual tests...
1068 if (((forbidden & NOT_IN_DIRECTORY)
1069 && ((found = find_parent(cmd->directive, "<Directory"))
1070 || (found = find_parent(cmd->directive, "<DirectoryMatch"))))
1071 || ((forbidden & NOT_IN_LOCATION)
1072 && ((found = find_parent(cmd->directive, "<Location"))
1073 || (found = find_parent(cmd->directive, "<LocationMatch"))))
1074 || ((forbidden & NOT_IN_FILES)
1075 && ((found = find_parent(cmd->directive, "<Files"))
1076 || (found = find_parent(cmd->directive, "<FilesMatch"))))) {
1077 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1078 " cannot occur within ", found->directive,
1085 static const char *set_access_name(cmd_parms *cmd, void *dummy,
1088 void *sconf = cmd->server->module_config;
1089 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1091 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
1096 conf->access_name = apr_pstrdup(cmd->pool, arg);
1101 static const char *set_define(cmd_parms *cmd, void *dummy,
1106 const char *err = ap_check_cmd_context(cmd,
1112 newv = (char **)apr_array_push(ap_server_config_defines);
1113 *newv = apr_pstrdup(cmd->pool, optarg);
1119 static const char *set_gprof_dir(cmd_parms *cmd, void *dummy, const char *arg)
1121 void *sconf = cmd->server->module_config;
1122 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1124 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
1129 conf->gprof_dir = apr_pstrdup(cmd->pool, arg);
1134 static const char *set_add_default_charset(cmd_parms *cmd,
1135 void *d_, const char *arg)
1137 core_dir_config *d = d_;
1139 if (!strcasecmp(arg, "Off")) {
1140 d->add_default_charset = ADD_DEFAULT_CHARSET_OFF;
1142 else if (!strcasecmp(arg, "On")) {
1143 d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1144 d->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
1147 d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1148 d->add_default_charset_name = arg;
1154 static const char *set_document_root(cmd_parms *cmd, void *dummy,
1157 void *sconf = cmd->server->module_config;
1158 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1160 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
1165 /* When ap_document_root_check is false; skip all the stuff below */
1166 if (!ap_document_root_check) {
1167 conf->ap_document_root = arg;
1171 /* Make it absolute, relative to ServerRoot */
1172 arg = ap_server_root_relative(cmd->pool, arg);
1174 return "DocumentRoot must be a directory";
1177 /* TODO: ap_configtestonly */
1178 if (apr_filepath_merge((char**)&conf->ap_document_root, NULL, arg,
1179 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS
1180 || !ap_is_directory(cmd->pool, arg)) {
1181 if (cmd->server->is_virtual) {
1182 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0,
1184 "Warning: DocumentRoot [%s] does not exist",
1186 conf->ap_document_root = arg;
1189 return "DocumentRoot must be a directory";
1195 AP_DECLARE(void) ap_custom_response(request_rec *r, int status,
1198 core_request_config *conf =
1199 ap_get_module_config(r->request_config, &core_module);
1202 if (conf->response_code_strings == NULL) {
1203 conf->response_code_strings =
1204 apr_pcalloc(r->pool,
1205 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
1208 idx = ap_index_of_response(status);
1210 conf->response_code_strings[idx] =
1211 ((ap_is_url(string) || (*string == '/')) && (*string != '"')) ?
1212 apr_pstrdup(r->pool, string) : apr_pstrcat(r->pool, "\"", string, NULL);
1215 static const char *set_error_document(cmd_parms *cmd, void *conf_,
1216 const char *errno_str, const char *msg)
1218 core_dir_config *conf = conf_;
1219 int error_number, index_number, idx500;
1220 enum { MSG, LOCAL_PATH, REMOTE_PATH } what = MSG;
1222 /* 1st parameter should be a 3 digit number, which we recognize;
1223 * convert it into an array index
1225 error_number = atoi(errno_str);
1226 idx500 = ap_index_of_response(HTTP_INTERNAL_SERVER_ERROR);
1228 if (error_number == HTTP_INTERNAL_SERVER_ERROR) {
1229 index_number = idx500;
1231 else if ((index_number = ap_index_of_response(error_number)) == idx500) {
1232 return apr_pstrcat(cmd->pool, "Unsupported HTTP response code ",
1236 /* Heuristic to determine second argument. */
1237 if (ap_strchr_c(msg,' '))
1239 else if (msg[0] == '/')
1241 else if (ap_is_url(msg))
1246 /* The entry should be ignored if it is a full URL for a 401 error */
1248 if (error_number == 401 && what == REMOTE_PATH) {
1249 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, cmd->server,
1250 "cannot use a full URL in a 401 ErrorDocument "
1251 "directive --- ignoring!");
1253 else { /* Store it... */
1254 if (conf->response_code_strings == NULL) {
1255 conf->response_code_strings =
1256 apr_pcalloc(cmd->pool,
1257 sizeof(*conf->response_code_strings) *
1261 if (strcmp(msg, "default") == 0) {
1262 /* special case: ErrorDocument 404 default restores the
1263 * canned server error response
1265 conf->response_code_strings[index_number] = &errordocument_default;
1268 /* hack. Prefix a " if it is a msg; as that is what
1269 * http_protocol.c relies on to distinguish between
1270 * a msg and a (local) path.
1272 conf->response_code_strings[index_number] = (what == MSG) ?
1273 apr_pstrcat(cmd->pool, "\"",msg,NULL) :
1274 apr_pstrdup(cmd->pool, msg);
1281 static const char *set_allow_opts(cmd_parms *cmd, allow_options_t *opts,
1284 allow_options_t opt;
1287 char *w, *p = (char *) l;
1290 while ((w = apr_strtok(p, ",", &tok_state)) != NULL) {
1298 if (!strcasecmp(w, "Indexes")) {
1301 else if (!strcasecmp(w, "Includes")) {
1302 /* If Includes is permitted, both Includes and
1303 * IncludesNOEXEC may be changed. */
1304 opt = (OPT_INCLUDES | OPT_INC_WITH_EXEC);
1306 else if (!strcasecmp(w, "IncludesNOEXEC")) {
1309 else if (!strcasecmp(w, "FollowSymLinks")) {
1310 opt = OPT_SYM_LINKS;
1312 else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1313 opt = OPT_SYM_OWNER;
1315 else if (!strcasecmp(w, "ExecCGI")) {
1318 else if (!strcasecmp(w, "MultiViews")) {
1321 else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1322 opt = OPT_MULTI|OPT_EXECCGI;
1324 else if (!strcasecmp(w, "None")) {
1327 else if (!strcasecmp(w, "All")) {
1331 return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1337 (*opts) &= (~OPT_UNSET);
1342 static const char *set_override(cmd_parms *cmd, void *d_, const char *l)
1344 core_dir_config *d = d_;
1348 /* Throw a warning if we're in <Location> or <Files> */
1349 if (ap_check_cmd_context(cmd, NOT_IN_LOCATION | NOT_IN_FILES)) {
1350 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
1351 "Useless use of AllowOverride in line %d of %s.",
1352 cmd->directive->line_num, cmd->directive->filename);
1355 d->override = OR_NONE;
1357 w = ap_getword_conf(cmd->pool, &l);
1365 if (!strcasecmp(w, "Limit")) {
1366 d->override |= OR_LIMIT;
1368 else if (!strcasecmp(k, "Options")) {
1369 d->override |= OR_OPTIONS;
1371 set_allow_opts(cmd, &(d->override_opts), v);
1373 d->override_opts = OPT_ALL;
1375 else if (!strcasecmp(w, "FileInfo")) {
1376 d->override |= OR_FILEINFO;
1378 else if (!strcasecmp(w, "AuthConfig")) {
1379 d->override |= OR_AUTHCFG;
1381 else if (!strcasecmp(w, "Indexes")) {
1382 d->override |= OR_INDEXES;
1384 else if (!strcasecmp(w, "None")) {
1385 d->override = OR_NONE;
1387 else if (!strcasecmp(w, "All")) {
1388 d->override = OR_ALL;
1391 return apr_pstrcat(cmd->pool, "Illegal override option ", w, NULL);
1394 d->override &= ~OR_UNSET;
1400 static const char *set_options(cmd_parms *cmd, void *d_, const char *l)
1402 core_dir_config *d = d_;
1403 allow_options_t opt;
1408 char *w = ap_getword_conf(cmd->pool, &l);
1411 if (*w == '+' || *w == '-') {
1419 if (!strcasecmp(w, "Indexes")) {
1422 else if (!strcasecmp(w, "Includes")) {
1423 opt = (OPT_INCLUDES | OPT_INC_WITH_EXEC);
1425 else if (!strcasecmp(w, "IncludesNOEXEC")) {
1428 else if (!strcasecmp(w, "FollowSymLinks")) {
1429 opt = OPT_SYM_LINKS;
1431 else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1432 opt = OPT_SYM_OWNER;
1434 else if (!strcasecmp(w, "ExecCGI")) {
1437 else if (!strcasecmp(w, "MultiViews")) {
1440 else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1441 opt = OPT_MULTI|OPT_EXECCGI;
1443 else if (!strcasecmp(w, "None")) {
1446 else if (!strcasecmp(w, "All")) {
1450 return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1453 if ( (cmd->override_opts & opt) != opt ) {
1454 return apr_pstrcat(cmd->pool, "Option ", w, " not allowed here", NULL);
1456 else if (action == '-') {
1457 /* we ensure the invariant (d->opts_add & d->opts_remove) == 0 */
1458 d->opts_remove |= opt;
1459 d->opts_add &= ~opt;
1462 else if (action == '+') {
1464 d->opts_remove &= ~opt;
1475 static const char *set_default_type(cmd_parms *cmd, void *d_,
1478 if ((strcasecmp(arg, "off") != 0) && (strcasecmp(arg, "none") != 0)) {
1479 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
1480 "Ignoring deprecated use of DefaultType in line %d of %s.",
1481 cmd->directive->line_num, cmd->directive->filename);
1488 * Note what data should be used when forming file ETag values.
1489 * It would be nicer to do this as an ITERATE, but then we couldn't
1490 * remember the +/- state properly.
1492 static const char *set_etag_bits(cmd_parms *cmd, void *mconfig,
1495 core_dir_config *cfg;
1496 etag_components_t bit;
1504 cfg = (core_dir_config *)mconfig;
1509 while (args[0] != '\0') {
1513 token = ap_getword_conf(cmd->pool, &args);
1514 if ((*token == '+') || (*token == '-')) {
1520 * The occurrence of an absolute setting wipes
1521 * out any previous relative ones. The first such
1522 * occurrence forgets any inherited ones, too.
1525 cfg->etag_bits = ETAG_UNSET;
1526 cfg->etag_add = ETAG_UNSET;
1527 cfg->etag_remove = ETAG_UNSET;
1532 if (strcasecmp(token, "None") == 0) {
1533 if (action != '*') {
1537 cfg->etag_bits = bit = ETAG_NONE;
1541 else if (strcasecmp(token, "All") == 0) {
1542 if (action != '*') {
1547 cfg->etag_bits = bit = ETAG_ALL;
1550 else if (strcasecmp(token, "Size") == 0) {
1553 else if ((strcasecmp(token, "LMTime") == 0)
1554 || (strcasecmp(token, "MTime") == 0)
1555 || (strcasecmp(token, "LastModified") == 0)) {
1558 else if (strcasecmp(token, "INode") == 0) {
1562 return apr_pstrcat(cmd->pool, "Unknown keyword '",
1563 token, "' for ", cmd->cmd->name,
1564 " directive", NULL);
1568 return apr_pstrcat(cmd->pool, cmd->cmd->name, " keyword '",
1569 token, "' cannot be used with '+' or '-'",
1573 if (action == '+') {
1575 * Make sure it's in the 'add' list and absent from the
1578 cfg->etag_add |= bit;
1579 cfg->etag_remove &= (~ bit);
1581 else if (action == '-') {
1582 cfg->etag_remove |= bit;
1583 cfg->etag_add &= (~ bit);
1587 * Non-relative values wipe out any + or - values
1588 * accumulated so far.
1590 cfg->etag_bits |= bit;
1591 cfg->etag_add = ETAG_UNSET;
1592 cfg->etag_remove = ETAG_UNSET;
1598 * Any setting at all will clear the 'None' and 'Unset' bits.
1601 if (cfg->etag_add != ETAG_UNSET) {
1602 cfg->etag_add &= (~ ETAG_UNSET);
1605 if (cfg->etag_remove != ETAG_UNSET) {
1606 cfg->etag_remove &= (~ ETAG_UNSET);
1610 cfg->etag_bits &= (~ ETAG_UNSET);
1612 if ((cfg->etag_bits & ETAG_NONE) != ETAG_NONE) {
1613 cfg->etag_bits &= (~ ETAG_NONE);
1620 static const char *set_enable_mmap(cmd_parms *cmd, void *d_,
1623 core_dir_config *d = d_;
1625 if (strcasecmp(arg, "on") == 0) {
1626 d->enable_mmap = ENABLE_MMAP_ON;
1628 else if (strcasecmp(arg, "off") == 0) {
1629 d->enable_mmap = ENABLE_MMAP_OFF;
1632 return "parameter must be 'on' or 'off'";
1638 static const char *set_enable_sendfile(cmd_parms *cmd, void *d_,
1641 core_dir_config *d = d_;
1643 if (strcasecmp(arg, "on") == 0) {
1644 d->enable_sendfile = ENABLE_SENDFILE_ON;
1646 else if (strcasecmp(arg, "off") == 0) {
1647 d->enable_sendfile = ENABLE_SENDFILE_OFF;
1650 return "parameter must be 'on' or 'off'";
1658 * Report a missing-'>' syntax error.
1660 static char *unclosed_directive(cmd_parms *cmd)
1662 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1663 "> directive missing closing '>'", NULL);
1667 * Report a missing args in '<Foo >' syntax error.
1669 static char *missing_container_arg(cmd_parms *cmd)
1671 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1672 "> directive requires additional arguments", NULL);
1675 AP_CORE_DECLARE_NONSTD(const char *) ap_limit_section(cmd_parms *cmd,
1679 const char *endp = ap_strrchr_c(arg, '>');
1680 const char *limited_methods;
1681 void *tog = cmd->cmd->cmd_data;
1682 apr_int64_t limited = 0;
1683 apr_int64_t old_limited = cmd->limited;
1687 return unclosed_directive(cmd);
1690 limited_methods = apr_pstrndup(cmd->pool, arg, endp - arg);
1692 if (!limited_methods[0]) {
1693 return missing_container_arg(cmd);
1696 while (limited_methods[0]) {
1697 char *method = ap_getword_conf(cmd->pool, &limited_methods);
1700 /* check for builtin or module registered method number */
1701 methnum = ap_method_number_of(method);
1703 if (methnum == M_TRACE && !tog) {
1704 return "TRACE cannot be controlled by <Limit>, see TraceEnable";
1706 else if (methnum == M_INVALID) {
1707 /* method has not been registered yet, but resorce restriction
1708 * is always checked before method handling, so register it.
1710 methnum = ap_method_register(cmd->pool, method);
1713 limited |= (AP_METHOD_BIT << methnum);
1716 /* Killing two features with one function,
1717 * if (tog == NULL) <Limit>, else <LimitExcept>
1719 limited = tog ? ~limited : limited;
1721 if (!(old_limited & limited)) {
1722 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1723 "> directive excludes all methods", NULL);
1725 else if ((old_limited & limited) == old_limited) {
1726 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1727 "> directive specifies methods already excluded",
1731 cmd->limited &= limited;
1733 errmsg = ap_walk_config(cmd->directive->first_child, cmd, cmd->context);
1735 cmd->limited = old_limited;
1740 /* XXX: Bogus - need to do this differently (at least OS2/Netware suffer
1741 * the same problem!!!
1742 * We use this in <DirectoryMatch> and <FilesMatch>, to ensure that
1743 * people don't get bitten by wrong-cased regex matches
1747 #define USE_ICASE AP_REG_ICASE
1752 static const char *dirsection(cmd_parms *cmd, void *mconfig, const char *arg)
1755 const char *endp = ap_strrchr_c(arg, '>');
1756 int old_overrides = cmd->override;
1757 char *old_path = cmd->path;
1758 core_dir_config *conf;
1759 ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
1760 ap_regex_t *r = NULL;
1761 const command_rec *thiscmd = cmd->cmd;
1763 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
1769 return unclosed_directive(cmd);
1772 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1775 return missing_container_arg(cmd);
1779 if (thiscmd->cmd_data)
1780 return "<DirectoryMatch > block must specify a path";
1782 return "<Directory > block must specify a path";
1785 cmd->path = ap_getword_conf(cmd->pool, &arg);
1786 cmd->override = OR_ALL|ACCESS_CONF;
1788 if (!strcmp(cmd->path, "~")) {
1789 cmd->path = ap_getword_conf(cmd->pool, &arg);
1791 return "<Directory ~ > block must specify a path";
1792 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1794 return "Regex could not be compiled";
1797 else if (thiscmd->cmd_data) { /* <DirectoryMatch> */
1798 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1800 return "Regex could not be compiled";
1803 else if (!strcmp(cmd->path, "/") == 0)
1808 * Ensure that the pathname is canonical, and append the trailing /
1810 apr_status_t rv = apr_filepath_merge(&newpath, NULL, cmd->path,
1811 APR_FILEPATH_TRUENAME, cmd->pool);
1812 if (rv != APR_SUCCESS && rv != APR_EPATHWILD) {
1813 return apr_pstrcat(cmd->pool, "<Directory \"", cmd->path,
1814 "\"> path is invalid.", NULL);
1817 cmd->path = newpath;
1818 if (cmd->path[strlen(cmd->path) - 1] != '/')
1819 cmd->path = apr_pstrcat(cmd->pool, cmd->path, "/", NULL);
1822 /* initialize our config and fetch it */
1823 conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
1824 &core_module, cmd->pool);
1826 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
1831 conf->d = cmd->path;
1832 conf->d_is_fnmatch = (apr_fnmatch_test(conf->d) != 0);
1834 /* Make this explicit - the "/" root has 0 elements, that is, we
1835 * will always merge it, and it will always sort and merge first.
1836 * All others are sorted and tested by the number of slashes.
1838 if (strcmp(conf->d, "/") == 0)
1839 conf->d_components = 0;
1841 conf->d_components = ap_count_dirs(conf->d);
1843 ap_add_per_dir_conf(cmd->server, new_dir_conf);
1846 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1847 "> arguments not (yet) supported.", NULL);
1850 cmd->path = old_path;
1851 cmd->override = old_overrides;
1856 static const char *urlsection(cmd_parms *cmd, void *mconfig, const char *arg)
1859 const char *endp = ap_strrchr_c(arg, '>');
1860 int old_overrides = cmd->override;
1861 char *old_path = cmd->path;
1862 core_dir_config *conf;
1863 ap_regex_t *r = NULL;
1864 const command_rec *thiscmd = cmd->cmd;
1865 ap_conf_vector_t *new_url_conf = ap_create_per_dir_config(cmd->pool);
1866 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
1872 return unclosed_directive(cmd);
1875 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1878 return missing_container_arg(cmd);
1881 cmd->path = ap_getword_conf(cmd->pool, &arg);
1882 cmd->override = OR_ALL|ACCESS_CONF;
1884 if (thiscmd->cmd_data) { /* <LocationMatch> */
1885 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
1887 return "Regex could not be compiled";
1890 else if (!strcmp(cmd->path, "~")) {
1891 cmd->path = ap_getword_conf(cmd->pool, &arg);
1892 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
1894 return "Regex could not be compiled";
1898 /* initialize our config and fetch it */
1899 conf = ap_set_config_vectors(cmd->server, new_url_conf, cmd->path,
1900 &core_module, cmd->pool);
1902 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_url_conf);
1906 conf->d = apr_pstrdup(cmd->pool, cmd->path); /* No mangling, please */
1907 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1910 ap_add_per_url_conf(cmd->server, new_url_conf);
1913 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1914 "> arguments not (yet) supported.", NULL);
1917 cmd->path = old_path;
1918 cmd->override = old_overrides;
1923 static const char *filesection(cmd_parms *cmd, void *mconfig, const char *arg)
1926 const char *endp = ap_strrchr_c(arg, '>');
1927 int old_overrides = cmd->override;
1928 char *old_path = cmd->path;
1929 core_dir_config *conf;
1930 ap_regex_t *r = NULL;
1931 const command_rec *thiscmd = cmd->cmd;
1932 core_dir_config *c = mconfig;
1933 ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
1934 const char *err = ap_check_cmd_context(cmd,
1935 NOT_IN_LOCATION | NOT_IN_LIMIT);
1942 return unclosed_directive(cmd);
1945 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1948 return missing_container_arg(cmd);
1951 cmd->path = ap_getword_conf(cmd->pool, &arg);
1952 /* Only if not an .htaccess file */
1954 cmd->override = OR_ALL|ACCESS_CONF;
1957 if (thiscmd->cmd_data) { /* <FilesMatch> */
1958 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1960 return "Regex could not be compiled";
1963 else if (!strcmp(cmd->path, "~")) {
1964 cmd->path = ap_getword_conf(cmd->pool, &arg);
1965 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1967 return "Regex could not be compiled";
1972 /* Ensure that the pathname is canonical, but we
1973 * can't test the case/aliases without a fixed path */
1974 if (apr_filepath_merge(&newpath, "", cmd->path,
1975 0, cmd->pool) != APR_SUCCESS)
1976 return apr_pstrcat(cmd->pool, "<Files \"", cmd->path,
1977 "\"> is invalid.", NULL);
1978 cmd->path = newpath;
1981 /* initialize our config and fetch it */
1982 conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
1983 &core_module, cmd->pool);
1985 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
1989 conf->d = cmd->path;
1990 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1993 ap_add_file_conf(c, new_file_conf);
1996 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1997 "> arguments not (yet) supported.", NULL);
2000 cmd->path = old_path;
2001 cmd->override = old_overrides;
2005 static const char *ifsection(cmd_parms *cmd, void *mconfig, const char *arg)
2008 const char *endp = ap_strrchr_c(arg, '>');
2009 int old_overrides = cmd->override;
2010 char *old_path = cmd->path;
2011 core_dir_config *conf;
2012 const command_rec *thiscmd = cmd->cmd;
2013 core_dir_config *c = mconfig;
2014 ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
2015 const char *err = ap_check_cmd_context(cmd,
2016 NOT_IN_LOCATION | NOT_IN_LIMIT);
2017 const char *condition;
2025 return unclosed_directive(cmd);
2028 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2031 return missing_container_arg(cmd);
2034 condition = ap_getword_conf(cmd->pool, &arg);
2035 /* Only if not an .htaccess file */
2037 cmd->override = OR_ALL|ACCESS_CONF;
2040 /* initialize our config and fetch it */
2041 conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
2042 &core_module, cmd->pool);
2044 conf->condition = ap_expr_parse(cmd->pool, condition, &expr_err);
2046 return "Cannot parse condition clause";
2049 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
2053 conf->d = cmd->path;
2054 conf->d_is_fnmatch = 0;
2057 ap_add_file_conf(c, new_file_conf);
2060 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
2061 "> arguments not supported.", NULL);
2064 cmd->path = old_path;
2065 cmd->override = old_overrides;
2070 static const char *start_ifmod(cmd_parms *cmd, void *mconfig, const char *arg)
2072 const char *endp = ap_strrchr_c(arg, '>');
2073 int not = (arg[0] == '!');
2077 return unclosed_directive(cmd);
2080 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2087 return missing_container_arg(cmd);
2090 found = ap_find_linked_module(arg);
2092 /* search prelinked stuff */
2094 ap_module_symbol_t *current = ap_prelinked_module_symbols;
2096 for (; current->name; ++current) {
2097 if (!strcmp(current->name, arg)) {
2098 found = current->modp;
2104 /* search dynamic stuff */
2106 APR_OPTIONAL_FN_TYPE(ap_find_loaded_module_symbol) *check_symbol =
2107 APR_RETRIEVE_OPTIONAL_FN(ap_find_loaded_module_symbol);
2110 found = check_symbol(cmd->server, arg);
2114 if ((!not && found) || (not && !found)) {
2115 ap_directive_t *parent = NULL;
2116 ap_directive_t *current = NULL;
2119 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2120 ¤t, &parent, "<IfModule");
2121 *(ap_directive_t **)mconfig = current;
2125 *(ap_directive_t **)mconfig = NULL;
2126 return ap_soak_end_container(cmd, "<IfModule");
2130 AP_DECLARE(int) ap_exists_config_define(const char *name)
2135 defines = (char **)ap_server_config_defines->elts;
2136 for (i = 0; i < ap_server_config_defines->nelts; i++) {
2137 if (strcmp(defines[i], name) == 0) {
2145 static const char *start_ifdefine(cmd_parms *cmd, void *dummy, const char *arg)
2151 endp = ap_strrchr_c(arg, '>');
2153 return unclosed_directive(cmd);
2156 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2158 if (arg[0] == '!') {
2164 return missing_container_arg(cmd);
2167 defined = ap_exists_config_define(arg);
2168 if ((!not && defined) || (not && !defined)) {
2169 ap_directive_t *parent = NULL;
2170 ap_directive_t *current = NULL;
2173 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2174 ¤t, &parent, "<IfDefine");
2175 *(ap_directive_t **)dummy = current;
2179 *(ap_directive_t **)dummy = NULL;
2180 return ap_soak_end_container(cmd, "<IfDefine");
2184 /* httpd.conf commands... beginning with the <VirtualHost> business */
2186 static const char *virtualhost_section(cmd_parms *cmd, void *dummy,
2189 server_rec *main_server = cmd->server, *s;
2191 const char *endp = ap_strrchr_c(arg, '>');
2192 apr_pool_t *p = cmd->pool;
2194 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2200 return unclosed_directive(cmd);
2203 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2206 return missing_container_arg(cmd);
2209 /* FIXME: There's another feature waiting to happen here -- since you
2210 can now put multiple addresses/names on a single <VirtualHost>
2211 you might want to use it to group common definitions and then
2212 define other "subhosts" with their individual differences. But
2213 personally I'd rather just do it with a macro preprocessor. -djg */
2214 if (main_server->is_virtual) {
2215 return "<VirtualHost> doesn't nest!";
2218 errmsg = ap_init_virtual_host(p, arg, main_server, &s);
2223 s->next = main_server->next;
2224 main_server->next = s;
2226 s->defn_name = cmd->directive->filename;
2227 s->defn_line_number = cmd->directive->line_num;
2231 errmsg = ap_walk_config(cmd->directive->first_child, cmd,
2232 s->lookup_defaults);
2234 cmd->server = main_server;
2239 static const char *set_server_alias(cmd_parms *cmd, void *dummy,
2242 if (!cmd->server->names) {
2243 return "ServerAlias only used in <VirtualHost>";
2247 char **item, *name = ap_getword_conf(cmd->pool, &arg);
2249 if (ap_is_matchexp(name)) {
2250 item = (char **)apr_array_push(cmd->server->wild_names);
2253 item = (char **)apr_array_push(cmd->server->names);
2262 static const char *set_accf_map(cmd_parms *cmd, void *dummy,
2263 const char *iproto, const char* iaccf)
2265 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2266 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2274 proto = apr_pstrdup(cmd->pool, iproto);
2275 ap_str_tolower(proto);
2276 accf = apr_pstrdup(cmd->pool, iaccf);
2277 ap_str_tolower(accf);
2278 apr_table_setn(conf->accf_map, proto, accf);
2283 AP_DECLARE(const char*) ap_get_server_protocol(server_rec* s)
2285 core_server_config *conf = ap_get_module_config(s->module_config,
2287 return conf->protocol;
2290 AP_DECLARE(void) ap_set_server_protocol(server_rec* s, const char* proto)
2292 core_server_config *conf = ap_get_module_config(s->module_config,
2294 conf->protocol = proto;
2297 static const char *set_protocol(cmd_parms *cmd, void *dummy,
2300 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
2301 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2309 proto = apr_pstrdup(cmd->pool, arg);
2310 ap_str_tolower(proto);
2311 conf->protocol = proto;
2316 static const char *set_server_string_slot(cmd_parms *cmd, void *dummy,
2319 /* This one's pretty generic... */
2321 int offset = (int)(long)cmd->info;
2322 char *struct_ptr = (char *)cmd->server;
2324 const char *err = ap_check_cmd_context(cmd,
2325 NOT_IN_DIR_LOC_FILE);
2330 *(const char **)(struct_ptr + offset) = arg;
2335 * The ServerName directive takes one argument with format
2336 * [scheme://]fully-qualified-domain-name[:port], for instance
2337 * ServerName www.example.com
2338 * ServerName www.example.com:80
2339 * ServerName https://www.example.com:443
2342 static const char *server_hostname_port(cmd_parms *cmd, void *dummy, const char *arg)
2344 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
2345 const char *portstr, *part;
2353 part = ap_strstr_c(arg, "://");
2356 scheme = apr_pstrndup(cmd->pool, arg, part - arg);
2357 ap_str_tolower(scheme);
2358 cmd->server->server_scheme = (const char *)scheme;
2364 portstr = ap_strchr_c(part, ':');
2366 cmd->server->server_hostname = apr_pstrndup(cmd->pool, part,
2369 port = atoi(portstr);
2370 if (port <= 0 || port >= 65536) { /* 65536 == 1<<16 */
2371 return apr_pstrcat(cmd->temp_pool, "The port number \"", arg,
2372 "\" is outside the appropriate range "
2373 "(i.e., 1..65535).", NULL);
2377 cmd->server->server_hostname = apr_pstrdup(cmd->pool, part);
2381 cmd->server->port = port;
2385 static const char *set_signature_flag(cmd_parms *cmd, void *d_,
2388 core_dir_config *d = d_;
2390 if (strcasecmp(arg, "On") == 0) {
2391 d->server_signature = srv_sig_on;
2393 else if (strcasecmp(arg, "Off") == 0) {
2394 d->server_signature = srv_sig_off;
2396 else if (strcasecmp(arg, "EMail") == 0) {
2397 d->server_signature = srv_sig_withmail;
2400 return "ServerSignature: use one of: off | on | email";
2406 static const char *set_server_root(cmd_parms *cmd, void *dummy,
2409 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2415 if ((apr_filepath_merge((char**)&ap_server_root, NULL, arg,
2416 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS)
2417 || !ap_is_directory(cmd->pool, ap_server_root)) {
2418 return "ServerRoot must be a valid directory";
2424 static const char *set_timeout(cmd_parms *cmd, void *dummy, const char *arg)
2426 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
2432 cmd->server->timeout = apr_time_from_sec(atoi(arg));
2436 static const char *set_allow2f(cmd_parms *cmd, void *d_, int arg)
2438 core_dir_config *d = d_;
2440 d->allow_encoded_slashes = arg != 0;
2444 static const char *set_hostname_lookups(cmd_parms *cmd, void *d_,
2447 core_dir_config *d = d_;
2449 if (!strcasecmp(arg, "on")) {
2450 d->hostname_lookups = HOSTNAME_LOOKUP_ON;
2452 else if (!strcasecmp(arg, "off")) {
2453 d->hostname_lookups = HOSTNAME_LOOKUP_OFF;
2455 else if (!strcasecmp(arg, "double")) {
2456 d->hostname_lookups = HOSTNAME_LOOKUP_DOUBLE;
2459 return "parameter must be 'on', 'off', or 'double'";
2465 static const char *set_serverpath(cmd_parms *cmd, void *dummy,
2468 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
2474 cmd->server->path = arg;
2475 cmd->server->pathlen = (int)strlen(arg);
2479 static const char *set_content_md5(cmd_parms *cmd, void *d_, int arg)
2481 core_dir_config *d = d_;
2483 d->content_md5 = arg != 0;
2487 static const char *set_accept_path_info(cmd_parms *cmd, void *d_, const char *arg)
2489 core_dir_config *d = d_;
2491 if (strcasecmp(arg, "on") == 0) {
2492 d->accept_path_info = AP_REQ_ACCEPT_PATH_INFO;
2494 else if (strcasecmp(arg, "off") == 0) {
2495 d->accept_path_info = AP_REQ_REJECT_PATH_INFO;
2497 else if (strcasecmp(arg, "default") == 0) {
2498 d->accept_path_info = AP_REQ_DEFAULT_PATH_INFO;
2501 return "AcceptPathInfo must be set to on, off or default";
2507 static const char *set_use_canonical_name(cmd_parms *cmd, void *d_,
2510 core_dir_config *d = d_;
2512 if (strcasecmp(arg, "on") == 0) {
2513 d->use_canonical_name = USE_CANONICAL_NAME_ON;
2515 else if (strcasecmp(arg, "off") == 0) {
2516 d->use_canonical_name = USE_CANONICAL_NAME_OFF;
2518 else if (strcasecmp(arg, "dns") == 0) {
2519 d->use_canonical_name = USE_CANONICAL_NAME_DNS;
2522 return "parameter must be 'on', 'off', or 'dns'";
2528 static const char *set_use_canonical_phys_port(cmd_parms *cmd, void *d_,
2531 core_dir_config *d = d_;
2533 if (strcasecmp(arg, "on") == 0) {
2534 d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_ON;
2536 else if (strcasecmp(arg, "off") == 0) {
2537 d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_OFF;
2540 return "parameter must be 'on' or 'off'";
2547 static const char *include_config (cmd_parms *cmd, void *dummy,
2550 ap_directive_t *conftree = NULL;
2551 const char* conffile, *error;
2552 unsigned *recursion;
2555 apr_pool_userdata_get(&data, "ap_include_sentinel", cmd->pool);
2560 data = recursion = apr_palloc(cmd->pool, sizeof(*recursion));
2562 apr_pool_userdata_setn(data, "ap_include_sentinel", NULL, cmd->pool);
2565 if (++*recursion > AP_MAX_INCLUDE_DEPTH) {
2567 return apr_psprintf(cmd->pool, "Exceeded maximum include depth of %u. "
2568 "You have probably a recursion somewhere.",
2569 AP_MAX_INCLUDE_DEPTH);
2572 conffile = ap_server_root_relative(cmd->pool, name);
2575 return apr_pstrcat(cmd->pool, "Invalid Include path ",
2579 error = ap_process_resource_config(cmd->server, conffile,
2580 &conftree, cmd->pool, cmd->temp_pool);
2586 *(ap_directive_t **)dummy = conftree;
2588 /* recursion level done */
2596 static const char *set_loglevel(cmd_parms *cmd, void *dummy, const char *arg)
2600 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
2605 if ((str = ap_getword_conf(cmd->pool, &arg))) {
2606 if (!strcasecmp(str, "emerg")) {
2607 cmd->server->loglevel = APLOG_EMERG;
2609 else if (!strcasecmp(str, "alert")) {
2610 cmd->server->loglevel = APLOG_ALERT;
2612 else if (!strcasecmp(str, "crit")) {
2613 cmd->server->loglevel = APLOG_CRIT;
2615 else if (!strcasecmp(str, "error")) {
2616 cmd->server->loglevel = APLOG_ERR;
2618 else if (!strcasecmp(str, "warn")) {
2619 cmd->server->loglevel = APLOG_WARNING;
2621 else if (!strcasecmp(str, "notice")) {
2622 cmd->server->loglevel = APLOG_NOTICE;
2624 else if (!strcasecmp(str, "info")) {
2625 cmd->server->loglevel = APLOG_INFO;
2627 else if (!strcasecmp(str, "debug")) {
2628 cmd->server->loglevel = APLOG_DEBUG;
2631 return "LogLevel requires level keyword: one of "
2632 "emerg/alert/crit/error/warn/notice/info/debug";
2636 return "LogLevel requires level keyword";
2642 AP_DECLARE(const char *) ap_psignature(const char *prefix, request_rec *r)
2645 core_dir_config *conf;
2647 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
2649 if ((conf->server_signature == srv_sig_off)
2650 || (conf->server_signature == srv_sig_unset)) {
2654 apr_snprintf(sport, sizeof sport, "%u", (unsigned) ap_get_server_port(r));
2656 if (conf->server_signature == srv_sig_withmail) {
2657 return apr_pstrcat(r->pool, prefix, "<address>",
2658 ap_get_server_banner(),
2659 " Server at <a href=\"",
2660 ap_is_url(r->server->server_admin) ? "" : "mailto:",
2661 ap_escape_html(r->pool, r->server->server_admin),
2663 ap_escape_html(r->pool, ap_get_server_name(r)),
2664 "</a> Port ", sport,
2665 "</address>\n", NULL);
2668 return apr_pstrcat(r->pool, prefix, "<address>", ap_get_server_banner(),
2670 ap_escape_html(r->pool, ap_get_server_name(r)),
2672 "</address>\n", NULL);
2676 * Handle a request to include the server's OS platform in the Server
2677 * response header field (the ServerTokens directive). Unfortunately
2678 * this requires a new global in order to communicate the setting back to
2679 * http_main so it can insert the information in the right place in the
2683 static char *server_banner = NULL;
2684 static int banner_locked = 0;
2685 static char *server_description = NULL;
2687 enum server_token_type {
2688 SrvTk_MAJOR, /* eg: Apache/2 */
2689 SrvTk_MINOR, /* eg. Apache/2.0 */
2690 SrvTk_MINIMAL, /* eg: Apache/2.0.41 */
2691 SrvTk_OS, /* eg: Apache/2.0.41 (UNIX) */
2692 SrvTk_FULL, /* eg: Apache/2.0.41 (UNIX) PHP/4.2.2 FooBar/1.2b */
2693 SrvTk_PRODUCT_ONLY /* eg: Apache */
2695 static enum server_token_type ap_server_tokens = SrvTk_FULL;
2697 static apr_status_t reset_banner(void *dummy)
2700 ap_server_tokens = SrvTk_FULL;
2701 server_banner = NULL;
2702 server_description = NULL;
2706 AP_DECLARE(void) ap_get_server_revision(ap_version_t *version)
2708 version->major = AP_SERVER_MAJORVERSION_NUMBER;
2709 version->minor = AP_SERVER_MINORVERSION_NUMBER;
2710 version->patch = AP_SERVER_PATCHLEVEL_NUMBER;
2711 version->add_string = AP_SERVER_ADD_STRING;
2714 AP_DECLARE(const char *) ap_get_server_description(void)
2716 return server_description ? server_description :
2717 AP_SERVER_BASEVERSION " (" PLATFORM ")";
2720 AP_DECLARE(const char *) ap_get_server_banner(void)
2722 return server_banner ? server_banner : AP_SERVER_BASEVERSION;
2725 AP_DECLARE(void) ap_add_version_component(apr_pool_t *pconf, const char *component)
2727 if (! banner_locked) {
2729 * If the version string is null, register our cleanup to reset the
2730 * pointer on pool destruction. We also know that, if NULL,
2731 * we are adding the original SERVER_BASEVERSION string.
2733 if (server_banner == NULL) {
2734 apr_pool_cleanup_register(pconf, NULL, reset_banner,
2735 apr_pool_cleanup_null);
2736 server_banner = apr_pstrdup(pconf, component);
2740 * Tack the given component identifier to the end of
2741 * the existing string.
2743 server_banner = apr_pstrcat(pconf, server_banner, " ",
2747 server_description = apr_pstrcat(pconf, server_description, " ",
2752 * This routine adds the real server base identity to the banner string,
2753 * and then locks out changes until the next reconfig.
2755 static void set_banner(apr_pool_t *pconf)
2757 if (ap_server_tokens == SrvTk_PRODUCT_ONLY) {
2758 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT);
2760 else if (ap_server_tokens == SrvTk_MINIMAL) {
2761 ap_add_version_component(pconf, AP_SERVER_BASEVERSION);
2763 else if (ap_server_tokens == SrvTk_MINOR) {
2764 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MINORREVISION);
2766 else if (ap_server_tokens == SrvTk_MAJOR) {
2767 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MAJORVERSION);
2770 ap_add_version_component(pconf, AP_SERVER_BASEVERSION " (" PLATFORM ")");
2774 * Lock the server_banner string if we're not displaying
2775 * the full set of tokens
2777 if (ap_server_tokens != SrvTk_FULL) {
2780 server_description = AP_SERVER_BASEVERSION " (" PLATFORM ")";
2783 static const char *set_serv_tokens(cmd_parms *cmd, void *dummy,
2784 const char *arg1, const char *arg2)
2786 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2792 if (!strcasecmp(arg1, "OS")) {
2793 ap_server_tokens = SrvTk_OS;
2795 else if (!strcasecmp(arg1, "Min") || !strcasecmp(arg1, "Minimal")) {
2796 ap_server_tokens = SrvTk_MINIMAL;
2798 else if (!strcasecmp(arg1, "Major")) {
2799 ap_server_tokens = SrvTk_MAJOR;
2801 else if (!strcasecmp(arg1, "Minor") ) {
2802 ap_server_tokens = SrvTk_MINOR;
2804 else if (!strcasecmp(arg1, "Prod") || !strcasecmp(arg1, "ProductOnly")) {
2805 ap_server_tokens = SrvTk_PRODUCT_ONLY;
2808 ap_server_tokens = SrvTk_FULL;
2814 static const char *set_limit_req_line(cmd_parms *cmd, void *dummy,
2817 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
2826 return apr_pstrcat(cmd->temp_pool, "LimitRequestLine \"", arg,
2827 "\" must be a non-negative integer", NULL);
2830 cmd->server->limit_req_line = lim;
2834 static const char *set_limit_req_fieldsize(cmd_parms *cmd, void *dummy,
2837 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
2846 return apr_pstrcat(cmd->temp_pool, "LimitRequestFieldsize \"", arg,
2847 "\" must be a non-negative integer",
2851 cmd->server->limit_req_fieldsize = lim;
2855 static const char *set_limit_req_fields(cmd_parms *cmd, void *dummy,
2858 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE);
2867 return apr_pstrcat(cmd->temp_pool, "LimitRequestFields \"", arg,
2868 "\" must be a non-negative integer (0 = no limit)",
2872 cmd->server->limit_req_fields = lim;
2876 static const char *set_limit_req_body(cmd_parms *cmd, void *conf_,
2879 core_dir_config *conf = conf_;
2882 if (APR_SUCCESS != apr_strtoff(&conf->limit_req_body, arg, &errp, 10)) {
2883 return "LimitRequestBody argument is not parsable.";
2885 if (*errp || conf->limit_req_body < 0) {
2886 return "LimitRequestBody requires a non-negative integer.";
2892 static const char *set_limit_xml_req_body(cmd_parms *cmd, void *conf_,
2895 core_dir_config *conf = conf_;
2897 conf->limit_xml_body = atol(arg);
2898 if (conf->limit_xml_body < 0)
2899 return "LimitXMLRequestBody requires a non-negative integer.";
2904 AP_DECLARE(size_t) ap_get_limit_xml_body(const request_rec *r)
2906 core_dir_config *conf;
2908 conf = ap_get_module_config(r->per_dir_config, &core_module);
2909 if (conf->limit_xml_body == AP_LIMIT_UNSET)
2910 return AP_DEFAULT_LIMIT_XML_BODY;
2912 return (size_t)conf->limit_xml_body;
2915 #if !defined (RLIMIT_CPU) || !(defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)) || !defined (RLIMIT_NPROC)
2916 static const char *no_set_limit(cmd_parms *cmd, void *conf_,
2917 const char *arg, const char *arg2)
2919 ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server,
2920 "%s not supported on this platform", cmd->cmd->name);
2927 static const char *set_limit_cpu(cmd_parms *cmd, void *conf_,
2928 const char *arg, const char *arg2)
2930 core_dir_config *conf = conf_;
2932 ap_unixd_set_rlimit(cmd, &conf->limit_cpu, arg, arg2, RLIMIT_CPU);
2937 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
2938 static const char *set_limit_mem(cmd_parms *cmd, void *conf_,
2939 const char *arg, const char * arg2)
2941 core_dir_config *conf = conf_;
2943 #if defined(RLIMIT_AS)
2944 ap_unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2 ,RLIMIT_AS);
2945 #elif defined(RLIMIT_DATA)
2946 ap_unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_DATA);
2947 #elif defined(RLIMIT_VMEM)
2948 ap_unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_VMEM);
2956 static const char *set_limit_nproc(cmd_parms *cmd, void *conf_,
2957 const char *arg, const char * arg2)
2959 core_dir_config *conf = conf_;
2961 ap_unixd_set_rlimit(cmd, &conf->limit_nproc, arg, arg2, RLIMIT_NPROC);
2966 static const char *set_recursion_limit(cmd_parms *cmd, void *dummy,
2967 const char *arg1, const char *arg2)
2969 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2971 int limit = atoi(arg1);
2974 return "The recursion limit must be greater than zero.";
2977 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2978 "Limiting internal redirects to very low numbers may "
2979 "cause normal requests to fail.");
2982 conf->redirect_limit = limit;
2988 return "The recursion limit must be greater than zero.";
2991 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2992 "Limiting the subrequest depth to a very low level may"
2993 " cause normal requests to fail.");
2997 conf->subreq_limit = limit;
3002 static void log_backtrace(const request_rec *r)
3004 const request_rec *top = r;
3006 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
3007 "r->uri = %s", r->uri ? r->uri : "(unexpectedly NULL)");
3009 while (top && (top->prev || top->main)) {
3012 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
3013 "redirected from r->uri = %s",
3014 top->uri ? top->uri : "(unexpectedly NULL)");
3017 if (!top->prev && top->main) {
3019 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
3020 "subrequested from r->uri = %s",
3021 top->uri ? top->uri : "(unexpectedly NULL)");
3027 * check whether redirect limit is reached
3029 AP_DECLARE(int) ap_is_recursion_limit_exceeded(const request_rec *r)
3031 core_server_config *conf = ap_get_module_config(r->server->module_config,
3033 const request_rec *top = r;
3034 int redirects = 0, subreqs = 0;
3035 int rlimit = conf->redirect_limit
3036 ? conf->redirect_limit
3037 : AP_DEFAULT_MAX_INTERNAL_REDIRECTS;
3038 int slimit = conf->subreq_limit
3039 ? conf->subreq_limit
3040 : AP_DEFAULT_MAX_SUBREQ_DEPTH;
3043 while (top->prev || top->main) {
3045 if (++redirects >= rlimit) {
3046 /* uuh, too much. */
3047 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3048 "Request exceeded the limit of %d internal "
3049 "redirects due to probable configuration error. "
3050 "Use 'LimitInternalRecursion' to increase the "
3051 "limit if necessary. Use 'LogLevel debug' to get "
3052 "a backtrace.", rlimit);
3054 /* post backtrace */
3057 /* return failure */
3064 if (!top->prev && top->main) {
3065 if (++subreqs >= slimit) {
3066 /* uuh, too much. */
3067 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3068 "Request exceeded the limit of %d subrequest "
3069 "nesting levels due to probable configuration "
3070 "error. Use 'LimitInternalRecursion' to increase "
3071 "the limit if necessary. Use 'LogLevel debug' to "
3072 "get a backtrace.", slimit);
3074 /* post backtrace */
3077 /* return failure */
3085 /* recursion state: ok */
3089 static const char *add_ct_output_filters(cmd_parms *cmd, void *conf_,
3090 const char *arg, const char *arg2)
3092 core_dir_config *conf = conf_;
3093 ap_filter_rec_t *old, *new = NULL;
3094 const char *filter_name;
3096 if (!conf->ct_output_filters) {
3097 conf->ct_output_filters = apr_hash_make(cmd->pool);
3101 old = (ap_filter_rec_t*) apr_hash_get(conf->ct_output_filters, arg2,
3102 APR_HASH_KEY_STRING);
3103 /* find last entry */
3112 (filter_name = ap_getword(cmd->pool, &arg, ';')) &&
3113 strcmp(filter_name, "")) {
3114 new = apr_pcalloc(cmd->pool, sizeof(ap_filter_rec_t));
3115 new->name = filter_name;
3117 /* We found something, so let's append it. */
3122 apr_hash_set(conf->ct_output_filters, arg2,
3123 APR_HASH_KEY_STRING, new);
3129 return "invalid filter name";
3135 * Insert filters requested by the AddOutputFilterByType
3136 * configuration directive. We cannot add filters based
3137 * on content-type until after the handler has started
3138 * to run. Only then do we reliably know the content-type.
3140 void ap_add_output_filters_by_type(request_rec *r)
3142 core_dir_config *conf;
3145 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3148 /* We can't do anything with no content-type or if we don't have a
3149 * filter configured.
3151 if (!r->content_type || !conf->ct_output_filters) {
3155 /* remove c-t decoration */
3156 ctype = ap_field_noparam(r->pool, r->content_type);
3158 ap_filter_rec_t *ct_filter;
3159 ct_filter = apr_hash_get(conf->ct_output_filters, ctype,
3160 APR_HASH_KEY_STRING);
3162 ap_add_output_filter(ct_filter->name, NULL, r, r->connection);
3163 ct_filter = ct_filter->next;
3170 static const char *set_trace_enable(cmd_parms *cmd, void *dummy,
3173 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
3176 if (strcasecmp(arg1, "on") == 0) {
3177 conf->trace_enable = AP_TRACE_ENABLE;
3179 else if (strcasecmp(arg1, "off") == 0) {
3180 conf->trace_enable = AP_TRACE_DISABLE;
3182 else if (strcasecmp(arg1, "extended") == 0) {
3183 conf->trace_enable = AP_TRACE_EXTENDED;
3186 return "TraceEnable must be one of 'on', 'off', or 'extended'";
3192 /* Note --- ErrorDocument will now work from .htaccess files.
3193 * The AllowOverride of Fileinfo allows webmasters to turn it off
3196 static const command_rec core_cmds[] = {
3198 /* Old access config file commands */
3200 AP_INIT_RAW_ARGS("<Directory", dirsection, NULL, RSRC_CONF,
3201 "Container for directives affecting resources located in the specified "
3203 AP_INIT_RAW_ARGS("<Location", urlsection, NULL, RSRC_CONF,
3204 "Container for directives affecting resources accessed through the "
3205 "specified URL paths"),
3206 AP_INIT_RAW_ARGS("<VirtualHost", virtualhost_section, NULL, RSRC_CONF,
3207 "Container to map directives to a particular virtual host, takes one or "
3208 "more host addresses"),
3209 AP_INIT_RAW_ARGS("<Files", filesection, NULL, OR_ALL,
3210 "Container for directives affecting files matching specified patterns"),
3211 AP_INIT_RAW_ARGS("<Limit", ap_limit_section, NULL, OR_LIMIT | OR_AUTHCFG,
3212 "Container for authentication directives when accessed using specified HTTP "
3214 AP_INIT_RAW_ARGS("<LimitExcept", ap_limit_section, (void*)1,
3215 OR_LIMIT | OR_AUTHCFG,
3216 "Container for authentication directives to be applied when any HTTP "
3217 "method other than those specified is used to access the resource"),
3218 AP_INIT_TAKE1("<IfModule", start_ifmod, NULL, EXEC_ON_READ | OR_ALL,
3219 "Container for directives based on existance of specified modules"),
3220 AP_INIT_TAKE1("<IfDefine", start_ifdefine, NULL, EXEC_ON_READ | OR_ALL,
3221 "Container for directives based on existance of command line defines"),
3222 AP_INIT_RAW_ARGS("<DirectoryMatch", dirsection, (void*)1, RSRC_CONF,
3223 "Container for directives affecting resources located in the "
3224 "specified directories"),
3225 AP_INIT_RAW_ARGS("<LocationMatch", urlsection, (void*)1, RSRC_CONF,
3226 "Container for directives affecting resources accessed through the "
3227 "specified URL paths"),
3228 AP_INIT_RAW_ARGS("<FilesMatch", filesection, (void*)1, OR_ALL,
3229 "Container for directives affecting files matching specified patterns"),
3231 AP_INIT_TAKE1("GprofDir", set_gprof_dir, NULL, RSRC_CONF,
3232 "Directory to plop gmon.out files"),
3234 AP_INIT_TAKE1("AddDefaultCharset", set_add_default_charset, NULL, OR_FILEINFO,
3235 "The name of the default charset to add to any Content-Type without one or 'Off' to disable"),
3236 AP_INIT_TAKE1("AcceptPathInfo", set_accept_path_info, NULL, OR_FILEINFO,
3237 "Set to on or off for PATH_INFO to be accepted by handlers, or default for the per-handler preference"),
3238 AP_INIT_TAKE1("Define", set_define, NULL, RSRC_CONF,
3239 "Define the existance of a variable. Same as passing -D to the command line."),
3240 AP_INIT_RAW_ARGS("<If", ifsection, NULL, OR_ALL,
3241 "Container for directives to be conditionally applied"),
3243 /* Old resource config file commands */
3245 AP_INIT_RAW_ARGS("AccessFileName", set_access_name, NULL, RSRC_CONF,
3246 "Name(s) of per-directory config files (default: .htaccess)"),
3247 AP_INIT_TAKE1("DocumentRoot", set_document_root, NULL, RSRC_CONF,
3248 "Root directory of the document tree"),
3249 AP_INIT_TAKE2("ErrorDocument", set_error_document, NULL, OR_FILEINFO,
3250 "Change responses for HTTP errors"),
3251 AP_INIT_RAW_ARGS("AllowOverride", set_override, NULL, ACCESS_CONF,
3252 "Controls what groups of directives can be configured by per-directory "
3254 AP_INIT_RAW_ARGS("Options", set_options, NULL, OR_OPTIONS,
3255 "Set a number of attributes for a given directory"),
3256 AP_INIT_TAKE1("DefaultType", set_default_type, NULL, OR_FILEINFO,
3257 "the default media type for otherwise untyped files (DEPRECATED)"),
3258 AP_INIT_RAW_ARGS("FileETag", set_etag_bits, NULL, OR_FILEINFO,
3259 "Specify components used to construct a file's ETag"),
3260 AP_INIT_TAKE1("EnableMMAP", set_enable_mmap, NULL, OR_FILEINFO,
3261 "Controls whether memory-mapping may be used to read files"),
3262 AP_INIT_TAKE1("EnableSendfile", set_enable_sendfile, NULL, OR_FILEINFO,
3263 "Controls whether sendfile may be used to transmit files"),
3265 /* Old server config file commands */
3267 AP_INIT_TAKE1("Protocol", set_protocol, NULL, RSRC_CONF,
3268 "Set the Protocol for httpd to use."),
3269 AP_INIT_TAKE2("AcceptFilter", set_accf_map, NULL, RSRC_CONF,
3270 "Set the Accept Filter to use for a protocol"),
3271 AP_INIT_TAKE1("Port", ap_set_deprecated, NULL, RSRC_CONF,
3272 "Port was replaced with Listen in Apache 2.0"),
3273 AP_INIT_TAKE1("HostnameLookups", set_hostname_lookups, NULL,
3274 ACCESS_CONF|RSRC_CONF,
3275 "\"on\" to enable, \"off\" to disable reverse DNS lookups, or \"double\" to "
3276 "enable double-reverse DNS lookups"),
3277 AP_INIT_TAKE1("ServerAdmin", set_server_string_slot,
3278 (void *)APR_OFFSETOF(server_rec, server_admin), RSRC_CONF,
3279 "The email address of the server administrator"),
3280 AP_INIT_TAKE1("ServerName", server_hostname_port, NULL, RSRC_CONF,
3281 "The hostname and port of the server"),
3282 AP_INIT_TAKE1("ServerSignature", set_signature_flag, NULL, OR_ALL,
3283 "En-/disable server signature (on|off|email)"),
3284 AP_INIT_TAKE1("ServerRoot", set_server_root, NULL, RSRC_CONF | EXEC_ON_READ,
3285 "Common directory of server-related files (logs, confs, etc.)"),
3286 AP_INIT_TAKE1("ErrorLog", set_server_string_slot,
3287 (void *)APR_OFFSETOF(server_rec, error_fname), RSRC_CONF,
3288 "The filename of the error log"),
3289 AP_INIT_RAW_ARGS("ServerAlias", set_server_alias, NULL, RSRC_CONF,
3290 "A name or names alternately used to access the server"),
3291 AP_INIT_TAKE1("ServerPath", set_serverpath, NULL, RSRC_CONF,
3292 "The pathname the server can be reached at"),
3293 AP_INIT_TAKE1("Timeout", set_timeout, NULL, RSRC_CONF,
3294 "Timeout duration (sec)"),
3295 AP_INIT_FLAG("ContentDigest", set_content_md5, NULL, OR_OPTIONS,
3296 "whether or not to send a Content-MD5 header with each request"),
3297 AP_INIT_TAKE1("UseCanonicalName", set_use_canonical_name, NULL,
3298 RSRC_CONF|ACCESS_CONF,
3299 "How to work out the ServerName : Port when constructing URLs"),
3300 AP_INIT_TAKE1("UseCanonicalPhysicalPort", set_use_canonical_phys_port, NULL,
3301 RSRC_CONF|ACCESS_CONF,
3302 "Whether to use the physical Port when constructing URLs"),
3303 /* TODO: RlimitFoo should all be part of mod_cgi, not in the core */
3304 /* TODO: ListenBacklog in MPM */
3305 AP_INIT_TAKE1("Include", include_config, NULL,
3306 (RSRC_CONF | ACCESS_CONF | EXEC_ON_READ),
3307 "Name of the config file to be included"),
3308 AP_INIT_TAKE1("LogLevel", set_loglevel, NULL, RSRC_CONF,
3309 "Level of verbosity in error logging"),
3310 AP_INIT_TAKE1("NameVirtualHost", ap_set_name_virtual_host, NULL, RSRC_CONF,
3311 "A numeric IP address:port, or the name of a host"),
3312 AP_INIT_TAKE12("ServerTokens", set_serv_tokens, NULL, RSRC_CONF,
3313 "Determine tokens displayed in the Server: header - Min(imal), "
3314 "Major, Minor, Prod, OS or Full"),
3315 AP_INIT_TAKE1("LimitRequestLine", set_limit_req_line, NULL, RSRC_CONF,
3316 "Limit on maximum size of an HTTP request line"),
3317 AP_INIT_TAKE1("LimitRequestFieldsize", set_limit_req_fieldsize, NULL,
3319 "Limit on maximum size of an HTTP request header field"),
3320 AP_INIT_TAKE1("LimitRequestFields", set_limit_req_fields, NULL, RSRC_CONF,
3321 "Limit (0 = unlimited) on max number of header fields in a request message"),
3322 AP_INIT_TAKE1("LimitRequestBody", set_limit_req_body,
3323 (void*)APR_OFFSETOF(core_dir_config, limit_req_body), OR_ALL,
3324 "Limit (in bytes) on maximum size of request message body"),
3325 AP_INIT_TAKE1("LimitXMLRequestBody", set_limit_xml_req_body, NULL, OR_ALL,
3326 "Limit (in bytes) on maximum size of an XML-based request "
3328 AP_INIT_RAW_ARGS("Mutex", ap_set_mutex, NULL, RSRC_CONF,
3329 "mutex (or \"default\") and mechanism"),
3331 /* System Resource Controls */
3333 AP_INIT_TAKE12("RLimitCPU", set_limit_cpu,
3334 (void*)APR_OFFSETOF(core_dir_config, limit_cpu),
3335 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3337 AP_INIT_TAKE12("RLimitCPU", no_set_limit, NULL,
3338 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3340 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined (RLIMIT_AS)
3341 AP_INIT_TAKE12("RLimitMEM", set_limit_mem,
3342 (void*)APR_OFFSETOF(core_dir_config, limit_mem),
3343 OR_ALL, "Soft/hard limits for max memory usage per process"),
3345 AP_INIT_TAKE12("RLimitMEM", no_set_limit, NULL,
3346 OR_ALL, "Soft/hard limits for max memory usage per process"),
3349 AP_INIT_TAKE12("RLimitNPROC", set_limit_nproc,
3350 (void*)APR_OFFSETOF(core_dir_config, limit_nproc),
3351 OR_ALL, "soft/hard limits for max number of processes per uid"),
3353 AP_INIT_TAKE12("RLimitNPROC", no_set_limit, NULL,
3354 OR_ALL, "soft/hard limits for max number of processes per uid"),
3357 /* internal recursion stopper */
3358 AP_INIT_TAKE12("LimitInternalRecursion", set_recursion_limit, NULL, RSRC_CONF,
3359 "maximum recursion depth of internal redirects and subrequests"),
3361 AP_INIT_TAKE1("ForceType", ap_set_string_slot_lower,
3362 (void *)APR_OFFSETOF(core_dir_config, mime_type), OR_FILEINFO,
3363 "a mime type that overrides other configured type"),
3364 AP_INIT_TAKE1("SetHandler", ap_set_string_slot_lower,
3365 (void *)APR_OFFSETOF(core_dir_config, handler), OR_FILEINFO,
3366 "a handler name that overrides any other configured handler"),
3367 AP_INIT_TAKE1("SetOutputFilter", ap_set_string_slot,
3368 (void *)APR_OFFSETOF(core_dir_config, output_filters), OR_FILEINFO,
3369 "filter (or ; delimited list of filters) to be run on the request content"),
3370 AP_INIT_TAKE1("SetInputFilter", ap_set_string_slot,
3371 (void *)APR_OFFSETOF(core_dir_config, input_filters), OR_FILEINFO,
3372 "filter (or ; delimited list of filters) to be run on the request body"),
3373 AP_INIT_ITERATE2("AddOutputFilterByType", add_ct_output_filters,
3374 (void *)APR_OFFSETOF(core_dir_config, ct_output_filters), OR_FILEINFO,
3375 "output filter name followed by one or more content-types"),
3376 AP_INIT_FLAG("AllowEncodedSlashes", set_allow2f, NULL, RSRC_CONF,
3377 "Allow URLs containing '/' encoded as '%2F'"),
3380 * These are default configuration directives that mpms can/should
3382 * XXX These are not for all platforms, and even some Unix MPMs might not want
3385 AP_INIT_TAKE1("PidFile", ap_mpm_set_pidfile, NULL, RSRC_CONF,
3386 "A file for logging the server process ID"),
3387 AP_INIT_TAKE1("ScoreBoardFile", ap_mpm_set_scoreboard, NULL, RSRC_CONF,
3388 "A file for Apache to maintain runtime process management information"),
3389 AP_INIT_TAKE1("MaxRequestsPerChild", ap_mpm_set_max_requests, NULL, RSRC_CONF,
3390 "Maximum number of requests a particular child serves before dying."),
3391 AP_INIT_TAKE1("CoreDumpDirectory", ap_mpm_set_coredumpdir, NULL, RSRC_CONF,
3392 "The location of the directory Apache changes to before dumping core"),
3393 AP_INIT_TAKE1("MaxMemFree", ap_mpm_set_max_mem_free, NULL, RSRC_CONF,
3394 "Maximum number of 1k blocks a particular childs allocator may hold."),
3395 AP_INIT_TAKE1("ThreadStackSize", ap_mpm_set_thread_stacksize, NULL, RSRC_CONF,
3396 "Size in bytes of stack used by threads handling client connections"),
3397 #if AP_ENABLE_EXCEPTION_HOOK
3398 AP_INIT_TAKE1("EnableExceptionHook", ap_mpm_set_exception_hook, NULL, RSRC_CONF,
3399 "Controls whether exception hook may be called after a crash"),
3401 AP_INIT_TAKE1("TraceEnable", set_trace_enable, NULL, RSRC_CONF,
3402 "'on' (default), 'off' or 'extended' to trace request body content"),
3406 /*****************************************************************
3408 * Core handlers for various phases of server operation...
3411 AP_DECLARE_NONSTD(int) ap_core_translate(request_rec *r)
3413 void *sconf = r->server->module_config;
3414 core_server_config *conf = ap_get_module_config(sconf, &core_module);
3417 /* XXX this seems too specific, this should probably become
3418 * some general-case test
3421 return HTTP_FORBIDDEN;
3423 if (!r->uri || ((r->uri[0] != '/') && strcmp(r->uri, "*"))) {
3424 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3425 "Invalid URI in request %s", r->the_request);
3426 return HTTP_BAD_REQUEST;
3430 && !strncmp(r->uri, r->server->path, r->server->pathlen)
3431 && (r->server->path[r->server->pathlen - 1] == '/'
3432 || r->uri[r->server->pathlen] == '/'
3433 || r->uri[r->server->pathlen] == '\0'))
3435 /* skip all leading /'s (e.g. http://localhost///foo)
3436 * so we are looking at only the relative path.
3438 char *path = r->uri + r->server->pathlen;
3439 while (*path == '/') {
3442 if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3443 APR_FILEPATH_TRUENAME
3444 | APR_FILEPATH_SECUREROOT, r->pool))
3446 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3447 "Cannot map %s to file", r->the_request);
3448 return HTTP_FORBIDDEN;
3450 r->canonical_filename = r->filename;
3454 * Make sure that we do not mess up the translation by adding two
3455 * /'s in a row. This happens under windows when the document
3456 * root ends with a /
3458 /* skip all leading /'s (e.g. http://localhost///foo)
3459 * so we are looking at only the relative path.
3461 char *path = r->uri;
3462 while (*path == '/') {
3465 if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3466 APR_FILEPATH_TRUENAME
3467 | APR_FILEPATH_SECUREROOT, r->pool))
3469 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3470 "Cannot map %s to file", r->the_request);
3471 return HTTP_FORBIDDEN;
3473 r->canonical_filename = r->filename;
3479 /*****************************************************************
3481 * Test the filesystem name through directory_walk and file_walk
3483 static int core_map_to_storage(request_rec *r)
3487 if ((access_status = ap_directory_walk(r))) {
3488 return access_status;
3491 if ((access_status = ap_file_walk(r))) {
3492 return access_status;
3499 static int do_nothing(request_rec *r) { return OK; }
3502 static int core_override_type(request_rec *r)
3504 core_dir_config *conf =
3505 (core_dir_config *)ap_get_module_config(r->per_dir_config,
3508 /* Check for overrides with ForceType / SetHandler
3510 if (conf->mime_type && strcmp(conf->mime_type, "none"))
3511 ap_set_content_type(r, (char*) conf->mime_type);
3513 if (conf->handler && strcmp(conf->handler, "none"))
3514 r->handler = conf->handler;
3516 /* Deal with the poor soul who is trying to force path_info to be
3517 * accepted within the core_handler, where they will let the subreq
3518 * address its contents. This is toggled by the user in the very
3519 * beginning of the fixup phase, so modules should override the user's
3520 * discretion in their own module fixup phase. It is tristate, if
3521 * the user doesn't specify, the result is 2 (which the module may
3522 * interpret to its own customary behavior.) It won't be touched
3523 * if the value is no longer undefined (2), so any module changing
3524 * the value prior to the fixup phase OVERRIDES the user's choice.
3526 if ((r->used_path_info == AP_REQ_DEFAULT_PATH_INFO)
3527 && (conf->accept_path_info != 3)) {
3528 r->used_path_info = conf->accept_path_info;
3534 static int default_handler(request_rec *r)
3536 conn_rec *c = r->connection;
3537 apr_bucket_brigade *bb;
3541 apr_file_t *fd = NULL;
3542 apr_status_t status;
3543 /* XXX if/when somebody writes a content-md5 filter we either need to
3544 * remove this support or coordinate when to use the filter vs.
3545 * when to use this code
3546 * The current choice of when to compute the md5 here matches the 1.3
3547 * support fairly closely (unlike 1.3, we don't handle computing md5
3548 * when the charset is translated).
3550 int bld_content_md5;
3552 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3554 bld_content_md5 = (d->content_md5 & 1)
3555 && r->output_filters->frec->ftype != AP_FTYPE_RESOURCE;
3557 ap_allow_standard_methods(r, MERGE_ALLOW, M_GET, M_OPTIONS, M_POST, -1);
3559 /* If filters intend to consume the request body, they must
3560 * register an InputFilter to slurp the contents of the POST
3561 * data from the POST input stream. It no longer exists when
3562 * the output filters are invoked by the default handler.
3564 if ((errstatus = ap_discard_request_body(r)) != OK) {
3568 if (r->method_number == M_GET || r->method_number == M_POST) {
3569 if (r->finfo.filetype == 0) {
3570 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3571 "File does not exist: %s", r->filename);
3572 return HTTP_NOT_FOUND;
3575 /* Don't try to serve a dir. Some OSs do weird things with
3578 if (r->finfo.filetype == APR_DIR) {
3579 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3580 "Attempt to serve directory: %s", r->filename);
3581 return HTTP_NOT_FOUND;
3584 if ((r->used_path_info != AP_REQ_ACCEPT_PATH_INFO) &&
3585 r->path_info && *r->path_info)
3587 /* default to reject */
3588 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3589 "File does not exist: %s",
3590 apr_pstrcat(r->pool, r->filename, r->path_info, NULL));
3591 return HTTP_NOT_FOUND;
3594 /* We understood the (non-GET) method, but it might not be legal for
3595 this particular resource. Check to see if the 'deliver_script'
3596 flag is set. If so, then we go ahead and deliver the file since
3597 it isn't really content (only GET normally returns content).
3599 Note: based on logic further above, the only possible non-GET
3600 method at this point is POST. In the future, we should enable
3601 script delivery for all methods. */
3602 if (r->method_number != M_GET) {
3603 core_request_config *req_cfg;
3605 req_cfg = ap_get_module_config(r->request_config, &core_module);
3606 if (!req_cfg->deliver_script) {
3607 /* The flag hasn't been set for this request. Punt. */
3608 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3609 "This resource does not accept the %s method.",
3611 return HTTP_METHOD_NOT_ALLOWED;
3616 if ((status = apr_file_open(&fd, r->filename, APR_READ | APR_BINARY
3617 #if APR_HAS_SENDFILE
3618 | ((d->enable_sendfile == ENABLE_SENDFILE_OFF)
3619 ? 0 : APR_SENDFILE_ENABLED)
3621 , 0, r->pool)) != APR_SUCCESS) {
3622 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
3623 "file permissions deny server access: %s", r->filename);
3624 return HTTP_FORBIDDEN;
3627 ap_update_mtime(r, r->finfo.mtime);
3628 ap_set_last_modified(r);
3630 apr_table_setn(r->headers_out, "Accept-Ranges", "bytes");
3631 ap_set_content_length(r, r->finfo.size);
3632 if (bld_content_md5) {
3633 apr_table_setn(r->headers_out, "Content-MD5",
3634 ap_md5digest(r->pool, fd));
3637 bb = apr_brigade_create(r->pool, c->bucket_alloc);
3639 if ((errstatus = ap_meets_conditions(r)) != OK) {
3641 r->status = errstatus;
3644 e = apr_brigade_insert_file(bb, fd, 0, r->finfo.size, r->pool);
3647 if (d->enable_mmap == ENABLE_MMAP_OFF) {
3648 (void)apr_bucket_file_enable_mmap(e, 0);
3653 e = apr_bucket_eos_create(c->bucket_alloc);
3654 APR_BRIGADE_INSERT_TAIL(bb, e);
3656 status = ap_pass_brigade(r->output_filters, bb);
3657 if (status == APR_SUCCESS
3658 || r->status != HTTP_OK
3663 /* no way to know what type of error occurred */
3664 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r,
3665 "default_handler: ap_pass_brigade returned %i",
3667 return HTTP_INTERNAL_SERVER_ERROR;
3670 else { /* unusual method (not GET or POST) */
3671 if (r->method_number == M_INVALID) {
3672 /* See if this looks like an undecrypted SSL handshake attempt.
3673 * It's safe to look a couple bytes into the_request if it exists, as it's
3674 * always allocated at least MIN_LINE_ALLOC (80) bytes.
3677 && r->the_request[0] == 0x16
3678 && (r->the_request[1] == 0x2 || r->the_request[1] == 0x3)) {
3679 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3680 "Invalid method in request %s - possible attempt to establish SSL connection on non-SSL port", r->the_request);
3682 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3683 "Invalid method in request %s", r->the_request);
3685 return HTTP_NOT_IMPLEMENTED;
3688 if (r->method_number == M_OPTIONS) {
3689 return ap_send_http_options(r);
3691 return HTTP_METHOD_NOT_ALLOWED;
3695 /* Optional function coming from mod_logio, used for logging of output
3698 APR_OPTIONAL_FN_TYPE(ap_logio_add_bytes_out) *ap__logio_add_bytes_out;
3699 APR_OPTIONAL_FN_TYPE(authz_some_auth_required) *ap__authz_ap_some_auth_required;
3701 /* Insist that at least one module will undertake to provide system
3702 * security by dropping startup privileges.
3704 static int sys_privileges = 0;
3705 AP_DECLARE(int) ap_sys_privileges_handlers(int inc)
3707 sys_privileges += inc;
3708 return sys_privileges;
3710 static int core_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
3712 ap__logio_add_bytes_out = APR_RETRIEVE_OPTIONAL_FN(ap_logio_add_bytes_out);
3713 ident_lookup = APR_RETRIEVE_OPTIONAL_FN(ap_ident_lookup);
3714 ap__authz_ap_some_auth_required = APR_RETRIEVE_OPTIONAL_FN(authz_some_auth_required);
3715 authn_ap_auth_type = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_type);
3716 authn_ap_auth_name = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_name);
3717 access_compat_ap_satisfies = APR_RETRIEVE_OPTIONAL_FN(access_compat_ap_satisfies);
3720 ap_setup_make_content_type(pconf);
3721 ap_setup_auth_internal(ptemp);
3722 if (!sys_privileges) {
3723 ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL,
3724 "Server MUST relinquish startup privileges before "
3725 "accepting connections. Please ensure mod_unixd "
3726 "or other system security module is loaded.");
3732 static void core_insert_filter(request_rec *r)
3734 core_dir_config *conf = (core_dir_config *)
3735 ap_get_module_config(r->per_dir_config,
3737 const char *filter, *filters = conf->output_filters;
3740 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3741 ap_add_output_filter(filter, NULL, r, r->connection);
3745 filters = conf->input_filters;
3747 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3748 ap_add_input_filter(filter, NULL, r, r->connection);
3753 static apr_size_t num_request_notes = AP_NUM_STD_NOTES;
3755 static apr_status_t reset_request_notes(void *dummy)
3757 num_request_notes = AP_NUM_STD_NOTES;
3761 AP_DECLARE(apr_size_t) ap_register_request_note(void)
3763 apr_pool_cleanup_register(apr_hook_global_pool, NULL, reset_request_notes,
3764 apr_pool_cleanup_null);
3765 return num_request_notes++;
3768 AP_DECLARE(void **) ap_get_request_note(request_rec *r, apr_size_t note_num)
3770 core_request_config *req_cfg;
3772 if (note_num >= num_request_notes) {
3776 req_cfg = (core_request_config *)
3777 ap_get_module_config(r->request_config, &core_module);
3783 return &(req_cfg->notes[note_num]);
3786 static int core_create_req(request_rec *r)
3788 /* Alloc the config struct and the array of request notes in
3789 * a single block for efficiency
3791 core_request_config *req_cfg;
3793 req_cfg = apr_pcalloc(r->pool, sizeof(core_request_config) +
3794 sizeof(void *) * num_request_notes);
3795 req_cfg->notes = (void **)((char *)req_cfg + sizeof(core_request_config));
3797 /* ### temporarily enable script delivery as the default */
3798 req_cfg->deliver_script = 1;
3801 core_request_config *main_req_cfg = (core_request_config *)
3802 ap_get_module_config(r->main->request_config, &core_module);
3803 req_cfg->bb = main_req_cfg->bb;
3806 req_cfg->bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
3809 ap_set_module_config(r->request_config, &core_module, req_cfg);
3814 static int core_create_proxy_req(request_rec *r, request_rec *pr)
3816 return core_create_req(pr);
3819 static conn_rec *core_create_conn(apr_pool_t *ptrans, server_rec *server,
3820 apr_socket_t *csd, long id, void *sbh,
3821 apr_bucket_alloc_t *alloc)
3824 conn_rec *c = (conn_rec *) apr_pcalloc(ptrans, sizeof(conn_rec));
3827 (void)ap_update_child_status(c->sbh, SERVER_BUSY_READ, (request_rec *)NULL);
3829 /* Got a connection structure, so initialize what fields we can
3830 * (the rest are zeroed out by pcalloc).
3832 c->conn_config = ap_create_conn_config(ptrans);
3833 c->notes = apr_table_make(ptrans, 5);
3836 if ((rv = apr_socket_addr_get(&c->local_addr, APR_LOCAL, csd))
3838 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3839 "apr_socket_addr_get(APR_LOCAL)");
3840 apr_socket_close(csd);
3844 apr_sockaddr_ip_get(&c->local_ip, c->local_addr);
3845 if ((rv = apr_socket_addr_get(&c->remote_addr, APR_REMOTE, csd))
3847 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3848 "apr_socket_addr_get(APR_REMOTE)");
3849 apr_socket_close(csd);
3853 apr_sockaddr_ip_get(&c->remote_ip, c->remote_addr);
3854 c->base_server = server;
3857 c->bucket_alloc = alloc;
3859 c->cs = (conn_state_t *)apr_pcalloc(ptrans, sizeof(conn_state_t));
3860 APR_RING_INIT(&(c->cs->timeout_list), conn_state_t, timeout_list);
3861 c->cs->expiration_time = 0;
3862 c->cs->state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
3865 c->cs->bucket_alloc = alloc;
3866 c->clogging_input_filters = 0;
3871 static int core_pre_connection(conn_rec *c, void *csd)
3873 core_net_rec *net = apr_palloc(c->pool, sizeof(*net));
3876 /* The Nagle algorithm says that we should delay sending partial
3877 * packets in hopes of getting more data. We don't want to do
3878 * this; we are not telnet. There are bad interactions between
3879 * persistent connections and Nagle's algorithm that have very severe
3880 * performance penalties. (Failing to disable Nagle is not much of a
3881 * problem with simple HTTP.)
3883 rv = apr_socket_opt_set(csd, APR_TCP_NODELAY, 1);
3884 if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
3885 /* expected cause is that the client disconnected already,
3886 * hence the debug level
3888 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c,
3889 "apr_socket_opt_set(APR_TCP_NODELAY)");
3892 /* The core filter requires the timeout mode to be set, which
3893 * incidentally sets the socket to be nonblocking. If this
3894 * is not initialized correctly, Linux - for example - will
3895 * be initially blocking, while Solaris will be non blocking
3896 * and any initial read will fail.
3898 rv = apr_socket_timeout_set(csd, c->base_server->timeout);
3899 if (rv != APR_SUCCESS) {
3900 /* expected cause is that the client disconnected already */
3901 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c,
3902 "apr_socket_timeout_set");
3907 net->out_ctx = NULL;
3908 net->client_socket = csd;
3910 ap_set_module_config(net->c->conn_config, &core_module, csd);
3911 ap_add_input_filter_handle(ap_core_input_filter_handle, net, NULL, net->c);
3912 ap_add_output_filter_handle(ap_core_output_filter_handle, net, NULL, net->c);
3916 static void register_hooks(apr_pool_t *p)
3918 /* create_connection and install_transport_filters are
3919 * hooks that should always be APR_HOOK_REALLY_LAST to give other
3920 * modules the opportunity to install alternate network transports
3921 * and stop other functions from being run.
3923 ap_hook_create_connection(core_create_conn, NULL, NULL,
3924 APR_HOOK_REALLY_LAST);
3925 ap_hook_pre_connection(core_pre_connection, NULL, NULL,
3926 APR_HOOK_REALLY_LAST);
3928 ap_hook_post_config(core_post_config,NULL,NULL,APR_HOOK_REALLY_FIRST);
3929 ap_hook_translate_name(ap_core_translate,NULL,NULL,APR_HOOK_REALLY_LAST);
3930 ap_hook_map_to_storage(core_map_to_storage,NULL,NULL,APR_HOOK_REALLY_LAST);
3931 ap_hook_open_logs(ap_open_logs,NULL,NULL,APR_HOOK_REALLY_FIRST);
3932 ap_hook_child_init(ap_logs_child_init,NULL,NULL,APR_HOOK_MIDDLE);
3933 ap_hook_handler(default_handler,NULL,NULL,APR_HOOK_REALLY_LAST);
3934 /* FIXME: I suspect we can eliminate the need for these do_nothings - Ben */
3935 ap_hook_type_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
3936 ap_hook_fixups(core_override_type,NULL,NULL,APR_HOOK_REALLY_FIRST);
3937 ap_hook_create_request(core_create_req, NULL, NULL, APR_HOOK_MIDDLE);
3938 APR_OPTIONAL_HOOK(proxy, create_req, core_create_proxy_req, NULL, NULL,
3940 ap_hook_pre_mpm(ap_create_scoreboard, NULL, NULL, APR_HOOK_MIDDLE);
3942 /* register the core's insert_filter hook and register core-provided
3945 ap_hook_insert_filter(core_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
3947 ap_core_input_filter_handle =
3948 ap_register_input_filter("CORE_IN", ap_core_input_filter,
3949 NULL, AP_FTYPE_NETWORK);
3950 ap_content_length_filter_handle =
3951 ap_register_output_filter("CONTENT_LENGTH", ap_content_length_filter,
3952 NULL, AP_FTYPE_PROTOCOL);
3953 ap_core_output_filter_handle =
3954 ap_register_output_filter("CORE", ap_core_output_filter,
3955 NULL, AP_FTYPE_NETWORK);
3956 ap_subreq_core_filter_handle =
3957 ap_register_output_filter("SUBREQ_CORE", ap_sub_req_output_filter,
3958 NULL, AP_FTYPE_CONTENT_SET);
3960 ap_register_output_filter("OLD_WRITE", ap_old_write_filter,
3961 NULL, AP_FTYPE_RESOURCE - 10);
3964 AP_DECLARE_DATA module core_module = {
3966 AP_PLATFORM_REWRITE_ARGS_HOOK, /* hook to run before apache parses args */
3967 create_core_dir_config, /* create per-directory config structure */
3968 merge_core_dir_configs, /* merge per-directory config structures */
3969 create_core_server_config, /* create per-server config structure */
3970 merge_core_server_configs, /* merge per-server config structures */
3971 core_cmds, /* command apr_table_t */
3972 register_hooks /* register hooks */