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 * 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 * mod_userdir... implement the UserDir command. Broken away from the
0797faae937515a5225a36db4a1ec79480d2555cjorton * Alias stuff for a couple of good and not-so-good reasons:
0797faae937515a5225a36db4a1ec79480d2555cjorton * 1) It shows a real minimal working example of how to do something like
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 * Modified by Alexei Kosut to support the following constructs
0797faae937515a5225a36db4a1ec79480d2555cjorton * (server running at www.foo.com, request for /~bar/one/two.html)
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir public_html -> ~bar/public_html/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 * In addition, you can use multiple entries, to specify alternate
2685f3814b77577ef7b2523442dab1ca88df1e41jorton * user directories (a la Directory Index). For example:
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir public_html /usr/web http://www.xyz.com/users
0797faae937515a5225a36db4a1ec79480d2555cjorton * Modified by Ken Coar to provide for the following:
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir disable[d] username ...
0797faae937515a5225a36db4a1ec79480d2555cjorton * UserDir enable[d] username ...
0797faae937515a5225a36db4a1ec79480d2555cjorton * If "disabled" has no other arguments, *all* ~<username> references are
0797faae937515a5225a36db4a1ec79480d2555cjorton * disabled, except those explicitly turned on with the "enabled" keyword.
0797faae937515a5225a36db4a1ec79480d2555cjorton#if !defined(WIN32) && !defined(OS2) && !defined(NETWARE)
2685f3814b77577ef7b2523442dab1ca88df1e41jorton#include "unixd.h" /* Contains the suexec_identity hook used on Unix */
0797faae937515a5225a36db4a1ec79480d2555cjorton * The default directory in user's home dir
0797faae937515a5225a36db4a1ec79480d2555cjorton * In the default install, the module is disabled
2685f3814b77577ef7b2523442dab1ca88df1e41jortontypedef struct {
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.
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void *create_userdir_config(apr_pool_t *p, server_rec *s)
0797faae937515a5225a36db4a1ec79480d2555cjorton userdir_config *newcfg = apr_pcalloc(p, sizeof(*newcfg));
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic void *merge_userdir_config(apr_pool_t *p, void *basev, void *overridesv)
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton userdir_config *cfg = apr_pcalloc(p, sizeof(userdir_config));
fca945cb6bed035dcc6bbced5e327bbd4d8420abjorton userdir_config *base = basev, *overrides = overridesv;
0797faae937515a5225a36db4a1ec79480d2555cjorton cfg->globally_disabled = (overrides->globally_disabled != O_DEFAULT) ?
0797faae937515a5225a36db4a1ec79480d2555cjorton cfg->userdir = (overrides->userdir != DEFAULT_USER_DIR) ?
0797faae937515a5225a36db4a1ec79480d2555cjorton /* not merged */
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic const char *set_user_dir(cmd_parms *cmd, void *dummy, const char *arg)
0797faae937515a5225a36db4a1ec79480d2555cjorton userdir_config *s_cfg = ap_get_module_config(cmd->server->module_config,
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 return "UserDir requires an argument.";
0797faae937515a5225a36db4a1ec79480d2555cjorton * Let's do the comparisons once.
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((!strcasecmp(kw, "disable")) || (!strcasecmp(kw, "disabled"))) {
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.
072f7e449a76d28b580de6e89a1723713ab9adb1jorton else if ((!strcasecmp(kw, "enable")) || (!strcasecmp(kw, "enabled"))) {
0797faae937515a5225a36db4a1ec79480d2555cjorton * If the first (only?) value isn't one of our keywords, just copy
0797faae937515a5225a36db4a1ec79480d2555cjorton * the string to the userdir string.
0797faae937515a5225a36db4a1ec79480d2555cjorton * Now we just take each word in turn from the command line and add it to
0797faae937515a5225a36db4a1ec79480d2555cjorton * the appropriate table.
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 const char *userdirs;
0797faae937515a5225a36db4a1ec79480d2555cjorton * If the URI doesn't match our basic pattern, we've nothing to do with
0797faae937515a5225a36db4a1ec79480d2555cjorton s_cfg = ap_get_module_config(server_conf, &userdir_module);
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).
072f7e449a76d28b580de6e89a1723713ab9adb1jorton * If there's no username, it's not for us. Ignore . and .. as well.
0797faae937515a5225a36db4a1ec79480d2555cjorton * Nor if there's an username but it's in the disabled list.
0797faae937515a5225a36db4a1ec79480d2555cjorton if (apr_table_get(s_cfg->disabled_users, user) != NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton * If there's a global interdiction on UserDirs, check to see if this
0797faae937515a5225a36db4a1ec79480d2555cjorton * name is one of the Blessed.
0797faae937515a5225a36db4a1ec79480d2555cjorton && apr_table_get(s_cfg->enabled_users, user) == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton * Special cases all checked, onward to normal substitution processing.
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *userdir = ap_getword_conf(r->pool, &userdirs);
0797faae937515a5225a36db4a1ec79480d2555cjorton int is_absolute = ap_os_is_path_absolute(r->pool, userdir);
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#endif /* HAVE_DRIVE_LETTERS */
0797faae937515a5225a36db4a1ec79480d2555cjorton redirect = apr_pstrcat(r->pool, prefix, user, userdir,
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_table_setn(r->headers_out, "Location", redirect);
0797faae937515a5225a36db4a1ec79480d2555cjorton filename = apr_pstrcat(r->pool, prefix, user, userdir,
0797faae937515a5225a36db4a1ec79480d2555cjorton filename = apr_pstrcat(r->pool, userdir, "/", user, NULL);
0797faae937515a5225a36db4a1ec79480d2555cjorton redirect = apr_pstrcat(r->pool, prefix, user, dname, NULL);
0797faae937515a5225a36db4a1ec79480d2555cjorton apr_table_setn(r->headers_out, "Location", redirect);
0797faae937515a5225a36db4a1ec79480d2555cjorton if (apr_uid_homepath_get(&homedir, user, r->pool) == APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton filename = apr_pstrcat(r->pool, homedir, "/", userdir, NULL);
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 || ((rv = apr_stat(&statbuf, filename, APR_FINFO_MIN,
0797faae937515a5225a36db4a1ec79480d2555cjorton r->filename = apr_pstrcat(r->pool, filename, dname, NULL);
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_set_context_info(r, apr_pstrmemdup(r->pool, r->uri,
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 /* For use in the get_suexec_identity phase */
0797faae937515a5225a36db4a1ec79480d2555cjortonstatic ap_unix_identity_t *get_suexec_id_doer(const request_rec *r)
0797faae937515a5225a36db4a1ec79480d2555cjorton const char *username = apr_table_get(r->notes, "mod_userdir_user");
0797faae937515a5225a36db4a1ec79480d2555cjorton if ((ugid = apr_palloc(r->pool, sizeof(*ugid))) == NULL) {
0797faae937515a5225a36db4a1ec79480d2555cjorton if (apr_uid_get(&ugid->uid, &ugid->gid, username, r->pool) != APR_SUCCESS) {
0797faae937515a5225a36db4a1ec79480d2555cjorton#endif /* HAVE_UNIX_SUEXEC */
0797faae937515a5225a36db4a1ec79480d2555cjorton static const char * const aszPre[]={ "mod_alias.c",NULL };
0797faae937515a5225a36db4a1ec79480d2555cjorton static const char * const aszSucc[]={ "mod_vhost_alias.c",NULL };
0797faae937515a5225a36db4a1ec79480d2555cjorton ap_hook_translate_name(translate_userdir,aszPre,aszSucc,APR_HOOK_MIDDLE);