2 a555e0d6 2022-07-04 op * Copyright (c) 2021, 2022 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>
20 d29a2ee2 2022-09-06 op #include <sys/un.h>
22 d3a08f4d 2021-01-17 op #include <assert.h>
23 efe7d180 2021-10-02 op #include <ctype.h>
24 d3a08f4d 2021-01-17 op #include <errno.h>
25 abc007d2 2021-02-08 op #include <event.h>
26 d3a08f4d 2021-01-17 op #include <fcntl.h>
27 b4d409cf 2021-01-21 op #include <fnmatch.h>
28 2fafa2d2 2021-02-01 op #include <limits.h>
29 d3a08f4d 2021-01-17 op #include <string.h>
31 eae52ad4 2023-06-06 op #include "log.h"
32 c26f2460 2023-06-08 op #include "proc.h"
34 efe7d180 2021-10-02 op #define MIN(a, b) ((a) < (b) ? (a) : (b))
36 c26f2460 2023-06-08 op #ifndef nitems
37 c26f2460 2023-06-08 op #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
40 090b8a89 2021-07-06 op int shutting_down;
42 bc99d868 2021-03-19 op static struct tls *ctx;
44 237095fd 2023-06-11 op #ifdef SIGINFO
45 237095fd 2023-06-11 op static struct event siginfo;
47 237095fd 2023-06-11 op static struct event sigusr2;
49 d3a08f4d 2021-01-17 op int connected_clients;
52 86693a33 2023-06-11 op * This function is not publicy exported because it is a hack until libtls
53 86693a33 2023-06-11 op * has a proper privsep setup.
55 86693a33 2023-06-11 op void tls_config_use_fake_private_key(struct tls_config *);
57 49b73ba1 2021-02-10 op static inline int matches(const char*, const char*);
59 fe406389 2021-02-02 op static int check_path(struct client*, const char*, int*);
60 abc007d2 2021-02-08 op static void open_file(struct client*);
61 abc007d2 2021-02-08 op static void handle_handshake(int, short, void*);
62 91b9f2a8 2021-05-15 op static const char *strip_path(const char*, int);
63 57ec3e77 2021-02-08 op static void fmt_sbuf(const char*, struct client*, const char*);
64 abc007d2 2021-02-08 op static int apply_block_return(struct client*);
65 d474a979 2022-01-04 op static int check_matching_certificate(X509_STORE *, struct client *);
66 72b033ef 2021-12-29 op static int apply_reverse_proxy(struct client *);
67 efe7d180 2021-10-02 op static int apply_fastcgi(struct client*);
68 02be96c6 2021-02-09 op static int apply_require_ca(struct client*);
69 abc007d2 2021-02-08 op static void open_dir(struct client*);
70 abc007d2 2021-02-08 op static void redirect_canonical_dir(struct client*);
72 efe7d180 2021-10-02 op static void client_tls_readcb(int, short, void *);
73 efe7d180 2021-10-02 op static void client_tls_writecb(int, short, void *);
75 efe7d180 2021-10-02 op static void client_read(struct bufferevent *, void *);
76 efe7d180 2021-10-02 op void client_write(struct bufferevent *, void *);
77 efe7d180 2021-10-02 op static void client_error(struct bufferevent *, short, void *);
79 efe7d180 2021-10-02 op static void client_close_ev(int, short, void *);
81 bc99d868 2021-03-19 op static void handle_siginfo(int, short, void*);
83 c26f2460 2023-06-08 op static int server_dispatch_parent(int, struct privsep_proc *, struct imsg *);
84 86693a33 2023-06-11 op static int server_dispatch_crypto(int, struct privsep_proc *, struct imsg *);
85 c26f2460 2023-06-08 op static int server_dispatch_logger(int, struct privsep_proc *, struct imsg *);
87 c26f2460 2023-06-08 op static struct privsep_proc procs[] = {
88 c26f2460 2023-06-08 op { "parent", PROC_PARENT, server_dispatch_parent },
89 86693a33 2023-06-11 op { "crypto", PROC_CRYPTO, server_dispatch_crypto },
90 c26f2460 2023-06-08 op { "logger", PROC_LOGGER, server_dispatch_logger },
93 207b3e80 2021-10-07 op static uint32_t server_client_id;
95 207b3e80 2021-10-07 op struct client_tree_id clients;
97 49b73ba1 2021-02-10 op static inline int
98 49b73ba1 2021-02-10 op matches(const char *pattern, const char *path)
100 49b73ba1 2021-02-10 op if (*path == '/')
102 49b73ba1 2021-02-10 op return !fnmatch(pattern, path, 0);
106 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
108 c8b74339 2021-01-24 op struct location *loc;
110 caad0308 2021-01-27 op if (v == NULL || path == NULL)
113 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
114 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
115 534afd0d 2022-10-05 op if (*loc->lang != '\0') {
116 49b73ba1 2021-02-10 op if (matches(loc->match, path))
117 6016a593 2021-01-30 op return loc->lang;
121 534afd0d 2022-10-05 op loc = TAILQ_FIRST(&v->locations);
122 534afd0d 2022-10-05 op if (*loc->lang == '\0')
124 534afd0d 2022-10-05 op return loc->lang;
128 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
130 c8b74339 2021-01-24 op struct location *loc;
131 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
133 caad0308 2021-01-27 op if (v == NULL || path == NULL)
134 caad0308 2021-01-27 op return default_mime;
136 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
137 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
138 534afd0d 2022-10-05 op if (*loc->default_mime != '\0') {
139 49b73ba1 2021-02-10 op if (matches(loc->match, path))
140 6016a593 2021-01-30 op return loc->default_mime;
144 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
145 534afd0d 2022-10-05 op if (*loc->default_mime != '\0')
146 b8e64ccd 2021-03-31 op return loc->default_mime;
147 c8b74339 2021-01-24 op return default_mime;
151 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
153 c8b74339 2021-01-24 op struct location *loc;
154 c8b74339 2021-01-24 op const char *index = "index.gmi";
156 caad0308 2021-01-27 op if (v == NULL || path == NULL)
157 caad0308 2021-01-27 op return index;
159 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
160 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
161 534afd0d 2022-10-05 op if (*loc->index != '\0') {
162 49b73ba1 2021-02-10 op if (matches(loc->match, path))
163 6016a593 2021-01-30 op return loc->index;
167 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
168 534afd0d 2022-10-05 op if (*loc->index != '\0')
169 b8e64ccd 2021-03-31 op return loc->index;
170 c8b74339 2021-01-24 op return index;
174 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
176 252908e6 2021-01-24 op struct location *loc;
178 6016a593 2021-01-30 op if (v == NULL || path == NULL)
181 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
182 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
183 cd761624 2021-02-06 op if (loc->auto_index != 0) {
184 49b73ba1 2021-02-10 op if (matches(loc->match, path))
185 6016a593 2021-01-30 op return loc->auto_index == 1;
189 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
190 b8e64ccd 2021-03-31 op return loc->auto_index == 1;
194 6abda252 2021-02-06 op vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
196 6abda252 2021-02-06 op struct location *loc;
198 6abda252 2021-02-06 op if (v == NULL || path == NULL)
201 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
202 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
203 cd761624 2021-02-06 op if (loc->block_code != 0) {
204 49b73ba1 2021-02-10 op if (matches(loc->match, path)) {
205 6abda252 2021-02-06 op *code = loc->block_code;
206 6abda252 2021-02-06 op *fmt = loc->block_fmt;
212 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
213 b8e64ccd 2021-03-31 op *code = loc->block_code;
214 b8e64ccd 2021-03-31 op *fmt = loc->block_fmt;
215 b8e64ccd 2021-03-31 op return loc->block_code != 0;
219 8ad1c570 2021-05-09 op vhost_fastcgi(struct vhost *v, const char *path)
221 8ad1c570 2021-05-09 op struct location *loc;
223 8ad1c570 2021-05-09 op if (v == NULL || path == NULL)
226 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
227 8ad1c570 2021-05-09 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
228 8ad1c570 2021-05-09 op if (loc->fcgi != -1)
229 8ad1c570 2021-05-09 op if (matches(loc->match, path))
230 8ad1c570 2021-05-09 op return loc->fcgi;
233 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
234 8ad1c570 2021-05-09 op return loc->fcgi;
238 1feaf2a6 2021-05-15 op vhost_dirfd(struct vhost *v, const char *path, size_t *retloc)
240 fdea6aa0 2021-04-30 op struct location *loc;
241 1feaf2a6 2021-05-15 op size_t l = 0;
243 fdea6aa0 2021-04-30 op if (v == NULL || path == NULL)
246 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
247 fdea6aa0 2021-04-30 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
249 fdea6aa0 2021-04-30 op if (loc->dirfd != -1)
250 1feaf2a6 2021-05-15 op if (matches(loc->match, path)) {
252 fdea6aa0 2021-04-30 op return loc->dirfd;
257 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
258 fdea6aa0 2021-04-30 op return loc->dirfd;
262 6abda252 2021-02-06 op vhost_strip(struct vhost *v, const char *path)
264 6abda252 2021-02-06 op struct location *loc;
266 6abda252 2021-02-06 op if (v == NULL || path == NULL)
269 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
270 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
271 cd761624 2021-02-06 op if (loc->strip != 0) {
272 49b73ba1 2021-02-10 op if (matches(loc->match, path))
273 6abda252 2021-02-06 op return loc->strip;
277 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
278 b8e64ccd 2021-03-31 op return loc->strip;
282 02be96c6 2021-02-09 op vhost_require_ca(struct vhost *v, const char *path)
284 02be96c6 2021-02-09 op struct location *loc;
286 02be96c6 2021-02-09 op if (v == NULL || path == NULL)
289 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
290 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
291 02be96c6 2021-02-09 op if (loc->reqca != NULL) {
292 49b73ba1 2021-02-10 op if (matches(loc->match, path))
293 02be96c6 2021-02-09 op return loc->reqca;
297 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
298 b8e64ccd 2021-03-31 op return loc->reqca;
302 793835cb 2021-02-23 op vhost_disable_log(struct vhost *v, const char *path)
304 793835cb 2021-02-23 op struct location *loc;
306 793835cb 2021-02-23 op if (v == NULL || path == NULL)
309 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
310 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
311 793835cb 2021-02-23 op if (loc->disable_log && matches(loc->match, path))
315 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
316 b8e64ccd 2021-03-31 op return loc->disable_log;
320 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
322 d3a08f4d 2021-01-17 op struct stat sb;
323 d1ca3911 2021-01-21 op const char *p;
324 efb48052 2021-07-27 op int dirfd, strip;
326 d3a08f4d 2021-01-17 op assert(path != NULL);
329 fdea6aa0 2021-04-30 op * in send_dir we add an initial / (to be redirect-friendly),
330 fdea6aa0 2021-04-30 op * but here we want to skip it
332 fdea6aa0 2021-04-30 op if (*path == '/')
335 fdea6aa0 2021-04-30 op strip = vhost_strip(c->host, path);
336 fdea6aa0 2021-04-30 op p = strip_path(path, strip);
338 fdea6aa0 2021-04-30 op if (*p == '/')
340 fdea6aa0 2021-04-30 op if (*p == '\0')
343 1feaf2a6 2021-05-15 op dirfd = vhost_dirfd(c->host, path, &c->loc);
344 eae52ad4 2023-06-06 op log_debug("check_path: strip=%d path=%s original=%s",
345 fdea6aa0 2021-04-30 op strip, p, path);
346 3bd4a6de 2022-07-04 op if (*fd == -1 && (*fd = openat(dirfd, p, O_RDONLY)) == -1) {
347 3bd4a6de 2022-07-04 op if (errno == EACCES)
348 eae52ad4 2023-06-06 op log_info("can't open %s: %s", p, strerror(errno));
349 d3a08f4d 2021-01-17 op return FILE_MISSING;
352 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
353 eae52ad4 2023-06-06 op log_warn("fstat %s", path);
354 d3a08f4d 2021-01-17 op return FILE_MISSING;
357 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
358 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
360 d3a08f4d 2021-01-17 op return FILE_EXISTS;
364 abc007d2 2021-02-08 op open_file(struct client *c)
366 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
367 d3a08f4d 2021-01-17 op case FILE_EXISTS:
368 efe7d180 2021-10-02 op c->type = REQUEST_FILE;
369 af1dab18 2023-06-09 op start_reply(c, SUCCESS, mime(c->conf, c->host, c->iri.path));
372 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
376 d3a08f4d 2021-01-17 op case FILE_MISSING:
377 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
381 d3a08f4d 2021-01-17 op /* unreachable */
387 9b8f5ed2 2021-02-03 op mark_nonblock(int fd)
391 9b8f5ed2 2021-02-03 op if ((flags = fcntl(fd, F_GETFL)) == -1)
392 df5058c9 2023-06-05 op fatal("fcntl(F_GETFL)");
393 9b8f5ed2 2021-02-03 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
394 df5058c9 2023-06-05 op fatal("fcntl(F_SETFL)");
398 abc007d2 2021-02-08 op handle_handshake(int fd, short ev, void *d)
400 abc007d2 2021-02-08 op struct client *c = d;
401 af1dab18 2023-06-09 op struct conf *conf = c->conf;
402 d3a08f4d 2021-01-17 op struct vhost *h;
403 cc8c2901 2021-04-29 op struct alist *a;
404 d3a08f4d 2021-01-17 op const char *servname;
405 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
407 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
408 d3a08f4d 2021-01-17 op case 0: /* success */
409 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
411 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
412 efe7d180 2021-10-02 op event_once(c->fd, EV_READ, handle_handshake, c, NULL);
414 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
415 efe7d180 2021-10-02 op event_once(c->fd, EV_WRITE, handle_handshake, c, NULL);
418 d3a08f4d 2021-01-17 op /* unreachable */
422 80444938 2021-10-15 op c->bev = bufferevent_new(fd, client_read, client_write,
423 80444938 2021-10-15 op client_error, c);
424 80444938 2021-10-15 op if (c->bev == NULL)
425 df5058c9 2023-06-05 op fatal("%s: failed to allocate client buffer", __func__);
427 80444938 2021-10-15 op event_set(&c->bev->ev_read, c->fd, EV_READ,
428 80444938 2021-10-15 op client_tls_readcb, c->bev);
429 80444938 2021-10-15 op event_set(&c->bev->ev_write, c->fd, EV_WRITE,
430 80444938 2021-10-15 op client_tls_writecb, c->bev);
432 80444938 2021-10-15 op #if HAVE_LIBEVENT2
433 80444938 2021-10-15 op evbuffer_unfreeze(c->bev->input, 0);
434 80444938 2021-10-15 op evbuffer_unfreeze(c->bev->output, 1);
437 79288c8b 2021-09-24 op if ((servname = tls_conn_servername(c->ctx)) == NULL) {
438 eae52ad4 2023-06-06 op log_debug("handshake: missing SNI");
442 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
443 eae52ad4 2023-06-06 op log_info("puny_decode: %s", parse_err);
447 af1dab18 2023-06-09 op TAILQ_FOREACH(h, &conf->hosts, vhosts) {
448 eecad7a3 2021-02-12 op if (matches(h->domain, c->domain))
450 cc8c2901 2021-04-29 op TAILQ_FOREACH(a, &h->aliases, aliases) {
451 cc8c2901 2021-04-29 op if (matches(a->alias, c->domain))
457 eae52ad4 2023-06-06 op log_debug("handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
458 3abf91b0 2021-02-07 op servname != NULL ? servname : "(null)",
460 b8e64ccd 2021-03-31 op h != NULL ? h->domain : "(null)");
462 b8e64ccd 2021-03-31 op if (h != NULL) {
464 efe7d180 2021-10-02 op bufferevent_enable(c->bev, EV_READ);
469 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
472 91b9f2a8 2021-05-15 op static const char *
473 91b9f2a8 2021-05-15 op strip_path(const char *path, int strip)
477 6abda252 2021-02-06 op while (strip > 0) {
478 6abda252 2021-02-06 op if ((t = strchr(path, '/')) == NULL) {
479 6abda252 2021-02-06 op path = strchr(path, '\0');
490 57ec3e77 2021-02-08 op fmt_sbuf(const char *fmt, struct client *c, const char *path)
492 af1dab18 2023-06-09 op struct conf *conf = c->conf;
494 4842c72d 2021-10-18 op char buf[32];
496 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
497 6abda252 2021-02-06 op for (i = 0; *fmt; ++fmt) {
498 6abda252 2021-02-06 op if (i == sizeof(buf)-1 || *fmt == '%') {
499 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
500 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
504 6abda252 2021-02-06 op if (*fmt != '%') {
505 6abda252 2021-02-06 op buf[i++] = *fmt;
509 6abda252 2021-02-06 op switch (*++fmt) {
511 6abda252 2021-02-06 op strlcat(c->sbuf, "%", sizeof(c->sbuf));
514 737a6b50 2021-04-30 op if (*path != '/')
515 737a6b50 2021-04-30 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
516 6abda252 2021-02-06 op strlcat(c->sbuf, path, sizeof(c->sbuf));
519 6abda252 2021-02-06 op strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
522 af1dab18 2023-06-09 op snprintf(buf, sizeof(buf), "%d", conf->port);
523 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
524 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
527 6abda252 2021-02-06 op strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
530 df5058c9 2023-06-05 op fatalx("%s: unknown fmt specifier %c",
531 6abda252 2021-02-06 op __func__, *fmt);
536 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
539 57ec3e77 2021-02-08 op /* 1 if a matching `block return' (and apply it), 0 otherwise */
541 57ec3e77 2021-02-08 op apply_block_return(struct client *c)
543 57ec3e77 2021-02-08 op const char *fmt, *path;
546 57ec3e77 2021-02-08 op if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
549 57ec3e77 2021-02-08 op path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
550 57ec3e77 2021-02-08 op fmt_sbuf(fmt, c, path);
552 abc007d2 2021-02-08 op start_reply(c, code, c->sbuf);
556 b7967bc1 2021-01-02 op static struct proxy *
557 b7967bc1 2021-01-02 op matched_proxy(struct client *c)
559 b7967bc1 2021-01-02 op struct proxy *p;
560 b7967bc1 2021-01-02 op const char *proto;
561 b7967bc1 2021-01-02 op const char *host;
562 b7967bc1 2021-01-02 op const char *port;
564 b7967bc1 2021-01-02 op TAILQ_FOREACH(p, &c->host->proxies, proxies) {
565 534afd0d 2022-10-05 op if (*(proto = p->match_proto) == '\0')
566 b7967bc1 2021-01-02 op proto = "gemini";
567 534afd0d 2022-10-05 op if (*(host = p->match_host) == '\0')
569 534afd0d 2022-10-05 op if (*(port = p->match_port) == '\0')
572 b7967bc1 2021-01-02 op if (matches(proto, c->iri.schema) &&
573 b7967bc1 2021-01-02 op matches(host, c->domain) &&
574 b7967bc1 2021-01-02 op matches(port, c->iri.port))
582 ba94a608 2022-01-04 op check_matching_certificate(X509_STORE *store, struct client *c)
584 ba94a608 2022-01-04 op const uint8_t *cert;
587 ba94a608 2022-01-04 op if (!tls_peer_cert_provided(c->ctx)) {
588 ba94a608 2022-01-04 op start_reply(c, CLIENT_CERT_REQ, "client certificate required");
592 ba94a608 2022-01-04 op cert = tls_peer_cert_chain_pem(c->ctx, &len);
593 ba94a608 2022-01-04 op if (!validate_against_ca(store, cert, len)) {
594 ba94a608 2022-01-04 op start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
602 d29a2ee2 2022-09-06 op proxy_socket(struct client *c, const char *host, const char *port)
604 d29a2ee2 2022-09-06 op struct addrinfo hints, *res, *res0;
605 eae52ad4 2023-06-06 op int r, sock, save_errno;
606 eae52ad4 2023-06-06 op const char *cause = NULL;
608 d29a2ee2 2022-09-06 op memset(&hints, 0, sizeof(hints));
609 d29a2ee2 2022-09-06 op hints.ai_family = AF_UNSPEC;
610 d29a2ee2 2022-09-06 op hints.ai_socktype = SOCK_STREAM;
612 d29a2ee2 2022-09-06 op /* XXX: asr_run? :> */
613 d29a2ee2 2022-09-06 op r = getaddrinfo(host, port, &hints, &res0);
614 d29a2ee2 2022-09-06 op if (r != 0) {
615 eae52ad4 2023-06-06 op log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
616 d29a2ee2 2022-09-06 op host, port, gai_strerror(r));
620 d29a2ee2 2022-09-06 op for (res = res0; res; res = res->ai_next) {
621 d29a2ee2 2022-09-06 op sock = socket(res->ai_family, res->ai_socktype,
622 d29a2ee2 2022-09-06 op res->ai_protocol);
623 eae52ad4 2023-06-06 op if (sock == -1) {
624 eae52ad4 2023-06-06 op cause = "socket";
628 d29a2ee2 2022-09-06 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
629 eae52ad4 2023-06-06 op cause = "connect";
630 eae52ad4 2023-06-06 op save_errno = errno;
632 eae52ad4 2023-06-06 op errno = save_errno;
640 d29a2ee2 2022-09-06 op if (sock == -1)
641 eae52ad4 2023-06-06 op log_warn("can't connect to %s:%s: %s", host, port, cause);
643 eae52ad4 2023-06-06 op freeaddrinfo(res0);
648 72b033ef 2021-12-29 op /* 1 if matching a proxy relay-to (and apply it), 0 otherwise */
650 72b033ef 2021-12-29 op apply_reverse_proxy(struct client *c)
652 7bdcc91e 2021-01-01 op struct proxy *p;
654 b7967bc1 2021-01-02 op if ((p = matched_proxy(c)) == NULL)
657 b7967bc1 2021-01-02 op c->proxy = p;
659 ba94a608 2022-01-04 op if (p->reqca != NULL && check_matching_certificate(p->reqca, c))
662 eae52ad4 2023-06-06 op log_debug("opening proxy connection for %s:%s",
663 7bdcc91e 2021-01-01 op p->host, p->port);
665 d29a2ee2 2022-09-06 op if ((c->pfd = proxy_socket(c, p->host, p->port)) == -1) {
666 d29a2ee2 2022-09-06 op start_reply(c, PROXY_ERROR, "proxy error");
670 d29a2ee2 2022-09-06 op mark_nonblock(c->pfd);
671 d29a2ee2 2022-09-06 op if (proxy_init(c) == -1)
672 d29a2ee2 2022-09-06 op start_reply(c, PROXY_ERROR, "proxy error");
678 d29a2ee2 2022-09-06 op fcgi_open_sock(struct fcgi *f)
680 d29a2ee2 2022-09-06 op struct sockaddr_un addr;
683 d29a2ee2 2022-09-06 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
684 eae52ad4 2023-06-06 op log_warn("socket");
688 d29a2ee2 2022-09-06 op memset(&addr, 0, sizeof(addr));
689 d29a2ee2 2022-09-06 op addr.sun_family = AF_UNIX;
690 d29a2ee2 2022-09-06 op strlcpy(addr.sun_path, f->path, sizeof(addr.sun_path));
692 d29a2ee2 2022-09-06 op if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
693 eae52ad4 2023-06-06 op log_warn("failed to connect to %s", f->path);
702 d29a2ee2 2022-09-06 op fcgi_open_conn(struct fcgi *f)
704 eae52ad4 2023-06-06 op struct addrinfo hints, *servinfo, *p;
705 eae52ad4 2023-06-06 op int r, sock, save_errno;
706 eae52ad4 2023-06-06 op const char *cause = NULL;
708 d29a2ee2 2022-09-06 op memset(&hints, 0, sizeof(hints));
709 d29a2ee2 2022-09-06 op hints.ai_family = AF_UNSPEC;
710 d29a2ee2 2022-09-06 op hints.ai_socktype = SOCK_STREAM;
711 d29a2ee2 2022-09-06 op hints.ai_flags = AI_ADDRCONFIG;
713 d29a2ee2 2022-09-06 op if ((r = getaddrinfo(f->path, f->port, &hints, &servinfo)) != 0) {
714 eae52ad4 2023-06-06 op log_warnx("getaddrinfo %s:%s: %s", f->path, f->port,
715 d29a2ee2 2022-09-06 op gai_strerror(r));
719 d29a2ee2 2022-09-06 op for (p = servinfo; p != NULL; p = p->ai_next) {
720 d29a2ee2 2022-09-06 op sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
721 eae52ad4 2023-06-06 op if (sock == -1) {
722 eae52ad4 2023-06-06 op cause = "socket";
725 d29a2ee2 2022-09-06 op if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
726 eae52ad4 2023-06-06 op cause = "connect";
727 eae52ad4 2023-06-06 op save_errno = errno;
729 eae52ad4 2023-06-06 op errno = save_errno;
735 d29a2ee2 2022-09-06 op if (p == NULL) {
736 eae52ad4 2023-06-06 op log_warn("couldn't connect to %s:%s: %s", f->path, f->port,
741 d29a2ee2 2022-09-06 op freeaddrinfo(servinfo);
745 8ad1c570 2021-05-09 op /* 1 if matching `fcgi' (and apply it), 0 otherwise */
747 8ad1c570 2021-05-09 op apply_fastcgi(struct client *c)
749 5d22294a 2023-06-09 op int id, i = 0;
750 8ad1c570 2021-05-09 op struct fcgi *f;
752 8ad1c570 2021-05-09 op if ((id = vhost_fastcgi(c->host, c->iri.path)) == -1)
755 af1dab18 2023-06-09 op TAILQ_FOREACH(f, &c->conf->fcgi, fcgi) {
761 5d22294a 2023-06-09 op if (f == NULL) {
762 5d22294a 2023-06-09 op log_warnx("can't find fcgi #%d", id);
766 eae52ad4 2023-06-06 op log_debug("opening fastcgi connection for (%s,%s)",
767 d29a2ee2 2022-09-06 op f->path, f->port);
769 eb4f96c1 2022-11-27 op if (*f->port == '\0')
770 d29a2ee2 2022-09-06 op c->pfd = fcgi_open_sock(f);
772 d29a2ee2 2022-09-06 op c->pfd = fcgi_open_conn(f);
774 d29a2ee2 2022-09-06 op if (c->pfd == -1) {
775 d29a2ee2 2022-09-06 op start_reply(c, CGI_ERROR, "CGI error");
779 d29a2ee2 2022-09-06 op mark_nonblock(c->pfd);
781 d29a2ee2 2022-09-06 op c->cgibev = bufferevent_new(c->pfd, fcgi_read, fcgi_write,
782 d29a2ee2 2022-09-06 op fcgi_error, c);
783 d29a2ee2 2022-09-06 op if (c->cgibev == NULL) {
784 d29a2ee2 2022-09-06 op start_reply(c, TEMP_FAILURE, "internal server error");
788 d29a2ee2 2022-09-06 op bufferevent_enable(c->cgibev, EV_READ|EV_WRITE);
794 02be96c6 2021-02-09 op /* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
796 02be96c6 2021-02-09 op apply_require_ca(struct client *c)
798 02be96c6 2021-02-09 op X509_STORE *store;
800 02be96c6 2021-02-09 op if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
802 ba94a608 2022-01-04 op return check_matching_certificate(store, c);
806 abc007d2 2021-02-08 op open_dir(struct client *c)
809 11c98667 2021-04-25 op int dirfd, root;
810 252908e6 2021-01-24 op char *before_file;
812 11c98667 2021-04-25 op root = !strcmp(c->iri.path, "/") || *c->iri.path == '\0';
814 0be51733 2021-01-20 op len = strlen(c->iri.path);
815 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
816 abc007d2 2021-02-08 op redirect_canonical_dir(c);
820 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
821 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
822 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
823 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
824 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
825 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
826 c8b74339 2021-01-24 op sizeof(c->sbuf));
827 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
828 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
832 252908e6 2021-01-24 op c->iri.path = c->sbuf;
834 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
835 abc007d2 2021-02-08 op dirfd = c->pfd;
838 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
839 252908e6 2021-01-24 op case FILE_EXISTS:
840 efe7d180 2021-10-02 op c->type = REQUEST_FILE;
841 af1dab18 2023-06-09 op start_reply(c, SUCCESS, mime(c->conf, c->host, c->iri.path));
844 252908e6 2021-01-24 op case FILE_DIRECTORY:
845 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
848 252908e6 2021-01-24 op case FILE_MISSING:
849 252908e6 2021-01-24 op *before_file = '\0';
851 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
852 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
856 efe7d180 2021-10-02 op c->type = REQUEST_DIR;
858 e76f2c74 2021-04-25 op c->dirlen = scandir_fd(dirfd, &c->dir,
859 11c98667 2021-04-25 op root ? select_non_dotdot : select_non_dot,
861 11c98667 2021-04-25 op if (c->dirlen == -1) {
862 eae52ad4 2023-06-06 op log_warn("scandir_fd(%d) (vhost:%s) %s",
863 eae52ad4 2023-06-06 op c->pfd, c->host->domain, c->iri.path);
864 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
867 11c98667 2021-04-25 op c->diroff = 0;
870 4842c72d 2021-10-18 op start_reply(c, SUCCESS, "text/gemini");
871 efe7d180 2021-10-02 op evbuffer_add_printf(EVBUFFER_OUTPUT(c->bev),
872 efe7d180 2021-10-02 op "# Index of %s\n\n", c->iri.path);
876 252908e6 2021-01-24 op /* unreachable */
880 252908e6 2021-01-24 op close(dirfd);
884 abc007d2 2021-02-08 op redirect_canonical_dir(struct client *c)
888 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
889 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
890 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
892 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
893 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
897 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
901 efe7d180 2021-10-02 op client_tls_readcb(int fd, short event, void *d)
903 efe7d180 2021-10-02 op struct bufferevent *bufev = d;
904 efe7d180 2021-10-02 op struct client *client = bufev->cbarg;
907 efe7d180 2021-10-02 op int what = EVBUFFER_READ;
908 efe7d180 2021-10-02 op int howmuch = IBUF_READ_SIZE;
909 efe7d180 2021-10-02 op char buf[IBUF_READ_SIZE];
911 efe7d180 2021-10-02 op if (event == EV_TIMEOUT) {
912 efe7d180 2021-10-02 op what |= EVBUFFER_TIMEOUT;
916 efe7d180 2021-10-02 op if (bufev->wm_read.high != 0)
917 efe7d180 2021-10-02 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
919 efe7d180 2021-10-02 op switch (ret = tls_read(client->ctx, buf, howmuch)) {
920 efe7d180 2021-10-02 op case TLS_WANT_POLLIN:
921 efe7d180 2021-10-02 op case TLS_WANT_POLLOUT:
924 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
929 efe7d180 2021-10-02 op if (len == 0) {
930 efe7d180 2021-10-02 op what |= EVBUFFER_EOF;
934 efe7d180 2021-10-02 op if (evbuffer_add(bufev->input, buf, len) == -1) {
935 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
939 efe7d180 2021-10-02 op event_add(&bufev->ev_read, NULL);
940 efe7d180 2021-10-02 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
942 efe7d180 2021-10-02 op if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
944 efe7d180 2021-10-02 op * here we could implement a read pressure policy.
948 efe7d180 2021-10-02 op if (bufev->readcb != NULL)
949 efe7d180 2021-10-02 op (*bufev->readcb)(bufev, bufev->cbarg);
954 efe7d180 2021-10-02 op event_add(&bufev->ev_read, NULL);
958 efe7d180 2021-10-02 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
962 efe7d180 2021-10-02 op client_tls_writecb(int fd, short event, void *d)
964 efe7d180 2021-10-02 op struct bufferevent *bufev = d;
965 efe7d180 2021-10-02 op struct client *client = bufev->cbarg;
968 efe7d180 2021-10-02 op short what = EVBUFFER_WRITE;
970 efe7d180 2021-10-02 op if (event == EV_TIMEOUT) {
971 efe7d180 2021-10-02 op what |= EVBUFFER_TIMEOUT;
975 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
976 efe7d180 2021-10-02 op ret = tls_write(client->ctx,
977 efe7d180 2021-10-02 op EVBUFFER_DATA(bufev->output),
978 efe7d180 2021-10-02 op EVBUFFER_LENGTH(bufev->output));
979 efe7d180 2021-10-02 op switch (ret) {
980 efe7d180 2021-10-02 op case TLS_WANT_POLLIN:
981 efe7d180 2021-10-02 op case TLS_WANT_POLLOUT:
984 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
988 efe7d180 2021-10-02 op evbuffer_drain(bufev->output, len);
991 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(bufev->output) != 0)
992 efe7d180 2021-10-02 op event_add(&bufev->ev_write, NULL);
994 efe7d180 2021-10-02 op if (bufev->writecb != NULL &&
995 efe7d180 2021-10-02 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
996 efe7d180 2021-10-02 op (*bufev->writecb)(bufev, bufev->cbarg);
1000 efe7d180 2021-10-02 op event_add(&bufev->ev_write, NULL);
1003 eae52ad4 2023-06-06 op log_warnx("tls error: %s", tls_error(client->ctx));
1004 efe7d180 2021-10-02 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
1008 efe7d180 2021-10-02 op client_read(struct bufferevent *bev, void *d)
1010 efe7d180 2021-10-02 op struct client *c = d;
1011 efe7d180 2021-10-02 op struct evbuffer *src = EVBUFFER_INPUT(bev);
1012 efe7d180 2021-10-02 op const char *parse_err = "invalid request";
1013 efe7d180 2021-10-02 op char decoded[DOMAIN_NAME_LEN];
1016 efe7d180 2021-10-02 op bufferevent_disable(bev, EVBUFFER_READ);
1019 901905e0 2022-01-05 op * libevent2 can still somehow call this function, even
1020 901905e0 2022-01-05 op * though I never enable EV_READ in the bufferevent. If
1021 901905e0 2022-01-05 op * that's the case, bail out.
1023 901905e0 2022-01-05 op if (c->type != REQUEST_UNDECIDED)
1026 efe7d180 2021-10-02 op /* max url len + \r\n */
1027 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(src) > 1024 + 2) {
1028 eae52ad4 2023-06-06 op log_debug("too much data received");
1029 efe7d180 2021-10-02 op start_reply(c, BAD_REQUEST, "bad request");
1033 efe7d180 2021-10-02 op c->req = evbuffer_readln(src, &len, EVBUFFER_EOL_CRLF_STRICT);
1034 efe7d180 2021-10-02 op if (c->req == NULL) {
1035 efe7d180 2021-10-02 op /* not enough data yet. */
1036 efe7d180 2021-10-02 op bufferevent_enable(bev, EVBUFFER_READ);
1039 ea27eaaa 2022-03-27 op c->reqlen = strlen(c->req);
1040 ea27eaaa 2022-03-27 op if (c->reqlen > 1024+2) {
1041 eae52ad4 2023-06-06 op log_debug("URL too long");
1042 ea27eaaa 2022-03-27 op start_reply(c, BAD_REQUEST, "bad request");
1046 efe7d180 2021-10-02 op if (!parse_iri(c->req, &c->iri, &parse_err) ||
1047 efe7d180 2021-10-02 op !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
1048 eae52ad4 2023-06-06 op log_debug("IRI parse error: %s", parse_err);
1049 807a80cb 2021-10-06 op start_reply(c, BAD_REQUEST, "bad request");
1053 72b033ef 2021-12-29 op if (apply_reverse_proxy(c))
1056 52c92ef6 2021-12-09 op /* ignore the port number */
1057 52c92ef6 2021-12-09 op if (strcmp(c->iri.schema, "gemini") ||
1058 efe7d180 2021-10-02 op strcmp(decoded, c->domain)) {
1059 efe7d180 2021-10-02 op start_reply(c, PROXY_REFUSED, "won't proxy request");
1063 efe7d180 2021-10-02 op if (apply_require_ca(c) ||
1064 efe7d180 2021-10-02 op apply_block_return(c)||
1065 efe7d180 2021-10-02 op apply_fastcgi(c))
1068 efe7d180 2021-10-02 op open_file(c);
1072 efe7d180 2021-10-02 op client_write(struct bufferevent *bev, void *d)
1074 efe7d180 2021-10-02 op struct client *c = d;
1075 efe7d180 2021-10-02 op struct evbuffer *out = EVBUFFER_OUTPUT(bev);
1076 f2f8eb35 2022-07-04 op char nam[PATH_MAX];
1077 efe7d180 2021-10-02 op char buf[BUFSIZ];
1080 efe7d180 2021-10-02 op switch (c->type) {
1081 efe7d180 2021-10-02 op case REQUEST_UNDECIDED:
1083 efe7d180 2021-10-02 op * Ignore spurious calls when we still don't have idea
1084 efe7d180 2021-10-02 op * what to do with the request.
1088 efe7d180 2021-10-02 op case REQUEST_FILE:
1089 efe7d180 2021-10-02 op if ((r = read(c->pfd, buf, sizeof(buf))) == -1) {
1090 eae52ad4 2023-06-06 op log_warn("read");
1091 efe7d180 2021-10-02 op client_error(bev, EVBUFFER_ERROR, c);
1093 efe7d180 2021-10-02 op } else if (r == 0) {
1094 efe7d180 2021-10-02 op client_close(c);
1096 efe7d180 2021-10-02 op } else if (r != sizeof(buf))
1097 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1098 efe7d180 2021-10-02 op bufferevent_write(bev, buf, r);
1101 efe7d180 2021-10-02 op case REQUEST_DIR:
1102 efe7d180 2021-10-02 op /* TODO: handle big big directories better */
1103 efe7d180 2021-10-02 op for (c->diroff = 0; c->diroff < c->dirlen; ++c->diroff) {
1104 543f4a66 2022-07-04 op const char *sufx = "";
1106 f2f8eb35 2022-07-04 op encode_path(nam, sizeof(nam),
1107 efe7d180 2021-10-02 op c->dir[c->diroff]->d_name);
1108 543f4a66 2022-07-04 op if (c->dir[c->diroff]->d_type == DT_DIR)
1110 543f4a66 2022-07-04 op evbuffer_add_printf(out, "=> ./%s%s\n", nam, sufx);
1111 efe7d180 2021-10-02 op free(c->dir[c->diroff]);
1113 efe7d180 2021-10-02 op free(c->dir);
1114 efe7d180 2021-10-02 op c->dir = NULL;
1116 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1118 efe7d180 2021-10-02 op event_add(&c->bev->ev_write, NULL);
1121 efe7d180 2021-10-02 op case REQUEST_FCGI:
1122 72b033ef 2021-12-29 op case REQUEST_PROXY:
1124 d29a2ee2 2022-09-06 op * Here we depend on fastcgi or proxy connection to
1125 d29a2ee2 2022-09-06 op * provide data.
1129 efe7d180 2021-10-02 op case REQUEST_DONE:
1130 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(out) == 0)
1131 efe7d180 2021-10-02 op client_close(c);
1137 efe7d180 2021-10-02 op client_error(struct bufferevent *bev, short error, void *d)
1139 efe7d180 2021-10-02 op struct client *c = d;
1141 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1143 efe7d180 2021-10-02 op if (error & EVBUFFER_TIMEOUT) {
1144 eae52ad4 2023-06-06 op log_debug("timeout; forcefully closing the connection");
1145 efe7d180 2021-10-02 op if (c->code == 0)
1146 efe7d180 2021-10-02 op start_reply(c, BAD_REQUEST, "timeout");
1148 efe7d180 2021-10-02 op client_close(c);
1152 efe7d180 2021-10-02 op if (error & EVBUFFER_EOF) {
1153 efe7d180 2021-10-02 op client_close(c);
1157 eae52ad4 2023-06-06 op log_warnx("unknown bufferevent error 0x%x", error);
1158 efe7d180 2021-10-02 op client_close(c);
1162 efe7d180 2021-10-02 op start_reply(struct client *c, int code, const char *meta)
1164 efe7d180 2021-10-02 op struct evbuffer *evb = EVBUFFER_OUTPUT(c->bev);
1165 efe7d180 2021-10-02 op const char *lang;
1168 efe7d180 2021-10-02 op bufferevent_enable(c->bev, EVBUFFER_WRITE);
1170 efe7d180 2021-10-02 op c->code = code;
1171 efe7d180 2021-10-02 op c->meta = meta;
1173 efe7d180 2021-10-02 op r = evbuffer_add_printf(evb, "%d %s", code, meta);
1174 efe7d180 2021-10-02 op if (r == -1)
1177 efe7d180 2021-10-02 op /* 2 digit status + space + 1024 max reply */
1178 efe7d180 2021-10-02 op if (r > 1027)
1179 efe7d180 2021-10-02 op goto overflow;
1181 d29a2ee2 2022-09-06 op if (c->type != REQUEST_FCGI &&
1182 72b033ef 2021-12-29 op c->type != REQUEST_PROXY &&
1183 efe7d180 2021-10-02 op !strcmp(meta, "text/gemini") &&
1184 efe7d180 2021-10-02 op (lang = vhost_lang(c->host, c->iri.path)) != NULL) {
1185 4842c72d 2021-10-18 op rr = evbuffer_add_printf(evb, ";lang=%s", lang);
1186 efe7d180 2021-10-02 op if (rr == -1)
1188 efe7d180 2021-10-02 op if (r + rr > 1027)
1189 efe7d180 2021-10-02 op goto overflow;
1192 efe7d180 2021-10-02 op bufferevent_write(c->bev, "\r\n", 2);
1194 efe7d180 2021-10-02 op if (!vhost_disable_log(c->host, c->iri.path))
1195 b8d68fc8 2023-06-11 op log_request(c, (char *)EVBUFFER_DATA(evb),
1196 b8d68fc8 2023-06-11 op EVBUFFER_LENGTH(evb));
1198 d28bd963 2022-01-27 op if (code != 20)
1199 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1204 eae52ad4 2023-06-06 op log_warnx("evbuffer_add_printf error: no memory");
1205 efe7d180 2021-10-02 op evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1206 efe7d180 2021-10-02 op client_close(c);
1210 eae52ad4 2023-06-06 op log_warnx("reply header overflow");
1211 efe7d180 2021-10-02 op evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1212 efe7d180 2021-10-02 op start_reply(c, TEMP_FAILURE, "internal error");
1216 efe7d180 2021-10-02 op client_close_ev(int fd, short event, void *d)
1218 8ad1c570 2021-05-09 op struct client *c = d;
1220 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
1221 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1222 efe7d180 2021-10-02 op event_once(c->fd, EV_READ, client_close_ev, c, NULL);
1224 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1225 efe7d180 2021-10-02 op event_once(c->fd, EV_WRITE, client_close_ev, c, NULL);
1229 d3a08f4d 2021-01-17 op connected_clients--;
1231 e4daebe4 2021-10-06 op free(c->req);
1233 d3a08f4d 2021-01-17 op tls_free(c->ctx);
1234 d3a08f4d 2021-01-17 op c->ctx = NULL;
1236 efe7d180 2021-10-02 op free(c->header);
1238 abc007d2 2021-02-08 op if (c->pfd != -1)
1239 abc007d2 2021-02-08 op close(c->pfd);
1241 252908e6 2021-01-24 op if (c->dir != NULL)
1242 11c98667 2021-04-25 op free(c->dir);
1244 abc007d2 2021-02-08 op close(c->fd);
1249 72b033ef 2021-12-29 op client_proxy_close(int fd, short event, void *d)
1251 72b033ef 2021-12-29 op struct tls *ctx = d;
1253 72b033ef 2021-12-29 op if (ctx == NULL) {
1258 72b033ef 2021-12-29 op switch (tls_close(ctx)) {
1259 72b033ef 2021-12-29 op case TLS_WANT_POLLIN:
1260 72b033ef 2021-12-29 op event_once(fd, EV_READ, client_proxy_close, d, NULL);
1262 72b033ef 2021-12-29 op case TLS_WANT_POLLOUT:
1263 72b033ef 2021-12-29 op event_once(fd, EV_WRITE, client_proxy_close, d, NULL);
1267 72b033ef 2021-12-29 op tls_free(ctx);
1272 efe7d180 2021-10-02 op client_close(struct client *c)
1275 efe7d180 2021-10-02 op * We may end up calling client_close in various situations
1276 efe7d180 2021-10-02 op * and for the most unexpected reasons. Therefore, we need to
1277 b9b77f53 2022-01-27 op * ensure that everything gets properly released once we reach
1278 efe7d180 2021-10-02 op * this point.
1281 207b3e80 2021-10-07 op SPLAY_REMOVE(client_tree_id, &clients, c);
1283 efe7d180 2021-10-02 op if (c->cgibev != NULL) {
1284 efe7d180 2021-10-02 op bufferevent_disable(c->cgibev, EVBUFFER_READ|EVBUFFER_WRITE);
1285 efe7d180 2021-10-02 op bufferevent_free(c->cgibev);
1286 efe7d180 2021-10-02 op c->cgibev = NULL;
1287 efe7d180 2021-10-02 op close(c->pfd);
1288 efe7d180 2021-10-02 op c->pfd = -1;
1291 efe7d180 2021-10-02 op bufferevent_disable(c->bev, EVBUFFER_READ|EVBUFFER_WRITE);
1292 efe7d180 2021-10-02 op bufferevent_free(c->bev);
1293 efe7d180 2021-10-02 op c->bev = NULL;
1295 e0f6dc64 2022-01-27 op if (c->proxyevset &&
1296 e0f6dc64 2022-01-27 op event_pending(&c->proxyev, EV_READ|EV_WRITE, NULL)) {
1297 e0f6dc64 2022-01-27 op c->proxyevset = 0;
1298 e0f6dc64 2022-01-27 op event_del(&c->proxyev);
1301 e0f6dc64 2022-01-27 op if (c->pfd != -1 && c->proxyctx != NULL) {
1302 e0f6dc64 2022-01-27 op /* shut down the proxy TLS connection */
1303 e0f6dc64 2022-01-27 op client_proxy_close(c->pfd, 0, c->proxyctx);
1304 e0f6dc64 2022-01-27 op c->pfd = -1;
1307 e0f6dc64 2022-01-27 op if (c->proxybev != NULL)
1308 e0f6dc64 2022-01-27 op bufferevent_free(c->proxybev);
1310 efe7d180 2021-10-02 op client_close_ev(c->fd, 0, c);
1314 abc007d2 2021-02-08 op do_accept(int sock, short et, void *d)
1316 af1dab18 2023-06-09 op struct conf *conf = d;
1317 abc007d2 2021-02-08 op struct client *c;
1318 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
1319 abc007d2 2021-02-08 op struct sockaddr *saddr;
1320 d3a08f4d 2021-01-17 op socklen_t len;
1323 abc007d2 2021-02-08 op saddr = (struct sockaddr*)&addr;
1324 d3a08f4d 2021-01-17 op len = sizeof(addr);
1325 3cb3dd4d 2021-02-12 op if ((fd = accept(sock, saddr, &len)) == -1) {
1326 c62a411f 2021-10-13 op if (errno == EWOULDBLOCK || errno == EAGAIN ||
1327 c62a411f 2021-10-13 op errno == ECONNABORTED)
1329 df5058c9 2023-06-05 op fatal("accept");
1332 3cb3dd4d 2021-02-12 op mark_nonblock(fd);
1334 207b3e80 2021-10-07 op c = xcalloc(1, sizeof(*c));
1335 af1dab18 2023-06-09 op c->conf = conf;
1336 207b3e80 2021-10-07 op c->id = ++server_client_id;
1338 207b3e80 2021-10-07 op c->pfd = -1;
1339 207b3e80 2021-10-07 op c->addr = addr;
1341 207b3e80 2021-10-07 op if (tls_accept_socket(ctx, &c->ctx, fd) == -1) {
1342 eae52ad4 2023-06-06 op log_warnx("failed to accept socket: %s", tls_error(c->ctx));
1343 207b3e80 2021-10-07 op close(c->fd);
1348 207b3e80 2021-10-07 op SPLAY_INSERT(client_tree_id, &clients, c);
1349 207b3e80 2021-10-07 op event_once(c->fd, EV_READ|EV_WRITE, handle_handshake, c, NULL);
1350 207b3e80 2021-10-07 op connected_clients++;
1353 8ad1c570 2021-05-09 op struct client *
1354 3fdc457c 2022-03-26 op client_by_id(int id)
1356 207b3e80 2021-10-07 op struct client find;
1358 207b3e80 2021-10-07 op find.id = id;
1359 207b3e80 2021-10-07 op return SPLAY_FIND(client_tree_id, &clients, &find);
1363 abc007d2 2021-02-08 op handle_siginfo(int fd, short ev, void *d)
1365 eae52ad4 2023-06-06 op log_info("%d connected clients", connected_clients);
1369 070b3295 2023-06-05 op add_keypair(struct vhost *h, struct tls_config *conf)
1371 c26f2460 2023-06-08 op if (h->ocsp == NULL) {
1372 c26f2460 2023-06-08 op if (tls_config_add_keypair_mem(conf, h->cert, h->certlen,
1373 c26f2460 2023-06-08 op h->key, h->keylen) == -1)
1374 c26f2460 2023-06-08 op fatalx("failed to load the keypair: %s",
1375 c26f2460 2023-06-08 op tls_config_error(conf));
1377 c26f2460 2023-06-08 op if (tls_config_add_keypair_ocsp_mem(conf, h->cert, h->certlen,
1378 c26f2460 2023-06-08 op h->key, h->keylen, h->ocsp, h->ocsplen) == -1)
1379 c26f2460 2023-06-08 op fatalx("failed to load the keypair: %s",
1380 070b3295 2023-06-05 op tls_config_error(conf));
1385 af1dab18 2023-06-09 op setup_tls(struct conf *conf)
1387 070b3295 2023-06-05 op struct tls_config *tlsconf;
1388 070b3295 2023-06-05 op struct vhost *h;
1390 c26f2460 2023-06-08 op if (ctx == NULL) {
1391 c26f2460 2023-06-08 op if ((ctx = tls_server()) == NULL)
1392 c26f2460 2023-06-08 op fatal("tls_server failure");
1395 070b3295 2023-06-05 op if ((tlsconf = tls_config_new()) == NULL)
1396 070b3295 2023-06-05 op fatal("tls_config_new");
1398 ba290ef3 2023-06-11 op if (conf->use_privsep_crypto)
1399 86693a33 2023-06-11 op tls_config_use_fake_private_key(tlsconf);
1401 070b3295 2023-06-05 op /* optionally accept client certs, but don't try to verify them */
1402 070b3295 2023-06-05 op tls_config_verify_client_optional(tlsconf);
1403 070b3295 2023-06-05 op tls_config_insecure_noverifycert(tlsconf);
1405 af1dab18 2023-06-09 op if (tls_config_set_protocols(tlsconf, conf->protos) == -1)
1406 070b3295 2023-06-05 op fatalx("tls_config_set_protocols: %s",
1407 070b3295 2023-06-05 op tls_config_error(tlsconf));
1409 af1dab18 2023-06-09 op h = TAILQ_FIRST(&conf->hosts);
1411 070b3295 2023-06-05 op /* we need to set something, then we can add how many key we want */
1412 c26f2460 2023-06-08 op if (tls_config_set_keypair_mem(tlsconf, h->cert, h->certlen,
1413 c26f2460 2023-06-08 op h->key, h->keylen) == -1)
1414 c26f2460 2023-06-08 op fatalx("tls_config_set_keypair_mem failed: %s",
1415 c26f2460 2023-06-08 op tls_config_error(tlsconf));
1417 070b3295 2023-06-05 op /* same for OCSP */
1418 c26f2460 2023-06-08 op if (h->ocsp != NULL &&
1419 c26f2460 2023-06-08 op tls_config_set_ocsp_staple_mem(tlsconf, h->ocsp, h->ocsplen)
1421 c26f2460 2023-06-08 op fatalx("tls_config_set_ocsp_staple_file failed: %s",
1422 c26f2460 2023-06-08 op tls_config_error(tlsconf));
1424 070b3295 2023-06-05 op while ((h = TAILQ_NEXT(h, vhosts)) != NULL)
1425 070b3295 2023-06-05 op add_keypair(h, tlsconf);
1427 c26f2460 2023-06-08 op tls_reset(ctx);
1428 070b3295 2023-06-05 op if (tls_configure(ctx, tlsconf) == -1)
1429 070b3295 2023-06-05 op fatalx("tls_configure: %s", tls_error(ctx));
1431 070b3295 2023-06-05 op tls_config_free(tlsconf);
1435 af1dab18 2023-06-09 op load_vhosts(struct conf *conf)
1437 a01a91db 2023-06-05 op struct vhost *h;
1438 a01a91db 2023-06-05 op struct location *l;
1440 af1dab18 2023-06-09 op TAILQ_FOREACH(h, &conf->hosts, vhosts) {
1441 a01a91db 2023-06-05 op TAILQ_FOREACH(l, &h->locations, locations) {
1442 a01a91db 2023-06-05 op if (*l->dir == '\0')
1444 a01a91db 2023-06-05 op l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY);
1445 a01a91db 2023-06-05 op if (l->dirfd == -1)
1446 df5058c9 2023-06-05 op fatal("open %s for domain %s", l->dir,
1453 c26f2460 2023-06-08 op server(struct privsep *ps, struct privsep_proc *p)
1455 c26f2460 2023-06-08 op proc_run(ps, p, procs, nitems(procs), server_init, NULL);
1459 c26f2460 2023-06-08 op server_init(struct privsep *ps, struct privsep_proc *p, void *arg)
1461 ba290ef3 2023-06-11 op struct conf *c;
1463 c26f2460 2023-06-08 op SPLAY_INIT(&clients);
1465 c26f2460 2023-06-08 op #ifdef SIGINFO
1466 c26f2460 2023-06-08 op signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
1467 c26f2460 2023-06-08 op signal_add(&siginfo, NULL);
1469 c26f2460 2023-06-08 op signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
1470 c26f2460 2023-06-08 op signal_add(&sigusr2, NULL);
1472 c26f2460 2023-06-08 op sandbox_server_process();
1475 86693a33 2023-06-11 op * ge doesn't use the privsep crypto engine; it doesn't use
1476 86693a33 2023-06-11 op * privsep at all so `ps' is NULL.
1478 ba290ef3 2023-06-11 op if (ps != NULL) {
1479 ba290ef3 2023-06-11 op c = ps->ps_env;
1480 ba290ef3 2023-06-11 op if (c->use_privsep_crypto)
1481 ba290ef3 2023-06-11 op crypto_engine_init(ps->ps_env);
1486 3886afce 2023-06-08 op server_configure_done(struct conf *conf)
1488 3886afce 2023-06-08 op if (load_default_mime(&conf->mime) == -1)
1489 3886afce 2023-06-08 op fatal("can't load default mime");
1490 3886afce 2023-06-08 op sort_mime(&conf->mime);
1491 af1dab18 2023-06-09 op setup_tls(conf);
1492 af1dab18 2023-06-09 op load_vhosts(conf);
1493 3886afce 2023-06-08 op if (conf->sock4 != -1)
1494 3886afce 2023-06-08 op event_add(&conf->evsock4, NULL);
1495 3886afce 2023-06-08 op if (conf->sock6 != -1)
1496 3886afce 2023-06-08 op event_add(&conf->evsock6, NULL);
1502 c26f2460 2023-06-08 op server_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
1504 c26f2460 2023-06-08 op struct privsep *ps = p->p_ps;
1505 c26f2460 2023-06-08 op struct conf *conf = ps->ps_env;
1507 c26f2460 2023-06-08 op switch (imsg->hdr.type) {
1508 c26f2460 2023-06-08 op case IMSG_RECONF_START:
1509 c26f2460 2023-06-08 op case IMSG_RECONF_MIME:
1510 c26f2460 2023-06-08 op case IMSG_RECONF_PROTOS:
1511 c26f2460 2023-06-08 op case IMSG_RECONF_PORT:
1512 c26f2460 2023-06-08 op case IMSG_RECONF_SOCK4:
1513 c26f2460 2023-06-08 op case IMSG_RECONF_SOCK6:
1514 c26f2460 2023-06-08 op case IMSG_RECONF_FCGI:
1515 c26f2460 2023-06-08 op case IMSG_RECONF_HOST:
1516 c26f2460 2023-06-08 op case IMSG_RECONF_CERT:
1517 c26f2460 2023-06-08 op case IMSG_RECONF_KEY:
1518 c26f2460 2023-06-08 op case IMSG_RECONF_OCSP:
1519 c26f2460 2023-06-08 op case IMSG_RECONF_LOC:
1520 c26f2460 2023-06-08 op case IMSG_RECONF_ENV:
1521 c26f2460 2023-06-08 op case IMSG_RECONF_ALIAS:
1522 c26f2460 2023-06-08 op case IMSG_RECONF_PROXY:
1523 deadd9e1 2023-06-09 op case IMSG_RECONF_PROXY_CERT:
1524 deadd9e1 2023-06-09 op case IMSG_RECONF_PROXY_KEY:
1525 c26f2460 2023-06-08 op return config_recv(conf, imsg);
1526 c26f2460 2023-06-08 op case IMSG_RECONF_END:
1527 c26f2460 2023-06-08 op if (config_recv(conf, imsg) == -1)
1529 3886afce 2023-06-08 op if (server_configure_done(conf) == -1)
1540 86693a33 2023-06-11 op server_dispatch_crypto(int fd, struct privsep_proc *p, struct imsg *imsg)
1546 c26f2460 2023-06-08 op server_dispatch_logger(int fd, struct privsep_proc *p, struct imsg *imsg)
1552 207b3e80 2021-10-07 op client_tree_cmp(struct client *a, struct client *b)
1554 207b3e80 2021-10-07 op if (a->id == b->id)
1556 207b3e80 2021-10-07 op else if (a->id < b->id)
1562 207b3e80 2021-10-07 op SPLAY_GENERATE(client_tree_id, client, entry, client_tree_cmp)