Blame server/request.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * @file  request.c
Packit 90a5c9
 * @brief functions to get and process requests
Packit 90a5c9
 *
Packit 90a5c9
 * @author Rob McCool 3/21/93
Packit 90a5c9
 *
Packit 90a5c9
 * Thoroughly revamped by rst for Apache.  NB this file reads
Packit 90a5c9
 * best from the bottom up.
Packit 90a5c9
 *
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_file_io.h"
Packit 90a5c9
#include "apr_fnmatch.h"
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "ap_provider.h"
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "http_main.h"
Packit 90a5c9
#include "util_filter.h"
Packit 90a5c9
#include "util_charset.h"
Packit 90a5c9
#include "util_script.h"
Packit 90a5c9
#include "ap_expr.h"
Packit 90a5c9
#include "mod_request.h"
Packit 90a5c9
Packit 90a5c9
#include "mod_core.h"
Packit 90a5c9
#include "mod_auth.h"
Packit 90a5c9
Packit 90a5c9
#if APR_HAVE_STDARG_H
Packit 90a5c9
#include <stdarg.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
/* we know core's module_index is 0 */
Packit 90a5c9
#undef APLOG_MODULE_INDEX
Packit 90a5c9
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
Packit 90a5c9
Packit 90a5c9
APR_HOOK_STRUCT(
Packit 90a5c9
    APR_HOOK_LINK(translate_name)
Packit 90a5c9
    APR_HOOK_LINK(map_to_storage)
Packit 90a5c9
    APR_HOOK_LINK(check_user_id)
Packit 90a5c9
    APR_HOOK_LINK(fixups)
Packit 90a5c9
    APR_HOOK_LINK(type_checker)
Packit 90a5c9
    APR_HOOK_LINK(access_checker)
Packit 90a5c9
    APR_HOOK_LINK(access_checker_ex)
Packit 90a5c9
    APR_HOOK_LINK(auth_checker)
Packit 90a5c9
    APR_HOOK_LINK(insert_filter)
Packit 90a5c9
    APR_HOOK_LINK(create_request)
Packit 90a5c9
    APR_HOOK_LINK(post_perdir_config)
Packit 90a5c9
    APR_HOOK_LINK(dirwalk_stat)
Packit 90a5c9
    APR_HOOK_LINK(force_authn)
Packit 90a5c9
)
Packit 90a5c9
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(int,translate_name,
Packit 90a5c9
                            (request_rec *r), (r), DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(int,map_to_storage,
Packit 90a5c9
                            (request_rec *r), (r), DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(int,check_user_id,
Packit 90a5c9
                            (request_rec *r), (r), DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_ALL(int,fixups,
Packit 90a5c9
                          (request_rec *r), (r), OK, DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(int,type_checker,
Packit 90a5c9
                            (request_rec *r), (r), DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_ALL(int,access_checker,
Packit 90a5c9
                          (request_rec *r), (r), OK, DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(int,access_checker_ex,
Packit 90a5c9
                          (request_rec *r), (r), DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(int,auth_checker,
Packit 90a5c9
                            (request_rec *r), (r), DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_VOID(insert_filter, (request_rec *r), (r))
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_ALL(int, create_request,
Packit 90a5c9
                          (request_rec *r), (r), OK, DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_ALL(int, post_perdir_config,
Packit 90a5c9
                          (request_rec *r), (r), OK, DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(apr_status_t,dirwalk_stat,
Packit 90a5c9
                            (apr_finfo_t *finfo, request_rec *r, apr_int32_t wanted),
Packit 90a5c9
                            (finfo, r, wanted), AP_DECLINED)
Packit 90a5c9
AP_IMPLEMENT_HOOK_RUN_FIRST(int,force_authn,
Packit 90a5c9
                            (request_rec *r), (r), DECLINED)
Packit 90a5c9
Packit 90a5c9
static int auth_internal_per_conf = 0;
Packit 90a5c9
static int auth_internal_per_conf_hooks = 0;
Packit 90a5c9
static int auth_internal_per_conf_providers = 0;
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static int decl_die(int status, const char *phase, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    if (status == DECLINED) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, APLOGNO(00025)
Packit 90a5c9
                      "configuration error:  couldn't %s: %s", phase, r->uri);
Packit 90a5c9
        return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
Packit 90a5c9
                      "auth phase '%s' gave status %d: %s", phase,
Packit 90a5c9
                      status, r->uri);
Packit 90a5c9
        return status;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_some_authn_required(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    int access_status;
Packit 90a5c9
    char *olduser = r->user;
Packit 90a5c9
    int rv = FALSE;
Packit 90a5c9
Packit 90a5c9
    switch (ap_satisfies(r)) {
Packit 90a5c9
    case SATISFY_ALL:
Packit 90a5c9
    case SATISFY_NOSPEC:
Packit 90a5c9
        if ((access_status = ap_run_access_checker(r)) != OK) {
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        access_status = ap_run_access_checker_ex(r);
Packit 90a5c9
        if (access_status == DECLINED) {
Packit 90a5c9
            rv = TRUE;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        break;
Packit 90a5c9
    case SATISFY_ANY:
Packit 90a5c9
        if ((access_status = ap_run_access_checker(r)) == OK) {
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        access_status = ap_run_access_checker_ex(r);
Packit 90a5c9
        if (access_status == DECLINED) {
Packit 90a5c9
            rv = TRUE;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        break;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    r->user = olduser;
Packit 90a5c9
    return rv;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/* This is the master logic for processing requests.  Do NOT duplicate
Packit 90a5c9
 * this logic elsewhere, or the security model will be broken by future
Packit 90a5c9
 * API changes.  Each phase must be individually optimized to pick up
Packit 90a5c9
 * redundant/duplicate calls by subrequests, and redirects.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_process_request_internal(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    int file_req = (r->main && r->filename);
Packit 90a5c9
    int access_status;
Packit 90a5c9
    core_dir_config *d;
Packit 90a5c9
Packit 90a5c9
    /* Ignore embedded %2F's in path for proxy requests */
Packit 90a5c9
    if (!r->proxyreq && r->parsed_uri.path) {
Packit 90a5c9
        d = ap_get_core_module_config(r->per_dir_config);
Packit 90a5c9
        if (d->allow_encoded_slashes) {
Packit 90a5c9
            access_status = ap_unescape_url_keep2f(r->parsed_uri.path, d->decode_encoded_slashes);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            access_status = ap_unescape_url(r->parsed_uri.path);
Packit 90a5c9
        }
Packit 90a5c9
        if (access_status) {
Packit 90a5c9
            if (access_status == HTTP_NOT_FOUND) {
Packit 90a5c9
                if (! d->allow_encoded_slashes) {
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00026)
Packit 90a5c9
                                  "found %%2f (encoded '/') in URI "
Packit 90a5c9
                                  "(decoded='%s'), returning 404",
Packit 90a5c9
                                  r->parsed_uri.path);
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            return access_status;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_getparents(r->uri);     /* OK --- shrinking transformations... */
Packit 90a5c9
Packit 90a5c9
    /* All file subrequests are a huge pain... they cannot bubble through the
Packit 90a5c9
     * next several steps.  Only file subrequests are allowed an empty uri,
Packit 90a5c9
     * otherwise let translate_name kill the request.
Packit 90a5c9
     */
Packit 90a5c9
    if (!file_req) {
Packit 90a5c9
        if ((access_status = ap_location_walk(r))) {
Packit 90a5c9
            return access_status;
Packit 90a5c9
        }
Packit 90a5c9
        if ((access_status = ap_if_walk(r))) {
Packit 90a5c9
            return access_status;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        d = ap_get_core_module_config(r->per_dir_config);
Packit 90a5c9
        if (d->log) {
Packit 90a5c9
            r->log = d->log;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if ((access_status = ap_run_translate_name(r))) {
Packit 90a5c9
            return decl_die(access_status, "translate", r);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Reset to the server default config prior to running map_to_storage
Packit 90a5c9
     */
Packit 90a5c9
    r->per_dir_config = r->server->lookup_defaults;
Packit 90a5c9
Packit 90a5c9
    if ((access_status = ap_run_map_to_storage(r))) {
Packit 90a5c9
        /* This request wasn't in storage (e.g. TRACE) */
Packit 90a5c9
        return access_status;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Rerun the location walk, which overrides any map_to_storage config.
Packit 90a5c9
     */
Packit 90a5c9
    if ((access_status = ap_location_walk(r))) {
Packit 90a5c9
        return access_status;
Packit 90a5c9
    }
Packit 90a5c9
    if ((access_status = ap_if_walk(r))) {
Packit 90a5c9
        return access_status;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    d = ap_get_core_module_config(r->per_dir_config);
Packit 90a5c9
    if (d->log) {
Packit 90a5c9
        r->log = d->log;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((access_status = ap_run_post_perdir_config(r))) {
Packit 90a5c9
        return access_status;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Only on the main request! */
Packit 90a5c9
    if (r->main == NULL) {
Packit 90a5c9
        if ((access_status = ap_run_header_parser(r))) {
Packit 90a5c9
            return access_status;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Skip authn/authz if the parent or prior request passed the authn/authz,
Packit 90a5c9
     * and that configuration didn't change (this requires optimized _walk()
Packit 90a5c9
     * functions in map_to_storage that use the same merge results given
Packit 90a5c9
     * identical input.)  If the config changes, we must re-auth.
Packit 90a5c9
     */
Packit 90a5c9
    if (r->prev && (r->prev->per_dir_config == r->per_dir_config)) {
Packit 90a5c9
        r->user = r->prev->user;
Packit 90a5c9
        r->ap_auth_type = r->prev->ap_auth_type;
Packit 90a5c9
    }
Packit 90a5c9
    else if (r->main && (r->main->per_dir_config == r->per_dir_config)) {
Packit 90a5c9
        r->user = r->main->user;
Packit 90a5c9
        r->ap_auth_type = r->main->ap_auth_type;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /* A module using a confusing API (ap_get_basic_auth_pw) caused
Packit 90a5c9
        ** r->user to be filled out prior to check_authn hook. We treat
Packit 90a5c9
        ** it is inadvertent.
Packit 90a5c9
        */
Packit 90a5c9
        if (r->user && apr_table_get(r->notes, AP_GET_BASIC_AUTH_PW_NOTE)) { 
Packit 90a5c9
            r->user = NULL;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        switch (ap_satisfies(r)) {
Packit 90a5c9
        case SATISFY_ALL:
Packit 90a5c9
        case SATISFY_NOSPEC:
Packit 90a5c9
            if ((access_status = ap_run_access_checker(r)) != OK) {
Packit 90a5c9
                return decl_die(access_status,
Packit 90a5c9
                                "check access (with Satisfy All)", r);
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            access_status = ap_run_access_checker_ex(r);
Packit 90a5c9
            if (access_status == DECLINED
Packit 90a5c9
                || (access_status == OK && ap_run_force_authn(r) == OK)) {
Packit 90a5c9
                if ((access_status = ap_run_check_user_id(r)) != OK) {
Packit 90a5c9
                    return decl_die(access_status, "check user", r);
Packit 90a5c9
                }
Packit 90a5c9
                if (r->user == NULL) {
Packit 90a5c9
                    /* don't let buggy authn module crash us in authz */
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00027)
Packit 90a5c9
                                  "No authentication done but request not "
Packit 90a5c9
                                  "allowed without authentication for %s. "
Packit 90a5c9
                                  "Authentication not configured?",
Packit 90a5c9
                                  r->uri);
Packit 90a5c9
                    access_status = HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
                    return decl_die(access_status, "check user", r);
Packit 90a5c9
                }
Packit 90a5c9
                if ((access_status = ap_run_auth_checker(r)) != OK) {
Packit 90a5c9
                    return decl_die(access_status, "check authorization", r);
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            else if (access_status == OK) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
Packit 90a5c9
                              "request authorized without authentication by "
Packit 90a5c9
                              "access_checker_ex hook: %s", r->uri);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                return decl_die(access_status, "check access", r);
Packit 90a5c9
            }
Packit 90a5c9
            break;
Packit 90a5c9
        case SATISFY_ANY:
Packit 90a5c9
            if ((access_status = ap_run_access_checker(r)) == OK) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
Packit 90a5c9
                              "request authorized without authentication by "
Packit 90a5c9
                              "access_checker hook and 'Satisfy any': %s",
Packit 90a5c9
                              r->uri);
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            access_status = ap_run_access_checker_ex(r);
Packit 90a5c9
            if (access_status == DECLINED
Packit 90a5c9
                || (access_status == OK && ap_run_force_authn(r) == OK)) {
Packit 90a5c9
                if ((access_status = ap_run_check_user_id(r)) != OK) {
Packit 90a5c9
                    return decl_die(access_status, "check user", r);
Packit 90a5c9
                }
Packit 90a5c9
                if (r->user == NULL) {
Packit 90a5c9
                    /* don't let buggy authn module crash us in authz */
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00028)
Packit 90a5c9
                                  "No authentication done but request not "
Packit 90a5c9
                                  "allowed without authentication for %s. "
Packit 90a5c9
                                  "Authentication not configured?",
Packit 90a5c9
                                  r->uri);
Packit 90a5c9
                    access_status = HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
                    return decl_die(access_status, "check user", r);
Packit 90a5c9
                }
Packit 90a5c9
                if ((access_status = ap_run_auth_checker(r)) != OK) {
Packit 90a5c9
                    return decl_die(access_status, "check authorization", r);
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            else if (access_status == OK) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r,
Packit 90a5c9
                              "request authorized without authentication by "
Packit 90a5c9
                              "access_checker_ex hook: %s", r->uri);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                return decl_die(access_status, "check access", r);
Packit 90a5c9
            }
Packit 90a5c9
            break;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    /* XXX Must make certain the ap_run_type_checker short circuits mime
Packit 90a5c9
     * in mod-proxy for r->proxyreq && r->parsed_uri.scheme
Packit 90a5c9
     *                              && !strcmp(r->parsed_uri.scheme, "http")
Packit 90a5c9
     */
Packit 90a5c9
    if ((access_status = ap_run_type_checker(r)) != OK) {
Packit 90a5c9
        return decl_die(access_status, "find types", r);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((access_status = ap_run_fixups(r)) != OK) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_TRACE3, 0, r, "fixups hook gave %d: %s",
Packit 90a5c9
                      access_status, r->uri);
Packit 90a5c9
        return access_status;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/* Useful caching structures to repeat _walk/merge sequences as required
Packit 90a5c9
 * when a subrequest or redirect reuses substantially the same config.
Packit 90a5c9
 *
Packit 90a5c9
 * Directive order in the httpd.conf file and its Includes significantly
Packit 90a5c9
 * impact this optimization.  Grouping common blocks at the front of the
Packit 90a5c9
 * config that are less likely to change between a request and
Packit 90a5c9
 * its subrequests, or between a request and its redirects reduced
Packit 90a5c9
 * the work of these functions significantly.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
typedef struct walk_walked_t {
Packit 90a5c9
    ap_conf_vector_t *matched; /* A dir_conf sections we matched */
Packit 90a5c9
    ap_conf_vector_t *merged;  /* The dir_conf merged result */
Packit 90a5c9
} walk_walked_t;
Packit 90a5c9
Packit 90a5c9
typedef struct walk_cache_t {
Packit 90a5c9
    const char         *cached;          /* The identifier we matched */
Packit 90a5c9
    ap_conf_vector_t  **dir_conf_tested; /* The sections we matched against */
Packit 90a5c9
    ap_conf_vector_t   *dir_conf_merged; /* Base per_dir_config */
Packit 90a5c9
    ap_conf_vector_t   *per_dir_result;  /* per_dir_config += walked result */
Packit 90a5c9
    apr_array_header_t *walked;          /* The list of walk_walked_t results */
Packit 90a5c9
    struct walk_cache_t *prev; /* Prev cache of same call in this (sub)req */
Packit 90a5c9
    int count; /* Number of prev invocations of same call in this (sub)req */
Packit 90a5c9
} walk_cache_t;
Packit 90a5c9
Packit 90a5c9
static walk_cache_t *prep_walk_cache(apr_size_t t, request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    void **note, **inherit_note;
Packit 90a5c9
    walk_cache_t *cache, *prev_cache, *copy_cache;
Packit 90a5c9
    int count;
Packit 90a5c9
Packit 90a5c9
    /* Find the most relevant, recent walk cache to work from and provide
Packit 90a5c9
     * a copy the caller is allowed to munge.  In the case of a sub-request
Packit 90a5c9
     * or internal redirect, this is the cache corresponding to the equivalent
Packit 90a5c9
     * invocation of the same function call in the "parent" request, if such
Packit 90a5c9
     * a cache exists.  Otherwise it is the walk cache of the previous
Packit 90a5c9
     * invocation of the same function call in the current request, if
Packit 90a5c9
     * that exists; if not, then create a new walk cache.
Packit 90a5c9
     */
Packit 90a5c9
    note = ap_get_request_note(r, t);
Packit 90a5c9
    AP_DEBUG_ASSERT(note != NULL);
Packit 90a5c9
Packit 90a5c9
    copy_cache = prev_cache = *note;
Packit 90a5c9
    count = prev_cache ? (prev_cache->count + 1) : 0;
Packit 90a5c9
Packit 90a5c9
    if ((r->prev
Packit 90a5c9
         && (inherit_note = ap_get_request_note(r->prev, t))
Packit 90a5c9
         && *inherit_note)
Packit 90a5c9
        || (r->main
Packit 90a5c9
            && (inherit_note = ap_get_request_note(r->main, t))
Packit 90a5c9
            && *inherit_note)) {
Packit 90a5c9
        walk_cache_t *inherit_cache = *inherit_note;
Packit 90a5c9
Packit 90a5c9
        while (inherit_cache->count > count) {
Packit 90a5c9
            inherit_cache = inherit_cache->prev;
Packit 90a5c9
        }
Packit 90a5c9
        if (inherit_cache->count == count) {
Packit 90a5c9
            copy_cache = inherit_cache;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (copy_cache) {
Packit 90a5c9
        cache = apr_pmemdup(r->pool, copy_cache, sizeof(*cache));
Packit 90a5c9
        cache->walked = apr_array_copy(r->pool, cache->walked);
Packit 90a5c9
        cache->prev = prev_cache;
Packit 90a5c9
        cache->count = count;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        cache = apr_pcalloc(r->pool, sizeof(*cache));
Packit 90a5c9
        cache->walked = apr_array_make(r->pool, 4, sizeof(walk_walked_t));
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    *note = cache;
Packit 90a5c9
Packit 90a5c9
    return cache;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*****************************************************************
Packit 90a5c9
 *
Packit 90a5c9
 * Getting and checking directory configuration.  Also checks the
Packit 90a5c9
 * FollowSymlinks and FollowSymOwner stuff, since this is really the
Packit 90a5c9
 * only place that can happen (barring a new mid_dir_walk callout).
Packit 90a5c9
 *
Packit 90a5c9
 * We can't do it as an access_checker module function which gets
Packit 90a5c9
 * called with the final per_dir_config, since we could have a directory
Packit 90a5c9
 * with FollowSymLinks disabled, which contains a symlink to another
Packit 90a5c9
 * with a .htaccess file which turns FollowSymLinks back on --- and
Packit 90a5c9
 * access in such a case must be denied.  So, whatever it is that
Packit 90a5c9
 * checks FollowSymLinks needs to know the state of the options as
Packit 90a5c9
 * they change, all the way down.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * resolve_symlink must _always_ be called on an APR_LNK file type!
Packit 90a5c9
 * It will resolve the actual target file type, modification date, etc,
Packit 90a5c9
 * and provide any processing required for symlink evaluation.
Packit 90a5c9
 * Path must already be cleaned, no trailing slash, no multi-slashes,
Packit 90a5c9
 * and don't call this on the root!
Packit 90a5c9
 *
Packit 90a5c9
 * Simply, the number of times we deref a symlink are minimal compared
Packit 90a5c9
 * to the number of times we had an extra lstat() since we 'weren't sure'.
Packit 90a5c9
 *
Packit 90a5c9
 * To optimize, we stat() anything when given (opts & OPT_SYM_LINKS), otherwise
Packit 90a5c9
 * we start off with an lstat().  Every lstat() must be dereferenced in case
Packit 90a5c9
 * it points at a 'nasty' - we must always rerun check_safe_file (or similar.)
Packit 90a5c9
 */
Packit 90a5c9
static int resolve_symlink(char *d, apr_finfo_t *lfi, int opts, apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    apr_finfo_t fi;
Packit 90a5c9
    const char *savename;
Packit 90a5c9
Packit 90a5c9
    if (!(opts & (OPT_SYM_OWNER | OPT_SYM_LINKS))) {
Packit 90a5c9
        return HTTP_FORBIDDEN;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Save the name from the valid bits. */
Packit 90a5c9
    savename = (lfi->valid & APR_FINFO_NAME) ? lfi->name : NULL;
Packit 90a5c9
Packit 90a5c9
    /* if OPT_SYM_OWNER is unset, we only need to check target accessible */
Packit 90a5c9
    if (!(opts & OPT_SYM_OWNER)) {
Packit 90a5c9
        if (apr_stat(&fi, d, lfi->valid & ~(APR_FINFO_NAME | APR_FINFO_LINK), p)
Packit 90a5c9
            != APR_SUCCESS)
Packit 90a5c9
        {
Packit 90a5c9
            return HTTP_FORBIDDEN;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Give back the target */
Packit 90a5c9
        memcpy(lfi, &fi, sizeof(fi));
Packit 90a5c9
        if (savename) {
Packit 90a5c9
            lfi->name = savename;
Packit 90a5c9
            lfi->valid |= APR_FINFO_NAME;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* OPT_SYM_OWNER only works if we can get the owner of
Packit 90a5c9
     * both the file and symlink.  First fill in a missing
Packit 90a5c9
     * owner of the symlink, then get the info of the target.
Packit 90a5c9
     */
Packit 90a5c9
    if (!(lfi->valid & APR_FINFO_OWNER)) {
Packit 90a5c9
        if (apr_stat(lfi, d, lfi->valid | APR_FINFO_LINK | APR_FINFO_OWNER, p)
Packit 90a5c9
            != APR_SUCCESS)
Packit 90a5c9
        {
Packit 90a5c9
            return HTTP_FORBIDDEN;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (apr_stat(&fi, d, lfi->valid & ~(APR_FINFO_NAME), p) != APR_SUCCESS) {
Packit 90a5c9
        return HTTP_FORBIDDEN;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (apr_uid_compare(fi.user, lfi->user) != APR_SUCCESS) {
Packit 90a5c9
        return HTTP_FORBIDDEN;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Give back the target */
Packit 90a5c9
    memcpy(lfi, &fi, sizeof(fi));
Packit 90a5c9
    if (savename) {
Packit 90a5c9
        lfi->name = savename;
Packit 90a5c9
        lfi->valid |= APR_FINFO_NAME;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * As we walk the directory configuration, the merged config won't
Packit 90a5c9
 * be 'rooted' to a specific vhost until the very end of the merge.
Packit 90a5c9
 *
Packit 90a5c9
 * We need a very fast mini-merge to a real, vhost-rooted merge
Packit 90a5c9
 * of core.opts and core.override, the only options tested within
Packit 90a5c9
 * directory_walk itself.
Packit 90a5c9
 *
Packit 90a5c9
 * See core.c::merge_core_dir_configs() for explanation.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
typedef struct core_opts_t {
Packit 90a5c9
        allow_options_t opts;
Packit 90a5c9
        allow_options_t add;
Packit 90a5c9
        allow_options_t remove;
Packit 90a5c9
        overrides_t override;
Packit 90a5c9
        overrides_t override_opts;
Packit 90a5c9
        apr_table_t *override_list;
Packit 90a5c9
} core_opts_t;
Packit 90a5c9
Packit 90a5c9
static void core_opts_merge(const ap_conf_vector_t *sec, core_opts_t *opts)
Packit 90a5c9
{
Packit 90a5c9
    core_dir_config *this_dir = ap_get_core_module_config(sec);
Packit 90a5c9
Packit 90a5c9
    if (!this_dir) {
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (this_dir->opts & OPT_UNSET) {
Packit 90a5c9
        opts->add = (opts->add & ~this_dir->opts_remove)
Packit 90a5c9
                   | this_dir->opts_add;
Packit 90a5c9
        opts->remove = (opts->remove & ~this_dir->opts_add)
Packit 90a5c9
                      | this_dir->opts_remove;
Packit 90a5c9
        opts->opts = (opts->opts & ~opts->remove) | opts->add;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        opts->opts = this_dir->opts;
Packit 90a5c9
        opts->add = this_dir->opts_add;
Packit 90a5c9
        opts->remove = this_dir->opts_remove;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (!(this_dir->override & OR_UNSET)) {
Packit 90a5c9
        opts->override = this_dir->override;
Packit 90a5c9
        opts->override_opts = this_dir->override_opts;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (this_dir->override_list != NULL) {
Packit 90a5c9
        opts->override_list = this_dir->override_list;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
/*****************************************************************
Packit 90a5c9
 *
Packit 90a5c9
 * Getting and checking directory configuration.  Also checks the
Packit 90a5c9
 * FollowSymlinks and FollowSymOwner stuff, since this is really the
Packit 90a5c9
 * only place that can happen (barring a new mid_dir_walk callout).
Packit 90a5c9
 *
Packit 90a5c9
 * We can't do it as an access_checker module function which gets
Packit 90a5c9
 * called with the final per_dir_config, since we could have a directory
Packit 90a5c9
 * with FollowSymLinks disabled, which contains a symlink to another
Packit 90a5c9
 * with a .htaccess file which turns FollowSymLinks back on --- and
Packit 90a5c9
 * access in such a case must be denied.  So, whatever it is that
Packit 90a5c9
 * checks FollowSymLinks needs to know the state of the options as
Packit 90a5c9
 * they change, all the way down.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_directory_walk(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ap_conf_vector_t *now_merged = NULL;
Packit 90a5c9
    core_server_config *sconf =
Packit 90a5c9
        ap_get_core_module_config(r->server->module_config);
Packit 90a5c9
    ap_conf_vector_t **sec_ent = (ap_conf_vector_t **) sconf->sec_dir->elts;
Packit 90a5c9
    int num_sec = sconf->sec_dir->nelts;
Packit 90a5c9
    walk_cache_t *cache;
Packit 90a5c9
    char *entry_dir;
Packit 90a5c9
    apr_status_t rv;
Packit 90a5c9
    int cached;
Packit 90a5c9
Packit 90a5c9
    /* XXX: Better (faster) tests needed!!!
Packit 90a5c9
     *
Packit 90a5c9
     * "OK" as a response to a real problem is not _OK_, but to allow broken
Packit 90a5c9
     * modules to proceed, we will permit the not-a-path filename to pass the
Packit 90a5c9
     * following two tests.  This behavior may be revoked in future versions
Packit 90a5c9
     * of Apache.  We still must catch it later if it's heading for the core
Packit 90a5c9
     * handler.  Leave INFO notes here for module debugging.
Packit 90a5c9
     */
Packit 90a5c9
    if (r->filename == NULL) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00029)
Packit 90a5c9
                      "Module bug?  Request filename is missing for URI %s",
Packit 90a5c9
                      r->uri);
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Canonicalize the file path without resolving filename case or aliases
Packit 90a5c9
     * so we can begin by checking the cache for a recent directory walk.
Packit 90a5c9
     * This call will ensure we have an absolute path in the same pass.
Packit 90a5c9
     */
Packit 90a5c9
    if ((rv = apr_filepath_merge(&entry_dir, NULL, r->filename,
Packit 90a5c9
                                 APR_FILEPATH_NOTRELATIVE, r->pool))
Packit 90a5c9
                  != APR_SUCCESS) {
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(00030)
Packit 90a5c9
                      "Module bug?  Request filename path %s is invalid or "
Packit 90a5c9
                      "or not absolute for uri %s",
Packit 90a5c9
                      r->filename, r->uri);
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* XXX Notice that this forces path_info to be canonical.  That might
Packit 90a5c9
     * not be desired by all apps.  However, some of those same apps likely
Packit 90a5c9
     * have significant security holes.
Packit 90a5c9
     */
Packit 90a5c9
    r->filename = entry_dir;
Packit 90a5c9
Packit 90a5c9
    cache = prep_walk_cache(AP_NOTE_DIRECTORY_WALK, r);
Packit 90a5c9
    cached = (cache->cached != NULL);
Packit 90a5c9
Packit 90a5c9
    /* If this is not a dirent subrequest with a preconstructed
Packit 90a5c9
     * r->finfo value, then we can simply stat the filename to
Packit 90a5c9
     * save burning mega-cycles with unneeded stats - if this is
Packit 90a5c9
     * an exact file match.  We don't care about failure... we
Packit 90a5c9
     * will stat by component failing this meager attempt.
Packit 90a5c9
     *
Packit 90a5c9
     * It would be nice to distinguish APR_ENOENT from other
Packit 90a5c9
     * types of failure, such as APR_ENOTDIR.  We can do something
Packit 90a5c9
     * with APR_ENOENT, knowing that the path is good.
Packit 90a5c9
     */
Packit 90a5c9
    if (r->finfo.filetype == APR_NOFILE || r->finfo.filetype == APR_LNK) {
Packit 90a5c9
        rv = ap_run_dirwalk_stat(&r->finfo, r, APR_FINFO_MIN);
Packit 90a5c9
Packit 90a5c9
        /* some OSs will return APR_SUCCESS/APR_REG if we stat
Packit 90a5c9
         * a regular file but we have '/' at the end of the name;
Packit 90a5c9
         *
Packit 90a5c9
         * other OSs will return APR_ENOTDIR for that situation;
Packit 90a5c9
         *
Packit 90a5c9
         * handle it the same everywhere by simulating a failure
Packit 90a5c9
         * if it looks like a directory but really isn't
Packit 90a5c9
         *
Packit 90a5c9
         * Also reset if the stat failed, just for safety.
Packit 90a5c9
         */
Packit 90a5c9
        if ((rv != APR_SUCCESS) ||
Packit 90a5c9
            (r->finfo.filetype != APR_NOFILE &&
Packit 90a5c9
             (r->finfo.filetype != APR_DIR) &&
Packit 90a5c9
             (r->filename[strlen(r->filename) - 1] == '/'))) {
Packit 90a5c9
             r->finfo.filetype = APR_NOFILE; /* forget what we learned */
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (r->finfo.filetype == APR_REG) {
Packit 90a5c9
        entry_dir = ap_make_dirstr_parent(r->pool, entry_dir);
Packit 90a5c9
    }
Packit 90a5c9
    else if (r->filename[strlen(r->filename) - 1] != '/') {
Packit 90a5c9
        entry_dir = apr_pstrcat(r->pool, r->filename, "/", NULL);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* If we have a file already matches the path of r->filename,
Packit 90a5c9
     * and the vhost's list of directory sections hasn't changed,
Packit 90a5c9
     * we can skip rewalking the directory_walk entries.
Packit 90a5c9
     */
Packit 90a5c9
    if (cached
Packit 90a5c9
        && ((r->finfo.filetype == APR_REG)
Packit 90a5c9
            || ((r->finfo.filetype == APR_DIR)
Packit 90a5c9
                && (!r->path_info || !*r->path_info)))
Packit 90a5c9
        && (cache->dir_conf_tested == sec_ent)
Packit 90a5c9
        && (strcmp(entry_dir, cache->cached) == 0)) {
Packit 90a5c9
        int familiar = 0;
Packit 90a5c9
Packit 90a5c9
        /* Well this looks really familiar!  If our end-result (per_dir_result)
Packit 90a5c9
         * didn't change, we have absolutely nothing to do :)
Packit 90a5c9
         * Otherwise (as is the case with most dir_merged/file_merged requests)
Packit 90a5c9
         * we must merge our dir_conf_merged onto this new r->per_dir_config.
Packit 90a5c9
         */
Packit 90a5c9
        if (r->per_dir_config == cache->per_dir_result) {
Packit 90a5c9
            familiar = 1;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (r->per_dir_config == cache->dir_conf_merged) {
Packit 90a5c9
            r->per_dir_config = cache->per_dir_result;
Packit 90a5c9
            familiar = 1;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (familiar) {
Packit 90a5c9
            apr_finfo_t thisinfo;
Packit 90a5c9
            int res;
Packit 90a5c9
            allow_options_t opts;
Packit 90a5c9
            core_dir_config *this_dir;
Packit 90a5c9
Packit 90a5c9
            this_dir = ap_get_core_module_config(r->per_dir_config);
Packit 90a5c9
            opts = this_dir->opts;
Packit 90a5c9
            /*
Packit 90a5c9
             * If Symlinks are allowed in general we do not need the following
Packit 90a5c9
             * check.
Packit 90a5c9
             */
Packit 90a5c9
            if (!(opts & OPT_SYM_LINKS)) {
Packit 90a5c9
                rv = ap_run_dirwalk_stat(&thisinfo, r,
Packit 90a5c9
                                         APR_FINFO_MIN | APR_FINFO_NAME | APR_FINFO_LINK);
Packit 90a5c9
                /*
Packit 90a5c9
                 * APR_INCOMPLETE is as fine as result as APR_SUCCESS as we
Packit 90a5c9
                 * have added APR_FINFO_NAME to the wanted parameter of
Packit 90a5c9
                 * apr_stat above. On Unix platforms this means that apr_stat
Packit 90a5c9
                 * is always going to return APR_INCOMPLETE in the case that
Packit 90a5c9
                 * the call to the native stat / lstat did not fail.
Packit 90a5c9
                 */
Packit 90a5c9
                if ((rv != APR_INCOMPLETE) && (rv != APR_SUCCESS)) {
Packit 90a5c9
                    /*
Packit 90a5c9
                     * This should never happen, because we did a stat on the
Packit 90a5c9
                     * same file, resolving a possible symlink several lines
Packit 90a5c9
                     * above. Therefore do not make a detailed analysis of rv
Packit 90a5c9
                     * in this case for the reason of the failure, just bail out
Packit 90a5c9
                     * with a HTTP_FORBIDDEN in case we hit a race condition
Packit 90a5c9
                     * here.
Packit 90a5c9
                     */
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00031)
Packit 90a5c9
                                  "access to %s failed; stat of '%s' failed.",
Packit 90a5c9
                                  r->uri, r->filename);
Packit 90a5c9
                    return r->status = HTTP_FORBIDDEN;
Packit 90a5c9
                }
Packit 90a5c9
                if (thisinfo.filetype == APR_LNK) {
Packit 90a5c9
                    /* Is this a possibly acceptable symlink? */
Packit 90a5c9
                    if ((res = resolve_symlink(r->filename, &thisinfo,
Packit 90a5c9
                                               opts, r->pool)) != OK) {
Packit 90a5c9
                        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00032)
Packit 90a5c9
                                      "Symbolic link not allowed "
Packit 90a5c9
                                      "or link target not accessible: %s",
Packit 90a5c9
                                      r->filename);
Packit 90a5c9
                        return r->status = res;
Packit 90a5c9
                    }
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
            return OK;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (cache->walked->nelts) {
Packit 90a5c9
            now_merged = ((walk_walked_t*)cache->walked->elts)
Packit 90a5c9
                [cache->walked->nelts - 1].merged;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /* We start now_merged from NULL since we want to build
Packit 90a5c9
         * a locations list that can be merged to any vhost.
Packit 90a5c9
         */
Packit 90a5c9
        int sec_idx;
Packit 90a5c9
        int matches = cache->walked->nelts;
Packit 90a5c9
        int cached_matches = matches;
Packit 90a5c9
        walk_walked_t *last_walk = (walk_walked_t*)cache->walked->elts;
Packit 90a5c9
        core_dir_config *this_dir;
Packit 90a5c9
        core_opts_t opts;
Packit 90a5c9
        apr_finfo_t thisinfo;
Packit 90a5c9
        char *save_path_info;
Packit 90a5c9
        apr_size_t buflen;
Packit 90a5c9
        char *buf;
Packit 90a5c9
        unsigned int seg, startseg;
Packit 90a5c9
        apr_pool_t *rxpool = NULL;
Packit 90a5c9
Packit 90a5c9
        /* Invariant: from the first time filename_len is set until
Packit 90a5c9
         * it goes out of scope, filename_len==strlen(r->filename)
Packit 90a5c9
         */
Packit 90a5c9
        apr_size_t filename_len;
Packit 90a5c9
#ifdef CASE_BLIND_FILESYSTEM
Packit 90a5c9
        apr_size_t canonical_len;
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
        cached &= auth_internal_per_conf;
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * We must play our own mini-merge game here, for the few
Packit 90a5c9
         * running dir_config values we care about within dir_walk.
Packit 90a5c9
         * We didn't start the merge from r->per_dir_config, so we
Packit 90a5c9
         * accumulate opts and override as we merge, from the globals.
Packit 90a5c9
         */
Packit 90a5c9
        this_dir = ap_get_core_module_config(r->per_dir_config);
Packit 90a5c9
        opts.opts = this_dir->opts;
Packit 90a5c9
        opts.add = this_dir->opts_add;
Packit 90a5c9
        opts.remove = this_dir->opts_remove;
Packit 90a5c9
        opts.override = this_dir->override;
Packit 90a5c9
        opts.override_opts = this_dir->override_opts;
Packit 90a5c9
        opts.override_list = this_dir->override_list;
Packit 90a5c9
Packit 90a5c9
        /* Set aside path_info to merge back onto path_info later.
Packit 90a5c9
         * If r->filename is a directory, we must remerge the path_info,
Packit 90a5c9
         * before we continue!  [Directories cannot, by definition, have
Packit 90a5c9
         * path info.  Either the next segment is not-found, or a file.]
Packit 90a5c9
         *
Packit 90a5c9
         * r->path_info tracks the unconsumed source path.
Packit 90a5c9
         * r->filename  tracks the path as we process it
Packit 90a5c9
         */
Packit 90a5c9
        if ((r->finfo.filetype == APR_DIR) && r->path_info && *r->path_info)
Packit 90a5c9
        {
Packit 90a5c9
            if ((rv = apr_filepath_merge(&r->path_info, r->filename,
Packit 90a5c9
                                         r->path_info,
Packit 90a5c9
                                         APR_FILEPATH_NOTABOVEROOT, r->pool))
Packit 90a5c9
                != APR_SUCCESS) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00033)
Packit 90a5c9
                              "dir_walk error, path_info %s is not relative "
Packit 90a5c9
                              "to the filename path %s for uri %s",
Packit 90a5c9
                              r->path_info, r->filename, r->uri);
Packit 90a5c9
                return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            save_path_info = NULL;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            save_path_info = r->path_info;
Packit 90a5c9
            r->path_info = r->filename;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
#ifdef CASE_BLIND_FILESYSTEM
Packit 90a5c9
Packit 90a5c9
        canonical_len = 0;
Packit 90a5c9
        while (r->canonical_filename && r->canonical_filename[canonical_len]
Packit 90a5c9
               && (r->canonical_filename[canonical_len]
Packit 90a5c9
                   == r->path_info[canonical_len])) {
Packit 90a5c9
             ++canonical_len;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        while (canonical_len
Packit 90a5c9
               && ((r->canonical_filename[canonical_len - 1] != '/'
Packit 90a5c9
                   && r->canonical_filename[canonical_len - 1])
Packit 90a5c9
                   || (r->path_info[canonical_len - 1] != '/'
Packit 90a5c9
                       && r->path_info[canonical_len - 1]))) {
Packit 90a5c9
            --canonical_len;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * Now build r->filename component by component, starting
Packit 90a5c9
         * with the root (on Unix, simply "/").  We will make a huge
Packit 90a5c9
         * assumption here for efficiency, that any canonical path
Packit 90a5c9
         * already given included a canonical root.
Packit 90a5c9
         */
Packit 90a5c9
        rv = apr_filepath_root((const char **)&r->filename,
Packit 90a5c9
                               (const char **)&r->path_info,
Packit 90a5c9
                               canonical_len ? 0 : APR_FILEPATH_TRUENAME,
Packit 90a5c9
                               r->pool);
Packit 90a5c9
        filename_len = strlen(r->filename);
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * Bad assumption above?  If the root's length is longer
Packit 90a5c9
         * than the canonical length, then it cannot be trusted as
Packit 90a5c9
         * a truename.  So try again, this time more seriously.
Packit 90a5c9
         */
Packit 90a5c9
        if ((rv == APR_SUCCESS) && canonical_len
Packit 90a5c9
            && (filename_len > canonical_len)) {
Packit 90a5c9
            rv = apr_filepath_root((const char **)&r->filename,
Packit 90a5c9
                                   (const char **)&r->path_info,
Packit 90a5c9
                                   APR_FILEPATH_TRUENAME, r->pool);
Packit 90a5c9
            filename_len = strlen(r->filename);
Packit 90a5c9
            canonical_len = 0;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
#else /* ndef CASE_BLIND_FILESYSTEM, really this simple for Unix today; */
Packit 90a5c9
Packit 90a5c9
        rv = apr_filepath_root((const char **)&r->filename,
Packit 90a5c9
                               (const char **)&r->path_info,
Packit 90a5c9
                               0, r->pool);
Packit 90a5c9
        filename_len = strlen(r->filename);
Packit 90a5c9
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
        if (rv != APR_SUCCESS) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00034)
Packit 90a5c9
                          "dir_walk error, could not determine the root "
Packit 90a5c9
                          "path of filename %s%s for uri %s",
Packit 90a5c9
                          r->filename, r->path_info, r->uri);
Packit 90a5c9
            return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Working space for terminating null and an extra / is required.
Packit 90a5c9
         */
Packit 90a5c9
        buflen = filename_len + strlen(r->path_info) + 2;
Packit 90a5c9
        buf = apr_palloc(r->pool, buflen);
Packit 90a5c9
        memcpy(buf, r->filename, filename_len + 1);
Packit 90a5c9
        r->filename = buf;
Packit 90a5c9
        thisinfo.valid = APR_FINFO_TYPE;
Packit 90a5c9
        thisinfo.filetype = APR_DIR; /* It's the root, of course it's a dir */
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * seg keeps track of which segment we've copied.
Packit 90a5c9
         * sec_idx keeps track of which section we're on, since sections are
Packit 90a5c9
         *     ordered by number of segments. See core_reorder_directories
Packit 90a5c9
         * startseg tells us how many segments describe the root path
Packit 90a5c9
         *     e.g. the complete path "//host/foo/" to a UNC share (4)
Packit 90a5c9
         */
Packit 90a5c9
        startseg = seg = ap_count_dirs(r->filename);
Packit 90a5c9
        sec_idx = 0;
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * Go down the directory hierarchy.  Where we have to check for
Packit 90a5c9
         * symlinks, do so.  Where a .htaccess file has permission to
Packit 90a5c9
         * override anything, try to find one.
Packit 90a5c9
         */
Packit 90a5c9
        do {
Packit 90a5c9
            int res;
Packit 90a5c9
            char *seg_name;
Packit 90a5c9
            char *delim;
Packit 90a5c9
            int temp_slash=0;
Packit 90a5c9
Packit 90a5c9
            /* We have no trailing slash, but we sure would appreciate one.
Packit 90a5c9
             * However, we don't want to append a / our first time through.
Packit 90a5c9
             */
Packit 90a5c9
            if ((seg > startseg) && r->filename[filename_len-1] != '/') {
Packit 90a5c9
                r->filename[filename_len++] = '/';
Packit 90a5c9
                r->filename[filename_len] = 0;
Packit 90a5c9
                temp_slash=1;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Begin *this* level by looking for matching <Directory> sections
Packit 90a5c9
             * from the server config.
Packit 90a5c9
             */
Packit 90a5c9
            for (; sec_idx < num_sec; ++sec_idx) {
Packit 90a5c9
Packit 90a5c9
                ap_conf_vector_t *entry_config = sec_ent[sec_idx];
Packit 90a5c9
                core_dir_config *entry_core;
Packit 90a5c9
                entry_core = ap_get_core_module_config(entry_config);
Packit 90a5c9
Packit 90a5c9
                /* No more possible matches for this many segments?
Packit 90a5c9
                 * We are done when we find relative/regex/longer components.
Packit 90a5c9
                 */
Packit 90a5c9
                if (entry_core->r || entry_core->d_components > seg) {
Packit 90a5c9
                    break;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                /* We will never skip '0' element components, e.g. plain old
Packit 90a5c9
                 * <Directory >, and <Directory "/"> are classified as zero
Packit 90a5c9
                 * so that Win32/Netware/OS2 etc all pick them up.
Packit 90a5c9
                 * Otherwise, skip over the mismatches.
Packit 90a5c9
                 */
Packit 90a5c9
                if (entry_core->d_components
Packit 90a5c9
                    && ((entry_core->d_components < seg)
Packit 90a5c9
                     || (entry_core->d_is_fnmatch
Packit 90a5c9
                         ? (apr_fnmatch(entry_core->d, r->filename,
Packit 90a5c9
                                        APR_FNM_PATHNAME) != APR_SUCCESS)
Packit 90a5c9
                         : (strcmp(r->filename, entry_core->d) != 0)))) {
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                /* If we haven't continue'd above, we have a match.
Packit 90a5c9
                 *
Packit 90a5c9
                 * Calculate our full-context core opts & override.
Packit 90a5c9
                 */
Packit 90a5c9
                core_opts_merge(sec_ent[sec_idx], &opts);
Packit 90a5c9
Packit 90a5c9
                /* If we merged this same section last time, reuse it
Packit 90a5c9
                 */
Packit 90a5c9
                if (matches) {
Packit 90a5c9
                    if (last_walk->matched == sec_ent[sec_idx]) {
Packit 90a5c9
                        now_merged = last_walk->merged;
Packit 90a5c9
                        ++last_walk;
Packit 90a5c9
                        --matches;
Packit 90a5c9
                        continue;
Packit 90a5c9
                    }
Packit 90a5c9
Packit 90a5c9
                    /* We fell out of sync.  This is our own copy of walked,
Packit 90a5c9
                     * so truncate the remaining matches and reset remaining.
Packit 90a5c9
                     */
Packit 90a5c9
                    cache->walked->nelts -= matches;
Packit 90a5c9
                    matches = 0;
Packit 90a5c9
                    cached = 0;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                if (now_merged) {
Packit 90a5c9
                    now_merged = ap_merge_per_dir_configs(r->pool,
Packit 90a5c9
                                                          now_merged,
Packit 90a5c9
                                                          sec_ent[sec_idx]);
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    now_merged = sec_ent[sec_idx];
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                last_walk = (walk_walked_t*)apr_array_push(cache->walked);
Packit 90a5c9
                last_walk->matched = sec_ent[sec_idx];
Packit 90a5c9
                last_walk->merged = now_merged;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* If .htaccess files are enabled, check for one, provided we
Packit 90a5c9
             * have reached a real path.
Packit 90a5c9
             */
Packit 90a5c9
            do {  /* Not really a loop, just a break'able code block */
Packit 90a5c9
Packit 90a5c9
                ap_conf_vector_t *htaccess_conf = NULL;
Packit 90a5c9
Packit 90a5c9
                /* No htaccess in an incomplete root path,
Packit 90a5c9
                 * nor if it's disabled
Packit 90a5c9
                 */
Packit 90a5c9
                if (seg < startseg || (!opts.override 
Packit 90a5c9
                    && apr_is_empty_table(opts.override_list)
Packit 90a5c9
                    )) {
Packit 90a5c9
                    break;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
                res = ap_parse_htaccess(&htaccess_conf, r, opts.override,
Packit 90a5c9
                                        opts.override_opts, opts.override_list,
Packit 90a5c9
                                        r->filename, sconf->access_name);
Packit 90a5c9
                if (res) {
Packit 90a5c9
                    return res;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                if (!htaccess_conf) {
Packit 90a5c9
                    break;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                /* If we are still here, we found our htaccess.
Packit 90a5c9
                 *
Packit 90a5c9
                 * Calculate our full-context core opts & override.
Packit 90a5c9
                 */
Packit 90a5c9
                core_opts_merge(htaccess_conf, &opts);
Packit 90a5c9
Packit 90a5c9
                /* If we merged this same htaccess last time, reuse it...
Packit 90a5c9
                 * this wouldn't work except that we cache the htaccess
Packit 90a5c9
                 * sections for the lifetime of the request, so we match
Packit 90a5c9
                 * the same conf.  Good planning (no, pure luck ;)
Packit 90a5c9
                 */
Packit 90a5c9
                if (matches) {
Packit 90a5c9
                    if (last_walk->matched == htaccess_conf) {
Packit 90a5c9
                        now_merged = last_walk->merged;
Packit 90a5c9
                        ++last_walk;
Packit 90a5c9
                        --matches;
Packit 90a5c9
                        break;
Packit 90a5c9
                    }
Packit 90a5c9
Packit 90a5c9
                    /* We fell out of sync.  This is our own copy of walked,
Packit 90a5c9
                     * so truncate the remaining matches and reset
Packit 90a5c9
                     * remaining.
Packit 90a5c9
                     */
Packit 90a5c9
                    cache->walked->nelts -= matches;
Packit 90a5c9
                    matches = 0;
Packit 90a5c9
                    cached = 0;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                if (now_merged) {
Packit 90a5c9
                    now_merged = ap_merge_per_dir_configs(r->pool,
Packit 90a5c9
                                                          now_merged,
Packit 90a5c9
                                                          htaccess_conf);
Packit 90a5c9
                }
Packit 90a5c9
                else {
Packit 90a5c9
                    now_merged = htaccess_conf;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                last_walk = (walk_walked_t*)apr_array_push(cache->walked);
Packit 90a5c9
                last_walk->matched = htaccess_conf;
Packit 90a5c9
                last_walk->merged = now_merged;
Packit 90a5c9
Packit 90a5c9
            } while (0); /* Only one htaccess, not a real loop */
Packit 90a5c9
Packit 90a5c9
            /* That temporary trailing slash was useful, now drop it.
Packit 90a5c9
             */
Packit 90a5c9
            if (temp_slash) {
Packit 90a5c9
                r->filename[--filename_len] = '\0';
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Time for all good things to come to an end?
Packit 90a5c9
             */
Packit 90a5c9
            if (!r->path_info || !*r->path_info) {
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Now it's time for the next segment...
Packit 90a5c9
             * We will assume the next element is an end node, and fix it up
Packit 90a5c9
             * below as necessary...
Packit 90a5c9
             */
Packit 90a5c9
Packit 90a5c9
            seg_name = r->filename + filename_len;
Packit 90a5c9
            delim = strchr(r->path_info + (*r->path_info == '/' ? 1 : 0), '/');
Packit 90a5c9
            if (delim) {
Packit 90a5c9
                apr_size_t path_info_len = delim - r->path_info;
Packit 90a5c9
                *delim = '\0';
Packit 90a5c9
                memcpy(seg_name, r->path_info, path_info_len + 1);
Packit 90a5c9
                filename_len += path_info_len;
Packit 90a5c9
                r->path_info = delim;
Packit 90a5c9
                *delim = '/';
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                apr_size_t path_info_len = strlen(r->path_info);
Packit 90a5c9
                memcpy(seg_name, r->path_info, path_info_len + 1);
Packit 90a5c9
                filename_len += path_info_len;
Packit 90a5c9
                r->path_info += path_info_len;
Packit 90a5c9
            }
Packit 90a5c9
            if (*seg_name == '/')
Packit 90a5c9
                ++seg_name;
Packit 90a5c9
Packit 90a5c9
            /* If nothing remained but a '/' string, we are finished
Packit 90a5c9
             * XXX: NO WE ARE NOT!!!  Now process this puppy!!! */
Packit 90a5c9
            if (!*seg_name) {
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* First optimization;
Packit 90a5c9
             * If...we knew r->filename was a file, and
Packit 90a5c9
             * if...we have strict (case-sensitive) filenames, or
Packit 90a5c9
             *      we know the canonical_filename matches to _this_ name, and
Packit 90a5c9
             * if...we have allowed symlinks
Packit 90a5c9
             * skip the lstat and dummy up an APR_DIR value for thisinfo.
Packit 90a5c9
             */
Packit 90a5c9
            if (r->finfo.filetype != APR_NOFILE
Packit 90a5c9
#ifdef CASE_BLIND_FILESYSTEM
Packit 90a5c9
                && (filename_len <= canonical_len)
Packit 90a5c9
#endif
Packit 90a5c9
                && ((opts.opts & (OPT_SYM_OWNER | OPT_SYM_LINKS)) == OPT_SYM_LINKS))
Packit 90a5c9
            {
Packit 90a5c9
Packit 90a5c9
                thisinfo.filetype = APR_DIR;
Packit 90a5c9
                ++seg;
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* We choose apr_stat with flag APR_FINFO_LINK here, rather that
Packit 90a5c9
             * plain apr_stat, so that we capture this path object rather than
Packit 90a5c9
             * its target.  We will replace the info with our target's info
Packit 90a5c9
             * below.  We especially want the name of this 'link' object, not
Packit 90a5c9
             * the name of its target, if we are fixing the filename
Packit 90a5c9
             * case/resolving aliases.
Packit 90a5c9
             */
Packit 90a5c9
            rv = ap_run_dirwalk_stat(&thisinfo, r,
Packit 90a5c9
                                     APR_FINFO_MIN | APR_FINFO_NAME | APR_FINFO_LINK);
Packit 90a5c9
Packit 90a5c9
            if (APR_STATUS_IS_ENOENT(rv)) {
Packit 90a5c9
                /* Nothing?  That could be nice.  But our directory
Packit 90a5c9
                 * walk is done.
Packit 90a5c9
                 */
Packit 90a5c9
                thisinfo.filetype = APR_NOFILE;
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
            else if (APR_STATUS_IS_EACCES(rv)) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00035)
Packit 90a5c9
                              "access to %s denied (filesystem path '%s') "
Packit 90a5c9
                              "because search permissions are missing on a "
Packit 90a5c9
                              "component of the path", r->uri, r->filename);
Packit 90a5c9
                return r->status = HTTP_FORBIDDEN;
Packit 90a5c9
            }
Packit 90a5c9
            else if ((rv != APR_SUCCESS && rv != APR_INCOMPLETE)
Packit 90a5c9
                     || !(thisinfo.valid & APR_FINFO_TYPE)) {
Packit 90a5c9
                /* If we hit ENOTDIR, we must have over-optimized, deny
Packit 90a5c9
                 * rather than assume not found.
Packit 90a5c9
                 */
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(00036)
Packit 90a5c9
                              "access to %s failed (filesystem path '%s')", 
Packit 90a5c9
                              r->uri, r->filename);
Packit 90a5c9
                return r->status = HTTP_FORBIDDEN;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Fix up the path now if we have a name, and they don't agree
Packit 90a5c9
             */
Packit 90a5c9
            if ((thisinfo.valid & APR_FINFO_NAME)
Packit 90a5c9
                && strcmp(seg_name, thisinfo.name)) {
Packit 90a5c9
                /* TODO: provide users an option that an internal/external
Packit 90a5c9
                 * redirect is required here?  We need to walk the URI and
Packit 90a5c9
                 * filename in tandem to properly correlate these.
Packit 90a5c9
                 */
Packit 90a5c9
                strcpy(seg_name, thisinfo.name);
Packit 90a5c9
                filename_len = strlen(r->filename);
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (thisinfo.filetype == APR_LNK) {
Packit 90a5c9
                /* Is this a possibly acceptable symlink?
Packit 90a5c9
                 */
Packit 90a5c9
                if ((res = resolve_symlink(r->filename, &thisinfo,
Packit 90a5c9
                                           opts.opts, r->pool)) != OK) {
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00037)
Packit 90a5c9
                                  "Symbolic link not allowed "
Packit 90a5c9
                                  "or link target not accessible: %s",
Packit 90a5c9
                                  r->filename);
Packit 90a5c9
                    return r->status = res;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* Ok, we are done with the link's info, test the real target
Packit 90a5c9
             */
Packit 90a5c9
            if (thisinfo.filetype == APR_REG ||
Packit 90a5c9
                thisinfo.filetype == APR_NOFILE) {
Packit 90a5c9
                /* That was fun, nothing left for us here
Packit 90a5c9
                 */
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
            else if (thisinfo.filetype != APR_DIR) {
Packit 90a5c9
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00038)
Packit 90a5c9
                              "Forbidden: %s doesn't point to "
Packit 90a5c9
                              "a file or directory",
Packit 90a5c9
                              r->filename);
Packit 90a5c9
                return r->status = HTTP_FORBIDDEN;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            ++seg;
Packit 90a5c9
        } while (thisinfo.filetype == APR_DIR);
Packit 90a5c9
Packit 90a5c9
        /* If we have _not_ optimized, this is the time to recover
Packit 90a5c9
         * the final stat result.
Packit 90a5c9
         */
Packit 90a5c9
        if (r->finfo.filetype == APR_NOFILE || r->finfo.filetype == APR_LNK) {
Packit 90a5c9
            r->finfo = thisinfo;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Now splice the saved path_info back onto any new path_info
Packit 90a5c9
         */
Packit 90a5c9
        if (save_path_info) {
Packit 90a5c9
            if (r->path_info && *r->path_info) {
Packit 90a5c9
                r->path_info = ap_make_full_path(r->pool, r->path_info,
Packit 90a5c9
                                                 save_path_info);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                r->path_info = save_path_info;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * Now we'll deal with the regexes, note we pick up sec_idx
Packit 90a5c9
         * where we left off (we gave up after we hit entry_core->r)
Packit 90a5c9
         */
Packit 90a5c9
        for (; sec_idx < num_sec; ++sec_idx) {
Packit 90a5c9
Packit 90a5c9
            int nmatch = 0;
Packit 90a5c9
            int i;
Packit 90a5c9
            ap_regmatch_t *pmatch = NULL;
Packit 90a5c9
Packit 90a5c9
            core_dir_config *entry_core;
Packit 90a5c9
            entry_core = ap_get_core_module_config(sec_ent[sec_idx]);
Packit 90a5c9
Packit 90a5c9
            if (!entry_core->r) {
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (entry_core->refs && entry_core->refs->nelts) {
Packit 90a5c9
                if (!rxpool) {
Packit 90a5c9
                    apr_pool_create(&rxpool, r->pool);
Packit 90a5c9
                }
Packit 90a5c9
                nmatch = entry_core->refs->nelts;
Packit 90a5c9
                pmatch = apr_palloc(rxpool, nmatch*sizeof(ap_regmatch_t));
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (ap_regexec(entry_core->r, r->filename, nmatch, pmatch, 0)) {
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            for (i = 0; i < nmatch; i++) {
Packit 90a5c9
                if (pmatch[i].rm_so >= 0 && pmatch[i].rm_eo >= 0 &&
Packit 90a5c9
                    ((const char **)entry_core->refs->elts)[i]) {
Packit 90a5c9
                    apr_table_setn(r->subprocess_env, 
Packit 90a5c9
                                   ((const char **)entry_core->refs->elts)[i],
Packit 90a5c9
                                   apr_pstrndup(r->pool,
Packit 90a5c9
                                   r->filename + pmatch[i].rm_so,
Packit 90a5c9
                                   pmatch[i].rm_eo - pmatch[i].rm_so));
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* If we haven't already continue'd above, we have a match.
Packit 90a5c9
             *
Packit 90a5c9
             * Calculate our full-context core opts & override.
Packit 90a5c9
             */
Packit 90a5c9
            core_opts_merge(sec_ent[sec_idx], &opts);
Packit 90a5c9
Packit 90a5c9
            /* If we merged this same section last time, reuse it
Packit 90a5c9
             */
Packit 90a5c9
            if (matches) {
Packit 90a5c9
                if (last_walk->matched == sec_ent[sec_idx]) {
Packit 90a5c9
                    now_merged = last_walk->merged;
Packit 90a5c9
                    ++last_walk;
Packit 90a5c9
                    --matches;
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                /* We fell out of sync.  This is our own copy of walked,
Packit 90a5c9
                 * so truncate the remaining matches and reset remaining.
Packit 90a5c9
                 */
Packit 90a5c9
                cache->walked->nelts -= matches;
Packit 90a5c9
                matches = 0;
Packit 90a5c9
                cached = 0;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (now_merged) {
Packit 90a5c9
                now_merged = ap_merge_per_dir_configs(r->pool,
Packit 90a5c9
                                                      now_merged,
Packit 90a5c9
                                                      sec_ent[sec_idx]);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                now_merged = sec_ent[sec_idx];
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            last_walk = (walk_walked_t*)apr_array_push(cache->walked);
Packit 90a5c9
            last_walk->matched = sec_ent[sec_idx];
Packit 90a5c9
            last_walk->merged = now_merged;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (rxpool) {
Packit 90a5c9
            apr_pool_destroy(rxpool);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Whoops - everything matched in sequence, but either the original
Packit 90a5c9
         * walk found some additional matches (which we need to truncate), or
Packit 90a5c9
         * this walk found some additional matches.
Packit 90a5c9
         */
Packit 90a5c9
        if (matches) {
Packit 90a5c9
            cache->walked->nelts -= matches;
Packit 90a5c9
            cached = 0;
Packit 90a5c9
        }
Packit 90a5c9
        else if (cache->walked->nelts > cached_matches) {
Packit 90a5c9
            cached = 0;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
/* It seems this shouldn't be needed anymore.  We translated the
Packit 90a5c9
 x symlink above into a real resource, and should have died up there.
Packit 90a5c9
 x Even if we keep this, it needs more thought (maybe an r->file_is_symlink)
Packit 90a5c9
 x perhaps it should actually happen in file_walk, so we catch more
Packit 90a5c9
 x obscure cases in autoindex subrequests, etc.
Packit 90a5c9
 x
Packit 90a5c9
 x    * Symlink permissions are determined by the parent.  If the request is
Packit 90a5c9
 x    * for a directory then applying the symlink test here would use the
Packit 90a5c9
 x    * permissions of the directory as opposed to its parent.  Consider a
Packit 90a5c9
 x    * symlink pointing to a dir with a .htaccess disallowing symlinks.  If
Packit 90a5c9
 x    * you access /symlink (or /symlink/) you would get a 403 without this
Packit 90a5c9
 x    * APR_DIR test.  But if you accessed /symlink/index.html, for example,
Packit 90a5c9
 x    * you would *not* get the 403.
Packit 90a5c9
 x
Packit 90a5c9
 x   if (r->finfo.filetype != APR_DIR
Packit 90a5c9
 x       && (res = resolve_symlink(r->filename, r->info, ap_allow_options(r),
Packit 90a5c9
 x                                 r->pool))) {
Packit 90a5c9
 x       ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
Packit 90a5c9
 x                     "Symbolic link not allowed: %s", r->filename);
Packit 90a5c9
 x       return res;
Packit 90a5c9
 x   }
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
    /* Save future sub-requestors much angst in processing
Packit 90a5c9
     * this subrequest.  If dir_walk couldn't canonicalize
Packit 90a5c9
     * the file path, nothing can.
Packit 90a5c9
     */
Packit 90a5c9
    r->canonical_filename = r->filename;
Packit 90a5c9
Packit 90a5c9
    if (r->finfo.filetype == APR_DIR) {
Packit 90a5c9
        cache->cached = r->filename;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        cache->cached = ap_make_dirstr_parent(r->pool, r->filename);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (cached
Packit 90a5c9
        && r->per_dir_config == cache->dir_conf_merged) {
Packit 90a5c9
        r->per_dir_config = cache->per_dir_result;
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    cache->dir_conf_tested = sec_ent;
Packit 90a5c9
    cache->dir_conf_merged = r->per_dir_config;
Packit 90a5c9
Packit 90a5c9
    /* Merge our cache->dir_conf_merged construct with the r->per_dir_configs,
Packit 90a5c9
     * and note the end result to (potentially) skip this step next time.
Packit 90a5c9
     */
Packit 90a5c9
    if (now_merged) {
Packit 90a5c9
        r->per_dir_config = ap_merge_per_dir_configs(r->pool,
Packit 90a5c9
                                                     r->per_dir_config,
Packit 90a5c9
                                                     now_merged);
Packit 90a5c9
    }
Packit 90a5c9
    cache->per_dir_result = r->per_dir_config;
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_location_walk(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ap_conf_vector_t *now_merged = NULL;
Packit 90a5c9
    core_server_config *sconf =
Packit 90a5c9
        ap_get_core_module_config(r->server->module_config);
Packit 90a5c9
    ap_conf_vector_t **sec_ent = (ap_conf_vector_t **)sconf->sec_url->elts;
Packit 90a5c9
    int num_sec = sconf->sec_url->nelts;
Packit 90a5c9
    walk_cache_t *cache;
Packit 90a5c9
    const char *entry_uri;
Packit 90a5c9
    int cached;
Packit 90a5c9
Packit 90a5c9
    /* No tricks here, there are no <Locations > to parse in this vhost.
Packit 90a5c9
     * We won't destroy the cache, just in case _this_ redirect is later
Packit 90a5c9
     * redirected again to a vhost with <Location > blocks to optimize.
Packit 90a5c9
     */
Packit 90a5c9
    if (!num_sec) {
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    cache = prep_walk_cache(AP_NOTE_LOCATION_WALK, r);
Packit 90a5c9
    cached = (cache->cached != NULL);
Packit 90a5c9
Packit 90a5c9
    /* Location and LocationMatch differ on their behaviour w.r.t. multiple
Packit 90a5c9
     * slashes.  Location matches multiple slashes with a single slash,
Packit 90a5c9
     * LocationMatch doesn't.  An exception, for backwards brokenness is
Packit 90a5c9
     * absoluteURIs... in which case neither match multiple slashes.
Packit 90a5c9
     */
Packit 90a5c9
    if (r->uri[0] != '/') {
Packit 90a5c9
        entry_uri = r->uri;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        char *uri = apr_pstrdup(r->pool, r->uri);
Packit 90a5c9
        ap_no2slash(uri);
Packit 90a5c9
        entry_uri = uri;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* If we have an cache->cached location that matches r->uri,
Packit 90a5c9
     * and the vhost's list of locations hasn't changed, we can skip
Packit 90a5c9
     * rewalking the location_walk entries.
Packit 90a5c9
     */
Packit 90a5c9
    if (cached
Packit 90a5c9
        && (cache->dir_conf_tested == sec_ent)
Packit 90a5c9
        && (strcmp(entry_uri, cache->cached) == 0)) {
Packit 90a5c9
        /* Well this looks really familiar!  If our end-result (per_dir_result)
Packit 90a5c9
         * didn't change, we have absolutely nothing to do :)
Packit 90a5c9
         * Otherwise (as is the case with most dir_merged/file_merged requests)
Packit 90a5c9
         * we must merge our dir_conf_merged onto this new r->per_dir_config.
Packit 90a5c9
         */
Packit 90a5c9
        if (r->per_dir_config == cache->per_dir_result) {
Packit 90a5c9
            return OK;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (cache->walked->nelts) {
Packit 90a5c9
            now_merged = ((walk_walked_t*)cache->walked->elts)
Packit 90a5c9
                                            [cache->walked->nelts - 1].merged;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /* We start now_merged from NULL since we want to build
Packit 90a5c9
         * a locations list that can be merged to any vhost.
Packit 90a5c9
         */
Packit 90a5c9
        int len, sec_idx;
Packit 90a5c9
        int matches = cache->walked->nelts;
Packit 90a5c9
        int cached_matches = matches;
Packit 90a5c9
        walk_walked_t *last_walk = (walk_walked_t*)cache->walked->elts;
Packit 90a5c9
        apr_pool_t *rxpool = NULL;
Packit 90a5c9
Packit 90a5c9
        cached &= auth_internal_per_conf;
Packit 90a5c9
        cache->cached = entry_uri;
Packit 90a5c9
Packit 90a5c9
        /* Go through the location entries, and check for matches.
Packit 90a5c9
         * We apply the directive sections in given order, we should
Packit 90a5c9
         * really try them with the most general first.
Packit 90a5c9
         */
Packit 90a5c9
        for (sec_idx = 0; sec_idx < num_sec; ++sec_idx) {
Packit 90a5c9
Packit 90a5c9
            core_dir_config *entry_core;
Packit 90a5c9
            entry_core = ap_get_core_module_config(sec_ent[sec_idx]);
Packit 90a5c9
Packit 90a5c9
            /* ### const strlen can be optimized in location config parsing */
Packit 90a5c9
            len = strlen(entry_core->d);
Packit 90a5c9
Packit 90a5c9
            /* Test the regex, fnmatch or string as appropriate.
Packit 90a5c9
             * If it's a strcmp, and the <Location > pattern was
Packit 90a5c9
             * not slash terminated, then this uri must be slash
Packit 90a5c9
             * terminated (or at the end of the string) to match.
Packit 90a5c9
             */
Packit 90a5c9
            if (entry_core->r) {
Packit 90a5c9
Packit 90a5c9
                int nmatch = 0;
Packit 90a5c9
                int i;
Packit 90a5c9
                ap_regmatch_t *pmatch = NULL;
Packit 90a5c9
Packit 90a5c9
                if (entry_core->refs && entry_core->refs->nelts) {
Packit 90a5c9
                    if (!rxpool) {
Packit 90a5c9
                        apr_pool_create(&rxpool, r->pool);
Packit 90a5c9
                    }
Packit 90a5c9
                    nmatch = entry_core->refs->nelts;
Packit 90a5c9
                    pmatch = apr_palloc(rxpool, nmatch*sizeof(ap_regmatch_t));
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                if (ap_regexec(entry_core->r, r->uri, nmatch, pmatch, 0)) {
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                for (i = 0; i < nmatch; i++) {
Packit 90a5c9
                    if (pmatch[i].rm_so >= 0 && pmatch[i].rm_eo >= 0 && 
Packit 90a5c9
                        ((const char **)entry_core->refs->elts)[i]) {
Packit 90a5c9
                        apr_table_setn(r->subprocess_env,
Packit 90a5c9
                                       ((const char **)entry_core->refs->elts)[i],
Packit 90a5c9
                                       apr_pstrndup(r->pool,
Packit 90a5c9
                                       r->uri + pmatch[i].rm_so,
Packit 90a5c9
                                       pmatch[i].rm_eo - pmatch[i].rm_so));
Packit 90a5c9
                    }
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
Packit 90a5c9
                if ((entry_core->d_is_fnmatch
Packit 90a5c9
                   ? apr_fnmatch(entry_core->d, cache->cached, APR_FNM_PATHNAME)
Packit 90a5c9
                   : (strncmp(entry_core->d, cache->cached, len)
Packit 90a5c9
                      || (len > 0
Packit 90a5c9
                          && entry_core->d[len - 1] != '/'
Packit 90a5c9
                          && cache->cached[len] != '/'
Packit 90a5c9
                          && cache->cached[len] != '\0')))) {
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* If we merged this same section last time, reuse it
Packit 90a5c9
             */
Packit 90a5c9
            if (matches) {
Packit 90a5c9
                if (last_walk->matched == sec_ent[sec_idx]) {
Packit 90a5c9
                    now_merged = last_walk->merged;
Packit 90a5c9
                    ++last_walk;
Packit 90a5c9
                    --matches;
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                /* We fell out of sync.  This is our own copy of walked,
Packit 90a5c9
                 * so truncate the remaining matches and reset remaining.
Packit 90a5c9
                 */
Packit 90a5c9
                cache->walked->nelts -= matches;
Packit 90a5c9
                matches = 0;
Packit 90a5c9
                cached = 0;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (now_merged) {
Packit 90a5c9
                now_merged = ap_merge_per_dir_configs(r->pool,
Packit 90a5c9
                                                      now_merged,
Packit 90a5c9
                                                      sec_ent[sec_idx]);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                now_merged = sec_ent[sec_idx];
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            last_walk = (walk_walked_t*)apr_array_push(cache->walked);
Packit 90a5c9
            last_walk->matched = sec_ent[sec_idx];
Packit 90a5c9
            last_walk->merged = now_merged;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (rxpool) {
Packit 90a5c9
            apr_pool_destroy(rxpool);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Whoops - everything matched in sequence, but either the original
Packit 90a5c9
         * walk found some additional matches (which we need to truncate), or
Packit 90a5c9
         * this walk found some additional matches.
Packit 90a5c9
         */
Packit 90a5c9
        if (matches) {
Packit 90a5c9
            cache->walked->nelts -= matches;
Packit 90a5c9
            cached = 0;
Packit 90a5c9
        }
Packit 90a5c9
        else if (cache->walked->nelts > cached_matches) {
Packit 90a5c9
            cached = 0;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (cached
Packit 90a5c9
        && r->per_dir_config == cache->dir_conf_merged) {
Packit 90a5c9
        r->per_dir_config = cache->per_dir_result;
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    cache->dir_conf_tested = sec_ent;
Packit 90a5c9
    cache->dir_conf_merged = r->per_dir_config;
Packit 90a5c9
Packit 90a5c9
    /* Merge our cache->dir_conf_merged construct with the r->per_dir_configs,
Packit 90a5c9
     * and note the end result to (potentially) skip this step next time.
Packit 90a5c9
     */
Packit 90a5c9
    if (now_merged) {
Packit 90a5c9
        r->per_dir_config = ap_merge_per_dir_configs(r->pool,
Packit 90a5c9
                                                     r->per_dir_config,
Packit 90a5c9
                                                     now_merged);
Packit 90a5c9
    }
Packit 90a5c9
    cache->per_dir_result = r->per_dir_config;
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_file_walk(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    ap_conf_vector_t *now_merged = NULL;
Packit 90a5c9
    core_dir_config *dconf = ap_get_core_module_config(r->per_dir_config);
Packit 90a5c9
    ap_conf_vector_t **sec_ent = NULL;
Packit 90a5c9
    int num_sec = 0;
Packit 90a5c9
    walk_cache_t *cache;
Packit 90a5c9
    const char *test_file;
Packit 90a5c9
    int cached;
Packit 90a5c9
Packit 90a5c9
    if (dconf->sec_file) {
Packit 90a5c9
        sec_ent = (ap_conf_vector_t **)dconf->sec_file->elts;
Packit 90a5c9
        num_sec = dconf->sec_file->nelts;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* To allow broken modules to proceed, we allow missing filenames to pass.
Packit 90a5c9
     * We will catch it later if it's heading for the core handler.
Packit 90a5c9
     * directory_walk already posted an INFO note for module debugging.
Packit 90a5c9
     */
Packit 90a5c9
    if (r->filename == NULL) {
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* No tricks here, there are just no <Files > to parse in this context.
Packit 90a5c9
     * We won't destroy the cache, just in case _this_ redirect is later
Packit 90a5c9
     * redirected again to a context containing the same or similar <Files >.
Packit 90a5c9
     */
Packit 90a5c9
    if (!num_sec) {
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    cache = prep_walk_cache(AP_NOTE_FILE_WALK, r);
Packit 90a5c9
    cached = (cache->cached != NULL);
Packit 90a5c9
Packit 90a5c9
    /* Get the basename .. and copy for the cache just
Packit 90a5c9
     * in case r->filename is munged by another module
Packit 90a5c9
     */
Packit 90a5c9
    test_file = strrchr(r->filename, '/');
Packit 90a5c9
    if (test_file == NULL) {
Packit 90a5c9
        test_file = apr_pstrdup(r->pool, r->filename);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        test_file = apr_pstrdup(r->pool, ++test_file);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* If we have an cache->cached file name that matches test_file,
Packit 90a5c9
     * and the directory's list of file sections hasn't changed, we
Packit 90a5c9
     * can skip rewalking the file_walk entries.
Packit 90a5c9
     */
Packit 90a5c9
    if (cached
Packit 90a5c9
        && (cache->dir_conf_tested == sec_ent)
Packit 90a5c9
        && (strcmp(test_file, cache->cached) == 0)) {
Packit 90a5c9
        /* Well this looks really familiar!  If our end-result (per_dir_result)
Packit 90a5c9
         * didn't change, we have absolutely nothing to do :)
Packit 90a5c9
         * Otherwise (as is the case with most dir_merged requests)
Packit 90a5c9
         * we must merge our dir_conf_merged onto this new r->per_dir_config.
Packit 90a5c9
         */
Packit 90a5c9
        if (r->per_dir_config == cache->per_dir_result) {
Packit 90a5c9
            return OK;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (cache->walked->nelts) {
Packit 90a5c9
            now_merged = ((walk_walked_t*)cache->walked->elts)
Packit 90a5c9
                [cache->walked->nelts - 1].merged;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /* We start now_merged from NULL since we want to build
Packit 90a5c9
         * a file section list that can be merged to any dir_walk.
Packit 90a5c9
         */
Packit 90a5c9
        int sec_idx;
Packit 90a5c9
        int matches = cache->walked->nelts;
Packit 90a5c9
        int cached_matches = matches;
Packit 90a5c9
        walk_walked_t *last_walk = (walk_walked_t*)cache->walked->elts;
Packit 90a5c9
        apr_pool_t *rxpool = NULL;
Packit 90a5c9
Packit 90a5c9
        cached &= auth_internal_per_conf;
Packit 90a5c9
        cache->cached = test_file;
Packit 90a5c9
Packit 90a5c9
        /* Go through the location entries, and check for matches.
Packit 90a5c9
         * We apply the directive sections in given order, we should
Packit 90a5c9
         * really try them with the most general first.
Packit 90a5c9
         */
Packit 90a5c9
        for (sec_idx = 0; sec_idx < num_sec; ++sec_idx) {
Packit 90a5c9
            core_dir_config *entry_core;
Packit 90a5c9
            entry_core = ap_get_core_module_config(sec_ent[sec_idx]);
Packit 90a5c9
Packit 90a5c9
            if (entry_core->r) {
Packit 90a5c9
Packit 90a5c9
                int nmatch = 0;
Packit 90a5c9
                int i;
Packit 90a5c9
                ap_regmatch_t *pmatch = NULL;
Packit 90a5c9
Packit 90a5c9
                if (entry_core->refs && entry_core->refs->nelts) {
Packit 90a5c9
                    if (!rxpool) {
Packit 90a5c9
                        apr_pool_create(&rxpool, r->pool);
Packit 90a5c9
                    }
Packit 90a5c9
                    nmatch = entry_core->refs->nelts;
Packit 90a5c9
                    pmatch = apr_palloc(rxpool, nmatch*sizeof(ap_regmatch_t));
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                if (ap_regexec(entry_core->r, cache->cached, nmatch, pmatch, 0)) {
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                for (i = 0; i < nmatch; i++) {
Packit 90a5c9
                    if (pmatch[i].rm_so >= 0 && pmatch[i].rm_eo >= 0 && 
Packit 90a5c9
                        ((const char **)entry_core->refs->elts)[i]) {
Packit 90a5c9
                        apr_table_setn(r->subprocess_env,
Packit 90a5c9
                                       ((const char **)entry_core->refs->elts)[i],
Packit 90a5c9
                                       apr_pstrndup(r->pool,
Packit 90a5c9
                                       cache->cached + pmatch[i].rm_so,
Packit 90a5c9
                                       pmatch[i].rm_eo - pmatch[i].rm_so));
Packit 90a5c9
                    }
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                if ((entry_core->d_is_fnmatch
Packit 90a5c9
                       ? apr_fnmatch(entry_core->d, cache->cached, APR_FNM_PATHNAME)
Packit 90a5c9
                       : strcmp(entry_core->d, cache->cached))) {
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* If we merged this same section last time, reuse it
Packit 90a5c9
             */
Packit 90a5c9
            if (matches) {
Packit 90a5c9
                if (last_walk->matched == sec_ent[sec_idx]) {
Packit 90a5c9
                    now_merged = last_walk->merged;
Packit 90a5c9
                    ++last_walk;
Packit 90a5c9
                    --matches;
Packit 90a5c9
                    continue;
Packit 90a5c9
                }
Packit 90a5c9
Packit 90a5c9
                /* We fell out of sync.  This is our own copy of walked,
Packit 90a5c9
                 * so truncate the remaining matches and reset remaining.
Packit 90a5c9
                 */
Packit 90a5c9
                cache->walked->nelts -= matches;
Packit 90a5c9
                matches = 0;
Packit 90a5c9
                cached = 0;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            if (now_merged) {
Packit 90a5c9
                now_merged = ap_merge_per_dir_configs(r->pool,
Packit 90a5c9
                                                      now_merged,
Packit 90a5c9
                                                      sec_ent[sec_idx]);
Packit 90a5c9
            }
Packit 90a5c9
            else {
Packit 90a5c9
                now_merged = sec_ent[sec_idx];
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            last_walk = (walk_walked_t*)apr_array_push(cache->walked);
Packit 90a5c9
            last_walk->matched = sec_ent[sec_idx];
Packit 90a5c9
            last_walk->merged = now_merged;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (rxpool) {
Packit 90a5c9
            apr_pool_destroy(rxpool);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Whoops - everything matched in sequence, but either the original
Packit 90a5c9
         * walk found some additional matches (which we need to truncate), or
Packit 90a5c9
         * this walk found some additional matches.
Packit 90a5c9
         */
Packit 90a5c9
        if (matches) {
Packit 90a5c9
            cache->walked->nelts -= matches;
Packit 90a5c9
            cached = 0;
Packit 90a5c9
        }
Packit 90a5c9
        else if (cache->walked->nelts > cached_matches) {
Packit 90a5c9
            cached = 0;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (cached
Packit 90a5c9
        && r->per_dir_config == cache->dir_conf_merged) {
Packit 90a5c9
        r->per_dir_config = cache->per_dir_result;
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    cache->dir_conf_tested = sec_ent;
Packit 90a5c9
    cache->dir_conf_merged = r->per_dir_config;
Packit 90a5c9
Packit 90a5c9
    /* Merge our cache->dir_conf_merged construct with the r->per_dir_configs,
Packit 90a5c9
     * and note the end result to (potentially) skip this step next time.
Packit 90a5c9
     */
Packit 90a5c9
    if (now_merged) {
Packit 90a5c9
        r->per_dir_config = ap_merge_per_dir_configs(r->pool,
Packit 90a5c9
                                                     r->per_dir_config,
Packit 90a5c9
                                                     now_merged);
Packit 90a5c9
    }
Packit 90a5c9
    cache->per_dir_result = r->per_dir_config;
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int ap_if_walk_sub(request_rec *r, core_dir_config* dconf)
Packit 90a5c9
{
Packit 90a5c9
    ap_conf_vector_t *now_merged = NULL;
Packit 90a5c9
    ap_conf_vector_t **sec_ent = NULL;
Packit 90a5c9
    int num_sec = 0;
Packit 90a5c9
    walk_cache_t *cache;
Packit 90a5c9
    int cached;
Packit 90a5c9
    int sec_idx;
Packit 90a5c9
    int matches;
Packit 90a5c9
    int cached_matches;
Packit 90a5c9
    int prev_result = -1;
Packit 90a5c9
    walk_walked_t *last_walk;
Packit 90a5c9
Packit 90a5c9
    if (dconf && dconf->sec_if) {
Packit 90a5c9
        sec_ent = (ap_conf_vector_t **)dconf->sec_if->elts;
Packit 90a5c9
        num_sec = dconf->sec_if->nelts;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* No tricks here, there are just no <If > to parse in this context.
Packit 90a5c9
     * We won't destroy the cache, just in case _this_ redirect is later
Packit 90a5c9
     * redirected again to a context containing the same or similar <If >.
Packit 90a5c9
     */
Packit 90a5c9
    if (!num_sec) {
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    cache = prep_walk_cache(AP_NOTE_IF_WALK, r);
Packit 90a5c9
    cached = (cache->cached != NULL);
Packit 90a5c9
    cache->cached = (void *)1;
Packit 90a5c9
    matches = cache->walked->nelts;
Packit 90a5c9
    cached_matches = matches;
Packit 90a5c9
    last_walk = (walk_walked_t*)cache->walked->elts;
Packit 90a5c9
Packit 90a5c9
    cached &= auth_internal_per_conf;
Packit 90a5c9
Packit 90a5c9
    /* Go through the if entries, and check for matches  */
Packit 90a5c9
    for (sec_idx = 0; sec_idx < num_sec; ++sec_idx) {
Packit 90a5c9
        const char *err = NULL;
Packit 90a5c9
        core_dir_config *entry_core;
Packit 90a5c9
        int rc;
Packit 90a5c9
        entry_core = ap_get_core_module_config(sec_ent[sec_idx]);
Packit 90a5c9
Packit 90a5c9
        AP_DEBUG_ASSERT(entry_core->condition_ifelse != 0);
Packit 90a5c9
        if (entry_core->condition_ifelse & AP_CONDITION_ELSE) {
Packit 90a5c9
            AP_DEBUG_ASSERT(prev_result != -1);
Packit 90a5c9
            if (prev_result == 1)
Packit 90a5c9
                continue;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (entry_core->condition_ifelse & AP_CONDITION_IF) {
Packit 90a5c9
            rc = ap_expr_exec(r, entry_core->condition, &err;;
Packit 90a5c9
            if (rc <= 0) {
Packit 90a5c9
                if (rc < 0)
Packit 90a5c9
                    ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(00039)
Packit 90a5c9
                                  "Failed to evaluate <If > condition: %s",
Packit 90a5c9
                                  err);
Packit 90a5c9
                prev_result = 0;
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
            prev_result = 1;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            prev_result = -1;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* If we merged this same section last time, reuse it
Packit 90a5c9
         */
Packit 90a5c9
        if (matches) {
Packit 90a5c9
            if (last_walk->matched == sec_ent[sec_idx]) {
Packit 90a5c9
                now_merged = last_walk->merged;
Packit 90a5c9
                ++last_walk;
Packit 90a5c9
                --matches;
Packit 90a5c9
                continue;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            /* We fell out of sync.  This is our own copy of walked,
Packit 90a5c9
             * so truncate the remaining matches and reset remaining.
Packit 90a5c9
             */
Packit 90a5c9
            cache->walked->nelts -= matches;
Packit 90a5c9
            matches = 0;
Packit 90a5c9
            cached = 0;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (now_merged) {
Packit 90a5c9
            now_merged = ap_merge_per_dir_configs(r->pool,
Packit 90a5c9
                                                  now_merged,
Packit 90a5c9
                                                  sec_ent[sec_idx]);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            now_merged = sec_ent[sec_idx];
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        last_walk = (walk_walked_t*)apr_array_push(cache->walked);
Packit 90a5c9
        last_walk->matched = sec_ent[sec_idx];
Packit 90a5c9
        last_walk->merged = now_merged;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Everything matched in sequence, but it may be that the original
Packit 90a5c9
     * walk found some additional matches (which we need to truncate), or
Packit 90a5c9
     * this walk found some additional matches.
Packit 90a5c9
     */
Packit 90a5c9
    if (matches) {
Packit 90a5c9
        cache->walked->nelts -= matches;
Packit 90a5c9
        cached = 0;
Packit 90a5c9
    }
Packit 90a5c9
    else if (cache->walked->nelts > cached_matches) {
Packit 90a5c9
        cached = 0;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (cached
Packit 90a5c9
        && r->per_dir_config == cache->dir_conf_merged) {
Packit 90a5c9
        r->per_dir_config = cache->per_dir_result;
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    cache->dir_conf_tested = sec_ent;
Packit 90a5c9
    cache->dir_conf_merged = r->per_dir_config;
Packit 90a5c9
Packit 90a5c9
    /* Merge our cache->dir_conf_merged construct with the r->per_dir_configs,
Packit 90a5c9
     * and note the end result to (potentially) skip this step next time.
Packit 90a5c9
     */
Packit 90a5c9
    if (now_merged) {
Packit 90a5c9
        r->per_dir_config = ap_merge_per_dir_configs(r->pool,
Packit 90a5c9
                                                     r->per_dir_config,
Packit 90a5c9
                                                     now_merged);
Packit 90a5c9
    }
Packit 90a5c9
    cache->per_dir_result = r->per_dir_config;
Packit 90a5c9
Packit 90a5c9
    if (now_merged) {
Packit 90a5c9
        core_dir_config *dconf_merged = ap_get_core_module_config(now_merged);
Packit 90a5c9
Packit 90a5c9
        /* Allow nested <If>s and their configs to get merged
Packit 90a5c9
         * with the current one.
Packit 90a5c9
         */
Packit 90a5c9
        return ap_if_walk_sub(r, dconf_merged);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_if_walk(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    core_dir_config *dconf = ap_get_core_module_config(r->per_dir_config);
Packit 90a5c9
    int status = ap_if_walk_sub(r, dconf);
Packit 90a5c9
    return status;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*****************************************************************
Packit 90a5c9
 *
Packit 90a5c9
 * The sub_request mechanism.
Packit 90a5c9
 *
Packit 90a5c9
 * Fns to look up a relative URI from, e.g., a map file or SSI document.
Packit 90a5c9
 * These do all access checks, etc., but don't actually run the transaction
Packit 90a5c9
 * ... use run_sub_req below for that.  Also, be sure to use destroy_sub_req
Packit 90a5c9
 * as appropriate if you're likely to be creating more than a few of these.
Packit 90a5c9
 * (An early Apache version didn't destroy the sub_reqs used in directory
Packit 90a5c9
 * indexing.  The result, when indexing a directory with 800-odd files in
Packit 90a5c9
 * it, was massively excessive storage allocation).
Packit 90a5c9
 *
Packit 90a5c9
 * Note more manipulation of protocol-specific vars in the request
Packit 90a5c9
 * structure...
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
static request_rec *make_sub_request(const request_rec *r,
Packit 90a5c9
                                     ap_filter_t *next_filter)
Packit 90a5c9
{
Packit 90a5c9
    apr_pool_t *rrp;
Packit 90a5c9
    request_rec *rnew;
Packit 90a5c9
Packit 90a5c9
    apr_pool_create(&rrp, r->pool);
Packit 90a5c9
    apr_pool_tag(rrp, "subrequest");
Packit 90a5c9
    rnew = apr_pcalloc(rrp, sizeof(request_rec));
Packit 90a5c9
    rnew->pool = rrp;
Packit 90a5c9
Packit 90a5c9
    rnew->hostname       = r->hostname;
Packit 90a5c9
    rnew->request_time   = r->request_time;
Packit 90a5c9
    rnew->connection     = r->connection;
Packit 90a5c9
    rnew->server         = r->server;
Packit 90a5c9
    rnew->log            = r->log;
Packit 90a5c9
Packit 90a5c9
    rnew->request_config = ap_create_request_config(rnew->pool);
Packit 90a5c9
Packit 90a5c9
    /* Start a clean config from this subrequest's vhost.  Optimization in
Packit 90a5c9
     * Location/File/Dir walks from the parent request assure that if the
Packit 90a5c9
     * config blocks of the subrequest match the parent request, no merges
Packit 90a5c9
     * will actually occur (and generally a minimal number of merges are
Packit 90a5c9
     * required, even if the parent and subrequest aren't quite identical.)
Packit 90a5c9
     */
Packit 90a5c9
    rnew->per_dir_config = r->server->lookup_defaults;
Packit 90a5c9
Packit 90a5c9
    rnew->htaccess = r->htaccess;
Packit 90a5c9
    rnew->allowed_methods = ap_make_method_list(rnew->pool, 2);
Packit 90a5c9
Packit 90a5c9
    /* make a copy of the allowed-methods list */
Packit 90a5c9
    ap_copy_method_list(rnew->allowed_methods, r->allowed_methods);
Packit 90a5c9
Packit 90a5c9
    /* start with the same set of output filters */
Packit 90a5c9
    if (next_filter) {
Packit 90a5c9
        ap_filter_t *scan = next_filter;
Packit 90a5c9
Packit 90a5c9
        /* while there are no input filters for a subrequest, we will
Packit 90a5c9
         * try to insert some, so if we don't have valid data, the code
Packit 90a5c9
         * will seg fault.
Packit 90a5c9
         */
Packit 90a5c9
        rnew->input_filters = r->input_filters;
Packit 90a5c9
        rnew->proto_input_filters = r->proto_input_filters;
Packit 90a5c9
        rnew->output_filters = next_filter;
Packit 90a5c9
        rnew->proto_output_filters = r->proto_output_filters;
Packit 90a5c9
        while (scan && (scan != r->proto_output_filters)) {
Packit 90a5c9
            if (scan->frec == ap_subreq_core_filter_handle) {
Packit 90a5c9
                break;
Packit 90a5c9
            }
Packit 90a5c9
            scan = scan->next;
Packit 90a5c9
        }
Packit 90a5c9
        if (!scan || scan == r->proto_output_filters) {
Packit 90a5c9
            ap_add_output_filter_handle(ap_subreq_core_filter_handle,
Packit 90a5c9
                    NULL, rnew, rnew->connection);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /* If NULL - we are expecting to be internal_fast_redirect'ed
Packit 90a5c9
         * to this subrequest - or this request will never be invoked.
Packit 90a5c9
         * Ignore the original request filter stack entirely, and
Packit 90a5c9
         * drill the input and output stacks back to the connection.
Packit 90a5c9
         */
Packit 90a5c9
        rnew->proto_input_filters = r->proto_input_filters;
Packit 90a5c9
        rnew->proto_output_filters = r->proto_output_filters;
Packit 90a5c9
Packit 90a5c9
        rnew->input_filters = r->proto_input_filters;
Packit 90a5c9
        rnew->output_filters = r->proto_output_filters;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    rnew->useragent_addr = r->useragent_addr;
Packit 90a5c9
    rnew->useragent_ip = r->useragent_ip;
Packit 90a5c9
Packit 90a5c9
    /* no input filters for a subrequest */
Packit 90a5c9
Packit 90a5c9
    ap_set_sub_req_protocol(rnew, r);
Packit 90a5c9
Packit 90a5c9
    /* We have to run this after we fill in sub req vars,
Packit 90a5c9
     * or the r->main pointer won't be setup
Packit 90a5c9
     */
Packit 90a5c9
    ap_run_create_request(rnew);
Packit 90a5c9
Packit 90a5c9
    /* Begin by presuming any module can make its own path_info assumptions,
Packit 90a5c9
     * until some module interjects and changes the value.
Packit 90a5c9
     */
Packit 90a5c9
    rnew->used_path_info = AP_REQ_DEFAULT_PATH_INFO;
Packit 90a5c9
Packit 90a5c9
    /* Pass on the kept body (if any) into the new request. */
Packit 90a5c9
    rnew->kept_body = r->kept_body;
Packit 90a5c9
Packit 90a5c9
    return rnew;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_CORE_DECLARE_NONSTD(apr_status_t) ap_sub_req_output_filter(ap_filter_t *f,
Packit 90a5c9
                                                              apr_bucket_brigade *bb)
Packit 90a5c9
{
Packit 90a5c9
    apr_bucket *e = APR_BRIGADE_LAST(bb);
Packit 90a5c9
Packit 90a5c9
    if (APR_BUCKET_IS_EOS(e)) {
Packit 90a5c9
        apr_bucket_delete(e);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (!APR_BRIGADE_EMPTY(bb)) {
Packit 90a5c9
        return ap_pass_brigade(f->next, bb);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return APR_SUCCESS;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
extern APR_OPTIONAL_FN_TYPE(authz_some_auth_required) *ap__authz_ap_some_auth_required;
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_some_auth_required(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    /* Is there a require line configured for the type of *this* req? */
Packit 90a5c9
    if (ap__authz_ap_some_auth_required) {
Packit 90a5c9
        return ap__authz_ap_some_auth_required(r);
Packit 90a5c9
    }
Packit 90a5c9
    else
Packit 90a5c9
        return 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_clear_auth_internal(void)
Packit 90a5c9
{
Packit 90a5c9
    auth_internal_per_conf_hooks = 0;
Packit 90a5c9
    auth_internal_per_conf_providers = 0;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_setup_auth_internal(apr_pool_t *ptemp)
Packit 90a5c9
{
Packit 90a5c9
    int total_auth_hooks = 0;
Packit 90a5c9
    int total_auth_providers = 0;
Packit 90a5c9
Packit 90a5c9
    auth_internal_per_conf = 0;
Packit 90a5c9
Packit 90a5c9
    if (_hooks.link_access_checker) {
Packit 90a5c9
        total_auth_hooks += _hooks.link_access_checker->nelts;
Packit 90a5c9
    }
Packit 90a5c9
    if (_hooks.link_access_checker_ex) {
Packit 90a5c9
        total_auth_hooks += _hooks.link_access_checker_ex->nelts;
Packit 90a5c9
    }
Packit 90a5c9
    if (_hooks.link_check_user_id) {
Packit 90a5c9
        total_auth_hooks += _hooks.link_check_user_id->nelts;
Packit 90a5c9
    }
Packit 90a5c9
    if (_hooks.link_auth_checker) {
Packit 90a5c9
        total_auth_hooks += _hooks.link_auth_checker->nelts;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (total_auth_hooks > auth_internal_per_conf_hooks) {
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    total_auth_providers +=
Packit 90a5c9
        ap_list_provider_names(ptemp, AUTHN_PROVIDER_GROUP,
Packit 90a5c9
                               AUTHN_PROVIDER_VERSION)->nelts;
Packit 90a5c9
    total_auth_providers +=
Packit 90a5c9
        ap_list_provider_names(ptemp, AUTHZ_PROVIDER_GROUP,
Packit 90a5c9
                               AUTHZ_PROVIDER_VERSION)->nelts;
Packit 90a5c9
Packit 90a5c9
    if (total_auth_providers > auth_internal_per_conf_providers) {
Packit 90a5c9
        return;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    auth_internal_per_conf = 1;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(apr_status_t) ap_register_auth_provider(apr_pool_t *pool,
Packit 90a5c9
                                                   const char *provider_group,
Packit 90a5c9
                                                   const char *provider_name,
Packit 90a5c9
                                                   const char *provider_version,
Packit 90a5c9
                                                   const void *provider,
Packit 90a5c9
                                                   int type)
Packit 90a5c9
{
Packit 90a5c9
    if ((type & AP_AUTH_INTERNAL_MASK) == AP_AUTH_INTERNAL_PER_CONF) {
Packit 90a5c9
        ++auth_internal_per_conf_providers;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return ap_register_provider(pool, provider_group, provider_name,
Packit 90a5c9
                                provider_version, provider);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_hook_check_access(ap_HOOK_access_checker_t *pf,
Packit 90a5c9
                                      const char * const *aszPre,
Packit 90a5c9
                                      const char * const *aszSucc,
Packit 90a5c9
                                      int nOrder, int type)
Packit 90a5c9
{
Packit 90a5c9
    if ((type & AP_AUTH_INTERNAL_MASK) == AP_AUTH_INTERNAL_PER_CONF) {
Packit 90a5c9
        ++auth_internal_per_conf_hooks;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_hook_access_checker(pf, aszPre, aszSucc, nOrder);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_hook_check_access_ex(ap_HOOK_access_checker_ex_t *pf,
Packit 90a5c9
                                      const char * const *aszPre,
Packit 90a5c9
                                      const char * const *aszSucc,
Packit 90a5c9
                                      int nOrder, int type)
Packit 90a5c9
{
Packit 90a5c9
    if ((type & AP_AUTH_INTERNAL_MASK) == AP_AUTH_INTERNAL_PER_CONF) {
Packit 90a5c9
        ++auth_internal_per_conf_hooks;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_hook_access_checker_ex(pf, aszPre, aszSucc, nOrder);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_hook_check_authn(ap_HOOK_check_user_id_t *pf,
Packit 90a5c9
                                     const char * const *aszPre,
Packit 90a5c9
                                     const char * const *aszSucc,
Packit 90a5c9
                                     int nOrder, int type)
Packit 90a5c9
{
Packit 90a5c9
    if ((type & AP_AUTH_INTERNAL_MASK) == AP_AUTH_INTERNAL_PER_CONF) {
Packit 90a5c9
        ++auth_internal_per_conf_hooks;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_hook_check_user_id(pf, aszPre, aszSucc, nOrder);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_hook_check_authz(ap_HOOK_auth_checker_t *pf,
Packit 90a5c9
                                     const char * const *aszPre,
Packit 90a5c9
                                     const char * const *aszSucc,
Packit 90a5c9
                                     int nOrder, int type)
Packit 90a5c9
{
Packit 90a5c9
    if ((type & AP_AUTH_INTERNAL_MASK) == AP_AUTH_INTERNAL_PER_CONF) {
Packit 90a5c9
        ++auth_internal_per_conf_hooks;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_hook_auth_checker(pf, aszPre, aszSucc, nOrder);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(request_rec *) ap_sub_req_method_uri(const char *method,
Packit 90a5c9
                                                const char *new_uri,
Packit 90a5c9
                                                const request_rec *r,
Packit 90a5c9
                                                ap_filter_t *next_filter)
Packit 90a5c9
{
Packit 90a5c9
    request_rec *rnew;
Packit 90a5c9
    /* Initialise res, to avoid a gcc warning */
Packit 90a5c9
    int res = HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
    char *udir;
Packit 90a5c9
Packit 90a5c9
    rnew = make_sub_request(r, next_filter);
Packit 90a5c9
Packit 90a5c9
    /* would be nicer to pass "method" to ap_set_sub_req_protocol */
Packit 90a5c9
    rnew->method = method;
Packit 90a5c9
    rnew->method_number = ap_method_number_of(method);
Packit 90a5c9
Packit 90a5c9
    if (new_uri[0] == '/') {
Packit 90a5c9
        ap_parse_uri(rnew, new_uri);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        udir = ap_make_dirstr_parent(rnew->pool, r->uri);
Packit 90a5c9
        udir = ap_escape_uri(rnew->pool, udir);    /* re-escape it */
Packit 90a5c9
        ap_parse_uri(rnew, ap_make_full_path(rnew->pool, udir, new_uri));
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* We cannot return NULL without violating the API. So just turn this
Packit 90a5c9
     * subrequest into a 500 to indicate the failure. */
Packit 90a5c9
    if (ap_is_recursion_limit_exceeded(r)) {
Packit 90a5c9
        rnew->status = HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
        return rnew;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* lookup_uri
Packit 90a5c9
     * If the content can be served by the quick_handler, we can
Packit 90a5c9
     * safely bypass request_internal processing.
Packit 90a5c9
     *
Packit 90a5c9
     * If next_filter is NULL we are expecting to be
Packit 90a5c9
     * internal_fast_redirect'ed to the subrequest, or the subrequest will
Packit 90a5c9
     * never be invoked. We need to make sure that the quickhandler is not
Packit 90a5c9
     * invoked by any lookups. Since an internal_fast_redirect will always
Packit 90a5c9
     * occur too late for the quickhandler to handle the request.
Packit 90a5c9
     */
Packit 90a5c9
    if (next_filter) {
Packit 90a5c9
        res = ap_run_quick_handler(rnew, 1);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (next_filter == NULL || res != OK) {
Packit 90a5c9
        if ((res = ap_process_request_internal(rnew))) {
Packit 90a5c9
            rnew->status = res;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return rnew;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(request_rec *) ap_sub_req_lookup_uri(const char *new_uri,
Packit 90a5c9
                                                const request_rec *r,
Packit 90a5c9
                                                ap_filter_t *next_filter)
Packit 90a5c9
{
Packit 90a5c9
    return ap_sub_req_method_uri("GET", new_uri, r, next_filter);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(request_rec *) ap_sub_req_lookup_dirent(const apr_finfo_t *dirent,
Packit 90a5c9
                                                   const request_rec *r,
Packit 90a5c9
                                                   int subtype,
Packit 90a5c9
                                                   ap_filter_t *next_filter)
Packit 90a5c9
{
Packit 90a5c9
    request_rec *rnew;
Packit 90a5c9
    int res;
Packit 90a5c9
    char *fdir;
Packit 90a5c9
    char *udir;
Packit 90a5c9
Packit 90a5c9
    rnew = make_sub_request(r, next_filter);
Packit 90a5c9
Packit 90a5c9
    /* Special case: we are looking at a relative lookup in the same directory.
Packit 90a5c9
     * This is 100% safe, since dirent->name just came from the filesystem.
Packit 90a5c9
     */
Packit 90a5c9
    if (r->path_info && *r->path_info) {
Packit 90a5c9
        /* strip path_info off the end of the uri to keep it in sync
Packit 90a5c9
         * with r->filename, which has already been stripped by directory_walk,
Packit 90a5c9
         * merge the dirent->name, and then, if the caller wants us to remerge
Packit 90a5c9
         * the original path info, do so.  Note we never fix the path_info back
Packit 90a5c9
         * to r->filename, since dir_walk would do so (but we don't expect it
Packit 90a5c9
         * to happen in the usual cases)
Packit 90a5c9
         */
Packit 90a5c9
        udir = apr_pstrdup(rnew->pool, r->uri);
Packit 90a5c9
        udir[ap_find_path_info(udir, r->path_info)] = '\0';
Packit 90a5c9
        udir = ap_make_dirstr_parent(rnew->pool, udir);
Packit 90a5c9
Packit 90a5c9
        rnew->uri = ap_make_full_path(rnew->pool, udir, dirent->name);
Packit 90a5c9
        if (subtype == AP_SUBREQ_MERGE_ARGS) {
Packit 90a5c9
            rnew->uri = ap_make_full_path(rnew->pool, rnew->uri, r->path_info + 1);
Packit 90a5c9
            rnew->path_info = apr_pstrdup(rnew->pool, r->path_info);
Packit 90a5c9
        }
Packit 90a5c9
        rnew->uri = ap_escape_uri(rnew->pool, rnew->uri);
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        udir = ap_make_dirstr_parent(rnew->pool, r->uri);
Packit 90a5c9
        rnew->uri = ap_escape_uri(rnew->pool, ap_make_full_path(rnew->pool,
Packit 90a5c9
                                                                udir,
Packit 90a5c9
                                                                dirent->name));
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    fdir = ap_make_dirstr_parent(rnew->pool, r->filename);
Packit 90a5c9
    rnew->filename = ap_make_full_path(rnew->pool, fdir, dirent->name);
Packit 90a5c9
    if (r->canonical_filename == r->filename) {
Packit 90a5c9
        rnew->canonical_filename = rnew->filename;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* XXX This is now less relevant; we will do a full location walk
Packit 90a5c9
     * these days for this case.  Preserve the apr_stat results, and
Packit 90a5c9
     * perhaps we also tag that symlinks were tested and/or found for
Packit 90a5c9
     * r->filename.
Packit 90a5c9
     */
Packit 90a5c9
    rnew->per_dir_config = r->server->lookup_defaults;
Packit 90a5c9
Packit 90a5c9
    if ((dirent->valid & APR_FINFO_MIN) != APR_FINFO_MIN) {
Packit 90a5c9
        /*
Packit 90a5c9
         * apr_dir_read isn't very complete on this platform, so
Packit 90a5c9
         * we need another apr_stat (with or without APR_FINFO_LINK
Packit 90a5c9
         * depending on whether we allow all symlinks here.)  If this
Packit 90a5c9
         * is an APR_LNK that resolves to an APR_DIR, then we will rerun
Packit 90a5c9
         * everything anyways... this should be safe.
Packit 90a5c9
         */
Packit 90a5c9
        apr_status_t rv;
Packit 90a5c9
        if (ap_allow_options(rnew) & OPT_SYM_LINKS) {
Packit 90a5c9
            if (((rv = apr_stat(&rnew->finfo, rnew->filename,
Packit 90a5c9
                                APR_FINFO_MIN, rnew->pool)) != APR_SUCCESS)
Packit 90a5c9
                && (rv != APR_INCOMPLETE)) {
Packit 90a5c9
                rnew->finfo.filetype = APR_NOFILE;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            if (((rv = apr_stat(&rnew->finfo, rnew->filename,
Packit 90a5c9
                                APR_FINFO_LINK | APR_FINFO_MIN,
Packit 90a5c9
                                rnew->pool)) != APR_SUCCESS)
Packit 90a5c9
                && (rv != APR_INCOMPLETE)) {
Packit 90a5c9
                rnew->finfo.filetype = APR_NOFILE;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        memcpy(&rnew->finfo, dirent, sizeof(apr_finfo_t));
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (rnew->finfo.filetype == APR_LNK) {
Packit 90a5c9
        /*
Packit 90a5c9
         * Resolve this symlink.  We should tie this back to dir_walk's cache
Packit 90a5c9
         */
Packit 90a5c9
        if ((res = resolve_symlink(rnew->filename, &rnew->finfo,
Packit 90a5c9
                                   ap_allow_options(rnew), rnew->pool))
Packit 90a5c9
            != OK) {
Packit 90a5c9
            rnew->status = res;
Packit 90a5c9
            return rnew;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (rnew->finfo.filetype == APR_DIR) {
Packit 90a5c9
        /* ap_make_full_path overallocated the buffers
Packit 90a5c9
         * by one character to help us out here.
Packit 90a5c9
         */
Packit 90a5c9
        strcat(rnew->filename, "/");
Packit 90a5c9
        if (!rnew->path_info || !*rnew->path_info) {
Packit 90a5c9
            strcat(rnew->uri, "/");
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* fill in parsed_uri values
Packit 90a5c9
     */
Packit 90a5c9
    if (r->args && *r->args && (subtype == AP_SUBREQ_MERGE_ARGS)) {
Packit 90a5c9
        ap_parse_uri(rnew, apr_pstrcat(r->pool, rnew->uri, "?",
Packit 90a5c9
                                       r->args, NULL));
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        ap_parse_uri(rnew, rnew->uri);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* We cannot return NULL without violating the API. So just turn this
Packit 90a5c9
     * subrequest into a 500. */
Packit 90a5c9
    if (ap_is_recursion_limit_exceeded(r)) {
Packit 90a5c9
        rnew->status = HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
        return rnew;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((res = ap_process_request_internal(rnew))) {
Packit 90a5c9
        rnew->status = res;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return rnew;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(request_rec *) ap_sub_req_lookup_file(const char *new_file,
Packit 90a5c9
                                                 const request_rec *r,
Packit 90a5c9
                                                 ap_filter_t *next_filter)
Packit 90a5c9
{
Packit 90a5c9
    request_rec *rnew;
Packit 90a5c9
    int res;
Packit 90a5c9
    char *fdir;
Packit 90a5c9
    apr_size_t fdirlen;
Packit 90a5c9
Packit 90a5c9
    rnew = make_sub_request(r, next_filter);
Packit 90a5c9
Packit 90a5c9
    fdir = ap_make_dirstr_parent(rnew->pool, r->filename);
Packit 90a5c9
    fdirlen = strlen(fdir);
Packit 90a5c9
Packit 90a5c9
    /* Translate r->filename, if it was canonical, it stays canonical
Packit 90a5c9
     */
Packit 90a5c9
    if (r->canonical_filename == r->filename) {
Packit 90a5c9
        rnew->canonical_filename = (char*)(1);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (apr_filepath_merge(&rnew->filename, fdir, new_file,
Packit 90a5c9
                           APR_FILEPATH_TRUENAME, rnew->pool) != APR_SUCCESS) {
Packit 90a5c9
        rnew->status = HTTP_FORBIDDEN;
Packit 90a5c9
        return rnew;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (rnew->canonical_filename) {
Packit 90a5c9
        rnew->canonical_filename = rnew->filename;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Check for a special case... if there are no '/' characters in new_file
Packit 90a5c9
     * at all, and the path was the same, then we are looking at a relative
Packit 90a5c9
     * lookup in the same directory.  Fixup the URI to match.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    if (strncmp(rnew->filename, fdir, fdirlen) == 0
Packit 90a5c9
        && rnew->filename[fdirlen]
Packit 90a5c9
        && ap_strchr_c(rnew->filename + fdirlen, '/') == NULL) {
Packit 90a5c9
        apr_status_t rv;
Packit 90a5c9
        if (ap_allow_options(rnew) & OPT_SYM_LINKS) {
Packit 90a5c9
            if (((rv = apr_stat(&rnew->finfo, rnew->filename,
Packit 90a5c9
                                APR_FINFO_MIN, rnew->pool)) != APR_SUCCESS)
Packit 90a5c9
                && (rv != APR_INCOMPLETE)) {
Packit 90a5c9
                rnew->finfo.filetype = APR_NOFILE;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            if (((rv = apr_stat(&rnew->finfo, rnew->filename,
Packit 90a5c9
                                APR_FINFO_LINK | APR_FINFO_MIN,
Packit 90a5c9
                                rnew->pool)) != APR_SUCCESS)
Packit 90a5c9
                && (rv != APR_INCOMPLETE)) {
Packit 90a5c9
                rnew->finfo.filetype = APR_NOFILE;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (r->uri && *r->uri) {
Packit 90a5c9
            char *udir = ap_make_dirstr_parent(rnew->pool, r->uri);
Packit 90a5c9
            rnew->uri = ap_make_full_path(rnew->pool, udir,
Packit 90a5c9
                                          rnew->filename + fdirlen);
Packit 90a5c9
            ap_parse_uri(rnew, rnew->uri);    /* fill in parsed_uri values */
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            ap_parse_uri(rnew, new_file);        /* fill in parsed_uri values */
Packit 90a5c9
            rnew->uri = apr_pstrdup(rnew->pool, "");
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /* XXX: @@@: What should be done with the parsed_uri values?
Packit 90a5c9
         * We would be better off stripping down to the 'common' elements
Packit 90a5c9
         * of the path, then reassembling the URI as best as we can.
Packit 90a5c9
         */
Packit 90a5c9
        ap_parse_uri(rnew, new_file);        /* fill in parsed_uri values */
Packit 90a5c9
        /*
Packit 90a5c9
         * XXX: this should be set properly like it is in the same-dir case
Packit 90a5c9
         * but it's actually sometimes to impossible to do it... because the
Packit 90a5c9
         * file may not have a uri associated with it -djg
Packit 90a5c9
         */
Packit 90a5c9
        rnew->uri = apr_pstrdup(rnew->pool, "");
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* We cannot return NULL without violating the API. So just turn this
Packit 90a5c9
     * subrequest into a 500. */
Packit 90a5c9
    if (ap_is_recursion_limit_exceeded(r)) {
Packit 90a5c9
        rnew->status = HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
        return rnew;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if ((res = ap_process_request_internal(rnew))) {
Packit 90a5c9
        rnew->status = res;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return rnew;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(int) ap_run_sub_req(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    int retval = DECLINED;
Packit 90a5c9
    /* Run the quick handler if the subrequest is not a dirent or file
Packit 90a5c9
     * subrequest
Packit 90a5c9
     */
Packit 90a5c9
    if (!(r->filename && r->finfo.filetype != APR_NOFILE)) {
Packit 90a5c9
        retval = ap_run_quick_handler(r, 0);
Packit 90a5c9
    }
Packit 90a5c9
    if (retval != OK) {
Packit 90a5c9
        retval = ap_invoke_handler(r);
Packit 90a5c9
        if (retval == DONE) {
Packit 90a5c9
            retval = OK;
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    ap_finalize_sub_req_protocol(r);
Packit 90a5c9
    return retval;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE(void) ap_destroy_sub_req(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    /* Reclaim the space */
Packit 90a5c9
    apr_pool_destroy(r->pool);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Function to set the r->mtime field to the specified value if it's later
Packit 90a5c9
 * than what's already there.
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(void) ap_update_mtime(request_rec *r, apr_time_t dependency_mtime)
Packit 90a5c9
{
Packit 90a5c9
    if (r->mtime < dependency_mtime) {
Packit 90a5c9
        r->mtime = dependency_mtime;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Is it the initial main request, which we only get *once* per HTTP request?
Packit 90a5c9
 */
Packit 90a5c9
AP_DECLARE(int) ap_is_initial_req(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    return (r->main == NULL)       /* otherwise, this is a sub-request */
Packit 90a5c9
           && (r->prev == NULL);   /* otherwise, this is an internal redirect */
Packit 90a5c9
}
Packit 90a5c9