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 startlisten(void)
117 struct addrinfo *ai, *cai;
119 struct sockaddr_un unm;
125 for(cai = ai = resolv(AI_PASSIVE); cai != NULL; cai = cai->ai_next) {
126 if((fd = socket(cai->ai_family, cai->ai_socktype, cai->ai_protocol)) < 0)
128 if(bind(fd, cai->ai_addr, cai->ai_addrlen)) {
133 if(listen(fd, 128)) {
142 flog(LOG_ERR, "could not bind to specified TCP address: %s", strerror(errno));
145 } else if((unspec != NULL) || (sockid != NULL)) {
146 if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
147 flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno));
153 unpath = mksockid(sockid);
155 unm.sun_family = AF_UNIX;
156 strcpy(unm.sun_path, unpath);
157 if(bind(fd, (struct sockaddr *)&unm, sizeof(unm))) {
158 flog(LOG_ERR, "could not bind Unix socket to `%s': %s", unspec, strerror(errno));
161 if(listen(fd, 128)) {
162 flog(LOG_ERR, "listen: %s", strerror(errno));
166 if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
167 flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno));
170 memset(&unm, 0, sizeof(unm));
172 unm.sun_family = AF_UNIX;
173 strcpy(unm.sun_path, aname);
175 if(bind(fd, (struct sockaddr *)&unm, sizeof(unm))) {
176 flog(LOG_ERR, "could not bind Unix socket to `%s': %s", unspec, strerror(errno));
179 if(listen(fd, 128)) {
180 flog(LOG_ERR, "listen: %s", strerror(errno));
184 curaddr = smalloc(caddrlen = sizeof(unm));
185 memcpy(curaddr, &unm, sizeof(unm));
189 if((child = fork()) < 0) {
190 flog(LOG_ERR, "could not fork: %s", strerror(errno));
195 for(i = 3; i < FD_SETSIZE; i++)
197 execvp(*progspec, progspec);
198 flog(LOG_ERR, "callscgi: %s: %s", *progspec, strerror(errno));
204 static void startnolisten(void)
208 if((child = fork()) < 0) {
209 flog(LOG_ERR, "could not fork: %s", strerror(errno));
213 for(i = 3; i < FD_SETSIZE; i++)
215 if((fd = open("/dev/null", O_RDONLY)) < 0) {
216 flog(LOG_ERR, "/dev/null: %s", strerror(errno));
221 execvp(*progspec, progspec);
222 flog(LOG_ERR, "callscgi: %s: %s", *progspec, strerror(errno));
227 static int sconnect(void)
233 fd = socket(cafamily, SOCK_STREAM, 0);
234 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
236 if(connect(fd, curaddr, caddrlen)) {
237 if(errno == EINPROGRESS) {
238 block(fd, EV_WRITE, 30);
239 errlen = sizeof(err);
240 if(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) || ((errno = err) != 0)) {
253 static int econnect(void)
256 struct addrinfo *ai, *cai;
259 struct sockaddr_un unm;
265 for(cai = ai = resolv(0); cai != NULL; cai = cai->ai_next) {
266 if((fd = socket(cai->ai_family, cai->ai_socktype, cai->ai_protocol)) < 0)
268 if(connect(fd, cai->ai_addr, cai->ai_addrlen)) {
276 if(tries++ < nolisten) {
280 flog(LOG_ERR, "could not connect to specified TCP address: %s", strerror(errno));
283 curaddr = smalloc(caddrlen = cai->ai_addrlen);
284 memcpy(curaddr, cai->ai_addr, caddrlen);
285 cafamily = cai->ai_family;
289 } else if((unspec != NULL) || (sockid != NULL)) {
290 if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
291 flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno));
297 unpath = mksockid(sockid);
299 unm.sun_family = AF_UNIX;
300 strcpy(unm.sun_path, unpath);
301 if(connect(fd, (struct sockaddr *)&unm, sizeof(unm))) {
303 if(tries++ < nolisten) {
307 flog(LOG_ERR, "could not connect to Unix socket `%s': %s", unspec, strerror(errno));
310 curaddr = smalloc(caddrlen = sizeof(unm));
311 memcpy(curaddr, &unm, sizeof(unm));
316 flog(LOG_ERR, "callscgi: cannot use an anonymous socket without a program to start");
321 static int startconn(void)
334 static void killcuraddr(void)
339 unlink(((struct sockaddr_un *)curaddr)->sun_path);
341 kill(child, SIGTERM);
347 static int reconn(void)
351 if(curaddr != NULL) {
352 if((fd = sconnect()) >= 0)
359 static off_t passdata(FILE *in, FILE *out)
367 read = fread(buf, 1, sizeof(buf), in);
370 if(fwrite(buf, 1, read, out) != read)
377 static void bufaddenv(struct charbuf *dst, char *name, char *fmt, ...)
381 bufcatstr2(*dst, name);
383 bvprintf(dst, fmt, args);
388 static char *absolutify(char *file)
390 static int inited = 0;
391 static char cwd[1024];
395 getcwd(cwd, sizeof(cwd));
398 return(sprintf2("%s/%s", cwd, file));
400 return(sstrdup(file));
403 /* Mostly copied from callcgi. */
404 static void mkcgienv(struct hthead *req, struct charbuf *dst)
407 char *url, *pi, *tmp, *qp, *h, *p;
409 bufaddenv(dst, "SERVER_SOFTWARE", "ashd/%s", VERSION);
410 bufaddenv(dst, "GATEWAY_INTERFACE", "CGI/1.1");
411 bufaddenv(dst, "SCGI", "1");
412 bufaddenv(dst, "SERVER_PROTOCOL", "%s", req->ver);
413 bufaddenv(dst, "REQUEST_METHOD", "%s", req->method);
414 bufaddenv(dst, "REQUEST_URI", "%s", req->url);
415 url = sstrdup(req->url);
416 if((qp = strchr(url, '?')) != NULL)
418 /* XXX: This is an ugly hack (I think), but though I can think of
419 * several alternatives, none seem to be better. */
420 if(*req->rest && (strlen(url) >= strlen(req->rest)) &&
421 !strcmp(req->rest, url + strlen(url) - strlen(req->rest))) {
422 url[strlen(url) - strlen(req->rest)] = 0;
424 if((pi = unquoteurl(req->rest)) == NULL)
425 pi = sstrdup(req->rest);
426 if(!strcmp(url, "/")) {
427 /* This seems to be normal CGI behavior, but see callcgi.c for
430 pi = sprintf2("/%s", tmp = pi);
433 bufaddenv(dst, "PATH_INFO", pi);
434 bufaddenv(dst, "SCRIPT_NAME", url);
435 bufaddenv(dst, "QUERY_STRING", "%s", qp?qp:"");
438 if((h = getheader(req, "Host")) != NULL)
439 bufaddenv(dst, "SERVER_NAME", "%s", h);
440 if((h = getheader(req, "X-Ash-Server-Address")) != NULL)
441 bufaddenv(dst, "SERVER_ADDR", "%s", h);
442 if((h = getheader(req, "X-Ash-Server-Port")) != NULL)
443 bufaddenv(dst, "SERVER_PORT", "%s", h);
444 if((h = getheader(req, "X-Ash-Remote-User")) != NULL)
445 bufaddenv(dst, "REMOTE_USER", "%s", h);
446 if(((h = getheader(req, "X-Ash-Protocol")) != NULL) && !strcmp(h, "https"))
447 bufaddenv(dst, "HTTPS", "on");
448 if((h = getheader(req, "X-Ash-Address")) != NULL)
449 bufaddenv(dst, "REMOTE_ADDR", "%s", h);
450 if((h = getheader(req, "X-Ash-Port")) != NULL)
451 bufaddenv(dst, "REMOTE_PORT", "%s", h);
452 if((h = getheader(req, "Content-Type")) != NULL)
453 bufaddenv(dst, "CONTENT_TYPE", "%s", h);
454 if((h = getheader(req, "Content-Length")) != NULL)
455 bufaddenv(dst, "CONTENT_LENGTH", "%s", h);
457 bufaddenv(dst, "CONTENT_LENGTH", "0");
458 if((h = getheader(req, "X-Ash-File")) != NULL) {
460 bufaddenv(dst, "SCRIPT_FILENAME", "%s", h);
463 for(i = 0; i < req->noheaders; i++) {
464 h = sprintf2("HTTP_%s", req->headers[i][0]);
465 for(p = h; *p; p++) {
473 bufcatstr2(*dst, req->headers[i][1]);
477 static struct hthead *parseresp(FILE *in)
483 resp->ver = sstrdup("HTTP/1.1");
484 if(parseheaders(resp, in)) {
488 if((st = getheader(resp, "Status")) != NULL) {
489 if((p = strchr(st, ' ')) != NULL) {
491 resp->code = atoi(st);
492 resp->msg = sstrdup(p);
494 resp->code = atoi(st);
495 resp->msg = sstrdup(httpdefstatus(resp->code));
497 headrmheader(resp, "Status");
498 } else if(getheader(resp, "Location")) {
500 resp->msg = sstrdup("See Other");
503 resp->msg = sstrdup("OK");
508 static void serve(struct muth *muth, va_list args)
510 vavar(struct hthead *, req);
518 is = mtstdopen(fd, 1, 60, "r+");
519 os = mtstdopen(sfd, 1, 600, "r+");
522 mkcgienv(req, &head);
523 fprintf(os, "%zi:", head.d);
524 fwrite(head.b, head.d, 1, os);
527 if(passdata(is, os) < 0)
530 if((resp = parseresp(os)) == NULL)
535 if(passdata(os, is) < 0)
544 static void listenloop(struct muth *muth, va_list args)
551 block(0, EV_READ, 0);
552 if((fd = recvreq(lfd, &req)) < 0) {
554 flog(LOG_ERR, "recvreq: %s", strerror(errno));
557 mustart(serve, req, fd);
561 static void sigign(int sig)
565 static void sigexit(int sig)
567 shutdown(0, SHUT_RDWR);
570 static void usage(FILE *out)
572 fprintf(out, "usage: callscgi [-h] [-N RETRIES] [-i ID] [-u UNIX-PATH] [-t [HOST:]TCP-PORT] [PROGRAM [ARGS...]]\n");
575 int main(int argc, char **argv)
579 while((c = getopt(argc, argv, "+hN:i:u:t:")) >= 0) {
585 nolisten = atoi(optarg);
601 progspec = argv + optind;
602 if(((sockid != NULL) + (unspec != NULL) + (inspec != NULL)) > 1) {
603 flog(LOG_ERR, "callscgi: at most one of -i, -u or -t may be given");
606 signal(SIGCHLD, SIG_IGN);
607 signal(SIGPIPE, sigign);
608 signal(SIGINT, sigexit);
609 signal(SIGTERM, sigexit);
610 mustart(listenloop, 0);