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 || path == 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 if (v == NULL || path == NULL)
58 return default_mime;
60 for (loc = v->locations; loc->match != NULL; ++loc) {
61 if (!fnmatch(loc->match, path, 0)) {
62 if (loc->default_mime != NULL)
63 default_mime = loc->default_mime;
64 }
65 }
67 return default_mime;
68 }
70 const char *
71 vhost_index(struct vhost *v, const char *path)
72 {
73 struct location *loc;
74 const char *index = "index.gmi";
76 if (v == NULL || path == NULL)
77 return index;
79 for (loc = v->locations; loc->match != NULL; ++loc) {
80 if (!fnmatch(loc->match, path, 0)) {
81 if (loc->index != NULL)
82 index = loc->index;
83 }
84 }
86 return index;
87 }
89 int
90 vhost_auto_index(struct vhost *v, const char *path)
91 {
92 struct location *loc;
93 int auto_index = 0;
95 for (loc = v->locations; loc->match != NULL; ++loc) {
96 if (!fnmatch(loc->match, path, 0)) {
97 if (loc->auto_index)
98 auto_index = loc->auto_index;
99 }
102 return auto_index == 1;
105 int
106 check_path(struct client *c, const char *path, int *fd)
108 struct stat sb;
109 const char *p;
110 int flags;
112 assert(path != NULL);
114 if (*path == '\0')
115 p = ".";
116 else if (*path == '/')
117 /* in send_dir we add an initial / (to be
118 * redirect-friendly), but here we want to skip it */
119 p = path+1;
120 else
121 p = path;
123 flags = O_RDONLY | O_NOFOLLOW;
125 if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
126 return FILE_MISSING;
128 if (fstat(*fd, &sb) == -1) {
129 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
130 return FILE_MISSING;
133 if (S_ISDIR(sb.st_mode))
134 return FILE_DIRECTORY;
136 if (sb.st_mode & S_IXUSR)
137 return FILE_EXECUTABLE;
139 return FILE_EXISTS;
142 void
143 open_file(struct pollfd *fds, struct client *c)
145 switch (check_path(c, c->iri.path, &c->fd)) {
146 case FILE_EXECUTABLE:
147 if (starts_with(c->iri.path, c->host->cgi)) {
148 start_cgi(c->iri.path, "", c->iri.query, fds, c);
149 return;
152 /* fallthrough */
154 case FILE_EXISTS:
155 load_file(fds, c);
156 return;
158 case FILE_DIRECTORY:
159 open_dir(fds, c);
160 return;
162 case FILE_MISSING:
163 if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
164 check_for_cgi(c->iri.path, c->iri.query, fds, c);
165 return;
167 start_reply(fds, c, NOT_FOUND, "not found");
168 return;
170 default:
171 /* unreachable */
172 abort();
176 void
177 load_file(struct pollfd *fds, struct client *c)
179 if ((c->len = filesize(c->fd)) == -1) {
180 LOGE(c, "failed to get file size for %s", c->iri.path);
181 start_reply(fds, c, TEMP_FAILURE, "internal server error");
182 return;
185 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
186 c->fd, 0)) == MAP_FAILED) {
187 LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
188 start_reply(fds, c, TEMP_FAILURE, "internal server error");
189 return;
191 c->i = c->buf;
192 c->next = S_SENDING_FILE;
193 start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
196 /*
197 * the inverse of this algorithm, i.e. starting from the start of the
198 * path + strlen(cgi), and checking if each component, should be
199 * faster. But it's tedious to write. This does the opposite: starts
200 * from the end and strip one component at a time, until either an
201 * executable is found or we emptied the path.
202 */
203 void
204 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
206 char *end;
207 end = strchr(path, '\0');
209 /* NB: assume CGI is enabled and path matches cgi */
211 while (end > path) {
212 /* go up one level. UNIX paths are simple and POSIX
213 * dirname, with its ambiguities on if the given path
214 * is changed or not, gives me headaches. */
215 while (*end != '/')
216 end--;
217 *end = '\0';
219 switch (check_path(c, path, &c->fd)) {
220 case FILE_EXECUTABLE:
221 start_cgi(path, end+1, query, fds,c);
222 return;
223 case FILE_MISSING:
224 break;
225 default:
226 goto err;
229 *end = '/';
230 end--;
233 err:
234 start_reply(fds, c, NOT_FOUND, "not found");
235 return;
238 void
239 mark_nonblock(int fd)
241 int flags;
243 if ((flags = fcntl(fd, F_GETFL)) == -1)
244 fatal("fcntl(F_GETFL): %s", strerror(errno));
245 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
246 fatal("fcntl(F_SETFL): %s", strerror(errno));
249 void
250 handle_handshake(struct pollfd *fds, struct client *c)
252 struct vhost *h;
253 const char *servname;
254 const char *parse_err = "unknown error";
256 switch (tls_handshake(c->ctx)) {
257 case 0: /* success */
258 case -1: /* already handshaked */
259 break;
260 case TLS_WANT_POLLIN:
261 fds->events = POLLIN;
262 return;
263 case TLS_WANT_POLLOUT:
264 fds->events = POLLOUT;
265 return;
266 default:
267 /* unreachable */
268 abort();
271 servname = tls_conn_servername(c->ctx);
272 if (!puny_decode(servname, c->domain, sizeof(c->domain), &parse_err)) {
273 LOGI(c, "%s", parse_err);
274 goto err;
277 for (h = hosts; h->domain != NULL; ++h) {
278 if (!fnmatch(h->domain, c->domain, 0))
279 break;
282 /* LOGD(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"", */
283 /* servname != NULL ? servname : "(null)", */
284 /* c->domain, */
285 /* h->domain != NULL ? h->domain : "(null)"); */
287 if (h->domain != NULL) {
288 c->state = S_OPEN;
289 c->host = h;
290 handle_open_conn(fds, c);
291 return;
294 err:
295 if (servname != NULL)
296 strncpy(c->req, servname, sizeof(c->req));
297 else
298 strncpy(c->req, "null", sizeof(c->req));
300 start_reply(fds, c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
303 void
304 handle_open_conn(struct pollfd *fds, struct client *c)
306 const char *parse_err = "invalid request";
307 char decoded[DOMAIN_NAME_LEN];
309 bzero(c->req, sizeof(c->req));
310 bzero(&c->iri, sizeof(c->iri));
312 switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
313 case -1:
314 LOGE(c, "tls_read: %s", tls_error(c->ctx));
315 close_conn(fds, c);
316 return;
318 case TLS_WANT_POLLIN:
319 fds->events = POLLIN;
320 return;
322 case TLS_WANT_POLLOUT:
323 fds->events = POLLOUT;
324 return;
327 if (!trim_req_iri(c->req, &parse_err)
328 || !parse_iri(c->req, &c->iri, &parse_err)
329 || !puny_decode(c->iri.host, decoded, sizeof(decoded), &parse_err)) {
330 LOGI(c, "iri parse error: %s", parse_err);
331 start_reply(fds, c, BAD_REQUEST, "invalid request");
332 return;
335 if (c->iri.port_no != conf.port
336 || strcmp(c->iri.schema, "gemini")
337 || strcmp(decoded, c->domain)) {
338 start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
339 return;
342 open_file(fds, c);
345 void
346 start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
348 char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
349 const char *lang;
350 size_t len;
352 c->code = code;
353 c->meta = meta;
354 c->state = S_INITIALIZING;
356 lang = vhost_lang(c->host, c->iri.path);
358 snprintf(buf, sizeof(buf), "%d ", code);
359 strlcat(buf, meta, sizeof(buf));
360 if (!strcmp(meta, "text/gemini") && lang != NULL) {
361 strlcat(buf, "; lang=", sizeof(buf));
362 strlcat(buf, lang, sizeof(buf));
365 len = strlcat(buf, "\r\n", sizeof(buf));
366 assert(len < sizeof(buf));
368 switch (tls_write(c->ctx, buf, len)) {
369 case -1:
370 close_conn(pfd, c);
371 return;
372 case TLS_WANT_POLLIN:
373 pfd->events = POLLIN;
374 return;
375 case TLS_WANT_POLLOUT:
376 pfd->events = POLLOUT;
377 return;
380 log_request(c, buf, sizeof(buf));
382 /* we don't need a body */
383 if (c->code != SUCCESS) {
384 close_conn(pfd, c);
385 return;
388 /* advance the state machine */
389 c->state = c->next;
390 handle(pfd, c);
393 void
394 start_cgi(const char *spath, const char *relpath, const char *query,
395 struct pollfd *fds, struct client *c)
397 char addr[NI_MAXHOST];
398 const char *ruser, *cissuer, *chash;
399 int e;
401 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
402 addr, sizeof(addr),
403 NULL, 0,
404 NI_NUMERICHOST);
405 if (e != 0)
406 goto err;
408 if (tls_peer_cert_provided(c->ctx)) {
409 ruser = tls_peer_cert_subject(c->ctx);
410 cissuer = tls_peer_cert_issuer(c->ctx);
411 chash = tls_peer_cert_hash(c->ctx);
412 } else {
413 ruser = NULL;
414 cissuer = NULL;
415 chash = NULL;
418 if (!send_string(exfd, spath)
419 || !send_string(exfd, relpath)
420 || !send_string(exfd, query)
421 || !send_string(exfd, addr)
422 || !send_string(exfd, ruser)
423 || !send_string(exfd, cissuer)
424 || !send_string(exfd, chash)
425 || !send_vhost(exfd, c->host))
426 goto err;
428 close(c->fd);
429 if ((c->fd = recv_fd(exfd)) == -1) {
430 start_reply(fds, c, TEMP_FAILURE, "internal server error");
431 return;
433 c->state = S_SENDING_CGI;
434 cgi_poll_on_child(fds, c);
435 c->code = -1;
436 /* handle_cgi(fds, c); */
437 return;
439 err:
440 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
441 fatal("cannot talk to the executor process");
444 void
445 send_file(struct pollfd *fds, struct client *c)
447 ssize_t ret, len;
449 /* ensure the correct state */
450 c->state = S_SENDING_FILE;
452 len = (c->buf + c->len) - c->i;
454 while (len > 0) {
455 switch (ret = tls_write(c->ctx, c->i, len)) {
456 case -1:
457 LOGE(c, "tls_write: %s", tls_error(c->ctx));
458 close_conn(fds, c);
459 return;
461 case TLS_WANT_POLLIN:
462 fds->events = POLLIN;
463 return;
465 case TLS_WANT_POLLOUT:
466 fds->events = POLLOUT;
467 return;
469 default:
470 c->i += ret;
471 len -= ret;
472 break;
476 close_conn(fds, c);
479 void
480 open_dir(struct pollfd *fds, struct client *c)
482 size_t len;
483 int dirfd;
484 char *before_file;
486 len = strlen(c->iri.path);
487 if (len > 0 && !ends_with(c->iri.path, "/")) {
488 redirect_canonical_dir(fds, c);
489 return;
492 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
493 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
494 if (!ends_with(c->sbuf, "/"))
495 strlcat(c->sbuf, "/", sizeof(c->sbuf));
496 before_file = strchr(c->sbuf, '\0');
497 len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
498 sizeof(c->sbuf));
499 if (len >= sizeof(c->sbuf)) {
500 start_reply(fds, c, TEMP_FAILURE, "internal server error");
501 return;
504 c->iri.path = c->sbuf;
506 /* close later unless we have to generate the dir listing */
507 dirfd = c->fd;
508 c->fd = -1;
510 switch (check_path(c, c->iri.path, &c->fd)) {
511 case FILE_EXECUTABLE:
512 if (starts_with(c->iri.path, c->host->cgi)) {
513 start_cgi(c->iri.path, "", c->iri.query, fds, c);
514 break;
517 /* fallthrough */
519 case FILE_EXISTS:
520 load_file(fds, c);
521 break;
523 case FILE_DIRECTORY:
524 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
525 break;
527 case FILE_MISSING:
528 *before_file = '\0';
530 if (!vhost_auto_index(c->host, c->iri.path)) {
531 start_reply(fds, c, NOT_FOUND, "not found");
532 break;
535 c->fd = dirfd;
536 c->next = S_SENDING_DIR;
538 if ((c->dir = fdopendir(c->fd)) == NULL) {
539 LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
540 c->fd, c->host->domain, c->iri.path, strerror(errno));
541 start_reply(fds, c, TEMP_FAILURE, "internal server error");
542 return;
544 c->off = 0;
546 start_reply(fds, c, SUCCESS, "text/gemini");
547 return;
549 default:
550 /* unreachable */
551 abort();
554 close(dirfd);
557 void
558 redirect_canonical_dir(struct pollfd *fds, struct client *c)
560 size_t len;
562 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
563 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
564 len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
566 if (len >= sizeof(c->sbuf)) {
567 start_reply(fds, c, TEMP_FAILURE, "internal server error");
568 return;
571 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
574 int
575 read_next_dir_entry(struct client *c)
577 struct dirent *d;
579 do {
580 errno = 0;
581 if ((d = readdir(c->dir)) == NULL) {
582 if (errno != 0)
583 LOGE(c, "readdir: %s", strerror(errno));
584 return 0;
586 } while (!strcmp(d->d_name, "."));
588 /* XXX: url escape */
589 snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
590 d->d_name, d->d_name);
591 c->len = strlen(c->sbuf);
592 c->off = 0;
594 return 1;
597 void
598 send_directory_listing(struct pollfd *fds, struct client *c)
600 ssize_t r;
602 while (1) {
603 if (c->len == 0) {
604 if (!read_next_dir_entry(c))
605 goto end;
608 while (c->len > 0) {
609 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
610 case -1:
611 goto end;
613 case TLS_WANT_POLLOUT:
614 fds->events = POLLOUT;
615 return;
617 case TLS_WANT_POLLIN:
618 fds->events = POLLIN;
619 return;
621 default:
622 c->off += r;
623 c->len -= r;
624 break;
629 end:
630 close_conn(fds, c);
633 void
634 cgi_poll_on_child(struct pollfd *fds, struct client *c)
636 int fd;
638 if (c->waiting_on_child)
639 return;
640 c->waiting_on_child = 1;
642 fds->events = POLLIN;
644 fd = fds->fd;
645 fds->fd = c->fd;
646 c->fd = fd;
649 void
650 cgi_poll_on_client(struct pollfd *fds, struct client *c)
652 int fd;
654 if (!c->waiting_on_child)
655 return;
656 c->waiting_on_child = 0;
658 fd = fds->fd;
659 fds->fd = c->fd;
660 c->fd = fd;
663 /* handle the read from the child process. Return like read(2) */
664 static ssize_t
665 read_from_cgi(struct client *c)
667 void *buf;
668 size_t len;
669 ssize_t r;
671 /* if we haven't read a whole response line, we want to
672 * continue reading. */
674 if (c->code == -1) {
675 buf = c->sbuf + c->len;
676 len = sizeof(c->sbuf) - c->len;
677 } else {
678 buf = c->sbuf;
679 len = sizeof(c->sbuf);
682 r = read(c->fd, buf, len);
683 if (r == 0 || r == -1)
684 return r;
686 c->len += r;
687 c->off = 0;
689 if (c->code != -1)
690 return r;
692 if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
693 c->code = 0;
694 log_request(c, c->sbuf, c->len);
697 return r;
700 void
701 handle_cgi(struct pollfd *fds, struct client *c)
703 ssize_t r;
705 /* ensure c->fd is the child and fds->fd the client */
706 cgi_poll_on_client(fds, c);
708 while (1) {
709 if (c->code == -1 || c->len == 0) {
710 switch (r = read_from_cgi(c)) {
711 case 0:
712 goto end;
714 case -1:
715 if (errno == EAGAIN || errno == EWOULDBLOCK) {
716 cgi_poll_on_child(fds, c);
717 return;
719 goto end;
723 if (c->code == -1) {
724 cgi_poll_on_child(fds, c);
725 return;
728 while (c->len > 0) {
729 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
730 case -1:
731 goto end;
733 case TLS_WANT_POLLOUT:
734 fds->events = POLLOUT;
735 return;
737 case TLS_WANT_POLLIN:
738 fds->events = POLLIN;
739 return;
741 default:
742 c->off += r;
743 c->len -= r;
744 break;
749 end:
750 close_conn(fds, c);
753 void
754 close_conn(struct pollfd *pfd, struct client *c)
756 c->state = S_CLOSING;
758 switch (tls_close(c->ctx)) {
759 case TLS_WANT_POLLIN:
760 pfd->events = POLLIN;
761 return;
762 case TLS_WANT_POLLOUT:
763 pfd->events = POLLOUT;
764 return;
767 connected_clients--;
769 tls_free(c->ctx);
770 c->ctx = NULL;
772 if (c->buf != MAP_FAILED)
773 munmap(c->buf, c->len);
775 if (c->fd != -1)
776 close(c->fd);
778 if (c->dir != NULL)
779 closedir(c->dir);
781 close(pfd->fd);
782 pfd->fd = -1;
785 void
786 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
788 int i, fd;
789 struct sockaddr_storage addr;
790 socklen_t len;
792 len = sizeof(addr);
793 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
794 if (errno == EWOULDBLOCK)
795 return;
796 fatal("accept: %s", strerror(errno));
799 mark_nonblock(fd);
801 for (i = 0; i < MAX_USERS; ++i) {
802 if (fds[i].fd == -1) {
803 bzero(&clients[i], sizeof(struct client));
804 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
805 break; /* goodbye fd! */
807 fds[i].fd = fd;
808 fds[i].events = POLLIN;
810 clients[i].state = S_HANDSHAKE;
811 clients[i].next = S_SENDING_FILE;
812 clients[i].fd = -1;
813 clients[i].waiting_on_child = 0;
814 clients[i].buf = MAP_FAILED;
815 clients[i].dir = NULL;
816 clients[i].addr = addr;
818 connected_clients++;
819 return;
823 close(fd);
826 void
827 handle(struct pollfd *fds, struct client *client)
829 switch (client->state) {
830 case S_HANDSHAKE:
831 handle_handshake(fds, client);
832 break;
834 case S_OPEN:
835 handle_open_conn(fds, client);
836 break;
838 case S_INITIALIZING:
839 start_reply(fds, client, client->code, client->meta);
840 break;
842 case S_SENDING_FILE:
843 send_file(fds, client);
844 break;
846 case S_SENDING_DIR:
847 send_directory_listing(fds, client);
848 break;
850 case S_SENDING_CGI:
851 handle_cgi(fds, client);
852 break;
854 case S_CLOSING:
855 close_conn(fds, client);
856 break;
858 default:
859 /* unreachable */
860 abort();
864 void
865 loop(struct tls *ctx, int sock4, int sock6)
867 int i;
868 struct client clients[MAX_USERS];
869 struct pollfd fds[MAX_USERS];
871 connected_clients = 0;
873 for (i = 0; i < MAX_USERS; ++i) {
874 fds[i].fd = -1;
875 fds[i].events = POLLIN;
876 bzero(&clients[i], sizeof(struct client));
879 fds[0].fd = sock4;
880 fds[1].fd = sock6;
882 for (;;) {
883 if (poll(fds, MAX_USERS, INFTIM) == -1) {
884 if (errno == EINTR) {
885 fprintf(stderr, "connected clients: %d\n",
886 connected_clients);
887 continue;
889 fatal("poll: %s", strerror(errno));
892 for (i = 0; i < MAX_USERS; i++) {
893 if (fds[i].revents == 0)
894 continue;
896 if (fds[i].revents & (POLLERR|POLLNVAL))
897 fatal("bad fd %d: %s", fds[i].fd,
898 strerror(errno));
900 if (fds[i].revents & POLLHUP) {
901 /* fds[i] may be the fd of the stdin
902 * of a cgi script that has exited. */
903 if (!clients[i].waiting_on_child) {
904 close_conn(&fds[i], &clients[i]);
905 continue;
909 if (fds[i].fd == sock4)
910 do_accept(sock4, ctx, fds, clients);
911 else if (fds[i].fd == sock6)
912 do_accept(sock6, ctx, fds, clients);
913 else
914 handle(&fds[i], &clients[i]);