Blame modules/mappers/mod_dir.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
 * mod_dir.c: handle default index files, and trailing-/ redirects
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
#include "apr_lib.h"
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "http_core.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "http_main.h"
Packit 90a5c9
#include "util_script.h"
Packit 90a5c9
#include "mod_rewrite.h"
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA dir_module;
Packit 90a5c9
Packit 90a5c9
typedef enum {
Packit 90a5c9
    MODDIR_OFF = 0,
Packit 90a5c9
    MODDIR_ON,
Packit 90a5c9
    MODDIR_UNSET
Packit 90a5c9
} moddir_cfg;
Packit 90a5c9
Packit 90a5c9
#define REDIRECT_OFF   0
Packit 90a5c9
#define REDIRECT_UNSET 1
Packit 90a5c9
Packit 90a5c9
typedef struct dir_config_struct {
Packit 90a5c9
    apr_array_header_t *index_names;
Packit 90a5c9
    moddir_cfg do_slash;
Packit 90a5c9
    moddir_cfg checkhandler;
Packit 90a5c9
    int redirect_index;
Packit 90a5c9
    const char *dflt;
Packit 90a5c9
} dir_config_rec;
Packit 90a5c9
Packit 90a5c9
#define DIR_CMD_PERMS OR_INDEXES
Packit 90a5c9
Packit 90a5c9
static const char *add_index(cmd_parms *cmd, void *dummy, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    dir_config_rec *d = dummy;
Packit 90a5c9
    const char *t, *w;
Packit 90a5c9
    int count = 0;
Packit 90a5c9
Packit 90a5c9
    if (!d->index_names) {
Packit 90a5c9
        d->index_names = apr_array_make(cmd->pool, 2, sizeof(char *));
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    t = arg;
Packit 90a5c9
    while ((w = ap_getword_conf(cmd->pool, &t)) && w[0]) {
Packit 90a5c9
        if (count == 0 && !strcasecmp(w, "disabled")) {
Packit 90a5c9
            /* peek to see if "disabled" is first in a series of arguments */
Packit 90a5c9
            const char *tt = t;
Packit 90a5c9
            const char *ww = ap_getword_conf(cmd->temp_pool, &tt;;
Packit 90a5c9
            if (ww[0] == '\0') {
Packit 90a5c9
               /* "disabled" is first, and alone */
Packit 90a5c9
               apr_array_clear(d->index_names); 
Packit 90a5c9
               break;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
        *(const char **)apr_array_push(d->index_names) = w;
Packit 90a5c9
        count++;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *configure_slash(cmd_parms *cmd, void *d_, int arg)
Packit 90a5c9
{
Packit 90a5c9
    dir_config_rec *d = d_;
Packit 90a5c9
Packit 90a5c9
    d->do_slash = arg ? MODDIR_ON : MODDIR_OFF;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
static const char *configure_checkhandler(cmd_parms *cmd, void *d_, int arg)
Packit 90a5c9
{
Packit 90a5c9
    dir_config_rec *d = d_;
Packit 90a5c9
Packit 90a5c9
    d->checkhandler = arg ? MODDIR_ON : MODDIR_OFF;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
static const char *configure_redirect(cmd_parms *cmd, void *d_, const char *arg1)
Packit 90a5c9
{
Packit 90a5c9
    dir_config_rec *d = d_;
Packit 90a5c9
    int status;
Packit 90a5c9
Packit 90a5c9
    if (!strcasecmp(arg1, "ON"))
Packit 90a5c9
        status = HTTP_MOVED_TEMPORARILY;
Packit 90a5c9
    else if (!strcasecmp(arg1, "OFF"))
Packit 90a5c9
        status = REDIRECT_OFF;
Packit 90a5c9
    else if (!strcasecmp(arg1, "permanent"))
Packit 90a5c9
        status = HTTP_MOVED_PERMANENTLY;
Packit 90a5c9
    else if (!strcasecmp(arg1, "temp"))
Packit 90a5c9
        status = HTTP_MOVED_TEMPORARILY;
Packit 90a5c9
    else if (!strcasecmp(arg1, "seeother"))
Packit 90a5c9
        status = HTTP_SEE_OTHER;
Packit 90a5c9
    else if (apr_isdigit(*arg1)) {
Packit 90a5c9
        status = atoi(arg1);
Packit 90a5c9
        if (!ap_is_HTTP_REDIRECT(status)) {
Packit 90a5c9
            return "DirectoryIndexRedirect only accepts values between 300 and 399";
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        return "DirectoryIndexRedirect ON|OFF|permanent|temp|seeother|3xx";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    d->redirect_index = status;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
static const command_rec dir_cmds[] =
Packit 90a5c9
{
Packit 90a5c9
    AP_INIT_TAKE1("FallbackResource", ap_set_string_slot,
Packit 90a5c9
                  (void*)APR_OFFSETOF(dir_config_rec, dflt),
Packit 90a5c9
                  DIR_CMD_PERMS, "Set a default handler"),
Packit 90a5c9
    AP_INIT_RAW_ARGS("DirectoryIndex", add_index, NULL, DIR_CMD_PERMS,
Packit 90a5c9
                    "a list of file names"),
Packit 90a5c9
    AP_INIT_FLAG("DirectorySlash", configure_slash, NULL, DIR_CMD_PERMS,
Packit 90a5c9
                 "On or Off"),
Packit 90a5c9
    AP_INIT_FLAG("DirectoryCheckHandler", configure_checkhandler, NULL, DIR_CMD_PERMS,
Packit 90a5c9
                 "On or Off"),
Packit 90a5c9
    AP_INIT_TAKE1("DirectoryIndexRedirect", configure_redirect,
Packit 90a5c9
                   NULL, DIR_CMD_PERMS, "On, Off, or a 3xx status code."),
Packit 90a5c9
Packit 90a5c9
    {NULL}
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
static void *create_dir_config(apr_pool_t *p, char *dummy)
Packit 90a5c9
{
Packit 90a5c9
    dir_config_rec *new = apr_pcalloc(p, sizeof(dir_config_rec));
Packit 90a5c9
Packit 90a5c9
    new->index_names = NULL;
Packit 90a5c9
    new->do_slash = MODDIR_UNSET;
Packit 90a5c9
    new->checkhandler = MODDIR_UNSET;
Packit 90a5c9
    new->redirect_index = REDIRECT_UNSET;
Packit 90a5c9
    return (void *) new;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *merge_dir_configs(apr_pool_t *p, void *basev, void *addv)
Packit 90a5c9
{
Packit 90a5c9
    dir_config_rec *new = apr_pcalloc(p, sizeof(dir_config_rec));
Packit 90a5c9
    dir_config_rec *base = (dir_config_rec *)basev;
Packit 90a5c9
    dir_config_rec *add = (dir_config_rec *)addv;
Packit 90a5c9
Packit 90a5c9
    new->index_names = add->index_names ? add->index_names : base->index_names;
Packit 90a5c9
    new->do_slash =
Packit 90a5c9
        (add->do_slash == MODDIR_UNSET) ? base->do_slash : add->do_slash;
Packit 90a5c9
    new->checkhandler =
Packit 90a5c9
        (add->checkhandler == MODDIR_UNSET) ? base->checkhandler : add->checkhandler;
Packit 90a5c9
    new->redirect_index=
Packit 90a5c9
        (add->redirect_index == REDIRECT_UNSET) ? base->redirect_index : add->redirect_index;
Packit 90a5c9
    new->dflt = add->dflt ? add->dflt : base->dflt;
Packit 90a5c9
    return new;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int fixup_dflt(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    dir_config_rec *d = ap_get_module_config(r->per_dir_config, &dir_module);
Packit 90a5c9
    const char *name_ptr;
Packit 90a5c9
    request_rec *rr;
Packit 90a5c9
    int error_notfound = 0;
Packit 90a5c9
Packit 90a5c9
    name_ptr = d->dflt;
Packit 90a5c9
    if ((name_ptr == NULL) || !(strcasecmp(name_ptr,"disabled"))){
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
    /* XXX: if FallbackResource points to something that doesn't exist,
Packit 90a5c9
     * this may recurse until it hits the limit for internal redirects
Packit 90a5c9
     * before returning an Internal Server Error.
Packit 90a5c9
     */
Packit 90a5c9
Packit 90a5c9
    /* The logic of this function is basically cloned and simplified
Packit 90a5c9
     * from fixup_dir below.  See the comments there.
Packit 90a5c9
     */
Packit 90a5c9
    if (r->args != NULL) {
Packit 90a5c9
        name_ptr = apr_pstrcat(r->pool, name_ptr, "?", r->args, NULL);
Packit 90a5c9
    }
Packit 90a5c9
    rr = ap_sub_req_lookup_uri(name_ptr, r, r->output_filters);
Packit 90a5c9
    if (rr->status == HTTP_OK
Packit 90a5c9
        && (   (rr->handler && !strcmp(rr->handler, "proxy-server"))
Packit 90a5c9
            || rr->finfo.filetype == APR_REG)) {
Packit 90a5c9
        ap_internal_fast_redirect(rr, r);
Packit 90a5c9
        return OK;
Packit 90a5c9
    }
Packit 90a5c9
    else if (ap_is_HTTP_REDIRECT(rr->status)) {
Packit 90a5c9
Packit 90a5c9
        apr_pool_join(r->pool, rr->pool);
Packit 90a5c9
        r->notes = apr_table_overlay(r->pool, r->notes, rr->notes);
Packit 90a5c9
        r->headers_out = apr_table_overlay(r->pool, r->headers_out,
Packit 90a5c9
                                           rr->headers_out);
Packit 90a5c9
        r->err_headers_out = apr_table_overlay(r->pool, r->err_headers_out,
Packit 90a5c9
                                               rr->err_headers_out);
Packit 90a5c9
        error_notfound = rr->status;
Packit 90a5c9
    }
Packit 90a5c9
    else if (rr->status && rr->status != HTTP_NOT_FOUND
Packit 90a5c9
             && rr->status != HTTP_OK) {
Packit 90a5c9
        error_notfound = rr->status;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    ap_destroy_sub_req(rr);
Packit 90a5c9
    if (error_notfound) {
Packit 90a5c9
        return error_notfound;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* nothing for us to do, pass on through */
Packit 90a5c9
    return DECLINED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int fixup_dir(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    dir_config_rec *d;
Packit 90a5c9
    char *dummy_ptr[1];
Packit 90a5c9
    char **names_ptr;
Packit 90a5c9
    int num_names;
Packit 90a5c9
    int error_notfound = 0;
Packit 90a5c9
Packit 90a5c9
    /* In case mod_mime wasn't present, and no handler was assigned. */
Packit 90a5c9
    if (!r->handler) {
Packit 90a5c9
        r->handler = DIR_MAGIC_TYPE;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* Never tolerate path_info on dir requests */
Packit 90a5c9
    if (r->path_info && *r->path_info) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    d = (dir_config_rec *)ap_get_module_config(r->per_dir_config,
Packit 90a5c9
                                               &dir_module);
Packit 90a5c9
Packit 90a5c9
    /* Redirect requests that are not '/' terminated */
Packit 90a5c9
    if (r->uri[0] == '\0' || r->uri[strlen(r->uri) - 1] != '/')
Packit 90a5c9
    {
Packit 90a5c9
        char *ifile;
Packit 90a5c9
Packit 90a5c9
        if (!d->do_slash) {
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* Only redirect non-get requests if we have no note to warn
Packit 90a5c9
         * that this browser cannot handle redirs on non-GET requests
Packit 90a5c9
         * (such as Microsoft's WebFolders).
Packit 90a5c9
         */
Packit 90a5c9
        if ((r->method_number != M_GET)
Packit 90a5c9
                && apr_table_get(r->subprocess_env, "redirect-carefully")) {
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (r->args != NULL) {
Packit 90a5c9
            ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
Packit 90a5c9
                                "/?", r->args, NULL);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
Packit 90a5c9
                                "/", NULL);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        apr_table_setn(r->headers_out, "Location",
Packit 90a5c9
                       ap_construct_url(r->pool, ifile, r));
Packit 90a5c9
        return HTTP_MOVED_PERMANENTLY;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* we're running between mod_rewrites fixup and its internal redirect handler, step aside */
Packit 90a5c9
    if (!strcmp(r->handler, REWRITE_REDIRECT_HANDLER_NAME)) { 
Packit 90a5c9
        /* Prevent DIR_MAGIC_TYPE from leaking out when someone has taken over */
Packit 90a5c9
        if (!strcmp(r->content_type, DIR_MAGIC_TYPE)) { 
Packit 90a5c9
            r->content_type = NULL;
Packit 90a5c9
        }
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (d->checkhandler == MODDIR_ON && strcmp(r->handler, DIR_MAGIC_TYPE)) {
Packit 90a5c9
        /* Prevent DIR_MAGIC_TYPE from leaking out when someone has taken over */
Packit 90a5c9
        if (!strcmp(r->content_type, DIR_MAGIC_TYPE)) { 
Packit 90a5c9
            r->content_type = NULL;
Packit 90a5c9
        }
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (d->index_names) {
Packit 90a5c9
        names_ptr = (char **)d->index_names->elts;
Packit 90a5c9
        num_names = d->index_names->nelts;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        dummy_ptr[0] = AP_DEFAULT_INDEX;
Packit 90a5c9
        names_ptr = dummy_ptr;
Packit 90a5c9
        num_names = 1;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    for (; num_names; ++names_ptr, --num_names) {
Packit 90a5c9
        /* XXX: Is this name_ptr considered escaped yet, or not??? */
Packit 90a5c9
        char *name_ptr = *names_ptr;
Packit 90a5c9
        request_rec *rr;
Packit 90a5c9
Packit 90a5c9
        /* Once upon a time args were handled _after_ the successful redirect.
Packit 90a5c9
         * But that redirect might then _refuse_ the given r->args, creating
Packit 90a5c9
         * a nasty tangle.  It seems safer to consider the r->args while we
Packit 90a5c9
         * determine if name_ptr is our viable index, and therefore set them
Packit 90a5c9
         * up correctly on redirect.
Packit 90a5c9
         */
Packit 90a5c9
        if (r->args != NULL) {
Packit 90a5c9
            name_ptr = apr_pstrcat(r->pool, name_ptr, "?", r->args, NULL);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        rr = ap_sub_req_lookup_uri(name_ptr, r, r->output_filters);
Packit 90a5c9
Packit 90a5c9
        /* The sub request lookup is very liberal, and the core map_to_storage
Packit 90a5c9
         * handler will almost always result in HTTP_OK as /foo/index.html
Packit 90a5c9
         * may be /foo with PATH_INFO="/index.html", or even / with
Packit 90a5c9
         * PATH_INFO="/foo/index.html". To get around this we insist that the
Packit 90a5c9
         * the index be a regular filetype.
Packit 90a5c9
         *
Packit 90a5c9
         * Another reason is that the core handler also makes the assumption
Packit 90a5c9
         * that if r->finfo is still NULL by the time it gets called, the
Packit 90a5c9
         * file does not exist.
Packit 90a5c9
         */
Packit 90a5c9
        if (rr->status == HTTP_OK
Packit 90a5c9
            && (   (rr->handler && !strcmp(rr->handler, "proxy-server"))
Packit 90a5c9
                || rr->finfo.filetype == APR_REG)) {
Packit 90a5c9
Packit 90a5c9
            if (ap_is_HTTP_REDIRECT(d->redirect_index)) {
Packit 90a5c9
                apr_table_setn(r->headers_out, "Location", ap_construct_url(r->pool, rr->uri, r));
Packit 90a5c9
                return d->redirect_index;
Packit 90a5c9
            }
Packit 90a5c9
Packit 90a5c9
            ap_internal_fast_redirect(rr, r);
Packit 90a5c9
            return OK;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* If the request returned a redirect, propagate it to the client */
Packit 90a5c9
Packit 90a5c9
        if (ap_is_HTTP_REDIRECT(rr->status)
Packit 90a5c9
            || (rr->status == HTTP_NOT_ACCEPTABLE && num_names == 1)
Packit 90a5c9
            || (rr->status == HTTP_UNAUTHORIZED && num_names == 1)) {
Packit 90a5c9
Packit 90a5c9
            apr_pool_join(r->pool, rr->pool);
Packit 90a5c9
            error_notfound = rr->status;
Packit 90a5c9
            r->notes = apr_table_overlay(r->pool, r->notes, rr->notes);
Packit 90a5c9
            r->headers_out = apr_table_overlay(r->pool, r->headers_out,
Packit 90a5c9
                                               rr->headers_out);
Packit 90a5c9
            r->err_headers_out = apr_table_overlay(r->pool, r->err_headers_out,
Packit 90a5c9
                                                   rr->err_headers_out);
Packit 90a5c9
            return error_notfound;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* If the request returned something other than 404 (or 200),
Packit 90a5c9
         * it means the module encountered some sort of problem. To be
Packit 90a5c9
         * secure, we should return the error, rather than allow autoindex
Packit 90a5c9
         * to create a (possibly unsafe) directory index.
Packit 90a5c9
         *
Packit 90a5c9
         * So we store the error, and if none of the listed files
Packit 90a5c9
         * exist, we return the last error response we got, instead
Packit 90a5c9
         * of a directory listing.
Packit 90a5c9
         */
Packit 90a5c9
        if (rr->status && rr->status != HTTP_NOT_FOUND
Packit 90a5c9
                && rr->status != HTTP_OK) {
Packit 90a5c9
            error_notfound = rr->status;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        ap_destroy_sub_req(rr);
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    if (error_notfound) {
Packit 90a5c9
        return error_notfound;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* record what we tried, mostly for the benefit of mod_autoindex */
Packit 90a5c9
    apr_table_setn(r->notes, "dir-index-names",
Packit 90a5c9
                   d->index_names ?
Packit 90a5c9
                       apr_array_pstrcat(r->pool, d->index_names, ',') :
Packit 90a5c9
                       AP_DEFAULT_INDEX);
Packit 90a5c9
Packit 90a5c9
    /* nothing for us to do, pass on through */
Packit 90a5c9
    return DECLINED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int dir_fixups(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    if (r->finfo.filetype == APR_DIR) {
Packit 90a5c9
        /* serve up a directory */
Packit 90a5c9
        return fixup_dir(r);
Packit 90a5c9
    }
Packit 90a5c9
    else if ((r->finfo.filetype == APR_NOFILE) && (r->handler == NULL)) {
Packit 90a5c9
        /* No handler and nothing in the filesystem - use fallback */
Packit 90a5c9
        return fixup_dflt(r);
Packit 90a5c9
    }
Packit 90a5c9
    return DECLINED;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    ap_hook_fixups(dir_fixups,NULL,NULL,APR_HOOK_LAST);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(dir) = {
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    create_dir_config,          /* create per-directory config structure */
Packit 90a5c9
    merge_dir_configs,          /* merge per-directory config structures */
Packit 90a5c9
    NULL,                       /* create per-server config structure */
Packit 90a5c9
    NULL,                       /* merge per-server config structures */
Packit 90a5c9
    dir_cmds,                   /* command apr_table_t */
Packit 90a5c9
    register_hooks              /* register hooks */
Packit 90a5c9
};