upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / modules / mappers / mod_userdir.c
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /*
18  * mod_userdir... implement the UserDir command.  Broken away from the
19  * Alias stuff for a couple of good and not-so-good reasons:
20  *
21  * 1) It shows a real minimal working example of how to do something like
22  *    this.
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...
26  *
27  * Modified by Alexei Kosut to support the following constructs
28  * (server running at www.foo.com, request for /~bar/one/two.html)
29  *
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
36  *  NOTE: here also ^ ^
37  *
38  * In addition, you can use multiple entries, to specify alternate
39  * user directories (a la Directory Index). For example:
40  *
41  * UserDir public_html /usr/web http://www.xyz.com/users
42  *
43  * Modified by Ken Coar to provide for the following:
44  *
45  * UserDir disable[d] username ...
46  * UserDir enable[d] username ...
47  *
48  * If "disabled" has no other arguments, *all* ~<username> references are
49  * disabled, except those explicitly turned on with the "enabled" keyword.
50  */
51
52 #include "apr_strings.h"
53 #include "apr_user.h"
54
55 #define APR_WANT_STRFUNC
56 #include "apr_want.h"
57
58 #if APR_HAVE_UNISTD_H
59 #include <unistd.h>
60 #endif
61
62 #include "ap_config.h"
63 #include "httpd.h"
64 #include "http_config.h"
65 #include "http_request.h"
66
67 #if !defined(WIN32) && !defined(OS2) && !defined(BEOS) && !defined(NETWARE)
68 #define HAVE_UNIX_SUEXEC
69 #endif
70
71 #ifdef HAVE_UNIX_SUEXEC
72 #include "unixd.h"        /* Contains the suexec_identity hook used on Unix */
73 #endif
74
75
76 /* The default directory in user's home dir */
77 #ifndef DEFAULT_USER_DIR
78 #define DEFAULT_USER_DIR "public_html"
79 #endif
80
81 module AP_MODULE_DECLARE_DATA userdir_module;
82
83 typedef struct {
84     int globally_disabled;
85     char *userdir;
86     apr_table_t *enabled_users;
87     apr_table_t *disabled_users;
88 } userdir_config;
89
90 /*
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.
94  */
95
96 static void *create_userdir_config(apr_pool_t *p, server_rec *s)
97 {
98     userdir_config *newcfg = apr_pcalloc(p, sizeof(*newcfg));
99
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);
104
105     return newcfg;
106 }
107
108 #define O_DEFAULT 0
109 #define O_ENABLE 1
110 #define O_DISABLE 2
111
112 static const char *set_user_dir(cmd_parms *cmd, void *dummy, const char *arg)
113 {
114     userdir_config *s_cfg = ap_get_module_config(cmd->server->module_config,
115                                                  &userdir_module);
116     char *username;
117     const char *usernames = arg;
118     char *kw = ap_getword_conf(cmd->pool, &usernames);
119     apr_table_t *usertable;
120
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.
123      */
124     if (*kw == '\0') {
125         return "UserDir requires an argument.";
126     }
127
128     /*
129      * Let's do the comparisons once.
130      */
131     if ((!strcasecmp(kw, "disable")) || (!strcasecmp(kw, "disabled"))) {
132         /*
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.
135          */
136         if (strlen(usernames) == 0) {
137             s_cfg->globally_disabled = 1;
138             return NULL;
139         }
140         usertable = s_cfg->disabled_users;
141     }
142     else if ((!strcasecmp(kw, "enable")) || (!strcasecmp(kw, "enabled"))) {
143         /*
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
146          * it.
147          */
148         if (strlen(usernames) == 0) {
149             return "UserDir \"enable\" keyword requires a list of usernames";
150         }
151         usertable = s_cfg->enabled_users;
152     }
153     else {
154         /*
155          * If the first (only?) value isn't one of our keywords, just copy
156          * the string to the userdir string.
157          */
158         s_cfg->userdir = apr_pstrdup(cmd->pool, arg);
159         return NULL;
160     }
161     /*
162      * Now we just take each word in turn from the command line and add it to
163      * the appropriate table.
164      */
165     while (*usernames) {
166         username = ap_getword_conf(cmd->pool, &usernames);
167         apr_table_setn(usertable, username, kw);
168     }
169     return NULL;
170 }
171
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...'"),
177     {NULL}
178 };
179
180 static int translate_userdir(request_rec *r)
181 {
182     ap_conf_vector_t *server_conf;
183     const userdir_config *s_cfg;
184     char *name = r->uri;
185     const char *userdirs;
186     const char *w, *dname;
187     char *redirect;
188     apr_finfo_t statbuf;
189
190     /*
191      * If the URI doesn't match our basic pattern, we've nothing to do with
192      * it.
193      */
194     if (name[0] != '/' || name[1] != '~') {
195         return DECLINED;
196     }
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) {
201         return DECLINED;
202     }
203
204     dname = name + 2;
205     w = ap_getword(r->pool, &dname, '/');
206
207     /*
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).
212      */
213
214     if (dname[-1] == '/') {
215         --dname;
216     }
217
218     /*
219      * If there's no username, it's not for us.  Ignore . and .. as well.
220      */
221     if (w[0] == '\0' || (w[1] == '.' && (w[2] == '\0' || (w[2] == '.' && w[3] == '\0')))) {
222         return DECLINED;
223     }
224     /*
225      * Nor if there's an username but it's in the disabled list.
226      */
227     if (apr_table_get(s_cfg->disabled_users, w) != NULL) {
228         return DECLINED;
229     }
230     /*
231      * If there's a global interdiction on UserDirs, check to see if this
232      * name is one of the Blessed.
233      */
234     if (s_cfg->globally_disabled
235         && apr_table_get(s_cfg->enabled_users, w) == NULL) {
236         return DECLINED;
237     }
238
239     /*
240      * Special cases all checked, onward to normal substitution processing.
241      */
242
243     while (*userdirs) {
244         const char *userdir = ap_getword_conf(r->pool, &userdirs);
245         char *filename = NULL, *x = NULL;
246         apr_status_t rv;
247         int is_absolute = ap_os_is_path_absolute(r->pool, userdir);
248
249         if (ap_strchr_c(userdir, '*'))
250             x = ap_getword(r->pool, &userdir, '*');
251
252         if (userdir[0] == '\0' || is_absolute) {
253             if (x) {
254 #ifdef HAVE_DRIVE_LETTERS
255                 /*
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 
260                  * was specified
261                  */
262                 if (strchr(x + 2, ':'))
263 #else
264                 if (strchr(x, ':') && !is_absolute)
265 #endif /* HAVE_DRIVE_LETTERS */
266                 {
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;
270                 }
271                 else
272                     filename = apr_pstrcat(r->pool, x, w, userdir, NULL);
273             }
274             else
275                 filename = apr_pstrcat(r->pool, userdir, "/", w, NULL);
276         }
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;
281         }
282         else {
283 #if APR_HAS_USER
284             char *homedir;
285
286             if (apr_get_home_directory(&homedir, w, r->pool) == APR_SUCCESS) {
287                 filename = apr_pstrcat(r->pool, homedir, "/", userdir, NULL);
288             }
289 #else
290             return DECLINED;
291 #endif
292         }
293
294         /*
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.
299          */
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
308              */
309             if (*userdirs && dname[0] == 0)
310                 r->finfo = statbuf;
311
312             /* For use in the get_suexec_identity phase */
313             apr_table_setn(r->notes, "mod_userdir_user", w);
314
315             return OK;
316         }
317     }
318
319     return DECLINED;
320 }
321
322 #ifdef HAVE_UNIX_SUEXEC
323 static ap_unix_identity_t *get_suexec_id_doer(const request_rec *r)
324 {
325     ap_unix_identity_t *ugid = NULL;
326 #if APR_HAS_USER
327     const char *username = apr_table_get(r->notes, "mod_userdir_user");
328
329     if (username == NULL) {
330         return NULL;
331     }
332
333     if ((ugid = apr_palloc(r->pool, sizeof(*ugid))) == NULL) {
334         return NULL;
335     }
336
337     if (apr_get_userid(&ugid->uid, &ugid->gid, username, r->pool) != APR_SUCCESS) {
338         return NULL;
339     }
340
341     ugid->userdir = 1;
342 #endif 
343     return ugid;
344 }
345 #endif /* HAVE_UNIX_SUEXEC */
346
347 static void register_hooks(apr_pool_t *p)
348 {
349     static const char * const aszPre[]={ "mod_alias.c",NULL };
350     static const char * const aszSucc[]={ "mod_vhost_alias.c",NULL };
351
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);
355 #endif
356 }
357
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 */
366 };