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 goodbye(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 goodbye(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);
106 if (!start_reply(fds, c, NOT_FOUND, "not found"))
107 return 0;
108 goodbye(fds, c);
109 return 0;
111 default:
112 /* unreachable */
113 abort();
118 /*
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.
124 */
125 int
126 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
128 char *end;
129 end = strchr(path, '\0');
131 /* NB: assume CGI is enabled and path matches cgi */
133 while (end > path) {
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. */
137 while (*end != '/')
138 end--;
139 *end = '\0';
141 switch (check_path(c, path, &c->fd)) {
142 case FILE_EXECUTABLE:
143 return start_cgi(path, end+1, query, fds,c);
144 case FILE_MISSING:
145 break;
146 default:
147 goto err;
150 *end = '/';
151 end--;
154 err:
155 if (!start_reply(fds, c, NOT_FOUND, "not found"))
156 return 0;
157 goodbye(fds, c);
158 return 0;
161 void
162 mark_nonblock(int fd)
164 int flags;
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));
172 void
173 handle_handshake(struct pollfd *fds, struct client *c)
175 struct vhost *h;
176 const char *servname;
178 switch (tls_handshake(c->ctx)) {
179 case 0: /* success */
180 case -1: /* already handshaked */
181 break;
182 case TLS_WANT_POLLIN:
183 fds->events = POLLIN;
184 return;
185 case TLS_WANT_POLLOUT:
186 fds->events = POLLOUT;
187 return;
188 default:
189 /* unreachable */
190 abort();
193 servname = tls_conn_servername(c->ctx);
195 for (h = hosts; h->domain != NULL; ++h) {
196 if (!strcmp(h->domain, "*"))
197 break;
199 if (servname != NULL && !fnmatch(h->domain, servname, 0))
200 break;
203 if (h->domain != NULL) {
204 c->state = S_OPEN;
205 c->host = h;
206 handle_open_conn(fds, c);
207 return;
210 if (servname != NULL)
211 strncpy(c->req, servname, sizeof(c->req));
212 else
213 strncpy(c->req, "null", sizeof(c->req));
215 if (!start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI"))
216 return;
217 goodbye(fds, c);
220 void
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)) {
229 case -1:
230 LOGE(c, "tls_read: %s", tls_error(c->ctx));
231 goodbye(fds, c);
232 return;
234 case TLS_WANT_POLLIN:
235 fds->events = POLLIN;
236 return;
238 case TLS_WANT_POLLOUT:
239 fds->events = POLLOUT;
240 return;
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))
245 return;
246 goodbye(fds, c);
247 return;
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"))
253 return;
254 goodbye(fds, c);
255 return;
258 open_file(fds, c);
261 int
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 */
265 size_t len;
267 c->code = code;
268 c->meta = meta;
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;
284 return 0;
285 case TLS_WANT_POLLOUT:
286 pfd->events = POLLOUT;
287 return 0;
288 default:
289 log_request(c, buf, sizeof(buf));
290 return 1;
294 int
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;
300 int e;
302 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
303 addr, sizeof(addr),
304 NULL, 0,
305 NI_NUMERICHOST);
306 if (e != 0)
307 goto err;
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);
313 } else {
314 ruser = NULL;
315 cissuer = NULL;
316 chash = NULL;
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))
327 goto err;
329 close(c->fd);
330 if ((c->fd = recv_fd(exfd)) == -1) {
331 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
332 return 0;
333 goodbye(fds, c);
334 return 0;
336 c->child = 1;
337 c->state = S_SENDING;
338 cgi_poll_on_child(fds, c);
339 c->code = -1;
340 /* handle_cgi(fds, c); */
341 return 0;
343 err:
344 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
345 fatal("cannot talk to the executor process");
348 void
349 send_file(struct pollfd *fds, struct client *c)
351 ssize_t ret, len;
353 /* ensure the correct state */
354 c->state = S_SENDING;
356 len = (c->buf + c->len) - c->i;
358 while (len > 0) {
359 switch (ret = tls_write(c->ctx, c->i, len)) {
360 case -1:
361 LOGE(c, "tls_write: %s", tls_error(c->ctx));
362 goodbye(fds, c);
363 return;
365 case TLS_WANT_POLLIN:
366 fds->events = POLLIN;
367 return;
369 case TLS_WANT_POLLOUT:
370 fds->events = POLLOUT;
371 return;
373 default:
374 c->i += ret;
375 len -= ret;
376 break;
380 goodbye(fds, c);
383 void
384 send_dir(struct pollfd *fds, struct client *c)
386 size_t len;
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
399 * to foo/index.gmi
400 */
401 if (c->iri.path == c->sbuf) {
402 if (!start_reply(fds, c, TEMP_REDIRECT, c->sbuf))
403 return;
404 goodbye(fds, c);
405 return;
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))
416 return;
417 goodbye(fds, c);
418 return;
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"))
430 return;
431 goodbye(fds, c);
432 return;
435 close(c->fd);
436 c->iri.path = c->sbuf;
437 open_file(fds, c);
440 void
441 cgi_poll_on_child(struct pollfd *fds, struct client *c)
443 int fd;
445 if (c->waiting_on_child)
446 return;
447 c->waiting_on_child = 1;
449 fds->events = POLLIN;
451 fd = fds->fd;
452 fds->fd = c->fd;
453 c->fd = fd;
456 void
457 cgi_poll_on_client(struct pollfd *fds, struct client *c)
459 int fd;
461 if (!c->waiting_on_child)
462 return;
463 c->waiting_on_child = 0;
465 fd = fds->fd;
466 fds->fd = c->fd;
467 c->fd = fd;
470 void
471 handle_cgi(struct pollfd *fds, struct client *c)
473 ssize_t r;
475 /* ensure c->fd is the child and fds->fd the client */
476 cgi_poll_on_client(fds, c);
478 while (1) {
479 if (c->len == 0) {
480 if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
481 goto end;
482 if (r == -1) {
483 if (errno == EAGAIN || errno == EWOULDBLOCK) {
484 cgi_poll_on_child(fds, c);
485 return;
487 goto end;
489 c->len = r;
490 c->off = 0;
492 /* XXX: if we haven't still read a whole
493 * reply line, we should go back to poll! */
494 if (c->code == -1) {
495 c->code = 0;
496 log_request(c, c->sbuf, sizeof(c->sbuf));
500 while (c->len > 0) {
501 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
502 case -1:
503 goto end;
505 case TLS_WANT_POLLOUT:
506 fds->events = POLLOUT;
507 return;
509 case TLS_WANT_POLLIN:
510 fds->events = POLLIN;
511 return;
513 default:
514 c->off += r;
515 c->len -= r;
516 break;
521 end:
522 goodbye(fds, c);
525 void
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;
533 return;
534 case TLS_WANT_POLLOUT:
535 pfd->events = POLLOUT;
536 return;
539 connected_clients--;
541 tls_free(c->ctx);
542 c->ctx = NULL;
544 if (c->buf != MAP_FAILED)
545 munmap(c->buf, c->len);
547 if (c->fd != -1)
548 close(c->fd);
550 close(pfd->fd);
551 pfd->fd = -1;
554 void
555 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
557 int i, fd;
558 struct sockaddr_storage addr;
559 socklen_t len;
561 len = sizeof(addr);
562 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
563 if (errno == EWOULDBLOCK)
564 return;
565 fatal("accept: %s", strerror(errno));
568 mark_nonblock(fd);
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! */
576 fds[i].fd = fd;
577 fds[i].events = POLLIN;
579 clients[i].state = S_HANDSHAKE;
580 clients[i].fd = -1;
581 clients[i].child = 0;
582 clients[i].waiting_on_child = 0;
583 clients[i].buf = MAP_FAILED;
584 clients[i].addr = addr;
586 connected_clients++;
587 return;
591 close(fd);
594 void
595 handle(struct pollfd *fds, struct client *client)
597 switch (client->state) {
598 case S_HANDSHAKE:
599 handle_handshake(fds, client);
600 break;
602 case S_OPEN:
603 handle_open_conn(fds, client);
604 break;
606 case S_INITIALIZING:
607 if (!start_reply(fds, client, client->code, client->meta))
608 return;
610 if (client->code != SUCCESS) {
611 /* we don't need a body */
612 goodbye(fds, client);
613 return;
616 client->state = S_SENDING;
618 /* fallthrough */
620 case S_SENDING:
621 if (client->child)
622 handle_cgi(fds, client);
623 else
624 send_file(fds, client);
625 break;
627 case S_CLOSING:
628 goodbye(fds, client);
629 break;
631 default:
632 /* unreachable */
633 abort();
637 void
638 loop(struct tls *ctx, int sock4, int sock6)
640 int i;
641 struct client clients[MAX_USERS];
642 struct pollfd fds[MAX_USERS];
644 connected_clients = 0;
646 for (i = 0; i < MAX_USERS; ++i) {
647 fds[i].fd = -1;
648 fds[i].events = POLLIN;
649 bzero(&clients[i], sizeof(struct client));
652 fds[0].fd = sock4;
653 fds[1].fd = sock6;
655 for (;;) {
656 if (poll(fds, MAX_USERS, INFTIM) == -1) {
657 if (errno == EINTR) {
658 fprintf(stderr, "connected clients: %d\n",
659 connected_clients);
660 continue;
662 fatal("poll: %s", strerror(errno));
665 for (i = 0; i < MAX_USERS; i++) {
666 if (fds[i].revents == 0)
667 continue;
669 if (fds[i].revents & (POLLERR|POLLNVAL))
670 fatal("bad fd %d: %s", fds[i].fd,
671 strerror(errno));
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]);
678 continue;
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);
686 else
687 handle(&fds[i], &clients[i]);