2 014c66b6 2023-06-25 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 014c66b6 2023-06-25 op * Permission to use, copy, modify, and distribute this software for any
5 014c66b6 2023-06-25 op * purpose with or without fee is hereby granted, provided that the above
6 014c66b6 2023-06-25 op * copyright notice and this permission notice appear in all copies.
8 014c66b6 2023-06-25 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 014c66b6 2023-06-25 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 014c66b6 2023-06-25 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 014c66b6 2023-06-25 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 014c66b6 2023-06-25 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 014c66b6 2023-06-25 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 014c66b6 2023-06-25 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 014c66b6 2023-06-25 op #include <sys/queue.h>
18 014c66b6 2023-06-25 op #include <sys/tree.h>
19 014c66b6 2023-06-25 op #include <sys/socket.h>
21 014c66b6 2023-06-25 op #include <netinet/in.h>
22 014c66b6 2023-06-25 op #include <arpa/inet.h>
24 014c66b6 2023-06-25 op #include <errno.h>
25 014c66b6 2023-06-25 op #include <event.h>
26 014c66b6 2023-06-25 op #include <limits.h>
27 014c66b6 2023-06-25 op #include <stdarg.h>
28 014c66b6 2023-06-25 op #include <stdio.h>
29 014c66b6 2023-06-25 op #include <stdlib.h>
30 014c66b6 2023-06-25 op #include <stdint.h>
31 014c66b6 2023-06-25 op #include <string.h>
32 014c66b6 2023-06-25 op #include <unistd.h>
34 014c66b6 2023-06-25 op #include "log.h"
35 014c66b6 2023-06-25 op #include "pkg.h"
38 014c66b6 2023-06-25 op #include "tmpl.h"
41 014c66b6 2023-06-25 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
43 014c66b6 2023-06-25 op struct fcgi_header {
44 014c66b6 2023-06-25 op unsigned char version;
45 014c66b6 2023-06-25 op unsigned char type;
46 014c66b6 2023-06-25 op unsigned char req_id1;
47 014c66b6 2023-06-25 op unsigned char req_id0;
48 014c66b6 2023-06-25 op unsigned char content_len1;
49 014c66b6 2023-06-25 op unsigned char content_len0;
50 014c66b6 2023-06-25 op unsigned char padding;
51 014c66b6 2023-06-25 op unsigned char reserved;
52 014c66b6 2023-06-25 op } __attribute__((packed));
55 014c66b6 2023-06-25 op * number of bytes in a FCGI_HEADER. Future version of the protocol
56 014c66b6 2023-06-25 op * will not reduce this number.
58 014c66b6 2023-06-25 op #define FCGI_HEADER_LEN 8
61 014c66b6 2023-06-25 op * values for the version component
63 014c66b6 2023-06-25 op #define FCGI_VERSION_1 1
66 014c66b6 2023-06-25 op * values for the type component
68 014c66b6 2023-06-25 op #define FCGI_BEGIN_REQUEST 1
69 014c66b6 2023-06-25 op #define FCGI_ABORT_REQUEST 2
70 014c66b6 2023-06-25 op #define FCGI_END_REQUEST 3
71 014c66b6 2023-06-25 op #define FCGI_PARAMS 4
72 014c66b6 2023-06-25 op #define FCGI_STDIN 5
73 014c66b6 2023-06-25 op #define FCGI_STDOUT 6
74 014c66b6 2023-06-25 op #define FCGI_STDERR 7
75 014c66b6 2023-06-25 op #define FCGI_DATA 8
76 014c66b6 2023-06-25 op #define FCGI_GET_VALUES 9
77 014c66b6 2023-06-25 op #define FCGI_GET_VALUES_RESULT 10
78 014c66b6 2023-06-25 op #define FCGI_UNKNOWN_TYPE 11
79 014c66b6 2023-06-25 op #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
81 014c66b6 2023-06-25 op struct fcgi_begin_req {
82 014c66b6 2023-06-25 op unsigned char role1;
83 014c66b6 2023-06-25 op unsigned char role0;
84 014c66b6 2023-06-25 op unsigned char flags;
85 014c66b6 2023-06-25 op unsigned char reserved[5];
88 014c66b6 2023-06-25 op struct fcgi_begin_req_record {
89 014c66b6 2023-06-25 op struct fcgi_header header;
90 014c66b6 2023-06-25 op struct fcgi_begin_req body;
94 014c66b6 2023-06-25 op * mask for flags;
96 014c66b6 2023-06-25 op #define FCGI_KEEP_CONN 1
99 014c66b6 2023-06-25 op * values for the role
101 014c66b6 2023-06-25 op #define FCGI_RESPONDER 1
102 014c66b6 2023-06-25 op #define FCGI_AUTHORIZER 2
103 014c66b6 2023-06-25 op #define FCGI_FILTER 3
105 014c66b6 2023-06-25 op struct fcgi_end_req_body {
106 014c66b6 2023-06-25 op unsigned char app_status3;
107 014c66b6 2023-06-25 op unsigned char app_status2;
108 014c66b6 2023-06-25 op unsigned char app_status1;
109 014c66b6 2023-06-25 op unsigned char app_status0;
110 014c66b6 2023-06-25 op unsigned char proto_status;
111 014c66b6 2023-06-25 op unsigned char reserved[3];
115 014c66b6 2023-06-25 op * values for proto_status
117 014c66b6 2023-06-25 op #define FCGI_REQUEST_COMPLETE 0
118 014c66b6 2023-06-25 op #define FCGI_CANT_MPX_CONN 1
119 014c66b6 2023-06-25 op #define FCGI_OVERLOADED 2
120 014c66b6 2023-06-25 op #define FCGI_UNKNOWN_ROLE 3
123 014c66b6 2023-06-25 op * Variable names for FCGI_GET_VALUES / FCGI_GET_VALUES_RESULT
126 014c66b6 2023-06-25 op #define FCGI_MAX_CONNS "FCGI_MAX_CONNS"
127 014c66b6 2023-06-25 op #define FCGI_MAX_REQS "FCGI_MAX_REQS"
128 014c66b6 2023-06-25 op #define FCGI_MPXS_CONNS "FCGI_MPXS_CONNS"
130 014c66b6 2023-06-25 op #define CAT(f0, f1) ((f0) + ((f1) << 8))
133 014c66b6 2023-06-25 op FCGI_RECORD_HEADER,
134 014c66b6 2023-06-25 op FCGI_RECORD_BODY,
137 014c66b6 2023-06-25 op volatile int fcgi_inflight;
138 014c66b6 2023-06-25 op int32_t fcgi_id;
140 014c66b6 2023-06-25 op int accept_reserve(int, struct sockaddr *, socklen_t *, int,
141 014c66b6 2023-06-25 op volatile int *);
144 014c66b6 2023-06-25 op fcgi_send_end_req(struct fcgi *fcgi, int id, int as, int ps)
146 014c66b6 2023-06-25 op struct bufferevent *bev = fcgi->fcg_bev;
147 014c66b6 2023-06-25 op struct fcgi_header hdr;
148 014c66b6 2023-06-25 op struct fcgi_end_req_body end;
150 014c66b6 2023-06-25 op memset(&hdr, 0, sizeof(hdr));
151 014c66b6 2023-06-25 op memset(&end, 0, sizeof(end));
153 014c66b6 2023-06-25 op hdr.version = FCGI_VERSION_1;
154 014c66b6 2023-06-25 op hdr.type = FCGI_END_REQUEST;
155 014c66b6 2023-06-25 op hdr.req_id0 = (id & 0xFF);
156 014c66b6 2023-06-25 op hdr.req_id1 = (id >> 8);
157 014c66b6 2023-06-25 op hdr.content_len0 = sizeof(end);
159 014c66b6 2023-06-25 op end.app_status0 = (unsigned char)as;
160 014c66b6 2023-06-25 op end.proto_status = (unsigned char)ps;
162 014c66b6 2023-06-25 op if (bufferevent_write(bev, &hdr, sizeof(hdr)) == -1)
164 014c66b6 2023-06-25 op if (bufferevent_write(bev, &end, sizeof(end)) == -1)
170 014c66b6 2023-06-25 op end_request(struct client *clt, int status, int proto_status)
172 014c66b6 2023-06-25 op struct fcgi *fcgi = clt->clt_fcgi;
175 014c66b6 2023-06-25 op if (clt_flush(clt) == -1)
178 014c66b6 2023-06-25 op r = fcgi_send_end_req(fcgi, clt->clt_id, status,
179 014c66b6 2023-06-25 op proto_status);
180 014c66b6 2023-06-25 op if (r == -1) {
181 014c66b6 2023-06-25 op fcgi_error(fcgi->fcg_bev, EV_WRITE, fcgi);
185 014c66b6 2023-06-25 op SPLAY_REMOVE(client_tree, &fcgi->fcg_clients, clt);
186 014c66b6 2023-06-25 op server_client_free(clt);
188 014c66b6 2023-06-25 op if (!fcgi->fcg_keep_conn)
189 014c66b6 2023-06-25 op fcgi->fcg_done = 1;
195 014c66b6 2023-06-25 op fcgi_end_request(struct client *clt, int status)
197 014c66b6 2023-06-25 op return (end_request(clt, status, FCGI_REQUEST_COMPLETE));
201 014c66b6 2023-06-25 op fcgi_abort_request(struct client *clt)
203 014c66b6 2023-06-25 op return (end_request(clt, 1, FCGI_OVERLOADED));
207 014c66b6 2023-06-25 op fcgi_inflight_dec(const char *why)
209 014c66b6 2023-06-25 op fcgi_inflight--;
210 014c66b6 2023-06-25 op log_debug("%s: fcgi inflight decremented, now %d, %s",
211 014c66b6 2023-06-25 op __func__, fcgi_inflight, why);
215 014c66b6 2023-06-25 op fcgi_accept(int fd, short event, void *arg)
217 014c66b6 2023-06-25 op struct env *env = arg;
218 014c66b6 2023-06-25 op struct fcgi *fcgi = NULL;
219 014c66b6 2023-06-25 op socklen_t slen;
220 014c66b6 2023-06-25 op struct sockaddr_storage ss;
223 014c66b6 2023-06-25 op event_add(&env->env_pausev, NULL);
224 014c66b6 2023-06-25 op if ((event & EV_TIMEOUT))
227 014c66b6 2023-06-25 op slen = sizeof(ss);
228 014c66b6 2023-06-25 op if ((s = accept_reserve(env->env_sockfd, (struct sockaddr *)&ss,
229 014c66b6 2023-06-25 op &slen, FD_RESERVE, &fcgi_inflight)) == -1) {
231 014c66b6 2023-06-25 op * Pause accept if we are out of file descriptors, or
232 014c66b6 2023-06-25 op * libevent will haunt us here too.
234 014c66b6 2023-06-25 op if (errno == ENFILE || errno == EMFILE) {
235 014c66b6 2023-06-25 op struct timeval evtpause = { 1, 0 };
237 014c66b6 2023-06-25 op event_del(&env->env_sockev);
238 014c66b6 2023-06-25 op evtimer_add(&env->env_pausev, &evtpause);
239 014c66b6 2023-06-25 op log_debug("%s: deferring connections", __func__);
244 014c66b6 2023-06-25 op if ((fcgi = calloc(1, sizeof(*fcgi))) == NULL)
247 014c66b6 2023-06-25 op fcgi->fcg_id = ++fcgi_id;
248 014c66b6 2023-06-25 op fcgi->fcg_s = s;
249 014c66b6 2023-06-25 op fcgi->fcg_env = env;
250 014c66b6 2023-06-25 op fcgi->fcg_want = FCGI_RECORD_HEADER;
251 014c66b6 2023-06-25 op fcgi->fcg_toread = sizeof(struct fcgi_header);
252 014c66b6 2023-06-25 op SPLAY_INIT(&fcgi->fcg_clients);
254 014c66b6 2023-06-25 op /* assume it's enabled until we get a FCGI_BEGIN_REQUEST */
255 014c66b6 2023-06-25 op fcgi->fcg_keep_conn = 1;
257 014c66b6 2023-06-25 op fcgi->fcg_bev = bufferevent_new(fcgi->fcg_s, fcgi_read, fcgi_write,
258 014c66b6 2023-06-25 op fcgi_error, fcgi);
259 014c66b6 2023-06-25 op if (fcgi->fcg_bev == NULL)
262 014c66b6 2023-06-25 op bufferevent_enable(fcgi->fcg_bev, EV_READ | EV_WRITE);
266 014c66b6 2023-06-25 op if (s != -1) {
269 014c66b6 2023-06-25 op fcgi_inflight_dec(__func__);
274 014c66b6 2023-06-25 op parse_len(struct fcgi *fcgi, struct evbuffer *src)
276 014c66b6 2023-06-25 op unsigned char c, x[3];
278 014c66b6 2023-06-25 op fcgi->fcg_toread--;
279 014c66b6 2023-06-25 op evbuffer_remove(src, &c, 1);
280 014c66b6 2023-06-25 op if (c >> 7 == 0)
283 014c66b6 2023-06-25 op if (fcgi->fcg_toread < 3)
286 014c66b6 2023-06-25 op fcgi->fcg_toread -= 3;
287 014c66b6 2023-06-25 op evbuffer_remove(src, x, sizeof(x));
288 014c66b6 2023-06-25 op return (((c & 0x7F) << 24) | (x[0] << 16) | (x[1] << 8) | x[2]);
292 014c66b6 2023-06-25 op fcgi_parse_params(struct fcgi *fcgi, struct evbuffer *src, struct client *clt)
294 014c66b6 2023-06-25 op char pname[32];
295 014c66b6 2023-06-25 op char server[HOST_NAME_MAX + 1];
296 014c66b6 2023-06-25 op char path[PATH_MAX];
297 014c66b6 2023-06-25 op char query[GEMINI_MAXLEN];
298 014c66b6 2023-06-25 op char method[8];
299 014c66b6 2023-06-25 op int nlen, vlen;
301 014c66b6 2023-06-25 op while (fcgi->fcg_toread > 0) {
302 014c66b6 2023-06-25 op if ((nlen = parse_len(fcgi, src)) < 0 ||
303 014c66b6 2023-06-25 op (vlen = parse_len(fcgi, src)) < 0)
306 014c66b6 2023-06-25 op if (fcgi->fcg_toread < nlen + vlen)
309 014c66b6 2023-06-25 op if ((size_t)nlen > sizeof(pname) - 1) {
310 014c66b6 2023-06-25 op /* ignore this parameter */
311 014c66b6 2023-06-25 op fcgi->fcg_toread -= nlen - vlen;
312 014c66b6 2023-06-25 op evbuffer_drain(src, nlen + vlen);
316 014c66b6 2023-06-25 op fcgi->fcg_toread -= nlen;
317 014c66b6 2023-06-25 op evbuffer_remove(src, &pname, nlen);
318 014c66b6 2023-06-25 op pname[nlen] = '\0';
320 014c66b6 2023-06-25 op if (!strcmp(pname, "SERVER_NAME") &&
321 014c66b6 2023-06-25 op (size_t)vlen < sizeof(server)) {
322 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
323 014c66b6 2023-06-25 op evbuffer_remove(src, &server, vlen);
324 014c66b6 2023-06-25 op server[vlen] = '\0';
326 014c66b6 2023-06-25 op free(clt->clt_server_name);
327 014c66b6 2023-06-25 op if ((clt->clt_server_name = strdup(server)) == NULL)
329 014c66b6 2023-06-25 op DPRINTF("clt %d: server_name: %s", clt->clt_id,
330 014c66b6 2023-06-25 op clt->clt_server_name);
334 014c66b6 2023-06-25 op if (!strcmp(pname, "SCRIPT_NAME") &&
335 014c66b6 2023-06-25 op (size_t)vlen < sizeof(path)) {
336 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
337 014c66b6 2023-06-25 op evbuffer_remove(src, &path, vlen);
338 014c66b6 2023-06-25 op path[vlen] = '\0';
340 014c66b6 2023-06-25 op free(clt->clt_script_name);
341 dd58c961 2023-08-08 op clt->clt_script_name = strdup(path);
342 014c66b6 2023-06-25 op if (clt->clt_script_name == NULL)
345 014c66b6 2023-06-25 op DPRINTF("clt %d: script_name: %s", clt->clt_id,
346 014c66b6 2023-06-25 op clt->clt_script_name);
350 505e5764 2023-08-08 op if (!strcmp(pname, "PATH_INFO") &&
351 014c66b6 2023-06-25 op (size_t)vlen < sizeof(path)) {
352 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
353 014c66b6 2023-06-25 op evbuffer_remove(src, &path, vlen);
354 014c66b6 2023-06-25 op path[vlen] = '\0';
356 014c66b6 2023-06-25 op free(clt->clt_path_info);
357 dd58c961 2023-08-08 op clt->clt_path_info = strdup(path);
358 014c66b6 2023-06-25 op if (clt->clt_path_info == NULL)
361 014c66b6 2023-06-25 op DPRINTF("clt %d: path_info: %s", clt->clt_id,
362 014c66b6 2023-06-25 op clt->clt_path_info);
366 014c66b6 2023-06-25 op if (!strcmp(pname, "QUERY_STRING") &&
367 014c66b6 2023-06-25 op (size_t)vlen < sizeof(query) &&
369 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
370 014c66b6 2023-06-25 op evbuffer_remove(src, &query, vlen);
371 014c66b6 2023-06-25 op query[vlen] = '\0';
373 014c66b6 2023-06-25 op free(clt->clt_query);
374 014c66b6 2023-06-25 op if ((clt->clt_query = strdup(query)) == NULL)
377 014c66b6 2023-06-25 op DPRINTF("clt %d: query: %s", clt->clt_id,
378 014c66b6 2023-06-25 op clt->clt_query);
382 014c66b6 2023-06-25 op if (!strcmp(pname, "REQUEST_METHOD") &&
383 014c66b6 2023-06-25 op (size_t)vlen < sizeof(method)) {
384 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
385 014c66b6 2023-06-25 op evbuffer_remove(src, &method, vlen);
386 014c66b6 2023-06-25 op method[vlen] = '\0';
388 014c66b6 2023-06-25 op if (!strcasecmp(method, "GET"))
389 014c66b6 2023-06-25 op clt->clt_method = METHOD_GET;
390 014c66b6 2023-06-25 op if (!strcasecmp(method, "POST"))
391 014c66b6 2023-06-25 op clt->clt_method = METHOD_POST;
396 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
397 014c66b6 2023-06-25 op evbuffer_drain(src, vlen);
404 014c66b6 2023-06-25 op fcgi_read(struct bufferevent *bev, void *d)
406 014c66b6 2023-06-25 op struct fcgi *fcgi = d;
407 014c66b6 2023-06-25 op struct env *env = fcgi->fcg_env;
408 014c66b6 2023-06-25 op struct evbuffer *src = EVBUFFER_INPUT(bev);
409 014c66b6 2023-06-25 op struct fcgi_header hdr;
410 014c66b6 2023-06-25 op struct fcgi_begin_req breq;
411 014c66b6 2023-06-25 op struct client *clt, q;
414 014c66b6 2023-06-25 op memset(&q, 0, sizeof(q));
417 014c66b6 2023-06-25 op if (EVBUFFER_LENGTH(src) < (size_t)fcgi->fcg_toread)
420 014c66b6 2023-06-25 op if (fcgi->fcg_want == FCGI_RECORD_HEADER) {
421 014c66b6 2023-06-25 op fcgi->fcg_want = FCGI_RECORD_BODY;
422 014c66b6 2023-06-25 op bufferevent_read(bev, &hdr, sizeof(hdr));
424 014c66b6 2023-06-25 op DPRINTF("header: v=%d t=%d id=%d len=%d p=%d",
425 014c66b6 2023-06-25 op hdr.version, hdr.type,
426 014c66b6 2023-06-25 op CAT(hdr.req_id0, hdr.req_id1),
427 014c66b6 2023-06-25 op CAT(hdr.content_len0, hdr.content_len1),
428 014c66b6 2023-06-25 op hdr.padding);
430 014c66b6 2023-06-25 op if (hdr.version != FCGI_VERSION_1) {
431 014c66b6 2023-06-25 op log_warnx("unknown fastcgi version: %d",
432 014c66b6 2023-06-25 op hdr.version);
433 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
437 014c66b6 2023-06-25 op fcgi->fcg_toread = CAT(hdr.content_len0,
438 014c66b6 2023-06-25 op hdr.content_len1);
439 014c66b6 2023-06-25 op if (fcgi->fcg_toread < 0) {
440 014c66b6 2023-06-25 op log_warnx("invalid record length: %d",
441 014c66b6 2023-06-25 op fcgi->fcg_toread);
442 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
446 014c66b6 2023-06-25 op fcgi->fcg_padding = hdr.padding;
447 014c66b6 2023-06-25 op if (fcgi->fcg_padding < 0) {
448 014c66b6 2023-06-25 op log_warnx("invalid padding: %d",
449 014c66b6 2023-06-25 op fcgi->fcg_padding);
450 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
454 014c66b6 2023-06-25 op fcgi->fcg_type = hdr.type;
455 014c66b6 2023-06-25 op fcgi->fcg_rec_id = CAT(hdr.req_id0, hdr.req_id1);
459 014c66b6 2023-06-25 op q.clt_id = fcgi->fcg_rec_id;
460 014c66b6 2023-06-25 op clt = SPLAY_FIND(client_tree, &fcgi->fcg_clients, &q);
462 014c66b6 2023-06-25 op switch (fcgi->fcg_type) {
463 014c66b6 2023-06-25 op case FCGI_BEGIN_REQUEST:
464 014c66b6 2023-06-25 op if (sizeof(breq) != fcgi->fcg_toread) {
465 014c66b6 2023-06-25 op log_warnx("unexpected size for "
466 014c66b6 2023-06-25 op "FCGI_BEGIN_REQUEST");
467 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
471 014c66b6 2023-06-25 op evbuffer_remove(src, &breq, sizeof(breq));
473 014c66b6 2023-06-25 op role = CAT(breq.role0, breq.role1);
474 014c66b6 2023-06-25 op if (role != FCGI_RESPONDER) {
475 014c66b6 2023-06-25 op log_warnx("unknown fastcgi role: %d",
477 014c66b6 2023-06-25 op if (fcgi_send_end_req(fcgi, fcgi->fcg_rec_id,
478 014c66b6 2023-06-25 op 1, FCGI_UNKNOWN_ROLE) == -1) {
479 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
485 014c66b6 2023-06-25 op if (!fcgi->fcg_keep_conn) {
486 014c66b6 2023-06-25 op log_warnx("trying to reuse the fastcgi "
487 014c66b6 2023-06-25 op "socket without marking it as so.");
488 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
491 014c66b6 2023-06-25 op fcgi->fcg_keep_conn = breq.flags & FCGI_KEEP_CONN;
493 014c66b6 2023-06-25 op if (clt != NULL) {
494 014c66b6 2023-06-25 op log_warnx("ignoring attemp to re-use an "
495 014c66b6 2023-06-25 op "active request id (%d)",
496 014c66b6 2023-06-25 op fcgi->fcg_rec_id);
500 014c66b6 2023-06-25 op if ((clt = calloc(1, sizeof(*clt))) == NULL) {
501 014c66b6 2023-06-25 op log_warnx("calloc");
506 014c66b6 2023-06-25 op clt->clt_tp = template(clt, clt_tp_puts, clt_tp_putc);
507 014c66b6 2023-06-25 op if (clt->clt_tp == NULL) {
509 014c66b6 2023-06-25 op log_warn("template");
514 014c66b6 2023-06-25 op clt->clt_id = fcgi->fcg_rec_id;
515 014c66b6 2023-06-25 op clt->clt_fd = -1;
516 014c66b6 2023-06-25 op clt->clt_fcgi = fcgi;
517 014c66b6 2023-06-25 op SPLAY_INSERT(client_tree, &fcgi->fcg_clients, clt);
519 014c66b6 2023-06-25 op case FCGI_PARAMS:
520 014c66b6 2023-06-25 op if (clt == NULL) {
521 014c66b6 2023-06-25 op log_warnx("got FCGI_PARAMS for inactive id "
522 014c66b6 2023-06-25 op "(%d)", fcgi->fcg_rec_id);
523 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_toread);
526 014c66b6 2023-06-25 op if (fcgi->fcg_toread == 0) {
527 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_toread);
528 014c66b6 2023-06-25 op if (server_handle(env, clt) == -1)
532 014c66b6 2023-06-25 op if (fcgi_parse_params(fcgi, src, clt) == -1) {
533 014c66b6 2023-06-25 op log_warnx("fcgi_parse_params failed");
534 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
538 014c66b6 2023-06-25 op case FCGI_STDIN:
539 014c66b6 2023-06-25 op /* not interested in reading stdin */
540 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_toread);
542 014c66b6 2023-06-25 op case FCGI_ABORT_REQUEST:
543 014c66b6 2023-06-25 op if (clt == NULL) {
544 014c66b6 2023-06-25 op log_warnx("got FCGI_ABORT_REQUEST for inactive"
545 014c66b6 2023-06-25 op " id (%d)", fcgi->fcg_rec_id);
546 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_toread);
549 014c66b6 2023-06-25 op if (fcgi_end_request(clt, 1) == -1) {
550 014c66b6 2023-06-25 op /* calls fcgi_error on failure */
555 014c66b6 2023-06-25 op log_warnx("unknown fastcgi record type %d",
556 014c66b6 2023-06-25 op fcgi->fcg_type);
557 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_toread);
561 014c66b6 2023-06-25 op /* Prepare for the next record. */
562 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_padding);
563 014c66b6 2023-06-25 op fcgi->fcg_want = FCGI_RECORD_HEADER;
564 014c66b6 2023-06-25 op fcgi->fcg_toread = sizeof(struct fcgi_header);
569 014c66b6 2023-06-25 op fcgi_write(struct bufferevent *bev, void *d)
571 014c66b6 2023-06-25 op struct fcgi *fcgi = d;
572 014c66b6 2023-06-25 op struct evbuffer *out = EVBUFFER_OUTPUT(bev);
574 014c66b6 2023-06-25 op if (fcgi->fcg_done && EVBUFFER_LENGTH(out) == 0)
575 014c66b6 2023-06-25 op fcgi_error(bev, EVBUFFER_EOF, fcgi);
579 014c66b6 2023-06-25 op fcgi_error(struct bufferevent *bev, short event, void *d)
581 014c66b6 2023-06-25 op struct fcgi *fcgi = d;
582 014c66b6 2023-06-25 op struct env *env = fcgi->fcg_env;
583 014c66b6 2023-06-25 op struct client *clt;
585 014c66b6 2023-06-25 op log_debug("fcgi failure, shutting down connection (ev: %x)",
587 014c66b6 2023-06-25 op fcgi_inflight_dec(__func__);
589 014c66b6 2023-06-25 op while ((clt = SPLAY_MIN(client_tree, &fcgi->fcg_clients)) != NULL) {
590 014c66b6 2023-06-25 op SPLAY_REMOVE(client_tree, &fcgi->fcg_clients, clt);
591 014c66b6 2023-06-25 op server_client_free(clt);
594 014c66b6 2023-06-25 op SPLAY_REMOVE(fcgi_tree, &env->env_fcgi_socks, fcgi);
595 014c66b6 2023-06-25 op fcgi_free(fcgi);
601 014c66b6 2023-06-25 op fcgi_free(struct fcgi *fcgi)
603 014c66b6 2023-06-25 op close(fcgi->fcg_s);
604 014c66b6 2023-06-25 op bufferevent_free(fcgi->fcg_bev);
609 014c66b6 2023-06-25 op clt_flush(struct client *clt)
611 014c66b6 2023-06-25 op struct fcgi *fcgi = clt->clt_fcgi;
612 014c66b6 2023-06-25 op struct bufferevent *bev = fcgi->fcg_bev;
613 014c66b6 2023-06-25 op struct fcgi_header hdr;
615 014c66b6 2023-06-25 op if (clt->clt_buflen == 0)
618 014c66b6 2023-06-25 op memset(&hdr, 0, sizeof(hdr));
619 014c66b6 2023-06-25 op hdr.version = FCGI_VERSION_1;
620 014c66b6 2023-06-25 op hdr.type = FCGI_STDOUT;
621 014c66b6 2023-06-25 op hdr.req_id0 = (clt->clt_id & 0xFF);
622 014c66b6 2023-06-25 op hdr.req_id1 = (clt->clt_id >> 8);
623 014c66b6 2023-06-25 op hdr.content_len0 = (clt->clt_buflen & 0xFF);
624 014c66b6 2023-06-25 op hdr.content_len1 = (clt->clt_buflen >> 8);
626 014c66b6 2023-06-25 op if (bufferevent_write(bev, &hdr, sizeof(hdr)) == -1 ||
627 014c66b6 2023-06-25 op bufferevent_write(bev, clt->clt_buf, clt->clt_buflen) == -1) {
628 014c66b6 2023-06-25 op fcgi_error(bev, EV_WRITE, fcgi);
632 014c66b6 2023-06-25 op clt->clt_buflen = 0;
638 014c66b6 2023-06-25 op clt_write(struct client *clt, const uint8_t *buf, size_t len)
640 014c66b6 2023-06-25 op size_t left, copy;
642 014c66b6 2023-06-25 op while (len > 0) {
643 014c66b6 2023-06-25 op left = sizeof(clt->clt_buf) - clt->clt_buflen;
644 014c66b6 2023-06-25 op if (left == 0) {
645 014c66b6 2023-06-25 op if (clt_flush(clt) == -1)
647 014c66b6 2023-06-25 op left = sizeof(clt->clt_buf);
650 014c66b6 2023-06-25 op copy = MIN(left, len);
652 014c66b6 2023-06-25 op memcpy(&clt->clt_buf[clt->clt_buflen], buf, copy);
653 014c66b6 2023-06-25 op clt->clt_buflen += copy;
662 014c66b6 2023-06-25 op clt_putc(struct client *clt, char ch)
664 014c66b6 2023-06-25 op return (clt_write(clt, &ch, 1));
668 014c66b6 2023-06-25 op clt_puts(struct client *clt, const char *str)
670 014c66b6 2023-06-25 op return (clt_write(clt, str, strlen(str)));
674 014c66b6 2023-06-25 op clt_write_bufferevent(struct client *clt, struct bufferevent *bev)
676 014c66b6 2023-06-25 op struct evbuffer *src = EVBUFFER_INPUT(bev);
677 014c66b6 2023-06-25 op size_t len, left, copy;
679 014c66b6 2023-06-25 op len = EVBUFFER_LENGTH(src);
680 014c66b6 2023-06-25 op while (len > 0) {
681 014c66b6 2023-06-25 op left = sizeof(clt->clt_buf) - clt->clt_buflen;
682 014c66b6 2023-06-25 op if (left == 0) {
683 014c66b6 2023-06-25 op if (clt_flush(clt) == -1)
685 014c66b6 2023-06-25 op left = sizeof(clt->clt_buf);
688 014c66b6 2023-06-25 op copy = bufferevent_read(bev, &clt->clt_buf[clt->clt_buflen],
689 014c66b6 2023-06-25 op MIN(left, len));
690 014c66b6 2023-06-25 op clt->clt_buflen += copy;
692 014c66b6 2023-06-25 op len = EVBUFFER_LENGTH(src);
699 014c66b6 2023-06-25 op clt_printf(struct client *clt, const char *fmt, ...)
701 014c66b6 2023-06-25 op struct fcgi *fcgi = clt->clt_fcgi;
702 014c66b6 2023-06-25 op struct bufferevent *bev = fcgi->fcg_bev;
707 014c66b6 2023-06-25 op va_start(ap, fmt);
708 014c66b6 2023-06-25 op r = vasprintf(&str, fmt, ap);
710 014c66b6 2023-06-25 op if (r == -1) {
711 014c66b6 2023-06-25 op fcgi_error(bev, EV_WRITE, fcgi);
715 014c66b6 2023-06-25 op r = clt_write(clt, str, r);
722 014c66b6 2023-06-25 op clt_tp_puts(struct template *tp, const char *str)
724 014c66b6 2023-06-25 op struct client *clt = tp->tp_arg;
726 014c66b6 2023-06-25 op if (clt_puts(clt, str) == -1)
733 014c66b6 2023-06-25 op clt_tp_putc(struct template *tp, int c)
735 014c66b6 2023-06-25 op struct client *clt = tp->tp_arg;
737 014c66b6 2023-06-25 op if (clt_putc(clt, c) == -1)
745 014c66b6 2023-06-25 op fcgi_cmp(struct fcgi *a, struct fcgi *b)
747 014c66b6 2023-06-25 op return ((int)a->fcg_id - b->fcg_id);
751 014c66b6 2023-06-25 op fcgi_client_cmp(struct client *a, struct client *b)
753 014c66b6 2023-06-25 op return ((int)a->clt_id - b->clt_id);
757 014c66b6 2023-06-25 op accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
758 014c66b6 2023-06-25 op int reserve, volatile int *counter)
762 014c66b6 2023-06-25 op if (getdtablecount() + reserve + *counter >= getdtablesize()) {
763 014c66b6 2023-06-25 op errno = EMFILE;
767 014c66b6 2023-06-25 op if ((ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK)) > -1) {
768 014c66b6 2023-06-25 op (*counter)++;
769 014c66b6 2023-06-25 op log_debug("%s: inflight incremented, now %d", __func__,
773 014c66b6 2023-06-25 op return (ret);
776 014c66b6 2023-06-25 op SPLAY_GENERATE(fcgi_tree, fcgi, fcg_nodes, fcgi_cmp);
777 014c66b6 2023-06-25 op SPLAY_GENERATE(client_tree, client, clt_nodes, fcgi_client_cmp);