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 "got_compat.h"
21 #include <arpa/inet.h>
22 #include <sys/queue.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <sys/uio.h>
27 #include <errno.h>
28 #include <event.h>
29 #include <imsg.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
37 #include "got_error.h"
38 #include "got_reference.h"
40 #include "proc.h"
41 #include "gotwebd.h"
42 #include "tmpl.h"
44 size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
45 void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
46 uint16_t);
47 void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
48 int fcgi_send_response(struct request *, int, const void *, size_t);
50 void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
51 void dump_fcgi_begin_request_body(const char *,
52 struct fcgi_begin_request_body *);
53 void dump_fcgi_end_request_body(const char *,
54 struct fcgi_end_request_body *);
56 extern int cgi_inflight;
57 extern volatile int client_cnt;
59 void
60 fcgi_request(int fd, short events, void *arg)
61 {
62 struct request *c = arg;
63 ssize_t n;
64 size_t parsed = 0;
66 n = read(fd, c->buf + c->buf_pos + c->buf_len,
67 FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
69 switch (n) {
70 case -1:
71 switch (errno) {
72 case EINTR:
73 case EAGAIN:
74 return;
75 default:
76 goto fail;
77 }
78 break;
80 case 0:
81 log_debug("closed connection");
82 goto fail;
83 default:
84 break;
85 }
87 c->buf_len += n;
89 /*
90 * Parse the records as they are received. Per the FastCGI
91 * specification, the server need only receive the FastCGI
92 * parameter records in full; it is free to begin execution
93 * at that point, which is what happens here.
94 */
95 do {
96 parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
97 if (parsed != 0) {
98 c->buf_pos += parsed;
99 c->buf_len -= parsed;
102 /* drop the parsed record */
103 if (parsed != 0 && c->buf_len > 0) {
104 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
105 c->buf_pos = 0;
107 } while (parsed > 0 && c->buf_len > 0);
109 return;
110 fail:
111 fcgi_cleanup_request(c);
114 size_t
115 fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
117 struct fcgi_record_header *h;
119 if (n < sizeof(struct fcgi_record_header))
120 return 0;
122 h = (struct fcgi_record_header*) buf;
124 dump_fcgi_record("", h);
126 if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
127 + h->padding_len)
128 return 0;
130 if (h->version != 1)
131 log_warn("wrong version");
133 switch (h->type) {
134 case FCGI_BEGIN_REQUEST:
135 fcgi_parse_begin_request(buf +
136 sizeof(struct fcgi_record_header),
137 ntohs(h->content_len), c, ntohs(h->id));
138 break;
139 case FCGI_PARAMS:
140 fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
141 ntohs(h->content_len), c, ntohs(h->id));
142 break;
143 case FCGI_STDIN:
144 case FCGI_ABORT_REQUEST:
145 fcgi_create_end_record(c);
146 fcgi_cleanup_request(c);
147 return 0;
148 default:
149 log_warn("unimplemented type %d", h->type);
150 break;
153 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
154 + h->padding_len);
157 void
158 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
159 struct request *c, uint16_t id)
161 /* XXX -- FCGI_CANT_MPX_CONN */
162 if (c->request_started) {
163 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
164 return;
167 if (n != sizeof(struct fcgi_begin_request_body)) {
168 log_warn("wrong size %d != %lu", n,
169 sizeof(struct fcgi_begin_request_body));
170 return;
173 c->request_started = 1;
174 c->id = id;
177 void
178 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
180 uint32_t name_len, val_len;
181 uint8_t *val;
183 if (!c->request_started) {
184 log_warn("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
185 return;
188 if (c->id != id) {
189 log_warn("unexpected id, ignoring");
190 return;
193 if (n == 0) {
194 gotweb_process_request(c);
195 template_flush(c->tp);
196 return;
199 while (n > 0) {
200 if (buf[0] >> 7 == 0) {
201 name_len = buf[0];
202 n--;
203 buf++;
204 } else {
205 if (n > 3) {
206 name_len = ((buf[0] & 0x7f) << 24) +
207 (buf[1] << 16) + (buf[2] << 8) + buf[3];
208 n -= 4;
209 buf += 4;
210 } else
211 return;
214 if (n == 0)
215 return;
217 if (buf[0] >> 7 == 0) {
218 val_len = buf[0];
219 n--;
220 buf++;
221 } else {
222 if (n > 3) {
223 val_len = ((buf[0] & 0x7f) << 24) +
224 (buf[1] << 16) + (buf[2] << 8) +
225 buf[3];
226 n -= 4;
227 buf += 4;
228 } else
229 return;
232 if (n < name_len + val_len)
233 return;
235 val = buf + name_len;
237 if (c->querystring[0] == '\0' &&
238 val_len < MAX_QUERYSTRING &&
239 name_len == 12 &&
240 strncmp(buf, "QUERY_STRING", 12) == 0) {
241 memcpy(c->querystring, val, val_len);
242 c->querystring[val_len] = '\0';
245 if (c->document_uri[0] == '\0' &&
246 val_len < MAX_DOCUMENT_URI &&
247 name_len == 12 &&
248 strncmp(buf, "DOCUMENT_URI", 12) == 0) {
249 memcpy(c->document_uri, val, val_len);
250 c->document_uri[val_len] = '\0';
253 if (c->server_name[0] == '\0' &&
254 val_len < MAX_SERVER_NAME &&
255 name_len == 11 &&
256 strncmp(buf, "SERVER_NAME", 11) == 0) {
257 memcpy(c->server_name, val, val_len);
258 c->server_name[val_len] = '\0';
261 if (name_len == 5 &&
262 strncmp(buf, "HTTPS", 5) == 0)
263 c->https = 1;
265 buf += name_len + val_len;
266 n -= name_len - val_len;
270 void
271 fcgi_timeout(int fd, short events, void *arg)
273 fcgi_cleanup_request((struct request*) arg);
276 static int
277 send_response(struct request *c, int type, const uint8_t *data,
278 size_t len)
280 static const uint8_t padding[FCGI_PADDING_SIZE];
281 struct fcgi_record_header header;
282 struct iovec iov[3];
283 struct timespec ts;
284 ssize_t nw;
285 size_t padded_len, tot;
286 int i, err = 0, th = 2000;
288 ts.tv_sec = 0;
289 ts.tv_nsec = 50;
291 memset(&header, 0, sizeof(header));
292 header.version = 1;
293 header.type = type;
294 header.id = htons(c->id);
295 header.content_len = htons(len);
297 /* The FastCGI spec suggests to align the output buffer */
298 tot = sizeof(header) + len;
299 padded_len = FCGI_ALIGN(tot);
300 if (padded_len > tot) {
301 header.padding_len = padded_len - tot;
302 tot += header.padding_len;
305 iov[0].iov_base = &header;
306 iov[0].iov_len = sizeof(header);
308 iov[1].iov_base = (void *)data;
309 iov[1].iov_len = len;
311 iov[2].iov_base = (void *)padding;
312 iov[2].iov_len = header.padding_len;
314 dump_fcgi_record("resp ", &header);
316 /*
317 * XXX: add some simple write heuristics here
318 * On slower VMs, spotty connections, etc., we don't want to go right to
319 * disconnect. Let's at least try to write the data a few times before
320 * giving up.
321 */
322 while (tot > 0) {
323 nw = writev(c->fd, iov, nitems(iov));
324 if (nw == 0) {
325 c->sock->client_status = CLIENT_DISCONNECT;
326 break;
328 if (nw == -1) {
329 err++;
330 if (errno == EAGAIN && err < th) {
331 nanosleep(&ts, NULL);
332 continue;
334 log_debug("%s: write failure: %s", __func__,
335 strerror(errno));
336 c->sock->client_status = CLIENT_DISCONNECT;
337 return -1;
340 if (nw != tot)
341 log_debug("%s: partial write: %zu vs %zu", __func__,
342 nw, tot);
344 tot -= nw;
345 for (i = 0; i < nitems(iov); ++i) {
346 if (nw < iov[i].iov_len) {
347 iov[i].iov_base += nw;
348 iov[i].iov_len -= nw;
349 break;
351 nw -= iov[i].iov_len;
352 iov[i].iov_len = 0;
356 return 0;
359 int
360 fcgi_send_response(struct request *c, int type, const void *data,
361 size_t len)
363 if (c->sock->client_status == CLIENT_DISCONNECT)
364 return -1;
366 while (len > FCGI_CONTENT_SIZE) {
367 if (send_response(c, type, data, len) == -1)
368 return -1;
370 data += FCGI_CONTENT_SIZE;
371 len -= FCGI_CONTENT_SIZE;
374 if (len == 0)
375 return 0;
377 return send_response(c, type, data, len);
380 int
381 fcgi_write(void *arg, const void *buf, size_t len)
383 struct request *c = arg;
385 return fcgi_send_response(c, FCGI_STDOUT, buf, len);
388 void
389 fcgi_create_end_record(struct request *c)
391 struct fcgi_end_request_body end_request;
393 memset(&end_request, 0, sizeof(end_request));
394 end_request.app_status = htonl(0); /* script status */
395 end_request.protocol_status = FCGI_REQUEST_COMPLETE;
397 fcgi_send_response(c, FCGI_END_REQUEST, &end_request,
398 sizeof(end_request));
401 void
402 fcgi_cleanup_request(struct request *c)
404 cgi_inflight--;
405 client_cnt--;
407 evtimer_del(&c->tmo);
408 if (event_initialized(&c->ev))
409 event_del(&c->ev);
411 close(c->fd);
412 template_free(c->tp);
413 if (c->t != NULL)
414 gotweb_free_transport(c->t);
415 free(c);
418 void
419 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
421 dump_fcgi_record_header(p, h);
423 if (h->type == FCGI_BEGIN_REQUEST)
424 dump_fcgi_begin_request_body(p,
425 (struct fcgi_begin_request_body *)(h + 1));
426 else if (h->type == FCGI_END_REQUEST)
427 dump_fcgi_end_request_body(p,
428 (struct fcgi_end_request_body *)(h + 1));
431 void
432 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
434 log_debug("%sversion: %d", p, h->version);
435 log_debug("%stype: %d", p, h->type);
436 log_debug("%srequestId: %d", p, ntohs(h->id));
437 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
438 log_debug("%spaddingLength: %d", p, h->padding_len);
439 log_debug("%sreserved: %d", p, h->reserved);
442 void
443 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
445 log_debug("%srole %d", p, ntohs(b->role));
446 log_debug("%sflags %d", p, b->flags);
449 void
450 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
452 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
453 log_debug("%sprotocolStatus: %d", p, b->protocol_status);