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 <string.h>
28 #include "gmid.h"
30 int connected_clients;
32 const char *
33 vhost_lang(struct vhost *v, const char *path)
34 {
35 struct location *loc;
36 const char *lang = NULL;
38 if (v == NULL)
39 return lang;
41 for (loc = v->locations; loc->match != NULL; ++loc) {
42 if (!fnmatch(loc->match, path, 0)) {
43 if (loc->lang != NULL)
44 lang = loc->lang;
45 }
46 }
48 return 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 for (loc = v->locations; loc->match != NULL; ++loc) {
58 if (!fnmatch(loc->match, path, 0)) {
59 if (loc->default_mime != NULL)
60 default_mime = loc->default_mime;
61 }
62 }
64 return default_mime;
65 }
67 const char *
68 vhost_index(struct vhost *v, const char *path)
69 {
70 struct location *loc;
71 const char *index = "index.gmi";
73 for (loc = v->locations; loc->match != NULL; ++loc) {
74 if (!fnmatch(loc->match, path, 0)) {
75 if (loc->index != NULL)
76 index = loc->index;
77 }
78 }
80 return index;
81 }
83 int
84 vhost_auto_index(struct vhost *v, const char *path)
85 {
86 struct location *loc;
87 int auto_index = 0;
89 for (loc = v->locations; loc->match != NULL; ++loc) {
90 if (!fnmatch(loc->match, path, 0)) {
91 if (loc->auto_index)
92 auto_index = loc->auto_index;
93 }
94 }
96 return auto_index == 1;
97 }
99 int
100 check_path(struct client *c, const char *path, int *fd)
102 struct stat sb;
103 const char *p;
104 int flags;
106 assert(path != NULL);
108 if (*path == '\0')
109 p = ".";
110 else if (*path == '/')
111 /* in send_dir we add an initial / (to be
112 * redirect-friendly), but here we want to skip it */
113 p = path+1;
114 else
115 p = path;
117 flags = O_RDONLY | O_NOFOLLOW;
119 if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
120 return FILE_MISSING;
122 if (fstat(*fd, &sb) == -1) {
123 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
124 return FILE_MISSING;
127 if (S_ISDIR(sb.st_mode))
128 return FILE_DIRECTORY;
130 if (sb.st_mode & S_IXUSR)
131 return FILE_EXECUTABLE;
133 return FILE_EXISTS;
136 void
137 open_file(struct pollfd *fds, struct client *c)
139 switch (check_path(c, c->iri.path, &c->fd)) {
140 case FILE_EXECUTABLE:
141 if (starts_with(c->iri.path, c->host->cgi)) {
142 start_cgi(c->iri.path, "", c->iri.query, fds, c);
143 return;
146 /* fallthrough */
148 case FILE_EXISTS:
149 load_file(fds, c);
150 return;
152 case FILE_DIRECTORY:
153 open_dir(fds, c);
154 return;
156 case FILE_MISSING:
157 if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
158 check_for_cgi(c->iri.path, c->iri.query, fds, c);
159 return;
161 start_reply(fds, c, NOT_FOUND, "not found");
162 return;
164 default:
165 /* unreachable */
166 abort();
170 void
171 load_file(struct pollfd *fds, struct client *c)
173 if ((c->len = filesize(c->fd)) == -1) {
174 LOGE(c, "failed to get file size for %s", c->iri.path);
175 start_reply(fds, c, TEMP_FAILURE, "internal server error");
176 return;
179 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
180 c->fd, 0)) == MAP_FAILED) {
181 LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
182 start_reply(fds, c, TEMP_FAILURE, "internal server error");
183 return;
185 c->i = c->buf;
186 c->next = S_SENDING_FILE;
187 start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
190 /*
191 * the inverse of this algorithm, i.e. starting from the start of the
192 * path + strlen(cgi), and checking if each component, should be
193 * faster. But it's tedious to write. This does the opposite: starts
194 * from the end and strip one component at a time, until either an
195 * executable is found or we emptied the path.
196 */
197 void
198 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
200 char *end;
201 end = strchr(path, '\0');
203 /* NB: assume CGI is enabled and path matches cgi */
205 while (end > path) {
206 /* go up one level. UNIX paths are simple and POSIX
207 * dirname, with its ambiguities on if the given path
208 * is changed or not, gives me headaches. */
209 while (*end != '/')
210 end--;
211 *end = '\0';
213 switch (check_path(c, path, &c->fd)) {
214 case FILE_EXECUTABLE:
215 start_cgi(path, end+1, query, fds,c);
216 return;
217 case FILE_MISSING:
218 break;
219 default:
220 goto err;
223 *end = '/';
224 end--;
227 err:
228 start_reply(fds, c, NOT_FOUND, "not found");
229 return;
232 void
233 mark_nonblock(int fd)
235 int flags;
237 if ((flags = fcntl(fd, F_GETFL)) == -1)
238 fatal("fcntl(F_GETFL): %s", strerror(errno));
239 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
240 fatal("fcntl(F_SETFL): %s", strerror(errno));
243 void
244 handle_handshake(struct pollfd *fds, struct client *c)
246 struct vhost *h;
247 const char *servname;
249 switch (tls_handshake(c->ctx)) {
250 case 0: /* success */
251 case -1: /* already handshaked */
252 break;
253 case TLS_WANT_POLLIN:
254 fds->events = POLLIN;
255 return;
256 case TLS_WANT_POLLOUT:
257 fds->events = POLLOUT;
258 return;
259 default:
260 /* unreachable */
261 abort();
264 servname = tls_conn_servername(c->ctx);
265 puny_decode(servname, c->domain, sizeof(c->domain));
267 for (h = hosts; h->domain != NULL; ++h) {
268 if (!fnmatch(h->domain, c->domain, 0))
269 break;
272 if (h->domain != NULL) {
273 c->state = S_OPEN;
274 c->host = h;
275 handle_open_conn(fds, c);
276 return;
279 if (servname != NULL)
280 strncpy(c->req, servname, sizeof(c->req));
281 else
282 strncpy(c->req, "null", sizeof(c->req));
284 start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI");
287 void
288 handle_open_conn(struct pollfd *fds, struct client *c)
290 const char *parse_err = "invalid request";
291 char decoded[DOMAIN_NAME_LEN];
293 bzero(c->req, sizeof(c->req));
294 bzero(&c->iri, sizeof(c->iri));
296 switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
297 case -1:
298 LOGE(c, "tls_read: %s", tls_error(c->ctx));
299 close_conn(fds, c);
300 return;
302 case TLS_WANT_POLLIN:
303 fds->events = POLLIN;
304 return;
306 case TLS_WANT_POLLOUT:
307 fds->events = POLLOUT;
308 return;
311 if (!trim_req_iri(c->req) || !parse_iri(c->req, &c->iri, &parse_err)) {
312 start_reply(fds, c, BAD_REQUEST, parse_err);
313 return;
316 puny_decode(c->iri.host, decoded, sizeof(decoded));
318 if (c->iri.port_no != conf.port
319 || strcmp(c->iri.schema, "gemini")
320 || strcmp(decoded, c->domain)) {
321 start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
322 return;
325 open_file(fds, c);
328 void
329 start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
331 char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
332 const char *lang;
333 size_t len;
335 c->code = code;
336 c->meta = meta;
337 c->state = S_INITIALIZING;
339 lang = vhost_lang(c->host, c->iri.path);
341 snprintf(buf, sizeof(buf), "%d ", code);
342 strlcat(buf, meta, sizeof(buf));
343 if (!strcmp(meta, "text/gemini") && lang != NULL) {
344 strlcat(buf, "; lang=", sizeof(buf));
345 strlcat(buf, lang, sizeof(buf));
348 len = strlcat(buf, "\r\n", sizeof(buf));
349 assert(len < sizeof(buf));
351 switch (tls_write(c->ctx, buf, len)) {
352 case -1:
353 close_conn(pfd, c);
354 return;
355 case TLS_WANT_POLLIN:
356 pfd->events = POLLIN;
357 return;
358 case TLS_WANT_POLLOUT:
359 pfd->events = POLLOUT;
360 return;
363 log_request(c, buf, sizeof(buf));
365 /* we don't need a body */
366 if (c->code != SUCCESS) {
367 close_conn(pfd, c);
368 return;
371 /* advance the state machine */
372 c->state = c->next;
373 handle(pfd, c);
376 void
377 start_cgi(const char *spath, const char *relpath, const char *query,
378 struct pollfd *fds, struct client *c)
380 char addr[NI_MAXHOST];
381 const char *ruser, *cissuer, *chash;
382 int e;
384 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
385 addr, sizeof(addr),
386 NULL, 0,
387 NI_NUMERICHOST);
388 if (e != 0)
389 goto err;
391 if (tls_peer_cert_provided(c->ctx)) {
392 ruser = tls_peer_cert_subject(c->ctx);
393 cissuer = tls_peer_cert_issuer(c->ctx);
394 chash = tls_peer_cert_hash(c->ctx);
395 } else {
396 ruser = NULL;
397 cissuer = NULL;
398 chash = NULL;
401 if (!send_string(exfd, spath)
402 || !send_string(exfd, relpath)
403 || !send_string(exfd, query)
404 || !send_string(exfd, addr)
405 || !send_string(exfd, ruser)
406 || !send_string(exfd, cissuer)
407 || !send_string(exfd, chash)
408 || !send_vhost(exfd, c->host))
409 goto err;
411 close(c->fd);
412 if ((c->fd = recv_fd(exfd)) == -1) {
413 start_reply(fds, c, TEMP_FAILURE, "internal server error");
414 return;
416 c->state = S_SENDING_CGI;
417 cgi_poll_on_child(fds, c);
418 c->code = -1;
419 /* handle_cgi(fds, c); */
420 return;
422 err:
423 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
424 fatal("cannot talk to the executor process");
427 void
428 send_file(struct pollfd *fds, struct client *c)
430 ssize_t ret, len;
432 /* ensure the correct state */
433 c->state = S_SENDING_FILE;
435 len = (c->buf + c->len) - c->i;
437 while (len > 0) {
438 switch (ret = tls_write(c->ctx, c->i, len)) {
439 case -1:
440 LOGE(c, "tls_write: %s", tls_error(c->ctx));
441 close_conn(fds, c);
442 return;
444 case TLS_WANT_POLLIN:
445 fds->events = POLLIN;
446 return;
448 case TLS_WANT_POLLOUT:
449 fds->events = POLLOUT;
450 return;
452 default:
453 c->i += ret;
454 len -= ret;
455 break;
459 close_conn(fds, c);
462 void
463 open_dir(struct pollfd *fds, struct client *c)
465 size_t len;
466 int dirfd;
467 char *before_file;
469 len = strlen(c->iri.path);
470 if (len > 0 && !ends_with(c->iri.path, "/")) {
471 redirect_canonical_dir(fds, c);
472 return;
475 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
476 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
477 if (!ends_with(c->sbuf, "/"))
478 strlcat(c->sbuf, "/", sizeof(c->sbuf));
479 before_file = strchr(c->sbuf, '\0');
480 len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
481 sizeof(c->sbuf));
482 if (len >= sizeof(c->sbuf)) {
483 start_reply(fds, c, TEMP_FAILURE, "internal server error");
484 return;
487 c->iri.path = c->sbuf;
489 /* close later unless we have to generate the dir listing */
490 dirfd = c->fd;
491 c->fd = -1;
493 switch (check_path(c, c->iri.path, &c->fd)) {
494 case FILE_EXECUTABLE:
495 if (starts_with(c->iri.path, c->host->cgi)) {
496 start_cgi(c->iri.path, "", c->iri.query, fds, c);
497 break;
500 /* fallthrough */
502 case FILE_EXISTS:
503 load_file(fds, c);
504 break;
506 case FILE_DIRECTORY:
507 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
508 break;
510 case FILE_MISSING:
511 *before_file = '\0';
513 if (!vhost_auto_index(c->host, c->iri.path)) {
514 start_reply(fds, c, NOT_FOUND, "not found");
515 break;
518 c->fd = dirfd;
519 c->next = S_SENDING_DIR;
521 if ((c->dir = fdopendir(c->fd)) == NULL) {
522 LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
523 c->fd, c->host->domain, c->iri.path, strerror(errno));
524 start_reply(fds, c, TEMP_FAILURE, "internal server error");
525 return;
527 c->off = 0;
529 start_reply(fds, c, SUCCESS, "text/gemini");
530 return;
532 default:
533 /* unreachable */
534 abort();
537 close(dirfd);
540 void
541 redirect_canonical_dir(struct pollfd *fds, struct client *c)
543 size_t len;
545 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
546 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
547 len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
549 if (len >= sizeof(c->sbuf)) {
550 start_reply(fds, c, TEMP_FAILURE, "internal server error");
551 return;
554 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
557 int
558 read_next_dir_entry(struct client *c)
560 struct dirent *d;
562 do {
563 errno = 0;
564 if ((d = readdir(c->dir)) == NULL) {
565 if (errno != 0)
566 LOGE(c, "readdir: %s", strerror(errno));
567 return 0;
569 } while (!strcmp(d->d_name, "."));
571 /* XXX: url escape */
572 snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
573 d->d_name, d->d_name);
574 c->len = strlen(c->sbuf);
575 c->off = 0;
577 return 1;
580 void
581 send_directory_listing(struct pollfd *fds, struct client *c)
583 ssize_t r;
585 while (1) {
586 if (c->len == 0) {
587 if (!read_next_dir_entry(c))
588 goto end;
591 while (c->len > 0) {
592 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
593 case -1:
594 goto end;
596 case TLS_WANT_POLLOUT:
597 fds->events = POLLOUT;
598 return;
600 case TLS_WANT_POLLIN:
601 fds->events = POLLIN;
602 return;
604 default:
605 c->off += r;
606 c->len -= r;
607 break;
612 end:
613 close_conn(fds, c);
616 void
617 cgi_poll_on_child(struct pollfd *fds, struct client *c)
619 int fd;
621 if (c->waiting_on_child)
622 return;
623 c->waiting_on_child = 1;
625 fds->events = POLLIN;
627 fd = fds->fd;
628 fds->fd = c->fd;
629 c->fd = fd;
632 void
633 cgi_poll_on_client(struct pollfd *fds, struct client *c)
635 int fd;
637 if (!c->waiting_on_child)
638 return;
639 c->waiting_on_child = 0;
641 fd = fds->fd;
642 fds->fd = c->fd;
643 c->fd = fd;
646 /* handle the read from the child process. Return like read(2) */
647 static ssize_t
648 read_from_cgi(struct client *c)
650 void *buf;
651 size_t len;
652 ssize_t r;
654 /* if we haven't read a whole response line, we want to
655 * continue reading. */
657 if (c->code == -1) {
658 buf = c->sbuf + c->len;
659 len = sizeof(c->sbuf) - c->len;
660 } else {
661 buf = c->sbuf;
662 len = sizeof(c->sbuf);
665 r = read(c->fd, buf, len);
666 if (r == 0 || r == -1)
667 return r;
669 c->len += r;
670 c->off = 0;
672 if (c->code != -1)
673 return r;
675 if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
676 c->code = 0;
677 log_request(c, c->sbuf, c->len);
680 return r;
683 void
684 handle_cgi(struct pollfd *fds, struct client *c)
686 ssize_t r;
688 /* ensure c->fd is the child and fds->fd the client */
689 cgi_poll_on_client(fds, c);
691 while (1) {
692 if (c->code == -1 || c->len == 0) {
693 switch (r = read_from_cgi(c)) {
694 case 0:
695 goto end;
697 case -1:
698 if (errno == EAGAIN || errno == EWOULDBLOCK) {
699 cgi_poll_on_child(fds, c);
700 return;
702 goto end;
706 if (c->code == -1) {
707 cgi_poll_on_child(fds, c);
708 return;
711 while (c->len > 0) {
712 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
713 case -1:
714 goto end;
716 case TLS_WANT_POLLOUT:
717 fds->events = POLLOUT;
718 return;
720 case TLS_WANT_POLLIN:
721 fds->events = POLLIN;
722 return;
724 default:
725 c->off += r;
726 c->len -= r;
727 break;
732 end:
733 close_conn(fds, c);
736 void
737 close_conn(struct pollfd *pfd, struct client *c)
739 c->state = S_CLOSING;
741 switch (tls_close(c->ctx)) {
742 case TLS_WANT_POLLIN:
743 pfd->events = POLLIN;
744 return;
745 case TLS_WANT_POLLOUT:
746 pfd->events = POLLOUT;
747 return;
750 connected_clients--;
752 tls_free(c->ctx);
753 c->ctx = NULL;
755 if (c->buf != MAP_FAILED)
756 munmap(c->buf, c->len);
758 if (c->fd != -1)
759 close(c->fd);
761 if (c->dir != NULL)
762 closedir(c->dir);
764 close(pfd->fd);
765 pfd->fd = -1;
768 void
769 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
771 int i, fd;
772 struct sockaddr_storage addr;
773 socklen_t len;
775 len = sizeof(addr);
776 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
777 if (errno == EWOULDBLOCK)
778 return;
779 fatal("accept: %s", strerror(errno));
782 mark_nonblock(fd);
784 for (i = 0; i < MAX_USERS; ++i) {
785 if (fds[i].fd == -1) {
786 bzero(&clients[i], sizeof(struct client));
787 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
788 break; /* goodbye fd! */
790 fds[i].fd = fd;
791 fds[i].events = POLLIN;
793 clients[i].state = S_HANDSHAKE;
794 clients[i].next = S_SENDING_FILE;
795 clients[i].fd = -1;
796 clients[i].waiting_on_child = 0;
797 clients[i].buf = MAP_FAILED;
798 clients[i].dir = NULL;
799 clients[i].addr = addr;
801 connected_clients++;
802 return;
806 close(fd);
809 void
810 handle(struct pollfd *fds, struct client *client)
812 switch (client->state) {
813 case S_HANDSHAKE:
814 handle_handshake(fds, client);
815 break;
817 case S_OPEN:
818 handle_open_conn(fds, client);
819 break;
821 case S_INITIALIZING:
822 start_reply(fds, client, client->code, client->meta);
823 break;
825 case S_SENDING_FILE:
826 send_file(fds, client);
827 break;
829 case S_SENDING_DIR:
830 send_directory_listing(fds, client);
831 break;
833 case S_SENDING_CGI:
834 handle_cgi(fds, client);
835 break;
837 case S_CLOSING:
838 close_conn(fds, client);
839 break;
841 default:
842 /* unreachable */
843 abort();
847 void
848 loop(struct tls *ctx, int sock4, int sock6)
850 int i;
851 struct client clients[MAX_USERS];
852 struct pollfd fds[MAX_USERS];
854 connected_clients = 0;
856 for (i = 0; i < MAX_USERS; ++i) {
857 fds[i].fd = -1;
858 fds[i].events = POLLIN;
859 bzero(&clients[i], sizeof(struct client));
862 fds[0].fd = sock4;
863 fds[1].fd = sock6;
865 for (;;) {
866 if (poll(fds, MAX_USERS, INFTIM) == -1) {
867 if (errno == EINTR) {
868 fprintf(stderr, "connected clients: %d\n",
869 connected_clients);
870 continue;
872 fatal("poll: %s", strerror(errno));
875 for (i = 0; i < MAX_USERS; i++) {
876 if (fds[i].revents == 0)
877 continue;
879 if (fds[i].revents & (POLLERR|POLLNVAL))
880 fatal("bad fd %d: %s", fds[i].fd,
881 strerror(errno));
883 if (fds[i].revents & POLLHUP) {
884 /* fds[i] may be the fd of the stdin
885 * of a cgi script that has exited. */
886 if (!clients[i].waiting_on_child) {
887 close_conn(&fds[i], &clients[i]);
888 continue;
892 if (fds[i].fd == sock4)
893 do_accept(sock4, ctx, fds, clients);
894 else if (fds[i].fd == sock6)
895 do_accept(sock6, ctx, fds, clients);
896 else
897 handle(&fds[i], &clients[i]);