Blame


1 d3a08f4d 2021-01-17 op /*
2 d3a08f4d 2021-01-17 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 d3a08f4d 2021-01-17 op *
4 d3a08f4d 2021-01-17 op * Permission to use, copy, modify, and distribute this software for any
5 d3a08f4d 2021-01-17 op * purpose with or without fee is hereby granted, provided that the above
6 d3a08f4d 2021-01-17 op * copyright notice and this permission notice appear in all copies.
7 d3a08f4d 2021-01-17 op *
8 d3a08f4d 2021-01-17 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d3a08f4d 2021-01-17 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d3a08f4d 2021-01-17 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d3a08f4d 2021-01-17 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d3a08f4d 2021-01-17 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d3a08f4d 2021-01-17 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d3a08f4d 2021-01-17 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 d3a08f4d 2021-01-17 op */
16 52418c8d 2021-02-12 op
17 52418c8d 2021-02-12 op #include "gmid.h"
18 d3a08f4d 2021-01-17 op
19 d3a08f4d 2021-01-17 op #include <sys/stat.h>
20 d3a08f4d 2021-01-17 op
21 d3a08f4d 2021-01-17 op #include <assert.h>
22 efe7d180 2021-10-02 op #include <ctype.h>
23 d3a08f4d 2021-01-17 op #include <errno.h>
24 abc007d2 2021-02-08 op #include <event.h>
25 d3a08f4d 2021-01-17 op #include <fcntl.h>
26 b4d409cf 2021-01-21 op #include <fnmatch.h>
27 2fafa2d2 2021-02-01 op #include <limits.h>
28 d3a08f4d 2021-01-17 op #include <string.h>
29 090b8a89 2021-07-06 op
30 efe7d180 2021-10-02 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
31 efe7d180 2021-10-02 op
32 090b8a89 2021-07-06 op int shutting_down;
33 d3a08f4d 2021-01-17 op
34 8ad1c570 2021-05-09 op struct client clients[MAX_USERS];
35 8ad1c570 2021-05-09 op
36 bc99d868 2021-03-19 op static struct tls *ctx;
37 abc007d2 2021-02-08 op
38 bc99d868 2021-03-19 op static struct event e4, e6, imsgev, siginfo, sigusr2;
39 6b191ed5 2021-02-23 op static int has_ipv6, has_siginfo;
40 abc007d2 2021-02-08 op
41 d3a08f4d 2021-01-17 op int connected_clients;
42 d3a08f4d 2021-01-17 op
43 49b73ba1 2021-02-10 op static inline int matches(const char*, const char*);
44 49b73ba1 2021-02-10 op
45 fe406389 2021-02-02 op static int check_path(struct client*, const char*, int*);
46 abc007d2 2021-02-08 op static void open_file(struct client*);
47 abc007d2 2021-02-08 op static void check_for_cgi(struct client*);
48 abc007d2 2021-02-08 op static void handle_handshake(int, short, void*);
49 91b9f2a8 2021-05-15 op static const char *strip_path(const char*, int);
50 57ec3e77 2021-02-08 op static void fmt_sbuf(const char*, struct client*, const char*);
51 abc007d2 2021-02-08 op static int apply_block_return(struct client*);
52 efe7d180 2021-10-02 op static int apply_fastcgi(struct client*);
53 02be96c6 2021-02-09 op static int apply_require_ca(struct client*);
54 b8e64ccd 2021-03-31 op static size_t host_nth(struct vhost*);
55 abc007d2 2021-02-08 op static void start_cgi(const char*, const char*, struct client*);
56 abc007d2 2021-02-08 op static void open_dir(struct client*);
57 abc007d2 2021-02-08 op static void redirect_canonical_dir(struct client*);
58 efe7d180 2021-10-02 op
59 efe7d180 2021-10-02 op static void client_tls_readcb(int, short, void *);
60 efe7d180 2021-10-02 op static void client_tls_writecb(int, short, void *);
61 efe7d180 2021-10-02 op
62 efe7d180 2021-10-02 op static void client_read(struct bufferevent *, void *);
63 efe7d180 2021-10-02 op void client_write(struct bufferevent *, void *);
64 efe7d180 2021-10-02 op static void client_error(struct bufferevent *, short, void *);
65 efe7d180 2021-10-02 op
66 efe7d180 2021-10-02 op static void client_close_ev(int, short, void *);
67 efe7d180 2021-10-02 op
68 efe7d180 2021-10-02 op static void cgi_read(struct bufferevent *, void *);
69 efe7d180 2021-10-02 op static void cgi_write(struct bufferevent *, void *);
70 efe7d180 2021-10-02 op static void cgi_error(struct bufferevent *, short, void *);
71 efe7d180 2021-10-02 op
72 abc007d2 2021-02-08 op static void do_accept(int, short, void*);
73 efe7d180 2021-10-02 op static struct client *client_by_id(int);
74 efe7d180 2021-10-02 op
75 bc99d868 2021-03-19 op static void handle_imsg_cgi_res(struct imsgbuf*, struct imsg*, size_t);
76 8ad1c570 2021-05-09 op static void handle_imsg_fcgi_fd(struct imsgbuf*, struct imsg*, size_t);
77 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
78 efe7d180 2021-10-02 op static void handle_dispatch_imsg(int, short, void *);
79 bc99d868 2021-03-19 op static void handle_siginfo(int, short, void*);
80 bc99d868 2021-03-19 op
81 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
82 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
83 bc99d868 2021-03-19 op [IMSG_CGI_RES] = handle_imsg_cgi_res,
84 8ad1c570 2021-05-09 op [IMSG_FCGI_FD] = handle_imsg_fcgi_fd,
85 bc99d868 2021-03-19 op };
86 fe406389 2021-02-02 op
87 49b73ba1 2021-02-10 op static inline int
88 49b73ba1 2021-02-10 op matches(const char *pattern, const char *path)
89 49b73ba1 2021-02-10 op {
90 49b73ba1 2021-02-10 op if (*path == '/')
91 49b73ba1 2021-02-10 op path++;
92 49b73ba1 2021-02-10 op return !fnmatch(pattern, path, 0);
93 abc007d2 2021-02-08 op }
94 abc007d2 2021-02-08 op
95 c8b74339 2021-01-24 op const char *
96 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
97 c8b74339 2021-01-24 op {
98 c8b74339 2021-01-24 op struct location *loc;
99 c8b74339 2021-01-24 op
100 caad0308 2021-01-27 op if (v == NULL || path == NULL)
101 6016a593 2021-01-30 op return NULL;
102 8443bff7 2021-01-25 op
103 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
104 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
105 cd761624 2021-02-06 op if (loc->lang != NULL) {
106 49b73ba1 2021-02-10 op if (matches(loc->match, path))
107 6016a593 2021-01-30 op return loc->lang;
108 c8b74339 2021-01-24 op }
109 c8b74339 2021-01-24 op }
110 c8b74339 2021-01-24 op
111 b8e64ccd 2021-03-31 op return TAILQ_FIRST(&v->locations)->lang;
112 c8b74339 2021-01-24 op }
113 c8b74339 2021-01-24 op
114 c8b74339 2021-01-24 op const char *
115 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
116 c8b74339 2021-01-24 op {
117 c8b74339 2021-01-24 op struct location *loc;
118 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
119 caad0308 2021-01-27 op
120 caad0308 2021-01-27 op if (v == NULL || path == NULL)
121 caad0308 2021-01-27 op return default_mime;
122 c8b74339 2021-01-24 op
123 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
124 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
125 cd761624 2021-02-06 op if (loc->default_mime != NULL) {
126 49b73ba1 2021-02-10 op if (matches(loc->match, path))
127 6016a593 2021-01-30 op return loc->default_mime;
128 c8b74339 2021-01-24 op }
129 c8b74339 2021-01-24 op }
130 c8b74339 2021-01-24 op
131 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
132 b8e64ccd 2021-03-31 op if (loc->default_mime != NULL)
133 b8e64ccd 2021-03-31 op return loc->default_mime;
134 c8b74339 2021-01-24 op return default_mime;
135 c8b74339 2021-01-24 op }
136 c8b74339 2021-01-24 op
137 c8b74339 2021-01-24 op const char *
138 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
139 c8b74339 2021-01-24 op {
140 c8b74339 2021-01-24 op struct location *loc;
141 c8b74339 2021-01-24 op const char *index = "index.gmi";
142 c8b74339 2021-01-24 op
143 caad0308 2021-01-27 op if (v == NULL || path == NULL)
144 caad0308 2021-01-27 op return index;
145 caad0308 2021-01-27 op
146 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
147 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
148 cd761624 2021-02-06 op if (loc->index != NULL) {
149 49b73ba1 2021-02-10 op if (matches(loc->match, path))
150 6016a593 2021-01-30 op return loc->index;
151 c8b74339 2021-01-24 op }
152 c8b74339 2021-01-24 op }
153 c8b74339 2021-01-24 op
154 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
155 b8e64ccd 2021-03-31 op if (loc->index != NULL)
156 b8e64ccd 2021-03-31 op return loc->index;
157 c8b74339 2021-01-24 op return index;
158 c8b74339 2021-01-24 op }
159 c8b74339 2021-01-24 op
160 d3a08f4d 2021-01-17 op int
161 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
162 252908e6 2021-01-24 op {
163 252908e6 2021-01-24 op struct location *loc;
164 252908e6 2021-01-24 op
165 6016a593 2021-01-30 op if (v == NULL || path == NULL)
166 6016a593 2021-01-30 op return 0;
167 6016a593 2021-01-30 op
168 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
169 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
170 cd761624 2021-02-06 op if (loc->auto_index != 0) {
171 49b73ba1 2021-02-10 op if (matches(loc->match, path))
172 6016a593 2021-01-30 op return loc->auto_index == 1;
173 252908e6 2021-01-24 op }
174 252908e6 2021-01-24 op }
175 252908e6 2021-01-24 op
176 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
177 b8e64ccd 2021-03-31 op return loc->auto_index == 1;
178 6abda252 2021-02-06 op }
179 6abda252 2021-02-06 op
180 6abda252 2021-02-06 op int
181 6abda252 2021-02-06 op vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
182 6abda252 2021-02-06 op {
183 6abda252 2021-02-06 op struct location *loc;
184 6abda252 2021-02-06 op
185 6abda252 2021-02-06 op if (v == NULL || path == NULL)
186 6abda252 2021-02-06 op return 0;
187 6abda252 2021-02-06 op
188 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
189 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
190 cd761624 2021-02-06 op if (loc->block_code != 0) {
191 49b73ba1 2021-02-10 op if (matches(loc->match, path)) {
192 6abda252 2021-02-06 op *code = loc->block_code;
193 6abda252 2021-02-06 op *fmt = loc->block_fmt;
194 6abda252 2021-02-06 op return 1;
195 6abda252 2021-02-06 op }
196 6abda252 2021-02-06 op }
197 6abda252 2021-02-06 op }
198 6abda252 2021-02-06 op
199 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
200 b8e64ccd 2021-03-31 op *code = loc->block_code;
201 b8e64ccd 2021-03-31 op *fmt = loc->block_fmt;
202 b8e64ccd 2021-03-31 op return loc->block_code != 0;
203 6abda252 2021-02-06 op }
204 6abda252 2021-02-06 op
205 6abda252 2021-02-06 op int
206 8ad1c570 2021-05-09 op vhost_fastcgi(struct vhost *v, const char *path)
207 8ad1c570 2021-05-09 op {
208 8ad1c570 2021-05-09 op struct location *loc;
209 8ad1c570 2021-05-09 op
210 8ad1c570 2021-05-09 op if (v == NULL || path == NULL)
211 8ad1c570 2021-05-09 op return -1;
212 8ad1c570 2021-05-09 op
213 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
214 8ad1c570 2021-05-09 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
215 8ad1c570 2021-05-09 op if (loc->fcgi != -1)
216 8ad1c570 2021-05-09 op if (matches(loc->match, path))
217 8ad1c570 2021-05-09 op return loc->fcgi;
218 8ad1c570 2021-05-09 op }
219 8ad1c570 2021-05-09 op
220 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
221 8ad1c570 2021-05-09 op return loc->fcgi;
222 8ad1c570 2021-05-09 op }
223 8ad1c570 2021-05-09 op
224 8ad1c570 2021-05-09 op int
225 1feaf2a6 2021-05-15 op vhost_dirfd(struct vhost *v, const char *path, size_t *retloc)
226 fdea6aa0 2021-04-30 op {
227 fdea6aa0 2021-04-30 op struct location *loc;
228 1feaf2a6 2021-05-15 op size_t l = 0;
229 fdea6aa0 2021-04-30 op
230 fdea6aa0 2021-04-30 op if (v == NULL || path == NULL)
231 fdea6aa0 2021-04-30 op return -1;
232 fdea6aa0 2021-04-30 op
233 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
234 fdea6aa0 2021-04-30 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
235 1feaf2a6 2021-05-15 op l++;
236 fdea6aa0 2021-04-30 op if (loc->dirfd != -1)
237 1feaf2a6 2021-05-15 op if (matches(loc->match, path)) {
238 1feaf2a6 2021-05-15 op *retloc = l;
239 fdea6aa0 2021-04-30 op return loc->dirfd;
240 1feaf2a6 2021-05-15 op }
241 fdea6aa0 2021-04-30 op }
242 fdea6aa0 2021-04-30 op
243 1feaf2a6 2021-05-15 op *retloc = 0;
244 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
245 fdea6aa0 2021-04-30 op return loc->dirfd;
246 fdea6aa0 2021-04-30 op }
247 fdea6aa0 2021-04-30 op
248 fdea6aa0 2021-04-30 op int
249 6abda252 2021-02-06 op vhost_strip(struct vhost *v, const char *path)
250 6abda252 2021-02-06 op {
251 6abda252 2021-02-06 op struct location *loc;
252 6abda252 2021-02-06 op
253 6abda252 2021-02-06 op if (v == NULL || path == NULL)
254 6abda252 2021-02-06 op return 0;
255 6abda252 2021-02-06 op
256 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
257 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
258 cd761624 2021-02-06 op if (loc->strip != 0) {
259 49b73ba1 2021-02-10 op if (matches(loc->match, path))
260 6abda252 2021-02-06 op return loc->strip;
261 6abda252 2021-02-06 op }
262 6abda252 2021-02-06 op }
263 6abda252 2021-02-06 op
264 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
265 b8e64ccd 2021-03-31 op return loc->strip;
266 252908e6 2021-01-24 op }
267 252908e6 2021-01-24 op
268 02be96c6 2021-02-09 op X509_STORE *
269 02be96c6 2021-02-09 op vhost_require_ca(struct vhost *v, const char *path)
270 02be96c6 2021-02-09 op {
271 02be96c6 2021-02-09 op struct location *loc;
272 02be96c6 2021-02-09 op
273 02be96c6 2021-02-09 op if (v == NULL || path == NULL)
274 02be96c6 2021-02-09 op return NULL;
275 02be96c6 2021-02-09 op
276 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
277 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
278 02be96c6 2021-02-09 op if (loc->reqca != NULL) {
279 49b73ba1 2021-02-10 op if (matches(loc->match, path))
280 02be96c6 2021-02-09 op return loc->reqca;
281 02be96c6 2021-02-09 op }
282 02be96c6 2021-02-09 op }
283 02be96c6 2021-02-09 op
284 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
285 b8e64ccd 2021-03-31 op return loc->reqca;
286 793835cb 2021-02-23 op }
287 793835cb 2021-02-23 op
288 793835cb 2021-02-23 op int
289 793835cb 2021-02-23 op vhost_disable_log(struct vhost *v, const char *path)
290 793835cb 2021-02-23 op {
291 793835cb 2021-02-23 op struct location *loc;
292 793835cb 2021-02-23 op
293 793835cb 2021-02-23 op if (v == NULL || path == NULL)
294 793835cb 2021-02-23 op return 0;
295 793835cb 2021-02-23 op
296 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
297 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
298 793835cb 2021-02-23 op if (loc->disable_log && matches(loc->match, path))
299 793835cb 2021-02-23 op return 1;
300 793835cb 2021-02-23 op }
301 793835cb 2021-02-23 op
302 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
303 b8e64ccd 2021-03-31 op return loc->disable_log;
304 02be96c6 2021-02-09 op }
305 02be96c6 2021-02-09 op
306 fe406389 2021-02-02 op static int
307 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
308 d3a08f4d 2021-01-17 op {
309 d3a08f4d 2021-01-17 op struct stat sb;
310 d1ca3911 2021-01-21 op const char *p;
311 efb48052 2021-07-27 op int dirfd, strip;
312 d3a08f4d 2021-01-17 op
313 d3a08f4d 2021-01-17 op assert(path != NULL);
314 d1ca3911 2021-01-21 op
315 fdea6aa0 2021-04-30 op /*
316 fdea6aa0 2021-04-30 op * in send_dir we add an initial / (to be redirect-friendly),
317 fdea6aa0 2021-04-30 op * but here we want to skip it
318 fdea6aa0 2021-04-30 op */
319 fdea6aa0 2021-04-30 op if (*path == '/')
320 fdea6aa0 2021-04-30 op path++;
321 fdea6aa0 2021-04-30 op
322 fdea6aa0 2021-04-30 op strip = vhost_strip(c->host, path);
323 fdea6aa0 2021-04-30 op p = strip_path(path, strip);
324 fdea6aa0 2021-04-30 op
325 fdea6aa0 2021-04-30 op if (*p == '/')
326 fdea6aa0 2021-04-30 op p = p+1;
327 fdea6aa0 2021-04-30 op if (*p == '\0')
328 d1ca3911 2021-01-21 op p = ".";
329 d1ca3911 2021-01-21 op
330 1feaf2a6 2021-05-15 op dirfd = vhost_dirfd(c->host, path, &c->loc);
331 fdea6aa0 2021-04-30 op log_debug(c, "check_path: strip=%d path=%s original=%s",
332 fdea6aa0 2021-04-30 op strip, p, path);
333 efb48052 2021-07-27 op if (*fd == -1 && (*fd = openat(dirfd, p, O_RDONLY)) == -1)
334 d3a08f4d 2021-01-17 op return FILE_MISSING;
335 d3a08f4d 2021-01-17 op
336 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
337 3abf91b0 2021-02-07 op log_notice(c, "failed stat for %s: %s", path, strerror(errno));
338 d3a08f4d 2021-01-17 op return FILE_MISSING;
339 d3a08f4d 2021-01-17 op }
340 d3a08f4d 2021-01-17 op
341 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
342 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
343 d3a08f4d 2021-01-17 op
344 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
345 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
346 d3a08f4d 2021-01-17 op
347 d3a08f4d 2021-01-17 op return FILE_EXISTS;
348 d3a08f4d 2021-01-17 op }
349 d3a08f4d 2021-01-17 op
350 fe406389 2021-02-02 op static void
351 abc007d2 2021-02-08 op open_file(struct client *c)
352 d3a08f4d 2021-01-17 op {
353 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
354 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
355 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
356 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
357 07b0a142 2021-01-24 op return;
358 07b0a142 2021-01-24 op }
359 d3a08f4d 2021-01-17 op
360 d3a08f4d 2021-01-17 op /* fallthrough */
361 d3a08f4d 2021-01-17 op
362 d3a08f4d 2021-01-17 op case FILE_EXISTS:
363 efe7d180 2021-10-02 op c->type = REQUEST_FILE;
364 27b2fa9a 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
365 07b0a142 2021-01-24 op return;
366 d3a08f4d 2021-01-17 op
367 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
368 abc007d2 2021-02-08 op open_dir(c);
369 07b0a142 2021-01-24 op return;
370 d3a08f4d 2021-01-17 op
371 d3a08f4d 2021-01-17 op case FILE_MISSING:
372 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
373 abc007d2 2021-02-08 op check_for_cgi(c);
374 07b0a142 2021-01-24 op return;
375 07b0a142 2021-01-24 op }
376 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
377 07b0a142 2021-01-24 op return;
378 d3a08f4d 2021-01-17 op
379 d3a08f4d 2021-01-17 op default:
380 d3a08f4d 2021-01-17 op /* unreachable */
381 d3a08f4d 2021-01-17 op abort();
382 252908e6 2021-01-24 op }
383 252908e6 2021-01-24 op }
384 252908e6 2021-01-24 op
385 d3a08f4d 2021-01-17 op /*
386 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
387 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
388 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
389 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
390 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
391 d3a08f4d 2021-01-17 op */
392 fe406389 2021-02-02 op static void
393 abc007d2 2021-02-08 op check_for_cgi(struct client *c)
394 d3a08f4d 2021-01-17 op {
395 2fafa2d2 2021-02-01 op char path[PATH_MAX];
396 d3a08f4d 2021-01-17 op char *end;
397 2fafa2d2 2021-02-01 op
398 2fafa2d2 2021-02-01 op strlcpy(path, c->iri.path, sizeof(path));
399 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
400 d3a08f4d 2021-01-17 op
401 d3a08f4d 2021-01-17 op while (end > path) {
402 571d20fb 2021-05-15 op /*
403 571d20fb 2021-05-15 op * go up one level. UNIX paths are simple and POSIX
404 571d20fb 2021-05-15 op * dirname, with its ambiguities on if the given
405 571d20fb 2021-05-15 op * pointer is changed or not, gives me headaches.
406 571d20fb 2021-05-15 op */
407 3571854e 2021-09-24 op while (*end != '/' && end > path)
408 d3a08f4d 2021-01-17 op end--;
409 3571854e 2021-09-24 op
410 3571854e 2021-09-24 op if (end == path)
411 3571854e 2021-09-24 op break;
412 3571854e 2021-09-24 op
413 d3a08f4d 2021-01-17 op *end = '\0';
414 d3a08f4d 2021-01-17 op
415 abc007d2 2021-02-08 op switch (check_path(c, path, &c->pfd)) {
416 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
417 abc007d2 2021-02-08 op start_cgi(path, end+1, c);
418 07b0a142 2021-01-24 op return;
419 d3a08f4d 2021-01-17 op case FILE_MISSING:
420 d3a08f4d 2021-01-17 op break;
421 d3a08f4d 2021-01-17 op default:
422 d3a08f4d 2021-01-17 op goto err;
423 d3a08f4d 2021-01-17 op }
424 d3a08f4d 2021-01-17 op
425 d3a08f4d 2021-01-17 op *end = '/';
426 d3a08f4d 2021-01-17 op end--;
427 d3a08f4d 2021-01-17 op }
428 d3a08f4d 2021-01-17 op
429 d3a08f4d 2021-01-17 op err:
430 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
431 07b0a142 2021-01-24 op return;
432 d3a08f4d 2021-01-17 op }
433 d3a08f4d 2021-01-17 op
434 9b8f5ed2 2021-02-03 op void
435 9b8f5ed2 2021-02-03 op mark_nonblock(int fd)
436 9b8f5ed2 2021-02-03 op {
437 9b8f5ed2 2021-02-03 op int flags;
438 9b8f5ed2 2021-02-03 op
439 9b8f5ed2 2021-02-03 op if ((flags = fcntl(fd, F_GETFL)) == -1)
440 9b8f5ed2 2021-02-03 op fatal("fcntl(F_GETFL): %s", strerror(errno));
441 9b8f5ed2 2021-02-03 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
442 9b8f5ed2 2021-02-03 op fatal("fcntl(F_SETFL): %s", strerror(errno));
443 9b8f5ed2 2021-02-03 op }
444 9b8f5ed2 2021-02-03 op
445 fe406389 2021-02-02 op static void
446 abc007d2 2021-02-08 op handle_handshake(int fd, short ev, void *d)
447 d3a08f4d 2021-01-17 op {
448 abc007d2 2021-02-08 op struct client *c = d;
449 d3a08f4d 2021-01-17 op struct vhost *h;
450 cc8c2901 2021-04-29 op struct alist *a;
451 d3a08f4d 2021-01-17 op const char *servname;
452 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
453 d3a08f4d 2021-01-17 op
454 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
455 d3a08f4d 2021-01-17 op case 0: /* success */
456 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
457 d3a08f4d 2021-01-17 op break;
458 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
459 efe7d180 2021-10-02 op event_once(c->fd, EV_READ, handle_handshake, c, NULL);
460 d3a08f4d 2021-01-17 op return;
461 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
462 efe7d180 2021-10-02 op event_once(c->fd, EV_WRITE, handle_handshake, c, NULL);
463 d3a08f4d 2021-01-17 op return;
464 d3a08f4d 2021-01-17 op default:
465 d3a08f4d 2021-01-17 op /* unreachable */
466 d3a08f4d 2021-01-17 op abort();
467 d3a08f4d 2021-01-17 op }
468 d3a08f4d 2021-01-17 op
469 79288c8b 2021-09-24 op if ((servname = tls_conn_servername(c->ctx)) == NULL) {
470 79288c8b 2021-09-24 op log_debug(c, "handshake: missing SNI");
471 79288c8b 2021-09-24 op goto err;
472 79288c8b 2021-09-24 op }
473 79288c8b 2021-09-24 op
474 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
475 3abf91b0 2021-02-07 op log_info(c, "puny_decode: %s", parse_err);
476 a8d4a897 2021-01-29 op goto err;
477 a8d4a897 2021-01-29 op }
478 d3a08f4d 2021-01-17 op
479 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
480 eecad7a3 2021-02-12 op if (matches(h->domain, c->domain))
481 cc8c2901 2021-04-29 op goto found;
482 cc8c2901 2021-04-29 op TAILQ_FOREACH(a, &h->aliases, aliases) {
483 cc8c2901 2021-04-29 op if (matches(a->alias, c->domain))
484 cc8c2901 2021-04-29 op goto found;
485 cc8c2901 2021-04-29 op }
486 d3a08f4d 2021-01-17 op }
487 d3a08f4d 2021-01-17 op
488 cc8c2901 2021-04-29 op found:
489 3abf91b0 2021-02-07 op log_debug(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
490 3abf91b0 2021-02-07 op servname != NULL ? servname : "(null)",
491 3abf91b0 2021-02-07 op c->domain,
492 b8e64ccd 2021-03-31 op h != NULL ? h->domain : "(null)");
493 22c6d633 2021-01-27 op
494 b8e64ccd 2021-03-31 op if (h != NULL) {
495 d3a08f4d 2021-01-17 op c->host = h;
496 efe7d180 2021-10-02 op
497 efe7d180 2021-10-02 op c->bev = bufferevent_new(fd, client_read, client_write,
498 efe7d180 2021-10-02 op client_error, c);
499 efe7d180 2021-10-02 op if (c->bev == NULL)
500 efe7d180 2021-10-02 op fatal("%s: failed to allocate client buffer: %s",
501 efe7d180 2021-10-02 op __func__, strerror(errno));
502 efe7d180 2021-10-02 op
503 efe7d180 2021-10-02 op event_set(&c->bev->ev_read, c->fd, EV_READ,
504 efe7d180 2021-10-02 op client_tls_readcb, c->bev);
505 efe7d180 2021-10-02 op event_set(&c->bev->ev_write, c->fd, EV_WRITE,
506 efe7d180 2021-10-02 op client_tls_writecb, c->bev);
507 efe7d180 2021-10-02 op
508 acafce5b 2021-10-02 op #if HAVE_LIBEVENT2
509 acafce5b 2021-10-02 op evbuffer_unfreeze(c->bev->input, 0);
510 acafce5b 2021-10-02 op evbuffer_unfreeze(c->bev->output, 1);
511 acafce5b 2021-10-02 op #endif
512 acafce5b 2021-10-02 op
513 efe7d180 2021-10-02 op bufferevent_enable(c->bev, EV_READ);
514 efe7d180 2021-10-02 op
515 d3a08f4d 2021-01-17 op return;
516 d3a08f4d 2021-01-17 op }
517 d3a08f4d 2021-01-17 op
518 a8d4a897 2021-01-29 op err:
519 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
520 6abda252 2021-02-06 op }
521 6abda252 2021-02-06 op
522 91b9f2a8 2021-05-15 op static const char *
523 91b9f2a8 2021-05-15 op strip_path(const char *path, int strip)
524 6abda252 2021-02-06 op {
525 57ec3e77 2021-02-08 op char *t;
526 6abda252 2021-02-06 op
527 6abda252 2021-02-06 op while (strip > 0) {
528 6abda252 2021-02-06 op if ((t = strchr(path, '/')) == NULL) {
529 6abda252 2021-02-06 op path = strchr(path, '\0');
530 6abda252 2021-02-06 op break;
531 6abda252 2021-02-06 op }
532 6abda252 2021-02-06 op path = t;
533 6abda252 2021-02-06 op strip--;
534 6abda252 2021-02-06 op }
535 57ec3e77 2021-02-08 op
536 57ec3e77 2021-02-08 op return path;
537 57ec3e77 2021-02-08 op }
538 57ec3e77 2021-02-08 op
539 57ec3e77 2021-02-08 op static void
540 57ec3e77 2021-02-08 op fmt_sbuf(const char *fmt, struct client *c, const char *path)
541 57ec3e77 2021-02-08 op {
542 57ec3e77 2021-02-08 op size_t i;
543 57ec3e77 2021-02-08 op char buf[32];
544 6abda252 2021-02-06 op
545 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
546 6abda252 2021-02-06 op for (i = 0; *fmt; ++fmt) {
547 6abda252 2021-02-06 op if (i == sizeof(buf)-1 || *fmt == '%') {
548 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
549 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
550 6abda252 2021-02-06 op i = 0;
551 6abda252 2021-02-06 op }
552 6abda252 2021-02-06 op
553 6abda252 2021-02-06 op if (*fmt != '%') {
554 6abda252 2021-02-06 op buf[i++] = *fmt;
555 6abda252 2021-02-06 op continue;
556 6abda252 2021-02-06 op }
557 6abda252 2021-02-06 op
558 6abda252 2021-02-06 op switch (*++fmt) {
559 6abda252 2021-02-06 op case '%':
560 6abda252 2021-02-06 op strlcat(c->sbuf, "%", sizeof(c->sbuf));
561 6abda252 2021-02-06 op break;
562 6abda252 2021-02-06 op case 'p':
563 737a6b50 2021-04-30 op if (*path != '/')
564 737a6b50 2021-04-30 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
565 6abda252 2021-02-06 op strlcat(c->sbuf, path, sizeof(c->sbuf));
566 6abda252 2021-02-06 op break;
567 6abda252 2021-02-06 op case 'q':
568 6abda252 2021-02-06 op strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
569 6abda252 2021-02-06 op break;
570 6abda252 2021-02-06 op case 'P':
571 6abda252 2021-02-06 op snprintf(buf, sizeof(buf), "%d", conf.port);
572 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
573 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
574 6abda252 2021-02-06 op break;
575 6abda252 2021-02-06 op case 'N':
576 6abda252 2021-02-06 op strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
577 6abda252 2021-02-06 op break;
578 6abda252 2021-02-06 op default:
579 6abda252 2021-02-06 op fatal("%s: unknown fmt specifier %c",
580 6abda252 2021-02-06 op __func__, *fmt);
581 6abda252 2021-02-06 op }
582 6abda252 2021-02-06 op }
583 6abda252 2021-02-06 op
584 6abda252 2021-02-06 op if (i != 0)
585 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
586 57ec3e77 2021-02-08 op }
587 6abda252 2021-02-06 op
588 57ec3e77 2021-02-08 op /* 1 if a matching `block return' (and apply it), 0 otherwise */
589 57ec3e77 2021-02-08 op static int
590 57ec3e77 2021-02-08 op apply_block_return(struct client *c)
591 57ec3e77 2021-02-08 op {
592 57ec3e77 2021-02-08 op const char *fmt, *path;
593 57ec3e77 2021-02-08 op int code;
594 57ec3e77 2021-02-08 op
595 57ec3e77 2021-02-08 op if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
596 57ec3e77 2021-02-08 op return 0;
597 57ec3e77 2021-02-08 op
598 57ec3e77 2021-02-08 op path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
599 57ec3e77 2021-02-08 op fmt_sbuf(fmt, c, path);
600 57ec3e77 2021-02-08 op
601 abc007d2 2021-02-08 op start_reply(c, code, c->sbuf);
602 8ad1c570 2021-05-09 op return 1;
603 8ad1c570 2021-05-09 op }
604 8ad1c570 2021-05-09 op
605 8ad1c570 2021-05-09 op /* 1 if matching `fcgi' (and apply it), 0 otherwise */
606 8ad1c570 2021-05-09 op static int
607 8ad1c570 2021-05-09 op apply_fastcgi(struct client *c)
608 8ad1c570 2021-05-09 op {
609 8ad1c570 2021-05-09 op int id;
610 8ad1c570 2021-05-09 op struct fcgi *f;
611 8ad1c570 2021-05-09 op
612 8ad1c570 2021-05-09 op if ((id = vhost_fastcgi(c->host, c->iri.path)) == -1)
613 8ad1c570 2021-05-09 op return 0;
614 8ad1c570 2021-05-09 op
615 8ad1c570 2021-05-09 op switch ((f = &fcgi[id])->s) {
616 8ad1c570 2021-05-09 op case FCGI_OFF:
617 8ad1c570 2021-05-09 op f->s = FCGI_INFLIGHT;
618 8ad1c570 2021-05-09 op log_info(c, "opening fastcgi connection for (%s,%s,%s)",
619 8ad1c570 2021-05-09 op f->path, f->port, f->prog);
620 8ad1c570 2021-05-09 op imsg_compose(&exibuf, IMSG_FCGI_REQ, 0, 0, -1,
621 8ad1c570 2021-05-09 op &id, sizeof(id));
622 8ad1c570 2021-05-09 op imsg_flush(&exibuf);
623 8ad1c570 2021-05-09 op /* fallthrough */
624 8ad1c570 2021-05-09 op case FCGI_INFLIGHT:
625 8ad1c570 2021-05-09 op c->fcgi = id;
626 8ad1c570 2021-05-09 op break;
627 8ad1c570 2021-05-09 op case FCGI_READY:
628 8ad1c570 2021-05-09 op c->fcgi = id;
629 741b69be 2021-09-26 op fcgi_req(f, c);
630 8ad1c570 2021-05-09 op break;
631 8ad1c570 2021-05-09 op }
632 8ad1c570 2021-05-09 op
633 6abda252 2021-02-06 op return 1;
634 d3a08f4d 2021-01-17 op }
635 d3a08f4d 2021-01-17 op
636 02be96c6 2021-02-09 op /* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
637 02be96c6 2021-02-09 op static int
638 02be96c6 2021-02-09 op apply_require_ca(struct client *c)
639 02be96c6 2021-02-09 op {
640 02be96c6 2021-02-09 op X509_STORE *store;
641 02be96c6 2021-02-09 op const uint8_t *cert;
642 02be96c6 2021-02-09 op size_t len;
643 02be96c6 2021-02-09 op
644 02be96c6 2021-02-09 op if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
645 02be96c6 2021-02-09 op return 0;
646 02be96c6 2021-02-09 op
647 02be96c6 2021-02-09 op if (!tls_peer_cert_provided(c->ctx)) {
648 02be96c6 2021-02-09 op start_reply(c, CLIENT_CERT_REQ, "client certificate required");
649 02be96c6 2021-02-09 op return 1;
650 02be96c6 2021-02-09 op }
651 02be96c6 2021-02-09 op
652 02be96c6 2021-02-09 op cert = tls_peer_cert_chain_pem(c->ctx, &len);
653 02be96c6 2021-02-09 op if (!validate_against_ca(store, cert, len)) {
654 02be96c6 2021-02-09 op start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
655 02be96c6 2021-02-09 op return 1;
656 02be96c6 2021-02-09 op }
657 02be96c6 2021-02-09 op
658 02be96c6 2021-02-09 op return 0;
659 02be96c6 2021-02-09 op }
660 02be96c6 2021-02-09 op
661 b8e64ccd 2021-03-31 op static size_t
662 b8e64ccd 2021-03-31 op host_nth(struct vhost *h)
663 b8e64ccd 2021-03-31 op {
664 b8e64ccd 2021-03-31 op struct vhost *v;
665 b8e64ccd 2021-03-31 op size_t i = 0;
666 b8e64ccd 2021-03-31 op
667 b8e64ccd 2021-03-31 op TAILQ_FOREACH(v, &hosts, vhosts) {
668 b8e64ccd 2021-03-31 op if (v == h)
669 b8e64ccd 2021-03-31 op return i;
670 b8e64ccd 2021-03-31 op i++;
671 b8e64ccd 2021-03-31 op }
672 b8e64ccd 2021-03-31 op
673 b8e64ccd 2021-03-31 op abort();
674 b8e64ccd 2021-03-31 op }
675 b8e64ccd 2021-03-31 op
676 fe406389 2021-02-02 op static void
677 abc007d2 2021-02-08 op start_cgi(const char *spath, const char *relpath, struct client *c)
678 d3a08f4d 2021-01-17 op {
679 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
680 bc99d868 2021-03-19 op const char *t;
681 bc99d868 2021-03-19 op struct cgireq req;
682 d3a08f4d 2021-01-17 op int e;
683 d3a08f4d 2021-01-17 op
684 efe7d180 2021-10-02 op c->type = REQUEST_CGI;
685 efe7d180 2021-10-02 op
686 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
687 d3a08f4d 2021-01-17 op addr, sizeof(addr),
688 d3a08f4d 2021-01-17 op NULL, 0,
689 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
690 d3a08f4d 2021-01-17 op if (e != 0)
691 bc99d868 2021-03-19 op fatal("getnameinfo failed");
692 d3a08f4d 2021-01-17 op
693 bc99d868 2021-03-19 op memset(&req, 0, sizeof(req));
694 d3a08f4d 2021-01-17 op
695 bc99d868 2021-03-19 op memcpy(req.buf, c->req, sizeof(req.buf));
696 bc99d868 2021-03-19 op
697 bc99d868 2021-03-19 op req.iri_schema_off = c->iri.schema - c->req;
698 bc99d868 2021-03-19 op req.iri_host_off = c->iri.host - c->req;
699 bc99d868 2021-03-19 op req.iri_port_off = c->iri.port - c->req;
700 bc99d868 2021-03-19 op req.iri_path_off = c->iri.path - c->req;
701 bc99d868 2021-03-19 op req.iri_query_off = c->iri.query - c->req;
702 bc99d868 2021-03-19 op req.iri_fragment_off = c->iri.fragment - c->req;
703 35744950 2021-02-01 op
704 bc99d868 2021-03-19 op req.iri_portno = c->iri.port_no;
705 d3a08f4d 2021-01-17 op
706 bc99d868 2021-03-19 op strlcpy(req.spath, spath, sizeof(req.spath));
707 bc99d868 2021-03-19 op strlcpy(req.relpath, relpath, sizeof(req.relpath));
708 bc99d868 2021-03-19 op strlcpy(req.addr, addr, sizeof(req.addr));
709 bc99d868 2021-03-19 op
710 bc99d868 2021-03-19 op if ((t = tls_peer_cert_subject(c->ctx)) != NULL)
711 bc99d868 2021-03-19 op strlcpy(req.subject, t, sizeof(req.subject));
712 bc99d868 2021-03-19 op if ((t = tls_peer_cert_issuer(c->ctx)) != NULL)
713 bc99d868 2021-03-19 op strlcpy(req.issuer, t, sizeof(req.issuer));
714 bc99d868 2021-03-19 op if ((t = tls_peer_cert_hash(c->ctx)) != NULL)
715 bc99d868 2021-03-19 op strlcpy(req.hash, t, sizeof(req.hash));
716 89541eee 2021-04-13 op if ((t = tls_conn_version(c->ctx)) != NULL)
717 89541eee 2021-04-13 op strlcpy(req.version, t, sizeof(req.version));
718 89541eee 2021-04-13 op if ((t = tls_conn_cipher(c->ctx)) != NULL)
719 89541eee 2021-04-13 op strlcpy(req.cipher, t, sizeof(req.cipher));
720 bc99d868 2021-03-19 op
721 89541eee 2021-04-13 op req.cipher_strength = tls_conn_cipher_strength(c->ctx);
722 bc99d868 2021-03-19 op req.notbefore = tls_peer_cert_notbefore(c->ctx);
723 bc99d868 2021-03-19 op req.notafter = tls_peer_cert_notafter(c->ctx);
724 bc99d868 2021-03-19 op
725 b8e64ccd 2021-03-31 op req.host_off = host_nth(c->host);
726 1feaf2a6 2021-05-15 op req.loc_off = c->loc;
727 bc99d868 2021-03-19 op
728 bc99d868 2021-03-19 op imsg_compose(&exibuf, IMSG_CGI_REQ, c->id, 0, -1, &req, sizeof(req));
729 bc99d868 2021-03-19 op imsg_flush(&exibuf);
730 bc99d868 2021-03-19 op
731 bc99d868 2021-03-19 op close(c->pfd);
732 d3a08f4d 2021-01-17 op }
733 d3a08f4d 2021-01-17 op
734 fe406389 2021-02-02 op static void
735 abc007d2 2021-02-08 op open_dir(struct client *c)
736 d3a08f4d 2021-01-17 op {
737 d3a08f4d 2021-01-17 op size_t len;
738 11c98667 2021-04-25 op int dirfd, root;
739 252908e6 2021-01-24 op char *before_file;
740 11c98667 2021-04-25 op
741 11c98667 2021-04-25 op root = !strcmp(c->iri.path, "/") || *c->iri.path == '\0';
742 d1ca3911 2021-01-21 op
743 0be51733 2021-01-20 op len = strlen(c->iri.path);
744 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
745 abc007d2 2021-02-08 op redirect_canonical_dir(c);
746 0be51733 2021-01-20 op return;
747 d3a08f4d 2021-01-17 op }
748 d3a08f4d 2021-01-17 op
749 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
750 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
751 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
752 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
753 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
754 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
755 c8b74339 2021-01-24 op sizeof(c->sbuf));
756 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
757 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
758 252908e6 2021-01-24 op return;
759 252908e6 2021-01-24 op }
760 d3a08f4d 2021-01-17 op
761 252908e6 2021-01-24 op c->iri.path = c->sbuf;
762 252908e6 2021-01-24 op
763 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
764 abc007d2 2021-02-08 op dirfd = c->pfd;
765 abc007d2 2021-02-08 op c->pfd = -1;
766 252908e6 2021-01-24 op
767 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
768 252908e6 2021-01-24 op case FILE_EXECUTABLE:
769 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
770 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
771 252908e6 2021-01-24 op break;
772 252908e6 2021-01-24 op }
773 252908e6 2021-01-24 op
774 252908e6 2021-01-24 op /* fallthrough */
775 252908e6 2021-01-24 op
776 252908e6 2021-01-24 op case FILE_EXISTS:
777 efe7d180 2021-10-02 op c->type = REQUEST_FILE;
778 98ee8406 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
779 252908e6 2021-01-24 op break;
780 252908e6 2021-01-24 op
781 252908e6 2021-01-24 op case FILE_DIRECTORY:
782 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
783 252908e6 2021-01-24 op break;
784 252908e6 2021-01-24 op
785 252908e6 2021-01-24 op case FILE_MISSING:
786 252908e6 2021-01-24 op *before_file = '\0';
787 252908e6 2021-01-24 op
788 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
789 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
790 252908e6 2021-01-24 op break;
791 252908e6 2021-01-24 op }
792 252908e6 2021-01-24 op
793 efe7d180 2021-10-02 op c->type = REQUEST_DIR;
794 252908e6 2021-01-24 op
795 e76f2c74 2021-04-25 op c->dirlen = scandir_fd(dirfd, &c->dir,
796 11c98667 2021-04-25 op root ? select_non_dotdot : select_non_dot,
797 11c98667 2021-04-25 op alphasort);
798 11c98667 2021-04-25 op if (c->dirlen == -1) {
799 11c98667 2021-04-25 op log_err(c, "scandir_fd(%d) (vhost:%s) %s: %s",
800 abc007d2 2021-02-08 op c->pfd, c->host->domain, c->iri.path, strerror(errno));
801 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
802 252908e6 2021-01-24 op return;
803 252908e6 2021-01-24 op }
804 11c98667 2021-04-25 op c->diroff = 0;
805 252908e6 2021-01-24 op c->off = 0;
806 252908e6 2021-01-24 op
807 abc007d2 2021-02-08 op start_reply(c, SUCCESS, "text/gemini");
808 efe7d180 2021-10-02 op evbuffer_add_printf(EVBUFFER_OUTPUT(c->bev),
809 efe7d180 2021-10-02 op "# Index of %s\n\n", c->iri.path);
810 252908e6 2021-01-24 op return;
811 252908e6 2021-01-24 op
812 252908e6 2021-01-24 op default:
813 252908e6 2021-01-24 op /* unreachable */
814 252908e6 2021-01-24 op abort();
815 252908e6 2021-01-24 op }
816 252908e6 2021-01-24 op
817 252908e6 2021-01-24 op close(dirfd);
818 252908e6 2021-01-24 op }
819 252908e6 2021-01-24 op
820 fe406389 2021-02-02 op static void
821 abc007d2 2021-02-08 op redirect_canonical_dir(struct client *c)
822 252908e6 2021-01-24 op {
823 252908e6 2021-01-24 op size_t len;
824 252908e6 2021-01-24 op
825 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
826 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
827 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
828 252908e6 2021-01-24 op
829 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
830 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
831 0be51733 2021-01-20 op return;
832 0be51733 2021-01-20 op }
833 0be51733 2021-01-20 op
834 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
835 d3a08f4d 2021-01-17 op }
836 5f715ce4 2021-02-02 op
837 fe406389 2021-02-02 op static void
838 efe7d180 2021-10-02 op client_tls_readcb(int fd, short event, void *d)
839 5f715ce4 2021-02-02 op {
840 efe7d180 2021-10-02 op struct bufferevent *bufev = d;
841 efe7d180 2021-10-02 op struct client *client = bufev->cbarg;
842 efe7d180 2021-10-02 op ssize_t ret;
843 efe7d180 2021-10-02 op size_t len;
844 efe7d180 2021-10-02 op int what = EVBUFFER_READ;
845 efe7d180 2021-10-02 op int howmuch = IBUF_READ_SIZE;
846 efe7d180 2021-10-02 op char buf[IBUF_READ_SIZE];
847 d3a08f4d 2021-01-17 op
848 efe7d180 2021-10-02 op if (event == EV_TIMEOUT) {
849 efe7d180 2021-10-02 op what |= EVBUFFER_TIMEOUT;
850 efe7d180 2021-10-02 op goto err;
851 5f715ce4 2021-02-02 op }
852 5f715ce4 2021-02-02 op
853 efe7d180 2021-10-02 op if (bufev->wm_read.high != 0)
854 efe7d180 2021-10-02 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
855 5f715ce4 2021-02-02 op
856 efe7d180 2021-10-02 op switch (ret = tls_read(client->ctx, buf, howmuch)) {
857 efe7d180 2021-10-02 op case TLS_WANT_POLLIN:
858 efe7d180 2021-10-02 op case TLS_WANT_POLLOUT:
859 efe7d180 2021-10-02 op goto retry;
860 efe7d180 2021-10-02 op case -1:
861 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
862 efe7d180 2021-10-02 op goto err;
863 efe7d180 2021-10-02 op }
864 efe7d180 2021-10-02 op len = ret;
865 5f715ce4 2021-02-02 op
866 efe7d180 2021-10-02 op if (len == 0) {
867 efe7d180 2021-10-02 op what |= EVBUFFER_EOF;
868 efe7d180 2021-10-02 op goto err;
869 5f715ce4 2021-02-02 op }
870 5f715ce4 2021-02-02 op
871 efe7d180 2021-10-02 op if (evbuffer_add(bufev->input, buf, len) == -1) {
872 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
873 efe7d180 2021-10-02 op goto err;
874 efe7d180 2021-10-02 op }
875 5f715ce4 2021-02-02 op
876 efe7d180 2021-10-02 op event_add(&bufev->ev_read, NULL);
877 efe7d180 2021-10-02 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
878 efe7d180 2021-10-02 op return;
879 efe7d180 2021-10-02 op if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
880 efe7d180 2021-10-02 op /*
881 efe7d180 2021-10-02 op * here we could implement a read pressure policy.
882 efe7d180 2021-10-02 op */
883 efe7d180 2021-10-02 op }
884 252908e6 2021-01-24 op
885 efe7d180 2021-10-02 op if (bufev->readcb != NULL)
886 efe7d180 2021-10-02 op (*bufev->readcb)(bufev, bufev->cbarg);
887 11c98667 2021-04-25 op
888 efe7d180 2021-10-02 op return;
889 11c98667 2021-04-25 op
890 efe7d180 2021-10-02 op retry:
891 efe7d180 2021-10-02 op event_add(&bufev->ev_read, NULL);
892 efe7d180 2021-10-02 op return;
893 efe7d180 2021-10-02 op
894 efe7d180 2021-10-02 op err:
895 efe7d180 2021-10-02 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
896 252908e6 2021-01-24 op }
897 252908e6 2021-01-24 op
898 fe406389 2021-02-02 op static void
899 efe7d180 2021-10-02 op client_tls_writecb(int fd, short event, void *d)
900 252908e6 2021-01-24 op {
901 efe7d180 2021-10-02 op struct bufferevent *bufev = d;
902 efe7d180 2021-10-02 op struct client *client = bufev->cbarg;
903 efe7d180 2021-10-02 op ssize_t ret;
904 efe7d180 2021-10-02 op size_t len;
905 efe7d180 2021-10-02 op short what = EVBUFFER_WRITE;
906 252908e6 2021-01-24 op
907 efe7d180 2021-10-02 op if (event == EV_TIMEOUT) {
908 efe7d180 2021-10-02 op what |= EVBUFFER_TIMEOUT;
909 efe7d180 2021-10-02 op goto err;
910 efe7d180 2021-10-02 op }
911 252908e6 2021-01-24 op
912 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
913 efe7d180 2021-10-02 op ret = tls_write(client->ctx,
914 efe7d180 2021-10-02 op EVBUFFER_DATA(bufev->output),
915 efe7d180 2021-10-02 op EVBUFFER_LENGTH(bufev->output));
916 efe7d180 2021-10-02 op switch (ret) {
917 efe7d180 2021-10-02 op case TLS_WANT_POLLIN:
918 efe7d180 2021-10-02 op case TLS_WANT_POLLOUT:
919 efe7d180 2021-10-02 op goto retry;
920 efe7d180 2021-10-02 op case -1:
921 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
922 efe7d180 2021-10-02 op goto err;
923 252908e6 2021-01-24 op }
924 efe7d180 2021-10-02 op len = ret;
925 efe7d180 2021-10-02 op evbuffer_drain(bufev->output, len);
926 252908e6 2021-01-24 op }
927 252908e6 2021-01-24 op
928 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(bufev->output) != 0)
929 efe7d180 2021-10-02 op event_add(&bufev->ev_write, NULL);
930 efe7d180 2021-10-02 op
931 efe7d180 2021-10-02 op if (bufev->writecb != NULL &&
932 efe7d180 2021-10-02 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
933 efe7d180 2021-10-02 op (*bufev->writecb)(bufev, bufev->cbarg);
934 efe7d180 2021-10-02 op return;
935 efe7d180 2021-10-02 op
936 efe7d180 2021-10-02 op retry:
937 efe7d180 2021-10-02 op event_add(&bufev->ev_write, NULL);
938 efe7d180 2021-10-02 op return;
939 efe7d180 2021-10-02 op err:
940 efe7d180 2021-10-02 op log_err(client, "tls error: %s", tls_error(client->ctx));
941 efe7d180 2021-10-02 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
942 252908e6 2021-01-24 op }
943 252908e6 2021-01-24 op
944 fe406389 2021-02-02 op static void
945 efe7d180 2021-10-02 op client_read(struct bufferevent *bev, void *d)
946 3309ef97 2021-01-23 op {
947 efe7d180 2021-10-02 op struct client *c = d;
948 efe7d180 2021-10-02 op struct evbuffer *src = EVBUFFER_INPUT(bev);
949 efe7d180 2021-10-02 op const char *parse_err = "invalid request";
950 efe7d180 2021-10-02 op char decoded[DOMAIN_NAME_LEN];
951 efe7d180 2021-10-02 op size_t len;
952 3309ef97 2021-01-23 op
953 efe7d180 2021-10-02 op bufferevent_disable(bev, EVBUFFER_READ);
954 abc007d2 2021-02-08 op
955 efe7d180 2021-10-02 op /* max url len + \r\n */
956 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(src) > 1024 + 2) {
957 efe7d180 2021-10-02 op log_err(c, "too much data received");
958 efe7d180 2021-10-02 op start_reply(c, BAD_REQUEST, "bad request");
959 efe7d180 2021-10-02 op return;
960 efe7d180 2021-10-02 op }
961 3309ef97 2021-01-23 op
962 efe7d180 2021-10-02 op c->req = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
963 efe7d180 2021-10-02 op if (c->req == NULL) {
964 efe7d180 2021-10-02 op /* not enough data yet. */
965 efe7d180 2021-10-02 op bufferevent_enable(bev, EVBUFFER_READ);
966 35744950 2021-02-01 op return;
967 3309ef97 2021-01-23 op }
968 3309ef97 2021-01-23 op
969 efe7d180 2021-10-02 op if (!parse_iri(c->req, &c->iri, &parse_err) ||
970 efe7d180 2021-10-02 op !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
971 efe7d180 2021-10-02 op log_err(c, "IRI parse error: %s", parse_err);
972 efe7d180 2021-10-02 op start_reply(c, BAD_REQUEST, "bad request");
973 efe7d180 2021-10-02 op return;
974 efe7d180 2021-10-02 op }
975 3309ef97 2021-01-23 op
976 efe7d180 2021-10-02 op if (c->iri.port_no != conf.port ||
977 efe7d180 2021-10-02 op strcmp(c->iri.schema, "gemini") ||
978 efe7d180 2021-10-02 op strcmp(decoded, c->domain)) {
979 efe7d180 2021-10-02 op start_reply(c, PROXY_REFUSED, "won't proxy request");
980 efe7d180 2021-10-02 op return;
981 efe7d180 2021-10-02 op }
982 3309ef97 2021-01-23 op
983 efe7d180 2021-10-02 op if (apply_require_ca(c) ||
984 efe7d180 2021-10-02 op apply_block_return(c)||
985 efe7d180 2021-10-02 op apply_fastcgi(c))
986 35744950 2021-02-01 op return;
987 efe7d180 2021-10-02 op
988 efe7d180 2021-10-02 op if (c->host->entrypoint != NULL) {
989 efe7d180 2021-10-02 op c->loc = 0;
990 efe7d180 2021-10-02 op start_cgi(c->host->entrypoint, c->iri.path, c);
991 efe7d180 2021-10-02 op return;
992 35744950 2021-02-01 op }
993 abc007d2 2021-02-08 op
994 efe7d180 2021-10-02 op open_file(c);
995 3309ef97 2021-01-23 op }
996 3309ef97 2021-01-23 op
997 efe7d180 2021-10-02 op void
998 efe7d180 2021-10-02 op client_write(struct bufferevent *bev, void *d)
999 d3a08f4d 2021-01-17 op {
1000 efe7d180 2021-10-02 op struct client *c = d;
1001 efe7d180 2021-10-02 op struct evbuffer *out = EVBUFFER_OUTPUT(bev);
1002 efe7d180 2021-10-02 op char buf[BUFSIZ];
1003 efe7d180 2021-10-02 op ssize_t r;
1004 d3a08f4d 2021-01-17 op
1005 efe7d180 2021-10-02 op switch (c->type) {
1006 efe7d180 2021-10-02 op case REQUEST_UNDECIDED:
1007 efe7d180 2021-10-02 op /*
1008 efe7d180 2021-10-02 op * Ignore spurious calls when we still don't have idea
1009 efe7d180 2021-10-02 op * what to do with the request.
1010 efe7d180 2021-10-02 op */
1011 efe7d180 2021-10-02 op break;
1012 d3a08f4d 2021-01-17 op
1013 efe7d180 2021-10-02 op case REQUEST_FILE:
1014 efe7d180 2021-10-02 op if ((r = read(c->pfd, buf, sizeof(buf))) == -1) {
1015 efe7d180 2021-10-02 op log_warn(c, "read: %s", strerror(errno));
1016 efe7d180 2021-10-02 op client_error(bev, EVBUFFER_ERROR, c);
1017 efe7d180 2021-10-02 op return;
1018 efe7d180 2021-10-02 op } else if (r == 0) {
1019 efe7d180 2021-10-02 op client_close(c);
1020 efe7d180 2021-10-02 op return;
1021 efe7d180 2021-10-02 op } else if (r != sizeof(buf))
1022 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1023 efe7d180 2021-10-02 op bufferevent_write(bev, buf, r);
1024 efe7d180 2021-10-02 op break;
1025 d3a08f4d 2021-01-17 op
1026 efe7d180 2021-10-02 op case REQUEST_DIR:
1027 efe7d180 2021-10-02 op /* TODO: handle big big directories better */
1028 efe7d180 2021-10-02 op for (c->diroff = 0; c->diroff < c->dirlen; ++c->diroff) {
1029 efe7d180 2021-10-02 op evbuffer_add_printf(out, "=> %s\n",
1030 efe7d180 2021-10-02 op c->dir[c->diroff]->d_name);
1031 efe7d180 2021-10-02 op free(c->dir[c->diroff]);
1032 d3a08f4d 2021-01-17 op }
1033 efe7d180 2021-10-02 op free(c->dir);
1034 efe7d180 2021-10-02 op c->dir = NULL;
1035 27b2fa9a 2021-02-12 op
1036 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1037 efe7d180 2021-10-02 op
1038 efe7d180 2021-10-02 op event_add(&c->bev->ev_write, NULL);
1039 efe7d180 2021-10-02 op break;
1040 efe7d180 2021-10-02 op
1041 efe7d180 2021-10-02 op case REQUEST_CGI:
1042 efe7d180 2021-10-02 op case REQUEST_FCGI:
1043 efe7d180 2021-10-02 op /*
1044 efe7d180 2021-10-02 op * Here we depend on on the cgi script or fastcgi
1045 efe7d180 2021-10-02 op * connection to provide data.
1046 efe7d180 2021-10-02 op */
1047 efe7d180 2021-10-02 op break;
1048 efe7d180 2021-10-02 op
1049 efe7d180 2021-10-02 op case REQUEST_DONE:
1050 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(out) == 0)
1051 efe7d180 2021-10-02 op client_close(c);
1052 efe7d180 2021-10-02 op break;
1053 d3a08f4d 2021-01-17 op }
1054 efe7d180 2021-10-02 op }
1055 d3a08f4d 2021-01-17 op
1056 efe7d180 2021-10-02 op static void
1057 efe7d180 2021-10-02 op client_error(struct bufferevent *bev, short error, void *d)
1058 efe7d180 2021-10-02 op {
1059 efe7d180 2021-10-02 op struct client *c = d;
1060 efe7d180 2021-10-02 op
1061 efe7d180 2021-10-02 op if (c->type == REQUEST_FCGI)
1062 efe7d180 2021-10-02 op fcgi_abort_request(c);
1063 efe7d180 2021-10-02 op
1064 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1065 efe7d180 2021-10-02 op
1066 efe7d180 2021-10-02 op if (error & EVBUFFER_TIMEOUT) {
1067 efe7d180 2021-10-02 op log_warn(c, "timeout reached, "
1068 efe7d180 2021-10-02 op "forcefully closing the connection");
1069 efe7d180 2021-10-02 op if (c->code == 0)
1070 efe7d180 2021-10-02 op start_reply(c, BAD_REQUEST, "timeout");
1071 efe7d180 2021-10-02 op else
1072 efe7d180 2021-10-02 op client_close(c);
1073 efe7d180 2021-10-02 op return;
1074 efe7d180 2021-10-02 op }
1075 efe7d180 2021-10-02 op
1076 efe7d180 2021-10-02 op if (error & EVBUFFER_EOF) {
1077 efe7d180 2021-10-02 op client_close(c);
1078 efe7d180 2021-10-02 op return;
1079 efe7d180 2021-10-02 op }
1080 efe7d180 2021-10-02 op
1081 efe7d180 2021-10-02 op log_err(c, "unknown bufferevent error: %s", strerror(errno));
1082 efe7d180 2021-10-02 op client_close(c);
1083 d3a08f4d 2021-01-17 op }
1084 d3a08f4d 2021-01-17 op
1085 8ad1c570 2021-05-09 op void
1086 efe7d180 2021-10-02 op start_reply(struct client *c, int code, const char *meta)
1087 d3a08f4d 2021-01-17 op {
1088 efe7d180 2021-10-02 op struct evbuffer *evb = EVBUFFER_OUTPUT(c->bev);
1089 efe7d180 2021-10-02 op const char *lang;
1090 efe7d180 2021-10-02 op int r, rr;
1091 efe7d180 2021-10-02 op
1092 efe7d180 2021-10-02 op bufferevent_enable(c->bev, EVBUFFER_WRITE);
1093 efe7d180 2021-10-02 op
1094 efe7d180 2021-10-02 op c->code = code;
1095 efe7d180 2021-10-02 op c->meta = meta;
1096 efe7d180 2021-10-02 op
1097 efe7d180 2021-10-02 op r = evbuffer_add_printf(evb, "%d %s", code, meta);
1098 efe7d180 2021-10-02 op if (r == -1)
1099 efe7d180 2021-10-02 op goto err;
1100 efe7d180 2021-10-02 op
1101 efe7d180 2021-10-02 op /* 2 digit status + space + 1024 max reply */
1102 efe7d180 2021-10-02 op if (r > 1027)
1103 efe7d180 2021-10-02 op goto overflow;
1104 efe7d180 2021-10-02 op
1105 efe7d180 2021-10-02 op if (c->type != REQUEST_CGI &&
1106 efe7d180 2021-10-02 op c->type != REQUEST_FCGI &&
1107 efe7d180 2021-10-02 op !strcmp(meta, "text/gemini") &&
1108 efe7d180 2021-10-02 op (lang = vhost_lang(c->host, c->iri.path)) != NULL) {
1109 efe7d180 2021-10-02 op rr = evbuffer_add_printf(evb, ";lang=%s", lang);
1110 efe7d180 2021-10-02 op if (rr == -1)
1111 efe7d180 2021-10-02 op goto err;
1112 efe7d180 2021-10-02 op if (r + rr > 1027)
1113 efe7d180 2021-10-02 op goto overflow;
1114 efe7d180 2021-10-02 op }
1115 efe7d180 2021-10-02 op
1116 efe7d180 2021-10-02 op bufferevent_write(c->bev, "\r\n", 2);
1117 efe7d180 2021-10-02 op
1118 efe7d180 2021-10-02 op if (!vhost_disable_log(c->host, c->iri.path))
1119 efe7d180 2021-10-02 op log_request(c, EVBUFFER_DATA(evb), EVBUFFER_LENGTH(evb));
1120 efe7d180 2021-10-02 op
1121 efe7d180 2021-10-02 op if (code != 20 && IS_INTERNAL_REQUEST(c->type))
1122 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1123 efe7d180 2021-10-02 op
1124 efe7d180 2021-10-02 op return;
1125 efe7d180 2021-10-02 op
1126 efe7d180 2021-10-02 op err:
1127 efe7d180 2021-10-02 op log_err(c, "evbuffer_add_printf error: no memory");
1128 efe7d180 2021-10-02 op evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1129 efe7d180 2021-10-02 op client_close(c);
1130 efe7d180 2021-10-02 op return;
1131 efe7d180 2021-10-02 op
1132 efe7d180 2021-10-02 op overflow:
1133 efe7d180 2021-10-02 op log_warn(c, "reply header overflow");
1134 efe7d180 2021-10-02 op evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1135 efe7d180 2021-10-02 op start_reply(c, TEMP_FAILURE, "internal error");
1136 efe7d180 2021-10-02 op }
1137 efe7d180 2021-10-02 op
1138 efe7d180 2021-10-02 op static void
1139 efe7d180 2021-10-02 op client_close_ev(int fd, short event, void *d)
1140 efe7d180 2021-10-02 op {
1141 8ad1c570 2021-05-09 op struct client *c = d;
1142 d3a08f4d 2021-01-17 op
1143 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
1144 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1145 efe7d180 2021-10-02 op event_once(c->fd, EV_READ, client_close_ev, c, NULL);
1146 efe7d180 2021-10-02 op break;
1147 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1148 efe7d180 2021-10-02 op event_once(c->fd, EV_WRITE, client_close_ev, c, NULL);
1149 efe7d180 2021-10-02 op break;
1150 d3a08f4d 2021-01-17 op }
1151 d3a08f4d 2021-01-17 op
1152 d3a08f4d 2021-01-17 op connected_clients--;
1153 d3a08f4d 2021-01-17 op
1154 d3a08f4d 2021-01-17 op tls_free(c->ctx);
1155 d3a08f4d 2021-01-17 op c->ctx = NULL;
1156 d3a08f4d 2021-01-17 op
1157 efe7d180 2021-10-02 op free(c->header);
1158 efe7d180 2021-10-02 op
1159 abc007d2 2021-02-08 op if (c->pfd != -1)
1160 abc007d2 2021-02-08 op close(c->pfd);
1161 d3a08f4d 2021-01-17 op
1162 252908e6 2021-01-24 op if (c->dir != NULL)
1163 11c98667 2021-04-25 op free(c->dir);
1164 252908e6 2021-01-24 op
1165 abc007d2 2021-02-08 op close(c->fd);
1166 abc007d2 2021-02-08 op c->fd = -1;
1167 f890c8c5 2021-01-22 op }
1168 f890c8c5 2021-01-22 op
1169 efe7d180 2021-10-02 op void
1170 efe7d180 2021-10-02 op client_close(struct client *c)
1171 efe7d180 2021-10-02 op {
1172 efe7d180 2021-10-02 op /*
1173 efe7d180 2021-10-02 op * We may end up calling client_close in various situations
1174 efe7d180 2021-10-02 op * and for the most unexpected reasons. Therefore, we need to
1175 efe7d180 2021-10-02 op * ensure that everything is properly released once we reach
1176 efe7d180 2021-10-02 op * this point.
1177 efe7d180 2021-10-02 op */
1178 efe7d180 2021-10-02 op
1179 efe7d180 2021-10-02 op if (c->type == REQUEST_FCGI)
1180 efe7d180 2021-10-02 op fcgi_abort_request(c);
1181 efe7d180 2021-10-02 op
1182 efe7d180 2021-10-02 op if (c->cgibev != NULL) {
1183 efe7d180 2021-10-02 op bufferevent_disable(c->cgibev, EVBUFFER_READ|EVBUFFER_WRITE);
1184 efe7d180 2021-10-02 op bufferevent_free(c->cgibev);
1185 efe7d180 2021-10-02 op c->cgibev = NULL;
1186 efe7d180 2021-10-02 op close(c->pfd);
1187 efe7d180 2021-10-02 op c->pfd = -1;
1188 efe7d180 2021-10-02 op }
1189 efe7d180 2021-10-02 op
1190 efe7d180 2021-10-02 op bufferevent_disable(c->bev, EVBUFFER_READ|EVBUFFER_WRITE);
1191 efe7d180 2021-10-02 op bufferevent_free(c->bev);
1192 efe7d180 2021-10-02 op c->bev = NULL;
1193 efe7d180 2021-10-02 op
1194 efe7d180 2021-10-02 op client_close_ev(c->fd, 0, c);
1195 efe7d180 2021-10-02 op }
1196 efe7d180 2021-10-02 op
1197 fe406389 2021-02-02 op static void
1198 efe7d180 2021-10-02 op cgi_read(struct bufferevent *bev, void *d)
1199 efe7d180 2021-10-02 op {
1200 efe7d180 2021-10-02 op struct client *client = d;
1201 efe7d180 2021-10-02 op struct evbuffer *src = EVBUFFER_INPUT(bev);
1202 efe7d180 2021-10-02 op char *header;
1203 efe7d180 2021-10-02 op size_t len;
1204 efe7d180 2021-10-02 op int code;
1205 efe7d180 2021-10-02 op
1206 efe7d180 2021-10-02 op /* intercept the header */
1207 efe7d180 2021-10-02 op if (client->code == 0) {
1208 efe7d180 2021-10-02 op header = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
1209 efe7d180 2021-10-02 op if (header == NULL) {
1210 efe7d180 2021-10-02 op /* max reply + \r\n */
1211 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(src) > 1026) {
1212 efe7d180 2021-10-02 op log_warn(client, "CGI script is trying to "
1213 efe7d180 2021-10-02 op "send a header too long.");
1214 efe7d180 2021-10-02 op cgi_error(bev, EVBUFFER_READ, client);
1215 efe7d180 2021-10-02 op }
1216 efe7d180 2021-10-02 op
1217 efe7d180 2021-10-02 op /* wait a bit */
1218 efe7d180 2021-10-02 op return;
1219 efe7d180 2021-10-02 op }
1220 efe7d180 2021-10-02 op
1221 efe7d180 2021-10-02 op if (len < 3 || len > 1029 ||
1222 efe7d180 2021-10-02 op !isdigit(header[0]) ||
1223 efe7d180 2021-10-02 op !isdigit(header[1]) ||
1224 efe7d180 2021-10-02 op !isspace(header[2])) {
1225 efe7d180 2021-10-02 op free(header);
1226 efe7d180 2021-10-02 op log_warn(client, "CGI script is trying to send a "
1227 efe7d180 2021-10-02 op "malformed header");
1228 efe7d180 2021-10-02 op cgi_error(bev, EVBUFFER_READ, client);
1229 efe7d180 2021-10-02 op return;
1230 efe7d180 2021-10-02 op }
1231 efe7d180 2021-10-02 op
1232 efe7d180 2021-10-02 op client->header = header;
1233 efe7d180 2021-10-02 op code = (header[0] - '0') * 10 + (header[1] - '0');
1234 efe7d180 2021-10-02 op
1235 efe7d180 2021-10-02 op if (code < 10 || code >= 70) {
1236 efe7d180 2021-10-02 op log_warn(client, "CGI script is trying to send an "
1237 efe7d180 2021-10-02 op "invalid reply code (%d)", code);
1238 efe7d180 2021-10-02 op cgi_error(bev, EVBUFFER_READ, client);
1239 efe7d180 2021-10-02 op return;
1240 efe7d180 2021-10-02 op }
1241 efe7d180 2021-10-02 op
1242 efe7d180 2021-10-02 op start_reply(client, code, header + 3);
1243 efe7d180 2021-10-02 op
1244 efe7d180 2021-10-02 op if (client->code < 20 || client->code > 29) {
1245 efe7d180 2021-10-02 op cgi_error(client->cgibev, EVBUFFER_EOF, client);
1246 efe7d180 2021-10-02 op return;
1247 efe7d180 2021-10-02 op }
1248 efe7d180 2021-10-02 op }
1249 efe7d180 2021-10-02 op
1250 efe7d180 2021-10-02 op bufferevent_write_buffer(client->bev, src);
1251 efe7d180 2021-10-02 op }
1252 efe7d180 2021-10-02 op
1253 efe7d180 2021-10-02 op static void
1254 efe7d180 2021-10-02 op cgi_write(struct bufferevent *bev, void *d)
1255 efe7d180 2021-10-02 op {
1256 efe7d180 2021-10-02 op /*
1257 efe7d180 2021-10-02 op * Never called. We don't send data to a CGI script.
1258 efe7d180 2021-10-02 op */
1259 efe7d180 2021-10-02 op abort();
1260 efe7d180 2021-10-02 op }
1261 efe7d180 2021-10-02 op
1262 efe7d180 2021-10-02 op static void
1263 efe7d180 2021-10-02 op cgi_error(struct bufferevent *bev, short error, void *d)
1264 efe7d180 2021-10-02 op {
1265 efe7d180 2021-10-02 op struct client *client = d;
1266 efe7d180 2021-10-02 op
1267 efe7d180 2021-10-02 op if (error & EVBUFFER_ERROR)
1268 efe7d180 2021-10-02 op log_err(client, "%s: evbuffer error (%x): %s",
1269 efe7d180 2021-10-02 op __func__, error, strerror(errno));
1270 efe7d180 2021-10-02 op
1271 efe7d180 2021-10-02 op bufferevent_disable(bev, EVBUFFER_READ|EVBUFFER_WRITE);
1272 efe7d180 2021-10-02 op bufferevent_free(bev);
1273 efe7d180 2021-10-02 op client->cgibev = NULL;
1274 efe7d180 2021-10-02 op
1275 efe7d180 2021-10-02 op close(client->pfd);
1276 efe7d180 2021-10-02 op client->pfd = -1;
1277 efe7d180 2021-10-02 op
1278 efe7d180 2021-10-02 op client->type = REQUEST_DONE;
1279 efe7d180 2021-10-02 op if (client->code != 0)
1280 efe7d180 2021-10-02 op client_write(client->bev, client);
1281 efe7d180 2021-10-02 op else
1282 efe7d180 2021-10-02 op start_reply(client, CGI_ERROR, "CGI error");
1283 efe7d180 2021-10-02 op }
1284 efe7d180 2021-10-02 op
1285 efe7d180 2021-10-02 op static void
1286 abc007d2 2021-02-08 op do_accept(int sock, short et, void *d)
1287 d3a08f4d 2021-01-17 op {
1288 abc007d2 2021-02-08 op struct client *c;
1289 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
1290 abc007d2 2021-02-08 op struct sockaddr *saddr;
1291 d3a08f4d 2021-01-17 op socklen_t len;
1292 abc007d2 2021-02-08 op int i, fd;
1293 d3a08f4d 2021-01-17 op
1294 abc007d2 2021-02-08 op (void)et;
1295 abc007d2 2021-02-08 op
1296 abc007d2 2021-02-08 op saddr = (struct sockaddr*)&addr;
1297 d3a08f4d 2021-01-17 op len = sizeof(addr);
1298 3cb3dd4d 2021-02-12 op if ((fd = accept(sock, saddr, &len)) == -1) {
1299 cfb8a77f 2021-02-07 op if (errno == EWOULDBLOCK || errno == EAGAIN)
1300 d3a08f4d 2021-01-17 op return;
1301 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
1302 d3a08f4d 2021-01-17 op }
1303 d3a08f4d 2021-01-17 op
1304 3cb3dd4d 2021-02-12 op mark_nonblock(fd);
1305 3cb3dd4d 2021-02-12 op
1306 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
1307 bc99d868 2021-03-19 op c = &clients[i];
1308 abc007d2 2021-02-08 op if (c->fd == -1) {
1309 abc007d2 2021-02-08 op memset(c, 0, sizeof(*c));
1310 bc99d868 2021-03-19 op c->id = i;
1311 bc99d868 2021-03-19 op if (tls_accept_socket(ctx, &c->ctx, fd) == -1)
1312 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
1313 d3a08f4d 2021-01-17 op
1314 abc007d2 2021-02-08 op c->fd = fd;
1315 abc007d2 2021-02-08 op c->pfd = -1;
1316 abc007d2 2021-02-08 op c->dir = NULL;
1317 abc007d2 2021-02-08 op c->addr = addr;
1318 8ad1c570 2021-05-09 op c->fcgi = -1;
1319 83fe545a 2021-09-26 op
1320 efe7d180 2021-10-02 op event_once(c->fd, EV_READ|EV_WRITE, handle_handshake,
1321 efe7d180 2021-10-02 op c, NULL);
1322 d3a08f4d 2021-01-17 op
1323 d3a08f4d 2021-01-17 op connected_clients++;
1324 d3a08f4d 2021-01-17 op return;
1325 d3a08f4d 2021-01-17 op }
1326 d3a08f4d 2021-01-17 op }
1327 d3a08f4d 2021-01-17 op
1328 d3a08f4d 2021-01-17 op close(fd);
1329 d3a08f4d 2021-01-17 op }
1330 d3a08f4d 2021-01-17 op
1331 8ad1c570 2021-05-09 op static struct client *
1332 bc99d868 2021-03-19 op client_by_id(int id)
1333 bc99d868 2021-03-19 op {
1334 bc99d868 2021-03-19 op if ((size_t)id > sizeof(clients)/sizeof(clients[0]))
1335 bc99d868 2021-03-19 op fatal("in client_by_id: invalid id %d", id);
1336 bc99d868 2021-03-19 op return &clients[id];
1337 bc99d868 2021-03-19 op }
1338 bc99d868 2021-03-19 op
1339 8ad1c570 2021-05-09 op struct client *
1340 8ad1c570 2021-05-09 op try_client_by_id(int id)
1341 8ad1c570 2021-05-09 op {
1342 8ad1c570 2021-05-09 op if ((size_t)id > sizeof(clients)/sizeof(clients[0]))
1343 8ad1c570 2021-05-09 op return NULL;
1344 8ad1c570 2021-05-09 op return &clients[id];
1345 8ad1c570 2021-05-09 op }
1346 8ad1c570 2021-05-09 op
1347 abc007d2 2021-02-08 op static void
1348 bc99d868 2021-03-19 op handle_imsg_cgi_res(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1349 d3a08f4d 2021-01-17 op {
1350 bc99d868 2021-03-19 op struct client *c;
1351 d3a08f4d 2021-01-17 op
1352 bc99d868 2021-03-19 op c = client_by_id(imsg->hdr.peerid);
1353 bc99d868 2021-03-19 op
1354 efe7d180 2021-10-02 op if ((c->pfd = imsg->fd) == -1) {
1355 bc99d868 2021-03-19 op start_reply(c, TEMP_FAILURE, "internal server error");
1356 efe7d180 2021-10-02 op return;
1357 efe7d180 2021-10-02 op }
1358 efe7d180 2021-10-02 op
1359 efe7d180 2021-10-02 op c->type = REQUEST_CGI;
1360 efe7d180 2021-10-02 op
1361 efe7d180 2021-10-02 op c->cgibev = bufferevent_new(c->pfd, cgi_read, cgi_write,
1362 efe7d180 2021-10-02 op cgi_error, c);
1363 efe7d180 2021-10-02 op
1364 efe7d180 2021-10-02 op bufferevent_enable(c->cgibev, EV_READ);
1365 8ad1c570 2021-05-09 op }
1366 8ad1c570 2021-05-09 op
1367 8ad1c570 2021-05-09 op static void
1368 8ad1c570 2021-05-09 op handle_imsg_fcgi_fd(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1369 8ad1c570 2021-05-09 op {
1370 8ad1c570 2021-05-09 op struct client *c;
1371 8ad1c570 2021-05-09 op struct fcgi *f;
1372 8ad1c570 2021-05-09 op int i, id;
1373 8ad1c570 2021-05-09 op
1374 8ad1c570 2021-05-09 op id = imsg->hdr.peerid;
1375 8ad1c570 2021-05-09 op f = &fcgi[id];
1376 8ad1c570 2021-05-09 op
1377 741b69be 2021-09-26 op if ((f->fd = imsg->fd) == -1)
1378 8ad1c570 2021-05-09 op f->s = FCGI_OFF;
1379 741b69be 2021-09-26 op else {
1380 741b69be 2021-09-26 op mark_nonblock(f->fd);
1381 741b69be 2021-09-26 op
1382 741b69be 2021-09-26 op f->s = FCGI_READY;
1383 741b69be 2021-09-26 op
1384 741b69be 2021-09-26 op f->bev = bufferevent_new(f->fd, fcgi_read, fcgi_write,
1385 741b69be 2021-09-26 op fcgi_error, f);
1386 741b69be 2021-09-26 op if (f->bev == NULL) {
1387 741b69be 2021-09-26 op close(f->fd);
1388 741b69be 2021-09-26 op log_err(NULL, "%s: failed to allocate client buffer",
1389 741b69be 2021-09-26 op __func__);
1390 741b69be 2021-09-26 op f->s = FCGI_OFF;
1391 741b69be 2021-09-26 op }
1392 741b69be 2021-09-26 op
1393 741b69be 2021-09-26 op bufferevent_enable(f->bev, EV_READ|EV_WRITE);
1394 8ad1c570 2021-05-09 op }
1395 8ad1c570 2021-05-09 op
1396 8ad1c570 2021-05-09 op for (i = 0; i < MAX_USERS; ++i) {
1397 8ad1c570 2021-05-09 op c = &clients[i];
1398 8ad1c570 2021-05-09 op if (c->fd == -1)
1399 8ad1c570 2021-05-09 op continue;
1400 8ad1c570 2021-05-09 op if (c->fcgi != id)
1401 8ad1c570 2021-05-09 op continue;
1402 8ad1c570 2021-05-09 op
1403 741b69be 2021-09-26 op if (f->s == FCGI_OFF) {
1404 8ad1c570 2021-05-09 op c->fcgi = -1;
1405 8ad1c570 2021-05-09 op start_reply(c, TEMP_FAILURE, "internal server error");
1406 8ad1c570 2021-05-09 op } else
1407 741b69be 2021-09-26 op fcgi_req(f, c);
1408 8ad1c570 2021-05-09 op }
1409 bc99d868 2021-03-19 op }
1410 bc99d868 2021-03-19 op
1411 bc99d868 2021-03-19 op static void
1412 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1413 bc99d868 2021-03-19 op {
1414 090b8a89 2021-07-06 op size_t i;
1415 090b8a89 2021-07-06 op
1416 bc99d868 2021-03-19 op (void)imsg;
1417 bc99d868 2021-03-19 op (void)len;
1418 bc99d868 2021-03-19 op
1419 a8a1f439 2021-07-07 op /*
1420 a8a1f439 2021-07-07 op * don't call event_loopbreak since we want to finish to
1421 a8a1f439 2021-07-07 op * handle the ongoing connections.
1422 a8a1f439 2021-07-07 op */
1423 bc99d868 2021-03-19 op
1424 090b8a89 2021-07-06 op shutting_down = 1;
1425 090b8a89 2021-07-06 op
1426 a6e689d7 2021-02-12 op event_del(&e4);
1427 a6e689d7 2021-02-12 op if (has_ipv6)
1428 a6e689d7 2021-02-12 op event_del(&e6);
1429 a6e689d7 2021-02-12 op if (has_siginfo)
1430 a6e689d7 2021-02-12 op signal_del(&siginfo);
1431 bc99d868 2021-03-19 op event_del(&imsgev);
1432 a6e689d7 2021-02-12 op signal_del(&sigusr2);
1433 090b8a89 2021-07-06 op
1434 090b8a89 2021-07-06 op for (i = 0; i < FCGI_MAX; ++i) {
1435 090b8a89 2021-07-06 op if (fcgi[i].path == NULL && fcgi[i].prog == NULL)
1436 090b8a89 2021-07-06 op break;
1437 090b8a89 2021-07-06 op
1438 741b69be 2021-09-26 op if (fcgi[i].bev == NULL || fcgi[i].pending != 0)
1439 090b8a89 2021-07-06 op continue;
1440 090b8a89 2021-07-06 op
1441 090b8a89 2021-07-06 op fcgi_close_backend(&fcgi[i]);
1442 090b8a89 2021-07-06 op }
1443 abc007d2 2021-02-08 op }
1444 d3a08f4d 2021-01-17 op
1445 abc007d2 2021-02-08 op static void
1446 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
1447 bc99d868 2021-03-19 op {
1448 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
1449 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
1450 bc99d868 2021-03-19 op }
1451 bc99d868 2021-03-19 op
1452 bc99d868 2021-03-19 op static void
1453 abc007d2 2021-02-08 op handle_siginfo(int fd, short ev, void *d)
1454 abc007d2 2021-02-08 op {
1455 abc007d2 2021-02-08 op (void)fd;
1456 abc007d2 2021-02-08 op (void)ev;
1457 abc007d2 2021-02-08 op (void)d;
1458 d3a08f4d 2021-01-17 op
1459 abc007d2 2021-02-08 op log_info(NULL, "%d connected clients", connected_clients);
1460 abc007d2 2021-02-08 op }
1461 d3a08f4d 2021-01-17 op
1462 abc007d2 2021-02-08 op void
1463 bc99d868 2021-03-19 op loop(struct tls *ctx_, int sock4, int sock6, struct imsgbuf *ibuf)
1464 abc007d2 2021-02-08 op {
1465 abc007d2 2021-02-08 op size_t i;
1466 d3a08f4d 2021-01-17 op
1467 bc99d868 2021-03-19 op ctx = ctx_;
1468 bc99d868 2021-03-19 op
1469 abc007d2 2021-02-08 op event_init();
1470 1e3ef7ab 2021-02-03 op
1471 bc99d868 2021-03-19 op memset(&clients, 0, sizeof(clients));
1472 abc007d2 2021-02-08 op for (i = 0; i < MAX_USERS; ++i)
1473 bc99d868 2021-03-19 op clients[i].fd = -1;
1474 d3a08f4d 2021-01-17 op
1475 bc99d868 2021-03-19 op event_set(&e4, sock4, EV_READ | EV_PERSIST, &do_accept, NULL);
1476 a6e689d7 2021-02-12 op event_add(&e4, NULL);
1477 ca21e100 2021-02-04 op
1478 abc007d2 2021-02-08 op if (sock6 != -1) {
1479 a6e689d7 2021-02-12 op has_ipv6 = 1;
1480 bc99d868 2021-03-19 op event_set(&e6, sock6, EV_READ | EV_PERSIST, &do_accept, NULL);
1481 a6e689d7 2021-02-12 op event_add(&e6, NULL);
1482 d3a08f4d 2021-01-17 op }
1483 abc007d2 2021-02-08 op
1484 bc99d868 2021-03-19 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
1485 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
1486 abc007d2 2021-02-08 op
1487 abc007d2 2021-02-08 op #ifdef SIGINFO
1488 a6e689d7 2021-02-12 op has_siginfo = 1;
1489 a6e689d7 2021-02-12 op signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
1490 a6e689d7 2021-02-12 op signal_add(&siginfo, NULL);
1491 abc007d2 2021-02-08 op #endif
1492 5e3285d5 2021-02-12 op signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
1493 a6e689d7 2021-02-12 op signal_add(&sigusr2, NULL);
1494 abc007d2 2021-02-08 op
1495 62e001b0 2021-03-20 op sandbox_server_process();
1496 abc007d2 2021-02-08 op event_dispatch();
1497 df58efff 2021-02-08 op _exit(0);
1498 d3a08f4d 2021-01-17 op }