2 d3a08f4d 2021-01-17 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 52418c8d 2021-02-12 op #include "gmid.h"
19 d3a08f4d 2021-01-17 op #include <sys/stat.h>
21 d3a08f4d 2021-01-17 op #include <assert.h>
22 d3a08f4d 2021-01-17 op #include <errno.h>
23 abc007d2 2021-02-08 op #include <event.h>
24 d3a08f4d 2021-01-17 op #include <fcntl.h>
25 b4d409cf 2021-01-21 op #include <fnmatch.h>
26 2fafa2d2 2021-02-01 op #include <limits.h>
27 d3a08f4d 2021-01-17 op #include <string.h>
29 bc99d868 2021-03-19 op static struct client clients[MAX_USERS];
30 bc99d868 2021-03-19 op static struct tls *ctx;
32 bc99d868 2021-03-19 op static struct event e4, e6, imsgev, siginfo, sigusr2;
33 6b191ed5 2021-02-23 op static int has_ipv6, has_siginfo;
35 d3a08f4d 2021-01-17 op int connected_clients;
37 49b73ba1 2021-02-10 op static inline int matches(const char*, const char*);
39 abc007d2 2021-02-08 op static inline void reschedule_read(int, struct client*, statefn);
40 abc007d2 2021-02-08 op static inline void reschedule_write(int, struct client*, statefn);
42 fe406389 2021-02-02 op static int check_path(struct client*, const char*, int*);
43 abc007d2 2021-02-08 op static void open_file(struct client*);
44 abc007d2 2021-02-08 op static void check_for_cgi(struct client*);
45 abc007d2 2021-02-08 op static void handle_handshake(int, short, void*);
46 57ec3e77 2021-02-08 op static char *strip_path(char*, int);
47 57ec3e77 2021-02-08 op static void fmt_sbuf(const char*, struct client*, const char*);
48 abc007d2 2021-02-08 op static int apply_block_return(struct client*);
49 02be96c6 2021-02-09 op static int apply_require_ca(struct client*);
50 abc007d2 2021-02-08 op static void handle_open_conn(int, short, void*);
51 abc007d2 2021-02-08 op static void start_reply(struct client*, int, const char*);
52 abc007d2 2021-02-08 op static void handle_start_reply(int, short, void*);
53 b8e64ccd 2021-03-31 op static size_t host_nth(struct vhost*);
54 abc007d2 2021-02-08 op static void start_cgi(const char*, const char*, struct client*);
55 abc007d2 2021-02-08 op static void open_dir(struct client*);
56 abc007d2 2021-02-08 op static void redirect_canonical_dir(struct client*);
57 abc007d2 2021-02-08 op static void enter_handle_dirlist(int, short, void*);
58 abc007d2 2021-02-08 op static void handle_dirlist(int, short, void*);
59 fe406389 2021-02-02 op static int read_next_dir_entry(struct client*);
60 abc007d2 2021-02-08 op static void send_directory_listing(int, short, void*);
61 abc007d2 2021-02-08 op static void handle_cgi_reply(int, short, void*);
62 27b2fa9a 2021-02-12 op static void handle_copy(int, short, void*);
63 abc007d2 2021-02-08 op static void close_conn(int, short, void*);
64 abc007d2 2021-02-08 op static void do_accept(int, short, void*);
65 bc99d868 2021-03-19 op struct client *client_by_id(int);
66 bc99d868 2021-03-19 op static void handle_imsg_cgi_res(struct imsgbuf*, struct imsg*, size_t);
67 bc99d868 2021-03-19 op static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
68 bc99d868 2021-03-19 op static void handle_siginfo(int, short, void*);
70 bc99d868 2021-03-19 op static imsg_handlerfn *handlers[] = {
71 bc99d868 2021-03-19 op [IMSG_QUIT] = handle_imsg_quit,
72 bc99d868 2021-03-19 op [IMSG_CGI_RES] = handle_imsg_cgi_res,
75 49b73ba1 2021-02-10 op static inline int
76 49b73ba1 2021-02-10 op matches(const char *pattern, const char *path)
78 49b73ba1 2021-02-10 op if (*path == '/')
80 49b73ba1 2021-02-10 op return !fnmatch(pattern, path, 0);
83 abc007d2 2021-02-08 op static inline void
84 abc007d2 2021-02-08 op reschedule_read(int fd, struct client *c, statefn fn)
86 abc007d2 2021-02-08 op event_once(fd, EV_READ, fn, c, NULL);
89 c39b26d3 2021-02-12 op static inline void
90 abc007d2 2021-02-08 op reschedule_write(int fd, struct client *c, statefn fn)
92 abc007d2 2021-02-08 op event_once(fd, EV_WRITE, fn, c, NULL);
96 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
98 c8b74339 2021-01-24 op struct location *loc;
100 caad0308 2021-01-27 op if (v == NULL || path == NULL)
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;
111 b8e64ccd 2021-03-31 op return TAILQ_FIRST(&v->locations)->lang;
115 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
117 c8b74339 2021-01-24 op struct location *loc;
118 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
120 caad0308 2021-01-27 op if (v == NULL || path == NULL)
121 caad0308 2021-01-27 op return default_mime;
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;
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;
138 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
140 c8b74339 2021-01-24 op struct location *loc;
141 c8b74339 2021-01-24 op const char *index = "index.gmi";
143 caad0308 2021-01-27 op if (v == NULL || path == NULL)
144 caad0308 2021-01-27 op return index;
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;
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;
161 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
163 252908e6 2021-01-24 op struct location *loc;
165 6016a593 2021-01-30 op if (v == NULL || path == NULL)
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;
176 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
177 b8e64ccd 2021-03-31 op return loc->auto_index == 1;
181 6abda252 2021-02-06 op vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
183 6abda252 2021-02-06 op struct location *loc;
185 6abda252 2021-02-06 op if (v == NULL || path == NULL)
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;
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;
206 6abda252 2021-02-06 op vhost_strip(struct vhost *v, const char *path)
208 6abda252 2021-02-06 op struct location *loc;
210 6abda252 2021-02-06 op if (v == NULL || path == NULL)
213 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
214 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
215 cd761624 2021-02-06 op if (loc->strip != 0) {
216 49b73ba1 2021-02-10 op if (matches(loc->match, path))
217 6abda252 2021-02-06 op return loc->strip;
221 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
222 b8e64ccd 2021-03-31 op return loc->strip;
226 02be96c6 2021-02-09 op vhost_require_ca(struct vhost *v, const char *path)
228 02be96c6 2021-02-09 op struct location *loc;
230 02be96c6 2021-02-09 op if (v == NULL || path == NULL)
233 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
234 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
235 02be96c6 2021-02-09 op if (loc->reqca != NULL) {
236 49b73ba1 2021-02-10 op if (matches(loc->match, path))
237 02be96c6 2021-02-09 op return loc->reqca;
241 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
242 b8e64ccd 2021-03-31 op return loc->reqca;
246 793835cb 2021-02-23 op vhost_disable_log(struct vhost *v, const char *path)
248 793835cb 2021-02-23 op struct location *loc;
250 793835cb 2021-02-23 op if (v == NULL || path == NULL)
253 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
254 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
255 793835cb 2021-02-23 op if (loc->disable_log && matches(loc->match, path))
259 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
260 b8e64ccd 2021-03-31 op return loc->disable_log;
264 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
266 d3a08f4d 2021-01-17 op struct stat sb;
267 d1ca3911 2021-01-21 op const char *p;
270 d3a08f4d 2021-01-17 op assert(path != NULL);
272 d1ca3911 2021-01-21 op if (*path == '\0')
274 d1ca3911 2021-01-21 op else if (*path == '/')
275 d1ca3911 2021-01-21 op /* in send_dir we add an initial / (to be
276 d1ca3911 2021-01-21 op * redirect-friendly), but here we want to skip it */
281 252908e6 2021-01-24 op flags = O_RDONLY | O_NOFOLLOW;
283 252908e6 2021-01-24 op if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
284 d3a08f4d 2021-01-17 op return FILE_MISSING;
286 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
287 3abf91b0 2021-02-07 op log_notice(c, "failed stat for %s: %s", path, strerror(errno));
288 d3a08f4d 2021-01-17 op return FILE_MISSING;
291 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
292 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
294 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
295 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
297 d3a08f4d 2021-01-17 op return FILE_EXISTS;
301 abc007d2 2021-02-08 op open_file(struct client *c)
303 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
304 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
305 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
306 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
310 d3a08f4d 2021-01-17 op /* fallthrough */
312 d3a08f4d 2021-01-17 op case FILE_EXISTS:
313 27b2fa9a 2021-02-12 op c->next = handle_copy;
314 27b2fa9a 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
317 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
321 d3a08f4d 2021-01-17 op case FILE_MISSING:
322 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
323 abc007d2 2021-02-08 op check_for_cgi(c);
326 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
330 d3a08f4d 2021-01-17 op /* unreachable */
336 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
337 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
338 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
339 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
340 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
343 abc007d2 2021-02-08 op check_for_cgi(struct client *c)
345 2fafa2d2 2021-02-01 op char path[PATH_MAX];
348 2fafa2d2 2021-02-01 op strlcpy(path, c->iri.path, sizeof(path));
349 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
351 d3a08f4d 2021-01-17 op while (end > path) {
352 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
353 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
354 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
355 d3a08f4d 2021-01-17 op while (*end != '/')
359 abc007d2 2021-02-08 op switch (check_path(c, path, &c->pfd)) {
360 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
361 abc007d2 2021-02-08 op start_cgi(path, end+1, c);
363 d3a08f4d 2021-01-17 op case FILE_MISSING:
374 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
379 9b8f5ed2 2021-02-03 op mark_nonblock(int fd)
383 9b8f5ed2 2021-02-03 op if ((flags = fcntl(fd, F_GETFL)) == -1)
384 9b8f5ed2 2021-02-03 op fatal("fcntl(F_GETFL): %s", strerror(errno));
385 9b8f5ed2 2021-02-03 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
386 9b8f5ed2 2021-02-03 op fatal("fcntl(F_SETFL): %s", strerror(errno));
390 abc007d2 2021-02-08 op handle_handshake(int fd, short ev, void *d)
392 abc007d2 2021-02-08 op struct client *c = d;
393 d3a08f4d 2021-01-17 op struct vhost *h;
394 d3a08f4d 2021-01-17 op const char *servname;
395 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
397 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
398 d3a08f4d 2021-01-17 op case 0: /* success */
399 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
401 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
402 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_handshake);
404 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
405 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_handshake);
408 d3a08f4d 2021-01-17 op /* unreachable */
412 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
413 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
414 3abf91b0 2021-02-07 op log_info(c, "puny_decode: %s", parse_err);
418 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
419 eecad7a3 2021-02-12 op if (matches(h->domain, c->domain))
423 3abf91b0 2021-02-07 op log_debug(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
424 3abf91b0 2021-02-07 op servname != NULL ? servname : "(null)",
426 b8e64ccd 2021-03-31 op h != NULL ? h->domain : "(null)");
428 b8e64ccd 2021-03-31 op if (h != NULL) {
430 abc007d2 2021-02-08 op handle_open_conn(fd, ev, c);
435 0ab65593 2021-01-21 op if (servname != NULL)
436 0ab65593 2021-01-21 op strncpy(c->req, servname, sizeof(c->req));
438 0ab65593 2021-01-21 op strncpy(c->req, "null", sizeof(c->req));
440 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
443 57ec3e77 2021-02-08 op static char *
444 57ec3e77 2021-02-08 op strip_path(char *path, int strip)
448 6abda252 2021-02-06 op while (strip > 0) {
449 6abda252 2021-02-06 op if ((t = strchr(path, '/')) == NULL) {
450 6abda252 2021-02-06 op path = strchr(path, '\0');
461 57ec3e77 2021-02-08 op fmt_sbuf(const char *fmt, struct client *c, const char *path)
464 57ec3e77 2021-02-08 op char buf[32];
466 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
467 6abda252 2021-02-06 op for (i = 0; *fmt; ++fmt) {
468 6abda252 2021-02-06 op if (i == sizeof(buf)-1 || *fmt == '%') {
469 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
470 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
474 6abda252 2021-02-06 op if (*fmt != '%') {
475 6abda252 2021-02-06 op buf[i++] = *fmt;
479 6abda252 2021-02-06 op switch (*++fmt) {
481 6abda252 2021-02-06 op strlcat(c->sbuf, "%", sizeof(c->sbuf));
484 6abda252 2021-02-06 op strlcat(c->sbuf, path, sizeof(c->sbuf));
487 6abda252 2021-02-06 op strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
490 6abda252 2021-02-06 op snprintf(buf, sizeof(buf), "%d", conf.port);
491 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
492 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
495 6abda252 2021-02-06 op strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
498 6abda252 2021-02-06 op fatal("%s: unknown fmt specifier %c",
499 6abda252 2021-02-06 op __func__, *fmt);
504 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
507 57ec3e77 2021-02-08 op /* 1 if a matching `block return' (and apply it), 0 otherwise */
509 57ec3e77 2021-02-08 op apply_block_return(struct client *c)
511 57ec3e77 2021-02-08 op const char *fmt, *path;
514 57ec3e77 2021-02-08 op if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
517 57ec3e77 2021-02-08 op path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
518 57ec3e77 2021-02-08 op fmt_sbuf(fmt, c, path);
520 abc007d2 2021-02-08 op start_reply(c, code, c->sbuf);
524 02be96c6 2021-02-09 op /* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
526 02be96c6 2021-02-09 op apply_require_ca(struct client *c)
528 02be96c6 2021-02-09 op X509_STORE *store;
529 02be96c6 2021-02-09 op const uint8_t *cert;
532 02be96c6 2021-02-09 op if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
535 02be96c6 2021-02-09 op if (!tls_peer_cert_provided(c->ctx)) {
536 02be96c6 2021-02-09 op start_reply(c, CLIENT_CERT_REQ, "client certificate required");
540 02be96c6 2021-02-09 op cert = tls_peer_cert_chain_pem(c->ctx, &len);
541 02be96c6 2021-02-09 op if (!validate_against_ca(store, cert, len)) {
542 02be96c6 2021-02-09 op start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
550 abc007d2 2021-02-08 op handle_open_conn(int fd, short ev, void *d)
552 abc007d2 2021-02-08 op struct client *c = d;
553 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
554 3300cbe0 2021-01-27 op char decoded[DOMAIN_NAME_LEN];
556 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
557 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
559 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
561 3abf91b0 2021-02-07 op log_err(c, "tls_read: %s", tls_error(c->ctx));
562 abc007d2 2021-02-08 op close_conn(fd, ev, c);
565 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
566 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_open_conn);
569 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
570 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_open_conn);
574 c4f682f8 2021-01-27 op if (!trim_req_iri(c->req, &parse_err)
575 a2fd8013 2021-01-29 op || !parse_iri(c->req, &c->iri, &parse_err)
576 a2fd8013 2021-01-29 op || !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
577 3abf91b0 2021-02-07 op log_info(c, "iri parse error: %s", parse_err);
578 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "invalid request");
582 3300cbe0 2021-01-27 op if (c->iri.port_no != conf.port
583 3300cbe0 2021-01-27 op || strcmp(c->iri.schema, "gemini")
584 3300cbe0 2021-01-27 op || strcmp(decoded, c->domain)) {
585 abc007d2 2021-02-08 op start_reply(c, PROXY_REFUSED, "won't proxy request");
589 02be96c6 2021-02-09 op if (apply_require_ca(c))
592 abc007d2 2021-02-08 op if (apply_block_return(c))
595 e3ddf390 2021-02-06 op if (c->host->entrypoint != NULL) {
596 abc007d2 2021-02-08 op start_cgi(c->host->entrypoint, c->iri.path, c);
600 abc007d2 2021-02-08 op open_file(c);
604 abc007d2 2021-02-08 op start_reply(struct client *c, int code, const char *meta)
606 112802ea 2021-02-01 op c->code = code;
607 112802ea 2021-02-01 op c->meta = meta;
608 abc007d2 2021-02-08 op handle_start_reply(c->fd, 0, c);
612 abc007d2 2021-02-08 op handle_start_reply(int fd, short ev, void *d)
614 abc007d2 2021-02-08 op struct client *c = d;
615 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
616 c8b74339 2021-01-24 op const char *lang;
619 c8b74339 2021-01-24 op lang = vhost_lang(c->host, c->iri.path);
621 112802ea 2021-02-01 op snprintf(buf, sizeof(buf), "%d ", c->code);
622 112802ea 2021-02-01 op strlcat(buf, c->meta, sizeof(buf));
623 112802ea 2021-02-01 op if (!strcmp(c->meta, "text/gemini") && lang != NULL) {
624 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
625 c8b74339 2021-01-24 op strlcat(buf, lang, sizeof(buf));
628 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
629 0be51733 2021-01-20 op assert(len < sizeof(buf));
631 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
633 abc007d2 2021-02-08 op close_conn(fd, ev, c);
635 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
636 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_start_reply);
638 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
639 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_start_reply);
643 4604dc96 2021-02-23 op if (!vhost_disable_log(c->host, c->iri.path))
644 4604dc96 2021-02-23 op log_request(c, buf, sizeof(buf));
646 abc007d2 2021-02-08 op if (c->code != SUCCESS)
647 abc007d2 2021-02-08 op close_conn(fd, ev, c);
649 abc007d2 2021-02-08 op c->next(fd, ev, c);
652 b8e64ccd 2021-03-31 op static size_t
653 b8e64ccd 2021-03-31 op host_nth(struct vhost *h)
655 b8e64ccd 2021-03-31 op struct vhost *v;
656 b8e64ccd 2021-03-31 op size_t i = 0;
658 b8e64ccd 2021-03-31 op TAILQ_FOREACH(v, &hosts, vhosts) {
668 abc007d2 2021-02-08 op start_cgi(const char *spath, const char *relpath, struct client *c)
670 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
671 bc99d868 2021-03-19 op const char *t;
672 bc99d868 2021-03-19 op struct cgireq req;
675 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
676 d3a08f4d 2021-01-17 op addr, sizeof(addr),
678 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
680 bc99d868 2021-03-19 op fatal("getnameinfo failed");
682 bc99d868 2021-03-19 op memset(&req, 0, sizeof(req));
684 bc99d868 2021-03-19 op memcpy(req.buf, c->req, sizeof(req.buf));
686 bc99d868 2021-03-19 op req.iri_schema_off = c->iri.schema - c->req;
687 bc99d868 2021-03-19 op req.iri_host_off = c->iri.host - c->req;
688 bc99d868 2021-03-19 op req.iri_port_off = c->iri.port - c->req;
689 bc99d868 2021-03-19 op req.iri_path_off = c->iri.path - c->req;
690 bc99d868 2021-03-19 op req.iri_query_off = c->iri.query - c->req;
691 bc99d868 2021-03-19 op req.iri_fragment_off = c->iri.fragment - c->req;
693 bc99d868 2021-03-19 op req.iri_portno = c->iri.port_no;
695 bc99d868 2021-03-19 op strlcpy(req.spath, spath, sizeof(req.spath));
696 bc99d868 2021-03-19 op strlcpy(req.relpath, relpath, sizeof(req.relpath));
697 bc99d868 2021-03-19 op strlcpy(req.addr, addr, sizeof(req.addr));
699 bc99d868 2021-03-19 op if ((t = tls_peer_cert_subject(c->ctx)) != NULL)
700 bc99d868 2021-03-19 op strlcpy(req.subject, t, sizeof(req.subject));
701 bc99d868 2021-03-19 op if ((t = tls_peer_cert_issuer(c->ctx)) != NULL)
702 bc99d868 2021-03-19 op strlcpy(req.issuer, t, sizeof(req.issuer));
703 bc99d868 2021-03-19 op if ((t = tls_peer_cert_hash(c->ctx)) != NULL)
704 bc99d868 2021-03-19 op strlcpy(req.hash, t, sizeof(req.hash));
705 89541eee 2021-04-13 op if ((t = tls_conn_version(c->ctx)) != NULL)
706 89541eee 2021-04-13 op strlcpy(req.version, t, sizeof(req.version));
707 89541eee 2021-04-13 op if ((t = tls_conn_cipher(c->ctx)) != NULL)
708 89541eee 2021-04-13 op strlcpy(req.cipher, t, sizeof(req.cipher));
710 89541eee 2021-04-13 op req.cipher_strength = tls_conn_cipher_strength(c->ctx);
711 bc99d868 2021-03-19 op req.notbefore = tls_peer_cert_notbefore(c->ctx);
712 bc99d868 2021-03-19 op req.notafter = tls_peer_cert_notafter(c->ctx);
714 b8e64ccd 2021-03-31 op req.host_off = host_nth(c->host);
716 bc99d868 2021-03-19 op imsg_compose(&exibuf, IMSG_CGI_REQ, c->id, 0, -1, &req, sizeof(req));
717 bc99d868 2021-03-19 op imsg_flush(&exibuf);
719 bc99d868 2021-03-19 op close(c->pfd);
723 abc007d2 2021-02-08 op open_dir(struct client *c)
727 252908e6 2021-01-24 op char *before_file;
729 0be51733 2021-01-20 op len = strlen(c->iri.path);
730 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
731 abc007d2 2021-02-08 op redirect_canonical_dir(c);
735 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
736 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
737 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
738 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
739 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
740 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
741 c8b74339 2021-01-24 op sizeof(c->sbuf));
742 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
743 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
747 252908e6 2021-01-24 op c->iri.path = c->sbuf;
749 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
750 abc007d2 2021-02-08 op dirfd = c->pfd;
753 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
754 252908e6 2021-01-24 op case FILE_EXECUTABLE:
755 eecad7a3 2021-02-12 op if (c->host->cgi != NULL && matches(c->host->cgi, c->iri.path)) {
756 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
760 252908e6 2021-01-24 op /* fallthrough */
762 252908e6 2021-01-24 op case FILE_EXISTS:
763 98ee8406 2021-02-12 op c->next = handle_copy;
764 98ee8406 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
767 252908e6 2021-01-24 op case FILE_DIRECTORY:
768 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
771 252908e6 2021-01-24 op case FILE_MISSING:
772 252908e6 2021-01-24 op *before_file = '\0';
774 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
775 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
779 abc007d2 2021-02-08 op c->pfd = dirfd;
780 5f715ce4 2021-02-02 op c->next = enter_handle_dirlist;
782 abc007d2 2021-02-08 op if ((c->dir = fdopendir(c->pfd)) == NULL) {
783 3abf91b0 2021-02-07 op log_err(c, "fdopendir(%d) (vhost:%s) %s: %s",
784 abc007d2 2021-02-08 op c->pfd, c->host->domain, c->iri.path, strerror(errno));
785 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
790 abc007d2 2021-02-08 op start_reply(c, SUCCESS, "text/gemini");
794 252908e6 2021-01-24 op /* unreachable */
798 252908e6 2021-01-24 op close(dirfd);
802 abc007d2 2021-02-08 op redirect_canonical_dir(struct client *c)
806 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
807 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
808 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
810 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
811 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
815 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
819 abc007d2 2021-02-08 op enter_handle_dirlist(int fd, short ev, void *d)
821 abc007d2 2021-02-08 op struct client *c = d;
822 5f715ce4 2021-02-02 op char b[PATH_MAX];
825 5f715ce4 2021-02-02 op strlcpy(b, c->iri.path, sizeof(b));
826 5f715ce4 2021-02-02 op l = snprintf(c->sbuf, sizeof(c->sbuf),
827 5f715ce4 2021-02-02 op "# Index of %s\n\n", b);
828 5f715ce4 2021-02-02 op if (l >= sizeof(c->sbuf)) {
829 5f715ce4 2021-02-02 op /* this is impossible, given that we have enough space
830 5f715ce4 2021-02-02 op * in c->sbuf to hold the ancilliary string plus the
831 5f715ce4 2021-02-02 op * full path; but it wouldn't read nice without some
832 5f715ce4 2021-02-02 op * error checking, and I'd like to avoid a strlen. */
833 abc007d2 2021-02-08 op close_conn(fd, ev, c);
838 abc007d2 2021-02-08 op handle_dirlist(fd, ev, c);
842 abc007d2 2021-02-08 op handle_dirlist(int fd, short ev, void *d)
844 abc007d2 2021-02-08 op struct client *c = d;
847 5f715ce4 2021-02-02 op while (c->len > 0) {
848 5f715ce4 2021-02-02 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
850 abc007d2 2021-02-08 op close_conn(fd, ev, c);
852 5f715ce4 2021-02-02 op case TLS_WANT_POLLOUT:
853 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_dirlist);
855 5f715ce4 2021-02-02 op case TLS_WANT_POLLIN:
856 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_dirlist);
864 abc007d2 2021-02-08 op send_directory_listing(fd, ev, c);
868 252908e6 2021-01-24 op read_next_dir_entry(struct client *c)
870 252908e6 2021-01-24 op struct dirent *d;
874 252908e6 2021-01-24 op if ((d = readdir(c->dir)) == NULL) {
875 252908e6 2021-01-24 op if (errno != 0)
876 3abf91b0 2021-02-07 op log_err(c, "readdir: %s", strerror(errno));
879 252908e6 2021-01-24 op } while (!strcmp(d->d_name, "."));
881 252908e6 2021-01-24 op /* XXX: url escape */
882 252908e6 2021-01-24 op snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
883 252908e6 2021-01-24 op d->d_name, d->d_name);
884 252908e6 2021-01-24 op c->len = strlen(c->sbuf);
891 abc007d2 2021-02-08 op send_directory_listing(int fd, short ev, void *d)
893 abc007d2 2021-02-08 op struct client *c = d;
897 252908e6 2021-01-24 op if (c->len == 0) {
898 252908e6 2021-01-24 op if (!read_next_dir_entry(c))
902 252908e6 2021-01-24 op while (c->len > 0) {
903 252908e6 2021-01-24 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
907 252908e6 2021-01-24 op case TLS_WANT_POLLOUT:
908 abc007d2 2021-02-08 op reschedule_read(fd, c, &send_directory_listing);
911 252908e6 2021-01-24 op case TLS_WANT_POLLIN:
912 abc007d2 2021-02-08 op reschedule_write(fd, c, &send_directory_listing);
924 abc007d2 2021-02-08 op close_conn(fd, ev, d);
927 35744950 2021-02-01 op /* accumulate the meta line from the cgi script. */
929 abc007d2 2021-02-08 op handle_cgi_reply(int fd, short ev, void *d)
931 abc007d2 2021-02-08 op struct client *c = d;
932 35744950 2021-02-01 op void *buf, *e;
937 35744950 2021-02-01 op buf = c->sbuf + c->len;
938 35744950 2021-02-01 op len = sizeof(c->sbuf) - c->len;
940 abc007d2 2021-02-08 op r = read(c->pfd, buf, len);
941 35744950 2021-02-01 op if (r == 0 || r == -1) {
942 abc007d2 2021-02-08 op start_reply(c, CGI_ERROR, "CGI error");
948 35744950 2021-02-01 op /* TODO: error if the CGI script don't reply correctly */
949 35744950 2021-02-01 op e = strchr(c->sbuf, '\n');
950 35744950 2021-02-01 op if (e != NULL || c->len == sizeof(c->sbuf)) {
951 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
954 27b2fa9a 2021-02-12 op handle_copy(fd, ev, c);
958 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_cgi_reply);
962 27b2fa9a 2021-02-12 op handle_copy(int fd, short ev, void *d)
964 abc007d2 2021-02-08 op struct client *c = d;
968 d3a08f4d 2021-01-17 op while (c->len > 0) {
969 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
973 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
974 27b2fa9a 2021-02-12 op reschedule_write(c->fd, c, &handle_copy);
977 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
978 27b2fa9a 2021-02-12 op reschedule_read(c->fd, c, &handle_copy);
988 27b2fa9a 2021-02-12 op switch (r = read(c->pfd, c->sbuf, sizeof(c->sbuf))) {
992 27b2fa9a 2021-02-12 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
993 27b2fa9a 2021-02-12 op reschedule_read(c->pfd, c, &handle_copy);
1004 abc007d2 2021-02-08 op close_conn(c->fd, ev, d);
1008 abc007d2 2021-02-08 op close_conn(int fd, short ev, void *d)
1010 abc007d2 2021-02-08 op struct client *c = d;
1012 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
1013 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1014 abc007d2 2021-02-08 op reschedule_read(fd, c, &close_conn);
1016 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1017 abc007d2 2021-02-08 op reschedule_read(fd, c, &close_conn);
1021 d3a08f4d 2021-01-17 op connected_clients--;
1023 d3a08f4d 2021-01-17 op tls_free(c->ctx);
1024 d3a08f4d 2021-01-17 op c->ctx = NULL;
1026 abc007d2 2021-02-08 op if (c->pfd != -1)
1027 abc007d2 2021-02-08 op close(c->pfd);
1029 252908e6 2021-01-24 op if (c->dir != NULL)
1030 252908e6 2021-01-24 op closedir(c->dir);
1032 abc007d2 2021-02-08 op close(c->fd);
1037 abc007d2 2021-02-08 op do_accept(int sock, short et, void *d)
1039 abc007d2 2021-02-08 op struct client *c;
1040 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
1041 abc007d2 2021-02-08 op struct sockaddr *saddr;
1042 d3a08f4d 2021-01-17 op socklen_t len;
1047 abc007d2 2021-02-08 op saddr = (struct sockaddr*)&addr;
1048 d3a08f4d 2021-01-17 op len = sizeof(addr);
1049 3cb3dd4d 2021-02-12 op if ((fd = accept(sock, saddr, &len)) == -1) {
1050 cfb8a77f 2021-02-07 op if (errno == EWOULDBLOCK || errno == EAGAIN)
1052 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
1055 3cb3dd4d 2021-02-12 op mark_nonblock(fd);
1057 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
1058 bc99d868 2021-03-19 op c = &clients[i];
1059 abc007d2 2021-02-08 op if (c->fd == -1) {
1060 abc007d2 2021-02-08 op memset(c, 0, sizeof(*c));
1062 bc99d868 2021-03-19 op if (tls_accept_socket(ctx, &c->ctx, fd) == -1)
1063 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
1066 abc007d2 2021-02-08 op c->pfd = -1;
1067 abc007d2 2021-02-08 op c->dir = NULL;
1068 abc007d2 2021-02-08 op c->addr = addr;
1070 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_handshake);
1071 d3a08f4d 2021-01-17 op connected_clients++;
1079 bc99d868 2021-03-19 op struct client *
1080 bc99d868 2021-03-19 op client_by_id(int id)
1082 bc99d868 2021-03-19 op if ((size_t)id > sizeof(clients)/sizeof(clients[0]))
1083 bc99d868 2021-03-19 op fatal("in client_by_id: invalid id %d", id);
1084 bc99d868 2021-03-19 op return &clients[id];
1088 bc99d868 2021-03-19 op handle_imsg_cgi_res(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1090 bc99d868 2021-03-19 op struct client *c;
1092 bc99d868 2021-03-19 op c = client_by_id(imsg->hdr.peerid);
1094 bc99d868 2021-03-19 op if ((c->pfd = imsg->fd) == -1)
1095 bc99d868 2021-03-19 op start_reply(c, TEMP_FAILURE, "internal server error");
1097 bc99d868 2021-03-19 op reschedule_read(c->pfd, c, &handle_cgi_reply);
1101 bc99d868 2021-03-19 op handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t len)
1106 bc99d868 2021-03-19 op /* don't call event_loopbreak since we want to finish to
1107 bc99d868 2021-03-19 op * handle the ongoing connections. */
1109 a6e689d7 2021-02-12 op event_del(&e4);
1110 a6e689d7 2021-02-12 op if (has_ipv6)
1111 a6e689d7 2021-02-12 op event_del(&e6);
1112 a6e689d7 2021-02-12 op if (has_siginfo)
1113 a6e689d7 2021-02-12 op signal_del(&siginfo);
1114 bc99d868 2021-03-19 op event_del(&imsgev);
1115 a6e689d7 2021-02-12 op signal_del(&sigusr2);
1119 bc99d868 2021-03-19 op handle_dispatch_imsg(int fd, short ev, void *d)
1121 bc99d868 2021-03-19 op struct imsgbuf *ibuf = d;
1122 bc99d868 2021-03-19 op dispatch_imsg(ibuf, handlers, sizeof(handlers));
1126 abc007d2 2021-02-08 op handle_siginfo(int fd, short ev, void *d)
1132 abc007d2 2021-02-08 op log_info(NULL, "%d connected clients", connected_clients);
1136 bc99d868 2021-03-19 op loop(struct tls *ctx_, int sock4, int sock6, struct imsgbuf *ibuf)
1142 abc007d2 2021-02-08 op event_init();
1144 bc99d868 2021-03-19 op memset(&clients, 0, sizeof(clients));
1145 abc007d2 2021-02-08 op for (i = 0; i < MAX_USERS; ++i)
1146 bc99d868 2021-03-19 op clients[i].fd = -1;
1148 bc99d868 2021-03-19 op event_set(&e4, sock4, EV_READ | EV_PERSIST, &do_accept, NULL);
1149 a6e689d7 2021-02-12 op event_add(&e4, NULL);
1151 abc007d2 2021-02-08 op if (sock6 != -1) {
1152 a6e689d7 2021-02-12 op has_ipv6 = 1;
1153 bc99d868 2021-03-19 op event_set(&e6, sock6, EV_READ | EV_PERSIST, &do_accept, NULL);
1154 a6e689d7 2021-02-12 op event_add(&e6, NULL);
1157 bc99d868 2021-03-19 op event_set(&imsgev, ibuf->fd, EV_READ | EV_PERSIST, handle_dispatch_imsg, ibuf);
1158 bc99d868 2021-03-19 op event_add(&imsgev, NULL);
1160 abc007d2 2021-02-08 op #ifdef SIGINFO
1161 a6e689d7 2021-02-12 op has_siginfo = 1;
1162 a6e689d7 2021-02-12 op signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
1163 a6e689d7 2021-02-12 op signal_add(&siginfo, NULL);
1165 5e3285d5 2021-02-12 op signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
1166 a6e689d7 2021-02-12 op signal_add(&sigusr2, NULL);
1168 62e001b0 2021-03-20 op sandbox_server_process();
1169 abc007d2 2021-02-08 op event_dispatch();