Blame


1 9ae1b92d 2022-08-13 op /* $OpenBSD: slowcgi.c,v 1.64 2022/08/07 07:43:53 op Exp $ */
2 9ae1b92d 2022-08-13 op /*
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>
5 9ae1b92d 2022-08-13 op *
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.
9 9ae1b92d 2022-08-13 op *
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.
17 9ae1b92d 2022-08-13 op */
18 9ae1b92d 2022-08-13 op
19 80189b3a 2022-08-13 op #include "config.h"
20 80189b3a 2022-08-13 op
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>
40 9ae1b92d 2022-08-13 op
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"
44 9ae1b92d 2022-08-13 op
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)
49 9ae1b92d 2022-08-13 op
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))
53 9ae1b92d 2022-08-13 op
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
57 9ae1b92d 2022-08-13 op
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)
70 9ae1b92d 2022-08-13 op
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
75 9ae1b92d 2022-08-13 op
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;
79 9ae1b92d 2022-08-13 op
80 9ae1b92d 2022-08-13 op struct listener {
81 9ae1b92d 2022-08-13 op struct event ev, pause;
82 9ae1b92d 2022-08-13 op };
83 9ae1b92d 2022-08-13 op
84 9ae1b92d 2022-08-13 op struct env_val {
85 9ae1b92d 2022-08-13 op SLIST_ENTRY(env_val) entry;
86 9ae1b92d 2022-08-13 op char *val;
87 9ae1b92d 2022-08-13 op };
88 9ae1b92d 2022-08-13 op SLIST_HEAD(env_head, env_val);
89 9ae1b92d 2022-08-13 op
90 9ae1b92d 2022-08-13 op struct fcgi_record_header {
91 9ae1b92d 2022-08-13 op uint8_t version;
92 9ae1b92d 2022-08-13 op uint8_t type;
93 9ae1b92d 2022-08-13 op uint16_t id;
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;
97 9ae1b92d 2022-08-13 op }__packed;
98 9ae1b92d 2022-08-13 op
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;
104 9ae1b92d 2022-08-13 op };
105 9ae1b92d 2022-08-13 op TAILQ_HEAD(fcgi_response_head, fcgi_response);
106 9ae1b92d 2022-08-13 op
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;
112 9ae1b92d 2022-08-13 op };
113 9ae1b92d 2022-08-13 op TAILQ_HEAD(fcgi_stdin_head, fcgi_stdin);
114 9ae1b92d 2022-08-13 op
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;
120 9ae1b92d 2022-08-13 op int fd;
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;
126 9ae1b92d 2022-08-13 op uint16_t id;
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;
141 9ae1b92d 2022-08-13 op };
142 9ae1b92d 2022-08-13 op
143 9ae1b92d 2022-08-13 op LIST_HEAD(requests_head, request);
144 9ae1b92d 2022-08-13 op
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;
148 9ae1b92d 2022-08-13 op };
149 9ae1b92d 2022-08-13 op
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];
154 9ae1b92d 2022-08-13 op }__packed;
155 9ae1b92d 2022-08-13 op
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];
160 9ae1b92d 2022-08-13 op }__packed;
161 9ae1b92d 2022-08-13 op
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 *,
174 9ae1b92d 2022-08-13 op uint16_t);
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 *);
192 9ae1b92d 2022-08-13 op
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)));
206 9ae1b92d 2022-08-13 op };
207 9ae1b92d 2022-08-13 op
208 9ae1b92d 2022-08-13 op const struct loggers conslogger = {
209 9ae1b92d 2022-08-13 op err,
210 9ae1b92d 2022-08-13 op errx,
211 9ae1b92d 2022-08-13 op warn,
212 9ae1b92d 2022-08-13 op warnx,
213 9ae1b92d 2022-08-13 op warnx, /* info */
214 9ae1b92d 2022-08-13 op warnx /* debug */
215 9ae1b92d 2022-08-13 op };
216 9ae1b92d 2022-08-13 op
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)));
231 9ae1b92d 2022-08-13 op
232 9ae1b92d 2022-08-13 op const struct loggers syslogger = {
233 9ae1b92d 2022-08-13 op syslog_err,
234 9ae1b92d 2022-08-13 op syslog_errx,
235 9ae1b92d 2022-08-13 op syslog_warn,
236 9ae1b92d 2022-08-13 op syslog_warnx,
237 9ae1b92d 2022-08-13 op syslog_info,
238 9ae1b92d 2022-08-13 op syslog_debug
239 9ae1b92d 2022-08-13 op };
240 9ae1b92d 2022-08-13 op
241 9ae1b92d 2022-08-13 op const struct loggers *logger = &conslogger;
242 9ae1b92d 2022-08-13 op
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)
249 9ae1b92d 2022-08-13 op
250 9ae1b92d 2022-08-13 op __dead void
251 9ae1b92d 2022-08-13 op usage(void)
252 9ae1b92d 2022-08-13 op {
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);
257 9ae1b92d 2022-08-13 op exit(1);
258 9ae1b92d 2022-08-13 op }
259 9ae1b92d 2022-08-13 op
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;
264 9ae1b92d 2022-08-13 op int on = 1;
265 9ae1b92d 2022-08-13 op char *fcgi_socket = "/var/www/run/slowcgi.sock";
266 9ae1b92d 2022-08-13 op
267 9ae1b92d 2022-08-13 op int
268 9ae1b92d 2022-08-13 op main(int argc, char *argv[])
269 9ae1b92d 2022-08-13 op {
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;
274 9ae1b92d 2022-08-13 op int c, fd;
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;
279 9ae1b92d 2022-08-13 op
280 9ae1b92d 2022-08-13 op /*
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.
284 9ae1b92d 2022-08-13 op */
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)
289 9ae1b92d 2022-08-13 op exit(1);
290 9ae1b92d 2022-08-13 op if (fd > c)
291 9ae1b92d 2022-08-13 op close(fd);
292 9ae1b92d 2022-08-13 op } else
293 9ae1b92d 2022-08-13 op exit(1);
294 9ae1b92d 2022-08-13 op }
295 9ae1b92d 2022-08-13 op }
296 9ae1b92d 2022-08-13 op
297 9ae1b92d 2022-08-13 op while ((c = getopt(argc, argv, "dp:s:t:U:u:v")) != -1) {
298 9ae1b92d 2022-08-13 op switch (c) {
299 9ae1b92d 2022-08-13 op case 'd':
300 9ae1b92d 2022-08-13 op debug++;
301 9ae1b92d 2022-08-13 op break;
302 9ae1b92d 2022-08-13 op case 'p':
303 9ae1b92d 2022-08-13 op chrootpath = optarg;
304 9ae1b92d 2022-08-13 op break;
305 9ae1b92d 2022-08-13 op case 's':
306 9ae1b92d 2022-08-13 op fcgi_socket = optarg;
307 9ae1b92d 2022-08-13 op break;
308 9ae1b92d 2022-08-13 op case 't':
309 9ae1b92d 2022-08-13 op timeout.tv_sec = strtonum(optarg, 1, TIMEOUT_MAX,
310 9ae1b92d 2022-08-13 op &errstr);
311 9ae1b92d 2022-08-13 op if (errstr != NULL)
312 9ae1b92d 2022-08-13 op errx(1, "timeout is %s: %s", errstr, optarg);
313 9ae1b92d 2022-08-13 op break;
314 9ae1b92d 2022-08-13 op case 'U':
315 9ae1b92d 2022-08-13 op sock_user = optarg;
316 9ae1b92d 2022-08-13 op break;
317 9ae1b92d 2022-08-13 op case 'u':
318 9ae1b92d 2022-08-13 op slowcgi_user = optarg;
319 9ae1b92d 2022-08-13 op break;
320 9ae1b92d 2022-08-13 op case 'v':
321 9ae1b92d 2022-08-13 op verbose++;
322 9ae1b92d 2022-08-13 op break;
323 9ae1b92d 2022-08-13 op default:
324 9ae1b92d 2022-08-13 op usage();
325 9ae1b92d 2022-08-13 op /* NOTREACHED */
326 9ae1b92d 2022-08-13 op }
327 9ae1b92d 2022-08-13 op }
328 9ae1b92d 2022-08-13 op
329 9ae1b92d 2022-08-13 op if (geteuid() != 0)
330 9ae1b92d 2022-08-13 op errx(1, "need root privileges");
331 9ae1b92d 2022-08-13 op
332 9ae1b92d 2022-08-13 op if (!debug && daemon(0, 0) == -1)
333 9ae1b92d 2022-08-13 op err(1, "daemon");
334 9ae1b92d 2022-08-13 op
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;
338 9ae1b92d 2022-08-13 op }
339 9ae1b92d 2022-08-13 op
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);
344 9ae1b92d 2022-08-13 op
345 9ae1b92d 2022-08-13 op fd = slowcgi_listen(fcgi_socket, pw);
346 9ae1b92d 2022-08-13 op
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);
351 9ae1b92d 2022-08-13 op
352 9ae1b92d 2022-08-13 op if (chrootpath == NULL)
353 9ae1b92d 2022-08-13 op chrootpath = pw->pw_dir;
354 9ae1b92d 2022-08-13 op
355 9ae1b92d 2022-08-13 op if (chroot(chrootpath) == -1)
356 9ae1b92d 2022-08-13 op lerr(1, "chroot(%s)", chrootpath);
357 9ae1b92d 2022-08-13 op
358 9ae1b92d 2022-08-13 op ldebug("chroot: %s", chrootpath);
359 9ae1b92d 2022-08-13 op
360 9ae1b92d 2022-08-13 op if (chdir("/") == -1)
361 9ae1b92d 2022-08-13 op lerr(1, "chdir(/)");
362 9ae1b92d 2022-08-13 op
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");
367 9ae1b92d 2022-08-13 op
368 9ae1b92d 2022-08-13 op if (pledge("stdio rpath unix proc exec", NULL) == -1)
369 9ae1b92d 2022-08-13 op lerr(1, "pledge");
370 9ae1b92d 2022-08-13 op
371 9ae1b92d 2022-08-13 op LIST_INIT(&slowcgi_proc.requests);
372 9ae1b92d 2022-08-13 op event_init();
373 9ae1b92d 2022-08-13 op
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");
377 9ae1b92d 2022-08-13 op
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);
381 9ae1b92d 2022-08-13 op
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);
385 9ae1b92d 2022-08-13 op
386 9ae1b92d 2022-08-13 op signal_add(&slowcgi_proc.ev_sigchld, NULL);
387 9ae1b92d 2022-08-13 op
388 9ae1b92d 2022-08-13 op event_dispatch();
389 9ae1b92d 2022-08-13 op return (0);
390 9ae1b92d 2022-08-13 op }
391 9ae1b92d 2022-08-13 op
392 9ae1b92d 2022-08-13 op int
393 9ae1b92d 2022-08-13 op slowcgi_listen(char *path, struct passwd *pw)
394 9ae1b92d 2022-08-13 op {
395 9ae1b92d 2022-08-13 op struct sockaddr_un sun;
396 9ae1b92d 2022-08-13 op mode_t old_umask;
397 9ae1b92d 2022-08-13 op int fd;
398 9ae1b92d 2022-08-13 op
399 9ae1b92d 2022-08-13 op if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
400 9ae1b92d 2022-08-13 op 0)) == -1)
401 9ae1b92d 2022-08-13 op lerr(1, "slowcgi_listen: socket");
402 9ae1b92d 2022-08-13 op
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");
408 9ae1b92d 2022-08-13 op
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);
412 9ae1b92d 2022-08-13 op
413 9ae1b92d 2022-08-13 op old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
414 9ae1b92d 2022-08-13 op
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);
417 9ae1b92d 2022-08-13 op
418 9ae1b92d 2022-08-13 op umask(old_umask);
419 9ae1b92d 2022-08-13 op
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);
422 9ae1b92d 2022-08-13 op
423 9ae1b92d 2022-08-13 op if (listen(fd, 5) == -1)
424 9ae1b92d 2022-08-13 op lerr(1, "listen");
425 9ae1b92d 2022-08-13 op
426 9ae1b92d 2022-08-13 op ldebug("socket: %s", path);
427 9ae1b92d 2022-08-13 op return fd;
428 9ae1b92d 2022-08-13 op }
429 9ae1b92d 2022-08-13 op
430 9ae1b92d 2022-08-13 op void
431 9ae1b92d 2022-08-13 op slowcgi_paused(int fd, short events, void *arg)
432 9ae1b92d 2022-08-13 op {
433 9ae1b92d 2022-08-13 op struct listener *l = arg;
434 9ae1b92d 2022-08-13 op event_add(&l->ev, NULL);
435 9ae1b92d 2022-08-13 op }
436 9ae1b92d 2022-08-13 op
437 9ae1b92d 2022-08-13 op int
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)
440 9ae1b92d 2022-08-13 op {
441 9ae1b92d 2022-08-13 op int ret;
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;
446 9ae1b92d 2022-08-13 op return -1;
447 9ae1b92d 2022-08-13 op }
448 9ae1b92d 2022-08-13 op
449 9ae1b92d 2022-08-13 op if ((ret = accept4(sockfd, addr, addrlen, SOCK_NONBLOCK | SOCK_CLOEXEC))
450 9ae1b92d 2022-08-13 op > -1) {
451 9ae1b92d 2022-08-13 op (*counter)++;
452 9ae1b92d 2022-08-13 op ldebug("inflight incremented, now %d", *counter);
453 9ae1b92d 2022-08-13 op }
454 9ae1b92d 2022-08-13 op return ret;
455 9ae1b92d 2022-08-13 op }
456 9ae1b92d 2022-08-13 op
457 9ae1b92d 2022-08-13 op void
458 9ae1b92d 2022-08-13 op slowcgi_accept(int fd, short events, void *arg)
459 9ae1b92d 2022-08-13 op {
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;
465 9ae1b92d 2022-08-13 op int s;
466 9ae1b92d 2022-08-13 op
467 9ae1b92d 2022-08-13 op l = arg;
468 9ae1b92d 2022-08-13 op backoff.tv_sec = 1;
469 9ae1b92d 2022-08-13 op backoff.tv_usec = 0;
470 9ae1b92d 2022-08-13 op c = NULL;
471 9ae1b92d 2022-08-13 op
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) {
476 9ae1b92d 2022-08-13 op case EINTR:
477 9ae1b92d 2022-08-13 op case EWOULDBLOCK:
478 9ae1b92d 2022-08-13 op case ECONNABORTED:
479 9ae1b92d 2022-08-13 op return;
480 9ae1b92d 2022-08-13 op case EMFILE:
481 9ae1b92d 2022-08-13 op case ENFILE:
482 9ae1b92d 2022-08-13 op event_del(&l->ev);
483 9ae1b92d 2022-08-13 op evtimer_add(&l->pause, &backoff);
484 9ae1b92d 2022-08-13 op return;
485 9ae1b92d 2022-08-13 op default:
486 9ae1b92d 2022-08-13 op lerr(1, "accept");
487 9ae1b92d 2022-08-13 op }
488 9ae1b92d 2022-08-13 op }
489 9ae1b92d 2022-08-13 op
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");
493 9ae1b92d 2022-08-13 op close(s);
494 9ae1b92d 2022-08-13 op cgi_inflight--;
495 9ae1b92d 2022-08-13 op return;
496 9ae1b92d 2022-08-13 op }
497 9ae1b92d 2022-08-13 op c->fd = s;
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);
505 9ae1b92d 2022-08-13 op
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);
512 9ae1b92d 2022-08-13 op }
513 9ae1b92d 2022-08-13 op
514 9ae1b92d 2022-08-13 op void
515 9ae1b92d 2022-08-13 op slowcgi_timeout(int fd, short events, void *arg)
516 9ae1b92d 2022-08-13 op {
517 9ae1b92d 2022-08-13 op cleanup_request((struct request*) arg);
518 9ae1b92d 2022-08-13 op }
519 9ae1b92d 2022-08-13 op
520 9ae1b92d 2022-08-13 op void
521 9ae1b92d 2022-08-13 op slowcgi_sig_handler(int sig, short event, void *arg)
522 9ae1b92d 2022-08-13 op {
523 9ae1b92d 2022-08-13 op struct request *c;
524 9ae1b92d 2022-08-13 op struct slowcgi_proc *p;
525 9ae1b92d 2022-08-13 op pid_t pid;
526 9ae1b92d 2022-08-13 op int status;
527 9ae1b92d 2022-08-13 op
528 9ae1b92d 2022-08-13 op p = arg;
529 9ae1b92d 2022-08-13 op
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)
535 9ae1b92d 2022-08-13 op break;
536 9ae1b92d 2022-08-13 op if (c == NULL) {
537 9ae1b92d 2022-08-13 op lwarnx("caught exit of unknown child %i", pid);
538 9ae1b92d 2022-08-13 op continue;
539 9ae1b92d 2022-08-13 op }
540 9ae1b92d 2022-08-13 op
541 9ae1b92d 2022-08-13 op if (WIFSIGNALED(status))
542 9ae1b92d 2022-08-13 op c->script_status = WTERMSIG(status);
543 9ae1b92d 2022-08-13 op else
544 9ae1b92d 2022-08-13 op c->script_status = WEXITSTATUS(status);
545 9ae1b92d 2022-08-13 op
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;
549 9ae1b92d 2022-08-13 op
550 9ae1b92d 2022-08-13 op ldebug("wait: %s", c->script_name);
551 9ae1b92d 2022-08-13 op }
552 9ae1b92d 2022-08-13 op if (pid == -1 && errno != ECHILD)
553 9ae1b92d 2022-08-13 op lwarn("waitpid");
554 9ae1b92d 2022-08-13 op break;
555 9ae1b92d 2022-08-13 op default:
556 9ae1b92d 2022-08-13 op lerr(1, "unexpected signal: %d", sig);
557 9ae1b92d 2022-08-13 op break;
558 9ae1b92d 2022-08-13 op }
559 9ae1b92d 2022-08-13 op }
560 9ae1b92d 2022-08-13 op
561 9ae1b92d 2022-08-13 op void
562 9ae1b92d 2022-08-13 op slowcgi_add_response(struct request *c, struct fcgi_response *resp)
563 9ae1b92d 2022-08-13 op {
564 9ae1b92d 2022-08-13 op struct fcgi_record_header *header;
565 9ae1b92d 2022-08-13 op size_t padded_len;
566 9ae1b92d 2022-08-13 op
567 9ae1b92d 2022-08-13 op header = (struct fcgi_record_header*)resp->data;
568 9ae1b92d 2022-08-13 op
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;
577 9ae1b92d 2022-08-13 op }
578 9ae1b92d 2022-08-13 op
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);
581 9ae1b92d 2022-08-13 op }
582 9ae1b92d 2022-08-13 op
583 9ae1b92d 2022-08-13 op void
584 9ae1b92d 2022-08-13 op slowcgi_response(int fd, short events, void *arg)
585 9ae1b92d 2022-08-13 op {
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;
589 9ae1b92d 2022-08-13 op ssize_t n;
590 9ae1b92d 2022-08-13 op
591 9ae1b92d 2022-08-13 op c = arg;
592 9ae1b92d 2022-08-13 op
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);
597 9ae1b92d 2022-08-13 op
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)
601 9ae1b92d 2022-08-13 op return;
602 9ae1b92d 2022-08-13 op cleanup_request(c);
603 9ae1b92d 2022-08-13 op return;
604 9ae1b92d 2022-08-13 op }
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);
609 9ae1b92d 2022-08-13 op free(resp);
610 9ae1b92d 2022-08-13 op }
611 9ae1b92d 2022-08-13 op }
612 9ae1b92d 2022-08-13 op
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);
617 9ae1b92d 2022-08-13 op else
618 9ae1b92d 2022-08-13 op event_del(&c->resp_ev);
619 9ae1b92d 2022-08-13 op }
620 9ae1b92d 2022-08-13 op }
621 9ae1b92d 2022-08-13 op
622 9ae1b92d 2022-08-13 op void
623 9ae1b92d 2022-08-13 op slowcgi_request(int fd, short events, void *arg)
624 9ae1b92d 2022-08-13 op {
625 9ae1b92d 2022-08-13 op struct request *c;
626 9ae1b92d 2022-08-13 op ssize_t n;
627 9ae1b92d 2022-08-13 op size_t parsed;
628 9ae1b92d 2022-08-13 op
629 9ae1b92d 2022-08-13 op c = arg;
630 9ae1b92d 2022-08-13 op
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);
633 9ae1b92d 2022-08-13 op
634 9ae1b92d 2022-08-13 op switch (n) {
635 9ae1b92d 2022-08-13 op case -1:
636 9ae1b92d 2022-08-13 op switch (errno) {
637 9ae1b92d 2022-08-13 op case EINTR:
638 9ae1b92d 2022-08-13 op case EAGAIN:
639 9ae1b92d 2022-08-13 op return;
640 9ae1b92d 2022-08-13 op default:
641 9ae1b92d 2022-08-13 op goto fail;
642 9ae1b92d 2022-08-13 op }
643 9ae1b92d 2022-08-13 op break;
644 9ae1b92d 2022-08-13 op
645 9ae1b92d 2022-08-13 op case 0:
646 9ae1b92d 2022-08-13 op ldebug("closed connection");
647 9ae1b92d 2022-08-13 op goto fail;
648 9ae1b92d 2022-08-13 op default:
649 9ae1b92d 2022-08-13 op break;
650 9ae1b92d 2022-08-13 op }
651 9ae1b92d 2022-08-13 op
652 9ae1b92d 2022-08-13 op c->buf_len += n;
653 9ae1b92d 2022-08-13 op
654 9ae1b92d 2022-08-13 op /*
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.
659 9ae1b92d 2022-08-13 op */
660 9ae1b92d 2022-08-13 op do {
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);
665 9ae1b92d 2022-08-13 op
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;
670 9ae1b92d 2022-08-13 op }
671 9ae1b92d 2022-08-13 op return;
672 9ae1b92d 2022-08-13 op fail:
673 9ae1b92d 2022-08-13 op cleanup_request(c);
674 9ae1b92d 2022-08-13 op }
675 9ae1b92d 2022-08-13 op
676 9ae1b92d 2022-08-13 op void
677 9ae1b92d 2022-08-13 op parse_begin_request(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
678 9ae1b92d 2022-08-13 op {
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");
682 9ae1b92d 2022-08-13 op return;
683 9ae1b92d 2022-08-13 op }
684 9ae1b92d 2022-08-13 op
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));
688 9ae1b92d 2022-08-13 op return;
689 9ae1b92d 2022-08-13 op }
690 9ae1b92d 2022-08-13 op
691 9ae1b92d 2022-08-13 op c->request_started = 1;
692 9ae1b92d 2022-08-13 op
693 9ae1b92d 2022-08-13 op c->id = id;
694 9ae1b92d 2022-08-13 op SLIST_INIT(&c->env);
695 9ae1b92d 2022-08-13 op c->env_count = 0;
696 9ae1b92d 2022-08-13 op }
697 9ae1b92d 2022-08-13 op
698 9ae1b92d 2022-08-13 op void
699 9ae1b92d 2022-08-13 op parse_params(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
700 9ae1b92d 2022-08-13 op {
701 9ae1b92d 2022-08-13 op struct env_val *env_entry;
702 9ae1b92d 2022-08-13 op uint32_t name_len, val_len;
703 9ae1b92d 2022-08-13 op
704 9ae1b92d 2022-08-13 op if (!c->request_started) {
705 9ae1b92d 2022-08-13 op lwarnx("FCGI_PARAMS without FCGI_BEGIN_REQUEST, ignoring");
706 9ae1b92d 2022-08-13 op return;
707 9ae1b92d 2022-08-13 op }
708 9ae1b92d 2022-08-13 op
709 9ae1b92d 2022-08-13 op if (c->id != id) {
710 9ae1b92d 2022-08-13 op lwarnx("unexpected id, ignoring");
711 9ae1b92d 2022-08-13 op return;
712 9ae1b92d 2022-08-13 op }
713 9ae1b92d 2022-08-13 op
714 9ae1b92d 2022-08-13 op /*
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.
717 9ae1b92d 2022-08-13 op */
718 9ae1b92d 2022-08-13 op if (n == 0) {
719 9ae1b92d 2022-08-13 op exec_cgi(c);
720 9ae1b92d 2022-08-13 op return;
721 9ae1b92d 2022-08-13 op }
722 9ae1b92d 2022-08-13 op
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];
726 9ae1b92d 2022-08-13 op n--;
727 9ae1b92d 2022-08-13 op buf++;
728 9ae1b92d 2022-08-13 op } else {
729 9ae1b92d 2022-08-13 op if (n > 3) {
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];
732 9ae1b92d 2022-08-13 op n -= 4;
733 9ae1b92d 2022-08-13 op buf += 4;
734 9ae1b92d 2022-08-13 op } else
735 9ae1b92d 2022-08-13 op return;
736 9ae1b92d 2022-08-13 op }
737 9ae1b92d 2022-08-13 op
738 9ae1b92d 2022-08-13 op if (n > 0) {
739 9ae1b92d 2022-08-13 op if (buf[0] >> 7 == 0) {
740 9ae1b92d 2022-08-13 op val_len = buf[0];
741 9ae1b92d 2022-08-13 op n--;
742 9ae1b92d 2022-08-13 op buf++;
743 9ae1b92d 2022-08-13 op } else {
744 9ae1b92d 2022-08-13 op if (n > 3) {
745 9ae1b92d 2022-08-13 op val_len = ((buf[0] & 0x7f) << 24) +
746 9ae1b92d 2022-08-13 op (buf[1] << 16) + (buf[2] << 8) +
747 9ae1b92d 2022-08-13 op buf[3];
748 9ae1b92d 2022-08-13 op n -= 4;
749 9ae1b92d 2022-08-13 op buf += 4;
750 9ae1b92d 2022-08-13 op } else
751 9ae1b92d 2022-08-13 op return;
752 9ae1b92d 2022-08-13 op }
753 9ae1b92d 2022-08-13 op } else
754 9ae1b92d 2022-08-13 op return;
755 9ae1b92d 2022-08-13 op
756 9ae1b92d 2022-08-13 op if (n < name_len + val_len)
757 9ae1b92d 2022-08-13 op return;
758 9ae1b92d 2022-08-13 op
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");
761 9ae1b92d 2022-08-13 op return;
762 9ae1b92d 2022-08-13 op }
763 9ae1b92d 2022-08-13 op
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);
768 9ae1b92d 2022-08-13 op return;
769 9ae1b92d 2022-08-13 op }
770 9ae1b92d 2022-08-13 op
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;
774 9ae1b92d 2022-08-13 op
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';
784 9ae1b92d 2022-08-13 op }
785 9ae1b92d 2022-08-13 op env_entry->val[name_len] = '=';
786 9ae1b92d 2022-08-13 op
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;
790 9ae1b92d 2022-08-13 op
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++;
794 9ae1b92d 2022-08-13 op }
795 9ae1b92d 2022-08-13 op }
796 9ae1b92d 2022-08-13 op
797 9ae1b92d 2022-08-13 op void
798 9ae1b92d 2022-08-13 op parse_stdin(uint8_t *buf, uint16_t n, struct request *c, uint16_t id)
799 9ae1b92d 2022-08-13 op {
800 9ae1b92d 2022-08-13 op struct fcgi_stdin *node;
801 9ae1b92d 2022-08-13 op
802 9ae1b92d 2022-08-13 op if (c->id != id) {
803 9ae1b92d 2022-08-13 op lwarnx("unexpected id, ignoring");
804 9ae1b92d 2022-08-13 op return;
805 9ae1b92d 2022-08-13 op }
806 9ae1b92d 2022-08-13 op
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");
809 9ae1b92d 2022-08-13 op return;
810 9ae1b92d 2022-08-13 op }
811 9ae1b92d 2022-08-13 op
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;
815 9ae1b92d 2022-08-13 op
816 9ae1b92d 2022-08-13 op TAILQ_INSERT_TAIL(&c->stdin_head, node, entry);
817 9ae1b92d 2022-08-13 op
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);
820 9ae1b92d 2022-08-13 op }
821 9ae1b92d 2022-08-13 op
822 9ae1b92d 2022-08-13 op size_t
823 9ae1b92d 2022-08-13 op parse_record(uint8_t *buf, size_t n, struct request *c)
824 9ae1b92d 2022-08-13 op {
825 9ae1b92d 2022-08-13 op struct fcgi_record_header *h;
826 9ae1b92d 2022-08-13 op
827 9ae1b92d 2022-08-13 op if (n < sizeof(struct fcgi_record_header))
828 9ae1b92d 2022-08-13 op return (0);
829 9ae1b92d 2022-08-13 op
830 9ae1b92d 2022-08-13 op h = (struct fcgi_record_header*) buf;
831 9ae1b92d 2022-08-13 op
832 9ae1b92d 2022-08-13 op if (debug > 1)
833 9ae1b92d 2022-08-13 op dump_fcgi_record("", h);
834 9ae1b92d 2022-08-13 op
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)
837 9ae1b92d 2022-08-13 op return (0);
838 9ae1b92d 2022-08-13 op
839 9ae1b92d 2022-08-13 op if (h->version != 1)
840 9ae1b92d 2022-08-13 op lerrx(1, "wrong version");
841 9ae1b92d 2022-08-13 op
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));
846 9ae1b92d 2022-08-13 op break;
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));
850 9ae1b92d 2022-08-13 op break;
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));
854 9ae1b92d 2022-08-13 op break;
855 9ae1b92d 2022-08-13 op default:
856 9ae1b92d 2022-08-13 op lwarnx("unimplemented type %d", h->type);
857 9ae1b92d 2022-08-13 op break;
858 9ae1b92d 2022-08-13 op }
859 9ae1b92d 2022-08-13 op
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);
862 9ae1b92d 2022-08-13 op }
863 9ae1b92d 2022-08-13 op
864 9ae1b92d 2022-08-13 op /*
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.
869 9ae1b92d 2022-08-13 op */
870 9ae1b92d 2022-08-13 op void
871 9ae1b92d 2022-08-13 op exec_cgi(struct request *c)
872 9ae1b92d 2022-08-13 op {
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;
875 9ae1b92d 2022-08-13 op pid_t pid;
876 9ae1b92d 2022-08-13 op char *argv[2];
877 9ae1b92d 2022-08-13 op char **env;
878 9ae1b92d 2022-08-13 op char *path;
879 9ae1b92d 2022-08-13 op
880 9ae1b92d 2022-08-13 op i = 0;
881 9ae1b92d 2022-08-13 op
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);
891 9ae1b92d 2022-08-13 op
892 9ae1b92d 2022-08-13 op switch (pid = fork()) {
893 9ae1b92d 2022-08-13 op case -1:
894 9ae1b92d 2022-08-13 op c->script_status = errno;
895 9ae1b92d 2022-08-13 op
896 9ae1b92d 2022-08-13 op lwarn("fork");
897 9ae1b92d 2022-08-13 op
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]);
901 9ae1b92d 2022-08-13 op
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]);
905 9ae1b92d 2022-08-13 op
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);
909 9ae1b92d 2022-08-13 op
910 9ae1b92d 2022-08-13 op create_end_record(c);
911 9ae1b92d 2022-08-13 op return;
912 9ae1b92d 2022-08-13 op case 0:
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]);
919 9ae1b92d 2022-08-13 op
920 9ae1b92d 2022-08-13 op if (dup2(s_in[1], STDIN_FILENO) == -1)
921 9ae1b92d 2022-08-13 op _exit(1);
922 9ae1b92d 2022-08-13 op if (dup2(s_out[1], STDOUT_FILENO) == -1)
923 9ae1b92d 2022-08-13 op _exit(1);
924 9ae1b92d 2022-08-13 op if (dup2(s_err[1], STDERR_FILENO) == -1)
925 9ae1b92d 2022-08-13 op _exit(1);
926 9ae1b92d 2022-08-13 op
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]);
930 9ae1b92d 2022-08-13 op
931 9ae1b92d 2022-08-13 op signal(SIGPIPE, SIG_DFL);
932 9ae1b92d 2022-08-13 op
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);
940 9ae1b92d 2022-08-13 op *path = '/';
941 9ae1b92d 2022-08-13 op } else
942 9ae1b92d 2022-08-13 op if (chdir("/") == -1)
943 9ae1b92d 2022-08-13 op lwarn("cannot chdir to /");
944 9ae1b92d 2022-08-13 op }
945 9ae1b92d 2022-08-13 op
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)
949 9ae1b92d 2022-08-13 op _exit(1);
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);
955 9ae1b92d 2022-08-13 op _exit(1);
956 9ae1b92d 2022-08-13 op
957 9ae1b92d 2022-08-13 op }
958 9ae1b92d 2022-08-13 op
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]);
963 9ae1b92d 2022-08-13 op
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);
967 9ae1b92d 2022-08-13 op
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)");
974 9ae1b92d 2022-08-13 op
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);
985 9ae1b92d 2022-08-13 op }
986 9ae1b92d 2022-08-13 op
987 9ae1b92d 2022-08-13 op void
988 9ae1b92d 2022-08-13 op create_end_record(struct request *c)
989 9ae1b92d 2022-08-13 op {
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;
993 9ae1b92d 2022-08-13 op
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");
996 9ae1b92d 2022-08-13 op return;
997 9ae1b92d 2022-08-13 op }
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);
1017 9ae1b92d 2022-08-13 op }
1018 9ae1b92d 2022-08-13 op
1019 9ae1b92d 2022-08-13 op void
1020 9ae1b92d 2022-08-13 op script_in(int fd, struct event *ev, struct request *c, uint8_t type)
1021 9ae1b92d 2022-08-13 op {
1022 9ae1b92d 2022-08-13 op struct fcgi_response *resp;
1023 9ae1b92d 2022-08-13 op struct fcgi_record_header *header;
1024 9ae1b92d 2022-08-13 op ssize_t n;
1025 9ae1b92d 2022-08-13 op
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");
1028 9ae1b92d 2022-08-13 op return;
1029 9ae1b92d 2022-08-13 op }
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;
1036 9ae1b92d 2022-08-13 op
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);
1039 9ae1b92d 2022-08-13 op
1040 9ae1b92d 2022-08-13 op if (n == -1) {
1041 9ae1b92d 2022-08-13 op switch (errno) {
1042 9ae1b92d 2022-08-13 op case EINTR:
1043 9ae1b92d 2022-08-13 op case EAGAIN:
1044 9ae1b92d 2022-08-13 op free(resp);
1045 9ae1b92d 2022-08-13 op return;
1046 9ae1b92d 2022-08-13 op default:
1047 9ae1b92d 2022-08-13 op n = 0; /* fake empty FCGI_STD{OUT,ERR} response */
1048 9ae1b92d 2022-08-13 op }
1049 9ae1b92d 2022-08-13 op }
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);
1054 9ae1b92d 2022-08-13 op
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;
1058 9ae1b92d 2022-08-13 op else
1059 9ae1b92d 2022-08-13 op c->script_flags |= STDERR_DONE;
1060 9ae1b92d 2022-08-13 op
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);
1064 9ae1b92d 2022-08-13 op }
1065 9ae1b92d 2022-08-13 op event_del(ev);
1066 9ae1b92d 2022-08-13 op close(fd);
1067 9ae1b92d 2022-08-13 op if (type == FCGI_STDOUT)
1068 9ae1b92d 2022-08-13 op c->stdout_fd_closed = 1;
1069 9ae1b92d 2022-08-13 op else
1070 9ae1b92d 2022-08-13 op c->stderr_fd_closed = 1;
1071 9ae1b92d 2022-08-13 op }
1072 9ae1b92d 2022-08-13 op }
1073 9ae1b92d 2022-08-13 op
1074 9ae1b92d 2022-08-13 op void
1075 9ae1b92d 2022-08-13 op script_std_in(int fd, short events, void *arg)
1076 9ae1b92d 2022-08-13 op {
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);
1079 9ae1b92d 2022-08-13 op }
1080 9ae1b92d 2022-08-13 op
1081 9ae1b92d 2022-08-13 op void
1082 9ae1b92d 2022-08-13 op script_err_in(int fd, short events, void *arg)
1083 9ae1b92d 2022-08-13 op {
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);
1086 9ae1b92d 2022-08-13 op }
1087 9ae1b92d 2022-08-13 op
1088 9ae1b92d 2022-08-13 op void
1089 9ae1b92d 2022-08-13 op script_out(int fd, short events, void *arg)
1090 9ae1b92d 2022-08-13 op {
1091 9ae1b92d 2022-08-13 op struct request *c;
1092 9ae1b92d 2022-08-13 op struct fcgi_stdin *node;
1093 9ae1b92d 2022-08-13 op ssize_t n;
1094 9ae1b92d 2022-08-13 op
1095 9ae1b92d 2022-08-13 op c = arg;
1096 9ae1b92d 2022-08-13 op
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 */
1099 9ae1b92d 2022-08-13 op close(fd);
1100 9ae1b92d 2022-08-13 op c->stdin_fd_closed = 1;
1101 9ae1b92d 2022-08-13 op break;
1102 9ae1b92d 2022-08-13 op }
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)
1106 9ae1b92d 2022-08-13 op return;
1107 9ae1b92d 2022-08-13 op event_del(&c->script_stdin_ev);
1108 9ae1b92d 2022-08-13 op return;
1109 9ae1b92d 2022-08-13 op }
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);
1114 9ae1b92d 2022-08-13 op free(node);
1115 9ae1b92d 2022-08-13 op }
1116 9ae1b92d 2022-08-13 op }
1117 9ae1b92d 2022-08-13 op event_del(&c->script_stdin_ev);
1118 9ae1b92d 2022-08-13 op }
1119 9ae1b92d 2022-08-13 op
1120 9ae1b92d 2022-08-13 op void
1121 9ae1b92d 2022-08-13 op cleanup_request(struct request *c)
1122 9ae1b92d 2022-08-13 op {
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;
1126 9ae1b92d 2022-08-13 op
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);
1136 9ae1b92d 2022-08-13 op }
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);
1141 9ae1b92d 2022-08-13 op }
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);
1146 9ae1b92d 2022-08-13 op }
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);
1153 9ae1b92d 2022-08-13 op }
1154 9ae1b92d 2022-08-13 op
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);
1157 9ae1b92d 2022-08-13 op free(resp);
1158 9ae1b92d 2022-08-13 op }
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);
1162 9ae1b92d 2022-08-13 op }
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--;
1166 9ae1b92d 2022-08-13 op free(c);
1167 9ae1b92d 2022-08-13 op }
1168 9ae1b92d 2022-08-13 op
1169 9ae1b92d 2022-08-13 op void
1170 9ae1b92d 2022-08-13 op dump_fcgi_record(const char *p, struct fcgi_record_header *h)
1171 9ae1b92d 2022-08-13 op {
1172 9ae1b92d 2022-08-13 op dump_fcgi_record_header(p, h);
1173 9ae1b92d 2022-08-13 op
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));
1180 9ae1b92d 2022-08-13 op }
1181 9ae1b92d 2022-08-13 op
1182 9ae1b92d 2022-08-13 op void
1183 9ae1b92d 2022-08-13 op dump_fcgi_record_header(const char* p, struct fcgi_record_header *h)
1184 9ae1b92d 2022-08-13 op {
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);
1191 9ae1b92d 2022-08-13 op }
1192 9ae1b92d 2022-08-13 op
1193 9ae1b92d 2022-08-13 op void
1194 9ae1b92d 2022-08-13 op dump_fcgi_begin_request_body(const char *p, struct fcgi_begin_request_body *b)
1195 9ae1b92d 2022-08-13 op {
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);
1198 9ae1b92d 2022-08-13 op }
1199 9ae1b92d 2022-08-13 op
1200 9ae1b92d 2022-08-13 op void
1201 9ae1b92d 2022-08-13 op dump_fcgi_end_request_body(const char *p, struct fcgi_end_request_body *b)
1202 9ae1b92d 2022-08-13 op {
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);
1205 9ae1b92d 2022-08-13 op }
1206 9ae1b92d 2022-08-13 op
1207 9ae1b92d 2022-08-13 op void
1208 9ae1b92d 2022-08-13 op syslog_vstrerror(int e, int priority, const char *fmt, va_list ap)
1209 9ae1b92d 2022-08-13 op {
1210 9ae1b92d 2022-08-13 op char *s;
1211 9ae1b92d 2022-08-13 op
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");
1214 9ae1b92d 2022-08-13 op exit(1);
1215 9ae1b92d 2022-08-13 op }
1216 9ae1b92d 2022-08-13 op syslog(priority, "%s: %s", s, strerror(e));
1217 9ae1b92d 2022-08-13 op free(s);
1218 9ae1b92d 2022-08-13 op }
1219 9ae1b92d 2022-08-13 op
1220 9ae1b92d 2022-08-13 op __dead void
1221 9ae1b92d 2022-08-13 op syslog_err(int ecode, const char *fmt, ...)
1222 9ae1b92d 2022-08-13 op {
1223 9ae1b92d 2022-08-13 op va_list ap;
1224 9ae1b92d 2022-08-13 op
1225 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1226 9ae1b92d 2022-08-13 op syslog_vstrerror(errno, LOG_CRIT, fmt, ap);
1227 9ae1b92d 2022-08-13 op va_end(ap);
1228 9ae1b92d 2022-08-13 op exit(ecode);
1229 9ae1b92d 2022-08-13 op }
1230 9ae1b92d 2022-08-13 op
1231 9ae1b92d 2022-08-13 op __dead void
1232 9ae1b92d 2022-08-13 op syslog_errx(int ecode, const char *fmt, ...)
1233 9ae1b92d 2022-08-13 op {
1234 9ae1b92d 2022-08-13 op va_list ap;
1235 9ae1b92d 2022-08-13 op
1236 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1237 9ae1b92d 2022-08-13 op vsyslog(LOG_CRIT, fmt, ap);
1238 9ae1b92d 2022-08-13 op va_end(ap);
1239 9ae1b92d 2022-08-13 op exit(ecode);
1240 9ae1b92d 2022-08-13 op }
1241 9ae1b92d 2022-08-13 op
1242 9ae1b92d 2022-08-13 op void
1243 9ae1b92d 2022-08-13 op syslog_warn(const char *fmt, ...)
1244 9ae1b92d 2022-08-13 op {
1245 9ae1b92d 2022-08-13 op va_list ap;
1246 9ae1b92d 2022-08-13 op
1247 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1248 9ae1b92d 2022-08-13 op syslog_vstrerror(errno, LOG_ERR, fmt, ap);
1249 9ae1b92d 2022-08-13 op va_end(ap);
1250 9ae1b92d 2022-08-13 op }
1251 9ae1b92d 2022-08-13 op
1252 9ae1b92d 2022-08-13 op void
1253 9ae1b92d 2022-08-13 op syslog_warnx(const char *fmt, ...)
1254 9ae1b92d 2022-08-13 op {
1255 9ae1b92d 2022-08-13 op va_list ap;
1256 9ae1b92d 2022-08-13 op
1257 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1258 9ae1b92d 2022-08-13 op vsyslog(LOG_ERR, fmt, ap);
1259 9ae1b92d 2022-08-13 op va_end(ap);
1260 9ae1b92d 2022-08-13 op }
1261 9ae1b92d 2022-08-13 op
1262 9ae1b92d 2022-08-13 op void
1263 9ae1b92d 2022-08-13 op syslog_info(const char *fmt, ...)
1264 9ae1b92d 2022-08-13 op {
1265 9ae1b92d 2022-08-13 op va_list ap;
1266 9ae1b92d 2022-08-13 op
1267 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1268 9ae1b92d 2022-08-13 op vsyslog(LOG_INFO, fmt, ap);
1269 9ae1b92d 2022-08-13 op va_end(ap);
1270 9ae1b92d 2022-08-13 op }
1271 9ae1b92d 2022-08-13 op
1272 9ae1b92d 2022-08-13 op void
1273 9ae1b92d 2022-08-13 op syslog_debug(const char *fmt, ...)
1274 9ae1b92d 2022-08-13 op {
1275 9ae1b92d 2022-08-13 op if (verbose > 0) {
1276 9ae1b92d 2022-08-13 op va_list ap;
1277 9ae1b92d 2022-08-13 op va_start(ap, fmt);
1278 9ae1b92d 2022-08-13 op vsyslog(LOG_DEBUG, fmt, ap);
1279 9ae1b92d 2022-08-13 op va_end(ap);
1280 9ae1b92d 2022-08-13 op }
1281 9ae1b92d 2022-08-13 op }