Coverage Report

Created: 2023-06-07 06:38

/src/httpd/server/core.c
Line
Count
Source (jump to first uncovered line)
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_random.h"
24
25
#include "apr_version.h"
26
#if APR_MAJOR_VERSION < 2
27
#include "apu_version.h"
28
#endif
29
30
#define APR_WANT_IOVEC
31
#define APR_WANT_STRFUNC
32
#define APR_WANT_MEMFUNC
33
#include "apr_want.h"
34
35
#include "ap_config.h"
36
#include "httpd.h"
37
#include "http_config.h"
38
#include "http_core.h"
39
#include "http_protocol.h" /* For index_of_response().  Grump. */
40
#include "http_request.h"
41
#include "http_ssl.h"
42
#include "http_vhost.h"
43
#include "http_main.h"     /* For the default_handler below... */
44
#include "http_log.h"
45
#include "http_connection.h"
46
#include "apr_buckets.h"
47
#include "util_filter.h"
48
#include "util_ebcdic.h"
49
#include "util_mutex.h"
50
#include "util_time.h"
51
#include "mpm_common.h"
52
#include "scoreboard.h"
53
#include "mod_core.h"
54
#include "mod_proxy.h"
55
#include "ap_listen.h"
56
#include "ap_provider.h"
57
#include "ap_regex.h"
58
#include "core.h"
59
60
#include "mod_so.h" /* for ap_find_loaded_module_symbol */
61
62
#if defined(RLIMIT_CPU) || defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS) || defined (RLIMIT_NPROC)
63
#include "unixd.h"
64
#endif
65
#if APR_HAVE_UNISTD_H
66
#include <unistd.h>
67
#endif
68
69
/* LimitRequestBody handling */
70
0
#define AP_LIMIT_REQ_BODY_UNSET         ((apr_off_t) -1)
71
0
#define AP_DEFAULT_LIMIT_REQ_BODY       ((apr_off_t) 1<<30) /* 1GB */
72
73
/* LimitXMLRequestBody handling */
74
0
#define AP_LIMIT_UNSET                  ((long) -1)
75
0
#define AP_DEFAULT_LIMIT_XML_BODY       ((apr_size_t)1000000)
76
/* Hard limit for ap_escape_html2() */
77
0
#define AP_MAX_LIMIT_XML_BODY           ((apr_size_t)(APR_SIZE_MAX / 6 - 1))
78
79
#define AP_MIN_SENDFILE_BYTES           (256)
80
81
/* maximum include nesting level */
82
#ifndef AP_MAX_INCLUDE_DEPTH
83
0
#define AP_MAX_INCLUDE_DEPTH            (128)
84
#endif
85
86
/* valid in core-conf, but not in runtime r->used_path_info */
87
0
#define AP_ACCEPT_PATHINFO_UNSET 3
88
89
0
#define AP_FLUSH_MAX_THRESHOLD 65535
90
0
#define AP_FLUSH_MAX_PIPELINED 4
91
92
APR_HOOK_STRUCT(
93
    APR_HOOK_LINK(get_mgmt_items)
94
    APR_HOOK_LINK(insert_network_bucket)
95
)
96
97
AP_IMPLEMENT_HOOK_RUN_ALL(int, get_mgmt_items,
98
                          (apr_pool_t *p, const char *val, apr_hash_t *ht),
99
                          (p, val, ht), OK, DECLINED)
100
101
AP_IMPLEMENT_HOOK_RUN_FIRST(apr_status_t, insert_network_bucket,
102
                            (conn_rec *c, apr_bucket_brigade *bb,
103
                             apr_socket_t *socket),
104
                            (c, bb, socket), AP_DECLINED)
105
106
/* Server core module... This module provides support for really basic
107
 * server operations, including options and commands which control the
108
 * operation of other modules.  Consider this the bureaucracy module.
109
 *
110
 * The core module also defines handlers, etc., to handle just enough
111
 * to allow a server with the core module ONLY to actually serve documents.
112
 *
113
 * This file could almost be mod_core.c, except for the stuff which affects
114
 * the http_conf_globals.
115
 */
116
117
/* we know core's module_index is 0 */
118
#undef APLOG_MODULE_INDEX
119
0
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
120
121
/* Handles for core filters */
122
AP_DECLARE_DATA ap_filter_rec_t *ap_subreq_core_filter_handle;
123
AP_DECLARE_DATA ap_filter_rec_t *ap_core_output_filter_handle;
124
AP_DECLARE_DATA ap_filter_rec_t *ap_content_length_filter_handle;
125
AP_DECLARE_DATA ap_filter_rec_t *ap_core_input_filter_handle;
126
127
/* Provide ap_document_root_check storage and default value = true */
128
AP_DECLARE_DATA int ap_document_root_check = 1;
129
130
/* magic pointer for ErrorDocument xxx "default" */
131
static char errordocument_default;
132
133
/* Global state allocated out of pconf: variables here MUST be
134
 * cleared/reset in reset_config(), a pconf cleanup, to avoid the
135
 * variable getting reused after the pool is cleared. */
136
static apr_array_header_t *saved_server_config_defines = NULL;
137
static apr_table_t *server_config_defined_vars = NULL;
138
AP_DECLARE_DATA const char *ap_runtime_dir = NULL;
139
static const char *core_state_dir;
140
141
AP_DECLARE_DATA int ap_main_state = AP_SQ_MS_INITIAL_STARTUP;
142
AP_DECLARE_DATA int ap_run_mode = AP_SQ_RM_UNKNOWN;
143
AP_DECLARE_DATA int ap_config_generation = 0;
144
145
typedef struct {
146
    apr_ipsubnet_t *subnet;
147
    struct ap_logconf log;
148
} conn_log_config;
149
150
static apr_socket_t *dummy_socket;
151
152
static void *create_core_dir_config(apr_pool_t *a, char *dir)
153
0
{
154
0
    core_dir_config *conf;
155
156
0
    conf = (core_dir_config *)apr_pcalloc(a, sizeof(core_dir_config));
157
158
    /* conf->r and conf->d[_*] are initialized by dirsection() or left NULL */
159
160
0
    conf->opts = dir ? OPT_UNSET : OPT_UNSET|OPT_SYM_LINKS;
161
0
    conf->opts_add = conf->opts_remove = OPT_NONE;
162
0
    conf->override = OR_UNSET|OR_NONE;
163
0
    conf->override_opts = OPT_UNSET | OPT_ALL | OPT_SYM_OWNER | OPT_MULTI;
164
165
0
    conf->accept_path_info = AP_ACCEPT_PATHINFO_UNSET;
166
167
0
    conf->use_canonical_name = USE_CANONICAL_NAME_UNSET;
168
0
    conf->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_UNSET;
169
170
0
    conf->hostname_lookups = HOSTNAME_LOOKUP_UNSET;
171
172
    /*
173
     * left as NULL (we use apr_pcalloc):
174
     * conf->limit_cpu = NULL;
175
     * conf->limit_mem = NULL;
176
     * conf->limit_nproc = NULL;
177
     * conf->sec_file = NULL;
178
     * conf->sec_if   = NULL;
179
     */
180
181
0
    conf->limit_req_body = AP_LIMIT_REQ_BODY_UNSET;
182
0
    conf->limit_xml_body = AP_LIMIT_UNSET;
183
184
0
    conf->server_signature = srv_sig_unset;
185
186
0
    conf->add_default_charset = ADD_DEFAULT_CHARSET_UNSET;
187
0
    conf->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
188
189
    /* Overriding all negotiation
190
     * Set NULL by apr_pcalloc:
191
     * conf->mime_type = NULL;
192
     * conf->handler = NULL;
193
     * conf->output_filters = NULL;
194
     * conf->input_filters = NULL;
195
     */
196
197
    /*
198
     * Flag for use of inodes in ETags.
199
     */
200
0
    conf->etag_bits = ETAG_UNSET;
201
0
    conf->etag_add = ETAG_UNSET;
202
0
    conf->etag_remove = ETAG_UNSET;
203
204
0
    conf->enable_mmap = ENABLE_MMAP_UNSET;
205
0
    conf->enable_sendfile = ENABLE_SENDFILE_UNSET;
206
0
    conf->allow_encoded_slashes = 0;
207
0
    conf->decode_encoded_slashes = 0;
208
209
0
    conf->max_ranges = AP_MAXRANGES_UNSET;
210
0
    conf->max_overlaps = AP_MAXRANGES_UNSET;
211
0
    conf->max_reversals = AP_MAXRANGES_UNSET;
212
213
0
    conf->cgi_pass_auth = AP_CGI_PASS_AUTH_UNSET;
214
0
    conf->qualify_redirect_url = AP_CORE_CONFIG_UNSET; 
215
216
0
    return (void *)conf;
217
0
}
218
219
static void *merge_core_dir_configs(apr_pool_t *a, void *basev, void *newv)
220
0
{
221
0
    core_dir_config *base = (core_dir_config *)basev;
222
0
    core_dir_config *new = (core_dir_config *)newv;
223
0
    core_dir_config *conf;
224
225
    /* Create this conf by duplicating the base, replacing elements
226
     * (or creating copies for merging) where new-> values exist.
227
     */
228
0
    conf = (core_dir_config *)apr_pmemdup(a, base, sizeof(core_dir_config));
229
230
0
    conf->d = new->d;
231
0
    conf->d_is_fnmatch = new->d_is_fnmatch;
232
0
    conf->d_components = new->d_components;
233
0
    conf->r = new->r;
234
0
    conf->refs = new->refs;
235
0
    conf->condition = new->condition;
236
237
0
    if (new->opts & OPT_UNSET) {
238
        /* there was no explicit setting of new->opts, so we merge
239
         * preserve the invariant (opts_add & opts_remove) == 0
240
         */
241
0
        conf->opts_add = (conf->opts_add & ~new->opts_remove) | new->opts_add;
242
0
        conf->opts_remove = (conf->opts_remove & ~new->opts_add)
243
0
                            | new->opts_remove;
244
0
        conf->opts = (conf->opts & ~conf->opts_remove) | conf->opts_add;
245
246
        /* If Includes was enabled with exec in the base config, but
247
         * was enabled without exec in the new config, then disable
248
         * exec in the merged set. */
249
0
        if (((base->opts & (OPT_INCLUDES|OPT_INC_WITH_EXEC))
250
0
             == (OPT_INCLUDES|OPT_INC_WITH_EXEC))
251
0
            && ((new->opts & (OPT_INCLUDES|OPT_INC_WITH_EXEC))
252
0
                == OPT_INCLUDES)) {
253
0
            conf->opts &= ~OPT_INC_WITH_EXEC;
254
0
        }
255
0
    }
256
0
    else {
257
        /* otherwise we just copy, because an explicit opts setting
258
         * overrides all earlier +/- modifiers
259
         */
260
0
        conf->opts = new->opts;
261
0
        conf->opts_add = new->opts_add;
262
0
        conf->opts_remove = new->opts_remove;
263
0
    }
264
265
0
    if (!(new->override & OR_UNSET)) {
266
0
        conf->override = new->override;
267
0
    }
268
269
0
    if (!(new->override_opts & OPT_UNSET)) {
270
0
        conf->override_opts = new->override_opts;
271
0
    }
272
273
0
    if (new->override_list != NULL) {
274
0
        conf->override_list = new->override_list;
275
0
    }
276
277
0
    if (conf->response_code_exprs == NULL) {
278
0
        conf->response_code_exprs = new->response_code_exprs;
279
0
    }
280
0
    else if (new->response_code_exprs != NULL) {
281
0
        conf->response_code_exprs = apr_hash_overlay(a,
282
0
                new->response_code_exprs, conf->response_code_exprs);
283
0
    }
284
    /* Otherwise we simply use the base->response_code_exprs array
285
     */
286
287
0
    if (new->hostname_lookups != HOSTNAME_LOOKUP_UNSET) {
288
0
        conf->hostname_lookups = new->hostname_lookups;
289
0
    }
290
291
0
    if (new->accept_path_info != AP_ACCEPT_PATHINFO_UNSET) {
292
0
        conf->accept_path_info = new->accept_path_info;
293
0
    }
294
295
0
    if (new->use_canonical_name != USE_CANONICAL_NAME_UNSET) {
296
0
        conf->use_canonical_name = new->use_canonical_name;
297
0
    }
298
299
0
    if (new->use_canonical_phys_port != USE_CANONICAL_PHYS_PORT_UNSET) {
300
0
        conf->use_canonical_phys_port = new->use_canonical_phys_port;
301
0
    }
302
303
0
#ifdef RLIMIT_CPU
304
0
    if (new->limit_cpu) {
305
0
        conf->limit_cpu = new->limit_cpu;
306
0
    }
307
0
#endif
308
309
0
#if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS)
310
0
    if (new->limit_mem) {
311
0
        conf->limit_mem = new->limit_mem;
312
0
    }
313
0
#endif
314
315
0
#ifdef RLIMIT_NPROC
316
0
    if (new->limit_nproc) {
317
0
        conf->limit_nproc = new->limit_nproc;
318
0
    }
319
0
#endif
320
321
0
    if (new->limit_req_body != AP_LIMIT_REQ_BODY_UNSET) {
322
0
        conf->limit_req_body = new->limit_req_body;
323
0
    }
324
325
0
    if (new->limit_xml_body != AP_LIMIT_UNSET)
326
0
        conf->limit_xml_body = new->limit_xml_body;
327
328
0
    if (!conf->sec_file) {
329
0
        conf->sec_file = new->sec_file;
330
0
    }
331
0
    else if (new->sec_file) {
332
        /* If we merge, the merge-result must have its own array
333
         */
334
0
        conf->sec_file = apr_array_append(a, base->sec_file, new->sec_file);
335
0
    }
336
    /* Otherwise we simply use the base->sec_file array
337
     */
338
339
0
    if (!conf->sec_if) {
340
0
        conf->sec_if = new->sec_if;
341
0
    }
342
0
    else if (new->sec_if) {
343
        /* If we merge, the merge-result must have its own array
344
         */
345
0
        conf->sec_if = apr_array_append(a, base->sec_if, new->sec_if);
346
0
    }
347
    /* Otherwise we simply use the base->sec_if array
348
     */
349
350
0
    if (new->server_signature != srv_sig_unset) {
351
0
        conf->server_signature = new->server_signature;
352
0
    }
353
354
0
    if (new->add_default_charset != ADD_DEFAULT_CHARSET_UNSET) {
355
0
        conf->add_default_charset = new->add_default_charset;
356
0
        conf->add_default_charset_name = new->add_default_charset_name;
357
0
    }
358
359
    /* Overriding all negotiation
360
     */
361
0
    if (new->mime_type) {
362
0
        conf->mime_type = new->mime_type;
363
0
    }
364
365
0
    if (new->handler) {
366
0
        conf->handler = new->handler;
367
0
    }
368
0
    if (new->expr_handler) {
369
0
        conf->expr_handler = new->expr_handler;
370
0
    }
371
372
0
    if (new->output_filters) {
373
0
        conf->output_filters = new->output_filters;
374
0
    }
375
376
0
    if (new->input_filters) {
377
0
        conf->input_filters = new->input_filters;
378
0
    }
379
380
    /*
381
     * Now merge the setting of the FileETag directive.
382
     */
383
0
    if (new->etag_bits == ETAG_UNSET) {
384
0
        conf->etag_add =
385
0
            (conf->etag_add & (~ new->etag_remove)) | new->etag_add;
386
0
        conf->etag_remove =
387
0
            (conf->etag_remove & (~ new->etag_add)) | new->etag_remove;
388
0
        conf->etag_bits =
389
0
            (conf->etag_bits & (~ conf->etag_remove)) | conf->etag_add;
390
0
    }
391
0
    else {
392
0
        conf->etag_bits = new->etag_bits;
393
0
        conf->etag_add = new->etag_add;
394
0
        conf->etag_remove = new->etag_remove;
395
0
    }
396
397
0
    if (conf->etag_bits != ETAG_NONE) {
398
0
        conf->etag_bits &= (~ ETAG_NONE);
399
0
    }
400
401
0
    if (new->enable_mmap != ENABLE_MMAP_UNSET) {
402
0
        conf->enable_mmap = new->enable_mmap;
403
0
    }
404
405
0
    if (new->enable_sendfile != ENABLE_SENDFILE_UNSET) {
406
0
        conf->enable_sendfile = new->enable_sendfile;
407
0
    }
408
 
409
0
    if (new->read_buf_size) {
410
0
        conf->read_buf_size = new->read_buf_size;
411
0
    }
412
0
    else {
413
0
        conf->read_buf_size = base->read_buf_size;
414
0
    }
415
416
0
    if (new->allow_encoded_slashes_set) {
417
0
        conf->allow_encoded_slashes = new->allow_encoded_slashes;
418
0
    }
419
0
    if (new->decode_encoded_slashes_set) {
420
0
        conf->decode_encoded_slashes = new->decode_encoded_slashes;
421
0
    }
422
423
0
    if (new->log) {
424
0
        if (!conf->log) {
425
0
            conf->log = new->log;
426
0
        }
427
0
        else {
428
0
            conf->log = ap_new_log_config(a, new->log);
429
0
            ap_merge_log_config(base->log, conf->log);
430
0
        }
431
0
    }
432
433
0
    conf->max_ranges = new->max_ranges != AP_MAXRANGES_UNSET ? new->max_ranges : base->max_ranges;
434
0
    conf->max_overlaps = new->max_overlaps != AP_MAXRANGES_UNSET ? new->max_overlaps : base->max_overlaps;
435
0
    conf->max_reversals = new->max_reversals != AP_MAXRANGES_UNSET ? new->max_reversals : base->max_reversals;
436
437
0
    conf->cgi_pass_auth = new->cgi_pass_auth != AP_CGI_PASS_AUTH_UNSET ? new->cgi_pass_auth : base->cgi_pass_auth;
438
439
0
    if (new->cgi_var_rules) {
440
0
        if (!conf->cgi_var_rules) {
441
0
            conf->cgi_var_rules = new->cgi_var_rules;
442
0
        }
443
0
        else {
444
0
            conf->cgi_var_rules = apr_hash_overlay(a, new->cgi_var_rules, conf->cgi_var_rules);
445
0
        }
446
0
    }
447
448
0
    AP_CORE_MERGE_FLAG(qualify_redirect_url, conf, base, new);
449
450
0
    return (void*)conf;
451
0
}
452
453
#if APR_HAS_SO_ACCEPTFILTER
454
#ifndef ACCEPT_FILTER_NAME
455
#define ACCEPT_FILTER_NAME "httpready"
456
#ifdef __FreeBSD_version
457
#if __FreeBSD_version < 411000 /* httpready broken before 4.1.1 */
458
#undef ACCEPT_FILTER_NAME
459
#define ACCEPT_FILTER_NAME "dataready"
460
#endif
461
#endif
462
#endif
463
#endif
464
465
static void *create_core_server_config(apr_pool_t *a, server_rec *s)
466
0
{
467
0
    core_server_config *conf;
468
0
    int is_virtual = s->is_virtual;
469
470
0
    conf = (core_server_config *)apr_pcalloc(a, sizeof(core_server_config));
471
472
    /* global-default / global-only settings */
473
474
0
    if (!is_virtual) {
475
0
        conf->ap_document_root = DOCUMENT_LOCATION;
476
0
        conf->access_name = DEFAULT_ACCESS_FNAME;
477
478
        /* A mapping only makes sense in the global context */
479
0
        conf->accf_map = apr_table_make(a, 5);
480
#if APR_HAS_SO_ACCEPTFILTER
481
        apr_table_setn(conf->accf_map, "http", ACCEPT_FILTER_NAME);
482
        apr_table_setn(conf->accf_map, "https", "dataready");
483
#elif defined(WIN32)
484
        /* 'data' is disabled on Windows due to a DoS vuln (PR 59970) */
485
        apr_table_setn(conf->accf_map, "http", "connect");
486
        apr_table_setn(conf->accf_map, "https", "connect");
487
#else
488
0
        apr_table_setn(conf->accf_map, "http", "data");
489
0
        apr_table_setn(conf->accf_map, "https", "data");
490
0
#endif
491
492
0
        conf->flush_max_threshold = AP_FLUSH_MAX_THRESHOLD;
493
0
        conf->flush_max_pipelined = AP_FLUSH_MAX_PIPELINED;
494
0
    }
495
0
    else {
496
        /* Use main ErrorLogFormat while the vhost is loading */
497
0
        core_server_config *main_conf =
498
0
            ap_get_core_module_config(ap_server_conf->module_config);
499
0
        conf->error_log_format = main_conf->error_log_format;
500
501
0
        conf->flush_max_pipelined = -1;
502
0
    }
503
504
    /* initialization, no special case for global context */
505
506
0
    conf->sec_dir = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
507
0
    conf->sec_url = apr_array_make(a, 40, sizeof(ap_conf_vector_t *));
508
509
    /* pcalloc'ed - we have NULL's/0's
510
    conf->gprof_dir = NULL;
511
512
    ** recursion stopper; 0 == unset
513
    conf->redirect_limit = 0;
514
    conf->subreq_limit = 0;
515
516
    conf->protocol = NULL;
517
     */
518
519
0
    conf->trace_enable = AP_TRACE_UNSET;
520
521
0
    conf->protocols = apr_array_make(a, 5, sizeof(const char *));
522
0
    conf->protocols_honor_order = -1;
523
0
    conf->async_filter = 0;
524
0
    conf->strict_host_check= AP_CORE_CONFIG_UNSET; 
525
0
    conf->merge_slashes    = AP_CORE_CONFIG_UNSET; 
526
527
0
    return (void *)conf;
528
0
}
529
530
static void *merge_core_server_configs(apr_pool_t *p, void *basev, void *virtv)
531
0
{
532
0
    core_server_config *base = (core_server_config *)basev;
533
0
    core_server_config *virt = (core_server_config *)virtv;
534
0
    core_server_config *conf = (core_server_config *)
535
0
                               apr_pmemdup(p, base, sizeof(core_server_config));
536
537
0
    if (virt->ap_document_root)
538
0
        conf->ap_document_root = virt->ap_document_root;
539
540
0
    if (virt->access_name)
541
0
        conf->access_name = virt->access_name;
542
543
    /* XXX optimize to keep base->sec_ pointers if virt->sec_ array is empty */
544
0
    conf->sec_dir = apr_array_append(p, base->sec_dir, virt->sec_dir);
545
0
    conf->sec_url = apr_array_append(p, base->sec_url, virt->sec_url);
546
547
0
    if (virt->redirect_limit)
548
0
        conf->redirect_limit = virt->redirect_limit;
549
550
0
    if (virt->subreq_limit)
551
0
        conf->subreq_limit = virt->subreq_limit;
552
553
0
    if (virt->trace_enable != AP_TRACE_UNSET)
554
0
        conf->trace_enable = virt->trace_enable;
555
556
0
    if (virt->http09_enable != AP_HTTP09_UNSET)
557
0
        conf->http09_enable = virt->http09_enable;
558
559
0
    if (virt->http_conformance != AP_HTTP_CONFORMANCE_UNSET)
560
0
        conf->http_conformance = virt->http_conformance;
561
562
0
    if (virt->http_methods != AP_HTTP_METHODS_UNSET)
563
0
        conf->http_methods = virt->http_methods;
564
565
0
    if (virt->http_cl_head_zero != AP_HTTP_CL_HEAD_ZERO_UNSET)
566
0
        conf->http_cl_head_zero = virt->http_cl_head_zero;
567
568
0
    if (virt->http_expect_strict != AP_HTTP_EXPECT_STRICT_UNSET)
569
0
        conf->http_expect_strict = virt->http_expect_strict;
570
571
    /* no action for virt->accf_map, not allowed per-vhost */
572
573
0
    if (virt->protocol)
574
0
        conf->protocol = virt->protocol;
575
576
0
    if (virt->gprof_dir)
577
0
        conf->gprof_dir = virt->gprof_dir;
578
579
0
    if (virt->error_log_format)
580
0
        conf->error_log_format = virt->error_log_format;
581
582
0
    if (virt->error_log_conn)
583
0
        conf->error_log_conn = virt->error_log_conn;
584
585
0
    if (virt->error_log_req)
586
0
        conf->error_log_req = virt->error_log_req;
587
588
0
    if (virt->conn_log_level) {
589
0
        if (!conf->conn_log_level) {
590
0
            conf->conn_log_level = virt->conn_log_level;
591
0
        }
592
0
        else {
593
            /* apr_array_append actually creates a new array */
594
0
            conf->conn_log_level = apr_array_append(p, conf->conn_log_level,
595
0
                                                    virt->conn_log_level);
596
0
        }
597
0
    }
598
599
0
    conf->merge_trailers = (virt->merge_trailers != AP_MERGE_TRAILERS_UNSET)
600
0
                           ? virt->merge_trailers
601
0
                           : base->merge_trailers;
602
603
0
    conf->protocols = ((virt->protocols->nelts > 0) ?
604
0
                       virt->protocols : base->protocols);
605
0
    conf->protocols_honor_order = ((virt->protocols_honor_order < 0) ?
606
0
                                       base->protocols_honor_order :
607
0
                                       virt->protocols_honor_order);
608
609
0
    conf->async_filter = ((virt->async_filter_set) ?
610
0
                                       virt->async_filter :
611
0
                                       base->async_filter);
612
0
    conf->async_filter_set = base->async_filter_set || virt->async_filter_set;
613
614
0
    conf->flush_max_threshold = (virt->flush_max_threshold)
615
0
                                  ? virt->flush_max_threshold
616
0
                                  : base->flush_max_threshold;
617
0
    conf->flush_max_pipelined = (virt->flush_max_pipelined >= 0)
618
0
                                  ? virt->flush_max_pipelined
619
0
                                  : base->flush_max_pipelined;
620
621
0
    conf->strict_host_check = (virt->strict_host_check != AP_CORE_CONFIG_UNSET)
622
0
                              ? virt->strict_host_check 
623
0
                              : base->strict_host_check;
624
625
0
    AP_CORE_MERGE_FLAG(strict_host_check, conf, base, virt);
626
0
    AP_CORE_MERGE_FLAG(merge_slashes, conf, base, virt);
627
628
0
    return conf;
629
0
}
630
631
/* Add per-directory configuration entry (for <directory> section);
632
 * these are part of the core server config.
633
 */
634
635
AP_CORE_DECLARE(void) ap_add_per_dir_conf(server_rec *s, void *dir_config)
636
0
{
637
0
    core_server_config *sconf = ap_get_core_module_config(s->module_config);
638
0
    void **new_space = (void **)apr_array_push(sconf->sec_dir);
639
640
0
    *new_space = dir_config;
641
0
}
642
643
AP_CORE_DECLARE(void) ap_add_per_url_conf(server_rec *s, void *url_config)
644
0
{
645
0
    core_server_config *sconf = ap_get_core_module_config(s->module_config);
646
0
    void **new_space = (void **)apr_array_push(sconf->sec_url);
647
648
0
    *new_space = url_config;
649
0
}
650
651
AP_CORE_DECLARE(void) ap_add_file_conf(apr_pool_t *p, core_dir_config *conf,
652
                                       void *url_config)
653
0
{
654
0
    void **new_space;
655
656
0
    if (!conf->sec_file)
657
0
        conf->sec_file = apr_array_make(p, 2, sizeof(ap_conf_vector_t *));
658
659
0
    new_space = (void **)apr_array_push(conf->sec_file);
660
0
    *new_space = url_config;
661
0
}
662
663
AP_CORE_DECLARE(const char *) ap_add_if_conf(apr_pool_t *p,
664
                                             core_dir_config *conf,
665
                                             void *if_config)
666
0
{
667
0
    void **new_space;
668
0
    core_dir_config *new = ap_get_module_config(if_config, &core_module);
669
670
0
    if (!conf->sec_if) {
671
0
        conf->sec_if = apr_array_make(p, 2, sizeof(ap_conf_vector_t *));
672
0
    }
673
0
    if (new->condition_ifelse & AP_CONDITION_ELSE) {
674
0
        int have_if = 0;
675
0
        if (conf->sec_if->nelts > 0) {
676
0
            core_dir_config *last;
677
0
            ap_conf_vector_t *lastelt = APR_ARRAY_IDX(conf->sec_if,
678
0
                                                      conf->sec_if->nelts - 1,
679
0
                                                      ap_conf_vector_t *);
680
0
            last = ap_get_module_config(lastelt, &core_module);
681
0
            if (last->condition_ifelse & AP_CONDITION_IF)
682
0
                have_if = 1;
683
0
        }
684
0
        if (!have_if)
685
0
            return "<Else> or <ElseIf> section without previous <If> or "
686
0
                   "<ElseIf> section in same scope";
687
0
    }
688
689
0
    new_space = (void **)apr_array_push(conf->sec_if);
690
0
    *new_space = if_config;
691
0
    return NULL;
692
0
}
693
694
695
/* We need to do a stable sort, qsort isn't stable.  So to make it stable
696
 * we'll be maintaining the original index into the list, and using it
697
 * as the minor key during sorting.  The major key is the number of
698
 * components (where the root component is zero).
699
 */
700
struct reorder_sort_rec {
701
    ap_conf_vector_t *elt;
702
    int orig_index;
703
};
704
705
static int reorder_sorter(const void *va, const void *vb)
706
0
{
707
0
    const struct reorder_sort_rec *a = va;
708
0
    const struct reorder_sort_rec *b = vb;
709
0
    core_dir_config *core_a;
710
0
    core_dir_config *core_b;
711
712
0
    core_a = ap_get_core_module_config(a->elt);
713
0
    core_b = ap_get_core_module_config(b->elt);
714
715
    /* a regex always sorts after a non-regex
716
     */
717
0
    if (!core_a->r && core_b->r) {
718
0
        return -1;
719
0
    }
720
0
    else if (core_a->r && !core_b->r) {
721
0
        return 1;
722
0
    }
723
724
    /* we always sort next by the number of components
725
     */
726
0
    if (core_a->d_components < core_b->d_components) {
727
0
        return -1;
728
0
    }
729
0
    else if (core_a->d_components > core_b->d_components) {
730
0
        return 1;
731
0
    }
732
733
    /* They have the same number of components, we now have to compare
734
     * the minor key to maintain the original order (from the config.)
735
     */
736
0
    return a->orig_index - b->orig_index;
737
0
}
738
739
void ap_core_reorder_directories(apr_pool_t *p, server_rec *s)
740
0
{
741
0
    core_server_config *sconf;
742
0
    apr_array_header_t *sec_dir;
743
0
    struct reorder_sort_rec *sortbin;
744
0
    int nelts;
745
0
    ap_conf_vector_t **elts;
746
0
    int i;
747
0
    apr_pool_t *tmp;
748
749
0
    sconf = ap_get_core_module_config(s->module_config);
750
0
    sec_dir = sconf->sec_dir;
751
0
    nelts = sec_dir->nelts;
752
0
    elts = (ap_conf_vector_t **)sec_dir->elts;
753
754
0
    if (!nelts) {
755
        /* simple case of already being sorted... */
756
        /* We're not checking this condition to be fast... we're checking
757
         * it to avoid trying to palloc zero bytes, which can trigger some
758
         * memory debuggers to barf
759
         */
760
0
        return;
761
0
    }
762
763
    /* we have to allocate tmp space to do a stable sort */
764
0
    apr_pool_create(&tmp, p);
765
0
    apr_pool_tag(tmp, "core_reorder_directories");
766
0
    sortbin = apr_palloc(tmp, sec_dir->nelts * sizeof(*sortbin));
767
0
    for (i = 0; i < nelts; ++i) {
768
0
        sortbin[i].orig_index = i;
769
0
        sortbin[i].elt = elts[i];
770
0
    }
771
772
0
    qsort(sortbin, nelts, sizeof(*sortbin), reorder_sorter);
773
774
    /* and now copy back to the original array */
775
0
    for (i = 0; i < nelts; ++i) {
776
0
        elts[i] = sortbin[i].elt;
777
0
    }
778
779
0
    apr_pool_destroy(tmp);
780
0
}
781
782
/*****************************************************************
783
 *
784
 * There are some elements of the core config structures in which
785
 * other modules have a legitimate interest (this is ugly, but necessary
786
 * to preserve NCSA back-compatibility).  So, we have a bunch of accessors
787
 * here...
788
 */
789
790
AP_DECLARE(int) ap_allow_options(request_rec *r)
791
0
{
792
0
    core_dir_config *conf =
793
0
      (core_dir_config *)ap_get_core_module_config(r->per_dir_config);
794
795
0
    return conf->opts;
796
0
}
797
798
AP_DECLARE(int) ap_allow_overrides(request_rec *r)
799
0
{
800
0
    core_dir_config *conf;
801
0
    conf = (core_dir_config *)ap_get_core_module_config(r->per_dir_config);
802
803
0
    return conf->override;
804
0
}
805
806
/*
807
 * Optional function coming from mod_authn_core, used for
808
 * retrieving the type of authorization
809
 */
