Coverage Report

Created: 2026-10-17 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/httpd/server/request.c
Line
Count
Source
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
/*
18
 * @file  request.c
19
 * @brief functions to get and process requests
20
 *
21
 * @author Rob McCool 3/21/93
22
 *
23
 * Thoroughly revamped by rst for Apache.  NB this file reads
24
 * best from the bottom up.
25
 *
26
 */
27
28
#include "apr_strings.h"
29
#include "apr_file_io.h"
30
#include "apr_fnmatch.h"
31
32
#define APR_WANT_STRFUNC
33
#include "apr_want.h"
34
35
#include "ap_config.h"
36
#include "ap_provider.h"
37
#include "httpd.h"
38
#include "http_config.h"
39
#include "http_request.h"
40
#include "http_core.h"
41
#include "http_protocol.h"
42
#include "http_log.h"
43
#include "http_main.h"
44
#include "util_filter.h"
45
#include "util_charset.h"
46
#include "util_script.h"
47
#include "ap_expr.h"
48
#include "mod_request.h"
49
50
#include "mod_core.h"
51
#include "mod_auth.h"
52
53
#if APR_HAVE_STDARG_H
54
#include <stdarg.h>
55
#endif
56
57
/* we know core's module_index is 0 */
58
#undef APLOG_MODULE_INDEX
59
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
60
61
APR_HOOK_STRUCT(
62
    APR_HOOK_LINK(pre_translate_name)
63
    APR_HOOK_LINK(translate_name)
64
    APR_HOOK_LINK(map_to_storage)
65
    APR_HOOK_LINK(check_user_id)
66
    APR_HOOK_LINK(fixups)
67
    APR_HOOK_LINK(type_checker)
68
    APR_HOOK_LINK(access_checker)
69
    APR_HOOK_LINK(access_checker_ex)
70
    APR_HOOK_LINK(auth_checker)
71
    APR_HOOK_LINK(insert_filter)
72
    APR_HOOK_LINK(create_request)
73
    APR_HOOK_LINK(post_perdir_config)
74
    APR_HOOK_LINK(dirwalk_stat)
75
    APR_HOOK_LINK(force_authn)
76
    APR_HOOK_LINK(token_checker)
77
)
78
79
AP_IMPLEMENT_HOOK_RUN_FIRST(int,pre_translate_name,
80
                            (request_rec *r), (r), DECLINED)
81
AP_IMPLEMENT_HOOK_RUN_FIRST(int,translate_name,
82
                            (request_rec *r), (r), DECLINED)
83
AP_IMPLEMENT_HOOK_RUN_FIRST(int,map_to_storage,
84
                            (request_rec *r), (r), DECLINED)
85
AP_IMPLEMENT_HOOK_RUN_FIRST(int,check_user_id,
86
                            (request_rec *r), (r), DECLINED)
87
AP_IMPLEMENT_HOOK_RUN_ALL(int,fixups,
88
                          (request_rec *r), (r), OK, DECLINED)
89
AP_IMPLEMENT_HOOK_RUN_FIRST(int,type_checker,
90
                            (request_rec *r), (r), DECLINED)
91
AP_IMPLEMENT_HOOK_RUN_ALL(int,access_checker,
92
                          (request_rec *r), (r), OK, DECLINED)
93
AP_IMPLEMENT_HOOK_RUN_FIRST(int,access_checker_ex,
94
                          (request_rec *r), (r), DECLINED)
95
AP_IMPLEMENT_HOOK_RUN_FIRST(int,auth_checker,
96
                            (request_rec *r), (r), DECLINED)
97
AP_IMPLEMENT_HOOK_VOID(insert_filter, (request_rec *r), (r))
98
AP_IMPLEMENT_HOOK_RUN_ALL(int, create_request,
99
                          (request_rec *r), (r), OK, DECLINED)
100
AP_IMPLEMENT_HOOK_RUN_ALL(int, post_perdir_config,
101
                          (request_rec *r), (r), OK, DECLINED)
102
AP_IMPLEMENT_HOOK_RUN_FIRST(apr_status_t,dirwalk_stat,
103
                            (apr_finfo_t *finfo, request_rec *r, apr_int32_t wanted),
104
                            (finfo, r, wanted), AP_DECLINED)
105
AP_IMPLEMENT_HOOK_RUN_FIRST(int,force_authn,
106
                            (request_rec *r), (r), DECLINED)
107
AP_IMPLEMENT_HOOK_RUN_FIRST(int,token_checker,
108
                            (request_rec *r), (r), DECLINED)
109
110
static int auth_internal_per_conf = 0;
111
static int auth_internal_per_conf_hooks = 0;
112
static int auth_internal_per_conf_providers = 0;
113
114
115
static int decl_die(int status, const char *phase, request_rec *r)
116
0
{
117
0
    if (status == DECLINED) {
118
0
        ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(00025)
119
0
                      "configuration error:  couldn't %s: %s", phase, r->uri);
120
0
        return HTTP_INTERNAL_SERVER_ERROR;
121
0
    }
122
0
    else {
123
0
        ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
124
0
                      "auth phase '%s' gave status %d: %s", phase,
125
0
                      status, r->uri);
126
0
        return status;
127
0
    }
128
0
}
129
130
AP_DECLARE(int) ap_some_authn_required(request_rec *r)
131
0
{
132
0
    int access_status;
133
0
    char *olduser = r->user;
134
0
    int rv = FALSE;
135
136
0
    switch (ap_satisfies(r)) {
137
0
    case SATISFY_ALL:
138
0
    case SATISFY_NOSPEC:
139
0
        if ((access_status = ap_run_access_checker(r)) != OK) {
140
0
            break;
141
0
        }
142
143
0
        access_status = ap_run_access_checker_ex(r);
144
0
        if (access_status == DECLINED) {
145
0
            rv = TRUE;
146
0
        }
147
148
0
        break;
149
0
    case SATISFY_ANY:
150
0
        if ((access_status = ap_run_access_checker(r)) == OK) {
151
0
            break;
152
0
        }
153
154
0
        access_status = ap_run_access_checker_ex(r);
155
0
        if (access_status == DECLINED) {
156
0
            rv = TRUE;
157
0
        }
158
159
0
        break;
160
0
    }
161
162
0
    r->user = olduser;
163
0
    return rv;
164
0
}
165
166
static int walk_location_and_if(request_rec *r)
167
0
{
168
0
    int access_status;
169
170
0
    if ((access_status = ap_location_walk(r))) {
171
0
        return access_status;
172
0
    }
173
0
    if ((access_status = ap_if_walk(r))) {
174
0
        return access_status;
175
0
    }
176
177
    /* Don't set per-dir loglevel if LogLevelOverride is set */
178
0
    if (!r->connection->log) {
179
0
        core_dir_config *d = ap_get_core_module_config(r->per_dir_config);
180
0
        if (d->log)
181
0
            r->log = d->log;
182
0
    }
183
184
0
    return OK;
185
0
}
186
187
/* This is the master logic for processing requests.  Do NOT duplicate
188
 * this logic elsewhere, or the security model will be broken by future
189
 * API changes.  Each phase must be individually optimized to pick up
190
 * redundant/duplicate calls by subrequests, and redirects.
191
 */
192
AP_DECLARE(int) ap_process_request_internal(request_rec *r)
193
0
{
194
0
    int access_status = DECLINED;
195
0
    int file_req = (r->main && r->filename);
196
0
    core_server_config *sconf =
197
0
        ap_get_core_module_config(r->server->module_config);
198
0
    unsigned int normalize_flags;
199
200
0
    normalize_flags = AP_NORMALIZE_NOT_ABOVE_ROOT;
201
0
    if (sconf->merge_slashes != AP_CORE_CONFIG_OFF) { 
202
0
        normalize_flags |= AP_NORMALIZE_MERGE_SLASHES;
203
0
    }
204
0
    if (file_req) {
205
        /* File subrequests can have a relative path. */
206
0
        normalize_flags |= AP_NORMALIZE_ALLOW_RELATIVE;
207
0
    }
208
209
0
    if (r->parsed_uri.path) {
210
        /* Normalize: remove /./ and shrink /../ segments, plus
211
         * decode unreserved chars (first time only to avoid
212
         * double decoding after ap_unescape_url() below).
213
         */
214
0
        if (!ap_normalize_path(r->parsed_uri.path,
215
0
                               normalize_flags |
216
0
                               AP_NORMALIZE_DECODE_UNRESERVED)) {
217
0
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(10244)
218
0
                          "invalid URI path (%s)", r->unparsed_uri);
219
0
            return HTTP_BAD_REQUEST;
220
0
        }
221
0
    }
222
223
    /* All file subrequests are a huge pain... they cannot bubble through the
224
     * next several steps.  Only file subrequests are allowed an empty uri,
225
     * otherwise let (pre_)translate_name kill the request.
226
     */
227
0
    if (!file_req) {
228
0
        ap_conf_vector_t *per_dir_config = r->per_dir_config;
229
230
0
        if ((access_status = walk_location_and_if(r))) {
231
0
            return access_status;
232
0
        }
233
234
        /* Let pre_translate_name hooks work with non-decoded URIs, and
235
         * eventually prevent further URI transformations (return DONE).
236
         */
237
0
        access_status = ap_run_pre_translate_name(r);
238
0
        if (ap_is_HTTP_ERROR(access_status)) {
239
0
            return access_status;
240
0
        }
241
242
        /* Throw away pre_trans only merging */
243
0
        r->per_dir_config = per_dir_config;
244
0
    }
245
246
    /* Ignore URL unescaping for translated URIs already */
247
0
    if (access_status != DONE && r->parsed_uri.path) {
248
0
        core_dir_config *d = ap_get_core_module_config(r->per_dir_config);
249
        /* Unreserved chars were already decoded by ap_normalize_path() */
250
0
        unsigned int unescape_flags = AP_UNESCAPE_URL_KEEP_UNRESERVED;
251
0
        if (!d->allow_encoded_slashes) {
252
0
            unescape_flags |= AP_UNESCAPE_URL_FORBID_SLASHES;
253
0
        }
254
0
        else if (!d->decode_encoded_slashes) {
255
0
            unescape_flags |= AP_UNESCAPE_URL_KEEP_SLASHES;
256
0
        }
257
0
        access_status = ap_unescape_url_ex(r->parsed_uri.path, unescape_flags);
258
0
        if (access_status) {
259
0
            if (access_status == HTTP_NOT_FOUND) {
260
0
                if (! d->allow_encoded_slashes) {
261
0
                    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00026)
262
0
                                  "found %%2f (encoded '/') in URI path (%s), "
263
0
                                  "returning 404", r->unparsed_uri);
264
0
                }
265
0
            }
266
0
            return access_status;
267
0
        }
268
269
0
        if (d->allow_encoded_slashes && d->decode_encoded_slashes) {
270
            /* Decoding slashes might have created new // or /./ or /../
271
             * segments (e.g. "/.%2F/"), so re-normalize.
272
             */
273
0
            ap_normalize_path(r->parsed_uri.path, normalize_flags);
274
0
        }
275
0
    }
276
277
    /* Same, translate_name is not suited for file subrequests */
278
0
    if (!file_req) {
279
0
        if ((access_status = walk_location_and_if(r))) {
280
0
            return access_status;
281
0
        }
282
283
0
        if ((access_status = ap_run_translate_name(r))) {
284
0
            return decl_die(access_status, "translate", r);
285
0
        }
286
0
    }
287
288
    /* Reset to the server default config prior to running map_to_storage
289
     */
290
0
    r->per_dir_config = r->server->lookup_defaults;
291
292
0
    if ((access_status = ap_run_map_to_storage(r))) {
293
        /* This request wasn't in storage (e.g. TRACE) */
294
0
        return access_status;
295
0
    }
296
297
    /* Rerun the location walk, which overrides any map_to_storage config.
298
     */
299
0
    if ((access_status = walk_location_and_if(r))) {
300
0
        return access_status;
301
0
    }
302
303
0
    if ((access_status = ap_run_post_perdir_config(r))) {
304
0
        return access_status;
305
0
    }
306
307
    /* Only on the main request! */
308
0
    if (r->main == NULL) {
309
0
        if ((access_status = ap_run_header_parser(r))) {
310
0
            return access_status;
311
0
        }
312
0
    }
313
314
    /* Skip authn/authz if the parent or prior request passed the authn/authz,
315
     * and that configuration didn't change (this requires optimized _walk()
316
     * functions in map_to_storage that use the same merge results given
317
     * identical input.)  If the config changes, we must re-auth.
318
     */
319
0
    if (r->prev && (r->prev->per_dir_config == r->per_dir_config)) {
320
0
        r->user = r->prev->user;
321
0
        r->ap_auth_type = r->prev->ap_auth_type;
322
0
    }
323
0
    else if (r->main && (r->main->per_dir_config == r->per_dir_config)) {
324
0
        r->user = r->main->user;
325
0
        r->ap_auth_type = r->main->ap_auth_type;
326
0
    }
