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 check_path(struct client *c, const char *path, int *fd)
42 else if (*path == '/')
43 /* in send_dir we add an initial / (to be
44 * redirect-friendly), but here we want to skip it */
49 if ((*fd = openat(c->host->dirfd, p, O_RDONLY | O_NOFOLLOW)) == -1) {
53 if (fstat(*fd, &sb) == -1) {
54 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
58 if (S_ISDIR(sb.st_mode))
59 return FILE_DIRECTORY;
61 if (sb.st_mode & S_IXUSR)
62 return FILE_EXECUTABLE;
68 open_file(struct pollfd *fds, struct client *c)
70 switch (check_path(c, c->iri.path, &c->fd)) {
72 if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi))
73 return start_cgi(c->iri.path, "", c->iri.query, fds, c);
78 if ((c->len = filesize(c->fd)) == -1) {
79 LOGE(c, "failed to get file size for %s", c->iri.path);
84 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
85 c->fd, 0)) == MAP_FAILED) {
86 LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
91 if (!start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path)))
103 if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi))
104 return check_for_cgi(c->iri.path, c->iri.query, fds, c);
106 if (!start_reply(fds, c, NOT_FOUND, "not found"))
119 * the inverse of this algorithm, i.e. starting from the start of the
120 * path + strlen(cgi), and checking if each component, should be
121 * faster. But it's tedious to write. This does the opposite: starts
122 * from the end and strip one component at a time, until either an
123 * executable is found or we emptied the path.
126 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
129 end = strchr(path, '\0');
131 /* NB: assume CGI is enabled and path matches cgi */
134 /* go up one level. UNIX paths are simple and POSIX
135 * dirname, with its ambiguities on if the given path
136 * is changed or not, gives me headaches. */
141 switch (check_path(c, path, &c->fd)) {
142 case FILE_EXECUTABLE:
143 return start_cgi(path, end+1, query, fds,c);
155 if (!start_reply(fds, c, NOT_FOUND, "not found"))
162 mark_nonblock(int fd)
166 if ((flags = fcntl(fd, F_GETFL)) == -1)
167 fatal("fcntl(F_GETFL): %s", strerror(errno));
168 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
169 fatal("fcntl(F_SETFL): %s", strerror(errno));
173 handle_handshake(struct pollfd *fds, struct client *c)
176 const char *servname;
178 switch (tls_handshake(c->ctx)) {
179 case 0: /* success */
180 case -1: /* already handshaked */
182 case TLS_WANT_POLLIN:
183 fds->events = POLLIN;
185 case TLS_WANT_POLLOUT:
186 fds->events = POLLOUT;
193 servname = tls_conn_servername(c->ctx);
195 for (h = hosts; h->domain != NULL; ++h) {
196 if (!strcmp(h->domain, "*"))
199 if (servname != NULL && !fnmatch(h->domain, servname, 0))
203 if (h->domain != NULL) {
206 handle_open_conn(fds, c);
210 if (servname != NULL)
211 strncpy(c->req, servname, sizeof(c->req));
213 strncpy(c->req, "null", sizeof(c->req));
215 if (!start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI"))
221 handle_open_conn(struct pollfd *fds, struct client *c)
223 const char *parse_err = "invalid request";
225 bzero(c->req, sizeof(c->req));
226 bzero(&c->iri, sizeof(c->iri));
228 switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
230 LOGE(c, "tls_read: %s", tls_error(c->ctx));
234 case TLS_WANT_POLLIN:
235 fds->events = POLLIN;
238 case TLS_WANT_POLLOUT:
239 fds->events = POLLOUT;
243 if (!trim_req_iri(c->req) || !parse_iri(c->req, &c->iri, &parse_err)) {
244 if (!start_reply(fds, c, BAD_REQUEST, parse_err))
250 /* XXX: we should check that the SNI matches the requested host */
251 if (strcmp(c->iri.schema, "gemini") || c->iri.port_no != conf.port) {
252 if (!start_reply(fds, c, PROXY_REFUSED, "won't proxy request"))
262 start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
264 char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
269 c->state = S_INITIALIZING;
271 snprintf(buf, sizeof(buf), "%d ", code);
272 strlcat(buf, meta, sizeof(buf));
273 if (!strcmp(meta, "text/gemini") && c->host->lang != NULL) {
274 strlcat(buf, "; lang=", sizeof(buf));
275 strlcat(buf, c->host->lang, sizeof(buf));
278 len = strlcat(buf, "\r\n", sizeof(buf));
279 assert(len < sizeof(buf));
281 switch (tls_write(c->ctx, buf, len)) {
282 case TLS_WANT_POLLIN:
283 pfd->events = POLLIN;
285 case TLS_WANT_POLLOUT:
286 pfd->events = POLLOUT;
289 log_request(c, buf, sizeof(buf));
295 start_cgi(const char *spath, const char *relpath, const char *query,
296 struct pollfd *fds, struct client *c)
298 char addr[NI_MAXHOST];
299 const char *ruser, *cissuer, *chash;
302 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
309 if (tls_peer_cert_provided(c->ctx)) {
310 ruser = tls_peer_cert_subject(c->ctx);
311 cissuer = tls_peer_cert_issuer(c->ctx);
312 chash = tls_peer_cert_hash(c->ctx);
319 if (!send_string(exfd, spath)
320 || !send_string(exfd, relpath)
321 || !send_string(exfd, query)
322 || !send_string(exfd, addr)
323 || !send_string(exfd, ruser)
324 || !send_string(exfd, cissuer)
325 || !send_string(exfd, chash)
326 || !send_vhost(exfd, c->host))
330 if ((c->fd = recv_fd(exfd)) == -1) {
331 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
337 c->state = S_SENDING;
338 cgi_poll_on_child(fds, c);
340 /* handle_cgi(fds, c); */
344 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
345 fatal("cannot talk to the executor process");
349 send_file(struct pollfd *fds, struct client *c)
353 /* ensure the correct state */
354 c->state = S_SENDING;
356 len = (c->buf + c->len) - c->i;
359 switch (ret = tls_write(c->ctx, c->i, len)) {
361 LOGE(c, "tls_write: %s", tls_error(c->ctx));
365 case TLS_WANT_POLLIN:
366 fds->events = POLLIN;
369 case TLS_WANT_POLLOUT:
370 fds->events = POLLOUT;
384 send_dir(struct pollfd *fds, struct client *c)
388 /* guard against a re-entrant call:
390 * open_file -> send_dir -> open_file -> send_dir
392 * this can happen only if:
394 * - user requested a dir, say foo/
395 * - we try to serve foo/index.gmi
396 * - foo/index.gmi is a directory.
398 * It's an unlikely case, but can happen. We then redirect
401 if (c->iri.path == c->sbuf) {
402 if (!start_reply(fds, c, TEMP_REDIRECT, c->sbuf))
408 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
410 len = strlen(c->iri.path);
411 if (len > 0 && c->iri.path[len-1] != '/') {
412 /* redirect to url with the trailing / */
413 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
414 strlcat(c->sbuf, "/", sizeof(c->sbuf));
415 if (!start_reply(fds, c, TEMP_REDIRECT, c->sbuf))
421 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
423 if (!ends_with(c->sbuf, "/"))
424 strlcat(c->sbuf, "/", sizeof(c->sbuf));
426 len = strlcat(c->sbuf, "index.gmi", sizeof(c->sbuf));
428 if (len >= sizeof(c->sbuf)) {
429 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
436 c->iri.path = c->sbuf;
441 cgi_poll_on_child(struct pollfd *fds, struct client *c)
445 if (c->waiting_on_child)
447 c->waiting_on_child = 1;
449 fds->events = POLLIN;
457 cgi_poll_on_client(struct pollfd *fds, struct client *c)
461 if (!c->waiting_on_child)
463 c->waiting_on_child = 0;
471 handle_cgi(struct pollfd *fds, struct client *c)
475 /* ensure c->fd is the child and fds->fd the client */
476 cgi_poll_on_client(fds, c);
480 if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
483 if (errno == EAGAIN || errno == EWOULDBLOCK) {
484 cgi_poll_on_child(fds, c);
492 /* XXX: if we haven't still read a whole
493 * reply line, we should go back to poll! */
496 log_request(c, c->sbuf, sizeof(c->sbuf));
501 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
505 case TLS_WANT_POLLOUT:
506 fds->events = POLLOUT;
509 case TLS_WANT_POLLIN:
510 fds->events = POLLIN;
526 goodbye(struct pollfd *pfd, struct client *c)
528 c->state = S_CLOSING;
530 switch (tls_close(c->ctx)) {
531 case TLS_WANT_POLLIN:
532 pfd->events = POLLIN;
534 case TLS_WANT_POLLOUT:
535 pfd->events = POLLOUT;
544 if (c->buf != MAP_FAILED)
545 munmap(c->buf, c->len);
555 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
558 struct sockaddr_storage addr;
562 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
563 if (errno == EWOULDBLOCK)
565 fatal("accept: %s", strerror(errno));
570 for (i = 0; i < MAX_USERS; ++i) {
571 if (fds[i].fd == -1) {
572 bzero(&clients[i], sizeof(struct client));
573 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
574 break; /* goodbye fd! */
577 fds[i].events = POLLIN;
579 clients[i].state = S_HANDSHAKE;
581 clients[i].child = 0;
582 clients[i].waiting_on_child = 0;
583 clients[i].buf = MAP_FAILED;
584 clients[i].addr = addr;
595 handle(struct pollfd *fds, struct client *client)
597 switch (client->state) {
599 handle_handshake(fds, client);
603 handle_open_conn(fds, client);
607 if (!start_reply(fds, client, client->code, client->meta))
610 if (client->code != SUCCESS) {
611 /* we don't need a body */
612 goodbye(fds, client);
616 client->state = S_SENDING;
622 handle_cgi(fds, client);
624 send_file(fds, client);
628 goodbye(fds, client);
638 loop(struct tls *ctx, int sock4, int sock6)
641 struct client clients[MAX_USERS];
642 struct pollfd fds[MAX_USERS];
644 connected_clients = 0;
646 for (i = 0; i < MAX_USERS; ++i) {
648 fds[i].events = POLLIN;
649 bzero(&clients[i], sizeof(struct client));
656 if (poll(fds, MAX_USERS, INFTIM) == -1) {
657 if (errno == EINTR) {
658 fprintf(stderr, "connected clients: %d\n",
662 fatal("poll: %s", strerror(errno));
665 for (i = 0; i < MAX_USERS; i++) {
666 if (fds[i].revents == 0)
669 if (fds[i].revents & (POLLERR|POLLNVAL))
670 fatal("bad fd %d: %s", fds[i].fd,
673 if (fds[i].revents & POLLHUP) {
674 /* fds[i] may be the fd of the stdin
675 * of a cgi script that has exited. */
676 if (!clients[i].waiting_on_child) {
677 goodbye(&fds[i], &clients[i]);
682 if (fds[i].fd == sock4)
683 do_accept(sock4, ctx, fds, clients);
684 else if (fds[i].fd == sock6)
685 do_accept(sock6, ctx, fds, clients);
687 handle(&fds[i], &clients[i]);