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