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>
24 #include <errno.h>
25 #include <event.h>
26 #include <imsg.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <time.h>
31 #include <unistd.h>
33 #include "got_error.h"
35 #include "proc.h"
36 #include "gotwebd.h"
38 size_t fcgi_parse_record(uint8_t *, size_t, struct request *);
39 void fcgi_parse_begin_request(uint8_t *, uint16_t, struct request *,
40 uint16_t);
41 void fcgi_parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
42 void fcgi_send_response(struct request *, struct fcgi_response *);
44 void dump_fcgi_record_header(const char *, struct fcgi_record_header *);
45 void dump_fcgi_begin_request_body(const char *,
46 struct fcgi_begin_request_body *);
47 void dump_fcgi_end_request_body(const char *,
48 struct fcgi_end_request_body *);
50 extern int cgi_inflight;
51 extern volatile int client_cnt;
53 void
54 fcgi_request(int fd, short events, void *arg)
55 {
56 struct request *c = arg;
57 ssize_t n;
58 size_t parsed = 0;
60 n = read(fd, c->buf + c->buf_pos + c->buf_len,
61 FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
63 switch (n) {
64 case -1:
65 switch (errno) {
66 case EINTR:
67 case EAGAIN:
68 return;
69 default:
70 goto fail;
71 }
72 break;
74 case 0:
75 log_debug("closed connection");
76 goto fail;
77 default:
78 break;
79 }
81 c->buf_len += n;
83 /*
84 * Parse the records as they are received. Per the FastCGI
85 * specification, the server need only receive the FastCGI
86 * parameter records in full; it is free to begin execution
87 * at that point, which is what happens here.
88 */
89 do {
90 parsed = fcgi_parse_record(c->buf + c->buf_pos, c->buf_len, c);
91 if (parsed != 0) {
92 c->buf_pos += parsed;
93 c->buf_len -= parsed;
94 }
95 } while (parsed > 0 && c->buf_len > 0);
97 /* Make space for further reads */
98 if (parsed != 0)
99 if (c->buf_len > 0) {
100 bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
101 c->buf_pos = 0;
103 return;
104 fail:
105 fcgi_cleanup_request(c);
108 size_t
109 fcgi_parse_record(uint8_t *buf, size_t n, struct request *c)
111 struct fcgi_record_header *h;
113 if (n < sizeof(struct fcgi_record_header))
114 return 0;
116 h = (struct fcgi_record_header*) buf;
118 dump_fcgi_record("", h);
120 if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
121 + h->padding_len)
122 return 0;
124 if (h->version != 1)
125 log_warn("wrong version");
127 switch (h->type) {
128 case FCGI_BEGIN_REQUEST:
129 fcgi_parse_begin_request(buf +
130 sizeof(struct fcgi_record_header),
131 ntohs(h->content_len), c, ntohs(h->id));
132 break;
133 case FCGI_PARAMS:
134 fcgi_parse_params(buf + sizeof(struct fcgi_record_header),
135 ntohs(h->content_len), c, ntohs(h->id));
136 break;
137 case FCGI_STDIN:
138 case FCGI_ABORT_REQUEST:
139 fcgi_create_end_record(c);
140 fcgi_cleanup_request(c);
141 return 0;
142 default:
143 log_warn("unimplemented type %d", h->type);
144 break;
147 return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
148 + h->padding_len);
151 void
152 fcgi_parse_begin_request(uint8_t *buf, uint16_t n,
153 struct request *c, uint16_t id)
155 /* XXX -- FCGI_CANT_MPX_CONN */
156 if (c->request_started) {
157 log_warn("unexpected FCGI_BEGIN_REQUEST, ignoring");
158 return;
161 if (n != sizeof(struct fcgi_begin_request_body)) {
162 log_warn("wrong size %d != %lu", n,
163 sizeof(struct fcgi_begin_request_body));
164 return;
167 c->request_started = 1;
169 c->id = id;
170 SLIST_INIT(&c->env);
171 c->env_count = 0;
174 void
175 fcgi_parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
177 struct env_val *env_entry;
178 uint32_t name_len, val_len;
179 uint8_t *sd, *dr_buf;
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 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 if (buf[0] >> 7 == 0) {
213 val_len = buf[0];
214 n--;
215 buf++;
216 } else {
217 if (n > 3) {
218 val_len = ((buf[0] & 0x7f) << 24) +
219 (buf[1] << 16) + (buf[2] << 8) +
220 buf[3];
221 n -= 4;
222 buf += 4;
223 } else
224 return;
226 } else
227 return;
229 if (n < name_len + val_len)
230 return;
232 if ((env_entry = malloc(sizeof(struct env_val))) == NULL) {
233 log_warn("cannot malloc env_entry");
234 return;
237 if ((env_entry->val = calloc(sizeof(char), name_len + val_len +
238 2)) == NULL) {
239 log_warn("cannot allocate env_entry->val");
240 free(env_entry);
241 return;
244 bcopy(buf, env_entry->val, name_len);
245 buf += name_len;
246 n -= name_len;
248 env_entry->val[name_len] = '\0';
249 if (val_len < MAX_QUERYSTRING && strcmp(env_entry->val,
250 "QUERY_STRING") == 0 && c->querystring[0] == '\0') {
251 bcopy(buf, c->querystring, val_len);
252 c->querystring[val_len] = '\0';
254 if (val_len < GOTWEBD_MAXTEXT && strcmp(env_entry->val,
255 "HTTP_HOST") == 0 && c->http_host[0] == '\0') {
257 /*
258 * lazily get subdomain
259 * will only get domain if no subdomain exists
260 * this can still work if gotweb server name is the same
261 */
262 sd = strchr(buf, '.');
263 if (sd)
264 *sd = '\0';
266 bcopy(buf, c->http_host, val_len);
267 c->http_host[val_len] = '\0';
269 if (val_len < MAX_DOCUMENT_ROOT && strcmp(env_entry->val,
270 "DOCUMENT_ROOT") == 0 && c->document_root[0] == '\0') {
272 /* drop first char, as it's always / */
273 dr_buf = &buf[1];
275 bcopy(dr_buf, c->document_root, val_len - 1);
276 c->document_root[val_len] = '\0';
278 if (val_len < MAX_SERVER_NAME && strcmp(env_entry->val,
279 "SERVER_NAME") == 0 && c->server_name[0] == '\0') {
280 /* drop first char, as it's always / */
282 bcopy(buf, c->server_name, val_len);
283 c->server_name[val_len] = '\0';
285 env_entry->val[name_len] = '=';
287 bcopy(buf, (env_entry->val) + name_len + 1, val_len);
288 buf += val_len;
289 n -= val_len;
291 SLIST_INSERT_HEAD(&c->env, env_entry, entry);
292 log_debug("env[%d], %s", c->env_count, env_entry->val);
293 c->env_count++;
297 void
298 fcgi_timeout(int fd, short events, void *arg)
300 fcgi_cleanup_request((struct request*) arg);
303 int
304 fcgi_gen_binary_response(struct request *c, const uint8_t *data, int len)
306 struct fcgi_response *resp;
307 struct fcgi_record_header *header;
308 ssize_t n = 0;
309 int i;
311 if (c->sock->client_status == CLIENT_DISCONNECT)
312 return -1;
314 if (data == NULL)
315 return 0;
317 if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
318 log_warn("%s: cannot calloc fcgi_response", __func__);
319 return -1;
322 header = (struct fcgi_record_header*) resp->data;
323 header->version = 1;
324 header->type = FCGI_STDOUT;
325 header->id = htons(c->id);
326 header->padding_len = 0;
327 header->reserved = 0;
329 for (i = 0; i < len; i++) {
330 resp->data[i+8] = data[i];
331 n++;
334 header->content_len = htons(n);
335 resp->data_pos = 0;
336 resp->data_len = n + sizeof(struct fcgi_record_header);
337 fcgi_send_response(c, resp);
339 return 0;
342 int
343 fcgi_gen_response(struct request *c, const char *data)
345 struct fcgi_response *resp;
346 struct fcgi_record_header *header;
347 ssize_t n = 0;
348 int i;
350 if (c->sock->client_status == CLIENT_DISCONNECT)
351 return -1;
353 if (data == NULL)
354 return 0;
356 if (strlen(data) == 0)
357 return 0;
359 if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
360 log_warn("%s: cannot calloc fcgi_response", __func__);
361 return -1;
364 header = (struct fcgi_record_header*) resp->data;
365 header->version = 1;
366 header->type = FCGI_STDOUT;
367 header->id = htons(c->id);
368 header->padding_len = 0;
369 header->reserved = 0;
371 for (i = 0; i < strlen(data); i++) {
372 resp->data[i+8] = data[i];
373 n++;
376 header->content_len = htons(n);
377 resp->data_pos = 0;
378 resp->data_len = n + sizeof(struct fcgi_record_header);
379 fcgi_send_response(c, resp);
381 return 0;
384 void
385 fcgi_send_response(struct request *c, struct fcgi_response *resp)
387 struct fcgi_record_header *header;
388 struct timespec ts;
389 size_t padded_len;
390 int err = 0, th = 2000;
392 ts.tv_sec = 0;
393 ts.tv_nsec = 50;
395 header = (struct fcgi_record_header*)resp->data;
397 /* The FastCGI spec suggests to align the output buffer */
398 padded_len = FCGI_ALIGN(resp->data_len);
399 if (padded_len > resp->data_len) {
400 /* There should always be FCGI_PADDING_SIZE bytes left */
401 if (padded_len > FCGI_RECORD_SIZE)
402 log_warn("response too long");
403 header->padding_len = padded_len - resp->data_len;
404 resp->data_len = padded_len;
407 dump_fcgi_record("resp ", header);
409 /*
410 * XXX: add some simple write heuristics here
411 * On slower VMs, spotty connections, etc., we don't want to go right to
412 * disconnect. Let's at least try to write the data a few times before
413 * giving up.
414 */
415 while ((write(c->fd, resp->data + resp->data_pos,
416 resp->data_len)) == -1) {
417 nanosleep(&ts, NULL);
418 err++;
419 if (err == th) {
420 c->sock->client_status = CLIENT_DISCONNECT;
421 break;
425 free(resp);
428 void
429 fcgi_create_end_record(struct request *c)
431 struct fcgi_response *resp;
432 struct fcgi_record_header *header;
433 struct fcgi_end_request_body *end_request;
435 if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
436 log_warn("cannot calloc fcgi_response");
437 return;
439 header = (struct fcgi_record_header*) resp->data;
440 header->version = 1;
441 header->type = FCGI_END_REQUEST;
442 header->id = htons(c->id);
443 header->content_len = htons(sizeof(struct
444 fcgi_end_request_body));
445 header->padding_len = 0;
446 header->reserved = 0;
447 end_request = (struct fcgi_end_request_body *) (resp->data +
448 sizeof(struct fcgi_record_header));
449 end_request->app_status = htonl(0); /* script_status */
450 end_request->protocol_status = FCGI_REQUEST_COMPLETE;
451 end_request->reserved[0] = 0;
452 end_request->reserved[1] = 0;
453 end_request->reserved[2] = 0;
454 resp->data_pos = 0;
455 resp->data_len = sizeof(struct fcgi_end_request_body) +
456 sizeof(struct fcgi_record_header);
457 fcgi_send_response(c, resp);
460 void
461 fcgi_cleanup_request(struct request *c)
463 cgi_inflight--;
464 client_cnt--;
466 evtimer_del(&c->tmo);
467 if (event_initialized(&c->ev))
468 event_del(&c->ev);
470 close(c->fd);
471 gotweb_free_transport(c->t);
472 free(c);
475 void
476 dump_fcgi_record(const char *p, struct fcgi_record_header *h)
478 dump_fcgi_record_header(p, h);
480 if (h->type == FCGI_BEGIN_REQUEST)
481 dump_fcgi_begin_request_body(p,
482 (struct fcgi_begin_request_body *)(h + 1));
483 else if (h->type == FCGI_END_REQUEST)
484 dump_fcgi_end_request_body(p,
485 (struct fcgi_end_request_body *)(h + 1));
488 void
489 dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
491 log_debug("%sversion: %d", p, h->version);
492 log_debug("%stype: %d", p, h->type);
493 log_debug("%srequestId: %d", p, ntohs(h->id));
494 log_debug("%scontentLength: %d", p, ntohs(h->content_len));
495 log_debug("%spaddingLength: %d", p, h->padding_len);
496 log_debug("%sreserved: %d", p, h->reserved);
499 void
500 dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
502 log_debug("%srole %d", p, ntohs(b->role));
503 log_debug("%sflags %d", p, b->flags);
506 void
507 dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
509 log_debug("%sappStatus: %d", p, ntohl(b->app_status));
510 log_debug("%sprotocolStatus: %d", p, b->protocol_status);