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);
266 for (h = hosts; h->domain != NULL; ++h) {
267 if (!strcmp(h->domain, "*"))
268 break;
270 if (servname != NULL && !fnmatch(h->domain, servname, 0))
271 break;
274 if (h->domain != NULL) {
275 c->state = S_OPEN;
276 c->host = h;
277 handle_open_conn(fds, c);
278 return;
281 if (servname != NULL)
282 strncpy(c->req, servname, sizeof(c->req));
283 else
284 strncpy(c->req, "null", sizeof(c->req));
286 start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI");
289 void
290 handle_open_conn(struct pollfd *fds, struct client *c)
292 const char *parse_err = "invalid request";
294 bzero(c->req, sizeof(c->req));
295 bzero(&c->iri, sizeof(c->iri));
297 switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
298 case -1:
299 LOGE(c, "tls_read: %s", tls_error(c->ctx));
300 close_conn(fds, c);
301 return;
303 case TLS_WANT_POLLIN:
304 fds->events = POLLIN;
305 return;
307 case TLS_WANT_POLLOUT:
308 fds->events = POLLOUT;
309 return;
312 if (!trim_req_iri(c->req) || !parse_iri(c->req, &c->iri, &parse_err)) {
313 start_reply(fds, c, BAD_REQUEST, parse_err);
314 return;
317 /* XXX: we should check that the SNI matches the requested host */
318 if (strcmp(c->iri.schema, "gemini") || c->iri.port_no != conf.port) {
319 start_reply(fds, c, PROXY_REFUSED, "won't proxy request");
320 return;
323 open_file(fds, c);
326 void
327 start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
329 char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
330 const char *lang;
331 size_t len;
333 c->code = code;
334 c->meta = meta;
335 c->state = S_INITIALIZING;
337 lang = vhost_lang(c->host, c->iri.path);
339 snprintf(buf, sizeof(buf), "%d ", code);
340 strlcat(buf, meta, sizeof(buf));
341 if (!strcmp(meta, "text/gemini") && lang != NULL) {
342 strlcat(buf, "; lang=", sizeof(buf));
343 strlcat(buf, lang, sizeof(buf));
346 len = strlcat(buf, "\r\n", sizeof(buf));
347 assert(len < sizeof(buf));
349 switch (tls_write(c->ctx, buf, len)) {
350 case -1:
351 close_conn(pfd, c);
352 return;
353 case TLS_WANT_POLLIN:
354 pfd->events = POLLIN;
355 return;
356 case TLS_WANT_POLLOUT:
357 pfd->events = POLLOUT;
358 return;
361 log_request(c, buf, sizeof(buf));
363 /* we don't need a body */
364 if (c->code != SUCCESS) {
365 close_conn(pfd, c);
366 return;
369 /* advance the state machine */
370 c->state = c->next;
371 handle(pfd, c);
374 void
375 start_cgi(const char *spath, const char *relpath, const char *query,
376 struct pollfd *fds, struct client *c)
378 char addr[NI_MAXHOST];
379 const char *ruser, *cissuer, *chash;
380 int e;
382 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
383 addr, sizeof(addr),
384 NULL, 0,
385 NI_NUMERICHOST);
386 if (e != 0)
387 goto err;
389 if (tls_peer_cert_provided(c->ctx)) {
390 ruser = tls_peer_cert_subject(c->ctx);
391 cissuer = tls_peer_cert_issuer(c->ctx);
392 chash = tls_peer_cert_hash(c->ctx);
393 } else {
394 ruser = NULL;
395 cissuer = NULL;
396 chash = NULL;
399 if (!send_string(exfd, spath)
400 || !send_string(exfd, relpath)
401 || !send_string(exfd, query)
402 || !send_string(exfd, addr)
403 || !send_string(exfd, ruser)
404 || !send_string(exfd, cissuer)
405 || !send_string(exfd, chash)
406 || !send_vhost(exfd, c->host))
407 goto err;
409 close(c->fd);
410 if ((c->fd = recv_fd(exfd)) == -1) {
411 start_reply(fds, c, TEMP_FAILURE, "internal server error");
412 return;
414 c->state = S_SENDING_CGI;
415 cgi_poll_on_child(fds, c);
416 c->code = -1;
417 /* handle_cgi(fds, c); */
418 return;
420 err:
421 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
422 fatal("cannot talk to the executor process");
425 void
426 send_file(struct pollfd *fds, struct client *c)
428 ssize_t ret, len;
430 /* ensure the correct state */
431 c->state = S_SENDING_FILE;
433 len = (c->buf + c->len) - c->i;
435 while (len > 0) {
436 switch (ret = tls_write(c->ctx, c->i, len)) {
437 case -1:
438 LOGE(c, "tls_write: %s", tls_error(c->ctx));
439 close_conn(fds, c);
440 return;
442 case TLS_WANT_POLLIN:
443 fds->events = POLLIN;
444 return;
446 case TLS_WANT_POLLOUT:
447 fds->events = POLLOUT;
448 return;
450 default:
451 c->i += ret;
452 len -= ret;
453 break;
457 close_conn(fds, c);
460 void
461 open_dir(struct pollfd *fds, struct client *c)
463 size_t len;
464 int dirfd;
465 char *before_file;
467 len = strlen(c->iri.path);
468 if (len > 0 && !ends_with(c->iri.path, "/")) {
469 redirect_canonical_dir(fds, c);
470 return;
473 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
474 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
475 if (!ends_with(c->sbuf, "/"))
476 strlcat(c->sbuf, "/", sizeof(c->sbuf));
477 before_file = strchr(c->sbuf, '\0');
478 len = strlcat(c->sbuf, vhost_index(c->host, c->iri.path),
479 sizeof(c->sbuf));
480 if (len >= sizeof(c->sbuf)) {
481 start_reply(fds, c, TEMP_FAILURE, "internal server error");
482 return;
485 c->iri.path = c->sbuf;
487 /* close later unless we have to generate the dir listing */
488 dirfd = c->fd;
489 c->fd = -1;
491 switch (check_path(c, c->iri.path, &c->fd)) {
492 case FILE_EXECUTABLE:
493 if (starts_with(c->iri.path, c->host->cgi)) {
494 start_cgi(c->iri.path, "", c->iri.query, fds, c);
495 break;
498 /* fallthrough */
500 case FILE_EXISTS:
501 load_file(fds, c);
502 break;
504 case FILE_DIRECTORY:
505 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
506 break;
508 case FILE_MISSING:
509 *before_file = '\0';
511 if (!vhost_auto_index(c->host, c->iri.path)) {
512 start_reply(fds, c, NOT_FOUND, "not found");
513 break;
516 c->fd = dirfd;
517 c->next = S_SENDING_DIR;
519 if ((c->dir = fdopendir(c->fd)) == NULL) {
520 LOGE(c, "can't fdopendir(%d) (vhost:%s) %s: %s",
521 c->fd, c->host->domain, c->iri.path, strerror(errno));
522 start_reply(fds, c, TEMP_FAILURE, "internal server error");
523 return;
525 c->off = 0;
527 start_reply(fds, c, SUCCESS, "text/gemini");
528 return;
530 default:
531 /* unreachable */
532 abort();
535 close(dirfd);
538 void
539 redirect_canonical_dir(struct pollfd *fds, struct client *c)
541 size_t len;
543 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
544 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
545 len = strlcat(c->sbuf, "/", sizeof(c->sbuf));
547 if (len >= sizeof(c->sbuf)) {
548 start_reply(fds, c, TEMP_FAILURE, "internal server error");
549 return;
552 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
555 int
556 read_next_dir_entry(struct client *c)
558 struct dirent *d;
560 do {
561 errno = 0;
562 if ((d = readdir(c->dir)) == NULL) {
563 if (errno != 0)
564 LOGE(c, "readdir: %s", strerror(errno));
565 return 0;
567 } while (!strcmp(d->d_name, "."));
569 /* XXX: url escape */
570 snprintf(c->sbuf, sizeof(c->sbuf), "=> %s %s\n",
571 d->d_name, d->d_name);
572 c->len = strlen(c->sbuf);
573 c->off = 0;
575 return 1;
578 void
579 send_directory_listing(struct pollfd *fds, struct client *c)
581 ssize_t r;
583 while (1) {
584 if (c->len == 0) {
585 if (!read_next_dir_entry(c))
586 goto end;
589 while (c->len > 0) {
590 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
591 case -1:
592 goto end;
594 case TLS_WANT_POLLOUT:
595 fds->events = POLLOUT;
596 return;
598 case TLS_WANT_POLLIN:
599 fds->events = POLLIN;
600 return;
602 default:
603 c->off += r;
604 c->len -= r;
605 break;
610 end:
611 close_conn(fds, c);
614 void
615 cgi_poll_on_child(struct pollfd *fds, struct client *c)
617 int fd;
619 if (c->waiting_on_child)
620 return;
621 c->waiting_on_child = 1;
623 fds->events = POLLIN;
625 fd = fds->fd;
626 fds->fd = c->fd;
627 c->fd = fd;
630 void
631 cgi_poll_on_client(struct pollfd *fds, struct client *c)
633 int fd;
635 if (!c->waiting_on_child)
636 return;
637 c->waiting_on_child = 0;
639 fd = fds->fd;
640 fds->fd = c->fd;
641 c->fd = fd;
644 /* handle the read from the child process. Return like read(2) */
645 static ssize_t
646 read_from_cgi(struct client *c)
648 void *buf;
649 size_t len;
650 ssize_t r;
652 /* if we haven't read a whole response line, we want to
653 * continue reading. */
655 if (c->code == -1) {
656 buf = c->sbuf + c->len;
657 len = sizeof(c->sbuf) - c->len;
658 } else {
659 buf = c->sbuf;
660 len = sizeof(c->sbuf);
663 r = read(c->fd, buf, len);
664 if (r == 0 || r == -1)
665 return r;
667 c->len += r;
668 c->off = 0;
670 if (c->code != -1)
671 return r;
673 if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
674 c->code = 0;
675 log_request(c, c->sbuf, c->len);
678 return r;
681 void
682 handle_cgi(struct pollfd *fds, struct client *c)
684 ssize_t r;
686 /* ensure c->fd is the child and fds->fd the client */
687 cgi_poll_on_client(fds, c);
689 while (1) {
690 if (c->code == -1 || c->len == 0) {
691 switch (r = read_from_cgi(c)) {
692 case 0:
693 goto end;
695 case -1:
696 if (errno == EAGAIN || errno == EWOULDBLOCK) {
697 cgi_poll_on_child(fds, c);
698 return;
700 goto end;
704 if (c->code == -1) {
705 cgi_poll_on_child(fds, c);
706 return;
709 while (c->len > 0) {
710 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
711 case -1:
712 goto end;
714 case TLS_WANT_POLLOUT:
715 fds->events = POLLOUT;
716 return;
718 case TLS_WANT_POLLIN:
719 fds->events = POLLIN;
720 return;
722 default:
723 c->off += r;
724 c->len -= r;
725 break;
730 end:
731 close_conn(fds, c);
734 void
735 close_conn(struct pollfd *pfd, struct client *c)
737 c->state = S_CLOSING;
739 switch (tls_close(c->ctx)) {
740 case TLS_WANT_POLLIN:
741 pfd->events = POLLIN;
742 return;
743 case TLS_WANT_POLLOUT:
744 pfd->events = POLLOUT;
745 return;
748 connected_clients--;
750 tls_free(c->ctx);
751 c->ctx = NULL;
753 if (c->buf != MAP_FAILED)
754 munmap(c->buf, c->len);
756 if (c->fd != -1)
757 close(c->fd);
759 if (c->dir != NULL)
760 closedir(c->dir);
762 close(pfd->fd);
763 pfd->fd = -1;
766 void
767 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
769 int i, fd;
770 struct sockaddr_storage addr;
771 socklen_t len;
773 len = sizeof(addr);
774 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
775 if (errno == EWOULDBLOCK)
776 return;
777 fatal("accept: %s", strerror(errno));
780 mark_nonblock(fd);
782 for (i = 0; i < MAX_USERS; ++i) {
783 if (fds[i].fd == -1) {
784 bzero(&clients[i], sizeof(struct client));
785 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
786 break; /* goodbye fd! */
788 fds[i].fd = fd;
789 fds[i].events = POLLIN;
791 clients[i].state = S_HANDSHAKE;
792 clients[i].next = S_SENDING_FILE;
793 clients[i].fd = -1;
794 clients[i].waiting_on_child = 0;
795 clients[i].buf = MAP_FAILED;
796 clients[i].dir = NULL;
797 clients[i].addr = addr;
799 connected_clients++;
800 return;
804 close(fd);
807 void
808 handle(struct pollfd *fds, struct client *client)
810 switch (client->state) {
811 case S_HANDSHAKE:
812 handle_handshake(fds, client);
813 break;
815 case S_OPEN:
816 handle_open_conn(fds, client);
817 break;
819 case S_INITIALIZING:
820 start_reply(fds, client, client->code, client->meta);
821 break;
823 case S_SENDING_FILE:
824 send_file(fds, client);
825 break;
827 case S_SENDING_DIR:
828 send_directory_listing(fds, client);
829 break;
831 case S_SENDING_CGI:
832 handle_cgi(fds, client);
833 break;
835 case S_CLOSING:
836 close_conn(fds, client);
837 break;
839 default:
840 /* unreachable */
841 abort();
845 void
846 loop(struct tls *ctx, int sock4, int sock6)
848 int i;
849 struct client clients[MAX_USERS];
850 struct pollfd fds[MAX_USERS];
852 connected_clients = 0;
854 for (i = 0; i < MAX_USERS; ++i) {
855 fds[i].fd = -1;
856 fds[i].events = POLLIN;
857 bzero(&clients[i], sizeof(struct client));
860 fds[0].fd = sock4;
861 fds[1].fd = sock6;
863 for (;;) {
864 if (poll(fds, MAX_USERS, INFTIM) == -1) {
865 if (errno == EINTR) {
866 fprintf(stderr, "connected clients: %d\n",
867 connected_clients);
868 continue;
870 fatal("poll: %s", strerror(errno));
873 for (i = 0; i < MAX_USERS; i++) {
874 if (fds[i].revents == 0)
875 continue;
877 if (fds[i].revents & (POLLERR|POLLNVAL))
878 fatal("bad fd %d: %s", fds[i].fd,
879 strerror(errno));
881 if (fds[i].revents & POLLHUP) {
882 /* fds[i] may be the fd of the stdin
883 * of a cgi script that has exited. */
884 if (!clients[i].waiting_on_child) {
885 close_conn(&fds[i], &clients[i]);
886 continue;
890 if (fds[i].fd == sock4)
891 do_accept(sock4, ctx, fds, clients);
892 else if (fds[i].fd == sock6)
893 do_accept(sock6, ctx, fds, clients);
894 else
895 handle(&fds[i], &clients[i]);