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