2 eac9287d 2023-06-24 op * Copyright (c) 2021, 2022, 2023 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 237095fd 2023-06-11 op #ifdef SIGINFO
41 237095fd 2023-06-11 op static struct event siginfo;
43 237095fd 2023-06-11 op static struct event sigusr2;
45 d3a08f4d 2021-01-17 op int connected_clients;
48 86693a33 2023-06-11 op * This function is not publicy exported because it is a hack until libtls
49 86693a33 2023-06-11 op * has a proper privsep setup.
51 86693a33 2023-06-11 op void tls_config_use_fake_private_key(struct tls_config *);
53 49b73ba1 2021-02-10 op static inline int matches(const char*, const char*);
55 abc007d2 2021-02-08 op static void handle_handshake(int, short, void*);
56 80192f45 2023-07-01 op static void fmtbuf(char *, size_t, const char *, struct client *,
57 80192f45 2023-07-01 op const char *);
58 abc007d2 2021-02-08 op static int apply_block_return(struct client*);
59 d474a979 2022-01-04 op static int check_matching_certificate(X509_STORE *, struct client *);
60 72b033ef 2021-12-29 op static int apply_reverse_proxy(struct client *);
61 efe7d180 2021-10-02 op static int apply_fastcgi(struct client*);
62 02be96c6 2021-02-09 op static int apply_require_ca(struct client*);
63 abc007d2 2021-02-08 op static void open_dir(struct client*);
64 abc007d2 2021-02-08 op static void redirect_canonical_dir(struct client*);
66 efe7d180 2021-10-02 op static void client_tls_readcb(int, short, void *);
67 efe7d180 2021-10-02 op static void client_tls_writecb(int, short, void *);
69 efe7d180 2021-10-02 op static void client_read(struct bufferevent *, void *);
70 efe7d180 2021-10-02 op void client_write(struct bufferevent *, void *);
71 efe7d180 2021-10-02 op static void client_error(struct bufferevent *, short, void *);
73 efe7d180 2021-10-02 op static void client_close_ev(int, short, void *);
75 bc99d868 2021-03-19 op static void handle_siginfo(int, short, void*);
77 c26f2460 2023-06-08 op static int server_dispatch_parent(int, struct privsep_proc *, struct imsg *);
78 86693a33 2023-06-11 op static int server_dispatch_crypto(int, struct privsep_proc *, struct imsg *);
79 c26f2460 2023-06-08 op static int server_dispatch_logger(int, struct privsep_proc *, struct imsg *);
81 c26f2460 2023-06-08 op static struct privsep_proc procs[] = {
82 c26f2460 2023-06-08 op { "parent", PROC_PARENT, server_dispatch_parent },
83 86693a33 2023-06-11 op { "crypto", PROC_CRYPTO, server_dispatch_crypto },
84 c26f2460 2023-06-08 op { "logger", PROC_LOGGER, server_dispatch_logger },
87 207b3e80 2021-10-07 op static uint32_t server_client_id;
89 207b3e80 2021-10-07 op struct client_tree_id clients;
91 49b73ba1 2021-02-10 op static inline int
92 aa30aaed 2023-06-24 op match_addr(struct address *target, struct address *source)
94 aa30aaed 2023-06-24 op return (target->ai_flags == source->ai_flags &&
95 aa30aaed 2023-06-24 op target->ai_family == source->ai_family &&
96 aa30aaed 2023-06-24 op target->ai_socktype == source->ai_socktype &&
97 aa30aaed 2023-06-24 op target->ai_protocol == source->ai_protocol &&
98 aa30aaed 2023-06-24 op target->slen == source->slen &&
99 aa30aaed 2023-06-24 op !memcmp(&target->ss, &source->ss, target->slen));
102 aa30aaed 2023-06-24 op static inline int
103 49b73ba1 2021-02-10 op matches(const char *pattern, const char *path)
105 49b73ba1 2021-02-10 op if (*path == '/')
107 49b73ba1 2021-02-10 op return !fnmatch(pattern, path, 0);
110 aa30aaed 2023-06-24 op static inline int
111 aa30aaed 2023-06-24 op match_host(struct vhost *v, struct client *c)
113 aa30aaed 2023-06-24 op struct alist *a;
114 aa30aaed 2023-06-24 op struct address *addr;
116 aa30aaed 2023-06-24 op TAILQ_FOREACH(addr, &v->addrs, addrs)
117 aa30aaed 2023-06-24 op if (match_addr(addr, c->addr))
119 aa30aaed 2023-06-24 op if (addr == NULL)
122 aa30aaed 2023-06-24 op if (matches(v->domain, c->domain))
125 aa30aaed 2023-06-24 op TAILQ_FOREACH(a, &v->aliases, aliases)
126 aa30aaed 2023-06-24 op if (matches(a->alias, c->domain))
133 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
135 c8b74339 2021-01-24 op struct location *loc;
137 caad0308 2021-01-27 op if (v == NULL || path == NULL)
140 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
141 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
142 534afd0d 2022-10-05 op if (*loc->lang != '\0') {
143 49b73ba1 2021-02-10 op if (matches(loc->match, path))
144 6016a593 2021-01-30 op return loc->lang;
148 534afd0d 2022-10-05 op loc = TAILQ_FIRST(&v->locations);
149 534afd0d 2022-10-05 op if (*loc->lang == '\0')
151 534afd0d 2022-10-05 op return loc->lang;
155 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
157 c8b74339 2021-01-24 op struct location *loc;
158 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
160 caad0308 2021-01-27 op if (v == NULL || path == NULL)
161 caad0308 2021-01-27 op return default_mime;
163 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
164 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
165 534afd0d 2022-10-05 op if (*loc->default_mime != '\0') {
166 49b73ba1 2021-02-10 op if (matches(loc->match, path))
167 6016a593 2021-01-30 op return loc->default_mime;
171 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
172 534afd0d 2022-10-05 op if (*loc->default_mime != '\0')
173 b8e64ccd 2021-03-31 op return loc->default_mime;
174 c8b74339 2021-01-24 op return default_mime;
178 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
180 c8b74339 2021-01-24 op struct location *loc;
181 c8b74339 2021-01-24 op const char *index = "index.gmi";
183 caad0308 2021-01-27 op if (v == NULL || path == NULL)
184 caad0308 2021-01-27 op return index;
186 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
187 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
188 534afd0d 2022-10-05 op if (*loc->index != '\0') {
189 49b73ba1 2021-02-10 op if (matches(loc->match, path))
190 6016a593 2021-01-30 op return loc->index;
194 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
195 534afd0d 2022-10-05 op if (*loc->index != '\0')
196 b8e64ccd 2021-03-31 op return loc->index;
197 c8b74339 2021-01-24 op return index;
201 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
203 252908e6 2021-01-24 op struct location *loc;
205 6016a593 2021-01-30 op if (v == NULL || path == NULL)
208 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
209 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
210 cd761624 2021-02-06 op if (loc->auto_index != 0) {
211 49b73ba1 2021-02-10 op if (matches(loc->match, path))
212 6016a593 2021-01-30 op return loc->auto_index == 1;
216 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
217 b8e64ccd 2021-03-31 op return loc->auto_index == 1;
221 6abda252 2021-02-06 op vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
223 6abda252 2021-02-06 op struct location *loc;
225 6abda252 2021-02-06 op if (v == NULL || path == NULL)
228 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
229 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
230 cd761624 2021-02-06 op if (loc->block_code != 0) {
231 49b73ba1 2021-02-10 op if (matches(loc->match, path)) {
232 6abda252 2021-02-06 op *code = loc->block_code;
233 6abda252 2021-02-06 op *fmt = loc->block_fmt;
239 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
240 b8e64ccd 2021-03-31 op *code = loc->block_code;
241 b8e64ccd 2021-03-31 op *fmt = loc->block_fmt;
242 b8e64ccd 2021-03-31 op return loc->block_code != 0;
245 a1ba9650 2023-07-23 op struct location *
246 8ad1c570 2021-05-09 op vhost_fastcgi(struct vhost *v, const char *path)
248 8ad1c570 2021-05-09 op struct location *loc;
249 6a8387e5 2023-07-23 op int force_disable = 0;
251 8ad1c570 2021-05-09 op if (v == NULL || path == NULL)
254 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
255 8ad1c570 2021-05-09 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
256 8ad1c570 2021-05-09 op if (loc->fcgi != -1)
257 8ad1c570 2021-05-09 op if (matches(loc->match, path))
259 6a8387e5 2023-07-23 op if (loc->nofcgi && matches(loc->match, path))
260 6a8387e5 2023-07-23 op force_disable = 1;
263 6a8387e5 2023-07-23 op if (force_disable)
266 8ad1c570 2021-05-09 op loc = TAILQ_FIRST(&v->locations);
267 a1ba9650 2023-07-23 op return loc->fcgi == -1 ? NULL : loc;
271 1feaf2a6 2021-05-15 op vhost_dirfd(struct vhost *v, const char *path, size_t *retloc)
273 fdea6aa0 2021-04-30 op struct location *loc;
274 1feaf2a6 2021-05-15 op size_t l = 0;
276 fdea6aa0 2021-04-30 op if (v == NULL || path == NULL)
279 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
280 fdea6aa0 2021-04-30 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
282 fdea6aa0 2021-04-30 op if (loc->dirfd != -1)
283 1feaf2a6 2021-05-15 op if (matches(loc->match, path)) {
285 fdea6aa0 2021-04-30 op return loc->dirfd;
290 fdea6aa0 2021-04-30 op loc = TAILQ_FIRST(&v->locations);
291 fdea6aa0 2021-04-30 op return loc->dirfd;
295 6abda252 2021-02-06 op vhost_strip(struct vhost *v, const char *path)
297 6abda252 2021-02-06 op struct location *loc;
299 6abda252 2021-02-06 op if (v == NULL || path == NULL)
302 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
303 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
304 cd761624 2021-02-06 op if (loc->strip != 0) {
305 49b73ba1 2021-02-10 op if (matches(loc->match, path))
306 6abda252 2021-02-06 op return loc->strip;
310 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
311 b8e64ccd 2021-03-31 op return loc->strip;
315 02be96c6 2021-02-09 op vhost_require_ca(struct vhost *v, const char *path)
317 02be96c6 2021-02-09 op struct location *loc;
319 02be96c6 2021-02-09 op if (v == NULL || path == NULL)
322 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
323 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
324 02be96c6 2021-02-09 op if (loc->reqca != NULL) {
325 49b73ba1 2021-02-10 op if (matches(loc->match, path))
326 02be96c6 2021-02-09 op return loc->reqca;
330 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
331 b8e64ccd 2021-03-31 op return loc->reqca;
335 793835cb 2021-02-23 op vhost_disable_log(struct vhost *v, const char *path)
337 793835cb 2021-02-23 op struct location *loc;
339 793835cb 2021-02-23 op if (v == NULL || path == NULL)
342 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
343 b8e64ccd 2021-03-31 op while ((loc = TAILQ_NEXT(loc, locations)) != NULL) {
344 793835cb 2021-02-23 op if (loc->disable_log && matches(loc->match, path))
348 b8e64ccd 2021-03-31 op loc = TAILQ_FIRST(&v->locations);
349 b8e64ccd 2021-03-31 op return loc->disable_log;
353 9b8f5ed2 2021-02-03 op mark_nonblock(int fd)
357 9b8f5ed2 2021-02-03 op if ((flags = fcntl(fd, F_GETFL)) == -1)
358 df5058c9 2023-06-05 op fatal("fcntl(F_GETFL)");
359 9b8f5ed2 2021-02-03 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
360 df5058c9 2023-06-05 op fatal("fcntl(F_SETFL)");
364 abc007d2 2021-02-08 op handle_handshake(int fd, short ev, void *d)
366 abc007d2 2021-02-08 op struct client *c = d;
367 af1dab18 2023-06-09 op struct conf *conf = c->conf;
368 d3a08f4d 2021-01-17 op struct vhost *h;
369 d3a08f4d 2021-01-17 op const char *servname;
370 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
372 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
373 d3a08f4d 2021-01-17 op case 0: /* success */
376 c440a0de 2023-08-28 op log_warnx("(%s:%s) tls_handshake failed: %s",
377 c440a0de 2023-08-28 op c->rhost, c->rserv, tls_error(c->ctx));
378 c5edb157 2023-06-24 op client_close(c);
380 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
381 efe7d180 2021-10-02 op event_once(c->fd, EV_READ, handle_handshake, c, NULL);
383 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
384 efe7d180 2021-10-02 op event_once(c->fd, EV_WRITE, handle_handshake, c, NULL);
387 d3a08f4d 2021-01-17 op /* unreachable */
391 80444938 2021-10-15 op c->bev = bufferevent_new(fd, client_read, client_write,
392 80444938 2021-10-15 op client_error, c);
393 80444938 2021-10-15 op if (c->bev == NULL)
394 df5058c9 2023-06-05 op fatal("%s: failed to allocate client buffer", __func__);
396 80444938 2021-10-15 op event_set(&c->bev->ev_read, c->fd, EV_READ,
397 80444938 2021-10-15 op client_tls_readcb, c->bev);
398 80444938 2021-10-15 op event_set(&c->bev->ev_write, c->fd, EV_WRITE,
399 80444938 2021-10-15 op client_tls_writecb, c->bev);
401 80444938 2021-10-15 op #if HAVE_LIBEVENT2
402 80444938 2021-10-15 op evbuffer_unfreeze(c->bev->input, 0);
403 80444938 2021-10-15 op evbuffer_unfreeze(c->bev->output, 1);
406 79288c8b 2021-09-24 op if ((servname = tls_conn_servername(c->ctx)) == NULL) {
407 eae52ad4 2023-06-06 op log_debug("handshake: missing SNI");
411 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
412 eae52ad4 2023-06-06 op log_info("puny_decode: %s", parse_err);
416 aa30aaed 2023-06-24 op TAILQ_FOREACH(h, &conf->hosts, vhosts)
417 aa30aaed 2023-06-24 op if (match_host(h, c))
420 eae52ad4 2023-06-06 op log_debug("handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
421 3abf91b0 2021-02-07 op servname != NULL ? servname : "(null)",
423 b8e64ccd 2021-03-31 op h != NULL ? h->domain : "(null)");
425 b8e64ccd 2021-03-31 op if (h != NULL) {
427 efe7d180 2021-10-02 op bufferevent_enable(c->bev, EV_READ);
432 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
436 80192f45 2023-07-01 op fmtbuf(char *buf, size_t buflen, const char *fmt, struct client *c,
437 80192f45 2023-07-01 op const char *path)
440 80192f45 2023-07-01 op char tmp[32];
443 80192f45 2023-07-01 op memset(tmp, 0, sizeof(tmp));
444 6abda252 2021-02-06 op for (i = 0; *fmt; ++fmt) {
445 80192f45 2023-07-01 op if (i == sizeof(tmp)-1 || *fmt == '%') {
446 80192f45 2023-07-01 op strlcat(buf, tmp, buflen);
447 80192f45 2023-07-01 op memset(tmp, 0, sizeof(tmp));
451 6abda252 2021-02-06 op if (*fmt != '%') {
452 80192f45 2023-07-01 op tmp[i++] = *fmt;
456 6abda252 2021-02-06 op switch (*++fmt) {
458 80192f45 2023-07-01 op strlcat(buf, "%", buflen);
461 737a6b50 2021-04-30 op if (*path != '/')
462 80192f45 2023-07-01 op strlcat(buf, "/", buflen);
463 80192f45 2023-07-01 op strlcat(buf, path, buflen);
466 80192f45 2023-07-01 op strlcat(buf, c->iri.query, buflen);
469 80192f45 2023-07-01 op snprintf(tmp, sizeof(tmp), "%d", c->addr->port);
470 80192f45 2023-07-01 op strlcat(buf, tmp, buflen);
471 80192f45 2023-07-01 op memset(tmp, 0, sizeof(tmp));
474 80192f45 2023-07-01 op strlcat(buf, c->domain, buflen);
477 e3ce19dc 2023-07-01 op log_warnx("%s: unknown fmt specifier %c",
478 6abda252 2021-02-06 op __func__, *fmt);
483 80192f45 2023-07-01 op strlcat(buf, tmp, buflen);
486 57ec3e77 2021-02-08 op /* 1 if a matching `block return' (and apply it), 0 otherwise */
488 57ec3e77 2021-02-08 op apply_block_return(struct client *c)
490 80192f45 2023-07-01 op char buf[GEMINI_URL_LEN];
491 57ec3e77 2021-02-08 op const char *fmt, *path;
494 57ec3e77 2021-02-08 op if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
497 57ec3e77 2021-02-08 op path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
498 80192f45 2023-07-01 op fmtbuf(buf, sizeof(buf), fmt, c, path);
500 80192f45 2023-07-01 op start_reply(c, code, buf);
504 b7967bc1 2021-01-02 op static struct proxy *
505 b7967bc1 2021-01-02 op matched_proxy(struct client *c)
507 b7967bc1 2021-01-02 op struct proxy *p;
508 b7967bc1 2021-01-02 op const char *proto;
509 b7967bc1 2021-01-02 op const char *host;
510 b7967bc1 2021-01-02 op const char *port;
512 b7967bc1 2021-01-02 op TAILQ_FOREACH(p, &c->host->proxies, proxies) {
513 534afd0d 2022-10-05 op if (*(proto = p->match_proto) == '\0')
514 b7967bc1 2021-01-02 op proto = "gemini";
515 534afd0d 2022-10-05 op if (*(host = p->match_host) == '\0')
517 534afd0d 2022-10-05 op if (*(port = p->match_port) == '\0')
520 b7967bc1 2021-01-02 op if (matches(proto, c->iri.schema) &&
521 b7967bc1 2021-01-02 op matches(host, c->domain) &&
522 b7967bc1 2021-01-02 op matches(port, c->iri.port))
530 ba94a608 2022-01-04 op check_matching_certificate(X509_STORE *store, struct client *c)
532 ba94a608 2022-01-04 op const uint8_t *cert;
535 ba94a608 2022-01-04 op if (!tls_peer_cert_provided(c->ctx)) {
536 ba94a608 2022-01-04 op start_reply(c, CLIENT_CERT_REQ, "client certificate required");
540 ba94a608 2022-01-04 op cert = tls_peer_cert_chain_pem(c->ctx, &len);
541 ba94a608 2022-01-04 op if (!validate_against_ca(store, cert, len)) {
542 ba94a608 2022-01-04 op start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
550 d29a2ee2 2022-09-06 op proxy_socket(struct client *c, const char *host, const char *port)
552 d29a2ee2 2022-09-06 op struct addrinfo hints, *res, *res0;
553 eae52ad4 2023-06-06 op int r, sock, save_errno;
554 eae52ad4 2023-06-06 op const char *cause = NULL;
556 d29a2ee2 2022-09-06 op memset(&hints, 0, sizeof(hints));
557 d29a2ee2 2022-09-06 op hints.ai_family = AF_UNSPEC;
558 d29a2ee2 2022-09-06 op hints.ai_socktype = SOCK_STREAM;
560 d29a2ee2 2022-09-06 op /* XXX: asr_run? :> */
561 d29a2ee2 2022-09-06 op r = getaddrinfo(host, port, &hints, &res0);
562 d29a2ee2 2022-09-06 op if (r != 0) {
563 eae52ad4 2023-06-06 op log_warnx("getaddrinfo(\"%s\", \"%s\"): %s",
564 d29a2ee2 2022-09-06 op host, port, gai_strerror(r));
568 d29a2ee2 2022-09-06 op for (res = res0; res; res = res->ai_next) {
569 d29a2ee2 2022-09-06 op sock = socket(res->ai_family, res->ai_socktype,
570 d29a2ee2 2022-09-06 op res->ai_protocol);
571 eae52ad4 2023-06-06 op if (sock == -1) {
572 eae52ad4 2023-06-06 op cause = "socket";
576 d29a2ee2 2022-09-06 op if (connect(sock, res->ai_addr, res->ai_addrlen) == -1) {
577 eae52ad4 2023-06-06 op cause = "connect";
578 eae52ad4 2023-06-06 op save_errno = errno;
580 eae52ad4 2023-06-06 op errno = save_errno;
588 d29a2ee2 2022-09-06 op if (sock == -1)
589 eae52ad4 2023-06-06 op log_warn("can't connect to %s:%s: %s", host, port, cause);
591 eae52ad4 2023-06-06 op freeaddrinfo(res0);
596 72b033ef 2021-12-29 op /* 1 if matching a proxy relay-to (and apply it), 0 otherwise */
598 72b033ef 2021-12-29 op apply_reverse_proxy(struct client *c)
600 7bdcc91e 2021-01-01 op struct proxy *p;
602 b7967bc1 2021-01-02 op if ((p = matched_proxy(c)) == NULL)
605 b7967bc1 2021-01-02 op c->proxy = p;
607 ba94a608 2022-01-04 op if (p->reqca != NULL && check_matching_certificate(p->reqca, c))
610 eae52ad4 2023-06-06 op log_debug("opening proxy connection for %s:%s",
611 7bdcc91e 2021-01-01 op p->host, p->port);
613 d29a2ee2 2022-09-06 op if ((c->pfd = proxy_socket(c, p->host, p->port)) == -1) {
614 d29a2ee2 2022-09-06 op start_reply(c, PROXY_ERROR, "proxy error");
618 d29a2ee2 2022-09-06 op mark_nonblock(c->pfd);
619 d29a2ee2 2022-09-06 op if (proxy_init(c) == -1)
620 d29a2ee2 2022-09-06 op start_reply(c, PROXY_ERROR, "proxy error");
626 d29a2ee2 2022-09-06 op fcgi_open_sock(struct fcgi *f)
628 d29a2ee2 2022-09-06 op struct sockaddr_un addr;
631 d29a2ee2 2022-09-06 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
632 eae52ad4 2023-06-06 op log_warn("socket");
636 d29a2ee2 2022-09-06 op memset(&addr, 0, sizeof(addr));
637 d29a2ee2 2022-09-06 op addr.sun_family = AF_UNIX;
638 d29a2ee2 2022-09-06 op strlcpy(addr.sun_path, f->path, sizeof(addr.sun_path));
640 d29a2ee2 2022-09-06 op if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
641 eae52ad4 2023-06-06 op log_warn("failed to connect to %s", f->path);
650 d29a2ee2 2022-09-06 op fcgi_open_conn(struct fcgi *f)
652 eae52ad4 2023-06-06 op struct addrinfo hints, *servinfo, *p;
653 eae52ad4 2023-06-06 op int r, sock, save_errno;
654 eae52ad4 2023-06-06 op const char *cause = NULL;
656 d29a2ee2 2022-09-06 op memset(&hints, 0, sizeof(hints));
657 d29a2ee2 2022-09-06 op hints.ai_family = AF_UNSPEC;
658 d29a2ee2 2022-09-06 op hints.ai_socktype = SOCK_STREAM;
659 d29a2ee2 2022-09-06 op hints.ai_flags = AI_ADDRCONFIG;
661 d29a2ee2 2022-09-06 op if ((r = getaddrinfo(f->path, f->port, &hints, &servinfo)) != 0) {
662 eae52ad4 2023-06-06 op log_warnx("getaddrinfo %s:%s: %s", f->path, f->port,
663 d29a2ee2 2022-09-06 op gai_strerror(r));
667 d29a2ee2 2022-09-06 op for (p = servinfo; p != NULL; p = p->ai_next) {
668 d29a2ee2 2022-09-06 op sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
669 eae52ad4 2023-06-06 op if (sock == -1) {
670 eae52ad4 2023-06-06 op cause = "socket";
673 d29a2ee2 2022-09-06 op if (connect(sock, p->ai_addr, p->ai_addrlen) == -1) {
674 eae52ad4 2023-06-06 op cause = "connect";
675 eae52ad4 2023-06-06 op save_errno = errno;
677 eae52ad4 2023-06-06 op errno = save_errno;
683 d29a2ee2 2022-09-06 op if (p == NULL) {
684 eae52ad4 2023-06-06 op log_warn("couldn't connect to %s:%s: %s", f->path, f->port,
689 d29a2ee2 2022-09-06 op freeaddrinfo(servinfo);
693 8ad1c570 2021-05-09 op /* 1 if matching `fcgi' (and apply it), 0 otherwise */
695 8ad1c570 2021-05-09 op apply_fastcgi(struct client *c)
698 8ad1c570 2021-05-09 op struct fcgi *f;
699 a1ba9650 2023-07-23 op struct location *loc;
701 a1ba9650 2023-07-23 op if ((loc = vhost_fastcgi(c->host, c->iri.path)) == NULL)
704 af1dab18 2023-06-09 op TAILQ_FOREACH(f, &c->conf->fcgi, fcgi) {
705 a1ba9650 2023-07-23 op if (i == loc->fcgi)
710 5d22294a 2023-06-09 op if (f == NULL) {
711 a1ba9650 2023-07-23 op log_warnx("can't find fcgi #%d", loc->fcgi);
715 eae52ad4 2023-06-06 op log_debug("opening fastcgi connection for (%s,%s)",
716 d29a2ee2 2022-09-06 op f->path, f->port);
718 eb4f96c1 2022-11-27 op if (*f->port == '\0')
719 d29a2ee2 2022-09-06 op c->pfd = fcgi_open_sock(f);
721 d29a2ee2 2022-09-06 op c->pfd = fcgi_open_conn(f);
723 d29a2ee2 2022-09-06 op if (c->pfd == -1) {
724 d29a2ee2 2022-09-06 op start_reply(c, CGI_ERROR, "CGI error");
728 d29a2ee2 2022-09-06 op mark_nonblock(c->pfd);
730 d29a2ee2 2022-09-06 op c->cgibev = bufferevent_new(c->pfd, fcgi_read, fcgi_write,
731 d29a2ee2 2022-09-06 op fcgi_error, c);
732 d29a2ee2 2022-09-06 op if (c->cgibev == NULL) {
733 d29a2ee2 2022-09-06 op start_reply(c, TEMP_FAILURE, "internal server error");
737 d29a2ee2 2022-09-06 op bufferevent_enable(c->cgibev, EV_READ|EV_WRITE);
738 a1ba9650 2023-07-23 op fcgi_req(c, loc);
743 02be96c6 2021-02-09 op /* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
745 02be96c6 2021-02-09 op apply_require_ca(struct client *c)
747 02be96c6 2021-02-09 op X509_STORE *store;
749 02be96c6 2021-02-09 op if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
751 ba94a608 2022-01-04 op return check_matching_certificate(store, c);
755 e2003e7e 2023-07-01 op server_dir_listing(struct client *c)
759 11c98667 2021-04-25 op root = !strcmp(c->iri.path, "/") || *c->iri.path == '\0';
761 e2003e7e 2023-07-01 op if (!vhost_auto_index(c->host, c->iri.path)) {
762 e2003e7e 2023-07-01 op start_reply(c, NOT_FOUND, "not found");
766 e2003e7e 2023-07-01 op c->dirlen = scandir_fd(c->pfd, &c->dir,
767 e2003e7e 2023-07-01 op root ? select_non_dotdot : select_non_dot,
769 e2003e7e 2023-07-01 op if (c->dirlen == -1) {
770 e2003e7e 2023-07-01 op log_warn("scandir_fd(%d) (vhost:%s) %s",
771 e2003e7e 2023-07-01 op c->pfd, c->host->domain, c->iri.path);
772 e2003e7e 2023-07-01 op start_reply(c, TEMP_FAILURE, "internal server error");
776 e2003e7e 2023-07-01 op c->type = REQUEST_DIR;
777 e2003e7e 2023-07-01 op start_reply(c, SUCCESS, "text/gemini");
778 e2003e7e 2023-07-01 op evbuffer_add_printf(EVBUFFER_OUTPUT(c->bev),
779 e2003e7e 2023-07-01 op "# Index of /%s\n\n", c->iri.path);
783 e2003e7e 2023-07-01 op open_dir(struct client *c)
785 e2003e7e 2023-07-01 op struct stat sb;
786 e2003e7e 2023-07-01 op const char *index;
787 e2003e7e 2023-07-01 op char path[PATH_MAX];
790 994fc034 2023-07-01 op if (*c->iri.path != '\0' && !ends_with(c->iri.path, "/")) {
791 abc007d2 2021-02-08 op redirect_canonical_dir(c);
795 e2003e7e 2023-07-01 op index = vhost_index(c->host, c->iri.path);
796 e2003e7e 2023-07-01 op fd = openat(c->pfd, index, O_RDONLY);
797 e2003e7e 2023-07-01 op if (fd == -1) {
798 e2003e7e 2023-07-01 op server_dir_listing(c);
802 e2003e7e 2023-07-01 op if (fstat(fd, &sb) == -1) {
803 e2003e7e 2023-07-01 op log_warn("fstat");
805 e2003e7e 2023-07-01 op start_reply(c, TEMP_FAILURE, "internal server error");
809 e2003e7e 2023-07-01 op if (!S_ISREG(sb.st_mode)) {
811 e2003e7e 2023-07-01 op server_dir_listing(c);
815 e2003e7e 2023-07-01 op strlcpy(path, c->iri.path, sizeof(path));
816 e2003e7e 2023-07-01 op strlcat(path, index, sizeof(path));
818 e2003e7e 2023-07-01 op close(c->pfd);
820 e2003e7e 2023-07-01 op c->type = REQUEST_FILE;
821 e2003e7e 2023-07-01 op start_reply(c, SUCCESS, mime(c->conf, c->host, path));
825 abc007d2 2021-02-08 op redirect_canonical_dir(struct client *c)
827 2339a711 2023-07-01 op char buf[GEMINI_URL_LEN];
830 2339a711 2023-07-01 op r = snprintf(buf, sizeof(buf), "/%s/", c->iri.path);
831 2339a711 2023-07-01 op if (r < 0 || (size_t)r >= sizeof(buf)) {
832 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
836 2339a711 2023-07-01 op start_reply(c, TEMP_REDIRECT, buf);
840 efe7d180 2021-10-02 op client_tls_readcb(int fd, short event, void *d)
842 efe7d180 2021-10-02 op struct bufferevent *bufev = d;
843 efe7d180 2021-10-02 op struct client *client = bufev->cbarg;
846 efe7d180 2021-10-02 op int what = EVBUFFER_READ;
847 efe7d180 2021-10-02 op int howmuch = IBUF_READ_SIZE;
848 efe7d180 2021-10-02 op char buf[IBUF_READ_SIZE];
850 efe7d180 2021-10-02 op if (event == EV_TIMEOUT) {
851 efe7d180 2021-10-02 op what |= EVBUFFER_TIMEOUT;
855 efe7d180 2021-10-02 op if (bufev->wm_read.high != 0)
856 efe7d180 2021-10-02 op howmuch = MIN(sizeof(buf), bufev->wm_read.high);
858 efe7d180 2021-10-02 op switch (ret = tls_read(client->ctx, buf, howmuch)) {
859 efe7d180 2021-10-02 op case TLS_WANT_POLLIN:
860 efe7d180 2021-10-02 op case TLS_WANT_POLLOUT:
863 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
868 efe7d180 2021-10-02 op if (len == 0) {
869 efe7d180 2021-10-02 op what |= EVBUFFER_EOF;
873 efe7d180 2021-10-02 op if (evbuffer_add(bufev->input, buf, len) == -1) {
874 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
878 efe7d180 2021-10-02 op event_add(&bufev->ev_read, NULL);
879 efe7d180 2021-10-02 op if (bufev->wm_read.low != 0 && len < bufev->wm_read.low)
881 efe7d180 2021-10-02 op if (bufev->wm_read.high != 0 && len > bufev->wm_read.high) {
883 efe7d180 2021-10-02 op * here we could implement a read pressure policy.
887 efe7d180 2021-10-02 op if (bufev->readcb != NULL)
888 efe7d180 2021-10-02 op (*bufev->readcb)(bufev, bufev->cbarg);
893 efe7d180 2021-10-02 op event_add(&bufev->ev_read, NULL);
897 efe7d180 2021-10-02 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
901 efe7d180 2021-10-02 op client_tls_writecb(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 short what = EVBUFFER_WRITE;
909 efe7d180 2021-10-02 op if (event == EV_TIMEOUT) {
910 efe7d180 2021-10-02 op what |= EVBUFFER_TIMEOUT;
914 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(bufev->output) != 0) {
915 efe7d180 2021-10-02 op ret = tls_write(client->ctx,
916 efe7d180 2021-10-02 op EVBUFFER_DATA(bufev->output),
917 efe7d180 2021-10-02 op EVBUFFER_LENGTH(bufev->output));
918 efe7d180 2021-10-02 op switch (ret) {
919 efe7d180 2021-10-02 op case TLS_WANT_POLLIN:
920 efe7d180 2021-10-02 op case TLS_WANT_POLLOUT:
923 efe7d180 2021-10-02 op what |= EVBUFFER_ERROR;
927 efe7d180 2021-10-02 op evbuffer_drain(bufev->output, len);
930 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(bufev->output) != 0)
931 efe7d180 2021-10-02 op event_add(&bufev->ev_write, NULL);
933 efe7d180 2021-10-02 op if (bufev->writecb != NULL &&
934 efe7d180 2021-10-02 op EVBUFFER_LENGTH(bufev->output) <= bufev->wm_write.low)
935 efe7d180 2021-10-02 op (*bufev->writecb)(bufev, bufev->cbarg);
939 efe7d180 2021-10-02 op event_add(&bufev->ev_write, NULL);
942 eae52ad4 2023-06-06 op log_warnx("tls error: %s", tls_error(client->ctx));
943 efe7d180 2021-10-02 op (*bufev->errorcb)(bufev, what, bufev->cbarg);
947 efe7d180 2021-10-02 op client_read(struct bufferevent *bev, void *d)
949 e2003e7e 2023-07-01 op struct stat sb;
950 efe7d180 2021-10-02 op struct client *c = d;
951 efe7d180 2021-10-02 op struct evbuffer *src = EVBUFFER_INPUT(bev);
952 e2003e7e 2023-07-01 op const char *path, *p, *parse_err = "invalid request";
953 efe7d180 2021-10-02 op char decoded[DOMAIN_NAME_LEN];
955 efe7d180 2021-10-02 op bufferevent_disable(bev, EVBUFFER_READ);
958 901905e0 2022-01-05 op * libevent2 can still somehow call this function, even
959 901905e0 2022-01-05 op * though I never enable EV_READ in the bufferevent. If
960 901905e0 2022-01-05 op * that's the case, bail out.
962 901905e0 2022-01-05 op if (c->type != REQUEST_UNDECIDED)
965 efe7d180 2021-10-02 op /* max url len + \r\n */
966 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(src) > 1024 + 2) {
967 eae52ad4 2023-06-06 op log_debug("too much data received");
968 efe7d180 2021-10-02 op start_reply(c, BAD_REQUEST, "bad request");
972 da0821b6 2023-06-24 op c->req = evbuffer_readln(src, &c->reqlen, EVBUFFER_EOL_CRLF_STRICT);
973 efe7d180 2021-10-02 op if (c->req == NULL) {
974 efe7d180 2021-10-02 op /* not enough data yet. */
975 efe7d180 2021-10-02 op bufferevent_enable(bev, EVBUFFER_READ);
978 ea27eaaa 2022-03-27 op if (c->reqlen > 1024+2) {
979 eae52ad4 2023-06-06 op log_debug("URL too long");
980 ea27eaaa 2022-03-27 op start_reply(c, BAD_REQUEST, "bad request");
984 efe7d180 2021-10-02 op if (!parse_iri(c->req, &c->iri, &parse_err) ||
985 efe7d180 2021-10-02 op !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
986 eae52ad4 2023-06-06 op log_debug("IRI parse error: %s", parse_err);
987 807a80cb 2021-10-06 op start_reply(c, BAD_REQUEST, "bad request");
991 72b033ef 2021-12-29 op if (apply_reverse_proxy(c))
994 52c92ef6 2021-12-09 op /* ignore the port number */
995 52c92ef6 2021-12-09 op if (strcmp(c->iri.schema, "gemini") ||
996 efe7d180 2021-10-02 op strcmp(decoded, c->domain)) {
997 efe7d180 2021-10-02 op start_reply(c, PROXY_REFUSED, "won't proxy request");
1001 efe7d180 2021-10-02 op if (apply_require_ca(c) ||
1002 efe7d180 2021-10-02 op apply_block_return(c)||
1003 efe7d180 2021-10-02 op apply_fastcgi(c))
1006 e2003e7e 2023-07-01 op path = c->iri.path;
1007 e2003e7e 2023-07-01 op p = strip_path(path, vhost_strip(c->host, path));
1008 e2003e7e 2023-07-01 op while (*p == '/')
1010 e2003e7e 2023-07-01 op if (*p == '\0')
1013 e2003e7e 2023-07-01 op c->pfd = openat(vhost_dirfd(c->host, path, &c->loc), p, O_RDONLY);
1014 e2003e7e 2023-07-01 op if (c->pfd == -1) {
1015 e2003e7e 2023-07-01 op if (errno == EACCES)
1016 e2003e7e 2023-07-01 op log_info("can't open %s: %s", p, strerror(errno));
1017 e2003e7e 2023-07-01 op start_reply(c, NOT_FOUND, "not found");
1021 e2003e7e 2023-07-01 op if (fstat(c->pfd, &sb) == -1) {
1022 e2003e7e 2023-07-01 op log_warnx("fstat %s", path);
1023 e2003e7e 2023-07-01 op start_reply(c, TEMP_FAILURE, "internal server error");
1027 e2003e7e 2023-07-01 op if (S_ISDIR(sb.st_mode)) {
1028 e2003e7e 2023-07-01 op open_dir(c);
1032 e2003e7e 2023-07-01 op c->type = REQUEST_FILE;
1033 e2003e7e 2023-07-01 op start_reply(c, SUCCESS, mime(c->conf, c->host, p));
1037 efe7d180 2021-10-02 op client_write(struct bufferevent *bev, void *d)
1039 efe7d180 2021-10-02 op struct client *c = d;
1040 efe7d180 2021-10-02 op struct evbuffer *out = EVBUFFER_OUTPUT(bev);
1041 f2f8eb35 2022-07-04 op char nam[PATH_MAX];
1042 efe7d180 2021-10-02 op char buf[BUFSIZ];
1045 efe7d180 2021-10-02 op switch (c->type) {
1046 efe7d180 2021-10-02 op case REQUEST_UNDECIDED:
1048 efe7d180 2021-10-02 op * Ignore spurious calls when we still don't have idea
1049 efe7d180 2021-10-02 op * what to do with the request.
1053 efe7d180 2021-10-02 op case REQUEST_FILE:
1054 efe7d180 2021-10-02 op if ((r = read(c->pfd, buf, sizeof(buf))) == -1) {
1055 eae52ad4 2023-06-06 op log_warn("read");
1056 efe7d180 2021-10-02 op client_error(bev, EVBUFFER_ERROR, c);
1058 efe7d180 2021-10-02 op } else if (r == 0) {
1059 efe7d180 2021-10-02 op client_close(c);
1061 efe7d180 2021-10-02 op } else if (r != sizeof(buf))
1062 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1063 efe7d180 2021-10-02 op bufferevent_write(bev, buf, r);
1066 efe7d180 2021-10-02 op case REQUEST_DIR:
1067 efe7d180 2021-10-02 op /* TODO: handle big big directories better */
1068 efe7d180 2021-10-02 op for (c->diroff = 0; c->diroff < c->dirlen; ++c->diroff) {
1069 543f4a66 2022-07-04 op const char *sufx = "";
1071 f2f8eb35 2022-07-04 op encode_path(nam, sizeof(nam),
1072 efe7d180 2021-10-02 op c->dir[c->diroff]->d_name);
1073 543f4a66 2022-07-04 op if (c->dir[c->diroff]->d_type == DT_DIR)
1075 543f4a66 2022-07-04 op evbuffer_add_printf(out, "=> ./%s%s\n", nam, sufx);
1076 efe7d180 2021-10-02 op free(c->dir[c->diroff]);
1078 efe7d180 2021-10-02 op free(c->dir);
1079 efe7d180 2021-10-02 op c->dir = NULL;
1081 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1083 efe7d180 2021-10-02 op event_add(&c->bev->ev_write, NULL);
1086 efe7d180 2021-10-02 op case REQUEST_FCGI:
1087 72b033ef 2021-12-29 op case REQUEST_PROXY:
1089 d29a2ee2 2022-09-06 op * Here we depend on fastcgi or proxy connection to
1090 d29a2ee2 2022-09-06 op * provide data.
1094 efe7d180 2021-10-02 op case REQUEST_DONE:
1095 efe7d180 2021-10-02 op if (EVBUFFER_LENGTH(out) == 0)
1096 efe7d180 2021-10-02 op client_close(c);
1102 efe7d180 2021-10-02 op client_error(struct bufferevent *bev, short error, void *d)
1104 efe7d180 2021-10-02 op struct client *c = d;
1106 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1108 efe7d180 2021-10-02 op if (error & EVBUFFER_TIMEOUT) {
1109 eae52ad4 2023-06-06 op log_debug("timeout; forcefully closing the connection");
1110 efe7d180 2021-10-02 op if (c->code == 0)
1111 efe7d180 2021-10-02 op start_reply(c, BAD_REQUEST, "timeout");
1113 efe7d180 2021-10-02 op client_close(c);
1117 efe7d180 2021-10-02 op if (error & EVBUFFER_EOF) {
1118 efe7d180 2021-10-02 op client_close(c);
1122 eae52ad4 2023-06-06 op log_warnx("unknown bufferevent error 0x%x", error);
1123 efe7d180 2021-10-02 op client_close(c);
1127 efe7d180 2021-10-02 op start_reply(struct client *c, int code, const char *meta)
1129 efe7d180 2021-10-02 op struct evbuffer *evb = EVBUFFER_OUTPUT(c->bev);
1130 efe7d180 2021-10-02 op const char *lang;
1133 efe7d180 2021-10-02 op bufferevent_enable(c->bev, EVBUFFER_WRITE);
1135 efe7d180 2021-10-02 op c->code = code;
1136 efe7d180 2021-10-02 op c->meta = meta;
1138 efe7d180 2021-10-02 op r = evbuffer_add_printf(evb, "%d %s", code, meta);
1139 efe7d180 2021-10-02 op if (r == -1)
1142 efe7d180 2021-10-02 op /* 2 digit status + space + 1024 max reply */
1143 efe7d180 2021-10-02 op if (r > 1027)
1144 efe7d180 2021-10-02 op goto overflow;
1146 d29a2ee2 2022-09-06 op if (c->type != REQUEST_FCGI &&
1147 72b033ef 2021-12-29 op c->type != REQUEST_PROXY &&
1148 efe7d180 2021-10-02 op !strcmp(meta, "text/gemini") &&
1149 efe7d180 2021-10-02 op (lang = vhost_lang(c->host, c->iri.path)) != NULL) {
1150 4842c72d 2021-10-18 op rr = evbuffer_add_printf(evb, ";lang=%s", lang);
1151 efe7d180 2021-10-02 op if (rr == -1)
1153 efe7d180 2021-10-02 op if (r + rr > 1027)
1154 efe7d180 2021-10-02 op goto overflow;
1157 efe7d180 2021-10-02 op bufferevent_write(c->bev, "\r\n", 2);
1159 efe7d180 2021-10-02 op if (!vhost_disable_log(c->host, c->iri.path))
1160 2c381068 2023-07-01 op log_request(c, code, meta);
1162 d28bd963 2022-01-27 op if (code != 20)
1163 efe7d180 2021-10-02 op c->type = REQUEST_DONE;
1168 eae52ad4 2023-06-06 op log_warnx("evbuffer_add_printf error: no memory");
1169 efe7d180 2021-10-02 op evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1170 390d312b 2023-08-09 op c->type = REQUEST_DONE;
1174 eae52ad4 2023-06-06 op log_warnx("reply header overflow");
1175 efe7d180 2021-10-02 op evbuffer_drain(evb, EVBUFFER_LENGTH(evb));
1176 efe7d180 2021-10-02 op start_reply(c, TEMP_FAILURE, "internal error");
1181 efe7d180 2021-10-02 op client_close_ev(int fd, short event, void *d)
1183 8ad1c570 2021-05-09 op struct client *c = d;
1185 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
1186 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
1187 efe7d180 2021-10-02 op event_once(c->fd, EV_READ, client_close_ev, c, NULL);
1189 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
1190 efe7d180 2021-10-02 op event_once(c->fd, EV_WRITE, client_close_ev, c, NULL);
1194 d3a08f4d 2021-01-17 op connected_clients--;
1196 e4daebe4 2021-10-06 op free(c->req);
1198 d3a08f4d 2021-01-17 op tls_free(c->ctx);
1199 d3a08f4d 2021-01-17 op c->ctx = NULL;
1201 efe7d180 2021-10-02 op free(c->header);
1203 abc007d2 2021-02-08 op if (c->pfd != -1)
1204 abc007d2 2021-02-08 op close(c->pfd);
1206 252908e6 2021-01-24 op if (c->dir != NULL)
1207 11c98667 2021-04-25 op free(c->dir);
1209 abc007d2 2021-02-08 op close(c->fd);
1216 72b033ef 2021-12-29 op client_proxy_close(int fd, short event, void *d)
1218 72b033ef 2021-12-29 op struct tls *ctx = d;
1220 72b033ef 2021-12-29 op if (ctx == NULL) {
1225 72b033ef 2021-12-29 op switch (tls_close(ctx)) {
1226 72b033ef 2021-12-29 op case TLS_WANT_POLLIN:
1227 72b033ef 2021-12-29 op event_once(fd, EV_READ, client_proxy_close, d, NULL);
1229 72b033ef 2021-12-29 op case TLS_WANT_POLLOUT:
1230 72b033ef 2021-12-29 op event_once(fd, EV_WRITE, client_proxy_close, d, NULL);
1234 72b033ef 2021-12-29 op tls_free(ctx);
1239 efe7d180 2021-10-02 op client_close(struct client *c)
1242 efe7d180 2021-10-02 op * We may end up calling client_close in various situations
1243 efe7d180 2021-10-02 op * and for the most unexpected reasons. Therefore, we need to
1244 b9b77f53 2022-01-27 op * ensure that everything gets properly released once we reach
1245 efe7d180 2021-10-02 op * this point.
1248 207b3e80 2021-10-07 op SPLAY_REMOVE(client_tree_id, &clients, c);
1250 efe7d180 2021-10-02 op if (c->cgibev != NULL) {
1251 efe7d180 2021-10-02 op bufferevent_disable(c->cgibev, EVBUFFER_READ|EVBUFFER_WRITE);
1252 efe7d180 2021-10-02 op bufferevent_free(c->cgibev);
1253 efe7d180 2021-10-02 op c->cgibev = NULL;
1254 efe7d180 2021-10-02 op close(c->pfd);
1255 efe7d180 2021-10-02 op c->pfd = -1;
1258 c5edb157 2023-06-24 op if (c->bev != NULL) {
1259 c5edb157 2023-06-24 op bufferevent_disable(c->bev, EVBUFFER_READ|EVBUFFER_WRITE);
1260 c5edb157 2023-06-24 op bufferevent_free(c->bev);
1263 e0f6dc64 2022-01-27 op if (c->proxyevset &&
1264 e0f6dc64 2022-01-27 op event_pending(&c->proxyev, EV_READ|EV_WRITE, NULL)) {
1265 e0f6dc64 2022-01-27 op c->proxyevset = 0;
1266 e0f6dc64 2022-01-27 op event_del(&c->proxyev);
1269 e0f6dc64 2022-01-27 op if (c->pfd != -1 && c->proxyctx != NULL) {
1270 e0f6dc64 2022-01-27 op /* shut down the proxy TLS connection */
1271 e0f6dc64 2022-01-27 op client_proxy_close(c->pfd, 0, c->proxyctx);
1272 e0f6dc64 2022-01-27 op c->pfd = -1;
1275 e0f6dc64 2022-01-27 op if (c->proxybev != NULL)
1276 e0f6dc64 2022-01-27 op bufferevent_free(c->proxybev);
1278 efe7d180 2021-10-02 op client_close_ev(c->fd, 0, c);
1282 71b02f63 2023-07-01 op server_accept(int sock, short et, void *d)
1284 509d0509 2023-06-23 op struct address *addr = d;
1285 abc007d2 2021-02-08 op struct client *c;
1286 37df23d1 2023-06-23 op struct sockaddr_storage raddr;
1287 37df23d1 2023-06-23 op struct sockaddr *sraddr;
1288 d3a08f4d 2021-01-17 op socklen_t len;
1291 37df23d1 2023-06-23 op sraddr = (struct sockaddr *)&raddr;
1292 37df23d1 2023-06-23 op len = sizeof(raddr);
1293 37df23d1 2023-06-23 op if ((fd = accept(sock, sraddr, &len)) == -1) {
1294 c62a411f 2021-10-13 op if (errno == EWOULDBLOCK || errno == EAGAIN ||
1295 c62a411f 2021-10-13 op errno == ECONNABORTED)
1297 df5058c9 2023-06-05 op fatal("accept");
1300 3cb3dd4d 2021-02-12 op mark_nonblock(fd);
1302 207b3e80 2021-10-07 op c = xcalloc(1, sizeof(*c));
1303 509d0509 2023-06-23 op c->conf = addr->conf;
1304 509d0509 2023-06-23 op c->addr = addr;
1305 207b3e80 2021-10-07 op c->id = ++server_client_id;
1307 207b3e80 2021-10-07 op c->pfd = -1;
1308 37df23d1 2023-06-23 op memcpy(&c->raddr, &raddr, sizeof(raddr));
1309 37df23d1 2023-06-23 op c->raddrlen = len;
1311 ed164e72 2023-06-26 op e = getnameinfo(sraddr, len, c->rhost, sizeof(c->rhost),
1312 ed164e72 2023-06-26 op c->rserv, sizeof(c->rserv), NI_NUMERICHOST | NI_NUMERICSERV);
1313 ed164e72 2023-06-26 op if (e != 0) {
1314 ed164e72 2023-06-26 op log_warnx("getnameinfo failed: %s", gai_strerror(e));
1315 ed164e72 2023-06-26 op close(c->fd);
1320 e50f85ad 2023-06-24 op if (tls_accept_socket(addr->ctx, &c->ctx, fd) == -1) {
1321 eae52ad4 2023-06-06 op log_warnx("failed to accept socket: %s", tls_error(c->ctx));
1322 207b3e80 2021-10-07 op close(c->fd);
1327 207b3e80 2021-10-07 op SPLAY_INSERT(client_tree_id, &clients, c);
1328 207b3e80 2021-10-07 op event_once(c->fd, EV_READ|EV_WRITE, handle_handshake, c, NULL);
1329 207b3e80 2021-10-07 op connected_clients++;
1333 abc007d2 2021-02-08 op handle_siginfo(int fd, short ev, void *d)
1335 eae52ad4 2023-06-06 op log_info("%d connected clients", connected_clients);
1339 e50f85ad 2023-06-24 op add_matching_kps(struct tls_config *tlsconf, struct address *addr,
1340 e50f85ad 2023-06-24 op struct conf *conf)
1342 e50f85ad 2023-06-24 op struct address *vaddr;
1343 e50f85ad 2023-06-24 op struct vhost *h;
1344 e50f85ad 2023-06-24 op int r, any = 0;
1346 e50f85ad 2023-06-24 op TAILQ_FOREACH(h, &conf->hosts, vhosts) {
1347 e50f85ad 2023-06-24 op TAILQ_FOREACH(vaddr, &h->addrs, addrs) {
1348 aa30aaed 2023-06-24 op if (!match_addr(addr, vaddr))
1353 e50f85ad 2023-06-24 op r = tls_config_set_keypair_ocsp_mem(tlsconf,
1354 e50f85ad 2023-06-24 op h->cert, h->certlen, h->key, h->keylen,
1355 e50f85ad 2023-06-24 op h->ocsp, h->ocsplen);
1357 e50f85ad 2023-06-24 op r = tls_config_add_keypair_ocsp_mem(tlsconf,
1358 e50f85ad 2023-06-24 op h->cert, h->certlen, h->key, h->keylen,
1359 e50f85ad 2023-06-24 op h->ocsp, h->ocsplen);
1362 e50f85ad 2023-06-24 op if (r == -1)
1363 e50f85ad 2023-06-24 op fatalx("failed to load keypair"
1364 e50f85ad 2023-06-24 op " for host %s: %s", h->domain,
1365 e50f85ad 2023-06-24 op tls_config_error(tlsconf));
1371 af1dab18 2023-06-09 op setup_tls(struct conf *conf)
1373 070b3295 2023-06-05 op struct tls_config *tlsconf;
1374 e50f85ad 2023-06-24 op struct address *addr;
1376 e50f85ad 2023-06-24 op TAILQ_FOREACH(addr, &conf->addrs, addrs) {
1377 e50f85ad 2023-06-24 op if ((tlsconf = tls_config_new()) == NULL)
1378 e50f85ad 2023-06-24 op fatal("tls_config_new");
1380 e50f85ad 2023-06-24 op if (conf->use_privsep_crypto)
1381 e50f85ad 2023-06-24 op tls_config_use_fake_private_key(tlsconf);
1383 e50f85ad 2023-06-24 op /* optionally accept client certs but don't verify */
1384 e50f85ad 2023-06-24 op tls_config_verify_client_optional(tlsconf);
1385 e50f85ad 2023-06-24 op tls_config_insecure_noverifycert(tlsconf);
1387 e50f85ad 2023-06-24 op if (tls_config_set_protocols(tlsconf, conf->protos) == -1)
1388 e50f85ad 2023-06-24 op fatalx("tls_config_set_protocols: %s",
1389 e50f85ad 2023-06-24 op tls_config_error(tlsconf));
1391 e50f85ad 2023-06-24 op add_matching_kps(tlsconf, addr, conf);
1393 e50f85ad 2023-06-24 op tls_reset(addr->ctx);
1394 e50f85ad 2023-06-24 op if (tls_configure(addr->ctx, tlsconf) == -1)
1395 e50f85ad 2023-06-24 op fatalx("tls_configure: %s", tls_error(addr->ctx));
1397 e50f85ad 2023-06-24 op tls_config_free(tlsconf);
1402 af1dab18 2023-06-09 op load_vhosts(struct conf *conf)
1404 a01a91db 2023-06-05 op struct vhost *h;
1405 a01a91db 2023-06-05 op struct location *l;
1406 e371817b 2024-01-09 op char path[PATH_MAX], *p;
1409 af1dab18 2023-06-09 op TAILQ_FOREACH(h, &conf->hosts, vhosts) {
1410 a01a91db 2023-06-05 op TAILQ_FOREACH(l, &h->locations, locations) {
1411 a01a91db 2023-06-05 op if (*l->dir == '\0')
1416 e371817b 2024-01-09 op if (conf->conftest && *conf->chroot != '\0') {
1417 e371817b 2024-01-09 op r = snprintf(path, sizeof(path), "%s/%s",
1418 e371817b 2024-01-09 op conf->chroot, l->dir);
1419 e371817b 2024-01-09 op if (r < 0 || (size_t)r >= sizeof(path))
1420 e371817b 2024-01-09 op fatalx("path too long: %s", l->dir);
1424 e371817b 2024-01-09 op l->dirfd = open(p, O_RDONLY | O_DIRECTORY);
1425 a01a91db 2023-06-05 op if (l->dirfd == -1)
1426 df5058c9 2023-06-05 op fatal("open %s for domain %s", l->dir,
1433 c26f2460 2023-06-08 op server(struct privsep *ps, struct privsep_proc *p)
1435 c26f2460 2023-06-08 op proc_run(ps, p, procs, nitems(procs), server_init, NULL);
1439 c26f2460 2023-06-08 op server_init(struct privsep *ps, struct privsep_proc *p, void *arg)
1441 ba290ef3 2023-06-11 op struct conf *c;
1443 c26f2460 2023-06-08 op SPLAY_INIT(&clients);
1445 c26f2460 2023-06-08 op #ifdef SIGINFO
1446 c26f2460 2023-06-08 op signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
1447 c26f2460 2023-06-08 op signal_add(&siginfo, NULL);
1449 c26f2460 2023-06-08 op signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
1450 c26f2460 2023-06-08 op signal_add(&sigusr2, NULL);
1452 c26f2460 2023-06-08 op sandbox_server_process();
1455 81634643 2023-08-11 op * gemexp doesn't use the privsep crypto engine; it doesn't
1456 81634643 2023-08-11 op * use privsep at all so `ps' is NULL.
1458 ba290ef3 2023-06-11 op if (ps != NULL) {
1459 ba290ef3 2023-06-11 op c = ps->ps_env;
1460 ba290ef3 2023-06-11 op if (c->use_privsep_crypto)
1461 ba290ef3 2023-06-11 op crypto_engine_init(ps->ps_env);
1466 3886afce 2023-06-08 op server_configure_done(struct conf *conf)
1468 509d0509 2023-06-23 op struct address *addr;
1470 3886afce 2023-06-08 op if (load_default_mime(&conf->mime) == -1)
1471 3886afce 2023-06-08 op fatal("can't load default mime");
1472 3886afce 2023-06-08 op sort_mime(&conf->mime);
1473 af1dab18 2023-06-09 op setup_tls(conf);
1474 af1dab18 2023-06-09 op load_vhosts(conf);
1476 509d0509 2023-06-23 op TAILQ_FOREACH(addr, &conf->addrs, addrs) {
1477 509d0509 2023-06-23 op if (addr->sock != -1)
1478 509d0509 2023-06-23 op event_add(&addr->evsock, NULL);
1485 c26f2460 2023-06-08 op server_dispatch_parent(int fd, struct privsep_proc *p, struct imsg *imsg)
1487 c26f2460 2023-06-08 op struct privsep *ps = p->p_ps;
1488 c26f2460 2023-06-08 op struct conf *conf = ps->ps_env;
1490 83a2644b 2024-01-21 op switch (imsg_get_type(imsg)) {
1491 c26f2460 2023-06-08 op case IMSG_RECONF_START:
1492 26df5098 2023-08-03 op case IMSG_RECONF_LOG_FMT:
1493 c26f2460 2023-06-08 op case IMSG_RECONF_MIME:
1494 c26f2460 2023-06-08 op case IMSG_RECONF_PROTOS:
1495 509d0509 2023-06-23 op case IMSG_RECONF_SOCK:
1496 c26f2460 2023-06-08 op case IMSG_RECONF_FCGI:
1497 c26f2460 2023-06-08 op case IMSG_RECONF_HOST:
1498 c26f2460 2023-06-08 op case IMSG_RECONF_CERT:
1499 c26f2460 2023-06-08 op case IMSG_RECONF_KEY:
1500 c26f2460 2023-06-08 op case IMSG_RECONF_OCSP:
1501 a0a42860 2023-06-24 op case IMSG_RECONF_HOST_ADDR:
1502 c26f2460 2023-06-08 op case IMSG_RECONF_LOC:
1503 c26f2460 2023-06-08 op case IMSG_RECONF_ENV:
1504 c26f2460 2023-06-08 op case IMSG_RECONF_ALIAS:
1505 c26f2460 2023-06-08 op case IMSG_RECONF_PROXY:
1506 deadd9e1 2023-06-09 op case IMSG_RECONF_PROXY_CERT:
1507 deadd9e1 2023-06-09 op case IMSG_RECONF_PROXY_KEY:
1508 c26f2460 2023-06-08 op return config_recv(conf, imsg);
1509 c26f2460 2023-06-08 op case IMSG_RECONF_END:
1510 c26f2460 2023-06-08 op if (config_recv(conf, imsg) == -1)
1512 3886afce 2023-06-08 op if (server_configure_done(conf) == -1)
1523 86693a33 2023-06-11 op server_dispatch_crypto(int fd, struct privsep_proc *p, struct imsg *imsg)
1529 c26f2460 2023-06-08 op server_dispatch_logger(int fd, struct privsep_proc *p, struct imsg *imsg)
1535 207b3e80 2021-10-07 op client_tree_cmp(struct client *a, struct client *b)
1537 207b3e80 2021-10-07 op if (a->id == b->id)
1539 207b3e80 2021-10-07 op else if (a->id < b->id)
1545 207b3e80 2021-10-07 op SPLAY_GENERATE(client_tree_id, client, entry, client_tree_cmp)