810
static APR_OPTIONAL_FN_TYPE(authn_ap_auth_type) *authn_ap_auth_type;
811
812
AP_DECLARE(const char *) ap_auth_type(request_rec *r)
813
0
{
814
0
    if (authn_ap_auth_type) {
815
0
        return authn_ap_auth_type(r);
816
0
    }
817
0
    return NULL;
818
0
}
819
820
/*
821
 * Optional function coming from mod_authn_core, used for
822
 * retrieving the authorization realm
823
 */
824
static APR_OPTIONAL_FN_TYPE(authn_ap_auth_name) *authn_ap_auth_name;
825
826
AP_DECLARE(const char *) ap_auth_name(request_rec *r)
827
0
{
828
0
    if (authn_ap_auth_name) {
829
0
        return authn_ap_auth_name(r);
830
0
    }
831
0
    return NULL;
832
0
}
833
834
/*
835
 * Optional function coming from mod_access_compat, used to determine how
836
   access control interacts with authentication/authorization
837
 */
838
static APR_OPTIONAL_FN_TYPE(access_compat_ap_satisfies) *access_compat_ap_satisfies;
839
840
AP_DECLARE(int) ap_satisfies(request_rec *r)
841
0
{
842
0
    if (access_compat_ap_satisfies) {
843
0
        return access_compat_ap_satisfies(r);
844
0
    }
845
0
    return SATISFY_NOSPEC;
846
0
}
847
848
AP_DECLARE(const char *) ap_document_root(request_rec *r) /* Don't use this! */
849
0
{
850
0
    core_server_config *sconf;
851
0
    core_request_config *rconf = ap_get_core_module_config(r->request_config);
852
0
    if (rconf->document_root)
853
0
        return rconf->document_root;
854
0
    sconf = ap_get_core_module_config(r->server->module_config);
855
0
    return sconf->ap_document_root;
856
0
}
857
858
AP_DECLARE(const char *) ap_context_prefix(request_rec *r)
859
0
{
860
0
    core_request_config *conf = ap_get_core_module_config(r->request_config);
861
0
    if (conf->context_prefix)
862
0
        return conf->context_prefix;
863
0
    else
864
0
        return "";
865
0
}
866
867
AP_DECLARE(const char *) ap_context_document_root(request_rec *r)
868
0
{
869
0
    core_request_config *conf = ap_get_core_module_config(r->request_config);
870
0
    if (conf->context_document_root)
871
0
        return conf->context_document_root;
872
0
    else
873
0
        return ap_document_root(r);
874
0
}
875
876
AP_DECLARE(void) ap_set_document_root(request_rec *r, const char *document_root)
877
0
{
878
0
    core_request_config *conf = ap_get_core_module_config(r->request_config);
879
0
    conf->document_root = document_root;
880
0
}
881
882
AP_DECLARE(void) ap_set_context_info(request_rec *r, const char *context_prefix,
883
                                     const char *context_document_root)
884
0
{
885
0
    core_request_config *conf = ap_get_core_module_config(r->request_config);
886
0
    if (context_prefix)
887
0
        conf->context_prefix = context_prefix;
888
0
    if (context_document_root)
889
0
        conf->context_document_root = context_document_root;
890
0
}
891
892
/* Should probably just get rid of this... the only code that cares is
893
 * part of the core anyway (and in fact, it isn't publicised to other
894
 * modules).
895
 */
896
897
char *ap_response_code_string(request_rec *r, int error_index)
898
0
{
899
0
    core_dir_config *dirconf;
900
0
    core_request_config *reqconf = ap_get_core_module_config(r->request_config);
901
0
    const char *err;
902
0
    const char *response;
903
0
    ap_expr_info_t *expr;
904
905
    /* check for string registered via ap_custom_response() first */
906
0
    if (reqconf->response_code_strings != NULL
907
0
            && reqconf->response_code_strings[error_index] != NULL) {
908
0
        return reqconf->response_code_strings[error_index];
909
0
    }
910
911
    /* check for string specified via ErrorDocument */
912
0
    dirconf = ap_get_core_module_config(r->per_dir_config);
913
914
0
    if (!dirconf->response_code_exprs) {
915
0
        return NULL;
916
0
    }
917
918
0
    expr = apr_hash_get(dirconf->response_code_exprs, &error_index,
919
0
            sizeof(error_index));
920
0
    if (!expr) {
921
0
        return NULL;
922
0
    }
923
924
    /* special token to indicate revert back to default */
925
0
    if ((char *) expr == &errordocument_default) {
926
0
        return NULL;
927
0
    }
928
929
0
    err = NULL;
930
0
    response = ap_expr_str_exec(r, expr, &err);
931
0
    if (err) {
932
0
        ap_log_rerror(
933
0
                APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02841) "core: ErrorDocument: can't "
934
0
                "evaluate require expression: %s", err);
935
0
        return NULL;
936
0
    }
937
938
    /* alas, duplication required as we return not-const */
939
0
    return apr_pstrdup(r->pool, response);
940
0
}
941
942
943
/* Code from Harald Hanche-Olsen <hanche@imf.unit.no> */
944
static APR_INLINE int do_double_reverse (int double_reverse,
945
                                         const char *remote_host,
946
                                         apr_sockaddr_t *client_addr,
947
                                         apr_pool_t *pool)
948
0
{
949
0
    apr_sockaddr_t *sa;
950
0
    apr_status_t rv;
951
952
0
    if (double_reverse) {
953
        /* already done */
954
0
        return double_reverse;
955
0
    }
956
957
0
    if (remote_host == NULL || remote_host[0] == '\0') {
958
        /* single reverse failed, so don't bother */
959
0
        return -1;
960
0
    }
961
962
0
    rv = apr_sockaddr_info_get(&sa, remote_host, APR_UNSPEC, 0, 0, pool);
963
0
    if (rv == APR_SUCCESS) {
964
0
        while (sa) {
965
0
            if (apr_sockaddr_equal(sa, client_addr)) {
966
0
                return 1;
967
0
            }
968
969
0
            sa = sa->next;
970
0
        }
971
0
    }
972
973
0
    return -1;
974
0
}
975
976
AP_DECLARE(const char *) ap_get_remote_host(conn_rec *conn, void *dir_config,
977
                                            int type, int *str_is_ip)
978
0
{
979
0
    int hostname_lookups;
980
0
    int ignored_str_is_ip;
981
982
0
    if (!str_is_ip) { /* caller doesn't want to know */
983
0
        str_is_ip = &ignored_str_is_ip;
984
0
    }
985
0
    *str_is_ip = 0;
986
987
    /* If we haven't checked the host name, and we want to */
988
0
    if (dir_config) {
989
0
        hostname_lookups = ((core_dir_config *)ap_get_core_module_config(dir_config))
990
0
                           ->hostname_lookups;
991
992
0
        if (hostname_lookups == HOSTNAME_LOOKUP_UNSET) {
993
0
            hostname_lookups = HOSTNAME_LOOKUP_OFF;
994
0
        }
995
0
    }
996
0
    else {
997
        /* the default */
998
0
        hostname_lookups = HOSTNAME_LOOKUP_OFF;
999
0
    }
1000
1001
0
    if (type != REMOTE_NOLOOKUP
1002
0
        && conn->remote_host == NULL
1003
0
        && (type == REMOTE_DOUBLE_REV
1004
0
        || hostname_lookups != HOSTNAME_LOOKUP_OFF)) {
1005
1006
0
        if (apr_getnameinfo(&conn->remote_host, conn->client_addr, 0)
1007
0
            == APR_SUCCESS) {
1008
0
            ap_str_tolower(conn->remote_host);
1009
1010
0
            if (hostname_lookups == HOSTNAME_LOOKUP_DOUBLE) {
1011
0
                conn->double_reverse = do_double_reverse(conn->double_reverse,
1012
0
                                                         conn->remote_host,
1013
0
                                                         conn->client_addr,
1014
0
                                                         conn->pool);
1015
0
                if (conn->double_reverse != 1) {
1016
0
                    conn->remote_host = NULL;
1017
0
                }
1018
0
            }
1019
0
        }
1020
1021
        /* if failed, set it to the NULL string to indicate error */
1022
0
        if (conn->remote_host == NULL) {
1023
0
            conn->remote_host = "";
1024
0
        }
1025
0
    }
1026
1027
0
    if (type == REMOTE_DOUBLE_REV) {
1028
0
        conn->double_reverse = do_double_reverse(conn->double_reverse,
1029
0
                                                 conn->remote_host,
1030
0
                                                 conn->client_addr, conn->pool);
1031
0
        if (conn->double_reverse == -1) {
1032
0
            return NULL;
1033
0
        }
1034
0
    }
1035
1036
    /*
1037
     * Return the desired information; either the remote DNS name, if found,
1038
     * or either NULL (if the hostname was requested) or the IP address
1039
     * (if any identifier was requested).
1040
     */
1041
0
    if (conn->remote_host != NULL && conn->remote_host[0] != '\0') {
1042
0
        return conn->remote_host;
1043
0
    }
1044
0
    else {
1045
0
        if (type == REMOTE_HOST || type == REMOTE_DOUBLE_REV) {
1046
0
            return NULL;
1047
0
        }
1048
0
        else {
1049
0
            *str_is_ip = 1;
1050
0
            return conn->client_ip;
1051
0
        }
1052
0
    }
1053
0
}
1054
1055
AP_DECLARE(const char *) ap_get_useragent_host(request_rec *r,
1056
                                               int type, int *str_is_ip)
1057
0
{
1058
0
    conn_rec *conn = r->connection;
1059
0
    int hostname_lookups;
1060
0
    int ignored_str_is_ip;
1061
1062
    /* Guard here when examining the host before the read_request hook
1063
     * has populated an r->useragent_addr
1064
     */
1065
0
    if (!r->useragent_addr || (r->useragent_addr == conn->client_addr)) {
1066
0
        return ap_get_remote_host(conn, r->per_dir_config, type, str_is_ip);
1067
0
    }
1068
1069
0
    if (!str_is_ip) { /* caller doesn't want to know */
1070
0
        str_is_ip = &ignored_str_is_ip;
1071
0
    }
1072
0
    *str_is_ip = 0;
1073
1074
0
    hostname_lookups = ((core_dir_config *)
1075
0
                        ap_get_core_module_config(r->per_dir_config))
1076
0
                            ->hostname_lookups;
1077
0
    if (hostname_lookups == HOSTNAME_LOOKUP_UNSET) {
1078
0
        hostname_lookups = HOSTNAME_LOOKUP_OFF;
1079
0
    }
1080
1081
0
    if (type != REMOTE_NOLOOKUP
1082
0
        && r->useragent_host == NULL
1083
0
        && (type == REMOTE_DOUBLE_REV
1084
0
        || hostname_lookups != HOSTNAME_LOOKUP_OFF)) {
1085
1086
0
        if (apr_getnameinfo(&r->useragent_host, r->useragent_addr, 0)
1087
0
            == APR_SUCCESS) {
1088
0
            ap_str_tolower(r->useragent_host);
1089
1090
0
            if (hostname_lookups == HOSTNAME_LOOKUP_DOUBLE) {
1091
0
                r->double_reverse = do_double_reverse(r->double_reverse,
1092
0
                                                      r->useragent_host,
1093
0
                                                      r->useragent_addr,
1094
0
                                                      r->pool);
1095
0
                if (r->double_reverse != 1) {
1096
0
                    r->useragent_host = NULL;
1097
0
                }
1098
0
            }
1099
0
        }
1100
1101
        /* if failed, set it to the NULL string to indicate error */
1102
0
        if (r->useragent_host == NULL) {
1103
0
            r->useragent_host = "";
1104
0
        }
1105
0
    }
1106
1107
0
    if (type == REMOTE_DOUBLE_REV) {
1108
0
        r->double_reverse = do_double_reverse(r->double_reverse,
1109
0
                                              r->useragent_host,
1110
0
                                              r->useragent_addr, r->pool);
1111
0
        if (r->double_reverse == -1) {
1112
0
            return NULL;
1113
0
        }
1114
0
    }
1115
1116
    /*
1117
     * Return the desired information; either the remote DNS name, if found,
1118
     * or either NULL (if the hostname was requested) or the IP address
1119
     * (if any identifier was requested).
1120
     */
1121
0
    if (r->useragent_host != NULL && r->useragent_host[0] != '\0') {
1122
0
        return r->useragent_host;
1123
0
    }
1124
0
    else {
1125
0
        if (type == REMOTE_HOST || type == REMOTE_DOUBLE_REV) {
1126
0
            return NULL;
1127
0
        }
1128
0
        else {
1129
0
            *str_is_ip = 1;
1130
0
            return r->useragent_ip;
1131
0
        }
1132
0
    }
1133
0
}
1134
1135
/*
1136
 * Optional function coming from mod_ident, used for looking up ident user
1137
 */
1138
static APR_OPTIONAL_FN_TYPE(ap_ident_lookup) *ident_lookup;
1139
1140
AP_DECLARE(const char *) ap_get_remote_logname(request_rec *r)
1141
0
{
1142
0
    if (r->connection->remote_logname != NULL) {
1143
0
        return r->connection->remote_logname;
1144
0
    }
1145
1146
0
    if (ident_lookup) {
1147
0
        return ident_lookup(r);
1148
0
    }
1149
1150
0
    return NULL;
1151
0
}
1152
1153
/* There are two options regarding what the "name" of a server is.  The
1154
 * "canonical" name as defined by ServerName and Port, or the "client's
1155
 * name" as supplied by a possible Host: header or full URI.
1156
 *
1157
 * The DNS option to UseCanonicalName causes this routine to do a
1158
 * reverse lookup on the local IP address of the connection and use
1159
 * that for the ServerName. This makes its value more reliable while
1160
 * at the same time allowing Demon's magic virtual hosting to work.
1161
 * The assumption is that DNS lookups are sufficiently quick...
1162
 * -- fanf 1998-10-03
1163
 */
1164
AP_DECLARE(const char *) ap_get_server_name(request_rec *r)
1165
0
{
1166
0
    conn_rec *conn = r->connection;
1167
0
    core_dir_config *d;
1168
0
    const char *retval;
1169
1170
0
    d = (core_dir_config *)ap_get_core_module_config(r->per_dir_config);
1171
1172
0
    switch (d->use_canonical_name) {
1173
0
        case USE_CANONICAL_NAME_ON:
1174
0
            retval = r->server->server_hostname;
1175
0
            break;
1176
0
        case USE_CANONICAL_NAME_DNS:
1177
0
            if (conn->local_host == NULL) {
1178
0
                if (apr_getnameinfo(&conn->local_host,
1179
0
                                conn->local_addr, 0) != APR_SUCCESS)
1180
0
                    conn->local_host = apr_pstrdup(conn->pool,
1181
0
                                               r->server->server_hostname);
1182
0
                else {
1183
0
                    ap_str_tolower(conn->local_host);
1184
0
                }
1185
0
            }
1186
0
            retval = conn->local_host;
1187
0
            break;
1188
0
        case USE_CANONICAL_NAME_OFF:
1189
0
        case USE_CANONICAL_NAME_UNSET:
1190
0
            retval = r->hostname ? r->hostname : r->server->server_hostname;
1191
0
            break;
1192
0
        default:
1193
0
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00109)
1194
0
                         "ap_get_server_name: Invalid UCN Option somehow");
1195
0
            retval = "localhost";
1196
0
            break;
1197
0
    }
1198
0
    return retval;
1199
0
}
1200
1201
/*
1202
 * Get the current server name from the request for the purposes
1203
 * of using in a URL.  If the server name is an IPv6 literal
1204
 * address, it will be returned in URL format (e.g., "[fe80::1]").
1205
 */
1206
AP_DECLARE(const char *) ap_get_server_name_for_url(request_rec *r)
1207
0
{
1208
0
    const char *plain_server_name = ap_get_server_name(r);
1209
1210
0
#if APR_HAVE_IPV6
1211
0
    if (ap_strchr_c(plain_server_name, ':')) { /* IPv6 literal? */
1212
0
        return apr_pstrcat(r->pool, "[", plain_server_name, "]", NULL);
1213
0
    }
1214
0
#endif
1215
0
    return plain_server_name;
1216
0
}
1217
1218
AP_DECLARE(apr_port_t) ap_get_server_port(const request_rec *r)
1219
0
{
1220
0
    apr_port_t port;
1221
0
    core_dir_config *d =
1222
0
      (core_dir_config *)ap_get_core_module_config(r->per_dir_config);
1223
1224
0
    switch (d->use_canonical_name) {
1225
0
        case USE_CANONICAL_NAME_OFF:
1226
0
        case USE_CANONICAL_NAME_DNS:
1227
0
        case USE_CANONICAL_NAME_UNSET:
1228
0
            if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
1229
0
                port = r->parsed_uri.port_str ? r->parsed_uri.port :
1230
0
                       r->connection->local_addr->port ? r->connection->local_addr->port :
1231
0
                       r->server->port ? r->server->port :
1232
0
                       ap_default_port(r);
1233
0
            else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
1234
0
                port = r->parsed_uri.port_str ? r->parsed_uri.port :
1235
0
                       r->server->port ? r->server->port :
1236
0
                       ap_default_port(r);
1237
0
            break;
1238
0
        case USE_CANONICAL_NAME_ON:
1239
            /* With UseCanonicalName on (and in all versions prior to 1.3)
1240
             * Apache will use the hostname and port specified in the
1241
             * ServerName directive to construct a canonical name for the
1242
             * server. (If no port was specified in the ServerName
1243
             * directive, Apache uses the port supplied by the client if
1244
             * any is supplied, and finally the default port for the protocol
1245
             * used.
1246
             */
1247
0
            if (d->use_canonical_phys_port == USE_CANONICAL_PHYS_PORT_ON)
1248
0
                port = r->server->port ? r->server->port :
1249
0
                       r->connection->local_addr->port ? r->connection->local_addr->port :
1250
0
                       ap_default_port(r);
1251
0
            else /* USE_CANONICAL_PHYS_PORT_OFF or USE_CANONICAL_PHYS_PORT_UNSET */
1252
0
                port = r->server->port ? r->server->port :
1253
0
                       ap_default_port(r);
1254
0
            break;
1255
0
        default:
1256
0
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00110)
1257
0
                         "ap_get_server_port: Invalid UCN Option somehow");
1258
0
            port = ap_default_port(r);
1259
0
            break;
1260
0
    }
1261
1262
0
    return port;
1263
0
}
1264
1265
AP_DECLARE(char *) ap_construct_url(apr_pool_t *p, const char *uri,
1266
                                    request_rec *r)
1267
0
{
1268
0
    unsigned port = ap_get_server_port(r);
1269
0
    const char *host = ap_get_server_name_for_url(r);
1270
1271
0
    if (ap_is_default_port(port, r)) {
1272
0
        return apr_pstrcat(p, ap_http_scheme(r), "://", host, uri, NULL);
1273
0
    }
1274
1275
0
    return apr_psprintf(p, "%s://%s:%u%s", ap_http_scheme(r), host, port, uri);
1276
0
}
1277
1278
AP_DECLARE(apr_off_t) ap_get_limit_req_body(const request_rec *r)
1279
0
{
1280
0
    core_dir_config *d =
1281
0
      (core_dir_config *)ap_get_core_module_config(r->per_dir_config);
1282
1283
0
    if (d->limit_req_body == AP_LIMIT_REQ_BODY_UNSET) {
1284
0
        return AP_DEFAULT_LIMIT_REQ_BODY;
1285
0
    }
1286
1287
0
    return d->limit_req_body;
1288
0
}
1289
1290
AP_DECLARE(apr_size_t) ap_get_read_buf_size(const request_rec *r)
1291
0
{
1292
0
    core_dir_config *d = ap_get_core_module_config(r->per_dir_config);
1293
1294
0
    return d->read_buf_size ? d->read_buf_size : AP_IOBUFSIZE;
1295
0
}
1296
1297
1298
/*****************************************************************
1299
 *
1300
 * Commands... this module handles almost all of the NCSA httpd.conf
1301
 * commands, but most of the old srm.conf is in the modules.
1302
 */
1303
1304
1305
/* returns a parent if it matches the given directive */
1306
static const ap_directive_t * find_parent(const ap_directive_t *dirp,
1307
                                          const char *what)
1308
0
{
1309
0
    while (dirp->parent != NULL) {
1310
0
        dirp = dirp->parent;
1311
1312
        /* ### it would be nice to have atom-ized directives */
1313
0
        if (ap_cstr_casecmp(dirp->directive, what) == 0)
1314
0
            return dirp;
1315
0
    }
1316
1317
0
    return NULL;
1318
0
}
1319
1320
AP_DECLARE(const char *) ap_check_cmd_context(cmd_parms *cmd,
1321
                                              unsigned forbidden)
1322
0
{
1323
0
    const char *gt = (cmd->cmd->name[0] == '<'
1324
0
                      && cmd->cmd->name[strlen(cmd->cmd->name)-1] != '>')
1325
0
                         ? ">" : "";
1326
0
    const ap_directive_t *found;
1327
1328
0
    if ((forbidden & NOT_IN_VIRTUALHOST) && cmd->server->is_virtual) {
1329
0
        return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1330
0
                           " cannot occur within <VirtualHost> section", NULL);
1331
0
    }
1332
1333
0
    if ((forbidden & NOT_IN_DIR_CONTEXT) && cmd->limited != -1) {
1334
0
        return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1335
0
                           " cannot occur within <Limit> or <LimitExcept> "
1336
0
                           "section", NULL);
1337
0
    }
1338
1339
0
    if ((forbidden & NOT_IN_HTACCESS) && (cmd->pool == cmd->temp_pool)) {
1340
0
         return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1341
0
                            " cannot occur within htaccess files", NULL);
1342
0
    }
1343
1344
0
    if ((forbidden & NOT_IN_DIR_CONTEXT) == NOT_IN_DIR_CONTEXT) {
1345
0
        if (cmd->path != NULL) {
1346
0
            return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1347
0
                            " cannot occur within directory context", NULL);
1348
0
        }
1349
0
        if (cmd->cmd->req_override & EXEC_ON_READ) {
1350
            /* EXEC_ON_READ must be NOT_IN_DIR_CONTEXT, if not, it will
1351
             * (deliberately) segfault below in the individual tests...
1352
             */
1353
0
            return NULL;
1354
0
        }
1355
0
    }
1356
1357
0
    if (((forbidden & NOT_IN_DIRECTORY)
1358
0
         && ((found = find_parent(cmd->directive, "<Directory"))
1359
0
             || (found = find_parent(cmd->directive, "<DirectoryMatch"))))
1360
0
        || ((forbidden & NOT_IN_LOCATION)
1361
0
            && ((found = find_parent(cmd->directive, "<Location"))
1362
0
                || (found = find_parent(cmd->directive, "<LocationMatch"))))
1363
0
        || ((forbidden & NOT_IN_FILES)
1364
0
            && ((found = find_parent(cmd->directive, "<Files"))
1365
0
                || (found = find_parent(cmd->directive, "<FilesMatch"))
1366
0
                || (found = find_parent(cmd->directive, "<If"))
1367
0
                || (found = find_parent(cmd->directive, "<ElseIf"))
1368
0
                || (found = find_parent(cmd->directive, "<Else"))))
1369
0
        || ((forbidden & NOT_IN_PROXY)
1370
0
            && ((found = find_parent(cmd->directive, "<Proxy"))
1371
0
                || (found = find_parent(cmd->directive, "<ProxyMatch"))))) {
1372
0
        return apr_pstrcat(cmd->pool, cmd->cmd->name, gt,
1373
0
                           " cannot occur within ", found->directive,
1374
0
                           "> section", NULL);
1375
0
    }
1376
1377
0
    return NULL;
1378
0
}
1379
1380
static const char *set_access_name(cmd_parms *cmd, void *dummy,
1381
                                   const char *arg)
1382
0
{
1383
0
    void *sconf = cmd->server->module_config;
1384
0
    core_server_config *conf = ap_get_core_module_config(sconf);
1385
1386
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
1387
0
    if (err != NULL) {
1388
0
        return err;
1389
0
    }
1390
1391
0
    conf->access_name = apr_pstrdup(cmd->pool, arg);
1392
0
    return NULL;
1393
0
}
1394
1395
AP_DECLARE(const char *) ap_resolve_env(apr_pool_t *p, const char * word)
1396
0
{
1397
0
# define SMALL_EXPANSION 5
1398
0
    struct sll {
1399
0
        struct sll *next;
1400
0
        const char *string;
1401
0
        apr_size_t len;
1402
0
    } *result, *current, sresult[SMALL_EXPANSION];
1403
0
    char *res_buf, *cp;
1404
0
    const char *s, *e, *ep;
1405
0
    unsigned spc;
1406
0
    apr_size_t outlen;
1407
1408
0
    s = ap_strchr_c(word, '$');
1409
0
    if (!s) {
1410
0
        return word;
1411
0
    }
1412
1413
    /* well, actually something to do */
1414
0
    ep = word + strlen(word);
1415
0
    spc = 0;
1416
0
    result = current = &(sresult[spc++]);
1417
0
    current->next = NULL;
1418
0
    current->string = word;
1419
0
    current->len = s - word;
1420
0
    outlen = current->len;
1421
1422
0
    do {
1423
        /* prepare next entry */
1424
0
        if (current->len) {
1425
0
            current->next = (spc < SMALL_EXPANSION)
1426
0
                            ? &(sresult[spc++])
1427
0
                            : (struct sll *)apr_palloc(p,
1428
0
                                                       sizeof(*current->next));
1429
0
            current = current->next;
1430
0
            current->next = NULL;
1431
0
            current->len = 0;
1432
0
        }
1433
1434
0
        if (*s == '$') {
1435
0
            if (s[1] == '{' && (e = ap_strchr_c(s+2, '}'))) {
1436
0
                char *name = apr_pstrmemdup(p, s+2, e-s-2);
1437
0
                char *dflt = ap_strstr(name, "?=");
1438
0
                if (dflt) {
1439
                    /* Default value for when var is not defined */
1440
0
                    *dflt = '\0';
1441
0
                    dflt += 2;
1442
0
                }
1443
0
                word = NULL;
1444
0
                if (server_config_defined_vars)
1445
0
                    word = apr_table_get(server_config_defined_vars, name);
1446
0
                if (!word)
1447
0
                    word = apr_pstrdup(p, getenv(name));
1448
0
                if (word) {
1449
0
                    current->string = word;
1450
0
                    current->len = strlen(word);
1451
0
                    outlen += current->len;
1452
0
                }
1453
0
                else if (dflt) {
1454
0
                    current->string = dflt;
1455
0
                    current->len = strlen(dflt);
1456
0
                    outlen += current->len;
1457
0
                }
1458
0
                else {
1459
0
                    if (ap_strchr(name, ':') == 0)
1460
0
                        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, NULL, APLOGNO(00111)
1461
0
                                     "Config variable ${%s} is not defined",
1462
0
                                     name);
1463
0
                    current->string = s;
1464
0
                    current->len = e - s + 1;
1465
0
                    outlen += current->len;
1466
0
                }
1467
0
                s = e + 1;
1468
0
            }
1469
0
            else {
1470
0
                current->string = s++;
1471
0
                current->len = 1;
1472
0
                ++outlen;
1473
0
            }
1474
0
        }
1475
0
        else {
1476
0
            word = s;
1477
0
            s = ap_strchr_c(s, '$');
1478
0
            current->string = word;
1479
0
            current->len = s ? s - word : ep - word;
1480
0
            outlen += current->len;
1481
0
        }
1482
0
    } while (s && *s);
1483
1484
    /* assemble result */
1485
0
    res_buf = cp = apr_palloc(p, outlen + 1);
1486
0
    do {
1487
0
        if (result->len) {
1488
0
            memcpy(cp, result->string, result->len);
1489
0
            cp += result->len;
1490
0
        }
1491
0
        result = result->next;
1492
0
    } while (result);
1493
0
    res_buf[outlen] = '\0';
1494
1495
0
    return res_buf;
1496
0
}
1497
1498
/* pconf cleanup - clear global variables set from config here. */
1499
static apr_status_t reset_config(void *dummy)
1500
0
{
1501
0
    ap_server_config_defines = saved_server_config_defines;
1502
0
    saved_server_config_defines = NULL;
1503
0
    server_config_defined_vars = NULL;
1504
0
    core_state_dir = NULL;
1505
0
    ap_runtime_dir = NULL;
1506
1507
0
    return APR_SUCCESS;
1508
0
}
1509
1510
/*
1511
 * Make sure we can revert the effects of Define/UnDefine when restarting.
1512
 * This function must be called once per loading of the config, before
1513
 * ap_server_config_defines is changed. This may be during reading of the
1514
 * config, which is even before the pre_config hook is run (due to
1515
 * EXEC_ON_READ for Define/UnDefine).
1516
 */
1517
static void init_config_defines(apr_pool_t *pconf)
1518
0
{
1519
0
    saved_server_config_defines = ap_server_config_defines;
1520
    /* Use apr_array_copy instead of apr_array_copy_hdr because it does not
1521
     * protect from the way unset_define removes entries.
1522
     */
1523
0
    ap_server_config_defines = apr_array_copy(pconf, ap_server_config_defines);
1524
0
}
1525
1526
static const char *set_define(cmd_parms *cmd, void *dummy,
1527
                              const char *name, const char *value)
1528
0
{
1529
0
    if (cmd->parent && ap_cstr_casecmp(cmd->parent->directive, "<VirtualHost")) {
1530
0
        return apr_pstrcat(cmd->pool, cmd->cmd->name, " is not valid in ",
1531
0
                                      cmd->parent->directive, " context", NULL);
1532
0
    }
1533
1534
0
    if (ap_strchr_c(name, ':') != NULL) {
1535
0
        return "Variable name must not contain ':'";
1536
0
    }
1537
1538
0
    if (!saved_server_config_defines) {
1539
0
        init_config_defines(cmd->pool);
1540
0
    }
1541
0
    if (!ap_exists_config_define(name)) {
1542
0
        *(const char **)apr_array_push(ap_server_config_defines) = name;
1543
0
    }
1544
0
    if (value) {
1545
0
        if (!server_config_defined_vars) {
1546
0
            server_config_defined_vars = apr_table_make(cmd->pool, 5);
1547
0
        }
1548
0
        apr_table_setn(server_config_defined_vars, name, value);
1549
0
    }
1550
1551
0
    return NULL;
1552
0
}
1553
1554
static const char *unset_define(cmd_parms *cmd, void *dummy,
1555
                                const char *name)
1556
0
{
1557
0
    int i;
1558
0
    const char **defines;
1559
0
    if (cmd->parent && ap_cstr_casecmp(cmd->parent->directive, "<VirtualHost")) {
1560
0
        return apr_pstrcat(cmd->pool, cmd->cmd->name, " is not valid in ",
1561
0
                                      cmd->parent->directive, " context", NULL);
1562
0
    }
1563
1564
0
    if (ap_strchr_c(name, ':') != NULL) {
1565
0
        return "Variable name must not contain ':'";
1566
0
    }
1567
1568
0
    if (!saved_server_config_defines) {
1569
0
        init_config_defines(cmd->pool);
1570
0
    }
1571
1572
0
    defines = (const char **)ap_server_config_defines->elts;
1573
0
    for (i = 0; i < ap_server_config_defines->nelts; i++) {
1574
0
        if (strcmp(defines[i], name) == 0) {
1575
0
            defines[i] = *(const char **)apr_array_pop(ap_server_config_defines);
1576
0
            break;
1577
0
        }
1578
0
    }
1579
1580
0
    if (server_config_defined_vars) {
1581
0
        apr_table_unset(server_config_defined_vars, name);
1582
0
    }
1583
1584
0
    return NULL;
1585
0
}
1586
1587
static const char *generate_message(cmd_parms *cmd, void *dummy,
1588
                                    const char *arg)