327
0
    else {
328
        /* A module using a confusing API (ap_get_basic_auth_pw) caused
329
        ** r->user to be filled out prior to check_authn hook. We treat
330
        ** it is inadvertent.
331
        */
332
0
        if (r->user && apr_table_get(r->notes, AP_GET_BASIC_AUTH_PW_NOTE)) { 
333
0
            r->user = NULL;
334
0
        }
335
336
0
        switch (ap_satisfies(r)) {
337
0
        case SATISFY_ALL:
338
0
        case SATISFY_NOSPEC:
339
0
            if ((access_status = ap_run_token_checker(r)) != OK &&
340
0
                access_status != DECLINED) {
341
0
                return decl_die(access_status,
342
0
                                "check token (with Satisfy All)", r);
343
0
            }
344
345
0
            if ((access_status = ap_run_access_checker(r)) != OK) {
346
0
                return decl_die(access_status,
347
0
                                "check access (with Satisfy All)", r);
348
0
            }
349
350
0
            access_status = ap_run_access_checker_ex(r);
351
0
            if (access_status == DECLINED
352
0
                || (access_status == OK && ap_run_force_authn(r) == OK)) {
353
0
                if ((access_status = ap_run_check_user_id(r)) != OK) {
354
0
                    return decl_die(access_status, "check user", r);
355
0
                }
356
0
                if (r->user == NULL) {
357
                    /* don't let buggy authn module crash us in authz */
358
0
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00027)
359
0
                                  "No authentication done but request not "
360
0
                                  "allowed without authentication for %s. "
361
0
                                  "Authentication not configured?",
362
0
                                  r->uri);
363
0
                    access_status = HTTP_INTERNAL_SERVER_ERROR;
364
0
                    return decl_die(access_status, "check user", r);
365
0
                }
366
0
                if ((access_status = ap_run_auth_checker(r)) != OK) {
367
0
                    return decl_die(access_status, "check authorization", r);
368
0
                }
369
0
            }
370
0
            else if (access_status == OK) {
371
0
                ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
372
0
                              "request authorized without authentication by "
373
0
                              "access_checker_ex hook: %s", r->uri);
374
0
            }
375
0
            else {
376
0
                return decl_die(access_status, "check access", r);
377
0
            }
378
0
            break;
379
0
        case SATISFY_ANY:
380
0
            if ((access_status = ap_run_token_checker(r)) == OK) {
381
0
                ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
382
0
                              "request authorized bypassing access_checker by "
383
0
                              "token_checker hook and 'Satisfy any': %s",
384
0
                              r->uri);
385
0
                break;
386
0
            }
387
388
0
            if ((access_status = ap_run_access_checker(r)) == OK) {
389
0
                ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
390
0
                              "request authorized without authentication by "
391
0
                              "access_checker hook and 'Satisfy any': %s",
392
0
                              r->uri);
393
0
                break;
394
0
            }
395
396
0
            access_status = ap_run_access_checker_ex(r);
397
0
            if (access_status == DECLINED
398
0
                || (access_status == OK && ap_run_force_authn(r) == OK)) {
399
0
                if ((access_status = ap_run_check_user_id(r)) != OK) {
400
0
                    return decl_die(access_status, "check user", r);
401
0
                }
402
0
                if (r->user == NULL) {
403
                    /* don't let buggy authn module crash us in authz */
404
0
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00028)
405
0
                                  "No authentication done but request not "
406
0
                                  "allowed without authentication for %s. "
407
0
                                  "Authentication not configured?",
408
0
                                  r->uri);
409
0
                    access_status = HTTP_INTERNAL_SERVER_ERROR;
410
0
                    return decl_die(access_status, "check user", r);
411
0
                }
412
0
                if ((access_status = ap_run_auth_checker(r)) != OK) {
413
0
                    return decl_die(access_status, "check authorization", r);
414
0
                }
415
0
            }
416
0
            else if (access_status == OK) {
417
0
                ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
418
0
                              "request authorized without authentication by "
419
0
                              "access_checker_ex hook: %s", r->uri);
420
0
            }
421
0
            else {
422
0
                return decl_die(access_status, "check access", r);
423
0
            }
424
0
            break;
425
0
        }
426
0
    }
427
    /* XXX Must make certain the ap_run_type_checker short circuits mime
428
     * in mod-proxy for r->proxyreq && r->parsed_uri.scheme
429
     *                              && !strcmp(r->parsed_uri.scheme, "http")
430
     */
431
0
    if ((access_status = ap_run_type_checker(r)) != OK) {
432
0
        return decl_die(access_status, "find types", r);
433
0
    }
434
435
0
    if ((access_status = ap_run_fixups(r)) != OK) {
436
0
        ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r, "fixups hook gave %d: %s",
437
0
                      access_status, r->uri);
438
0
        return access_status;
439
0
    }
440
441
0
    return OK;
442
0
}
443
444
445
/* Useful caching structures to repeat _walk/merge sequences as required
446
 * when a subrequest or redirect reuses substantially the same config.
447
 *
448
 * Directive order in the httpd.conf file and its Includes significantly
449
 * impact this optimization.  Grouping common blocks at the front of the
450
 * config that are less likely to change between a request and
451
 * its subrequests, or between a request and its redirects reduced
452
 * the work of these functions significantly.
453
 */
454
455
typedef struct walk_walked_t {
456
    ap_conf_vector_t *matched; /* A dir_conf sections we matched */
457
    ap_conf_vector_t *merged;  /* The dir_conf merged result */
458
} walk_walked_t;
459
460
typedef struct walk_cache_t {
461
    const char         *cached;          /* The identifier we matched */
462
    ap_conf_vector_t  **dir_conf_tested; /* The sections we matched against */
463
    ap_conf_vector_t   *dir_conf_merged; /* Base per_dir_config */
464
    ap_conf_vector_t   *per_dir_result;  /* per_dir_config += walked result */
465
    apr_array_header_t *walked;          /* The list of walk_walked_t results */
466
    struct walk_cache_t *prev; /* Prev cache of same call in this (sub)req */
467
    int count; /* Number of prev invocations of same call in this (sub)req */
468
} walk_cache_t;
469
470
static walk_cache_t *prep_walk_cache(apr_size_t t, request_rec *r)
471
0
{
472
0
    void **note, **inherit_note;
473
0
    walk_cache_t *cache, *prev_cache, *copy_cache;
474
0
    int count;
475
476
    /* Find the most relevant, recent walk cache to work from and provide
477
     * a copy the caller is allowed to munge.  In the case of a sub-request
478
     * or internal redirect, this is the cache corresponding to the equivalent
479
     * invocation of the same function call in the "parent" request, if such
480
     * a cache exists.  Otherwise it is the walk cache of the previous
481
     * invocation of the same function call in the current request, if
482
     * that exists; if not, then create a new walk cache.
483
     */
484
0
    note = ap_get_request_note(r, t);
485
0
    AP_DEBUG_ASSERT(note != NULL);
486
487
0
    copy_cache = prev_cache = *note;
488
0
    count = prev_cache ? (prev_cache->count + 1) : 0;
489
490
0
    if ((r->prev
491
0
         && (inherit_note = ap_get_request_note(r->prev, t))
492
0
         && *inherit_note)
493
0
        || (r->main
494
0
            && (inherit_note = ap_get_request_note(r->main, t))
495
0
            && *inherit_note)) {
496
0
        walk_cache_t *inherit_cache = *inherit_note;
497
498
0
        while (inherit_cache->count > count) {
499
0
            inherit_cache = inherit_cache->prev;
500
0
        }
501
0
        if (inherit_cache->count == count) {
502
0
            copy_cache = inherit_cache;
503
0
        }
504
0
    }
505
506
0
    if (copy_cache) {
507
0
        cache = apr_pmemdup(r->pool, copy_cache, sizeof(*cache));
508
0
        cache->walked = apr_array_copy(r->pool, cache->walked);
509
0
        cache->prev = prev_cache;
510
0
        cache->count = count;
511
0
    }
512
0
    else {
513
0
        cache = apr_pcalloc(r->pool, sizeof(*cache));
514
0
        cache->walked = apr_array_make(r->pool, 4, sizeof(walk_walked_t));
515
0
    }
516
517
0
    *note = cache;
518
519
0
    return cache;
520
0
}
521
522
/*****************************************************************
523
 *
524
 * Getting and checking directory configuration.  Also checks the
525
 * FollowSymlinks and FollowSymOwner stuff, since this is really the
526
 * only place that can happen (barring a new mid_dir_walk callout).
527
 *
528
 * We can't do it as an access_checker module function which gets
529
 * called with the final per_dir_config, since we could have a directory
530
 * with FollowSymLinks disabled, which contains a symlink to another
531
 * with a .htaccess file which turns FollowSymLinks back on --- and
532
 * access in such a case must be denied.  So, whatever it is that
533
 * checks FollowSymLinks needs to know the state of the options as
534
 * they change, all the way down.
535
 */
536
537
538
/*
539
 * resolve_symlink must _always_ be called on an APR_LNK file type!
540
 * It will resolve the actual target file type, modification date, etc,
541
 * and provide any processing required for symlink evaluation.
542
 * Path must already be cleaned, no trailing slash, no multi-slashes,
543
 * and don't call this on the root!
544
 *
545
 * Simply, the number of times we deref a symlink are minimal compared
546
 * to the number of times we had an extra lstat() since we 'weren't sure'.
547
 *
548
 * To optimize, we stat() anything when given (opts & OPT_SYM_LINKS), otherwise
549
 * we start off with an lstat().  Every lstat() must be dereferenced in case
550
 * it points at a 'nasty' - we must always rerun check_safe_file (or similar.)
551
 */
552
static int resolve_symlink(char *d, apr_finfo_t *lfi, int opts, apr_pool_t *p)
553
0
{
554
0
    apr_finfo_t fi;
555
0
    const char *savename;
556
557
0
    if (!(opts & (OPT_SYM_OWNER | OPT_SYM_LINKS))) {
558
0
        return HTTP_FORBIDDEN;
559
0
    }
560
561
    /* Save the name from the valid bits. */
562
0
    savename = (lfi->valid & APR_FINFO_NAME) ? lfi->name : NULL;
563
564
    /* if OPT_SYM_OWNER is unset, we only need to check target accessible */
565
0
    if (!(opts & OPT_SYM_OWNER)) {
566
0
        if (apr_stat(&fi, d, lfi->valid & ~(APR_FINFO_NAME | APR_FINFO_LINK), p)
567
0
            != APR_SUCCESS)
568
0
        {
569
0
            return HTTP_FORBIDDEN;
570
0
        }
571
572
        /* Give back the target */
573
0
        memcpy(lfi, &fi, sizeof(fi));
574
0
        if (savename) {
575
0
            lfi->name = savename;
576
0
            lfi->valid |= APR_FINFO_NAME;
577
0
        }
578
579
0
        return OK;
580
0
    }
581
582
    /* OPT_SYM_OWNER only works if we can get the owner of
583
     * both the file and symlink.  First fill in a missing
584
     * owner of the symlink, then get the info of the target.
585
     */
586
0
    if (!(lfi->valid & APR_FINFO_OWNER)) {
587
0
        if (apr_stat(lfi, d, lfi->valid | APR_FINFO_LINK | APR_FINFO_OWNER, p)
588
0
            != APR_SUCCESS)
589
0
        {
590
0
            return HTTP_FORBIDDEN;
591
0
        }
592
0
    }
593
594
0
    if (apr_stat(&fi, d, lfi->valid & ~(APR_FINFO_NAME), p) != APR_SUCCESS) {
595
0
        return HTTP_FORBIDDEN;
596
0
    }
597
598
0
    if (apr_uid_compare(fi.user, lfi->user) != APR_SUCCESS) {
599
0
        return HTTP_FORBIDDEN;
600
0
    }
601
602
    /* Give back the target */
603
0
    memcpy(lfi, &fi, sizeof(fi));
604
0
    if (savename) {
605
0
        lfi->name = savename;
606
0
        lfi->valid |= APR_FINFO_NAME;
607
0
    }
608
609
0
    return OK;
610
0
}
611
612
613
/*
614
 * As we walk the directory configuration, the merged config won't
615
 * be 'rooted' to a specific vhost until the very end of the merge.
616
 *
617
 * We need a very fast mini-merge to a real, vhost-rooted merge
618
 * of core.opts and core.override, the only options tested within
619
 * directory_walk itself.
620
 *
621
 * See core.c::merge_core_dir_configs() for explanation.
622
 */
623
624
typedef struct core_opts_t {
625
        allow_options_t opts;
626
        allow_options_t add;
627
        allow_options_t remove;
628
        overrides_t override;
629
        overrides_t override_opts;
630
        apr_table_t *override_list;
631
} core_opts_t;
632
633
static void core_opts_merge(const ap_conf_vector_t *sec, core_opts_t *opts)
634
0
{
635
0
    core_dir_config *this_dir = ap_get_core_module_config(sec);
636
637
0
    if (!this_dir) {
638
0
        return;
639
0
    }
640
641
0
    if (this_dir->opts & OPT_UNSET) {
642
0
        opts->add = (opts->add & ~this_dir->opts_remove)
643
0
                   | this_dir->opts_add;
644
0
        opts->remove = (opts->remove & ~this_dir->opts_add)
645
0
                      | this_dir->opts_remove;
646
0
        opts->opts = (opts->opts & ~opts->remove) | opts->add;
647
0
    }
648
0
    else {
649
0
        opts->opts = this_dir->opts;
650
0
        opts->add = this_dir->opts_add;
651
0
        opts->remove = this_dir->opts_remove;
652
0
    }
653
654
0
    if (!(this_dir->override & OR_UNSET)) {
655
0
        opts->override = this_dir->override;
656
0
        opts->override_opts = this_dir->override_opts;
657
0
    }
658
659
0
    if (this_dir->override_list != NULL) {
660
0
        opts->override_list = this_dir->override_list;
661
0
    }
662
0
}
663
664
665
/*****************************************************************
666
 *
667
 * Getting and checking directory configuration.  Also checks the
668
 * FollowSymlinks and FollowSymOwner stuff, since this is really the
669
 * only place that can happen (barring a new mid_dir_walk callout).
670
 *
671
 * We can't do it as an access_checker module function which gets
672
 * called with the final per_dir_config, since we could have a directory
673
 * with FollowSymLinks disabled, which contains a symlink to another
674
 * with a .htaccess file which turns FollowSymLinks back on --- and
675
 * access in such a case must be denied.  So, whatever it is that
676
 * checks FollowSymLinks needs to know the state of the options as
677
 * they change, all the way down.
678
 */
