2 50794f47 2023-04-04 op * This file is in the public domain.
5 50794f47 2023-04-04 op #include <sys/queue.h>
6 50794f47 2023-04-04 op #include <sys/tree.h>
7 50794f47 2023-04-04 op #include <sys/socket.h>
9 50794f47 2023-04-04 op #include <netinet/in.h>
10 50794f47 2023-04-04 op #include <arpa/inet.h>
12 50794f47 2023-04-04 op #include <errno.h>
13 50794f47 2023-04-04 op #include <event.h>
14 50794f47 2023-04-04 op #include <limits.h>
15 50794f47 2023-04-04 op #include <stdarg.h>
16 50794f47 2023-04-04 op #include <stdio.h>
17 50794f47 2023-04-04 op #include <stdlib.h>
18 50794f47 2023-04-04 op #include <stdint.h>
19 50794f47 2023-04-04 op #include <string.h>
20 50794f47 2023-04-04 op #include <unistd.h>
22 dded5403 2023-10-16 op #include "log.h"
23 50794f47 2023-04-04 op #include "msearchd.h"
25 50794f47 2023-04-04 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
27 50794f47 2023-04-04 op struct fcgi_header {
28 50794f47 2023-04-04 op unsigned char version;
29 50794f47 2023-04-04 op unsigned char type;
30 50794f47 2023-04-04 op unsigned char req_id1;
31 50794f47 2023-04-04 op unsigned char req_id0;
32 50794f47 2023-04-04 op unsigned char content_len1;
33 50794f47 2023-04-04 op unsigned char content_len0;
34 50794f47 2023-04-04 op unsigned char padding;
35 50794f47 2023-04-04 op unsigned char reserved;
36 50794f47 2023-04-04 op } __attribute__((packed));
39 50794f47 2023-04-04 op * number of bytes in a FCGI_HEADER. Future version of the protocol
40 50794f47 2023-04-04 op * will not reduce this number.
42 50794f47 2023-04-04 op #define FCGI_HEADER_LEN 8
45 50794f47 2023-04-04 op * values for the version component
47 50794f47 2023-04-04 op #define FCGI_VERSION_1 1
50 50794f47 2023-04-04 op * values for the type component
52 50794f47 2023-04-04 op #define FCGI_BEGIN_REQUEST 1
53 50794f47 2023-04-04 op #define FCGI_ABORT_REQUEST 2
54 50794f47 2023-04-04 op #define FCGI_END_REQUEST 3
55 50794f47 2023-04-04 op #define FCGI_PARAMS 4
56 50794f47 2023-04-04 op #define FCGI_STDIN 5
57 50794f47 2023-04-04 op #define FCGI_STDOUT 6
58 50794f47 2023-04-04 op #define FCGI_STDERR 7
59 50794f47 2023-04-04 op #define FCGI_DATA 8
60 50794f47 2023-04-04 op #define FCGI_GET_VALUES 9
61 50794f47 2023-04-04 op #define FCGI_GET_VALUES_RESULT 10
62 50794f47 2023-04-04 op #define FCGI_UNKNOWN_TYPE 11
63 50794f47 2023-04-04 op #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
65 50794f47 2023-04-04 op struct fcgi_begin_req {
66 50794f47 2023-04-04 op unsigned char role1;
67 50794f47 2023-04-04 op unsigned char role0;
68 50794f47 2023-04-04 op unsigned char flags;
69 50794f47 2023-04-04 op unsigned char reserved[5];
72 50794f47 2023-04-04 op struct fcgi_begin_req_record {
73 50794f47 2023-04-04 op struct fcgi_header header;
74 50794f47 2023-04-04 op struct fcgi_begin_req body;
78 50794f47 2023-04-04 op * mask for flags;
80 50794f47 2023-04-04 op #define FCGI_KEEP_CONN 1
83 50794f47 2023-04-04 op * values for the role
85 50794f47 2023-04-04 op #define FCGI_RESPONDER 1
86 50794f47 2023-04-04 op #define FCGI_AUTHORIZER 2
87 50794f47 2023-04-04 op #define FCGI_FILTER 3
89 50794f47 2023-04-04 op struct fcgi_end_req_body {
90 50794f47 2023-04-04 op unsigned char app_status3;
91 50794f47 2023-04-04 op unsigned char app_status2;
92 50794f47 2023-04-04 op unsigned char app_status1;
93 50794f47 2023-04-04 op unsigned char app_status0;
94 50794f47 2023-04-04 op unsigned char proto_status;
95 50794f47 2023-04-04 op unsigned char reserved[3];
99 50794f47 2023-04-04 op * values for proto_status
101 50794f47 2023-04-04 op #define FCGI_REQUEST_COMPLETE 0
102 50794f47 2023-04-04 op #define FCGI_CANT_MPX_CONN 1
103 50794f47 2023-04-04 op #define FCGI_OVERLOADED 2
104 50794f47 2023-04-04 op #define FCGI_UNKNOWN_ROLE 3
107 50794f47 2023-04-04 op * Variable names for FCGI_GET_VALUES / FCGI_GET_VALUES_RESULT
110 50794f47 2023-04-04 op #define FCGI_MAX_CONNS "FCGI_MAX_CONNS"
111 50794f47 2023-04-04 op #define FCGI_MAX_REQS "FCGI_MAX_REQS"
112 50794f47 2023-04-04 op #define FCGI_MPXS_CONNS "FCGI_MPXS_CONNS"
114 50794f47 2023-04-04 op #define CAT(f0, f1) ((f0) + ((f1) << 8))
117 50794f47 2023-04-04 op FCGI_RECORD_HEADER,
118 50794f47 2023-04-04 op FCGI_RECORD_BODY,
121 50794f47 2023-04-04 op volatile int fcgi_inflight;
122 50794f47 2023-04-04 op int32_t fcgi_id;
124 50794f47 2023-04-04 op int accept_reserve(int, struct sockaddr *, socklen_t *, int,
125 50794f47 2023-04-04 op volatile int *);
128 50794f47 2023-04-04 op fcgi_send_end_req(struct fcgi *fcgi, int id, int as, int ps)
130 50794f47 2023-04-04 op struct bufferevent *bev = fcgi->fcg_bev;
131 50794f47 2023-04-04 op struct fcgi_header hdr;
132 50794f47 2023-04-04 op struct fcgi_end_req_body end;
134 50794f47 2023-04-04 op memset(&hdr, 0, sizeof(hdr));
135 50794f47 2023-04-04 op memset(&end, 0, sizeof(end));
137 50794f47 2023-04-04 op hdr.version = FCGI_VERSION_1;
138 50794f47 2023-04-04 op hdr.type = FCGI_END_REQUEST;
139 50794f47 2023-04-04 op hdr.req_id0 = (id & 0xFF);
140 50794f47 2023-04-04 op hdr.req_id1 = (id >> 8);
141 50794f47 2023-04-04 op hdr.content_len0 = sizeof(end);
143 50794f47 2023-04-04 op end.app_status0 = (unsigned char)as;
144 50794f47 2023-04-04 op end.proto_status = (unsigned char)ps;
146 50794f47 2023-04-04 op if (bufferevent_write(bev, &hdr, sizeof(hdr)) == -1)
148 50794f47 2023-04-04 op if (bufferevent_write(bev, &end, sizeof(end)) == -1)
154 50794f47 2023-04-04 op end_request(struct client *clt, int status, int proto_status)
156 50794f47 2023-04-04 op struct fcgi *fcgi = clt->clt_fcgi;
159 50794f47 2023-04-04 op if (clt_flush(clt) == -1)
162 50794f47 2023-04-04 op r = fcgi_send_end_req(fcgi, clt->clt_id, status,
163 50794f47 2023-04-04 op proto_status);
164 50794f47 2023-04-04 op if (r == -1) {
165 50794f47 2023-04-04 op fcgi_error(fcgi->fcg_bev, EV_WRITE, fcgi);
169 50794f47 2023-04-04 op SPLAY_REMOVE(client_tree, &fcgi->fcg_clients, clt);
170 50794f47 2023-04-04 op server_client_free(clt);
172 50794f47 2023-04-04 op if (!fcgi->fcg_keep_conn)
173 50794f47 2023-04-04 op fcgi->fcg_done = 1;
179 50794f47 2023-04-04 op fcgi_end_request(struct client *clt, int status)
181 50794f47 2023-04-04 op return (end_request(clt, status, FCGI_REQUEST_COMPLETE));
185 50794f47 2023-04-04 op fcgi_abort_request(struct client *clt)
187 50794f47 2023-04-04 op return (end_request(clt, 1, FCGI_OVERLOADED));
191 50794f47 2023-04-04 op fcgi_inflight_dec(const char *why)
193 50794f47 2023-04-04 op fcgi_inflight--;
194 50794f47 2023-04-04 op log_debug("%s: fcgi inflight decremented, now %d, %s",
195 50794f47 2023-04-04 op __func__, fcgi_inflight, why);
199 50794f47 2023-04-04 op fcgi_accept(int fd, short event, void *arg)
201 50794f47 2023-04-04 op struct env *env = arg;
202 50794f47 2023-04-04 op struct fcgi *fcgi = NULL;
203 50794f47 2023-04-04 op socklen_t slen;
204 50794f47 2023-04-04 op struct sockaddr_storage ss;
207 50794f47 2023-04-04 op event_add(&env->env_pausev, NULL);
208 50794f47 2023-04-04 op if ((event & EV_TIMEOUT))
211 50794f47 2023-04-04 op slen = sizeof(ss);
212 50794f47 2023-04-04 op if ((s = accept_reserve(env->env_sockfd, (struct sockaddr *)&ss,
213 50794f47 2023-04-04 op &slen, FD_RESERVE, &fcgi_inflight)) == -1) {
215 50794f47 2023-04-04 op * Pause accept if we are out of file descriptors, or
216 50794f47 2023-04-04 op * libevent will haunt us here too.
218 50794f47 2023-04-04 op if (errno == ENFILE || errno == EMFILE) {
219 50794f47 2023-04-04 op struct timeval evtpause = { 1, 0 };
221 50794f47 2023-04-04 op event_del(&env->env_sockev);
222 50794f47 2023-04-04 op evtimer_add(&env->env_pausev, &evtpause);
223 50794f47 2023-04-04 op log_debug("%s: deferring connections", __func__);
228 50794f47 2023-04-04 op if ((fcgi = calloc(1, sizeof(*fcgi))) == NULL)
231 50794f47 2023-04-04 op fcgi->fcg_id = ++fcgi_id;
232 50794f47 2023-04-04 op fcgi->fcg_s = s;
233 50794f47 2023-04-04 op fcgi->fcg_env = env;
234 50794f47 2023-04-04 op fcgi->fcg_want = FCGI_RECORD_HEADER;
235 50794f47 2023-04-04 op fcgi->fcg_toread = sizeof(struct fcgi_header);
236 50794f47 2023-04-04 op SPLAY_INIT(&fcgi->fcg_clients);
238 50794f47 2023-04-04 op /* assume it's enabled until we get a FCGI_BEGIN_REQUEST */
239 50794f47 2023-04-04 op fcgi->fcg_keep_conn = 1;
241 50794f47 2023-04-04 op fcgi->fcg_bev = bufferevent_new(fcgi->fcg_s, fcgi_read, fcgi_write,
242 50794f47 2023-04-04 op fcgi_error, fcgi);
243 50794f47 2023-04-04 op if (fcgi->fcg_bev == NULL)
246 50794f47 2023-04-04 op bufferevent_enable(fcgi->fcg_bev, EV_READ | EV_WRITE);
250 50794f47 2023-04-04 op if (s != -1) {
253 50794f47 2023-04-04 op fcgi_inflight_dec(__func__);
258 50794f47 2023-04-04 op parse_len(struct fcgi *fcgi, struct evbuffer *src)
260 50794f47 2023-04-04 op unsigned char c, x[3];
262 50794f47 2023-04-04 op fcgi->fcg_toread--;
263 50794f47 2023-04-04 op evbuffer_remove(src, &c, 1);
264 50794f47 2023-04-04 op if (c >> 7 == 0)
267 50794f47 2023-04-04 op if (fcgi->fcg_toread < 3)
270 50794f47 2023-04-04 op fcgi->fcg_toread -= 3;
271 50794f47 2023-04-04 op evbuffer_remove(src, x, sizeof(x));
272 50794f47 2023-04-04 op return (((c & 0x7F) << 24) | (x[0] << 16) | (x[1] << 8) | x[2]);
276 50794f47 2023-04-04 op fcgi_parse_params(struct fcgi *fcgi, struct evbuffer *src, struct client *clt)
278 50794f47 2023-04-04 op char pname[32];
279 50794f47 2023-04-04 op char server[HOST_NAME_MAX + 1];
280 50794f47 2023-04-04 op char path[PATH_MAX];
281 50794f47 2023-04-04 op char query[QUERY_MAXLEN];
282 50794f47 2023-04-04 op char method[8];
283 50794f47 2023-04-04 op int nlen, vlen;
285 50794f47 2023-04-04 op while (fcgi->fcg_toread > 0) {
286 50794f47 2023-04-04 op if ((nlen = parse_len(fcgi, src)) < 0 ||
287 50794f47 2023-04-04 op (vlen = parse_len(fcgi, src)) < 0)
290 50794f47 2023-04-04 op if (fcgi->fcg_toread < nlen + vlen)
293 50794f47 2023-04-04 op if ((size_t)nlen > sizeof(pname) - 1) {
294 50794f47 2023-04-04 op /* ignore this parameter */
295 50794f47 2023-04-04 op fcgi->fcg_toread -= nlen - vlen;
296 50794f47 2023-04-04 op evbuffer_drain(src, nlen + vlen);
300 50794f47 2023-04-04 op fcgi->fcg_toread -= nlen;
301 50794f47 2023-04-04 op evbuffer_remove(src, &pname, nlen);
302 50794f47 2023-04-04 op pname[nlen] = '\0';
304 50794f47 2023-04-04 op if (!strcmp(pname, "SERVER_NAME") &&
305 50794f47 2023-04-04 op (size_t)vlen < sizeof(server)) {
306 50794f47 2023-04-04 op fcgi->fcg_toread -= vlen;
307 50794f47 2023-04-04 op evbuffer_remove(src, &server, vlen);
308 50794f47 2023-04-04 op server[vlen] = '\0';
310 50794f47 2023-04-04 op free(clt->clt_server_name);
311 50794f47 2023-04-04 op if ((clt->clt_server_name = strdup(server)) == NULL)
313 50794f47 2023-04-04 op DPRINTF("clt %d: server_name: %s", clt->clt_id,
314 50794f47 2023-04-04 op clt->clt_server_name);
318 50794f47 2023-04-04 op if (!strcmp(pname, "SCRIPT_NAME") &&
319 50794f47 2023-04-04 op (size_t)vlen < sizeof(path)) {
320 50794f47 2023-04-04 op fcgi->fcg_toread -= vlen;
321 50794f47 2023-04-04 op evbuffer_remove(src, &path, vlen);
322 50794f47 2023-04-04 op path[vlen] = '\0';
324 50794f47 2023-04-04 op free(clt->clt_script_name);
325 50794f47 2023-04-04 op clt->clt_script_name = NULL;
327 50794f47 2023-04-04 op if (vlen == 0 || path[vlen - 1] != '/')
328 50794f47 2023-04-04 op asprintf(&clt->clt_script_name, "%s/", path);
330 50794f47 2023-04-04 op clt->clt_script_name = strdup(path);
332 50794f47 2023-04-04 op if (clt->clt_script_name == NULL)
335 50794f47 2023-04-04 op DPRINTF("clt %d: script_name: %s", clt->clt_id,
336 50794f47 2023-04-04 op clt->clt_script_name);
340 50794f47 2023-04-04 op if (!strcmp(pname, "PATH_INFO") &&
341 50794f47 2023-04-04 op (size_t)vlen < sizeof(path)) {
342 50794f47 2023-04-04 op fcgi->fcg_toread -= vlen;
343 50794f47 2023-04-04 op evbuffer_remove(src, &path, vlen);
344 50794f47 2023-04-04 op path[vlen] = '\0';
346 50794f47 2023-04-04 op free(clt->clt_path_info);
347 50794f47 2023-04-04 op clt->clt_path_info = NULL;
349 50794f47 2023-04-04 op if (*path != '/')
350 50794f47 2023-04-04 op asprintf(&clt->clt_path_info, "/%s", path);
352 50794f47 2023-04-04 op clt->clt_path_info = strdup(path);
354 50794f47 2023-04-04 op if (clt->clt_path_info == NULL)
357 50794f47 2023-04-04 op DPRINTF("clt %d: path_info: %s", clt->clt_id,
358 50794f47 2023-04-04 op clt->clt_path_info);
362 50794f47 2023-04-04 op if (!strcmp(pname, "QUERY_STRING") &&
363 50794f47 2023-04-04 op (size_t)vlen < sizeof(query) &&
365 50794f47 2023-04-04 op fcgi->fcg_toread -= vlen;
366 50794f47 2023-04-04 op evbuffer_remove(src, &query, vlen);
367 50794f47 2023-04-04 op query[vlen] = '\0';
369 50794f47 2023-04-04 op free(clt->clt_query);
370 50794f47 2023-04-04 op if ((clt->clt_query = strdup(query)) == NULL)
373 50794f47 2023-04-04 op DPRINTF("clt %d: query: %s", clt->clt_id,
374 50794f47 2023-04-04 op clt->clt_query);
378 50794f47 2023-04-04 op if (!strcmp(pname, "REQUEST_METHOD") &&
379 50794f47 2023-04-04 op (size_t)vlen < sizeof(method)) {
380 50794f47 2023-04-04 op fcgi->fcg_toread -= vlen;
381 50794f47 2023-04-04 op evbuffer_remove(src, &method, vlen);
382 50794f47 2023-04-04 op method[vlen] = '\0';
384 50794f47 2023-04-04 op if (!strcasecmp(method, "GET"))
385 50794f47 2023-04-04 op clt->clt_method = METHOD_GET;
386 50794f47 2023-04-04 op if (!strcasecmp(method, "POST"))
387 50794f47 2023-04-04 op clt->clt_method = METHOD_POST;
392 50794f47 2023-04-04 op fcgi->fcg_toread -= vlen;
393 50794f47 2023-04-04 op evbuffer_drain(src, vlen);
400 50794f47 2023-04-04 op fcgi_read(struct bufferevent *bev, void *d)
402 50794f47 2023-04-04 op struct fcgi *fcgi = d;
403 50794f47 2023-04-04 op struct env *env = fcgi->fcg_env;
404 50794f47 2023-04-04 op struct evbuffer *src = EVBUFFER_INPUT(bev);
405 50794f47 2023-04-04 op struct fcgi_header hdr;
406 50794f47 2023-04-04 op struct fcgi_begin_req breq;
407 50794f47 2023-04-04 op struct client *clt, q;
410 50794f47 2023-04-04 op memset(&q, 0, sizeof(q));
413 50794f47 2023-04-04 op if (EVBUFFER_LENGTH(src) < (size_t)fcgi->fcg_toread)
416 50794f47 2023-04-04 op if (fcgi->fcg_want == FCGI_RECORD_HEADER) {
417 50794f47 2023-04-04 op fcgi->fcg_want = FCGI_RECORD_BODY;
418 50794f47 2023-04-04 op bufferevent_read(bev, &hdr, sizeof(hdr));
420 50794f47 2023-04-04 op DPRINTF("header: v=%d t=%d id=%d len=%d p=%d",
421 50794f47 2023-04-04 op hdr.version, hdr.type,
422 50794f47 2023-04-04 op CAT(hdr.req_id0, hdr.req_id1),
423 50794f47 2023-04-04 op CAT(hdr.content_len0, hdr.content_len1),
424 50794f47 2023-04-04 op hdr.padding);
426 50794f47 2023-04-04 op if (hdr.version != FCGI_VERSION_1) {
427 50794f47 2023-04-04 op log_warnx("unknown fastcgi version: %d",
428 50794f47 2023-04-04 op hdr.version);
429 50794f47 2023-04-04 op fcgi_error(bev, EV_READ, d);
433 50794f47 2023-04-04 op fcgi->fcg_toread = CAT(hdr.content_len0,
434 50794f47 2023-04-04 op hdr.content_len1);
435 50794f47 2023-04-04 op if (fcgi->fcg_toread < 0) {
436 50794f47 2023-04-04 op log_warnx("invalid record length: %d",
437 50794f47 2023-04-04 op fcgi->fcg_toread);
438 50794f47 2023-04-04 op fcgi_error(bev, EV_READ, d);
442 50794f47 2023-04-04 op fcgi->fcg_padding = hdr.padding;
443 50794f47 2023-04-04 op if (fcgi->fcg_padding < 0) {
444 50794f47 2023-04-04 op log_warnx("invalid padding: %d",
445 50794f47 2023-04-04 op fcgi->fcg_padding);
446 50794f47 2023-04-04 op fcgi_error(bev, EV_READ, d);
450 50794f47 2023-04-04 op fcgi->fcg_type = hdr.type;
451 50794f47 2023-04-04 op fcgi->fcg_rec_id = CAT(hdr.req_id0, hdr.req_id1);
455 50794f47 2023-04-04 op q.clt_id = fcgi->fcg_rec_id;
456 50794f47 2023-04-04 op clt = SPLAY_FIND(client_tree, &fcgi->fcg_clients, &q);
458 50794f47 2023-04-04 op switch (fcgi->fcg_type) {
459 50794f47 2023-04-04 op case FCGI_BEGIN_REQUEST:
460 50794f47 2023-04-04 op if (sizeof(breq) != fcgi->fcg_toread) {
461 50794f47 2023-04-04 op log_warnx("unexpected size for "
462 50794f47 2023-04-04 op "FCGI_BEGIN_REQUEST");
463 50794f47 2023-04-04 op fcgi_error(bev, EV_READ, d);
467 50794f47 2023-04-04 op evbuffer_remove(src, &breq, sizeof(breq));
469 50794f47 2023-04-04 op role = CAT(breq.role0, breq.role1);
470 50794f47 2023-04-04 op if (role != FCGI_RESPONDER) {
471 50794f47 2023-04-04 op log_warnx("unknown fastcgi role: %d",
473 50794f47 2023-04-04 op if (fcgi_send_end_req(fcgi, fcgi->fcg_rec_id,
474 50794f47 2023-04-04 op 1, FCGI_UNKNOWN_ROLE) == -1) {
475 50794f47 2023-04-04 op fcgi_error(bev, EV_READ, d);
481 50794f47 2023-04-04 op if (!fcgi->fcg_keep_conn) {
482 50794f47 2023-04-04 op log_warnx("trying to reuse the fastcgi "
483 50794f47 2023-04-04 op "socket without marking it as so.");
484 50794f47 2023-04-04 op fcgi_error(bev, EV_READ, d);
487 50794f47 2023-04-04 op fcgi->fcg_keep_conn = breq.flags & FCGI_KEEP_CONN;
489 50794f47 2023-04-04 op if (clt != NULL) {
490 50794f47 2023-04-04 op log_warnx("ignoring attemp to re-use an "
491 50794f47 2023-04-04 op "active request id (%d)",
492 50794f47 2023-04-04 op fcgi->fcg_rec_id);
496 50794f47 2023-04-04 op if ((clt = calloc(1, sizeof(*clt))) == NULL) {
497 50794f47 2023-04-04 op log_warnx("calloc");
501 50794f47 2023-04-04 op clt->clt_id = fcgi->fcg_rec_id;
502 50794f47 2023-04-04 op clt->clt_fd = -1;
503 50794f47 2023-04-04 op clt->clt_fcgi = fcgi;
504 50794f47 2023-04-04 op SPLAY_INSERT(client_tree, &fcgi->fcg_clients, clt);
506 50794f47 2023-04-04 op case FCGI_PARAMS:
507 50794f47 2023-04-04 op if (clt == NULL) {
508 50794f47 2023-04-04 op log_warnx("got FCGI_PARAMS for inactive id "
509 50794f47 2023-04-04 op "(%d)", fcgi->fcg_rec_id);
510 50794f47 2023-04-04 op evbuffer_drain(src, fcgi->fcg_toread);
513 50794f47 2023-04-04 op if (fcgi->fcg_toread == 0) {
514 50794f47 2023-04-04 op evbuffer_drain(src, fcgi->fcg_toread);
515 50794f47 2023-04-04 op if (server_handle(env, clt) == -1)
519 50794f47 2023-04-04 op if (fcgi_parse_params(fcgi, src, clt) == -1) {
520 50794f47 2023-04-04 op log_warnx("fcgi_parse_params failed");
521 50794f47 2023-04-04 op fcgi_error(bev, EV_READ, d);
525 50794f47 2023-04-04 op case FCGI_STDIN:
526 50794f47 2023-04-04 op /* not interested in reading stdin */
527 50794f47 2023-04-04 op evbuffer_drain(src, fcgi->fcg_toread);
529 50794f47 2023-04-04 op case FCGI_ABORT_REQUEST:
530 50794f47 2023-04-04 op if (clt == NULL) {
531 50794f47 2023-04-04 op log_warnx("got FCGI_ABORT_REQUEST for inactive"
532 50794f47 2023-04-04 op " id (%d)", fcgi->fcg_rec_id);
533 50794f47 2023-04-04 op evbuffer_drain(src, fcgi->fcg_toread);
536 50794f47 2023-04-04 op if (fcgi_end_request(clt, 1) == -1) {
537 50794f47 2023-04-04 op /* calls fcgi_error on failure */
542 50794f47 2023-04-04 op log_warnx("unknown fastcgi record type %d",
543 50794f47 2023-04-04 op fcgi->fcg_type);
544 50794f47 2023-04-04 op evbuffer_drain(src, fcgi->fcg_toread);
548 50794f47 2023-04-04 op /* Prepare for the next record. */
549 50794f47 2023-04-04 op evbuffer_drain(src, fcgi->fcg_padding);
550 50794f47 2023-04-04 op fcgi->fcg_want = FCGI_RECORD_HEADER;
551 50794f47 2023-04-04 op fcgi->fcg_toread = sizeof(struct fcgi_header);
556 50794f47 2023-04-04 op fcgi_write(struct bufferevent *bev, void *d)
558 50794f47 2023-04-04 op struct fcgi *fcgi = d;
559 50794f47 2023-04-04 op struct evbuffer *out = EVBUFFER_OUTPUT(bev);
561 50794f47 2023-04-04 op if (fcgi->fcg_done && EVBUFFER_LENGTH(out) == 0)
562 50794f47 2023-04-04 op fcgi_error(bev, EVBUFFER_EOF, fcgi);
566 50794f47 2023-04-04 op fcgi_error(struct bufferevent *bev, short event, void *d)
568 50794f47 2023-04-04 op struct fcgi *fcgi = d;
569 50794f47 2023-04-04 op struct env *env = fcgi->fcg_env;
570 50794f47 2023-04-04 op struct client *clt;
572 50794f47 2023-04-04 op log_debug("fcgi failure, shutting down connection (ev: %x)",
574 50794f47 2023-04-04 op fcgi_inflight_dec(__func__);
576 50794f47 2023-04-04 op while ((clt = SPLAY_MIN(client_tree, &fcgi->fcg_clients)) != NULL) {
577 50794f47 2023-04-04 op SPLAY_REMOVE(client_tree, &fcgi->fcg_clients, clt);
578 50794f47 2023-04-04 op server_client_free(clt);
581 50794f47 2023-04-04 op SPLAY_REMOVE(fcgi_tree, &env->env_fcgi_socks, fcgi);
582 50794f47 2023-04-04 op fcgi_free(fcgi);
588 50794f47 2023-04-04 op fcgi_free(struct fcgi *fcgi)
590 50794f47 2023-04-04 op close(fcgi->fcg_s);
591 50794f47 2023-04-04 op bufferevent_free(fcgi->fcg_bev);
596 50794f47 2023-04-04 op clt_flush(struct client *clt)
598 50794f47 2023-04-04 op struct fcgi *fcgi = clt->clt_fcgi;
599 50794f47 2023-04-04 op struct bufferevent *bev = fcgi->fcg_bev;
600 50794f47 2023-04-04 op struct fcgi_header hdr;
602 50794f47 2023-04-04 op if (clt->clt_buflen == 0)
605 50794f47 2023-04-04 op memset(&hdr, 0, sizeof(hdr));
606 50794f47 2023-04-04 op hdr.version = FCGI_VERSION_1;
607 50794f47 2023-04-04 op hdr.type = FCGI_STDOUT;
608 50794f47 2023-04-04 op hdr.req_id0 = (clt->clt_id & 0xFF);
609 50794f47 2023-04-04 op hdr.req_id1 = (clt->clt_id >> 8);
610 50794f47 2023-04-04 op hdr.content_len0 = (clt->clt_buflen & 0xFF);
611 50794f47 2023-04-04 op hdr.content_len1 = (clt->clt_buflen >> 8);
613 50794f47 2023-04-04 op if (bufferevent_write(bev, &hdr, sizeof(hdr)) == -1 ||
614 50794f47 2023-04-04 op bufferevent_write(bev, clt->clt_buf, clt->clt_buflen) == -1) {
615 50794f47 2023-04-04 op fcgi_error(bev, EV_WRITE, fcgi);
619 50794f47 2023-04-04 op clt->clt_buflen = 0;
625 50794f47 2023-04-04 op clt_write(struct client *clt, const uint8_t *buf, size_t len)
627 50794f47 2023-04-04 op size_t left, copy;
629 50794f47 2023-04-04 op while (len > 0) {
630 50794f47 2023-04-04 op left = sizeof(clt->clt_buf) - clt->clt_buflen;
631 50794f47 2023-04-04 op if (left == 0) {
632 50794f47 2023-04-04 op if (clt_flush(clt) == -1)
634 50794f47 2023-04-04 op left = sizeof(clt->clt_buf);
637 50794f47 2023-04-04 op copy = MIN(left, len);
639 50794f47 2023-04-04 op memcpy(&clt->clt_buf[clt->clt_buflen], buf, copy);
640 50794f47 2023-04-04 op clt->clt_buflen += copy;
649 50794f47 2023-04-04 op clt_putc(struct client *clt, char ch)
651 50794f47 2023-04-04 op return (clt_write(clt, &ch, 1));
655 50794f47 2023-04-04 op clt_puts(struct client *clt, const char *str)
657 50794f47 2023-04-04 op return (clt_write(clt, str, strlen(str)));
661 50794f47 2023-04-04 op clt_write_bufferevent(struct client *clt, struct bufferevent *bev)
663 50794f47 2023-04-04 op struct evbuffer *src = EVBUFFER_INPUT(bev);
664 50794f47 2023-04-04 op size_t len, left, copy;
666 50794f47 2023-04-04 op len = EVBUFFER_LENGTH(src);
667 50794f47 2023-04-04 op while (len > 0) {
668 50794f47 2023-04-04 op left = sizeof(clt->clt_buf) - clt->clt_buflen;
669 50794f47 2023-04-04 op if (left == 0) {
670 50794f47 2023-04-04 op if (clt_flush(clt) == -1)
672 50794f47 2023-04-04 op left = sizeof(clt->clt_buf);
675 50794f47 2023-04-04 op copy = bufferevent_read(bev, &clt->clt_buf[clt->clt_buflen],
676 50794f47 2023-04-04 op MIN(left, len));
677 50794f47 2023-04-04 op clt->clt_buflen += copy;
679 50794f47 2023-04-04 op len = EVBUFFER_LENGTH(src);
686 50794f47 2023-04-04 op clt_putsan(struct client *clt, const char *s)
690 50794f47 2023-04-04 op if (s == NULL)
693 50794f47 2023-04-04 op for (; *s; ++s) {
694 50794f47 2023-04-04 op switch (*s) {
696 50794f47 2023-04-04 op r = clt_puts(clt, "<");
699 50794f47 2023-04-04 op r = clt_puts(clt, ">");
702 61acbd55 2023-09-03 op r = clt_puts(clt, "&");
705 61acbd55 2023-09-03 op r = clt_puts(clt, """);
708 61acbd55 2023-09-03 op r = clt_puts(clt, "'");
711 61acbd55 2023-09-03 op r = clt_putc(clt, *s);
723 61acbd55 2023-09-03 op clt_putmatch(struct client *clt, const char *s)
725 61acbd55 2023-09-03 op int r, instrong = 0, intag = 0, lastnl = 0;
727 61acbd55 2023-09-03 op if (s == NULL)
730 61acbd55 2023-09-03 op for (; *s; ++s) {
731 61acbd55 2023-09-03 op switch (*s) {
733 61acbd55 2023-09-03 op if (!instrong && !strncmp(s, "<strong>", 8)) {
734 61acbd55 2023-09-03 op instrong = intag = 1;
735 61acbd55 2023-09-03 op r = clt_putc(clt, *s);
736 61acbd55 2023-09-03 op } else if (instrong && !strncmp(s, "</strong>", 9)) {
737 61acbd55 2023-09-03 op instrong = 0;
739 61acbd55 2023-09-03 op r = clt_putc(clt, *s);
741 61acbd55 2023-09-03 op r = clt_puts(clt, "<");
746 61acbd55 2023-09-03 op r = clt_putc(clt, *s);
748 61acbd55 2023-09-03 op r = clt_puts(clt, ">");
751 50794f47 2023-04-04 op r = clt_puts(clt, "&");
754 50794f47 2023-04-04 op r = clt_puts(clt, """);
757 50794f47 2023-04-04 op r = clt_puts(clt, "'");
762 61acbd55 2023-09-03 op r = clt_puts(clt, "<br />");
767 50794f47 2023-04-04 op r = clt_putc(clt, *s);
775 61acbd55 2023-09-03 op if (instrong) /* something went wrong... */
776 61acbd55 2023-09-03 op return clt_puts(clt, "</strong>");
781 50794f47 2023-04-04 op clt_printf(struct client *clt, const char *fmt, ...)
783 50794f47 2023-04-04 op struct fcgi *fcgi = clt->clt_fcgi;
784 50794f47 2023-04-04 op struct bufferevent *bev = fcgi->fcg_bev;
789 50794f47 2023-04-04 op va_start(ap, fmt);
790 50794f47 2023-04-04 op r = vasprintf(&str, fmt, ap);
792 50794f47 2023-04-04 op if (r == -1) {
793 50794f47 2023-04-04 op fcgi_error(bev, EV_WRITE, fcgi);
797 50794f47 2023-04-04 op r = clt_write(clt, str, r);
803 50794f47 2023-04-04 op fcgi_cmp(struct fcgi *a, struct fcgi *b)
805 50794f47 2023-04-04 op return ((int)a->fcg_id - b->fcg_id);
809 50794f47 2023-04-04 op fcgi_client_cmp(struct client *a, struct client *b)
811 50794f47 2023-04-04 op return ((int)a->clt_id - b->clt_id);
815 50794f47 2023-04-04 op accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
816 50794f47 2023-04-04 op int reserve, volatile int *counter)
820 50794f47 2023-04-04 op if (getdtablecount() + reserve + *counter >= getdtablesize()) {
821 50794f47 2023-04-04 op errno = EMFILE;
825 50794f47 2023-04-04 op if ((ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK)) > -1) {
826 50794f47 2023-04-04 op (*counter)++;
827 50794f47 2023-04-04 op log_debug("%s: inflight incremented, now %d", __func__,
831 50794f47 2023-04-04 op return (ret);
834 50794f47 2023-04-04 op SPLAY_GENERATE(fcgi_tree, fcgi, fcg_nodes, fcgi_cmp);
835 50794f47 2023-04-04 op SPLAY_GENERATE(client_tree, client, clt_nodes, fcgi_client_cmp);