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