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 void
452 handle_cgi(struct pollfd *fds, struct client *c)
454 ssize_t r;
456 /* ensure c->fd is the child and fds->fd the client */
457 cgi_poll_on_client(fds, c);
459 while (1) {
460 if (c->len == 0) {
461 if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
462 goto end;
463 if (r == -1) {
464 if (errno == EAGAIN || errno == EWOULDBLOCK) {
465 cgi_poll_on_child(fds, c);
466 return;
468 goto end;
470 c->len = r;
471 c->off = 0;
473 /* XXX: if we haven't still read a whole
474 * reply line, we should go back to poll! */
475 if (c->code == -1) {
476 c->code = 0;
477 log_request(c, c->sbuf, sizeof(c->sbuf));
481 while (c->len > 0) {
482 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
483 case -1:
484 goto end;
486 case TLS_WANT_POLLOUT:
487 fds->events = POLLOUT;
488 return;
490 case TLS_WANT_POLLIN:
491 fds->events = POLLIN;
492 return;
494 default:
495 c->off += r;
496 c->len -= r;
497 break;
502 end:
503 close_conn(fds, c);
506 void
507 close_conn(struct pollfd *pfd, struct client *c)
509 c->state = S_CLOSING;
511 switch (tls_close(c->ctx)) {
512 case TLS_WANT_POLLIN:
513 pfd->events = POLLIN;
514 return;
515 case TLS_WANT_POLLOUT:
516 pfd->events = POLLOUT;
517 return;
520 connected_clients--;
522 tls_free(c->ctx);
523 c->ctx = NULL;
525 if (c->buf != MAP_FAILED)
526 munmap(c->buf, c->len);
528 if (c->fd != -1)
529 close(c->fd);
531 close(pfd->fd);
532 pfd->fd = -1;
535 void
536 goodbye(struct pollfd *fds, struct client *c, int code, const char *meta)
538 if (!start_reply(fds, c, code, meta))
539 return;
540 close_conn(fds, c);
543 void
544 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
546 int i, fd;
547 struct sockaddr_storage addr;
548 socklen_t len;
550 len = sizeof(addr);
551 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
552 if (errno == EWOULDBLOCK)
553 return;
554 fatal("accept: %s", strerror(errno));
557 mark_nonblock(fd);
559 for (i = 0; i < MAX_USERS; ++i) {
560 if (fds[i].fd == -1) {
561 bzero(&clients[i], sizeof(struct client));
562 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
563 break; /* goodbye fd! */
565 fds[i].fd = fd;
566 fds[i].events = POLLIN;
568 clients[i].state = S_HANDSHAKE;
569 clients[i].fd = -1;
570 clients[i].child = 0;
571 clients[i].waiting_on_child = 0;
572 clients[i].buf = MAP_FAILED;
573 clients[i].addr = addr;
575 connected_clients++;
576 return;
580 close(fd);
583 void
584 handle(struct pollfd *fds, struct client *client)
586 switch (client->state) {
587 case S_HANDSHAKE:
588 handle_handshake(fds, client);
589 break;
591 case S_OPEN:
592 handle_open_conn(fds, client);
593 break;
595 case S_INITIALIZING:
596 if (!start_reply(fds, client, client->code, client->meta))
597 return;
599 if (client->code != SUCCESS) {
600 /* we don't need a body */
601 close_conn(fds, client);
602 return;
605 client->state = S_SENDING;
607 /* fallthrough */
609 case S_SENDING:
610 if (client->child)
611 handle_cgi(fds, client);
612 else
613 send_file(fds, client);
614 break;
616 case S_CLOSING:
617 close_conn(fds, client);
618 break;
620 default:
621 /* unreachable */
622 abort();
626 void
627 loop(struct tls *ctx, int sock4, int sock6)
629 int i;
630 struct client clients[MAX_USERS];
631 struct pollfd fds[MAX_USERS];
633 connected_clients = 0;
635 for (i = 0; i < MAX_USERS; ++i) {
636 fds[i].fd = -1;
637 fds[i].events = POLLIN;
638 bzero(&clients[i], sizeof(struct client));
641 fds[0].fd = sock4;
642 fds[1].fd = sock6;
644 for (;;) {
645 if (poll(fds, MAX_USERS, INFTIM) == -1) {
646 if (errno == EINTR) {
647 fprintf(stderr, "connected clients: %d\n",
648 connected_clients);
649 continue;
651 fatal("poll: %s", strerror(errno));
654 for (i = 0; i < MAX_USERS; i++) {
655 if (fds[i].revents == 0)
656 continue;
658 if (fds[i].revents & (POLLERR|POLLNVAL))
659 fatal("bad fd %d: %s", fds[i].fd,
660 strerror(errno));
662 if (fds[i].revents & POLLHUP) {
663 /* fds[i] may be the fd of the stdin
664 * of a cgi script that has exited. */
665 if (!clients[i].waiting_on_child) {
666 close_conn(&fds[i], &clients[i]);
667 continue;
671 if (fds[i].fd == sock4)
672 do_accept(sock4, ctx, fds, clients);
673 else if (fds[i].fd == sock6)
674 do_accept(sock6, ctx, fds, clients);
675 else
676 handle(&fds[i], &clients[i]);