]> granicus.if.org Git - apache/blob - server/core.c
* docs/manual/mod/mpm_common.xml: s/depreciated/deprecated/
[apache] / server / core.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "apr.h"
18 #include "apr_strings.h"
19 #include "apr_lib.h"
20 #include "apr_fnmatch.h"
21 #include "apr_hash.h"
22 #include "apr_thread_proc.h"    /* for RLIMIT stuff */
23 #include "apr_hooks.h"
24
25 #define APR_WANT_IOVEC
26 #define APR_WANT_STRFUNC
27 #define APR_WANT_MEMFUNC
28 #include "apr_want.h"
29
30 #define CORE_PRIVATE
31 #include "ap_config.h"
32 #include "httpd.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... */
39 #include "http_log.h"
40 #include "util_md5.h"
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"
46 #include "mpm.h"
47 #include "mpm_common.h"
48 #include "scoreboard.h"
49 #include "mod_core.h"
50 #include "mod_proxy.h"
51 #include "ap_listen.h"
52
53 #include "mod_so.h" /* for ap_find_loaded_module_symbol */
54
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)
58
59 /* LimitXMLRequestBody handling */
60 #define AP_LIMIT_UNSET                  ((long) -1)
61 #define AP_DEFAULT_LIMIT_XML_BODY       ((size_t)1000000)
62
63 #define AP_MIN_SENDFILE_BYTES           (256)
64
65 /* maximum include nesting level */
66 #ifndef AP_MAX_INCLUDE_DEPTH
67 #define AP_MAX_INCLUDE_DEPTH            (128)
68 #endif
69
70 APR_HOOK_STRUCT(
71     APR_HOOK_LINK(get_mgmt_items)
72 )
73
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)
77
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.
81  *
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.
86  *
87  * This file could almost be mod_core.c, except for the stuff which affects
88  * the http_conf_globals.
89  */
90
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;
96
97 /* magic pointer for ErrorDocument xxx "default" */
98 static char errordocument_default;
99
100 static void *create_core_dir_config(apr_pool_t *a, char *dir)
101 {
102     core_dir_config *conf;
103
104     conf = (core_dir_config *)apr_pcalloc(a, sizeof(core_dir_config));
105
106     /* conf->r and conf->d[_*] are initialized by dirsection() or left NULL */
107
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
112                           | OPT_MULTI;
113
114     conf->content_md5 = 2;
115     conf->accept_path_info = 3;
116
117     conf->use_canonical_name = USE_CANONICAL_NAME_UNSET;
118     conf->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_UNSET;
119
120     conf->hostname_lookups = HOSTNAME_LOOKUP_UNSET;
121
122 #ifdef RLIMIT_CPU
123     conf->limit_cpu = NULL;
124 #endif
125 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
126     conf->limit_mem = NULL;
127 #endif
128 #ifdef RLIMIT_NPROC
129     conf->limit_nproc = NULL;
130 #endif
131
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 *));
135
136     conf->server_signature = srv_sig_unset;
137
138     conf->add_default_charset = ADD_DEFAULT_CHARSET_UNSET;
139     conf->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
140
141     /* Overriding all negotiation
142      */
143     conf->mime_type = NULL;
144     conf->handler = NULL;
145     conf->output_filters = NULL;
146     conf->input_filters = NULL;
147
148     /*
149      * Flag for use of inodes in ETags.
150      */
151     conf->etag_bits = ETAG_UNSET;
152     conf->etag_add = ETAG_UNSET;
153     conf->etag_remove = ETAG_UNSET;
154
155     conf->enable_mmap = ENABLE_MMAP_UNSET;
156     conf->enable_sendfile = ENABLE_SENDFILE_UNSET;
157     conf->allow_encoded_slashes = 0;
158
159     return (void *)conf;
160 }
161
162 /*
163  * Overlay one hash table of ct_output_filters onto another
164  */
165 static void *merge_ct_filters(apr_pool_t *p,
166                               const void *key,
167                               apr_ssize_t klen,
168                               const void *overlay_val,
169                               const void *base_val,
170                               const void *data)
171 {
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;
175
176     cur = NULL;
177
178     while (overlay_info) {
179         ap_filter_rec_t *new;
180
181         new = apr_pcalloc(p, sizeof(ap_filter_rec_t));
182         new->name = apr_pstrdup(p, overlay_info->name);
183         new->next = cur;
184         cur = new;
185         overlay_info = overlay_info->next;
186     }
187
188     while (base_info) {
189         ap_filter_rec_t *f;
190         int found = 0;
191
192         /* We can't have dups. */
193         f = cur;
194         while (f) {
195             if (!strcasecmp(base_info->name, f->name)) {
196                 found = 1;
197                 break;
198             }
199
200             f = f->next;
201         }
202
203         if (!found) {
204             f = apr_pcalloc(p, sizeof(ap_filter_rec_t));
205             f->name = apr_pstrdup(p, base_info->name);
206             f->next = cur;
207             cur = f;
208         }
209
210         base_info = base_info->next;
211     }
212
213     return cur;
214 }
215
216 static void *merge_core_dir_configs(apr_pool_t *a, void *basev, void *newv)
217 {
218     core_dir_config *base = (core_dir_config *)basev;
219     core_dir_config *new = (core_dir_config *)newv;
220     core_dir_config *conf;
221     int i;
222
223     /* Create this conf by duplicating the base, replacing elements
224      * (or creating copies for merging) where new-> values exist.
225      */
226     conf = (core_dir_config *)apr_palloc(a, sizeof(core_dir_config));
227     memcpy(conf, base, sizeof(core_dir_config));
228
229     conf->d = new->d;
230     conf->d_is_fnmatch = new->d_is_fnmatch;
231     conf->d_components = new->d_components;
232     conf->r = new->r;
233
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
237          */
238         conf->opts_add = (conf->opts_add & ~new->opts_remove) | new->opts_add;
239         conf->opts_remove = (conf->opts_remove & ~new->opts_add)
240                             | new->opts_remove;
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;
244         }
245     }
246     else {
247         /* otherwise we just copy, because an explicit opts setting
248          * overrides all earlier +/- modifiers
249          */
250         conf->opts = new->opts;
251         conf->opts_add = new->opts_add;
252         conf->opts_remove = new->opts_remove;
253     }
254
255     if (!(new->override & OR_UNSET)) {
256         conf->override = new->override;
257     }
258
259     if (!(new->override_opts & OPT_UNSET)) {
260         conf->override_opts = new->override_opts;
261     }
262
263     if (new->ap_default_type) {
264         conf->ap_default_type = new->ap_default_type;
265     }
266
267     if (conf->response_code_strings == NULL) {
268         conf->response_code_strings = new->response_code_strings;
269     }
270     else if (new->response_code_strings != NULL) {
271         /* If we merge, the merge-result must have it's own array
272          */
273         conf->response_code_strings = apr_palloc(a,
274             sizeof(*conf->response_code_strings) * RESPONSE_CODES);
275         memcpy(conf->response_code_strings, base->response_code_strings,
276                sizeof(*conf->response_code_strings) * RESPONSE_CODES);
277
278         for (i = 0; i < RESPONSE_CODES; ++i) {
279             if (new->response_code_strings[i] != NULL) {
280                 conf->response_code_strings[i] = new->response_code_strings[i];
281             }
282         }
283     }
284     /* Otherwise we simply use the base->response_code_strings array
285      */
286
287     if (new->hostname_lookups != HOSTNAME_LOOKUP_UNSET) {
288         conf->hostname_lookups = new->hostname_lookups;
289     }
290
291     if ((new->content_md5 & 2) == 0) {
292         conf->content_md5 = new->content_md5;
293     }
294
295     if (new->accept_path_info != 3) {
296         conf->accept_path_info = new->accept_path_info;
297     }
298
299     if (new->use_canonical_name != USE_CANONICAL_NAME_UNSET) {
300         conf->use_canonical_name = new->use_canonical_name;
301     }
302
303     if (new->use_canonical_phys_port != USE_CANONICAL_PHYS_PORT_UNSET) {
304         conf->use_canonical_phys_port = new->use_canonical_phys_port;
305     }
306
307 #ifdef RLIMIT_CPU
308     if (new->limit_cpu) {
309         conf->limit_cpu = new->limit_cpu;
310     }
311 #endif
312
313 #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
314     if (new->limit_mem) {
315         conf->limit_mem = new->limit_mem;
316     }
317 #endif
318
319 #ifdef RLIMIT_NPROC
320     if (new->limit_nproc) {
321         conf->limit_nproc = new->limit_nproc;
322     }
323 #endif
324
325     if (new->limit_req_body != AP_LIMIT_REQ_BODY_UNSET) {
326         conf->limit_req_body = new->limit_req_body;
327     }
328
329     if (new->limit_xml_body != AP_LIMIT_UNSET)
330         conf->limit_xml_body = new->limit_xml_body;
331     else
332         conf->limit_xml_body = base->limit_xml_body;
333
334     if (!conf->sec_file) {
335         conf->sec_file = new->sec_file;
336     }
337     else if (new->sec_file) {
338         /* If we merge, the merge-result must have it's own array
339          */
340         conf->sec_file = apr_array_append(a, base->sec_file, new->sec_file);
341     }
342     /* Otherwise we simply use the base->sec_file array
343      */
344
345     if (new->server_signature != srv_sig_unset) {
346         conf->server_signature = new->server_signature;
347     }
348
349     if (new->add_default_charset != ADD_DEFAULT_CHARSET_UNSET) {
350         conf->add_default_charset = new->add_default_charset;
351         conf->add_default_charset_name = new->add_default_charset_name;
352     }
353
354     /* Overriding all negotiation
355      */
356     if (new->mime_type) {
357         conf->mime_type = new->mime_type;
358     }
359
360     if (new->handler) {
361         conf->handler = new->handler;
362     }
363
364     if (new->output_filters) {
365         conf->output_filters = new->output_filters;
366     }
367
368     if (new->input_filters) {
369         conf->input_filters = new->input_filters;
370     }
371
372     if (conf->ct_output_filters && new->ct_output_filters) {
373         conf->ct_output_filters = apr_hash_merge(a,
374                                                  new->ct_output_filters,
375                                                  conf->ct_output_filters,
376                                                  merge_ct_filters,
377                                                  NULL);
378     }
379     else if (new->ct_output_filters) {
380         conf->ct_output_filters = apr_hash_copy(a, new->ct_output_filters);
381     }
382     else if (conf->ct_output_filters) {
383         /* That memcpy above isn't enough. */
384         conf->ct_output_filters = apr_hash_copy(a, base->ct_output_filters);
385     }
386
387     /*
388      * Now merge the setting of the FileETag directive.
389      */
390     if (new->etag_bits == ETAG_UNSET) {
391         conf->etag_add =
392             (conf->etag_add & (~ new->etag_remove)) | new->etag_add;
393         conf->etag_remove =
394             (conf->opts_remove & (~ new->etag_add)) | new->etag_remove;
395         conf->etag_bits =
396             (conf->etag_bits & (~ conf->etag_remove)) | conf->etag_add;
397     }
398     else {
399         conf->etag_bits = new->etag_bits;
400         conf->etag_add = new->etag_add;
401         conf->etag_remove = new->etag_remove;
402     }
403
404     if (conf->etag_bits != ETAG_NONE) {
405         conf->etag_bits &= (~ ETAG_NONE);
406     }
407
408     if (new->enable_mmap != ENABLE_MMAP_UNSET) {
409         conf->enable_mmap = new->enable_mmap;
410     }
411
412     if (new->enable_sendfile != ENABLE_SENDFILE_UNSET) {
413         conf->enable_sendfile = new->enable_sendfile;
414     }
415
416     conf->allow_encoded_slashes = new->allow_encoded_slashes;
417
418     return (void*)conf;
419 }
420
421 static void *create_core_server_config(apr_pool_t *a, server_rec *s)
422 {
423     core_server_config *conf;
424     int is_virtual = s->is_virtual;
425
426     conf = (core_server_config *)apr_pcalloc(a, sizeof(core_server_config));
427
428 #ifdef GPROF
429     conf->gprof_dir = NULL;
430 #endif
431
432     conf->access_name = is_virtual ? NULL : DEFAULT_ACCESS_FNAME;
433     conf->ap_document_root = is_virtual ? NULL : DOCUMENT_LOCATION;
434     conf->sec_dir = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
435     conf->sec_url = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
436
437     /* recursion stopper */
438     conf->redirect_limit = 0; /* 0 == unset */
439     conf->subreq_limit = 0;
440
441     conf->protocol = NULL;
442     conf->accf_map = apr_table_make(a, 5);
443
444 #ifdef APR_TCP_DEFER_ACCEPT
445     apr_table_set(conf->accf_map, "http", "data");
446     apr_table_set(conf->accf_map, "https", "data");
447 #endif
448
449 #if APR_HAS_SO_ACCEPTFILTER
450 #ifndef ACCEPT_FILTER_NAME
451 #define ACCEPT_FILTER_NAME "httpready"
452 #ifdef __FreeBSD_version
453 #if __FreeBSD_version < 411000 /* httpready broken before 4.1.1 */
454 #undef ACCEPT_FILTER_NAME
455 #define ACCEPT_FILTER_NAME "dataready"
456 #endif
457 #endif
458 #endif
459     apr_table_set(conf->accf_map, "http", ACCEPT_FILTER_NAME);
460     apr_table_set(conf->accf_map, "https", "dataready");
461 #endif
462
463     conf->trace_enable = AP_TRACE_UNSET;
464
465     return (void *)conf;
466 }
467
468 static void *merge_core_server_configs(apr_pool_t *p, void *basev, void *virtv)
469 {
470     core_server_config *base = (core_server_config *)basev;
471     core_server_config *virt = (core_server_config *)virtv;
472     core_server_config *conf;
473
474     conf = (core_server_config *)apr_palloc(p, sizeof(core_server_config));
475     memcpy(conf, virt, sizeof(core_server_config));
476
477     if (!conf->access_name) {
478         conf->access_name = base->access_name;
479     }
480
481     if (!conf->ap_document_root) {
482         conf->ap_document_root = base->ap_document_root;
483     }
484
485     if (!conf->protocol) {
486         conf->protocol = base->protocol;
487     }
488
489     conf->sec_dir = apr_array_append(p, base->sec_dir, virt->sec_dir);
490     conf->sec_url = apr_array_append(p, base->sec_url, virt->sec_url);
491
492     conf->redirect_limit = virt->redirect_limit
493                            ? virt->redirect_limit
494                            : base->redirect_limit;
495
496     conf->subreq_limit = virt->subreq_limit
497                          ? virt->subreq_limit
498                          : base->subreq_limit;
499
500     conf->trace_enable = (virt->trace_enable != AP_TRACE_UNSET)
501                          ? virt->trace_enable
502                          : base->trace_enable;
503
504     return conf;
505 }
506
507 /* Add per-directory configuration entry (for <directory> section);
508  * these are part of the core server config.
509  */
510
511 AP_CORE_DECLARE(void) ap_add_per_dir_conf(server_rec *s, void *dir_config)
512 {
513     core_server_config *sconf = ap_get_module_config(s->module_config,
514                                                      &core_module);
515     void **new_space = (void **)apr_array_push(sconf->sec_dir);
516
517     *new_space = dir_config;
518 }
519
520 AP_CORE_DECLARE(void) ap_add_per_url_conf(server_rec *s, void *url_config)
521 {
522     core_server_config *sconf = ap_get_module_config(s->module_config,
523                                                      &core_module);
524     void **new_space = (void **)apr_array_push(sconf->sec_url);
525
526     *new_space = url_config;
527 }
528
529 AP_CORE_DECLARE(void) ap_add_file_conf(core_dir_config *conf, void *url_config)
530 {
531     void **new_space = (void **)apr_array_push(conf->sec_file);
532
533     *new_space = url_config;
534 }
535
536 /* We need to do a stable sort, qsort isn't stable.  So to make it stable
537  * we'll be maintaining the original index into the list, and using it
538  * as the minor key during sorting.  The major key is the number of
539  * components (where the root component is zero).
540  */
541 struct reorder_sort_rec {
542     ap_conf_vector_t *elt;
543     int orig_index;
544 };
545
546 static int reorder_sorter(const void *va, const void *vb)
547 {
548     const struct reorder_sort_rec *a = va;
549     const struct reorder_sort_rec *b = vb;
550     core_dir_config *core_a;
551     core_dir_config *core_b;
552
553     core_a = ap_get_module_config(a->elt, &core_module);
554     core_b = ap_get_module_config(b->elt, &core_module);
555
556     /* a regex always sorts after a non-regex
557      */
558     if (!core_a->r && core_b->r) {
559         return -1;
560     }
561     else if (core_a->r && !core_b->r) {
562         return 1;
563     }
564
565     /* we always sort next by the number of components
566      */
567     if (core_a->d_components < core_b->d_components) {
568         return -1;
569     }
570     else if (core_a->d_components > core_b->d_components) {
571         return 1;
572     }
573
574     /* They have the same number of components, we now have to compare
575      * the minor key to maintain the original order (from the config.)
576      */
577     return a->orig_index - b->orig_index;
578 }
579
580 void ap_core_reorder_directories(apr_pool_t *p, server_rec *s)
581 {
582     core_server_config *sconf;
583     apr_array_header_t *sec_dir;
584     struct reorder_sort_rec *sortbin;
585     int nelts;
586     ap_conf_vector_t **elts;
587     int i;
588     apr_pool_t *tmp;
589
590     sconf = ap_get_module_config(s->module_config, &core_module);
591     sec_dir = sconf->sec_dir;
592     nelts = sec_dir->nelts;
593     elts = (ap_conf_vector_t **)sec_dir->elts;
594
595     if (!nelts) {
596         /* simple case of already being sorted... */
597         /* We're not checking this condition to be fast... we're checking
598          * it to avoid trying to palloc zero bytes, which can trigger some
599          * memory debuggers to barf
600          */
601         return;
602     }
603
604     /* we have to allocate tmp space to do a stable sort */
605     apr_pool_create(&tmp, p);
606     sortbin = apr_palloc(tmp, sec_dir->nelts * sizeof(*sortbin));
607     for (i = 0; i < nelts; ++i) {
608         sortbin[i].orig_index = i;
609         sortbin[i].elt = elts[i];
610     }
611
612     qsort(sortbin, nelts, sizeof(*sortbin), reorder_sorter);
613
614     /* and now copy back to the original array */
615     for (i = 0; i < nelts; ++i) {
616         elts[i] = sortbin[i].elt;
617     }
618
619     apr_pool_destroy(tmp);
620 }
621
622 /*****************************************************************
623  *
624  * There are some elements of the core config structures in which
625  * other modules have a legitimate interest (this is ugly, but necessary
626  * to preserve NCSA back-compatibility).  So, we have a bunch of accessors
627  * here...
628  */
629
630 AP_DECLARE(int) ap_allow_options(request_rec *r)
631 {
632     core_dir_config *conf =
633       (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
634
635     return conf->opts;
636 }
637
638 AP_DECLARE(int) ap_allow_overrides(request_rec *r)
639 {
640     core_dir_config *conf;
641     conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
642                                                    &core_module);
643
644     return conf->override;
645 }
646
647 /*
648  * Optional function coming from mod_ident, used for looking up ident user
649  */
650 static APR_OPTIONAL_FN_TYPE(authn_ap_auth_type) *authn_ap_auth_type;
651
652 AP_DECLARE(const char *) ap_auth_type(request_rec *r)
653 {
654     if (authn_ap_auth_type) {
655         return authn_ap_auth_type(r);
656     }
657     return NULL;
658 }
659
660 /*
661  * Optional function coming from mod_ident, used for looking up ident user
662  */
663 static APR_OPTIONAL_FN_TYPE(authn_ap_auth_name) *authn_ap_auth_name;
664
665 AP_DECLARE(const char *) ap_auth_name(request_rec *r)
666 {
667     if (authn_ap_auth_name) {
668         return authn_ap_auth_name(r);
669     }
670     return NULL;
671 }
672
673 AP_DECLARE(const char *) ap_default_type(request_rec *r)
674 {
675     core_dir_config *conf;
676
677     conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
678                                                    &core_module);
679
680     return conf->ap_default_type
681                ? conf->ap_default_type
682                : DEFAULT_CONTENT_TYPE;
683 }
684
685 AP_DECLARE(const char *) ap_document_root(request_rec *r) /* Don't use this! */
686 {
687     core_server_config *conf;
688
689     conf = (core_server_config *)ap_get_module_config(r->server->module_config,
690                                                       &core_module);
691
692     return conf->ap_document_root;
693 }
694
695 /* Should probably just get rid of this... the only code that cares is
696  * part of the core anyway (and in fact, it isn't publicised to other
697  * modules).
698  */
699
700 char *ap_response_code_string(request_rec *r, int error_index)
701 {
702     core_dir_config *dirconf;
703     core_request_config *reqconf;
704
705     /* check for string registered via ap_custom_response() first */
706     reqconf = (core_request_config *)ap_get_module_config(r->request_config,
707                                                           &core_module);
708     if (reqconf->response_code_strings != NULL &&
709         reqconf->response_code_strings[error_index] != NULL) {
710         return reqconf->response_code_strings[error_index];
711     }
712
713     /* check for string specified via ErrorDocument */
714     dirconf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
715                                                       &core_module);
716
717     if (dirconf->response_code_strings == NULL) {
718         return NULL;
719     }
720
721     if (dirconf->response_code_strings[error_index] == &errordocument_default) {
722         return NULL;
723     }
724
725     return dirconf->response_code_strings[error_index];
726 }
727
728
729 /* Code from Harald Hanche-Olsen <hanche@imf.unit.no> */
730 static APR_INLINE void do_double_reverse (conn_rec *conn)
731 {
732     apr_sockaddr_t *sa;
733     apr_status_t rv;
734
735     if (conn->double_reverse) {
736         /* already done */
737         return;
738     }
739
740     if (conn->remote_host == NULL || conn->remote_host[0] == '\0') {
741         /* single reverse failed, so don't bother */
742         conn->double_reverse = -1;
743         return;
744     }
745
746     rv = apr_sockaddr_info_get(&sa, conn->remote_host, APR_UNSPEC, 0, 0, conn->pool);
747     if (rv == APR_SUCCESS) {
748         while (sa) {
749             if (apr_sockaddr_equal(sa, conn->remote_addr)) {
750                 conn->double_reverse = 1;
751                 return;
752             }
753
754             sa = sa->next;
755         }
756     }
757
758     conn->double_reverse = -1;
759 }
760
761 AP_DECLARE(const char *) ap_get_remote_host(conn_rec *conn, void *dir_config,
762                                             int type, int *str_is_ip)
763 {
764     int hostname_lookups;
765     int ignored_str_is_ip;
766
767     if (!str_is_ip) { /* caller doesn't want to know */
768         str_is_ip = &ignored_str_is_ip;
769     }
770     *str_is_ip = 0;
771
772     /* If we haven't checked the host name, and we want to */
773     if (dir_config) {
774         hostname_lookups =
775             ((core_dir_config *)ap_get_module_config(dir_config, &core_module))
776             ->hostname_lookups;
777
778         if (hostname_lookups == HOSTNAME_LOOKUP_UNSET) {
779             hostname_lookups = HOSTNAME_LOOKUP_OFF;
780         }
781     }
782     else {
783         /* the default */
784         hostname_lookups = HOSTNAME_LOOKUP_OFF;
785     }
786
787     if (type != REMOTE_NOLOOKUP
788         && conn->remote_host == NULL
789         && (type == REMOTE_DOUBLE_REV
790         || hostname_lookups != HOSTNAME_LOOKUP_OFF)) {
791
792         if (apr_getnameinfo(&conn->remote_host, conn->remote_addr, 0)
793             == APR_SUCCESS) {
794             ap_str_tolower(conn->remote_host);
795
796             if (hostname_lookups == HOSTNAME_LOOKUP_DOUBLE) {
797                 do_double_reverse(conn);
798                 if (conn->double_reverse != 1) {
799                     conn->remote_host = NULL;
800                 }
801             }
802         }
803
804         /* if failed, set it to the NULL string to indicate error */
805         if (conn->remote_host == NULL) {
806             conn->remote_host = "";
807         }
808     }
809
810     if (type == REMOTE_DOUBLE_REV) {
811         do_double_reverse(conn);
812         if (conn->double_reverse == -1) {
813             return NULL;
814         }
815     }
816
817     /*
818      * Return the desired information; either the remote DNS name, if found,
819      * or either NULL (if the hostname was requested) or the IP address
820      * (if any identifier was requested).
821      */
822     if (conn->remote_host != NULL && conn->remote_host[0] != '\0') {
823         return conn->remote_host;
824     }
825     else {
826         if (type == REMOTE_HOST || type == REMOTE_DOUBLE_REV) {
827             return NULL;
828         }
829         else {
830             *str_is_ip = 1;
831             return conn->remote_ip;
832         }
833     }
834 }
835
836 /*
837  * Optional function coming from mod_ident, used for looking up ident user
838  */
839 static APR_OPTIONAL_FN_TYPE(ap_ident_lookup) *ident_lookup;
840
841 AP_DECLARE(const char *) ap_get_remote_logname(request_rec *r)
842 {
843     if (r->connection->remote_logname != NULL) {
844         return r->connection->remote_logname;
845     }
846
847     if (ident_lookup) {
848         return ident_lookup(r);
849     }
850
851     return NULL;
852 }
853
854 /* There are two options regarding what the "name" of a server is.  The
855  * "canonical" name as defined by ServerName and Port, or the "client's
856  * name" as supplied by a possible Host: header or full URI.
857  *
858  * The DNS option to UseCanonicalName causes this routine to do a
859  * reverse lookup on the local IP address of the connection and use
860  * that for the ServerName. This makes its value more reliable while
861  * at the same time allowing Demon's magic virtual hosting to work.
862  * The assumption is that DNS lookups are sufficiently quick...
863  * -- fanf 1998-10-03
864  */
865 AP_DECLARE(const char *) ap_get_server_name(request_rec *r)
866 {
867     conn_rec *conn = r->connection;
868     core_dir_config *d;
869     const char *retval;
870
871     d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
872                                                 &core_module);
873
874     switch (d->use_canonical_name) {
875         case USE_CANONICAL_NAME_ON:
876             retval = r->server->server_hostname;
877             break;
878         case USE_CANONICAL_NAME_DNS:
879             if (conn->local_host == NULL) {
880                 if (apr_getnameinfo(&conn->local_host,
881                                 conn->local_addr, 0) != APR_SUCCESS)
882                     conn->local_host = apr_pstrdup(conn->pool,
883                                                r->server->server_hostname);
884                 else {
885                     ap_str_tolower(conn->local_host);
886                 }
887             }
888             retval = conn->local_host;
889             break;
890         case USE_CANONICAL_NAME_OFF:
891         case USE_CANONICAL_NAME_UNSET:
892             retval = r->hostname ? r->hostname : r->server->server_hostname;
893             break;
894         default:
895             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
896                          "ap_get_server_name: Invalid UCN Option somehow");
897             retval = "localhost";
898             break;
899     }
900     return retval;
901 }
902
903 /*
904  * Get the current server name from the request for the purposes
905  * of using in a URL.  If the server name is an IPv6 literal
906  * address, it will be returned in URL format (e.g., "[fe80::1]").
907  */
908 static const char *get_server_name_for_url(request_rec *r)
909 {
910     const char *plain_server_name = ap_get_server_name(r);
911
912 #if APR_HAVE_IPV6
913     if (ap_strchr_c(plain_server_name, ':')) { /* IPv6 literal? */
914         return apr_psprintf(r->pool, "[%s]", plain_server_name);
915     }
916 #endif
917     return plain_server_name;
918 }
919
920 AP_DECLARE(apr_port_t) ap_get_server_port(const request_rec *r)
921 {
922     apr_port_t port;
923     core_dir_config *d =
924       (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
925
926     switch (d->use_canonical_name) {
927         case USE_CANONICAL_NAME_OFF:
928         case USE_CANONICAL_NAME_DNS:
929         case USE_CANONICAL_NAME_UNSET:
930             if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
931                 port = r->parsed_uri.port_str ? r->parsed_uri.port :
932                        r->connection->local_addr->port ? r->connection->local_addr->port :
933                        r->server->port ? r->server->port :
934                        ap_default_port(r);
935             else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
936                 port = r->parsed_uri.port_str ? r->parsed_uri.port :
937                        r->server->port ? r->server->port :
938                        ap_default_port(r);
939             break;
940         case USE_CANONICAL_NAME_ON:
941             /* With UseCanonicalName on (and in all versions prior to 1.3)
942              * Apache will use the hostname and port specified in the
943              * ServerName directive to construct a canonical name for the
944              * server. (If no port was specified in the ServerName
945              * directive, Apache uses the port supplied by the client if
946              * any is supplied, and finally the default port for the protocol
947              * used.
948              */
949             if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
950                 port = r->server->port ? r->server->port :
951                        r->connection->local_addr->port ? r->connection->local_addr->port :
952                        ap_default_port(r);
953             else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
954                 port = r->server->port ? r->server->port :
955                        ap_default_port(r);
956             break;
957         default:
958             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
959                          "ap_get_server_port: Invalid UCN Option somehow");
960             port = ap_default_port(r);
961             break;
962     }
963
964     return port;
965 }
966
967 AP_DECLARE(char *) ap_construct_url(apr_pool_t *p, const char *uri,
968                                     request_rec *r)
969 {
970     unsigned port = ap_get_server_port(r);
971     const char *host = get_server_name_for_url(r);
972
973     if (ap_is_default_port(port, r)) {
974         return apr_pstrcat(p, ap_http_scheme(r), "://", host, uri, NULL);
975     }
976
977     return apr_psprintf(p, "%s://%s:%u%s", ap_http_scheme(r), host, port, uri);
978 }
979
980 AP_DECLARE(apr_off_t) ap_get_limit_req_body(const request_rec *r)
981 {
982     core_dir_config *d =
983       (core_dir_config *)ap_get_module_config(r->per_dir_config, &core_module);
984
985     if (d->limit_req_body == AP_LIMIT_REQ_BODY_UNSET) {
986         return AP_DEFAULT_LIMIT_REQ_BODY;
987     }
988
989     return d->limit_req_body;
990 }
991
992
993 /*****************************************************************
994  *
995  * Commands... this module handles almost all of the NCSA httpd.conf
996  * commands, but most of the old srm.conf is in the the modules.
997  */
998
999
1000 /* returns a parent if it matches the given directive */
1001 static const ap_directive_t * find_parent(const ap_directive_t *dirp,
1002                                           const char *what)
1003 {
1004     while (dirp->parent != NULL) {
1005         dirp = dirp->parent;
1006
1007         /* ### it would be nice to have atom-ized directives */
1008         if (strcasecmp(dirp->directive, what) == 0)
1009             return dirp;
1010     }
1011
1012     return NULL;
1013 }
1014
1015 AP_DECLARE(const char *) ap_check_cmd_context(cmd_parms *cmd,
1016                                               unsigned forbidden)
1017 {
1018     const char *gt = (cmd->cmd->name[0] == '<'
1019                       && cmd->cmd->name[strlen(cmd->cmd->name)-1] != '>')
1020                          ? ">" : "";
1021     const ap_directive_t *found;
1022
1023     if ((forbidden & NOT_IN_VIRTUALHOST) && cmd->server->is_virtual) {
1024         return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1025                            " cannot occur within <VirtualHost> section", NULL);
1026     }
1027
1028     if ((forbidden & NOT_IN_LIMIT) && cmd->limited != -1) {
1029         return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1030                            " cannot occur within <Limit> section", NULL);
1031     }
1032
1033     if ((forbidden & NOT_IN_DIR_LOC_FILE) == NOT_IN_DIR_LOC_FILE) {
1034         if (cmd->path != NULL) {
1035             return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1036                             " cannot occur within <Directory/Location/Files> "
1037                             "section", NULL);
1038         }
1039         if (cmd->cmd->req_override & EXEC_ON_READ) {
1040             /* EXEC_ON_READ must be NOT_IN_DIR_LOC_FILE, if not, it will
1041              * (deliberately) segfault below in the individual tests...
1042              */
1043             return NULL;
1044         }
1045     }
1046
1047     if (((forbidden & NOT_IN_DIRECTORY)
1048          && ((found = find_parent(cmd->directive, "<Directory"))
1049              || (found = find_parent(cmd->directive, "<DirectoryMatch"))))
1050         || ((forbidden & NOT_IN_LOCATION)
1051             && ((found = find_parent(cmd->directive, "<Location"))
1052                 || (found = find_parent(cmd->directive, "<LocationMatch"))))
1053         || ((forbidden & NOT_IN_FILES)
1054             && ((found = find_parent(cmd->directive, "<Files"))
1055                 || (found = find_parent(cmd->directive, "<FilesMatch"))))) {
1056         return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1057                            " cannot occur within ", found->directive,
1058                            "> section", NULL);
1059     }
1060
1061     return NULL;
1062 }
1063
1064 static const char *set_access_name(cmd_parms *cmd, void *dummy,
1065                                    const char *arg)
1066 {
1067     void *sconf = cmd->server->module_config;
1068     core_server_config *conf = ap_get_module_config(sconf, &core_module);
1069
1070     const char *err = ap_check_cmd_context(cmd,
1071                                            NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1072     if (err != NULL) {
1073         return err;
1074     }
1075
1076     conf->access_name = apr_pstrdup(cmd->pool, arg);
1077     return NULL;
1078 }
1079
1080 #ifdef GPROF
1081 static const char *set_gprof_dir(cmd_parms *cmd, void *dummy, const char *arg)
1082 {
1083     void *sconf = cmd->server->module_config;
1084     core_server_config *conf = ap_get_module_config(sconf, &core_module);
1085
1086     const char *err = ap_check_cmd_context(cmd,
1087                                            NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1088     if (err != NULL) {
1089         return err;
1090     }
1091
1092     conf->gprof_dir = apr_pstrdup(cmd->pool, arg);
1093     return NULL;
1094 }
1095 #endif /*GPROF*/
1096
1097 static const char *set_add_default_charset(cmd_parms *cmd,
1098                                            void *d_, const char *arg)
1099 {
1100     core_dir_config *d = d_;
1101
1102     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1103     if (err != NULL) {
1104         return err;
1105     }
1106
1107     if (!strcasecmp(arg, "Off")) {
1108        d->add_default_charset = ADD_DEFAULT_CHARSET_OFF;
1109     }
1110     else if (!strcasecmp(arg, "On")) {
1111        d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1112        d->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
1113     }
1114     else {
1115        d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1116        d->add_default_charset_name = arg;
1117     }
1118
1119     return NULL;
1120 }
1121
1122 static const char *set_document_root(cmd_parms *cmd, void *dummy,
1123                                      const char *arg)
1124 {
1125     void *sconf = cmd->server->module_config;
1126     core_server_config *conf = ap_get_module_config(sconf, &core_module);
1127
1128     const char *err = ap_check_cmd_context(cmd,
1129                                            NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1130     if (err != NULL) {
1131         return err;
1132     }
1133
1134     /* Make it absolute, relative to ServerRoot */
1135     arg = ap_server_root_relative(cmd->pool, arg);
1136
1137     /* TODO: ap_configtestonly && ap_docrootcheck && */
1138     if (apr_filepath_merge((char**)&conf->ap_document_root, NULL, arg,
1139                            APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS
1140         || !ap_is_directory(cmd->pool, arg)) {
1141         if (cmd->server->is_virtual) {
1142             ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0,
1143                           cmd->pool,
1144                           "Warning: DocumentRoot [%s] does not exist",
1145                           arg);
1146             conf->ap_document_root = arg;
1147         }
1148         else {
1149             return "DocumentRoot must be a directory";
1150         }
1151     }
1152     return NULL;
1153 }
1154
1155 AP_DECLARE(void) ap_custom_response(request_rec *r, int status,
1156                                     const char *string)
1157 {
1158     core_request_config *conf =
1159         ap_get_module_config(r->request_config, &core_module);
1160     int idx;
1161
1162     if (conf->response_code_strings == NULL) {
1163         conf->response_code_strings =
1164             apr_pcalloc(r->pool,
1165                         sizeof(*conf->response_code_strings) * RESPONSE_CODES);
1166     }
1167
1168     idx = ap_index_of_response(status);
1169
1170     conf->response_code_strings[idx] =
1171        ((ap_is_url(string) || (*string == '/')) && (*string != '"')) ?
1172        apr_pstrdup(r->pool, string) : apr_pstrcat(r->pool, "\"", string, NULL);
1173 }
1174
1175 static const char *set_error_document(cmd_parms *cmd, void *conf_,
1176                                       const char *errno_str, const char *msg)
1177 {
1178     core_dir_config *conf = conf_;
1179     int error_number, index_number, idx500;
1180     enum { MSG, LOCAL_PATH, REMOTE_PATH } what = MSG;
1181
1182     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1183     if (err != NULL) {
1184         return err;
1185     }
1186
1187     /* 1st parameter should be a 3 digit number, which we recognize;
1188      * convert it into an array index
1189      */
1190     error_number = atoi(errno_str);
1191     idx500 = ap_index_of_response(HTTP_INTERNAL_SERVER_ERROR);
1192
1193     if (error_number == HTTP_INTERNAL_SERVER_ERROR) {
1194         index_number = idx500;
1195     }
1196     else if ((index_number = ap_index_of_response(error_number)) == idx500) {
1197         return apr_pstrcat(cmd->pool, "Unsupported HTTP response code ",
1198                            errno_str, NULL);
1199     }
1200
1201     /* Heuristic to determine second argument. */
1202     if (ap_strchr_c(msg,' '))
1203         what = MSG;
1204     else if (msg[0] == '/')
1205         what = LOCAL_PATH;
1206     else if (ap_is_url(msg))
1207         what = REMOTE_PATH;
1208     else
1209         what = MSG;
1210
1211     /* The entry should be ignored if it is a full URL for a 401 error */
1212
1213     if (error_number == 401 && what == REMOTE_PATH) {
1214         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, cmd->server,
1215                      "cannot use a full URL in a 401 ErrorDocument "
1216                      "directive --- ignoring!");
1217     }
1218     else { /* Store it... */
1219         if (conf->response_code_strings == NULL) {
1220             conf->response_code_strings =
1221                 apr_pcalloc(cmd->pool,
1222                             sizeof(*conf->response_code_strings) *
1223                             RESPONSE_CODES);
1224         }
1225
1226         if (strcmp(msg, "default") == 0) {
1227             /* special case: ErrorDocument 404 default restores the
1228              * canned server error response
1229              */
1230             conf->response_code_strings[index_number] = &errordocument_default;
1231         }
1232         else {
1233             /* hack. Prefix a " if it is a msg; as that is what
1234              * http_protocol.c relies on to distinguish between
1235              * a msg and a (local) path.
1236              */
1237             conf->response_code_strings[index_number] = (what == MSG) ?
1238                     apr_pstrcat(cmd->pool, "\"",msg,NULL) :
1239                     apr_pstrdup(cmd->pool, msg);
1240         }
1241     }
1242
1243     return NULL;
1244 }
1245
1246 static const char *set_allow_opts(cmd_parms *cmd, allow_options_t *opts,
1247                                   const char *l)
1248 {
1249     allow_options_t opt;
1250     int first = 1;
1251
1252     char *w, *p = (char *) l;
1253     char *tok_state;
1254
1255     while ((w = apr_strtok(p, ",", &tok_state)) != NULL) {
1256
1257         if (first) {
1258             p = NULL;
1259             *opts = OPT_NONE;
1260             first = 0;
1261         }
1262
1263         if (!strcasecmp(w, "Indexes")) {
1264             opt = OPT_INDEXES;
1265         }
1266         else if (!strcasecmp(w, "Includes")) {
1267             opt = OPT_INCLUDES;
1268         }
1269         else if (!strcasecmp(w, "IncludesNOEXEC")) {
1270             opt = (OPT_INCLUDES | OPT_INCNOEXEC);
1271         }
1272         else if (!strcasecmp(w, "FollowSymLinks")) {
1273             opt = OPT_SYM_LINKS;
1274         }
1275         else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1276             opt = OPT_SYM_OWNER;
1277         }
1278         else if (!strcasecmp(w, "ExecCGI")) {
1279             opt = OPT_EXECCGI;
1280         }
1281         else if (!strcasecmp(w, "MultiViews")) {
1282             opt = OPT_MULTI;
1283         }
1284         else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1285             opt = OPT_MULTI|OPT_EXECCGI;
1286         }
1287         else if (!strcasecmp(w, "None")) {
1288             opt = OPT_NONE;
1289         }
1290         else if (!strcasecmp(w, "All")) {
1291             opt = OPT_ALL;
1292         }
1293         else {
1294             return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1295         }
1296
1297         *opts |= opt;
1298     }
1299
1300     (*opts) &= (~OPT_UNSET);
1301
1302     return NULL;
1303 }
1304
1305 static const char *set_override(cmd_parms *cmd, void *d_, const char *l)
1306 {
1307     core_dir_config *d = d_;
1308     char *w;
1309     char *k, *v;
1310
1311     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1312     if (err != NULL) {
1313         return err;
1314     }
1315
1316     /* Throw a warning if we're in <Location> or <Files> */
1317     if (ap_check_cmd_context(cmd, NOT_IN_LOCATION | NOT_IN_FILES)) {
1318         ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
1319                      "Useless use of AllowOverride in line %d of %s.",
1320                      cmd->directive->line_num, cmd->directive->filename);
1321     }
1322
1323     d->override = OR_NONE;
1324     while (l[0]) {
1325         w = ap_getword_conf(cmd->pool, &l);
1326
1327         k = w;
1328         v = strchr(k, '=');
1329         if (v) {
1330                 *v++ = '\0';
1331         }
1332
1333         if (!strcasecmp(w, "Limit")) {
1334             d->override |= OR_LIMIT;
1335         }
1336         else if (!strcasecmp(k, "Options")) {
1337             d->override |= OR_OPTIONS;
1338             if (v)
1339                 set_allow_opts(cmd, &(d->override_opts), v);
1340             else
1341                 d->override_opts = OPT_ALL;
1342         }
1343         else if (!strcasecmp(w, "FileInfo")) {
1344             d->override |= OR_FILEINFO;
1345         }
1346         else if (!strcasecmp(w, "AuthConfig")) {
1347             d->override |= OR_AUTHCFG;
1348         }
1349         else if (!strcasecmp(w, "Indexes")) {
1350             d->override |= OR_INDEXES;
1351         }
1352         else if (!strcasecmp(w, "None")) {
1353             d->override = OR_NONE;
1354         }
1355         else if (!strcasecmp(w, "All")) {
1356             d->override = OR_ALL;
1357         }
1358         else {
1359             return apr_pstrcat(cmd->pool, "Illegal override option ", w, NULL);
1360         }
1361
1362         d->override &= ~OR_UNSET;
1363     }
1364
1365     return NULL;
1366 }
1367
1368 static const char *set_options(cmd_parms *cmd, void *d_, const char *l)
1369 {
1370     core_dir_config *d = d_;
1371     allow_options_t opt;
1372     int first = 1;
1373     char action;
1374
1375     while (l[0]) {
1376         char *w = ap_getword_conf(cmd->pool, &l);
1377         action = '\0';
1378
1379         if (*w == '+' || *w == '-') {
1380             action = *(w++);
1381         }
1382         else if (first) {
1383               d->opts = OPT_NONE;
1384             first = 0;
1385         }
1386
1387         if (!strcasecmp(w, "Indexes")) {
1388             opt = OPT_INDEXES;
1389         }
1390         else if (!strcasecmp(w, "Includes")) {
1391             opt = OPT_INCLUDES;
1392         }
1393         else if (!strcasecmp(w, "IncludesNOEXEC")) {
1394             opt = (OPT_INCLUDES | OPT_INCNOEXEC);
1395         }
1396         else if (!strcasecmp(w, "FollowSymLinks")) {
1397             opt = OPT_SYM_LINKS;
1398         }
1399         else if (!strcasecmp(w, "SymLinksIfOwnerMatch")) {
1400             opt = OPT_SYM_OWNER;
1401         }
1402         else if (!strcasecmp(w, "ExecCGI")) {
1403             opt = OPT_EXECCGI;
1404         }
1405         else if (!strcasecmp(w, "MultiViews")) {
1406             opt = OPT_MULTI;
1407         }
1408         else if (!strcasecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1409             opt = OPT_MULTI|OPT_EXECCGI;
1410         }
1411         else if (!strcasecmp(w, "None")) {
1412             opt = OPT_NONE;
1413         }
1414         else if (!strcasecmp(w, "All")) {
1415             opt = OPT_ALL;
1416         }
1417         else {
1418             return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1419         }
1420
1421         if (!(cmd->override_opts & opt) && opt != OPT_NONE) {
1422             return apr_pstrcat(cmd->pool, "Option ", w, " not allowed here", NULL);
1423         }
1424         else if (action == '-') {
1425             /* we ensure the invariant (d->opts_add & d->opts_remove) == 0 */
1426             d->opts_remove |= opt;
1427             d->opts_add &= ~opt;
1428             d->opts &= ~opt;
1429         }
1430         else if (action == '+') {
1431             d->opts_add |= opt;
1432             d->opts_remove &= ~opt;
1433             d->opts |= opt;
1434         }
1435         else {
1436             d->opts |= opt;
1437         }
1438     }
1439
1440     return NULL;
1441 }
1442
1443 /*
1444  * Note what data should be used when forming file ETag values.
1445  * It would be nicer to do this as an ITERATE, but then we couldn't
1446  * remember the +/- state properly.
1447  */
1448 static const char *set_etag_bits(cmd_parms *cmd, void *mconfig,
1449                                  const char *args_p)
1450 {
1451     core_dir_config *cfg;
1452     etag_components_t bit;
1453     char action;
1454     char *token;
1455     const char *args;
1456     int valid;
1457     int first;
1458     int explicit;
1459
1460     cfg = (core_dir_config *)mconfig;
1461
1462     args = args_p;
1463     first = 1;
1464     explicit = 0;
1465     while (args[0] != '\0') {
1466         action = '*';
1467         bit = ETAG_UNSET;
1468         valid = 1;
1469         token = ap_getword_conf(cmd->pool, &args);
1470         if ((*token == '+') || (*token == '-')) {
1471             action = *token;
1472             token++;
1473         }
1474         else {
1475             /*
1476              * The occurrence of an absolute setting wipes
1477              * out any previous relative ones.  The first such
1478              * occurrence forgets any inherited ones, too.
1479              */
1480             if (first) {
1481                 cfg->etag_bits = ETAG_UNSET;
1482                 cfg->etag_add = ETAG_UNSET;
1483                 cfg->etag_remove = ETAG_UNSET;
1484                 first = 0;
1485             }
1486         }
1487
1488         if (strcasecmp(token, "None") == 0) {
1489             if (action != '*') {
1490                 valid = 0;
1491             }
1492             else {
1493                 cfg->etag_bits = bit = ETAG_NONE;
1494                 explicit = 1;
1495             }
1496         }
1497         else if (strcasecmp(token, "All") == 0) {
1498             if (action != '*') {
1499                 valid = 0;
1500             }
1501             else {
1502                 explicit = 1;
1503                 cfg->etag_bits = bit = ETAG_ALL;
1504             }
1505         }
1506         else if (strcasecmp(token, "Size") == 0) {
1507             bit = ETAG_SIZE;
1508         }
1509         else if ((strcasecmp(token, "LMTime") == 0)
1510                  || (strcasecmp(token, "MTime") == 0)
1511                  || (strcasecmp(token, "LastModified") == 0)) {
1512             bit = ETAG_MTIME;
1513         }
1514         else if (strcasecmp(token, "INode") == 0) {
1515             bit = ETAG_INODE;
1516         }
1517         else {
1518             return apr_pstrcat(cmd->pool, "Unknown keyword '",
1519                                token, "' for ", cmd->cmd->name,
1520                                " directive", NULL);
1521         }
1522
1523         if (! valid) {
1524             return apr_pstrcat(cmd->pool, cmd->cmd->name, " keyword '",
1525                                token, "' cannot be used with '+' or '-'",
1526                                NULL);
1527         }
1528
1529         if (action == '+') {
1530             /*
1531              * Make sure it's in the 'add' list and absent from the
1532              * 'subtract' list.
1533              */
1534             cfg->etag_add |= bit;
1535             cfg->etag_remove &= (~ bit);
1536         }
1537         else if (action == '-') {
1538             cfg->etag_remove |= bit;
1539             cfg->etag_add &= (~ bit);
1540         }
1541         else {
1542             /*
1543              * Non-relative values wipe out any + or - values
1544              * accumulated so far.
1545              */
1546             cfg->etag_bits |= bit;
1547             cfg->etag_add = ETAG_UNSET;
1548             cfg->etag_remove = ETAG_UNSET;
1549             explicit = 1;
1550         }
1551     }
1552
1553     /*
1554      * Any setting at all will clear the 'None' and 'Unset' bits.
1555      */
1556
1557     if (cfg->etag_add != ETAG_UNSET) {
1558         cfg->etag_add &= (~ ETAG_UNSET);
1559     }
1560
1561     if (cfg->etag_remove != ETAG_UNSET) {
1562         cfg->etag_remove &= (~ ETAG_UNSET);
1563     }
1564
1565     if (explicit) {
1566         cfg->etag_bits &= (~ ETAG_UNSET);
1567
1568         if ((cfg->etag_bits & ETAG_NONE) != ETAG_NONE) {
1569             cfg->etag_bits &= (~ ETAG_NONE);
1570         }
1571     }
1572
1573     return NULL;
1574 }
1575
1576 static const char *set_enable_mmap(cmd_parms *cmd, void *d_,
1577                                    const char *arg)
1578 {
1579     core_dir_config *d = d_;
1580     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1581
1582     if (err != NULL) {
1583         return err;
1584     }
1585
1586     if (strcasecmp(arg, "on") == 0) {
1587         d->enable_mmap = ENABLE_MMAP_ON;
1588     }
1589     else if (strcasecmp(arg, "off") == 0) {
1590         d->enable_mmap = ENABLE_MMAP_OFF;
1591     }
1592     else {
1593         return "parameter must be 'on' or 'off'";
1594     }
1595
1596     return NULL;
1597 }
1598
1599 static const char *set_enable_sendfile(cmd_parms *cmd, void *d_,
1600                                    const char *arg)
1601 {
1602     core_dir_config *d = d_;
1603     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1604
1605     if (err != NULL) {
1606         return err;
1607     }
1608
1609     if (strcasecmp(arg, "on") == 0) {
1610         d->enable_sendfile = ENABLE_SENDFILE_ON;
1611     }
1612     else if (strcasecmp(arg, "off") == 0) {
1613         d->enable_sendfile = ENABLE_SENDFILE_OFF;
1614     }
1615     else {
1616         return "parameter must be 'on' or 'off'";
1617     }
1618
1619     return NULL;
1620 }
1621
1622
1623 /*
1624  * Report a missing-'>' syntax error.
1625  */
1626 static char *unclosed_directive(cmd_parms *cmd)
1627 {
1628     return apr_pstrcat(cmd->pool, cmd->cmd->name,
1629                        "> directive missing closing '>'", NULL);
1630 }
1631
1632 /*
1633  * Report a missing args in '<Foo >' syntax error.
1634  */
1635 static char *missing_container_arg(cmd_parms *cmd)
1636 {
1637     return apr_pstrcat(cmd->pool, cmd->cmd->name,
1638                        "> directive requires additional arguments", NULL);
1639 }
1640
1641 AP_CORE_DECLARE_NONSTD(const char *) ap_limit_section(cmd_parms *cmd,
1642                                                       void *dummy,
1643                                                       const char *arg)
1644 {
1645     const char *endp = ap_strrchr_c(arg, '>');
1646     const char *limited_methods;
1647     void *tog = cmd->cmd->cmd_data;
1648     apr_int64_t limited = 0;
1649     const char *errmsg;
1650
1651     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
1652     if (err != NULL) {
1653         return err;
1654     }
1655
1656     if (endp == NULL) {
1657         return unclosed_directive(cmd);
1658     }
1659
1660     limited_methods = apr_pstrndup(cmd->pool, arg, endp - arg);
1661
1662     if (!limited_methods[0]) {
1663         return missing_container_arg(cmd);
1664     }
1665
1666     while (limited_methods[0]) {
1667         char *method = ap_getword_conf(cmd->pool, &limited_methods);
1668         int methnum;
1669
1670         /* check for builtin or module registered method number */
1671         methnum = ap_method_number_of(method);
1672
1673         if (methnum == M_TRACE && !tog) {
1674             return "TRACE cannot be controlled by <Limit>, see TraceEnable";
1675         }
1676         else if (methnum == M_INVALID) {
1677             /* method has not been registered yet, but resorce restriction
1678              * is always checked before method handling, so register it.
1679              */
1680             methnum = ap_method_register(cmd->pool, method);
1681         }
1682
1683         limited |= (AP_METHOD_BIT << methnum);
1684     }
1685
1686     /* Killing two features with one function,
1687      * if (tog == NULL) <Limit>, else <LimitExcept>
1688      */
1689     cmd->limited = tog ? ~limited : limited;
1690
1691     errmsg = ap_walk_config(cmd->directive->first_child, cmd, cmd->context);
1692
1693     cmd->limited = -1;
1694
1695     return errmsg;
1696 }
1697
1698 /* XXX: Bogus - need to do this differently (at least OS2/Netware suffer
1699  * the same problem!!!
1700  * We use this in <DirectoryMatch> and <FilesMatch>, to ensure that
1701  * people don't get bitten by wrong-cased regex matches
1702  */
1703
1704 #ifdef WIN32
1705 #define USE_ICASE AP_REG_ICASE
1706 #else
1707 #define USE_ICASE 0
1708 #endif
1709
1710 static const char *dirsection(cmd_parms *cmd, void *mconfig, const char *arg)
1711 {
1712     const char *errmsg;
1713     const char *endp = ap_strrchr_c(arg, '>');
1714     int old_overrides = cmd->override;
1715     char *old_path = cmd->path;
1716     core_dir_config *conf;
1717     ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
1718     ap_regex_t *r = NULL;
1719     const command_rec *thiscmd = cmd->cmd;
1720
1721     const char *err = ap_check_cmd_context(cmd,
1722                                            NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1723     if (err != NULL) {
1724         return err;
1725     }
1726
1727     if (endp == NULL) {
1728         return unclosed_directive(cmd);
1729     }
1730
1731     arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1732
1733     if (!arg[0]) {
1734         return missing_container_arg(cmd);
1735     }
1736
1737     if (!arg) {
1738         if (thiscmd->cmd_data)
1739             return "<DirectoryMatch > block must specify a path";
1740         else
1741             return "<Directory > block must specify a path";
1742     }
1743
1744     cmd->path = ap_getword_conf(cmd->pool, &arg);
1745     cmd->override = OR_ALL|ACCESS_CONF;
1746
1747     if (!strcmp(cmd->path, "~")) {
1748         cmd->path = ap_getword_conf(cmd->pool, &arg);
1749         if (!cmd->path)
1750             return "<Directory ~ > block must specify a path";
1751         r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1752         if (!r) {
1753             return "Regex could not be compiled";
1754         }
1755     }
1756     else if (thiscmd->cmd_data) { /* <DirectoryMatch> */
1757         r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1758         if (!r) {
1759             return "Regex could not be compiled";
1760         }
1761     }
1762     else if (!strcmp(cmd->path, "/") == 0)
1763     {
1764         char *newpath;
1765
1766         /*
1767          * Ensure that the pathname is canonical, and append the trailing /
1768          */
1769         apr_status_t rv = apr_filepath_merge(&newpath, NULL, cmd->path,
1770                                              APR_FILEPATH_TRUENAME, cmd->pool);
1771         if (rv != APR_SUCCESS && rv != APR_EPATHWILD) {
1772             return apr_pstrcat(cmd->pool, "<Directory \"", cmd->path,
1773                                "\"> path is invalid.", NULL);
1774         }
1775
1776         cmd->path = newpath;
1777         if (cmd->path[strlen(cmd->path) - 1] != '/')
1778             cmd->path = apr_pstrcat(cmd->pool, cmd->path, "/", NULL);
1779     }
1780
1781     /* initialize our config and fetch it */
1782     conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
1783                                  &core_module, cmd->pool);
1784
1785     errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
1786     if (errmsg != NULL)
1787         return errmsg;
1788
1789     conf->r = r;
1790     conf->d = cmd->path;
1791     conf->d_is_fnmatch = (apr_fnmatch_test(conf->d) != 0);
1792
1793     /* Make this explicit - the "/" root has 0 elements, that is, we
1794      * will always merge it, and it will always sort and merge first.
1795      * All others are sorted and tested by the number of slashes.
1796      */
1797     if (strcmp(conf->d, "/") == 0)
1798         conf->d_components = 0;
1799     else
1800         conf->d_components = ap_count_dirs(conf->d);
1801
1802     ap_add_per_dir_conf(cmd->server, new_dir_conf);
1803
1804     if (*arg != '\0') {
1805         return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1806                            "> arguments not (yet) supported.", NULL);
1807     }
1808
1809     cmd->path = old_path;
1810     cmd->override = old_overrides;
1811
1812     return NULL;
1813 }
1814
1815 static const char *urlsection(cmd_parms *cmd, void *mconfig, const char *arg)
1816 {
1817     const char *errmsg;
1818     const char *endp = ap_strrchr_c(arg, '>');
1819     int old_overrides = cmd->override;
1820     char *old_path = cmd->path;
1821     core_dir_config *conf;
1822     ap_regex_t *r = NULL;
1823     const command_rec *thiscmd = cmd->cmd;
1824     ap_conf_vector_t *new_url_conf = ap_create_per_dir_config(cmd->pool);
1825     const char *err = ap_check_cmd_context(cmd,
1826                                            NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
1827     if (err != NULL) {
1828         return err;
1829     }
1830
1831     if (endp == NULL) {
1832         return unclosed_directive(cmd);
1833     }
1834
1835     arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1836
1837     if (!arg[0]) {
1838         return missing_container_arg(cmd);
1839     }
1840
1841     cmd->path = ap_getword_conf(cmd->pool, &arg);
1842     cmd->override = OR_ALL|ACCESS_CONF;
1843
1844     if (thiscmd->cmd_data) { /* <LocationMatch> */
1845         r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
1846         if (!r) {
1847             return "Regex could not be compiled";
1848         }
1849     }
1850     else if (!strcmp(cmd->path, "~")) {
1851         cmd->path = ap_getword_conf(cmd->pool, &arg);
1852         r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
1853         if (!r) {
1854             return "Regex could not be compiled";
1855         }
1856     }
1857
1858     /* initialize our config and fetch it */
1859     conf = ap_set_config_vectors(cmd->server, new_url_conf, cmd->path,
1860                                  &core_module, cmd->pool);
1861
1862     errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_url_conf);
1863     if (errmsg != NULL)
1864         return errmsg;
1865
1866     conf->d = apr_pstrdup(cmd->pool, cmd->path);     /* No mangling, please */
1867     conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1868     conf->r = r;
1869
1870     ap_add_per_url_conf(cmd->server, new_url_conf);
1871
1872     if (*arg != '\0') {
1873         return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1874                            "> arguments not (yet) supported.", NULL);
1875     }
1876
1877     cmd->path = old_path;
1878     cmd->override = old_overrides;
1879
1880     return NULL;
1881 }
1882
1883 static const char *filesection(cmd_parms *cmd, void *mconfig, const char *arg)
1884 {
1885     const char *errmsg;
1886     const char *endp = ap_strrchr_c(arg, '>');
1887     int old_overrides = cmd->override;
1888     char *old_path = cmd->path;
1889     core_dir_config *conf;
1890     ap_regex_t *r = NULL;
1891     const command_rec *thiscmd = cmd->cmd;
1892     core_dir_config *c = mconfig;
1893     ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
1894     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT|NOT_IN_LOCATION);
1895
1896     if (err != NULL) {
1897         return err;
1898     }
1899
1900     if (endp == NULL) {
1901         return unclosed_directive(cmd);
1902     }
1903
1904     arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1905
1906     if (!arg[0]) {
1907         return missing_container_arg(cmd);
1908     }
1909
1910     cmd->path = ap_getword_conf(cmd->pool, &arg);
1911     /* Only if not an .htaccess file */
1912     if (!old_path) {
1913         cmd->override = OR_ALL|ACCESS_CONF;
1914     }
1915
1916     if (thiscmd->cmd_data) { /* <FilesMatch> */
1917         r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1918         if (!r) {
1919             return "Regex could not be compiled";
1920         }
1921     }
1922     else if (!strcmp(cmd->path, "~")) {
1923         cmd->path = ap_getword_conf(cmd->pool, &arg);
1924         r = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
1925         if (!r) {
1926             return "Regex could not be compiled";
1927         }
1928     }
1929     else {
1930         char *newpath;
1931         /* Ensure that the pathname is canonical, but we
1932          * can't test the case/aliases without a fixed path */
1933         if (apr_filepath_merge(&newpath, "", cmd->path,
1934                                0, cmd->pool) != APR_SUCCESS)
1935                 return apr_pstrcat(cmd->pool, "<Files \"", cmd->path,
1936                                "\"> is invalid.", NULL);
1937         cmd->path = newpath;
1938     }
1939
1940     /* initialize our config and fetch it */
1941     conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
1942                                  &core_module, cmd->pool);
1943
1944     errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
1945     if (errmsg != NULL)
1946         return errmsg;
1947
1948     conf->d = cmd->path;
1949     conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
1950     conf->r = r;
1951
1952     ap_add_file_conf(c, new_file_conf);
1953
1954     if (*arg != '\0') {
1955         return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
1956                            "> arguments not (yet) supported.", NULL);
1957     }
1958
1959     cmd->path = old_path;
1960     cmd->override = old_overrides;
1961
1962     return NULL;
1963 }
1964
1965 static const char *start_ifmod(cmd_parms *cmd, void *mconfig, const char *arg)
1966 {
1967     const char *endp = ap_strrchr_c(arg, '>');
1968     int not = (arg[0] == '!');
1969     module *found;
1970
1971     if (endp == NULL) {
1972         return unclosed_directive(cmd);
1973     }
1974
1975     arg = apr_pstrndup(cmd->pool, arg, endp - arg);
1976
1977     if (not) {
1978         arg++;
1979     }
1980
1981     if (!arg[0]) {
1982         return missing_container_arg(cmd);
1983     }
1984
1985     found = ap_find_linked_module(arg);
1986
1987     /* search prelinked stuff */
1988     if (!found) {
1989         ap_module_symbol_t *current = ap_prelinked_module_symbols;
1990
1991         for (; current->name; ++current) {
1992             if (!strcmp(current->name, arg)) {
1993                 found = current->modp;
1994                 break;
1995             }
1996         }
1997     }
1998
1999     /* search dynamic stuff */
2000     if (!found) {
2001         APR_OPTIONAL_FN_TYPE(ap_find_loaded_module_symbol) *check_symbol =
2002             APR_RETRIEVE_OPTIONAL_FN(ap_find_loaded_module_symbol);
2003
2004         if (check_symbol) {
2005             found = check_symbol(cmd->server, arg);
2006         }
2007     }
2008
2009     if ((!not && found) || (not && !found)) {
2010         ap_directive_t *parent = NULL;
2011         ap_directive_t *current = NULL;
2012         const char *retval;
2013
2014         retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2015                                       &current, &parent, "<IfModule");
2016         *(ap_directive_t **)mconfig = current;
2017         return retval;
2018     }
2019     else {
2020         *(ap_directive_t **)mconfig = NULL;
2021         return ap_soak_end_container(cmd, "<IfModule");
2022     }
2023 }
2024
2025 AP_DECLARE(int) ap_exists_config_define(const char *name)
2026 {
2027     char **defines;
2028     int i;
2029
2030     defines = (char **)ap_server_config_defines->elts;
2031     for (i = 0; i < ap_server_config_defines->nelts; i++) {
2032         if (strcmp(defines[i], name) == 0) {
2033             return 1;
2034         }
2035     }
2036
2037     return 0;
2038 }
2039
2040 static const char *start_ifdefine(cmd_parms *cmd, void *dummy, const char *arg)
2041 {
2042     const char *endp;
2043     int defined;
2044     int not = 0;
2045
2046     endp = ap_strrchr_c(arg, '>');
2047     if (endp == NULL) {
2048         return unclosed_directive(cmd);
2049     }
2050
2051     arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2052
2053     if (arg[0] == '!') {
2054         not = 1;
2055         arg++;
2056     }
2057
2058     if (!arg[0]) {
2059         return missing_container_arg(cmd);
2060     }
2061
2062     defined = ap_exists_config_define(arg);
2063     if ((!not && defined) || (not && !defined)) {
2064         ap_directive_t *parent = NULL;
2065         ap_directive_t *current = NULL;
2066         const char *retval;
2067
2068         retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2069                                       &current, &parent, "<IfDefine");
2070         *(ap_directive_t **)dummy = current;
2071         return retval;
2072     }
2073     else {
2074         *(ap_directive_t **)dummy = NULL;
2075         return ap_soak_end_container(cmd, "<IfDefine");
2076     }
2077 }
2078
2079 /* httpd.conf commands... beginning with the <VirtualHost> business */
2080
2081 static const char *virtualhost_section(cmd_parms *cmd, void *dummy,
2082                                        const char *arg)
2083 {
2084     server_rec *main_server = cmd->server, *s;
2085     const char *errmsg;
2086     const char *endp = ap_strrchr_c(arg, '>');
2087     apr_pool_t *p = cmd->pool;
2088
2089     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2090     if (err != NULL) {
2091         return err;
2092     }
2093
2094     if (endp == NULL) {
2095         return unclosed_directive(cmd);
2096     }
2097
2098     arg = apr_pstrndup(cmd->pool, arg, endp - arg);
2099
2100     if (!arg[0]) {
2101         return missing_container_arg(cmd);
2102     }
2103
2104     /* FIXME: There's another feature waiting to happen here -- since you
2105         can now put multiple addresses/names on a single <VirtualHost>
2106         you might want to use it to group common definitions and then
2107         define other "subhosts" with their individual differences.  But
2108         personally I'd rather just do it with a macro preprocessor. -djg */
2109     if (main_server->is_virtual) {
2110         return "<VirtualHost> doesn't nest!";
2111     }
2112
2113     errmsg = ap_init_virtual_host(p, arg, main_server, &s);
2114     if (errmsg) {
2115         return errmsg;
2116     }
2117
2118     s->next = main_server->next;
2119     main_server->next = s;
2120
2121     s->defn_name = cmd->directive->filename;
2122     s->defn_line_number = cmd->directive->line_num;
2123
2124     cmd->server = s;
2125
2126     errmsg = ap_walk_config(cmd->directive->first_child, cmd,
2127                             s->lookup_defaults);
2128
2129     cmd->server = main_server;
2130
2131     return errmsg;
2132 }
2133
2134 static const char *set_server_alias(cmd_parms *cmd, void *dummy,
2135                                     const char *arg)
2136 {
2137     if (!cmd->server->names) {
2138         return "ServerAlias only used in <VirtualHost>";
2139     }
2140
2141     while (*arg) {
2142         char **item, *name = ap_getword_conf(cmd->pool, &arg);
2143
2144         if (ap_is_matchexp(name)) {
2145             item = (char **)apr_array_push(cmd->server->wild_names);
2146         }
2147         else {
2148             item = (char **)apr_array_push(cmd->server->names);
2149         }
2150
2151         *item = name;
2152     }
2153
2154     return NULL;
2155 }
2156
2157 static const char *set_accf_map(cmd_parms *cmd, void *dummy,
2158                                 const char *iproto, const char* iaccf)
2159 {
2160     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2161     core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2162                                                     &core_module);
2163     char* proto;
2164     char* accf;
2165     if (err != NULL) {
2166         return err;
2167     }
2168
2169     proto = apr_pstrdup(cmd->pool, iproto);
2170     ap_str_tolower(proto);
2171     accf = apr_pstrdup(cmd->pool, iaccf);
2172     ap_str_tolower(accf);
2173     apr_table_set(conf->accf_map, proto, accf);
2174
2175     return NULL;
2176 }
2177
2178 AP_DECLARE(const char*) ap_get_server_protocol(server_rec* s)
2179 {
2180     core_server_config *conf = ap_get_module_config(s->module_config,
2181                                                     &core_module);
2182     return conf->protocol;
2183 }
2184
2185 AP_DECLARE(void) ap_set_server_protocol(server_rec* s, const char* proto)
2186 {
2187     core_server_config *conf = ap_get_module_config(s->module_config,
2188                                                     &core_module);
2189     conf->protocol = proto;
2190 }
2191
2192 static const char *set_protocol(cmd_parms *cmd, void *dummy,
2193                                 const char *arg)
2194 {
2195     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2196     core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2197                                                     &core_module);
2198     char* proto;
2199
2200     if (err != NULL) {
2201         return err;
2202     }
2203
2204     proto = apr_pstrdup(cmd->pool, arg);
2205     ap_str_tolower(proto);
2206     conf->protocol = proto;
2207
2208     return NULL;
2209 }
2210
2211 static const char *set_server_string_slot(cmd_parms *cmd, void *dummy,
2212                                           const char *arg)
2213 {
2214     /* This one's pretty generic... */
2215
2216     int offset = (int)(long)cmd->info;
2217     char *struct_ptr = (char *)cmd->server;
2218
2219     const char *err = ap_check_cmd_context(cmd,
2220                                            NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2221     if (err != NULL) {
2222         return err;
2223     }
2224
2225     *(const char **)(struct_ptr + offset) = arg;
2226     return NULL;
2227 }
2228
2229 /*
2230  * The ServerName directive takes one argument with format
2231  * [scheme://]fully-qualified-domain-name[:port], for instance
2232  * ServerName www.example.com
2233  * ServerName www.example.com:80
2234  * ServerName https://www.example.com:443
2235  */
2236
2237 static const char *server_hostname_port(cmd_parms *cmd, void *dummy, const char *arg)
2238 {
2239     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2240     const char *portstr, *part;
2241     char *scheme;
2242     int port;
2243
2244     if (err != NULL) {
2245         return err;
2246     }
2247
2248     part = ap_strstr_c(arg, "://");
2249
2250     if (part) {
2251       scheme = apr_pstrndup(cmd->pool, arg, part - arg);
2252       ap_str_tolower(scheme);
2253       cmd->server->server_scheme = (const char *)scheme;
2254       part += 3;
2255     } else {
2256       part = arg;
2257     }
2258
2259     portstr = ap_strchr_c(part, ':');
2260     if (portstr) {
2261         cmd->server->server_hostname = apr_pstrndup(cmd->pool, part,
2262                                                     portstr - part);
2263         portstr++;
2264         port = atoi(portstr);
2265         if (port <= 0 || port >= 65536) { /* 65536 == 1<<16 */
2266             return apr_pstrcat(cmd->temp_pool, "The port number \"", arg,
2267                           "\" is outside the appropriate range "
2268                           "(i.e., 1..65535).", NULL);
2269         }
2270     }
2271     else {
2272         cmd->server->server_hostname = apr_pstrdup(cmd->pool, part);
2273         port = 0;
2274     }
2275
2276     cmd->server->port = port;
2277     return NULL;
2278 }
2279
2280 static const char *set_signature_flag(cmd_parms *cmd, void *d_,
2281                                       const char *arg)
2282 {
2283     core_dir_config *d = d_;
2284     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2285
2286     if (err != NULL) {
2287         return err;
2288     }
2289
2290     if (strcasecmp(arg, "On") == 0) {
2291         d->server_signature = srv_sig_on;
2292     }
2293     else if (strcasecmp(arg, "Off") == 0) {
2294         d->server_signature = srv_sig_off;
2295     }
2296     else if (strcasecmp(arg, "EMail") == 0) {
2297         d->server_signature = srv_sig_withmail;
2298     }
2299     else {
2300         return "ServerSignature: use one of: off | on | email";
2301     }
2302
2303     return NULL;
2304 }
2305
2306 static const char *set_server_root(cmd_parms *cmd, void *dummy,
2307                                    const char *arg)
2308 {
2309     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2310
2311     if (err != NULL) {
2312         return err;
2313     }
2314
2315     if ((apr_filepath_merge((char**)&ap_server_root, NULL, arg,
2316                             APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS)
2317         || !ap_is_directory(cmd->pool, ap_server_root)) {
2318         return "ServerRoot must be a valid directory";
2319     }
2320
2321     return NULL;
2322 }
2323
2324 static const char *set_timeout(cmd_parms *cmd, void *dummy, const char *arg)
2325 {
2326     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2327
2328     if (err != NULL) {
2329         return err;
2330     }
2331
2332     cmd->server->timeout = apr_time_from_sec(atoi(arg));
2333     return NULL;
2334 }
2335
2336 static const char *set_allow2f(cmd_parms *cmd, void *d_, int arg)
2337 {
2338     core_dir_config *d = d_;
2339     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2340
2341     if (err != NULL) {
2342         return err;
2343     }
2344
2345     d->allow_encoded_slashes = arg != 0;
2346     return NULL;
2347 }
2348
2349 static const char *set_hostname_lookups(cmd_parms *cmd, void *d_,
2350                                         const char *arg)
2351 {
2352     core_dir_config *d = d_;
2353     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2354
2355     if (err != NULL) {
2356         return err;
2357     }
2358
2359     if (!strcasecmp(arg, "on")) {
2360         d->hostname_lookups = HOSTNAME_LOOKUP_ON;
2361     }
2362     else if (!strcasecmp(arg, "off")) {
2363         d->hostname_lookups = HOSTNAME_LOOKUP_OFF;
2364     }
2365     else if (!strcasecmp(arg, "double")) {
2366         d->hostname_lookups = HOSTNAME_LOOKUP_DOUBLE;
2367     }
2368     else {
2369         return "parameter must be 'on', 'off', or 'double'";
2370     }
2371
2372     return NULL;
2373 }
2374
2375 static const char *set_serverpath(cmd_parms *cmd, void *dummy,
2376                                   const char *arg)
2377 {
2378     const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2379
2380     if (err != NULL) {
2381         return err;
2382     }
2383
2384     cmd->server->path = arg;
2385     cmd->server->pathlen = (int)strlen(arg);
2386     return NULL;
2387 }
2388
2389 static const char *set_content_md5(cmd_parms *cmd, void *d_, int arg)
2390 {
2391     core_dir_config *d = d_;
2392     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2393
2394     if (err != NULL) {
2395         return err;
2396     }
2397
2398     d->content_md5 = arg != 0;
2399     return NULL;
2400 }
2401
2402 static const char *set_accept_path_info(cmd_parms *cmd, void *d_, const char *arg)
2403 {
2404     core_dir_config *d = d_;
2405
2406     if (strcasecmp(arg, "on") == 0) {
2407         d->accept_path_info = AP_REQ_ACCEPT_PATH_INFO;
2408     }
2409     else if (strcasecmp(arg, "off") == 0) {
2410         d->accept_path_info = AP_REQ_REJECT_PATH_INFO;
2411     }
2412     else if (strcasecmp(arg, "default") == 0) {
2413         d->accept_path_info = AP_REQ_DEFAULT_PATH_INFO;
2414     }
2415     else {
2416         return "AcceptPathInfo must be set to on, off or default";
2417     }
2418
2419     return NULL;
2420 }
2421
2422 static const char *set_use_canonical_name(cmd_parms *cmd, void *d_,
2423                                           const char *arg)
2424 {
2425     core_dir_config *d = d_;
2426     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2427
2428     if (err != NULL) {
2429         return err;
2430     }
2431
2432     if (strcasecmp(arg, "on") == 0) {
2433         d->use_canonical_name = USE_CANONICAL_NAME_ON;
2434     }
2435     else if (strcasecmp(arg, "off") == 0) {
2436         d->use_canonical_name = USE_CANONICAL_NAME_OFF;
2437     }
2438     else if (strcasecmp(arg, "dns") == 0) {
2439         d->use_canonical_name = USE_CANONICAL_NAME_DNS;
2440     }
2441     else {
2442         return "parameter must be 'on', 'off', or 'dns'";
2443     }
2444
2445     return NULL;
2446 }
2447
2448 static const char *set_use_canonical_phys_port(cmd_parms *cmd, void *d_,
2449                                           const char *arg)
2450 {
2451     core_dir_config *d = d_;
2452     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2453
2454     if (err != NULL) {
2455         return err;
2456     }
2457
2458     if (strcasecmp(arg, "on") == 0) {
2459         d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_ON;
2460     }
2461     else if (strcasecmp(arg, "off") == 0) {
2462         d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_OFF;
2463     }
2464     else {
2465         return "parameter must be 'on' or 'off'";
2466     }
2467
2468     return NULL;
2469 }
2470
2471
2472 static const char *include_config (cmd_parms *cmd, void *dummy,
2473                                    const char *name)
2474 {
2475     ap_directive_t *conftree = NULL;
2476     const char* conffile, *error;
2477     unsigned *recursion;
2478     void *data;
2479
2480     apr_pool_userdata_get(&data, "ap_include_sentinel", cmd->pool);
2481     if (data) {
2482         recursion = data;
2483     }
2484     else {
2485         data = recursion = apr_palloc(cmd->pool, sizeof(*recursion));
2486         *recursion = 0;
2487         apr_pool_userdata_setn(data, "ap_include_sentinel", NULL, cmd->pool);
2488     }
2489
2490     if (++*recursion > AP_MAX_INCLUDE_DEPTH) {
2491         *recursion = 0;
2492         return apr_psprintf(cmd->pool, "Exceeded maximum include depth of %u. "
2493                             "You have probably a recursion somewhere.",
2494                             AP_MAX_INCLUDE_DEPTH);
2495     }
2496
2497     conffile = ap_server_root_relative(cmd->pool, name);
2498     if (!conffile) {
2499         *recursion = 0;
2500         return apr_pstrcat(cmd->pool, "Invalid Include path ",
2501                            name, NULL);
2502     }
2503
2504     error = ap_process_resource_config(cmd->server, conffile,
2505                                        &conftree, cmd->pool, cmd->temp_pool);
2506     if (error) {
2507         *recursion = 0;
2508         return error;
2509     }
2510
2511     *(ap_directive_t **)dummy = conftree;
2512
2513     /* recursion level done */
2514     if (*recursion) {
2515         --*recursion;
2516     }
2517
2518     return NULL;
2519 }
2520
2521 static const char *set_loglevel(cmd_parms *cmd, void *dummy, const char *arg)
2522 {
2523     char *str;
2524
2525     const char *err = ap_check_cmd_context(cmd,
2526                                            NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2527     if (err != NULL) {
2528         return err;
2529     }
2530
2531     if ((str = ap_getword_conf(cmd->pool, &arg))) {
2532         if (!strcasecmp(str, "emerg")) {
2533             cmd->server->loglevel = APLOG_EMERG;
2534         }
2535         else if (!strcasecmp(str, "alert")) {
2536             cmd->server->loglevel = APLOG_ALERT;
2537         }
2538         else if (!strcasecmp(str, "crit")) {
2539             cmd->server->loglevel = APLOG_CRIT;
2540         }
2541         else if (!strcasecmp(str, "error")) {
2542             cmd->server->loglevel = APLOG_ERR;
2543         }
2544         else if (!strcasecmp(str, "warn")) {
2545             cmd->server->loglevel = APLOG_WARNING;
2546         }
2547         else if (!strcasecmp(str, "notice")) {
2548             cmd->server->loglevel = APLOG_NOTICE;
2549         }
2550         else if (!strcasecmp(str, "info")) {
2551             cmd->server->loglevel = APLOG_INFO;
2552         }
2553         else if (!strcasecmp(str, "debug")) {
2554             cmd->server->loglevel = APLOG_DEBUG;
2555         }
2556         else {
2557             return "LogLevel requires level keyword: one of "
2558                    "emerg/alert/crit/error/warn/notice/info/debug";
2559         }
2560     }
2561     else {
2562         return "LogLevel requires level keyword";
2563     }
2564
2565     return NULL;
2566 }
2567
2568 AP_DECLARE(const char *) ap_psignature(const char *prefix, request_rec *r)
2569 {
2570     char sport[20];
2571     core_dir_config *conf;
2572
2573     conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
2574                                                    &core_module);
2575     if ((conf->server_signature == srv_sig_off)
2576             || (conf->server_signature == srv_sig_unset)) {
2577         return "";
2578     }
2579
2580     apr_snprintf(sport, sizeof sport, "%u", (unsigned) ap_get_server_port(r));
2581
2582     if (conf->server_signature == srv_sig_withmail) {
2583         return apr_pstrcat(r->pool, prefix, "<address>",
2584                            ap_get_server_banner(),
2585                            " Server at <a href=\"",
2586                            ap_is_url(r->server->server_admin) ? "" : "mailto:",
2587                            ap_escape_html(r->pool, r->server->server_admin),
2588                            "\">",
2589                            ap_escape_html(r->pool, ap_get_server_name(r)),
2590                            "</a> Port ", sport,
2591                            "</address>\n", NULL);
2592     }
2593
2594     return apr_pstrcat(r->pool, prefix, "<address>", ap_get_server_banner(),
2595                        " Server at ",
2596                        ap_escape_html(r->pool, ap_get_server_name(r)),
2597                        " Port ", sport,
2598                        "</address>\n", NULL);
2599 }
2600
2601 /*
2602  * Handle a request to include the server's OS platform in the Server
2603  * response header field (the ServerTokens directive).  Unfortunately
2604  * this requires a new global in order to communicate the setting back to
2605  * http_main so it can insert the information in the right place in the
2606  * string.
2607  */
2608
2609 static char *server_banner = NULL;
2610 static int banner_locked = 0;
2611 static char *server_description = NULL;
2612
2613 enum server_token_type {
2614     SrvTk_MAJOR,        /* eg: Apache/2 */
2615     SrvTk_MINOR,        /* eg. Apache/2.0 */
2616     SrvTk_MINIMAL,      /* eg: Apache/2.0.41 */
2617     SrvTk_OS,           /* eg: Apache/2.0.41 (UNIX) */
2618     SrvTk_FULL,         /* eg: Apache/2.0.41 (UNIX) PHP/4.2.2 FooBar/1.2b */
2619     SrvTk_PRODUCT_ONLY  /* eg: Apache */
2620 };
2621 static enum server_token_type ap_server_tokens = SrvTk_FULL;
2622
2623 static apr_status_t reset_banner(void *dummy)
2624 {
2625     banner_locked = 0;
2626     ap_server_tokens = SrvTk_FULL;
2627     server_banner = NULL;
2628     server_description = NULL;
2629     return APR_SUCCESS;
2630 }
2631
2632 AP_DECLARE(void) ap_get_server_revision(ap_version_t *version)
2633 {
2634     version->major = AP_SERVER_MAJORVERSION_NUMBER;
2635     version->minor = AP_SERVER_MINORVERSION_NUMBER;
2636     version->patch = AP_SERVER_PATCHLEVEL_NUMBER;
2637     version->add_string = AP_SERVER_ADD_STRING;
2638 }
2639
2640 AP_DECLARE(const char *) ap_get_server_description(void)
2641 {
2642     return server_description ? server_description :
2643         AP_SERVER_BASEVERSION " (" PLATFORM ")";
2644 }
2645
2646 AP_DECLARE(const char *) ap_get_server_banner(void)
2647 {
2648     return server_banner ? server_banner : AP_SERVER_BASEVERSION;
2649 }
2650
2651 AP_DECLARE(void) ap_add_version_component(apr_pool_t *pconf, const char *component)
2652 {
2653     if (! banner_locked) {
2654         /*
2655          * If the version string is null, register our cleanup to reset the
2656          * pointer on pool destruction. We also know that, if NULL,
2657          * we are adding the original SERVER_BASEVERSION string.
2658          */
2659         if (server_banner == NULL) {
2660             apr_pool_cleanup_register(pconf, NULL, reset_banner,
2661                                       apr_pool_cleanup_null);
2662             server_banner = apr_pstrdup(pconf, component);
2663         }
2664         else {
2665             /*
2666              * Tack the given component identifier to the end of
2667              * the existing string.
2668              */
2669             server_banner = apr_pstrcat(pconf, server_banner, " ",
2670                                         component, NULL);
2671         }
2672     }
2673     server_description = apr_pstrcat(pconf, server_description, " ",
2674                                      component, NULL);
2675 }
2676
2677 /*
2678  * This routine adds the real server base identity to the banner string,
2679  * and then locks out changes until the next reconfig.
2680  */
2681 static void set_banner(apr_pool_t *pconf)
2682 {
2683     if (ap_server_tokens == SrvTk_PRODUCT_ONLY) {
2684         ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT);
2685     }
2686     else if (ap_server_tokens == SrvTk_MINIMAL) {
2687         ap_add_version_component(pconf, AP_SERVER_BASEVERSION);
2688     }
2689     else if (ap_server_tokens == SrvTk_MINOR) {
2690         ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MINORREVISION);
2691     }
2692     else if (ap_server_tokens == SrvTk_MAJOR) {
2693         ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MAJORVERSION);
2694     }
2695     else {
2696         ap_add_version_component(pconf, AP_SERVER_BASEVERSION " (" PLATFORM ")");
2697     }
2698
2699     /*
2700      * Lock the server_banner string if we're not displaying
2701      * the full set of tokens
2702      */
2703     if (ap_server_tokens != SrvTk_FULL) {
2704         banner_locked++;
2705     }
2706     server_description = AP_SERVER_BASEVERSION " (" PLATFORM ")";
2707 }
2708
2709 static const char *set_serv_tokens(cmd_parms *cmd, void *dummy,
2710                                    const char *arg)
2711 {
2712     const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
2713
2714     if (err != NULL) {
2715         return err;
2716     }
2717
2718     if (!strcasecmp(arg, "OS")) {
2719         ap_server_tokens = SrvTk_OS;
2720     }
2721     else if (!strcasecmp(arg, "Min") || !strcasecmp(arg, "Minimal")) {
2722         ap_server_tokens = SrvTk_MINIMAL;
2723     }
2724     else if (!strcasecmp(arg, "Major")) {
2725         ap_server_tokens = SrvTk_MAJOR;
2726     }
2727     else if (!strcasecmp(arg, "Minor") ) {
2728         ap_server_tokens = SrvTk_MINOR;
2729     }
2730     else if (!strcasecmp(arg, "Prod") || !strcasecmp(arg, "ProductOnly")) {
2731         ap_server_tokens = SrvTk_PRODUCT_ONLY;
2732     }
2733     else {
2734         ap_server_tokens = SrvTk_FULL;
2735     }
2736
2737     return NULL;
2738 }
2739
2740 static const char *set_limit_req_line(cmd_parms *cmd, void *dummy,
2741                                       const char *arg)
2742 {
2743     const char *err = ap_check_cmd_context(cmd,
2744                                            NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2745     int lim;
2746
2747     if (err != NULL) {
2748         return err;
2749     }
2750
2751     lim = atoi(arg);
2752     if (lim < 0) {
2753         return apr_pstrcat(cmd->temp_pool, "LimitRequestLine \"", arg,
2754                            "\" must be a non-negative integer", NULL);
2755     }
2756
2757     cmd->server->limit_req_line = lim;
2758     return NULL;
2759 }
2760
2761 static const char *set_limit_req_fieldsize(cmd_parms *cmd, void *dummy,
2762                                            const char *arg)
2763 {
2764     const char *err = ap_check_cmd_context(cmd,
2765                                            NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2766     int lim;
2767
2768     if (err != NULL) {
2769         return err;
2770     }
2771
2772     lim = atoi(arg);
2773     if (lim < 0) {
2774         return apr_pstrcat(cmd->temp_pool, "LimitRequestFieldsize \"", arg,
2775                           "\" must be a non-negative integer",
2776                           NULL);
2777     }
2778
2779     cmd->server->limit_req_fieldsize = lim;
2780     return NULL;
2781 }
2782
2783 static const char *set_limit_req_fields(cmd_parms *cmd, void *dummy,
2784                                         const char *arg)
2785 {
2786     const char *err = ap_check_cmd_context(cmd,
2787                                            NOT_IN_DIR_LOC_FILE|NOT_IN_LIMIT);
2788     int lim;
2789
2790     if (err != NULL) {
2791         return err;
2792     }
2793
2794     lim = atoi(arg);
2795     if (lim < 0) {
2796         return apr_pstrcat(cmd->temp_pool, "LimitRequestFields \"", arg,
2797                            "\" must be a non-negative integer (0 = no limit)",
2798                            NULL);
2799     }
2800
2801     cmd->server->limit_req_fields = lim;
2802     return NULL;
2803 }
2804
2805 static const char *set_limit_req_body(cmd_parms *cmd, void *conf_,
2806                                       const char *arg)
2807 {
2808     core_dir_config *conf = conf_;
2809     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2810     char *errp;
2811
2812     if (err != NULL) {
2813         return err;
2814     }
2815
2816     if (APR_SUCCESS != apr_strtoff(&conf->limit_req_body, arg, &errp, 10)) {
2817         return "LimitRequestBody argument is not parsable.";
2818     }
2819     if (*errp || conf->limit_req_body < 0) {
2820         return "LimitRequestBody requires a non-negative integer.";
2821     }
2822
2823     return NULL;
2824 }
2825
2826 static const char *set_limit_xml_req_body(cmd_parms *cmd, void *conf_,
2827                                           const char *arg)
2828 {
2829     core_dir_config *conf = conf_;
2830     const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2831
2832     if (err != NULL) {
2833         return err;
2834     }
2835
2836     conf->limit_xml_body = atol(arg);
2837     if (conf->limit_xml_body < 0)
2838         return "LimitXMLRequestBody requires a non-negative integer.";
2839
2840     return NULL;
2841 }
2842
2843 AP_DECLARE(size_t) ap_get_limit_xml_body(const request_rec *r)
2844 {
2845     core_dir_config *conf;
2846
2847     conf = ap_get_module_config(r->per_dir_config, &core_module);
2848     if (conf->limit_xml_body == AP_LIMIT_UNSET)
2849         return AP_DEFAULT_LIMIT_XML_BODY;
2850
2851     return (size_t)conf->limit_xml_body;
2852 }
2853
2854 #if !defined (RLIMIT_CPU) || !(defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)) || !defined (RLIMIT_NPROC)
2855 static const char *no_set_limit(cmd_parms *cmd, void *conf_,
2856                                 const char *arg, const char *arg2)
2857 {
2858     ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server,
2859                 "%s not supported on this platform", cmd->cmd->name);
2860
2861     return NULL;
2862 }
2863 #endif
2864
2865 #ifdef RLIMIT_CPU
2866 static const char *set_limit_cpu(cmd_parms *cmd, void *conf_,
2867                                  const char *arg, const char *arg2)
2868 {
2869     core_dir_config *conf = conf_;
2870
2871     unixd_set_rlimit(cmd, &conf->limit_cpu, arg, arg2, RLIMIT_CPU);
2872     return NULL;
2873 }
2874 #endif
2875
2876 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
2877 static const char *set_limit_mem(cmd_parms *cmd, void *conf_,
2878                                  const char *arg, const char * arg2)
2879 {
2880     core_dir_config *conf = conf_;
2881
2882 #if defined(RLIMIT_AS)
2883     unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2 ,RLIMIT_AS);
2884 #elif defined(RLIMIT_DATA)
2885     unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_DATA);
2886 #elif defined(RLIMIT_VMEM)
2887     unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_VMEM);
2888 #endif
2889
2890     return NULL;
2891 }
2892 #endif
2893
2894 #ifdef RLIMIT_NPROC
2895 static const char *set_limit_nproc(cmd_parms *cmd, void *conf_,
2896                                    const char *arg, const char * arg2)
2897 {
2898     core_dir_config *conf = conf_;
2899
2900     unixd_set_rlimit(cmd, &conf->limit_nproc, arg, arg2, RLIMIT_NPROC);
2901     return NULL;
2902 }
2903 #endif
2904
2905 static const char *set_recursion_limit(cmd_parms *cmd, void *dummy,
2906                                        const char *arg1, const char *arg2)
2907 {
2908     core_server_config *conf = ap_get_module_config(cmd->server->module_config,
2909                                                     &core_module);
2910     int limit = atoi(arg1);
2911
2912     if (limit <= 0) {
2913         return "The recursion limit must be greater than zero.";
2914     }
2915     if (limit < 4) {
2916         ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2917                      "Limiting internal redirects to very low numbers may "
2918                      "cause normal requests to fail.");
2919     }
2920
2921     conf->redirect_limit = limit;
2922
2923     if (arg2) {
2924         limit = atoi(arg2);
2925
2926         if (limit <= 0) {
2927             return "The recursion limit must be greater than zero.";
2928         }
2929         if (limit < 4) {
2930             ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2931                          "Limiting the subrequest depth to a very low level may"
2932                          " cause normal requests to fail.");
2933         }
2934     }
2935
2936     conf->subreq_limit = limit;
2937
2938     return NULL;
2939 }
2940
2941 static void log_backtrace(const request_rec *r)
2942 {
2943     const request_rec *top = r;
2944
2945     ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2946                   "r->uri = %s", r->uri ? r->uri : "(unexpectedly NULL)");
2947
2948     while (top && (top->prev || top->main)) {
2949         if (top->prev) {
2950             top = top->prev;
2951             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2952                           "redirected from r->uri = %s",
2953                           top->uri ? top->uri : "(unexpectedly NULL)");
2954         }
2955
2956         if (!top->prev && top->main) {
2957             top = top->main;
2958             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
2959                           "subrequested from r->uri = %s",
2960                           top->uri ? top->uri : "(unexpectedly NULL)");
2961         }
2962     }
2963 }
2964
2965 /*
2966  * check whether redirect limit is reached
2967  */
2968 AP_DECLARE(int) ap_is_recursion_limit_exceeded(const request_rec *r)
2969 {
2970     core_server_config *conf = ap_get_module_config(r->server->module_config,
2971                                                     &core_module);
2972     const request_rec *top = r;
2973     int redirects = 0, subreqs = 0;
2974     int rlimit = conf->redirect_limit
2975                  ? conf->redirect_limit
2976                  : AP_DEFAULT_MAX_INTERNAL_REDIRECTS;
2977     int slimit = conf->subreq_limit
2978                  ? conf->subreq_limit
2979                  : AP_DEFAULT_MAX_SUBREQ_DEPTH;
2980
2981
2982     while (top->prev || top->main) {
2983         if (top->prev) {
2984             if (++redirects >= rlimit) {
2985                 /* uuh, too much. */
2986                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
2987                               "Request exceeded the limit of %d internal "
2988                               "redirects due to probable configuration error. "
2989                               "Use 'LimitInternalRecursion' to increase the "
2990                               "limit if necessary. Use 'LogLevel debug' to get "
2991                               "a backtrace.", rlimit);
2992
2993                 /* post backtrace */
2994                 log_backtrace(r);
2995
2996                 /* return failure */
2997                 return 1;
2998             }
2999
3000             top = top->prev;
3001         }
3002
3003         if (!top->prev && top->main) {
3004             if (++subreqs >= slimit) {
3005                 /* uuh, too much. */
3006                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3007                               "Request exceeded the limit of %d subrequest "
3008                               "nesting levels due to probable confguration "
3009                               "error. Use 'LimitInternalRecursion' to increase "
3010                               "the limit if necessary. Use 'LogLevel debug' to "
3011                               "get a backtrace.", slimit);
3012
3013                 /* post backtrace */
3014                 log_backtrace(r);
3015
3016                 /* return failure */
3017                 return 1;
3018             }
3019
3020             top = top->main;
3021         }
3022     }
3023
3024     /* recursion state: ok */
3025     return 0;
3026 }
3027
3028 static const char *add_ct_output_filters(cmd_parms *cmd, void *conf_,
3029                                          const char *arg, const char *arg2)
3030 {
3031     core_dir_config *conf = conf_;
3032     ap_filter_rec_t *old, *new = NULL;
3033     const char *filter_name;
3034
3035     if (!conf->ct_output_filters) {
3036         conf->ct_output_filters = apr_hash_make(cmd->pool);
3037         old = NULL;
3038     }
3039     else {
3040         old = (ap_filter_rec_t*) apr_hash_get(conf->ct_output_filters, arg2,
3041                                               APR_HASH_KEY_STRING);
3042         /* find last entry */
3043         if (old) {
3044             while (old->next) {
3045                 old = old->next;
3046             }
3047         }
3048     }
3049
3050     while (*arg &&
3051            (filter_name = ap_getword(cmd->pool, &arg, ';')) &&
3052            strcmp(filter_name, "")) {
3053         new = apr_pcalloc(cmd->pool, sizeof(ap_filter_rec_t));
3054         new->name = filter_name;
3055
3056         /* We found something, so let's append it.  */
3057         if (old) {
3058             old->next = new;
3059         }
3060         else {
3061             apr_hash_set(conf->ct_output_filters, arg2,
3062                          APR_HASH_KEY_STRING, new);
3063         }
3064         old = new;
3065     }
3066
3067     if (!new) {
3068         return "invalid filter name";
3069     }
3070
3071     return NULL;
3072 }
3073 /*
3074  * Insert filters requested by the AddOutputFilterByType
3075  * configuration directive. We cannot add filters based
3076  * on content-type until after the handler has started
3077  * to run. Only then do we reliably know the content-type.
3078  */
3079 void ap_add_output_filters_by_type(request_rec *r)
3080 {
3081     core_dir_config *conf;
3082     const char *ctype;
3083
3084     conf = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3085                                                    &core_module);
3086
3087     /* We can't do anything with no content-type or if we don't have a
3088      * filter configured.
3089      */
3090     if (!r->content_type || !conf->ct_output_filters) {
3091         return;
3092     }
3093
3094     /* remove c-t decoration */
3095     ctype = ap_field_noparam(r->pool, r->content_type);
3096     if (ctype) {
3097         ap_filter_rec_t *ct_filter;
3098         ct_filter = apr_hash_get(conf->ct_output_filters, ctype,
3099                                  APR_HASH_KEY_STRING);
3100         while (ct_filter) {
3101             ap_add_output_filter(ct_filter->name, NULL, r, r->connection);
3102             ct_filter = ct_filter->next;
3103         }
3104     }
3105
3106     return;
3107 }
3108
3109 static const char *set_trace_enable(cmd_parms *cmd, void *dummy,
3110                                     const char *arg1)
3111 {
3112     core_server_config *conf = ap_get_module_config(cmd->server->module_config,
3113                                                     &core_module);
3114
3115     if (strcasecmp(arg1, "on") == 0) {
3116         conf->trace_enable = AP_TRACE_ENABLE;
3117     }
3118     else if (strcasecmp(arg1, "off") == 0) {
3119         conf->trace_enable = AP_TRACE_DISABLE;
3120     }
3121     else if (strcasecmp(arg1, "extended") == 0) {
3122         conf->trace_enable = AP_TRACE_EXTENDED;
3123     }
3124     else {
3125         return "TraceEnable must be one of 'on', 'off', or 'extended'";
3126     }
3127
3128     return NULL;
3129 }
3130
3131 /* Note --- ErrorDocument will now work from .htaccess files.
3132  * The AllowOverride of Fileinfo allows webmasters to turn it off
3133  */
3134
3135 static const command_rec core_cmds[] = {
3136
3137 /* Old access config file commands */
3138
3139 AP_INIT_RAW_ARGS("<Directory", dirsection, NULL, RSRC_CONF,
3140   "Container for directives affecting resources located in the specified "
3141   "directories"),
3142 AP_INIT_RAW_ARGS("<Location", urlsection, NULL, RSRC_CONF,
3143   "Container for directives affecting resources accessed through the "
3144   "specified URL paths"),
3145 AP_INIT_RAW_ARGS("<VirtualHost", virtualhost_section, NULL, RSRC_CONF,
3146   "Container to map directives to a particular virtual host, takes one or "
3147   "more host addresses"),
3148 AP_INIT_RAW_ARGS("<Files", filesection, NULL, OR_ALL,
3149   "Container for directives affecting files matching specified patterns"),
3150 AP_INIT_RAW_ARGS("<Limit", ap_limit_section, NULL, OR_ALL,
3151   "Container for authentication directives when accessed using specified HTTP "
3152   "methods"),
3153 AP_INIT_RAW_ARGS("<LimitExcept", ap_limit_section, (void*)1, OR_ALL,
3154   "Container for authentication directives to be applied when any HTTP "
3155   "method other than those specified is used to access the resource"),
3156 AP_INIT_TAKE1("<IfModule", start_ifmod, NULL, EXEC_ON_READ | OR_ALL,
3157   "Container for directives based on existance of specified modules"),
3158 AP_INIT_TAKE1("<IfDefine", start_ifdefine, NULL, EXEC_ON_READ | OR_ALL,
3159   "Container for directives based on existance of command line defines"),
3160 AP_INIT_RAW_ARGS("<DirectoryMatch", dirsection, (void*)1, RSRC_CONF,
3161   "Container for directives affecting resources located in the "
3162   "specified directories"),
3163 AP_INIT_RAW_ARGS("<LocationMatch", urlsection, (void*)1, RSRC_CONF,
3164   "Container for directives affecting resources accessed through the "
3165   "specified URL paths"),
3166 AP_INIT_RAW_ARGS("<FilesMatch", filesection, (void*)1, OR_ALL,
3167   "Container for directives affecting files matching specified patterns"),
3168 #ifdef GPROF
3169 AP_INIT_TAKE1("GprofDir", set_gprof_dir, NULL, RSRC_CONF,
3170   "Directory to plop gmon.out files"),
3171 #endif
3172 AP_INIT_TAKE1("AddDefaultCharset", set_add_default_charset, NULL, OR_FILEINFO,
3173   "The name of the default charset to add to any Content-Type without one or 'Off' to disable"),
3174 AP_INIT_TAKE1("AcceptPathInfo", set_accept_path_info, NULL, OR_FILEINFO,
3175   "Set to on or off for PATH_INFO to be accepted by handlers, or default for the per-handler preference"),
3176
3177 /* Old resource config file commands */
3178
3179 AP_INIT_RAW_ARGS("AccessFileName", set_access_name, NULL, RSRC_CONF,
3180   "Name(s) of per-directory config files (default: .htaccess)"),
3181 AP_INIT_TAKE1("DocumentRoot", set_document_root, NULL, RSRC_CONF,
3182   "Root directory of the document tree"),
3183 AP_INIT_TAKE2("ErrorDocument", set_error_document, NULL, OR_FILEINFO,
3184   "Change responses for HTTP errors"),
3185 AP_INIT_RAW_ARGS("AllowOverride", set_override, NULL, ACCESS_CONF,
3186   "Controls what groups of directives can be configured by per-directory "
3187   "config files"),
3188 AP_INIT_RAW_ARGS("Options", set_options, NULL, OR_OPTIONS,
3189   "Set a number of attributes for a given directory"),
3190 AP_INIT_TAKE1("DefaultType", ap_set_string_slot,
3191   (void*)APR_OFFSETOF(core_dir_config, ap_default_type),
3192   OR_FILEINFO, "the default MIME type for untypable files"),
3193 AP_INIT_RAW_ARGS("FileETag", set_etag_bits, NULL, OR_FILEINFO,
3194   "Specify components used to construct a file's ETag"),
3195 AP_INIT_TAKE1("EnableMMAP", set_enable_mmap, NULL, OR_FILEINFO,
3196   "Controls whether memory-mapping may be used to read files"),
3197 AP_INIT_TAKE1("EnableSendfile", set_enable_sendfile, NULL, OR_FILEINFO,
3198   "Controls whether sendfile may be used to transmit files"),
3199
3200 /* Old server config file commands */
3201
3202 AP_INIT_TAKE1("Protocol", set_protocol, NULL, RSRC_CONF,
3203   "Set the Protocol for httpd to use."),
3204 AP_INIT_TAKE2("AcceptFilter", set_accf_map, NULL, RSRC_CONF,
3205   "Set the Accept Filter to use for a protocol"),
3206 AP_INIT_TAKE1("Port", ap_set_deprecated, NULL, RSRC_CONF,
3207   "Port was replaced with Listen in Apache 2.0"),
3208 AP_INIT_TAKE1("HostnameLookups", set_hostname_lookups, NULL,
3209   ACCESS_CONF|RSRC_CONF,
3210   "\"on\" to enable, \"off\" to disable reverse DNS lookups, or \"double\" to "
3211   "enable double-reverse DNS lookups"),
3212 AP_INIT_TAKE1("ServerAdmin", set_server_string_slot,
3213   (void *)APR_OFFSETOF(server_rec, server_admin), RSRC_CONF,
3214   "The email address of the server administrator"),
3215 AP_INIT_TAKE1("ServerName", server_hostname_port, NULL, RSRC_CONF,
3216   "The hostname and port of the server"),
3217 AP_INIT_TAKE1("ServerSignature", set_signature_flag, NULL, OR_ALL,
3218   "En-/disable server signature (on|off|email)"),
3219 AP_INIT_TAKE1("ServerRoot", set_server_root, NULL, RSRC_CONF | EXEC_ON_READ,
3220   "Common directory of server-related files (logs, confs, etc.)"),
3221 AP_INIT_TAKE1("ErrorLog", set_server_string_slot,
3222   (void *)APR_OFFSETOF(server_rec, error_fname), RSRC_CONF,
3223   "The filename of the error log"),
3224 AP_INIT_RAW_ARGS("ServerAlias", set_server_alias, NULL, RSRC_CONF,
3225   "A name or names alternately used to access the server"),
3226 AP_INIT_TAKE1("ServerPath", set_serverpath, NULL, RSRC_CONF,
3227   "The pathname the server can be reached at"),
3228 AP_INIT_TAKE1("Timeout", set_timeout, NULL, RSRC_CONF,
3229   "Timeout duration (sec)"),
3230 AP_INIT_FLAG("ContentDigest", set_content_md5, NULL, OR_OPTIONS,
3231   "whether or not to send a Content-MD5 header with each request"),
3232 AP_INIT_TAKE1("UseCanonicalName", set_use_canonical_name, NULL,
3233   RSRC_CONF|ACCESS_CONF,
3234   "How to work out the ServerName : Port when constructing URLs"),
3235 AP_INIT_TAKE1("UseCanonicalPhysicalPort", set_use_canonical_phys_port, NULL,
3236   RSRC_CONF|ACCESS_CONF,
3237   "Whether to use the physical Port when constructing URLs"),
3238 /* TODO: RlimitFoo should all be part of mod_cgi, not in the core */
3239 /* TODO: ListenBacklog in MPM */
3240 AP_INIT_TAKE1("Include", include_config, NULL,
3241   (RSRC_CONF | ACCESS_CONF | EXEC_ON_READ),
3242   "Name of the config file to be included"),
3243 AP_INIT_TAKE1("LogLevel", set_loglevel, NULL, RSRC_CONF,
3244   "Level of verbosity in error logging"),
3245 AP_INIT_TAKE1("NameVirtualHost", ap_set_name_virtual_host, NULL, RSRC_CONF,
3246   "A numeric IP address:port, or the name of a host"),
3247 AP_INIT_TAKE1("ServerTokens", set_serv_tokens, NULL, RSRC_CONF,
3248   "Determine tokens displayed in the Server: header - Min(imal), OS or Full"),
3249 AP_INIT_TAKE1("LimitRequestLine", set_limit_req_line, NULL, RSRC_CONF,
3250   "Limit on maximum size of an HTTP request line"),
3251 AP_INIT_TAKE1("LimitRequestFieldsize", set_limit_req_fieldsize, NULL,
3252   RSRC_CONF,
3253   "Limit on maximum size of an HTTP request header field"),
3254 AP_INIT_TAKE1("LimitRequestFields", set_limit_req_fields, NULL, RSRC_CONF,
3255   "Limit (0 = unlimited) on max number of header fields in a request message"),
3256 AP_INIT_TAKE1("LimitRequestBody", set_limit_req_body,
3257   (void*)APR_OFFSETOF(core_dir_config, limit_req_body), OR_ALL,
3258   "Limit (in bytes) on maximum size of request message body"),
3259 AP_INIT_TAKE1("LimitXMLRequestBody", set_limit_xml_req_body, NULL, OR_ALL,
3260               "Limit (in bytes) on maximum size of an XML-based request "
3261               "body"),
3262
3263 /* System Resource Controls */
3264 #ifdef RLIMIT_CPU
3265 AP_INIT_TAKE12("RLimitCPU", set_limit_cpu,
3266   (void*)APR_OFFSETOF(core_dir_config, limit_cpu),
3267   OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3268 #else
3269 AP_INIT_TAKE12("RLimitCPU", no_set_limit, NULL,
3270   OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
3271 #endif
3272 #if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined (RLIMIT_AS)
3273 AP_INIT_TAKE12("RLimitMEM", set_limit_mem,
3274   (void*)APR_OFFSETOF(core_dir_config, limit_mem),
3275   OR_ALL, "Soft/hard limits for max memory usage per process"),
3276 #else
3277 AP_INIT_TAKE12("RLimitMEM", no_set_limit, NULL,
3278   OR_ALL, "Soft/hard limits for max memory usage per process"),
3279 #endif
3280 #ifdef RLIMIT_NPROC
3281 AP_INIT_TAKE12("RLimitNPROC", set_limit_nproc,
3282   (void*)APR_OFFSETOF(core_dir_config, limit_nproc),
3283   OR_ALL, "soft/hard limits for max number of processes per uid"),
3284 #else
3285 AP_INIT_TAKE12("RLimitNPROC", no_set_limit, NULL,
3286    OR_ALL, "soft/hard limits for max number of processes per uid"),
3287 #endif
3288
3289 /* internal recursion stopper */
3290 AP_INIT_TAKE12("LimitInternalRecursion", set_recursion_limit, NULL, RSRC_CONF,
3291               "maximum recursion depth of internal redirects and subrequests"),
3292
3293 AP_INIT_TAKE1("ForceType", ap_set_string_slot_lower,
3294        (void *)APR_OFFSETOF(core_dir_config, mime_type), OR_FILEINFO,
3295      "a mime type that overrides other configured type"),
3296 AP_INIT_TAKE1("SetHandler", ap_set_string_slot_lower,
3297        (void *)APR_OFFSETOF(core_dir_config, handler), OR_FILEINFO,
3298    "a handler name that overrides any other configured handler"),
3299 AP_INIT_TAKE1("SetOutputFilter", ap_set_string_slot,
3300        (void *)APR_OFFSETOF(core_dir_config, output_filters), OR_FILEINFO,
3301    "filter (or ; delimited list of filters) to be run on the request content"),
3302 AP_INIT_TAKE1("SetInputFilter", ap_set_string_slot,
3303        (void *)APR_OFFSETOF(core_dir_config, input_filters), OR_FILEINFO,
3304    "filter (or ; delimited list of filters) to be run on the request body"),
3305 AP_INIT_ITERATE2("AddOutputFilterByType", add_ct_output_filters,
3306        (void *)APR_OFFSETOF(core_dir_config, ct_output_filters), OR_FILEINFO,
3307      "output filter name followed by one or more content-types"),
3308 AP_INIT_FLAG("AllowEncodedSlashes", set_allow2f, NULL, RSRC_CONF,
3309              "Allow URLs containing '/' encoded as '%2F'"),
3310
3311 /*
3312  * These are default configuration directives that mpms can/should
3313  * pay attention to. If an mpm wishes to use these, they should
3314  * #defined them in mpm.h.
3315  */
3316 #ifdef AP_MPM_WANT_SET_PIDFILE
3317 AP_INIT_TAKE1("PidFile",  ap_mpm_set_pidfile, NULL, RSRC_CONF,
3318               "A file for logging the server process ID"),
3319 #endif
3320 #ifdef AP_MPM_WANT_SET_SCOREBOARD
3321 AP_INIT_TAKE1("ScoreBoardFile", ap_mpm_set_scoreboard, NULL, RSRC_CONF,
3322               "A file for Apache to maintain runtime process management information"),
3323 #endif
3324 #ifdef AP_MPM_WANT_SET_LOCKFILE
3325 AP_INIT_TAKE1("LockFile",  ap_mpm_set_lockfile, NULL, RSRC_CONF,
3326               "The lockfile used when Apache needs to lock the accept() call (deprecated)"),
3327 #endif
3328 #ifdef AP_MPM_WANT_SET_MAX_REQUESTS
3329 AP_INIT_TAKE1("MaxRequestsPerChild", ap_mpm_set_max_requests, NULL, RSRC_CONF,
3330               "Maximum number of requests a particular child serves before dying."),
3331 #endif
3332 #ifdef AP_MPM_WANT_SET_COREDUMPDIR
3333 AP_INIT_TAKE1("CoreDumpDirectory", ap_mpm_set_coredumpdir, NULL, RSRC_CONF,
3334               "The location of the directory Apache changes to before dumping core"),
3335 #endif
3336 #ifdef AP_MPM_WANT_SET_ACCEPT_LOCK_MECH
3337 AP_INIT_TAKE1("AcceptMutex", ap_mpm_set_accept_lock_mech, NULL, RSRC_CONF,
3338               ap_available_mutexes_string),
3339 #endif
3340 #ifdef AP_MPM_WANT_SET_MAX_MEM_FREE
3341 AP_INIT_TAKE1("MaxMemFree", ap_mpm_set_max_mem_free, NULL, RSRC_CONF,
3342               "Maximum number of 1k blocks a particular childs allocator may hold."),
3343 #endif
3344 #ifdef AP_MPM_WANT_SET_STACKSIZE
3345 AP_INIT_TAKE1("ThreadStackSize", ap_mpm_set_thread_stacksize, NULL, RSRC_CONF,
3346               "Size in bytes of stack used by threads handling client connections"),
3347 #endif
3348 #if AP_ENABLE_EXCEPTION_HOOK
3349 AP_INIT_TAKE1("EnableExceptionHook", ap_mpm_set_exception_hook, NULL, RSRC_CONF,
3350               "Controls whether exception hook may be called after a crash"),
3351 #endif
3352 AP_INIT_TAKE1("TraceEnable", set_trace_enable, NULL, RSRC_CONF,
3353               "'on' (default), 'off' or 'extended' to trace request body content"),
3354 { NULL }
3355 };
3356
3357 /*****************************************************************
3358  *
3359  * Core handlers for various phases of server operation...
3360  */
3361
3362 AP_DECLARE_NONSTD(int) ap_core_translate(request_rec *r)
3363 {
3364     void *sconf = r->server->module_config;
3365     core_server_config *conf = ap_get_module_config(sconf, &core_module);
3366     apr_status_t rv;
3367
3368     /* XXX this seems too specific, this should probably become
3369      * some general-case test
3370      */
3371     if (r->proxyreq) {
3372         return HTTP_FORBIDDEN;
3373     }
3374     if (!r->uri || ((r->uri[0] != '/') && strcmp(r->uri, "*"))) {
3375         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3376                      "Invalid URI in request %s", r->the_request);
3377         return HTTP_BAD_REQUEST;
3378     }
3379
3380     if (r->server->path
3381         && !strncmp(r->uri, r->server->path, r->server->pathlen)
3382         && (r->server->path[r->server->pathlen - 1] == '/'
3383             || r->uri[r->server->pathlen] == '/'
3384             || r->uri[r->server->pathlen] == '\0'))
3385     {
3386         /* skip all leading /'s (e.g. http://localhost///foo)
3387          * so we are looking at only the relative path.
3388          */
3389         char *path = r->uri + r->server->pathlen;
3390         while (*path == '/') {
3391             ++path;
3392         }
3393         if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3394                                      APR_FILEPATH_TRUENAME
3395                                    | APR_FILEPATH_SECUREROOT, r->pool))
3396                     != APR_SUCCESS) {
3397             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3398                          "Cannot map %s to file", r->the_request);
3399             return HTTP_FORBIDDEN;
3400         }
3401         r->canonical_filename = r->filename;
3402     }
3403     else {
3404         /*
3405          * Make sure that we do not mess up the translation by adding two
3406          * /'s in a row.  This happens under windows when the document
3407          * root ends with a /
3408          */
3409         /* skip all leading /'s (e.g. http://localhost///foo)
3410          * so we are looking at only the relative path.
3411          */
3412         char *path = r->uri;
3413         while (*path == '/') {
3414             ++path;
3415         }
3416         if ((rv = apr_filepath_merge(&r->filename, conf->ap_document_root, path,
3417                                      APR_FILEPATH_TRUENAME
3418                                    | APR_FILEPATH_SECUREROOT, r->pool))
3419                     != APR_SUCCESS) {
3420             ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
3421                          "Cannot map %s to file", r->the_request);
3422             return HTTP_FORBIDDEN;
3423         }
3424         r->canonical_filename = r->filename;
3425     }
3426
3427     return OK;
3428 }
3429
3430 /*****************************************************************
3431  *
3432  * Test the filesystem name through directory_walk and file_walk
3433  */
3434 static int core_map_to_storage(request_rec *r)
3435 {
3436     int access_status;
3437
3438     if ((access_status = ap_directory_walk(r))) {
3439         return access_status;
3440     }
3441
3442     if ((access_status = ap_file_walk(r))) {
3443         return access_status;
3444     }
3445
3446     return OK;
3447 }
3448
3449
3450 static int do_nothing(request_rec *r) { return OK; }
3451
3452
3453 static int core_override_type(request_rec *r)
3454 {
3455     core_dir_config *conf =
3456         (core_dir_config *)ap_get_module_config(r->per_dir_config,
3457                                                 &core_module);
3458
3459     /* Check for overrides with ForceType / SetHandler
3460      */
3461     if (conf->mime_type && strcmp(conf->mime_type, "none"))
3462         ap_set_content_type(r, (char*) conf->mime_type);
3463
3464     if (conf->handler && strcmp(conf->handler, "none"))
3465         r->handler = conf->handler;
3466
3467     /* Deal with the poor soul who is trying to force path_info to be
3468      * accepted within the core_handler, where they will let the subreq
3469      * address its contents.  This is toggled by the user in the very
3470      * beginning of the fixup phase, so modules should override the user's
3471      * discretion in their own module fixup phase.  It is tristate, if
3472      * the user doesn't specify, the result is 2 (which the module may
3473      * interpret to its own customary behavior.)  It won't be touched
3474      * if the value is no longer undefined (2), so any module changing
3475      * the value prior to the fixup phase OVERRIDES the user's choice.
3476      */
3477     if ((r->used_path_info == AP_REQ_DEFAULT_PATH_INFO)
3478         && (conf->accept_path_info != 3)) {
3479         r->used_path_info = conf->accept_path_info;
3480     }
3481
3482     return OK;
3483 }
3484
3485 static int default_handler(request_rec *r)
3486 {
3487     conn_rec *c = r->connection;
3488     apr_bucket_brigade *bb;
3489     apr_bucket *e;
3490     core_dir_config *d;
3491     int errstatus;
3492     apr_file_t *fd = NULL;
3493     apr_status_t status;
3494     /* XXX if/when somebody writes a content-md5 filter we either need to
3495      *     remove this support or coordinate when to use the filter vs.
3496      *     when to use this code
3497      *     The current choice of when to compute the md5 here matches the 1.3
3498      *     support fairly closely (unlike 1.3, we don't handle computing md5
3499      *     when the charset is translated).
3500      */
3501     int bld_content_md5;
3502
3503     d = (core_dir_config *)ap_get_module_config(r->per_dir_config,
3504                                                 &core_module);
3505     bld_content_md5 = (d->content_md5 & 1)
3506                       && r->output_filters->frec->ftype != AP_FTYPE_RESOURCE;
3507
3508     ap_allow_standard_methods(r, MERGE_ALLOW, M_GET, M_OPTIONS, M_POST, -1);
3509
3510     /* If filters intend to consume the request body, they must
3511      * register an InputFilter to slurp the contents of the POST
3512      * data from the POST input stream.  It no longer exists when
3513      * the output filters are invoked by the default handler.
3514      */
3515     if ((errstatus = ap_discard_request_body(r)) != OK) {
3516         return errstatus;
3517     }
3518
3519     if (r->method_number == M_GET || r->method_number == M_POST) {
3520         if (r->finfo.filetype == 0) {
3521             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3522                           "File does not exist: %s", r->filename);
3523             return HTTP_NOT_FOUND;
3524         }
3525
3526         /* Don't try to serve a dir.  Some OSs do weird things with
3527          * raw I/O on a dir.
3528          */
3529         if (r->finfo.filetype == APR_DIR) {
3530             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3531                           "Attempt to serve directory: %s", r->filename);
3532             return HTTP_NOT_FOUND;
3533         }
3534
3535         if ((r->used_path_info != AP_REQ_ACCEPT_PATH_INFO) &&
3536             r->path_info && *r->path_info)
3537         {
3538             /* default to reject */
3539             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3540                           "File does not exist: %s",
3541                           apr_pstrcat(r->pool, r->filename, r->path_info, NULL));
3542             return HTTP_NOT_FOUND;
3543         }
3544
3545         /* We understood the (non-GET) method, but it might not be legal for
3546            this particular resource. Check to see if the 'deliver_script'
3547            flag is set. If so, then we go ahead and deliver the file since
3548            it isn't really content (only GET normally returns content).
3549
3550            Note: based on logic further above, the only possible non-GET
3551            method at this point is POST. In the future, we should enable
3552            script delivery for all methods.  */
3553         if (r->method_number != M_GET) {
3554             core_request_config *req_cfg;
3555
3556             req_cfg = ap_get_module_config(r->request_config, &core_module);
3557             if (!req_cfg->deliver_script) {
3558                 /* The flag hasn't been set for this request. Punt. */
3559                 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3560                               "This resource does not accept the %s method.",
3561                               r->method);
3562                 return HTTP_METHOD_NOT_ALLOWED;
3563             }
3564         }
3565
3566
3567         if ((status = apr_file_open(&fd, r->filename, APR_READ | APR_BINARY
3568 #if APR_HAS_SENDFILE
3569                             | ((d->enable_sendfile == ENABLE_SENDFILE_OFF)
3570                                                 ? 0 : APR_SENDFILE_ENABLED)
3571 #endif
3572                                     , 0, r->pool)) != APR_SUCCESS) {
3573             ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r,
3574                           "file permissions deny server access: %s", r->filename);
3575             return HTTP_FORBIDDEN;
3576         }
3577
3578         ap_update_mtime(r, r->finfo.mtime);
3579         ap_set_last_modified(r);
3580         ap_set_etag(r);
3581         apr_table_setn(r->headers_out, "Accept-Ranges", "bytes");
3582         ap_set_content_length(r, r->finfo.size);
3583
3584         bb = apr_brigade_create(r->pool, c->bucket_alloc);
3585
3586         if ((errstatus = ap_meets_conditions(r)) != OK) {
3587             apr_file_close(fd);
3588             r->status = errstatus;
3589         }
3590         else {
3591             if (bld_content_md5) {
3592                 apr_table_setn(r->headers_out, "Content-MD5",
3593                                ap_md5digest(r->pool, fd));
3594             }
3595
3596             e = apr_brigade_insert_file(bb, fd, 0, r->finfo.size, r->pool);
3597
3598 #if APR_HAS_MMAP
3599             if (d->enable_mmap == ENABLE_MMAP_OFF) {
3600                 (void)apr_bucket_file_enable_mmap(e, 0);
3601             }
3602 #endif
3603         }
3604
3605         e = apr_bucket_eos_create(c->bucket_alloc);
3606         APR_BRIGADE_INSERT_TAIL(bb, e);
3607
3608         status = ap_pass_brigade(r->output_filters, bb);
3609         if (status == APR_SUCCESS
3610             || r->status != HTTP_OK
3611             || c->aborted) {
3612             return OK;
3613         }
3614         else {
3615             /* no way to know what type of error occurred */
3616             ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r,
3617                           "default_handler: ap_pass_brigade returned %i",
3618                           status);
3619             return HTTP_INTERNAL_SERVER_ERROR;
3620         }
3621     }
3622     else {              /* unusual method (not GET or POST) */
3623         if (r->method_number == M_INVALID) {
3624             ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
3625                           "Invalid method in request %s", r->the_request);
3626             return HTTP_NOT_IMPLEMENTED;
3627         }
3628
3629         if (r->method_number == M_OPTIONS) {
3630             return ap_send_http_options(r);
3631         }
3632         return HTTP_METHOD_NOT_ALLOWED;
3633     }
3634 }
3635
3636 /* Optional function coming from mod_logio, used for logging of output
3637  * traffic
3638  */
3639 APR_OPTIONAL_FN_TYPE(ap_logio_add_bytes_out) *logio_add_bytes_out;
3640 APR_OPTIONAL_FN_TYPE(authz_some_auth_required) *authz_ap_some_auth_required;
3641
3642 static int core_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
3643 {
3644     logio_add_bytes_out = APR_RETRIEVE_OPTIONAL_FN(ap_logio_add_bytes_out);
3645     ident_lookup = APR_RETRIEVE_OPTIONAL_FN(ap_ident_lookup);
3646     authz_ap_some_auth_required = APR_RETRIEVE_OPTIONAL_FN(authz_some_auth_required);
3647     authn_ap_auth_type = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_type);
3648     authn_ap_auth_name = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_name);
3649
3650     set_banner(pconf);
3651     ap_setup_make_content_type(pconf);
3652     return OK;
3653 }
3654
3655 static void core_insert_filter(request_rec *r)
3656 {
3657     core_dir_config *conf = (core_dir_config *)
3658                             ap_get_module_config(r->per_dir_config,
3659                                                  &core_module);
3660     const char *filter, *filters = conf->output_filters;
3661
3662     if (filters) {
3663         while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3664             ap_add_output_filter(filter, NULL, r, r->connection);
3665         }
3666     }
3667
3668     filters = conf->input_filters;
3669     if (filters) {
3670         while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
3671             ap_add_input_filter(filter, NULL, r, r->connection);
3672         }
3673     }
3674 }
3675
3676 static apr_size_t num_request_notes = AP_NUM_STD_NOTES;
3677
3678 static apr_status_t reset_request_notes(void *dummy)
3679 {
3680     num_request_notes = AP_NUM_STD_NOTES;
3681     return APR_SUCCESS;
3682 }
3683
3684 AP_DECLARE(apr_size_t) ap_register_request_note(void)
3685 {
3686     apr_pool_cleanup_register(apr_hook_global_pool, NULL, reset_request_notes,
3687                               apr_pool_cleanup_null);
3688     return num_request_notes++;
3689 }
3690
3691 AP_DECLARE(void **) ap_get_request_note(request_rec *r, apr_size_t note_num)
3692 {
3693     core_request_config *req_cfg;
3694
3695     if (note_num >= num_request_notes) {
3696         return NULL;
3697     }
3698
3699     req_cfg = (core_request_config *)
3700         ap_get_module_config(r->request_config, &core_module);
3701
3702     if (!req_cfg) {
3703         return NULL;
3704     }
3705
3706     return &(req_cfg->notes[note_num]);
3707 }
3708
3709 static int core_create_req(request_rec *r)
3710 {
3711     /* Alloc the config struct and the array of request notes in
3712      * a single block for efficiency
3713      */
3714     core_request_config *req_cfg;
3715
3716     req_cfg = apr_pcalloc(r->pool, sizeof(core_request_config) +
3717                           sizeof(void *) * num_request_notes);
3718     req_cfg->notes = (void **)((char *)req_cfg + sizeof(core_request_config));
3719
3720     /* ### temporarily enable script delivery as the default */
3721     req_cfg->deliver_script = 1;
3722
3723     if (r->main) {
3724         core_request_config *main_req_cfg = (core_request_config *)
3725             ap_get_module_config(r->main->request_config, &core_module);
3726         req_cfg->bb = main_req_cfg->bb;
3727     }
3728     else {
3729         req_cfg->bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
3730     }
3731
3732     ap_set_module_config(r->request_config, &core_module, req_cfg);
3733
3734     return OK;
3735 }
3736
3737 static int core_create_proxy_req(request_rec *r, request_rec *pr)
3738 {
3739     return core_create_req(pr);
3740 }
3741
3742 static conn_rec *core_create_conn(apr_pool_t *ptrans, server_rec *server,
3743                                   apr_socket_t *csd, long id, void *sbh,
3744                                   apr_bucket_alloc_t *alloc)
3745 {
3746     apr_status_t rv;
3747     conn_rec *c = (conn_rec *) apr_pcalloc(ptrans, sizeof(conn_rec));
3748
3749     c->sbh = sbh;
3750     (void)ap_update_child_status(c->sbh, SERVER_BUSY_READ, (request_rec *)NULL);
3751
3752     /* Got a connection structure, so initialize what fields we can
3753      * (the rest are zeroed out by pcalloc).
3754      */
3755     c->conn_config = ap_create_conn_config(ptrans);
3756     c->notes = apr_table_make(ptrans, 5);
3757
3758     c->pool = ptrans;
3759     if ((rv = apr_socket_addr_get(&c->local_addr, APR_LOCAL, csd))
3760         != APR_SUCCESS) {
3761         ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3762                      "apr_socket_addr_get(APR_LOCAL)");
3763         apr_socket_close(csd);
3764         return NULL;
3765     }
3766
3767     apr_sockaddr_ip_get(&c->local_ip, c->local_addr);
3768     if ((rv = apr_socket_addr_get(&c->remote_addr, APR_REMOTE, csd))
3769         != APR_SUCCESS) {
3770         ap_log_error(APLOG_MARK, APLOG_INFO, rv, server,
3771                      "apr_socket_addr_get(APR_REMOTE)");
3772         apr_socket_close(csd);
3773         return NULL;
3774     }
3775
3776     apr_sockaddr_ip_get(&c->remote_ip, c->remote_addr);
3777     c->base_server = server;
3778
3779     c->id = id;
3780     c->bucket_alloc = alloc;
3781
3782     c->cs = (conn_state_t *)apr_pcalloc(ptrans, sizeof(conn_state_t));
3783     APR_RING_INIT(&(c->cs->timeout_list), conn_state_t, timeout_list);
3784     c->cs->expiration_time = 0;
3785     c->cs->state = CONN_STATE_CHECK_REQUEST_LINE_READABLE;
3786     c->cs->c = c;
3787     c->cs->p = ptrans;
3788     c->cs->bucket_alloc = alloc;
3789
3790     return c;
3791 }
3792
3793 static int core_pre_connection(conn_rec *c, void *csd)
3794 {
3795     core_net_rec *net = apr_palloc(c->pool, sizeof(*net));
3796     apr_status_t rv;
3797
3798 #ifdef AP_MPM_DISABLE_NAGLE_ACCEPTED_SOCK
3799     /* The Nagle algorithm says that we should delay sending partial
3800      * packets in hopes of getting more data.  We don't want to do
3801      * this; we are not telnet.  There are bad interactions between
3802      * persistent connections and Nagle's algorithm that have very severe
3803      * performance penalties.  (Failing to disable Nagle is not much of a
3804      * problem with simple HTTP.)
3805      */
3806     rv = apr_socket_opt_set(csd, APR_TCP_NODELAY, 1);
3807     if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
3808         /* expected cause is that the client disconnected already,
3809          * hence the debug level
3810          */
3811         ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c,
3812                       "apr_socket_opt_set(APR_TCP_NODELAY)");
3813     }
3814 #endif
3815
3816     /* The core filter requires the timeout mode to be set, which
3817      * incidentally sets the socket to be nonblocking.  If this
3818      * is not initialized correctly, Linux - for example - will
3819      * be initially blocking, while Solaris will be non blocking
3820      * and any initial read will fail.
3821      */
3822     rv = apr_socket_timeout_set(csd, c->base_server->timeout);
3823     if (rv != APR_SUCCESS) {
3824         /* expected cause is that the client disconnected already */
3825         ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c,
3826                       "apr_socket_timeout_set");
3827     }
3828
3829     net->c = c;
3830     net->in_ctx = NULL;
3831     net->out_ctx = NULL;
3832     net->client_socket = csd;
3833
3834     ap_set_module_config(net->c->conn_config, &core_module, csd);
3835     ap_add_input_filter_handle(ap_core_input_filter_handle, net, NULL, net->c);
3836     ap_add_output_filter_handle(ap_core_output_filter_handle, net, NULL, net->c);
3837     return DONE;
3838 }
3839
3840 static void register_hooks(apr_pool_t *p)
3841 {
3842     /* create_connection and install_transport_filters are
3843      * hooks that should always be APR_HOOK_REALLY_LAST to give other
3844      * modules the opportunity to install alternate network transports
3845      * and stop other functions from being run.
3846      */
3847     ap_hook_create_connection(core_create_conn, NULL, NULL,
3848                               APR_HOOK_REALLY_LAST);
3849     ap_hook_pre_connection(core_pre_connection, NULL, NULL,
3850                            APR_HOOK_REALLY_LAST);
3851
3852     ap_hook_post_config(core_post_config,NULL,NULL,APR_HOOK_REALLY_FIRST);
3853     ap_hook_translate_name(ap_core_translate,NULL,NULL,APR_HOOK_REALLY_LAST);
3854     ap_hook_map_to_storage(core_map_to_storage,NULL,NULL,APR_HOOK_REALLY_LAST);
3855     ap_hook_open_logs(ap_open_logs,NULL,NULL,APR_HOOK_REALLY_FIRST);
3856     ap_hook_child_init(ap_logs_child_init,NULL,NULL,APR_HOOK_MIDDLE);
3857     ap_hook_handler(default_handler,NULL,NULL,APR_HOOK_REALLY_LAST);
3858     /* FIXME: I suspect we can eliminate the need for these do_nothings - Ben */
3859     ap_hook_type_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
3860     ap_hook_fixups(core_override_type,NULL,NULL,APR_HOOK_REALLY_FIRST);
3861     ap_hook_access_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
3862     ap_hook_create_request(core_create_req, NULL, NULL, APR_HOOK_MIDDLE);
3863     APR_OPTIONAL_HOOK(proxy, create_req, core_create_proxy_req, NULL, NULL,
3864                       APR_HOOK_MIDDLE);
3865     ap_hook_pre_mpm(ap_create_scoreboard, NULL, NULL, APR_HOOK_MIDDLE);
3866
3867     /* register the core's insert_filter hook and register core-provided
3868      * filters
3869      */
3870     ap_hook_insert_filter(core_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
3871
3872     ap_core_input_filter_handle =
3873         ap_register_input_filter("CORE_IN", ap_core_input_filter,
3874                                  NULL, AP_FTYPE_NETWORK);
3875     ap_content_length_filter_handle =
3876         ap_register_output_filter("CONTENT_LENGTH", ap_content_length_filter,
3877                                   NULL, AP_FTYPE_PROTOCOL);
3878     ap_core_output_filter_handle =
3879         ap_register_output_filter("CORE", ap_core_output_filter,
3880                                   NULL, AP_FTYPE_NETWORK);
3881     ap_subreq_core_filter_handle =
3882         ap_register_output_filter("SUBREQ_CORE", ap_sub_req_output_filter,
3883                                   NULL, AP_FTYPE_CONTENT_SET);
3884     ap_old_write_func =
3885         ap_register_output_filter("OLD_WRITE", ap_old_write_filter,
3886                                   NULL, AP_FTYPE_RESOURCE - 10);
3887 }
3888
3889 AP_DECLARE_DATA module core_module = {
3890     STANDARD20_MODULE_STUFF,
3891     create_core_dir_config,       /* create per-directory config structure */
3892     merge_core_dir_configs,       /* merge per-directory config structures */
3893     create_core_server_config,    /* create per-server config structure */
3894     merge_core_server_configs,    /* merge per-server config structures */
3895     core_cmds,                    /* command apr_table_t */
3896     register_hooks                /* register hooks */
3897 };
3898