]> granicus.if.org Git - apache/blob - modules/mappers/mod_dir.c
9391a919b7aaf3ee002d13eddff7193e4879fcb1
[apache] / modules / mappers / mod_dir.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_dir.c: handle default index files, and trailing-/ redirects
61  */
62
63 #include "apr_strings.h"
64 #include "ap_config.h"
65 #include "httpd.h"
66 #include "http_config.h"
67 #include "http_core.h"
68 #include "http_request.h"
69 #include "http_protocol.h"
70 #include "http_log.h"
71 #include "http_main.h"
72 #include "util_script.h"
73
74 module AP_MODULE_DECLARE_DATA dir_module;
75
76 typedef struct dir_config_struct {
77     apr_array_header_t *index_names;
78 } dir_config_rec;
79
80 #define DIR_CMD_PERMS OR_INDEXES
81
82 static const char *add_index(cmd_parms *cmd, void *dummy, const char *arg)
83 {
84     dir_config_rec *d = dummy;
85
86     if (!d->index_names) {
87         d->index_names = apr_make_array(cmd->pool, 2, sizeof(char *));
88     }
89     *(const char **)apr_push_array(d->index_names) = arg;
90     return NULL;
91 }
92
93 static const command_rec dir_cmds[] =
94 {
95     AP_INIT_ITERATE("DirectoryIndex", add_index, NULL, DIR_CMD_PERMS,
96                     "a list of file names"),
97     {NULL}
98 };
99
100 static void *create_dir_config(apr_pool_t *p, char *dummy)
101 {
102     dir_config_rec *new =
103     (dir_config_rec *) apr_pcalloc(p, sizeof(dir_config_rec));
104
105     new->index_names = NULL;
106     return (void *) new;
107 }
108
109 static void *merge_dir_configs(apr_pool_t *p, void *basev, void *addv)
110 {
111     dir_config_rec *new = (dir_config_rec *) apr_pcalloc(p, sizeof(dir_config_rec));
112     dir_config_rec *base = (dir_config_rec *) basev;
113     dir_config_rec *add = (dir_config_rec *) addv;
114
115     new->index_names = add->index_names ? add->index_names : base->index_names;
116     return new;
117 }
118
119 static int handle_dir(const char *handler,request_rec *r)
120 {
121     dir_config_rec *d;
122     char *dummy_ptr[1];
123     char **names_ptr;
124     int num_names;
125     int error_notfound = 0;
126
127     if(strcmp(handler,DIR_MAGIC_TYPE))
128         return DECLINED;
129
130     d = (dir_config_rec *) ap_get_module_config(r->per_dir_config,
131                                                 &dir_module);
132
133     if (r->uri[0] == '\0' || r->uri[strlen(r->uri) - 1] != '/') {
134         char *ifile;
135         if (r->args != NULL)
136             ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
137                             "/", "?", r->args, NULL);
138         else
139             ifile = apr_pstrcat(r->pool, ap_escape_uri(r->pool, r->uri),
140                             "/", NULL);
141
142         apr_table_setn(r->headers_out, "Location",
143                   ap_construct_url(r->pool, ifile, r));
144         return HTTP_MOVED_PERMANENTLY;
145     }
146
147     /* KLUDGE --- make the sub_req lookups happen in the right directory.
148      * Fixing this in the sub_req_lookup functions themselves is difficult,
149      * and would probably break virtual includes...
150      */
151
152     if (r->filename[strlen(r->filename) - 1] != '/') {
153         r->filename = apr_pstrcat(r->pool, r->filename, "/", NULL);
154     }
155
156     if (d->index_names) {
157         names_ptr = (char **)d->index_names->elts;
158         num_names = d->index_names->nelts;
159     }
160     else {
161         dummy_ptr[0] = AP_DEFAULT_INDEX;
162         names_ptr = dummy_ptr;
163         num_names = 1;
164     }
165
166     for (; num_names; ++names_ptr, --num_names) {
167         char *name_ptr = *names_ptr;
168         request_rec *rr = ap_sub_req_lookup_uri(name_ptr, r, NULL);
169
170         if (rr->status == HTTP_OK && rr->finfo.filetype == APR_REG) {
171             char *new_uri = ap_escape_uri(r->pool, rr->uri);
172
173             if (rr->args != NULL)
174                 new_uri = apr_pstrcat(r->pool, new_uri, "?", rr->args, NULL);
175             else if (r->args != NULL)
176                 new_uri = apr_pstrcat(r->pool, new_uri, "?", r->args, NULL);
177
178             ap_destroy_sub_req(rr);
179             ap_internal_redirect(new_uri, r);
180             return OK;
181         }
182
183         /* If the request returned a redirect, propagate it to the client */
184
185         if (ap_is_HTTP_REDIRECT(rr->status) ||
186             (rr->status == HTTP_NOT_ACCEPTABLE && num_names == 1)) {
187
188             apr_pool_join(r->pool, rr->pool);
189             error_notfound = rr->status;
190             r->notes = apr_overlay_tables(r->pool, r->notes, rr->notes);
191             r->headers_out = apr_overlay_tables(r->pool, r->headers_out,
192                                             rr->headers_out);
193             r->err_headers_out = apr_overlay_tables(r->pool, r->err_headers_out,
194                                                 rr->err_headers_out);
195             return error_notfound;
196         }
197
198         /* If the request returned something other than 404 (or 200),
199          * it means the module encountered some sort of problem. To be
200          * secure, we should return the error, rather than create
201          * along a (possibly unsafe) directory index.
202          *
203          * So we store the error, and if none of the listed files
204          * exist, we return the last error response we got, instead
205          * of a directory listing.
206          */
207         if (rr->status && rr->status != HTTP_NOT_FOUND && rr->status != HTTP_OK)
208             error_notfound = rr->status;
209
210         ap_destroy_sub_req(rr);
211     }
212
213     if (error_notfound)
214         return error_notfound;
215
216     if (r->method_number != M_GET)
217         return DECLINED;
218
219     /* nothing for us to do, pass on through */
220
221     return DECLINED;
222 }
223
224
225 static void register_hooks(void)
226 {
227     static const char * const aszSucc[]={ "mod_autoindex.c", NULL };
228
229     ap_hook_handler(handle_dir,NULL,aszSucc,AP_HOOK_MIDDLE);
230 }
231
232 module AP_MODULE_DECLARE_DATA dir_module = {
233     STANDARD20_MODULE_STUFF,
234     create_dir_config,          /* create per-directory config structure */
235     merge_dir_configs,          /* merge per-directory config structures */
236     NULL,                       /* create per-server config structure */
237     NULL,                       /* merge per-server config structures */
238     dir_cmds,                   /* command apr_table_t */
239     register_hooks              /* register hooks */
240 };