1 9ae1b92d 2022-08-13 op /* $OpenBSD: slowcgi.c,v 1.64 2022/08/07 07:43:53 op Exp $ */
3 9ae1b92d 2022-08-13 op * Copyright (c) 2013 David Gwynne <dlg@openbsd.org>
4 9ae1b92d 2022-08-13 op * Copyright (c) 2013 Florian Obser <florian@openbsd.org>
6 9ae1b92d 2022-08-13 op * Permission to use, copy, modify, and distribute this software for any
7 9ae1b92d 2022-08-13 op * purpose with or without fee is hereby granted, provided that the above
8 9ae1b92d 2022-08-13 op * copyright notice and this permission notice appear in all copies.
10 9ae1b92d 2022-08-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 9ae1b92d 2022-08-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 9ae1b92d 2022-08-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 9ae1b92d 2022-08-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 9ae1b92d 2022-08-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 9ae1b92d 2022-08-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 9ae1b92d 2022-08-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 80189b3a 2022-08-13 op #include "config.h"
21 9ae1b92d 2022-08-13 op #include <sys/types.h>
22 9ae1b92d 2022-08-13 op #include <sys/ioctl.h>
23 9ae1b92d 2022-08-13 op #include <sys/socket.h>
24 9ae1b92d 2022-08-13 op #include <sys/stat.h>
25 9ae1b92d 2022-08-13 op #include <sys/time.h>
26 9ae1b92d 2022-08-13 op #include <sys/un.h>
27 9ae1b92d 2022-08-13 op #include <sys/wait.h>
28 9ae1b92d 2022-08-13 op #include <arpa/inet.h>
29 9ae1b92d 2022-08-13 op #include <fcntl.h>
30 9ae1b92d 2022-08-13 op #include <errno.h>
31 9ae1b92d 2022-08-13 op #include <limits.h>
32 9ae1b92d 2022-08-13 op #include <pwd.h>
33 9ae1b92d 2022-08-13 op #include <signal.h>
34 9ae1b92d 2022-08-13 op #include <stdarg.h>
35 9ae1b92d 2022-08-13 op #include <stdio.h>
36 9ae1b92d 2022-08-13 op #include <stdlib.h>
37 9ae1b92d 2022-08-13 op #include <string.h>
38 9ae1b92d 2022-08-13 op #include <syslog.h>
39 9ae1b92d 2022-08-13 op #include <unistd.h>
41 9ae1b92d 2022-08-13 op #define TIMEOUT_DEFAULT 120
42 9ae1b92d 2022-08-13 op #define TIMEOUT_MAX (86400 * 365)
43 9ae1b92d 2022-08-13 op #define SLOWCGI_USER "www"
45 9ae1b92d 2022-08-13 op #define FCGI_CONTENT_SIZE 65535
46 9ae1b92d 2022-08-13 op #define FCGI_PADDING_SIZE 255
47 9ae1b92d 2022-08-13 op #define FCGI_RECORD_SIZE \
48 9ae1b92d 2022-08-13 op (sizeof(struct fcgi_record_header) + FCGI_CONTENT_SIZE + FCGI_PADDING_SIZE)
50 9ae1b92d 2022-08-13 op #define FCGI_ALIGNMENT 8
51 9ae1b92d 2022-08-13 op #define FCGI_ALIGN(n) \
52 9ae1b92d 2022-08-13 op (((n) + (FCGI_ALIGNMENT - 1)) & ~(FCGI_ALIGNMENT - 1))
54 9ae1b92d 2022-08-13 op #define STDOUT_DONE 1
55 9ae1b92d 2022-08-13 op #define STDERR_DONE 2
56 9ae1b92d 2022-08-13 op #define SCRIPT_DONE 4
58 9ae1b92d 2022-08-13 op #define FCGI_BEGIN_REQUEST 1
59 9ae1b92d 2022-08-13 op #define FCGI_ABORT_REQUEST 2
60 9ae1b92d 2022-08-13 op #define FCGI_END_REQUEST 3
61 9ae1b92d 2022-08-13 op #define FCGI_PARAMS 4
62 9ae1b92d 2022-08-13 op #define FCGI_STDIN 5
63 9ae1b92d 2022-08-13 op #define FCGI_STDOUT 6
64 9ae1b92d 2022-08-13 op #define FCGI_STDERR 7
65 9ae1b92d 2022-08-13 op #define FCGI_DATA 8
66 9ae1b92d 2022-08-13 op #define FCGI_GET_VALUES 9
67 9ae1b92d 2022-08-13 op #define FCGI_GET_VALUES_RESULT 10
68 9ae1b92d 2022-08-13 op #define FCGI_UNKNOWN_TYPE 11
69 9ae1b92d 2022-08-13 op #define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
71 9ae1b92d 2022-08-13 op #define FCGI_REQUEST_COMPLETE 0
72 9ae1b92d 2022-08-13 op #define FCGI_CANT_MPX_CONN 1
73 9ae1b92d 2022-08-13 op #define FCGI_OVERLOADED 2
74 9ae1b92d 2022-08-13 op #define FCGI_UNKNOWN_ROLE 3
76 9ae1b92d 2022-08-13 op #define FD_RESERVE 5
77 9ae1b92d 2022-08-13 op #define FD_NEEDED 6
78 9ae1b92d 2022-08-13 op int cgi_inflight = 0;
80 9ae1b92d 2022-08-13 op struct listener {
81 9ae1b92d 2022-08-13 op struct event ev, pause;
84 9ae1b92d 2022-08-13 op struct env_val {
85 9ae1b92d 2022-08-13 op SLIST_ENTRY(env_val) entry;
88 9ae1b92d 2022-08-13 op SLIST_HEAD(env_head, env_val);
90 9ae1b92d 2022-08-13 op struct fcgi_record_header {
91 9ae1b92d 2022-08-13 op uint8_t version;
94 9ae1b92d 2022-08-13 op uint16_t content_len;
95 9ae1b92d 2022-08-13 op uint8_t padding_len;
96 9ae1b92d 2022-08-13 op uint8_t reserved;
99 9ae1b92d 2022-08-13 op struct fcgi_response {
100 9ae1b92d 2022-08-13 op TAILQ_ENTRY(fcgi_response) entry;
101 9ae1b92d 2022-08-13 op uint8_t data[FCGI_RECORD_SIZE];
102 9ae1b92d 2022-08-13 op size_t data_pos;
103 9ae1b92d 2022-08-13 op size_t data_len;
105 9ae1b92d 2022-08-13 op TAILQ_HEAD(fcgi_response_head, fcgi_response);
107 9ae1b92d 2022-08-13 op struct fcgi_stdin {
108 9ae1b92d 2022-08-13 op TAILQ_ENTRY(fcgi_stdin) entry;
109 9ae1b92d 2022-08-13 op uint8_t data[FCGI_RECORD_SIZE];
110 9ae1b92d 2022-08-13 op size_t data_pos;
111 9ae1b92d 2022-08-13 op size_t data_len;
113 9ae1b92d 2022-08-13 op TAILQ_HEAD(fcgi_stdin_head, fcgi_stdin);
115 9ae1b92d 2022-08-13 op struct request {
116 9ae1b92d 2022-08-13 op LIST_ENTRY(request) entry;
117 9ae1b92d 2022-08-13 op struct event ev;
118 9ae1b92d 2022-08-13 op struct event resp_ev;
119 9ae1b92d 2022-08-13 op struct event tmo;
121 9ae1b92d 2022-08-13 op uint8_t buf[FCGI_RECORD_SIZE];
122 9ae1b92d 2022-08-13 op size_t buf_pos;
123 9ae1b92d 2022-08-13 op size_t buf_len;
124 9ae1b92d 2022-08-13 op struct fcgi_response_head response_head;
125 9ae1b92d 2022-08-13 op struct fcgi_stdin_head stdin_head;
127 9ae1b92d 2022-08-13 op char script_name[PATH_MAX];
128 9ae1b92d 2022-08-13 op struct env_head env;
129 9ae1b92d 2022-08-13 op int env_count;
130 9ae1b92d 2022-08-13 op pid_t script_pid;
131 9ae1b92d 2022-08-13 op int script_status;
132 9ae1b92d 2022-08-13 op struct event script_ev;
133 9ae1b92d 2022-08-13 op struct event script_err_ev;
134 9ae1b92d 2022-08-13 op struct event script_stdin_ev;
135 9ae1b92d 2022-08-13 op int stdin_fd_closed;
136 9ae1b92d 2022-08-13 op int stdout_fd_closed;
137 9ae1b92d 2022-08-13 op int stderr_fd_closed;
138 9ae1b92d 2022-08-13 op uint8_t script_flags;
139 9ae1b92d 2022-08-13 op uint8_t request_started;
140 9ae1b92d 2022-08-13 op int inflight_fds_accounted;
143 9ae1b92d 2022-08-13 op LIST_HEAD(requests_head, request);
145 9ae1b92d 2022-08-13 op struct slowcgi_proc {
146 9ae1b92d 2022-08-13 op struct requests_head requests;
147 9ae1b92d 2022-08-13 op struct event ev_sigchld;
150 9ae1b92d 2022-08-13 op struct fcgi_begin_request_body {
151 9ae1b92d 2022-08-13 op uint16_t role;
152 9ae1b92d 2022-08-13 op uint8_t flags;
153 9ae1b92d 2022-08-13 op uint8_t reserved[5];
156 9ae1b92d 2022-08-13 op struct fcgi_end_request_body {
157 9ae1b92d 2022-08-13 op uint32_t app_status;
158 9ae1b92d 2022-08-13 op uint8_t protocol_status;
159 9ae1b92d 2022-08-13 op uint8_t reserved[3];
162 9ae1b92d 2022-08-13 op __dead void usage(void);
163 9ae1b92d 2022-08-13 op int slowcgi_listen(char *, struct passwd *);
164 9ae1b92d 2022-08-13 op void slowcgi_paused(int, short, void *);
165 9ae1b92d 2022-08-13 op int accept_reserve(int, struct sockaddr *, socklen_t *, int, int *);
166 9ae1b92d 2022-08-13 op void slowcgi_accept(int, short, void *);
167 9ae1b92d 2022-08-13 op void slowcgi_request(int, short, void *);
168 9ae1b92d 2022-08-13 op void slowcgi_response(int, short, void *);
169 9ae1b92d 2022-08-13 op void slowcgi_add_response(struct request *, struct fcgi_response *);
170 9ae1b92d 2022-08-13 op void slowcgi_timeout(int, short, void *);
171 9ae1b92d 2022-08-13 op void slowcgi_sig_handler(int, short, void *);
172 9ae1b92d 2022-08-13 op size_t parse_record(uint8_t * , size_t, struct request *);
173 9ae1b92d 2022-08-13 op void parse_begin_request(uint8_t *, uint16_t, struct request *,
175 9ae1b92d 2022-08-13 op void parse_params(uint8_t *, uint16_t, struct request *, uint16_t);
176 9ae1b92d 2022-08-13 op void parse_stdin(uint8_t *, uint16_t, struct request *, uint16_t);
177 9ae1b92d 2022-08-13 op void exec_cgi(struct request *);
178 9ae1b92d 2022-08-13 op void script_in(int, struct event *, struct request *, uint8_t);
179 9ae1b92d 2022-08-13 op void script_std_in(int, short, void *);
180 9ae1b92d 2022-08-13 op void script_err_in(int, short, void *);
181 9ae1b92d 2022-08-13 op void script_out(int, short, void *);
182 9ae1b92d 2022-08-13 op void create_end_record(struct request *);
183 9ae1b92d 2022-08-13 op void dump_fcgi_record(const char *,
184 9ae1b92d 2022-08-13 op struct fcgi_record_header *);
185 9ae1b92d 2022-08-13 op void dump_fcgi_record_header(const char *,
186 9ae1b92d 2022-08-13 op struct fcgi_record_header *);
187 9ae1b92d 2022-08-13 op void dump_fcgi_begin_request_body(const char *,
188 9ae1b92d 2022-08-13 op struct fcgi_begin_request_body *);
189 9ae1b92d 2022-08-13 op void dump_fcgi_end_request_body(const char *,
190 9ae1b92d 2022-08-13 op struct fcgi_end_request_body *);
191 9ae1b92d 2022-08-13 op void cleanup_request(struct request *);
193 9ae1b92d 2022-08-13 op struct loggers {
194 9ae1b92d 2022-08-13 op __dead void (*err)(int, const char *, ...)
195 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 2, 3)));
196 9ae1b92d 2022-08-13 op __dead void (*errx)(int, const char *, ...)
197 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 2, 3)));
198 9ae1b92d 2022-08-13 op void (*warn)(const char *, ...)
199 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 1, 2)));
200 9ae1b92d 2022-08-13 op void (*warnx)(const char *, ...)
201 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 1, 2)));
202 9ae1b92d 2022-08-13 op void (*info)(const char *, ...)
203 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 1, 2)));
204 9ae1b92d 2022-08-13 op void (*debug)(const char *, ...)
205 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 1, 2)));
208 9ae1b92d 2022-08-13 op const struct loggers conslogger = {
213 9ae1b92d 2022-08-13 op warnx, /* info */
214 9ae1b92d 2022-08-13 op warnx /* debug */
217 9ae1b92d 2022-08-13 op __dead void syslog_err(int, const char *, ...)
218 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 2, 3)));
219 9ae1b92d 2022-08-13 op __dead void syslog_errx(int, const char *, ...)
220 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 2, 3)));
221 9ae1b92d 2022-08-13 op void syslog_warn(const char *, ...)
222 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 1, 2)));
223 9ae1b92d 2022-08-13 op void syslog_warnx(const char *, ...)
224 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 1, 2)));
225 9ae1b92d 2022-08-13 op void syslog_info(const char *, ...)
226 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 1, 2)));
227 9ae1b92d 2022-08-13 op void syslog_debug(const char *, ...)
228 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 1, 2)));
229 9ae1b92d 2022-08-13 op void syslog_vstrerror(int, int, const char *, va_list)
230 9ae1b92d 2022-08-13 op __attribute__((__format__ (printf, 3, 0)));
232 9ae1b92d 2022-08-13 op const struct loggers syslogger = {
236 9ae1b92d 2022-08-13 op syslog_warnx,
241 9ae1b92d 2022-08-13 op const struct loggers *logger = &conslogger;
243 9ae1b92d 2022-08-13 op #define lerr(_e, _f...) logger->err((_e), _f)
244 9ae1b92d 2022-08-13 op #define lerrx(_e, _f...) logger->errx((_e), _f)
245 9ae1b92d 2022-08-13 op #define lwarn(_f...) logger->warn(_f)
246 9ae1b92d 2022-08-13 op #define lwarnx(_f...) logger->warnx(_f)
247 9ae1b92d 2022-08-13 op #define linfo(_f...) logger->info(_f)
248 9ae1b92d 2022-08-13 op #define ldebug(_f...) logger->debug(_f)
253 9ae1b92d 2022-08-13 op extern char *__progname;
254 9ae1b92d 2022-08-13 op fprintf(stderr,
255 9ae1b92d 2022-08-13 op "usage: %s [-dv] [-p path] [-s socket] [-t timeout] [-U user] "
256 9ae1b92d 2022-08-13 op "[-u user]\n", __progname);
260 9ae1b92d 2022-08-13 op struct timeval timeout = { TIMEOUT_DEFAULT, 0 };
261 9ae1b92d 2022-08-13 op struct slowcgi_proc slowcgi_proc;
262 9ae1b92d 2022-08-13 op int debug = 0;
263 9ae1b92d 2022-08-13 op int verbose = 0;
265 9ae1b92d 2022-08-13 op char *fcgi_socket = "/var/www/run/slowcgi.sock";
268 9ae1b92d 2022-08-13 op main(int argc, char *argv[])
270 9ae1b92d 2022-08-13 op extern char *__progname;
271 9ae1b92d 2022-08-13 op struct listener *l = NULL;
272 9ae1b92d 2022-08-13 op struct passwd *pw;
273 9ae1b92d 2022-08-13 op struct stat sb;
275 9ae1b92d 2022-08-13 op const char *chrootpath = NULL;
276 9ae1b92d 2022-08-13 op const char *sock_user = SLOWCGI_USER;
277 9ae1b92d 2022-08-13 op const char *slowcgi_user = SLOWCGI_USER;
278 9ae1b92d 2022-08-13 op const char *errstr;
281 9ae1b92d 2022-08-13 op * Ensure we have fds 0-2 open so that we have no fd overlaps
282 9ae1b92d 2022-08-13 op * in exec_cgi() later. Just exit on error, we don't have enough
283 9ae1b92d 2022-08-13 op * fds open to output an error message anywhere.
285 9ae1b92d 2022-08-13 op for (c=0; c < 3; c++) {
286 9ae1b92d 2022-08-13 op if (fstat(c, &sb) == -1) {
287 9ae1b92d 2022-08-13 op if ((fd = open("/dev/null", O_RDWR)) != -1) {
288 9ae1b92d 2022-08-13 op if (dup2(fd, c) == -1)
297 9ae1b92d 2022-08-13 op while ((c = getopt(argc, argv, "dp:s:t:U:u:v")) != -1) {
303 9ae1b92d 2022-08-13 op chrootpath = optarg;
306 9ae1b92d 2022-08-13 op fcgi_socket = optarg;
309 9ae1b92d 2022-08-13 op timeout.tv_sec = strtonum(optarg, 1, TIMEOUT_MAX,
311 9ae1b92d 2022-08-13 op if (errstr != NULL)
312 9ae1b92d 2022-08-13 op errx(1, "timeout is %s: %s", errstr, optarg);
315 9ae1b92d 2022-08-13 op sock_user = optarg;
318 9ae1b92d 2022-08-13 op slowcgi_user = optarg;
325 9ae1b92d 2022-08-13 op /* NOTREACHED */
329 9ae1b92d 2022-08-13 op if (geteuid() != 0)
330 9ae1b92d 2022-08-13 op errx(1, "need root privileges");
332 9ae1b92d 2022-08-13 op if (!debug && daemon(0, 0) == -1)
333 9ae1b92d 2022-08-13 op err(1, "daemon");
335 9ae1b92d 2022-08-13 op if (!debug) {
336 9ae1b92d 2022-08-13 op openlog(__progname, LOG_PID|LOG_NDELAY, LOG_DAEMON);
337 9ae1b92d 2022-08-13 op logger = &syslogger;
340 9ae1b92d 2022-08-13 op ldebug("sock_user: %s", sock_user);
341 9ae1b92d 2022-08-13 op pw = getpwnam(sock_user);
342 9ae1b92d 2022-08-13 op if (pw == NULL)
343 9ae1b92d 2022-08-13 op lerrx(1, "no %s user", sock_user);
345 9ae1b92d 2022-08-13 op fd = slowcgi_listen(fcgi_socket, pw);
347 9ae1b92d 2022-08-13 op ldebug("slowcgi_user: %s", slowcgi_user);
348 9ae1b92d 2022-08-13 op pw = getpwnam(slowcgi_user);
349 9ae1b92d 2022-08-13 op if (pw == NULL)
350 9ae1b92d 2022-08-13 op lerrx(1, "no %s user", slowcgi_user);
352 9ae1b92d 2022-08-13 op if (chrootpath == NULL)
353 9ae1b92d 2022-08-13 op chrootpath = pw->pw_dir;
355 9ae1b92d 2022-08-13 op if (chroot(chrootpath) == -1)
356 9ae1b92d 2022-08-13 op lerr(1, "chroot(%s)", chrootpath);
358 9ae1b92d 2022-08-13 op ldebug("chroot: %s", chrootpath);
360 9ae1b92d 2022-08-13 op if (chdir("/") == -1)
361 9ae1b92d 2022-08-13 op lerr(1, "chdir(/)");
363 9ae1b92d 2022-08-13 op if (setgroups(1, &pw->pw_gid) ||
364 9ae1b92d 2022-08-13 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
365 9ae1b92d 2022-08-13 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
366 9ae1b92d 2022-08-13 op lerr(1, "unable to revoke privs");
368 9ae1b92d 2022-08-13 op if (pledge("stdio rpath unix proc exec", NULL) == -1)
369 9ae1b92d 2022-08-13 op lerr(1, "pledge");
371 9ae1b92d 2022-08-13 op LIST_INIT(&slowcgi_proc.requests);
372 9ae1b92d 2022-08-13 op event_init();
374 9ae1b92d 2022-08-13 op l = calloc(1, sizeof(*l));
375 9ae1b92d 2022-08-13 op if (l == NULL)
376 9ae1b92d 2022-08-13 op lerr(1, "listener ev alloc");
378 9ae1b92d 2022-08-13 op event_set(&l->ev, fd, EV_READ | EV_PERSIST, slowcgi_accept, l);
379 9ae1b92d 2022-08-13 op event_add(&l->ev, NULL);
380 9ae1b92d 2022-08-13 op evtimer_set(&l->pause, slowcgi_paused, l);
382 9ae1b92d 2022-08-13 op signal_set(&slowcgi_proc.ev_sigchld, SIGCHLD, slowcgi_sig_handler,
383 9ae1b92d 2022-08-13 op &slowcgi_proc);
384 9ae1b92d 2022-08-13 op signal(SIGPIPE, SIG_IGN);
386 9ae1b92d 2022-08-13 op signal_add(&slowcgi_proc.ev_sigchld, NULL);
388 9ae1b92d 2022-08-13 op event_dispatch();
393 9ae1b92d 2022-08-13 op slowcgi_listen(char *path, struct passwd *pw)
395 9ae1b92d 2022-08-13 op struct sockaddr_un sun;
396 9ae1b92d 2022-08-13 op mode_t old_umask;
399 9ae1b92d 2022-08-13 op if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
401 9ae1b92d 2022-08-13 op lerr(1, "slowcgi_listen: socket");
403 9ae1b92d 2022-08-13 op bzero(&sun, sizeof(sun));
404 9ae1b92d 2022-08-13 op sun.sun_family = AF_UNIX;
405 9ae1b92d 2022-08-13 op if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
406 9ae1b92d 2022-08-13 op sizeof(sun.sun_path))
407 9ae1b92d 2022-08-13 op lerrx(1, "socket path too long");
409 9ae1b92d 2022-08-13 op if (unlink(path) == -1)
410 9ae1b92d 2022-08-13 op if (errno != ENOENT)
411 9ae1b92d 2022-08-13 op lerr(1, "slowcgi_listen: unlink %s", path);
413 9ae1b92d 2022-08-13 op old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
415 9ae1b92d 2022-08-13 op if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
416 9ae1b92d 2022-08-13 op lerr(1,"slowcgi_listen: bind: %s", path);
418 9ae1b92d 2022-08-13 op umask(old_umask);
420 9ae1b92d 2022-08-13 op if (chown(path, pw->pw_uid, pw->pw_gid) == -1)
421 9ae1b92d 2022-08-13 op lerr(1, "slowcgi_listen: chown: %s", path);
423 9ae1b92d 2022-08-13 op if (listen(fd, 5) == -1)
424 9ae1b92d 2022-08-13 op lerr(1, "listen");
426 9ae1b92d 2022-08-13 op ldebug("socket: %s", path);
431 9ae1b92d 2022-08-13 op slowcgi_paused(int fd, short events, void *arg)
433 9ae1b92d 2022-08-13 op struct listener *l = arg;
434 9ae1b92d 2022-08-13 op event_add(&l->ev, NULL);
438 9ae1b92d 2022-08-13 op accept_reserve(int sockfd, struct sockaddr *addr, socklen_t *addrlen,
439 9ae1b92d 2022-08-13 op int reserve, int *counter)
442 9ae1b92d 2022-08-13 op if (getdtablecount() + reserve +
443 9ae1b92d 2022-08-13 op ((*counter + 1) * FD_NEEDED) >= getdtablesize()) {
444 9ae1b92d 2022-08-13 op ldebug("inflight fds exceeded");
445 9ae1b92d 2022-08-13 op errno = EMFILE;
449 9ae1b92d 2022-08-13 op if ((ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC))
451 9ae1b92d 2022-08-13 op (*counter)++;
452 9ae1b92d 2022-08-13 op ldebug("inflight incremented, now %d", *counter);
458 9ae1b92d 2022-08-13 op slowcgi_accept(int fd, short events, void *arg)
460 9ae1b92d 2022-08-13 op struct listener *l;
461 9ae1b92d 2022-08-13 op struct sockaddr_storage ss;
462 9ae1b92d 2022-08-13 op struct timeval backoff;
463 9ae1b92d 2022-08-13 op struct request *c;
464 9ae1b92d 2022-08-13 op socklen_t len;
468 9ae1b92d 2022-08-13 op backoff.tv_sec = 1;
469 9ae1b92d 2022-08-13 op backoff.tv_usec = 0;
472 9ae1b92d 2022-08-13 op len = sizeof(ss);
473 9ae1b92d 2022-08-13 op if ((s = accept_reserve(fd, (struct sockaddr *)&ss,
474 9ae1b92d 2022-08-13 op &len, FD_RESERVE, &cgi_inflight)) == -1) {
475 9ae1b92d 2022-08-13 op switch (errno) {
477 9ae1b92d 2022-08-13 op case EWOULDBLOCK:
478 9ae1b92d 2022-08-13 op case ECONNABORTED:
482 9ae1b92d 2022-08-13 op event_del(&l->ev);
483 9ae1b92d 2022-08-13 op evtimer_add(&l->pause, &backoff);
486 9ae1b92d 2022-08-13 op lerr(1, "accept");
490 9ae1b92d 2022-08-13 op c = calloc(1, sizeof(*c));
491 9ae1b92d 2022-08-13 op if (c == NULL) {
492 9ae1b92d 2022-08-13 op lwarn("cannot calloc request");
494 9ae1b92d 2022-08-13 op cgi_inflight--;
498 9ae1b92d 2022-08-13 op c->buf_pos = 0;
499 9ae1b92d 2022-08-13 op c->buf_len = 0;
500 9ae1b92d 2022-08-13 op c->request_started = 0;
501 9ae1b92d 2022-08-13 op c->stdin_fd_closed = c->stdout_fd_closed = c->stderr_fd_closed = 0;
502 9ae1b92d 2022-08-13 op c->inflight_fds_accounted = 0;
503 9ae1b92d 2022-08-13 op TAILQ_INIT(&c->response_head);
504 9ae1b92d 2022-08-13 op TAILQ_INIT(&c->stdin_head);
506 9ae1b92d 2022-08-13 op event_set(&c->ev, s, EV_READ | EV_PERSIST, slowcgi_request, c);
507 9ae1b92d 2022-08-13 op event_add(&c->ev, NULL);
508 9ae1b92d 2022-08-13 op event_set(&c->resp_ev, s, EV_WRITE | EV_PERSIST, slowcgi_response, c);
509 9ae1b92d 2022-08-13 op evtimer_set(&c->tmo, slowcgi_timeout, c);
510 9ae1b92d 2022-08-13 op evtimer_add(&c->tmo, &timeout);
511 9ae1b92d 2022-08-13 op LIST_INSERT_HEAD(&slowcgi_proc.requests, c, entry);
515 9ae1b92d 2022-08-13 op slowcgi_timeout(int fd, short events, void *arg)
517 9ae1b92d 2022-08-13 op cleanup_request((struct request*) arg);
521 9ae1b92d 2022-08-13 op slowcgi_sig_handler(int sig, short event, void *arg)
523 9ae1b92d 2022-08-13 op struct request *c;
524 9ae1b92d 2022-08-13 op struct slowcgi_proc *p;
530 9ae1b92d 2022-08-13 op switch (sig) {
531 9ae1b92d 2022-08-13 op case SIGCHLD:
532 9ae1b92d 2022-08-13 op while ((pid = waitpid(WAIT_ANY, &status, WNOHANG)) > 0) {
533 9ae1b92d 2022-08-13 op LIST_FOREACH(c, &p->requests, entry)
534 9ae1b92d 2022-08-13 op if (c->script_pid == pid)
536 9ae1b92d 2022-08-13 op if (c == NULL) {
537 9ae1b92d 2022-08-13 op lwarnx("caught exit of unknown child %i", pid);
541 9ae1b92d 2022-08-13 op if (WIFSIGNALED(status))
542 9ae1b92d 2022-08-13 op c->script_status = WTERMSIG(status);
544 9ae1b92d 2022-08-13 op c->script_status = WEXITSTATUS(status);
546 9ae1b92d 2022-08-13 op if (c->script_flags == (STDOUT_DONE | STDERR_DONE))
547 9ae1b92d 2022-08-13 op create_end_record(c);
548 9ae1b92d 2022-08-13 op c->script_flags |= SCRIPT_DONE;
550 9ae1b92d 2022-08-13 op ldebug("wait: %s", c->script_name);
552 9ae1b92d 2022-08-13 op if (pid == -1 && errno != ECHILD)
553 9ae1b92d 2022-08-13 op lwarn("waitpid");
556 9ae1b92d 2022-08-13 op lerr(1, "unexpected signal: %d", sig);
562 9ae1b92d 2022-08-13 op slowcgi_add_response(struct request *c, struct fcgi_response *resp)
564 9ae1b92d 2022-08-13 op struct fcgi_record_header *header;
565 9ae1b92d 2022-08-13 op size_t padded_len;
567 9ae1b92d 2022-08-13 op header = (struct fcgi_record_header*)resp->data;
569 9ae1b92d 2022-08-13 op /* The FastCGI spec suggests to align the output buffer */
570 9ae1b92d 2022-08-13 op padded_len = FCGI_ALIGN(resp->data_len);
571 9ae1b92d 2022-08-13 op if (padded_len > resp->data_len) {
572 9ae1b92d 2022-08-13 op /* There should always be FCGI_PADDING_SIZE bytes left */
573 9ae1b92d 2022-08-13 op if (padded_len > FCGI_RECORD_SIZE)
574 9ae1b92d 2022-08-13 op lerr(1, "response too long");
575 9ae1b92d 2022-08-13 op header->padding_len = padded_len - resp->data_len;
576 9ae1b92d 2022-08-13 op resp->data_len = padded_len;
579 9ae1b92d 2022-08-13 op TAILQ_INSERT_TAIL(&c->response_head, resp, entry);
580 9ae1b92d 2022-08-13 op event_add(&c->resp_ev, NULL);
584 9ae1b92d 2022-08-13 op slowcgi_response(int fd, short events, void *arg)
586 9ae1b92d 2022-08-13 op struct request *c;
587 9ae1b92d 2022-08-13 op struct fcgi_record_header *header;
588 9ae1b92d 2022-08-13 op struct fcgi_response *resp;
593 9ae1b92d 2022-08-13 op while ((resp = TAILQ_FIRST(&c->response_head))) {
594 9ae1b92d 2022-08-13 op header = (struct fcgi_record_header*) resp->data;
595 9ae1b92d 2022-08-13 op if (debug > 1)
596 9ae1b92d 2022-08-13 op dump_fcgi_record("resp ", header);
598 9ae1b92d 2022-08-13 op n = write(fd, resp->data + resp->data_pos, resp->data_len);
599 9ae1b92d 2022-08-13 op if (n == -1) {
600 9ae1b92d 2022-08-13 op if (errno == EAGAIN || errno == EINTR)
602 9ae1b92d 2022-08-13 op cleanup_request(c);
605 9ae1b92d 2022-08-13 op resp->data_pos += n;
606 9ae1b92d 2022-08-13 op resp->data_len -= n;
607 9ae1b92d 2022-08-13 op if (resp->data_len == 0) {
608 9ae1b92d 2022-08-13 op TAILQ_REMOVE(&c->response_head, resp, entry);
613 9ae1b92d 2022-08-13 op if (TAILQ_EMPTY(&c->response_head)) {
614 9ae1b92d 2022-08-13 op if (c->script_flags == (STDOUT_DONE | STDERR_DONE |
615 9ae1b92d 2022-08-13 op SCRIPT_DONE))
616 9ae1b92d 2022-08-13 op cleanup_request(c);
618 9ae1b92d 2022-08-13 op event_del(&c->resp_ev);
623 9ae1b92d 2022-08-13 op slowcgi_request(int fd, short events, void *arg)
625 9ae1b92d 2022-08-13 op struct request *c;
627 9ae1b92d 2022-08-13 op size_t parsed;
631 9ae1b92d 2022-08-13 op n = read(fd, c->buf + c->buf_pos + c->buf_len,
632 9ae1b92d 2022-08-13 op FCGI_RECORD_SIZE - c->buf_pos-c->buf_len);
636 9ae1b92d 2022-08-13 op switch (errno) {
646 9ae1b92d 2022-08-13 op ldebug("closed connection");
652 9ae1b92d 2022-08-13 op c->buf_len += n;
655 9ae1b92d 2022-08-13 op * Parse the records as they are received. Per the FastCGI
656 9ae1b92d 2022-08-13 op * specification, the server need only receive the FastCGI
657 9ae1b92d 2022-08-13 op * parameter records in full; it is free to begin execution
658 9ae1b92d 2022-08-13 op * at that point, which is what happens here.
661 9ae1b92d 2022-08-13 op parsed = parse_record(c->buf + c->buf_pos, c->buf_len, c);
662 9ae1b92d 2022-08-13 op c->buf_pos += parsed;
663 9ae1b92d 2022-08-13 op c->buf_len -= parsed;
664 9ae1b92d 2022-08-13 op } while (parsed > 0 && c->buf_len > 0);
666 9ae1b92d 2022-08-13 op /* Make space for further reads */
667 9ae1b92d 2022-08-13 op if (c->buf_len > 0) {
668 9ae1b92d 2022-08-13 op bcopy(c->buf + c->buf_pos, c->buf, c->buf_len);
669 9ae1b92d 2022-08-13 op c->buf_pos = 0;
673 9ae1b92d 2022-08-13 op cleanup_request(c);
677 9ae1b92d 2022-08-13 op parse_begin_request(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
679 9ae1b92d 2022-08-13 op /* XXX -- FCGI_CANT_MPX_CONN */
680 9ae1b92d 2022-08-13 op if (c->request_started) {
681 9ae1b92d 2022-08-13 op lwarnx("unexpected FCGI_BEGIN_REQUEST, ignoring");
685 9ae1b92d 2022-08-13 op if (n != sizeof(struct fcgi_begin_request_body)) {
686 9ae1b92d 2022-08-13 op lwarnx("wrong size %d != %lu", n,
687 9ae1b92d 2022-08-13 op sizeof(struct fcgi_begin_request_body));
691 9ae1b92d 2022-08-13 op c->request_started = 1;
694 9ae1b92d 2022-08-13 op SLIST_INIT(&c->env);
695 9ae1b92d 2022-08-13 op c->env_count = 0;
699 9ae1b92d 2022-08-13 op parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
701 9ae1b92d 2022-08-13 op struct env_val *env_entry;
702 9ae1b92d 2022-08-13 op uint32_t name_len, val_len;
704 9ae1b92d 2022-08-13 op if (!c->request_started) {
705 9ae1b92d 2022-08-13 op lwarnx("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
709 9ae1b92d 2022-08-13 op if (c->id != id) {
710 9ae1b92d 2022-08-13 op lwarnx("unexpected id, ignoring");
715 9ae1b92d 2022-08-13 op * If this is the last FastCGI parameter record,
716 9ae1b92d 2022-08-13 op * begin execution of the CGI script.
718 9ae1b92d 2022-08-13 op if (n == 0) {
723 9ae1b92d 2022-08-13 op while (n > 0) {
724 9ae1b92d 2022-08-13 op if (buf[0] >> 7 == 0) {
725 9ae1b92d 2022-08-13 op name_len = buf[0];
730 9ae1b92d 2022-08-13 op name_len = ((buf[0] & 0x7f) << 24) +
731 9ae1b92d 2022-08-13 op (buf[1] << 16) + (buf[2] << 8) + buf[3];
739 9ae1b92d 2022-08-13 op if (buf[0] >> 7 == 0) {
740 9ae1b92d 2022-08-13 op val_len = buf[0];
745 9ae1b92d 2022-08-13 op val_len = ((buf[0] & 0x7f) << 24) +
746 9ae1b92d 2022-08-13 op (buf[1] << 16) + (buf[2] << 8) +
756 9ae1b92d 2022-08-13 op if (n < name_len + val_len)
759 9ae1b92d 2022-08-13 op if ((env_entry = malloc(sizeof(struct env_val))) == NULL) {
760 9ae1b92d 2022-08-13 op lwarnx("cannot allocate env_entry");
764 9ae1b92d 2022-08-13 op if ((env_entry->val = calloc(sizeof(char), name_len + val_len +
765 9ae1b92d 2022-08-13 op 2)) == NULL) {
766 9ae1b92d 2022-08-13 op lwarnx("cannot allocate env_entry->val");
767 9ae1b92d 2022-08-13 op free(env_entry);
771 9ae1b92d 2022-08-13 op bcopy(buf, env_entry->val, name_len);
772 9ae1b92d 2022-08-13 op buf += name_len;
773 9ae1b92d 2022-08-13 op n -= name_len;
775 9ae1b92d 2022-08-13 op env_entry->val[name_len] = '\0';
776 9ae1b92d 2022-08-13 op if (val_len < PATH_MAX && strcmp(env_entry->val,
777 9ae1b92d 2022-08-13 op "SCRIPT_NAME") == 0 && c->script_name[0] == '\0') {
778 9ae1b92d 2022-08-13 op bcopy(buf, c->script_name, val_len);
779 9ae1b92d 2022-08-13 op c->script_name[val_len] = '\0';
780 9ae1b92d 2022-08-13 op } else if (val_len < PATH_MAX && strcmp(env_entry->val,
781 9ae1b92d 2022-08-13 op "SCRIPT_FILENAME") == 0) {
782 9ae1b92d 2022-08-13 op bcopy(buf, c->script_name, val_len);
783 9ae1b92d 2022-08-13 op c->script_name[val_len] = '\0';
785 9ae1b92d 2022-08-13 op env_entry->val[name_len] = '=';
787 9ae1b92d 2022-08-13 op bcopy(buf, (env_entry->val) + name_len + 1, val_len);
788 9ae1b92d 2022-08-13 op buf += val_len;
789 9ae1b92d 2022-08-13 op n -= val_len;
791 9ae1b92d 2022-08-13 op SLIST_INSERT_HEAD(&c->env, env_entry, entry);
792 9ae1b92d 2022-08-13 op ldebug("env[%d], %s", c->env_count, env_entry->val);
793 9ae1b92d 2022-08-13 op c->env_count++;
798 9ae1b92d 2022-08-13 op parse_stdin(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
800 9ae1b92d 2022-08-13 op struct fcgi_stdin *node;
802 9ae1b92d 2022-08-13 op if (c->id != id) {
803 9ae1b92d 2022-08-13 op lwarnx("unexpected id, ignoring");
807 9ae1b92d 2022-08-13 op if ((node = calloc(1, sizeof(struct fcgi_stdin))) == NULL) {
808 9ae1b92d 2022-08-13 op lwarnx("cannot calloc stdin node");
812 9ae1b92d 2022-08-13 op bcopy(buf, node->data, n);
813 9ae1b92d 2022-08-13 op node->data_pos = 0;
814 9ae1b92d 2022-08-13 op node->data_len = n;
816 9ae1b92d 2022-08-13 op TAILQ_INSERT_TAIL(&c->stdin_head, node, entry);
818 9ae1b92d 2022-08-13 op if (event_initialized(&c->script_stdin_ev))
819 9ae1b92d 2022-08-13 op event_add(&c->script_stdin_ev, NULL);
823 9ae1b92d 2022-08-13 op parse_record(uint8_t *buf, size_t n, struct request *c)
825 9ae1b92d 2022-08-13 op struct fcgi_record_header *h;
827 9ae1b92d 2022-08-13 op if (n < sizeof(struct fcgi_record_header))
830 9ae1b92d 2022-08-13 op h = (struct fcgi_record_header*) buf;
832 9ae1b92d 2022-08-13 op if (debug > 1)
833 9ae1b92d 2022-08-13 op dump_fcgi_record("", h);
835 9ae1b92d 2022-08-13 op if (n < sizeof(struct fcgi_record_header) + ntohs(h->content_len)
836 9ae1b92d 2022-08-13 op + h->padding_len)
839 9ae1b92d 2022-08-13 op if (h->version != 1)
840 9ae1b92d 2022-08-13 op lerrx(1, "wrong version");
842 9ae1b92d 2022-08-13 op switch (h->type) {
843 9ae1b92d 2022-08-13 op case FCGI_BEGIN_REQUEST:
844 9ae1b92d 2022-08-13 op parse_begin_request(buf + sizeof(struct fcgi_record_header),
845 9ae1b92d 2022-08-13 op ntohs(h->content_len), c, ntohs(h->id));
847 9ae1b92d 2022-08-13 op case FCGI_PARAMS:
848 9ae1b92d 2022-08-13 op parse_params(buf + sizeof(struct fcgi_record_header),
849 9ae1b92d 2022-08-13 op ntohs(h->content_len), c, ntohs(h->id));
851 9ae1b92d 2022-08-13 op case FCGI_STDIN:
852 9ae1b92d 2022-08-13 op parse_stdin(buf + sizeof(struct fcgi_record_header),
853 9ae1b92d 2022-08-13 op ntohs(h->content_len), c, ntohs(h->id));
856 9ae1b92d 2022-08-13 op lwarnx("unimplemented type %d", h->type);
860 9ae1b92d 2022-08-13 op return (sizeof(struct fcgi_record_header) + ntohs(h->content_len)
861 9ae1b92d 2022-08-13 op + h->padding_len);
865 9ae1b92d 2022-08-13 op * Fork a new CGI process to handle the request, translating
866 9ae1b92d 2022-08-13 op * between FastCGI parameter records and CGI's environment variables,
867 9ae1b92d 2022-08-13 op * as well as between the CGI process' stdin/stdout and the
868 9ae1b92d 2022-08-13 op * corresponding FastCGI records.
871 9ae1b92d 2022-08-13 op exec_cgi(struct request *c)
873 9ae1b92d 2022-08-13 op struct env_val *env_entry;
874 9ae1b92d 2022-08-13 op int s_in[2], s_out[2], s_err[2], i;
876 9ae1b92d 2022-08-13 op char *argv[2];
882 9ae1b92d 2022-08-13 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, s_in) == -1)
883 9ae1b92d 2022-08-13 op lerr(1, "socketpair");
884 9ae1b92d 2022-08-13 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, s_out) == -1)
885 9ae1b92d 2022-08-13 op lerr(1, "socketpair");
886 9ae1b92d 2022-08-13 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, s_err) == -1)
887 9ae1b92d 2022-08-13 op lerr(1, "socketpair");
888 9ae1b92d 2022-08-13 op cgi_inflight--;
889 9ae1b92d 2022-08-13 op c->inflight_fds_accounted = 1;
890 9ae1b92d 2022-08-13 op ldebug("fork: %s", c->script_name);
892 9ae1b92d 2022-08-13 op switch (pid = fork()) {
894 9ae1b92d 2022-08-13 op c->script_status = errno;
896 9ae1b92d 2022-08-13 op lwarn("fork");
898 9ae1b92d 2022-08-13 op close(s_in[0]);
899 9ae1b92d 2022-08-13 op close(s_out[0]);
900 9ae1b92d 2022-08-13 op close(s_err[0]);
902 9ae1b92d 2022-08-13 op close(s_in[1]);
903 9ae1b92d 2022-08-13 op close(s_out[1]);
904 9ae1b92d 2022-08-13 op close(s_err[1]);
906 9ae1b92d 2022-08-13 op c->stdin_fd_closed = c->stdout_fd_closed =
907 9ae1b92d 2022-08-13 op c->stderr_fd_closed = 1;
908 9ae1b92d 2022-08-13 op c->script_flags = (STDOUT_DONE | STDERR_DONE | SCRIPT_DONE);
910 9ae1b92d 2022-08-13 op create_end_record(c);
913 9ae1b92d 2022-08-13 op /* Child process */
914 9ae1b92d 2022-08-13 op if (pledge("stdio rpath exec", NULL) == -1)
915 9ae1b92d 2022-08-13 op lerr(1, "pledge");
916 9ae1b92d 2022-08-13 op close(s_in[0]);
917 9ae1b92d 2022-08-13 op close(s_out[0]);
918 9ae1b92d 2022-08-13 op close(s_err[0]);
920 9ae1b92d 2022-08-13 op if (dup2(s_in[1], STDIN_FILENO) == -1)
922 9ae1b92d 2022-08-13 op if (dup2(s_out[1], STDOUT_FILENO) == -1)
924 9ae1b92d 2022-08-13 op if (dup2(s_err[1], STDERR_FILENO) == -1)
927 9ae1b92d 2022-08-13 op close(s_in[1]);
928 9ae1b92d 2022-08-13 op close(s_out[1]);
929 9ae1b92d 2022-08-13 op close(s_err[1]);
931 9ae1b92d 2022-08-13 op signal(SIGPIPE, SIG_DFL);
933 9ae1b92d 2022-08-13 op path = strrchr(c->script_name, '/');
934 9ae1b92d 2022-08-13 op if (path != NULL) {
935 9ae1b92d 2022-08-13 op if (path != c->script_name) {
936 9ae1b92d 2022-08-13 op *path = '\0';
937 9ae1b92d 2022-08-13 op if (chdir(c->script_name) == -1)
938 9ae1b92d 2022-08-13 op lwarn("cannot chdir to %s",
939 9ae1b92d 2022-08-13 op c->script_name);
942 9ae1b92d 2022-08-13 op if (chdir("/") == -1)
943 9ae1b92d 2022-08-13 op lwarn("cannot chdir to /");
946 9ae1b92d 2022-08-13 op argv[0] = c->script_name;
947 9ae1b92d 2022-08-13 op argv[1] = NULL;
948 9ae1b92d 2022-08-13 op if ((env = calloc(c->env_count + 1, sizeof(char*))) == NULL)
950 9ae1b92d 2022-08-13 op SLIST_FOREACH(env_entry, &c->env, entry)
951 9ae1b92d 2022-08-13 op env[i++] = env_entry->val;
952 9ae1b92d 2022-08-13 op env[i++] = NULL;
953 9ae1b92d 2022-08-13 op execve(c->script_name, argv, env);
954 9ae1b92d 2022-08-13 op lwarn("execve %s", c->script_name);
959 9ae1b92d 2022-08-13 op /* Parent process*/
960 9ae1b92d 2022-08-13 op close(s_in[1]);
961 9ae1b92d 2022-08-13 op close(s_out[1]);
962 9ae1b92d 2022-08-13 op close(s_err[1]);
964 9ae1b92d 2022-08-13 op fcntl(s_in[0], F_SETFD, FD_CLOEXEC);
965 9ae1b92d 2022-08-13 op fcntl(s_out[0], F_SETFD, FD_CLOEXEC);
966 9ae1b92d 2022-08-13 op fcntl(s_err[0], F_SETFD, FD_CLOEXEC);
968 9ae1b92d 2022-08-13 op if (ioctl(s_in[0], FIONBIO, &on) == -1)
969 9ae1b92d 2022-08-13 op lerr(1, "script ioctl(FIONBIO)");
970 9ae1b92d 2022-08-13 op if (ioctl(s_out[0], FIONBIO, &on) == -1)
971 9ae1b92d 2022-08-13 op lerr(1, "script ioctl(FIONBIO)");
972 9ae1b92d 2022-08-13 op if (ioctl(s_err[0], FIONBIO, &on) == -1)
973 9ae1b92d 2022-08-13 op lerr(1, "script ioctl(FIONBIO)");
975 9ae1b92d 2022-08-13 op c->script_pid = pid;
976 9ae1b92d 2022-08-13 op event_set(&c->script_stdin_ev, s_in[0], EV_WRITE | EV_PERSIST,
977 9ae1b92d 2022-08-13 op script_out, c);
978 9ae1b92d 2022-08-13 op event_add(&c->script_stdin_ev, NULL);
979 9ae1b92d 2022-08-13 op event_set(&c->script_ev, s_out[0], EV_READ | EV_PERSIST,
980 9ae1b92d 2022-08-13 op script_std_in, c);
981 9ae1b92d 2022-08-13 op event_add(&c->script_ev, NULL);
982 9ae1b92d 2022-08-13 op event_set(&c->script_err_ev, s_err[0], EV_READ | EV_PERSIST,
983 9ae1b92d 2022-08-13 op script_err_in, c);
984 9ae1b92d 2022-08-13 op event_add(&c->script_err_ev, NULL);
988 9ae1b92d 2022-08-13 op create_end_record(struct request *c)
990 9ae1b92d 2022-08-13 op struct fcgi_response *resp;
991 9ae1b92d 2022-08-13 op struct fcgi_record_header *header;
992 9ae1b92d 2022-08-13 op struct fcgi_end_request_body *end_request;
994 9ae1b92d 2022-08-13 op if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
995 9ae1b92d 2022-08-13 op lwarnx("cannot malloc fcgi_response");
998 9ae1b92d 2022-08-13 op header = (struct fcgi_record_header*) resp->data;
999 9ae1b92d 2022-08-13 op header->version = 1;
1000 9ae1b92d 2022-08-13 op header->type = FCGI_END_REQUEST;
1001 9ae1b92d 2022-08-13 op header->id = htons(c->id);
1002 9ae1b92d 2022-08-13 op header->content_len = htons(sizeof(struct
1003 9ae1b92d 2022-08-13 op fcgi_end_request_body));
1004 9ae1b92d 2022-08-13 op header->padding_len = 0;
1005 9ae1b92d 2022-08-13 op header->reserved = 0;
1006 9ae1b92d 2022-08-13 op end_request = (struct fcgi_end_request_body *) (resp->data +
1007 9ae1b92d 2022-08-13 op sizeof(struct fcgi_record_header));
1008 9ae1b92d 2022-08-13 op end_request->app_status = htonl(c->script_status);
1009 9ae1b92d 2022-08-13 op end_request->protocol_status = FCGI_REQUEST_COMPLETE;
1010 9ae1b92d 2022-08-13 op end_request->reserved[0] = 0;
1011 9ae1b92d 2022-08-13 op end_request->reserved[1] = 0;
1012 9ae1b92d 2022-08-13 op end_request->reserved[2] = 0;
1013 9ae1b92d 2022-08-13 op resp->data_pos = 0;
1014 9ae1b92d 2022-08-13 op resp->data_len = sizeof(struct fcgi_end_request_body) +
1015 9ae1b92d 2022-08-13 op sizeof(struct fcgi_record_header);
1016 9ae1b92d 2022-08-13 op slowcgi_add_response(c, resp);
1020 9ae1b92d 2022-08-13 op script_in(int fd, struct event *ev, struct request *c, uint8_t type)
1022 9ae1b92d 2022-08-13 op struct fcgi_response *resp;
1023 9ae1b92d 2022-08-13 op struct fcgi_record_header *header;
1026 9ae1b92d 2022-08-13 op if ((resp = calloc(1, sizeof(struct fcgi_response))) == NULL) {
1027 9ae1b92d 2022-08-13 op lwarnx("cannot malloc fcgi_response");
1030 9ae1b92d 2022-08-13 op header = (struct fcgi_record_header*) resp->data;
1031 9ae1b92d 2022-08-13 op header->version = 1;
1032 9ae1b92d 2022-08-13 op header->type = type;
1033 9ae1b92d 2022-08-13 op header->id = htons(c->id);
1034 9ae1b92d 2022-08-13 op header->padding_len = 0;
1035 9ae1b92d 2022-08-13 op header->reserved = 0;
1037 9ae1b92d 2022-08-13 op n = read(fd, resp->data + sizeof(struct fcgi_record_header),
1038 9ae1b92d 2022-08-13 op FCGI_CONTENT_SIZE);
1040 9ae1b92d 2022-08-13 op if (n == -1) {
1041 9ae1b92d 2022-08-13 op switch (errno) {
1043 9ae1b92d 2022-08-13 op case EAGAIN:
1047 9ae1b92d 2022-08-13 op n = 0; /* fake empty FCGI_STD{OUT,ERR} response */
1050 9ae1b92d 2022-08-13 op header->content_len = htons(n);
1051 9ae1b92d 2022-08-13 op resp->data_pos = 0;
1052 9ae1b92d 2022-08-13 op resp->data_len = n + sizeof(struct fcgi_record_header);
1053 9ae1b92d 2022-08-13 op slowcgi_add_response(c, resp);
1055 9ae1b92d 2022-08-13 op if (n == 0) {
1056 9ae1b92d 2022-08-13 op if (type == FCGI_STDOUT)
1057 9ae1b92d 2022-08-13 op c->script_flags |= STDOUT_DONE;
1059 9ae1b92d 2022-08-13 op c->script_flags |= STDERR_DONE;
1061 9ae1b92d 2022-08-13 op if (c->script_flags == (STDOUT_DONE | STDERR_DONE |
1062 9ae1b92d 2022-08-13 op SCRIPT_DONE)) {
1063 9ae1b92d 2022-08-13 op create_end_record(c);
1065 9ae1b92d 2022-08-13 op event_del(ev);
1067 9ae1b92d 2022-08-13 op if (type == FCGI_STDOUT)
1068 9ae1b92d 2022-08-13 op c->stdout_fd_closed = 1;
1070 9ae1b92d 2022-08-13 op c->stderr_fd_closed = 1;
1075 9ae1b92d 2022-08-13 op script_std_in(int fd, short events, void *arg)
1077 9ae1b92d 2022-08-13 op struct request *c = arg;
1078 9ae1b92d 2022-08-13 op script_in(fd, &c->script_ev, c, FCGI_STDOUT);
1082 9ae1b92d 2022-08-13 op script_err_in(int fd, short events, void *arg)
1084 9ae1b92d 2022-08-13 op struct request *c = arg;
1085 9ae1b92d 2022-08-13 op script_in(fd, &c->script_err_ev, c, FCGI_STDERR);
1089 9ae1b92d 2022-08-13 op script_out(int fd, short events, void *arg)
1091 9ae1b92d 2022-08-13 op struct request *c;
1092 9ae1b92d 2022-08-13 op struct fcgi_stdin *node;
1097 9ae1b92d 2022-08-13 op while ((node = TAILQ_FIRST(&c->stdin_head))) {
1098 9ae1b92d 2022-08-13 op if (node->data_len == 0) { /* end of stdin marker */
1100 9ae1b92d 2022-08-13 op c->stdin_fd_closed = 1;
1103 9ae1b92d 2022-08-13 op n = write(fd, node->data + node->data_pos, node->data_len);
1104 9ae1b92d 2022-08-13 op if (n == -1) {
1105 9ae1b92d 2022-08-13 op if (errno == EAGAIN || errno == EINTR)
1107 9ae1b92d 2022-08-13 op event_del(&c->script_stdin_ev);
1110 9ae1b92d 2022-08-13 op node->data_pos += n;
1111 9ae1b92d 2022-08-13 op node->data_len -= n;
1112 9ae1b92d 2022-08-13 op if (node->data_len == 0) {
1113 9ae1b92d 2022-08-13 op TAILQ_REMOVE(&c->stdin_head, node, entry);
1117 9ae1b92d 2022-08-13 op event_del(&c->script_stdin_ev);
1121 9ae1b92d 2022-08-13 op cleanup_request(struct request *c)
1123 9ae1b92d 2022-08-13 op struct fcgi_response *resp;
1124 9ae1b92d 2022-08-13 op struct fcgi_stdin *stdin_node;
1125 9ae1b92d 2022-08-13 op struct env_val *env_entry;
1127 9ae1b92d 2022-08-13 op evtimer_del(&c->tmo);
1128 9ae1b92d 2022-08-13 op if (event_initialized(&c->ev))
1129 9ae1b92d 2022-08-13 op event_del(&c->ev);
1130 9ae1b92d 2022-08-13 op if (event_initialized(&c->resp_ev))
1131 9ae1b92d 2022-08-13 op event_del(&c->resp_ev);
1132 9ae1b92d 2022-08-13 op if (event_initialized(&c->script_ev)) {
1133 9ae1b92d 2022-08-13 op if (!c->stdout_fd_closed)
1134 9ae1b92d 2022-08-13 op close(EVENT_FD(&c->script_ev));
1135 9ae1b92d 2022-08-13 op event_del(&c->script_ev);
1137 9ae1b92d 2022-08-13 op if (event_initialized(&c->script_err_ev)) {
1138 9ae1b92d 2022-08-13 op if (!c->stderr_fd_closed)
1139 9ae1b92d 2022-08-13 op close(EVENT_FD(&c->script_err_ev));
1140 9ae1b92d 2022-08-13 op event_del(&c->script_err_ev);
1142 9ae1b92d 2022-08-13 op if (event_initialized(&c->script_stdin_ev)) {
1143 9ae1b92d 2022-08-13 op if (!c->stdin_fd_closed)
1144 9ae1b92d 2022-08-13 op close(EVENT_FD(&c->script_stdin_ev));
1145 9ae1b92d 2022-08-13 op event_del(&c->script_stdin_ev);
1147 9ae1b92d 2022-08-13 op close(c->fd);
1148 9ae1b92d 2022-08-13 op while (!SLIST_EMPTY(&c->env)) {
1149 9ae1b92d 2022-08-13 op env_entry = SLIST_FIRST(&c->env);
1150 9ae1b92d 2022-08-13 op SLIST_REMOVE_HEAD(&c->env, entry);
1151 9ae1b92d 2022-08-13 op free(env_entry->val);
1152 9ae1b92d 2022-08-13 op free(env_entry);
1155 9ae1b92d 2022-08-13 op while ((resp = TAILQ_FIRST(&c->response_head))) {
1156 9ae1b92d 2022-08-13 op TAILQ_REMOVE(&c->response_head, resp, entry);
1159 9ae1b92d 2022-08-13 op while ((stdin_node = TAILQ_FIRST(&c->stdin_head))) {
1160 9ae1b92d 2022-08-13 op TAILQ_REMOVE(&c->stdin_head, stdin_node, entry);
1161 9ae1b92d 2022-08-13 op free(stdin_node);
1163 9ae1b92d 2022-08-13 op LIST_REMOVE(c, entry);
1164 9ae1b92d 2022-08-13 op if (! c->inflight_fds_accounted)
1165 9ae1b92d 2022-08-13 op cgi_inflight--;
1170 9ae1b92d 2022-08-13 op dump_fcgi_record(const char *p, struct fcgi_record_header *h)
1172 9ae1b92d 2022-08-13 op dump_fcgi_record_header(p, h);
1174 9ae1b92d 2022-08-13 op if (h->type == FCGI_BEGIN_REQUEST)
1175 9ae1b92d 2022-08-13 op dump_fcgi_begin_request_body(p,
1176 9ae1b92d 2022-08-13 op (struct fcgi_begin_request_body *)(h + 1));
1177 9ae1b92d 2022-08-13 op else if (h->type == FCGI_END_REQUEST)
1178 9ae1b92d 2022-08-13 op dump_fcgi_end_request_body(p,
1179 9ae1b92d 2022-08-13 op (struct fcgi_end_request_body *)(h + 1));
1183 9ae1b92d 2022-08-13 op dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
1185 9ae1b92d 2022-08-13 op ldebug("%sversion: %d", p, h->version);
1186 9ae1b92d 2022-08-13 op ldebug("%stype: %d", p, h->type);
1187 9ae1b92d 2022-08-13 op ldebug("%srequestId: %d", p, ntohs(h->id));
1188 9ae1b92d 2022-08-13 op ldebug("%scontentLength: %d", p, ntohs(h->content_len));
1189 9ae1b92d 2022-08-13 op ldebug("%spaddingLength: %d", p, h->padding_len);
1190 9ae1b92d 2022-08-13 op ldebug("%sreserved: %d", p, h->reserved);
1194 9ae1b92d 2022-08-13 op dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
1196 9ae1b92d 2022-08-13 op ldebug("%srole %d", p, ntohs(b->role));
1197 9ae1b92d 2022-08-13 op ldebug("%sflags %d", p, b->flags);
1201 9ae1b92d 2022-08-13 op dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
1203 9ae1b92d 2022-08-13 op ldebug("%sappStatus: %d", p, ntohl(b->app_status));
1204 9ae1b92d 2022-08-13 op ldebug("%sprotocolStatus: %d", p, b->protocol_status);
1208 9ae1b92d 2022-08-13 op syslog_vstrerror(int e, int priority, const char *fmt, va_list ap)
1212 9ae1b92d 2022-08-13 op if (vasprintf(&s, fmt, ap) == -1) {
1213 9ae1b92d 2022-08-13 op syslog(LOG_EMERG, "unable to alloc in syslog_vstrerror");
1216 9ae1b92d 2022-08-13 op syslog(priority, "%s: %s", s, strerror(e));
1221 9ae1b92d 2022-08-13 op syslog_err(int ecode, const char *fmt, ...)
1225 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1226 9ae1b92d 2022-08-13 op syslog_vstrerror(errno, LOG_CRIT, fmt, ap);
1228 9ae1b92d 2022-08-13 op exit(ecode);
1232 9ae1b92d 2022-08-13 op syslog_errx(int ecode, const char *fmt, ...)
1236 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1237 9ae1b92d 2022-08-13 op vsyslog(LOG_CRIT, fmt, ap);
1239 9ae1b92d 2022-08-13 op exit(ecode);
1243 9ae1b92d 2022-08-13 op syslog_warn(const char *fmt, ...)
1247 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1248 9ae1b92d 2022-08-13 op syslog_vstrerror(errno, LOG_ERR, fmt, ap);
1253 9ae1b92d 2022-08-13 op syslog_warnx(const char *fmt, ...)
1257 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1258 9ae1b92d 2022-08-13 op vsyslog(LOG_ERR, fmt, ap);
1263 9ae1b92d 2022-08-13 op syslog_info(const char *fmt, ...)
1267 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1268 9ae1b92d 2022-08-13 op vsyslog(LOG_INFO, fmt, ap);
1273 9ae1b92d 2022-08-13 op syslog_debug(const char *fmt, ...)
1275 9ae1b92d 2022-08-13 op if (verbose > 0) {
1277 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1278 9ae1b92d 2022-08-13 op vsyslog(LOG_DEBUG, fmt, ap);