]> granicus.if.org Git - apache/blob - modules/metadata/mod_cern_meta.c
adjust remaining modules to use the new handler hook method (Alan Edwards)
[apache] / modules / metadata / mod_cern_meta.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000 The Apache Software Foundation.  All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Apache" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation.  For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  * Portions of this software are based upon public domain software
55  * originally written at the National Center for Supercomputing Applications,
56  * University of Illinois, Urbana-Champaign.
57  */
58
59 /*
60  * mod_cern_meta.c
61  * version 0.1.0
62  * status beta
63  * 
64  * Andrew Wilson <Andrew.Wilson@cm.cf.ac.uk> 25.Jan.96
65  *
66  * *** IMPORTANT ***
67  * This version of mod_cern_meta.c controls Meta File behaviour on a
68  * per-directory basis.  Previous versions of the module defined behaviour
69  * on a per-server basis.  The upshot is that you'll need to revisit your 
70  * configuration files in order to make use of the new module.
71  * ***
72  *
73  * Emulate the CERN HTTPD Meta file semantics.  Meta files are HTTP
74  * headers that can be output in addition to the normal range of
75  * headers for each file accessed.  They appear rather like the Apache
76  * .asis files, and are able to provide a crude way of influencing
77  * the Expires: header, as well as providing other curiosities.
78  * There are many ways to manage meta information, this one was
79  * chosen because there is already a large number of CERN users
80  * who can exploit this module.  It should be noted that there are probably
81  * more sensitive ways of managing the Expires: header specifically.
82  *
83  * The module obeys the following directives, which can appear 
84  * in the server's .conf files and in .htaccess files.
85  *
86  *  MetaFiles <on|off> 
87  *
88  *    turns on|off meta file processing for any directory.  
89  *    Default value is off
90  *
91  *        # turn on MetaFiles in this directory
92  *        MetaFiles on
93  *
94  *  MetaDir <directory name>
95  *      
96  *    specifies the name of the directory in which Apache can find
97  *    meta information files.  The directory is usually a 'hidden'
98  *    subdirectory of the directory that contains the file being
99  *    accessed.  eg:
100  *
101  *        # .meta files are in the *same* directory as the 
102  *        # file being accessed
103  *        MetaDir .
104  *
105  *    the default is to look in a '.web' subdirectory. This is the
106  *    same as for CERN 3.+ webservers and behaviour is the same as 
107  *    for the directive:
108  *
109  *        MetaDir .web
110  *
111  *  MetaSuffix <meta file suffix>
112  *
113  *    specifies the file name suffix for the file containing the
114  *    meta information.  eg:
115  *
116  *       # our meta files are suffixed with '.cern_meta'
117  *       MetaSuffix .cern_meta
118  *
119  *    the default is to look for files with the suffix '.meta'.  This
120  *    behaviour is the same as for the directive:
121  *
122  *       MetaSuffix .meta
123  *
124  * When accessing the file
125  *
126  *   DOCUMENT_ROOT/somedir/index.html
127  *
128  * this module will look for the file
129  *
130  *   DOCUMENT_ROOT/somedir/.web/index.html.meta
131  *
132  * and will use its contents to generate additional MIME header 
133  * information.
134  *
135  * For more information on the CERN Meta file semantics see:
136  *
137  *   http://www.w3.org/hypertext/WWW/Daemon/User/Config/General.html#MetaDir
138  *
139  * Change-log:
140  * 29.Jan.96 pfopen/pfclose instead of fopen/fclose
141  *           DECLINE when real file not found, we may be checking each
142  *           of the index.html/index.shtml/index.htm variants and don't
143  *           need to report missing ones as spurious errors. 
144  * 31.Jan.96 log_error reports about a malformed .meta file, rather
145  *           than a script error.
146  * 20.Jun.96 MetaFiles <on|off> default off, added, so that module
147  *           can be configured per-directory.  Prior to this the module
148  *           was running for each request anywhere on the server, naughty..
149  * 29.Jun.96 All directives made per-directory.
150  */
151
152 #include "ap_config.h"
153 #include "httpd.h"
154 #include "http_config.h"
155 #include "util_script.h"
156 #include "http_log.h"
157 #include "http_request.h"
158 #include "apr_strings.h"
159
160 #ifdef HAVE_SYS_TYPES_H
161 #include <sys/types.h>
162 #endif
163 #ifdef HAVE_STRINGS_H
164 #include <strings.h>
165 #endif
166
167 #define DIR_CMD_PERMS OR_INDEXES
168
169 #define DEFAULT_METADIR         ".web"
170 #define DEFAULT_METASUFFIX      ".meta"
171 #define DEFAULT_METAFILES       0
172
173 module AP_MODULE_DECLARE_DATA cern_meta_module;
174
175 typedef struct {
176     const char *metadir;
177     const char *metasuffix;
178     int metafiles;
179 } cern_meta_dir_config;
180
181 static void *create_cern_meta_dir_config(apr_pool_t *p, char *dummy)
182 {
183     cern_meta_dir_config *new =
184     (cern_meta_dir_config *) apr_palloc(p, sizeof(cern_meta_dir_config));
185
186     new->metadir = NULL;
187     new->metasuffix = NULL;
188     new->metafiles = DEFAULT_METAFILES;
189
190     return new;
191 }
192
193 static void *merge_cern_meta_dir_configs(apr_pool_t *p, void *basev, void *addv)
194 {
195     cern_meta_dir_config *base = (cern_meta_dir_config *) basev;
196     cern_meta_dir_config *add = (cern_meta_dir_config *) addv;
197     cern_meta_dir_config *new =
198     (cern_meta_dir_config *) apr_palloc(p, sizeof(cern_meta_dir_config));
199
200     new->metadir = add->metadir ? add->metadir : base->metadir;
201     new->metasuffix = add->metasuffix ? add->metasuffix : base->metasuffix;
202     new->metafiles = add->metafiles;
203
204     return new;
205 }
206
207 static const char *set_metadir(cmd_parms *parms, void *in_dconf, const char *arg)
208 {
209     cern_meta_dir_config *dconf = in_dconf;
210
211     dconf->metadir = arg;
212     return NULL;
213 }
214
215 static const char *set_metasuffix(cmd_parms *parms, void *in_dconf, const char *arg)
216 {
217     cern_meta_dir_config *dconf = in_dconf;
218
219     dconf->metasuffix = arg;
220     return NULL;
221 }
222
223 static const char *set_metafiles(cmd_parms *parms, void *in_dconf, int arg)
224 {
225     cern_meta_dir_config *dconf = in_dconf;
226
227     dconf->metafiles = arg;
228     return NULL;
229 }
230
231
232 static const command_rec cern_meta_cmds[] =
233 {
234     AP_INIT_FLAG("MetaFiles", set_metafiles, NULL, DIR_CMD_PERMS,
235                  "Limited to 'on' or 'off'"),
236     AP_INIT_TAKE1("MetaDir", set_metadir, NULL, DIR_CMD_PERMS,
237                   "the name of the directory containing meta files"),
238     AP_INIT_TAKE1("MetaSuffix", set_metasuffix, NULL, DIR_CMD_PERMS,
239                   "the filename suffix for meta files"),
240     {NULL}
241 };
242
243 /* XXX: this is very similar to ap_scan_script_header_err_core...
244  * are the differences deliberate, or just a result of bit rot?
245  */
246 static int scan_meta_file(request_rec *r, apr_file_t *f)
247 {
248     char w[MAX_STRING_LEN];
249     char *l;
250     int p;
251     apr_table_t *tmp_headers;
252
253     tmp_headers = apr_make_table(r->pool, 5);
254     while (apr_fgets(w, MAX_STRING_LEN - 1, f) == APR_SUCCESS) {
255
256         /* Delete terminal (CR?)LF */
257
258         p = strlen(w);
259         if (p > 0 && w[p - 1] == '\n') {
260             if (p > 1 && w[p - 2] == '\015')
261                 w[p - 2] = '\0';
262             else
263                 w[p - 1] = '\0';
264         }
265
266         if (w[0] == '\0') {
267             return OK;
268         }
269
270         /* if we see a bogus header don't ignore it. Shout and scream */
271
272         if (!(l = strchr(w, ':'))) {
273             ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
274                         "malformed header in meta file: %s", r->filename);
275             return HTTP_INTERNAL_SERVER_ERROR;
276         }
277
278         *l++ = '\0';
279         while (*l && apr_isspace(*l))
280             ++l;
281
282         if (!strcasecmp(w, "Content-type")) {
283             char *tmp;
284             /* Nuke trailing whitespace */
285
286             char *endp = l + strlen(l) - 1;
287             while (endp > l && apr_isspace(*endp))
288                 *endp-- = '\0';
289
290             tmp = apr_pstrdup(r->pool, l);
291             ap_content_type_tolower(tmp);
292             r->content_type = tmp;
293         }
294         else if (!strcasecmp(w, "Status")) {
295             sscanf(l, "%d", &r->status);
296             r->status_line = apr_pstrdup(r->pool, l);
297         }
298         else {
299             apr_table_set(tmp_headers, w, l);
300         }
301     }
302     apr_overlap_tables(r->headers_out, tmp_headers, APR_OVERLAP_TABLES_SET);
303     return OK;
304 }
305
306 static int add_cern_meta_data(request_rec *r)
307 {
308     char *metafilename;
309     char *last_slash;
310     char *real_file;
311     char *scrap_book;
312     apr_file_t *f = NULL;
313     apr_status_t retcode;
314     cern_meta_dir_config *dconf;
315     int rv;
316     request_rec *rr;
317
318     dconf = ap_get_module_config(r->per_dir_config, &cern_meta_module);
319
320     if (!dconf->metafiles) {
321         return DECLINED;
322     };
323
324     /* if ./.web/$1.meta exists then output 'asis' */
325
326     if (r->finfo.protection == 0) {
327         return DECLINED;
328     };
329
330     /* is this a directory? */
331     if (r->finfo.filetype == APR_DIR || r->uri[strlen(r->uri) - 1] == '/') {
332         return DECLINED;
333     };
334
335     /* what directory is this file in? */
336     scrap_book = apr_pstrdup(r->pool, r->filename);
337     /* skip leading slash, recovered in later processing */
338     scrap_book++;
339     last_slash = strrchr(scrap_book, '/');
340     if (last_slash != NULL) {
341         /* skip over last slash */
342         real_file = last_slash;
343         real_file++;
344         *last_slash = '\0';
345     }
346     else {
347         /* no last slash, buh?! */
348         ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r,
349                     "internal error in mod_cern_meta: %s", r->filename);
350         /* should really barf, but hey, let's be friends... */
351         return DECLINED;
352     };
353
354     metafilename = apr_pstrcat(r->pool, "/", scrap_book, "/",
355                            dconf->metadir ? dconf->metadir : DEFAULT_METADIR,
356                            "/", real_file,
357                  dconf->metasuffix ? dconf->metasuffix : DEFAULT_METASUFFIX,
358                            NULL);
359
360     /* XXX: it sucks to require this subrequest to complete, because this
361      * means people must leave their meta files accessible to the world.
362      * A better solution might be a "safe open" feature of pfopen to avoid
363      * pipes, symlinks, and crap like that.
364      */
365     rr = ap_sub_req_lookup_file(metafilename, r, NULL);
366     if (rr->status != HTTP_OK) {
367         ap_destroy_sub_req(rr);
368         return DECLINED;
369     }
370     ap_destroy_sub_req(rr);
371
372     retcode = apr_open(&f, metafilename, APR_READ | APR_CREATE, APR_OS_DEFAULT, r->pool);
373     if (retcode != APR_SUCCESS) {
374         if (APR_STATUS_IS_ENOENT(retcode)) {
375             return DECLINED;
376         }
377         ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
378               "meta file permissions deny server access: %s", metafilename);
379         return HTTP_FORBIDDEN;
380     };
381
382     /* read the headers in */
383     rv = scan_meta_file(r, f);
384     apr_close(f);
385
386     return rv;
387 }
388 static void register_hooks(void)
389 {
390     ap_hook_fixups(add_cern_meta_data,NULL,NULL,AP_HOOK_MIDDLE);
391 }
392 module AP_MODULE_DECLARE_DATA cern_meta_module =
393 {
394     STANDARD20_MODULE_STUFF,
395     create_cern_meta_dir_config,/* dir config creater */
396     merge_cern_meta_dir_configs,/* dir merger --- default is to override */
397     NULL,                       /* server config */
398     NULL,                       /* merge server configs */
399     cern_meta_cmds,             /* command apr_table_t */
400     register_hooks              /* register hooks */
401 };