679
680
AP_DECLARE(int) ap_directory_walk(request_rec *r)
681
0
{
682
0
    ap_conf_vector_t *now_merged = NULL;
683
0
    core_server_config *sconf =
684
0
        ap_get_core_module_config(r->server->module_config);
685
0
    ap_conf_vector_t **sec_ent = (ap_conf_vector_t **) sconf->sec_dir->elts;
686
0
    int num_sec = sconf->sec_dir->nelts;
687
0
    walk_cache_t *cache;
688
0
    char *entry_dir;
689
0
    apr_status_t rv;
690
0
    int cached;
691
692
    /* XXX: Better (faster) tests needed!!!
693
     *
694
     * "OK" as a response to a real problem is not _OK_, but to allow broken
695
     * modules to proceed, we will permit the not-a-path filename to pass the
696
     * following two tests.  This behavior may be revoked in future versions
697
     * of Apache.  We still must catch it later if it's heading for the core
698
     * handler.  Leave INFO notes here for module debugging.
699
     */
700
0
    if (r->filename == NULL) {
701
0
        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00029)
702
0
                      "Module bug?  Request filename is missing for URI %s",
703
0
                      r->uri);
704
0
        return OK;
705
0
    }
706
707
    /* Canonicalize the file path without resolving filename case or aliases
708
     * so we can begin by checking the cache for a recent directory walk.
709
     * This call will ensure we have an absolute path in the same pass.
710
     */
711
0
    if ((rv = apr_filepath_merge(&entry_dir, NULL, r->filename,
712
0
                                 APR_FILEPATH_NOTRELATIVE, r->pool))
713
0
                  != APR_SUCCESS) {
714
0
        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00030)
715
0
                      "Module bug?  Request filename path %s is invalid or "
716
0
                      "or not absolute for uri %s",
717
0
                      r->filename, r->uri);
718
0
        return OK;
719
0
    }
720
721
    /* XXX Notice that this forces path_info to be canonical.  That might
722
     * not be desired by all apps.  However, some of those same apps likely
723
     * have significant security holes.
724
     */
725
0
    r->filename = entry_dir;
726
727
0
    cache = prep_walk_cache(AP_NOTE_DIRECTORY_WALK, r);
728
0
    cached = (cache->cached != NULL);
729
730
    /* If this is not a dirent subrequest with a preconstructed
731
     * r->finfo value, then we can simply stat the filename to
732
     * save burning mega-cycles with unneeded stats - if this is
733
     * an exact file match.  We don't care about failure... we
734
     * will stat by component failing this meager attempt.
735
     *
736
     * It would be nice to distinguish APR_ENOENT from other
737
     * types of failure, such as APR_ENOTDIR.  We can do something
738
     * with APR_ENOENT, knowing that the path is good.
739
     */
740
0
    if (r->finfo.filetype == APR_NOFILE || r->finfo.filetype == APR_LNK) {
741
0
        rv = ap_run_dirwalk_stat(&r->finfo, r, APR_FINFO_MIN);
742
743
        /* some OSs will return APR_SUCCESS/APR_REG if we stat
744
         * a regular file but we have '/' at the end of the name;
745
         *
746
         * other OSs will return APR_ENOTDIR for that situation;
747
         *
748
         * handle it the same everywhere by simulating a failure
749
         * if it looks like a directory but really isn't
750
         *
751
         * Also reset if the stat failed, just for safety.
752
         */
753
0
        if ((rv != APR_SUCCESS) ||
754
0
            (r->finfo.filetype != APR_NOFILE &&
755
0
             (r->finfo.filetype != APR_DIR) &&
756
0
             (r->filename[strlen(r->filename) - 1] == '/'))) {
757
0
             r->finfo.filetype = APR_NOFILE; /* forget what we learned */
758
0
        }
759
0
    }
760
761
0
    if (r->finfo.filetype == APR_REG) {
762
0
        entry_dir = ap_make_dirstr_parent(r->pool, entry_dir);
763
0
    }
764
0
    else if (r->filename[strlen(r->filename) - 1] != '/') {
765
0
        entry_dir = apr_pstrcat(r->pool, r->filename, "/", NULL);
766
0
    }
767
768
    /* If we have a file already matches the path of r->filename,
769
     * and the vhost's list of directory sections hasn't changed,
770
     * we can skip rewalking the directory_walk entries.
771
     */
772
0
    if (cached
773
0
        && ((r->finfo.filetype == APR_REG)
774
0
            || ((r->finfo.filetype == APR_DIR)
775
0
                && (!r->path_info || !*r->path_info)))
776
0
        && (cache->dir_conf_tested == sec_ent)
777
0
        && (strcmp(entry_dir, cache->cached) == 0)) {
778
0
        int familiar = 0;
779
780
        /* Well this looks really familiar!  If our end-result (per_dir_result)
781
         * didn't change, we have absolutely nothing to do :)
782
         * Otherwise (as is the case with most dir_merged/file_merged requests)
783
         * we must merge our dir_conf_merged onto this new r->per_dir_config.
784
         */
785
0
        if (r->per_dir_config == cache->per_dir_result) {
786
0
            familiar = 1;
787
0
        }
788
789
0
        if (r->per_dir_config == cache->dir_conf_merged) {
790
0
            r->per_dir_config = cache->per_dir_result;
791
0
            familiar = 1;
792
0
        }
793
794
0
        if (familiar) {
795
0
            apr_finfo_t thisinfo;
796
0
            int res;
797
0
            allow_options_t opts;
798
0
            core_dir_config *this_dir;
799
800
0
            this_dir = ap_get_core_module_config(r->per_dir_config);
801
0
            opts = this_dir->opts;
802
            /*
803
             * If Symlinks are allowed in general we do not need the following
804
             * check.
805
             */
806
0
            if (!(opts & OPT_SYM_LINKS)) {
807
0
                rv = ap_run_dirwalk_stat(&thisinfo, r,
808
0
                                         APR_FINFO_MIN | APR_FINFO_NAME | APR_FINFO_LINK);
809
                /*
810
                 * APR_INCOMPLETE is as fine as result as APR_SUCCESS as we
811
                 * have added APR_FINFO_NAME to the wanted parameter of
812
                 * apr_stat above. On Unix platforms this means that apr_stat
813
                 * is always going to return APR_INCOMPLETE in the case that
814
                 * the call to the native stat / lstat did not fail.
815
                 */
816
0
                if ((rv != APR_INCOMPLETE) && (rv != APR_SUCCESS)) {
817
                    /*
818
                     * This should never happen, because we did a stat on the
819
                     * same file, resolving a possible symlink several lines
820
                     * above. Therefore do not make a detailed analysis of rv
821
                     * in this case for the reason of the failure, just bail out
822
                     * with a HTTP_FORBIDDEN in case we hit a race condition
823
                     * here.
824
                     */
825
0
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00031)
826
0
                                  "access to %s failed; stat of '%s' failed.",
827
0
                                  r->uri, r->filename);
828
0
                    return r->status = HTTP_FORBIDDEN;
829
0
                }
830
0
                if (thisinfo.filetype == APR_LNK) {
831
                    /* Is this a possibly acceptable symlink? */
832
0
                    if ((res = resolve_symlink(r->filename, &thisinfo,
833
0
                                               opts, r->pool)) != OK) {
834
0
                        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00032)
835
0
                                      "Symbolic link not allowed "
836
0
                                      "or link target not accessible: %s",
837
0
                                      r->filename);
838
0
                        return r->status = res;
839
0
                    }
840
0
                }
841
0
            }
842
0
            return OK;
843
0
        }
844
845
0
        if (cache->walked->nelts) {
846
0
            now_merged = ((walk_walked_t*)cache->walked->elts)
847
0
                [cache->walked->nelts - 1].merged;
848
0
        }
849
0
    }
850
0
    else {
851
        /* We start now_merged from NULL since we want to build
852
         * a locations list that can be merged to any vhost.
853
         */
854
0
        int sec_idx;
855
0
        int matches = cache->walked->nelts;
856
0
        int cached_matches = matches;
857
0
        walk_walked_t *last_walk = (walk_walked_t*)cache->walked->elts;
858
0
        core_dir_config *this_dir;
859
0
        core_opts_t opts;
860
0
        apr_finfo_t thisinfo;
861
0
        char *save_path_info;
862
0
        apr_size_t buflen;
863
0
        char *buf;
864
0
        unsigned int seg, startseg;
865
0
        apr_pool_t *rxpool = NULL;
866
867
        /* Invariant: from the first time filename_len is set until
868
         * it goes out of scope, filename_len==strlen(r->filename)
869
         */
870
0
        apr_size_t filename_len;
871
#ifdef CASE_BLIND_FILESYSTEM
872
        apr_size_t canonical_len;
873
#endif
874
875
0
        cached &= auth_internal_per_conf;
876
877
        /*
878
         * We must play our own mini-merge game here, for the few
879
         * running dir_config values we care about within dir_walk.
880
         * We didn't start the merge from r->per_dir_config, so we
881
         * accumulate opts and override as we merge, from the globals.
882
         */
883
0
        this_dir = ap_get_core_module_config(r->per_dir_config);
884
0
        opts.opts = this_dir->opts;
885
0
        opts.add = this_dir->opts_add;
886
0
        opts.remove = this_dir->opts_remove;
887
0
        opts.override = this_dir->override;
888
0
        opts.override_opts = this_dir->override_opts;
889
0
        opts.override_list = this_dir->override_list;
890
891
        /* Set aside path_info to merge back onto path_info later.
892
         * If r->filename is a directory, we must remerge the path_info,
893
         * before we continue!  [Directories cannot, by definition, have
894
         * path info.  Either the next segment is not-found, or a file.]
895
         *
896
         * r->path_info tracks the unconsumed source path.
897
         * r->filename  tracks the path as we process it
898
         */
899
0
        if ((r->finfo.filetype == APR_DIR) && r->path_info && *r->path_info)
900
0
        {
901
0
            if ((rv = apr_filepath_merge(&r->path_info, r->filename,
902
0
                                         r->path_info,
903
0
                                         APR_FILEPATH_NOTABOVEROOT, r->pool))
904
0
                != APR_SUCCESS) {
905
0
                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00033)
906
0
                              "dir_walk error, path_info %s is not relative "
907
0
                              "to the filename path %s for uri %s",
908
0
                              r->path_info, r->filename, r->uri);
909
0
                return HTTP_INTERNAL_SERVER_ERROR;
910
0
            }
911
912
0
            save_path_info = NULL;
913
0
        }
914
0
        else {
915
0
            save_path_info = r->path_info;
916
0
            r->path_info = r->filename;
917
0
        }
918
919
#ifdef CASE_BLIND_FILESYSTEM
920
921
        canonical_len = 0;
922
        while (r->canonical_filename && r->canonical_filename[canonical_len]
923
               && (r->canonical_filename[canonical_len]
924
                   == r->path_info[canonical_len])) {
925
             ++canonical_len;
926
        }
927
928
        while (canonical_len
929
               && ((r->canonical_filename[canonical_len - 1] != '/'
930
                   && r->canonical_filename[canonical_len - 1])
931
                   || (r->path_info[canonical_len - 1] != '/'
932
                       && r->path_info[canonical_len - 1]))) {
933
            --canonical_len;
934
        }
935
936
        /*
937
         * Now build r->filename component by component, starting
938
         * with the root (on Unix, simply "/").  We will make a huge
939
         * assumption here for efficiency, that any canonical path
940
         * already given included a canonical root.
941
         */
942
        rv = apr_filepath_root((const char **)&r->filename,
943
                               (const char **)&r->path_info,
944
                               canonical_len ? 0 : APR_FILEPATH_TRUENAME,
945
                               r->pool);
946
        filename_len = strlen(r->filename);
947
948
        /*
949
         * Bad assumption above?  If the root's length is longer
950
         * than the canonical length, then it cannot be trusted as
951
         * a truename.  So try again, this time more seriously.
952
         */
953
        if ((rv == APR_SUCCESS) && canonical_len
954
            && (filename_len > canonical_len)) {
955
            rv = apr_filepath_root((const char **)&r->filename,
956
                                   (const char **)&r->path_info,
957
                                   APR_FILEPATH_TRUENAME, r->pool);
958
            filename_len = strlen(r->filename);
959
            canonical_len = 0;
960
        }
961
962
#else /* ndef CASE_BLIND_FILESYSTEM, really this simple for Unix today; */
963
964
0
        rv = apr_filepath_root((const char **)&r->filename,
965
0
                               (const char **)&r->path_info,
966
0
                               0, r->pool);
967
0
        filename_len = strlen(r->filename);
968
969
0
#endif
970
971
0
        if (rv != APR_SUCCESS) {
972
0
            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00034)
973
0
                          "dir_walk error, could not determine the root "
974
0
                          "path of filename %s%s for uri %s",
975
0
                          r->filename, r->path_info, r->uri);
976
0
            return HTTP_INTERNAL_SERVER_ERROR;
977
0
        }
978
979
        /* Working space for terminating null and an extra / is required.
980
         */
981
0
        buflen = filename_len + strlen(r->path_info) + 2;
982
0
        buf = apr_palloc(r->pool, buflen);
983
0
        memcpy(buf, r->filename, filename_len + 1);
984
0
        r->filename = buf;
985
0
        thisinfo.valid = APR_FINFO_TYPE;
986
0
        thisinfo.filetype = APR_DIR; /* It's the root, of course it's a dir */
987
988
        /*
989
         * seg keeps track of which segment we've copied.
990
         * sec_idx keeps track of which section we're on, since sections are
991
         *     ordered by number of segments. See core_reorder_directories
992
         * startseg tells us how many segments describe the root path
993
         *     e.g. the complete path "//host/foo/" to a UNC share (4)
994
         */
995
0
        startseg = seg = ap_count_dirs(r->filename);
996
0
        sec_idx = 0;
997
998
        /*
999
         * Go down the directory hierarchy.  Where we have to check for
1000
         * symlinks, do so.  Where a .htaccess file has permission to
1001
         * override anything, try to find one.
1002
         */
