Apache HTTPD
mod_dir.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * mod_dir.c: handle default index files, and trailing-/ redirects
19 */
20
21#include "apr_strings.h"
22#include "apr_lib.h"
23#include "ap_config.h"
24#include "httpd.h"
25#include "http_config.h"
26#include "http_core.h"
27#include "http_request.h"
28#include "http_protocol.h"
29#include "http_log.h"
30#include "http_main.h"
31#include "util_script.h"
32#include "mod_rewrite.h"
33
34module AP_MODULE_DECLARE_DATA dir_module;
35
41
42#define REDIRECT_OFF 0
43#define REDIRECT_UNSET 1
44
52
53#define DIR_CMD_PERMS OR_INDEXES
54
55static const char *add_index(cmd_parms *cmd, void *dummy, const char *arg)
56{
58 const char *t, *w;
59 int count = 0;
60
61 if (!d->index_names) {
62 d->index_names = apr_array_make(cmd->pool, 2, sizeof(char *));
63 }
64
65 t = arg;
66 while ((w = ap_getword_conf(cmd->pool, &t)) && w[0]) {
67 if (count == 0 && !strcasecmp(w, "disabled")) {
68 /* peek to see if "disabled" is first in a series of arguments */
69 const char *tt = t;
70 const char *ww = ap_getword_conf(cmd->temp_pool, &tt);
71 if (ww[0] == '\0') {
72 /* "disabled" is first, and alone */
73 apr_array_clear(d->index_names);
74 break;
75 }
76 }
77 *(const char **)apr_array_push(d->index_names) = w;
78 count++;
79 }
80
81 return NULL;
82}
83
84static const char *configure_slash(cmd_parms *cmd, void *d_, int arg)
85{
87
89 return NULL;
90}
91static const char *configure_checkhandler(cmd_parms *cmd, void *d_, int arg)
92{
94
96 return NULL;
97}
98static const char *configure_redirect(cmd_parms *cmd, void *d_, const char *arg1)
99{
101 int status;
102
103 if (!strcasecmp(arg1, "ON"))
105 else if (!strcasecmp(arg1, "OFF"))
107 else if (!strcasecmp(arg1, "permanent"))
109 else if (!strcasecmp(arg1, "temp"))
111 else if (!strcasecmp(arg1, "seeother"))
113 else if (apr_isdigit(*arg1)) {
114 status = atoi(arg1);
116 return "DirectoryIndexRedirect only accepts values between 300 and 399";
117 }
118 }
119 else {
120 return "DirectoryIndexRedirect ON|OFF|permanent|temp|seeother|3xx";
121 }
122
123 d->redirect_index = status;
124 return NULL;
125}
126static const command_rec dir_cmds[] =
127{
128 AP_INIT_TAKE1("FallbackResource", ap_set_string_slot,
129 (void*)APR_OFFSETOF(dir_config_rec, dflt),
130 DIR_CMD_PERMS, "Set a default handler"),
131 AP_INIT_RAW_ARGS("DirectoryIndex", add_index, NULL, DIR_CMD_PERMS,
132 "a list of file names"),
134 "On or Off"),
135 AP_INIT_FLAG("DirectoryCheckHandler", configure_checkhandler, NULL, DIR_CMD_PERMS,
136 "On or Off"),
137 AP_INIT_TAKE1("DirectoryIndexRedirect", configure_redirect,
138 NULL, DIR_CMD_PERMS, "On, Off, or a 3xx status code."),
139
140 {NULL}
141};
142
143static void *create_dir_config(apr_pool_t *p, char *dummy)
144{
146
147 new->index_names = NULL;
148 new->do_slash = MODDIR_UNSET;
149 new->checkhandler = MODDIR_UNSET;
150 new->redirect_index = REDIRECT_UNSET;
151 return (void *) new;
152}
153
154static void *merge_dir_configs(apr_pool_t *p, void *basev, void *addv)
155{
159
160 new->index_names = add->index_names ? add->index_names : base->index_names;
161 new->do_slash =
162 (add->do_slash == MODDIR_UNSET) ? base->do_slash : add->do_slash;
163 new->checkhandler =
164 (add->checkhandler == MODDIR_UNSET) ? base->checkhandler : add->checkhandler;
165 new->redirect_index=
166 (add->redirect_index == REDIRECT_UNSET) ? base->redirect_index : add->redirect_index;
167 new->dflt = add->dflt ? add->dflt : base->dflt;
168 return new;
169}
170
172{
174 const char *name_ptr;
176 int error_notfound = 0;
177
178 name_ptr = d->dflt;
179 if ((name_ptr == NULL) || !(strcasecmp(name_ptr,"disabled"))){
180 return DECLINED;
181 }
182 /* XXX: if FallbackResource points to something that doesn't exist,
183 * this may recurse until it hits the limit for internal redirects
184 * before returning an Internal Server Error.
185 */
186
187 /* The logic of this function is basically cloned and simplified
188 * from fixup_dir below. See the comments there.
189 */
190 if (r->args != NULL) {
192 }
194 if (rr->status == HTTP_OK
195 && ( (rr->handler && !strcmp(rr->handler, "proxy-server"))
196 || rr->finfo.filetype == APR_REG)) {
198 return OK;
199 }
200 else if (ap_is_HTTP_REDIRECT(rr->status)) {
201
202 apr_pool_join(r->pool, rr->pool);
203 r->notes = apr_table_overlay(r->pool, r->notes, rr->notes);
205 rr->headers_out);
207 rr->err_headers_out);
208 error_notfound = rr->status;
209 }
210 else if (rr->status && rr->status != HTTP_NOT_FOUND
211 && rr->status != HTTP_OK) {
212 error_notfound = rr->status;
213 }
214
216 if (error_notfound) {
217 return error_notfound;
218 }
219
220 /* nothing for us to do, pass on through */
221 return DECLINED;
222}
223
225{
227 char *dummy_ptr[1];
228 char **names_ptr;
229 int num_names;
230 int error_notfound = 0;
231
232 /* In case mod_mime wasn't present, and no handler was assigned. */
233 if (!r->handler) {
235 }
236
237 /* Never tolerate path_info on dir requests */
238 if (r->path_info && *r->path_info) {
239 return DECLINED;
240 }
241
243 &dir_module);
244
245 /* Redirect requests that are not '/' terminated */
246 if (r->uri[0] == '\0' || r->uri[strlen(r->uri) - 1] != '/')
247 {
248 char *ifile;
249
250 if (!d->do_slash) {
251 return DECLINED;
252 }
253
254 /* Only redirect non-get requests if we have no note to warn
255 * that this browser cannot handle redirs on non-GET requests
256 * (such as Microsoft's WebFolders).
257 */
258 if ((r->method_number != M_GET)
259 && apr_table_get(r->subprocess_env, "redirect-carefully")) {
260 return DECLINED;
261 }
262
263 if (r->args != NULL) {
265 "/?", r->args, NULL);
266 }
267 else {
269 "/", NULL);
270 }
271
272 apr_table_setn(r->headers_out, "Location",
275 }
276
277 /* we're running between mod_rewrites fixup and its internal redirect handler, step aside */
279 /* Prevent DIR_MAGIC_TYPE from leaking out when someone has taken over */
282 }
283 return DECLINED;
284 }
285
286 if (d->checkhandler == MODDIR_ON && strcmp(r->handler, DIR_MAGIC_TYPE)) {
287 /* Prevent DIR_MAGIC_TYPE from leaking out when someone has taken over */
290 }
291 return DECLINED;
292 }
293
294 if (d->index_names) {
295 names_ptr = (char **)d->index_names->elts;
296 num_names = d->index_names->nelts;
297 }
298 else {
301 num_names = 1;
302 }
303
304 for (; num_names; ++names_ptr, --num_names) {
305 /* XXX: Is this name_ptr considered escaped yet, or not??? */
306 char *name_ptr = *names_ptr;
308
309 /* Once upon a time args were handled _after_ the successful redirect.
310 * But that redirect might then _refuse_ the given r->args, creating
311 * a nasty tangle. It seems safer to consider the r->args while we
312 * determine if name_ptr is our viable index, and therefore set them
313 * up correctly on redirect.
314 */
315 if (r->args != NULL) {
317 }
318
320
321 /* The sub request lookup is very liberal, and the core map_to_storage
322 * handler will almost always result in HTTP_OK as /foo/index.html
323 * may be /foo with PATH_INFO="/index.html", or even / with
324 * PATH_INFO="/foo/index.html". To get around this we insist that the
325 * the index be a regular filetype.
326 *
327 * Another reason is that the core handler also makes the assumption
328 * that if r->finfo is still NULL by the time it gets called, the
329 * file does not exist.
330 */
331 if (rr->status == HTTP_OK
332 && ( (rr->handler && !strcmp(rr->handler, "proxy-server"))
333 || rr->finfo.filetype == APR_REG)) {
334
335 if (ap_is_HTTP_REDIRECT(d->redirect_index)) {
336 apr_table_setn(r->headers_out, "Location", ap_construct_url(r->pool, rr->uri, r));
337 return d->redirect_index;
338 }
339
341 return OK;
342 }
343
344 /* If the request returned a redirect, propagate it to the client */
345
346 if (ap_is_HTTP_REDIRECT(rr->status)
347 || (rr->status == HTTP_NOT_ACCEPTABLE && num_names == 1)
348 || (rr->status == HTTP_UNAUTHORIZED && num_names == 1)) {
349
350 apr_pool_join(r->pool, rr->pool);
351 error_notfound = rr->status;
352 r->notes = apr_table_overlay(r->pool, r->notes, rr->notes);
354 rr->headers_out);
356 rr->err_headers_out);
357 return error_notfound;
358 }
359
360 /* If the request returned something other than 404 (or 200),
361 * it means the module encountered some sort of problem. To be
362 * secure, we should return the error, rather than allow autoindex
363 * to create a (possibly unsafe) directory index.
364 *
365 * So we store the error, and if none of the listed files
366 * exist, we return the last error response we got, instead
367 * of a directory listing.
368 */
369 if (rr->status && rr->status != HTTP_NOT_FOUND
370 && rr->status != HTTP_OK) {
371 error_notfound = rr->status;
372 }
373
375 }
376
377 if (error_notfound) {
378 return error_notfound;
379 }
380
381 /* record what we tried, mostly for the benefit of mod_autoindex */
382 apr_table_setn(r->notes, "dir-index-names",
383 d->index_names ?
384 apr_array_pstrcat(r->pool, d->index_names, ',') :
386
387 /* nothing for us to do, pass on through */
388 return DECLINED;
389}
390
392{
393 if (r->finfo.filetype == APR_DIR) {
394 /* serve up a directory */
395 return fixup_dir(r);
396 }
397 else if ((r->finfo.filetype == APR_NOFILE) && (r->handler == NULL)) {
398 /* No handler and nothing in the filesystem - use fallback */
399 return fixup_dflt(r);
400 }
401 return DECLINED;
402}
403
408
411 create_dir_config, /* create per-directory config structure */
412 merge_dir_configs, /* merge per-directory config structures */
413 NULL, /* create per-server config structure */
414 NULL, /* merge per-server config structures */
415 dir_cmds, /* command apr_table_t */
416 register_hooks /* register hooks */
417};
Symbol export macros and hook functions.
APR general purpose library routines.
apr_size_t const unsigned char unsigned int unsigned int d
Definition apr_siphash.h:72
APR Strings library.
#define AP_INIT_TAKE1(directive, func, mconfig, where, help)
#define ap_get_module_config(v, m)
#define AP_DECLARE_MODULE(foo)
#define AP_INIT_FLAG(directive, func, mconfig, where, help)
ap_conf_vector_t * base
#define AP_INIT_RAW_ARGS(directive, func, mconfig, where, help)
const char * ap_set_string_slot(cmd_parms *cmd, void *struct_ptr, const char *arg)
Definition config.c:1469
request_rec * r
#define DECLINED
Definition httpd.h:457
#define AP_DEFAULT_INDEX
Definition httpd.h:223
#define OK
Definition httpd.h:456
char * ap_construct_url(apr_pool_t *p, const char *uri, request_rec *r)
Definition core.c:1246
void ap_internal_fast_redirect(request_rec *sub_req, request_rec *r)
request_rec * ap_sub_req_lookup_uri(const char *new_uri, const request_rec *r, ap_filter_t *next_filter)
Definition request.c:2297
void ap_hook_fixups(ap_HOOK_fixups_t *pf, const char *const *aszPre, const char *const *aszSucc, int nOrder)
Definition request.c:87
void ap_destroy_sub_req(request_rec *r)
Definition request.c:2547
void * dummy
Definition http_vhost.h:62
void const char * arg
Definition http_vhost.h:63
unsigned int count
Definition apr_md5.h:152
#define APR_HOOK_LAST
Definition apr_hooks.h:305
#define HTTP_OK
Definition httpd.h:490
#define HTTP_SEE_OTHER
Definition httpd.h:503
#define HTTP_MOVED_TEMPORARILY
Definition httpd.h:502
#define ap_is_HTTP_REDIRECT(x)
Definition httpd.h:552
#define HTTP_MOVED_PERMANENTLY
Definition httpd.h:501
#define HTTP_NOT_FOUND
Definition httpd.h:512
#define HTTP_UNAUTHORIZED
Definition httpd.h:509
#define HTTP_NOT_ACCEPTABLE
Definition httpd.h:514
#define REWRITE_REDIRECT_HANDLER_NAME
Definition mod_rewrite.h:32
#define M_GET
Definition httpd.h:592
#define STANDARD20_MODULE_STUFF
#define apr_pool_join(a, b)
Definition apr_pools.h:800
#define ap_escape_uri(ppool, path)
Definition httpd.h:1836
char * ap_getword_conf(apr_pool_t *p, const char **line)
Definition util.c:833
apr_size_t size
#define apr_isdigit(c)
Definition apr_lib.h:209
@ APR_REG
@ APR_DIR
@ APR_NOFILE
int strcasecmp(const char *a, const char *b)
apr_interval_time_t t
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
apr_dir_t * dir
apr_cmdtype_e cmd
int int status
#define DIR_MAGIC_TYPE
Definition httpd.h:711
Apache Configuration.
CORE HTTP Daemon.
Apache Logging library.
Command line options.
HTTP protocol handling.
Apache Request library.
HTTP Daemon routines.
apr_pool_t * p
Definition md_event.c:32
struct dir_config_struct dir_config_rec
static void * merge_dir_configs(apr_pool_t *p, void *basev, void *addv)
Definition mod_dir.c:154
static const char * add_index(cmd_parms *cmd, void *dummy, const char *arg)
Definition mod_dir.c:55
static const char * configure_checkhandler(cmd_parms *cmd, void *d_, int arg)
Definition mod_dir.c:91
static void * create_dir_config(apr_pool_t *p, char *dummy)
Definition mod_dir.c:143
static int fixup_dir(request_rec *r)
Definition mod_dir.c:224
static int fixup_dflt(request_rec *r)
Definition mod_dir.c:171
static const char * configure_slash(cmd_parms *cmd, void *d_, int arg)
Definition mod_dir.c:84
static void register_hooks(apr_pool_t *p)
Definition mod_dir.c:404
static const char * configure_redirect(cmd_parms *cmd, void *d_, const char *arg1)
Definition mod_dir.c:98
#define REDIRECT_OFF
Definition mod_dir.c:42
static const command_rec dir_cmds[]
Definition mod_dir.c:126
#define REDIRECT_UNSET
Definition mod_dir.c:43
static int dir_fixups(request_rec *r)
Definition mod_dir.c:391
moddir_cfg
Definition mod_dir.c:36
@ MODDIR_ON
Definition mod_dir.c:38
@ MODDIR_OFF
Definition mod_dir.c:37
@ MODDIR_UNSET
Definition mod_dir.c:39
#define DIR_CMD_PERMS
Definition mod_dir.c:53
Rewrite Extension module for Apache.
return NULL
Definition mod_so.c:359
apr_filetype_e filetype
moddir_cfg checkhandler
Definition mod_dir.c:48
apr_array_header_t * index_names
Definition mod_dir.c:46
const char * dflt
Definition mod_dir.c:50
moddir_cfg do_slash
Definition mod_dir.c:47
A structure that represents the current request.
Definition httpd.h:845
char * uri
Definition httpd.h:1016
const char * content_type
Definition httpd.h:992
struct ap_filter_t * output_filters
Definition httpd.h:1070
const char * handler
Definition httpd.h:994
apr_table_t * notes
Definition httpd.h:985
int method_number
Definition httpd.h:898
apr_pool_t * pool
Definition httpd.h:847
apr_table_t * err_headers_out
Definition httpd.h:981
apr_finfo_t finfo
Definition httpd.h:1094
apr_table_t * subprocess_env
Definition httpd.h:983
struct ap_conf_vector_t * per_dir_config
Definition httpd.h:1047
char * path_info
Definition httpd.h:1024
char * args
Definition httpd.h:1026
apr_table_t * headers_out
Definition httpd.h:978
Apache script tools.