tilde-luzah-bozo.c revision 1.1.1.5 1 /* $eterna: tilde-luzah-bozo.c,v 1.14 2010/05/10 14:36:37 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1997-2010 Matthew R. Green
5 * All rights 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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer and
14 * dedication in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31 /* this code implements ~user support for bozohttpd */
32
33 #ifndef NO_USER_SUPPORT
34
35 #include <sys/param.h>
36
37 #include <errno.h>
38 #include <pwd.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "bozohttpd.h"
44
45 /*
46 * bozo_user_transform does this:
47 * - chdir's /~user/public_html
48 * - returns the rest of the file, index.html appended if required
49 * - returned malloced file to serve in request->hr_file,
50 * ala transform_request().
51 *
52 * transform_request() is supposed to check that we have user support
53 * enabled.
54 */
55 int
56 bozo_user_transform(bozo_httpreq_t *request, int *isindex)
57 {
58 bozohttpd_t *httpd = request->hr_httpd;
59 char c, *s, *file = NULL;
60 struct passwd *pw;
61
62 *isindex = 0;
63
64 if ((s = strchr(request->hr_file + 2, '/')) != NULL) {
65 *s++ = '\0';
66 c = s[strlen(s)-1];
67 *isindex = (c == '/' || c == '\0');
68 }
69
70 debug((httpd, DEBUG_OBESE, "looking for user %s",
71 request->hr_file + 2));
72 pw = getpwnam(request->hr_file + 2);
73 /* fix this up immediately */
74 if (s)
75 s[-1] = '/';
76 if (pw == NULL) {
77 (void)bozo_http_error(httpd, 404, request, "no such user");
78 return 0;
79 }
80
81 debug((httpd, DEBUG_OBESE, "user %s home dir %s uid %d gid %d",
82 pw->pw_name, pw->pw_dir, pw->pw_uid, pw->pw_gid));
83
84 if (chdir(pw->pw_dir) < 0) {
85 bozo_warn(httpd, "chdir1 error: %s: %s", pw->pw_dir,
86 strerror(errno));
87 (void)bozo_http_error(httpd, 403, request,
88 "can't chdir to homedir");
89 return 0;
90 }
91 if (chdir(httpd->public_html) < 0) {
92 bozo_warn(httpd, "chdir2 error: %s: %s", httpd->public_html,
93 strerror(errno));
94 (void)bozo_http_error(httpd, 403, request,
95 "can't chdir to public_html");
96 return 0;
97 }
98 if (s == NULL || *s == '\0') {
99 file = bozostrdup(httpd, httpd->index_html);
100 } else {
101 file = bozomalloc(httpd, strlen(s) +
102 (*isindex ? strlen(httpd->index_html) + 1 : 1));
103 strcpy(file, s);
104 if (*isindex)
105 strcat(file, httpd->index_html);
106 }
107
108 /* see transform_request() */
109 if (*file == '/' || strcmp(file, "..") == 0 ||
110 strstr(file, "/..") || strstr(file, "../")) {
111 (void)bozo_http_error(httpd, 403, request, "illegal request");
112 free(file);
113 return 0;
114 }
115
116 if (bozo_auth_check(request, file)) {
117 free(file);
118 return 0;
119 }
120
121 free(request->hr_file);
122 request->hr_file = file;
123
124 debug((httpd, DEBUG_FAT, "transform_user returning %s under %s", file,
125 pw->pw_dir));
126 return 1;
127 }
128 #endif /* NO_USER_SUPPORT */
129