2 d3a08f4d 2021-01-17 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 d3a08f4d 2021-01-17 op * Permission to use, copy, modify, and distribute this software for any
5 d3a08f4d 2021-01-17 op * purpose with or without fee is hereby granted, provided that the above
6 d3a08f4d 2021-01-17 op * copyright notice and this permission notice appear in all copies.
8 d3a08f4d 2021-01-17 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d3a08f4d 2021-01-17 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d3a08f4d 2021-01-17 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d3a08f4d 2021-01-17 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d3a08f4d 2021-01-17 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d3a08f4d 2021-01-17 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d3a08f4d 2021-01-17 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 52418c8d 2021-02-12 op #include "gmid.h"
19 d3a08f4d 2021-01-17 op #include <sys/stat.h>
21 d3a08f4d 2021-01-17 op #include <netdb.h>
23 d3a08f4d 2021-01-17 op #include <assert.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 abc007d2 2021-02-08 op struct server {
32 abc007d2 2021-02-08 op struct client clients[MAX_USERS];
33 abc007d2 2021-02-08 op struct tls *ctx;
36 a6e689d7 2021-02-12 op struct event e4, e6, sighup, siginfo, sigusr2;
37 a6e689d7 2021-02-12 op int has_ipv6, has_siginfo;
39 d3a08f4d 2021-01-17 op int connected_clients;
41 49b73ba1 2021-02-10 op static inline int matches(const char*, const char*);
43 abc007d2 2021-02-08 op static inline void reschedule_read(int, struct client*, statefn);
44 abc007d2 2021-02-08 op static inline void reschedule_write(int, struct client*, statefn);
46 fe406389 2021-02-02 op static int check_path(struct client*, const char*, int*);
47 abc007d2 2021-02-08 op static void open_file(struct client*);
48 abc007d2 2021-02-08 op static void check_for_cgi(struct client*);
49 abc007d2 2021-02-08 op static void handle_handshake(int, short, void*);
50 57ec3e77 2021-02-08 op static char *strip_path(char*, int);
51 57ec3e77 2021-02-08 op static void fmt_sbuf(const char*, struct client*, const char*);
52 abc007d2 2021-02-08 op static int apply_block_return(struct client*);
53 02be96c6 2021-02-09 op static int apply_require_ca(struct client*);
54 abc007d2 2021-02-08 op static void handle_open_conn(int, short, void*);
55 abc007d2 2021-02-08 op static void start_reply(struct client*, int, const char*);
56 abc007d2 2021-02-08 op static void handle_start_reply(int, short, void*);
57 abc007d2 2021-02-08 op static void start_cgi(const char*, const char*, struct client*);
58 abc007d2 2021-02-08 op static void open_dir(struct client*);
59 abc007d2 2021-02-08 op static void redirect_canonical_dir(struct client*);
60 abc007d2 2021-02-08 op static void enter_handle_dirlist(int, short, void*);
61 abc007d2 2021-02-08 op static void handle_dirlist(int, short, void*);
62 fe406389 2021-02-02 op static int read_next_dir_entry(struct client*);
63 abc007d2 2021-02-08 op static void send_directory_listing(int, short, void*);
64 abc007d2 2021-02-08 op static void handle_cgi_reply(int, short, void*);
65 27b2fa9a 2021-02-12 op static void handle_copy(int, short, void*);
66 abc007d2 2021-02-08 op static void close_conn(int, short, void*);
67 abc007d2 2021-02-08 op static void do_accept(int, short, void*);
68 abc007d2 2021-02-08 op static void handle_sighup(int, short, void*);
70 49b73ba1 2021-02-10 op static inline int
71 49b73ba1 2021-02-10 op matches(const char *pattern, const char *path)
73 49b73ba1 2021-02-10 op if (*path == '/')
75 49b73ba1 2021-02-10 op return !fnmatch(pattern, path, 0);
78 abc007d2 2021-02-08 op static inline void
79 abc007d2 2021-02-08 op reschedule_read(int fd, struct client *c, statefn fn)
81 abc007d2 2021-02-08 op event_once(fd, EV_READ, fn, c, NULL);
85 abc007d2 2021-02-08 op reschedule_write(int fd, struct client *c, statefn fn)
87 abc007d2 2021-02-08 op event_once(fd, EV_WRITE, fn, c, NULL);
91 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
93 c8b74339 2021-01-24 op struct location *loc;
95 caad0308 2021-01-27 op if (v == NULL || path == NULL)
98 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
99 cd761624 2021-02-06 op if (loc->lang != NULL) {
100 49b73ba1 2021-02-10 op if (matches(loc->match, path))
101 6016a593 2021-01-30 op return loc->lang;
105 6016a593 2021-01-30 op return v->locations[0].lang;
109 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
111 c8b74339 2021-01-24 op struct location *loc;
112 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
114 caad0308 2021-01-27 op if (v == NULL || path == NULL)
115 caad0308 2021-01-27 op return default_mime;
117 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
118 cd761624 2021-02-06 op if (loc->default_mime != NULL) {
119 49b73ba1 2021-02-10 op if (matches(loc->match, path))
120 6016a593 2021-01-30 op return loc->default_mime;
124 6016a593 2021-01-30 op if (v->locations[0].default_mime != NULL)
125 6016a593 2021-01-30 op return v->locations[0].default_mime;
126 c8b74339 2021-01-24 op return default_mime;
130 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
132 c8b74339 2021-01-24 op struct location *loc;
133 c8b74339 2021-01-24 op const char *index = "index.gmi";
135 caad0308 2021-01-27 op if (v == NULL || path == NULL)
136 caad0308 2021-01-27 op return index;
138 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
139 cd761624 2021-02-06 op if (loc->index != NULL) {
140 49b73ba1 2021-02-10 op if (matches(loc->match, path))
141 6016a593 2021-01-30 op return loc->index;
145 b59f3cdd 2021-01-30 op if (v->locations[0].index != NULL)
146 b59f3cdd 2021-01-30 op return v->locations[0].index;
147 c8b74339 2021-01-24 op return index;
151 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
153 252908e6 2021-01-24 op struct location *loc;
155 6016a593 2021-01-30 op if (v == NULL || path == NULL)
158 daac4a94 2021-02-06 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
159 cd761624 2021-02-06 op if (loc->auto_index != 0) {
160 49b73ba1 2021-02-10 op if (matches(loc->match, path))
161 6016a593 2021-01-30 op return loc->auto_index == 1;
165 6016a593 2021-01-30 op return v->locations[0].auto_index == 1;
169 6abda252 2021-02-06 op vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
171 6abda252 2021-02-06 op struct location *loc;
173 6abda252 2021-02-06 op if (v == NULL || path == NULL)
176 6abda252 2021-02-06 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
177 cd761624 2021-02-06 op if (loc->block_code != 0) {
178 49b73ba1 2021-02-10 op if (matches(loc->match, path)) {
179 6abda252 2021-02-06 op *code = loc->block_code;
180 6abda252 2021-02-06 op *fmt = loc->block_fmt;
186 6abda252 2021-02-06 op *code = v->locations[0].block_code;
187 6abda252 2021-02-06 op *fmt = v->locations[0].block_fmt;
188 6abda252 2021-02-06 op return v->locations[0].block_code != 0;
192 6abda252 2021-02-06 op vhost_strip(struct vhost *v, const char *path)
194 6abda252 2021-02-06 op struct location *loc;
196 6abda252 2021-02-06 op if (v == NULL || path == NULL)
199 6abda252 2021-02-06 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
200 cd761624 2021-02-06 op if (loc->strip != 0) {
201 49b73ba1 2021-02-10 op if (matches(loc->match, path))
202 6abda252 2021-02-06 op return loc->strip;
206 6abda252 2021-02-06 op return v->locations[0].strip;
210 02be96c6 2021-02-09 op vhost_require_ca(struct vhost *v, const char *path)
212 02be96c6 2021-02-09 op struct location *loc;
214 02be96c6 2021-02-09 op if (v == NULL || path == NULL)
217 02be96c6 2021-02-09 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
218 02be96c6 2021-02-09 op if (loc->reqca != NULL) {
219 49b73ba1 2021-02-10 op if (matches(loc->match, path))
220 02be96c6 2021-02-09 op return loc->reqca;
224 02be96c6 2021-02-09 op return v->locations[0].reqca;
228 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
230 d3a08f4d 2021-01-17 op struct stat sb;
231 d1ca3911 2021-01-21 op const char *p;
234 d3a08f4d 2021-01-17 op assert(path != NULL);
236 d1ca3911 2021-01-21 op if (*path == '\0')
238 d1ca3911 2021-01-21 op else if (*path == '/')
239 d1ca3911 2021-01-21 op /* in send_dir we add an initial / (to be
240 d1ca3911 2021-01-21 op * redirect-friendly), but here we want to skip it */
245 252908e6 2021-01-24 op flags = O_RDONLY | O_NOFOLLOW;
247 252908e6 2021-01-24 op if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
248 d3a08f4d 2021-01-17 op return FILE_MISSING;
250 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
251 3abf91b0 2021-02-07 op log_notice(c, "failed stat for %s: %s", path, strerror(errno));
252 d3a08f4d 2021-01-17 op return FILE_MISSING;
255 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
256 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
258 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
259 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
261 d3a08f4d 2021-01-17 op return FILE_EXISTS;
265 abc007d2 2021-02-08 op open_file(struct client *c)
267 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
268 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
269 87f2b68b 2021-02-02 op if (c->host->cgi != NULL && !fnmatch(c->host->cgi, c->iri.path, 0)) {
270 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
274 d3a08f4d 2021-01-17 op /* fallthrough */
276 d3a08f4d 2021-01-17 op case FILE_EXISTS:
277 27b2fa9a 2021-02-12 op c->next = handle_copy;
278 27b2fa9a 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
281 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
285 d3a08f4d 2021-01-17 op case FILE_MISSING:
286 87f2b68b 2021-02-02 op if (c->host->cgi != NULL && !fnmatch(c->host->cgi, c->iri.path, 0)) {
287 abc007d2 2021-02-08 op check_for_cgi(c);
290 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
294 d3a08f4d 2021-01-17 op /* unreachable */
300 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
301 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
302 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
303 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
304 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
307 abc007d2 2021-02-08 op check_for_cgi(struct client *c)
309 2fafa2d2 2021-02-01 op char path[PATH_MAX];
312 2fafa2d2 2021-02-01 op strlcpy(path, c->iri.path, sizeof(path));
313 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
315 d3a08f4d 2021-01-17 op while (end > path) {
316 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
317 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
318 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
319 d3a08f4d 2021-01-17 op while (*end != '/')
323 abc007d2 2021-02-08 op switch (check_path(c, path, &c->pfd)) {
324 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
325 abc007d2 2021-02-08 op start_cgi(path, end+1, c);
327 d3a08f4d 2021-01-17 op case FILE_MISSING:
338 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
343 9b8f5ed2 2021-02-03 op mark_nonblock(int fd)
347 9b8f5ed2 2021-02-03 op if ((flags = fcntl(fd, F_GETFL)) == -1)
348 9b8f5ed2 2021-02-03 op fatal("fcntl(F_GETFL): %s", strerror(errno));
349 9b8f5ed2 2021-02-03 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
350 9b8f5ed2 2021-02-03 op fatal("fcntl(F_SETFL): %s", strerror(errno));
354 abc007d2 2021-02-08 op handle_handshake(int fd, short ev, void *d)
356 abc007d2 2021-02-08 op struct client *c = d;
357 d3a08f4d 2021-01-17 op struct vhost *h;
358 d3a08f4d 2021-01-17 op const char *servname;
359 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
361 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
362 d3a08f4d 2021-01-17 op case 0: /* success */
363 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
365 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
366 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_handshake);
368 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
369 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_handshake);
372 d3a08f4d 2021-01-17 op /* unreachable */
376 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
377 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
378 3abf91b0 2021-02-07 op log_info(c, "puny_decode: %s", parse_err);
382 d3a08f4d 2021-01-17 op for (h = hosts; h->domain != NULL; ++h) {
383 3300cbe0 2021-01-27 op if (!fnmatch(h->domain, c->domain, 0))
387 3abf91b0 2021-02-07 op log_debug(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
388 3abf91b0 2021-02-07 op servname != NULL ? servname : "(null)",
390 3abf91b0 2021-02-07 op h->domain != NULL ? h->domain : "(null)");
392 d3a08f4d 2021-01-17 op if (h->domain != NULL) {
394 abc007d2 2021-02-08 op handle_open_conn(fd, ev, c);
399 0ab65593 2021-01-21 op if (servname != NULL)
400 0ab65593 2021-01-21 op strncpy(c->req, servname, sizeof(c->req));
402 0ab65593 2021-01-21 op strncpy(c->req, "null", sizeof(c->req));
404 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
407 57ec3e77 2021-02-08 op static char *
408 57ec3e77 2021-02-08 op strip_path(char *path, int strip)
412 6abda252 2021-02-06 op while (strip > 0) {
413 6abda252 2021-02-06 op if ((t = strchr(path, '/')) == NULL) {
414 6abda252 2021-02-06 op path = strchr(path, '\0');
425 57ec3e77 2021-02-08 op fmt_sbuf(const char *fmt, struct client *c, const char *path)
428 57ec3e77 2021-02-08 op char buf[32];
430 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
431 6abda252 2021-02-06 op for (i = 0; *fmt; ++fmt) {
432 6abda252 2021-02-06 op if (i == sizeof(buf)-1 || *fmt == '%') {
433 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
434 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
438 6abda252 2021-02-06 op if (*fmt != '%') {
439 6abda252 2021-02-06 op buf[i++] = *fmt;
443 6abda252 2021-02-06 op switch (*++fmt) {
445 6abda252 2021-02-06 op strlcat(c->sbuf, "%", sizeof(c->sbuf));
448 6abda252 2021-02-06 op strlcat(c->sbuf, path, sizeof(c->sbuf));
451 6abda252 2021-02-06 op strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
454 6abda252 2021-02-06 op snprintf(buf, sizeof(buf), "%d", conf.port);
455 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
456 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
459 6abda252 2021-02-06 op strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
462 6abda252 2021-02-06 op fatal("%s: unknown fmt specifier %c",
463 6abda252 2021-02-06 op __func__, *fmt);
468 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
471 57ec3e77 2021-02-08 op /* 1 if a matching `block return' (and apply it), 0 otherwise */
473 57ec3e77 2021-02-08 op apply_block_return(struct client *c)
475 57ec3e77 2021-02-08 op const char *fmt, *path;
478 57ec3e77 2021-02-08 op if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
481 57ec3e77 2021-02-08 op path = strip_path(c->iri.path, vhost_strip(c->host, c->iri.path));
482 57ec3e77 2021-02-08 op fmt_sbuf(fmt, c, path);
484 abc007d2 2021-02-08 op start_reply(c, code, c->sbuf);
488 02be96c6 2021-02-09 op /* 1 if matching `require client ca' fails (and apply it), 0 otherwise */
490 02be96c6 2021-02-09 op apply_require_ca(struct client *c)
492 02be96c6 2021-02-09 op X509_STORE *store;
493 02be96c6 2021-02-09 op const uint8_t *cert;
496 02be96c6 2021-02-09 op if ((store = vhost_require_ca(c->host, c->iri.path)) == NULL)
499 02be96c6 2021-02-09 op if (!tls_peer_cert_provided(c->ctx)) {
500 02be96c6 2021-02-09 op start_reply(c, CLIENT_CERT_REQ, "client certificate required");
504 02be96c6 2021-02-09 op cert = tls_peer_cert_chain_pem(c->ctx, &len);
505 02be96c6 2021-02-09 op if (!validate_against_ca(store, cert, len)) {
506 02be96c6 2021-02-09 op start_reply(c, CERT_NOT_AUTH, "certificate not authorised");
514 abc007d2 2021-02-08 op handle_open_conn(int fd, short ev, void *d)
516 abc007d2 2021-02-08 op struct client *c = d;
517 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
518 3300cbe0 2021-01-27 op char decoded[DOMAIN_NAME_LEN];
520 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
521 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
523 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
525 3abf91b0 2021-02-07 op log_err(c, "tls_read: %s", tls_error(c->ctx));
526 abc007d2 2021-02-08 op close_conn(fd, ev, c);
529 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
530 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_open_conn);
533 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
534 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_open_conn);
538 c4f682f8 2021-01-27 op if (!trim_req_iri(c->req, &parse_err)
539 a2fd8013 2021-01-29 op || !parse_iri(c->req, &c->iri, &parse_err)
540 a2fd8013 2021-01-29 op || !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
541 3abf91b0 2021-02-07 op log_info(c, "iri parse error: %s", parse_err);
542 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "invalid request");
546 3300cbe0 2021-01-27 op if (c->iri.port_no != conf.port
547 3300cbe0 2021-01-27 op || strcmp(c->iri.schema, "gemini")
548 3300cbe0 2021-01-27 op || strcmp(decoded, c->domain)) {
549 abc007d2 2021-02-08 op start_reply(c, PROXY_REFUSED, "won't proxy request");
553 02be96c6 2021-02-09 op if (apply_require_ca(c))
556 abc007d2 2021-02-08 op if (apply_block_return(c))
559 e3ddf390 2021-02-06 op if (c->host->entrypoint != NULL) {
560 abc007d2 2021-02-08 op start_cgi(c->host->entrypoint, c->iri.path, c);
564 abc007d2 2021-02-08 op open_file(c);
568 abc007d2 2021-02-08 op start_reply(struct client *c, int code, const char *meta)
570 112802ea 2021-02-01 op c->code = code;
571 112802ea 2021-02-01 op c->meta = meta;
572 abc007d2 2021-02-08 op handle_start_reply(c->fd, 0, c);
576 abc007d2 2021-02-08 op handle_start_reply(int fd, short ev, void *d)
578 abc007d2 2021-02-08 op struct client *c = d;
579 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
580 c8b74339 2021-01-24 op const char *lang;
583 c8b74339 2021-01-24 op lang = vhost_lang(c->host, c->iri.path);
585 112802ea 2021-02-01 op snprintf(buf, sizeof(buf), "%d ", c->code);
586 112802ea 2021-02-01 op strlcat(buf, c->meta, sizeof(buf));
587 112802ea 2021-02-01 op if (!strcmp(c->meta, "text/gemini") && lang != NULL) {
588 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
589 c8b74339 2021-01-24 op strlcat(buf, lang, sizeof(buf));
592 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
593 0be51733 2021-01-20 op assert(len < sizeof(buf));
595 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
597 abc007d2 2021-02-08 op close_conn(fd, ev, c);
599 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
600 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_start_reply);
602 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
603 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_start_reply);
607 a87f6625 2021-01-24 op log_request(c, buf, sizeof(buf));
609 abc007d2 2021-02-08 op if (c->code != SUCCESS)
610 abc007d2 2021-02-08 op close_conn(fd, ev, c);
612 abc007d2 2021-02-08 op c->next(fd, ev, c);
616 abc007d2 2021-02-08 op start_cgi(const char *spath, const char *relpath, struct client *c)
618 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
621 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
622 d3a08f4d 2021-01-17 op addr, sizeof(addr),
624 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
628 2fafa2d2 2021-02-01 op if (!send_iri(exfd, &c->iri)
629 2fafa2d2 2021-02-01 op || !send_string(exfd, spath)
630 d3a08f4d 2021-01-17 op || !send_string(exfd, relpath)
631 d3a08f4d 2021-01-17 op || !send_string(exfd, addr)
632 b63e30ff 2021-02-07 op || !send_string(exfd, tls_peer_cert_subject(c->ctx))
633 b63e30ff 2021-02-07 op || !send_string(exfd, tls_peer_cert_issuer(c->ctx))
634 b63e30ff 2021-02-07 op || !send_string(exfd, tls_peer_cert_hash(c->ctx))
635 b63e30ff 2021-02-07 op || !send_time(exfd, tls_peer_cert_notbefore(c->ctx))
636 b63e30ff 2021-02-07 op || !send_time(exfd, tls_peer_cert_notafter(c->ctx))
637 d3a08f4d 2021-01-17 op || !send_vhost(exfd, c->host))
640 abc007d2 2021-02-08 op close(c->pfd);
641 abc007d2 2021-02-08 op if ((c->pfd = recv_fd(exfd)) == -1) {
642 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
646 abc007d2 2021-02-08 op reschedule_read(c->pfd, c, &handle_cgi_reply);
650 d3a08f4d 2021-01-17 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
651 132cae8c 2021-01-18 op fatal("cannot talk to the executor process");
655 abc007d2 2021-02-08 op open_dir(struct client *c)
659 252908e6 2021-01-24 op char *before_file;
661 0be51733 2021-01-20 op len = strlen(c->iri.path);
662 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
663 abc007d2 2021-02-08 op redirect_canonical_dir(c);
667 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
668 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
669 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
670 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
671 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
672 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
673 c8b74339 2021-01-24 op sizeof(c->sbuf));
674 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
675 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
679 252908e6 2021-01-24 op c->iri.path = c->sbuf;
681 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
682 abc007d2 2021-02-08 op dirfd = c->pfd;
685 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
686 252908e6 2021-01-24 op case FILE_EXECUTABLE:
687 87f2b68b 2021-02-02 op if (c->host->cgi != NULL && !fnmatch(c->host->cgi, c->iri.path, 0)) {
688 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
692 252908e6 2021-01-24 op /* fallthrough */
694 252908e6 2021-01-24 op case FILE_EXISTS:
695 98ee8406 2021-02-12 op c->next = handle_copy;
696 98ee8406 2021-02-12 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
699 252908e6 2021-01-24 op case FILE_DIRECTORY:
700 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
703 252908e6 2021-01-24 op case FILE_MISSING:
704 252908e6 2021-01-24 op *before_file = '\0';
706 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
707 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
711 abc007d2 2021-02-08 op c->pfd = dirfd;
712 5f715ce4 2021-02-02 op c->next = enter_handle_dirlist;
714 abc007d2 2021-02-08 op if ((c->dir = fdopendir(c->pfd)) == NULL) {
715 3abf91b0 2021-02-07 op log_err(c, "fdopendir(%d) (vhost:%s) %s: %s",
716 abc007d2 2021-02-08 op c->pfd, c->host->domain, c->iri.path, strerror(errno));
717 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
722 abc007d2 2021-02-08 op start_reply(c, SUCCESS, "text/gemini");
726 252908e6 2021-01-24 op /* unreachable */
730 252908e6 2021-01-24 op close(dirfd);
734 abc007d2 2021-02-08 op redirect_canonical_dir(struct client *c)
738 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
739 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
740 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
742 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
743 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
747 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
751 abc007d2 2021-02-08 op enter_handle_dirlist(int fd, short ev, void *d)
753 abc007d2 2021-02-08 op struct client *c = d;
754 5f715ce4 2021-02-02 op char b[PATH_MAX];
757 5f715ce4 2021-02-02 op strlcpy(b, c->iri.path, sizeof(b));
758 5f715ce4 2021-02-02 op l = snprintf(c->sbuf, sizeof(c->sbuf),
759 5f715ce4 2021-02-02 op "# Index of %s\n\n", b);
760 5f715ce4 2021-02-02 op if (l >= sizeof(c->sbuf)) {
761 5f715ce4 2021-02-02 op /* this is impossible, given that we have enough space
762 5f715ce4 2021-02-02 op * in c->sbuf to hold the ancilliary string plus the
763 5f715ce4 2021-02-02 op * full path; but it wouldn't read nice without some
764 5f715ce4 2021-02-02 op * error checking, and I'd like to avoid a strlen. */
765 abc007d2 2021-02-08 op close_conn(fd, ev, c);
770 abc007d2 2021-02-08 op handle_dirlist(fd, ev, c);
774 abc007d2 2021-02-08 op handle_dirlist(int fd, short ev, void *d)
776 abc007d2 2021-02-08 op struct client *c = d;
779 5f715ce4 2021-02-02 op while (c->len > 0) {
780 5f715ce4 2021-02-02 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
782 abc007d2 2021-02-08 op close_conn(fd, ev, c);
784 5f715ce4 2021-02-02 op case TLS_WANT_POLLOUT:
785 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_dirlist);
787 5f715ce4 2021-02-02 op case TLS_WANT_POLLIN:
788 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_dirlist);
796 abc007d2 2021-02-08 op send_directory_listing(fd, ev, c);
800 252908e6 2021-01-24 op read_next_dir_entry(struct client *c)
802 252908e6 2021-01-24 op struct dirent *d;
806 252908e6 2021-01-24 op if ((d = readdir(c->dir)) == NULL) {
807 252908e6 2021-01-24 op if (errno != 0)
808 3abf91b0 2021-02-07 op log_err(c, "readdir: %s", strerror(errno));
811 252908e6 2021-01-24 op } while (!strcmp(d->d_name, "."));
813 252908e6 2021-01-24 op /* XXX: url escape */
814 252908e6 2021-01-24 op snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
815 252908e6 2021-01-24 op d->d_name, d->d_name);
816 252908e6 2021-01-24 op c->len = strlen(c->sbuf);
823 abc007d2 2021-02-08 op send_directory_listing(int fd, short ev, void *d)
825 abc007d2 2021-02-08 op struct client *c = d;
829 252908e6 2021-01-24 op if (c->len == 0) {
830 252908e6 2021-01-24 op if (!read_next_dir_entry(c))
834 252908e6 2021-01-24 op while (c->len > 0) {
835 252908e6 2021-01-24 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
839 252908e6 2021-01-24 op case TLS_WANT_POLLOUT:
840 abc007d2 2021-02-08 op reschedule_read(fd, c, &send_directory_listing);
843 252908e6 2021-01-24 op case TLS_WANT_POLLIN:
844 abc007d2 2021-02-08 op reschedule_write(fd, c, &send_directory_listing);
856 abc007d2 2021-02-08 op close_conn(fd, ev, d);
859 35744950 2021-02-01 op /* accumulate the meta line from the cgi script. */
861 abc007d2 2021-02-08 op handle_cgi_reply(int fd, short ev, void *d)
863 abc007d2 2021-02-08 op struct client *c = d;
864 35744950 2021-02-01 op void *buf, *e;
869 35744950 2021-02-01 op buf = c->sbuf + c->len;
870 35744950 2021-02-01 op len = sizeof(c->sbuf) - c->len;
872 abc007d2 2021-02-08 op r = read(c->pfd, buf, len);
873 35744950 2021-02-01 op if (r == 0 || r == -1) {
874 abc007d2 2021-02-08 op start_reply(c, CGI_ERROR, "CGI error");
880 35744950 2021-02-01 op /* TODO: error if the CGI script don't reply correctly */
881 35744950 2021-02-01 op e = strchr(c->sbuf, '\n');
882 35744950 2021-02-01 op if (e != NULL || c->len == sizeof(c->sbuf)) {
883 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
886 27b2fa9a 2021-02-12 op handle_copy(fd, ev, c);
890 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_cgi_reply);
894 27b2fa9a 2021-02-12 op handle_copy(int fd, short ev, void *d)
896 abc007d2 2021-02-08 op struct client *c = d;
900 d3a08f4d 2021-01-17 op while (c->len > 0) {
901 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
905 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
906 27b2fa9a 2021-02-12 op reschedule_write(c->fd, c, &handle_copy);
909 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
910 27b2fa9a 2021-02-12 op reschedule_read(c->fd, c, &handle_copy);
920 27b2fa9a 2021-02-12 op switch (r = read(c->pfd, c->sbuf, sizeof(c->sbuf))) {
924 27b2fa9a 2021-02-12 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
925 27b2fa9a 2021-02-12 op reschedule_read(c->pfd, c, &handle_copy);
936 abc007d2 2021-02-08 op close_conn(c->fd, ev, d);
940 abc007d2 2021-02-08 op close_conn(int fd, short ev, void *d)
942 abc007d2 2021-02-08 op struct client *c = d;
944 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
945 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
946 abc007d2 2021-02-08 op reschedule_read(fd, c, &close_conn);
948 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
949 abc007d2 2021-02-08 op reschedule_read(fd, c, &close_conn);
953 d3a08f4d 2021-01-17 op connected_clients--;
955 d3a08f4d 2021-01-17 op tls_free(c->ctx);
956 d3a08f4d 2021-01-17 op c->ctx = NULL;
958 abc007d2 2021-02-08 op if (c->pfd != -1)
959 abc007d2 2021-02-08 op close(c->pfd);
961 252908e6 2021-01-24 op if (c->dir != NULL)
962 252908e6 2021-01-24 op closedir(c->dir);
964 abc007d2 2021-02-08 op close(c->fd);
969 abc007d2 2021-02-08 op do_accept(int sock, short et, void *d)
971 abc007d2 2021-02-08 op struct client *c;
972 abc007d2 2021-02-08 op struct server *s = d;
973 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
974 abc007d2 2021-02-08 op struct sockaddr *saddr;
975 d3a08f4d 2021-01-17 op socklen_t len;
981 abc007d2 2021-02-08 op saddr = (struct sockaddr*)&addr;
982 d3a08f4d 2021-01-17 op len = sizeof(addr);
983 3cb3dd4d 2021-02-12 op if ((fd = accept(sock, saddr, &len)) == -1) {
984 cfb8a77f 2021-02-07 op if (errno == EWOULDBLOCK || errno == EAGAIN)
986 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
989 3cb3dd4d 2021-02-12 op mark_nonblock(fd);
991 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
992 abc007d2 2021-02-08 op c = &s->clients[i];
993 abc007d2 2021-02-08 op if (c->fd == -1) {
994 abc007d2 2021-02-08 op memset(c, 0, sizeof(*c));
995 abc007d2 2021-02-08 op if (tls_accept_socket(s->ctx, &c->ctx, fd) == -1)
996 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
1000 abc007d2 2021-02-08 op c->dir = NULL;
1001 abc007d2 2021-02-08 op c->addr = addr;
1003 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_handshake);
1004 d3a08f4d 2021-01-17 op connected_clients++;
1013 abc007d2 2021-02-08 op handle_sighup(int fd, short ev, void *d)
1018 a6e689d7 2021-02-12 op event_del(&e4);
1019 a6e689d7 2021-02-12 op if (has_ipv6)
1020 a6e689d7 2021-02-12 op event_del(&e6);
1021 a6e689d7 2021-02-12 op if (has_siginfo)
1022 a6e689d7 2021-02-12 op signal_del(&siginfo);
1023 a6e689d7 2021-02-12 op signal_del(&sigusr2);
1024 a6e689d7 2021-02-12 op signal_del(&sighup);
1028 abc007d2 2021-02-08 op handle_siginfo(int fd, short ev, void *d)
1034 abc007d2 2021-02-08 op log_info(NULL, "%d connected clients", connected_clients);
1038 abc007d2 2021-02-08 op loop(struct tls *ctx, int sock4, int sock6)
1040 abc007d2 2021-02-08 op struct server server;
1043 abc007d2 2021-02-08 op event_init();
1045 abc007d2 2021-02-08 op memset(&server, 0, sizeof(server));
1046 abc007d2 2021-02-08 op for (i = 0; i < MAX_USERS; ++i)
1047 abc007d2 2021-02-08 op server.clients[i].fd = -1;
1049 a6e689d7 2021-02-12 op event_set(&e4, sock4, EV_READ | EV_PERSIST, &do_accept, &server);
1050 a6e689d7 2021-02-12 op event_add(&e4, NULL);
1052 abc007d2 2021-02-08 op if (sock6 != -1) {
1053 a6e689d7 2021-02-12 op has_ipv6 = 1;
1054 a6e689d7 2021-02-12 op event_set(&e6, sock6, EV_READ | EV_PERSIST, &do_accept, &server);
1055 a6e689d7 2021-02-12 op event_add(&e6, NULL);
1058 a6e689d7 2021-02-12 op signal_set(&sighup, SIGHUP, &handle_sighup, NULL);
1059 a6e689d7 2021-02-12 op signal_add(&sighup, NULL);
1061 abc007d2 2021-02-08 op #ifdef SIGINFO
1062 a6e689d7 2021-02-12 op has_siginfo = 1;
1063 a6e689d7 2021-02-12 op signal_set(&siginfo, SIGINFO, &handle_siginfo, NULL);
1064 a6e689d7 2021-02-12 op signal_add(&siginfo, NULL);
1066 5e3285d5 2021-02-12 op signal_set(&sigusr2, SIGUSR2, &handle_siginfo, NULL);
1067 a6e689d7 2021-02-12 op signal_add(&sigusr2, NULL);
1069 abc007d2 2021-02-08 op server.ctx = ctx;
1072 abc007d2 2021-02-08 op event_dispatch();