2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
30 int connected_clients;
33 vhost_lang(struct vhost *v, const char *path)
37 if (v == NULL || path == NULL)
40 for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
41 if (!fnmatch(loc->match, path, 0)) {
42 if (loc->lang != NULL)
47 return v->locations[0].lang;
51 vhost_default_mime(struct vhost *v, const char *path)
54 const char *default_mime = "application/octet-stream";
56 if (v == NULL || path == NULL)
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;
66 if (v->locations[0].default_mime != NULL)
67 return v->locations[0].default_mime;
72 vhost_index(struct vhost *v, const char *path)
75 const char *index = "index.gmi";
77 if (v == NULL || path == NULL)
80 for (loc = &v->locations[1]; loc->match != NULL; ++loc) {
81 if (!fnmatch(loc->match, path, 0)) {
82 if (loc->index != NULL)
87 if (v->locations[0].index != NULL)
88 return v->locations[0].index;
93 vhost_auto_index(struct vhost *v, const char *path)
97 if (v == NULL || path == NULL)
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;
111 check_path(struct client *c, const char *path, int *fd)
117 assert(path != NULL);
121 else if (*path == '/')
122 /* in send_dir we add an initial / (to be
123 * redirect-friendly), but here we want to skip it */
128 flags = O_RDONLY | O_NOFOLLOW;
130 if (*fd == -1 && (*fd = openat(c->host->dirfd, p, flags)) == -1)
133 if (fstat(*fd, &sb) == -1) {
134 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
138 if (S_ISDIR(sb.st_mode))
139 return FILE_DIRECTORY;
141 if (sb.st_mode & S_IXUSR)
142 return FILE_EXECUTABLE;
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);
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);
172 start_reply(fds, c, NOT_FOUND, "not found");
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");
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");
197 c->next = S_SENDING_FILE;
198 start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path));
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.
209 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
212 end = strchr(path, '\0');
214 /* NB: assume CGI is enabled and path matches cgi */
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. */
224 switch (check_path(c, path, &c->fd)) {
225 case FILE_EXECUTABLE:
226 start_cgi(path, end+1, query, fds,c);
239 start_reply(fds, c, NOT_FOUND, "not found");
244 mark_nonblock(int fd)
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));
255 handle_handshake(struct pollfd *fds, struct client *c)
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 */
265 case TLS_WANT_POLLIN:
266 fds->events = POLLIN;
268 case TLS_WANT_POLLOUT:
269 fds->events = POLLOUT;
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);
282 for (h = hosts; h->domain != NULL; ++h) {
283 if (!fnmatch(h->domain, c->domain, 0))
287 /* LOGD(c, "handshake: SNI: \"%s\"; decoded: \"%s\"; matched: \"%s\"", */
288 /* servname != NULL ? servname : "(null)", */
290 /* h->domain != NULL ? h->domain : "(null)"); */
292 if (h->domain != NULL) {
295 handle_open_conn(fds, c);
300 if (servname != NULL)
301 strncpy(c->req, servname, sizeof(c->req));
303 strncpy(c->req, "null", sizeof(c->req));
305 start_reply(fds, c, BAD_REQUEST, "Wrong/malformed host or missing SNI");
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)) {
319 LOGE(c, "tls_read: %s", tls_error(c->ctx));
323 case TLS_WANT_POLLIN:
324 fds->events = POLLIN;
327 case TLS_WANT_POLLOUT:
328 fds->events = POLLOUT;
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");
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");
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 */
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)) {
377 case TLS_WANT_POLLIN:
378 pfd->events = POLLIN;
380 case TLS_WANT_POLLOUT:
381 pfd->events = POLLOUT;
385 log_request(c, buf, sizeof(buf));
387 /* we don't need a body */
388 if (c->code != SUCCESS) {
393 /* advance the state machine */
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;
406 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
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);
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))
434 if ((c->fd = recv_fd(exfd)) == -1) {
435 start_reply(fds, c, TEMP_FAILURE, "internal server error");
438 c->state = S_SENDING_CGI;
439 cgi_poll_on_child(fds, c);
441 /* handle_cgi(fds, c); */
445 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
446 fatal("cannot talk to the executor process");
450 send_file(struct pollfd *fds, struct client *c)
454 /* ensure the correct state */
455 c->state = S_SENDING_FILE;
457 len = (c->buf + c->len) - c->i;
460 switch (ret = tls_write(c->ctx, c->i, len)) {
462 LOGE(c, "tls_write: %s", tls_error(c->ctx));
466 case TLS_WANT_POLLIN:
467 fds->events = POLLIN;
470 case TLS_WANT_POLLOUT:
471 fds->events = POLLOUT;
485 open_dir(struct pollfd *fds, struct client *c)
491 len = strlen(c->iri.path);
492 if (len > 0 && !ends_with(c->iri.path, "/")) {
493 redirect_canonical_dir(fds, c);
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),
504 if (len >= sizeof(c->sbuf)) {
505 start_reply(fds, c, TEMP_FAILURE, "internal server error");
509 c->iri.path = c->sbuf;
511 /* close later unless we have to generate the dir listing */
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);
529 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
535 if (!vhost_auto_index(c->host, c->iri.path)) {
536 start_reply(fds, c, NOT_FOUND, "not found");
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");
551 start_reply(fds, c, SUCCESS, "text/gemini");
563 redirect_canonical_dir(struct pollfd *fds, struct client *c)
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");
576 start_reply(fds, c, TEMP_REDIRECT, c->sbuf);
580 read_next_dir_entry(struct client *c)
586 if ((d = readdir(c->dir)) == NULL) {
588 LOGE(c, "readdir: %s", strerror(errno));
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);
603 send_directory_listing(struct pollfd *fds, struct client *c)
609 if (!read_next_dir_entry(c))
614 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
618 case TLS_WANT_POLLOUT:
619 fds->events = POLLOUT;
622 case TLS_WANT_POLLIN:
623 fds->events = POLLIN;
639 cgi_poll_on_child(struct pollfd *fds, struct client *c)
643 if (c->waiting_on_child)
645 c->waiting_on_child = 1;
647 fds->events = POLLIN;
655 cgi_poll_on_client(struct pollfd *fds, struct client *c)
659 if (!c->waiting_on_child)
661 c->waiting_on_child = 0;
668 /* handle the read from the child process. Return like read(2) */
670 read_from_cgi(struct client *c)
676 /* if we haven't read a whole response line, we want to
677 * continue reading. */
680 buf = c->sbuf + c->len;
681 len = sizeof(c->sbuf) - c->len;
684 len = sizeof(c->sbuf);
687 r = read(c->fd, buf, len);
688 if (r == 0 || r == -1)
697 if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
699 log_request(c, c->sbuf, c->len);
706 handle_cgi(struct pollfd *fds, struct client *c)
710 /* ensure c->fd is the child and fds->fd the client */
711 cgi_poll_on_client(fds, c);
714 if (c->code == -1 || c->len == 0) {
715 switch (r = read_from_cgi(c)) {
720 if (errno == EAGAIN || errno == EWOULDBLOCK) {
721 cgi_poll_on_child(fds, c);
729 cgi_poll_on_child(fds, c);
734 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
738 case TLS_WANT_POLLOUT:
739 fds->events = POLLOUT;
742 case TLS_WANT_POLLIN:
743 fds->events = POLLIN;
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;
767 case TLS_WANT_POLLOUT:
768 pfd->events = POLLOUT;
777 if (c->buf != MAP_FAILED)
778 munmap(c->buf, c->len);
791 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
794 struct sockaddr_storage addr;
798 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
799 if (errno == EWOULDBLOCK)
801 fatal("accept: %s", strerror(errno));
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! */
813 fds[i].events = POLLIN;
815 clients[i].state = S_HANDSHAKE;
816 clients[i].next = S_SENDING_FILE;
818 clients[i].waiting_on_child = 0;
819 clients[i].buf = MAP_FAILED;
820 clients[i].dir = NULL;
821 clients[i].addr = addr;
832 handle(struct pollfd *fds, struct client *client)
834 switch (client->state) {
836 handle_handshake(fds, client);
840 handle_open_conn(fds, client);
844 start_reply(fds, client, client->code, client->meta);
848 send_file(fds, client);
852 send_directory_listing(fds, client);
856 handle_cgi(fds, client);
860 close_conn(fds, client);
870 loop(struct tls *ctx, int sock4, int sock6)
873 struct client clients[MAX_USERS];
874 struct pollfd fds[MAX_USERS];
876 connected_clients = 0;
878 for (i = 0; i < MAX_USERS; ++i) {
880 fds[i].events = POLLIN;
881 bzero(&clients[i], sizeof(struct client));
888 if (poll(fds, MAX_USERS, INFTIM) == -1) {
889 if (errno == EINTR) {
890 fprintf(stderr, "connected clients: %d\n",
894 fatal("poll: %s", strerror(errno));
897 for (i = 0; i < MAX_USERS; i++) {
898 if (fds[i].revents == 0)
901 if (fds[i].revents & (POLLERR|POLLNVAL))
902 fatal("bad fd %d: %s", fds[i].fd,
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]);
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);
919 handle(&fds[i], &clients[i]);