842ae4bd224140319ae7feec1872b93dfd491143fielding/* Licensed to the Apache Software Foundation (ASF) under one or more
842ae4bd224140319ae7feec1872b93dfd491143fielding * contributor license agreements. See the NOTICE file distributed with
842ae4bd224140319ae7feec1872b93dfd491143fielding * this work for additional information regarding copyright ownership.
842ae4bd224140319ae7feec1872b93dfd491143fielding * The ASF licenses this file to You under the Apache License, Version 2.0
842ae4bd224140319ae7feec1872b93dfd491143fielding * (the "License"); you may not use this file except in compliance with
842ae4bd224140319ae7feec1872b93dfd491143fielding * the License. You may obtain a copy of the License at
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd * limitations under the License.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * version 0.1.0
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * status beta
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 25.Jan.96
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * *** IMPORTANT ***
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * This version of mod_cern_meta.c controls Meta File behaviour on a
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * per-directory basis. Previous versions of the module defined behaviour
e8f95a682820a599fe41b22977010636be5c2717jim * on a per-server basis. The upshot is that you'll need to revisit your
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * configuration files in order to make use of the new module.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Emulate the CERN HTTPD Meta file semantics. Meta files are HTTP
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * headers that can be output in addition to the normal range of
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * headers for each file accessed. They appear rather like the Apache
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * .asis files, and are able to provide a crude way of influencing
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the Expires: header, as well as providing other curiosities.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * There are many ways to manage meta information, this one was
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * chosen because there is already a large number of CERN users
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * who can exploit this module. It should be noted that there are probably
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * more sensitive ways of managing the Expires: header specifically.
e8f95a682820a599fe41b22977010636be5c2717jim * The module obeys the following directives, which can appear
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * in the server's .conf files and in .htaccess files.
e8f95a682820a599fe41b22977010636be5c2717jim * MetaFiles <on|off>
e8f95a682820a599fe41b22977010636be5c2717jim * turns on|off meta file processing for any directory.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Default value is off
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * # turn on MetaFiles in this directory
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * MetaFiles on
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * MetaDir <directory name>
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * specifies the name of the directory in which Apache can find
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * meta information files. The directory is usually a 'hidden'
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * subdirectory of the directory that contains the file being
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * accessed. eg:
e8f95a682820a599fe41b22977010636be5c2717jim * # .meta files are in the *same* directory as the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * # file being accessed
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * MetaDir .
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the default is to look in a '.web' subdirectory. This is the
e8f95a682820a599fe41b22977010636be5c2717jim * same as for CERN 3.+ webservers and behaviour is the same as
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * for the directive:
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * MetaDir .web
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * MetaSuffix <meta file suffix>
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * specifies the file name suffix for the file containing the
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * meta information. eg:
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * # our meta files are suffixed with '.cern_meta'
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * MetaSuffix .cern_meta
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * the default is to look for files with the suffix '.meta'. This
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * behaviour is the same as for the directive:
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * MetaSuffix .meta
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * When accessing the file
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * this module will look for the file
e8f95a682820a599fe41b22977010636be5c2717jim * and will use its contents to generate additional MIME header
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * information.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * For more information on the CERN Meta file semantics see:
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * http://www.w3.org/hypertext/WWW/Daemon/User/Config/General.html#MetaDir
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * Change-log:
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * DECLINE when real file not found, we may be checking each
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * of the index.html/index.shtml/index.htm variants and don't
e8f95a682820a599fe41b22977010636be5c2717jim * need to report missing ones as spurious errors.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * 31.Jan.96 log_error reports about a malformed .meta file, rather
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * than a script error.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * 20.Jun.96 MetaFiles <on|off> default off, added, so that module
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * can be configured per-directory. Prior to this the module
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * was running for each request anywhere on the server, naughty..
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * 29.Jun.96 All directives made per-directory.
0f081398cf0eef8cc7c66a535d450110a92dc8aefieldingtypedef struct {
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void *create_cern_meta_dir_config(apr_pool_t *p, char *dummy)
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm (cern_meta_dir_config *) apr_palloc(p, sizeof(cern_meta_dir_config));
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic void *merge_cern_meta_dir_configs(apr_pool_t *p, void *basev, void *addv)
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding cern_meta_dir_config *base = (cern_meta_dir_config *) basev;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding cern_meta_dir_config *add = (cern_meta_dir_config *) addv;
1ccd992d37d62c8cb2056126f2234f64ec189bfddougm (cern_meta_dir_config *) apr_palloc(p, sizeof(cern_meta_dir_config));
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding new->metadir = add->metadir ? add->metadir : base->metadir;
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding new->metasuffix = add->metasuffix ? add->metasuffix : base->metasuffix;
ba09062ce0c7e0d6904a53aaf99896f5924b3989trawickstatic const char *set_metadir(cmd_parms *parms, void *in_dconf, const char *arg)
ba09062ce0c7e0d6904a53aaf99896f5924b3989trawickstatic const char *set_metasuffix(cmd_parms *parms, void *in_dconf, const char *arg)
ba09062ce0c7e0d6904a53aaf99896f5924b3989trawickstatic const char *set_metafiles(cmd_parms *parms, void *in_dconf, int arg)
ba09062ce0c7e0d6904a53aaf99896f5924b3989trawick AP_INIT_FLAG("MetaFiles", set_metafiles, NULL, DIR_CMD_PERMS,
ba09062ce0c7e0d6904a53aaf99896f5924b3989trawick "Limited to 'on' or 'off'"),
ba09062ce0c7e0d6904a53aaf99896f5924b3989trawick AP_INIT_TAKE1("MetaDir", set_metadir, NULL, DIR_CMD_PERMS,
ba09062ce0c7e0d6904a53aaf99896f5924b3989trawick "the name of the directory containing meta files"),
ba09062ce0c7e0d6904a53aaf99896f5924b3989trawick AP_INIT_TAKE1("MetaSuffix", set_metasuffix, NULL, DIR_CMD_PERMS,
ba09062ce0c7e0d6904a53aaf99896f5924b3989trawick "the filename suffix for meta files"),
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding/* XXX: this is very similar to ap_scan_script_header_err_core...
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * are the differences deliberate, or just a result of bit rot?
1ccd992d37d62c8cb2056126f2234f64ec189bfddougmstatic int scan_meta_file(request_rec *r, apr_file_t *f)
066877f1a045103acfdd376d48cdd473c33f409bdougm while (apr_file_gets(w, MAX_STRING_LEN - 1, f) == APR_SUCCESS) {
66724147170b966b8ce54b0b5f9d9bcc67641b90nd /* Delete terminal (CR?)LF */
66724147170b966b8ce54b0b5f9d9bcc67641b90nd if (w[0] == '\0') {
66724147170b966b8ce54b0b5f9d9bcc67641b90nd /* if we see a bogus header don't ignore it. Shout and scream */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01560)
66724147170b966b8ce54b0b5f9d9bcc67641b90nd *l++ = '\0';
66724147170b966b8ce54b0b5f9d9bcc67641b90nd /* Nuke trailing whitespace */
066877f1a045103acfdd376d48cdd473c33f409bdougm apr_table_overlap(r->headers_out, tmp_headers, APR_OVERLAP_TABLES_SET);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding dconf = ap_get_module_config(r->per_dir_config, &cern_meta_module);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* if ./.web/$1.meta exists then output 'asis' */
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* is this a directory? */
d9f2c22804bcbc9ad07d4ec18fbb8aa7c042dcd0bjh if (r->finfo.filetype == APR_DIR || r->uri[strlen(r->uri) - 1] == '/') {
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* what directory is this file in? */
f3259946a9f26eb57f0eeb3fb0e840da809c5495stoddard if ((last_slash != NULL) && (last_slash != leading_slash)) {
66724147170b966b8ce54b0b5f9d9bcc67641b90nd /* skip over last slash */
66724147170b966b8ce54b0b5f9d9bcc67641b90nd /* no last slash, buh?! */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01561)
66724147170b966b8ce54b0b5f9d9bcc67641b90nd /* should really barf, but hey, let's be friends... */
f3259946a9f26eb57f0eeb3fb0e840da809c5495stoddard metafilename = apr_pstrcat(r->pool, scrap_book, "/",
66724147170b966b8ce54b0b5f9d9bcc67641b90nd dconf->metasuffix ? dconf->metasuffix : DEFAULT_METASUFFIX,
c5b6ab7b079b3fe0f13115786ad52dff3c0af87cwrowe /* It sucks to require this subrequest to complete, because this
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * means people must leave their meta files accessible to the world.
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * A better solution might be a "safe open" feature of pfopen to avoid
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding * pipes, symlinks, and crap like that.
c5b6ab7b079b3fe0f13115786ad52dff3c0af87cwrowe * In fact, this doesn't suck. Because <Location > blocks are never run
c5b6ab7b079b3fe0f13115786ad52dff3c0af87cwrowe * against sub_req_lookup_file, the meta can be somewhat protected by
c5b6ab7b079b3fe0f13115786ad52dff3c0af87cwrowe * either masking it with a <Location > directive or alias, or stowing
c5b6ab7b079b3fe0f13115786ad52dff3c0af87cwrowe * the file outside of the web document tree, while providing the
c5b6ab7b079b3fe0f13115786ad52dff3c0af87cwrowe * appropriate directory blocks to allow access to it as a file.
1f78f6d87f69c241a3e903a46bae4666d17a8513trawick retcode = apr_file_open(&f, metafilename, APR_READ, APR_OS_DEFAULT, r->pool);
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(01562)
66724147170b966b8ce54b0b5f9d9bcc67641b90nd "meta file permissions deny server access: %s", metafilename);
0f081398cf0eef8cc7c66a535d450110a92dc8aefielding /* read the headers in */
b980ad7fdc218b4855cde9f75a747527f50c554dwrowe ap_hook_fixups(add_cern_meta_data,NULL,NULL,APR_HOOK_MIDDLE);
66724147170b966b8ce54b0b5f9d9bcc67641b90nd merge_cern_meta_dir_configs, /* dir merger --- default is to override */