]> granicus.if.org Git - apache/blob - modules/mappers/mod_userdir.c
9a6871e81259014055738f9606ae4fe9963d8914
[apache] / modules / mappers / mod_userdir.c
1 /* ====================================================================
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2000-2002 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_userdir... implement the UserDir command.  Broken away from the
61  * Alias stuff for a couple of good and not-so-good reasons:
62  *
63  * 1) It shows a real minimal working example of how to do something like
64  *    this.
65  * 2) I know people who are actually interested in changing this *particular*
66  *    aspect of server functionality without changing the rest of it.  That's
67  *    what this whole modular arrangement is supposed to be good at...
68  *
69  * Modified by Alexei Kosut to support the following constructs
70  * (server running at www.foo.com, request for /~bar/one/two.html)
71  *
72  * UserDir public_html      -> ~bar/public_html/one/two.html
73  * UserDir /usr/web         -> /usr/web/bar/one/two.html
74  * UserDir /home/ * /www     -> /home/bar/www/one/two.html
75  *  NOTE: theses ^ ^ space only added allow it to work in a comment, ignore
76  * UserDir http://x/users   -> (302) http://x/users/bar/one/two.html
77  * UserDir http://x/ * /y     -> (302) http://x/bar/y/one/two.html
78  *  NOTE: here also ^ ^
79  *
80  * In addition, you can use multiple entries, to specify alternate
81  * user directories (a la Directory Index). For example:
82  *
83  * UserDir public_html /usr/web http://www.xyz.com/users
84  *
85  * Modified by Ken Coar to provide for the following:
86  *
87  * UserDir disable[d] username ...
88  * UserDir enable[d] username ...
89  *
90  * If "disabled" has no other arguments, *all* ~<username> references are
91  * disabled, except those explicitly turned on with the "enabled" keyword.
92  */
93
94 #include "apr_strings.h"
95 #include "apr_user.h"
96
97 #define APR_WANT_STRFUNC
98 #include "apr_want.h"
99
100 #if APR_HAVE_UNISTD_H
101 #include <unistd.h>
102 #endif
103
104 #include "ap_config.h"
105 #include "httpd.h"
106 #include "http_config.h"
107 #include "http_request.h"
108
109 #if !defined(WIN32) && !defined(OS2) && !defined(BEOS) && !defined(NETWARE)
110 #define HAVE_UNIX_SUEXEC
111 #endif
112
113 #ifdef HAVE_UNIX_SUEXEC
114 #include "unixd.h"        /* Contains the suexec_identity hook used on Unix */
115 #endif
116
117
118 /* The default directory in user's home dir */
119 #ifndef DEFAULT_USER_DIR
120 #define DEFAULT_USER_DIR "public_html"
121 #endif
122
123 module AP_MODULE_DECLARE_DATA userdir_module;
124
125 typedef struct {
126     int globally_disabled;
127     char *userdir;
128     apr_table_t *enabled_users;
129     apr_table_t *disabled_users;
130 } userdir_config;
131
132 /*
133  * Server config for this module: global disablement flag, a list of usernames
134  * ineligible for UserDir access, a list of those immune to global (but not
135  * explicit) disablement, and the replacement string for all others.
136  */
137
138 static void *create_userdir_config(apr_pool_t *p, server_rec *s)
139 {
140     userdir_config *newcfg = apr_pcalloc(p, sizeof(*newcfg));
141
142     newcfg->globally_disabled = 0;
143     newcfg->userdir = DEFAULT_USER_DIR;
144     newcfg->enabled_users = apr_table_make(p, 4);
145     newcfg->disabled_users = apr_table_make(p, 4);
146
147     return newcfg;
148 }
149
150 #define O_DEFAULT 0
151 #define O_ENABLE 1
152 #define O_DISABLE 2
153
154 static const char *set_user_dir(cmd_parms *cmd, void *dummy, const char *arg)
155 {
156     userdir_config *s_cfg = ap_get_module_config(cmd->server->module_config,
157                                                  &userdir_module);
158     char *username;
159     const char *usernames = arg;
160     char *kw = ap_getword_conf(cmd->pool, &usernames);
161     apr_table_t *usertable;
162
163     /*
164      * Let's do the comparisons once.
165      */
166     if ((!strcasecmp(kw, "disable")) || (!strcasecmp(kw, "disabled"))) {
167         /*
168          * If there are no usernames specified, this is a global disable - we
169          * need do no more at this point than record the fact.
170          */
171         if (strlen(usernames) == 0) {
172             s_cfg->globally_disabled = 1;
173             return NULL;
174         }
175         usertable = s_cfg->disabled_users;
176     }
177     else if ((!strcasecmp(kw, "enable")) || (!strcasecmp(kw, "enabled"))) {
178         /*
179          * The "disable" keyword can stand alone or take a list of names, but
180          * the "enable" keyword requires the list.  Whinge if it doesn't have
181          * it.
182          */
183         if (strlen(usernames) == 0) {
184             return "UserDir \"enable\" keyword requires a list of usernames";
185         }
186         usertable = s_cfg->enabled_users;
187     }
188     else {
189         /*
190          * If the first (only?) value isn't one of our keywords, just copy
191          * the string to the userdir string.
192          */
193         s_cfg->userdir = apr_pstrdup(cmd->pool, arg);
194         return NULL;
195     }
196     /*
197      * Now we just take each word in turn from the command line and add it to
198      * the appropriate table.
199      */
200     while (*usernames) {
201         username = ap_getword_conf(cmd->pool, &usernames);
202         apr_table_setn(usertable, username, kw);
203     }
204     return NULL;
205 }
206
207 static const command_rec userdir_cmds[] = {
208     AP_INIT_RAW_ARGS("UserDir", set_user_dir, NULL, RSRC_CONF,
209                      "the public subdirectory in users' home directories, or "
210                      "'disabled', or 'disabled username username...', or "
211                      "'enabled username username...'"),
212     {NULL}
213 };
214
215 static int translate_userdir(request_rec *r)
216 {
217     ap_conf_vector_t *server_conf = r->server->module_config;
218     const userdir_config *s_cfg = ap_get_module_config(server_conf,
219                                                        &userdir_module);
220     char *name = r->uri;
221     const char *userdirs = s_cfg->userdir;
222     const char *w, *dname;
223     char *redirect;
224     char *x = NULL;
225     apr_finfo_t statbuf;
226
227     /*
228      * If the URI doesn't match our basic pattern, we've nothing to do with
229      * it.
230      */
231     if (s_cfg->userdir == NULL || name[0] != '/' || name[1] != '~') {
232         return DECLINED;
233     }
234
235     dname = name + 2;
236     w = ap_getword(r->pool, &dname, '/');
237
238     /*
239      * The 'dname' funny business involves backing it up to capture the '/'
240      * delimiting the "/~user" part from the rest of the URL, in case there
241      * was one (the case where there wasn't being just "GET /~user HTTP/1.0",
242      * for which we don't want to tack on a '/' onto the filename).
243      */
244
245     if (dname[-1] == '/') {
246         --dname;
247     }
248
249     /*
250      * If there's no username, it's not for us.  Ignore . and .. as well.
251      */
252     if (w[0] == '\0' || (w[1] == '.' && (w[2] == '\0' || (w[2] == '.' && w[3] == '\0')))) {
253         return DECLINED;
254     }
255     /*
256      * Nor if there's an username but it's in the disabled list.
257      */
258     if (apr_table_get(s_cfg->disabled_users, w) != NULL) {
259         return DECLINED;
260     }
261     /*
262      * If there's a global interdiction on UserDirs, check to see if this
263      * name is one of the Blessed.
264      */
265     if (s_cfg->globally_disabled
266         && apr_table_get(s_cfg->enabled_users, w) == NULL) {
267         return DECLINED;
268     }
269
270     /*
271      * Special cases all checked, onward to normal substitution processing.
272      */
273
274     while (*userdirs) {
275         const char *userdir = ap_getword_conf(r->pool, &userdirs);
276         char *filename = NULL;
277         apr_status_t rv;
278         int is_absolute = ap_os_is_path_absolute(r->pool, userdir);
279
280         if (ap_strchr_c(userdir, '*'))
281             x = ap_getword(r->pool, &userdir, '*');
282
283         if (userdir[0] == '\0' || is_absolute) {
284             if (x) {
285 #ifdef HAVE_DRIVE_LETTERS
286                 /*
287                  * Crummy hack. Need to figure out whether we have been
288                  * redirected to a URL or to a file on some drive. Since I
289                  * know of no protocols that are a single letter, ignore
290                  * a : as the first or second character, and assume a file 
291                  * was specified
292                  */
293                 if (strchr(x + 2, ':'))
294 #else
295                 if (strchr(x, ':') && !is_absolute)
296 #endif /* HAVE_DRIVE_LETTERS */
297                 {
298                     redirect = apr_pstrcat(r->pool, x, w, userdir, dname, NULL);
299                     apr_table_setn(r->headers_out, "Location", redirect);
300                     return HTTP_MOVED_TEMPORARILY;
301                 }
302                 else
303                     filename = apr_pstrcat(r->pool, x, w, userdir, NULL);
304             }
305             else
306                 filename = apr_pstrcat(r->pool, userdir, "/", w, NULL);
307         }
308         else if (ap_strchr_c(userdir, ':')) {
309             redirect = apr_pstrcat(r->pool, userdir, "/", w, dname, NULL);
310             apr_table_setn(r->headers_out, "Location", redirect);
311             return HTTP_MOVED_TEMPORARILY;
312         }
313         else {
314 #if APR_HAS_USER
315             char *homedir;
316
317             if (apr_get_home_directory(&homedir, w, r->pool) == APR_SUCCESS) {
318                 filename = apr_pstrcat(r->pool, homedir, "/", userdir, NULL);
319             }
320             else {
321                 return DECLINED;
322             }
323 #else
324             return DECLINED;
325 #endif
326         }
327
328         /*
329          * Now see if it exists, or we're at the last entry. If we are at the
330          * last entry, then use the filename generated (if there is one)
331          * anyway, in the hope that some handler might handle it. This can be
332          * used, for example, to run a CGI script for the user.
333          */
334         if (filename && (!*userdirs 
335                       || ((rv = apr_stat(&statbuf, filename, APR_FINFO_MIN,
336                                          r->pool)) == APR_SUCCESS
337                                              || rv == APR_INCOMPLETE))) {
338             r->filename = apr_pstrcat(r->pool, filename, dname, NULL);
339             /* XXX: Does this walk us around FollowSymLink rules?
340              * When statbuf contains info on r->filename we can save a syscall
341              * by copying it to r->finfo
342              */
343             if (*userdirs && dname[0] == 0)
344                 r->finfo = statbuf;
345
346             /* For use in the get_suexec_identity phase */
347             apr_table_setn(r->notes, "mod_userdir_user", w);
348
349             return OK;
350         }
351     }
352
353     return DECLINED;
354 }
355
356 #ifdef HAVE_UNIX_SUEXEC
357 static ap_unix_identity_t *get_suexec_id_doer(const request_rec *r)
358 {
359     ap_unix_identity_t *ugid = NULL;
360 #if APR_HAS_USER
361     const char *username = apr_table_get(r->notes, "mod_userdir_user");
362
363     if (username == NULL) {
364         return NULL;
365     }
366
367     if ((ugid = apr_palloc(r->pool, sizeof(ap_unix_identity_t *))) == NULL) {
368         return NULL;
369     }
370
371     if (apr_get_userid(&ugid->uid, &ugid->gid, username, r->pool) != APR_SUCCESS) {
372         return NULL;
373     }
374
375 #endif 
376     return ugid;
377 }
378 #endif /* HAVE_UNIX_SUEXEC */
379
380 static void register_hooks(apr_pool_t *p)
381 {
382     static const char * const aszSucc[]={ "mod_alias.c",NULL };
383
384     ap_hook_translate_name(translate_userdir,NULL,aszSucc,APR_HOOK_MIDDLE);
385 #ifdef HAVE_UNIX_SUEXEC
386     ap_hook_get_suexec_identity(get_suexec_id_doer,NULL,NULL,APR_HOOK_MIDDLE);
387 #endif
388 }
389
390 module AP_MODULE_DECLARE_DATA userdir_module = {
391     STANDARD20_MODULE_STUFF,
392     NULL,                       /* dir config creater */
393     NULL,                       /* dir merger --- default is to override */
394     create_userdir_config,      /* server config */
395     NULL,                       /* merge server config */
396     userdir_cmds,               /* command apr_table_t */
397     register_hooks              /* register hooks */
398 };