Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/mman.h>
18 #include <sys/stat.h>
20 #include <netdb.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <fnmatch.h>
26 #include <limits.h>
27 #include <string.h>
29 #include "gmid.h"
31 int connected_clients;
33 const char *
34 vhost_lang(struct vhost *v, const char *path)
35 {
36 struct location *loc;
38 if (v == NULL || path == NULL)
39 return NULL;
41 for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
42 if (!fnmatch(loc->match, path, 0)) {
43 if (loc->lang != NULL)
44 return loc->lang;
45 }
46 }
48 return v->locations[0].lang;
49 }
51 const char *
52 vhost_default_mime(struct vhost *v, const char *path)
53 {
54 struct location *loc;
55 const char *default_mime = "application/octet-stream";
57 if (v == NULL || path == NULL)
58 return default_mime;
60 for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
61 if (!fnmatch(loc->match, path, 0)) {
62 if (loc->default_mime != NULL)
63 return loc->default_mime;
64 }
65 }
67 if (v->locations[0].default_mime != NULL)
68 return v->locations[0].default_mime;
69 return default_mime;
70 }
72 const char *
73 vhost_index(struct vhost *v, const char *path)
74 {
75 struct location *loc;
76 const char *index = "index.gmi";
78 if (v == NULL || path == NULL)
79 return index;
81 for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
82 if (!fnmatch(loc->match, path, 0)) {
83 if (loc->index != NULL)
84 return loc->index;
85 }
86 }
88 if (v->locations[0].index != NULL)
89 return v->locations[0].index;
90 return index;
91 }
93 int
94 vhost_auto_index(struct vhost *v, const char *path)
95 {
96 struct location *loc;
98 if (v == NULL || path == NULL)
99 return 0;
101 for (loc = v->locations; loc->match != NULL; ++loc) {
102 if (!fnmatch(loc->match, path, 0)) {
103 if (loc->auto_index != 0)
104 return loc->auto_index == 1;
108 return v->locations[0].auto_index == 1;
111 int
112 check_path(struct client *c, const char *path, int *fd)
114 struct stat sb;
115 const char *p;
116 int flags;
118 assert(path != NULL);
120 if (*path == '\0')
121 p = ".";
122 else if (*path == '/')
123 /* in send_dir we add an initial / (to be
124 * redirect-friendly), but here we want to skip it */
125 p = path+1;
126 else
127 p = path;
129 flags = O_RDONLY | O_NOFOLLOW;
131 if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
132 return FILE_MISSING;
134 if (fstat(*fd, &sb) == -1) {
135 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
136 return FILE_MISSING;
139 if (S_ISDIR(sb.st_mode))
140 return FILE_DIRECTORY;
142 if (sb.st_mode & S_IXUSR)
143 return FILE_EXECUTABLE;
145 return FILE_EXISTS;
148 void
149 open_file(struct pollfd *fds, struct client *c)
151 switch (check_path(c, c->iri.path, &c->fd)) {
152 case FILE_EXECUTABLE:
153 if (starts_with(c->iri.path, c->host->cgi)) {
154 start_cgi(c->iri.path, "", fds, c);
155 return;
158 /* fallthrough */
160 case FILE_EXISTS:
161 load_file(fds, c);
162 return;
164 case FILE_DIRECTORY:
165 open_dir(fds, c);
166 return;
168 case FILE_MISSING:
169 if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
170 check_for_cgi(fds, c);
171 return;
173 start_reply(fds, c, NOT_FOUND, "not found");
174 return;
176 default:
177 /* unreachable */
178 abort();
182 void
183 load_file(struct pollfd *fds, struct client *c)
185 if ((c->len = filesize(c->fd)) == -1) {
186 LOGE(c, "failed to get file size for %s", c->iri.path);
187 start_reply(fds, c, TEMP_FAILURE, "internal server error");
188 return;
191 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
192 c->fd, 0)) == MAP_FAILED) {
193 LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
194 start_reply(fds, c, TEMP_FAILURE, "internal server error");
195 return;
197 c->i = c->buf;
198 c->next = send_file;
199 start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
202 /*
203 * the inverse of this algorithm, i.e. starting from the start of the
204 * path + strlen(cgi), and checking if each component, should be
205 * faster. But it's tedious to write. This does the opposite: starts
206 * from the end and strip one component at a time, until either an
207 * executable is found or we emptied the path.
208 */
209 void
210 check_for_cgi(struct pollfd *fds, struct client *c)
212 char path[PATH_MAX];
213 char *end;
215 strlcpy(path, c->iri.path, sizeof(path));
216 end = strchr(path, '\0');
218 /* NB: assume CGI is enabled and path matches cgi */
220 while (end > path) {
221 /* go up one level. UNIX paths are simple and POSIX
222 * dirname, with its ambiguities on if the given path
223 * is changed or not, gives me headaches. */
224 while (*end != '/')
225 end--;
226 *end = '\0';
228 switch (check_path(c, path, &c->fd)) {
229 case FILE_EXECUTABLE:
230 start_cgi(path, end+1, fds, c);
231 return;
232 case FILE_MISSING:
233 break;
234 default:
235 goto err;
238 *end = '/';
239 end--;
242 err:
243 start_reply(fds, c, NOT_FOUND, "not found");
244 return;
247 void
248 mark_nonblock(int fd)
250 int flags;
252 if ((flags = fcntl(fd, F_GETFL)) == -1)
253 fatal("fcntl(F_GETFL): %s", strerror(errno));
254 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
255 fatal("fcntl(F_SETFL): %s", strerror(errno));
258 void
259 handle_handshake(struct pollfd *fds, struct client *c)
261 struct vhost *h;
262 const char *servname;
263 const char *parse_err = "unknown error";
265 switch (tls_handshake(c->ctx)) {
266 case 0: /* success */
267 case -1: /* already handshaked */
268 break;
269 case TLS_WANT_POLLIN:
270 fds->events = POLLIN;
271 return;
272 case TLS_WANT_POLLOUT:
273 fds->events = POLLOUT;
274 return;
275 default:
276 /* unreachable */
277 abort();
280 servname = tls_conn_servername(c->ctx);
281 if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
282 LOGI(c, "%s", parse_err);
283 goto err;
286 for (h = hosts; h->domain != NULL; ++h) {
287 if (!fnmatch(h->domain, c->domain, 0))
288 break;
291 /* LOGD(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"", */
292 /* servname != NULL ? servname : "(null)", */
293 /* c->domain, */
294 /* h->domain != NULL ? h->domain : "(null)"); */
296 if (h->domain != NULL) {
297 c->state = handle_open_conn;
298 c->host = h;
299 handle_open_conn(fds, c);
300 return;
303 err:
304 if (servname != NULL)
305 strncpy(c->req, servname, sizeof(c->req));
306 else
307 strncpy(c->req, "null", sizeof(c->req));
309 start_reply(fds, c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
312 void
313 handle_open_conn(struct pollfd *fds, struct client *c)
315 const char *parse_err = "invalid request";
316 char decoded[DOMAIN_NAME_LEN];
318 bzero(c->req, sizeof(c->req));
319 bzero(&c->iri, sizeof(c->iri));
321 switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
322 case -1:
323 LOGE(c, "tls_read: %s", tls_error(c->ctx));
324 close_conn(fds, c);
325 return;
327 case TLS_WANT_POLLIN:
328 fds->events = POLLIN;
329 return;
331 case TLS_WANT_POLLOUT:
332 fds->events = POLLOUT;
333 return;
336 if (!trim_req_iri(c->req, &parse_err)
337 || !parse_iri(c->req, &c->iri, &parse_err)
338 || !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
339 LOGI(c, "iri parse error: %s", parse_err);
340 start_reply(fds, c, BAD_REQUEST, "invalid request");
341 return;
344 if (c->iri.port_no != conf.port
345 || strcmp(c->iri.schema, "gemini")
346 || strcmp(decoded, c->domain)) {
347 start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
348 return;
351 open_file(fds, c);
354 void
355 start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
357 c->code = code;
358 c->meta = meta;
359 c->state = handle_start_reply;
360 c->state(pfd, c);
363 void
364 handle_start_reply(struct pollfd *pfd, struct client *c)
366 char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
367 const char *lang;
368 size_t len;
370 lang = vhost_lang(c->host, c->iri.path);
372 snprintf(buf, sizeof(buf), "%d ", c->code);
373 strlcat(buf, c->meta, sizeof(buf));
374 if (!strcmp(c->meta, "text/gemini") && lang != NULL) {
375 strlcat(buf, "; lang=", sizeof(buf));
376 strlcat(buf, lang, sizeof(buf));
379 len = strlcat(buf, "\r\n", sizeof(buf));
380 assert(len < sizeof(buf));
382 switch (tls_write(c->ctx, buf, len)) {
383 case -1:
384 close_conn(pfd, c);
385 return;
386 case TLS_WANT_POLLIN:
387 pfd->events = POLLIN;
388 return;
389 case TLS_WANT_POLLOUT:
390 pfd->events = POLLOUT;
391 return;
394 log_request(c, buf, sizeof(buf));
396 /* we don't need a body */
397 if (c->code != SUCCESS) {
398 close_conn(pfd, c);
399 return;
402 /* advance the state machine */
403 c->state = c->next;
404 c->state(pfd, c);
407 void
408 start_cgi(const char *spath, const char *relpath,
409 struct pollfd *fds, struct client *c)
411 char addr[NI_MAXHOST];
412 const char *ruser, *cissuer, *chash;
413 int e;
415 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
416 addr, sizeof(addr),
417 NULL, 0,
418 NI_NUMERICHOST);
419 if (e != 0)
420 goto err;
422 if (tls_peer_cert_provided(c->ctx)) {
423 ruser = tls_peer_cert_subject(c->ctx);
424 cissuer = tls_peer_cert_issuer(c->ctx);
425 chash = tls_peer_cert_hash(c->ctx);
426 } else {
427 ruser = NULL;
428 cissuer = NULL;
429 chash = NULL;
432 if (!send_iri(exfd, &c->iri)
433 || !send_string(exfd, spath)
434 || !send_string(exfd, relpath)
435 || !send_string(exfd, addr)
436 || !send_string(exfd, ruser)
437 || !send_string(exfd, cissuer)
438 || !send_string(exfd, chash)
439 || !send_vhost(exfd, c->host))
440 goto err;
442 close(c->fd);
443 if ((c->fd = recv_fd(exfd)) == -1) {
444 start_reply(fds, c, TEMP_FAILURE, "internal server error");
445 return;
447 c->state = handle_cgi;
448 cgi_poll_on_child(fds, c);
449 c->code = -1;
450 /* handle_cgi(fds, c); */
451 return;
453 err:
454 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
455 fatal("cannot talk to the executor process");
458 void
459 send_file(struct pollfd *fds, struct client *c)
461 ssize_t ret, len;
463 len = (c->buf + c->len) - c->i;
465 while (len > 0) {
466 switch (ret = tls_write(c->ctx, c->i, len)) {
467 case -1:
468 LOGE(c, "tls_write: %s", tls_error(c->ctx));
469 close_conn(fds, c);
470 return;
472 case TLS_WANT_POLLIN:
473 fds->events = POLLIN;
474 return;
476 case TLS_WANT_POLLOUT:
477 fds->events = POLLOUT;
478 return;
480 default:
481 c->i += ret;
482 len -= ret;
483 break;
487 close_conn(fds, c);
490 void
491 open_dir(struct pollfd *fds, struct client *c)
493 size_t len;
494 int dirfd;
495 char *before_file;
497 len = strlen(c->iri.path);
498 if (len > 0 && !ends_with(c->iri.path, "/")) {
499 redirect_canonical_dir(fds, c);
500 return;
503 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
504 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
505 if (!ends_with(c->sbuf, "/"))
506 strlcat(c->sbuf, "/", sizeof(c->sbuf));
507 before_file = strchr(c->sbuf, '\0');
508 len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
509 sizeof(c->sbuf));
510 if (len >= sizeof(c->sbuf)) {
511 start_reply(fds, c, TEMP_FAILURE, "internal server error");
512 return;
515 c->iri.path = c->sbuf;
517 /* close later unless we have to generate the dir listing */
518 dirfd = c->fd;
519 c->fd = -1;
521 switch (check_path(c, c->iri.path, &c->fd)) {
522 case FILE_EXECUTABLE:
523 if (starts_with(c->iri.path, c->host->cgi)) {
524 start_cgi(c->iri.path, "", fds, c);
525 break;
528 /* fallthrough */
530 case FILE_EXISTS:
531 load_file(fds, c);
532 break;
534 case FILE_DIRECTORY:
535 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
536 break;
538 case FILE_MISSING:
539 *before_file = '\0';
541 if (!vhost_auto_index(c->host, c->iri.path)) {
542 start_reply(fds, c, NOT_FOUND, "not found");
543 break;
546 c->fd = dirfd;
547 c->next = send_directory_listing;
549 if ((c->dir = fdopendir(c->fd)) == NULL) {
550 LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
551 c->fd, c->host->domain, c->iri.path, strerror(errno));
552 start_reply(fds, c, TEMP_FAILURE, "internal server error");
553 return;
555 c->off = 0;
557 start_reply(fds, c, SUCCESS, "text/gemini");
558 return;
560 default:
561 /* unreachable */
562 abort();
565 close(dirfd);
568 void
569 redirect_canonical_dir(struct pollfd *fds, struct client *c)
571 size_t len;
573 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
574 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
575 len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
577 if (len >= sizeof(c->sbuf)) {
578 start_reply(fds, c, TEMP_FAILURE, "internal server error");
579 return;
582 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
585 int
586 read_next_dir_entry(struct client *c)
588 struct dirent *d;
590 do {
591 errno = 0;
592 if ((d = readdir(c->dir)) == NULL) {
593 if (errno != 0)
594 LOGE(c, "readdir: %s", strerror(errno));
595 return 0;
597 } while (!strcmp(d->d_name, "."));
599 /* XXX: url escape */
600 snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
601 d->d_name, d->d_name);
602 c->len = strlen(c->sbuf);
603 c->off = 0;
605 return 1;
608 void
609 send_directory_listing(struct pollfd *fds, struct client *c)
611 ssize_t r;
613 while (1) {
614 if (c->len == 0) {
615 if (!read_next_dir_entry(c))
616 goto end;
619 while (c->len > 0) {
620 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
621 case -1:
622 goto end;
624 case TLS_WANT_POLLOUT:
625 fds->events = POLLOUT;
626 return;
628 case TLS_WANT_POLLIN:
629 fds->events = POLLIN;
630 return;
632 default:
633 c->off += r;
634 c->len -= r;
635 break;
640 end:
641 close_conn(fds, c);
644 void
645 cgi_poll_on_child(struct pollfd *fds, struct client *c)
647 int fd;
649 if (c->waiting_on_child)
650 return;
651 c->waiting_on_child = 1;
653 fds->events = POLLIN;
655 fd = fds->fd;
656 fds->fd = c->fd;
657 c->fd = fd;
660 void
661 cgi_poll_on_client(struct pollfd *fds, struct client *c)
663 int fd;
665 if (!c->waiting_on_child)
666 return;
667 c->waiting_on_child = 0;
669 fd = fds->fd;
670 fds->fd = c->fd;
671 c->fd = fd;
674 /* handle the read from the child process. Return like read(2) */
675 static ssize_t
676 read_from_cgi(struct client *c)
678 void *buf;
679 size_t len;
680 ssize_t r;
682 /* if we haven't read a whole response line, we want to
683 * continue reading. */
685 if (c->code == -1) {
686 buf = c->sbuf + c->len;
687 len = sizeof(c->sbuf) - c->len;
688 } else {
689 buf = c->sbuf;
690 len = sizeof(c->sbuf);
693 r = read(c->fd, buf, len);
694 if (r == 0 || r == -1)
695 return r;
697 c->len += r;
698 c->off = 0;
700 if (c->code != -1)
701 return r;
703 if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
704 c->code = 0;
705 log_request(c, c->sbuf, c->len);
708 return r;
711 void
712 handle_cgi(struct pollfd *fds, struct client *c)
714 ssize_t r;
716 /* ensure c->fd is the child and fds->fd the client */
717 cgi_poll_on_client(fds, c);
719 while (1) {
720 if (c->code == -1 || c->len == 0) {
721 switch (r = read_from_cgi(c)) {
722 case 0:
723 goto end;
725 case -1:
726 if (errno == EAGAIN || errno == EWOULDBLOCK) {
727 cgi_poll_on_child(fds, c);
728 return;
730 goto end;
734 if (c->code == -1) {
735 cgi_poll_on_child(fds, c);
736 return;
739 while (c->len > 0) {
740 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
741 case -1:
742 goto end;
744 case TLS_WANT_POLLOUT:
745 fds->events = POLLOUT;
746 return;
748 case TLS_WANT_POLLIN:
749 fds->events = POLLIN;
750 return;
752 default:
753 c->off += r;
754 c->len -= r;
755 break;
760 end:
761 close_conn(fds, c);
764 void
765 close_conn(struct pollfd *pfd, struct client *c)
767 c->state = close_conn;
769 switch (tls_close(c->ctx)) {
770 case TLS_WANT_POLLIN:
771 pfd->events = POLLIN;
772 return;
773 case TLS_WANT_POLLOUT:
774 pfd->events = POLLOUT;
775 return;
778 connected_clients--;
780 tls_free(c->ctx);
781 c->ctx = NULL;
783 if (c->buf != MAP_FAILED)
784 munmap(c->buf, c->len);
786 if (c->fd != -1)
787 close(c->fd);
789 if (c->dir != NULL)
790 closedir(c->dir);
792 close(pfd->fd);
793 pfd->fd = -1;
796 void
797 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
799 int i, fd;
800 struct sockaddr_storage addr;
801 socklen_t len;
803 len = sizeof(addr);
804 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
805 if (errno == EWOULDBLOCK)
806 return;
807 fatal("accept: %s", strerror(errno));
810 mark_nonblock(fd);
812 for (i = 0; i < MAX_USERS; ++i) {
813 if (fds[i].fd == -1) {
814 bzero(&clients[i], sizeof(struct client));
815 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
816 break; /* goodbye fd! */
818 fds[i].fd = fd;
819 fds[i].events = POLLIN;
821 clients[i].state = handle_handshake;
822 clients[i].next = send_file;
823 clients[i].fd = -1;
824 clients[i].waiting_on_child = 0;
825 clients[i].buf = MAP_FAILED;
826 clients[i].dir = NULL;
827 clients[i].addr = addr;
829 connected_clients++;
830 return;
834 close(fd);
837 void
838 loop(struct tls *ctx, int sock4, int sock6)
840 int i;
841 struct client clients[MAX_USERS];
842 struct pollfd fds[MAX_USERS];
844 connected_clients = 0;
846 for (i = 0; i < MAX_USERS; ++i) {
847 fds[i].fd = -1;
848 fds[i].events = POLLIN;
849 bzero(&clients[i], sizeof(struct client));
852 fds[0].fd = sock4;
853 fds[1].fd = sock6;
855 for (;;) {
856 if (poll(fds, MAX_USERS, INFTIM) == -1) {
857 if (errno == EINTR) {
858 fprintf(stderr, "connected clients: %d\n",
859 connected_clients);
860 continue;
862 fatal("poll: %s", strerror(errno));
865 for (i = 0; i < MAX_USERS; i++) {
866 if (fds[i].revents == 0)
867 continue;
869 if (fds[i].revents & (POLLERR|POLLNVAL))
870 fatal("bad fd %d: %s", fds[i].fd,
871 strerror(errno));
873 if (fds[i].revents & POLLHUP) {
874 /* fds[i] may be the fd of the stdin
875 * of a cgi script that has exited. */
876 if (!clients[i].waiting_on_child) {
877 close_conn(&fds[i], &clients[i]);
878 continue;
882 if (fds[i].fd == sock4)
883 do_accept(sock4, ctx, fds, clients);
884 else if (fds[i].fd == sock6)
885 do_accept(sock6, ctx, fds, clients);
886 else
887 clients[i].state(&fds[i], &clients[i]);