1003
0
        do {
1004
0
            int res;
1005
0
            char *seg_name;
1006
0
            char *delim;
1007
0
            int temp_slash=0;
1008
1009
            /* We have no trailing slash, but we sure would appreciate one.
1010
             * However, we don't want to append a / our first time through.
1011
             */
1012
0
            if ((seg > startseg) && r->filename[filename_len-1] != '/') {
1013
0
                r->filename[filename_len++] = '/';
1014
0
                r->filename[filename_len] = 0;
1015
0
                temp_slash=1;
1016
0
            }
1017
1018
            /* Begin *this* level by looking for matching <Directory> sections
1019
             * from the server config.
1020
             */
1021
0
            for (; sec_idx < num_sec; ++sec_idx) {
1022
1023
0
                ap_conf_vector_t *entry_config = sec_ent[sec_idx];
1024
0
                core_dir_config *entry_core;
1025
0
                entry_core = ap_get_core_module_config(entry_config);
1026
1027
                /* No more possible matches for this many segments?
1028
                 * We are done when we find relative/regex/longer components.
1029
                 */
1030
0
                if (entry_core->r || entry_core->d_components > seg) {
1031
0
                    break;
1032
0
                }
1033
1034
                /* We will never skip '0' element components, e.g. plain old
1035
                 * <Directory >, and <Directory "/"> are classified as zero
1036
                 * so that Win32/Netware/OS2 etc all pick them up.
1037
                 * Otherwise, skip over the mismatches.
1038
                 */
1039
0
                if (entry_core->d_components
1040
0
                    && ((entry_core->d_components < seg)
1041
0
                     || (entry_core->d_is_fnmatch
1042
0
                         ? (apr_fnmatch(entry_core->d, r->filename,
1043
0
                                        APR_FNM_PATHNAME) != APR_SUCCESS)
1044
0
                         : (strcmp(r->filename, entry_core->d) != 0)))) {
1045
0
                    continue;
1046
0
                }
1047
1048
                /* If we haven't continue'd above, we have a match.
1049
                 *
1050
                 * Calculate our full-context core opts & override.
1051
                 */
1052
0
                core_opts_merge(sec_ent[sec_idx], &opts);
1053
1054
                /* If we merged this same section last time, reuse it
1055
                 */
1056
0
                if (matches) {
1057
0
                    if (last_walk->matched == sec_ent[sec_idx]) {
1058
0
                        now_merged = last_walk->merged;
1059
0
                        ++last_walk;
1060
0
                        --matches;
1061
0
                        continue;
1062
0
                    }
1063
1064
                    /* We fell out of sync.  This is our own copy of walked,
1065
                     * so truncate the remaining matches and reset remaining.
1066
                     */
1067
0
                    cache->walked->nelts -= matches;
1068
0
                    matches = 0;
1069
0
                    cached = 0;
1070
0
                }
1071
1072
0
                if (now_merged) {
1073
0
                    now_merged = ap_merge_per_dir_configs(r->pool,
1074
0
                                                          now_merged,
1075
0
                                                          sec_ent[sec_idx]);
1076
0
                }
1077
0
                else {
1078
0
                    now_merged = sec_ent[sec_idx];
1079
0
                }
1080
1081
0
                last_walk = (walk_walked_t*)apr_array_push(cache->walked);
1082
0
                last_walk->matched = sec_ent[sec_idx];
1083
0
                last_walk->merged = now_merged;
1084
0
            }
1085
1086
            /* If .htaccess files are enabled, check for one, provided we
1087
             * have reached a real path.
1088
             */
1089
0
            do {  /* Not really a loop, just a break'able code block */
1090
1091
0
                ap_conf_vector_t *htaccess_conf = NULL;
1092
1093
                /* No htaccess in an incomplete root path,
1094
                 * nor if it's disabled
1095
                 */
1096
0
                if (seg < startseg || (!opts.override 
1097
0
                    && apr_is_empty_table(opts.override_list)
1098
0
                    )) {
1099
0
                    break;
1100
0
                }
1101
1102
1103
0
                res = ap_parse_htaccess(&htaccess_conf, r, opts.override,
1104
0
                                        opts.override_opts, opts.override_list,
1105
0
                                        r->filename, sconf->access_name);
1106
0
                if (res) {
1107
0
                    return res;
1108
0
                }
1109
1110
0
                if (!htaccess_conf) {
1111
0
                    break;
1112
0
                }
1113
1114
                /* If we are still here, we found our htaccess.
1115
                 *
1116
                 * Calculate our full-context core opts & override.
1117
                 */
1118
0
                core_opts_merge(htaccess_conf, &opts);
1119
1120
                /* If we merged this same htaccess last time, reuse it...
1121
                 * this wouldn't work except that we cache the htaccess
1122
                 * sections for the lifetime of the request, so we match
1123
                 * the same conf.  Good planning (no, pure luck ;)
1124
                 */
1125
0
                if (matches) {
1126
0
                    if (last_walk->matched == htaccess_conf) {
1127
0
                        now_merged = last_walk->merged;
1128
0
                        ++last_walk;
1129
0
                        --matches;
1130
0
                        break;
1131
0
                    }
1132
1133
                    /* We fell out of sync.  This is our own copy of walked,
1134
                     * so truncate the remaining matches and reset
1135
                     * remaining.
1136
                     */
1137
0
                    cache->walked->nelts -= matches;
1138
0
                    matches = 0;
1139
0
                    cached = 0;
1140
0
                }
1141
1142
0
                if (now_merged) {
1143
0
                    now_merged = ap_merge_per_dir_configs(r->pool,
1144
0
                                                          now_merged,
1145
0
                                                          htaccess_conf);
1146
0
                }
1147
0
                else {
1148
0
                    now_merged = htaccess_conf;
1149
0
                }
1150
1151
0
                last_walk = (walk_walked_t*)apr_array_push(cache->walked);
1152
0
                last_walk->matched = htaccess_conf;
1153
0
                last_walk->merged = now_merged;
1154
1155
0
            } while (0); /* Only one htaccess, not a real loop */
1156
1157
            /* That temporary trailing slash was useful, now drop it.
1158
             */
1159
0
            if (temp_slash) {
1160
0
                r->filename[--filename_len] = '\0';
1161
0
            }
1162
1163
            /* Time for all good things to come to an end?
1164
             */
1165
0
            if (!r->path_info || !*r->path_info) {
1166
0
                break;
1167
0
            }
1168
1169
            /* Now it's time for the next segment...
1170
             * We will assume the next element is an end node, and fix it up
1171
             * below as necessary...
1172
             */
1173
1174
0
            seg_name = r->filename + filename_len;
1175
0
            delim = strchr(r->path_info + (*r->path_info == '/' ? 1 : 0), '/');
1176
0
            if (delim) {
1177
0
                apr_size_t path_info_len = delim - r->path_info;
1178
0
                *delim = '\0';
1179
0
                memcpy(seg_name, r->path_info, path_info_len + 1);
1180
0
                filename_len += path_info_len;
1181
0
                r->path_info = delim;
1182
0
                *delim = '/';
1183
0
            }
1184
0
            else {
1185
0
                apr_size_t path_info_len = strlen(r->path_info);
1186
0
                memcpy(seg_name, r->path_info, path_info_len + 1);
1187
0
                filename_len += path_info_len;
1188
0
                r->path_info += path_info_len;
1189
0
            }
1190
0
            if (*seg_name == '/')
1191
0
                ++seg_name;
1192
1193
            /* If nothing remained but a '/' string, we are finished
1194
             * XXX: NO WE ARE NOT!!!  Now process this puppy!!! */
1195
0
            if (!*seg_name) {
1196
0
                break;
1197
0
            }
1198
1199
            /* First optimization;
1200
             * If...we knew r->filename was a file, and
1201
             * if...we have strict (case-sensitive) filenames, or
1202
             *      we know the canonical_filename matches to _this_ name, and
1203
             * if...we have allowed symlinks
1204
             * skip the lstat and dummy up an APR_DIR value for thisinfo.
1205
             */
1206
0
            if (r->finfo.filetype != APR_NOFILE
1207
#ifdef CASE_BLIND_FILESYSTEM
1208
                && (filename_len <= canonical_len)
1209
#endif
1210
0
                && ((opts.opts & (OPT_SYM_OWNER | OPT_SYM_LINKS)) == OPT_SYM_LINKS))
1211
0
            {
1212
1213
0
                thisinfo.filetype = APR_DIR;
1214
0
                ++seg;
1215
0
                continue;
1216
0
            }
1217
1218
            /* We choose apr_stat with flag APR_FINFO_LINK here, rather that
1219
             * plain apr_stat, so that we capture this path object rather than
1220
             * its target.  We will replace the info with our target's info
1221
             * below.  We especially want the name of this 'link' object, not
1222
             * the name of its target, if we are fixing the filename
1223
             * case/resolving aliases.
1224
             */
1225
0
            rv = ap_run_dirwalk_stat(&thisinfo, r,
1226
0
                                     APR_FINFO_MIN | APR_FINFO_NAME | APR_FINFO_LINK);
1227
1228
0
            if (APR_STATUS_IS_ENOENT(rv)) {
1229
                /* Nothing?  That could be nice.  But our directory
1230
                 * walk is done.
1231
                 */
1232
0
                thisinfo.filetype = APR_NOFILE;
1233
0
                break;
1234
0
            }
1235
0
            else if (APR_STATUS_IS_EACCES(rv)) {
1236
0
                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00035)
1237
0
                              "access to %s denied (filesystem path '%s') "
1238
0
                              "because search permissions are missing on a "
1239
0
                              "component of the path", r->uri, r->filename);
1240
0
                return r->status = HTTP_FORBIDDEN;
1241
0
            }
1242
0
            else if ((rv != APR_SUCCESS && rv != APR_INCOMPLETE)
1243
0
                     || !(thisinfo.valid & APR_FINFO_TYPE)) {
1244
                /* If we hit ENOTDIR, we must have over-optimized, deny
1245
                 * rather than assume not found.
1246
                 */
1247
0
                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00036)
1248
0
                              "access to %s failed (filesystem path '%s')", 
1249
0
                              r->uri, r->filename);
1250
0
                return r->status = HTTP_FORBIDDEN;
1251
0
            }
1252
1253
            /* Fix up the path now if we have a name, and they don't agree
1254
             */
1255
0
            if ((thisinfo.valid & APR_FINFO_NAME)
1256
0
                && strcmp(seg_name, thisinfo.name)) {
1257
                /* TODO: provide users an option that an internal/external
1258
                 * redirect is required here?  We need to walk the URI and
1259
                 * filename in tandem to properly correlate these.
1260
                 */
1261
0
                apr_size_t seg_len  = strlen(seg_name);
1262
0
                apr_size_t real_len = strlen(thisinfo.name);
1263
0
                if (real_len > seg_len) {
1264
                    /* The canonical name is longer than the copied segment
1265
                     * (e.g. 8.3 alias -> long name on a case-blind FS).
1266
                     * The current buffer was sized from the original path and
1267
                     * does not have room; allocate a replacement that does.
1268
                     */
1269
0
                    apr_size_t prefix_len = seg_name - r->filename;
1270
0
                    apr_size_t new_buflen = prefix_len + real_len
1271
0
                                           + strlen(r->path_info) + 2;
1272
0
                    char *new_buf = apr_palloc(r->pool, new_buflen);
1273
0
                    memcpy(new_buf, r->filename, prefix_len);
1274
0
                    seg_name = new_buf + prefix_len;
1275
0
                    r->filename = new_buf;
1276
0
                }
1277
0
                memcpy(seg_name, thisinfo.name, real_len + 1);
1278
0
                filename_len = strlen(r->filename);
1279
0
            }
1280
1281
0
            if (thisinfo.filetype == APR_LNK) {
1282
                /* Is this a possibly acceptable symlink?
1283
                 */
1284
0
                if ((res = resolve_symlink(r->filename, &thisinfo,
1285
0
                                           opts.opts, r->pool)) != OK) {
1286
0
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00037)
1287
0
                                  "Symbolic link not allowed "
1288
0
                                  "or link target not accessible: %s",
1289
0
                                  r->filename);
1290
0
                    return r->status = res;
1291
0
                }
1292
0
            }
1293
1294
            /* Ok, we are done with the link's info, test the real target
1295
             */
1296
0
            if (thisinfo.filetype == APR_REG ||
1297
0
                thisinfo.filetype == APR_NOFILE) {
1298
                /* That was fun, nothing left for us here
1299
                 */
1300
0
                break;
1301
0
            }
1302
0
            else if (thisinfo.filetype != APR_DIR) {
1303
0
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00038)
1304
0
                              "Forbidden: %s doesn't point to "
1305
0
                              "a file or directory",
1306
0
                              r->filename);
1307
0
                return r->status = HTTP_FORBIDDEN;
1308
0
            }
1309
1310
0
            ++seg;
1311
0
        } while (thisinfo.filetype == APR_DIR);
1312
1313
        /* If we have _not_ optimized, this is the time to recover
1314
         * the final stat result.
1315
         */
1316
0
        if (r->finfo.filetype == APR_NOFILE || r->finfo.filetype == APR_LNK) {
1317
0
            r->finfo = thisinfo;
1318
0
        }
1319
1320
        /* Now splice the saved path_info back onto any new path_info
1321
         */
1322
0
        if (save_path_info) {
1323
0
            if (r->path_info && *r->path_info) {
1324
0
                r->path_info = ap_make_full_path(r->pool, r->path_info,
1325
0
                                                 save_path_info);
1326
0
            }
1327
0
            else {
1328
0
                r->path_info = save_path_info;
1329
0
            }
1330
0
        }
1331
1332
        /*
1333
         * Now we'll deal with the regexes, note we pick up sec_idx
1334
         * where we left off (we gave up after we hit entry_core->r)
1335
         */
