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