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 "tmpl.h"
41 size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
42 void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
43 uint16_t);
44 void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
45 int fcgi_send_response(struct request *, int, const void *, size_t);
47 void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
48 void dump_fcgi_begin_request_body(const char *,
49 struct fcgi_begin_request_body *);
50 void dump_fcgi_end_request_body(const char *,
51 struct fcgi_end_request_body *);
53 extern int cgi_inflight;
54 extern volatile int client_cnt;
56 void
57 fcgi_request(int fd, short events, void *arg)
58 {
59 struct request *c = arg;
60 ssize_t n;
61 size_t parsed = 0;
63 n = read(fd, c->buf + c->buf_pos + c->buf_len,
64 FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
66 switch (n) {
67 case -1:
68 switch (errno) {
69 case EINTR:
70 case EAGAIN:
71 return;
72 default:
73 goto fail;
74 }
75 break;
77 case 0:
78 log_debug("closed connection");
79 goto fail;
80 default:
81 break;
82 }
84 c->buf_len += n;
86 /*
87 * Parse the records as they are received. Per the FastCGI
88 * specification, the server need only receive the FastCGI
89 * parameter records in full; it is free to begin execution
90 * at that point, which is what happens here.
91 */
92 do {
93 parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
94 if (parsed != 0) {
95 c->buf_pos += parsed;
96 c->buf_len -= parsed;
97 }
99 /* drop the parsed record */
100 if (parsed != 0 && c->buf_len > 0) {
101 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
102 c->buf_pos = 0;
104 } while (parsed > 0 && c->buf_len > 0);
106 return;
107 fail:
108 fcgi_cleanup_request(c);
111 size_t
112 fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
114 struct fcgi_record_header *h;
116 if (n < sizeof(struct fcgi_record_header))
117 return 0;
119 h = (struct fcgi_record_header*) buf;
121 dump_fcgi_record("", h);
123 if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
124 + h->padding_len)
125 return 0;
127 if (h->version != 1)
128 log_warn("wrong version");
130 switch (h->type) {
131 case FCGI_BEGIN_REQUEST:
132 fcgi_parse_begin_request(buf +
133 sizeof(struct fcgi_record_header),
134 ntohs(h->content_len), c, ntohs(h->id));
135 break;
136 case FCGI_PARAMS:
137 fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
138 ntohs(h->content_len), c, ntohs(h->id));
139 break;
140 case FCGI_STDIN:
141 case FCGI_ABORT_REQUEST:
142 fcgi_create_end_record(c);
143 fcgi_cleanup_request(c);
144 return 0;
145 default:
146 log_warn("unimplemented type %d", h->type);
147 break;
150 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
151 + h->padding_len);
154 void
155 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
156 struct request *c, uint16_t id)
158 /* XXX -- FCGI_CANT_MPX_CONN */
159 if (c->request_started) {
160 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
161 return;
164 if (n != sizeof(struct fcgi_begin_request_body)) {
165 log_warn("wrong size %d != %lu", n,
166 sizeof(struct fcgi_begin_request_body));
167 return;
170 c->request_started = 1;
171 c->id = id;
174 void
175 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
177 uint32_t name_len, val_len;
178 uint8_t *val;
180 if (!c->request_started) {
181 log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
182 return;
185 if (c->id != id) {
186 log_warn("unexpected id, ignoring");
187 return;
190 if (n == 0) {
191 gotweb_process_request(c);
192 template_flush(c->tp);
193 return;
196 while (n > 0) {
197 if (buf[0] >> 7 == 0) {
198 name_len = buf[0];
199 n--;
200 buf++;
201 } else {
202 if (n > 3) {
203 name_len = ((buf[0] & 0x7f) << 24) +
204 (buf[1] << 16) + (buf[2] << 8) + buf[3];
205 n -= 4;
206 buf += 4;
207 } else
208 return;
211 if (n == 0)
212 return;
214 if (buf[0] >> 7 == 0) {
215 val_len = buf[0];
216 n--;
217 buf++;
218 } else {
219 if (n > 3) {
220 val_len = ((buf[0] & 0x7f) << 24) +
221 (buf[1] << 16) + (buf[2] << 8) +
222 buf[3];
223 n -= 4;
224 buf += 4;
225 } else
226 return;
229 if (n < name_len + val_len)
230 return;
232 val = buf + name_len;
234 if (c->querystring[0] == '\0' &&
235 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 (c->document_uri[0] == '\0' &&
243 val_len < MAX_DOCUMENT_URI &&
244 name_len == 12 &&
245 strncmp(buf, "DOCUMENT_URI", 12) == 0) {
246 memcpy(c->document_uri, val, val_len);
247 c->document_uri[val_len] = '\0';
250 if (c->server_name[0] == '\0' &&
251 val_len < MAX_SERVER_NAME &&
252 name_len == 11 &&
253 strncmp(buf, "SERVER_NAME", 11) == 0) {
254 memcpy(c->server_name, val, val_len);
255 c->server_name[val_len] = '\0';
258 if (name_len == 5 &&
259 strncmp(buf, "HTTPS", 5) == 0)
260 c->https = 1;
262 buf += name_len + val_len;
263 n -= name_len - val_len;
267 void
268 fcgi_timeout(int fd, short events, void *arg)
270 fcgi_cleanup_request((struct request*) arg);
273 static int
274 send_response(struct request *c, int type, const uint8_t *data,
275 size_t len)
277 static const uint8_t padding[FCGI_PADDING_SIZE];
278 struct fcgi_record_header header;
279 struct iovec iov[3];
280 struct timespec ts;
281 ssize_t nw;
282 size_t padded_len, tot;
283 int i, err = 0, th = 2000;
285 ts.tv_sec = 0;
286 ts.tv_nsec = 50;
288 memset(&header, 0, sizeof(header));
289 header.version = 1;
290 header.type = type;
291 header.id = htons(c->id);
292 header.content_len = htons(len);
294 /* The FastCGI spec suggests to align the output buffer */
295 tot = sizeof(header) + len;
296 padded_len = FCGI_ALIGN(tot);
297 if (padded_len > tot) {
298 header.padding_len = padded_len - tot;
299 tot += header.padding_len;
302 iov[0].iov_base = &header;
303 iov[0].iov_len = sizeof(header);
305 iov[1].iov_base = (void *)data;
306 iov[1].iov_len = len;
308 iov[2].iov_base = (void *)padding;
309 iov[2].iov_len = header.padding_len;
311 dump_fcgi_record("resp ", &header);
313 /*
314 * XXX: add some simple write heuristics here
315 * On slower VMs, spotty connections, etc., we don't want to go right to
316 * disconnect. Let's at least try to write the data a few times before
317 * giving up.
318 */
319 while (tot > 0) {
320 nw = writev(c->fd, iov, nitems(iov));
321 if (nw == 0) {
322 c->sock->client_status = CLIENT_DISCONNECT;
323 break;
325 if (nw == -1) {
326 err++;
327 if (errno == EAGAIN && err < th) {
328 nanosleep(&ts, NULL);
329 continue;
331 log_debug("%s: write failure: %s", __func__,
332 strerror(errno));
333 c->sock->client_status = CLIENT_DISCONNECT;
334 return -1;
337 if (nw != tot)
338 log_debug("%s: partial write: %zu vs %zu", __func__,
339 nw, tot);
341 tot -= nw;
342 for (i = 0; i < nitems(iov); ++i) {
343 if (nw < iov[i].iov_len) {
344 iov[i].iov_base += nw;
345 iov[i].iov_len -= nw;
346 break;
348 nw -= iov[i].iov_len;
349 iov[i].iov_len = 0;
353 return 0;
356 int
357 fcgi_send_response(struct request *c, int type, const void *data,
358 size_t len)
360 if (c->sock->client_status == CLIENT_DISCONNECT)
361 return -1;
363 while (len > FCGI_CONTENT_SIZE) {
364 if (send_response(c, type, data, len) == -1)
365 return -1;
367 data += FCGI_CONTENT_SIZE;
368 len -= FCGI_CONTENT_SIZE;
371 if (len == 0)
372 return 0;
374 return send_response(c, type, data, len);
377 int
378 fcgi_write(void *arg, const void *buf, size_t len)
380 struct request *c = arg;
382 return fcgi_send_response(c, FCGI_STDOUT, buf, len);
385 void
386 fcgi_create_end_record(struct request *c)
388 struct fcgi_end_request_body end_request;
390 memset(&end_request, 0, sizeof(end_request));
391 end_request.app_status = htonl(0); /* script status */
392 end_request.protocol_status = FCGI_REQUEST_COMPLETE;
394 fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
395 sizeof(end_request));
398 void
399 fcgi_cleanup_request(struct request *c)
401 cgi_inflight--;
402 client_cnt--;
404 evtimer_del(&c->tmo);
405 if (event_initialized(&c->ev))
406 event_del(&c->ev);
408 close(c->fd);
409 template_free(c->tp);
410 if (c->t != NULL)
411 gotweb_free_transport(c->t);
412 free(c);
415 void
416 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
418 dump_fcgi_record_header(p, h);
420 if (h->type == FCGI_BEGIN_REQUEST)
421 dump_fcgi_begin_request_body(p,
422 (struct fcgi_begin_request_body *)(h + 1));
423 else if (h->type == FCGI_END_REQUEST)
424 dump_fcgi_end_request_body(p,
425 (struct fcgi_end_request_body *)(h + 1));
428 void
429 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
431 log_debug("%sversion: %d", p, h->version);
432 log_debug("%stype: %d", p, h->type);
433 log_debug("%srequestId: %d", p, ntohs(h->id));
434 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
435 log_debug("%spaddingLength: %d", p, h->padding_len);
436 log_debug("%sreserved: %d", p, h->reserved);
439 void
440 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
442 log_debug("%srole %d", p, ntohs(b->role));
443 log_debug("%sflags %d", p, b->flags);
446 void
447 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
449 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
450 log_debug("%sprotocolStatus: %d", p, b->protocol_status);