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>
35 #include <sys/signal.h>
47 static char **progspec;
48 static char *sockid, *unspec, *inspec;
50 static struct sockaddr *curaddr;
51 static size_t caddrlen;
52 static int cafamily, isanon;
55 static struct addrinfo *resolv(int flags)
58 struct addrinfo *ai, h;
61 if((p = strchr(inspec, ':')) != NULL) {
62 name = smalloc(p - inspec + 1);
63 memcpy(name, inspec, p - inspec);
67 name = sstrdup("localhost");
70 memset(&h, 0, sizeof(h));
71 h.ai_family = AF_UNSPEC;
72 h.ai_socktype = SOCK_STREAM;
74 ret = getaddrinfo(name, srv, &h, &ai);
77 flog(LOG_ERR, "could not resolve TCP specification `%s': %s", inspec, gai_strerror(ret));
83 static char *mksockid(char *sockid)
87 home = getenv("HOME");
88 if(home && !access(sprintf3("%s/.ashd/sockets/", home), X_OK))
89 return(sprintf3("%s/.ashd/sockets/scgi-p-%s", home, sockid));
90 return(sprintf3("/tmp/scgi-%i-%s", getuid(), sockid));
93 static char *mkanonid(void)
99 home = getenv("HOME");
100 if(home && !access(sprintf3("%s/.ashd/sockets/", home), X_OK))
101 tmpl = sprintf2("%s/.ashd/sockets/scgi-a-XXXXXX");
103 tmpl = sprintf2("/tmp/scgi-a-%i-XXXXXX", getuid());
104 if((fd = mkstemp(tmpl)) < 0) {
105 flog(LOG_ERR, "could not create anonymous socket `%s': %s", tmpl, strerror(errno));
112 static void startlisten(void)
115 struct addrinfo *ai, *cai;
117 struct sockaddr_un unm;
123 for(cai = ai = resolv(AI_PASSIVE); cai != NULL; cai = cai->ai_next) {
124 if((fd = socket(cai->ai_family, cai->ai_socktype, cai->ai_protocol)) < 0)
126 if(bind(fd, cai->ai_addr, cai->ai_addrlen)) {
131 if(listen(fd, 128)) {
140 flog(LOG_ERR, "could not bind to specified TCP address: %s", strerror(errno));
143 } else if((unspec != NULL) || (sockid != NULL)) {
144 if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
145 flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno));
151 unpath = mksockid(sockid);
153 unm.sun_family = AF_UNIX;
154 strcpy(unm.sun_path, unpath);
155 if(bind(fd, (struct sockaddr *)&unm, sizeof(unm))) {
156 flog(LOG_ERR, "could not bind Unix socket to `%s': %s", unspec, strerror(errno));
159 if(listen(fd, 128)) {
160 flog(LOG_ERR, "listen: %s", strerror(errno));
164 if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
165 flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno));
168 memset(&unm, 0, sizeof(unm));
170 unm.sun_family = AF_UNIX;
171 strcpy(unm.sun_path, aname);
173 if(bind(fd, (struct sockaddr *)&unm, sizeof(unm))) {
174 flog(LOG_ERR, "could not bind Unix socket to `%s': %s", unspec, strerror(errno));
177 if(listen(fd, 128)) {
178 flog(LOG_ERR, "listen: %s", strerror(errno));
182 curaddr = smalloc(caddrlen = sizeof(unm));
183 memcpy(curaddr, &unm, sizeof(unm));
187 if((child = fork()) < 0) {
188 flog(LOG_ERR, "could not fork: %s", strerror(errno));
193 for(i = 3; i < FD_SETSIZE; i++)
195 execvp(*progspec, progspec);
201 static void startnolisten(void)
205 if((child = fork()) < 0) {
206 flog(LOG_ERR, "could not fork: %s", strerror(errno));
210 for(i = 3; i < FD_SETSIZE; i++)
212 if((fd = open("/dev/null", O_RDONLY)) < 0) {
213 flog(LOG_ERR, "/dev/null: %s", strerror(errno));
218 execvp(*progspec, progspec);
223 static int sconnect(void)
229 fd = socket(cafamily, SOCK_STREAM, 0);
230 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
232 if(connect(fd, curaddr, caddrlen)) {
233 if(errno == EINPROGRESS) {
234 block(fd, EV_WRITE, 30);
235 errlen = sizeof(err);
236 if(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) || ((errno = err) != 0)) {
249 static int econnect(void)
252 struct addrinfo *ai, *cai;
255 struct sockaddr_un unm;
261 for(cai = ai = resolv(0); cai != NULL; cai = cai->ai_next) {
262 if((fd = socket(cai->ai_family, cai->ai_socktype, cai->ai_protocol)) < 0)
264 if(connect(fd, cai->ai_addr, cai->ai_addrlen)) {
272 if(tries++ < nolisten) {
276 flog(LOG_ERR, "could not connect to specified TCP address: %s", strerror(errno));
279 curaddr = smalloc(caddrlen = cai->ai_addrlen);
280 memcpy(curaddr, cai->ai_addr, caddrlen);
281 cafamily = cai->ai_family;
285 } else if((unspec != NULL) || (sockid != NULL)) {
286 if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
287 flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno));
293 unpath = mksockid(sockid);
295 unm.sun_family = AF_UNIX;
296 strcpy(unm.sun_path, unpath);
297 if(connect(fd, (struct sockaddr *)&unm, sizeof(unm))) {
299 if(tries++ < nolisten) {
303 flog(LOG_ERR, "could not connect to Unix socket `%s': %s", unspec, strerror(errno));
306 curaddr = smalloc(caddrlen = sizeof(unm));
307 memcpy(curaddr, &unm, sizeof(unm));
312 flog(LOG_ERR, "servescgi: cannot use an anonymous socket without a program to start");
317 static int startconn(void)
330 static void killcuraddr(void)
335 unlink(((struct sockaddr_un *)curaddr)->sun_path);
337 kill(child, SIGTERM);
343 static int reconn(void)
347 if(curaddr != NULL) {
348 if((fd = sconnect()) >= 0)
355 static off_t passdata(FILE *in, FILE *out)
363 read = fread(buf, 1, sizeof(buf), in);
366 if(fwrite(buf, 1, read, out) != read)
373 static void bufaddenv(struct charbuf *dst, char *name, char *fmt, ...)
377 bufcatstr2(*dst, name);
379 bvprintf(dst, fmt, args);
384 static char *absolutify(char *file)
386 static int inited = 0;
387 static char cwd[1024];
391 getcwd(cwd, sizeof(cwd));
394 return(sprintf2("%s/%s", cwd, file));
396 return(sstrdup(file));
399 /* Mostly copied from callcgi. */
400 static void mkcgienv(struct hthead *req, struct charbuf *dst)
403 char *url, *qp, *h, *p;
405 bufaddenv(dst, "SERVER_SOFTWARE", "ashd/%s", VERSION);
406 bufaddenv(dst, "GATEWAY_INTERFACE", "CGI/1.1");
407 bufaddenv(dst, "SCGI", "1");
408 bufaddenv(dst, "SERVER_PROTOCOL", "%s", req->ver);
409 bufaddenv(dst, "REQUEST_METHOD", "%s", req->method);
410 bufaddenv(dst, "REQUEST_URI", "%s", req->url);
412 bufaddenv(dst, "PATH_INFO", "/%s", req->rest);
414 bufaddenv(dst, "PATH_INFO", "");
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) - 1] == '/')) {
423 bufaddenv(dst, "SCRIPT_NAME", "%.*s", (int)(strlen(url) - strlen(req->rest) - 1), url);
425 bufaddenv(dst, "SCRIPT_NAME", "%s", url);
427 bufaddenv(dst, "QUERY_STRING", "%s", qp?qp:"");
428 if((h = getheader(req, "Host")) != NULL)
429 bufaddenv(dst, "SERVER_NAME", "%s", h);
430 if((h = getheader(req, "X-Ash-Server-Port")) != NULL)
431 bufaddenv(dst, "SERVER_PORT", "%s", h);
432 if(((h = getheader(req, "X-Ash-Server-Protocol")) != NULL) && !strcmp(h, "https"))
433 bufaddenv(dst, "HTTPS", "on");
434 if((h = getheader(req, "X-Ash-Address")) != NULL)
435 bufaddenv(dst, "REMOTE_ADDR", "%s", h);
436 if((h = getheader(req, "Content-Type")) != NULL)
437 bufaddenv(dst, "CONTENT_TYPE", "%s", h);
438 if((h = getheader(req, "Content-Length")) != NULL)
439 bufaddenv(dst, "CONTENT_LENGTH", "%s", h);
441 bufaddenv(dst, "CONTENT_LENGTH", "0");
442 if((h = getheader(req, "X-Ash-File")) != NULL)
443 bufaddenv(dst, "SCRIPT_FILENAME", "%s", absolutify(h));
444 for(i = 0; i < req->noheaders; i++) {
445 h = sprintf2("HTTP_%s", req->headers[i][0]);
446 for(p = h; *p; p++) {
454 bufcatstr2(*dst, req->headers[i][1]);
458 static char *defstatus(int code)
467 return("No Content");
469 return("Multiple Choices");
471 return("Moved Permanently");
477 return("Not Modified");
479 return("Moved Temporarily");
481 return("Bad Request");
483 return("Unauthorized");
489 return("Internal Server Error");
491 return("Not Implemented");
493 return("Service Unavailable");
495 return("Unknown status");
498 static struct hthead *parseresp(FILE *in)
504 resp->ver = sstrdup("HTTP/1.1");
505 if(parseheaders(resp, in)) {
509 if((st = getheader(resp, "Status")) != NULL) {
510 if((p = strchr(st, ' ')) != NULL) {
512 resp->code = atoi(st);
513 resp->msg = sstrdup(p);
515 resp->code = atoi(st);
516 resp->msg = sstrdup(defstatus(resp->code));
518 headrmheader(resp, "Status");
519 } else if(getheader(resp, "Location")) {
521 resp->msg = sstrdup("See Other");
524 resp->msg = sstrdup("OK");
529 static void serve(struct muth *muth, va_list args)
531 vavar(struct hthead *, req);
539 is = mtstdopen(fd, 1, 60, "r+");
540 os = mtstdopen(sfd, 1, 600, "r+");
543 mkcgienv(req, &head);
544 fprintf(os, "%zi:", head.d);
545 fwrite(head.b, head.d, 1, os);
548 if(passdata(is, os) < 0)
551 if((resp = parseresp(os)) == NULL)
556 if(passdata(os, is) < 0)
565 static void listenloop(struct muth *muth, va_list args)
572 block(0, EV_READ, 0);
573 if((fd = recvreq(lfd, &req)) < 0) {
575 flog(LOG_ERR, "recvreq: %s", strerror(errno));
578 mustart(serve, req, fd);
582 static void usage(FILE *out)
584 fprintf(out, "usage: servescgi [-h] [-N RETRIES] [-i ID] [-u UNIX-PATH] [-t [HOST:]TCP-PORT] [PROGRAM [ARGS...]]\n");
587 int main(int argc, char **argv)
591 while((c = getopt(argc, argv, "+hN:i:u:t:")) >= 0) {
597 nolisten = atoi(optarg);
613 progspec = argv + optind;
614 if(((sockid != NULL) + (unspec != NULL) + (inspec != NULL)) > 1) {
615 flog(LOG_ERR, "servescgi: at most one of -i, -u or -t may be given");
618 signal(SIGCHLD, SIG_IGN);
619 mustart(listenloop, 0);