1589
0
{
1590
    /* cast with 64-bit warning avoidance */
1591
0
    int level = (cmd->info==(void*)APLOG_ERR)? APLOG_ERR: APLOG_WARNING;
1592
0
    char * msg;
1593
1594
    /* get position information from wherever we can? */
1595
0
    ap_configfile_t * cf = cmd->config_file;
1596
0
    ap_directive_t const * ed1 = cmd->directive;
1597
0
    ap_directive_t const * ed2 = cmd->err_directive;
1598
1599
    /* expect an argument */
1600
0
    if (!arg || !*arg) {
1601
0
        return "The Error or Warning directive was used with no message.";
1602
0
    }
1603
1604
    /* set message, strip off quotes if necessary */
1605
0
    msg = (char *)arg;
1606
0
    if (*arg == '"' || *arg == '\'') {
1607
0
        apr_size_t len = strlen(arg);
1608
0
        char last = *(arg + len - 1);
1609
1610
0
        if (*arg == last) {
1611
0
            msg = apr_pstrndup(cmd->pool, arg + 1, len - 2);
1612
0
        }
1613
0
    }
1614
1615
    /* generate error or warning with a configuration file position.
1616
     * the log is displayed on the terminal as no log file is opened yet.
1617
     */
1618
0
    ap_log_error(APLOG_MARK, level, 0, NULL,
1619
0
                 "%s on line %d of %s", msg,
1620
0
                 cf? cf->line_number:
1621
0
                   ed1? ed1->line_num:
1622
0
                     ed2? ed2->line_num: -1,
1623
0
                 cf? cf->name:
1624
0
                   ed1? ed1->filename:
1625
0
                     ed2? ed2->filename: "<UNKNOWN>");
1626
1627
    /* message displayed above, return will stop configuration processing */
1628
0
    return level==APLOG_ERR?
1629
0
        "Configuration processing stopped by Error directive": NULL;
1630
0
}
1631
1632
#ifdef GPROF
1633
static const char *set_gprof_dir(cmd_parms *cmd, void *dummy, const char *arg)
1634
{
1635
    void *sconf = cmd->server->module_config;
1636
    core_server_config *conf = ap_get_core_module_config(sconf);
1637
1638
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
1639
    if (err != NULL) {
1640
        return err;
1641
    }
1642
1643
    conf->gprof_dir = apr_pstrdup(cmd->pool, arg);
1644
    return NULL;
1645
}
1646
#endif /*GPROF*/
1647
1648
static const char *set_add_default_charset(cmd_parms *cmd,
1649
                                           void *d_, const char *arg)
1650
0
{
1651
0
    core_dir_config *d = d_;
1652
1653
0
    if (!ap_cstr_casecmp(arg, "Off")) {
1654
0
       d->add_default_charset = ADD_DEFAULT_CHARSET_OFF;
1655
0
    }
1656
0
    else if (!ap_cstr_casecmp(arg, "On")) {
1657
0
       d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1658
0
       d->add_default_charset_name = DEFAULT_ADD_DEFAULT_CHARSET_NAME;
1659
0
    }
1660
0
    else {
1661
0
       d->add_default_charset = ADD_DEFAULT_CHARSET_ON;
1662
0
       d->add_default_charset_name = arg;
1663
0
    }
1664
1665
0
    return NULL;
1666
0
}
1667
1668
static const char *set_document_root(cmd_parms *cmd, void *dummy,
1669
                                     const char *arg)
1670
0
{
1671
0
    void *sconf = cmd->server->module_config;
1672
0
    core_server_config *conf = ap_get_core_module_config(sconf);
1673
1674
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
1675
0
    if (err != NULL) {
1676
0
        return err;
1677
0
    }
1678
1679
    /* When ap_document_root_check is false; skip all the stuff below */
1680
0
    if (!ap_document_root_check) {
1681
0
       conf->ap_document_root = arg;
1682
0
       return NULL;
1683
0
    }
1684
1685
    /* Make it absolute, relative to ServerRoot */
1686
0
    arg = ap_server_root_relative(cmd->pool, arg);
1687
0
    if (arg == NULL) {
1688
0
        return "DocumentRoot must be a directory";
1689
0
    }
1690
1691
0
    if (apr_filepath_merge((char**)&conf->ap_document_root, NULL, arg,
1692
0
                           APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS
1693
0
        || !ap_is_directory(cmd->temp_pool, arg)) {
1694
0
        if (cmd->server->is_virtual) {
1695
0
            ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0,
1696
0
                          cmd->pool, APLOGNO(00112)
1697
0
                          "Warning: DocumentRoot [%s] does not exist",
1698
0
                          arg);
1699
0
            conf->ap_document_root = arg;
1700
0
        }
1701
0
        else {
1702
0
            return apr_psprintf(cmd->pool, 
1703
0
                                "DocumentRoot '%s' is not a directory, or is not readable",
1704
0
                                arg);
1705
0
        }
1706
0
    }
1707
0
    return NULL;
1708
0
}
1709
1710
AP_DECLARE(void) ap_custom_response(request_rec *r, int status,
1711
                                    const char *string)
1712
0
{
1713
0
    core_request_config *conf = ap_get_core_module_config(r->request_config);
1714
0
    int idx;
1715
1716
0
    if (conf->response_code_strings == NULL) {
1717
0
        conf->response_code_strings =
1718
0
            apr_pcalloc(r->pool,
1719
0
                        sizeof(*conf->response_code_strings) * RESPONSE_CODES);
1720
0
    }
1721
1722
0
    idx = ap_index_of_response(status);
1723
1724
0
    conf->response_code_strings[idx] =
1725
0
       ((ap_is_url(string) || (*string == '/')) && (*string != '"')) ?
1726
0
       apr_pstrdup(r->pool, string) : apr_pstrcat(r->pool, "\"", string, NULL);
1727
0
}
1728
1729
static const char *set_error_document(cmd_parms *cmd, void *conf_,
1730
                                      const char *errno_str, const char *msg)
1731
0
{
1732
0
    core_dir_config *conf = conf_;
1733
0
    int error_number, index_number, idx500;
1734
0
    enum { MSG, LOCAL_PATH, REMOTE_PATH } what = MSG;
1735
1736
    /* 1st parameter should be a 3 digit number, which we recognize;
1737
     * convert it into an array index
1738
     */
1739
0
    error_number = atoi(errno_str);
1740
0
    idx500 = ap_index_of_response(HTTP_INTERNAL_SERVER_ERROR);
1741
1742
0
    if (error_number == HTTP_INTERNAL_SERVER_ERROR) {
1743
0
        index_number = idx500;
1744
0
    }
1745
0
    else if ((index_number = ap_index_of_response(error_number)) == idx500) {
1746
0
        return apr_pstrcat(cmd->pool, "Unsupported HTTP response code ",
1747
0
                           errno_str, NULL);
1748
0
    }
1749
1750
    /* Heuristic to determine second argument. */
1751
0
    if (ap_strchr_c(msg,' '))
1752
0
        what = MSG;
1753
0
    else if (msg[0] == '/')
1754
0
        what = LOCAL_PATH;
1755
0
    else if (ap_is_url(msg))
1756
0
        what = REMOTE_PATH;
1757
0
    else
1758
0
        what = MSG;
1759
1760
    /* The entry should be ignored if it is a full URL for a 401 error */
1761
1762
0
    if (error_number == 401 && what == REMOTE_PATH) {
1763
0
        ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, cmd->server, APLOGNO(00113)
1764
0
                     "%s:%d cannot use a full URL in a 401 ErrorDocument "
1765
0
                     "directive --- ignoring!", cmd->directive->filename, cmd->directive->line_num);
1766
0
    }
1767
0
    else { /* Store it... */
1768
0
        if (conf->response_code_exprs == NULL) {
1769
0
            conf->response_code_exprs = apr_hash_make(cmd->pool);
1770
0
        }
1771
1772
0
        if (ap_cstr_casecmp(msg, "default") == 0) {
1773
            /* special case: ErrorDocument 404 default restores the
1774
             * canned server error response
1775
             */
1776
0
            apr_hash_set(conf->response_code_exprs,
1777
0
                    apr_pmemdup(cmd->pool, &index_number, sizeof(index_number)),
1778
0
                    sizeof(index_number), &errordocument_default);
1779
0
        }
1780
0
        else {
1781
0
            ap_expr_info_t *expr;
1782
0
            const char *expr_err = NULL;
1783
1784
            /* hack. Prefix a " if it is a msg; as that is what
1785
             * http_protocol.c relies on to distinguish between
1786
             * a msg and a (local) path.
1787
             */
1788
0
            const char *response =
1789
0
                    (what == MSG) ? apr_pstrcat(cmd->pool, "\"", msg, NULL) :
1790
0
                            apr_pstrdup(cmd->pool, msg);
1791
1792
0
            expr = ap_expr_parse_cmd(cmd, response, AP_EXPR_FLAG_STRING_RESULT,
1793
0
                    &expr_err, NULL);
1794
1795
0
            if (expr_err) {
1796
0
                return apr_pstrcat(cmd->temp_pool,
1797
0
                                   "Cannot parse expression in ErrorDocument: ",
1798
0
                                   expr_err, NULL);
1799
0
            }
1800
1801
0
            apr_hash_set(conf->response_code_exprs,
1802
0
                    apr_pmemdup(cmd->pool, &index_number, sizeof(index_number)),
1803
0
                    sizeof(index_number), expr);
1804
1805
0
        }
1806
0
    }
1807
1808
0
    return NULL;
1809
0
}
1810
1811
static const char *set_allow_opts(cmd_parms *cmd, allow_options_t *opts,
1812
                                  const char *l)
1813
0
{
1814
0
    allow_options_t opt;
1815
0
    int first = 1;
1816
1817
0
    char *w, *p = (char *) l;
1818
0
    char *tok_state;
1819
1820
0
    while ((w = apr_strtok(p, ",", &tok_state)) != NULL) {
1821
1822
0
        if (first) {
1823
0
            p = NULL;
1824
0
            *opts = OPT_NONE;
1825
0
            first = 0;
1826
0
        }
1827
1828
0
        if (!ap_cstr_casecmp(w, "Indexes")) {
1829
0
            opt = OPT_INDEXES;
1830
0
        }
1831
0
        else if (!ap_cstr_casecmp(w, "Includes")) {
1832
            /* If Includes is permitted, both Includes and
1833
             * IncludesNOEXEC may be changed. */
1834
0
            opt = (OPT_INCLUDES | OPT_INC_WITH_EXEC);
1835
0
        }
1836
0
        else if (!ap_cstr_casecmp(w, "IncludesNOEXEC")) {
1837
0
            opt = OPT_INCLUDES;
1838
0
        }
1839
0
        else if (!ap_cstr_casecmp(w, "FollowSymLinks")) {
1840
0
            opt = OPT_SYM_LINKS;
1841
0
        }
1842
0
        else if (!ap_cstr_casecmp(w, "SymLinksIfOwnerMatch")) {
1843
0
            opt = OPT_SYM_OWNER;
1844
0
        }
1845
0
        else if (!ap_cstr_casecmp(w, "ExecCGI")) {
1846
0
            opt = OPT_EXECCGI;
1847
0
        }
1848
0
        else if (!ap_cstr_casecmp(w, "MultiViews")) {
1849
0
            opt = OPT_MULTI;
1850
0
        }
1851
0
        else if (!ap_cstr_casecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
1852
0
            opt = OPT_MULTI|OPT_EXECCGI;
1853
0
        }
1854
0
        else if (!ap_cstr_casecmp(w, "None")) {
1855
0
            opt = OPT_NONE;
1856
0
        }
1857
0
        else if (!ap_cstr_casecmp(w, "All")) {
1858
0
            opt = OPT_ALL;
1859
0
        }
1860
0
        else {
1861
0
            return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
1862
0
        }
1863
1864
0
        *opts |= opt;
1865
0
    }
1866
1867
0
    (*opts) &= (~OPT_UNSET);
1868
1869
0
    return NULL;
1870
0
}
1871
1872
static const char *set_override(cmd_parms *cmd, void *d_, const char *l)
1873
0
{
1874
0
    core_dir_config *d = d_;
1875
0
    char *w;
1876
0
    char *k, *v;
1877
0
    const char *err;
1878
1879
    /* Throw a warning if we're in <Location> or <Files> */
1880
0
    if (ap_check_cmd_context(cmd, NOT_IN_LOCATION | NOT_IN_FILES)) {
1881
0
        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(00114)
1882
0
                     "Useless use of AllowOverride in line %d of %s.",
1883
0
                     cmd->directive->line_num, cmd->directive->filename);
1884
0
    }
1885
0
    if ((err = ap_check_cmd_context(cmd, NOT_IN_HTACCESS)) != NULL)
1886
0
        return err;
1887
1888
0
    d->override = OR_NONE;
1889
0
    while (l[0]) {
1890
0
        w = ap_getword_conf(cmd->temp_pool, &l);
1891
1892
0
        k = w;
1893
0
        v = strchr(k, '=');
1894
0
        if (v) {
1895
0
                *v++ = '\0';
1896
0
        }
1897
1898
0
        if (!ap_cstr_casecmp(w, "Limit")) {
1899
0
            d->override |= OR_LIMIT;
1900
0
        }
1901
0
        else if (!ap_cstr_casecmp(k, "Options")) {
1902
0
            d->override |= OR_OPTIONS;
1903
0
            if (v)
1904
0
                set_allow_opts(cmd, &(d->override_opts), v);
1905
0
            else
1906
0
                d->override_opts = OPT_ALL;
1907
0
        }
1908
0
        else if (!ap_cstr_casecmp(w, "FileInfo")) {
1909
0
            d->override |= OR_FILEINFO;
1910
0
        }
1911
0
        else if (!ap_cstr_casecmp(w, "AuthConfig")) {
1912
0
            d->override |= OR_AUTHCFG;
1913
0
        }
1914
0
        else if (!ap_cstr_casecmp(w, "Indexes")) {
1915
0
            d->override |= OR_INDEXES;
1916
0
        }
1917
0
        else if (!ap_cstr_casecmp(w, "Nonfatal")) {
1918
0
            if (!v) {
1919
0
                return apr_pstrcat(cmd->pool, "=Override, =Unknown or =All expected after ", w, NULL);
1920
0
            }
1921
0
            else if (!ap_cstr_casecmp(v, "Override")) {
1922
0
                d->override |= NONFATAL_OVERRIDE;
1923
0
            }
1924
0
            else if (!ap_cstr_casecmp(v, "Unknown")) {
1925
0
                d->override |= NONFATAL_UNKNOWN;
1926
0
            }
1927
0
            else if (!ap_cstr_casecmp(v, "All")) {
1928
0
                d->override |= NONFATAL_ALL;
1929
0
            }
1930
0
        }
1931
0
        else if (!ap_cstr_casecmp(w, "None")) {
1932
0
            d->override = OR_NONE;
1933
0
        }
1934
0
        else if (!ap_cstr_casecmp(w, "All")) {
1935
0
            d->override = OR_ALL;
1936
0
        }
1937
0
        else {
1938
0
            return apr_pstrcat(cmd->pool, "Illegal override option ", w, NULL);
1939
0
        }
1940
1941
0
        d->override &= ~OR_UNSET;
1942
0
    }
1943
1944
0
    return NULL;
1945
0
}
1946
1947
static const char *set_cgi_pass_auth(cmd_parms *cmd, void *d_, int flag)
1948
0
{
1949
0
    core_dir_config *d = d_;
1950
1951
0
    d->cgi_pass_auth = flag ? AP_CGI_PASS_AUTH_ON : AP_CGI_PASS_AUTH_OFF;
1952
1953
0
    return NULL;
1954
0
}
1955
1956
static const char *set_cgi_var(cmd_parms *cmd, void *d_,
1957
                               const char *var, const char *rule_)
1958
0
{
1959
0
    core_dir_config *d = d_;
1960
0
    char *rule = apr_pstrdup(cmd->pool, rule_);
1961
1962
0
    ap_str_tolower(rule);
1963
1964
0
    if (!strcmp(var, "REQUEST_URI")) {
1965
0
        if (strcmp(rule, "current-uri") && strcmp(rule, "original-uri")) {
1966
0
            return "Valid rules for REQUEST_URI are 'current-uri' and 'original-uri'";
1967
0
        }
1968
0
    }
1969
0
    else {
1970
0
        return apr_pstrcat(cmd->pool, "Unrecognized CGI variable: \"",
1971
0
                           var, "\"", NULL);
1972
0
    }
1973
1974
0
    if (!d->cgi_var_rules) {
1975
0
        d->cgi_var_rules = apr_hash_make(cmd->pool);
1976
0
    }
1977
0
    apr_hash_set(d->cgi_var_rules, var, APR_HASH_KEY_STRING, rule);
1978
0
    return NULL;
1979
0
}
1980
1981
static const char *set_qualify_redirect_url(cmd_parms *cmd, void *d_, int flag)
1982
0
{
1983
0
    core_dir_config *d = d_;
1984
1985
0
    d->qualify_redirect_url = flag ? AP_CORE_CONFIG_ON : AP_CORE_CONFIG_OFF;
1986
1987
0
    return NULL;
1988
0
}
1989
static const char *set_core_server_flag(cmd_parms *cmd, void *s_, int flag)
1990
0
{
1991
0
    core_server_config *conf =
1992
0
        ap_get_core_module_config(cmd->server->module_config);
1993
0
    return ap_set_flag_slot(cmd, conf, flag);
1994
0
}
1995
static const char *set_override_list(cmd_parms *cmd, void *d_, int argc, char *const argv[])
1996
0
{
1997
0
    core_dir_config *d = d_;
1998
0
    int i;
1999
0
    const char *err;
2000
2001
    /* Throw a warning if we're in <Location> or <Files> */
2002
0
    if (ap_check_cmd_context(cmd, NOT_IN_LOCATION | NOT_IN_FILES)) {
2003
0
        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(00115)
2004
0
                     "Useless use of AllowOverrideList at %s:%d",
2005
0
                     cmd->directive->filename, cmd->directive->line_num);
2006
0
    }
2007
0
    if ((err = ap_check_cmd_context(cmd, NOT_IN_HTACCESS)) != NULL)
2008
0
        return err;
2009
2010
0
    d->override_list = apr_table_make(cmd->pool, argc);
2011
2012
0
    for (i = 0; i < argc; i++) {
2013
0
        if (!ap_cstr_casecmp(argv[i], "None")) {
2014
0
            if (argc != 1) {
2015
0
                return "'None' not allowed with other directives in "
2016
0
                       "AllowOverrideList";
2017
0
            }
2018
0
            return NULL;
2019
0
        }
2020
0
        else {
2021
0
            const command_rec *result = NULL;
2022
0
            module *mod = ap_top_module;
2023
2024
0
            result = ap_find_command_in_modules(argv[i], &mod);
2025
0
            if (result == NULL) {
2026
0
                ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2027
0
                             APLOGNO(00116) "Discarding unrecognized "
2028
0
                             "directive `%s' in AllowOverrideList at %s:%d",
2029
0
                             argv[i], cmd->directive->filename,
2030
0
                             cmd->directive->line_num);
2031
0
                continue;
2032
0
            }
2033
0
            else if ((result->req_override & (OR_ALL|ACCESS_CONF)) == 0) {
2034
0
                ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server,
2035
0
                             APLOGNO(02304) "Discarding directive `%s' not "
2036
0
                             "allowed in AllowOverrideList at %s:%d",
2037
0
                             argv[i], cmd->directive->filename,
2038
0
                             cmd->directive->line_num);
2039
0
                continue;
2040
0
            }
2041
0
            else {
2042
0
                apr_table_setn(d->override_list, argv[i], "1");
2043
0
            }
2044
0
        }
2045
0
    }
2046
2047
0
    return NULL;
2048
0
}
2049
2050
static const char *set_options(cmd_parms *cmd, void *d_, const char *l)
2051
0
{
2052
0
    core_dir_config *d = d_;
2053
0
    allow_options_t opt;
2054
0
    int first = 1;
2055
0
    int merge = 0;
2056
0
    int all_none = 0;
2057
0
    char action;
2058
2059
0
    while (l[0]) {
2060
0
        char *w = ap_getword_conf(cmd->temp_pool, &l);
2061
0
        action = '\0';
2062
2063
0
        if (*w == '+' || *w == '-') {
2064
0
            action = *(w++);
2065
0
            if (!merge && !first && !all_none) {
2066
0
                return "Either all Options must start with + or -, or no Option may.";
2067
0
            }
2068
0
            merge = 1;
2069
0
        }
2070
0
        else if (first) {
2071
0
            d->opts = OPT_NONE;
2072
0
        }
2073
0
        else if (merge) {
2074
0
            return "Either all Options must start with + or -, or no Option may.";
2075
0
        }
2076
2077
0
        if (!ap_cstr_casecmp(w, "Indexes")) {
2078
0
            opt = OPT_INDEXES;
2079
0
        }
2080
0
        else if (!ap_cstr_casecmp(w, "Includes")) {
2081
0
            opt = (OPT_INCLUDES | OPT_INC_WITH_EXEC);
2082
0
        }
2083
0
        else if (!ap_cstr_casecmp(w, "IncludesNOEXEC")) {
2084
0
            opt = OPT_INCLUDES;
2085
0
        }
2086
0
        else if (!ap_cstr_casecmp(w, "FollowSymLinks")) {
2087
0
            opt = OPT_SYM_LINKS;
2088
0
        }
2089
0
        else if (!ap_cstr_casecmp(w, "SymLinksIfOwnerMatch")) {
2090
0
            opt = OPT_SYM_OWNER;
2091
0
        }
2092
0
        else if (!ap_cstr_casecmp(w, "ExecCGI")) {
2093
0
            opt = OPT_EXECCGI;
2094
0
        }
2095
0
        else if (!ap_cstr_casecmp(w, "MultiViews")) {
2096
0
            opt = OPT_MULTI;
2097
0
        }
2098
0
        else if (!ap_cstr_casecmp(w, "RunScripts")) { /* AI backcompat. Yuck */
2099
0
            opt = OPT_MULTI|OPT_EXECCGI;
2100
0
        }
2101
0
        else if (!ap_cstr_casecmp(w, "None")) {
2102
0
            if (!first) {
2103
0
                return "'Options None' must be the first Option given.";
2104
0
            }
2105
0
            else if (merge) { /* Only works since None may not follow any other option. */
2106
0
                return "You may not use 'Options +None' or 'Options -None'.";
2107
0
            }
2108
0
            opt = OPT_NONE;
2109
0
            all_none = 1;
2110
0
        }
2111
0
        else if (!ap_cstr_casecmp(w, "All")) {
2112
0
            if (!first) {
2113
0
                return "'Options All' must be the first option given.";
2114
0
            }
2115
0
            else if (merge) { /* Only works since All may not follow any other option. */
2116
0
                return "You may not use 'Options +All' or 'Options -All'.";
2117
0
            }
2118
0
            opt = OPT_ALL;
2119
0
            all_none = 1;
2120
0
        }
2121
0
        else {
2122
0
            return apr_pstrcat(cmd->pool, "Illegal option ", w, NULL);
2123
0
        }
2124
2125
0
        if ( (cmd->override_opts & opt) != opt ) {
2126
0
            return apr_pstrcat(cmd->pool, "Option ", w, " not allowed here", NULL);
2127
0
        }
2128
0
        else if (action == '-') {
2129
            /* we ensure the invariant (d->opts_add & d->opts_remove) == 0 */
2130
0
            d->opts_remove |= opt;
2131
0
            d->opts_add &= ~opt;
2132
0
            d->opts &= ~opt;
2133
0
        }
2134
0
        else if (action == '+') {
2135
0
            d->opts_add |= opt;
2136
0
            d->opts_remove &= ~opt;
2137
0
            d->opts |= opt;
2138
0
        }
2139
0
        else {
2140
0
            d->opts |= opt;
2141
0
        }
2142
2143
0
        first = 0;
2144
0
    }
2145
2146
0
    return NULL;
2147
0
}
2148
2149
static const char *set_default_type(cmd_parms *cmd, void *d_,
2150
                                   const char *arg)
2151
0
{
2152
0
    if (ap_cstr_casecmp(arg, "off") != 0 && ap_cstr_casecmp(arg, "none") != 0) {
2153
0
        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(00117)
2154
0
              "Ignoring deprecated use of DefaultType in line %d of %s.",
2155
0
                     cmd->directive->line_num, cmd->directive->filename);
2156
0
    }
2157
2158
0
    return NULL;
2159
0
}
2160
2161
static const char *set_sethandler(cmd_parms *cmd,
2162
                                     void *d_,
2163
                                     const char *arg_)
2164
0
{
2165
0
    core_dir_config *dirconf = d_;
2166
0
    const char *err;
2167
0
    dirconf->expr_handler = ap_expr_parse_cmd(cmd, arg_,
2168
0
                                          AP_EXPR_FLAG_STRING_RESULT,
2169
0
                                          &err, NULL);
2170
0
    if (err) {
2171
0
        return apr_pstrcat(cmd->pool,
2172
0
                "Can't parse expression : ", err, NULL);
2173
0
    }
2174
0
    return NULL;
2175
0
}
2176
2177
/*
2178
 * Note what data should be used when forming file ETag values.
2179
 * It would be nicer to do this as an ITERATE, but then we couldn't
2180
 * remember the +/- state properly.
2181
 */
2182
static const char *set_etag_bits(cmd_parms *cmd, void *mconfig,
2183
                                 const char *args_p)
2184
0
{
2185
0
    core_dir_config *cfg;
2186
0
    etag_components_t bit;
2187
0
    char action;
2188
0
    char *token;
2189
0
    const char *args;
2190
0
    int valid;
2191
0
    int first;
2192
0
    int explicit;
2193
2194
0
    cfg = (core_dir_config *)mconfig;
2195
2196
0
    args = args_p;
2197
0
    first = 1;
2198
0
    explicit = 0;
2199
0
    while (args[0] != '\0') {
2200
0
        action = '*';
2201
0
        bit = ETAG_UNSET;
2202
0
        valid = 1;
2203
0
        token = ap_getword_conf(cmd->temp_pool, &args);
2204
0
        if ((*token == '+') || (*token == '-')) {
2205
0
            action = *token;
2206
0
            token++;
2207
0
        }
2208
0
        else {
2209
            /*
2210
             * The occurrence of an absolute setting wipes
2211
             * out any previous relative ones.  The first such
2212
             * occurrence forgets any inherited ones, too.
2213
             */
2214
0
            if (first) {
2215
0
                cfg->etag_bits = ETAG_UNSET;
2216
0
                cfg->etag_add = ETAG_UNSET;
2217
0
                cfg->etag_remove = ETAG_UNSET;
2218
0
                first = 0;
2219
0
            }
2220
0
        }
2221
2222
0
        if (ap_cstr_casecmp(token, "None") == 0) {
2223
0
            if (action != '*') {
2224
0
                valid = 0;
2225
0
            }
2226
0
            else {
2227
0
                cfg->etag_bits = bit = ETAG_NONE;
2228
0
                explicit = 1;
2229
0
            }
2230
0
        }
2231
0
        else if (ap_cstr_casecmp(token, "All") == 0) {
2232
0
            if (action != '*') {
2233
0
                valid = 0;
2234
0
            }
2235
0
            else {
2236
0
                explicit = 1;
2237
0
                cfg->etag_bits = bit = ETAG_ALL;
2238
0
            }
2239
0
        }
2240
0
        else if (ap_cstr_casecmp(token, "Size") == 0) {
2241
0
            bit = ETAG_SIZE;
2242
0
        }
2243
0
        else if ((ap_cstr_casecmp(token, "LMTime") == 0)
2244
0
                 || (ap_cstr_casecmp(token, "MTime") == 0)
2245
0
                 || (ap_cstr_casecmp(token, "LastModified") == 0)) {
2246
0
            bit = ETAG_MTIME;
2247
0
        }
2248
0
        else if (ap_cstr_casecmp(token, "INode") == 0) {
2249
0
            bit = ETAG_INODE;
2250
0
        }
2251
0
        else if (ap_cstr_casecmp(token, "Digest") == 0) {
2252
0
            bit = ETAG_DIGEST;
2253
0
        }
2254
0
        else {
2255
0
            return apr_pstrcat(cmd->pool, "Unknown keyword '",
2256
0
                               token, "' for ", cmd->cmd->name,
2257
0
                               " directive", NULL);
2258
0
        }
2259
2260
0
        if (! valid) {
2261
0
            return apr_pstrcat(cmd->pool, cmd->cmd->name, " keyword '",
2262
0
                               token, "' cannot be used with '+' or '-'",
2263
0
                               NULL);
2264
0
        }
2265
2266
0
        if (action == '+') {
2267
            /*
2268
             * Make sure it's in the 'add' list and absent from the
2269
             * 'subtract' list.
2270
             */
2271
0
            cfg->etag_add |= bit;
2272
0
            cfg->etag_remove &= (~ bit);
2273
0
        }
2274
0
        else if (action == '-') {
2275
0
            cfg->etag_remove |= bit;
2276
0
            cfg->etag_add &= (~ bit);
2277
0
        }
2278
0
        else {
2279
            /*
2280
             * Non-relative values wipe out any + or - values
2281
             * accumulated so far.
2282
             */
2283
0
            cfg->etag_bits |= bit;
2284
0
            cfg->etag_add = ETAG_UNSET;
2285
0
            cfg->etag_remove = ETAG_UNSET;
2286
0
            explicit = 1;
2287
0
        }
2288
0
    }
2289
2290
    /*
2291
     * Any setting at all will clear the 'None' and 'Unset' bits.
2292
     */
2293
2294
0
    if (cfg->etag_add != ETAG_UNSET) {
2295
0
        cfg->etag_add &= (~ ETAG_UNSET);
2296
0
    }
2297
2298
0
    if (cfg->etag_remove != ETAG_UNSET) {
2299
0
        cfg->etag_remove &= (~ ETAG_UNSET);
2300
0
    }
2301
2302
0
    if (explicit) {
2303
0
        cfg->etag_bits &= (~ ETAG_UNSET);
2304
2305
0
        if ((cfg->etag_bits & ETAG_NONE) != ETAG_NONE) {
2306
0
            cfg->etag_bits &= (~ ETAG_NONE);
2307
0
        }
2308
0
    }
2309
2310
0
    return NULL;
2311
0
}
2312
2313
static const char *set_enable_mmap(cmd_parms *cmd, void *d_,
2314
                                   const char *arg)
2315
0
{
2316
0
    core_dir_config *d = d_;
2317
2318
0
    if (ap_cstr_casecmp(arg, "on") == 0) {
2319
0
        d->enable_mmap = ENABLE_MMAP_ON;
2320
0
    }
2321
0
    else if (ap_cstr_casecmp(arg, "off") == 0) {
2322
0
        d->enable_mmap = ENABLE_MMAP_OFF;
2323
0
    }
2324
0
    else {
2325
0
        return "parameter must be 'on' or 'off'";
2326
0
    }
2327
2328
0
    return NULL;
2329
0
}
2330
2331
static const char *set_enable_sendfile(cmd_parms *cmd, void *d_,
2332
                                   const char *arg)
2333
0
{
2334
0
    core_dir_config *d = d_;
2335
2336
0
    if (ap_cstr_casecmp(arg, "on") == 0) {
2337
0
        d->enable_sendfile = ENABLE_SENDFILE_ON;
2338
0
    }
2339
0
    else if (ap_cstr_casecmp(arg, "off") == 0) {
2340
0
        d->enable_sendfile = ENABLE_SENDFILE_OFF;
2341
0
    }
2342
0
    else {
2343
0
        return "parameter must be 'on' or 'off'";
2344
0
    }
2345
2346
0
    return NULL;
2347
0
}
2348
2349
static const char *set_read_buf_size(cmd_parms *cmd, void *d_,
2350
                                     const char *arg)
2351
0
{
2352
0
    core_dir_config *d = d_;
2353
0
    apr_off_t size;
2354
0
    char *end;
2355
2356
0
    if (apr_strtoff(&size, arg, &end, 10)
2357
0
            || *end || size < 0 || size > APR_UINT32_MAX)
2358
0
        return apr_pstrcat(cmd->pool,
2359
0
                           "parameter must be a number between 0 and "
2360
0
                           APR_STRINGIFY(APR_UINT32_MAX) "): ",
2361
0
                           arg, NULL);
2362
2363
0
    d->read_buf_size = (apr_size_t)size;
2364
2365
0
    return NULL;
2366
0
}
2367
2368
static const char *set_flush_max_threshold(cmd_parms *cmd, void *d_,
2369
                                           const char *arg)
2370
0
{
2371
0
    core_server_config *conf =
2372
0
        ap_get_core_module_config(cmd->server->module_config);
2373
0
    apr_off_t size;
2374
0
    char *end;
2375
2376
0
    if (apr_strtoff(&size, arg, &end, 10)
2377
0
            || *end || size < 0 || size > APR_UINT32_MAX)
2378
0
        return apr_pstrcat(cmd->pool,
2379
0
                           "parameter must be a number between 0 and "
2380
0
                           APR_STRINGIFY(APR_UINT32_MAX) "): ",
2381
0
                           arg, NULL);
2382
2383
0
    conf->flush_max_threshold = (apr_size_t)size;
2384
2385
0
    return NULL;
2386
0
}
2387
2388
static const char *set_flush_max_pipelined(cmd_parms *cmd, void *d_,
2389
                                           const char *arg)
2390
0
{
2391
0
    core_server_config *conf =
2392
0
        ap_get_core_module_config(cmd->server->module_config);
2393
0
    apr_off_t num;
2394
0
    char *end;
2395
2396
0
    if (apr_strtoff(&num, arg, &end, 10)
2397
0
            || *end || num < -1 || num > APR_INT32_MAX)
2398
0
        return apr_pstrcat(cmd->pool,
2399
0
                           "parameter must be a number between -1 and "
2400
0
                           APR_STRINGIFY(APR_INT32_MAX) ": ",
2401
0
                           arg, NULL);
2402
2403
0
    conf->flush_max_pipelined = (apr_int32_t)num;
2404
2405
0
    return NULL;
2406
0
}
2407
2408
/*
2409
 * Report a missing-'>' syntax error.
2410
 */