1336
0
        for (; sec_idx < num_sec; ++sec_idx) {
1337
1338
0
            int nmatch = 0;
1339
0
            int i;
1340
0
            ap_regmatch_t *pmatch = NULL;
1341
1342
0
            core_dir_config *entry_core;
1343
0
            entry_core = ap_get_core_module_config(sec_ent[sec_idx]);
1344
1345
0
            if (!entry_core->r) {
1346
0
                continue;
1347
0
            }
1348
1349
0
            if (entry_core->refs && entry_core->refs->nelts) {
1350
0
                if (!rxpool) {
1351
0
                    apr_pool_create(&rxpool, r->pool);
1352
0
                    apr_pool_tag(rxpool, "directory_walk_rxpool");
1353
0
                }
1354
0
                nmatch = entry_core->refs->nelts;
1355
0
                pmatch = apr_palloc(rxpool, nmatch*sizeof(ap_regmatch_t));
1356
0
            }
1357
1358
0
            if (ap_regexec(entry_core->r, r->filename, nmatch, pmatch, 0)) {
1359
0
                continue;
1360
0
            }
1361
1362
0
            for (i = 0; i < nmatch; i++) {
1363
0
                if (pmatch[i].rm_so >= 0 && pmatch[i].rm_eo >= 0 &&
1364
0
                    ((const char **)entry_core->refs->elts)[i]) {
1365
0
                    apr_table_setn(r->subprocess_env, 
1366
0
                                   ((const char **)entry_core->refs->elts)[i],
1367
0
                                   apr_pstrndup(r->pool,
1368
0
                                   r->filename + pmatch[i].rm_so,
1369
0
                                   pmatch[i].rm_eo - pmatch[i].rm_so));
1370
0
                }
1371
0
            }
1372
1373
            /* If we haven't already continue'd above, we have a match.
1374
             *
1375
             * Calculate our full-context core opts & override.
1376
             */
1377
0
            core_opts_merge(sec_ent[sec_idx], &opts);
1378
1379
            /* If we merged this same section last time, reuse it
1380
             */
1381
0
            if (matches) {
1382
0
                if (last_walk->matched == sec_ent[sec_idx]) {
1383
0
                    now_merged = last_walk->merged;
1384
0
                    ++last_walk;
1385
0
                    --matches;
1386
0
                    continue;
1387
0
                }
1388
1389
                /* We fell out of sync.  This is our own copy of walked,
1390
                 * so truncate the remaining matches and reset remaining.
1391
                 */
1392
0
                cache->walked->nelts -= matches;
1393
0
                matches = 0;
1394
0
                cached = 0;
1395
0
            }
1396
1397
0
            if (now_merged) {
1398
0
                now_merged = ap_merge_per_dir_configs(r->pool,
1399
0
                                                      now_merged,
1400
0
                                                      sec_ent[sec_idx]);
1401
0
            }
1402
0
            else {
1403
0
                now_merged = sec_ent[sec_idx];
1404
0
            }
1405
1406
0
            last_walk = (walk_walked_t*)apr_array_push(cache->walked);
1407
0
            last_walk->matched = sec_ent[sec_idx];
1408
0
            last_walk->merged = now_merged;
1409
0
        }
1410
1411
0
        if (rxpool) {
1412
0
            apr_pool_destroy(rxpool);
1413
0
        }
1414
1415
        /* Whoops - everything matched in sequence, but either the original
1416
         * walk found some additional matches (which we need to truncate), or
1417
         * this walk found some additional matches.
1418
         */
1419
0
        if (matches) {
1420
0
            cache->walked->nelts -= matches;
1421
0
            cached = 0;
1422
0
        }
1423
0
        else if (cache->walked->nelts > cached_matches) {
1424
0
            cached = 0;
1425
0
        }
1426
0
    }
1427
1428
/* It seems this shouldn't be needed anymore.  We translated the
1429
 x symlink above into a real resource, and should have died up there.
1430
 x Even if we keep this, it needs more thought (maybe an r->file_is_symlink)
1431
 x perhaps it should actually happen in file_walk, so we catch more
1432
 x obscure cases in autoindex subrequests, etc.
1433
 x
1434
 x    * Symlink permissions are determined by the parent.  If the request is
1435
 x    * for a directory then applying the symlink test here would use the
1436
 x    * permissions of the directory as opposed to its parent.  Consider a
1437
 x    * symlink pointing to a dir with a .htaccess disallowing symlinks.  If
1438
 x    * you access /symlink (or /symlink/) you would get a 403 without this
1439
 x    * APR_DIR test.  But if you accessed /symlink/index.html, for example,
1440
 x    * you would *not* get the 403.
1441
 x
1442
 x   if (r->finfo.filetype != APR_DIR
1443
 x       && (res = resolve_symlink(r->filename, r->info, ap_allow_options(r),
1444
 x                                 r->pool))) {
1445
 x       ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(03295)
1446
 x                     "Symbolic link not allowed: %s", r->filename);
1447
 x       return res;
1448
 x   }
1449
 */
1450
1451
    /* Save future sub-requestors much angst in processing
1452
     * this subrequest.  If dir_walk couldn't canonicalize
1453
     * the file path, nothing can.
1454
     */
1455
0
    r->canonical_filename = r->filename;
1456
1457
0
    if (r->finfo.filetype == APR_DIR) {
1458
0
        cache->cached = apr_pstrdup(r->pool, r->filename);
1459
0
    }
1460
0
    else {
1461
0
        cache->cached = ap_make_dirstr_parent(r->pool, r->filename);
1462
0
    }
1463
1464
0
    if (cached
1465
0
        && r->per_dir_config == cache->dir_conf_merged) {
1466
0
        r->per_dir_config = cache->per_dir_result;
1467
0
        return OK;
1468
0
    }
1469
1470
0
    cache->dir_conf_tested = sec_ent;
1471
0
    cache->dir_conf_merged = r->per_dir_config;
1472
1473
    /* Merge our cache->dir_conf_merged construct with the r->per_dir_configs,
1474
     * and note the end result to (potentially) skip this step next time.
1475
     */
1476
0
    if (now_merged) {
1477
0
        r->per_dir_config = ap_merge_per_dir_configs(r->pool,
1478
0
                                                     r->per_dir_config,
1479
0
                                                     now_merged);
1480
0
    }
1481
0
    cache->per_dir_result = r->per_dir_config;
1482
1483
0
    return OK;
