Commit | Line | Data |
---|---|---|
5c2040da FT |
1 | /* |
2 | ashd - A Sane HTTP Daemon | |
3 | Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com> | |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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/>. | |
17 | */ | |
18 | ||
19 | /* | |
20 | * XXX: This program is mostly copied from callscgi. It may be | |
21 | * reasonable to unify some of their shared code in a source file. | |
22 | */ | |
23 | ||
24 | /* | |
25 | * XXX: All the various ways to start a child process makes this | |
26 | * program quite ugly at the moment. It is unclear whether it is | |
27 | * meaningfully possible to unify them better than they currently are. | |
28 | */ | |
29 | ||
30 | #include <stdlib.h> | |
31 | #include <stdio.h> | |
32 | #include <unistd.h> | |
33 | #include <string.h> | |
34 | #include <fcntl.h> | |
35 | #include <ctype.h> | |
36 | #include <sys/socket.h> | |
37 | #include <sys/un.h> | |
38 | #include <netinet/in.h> | |
39 | #include <netdb.h> | |
40 | #include <sys/signal.h> | |
41 | #include <errno.h> | |
42 | ||
43 | #ifdef HAVE_CONFIG_H | |
44 | #include <config.h> | |
45 | #endif | |
46 | #include <utils.h> | |
47 | #include <req.h> | |
48 | #include <log.h> | |
49 | #include <mt.h> | |
50 | #include <mtio.h> | |
51 | ||
52 | #define FCGI_BEGIN_REQUEST 1 | |
53 | #define FCGI_ABORT_REQUEST 2 | |
54 | #define FCGI_END_REQUEST 3 | |
55 | #define FCGI_PARAMS 4 | |
56 | #define FCGI_STDIN 5 | |
57 | #define FCGI_STDOUT 6 | |
58 | #define FCGI_STDERR 7 | |
59 | ||
60 | static char **progspec; | |
61 | static char *sockid, *unspec, *inspec; | |
62 | static int nolisten; | |
63 | static struct sockaddr *curaddr; | |
64 | static size_t caddrlen; | |
65 | static int cafamily, isanon; | |
66 | static pid_t child; | |
67 | ||
68 | static struct addrinfo *resolv(int flags) | |
69 | { | |
70 | int ret; | |
71 | struct addrinfo *ai, h; | |
72 | char *name, *srv, *p; | |
73 | ||
74 | if((p = strchr(inspec, ':')) != NULL) { | |
75 | name = smalloc(p - inspec + 1); | |
76 | memcpy(name, inspec, p - inspec); | |
77 | name[p - inspec] = 0; | |
78 | srv = p + 1; | |
79 | } else { | |
80 | name = sstrdup("localhost"); | |
81 | srv = inspec; | |
82 | } | |
83 | memset(&h, 0, sizeof(h)); | |
84 | h.ai_family = AF_UNSPEC; | |
85 | h.ai_socktype = SOCK_STREAM; | |
86 | h.ai_flags = flags; | |
87 | ret = getaddrinfo(name, srv, &h, &ai); | |
88 | free(name); | |
89 | if(ret != 0) { | |
90 | flog(LOG_ERR, "could not resolve TCP specification `%s': %s", inspec, gai_strerror(ret)); | |
91 | exit(1); | |
92 | } | |
93 | return(ai); | |
94 | } | |
95 | ||
96 | static char *mksockid(char *sockid) | |
97 | { | |
98 | char *home; | |
99 | ||
100 | home = getenv("HOME"); | |
101 | if(home && !access(sprintf3("%s/.ashd/sockets/", home), X_OK)) | |
102 | return(sprintf3("%s/.ashd/sockets/fcgi-p-%s", home, sockid)); | |
103 | return(sprintf3("/tmp/fcgi-%i-%s", getuid(), sockid)); | |
104 | } | |
105 | ||
106 | static char *mkanonid(void) | |
107 | { | |
108 | char *home; | |
109 | char *tmpl; | |
110 | int fd; | |
111 | ||
112 | home = getenv("HOME"); | |
113 | if(home && !access(sprintf3("%s/.ashd/sockets/", home), X_OK)) | |
114 | tmpl = sprintf2("%s/.ashd/sockets/fcgi-a-XXXXXX", home); | |
115 | else | |
116 | tmpl = sprintf2("/tmp/fcgi-a-%i-XXXXXX", getuid()); | |
117 | if((fd = mkstemp(tmpl)) < 0) { | |
118 | flog(LOG_ERR, "could not create anonymous socket `%s': %s", tmpl, strerror(errno)); | |
119 | exit(1); | |
120 | } | |
121 | close(fd); | |
122 | unlink(tmpl); | |
123 | return(tmpl); | |
124 | } | |
125 | ||
126 | static void startlisten(void) | |
127 | { | |
128 | int i, fd; | |
129 | struct addrinfo *ai, *cai; | |
130 | char *unpath; | |
131 | struct sockaddr_un unm; | |
132 | char *aname; | |
133 | ||
134 | isanon = 0; | |
135 | if(inspec != NULL) { | |
136 | fd = -1; | |
137 | for(cai = ai = resolv(AI_PASSIVE); cai != NULL; cai = cai->ai_next) { | |
138 | if((fd = socket(cai->ai_family, cai->ai_socktype, cai->ai_protocol)) < 0) | |
139 | continue; | |
140 | if(bind(fd, cai->ai_addr, cai->ai_addrlen)) { | |
141 | close(fd); | |
142 | fd = -1; | |
143 | continue; | |
144 | } | |
145 | if(listen(fd, 128)) { | |
146 | close(fd); | |
147 | fd = -1; | |
148 | continue; | |
149 | } | |
150 | break; | |
151 | } | |
152 | freeaddrinfo(ai); | |
153 | if(fd < 0) { | |
154 | flog(LOG_ERR, "could not bind to specified TCP address: %s", strerror(errno)); | |
155 | exit(1); | |
156 | } | |
157 | } else if((unspec != NULL) || (sockid != NULL)) { | |
158 | if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { | |
159 | flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno)); | |
160 | exit(1); | |
161 | } | |
162 | if(unspec != NULL) | |
163 | unpath = unspec; | |
164 | else | |
165 | unpath = mksockid(sockid); | |
166 | unlink(unpath); | |
167 | unm.sun_family = AF_UNIX; | |
168 | strcpy(unm.sun_path, unpath); | |
169 | if(bind(fd, (struct sockaddr *)&unm, sizeof(unm))) { | |
170 | flog(LOG_ERR, "could not bind Unix socket to `%s': %s", unspec, strerror(errno)); | |
171 | exit(1); | |
172 | } | |
173 | if(listen(fd, 128)) { | |
174 | flog(LOG_ERR, "listen: %s", strerror(errno)); | |
175 | exit(1); | |
176 | } | |
177 | } else { | |
178 | if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { | |
179 | flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno)); | |
180 | exit(1); | |
181 | } | |
182 | memset(&unm, 0, sizeof(unm)); | |
183 | aname = mkanonid(); | |
184 | unm.sun_family = AF_UNIX; | |
185 | strcpy(unm.sun_path, aname); | |
186 | free(aname); | |
187 | if(bind(fd, (struct sockaddr *)&unm, sizeof(unm))) { | |
188 | flog(LOG_ERR, "could not bind Unix socket to `%s': %s", unspec, strerror(errno)); | |
189 | exit(1); | |
190 | } | |
191 | if(listen(fd, 128)) { | |
192 | flog(LOG_ERR, "listen: %s", strerror(errno)); | |
193 | exit(1); | |
194 | } | |
195 | ||
196 | curaddr = smalloc(caddrlen = sizeof(unm)); | |
197 | memcpy(curaddr, &unm, sizeof(unm)); | |
198 | cafamily = AF_UNIX; | |
199 | isanon = 1; | |
200 | } | |
201 | if((child = fork()) < 0) { | |
202 | flog(LOG_ERR, "could not fork: %s", strerror(errno)); | |
203 | exit(1); | |
204 | } | |
205 | if(child == 0) { | |
206 | dup2(fd, 0); | |
207 | for(i = 3; i < FD_SETSIZE; i++) | |
208 | close(i); | |
209 | execvp(*progspec, progspec); | |
210 | flog(LOG_ERR, "callfcgi: %s: %s", *progspec, strerror(errno)); | |
211 | exit(127); | |
212 | } | |
213 | close(fd); | |
214 | } | |
215 | ||
216 | static void startnolisten(void) | |
217 | { | |
218 | int i, fd; | |
219 | ||
220 | if((child = fork()) < 0) { | |
221 | flog(LOG_ERR, "could not fork: %s", strerror(errno)); | |
222 | exit(1); | |
223 | } | |
224 | if(child == 0) { | |
225 | for(i = 3; i < FD_SETSIZE; i++) | |
226 | close(i); | |
227 | if((fd = open("/dev/null", O_RDONLY)) < 0) { | |
228 | flog(LOG_ERR, "/dev/null: %s", strerror(errno)); | |
229 | exit(127); | |
230 | } | |
231 | dup2(fd, 0); | |
232 | close(fd); | |
233 | execvp(*progspec, progspec); | |
234 | flog(LOG_ERR, "callfcgi: %s: %s", *progspec, strerror(errno)); | |
235 | exit(127); | |
236 | } | |
237 | } | |
238 | ||
239 | static int sconnect(void) | |
240 | { | |
241 | int fd; | |
242 | int err; | |
243 | socklen_t errlen; | |
244 | ||
245 | fd = socket(cafamily, SOCK_STREAM, 0); | |
246 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); | |
247 | while(1) { | |
248 | if(connect(fd, curaddr, caddrlen)) { | |
249 | if(errno == EINPROGRESS) { | |
250 | block(fd, EV_WRITE, 30); | |
251 | errlen = sizeof(err); | |
252 | if(getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) || ((errno = err) != 0)) { | |
253 | close(fd); | |
254 | return(-1); | |
255 | } | |
256 | return(fd); | |
257 | } | |
258 | close(fd); | |
259 | return(-1); | |
260 | } | |
261 | return(fd); | |
262 | } | |
263 | } | |
264 | ||
265 | static int econnect(void) | |
266 | { | |
267 | int fd; | |
268 | struct addrinfo *ai, *cai; | |
269 | int tries; | |
270 | char *unpath; | |
271 | struct sockaddr_un unm; | |
272 | ||
273 | tries = 0; | |
274 | retry: | |
275 | if(inspec != NULL) { | |
276 | fd = -1; | |
277 | for(cai = ai = resolv(0); cai != NULL; cai = cai->ai_next) { | |
278 | if((fd = socket(cai->ai_family, cai->ai_socktype, cai->ai_protocol)) < 0) | |
279 | continue; | |
280 | if(connect(fd, cai->ai_addr, cai->ai_addrlen)) { | |
281 | close(fd); | |
282 | fd = -1; | |
283 | continue; | |
284 | } | |
285 | break; | |
286 | } | |
287 | if(fd < 0) { | |
288 | if(tries++ < nolisten) { | |
289 | sleep(1); | |
290 | goto retry; | |
291 | } | |
292 | flog(LOG_ERR, "could not connect to specified TCP address: %s", strerror(errno)); | |
293 | exit(1); | |
294 | } | |
295 | curaddr = smalloc(caddrlen = cai->ai_addrlen); | |
296 | memcpy(curaddr, cai->ai_addr, caddrlen); | |
297 | cafamily = cai->ai_family; | |
298 | isanon = 0; | |
299 | freeaddrinfo(ai); | |
300 | return(fd); | |
301 | } else if((unspec != NULL) || (sockid != NULL)) { | |
302 | if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { | |
303 | flog(LOG_ERR, "could not create Unix socket: %s", strerror(errno)); | |
304 | exit(1); | |
305 | } | |
306 | if(unspec != NULL) | |
307 | unpath = unspec; | |
308 | else | |
309 | unpath = mksockid(sockid); | |
310 | unlink(unpath); | |
311 | unm.sun_family = AF_UNIX; | |
312 | strcpy(unm.sun_path, unpath); | |
313 | if(connect(fd, (struct sockaddr *)&unm, sizeof(unm))) { | |
314 | close(fd); | |
315 | if(tries++ < nolisten) { | |
316 | sleep(1); | |
317 | goto retry; | |
318 | } | |
319 | flog(LOG_ERR, "could not connect to Unix socket `%s': %s", unspec, strerror(errno)); | |
320 | exit(1); | |
321 | } | |
322 | curaddr = smalloc(caddrlen = sizeof(unm)); | |
323 | memcpy(curaddr, &unm, sizeof(unm)); | |
324 | cafamily = AF_UNIX; | |
325 | isanon = 0; | |
326 | return(fd); | |
327 | } else { | |
328 | flog(LOG_ERR, "callfcgi: cannot use an anonymous socket without a program to start"); | |
329 | exit(1); | |
330 | } | |
331 | } | |
332 | ||
333 | static int startconn(void) | |
334 | { | |
335 | if(*progspec) { | |
336 | if(nolisten == 0) | |
337 | startlisten(); | |
338 | else | |
339 | startnolisten(); | |
340 | } | |
341 | if(curaddr != NULL) | |
342 | return(sconnect()); | |
343 | return(econnect()); | |
344 | } | |
345 | ||
346 | static void killcuraddr(void) | |
347 | { | |
348 | if(curaddr == NULL) | |
349 | return; | |
350 | if(isanon) { | |
351 | unlink(((struct sockaddr_un *)curaddr)->sun_path); | |
352 | if(child > 0) | |
353 | kill(child, SIGTERM); | |
354 | } | |
355 | free(curaddr); | |
356 | curaddr = NULL; | |
357 | } | |
358 | ||
359 | static int reconn(void) | |
360 | { | |
361 | int fd; | |
362 | ||
363 | if(curaddr != NULL) { | |
364 | if((fd = sconnect()) >= 0) | |
365 | return(fd); | |
366 | killcuraddr(); | |
367 | } | |
368 | return(startconn()); | |
369 | } | |
370 | ||
371 | static off_t passdata(FILE *in, FILE *out) | |
372 | { | |
373 | size_t read; | |
374 | off_t total; | |
375 | char buf[8192]; | |
376 | ||
377 | total = 0; | |
378 | while(!feof(in)) { | |
379 | read = fread(buf, 1, sizeof(buf), in); | |
380 | if(ferror(in)) | |
381 | return(-1); | |
382 | if(fwrite(buf, 1, read, out) != read) | |
383 | return(-1); | |
384 | total += read; | |
385 | } | |
386 | return(total); | |
387 | } | |
388 | ||
389 | static void bufcatkv(struct charbuf *dst, char *key, char *val) | |
390 | { | |
391 | size_t kl, vl; | |
392 | ||
393 | if((kl = strlen(key)) < 128) { | |
394 | bufadd(*dst, kl); | |
395 | } else { | |
396 | bufadd(*dst, ((kl & 0x7f000000) >> 24) | 0x80); | |
397 | bufadd(*dst, (kl & 0x00ff0000) >> 16); | |
398 | bufadd(*dst, (kl & 0x0000ff00) >> 8); | |
399 | bufadd(*dst, kl & 0x000000ff); | |
400 | } | |
401 | if((vl = strlen(val)) < 128) { | |
402 | bufadd(*dst, vl); | |
403 | } else { | |
404 | bufadd(*dst, ((vl & 0x7f000000) >> 24) | 0x80); | |
405 | bufadd(*dst, (vl & 0x00ff0000) >> 16); | |
406 | bufadd(*dst, (vl & 0x0000ff00) >> 8); | |
407 | bufadd(*dst, vl & 0x000000ff); | |
408 | } | |
409 | bufcat(*dst, key, kl); | |
410 | bufcat(*dst, val, vl); | |
411 | } | |
412 | ||
413 | static void bufaddenv(struct charbuf *dst, char *name, char *fmt, ...) | |
414 | { | |
415 | va_list args; | |
416 | char *val = NULL; | |
417 | ||
418 | va_start(args, fmt); | |
419 | val = vsprintf2(fmt, args); | |
420 | va_end(args); | |
421 | bufcatkv(dst, name, val); | |
422 | free(val); | |
423 | } | |
424 | ||
425 | static char *absolutify(char *file) | |
426 | { | |
427 | static int inited = 0; | |
428 | static char cwd[1024]; | |
429 | ||
430 | if(*file != '/') { | |
431 | if(!inited) { | |
432 | getcwd(cwd, sizeof(cwd)); | |
433 | inited = 1; | |
434 | } | |
435 | return(sprintf2("%s/%s", cwd, file)); | |
436 | } | |
437 | return(sstrdup(file)); | |
438 | } | |
439 | ||
440 | /* Mostly copied from callcgi. */ | |
441 | static void mkcgienv(struct hthead *req, struct charbuf *dst) | |
442 | { | |
443 | int i; | |
444 | char *url, *qp, *h, *p; | |
445 | ||
446 | bufaddenv(dst, "SERVER_SOFTWARE", "ashd/%s", VERSION); | |
447 | bufaddenv(dst, "GATEWAY_INTERFACE", "CGI/1.1"); | |
448 | bufaddenv(dst, "SERVER_PROTOCOL", "%s", req->ver); | |
449 | bufaddenv(dst, "REQUEST_METHOD", "%s", req->method); | |
450 | bufaddenv(dst, "REQUEST_URI", "%s", req->url); | |
451 | bufaddenv(dst, "PATH_INFO", req->rest); | |
452 | url = sstrdup(req->url); | |
453 | if((qp = strchr(url, '?')) != NULL) | |
454 | *(qp++) = 0; | |
455 | /* XXX: This is an ugly hack (I think), but though I can think of | |
456 | * several alternatives, none seem to be better. */ | |
457 | if(*req->rest && (strlen(url) >= strlen(req->rest)) && | |
458 | !strcmp(req->rest, url + strlen(url) - strlen(req->rest))) { | |
459 | bufaddenv(dst, "SCRIPT_NAME", "%.*s", (int)(strlen(url) - strlen(req->rest)), url); | |
460 | } else { | |
461 | bufaddenv(dst, "SCRIPT_NAME", "%s", url); | |
462 | } | |
463 | bufaddenv(dst, "QUERY_STRING", "%s", qp?qp:""); | |
464 | if((h = getheader(req, "Host")) != NULL) | |
465 | bufaddenv(dst, "SERVER_NAME", "%s", h); | |
466 | if((h = getheader(req, "X-Ash-Server-Port")) != NULL) | |
467 | bufaddenv(dst, "SERVER_PORT", "%s", h); | |
468 | if(((h = getheader(req, "X-Ash-Protocol")) != NULL) && !strcmp(h, "https")) | |
469 | bufaddenv(dst, "HTTPS", "on"); | |
470 | if((h = getheader(req, "X-Ash-Address")) != NULL) | |
471 | bufaddenv(dst, "REMOTE_ADDR", "%s", h); | |
472 | if((h = getheader(req, "Content-Type")) != NULL) | |
473 | bufaddenv(dst, "CONTENT_TYPE", "%s", h); | |
474 | if((h = getheader(req, "Content-Length")) != NULL) | |
475 | bufaddenv(dst, "CONTENT_LENGTH", "%s", h); | |
476 | else | |
477 | bufaddenv(dst, "CONTENT_LENGTH", "0"); | |
478 | if((h = getheader(req, "X-Ash-File")) != NULL) | |
479 | bufaddenv(dst, "SCRIPT_FILENAME", "%s", absolutify(h)); | |
480 | for(i = 0; i < req->noheaders; i++) { | |
481 | h = sprintf2("HTTP_%s", req->headers[i][0]); | |
482 | for(p = h; *p; p++) { | |
483 | if(isalnum(*p)) | |
484 | *p = toupper(*p); | |
485 | else | |
486 | *p = '_'; | |
487 | } | |
488 | bufcatkv(dst, h, req->headers[i][1]); | |
489 | free(h); | |
490 | } | |
491 | } | |
492 | ||
493 | static char *defstatus(int code) | |
494 | { | |
495 | if(code == 200) | |
496 | return("OK"); | |
497 | else if(code == 201) | |
498 | return("Created"); | |
499 | else if(code == 202) | |
500 | return("Accepted"); | |
501 | else if(code == 204) | |
502 | return("No Content"); | |
503 | else if(code == 300) | |
504 | return("Multiple Choices"); | |
505 | else if(code == 301) | |
506 | return("Moved Permanently"); | |
507 | else if(code == 302) | |
508 | return("Found"); | |
509 | else if(code == 303) | |
510 | return("See Other"); | |
511 | else if(code == 304) | |
512 | return("Not Modified"); | |
513 | else if(code == 307) | |
514 | return("Moved Temporarily"); | |
515 | else if(code == 400) | |
516 | return("Bad Request"); | |
517 | else if(code == 401) | |
518 | return("Unauthorized"); | |
519 | else if(code == 403) | |
520 | return("Forbidden"); | |
521 | else if(code == 404) | |
522 | return("Not Found"); | |
523 | else if(code == 500) | |
524 | return("Internal Server Error"); | |
525 | else if(code == 501) | |
526 | return("Not Implemented"); | |
527 | else if(code == 503) | |
528 | return("Service Unavailable"); | |
529 | else | |
530 | return("Unknown status"); | |
531 | } | |
532 | ||
533 | static struct hthead *parseresp(FILE *in) | |
534 | { | |
535 | struct hthead *resp; | |
536 | char *st, *p; | |
537 | ||
538 | omalloc(resp); | |
539 | resp->ver = sstrdup("HTTP/1.1"); | |
540 | if(parseheaders(resp, in)) { | |
541 | freehthead(resp); | |
542 | return(NULL); | |
543 | } | |
544 | if((st = getheader(resp, "Status")) != NULL) { | |
545 | if((p = strchr(st, ' ')) != NULL) { | |
546 | *(p++) = 0; | |
547 | resp->code = atoi(st); | |
548 | resp->msg = sstrdup(p); | |
549 | } else { | |
550 | resp->code = atoi(st); | |
551 | resp->msg = sstrdup(defstatus(resp->code)); | |
552 | } | |
553 | headrmheader(resp, "Status"); | |
554 | } else if(getheader(resp, "Location")) { | |
555 | resp->code = 303; | |
556 | resp->msg = sstrdup("See Other"); | |
557 | } else { | |
558 | resp->code = 200; | |
559 | resp->msg = sstrdup("OK"); | |
560 | } | |
561 | return(resp); | |
562 | } | |
563 | ||
564 | #define fputc2(b, f) if(fputc((b), (f)) == EOF) return(-1); | |
565 | ||
566 | static int sendrec(FILE *out, int type, int rid, char *data, size_t dlen) | |
567 | { | |
568 | off_t off; | |
569 | size_t cl; | |
570 | int p; | |
571 | ||
572 | off = 0; | |
573 | do { | |
574 | cl = min(dlen - off, 65535); | |
575 | p = (8 - (cl % 8)) % 8; | |
576 | fputc2(1, out); | |
577 | fputc2(type, out); | |
578 | fputc2((rid & 0xff00) >> 8, out); | |
579 | fputc2(rid & 0x00ff, out); | |
580 | fputc2((cl & 0xff00) >> 8, out); | |
581 | fputc2(cl & 0x00ff, out); | |
582 | fputc2(p, out); | |
583 | fputc2(0, out); | |
584 | if(fwrite(data + off, 1, cl, out) != cl) | |
585 | return(-1); | |
586 | for(; p > 0; p--) | |
587 | fputc2(0, out); | |
588 | } while((off += cl) < dlen); | |
589 | return(0); | |
590 | } | |
591 | ||
592 | #define fgetc2(f) ({int __c__ = fgetc(f); if(__c__ == EOF) return(-1); __c__;}) | |
593 | ||
594 | static int recvrec(FILE *in, int *type, int *rid, char **data, size_t *dlen) | |
595 | { | |
596 | int b1, b2, pl; | |
597 | ||
598 | if(fgetc2(in) != 1) | |
599 | return(-1); | |
600 | *type = fgetc2(in); | |
601 | b1 = fgetc2(in); | |
602 | b2 = fgetc2(in); | |
603 | *rid = (b1 << 8) | b2; | |
604 | b1 = fgetc2(in); | |
605 | b2 = fgetc2(in); | |
606 | *dlen = (b1 << 8) | b2; | |
607 | pl = fgetc2(in); | |
608 | if(fgetc2(in) != 0) | |
609 | return(-1); | |
610 | *data = smalloc(max(*dlen, 1)); | |
611 | if(fread(*data, 1, *dlen, in) != *dlen) { | |
612 | free(data); | |
613 | return(-1); | |
614 | } | |
615 | for(; pl > 0; pl--) { | |
616 | if(fgetc(in) == EOF) { | |
617 | free(data); | |
618 | return(-1); | |
619 | } | |
620 | } | |
621 | return(0); | |
622 | } | |
623 | ||
624 | static int begreq(FILE *out, int rid) | |
625 | { | |
626 | char rec[] = {0, 1, 0, 0, 0, 0, 0, 0}; | |
627 | ||
628 | return(sendrec(out, FCGI_BEGIN_REQUEST, rid, rec, 8)); | |
629 | } | |
630 | ||
631 | static void mtiopipe(FILE **read, FILE **write) | |
632 | { | |
633 | int fds[2]; | |
634 | ||
635 | pipe(fds); | |
636 | *read = mtstdopen(fds[0], 0, 600, "r"); | |
637 | *write = mtstdopen(fds[1], 0, 600, "w"); | |
638 | } | |
639 | ||
640 | static void outplex(struct muth *muth, va_list args) | |
641 | { | |
642 | vavar(FILE *, sk); | |
643 | struct { | |
644 | struct ch { | |
645 | FILE *s; | |
646 | int id; | |
647 | } *b; | |
648 | size_t s, d; | |
649 | } outs; | |
650 | int i; | |
651 | struct ch ch; | |
652 | int type, rid; | |
653 | char *data; | |
654 | size_t dlen; | |
655 | ||
656 | bufinit(outs); | |
657 | while((ch.s = va_arg(args, FILE *)) != NULL) { | |
658 | ch.id = va_arg(args, int); | |
659 | bufadd(outs, ch); | |
660 | } | |
661 | data = NULL; | |
662 | while(1) { | |
663 | if(recvrec(sk, &type, &rid, &data, &dlen)) | |
664 | goto out; | |
665 | if(rid != 1) | |
666 | goto out; | |
667 | for(i = 0; i < outs.d; i++) { | |
668 | if(outs.b[i].id == type) { | |
669 | if(outs.b[i].s != NULL) { | |
670 | if(dlen == 0) { | |
671 | fclose(outs.b[i].s); | |
672 | outs.b[i].s = NULL; | |
673 | } else { | |
674 | if(fwrite(data, 1, dlen, outs.b[i].s) != dlen) | |
675 | goto out; | |
676 | } | |
677 | } | |
678 | break; | |
679 | } | |
680 | } | |
681 | free(data); | |
682 | data = NULL; | |
683 | } | |
684 | ||
685 | out: | |
686 | if(data != NULL) | |
687 | free(data); | |
688 | for(i = 0; i < outs.d; i++) { | |
689 | if(outs.b[i].s != NULL) | |
690 | fclose(outs.b[i].s); | |
691 | } | |
692 | buffree(outs); | |
693 | fclose(sk); | |
694 | } | |
695 | ||
696 | static void errhandler(struct muth *muth, va_list args) | |
697 | { | |
698 | vavar(FILE *, in); | |
699 | char buf[1024]; | |
700 | char *p; | |
701 | ||
702 | bufinit(buf); | |
703 | while(fgets(buf, sizeof(buf), in) != NULL) { | |
704 | p = buf + strlen(buf) - 1; | |
705 | while((p >= buf) && (*p == '\n')) | |
706 | *(p--) = 0; | |
707 | if(buf[0]) | |
708 | flog(LOG_INFO, "child said: %s", buf); | |
709 | } | |
710 | fclose(in); | |
711 | } | |
712 | ||
713 | static void serve(struct muth *muth, va_list args) | |
714 | { | |
715 | vavar(struct hthead *, req); | |
716 | vavar(int, fd); | |
717 | vavar(int, sfd); | |
718 | FILE *is, *os, *outi, *outo, *erri, *erro; | |
719 | struct charbuf head; | |
720 | struct hthead *resp; | |
721 | size_t read; | |
722 | char buf[8192]; | |
723 | ||
724 | sfd = reconn(); | |
725 | is = mtstdopen(fd, 1, 60, "r+"); | |
726 | os = mtstdopen(sfd, 1, 600, "r+"); | |
727 | ||
728 | outi = NULL; | |
729 | mtiopipe(&outi, &outo); mtiopipe(&erri, &erro); | |
730 | mustart(outplex, mtstdopen(dup(sfd), 1, 600, "r+"), outo, FCGI_STDOUT, erro, FCGI_STDERR, NULL); | |
731 | mustart(errhandler, erri); | |
732 | ||
733 | if(begreq(os, 1)) | |
734 | goto out; | |
735 | bufinit(head); | |
736 | mkcgienv(req, &head); | |
737 | if(sendrec(os, FCGI_PARAMS, 1, head.b, head.d)) | |
738 | goto out; | |
739 | if(sendrec(os, FCGI_PARAMS, 1, NULL, 0)) | |
740 | goto out; | |
741 | buffree(head); | |
742 | if(fflush(os)) | |
743 | goto out; | |
744 | ||
745 | while(!feof(is)) { | |
746 | read = fread(buf, 1, sizeof(buf), is); | |
747 | if(ferror(is)) | |
748 | goto out; | |
749 | if(sendrec(os, FCGI_STDIN, 1, buf, read)) | |
750 | goto out; | |
751 | } | |
752 | if(sendrec(os, FCGI_STDIN, 1, NULL, 0)) | |
753 | goto out; | |
754 | if(fflush(os)) | |
755 | goto out; | |
756 | ||
757 | if((resp = parseresp(outi)) == NULL) | |
758 | goto out; | |
759 | writeresp(is, resp); | |
760 | freehthead(resp); | |
761 | fputc('\n', is); | |
762 | if(passdata(outi, is) < 0) | |
763 | goto out; | |
764 | ||
765 | out: | |
766 | freehthead(req); | |
767 | buffree(head); | |
768 | shutdown(sfd, SHUT_RDWR); | |
769 | if(outi != NULL) | |
770 | fclose(outi); | |
771 | fclose(is); | |
772 | fclose(os); | |
773 | } | |
774 | ||
775 | static void listenloop(struct muth *muth, va_list args) | |
776 | { | |
777 | vavar(int, lfd); | |
778 | int fd; | |
779 | struct hthead *req; | |
780 | ||
781 | while(1) { | |
782 | block(0, EV_READ, 0); | |
783 | if((fd = recvreq(lfd, &req)) < 0) { | |
784 | if(errno != 0) | |
785 | flog(LOG_ERR, "recvreq: %s", strerror(errno)); | |
786 | break; | |
787 | } | |
788 | mustart(serve, req, fd); | |
789 | } | |
790 | } | |
791 | ||
792 | static void sigign(int sig) | |
793 | { | |
794 | } | |
795 | ||
796 | static void sigexit(int sig) | |
797 | { | |
798 | exit(0); | |
799 | } | |
800 | ||
801 | static void usage(FILE *out) | |
802 | { | |
803 | fprintf(out, "usage: callfcgi [-h] [-N RETRIES] [-i ID] [-u UNIX-PATH] [-t [HOST:]TCP-PORT] [PROGRAM [ARGS...]]\n"); | |
804 | } | |
805 | ||
806 | int main(int argc, char **argv) | |
807 | { | |
808 | int c; | |
809 | ||
810 | while((c = getopt(argc, argv, "+hN:i:u:t:")) >= 0) { | |
811 | switch(c) { | |
812 | case 'h': | |
813 | usage(stdout); | |
814 | exit(0); | |
815 | case 'N': | |
816 | nolisten = atoi(optarg); | |
817 | break; | |
818 | case 'i': | |
819 | sockid = optarg; | |
820 | break; | |
821 | case 'u': | |
822 | unspec = optarg; | |
823 | break; | |
824 | case 't': | |
825 | inspec = optarg; | |
826 | break; | |
827 | default: | |
828 | usage(stderr); | |
829 | exit(1); | |
830 | } | |
831 | } | |
832 | progspec = argv + optind; | |
833 | if(((sockid != NULL) + (unspec != NULL) + (inspec != NULL)) > 1) { | |
834 | flog(LOG_ERR, "callfcgi: at most one of -i, -u or -t may be given"); | |
835 | exit(1); | |
836 | } | |
837 | signal(SIGCHLD, SIG_IGN); | |
838 | signal(SIGPIPE, sigign); | |
839 | signal(SIGINT, sigexit); | |
840 | signal(SIGTERM, sigexit); | |
841 | mustart(listenloop, 0); | |
842 | atexit(killcuraddr); | |
843 | ioloop(); | |
844 | return(0); | |
845 | } |