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 for (loc = v->locations; loc->match != NULL; ++loc) {
39 if (!fnmatch(loc->match, path, 0)) {
40 if (loc->lang != NULL)
41 lang = loc->lang;
42 }
43 }
45 return lang;
46 }
48 const char *
49 vhost_default_mime(struct vhost *v, const char *path)
50 {
51 struct location *loc;
52 const char *default_mime = "application/octet-stream";
54 for (loc = v->locations; loc->match != NULL; ++loc) {
55 if (!fnmatch(loc->match, path, 0)) {
56 if (loc->default_mime != NULL)
57 default_mime = loc->default_mime;
58 }
59 }
61 return default_mime;
62 }
64 const char *
65 vhost_index(struct vhost *v, const char *path)
66 {
67 struct location *loc;
68 const char *index = "index.gmi";
70 for (loc = v->locations; loc->match != NULL; ++loc) {
71 if (!fnmatch(loc->match, path, 0)) {
72 if (loc->index != NULL)
73 index = loc->index;
74 }
75 }
77 return index;
78 }
80 int
81 vhost_auto_index(struct vhost *v, const char *path)
82 {
83 struct location *loc;
84 int auto_index = 0;
86 for (loc = v->locations; loc->match != NULL; ++loc) {
87 if (!fnmatch(loc->match, path, 0)) {
88 if (loc->auto_index)
89 auto_index = loc->auto_index;
90 }
91 }
93 return auto_index == 1;
94 }
96 int
97 check_path(struct client *c, const char *path, int *fd)
98 {
99 struct stat sb;
100 const char *p;
101 int flags;
103 assert(path != NULL);
105 if (*path == '\0')
106 p = ".";
107 else if (*path == '/')
108 /* in send_dir we add an initial / (to be
109 * redirect-friendly), but here we want to skip it */
110 p = path+1;
111 else
112 p = path;
114 flags = O_RDONLY | O_NOFOLLOW;
116 if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
117 return FILE_MISSING;
119 if (fstat(*fd, &sb) == -1) {
120 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
121 return FILE_MISSING;
124 if (S_ISDIR(sb.st_mode))
125 return FILE_DIRECTORY;
127 if (sb.st_mode & S_IXUSR)
128 return FILE_EXECUTABLE;
130 return FILE_EXISTS;
133 void
134 open_file(struct pollfd *fds, struct client *c)
136 switch (check_path(c, c->iri.path, &c->fd)) {
137 case FILE_EXECUTABLE:
138 if (starts_with(c->iri.path, c->host->cgi)) {
139 start_cgi(c->iri.path, "", c->iri.query, fds, c);
140 return;
143 /* fallthrough */
145 case FILE_EXISTS:
146 load_file(fds, c);
147 return;
149 case FILE_DIRECTORY:
150 open_dir(fds, c);
151 return;
153 case FILE_MISSING:
154 if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi)) {
155 check_for_cgi(c->iri.path, c->iri.query, fds, c);
156 return;
158 start_reply(fds, c, NOT_FOUND, "not found");
159 return;
161 default:
162 /* unreachable */
163 abort();
167 void
168 load_file(struct pollfd *fds, struct client *c)
170 if ((c->len = filesize(c->fd)) == -1) {
171 LOGE(c, "failed to get file size for %s", c->iri.path);
172 start_reply(fds, c, TEMP_FAILURE, "internal server error");
173 return;
176 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
177 c->fd, 0)) == MAP_FAILED) {
178 LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
179 start_reply(fds, c, TEMP_FAILURE, "internal server error");
180 return;
182 c->i = c->buf;
183 c->next = S_SENDING_FILE;
184 start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
187 /*
188 * the inverse of this algorithm, i.e. starting from the start of the
189 * path + strlen(cgi), and checking if each component, should be
190 * faster. But it's tedious to write. This does the opposite: starts
191 * from the end and strip one component at a time, until either an
192 * executable is found or we emptied the path.
193 */
194 void
195 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
197 char *end;
198 end = strchr(path, '\0');
200 /* NB: assume CGI is enabled and path matches cgi */
202 while (end > path) {
203 /* go up one level. UNIX paths are simple and POSIX
204 * dirname, with its ambiguities on if the given path
205 * is changed or not, gives me headaches. */
206 while (*end != '/')
207 end--;
208 *end = '\0';
210 switch (check_path(c, path, &c->fd)) {
211 case FILE_EXECUTABLE:
212 start_cgi(path, end+1, query, fds,c);
213 return;
214 case FILE_MISSING:
215 break;
216 default:
217 goto err;
220 *end = '/';
221 end--;
224 err:
225 start_reply(fds, c, NOT_FOUND, "not found");
226 return;
229 void
230 mark_nonblock(int fd)
232 int flags;
234 if ((flags = fcntl(fd, F_GETFL)) == -1)
235 fatal("fcntl(F_GETFL): %s", strerror(errno));
236 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
237 fatal("fcntl(F_SETFL): %s", strerror(errno));
240 void
241 handle_handshake(struct pollfd *fds, struct client *c)
243 struct vhost *h;
244 const char *servname;
246 switch (tls_handshake(c->ctx)) {
247 case 0: /* success */
248 case -1: /* already handshaked */
249 break;
250 case TLS_WANT_POLLIN:
251 fds->events = POLLIN;
252 return;
253 case TLS_WANT_POLLOUT:
254 fds->events = POLLOUT;
255 return;
256 default:
257 /* unreachable */
258 abort();
261 servname = tls_conn_servername(c->ctx);
263 for (h = hosts; h->domain != NULL; ++h) {
264 if (!strcmp(h->domain, "*"))
265 break;
267 if (servname != NULL && !fnmatch(h->domain, servname, 0))
268 break;
271 if (h->domain != NULL) {
272 c->state = S_OPEN;
273 c->host = h;
274 handle_open_conn(fds, c);
275 return;
278 if (servname != NULL)
279 strncpy(c->req, servname, sizeof(c->req));
280 else
281 strncpy(c->req, "null", sizeof(c->req));
283 start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI");
286 void
287 handle_open_conn(struct pollfd *fds, struct client *c)
289 const char *parse_err = "invalid request";
291 bzero(c->req, sizeof(c->req));
292 bzero(&c->iri, sizeof(c->iri));
294 switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
295 case -1:
296 LOGE(c, "tls_read: %s", tls_error(c->ctx));
297 close_conn(fds, c);
298 return;
300 case TLS_WANT_POLLIN:
301 fds->events = POLLIN;
302 return;
304 case TLS_WANT_POLLOUT:
305 fds->events = POLLOUT;
306 return;
309 if (!trim_req_iri(c->req) || !parse_iri(c->req, &c->iri, &parse_err)) {
310 start_reply(fds, c, BAD_REQUEST, parse_err);
311 return;
314 /* XXX: we should check that the SNI matches the requested host */
315 if (strcmp(c->iri.schema, "gemini") || c->iri.port_no != conf.port) {
316 start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
317 return;
320 open_file(fds, c);
323 void
324 start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
326 char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
327 const char *lang;
328 size_t len;
330 c->code = code;
331 c->meta = meta;
332 c->state = S_INITIALIZING;
334 lang = vhost_lang(c->host, c->iri.path);
336 snprintf(buf, sizeof(buf), "%d ", code);
337 strlcat(buf, meta, sizeof(buf));
338 if (!strcmp(meta, "text/gemini") && lang != NULL) {
339 strlcat(buf, "; lang=", sizeof(buf));
340 strlcat(buf, lang, sizeof(buf));
343 len = strlcat(buf, "\r\n", sizeof(buf));
344 assert(len < sizeof(buf));
346 switch (tls_write(c->ctx, buf, len)) {
347 case -1:
348 close_conn(pfd, c);
349 return;
350 case TLS_WANT_POLLIN:
351 pfd->events = POLLIN;
352 return;
353 case TLS_WANT_POLLOUT:
354 pfd->events = POLLOUT;
355 return;
358 log_request(c, buf, sizeof(buf));
360 /* we don't need a body */
361 if (c->code != SUCCESS) {
362 close_conn(pfd, c);
363 return;
366 /* advance the state machine */
367 c->state = c->next;
368 handle(pfd, c);
371 void
372 start_cgi(const char *spath, const char *relpath, const char *query,
373 struct pollfd *fds, struct client *c)
375 char addr[NI_MAXHOST];
376 const char *ruser, *cissuer, *chash;
377 int e;
379 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
380 addr, sizeof(addr),
381 NULL, 0,
382 NI_NUMERICHOST);
383 if (e != 0)
384 goto err;
386 if (tls_peer_cert_provided(c->ctx)) {
387 ruser = tls_peer_cert_subject(c->ctx);
388 cissuer = tls_peer_cert_issuer(c->ctx);
389 chash = tls_peer_cert_hash(c->ctx);
390 } else {
391 ruser = NULL;
392 cissuer = NULL;
393 chash = NULL;
396 if (!send_string(exfd, spath)
397 || !send_string(exfd, relpath)
398 || !send_string(exfd, query)
399 || !send_string(exfd, addr)
400 || !send_string(exfd, ruser)
401 || !send_string(exfd, cissuer)
402 || !send_string(exfd, chash)
403 || !send_vhost(exfd, c->host))
404 goto err;
406 close(c->fd);
407 if ((c->fd = recv_fd(exfd)) == -1) {
408 start_reply(fds, c, TEMP_FAILURE, "internal server error");
409 return;
411 c->state = S_SENDING_CGI;
412 cgi_poll_on_child(fds, c);
413 c->code = -1;
414 /* handle_cgi(fds, c); */
415 return;
417 err:
418 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
419 fatal("cannot talk to the executor process");
422 void
423 send_file(struct pollfd *fds, struct client *c)
425 ssize_t ret, len;
427 /* ensure the correct state */
428 c->state = S_SENDING_FILE;
430 len = (c->buf + c->len) - c->i;
432 while (len > 0) {
433 switch (ret = tls_write(c->ctx, c->i, len)) {
434 case -1:
435 LOGE(c, "tls_write: %s", tls_error(c->ctx));
436 close_conn(fds, c);
437 return;
439 case TLS_WANT_POLLIN:
440 fds->events = POLLIN;
441 return;
443 case TLS_WANT_POLLOUT:
444 fds->events = POLLOUT;
445 return;
447 default:
448 c->i += ret;
449 len -= ret;
450 break;
454 close_conn(fds, c);
457 void
458 open_dir(struct pollfd *fds, struct client *c)
460 size_t len;
461 int dirfd;
462 char *before_file;
464 len = strlen(c->iri.path);
465 if (len > 0 && !ends_with(c->iri.path, "/")) {
466 redirect_canonical_dir(fds, c);
467 return;
470 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
471 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
472 if (!ends_with(c->sbuf, "/"))
473 strlcat(c->sbuf, "/", sizeof(c->sbuf));
474 before_file = strchr(c->sbuf, '\0');
475 len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
476 sizeof(c->sbuf));
477 if (len >= sizeof(c->sbuf)) {
478 start_reply(fds, c, TEMP_FAILURE, "internal server error");
479 return;
482 c->iri.path = c->sbuf;
484 /* close later unless we have to generate the dir listing */
485 dirfd = c->fd;
486 c->fd = -1;
488 switch (check_path(c, c->iri.path, &c->fd)) {
489 case FILE_EXECUTABLE:
490 if (starts_with(c->iri.path, c->host->cgi)) {
491 start_cgi(c->iri.path, "", c->iri.query, fds, c);
492 break;
495 /* fallthrough */
497 case FILE_EXISTS:
498 load_file(fds, c);
499 break;
501 case FILE_DIRECTORY:
502 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
503 break;
505 case FILE_MISSING:
506 *before_file = '\0';
508 if (!vhost_auto_index(c->host, c->iri.path)) {
509 start_reply(fds, c, NOT_FOUND, "not found");
510 break;
513 c->fd = dirfd;
514 c->next = S_SENDING_DIR;
516 if ((c->dir = fdopendir(c->fd)) == NULL) {
517 LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
518 c->fd, c->host->domain, c->iri.path, strerror(errno));
519 start_reply(fds, c, TEMP_FAILURE, "internal server error");
520 return;
522 c->off = 0;
524 start_reply(fds, c, SUCCESS, "text/gemini");
525 return;
527 default:
528 /* unreachable */
529 abort();
532 close(dirfd);
535 void
536 redirect_canonical_dir(struct pollfd *fds, struct client *c)
538 size_t len;
540 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
541 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
542 len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
544 if (len >= sizeof(c->sbuf)) {
545 start_reply(fds, c, TEMP_FAILURE, "internal server error");
546 return;
549 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
552 int
553 read_next_dir_entry(struct client *c)
555 struct dirent *d;
557 do {
558 errno = 0;
559 if ((d = readdir(c->dir)) == NULL) {
560 if (errno != 0)
561 LOGE(c, "readdir: %s", strerror(errno));
562 return 0;
564 } while (!strcmp(d->d_name, "."));
566 /* XXX: url escape */
567 snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
568 d->d_name, d->d_name);
569 c->len = strlen(c->sbuf);
570 c->off = 0;
572 return 1;
575 void
576 send_directory_listing(struct pollfd *fds, struct client *c)
578 ssize_t r;
580 while (1) {
581 if (c->len == 0) {
582 if (!read_next_dir_entry(c))
583 goto end;
586 while (c->len > 0) {
587 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
588 case -1:
589 goto end;
591 case TLS_WANT_POLLOUT:
592 fds->events = POLLOUT;
593 return;
595 case TLS_WANT_POLLIN:
596 fds->events = POLLIN;
597 return;
599 default:
600 c->off += r;
601 c->len -= r;
602 break;
607 end:
608 close_conn(fds, c);
611 void
612 cgi_poll_on_child(struct pollfd *fds, struct client *c)
614 int fd;
616 if (c->waiting_on_child)
617 return;
618 c->waiting_on_child = 1;
620 fds->events = POLLIN;
622 fd = fds->fd;
623 fds->fd = c->fd;
624 c->fd = fd;
627 void
628 cgi_poll_on_client(struct pollfd *fds, struct client *c)
630 int fd;
632 if (!c->waiting_on_child)
633 return;
634 c->waiting_on_child = 0;
636 fd = fds->fd;
637 fds->fd = c->fd;
638 c->fd = fd;
641 /* handle the read from the child process. Return like read(2) */
642 static ssize_t
643 read_from_cgi(struct client *c)
645 void *buf;
646 size_t len;
647 ssize_t r;
649 /* if we haven't read a whole response line, we want to
650 * continue reading. */
652 if (c->code == -1) {
653 buf = c->sbuf + c->len;
654 len = sizeof(c->sbuf) - c->len;
655 } else {
656 buf = c->sbuf;
657 len = sizeof(c->sbuf);
660 r = read(c->fd, buf, len);
661 if (r == 0 || r == -1)
662 return r;
664 c->len += r;
665 c->off = 0;
667 if (c->code != -1)
668 return r;
670 if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
671 c->code = 0;
672 log_request(c, c->sbuf, c->len);
675 return r;
678 void
679 handle_cgi(struct pollfd *fds, struct client *c)
681 ssize_t r;
683 /* ensure c->fd is the child and fds->fd the client */
684 cgi_poll_on_client(fds, c);
686 while (1) {
687 if (c->code == -1 || c->len == 0) {
688 switch (r = read_from_cgi(c)) {
689 case 0:
690 goto end;
692 case -1:
693 if (errno == EAGAIN || errno == EWOULDBLOCK) {
694 cgi_poll_on_child(fds, c);
695 return;
697 goto end;
701 if (c->code == -1) {
702 cgi_poll_on_child(fds, c);
703 return;
706 while (c->len > 0) {
707 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
708 case -1:
709 goto end;
711 case TLS_WANT_POLLOUT:
712 fds->events = POLLOUT;
713 return;
715 case TLS_WANT_POLLIN:
716 fds->events = POLLIN;
717 return;
719 default:
720 c->off += r;
721 c->len -= r;
722 break;
727 end:
728 close_conn(fds, c);
731 void
732 close_conn(struct pollfd *pfd, struct client *c)
734 c->state = S_CLOSING;
736 switch (tls_close(c->ctx)) {
737 case TLS_WANT_POLLIN:
738 pfd->events = POLLIN;
739 return;
740 case TLS_WANT_POLLOUT:
741 pfd->events = POLLOUT;
742 return;
745 connected_clients--;
747 tls_free(c->ctx);
748 c->ctx = NULL;
750 if (c->buf != MAP_FAILED)
751 munmap(c->buf, c->len);
753 if (c->fd != -1)
754 close(c->fd);
756 if (c->dir != NULL)
757 closedir(c->dir);
759 close(pfd->fd);
760 pfd->fd = -1;
763 void
764 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
766 int i, fd;
767 struct sockaddr_storage addr;
768 socklen_t len;
770 len = sizeof(addr);
771 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
772 if (errno == EWOULDBLOCK)
773 return;
774 fatal("accept: %s", strerror(errno));
777 mark_nonblock(fd);
779 for (i = 0; i < MAX_USERS; ++i) {
780 if (fds[i].fd == -1) {
781 bzero(&clients[i], sizeof(struct client));
782 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
783 break; /* goodbye fd! */
785 fds[i].fd = fd;
786 fds[i].events = POLLIN;
788 clients[i].state = S_HANDSHAKE;
789 clients[i].next = S_SENDING_FILE;
790 clients[i].fd = -1;
791 clients[i].waiting_on_child = 0;
792 clients[i].buf = MAP_FAILED;
793 clients[i].dir = NULL;
794 clients[i].addr = addr;
796 connected_clients++;
797 return;
801 close(fd);
804 void
805 handle(struct pollfd *fds, struct client *client)
807 switch (client->state) {
808 case S_HANDSHAKE:
809 handle_handshake(fds, client);
810 break;
812 case S_OPEN:
813 handle_open_conn(fds, client);
814 break;
816 case S_INITIALIZING:
817 start_reply(fds, client, client->code, client->meta);
818 break;
820 case S_SENDING_FILE:
821 send_file(fds, client);
822 break;
824 case S_SENDING_DIR:
825 send_directory_listing(fds, client);
826 break;
828 case S_SENDING_CGI:
829 handle_cgi(fds, client);
830 break;
832 case S_CLOSING:
833 close_conn(fds, client);
834 break;
836 default:
837 /* unreachable */
838 abort();
842 void
843 loop(struct tls *ctx, int sock4, int sock6)
845 int i;
846 struct client clients[MAX_USERS];
847 struct pollfd fds[MAX_USERS];
849 connected_clients = 0;
851 for (i = 0; i < MAX_USERS; ++i) {
852 fds[i].fd = -1;
853 fds[i].events = POLLIN;
854 bzero(&clients[i], sizeof(struct client));
857 fds[0].fd = sock4;
858 fds[1].fd = sock6;
860 for (;;) {
861 if (poll(fds, MAX_USERS, INFTIM) == -1) {
862 if (errno == EINTR) {
863 fprintf(stderr, "connected clients: %d\n",
864 connected_clients);
865 continue;
867 fatal("poll: %s", strerror(errno));
870 for (i = 0; i < MAX_USERS; i++) {
871 if (fds[i].revents == 0)
872 continue;
874 if (fds[i].revents & (POLLERR|POLLNVAL))
875 fatal("bad fd %d: %s", fds[i].fd,
876 strerror(errno));
878 if (fds[i].revents & POLLHUP) {
879 /* fds[i] may be the fd of the stdin
880 * of a cgi script that has exited. */
881 if (!clients[i].waiting_on_child) {
882 close_conn(&fds[i], &clients[i]);
883 continue;
887 if (fds[i].fd == sock4)
888 do_accept(sock4, ctx, fds, clients);
889 else if (fds[i].fd == sock6)
890 do_accept(sock6, ctx, fds, clients);
891 else
892 handle(&fds[i], &clients[i]);