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