2411
static char *unclosed_directive(cmd_parms *cmd)
2412
0
{
2413
0
    return apr_pstrcat(cmd->pool, cmd->cmd->name,
2414
0
                       "> directive missing closing '>'", NULL);
2415
0
}
2416
2417
/*
2418
 * Report a missing args in '<Foo >' syntax error.
2419
 */
2420
static char *missing_container_arg(cmd_parms *cmd)
2421
0
{
2422
0
    return apr_pstrcat(cmd->pool, cmd->cmd->name,
2423
0
                       "> directive requires additional arguments", NULL);
2424
0
}
2425
2426
AP_CORE_DECLARE_NONSTD(const char *) ap_limit_section(cmd_parms *cmd,
2427
                                                      void *dummy,
2428
                                                      const char *arg)
2429
0
{
2430
0
    const char *endp = ap_strrchr_c(arg, '>');
2431
0
    const char *limited_methods;
2432
0
    void *tog = cmd->cmd->cmd_data;
2433
0
    apr_int64_t limited = 0;
2434
0
    apr_int64_t old_limited = cmd->limited;
2435
0
    const char *errmsg;
2436
2437
0
    if (endp == NULL) {
2438
0
        return unclosed_directive(cmd);
2439
0
    }
2440
2441
0
    limited_methods = apr_pstrmemdup(cmd->temp_pool, arg, endp - arg);
2442
2443
0
    if (!limited_methods[0]) {
2444
0
        return missing_container_arg(cmd);
2445
0
    }
2446
2447
0
    while (limited_methods[0]) {
2448
0
        char *method = ap_getword_conf(cmd->temp_pool, &limited_methods);
2449
0
        int methnum;
2450
2451
        /* check for builtin or module registered method number */
2452
0
        methnum = ap_method_number_of(method);
2453
2454
0
        if (methnum == M_TRACE && !tog) {
2455
0
            return "TRACE cannot be controlled by <Limit>, see TraceEnable";
2456
0
        }
2457
0
        else if (methnum == M_INVALID) {
2458
            /* method has not been registered yet, but resource restriction
2459
             * is always checked before method handling, so register it.
2460
             */
2461
0
            if (cmd->pool == cmd->temp_pool) {
2462
                /* In .htaccess, we can't globally register new methods. */
2463
0
                return apr_psprintf(cmd->pool, "Could not register method '%s' "
2464
0
                                   "for %s from .htaccess configuration",
2465
0
                                    method, cmd->cmd->name);
2466
0
            }
2467
0
            methnum = ap_method_register(cmd->pool,
2468
0
                                         apr_pstrdup(cmd->pool, method));
2469
0
        }
2470
2471
0
        limited |= (AP_METHOD_BIT << methnum);
2472
0
    }
2473
2474
    /* Killing two features with one function,
2475
     * if (tog == NULL) <Limit>, else <LimitExcept>
2476
     */
2477
0
    limited = tog ? ~limited : limited;
2478
2479
0
    if (!(old_limited & limited)) {
2480
0
        return apr_pstrcat(cmd->pool, cmd->cmd->name,
2481
0
                           "> directive excludes all methods", NULL);
2482
0
    }
2483
0
    else if ((old_limited & limited) == old_limited) {
2484
0
        return apr_pstrcat(cmd->pool, cmd->cmd->name,
2485
0
                           "> directive specifies methods already excluded",
2486
0
                           NULL);
2487
0
    }
2488
2489
0
    cmd->limited &= limited;
2490
2491
0
    errmsg = ap_walk_config(cmd->directive->first_child, cmd, cmd->context);
2492
2493
0
    cmd->limited = old_limited;
2494
2495
0
    return errmsg;
2496
0
}
2497
2498
/* XXX: Bogus - need to do this differently (at least OS2/Netware suffer
2499
 * the same problem!!!
2500
 * We use this in <DirectoryMatch> and <FilesMatch>, to ensure that
2501
 * people don't get bitten by wrong-cased regex matches
2502
 */
2503
2504
#ifdef WIN32
2505
#define USE_ICASE AP_REG_ICASE
2506
#else
2507
0
#define USE_ICASE 0
2508
#endif
2509
2510
static const char *dirsection(cmd_parms *cmd, void *mconfig, const char *arg)
2511
0
{
2512
0
    const char *errmsg;
2513
0
    const char *endp = ap_strrchr_c(arg, '>');
2514
0
    int old_overrides = cmd->override;
2515
0
    char *old_path = cmd->path;
2516
0
    ap_regex_t *old_regex = cmd->regex;
2517
0
    core_dir_config *conf;
2518
0
    ap_conf_vector_t *new_dir_conf = ap_create_per_dir_config(cmd->pool);
2519
0
    const command_rec *thiscmd = cmd->cmd;
2520
2521
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
2522
0
    if (err != NULL) {
2523
0
        return err;
2524
0
    }
2525
2526
0
    if (endp == NULL) {
2527
0
        return unclosed_directive(cmd);
2528
0
    }
2529
2530
0
    arg = apr_pstrndup(cmd->temp_pool, arg, endp - arg);
2531
2532
0
    if (!arg[0]) {
2533
0
        return missing_container_arg(cmd);
2534
0
    }
2535
2536
0
    cmd->path = ap_getword_conf(cmd->pool, &arg);
2537
0
    cmd->override = OR_ALL|ACCESS_CONF;
2538
2539
0
    if (!strcmp(cmd->path, "~")) {
2540
0
        cmd->path = ap_getword_conf(cmd->pool, &arg);
2541
0
        if (!cmd->path) {
2542
0
            return "<Directory ~ > block must specify a path";
2543
0
        }
2544
0
        cmd->regex = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
2545
0
        if (!cmd->regex) {
2546
0
            return "Regex could not be compiled";
2547
0
        }
2548
0
    }
2549
0
    else if (thiscmd->cmd_data) { /* <DirectoryMatch> */
2550
0
        cmd->regex = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
2551
0
        if (!cmd->regex) {
2552
0
            return "Regex could not be compiled";
2553
0
        }
2554
0
    }
2555
0
    else if (strcmp(cmd->path, "/") != 0) {
2556
0
        int run_mode = ap_state_query(AP_SQ_RUN_MODE);
2557
0
        apr_status_t rv;
2558
0
        char *newpath;
2559
2560
0
        cmd->regex = NULL;
2561
2562
        /*
2563
         * Ensure that the pathname is canonical, and append the trailing /
2564
         */
2565
0
        rv = apr_filepath_merge(&newpath, NULL, cmd->path,
2566
0
                                APR_FILEPATH_TRUENAME, cmd->pool);
2567
0
        if (rv != APR_SUCCESS && rv != APR_EPATHWILD) {
2568
0
            return apr_pstrcat(cmd->pool, "<Directory \"", cmd->path,
2569
0
                               "\"> path is invalid.", NULL);
2570
0
        }
2571
2572
0
        if (run_mode == AP_SQ_RM_CONFIG_TEST &&
2573
0
            !ap_is_directory(cmd->temp_pool, cmd->path)) {
2574
0
            ap_log_perror(APLOG_MARK, APLOG_STARTUP, 0,
2575
0
                          cmd->temp_pool, APLOGNO(10234)
2576
0
                          "Warning: <Directory \"%s\"> does not exist or is not a directory",
2577
0
                          cmd->path);
2578
0
        }
2579
2580
0
        cmd->path = newpath;
2581
0
        if (cmd->path[strlen(cmd->path) - 1] != '/')
2582
0
            cmd->path = apr_pstrcat(cmd->pool, cmd->path, "/", NULL);
2583
0
    }
2584
2585
    /* initialize our config and fetch it */
2586
0
    conf = ap_set_config_vectors(cmd->server, new_dir_conf, cmd->path,
2587
0
                                 &core_module, cmd->pool);
2588
2589
0
    errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_dir_conf);
2590
0
    if (errmsg != NULL)
2591
0
        return errmsg;
2592
2593
0
    conf->r = cmd->regex;
2594
0
    conf->d = cmd->path;
2595
0
    conf->d_is_fnmatch = (apr_fnmatch_test(conf->d) != 0);
2596
2597
0
    if (cmd->regex) {
2598
0
        conf->refs = apr_array_make(cmd->pool, 8, sizeof(char *));
2599
0
        ap_regname(cmd->regex, conf->refs, AP_REG_MATCH, 1);
2600
0
    }
2601
2602
    /* Make this explicit - the "/" root has 0 elements, that is, we
2603
     * will always merge it, and it will always sort and merge first.
2604
     * All others are sorted and tested by the number of slashes.
2605
     */
2606
0
    if (strcmp(conf->d, "/") == 0)
2607
0
        conf->d_components = 0;
2608
0
    else
2609
0
        conf->d_components = ap_count_dirs(conf->d);
2610
2611
0
    ap_add_per_dir_conf(cmd->server, new_dir_conf);
2612
2613
0
    if (*arg != '\0') {
2614
0
        return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
2615
0
                           "> arguments not (yet) supported.", NULL);
2616
0
    }
2617
2618
0
    cmd->path = old_path;
2619
0
    cmd->override = old_overrides;
2620
0
    cmd->regex = old_regex;
2621
2622
0
    return NULL;
2623
0
}
2624
2625
static const char *urlsection(cmd_parms *cmd, void *mconfig, const char *arg)
2626
0
{
2627
0
    const char *errmsg;
2628
0
    const char *endp = ap_strrchr_c(arg, '>');
2629
0
    int old_overrides = cmd->override;
2630
0
    char *old_path = cmd->path;
2631
0
    ap_regex_t *old_regex = cmd->regex;
2632
0
    core_dir_config *conf;
2633
0
    const command_rec *thiscmd = cmd->cmd;
2634
0
    ap_conf_vector_t *new_url_conf = ap_create_per_dir_config(cmd->pool);
2635
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
2636
0
    if (err != NULL) {
2637
0
        return err;
2638
0
    }
2639
2640
0
    if (endp == NULL) {
2641
0
        return unclosed_directive(cmd);
2642
0
    }
2643
2644
0
    arg = apr_pstrndup(cmd->temp_pool, arg, endp - arg);
2645
2646
0
    if (!arg[0]) {
2647
0
        return missing_container_arg(cmd);
2648
0
    }
2649
2650
0
    cmd->path = ap_getword_conf(cmd->pool, &arg);
2651
0
    cmd->override = OR_ALL|ACCESS_CONF;
2652
2653
0
    if (thiscmd->cmd_data) { /* <LocationMatch> */
2654
0
        cmd->regex = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
2655
0
        if (!cmd->regex) {
2656
0
            return "Regex could not be compiled";
2657
0
        }
2658
0
    }
2659
0
    else if (!strcmp(cmd->path, "~")) {
2660
0
        cmd->path = ap_getword_conf(cmd->pool, &arg);
2661
0
        cmd->regex = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED);
2662
0
        if (!cmd->regex) {
2663
0
            return "Regex could not be compiled";
2664
0
        }
2665
0
    }
2666
0
    else {
2667
0
        cmd->regex = NULL;
2668
0
    }
2669
2670
    /* initialize our config and fetch it */
2671
0
    conf = ap_set_config_vectors(cmd->server, new_url_conf, cmd->path,
2672
0
                                 &core_module, cmd->pool);
2673
2674
0
    errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_url_conf);
2675
0
    if (errmsg != NULL)
2676
0
        return errmsg;
2677
2678
0
    conf->d = apr_pstrdup(cmd->pool, cmd->path);     /* No mangling, please */
2679
0
    conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
2680
0
    conf->r = cmd->regex;
2681
2682
0
    if (cmd->regex) {
2683
0
        conf->refs = apr_array_make(cmd->pool, 8, sizeof(char *));
2684
0
        ap_regname(cmd->regex, conf->refs, AP_REG_MATCH, 1);
2685
0
    }
2686
2687
0
    ap_add_per_url_conf(cmd->server, new_url_conf);
2688
2689
0
    if (*arg != '\0') {
2690
0
        return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
2691
0
                           "> arguments not (yet) supported.", NULL);
2692
0
    }
2693
2694
0
    cmd->path = old_path;
2695
0
    cmd->override = old_overrides;
2696
0
    cmd->regex = old_regex;
2697
2698
0
    return NULL;
2699
0
}
2700
2701
static const char *filesection(cmd_parms *cmd, void *mconfig, const char *arg)
2702
0
{
2703
0
    const char *errmsg;
2704
0
    const char *endp = ap_strrchr_c(arg, '>');
2705
0
    int old_overrides = cmd->override;
2706
0
    char *old_path = cmd->path;
2707
0
    ap_regex_t *old_regex = cmd->regex;
2708
0
    core_dir_config *conf;
2709
0
    const command_rec *thiscmd = cmd->cmd;
2710
0
    ap_conf_vector_t *new_file_conf = ap_create_per_dir_config(cmd->pool);
2711
0
    const char *err = ap_check_cmd_context(cmd,
2712
0
                                           NOT_IN_LOCATION | NOT_IN_LIMIT);
2713
2714
0
    if (err != NULL) {
2715
0
        return err;
2716
0
    }
2717
2718
0
    if (endp == NULL) {
2719
0
        return unclosed_directive(cmd);
2720
0
    }
2721
2722
0
    arg = apr_pstrndup(cmd->temp_pool, arg, endp - arg);
2723
2724
0
    if (!arg[0]) {
2725
0
        return missing_container_arg(cmd);
2726
0
    }
2727
2728
0
    cmd->path = ap_getword_conf(cmd->pool, &arg);
2729
    /* Only if not an .htaccess file */
2730
0
    if (!old_path) {
2731
0
        cmd->override = OR_ALL|ACCESS_CONF;
2732
0
    }
2733
2734
0
    if (thiscmd->cmd_data) { /* <FilesMatch> */
2735
0
        cmd->regex = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
2736
0
        if (!cmd->regex) {
2737
0
            return "Regex could not be compiled";
2738
0
        }
2739
0
    }
2740
0
    else if (!strcmp(cmd->path, "~")) {
2741
0
        cmd->path = ap_getword_conf(cmd->pool, &arg);
2742
0
        cmd->regex = ap_pregcomp(cmd->pool, cmd->path, AP_REG_EXTENDED|USE_ICASE);
2743
0
        if (!cmd->regex) {
2744
0
            return "Regex could not be compiled";
2745
0
        }
2746
0
    }
2747
0
    else {
2748
0
        char *newpath;
2749
2750
0
        cmd->regex = NULL;
2751
2752
        /* Ensure that the pathname is canonical, but we
2753
         * can't test the case/aliases without a fixed path */
2754
0
        if (apr_filepath_merge(&newpath, "", cmd->path,
2755
0
                               0, cmd->pool) != APR_SUCCESS)
2756
0
                return apr_pstrcat(cmd->pool, "<Files \"", cmd->path,
2757
0
                               "\"> is invalid.", NULL);
2758
0
        cmd->path = newpath;
2759
0
    }
2760
2761
    /* initialize our config and fetch it */
2762
0
    conf = ap_set_config_vectors(cmd->server, new_file_conf, cmd->path,
2763
0
                                 &core_module, cmd->pool);
2764
2765
0
    errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_file_conf);
2766
0
    if (errmsg != NULL)
2767
0
        return errmsg;
2768
2769
0
    conf->d = cmd->path;
2770
0
    conf->d_is_fnmatch = apr_fnmatch_test(conf->d) != 0;
2771
0
    conf->r = cmd->regex;
2772
2773
0
    if (cmd->regex) {
2774
0
        conf->refs = apr_array_make(cmd->pool, 8, sizeof(char *));
2775
0
        ap_regname(cmd->regex, conf->refs, AP_REG_MATCH, 1);
2776
0
    }
2777
2778
0
    ap_add_file_conf(cmd->pool, (core_dir_config *)mconfig, new_file_conf);
2779
2780
0
    if (*arg != '\0') {
2781
0
        return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
2782
0
                           "> arguments not (yet) supported.", NULL);
2783
0
    }
2784
2785
0
    cmd->path = old_path;
2786
0
    cmd->override = old_overrides;
2787
0
    cmd->regex = old_regex;
2788
2789
0
    return NULL;
2790
0
}
2791
2792
0
#define COND_IF      ((void *)1)
2793
0
#define COND_ELSE    ((void *)2)
2794
0
#define COND_ELSEIF  ((void *)3)
2795
2796
static const char *ifsection(cmd_parms *cmd, void *mconfig, const char *arg)
2797
0
{
2798
0
    const char *errmsg;
2799
0
    const char *endp = ap_strrchr_c(arg, '>');
2800
0
    int old_overrides = cmd->override;
2801
0
    char *old_path = cmd->path;
2802
0
    core_dir_config *conf;
2803
0
    const command_rec *thiscmd = cmd->cmd;
2804
0
    ap_conf_vector_t *new_if_conf = ap_create_per_dir_config(cmd->pool);
2805
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_LIMIT);
2806
0
    const char *condition;
2807
0
    const char *expr_err;
2808
2809
0
    if (err != NULL) {
2810
0
        return err;
2811
0
    }
2812
2813
0
    if (endp == NULL) {
2814
0
        return unclosed_directive(cmd);
2815
0
    }
2816
2817
0
    arg = apr_pstrndup(cmd->temp_pool, arg, endp - arg);
2818
2819
    /*
2820
     * Set a dummy value so that other directives notice that they are inside
2821
     * a config section.
2822
     */
2823
0
    cmd->path = "*If";
2824
    /* Only if not an .htaccess file */
2825
0
    if (!old_path) {
2826
0
        cmd->override = OR_ALL|ACCESS_CONF;
2827
0
    }
2828
2829
    /* initialize our config and fetch it */
2830
0
    conf = ap_set_config_vectors(cmd->server, new_if_conf, cmd->path,
2831
0
                                 &core_module, cmd->pool);
2832
2833
0
    if (cmd->cmd->cmd_data == COND_IF)
2834
0
        conf->condition_ifelse = AP_CONDITION_IF;
2835
0
    else if (cmd->cmd->cmd_data == COND_ELSEIF)
2836
0
        conf->condition_ifelse = AP_CONDITION_ELSEIF;
2837
0
    else if (cmd->cmd->cmd_data == COND_ELSE)
2838
0
        conf->condition_ifelse = AP_CONDITION_ELSE;
2839
0
    else
2840
0
        ap_assert(0);
2841
2842
0
    if (conf->condition_ifelse == AP_CONDITION_ELSE) {
2843
0
        if (arg[0])
2844
0
            return "<Else> does not take an argument";
2845
0
    }
2846
0
    else {
2847
0
        if (!arg[0])
2848
0
            return missing_container_arg(cmd);
2849
0
        condition = ap_getword_conf(cmd->pool, &arg);
2850
0
        conf->condition = ap_expr_parse_cmd(cmd, condition, 0, &expr_err, NULL);
2851
0
        if (expr_err)
2852
0
            return apr_psprintf(cmd->pool, "Cannot parse condition clause: %s",
2853
0
                                expr_err);
2854
0
    }
2855
2856
0
    errmsg = ap_walk_config(cmd->directive->first_child, cmd, new_if_conf);
2857
0
    if (errmsg != NULL)
2858
0
        return errmsg;
2859
2860
0
    conf->d = cmd->path;
2861
0
    conf->d_is_fnmatch = 0;
2862
0
    conf->r = NULL;
2863
2864
0
    errmsg = ap_add_if_conf(cmd->pool, (core_dir_config *)mconfig, new_if_conf);
2865
0
    if (errmsg != NULL)
2866
0
        return errmsg;
2867
2868
0
    if (*arg != '\0') {
2869
0
        return apr_pstrcat(cmd->pool, "Multiple ", thiscmd->name,
2870
0
                           "> arguments not supported.", NULL);
2871
0
    }
2872
2873
0
    cmd->path = old_path;
2874
0
    cmd->override = old_overrides;
2875
2876
0
    return NULL;
2877
0
}
2878
2879
static module *find_module(server_rec *s, const char *name)
2880
0
{
2881
0
    module *found = ap_find_linked_module(name);
2882
2883
    /* search prelinked stuff */
2884
0
    if (!found) {
2885
0
        ap_module_symbol_t *current = ap_prelinked_module_symbols;
2886
2887
0
        for (; current->name; ++current) {
2888
0
            if (!strcmp(current->name, name)) {
2889
0
                found = current->modp;
2890
0
                break;
2891
0
            }
2892
0
        }
2893
0
    }
2894
2895
    /* search dynamic stuff */
2896
0
    if (!found) {
2897
0
        APR_OPTIONAL_FN_TYPE(ap_find_loaded_module_symbol) *check_symbol =
2898
0
            APR_RETRIEVE_OPTIONAL_FN(ap_find_loaded_module_symbol);
2899
2900
0
        if (check_symbol) {
2901
            /*
2902
             * There are two phases where calling ap_find_loaded_module_symbol
2903
             * is problematic:
2904
             *
2905
             * During reading of the config, ap_server_conf is invalid but s
2906
             * points to the main server config, if passed from cmd->server
2907
             * of an EXEC_ON_READ directive.
2908
             *
2909
             * During config parsing, s may be a virtual host that would cause
2910
             * a segfault in mod_so if passed to ap_find_loaded_module_symbol,
2911
             * because mod_so's server config for vhosts is initialized later.
2912
             * But ap_server_conf is already set at this time.
2913
             *
2914
             * Therefore we use s if it is not virtual and ap_server_conf if
2915
             * s is virtual.
2916
             */
2917
0
            found = check_symbol(s->is_virtual ? ap_server_conf : s, name);
2918
0
        }
2919
0
    }
2920
2921
0
    return found;
2922
0
}
2923
2924
/* Callback function type used by start_cond_section. */
2925
typedef int (*test_cond_section_fn)(cmd_parms *cmd, const char *arg);
2926
2927
/* Implementation of <IfXXXXX>-style conditional sections.  Callback
2928
 * to test condition must be in cmd->info, matching function type
2929
 * test_cond_section_fn. */
2930
static const char *start_cond_section(cmd_parms *cmd, void *mconfig, const char *arg)
2931
0
{
2932
0
    const char *endp = ap_strrchr_c(arg, '>');
2933
0
    int result, not = (arg[0] == '!');
2934
0
    test_cond_section_fn testfn = (test_cond_section_fn)cmd->info;
2935
0
    const char *arg1;
2936
2937
0
    if (endp == NULL) {
2938
0
        return unclosed_directive(cmd);
2939
0
    }
2940
2941
0
    arg = apr_pstrmemdup(cmd->temp_pool, arg, endp - arg);
2942
2943
0
    if (not) {
2944
0
        arg++;
2945
0
    }
2946
2947
0
    arg1 = ap_getword_conf(cmd->temp_pool, &arg);
2948
2949
0
    if (!arg1[0]) {
2950
0
        return missing_container_arg(cmd);
2951
0
    }
2952
2953
0
    result = testfn(cmd, arg1);
2954
2955
0
    if ((!not && result) || (not && !result)) {
2956
0
        ap_directive_t *parent = NULL;
2957
0
        ap_directive_t *current = NULL;
2958
0
        const char *retval;
2959
2960
0
        retval = ap_build_cont_config(cmd->pool, cmd->temp_pool, cmd,
2961
0
                                      &current, &parent, cmd->cmd->name);
2962
0
        *(ap_directive_t **)mconfig = current;
2963
0
        return retval;
2964
0
    }
2965
0
    else {
2966
0
        *(ap_directive_t **)mconfig = NULL;
2967
0
        return ap_soak_end_container(cmd, cmd->cmd->name);
2968
0
    }
2969
0
}
2970
2971
/* Callback to implement <IfModule> test for start_cond_section. */
2972
static int test_ifmod_section(cmd_parms *cmd, const char *arg)
2973
0
{
2974
0
    return find_module(cmd->server, arg) != NULL;
2975
0
}
2976
2977
AP_DECLARE(int) ap_exists_config_define(const char *name)
2978
0
{
2979
0
    return ap_array_str_contains(ap_server_config_defines, name);
2980
0
}
2981
2982
static int test_ifdefine_section(cmd_parms *cmd, const char *arg)
2983
0
{
2984
0
    return ap_exists_config_define(arg);
2985
0
}
2986
2987
static int test_iffile_section(cmd_parms *cmd, const char *arg)
2988
0
{
2989
0
    const char *relative;
2990
0
    apr_finfo_t sb;
2991
2992
    /*
2993
     * At least on Windows, if the path we are testing is not valid (for example
2994
     * a path on a USB key that is not plugged), 'ap_server_root_relative()' will
2995
     * return NULL. In such a case, consider that the file is not there and that
2996
     * the section should be skipped.
2997
     */
2998
0
    relative = ap_server_root_relative(cmd->temp_pool, arg);
2999
0
    return (relative &&
3000
0
           (apr_stat(&sb, relative, APR_FINFO_TYPE, cmd->temp_pool) == APR_SUCCESS));
3001
0
}
3002
3003
static int test_ifdirective_section(cmd_parms *cmd, const char *arg)
3004
0
{
3005
0
    return ap_exists_directive(cmd->temp_pool, arg);
3006
0
}
3007
3008
static int test_ifsection_section(cmd_parms *cmd, const char *arg)
3009
0
{
3010
0
    const char *name = apr_pstrcat(cmd->temp_pool, "<", arg, NULL);
3011
0
    return ap_exists_directive(cmd->temp_pool, name);
3012
0
}
3013
3014
/* httpd.conf commands... beginning with the <VirtualHost> business */
3015
3016
static const char *virtualhost_section(cmd_parms *cmd, void *dummy,
3017
                                       const char *arg)
3018
0
{
3019
0
    server_rec *main_server = cmd->server, *s;
3020
0
    const char *errmsg;
3021
0
    const char *endp = ap_strrchr_c(arg, '>');
3022
0
    apr_pool_t *p = cmd->pool;
3023
3024
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3025
0
    if (err != NULL) {
3026
0
        return err;
3027
0
    }
3028
3029
0
    if (endp == NULL) {
3030
0
        return unclosed_directive(cmd);
3031
0
    }
3032
3033
0
    arg = apr_pstrndup(cmd->temp_pool, arg, endp - arg);
3034
3035
0
    if (!arg[0]) {
3036
0
        return missing_container_arg(cmd);
3037
0
    }
3038
3039
    /* FIXME: There's another feature waiting to happen here -- since you
3040
        can now put multiple addresses/names on a single <VirtualHost>
3041
        you might want to use it to group common definitions and then
3042
        define other "subhosts" with their individual differences.  But
3043
        personally I'd rather just do it with a macro preprocessor. -djg */
3044
0
    if (main_server->is_virtual) {
3045
0
        return "<VirtualHost> doesn't nest!";
3046
0
    }
3047
3048
0
    errmsg = ap_init_virtual_host(p, arg, main_server, &s);
3049
0
    if (errmsg) {
3050
0
        return errmsg;
3051
0
    }
3052
3053
0
    s->next = main_server->next;
3054
0
    main_server->next = s;
3055
3056
0
    s->defn_name = cmd->directive->filename;
3057
0
    s->defn_line_number = cmd->directive->line_num;
3058
3059
0
    cmd->server = s;
3060
3061
0
    errmsg = ap_walk_config(cmd->directive->first_child, cmd,
3062
0
                            s->lookup_defaults);
3063
3064
0
    cmd->server = main_server;
3065
3066
0
    return errmsg;
3067
0
}
3068
3069
static const char *set_regex_default_options(cmd_parms *cmd,
3070
                                             void *dummy,
3071
                                             const char *arg)
3072
0
{
3073
0
    const command_rec *thiscmd = cmd->cmd;
3074
0
    int cflags, cflag;
3075
3076
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3077
0
    if (err != NULL) {
3078
0
        return err;
3079
0
    }
3080
3081
0
    cflags = ap_regcomp_get_default_cflags();
3082
0
    while (*arg) {
3083
0
        const char *name = ap_getword_conf(cmd->pool, &arg);
3084
0
        int how = 0;
3085
3086
0
        if (strcasecmp(name, "none") == 0) {
3087
0
            cflags = 0;
3088
0
            continue;
3089
0
        }
3090
3091
0
        if (*name == '+') {
3092
0
            name++;
3093
0
            how = +1;
3094
0
        }
3095
0
        else if (*name == '-') {
3096
0
            name++;
3097
0
            how = -1;
3098
0
        }
3099
3100
0
        cflag = ap_regcomp_default_cflag_by_name(name);
3101
0
        if (!cflag) {
3102
0
            return apr_psprintf(cmd->pool, "%s: option '%s' unknown",
3103
0
                                thiscmd->name, name);
3104
0
        }
3105
3106
0
        if (how > 0) {
3107
0
            cflags |= cflag;
3108
0
        }
3109
0
        else if (how < 0) {
3110
0
            cflags &= ~cflag;
3111
0
        }
3112
0
        else {
3113
0
            cflags = cflag;
3114
0
        }
3115
0
    }
3116
0
    ap_regcomp_set_default_cflags(cflags);
3117
3118
0
    return NULL;
3119
0
}
3120
3121
static const char *set_server_alias(cmd_parms *cmd, void *dummy,
3122
                                    const char *arg)
3123
0
{
3124
0
    if (!cmd->server->names) {
3125
0
        return "ServerAlias only used in <VirtualHost>";
3126
0
    }
3127
3128
0
    while (*arg) {
3129
0
        char **item, *name = ap_getword_conf(cmd->pool, &arg);
3130
3131
0
        if (ap_is_matchexp(name)) {
3132
0
            item = (char **)apr_array_push(cmd->server->wild_names);
3133
0
        }
3134
0
        else {
3135
0
            item = (char **)apr_array_push(cmd->server->names);
3136
0
        }
3137
3138
0
        *item = name;
3139
0
    }
3140
3141
0
    return NULL;
3142
0
}
3143
3144
static const char *set_accf_map(cmd_parms *cmd, void *dummy,
3145
                                const char *iproto, const char* iaccf)
3146
0
{
3147
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3148
0
    core_server_config *conf =
3149
0
        ap_get_core_module_config(cmd->server->module_config);
3150
0
    char* proto;
3151
0
    char* accf;
3152
0
    if (err != NULL) {
3153
0
        return err;
3154
0
    }
3155
3156
0
    proto = apr_pstrdup(cmd->pool, iproto);
3157
0
    ap_str_tolower(proto);
3158
0
    accf = apr_pstrdup(cmd->pool, iaccf);
3159
0
    ap_str_tolower(accf);
3160
0
    apr_table_setn(conf->accf_map, proto, accf);
3161
3162
0
    return NULL;
3163
0
}
3164
3165
AP_DECLARE(const char*) ap_get_server_protocol(server_rec* s)
3166
0
{
3167
0
    core_server_config *conf = ap_get_core_module_config(s->module_config);
3168
0
    return conf->protocol;
3169
0
}
3170
3171
AP_DECLARE(void) ap_set_server_protocol(server_rec* s, const char* proto)
3172
0
{
3173
0
    core_server_config *conf = ap_get_core_module_config(s->module_config);
3174
0
    conf->protocol = proto;
3175
0
}
3176
3177
static const char *set_protocol(cmd_parms *cmd, void *dummy,
3178
                                const char *arg)
3179
0
{
3180
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
3181
0
    core_server_config *conf =
3182
0
        ap_get_core_module_config(cmd->server->module_config);
3183
0
    char* proto;
3184
3185
0
    if (err != NULL) {
3186
0
        return err;
3187
0
    }
3188
3189
0
    proto = apr_pstrdup(cmd->pool, arg);
3190
0
    ap_str_tolower(proto);
3191
0
    conf->protocol = proto;
3192
3193
0
    return NULL;
3194
0
}
3195
3196
static const char *set_server_string_slot(cmd_parms *cmd, void *dummy,
3197
                                          const char *arg)
3198
0
{
3199
    /* This one's pretty generic... */
3200
3201
0
    int offset = (int)(long)cmd->info;
3202
0
    char *struct_ptr = (char *)cmd->server;
3203
3204
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
3205
0
    if (err != NULL) {
3206
0
        return err;
3207
0
    }
3208
3209
0
    *(const char **)(struct_ptr + offset) = arg;
3210
0
    return NULL;
3211
0
}
3212
3213
/*
3214
 * The ServerName directive takes one argument with format
3215
 * [scheme://]fully-qualified-domain-name[:port], for instance
3216
 * ServerName www.example.com
3217
 * ServerName www.example.com:80
3218
 * ServerName https://www.example.com:443
3219
 */
