2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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/>.
25 #include <sys/socket.h>
39 struct user *next, *prev;
44 static int ignore = 0;
45 static char *mgroup = NULL;
46 static char *dirname = NULL;
47 static char **childspec;
48 static uid_t minuid = 0;
49 static struct user *users = NULL;
51 static void login(struct passwd *pwd)
56 if(initgroups(pwd->pw_name, pwd->pw_gid)) {
57 flog(LOG_ERR, "could not init group list for %s: %s", pwd->pw_name, strerror(errno));
60 if(setgid(pwd->pw_gid)) {
61 flog(LOG_ERR, "could not switch group for %s: %s", pwd->pw_name, strerror(errno));
64 if(setuid(pwd->pw_uid)) {
65 flog(LOG_ERR, "could not switch user to %s: %s", pwd->pw_name, strerror(errno));
69 if(getuid() != pwd->pw_uid)
72 if(chdir(pwd->pw_dir)) {
73 flog(LOG_ERR, "could not change to home directory for %s: %s", pwd->pw_name, strerror(errno));
76 putenv(sprintf2("HOME=%s", pwd->pw_dir));
77 putenv(sprintf2("SHELL=%s", pwd->pw_shell));
78 putenv(sprintf2("USER=%s", pwd->pw_name));
79 putenv(sprintf2("LOGNAME", pwd->pw_name));
80 /* There's whole load of other stuff one could want to do here --
81 * getting Kerberos credentials, running PAM session modules, and
82 * who knows what. I'll add them along as I find them useful. */
83 if(((fd = open(".ashd/output", O_WRONLY | O_APPEND)) >= 0) ||
84 ((fd = open("/dev/null", 0)) >= 0)) {
88 if(((fd = open(".ashd/error", O_WRONLY | O_APPEND)) >= 0) ||
89 ((fd = open("/dev/null", 0)) >= 0)) {
95 static void execchild(struct passwd *pwd)
98 execl(".ashd/handler", ".ashd/handler", NULL);
100 if(access(dirname, X_OK | R_OK))
103 execvp(childspec[0], childspec);
106 static int forkchild(char *usrnm)
112 /* XXX: There should be a way for the child to report errors (like
113 * 404 when htpub doesn't exist), but for now I don't bother with
114 * that. I might return to it at some later time. */
115 if(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fd))
117 if((pwd = getpwnam(usrnm)) == NULL) {
118 flog(LOG_ERR, "already discovered user `%s' has disappeared", usrnm);
121 if((pid = fork()) < 0)
124 for(i = 3; i < FD_SETSIZE; i++) {
138 static void serve2(struct user *usr, struct hthead *req, int fd)
141 usr->fd = forkchild(usr->name);
142 if(sendreq(usr->fd, req, fd)) {
144 /* Assume that the child has crashed and restart it. */
146 usr->fd = forkchild(usr->name);
147 if(!sendreq(usr->fd, req, fd))
150 flog(LOG_ERR, "could not pass on request to user `%s': %s", usr->name, strerror(errno));
153 simpleerror(fd, 500, "User Error", "The request handler for that user keeps crashing.");
157 static void initnew(struct hthead *req, int fd, char *usrnm)
164 pwd = getpwnam(usrnm);
166 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
169 if(pwd->pw_uid < minuid) {
170 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
174 if((grp = getgrnam(mgroup)) == NULL) {
175 flog(LOG_ERR, "unknown group %s specified to userplex", mgroup);
176 simpleerror(fd, 500, "Configuration Error", "The server has been erroneously configured.");
180 if(grp->gr_gid == pwd->pw_gid) {
183 for(i = 0; grp->gr_mem[i] != NULL; i++) {
184 if(!strcmp(grp->gr_mem[i], usrnm)) {
191 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
196 usr->name = sstrdup(usrnm);
203 serve2(usr, req, fd);
206 static void serve(struct hthead *req, int fd)
211 if((p = strchr(req->rest, '/')) == NULL) {
213 stdredir(req, fd, 301, sprintf3("%s/", req->url));
215 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
219 usrnm = sstrdup(req->rest);
221 for(usr = users; usr != NULL; usr = usr->next) {
222 if(!strcmp(usr->name, usrnm)) {
223 serve2(usr, req, fd);
227 initnew(req, fd, usrnm);
233 static void usage(FILE *out)
235 fprintf(out, "usage: userplex [-hI] [-g GROUP] [-m MIN-UID] [-d PUB-DIR] [PROGRAM ARGS...]\n");
238 int main(int argc, char **argv)
243 struct charvbuf csbuf;
245 while((c = getopt(argc, argv, "+hIg:m:d:")) >= 0) {
251 if((minuid = atoi(optarg)) < 1) {
252 fprintf(stderr, "userplex: argument to -m must be greater than 0\n");
271 childspec = argv + optind;
276 bufadd(csbuf, "dirplex");
277 bufadd(csbuf, dirname);
281 signal(SIGCHLD, SIG_IGN);
283 if((fd = recvreq(0, &req)) < 0) {
285 flog(LOG_ERR, "recvreq: %s", strerror(errno));