mod_userdir.c revision 33510984c759eb3da154ceb0db9b75fa0031d3b4
0797faae937515a5225a36db4a1ec79480d2555cjorton/* Licensed to the Apache Software Foundation (ASF) under one or more
0797faae937515a5225a36db4a1ec79480d2555cjorton * contributor license agreements. See the NOTICE file distributed with
0797faae937515a5225a36db4a1ec79480d2555cjorton * this work for additional information regarding copyright ownership.
0797faae937515a5225a36db4a1ec79480d2555cjorton * The ASF licenses this file to You under the Apache License, Version 2.0
0797faae937515a5225a36db4a1ec79480d2555cjorton * (the "License"); you may not use this file except in compliance with
0797faae937515a5225a36db4a1ec79480d2555cjorton * the License. You may obtain a copy of the License at
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * http://www.apache.org/licenses/LICENSE-2.0
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * Unless required by applicable law or agreed to in writing, software
0797faae937515a5225a36db4a1ec79480d2555cjorton * distributed under the License is distributed on an "AS IS" BASIS,
0797faae937515a5225a36db4a1ec79480d2555cjorton * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0797faae937515a5225a36db4a1ec79480d2555cjorton * See the License for the specific language governing permissions and
0797faae937515a5225a36db4a1ec79480d2555cjorton * limitations under the License.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton/*
0797faae937515a5225a36db4a1ec79480d2555cjorton * mod_userdir... implement the UserDir command. Broken away from the
0797faae937515a5225a36db4a1ec79480d2555cjorton * Alias stuff for a couple of good and not-so-good reasons:
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * 1) It shows a real minimal working example of how to do something like
0797faae937515a5225a36db4a1ec79480d2555cjorton * this.
0797faae937515a5225a36db4a1ec79480d2555cjorton * 2) I know people who are actually interested in changing this *particular*
0797faae937515a5225a36db4a1ec79480d2555cjorton * aspect of server functionality without changing the rest of it. That's
0797faae937515a5225a36db4a1ec79480d2555cjorton * what this whole modular arrangement is supposed to be good at...
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * Modified by Alexei Kosut to support the following constructs
0797faae937515a5225a36db4a1ec79480d2555cjorton * (server running at www.foo.com, request for /~bar/one/two.html)
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir public_html -> ~bar/public_html/one/two.html
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir /usr/web -> /usr/web/bar/one/two.html
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir /home/ * /www -> /home/bar/www/one/two.html
0797faae937515a5225a36db4a1ec79480d2555cjorton * NOTE: theses ^ ^ space only added allow it to work in a comment, ignore
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir http://x/users -> (302) http://x/users/bar/one/two.html
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir http://x/ * /y -> (302) http://x/bar/y/one/two.html
0797faae937515a5225a36db4a1ec79480d2555cjorton * NOTE: here also ^ ^
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * In addition, you can use multiple entries, to specify alternate
2685f3814b77577ef7b2523442dab1ca88df1e41jorton * user directories (a la Directory Index). For example:
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir public_html /usr/web http://www.xyz.com/users
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * Modified by Ken Coar to provide for the following:
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir disable[d] username ...
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir enable[d] username ...
0797faae937515a5225a36db4a1ec79480d2555cjorton *
0797faae937515a5225a36db4a1ec79480d2555cjorton * If "disabled" has no other arguments, *all* ~<username> references are
0797faae937515a5225a36db4a1ec79480d2555cjorton * disabled, except those explicitly turned on with the "enabled" keyword.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr_strings.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr_user.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#define APR_WANT_STRFUNC
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "apr_want.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#if APR_HAVE_UNISTD_H
0797faae937515a5225a36db4a1ec79480d2555cjorton#include <unistd.h>
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "ap_config.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "httpd.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "http_config.h"
0797faae937515a5225a36db4a1ec79480d2555cjorton#include "http_request.h"
2685f3814b77577ef7b2523442dab1ca88df1e41jorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#if !defined(WIN32) && !defined(OS2) && !defined(NETWARE)
2685f3814b77577ef7b2523442dab1ca88df1e41jorton#define HAVE_UNIX_SUEXEC
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#ifdef HAVE_UNIX_SUEXEC
2685f3814b77577ef7b2523442dab1ca88df1e41jorton#include "unixd.h" /* Contains the suexec_identity hook used on Unix */
2685f3814b77577ef7b2523442dab1ca88df1e41jorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton
2685f3814b77577ef7b2523442dab1ca88df1e41jorton/*
0797faae937515a5225a36db4a1ec79480d2555cjorton * The default directory in user's home dir
0797faae937515a5225a36db4a1ec79480d2555cjorton * In the default install, the module is disabled
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton#ifndef DEFAULT_USER_DIR
0797faae937515a5225a36db4a1ec79480d2555cjorton#define DEFAULT_USER_DIR NULL
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton#define O_DEFAULT 0
0797faae937515a5225a36db4a1ec79480d2555cjorton#define O_ENABLE 1
0797faae937515a5225a36db4a1ec79480d2555cjorton#define O_DISABLE 2
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonmodule AP_MODULE_DECLARE_DATA userdir_module;
0797faae937515a5225a36db4a1ec79480d2555cjorton
2685f3814b77577ef7b2523442dab1ca88df1e41jortontypedef struct {
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton int globally_disabled;
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton char *userdir;
2685f3814b77577ef7b2523442dab1ca88df1e41jorton apr_table_t *enabled_users;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_table_t *disabled_users;
0797faae937515a5225a36db4a1ec79480d2555cjorton} userdir_config;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton/*
0797faae937515a5225a36db4a1ec79480d2555cjorton * Server config for this module: global disablement flag, a list of usernames
0797faae937515a5225a36db4a1ec79480d2555cjorton * ineligible for UserDir access, a list of those immune to global (but not
0797faae937515a5225a36db4a1ec79480d2555cjorton * explicit) disablement, and the replacement string for all others.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void *create_userdir_config(apr_pool_t *p, server_rec *s)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton userdir_config *newcfg = apr_pcalloc(p, sizeof(*newcfg));
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton newcfg->globally_disabled = O_DEFAULT;
0797faae937515a5225a36db4a1ec79480d2555cjorton newcfg->userdir = DEFAULT_USER_DIR;
0797faae937515a5225a36db4a1ec79480d2555cjorton newcfg->enabled_users = apr_table_make(p, 4);
0797faae937515a5225a36db4a1ec79480d2555cjorton newcfg->disabled_users = apr_table_make(p, 4);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return newcfg;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void *merge_userdir_config(apr_pool_t *p, void *basev, void *overridesv)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton userdir_config *cfg = apr_pcalloc(p, sizeof(userdir_config));
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton userdir_config *base = basev, *overrides = overridesv;
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton cfg->globally_disabled = (overrides->globally_disabled != O_DEFAULT) ?
0797faae937515a5225a36db4a1ec79480d2555cjorton overrides->globally_disabled :
0797faae937515a5225a36db4a1ec79480d2555cjorton base->globally_disabled;
0797faae937515a5225a36db4a1ec79480d2555cjorton cfg->userdir = (overrides->userdir != DEFAULT_USER_DIR) ?
0797faae937515a5225a36db4a1ec79480d2555cjorton overrides->userdir : base->userdir;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* not merged */
0797faae937515a5225a36db4a1ec79480d2555cjorton cfg->enabled_users = overrides->enabled_users;
0797faae937515a5225a36db4a1ec79480d2555cjorton cfg->disabled_users = overrides->disabled_users;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return cfg;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic const char *set_user_dir(cmd_parms *cmd, void *dummy, const char *arg)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton userdir_config *s_cfg = ap_get_module_config(cmd->server->module_config,
0797faae937515a5225a36db4a1ec79480d2555cjorton &userdir_module);
0797faae937515a5225a36db4a1ec79480d2555cjorton char *username;
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *usernames = arg;
0797faae937515a5225a36db4a1ec79480d2555cjorton char *kw = ap_getword_conf(cmd->pool, &usernames);
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_table_t *usertable;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* Since we are a raw argument, it is possible for us to be called with
0797faae937515a5225a36db4a1ec79480d2555cjorton * zero arguments. So that we aren't ambiguous, flat out reject this.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (*kw == '\0') {
0797faae937515a5225a36db4a1ec79480d2555cjorton return "UserDir requires an argument.";
0797faae937515a5225a36db4a1ec79480d2555cjorton }
2685f3814b77577ef7b2523442dab1ca88df1e41jorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * Let's do the comparisons once.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((!strcasecmp(kw, "disable")) || (!strcasecmp(kw, "disabled"))) {
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * If there are no usernames specified, this is a global disable - we
0797faae937515a5225a36db4a1ec79480d2555cjorton * need do no more at this point than record the fact.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (!*usernames) {
0797faae937515a5225a36db4a1ec79480d2555cjorton s_cfg->globally_disabled = O_DISABLE;
0797faae937515a5225a36db4a1ec79480d2555cjorton return NULL;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton usertable = s_cfg->disabled_users;
072f7e449a76d28b580de6e89a1723713ab9adb1jorton }
072f7e449a76d28b580de6e89a1723713ab9adb1jorton else if ((!strcasecmp(kw, "enable")) || (!strcasecmp(kw, "enabled"))) {
072f7e449a76d28b580de6e89a1723713ab9adb1jorton if (!*usernames) {
072f7e449a76d28b580de6e89a1723713ab9adb1jorton s_cfg->globally_disabled = O_ENABLE;
0797faae937515a5225a36db4a1ec79480d2555cjorton return NULL;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton usertable = s_cfg->enabled_users;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton else {
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * If the first (only?) value isn't one of our keywords, just copy
0797faae937515a5225a36db4a1ec79480d2555cjorton * the string to the userdir string.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton s_cfg->userdir = apr_pstrdup(cmd->pool, arg);
0797faae937515a5225a36db4a1ec79480d2555cjorton return NULL;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * Now we just take each word in turn from the command line and add it to
0797faae937515a5225a36db4a1ec79480d2555cjorton * the appropriate table.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton while (*usernames) {
0797faae937515a5225a36db4a1ec79480d2555cjorton username = ap_getword_conf(cmd->pool, &usernames);
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_table_setn(usertable, username, kw);
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton return NULL;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic const command_rec userdir_cmds[] = {
0797faae937515a5225a36db4a1ec79480d2555cjorton AP_INIT_RAW_ARGS("UserDir", set_user_dir, NULL, RSRC_CONF,
0797faae937515a5225a36db4a1ec79480d2555cjorton "the public subdirectory in users' home directories, or "
0797faae937515a5225a36db4a1ec79480d2555cjorton "'disabled', or 'disabled username username...', or "
0797faae937515a5225a36db4a1ec79480d2555cjorton "'enabled username username...'"),
0797faae937515a5225a36db4a1ec79480d2555cjorton {NULL}
0797faae937515a5225a36db4a1ec79480d2555cjorton};
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic int translate_userdir(request_rec *r)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_conf_vector_t *server_conf;
0797faae937515a5225a36db4a1ec79480d2555cjorton const userdir_config *s_cfg;
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *userdirs;
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *user, *dname;
0797faae937515a5225a36db4a1ec79480d2555cjorton char *redirect;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_finfo_t statbuf;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * If the URI doesn't match our basic pattern, we've nothing to do with
0797faae937515a5225a36db4a1ec79480d2555cjorton * it.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (r->uri[0] != '/' || r->uri[1] != '~') {
0797faae937515a5225a36db4a1ec79480d2555cjorton return DECLINED;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton server_conf = r->server->module_config;
0797faae937515a5225a36db4a1ec79480d2555cjorton s_cfg = ap_get_module_config(server_conf, &userdir_module);
0797faae937515a5225a36db4a1ec79480d2555cjorton userdirs = s_cfg->userdir;
0797faae937515a5225a36db4a1ec79480d2555cjorton if (userdirs == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton return DECLINED;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton dname = r->uri + 2;
0797faae937515a5225a36db4a1ec79480d2555cjorton user = ap_getword(r->pool, &dname, '/');
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * The 'dname' funny business involves backing it up to capture the '/'
0797faae937515a5225a36db4a1ec79480d2555cjorton * delimiting the "/~user" part from the rest of the URL, in case there
0797faae937515a5225a36db4a1ec79480d2555cjorton * was one (the case where there wasn't being just "GET /~user HTTP/1.0",
0797faae937515a5225a36db4a1ec79480d2555cjorton * for which we don't want to tack on a '/' onto the filename).
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if (dname[-1] == '/') {
0797faae937515a5225a36db4a1ec79480d2555cjorton --dname;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
072f7e449a76d28b580de6e89a1723713ab9adb1jorton * If there's no username, it's not for us. Ignore . and .. as well.
072f7e449a76d28b580de6e89a1723713ab9adb1jorton */
072f7e449a76d28b580de6e89a1723713ab9adb1jorton if (user[0] == '\0' ||
072f7e449a76d28b580de6e89a1723713ab9adb1jorton (user[1] == '.' && (user[2] == '\0' ||
0797faae937515a5225a36db4a1ec79480d2555cjorton (user[2] == '.' && user[3] == '\0')))) {
0797faae937515a5225a36db4a1ec79480d2555cjorton return DECLINED;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * Nor if there's an username but it's in the disabled list.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (apr_table_get(s_cfg->disabled_users, user) != NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton return DECLINED;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * If there's a global interdiction on UserDirs, check to see if this
0797faae937515a5225a36db4a1ec79480d2555cjorton * name is one of the Blessed.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (s_cfg->globally_disabled == O_DISABLE
0797faae937515a5225a36db4a1ec79480d2555cjorton && apr_table_get(s_cfg->enabled_users, user) == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton return DECLINED;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * Special cases all checked, onward to normal substitution processing.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton while (*userdirs) {
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *userdir = ap_getword_conf(r->pool, &userdirs);
0797faae937515a5225a36db4a1ec79480d2555cjorton char *filename = NULL, *prefix = NULL;
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_status_t rv;
0797faae937515a5225a36db4a1ec79480d2555cjorton int is_absolute = ap_os_is_path_absolute(r->pool, userdir);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if (ap_strchr_c(userdir, '*'))
0797faae937515a5225a36db4a1ec79480d2555cjorton prefix = ap_getword(r->pool, &userdir, '*');
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if (userdir[0] == '\0' || is_absolute) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if (prefix) {
0797faae937515a5225a36db4a1ec79480d2555cjorton#ifdef HAVE_DRIVE_LETTERS
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * Crummy hack. Need to figure out whether we have been
0797faae937515a5225a36db4a1ec79480d2555cjorton * redirected to a URL or to a file on some drive. Since I
0797faae937515a5225a36db4a1ec79480d2555cjorton * know of no protocols that are a single letter, ignore
0797faae937515a5225a36db4a1ec79480d2555cjorton * a : as the first or second character, and assume a file
0797faae937515a5225a36db4a1ec79480d2555cjorton * was specified
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (strchr(prefix + 2, ':'))
0797faae937515a5225a36db4a1ec79480d2555cjorton#else
0797faae937515a5225a36db4a1ec79480d2555cjorton if (strchr(prefix, ':') && !is_absolute)
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif /* HAVE_DRIVE_LETTERS */
0797faae937515a5225a36db4a1ec79480d2555cjorton {
0797faae937515a5225a36db4a1ec79480d2555cjorton redirect = apr_pstrcat(r->pool, prefix, user, userdir,
0797faae937515a5225a36db4a1ec79480d2555cjorton dname, NULL);
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_table_setn(r->headers_out, "Location", redirect);
0797faae937515a5225a36db4a1ec79480d2555cjorton return HTTP_MOVED_TEMPORARILY;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton else
0797faae937515a5225a36db4a1ec79480d2555cjorton filename = apr_pstrcat(r->pool, prefix, user, userdir,
0797faae937515a5225a36db4a1ec79480d2555cjorton NULL);
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton else
0797faae937515a5225a36db4a1ec79480d2555cjorton filename = apr_pstrcat(r->pool, userdir, "/", user, NULL);
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton else if (prefix && ap_strchr_c(prefix, ':')) {
0797faae937515a5225a36db4a1ec79480d2555cjorton redirect = apr_pstrcat(r->pool, prefix, user, dname, NULL);
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_table_setn(r->headers_out, "Location", redirect);
0797faae937515a5225a36db4a1ec79480d2555cjorton return HTTP_MOVED_TEMPORARILY;
2685f3814b77577ef7b2523442dab1ca88df1e41jorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton else {
0797faae937515a5225a36db4a1ec79480d2555cjorton#if APR_HAS_USER
0797faae937515a5225a36db4a1ec79480d2555cjorton char *homedir;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if (apr_uid_homepath_get(&homedir, user, r->pool) == APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton filename = apr_pstrcat(r->pool, homedir, "/", userdir, NULL);
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton#else
0797faae937515a5225a36db4a1ec79480d2555cjorton return DECLINED;
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /*
0797faae937515a5225a36db4a1ec79480d2555cjorton * Now see if it exists, or we're at the last entry. If we are at the
0797faae937515a5225a36db4a1ec79480d2555cjorton * last entry, then use the filename generated (if there is one)
0797faae937515a5225a36db4a1ec79480d2555cjorton * anyway, in the hope that some handler might handle it. This can be
0797faae937515a5225a36db4a1ec79480d2555cjorton * used, for example, to run a CGI script for the user.
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (filename && (!*userdirs
0797faae937515a5225a36db4a1ec79480d2555cjorton || ((rv = apr_stat(&statbuf, filename, APR_FINFO_MIN,
0797faae937515a5225a36db4a1ec79480d2555cjorton r->pool)) == APR_SUCCESS
0797faae937515a5225a36db4a1ec79480d2555cjorton || rv == APR_INCOMPLETE))) {
0797faae937515a5225a36db4a1ec79480d2555cjorton r->filename = apr_pstrcat(r->pool, filename, dname, NULL);
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_set_context_info(r, apr_pstrmemdup(r->pool, r->uri,
0797faae937515a5225a36db4a1ec79480d2555cjorton dname - r->uri),
0797faae937515a5225a36db4a1ec79480d2555cjorton filename);
0797faae937515a5225a36db4a1ec79480d2555cjorton /* XXX: Does this walk us around FollowSymLink rules?
0797faae937515a5225a36db4a1ec79480d2555cjorton * When statbuf contains info on r->filename we can save a syscall
2685f3814b77577ef7b2523442dab1ca88df1e41jorton * by copying it to r->finfo
0797faae937515a5225a36db4a1ec79480d2555cjorton */
0797faae937515a5225a36db4a1ec79480d2555cjorton if (*userdirs && dname[0] == 0)
0797faae937515a5225a36db4a1ec79480d2555cjorton r->finfo = statbuf;
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton /* For use in the get_suexec_identity phase */
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_table_setn(r->notes, "mod_userdir_user", user);
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return OK;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton return DECLINED;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton#ifdef HAVE_UNIX_SUEXEC
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic ap_unix_identity_t *get_suexec_id_doer(const request_rec *r)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_unix_identity_t *ugid = NULL;
0797faae937515a5225a36db4a1ec79480d2555cjorton#if APR_HAS_USER
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *username = apr_table_get(r->notes, "mod_userdir_user");
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if (username == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton return NULL;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((ugid = apr_palloc(r->pool, sizeof(*ugid))) == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton return NULL;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton if (apr_uid_get(&ugid->uid, &ugid->gid, username, r->pool) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton return NULL;
0797faae937515a5225a36db4a1ec79480d2555cjorton }
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton ugid->userdir = 1;
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton return ugid;
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif /* HAVE_UNIX_SUEXEC */
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void register_hooks(apr_pool_t *p)
0797faae937515a5225a36db4a1ec79480d2555cjorton{
0797faae937515a5225a36db4a1ec79480d2555cjorton static const char * const aszPre[]={ "mod_alias.c",NULL };
0797faae937515a5225a36db4a1ec79480d2555cjorton static const char * const aszSucc[]={ "mod_vhost_alias.c",NULL };
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_hook_translate_name(translate_userdir,aszPre,aszSucc,APR_HOOK_MIDDLE);
0797faae937515a5225a36db4a1ec79480d2555cjorton#ifdef HAVE_UNIX_SUEXEC
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_hook_get_suexec_identity(get_suexec_id_doer,NULL,NULL,APR_HOOK_FIRST);
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif
0797faae937515a5225a36db4a1ec79480d2555cjorton}
0797faae937515a5225a36db4a1ec79480d2555cjorton
0797faae937515a5225a36db4a1ec79480d2555cjortonAP_DECLARE_MODULE(userdir) = {
0797faae937515a5225a36db4a1ec79480d2555cjorton STANDARD20_MODULE_STUFF,
0797faae937515a5225a36db4a1ec79480d2555cjorton NULL, /* dir config creater */
0797faae937515a5225a36db4a1ec79480d2555cjorton NULL, /* dir merger --- default is to override */
0797faae937515a5225a36db4a1ec79480d2555cjorton create_userdir_config, /* server config */
0797faae937515a5225a36db4a1ec79480d2555cjorton merge_userdir_config, /* merge server config */
0797faae937515a5225a36db4a1ec79480d2555cjorton userdir_cmds, /* command apr_table_t */
0797faae937515a5225a36db4a1ec79480d2555cjorton register_hooks /* register hooks */
0797faae937515a5225a36db4a1ec79480d2555cjorton};
0797faae937515a5225a36db4a1ec79480d2555cjorton