3220
3221
static const char *server_hostname_port(cmd_parms *cmd, void *dummy, const char *arg)
3222
0
{
3223
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
3224
0
    const char *portstr, *part;
3225
0
    char *scheme;
3226
0
    int port;
3227
3228
0
    if (err != NULL) {
3229
0
        return err;
3230
0
    }
3231
3232
0
    if (apr_fnmatch_test(arg))
3233
0
        return apr_pstrcat(cmd->temp_pool, "Invalid ServerName \"", arg,
3234
0
                "\" use ServerAlias to set multiple server names.", NULL);
3235
3236
0
    part = ap_strstr_c(arg, "://");
3237
3238
0
    if (part) {
3239
0
      scheme = apr_pstrndup(cmd->pool, arg, part - arg);
3240
0
      ap_str_tolower(scheme);
3241
0
      cmd->server->server_scheme = (const char *)scheme;
3242
0
      part += 3;
3243
0
    } else {
3244
0
      part = arg;
3245
0
    }
3246
3247
0
    portstr = ap_strchr_c(part, ':');
3248
0
    if (portstr) {
3249
0
        cmd->server->server_hostname = apr_pstrndup(cmd->pool, part,
3250
0
                                                    portstr - part);
3251
0
        portstr++;
3252
0
        port = atoi(portstr);
3253
0
        if (port <= 0 || port >= 65536) { /* 65536 == 1<<16 */
3254
0
            return apr_pstrcat(cmd->temp_pool, "The port number \"", arg,
3255
0
                          "\" is outside the appropriate range "
3256
0
                          "(i.e., 1..65535).", NULL);
3257
0
        }
3258
0
    }
3259
0
    else {
3260
0
        cmd->server->server_hostname = apr_pstrdup(cmd->pool, part);
3261
0
        port = 0;
3262
0
    }
3263
3264
0
    cmd->server->port = port;
3265
0
    return NULL;
3266
0
}
3267
3268
static const char *set_signature_flag(cmd_parms *cmd, void *d_,
3269
                                      const char *arg)
3270
0
{
3271
0
    core_dir_config *d = d_;
3272
3273
0
    if (ap_cstr_casecmp(arg, "On") == 0) {
3274
0
        d->server_signature = srv_sig_on;
3275
0
    }
3276
0
    else if (ap_cstr_casecmp(arg, "Off") == 0) {
3277
0
        d->server_signature = srv_sig_off;
3278
0
    }
3279
0
    else if (ap_cstr_casecmp(arg, "EMail") == 0) {
3280
0
        d->server_signature = srv_sig_withmail;
3281
0
    }
3282
0
    else {
3283
0
        return "ServerSignature: use one of: off | on | email";
3284
0
    }
3285
3286
0
    return NULL;
3287
0
}
3288
3289
static const char *set_server_root(cmd_parms *cmd, void *dummy,
3290
                                   const char *arg)
3291
0
{
3292
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3293
3294
0
    if (err != NULL) {
3295
0
        return err;
3296
0
    }
3297
3298
0
    if ((apr_filepath_merge((char**)&ap_server_root, NULL, arg,
3299
0
                            APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS)
3300
0
        || !ap_is_directory(cmd->temp_pool, ap_server_root)) {
3301
0
        return "ServerRoot must be a valid directory";
3302
0
    }
3303
3304
0
    return NULL;
3305
0
}
3306
3307
static const char *set_runtime_dir(cmd_parms *cmd, void *dummy, const char *arg)
3308
0
{
3309
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3310
3311
0
    if (err != NULL) {
3312
0
        return err;
3313
0
    }
3314
3315
0
    if ((apr_filepath_merge((char**)&ap_runtime_dir, NULL,
3316
0
                            ap_server_root_relative(cmd->temp_pool, arg),
3317
0
                            APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS)
3318
0
        || !ap_is_directory(cmd->temp_pool, ap_runtime_dir)) {
3319
0
        return "DefaultRuntimeDir must be a valid directory, absolute or relative to ServerRoot";
3320
0
    }
3321
3322
0
    return NULL;
3323
0
}
3324
3325
static const char *set_state_dir(cmd_parms *cmd, void *dummy, const char *arg)
3326
0
{
3327
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3328
3329
0
    if (err != NULL) {
3330
0
        return err;
3331
0
    }
3332
3333
0
    if ((apr_filepath_merge((char**)&core_state_dir, NULL,
3334
0
                            ap_server_root_relative(cmd->temp_pool, arg),
3335
0
                            APR_FILEPATH_TRUENAME, cmd->pool) != APR_SUCCESS)
3336
0
        || !ap_is_directory(cmd->temp_pool, core_state_dir)) {
3337
0
        return "DefaultStateDir must be a valid directory, absolute or relative to ServerRoot";
3338
0
    }
3339
3340
0
    return NULL;
3341
0
}
3342
3343
static const char *set_timeout(cmd_parms *cmd, void *dummy, const char *arg)
3344
0
{
3345
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
3346
3347
0
    if (err != NULL) {
3348
0
        return err;
3349
0
    }
3350
3351
0
    cmd->server->timeout = apr_time_from_sec(atoi(arg));
3352
0
    return NULL;
3353
0
}
3354
3355
static const char *set_allow2f(cmd_parms *cmd, void *d_, const char *arg)
3356
0
{
3357
0
    core_dir_config *d = d_;
3358
3359
0
    if (0 == ap_cstr_casecmp(arg, "on")) {
3360
0
        d->allow_encoded_slashes = 1;
3361
0
        d->decode_encoded_slashes = 1; /* for compatibility with 2.0 & 2.2 */
3362
0
    } else if (0 == ap_cstr_casecmp(arg, "off")) {
3363
0
        d->allow_encoded_slashes = 0;
3364
0
        d->decode_encoded_slashes = 0;
3365
0
    } else if (0 == ap_cstr_casecmp(arg, "nodecode")) {
3366
0
        d->allow_encoded_slashes = 1;
3367
0
        d->decode_encoded_slashes = 0;
3368
0
    } else {
3369
0
        return apr_pstrcat(cmd->pool,
3370
0
                           cmd->cmd->name, " must be On, Off, or NoDecode",
3371
0
                           NULL);
3372
0
    }
3373
3374
0
    d->allow_encoded_slashes_set = 1;
3375
0
    d->decode_encoded_slashes_set = 1;
3376
3377
0
    return NULL;
3378
0
}
3379
3380
static const char *set_hostname_lookups(cmd_parms *cmd, void *d_,
3381
                                        const char *arg)
3382
0
{
3383
0
    core_dir_config *d = d_;
3384
3385
0
    if (!ap_cstr_casecmp(arg, "on")) {
3386
0
        d->hostname_lookups = HOSTNAME_LOOKUP_ON;
3387
0
    }
3388
0
    else if (!ap_cstr_casecmp(arg, "off")) {
3389
0
        d->hostname_lookups = HOSTNAME_LOOKUP_OFF;
3390
0
    }
3391
0
    else if (!ap_cstr_casecmp(arg, "double")) {
3392
0
        d->hostname_lookups = HOSTNAME_LOOKUP_DOUBLE;
3393
0
    }
3394
0
    else {
3395
0
        return "parameter must be 'on', 'off', or 'double'";
3396
0
    }
3397
3398
0
    return NULL;
3399
0
}
3400
3401
static const char *set_serverpath(cmd_parms *cmd, void *dummy,
3402
                                  const char *arg)
3403
0
{
3404
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
3405
3406
0
    if (err != NULL) {
3407
0
        return err;
3408
0
    }
3409
3410
0
    cmd->server->path = arg;
3411
0
    cmd->server->pathlen = (int)strlen(arg);
3412
0
    return NULL;
3413
0
}
3414
3415
static const char *set_accept_path_info(cmd_parms *cmd, void *d_, const char *arg)
3416
0
{
3417
0
    core_dir_config *d = d_;
3418
3419
0
    if (ap_cstr_casecmp(arg, "on") == 0) {
3420
0
        d->accept_path_info = AP_REQ_ACCEPT_PATH_INFO;
3421
0
    }
3422
0
    else if (ap_cstr_casecmp(arg, "off") == 0) {
3423
0
        d->accept_path_info = AP_REQ_REJECT_PATH_INFO;
3424
0
    }
3425
0
    else if (ap_cstr_casecmp(arg, "default") == 0) {
3426
0
        d->accept_path_info = AP_REQ_DEFAULT_PATH_INFO;
3427
0
    }
3428
0
    else {
3429
0
        return "AcceptPathInfo must be set to on, off or default";
3430
0
    }
3431
3432
0
    return NULL;
3433
0
}
3434
3435
static const char *set_use_canonical_name(cmd_parms *cmd, void *d_,
3436
                                          const char *arg)
3437
0
{
3438
0
    core_dir_config *d = d_;
3439
3440
0
    if (ap_cstr_casecmp(arg, "on") == 0) {
3441
0
        d->use_canonical_name = USE_CANONICAL_NAME_ON;
3442
0
    }
3443
0
    else if (ap_cstr_casecmp(arg, "off") == 0) {
3444
0
        d->use_canonical_name = USE_CANONICAL_NAME_OFF;
3445
0
    }
3446
0
    else if (ap_cstr_casecmp(arg, "dns") == 0) {
3447
0
        d->use_canonical_name = USE_CANONICAL_NAME_DNS;
3448
0
    }
3449
0
    else {
3450
0
        return "parameter must be 'on', 'off', or 'dns'";
3451
0
    }
3452
3453
0
    return NULL;
3454
0
}
3455
3456
static const char *set_use_canonical_phys_port(cmd_parms *cmd, void *d_,
3457
                                          const char *arg)
3458
0
{
3459
0
    core_dir_config *d = d_;
3460
3461
0
    if (ap_cstr_casecmp(arg, "on") == 0) {
3462
0
        d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_ON;
3463
0
    }
3464
0
    else if (ap_cstr_casecmp(arg, "off") == 0) {
3465
0
        d->use_canonical_phys_port = USE_CANONICAL_PHYS_PORT_OFF;
3466
0
    }
3467
0
    else {
3468
0
        return "parameter must be 'on' or 'off'";
3469
0
    }
3470
3471
0
    return NULL;
3472
0
}
3473
3474
static const char *include_config (cmd_parms *cmd, void *dummy,
3475
                                   const char *name)
3476
0
{
3477
0
    ap_directive_t *conftree = NULL;
3478
0
    const char *conffile, *error;
3479
0
    unsigned *recursion;
3480
0
    int optional = cmd->cmd->cmd_data ? 1 : 0;
3481
0
    void *data;
3482
3483
    /* NOTE: ap_include_sentinel is also used by ap_process_resource_config()
3484
     * during DUMP_INCLUDES; don't change its type or remove it without updating
3485
     * the other.
3486
     */
3487
0
    apr_pool_userdata_get(&data, "ap_include_sentinel", cmd->pool);
3488
0
    if (data) {
3489
0
        recursion = data;
3490
0
    }
3491
0
    else {
3492
0
        data = recursion = apr_palloc(cmd->pool, sizeof(*recursion));
3493
0
        *recursion = 0;
3494
0
        apr_pool_userdata_setn(data, "ap_include_sentinel", NULL, cmd->pool);
3495
0
    }
3496
3497
0
    if (++*recursion > AP_MAX_INCLUDE_DEPTH) {
3498
0
        *recursion = 0;
3499
0
        return apr_psprintf(cmd->pool, "Exceeded maximum include depth of %u, "
3500
0
                            "There appears to be a recursion.",
3501
0
                            AP_MAX_INCLUDE_DEPTH);
3502
0
    }
3503
3504
0
    conffile = ap_server_root_relative(cmd->pool, name);
3505
0
    if (!conffile) {
3506
0
        *recursion = 0;
3507
0
        return apr_pstrcat(cmd->pool, "Invalid Include path ",
3508
0
                           name, NULL);
3509
0
    }
3510
3511
0
    if (ap_exists_config_define("DUMP_INCLUDES")) {
3512
0
        unsigned *line_number;
3513
3514
        /* NOTE: ap_include_lineno is used by ap_process_resource_config()
3515
         * during DUMP_INCLUDES; don't change its type or remove it without
3516
         * updating the other.
3517
         */
3518
0
        apr_pool_userdata_get(&data, "ap_include_lineno", cmd->pool);
3519
0
        if (data) {
3520
0
            line_number = data;
3521
0
        } else {
3522
0
            data = line_number = apr_palloc(cmd->pool, sizeof(*line_number));
3523
0
            apr_pool_userdata_setn(data, "ap_include_lineno", NULL, cmd->pool);
3524
0
        }
3525
3526
0
        *line_number = cmd->config_file->line_number;
3527
0
    }
3528
3529
0
    error = ap_process_fnmatch_configs(cmd->server, conffile, &conftree,
3530
0
                                       cmd->pool, cmd->temp_pool,
3531
0
                                       optional);
3532
0
    if (error) {
3533
0
        *recursion = 0;
3534
0
        return error;
3535
0
    }
3536
3537
0
    *(ap_directive_t **)dummy = conftree;
3538
3539
    /* recursion level done */
3540
0
    if (*recursion) {
3541
0
        --*recursion;
3542
0
    }
3543
3544
0
    return NULL;
3545
0
}
3546
3547
static const char *update_loglevel(cmd_parms *cmd, struct ap_logconf *log,
3548
                                   const char *arg)
3549
0
{
3550
0
    const char *level_str, *err;
3551
0
    module *module;
3552
0
    int level;
3553
3554
0
    level_str = ap_strrchr_c(arg, ':');
3555
3556
0
    if (level_str == NULL) {
3557
0
        err = ap_parse_log_level(arg, &log->level);
3558
0
        if (err != NULL)
3559
0
            return err;
3560
0
        ap_reset_module_loglevels(log, APLOG_NO_MODULE);
3561
0
        ap_log_error(APLOG_MARK, APLOG_TRACE3, 0, cmd->server,
3562
0
                     "Setting %s for all modules to %s", cmd->cmd->name, arg);
3563
0
        return NULL;
3564
0
    }
3565
3566
0
    arg = apr_pstrmemdup(cmd->temp_pool, arg, level_str - arg);
3567
0
    level_str++;
3568
0
    if (!*level_str) {
3569
0
        return apr_psprintf(cmd->temp_pool, "Module specifier '%s' must be "
3570
0
                            "followed by a log level keyword", arg);
3571
0
    }
3572
3573
0
    err = ap_parse_log_level(level_str, &level);
3574
0
    if (err != NULL)
3575
0
        return apr_psprintf(cmd->temp_pool, "%s:%s: %s", arg, level_str, err);
3576
3577
0
    if ((module = find_module(cmd->server, arg)) == NULL) {
3578
0
        char *name = apr_psprintf(cmd->temp_pool, "%s_module", arg);
3579
0
        ap_log_error(APLOG_MARK, APLOG_TRACE6, 0, cmd->server,
3580
0
                     "Cannot find module '%s', trying '%s'", arg, name);
3581
0
        module = find_module(cmd->server, name);
3582
0
    }
3583
3584
0
    if (module == NULL) {
3585
0
        return apr_psprintf(cmd->temp_pool, "Cannot find module %s", arg);
3586
0
    }
3587
3588
0
    ap_set_module_loglevel(cmd->pool, log, module->module_index, level);
3589
0
    ap_log_error(APLOG_MARK, APLOG_TRACE3, 0, cmd->server,
3590
0
                 "Setting %s for module %s to %s", cmd->cmd->name,
3591
0
                 module->name, level_str);
3592
3593
0
    return NULL;
3594
0
}
3595
3596
static const char *set_loglevel(cmd_parms *cmd, void *config_, const char *arg)
3597
0
{
3598
0
    struct ap_logconf *log;
3599
3600
0
    if (cmd->path) {
3601
0
        core_dir_config *dconf = config_;
3602
0
        if (!dconf->log) {
3603
0
            dconf->log = ap_new_log_config(cmd->pool, NULL);
3604
0
        }
3605
0
        log = dconf->log;
3606
0
    }
3607
0
    else {
3608
0
        log = &cmd->server->log;
3609
0
    }
3610
3611
0
    if (arg == NULL)
3612
0
        return "LogLevel requires level keyword or module loglevel specifier";
3613
3614
0
    return update_loglevel(cmd, log, arg);
3615
0
}
3616
3617
static const char *set_loglevel_override(cmd_parms *cmd, void *d_, int argc,
3618
                                         char *const argv[])
3619
0
{
3620
0
    core_server_config *sconf;
3621
0
    conn_log_config *entry;
3622
0
    int ret, i;
3623
0
    const char *addr, *mask, *err;
3624
3625
0
    if (argc < 2)
3626
0
        return "LogLevelOverride requires at least two arguments";
3627
3628
0
    entry = apr_pcalloc(cmd->pool, sizeof(conn_log_config));
3629
0
    sconf = ap_get_core_module_config(cmd->server->module_config);
3630
0
    if (!sconf->conn_log_level)
3631
0
        sconf->conn_log_level = apr_array_make(cmd->pool, 4, sizeof(entry));
3632
0
    APR_ARRAY_PUSH(sconf->conn_log_level, conn_log_config *) = entry;
3633
3634
0
    addr = argv[0];
3635
0
    mask = ap_strchr_c(addr, '/');
3636
0
    if (mask) {
3637
0
        addr = apr_pstrmemdup(cmd->temp_pool, addr, mask - addr);
3638
0
        mask++;
3639
0
    }
3640
0
    ret = apr_ipsubnet_create(&entry->subnet, addr, mask, cmd->pool);
3641
0
    if (ret != APR_SUCCESS)
3642
0
        return "parsing of subnet/netmask failed";
3643
3644
0
    for (i = 1; i < argc; i++) {
3645
0
        if ((err = update_loglevel(cmd, &entry->log, argv[i])) != NULL)
3646
0
            return err;
3647
0
    }
3648
0
    return NULL;
3649
0
}
3650
3651
AP_DECLARE(const char *) ap_psignature(const char *prefix, request_rec *r)
3652
0
{
3653
0
    char sport[20];
3654
0
    core_dir_config *conf;
3655
3656
0
    conf = (core_dir_config *)ap_get_core_module_config(r->per_dir_config);
3657
0
    if ((conf->server_signature == srv_sig_off)
3658
0
            || (conf->server_signature == srv_sig_unset)) {
3659
0
        return "";
3660
0
    }
3661
3662
0
    apr_snprintf(sport, sizeof sport, "%u", (unsigned) ap_get_server_port(r));
3663
3664
0
    if (conf->server_signature == srv_sig_withmail) {
3665
0
        return apr_pstrcat(r->pool, prefix, "<address>",
3666
0
                           ap_get_server_banner(),
3667
0
                           " Server at <a href=\"",
3668
0
                           ap_is_url(r->server->server_admin) ? "" : "mailto:",
3669
0
                           ap_escape_html(r->pool, r->server->server_admin),
3670
0
                           "\">",
3671
0
                           ap_escape_html(r->pool, ap_get_server_name(r)),
3672
0
                           "</a> Port ", sport,
3673
0
                           "</address>\n", NULL);
3674
0
    }
3675
3676
0
    return apr_pstrcat(r->pool, prefix, "<address>", ap_get_server_banner(),
3677
0
                       " Server at ",
3678
0
                       ap_escape_html(r->pool, ap_get_server_name(r)),
3679
0
                       " Port ", sport,
3680
0
                       "</address>\n", NULL);
3681
0
}
3682
3683
/*
3684
 * Handle a request to include the server's OS platform in the Server
3685
 * response header field (the ServerTokens directive).  Unfortunately
3686
 * this requires a new global in order to communicate the setting back to
3687
 * http_main so it can insert the information in the right place in the
3688
 * string.
3689
 */
3690
3691
static char *server_banner = NULL;
3692
static int banner_locked = 0;
3693
static const char *server_description = NULL;
3694
3695
enum server_token_type {
3696
    SrvTk_MAJOR,         /* eg: Apache/2 */
3697
    SrvTk_MINOR,         /* eg. Apache/2.0 */
3698
    SrvTk_MINIMAL,       /* eg: Apache/2.0.41 */
3699
    SrvTk_OS,            /* eg: Apache/2.0.41 (UNIX) */
3700
    SrvTk_FULL,          /* eg: Apache/2.0.41 (UNIX) PHP/4.2.2 FooBar/1.2b */
3701
    SrvTk_PRODUCT_ONLY   /* eg: Apache */
3702
};
3703
static enum server_token_type ap_server_tokens = SrvTk_FULL;
3704
3705
static apr_status_t reset_banner(void *dummy)
3706
0
{
3707
0
    banner_locked = 0;
3708
0
    ap_server_tokens = SrvTk_FULL;
3709
0
    server_banner = NULL;
3710
0
    server_description = NULL;
3711
0
    return APR_SUCCESS;
3712
0
}
3713
3714
AP_DECLARE(void) ap_get_server_revision(ap_version_t *version)
3715
0
{
3716
0
    version->major = AP_SERVER_MAJORVERSION_NUMBER;
3717
0
    version->minor = AP_SERVER_MINORVERSION_NUMBER;
3718
0
    version->patch = AP_SERVER_PATCHLEVEL_NUMBER;
3719
0
    version->add_string = AP_SERVER_ADD_STRING;
3720
0
}
3721
3722
AP_DECLARE(const char *) ap_get_server_description(void)
3723
0
{
3724
0
    return server_description ? server_description :
3725
0
        AP_SERVER_BASEVERSION " (" PLATFORM ")";
3726
0
}
3727
3728
AP_DECLARE(const char *) ap_get_server_banner(void)
3729
0
{
3730
0
    return server_banner ? server_banner : AP_SERVER_BASEVERSION;
3731
0
}
3732
3733
AP_DECLARE(void) ap_add_version_component(apr_pool_t *pconf, const char *component)
3734
0
{
3735
0
    if (! banner_locked) {
3736
        /*
3737
         * If the version string is null, register our cleanup to reset the
3738
         * pointer on pool destruction. We also know that, if NULL,
3739
         * we are adding the original SERVER_BASEVERSION string.
3740
         */
3741
0
        if (server_banner == NULL) {
3742
0
            apr_pool_cleanup_register(pconf, NULL, reset_banner,
3743
0
                                      apr_pool_cleanup_null);
3744
0
            server_banner = apr_pstrdup(pconf, component);
3745
0
        }
3746
0
        else {
3747
            /*
3748
             * Tack the given component identifier to the end of
3749
             * the existing string.
3750
             */
3751
0
            server_banner = apr_pstrcat(pconf, server_banner, " ",
3752
0
                                        component, NULL);
3753
0
        }
3754
0
    }
3755
0
    server_description = apr_pstrcat(pconf, server_description, " ",
3756
0
                                     component, NULL);
3757
0
}
3758
3759
/*
3760
 * This routine adds the real server base identity to the banner string,
3761
 * and then locks out changes until the next reconfig.
3762
 */
3763
static void set_banner(apr_pool_t *pconf)
3764
0
{
3765
0
    if (ap_server_tokens == SrvTk_PRODUCT_ONLY) {
3766
0
        ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT);
3767
0
    }
3768
0
    else if (ap_server_tokens == SrvTk_MINIMAL) {
3769
0
        ap_add_version_component(pconf, AP_SERVER_BASEVERSION);
3770
0
    }
3771
0
    else if (ap_server_tokens == SrvTk_MINOR) {
3772
0
        ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MINORREVISION);
3773
0
    }
3774
0
    else if (ap_server_tokens == SrvTk_MAJOR) {
3775
0
        ap_add_version_component(pconf, AP_SERVER_BASEPRODUCT "/" AP_SERVER_MAJORVERSION);
3776
0
    }
3777
0
    else {
3778
0
        ap_add_version_component(pconf, AP_SERVER_BASEVERSION " (" PLATFORM ")");
3779
0
    }
3780
3781
    /*
3782
     * Lock the server_banner string if we're not displaying
3783
     * the full set of tokens
3784
     */
3785
0
    if (ap_server_tokens != SrvTk_FULL) {
3786
0
        banner_locked++;
3787
0
    }
3788
0
    server_description = AP_SERVER_BASEVERSION " (" PLATFORM ")";
3789
0
}
3790
3791
static const char *set_serv_tokens(cmd_parms *cmd, void *dummy,
3792
                                   const char *arg)
3793
0
{
3794
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
3795
3796
0
    if (err != NULL) {
3797
0
        return err;
3798
0
    }
3799
3800
0
    if (!ap_cstr_casecmp(arg, "OS")) {
3801
0
        ap_server_tokens = SrvTk_OS;
3802
0
    }
3803
0
    else if (!ap_cstr_casecmp(arg, "Min") || !ap_cstr_casecmp(arg, "Minimal")) {
3804
0
        ap_server_tokens = SrvTk_MINIMAL;
3805
0
    }
3806
0
    else if (!ap_cstr_casecmp(arg, "Major")) {
3807
0
        ap_server_tokens = SrvTk_MAJOR;
3808
0
    }
3809
0
    else if (!ap_cstr_casecmp(arg, "Minor") ) {
3810
0
        ap_server_tokens = SrvTk_MINOR;
3811
0
    }
3812
0
    else if (!ap_cstr_casecmp(arg, "Prod") || !ap_cstr_casecmp(arg, "ProductOnly")) {
3813
0
        ap_server_tokens = SrvTk_PRODUCT_ONLY;
3814
0
    }
3815
0
    else if (!ap_cstr_casecmp(arg, "Full")) {
3816
0
        ap_server_tokens = SrvTk_FULL;
3817
0
    }
3818
0
    else {
3819
0
        return "ServerTokens takes 1 argument: 'Prod(uctOnly)', 'Major', 'Minor', 'Min(imal)', 'OS', or 'Full'";
3820
0
    }
3821
3822
0
    return NULL;
3823
0
}
3824
3825
static const char *set_limit_req_line(cmd_parms *cmd, void *dummy,
3826
                                      const char *arg)
3827
0
{
3828
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
3829
0
    int lim;
3830
3831
0
    if (err != NULL) {
3832
0
        return err;
3833
0
    }
3834
3835
0
    lim = atoi(arg);
3836
0
    if (lim < 0) {
3837
0
        return apr_pstrcat(cmd->temp_pool, "LimitRequestLine \"", arg,
3838
0
                           "\" must be a non-negative integer", NULL);
3839
0
    }
3840
3841
0
    cmd->server->limit_req_line = lim;
3842
0
    return NULL;
3843
0
}
3844
3845
static const char *set_limit_req_fieldsize(cmd_parms *cmd, void *dummy,
3846
                                           const char *arg)
3847
0
{
3848
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
3849
0
    int lim;
3850
3851
0
    if (err != NULL) {
3852
0
        return err;
3853
0
    }
3854
3855
0
    lim = atoi(arg);
3856
0
    if (lim < 0) {
3857
0
        return apr_pstrcat(cmd->temp_pool, "LimitRequestFieldsize \"", arg,
3858
0
                          "\" must be a non-negative integer",
3859
0
                          NULL);
3860
0
    }
3861
3862
0
    cmd->server->limit_req_fieldsize = lim;
3863
0
    return NULL;
3864
0
}
3865
3866
static const char *set_limit_req_fields(cmd_parms *cmd, void *dummy,
3867
                                        const char *arg)
3868
0
{
3869
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
3870
0
    int lim;
3871
3872
0
    if (err != NULL) {
3873
0
        return err;
3874
0
    }
3875
3876
0
    lim = atoi(arg);
3877
0
    if (lim < 0) {
3878
0
        return apr_pstrcat(cmd->temp_pool, "LimitRequestFields \"", arg,
3879
0
                           "\" must be a non-negative integer (0 = no limit)",
3880
0
                           NULL);
3881
0
    }
3882
3883
0
    cmd->server->limit_req_fields = lim;
3884
0
    return NULL;
3885
0
}
3886
3887
static const char *set_limit_req_body(cmd_parms *cmd, void *conf_,
3888
                                      const char *arg)
3889
0
{
3890
0
    core_dir_config *conf = conf_;
3891
0
    char *errp;
3892
3893
0
    if (APR_SUCCESS != apr_strtoff(&conf->limit_req_body, arg, &errp, 10)) {
3894
0
        return "LimitRequestBody argument is not parsable.";
3895
0
    }
3896
0
    if (*errp || conf->limit_req_body < 0) {
3897
0
        return "LimitRequestBody requires a non-negative integer.";
3898
0
    }
3899
3900
0
    return NULL;
3901
0
}
3902
3903
static const char *set_limit_xml_req_body(cmd_parms *cmd, void *conf_,
3904
                                          const char *arg)
3905
0
{
3906
0
    core_dir_config *conf = conf_;
3907
3908
0
    conf->limit_xml_body = atol(arg);
3909
0
    if (conf->limit_xml_body < 0)
3910
0
        return "LimitXMLRequestBody requires a non-negative integer.";
3911
3912
    /* zero is AP_MAX_LIMIT_XML_BODY (implicitly) */
3913
0
    if ((apr_size_t)conf->limit_xml_body > AP_MAX_LIMIT_XML_BODY)
3914
0
        return apr_psprintf(cmd->pool, "LimitXMLRequestBody must not exceed "
3915
0
                            "%" APR_SIZE_T_FMT, AP_MAX_LIMIT_XML_BODY);
3916
3917
0
    return NULL;
3918
0
}
3919
3920
static const char *set_max_ranges(cmd_parms *cmd, void *conf_, const char *arg)
3921
0
{
3922
0
    core_dir_config *conf = conf_;
3923
0
    int val = 0;
3924
3925
0
    if (!ap_cstr_casecmp(arg, "none")) {
3926
0
        val = AP_MAXRANGES_NORANGES;
3927
0
    }
3928
0
    else if (!ap_cstr_casecmp(arg, "default")) {
3929
0
        val = AP_MAXRANGES_DEFAULT;
3930
0
    }
3931
0
    else if (!ap_cstr_casecmp(arg, "unlimited")) {
3932
0
        val = AP_MAXRANGES_UNLIMITED;
3933
0
    }
3934
0
    else {
3935
0
        val = atoi(arg);
3936
0
        if (val <= 0)
3937
0
            return "MaxRanges requires 'none', 'default', 'unlimited' or "
3938
0
                   "a positive integer";
3939
0
    }
3940
3941
0
    conf->max_ranges = val;
3942
3943
0
    return NULL;
3944
0
}
3945
3946
static const char *set_max_overlaps(cmd_parms *cmd, void *conf_, const char *arg)
3947
0
{
3948
0
    core_dir_config *conf = conf_;
3949
0
    int val = 0;
3950
3951
0
    if (!ap_cstr_casecmp(arg, "none")) {
3952
0
        val = AP_MAXRANGES_NORANGES;
3953
0
    }
3954
0
    else if (!ap_cstr_casecmp(arg, "default")) {
3955
0
        val = AP_MAXRANGES_DEFAULT;
3956
0
    }
3957
0
    else if (!ap_cstr_casecmp(arg, "unlimited")) {
3958
0
        val = AP_MAXRANGES_UNLIMITED;
3959
0
    }
3960
0
    else {
3961
0
        val = atoi(arg);
3962
0
        if (val <= 0)
3963
0
            return "MaxRangeOverlaps requires 'none', 'default', 'unlimited' or "
3964
0
            "a positive integer";
3965
0
    }
3966
3967
0
    conf->max_overlaps = val;
3968
3969
0
    return NULL;
3970
0
}
3971
3972
static const char *set_max_reversals(cmd_parms *cmd, void *conf_, const char *arg)
3973
0
{
3974
0
    core_dir_config *conf = conf_;
3975
0
    int val = 0;
3976
3977
0
    if (!ap_cstr_casecmp(arg, "none")) {
3978
0
        val = AP_MAXRANGES_NORANGES;
3979
0
    }
3980
0
    else if (!ap_cstr_casecmp(arg, "default")) {
3981
0
        val = AP_MAXRANGES_DEFAULT;
3982
0
    }
3983
0
    else if (!ap_cstr_casecmp(arg, "unlimited")) {
3984
0
        val = AP_MAXRANGES_UNLIMITED;
3985
0
    }
3986
0
    else {
3987
0
        val = atoi(arg);
3988
0
        if (val <= 0)
3989
0
            return "MaxRangeReversals requires 'none', 'default', 'unlimited' or "
3990
0
            "a positive integer";
3991
0
    }
3992
3993
0
    conf->max_reversals = val;
3994
3995
0
    return NULL;
3996
0
}
3997
3998
AP_DECLARE(apr_size_t) ap_get_limit_xml_body(const request_rec *r)
3999
0
{
4000
0
    core_dir_config *conf;
4001
4002
0
    conf = ap_get_core_module_config(r->per_dir_config);
4003
0
    if (conf->limit_xml_body == AP_LIMIT_UNSET)
4004
0
        return AP_DEFAULT_LIMIT_XML_BODY;
4005
0
    if (conf->limit_xml_body == 0)
4006
0
        return AP_MAX_LIMIT_XML_BODY;
4007
4008
0
    return (apr_size_t)conf->limit_xml_body;
4009
0
}
4010
4011
#if !defined (RLIMIT_CPU) || !(defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)) || !defined (RLIMIT_NPROC)
4012
static const char *no_set_limit(cmd_parms *cmd, void *conf_,
4013
                                const char *arg, const char *arg2)
4014
{
4015
    ap_log_error(APLOG_MARK, APLOG_ERR, 0, cmd->server, APLOGNO(00118)
4016
                "%s not supported on this platform", cmd->cmd->name);
4017
4018
    return NULL;
4019
}
4020
#endif
4021
4022
#ifdef RLIMIT_CPU
4023
static const char *set_limit_cpu(cmd_parms *cmd, void *conf_,
4024
                                 const char *arg, const char *arg2)
