mod_cern_meta.c revision 577a76180006add04a166b12f1ad130aeedeaa5e
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco/* ====================================================================
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * Redistribution and use in source and binary forms, with or without
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * modification, are permitted provided that the following conditions
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * are met:
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * 1. Redistributions of source code must retain the above copyright
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * notice, this list of conditions and the following disclaimer.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * 2. Redistributions in binary form must reproduce the above copyright
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * notice, this list of conditions and the following disclaimer in
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * the documentation and/or other materials provided with the
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * distribution.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * 3. All advertising materials mentioning features or use of this
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * software must display the following acknowledgment:
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * "This product includes software developed by the Apache Group
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * for use in the Apache HTTP server project (http://www.apache.org/)."
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * 4. The names "Apache Server" and "Apache Group" must not be used to
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * endorse or promote products derived from this software without
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * prior written permission. For written permission, please contact
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * apache@apache.org.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * 5. Products derived from this software may not be called "Apache"
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * nor may "Apache" appear in their names without prior written
e7443960658cc31ccf48ea5b9833b13887ee14e7Lubos Kosco * permission of the Apache Group.
3dc379d98a11243103242e2b43b1ab1322795b7eLubos Kosco *
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco * 6. Redistributions of any form whatsoever must retain the following
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco * acknowledgment:
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco * "This product includes software developed by the Apache Group
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco * for use in the Apache HTTP server project (http://www.apache.org/)."
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco *
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * OF THE POSSIBILITY OF SUCH DAMAGE.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * ====================================================================
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * This software consists of voluntary contributions made by many
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * individuals on behalf of the Apache Group and was originally based
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * on public domain software written at the National Center for
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * Supercomputing Applications, University of Illinois, Urbana-Champaign.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * For more information on the Apache Group and the Apache HTTP server
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * project, please see <http://www.apache.org/>.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco */
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco/*
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * mod_cern_meta.c
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco * version 0.1.0
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco * status beta
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco *
aa437711254f8bb8be871b7d0a4b530b718cbf4eLubos Kosco * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 25.Jan.96
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * *** IMPORTANT ***
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * This version of mod_cern_meta.c controls Meta File behaviour on a
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * per-directory basis. Previous versions of the module defined behaviour
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * on a per-server basis. The upshot is that you'll need to revisit your
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * configuration files in order to make use of the new module.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * ***
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * Emulate the CERN HTTPD Meta file semantics. Meta files are HTTP
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * headers that can be output in addition to the normal range of
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * headers for each file accessed. They appear rather like the Apache
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * .asis files, and are able to provide a crude way of influencing
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * the Expires: header, as well as providing other curiosities.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * There are many ways to manage meta information, this one was
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * chosen because there is already a large number of CERN users
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * who can exploit this module. It should be noted that there are probably
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * more sensitive ways of managing the Expires: header specifically.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * The module obeys the following directives, which can appear
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * in the server's .conf files and in .htaccess files.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * MetaFiles <on|off>
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * turns on|off meta file processing for any directory.
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * Default value is off
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * # turn on MetaFiles in this directory
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * MetaFiles on
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * MetaDir <directory name>
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * specifies the name of the directory in which Apache can find
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * meta information files. The directory is usually a 'hidden'
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * subdirectory of the directory that contains the file being
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * accessed. eg:
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * # .meta files are in the *same* directory as the
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * # file being accessed
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * MetaDir .
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * the default is to look in a '.web' subdirectory. This is the
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * same as for CERN 3.+ webservers and behaviour is the same as
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * for the directive:
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco * MetaDir .web
1b28b233e40eae109c16d5b1b917dffb64e45f94Lubos Kosco *
* MetaSuffix <meta file suffix>
*
* specifies the file name suffix for the file containing the
* meta information. eg:
*
* # our meta files are suffixed with '.cern_meta'
* MetaSuffix .cern_meta
*
* the default is to look for files with the suffix '.meta'. This
* behaviour is the same as for the directive:
*
* MetaSuffix .meta
*
* When accessing the file
*
* DOCUMENT_ROOT/somedir/index.html
*
* this module will look for the file
*
* DOCUMENT_ROOT/somedir/.web/index.html.meta
*
* and will use its contents to generate additional MIME header
* information.
*
* For more information on the CERN Meta file semantics see:
*
* http://www.w3.org/hypertext/WWW/Daemon/User/Config/General.html#MetaDir
*
* Change-log:
* 29.Jan.96 pfopen/pfclose instead of fopen/fclose
* DECLINE when real file not found, we may be checking each
* of the index.html/index.shtml/index.htm variants and don't
* need to report missing ones as spurious errors.
* 31.Jan.96 log_error reports about a malformed .meta file, rather
* than a script error.
* 20.Jun.96 MetaFiles <on|off> default off, added, so that module
* can be configured per-directory. Prior to this the module
* was running for each request anywhere on the server, naughty..
* 29.Jun.96 All directives made per-directory.
*/
#include "httpd.h"
#include "http_config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include "util_script.h"
#include "http_log.h"
#include "http_request.h"
#define DIR_CMD_PERMS OR_INDEXES
#define DEFAULT_METADIR ".web"
#define DEFAULT_METASUFFIX ".meta"
#define DEFAULT_METAFILES 0
module MODULE_VAR_EXPORT cern_meta_module;
typedef struct {
char *metadir;
char *metasuffix;
char *metafiles;
} cern_meta_dir_config;
static void *create_cern_meta_dir_config(pool *p, char *dummy)
{
cern_meta_dir_config *new =
(cern_meta_dir_config *) ap_palloc(p, sizeof(cern_meta_dir_config));
new->metadir = NULL;
new->metasuffix = NULL;
new->metafiles = DEFAULT_METAFILES;
return new;
}
static void *merge_cern_meta_dir_configs(pool *p, void *basev, void *addv)
{
cern_meta_dir_config *base = (cern_meta_dir_config *) basev;
cern_meta_dir_config *add = (cern_meta_dir_config *) addv;
cern_meta_dir_config *new =
(cern_meta_dir_config *) ap_palloc(p, sizeof(cern_meta_dir_config));
new->metadir = add->metadir ? add->metadir : base->metadir;
new->metasuffix = add->metasuffix ? add->metasuffix : base->metasuffix;
new->metafiles = add->metafiles;
return new;
}
static const char *set_metadir(cmd_parms *parms, cern_meta_dir_config * dconf, char *arg)
{
dconf->metadir = arg;
return NULL;
}
static const char *set_metasuffix(cmd_parms *parms, cern_meta_dir_config * dconf, char *arg)
{
dconf->metasuffix = arg;
return NULL;
}
static const char *set_metafiles(cmd_parms *parms, cern_meta_dir_config * dconf, char *arg)
{
dconf->metafiles = arg;
return NULL;
}
static const command_rec cern_meta_cmds[] =
{
{"MetaFiles", set_metafiles, NULL, DIR_CMD_PERMS, FLAG,
"Limited to 'on' or 'off'"},
{"MetaDir", set_metadir, NULL, DIR_CMD_PERMS, TAKE1,
"the name of the directory containing meta files"},
{"MetaSuffix", set_metasuffix, NULL, DIR_CMD_PERMS, TAKE1,
"the filename suffix for meta files"},
{NULL}
};
/* XXX: this is very similar to ap_scan_script_header_err_core...
* are the differences deliberate, or just a result of bit rot?
*/
static int scan_meta_file(request_rec *r, FILE *f)
{
char w[MAX_STRING_LEN];
char *l;
int p;
table *tmp_headers;
tmp_headers = ap_make_table(r->pool, 5);
while (fgets(w, MAX_STRING_LEN - 1, f) != NULL) {
/* Delete terminal (CR?)LF */
p = strlen(w);
if (p > 0 && w[p - 1] == '\n') {
if (p > 1 && w[p - 2] == '\015')
w[p - 2] = '\0';
else
w[p - 1] = '\0';
}
if (w[0] == '\0') {
return OK;
}
/* if we see a bogus header don't ignore it. Shout and scream */
if (!(l = strchr(w, ':'))) {
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
"malformed header in meta file: %s", r->filename);
return SERVER_ERROR;
}
*l++ = '\0';
while (*l && ap_isspace(*l))
++l;
if (!strcasecmp(w, "Content-type")) {
char *tmp;
/* Nuke trailing whitespace */
char *endp = l + strlen(l) - 1;
while (endp > l && ap_isspace(*endp))
*endp-- = '\0';
tmp = ap_pstrdup(r->pool, l);
ap_content_type_tolower(tmp);
r->content_type = tmp;
}
else if (!strcasecmp(w, "Status")) {
sscanf(l, "%d", &r->status);
r->status_line = ap_pstrdup(r->pool, l);
}
else {
ap_table_set(tmp_headers, w, l);
}
}
ap_overlap_tables(r->headers_out, tmp_headers, AP_OVERLAP_TABLES_SET);
return OK;
}
static int add_cern_meta_data(request_rec *r)
{
char *metafilename;
char *last_slash;
char *real_file;
char *scrap_book;
FILE *f;
cern_meta_dir_config *dconf;
int rv;
request_rec *rr;
dconf = ap_get_module_config(r->per_dir_config, &cern_meta_module);
if (!dconf->metafiles) {
return DECLINED;
};
/* if ./.web/$1.meta exists then output 'asis' */
if (r->finfo.st_mode == 0) {
return DECLINED;
};
/* is this a directory? */
if (S_ISDIR(r->finfo.st_mode) || r->uri[strlen(r->uri) - 1] == '/') {
return DECLINED;
};
/* what directory is this file in? */
scrap_book = ap_pstrdup(r->pool, r->filename);
/* skip leading slash, recovered in later processing */
scrap_book++;
last_slash = strrchr(scrap_book, '/');
if (last_slash != NULL) {
/* skip over last slash */
real_file = last_slash;
real_file++;
*last_slash = '\0';
}
else {
/* no last slash, buh?! */
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r,
"internal error in mod_cern_meta: %s", r->filename);
/* should really barf, but hey, let's be friends... */
return DECLINED;
};
metafilename = ap_pstrcat(r->pool, "/", scrap_book, "/",
dconf->metadir ? dconf->metadir : DEFAULT_METADIR,
"/", real_file,
dconf->metasuffix ? dconf->metasuffix : DEFAULT_METASUFFIX,
NULL);
/* XXX: it sucks to require this subrequest to complete, because this
* means people must leave their meta files accessible to the world.
* A better solution might be a "safe open" feature of pfopen to avoid
* pipes, symlinks, and crap like that.
*/
rr = ap_sub_req_lookup_file(metafilename, r);
if (rr->status != HTTP_OK) {
ap_destroy_sub_req(rr);
return DECLINED;
}
ap_destroy_sub_req(rr);
f = ap_pfopen(r->pool, metafilename, "r");
if (f == NULL) {
if (errno == ENOENT) {
return DECLINED;
}
ap_log_rerror(APLOG_MARK, APLOG_ERR, r,
"meta file permissions deny server access: %s", metafilename);
return FORBIDDEN;
};
/* read the headers in */
rv = scan_meta_file(r, f);
ap_pfclose(r->pool, f);
return rv;
}
static void register_hooks(void)
{
ap_hook_fixups(add_cern_meta_data,NULL,NULL,HOOK_MIDDLE);
}
module MODULE_VAR_EXPORT cern_meta_module =
{
STANDARD20_MODULE_STUFF,
create_cern_meta_dir_config,/* dir config creater */
merge_cern_meta_dir_configs,/* dir merger --- default is to override */
NULL, /* server config */
NULL, /* merge server configs */
cern_meta_cmds, /* command table */
NULL, /* handlers */
register_hooks /* register hooks */
};