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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * mod_userdir... implement the UserDir command. Broken away from the
19 * Alias stuff for a couple of good and not-so-good reasons:
21 * 1) It shows a real minimal working example of how to do something like
23 * 2) I know people who are actually interested in changing this *particular*
24 * aspect of server functionality without changing the rest of it. That's
25 * what this whole modular arrangement is supposed to be good at...
27 * Modified by Alexei Kosut to support the following constructs
28 * (server running at www.foo.com, request for /~bar/one/two.html)
30 * UserDir public_html -> ~bar/public_html/one/two.html
31 * UserDir /usr/web -> /usr/web/bar/one/two.html
32 * UserDir /home/ * /www -> /home/bar/www/one/two.html
33 * NOTE: theses ^ ^ space only added allow it to work in a comment, ignore
34 * UserDir http://x/users -> (302) http://x/users/bar/one/two.html
35 * UserDir http://x/ * /y -> (302) http://x/bar/y/one/two.html
38 * In addition, you can use multiple entries, to specify alternate
39 * user directories (a la Directory Index). For example:
41 * UserDir public_html /usr/web http://www.xyz.com/users
43 * Modified by Ken Coar to provide for the following:
45 * UserDir disable[d] username ...
46 * UserDir enable[d] username ...
48 * If "disabled" has no other arguments, *all* ~<username> references are
49 * disabled, except those explicitly turned on with the "enabled" keyword.
52 #include "apr_strings.h"
55 #define APR_WANT_STRFUNC
62 #include "ap_config.h"
64 #include "http_config.h"
65 #include "http_request.h"
67 #if !defined(WIN32) && !defined(OS2) && !defined(BEOS) && !defined(NETWARE)
68 #define HAVE_UNIX_SUEXEC
71 #ifdef HAVE_UNIX_SUEXEC
72 #include "unixd.h" /* Contains the suexec_identity hook used on Unix */
76 /* The default directory in user's home dir */
77 #ifndef DEFAULT_USER_DIR
78 #define DEFAULT_USER_DIR "public_html"
81 module AP_MODULE_DECLARE_DATA userdir_module;
84 int globally_disabled;
86 apr_table_t *enabled_users;
87 apr_table_t *disabled_users;
91 * Server config for this module: global disablement flag, a list of usernames
92 * ineligible for UserDir access, a list of those immune to global (but not
93 * explicit) disablement, and the replacement string for all others.
96 static void *create_userdir_config(apr_pool_t *p, server_rec *s)
98 userdir_config *newcfg = apr_pcalloc(p, sizeof(*newcfg));
100 newcfg->globally_disabled = 0;
101 newcfg->userdir = DEFAULT_USER_DIR;
102 newcfg->enabled_users = apr_table_make(p, 4);
103 newcfg->disabled_users = apr_table_make(p, 4);
112 static const char *set_user_dir(cmd_parms *cmd, void *dummy, const char *arg)
114 userdir_config *s_cfg = ap_get_module_config(cmd->server->module_config,
117 const char *usernames = arg;
118 char *kw = ap_getword_conf(cmd->pool, &usernames);
119 apr_table_t *usertable;
121 /* Since we are a raw argument, it is possible for us to be called with
122 * zero arguments. So that we aren't ambiguous, flat out reject this.
125 return "UserDir requires an argument.";
129 * Let's do the comparisons once.
131 if ((!strcasecmp(kw, "disable")) || (!strcasecmp(kw, "disabled"))) {
133 * If there are no usernames specified, this is a global disable - we
134 * need do no more at this point than record the fact.
136 if (strlen(usernames) == 0) {
137 s_cfg->globally_disabled = 1;
140 usertable = s_cfg->disabled_users;
142 else if ((!strcasecmp(kw, "enable")) || (!strcasecmp(kw, "enabled"))) {
144 * The "disable" keyword can stand alone or take a list of names, but
145 * the "enable" keyword requires the list. Whinge if it doesn't have
148 if (strlen(usernames) == 0) {
149 return "UserDir \"enable\" keyword requires a list of usernames";
151 usertable = s_cfg->enabled_users;
155 * If the first (only?) value isn't one of our keywords, just copy
156 * the string to the userdir string.
158 s_cfg->userdir = apr_pstrdup(cmd->pool, arg);
162 * Now we just take each word in turn from the command line and add it to
163 * the appropriate table.
166 username = ap_getword_conf(cmd->pool, &usernames);
167 apr_table_setn(usertable, username, kw);
172 static const command_rec userdir_cmds[] = {
173 AP_INIT_RAW_ARGS("UserDir", set_user_dir, NULL, RSRC_CONF,
174 "the public subdirectory in users' home directories, or "
175 "'disabled', or 'disabled username username...', or "
176 "'enabled username username...'"),
180 static int translate_userdir(request_rec *r)
182 ap_conf_vector_t *server_conf;
183 const userdir_config *s_cfg;
185 const char *userdirs;
186 const char *w, *dname;
191 * If the URI doesn't match our basic pattern, we've nothing to do with
194 if (name[0] != '/' || name[1] != '~') {
197 server_conf = r->server->module_config;
198 s_cfg = ap_get_module_config(server_conf, &userdir_module);
199 userdirs = s_cfg->userdir;
200 if (userdirs == NULL) {
205 w = ap_getword(r->pool, &dname, '/');
208 * The 'dname' funny business involves backing it up to capture the '/'
209 * delimiting the "/~user" part from the rest of the URL, in case there
210 * was one (the case where there wasn't being just "GET /~user HTTP/1.0",
211 * for which we don't want to tack on a '/' onto the filename).
214 if (dname[-1] == '/') {
219 * If there's no username, it's not for us. Ignore . and .. as well.
221 if (w[0] == '\0' || (w[1] == '.' && (w[2] == '\0' || (w[2] == '.' && w[3] == '\0')))) {
225 * Nor if there's an username but it's in the disabled list.
227 if (apr_table_get(s_cfg->disabled_users, w) != NULL) {
231 * If there's a global interdiction on UserDirs, check to see if this
232 * name is one of the Blessed.
234 if (s_cfg->globally_disabled
235 && apr_table_get(s_cfg->enabled_users, w) == NULL) {
240 * Special cases all checked, onward to normal substitution processing.
244 const char *userdir = ap_getword_conf(r->pool, &userdirs);
245 char *filename = NULL, *x = NULL;
247 int is_absolute = ap_os_is_path_absolute(r->pool, userdir);
249 if (ap_strchr_c(userdir, '*'))
250 x = ap_getword(r->pool, &userdir, '*');
252 if (userdir[0] == '\0' || is_absolute) {
254 #ifdef HAVE_DRIVE_LETTERS
256 * Crummy hack. Need to figure out whether we have been
257 * redirected to a URL or to a file on some drive. Since I
258 * know of no protocols that are a single letter, ignore
259 * a : as the first or second character, and assume a file
262 if (strchr(x + 2, ':'))
264 if (strchr(x, ':') && !is_absolute)
265 #endif /* HAVE_DRIVE_LETTERS */
267 redirect = apr_pstrcat(r->pool, x, w, userdir, dname, NULL);
268 apr_table_setn(r->headers_out, "Location", redirect);
269 return HTTP_MOVED_TEMPORARILY;
272 filename = apr_pstrcat(r->pool, x, w, userdir, NULL);
275 filename = apr_pstrcat(r->pool, userdir, "/", w, NULL);
277 else if (x && ap_strchr_c(x, ':')) {
278 redirect = apr_pstrcat(r->pool, x, w, dname, NULL);
279 apr_table_setn(r->headers_out, "Location", redirect);
280 return HTTP_MOVED_TEMPORARILY;
286 if (apr_get_home_directory(&homedir, w, r->pool) == APR_SUCCESS) {
287 filename = apr_pstrcat(r->pool, homedir, "/", userdir, NULL);
295 * Now see if it exists, or we're at the last entry. If we are at the
296 * last entry, then use the filename generated (if there is one)
297 * anyway, in the hope that some handler might handle it. This can be
298 * used, for example, to run a CGI script for the user.
300 if (filename && (!*userdirs
301 || ((rv = apr_stat(&statbuf, filename, APR_FINFO_MIN,
302 r->pool)) == APR_SUCCESS
303 || rv == APR_INCOMPLETE))) {
304 r->filename = apr_pstrcat(r->pool, filename, dname, NULL);
305 /* XXX: Does this walk us around FollowSymLink rules?
306 * When statbuf contains info on r->filename we can save a syscall
307 * by copying it to r->finfo
309 if (*userdirs && dname[0] == 0)
312 /* For use in the get_suexec_identity phase */
313 apr_table_setn(r->notes, "mod_userdir_user", w);
322 #ifdef HAVE_UNIX_SUEXEC
323 static ap_unix_identity_t *get_suexec_id_doer(const request_rec *r)
325 ap_unix_identity_t *ugid = NULL;
327 const char *username = apr_table_get(r->notes, "mod_userdir_user");
329 if (username == NULL) {
333 if ((ugid = apr_palloc(r->pool, sizeof(*ugid))) == NULL) {
337 if (apr_get_userid(&ugid->uid, &ugid->gid, username, r->pool) != APR_SUCCESS) {
345 #endif /* HAVE_UNIX_SUEXEC */
347 static void register_hooks(apr_pool_t *p)
349 static const char * const aszPre[]={ "mod_alias.c",NULL };
350 static const char * const aszSucc[]={ "mod_vhost_alias.c",NULL };
352 ap_hook_translate_name(translate_userdir,aszPre,aszSucc,APR_HOOK_MIDDLE);
353 #ifdef HAVE_UNIX_SUEXEC
354 ap_hook_get_suexec_identity(get_suexec_id_doer,NULL,NULL,APR_HOOK_FIRST);
358 module AP_MODULE_DECLARE_DATA userdir_module = {
359 STANDARD20_MODULE_STUFF,
360 NULL, /* dir config creater */
361 NULL, /* dir merger --- default is to override */
362 create_userdir_config, /* server config */
363 NULL, /* merge server config */
364 userdir_cmds, /* command apr_table_t */
365 register_hooks /* register hooks */