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 d3a08f4d 2021-01-17 op #include <sys/mman.h>
18 d3a08f4d 2021-01-17 op #include <sys/stat.h>
20 d3a08f4d 2021-01-17 op #include <netdb.h>
22 d3a08f4d 2021-01-17 op #include <assert.h>
23 d3a08f4d 2021-01-17 op #include <errno.h>
24 abc007d2 2021-02-08 op #include <event.h>
25 d3a08f4d 2021-01-17 op #include <fcntl.h>
26 b4d409cf 2021-01-21 op #include <fnmatch.h>
27 2fafa2d2 2021-02-01 op #include <limits.h>
28 d3a08f4d 2021-01-17 op #include <string.h>
30 d3a08f4d 2021-01-17 op #include "gmid.h"
32 abc007d2 2021-02-08 op struct server {
33 abc007d2 2021-02-08 op struct client clients[MAX_USERS];
34 abc007d2 2021-02-08 op struct tls *ctx;
37 abc007d2 2021-02-08 op struct server_events {
38 abc007d2 2021-02-08 op struct event e4;
40 abc007d2 2021-02-08 op struct event e6;
41 abc007d2 2021-02-08 op struct event sighup;
44 d3a08f4d 2021-01-17 op int connected_clients;
46 abc007d2 2021-02-08 op static inline void reschedule_read(int, struct client*, statefn);
47 abc007d2 2021-02-08 op static inline void reschedule_write(int, struct client*, statefn);
49 fe406389 2021-02-02 op static int check_path(struct client*, const char*, int*);
50 abc007d2 2021-02-08 op static void open_file(struct client*);
51 abc007d2 2021-02-08 op static void load_file(struct client*);
52 abc007d2 2021-02-08 op static void check_for_cgi(struct client*);
53 abc007d2 2021-02-08 op static void handle_handshake(int, short, void*);
54 abc007d2 2021-02-08 op static int apply_block_return(struct client*);
55 abc007d2 2021-02-08 op static void handle_open_conn(int, short, void*);
56 abc007d2 2021-02-08 op static void start_reply(struct client*, int, const char*);
57 abc007d2 2021-02-08 op static void handle_start_reply(int, short, void*);
58 abc007d2 2021-02-08 op static void start_cgi(const char*, const char*, struct client*);
59 abc007d2 2021-02-08 op static void send_file(int, short, void*);
60 abc007d2 2021-02-08 op static void open_dir(struct client*);
61 abc007d2 2021-02-08 op static void redirect_canonical_dir(struct client*);
62 abc007d2 2021-02-08 op static void enter_handle_dirlist(int, short, void*);
63 abc007d2 2021-02-08 op static void handle_dirlist(int, short, void*);
64 fe406389 2021-02-02 op static int read_next_dir_entry(struct client*);
65 abc007d2 2021-02-08 op static void send_directory_listing(int, short, void*);
66 abc007d2 2021-02-08 op static void handle_cgi_reply(int, short, void*);
67 abc007d2 2021-02-08 op static void handle_cgi(int, short, void*);
68 abc007d2 2021-02-08 op static void close_conn(int, short, void*);
69 abc007d2 2021-02-08 op static void do_accept(int, short, void*);
70 abc007d2 2021-02-08 op static void handle_sighup(int, short, void*);
72 abc007d2 2021-02-08 op static inline void
73 abc007d2 2021-02-08 op reschedule_read(int fd, struct client *c, statefn fn)
75 abc007d2 2021-02-08 op event_once(fd, EV_READ, fn, c, NULL);
79 abc007d2 2021-02-08 op reschedule_write(int fd, struct client *c, statefn fn)
81 abc007d2 2021-02-08 op event_once(fd, EV_WRITE, fn, c, NULL);
85 c8b74339 2021-01-24 op vhost_lang(struct vhost *v, const char *path)
87 c8b74339 2021-01-24 op struct location *loc;
89 caad0308 2021-01-27 op if (v == NULL || path == NULL)
92 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
93 cd761624 2021-02-06 op if (loc->lang != NULL) {
94 cd761624 2021-02-06 op if (!fnmatch(loc->match, path, 0))
95 6016a593 2021-01-30 op return loc->lang;
99 6016a593 2021-01-30 op return v->locations[0].lang;
103 c8b74339 2021-01-24 op vhost_default_mime(struct vhost *v, const char *path)
105 c8b74339 2021-01-24 op struct location *loc;
106 c8b74339 2021-01-24 op const char *default_mime = "application/octet-stream";
108 caad0308 2021-01-27 op if (v == NULL || path == NULL)
109 caad0308 2021-01-27 op return default_mime;
111 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
112 cd761624 2021-02-06 op if (loc->default_mime != NULL) {
113 cd761624 2021-02-06 op if (!fnmatch(loc->match, path, 0))
114 6016a593 2021-01-30 op return loc->default_mime;
118 6016a593 2021-01-30 op if (v->locations[0].default_mime != NULL)
119 6016a593 2021-01-30 op return v->locations[0].default_mime;
120 c8b74339 2021-01-24 op return default_mime;
124 c8b74339 2021-01-24 op vhost_index(struct vhost *v, const char *path)
126 c8b74339 2021-01-24 op struct location *loc;
127 c8b74339 2021-01-24 op const char *index = "index.gmi";
129 caad0308 2021-01-27 op if (v == NULL || path == NULL)
130 caad0308 2021-01-27 op return index;
132 6016a593 2021-01-30 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
133 cd761624 2021-02-06 op if (loc->index != NULL) {
134 cd761624 2021-02-06 op if (!fnmatch(loc->match, path, 0))
135 6016a593 2021-01-30 op return loc->index;
139 b59f3cdd 2021-01-30 op if (v->locations[0].index != NULL)
140 b59f3cdd 2021-01-30 op return v->locations[0].index;
141 c8b74339 2021-01-24 op return index;
145 252908e6 2021-01-24 op vhost_auto_index(struct vhost *v, const char *path)
147 252908e6 2021-01-24 op struct location *loc;
149 6016a593 2021-01-30 op if (v == NULL || path == NULL)
152 daac4a94 2021-02-06 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
153 cd761624 2021-02-06 op if (loc->auto_index != 0) {
154 cd761624 2021-02-06 op if (!fnmatch(loc->match, path, 0))
155 6016a593 2021-01-30 op return loc->auto_index == 1;
159 6016a593 2021-01-30 op return v->locations[0].auto_index == 1;
163 6abda252 2021-02-06 op vhost_block_return(struct vhost *v, const char *path, int *code, const char **fmt)
165 6abda252 2021-02-06 op struct location *loc;
167 6abda252 2021-02-06 op if (v == NULL || path == NULL)
170 6abda252 2021-02-06 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
171 cd761624 2021-02-06 op if (loc->block_code != 0) {
172 cd761624 2021-02-06 op if (!fnmatch(loc->match, path, 0)) {
173 6abda252 2021-02-06 op *code = loc->block_code;
174 6abda252 2021-02-06 op *fmt = loc->block_fmt;
180 6abda252 2021-02-06 op *code = v->locations[0].block_code;
181 6abda252 2021-02-06 op *fmt = v->locations[0].block_fmt;
182 6abda252 2021-02-06 op return v->locations[0].block_code != 0;
186 6abda252 2021-02-06 op vhost_strip(struct vhost *v, const char *path)
188 6abda252 2021-02-06 op struct location *loc;
190 6abda252 2021-02-06 op if (v == NULL || path == NULL)
193 6abda252 2021-02-06 op for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
194 cd761624 2021-02-06 op if (loc->strip != 0) {
195 cd761624 2021-02-06 op if (!fnmatch(loc->match, path, 0))
196 6abda252 2021-02-06 op return loc->strip;
200 6abda252 2021-02-06 op return v->locations[0].strip;
204 d3a08f4d 2021-01-17 op check_path(struct client *c, const char *path, int *fd)
206 d3a08f4d 2021-01-17 op struct stat sb;
207 d1ca3911 2021-01-21 op const char *p;
210 d3a08f4d 2021-01-17 op assert(path != NULL);
212 d1ca3911 2021-01-21 op if (*path == '\0')
214 d1ca3911 2021-01-21 op else if (*path == '/')
215 d1ca3911 2021-01-21 op /* in send_dir we add an initial / (to be
216 d1ca3911 2021-01-21 op * redirect-friendly), but here we want to skip it */
221 252908e6 2021-01-24 op flags = O_RDONLY | O_NOFOLLOW;
223 252908e6 2021-01-24 op if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
224 d3a08f4d 2021-01-17 op return FILE_MISSING;
226 d3a08f4d 2021-01-17 op if (fstat(*fd, &sb) == -1) {
227 3abf91b0 2021-02-07 op log_notice(c, "failed stat for %s: %s", path, strerror(errno));
228 d3a08f4d 2021-01-17 op return FILE_MISSING;
231 d3a08f4d 2021-01-17 op if (S_ISDIR(sb.st_mode))
232 d3a08f4d 2021-01-17 op return FILE_DIRECTORY;
234 d3a08f4d 2021-01-17 op if (sb.st_mode & S_IXUSR)
235 d3a08f4d 2021-01-17 op return FILE_EXECUTABLE;
237 d3a08f4d 2021-01-17 op return FILE_EXISTS;
241 abc007d2 2021-02-08 op open_file(struct client *c)
243 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
244 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
245 87f2b68b 2021-02-02 op if (c->host->cgi != NULL && !fnmatch(c->host->cgi, c->iri.path, 0)) {
246 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
250 d3a08f4d 2021-01-17 op /* fallthrough */
252 d3a08f4d 2021-01-17 op case FILE_EXISTS:
253 abc007d2 2021-02-08 op load_file(c);
256 d3a08f4d 2021-01-17 op case FILE_DIRECTORY:
260 d3a08f4d 2021-01-17 op case FILE_MISSING:
261 87f2b68b 2021-02-02 op if (c->host->cgi != NULL && !fnmatch(c->host->cgi, c->iri.path, 0)) {
262 abc007d2 2021-02-08 op check_for_cgi(c);
265 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
269 d3a08f4d 2021-01-17 op /* unreachable */
275 abc007d2 2021-02-08 op load_file(struct client *c)
277 abc007d2 2021-02-08 op if ((c->len = filesize(c->pfd)) == -1) {
278 3abf91b0 2021-02-07 op log_err(c, "failed to get file size for %s: %s",
279 87f2b68b 2021-02-02 op c->iri.path, strerror(errno));
280 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
284 252908e6 2021-01-24 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
285 abc007d2 2021-02-08 op c->pfd, 0)) == MAP_FAILED) {
286 3abf91b0 2021-02-07 op log_err(c, "mmap: %s: %s", c->iri.path, strerror(errno));
287 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
290 252908e6 2021-01-24 op c->i = c->buf;
291 112802ea 2021-02-01 op c->next = send_file;
292 abc007d2 2021-02-08 op start_reply(c, SUCCESS, mime(c->host, c->iri.path));
296 d3a08f4d 2021-01-17 op * the inverse of this algorithm, i.e. starting from the start of the
297 d3a08f4d 2021-01-17 op * path + strlen(cgi), and checking if each component, should be
298 d3a08f4d 2021-01-17 op * faster. But it's tedious to write. This does the opposite: starts
299 d3a08f4d 2021-01-17 op * from the end and strip one component at a time, until either an
300 d3a08f4d 2021-01-17 op * executable is found or we emptied the path.
303 abc007d2 2021-02-08 op check_for_cgi(struct client *c)
305 2fafa2d2 2021-02-01 op char path[PATH_MAX];
308 2fafa2d2 2021-02-01 op strlcpy(path, c->iri.path, sizeof(path));
309 d3a08f4d 2021-01-17 op end = strchr(path, '\0');
311 d3a08f4d 2021-01-17 op while (end > path) {
312 d3a08f4d 2021-01-17 op /* go up one level. UNIX paths are simple and POSIX
313 d3a08f4d 2021-01-17 op * dirname, with its ambiguities on if the given path
314 d3a08f4d 2021-01-17 op * is changed or not, gives me headaches. */
315 d3a08f4d 2021-01-17 op while (*end != '/')
319 abc007d2 2021-02-08 op switch (check_path(c, path, &c->pfd)) {
320 d3a08f4d 2021-01-17 op case FILE_EXECUTABLE:
321 abc007d2 2021-02-08 op start_cgi(path, end+1, c);
323 d3a08f4d 2021-01-17 op case FILE_MISSING:
334 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
339 9b8f5ed2 2021-02-03 op mark_nonblock(int fd)
343 9b8f5ed2 2021-02-03 op if ((flags = fcntl(fd, F_GETFL)) == -1)
344 9b8f5ed2 2021-02-03 op fatal("fcntl(F_GETFL): %s", strerror(errno));
345 9b8f5ed2 2021-02-03 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
346 9b8f5ed2 2021-02-03 op fatal("fcntl(F_SETFL): %s", strerror(errno));
350 abc007d2 2021-02-08 op handle_handshake(int fd, short ev, void *d)
352 abc007d2 2021-02-08 op struct client *c = d;
353 d3a08f4d 2021-01-17 op struct vhost *h;
354 d3a08f4d 2021-01-17 op const char *servname;
355 a8d4a897 2021-01-29 op const char *parse_err = "unknown error";
357 d3a08f4d 2021-01-17 op switch (tls_handshake(c->ctx)) {
358 d3a08f4d 2021-01-17 op case 0: /* success */
359 d3a08f4d 2021-01-17 op case -1: /* already handshaked */
361 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
362 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_handshake);
364 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
365 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_handshake);
368 d3a08f4d 2021-01-17 op /* unreachable */
372 d3a08f4d 2021-01-17 op servname = tls_conn_servername(c->ctx);
373 a8d4a897 2021-01-29 op if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
374 3abf91b0 2021-02-07 op log_info(c, "puny_decode: %s", parse_err);
378 d3a08f4d 2021-01-17 op for (h = hosts; h->domain != NULL; ++h) {
379 3300cbe0 2021-01-27 op if (!fnmatch(h->domain, c->domain, 0))
383 3abf91b0 2021-02-07 op log_debug(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"",
384 3abf91b0 2021-02-07 op servname != NULL ? servname : "(null)",
386 3abf91b0 2021-02-07 op h->domain != NULL ? h->domain : "(null)");
388 d3a08f4d 2021-01-17 op if (h->domain != NULL) {
390 abc007d2 2021-02-08 op handle_open_conn(fd, ev, c);
395 0ab65593 2021-01-21 op if (servname != NULL)
396 0ab65593 2021-01-21 op strncpy(c->req, servname, sizeof(c->req));
398 0ab65593 2021-01-21 op strncpy(c->req, "null", sizeof(c->req));
400 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
403 6abda252 2021-02-06 op /* 1 if a matching `block return' (and apply it), 0 otherwise */
405 abc007d2 2021-02-08 op apply_block_return(struct client *c)
407 6abda252 2021-02-06 op char *t, *path, buf[32];
408 6abda252 2021-02-06 op const char *fmt;
409 6abda252 2021-02-06 op int strip, code;
412 6abda252 2021-02-06 op if (!vhost_block_return(c->host, c->iri.path, &code, &fmt))
415 6abda252 2021-02-06 op strip = vhost_strip(c->host, c->iri.path);
416 6abda252 2021-02-06 op path = c->iri.path;
417 6abda252 2021-02-06 op while (strip > 0) {
418 6abda252 2021-02-06 op if ((t = strchr(path, '/')) == NULL) {
419 6abda252 2021-02-06 op path = strchr(path, '\0');
426 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
427 6abda252 2021-02-06 op for (i = 0; *fmt; ++fmt) {
428 6abda252 2021-02-06 op if (i == sizeof(buf)-1 || *fmt == '%') {
429 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
430 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
434 6abda252 2021-02-06 op if (*fmt != '%') {
435 6abda252 2021-02-06 op buf[i++] = *fmt;
439 6abda252 2021-02-06 op switch (*++fmt) {
441 6abda252 2021-02-06 op strlcat(c->sbuf, "%", sizeof(c->sbuf));
444 6abda252 2021-02-06 op strlcat(c->sbuf, path, sizeof(c->sbuf));
447 6abda252 2021-02-06 op strlcat(c->sbuf, c->iri.query, sizeof(c->sbuf));
450 6abda252 2021-02-06 op snprintf(buf, sizeof(buf), "%d", conf.port);
451 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
452 6abda252 2021-02-06 op memset(buf, 0, sizeof(buf));
455 6abda252 2021-02-06 op strlcat(c->sbuf, c->domain, sizeof(c->sbuf));
458 6abda252 2021-02-06 op fatal("%s: unknown fmt specifier %c",
459 6abda252 2021-02-06 op __func__, *fmt);
464 6abda252 2021-02-06 op strlcat(c->sbuf, buf, sizeof(c->sbuf));
466 abc007d2 2021-02-08 op start_reply(c, code, c->sbuf);
471 abc007d2 2021-02-08 op handle_open_conn(int fd, short ev, void *d)
473 abc007d2 2021-02-08 op struct client *c = d;
474 d3a08f4d 2021-01-17 op const char *parse_err = "invalid request";
475 3300cbe0 2021-01-27 op char decoded[DOMAIN_NAME_LEN];
477 0be51733 2021-01-20 op bzero(c->req, sizeof(c->req));
478 0be51733 2021-01-20 op bzero(&c->iri, sizeof(c->iri));
480 0be51733 2021-01-20 op switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
482 3abf91b0 2021-02-07 op log_err(c, "tls_read: %s", tls_error(c->ctx));
483 abc007d2 2021-02-08 op close_conn(fd, ev, c);
486 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
487 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_open_conn);
490 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
491 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_open_conn);
495 c4f682f8 2021-01-27 op if (!trim_req_iri(c->req, &parse_err)
496 a2fd8013 2021-01-29 op || !parse_iri(c->req, &c->iri, &parse_err)
497 a2fd8013 2021-01-29 op || !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
498 3abf91b0 2021-02-07 op log_info(c, "iri parse error: %s", parse_err);
499 abc007d2 2021-02-08 op start_reply(c, BAD_REQUEST, "invalid request");
503 3300cbe0 2021-01-27 op if (c->iri.port_no != conf.port
504 3300cbe0 2021-01-27 op || strcmp(c->iri.schema, "gemini")
505 3300cbe0 2021-01-27 op || strcmp(decoded, c->domain)) {
506 abc007d2 2021-02-08 op start_reply(c, PROXY_REFUSED, "won't proxy request");
510 abc007d2 2021-02-08 op if (apply_block_return(c))
513 e3ddf390 2021-02-06 op if (c->host->entrypoint != NULL) {
514 abc007d2 2021-02-08 op start_cgi(c->host->entrypoint, c->iri.path, c);
518 abc007d2 2021-02-08 op open_file(c);
522 abc007d2 2021-02-08 op start_reply(struct client *c, int code, const char *meta)
524 112802ea 2021-02-01 op c->code = code;
525 112802ea 2021-02-01 op c->meta = meta;
526 abc007d2 2021-02-08 op handle_start_reply(c->fd, 0, c);
530 abc007d2 2021-02-08 op handle_start_reply(int fd, short ev, void *d)
532 abc007d2 2021-02-08 op struct client *c = d;
533 05c23a54 2021-01-19 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
534 c8b74339 2021-01-24 op const char *lang;
537 c8b74339 2021-01-24 op lang = vhost_lang(c->host, c->iri.path);
539 112802ea 2021-02-01 op snprintf(buf, sizeof(buf), "%d ", c->code);
540 112802ea 2021-02-01 op strlcat(buf, c->meta, sizeof(buf));
541 112802ea 2021-02-01 op if (!strcmp(c->meta, "text/gemini") && lang != NULL) {
542 05c23a54 2021-01-19 op strlcat(buf, "; lang=", sizeof(buf));
543 c8b74339 2021-01-24 op strlcat(buf, lang, sizeof(buf));
546 05c23a54 2021-01-19 op len = strlcat(buf, "\r\n", sizeof(buf));
547 0be51733 2021-01-20 op assert(len < sizeof(buf));
549 df79b4c1 2021-01-19 op switch (tls_write(c->ctx, buf, len)) {
551 abc007d2 2021-02-08 op close_conn(fd, ev, c);
553 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
554 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_start_reply);
556 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
557 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_start_reply);
561 a87f6625 2021-01-24 op log_request(c, buf, sizeof(buf));
563 abc007d2 2021-02-08 op if (c->code != SUCCESS)
564 abc007d2 2021-02-08 op close_conn(fd, ev, c);
566 abc007d2 2021-02-08 op c->next(fd, ev, c);
570 abc007d2 2021-02-08 op start_cgi(const char *spath, const char *relpath, struct client *c)
572 d3a08f4d 2021-01-17 op char addr[NI_MAXHOST];
575 d3a08f4d 2021-01-17 op e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
576 d3a08f4d 2021-01-17 op addr, sizeof(addr),
578 d3a08f4d 2021-01-17 op NI_NUMERICHOST);
582 2fafa2d2 2021-02-01 op if (!send_iri(exfd, &c->iri)
583 2fafa2d2 2021-02-01 op || !send_string(exfd, spath)
584 d3a08f4d 2021-01-17 op || !send_string(exfd, relpath)
585 d3a08f4d 2021-01-17 op || !send_string(exfd, addr)
586 b63e30ff 2021-02-07 op || !send_string(exfd, tls_peer_cert_subject(c->ctx))
587 b63e30ff 2021-02-07 op || !send_string(exfd, tls_peer_cert_issuer(c->ctx))
588 b63e30ff 2021-02-07 op || !send_string(exfd, tls_peer_cert_hash(c->ctx))
589 b63e30ff 2021-02-07 op || !send_time(exfd, tls_peer_cert_notbefore(c->ctx))
590 b63e30ff 2021-02-07 op || !send_time(exfd, tls_peer_cert_notafter(c->ctx))
591 d3a08f4d 2021-01-17 op || !send_vhost(exfd, c->host))
594 abc007d2 2021-02-08 op close(c->pfd);
595 abc007d2 2021-02-08 op if ((c->pfd = recv_fd(exfd)) == -1) {
596 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
600 abc007d2 2021-02-08 op reschedule_read(c->pfd, c, &handle_cgi_reply);
604 d3a08f4d 2021-01-17 op /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
605 132cae8c 2021-01-18 op fatal("cannot talk to the executor process");
609 abc007d2 2021-02-08 op send_file(int fd, short ev, void *d)
611 abc007d2 2021-02-08 op struct client *c = d;
612 d3a08f4d 2021-01-17 op ssize_t ret, len;
614 d3a08f4d 2021-01-17 op len = (c->buf + c->len) - c->i;
616 d3a08f4d 2021-01-17 op while (len > 0) {
617 d3a08f4d 2021-01-17 op switch (ret = tls_write(c->ctx, c->i, len)) {
619 3abf91b0 2021-02-07 op log_err(c, "tls_write: %s", tls_error(c->ctx));
620 abc007d2 2021-02-08 op close_conn(fd, ev, c);
623 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
624 abc007d2 2021-02-08 op reschedule_read(fd, c, &send_file);
627 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
628 abc007d2 2021-02-08 op reschedule_write(fd, c, &send_file);
638 abc007d2 2021-02-08 op close_conn(fd, ev, c);
642 abc007d2 2021-02-08 op open_dir(struct client *c)
646 252908e6 2021-01-24 op char *before_file;
648 0be51733 2021-01-20 op len = strlen(c->iri.path);
649 252908e6 2021-01-24 op if (len > 0 && !ends_with(c->iri.path, "/")) {
650 abc007d2 2021-02-08 op redirect_canonical_dir(c);
654 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
655 d1ca3911 2021-01-21 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
656 d1ca3911 2021-01-21 op if (!ends_with(c->sbuf, "/"))
657 0be51733 2021-01-20 op strlcat(c->sbuf, "/", sizeof(c->sbuf));
658 252908e6 2021-01-24 op before_file = strchr(c->sbuf, '\0');
659 c8b74339 2021-01-24 op len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
660 c8b74339 2021-01-24 op sizeof(c->sbuf));
661 252908e6 2021-01-24 op if (len >= sizeof(c->sbuf)) {
662 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
666 252908e6 2021-01-24 op c->iri.path = c->sbuf;
668 252908e6 2021-01-24 op /* close later unless we have to generate the dir listing */
669 abc007d2 2021-02-08 op dirfd = c->pfd;
672 abc007d2 2021-02-08 op switch (check_path(c, c->iri.path, &c->pfd)) {
673 252908e6 2021-01-24 op case FILE_EXECUTABLE:
674 87f2b68b 2021-02-02 op if (c->host->cgi != NULL && !fnmatch(c->host->cgi, c->iri.path, 0)) {
675 abc007d2 2021-02-08 op start_cgi(c->iri.path, "", c);
679 252908e6 2021-01-24 op /* fallthrough */
681 252908e6 2021-01-24 op case FILE_EXISTS:
682 abc007d2 2021-02-08 op load_file(c);
685 252908e6 2021-01-24 op case FILE_DIRECTORY:
686 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
689 252908e6 2021-01-24 op case FILE_MISSING:
690 252908e6 2021-01-24 op *before_file = '\0';
692 252908e6 2021-01-24 op if (!vhost_auto_index(c->host, c->iri.path)) {
693 abc007d2 2021-02-08 op start_reply(c, NOT_FOUND, "not found");
697 abc007d2 2021-02-08 op c->pfd = dirfd;
698 5f715ce4 2021-02-02 op c->next = enter_handle_dirlist;
700 abc007d2 2021-02-08 op if ((c->dir = fdopendir(c->pfd)) == NULL) {
701 3abf91b0 2021-02-07 op log_err(c, "fdopendir(%d) (vhost:%s) %s: %s",
702 abc007d2 2021-02-08 op c->pfd, c->host->domain, c->iri.path, strerror(errno));
703 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
708 abc007d2 2021-02-08 op start_reply(c, SUCCESS, "text/gemini");
712 252908e6 2021-01-24 op /* unreachable */
716 252908e6 2021-01-24 op close(dirfd);
720 abc007d2 2021-02-08 op redirect_canonical_dir(struct client *c)
724 252908e6 2021-01-24 op strlcpy(c->sbuf, "/", sizeof(c->sbuf));
725 252908e6 2021-01-24 op strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
726 252908e6 2021-01-24 op len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
728 0be51733 2021-01-20 op if (len >= sizeof(c->sbuf)) {
729 abc007d2 2021-02-08 op start_reply(c, TEMP_FAILURE, "internal server error");
733 abc007d2 2021-02-08 op start_reply(c, TEMP_REDIRECT, c->sbuf);
737 abc007d2 2021-02-08 op enter_handle_dirlist(int fd, short ev, void *d)
739 abc007d2 2021-02-08 op struct client *c = d;
740 5f715ce4 2021-02-02 op char b[PATH_MAX];
743 5f715ce4 2021-02-02 op strlcpy(b, c->iri.path, sizeof(b));
744 5f715ce4 2021-02-02 op l = snprintf(c->sbuf, sizeof(c->sbuf),
745 5f715ce4 2021-02-02 op "# Index of %s\n\n", b);
746 5f715ce4 2021-02-02 op if (l >= sizeof(c->sbuf)) {
747 5f715ce4 2021-02-02 op /* this is impossible, given that we have enough space
748 5f715ce4 2021-02-02 op * in c->sbuf to hold the ancilliary string plus the
749 5f715ce4 2021-02-02 op * full path; but it wouldn't read nice without some
750 5f715ce4 2021-02-02 op * error checking, and I'd like to avoid a strlen. */
751 abc007d2 2021-02-08 op close_conn(fd, ev, c);
756 abc007d2 2021-02-08 op handle_dirlist(fd, ev, c);
760 abc007d2 2021-02-08 op handle_dirlist(int fd, short ev, void *d)
762 abc007d2 2021-02-08 op struct client *c = d;
765 5f715ce4 2021-02-02 op while (c->len > 0) {
766 5f715ce4 2021-02-02 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
768 abc007d2 2021-02-08 op close_conn(fd, ev, c);
770 5f715ce4 2021-02-02 op case TLS_WANT_POLLOUT:
771 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_dirlist);
773 5f715ce4 2021-02-02 op case TLS_WANT_POLLIN:
774 abc007d2 2021-02-08 op reschedule_write(fd, c, &handle_dirlist);
782 abc007d2 2021-02-08 op send_directory_listing(fd, ev, c);
786 252908e6 2021-01-24 op read_next_dir_entry(struct client *c)
788 252908e6 2021-01-24 op struct dirent *d;
792 252908e6 2021-01-24 op if ((d = readdir(c->dir)) == NULL) {
793 252908e6 2021-01-24 op if (errno != 0)
794 3abf91b0 2021-02-07 op log_err(c, "readdir: %s", strerror(errno));
797 252908e6 2021-01-24 op } while (!strcmp(d->d_name, "."));
799 252908e6 2021-01-24 op /* XXX: url escape */
800 252908e6 2021-01-24 op snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
801 252908e6 2021-01-24 op d->d_name, d->d_name);
802 252908e6 2021-01-24 op c->len = strlen(c->sbuf);
809 abc007d2 2021-02-08 op send_directory_listing(int fd, short ev, void *d)
811 abc007d2 2021-02-08 op struct client *c = d;
815 252908e6 2021-01-24 op if (c->len == 0) {
816 252908e6 2021-01-24 op if (!read_next_dir_entry(c))
820 252908e6 2021-01-24 op while (c->len > 0) {
821 252908e6 2021-01-24 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
825 252908e6 2021-01-24 op case TLS_WANT_POLLOUT:
826 abc007d2 2021-02-08 op reschedule_read(fd, c, &send_directory_listing);
829 252908e6 2021-01-24 op case TLS_WANT_POLLIN:
830 abc007d2 2021-02-08 op reschedule_write(fd, c, &send_directory_listing);
842 abc007d2 2021-02-08 op close_conn(fd, ev, d);
845 35744950 2021-02-01 op /* accumulate the meta line from the cgi script. */
847 abc007d2 2021-02-08 op handle_cgi_reply(int fd, short ev, void *d)
849 abc007d2 2021-02-08 op struct client *c = d;
850 35744950 2021-02-01 op void *buf, *e;
855 35744950 2021-02-01 op buf = c->sbuf + c->len;
856 35744950 2021-02-01 op len = sizeof(c->sbuf) - c->len;
858 abc007d2 2021-02-08 op r = read(c->pfd, buf, len);
859 35744950 2021-02-01 op if (r == 0 || r == -1) {
860 abc007d2 2021-02-08 op start_reply(c, CGI_ERROR, "CGI error");
866 35744950 2021-02-01 op /* TODO: error if the CGI script don't reply correctly */
867 35744950 2021-02-01 op e = strchr(c->sbuf, '\n');
868 35744950 2021-02-01 op if (e != NULL || c->len == sizeof(c->sbuf)) {
869 3309ef97 2021-01-23 op log_request(c, c->sbuf, c->len);
872 abc007d2 2021-02-08 op handle_cgi(fd, ev, c);
876 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_cgi_reply);
880 abc007d2 2021-02-08 op handle_cgi(int fd, short ev, void *d)
882 abc007d2 2021-02-08 op struct client *c = d;
886 35744950 2021-02-01 op if (c->len == 0) {
887 abc007d2 2021-02-08 op switch (r = read(c->pfd, c->sbuf, sizeof(c->sbuf))) {
891 d3a08f4d 2021-01-17 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
892 abc007d2 2021-02-08 op reschedule_read(c->pfd, c, &handle_cgi);
902 d3a08f4d 2021-01-17 op while (c->len > 0) {
903 d3a08f4d 2021-01-17 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
907 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
908 abc007d2 2021-02-08 op reschedule_read(c->fd, c, &handle_cgi);
911 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
912 abc007d2 2021-02-08 op reschedule_write(c->fd, c, &handle_cgi);
924 abc007d2 2021-02-08 op close_conn(c->fd, ev, d);
928 abc007d2 2021-02-08 op close_conn(int fd, short ev, void *d)
930 abc007d2 2021-02-08 op struct client *c = d;
932 d3a08f4d 2021-01-17 op switch (tls_close(c->ctx)) {
933 d3a08f4d 2021-01-17 op case TLS_WANT_POLLIN:
934 abc007d2 2021-02-08 op reschedule_read(fd, c, &close_conn);
936 d3a08f4d 2021-01-17 op case TLS_WANT_POLLOUT:
937 abc007d2 2021-02-08 op reschedule_read(fd, c, &close_conn);
941 d3a08f4d 2021-01-17 op connected_clients--;
943 d3a08f4d 2021-01-17 op tls_free(c->ctx);
944 d3a08f4d 2021-01-17 op c->ctx = NULL;
946 d3a08f4d 2021-01-17 op if (c->buf != MAP_FAILED)
947 d3a08f4d 2021-01-17 op munmap(c->buf, c->len);
949 abc007d2 2021-02-08 op if (c->pfd != -1)
950 abc007d2 2021-02-08 op close(c->pfd);
952 252908e6 2021-01-24 op if (c->dir != NULL)
953 252908e6 2021-01-24 op closedir(c->dir);
955 abc007d2 2021-02-08 op close(c->fd);
960 abc007d2 2021-02-08 op do_accept(int sock, short et, void *d)
962 abc007d2 2021-02-08 op struct client *c;
963 abc007d2 2021-02-08 op struct server *s = d;
964 d3a08f4d 2021-01-17 op struct sockaddr_storage addr;
965 abc007d2 2021-02-08 op struct sockaddr *saddr;
966 d3a08f4d 2021-01-17 op socklen_t len;
972 abc007d2 2021-02-08 op saddr = (struct sockaddr*)&addr;
973 d3a08f4d 2021-01-17 op len = sizeof(addr);
974 abc007d2 2021-02-08 op if ((fd = accept4(sock, saddr, &len, SOCK_NONBLOCK)) == -1) {
975 cfb8a77f 2021-02-07 op if (errno == EWOULDBLOCK || errno == EAGAIN)
977 d3a08f4d 2021-01-17 op fatal("accept: %s", strerror(errno));
980 d3a08f4d 2021-01-17 op for (i = 0; i < MAX_USERS; ++i) {
981 abc007d2 2021-02-08 op c = &s->clients[i];
982 abc007d2 2021-02-08 op if (c->fd == -1) {
983 abc007d2 2021-02-08 op memset(c, 0, sizeof(*c));
984 abc007d2 2021-02-08 op if (tls_accept_socket(s->ctx, &c->ctx, fd) == -1)
985 d3a08f4d 2021-01-17 op break; /* goodbye fd! */
989 abc007d2 2021-02-08 op c->buf = MAP_FAILED;
990 abc007d2 2021-02-08 op c->dir = NULL;
991 abc007d2 2021-02-08 op c->addr = addr;
993 abc007d2 2021-02-08 op reschedule_read(fd, c, &handle_handshake);
994 d3a08f4d 2021-01-17 op connected_clients++;
1003 abc007d2 2021-02-08 op handle_sighup(int fd, short ev, void *d)
1005 abc007d2 2021-02-08 op struct server_events *events = d;
1010 abc007d2 2021-02-08 op event_del(&events->e4);
1011 abc007d2 2021-02-08 op if (events->has_ipv6)
1012 abc007d2 2021-02-08 op event_del(&events->e6);
1013 abc007d2 2021-02-08 op signal_del(&events->sighup);
1017 abc007d2 2021-02-08 op handle_siginfo(int fd, short ev, void *d)
1023 abc007d2 2021-02-08 op log_info(NULL, "%d connected clients", connected_clients);
1027 abc007d2 2021-02-08 op loop(struct tls *ctx, int sock4, int sock6)
1029 abc007d2 2021-02-08 op struct server_events events;
1030 abc007d2 2021-02-08 op struct server server;
1031 abc007d2 2021-02-08 op struct event info;
1034 abc007d2 2021-02-08 op event_init();
1036 abc007d2 2021-02-08 op memset(&events, 0, sizeof(events));
1037 abc007d2 2021-02-08 op memset(&server, 0, sizeof(server));
1038 abc007d2 2021-02-08 op for (i = 0; i < MAX_USERS; ++i)
1039 abc007d2 2021-02-08 op server.clients[i].fd = -1;
1041 abc007d2 2021-02-08 op event_set(&events.e4, sock4, EV_READ | EV_PERSIST, &do_accept, &server);
1042 abc007d2 2021-02-08 op event_add(&events.e4, NULL);
1044 abc007d2 2021-02-08 op if (sock6 != -1) {
1045 abc007d2 2021-02-08 op events.has_ipv6 = 1;
1046 abc007d2 2021-02-08 op event_set(&events.e6, sock6, EV_READ | EV_PERSIST, &do_accept, &server);
1047 abc007d2 2021-02-08 op event_add(&events.e6, NULL);
1050 abc007d2 2021-02-08 op signal_set(&events.sighup, SIGHUP, &handle_sighup, &events);
1051 abc007d2 2021-02-08 op signal_add(&events.sighup, NULL);
1053 abc007d2 2021-02-08 op #ifdef SIGINFO
1054 abc007d2 2021-02-08 op signal_set(&info, SIGINFO, &handle_siginfo, NULL);
1055 abc007d2 2021-02-08 op signal_add(&info, NULL);
1057 abc007d2 2021-02-08 op signal_set(&info, SIGUSR2, &handle_siginfo, NULL);
1058 abc007d2 2021-02-08 op signal_add(&info, NULL);
1060 abc007d2 2021-02-08 op server.ctx = ctx;
1062 abc007d2 2021-02-08 op event_dispatch();