userplex: Corrected opening of /dev/null.
[ashd.git] / src / userplex.c
CommitLineData
3b5e2f2d
FT
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <unistd.h>
22#include <fcntl.h>
23#include <string.h>
24#include <signal.h>
25#include <sys/socket.h>
26#include <errno.h>
27#include <pwd.h>
28#include <grp.h>
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33#include <utils.h>
34#include <log.h>
35#include <req.h>
36#include <resp.h>
37
38struct user {
39 struct user *next, *prev;
40 char *name;
41 int fd;
42};
43
44static int ignore = 0;
45static char *mgroup = NULL;
23c627d2 46static char *dirname = NULL;
3b5e2f2d
FT
47static char **childspec;
48static uid_t minuid = 0;
d341283f 49static int usesyslog = 0;
3b5e2f2d
FT
50static struct user *users = NULL;
51
52static void login(struct passwd *pwd)
53{
54 int fd;
55
3227f13e 56 setsid();
3b5e2f2d
FT
57 if(getuid() == 0) {
58 if(initgroups(pwd->pw_name, pwd->pw_gid)) {
59 flog(LOG_ERR, "could not init group list for %s: %s", pwd->pw_name, strerror(errno));
60 exit(1);
61 }
62 if(setgid(pwd->pw_gid)) {
63 flog(LOG_ERR, "could not switch group for %s: %s", pwd->pw_name, strerror(errno));
64 exit(1);
65 }
66 if(setuid(pwd->pw_uid)) {
67 flog(LOG_ERR, "could not switch user to %s: %s", pwd->pw_name, strerror(errno));
68 exit(1);
69 }
70 } else {
71 if(getuid() != pwd->pw_uid)
72 exit(1);
73 }
74 if(chdir(pwd->pw_dir)) {
75 flog(LOG_ERR, "could not change to home directory for %s: %s", pwd->pw_name, strerror(errno));
76 exit(1);
77 }
d341283f
FT
78 if(usesyslog)
79 putenv("ASHD_USESYSLOG=1");
80 else
81 unsetenv("ASHD_USESYSLOG");
3b5e2f2d
FT
82 putenv(sprintf2("HOME=%s", pwd->pw_dir));
83 putenv(sprintf2("SHELL=%s", pwd->pw_shell));
84 putenv(sprintf2("USER=%s", pwd->pw_name));
85 putenv(sprintf2("LOGNAME", pwd->pw_name));
86 /* There's whole load of other stuff one could want to do here --
87 * getting Kerberos credentials, running PAM session modules, and
88 * who knows what. I'll add them along as I find them useful. */
1755d287 89 if(((fd = open(".ashd/output", O_WRONLY | O_APPEND)) >= 0) ||
9e70ef79 90 ((fd = open("/dev/null", O_WRONLY)) >= 0)) {
3b5e2f2d
FT
91 dup2(fd, 1);
92 close(fd);
93 }
1755d287 94 if(((fd = open(".ashd/error", O_WRONLY | O_APPEND)) >= 0) ||
9e70ef79 95 ((fd = open("/dev/null", O_WRONLY)) >= 0)) {
3b5e2f2d
FT
96 dup2(fd, 2);
97 close(fd);
98 }
99}
100
101static void execchild(struct passwd *pwd)
102{
103 if(!ignore)
104 execl(".ashd/handler", ".ashd/handler", NULL);
105 if(dirname != NULL) {
106 if(access(dirname, X_OK | R_OK))
107 return;
108 }
109 execvp(childspec[0], childspec);
110}
111
112static int forkchild(char *usrnm)
113{
114 struct passwd *pwd;
115 pid_t pid;
470938bd 116 int fd[2];
3b5e2f2d
FT
117
118 /* XXX: There should be a way for the child to report errors (like
119 * 404 when htpub doesn't exist), but for now I don't bother with
120 * that. I might return to it at some later time. */
121 if(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fd))
122 return(-1);
123 if((pwd = getpwnam(usrnm)) == NULL) {
124 flog(LOG_ERR, "already discovered user `%s' has disappeared", usrnm);
125 return(-1);
126 }
127 if((pid = fork()) < 0)
128 return(-1);
129 if(pid == 0) {
3b5e2f2d
FT
130 dup2(fd[0], 0);
131 close(fd[0]);
470938bd 132 close(fd[1]);
3b5e2f2d
FT
133 login(pwd);
134 execchild(pwd);
135 exit(127);
136 }
137 close(fd[0]);
470938bd 138 fcntl(fd[1], F_SETFD, FD_CLOEXEC);
3b5e2f2d
FT
139 return(fd[1]);
140}
141
142static void serve2(struct user *usr, struct hthead *req, int fd)
143{
144 if(usr->fd < 0)
145 usr->fd = forkchild(usr->name);
146 if(sendreq(usr->fd, req, fd)) {
f2df7a1b 147 if((errno == EPIPE) || (errno == ECONNRESET)) {
3b5e2f2d
FT
148 /* Assume that the child has crashed and restart it. */
149 close(usr->fd);
150 usr->fd = forkchild(usr->name);
151 if(!sendreq(usr->fd, req, fd))
152 return;
153 }
154 flog(LOG_ERR, "could not pass on request to user `%s': %s", usr->name, strerror(errno));
155 close(usr->fd);
156 usr->fd = -1;
157 simpleerror(fd, 500, "User Error", "The request handler for that user keeps crashing.");
158 }
159}
160
161static void initnew(struct hthead *req, int fd, char *usrnm)
162{
163 struct user *usr;
164 struct passwd *pwd;
165 struct group *grp;
166 int i, valid;
167
168 pwd = getpwnam(usrnm);
169 if(pwd == NULL) {
170 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
171 return;
172 }
173 if(pwd->pw_uid < minuid) {
174 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
175 return;
176 }
177 if(mgroup) {
178 if((grp = getgrnam(mgroup)) == NULL) {
179 flog(LOG_ERR, "unknown group %s specified to userplex", mgroup);
180 simpleerror(fd, 500, "Configuration Error", "The server has been erroneously configured.");
181 return;
182 }
183 valid = 0;
184 if(grp->gr_gid == pwd->pw_gid) {
185 valid = 1;
186 } else {
187 for(i = 0; grp->gr_mem[i] != NULL; i++) {
188 if(!strcmp(grp->gr_mem[i], usrnm)) {
189 valid = 1;
190 break;
191 }
192 }
193 }
194 if(!valid) {
195 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
196 return;
197 }
198 }
199 omalloc(usr);
200 usr->name = sstrdup(usrnm);
201 usr->fd = -1;
202 usr->next = users;
203 usr->prev = NULL;
204 if(users != NULL)
205 users->prev = usr;
206 users = usr;
207 serve2(usr, req, fd);
208}
209
210static void serve(struct hthead *req, int fd)
211{
212 struct user *usr;
213 char *usrnm, *p;
214
215 if((p = strchr(req->rest, '/')) == NULL) {
216 if(*req->rest)
217 stdredir(req, fd, 301, sprintf3("%s/", req->url));
218 else
219 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
220 return;
221 }
222 *(p++) = 0;
223 usrnm = sstrdup(req->rest);
224 replrest(req, p);
225 for(usr = users; usr != NULL; usr = usr->next) {
226 if(!strcmp(usr->name, usrnm)) {
227 serve2(usr, req, fd);
228 goto out;
229 }
230 }
231 initnew(req, fd, usrnm);
232
233out:
234 free(usrnm);
235}
236
fd735432
FT
237static void sighandler(int sig)
238{
239}
240
3b5e2f2d
FT
241static void usage(FILE *out)
242{
d341283f 243 fprintf(out, "usage: userplex [-hIs] [-g GROUP] [-m MIN-UID] [-d PUB-DIR] [PROGRAM ARGS...]\n");
3b5e2f2d
FT
244}
245
246int main(int argc, char **argv)
247{
248 struct hthead *req;
249 int c;
250 int fd;
251 struct charvbuf csbuf;
252
d341283f 253 while((c = getopt(argc, argv, "+hIsg:m:d:")) >= 0) {
3b5e2f2d
FT
254 switch(c) {
255 case 'I':
256 ignore = 1;
257 break;
d341283f
FT
258 case 's':
259 usesyslog = 1;
260 break;
3b5e2f2d
FT
261 case 'm':
262 if((minuid = atoi(optarg)) < 1) {
263 fprintf(stderr, "userplex: argument to -m must be greater than 0\n");
264 exit(1);
265 }
266 break;
267 case 'g':
268 mgroup = optarg;
269 break;
270 case 'd':
271 dirname = optarg;
272 break;
273 case 'h':
274 usage(stdout);
275 exit(0);
276 default:
277 usage(stderr);
278 exit(1);
279 }
280 }
281 if(optind < argc) {
282 childspec = argv + optind;
3b5e2f2d 283 } else {
23c627d2
FT
284 if(dirname == NULL)
285 dirname = "htpub";
3b5e2f2d
FT
286 bufinit(csbuf);
287 bufadd(csbuf, "dirplex");
288 bufadd(csbuf, dirname);
289 bufadd(csbuf, NULL);
290 childspec = csbuf.b;
291 }
292 signal(SIGCHLD, SIG_IGN);
fd735432 293 signal(SIGPIPE, sighandler);
3b5e2f2d
FT
294 while(1) {
295 if((fd = recvreq(0, &req)) < 0) {
296 if(errno != 0)
297 flog(LOG_ERR, "recvreq: %s", strerror(errno));
298 break;
299 }
300 serve(req, fd);
301 freehthead(req);
302 close(fd);
303 }
304 return(0);
305}