1484
0
}
1485
1486
1487
AP_DECLARE(int) ap_location_walk(request_rec *r)
1488
0
{
1489
0
    ap_conf_vector_t *now_merged = NULL;
1490
0
    core_server_config *sconf =
1491
0
        ap_get_core_module_config(r->server->module_config);
1492
0
    ap_conf_vector_t **sec_ent = (ap_conf_vector_t **)sconf->sec_url->elts;
1493
0
    int num_sec = sconf->sec_url->nelts;
1494
0
    walk_cache_t *cache;
1495
0
    const char *entry_uri;
1496
0
    int cached;
1497
1498
    /* No tricks here, there are no <Locations > to parse in this vhost.
1499
     * We won't destroy the cache, just in case _this_ redirect is later
1500
     * redirected again to a vhost with <Location > blocks to optimize.
1501
     */
1502
0
    if (!num_sec) {
1503
0
        return OK;
1504
0
    }
1505
1506
0
    cache = prep_walk_cache(AP_NOTE_LOCATION_WALK, r);
1507
0
    cached = (cache->cached != NULL);
1508
1509
   /*
1510
    * When merge_slashes is set to AP_CORE_CONFIG_OFF the slashes in r->uri
1511
    * have not been merged. But for Location walks we always go with merged
1512
    * slashes no matter what merge_slashes is set to.
1513
    */
1514
0
    if (sconf->merge_slashes != AP_CORE_CONFIG_OFF) {
1515
0
        entry_uri = r->uri;
1516
0
    }
1517
0
    else {
1518
0
        char *uri = apr_pstrdup(r->pool, r->uri);
1519
0
        ap_no2slash_ex(uri, 0/*not an FS path*/);
1520
0
        entry_uri = uri;
1521
0
    }
1522
1523
    /* If we have an cache->cached location that matches r->uri,
1524
     * and the vhost's list of locations hasn't changed, we can skip
1525
     * rewalking the location_walk entries.
1526
     */
1527
0
    if (cached
1528
0
        && (cache->dir_conf_tested == sec_ent)
1529
0
        && (strcmp(entry_uri, cache->cached) == 0)) {
1530
        /* Well this looks really familiar!  If our end-result (per_dir_result)
1531
         * didn't change, we have absolutely nothing to do :)
1532
         * Otherwise (as is the case with most dir_merged/file_merged requests)
1533
         * we must merge our dir_conf_merged onto this new r->per_dir_config.
1534
         */
1535
0
        if (r->per_dir_config == cache->per_dir_result) {
1536
0
            return OK;
1537
0
        }
1538
1539
0
        if (cache->walked->nelts) {
1540
0
            now_merged = ((walk_walked_t*)cache->walked->elts)
1541
0
                                            [cache->walked->nelts - 1].merged;
1542
0
        }
1543
0
    }
1544
0
    else {
1545
        /* We start now_merged from NULL since we want to build
1546
         * a locations list that can be merged to any vhost.
1547
         */
1548
0
        int len, sec_idx;
1549
0
        int matches = cache->walked->nelts;
1550
0
        int cached_matches = matches;
1551
0
        walk_walked_t *last_walk = (walk_walked_t*)cache->walked->elts;
1552
0
        apr_pool_t *rxpool = NULL;
1553
1554
0
        cached &= auth_internal_per_conf;
1555
0
        cache->cached = apr_pstrdup(r->pool, entry_uri);
1556
1557
        /* Go through the location entries, and check for matches.
1558
         * We apply the directive sections in given order, we should
1559
         * really try them with the most general first.
1560
         */
1561
0
        for (sec_idx = 0; sec_idx < num_sec; ++sec_idx) {
1562
1563
0
            core_dir_config *entry_core;
1564
0
            entry_core = ap_get_core_module_config(sec_ent[sec_idx]);
1565
1566
            /* ### const strlen can be optimized in location config parsing */
1567
0
            len = strlen(entry_core->d);
1568
1569
            /* Test the regex, fnmatch or string as appropriate.
1570
             * If it's a strcmp, and the <Location > pattern was
1571
             * not slash terminated, then this uri must be slash
1572
             * terminated (or at the end of the string) to match.
1573
             */
1574
0
            if (entry_core->r) {
1575
1576
0
                int nmatch = 0;
1577
0
                int i;
1578
0
                ap_regmatch_t *pmatch = NULL;
1579
1580
0
                if (entry_core->refs && entry_core->refs->nelts) {
1581
0
                    if (!rxpool) {
1582
0
                        apr_pool_create(&rxpool, r->pool);
1583
0
                        apr_pool_tag(rxpool, "location_walk_rxpool");
1584
0
                    }
1585
0
                    nmatch = entry_core->refs->nelts;
1586
0
                    pmatch = apr_palloc(rxpool, nmatch*sizeof(ap_regmatch_t));
1587
0
                }
1588
1589
0
                if (ap_regexec(entry_core->r, r->uri, nmatch, pmatch, 0)) {
1590
0
                    continue;
1591
0
                }
1592
1593
0
                for (i = 0; i < nmatch; i++) {
1594
0
                    if (pmatch[i].rm_so >= 0 && pmatch[i].rm_eo >= 0 && 
1595
0
                        ((const char **)entry_core->refs->elts)[i]) {
1596
0
                        apr_table_setn(r->subprocess_env,
1597
0
                                       ((const char **)entry_core->refs->elts)[i],
1598
0
                                       apr_pstrndup(r->pool,
1599
0
                                       r->uri + pmatch[i].rm_so,
1600
0
                                       pmatch[i].rm_eo - pmatch[i].rm_so));
1601
0
                    }
1602
0
                }
1603
1604
0
            }
1605
0
            else {
1606
1607
0
                if ((entry_core->d_is_fnmatch
1608
0
                   ? apr_fnmatch(entry_core->d, cache->cached, APR_FNM_PATHNAME)
1609
0
                   : (strncmp(entry_core->d, cache->cached, len)
1610
0
                      || (len > 0
1611
0
                          && entry_core->d[len - 1] != '/'
1612
0
                          && cache->cached[len] != '/'
1613
0
                          && cache->cached[len] != '\0')))) {
1614
0
                    continue;
1615
0
                }
1616
1617
0
            }
1618
1619
            /* If we merged this same section last time, reuse it
1620
             */
1621
0
            if (matches) {
1622
0
                if (last_walk->matched == sec_ent[sec_idx]) {
1623
0
                    now_merged = last_walk->merged;
1624
0
                    ++last_walk;
1625
0
                    --matches;
1626
0
                    continue;
1627
0
                }
1628
1629
                /* We fell out of sync.  This is our own copy of walked,
1630
                 * so truncate the remaining matches and reset remaining.
1631
                 */
1632
0
                cache->walked->nelts -= matches;
1633
0
                matches = 0;
1634
0
                cached = 0;
1635
0
            }
1636
1637
0
            if (now_merged) {
1638
0
                now_merged = ap_merge_per_dir_configs(r->pool,
1639
0
                                                      now_merged,
1640
0
                                                      sec_ent[sec_idx]);
1641
0
            }
1642
0
            else {
1643
0
                now_merged = sec_ent[sec_idx];
1644
0
            }
1645
1646
0
            last_walk = (walk_walked_t*)apr_array_push(cache->walked);
1647
0
            last_walk->matched = sec_ent[sec_idx];
1648
0
            last_walk->merged = now_merged;
1649
0
        }
1650
1651
0
        if (rxpool) {
1652
0
            apr_pool_destroy(rxpool);
1653
0
        }
1654
1655
        /* Whoops - everything matched in sequence, but either the original
1656
         * walk found some additional matches (which we need to truncate), or
1657
         * this walk found some additional matches.
1658
         */
1659
0
        if (matches) {
1660
0
            cache->walked->nelts -= matches;
1661
0
            cached = 0;
1662
0
        }
1663
0
        else if (cache->walked->nelts > cached_matches) {
1664
0
            cached = 0;
1665
0
        }
1666
0
    }
1667
1668
0
    if (cached
1669
0
        && r->per_dir_config == cache->dir_conf_merged) {
1670
0
        r->per_dir_config = cache->per_dir_result;
1671
0
        return OK;
1672
0
    }
1673
1674
0
    cache->dir_conf_tested = sec_ent;
1675
0
    cache->dir_conf_merged = r->per_dir_config;
1676
1677
    /* Merge our cache->dir_conf_merged construct with the r->per_dir_configs,
1678
     * and note the end result to (potentially) skip this step next time.
1679
     */
1680
0
    if (now_merged) {
1681
0
        r->per_dir_config = ap_merge_per_dir_configs(r->pool,
1682
0
                                                     r->per_dir_config,
1683
0
                                                     now_merged);
1684
0
    }
1685
0
    cache->per_dir_result = r->per_dir_config;
1686
1687
0
    return OK;
1688
0
}
1689
1690
AP_DECLARE(int) ap_file_walk(request_rec *r)
1691
0
{
1692
0
    ap_conf_vector_t *now_merged = NULL;
1693
0
    core_dir_config *dconf = ap_get_core_module_config(r->per_dir_config);
1694
0
    ap_conf_vector_t **sec_ent = NULL;
1695
0
    int num_sec = 0;
1696
0
    walk_cache_t *cache;
1697
0
    const char *test_file;
1698
0
    int cached;
1699
1700
0
    if (dconf->sec_file) {
1701
0
        sec_ent = (ap_conf_vector_t **)dconf->sec_file->elts;
1702
0
        num_sec = dconf->sec_file->nelts;
1703
0
    }
1704
1705
    /* To allow broken modules to proceed, we allow missing filenames to pass.
1706
     * We will catch it later if it's heading for the core handler.
1707
     * directory_walk already posted an INFO note for module debugging.
1708
     */
1709
0
    if (r->filename == NULL) {
1710
0
        return OK;
1711
0
    }
1712
1713
    /* No tricks here, there are just no <Files > to parse in this context.
1714
     * We won't destroy the cache, just in case _this_ redirect is later
1715
     * redirected again to a context containing the same or similar <Files >.
1716
     */
1717
0
    if (!num_sec) {
1718
0
        return OK;
1719
0
    }
1720
1721
0
    cache = prep_walk_cache(AP_NOTE_FILE_WALK, r);
1722
0
    cached = (cache->cached != NULL);
1723
1724
    /* Get the basename .. and copy for the cache just
1725
     * in case r->filename is munged by another module
1726
     */
1727
0
    test_file = strrchr(r->filename, '/');
1728
0
    if (test_file == NULL) {
1729
0
        test_file = apr_pstrdup(r->pool, r->filename);
1730
0
    }
1731
0
    else {
1732
0
        test_file = apr_pstrdup(r->pool, ++test_file);
1733
0
    }
1734
1735
    /* If we have an cache->cached file name that matches test_file,
1736
     * and the directory's list of file sections hasn't changed, we
1737
     * can skip rewalking the file_walk entries.
1738
     */
1739
0
    if (cached
1740
0
        && (cache->dir_conf_tested == sec_ent)
1741
0
        && (strcmp(test_file, cache->cached) == 0)) {
1742
        /* Well this looks really familiar!  If our end-result (per_dir_result)
1743
         * didn't change, we have absolutely nothing to do :)
1744
         * Otherwise (as is the case with most dir_merged requests)
1745
         * we must merge our dir_conf_merged onto this new r->per_dir_config.
1746
         */
1747
0
        if (r->per_dir_config == cache->per_dir_result) {
1748
0
            return OK;
1749
0
        }
1750
1751
0
        if (cache->walked->nelts) {
1752
0
            now_merged = ((walk_walked_t*)cache->walked->elts)
1753
0
                [cache->walked->nelts - 1].merged;
1754
0
        }
1755
0
    }
1756
0
    else {
1757
        /* We start now_merged from NULL since we want to build
1758
         * a file section list that can be merged to any dir_walk.
1759
         */
1760
0
        int sec_idx;
1761
0
        int matches = cache->walked->nelts;
1762
0
        int cached_matches = matches;
1763
0
        walk_walked_t *last_walk = (walk_walked_t*)cache->walked->elts;
1764
0
        apr_pool_t *rxpool = NULL;
1765
1766
0
        cached &= auth_internal_per_conf;
1767
0
        cache->cached = test_file;
1768
1769
        /* Go through the location entries, and check for matches.
1770
         * We apply the directive sections in given order, we should
1771
         * really try them with the most general first.
1772
         */
1773
0
        for (sec_idx = 0; sec_idx < num_sec; ++sec_idx) {
1774
0
            core_dir_config *entry_core;
1775
0
            entry_core = ap_get_core_module_config(sec_ent[sec_idx]);
1776
1777
0
            if (entry_core->r) {
1778
1779
0
                int nmatch = 0;
1780
0
                int i;
1781
0
                ap_regmatch_t *pmatch = NULL;
1782
1783
0
                if (entry_core->refs && entry_core->refs->nelts) {
1784
0
                    if (!rxpool) {
1785
0
                        apr_pool_create(&rxpool, r->pool);
1786
0
                        apr_pool_tag(rxpool, "file_walk_rxpool");
1787
0
                    }
1788
0
                    nmatch = entry_core->refs->nelts;
1789
0
                    pmatch = apr_palloc(rxpool, nmatch*sizeof(ap_regmatch_t));
1790
0
                }
1791
1792
0
                if (ap_regexec(entry_core->r, cache->cached, nmatch, pmatch, 0)) {
1793
0
                    continue;
1794
0
                }
1795
1796
0
                for (i = 0; i < nmatch; i++) {
1797
0
                    if (pmatch[i].rm_so >= 0 && pmatch[i].rm_eo >= 0 && 
1798
0
                        ((const char **)entry_core->refs->elts)[i]) {
1799
0
                        apr_table_setn(r->subprocess_env,
1800
0
                                       ((const char **)entry_core->refs->elts)[i],
1801
0
                                       apr_pstrndup(r->pool,
1802
0
                                       cache->cached + pmatch[i].rm_so,
1803
0
                                       pmatch[i].rm_eo - pmatch[i].rm_so));
1804
0
                    }
1805
0
                }
1806
1807
0
            }
1808
0
            else {
1809
0
                if ((entry_core->d_is_fnmatch
1810
0
                       ? apr_fnmatch(entry_core->d, cache->cached, APR_FNM_PATHNAME)
1811
0
                       : strcmp(entry_core->d, cache->cached))) {
1812
0
                    continue;
1813
0
                }
1814
0
            }
1815
1816
            /* If we merged this same section last time, reuse it
1817
             */
1818
0
            if (matches) {
1819
0
                if (last_walk->matched == sec_ent[sec_idx]) {
1820
0
                    now_merged = last_walk->merged;
1821
0
                    ++last_walk;
1822
0
                    --matches;
1823
0
                    continue;
1824
0
                }
1825
1826
                /* We fell out of sync.  This is our own copy of walked,
1827
                 * so truncate the remaining matches and reset remaining.
1828
                 */
1829
0
                cache->walked->nelts -= matches;
1830
0
                matches = 0;
1831
0
                cached = 0;
1832
0
            }
1833
1834
0
            if (now_merged) {
1835
0
                now_merged = ap_merge_per_dir_configs(r->pool,
1836
0
                                                      now_merged,
1837
0
                                                      sec_ent[sec_idx]);
1838
0
            }
1839
0
            else {
1840
0
                now_merged = sec_ent[sec_idx];
1841
0
            }
1842
1843
0
            last_walk = (walk_walked_t*)apr_array_push(cache->walked);
1844
0
            last_walk->matched = sec_ent[sec_idx];
1845
0
            last_walk->merged = now_merged;
1846
0
        }
1847
1848
0
        if (rxpool) {
1849
0
            apr_pool_destroy(rxpool);
1850
0
        }
1851
1852
        /* Whoops - everything matched in sequence, but either the original
1853
         * walk found some additional matches (which we need to truncate), or
1854
         * this walk found some additional matches.
1855
         */
1856
0
        if (matches) {
1857
0
            cache->walked->nelts -= matches;
1858
0
            cached = 0;
1859
0
        }
1860
0
        else if (cache->walked->nelts > cached_matches) {
1861
0
            cached = 0;
1862
0
        }
1863
0
    }
1864
1865
0
    if (cached
1866
0
        && r->per_dir_config == cache->dir_conf_merged) {
1867
0
        r->per_dir_config = cache->per_dir_result;
1868
0
        return OK;
1869
0
    }
1870
1871
0
    cache->dir_conf_tested = sec_ent;
1872
0
    cache->dir_conf_merged = r->per_dir_config;
1873
1874
    /* Merge our cache->dir_conf_merged construct with the r->per_dir_configs,
1875
     * and note the end result to (potentially) skip this step next time.
1876
     */
1877
0
    if (now_merged) {
1878
0
        r->per_dir_config = ap_merge_per_dir_configs(r->pool,
1879
0
                                                     r->per_dir_config,
1880
0
                                                     now_merged);
1881
0
    }
1882
0
    cache->per_dir_result = r->per_dir_config;
1883
1884
0
    return OK;
1885
0
}
1886
1887
static int ap_if_walk_sub(request_rec *r, core_dir_config* dconf)
1888
0
{
1889
0
    ap_conf_vector_t *now_merged = NULL;
1890
0
    ap_conf_vector_t **sec_ent = NULL;
1891
0
    int num_sec = 0;
1892
0
    walk_cache_t *cache;
1893
0
    int cached;
1894
0
    int sec_idx;
1895
0
    int matches;
1896
0
    int cached_matches;
1897
0
    int prev_result = -1;
1898
0
    walk_walked_t *last_walk;
1899
1900
0
    if (dconf && dconf->sec_if) {
1901
0
        sec_ent = (ap_conf_vector_t **)dconf->sec_if->elts;
1902
0
        num_sec = dconf->sec_if->nelts;
1903
0
    }
1904
1905
    /* No tricks here, there are just no <If > to parse in this context.
1906
     * We won't destroy the cache, just in case _this_ redirect is later
1907
     * redirected again to a context containing the same or similar <If >.
1908
     */
1909
0
    if (!num_sec) {
1910
0
        return OK;
1911
0
    }
1912
1913
0
    cache = prep_walk_cache(AP_NOTE_IF_WALK, r);
1914
0
    cached = (cache->cached != NULL);
1915
0
    cache->cached = (void *)1;
1916
0
    matches = cache->walked->nelts;
1917
0
    cached_matches = matches;
1918
0
    last_walk = (walk_walked_t*)cache->walked->elts;
1919
1920
0
    cached &= auth_internal_per_conf;
1921
1922
    /* Go through the if entries, and check for matches  */
1923
0
    for (sec_idx = 0; sec_idx < num_sec; ++sec_idx) {
1924
0
        const char *err = NULL;
1925
0
        core_dir_config *entry_core;
1926
0
        int rc;
1927
0
        entry_core = ap_get_core_module_config(sec_ent[sec_idx]);
1928
1929
0
        AP_DEBUG_ASSERT(entry_core->condition_ifelse != 0);
1930
0
        if (entry_core->condition_ifelse & AP_CONDITION_ELSE) {
1931
0
            AP_DEBUG_ASSERT(prev_result != -1);
1932
0
            if (prev_result == 1)
1933
0
                continue;
1934
0
        }
1935
1936
0
        if (entry_core->condition_ifelse & AP_CONDITION_IF) {
1937
0
            rc = ap_expr_exec(r, entry_core->condition, &err);
1938
0
            if (rc <= 0) {
1939
0
                if (rc < 0)
1940
0
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00039)
1941
0
                                  "Failed to evaluate <If > condition: %s",
1942
0
                                  err);
1943
0
                prev_result = 0;
1944
0
                continue;
1945
0
            }
1946
0
            prev_result = 1;
1947
0
        }
1948
0
        else {
1949
0
            prev_result = -1;
1950
0
        }
1951
1952
        /* If we merged this same section last time, reuse it
1953
         */
1954
0
        if (matches) {
1955
0
            if (last_walk->matched == sec_ent[sec_idx]) {
1956
0
                now_merged = last_walk->merged;
1957
0
                ++last_walk;
1958
0
                --matches;
1959
0
                continue;
1960
0
            }
1961
1962
            /* We fell out of sync.  This is our own copy of walked,
1963
             * so truncate the remaining matches and reset remaining.
1964
             */
1965
0
            cache->walked->nelts -= matches;
1966
0
            matches = 0;
1967
0
            cached = 0;
1968
0
        }
1969
1970
0
        if (now_merged) {
1971
0
            now_merged = ap_merge_per_dir_configs(r->pool,
1972
0
                                                  now_merged,
1973
0
                                                  sec_ent[sec_idx]);
1974
0
        }
1975
0
        else {
1976
0
            now_merged = sec_ent[sec_idx];
1977
0
        }
1978
1979
0
        last_walk = (walk_walked_t*)apr_array_push(cache->walked);
1980
0
        last_walk->matched = sec_ent[sec_idx];
1981
0
        last_walk->merged = now_merged;
1982
0
    }
1983
1984
    /* Everything matched in sequence, but it may be that the original
1985
     * walk found some additional matches (which we need to truncate), or
1986
     * this walk found some additional matches.
1987
     */
1988
0
    if (matches) {
1989
0
        cache->walked->nelts -= matches;
1990
0
        cached = 0;
1991
0
    }
1992
0
    else if (cache->walked->nelts > cached_matches) {
1993
0
        cached = 0;
1994
0
    }
1995
1996
0
    if (cached
1997
0
        && r->per_dir_config == cache->dir_conf_merged) {
1998
0
        r->per_dir_config = cache->per_dir_result;
1999
0
        return OK;
2000
0
    }
2001
2002
0
    cache->dir_conf_tested = sec_ent;