4025
0
{
4026
0
    core_dir_config *conf = conf_;
4027
4028
0
    ap_unixd_set_rlimit(cmd, &conf->limit_cpu, arg, arg2, RLIMIT_CPU);
4029
0
    return NULL;
4030
0
}
4031
#endif
4032
4033
#if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
4034
static const char *set_limit_mem(cmd_parms *cmd, void *conf_,
4035
                                 const char *arg, const char * arg2)
4036
0
{
4037
0
    core_dir_config *conf = conf_;
4038
4039
0
#if defined(RLIMIT_AS)
4040
0
    ap_unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2 ,RLIMIT_AS);
4041
#elif defined(RLIMIT_DATA)
4042
    ap_unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_DATA);
4043
#elif defined(RLIMIT_VMEM)
4044
    ap_unixd_set_rlimit(cmd, &conf->limit_mem, arg, arg2, RLIMIT_VMEM);
4045
#endif
4046
4047
0
    return NULL;
4048
0
}
4049
#endif
4050
4051
#ifdef RLIMIT_NPROC
4052
static const char *set_limit_nproc(cmd_parms *cmd, void *conf_,
4053
                                   const char *arg, const char * arg2)
4054
0
{
4055
0
    core_dir_config *conf = conf_;
4056
4057
0
    ap_unixd_set_rlimit(cmd, &conf->limit_nproc, arg, arg2, RLIMIT_NPROC);
4058
0
    return NULL;
4059
0
}
4060
#endif
4061
4062
static const char *set_recursion_limit(cmd_parms *cmd, void *dummy,
4063
                                       const char *arg1, const char *arg2)
4064
0
{
4065
0
    core_server_config *conf =
4066
0
        ap_get_core_module_config(cmd->server->module_config);
4067
0
    int limit = atoi(arg1);
4068
4069
0
    if (limit <= 0) {
4070
0
        return "The recursion limit must be greater than zero.";
4071
0
    }
4072
0
    if (limit < 4) {
4073
0
        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(00119)
4074
0
                     "Limiting internal redirects to very low numbers may "
4075
0
                     "cause normal requests to fail.");
4076
0
    }
4077
4078
0
    conf->redirect_limit = limit;
4079
4080
0
    if (arg2) {
4081
0
        limit = atoi(arg2);
4082
4083
0
        if (limit <= 0) {
4084
0
            return "The recursion limit must be greater than zero.";
4085
0
        }
4086
0
        if (limit < 4) {
4087
0
            ap_log_error(APLOG_MARK, APLOG_WARNING, 0, cmd->server, APLOGNO(00120)
4088
0
                         "Limiting the subrequest depth to a very low level may"
4089
0
                         " cause normal requests to fail.");
4090
0
        }
4091
0
    }
4092
4093
0
    conf->subreq_limit = limit;
4094
4095
0
    return NULL;
4096
0
}
4097
4098
static void log_backtrace(const request_rec *r)
4099
0
{
4100
0
    if (APLOGrdebug(r)) {
4101
0
        const request_rec *top = r;
4102
4103
0
        ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00121)
4104
0
                      "r->uri = %s", r->uri ? r->uri : "(unexpectedly NULL)");
4105
4106
0
        while (top && (top->prev || top->main)) {
4107
0
            if (top->prev) {
4108
0
                top = top->prev;
4109
0
                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00122)
4110
0
                              "redirected from r->uri = %s",
4111
0
                              top->uri ? top->uri : "(unexpectedly NULL)");
4112
0
            }
4113
4114
0
            if (!top->prev && top->main) {
4115
0
                top = top->main;
4116
0
                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(00123)
4117
0
                              "subrequested from r->uri = %s",
4118
0
                              top->uri ? top->uri : "(unexpectedly NULL)");
4119
0
            }
4120
0
        }
4121
0
    }
4122
0
}
4123
4124
/*
4125
 * check whether redirect limit is reached
4126
 */
4127
AP_DECLARE(int) ap_is_recursion_limit_exceeded(const request_rec *r)
4128
0
{
4129
0
    core_server_config *conf =
4130
0
        ap_get_core_module_config(r->server->module_config);
4131
0
    const request_rec *top = r;
4132
0
    int redirects = 0, subreqs = 0;
4133
0
    int rlimit = conf->redirect_limit
4134
0
                 ? conf->redirect_limit
4135
0
                 : AP_DEFAULT_MAX_INTERNAL_REDIRECTS;
4136
0
    int slimit = conf->subreq_limit
4137
0
                 ? conf->subreq_limit
4138
0
                 : AP_DEFAULT_MAX_SUBREQ_DEPTH;
4139
4140
4141
0
    while (top->prev || top->main) {
4142
0
        if (top->prev) {
4143
0
            if (++redirects >= rlimit) {
4144
                /* uuh, too much. */
4145
                /* As we check before a new internal redirect, top->prev->uri
4146
                 * should be the original request causing this.
4147
                 */
4148
0
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00124)
4149
0
                              "Request (%s) exceeded the limit of %d internal "
4150
0
                              "redirects due to probable configuration error. "
4151
0
                              "Use 'LimitInternalRecursion' to increase the "
4152
0
                              "limit if necessary. Use 'LogLevel debug' to get "
4153
0
                              "a backtrace.", top->prev->uri, rlimit);
4154
4155
                /* post backtrace */
4156
0
                log_backtrace(r);
4157
4158
                /* return failure */
4159
0
                return 1;
4160
0
            }
4161
4162
0
            top = top->prev;
4163
0
        }
4164
4165
0
        if (!top->prev && top->main) {
4166
0
            if (++subreqs >= slimit) {
4167
                /* uuh, too much. */
4168
                /* As we check before a new subrequest, top->main->uri should
4169
                 * be the original request causing this.
4170
                 */
4171
0
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00125)
4172
0
                              "Request (%s) exceeded the limit of %d subrequest "
4173
0
                              "nesting levels due to probable configuration "
4174
0
                              "error. Use 'LimitInternalRecursion' to increase "
4175
0
                              "the limit if necessary. Use 'LogLevel debug' to "
4176
0
                              "get a backtrace.", top->main->uri, slimit);
4177
4178
                /* post backtrace */
4179
0
                log_backtrace(r);
4180
4181
                /* return failure */
4182
0
                return 1;
4183
0
            }
4184
4185
0
            top = top->main;
4186
0
        }
4187
0
    }
4188
4189
    /* recursion state: ok */
4190
0
    return 0;
4191
0
}
4192
4193
static const char *set_trace_enable(cmd_parms *cmd, void *dummy,
4194
                                    const char *arg1)
4195
0
{
4196
0
    core_server_config *conf =
4197
0
        ap_get_core_module_config(cmd->server->module_config);
4198
4199
0
    if (ap_cstr_casecmp(arg1, "on") == 0) {
4200
0
        conf->trace_enable = AP_TRACE_ENABLE;
4201
0
    }
4202
0
    else if (ap_cstr_casecmp(arg1, "off") == 0) {
4203
0
        conf->trace_enable = AP_TRACE_DISABLE;
4204
0
    }
4205
0
    else if (ap_cstr_casecmp(arg1, "extended") == 0) {
4206
0
        conf->trace_enable = AP_TRACE_EXTENDED;
4207
0
    }
4208
0
    else {
4209
0
        return "TraceEnable must be one of 'on', 'off', or 'extended'";
4210
0
    }
4211
4212
0
    return NULL;
4213
0
}
4214
4215
static const char *set_protocols(cmd_parms *cmd, void *dummy,
4216
                                 const char *arg)
4217
0
{
4218
0
    core_server_config *conf =
4219
0
    ap_get_core_module_config(cmd->server->module_config);
4220
0
    const char **np;
4221
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
4222
4223
0
    if (err) {
4224
0
        return err;
4225
0
    }
4226
    
4227
0
    np = (const char **)apr_array_push(conf->protocols);
4228
0
    *np = arg;
4229
4230
0
    return NULL;
4231
0
}
4232
4233
static const char *set_protocols_honor_order(cmd_parms *cmd, void *dummy,
4234
                                             const char *arg)
4235
0
{
4236
0
    core_server_config *conf =
4237
0
    ap_get_core_module_config(cmd->server->module_config);
4238
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
4239
    
4240
0
    if (err) {
4241
0
        return err;
4242
0
    }
4243
    
4244
0
    if (ap_cstr_casecmp(arg, "on") == 0) {
4245
0
        conf->protocols_honor_order = 1;
4246
0
    }
4247
0
    else if (ap_cstr_casecmp(arg, "off") == 0) {
4248
0
        conf->protocols_honor_order = 0;
4249
0
    }
4250
0
    else {
4251
0
        return "ProtocolsHonorOrder must be 'on' or 'off'";
4252
0
    }
4253
    
4254
0
    return NULL;
4255
0
}
4256
4257
static const char *set_http_protocol_options(cmd_parms *cmd, void *dummy,
4258
                                             const char *arg)
4259
0
{
4260
0
    core_server_config *conf =
4261
0
        ap_get_core_module_config(cmd->server->module_config);
4262
4263
0
    if (strcasecmp(arg, "allow0.9") == 0)
4264
0
        conf->http09_enable |= AP_HTTP09_ENABLE;
4265
0
    else if (strcasecmp(arg, "require1.0") == 0)
4266
0
        conf->http09_enable |= AP_HTTP09_DISABLE;
4267
0
    else if (strcasecmp(arg, "strict") == 0)
4268
0
        conf->http_conformance |= AP_HTTP_CONFORMANCE_STRICT;
4269
0
    else if (strcasecmp(arg, "unsafe") == 0)
4270
0
        conf->http_conformance |= AP_HTTP_CONFORMANCE_UNSAFE;
4271
0
    else if (strcasecmp(arg, "registeredmethods") == 0)
4272
0
        conf->http_methods |= AP_HTTP_METHODS_REGISTERED;
4273
0
    else if (strcasecmp(arg, "lenientmethods") == 0)
4274
0
        conf->http_methods |= AP_HTTP_METHODS_LENIENT;
4275
0
    else
4276
0
        return "HttpProtocolOptions accepts "
4277
0
               "'Unsafe' or 'Strict' (default), "
4278
0
               "'RegisteredMethods' or 'LenientMethods' (default), and "
4279
0
               "'Require1.0' or 'Allow0.9' (default)";
4280
4281
0
    if ((conf->http09_enable & AP_HTTP09_ENABLE)
4282
0
            && (conf->http09_enable & AP_HTTP09_DISABLE))
4283
0
        return "HttpProtocolOptions 'Allow0.9' and 'Require1.0'"
4284
0
               " are mutually exclusive";
4285
4286
0
    if ((conf->http_conformance & AP_HTTP_CONFORMANCE_STRICT)
4287
0
            && (conf->http_conformance & AP_HTTP_CONFORMANCE_UNSAFE))
4288
0
        return "HttpProtocolOptions 'Strict' and 'Unsafe'"
4289
0
               " are mutually exclusive";
4290
4291
0
    if ((conf->http_methods & AP_HTTP_METHODS_REGISTERED)
4292
0
            && (conf->http_methods & AP_HTTP_METHODS_LENIENT))
4293
0
        return "HttpProtocolOptions 'RegisteredMethods' and 'LenientMethods'"
4294
0
               " are mutually exclusive";
4295
4296
0
    return NULL;
4297
0
}
4298
4299
static const char *set_async_filter(cmd_parms *cmd, void *dummy,
4300
                                             const char *arg)
4301
0
{
4302
0
    core_server_config *conf =
4303
0
    ap_get_core_module_config(cmd->server->module_config);
4304
0
    const char *err = ap_check_cmd_context(cmd, NOT_IN_DIR_CONTEXT);
4305
4306
0
    if (err) {
4307
0
        return err;
4308
0
    }
4309
4310
0
    if (ap_cstr_casecmp(arg, "network") == 0) {
4311
0
        conf->async_filter = AP_FTYPE_NETWORK;
4312
0
    }
4313
0
    else if (ap_cstr_casecmp(arg, "connection") == 0) {
4314
0
        conf->async_filter = AP_FTYPE_CONNECTION;
4315
0
    }
4316
0
    else if (ap_cstr_casecmp(arg, "request") == 0) {
4317
0
        conf->async_filter = 0;
4318
0
    }
4319
0
    else {
4320
0
        return "AsyncFilter must be 'network', 'connection' or 'request'";
4321
0
    }
4322
0
    conf->async_filter_set = 1;
4323
4324
0
    return NULL;
4325
0
}
4326
4327
static const char *set_http_method(cmd_parms *cmd, void *conf, const char *arg)
4328
0
{
4329
0
    const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
4330
0
    if (err != NULL)
4331
0
        return err;
4332
0
    ap_method_register(cmd->pool, arg);
4333
0
    return NULL;
4334
0
}
4335
4336
static const char *set_cl_head_zero(cmd_parms *cmd, void *dummy, int arg)
4337
0
{
4338
0
    core_server_config *conf =
4339
0
        ap_get_core_module_config(cmd->server->module_config);
4340
4341
0
    if (arg) {
4342
0
        conf->http_cl_head_zero = AP_HTTP_CL_HEAD_ZERO_ENABLE;
4343
0
    } else {
4344
0
        conf->http_cl_head_zero = AP_HTTP_CL_HEAD_ZERO_DISABLE;
4345
0
    }
4346
0
    return NULL;
4347
0
}
4348
4349
static const char *set_expect_strict(cmd_parms *cmd, void *dummy, int arg)
4350
0
{
4351
0
    core_server_config *conf =
4352
0
        ap_get_core_module_config(cmd->server->module_config);
4353
4354
0
    if (arg) {
4355
0
        conf->http_expect_strict = AP_HTTP_EXPECT_STRICT_ENABLE;
4356
0
    } else {
4357
0
        conf->http_expect_strict = AP_HTTP_EXPECT_STRICT_DISABLE;
4358
0
    }
4359
0
    return NULL;
4360
0
}
4361
4362
static apr_hash_t *errorlog_hash;
4363
4364
static int log_constant_item(const ap_errorlog_info *info, const char *arg,
4365
                             char *buf, int buflen)
4366
0
{
4367
0
    char *end = apr_cpystrn(buf, arg, buflen);
4368
0
    return end - buf;
4369
0
}
4370
4371
static char *parse_errorlog_misc_string(apr_pool_t *p,
4372
                                        ap_errorlog_format_item *it,
4373
                                        const char **sa)
4374
0
{
4375
0
    const char *s;
4376
0
    char scratch[MAX_STRING_LEN];
4377
0
    char *d = scratch;
4378
    /*
4379
     * non-leading white space terminates this string to allow the next field
4380
     * separator to be inserted
4381
     */
4382
0
    int at_start = 1;
4383
4384
0
    it->func = log_constant_item;
4385
0
    s = *sa;
4386
4387
0
    while (*s && *s != '%' && (*s != ' ' || at_start) && d < scratch + MAX_STRING_LEN) {
4388
0
        if (*s != '\\') {
4389
0
            if (*s != ' ') {
4390
0
                at_start = 0;
4391
0
            }
4392
0
            *d++ = *s++;
4393
0
        }
4394
0
        else {
4395
0
            s++;
4396
0
            switch (*s) {
4397
0
            case 'r':
4398
0
                *d++ = '\r';
4399
0
                s++;
4400
0
                break;
4401
0
            case 'n':
4402
0
                *d++ = '\n';
4403
0
                s++;
4404
0
                break;
4405
0
            case 't':
4406
0
                *d++ = '\t';
4407
0
                s++;
4408
0
                break;
4409
0
            case '\0':
4410
                /* handle end of string */
4411
0
                *d++ = '\\';
4412
0
                break;
4413
0
            default:
4414
                /* copy next char verbatim */
4415
0
                *d++ = *s++;
4416
0
                break;
4417
0
            }
4418
0
        }
4419
0
    }
4420
0
    *d = '\0';
4421
0
    it->arg = apr_pstrdup(p, scratch);
4422
4423
0
    *sa = s;
4424
0
    return NULL;
4425
0
}
4426
4427
static char *parse_errorlog_item(apr_pool_t *p, ap_errorlog_format_item *it,
4428
                                 const char **sa)
4429
0
{
4430
0
    const char *s = *sa;
4431
0
    ap_errorlog_handler *handler;
4432
0
    int i;
4433
4434
0
    if (*s != '%') {
4435
0
        if (*s == ' ') {
4436
0
            it->flags |= AP_ERRORLOG_FLAG_FIELD_SEP;
4437
0
        }
4438
0
        return parse_errorlog_misc_string(p, it, sa);
4439
0
    }
4440
4441
0
    ++s;
4442
4443
0
    if (*s == ' ') {
4444
        /* percent-space (% ) is a field separator */
4445
0
        it->flags |= AP_ERRORLOG_FLAG_FIELD_SEP;
4446
0
        *sa = ++s;
4447
        /* recurse */
4448
0
        return parse_errorlog_item(p, it, sa);
4449
0
    }
4450
0
    else if (*s == '%') {
4451
0
        it->arg = "%";
4452
0
        it->func = log_constant_item;
4453
0
        *sa = ++s;
4454
0
        return NULL;
4455
0
    }
4456
4457
0
    while (*s) {
4458
0
        switch (*s) {
4459
0
        case '{':
4460
0
            ++s;
4461
0
            it->arg = ap_getword(p, &s, '}');
4462
0
            break;
4463
0
        case '+':
4464
0
            ++s;
4465
0
            it->flags |= AP_ERRORLOG_FLAG_REQUIRED;
4466
0
            break;
4467
0
        case '-':
4468
0
            ++s;
4469
0
            it->flags |= AP_ERRORLOG_FLAG_NULL_AS_HYPHEN;
4470
0
            break;
4471
0
        case '0':
4472
0
        case '1':
4473
0
        case '2':
4474
0
        case '3':
4475
0
        case '4':
4476
0
        case '5':
4477
0
        case '6':
4478
0
        case '7':
4479
0
        case '8':
4480
0
        case '9':
4481
0
            i = *s - '0';
4482
0
            while (apr_isdigit(*++s))
4483
0
                i = i * 10 + (*s) - '0';
4484
0
            it->min_loglevel = i;
4485
0
            break;
4486
0
        case 'M':
4487
0
            it->func = NULL;
4488
0
            it->flags |= AP_ERRORLOG_FLAG_MESSAGE;
4489
0
            *sa = ++s;
4490
0
            return NULL;
4491
0
        default:
4492
0
            handler = (ap_errorlog_handler *)apr_hash_get(errorlog_hash, s, 1);
4493
0
            if (!handler) {
4494
0
                char dummy[2];
4495
4496
0
                dummy[0] = *s;
4497
0
                dummy[1] = '\0';
4498
0
                return apr_pstrcat(p, "Unrecognized error log format directive %",
4499
0
                               dummy, NULL);
4500
0
            }
4501
0
            it->func = handler->func;
4502
0
            *sa = ++s;
4503
0
            return NULL;
4504
0
        }
4505
0
    }
4506
4507
0
    return "Ran off end of error log format parsing args to some directive";
4508
0
}
4509
4510
static apr_array_header_t *parse_errorlog_string(apr_pool_t *p,
4511
                                                 const char *s,
4512
                                                 const char **err,
4513
                                                 int is_main_fmt)
4514
0
{
4515
0
    apr_array_header_t *a = apr_array_make(p, 30,
4516
0
                                           sizeof(ap_errorlog_format_item));
4517
0
    char *res;
4518
0
    int seen_msg_fmt = 0;
4519
4520
0
    while (s && *s) {
4521
0
        ap_errorlog_format_item *item =
4522
0
            (ap_errorlog_format_item *)apr_array_push(a);
4523
0
        memset(item, 0, sizeof(*item));
4524
0
        res = parse_errorlog_item(p, item, &s);
4525
0
        if (res) {
4526
0
            *err = res;
4527
0
            return NULL;
4528
0
        }
4529
0
        if (item->flags & AP_ERRORLOG_FLAG_MESSAGE) {
4530
0
            if (!is_main_fmt) {
4531
0
                *err = "%M cannot be used in once-per-request or "
4532
0
                       "once-per-connection formats";
4533
0
                return NULL;
4534
0
            }
4535
0
            seen_msg_fmt = 1;
4536
0
        }
4537
0
        if (is_main_fmt && item->flags & AP_ERRORLOG_FLAG_REQUIRED) {
4538
0
            *err = "The '+' flag cannot be used in the main error log format";
4539
0
            return NULL;
4540
0
        }
4541
0
        if (!is_main_fmt && item->min_loglevel) {
4542
0
            *err = "The loglevel cannot be used as a condition in "
4543
0
                   "once-per-request or once-per-connection formats";
4544
0
            return NULL;
4545
0
        }
4546
0
        if (item->min_loglevel > APLOG_TRACE8) {
4547
0
            *err = "The specified loglevel modifier is out of range";
4548
0
            return NULL;
4549
0
        }
4550
0
    }
4551
4552
0
    if (is_main_fmt && !seen_msg_fmt) {
4553
0
        *err = "main ErrorLogFormat must contain message format string '%M'";
4554
0
        return NULL;
4555
0
    }
4556
4557
0
    return a;
4558
0
}
4559
4560
static const char *set_errorlog(cmd_parms *cmd, void *dummy, const char *arg1,
4561
                                const char *arg2)
4562
0
{
4563
0
    ap_errorlog_provider *provider;
4564
0
    const char *err;
4565
0
    cmd->server->errorlog_provider = NULL;
4566
4567
0
    if (!arg2) {
4568
        /* Stay backward compatible and check for "syslog" */
4569
0
        if (strncmp("syslog", arg1, 6) == 0) {
4570
0
            arg2 = arg1 + 7; /* skip the ':' if any */
4571
0
            arg1 = "syslog";
4572
0
        }
4573
0
        else {
4574
            /* Admin can define only "ErrorLog provider" and we should 
4575
             * still handle that using the defined provider, but with empty
4576
             * error_fname. */
4577
0
            provider = ap_lookup_provider(AP_ERRORLOG_PROVIDER_GROUP, arg1,
4578
0
                                          AP_ERRORLOG_PROVIDER_VERSION);
4579
0
            if (provider) {
4580
0
                arg2 = "";
4581
0
            }
4582
0
            else {
4583
0
                return set_server_string_slot(cmd, dummy, arg1);
4584
0
            }
4585
0
        }
4586
0
    }
4587
4588
0
    if (strcmp("file", arg1) == 0) {
4589
0
        return set_server_string_slot(cmd, dummy, arg2);
4590
0
    }
4591
4592
0
    provider = ap_lookup_provider(AP_ERRORLOG_PROVIDER_GROUP, arg1,
4593
0
                                    AP_ERRORLOG_PROVIDER_VERSION);
4594
0
    if (!provider) {
4595
0
        return apr_psprintf(cmd->pool,
4596
0
                            "Unknown ErrorLog provider: %s",
4597
0
                            arg1);
4598
0
    }
4599
4600
0
    err = provider->parse_errorlog_arg(cmd, arg2);
4601
0
    if (err) {
4602
0
        return err;
4603
0
    }
4604
4605
0
    cmd->server->errorlog_provider = provider;
4606
0
    return set_server_string_slot(cmd, dummy, arg2);
4607
0
}
4608
4609
static const char *set_errorlog_format(cmd_parms *cmd, void *dummy,
4610
                                       const char *arg1, const char *arg2)
4611
0
{
4612
0
    const char *err_string = NULL;
4613
0
    core_server_config *conf =
4614
0
        ap_get_core_module_config(cmd->server->module_config);
4615
4616
0
    if (!arg2) {
4617
0
        conf->error_log_format = parse_errorlog_string(cmd->pool, arg1,
4618
0
                                                       &err_string, 1);
4619
0
    }
4620
0
    else if (!ap_cstr_casecmp(arg1, "connection")) {
4621
0
        if (!conf->error_log_conn) {
4622
0
            conf->error_log_conn = apr_array_make(cmd->pool, 5,
4623
0
                                                  sizeof(apr_array_header_t *));
4624
0
        }
4625
4626
0
        if (*arg2) {
4627
0
            apr_array_header_t **e;
4628
0
            e = (apr_array_header_t **) apr_array_push(conf->error_log_conn);
4629
0
            *e = parse_errorlog_string(cmd->pool, arg2, &err_string, 0);
4630
0
        }
4631
0
    }
4632
0
    else if (!ap_cstr_casecmp(arg1, "request")) {
4633
0
        if (!conf->error_log_req) {
4634
0
            conf->error_log_req = apr_array_make(cmd->pool, 5,
4635
0
                                                 sizeof(apr_array_header_t *));
4636
0
        }
4637
4638
0
        if (*arg2) {
4639
0
            apr_array_header_t **e;
4640
0
            e = (apr_array_header_t **) apr_array_push(conf->error_log_req);
4641
0
            *e = parse_errorlog_string(cmd->pool, arg2, &err_string, 0);
4642
0
        }
4643
0
    }
4644
0
    else {
4645
0
        err_string = "ErrorLogFormat type must be one of request, connection";
4646
0
    }
4647
4648
0
    return err_string;
4649
0
}
4650
4651
AP_DECLARE(void) ap_register_errorlog_handler(apr_pool_t *p, char *tag,
4652
                                              ap_errorlog_handler_fn_t *handler,
4653
                                              int flags)
4654
0
{
4655
0
    ap_errorlog_handler *log_struct = apr_palloc(p, sizeof(*log_struct));
4656
0
    log_struct->func = handler;
4657
0
    log_struct->flags = flags;
4658
4659
0
    apr_hash_set(errorlog_hash, tag, 1, (const void *)log_struct);
4660
0
}
4661
4662
4663
static const char *set_merge_trailers(cmd_parms *cmd, void *dummy, int arg)
4664
0
{
4665
0
    core_server_config *conf = ap_get_module_config(cmd->server->module_config,
4666
0
                                                    &core_module);
4667
0
    conf->merge_trailers = (arg ? AP_MERGE_TRAILERS_ENABLE :
4668
0
            AP_MERGE_TRAILERS_DISABLE);
4669
4670
0
    return NULL;
4671
0
}
4672
4673
/* Note --- ErrorDocument will now work from .htaccess files.
4674
 * The AllowOverride of Fileinfo allows webmasters to turn it off
4675
 */
