Blob


1 /*
2 * Copyright (c) 2020-2022 Tracey Emery <tracey@traceyemery.net>
3 * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
4 * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <arpa/inet.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 #include <sys/uio.h>
25 #include <errno.h>
26 #include <event.h>
27 #include <imsg.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
35 #include "got_error.h"
37 #include "got_compat.h"
39 #include "proc.h"
40 #include "gotwebd.h"
41 #include "tmpl.h"
43 size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
44 void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
45 uint16_t);
46 void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
47 int fcgi_send_response(struct request *, int, const void *, size_t);
49 void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
50 void dump_fcgi_begin_request_body(const char *,
51 struct fcgi_begin_request_body *);
52 void dump_fcgi_end_request_body(const char *,
53 struct fcgi_end_request_body *);
55 extern int cgi_inflight;
56 extern volatile int client_cnt;
58 void
59 fcgi_request(int fd, short events, void *arg)
60 {
61 struct request *c = arg;
62 ssize_t n;
63 size_t parsed = 0;
65 n = read(fd, c->buf + c->buf_pos + c->buf_len,
66 FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
68 switch (n) {
69 case -1:
70 switch (errno) {
71 case EINTR:
72 case EAGAIN:
73 return;
74 default:
75 goto fail;
76 }
77 break;
79 case 0:
80 log_debug("closed connection");
81 goto fail;
82 default:
83 break;
84 }
86 c->buf_len += n;
88 /*
89 * Parse the records as they are received. Per the FastCGI
90 * specification, the server need only receive the FastCGI
91 * parameter records in full; it is free to begin execution
92 * at that point, which is what happens here.
93 */
94 do {
95 parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
96 if (parsed != 0) {
97 c->buf_pos += parsed;
98 c->buf_len -= parsed;
99 }
100 } while (parsed > 0 && c->buf_len > 0);
102 /* Make space for further reads */
103 if (parsed != 0)
104 if (c->buf_len > 0) {
105 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
106 c->buf_pos = 0;
108 return;
109 fail:
110 fcgi_cleanup_request(c);
113 size_t
114 fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
116 struct fcgi_record_header *h;
118 if (n < sizeof(struct fcgi_record_header))
119 return 0;
121 h = (struct fcgi_record_header*) buf;
123 dump_fcgi_record("", h);
125 if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
126 + h->padding_len)
127 return 0;
129 if (h->version != 1)
130 log_warn("wrong version");
132 switch (h->type) {
133 case FCGI_BEGIN_REQUEST:
134 fcgi_parse_begin_request(buf +
135 sizeof(struct fcgi_record_header),
136 ntohs(h->content_len), c, ntohs(h->id));
137 break;
138 case FCGI_PARAMS:
139 fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
140 ntohs(h->content_len), c, ntohs(h->id));
141 break;
142 case FCGI_STDIN:
143 case FCGI_ABORT_REQUEST:
144 if (c->sock->client_status != CLIENT_DISCONNECT &&
145 c->outbuf_len != 0) {
146 fcgi_send_response(c, FCGI_STDOUT, c->outbuf,
147 c->outbuf_len);
150 fcgi_create_end_record(c);
151 fcgi_cleanup_request(c);
152 return 0;
153 default:
154 log_warn("unimplemented type %d", h->type);
155 break;
158 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
159 + h->padding_len);
162 void
163 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
164 struct request *c, uint16_t id)
166 /* XXX -- FCGI_CANT_MPX_CONN */
167 if (c->request_started) {
168 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
169 return;
172 if (n != sizeof(struct fcgi_begin_request_body)) {
173 log_warn("wrong size %d != %lu", n,
174 sizeof(struct fcgi_begin_request_body));
175 return;
178 c->request_started = 1;
179 c->id = id;
182 void
183 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
185 uint32_t name_len, val_len;
186 uint8_t *sd, *val;
188 if (!c->request_started) {
189 log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
190 return;
193 if (c->id != id) {
194 log_warn("unexpected id, ignoring");
195 return;
198 if (n == 0) {
199 gotweb_process_request(c);
200 return;
203 while (n > 0) {
204 if (buf[0] >> 7 == 0) {
205 name_len = buf[0];
206 n--;
207 buf++;
208 } else {
209 if (n > 3) {
210 name_len = ((buf[0] & 0x7f) << 24) +
211 (buf[1] << 16) + (buf[2] << 8) + buf[3];
212 n -= 4;
213 buf += 4;
214 } else
215 return;
218 if (n == 0)
219 return;
221 if (buf[0] >> 7 == 0) {
222 val_len = buf[0];
223 n--;
224 buf++;
225 } else {
226 if (n > 3) {
227 val_len = ((buf[0] & 0x7f) << 24) +
228 (buf[1] << 16) + (buf[2] << 8) +
229 buf[3];
230 n -= 4;
231 buf += 4;
232 } else
233 return;
236 if (n < name_len + val_len)
237 return;
239 val = buf + name_len;
241 if (c->querystring[0] == '\0' &&
242 val_len < MAX_QUERYSTRING &&
243 name_len == 12 &&
244 strncmp(buf, "QUERY_STRING", 12) == 0) {
245 memcpy(c->querystring, val, val_len);
246 c->querystring[val_len] = '\0';
249 if (c->http_host[0] == '\0' &&
250 val_len < GOTWEBD_MAXTEXT &&
251 name_len == 9 &&
252 strncmp(buf, "HTTP_HOST", 9) == 0) {
253 memcpy(c->http_host, val, val_len);
254 c->http_host[val_len] = '\0';
256 /*
257 * lazily get subdomain
258 * will only get domain if no subdomain exists
259 * this can still work if gotweb server name is the same
260 */
261 sd = strchr(c->http_host, '.');
262 if (sd)
263 *sd = '\0';
266 if (c->document_uri[0] == '\0' &&
267 val_len < MAX_DOCUMENT_URI &&
268 name_len == 12 &&
269 strncmp(buf, "DOCUMENT_URI", 12) == 0) {
270 memcpy(c->document_uri, val, val_len);
271 c->document_uri[val_len] = '\0';
274 if (c->server_name[0] == '\0' &&
275 val_len < MAX_SERVER_NAME &&
276 name_len == 11 &&
277 strncmp(buf, "SERVER_NAME", 11) == 0) {
278 memcpy(c->server_name, val, val_len);
279 c->server_name[val_len] = '\0';
282 if (name_len == 5 &&
283 strncmp(buf, "HTTPS", 5) == 0)
284 c->https = 1;
286 buf += name_len + val_len;
287 n -= name_len - val_len;
291 void
292 fcgi_timeout(int fd, short events, void *arg)
294 fcgi_cleanup_request((struct request*) arg);
297 int
298 fcgi_puts(struct template *tp, const char *str)
300 if (str == NULL)
301 return 0;
302 return fcgi_gen_binary_response(tp->tp_arg, str, strlen(str));
305 int
306 fcgi_putc(struct template *tp, int ch)
308 uint8_t c = ch;
309 return fcgi_gen_binary_response(tp->tp_arg, &c, 1);
312 int
313 fcgi_vprintf(struct request *c, const char *fmt, va_list ap)
315 char *str;
316 int r;
318 r = vasprintf(&str, fmt, ap);
319 if (r == -1) {
320 log_warn("%s: asprintf", __func__);
321 return -1;
324 r = fcgi_gen_binary_response(c, str, r);
325 free(str);
326 return r;
329 int
330 fcgi_printf(struct request *c, const char *fmt, ...)
332 va_list ap;
333 int r;
335 va_start(ap, fmt);
336 r = fcgi_vprintf(c, fmt, ap);
337 va_end(ap);
339 return r;
342 int
343 fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
345 int r;
347 if (c->sock->client_status == CLIENT_DISCONNECT)
348 return -1;
350 if (data == NULL || len == 0)
351 return 0;
353 /*
354 * special case: send big replies -like blobs- directly
355 * without copying.
356 */
357 if (len > sizeof(c->outbuf)) {
358 if (c->outbuf_len > 0) {
359 fcgi_send_response(c, FCGI_STDOUT,
360 c->outbuf, c->outbuf_len);
361 c->outbuf_len = 0;
363 return fcgi_send_response(c, FCGI_STDOUT, data, len);
366 if (len < sizeof(c->outbuf) - c->outbuf_len) {
367 memcpy(c->outbuf + c->outbuf_len, data, len);
368 c->outbuf_len += len;
369 return 0;
372 r = fcgi_send_response(c, FCGI_STDOUT, c->outbuf, c->outbuf_len);
373 if (r == -1)
374 return -1;
376 memcpy(c->outbuf, data, len);
377 c->outbuf_len = len;
378 return 0;
381 static int
382 send_response(struct request *c, int type, const uint8_t *data,
383 size_t len)
385 static const uint8_t padding[FCGI_PADDING_SIZE];
386 struct fcgi_record_header header;
387 struct iovec iov[3];
388 struct timespec ts;
389 ssize_t nw;
390 size_t padded_len, tot;
391 int i, err = 0, th = 2000;
393 ts.tv_sec = 0;
394 ts.tv_nsec = 50;
396 memset(&header, 0, sizeof(header));
397 header.version = 1;
398 header.type = type;
399 header.id = htons(c->id);
400 header.content_len = htons(len);
402 /* The FastCGI spec suggests to align the output buffer */
403 tot = sizeof(header) + len;
404 padded_len = FCGI_ALIGN(tot);
405 if (padded_len > tot) {
406 header.padding_len = padded_len - tot;
407 tot += header.padding_len;
410 iov[0].iov_base = &header;
411 iov[0].iov_len = sizeof(header);
413 iov[1].iov_base = (void *)data;
414 iov[1].iov_len = len;
416 iov[2].iov_base = (void *)padding;
417 iov[2].iov_len = header.padding_len;
419 dump_fcgi_record("resp ", &header);
421 /*
422 * XXX: add some simple write heuristics here
423 * On slower VMs, spotty connections, etc., we don't want to go right to
424 * disconnect. Let's at least try to write the data a few times before
425 * giving up.
426 */
427 while (tot > 0) {
428 nw = writev(c->fd, iov, nitems(iov));
429 if (nw == 0) {
430 c->sock->client_status = CLIENT_DISCONNECT;
431 break;
433 if (nw == -1) {
434 err++;
435 if (errno == EAGAIN && err < th) {
436 nanosleep(&ts, NULL);
437 continue;
439 log_warn("%s: write failure", __func__);
440 c->sock->client_status = CLIENT_DISCONNECT;
441 return -1;
444 if (nw != tot)
445 log_debug("%s: partial write: %zu vs %zu", __func__,
446 nw, tot);
448 tot -= nw;
449 for (i = 0; i < nitems(iov); ++i) {
450 if (nw < iov[i].iov_len) {
451 iov[i].iov_base += nw;
452 iov[i].iov_len -= nw;
453 break;
455 nw -= iov[i].iov_len;
456 iov[i].iov_len = 0;
460 return 0;
463 int
464 fcgi_send_response(struct request *c, int type, const void *data,
465 size_t len)
467 if (c->sock->client_status == CLIENT_DISCONNECT)
468 return -1;
470 while (len > FCGI_CONTENT_SIZE) {
471 if (send_response(c, type, data, len) == -1)
472 return -1;
474 data += FCGI_CONTENT_SIZE;
475 len -= FCGI_CONTENT_SIZE;
478 if (len == 0)
479 return 0;
481 return send_response(c, type, data, len);
484 void
485 fcgi_create_end_record(struct request *c)
487 struct fcgi_end_request_body end_request;
489 memset(&end_request, 0, sizeof(end_request));
490 end_request.app_status = htonl(0); /* script status */
491 end_request.protocol_status = FCGI_REQUEST_COMPLETE;
493 fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
494 sizeof(end_request));
497 void
498 fcgi_cleanup_request(struct request *c)
500 cgi_inflight--;
501 client_cnt--;
503 evtimer_del(&c->tmo);
504 if (event_initialized(&c->ev))
505 event_del(&c->ev);
507 close(c->fd);
508 template_free(c->tp);
509 gotweb_free_transport(c->t);
510 free(c);
513 void
514 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
516 dump_fcgi_record_header(p, h);
518 if (h->type == FCGI_BEGIN_REQUEST)
519 dump_fcgi_begin_request_body(p,
520 (struct fcgi_begin_request_body *)(h + 1));
521 else if (h->type == FCGI_END_REQUEST)
522 dump_fcgi_end_request_body(p,
523 (struct fcgi_end_request_body *)(h + 1));
526 void
527 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
529 log_debug("%sversion: %d", p, h->version);
530 log_debug("%stype: %d", p, h->type);
531 log_debug("%srequestId: %d", p, ntohs(h->id));
532 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
533 log_debug("%spaddingLength: %d", p, h->padding_len);
534 log_debug("%sreserved: %d", p, h->reserved);
537 void
538 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
540 log_debug("%srole %d", p, ntohs(b->role));
541 log_debug("%sflags %d", p, b->flags);
544 void
545 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
547 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
548 log_debug("%sprotocolStatus: %d", p, b->protocol_status);