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
31 #include "ap_config.h"
33 #include "http_config.h"
34 #include "http_core.h"
35 #include "http_protocol.h" /* For index_of_response(). Grump. */
36 #include "http_request.h"
37 #include "http_vhost.h"
38 #include "http_main.h" /* For the default_handler below... */
41 #include "http_connection.h"
42 #include "apr_buckets.h"
43 #include "util_filter.h"
44 #include "util_ebcdic.h"
45 #include "util_mutex.h"
47 #include "mpm_common.h"
48 #include "scoreboard.h"
50 #include "mod_proxy.h"
51 #include "ap_listen.h"
53 #include "mod_so.h" /* for ap_find_loaded_module_symbol */
55 /* LimitRequestBody handling */
56 #define AP_LIMIT_REQ_BODY_UNSET ((apr_off_t) -1)
57 #define AP_DEFAULT_LIMIT_REQ_BODY ((apr_off_t) 0)
59 /* LimitXMLRequestBody handling */
60 #define AP_LIMIT_UNSET ((long) -1)
61 #define AP_DEFAULT_LIMIT_XML_BODY ((size_t)1000000)
63 #define AP_MIN_SENDFILE_BYTES (256)
65 /* maximum include nesting level */
66 #ifndef AP_MAX_INCLUDE_DEPTH
67 #define AP_MAX_INCLUDE_DEPTH (128)
71 APR_HOOK_LINK(get_mgmt_items)
74 AP_IMPLEMENT_HOOK_RUN_ALL(int, get_mgmt_items,
75 (apr_pool_t *p, const char *val, apr_hash_t *ht),
76 (p, val, ht), OK, DECLINED)
78 /* Server core module... This module provides support for really basic
79 * server operations, including options and commands which control the
80 * operation of other modules. Consider this the bureaucracy module.
82 * The core module also defines handlers, etc., do handle just enough
83 * to allow a server with the core module ONLY to actually serve documents
84 * (though it slaps DefaultType on all of 'em); this was useful in testing,
85 * but may not be worth preserving.
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_INCNOEXEC | OPT_SYM_OWNER
114 conf->content_md5 = 2;
115 conf->accept_path_info = 3;
117 conf->use_canonical_name = USE_CANONICAL_NAME_UNSET;
118 conf->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_UNSET;
120 conf->hostname_lookups = HOSTNAME_LOOKUP_UNSET;
123 conf->limit_cpu = NULL;
125 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
126 conf->limit_mem = NULL;
129 conf->limit_nproc = NULL;
132 conf->limit_req_body = AP_LIMIT_REQ_BODY_UNSET;
133 conf->limit_xml_body = AP_LIMIT_UNSET;
134 conf->sec_file = apr_array_make(a, 2, sizeof(ap_conf_vector_t *));
136 conf->server_signature = srv_sig_unset;
138 conf->add_default_charset = ADD_DEFAULT_CHARSET_UNSET;
139 conf->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
141 /* Overriding all negotiation
143 conf->mime_type = NULL;
144 conf->handler = NULL;
145 conf->output_filters = NULL;
146 conf->input_filters = NULL;
149 * Flag for use of inodes in ETags.
151 conf->etag_bits = ETAG_UNSET;
152 conf->etag_add = ETAG_UNSET;
153 conf->etag_remove = ETAG_UNSET;
155 conf->enable_mmap = ENABLE_MMAP_UNSET;
156 conf->enable_sendfile = ENABLE_SENDFILE_UNSET;
157 conf->allow_encoded_slashes = 0;
163 * Overlay one hash table of ct_output_filters onto another
165 static void *merge_ct_filters(apr_pool_t *p,
168 const void *overlay_val,
169 const void *base_val,
172 ap_filter_rec_t *cur;
173 const ap_filter_rec_t *overlay_info = (const ap_filter_rec_t *)overlay_val;
174 const ap_filter_rec_t *base_info = (const ap_filter_rec_t *)base_val;
178 while (overlay_info) {
179 ap_filter_rec_t *new;
181 new = apr_pcalloc(p, sizeof(ap_filter_rec_t));
182 new->name = apr_pstrdup(p, overlay_info->name);
185 overlay_info = overlay_info->next;
192 /* We can't have dups. */
195 if (!strcasecmp(base_info->name, f->name)) {
204 f = apr_pcalloc(p, sizeof(ap_filter_rec_t));
205 f->name = apr_pstrdup(p, base_info->name);
210 base_info = base_info->next;
216 static void *merge_core_dir_configs(apr_pool_t *a, void *basev, void *newv)
218 core_dir_config *base = (core_dir_config *)basev;
219 core_dir_config *new = (core_dir_config *)newv;
220 core_dir_config *conf;
223 /* Create this conf by duplicating the base, replacing elements
224 * (or creating copies for merging) where new-> values exist.
226 conf = (core_dir_config *)apr_pmemdup(a, base, sizeof(core_dir_config));
229 conf->d_is_fnmatch = new->d_is_fnmatch;
230 conf->d_components = new->d_components;
232 conf->condition = new->condition;
234 if (new->opts & OPT_UNSET) {
235 /* there was no explicit setting of new->opts, so we merge
236 * preserve the invariant (opts_add & opts_remove) == 0
238 conf->opts_add = (conf->opts_add & ~new->opts_remove) | new->opts_add;
239 conf->opts_remove = (conf->opts_remove & ~new->opts_add)
241 conf->opts = (conf->opts & ~conf->opts_remove) | conf->opts_add;
242 if ((base->opts & OPT_INCNOEXEC) && (new->opts & OPT_INCLUDES)) {
243 conf->opts = (conf->opts & ~OPT_INCNOEXEC) | OPT_INCLUDES;
247 /* otherwise we just copy, because an explicit opts setting
248 * overrides all earlier +/- modifiers
250 conf->opts = new->opts;
251 conf->opts_add = new->opts_add;
252 conf->opts_remove = new->opts_remove;
255 if (!(new->override & OR_UNSET)) {
256 conf->override = new->override;
259 if (!(new->override_opts & OPT_UNSET)) {
260 conf->override_opts = new->override_opts;
263 if (new->ap_default_type) {
264 conf->ap_default_type = new->ap_default_type;
267 if (conf->response_code_strings == NULL) {
268 conf->response_code_strings = new->response_code_strings;
270 else if (new->response_code_strings != NULL) {
271 /* If we merge, the merge-result must have it's own array
273 conf->response_code_strings = apr_pmemdup(a,
274 base->response_code_strings,
275 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
277 for (i = 0; i < RESPONSE_CODES; ++i) {
278 if (new->response_code_strings[i] != NULL) {
279 conf->response_code_strings[i] = new->response_code_strings[i];
283 /* Otherwise we simply use the base->response_code_strings array
286 if (new->hostname_lookups != HOSTNAME_LOOKUP_UNSET) {
287 conf->hostname_lookups = new->hostname_lookups;
290 if ((new->content_md5 & 2) == 0) {
291 conf->content_md5 = new->content_md5;
294 if (new->accept_path_info != 3) {
295 conf->accept_path_info = new->accept_path_info;
298 if (new->use_canonical_name != USE_CANONICAL_NAME_UNSET) {
299 conf->use_canonical_name = new->use_canonical_name;
302 if (new->use_canonical_phys_port != USE_CANONICAL_PHYS_PORT_UNSET) {
303 conf->use_canonical_phys_port = new->use_canonical_phys_port;
307 if (new->limit_cpu) {
308 conf->limit_cpu = new->limit_cpu;
312 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
313 if (new->limit_mem) {
314 conf->limit_mem = new->limit_mem;
319 if (new->limit_nproc) {
320 conf->limit_nproc = new->limit_nproc;
324 if (new->limit_req_body != AP_LIMIT_REQ_BODY_UNSET) {
325 conf->limit_req_body = new->limit_req_body;
328 if (new->limit_xml_body != AP_LIMIT_UNSET)
329 conf->limit_xml_body = new->limit_xml_body;
331 conf->limit_xml_body = base->limit_xml_body;
333 if (!conf->sec_file) {
334 conf->sec_file = new->sec_file;
336 else if (new->sec_file) {
337 /* If we merge, the merge-result must have it's own array
339 conf->sec_file = apr_array_append(a, base->sec_file, new->sec_file);
341 /* Otherwise we simply use the base->sec_file array
344 if (new->server_signature != srv_sig_unset) {
345 conf->server_signature = new->server_signature;
348 if (new->add_default_charset != ADD_DEFAULT_CHARSET_UNSET) {
349 conf->add_default_charset = new->add_default_charset;
350 conf->add_default_charset_name = new->add_default_charset_name;
353 /* Overriding all negotiation
355 if (new->mime_type) {
356 conf->mime_type = new->mime_type;
360 conf->handler = new->handler;
363 if (new->output_filters) {
364 conf->output_filters = new->output_filters;
367 if (new->input_filters) {
368 conf->input_filters = new->input_filters;
371 if (conf->ct_output_filters && new->ct_output_filters) {
372 conf->ct_output_filters = apr_hash_merge(a,
373 new->ct_output_filters,
374 conf->ct_output_filters,
378 else if (new->ct_output_filters) {
379 conf->ct_output_filters = apr_hash_copy(a, new->ct_output_filters);
381 else if (conf->ct_output_filters) {
382 /* That memcpy above isn't enough. */
383 conf->ct_output_filters = apr_hash_copy(a, base->ct_output_filters);
387 * Now merge the setting of the FileETag directive.
389 if (new->etag_bits == ETAG_UNSET) {
391 (conf->etag_add & (~ new->etag_remove)) | new->etag_add;
393 (conf->etag_remove & (~ new->etag_add)) | new->etag_remove;
395 (conf->etag_bits & (~ conf->etag_remove)) | conf->etag_add;
398 conf->etag_bits = new->etag_bits;
399 conf->etag_add = new->etag_add;
400 conf->etag_remove = new->etag_remove;
403 if (conf->etag_bits != ETAG_NONE) {
404 conf->etag_bits &= (~ ETAG_NONE);
407 if (new->enable_mmap != ENABLE_MMAP_UNSET) {
408 conf->enable_mmap = new->enable_mmap;
411 if (new->enable_sendfile != ENABLE_SENDFILE_UNSET) {
412 conf->enable_sendfile = new->enable_sendfile;
415 conf->allow_encoded_slashes = new->allow_encoded_slashes;
420 static void *create_core_server_config(apr_pool_t *a, server_rec *s)
422 core_server_config *conf;
423 int is_virtual = s->is_virtual;
425 conf = (core_server_config *)apr_pcalloc(a, sizeof(core_server_config));
428 conf->gprof_dir = NULL;
431 conf->access_name = is_virtual ? NULL : DEFAULT_ACCESS_FNAME;
432 conf->ap_document_root = is_virtual ? NULL : DOCUMENT_LOCATION;
433 conf->sec_dir = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
434 conf->sec_url = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
436 /* recursion stopper */
437 conf->redirect_limit = 0; /* 0 == unset */
438 conf->subreq_limit = 0;
440 conf->protocol = NULL;
441 conf->accf_map = apr_table_make(a, 5);
443 #ifdef APR_TCP_DEFER_ACCEPT
444 apr_table_set(conf->accf_map, "http", "data");
445 apr_table_set(conf->accf_map, "https", "data");
448 #if APR_HAS_SO_ACCEPTFILTER
449 #ifndef ACCEPT_FILTER_NAME
450 #define ACCEPT_FILTER_NAME "httpready"
451 #ifdef __FreeBSD_version
452 #if __FreeBSD_version < 411000 /* httpready broken before 4.1.1 */
453 #undef ACCEPT_FILTER_NAME
454 #define ACCEPT_FILTER_NAME "dataready"
458 apr_table_set(conf->accf_map, "http", ACCEPT_FILTER_NAME);
459 apr_table_set(conf->accf_map, "https", "dataready");
462 conf->trace_enable = AP_TRACE_UNSET;
467 static void *merge_core_server_configs(apr_pool_t *p, void *basev, void *virtv)
469 core_server_config *base = (core_server_config *)basev;
470 core_server_config *virt = (core_server_config *)virtv;
471 core_server_config *conf;
473 conf = (core_server_config *)apr_pmemdup(p, virt, sizeof(core_server_config));
475 if (!conf->access_name) {
476 conf->access_name = base->access_name;
479 if (!conf->ap_document_root) {
480 conf->ap_document_root = base->ap_document_root;
483 if (!conf->protocol) {
484 conf->protocol = base->protocol;
487 conf->sec_dir = apr_array_append(p, base->sec_dir, virt->sec_dir);
488 conf->sec_url = apr_array_append(p, base->sec_url, virt->sec_url);
490 conf->redirect_limit = virt->redirect_limit
491 ? virt->redirect_limit
492 : base->redirect_limit;
494 conf->subreq_limit = virt->subreq_limit
496 : base->subreq_limit;
498 conf->trace_enable = (virt->trace_enable != AP_TRACE_UNSET)
500 : base->trace_enable;
505 /* Add per-directory configuration entry (for <directory> section);
506 * these are part of the core server config.
509 AP_CORE_DECLARE(void) ap_add_per_dir_conf(server_rec *s, void *dir_config)
511 core_server_config *sconf = ap_get_module_config(s->module_config,
513 void **new_space = (void **)apr_array_push(sconf->sec_dir);
515 *new_space = dir_config;
518 AP_CORE_DECLARE(void) ap_add_per_url_conf(server_rec *s, void *url_config)
520 core_server_config *sconf = ap_get_module_config(s->module_config,
522 void **new_space = (void **)apr_array_push(sconf->sec_url);
524 *new_space = url_config;
527 AP_CORE_DECLARE(void) ap_add_file_conf(core_dir_config *conf, void *url_config)
529 void **new_space = (void **)apr_array_push(conf->sec_file);
531 *new_space = url_config;
534 /* We need to do a stable sort, qsort isn't stable. So to make it stable
535 * we'll be maintaining the original index into the list, and using it
536 * as the minor key during sorting. The major key is the number of
537 * components (where the root component is zero).
539 struct reorder_sort_rec {
540 ap_conf_vector_t *elt;
544 static int reorder_sorter(const void *va, const void *vb)
546 const struct reorder_sort_rec *a = va;
547 const struct reorder_sort_rec *b = vb;
548 core_dir_config *core_a;
549 core_dir_config *core_b;
551 core_a = ap_get_module_config(a->elt, &core_module);
552 core_b = ap_get_module_config(b->elt, &core_module);
554 /* a regex always sorts after a non-regex
556 if (!core_a->r && core_b->r) {
559 else if (core_a->r && !core_b->r) {
563 /* we always sort next by the number of components
565 if (core_a->d_components < core_b->d_components) {
568 else if (core_a->d_components > core_b->d_components) {
572 /* They have the same number of components, we now have to compare
573 * the minor key to maintain the original order (from the config.)
575 return a->orig_index - b->orig_index;
578 void ap_core_reorder_directories(apr_pool_t *p, server_rec *s)
580 core_server_config *sconf;
581 apr_array_header_t *sec_dir;
582 struct reorder_sort_rec *sortbin;
584 ap_conf_vector_t **elts;
588 sconf = ap_get_module_config(s->module_config, &core_module);
589 sec_dir = sconf->sec_dir;
590 nelts = sec_dir->nelts;
591 elts = (ap_conf_vector_t **)sec_dir->elts;
594 /* simple case of already being sorted... */
595 /* We're not checking this condition to be fast... we're checking
596 * it to avoid trying to palloc zero bytes, which can trigger some
597 * memory debuggers to barf
602 /* we have to allocate tmp space to do a stable sort */
603 apr_pool_create(&tmp, p);
604 sortbin = apr_palloc(tmp, sec_dir->nelts * sizeof(*sortbin));
605 for (i = 0; i < nelts; ++i) {
606 sortbin[i].orig_index = i;
607 sortbin[i].elt = elts[i];
610 qsort(sortbin, nelts, sizeof(*sortbin), reorder_sorter);
612 /* and now copy back to the original array */
613 for (i = 0; i < nelts; ++i) {
614 elts[i] = sortbin[i].elt;
617 apr_pool_destroy(tmp);
620 /*****************************************************************
622 * There are some elements of the core config structures in which
623 * other modules have a legitimate interest (this is ugly, but necessary
624 * to preserve NCSA back-compatibility). So, we have a bunch of accessors
628 AP_DECLARE(int) ap_allow_options(request_rec *r)
630 core_dir_config *conf =
631 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
636 AP_DECLARE(int) ap_allow_overrides(request_rec *r)
638 core_dir_config *conf;
639 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
642 return conf->override;
646 * Optional function coming from mod_authn_core, used for
647 * retrieving the type of autorization
649 static APR_OPTIONAL_FN_TYPE(authn_ap_auth_type) *authn_ap_auth_type;
651 AP_DECLARE(const char *) ap_auth_type(request_rec *r)
653 if (authn_ap_auth_type) {
654 return authn_ap_auth_type(r);
660 * Optional function coming from mod_authn_core, used for
661 * retrieving the authorization realm
663 static APR_OPTIONAL_FN_TYPE(authn_ap_auth_name) *authn_ap_auth_name;
665 AP_DECLARE(const char *) ap_auth_name(request_rec *r)
667 if (authn_ap_auth_name) {
668 return authn_ap_auth_name(r);
674 * Optional function coming from mod_access_compat, used to determine how
675 access control interacts with authentication/authorization
677 static APR_OPTIONAL_FN_TYPE(access_compat_ap_satisfies) *access_compat_ap_satisfies;
679 AP_DECLARE(int) ap_satisfies(request_rec *r)
681 if (access_compat_ap_satisfies) {
682 return access_compat_ap_satisfies(r);
684 return SATISFY_NOSPEC;
687 AP_DECLARE(const char *) ap_default_type(request_rec *r)
689 core_dir_config *conf;
691 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
694 return conf->ap_default_type
695 ? conf->ap_default_type
696 : DEFAULT_CONTENT_TYPE;
699 AP_DECLARE(const char *) ap_document_root(request_rec *r) /* Don't use this! */
701 core_server_config *conf;
703 conf = (core_server_config *)ap_get_module_config(r->server->module_config,
706 return conf->ap_document_root;
709 /* Should probably just get rid of this... the only code that cares is
710 * part of the core anyway (and in fact, it isn't publicised to other
714 char *ap_response_code_string(request_rec *r, int error_index)
716 core_dir_config *dirconf;
717 core_request_config *reqconf;
719 /* check for string registered via ap_custom_response() first */
720 reqconf = (core_request_config *)ap_get_module_config(r->request_config,
722 if (reqconf->response_code_strings != NULL &&
723 reqconf->response_code_strings[error_index] != NULL) {
724 return reqconf->response_code_strings[error_index];
727 /* check for string specified via ErrorDocument */
728 dirconf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
731 if (dirconf->response_code_strings == NULL) {
735 if (dirconf->response_code_strings[error_index] == &errordocument_default) {
739 return dirconf->response_code_strings[error_index];
743 /* Code from Harald Hanche-Olsen <hanche@imf.unit.no> */
744 static APR_INLINE void do_double_reverse (conn_rec *conn)
749 if (conn->double_reverse) {
754 if (conn->remote_host == NULL || conn->remote_host[0] == '\0') {
755 /* single reverse failed, so don't bother */
756 conn->double_reverse = -1;
760 rv = apr_sockaddr_info_get(&sa, conn->remote_host, APR_UNSPEC, 0, 0, conn->pool);
761 if (rv == APR_SUCCESS) {
763 if (apr_sockaddr_equal(sa, conn->remote_addr)) {
764 conn->double_reverse = 1;
772 conn->double_reverse = -1;
775 AP_DECLARE(const char *) ap_get_remote_host(conn_rec *conn, void *dir_config,
776 int type, int *str_is_ip)
778 int hostname_lookups;
779 int ignored_str_is_ip;
781 if (!str_is_ip) { /* caller doesn't want to know */
782 str_is_ip = &ignored_str_is_ip;
786 /* If we haven't checked the host name, and we want to */
789 ((core_dir_config *)ap_get_module_config(dir_config, &core_module))
792 if (hostname_lookups == HOSTNAME_LOOKUP_UNSET) {
793 hostname_lookups = HOSTNAME_LOOKUP_OFF;
798 hostname_lookups = HOSTNAME_LOOKUP_OFF;
801 if (type != REMOTE_NOLOOKUP
802 && conn->remote_host == NULL
803 && (type == REMOTE_DOUBLE_REV
804 || hostname_lookups != HOSTNAME_LOOKUP_OFF)) {
806 if (apr_getnameinfo(&conn->remote_host, conn->remote_addr, 0)
808 ap_str_tolower(conn->remote_host);
810 if (hostname_lookups == HOSTNAME_LOOKUP_DOUBLE) {
811 do_double_reverse(conn);
812 if (conn->double_reverse != 1) {
813 conn->remote_host = NULL;
818 /* if failed, set it to the NULL string to indicate error */
819 if (conn->remote_host == NULL) {
820 conn->remote_host = "";
824 if (type == REMOTE_DOUBLE_REV) {
825 do_double_reverse(conn);
826 if (conn->double_reverse == -1) {
832 * Return the desired information; either the remote DNS name, if found,
833 * or either NULL (if the hostname was requested) or the IP address
834 * (if any identifier was requested).
836 if (conn->remote_host != NULL && conn->remote_host[0] != '\0') {
837 return conn->remote_host;
840 if (type == REMOTE_HOST || type == REMOTE_DOUBLE_REV) {
845 return conn->remote_ip;
851 * Optional function coming from mod_ident, used for looking up ident user
853 static APR_OPTIONAL_FN_TYPE(ap_ident_lookup) *ident_lookup;
855 AP_DECLARE(const char *) ap_get_remote_logname(request_rec *r)
857 if (r->connection->remote_logname != NULL) {
858 return r->connection->remote_logname;
862 return ident_lookup(r);
868 /* There are two options regarding what the "name" of a server is. The
869 * "canonical" name as defined by ServerName and Port, or the "client's
870 * name" as supplied by a possible Host: header or full URI.
872 * The DNS option to UseCanonicalName causes this routine to do a
873 * reverse lookup on the local IP address of the connection and use
874 * that for the ServerName. This makes its value more reliable while
875 * at the same time allowing Demon's magic virtual hosting to work.
876 * The assumption is that DNS lookups are sufficiently quick...
879 AP_DECLARE(const char *) ap_get_server_name(request_rec *r)
881 conn_rec *conn = r->connection;
885 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
888 switch (d->use_canonical_name) {
889 case USE_CANONICAL_NAME_ON:
890 retval = r->server->server_hostname;
892 case USE_CANONICAL_NAME_DNS:
893 if (conn->local_host == NULL) {
894 if (apr_getnameinfo(&conn->local_host,
895 conn->local_addr, 0) != APR_SUCCESS)
896 conn->local_host = apr_pstrdup(conn->pool,
897 r->server->server_hostname);
899 ap_str_tolower(conn->local_host);
902 retval = conn->local_host;
904 case USE_CANONICAL_NAME_OFF:
905 case USE_CANONICAL_NAME_UNSET:
906 retval = r->hostname ? r->hostname : r->server->server_hostname;
909 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
910 "ap_get_server_name: Invalid UCN Option somehow");
911 retval = "localhost";
918 * Get the current server name from the request for the purposes
919 * of using in a URL. If the server name is an IPv6 literal
920 * address, it will be returned in URL format (e.g., "[fe80::1]").
922 static const char *get_server_name_for_url(request_rec *r)
924 const char *plain_server_name = ap_get_server_name(r);
927 if (ap_strchr_c(plain_server_name, ':')) { /* IPv6 literal? */
928 return apr_psprintf(r->pool, "[%s]", plain_server_name);
931 return plain_server_name;
934 AP_DECLARE(apr_port_t) ap_get_server_port(const request_rec *r)
938 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
940 switch (d->use_canonical_name) {
941 case USE_CANONICAL_NAME_OFF:
942 case USE_CANONICAL_NAME_DNS:
943 case USE_CANONICAL_NAME_UNSET:
944 if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
945 port = r->parsed_uri.port_str ? r->parsed_uri.port :
946 r->connection->local_addr->port ? r->connection->local_addr->port :
947 r->server->port ? r->server->port :
949 else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
950 port = r->parsed_uri.port_str ? r->parsed_uri.port :
951 r->server->port ? r->server->port :
954 case USE_CANONICAL_NAME_ON:
955 /* With UseCanonicalName on (and in all versions prior to 1.3)
956 * Apache will use the hostname and port specified in the
957 * ServerName directive to construct a canonical name for the
958 * server. (If no port was specified in the ServerName
959 * directive, Apache uses the port supplied by the client if
960 * any is supplied, and finally the default port for the protocol
963 if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
964 port = r->server->port ? r->server->port :
965 r->connection->local_addr->port ? r->connection->local_addr->port :
967 else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
968 port = r->server->port ? r->server->port :
972 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
973 "ap_get_server_port: Invalid UCN Option somehow");
974 port = ap_default_port(r);
981 AP_DECLARE(char *) ap_construct_url(apr_pool_t *p, const char *uri,
984 unsigned port = ap_get_server_port(r);
985 const char *host = get_server_name_for_url(r);
987 if (ap_is_default_port(port, r)) {
988 return apr_pstrcat(p, ap_http_scheme(r), "://", host, uri, NULL);
991 return apr_psprintf(p, "%s://%s:%u%s", ap_http_scheme(r), host, port, uri);
994 AP_DECLARE(apr_off_t) ap_get_limit_req_body(const request_rec *r)
997 (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
999 if (d->limit_req_body == AP_LIMIT_REQ_BODY_UNSET) {
1000 return AP_DEFAULT_LIMIT_REQ_BODY;
1003 return d->limit_req_body;
1007 /*****************************************************************
1009 * Commands... this module handles almost all of the NCSA httpd.conf
1010 * commands, but most of the old srm.conf is in the the modules.
1014 /* returns a parent if it matches the given directive */
1015 static const ap_directive_t * find_parent(const ap_directive_t *dirp,
1018 while (dirp->parent != NULL) {
1019 dirp = dirp->parent;
1021 /* ### it would be nice to have atom-ized directives */
1022 if (strcasecmp(dirp->directive, what) == 0)
1029 AP_DECLARE(const char *) ap_check_cmd_context(cmd_parms *cmd,
1032 const char *gt = (cmd->cmd->name[0] == '<'
1033 && cmd->cmd->name[strlen(cmd->cmd->name)-1] != '>')
1035 const ap_directive_t *found;
1037 if ((forbidden & NOT_IN_VIRTUALHOST) && cmd->server->is_virtual) {
1038 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1039 " cannot occur within <VirtualHost> section", NULL);
1042 if ((forbidden & NOT_IN_LIMIT) && cmd->limited != -1) {
1043 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1044 " cannot occur within <Limit> section", NULL);
1047 if ((forbidden & NOT_IN_DIR_LOC_FILE) == NOT_IN_DIR_LOC_FILE) {
1048 if (cmd->path != NULL) {
1049 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1050 " cannot occur within <Directory/Location/Files> "
1053 if (cmd->cmd->req_override & EXEC_ON_READ) {
1054 /* EXEC_ON_READ must be NOT_IN_DIR_LOC_FILE, if not, it will
1055 * (deliberately) segfault below in the individual tests...
1061 if (((forbidden & NOT_IN_DIRECTORY)
1062 && ((found = find_parent(cmd->directive, "<Directory"))
1063 || (found = find_parent(cmd->directive, "<DirectoryMatch"))))
1064 || ((forbidden & NOT_IN_LOCATION)
1065 && ((found = find_parent(cmd->directive, "<Location"))
1066 || (found = find_parent(cmd->directive, "<LocationMatch"))))
1067 || ((forbidden & NOT_IN_FILES)
1068 && ((found = find_parent(cmd->directive, "<Files"))
1069 || (found = find_parent(cmd->directive, "<FilesMatch"))))) {
1070 return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1071 " cannot occur within ", found->directive,
1078 static const char *set_access_name(cmd_parms *cmd, void *dummy,
1081 void *sconf = cmd->server->module_config;
1082 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1084 const char *err = ap_check_cmd_context(cmd,
1085 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1090 conf->access_name = apr_pstrdup(cmd->pool, arg);
1095 static const char *set_define(cmd_parms *cmd, void *dummy,
1100 const char *err = ap_check_cmd_context(cmd,
1106 newv = (char **)apr_array_push(ap_server_config_defines);
1107 *newv = apr_pstrdup(cmd->pool, optarg);
1113 static const char *set_gprof_dir(cmd_parms *cmd, void *dummy, const char *arg)
1115 void *sconf = cmd->server->module_config;
1116 core_server_config *conf = ap_get_module_config(sconf, &core_module);
1118 const char *err = ap_check_cmd_context(cmd,
1119 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1124 conf->gprof_dir = apr_pstrdup(cmd->pool, arg);
1129 static const char *set_add_default_charset(cmd_parms *cmd,
1130 void *d_, const char *arg)
1132 core_dir_config *d = d_;
1134 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
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,
1161 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1166 /* Make it absolute, relative to ServerRoot */
1167 arg = ap_server_root_relative(cmd->pool, arg);
1169 return "DocumentRoot must be a directory";
1172 /* TODO: ap_configtestonly && ap_docrootcheck && */
1173 if (apr_filepath_merge((char**)&conf->ap_document_root, NULL, arg,
1174 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS
1175 || !ap_is_directory(cmd->pool, arg)) {
1176 if (cmd->server->is_virtual) {
1177 ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0,
1179 "Warning: DocumentRoot [%s] does not exist",
1181 conf->ap_document_root = arg;
1184 return "DocumentRoot must be a directory";
1190 AP_DECLARE(void) ap_custom_response(request_rec *r, int status,
1193 core_request_config *conf =
1194 ap_get_module_config(r->request_config, &core_module);
1197 if (conf->response_code_strings == NULL) {
1198 conf->response_code_strings =
1199 apr_pcalloc(r->pool,
1200 sizeof(*conf->response_code_strings) * RESPONSE_CODES);
1203 idx = ap_index_of_response(status);
1205 conf->response_code_strings[idx] =
1206 ((ap_is_url(string) || (*string == '/')) && (*string != '"')) ?
1207 apr_pstrdup(r->pool, string) : apr_pstrcat(r->pool, "\"", string, NULL);
1210 static const char *set_error_document(cmd_parms *cmd, void *conf_,
1211 const char *errno_str, const char *msg)
1213 core_dir_config *conf = conf_;
1214 int error_number, index_number, idx500;
1215 enum { MSG, LOCAL_PATH, REMOTE_PATH } what = MSG;
1217 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
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")) {
1304 else if (!strcasecmp(w, "IncludesNOEXEC")) {
1305 opt = (OPT_INCLUDES | OPT_INCNOEXEC);
1307 else if (!strcasecmp(w, "FollowSymLinks")) {
1308 opt = OPT_SYM_LINKS;
1310 else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1311 opt = OPT_SYM_OWNER;
1313 else if (!strcasecmp(w, "ExecCGI")) {
1316 else if (!strcasecmp(w, "MultiViews")) {
1319 else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1320 opt = OPT_MULTI|OPT_EXECCGI;
1322 else if (!strcasecmp(w, "None")) {
1325 else if (!strcasecmp(w, "All")) {
1329 return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1335 (*opts) &= (~OPT_UNSET);
1340 static const char *set_override(cmd_parms *cmd, void *d_, const char *l)
1342 core_dir_config *d = d_;
1346 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1351 /* Throw a warning if we're in <Location> or <Files> */
1352 if (ap_check_cmd_context(cmd, NOT_IN_LOCATION | NOT_IN_FILES)) {
1353 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
1354 "Useless use of AllowOverride in line %d of %s.",
1355 cmd->directive->line_num, cmd->directive->filename);
1358 d->override = OR_NONE;
1360 w = ap_getword_conf(cmd->pool, &l);
1368 if (!strcasecmp(w, "Limit")) {
1369 d->override |= OR_LIMIT;
1371 else if (!strcasecmp(k, "Options")) {
1372 d->override |= OR_OPTIONS;
1374 set_allow_opts(cmd, &(d->override_opts), v);
1376 d->override_opts = OPT_ALL;
1378 else if (!strcasecmp(w, "FileInfo")) {
1379 d->override |= OR_FILEINFO;
1381 else if (!strcasecmp(w, "AuthConfig")) {
1382 d->override |= OR_AUTHCFG;
1384 else if (!strcasecmp(w, "Indexes")) {
1385 d->override |= OR_INDEXES;
1387 else if (!strcasecmp(w, "None")) {
1388 d->override = OR_NONE;
1390 else if (!strcasecmp(w, "All")) {
1391 d->override = OR_ALL;
1394 return apr_pstrcat(cmd->pool, "Illegal override option ", w, NULL);
1397 d->override &= ~OR_UNSET;
1403 static const char *set_options(cmd_parms *cmd, void *d_, const char *l)
1405 core_dir_config *d = d_;
1406 allow_options_t opt;
1411 char *w = ap_getword_conf(cmd->pool, &l);
1414 if (*w == '+' || *w == '-') {
1422 if (!strcasecmp(w, "Indexes")) {
1425 else if (!strcasecmp(w, "Includes")) {
1428 else if (!strcasecmp(w, "IncludesNOEXEC")) {
1429 opt = (OPT_INCLUDES | OPT_INCNOEXEC);
1431 else if (!strcasecmp(w, "FollowSymLinks")) {
1432 opt = OPT_SYM_LINKS;
1434 else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1435 opt = OPT_SYM_OWNER;
1437 else if (!strcasecmp(w, "ExecCGI")) {
1440 else if (!strcasecmp(w, "MultiViews")) {
1443 else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1444 opt = OPT_MULTI|OPT_EXECCGI;
1446 else if (!strcasecmp(w, "None")) {
1449 else if (!strcasecmp(w, "All")) {
1453 return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1456 if (!(cmd->override_opts & opt) && opt != OPT_NONE) {
1457 return apr_pstrcat(cmd->pool, "Option ", w, " not allowed here", NULL);
1459 else if (action == '-') {
1460 /* we ensure the invariant (d->opts_add & d->opts_remove) == 0 */
1461 d->opts_remove |= opt;
1462 d->opts_add &= ~opt;
1465 else if (action == '+') {
1467 d->opts_remove &= ~opt;
1479 * Note what data should be used when forming file ETag values.
1480 * It would be nicer to do this as an ITERATE, but then we couldn't
1481 * remember the +/- state properly.
1483 static const char *set_etag_bits(cmd_parms *cmd, void *mconfig,
1486 core_dir_config *cfg;
1487 etag_components_t bit;
1495 cfg = (core_dir_config *)mconfig;
1500 while (args[0] != '\0') {
1504 token = ap_getword_conf(cmd->pool, &args);
1505 if ((*token == '+') || (*token == '-')) {
1511 * The occurrence of an absolute setting wipes
1512 * out any previous relative ones. The first such
1513 * occurrence forgets any inherited ones, too.
1516 cfg->etag_bits = ETAG_UNSET;
1517 cfg->etag_add = ETAG_UNSET;
1518 cfg->etag_remove = ETAG_UNSET;
1523 if (strcasecmp(token, "None") == 0) {
1524 if (action != '*') {
1528 cfg->etag_bits = bit = ETAG_NONE;
1532 else if (strcasecmp(token, "All") == 0) {
1533 if (action != '*') {
1538 cfg->etag_bits = bit = ETAG_ALL;
1541 else if (strcasecmp(token, "Size") == 0) {
1544 else if ((strcasecmp(token, "LMTime") == 0)
1545 || (strcasecmp(token, "MTime") == 0)
1546 || (strcasecmp(token, "LastModified") == 0)) {
1549 else if (strcasecmp(token, "INode") == 0) {
1553 return apr_pstrcat(cmd->pool, "Unknown keyword '",
1554 token, "' for ", cmd->cmd->name,
1555 " directive", NULL);
1559 return apr_pstrcat(cmd->pool, cmd->cmd->name, " keyword '",
1560 token, "' cannot be used with '+' or '-'",
1564 if (action == '+') {
1566 * Make sure it's in the 'add' list and absent from the
1569 cfg->etag_add |= bit;
1570 cfg->etag_remove &= (~ bit);
1572 else if (action == '-') {
1573 cfg->etag_remove |= bit;
1574 cfg->etag_add &= (~ bit);
1578 * Non-relative values wipe out any + or - values
1579 * accumulated so far.
1581 cfg->etag_bits |= bit;
1582 cfg->etag_add = ETAG_UNSET;
1583 cfg->etag_remove = ETAG_UNSET;
1589 * Any setting at all will clear the 'None' and 'Unset' bits.
1592 if (cfg->etag_add != ETAG_UNSET) {
1593 cfg->etag_add &= (~ ETAG_UNSET);
1596 if (cfg->etag_remove != ETAG_UNSET) {
1597 cfg->etag_remove &= (~ ETAG_UNSET);
1601 cfg->etag_bits &= (~ ETAG_UNSET);
1603 if ((cfg->etag_bits & ETAG_NONE) != ETAG_NONE) {
1604 cfg->etag_bits &= (~ ETAG_NONE);
1611 static const char *set_enable_mmap(cmd_parms *cmd, void *d_,
1614 core_dir_config *d = d_;
1615 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1621 if (strcasecmp(arg, "on") == 0) {
1622 d->enable_mmap = ENABLE_MMAP_ON;
1624 else if (strcasecmp(arg, "off") == 0) {
1625 d->enable_mmap = ENABLE_MMAP_OFF;
1628 return "parameter must be 'on' or 'off'";
1634 static const char *set_enable_sendfile(cmd_parms *cmd, void *d_,
1637 core_dir_config *d = d_;
1638 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1644 if (strcasecmp(arg, "on") == 0) {
1645 d->enable_sendfile = ENABLE_SENDFILE_ON;
1647 else if (strcasecmp(arg, "off") == 0) {
1648 d->enable_sendfile = ENABLE_SENDFILE_OFF;
1651 return "parameter must be 'on' or 'off'";
1659 * Report a missing-'>' syntax error.
1661 static char *unclosed_directive(cmd_parms *cmd)
1663 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1664 "> directive missing closing '>'", NULL);
1668 * Report a missing args in '<Foo >' syntax error.
1670 static char *missing_container_arg(cmd_parms *cmd)
1672 return apr_pstrcat(cmd->pool, cmd->cmd->name,
1673 "> directive requires additional arguments", NULL);
1676 AP_CORE_DECLARE_NONSTD(const char *) ap_limit_section(cmd_parms *cmd,
1680 const char *endp = ap_strrchr_c(arg, '>');
1681 const char *limited_methods;
1682 void *tog = cmd->cmd->cmd_data;
1683 apr_int64_t limited = 0;
1686 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1692 return unclosed_directive(cmd);
1695 limited_methods = apr_pstrndup(cmd->pool, arg, endp - arg);
1697 if (!limited_methods[0]) {
1698 return missing_container_arg(cmd);
1701 while (limited_methods[0]) {
1702 char *method = ap_getword_conf(cmd->pool, &limited_methods);
1705 /* check for builtin or module registered method number */
1706 methnum = ap_method_number_of(method);
1708 if (methnum == M_TRACE && !tog) {
1709 return "TRACE cannot be controlled by <Limit>, see TraceEnable";
1711 else if (methnum == M_INVALID) {
1712 /* method has not been registered yet, but resorce restriction
1713 * is always checked before method handling, so register it.
1715 methnum = ap_method_register(cmd->pool, method);
1718 limited |= (AP_METHOD_BIT << methnum);
1721 /* Killing two features with one function,
1722 * if (tog == NULL) <Limit>, else <LimitExcept>
1724 cmd->limited = tog ? ~limited : limited;
1726 errmsg = ap_walk_config(cmd->directive->first_child, cmd, cmd->context);
1733 /* XXX: Bogus - need to do this differently (at least OS2/Netware suffer
1734 * the same problem!!!
1735 * We use this in <DirectoryMatch> and <FilesMatch>, to ensure that
1736 * people don't get bitten by wrong-cased regex matches
1740 #define USE_ICASE AP_REG_ICASE
1745 static const char *dirsection(cmd_parms *cmd, void *mconfig, const char *arg)
1748 const char *endp = ap_strrchr_c(arg, '>');
1749 int old_overrides = cmd->override;
1750 char *old_path = cmd->path;
1751 core_dir_config *conf;
1752 ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
1753 ap_regex_t *r = NULL;
1754 const command_rec *thiscmd = cmd->cmd;
1756 const char *err = ap_check_cmd_context(cmd,
1757 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1763 return unclosed_directive(cmd);
1766 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1769 return missing_container_arg(cmd);
1773 if (thiscmd->cmd_data)
1774 return "<DirectoryMatch > block must specify a path";
1776 return "<Directory > block must specify a path";
1779 cmd->path = ap_getword_conf(cmd->pool, &arg);
1780 cmd->override = OR_ALL|ACCESS_CONF;
1782 if (!strcmp(cmd->path, "~")) {
1783 cmd->path = ap_getword_conf(cmd->pool, &arg);
1785 return "<Directory ~ > block must specify a path";
1786 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1788 return "Regex could not be compiled";
1791 else if (thiscmd->cmd_data) { /* <DirectoryMatch> */
1792 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1794 return "Regex could not be compiled";
1797 else if (!strcmp(cmd->path, "/") == 0)
1802 * Ensure that the pathname is canonical, and append the trailing /
1804 apr_status_t rv = apr_filepath_merge(&newpath, NULL, cmd->path,
1805 APR_FILEPATH_TRUENAME, cmd->pool);
1806 if (rv != APR_SUCCESS && rv != APR_EPATHWILD) {
1807 return apr_pstrcat(cmd->pool, "<Directory \"", cmd->path,
1808 "\"> path is invalid.", NULL);
1811 cmd->path = newpath;
1812 if (cmd->path[strlen(cmd->path) - 1] != '/')
1813 cmd->path = apr_pstrcat(cmd->pool, cmd->path, "/", NULL);
1816 /* initialize our config and fetch it */
1817 conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
1818 &core_module, cmd->pool);
1820 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
1825 conf->d = cmd->path;
1826 conf->d_is_fnmatch = (apr_fnmatch_test(conf->d) != 0);
1828 /* Make this explicit - the "/" root has 0 elements, that is, we
1829 * will always merge it, and it will always sort and merge first.
1830 * All others are sorted and tested by the number of slashes.
1832 if (strcmp(conf->d, "/") == 0)
1833 conf->d_components = 0;
1835 conf->d_components = ap_count_dirs(conf->d);
1837 ap_add_per_dir_conf(cmd->server, new_dir_conf);
1840 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1841 "> arguments not (yet) supported.", NULL);
1844 cmd->path = old_path;
1845 cmd->override = old_overrides;
1850 static const char *urlsection(cmd_parms *cmd, void *mconfig, const char *arg)
1853 const char *endp = ap_strrchr_c(arg, '>');
1854 int old_overrides = cmd->override;
1855 char *old_path = cmd->path;
1856 core_dir_config *conf;
1857 ap_regex_t *r = NULL;
1858 const command_rec *thiscmd = cmd->cmd;
1859 ap_conf_vector_t *new_url_conf = ap_create_per_dir_config(cmd->pool);
1860 const char *err = ap_check_cmd_context(cmd,
1861 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1867 return unclosed_directive(cmd);
1870 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1873 return missing_container_arg(cmd);
1876 cmd->path = ap_getword_conf(cmd->pool, &arg);
1877 cmd->override = OR_ALL|ACCESS_CONF;
1879 if (thiscmd->cmd_data) { /* <LocationMatch> */
1880 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
1882 return "Regex could not be compiled";
1885 else if (!strcmp(cmd->path, "~")) {
1886 cmd->path = ap_getword_conf(cmd->pool, &arg);
1887 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
1889 return "Regex could not be compiled";
1893 /* initialize our config and fetch it */
1894 conf = ap_set_config_vectors(cmd->server, new_url_conf, cmd->path,
1895 &core_module, cmd->pool);
1897 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_url_conf);
1901 conf->d = apr_pstrdup(cmd->pool, cmd->path); /* No mangling, please */
1902 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1905 ap_add_per_url_conf(cmd->server, new_url_conf);
1908 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1909 "> arguments not (yet) supported.", NULL);
1912 cmd->path = old_path;
1913 cmd->override = old_overrides;
1918 static const char *filesection(cmd_parms *cmd, void *mconfig, const char *arg)
1921 const char *endp = ap_strrchr_c(arg, '>');
1922 int old_overrides = cmd->override;
1923 char *old_path = cmd->path;
1924 core_dir_config *conf;
1925 ap_regex_t *r = NULL;
1926 const command_rec *thiscmd = cmd->cmd;
1927 core_dir_config *c = mconfig;
1928 ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
1929 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT|NOT_IN_LOCATION);
1936 return unclosed_directive(cmd);
1939 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1942 return missing_container_arg(cmd);
1945 cmd->path = ap_getword_conf(cmd->pool, &arg);
1946 /* Only if not an .htaccess file */
1948 cmd->override = OR_ALL|ACCESS_CONF;
1951 if (thiscmd->cmd_data) { /* <FilesMatch> */
1952 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1954 return "Regex could not be compiled";
1957 else if (!strcmp(cmd->path, "~")) {
1958 cmd->path = ap_getword_conf(cmd->pool, &arg);
1959 r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1961 return "Regex could not be compiled";
1966 /* Ensure that the pathname is canonical, but we
1967 * can't test the case/aliases without a fixed path */
1968 if (apr_filepath_merge(&newpath, "", cmd->path,
1969 0, cmd->pool) != APR_SUCCESS)
1970 return apr_pstrcat(cmd->pool, "<Files \"", cmd->path,
1971 "\"> is invalid.", NULL);
1972 cmd->path = newpath;
1975 /* initialize our config and fetch it */
1976 conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
1977 &core_module, cmd->pool);
1979 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
1983 conf->d = cmd->path;
1984 conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1987 ap_add_file_conf(c, new_file_conf);
1990 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1991 "> arguments not (yet) supported.", NULL);
1994 cmd->path = old_path;
1995 cmd->override = old_overrides;
1999 static const char *ifsection(cmd_parms *cmd, void *mconfig, const char *arg)
2002 const char *endp = ap_strrchr_c(arg, '>');
2003 int old_overrides = cmd->override;
2004 char *old_path = cmd->path;
2005 core_dir_config *conf;
2006 const command_rec *thiscmd = cmd->cmd;
2007 core_dir_config *c = mconfig;
2008 ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
2009 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT|NOT_IN_LOCATION);
2010 const char *condition;
2018 return unclosed_directive(cmd);
2021 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2024 return missing_container_arg(cmd);
2027 condition = ap_getword_conf(cmd->pool, &arg);
2028 /* Only if not an .htaccess file */
2030 cmd->override = OR_ALL|ACCESS_CONF;
2033 /* initialize our config and fetch it */
2034 conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
2035 &core_module, cmd->pool);
2037 conf->condition = ap_expr_parse(cmd->pool, condition, &expr_err);
2039 return "Cannot parse condition clause";
2042 errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
2046 conf->d = cmd->path;
2047 conf->d_is_fnmatch = 0;
2050 ap_add_file_conf(c, new_file_conf);
2053 return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
2054 "> arguments not supported.", NULL);
2057 cmd->path = old_path;
2058 cmd->override = old_overrides;
2063 static const char *start_ifmod(cmd_parms *cmd, void *mconfig, const char *arg)
2065 const char *endp = ap_strrchr_c(arg, '>');
2066 int not = (arg[0] == '!');
2070 return unclosed_directive(cmd);
2073 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2080 return missing_container_arg(cmd);
2083 found = ap_find_linked_module(arg);
2085 /* search prelinked stuff */
2087 ap_module_symbol_t *current = ap_prelinked_module_symbols;
2089 for (; current->name; ++current) {
2090 if (!strcmp(current->name, arg)) {
2091 found = current->modp;
2097 /* search dynamic stuff */
2099 APR_OPTIONAL_FN_TYPE(ap_find_loaded_module_symbol) *check_symbol =
2100 APR_RETRIEVE_OPTIONAL_FN(ap_find_loaded_module_symbol);
2103 found = check_symbol(cmd->server, arg);
2107 if ((!not && found) || (not && !found)) {
2108 ap_directive_t *parent = NULL;
2109 ap_directive_t *current = NULL;
2112 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2113 ¤t, &parent, "<IfModule");
2114 *(ap_directive_t **)mconfig = current;
2118 *(ap_directive_t **)mconfig = NULL;
2119 return ap_soak_end_container(cmd, "<IfModule");
2123 AP_DECLARE(int) ap_exists_config_define(const char *name)
2128 defines = (char **)ap_server_config_defines->elts;
2129 for (i = 0; i < ap_server_config_defines->nelts; i++) {
2130 if (strcmp(defines[i], name) == 0) {
2138 static const char *start_ifdefine(cmd_parms *cmd, void *dummy, const char *arg)
2144 endp = ap_strrchr_c(arg, '>');
2146 return unclosed_directive(cmd);
2149 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2151 if (arg[0] == '!') {
2157 return missing_container_arg(cmd);
2160 defined = ap_exists_config_define(arg);
2161 if ((!not && defined) || (not && !defined)) {
2162 ap_directive_t *parent = NULL;
2163 ap_directive_t *current = NULL;
2166 retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2167 ¤t, &parent, "<IfDefine");
2168 *(ap_directive_t **)dummy = current;
2172 *(ap_directive_t **)dummy = NULL;
2173 return ap_soak_end_container(cmd, "<IfDefine");
2177 /* httpd.conf commands... beginning with the <VirtualHost> business */
2179 static const char *virtualhost_section(cmd_parms *cmd, void *dummy,
2182 server_rec *main_server = cmd->server, *s;
2184 const char *endp = ap_strrchr_c(arg, '>');
2185 apr_pool_t *p = cmd->pool;
2187 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2193 return unclosed_directive(cmd);
2196 arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2199 return missing_container_arg(cmd);
2202 /* FIXME: There's another feature waiting to happen here -- since you
2203 can now put multiple addresses/names on a single <VirtualHost>
2204 you might want to use it to group common definitions and then
2205 define other "subhosts" with their individual differences. But
2206 personally I'd rather just do it with a macro preprocessor. -djg */
2207 if (main_server->is_virtual) {
2208 return "<VirtualHost> doesn't nest!";
2211 errmsg = ap_init_virtual_host(p, arg, main_server, &s);
2216 s->next = main_server->next;
2217 main_server->next = s;
2219 s->defn_name = cmd->directive->filename;
2220 s->defn_line_number = cmd->directive->line_num;
2224 errmsg = ap_walk_config(cmd->directive->first_child, cmd,
2225 s->lookup_defaults);
2227 cmd->server = main_server;
2232 static const char *set_server_alias(cmd_parms *cmd, void *dummy,
2235 if (!cmd->server->names) {
2236 return "ServerAlias only used in <VirtualHost>";
2240 char **item, *name = ap_getword_conf(cmd->pool, &arg);
2242 if (ap_is_matchexp(name)) {
2243 item = (char **)apr_array_push(cmd->server->wild_names);
2246 item = (char **)apr_array_push(cmd->server->names);
2255 static const char *set_accf_map(cmd_parms *cmd, void *dummy,
2256 const char *iproto, const char* iaccf)
2258 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2259 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2267 proto = apr_pstrdup(cmd->pool, iproto);
2268 ap_str_tolower(proto);
2269 accf = apr_pstrdup(cmd->pool, iaccf);
2270 ap_str_tolower(accf);
2271 apr_table_set(conf->accf_map, proto, accf);
2276 AP_DECLARE(const char*) ap_get_server_protocol(server_rec* s)
2278 core_server_config *conf = ap_get_module_config(s->module_config,
2280 return conf->protocol;
2283 AP_DECLARE(void) ap_set_server_protocol(server_rec* s, const char* proto)
2285 core_server_config *conf = ap_get_module_config(s->module_config,
2287 conf->protocol = proto;
2290 static const char *set_protocol(cmd_parms *cmd, void *dummy,
2293 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2294 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2302 proto = apr_pstrdup(cmd->pool, arg);
2303 ap_str_tolower(proto);
2304 conf->protocol = proto;
2309 static const char *set_server_string_slot(cmd_parms *cmd, void *dummy,
2312 /* This one's pretty generic... */
2314 int offset = (int)(long)cmd->info;
2315 char *struct_ptr = (char *)cmd->server;
2317 const char *err = ap_check_cmd_context(cmd,
2318 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2323 *(const char **)(struct_ptr + offset) = arg;
2328 * The ServerName directive takes one argument with format
2329 * [scheme://]fully-qualified-domain-name[:port], for instance
2330 * ServerName www.example.com
2331 * ServerName www.example.com:80
2332 * ServerName https://www.example.com:443
2335 static const char *server_hostname_port(cmd_parms *cmd, void *dummy, const char *arg)
2337 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2338 const char *portstr, *part;
2346 part = ap_strstr_c(arg, "://");
2349 scheme = apr_pstrndup(cmd->pool, arg, part - arg);
2350 ap_str_tolower(scheme);
2351 cmd->server->server_scheme = (const char *)scheme;
2357 portstr = ap_strchr_c(part, ':');
2359 cmd->server->server_hostname = apr_pstrndup(cmd->pool, part,
2362 port = atoi(portstr);
2363 if (port <= 0 || port >= 65536) { /* 65536 == 1<<16 */
2364 return apr_pstrcat(cmd->temp_pool, "The port number \"", arg,
2365 "\" is outside the appropriate range "
2366 "(i.e., 1..65535).", NULL);
2370 cmd->server->server_hostname = apr_pstrdup(cmd->pool, part);
2374 cmd->server->port = port;
2378 static const char *set_signature_flag(cmd_parms *cmd, void *d_,
2381 core_dir_config *d = d_;
2382 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2388 if (strcasecmp(arg, "On") == 0) {
2389 d->server_signature = srv_sig_on;
2391 else if (strcasecmp(arg, "Off") == 0) {
2392 d->server_signature = srv_sig_off;
2394 else if (strcasecmp(arg, "EMail") == 0) {
2395 d->server_signature = srv_sig_withmail;
2398 return "ServerSignature: use one of: off | on | email";
2404 static const char *set_server_root(cmd_parms *cmd, void *dummy,
2407 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2413 if ((apr_filepath_merge((char**)&ap_server_root, NULL, arg,
2414 APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS)
2415 || !ap_is_directory(cmd->pool, ap_server_root)) {
2416 return "ServerRoot must be a valid directory";
2422 static const char *set_timeout(cmd_parms *cmd, void *dummy, const char *arg)
2424 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2430 cmd->server->timeout = apr_time_from_sec(atoi(arg));
2434 static const char *set_allow2f(cmd_parms *cmd, void *d_, int arg)
2436 core_dir_config *d = d_;
2437 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2443 d->allow_encoded_slashes = arg != 0;
2447 static const char *set_hostname_lookups(cmd_parms *cmd, void *d_,
2450 core_dir_config *d = d_;
2451 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2457 if (!strcasecmp(arg, "on")) {
2458 d->hostname_lookups = HOSTNAME_LOOKUP_ON;
2460 else if (!strcasecmp(arg, "off")) {
2461 d->hostname_lookups = HOSTNAME_LOOKUP_OFF;
2463 else if (!strcasecmp(arg, "double")) {
2464 d->hostname_lookups = HOSTNAME_LOOKUP_DOUBLE;
2467 return "parameter must be 'on', 'off', or 'double'";
2473 static const char *set_serverpath(cmd_parms *cmd, void *dummy,
2476 const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2482 cmd->server->path = arg;
2483 cmd->server->pathlen = (int)strlen(arg);
2487 static const char *set_content_md5(cmd_parms *cmd, void *d_, int arg)
2489 core_dir_config *d = d_;
2490 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2496 d->content_md5 = arg != 0;
2500 static const char *set_accept_path_info(cmd_parms *cmd, void *d_, const char *arg)
2502 core_dir_config *d = d_;
2504 if (strcasecmp(arg, "on") == 0) {
2505 d->accept_path_info = AP_REQ_ACCEPT_PATH_INFO;
2507 else if (strcasecmp(arg, "off") == 0) {
2508 d->accept_path_info = AP_REQ_REJECT_PATH_INFO;
2510 else if (strcasecmp(arg, "default") == 0) {
2511 d->accept_path_info = AP_REQ_DEFAULT_PATH_INFO;
2514 return "AcceptPathInfo must be set to on, off or default";
2520 static const char *set_use_canonical_name(cmd_parms *cmd, void *d_,
2523 core_dir_config *d = d_;
2524 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2530 if (strcasecmp(arg, "on") == 0) {
2531 d->use_canonical_name = USE_CANONICAL_NAME_ON;
2533 else if (strcasecmp(arg, "off") == 0) {
2534 d->use_canonical_name = USE_CANONICAL_NAME_OFF;
2536 else if (strcasecmp(arg, "dns") == 0) {
2537 d->use_canonical_name = USE_CANONICAL_NAME_DNS;
2540 return "parameter must be 'on', 'off', or 'dns'";
2546 static const char *set_use_canonical_phys_port(cmd_parms *cmd, void *d_,
2549 core_dir_config *d = d_;
2550 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2556 if (strcasecmp(arg, "on") == 0) {
2557 d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_ON;
2559 else if (strcasecmp(arg, "off") == 0) {
2560 d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_OFF;
2563 return "parameter must be 'on' or 'off'";
2570 static const char *include_config (cmd_parms *cmd, void *dummy,
2573 ap_directive_t *conftree = NULL;
2574 const char* conffile, *error;
2575 unsigned *recursion;
2578 apr_pool_userdata_get(&data, "ap_include_sentinel", cmd->pool);
2583 data = recursion = apr_palloc(cmd->pool, sizeof(*recursion));
2585 apr_pool_userdata_setn(data, "ap_include_sentinel", NULL, cmd->pool);
2588 if (++*recursion > AP_MAX_INCLUDE_DEPTH) {
2590 return apr_psprintf(cmd->pool, "Exceeded maximum include depth of %u. "
2591 "You have probably a recursion somewhere.",
2592 AP_MAX_INCLUDE_DEPTH);
2595 conffile = ap_server_root_relative(cmd->pool, name);
2598 return apr_pstrcat(cmd->pool, "Invalid Include path ",
2602 error = ap_process_resource_config(cmd->server, conffile,
2603 &conftree, cmd->pool, cmd->temp_pool);
2609 *(ap_directive_t **)dummy = conftree;
2611 /* recursion level done */
2619 static const char *set_loglevel(cmd_parms *cmd, void *dummy, const char *arg)
2623 const char *err = ap_check_cmd_context(cmd,
2624 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2629 if ((str = ap_getword_conf(cmd->pool, &arg))) {
2630 if (!strcasecmp(str, "emerg")) {
2631 cmd->server->loglevel = APLOG_EMERG;
2633 else if (!strcasecmp(str, "alert")) {
2634 cmd->server->loglevel = APLOG_ALERT;
2636 else if (!strcasecmp(str, "crit")) {
2637 cmd->server->loglevel = APLOG_CRIT;
2639 else if (!strcasecmp(str, "error")) {
2640 cmd->server->loglevel = APLOG_ERR;
2642 else if (!strcasecmp(str, "warn")) {
2643 cmd->server->loglevel = APLOG_WARNING;
2645 else if (!strcasecmp(str, "notice")) {
2646 cmd->server->loglevel = APLOG_NOTICE;
2648 else if (!strcasecmp(str, "info")) {
2649 cmd->server->loglevel = APLOG_INFO;
2651 else if (!strcasecmp(str, "debug")) {
2652 cmd->server->loglevel = APLOG_DEBUG;
2655 return "LogLevel requires level keyword: one of "
2656 "emerg/alert/crit/error/warn/notice/info/debug";
2660 return "LogLevel requires level keyword";
2666 AP_DECLARE(const char *) ap_psignature(const char *prefix, request_rec *r)
2669 core_dir_config *conf;
2671 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
2673 if ((conf->server_signature == srv_sig_off)
2674 || (conf->server_signature == srv_sig_unset)) {
2678 apr_snprintf(sport, sizeof sport, "%u", (unsigned) ap_get_server_port(r));
2680 if (conf->server_signature == srv_sig_withmail) {
2681 return apr_pstrcat(r->pool, prefix, "<address>",
2682 ap_get_server_banner(),
2683 " Server at <a href=\"",
2684 ap_is_url(r->server->server_admin) ? "" : "mailto:",
2685 ap_escape_html(r->pool, r->server->server_admin),
2687 ap_escape_html(r->pool, ap_get_server_name(r)),
2688 "</a> Port ", sport,
2689 "</address>\n", NULL);
2692 return apr_pstrcat(r->pool, prefix, "<address>", ap_get_server_banner(),
2694 ap_escape_html(r->pool, ap_get_server_name(r)),
2696 "</address>\n", NULL);
2700 * Handle a request to include the server's OS platform in the Server
2701 * response header field (the ServerTokens directive). Unfortunately
2702 * this requires a new global in order to communicate the setting back to
2703 * http_main so it can insert the information in the right place in the
2707 static char *server_banner = NULL;
2708 static int banner_locked = 0;
2709 static char *server_description = NULL;
2711 enum server_token_type {
2712 SrvTk_MAJOR, /* eg: Apache/2 */
2713 SrvTk_MINOR, /* eg. Apache/2.0 */
2714 SrvTk_MINIMAL, /* eg: Apache/2.0.41 */
2715 SrvTk_OS, /* eg: Apache/2.0.41 (UNIX) */
2716 SrvTk_FULL, /* eg: Apache/2.0.41 (UNIX) PHP/4.2.2 FooBar/1.2b */
2717 SrvTk_PRODUCT_ONLY /* eg: Apache */
2719 static enum server_token_type ap_server_tokens = SrvTk_FULL;
2721 static apr_status_t reset_banner(void *dummy)
2724 ap_server_tokens = SrvTk_FULL;
2725 server_banner = NULL;
2726 server_description = NULL;
2730 AP_DECLARE(void) ap_get_server_revision(ap_version_t *version)
2732 version->major = AP_SERVER_MAJORVERSION_NUMBER;
2733 version->minor = AP_SERVER_MINORVERSION_NUMBER;
2734 version->patch = AP_SERVER_PATCHLEVEL_NUMBER;
2735 version->add_string = AP_SERVER_ADD_STRING;
2738 AP_DECLARE(const char *) ap_get_server_description(void)
2740 return server_description ? server_description :
2741 AP_SERVER_BASEVERSION " (" PLATFORM ")";
2744 AP_DECLARE(const char *) ap_get_server_banner(void)
2746 return server_banner ? server_banner : AP_SERVER_BASEVERSION;
2749 AP_DECLARE(void) ap_add_version_component(apr_pool_t *pconf, const char *component)
2751 if (! banner_locked) {
2753 * If the version string is null, register our cleanup to reset the
2754 * pointer on pool destruction. We also know that, if NULL,
2755 * we are adding the original SERVER_BASEVERSION string.
2757 if (server_banner == NULL) {
2758 apr_pool_cleanup_register(pconf, NULL, reset_banner,
2759 apr_pool_cleanup_null);
2760 server_banner = apr_pstrdup(pconf, component);
2764 * Tack the given component identifier to the end of
2765 * the existing string.
2767 server_banner = apr_pstrcat(pconf, server_banner, " ",
2771 server_description = apr_pstrcat(pconf, server_description, " ",
2776 * This routine adds the real server base identity to the banner string,
2777 * and then locks out changes until the next reconfig.
2779 static void set_banner(apr_pool_t *pconf)
2781 if (ap_server_tokens == SrvTk_PRODUCT_ONLY) {
2782 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT);
2784 else if (ap_server_tokens == SrvTk_MINIMAL) {
2785 ap_add_version_component(pconf, AP_SERVER_BASEVERSION);
2787 else if (ap_server_tokens == SrvTk_MINOR) {
2788 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MINORREVISION);
2790 else if (ap_server_tokens == SrvTk_MAJOR) {
2791 ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MAJORVERSION);
2794 ap_add_version_component(pconf, AP_SERVER_BASEVERSION " (" PLATFORM ")");
2798 * Lock the server_banner string if we're not displaying
2799 * the full set of tokens
2801 if (ap_server_tokens != SrvTk_FULL) {
2804 server_description = AP_SERVER_BASEVERSION " (" PLATFORM ")";
2807 static const char *set_serv_tokens(cmd_parms *cmd, void *dummy,
2810 const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2816 if (!strcasecmp(arg, "OS")) {
2817 ap_server_tokens = SrvTk_OS;
2819 else if (!strcasecmp(arg, "Min") || !strcasecmp(arg, "Minimal")) {
2820 ap_server_tokens = SrvTk_MINIMAL;
2822 else if (!strcasecmp(arg, "Major")) {
2823 ap_server_tokens = SrvTk_MAJOR;
2825 else if (!strcasecmp(arg, "Minor") ) {
2826 ap_server_tokens = SrvTk_MINOR;
2828 else if (!strcasecmp(arg, "Prod") || !strcasecmp(arg, "ProductOnly")) {
2829 ap_server_tokens = SrvTk_PRODUCT_ONLY;
2832 ap_server_tokens = SrvTk_FULL;
2838 static const char *set_limit_req_line(cmd_parms *cmd, void *dummy,
2841 const char *err = ap_check_cmd_context(cmd,
2842 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2851 return apr_pstrcat(cmd->temp_pool, "LimitRequestLine \"", arg,
2852 "\" must be a non-negative integer", NULL);
2855 cmd->server->limit_req_line = lim;
2859 static const char *set_limit_req_fieldsize(cmd_parms *cmd, void *dummy,
2862 const char *err = ap_check_cmd_context(cmd,
2863 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2872 return apr_pstrcat(cmd->temp_pool, "LimitRequestFieldsize \"", arg,
2873 "\" must be a non-negative integer",
2877 cmd->server->limit_req_fieldsize = lim;
2881 static const char *set_limit_req_fields(cmd_parms *cmd, void *dummy,
2884 const char *err = ap_check_cmd_context(cmd,
2885 NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2894 return apr_pstrcat(cmd->temp_pool, "LimitRequestFields \"", arg,
2895 "\" must be a non-negative integer (0 = no limit)",
2899 cmd->server->limit_req_fields = lim;
2903 static const char *set_limit_req_body(cmd_parms *cmd, void *conf_,
2906 core_dir_config *conf = conf_;
2907 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2914 if (APR_SUCCESS != apr_strtoff(&conf->limit_req_body, arg, &errp, 10)) {
2915 return "LimitRequestBody argument is not parsable.";
2917 if (*errp || conf->limit_req_body < 0) {
2918 return "LimitRequestBody requires a non-negative integer.";
2924 static const char *set_limit_xml_req_body(cmd_parms *cmd, void *conf_,
2927 core_dir_config *conf = conf_;
2928 const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2934 conf->limit_xml_body = atol(arg);
2935 if (conf->limit_xml_body < 0)
2936 return "LimitXMLRequestBody requires a non-negative integer.";
2941 AP_DECLARE(size_t) ap_get_limit_xml_body(const request_rec *r)
2943 core_dir_config *conf;
2945 conf = ap_get_module_config(r->per_dir_config, &core_module);
2946 if (conf->limit_xml_body == AP_LIMIT_UNSET)
2947 return AP_DEFAULT_LIMIT_XML_BODY;
2949 return (size_t)conf->limit_xml_body;
2952 #if !defined (RLIMIT_CPU) || !(defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)) || !defined (RLIMIT_NPROC)
2953 static const char *no_set_limit(cmd_parms *cmd, void *conf_,
2954 const char *arg, const char *arg2)
2956 ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server,
2957 "%s not supported on this platform", cmd->cmd->name);
2964 static const char *set_limit_cpu(cmd_parms *cmd, void *conf_,
2965 const char *arg, const char *arg2)
2967 core_dir_config *conf = conf_;
2969 unixd_set_rlimit(cmd, &conf->limit_cpu, arg, arg2, RLIMIT_CPU);
2974 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
2975 static const char *set_limit_mem(cmd_parms *cmd, void *conf_,
2976 const char *arg, const char * arg2)
2978 core_dir_config *conf = conf_;
2980 #if defined(RLIMIT_AS)
2981 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2 ,RLIMIT_AS);
2982 #elif defined(RLIMIT_DATA)
2983 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_DATA);
2984 #elif defined(RLIMIT_VMEM)
2985 unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_VMEM);
2993 static const char *set_limit_nproc(cmd_parms *cmd, void *conf_,
2994 const char *arg, const char * arg2)
2996 core_dir_config *conf = conf_;
2998 unixd_set_rlimit(cmd, &conf->limit_nproc, arg, arg2, RLIMIT_NPROC);
3003 static const char *set_recursion_limit(cmd_parms *cmd, void *dummy,
3004 const char *arg1, const char *arg2)
3006 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
3008 int limit = atoi(arg1);
3011 return "The recursion limit must be greater than zero.";
3014 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
3015 "Limiting internal redirects to very low numbers may "
3016 "cause normal requests to fail.");
3019 conf->redirect_limit = limit;
3025 return "The recursion limit must be greater than zero.";
3028 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
3029 "Limiting the subrequest depth to a very low level may"
3030 " cause normal requests to fail.");
3034 conf->subreq_limit = limit;
3039 static void log_backtrace(const request_rec *r)
3041 const request_rec *top = r;
3043 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
3044 "r->uri = %s", r->uri ? r->uri : "(unexpectedly NULL)");
3046 while (top && (top->prev || top->main)) {
3049 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
3050 "redirected from r->uri = %s",
3051 top->uri ? top->uri : "(unexpectedly NULL)");
3054 if (!top->prev && top->main) {
3056 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
3057 "subrequested from r->uri = %s",
3058 top->uri ? top->uri : "(unexpectedly NULL)");
3064 * check whether redirect limit is reached
3066 AP_DECLARE(int) ap_is_recursion_limit_exceeded(const request_rec *r)
3068 core_server_config *conf = ap_get_module_config(r->server->module_config,
3070 const request_rec *top = r;
3071 int redirects = 0, subreqs = 0;
3072 int rlimit = conf->redirect_limit
3073 ? conf->redirect_limit
3074 : AP_DEFAULT_MAX_INTERNAL_REDIRECTS;
3075 int slimit = conf->subreq_limit
3076 ? conf->subreq_limit
3077 : AP_DEFAULT_MAX_SUBREQ_DEPTH;
3080 while (top->prev || top->main) {
3082 if (++redirects >= rlimit) {
3083 /* uuh, too much. */
3084 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3085 "Request exceeded the limit of %d internal "
3086 "redirects due to probable configuration error. "
3087 "Use 'LimitInternalRecursion' to increase the "
3088 "limit if necessary. Use 'LogLevel debug' to get "
3089 "a backtrace.", rlimit);
3091 /* post backtrace */
3094 /* return failure */
3101 if (!top->prev && top->main) {
3102 if (++subreqs >= slimit) {
3103 /* uuh, too much. */
3104 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3105 "Request exceeded the limit of %d subrequest "
3106 "nesting levels due to probable configuration "
3107 "error. Use 'LimitInternalRecursion' to increase "
3108 "the limit if necessary. Use 'LogLevel debug' to "
3109 "get a backtrace.", slimit);
3111 /* post backtrace */
3114 /* return failure */
3122 /* recursion state: ok */
3126 static const char *add_ct_output_filters(cmd_parms *cmd, void *conf_,
3127 const char *arg, const char *arg2)
3129 core_dir_config *conf = conf_;
3130 ap_filter_rec_t *old, *new = NULL;
3131 const char *filter_name;
3133 if (!conf->ct_output_filters) {
3134 conf->ct_output_filters = apr_hash_make(cmd->pool);
3138 old = (ap_filter_rec_t*) apr_hash_get(conf->ct_output_filters, arg2,
3139 APR_HASH_KEY_STRING);
3140 /* find last entry */
3149 (filter_name = ap_getword(cmd->pool, &arg, ';')) &&
3150 strcmp(filter_name, "")) {
3151 new = apr_pcalloc(cmd->pool, sizeof(ap_filter_rec_t));
3152 new->name = filter_name;
3154 /* We found something, so let's append it. */
3159 apr_hash_set(conf->ct_output_filters, arg2,
3160 APR_HASH_KEY_STRING, new);
3166 return "invalid filter name";
3172 * Insert filters requested by the AddOutputFilterByType
3173 * configuration directive. We cannot add filters based
3174 * on content-type until after the handler has started
3175 * to run. Only then do we reliably know the content-type.
3177 void ap_add_output_filters_by_type(request_rec *r)
3179 core_dir_config *conf;
3182 conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3185 /* We can't do anything with no content-type or if we don't have a
3186 * filter configured.
3188 if (!r->content_type || !conf->ct_output_filters) {
3192 /* remove c-t decoration */
3193 ctype = ap_field_noparam(r->pool, r->content_type);
3195 ap_filter_rec_t *ct_filter;
3196 ct_filter = apr_hash_get(conf->ct_output_filters, ctype,
3197 APR_HASH_KEY_STRING);
3199 ap_add_output_filter(ct_filter->name, NULL, r, r->connection);
3200 ct_filter = ct_filter->next;
3207 static const char *set_trace_enable(cmd_parms *cmd, void *dummy,
3210 core_server_config *conf = ap_get_module_config(cmd->server->module_config,
3213 if (strcasecmp(arg1, "on") == 0) {
3214 conf->trace_enable = AP_TRACE_ENABLE;
3216 else if (strcasecmp(arg1, "off") == 0) {
3217 conf->trace_enable = AP_TRACE_DISABLE;
3219 else if (strcasecmp(arg1, "extended") == 0) {
3220 conf->trace_enable = AP_TRACE_EXTENDED;
3223 return "TraceEnable must be one of 'on', 'off', or 'extended'";
3229 /* Note --- ErrorDocument will now work from .htaccess files.
3230 * The AllowOverride of Fileinfo allows webmasters to turn it off
3233 static const command_rec core_cmds[] = {
3235 /* Old access config file commands */
3237 AP_INIT_RAW_ARGS("<Directory", dirsection, NULL, RSRC_CONF,
3238 "Container for directives affecting resources located in the specified "
3240 AP_INIT_RAW_ARGS("<Location", urlsection, NULL, RSRC_CONF,
3241 "Container for directives affecting resources accessed through the "
3242 "specified URL paths"),
3243 AP_INIT_RAW_ARGS("<VirtualHost", virtualhost_section, NULL, RSRC_CONF,
3244 "Container to map directives to a particular virtual host, takes one or "
3245 "more host addresses"),
3246 AP_INIT_RAW_ARGS("<Files", filesection, NULL, OR_ALL,
3247 "Container for directives affecting files matching specified patterns"),
3248 AP_INIT_RAW_ARGS("<Limit", ap_limit_section, NULL, OR_ALL,
3249 "Container for authentication directives when accessed using specified HTTP "
3251 AP_INIT_RAW_ARGS("<LimitExcept", ap_limit_section, (void*)1, OR_ALL,
3252 "Container for authentication directives to be applied when any HTTP "
3253 "method other than those specified is used to access the resource"),
3254 AP_INIT_TAKE1("<IfModule", start_ifmod, NULL, EXEC_ON_READ | OR_ALL,
3255 "Container for directives based on existance of specified modules"),
3256 AP_INIT_TAKE1("<IfDefine", start_ifdefine, NULL, EXEC_ON_READ | OR_ALL,
3257 "Container for directives based on existance of command line defines"),
3258 AP_INIT_RAW_ARGS("<DirectoryMatch", dirsection, (void*)1, RSRC_CONF,
3259 "Container for directives affecting resources located in the "
3260 "specified directories"),
3261 AP_INIT_RAW_ARGS("<LocationMatch", urlsection, (void*)1, RSRC_CONF,
3262 "Container for directives affecting resources accessed through the "
3263 "specified URL paths"),
3264 AP_INIT_RAW_ARGS("<FilesMatch", filesection, (void*)1, OR_ALL,
3265 "Container for directives affecting files matching specified patterns"),
3267 AP_INIT_TAKE1("GprofDir", set_gprof_dir, NULL, RSRC_CONF,
3268 "Directory to plop gmon.out files"),
3270 AP_INIT_TAKE1("AddDefaultCharset", set_add_default_charset, NULL, OR_FILEINFO,
3271 "The name of the default charset to add to any Content-Type without one or 'Off' to disable"),
3272 AP_INIT_TAKE1("AcceptPathInfo", set_accept_path_info, NULL, OR_FILEINFO,
3273 "Set to on or off for PATH_INFO to be accepted by handlers, or default for the per-handler preference"),
3274 AP_INIT_TAKE1("Define", set_define, NULL, RSRC_CONF,
3275 "Define the existance of a variable. Same as passing -D to the command line."),
3276 AP_INIT_RAW_ARGS("<If", ifsection, NULL, OR_ALL,
3277 "Container for directives to be conditionally applied"),
3279 /* Old resource config file commands */
3281 AP_INIT_RAW_ARGS("AccessFileName", set_access_name, NULL, RSRC_CONF,
3282 "Name(s) of per-directory config files (default: .htaccess)"),
3283 AP_INIT_TAKE1("DocumentRoot", set_document_root, NULL, RSRC_CONF,
3284 "Root directory of the document tree"),
3285 AP_INIT_TAKE2("ErrorDocument", set_error_document, NULL, OR_FILEINFO,
3286 "Change responses for HTTP errors"),
3287 AP_INIT_RAW_ARGS("AllowOverride", set_override, NULL, ACCESS_CONF,
3288 "Controls what groups of directives can be configured by per-directory "
3290 AP_INIT_RAW_ARGS("Options", set_options, NULL, OR_OPTIONS,
3291 "Set a number of attributes for a given directory"),
3292 AP_INIT_TAKE1("DefaultType", ap_set_string_slot,
3293 (void*)APR_OFFSETOF(core_dir_config, ap_default_type),
3294 OR_FILEINFO, "the default MIME type for untypable files"),
3295 AP_INIT_RAW_ARGS("FileETag", set_etag_bits, NULL, OR_FILEINFO,
3296 "Specify components used to construct a file's ETag"),
3297 AP_INIT_TAKE1("EnableMMAP", set_enable_mmap, NULL, OR_FILEINFO,
3298 "Controls whether memory-mapping may be used to read files"),
3299 AP_INIT_TAKE1("EnableSendfile", set_enable_sendfile, NULL, OR_FILEINFO,
3300 "Controls whether sendfile may be used to transmit files"),
3302 /* Old server config file commands */
3304 AP_INIT_TAKE1("Protocol", set_protocol, NULL, RSRC_CONF,
3305 "Set the Protocol for httpd to use."),
3306 AP_INIT_TAKE2("AcceptFilter", set_accf_map, NULL, RSRC_CONF,
3307 "Set the Accept Filter to use for a protocol"),
3308 AP_INIT_TAKE1("Port", ap_set_deprecated, NULL, RSRC_CONF,
3309 "Port was replaced with Listen in Apache 2.0"),
3310 AP_INIT_TAKE1("HostnameLookups", set_hostname_lookups, NULL,
3311 ACCESS_CONF|RSRC_CONF,
3312 "\"on\" to enable, \"off\" to disable reverse DNS lookups, or \"double\" to "
3313 "enable double-reverse DNS lookups"),
3314 AP_INIT_TAKE1("ServerAdmin", set_server_string_slot,
3315 (void *)APR_OFFSETOF(server_rec, server_admin), RSRC_CONF,
3316 "The email address of the server administrator"),
3317 AP_INIT_TAKE1("ServerName", server_hostname_port, NULL, RSRC_CONF,
3318 "The hostname and port of the server"),
3319 AP_INIT_TAKE1("ServerSignature", set_signature_flag, NULL, OR_ALL,
3320 "En-/disable server signature (on|off|email)"),
3321 AP_INIT_TAKE1("ServerRoot", set_server_root, NULL, RSRC_CONF | EXEC_ON_READ,
3322 "Common directory of server-related files (logs, confs, etc.)"),
3323 AP_INIT_TAKE1("ErrorLog", set_server_string_slot,
3324 (void *)APR_OFFSETOF(server_rec, error_fname), RSRC_CONF,
3325 "The filename of the error log"),
3326 AP_INIT_RAW_ARGS("ServerAlias", set_server_alias, NULL, RSRC_CONF,
3327 "A name or names alternately used to access the server"),
3328 AP_INIT_TAKE1("ServerPath", set_serverpath, NULL, RSRC_CONF,
3329 "The pathname the server can be reached at"),
3330 AP_INIT_TAKE1("Timeout", set_timeout, NULL, RSRC_CONF,
3331 "Timeout duration (sec)"),
3332 AP_INIT_FLAG("ContentDigest", set_content_md5, NULL, OR_OPTIONS,
3333 "whether or not to send a Content-MD5 header with each request"),
3334 AP_INIT_TAKE1("UseCanonicalName", set_use_canonical_name, NULL,
3335 RSRC_CONF|ACCESS_CONF,
3336 "How to work out the ServerName : Port when constructing URLs"),
3337 AP_INIT_TAKE1("UseCanonicalPhysicalPort", set_use_canonical_phys_port, NULL,
3338 RSRC_CONF|ACCESS_CONF,
3339 "Whether to use the physical Port when constructing URLs"),
3340 /* TODO: RlimitFoo should all be part of mod_cgi, not in the core */
3341 /* TODO: ListenBacklog in MPM */
3342 AP_INIT_TAKE1("Include", include_config, NULL,
3343 (RSRC_CONF | ACCESS_CONF | EXEC_ON_READ),
3344 "Name of the config file to be included"),
3345 AP_INIT_TAKE1("LogLevel", set_loglevel, NULL, RSRC_CONF,
3346 "Level of verbosity in error logging"),
3347 AP_INIT_TAKE1("NameVirtualHost", ap_set_name_virtual_host, NULL, RSRC_CONF,
3348 "A numeric IP address:port, or the name of a host"),
3349 AP_INIT_TAKE1("ServerTokens", set_serv_tokens, NULL, RSRC_CONF,
3350 "Determine tokens displayed in the Server: header - Min(imal), OS or Full"),
3351 AP_INIT_TAKE1("LimitRequestLine", set_limit_req_line, NULL, RSRC_CONF,
3352 "Limit on maximum size of an HTTP request line"),
3353 AP_INIT_TAKE1("LimitRequestFieldsize", set_limit_req_fieldsize, NULL,
3355 "Limit on maximum size of an HTTP request header field"),
3356 AP_INIT_TAKE1("LimitRequestFields", set_limit_req_fields, NULL, RSRC_CONF,
3357 "Limit (0 = unlimited) on max number of header fields in a request message"),
3358 AP_INIT_TAKE1("LimitRequestBody", set_limit_req_body,
3359 (void*)APR_OFFSETOF(core_dir_config, limit_req_body), OR_ALL,
3360 "Limit (in bytes) on maximum size of request message body"),
3361 AP_INIT_TAKE1("LimitXMLRequestBody", set_limit_xml_req_body, NULL, OR_ALL,
3362 "Limit (in bytes) on maximum size of an XML-based request "
3365 /* System Resource Controls */
3367 AP_INIT_TAKE12("RLimitCPU", set_limit_cpu,
3368 (void*)APR_OFFSETOF(core_dir_config, limit_cpu),
3369 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3371 AP_INIT_TAKE12("RLimitCPU", no_set_limit, NULL,
3372 OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3374 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined (RLIMIT_AS)
3375 AP_INIT_TAKE12("RLimitMEM", set_limit_mem,
3376 (void*)APR_OFFSETOF(core_dir_config, limit_mem),
3377 OR_ALL, "Soft/hard limits for max memory usage per process"),
3379 AP_INIT_TAKE12("RLimitMEM", no_set_limit, NULL,
3380 OR_ALL, "Soft/hard limits for max memory usage per process"),
3383 AP_INIT_TAKE12("RLimitNPROC", set_limit_nproc,
3384 (void*)APR_OFFSETOF(core_dir_config, limit_nproc),
3385 OR_ALL, "soft/hard limits for max number of processes per uid"),
3387 AP_INIT_TAKE12("RLimitNPROC", no_set_limit, NULL,
3388 OR_ALL, "soft/hard limits for max number of processes per uid"),
3391 /* internal recursion stopper */
3392 AP_INIT_TAKE12("LimitInternalRecursion", set_recursion_limit, NULL, RSRC_CONF,
3393 "maximum recursion depth of internal redirects and subrequests"),
3395 AP_INIT_TAKE1("ForceType", ap_set_string_slot_lower,
3396 (void *)APR_OFFSETOF(core_dir_config, mime_type), OR_FILEINFO,
3397 "a mime type that overrides other configured type"),
3398 AP_INIT_TAKE1("SetHandler", ap_set_string_slot_lower,
3399 (void *)APR_OFFSETOF(core_dir_config, handler), OR_FILEINFO,
3400 "a handler name that overrides any other configured handler"),
3401 AP_INIT_TAKE1("SetOutputFilter", ap_set_string_slot,
3402 (void *)APR_OFFSETOF(core_dir_config, output_filters), OR_FILEINFO,
3403 "filter (or ; delimited list of filters) to be run on the request content"),
3404 AP_INIT_TAKE1("SetInputFilter", ap_set_string_slot,
3405 (void *)APR_OFFSETOF(core_dir_config, input_filters), OR_FILEINFO,
3406 "filter (or ; delimited list of filters) to be run on the request body"),
3407 AP_INIT_ITERATE2("AddOutputFilterByType", add_ct_output_filters,
3408 (void *)APR_OFFSETOF(core_dir_config, ct_output_filters), OR_FILEINFO,
3409 "output filter name followed by one or more content-types"),
3410 AP_INIT_FLAG("AllowEncodedSlashes", set_allow2f, NULL, RSRC_CONF,
3411 "Allow URLs containing '/' encoded as '%2F'"),
3414 * These are default configuration directives that mpms can/should
3415 * pay attention to. If an mpm wishes to use these, they should
3416 * #defined them in mpm.h.
3418 #ifdef AP_MPM_WANT_SET_PIDFILE
3419 AP_INIT_TAKE1("PidFile", ap_mpm_set_pidfile, NULL, RSRC_CONF,
3420 "A file for logging the server process ID"),
3422 #ifdef AP_MPM_WANT_SET_SCOREBOARD
3423 AP_INIT_TAKE1("ScoreBoardFile", ap_mpm_set_scoreboard, NULL, RSRC_CONF,
3424 "A file for Apache to maintain runtime process management information"),
3426 #ifdef AP_MPM_WANT_SET_LOCKFILE
3427 AP_INIT_TAKE1("LockFile", ap_mpm_set_lockfile, NULL, RSRC_CONF,
3428 "The lockfile used when Apache needs to lock the accept() call (deprecated)"),
3430 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
3431 AP_INIT_TAKE1("MaxRequestsPerChild", ap_mpm_set_max_requests, NULL, RSRC_CONF,
3432 "Maximum number of requests a particular child serves before dying."),
3434 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
3435 AP_INIT_TAKE1("CoreDumpDirectory", ap_mpm_set_coredumpdir, NULL, RSRC_CONF,
3436 "The location of the directory Apache changes to before dumping core"),
3438 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
3439 AP_INIT_TAKE1("AcceptMutex", ap_mpm_set_accept_lock_mech, NULL, RSRC_CONF,
3440 AP_AVAILABLE_MUTEXES_STRING),
3442 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
3443 AP_INIT_TAKE1("MaxMemFree", ap_mpm_set_max_mem_free, NULL, RSRC_CONF,
3444 "Maximum number of 1k blocks a particular childs allocator may hold."),
3446 #ifdef AP_MPM_WANT_SET_STACKSIZE
3447 AP_INIT_TAKE1("ThreadStackSize", ap_mpm_set_thread_stacksize, NULL, RSRC_CONF,
3448 "Size in bytes of stack used by threads handling client connections"),
3450 #if AP_ENABLE_EXCEPTION_HOOK
3451 AP_INIT_TAKE1("EnableExceptionHook", ap_mpm_set_exception_hook, NULL, RSRC_CONF,
3452 "Controls whether exception hook may be called after a crash"),
3454 AP_INIT_TAKE1("TraceEnable", set_trace_enable, NULL, RSRC_CONF,
3455 "'on' (default), 'off' or 'extended' to trace request body content"),
3459 /*****************************************************************
3461 * Core handlers for various phases of server operation...
3464 AP_DECLARE_NONSTD(int) ap_core_translate(request_rec *r)
3466 void *sconf = r->server->module_config;
3467 core_server_config *conf = ap_get_module_config(sconf, &core_module);
3470 /* XXX this seems too specific, this should probably become
3471 * some general-case test
3474 return HTTP_FORBIDDEN;
3476 if (!r->uri || ((r->uri[0] != '/') && strcmp(r->uri, "*"))) {
3477 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3478 "Invalid URI in request %s", r->the_request);
3479 return HTTP_BAD_REQUEST;
3483 && !strncmp(r->uri, r->server->path, r->server->pathlen)
3484 && (r->server->path[r->server->pathlen - 1] == '/'
3485 || r->uri[r->server->pathlen] == '/'
3486 || r->uri[r->server->pathlen] == '\0'))
3488 /* skip all leading /'s (e.g. http://localhost///foo)
3489 * so we are looking at only the relative path.
3491 char *path = r->uri + r->server->pathlen;
3492 while (*path == '/') {
3495 if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3496 APR_FILEPATH_TRUENAME
3497 | APR_FILEPATH_SECUREROOT, r->pool))
3499 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3500 "Cannot map %s to file", r->the_request);
3501 return HTTP_FORBIDDEN;
3503 r->canonical_filename = r->filename;
3507 * Make sure that we do not mess up the translation by adding two
3508 * /'s in a row. This happens under windows when the document
3509 * root ends with a /
3511 /* skip all leading /'s (e.g. http://localhost///foo)
3512 * so we are looking at only the relative path.
3514 char *path = r->uri;
3515 while (*path == '/') {
3518 if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3519 APR_FILEPATH_TRUENAME
3520 | APR_FILEPATH_SECUREROOT, r->pool))
3522 ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3523 "Cannot map %s to file", r->the_request);
3524 return HTTP_FORBIDDEN;
3526 r->canonical_filename = r->filename;
3532 /*****************************************************************
3534 * Test the filesystem name through directory_walk and file_walk
3536 static int core_map_to_storage(request_rec *r)
3540 if ((access_status = ap_directory_walk(r))) {
3541 return access_status;
3544 if ((access_status = ap_file_walk(r))) {
3545 return access_status;
3552 static int do_nothing(request_rec *r) { return OK; }
3555 static int core_override_type(request_rec *r)
3557 core_dir_config *conf =
3558 (core_dir_config *)ap_get_module_config(r->per_dir_config,
3561 /* Check for overrides with ForceType / SetHandler
3563 if (conf->mime_type && strcmp(conf->mime_type, "none"))
3564 ap_set_content_type(r, (char*) conf->mime_type);
3566 if (conf->handler && strcmp(conf->handler, "none"))
3567 r->handler = conf->handler;
3569 /* Deal with the poor soul who is trying to force path_info to be
3570 * accepted within the core_handler, where they will let the subreq
3571 * address its contents. This is toggled by the user in the very
3572 * beginning of the fixup phase, so modules should override the user's
3573 * discretion in their own module fixup phase. It is tristate, if
3574 * the user doesn't specify, the result is 2 (which the module may
3575 * interpret to its own customary behavior.) It won't be touched
3576 * if the value is no longer undefined (2), so any module changing
3577 * the value prior to the fixup phase OVERRIDES the user's choice.
3579 if ((r->used_path_info == AP_REQ_DEFAULT_PATH_INFO)
3580 && (conf->accept_path_info != 3)) {
3581 r->used_path_info = conf->accept_path_info;
3587 static int default_handler(request_rec *r)
3589 conn_rec *c = r->connection;
3590 apr_bucket_brigade *bb;
3594 apr_file_t *fd = NULL;
3595 apr_status_t status;
3596 /* XXX if/when somebody writes a content-md5 filter we either need to
3597 * remove this support or coordinate when to use the filter vs.
3598 * when to use this code
3599 * The current choice of when to compute the md5 here matches the 1.3
3600 * support fairly closely (unlike 1.3, we don't handle computing md5
3601 * when the charset is translated).
3603 int bld_content_md5;
3605 d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3607 bld_content_md5 = (d->content_md5 & 1)
3608 && r->output_filters->frec->ftype != AP_FTYPE_RESOURCE;
3610 ap_allow_standard_methods(r, MERGE_ALLOW, M_GET, M_OPTIONS, M_POST, -1);
3612 /* If filters intend to consume the request body, they must
3613 * register an InputFilter to slurp the contents of the POST
3614 * data from the POST input stream. It no longer exists when
3615 * the output filters are invoked by the default handler.
3617 if ((errstatus = ap_discard_request_body(r)) != OK) {
3621 if (r->method_number == M_GET || r->method_number == M_POST) {
3622 if (r->finfo.filetype == 0) {
3623 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3624 "File does not exist: %s", r->filename);
3625 return HTTP_NOT_FOUND;
3628 /* Don't try to serve a dir. Some OSs do weird things with
3631 if (r->finfo.filetype == APR_DIR) {
3632 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3633 "Attempt to serve directory: %s", r->filename);
3634 return HTTP_NOT_FOUND;
3637 if ((r->used_path_info != AP_REQ_ACCEPT_PATH_INFO) &&
3638 r->path_info && *r->path_info)
3640 /* default to reject */
3641 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3642 "File does not exist: %s",
3643 apr_pstrcat(r->pool, r->filename, r->path_info, NULL));
3644 return HTTP_NOT_FOUND;
3647 /* We understood the (non-GET) method, but it might not be legal for
3648 this particular resource. Check to see if the 'deliver_script'
3649 flag is set. If so, then we go ahead and deliver the file since
3650 it isn't really content (only GET normally returns content).
3652 Note: based on logic further above, the only possible non-GET
3653 method at this point is POST. In the future, we should enable
3654 script delivery for all methods. */
3655 if (r->method_number != M_GET) {
3656 core_request_config *req_cfg;
3658 req_cfg = ap_get_module_config(r->request_config, &core_module);
3659 if (!req_cfg->deliver_script) {
3660 /* The flag hasn't been set for this request. Punt. */
3661 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3662 "This resource does not accept the %s method.",
3664 return HTTP_METHOD_NOT_ALLOWED;
3669 if ((status = apr_file_open(&fd, r->filename, APR_READ | APR_BINARY
3670 #if APR_HAS_SENDFILE
3671 | ((d->enable_sendfile == ENABLE_SENDFILE_OFF)
3672 ? 0 : APR_SENDFILE_ENABLED)
3674 , 0, r->pool)) != APR_SUCCESS) {
3675 ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
3676 "file permissions deny server access: %s", r->filename);
3677 return HTTP_FORBIDDEN;
3680 ap_update_mtime(r, r->finfo.mtime);
3681 ap_set_last_modified(r);
3683 apr_table_setn(r->headers_out, "Accept-Ranges", "bytes");
3684 ap_set_content_length(r, r->finfo.size);
3686 bb = apr_brigade_create(r->pool, c->bucket_alloc);
3688 if ((errstatus = ap_meets_conditions(r)) != OK) {
3690 r->status = errstatus;
3693 if (bld_content_md5) {
3694 apr_table_setn(r->headers_out, "Content-MD5",
3695 ap_md5digest(r->pool, fd));
3698 e = apr_brigade_insert_file(bb, fd, 0, r->finfo.size, r->pool);
3701 if (d->enable_mmap == ENABLE_MMAP_OFF) {
3702 (void)apr_bucket_file_enable_mmap(e, 0);
3707 e = apr_bucket_eos_create(c->bucket_alloc);
3708 APR_BRIGADE_INSERT_TAIL(bb, e);
3710 status = ap_pass_brigade(r->output_filters, bb);
3711 if (status == APR_SUCCESS
3712 || r->status != HTTP_OK
3717 /* no way to know what type of error occurred */
3718 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r,
3719 "default_handler: ap_pass_brigade returned %i",
3721 return HTTP_INTERNAL_SERVER_ERROR;
3724 else { /* unusual method (not GET or POST) */
3725 if (r->method_number == M_INVALID) {
3726 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3727 "Invalid method in request %s", r->the_request);
3728 return HTTP_NOT_IMPLEMENTED;
3731 if (r->method_number == M_OPTIONS) {
3732 return ap_send_http_options(r);
3734 return HTTP_METHOD_NOT_ALLOWED;
3738 /* Optional function coming from mod_logio, used for logging of output
3741 APR_OPTIONAL_FN_TYPE(ap_logio_add_bytes_out) *logio_add_bytes_out;
3742 APR_OPTIONAL_FN_TYPE(authz_some_auth_required) *authz_ap_some_auth_required;
3744 static int core_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
3746 logio_add_bytes_out = APR_RETRIEVE_OPTIONAL_FN(ap_logio_add_bytes_out);
3747 ident_lookup = APR_RETRIEVE_OPTIONAL_FN(ap_ident_lookup);
3748 authz_ap_some_auth_required = APR_RETRIEVE_OPTIONAL_FN(authz_some_auth_required);
3749 authn_ap_auth_type = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_type);
3750 authn_ap_auth_name = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_name);
3751 access_compat_ap_satisfies = APR_RETRIEVE_OPTIONAL_FN(access_compat_ap_satisfies);
3754 ap_setup_make_content_type(pconf);
3755 ap_setup_auth_internal(ptemp);
3759 static void core_insert_filter(request_rec *r)
3761 core_dir_config *conf = (core_dir_config *)
3762 ap_get_module_config(r->per_dir_config,
3764 const char *filter, *filters = conf->output_filters;
3767 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3768 ap_add_output_filter(filter, NULL, r, r->connection);
3772 filters = conf->input_filters;
3774 while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3775 ap_add_input_filter(filter, NULL, r, r->connection);
3780 static apr_size_t num_request_notes = AP_NUM_STD_NOTES;
3782 static apr_status_t reset_request_notes(void *dummy)
3784 num_request_notes = AP_NUM_STD_NOTES;
3788 AP_DECLARE(apr_size_t) ap_register_request_note(void)
3790 apr_pool_cleanup_register(apr_hook_global_pool, NULL, reset_request_notes,
3791 apr_pool_cleanup_null);
3792 return num_request_notes++;
3795 AP_DECLARE(void **) ap_get_request_note(request_rec *r, apr_size_t note_num)
3797 core_request_config *req_cfg;
3799 if (note_num >= num_request_notes) {
3803 req_cfg = (core_request_config *)
3804 ap_get_module_config(r->request_config, &core_module);
3810 return &(req_cfg->notes[note_num]);
3813 static int core_create_req(request_rec *r)
3815 /* Alloc the config struct and the array of request notes in
3816 * a single block for efficiency
3818 core_request_config *req_cfg;
3820 req_cfg = apr_pcalloc(r->pool, sizeof(core_request_config) +
3821 sizeof(void *) * num_request_notes);
3822 req_cfg->notes = (void **)((char *)req_cfg + sizeof(core_request_config));
3824 /* ### temporarily enable script delivery as the default */
3825 req_cfg->deliver_script = 1;
3828 core_request_config *main_req_cfg = (core_request_config *)
3829 ap_get_module_config(r->main->request_config, &core_module);
3830 req_cfg->bb = main_req_cfg->bb;
3833 req_cfg->bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
3836 ap_set_module_config(r->request_config, &core_module, req_cfg);
3841 static int core_create_proxy_req(request_rec *r, request_rec *pr)
3843 return core_create_req(pr);
3846 static conn_rec *core_create_conn(apr_pool_t *ptrans, server_rec *server,
3847 apr_socket_t *csd, long id, void *sbh,
3848 apr_bucket_alloc_t *alloc)
3851 conn_rec *c = (conn_rec *) apr_pcalloc(ptrans, sizeof(conn_rec));
3854 (void)ap_update_child_status(c->sbh, SERVER_BUSY_READ, (request_rec *)NULL);
3856 /* Got a connection structure, so initialize what fields we can
3857 * (the rest are zeroed out by pcalloc).
3859 c->conn_config = ap_create_conn_config(ptrans);
3860 c->notes = apr_table_make(ptrans, 5);
3863 if ((rv = apr_socket_addr_get(&c->local_addr, APR_LOCAL, csd))
3865 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3866 "apr_socket_addr_get(APR_LOCAL)");
3867 apr_socket_close(csd);
3871 apr_sockaddr_ip_get(&c->local_ip, c->local_addr);
3872 if ((rv = apr_socket_addr_get(&c->remote_addr, APR_REMOTE, csd))
3874 ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3875 "apr_socket_addr_get(APR_REMOTE)");
3876 apr_socket_close(csd);
3880 apr_sockaddr_ip_get(&c->remote_ip, c->remote_addr);
3881 c->base_server = server;
3884 c->bucket_alloc = alloc;
3886 c->cs = (conn_state_t *)apr_pcalloc(ptrans, sizeof(conn_state_t));
3887 APR_RING_INIT(&(c->cs->timeout_list), conn_state_t, timeout_list);
3888 c->cs->expiration_time = 0;
3889 c->cs->state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
3892 c->cs->bucket_alloc = alloc;
3893 c->clogging_input_filters = 0;
3898 static int core_pre_connection(conn_rec *c, void *csd)
3900 core_net_rec *net = apr_palloc(c->pool, sizeof(*net));
3903 #ifdef AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
3904 /* The Nagle algorithm says that we should delay sending partial
3905 * packets in hopes of getting more data. We don't want to do
3906 * this; we are not telnet. There are bad interactions between
3907 * persistent connections and Nagle's algorithm that have very severe
3908 * performance penalties. (Failing to disable Nagle is not much of a
3909 * problem with simple HTTP.)
3911 rv = apr_socket_opt_set(csd, APR_TCP_NODELAY, 1);
3912 if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
3913 /* expected cause is that the client disconnected already,
3914 * hence the debug level
3916 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c,
3917 "apr_socket_opt_set(APR_TCP_NODELAY)");
3921 /* The core filter requires the timeout mode to be set, which
3922 * incidentally sets the socket to be nonblocking. If this
3923 * is not initialized correctly, Linux - for example - will
3924 * be initially blocking, while Solaris will be non blocking
3925 * and any initial read will fail.
3927 rv = apr_socket_timeout_set(csd, c->base_server->timeout);
3928 if (rv != APR_SUCCESS) {
3929 /* expected cause is that the client disconnected already */
3930 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c,
3931 "apr_socket_timeout_set");
3936 net->out_ctx = NULL;
3937 net->client_socket = csd;
3939 ap_set_module_config(net->c->conn_config, &core_module, csd);
3940 ap_add_input_filter_handle(ap_core_input_filter_handle, net, NULL, net->c);
3941 ap_add_output_filter_handle(ap_core_output_filter_handle, net, NULL, net->c);
3945 static void register_hooks(apr_pool_t *p)
3947 /* create_connection and install_transport_filters are
3948 * hooks that should always be APR_HOOK_REALLY_LAST to give other
3949 * modules the opportunity to install alternate network transports
3950 * and stop other functions from being run.
3952 ap_hook_create_connection(core_create_conn, NULL, NULL,
3953 APR_HOOK_REALLY_LAST);
3954 ap_hook_pre_connection(core_pre_connection, NULL, NULL,
3955 APR_HOOK_REALLY_LAST);
3957 ap_hook_post_config(core_post_config,NULL,NULL,APR_HOOK_REALLY_FIRST);
3958 ap_hook_translate_name(ap_core_translate,NULL,NULL,APR_HOOK_REALLY_LAST);
3959 ap_hook_map_to_storage(core_map_to_storage,NULL,NULL,APR_HOOK_REALLY_LAST);
3960 ap_hook_open_logs(ap_open_logs,NULL,NULL,APR_HOOK_REALLY_FIRST);
3961 ap_hook_child_init(ap_logs_child_init,NULL,NULL,APR_HOOK_MIDDLE);
3962 ap_hook_handler(default_handler,NULL,NULL,APR_HOOK_REALLY_LAST);
3963 /* FIXME: I suspect we can eliminate the need for these do_nothings - Ben */
3964 ap_hook_type_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
3965 ap_hook_fixups(core_override_type,NULL,NULL,APR_HOOK_REALLY_FIRST);
3966 ap_hook_create_request(core_create_req, NULL, NULL, APR_HOOK_MIDDLE);
3967 APR_OPTIONAL_HOOK(proxy, create_req, core_create_proxy_req, NULL, NULL,
3969 ap_hook_pre_mpm(ap_create_scoreboard, NULL, NULL, APR_HOOK_MIDDLE);
3971 /* register the core's insert_filter hook and register core-provided
3974 ap_hook_insert_filter(core_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
3976 ap_core_input_filter_handle =
3977 ap_register_input_filter("CORE_IN", ap_core_input_filter,
3978 NULL, AP_FTYPE_NETWORK);
3979 ap_content_length_filter_handle =
3980 ap_register_output_filter("CONTENT_LENGTH", ap_content_length_filter,
3981 NULL, AP_FTYPE_PROTOCOL);
3982 ap_core_output_filter_handle =
3983 ap_register_output_filter("CORE", ap_core_output_filter,
3984 NULL, AP_FTYPE_NETWORK);
3985 ap_subreq_core_filter_handle =
3986 ap_register_output_filter("SUBREQ_CORE", ap_sub_req_output_filter,
3987 NULL, AP_FTYPE_CONTENT_SET);
3989 ap_register_output_filter("OLD_WRITE", ap_old_write_filter,
3990 NULL, AP_FTYPE_RESOURCE - 10);
3993 AP_DECLARE_DATA module core_module = {
3994 STANDARD20_MODULE_STUFF,
3995 create_core_dir_config, /* create per-directory config structure */
3996 merge_core_dir_configs, /* merge per-directory config structures */
3997 create_core_server_config, /* create per-server config structure */
3998 merge_core_server_configs, /* merge per-server config structures */
3999 core_cmds, /* command apr_table_t */
4000 register_hooks /* register hooks */