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_err)
312 || !parse_iri(c->req, &c->iri, &parse_err)) {
313 LOGI(c, "iri parse error: %s", parse_err);
314 start_reply(fds, c, BAD_REQUEST, "invalid request");
315 return;
318 puny_decode(c->iri.host, decoded, sizeof(decoded));
320 if (c->iri.port_no != conf.port
321 || strcmp(c->iri.schema, "gemini")
322 || strcmp(decoded, c->domain)) {
323 start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
324 return;
327 open_file(fds, c);
330 void
331 start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
333 char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
334 const char *lang;
335 size_t len;
337 c->code = code;
338 c->meta = meta;
339 c->state = S_INITIALIZING;
341 lang = vhost_lang(c->host, c->iri.path);
343 snprintf(buf, sizeof(buf), "%d ", code);
344 strlcat(buf, meta, sizeof(buf));
345 if (!strcmp(meta, "text/gemini") && lang != NULL) {
346 strlcat(buf, "; lang=", sizeof(buf));
347 strlcat(buf, lang, sizeof(buf));
350 len = strlcat(buf, "\r\n", sizeof(buf));
351 assert(len < sizeof(buf));
353 switch (tls_write(c->ctx, buf, len)) {
354 case -1:
355 close_conn(pfd, c);
356 return;
357 case TLS_WANT_POLLIN:
358 pfd->events = POLLIN;
359 return;
360 case TLS_WANT_POLLOUT:
361 pfd->events = POLLOUT;
362 return;
365 log_request(c, buf, sizeof(buf));
367 /* we don't need a body */
368 if (c->code != SUCCESS) {
369 close_conn(pfd, c);
370 return;
373 /* advance the state machine */
374 c->state = c->next;
375 handle(pfd, c);
378 void
379 start_cgi(const char *spath, const char *relpath, const char *query,
380 struct pollfd *fds, struct client *c)
382 char addr[NI_MAXHOST];
383 const char *ruser, *cissuer, *chash;
384 int e;
386 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
387 addr, sizeof(addr),
388 NULL, 0,
389 NI_NUMERICHOST);
390 if (e != 0)
391 goto err;
393 if (tls_peer_cert_provided(c->ctx)) {
394 ruser = tls_peer_cert_subject(c->ctx);
395 cissuer = tls_peer_cert_issuer(c->ctx);
396 chash = tls_peer_cert_hash(c->ctx);
397 } else {
398 ruser = NULL;
399 cissuer = NULL;
400 chash = NULL;
403 if (!send_string(exfd, spath)
404 || !send_string(exfd, relpath)
405 || !send_string(exfd, query)
406 || !send_string(exfd, addr)
407 || !send_string(exfd, ruser)
408 || !send_string(exfd, cissuer)
409 || !send_string(exfd, chash)
410 || !send_vhost(exfd, c->host))
411 goto err;
413 close(c->fd);
414 if ((c->fd = recv_fd(exfd)) == -1) {
415 start_reply(fds, c, TEMP_FAILURE, "internal server error");
416 return;
418 c->state = S_SENDING_CGI;
419 cgi_poll_on_child(fds, c);
420 c->code = -1;
421 /* handle_cgi(fds, c); */
422 return;
424 err:
425 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
426 fatal("cannot talk to the executor process");
429 void
430 send_file(struct pollfd *fds, struct client *c)
432 ssize_t ret, len;
434 /* ensure the correct state */
435 c->state = S_SENDING_FILE;
437 len = (c->buf + c->len) - c->i;
439 while (len > 0) {
440 switch (ret = tls_write(c->ctx, c->i, len)) {
441 case -1:
442 LOGE(c, "tls_write: %s", tls_error(c->ctx));
443 close_conn(fds, c);
444 return;
446 case TLS_WANT_POLLIN:
447 fds->events = POLLIN;
448 return;
450 case TLS_WANT_POLLOUT:
451 fds->events = POLLOUT;
452 return;
454 default:
455 c->i += ret;
456 len -= ret;
457 break;
461 close_conn(fds, c);
464 void
465 open_dir(struct pollfd *fds, struct client *c)
467 size_t len;
468 int dirfd;
469 char *before_file;
471 len = strlen(c->iri.path);
472 if (len > 0 && !ends_with(c->iri.path, "/")) {
473 redirect_canonical_dir(fds, c);
474 return;
477 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
478 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
479 if (!ends_with(c->sbuf, "/"))
480 strlcat(c->sbuf, "/", sizeof(c->sbuf));
481 before_file = strchr(c->sbuf, '\0');
482 len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
483 sizeof(c->sbuf));
484 if (len >= sizeof(c->sbuf)) {
485 start_reply(fds, c, TEMP_FAILURE, "internal server error");
486 return;
489 c->iri.path = c->sbuf;
491 /* close later unless we have to generate the dir listing */
492 dirfd = c->fd;
493 c->fd = -1;
495 switch (check_path(c, c->iri.path, &c->fd)) {
496 case FILE_EXECUTABLE:
497 if (starts_with(c->iri.path, c->host->cgi)) {
498 start_cgi(c->iri.path, "", c->iri.query, fds, c);
499 break;
502 /* fallthrough */
504 case FILE_EXISTS:
505 load_file(fds, c);
506 break;
508 case FILE_DIRECTORY:
509 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
510 break;
512 case FILE_MISSING:
513 *before_file = '\0';
515 if (!vhost_auto_index(c->host, c->iri.path)) {
516 start_reply(fds, c, NOT_FOUND, "not found");
517 break;
520 c->fd = dirfd;
521 c->next = S_SENDING_DIR;
523 if ((c->dir = fdopendir(c->fd)) == NULL) {
524 LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
525 c->fd, c->host->domain, c->iri.path, strerror(errno));
526 start_reply(fds, c, TEMP_FAILURE, "internal server error");
527 return;
529 c->off = 0;
531 start_reply(fds, c, SUCCESS, "text/gemini");
532 return;
534 default:
535 /* unreachable */
536 abort();
539 close(dirfd);
542 void
543 redirect_canonical_dir(struct pollfd *fds, struct client *c)
545 size_t len;
547 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
548 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
549 len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
551 if (len >= sizeof(c->sbuf)) {
552 start_reply(fds, c, TEMP_FAILURE, "internal server error");
553 return;
556 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
559 int
560 read_next_dir_entry(struct client *c)
562 struct dirent *d;
564 do {
565 errno = 0;
566 if ((d = readdir(c->dir)) == NULL) {
567 if (errno != 0)
568 LOGE(c, "readdir: %s", strerror(errno));
569 return 0;
571 } while (!strcmp(d->d_name, "."));
573 /* XXX: url escape */
574 snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
575 d->d_name, d->d_name);
576 c->len = strlen(c->sbuf);
577 c->off = 0;
579 return 1;
582 void
583 send_directory_listing(struct pollfd *fds, struct client *c)
585 ssize_t r;
587 while (1) {
588 if (c->len == 0) {
589 if (!read_next_dir_entry(c))
590 goto end;
593 while (c->len > 0) {
594 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
595 case -1:
596 goto end;
598 case TLS_WANT_POLLOUT:
599 fds->events = POLLOUT;
600 return;
602 case TLS_WANT_POLLIN:
603 fds->events = POLLIN;
604 return;
606 default:
607 c->off += r;
608 c->len -= r;
609 break;
614 end:
615 close_conn(fds, c);
618 void
619 cgi_poll_on_child(struct pollfd *fds, struct client *c)
621 int fd;
623 if (c->waiting_on_child)
624 return;
625 c->waiting_on_child = 1;
627 fds->events = POLLIN;
629 fd = fds->fd;
630 fds->fd = c->fd;
631 c->fd = fd;
634 void
635 cgi_poll_on_client(struct pollfd *fds, struct client *c)
637 int fd;
639 if (!c->waiting_on_child)
640 return;
641 c->waiting_on_child = 0;
643 fd = fds->fd;
644 fds->fd = c->fd;
645 c->fd = fd;
648 /* handle the read from the child process. Return like read(2) */
649 static ssize_t
650 read_from_cgi(struct client *c)
652 void *buf;
653 size_t len;
654 ssize_t r;
656 /* if we haven't read a whole response line, we want to
657 * continue reading. */
659 if (c->code == -1) {
660 buf = c->sbuf + c->len;
661 len = sizeof(c->sbuf) - c->len;
662 } else {
663 buf = c->sbuf;
664 len = sizeof(c->sbuf);
667 r = read(c->fd, buf, len);
668 if (r == 0 || r == -1)
669 return r;
671 c->len += r;
672 c->off = 0;
674 if (c->code != -1)
675 return r;
677 if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
678 c->code = 0;
679 log_request(c, c->sbuf, c->len);
682 return r;
685 void
686 handle_cgi(struct pollfd *fds, struct client *c)
688 ssize_t r;
690 /* ensure c->fd is the child and fds->fd the client */
691 cgi_poll_on_client(fds, c);
693 while (1) {
694 if (c->code == -1 || c->len == 0) {
695 switch (r = read_from_cgi(c)) {
696 case 0:
697 goto end;
699 case -1:
700 if (errno == EAGAIN || errno == EWOULDBLOCK) {
701 cgi_poll_on_child(fds, c);
702 return;
704 goto end;
708 if (c->code == -1) {
709 cgi_poll_on_child(fds, c);
710 return;
713 while (c->len > 0) {
714 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
715 case -1:
716 goto end;
718 case TLS_WANT_POLLOUT:
719 fds->events = POLLOUT;
720 return;
722 case TLS_WANT_POLLIN:
723 fds->events = POLLIN;
724 return;
726 default:
727 c->off += r;
728 c->len -= r;
729 break;
734 end:
735 close_conn(fds, c);
738 void
739 close_conn(struct pollfd *pfd, struct client *c)
741 c->state = S_CLOSING;
743 switch (tls_close(c->ctx)) {
744 case TLS_WANT_POLLIN:
745 pfd->events = POLLIN;
746 return;
747 case TLS_WANT_POLLOUT:
748 pfd->events = POLLOUT;
749 return;
752 connected_clients--;
754 tls_free(c->ctx);
755 c->ctx = NULL;
757 if (c->buf != MAP_FAILED)
758 munmap(c->buf, c->len);
760 if (c->fd != -1)
761 close(c->fd);
763 if (c->dir != NULL)
764 closedir(c->dir);
766 close(pfd->fd);
767 pfd->fd = -1;
770 void
771 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
773 int i, fd;
774 struct sockaddr_storage addr;
775 socklen_t len;
777 len = sizeof(addr);
778 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
779 if (errno == EWOULDBLOCK)
780 return;
781 fatal("accept: %s", strerror(errno));
784 mark_nonblock(fd);
786 for (i = 0; i < MAX_USERS; ++i) {
787 if (fds[i].fd == -1) {
788 bzero(&clients[i], sizeof(struct client));
789 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
790 break; /* goodbye fd! */
792 fds[i].fd = fd;
793 fds[i].events = POLLIN;
795 clients[i].state = S_HANDSHAKE;
796 clients[i].next = S_SENDING_FILE;
797 clients[i].fd = -1;
798 clients[i].waiting_on_child = 0;
799 clients[i].buf = MAP_FAILED;
800 clients[i].dir = NULL;
801 clients[i].addr = addr;
803 connected_clients++;
804 return;
808 close(fd);
811 void
812 handle(struct pollfd *fds, struct client *client)
814 switch (client->state) {
815 case S_HANDSHAKE:
816 handle_handshake(fds, client);
817 break;
819 case S_OPEN:
820 handle_open_conn(fds, client);
821 break;
823 case S_INITIALIZING:
824 start_reply(fds, client, client->code, client->meta);
825 break;
827 case S_SENDING_FILE:
828 send_file(fds, client);
829 break;
831 case S_SENDING_DIR:
832 send_directory_listing(fds, client);
833 break;
835 case S_SENDING_CGI:
836 handle_cgi(fds, client);
837 break;
839 case S_CLOSING:
840 close_conn(fds, client);
841 break;
843 default:
844 /* unreachable */
845 abort();
849 void
850 loop(struct tls *ctx, int sock4, int sock6)
852 int i;
853 struct client clients[MAX_USERS];
854 struct pollfd fds[MAX_USERS];
856 connected_clients = 0;
858 for (i = 0; i < MAX_USERS; ++i) {
859 fds[i].fd = -1;
860 fds[i].events = POLLIN;
861 bzero(&clients[i], sizeof(struct client));
864 fds[0].fd = sock4;
865 fds[1].fd = sock6;
867 for (;;) {
868 if (poll(fds, MAX_USERS, INFTIM) == -1) {
869 if (errno == EINTR) {
870 fprintf(stderr, "connected clients: %d\n",
871 connected_clients);
872 continue;
874 fatal("poll: %s", strerror(errno));
877 for (i = 0; i < MAX_USERS; i++) {
878 if (fds[i].revents == 0)
879 continue;
881 if (fds[i].revents & (POLLERR|POLLNVAL))
882 fatal("bad fd %d: %s", fds[i].fd,
883 strerror(errno));
885 if (fds[i].revents & POLLHUP) {
886 /* fds[i] may be the fd of the stdin
887 * of a cgi script that has exited. */
888 if (!clients[i].waiting_on_child) {
889 close_conn(&fds[i], &clients[i]);
890 continue;
894 if (fds[i].fd == sock4)
895 do_accept(sock4, ctx, fds, clients);
896 else if (fds[i].fd == sock6)
897 do_accept(sock6, ctx, fds, clients);
898 else
899 handle(&fds[i], &clients[i]);