2003
0
    cache->dir_conf_merged = r->per_dir_config;
2004
2005
    /* Merge our cache->dir_conf_merged construct with the r->per_dir_configs,
2006
     * and note the end result to (potentially) skip this step next time.
2007
     */
2008
0
    if (now_merged) {
2009
0
        r->per_dir_config = ap_merge_per_dir_configs(r->pool,
2010
0
                                                     r->per_dir_config,
2011
0
                                                     now_merged);
2012
0
    }
2013
0
    cache->per_dir_result = r->per_dir_config;
2014
2015
0
    if (now_merged) {
2016
0
        core_dir_config *dconf_merged = ap_get_core_module_config(now_merged);
2017
2018
        /* Allow nested <If>s and their configs to get merged
2019
         * with the current one.
2020
         */
2021
0
        return ap_if_walk_sub(r, dconf_merged);
2022
0
    }
2023
2024
0
    return OK;
2025
0
}
2026
2027
AP_DECLARE(int) ap_if_walk(request_rec *r)
2028
0
{
2029
0
    core_dir_config *dconf = ap_get_core_module_config(r->per_dir_config);
2030
0
    int status = ap_if_walk_sub(r, dconf);
2031
0
    return status;
2032
0
}
2033
2034
/*****************************************************************
2035
 *
2036
 * The sub_request mechanism.
2037
 *
2038
 * Fns to look up a relative URI from, e.g., a map file or SSI document.
2039
 * These do all access checks, etc., but don't actually run the transaction
2040
 * ... use run_sub_req below for that.  Also, be sure to use destroy_sub_req
2041
 * as appropriate if you're likely to be creating more than a few of these.
2042
 * (An early Apache version didn't destroy the sub_reqs used in directory
2043
 * indexing.  The result, when indexing a directory with 800-odd files in
2044
 * it, was massively excessive storage allocation).
2045
 *
2046
 * Note more manipulation of protocol-specific vars in the request
2047
 * structure...
2048
 */
2049
2050
static request_rec *make_sub_request(const request_rec *r,
2051
                                     ap_filter_t *next_filter)
2052
0
{
2053
0
    apr_pool_t *rrp;
2054
0
    request_rec *rnew;
2055
2056
0
    apr_pool_create(&rrp, r->pool);
2057
0
    apr_pool_tag(rrp, "subrequest");
2058
0
    rnew = apr_pcalloc(rrp, sizeof(request_rec));
2059
0
    rnew->pool = rrp;
2060
2061
0
    rnew->hostname       = r->hostname;
2062
0
    rnew->request_time   = r->request_time;
2063
0
    rnew->connection     = r->connection;
2064
0
    rnew->server         = r->server;
2065
0
    rnew->log            = r->log;
2066
2067
0
    rnew->request_config = ap_create_request_config(rnew->pool);
2068
2069
    /* Start a clean config from this subrequest's vhost.  Optimization in
2070
     * Location/File/Dir walks from the parent request assure that if the
2071
     * config blocks of the subrequest match the parent request, no merges
2072
     * will actually occur (and generally a minimal number of merges are
2073
     * required, even if the parent and subrequest aren't quite identical.)
2074
     */
2075
0
    rnew->per_dir_config = r->server->lookup_defaults;
2076
2077
0
    rnew->htaccess = r->htaccess;
2078
0
    rnew->allowed_methods = ap_make_method_list(rnew->pool, 2);
2079
2080
    /* make a copy of the allowed-methods list */
2081
0
    ap_copy_method_list(rnew->allowed_methods, r->allowed_methods);
2082
2083
    /* start with the same set of output filters */
2084
0
    if (next_filter) {
2085
0
        ap_filter_t *scan = next_filter;
2086
2087
        /* while there are no input filters for a subrequest, we will
2088
         * try to insert some, so if we don't have valid data, the code
2089
         * will seg fault.
2090
         */
2091
0
        rnew->input_filters = r->input_filters;
2092
0
        rnew->proto_input_filters = r->proto_input_filters;
2093
0
        rnew->output_filters = next_filter;
2094
0
        rnew->proto_output_filters = r->proto_output_filters;
2095
0
        while (scan && (scan != r->proto_output_filters)) {
2096
0
            if (scan->frec == ap_subreq_core_filter_handle) {
2097
0
                break;
2098
0
            }
2099
0
            scan = scan->next;
2100
0
        }
2101
0
        if (!scan || scan == r->proto_output_filters) {
2102
0
            ap_add_output_filter_handle(ap_subreq_core_filter_handle,
2103
0
                    NULL, rnew, rnew->connection);
2104
0
        }
2105
0
    }
2106
0
    else {
2107
        /* If NULL - we are expecting to be internal_fast_redirect'ed
2108
         * to this subrequest - or this request will never be invoked.
2109
         * Ignore the original request filter stack entirely, and
2110
         * drill the input and output stacks back to the connection.
2111
         */
2112
0
        rnew->proto_input_filters = r->proto_input_filters;
2113
0
        rnew->proto_output_filters = r->proto_output_filters;
2114
2115
0
        rnew->input_filters = r->proto_input_filters;
2116
0
        rnew->output_filters = r->proto_output_filters;
2117
0
    }
2118
2119
0
    rnew->useragent_addr = r->useragent_addr;
2120
0
    rnew->useragent_ip = r->useragent_ip;
2121
2122
    /* no input filters for a subrequest */
2123
2124
0
    ap_set_sub_req_protocol(rnew, r);
2125
2126
    /* We have to run this after we fill in sub req vars,
2127
     * or the r->main pointer won't be setup
2128
     */
2129
0
    ap_run_create_request(rnew);
2130
2131
    /* Begin by presuming any module can make its own path_info assumptions,
2132
     * until some module interjects and changes the value.
2133
     */
2134
0
    rnew->used_path_info = AP_REQ_DEFAULT_PATH_INFO;
2135
2136
    /* Pass on the kept body (if any) into the new request. */
2137
0
    rnew->kept_body = r->kept_body;
2138
2139
0
    return rnew;
2140
0
}
2141
2142
AP_CORE_DECLARE_NONSTD(apr_status_t) ap_sub_req_output_filter(ap_filter_t *f,
2143
                                                              apr_bucket_brigade *bb)
2144
0
{
2145
0
    apr_bucket *e = APR_BRIGADE_LAST(bb);
2146
2147
0
    if (APR_BUCKET_IS_EOS(e)) {
2148
0
        apr_bucket_delete(e);
2149
0
    }
2150
2151
0
    if (!APR_BRIGADE_EMPTY(bb)) {
2152
0
        return ap_pass_brigade(f->next, bb);
2153
0
    }
2154
2155
0
    return APR_SUCCESS;
2156
0
}
2157
2158
extern APR_OPTIONAL_FN_TYPE(authz_some_auth_required) *ap__authz_ap_some_auth_required;
2159
2160
AP_DECLARE(int) ap_some_auth_required(request_rec *r)
2161
0
{
2162
    /* Is there a require line configured for the type of *this* req? */
2163
0
    if (ap__authz_ap_some_auth_required) {
2164
0
        return ap__authz_ap_some_auth_required(r);
2165
0
    }
2166
0
    else
2167
0
        return 0;
2168
0
}
2169
2170
AP_DECLARE(void) ap_clear_auth_internal(void)
2171
0
{
2172
0
    auth_internal_per_conf_hooks = 0;
2173
0
    auth_internal_per_conf_providers = 0;
2174
0
}
2175
2176
AP_DECLARE(void) ap_setup_auth_internal(apr_pool_t *ptemp)
2177
0
{
2178
0
    int total_auth_hooks = 0;
2179
0
    int total_auth_providers = 0;
2180
2181
0
    auth_internal_per_conf = 0;
2182
2183
0
    if (_hooks.link_access_checker) {
2184
0
        total_auth_hooks += _hooks.link_access_checker->nelts;
2185
0
    }
2186
0
    if (_hooks.link_access_checker_ex) {
2187
0
        total_auth_hooks += _hooks.link_access_checker_ex->nelts;
2188
0
    }
2189
0
    if (_hooks.link_check_user_id) {
2190
0
        total_auth_hooks += _hooks.link_check_user_id->nelts;
2191
0
    }
2192
0
    if (_hooks.link_auth_checker) {
2193
0
        total_auth_hooks += _hooks.link_auth_checker->nelts;
2194
0
    }
2195
2196
0
    if (total_auth_hooks > auth_internal_per_conf_hooks) {
2197
0
        return;
2198
0
    }
2199
2200
0
    total_auth_providers +=
2201
0
        ap_list_provider_names(ptemp, AUTHN_PROVIDER_GROUP,
2202
0
                               AUTHN_PROVIDER_VERSION)->nelts;
2203
0
    total_auth_providers +=
2204
0
        ap_list_provider_names(ptemp, AUTHZ_PROVIDER_GROUP,
2205
0
                               AUTHZ_PROVIDER_VERSION)->nelts;
2206
2207
0
    if (total_auth_providers > auth_internal_per_conf_providers) {
2208
0
        return;
2209
0
    }
2210
2211
0
    auth_internal_per_conf = 1;
2212
0
}
2213
2214
AP_DECLARE(apr_status_t) ap_register_auth_provider(apr_pool_t *pool,
2215
                                                   const char *provider_group,
2216
                                                   const char *provider_name,
2217
                                                   const char *provider_version,
2218
                                                   const void *provider,
2219
                                                   int type)
2220
0
{
2221
0
    if ((type & AP_AUTH_INTERNAL_MASK) == AP_AUTH_INTERNAL_PER_CONF) {
2222
0
        ++auth_internal_per_conf_providers;
2223
0
    }
2224
2225
0
    return ap_register_provider(pool, provider_group, provider_name,
2226
0
                                provider_version, provider);
2227
0
}
2228
2229
AP_DECLARE(void) ap_hook_check_access(ap_HOOK_access_checker_t *pf,
2230
                                      const char * const *aszPre,
2231
                                      const char * const *aszSucc,
2232
                                      int nOrder, int type)
2233
0
{
2234
0
    if ((type & AP_AUTH_INTERNAL_MASK) == AP_AUTH_INTERNAL_PER_CONF) {
2235
0
        ++auth_internal_per_conf_hooks;
2236
0
    }
2237
2238
0
    ap_hook_access_checker(pf, aszPre, aszSucc, nOrder);
2239
0
}
2240
2241
AP_DECLARE(void) ap_hook_check_access_ex(ap_HOOK_access_checker_ex_t *pf,
2242
                                      const char * const *aszPre,
2243
                                      const char * const *aszSucc,
2244
                                      int nOrder, int type)
2245
0
{
2246
0
    if ((type & AP_AUTH_INTERNAL_MASK) == AP_AUTH_INTERNAL_PER_CONF) {
2247
0
        ++auth_internal_per_conf_hooks;
2248
0
    }
2249
2250
0
    ap_hook_access_checker_ex(pf, aszPre, aszSucc, nOrder);
2251
0
}
2252
2253
AP_DECLARE(void) ap_hook_check_autht(ap_HOOK_token_checker_t *pf,
2254
                                     const char * const *aszPre,
2255
                                     const char * const *aszSucc,
2256
                                     int nOrder, int type)
2257
0
{
2258
0
    if ((type & AP_AUTH_INTERNAL_MASK) == AP_AUTH_INTERNAL_PER_CONF) {
2259
0
        ++auth_internal_per_conf_hooks;
2260
0
    }
2261
2262
0
    ap_hook_token_checker(pf, aszPre, aszSucc, nOrder);
2263
0
}
2264
2265
AP_DECLARE(void) ap_hook_check_authn(ap_HOOK_check_user_id_t *pf,
2266
                                     const char * const *aszPre,
2267
                                     const char * const *aszSucc,
2268
                                     int nOrder, int type)
2269
0
{
2270
0
    if ((type & AP_AUTH_INTERNAL_MASK) == AP_AUTH_INTERNAL_PER_CONF) {
2271
0
        ++auth_internal_per_conf_hooks;
2272
0
    }
2273
2274
0
    ap_hook_check_user_id(pf, aszPre, aszSucc, nOrder);
2275
0
}
2276
2277
AP_DECLARE(void) ap_hook_check_authz(ap_HOOK_auth_checker_t *pf,
2278
                                     const char * const *aszPre,
2279
                                     const char * const *aszSucc,
2280
                                     int nOrder, int type)
2281
0
{
2282
0
    if ((type & AP_AUTH_INTERNAL_MASK) == AP_AUTH_INTERNAL_PER_CONF) {
2283
0
        ++auth_internal_per_conf_hooks;
2284
0
    }
2285
2286
0
    ap_hook_auth_checker(pf, aszPre, aszSucc, nOrder);
2287
0
}
2288
2289
AP_DECLARE(request_rec *) ap_sub_req_method_uri(const char *method,
2290
                                                const char *new_uri,
2291
                                                const request_rec *r,
2292
                                                ap_filter_t *next_filter)