4676
4677
static const command_rec core_cmds[] = {
4678
4679
/* Old access config file commands */
4680
4681
AP_INIT_RAW_ARGS("<Directory", dirsection, NULL, RSRC_CONF,
4682
  "Container for directives affecting resources located in the specified "
4683
  "directories"),
4684
AP_INIT_RAW_ARGS("<Location", urlsection, NULL, RSRC_CONF,
4685
  "Container for directives affecting resources accessed through the "
4686
  "specified URL paths"),
4687
AP_INIT_RAW_ARGS("<VirtualHost", virtualhost_section, NULL, RSRC_CONF,
4688
  "Container to map directives to a particular virtual host, takes one or "
4689
  "more host addresses"),
4690
AP_INIT_RAW_ARGS("<Files", filesection, NULL, OR_ALL,
4691
  "Container for directives affecting files matching specified patterns"),
4692
AP_INIT_RAW_ARGS("<Limit", ap_limit_section, NULL, OR_LIMIT | OR_AUTHCFG,
4693
  "Container for authentication directives when accessed using specified HTTP "
4694
  "methods"),
4695
AP_INIT_RAW_ARGS("<LimitExcept", ap_limit_section, (void*)1,
4696
                 OR_LIMIT | OR_AUTHCFG,
4697
  "Container for authentication directives to be applied when any HTTP "
4698
  "method other than those specified is used to access the resource"),
4699
AP_INIT_RAW_ARGS("<IfModule", start_cond_section, (void *)test_ifmod_section,
4700
              EXEC_ON_READ | OR_ALL,
4701
  "Container for directives based on existence of specified modules"),
4702
AP_INIT_RAW_ARGS("<IfDefine", start_cond_section, (void *)test_ifdefine_section,
4703
              EXEC_ON_READ | OR_ALL,
4704
  "Container for directives based on existence of command line defines"),
4705
AP_INIT_RAW_ARGS("<IfFile", start_cond_section, (void *)test_iffile_section,
4706
              EXEC_ON_READ | OR_ALL,
4707
  "Container for directives based on existence of files on disk"),
4708
AP_INIT_RAW_ARGS("<IfDirective", start_cond_section, (void *)test_ifdirective_section,
4709
              EXEC_ON_READ | OR_ALL,
4710
  "Container for directives based on existence of named directive"),
4711
AP_INIT_RAW_ARGS("<IfSection", start_cond_section, (void *)test_ifsection_section,
4712
              EXEC_ON_READ | OR_ALL,
4713
  "Container for directives based on existence of named section"),
4714
AP_INIT_RAW_ARGS("<DirectoryMatch", dirsection, (void*)1, RSRC_CONF,
4715
  "Container for directives affecting resources located in the "
4716
  "specified directories"),
4717
AP_INIT_RAW_ARGS("<LocationMatch", urlsection, (void*)1, RSRC_CONF,
4718
  "Container for directives affecting resources accessed through the "
4719
  "specified URL paths"),
4720
AP_INIT_RAW_ARGS("<FilesMatch", filesection, (void*)1, OR_ALL,
4721
  "Container for directives affecting files matching specified patterns"),
4722
#ifdef GPROF
4723
AP_INIT_TAKE1("GprofDir", set_gprof_dir, NULL, RSRC_CONF,
4724
  "Directory to plop gmon.out files"),
4725
#endif
4726
AP_INIT_TAKE1("AddDefaultCharset", set_add_default_charset, NULL, OR_FILEINFO,
4727
  "The name of the default charset to add to any Content-Type without one or 'Off' to disable"),
4728
AP_INIT_TAKE1("AcceptPathInfo", set_accept_path_info, NULL, OR_FILEINFO,
4729
  "Set to on or off for PATH_INFO to be accepted by handlers, or default for the per-handler preference"),
4730
AP_INIT_TAKE12("Define", set_define, NULL, EXEC_ON_READ|RSRC_CONF,
4731
              "Define a variable, optionally to a value.  Same as passing -D to the command line."),
4732
AP_INIT_TAKE1("UnDefine", unset_define, NULL, EXEC_ON_READ|RSRC_CONF,
4733
              "Undefine the existence of a variable. Undo a Define."),
4734
AP_INIT_RAW_ARGS("Error", generate_message, (void*) APLOG_ERR, OR_ALL,
4735
                 "Generate error message from within configuration."),
4736
AP_INIT_RAW_ARGS("Warning", generate_message, (void*) APLOG_WARNING, OR_ALL,
4737
                 "Generate warning message from within configuration."),
4738
AP_INIT_RAW_ARGS("<If", ifsection, COND_IF, OR_ALL,
4739
  "Container for directives to be conditionally applied"),
4740
AP_INIT_RAW_ARGS("<ElseIf", ifsection, COND_ELSEIF, OR_ALL,
4741
  "Container for directives to be conditionally applied"),
4742
AP_INIT_RAW_ARGS("<Else", ifsection, COND_ELSE, OR_ALL,
4743
  "Container for directives to be conditionally applied"),
4744
4745
/* Old resource config file commands */
4746
4747
AP_INIT_RAW_ARGS("AccessFileName", set_access_name, NULL, RSRC_CONF,
4748
  "Name(s) of per-directory config files (default: .htaccess)"),
4749
AP_INIT_TAKE1("DocumentRoot", set_document_root, NULL, RSRC_CONF,
4750
  "Root directory of the document tree"),
4751
AP_INIT_TAKE2("ErrorDocument", set_error_document, NULL, OR_FILEINFO,
4752
  "Change responses for HTTP errors"),
4753
AP_INIT_RAW_ARGS("AllowOverride", set_override, NULL, ACCESS_CONF,
4754
  "Controls what groups of directives can be configured by per-directory "
4755
  "config files"),
4756
AP_INIT_TAKE_ARGV("AllowOverrideList", set_override_list, NULL, ACCESS_CONF,
4757
  "Controls what individual directives can be configured by per-directory "
4758
  "config files"),
4759
AP_INIT_RAW_ARGS("Options", set_options, NULL, OR_OPTIONS,
4760
  "Set a number of attributes for a given directory"),
4761
AP_INIT_TAKE1("DefaultType", set_default_type, NULL, OR_FILEINFO,
4762
  "the default media type for otherwise untyped files (DEPRECATED)"),
4763
AP_INIT_RAW_ARGS("FileETag", set_etag_bits, NULL, OR_FILEINFO,
4764
  "Specify components used to construct a file's ETag"),
4765
AP_INIT_TAKE1("EnableMMAP", set_enable_mmap, NULL, OR_FILEINFO,
4766
  "Controls whether memory-mapping may be used to read files"),
4767
AP_INIT_TAKE1("EnableSendfile", set_enable_sendfile, NULL, OR_FILEINFO,
4768
  "Controls whether sendfile may be used to transmit files"),
4769
AP_INIT_TAKE1("ReadBufferSize", set_read_buf_size, NULL, ACCESS_CONF|RSRC_CONF,
4770
  "Size (in bytes) of the memory buffers used to read data"),
4771
AP_INIT_TAKE1("FlushMaxThreshold", set_flush_max_threshold, NULL, RSRC_CONF,
4772
  "Maximum threshold above which pending data are flushed to the network"),
4773
AP_INIT_TAKE1("FlushMaxPipelined", set_flush_max_pipelined, NULL, RSRC_CONF,
4774
  "Maximum number of pipelined responses (pending) above which they are "
4775
  "flushed to the network"),
4776
4777
/* Old server config file commands */
4778
4779
AP_INIT_TAKE1("Protocol", set_protocol, NULL, RSRC_CONF,
4780
  "Set the Protocol for httpd to use."),
4781
AP_INIT_TAKE2("AcceptFilter", set_accf_map, NULL, RSRC_CONF,
4782
  "Set the Accept Filter to use for a protocol"),
4783
AP_INIT_TAKE1("Port", ap_set_deprecated, NULL, RSRC_CONF,
4784
  "Port was replaced with Listen in Apache 2.0"),
4785
AP_INIT_TAKE1("HostnameLookups", set_hostname_lookups, NULL,
4786
  ACCESS_CONF|RSRC_CONF,
4787
  "\"on\" to enable, \"off\" to disable reverse DNS lookups, or \"double\" to "
4788
  "enable double-reverse DNS lookups"),
4789
AP_INIT_TAKE1("ServerAdmin", set_server_string_slot,
4790
  (void *)APR_OFFSETOF(server_rec, server_admin), RSRC_CONF,
4791
  "The email address of the server administrator"),
4792
AP_INIT_TAKE1("ServerName", server_hostname_port, NULL, RSRC_CONF,
4793
  "The hostname and port of the server"),
4794
AP_INIT_TAKE1("ServerSignature", set_signature_flag, NULL, OR_ALL,
4795
  "En-/disable server signature (on|off|email)"),
4796
AP_INIT_TAKE1("ServerRoot", set_server_root, NULL, RSRC_CONF | EXEC_ON_READ,
4797
  "Common directory of server-related files (logs, confs, etc.)"),
4798
AP_INIT_TAKE1("DefaultRuntimeDir", set_runtime_dir, NULL, RSRC_CONF | EXEC_ON_READ,
4799
  "Common directory for run-time files (shared memory, locks, etc.)"),
4800
AP_INIT_TAKE1("DefaultStateDir", set_state_dir, NULL, RSRC_CONF | EXEC_ON_READ,
4801
  "Common directory for persistent state (databases, long-lived caches, etc.)"),
4802
AP_INIT_TAKE12("ErrorLog", set_errorlog,
4803
  (void *)APR_OFFSETOF(server_rec, error_fname), RSRC_CONF,
4804
  "The filename of the error log"),
4805
AP_INIT_TAKE12("ErrorLogFormat", set_errorlog_format, NULL, RSRC_CONF,
4806
  "Format string for the ErrorLog"),
4807
AP_INIT_RAW_ARGS("ServerAlias", set_server_alias, NULL, RSRC_CONF,
4808
  "A name or names alternately used to access the server"),
4809
AP_INIT_TAKE1("ServerPath", set_serverpath, NULL, RSRC_CONF,
4810
  "The pathname the server can be reached at"),
4811
AP_INIT_TAKE1("Timeout", set_timeout, NULL, RSRC_CONF,
4812
  "Timeout duration (sec)"),
4813
AP_INIT_TAKE1("UseCanonicalName", set_use_canonical_name, NULL,
4814
  RSRC_CONF|ACCESS_CONF,
4815
  "How to work out the ServerName : Port when constructing URLs"),
4816
AP_INIT_TAKE1("UseCanonicalPhysicalPort", set_use_canonical_phys_port, NULL,
4817
  RSRC_CONF|ACCESS_CONF,
4818
  "Whether to use the physical Port when constructing URLs"),
4819
/* TODO: RlimitFoo should all be part of mod_cgi, not in the core */
4820
/* TODO: ListenBacklog in MPM */
4821
AP_INIT_TAKE1("Include", include_config, NULL,
4822
  (RSRC_CONF | ACCESS_CONF | EXEC_ON_READ),
4823
  "Name(s) of the config file(s) to be included; fails if the wildcard does "
4824
  "not match at least one file"),
4825
AP_INIT_TAKE1("IncludeOptional", include_config, (void*)1,
4826
  (RSRC_CONF | ACCESS_CONF | EXEC_ON_READ),
4827
  "Name or pattern of the config file(s) to be included; ignored if the file "
4828
  "does not exist or the pattern does not match any files"),
4829
AP_INIT_ITERATE("LogLevel", set_loglevel, NULL, RSRC_CONF|ACCESS_CONF,
4830
  "Level of verbosity in error logging"),
4831
AP_INIT_TAKE_ARGV("LogLevelOverride", set_loglevel_override, NULL, RSRC_CONF,
4832
  "Override LogLevel for clients with certain IPs"),
4833
AP_INIT_TAKE1("NameVirtualHost", ap_set_name_virtual_host, NULL, RSRC_CONF,
4834
  "A numeric IP address:port, or the name of a host"),
4835
AP_INIT_TAKE1("ServerTokens", set_serv_tokens, NULL, RSRC_CONF,
4836
  "Determine tokens displayed in the Server: header - Min(imal), "
4837
  "Major, Minor, Prod(uctOnly), OS, or Full"),
4838
AP_INIT_TAKE1("LimitRequestLine", set_limit_req_line, NULL, RSRC_CONF,
4839
  "Limit on maximum size of an HTTP request line"),
4840
AP_INIT_TAKE1("LimitRequestFieldsize", set_limit_req_fieldsize, NULL,
4841
  RSRC_CONF,
4842
  "Limit on maximum size of an HTTP request header field"),
4843
AP_INIT_TAKE1("LimitRequestFields", set_limit_req_fields, NULL, RSRC_CONF,
4844
  "Limit (0 = unlimited) on max number of header fields in a request message"),
4845
AP_INIT_TAKE1("LimitRequestBody", set_limit_req_body,
4846
  (void*)APR_OFFSETOF(core_dir_config, limit_req_body), OR_ALL,
4847
  "Limit (in bytes) on maximum size of request message body"),
4848
AP_INIT_TAKE1("LimitXMLRequestBody", set_limit_xml_req_body, NULL, OR_ALL,
4849
              "Limit (in bytes) on maximum size of an XML-based request "
4850
              "body"),
4851
AP_INIT_RAW_ARGS("Mutex", ap_set_mutex, NULL, RSRC_CONF,
4852
                 "mutex (or \"default\") and mechanism"),
4853
4854
AP_INIT_TAKE1("MaxRanges", set_max_ranges, NULL, RSRC_CONF|ACCESS_CONF,
4855
              "Maximum number of Ranges in a request before returning the entire "
4856
              "resource, or 0 for unlimited"),
4857
AP_INIT_TAKE1("MaxRangeOverlaps", set_max_overlaps, NULL, RSRC_CONF|ACCESS_CONF,
4858
              "Maximum number of overlaps in Ranges in a request before returning the entire "
4859
              "resource, or 0 for unlimited"),
4860
AP_INIT_TAKE1("MaxRangeReversals", set_max_reversals, NULL, RSRC_CONF|ACCESS_CONF,
4861
              "Maximum number of reversals in Ranges in a request before returning the entire "
4862
              "resource, or 0 for unlimited"),
4863
/* System Resource Controls */
4864
#ifdef RLIMIT_CPU
4865
AP_INIT_TAKE12("RLimitCPU", set_limit_cpu,
4866
  (void*)APR_OFFSETOF(core_dir_config, limit_cpu),
4867
  OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
4868
#else
4869
AP_INIT_TAKE12("RLimitCPU", no_set_limit, NULL,
4870
  OR_ALL, "Soft/hard limits for max CPU usage in seconds"),
4871
#endif
4872
#if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined (RLIMIT_AS)
4873
AP_INIT_TAKE12("RLimitMEM", set_limit_mem,
4874
  (void*)APR_OFFSETOF(core_dir_config, limit_mem),
4875
  OR_ALL, "Soft/hard limits for max memory usage per process"),
4876
#else
4877
AP_INIT_TAKE12("RLimitMEM", no_set_limit, NULL,
4878
  OR_ALL, "Soft/hard limits for max memory usage per process"),
4879
#endif
4880
#ifdef RLIMIT_NPROC
4881
AP_INIT_TAKE12("RLimitNPROC", set_limit_nproc,
4882
  (void*)APR_OFFSETOF(core_dir_config, limit_nproc),
4883
  OR_ALL, "soft/hard limits for max number of processes per uid"),
4884
#else
4885
AP_INIT_TAKE12("RLimitNPROC", no_set_limit, NULL,
4886
   OR_ALL, "soft/hard limits for max number of processes per uid"),
4887
#endif
4888
4889
AP_INIT_RAW_ARGS("RegexDefaultOptions", set_regex_default_options, NULL, RSRC_CONF,
4890
                 "default options for regexes (prefixed by '+' to add, '-' to del)"),
4891
4892
/* internal recursion stopper */
4893
AP_INIT_TAKE12("LimitInternalRecursion", set_recursion_limit, NULL, RSRC_CONF,
4894
              "maximum recursion depth of internal redirects and subrequests"),
4895
4896
AP_INIT_FLAG("CGIPassAuth", set_cgi_pass_auth, NULL, OR_AUTHCFG,
4897
             "Controls whether HTTP authorization headers, normally hidden, will "
4898
             "be passed to scripts"),
4899
AP_INIT_TAKE2("CGIVar", set_cgi_var, NULL, OR_FILEINFO,
4900
              "Controls how some CGI variables are set"),
4901
AP_INIT_FLAG("QualifyRedirectURL", set_qualify_redirect_url, NULL, OR_FILEINFO,
4902
             "Controls whether the REDIRECT_URL environment variable is fully "
4903
             "qualified"),
4904
AP_INIT_FLAG("StrictHostCheck", set_core_server_flag, 
4905
             (void *)APR_OFFSETOF(core_server_config, strict_host_check),  
4906
             RSRC_CONF,
4907
             "Controls whether a hostname match is required"),
4908
AP_INIT_TAKE1("ForceType", ap_set_string_slot_lower,
4909
       (void *)APR_OFFSETOF(core_dir_config, mime_type), OR_FILEINFO,
4910
     "a mime type that overrides other configured type"),
4911
AP_INIT_TAKE1("SetHandler", set_sethandler, NULL, OR_FILEINFO,
4912
   "a handler name that overrides any other configured handler"),
4913
AP_INIT_TAKE1("SetOutputFilter", ap_set_string_slot,
4914
       (void *)APR_OFFSETOF(core_dir_config, output_filters), OR_FILEINFO,
4915
   "filter (or ; delimited list of filters) to be run on the request content"),
4916
AP_INIT_TAKE1("SetInputFilter", ap_set_string_slot,
4917
       (void *)APR_OFFSETOF(core_dir_config, input_filters), OR_FILEINFO,
4918
   "filter (or ; delimited list of filters) to be run on the request body"),
4919
AP_INIT_TAKE1("AllowEncodedSlashes", set_allow2f, NULL, RSRC_CONF,
4920
             "Allow URLs containing '/' encoded as '%2F'"),
4921
4922
/* scoreboard.c directives */
4923
AP_INIT_TAKE1("ScoreBoardFile", ap_set_scoreboard, NULL, RSRC_CONF,
4924
              "A file for Apache to maintain runtime process management information"),
4925
AP_INIT_FLAG("ExtendedStatus", ap_set_extended_status, NULL, RSRC_CONF,
4926
             "\"On\" to track extended status information, \"Off\" to disable"),
4927
AP_INIT_FLAG("SeeRequestTail", ap_set_reqtail, NULL, RSRC_CONF,
4928
             "For extended status, \"On\" to see the last 63 chars of "
4929
             "the request line, \"Off\" (default) to see the first 63"),
4930
4931
/*
4932
 * These are default configuration directives that mpms can/should
4933
 * pay attention to.
4934
 * XXX These are not for all platforms, and even some Unix MPMs might not want
4935
 * some directives.
4936
 */
4937
AP_INIT_TAKE1("PidFile",  ap_mpm_set_pidfile, NULL, RSRC_CONF,
4938
              "A file for logging the server process ID"),
4939
AP_INIT_TAKE1("MaxRequestsPerChild", ap_mpm_set_max_requests, NULL, RSRC_CONF,
4940
              "Maximum number of connections a particular child serves before "
4941
              "dying. (DEPRECATED, use MaxConnectionsPerChild)"),
4942
AP_INIT_TAKE1("MaxConnectionsPerChild", ap_mpm_set_max_requests, NULL, RSRC_CONF,
4943
              "Maximum number of connections a particular child serves before dying."),
4944
AP_INIT_TAKE1("CoreDumpDirectory", ap_mpm_set_coredumpdir, NULL, RSRC_CONF,
4945
              "The location of the directory Apache changes to before dumping core"),
4946
AP_INIT_TAKE1("MaxMemFree", ap_mpm_set_max_mem_free, NULL, RSRC_CONF,
4947
              "Maximum number of 1k blocks a particular child's allocator may hold."),
4948
AP_INIT_TAKE1("ThreadStackSize", ap_mpm_set_thread_stacksize, NULL, RSRC_CONF,
4949
              "Size in bytes of stack used by threads handling client connections"),
4950
#if AP_ENABLE_EXCEPTION_HOOK
4951
AP_INIT_TAKE1("EnableExceptionHook", ap_mpm_set_exception_hook, NULL, RSRC_CONF,
4952
              "Controls whether exception hook may be called after a crash"),
4953
#endif
4954
AP_INIT_TAKE1("TraceEnable", set_trace_enable, NULL, RSRC_CONF,
4955
              "'on' (default), 'off' or 'extended' to trace request body content"),
4956
AP_INIT_FLAG("MergeTrailers", set_merge_trailers, NULL, RSRC_CONF,
4957
              "merge request trailers into request headers or not"),
4958
AP_INIT_ITERATE("HttpProtocolOptions", set_http_protocol_options, NULL, RSRC_CONF,
4959
                "'Allow0.9' or 'Require1.0' (default); "
4960
                "'RegisteredMethods' or 'LenientMethods' (default); "
4961
                "'Unsafe' or 'Strict' (default). Sets HTTP acceptance rules"),
4962
AP_INIT_ITERATE("RegisterHttpMethod", set_http_method, NULL, RSRC_CONF,
4963
                "Registers non-standard HTTP methods"),
4964
AP_INIT_FLAG("HttpContentLengthHeadZero", set_cl_head_zero, NULL, OR_OPTIONS,
4965
  "whether to permit Content-Length of 0 responses to HEAD requests"),
4966
AP_INIT_FLAG("HttpExpectStrict", set_expect_strict, NULL, OR_OPTIONS,
4967
  "whether to return a 417 if a client doesn't send 100-Continue"),
4968
AP_INIT_ITERATE("Protocols", set_protocols, NULL, RSRC_CONF,
4969
                "Controls which protocols are allowed"),
4970
AP_INIT_TAKE1("ProtocolsHonorOrder", set_protocols_honor_order, NULL, RSRC_CONF,
4971
              "'off' (default) or 'on' to respect given order of protocols, "
4972
              "by default the client specified order determines selection"),
4973
AP_INIT_TAKE1("AsyncFilter", set_async_filter, NULL, RSRC_CONF,
4974
              "'network', 'connection' (default) or 'request' to limit the "
4975
              "types of filters that support asynchronous handling"),
4976
AP_INIT_FLAG("MergeSlashes", set_core_server_flag, 
4977
             (void *)APR_OFFSETOF(core_server_config, merge_slashes),  
4978
             RSRC_CONF,
4979
             "Controls whether consecutive slashes in the URI path are merged"),
4980
{ NULL }
4981
};
4982
4983
/*****************************************************************
4984
 *
4985
 * Core handlers for various phases of server operation...
4986
 */
4987
4988
AP_DECLARE_NONSTD(int) ap_core_translate(request_rec *r)
4989
0
{
4990
0
    apr_status_t rv;
4991
0
    char *path;
4992
4993
    /* XXX this seems too specific, this should probably become
4994
     * some general-case test
4995
     */
4996
0
    if (r->proxyreq) {
4997
0
        return HTTP_FORBIDDEN;
4998
0
    }
4999
0
    if (!r->uri || ((r->uri[0] != '/') && strcmp(r->uri, "*"))) {
5000
0
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00126)
5001
0
                     "Invalid URI in request '%s' '%s'", r->uri, r->the_request);
5002
0
        return HTTP_BAD_REQUEST;
5003
0
    }
5004
5005
0
    if (r->server->path
5006
0
        && !strncmp(r->uri, r->server->path, r->server->pathlen)
5007
0
        && (r->server->path[r->server->pathlen - 1] == '/'
5008
0
            || r->uri[r->server->pathlen] == '/'
5009
0
            || r->uri[r->server->pathlen] == '\0'))
5010
0
    {
5011
0
        path = r->uri + r->server->pathlen;
5012
0
    }
5013
0
    else {
5014
0
        path = r->uri;
5015
0
    }
5016
    /*
5017
     * Make sure that we do not mess up the translation by adding two
5018
     * /'s in a row.  This happens under windows when the document
5019
     * root ends with a /
5020
     */
5021
    /* skip all leading /'s (e.g. http://localhost///foo)
5022
     * so we are looking at only the relative path.
5023
     */
5024
0
    while (*path == '/') {
5025
0
        ++path;
5026
0
    }
5027
0
    if ((rv = apr_filepath_merge(&r->filename, ap_document_root(r), path,
5028
0
                                 APR_FILEPATH_TRUENAME
5029
0
                               | APR_FILEPATH_SECUREROOT, r->pool))
5030
0
                != APR_SUCCESS) {
5031
0
        ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00127)
5032
0
                     "Cannot map %s to file", r->the_request);
5033
0
        return HTTP_FORBIDDEN;
5034
0
    }
5035
0
    r->canonical_filename = r->filename;
5036
5037
0
    return OK;
5038
0
}
5039
5040
/*****************************************************************
5041
 *
5042
 * Test the filesystem name through directory_walk and file_walk
5043
 */
5044
static int core_map_to_storage(request_rec *r)
5045
0
{
5046
0
    int access_status;
5047
5048
0
    if ((access_status = ap_directory_walk(r))) {
5049
0
        return access_status;
5050
0
    }
5051
5052
0
    if ((access_status = ap_file_walk(r))) {
5053
0
        return access_status;
5054
0
    }
5055
5056
0
    return OK;
5057
0
}
5058
5059
5060
0
static int do_nothing(request_rec *r) { return OK; }
5061
5062
static int core_override_type(request_rec *r)
5063
0
{
5064
0
    core_dir_config *conf =
5065
0
        (core_dir_config *)ap_get_core_module_config(r->per_dir_config);
5066
5067
    /* Check for overrides with ForceType / SetHandler
5068
     */
5069
0
    if (conf->mime_type && strcmp(conf->mime_type, "none"))
5070
0
        ap_set_content_type(r, (char*) conf->mime_type);
5071
5072
0
    if (conf->expr_handler) { 
5073
0
        const char *err;
5074
0
        const char *val;
5075
0
        val = ap_expr_str_exec(r, conf->expr_handler, &err);
5076
0
        if (err) {
5077
0
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(03154)
5078
0
                          "Can't evaluate handler expression: %s", err);
5079
0
            return HTTP_INTERNAL_SERVER_ERROR;
5080
0
        }
5081
5082
0
        if (val != ap_strstr_c(val, "proxy:unix")) { 
5083
            /* Retained for compatibility --  but not for UDS */
5084
0
            char *tmp = apr_pstrdup(r->pool, val);
5085
0
            ap_str_tolower(tmp);
5086
0
            val = tmp;
5087
0
        }
5088
5089
0
        if (strcmp(val, "none")) { 
5090
0
            r->handler = val;
5091
0
        }
5092
0
    }
5093
0
    else if (conf->handler && strcmp(conf->handler, "none")) { 
5094
0
        r->handler = conf->handler;
5095
0
    }
5096
5097
    /* Deal with the poor soul who is trying to force path_info to be
5098
     * accepted within the core_handler, where they will let the subreq
5099
     * address its contents.  This is toggled by the user in the very
5100
     * beginning of the fixup phase (here!), so modules should override the user's
5101
     * discretion in their own module fixup phase.  It is tristate, if
5102
     * the user doesn't specify, the result is AP_REQ_DEFAULT_PATH_INFO.
5103
     * (which the module may interpret to its own customary behavior.)
5104
     * It won't be touched if the value is no longer AP_ACCEPT_PATHINFO_UNSET,
5105
     * so any module changing the value prior to the fixup phase
5106
     * OVERRIDES the user's choice.
5107
     */
5108
0
    if ((r->used_path_info == AP_REQ_DEFAULT_PATH_INFO)
5109
0
        && (conf->accept_path_info != AP_ACCEPT_PATHINFO_UNSET)) {
5110
        /* No module knew better, and the user coded AcceptPathInfo */
5111
0
        r->used_path_info = conf->accept_path_info;
5112
0
    }
5113
5114
0
    return OK;
5115
0
}
5116
5117
static int default_handler(request_rec *r)
5118
0
{
5119
0
    conn_rec *c = r->connection;
5120
0
    apr_bucket_brigade *bb;
5121
0
    apr_bucket *e;
5122
0
    core_dir_config *d;
5123
0
    int errstatus;
5124
0
    apr_file_t *fd = NULL;
5125
0
    apr_status_t status;
5126
5127
0
    d = (core_dir_config *)ap_get_core_module_config(r->per_dir_config);
5128
5129
0
    ap_allow_standard_methods(r, MERGE_ALLOW, M_GET, M_OPTIONS, M_POST, -1);
5130
5131
    /* If filters intend to consume the request body, they must
5132
     * register an InputFilter to slurp the contents of the POST
5133
     * data from the POST input stream.  It no longer exists when
5134
     * the output filters are invoked by the default handler.
5135
     */
5136
0
    if ((errstatus = ap_discard_request_body(r)) != OK) {
5137
0
        return errstatus;
5138
0
    }
5139
5140
0
    if (r->method_number == M_GET || r->method_number == M_POST) {
5141
0
        if (r->finfo.filetype == APR_NOFILE) {
5142
0
            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00128)
5143
0
                          "File does not exist: %s",
5144
0
                          apr_pstrcat(r->pool, r->filename, r->path_info, NULL));
5145
0
            return HTTP_NOT_FOUND;
5146
0
        }
5147
5148
        /* Don't try to serve a dir.  Some OSs do weird things with
5149
         * raw I/O on a dir.
5150
         */
5151
0
        if (r->finfo.filetype == APR_DIR) {
5152
0
            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00129)
5153
0
                          "Attempt to serve directory: %s", r->filename);
5154
0
            return HTTP_NOT_FOUND;
5155
0
        }
5156
5157
0
        if ((r->used_path_info != AP_REQ_ACCEPT_PATH_INFO) &&
5158
0
            r->path_info && *r->path_info)
5159
0
        {
5160
            /* default to reject */
5161
0
            ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00130)
5162
0
                          "File does not exist: %s",
5163
0
                          apr_pstrcat(r->pool, r->filename, r->path_info, NULL));
5164
0
            return HTTP_NOT_FOUND;
5165
0
        }
5166
5167
        /* We understood the (non-GET) method, but it might not be legal for
5168
           this particular resource. Check to see if the 'deliver_script'
5169
           flag is set. If so, then we go ahead and deliver the file since
5170
           it isn't really content (only GET normally returns content).
5171
5172
           Note: based on logic further above, the only possible non-GET
5173
           method at this point is POST. In the future, we should enable
5174
           script delivery for all methods.  */
5175
0
        if (r->method_number != M_GET) {
5176
0
            core_request_config *req_cfg;
5177
5178
0
            req_cfg = ap_get_core_module_config(r->request_config);
5179
0
            if (!req_cfg->deliver_script) {
5180
                /* The flag hasn't been set for this request. Punt. */
5181
0
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00131)
5182
0
                              "This resource does not accept the %s method.",
5183
0
                              r->method);
5184
0
                return HTTP_METHOD_NOT_ALLOWED;
5185
0
            }
5186
0
        }
5187
5188
5189
0
        if ((status = apr_file_open(&fd, r->filename, APR_READ | APR_BINARY
5190
0
#if APR_HAS_SENDFILE
5191
0
                            | AP_SENDFILE_ENABLED(d->enable_sendfile)
5192
0
#endif
5193
0
                                    , 0, r->pool)) != APR_SUCCESS) {
5194
0
            ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(00132)
5195
0
                          "file permissions deny server access: %s", r->filename);
5196
0
            return HTTP_FORBIDDEN;
5197
0
        }
5198
5199
0
        ap_update_mtime(r, r->finfo.mtime);
5200
0
        ap_set_last_modified(r);
5201
0
        ap_set_etag_fd(r, fd);
5202
0
        ap_set_accept_ranges(r);
5203
0
        ap_set_content_length(r, r->finfo.size);
5204
5205
0
        bb = apr_brigade_create(r->pool, c->bucket_alloc);
5206
5207
0
        if ((errstatus = ap_meets_conditions(r)) != OK) {
5208
0
            apr_file_close(fd);
5209
0
            r->status = errstatus;
5210
0
        }
5211
0
        else {
5212
0
            e = apr_brigade_insert_file(bb, fd, 0, r->finfo.size, r->pool);
5213
5214
0
#if APR_HAS_MMAP
5215
0
            if (d->enable_mmap == ENABLE_MMAP_OFF) {
5216
0
                (void)apr_bucket_file_enable_mmap(e, 0);
5217
0
            }
5218
0
#endif
5219
0
#if APR_MAJOR_VERSION > 1 || (APU_MAJOR_VERSION == 1 && APU_MINOR_VERSION >= 6)
5220
0
            if (d->read_buf_size) {
5221
0
                apr_bucket_file_set_buf_size(e, d->read_buf_size);
5222
0
            }
5223
0
#endif
5224
0
        }
5225
5226
0
        e = apr_bucket_eos_create(c->bucket_alloc);
5227
0
        APR_BRIGADE_INSERT_TAIL(bb, e);
5228
5229
0
        status = ap_pass_brigade(r->output_filters, bb);
5230
0
        apr_brigade_cleanup(bb);
5231
5232
0
        if (status == APR_SUCCESS
5233
0
            || r->status != HTTP_OK
5234
0
            || c->aborted) {
5235
0
            return OK;
5236
0
        }
5237
0
        else {
5238
            /* no way to know what type of error occurred */
5239
0
            ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, r, APLOGNO(00133)
5240
0
                          "default_handler: ap_pass_brigade returned %i",
5241
0
                          status);
5242
0
            return AP_FILTER_ERROR;
5243
0
        }
5244
0
    }
5245
0
    else {              /* unusual method (not GET or POST) */
5246
0
        if (r->method_number == M_INVALID) {
5247
            /* See if this looks like an undecrypted SSL handshake attempt.
5248
             * It's safe to look a couple bytes into the_request if it exists, as it's
5249
             * always allocated at least MIN_LINE_ALLOC (80) bytes.
5250
             */
5251
0
            if (r->the_request
5252
0
                && r->the_request[0] == 0x16
5253
0
                && (r->the_request[1] == 0x2 || r->the_request[1] == 0x3)) {
5254
0
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00134)
5255
0
                              "Invalid method in request %s - possible attempt to establish SSL connection on non-SSL port", r->the_request);
5256
0
            } else {
5257
0
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00135)
5258
0
                              "Invalid method in request %s", r->the_request);
5259
0
            }
5260
0
            return HTTP_NOT_IMPLEMENTED;
5261
0
        }
5262
5263
0
        if (r->method_number == M_OPTIONS) {
5264
0
            return ap_send_http_options(r);
5265
0
        }
5266
0
        return HTTP_METHOD_NOT_ALLOWED;
5267
0
    }
5268
0
}
5269
5270
/* Optional function coming from mod_logio, used for logging of output
5271
 * traffic
5272
 */
5273
APR_OPTIONAL_FN_TYPE(ap_logio_add_bytes_out) *ap__logio_add_bytes_out;
5274
APR_OPTIONAL_FN_TYPE(authz_some_auth_required) *ap__authz_ap_some_auth_required;
5275
5276
/* Insist that at least one module will undertake to provide system
5277
 * security by dropping startup privileges.
5278
 */
5279
static int sys_privileges = 0;
5280
AP_DECLARE(int) ap_sys_privileges_handlers(int inc)
5281
0
{
5282
0
    sys_privileges += inc;
5283
0
    return sys_privileges;
5284
0
}
5285
5286
static int check_errorlog_dir(apr_pool_t *p, server_rec *s)
5287
0
{
5288
0
    if (!s->error_fname || s->error_fname[0] == '|'
5289
0
        || s->errorlog_provider != NULL) {
5290
0
        return APR_SUCCESS;
5291
0
    }
5292
0
    else {
5293
0
        char *abs = ap_server_root_relative(p, s->error_fname);
5294
0
        char *dir = ap_make_dirstr_parent(p, abs);
5295
0
        apr_finfo_t finfo;
5296
0
        apr_status_t rv = apr_stat(&finfo, dir, APR_FINFO_TYPE, p);
5297
0
        if (rv == APR_SUCCESS && finfo.filetype != APR_DIR)
5298
0
            rv = APR_ENOTDIR;
5299
0
        if (rv != APR_SUCCESS) {
5300
0
            const char *desc = "main error log";
5301
0
            if (s->defn_name)
5302
0
                desc = apr_psprintf(p, "error log of vhost defined at %s:%d",
5303
0
                                    s->defn_name, s->defn_line_number);
5304
0
            ap_log_error(APLOG_MARK, APLOG_STARTUP|APLOG_EMERG, rv,
5305
0
                          ap_server_conf, APLOGNO(02291)
5306
0
                         "Cannot access directory '%s' for %s", dir, desc);
5307
0
            return !OK;
5308
0
        }
5309
0
    }
5310
0
    return OK;
5311
0
}
5312
5313
static int core_check_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
5314
0
{
5315
0
    int rv = OK;
5316
0
    while (s) {
5317
0
        if (check_errorlog_dir(ptemp, s) != OK)
5318
0
            rv = !OK;
5319
0
        s = s->next;
5320
0
    }
5321
0
    return rv;
5322
0
}
5323
5324
5325
static int core_pre_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp)
5326
0
{
5327
0
    ap_mutex_init(pconf);
5328
5329
0
    if (!saved_server_config_defines)
5330
0
        init_config_defines(pconf);
5331
0
    apr_pool_cleanup_register(pconf, NULL, reset_config, apr_pool_cleanup_null);
5332
5333
0
    ap_regcomp_set_default_cflags(AP_REG_DEFAULT);
5334
5335
0
    mpm_common_pre_config(pconf);
5336
5337
0
    return OK;
5338
0
}
5339
5340
static int core_post_config(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
5341
0
{
5342
0
    ap__logio_add_bytes_out = APR_RETRIEVE_OPTIONAL_FN(ap_logio_add_bytes_out);
5343
0
    ident_lookup = APR_RETRIEVE_OPTIONAL_FN(ap_ident_lookup);
5344
0
    ap__authz_ap_some_auth_required = APR_RETRIEVE_OPTIONAL_FN(authz_some_auth_required);
5345
0
    authn_ap_auth_type = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_type);
5346
0
    authn_ap_auth_name = APR_RETRIEVE_OPTIONAL_FN(authn_ap_auth_name);
5347
0
    access_compat_ap_satisfies = APR_RETRIEVE_OPTIONAL_FN(access_compat_ap_satisfies);
5348
5349
0
    set_banner(pconf);
5350
0
    ap_setup_make_content_type(pconf);
5351
0
    ap_setup_auth_internal(ptemp);
5352
0
    ap_setup_ssl_optional_fns(pconf);
5353
0
    if (!sys_privileges) {
5354
0
        ap_log_error(APLOG_MARK, APLOG_CRIT, 0, NULL, APLOGNO(00136)
5355
0
                     "Server MUST relinquish startup privileges before "
5356
0
                     "accepting connections.  Please ensure mod_unixd "
5357
0
                     "or other system security module is loaded.");
5358
0
        return !OK;
5359
0
    }
5360
0
    apr_pool_cleanup_register(pconf, NULL, ap_mpm_end_gen_helper,
5361
0
                              apr_pool_cleanup_null);
5362
0
    return OK;
5363
0
}
5364
5365
static void core_insert_filter(request_rec *r)
5366
0
{
5367
0
    core_dir_config *conf = (core_dir_config *)
5368
0
                            ap_get_core_module_config(r->per_dir_config);
5369
0
    const char *filter, *filters = conf->output_filters;
5370
5371
0
    if (filters) {
5372
0
        while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
5373
0
            ap_add_output_filter(filter, NULL, r, r->connection);
5374
0
        }
5375
0
    }
