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/>.
20 * XXX: All the various ways to start a child process makes this
21 * program quite ugly at the moment. It is unclear whether it is
22 * meaningfully possible to unify them better than they currently are.
31 #include <sys/socket.h>
33 #include <netinet/in.h>
48 static char **progspec;
49 static char *sockid, *unspec, *inspec;
51 static struct sockaddr *curaddr;
52 static size_t caddrlen;
53 static int cafamily, isanon;
56 static struct addrinfo *resolv(int flags)
59 struct addrinfo *ai, h;
62 if((p = strchr(inspec, ':')) != NULL) {
63 name = smalloc(p - inspec + 1);
64 memcpy(name, inspec, p - inspec);
68 name = sstrdup("localhost");
71 memset(&h, 0, sizeof(h));
72 h.ai_family = AF_UNSPEC;
73 h.ai_socktype = SOCK_STREAM;
75 ret = getaddrinfo(name, srv, &h, &ai);
78 flog(LOG_ERR, "could not resolve TCP specification `%s': %s", inspec, gai_strerror(ret));
84 static char *mksockid(char *sockid)
88 home = getenv("HOME");
89 if(home && !access(sprintf3("%s/.ashd/sockets/", home), X_OK))
90 return(sprintf3("%s/.ashd/sockets/scgi-p-%s", home, sockid));
91 return(sprintf3("/tmp/scgi-%i-%s", getuid(), sockid));
94 static char *mkanonid(void)
100 home = getenv("HOME");
101 if(home && !access(sprintf3("%s/.ashd/sockets/", home), X_OK))
102 tmpl = sprintf2("%s/.ashd/sockets/scgi-a-XXXXXX", home);
104 tmpl = sprintf2("/tmp/scgi-a-%i-XXXXXX", getuid());
105 if((fd = mkstemp(tmpl)) < 0) {
106 flog(LOG_ERR, "could not create anonymous socket `%s': %s", tmpl, strerror(errno));
114 static void setupchild(void)
116 /* PHP appears to not expect to inherit SIGCHLD set to SIG_IGN, so
117 * reset it for it. */
118 signal(SIGCHLD, SIG_DFL);
121 static void startlisten(void)
124 struct addrinfo *ai, *cai;
126 struct sockaddr_un unm;
132 for(cai = ai = resolv(AI_PASSIVE); cai != NULL; cai = cai->ai_next) {
133 if((fd = socket(cai->ai_family, cai->ai_socktype, cai->ai_protocol)) < 0)
135 if(bind(fd, cai->ai_addr, cai->ai_addrlen)) {
140 if(listen(fd, 128)) {
149 flog(LOG_ERR, "could not bind to specified TCP address: %s", strerror(errno));
152 } else if((unspec != NULL) || (sockid != NULL)) {
153 if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
154 flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno));
160 unpath = mksockid(sockid);
162 unm.sun_family = AF_UNIX;
163 strcpy(unm.sun_path, unpath);
164 if(bind(fd, (struct sockaddr *)&unm, sizeof(unm))) {
165 flog(LOG_ERR, "could not bind Unix socket to `%s': %s", unspec, strerror(errno));
168 if(listen(fd, 128)) {
169 flog(LOG_ERR, "listen: %s", strerror(errno));
173 if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
174 flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno));
177 memset(&unm, 0, sizeof(unm));
179 unm.sun_family = AF_UNIX;
180 strcpy(unm.sun_path, aname);
182 if(bind(fd, (struct sockaddr *)&unm, sizeof(unm))) {
183 flog(LOG_ERR, "could not bind Unix socket to `%s': %s", unspec, strerror(errno));
186 if(listen(fd, 128)) {
187 flog(LOG_ERR, "listen: %s", strerror(errno));
191 curaddr = smalloc(caddrlen = sizeof(unm));
192 memcpy(curaddr, &unm, sizeof(unm));
196 if((child = fork()) < 0) {
197 flog(LOG_ERR, "could not fork: %s", strerror(errno));
204 execvp(*progspec, progspec);
205 flog(LOG_ERR, "callscgi: %s: %s", *progspec, strerror(errno));
211 static void startnolisten(void)
215 if((child = fork()) < 0) {
216 flog(LOG_ERR, "could not fork: %s", strerror(errno));
221 if((fd = open("/dev/null", O_RDONLY)) < 0) {
222 flog(LOG_ERR, "/dev/null: %s", strerror(errno));
227 execvp(*progspec, progspec);
228 flog(LOG_ERR, "callscgi: %s: %s", *progspec, strerror(errno));
233 static int sconnect(void)
239 fd = socket(cafamily, SOCK_STREAM, 0);
240 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
242 if(connect(fd, curaddr, caddrlen)) {
243 if(errno == EINPROGRESS) {
244 block(fd, EV_WRITE, 30);
245 errlen = sizeof(err);
246 if(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) || ((errno = err) != 0)) {
259 static int econnect(void)
262 struct addrinfo *ai, *cai;
265 struct sockaddr_un unm;
271 for(cai = ai = resolv(0); cai != NULL; cai = cai->ai_next) {
272 if((fd = socket(cai->ai_family, cai->ai_socktype, cai->ai_protocol)) < 0)
274 if(connect(fd, cai->ai_addr, cai->ai_addrlen)) {
282 if(tries++ < nolisten) {
286 flog(LOG_ERR, "could not connect to specified TCP address: %s", strerror(errno));
289 curaddr = smalloc(caddrlen = cai->ai_addrlen);
290 memcpy(curaddr, cai->ai_addr, caddrlen);
291 cafamily = cai->ai_family;
295 } else if((unspec != NULL) || (sockid != NULL)) {
296 if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
297 flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno));
303 unpath = mksockid(sockid);
305 unm.sun_family = AF_UNIX;
306 strcpy(unm.sun_path, unpath);
307 if(connect(fd, (struct sockaddr *)&unm, sizeof(unm))) {
309 if(tries++ < nolisten) {
313 flog(LOG_ERR, "could not connect to Unix socket `%s': %s", unspec, strerror(errno));
316 curaddr = smalloc(caddrlen = sizeof(unm));
317 memcpy(curaddr, &unm, sizeof(unm));
322 flog(LOG_ERR, "callscgi: cannot use an anonymous socket without a program to start");
327 static int startconn(void)
340 static void killcuraddr(void)
345 unlink(((struct sockaddr_un *)curaddr)->sun_path);
347 kill(child, SIGTERM);
353 static int reconn(void)
357 if(curaddr != NULL) {
358 if((fd = sconnect()) >= 0)
365 static off_t passdata(FILE *in, FILE *out)
373 read = fread(buf, 1, sizeof(buf), in);
376 if(fwrite(buf, 1, read, out) != read)
383 static void bufaddenv(struct charbuf *dst, char *name, char *fmt, ...)
387 bufcatstr2(*dst, name);
389 bvprintf(dst, fmt, args);
394 static char *absolutify(char *file)
396 static int inited = 0;
397 static char cwd[1024];
401 getcwd(cwd, sizeof(cwd));
404 return(sprintf2("%s/%s", cwd, file));
406 return(sstrdup(file));
409 /* Mostly copied from callcgi. */
410 static void mkcgienv(struct hthead *req, struct charbuf *dst)
413 char *url, *pi, *tmp, *qp, *h, *p;
415 bufaddenv(dst, "SERVER_SOFTWARE", "ashd/%s", VERSION);
416 bufaddenv(dst, "GATEWAY_INTERFACE", "CGI/1.1");
417 bufaddenv(dst, "SCGI", "1");
418 bufaddenv(dst, "SERVER_PROTOCOL", "%s", req->ver);
419 bufaddenv(dst, "REQUEST_METHOD", "%s", req->method);
420 bufaddenv(dst, "REQUEST_URI", "%s", req->url);
421 url = sstrdup(req->url);
422 if((qp = strchr(url, '?')) != NULL)
424 /* XXX: This is an ugly hack (I think), but though I can think of
425 * several alternatives, none seem to be better. */
426 if(*req->rest && (strlen(url) >= strlen(req->rest)) &&
427 !strcmp(req->rest, url + strlen(url) - strlen(req->rest))) {
428 url[strlen(url) - strlen(req->rest)] = 0;
430 if((pi = unquoteurl(req->rest)) == NULL)
431 pi = sstrdup(req->rest);
432 if(!strcmp(url, "/")) {
433 /* This seems to be normal CGI behavior, but see callcgi.c for
436 pi = sprintf2("/%s", tmp = pi);
439 bufaddenv(dst, "PATH_INFO", "%s", pi);
440 bufaddenv(dst, "SCRIPT_NAME", "%s", url);
441 bufaddenv(dst, "QUERY_STRING", "%s", qp?qp:"");
444 if((h = getheader(req, "Host")) != NULL)
445 bufaddenv(dst, "SERVER_NAME", "%s", h);
446 if((h = getheader(req, "X-Ash-Server-Address")) != NULL)
447 bufaddenv(dst, "SERVER_ADDR", "%s", h);
448 if((h = getheader(req, "X-Ash-Server-Port")) != NULL)
449 bufaddenv(dst, "SERVER_PORT", "%s", h);
450 if((h = getheader(req, "X-Ash-Remote-User")) != NULL)
451 bufaddenv(dst, "REMOTE_USER", "%s", h);
452 if(((h = getheader(req, "X-Ash-Protocol")) != NULL) && !strcmp(h, "https"))
453 bufaddenv(dst, "HTTPS", "on");
454 if((h = getheader(req, "X-Ash-Address")) != NULL)
455 bufaddenv(dst, "REMOTE_ADDR", "%s", h);
456 if((h = getheader(req, "X-Ash-Port")) != NULL)
457 bufaddenv(dst, "REMOTE_PORT", "%s", h);
458 if((h = getheader(req, "Content-Type")) != NULL)
459 bufaddenv(dst, "CONTENT_TYPE", "%s", h);
460 if((h = getheader(req, "Content-Length")) != NULL)
461 bufaddenv(dst, "CONTENT_LENGTH", "%s", h);
463 bufaddenv(dst, "CONTENT_LENGTH", "0");
464 if((h = getheader(req, "X-Ash-File")) != NULL) {
466 bufaddenv(dst, "SCRIPT_FILENAME", "%s", h);
469 for(i = 0; i < req->noheaders; i++) {
470 h = sprintf2("HTTP_%s", req->headers[i][0]);
471 for(p = h; *p; p++) {
479 bufcatstr2(*dst, req->headers[i][1]);
483 static struct hthead *parseresp(FILE *in)
489 resp->ver = sstrdup("HTTP/1.1");
490 if(parseheaders(resp, in)) {
494 if((st = getheader(resp, "Status")) != NULL) {
495 if((p = strchr(st, ' ')) != NULL) {
497 resp->code = atoi(st);
498 resp->msg = sstrdup(p);
500 resp->code = atoi(st);
501 resp->msg = sstrdup(httpdefstatus(resp->code));
503 headrmheader(resp, "Status");
504 } else if(getheader(resp, "Location")) {
506 resp->msg = sstrdup("See Other");
509 resp->msg = sstrdup("OK");
514 static void serve(struct muth *muth, va_list args)
516 vavar(struct hthead *, req);
524 is = mtstdopen(fd, 1, 60, "r+", NULL);
525 os = mtstdopen(sfd, 1, 600, "r+", NULL);
528 mkcgienv(req, &head);
529 fprintf(os, "%zi:", head.d);
530 fwrite(head.b, head.d, 1, os);
533 if(passdata(is, os) < 0)
536 if((resp = parseresp(os)) == NULL)
541 if(passdata(os, is) < 0)
550 static void listenloop(struct muth *muth, va_list args)
557 block(0, EV_READ, 0);
558 if((fd = recvreq(lfd, &req)) < 0) {
560 flog(LOG_ERR, "recvreq: %s", strerror(errno));
563 mustart(serve, req, fd);
567 static void sigign(int sig)
571 static void sigexit(int sig)
573 shutdown(0, SHUT_RDWR);
576 static void usage(FILE *out)
578 fprintf(out, "usage: callscgi [-h] [-N RETRIES] [-i ID] [-u UNIX-PATH] [-t [HOST:]TCP-PORT] [PROGRAM [ARGS...]]\n");
581 int main(int argc, char **argv)
585 while((c = getopt(argc, argv, "+hN:i:u:t:")) >= 0) {
591 nolisten = atoi(optarg);
607 progspec = argv + optind;
608 if(((sockid != NULL) + (unspec != NULL) + (inspec != NULL)) > 1) {
609 flog(LOG_ERR, "callscgi: at most one of -i, -u or -t may be given");
612 signal(SIGCHLD, SIG_IGN);
613 signal(SIGPIPE, sigign);
614 signal(SIGINT, sigexit);
615 signal(SIGTERM, sigexit);
616 mustart(listenloop, 0);