2293
0
{
2294
0
    request_rec *rnew;
2295
0
    int res = DECLINED;
2296
0
    char *udir;
2297
2298
0
    rnew = make_sub_request(r, next_filter);
2299
2300
    /* would be nicer to pass "method" to ap_set_sub_req_protocol */
2301
0
    rnew->method = method;
2302
0
    rnew->method_number = ap_method_number_of(method);
2303
2304
0
    if (new_uri[0] == '/') {
2305
0
        ap_parse_uri(rnew, new_uri);
2306
0
    }
2307
0
    else {
2308
0
        udir = ap_make_dirstr_parent(rnew->pool, r->uri);
2309
0
        udir = ap_escape_uri(rnew->pool, udir);    /* re-escape it */
2310
0
        ap_parse_uri(rnew, ap_make_full_path(rnew->pool, udir, new_uri));
2311
0
    }
2312
0
    if (ap_is_HTTP_ERROR(rnew->status)) {
2313
0
        return rnew;
2314
0
    }
2315
2316
    /* We cannot return NULL without violating the API. So just turn this
2317
     * subrequest into a 500 to indicate the failure. */
2318
0
    if (ap_is_recursion_limit_exceeded(r)) {
2319
0
        rnew->status = HTTP_INTERNAL_SERVER_ERROR;
2320
0
        return rnew;
2321
0
    }
2322
2323
    /* lookup_uri
2324
     * If the content can be served by the quick_handler, we can
2325
     * safely bypass request_internal processing.
2326
     *
2327
     * If next_filter is NULL we are expecting to be
2328
     * internal_fast_redirect'ed to the subrequest, or the subrequest will
2329
     * never be invoked. We need to make sure that the quickhandler is not
2330
     * invoked by any lookups. Since an internal_fast_redirect will always
2331
     * occur too late for the quickhandler to handle the request.
2332
     */
2333
0
    if (next_filter) {
2334
0
        res = ap_run_quick_handler(rnew, 1);
2335
0
    }
2336
0
    if (res == DECLINED) {
2337
0
        res = ap_process_request_internal(rnew);
2338
0
    }
2339
0
    if (res) {
2340
0
        rnew->status = res;
2341
0
    }
2342
2343
0
    return rnew;
2344
0
}
2345
2346
AP_DECLARE(request_rec *) ap_sub_req_lookup_uri(const char *new_uri,
2347
                                                const request_rec *r,
2348
                                                ap_filter_t *next_filter)
2349
0
{
2350
0
    return ap_sub_req_method_uri("GET", new_uri, r, next_filter);
2351
0
}
2352
2353
AP_DECLARE(request_rec *) ap_sub_req_lookup_dirent(const apr_finfo_t *dirent,
2354
                                                   const request_rec *r,
2355
                                                   int subtype,
2356
                                                   ap_filter_t *next_filter)
2357
0
{
2358
0
    request_rec *rnew;
2359
0
    int res;
2360
0
    char *fdir;
2361
0
    char *udir;
2362
2363
0
    rnew = make_sub_request(r, next_filter);
2364
2365
    /* Special case: we are looking at a relative lookup in the same directory.
2366
     * This is 100% safe, since dirent->name just came from the filesystem.
2367
     */
2368
0
    if (r->path_info && *r->path_info) {
2369
        /* strip path_info off the end of the uri to keep it in sync
2370
         * with r->filename, which has already been stripped by directory_walk,
2371
         * merge the dirent->name, and then, if the caller wants us to remerge
2372
         * the original path info, do so.  Note we never fix the path_info back
2373
         * to r->filename, since dir_walk would do so (but we don't expect it
2374
         * to happen in the usual cases)
2375
         */
2376
0
        udir = apr_pstrdup(rnew->pool, r->uri);
2377
0
        udir[ap_find_path_info(udir, r->path_info)] = '\0';
2378
0
        udir = ap_make_dirstr_parent(rnew->pool, udir);
2379
2380
0
        rnew->uri = ap_make_full_path(rnew->pool, udir, dirent->name);
2381
0
        if (subtype == AP_SUBREQ_MERGE_ARGS) {
2382
0
            rnew->uri = ap_make_full_path(rnew->pool, rnew->uri, r->path_info + 1);
2383
0
            rnew->path_info = apr_pstrdup(rnew->pool, r->path_info);
2384
0
        }
2385
0
        rnew->uri = ap_escape_uri(rnew->pool, rnew->uri);
2386
0
    }
2387
0
    else {
2388
0
        udir = ap_make_dirstr_parent(rnew->pool, r->uri);
2389
0
        rnew->uri = ap_escape_uri(rnew->pool, ap_make_full_path(rnew->pool,
2390
0
                                                                udir,
2391
0
                                                                dirent->name));
2392
0
    }
2393
2394
0
    fdir = ap_make_dirstr_parent(rnew->pool, r->filename);
2395
0
    rnew->filename = ap_make_full_path(rnew->pool, fdir, dirent->name);
2396
0
    if (r->canonical_filename == r->filename) {
2397
0
        rnew->canonical_filename = rnew->filename;
2398
0
    }
2399
2400
    /* XXX This is now less relevant; we will do a full location walk
2401
     * these days for this case.  Preserve the apr_stat results, and
2402
     * perhaps we also tag that symlinks were tested and/or found for
2403
     * r->filename.
2404
     */
2405
0
    rnew->per_dir_config = r->server->lookup_defaults;
2406
2407
0
    if ((dirent->valid & APR_FINFO_MIN) != APR_FINFO_MIN) {
2408
        /*
2409
         * apr_dir_read isn't very complete on this platform, so
2410
         * we need another apr_stat (with or without APR_FINFO_LINK
2411
         * depending on whether we allow all symlinks here.)  If this
2412
         * is an APR_LNK that resolves to an APR_DIR, then we will rerun
2413
         * everything anyways... this should be safe.
2414
         */
2415
0
        apr_status_t rv;
2416
0
        if (ap_allow_options(rnew) & OPT_SYM_LINKS) {
2417
0
            if (((rv = apr_stat(&rnew->finfo, rnew->filename,
2418
0
                                APR_FINFO_MIN, rnew->pool)) != APR_SUCCESS)
2419
0
                && (rv != APR_INCOMPLETE)) {
2420
0
                rnew->finfo.filetype = APR_NOFILE;
2421
0
            }
2422
0
        }
2423
0
        else {
2424
0
            if (((rv = apr_stat(&rnew->finfo, rnew->filename,
2425
0
                                APR_FINFO_LINK | APR_FINFO_MIN,
2426
0
                                rnew->pool)) != APR_SUCCESS)
2427
0
                && (rv != APR_INCOMPLETE)) {
2428
0
                rnew->finfo.filetype = APR_NOFILE;
2429
0
            }
2430
0
        }
2431
0
    }
2432
0
    else {
2433
0
        memcpy(&rnew->finfo, dirent, sizeof(apr_finfo_t));
2434
0
    }
2435
2436
0
    if (rnew->finfo.filetype == APR_LNK) {
2437
        /*
2438
         * Resolve this symlink.  We should tie this back to dir_walk's cache
2439
         */
2440
0
        if ((res = resolve_symlink(rnew->filename, &rnew->finfo,
2441
0
                                   ap_allow_options(rnew), rnew->pool))
2442
0
            != OK) {
2443
0
            rnew->status = res;
2444
0
            return rnew;
2445
0
        }
2446
0
    }
2447
2448
0
    if (rnew->finfo.filetype == APR_DIR) {
2449
        /* ap_make_full_path and ap_escape_uri overallocated the buffers
2450
         * by one character to help us out here.
2451
         */
2452
0
        strcat(rnew->filename, "/");
2453
0
        if (!rnew->path_info || !*rnew->path_info) {
2454
0
            strcat(rnew->uri, "/");
2455
0
        }
2456
0
    }
2457
2458
    /* fill in parsed_uri values
2459
     */
2460
0
    if (r->args && *r->args && (subtype == AP_SUBREQ_MERGE_ARGS)) {
2461
0
        ap_parse_uri(rnew, apr_pstrcat(r->pool, rnew->uri, "?",
2462
0
                                       r->args, NULL));
2463
0
    }
2464
0
    else {
2465
0
        ap_parse_uri(rnew, rnew->uri);
2466
0
    }
2467
2468
    /* We cannot return NULL without violating the API. So just turn this
2469
     * subrequest into a 500. */
2470
0
    if (ap_is_recursion_limit_exceeded(r)) {
2471
0
        rnew->status = HTTP_INTERNAL_SERVER_ERROR;
2472
0
        return rnew;
2473
0
    }
2474
2475
0
    if ((res = ap_process_request_internal(rnew))) {
2476
0
        rnew->status = res;
2477
0
    }
2478
2479
0
    return rnew;
2480
0
}
2481
2482
AP_DECLARE(request_rec *) ap_sub_req_lookup_file(const char *new_file,
2483
                                                 const request_rec *r,
2484
                                                 ap_filter_t *next_filter)
2485
0
{
2486
0
    request_rec *rnew;
2487
0
    int res;
2488
0
    char *fdir;
2489
0
    apr_size_t fdirlen;
2490
2491
0
    rnew = make_sub_request(r, next_filter);
2492
2493
0
    fdir = ap_make_dirstr_parent(rnew->pool, r->filename);
2494
0
    fdirlen = strlen(fdir);
2495
2496
    /* Translate r->filename, if it was canonical, it stays canonical
2497
     */
2498
0
    if (r->canonical_filename == r->filename) {
2499
0
        rnew->canonical_filename = (char*)(1);
2500
0
    }
2501
2502
0
    if (apr_filepath_merge(&rnew->filename, fdir, new_file,
2503
0
                           APR_FILEPATH_TRUENAME, rnew->pool) != APR_SUCCESS) {
2504
0
        rnew->status = HTTP_FORBIDDEN;
2505
0
        return rnew;
2506
0
    }
2507
2508
0
    if (rnew->canonical_filename) {
2509
0
        rnew->canonical_filename = rnew->filename;
2510
0
    }
2511
2512
    /*
2513
     * Check for a special case... if there are no '/' characters in new_file
2514
     * at all, and the path was the same, then we are looking at a relative
2515
     * lookup in the same directory.  Fixup the URI to match.
2516
     */
2517
2518
0
    if (strncmp(rnew->filename, fdir, fdirlen) == 0
2519
0
        && rnew->filename[fdirlen]
2520
0
        && ap_strchr_c(rnew->filename + fdirlen, '/') == NULL) {
2521
0
        apr_status_t rv;
2522
0
        if (ap_allow_options(rnew) & OPT_SYM_LINKS) {
2523
0
            if (((rv = apr_stat(&rnew->finfo, rnew->filename,
2524
0
                                APR_FINFO_MIN, rnew->pool)) != APR_SUCCESS)
2525
0
                && (rv != APR_INCOMPLETE)) {
2526
0
                rnew->finfo.filetype = APR_NOFILE;
2527
0
            }
2528
0
        }
2529
0
        else {
2530
0
            if (((rv = apr_stat(&rnew->finfo, rnew->filename,
2531
0
                                APR_FINFO_LINK | APR_FINFO_MIN,
2532
0
                                rnew->pool)) != APR_SUCCESS)
2533
0
                && (rv != APR_INCOMPLETE)) {
2534
0
                rnew->finfo.filetype = APR_NOFILE;
2535
0
            }
2536
0
        }
2537
2538
0
        if (r->uri && *r->uri) {
2539
0
            char *udir = ap_make_dirstr_parent(rnew->pool, r->uri);
2540
0
            rnew->uri = ap_make_full_path(rnew->pool, udir,
2541
0
                                          rnew->filename + fdirlen);
2542
0
            ap_parse_uri(rnew, rnew->uri);    /* fill in parsed_uri values */
2543
0
        }
2544
0
        else {
2545
0
            ap_parse_uri(rnew, new_file);        /* fill in parsed_uri values */
2546
0
            rnew->uri = apr_pstrdup(rnew->pool, "");
2547
0
        }
2548
0
    }
2549
0
    else {
2550
        /* XXX: @@@: What should be done with the parsed_uri values?
2551
         * We would be better off stripping down to the 'common' elements
2552
         * of the path, then reassembling the URI as best as we can.
2553
         */
2554
0
        ap_parse_uri(rnew, new_file);        /* fill in parsed_uri values */
2555
        /*
2556
         * XXX: this should be set properly like it is in the same-dir case
2557
         * but it's actually sometimes to impossible to do it... because the
2558
         * file may not have a uri associated with it -djg
2559
         */
2560
0
        rnew->uri = apr_pstrdup(rnew->pool, "");
2561
0
    }
2562
2563
    /* We cannot return NULL without violating the API. So just turn this
2564
     * subrequest into a 500. */
2565
0
    if (ap_is_recursion_limit_exceeded(r)) {
2566
0
        rnew->status = HTTP_INTERNAL_SERVER_ERROR;
2567
0
        return rnew;
2568
0
    }
2569
2570
0
    if ((res = ap_process_request_internal(rnew))) {
2571
0
        rnew->status = res;
2572
0
    }
2573
2574
0
    return rnew;
2575
0
}
2576
2577
AP_DECLARE(int) ap_run_sub_req(request_rec *r)
2578
0
{
2579
0
    int retval = DECLINED;
2580
    /* Run the quick handler if the subrequest is not a dirent or file
2581
     * subrequest
2582
     */
2583
0
    if (!(r->filename && r->finfo.filetype != APR_NOFILE)) {
2584
0
        retval = ap_run_quick_handler(r, 0);
2585
0
    }
2586
0
    if (retval == DECLINED) {
2587
0
        retval = ap_invoke_handler(r);
2588
0
    }
2589
0
    if (retval == DONE) {
2590
0
        retval = OK;
2591
0
    }
2592
0
    ap_finalize_sub_req_protocol(r);
2593
0
    return retval;
2594
0
}
2595
2596
AP_DECLARE(void) ap_destroy_sub_req(request_rec *r)
2597
0
{
2598
    /* Reclaim the space */
2599
0
    apr_pool_destroy(r->pool);
2600
0
}
2601
2602
/*
2603
 * Function to set the r->mtime field to the specified value if it's later
2604
 * than what's already there.
2605
 */
2606
AP_DECLARE(void) ap_update_mtime(request_rec *r, apr_time_t dependency_mtime)
2607
0
{
2608
0
    if (r->mtime < dependency_mtime) {
2609
0
        r->mtime = dependency_mtime;
2610
0
    }
2611
0
}
2612
2613
/*
2614
 * Is it the initial main request, which we only get *once* per HTTP request?
2615
 */
2616
AP_DECLARE(int) ap_is_initial_req(request_rec *r)
2617
0
{
2618
0
    return (r->main == NULL)       /* otherwise, this is a sub-request */
2619
0
           && (r->prev == NULL);   /* otherwise, this is an internal redirect */
2620
0
}
2621