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 int
33 check_path(struct client *c, const char *path, int *fd)
34 {
35 struct stat sb;
36 const char *p;
38 assert(path != NULL);
40 if (*path == '\0')
41 p = ".";
42 else if (*path == '/')
43 /* in send_dir we add an initial / (to be
44 * redirect-friendly), but here we want to skip it */
45 p = path+1;
46 else
47 p = path;
49 if ((*fd = openat(c->host->dirfd, p, O_RDONLY | O_NOFOLLOW)) == -1) {
50 return FILE_MISSING;
51 }
53 if (fstat(*fd, &sb) == -1) {
54 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
55 return FILE_MISSING;
56 }
58 if (S_ISDIR(sb.st_mode))
59 return FILE_DIRECTORY;
61 if (sb.st_mode & S_IXUSR)
62 return FILE_EXECUTABLE;
64 return FILE_EXISTS;
65 }
67 int
68 open_file(struct pollfd *fds, struct client *c)
69 {
70 switch (check_path(c, c->iri.path, &c->fd)) {
71 case FILE_EXECUTABLE:
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);
75 /* fallthrough */
77 case FILE_EXISTS:
78 if ((c->len = filesize(c->fd)) == -1) {
79 LOGE(c, "failed to get file size for %s", c->iri.path);
80 close_conn(fds, c);
81 return 0;
82 }
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));
87 close_conn(fds, c);
88 return 0;
89 }
90 c->i = c->buf;
91 if (!start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path)))
92 return 0;
93 send_file(fds, c);
94 return 0;
96 case FILE_DIRECTORY:
97 close(c->fd);
98 c->fd = -1;
99 send_dir(fds, c);
100 return 0;
102 case FILE_MISSING:
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);
105 goodbye(fds, c, NOT_FOUND, "not found");
106 return 0;
108 default:
109 /* unreachable */
110 abort();
115 /*
116 * the inverse of this algorithm, i.e. starting from the start of the
117 * path + strlen(cgi), and checking if each component, should be
118 * faster. But it's tedious to write. This does the opposite: starts
119 * from the end and strip one component at a time, until either an
120 * executable is found or we emptied the path.
121 */
122 int
123 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
125 char *end;
126 end = strchr(path, '\0');
128 /* NB: assume CGI is enabled and path matches cgi */
130 while (end > path) {
131 /* go up one level. UNIX paths are simple and POSIX
132 * dirname, with its ambiguities on if the given path
133 * is changed or not, gives me headaches. */
134 while (*end != '/')
135 end--;
136 *end = '\0';
138 switch (check_path(c, path, &c->fd)) {
139 case FILE_EXECUTABLE:
140 return start_cgi(path, end+1, query, fds,c);
141 case FILE_MISSING:
142 break;
143 default:
144 goto err;
147 *end = '/';
148 end--;
151 err:
152 goodbye(fds, c, NOT_FOUND, "not found");
153 return 0;
156 void
157 mark_nonblock(int fd)
159 int flags;
161 if ((flags = fcntl(fd, F_GETFL)) == -1)
162 fatal("fcntl(F_GETFL): %s", strerror(errno));
163 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
164 fatal("fcntl(F_SETFL): %s", strerror(errno));
167 void
168 handle_handshake(struct pollfd *fds, struct client *c)
170 struct vhost *h;
171 const char *servname;
173 switch (tls_handshake(c->ctx)) {
174 case 0: /* success */
175 case -1: /* already handshaked */
176 break;
177 case TLS_WANT_POLLIN:
178 fds->events = POLLIN;
179 return;
180 case TLS_WANT_POLLOUT:
181 fds->events = POLLOUT;
182 return;
183 default:
184 /* unreachable */
185 abort();
188 servname = tls_conn_servername(c->ctx);
190 for (h = hosts; h->domain != NULL; ++h) {
191 if (!strcmp(h->domain, "*"))
192 break;
194 if (servname != NULL && !fnmatch(h->domain, servname, 0))
195 break;
198 if (h->domain != NULL) {
199 c->state = S_OPEN;
200 c->host = h;
201 handle_open_conn(fds, c);
202 return;
205 if (servname != NULL)
206 strncpy(c->req, servname, sizeof(c->req));
207 else
208 strncpy(c->req, "null", sizeof(c->req));
210 goodbye(fds, c, BAD_REQUEST, "Wrong host or missing SNI");
213 void
214 handle_open_conn(struct pollfd *fds, struct client *c)
216 const char *parse_err = "invalid request";
218 bzero(c->req, sizeof(c->req));
219 bzero(&c->iri, sizeof(c->iri));
221 switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
222 case -1:
223 LOGE(c, "tls_read: %s", tls_error(c->ctx));
224 close_conn(fds, c);
225 return;
227 case TLS_WANT_POLLIN:
228 fds->events = POLLIN;
229 return;
231 case TLS_WANT_POLLOUT:
232 fds->events = POLLOUT;
233 return;
236 if (!trim_req_iri(c->req) || !parse_iri(c->req, &c->iri, &parse_err)) {
237 goodbye(fds, c, BAD_REQUEST, parse_err);
238 return;
241 /* XXX: we should check that the SNI matches the requested host */
242 if (strcmp(c->iri.schema, "gemini") || c->iri.port_no != conf.port) {
243 goodbye(fds, c, PROXY_REFUSED, "won't proxy request");
244 return;
247 open_file(fds, c);
250 int
251 start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
253 char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
254 size_t len;
256 c->code = code;
257 c->meta = meta;
258 c->state = S_INITIALIZING;
260 snprintf(buf, sizeof(buf), "%d ", code);
261 strlcat(buf, meta, sizeof(buf));
262 if (!strcmp(meta, "text/gemini") && c->host->lang != NULL) {
263 strlcat(buf, "; lang=", sizeof(buf));
264 strlcat(buf, c->host->lang, sizeof(buf));
267 len = strlcat(buf, "\r\n", sizeof(buf));
268 assert(len < sizeof(buf));
270 switch (tls_write(c->ctx, buf, len)) {
271 case TLS_WANT_POLLIN:
272 pfd->events = POLLIN;
273 return 0;
274 case TLS_WANT_POLLOUT:
275 pfd->events = POLLOUT;
276 return 0;
277 default:
278 log_request(c, buf, sizeof(buf));
279 return 1;
283 int
284 start_cgi(const char *spath, const char *relpath, const char *query,
285 struct pollfd *fds, struct client *c)
287 char addr[NI_MAXHOST];
288 const char *ruser, *cissuer, *chash;
289 int e;
291 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
292 addr, sizeof(addr),
293 NULL, 0,
294 NI_NUMERICHOST);
295 if (e != 0)
296 goto err;
298 if (tls_peer_cert_provided(c->ctx)) {
299 ruser = tls_peer_cert_subject(c->ctx);
300 cissuer = tls_peer_cert_issuer(c->ctx);
301 chash = tls_peer_cert_hash(c->ctx);
302 } else {
303 ruser = NULL;
304 cissuer = NULL;
305 chash = NULL;
308 if (!send_string(exfd, spath)
309 || !send_string(exfd, relpath)
310 || !send_string(exfd, query)
311 || !send_string(exfd, addr)
312 || !send_string(exfd, ruser)
313 || !send_string(exfd, cissuer)
314 || !send_string(exfd, chash)
315 || !send_vhost(exfd, c->host))
316 goto err;
318 close(c->fd);
319 if ((c->fd = recv_fd(exfd)) == -1) {
320 goodbye(fds, c, TEMP_FAILURE, "internal server error");
321 return 0;
323 c->child = 1;
324 c->state = S_SENDING;
325 cgi_poll_on_child(fds, c);
326 c->code = -1;
327 /* handle_cgi(fds, c); */
328 return 0;
330 err:
331 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
332 fatal("cannot talk to the executor process");
335 void
336 send_file(struct pollfd *fds, struct client *c)
338 ssize_t ret, len;
340 /* ensure the correct state */
341 c->state = S_SENDING;
343 len = (c->buf + c->len) - c->i;
345 while (len > 0) {
346 switch (ret = tls_write(c->ctx, c->i, len)) {
347 case -1:
348 LOGE(c, "tls_write: %s", tls_error(c->ctx));
349 close_conn(fds, c);
350 return;
352 case TLS_WANT_POLLIN:
353 fds->events = POLLIN;
354 return;
356 case TLS_WANT_POLLOUT:
357 fds->events = POLLOUT;
358 return;
360 default:
361 c->i += ret;
362 len -= ret;
363 break;
367 close_conn(fds, c);
370 void
371 send_dir(struct pollfd *fds, struct client *c)
373 size_t len;
375 /* guard against a re-entrant call:
377 * open_file -> send_dir -> open_file -> send_dir
379 * this can happen only if:
381 * - user requested a dir, say foo/
382 * - we try to serve foo/index.gmi
383 * - foo/index.gmi is a directory.
385 * It's an unlikely case, but can happen. We then redirect
386 * to foo/index.gmi
387 */
388 if (c->iri.path == c->sbuf) {
389 goodbye(fds, c, TEMP_REDIRECT, c->sbuf);
390 return;
393 strlcpy(c->sbuf, "/", sizeof(c->sbuf));
395 len = strlen(c->iri.path);
396 if (len > 0 && c->iri.path[len-1] != '/') {
397 /* redirect to url with the trailing / */
398 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
399 strlcat(c->sbuf, "/", sizeof(c->sbuf));
400 goodbye(fds, c, TEMP_REDIRECT, c->sbuf);
401 return;
404 strlcat(c->sbuf, c->iri.path, sizeof(c->sbuf));
406 if (!ends_with(c->sbuf, "/"))
407 strlcat(c->sbuf, "/", sizeof(c->sbuf));
409 len = strlcat(c->sbuf, "index.gmi", sizeof(c->sbuf));
411 if (len >= sizeof(c->sbuf)) {
412 goodbye(fds, c, TEMP_FAILURE, "internal server error");
413 return;
416 close(c->fd);
417 c->iri.path = c->sbuf;
418 open_file(fds, c);
421 void
422 cgi_poll_on_child(struct pollfd *fds, struct client *c)
424 int fd;
426 if (c->waiting_on_child)
427 return;
428 c->waiting_on_child = 1;
430 fds->events = POLLIN;
432 fd = fds->fd;
433 fds->fd = c->fd;
434 c->fd = fd;
437 void
438 cgi_poll_on_client(struct pollfd *fds, struct client *c)
440 int fd;
442 if (!c->waiting_on_child)
443 return;
444 c->waiting_on_child = 0;
446 fd = fds->fd;
447 fds->fd = c->fd;
448 c->fd = fd;
451 /* handle the read from the child process. Return like read(2) */
452 static ssize_t
453 read_from_cgi(struct client *c)
455 void *buf;
456 size_t len;
457 ssize_t r;
459 /* if we haven't read a whole response line, we want to
460 * continue reading. */
462 if (c->code == -1) {
463 buf = c->sbuf + c->len;
464 len = sizeof(c->sbuf) - c->len;
465 } else {
466 buf = c->sbuf;
467 len = sizeof(c->sbuf);
470 r = read(c->fd, buf, len);
471 if (r == 0 || r == -1)
472 return r;
474 c->len += r;
475 c->off = 0;
477 if (c->code != -1)
478 return r;
480 if (strchr(c->sbuf, '\n') || c->len == sizeof(c->sbuf)) {
481 c->code = 0;
482 log_request(c, c->sbuf, c->len);
485 return r;
488 void
489 handle_cgi(struct pollfd *fds, struct client *c)
491 ssize_t r;
493 /* ensure c->fd is the child and fds->fd the client */
494 cgi_poll_on_client(fds, c);
496 while (1) {
497 if (c->code == -1 || c->len == 0) {
498 switch (r = read_from_cgi(c)) {
499 case 0:
500 goto end;
502 case -1:
503 if (errno == EAGAIN || errno == EWOULDBLOCK) {
504 cgi_poll_on_child(fds, c);
505 return;
507 goto end;
511 if (c->code == -1) {
512 cgi_poll_on_child(fds, c);
513 return;
516 while (c->len > 0) {
517 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
518 case -1:
519 goto end;
521 case TLS_WANT_POLLOUT:
522 fds->events = POLLOUT;
523 return;
525 case TLS_WANT_POLLIN:
526 fds->events = POLLIN;
527 return;
529 default:
530 c->off += r;
531 c->len -= r;
532 break;
537 end:
538 close_conn(fds, c);
541 void
542 close_conn(struct pollfd *pfd, struct client *c)
544 c->state = S_CLOSING;
546 switch (tls_close(c->ctx)) {
547 case TLS_WANT_POLLIN:
548 pfd->events = POLLIN;
549 return;
550 case TLS_WANT_POLLOUT:
551 pfd->events = POLLOUT;
552 return;
555 connected_clients--;
557 tls_free(c->ctx);
558 c->ctx = NULL;
560 if (c->buf != MAP_FAILED)
561 munmap(c->buf, c->len);
563 if (c->fd != -1)
564 close(c->fd);
566 close(pfd->fd);
567 pfd->fd = -1;
570 void
571 goodbye(struct pollfd *fds, struct client *c, int code, const char *meta)
573 if (!start_reply(fds, c, code, meta))
574 return;
575 close_conn(fds, c);
578 void
579 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
581 int i, fd;
582 struct sockaddr_storage addr;
583 socklen_t len;
585 len = sizeof(addr);
586 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
587 if (errno == EWOULDBLOCK)
588 return;
589 fatal("accept: %s", strerror(errno));
592 mark_nonblock(fd);
594 for (i = 0; i < MAX_USERS; ++i) {
595 if (fds[i].fd == -1) {
596 bzero(&clients[i], sizeof(struct client));
597 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
598 break; /* goodbye fd! */
600 fds[i].fd = fd;
601 fds[i].events = POLLIN;
603 clients[i].state = S_HANDSHAKE;
604 clients[i].fd = -1;
605 clients[i].child = 0;
606 clients[i].waiting_on_child = 0;
607 clients[i].buf = MAP_FAILED;
608 clients[i].addr = addr;
610 connected_clients++;
611 return;
615 close(fd);
618 void
619 handle(struct pollfd *fds, struct client *client)
621 switch (client->state) {
622 case S_HANDSHAKE:
623 handle_handshake(fds, client);
624 break;
626 case S_OPEN:
627 handle_open_conn(fds, client);
628 break;
630 case S_INITIALIZING:
631 if (!start_reply(fds, client, client->code, client->meta))
632 return;
634 if (client->code != SUCCESS) {
635 /* we don't need a body */
636 close_conn(fds, client);
637 return;
640 client->state = S_SENDING;
642 /* fallthrough */
644 case S_SENDING:
645 if (client->child)
646 handle_cgi(fds, client);
647 else
648 send_file(fds, client);
649 break;
651 case S_CLOSING:
652 close_conn(fds, client);
653 break;
655 default:
656 /* unreachable */
657 abort();
661 void
662 loop(struct tls *ctx, int sock4, int sock6)
664 int i;
665 struct client clients[MAX_USERS];
666 struct pollfd fds[MAX_USERS];
668 connected_clients = 0;
670 for (i = 0; i < MAX_USERS; ++i) {
671 fds[i].fd = -1;
672 fds[i].events = POLLIN;
673 bzero(&clients[i], sizeof(struct client));
676 fds[0].fd = sock4;
677 fds[1].fd = sock6;
679 for (;;) {
680 if (poll(fds, MAX_USERS, INFTIM) == -1) {
681 if (errno == EINTR) {
682 fprintf(stderr, "connected clients: %d\n",
683 connected_clients);
684 continue;
686 fatal("poll: %s", strerror(errno));
689 for (i = 0; i < MAX_USERS; i++) {
690 if (fds[i].revents == 0)
691 continue;
693 if (fds[i].revents & (POLLERR|POLLNVAL))
694 fatal("bad fd %d: %s", fds[i].fd,
695 strerror(errno));
697 if (fds[i].revents & POLLHUP) {
698 /* fds[i] may be the fd of the stdin
699 * of a cgi script that has exited. */
700 if (!clients[i].waiting_on_child) {
701 close_conn(&fds[i], &clients[i]);
702 continue;
706 if (fds[i].fd == sock4)
707 do_accept(sock4, ctx, fds, clients);
708 else if (fds[i].fd == sock6)
709 do_accept(sock6, ctx, fds, clients);
710 else
711 handle(&fds[i], &clients[i]);