5376
5377
0
    filters = conf->input_filters;
5378
0
    if (filters) {
5379
0
        while (*filters && (filter = ap_getword(r->pool, &filters, ';'))) {
5380
0
            ap_add_input_filter(filter, NULL, r, r->connection);
5381
0
        }
5382
0
    }
5383
0
}
5384
5385
static apr_size_t num_request_notes = AP_NUM_STD_NOTES;
5386
5387
static apr_status_t reset_request_notes(void *dummy)
5388
0
{
5389
0
    num_request_notes = AP_NUM_STD_NOTES;
5390
0
    return APR_SUCCESS;
5391
0
}
5392
5393
AP_DECLARE(apr_size_t) ap_register_request_note(void)
5394
0
{
5395
0
    apr_pool_cleanup_register(apr_hook_global_pool, NULL, reset_request_notes,
5396
0
                              apr_pool_cleanup_null);
5397
0
    return num_request_notes++;
5398
0
}
5399
5400
AP_DECLARE(void **) ap_get_request_note(request_rec *r, apr_size_t note_num)
5401
0
{
5402
0
    core_request_config *req_cfg;
5403
5404
0
    if (note_num >= num_request_notes) {
5405
0
        return NULL;
5406
0
    }
5407
5408
0
    req_cfg = (core_request_config *)
5409
0
        ap_get_core_module_config(r->request_config);
5410
5411
0
    if (!req_cfg) {
5412
0
        return NULL;
5413
0
    }
5414
5415
0
    return &(req_cfg->notes[note_num]);
5416
0
}
5417
5418
AP_DECLARE(apr_socket_t *) ap_get_conn_socket(conn_rec *c)
5419
0
{
5420
0
    conn_config_t *conn_config = ap_get_core_module_config(c->conn_config);
5421
5422
0
    return AP_CORE_DEFAULT(conn_config, socket, NULL);
5423
0
}
5424
5425
static int core_create_req(request_rec *r)
5426
0
{
5427
    /* Alloc the config struct and the array of request notes in
5428
     * a single block for efficiency
5429
     */
5430
0
    core_request_config *req_cfg;
5431
5432
0
    req_cfg = apr_pcalloc(r->pool, sizeof(core_request_config) +
5433
0
                          sizeof(void *) * num_request_notes);
5434
0
    req_cfg->notes = (void **)((char *)req_cfg + sizeof(core_request_config));
5435
5436
    /* ### temporarily enable script delivery as the default */
5437
0
    req_cfg->deliver_script = 1;
5438
5439
0
    if (r->main) {
5440
0
        core_request_config *main_req_cfg = (core_request_config *)
5441
0
            ap_get_core_module_config(r->main->request_config);
5442
0
        req_cfg->bb = main_req_cfg->bb;
5443
0
    }
5444
0
    else {
5445
0
        req_cfg->bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
5446
0
    }
5447
5448
0
    ap_set_core_module_config(r->request_config, req_cfg);
5449
5450
0
    return OK;
5451
0
}
5452
5453
static int core_create_proxy_req(request_rec *r, request_rec *pr)
5454
0
{
5455
0
    return core_create_req(pr);
5456
0
}
5457
5458
static conn_rec *core_create_conn(apr_pool_t *ptrans, server_rec *s,
5459
                                  apr_socket_t *csd, long id, void *sbh,
5460
                                  apr_bucket_alloc_t *alloc)
5461
0
{
5462
0
    apr_status_t rv;
5463
0
    apr_pool_t *pool;
5464
0
    conn_rec *c = (conn_rec *) apr_pcalloc(ptrans, sizeof(conn_rec));
5465
0
    core_server_config *sconf = ap_get_core_module_config(s->module_config);
5466
5467
0
    c->sbh = sbh;
5468
0
    ap_update_child_status(c->sbh, SERVER_BUSY_READ, NULL);
5469
5470
    /* Got a connection structure, so initialize what fields we can
5471
     * (the rest are zeroed out by pcalloc).
5472
     */
5473
0
    apr_pool_create(&pool, ptrans);
5474
0
    apr_pool_tag(pool, "master_conn");
5475
0
    c->pool = pool;
5476
5477
0
    c->conn_config = ap_create_conn_config(c->pool);
5478
0
    c->notes = apr_table_make(c->pool, 5);
5479
0
    c->slaves = apr_array_make(c->pool, 20, sizeof(conn_slave_rec *));
5480
0
    c->requests = apr_array_make(c->pool, 20, sizeof(request_rec *));
5481
5482
0
    if ((rv = apr_socket_addr_get(&c->local_addr, APR_LOCAL, csd))
5483
0
        != APR_SUCCESS) {
5484
0
        ap_log_error(APLOG_MARK, APLOG_INFO, rv, s, APLOGNO(00137)
5485
0
                     "apr_socket_addr_get(APR_LOCAL)");
5486
0
        apr_socket_close(csd);
5487
0
        return NULL;
5488
0
    }
5489
0
    if (apr_sockaddr_ip_get(&c->local_ip, c->local_addr)) {
5490
0
#if APR_HAVE_SOCKADDR_UN
5491
0
        if (c->local_addr->family == APR_UNIX) {
5492
0
            c->local_ip = apr_pstrndup(c->pool, c->local_addr->ipaddr_ptr,
5493
0
                                       c->local_addr->ipaddr_len);
5494
0
        }
5495
0
        else
5496
0
#endif
5497
0
        c->local_ip = apr_pstrdup(c->pool, "unknown");
5498
0
    }
5499
5500
0
    if ((rv = apr_socket_addr_get(&c->client_addr, APR_REMOTE, csd))
5501
0
        != APR_SUCCESS) {
5502
0
        ap_log_error(APLOG_MARK, APLOG_INFO, rv, s, APLOGNO(00138)
5503
0
                     "apr_socket_addr_get(APR_REMOTE)");
5504
0
        apr_socket_close(csd);
5505
0
        return NULL;
5506
0
    }
5507
0
    if (apr_sockaddr_ip_get(&c->client_ip, c->client_addr)) {
5508
0
#if APR_HAVE_SOCKADDR_UN
5509
0
        if (c->client_addr->family == APR_UNIX) {
5510
0
            c->client_ip = apr_pstrndup(c->pool, c->client_addr->ipaddr_ptr,
5511
0
                                        c->client_addr->ipaddr_len);
5512
0
        }
5513
0
        else
5514
0
#endif
5515
0
        c->client_ip = apr_pstrdup(c->pool, "unknown");
5516
0
    }
5517
5518
0
    c->base_server = s;
5519
5520
0
    c->id = id;
5521
0
    c->bucket_alloc = alloc;
5522
0
    c->async_filter = sconf->async_filter;
5523
5524
0
    c->clogging_input_filters = 0;
5525
5526
0
    if (sconf->conn_log_level) {
5527
0
        int i;
5528
0
        conn_log_config *conf;
5529
0
        const struct ap_logconf *log = NULL;
5530
0
        struct ap_logconf *merged;
5531
5532
0
        for (i = 0; i < sconf->conn_log_level->nelts; i++) {
5533
0
            conf = APR_ARRAY_IDX(sconf->conn_log_level, i, conn_log_config *);
5534
0
            if (apr_ipsubnet_test(conf->subnet, c->client_addr))
5535
0
                log = &conf->log;
5536
0
        }
5537
0
        if (log) {
5538
0
            merged = ap_new_log_config(c->pool, log);
5539
0
            ap_merge_log_config(&s->log, merged);
5540
0
            c->log = merged;
5541
0
        }
5542
0
    }
5543
5544
0
    return c;
5545
0
}
5546
5547
static conn_rec *core_create_secondary_conn(apr_pool_t *ptrans,
5548
                                            conn_rec *master,
5549
                                            apr_bucket_alloc_t *alloc)
5550
0
{
5551
0
    apr_pool_t *pool;
5552
0
    conn_config_t *conn_config;
5553
0
    conn_rec *c = (conn_rec *) apr_pmemdup(ptrans, master, sizeof(*c));
5554
5555
    /* Got a connection structure, so initialize what fields we can
5556
     * (the rest are zeroed out by pcalloc).
5557
     */
5558
0
    apr_pool_create(&pool, ptrans);
5559
0
    apr_pool_tag(pool, "secondary_conn");
5560
5561
    /* we copied everything, now replace what is different */
5562
0
    c->master = master;
5563
0
    c->pool = pool;
5564
0
    c->bucket_alloc = alloc;
5565
0
    c->conn_config = ap_create_conn_config(pool);
5566
0
    c->notes = apr_table_make(pool, 5);
5567
0
    c->slaves = apr_array_make(pool, 20, sizeof(conn_slave_rec *));
5568
0
    c->requests = apr_array_make(pool, 20, sizeof(request_rec *));
5569
0
    c->input_filters = NULL;
5570
0
    c->output_filters = NULL;
5571
0
    c->filter_conn_ctx = NULL;
5572
5573
    /* prevent mpm_event from making wrong assumptions about this connection,
5574
     * like e.g. using its socket for an async read check. */
5575
0
    c->clogging_input_filters = 1;
5576
5577
0
    c->log = NULL;
5578
0
    c->aborted = 0;
5579
0
    c->keepalives = 0;
5580
5581
    /* FIXME: mpms (and maybe other) parts think that there is always
5582
     * a socket for a connection. We cannot use the master socket for
5583
     * secondary connections, as this gets modified (closed?) when
5584
     * the secondary connection terminates.
5585
     * There seem to be some checks for c->master necessary in other
5586
     * places.
5587
     */
5588
0
    conn_config = apr_pcalloc(pool, sizeof(*conn_config));
5589
0
    conn_config->socket = dummy_socket;
5590
0
    ap_set_core_module_config(c->conn_config, conn_config);
5591
5592
0
    return c;
5593
0
}
5594
5595
static int core_pre_connection(conn_rec *c, void *csd)
5596
0
{
5597
0
    conn_config_t *conn_config;
5598
0
    apr_status_t rv;
5599
5600
    /* only the master connection talks to the network */
5601
0
    if (c->master) {
5602
0
        return DONE;
5603
0
    }
5604
5605
    /* The Nagle algorithm says that we should delay sending partial
5606
     * packets in hopes of getting more data.  We don't want to do
5607
     * this; we are not telnet.  There are bad interactions between
5608
     * persistent connections and Nagle's algorithm that have very severe
5609
     * performance penalties.  (Failing to disable Nagle is not much of a
5610
     * problem with simple HTTP.)
5611
     */
5612
0
    rv = apr_socket_opt_set(csd, APR_TCP_NODELAY, 1);
5613
0
    if (rv != APR_SUCCESS && rv != APR_ENOTIMPL) {
5614
        /* expected cause is that the client disconnected already,
5615
         * hence the debug level
5616
         */
5617
0
        ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c, APLOGNO(00139)
5618
0
                      "apr_socket_opt_set(APR_TCP_NODELAY)");
5619
0
    }
5620
5621
    /* The core filter requires the timeout mode to be set, which
5622
     * incidentally sets the socket to be nonblocking.  If this
5623
     * is not initialized correctly, Linux - for example - will
5624
     * be initially blocking, while Solaris will be non blocking
5625
     * and any initial read will fail.
5626
     */
5627
0
    rv = apr_socket_timeout_set(csd, c->base_server->timeout);
5628
0
    if (rv != APR_SUCCESS) {
5629
        /* expected cause is that the client disconnected already */
5630
0
        ap_log_cerror(APLOG_MARK, APLOG_DEBUG, rv, c, APLOGNO(00140)
5631
0
                      "apr_socket_timeout_set");
5632
0
    }
5633
5634
0
    conn_config = apr_pcalloc(c->pool, sizeof(*conn_config));
5635
0
    conn_config->socket = csd;
5636
0
    ap_set_core_module_config(c->conn_config, conn_config);
5637
5638
0
    ap_add_input_filter_handle(ap_core_input_filter_handle, NULL, NULL, c);
5639
0
    ap_add_output_filter_handle(ap_core_output_filter_handle, NULL, NULL, c);
5640
5641
0
    return DONE;
5642
0
}
5643
5644
AP_DECLARE(int) ap_pre_connection(conn_rec *c, void *csd)
5645
0
{
5646
0
    int rc = OK;
5647
5648
0
    rc = ap_run_pre_connection(c, csd);
5649
0
    if (rc != OK && rc != DONE) {
5650
0
        c->aborted = 1;
5651
        /*
5652
         * In case we errored, the pre_connection hook of the core
5653
         * module maybe did not run (it is APR_HOOK_REALLY_LAST) and
5654
         * hence we missed to
5655
         *
5656
         * - Put the socket in c->conn_config
5657
         * - Setup core output and input filters
5658
         * - Set socket options and timeouts
5659
         *
5660
         * Hence call it in this case.
5661
         */
5662
0
        if (!ap_get_conn_socket(c)) {
5663
0
            core_pre_connection(c, csd);
5664
0
        }
5665
0
    }
5666
0
    return rc;
5667
0
}
5668
5669
AP_CORE_DECLARE(conn_rec *) ap_create_slave_connection(conn_rec *c)
5670
0
{
5671
0
    apr_pool_t *pool;
5672
0
    conn_slave_rec *new;
5673
0
    conn_rec *sc = (conn_rec *) apr_palloc(c->pool, sizeof(conn_rec));
5674
5675
0
    apr_pool_create(&pool, c->pool);
5676
0
    apr_pool_tag(pool, "slave_conn");
5677
0
    memcpy(sc, c, sizeof(conn_rec));
5678
0
    sc->slaves = NULL;
5679
0
    sc->master = c;
5680
0
    sc->input_filters = NULL;
5681
0
    sc->output_filters = NULL;
5682
0
    sc->filter_conn_ctx = NULL;
5683
0
    sc->pool = pool;
5684
0
    new = apr_array_push(c->slaves);
5685
0
    new->c = sc;
5686
0
    return sc;
5687
0
}
5688
5689
AP_DECLARE(int) ap_state_query(int query)
5690
0
{
5691
0
    switch (query) {
5692
0
    case AP_SQ_MAIN_STATE:
5693
0
        return ap_main_state;
5694
0
    case AP_SQ_RUN_MODE:
5695
0
        return ap_run_mode;
5696
0
    case AP_SQ_CONFIG_GEN:
5697
0
        return ap_config_generation;
5698
0
    default:
5699
0
        return AP_SQ_NOT_SUPPORTED;
5700
0
    }
5701
0
}
5702
5703
AP_DECLARE(char *) ap_state_dir_relative(apr_pool_t *p, const char *file)
5704
0
{
5705
0
    char *newpath = NULL;
5706
0
    apr_status_t rv;
5707
0
    const char *state_dir;
5708
5709
0
    state_dir = core_state_dir
5710
0
        ? core_state_dir
5711
0
        : ap_server_root_relative(p, DEFAULT_REL_STATEDIR);
5712
5713
0
    rv = apr_filepath_merge(&newpath, state_dir, file, APR_FILEPATH_TRUENAME, p);
5714
0
    if (newpath && (rv == APR_SUCCESS || APR_STATUS_IS_EPATHWILD(rv)
5715
0
                                      || APR_STATUS_IS_ENOENT(rv)
5716
0
                                      || APR_STATUS_IS_ENOTDIR(rv))) {
5717
0
        return newpath;
5718
0
    }
5719
0
    else {
5720
0
        return NULL;
5721
0
    }
5722
0
}
5723
5724
5725
static apr_random_t *rng = NULL;
5726
#if APR_HAS_THREADS
5727
static apr_thread_mutex_t *rng_mutex = NULL;
5728
#endif
5729
5730
static void core_child_init(apr_pool_t *pchild, server_rec *s)
5731
0
{
5732
0
    apr_proc_t proc;
5733
0
#if APR_HAS_THREADS
5734
0
    {
5735
0
        int threaded_mpm;
5736
0
        if (ap_mpm_query(AP_MPMQ_IS_THREADED, &threaded_mpm) == APR_SUCCESS
5737
0
            && threaded_mpm)
5738
0
        {
5739
0
            apr_thread_mutex_create(&rng_mutex, APR_THREAD_MUTEX_DEFAULT, pchild);
5740
0
        }
5741
0
    }
5742
0
#endif
5743
    /* The MPMs use plain fork() and not apr_proc_fork(), so we have to call
5744
     * apr_random_after_fork() manually in the child
5745
     */
5746
0
    proc.pid = getpid();
5747
0
    apr_random_after_fork(&proc);
5748
5749
    /* needed for secondary connections so people do not change the master
5750
     * connection socket. */
5751
0
    apr_socket_create(&dummy_socket, APR_INET, SOCK_STREAM,
5752
0
                      APR_PROTO_TCP, pchild);
5753
0
}
5754
5755
static void core_optional_fn_retrieve(void)
5756
0
{
5757
0
    ap_init_scoreboard(NULL);
5758
0
}
5759
5760
AP_CORE_DECLARE(void) ap_random_parent_after_fork(void)
5761
0
{
5762
    /*
5763
     * To ensure that the RNG state in the parent changes after the fork, we
5764
     * pull some data from the RNG and discard it. This ensures that the RNG
5765
     * states in the children are different even after the pid wraps around.
5766
     * As we only use apr_random for insecure random bytes, pulling 2 bytes
5767
     * should be enough.
5768
     * XXX: APR should probably have some dedicated API to do this, but it
5769
     * XXX: currently doesn't.
5770
     */
5771
0
    apr_uint16_t data;
5772
0
    apr_random_insecure_bytes(rng, &data, sizeof(data));
5773
0
}
5774
5775
AP_CORE_DECLARE(void) ap_init_rng(apr_pool_t *p)
5776
0
{
5777
0
    unsigned char seed[8];
5778
0
    apr_status_t rv;
5779
0
    rng = apr_random_standard_new(p);
5780
0
    do {
5781
0
        rv = apr_generate_random_bytes(seed, sizeof(seed));
5782
0
        if (rv != APR_SUCCESS)
5783
0
            goto error;
5784
0
        apr_random_add_entropy(rng, seed, sizeof(seed));
5785
0
        rv = apr_random_insecure_ready(rng);
5786
0
    } while (rv == APR_ENOTENOUGHENTROPY);
5787
0
    if (rv == APR_SUCCESS)
5788
0
        return;
5789
0
error:
5790
0
    ap_log_error(APLOG_MARK, APLOG_CRIT, rv, NULL, APLOGNO(00141)
5791
0
                 "Could not initialize random number generator");
5792
0
    exit(1);
5793
0
}
5794
5795
AP_DECLARE(void) ap_random_insecure_bytes(void *buf, apr_size_t size)
5796
0
{
5797
0
#if APR_HAS_THREADS
5798
0
    if (rng_mutex)
5799
0
        apr_thread_mutex_lock(rng_mutex);
5800
0
#endif
5801
    /* apr_random_insecure_bytes can only fail with APR_ENOTENOUGHENTROPY,
5802
     * and we have ruled that out during initialization. Therefore we don't
5803
     * need to check the return code.
5804
     */
5805
0
    apr_random_insecure_bytes(rng, buf, size);
5806
0
#if APR_HAS_THREADS
5807
0
    if (rng_mutex)
5808
0
        apr_thread_mutex_unlock(rng_mutex);
5809
0
#endif
5810
0
}
5811
5812
/*
5813
 * Finding a random number in a range.
5814
 *      n' = a + n(b-a+1)/(M+1)
5815
 * where:
5816
 *      n' = random number in range
5817
 *      a  = low end of range
5818
 *      b  = high end of range
5819
 *      n  = random number of 0..M
5820
 *      M  = maxint
5821
 * Algorithm 'borrowed' from PHP's rand() function.
5822
 */
5823
0
#define RAND_RANGE(__n, __min, __max, __tmax) \
5824
0
(__n) = (__min) + (long) ((double) ((__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
5825
AP_DECLARE(apr_uint32_t) ap_random_pick(apr_uint32_t min, apr_uint32_t max)
5826
0
{
5827
0
    apr_uint32_t number;
5828
0
#if (!__GNUC__ || __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) || \
5829
0
     !__sparc__ || APR_SIZEOF_VOIDP != 8)
5830
    /* This triggers a gcc bug on sparc/64bit with gcc < 4.8, PR 52900 */
5831
0
    if (max < 16384) {
5832
0
        apr_uint16_t num16;
5833
0
        ap_random_insecure_bytes(&num16, sizeof(num16));
5834
0
        RAND_RANGE(num16, min, max, APR_UINT16_MAX);
5835
0
        number = num16;
5836
0
    }
5837
0
    else
5838
0
#endif
5839
0
    {
5840
0
        ap_random_insecure_bytes(&number, sizeof(number));
5841
0
        RAND_RANGE(number, min, max, APR_UINT32_MAX);
5842
0
    }
5843
0
    return number;
5844
0
}
5845
5846
static apr_status_t core_insert_network_bucket(conn_rec *c,
5847
                                               apr_bucket_brigade *bb,
5848
                                               apr_socket_t *socket)
5849
0
{
5850
0
    apr_bucket *e = apr_bucket_socket_create(socket, c->bucket_alloc);
5851
0
    APR_BRIGADE_INSERT_TAIL(bb, e);
5852
0
    return APR_SUCCESS;
5853
0
}
5854
5855
static apr_status_t core_dirwalk_stat(apr_finfo_t *finfo, request_rec *r,
5856
                                      apr_int32_t wanted) 
5857
0
{
5858
0
    return apr_stat(finfo, r->filename, wanted, r->pool);
5859
0
}
5860
5861
static void core_dump_config(apr_pool_t *p, server_rec *s)
5862
0
{
5863
0
    core_server_config *sconf = ap_get_core_module_config(s->module_config);
5864
0
    apr_file_t *out = NULL;
5865
0
    const char *tmp;
5866
0
    const char **defines;
5867
0
    int i;
5868
0
    if (!ap_exists_config_define("DUMP_RUN_CFG"))
5869
0
        return;
5870
5871
0
    apr_file_open_stdout(&out, p);
5872
0
    apr_file_printf(out, "ServerRoot: \"%s\"\n", ap_server_root);
5873
0
    tmp = ap_server_root_relative(p, sconf->ap_document_root);
5874
0
    apr_file_printf(out, "Main DocumentRoot: \"%s\"\n", tmp);
5875
0
    if (s->error_fname[0] != '|' && s->errorlog_provider == NULL)
5876
0
        tmp = ap_server_root_relative(p, s->error_fname);
5877
0
    else
5878
0
        tmp = s->error_fname;
5879
0
    apr_file_printf(out, "Main ErrorLog: \"%s\"\n", tmp);
5880
0
    if (ap_scoreboard_fname) {
5881
0
        tmp = ap_runtime_dir_relative(p, ap_scoreboard_fname);
5882
0
        apr_file_printf(out, "ScoreBoardFile: \"%s\"\n", tmp);
5883
0
    }
5884
0
    ap_dump_mutexes(p, s, out);
5885
0
    ap_mpm_dump_pidfile(p, out);
5886
5887
0
    defines = (const char **)ap_server_config_defines->elts;
5888
0
    for (i = 0; i < ap_server_config_defines->nelts; i++) {
5889
0
        const char *name = defines[i];
5890
0
        const char *val = NULL;
5891
0
        if (server_config_defined_vars)
5892
0
           val = apr_table_get(server_config_defined_vars, name);
5893
0
        if (val)
5894
0
            apr_file_printf(out, "Define: %s=%s\n", name, val);
5895
0
        else
5896
0
            apr_file_printf(out, "Define: %s\n", name);
5897
0
    }
5898
0
}
5899
5900
static int core_upgrade_handler(request_rec *r)
5901
0
{
5902
0
    conn_rec *c = r->connection;
5903
0
    const char *upgrade;
5904
5905
0
    if (c->master) {
5906
        /* Not possible to perform an HTTP/1.1 upgrade from a slave
5907
         * connection. */
5908
0
        return DECLINED;
5909
0
    }
5910
    
5911
0
    upgrade = apr_table_get(r->headers_in, "Upgrade");
5912
0
    if (upgrade && *upgrade) {
5913
0
        const char *conn = apr_table_get(r->headers_in, "Connection");
5914
0
        if (ap_find_token(r->pool, conn, "upgrade")) {
5915
0
            apr_array_header_t *offers = NULL;
5916
0
            const char *err;
5917
            
5918
0
            err = ap_parse_token_list_strict(r->pool, upgrade, &offers, 0);
5919
0
            if (err) {
5920
0
                ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02910)
5921
0
                              "parsing Upgrade header: %s", err);
5922
0
                return DECLINED;
5923
0
            }
5924
            
5925
0
            if (offers && offers->nelts > 0) {
5926
0
                const char *protocol = ap_select_protocol(c, r, NULL, offers);
5927
0
                if (protocol && strcmp(protocol, ap_get_protocol(c))) {
5928
0
                    ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02909)
5929
0
                                  "Upgrade selects '%s'", protocol);
5930
                    /* Let the client know what we are upgrading to. */
5931
0
                    apr_table_clear(r->headers_out);
5932
0
                    apr_table_setn(r->headers_out, "Upgrade", protocol);
5933
0
                    apr_table_setn(r->headers_out, "Connection", "Upgrade");
5934
                    
5935
0
                    r->status = HTTP_SWITCHING_PROTOCOLS;
5936
0
                    r->status_line = ap_get_status_line(r->status);
5937
0
                    ap_send_interim_response(r, 1);
5938
5939
0
                    ap_switch_protocol(c, r, r->server, protocol);
5940
5941
                    /* make sure httpd closes the connection after this */
5942
0
                    c->keepalive = AP_CONN_CLOSE;
5943
0
                    return DONE;
5944
0
                }
5945
0
            }
5946
0
        }
5947
0
    }
5948
0
    else if (!c->keepalives) {
5949
        /* first request on a master connection, if we have protocols other
5950
         * than the current one enabled here, announce them to the
5951
         * client. If the client is already talking a protocol with requests
5952
         * on slave connections, leave it be. */
5953
0
        const apr_array_header_t *upgrades;
5954
0
        ap_get_protocol_upgrades(c, r, NULL, 0, &upgrades);
5955
0
        if (upgrades && upgrades->nelts > 0) {
5956
0
            char *protocols = apr_array_pstrcat(r->pool, upgrades, ',');
5957
0
            apr_table_setn(r->headers_out, "Upgrade", protocols);
5958
0
            apr_table_setn(r->headers_out, "Connection", "Upgrade");
5959
0
        }
5960
0
    }
5961
    
5962
0
    return DECLINED;
5963
0
}
5964
5965
static int core_upgrade_storage(request_rec *r)
5966
0
{
5967
0
    if ((r->method_number == M_OPTIONS) && r->uri && (r->uri[0] == '*') &&
5968
0
        (r->uri[1] == '\0')) {
5969
0
        return core_upgrade_handler(r);
5970
0
    }
5971
0
    return DECLINED;
5972
0
}
5973
5974
static void register_hooks(apr_pool_t *p)
5975
0
{
5976
0
    errorlog_hash = apr_hash_make(p);
5977
0
    ap_register_log_hooks(p);
5978
0
    ap_register_config_hooks(p);
5979
0
    ap_expr_init(p);
5980
5981
    /* create_connection and pre_connection should always be hooked
5982
     * APR_HOOK_REALLY_LAST by core to give other modules the opportunity
5983
     * to install alternate network transports and stop other functions
5984
     * from being run.
5985
     */
5986
0
    ap_hook_create_connection(core_create_conn, NULL, NULL,
5987
0
                              APR_HOOK_REALLY_LAST);
5988
0
    ap_hook_create_secondary_connection(core_create_secondary_conn, NULL, NULL,
5989
0
                                        APR_HOOK_REALLY_LAST);
5990
0
    ap_hook_pre_connection(core_pre_connection, NULL, NULL,
5991
0
                           APR_HOOK_REALLY_LAST);
5992
5993
0
    ap_hook_pre_config(core_pre_config, NULL, NULL, APR_HOOK_REALLY_FIRST);
5994
0
    ap_hook_post_config(core_post_config,NULL,NULL,APR_HOOK_REALLY_FIRST);
5995
0
    ap_hook_check_config(core_check_config,NULL,NULL,APR_HOOK_FIRST);
5996
0
    ap_hook_test_config(core_dump_config,NULL,NULL,APR_HOOK_FIRST);
5997
0
    ap_hook_translate_name(ap_core_translate,NULL,NULL,APR_HOOK_REALLY_LAST);
5998
0
    ap_hook_map_to_storage(core_upgrade_storage,NULL,NULL,APR_HOOK_REALLY_FIRST);
5999
0
    ap_hook_map_to_storage(core_map_to_storage,NULL,NULL,APR_HOOK_REALLY_LAST);
6000
0
    ap_hook_open_logs(ap_open_logs,NULL,NULL,APR_HOOK_REALLY_FIRST);
6001
0
    ap_hook_child_init(core_child_init,NULL,NULL,APR_HOOK_REALLY_FIRST);
6002
0
    ap_hook_child_init(ap_logs_child_init,NULL,NULL,APR_HOOK_MIDDLE);
6003
0
    ap_hook_handler(core_upgrade_handler,NULL,NULL,APR_HOOK_REALLY_FIRST);
6004
0
    ap_hook_handler(default_handler,NULL,NULL,APR_HOOK_REALLY_LAST);
6005
    /* FIXME: I suspect we can eliminate the need for these do_nothings - Ben */
6006
0
    ap_hook_type_checker(do_nothing,NULL,NULL,APR_HOOK_REALLY_LAST);
6007
0
    ap_hook_fixups(core_override_type,NULL,NULL,APR_HOOK_REALLY_FIRST);
6008
0
    ap_hook_create_request(core_create_req, NULL, NULL, APR_HOOK_MIDDLE);
6009
0
    APR_OPTIONAL_HOOK(proxy, create_req, core_create_proxy_req, NULL, NULL,
6010
0
                      APR_HOOK_MIDDLE);
6011
0
    ap_hook_pre_mpm(ap_create_scoreboard, NULL, NULL, APR_HOOK_MIDDLE);
6012
0
    ap_hook_child_status(ap_core_child_status, NULL, NULL, APR_HOOK_MIDDLE);
6013
0
    ap_hook_insert_network_bucket(core_insert_network_bucket, NULL, NULL,
6014
0
                                  APR_HOOK_REALLY_LAST);
6015
0
    ap_hook_dirwalk_stat(core_dirwalk_stat, NULL, NULL, APR_HOOK_REALLY_LAST);
6016
0
    ap_hook_open_htaccess(ap_open_htaccess, NULL, NULL, APR_HOOK_REALLY_LAST);
6017
0
    ap_hook_optional_fn_retrieve(core_optional_fn_retrieve, NULL, NULL,
6018
0
                                 APR_HOOK_MIDDLE);
6019
6020
0
    ap_hook_input_pending(ap_filter_input_pending, NULL, NULL,
6021
0
                          APR_HOOK_MIDDLE);
6022
0
    ap_hook_output_pending(ap_filter_output_pending, NULL, NULL,
6023
0
                           APR_HOOK_MIDDLE);
6024
6025
    /* register the core's insert_filter hook and register core-provided
6026
     * filters
6027
     */
6028
0
    ap_hook_insert_filter(core_insert_filter, NULL, NULL, APR_HOOK_MIDDLE);
6029
6030
0
    ap_core_input_filter_handle =
6031
0
        ap_register_input_filter("CORE_IN", ap_core_input_filter,
6032
0
                                 NULL, AP_FTYPE_NETWORK);
6033
0
    ap_content_length_filter_handle =
6034
0
        ap_register_output_filter("CONTENT_LENGTH", ap_content_length_filter,
6035
0
                                  NULL, AP_FTYPE_PROTOCOL);
6036
0
    ap_core_output_filter_handle =
6037
0
        ap_register_output_filter("CORE", ap_core_output_filter,
6038
0
                                  NULL, AP_FTYPE_NETWORK);
6039
0
    ap_subreq_core_filter_handle =
6040
0
        ap_register_output_filter("SUBREQ_CORE", ap_sub_req_output_filter,
6041
0
                                  NULL, AP_FTYPE_CONTENT_SET);
6042
0
    ap_old_write_func =
6043
0
        ap_register_output_filter("OLD_WRITE", ap_old_write_filter,
6044
0
                                  NULL, AP_FTYPE_RESOURCE - 10);
6045
0
}
6046
6047
AP_DECLARE_MODULE(core) = {
6048
    MPM20_MODULE_STUFF,
6049
    AP_PLATFORM_REWRITE_ARGS_HOOK, /* hook to run before apache parses args */
6050
    create_core_dir_config,       /* create per-directory config structure */
6051
    merge_core_dir_configs,       /* merge per-directory config structures */
6052
    create_core_server_config,    /* create per-server config structure */
6053
    merge_core_server_configs,    /* merge per-server config structures */
6054
    core_cmds,                    /* command apr_table_t */
6055
    register_hooks                /* register hooks */
6056
};