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"
36 #include "got_reference.h"
38 #include "gotwebd.h"
39 #include "log.h"
40 #include "tmpl.h"
42 size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
43 void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
44 uint16_t);
45 void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
46 int fcgi_send_response(struct request *, int, const void *, size_t);
48 void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
49 void dump_fcgi_begin_request_body(const char *,
50 struct fcgi_begin_request_body *);
51 void dump_fcgi_end_request_body(const char *,
52 struct fcgi_end_request_body *);
54 extern int cgi_inflight;
55 extern volatile int client_cnt;
57 void
58 fcgi_request(int fd, short events, void *arg)
59 {
60 struct request *c = arg;
61 ssize_t n;
62 size_t parsed = 0;
64 n = read(fd, c->buf + c->buf_pos + c->buf_len,
65 FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
67 switch (n) {
68 case -1:
69 switch (errno) {
70 case EINTR:
71 case EAGAIN:
72 return;
73 default:
74 goto fail;
75 }
76 break;
78 case 0:
79 log_debug("closed connection");
80 goto fail;
81 default:
82 break;
83 }
85 c->buf_len += n;
87 /*
88 * Parse the records as they are received. Per the FastCGI
89 * specification, the server need only receive the FastCGI
90 * parameter records in full; it is free to begin execution
91 * at that point, which is what happens here.
92 */
93 do {
94 parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
95 if (parsed != 0) {
96 c->buf_pos += parsed;
97 c->buf_len -= parsed;
98 }
100 /* drop the parsed record */
101 if (parsed != 0 && c->buf_len > 0) {
102 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
103 c->buf_pos = 0;
105 } while (parsed > 0 && c->buf_len > 0);
107 return;
108 fail:
109 fcgi_cleanup_request(c);
112 size_t
113 fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
115 struct fcgi_record_header *h;
117 if (n < sizeof(struct fcgi_record_header))
118 return 0;
120 h = (struct fcgi_record_header*) buf;
122 dump_fcgi_record("", h);
124 if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
125 + h->padding_len)
126 return 0;
128 if (h->version != 1)
129 log_warn("wrong version");
131 switch (h->type) {
132 case FCGI_BEGIN_REQUEST:
133 fcgi_parse_begin_request(buf +
134 sizeof(struct fcgi_record_header),
135 ntohs(h->content_len), c, ntohs(h->id));
136 break;
137 case FCGI_PARAMS:
138 fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
139 ntohs(h->content_len), c, ntohs(h->id));
140 break;
141 case FCGI_STDIN:
142 case FCGI_ABORT_REQUEST:
143 fcgi_create_end_record(c);
144 fcgi_cleanup_request(c);
145 return 0;
146 default:
147 log_warn("unimplemented type %d", h->type);
148 break;
151 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
152 + h->padding_len);
155 void
156 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
157 struct request *c, uint16_t id)
159 /* XXX -- FCGI_CANT_MPX_CONN */
160 if (c->request_started) {
161 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
162 return;
165 if (n != sizeof(struct fcgi_begin_request_body)) {
166 log_warn("wrong size %d != %lu", n,
167 sizeof(struct fcgi_begin_request_body));
168 return;
171 c->request_started = 1;
172 c->id = id;
175 void
176 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
178 uint32_t name_len, val_len;
179 uint8_t *val;
181 if (!c->request_started) {
182 log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
183 return;
186 if (c->id != id) {
187 log_warn("unexpected id, ignoring");
188 return;
191 if (n == 0) {
192 gotweb_process_request(c);
193 template_flush(c->tp);
194 return;
197 while (n > 0) {
198 if (buf[0] >> 7 == 0) {
199 name_len = buf[0];
200 n--;
201 buf++;
202 } else {
203 if (n > 3) {
204 name_len = ((buf[0] & 0x7f) << 24) +
205 (buf[1] << 16) + (buf[2] << 8) + buf[3];
206 n -= 4;
207 buf += 4;
208 } else
209 return;
212 if (n == 0)
213 return;
215 if (buf[0] >> 7 == 0) {
216 val_len = buf[0];
217 n--;
218 buf++;
219 } else {
220 if (n > 3) {
221 val_len = ((buf[0] & 0x7f) << 24) +
222 (buf[1] << 16) + (buf[2] << 8) +
223 buf[3];
224 n -= 4;
225 buf += 4;
226 } else
227 return;
230 if (n < name_len + val_len)
231 return;
233 val = buf + name_len;
235 if (val_len < MAX_QUERYSTRING &&
236 name_len == 12 &&
237 strncmp(buf, "QUERY_STRING", 12) == 0) {
238 memcpy(c->querystring, val, val_len);
239 c->querystring[val_len] = '\0';
242 if (val_len < MAX_DOCUMENT_URI &&
243 name_len == 12 &&
244 strncmp(buf, "DOCUMENT_URI", 12) == 0) {
245 memcpy(c->document_uri, val, val_len);
246 c->document_uri[val_len] = '\0';
249 if (val_len < MAX_SERVER_NAME &&
250 name_len == 11 &&
251 strncmp(buf, "SERVER_NAME", 11) == 0) {
252 memcpy(c->server_name, val, val_len);
253 c->server_name[val_len] = '\0';
256 if (name_len == 5 &&
257 strncmp(buf, "HTTPS", 5) == 0)
258 c->https = 1;
260 buf += name_len + val_len;
261 n -= name_len - val_len;
265 void
266 fcgi_timeout(int fd, short events, void *arg)
268 fcgi_cleanup_request((struct request*) arg);
271 static int
272 send_response(struct request *c, int type, const uint8_t *data,
273 size_t len)
275 static const uint8_t padding[FCGI_PADDING_SIZE];
276 struct fcgi_record_header header;
277 struct iovec iov[3];
278 struct timespec ts;
279 ssize_t nw;
280 size_t padded_len, tot;
281 int i, err = 0, th = 2000;
283 ts.tv_sec = 0;
284 ts.tv_nsec = 50;
286 memset(&header, 0, sizeof(header));
287 header.version = 1;
288 header.type = type;
289 header.id = htons(c->id);
290 header.content_len = htons(len);
292 /* The FastCGI spec suggests to align the output buffer */
293 tot = sizeof(header) + len;
294 padded_len = FCGI_ALIGN(tot);
295 if (padded_len > tot) {
296 header.padding_len = padded_len - tot;
297 tot += header.padding_len;
300 iov[0].iov_base = &header;
301 iov[0].iov_len = sizeof(header);
303 iov[1].iov_base = (void *)data;
304 iov[1].iov_len = len;
306 iov[2].iov_base = (void *)padding;
307 iov[2].iov_len = header.padding_len;
309 dump_fcgi_record("resp ", &header);
311 /*
312 * XXX: add some simple write heuristics here
313 * On slower VMs, spotty connections, etc., we don't want to go right to
314 * disconnect. Let's at least try to write the data a few times before
315 * giving up.
316 */
317 while (tot > 0) {
318 nw = writev(c->fd, iov, nitems(iov));
319 if (nw == 0) {
320 c->sock->client_status = CLIENT_DISCONNECT;
321 break;
323 if (nw == -1) {
324 err++;
325 if (errno == EAGAIN && err < th) {
326 nanosleep(&ts, NULL);
327 continue;
329 log_debug("%s: write failure: %s", __func__,
330 strerror(errno));
331 c->sock->client_status = CLIENT_DISCONNECT;
332 return -1;
335 if (nw != tot)
336 log_debug("%s: partial write: %zu vs %zu", __func__,
337 nw, tot);
339 tot -= nw;
340 for (i = 0; i < nitems(iov); ++i) {
341 if (nw < iov[i].iov_len) {
342 iov[i].iov_base += nw;
343 iov[i].iov_len -= nw;
344 break;
346 nw -= iov[i].iov_len;
347 iov[i].iov_len = 0;
351 return 0;
354 int
355 fcgi_send_response(struct request *c, int type, const void *data,
356 size_t len)
358 if (c->sock->client_status == CLIENT_DISCONNECT)
359 return -1;
361 while (len > FCGI_CONTENT_SIZE) {
362 if (send_response(c, type, data, len) == -1)
363 return -1;
365 data += FCGI_CONTENT_SIZE;
366 len -= FCGI_CONTENT_SIZE;
369 if (len == 0)
370 return 0;
372 return send_response(c, type, data, len);
375 int
376 fcgi_write(void *arg, const void *buf, size_t len)
378 struct request *c = arg;
380 return fcgi_send_response(c, FCGI_STDOUT, buf, len);
383 void
384 fcgi_create_end_record(struct request *c)
386 struct fcgi_end_request_body end_request;
388 memset(&end_request, 0, sizeof(end_request));
389 end_request.app_status = htonl(0); /* script status */
390 end_request.protocol_status = FCGI_REQUEST_COMPLETE;
392 fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
393 sizeof(end_request));
396 void
397 fcgi_cleanup_request(struct request *c)
399 cgi_inflight--;
400 client_cnt--;
402 evtimer_del(&c->tmo);
403 if (event_initialized(&c->ev))
404 event_del(&c->ev);
406 close(c->fd);
407 template_free(c->tp);
408 if (c->t != NULL)
409 gotweb_free_transport(c->t);
410 free(c);
413 void
414 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
416 dump_fcgi_record_header(p, h);
418 if (h->type == FCGI_BEGIN_REQUEST)
419 dump_fcgi_begin_request_body(p,
420 (struct fcgi_begin_request_body *)(h + 1));
421 else if (h->type == FCGI_END_REQUEST)
422 dump_fcgi_end_request_body(p,
423 (struct fcgi_end_request_body *)(h + 1));
426 void
427 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
429 log_debug("%sversion: %d", p, h->version);
430 log_debug("%stype: %d", p, h->type);
431 log_debug("%srequestId: %d", p, ntohs(h->id));
432 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
433 log_debug("%spaddingLength: %d", p, h->padding_len);
434 log_debug("%sreserved: %d", p, h->reserved);
437 void
438 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
440 log_debug("%srole %d", p, ntohs(b->role));
441 log_debug("%sflags %d", p, b->flags);
444 void
445 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
447 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
448 log_debug("%sprotocolStatus: %d", p, b->protocol_status);