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 = S_SENDING_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 = S_OPEN;
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 char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
358 const char *lang;
359 size_t len;
361 c->code = code;
362 c->meta = meta;
363 c->state = S_INITIALIZING;
365 lang = vhost_lang(c->host, c->iri.path);
367 snprintf(buf, sizeof(buf), "%d ", code);
368 strlcat(buf, meta, sizeof(buf));
369 if (!strcmp(meta, "text/gemini") && lang != NULL) {
370 strlcat(buf, "; lang=", sizeof(buf));
371 strlcat(buf, lang, sizeof(buf));
374 len = strlcat(buf, "\r\n", sizeof(buf));
375 assert(len < sizeof(buf));
377 switch (tls_write(c->ctx, buf, len)) {
378 case -1:
379 close_conn(pfd, c);
380 return;
381 case TLS_WANT_POLLIN:
382 pfd->events = POLLIN;
383 return;
384 case TLS_WANT_POLLOUT:
385 pfd->events = POLLOUT;
386 return;
389 log_request(c, buf, sizeof(buf));
391 /* we don't need a body */
392 if (c->code != SUCCESS) {
393 close_conn(pfd, c);
394 return;
397 /* advance the state machine */
398 c->state = c->next;
399 handle(pfd, c);
402 void
403 start_cgi(const char *spath, const char *relpath,
404 struct pollfd *fds, struct client *c)
406 char addr[NI_MAXHOST];
407 const char *ruser, *cissuer, *chash;
408 int e;
410 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
411 addr, sizeof(addr),
412 NULL, 0,
413 NI_NUMERICHOST);
414 if (e != 0)
415 goto err;
417 if (tls_peer_cert_provided(c->ctx)) {
418 ruser = tls_peer_cert_subject(c->ctx);
419 cissuer = tls_peer_cert_issuer(c->ctx);
420 chash = tls_peer_cert_hash(c->ctx);
421 } else {
422 ruser = NULL;
423 cissuer = NULL;
424 chash = NULL;
427 if (!send_iri(exfd, &c->iri)
428 || !send_string(exfd, spath)
429 || !send_string(exfd, relpath)
430 || !send_string(exfd, addr)
431 || !send_string(exfd, ruser)
432 || !send_string(exfd, cissuer)
433 || !send_string(exfd, chash)
434 || !send_vhost(exfd, c->host))
435 goto err;
437 close(c->fd);
438 if ((c->fd = recv_fd(exfd)) == -1) {
439 start_reply(fds, c, TEMP_FAILURE, "internal server error");
440 return;
442 c->state = S_SENDING_CGI;
443 cgi_poll_on_child(fds, c);
444 c->code = -1;
445 /* handle_cgi(fds, c); */
446 return;
448 err:
449 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
450 fatal("cannot talk to the executor process");
453 void
454 send_file(struct pollfd *fds, struct client *c)
456 ssize_t ret, len;
458 /* ensure the correct state */
459 c->state = S_SENDING_FILE;
461 len = (c->buf + c->len) - c->i;
463 while (len > 0) {
464 switch (ret = tls_write(c->ctx, c->i, len)) {
465 case -1:
466 LOGE(c, "tls_write: %s", tls_error(c->ctx));
467 close_conn(fds, c);
468 return;
470 case TLS_WANT_POLLIN:
471 fds->events = POLLIN;
472 return;
474 case TLS_WANT_POLLOUT:
475 fds->events = POLLOUT;
476 return;
478 default:
479 c->i += ret;
480 len -= ret;
481 break;
485 close_conn(fds, c);
488 void
489 open_dir(struct pollfd *fds, struct client *c)
491 size_t len;
492 int dirfd;
493 char *before_file;
495 len = strlen(c->iri.path);
496 if (len > 0 && !ends_with(c->iri.path, "/")) {
497 redirect_canonical_dir(fds, c);
498 return;
501 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
502 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
503 if (!ends_with(c->sbuf, "/"))
504 strlcat(c->sbuf, "/", sizeof(c->sbuf));
505 before_file = strchr(c->sbuf, '\0');
506 len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
507 sizeof(c->sbuf));
508 if (len >= sizeof(c->sbuf)) {
509 start_reply(fds, c, TEMP_FAILURE, "internal server error");
510 return;
513 c->iri.path = c->sbuf;
515 /* close later unless we have to generate the dir listing */
516 dirfd = c->fd;
517 c->fd = -1;
519 switch (check_path(c, c->iri.path, &c->fd)) {
520 case FILE_EXECUTABLE:
521 if (starts_with(c->iri.path, c->host->cgi)) {
522 start_cgi(c->iri.path, "", fds, c);
523 break;
526 /* fallthrough */
528 case FILE_EXISTS:
529 load_file(fds, c);
530 break;
532 case FILE_DIRECTORY:
533 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
534 break;
536 case FILE_MISSING:
537 *before_file = '\0';
539 if (!vhost_auto_index(c->host, c->iri.path)) {
540 start_reply(fds, c, NOT_FOUND, "not found");
541 break;
544 c->fd = dirfd;
545 c->next = S_SENDING_DIR;
547 if ((c->dir = fdopendir(c->fd)) == NULL) {
548 LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
549 c->fd, c->host->domain, c->iri.path, strerror(errno));
550 start_reply(fds, c, TEMP_FAILURE, "internal server error");
551 return;
553 c->off = 0;
555 start_reply(fds, c, SUCCESS, "text/gemini");
556 return;
558 default:
559 /* unreachable */
560 abort();
563 close(dirfd);
566 void
567 redirect_canonical_dir(struct pollfd *fds, struct client *c)
569 size_t len;
571 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
572 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
573 len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
575 if (len >= sizeof(c->sbuf)) {
576 start_reply(fds, c, TEMP_FAILURE, "internal server error");
577 return;
580 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
583 int
584 read_next_dir_entry(struct client *c)
586 struct dirent *d;
588 do {
589 errno = 0;
590 if ((d = readdir(c->dir)) == NULL) {
591 if (errno != 0)
592 LOGE(c, "readdir: %s", strerror(errno));
593 return 0;
595 } while (!strcmp(d->d_name, "."));
597 /* XXX: url escape */
598 snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
599 d->d_name, d->d_name);
600 c->len = strlen(c->sbuf);
601 c->off = 0;
603 return 1;
606 void
607 send_directory_listing(struct pollfd *fds, struct client *c)
609 ssize_t r;
611 while (1) {
612 if (c->len == 0) {
613 if (!read_next_dir_entry(c))
614 goto end;
617 while (c->len > 0) {
618 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
619 case -1:
620 goto end;
622 case TLS_WANT_POLLOUT:
623 fds->events = POLLOUT;
624 return;
626 case TLS_WANT_POLLIN:
627 fds->events = POLLIN;
628 return;
630 default:
631 c->off += r;
632 c->len -= r;
633 break;
638 end:
639 close_conn(fds, c);
642 void
643 cgi_poll_on_child(struct pollfd *fds, struct client *c)
645 int fd;
647 if (c->waiting_on_child)
648 return;
649 c->waiting_on_child = 1;
651 fds->events = POLLIN;
653 fd = fds->fd;
654 fds->fd = c->fd;
655 c->fd = fd;
658 void
659 cgi_poll_on_client(struct pollfd *fds, struct client *c)
661 int fd;
663 if (!c->waiting_on_child)
664 return;
665 c->waiting_on_child = 0;
667 fd = fds->fd;
668 fds->fd = c->fd;
669 c->fd = fd;
672 /* handle the read from the child process. Return like read(2) */
673 static ssize_t
674 read_from_cgi(struct client *c)
676 void *buf;
677 size_t len;
678 ssize_t r;
680 /* if we haven't read a whole response line, we want to
681 * continue reading. */
683 if (c->code == -1) {
684 buf = c->sbuf + c->len;
685 len = sizeof(c->sbuf) - c->len;
686 } else {
687 buf = c->sbuf;
688 len = sizeof(c->sbuf);
691 r = read(c->fd, buf, len);
692 if (r == 0 || r == -1)
693 return r;
695 c->len += r;
696 c->off = 0;
698 if (c->code != -1)
699 return r;
701 if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
702 c->code = 0;
703 log_request(c, c->sbuf, c->len);
706 return r;
709 void
710 handle_cgi(struct pollfd *fds, struct client *c)
712 ssize_t r;
714 /* ensure c->fd is the child and fds->fd the client */
715 cgi_poll_on_client(fds, c);
717 while (1) {
718 if (c->code == -1 || c->len == 0) {
719 switch (r = read_from_cgi(c)) {
720 case 0:
721 goto end;
723 case -1:
724 if (errno == EAGAIN || errno == EWOULDBLOCK) {
725 cgi_poll_on_child(fds, c);
726 return;
728 goto end;
732 if (c->code == -1) {
733 cgi_poll_on_child(fds, c);
734 return;
737 while (c->len > 0) {
738 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
739 case -1:
740 goto end;
742 case TLS_WANT_POLLOUT:
743 fds->events = POLLOUT;
744 return;
746 case TLS_WANT_POLLIN:
747 fds->events = POLLIN;
748 return;
750 default:
751 c->off += r;
752 c->len -= r;
753 break;
758 end:
759 close_conn(fds, c);
762 void
763 close_conn(struct pollfd *pfd, struct client *c)
765 c->state = S_CLOSING;
767 switch (tls_close(c->ctx)) {
768 case TLS_WANT_POLLIN:
769 pfd->events = POLLIN;
770 return;
771 case TLS_WANT_POLLOUT:
772 pfd->events = POLLOUT;
773 return;
776 connected_clients--;
778 tls_free(c->ctx);
779 c->ctx = NULL;
781 if (c->buf != MAP_FAILED)
782 munmap(c->buf, c->len);
784 if (c->fd != -1)
785 close(c->fd);
787 if (c->dir != NULL)
788 closedir(c->dir);
790 close(pfd->fd);
791 pfd->fd = -1;
794 void
795 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
797 int i, fd;
798 struct sockaddr_storage addr;
799 socklen_t len;
801 len = sizeof(addr);
802 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
803 if (errno == EWOULDBLOCK)
804 return;
805 fatal("accept: %s", strerror(errno));
808 mark_nonblock(fd);
810 for (i = 0; i < MAX_USERS; ++i) {
811 if (fds[i].fd == -1) {
812 bzero(&clients[i], sizeof(struct client));
813 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
814 break; /* goodbye fd! */
816 fds[i].fd = fd;
817 fds[i].events = POLLIN;
819 clients[i].state = S_HANDSHAKE;
820 clients[i].next = S_SENDING_FILE;
821 clients[i].fd = -1;
822 clients[i].waiting_on_child = 0;
823 clients[i].buf = MAP_FAILED;
824 clients[i].dir = NULL;
825 clients[i].addr = addr;
827 connected_clients++;
828 return;
832 close(fd);
835 void
836 handle(struct pollfd *fds, struct client *client)
838 switch (client->state) {
839 case S_HANDSHAKE:
840 handle_handshake(fds, client);
841 break;
843 case S_OPEN:
844 handle_open_conn(fds, client);
845 break;
847 case S_INITIALIZING:
848 start_reply(fds, client, client->code, client->meta);
849 break;
851 case S_SENDING_FILE:
852 send_file(fds, client);
853 break;
855 case S_SENDING_DIR:
856 send_directory_listing(fds, client);
857 break;
859 case S_SENDING_CGI:
860 handle_cgi(fds, client);
861 break;
863 case S_CLOSING:
864 close_conn(fds, client);
865 break;
867 default:
868 /* unreachable */
869 abort();
873 void
874 loop(struct tls *ctx, int sock4, int sock6)
876 int i;
877 struct client clients[MAX_USERS];
878 struct pollfd fds[MAX_USERS];
880 connected_clients = 0;
882 for (i = 0; i < MAX_USERS; ++i) {
883 fds[i].fd = -1;
884 fds[i].events = POLLIN;
885 bzero(&clients[i], sizeof(struct client));
888 fds[0].fd = sock4;
889 fds[1].fd = sock6;
891 for (;;) {
892 if (poll(fds, MAX_USERS, INFTIM) == -1) {
893 if (errno == EINTR) {
894 fprintf(stderr, "connected clients: %d\n",
895 connected_clients);
896 continue;
898 fatal("poll: %s", strerror(errno));
901 for (i = 0; i < MAX_USERS; i++) {
902 if (fds[i].revents == 0)
903 continue;
905 if (fds[i].revents & (POLLERR|POLLNVAL))
906 fatal("bad fd %d: %s", fds[i].fd,
907 strerror(errno));
909 if (fds[i].revents & POLLHUP) {
910 /* fds[i] may be the fd of the stdin
911 * of a cgi script that has exited. */
912 if (!clients[i].waiting_on_child) {
913 close_conn(&fds[i], &clients[i]);
914 continue;
918 if (fds[i].fd == sock4)
919 do_accept(sock4, ctx, fds, clients);
920 else if (fds[i].fd == sock6)
921 do_accept(sock6, ctx, fds, clients);
922 else
923 handle(&fds[i], &clients[i]);