Blame modules/metadata/mod_cern_meta.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_cern_meta.c
Packit 90a5c9
 * version 0.1.0
Packit 90a5c9
 * status beta
Packit 90a5c9
 *
Packit 90a5c9
 * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 25.Jan.96
Packit 90a5c9
 *
Packit 90a5c9
 * *** IMPORTANT ***
Packit 90a5c9
 * This version of mod_cern_meta.c controls Meta File behaviour on a
Packit 90a5c9
 * per-directory basis.  Previous versions of the module defined behaviour
Packit 90a5c9
 * on a per-server basis.  The upshot is that you'll need to revisit your
Packit 90a5c9
 * configuration files in order to make use of the new module.
Packit 90a5c9
 * ***
Packit 90a5c9
 *
Packit 90a5c9
 * Emulate the CERN HTTPD Meta file semantics.  Meta files are HTTP
Packit 90a5c9
 * headers that can be output in addition to the normal range of
Packit 90a5c9
 * headers for each file accessed.  They appear rather like the Apache
Packit 90a5c9
 * .asis files, and are able to provide a crude way of influencing
Packit 90a5c9
 * the Expires: header, as well as providing other curiosities.
Packit 90a5c9
 * There are many ways to manage meta information, this one was
Packit 90a5c9
 * chosen because there is already a large number of CERN users
Packit 90a5c9
 * who can exploit this module.  It should be noted that there are probably
Packit 90a5c9
 * more sensitive ways of managing the Expires: header specifically.
Packit 90a5c9
 *
Packit 90a5c9
 * The module obeys the following directives, which can appear
Packit 90a5c9
 * in the server's .conf files and in .htaccess files.
Packit 90a5c9
 *
Packit 90a5c9
 *  MetaFiles <on|off>
Packit 90a5c9
 *
Packit 90a5c9
 *    turns on|off meta file processing for any directory.
Packit 90a5c9
 *    Default value is off
Packit 90a5c9
 *
Packit 90a5c9
 *        # turn on MetaFiles in this directory
Packit 90a5c9
 *        MetaFiles on
Packit 90a5c9
 *
Packit 90a5c9
 *  MetaDir <directory name>
Packit 90a5c9
 *
Packit 90a5c9
 *    specifies the name of the directory in which Apache can find
Packit 90a5c9
 *    meta information files.  The directory is usually a 'hidden'
Packit 90a5c9
 *    subdirectory of the directory that contains the file being
Packit 90a5c9
 *    accessed.  eg:
Packit 90a5c9
 *
Packit 90a5c9
 *        # .meta files are in the *same* directory as the
Packit 90a5c9
 *        # file being accessed
Packit 90a5c9
 *        MetaDir .
Packit 90a5c9
 *
Packit 90a5c9
 *    the default is to look in a '.web' subdirectory. This is the
Packit 90a5c9
 *    same as for CERN 3.+ webservers and behaviour is the same as
Packit 90a5c9
 *    for the directive:
Packit 90a5c9
 *
Packit 90a5c9
 *        MetaDir .web
Packit 90a5c9
 *
Packit 90a5c9
 *  MetaSuffix <meta file suffix>
Packit 90a5c9
 *
Packit 90a5c9
 *    specifies the file name suffix for the file containing the
Packit 90a5c9
 *    meta information.  eg:
Packit 90a5c9
 *
Packit 90a5c9
 *       # our meta files are suffixed with '.cern_meta'
Packit 90a5c9
 *       MetaSuffix .cern_meta
Packit 90a5c9
 *
Packit 90a5c9
 *    the default is to look for files with the suffix '.meta'.  This
Packit 90a5c9
 *    behaviour is the same as for the directive:
Packit 90a5c9
 *
Packit 90a5c9
 *       MetaSuffix .meta
Packit 90a5c9
 *
Packit 90a5c9
 * When accessing the file
Packit 90a5c9
 *
Packit 90a5c9
 *   DOCUMENT_ROOT/somedir/index.html
Packit 90a5c9
 *
Packit 90a5c9
 * this module will look for the file
Packit 90a5c9
 *
Packit 90a5c9
 *   DOCUMENT_ROOT/somedir/.web/index.html.meta
Packit 90a5c9
 *
Packit 90a5c9
 * and will use its contents to generate additional MIME header
Packit 90a5c9
 * information.
Packit 90a5c9
 *
Packit 90a5c9
 * For more information on the CERN Meta file semantics see:
Packit 90a5c9
 *
Packit 90a5c9
 *   http://www.w3.org/hypertext/WWW/Daemon/User/Config/General.html#MetaDir
