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 int usesyslog = 0;
50 static struct user *users = NULL;
52 static void login(struct passwd *pwd)
57 if(initgroups(pwd->pw_name, pwd->pw_gid)) {
58 flog(LOG_ERR, "could not init group list for %s: %s", pwd->pw_name, strerror(errno));
61 if(setgid(pwd->pw_gid)) {
62 flog(LOG_ERR, "could not switch group for %s: %s", pwd->pw_name, strerror(errno));
65 if(setuid(pwd->pw_uid)) {
66 flog(LOG_ERR, "could not switch user to %s: %s", pwd->pw_name, strerror(errno));
70 if(getuid() != pwd->pw_uid)
73 if(chdir(pwd->pw_dir)) {
74 flog(LOG_ERR, "could not change to home directory for %s: %s", pwd->pw_name, strerror(errno));
78 putenv("ASHD_USESYSLOG=1");
80 unsetenv("ASHD_USESYSLOG");
81 putenv(sprintf2("HOME=%s", pwd->pw_dir));
82 putenv(sprintf2("SHELL=%s", pwd->pw_shell));
83 putenv(sprintf2("USER=%s", pwd->pw_name));
84 putenv(sprintf2("LOGNAME", pwd->pw_name));
85 /* There's whole load of other stuff one could want to do here --
86 * getting Kerberos credentials, running PAM session modules, and
87 * who knows what. I'll add them along as I find them useful. */
88 if(((fd = open(".ashd/output", O_WRONLY | O_APPEND)) >= 0) ||
89 ((fd = open("/dev/null", 0)) >= 0)) {
93 if(((fd = open(".ashd/error", O_WRONLY | O_APPEND)) >= 0) ||
94 ((fd = open("/dev/null", 0)) >= 0)) {
100 static void execchild(struct passwd *pwd)
103 execl(".ashd/handler", ".ashd/handler", NULL);
104 if(dirname != NULL) {
105 if(access(dirname, X_OK | R_OK))
108 execvp(childspec[0], childspec);
111 static int forkchild(char *usrnm)
117 /* XXX: There should be a way for the child to report errors (like
118 * 404 when htpub doesn't exist), but for now I don't bother with
119 * that. I might return to it at some later time. */
120 if(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fd))
122 if((pwd = getpwnam(usrnm)) == NULL) {
123 flog(LOG_ERR, "already discovered user `%s' has disappeared", usrnm);
126 if((pid = fork()) < 0)
129 for(i = 3; i < FD_SETSIZE; i++) {
143 static void serve2(struct user *usr, struct hthead *req, int fd)
146 usr->fd = forkchild(usr->name);
147 if(sendreq(usr->fd, req, fd)) {
148 if((errno == EPIPE) || (errno == ECONNRESET)) {
149 /* Assume that the child has crashed and restart it. */
151 usr->fd = forkchild(usr->name);
152 if(!sendreq(usr->fd, req, fd))
155 flog(LOG_ERR, "could not pass on request to user `%s': %s", usr->name, strerror(errno));
158 simpleerror(fd, 500, "User Error", "The request handler for that user keeps crashing.");
162 static void initnew(struct hthead *req, int fd, char *usrnm)
169 pwd = getpwnam(usrnm);
171 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
174 if(pwd->pw_uid < minuid) {
175 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
179 if((grp = getgrnam(mgroup)) == NULL) {
180 flog(LOG_ERR, "unknown group %s specified to userplex", mgroup);
181 simpleerror(fd, 500, "Configuration Error", "The server has been erroneously configured.");
185 if(grp->gr_gid == pwd->pw_gid) {
188 for(i = 0; grp->gr_mem[i] != NULL; i++) {
189 if(!strcmp(grp->gr_mem[i], usrnm)) {
196 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
201 usr->name = sstrdup(usrnm);
208 serve2(usr, req, fd);
211 static void serve(struct hthead *req, int fd)
216 if((p = strchr(req->rest, '/')) == NULL) {
218 stdredir(req, fd, 301, sprintf3("%s/", req->url));
220 simpleerror(fd, 404, "Not Found", "No such resource could be found.");
224 usrnm = sstrdup(req->rest);
226 for(usr = users; usr != NULL; usr = usr->next) {
227 if(!strcmp(usr->name, usrnm)) {
228 serve2(usr, req, fd);
232 initnew(req, fd, usrnm);
238 static void sighandler(int sig)
242 static void usage(FILE *out)
244 fprintf(out, "usage: userplex [-hIs] [-g GROUP] [-m MIN-UID] [-d PUB-DIR] [PROGRAM ARGS...]\n");
247 int main(int argc, char **argv)
252 struct charvbuf csbuf;
254 while((c = getopt(argc, argv, "+hIsg:m:d:")) >= 0) {
263 if((minuid = atoi(optarg)) < 1) {
264 fprintf(stderr, "userplex: argument to -m must be greater than 0\n");
283 childspec = argv + optind;
288 bufadd(csbuf, "dirplex");
289 bufadd(csbuf, dirname);
293 signal(SIGCHLD, SIG_IGN);
294 signal(SIGPIPE, sighandler);
296 if((fd = recvreq(0, &req)) < 0) {
298 flog(LOG_ERR, "recvreq: %s", strerror(errno));