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"
36 014c66b6 2023-06-25 op #include "tmpl.h"
38 014c66b6 2023-06-25 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
40 014c66b6 2023-06-25 op struct fcgi_header {
41 014c66b6 2023-06-25 op unsigned char version;
42 014c66b6 2023-06-25 op unsigned char type;
43 014c66b6 2023-06-25 op unsigned char req_id1;
44 014c66b6 2023-06-25 op unsigned char req_id0;
45 014c66b6 2023-06-25 op unsigned char content_len1;
46 014c66b6 2023-06-25 op unsigned char content_len0;
47 014c66b6 2023-06-25 op unsigned char padding;
48 014c66b6 2023-06-25 op unsigned char reserved;
49 014c66b6 2023-06-25 op } __attribute__((packed));
52 014c66b6 2023-06-25 op * number of bytes in a FCGI_HEADER. Future version of the protocol
53 014c66b6 2023-06-25 op * will not reduce this number.
55 014c66b6 2023-06-25 op #define FCGI_HEADER_LEN 8
58 014c66b6 2023-06-25 op * values for the version component
60 014c66b6 2023-06-25 op #define FCGI_VERSION_1 1
63 014c66b6 2023-06-25 op * values for the type component
65 014c66b6 2023-06-25 op #define FCGI_BEGIN_REQUEST 1
66 014c66b6 2023-06-25 op #define FCGI_ABORT_REQUEST 2
67 014c66b6 2023-06-25 op #define FCGI_END_REQUEST 3
68 014c66b6 2023-06-25 op #define FCGI_PARAMS 4
69 014c66b6 2023-06-25 op #define FCGI_STDIN 5
70 014c66b6 2023-06-25 op #define FCGI_STDOUT 6
71 014c66b6 2023-06-25 op #define FCGI_STDERR 7
72 014c66b6 2023-06-25 op #define FCGI_DATA 8
73 014c66b6 2023-06-25 op #define FCGI_GET_VALUES 9
74 014c66b6 2023-06-25 op #define FCGI_GET_VALUES_RESULT 10
75 014c66b6 2023-06-25 op #define FCGI_UNKNOWN_TYPE 11
76 014c66b6 2023-06-25 op #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
78 014c66b6 2023-06-25 op struct fcgi_begin_req {
79 014c66b6 2023-06-25 op unsigned char role1;
80 014c66b6 2023-06-25 op unsigned char role0;
81 014c66b6 2023-06-25 op unsigned char flags;
82 014c66b6 2023-06-25 op unsigned char reserved[5];
85 014c66b6 2023-06-25 op struct fcgi_begin_req_record {
86 014c66b6 2023-06-25 op struct fcgi_header header;
87 014c66b6 2023-06-25 op struct fcgi_begin_req body;
91 014c66b6 2023-06-25 op * mask for flags;
93 014c66b6 2023-06-25 op #define FCGI_KEEP_CONN 1
96 014c66b6 2023-06-25 op * values for the role
98 014c66b6 2023-06-25 op #define FCGI_RESPONDER 1
99 014c66b6 2023-06-25 op #define FCGI_AUTHORIZER 2
100 014c66b6 2023-06-25 op #define FCGI_FILTER 3
102 014c66b6 2023-06-25 op struct fcgi_end_req_body {
103 014c66b6 2023-06-25 op unsigned char app_status3;
104 014c66b6 2023-06-25 op unsigned char app_status2;
105 014c66b6 2023-06-25 op unsigned char app_status1;
106 014c66b6 2023-06-25 op unsigned char app_status0;
107 014c66b6 2023-06-25 op unsigned char proto_status;
108 014c66b6 2023-06-25 op unsigned char reserved[3];
112 014c66b6 2023-06-25 op * values for proto_status
114 014c66b6 2023-06-25 op #define FCGI_REQUEST_COMPLETE 0
115 014c66b6 2023-06-25 op #define FCGI_CANT_MPX_CONN 1
116 014c66b6 2023-06-25 op #define FCGI_OVERLOADED 2
117 014c66b6 2023-06-25 op #define FCGI_UNKNOWN_ROLE 3
120 014c66b6 2023-06-25 op * Variable names for FCGI_GET_VALUES / FCGI_GET_VALUES_RESULT
123 014c66b6 2023-06-25 op #define FCGI_MAX_CONNS "FCGI_MAX_CONNS"
124 014c66b6 2023-06-25 op #define FCGI_MAX_REQS "FCGI_MAX_REQS"
125 014c66b6 2023-06-25 op #define FCGI_MPXS_CONNS "FCGI_MPXS_CONNS"
127 014c66b6 2023-06-25 op #define CAT(f0, f1) ((f0) + ((f1) << 8))
130 014c66b6 2023-06-25 op FCGI_RECORD_HEADER,
131 014c66b6 2023-06-25 op FCGI_RECORD_BODY,
134 014c66b6 2023-06-25 op volatile int fcgi_inflight;
135 014c66b6 2023-06-25 op int32_t fcgi_id;
137 014c66b6 2023-06-25 op int accept_reserve(int, struct sockaddr *, socklen_t *, int,
138 014c66b6 2023-06-25 op volatile int *);
141 014c66b6 2023-06-25 op fcgi_send_end_req(struct fcgi *fcgi, int id, int as, int ps)
143 014c66b6 2023-06-25 op struct bufferevent *bev = fcgi->fcg_bev;
144 014c66b6 2023-06-25 op struct fcgi_header hdr;
145 014c66b6 2023-06-25 op struct fcgi_end_req_body end;
147 014c66b6 2023-06-25 op memset(&hdr, 0, sizeof(hdr));
148 014c66b6 2023-06-25 op memset(&end, 0, sizeof(end));
150 014c66b6 2023-06-25 op hdr.version = FCGI_VERSION_1;
151 014c66b6 2023-06-25 op hdr.type = FCGI_END_REQUEST;
152 014c66b6 2023-06-25 op hdr.req_id0 = (id & 0xFF);
153 014c66b6 2023-06-25 op hdr.req_id1 = (id >> 8);
154 014c66b6 2023-06-25 op hdr.content_len0 = sizeof(end);
156 014c66b6 2023-06-25 op end.app_status0 = (unsigned char)as;
157 014c66b6 2023-06-25 op end.proto_status = (unsigned char)ps;
159 014c66b6 2023-06-25 op if (bufferevent_write(bev, &hdr, sizeof(hdr)) == -1)
161 014c66b6 2023-06-25 op if (bufferevent_write(bev, &end, sizeof(end)) == -1)
167 014c66b6 2023-06-25 op end_request(struct client *clt, int status, int proto_status)
169 014c66b6 2023-06-25 op struct fcgi *fcgi = clt->clt_fcgi;
172 014c66b6 2023-06-25 op if (clt_flush(clt) == -1)
175 014c66b6 2023-06-25 op r = fcgi_send_end_req(fcgi, clt->clt_id, status,
176 014c66b6 2023-06-25 op proto_status);
177 014c66b6 2023-06-25 op if (r == -1) {
178 014c66b6 2023-06-25 op fcgi_error(fcgi->fcg_bev, EV_WRITE, fcgi);
182 014c66b6 2023-06-25 op SPLAY_REMOVE(client_tree, &fcgi->fcg_clients, clt);
183 014c66b6 2023-06-25 op server_client_free(clt);
185 014c66b6 2023-06-25 op if (!fcgi->fcg_keep_conn)
186 014c66b6 2023-06-25 op fcgi->fcg_done = 1;
192 014c66b6 2023-06-25 op fcgi_end_request(struct client *clt, int status)
194 014c66b6 2023-06-25 op return (end_request(clt, status, FCGI_REQUEST_COMPLETE));
198 014c66b6 2023-06-25 op fcgi_abort_request(struct client *clt)
200 014c66b6 2023-06-25 op return (end_request(clt, 1, FCGI_OVERLOADED));
204 014c66b6 2023-06-25 op fcgi_inflight_dec(const char *why)
206 014c66b6 2023-06-25 op fcgi_inflight--;
207 014c66b6 2023-06-25 op log_debug("%s: fcgi inflight decremented, now %d, %s",
208 014c66b6 2023-06-25 op __func__, fcgi_inflight, why);
212 014c66b6 2023-06-25 op fcgi_accept(int fd, short event, void *arg)
214 014c66b6 2023-06-25 op struct env *env = arg;
215 014c66b6 2023-06-25 op struct fcgi *fcgi = NULL;
216 014c66b6 2023-06-25 op socklen_t slen;
217 014c66b6 2023-06-25 op struct sockaddr_storage ss;
220 014c66b6 2023-06-25 op event_add(&env->env_pausev, NULL);
221 014c66b6 2023-06-25 op if ((event & EV_TIMEOUT))
224 014c66b6 2023-06-25 op slen = sizeof(ss);
225 014c66b6 2023-06-25 op if ((s = accept_reserve(env->env_sockfd, (struct sockaddr *)&ss,
226 014c66b6 2023-06-25 op &slen, FD_RESERVE, &fcgi_inflight)) == -1) {
228 014c66b6 2023-06-25 op * Pause accept if we are out of file descriptors, or
229 014c66b6 2023-06-25 op * libevent will haunt us here too.
231 014c66b6 2023-06-25 op if (errno == ENFILE || errno == EMFILE) {
232 014c66b6 2023-06-25 op struct timeval evtpause = { 1, 0 };
234 014c66b6 2023-06-25 op event_del(&env->env_sockev);
235 014c66b6 2023-06-25 op evtimer_add(&env->env_pausev, &evtpause);
236 014c66b6 2023-06-25 op log_debug("%s: deferring connections", __func__);
241 014c66b6 2023-06-25 op if ((fcgi = calloc(1, sizeof(*fcgi))) == NULL)
244 014c66b6 2023-06-25 op fcgi->fcg_id = ++fcgi_id;
245 014c66b6 2023-06-25 op fcgi->fcg_s = s;
246 014c66b6 2023-06-25 op fcgi->fcg_env = env;
247 014c66b6 2023-06-25 op fcgi->fcg_want = FCGI_RECORD_HEADER;
248 014c66b6 2023-06-25 op fcgi->fcg_toread = sizeof(struct fcgi_header);
249 014c66b6 2023-06-25 op SPLAY_INIT(&fcgi->fcg_clients);
251 014c66b6 2023-06-25 op /* assume it's enabled until we get a FCGI_BEGIN_REQUEST */
252 014c66b6 2023-06-25 op fcgi->fcg_keep_conn = 1;
254 014c66b6 2023-06-25 op fcgi->fcg_bev = bufferevent_new(fcgi->fcg_s, fcgi_read, fcgi_write,
255 014c66b6 2023-06-25 op fcgi_error, fcgi);
256 014c66b6 2023-06-25 op if (fcgi->fcg_bev == NULL)
259 014c66b6 2023-06-25 op bufferevent_enable(fcgi->fcg_bev, EV_READ | EV_WRITE);
263 014c66b6 2023-06-25 op if (s != -1) {
266 014c66b6 2023-06-25 op fcgi_inflight_dec(__func__);
271 014c66b6 2023-06-25 op parse_len(struct fcgi *fcgi, struct evbuffer *src)
273 014c66b6 2023-06-25 op unsigned char c, x[3];
275 014c66b6 2023-06-25 op fcgi->fcg_toread--;
276 014c66b6 2023-06-25 op evbuffer_remove(src, &c, 1);
277 014c66b6 2023-06-25 op if (c >> 7 == 0)
280 014c66b6 2023-06-25 op if (fcgi->fcg_toread < 3)
283 014c66b6 2023-06-25 op fcgi->fcg_toread -= 3;
284 014c66b6 2023-06-25 op evbuffer_remove(src, x, sizeof(x));
285 014c66b6 2023-06-25 op return (((c & 0x7F) << 24) | (x[0] << 16) | (x[1] << 8) | x[2]);
289 014c66b6 2023-06-25 op fcgi_parse_params(struct fcgi *fcgi, struct evbuffer *src, struct client *clt)
291 014c66b6 2023-06-25 op char pname[32];
292 014c66b6 2023-06-25 op char server[HOST_NAME_MAX + 1];
293 014c66b6 2023-06-25 op char path[PATH_MAX];
294 014c66b6 2023-06-25 op char query[GEMINI_MAXLEN];
295 014c66b6 2023-06-25 op char method[8];
296 014c66b6 2023-06-25 op int nlen, vlen;
298 014c66b6 2023-06-25 op while (fcgi->fcg_toread > 0) {
299 014c66b6 2023-06-25 op if ((nlen = parse_len(fcgi, src)) < 0 ||
300 014c66b6 2023-06-25 op (vlen = parse_len(fcgi, src)) < 0)
303 014c66b6 2023-06-25 op if (fcgi->fcg_toread < nlen + vlen)
306 014c66b6 2023-06-25 op if ((size_t)nlen > sizeof(pname) - 1) {
307 014c66b6 2023-06-25 op /* ignore this parameter */
308 014c66b6 2023-06-25 op fcgi->fcg_toread -= nlen - vlen;
309 014c66b6 2023-06-25 op evbuffer_drain(src, nlen + vlen);
313 014c66b6 2023-06-25 op fcgi->fcg_toread -= nlen;
314 014c66b6 2023-06-25 op evbuffer_remove(src, &pname, nlen);
315 014c66b6 2023-06-25 op pname[nlen] = '\0';
317 014c66b6 2023-06-25 op if (!strcmp(pname, "SERVER_NAME") &&
318 014c66b6 2023-06-25 op (size_t)vlen < sizeof(server)) {
319 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
320 014c66b6 2023-06-25 op evbuffer_remove(src, &server, vlen);
321 014c66b6 2023-06-25 op server[vlen] = '\0';
323 014c66b6 2023-06-25 op free(clt->clt_server_name);
324 014c66b6 2023-06-25 op if ((clt->clt_server_name = strdup(server)) == NULL)
326 014c66b6 2023-06-25 op DPRINTF("clt %d: server_name: %s", clt->clt_id,
327 014c66b6 2023-06-25 op clt->clt_server_name);
331 014c66b6 2023-06-25 op if (!strcmp(pname, "SCRIPT_NAME") &&
332 014c66b6 2023-06-25 op (size_t)vlen < sizeof(path)) {
333 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
334 014c66b6 2023-06-25 op evbuffer_remove(src, &path, vlen);
335 014c66b6 2023-06-25 op path[vlen] = '\0';
337 014c66b6 2023-06-25 op free(clt->clt_script_name);
338 dd58c961 2023-08-08 op clt->clt_script_name = strdup(path);
339 014c66b6 2023-06-25 op if (clt->clt_script_name == NULL)
342 014c66b6 2023-06-25 op DPRINTF("clt %d: script_name: %s", clt->clt_id,
343 014c66b6 2023-06-25 op clt->clt_script_name);
347 505e5764 2023-08-08 op if (!strcmp(pname, "PATH_INFO") &&
348 014c66b6 2023-06-25 op (size_t)vlen < sizeof(path)) {
349 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
350 014c66b6 2023-06-25 op evbuffer_remove(src, &path, vlen);
351 014c66b6 2023-06-25 op path[vlen] = '\0';
353 014c66b6 2023-06-25 op free(clt->clt_path_info);
354 dd58c961 2023-08-08 op clt->clt_path_info = strdup(path);
355 014c66b6 2023-06-25 op if (clt->clt_path_info == NULL)
358 014c66b6 2023-06-25 op DPRINTF("clt %d: path_info: %s", clt->clt_id,
359 014c66b6 2023-06-25 op clt->clt_path_info);
363 014c66b6 2023-06-25 op if (!strcmp(pname, "QUERY_STRING") &&
364 014c66b6 2023-06-25 op (size_t)vlen < sizeof(query) &&
366 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
367 014c66b6 2023-06-25 op evbuffer_remove(src, &query, vlen);
368 014c66b6 2023-06-25 op query[vlen] = '\0';
370 014c66b6 2023-06-25 op free(clt->clt_query);
371 014c66b6 2023-06-25 op if ((clt->clt_query = strdup(query)) == NULL)
374 014c66b6 2023-06-25 op DPRINTF("clt %d: query: %s", clt->clt_id,
375 014c66b6 2023-06-25 op clt->clt_query);
379 014c66b6 2023-06-25 op if (!strcmp(pname, "REQUEST_METHOD") &&
380 014c66b6 2023-06-25 op (size_t)vlen < sizeof(method)) {
381 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
382 014c66b6 2023-06-25 op evbuffer_remove(src, &method, vlen);
383 014c66b6 2023-06-25 op method[vlen] = '\0';
385 014c66b6 2023-06-25 op if (!strcasecmp(method, "GET"))
386 014c66b6 2023-06-25 op clt->clt_method = METHOD_GET;
387 014c66b6 2023-06-25 op if (!strcasecmp(method, "POST"))
388 014c66b6 2023-06-25 op clt->clt_method = METHOD_POST;
393 014c66b6 2023-06-25 op fcgi->fcg_toread -= vlen;
394 014c66b6 2023-06-25 op evbuffer_drain(src, vlen);
401 014c66b6 2023-06-25 op fcgi_read(struct bufferevent *bev, void *d)
403 014c66b6 2023-06-25 op struct fcgi *fcgi = d;
404 014c66b6 2023-06-25 op struct env *env = fcgi->fcg_env;
405 014c66b6 2023-06-25 op struct evbuffer *src = EVBUFFER_INPUT(bev);
406 014c66b6 2023-06-25 op struct fcgi_header hdr;
407 014c66b6 2023-06-25 op struct fcgi_begin_req breq;
408 014c66b6 2023-06-25 op struct client *clt, q;
411 014c66b6 2023-06-25 op memset(&q, 0, sizeof(q));
414 014c66b6 2023-06-25 op if (EVBUFFER_LENGTH(src) < (size_t)fcgi->fcg_toread)
417 014c66b6 2023-06-25 op if (fcgi->fcg_want == FCGI_RECORD_HEADER) {
418 014c66b6 2023-06-25 op fcgi->fcg_want = FCGI_RECORD_BODY;
419 014c66b6 2023-06-25 op bufferevent_read(bev, &hdr, sizeof(hdr));
421 014c66b6 2023-06-25 op DPRINTF("header: v=%d t=%d id=%d len=%d p=%d",
422 014c66b6 2023-06-25 op hdr.version, hdr.type,
423 014c66b6 2023-06-25 op CAT(hdr.req_id0, hdr.req_id1),
424 014c66b6 2023-06-25 op CAT(hdr.content_len0, hdr.content_len1),
425 014c66b6 2023-06-25 op hdr.padding);
427 014c66b6 2023-06-25 op if (hdr.version != FCGI_VERSION_1) {
428 014c66b6 2023-06-25 op log_warnx("unknown fastcgi version: %d",
429 014c66b6 2023-06-25 op hdr.version);
430 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
434 014c66b6 2023-06-25 op fcgi->fcg_toread = CAT(hdr.content_len0,
435 014c66b6 2023-06-25 op hdr.content_len1);
436 014c66b6 2023-06-25 op if (fcgi->fcg_toread < 0) {
437 014c66b6 2023-06-25 op log_warnx("invalid record length: %d",
438 014c66b6 2023-06-25 op fcgi->fcg_toread);
439 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
443 014c66b6 2023-06-25 op fcgi->fcg_padding = hdr.padding;
444 014c66b6 2023-06-25 op if (fcgi->fcg_padding < 0) {
445 014c66b6 2023-06-25 op log_warnx("invalid padding: %d",
446 014c66b6 2023-06-25 op fcgi->fcg_padding);
447 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
451 014c66b6 2023-06-25 op fcgi->fcg_type = hdr.type;
452 014c66b6 2023-06-25 op fcgi->fcg_rec_id = CAT(hdr.req_id0, hdr.req_id1);
456 014c66b6 2023-06-25 op q.clt_id = fcgi->fcg_rec_id;
457 014c66b6 2023-06-25 op clt = SPLAY_FIND(client_tree, &fcgi->fcg_clients, &q);
459 014c66b6 2023-06-25 op switch (fcgi->fcg_type) {
460 014c66b6 2023-06-25 op case FCGI_BEGIN_REQUEST:
461 014c66b6 2023-06-25 op if (sizeof(breq) != fcgi->fcg_toread) {
462 014c66b6 2023-06-25 op log_warnx("unexpected size for "
463 014c66b6 2023-06-25 op "FCGI_BEGIN_REQUEST");
464 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
468 014c66b6 2023-06-25 op evbuffer_remove(src, &breq, sizeof(breq));
470 014c66b6 2023-06-25 op role = CAT(breq.role0, breq.role1);
471 014c66b6 2023-06-25 op if (role != FCGI_RESPONDER) {
472 014c66b6 2023-06-25 op log_warnx("unknown fastcgi role: %d",
474 014c66b6 2023-06-25 op if (fcgi_send_end_req(fcgi, fcgi->fcg_rec_id,
475 014c66b6 2023-06-25 op 1, FCGI_UNKNOWN_ROLE) == -1) {
476 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
482 014c66b6 2023-06-25 op if (!fcgi->fcg_keep_conn) {
483 014c66b6 2023-06-25 op log_warnx("trying to reuse the fastcgi "
484 014c66b6 2023-06-25 op "socket without marking it as so.");
485 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
488 014c66b6 2023-06-25 op fcgi->fcg_keep_conn = breq.flags & FCGI_KEEP_CONN;
490 014c66b6 2023-06-25 op if (clt != NULL) {
491 014c66b6 2023-06-25 op log_warnx("ignoring attemp to re-use an "
492 014c66b6 2023-06-25 op "active request id (%d)",
493 014c66b6 2023-06-25 op fcgi->fcg_rec_id);
497 014c66b6 2023-06-25 op if ((clt = calloc(1, sizeof(*clt))) == NULL) {
498 014c66b6 2023-06-25 op log_warnx("calloc");
503 014c66b6 2023-06-25 op clt->clt_tp = template(clt, clt_tp_puts, clt_tp_putc);
504 014c66b6 2023-06-25 op if (clt->clt_tp == NULL) {
506 014c66b6 2023-06-25 op log_warn("template");
511 014c66b6 2023-06-25 op clt->clt_id = fcgi->fcg_rec_id;
512 014c66b6 2023-06-25 op clt->clt_fd = -1;
513 014c66b6 2023-06-25 op clt->clt_fcgi = fcgi;
514 014c66b6 2023-06-25 op SPLAY_INSERT(client_tree, &fcgi->fcg_clients, clt);
516 014c66b6 2023-06-25 op case FCGI_PARAMS:
517 014c66b6 2023-06-25 op if (clt == NULL) {
518 014c66b6 2023-06-25 op log_warnx("got FCGI_PARAMS for inactive id "
519 014c66b6 2023-06-25 op "(%d)", fcgi->fcg_rec_id);
520 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_toread);
523 014c66b6 2023-06-25 op if (fcgi->fcg_toread == 0) {
524 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_toread);
525 014c66b6 2023-06-25 op if (server_handle(env, clt) == -1)
529 014c66b6 2023-06-25 op if (fcgi_parse_params(fcgi, src, clt) == -1) {
530 014c66b6 2023-06-25 op log_warnx("fcgi_parse_params failed");
531 014c66b6 2023-06-25 op fcgi_error(bev, EV_READ, d);
535 014c66b6 2023-06-25 op case FCGI_STDIN:
536 014c66b6 2023-06-25 op /* not interested in reading stdin */
537 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_toread);
539 014c66b6 2023-06-25 op case FCGI_ABORT_REQUEST:
540 014c66b6 2023-06-25 op if (clt == NULL) {
541 014c66b6 2023-06-25 op log_warnx("got FCGI_ABORT_REQUEST for inactive"
542 014c66b6 2023-06-25 op " id (%d)", fcgi->fcg_rec_id);
543 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_toread);
546 014c66b6 2023-06-25 op if (fcgi_end_request(clt, 1) == -1) {
547 014c66b6 2023-06-25 op /* calls fcgi_error on failure */
552 014c66b6 2023-06-25 op log_warnx("unknown fastcgi record type %d",
553 014c66b6 2023-06-25 op fcgi->fcg_type);
554 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_toread);
558 014c66b6 2023-06-25 op /* Prepare for the next record. */
559 014c66b6 2023-06-25 op evbuffer_drain(src, fcgi->fcg_padding);
560 014c66b6 2023-06-25 op fcgi->fcg_want = FCGI_RECORD_HEADER;
561 014c66b6 2023-06-25 op fcgi->fcg_toread = sizeof(struct fcgi_header);
566 014c66b6 2023-06-25 op fcgi_write(struct bufferevent *bev, void *d)
568 014c66b6 2023-06-25 op struct fcgi *fcgi = d;
569 014c66b6 2023-06-25 op struct evbuffer *out = EVBUFFER_OUTPUT(bev);
571 014c66b6 2023-06-25 op if (fcgi->fcg_done && EVBUFFER_LENGTH(out) == 0)
572 014c66b6 2023-06-25 op fcgi_error(bev, EVBUFFER_EOF, fcgi);
576 014c66b6 2023-06-25 op fcgi_error(struct bufferevent *bev, short event, void *d)
578 014c66b6 2023-06-25 op struct fcgi *fcgi = d;
579 014c66b6 2023-06-25 op struct env *env = fcgi->fcg_env;
580 014c66b6 2023-06-25 op struct client *clt;
582 014c66b6 2023-06-25 op log_debug("fcgi failure, shutting down connection (ev: %x)",
584 014c66b6 2023-06-25 op fcgi_inflight_dec(__func__);
586 014c66b6 2023-06-25 op while ((clt = SPLAY_MIN(client_tree, &fcgi->fcg_clients)) != NULL) {
587 014c66b6 2023-06-25 op SPLAY_REMOVE(client_tree, &fcgi->fcg_clients, clt);
588 014c66b6 2023-06-25 op server_client_free(clt);
591 014c66b6 2023-06-25 op SPLAY_REMOVE(fcgi_tree, &env->env_fcgi_socks, fcgi);
592 014c66b6 2023-06-25 op fcgi_free(fcgi);
598 014c66b6 2023-06-25 op fcgi_free(struct fcgi *fcgi)
600 014c66b6 2023-06-25 op close(fcgi->fcg_s);
601 014c66b6 2023-06-25 op bufferevent_free(fcgi->fcg_bev);
606 014c66b6 2023-06-25 op clt_flush(struct client *clt)
608 014c66b6 2023-06-25 op struct fcgi *fcgi = clt->clt_fcgi;
609 014c66b6 2023-06-25 op struct bufferevent *bev = fcgi->fcg_bev;
610 014c66b6 2023-06-25 op struct fcgi_header hdr;
612 014c66b6 2023-06-25 op if (clt->clt_buflen == 0)
615 014c66b6 2023-06-25 op memset(&hdr, 0, sizeof(hdr));
616 014c66b6 2023-06-25 op hdr.version = FCGI_VERSION_1;
617 014c66b6 2023-06-25 op hdr.type = FCGI_STDOUT;
618 014c66b6 2023-06-25 op hdr.req_id0 = (clt->clt_id & 0xFF);
619 014c66b6 2023-06-25 op hdr.req_id1 = (clt->clt_id >> 8);
620 014c66b6 2023-06-25 op hdr.content_len0 = (clt->clt_buflen & 0xFF);
621 014c66b6 2023-06-25 op hdr.content_len1 = (clt->clt_buflen >> 8);
623 014c66b6 2023-06-25 op if (bufferevent_write(bev, &hdr, sizeof(hdr)) == -1 ||
624 014c66b6 2023-06-25 op bufferevent_write(bev, clt->clt_buf, clt->clt_buflen) == -1) {
625 014c66b6 2023-06-25 op fcgi_error(bev, EV_WRITE, fcgi);
629 014c66b6 2023-06-25 op clt->clt_buflen = 0;
635 014c66b6 2023-06-25 op clt_write(struct client *clt, const uint8_t *buf, size_t len)
637 014c66b6 2023-06-25 op size_t left, copy;
639 014c66b6 2023-06-25 op while (len > 0) {
640 014c66b6 2023-06-25 op left = sizeof(clt->clt_buf) - clt->clt_buflen;
641 014c66b6 2023-06-25 op if (left == 0) {
642 014c66b6 2023-06-25 op if (clt_flush(clt) == -1)
644 014c66b6 2023-06-25 op left = sizeof(clt->clt_buf);
647 014c66b6 2023-06-25 op copy = MIN(left, len);
649 014c66b6 2023-06-25 op memcpy(&clt->clt_buf[clt->clt_buflen], buf, copy);
650 014c66b6 2023-06-25 op clt->clt_buflen += copy;
659 014c66b6 2023-06-25 op clt_putc(struct client *clt, char ch)
661 014c66b6 2023-06-25 op return (clt_write(clt, &ch, 1));
665 014c66b6 2023-06-25 op clt_puts(struct client *clt, const char *str)
667 014c66b6 2023-06-25 op return (clt_write(clt, str, strlen(str)));
671 014c66b6 2023-06-25 op clt_write_bufferevent(struct client *clt, struct bufferevent *bev)
673 014c66b6 2023-06-25 op struct evbuffer *src = EVBUFFER_INPUT(bev);
674 014c66b6 2023-06-25 op size_t len, left, copy;
676 014c66b6 2023-06-25 op len = EVBUFFER_LENGTH(src);
677 014c66b6 2023-06-25 op while (len > 0) {
678 014c66b6 2023-06-25 op left = sizeof(clt->clt_buf) - clt->clt_buflen;
679 014c66b6 2023-06-25 op if (left == 0) {
680 014c66b6 2023-06-25 op if (clt_flush(clt) == -1)
682 014c66b6 2023-06-25 op left = sizeof(clt->clt_buf);
685 014c66b6 2023-06-25 op copy = bufferevent_read(bev, &clt->clt_buf[clt->clt_buflen],
686 014c66b6 2023-06-25 op MIN(left, len));
687 014c66b6 2023-06-25 op clt->clt_buflen += copy;
689 014c66b6 2023-06-25 op len = EVBUFFER_LENGTH(src);
696 014c66b6 2023-06-25 op clt_printf(struct client *clt, const char *fmt, ...)
698 014c66b6 2023-06-25 op struct fcgi *fcgi = clt->clt_fcgi;
699 014c66b6 2023-06-25 op struct bufferevent *bev = fcgi->fcg_bev;
704 014c66b6 2023-06-25 op va_start(ap, fmt);
705 014c66b6 2023-06-25 op r = vasprintf(&str, fmt, ap);
707 014c66b6 2023-06-25 op if (r == -1) {
708 014c66b6 2023-06-25 op fcgi_error(bev, EV_WRITE, fcgi);
712 014c66b6 2023-06-25 op r = clt_write(clt, str, r);
719 014c66b6 2023-06-25 op clt_tp_puts(struct template *tp, const char *str)
721 014c66b6 2023-06-25 op struct client *clt = tp->tp_arg;
723 014c66b6 2023-06-25 op if (clt_puts(clt, str) == -1)
730 014c66b6 2023-06-25 op clt_tp_putc(struct template *tp, int c)
732 014c66b6 2023-06-25 op struct client *clt = tp->tp_arg;
734 014c66b6 2023-06-25 op if (clt_putc(clt, c) == -1)
742 014c66b6 2023-06-25 op fcgi_cmp(struct fcgi *a, struct fcgi *b)
744 014c66b6 2023-06-25 op return ((int)a->fcg_id - b->fcg_id);
748 014c66b6 2023-06-25 op fcgi_client_cmp(struct client *a, struct client *b)
750 014c66b6 2023-06-25 op return ((int)a->clt_id - b->clt_id);
754 014c66b6 2023-06-25 op accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
755 014c66b6 2023-06-25 op int reserve, volatile int *counter)
759 014c66b6 2023-06-25 op if (getdtablecount() + reserve + *counter >= getdtablesize()) {
760 014c66b6 2023-06-25 op errno = EMFILE;
764 014c66b6 2023-06-25 op if ((ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK)) > -1) {
765 014c66b6 2023-06-25 op (*counter)++;
766 014c66b6 2023-06-25 op log_debug("%s: inflight incremented, now %d", __func__,
770 014c66b6 2023-06-25 op return (ret);
773 014c66b6 2023-06-25 op SPLAY_GENERATE(fcgi_tree, fcgi, fcg_nodes, fcgi_cmp);
774 014c66b6 2023-06-25 op SPLAY_GENERATE(client_tree, client, clt_nodes, fcgi_client_cmp);