Packit 90a5c9
 *
Packit 90a5c9
 * Change-log:
Packit 90a5c9
 * 29.Jan.96 pfopen/pfclose instead of fopen/fclose
Packit 90a5c9
 *           DECLINE when real file not found, we may be checking each
Packit 90a5c9
 *           of the index.html/index.shtml/index.htm variants and don't
Packit 90a5c9
 *           need to report missing ones as spurious errors.
Packit 90a5c9
 * 31.Jan.96 log_error reports about a malformed .meta file, rather
Packit 90a5c9
 *           than a script error.
Packit 90a5c9
 * 20.Jun.96 MetaFiles <on|off> default off, added, so that module
Packit 90a5c9
 *           can be configured per-directory.  Prior to this the module
Packit 90a5c9
 *           was running for each request anywhere on the server, naughty..
Packit 90a5c9
 * 29.Jun.96 All directives made per-directory.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "apr.h"
Packit 90a5c9
#include "apr_strings.h"
Packit 90a5c9
Packit 90a5c9
#define APR_WANT_STRFUNC
Packit 90a5c9
#include "apr_want.h"
Packit 90a5c9
Packit 90a5c9
#if APR_HAVE_SYS_TYPES_H
Packit 90a5c9
#include <sys/types.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#include "httpd.h"
Packit 90a5c9
#include "http_config.h"
Packit 90a5c9
#include "util_script.h"
Packit 90a5c9
#include "http_log.h"
Packit 90a5c9
#include "http_request.h"
Packit 90a5c9
#include "http_protocol.h"
Packit 90a5c9
#include "apr_lib.h"
Packit 90a5c9
Packit 90a5c9
#define DIR_CMD_PERMS OR_INDEXES
Packit 90a5c9
Packit 90a5c9
#define DEFAULT_METADIR     ".web"
Packit 90a5c9
#define DEFAULT_METASUFFIX  ".meta"
Packit 90a5c9
#define DEFAULT_METAFILES   0
Packit 90a5c9
Packit 90a5c9
module AP_MODULE_DECLARE_DATA cern_meta_module;
Packit 90a5c9
Packit 90a5c9
typedef struct {
Packit 90a5c9
    const char *metadir;
Packit 90a5c9
    const char *metasuffix;
Packit 90a5c9
    int metafiles;
Packit 90a5c9
} cern_meta_dir_config;
Packit 90a5c9
Packit 90a5c9
static void *create_cern_meta_dir_config(apr_pool_t *p, char *dummy)
Packit 90a5c9
{
Packit 90a5c9
    cern_meta_dir_config *new =
Packit 90a5c9
    (cern_meta_dir_config *) apr_palloc(p, sizeof(cern_meta_dir_config));
Packit 90a5c9
Packit 90a5c9
    new->metadir = NULL;
Packit 90a5c9
    new->metasuffix = NULL;
Packit 90a5c9
    new->metafiles = DEFAULT_METAFILES;
Packit 90a5c9
Packit 90a5c9
    return new;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void *merge_cern_meta_dir_configs(apr_pool_t *p, void *basev, void *addv)
Packit 90a5c9
{
Packit 90a5c9
    cern_meta_dir_config *base = (cern_meta_dir_config *) basev;
Packit 90a5c9
    cern_meta_dir_config *add = (cern_meta_dir_config *) addv;
Packit 90a5c9
    cern_meta_dir_config *new =
Packit 90a5c9
    (cern_meta_dir_config *) apr_palloc(p, sizeof(cern_meta_dir_config));
Packit 90a5c9
Packit 90a5c9
    new->metadir = add->metadir ? add->metadir : base->metadir;
Packit 90a5c9
    new->metasuffix = add->metasuffix ? add->metasuffix : base->metasuffix;
Packit 90a5c9
    new->metafiles = add->metafiles;
Packit 90a5c9
Packit 90a5c9
    return new;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *set_metadir(cmd_parms *parms, void *in_dconf, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    cern_meta_dir_config *dconf = in_dconf;
Packit 90a5c9
Packit 90a5c9
    dconf->metadir = arg;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *set_metasuffix(cmd_parms *parms, void *in_dconf, const char *arg)
Packit 90a5c9
{
Packit 90a5c9
    cern_meta_dir_config *dconf = in_dconf;
Packit 90a5c9
Packit 90a5c9
    dconf->metasuffix = arg;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static const char *set_metafiles(cmd_parms *parms, void *in_dconf, int arg)
Packit 90a5c9
{
Packit 90a5c9
    cern_meta_dir_config *dconf = in_dconf;
Packit 90a5c9
Packit 90a5c9
    dconf->metafiles = arg;
Packit 90a5c9
    return NULL;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
static const command_rec cern_meta_cmds[] =
Packit 90a5c9
{
Packit 90a5c9
    AP_INIT_FLAG("MetaFiles", set_metafiles, NULL, DIR_CMD_PERMS,
Packit 90a5c9
                 "Limited to 'on' or 'off'"),
Packit 90a5c9
    AP_INIT_TAKE1("MetaDir", set_metadir, NULL, DIR_CMD_PERMS,
Packit 90a5c9
                  "the name of the directory containing meta files"),
Packit 90a5c9
    AP_INIT_TAKE1("MetaSuffix", set_metasuffix, NULL, DIR_CMD_PERMS,
Packit 90a5c9
                  "the filename suffix for meta files"),
Packit 90a5c9
    {NULL}
Packit 90a5c9
};
Packit 90a5c9
Packit 90a5c9
/* XXX: this is very similar to ap_scan_script_header_err_core...
Packit 90a5c9
 * are the differences deliberate, or just a result of bit rot?
Packit 90a5c9
 */
Packit 90a5c9
static int scan_meta_file(request_rec *r, apr_file_t *f)
Packit 90a5c9
{
Packit 90a5c9
    char w[MAX_STRING_LEN];
Packit 90a5c9
    char *l;
Packit 90a5c9
    int p;
Packit 90a5c9
    apr_table_t *tmp_headers;
Packit 90a5c9
Packit 90a5c9
    tmp_headers = apr_table_make(r->pool, 5);
Packit 90a5c9
    while (apr_file_gets(w, MAX_STRING_LEN - 1, f) == APR_SUCCESS) {
Packit 90a5c9
Packit 90a5c9
    /* Delete terminal (CR?)LF */
Packit 90a5c9
        p = strlen(w);
Packit 90a5c9
        if (p > 0 && w[p - 1] == '\n') {
Packit 90a5c9
            if (p > 1 && w[p - 2] == '\015')
Packit 90a5c9
                w[p - 2] = '\0';
Packit 90a5c9
            else
Packit 90a5c9
                w[p - 1] = '\0';
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        if (w[0] == '\0') {
Packit 90a5c9
            return OK;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /* if we see a bogus header don't ignore it. Shout and scream */
Packit 90a5c9
Packit 90a5c9
        if (!(l = strchr(w, ':'))) {
Packit 90a5c9
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01560)
Packit 90a5c9
                "malformed header in meta file: %s", r->filename);
Packit 90a5c9
            return HTTP_INTERNAL_SERVER_ERROR;
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        *l++ = '\0';
Packit 90a5c9
        while (apr_isspace(*l))
Packit 90a5c9
            ++l;
Packit 90a5c9
Packit 90a5c9
        if (!strcasecmp(w, "Content-type")) {
Packit 90a5c9
            char *tmp;
Packit 90a5c9
            /* Nuke trailing whitespace */
Packit 90a5c9
Packit 90a5c9
            char *endp = l + strlen(l) - 1;
Packit 90a5c9
            while (endp > l && apr_isspace(*endp))
Packit 90a5c9
                *endp-- = '\0';
Packit 90a5c9
Packit 90a5c9
            tmp = apr_pstrdup(r->pool, l);
Packit 90a5c9
            ap_content_type_tolower(tmp);
Packit 90a5c9
            ap_set_content_type(r, tmp);
Packit 90a5c9
        }
Packit 90a5c9
        else if (!strcasecmp(w, "Status")) {
Packit 90a5c9
            sscanf(l, "%d", &r->status);
Packit 90a5c9
            r->status_line = apr_pstrdup(r->pool, l);
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            apr_table_set(tmp_headers, w, l);
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    apr_table_overlap(r->headers_out, tmp_headers, APR_OVERLAP_TABLES_SET);
Packit 90a5c9
    return OK;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static int add_cern_meta_data(request_rec *r)
Packit 90a5c9
{
Packit 90a5c9
    char *metafilename;
Packit 90a5c9
    char *leading_slash;
Packit 90a5c9
    char *last_slash;
Packit 90a5c9
    char *real_file;
Packit 90a5c9
    char *scrap_book;
Packit 90a5c9
    apr_file_t *f = NULL;
Packit 90a5c9
    apr_status_t retcode;
Packit 90a5c9
    cern_meta_dir_config *dconf;
Packit 90a5c9
    int rv;
Packit 90a5c9
    request_rec *rr;
Packit 90a5c9
Packit 90a5c9
    dconf = ap_get_module_config(r->per_dir_config, &cern_meta_module);
Packit 90a5c9
Packit 90a5c9
    if (!dconf->metafiles) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* if ./.web/$1.meta exists then output 'asis' */
Packit 90a5c9
Packit 90a5c9
    if (r->finfo.filetype == APR_NOFILE) {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* is this a directory? */
Packit 90a5c9
    if (r->finfo.filetype == APR_DIR || r->uri[strlen(r->uri) - 1] == '/') {
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* what directory is this file in? */
Packit 90a5c9
    scrap_book = apr_pstrdup(r->pool, r->filename);
Packit 90a5c9
Packit 90a5c9
    leading_slash = strchr(scrap_book, '/');
Packit 90a5c9
    last_slash = strrchr(scrap_book, '/');
Packit 90a5c9
    if ((last_slash != NULL) && (last_slash != leading_slash)) {
Packit 90a5c9
        /* skip over last slash */
Packit 90a5c9
        real_file = last_slash;
Packit 90a5c9
        real_file++;
Packit 90a5c9
        *last_slash = '\0';
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        /* no last slash, buh?! */
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01561)
Packit 90a5c9
            "internal error in mod_cern_meta: %s", r->filename);
Packit 90a5c9
        /* should really barf, but hey, let's be friends... */
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    metafilename = apr_pstrcat(r->pool, scrap_book, "/",
Packit 90a5c9
               dconf->metadir ? dconf->metadir : DEFAULT_METADIR,
Packit 90a5c9
               "/", real_file,
Packit 90a5c9
         dconf->metasuffix ? dconf->metasuffix : DEFAULT_METASUFFIX,
Packit 90a5c9
               NULL);
Packit 90a5c9
Packit 90a5c9
    /* It sucks to require this subrequest to complete, because this
Packit 90a5c9
     * means people must leave their meta files accessible to the world.
Packit 90a5c9
     * A better solution might be a "safe open" feature of pfopen to avoid
Packit 90a5c9
     * pipes, symlinks, and crap like that.
Packit 90a5c9
     *
Packit 90a5c9
     * In fact, this doesn't suck.  Because <Location > blocks are never run
Packit 90a5c9
     * against sub_req_lookup_file, the meta can be somewhat protected by
Packit 90a5c9
     * either masking it with a <Location > directive or alias, or stowing
Packit 90a5c9
     * the file outside of the web document tree, while providing the
Packit 90a5c9
     * appropriate directory blocks to allow access to it as a file.
Packit 90a5c9
     */
Packit 90a5c9
    rr = ap_sub_req_lookup_file(metafilename, r, NULL);
Packit 90a5c9
    if (rr->status != HTTP_OK) {
Packit 90a5c9
        ap_destroy_sub_req(rr);
Packit 90a5c9
        return DECLINED;
Packit 90a5c9
    }
Packit 90a5c9
    ap_destroy_sub_req(rr);
Packit 90a5c9
Packit 90a5c9
    retcode = apr_file_open(&f, metafilename, APR_READ, APR_OS_DEFAULT, r->pool);
Packit 90a5c9
    if (retcode != APR_SUCCESS) {
Packit 90a5c9
        if (APR_STATUS_IS_ENOENT(retcode)) {
Packit 90a5c9
            return DECLINED;
Packit 90a5c9
        }
Packit 90a5c9
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01562)
Packit 90a5c9
            "meta file permissions deny server access: %s", metafilename);
Packit 90a5c9
        return HTTP_FORBIDDEN;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    /* read the headers in */
Packit 90a5c9
    rv = scan_meta_file(r, f);
Packit 90a5c9
    apr_file_close(f);
Packit 90a5c9
Packit 90a5c9
    return rv;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
static void register_hooks(apr_pool_t *p)
Packit 90a5c9
{
Packit 90a5c9
    ap_hook_fixups(add_cern_meta_data,NULL,NULL,APR_HOOK_MIDDLE);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
AP_DECLARE_MODULE(cern_meta) =
Packit 90a5c9
{
Packit 90a5c9
    STANDARD20_MODULE_STUFF,
Packit 90a5c9
    create_cern_meta_dir_config, /* dir config creater */
Packit 90a5c9
    merge_cern_meta_dir_configs, /* dir merger --- default is to override */
Packit 90a5c9
    NULL,                        /* server config */
Packit 90a5c9
    NULL,                        /* merge server configs */
Packit 90a5c9
    cern_meta_cmds,              /* command apr_table_t */
Packit 90a5c9
    register_hooks               /* register hooks */
Packit 90a5c9
};