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;
37 if (v == NULL || path == NULL)
38 return NULL;
40 for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
41 if (!fnmatch(loc->match, path, 0)) {
42 if (loc->lang != NULL)
43 return loc->lang;
44 }
45 }
47 return v->locations[0].lang;
48 }
50 const char *
51 vhost_default_mime(struct vhost *v, const char *path)
52 {
53 struct location *loc;
54 const char *default_mime = "application/octet-stream";
56 if (v == NULL || path == NULL)
57 return default_mime;
59 for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
60 if (!fnmatch(loc->match, path, 0)) {
61 if (loc->default_mime != NULL)
62 return loc->default_mime;
63 }
64 }
66 if (v->locations[0].default_mime != NULL)
67 return v->locations[0].default_mime;
68 return default_mime;
69 }
71 const char *
72 vhost_index(struct vhost *v, const char *path)
73 {
74 struct location *loc;
75 const char *index = "index.gmi";
77 if (v == NULL || path == NULL)
78 return index;
80 for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
81 if (!fnmatch(loc->match, path, 0)) {
82 if (loc->index != NULL)
83 return loc->index;
84 }
85 }
87 if (v->locations[0].index != NULL)
88 return v->locations[0].index;
89 return index;
90 }
92 int
93 vhost_auto_index(struct vhost *v, const char *path)
94 {
95 struct location *loc;
97 if (v == NULL || path == NULL)
98 return 0;
100 for (loc = v->locations; loc->match != NULL; ++loc) {
101 if (!fnmatch(loc->match, path, 0)) {
102 if (loc->auto_index != 0)
103 return loc->auto_index == 1;
107 return v->locations[0].auto_index == 1;
110 int
111 check_path(struct client *c, const char *path, int *fd)
113 struct stat sb;
114 const char *p;
115 int flags;
117 assert(path != NULL);
119 if (*path == '\0')
120 p = ".";
121 else if (*path == '/')
122 /* in send_dir we add an initial / (to be
123 * redirect-friendly), but here we want to skip it */
124 p = path+1;
125 else
126 p = path;
128 flags = O_RDONLY | O_NOFOLLOW;
130 if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
131 return FILE_MISSING;
133 if (fstat(*fd, &sb) == -1) {
134 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
135 return FILE_MISSING;
138 if (S_ISDIR(sb.st_mode))
139 return FILE_DIRECTORY;
141 if (sb.st_mode & S_IXUSR)
142 return FILE_EXECUTABLE;
144 return FILE_EXISTS;
147 void
148 open_file(struct pollfd *fds, struct client *c)
150 switch (check_path(c, c->iri.path, &c->fd)) {
151 case FILE_EXECUTABLE:
152 if (starts_with(c->iri.path, c->host->cgi)) {
153 start_cgi(c->iri.path, "", c->iri.query, fds, c);
154 return;
157 /* fallthrough */
159 case FILE_EXISTS:
160 load_file(fds, c);
161 return;
163 case FILE_DIRECTORY:
164 open_dir(fds, c);
165 return;
167 case FILE_MISSING:
168 if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
169 check_for_cgi(c->iri.path, c->iri.query, fds, c);
170 return;
172 start_reply(fds, c, NOT_FOUND, "not found");
173 return;
175 default:
176 /* unreachable */
177 abort();
181 void
182 load_file(struct pollfd *fds, struct client *c)
184 if ((c->len = filesize(c->fd)) == -1) {
185 LOGE(c, "failed to get file size for %s", c->iri.path);
186 start_reply(fds, c, TEMP_FAILURE, "internal server error");
187 return;
190 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
191 c->fd, 0)) == MAP_FAILED) {
192 LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
193 start_reply(fds, c, TEMP_FAILURE, "internal server error");
194 return;
196 c->i = c->buf;
197 c->next = S_SENDING_FILE;
198 start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
201 /*
202 * the inverse of this algorithm, i.e. starting from the start of the
203 * path + strlen(cgi), and checking if each component, should be
204 * faster. But it's tedious to write. This does the opposite: starts
205 * from the end and strip one component at a time, until either an
206 * executable is found or we emptied the path.
207 */
208 void
209 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
211 char *end;
212 end = strchr(path, '\0');
214 /* NB: assume CGI is enabled and path matches cgi */
216 while (end > path) {
217 /* go up one level. UNIX paths are simple and POSIX
218 * dirname, with its ambiguities on if the given path
219 * is changed or not, gives me headaches. */
220 while (*end != '/')
221 end--;
222 *end = '\0';
224 switch (check_path(c, path, &c->fd)) {
225 case FILE_EXECUTABLE:
226 start_cgi(path, end+1, query, fds,c);
227 return;
228 case FILE_MISSING:
229 break;
230 default:
231 goto err;
234 *end = '/';
235 end--;
238 err:
239 start_reply(fds, c, NOT_FOUND, "not found");
240 return;
243 void
244 mark_nonblock(int fd)
246 int flags;
248 if ((flags = fcntl(fd, F_GETFL)) == -1)
249 fatal("fcntl(F_GETFL): %s", strerror(errno));
250 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
251 fatal("fcntl(F_SETFL): %s", strerror(errno));
254 void
255 handle_handshake(struct pollfd *fds, struct client *c)
257 struct vhost *h;
258 const char *servname;
259 const char *parse_err = "unknown error";
261 switch (tls_handshake(c->ctx)) {
262 case 0: /* success */
263 case -1: /* already handshaked */
264 break;
265 case TLS_WANT_POLLIN:
266 fds->events = POLLIN;
267 return;
268 case TLS_WANT_POLLOUT:
269 fds->events = POLLOUT;
270 return;
271 default:
272 /* unreachable */
273 abort();
276 servname = tls_conn_servername(c->ctx);
277 if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
278 LOGI(c, "%s", parse_err);
279 goto err;
282 for (h = hosts; h->domain != NULL; ++h) {
283 if (!fnmatch(h->domain, c->domain, 0))
284 break;
287 /* LOGD(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"", */
288 /* servname != NULL ? servname : "(null)", */
289 /* c->domain, */
290 /* h->domain != NULL ? h->domain : "(null)"); */
292 if (h->domain != NULL) {
293 c->state = S_OPEN;
294 c->host = h;
295 handle_open_conn(fds, c);
296 return;
299 err:
300 if (servname != NULL)
301 strncpy(c->req, servname, sizeof(c->req));
302 else
303 strncpy(c->req, "null", sizeof(c->req));
305 start_reply(fds, c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
308 void
309 handle_open_conn(struct pollfd *fds, struct client *c)
311 const char *parse_err = "invalid request";
312 char decoded[DOMAIN_NAME_LEN];
314 bzero(c->req, sizeof(c->req));
315 bzero(&c->iri, sizeof(c->iri));
317 switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
318 case -1:
319 LOGE(c, "tls_read: %s", tls_error(c->ctx));
320 close_conn(fds, c);
321 return;
323 case TLS_WANT_POLLIN:
324 fds->events = POLLIN;
325 return;
327 case TLS_WANT_POLLOUT:
328 fds->events = POLLOUT;
329 return;
332 if (!trim_req_iri(c->req, &parse_err)
333 || !parse_iri(c->req, &c->iri, &parse_err)
334 || !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
335 LOGI(c, "iri parse error: %s", parse_err);
336 start_reply(fds, c, BAD_REQUEST, "invalid request");
337 return;
340 if (c->iri.port_no != conf.port
341 || strcmp(c->iri.schema, "gemini")
342 || strcmp(decoded, c->domain)) {
343 start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
344 return;
347 open_file(fds, c);
350 void
351 start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
353 char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
354 const char *lang;
355 size_t len;
357 c->code = code;
358 c->meta = meta;
359 c->state = S_INITIALIZING;
361 lang = vhost_lang(c->host, c->iri.path);
363 snprintf(buf, sizeof(buf), "%d ", code);
364 strlcat(buf, meta, sizeof(buf));
365 if (!strcmp(meta, "text/gemini") && lang != NULL) {
366 strlcat(buf, "; lang=", sizeof(buf));
367 strlcat(buf, lang, sizeof(buf));
370 len = strlcat(buf, "\r\n", sizeof(buf));
371 assert(len < sizeof(buf));
373 switch (tls_write(c->ctx, buf, len)) {
374 case -1:
375 close_conn(pfd, c);
376 return;
377 case TLS_WANT_POLLIN:
378 pfd->events = POLLIN;
379 return;
380 case TLS_WANT_POLLOUT:
381 pfd->events = POLLOUT;
382 return;
385 log_request(c, buf, sizeof(buf));
387 /* we don't need a body */
388 if (c->code != SUCCESS) {
389 close_conn(pfd, c);
390 return;
393 /* advance the state machine */
394 c->state = c->next;
395 handle(pfd, c);
398 void
399 start_cgi(const char *spath, const char *relpath, const char *query,
400 struct pollfd *fds, struct client *c)
402 char addr[NI_MAXHOST];
403 const char *ruser, *cissuer, *chash;
404 int e;
406 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
407 addr, sizeof(addr),
408 NULL, 0,
409 NI_NUMERICHOST);
410 if (e != 0)
411 goto err;
413 if (tls_peer_cert_provided(c->ctx)) {
414 ruser = tls_peer_cert_subject(c->ctx);
415 cissuer = tls_peer_cert_issuer(c->ctx);
416 chash = tls_peer_cert_hash(c->ctx);
417 } else {
418 ruser = NULL;
419 cissuer = NULL;
420 chash = NULL;
423 if (!send_string(exfd, spath)
424 || !send_string(exfd, relpath)
425 || !send_string(exfd, query)
426 || !send_string(exfd, addr)
427 || !send_string(exfd, ruser)
428 || !send_string(exfd, cissuer)
429 || !send_string(exfd, chash)
430 || !send_vhost(exfd, c->host))
431 goto err;
433 close(c->fd);
434 if ((c->fd = recv_fd(exfd)) == -1) {
435 start_reply(fds, c, TEMP_FAILURE, "internal server error");
436 return;
438 c->state = S_SENDING_CGI;
439 cgi_poll_on_child(fds, c);
440 c->code = -1;
441 /* handle_cgi(fds, c); */
442 return;
444 err:
445 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
446 fatal("cannot talk to the executor process");
449 void
450 send_file(struct pollfd *fds, struct client *c)
452 ssize_t ret, len;
454 /* ensure the correct state */
455 c->state = S_SENDING_FILE;
457 len = (c->buf + c->len) - c->i;
459 while (len > 0) {
460 switch (ret = tls_write(c->ctx, c->i, len)) {
461 case -1:
462 LOGE(c, "tls_write: %s", tls_error(c->ctx));
463 close_conn(fds, c);
464 return;
466 case TLS_WANT_POLLIN:
467 fds->events = POLLIN;
468 return;
470 case TLS_WANT_POLLOUT:
471 fds->events = POLLOUT;
472 return;
474 default:
475 c->i += ret;
476 len -= ret;
477 break;
481 close_conn(fds, c);
484 void
485 open_dir(struct pollfd *fds, struct client *c)
487 size_t len;
488 int dirfd;
489 char *before_file;
491 len = strlen(c->iri.path);
492 if (len > 0 && !ends_with(c->iri.path, "/")) {
493 redirect_canonical_dir(fds, c);
494 return;
497 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
498 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
499 if (!ends_with(c->sbuf, "/"))
500 strlcat(c->sbuf, "/", sizeof(c->sbuf));
501 before_file = strchr(c->sbuf, '\0');
502 len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
503 sizeof(c->sbuf));
504 if (len >= sizeof(c->sbuf)) {
505 start_reply(fds, c, TEMP_FAILURE, "internal server error");
506 return;
509 c->iri.path = c->sbuf;
511 /* close later unless we have to generate the dir listing */
512 dirfd = c->fd;
513 c->fd = -1;
515 switch (check_path(c, c->iri.path, &c->fd)) {
516 case FILE_EXECUTABLE:
517 if (starts_with(c->iri.path, c->host->cgi)) {
518 start_cgi(c->iri.path, "", c->iri.query, fds, c);
519 break;
522 /* fallthrough */
524 case FILE_EXISTS:
525 load_file(fds, c);
526 break;
528 case FILE_DIRECTORY:
529 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
530 break;
532 case FILE_MISSING:
533 *before_file = '\0';
535 if (!vhost_auto_index(c->host, c->iri.path)) {
536 start_reply(fds, c, NOT_FOUND, "not found");
537 break;
540 c->fd = dirfd;
541 c->next = S_SENDING_DIR;
543 if ((c->dir = fdopendir(c->fd)) == NULL) {
544 LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
545 c->fd, c->host->domain, c->iri.path, strerror(errno));
546 start_reply(fds, c, TEMP_FAILURE, "internal server error");
547 return;
549 c->off = 0;
551 start_reply(fds, c, SUCCESS, "text/gemini");
552 return;
554 default:
555 /* unreachable */
556 abort();
559 close(dirfd);
562 void
563 redirect_canonical_dir(struct pollfd *fds, struct client *c)
565 size_t len;
567 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
568 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
569 len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
571 if (len >= sizeof(c->sbuf)) {
572 start_reply(fds, c, TEMP_FAILURE, "internal server error");
573 return;
576 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
579 int
580 read_next_dir_entry(struct client *c)
582 struct dirent *d;
584 do {
585 errno = 0;
586 if ((d = readdir(c->dir)) == NULL) {
587 if (errno != 0)
588 LOGE(c, "readdir: %s", strerror(errno));
589 return 0;
591 } while (!strcmp(d->d_name, "."));
593 /* XXX: url escape */
594 snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
595 d->d_name, d->d_name);
596 c->len = strlen(c->sbuf);
597 c->off = 0;
599 return 1;
602 void
603 send_directory_listing(struct pollfd *fds, struct client *c)
605 ssize_t r;
607 while (1) {
608 if (c->len == 0) {
609 if (!read_next_dir_entry(c))
610 goto end;
613 while (c->len > 0) {
614 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
615 case -1:
616 goto end;
618 case TLS_WANT_POLLOUT:
619 fds->events = POLLOUT;
620 return;
622 case TLS_WANT_POLLIN:
623 fds->events = POLLIN;
624 return;
626 default:
627 c->off += r;
628 c->len -= r;
629 break;
634 end:
635 close_conn(fds, c);
638 void
639 cgi_poll_on_child(struct pollfd *fds, struct client *c)
641 int fd;
643 if (c->waiting_on_child)
644 return;
645 c->waiting_on_child = 1;
647 fds->events = POLLIN;
649 fd = fds->fd;
650 fds->fd = c->fd;
651 c->fd = fd;
654 void
655 cgi_poll_on_client(struct pollfd *fds, struct client *c)
657 int fd;
659 if (!c->waiting_on_child)
660 return;
661 c->waiting_on_child = 0;
663 fd = fds->fd;
664 fds->fd = c->fd;
665 c->fd = fd;
668 /* handle the read from the child process. Return like read(2) */
669 static ssize_t
670 read_from_cgi(struct client *c)
672 void *buf;
673 size_t len;
674 ssize_t r;
676 /* if we haven't read a whole response line, we want to
677 * continue reading. */
679 if (c->code == -1) {
680 buf = c->sbuf + c->len;
681 len = sizeof(c->sbuf) - c->len;
682 } else {
683 buf = c->sbuf;
684 len = sizeof(c->sbuf);
687 r = read(c->fd, buf, len);
688 if (r == 0 || r == -1)
689 return r;
691 c->len += r;
692 c->off = 0;
694 if (c->code != -1)
695 return r;
697 if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
698 c->code = 0;
699 log_request(c, c->sbuf, c->len);
702 return r;
705 void
706 handle_cgi(struct pollfd *fds, struct client *c)
708 ssize_t r;
710 /* ensure c->fd is the child and fds->fd the client */
711 cgi_poll_on_client(fds, c);
713 while (1) {
714 if (c->code == -1 || c->len == 0) {
715 switch (r = read_from_cgi(c)) {
716 case 0:
717 goto end;
719 case -1:
720 if (errno == EAGAIN || errno == EWOULDBLOCK) {
721 cgi_poll_on_child(fds, c);
722 return;
724 goto end;
728 if (c->code == -1) {
729 cgi_poll_on_child(fds, c);
730 return;
733 while (c->len > 0) {
734 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
735 case -1:
736 goto end;
738 case TLS_WANT_POLLOUT:
739 fds->events = POLLOUT;
740 return;
742 case TLS_WANT_POLLIN:
743 fds->events = POLLIN;
744 return;
746 default:
747 c->off += r;
748 c->len -= r;
749 break;
754 end:
755 close_conn(fds, c);
758 void
759 close_conn(struct pollfd *pfd, struct client *c)
761 c->state = S_CLOSING;
763 switch (tls_close(c->ctx)) {
764 case TLS_WANT_POLLIN:
765 pfd->events = POLLIN;
766 return;
767 case TLS_WANT_POLLOUT:
768 pfd->events = POLLOUT;
769 return;
772 connected_clients--;
774 tls_free(c->ctx);
775 c->ctx = NULL;
777 if (c->buf != MAP_FAILED)
778 munmap(c->buf, c->len);
780 if (c->fd != -1)
781 close(c->fd);
783 if (c->dir != NULL)
784 closedir(c->dir);
786 close(pfd->fd);
787 pfd->fd = -1;
790 void
791 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
793 int i, fd;
794 struct sockaddr_storage addr;
795 socklen_t len;
797 len = sizeof(addr);
798 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
799 if (errno == EWOULDBLOCK)
800 return;
801 fatal("accept: %s", strerror(errno));
804 mark_nonblock(fd);
806 for (i = 0; i < MAX_USERS; ++i) {
807 if (fds[i].fd == -1) {
808 bzero(&clients[i], sizeof(struct client));
809 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
810 break; /* goodbye fd! */
812 fds[i].fd = fd;
813 fds[i].events = POLLIN;
815 clients[i].state = S_HANDSHAKE;
816 clients[i].next = S_SENDING_FILE;
817 clients[i].fd = -1;
818 clients[i].waiting_on_child = 0;
819 clients[i].buf = MAP_FAILED;
820 clients[i].dir = NULL;
821 clients[i].addr = addr;
823 connected_clients++;
824 return;
828 close(fd);
831 void
832 handle(struct pollfd *fds, struct client *client)
834 switch (client->state) {
835 case S_HANDSHAKE:
836 handle_handshake(fds, client);
837 break;
839 case S_OPEN:
840 handle_open_conn(fds, client);
841 break;
843 case S_INITIALIZING:
844 start_reply(fds, client, client->code, client->meta);
845 break;
847 case S_SENDING_FILE:
848 send_file(fds, client);
849 break;
851 case S_SENDING_DIR:
852 send_directory_listing(fds, client);
853 break;
855 case S_SENDING_CGI:
856 handle_cgi(fds, client);
857 break;
859 case S_CLOSING:
860 close_conn(fds, client);
861 break;
863 default:
864 /* unreachable */
865 abort();
869 void
870 loop(struct tls *ctx, int sock4, int sock6)
872 int i;
873 struct client clients[MAX_USERS];
874 struct pollfd fds[MAX_USERS];
876 connected_clients = 0;
878 for (i = 0; i < MAX_USERS; ++i) {
879 fds[i].fd = -1;
880 fds[i].events = POLLIN;
881 bzero(&clients[i], sizeof(struct client));
884 fds[0].fd = sock4;
885 fds[1].fd = sock6;
887 for (;;) {
888 if (poll(fds, MAX_USERS, INFTIM) == -1) {
889 if (errno == EINTR) {
890 fprintf(stderr, "connected clients: %d\n",
891 connected_clients);
892 continue;
894 fatal("poll: %s", strerror(errno));
897 for (i = 0; i < MAX_USERS; i++) {
898 if (fds[i].revents == 0)
899 continue;
901 if (fds[i].revents & (POLLERR|POLLNVAL))
902 fatal("bad fd %d: %s", fds[i].fd,
903 strerror(errno));
905 if (fds[i].revents & POLLHUP) {
906 /* fds[i] may be the fd of the stdin
907 * of a cgi script that has exited. */
908 if (!clients[i].waiting_on_child) {
909 close_conn(&fds[i], &clients[i]);
910 continue;
914 if (fds[i].fd == sock4)
915 do_accept(sock4, ctx, fds, clients);
916 else if (fds[i].fd == sock6)
917 do_accept(sock6, ctx, fds, clients);
918 else
919 handle(&fds[i], &clients[i]);