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;
37 assert(path != NULL);
38 if ((*fd = openat(c->host->dirfd, *path ? path : ".",
39 O_RDONLY | O_NOFOLLOW)) == -1) {
40 return FILE_MISSING;
41 }
43 if (fstat(*fd, &sb) == -1) {
44 LOGN(c, "failed stat for %s: %s", path, strerror(errno));
45 return FILE_MISSING;
46 }
48 if (S_ISDIR(sb.st_mode))
49 return FILE_DIRECTORY;
51 if (sb.st_mode & S_IXUSR)
52 return FILE_EXECUTABLE;
54 return FILE_EXISTS;
55 }
57 int
58 open_file(struct pollfd *fds, struct client *c)
59 {
60 switch (check_path(c, c->iri.path, &c->fd)) {
61 case FILE_EXECUTABLE:
62 if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi))
63 return start_cgi(c->iri.path, "", c->iri.query, fds, c);
65 /* fallthrough */
67 case FILE_EXISTS:
68 if ((c->len = filesize(c->fd)) == -1) {
69 LOGE(c, "failed to get file size for %s", c->iri.path);
70 goodbye(fds, c);
71 return 0;
72 }
74 if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
75 c->fd, 0)) == MAP_FAILED) {
76 LOGW(c, "mmap: %s: %s", c->iri.path, strerror(errno));
77 goodbye(fds, c);
78 return 0;
79 }
80 c->i = c->buf;
81 if (!start_reply(fds, c, SUCCESS, mime(c->host, c->iri.path)))
82 return 0;
83 send_file(fds, c);
84 return 0;
86 case FILE_DIRECTORY:
87 close(c->fd);
88 c->fd = -1;
89 send_dir(fds, c);
90 return 0;
92 case FILE_MISSING:
93 if (c->host->cgi != NULL && starts_with(c->iri.path, c->host->cgi))
94 return check_for_cgi(c->iri.path, c->iri.query, fds, c);
96 if (!start_reply(fds, c, NOT_FOUND, "not found"))
97 return 0;
98 goodbye(fds, c);
99 return 0;
101 default:
102 /* unreachable */
103 abort();
108 /*
109 * the inverse of this algorithm, i.e. starting from the start of the
110 * path + strlen(cgi), and checking if each component, should be
111 * faster. But it's tedious to write. This does the opposite: starts
112 * from the end and strip one component at a time, until either an
113 * executable is found or we emptied the path.
114 */
115 int
116 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
118 char *end;
119 end = strchr(path, '\0');
121 /* NB: assume CGI is enabled and path matches cgi */
123 while (end > path) {
124 /* go up one level. UNIX paths are simple and POSIX
125 * dirname, with its ambiguities on if the given path
126 * is changed or not, gives me headaches. */
127 while (*end != '/')
128 end--;
129 *end = '\0';
131 switch (check_path(c, path, &c->fd)) {
132 case FILE_EXECUTABLE:
133 return start_cgi(path, end+1, query, fds,c);
134 case FILE_MISSING:
135 break;
136 default:
137 goto err;
140 *end = '/';
141 end--;
144 err:
145 if (!start_reply(fds, c, NOT_FOUND, "not found"))
146 return 0;
147 goodbye(fds, c);
148 return 0;
151 void
152 mark_nonblock(int fd)
154 int flags;
156 if ((flags = fcntl(fd, F_GETFL)) == -1)
157 fatal("fcntl(F_GETFL): %s", strerror(errno));
158 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
159 fatal("fcntl(F_SETFL): %s", strerror(errno));
162 void
163 handle_handshake(struct pollfd *fds, struct client *c)
165 struct vhost *h;
166 const char *servname;
168 switch (tls_handshake(c->ctx)) {
169 case 0: /* success */
170 case -1: /* already handshaked */
171 break;
172 case TLS_WANT_POLLIN:
173 fds->events = POLLIN;
174 return;
175 case TLS_WANT_POLLOUT:
176 fds->events = POLLOUT;
177 return;
178 default:
179 /* unreachable */
180 abort();
183 servname = tls_conn_servername(c->ctx);
185 for (h = hosts; h->domain != NULL; ++h) {
186 if (!strcmp(h->domain, "*"))
187 break;
189 if (servname != NULL && !fnmatch(h->domain, servname, 0))
190 break;
193 if (h->domain != NULL) {
194 c->state = S_OPEN;
195 c->host = h;
196 handle_open_conn(fds, c);
197 return;
200 if (servname != NULL)
201 strncpy(c->req, servname, sizeof(c->req));
202 else
203 strncpy(c->req, "null", sizeof(c->req));
205 if (!start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI"))
206 return;
207 goodbye(fds, c);
210 void
211 handle_open_conn(struct pollfd *fds, struct client *c)
213 const char *parse_err = "invalid request";
215 bzero(c->req, sizeof(c->req));
216 bzero(&c->iri, sizeof(c->iri));
218 switch (tls_read(c->ctx, c->req, sizeof(c->req)-1)) {
219 case -1:
220 LOGE(c, "tls_read: %s", tls_error(c->ctx));
221 goodbye(fds, c);
222 return;
224 case TLS_WANT_POLLIN:
225 fds->events = POLLIN;
226 return;
228 case TLS_WANT_POLLOUT:
229 fds->events = POLLOUT;
230 return;
233 if (!trim_req_iri(c->req) || !parse_iri(c->req, &c->iri, &parse_err)) {
234 if (!start_reply(fds, c, BAD_REQUEST, parse_err))
235 return;
236 goodbye(fds, c);
237 return;
240 /* XXX: we should check that the SNI matches the requested host */
241 if (strcmp(c->iri.schema, "gemini") || c->iri.port_no != conf.port) {
242 if (!start_reply(fds, c, PROXY_REFUSED, "won't proxy request"))
243 return;
244 goodbye(fds, c);
245 return;
248 open_file(fds, c);
251 int
252 start_reply(struct pollfd *pfd, struct client *c, int code, const char *meta)
254 char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
255 size_t len;
257 c->code = code;
258 c->meta = meta;
259 c->state = S_INITIALIZING;
261 snprintf(buf, sizeof(buf), "%d ", code);
262 strlcat(buf, meta, sizeof(buf));
263 if (!strcmp(meta, "text/gemini") && c->host->lang != NULL) {
264 strlcat(buf, "; lang=", sizeof(buf));
265 strlcat(buf, c->host->lang, sizeof(buf));
268 len = strlcat(buf, "\r\n", sizeof(buf));
269 assert(len < sizeof(buf));
271 switch (tls_write(c->ctx, buf, len)) {
272 case TLS_WANT_POLLIN:
273 pfd->events = POLLIN;
274 return 0;
275 case TLS_WANT_POLLOUT:
276 pfd->events = POLLOUT;
277 return 0;
278 default:
279 log_request(c, buf, sizeof(buf));
280 return 1;
284 int
285 start_cgi(const char *spath, const char *relpath, const char *query,
286 struct pollfd *fds, struct client *c)
288 char addr[NI_MAXHOST];
289 const char *ruser, *cissuer, *chash;
290 int e;
292 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
293 addr, sizeof(addr),
294 NULL, 0,
295 NI_NUMERICHOST);
296 if (e != 0)
297 goto err;
299 if (tls_peer_cert_provided(c->ctx)) {
300 ruser = tls_peer_cert_subject(c->ctx);
301 cissuer = tls_peer_cert_issuer(c->ctx);
302 chash = tls_peer_cert_hash(c->ctx);
303 } else {
304 ruser = NULL;
305 cissuer = NULL;
306 chash = NULL;
309 if (!send_string(exfd, spath)
310 || !send_string(exfd, relpath)
311 || !send_string(exfd, query)
312 || !send_string(exfd, addr)
313 || !send_string(exfd, ruser)
314 || !send_string(exfd, cissuer)
315 || !send_string(exfd, chash)
316 || !send_vhost(exfd, c->host))
317 goto err;
319 close(c->fd);
320 if ((c->fd = recv_fd(exfd)) == -1) {
321 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
322 return 0;
323 goodbye(fds, c);
324 return 0;
326 c->child = 1;
327 c->state = S_SENDING;
328 cgi_poll_on_child(fds, c);
329 c->code = -1;
330 /* handle_cgi(fds, c); */
331 return 0;
333 err:
334 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
335 fatal("cannot talk to the executor process");
338 void
339 send_file(struct pollfd *fds, struct client *c)
341 ssize_t ret, len;
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 goodbye(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 goodbye(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 if (!start_reply(fds, c, TEMP_REDIRECT, c->sbuf))
390 return;
391 goodbye(fds, c);
392 return;
395 len = strlen(c->iri.path);
396 if (len > 0 && c->iri.path[len-1] != '/') {
397 /* redirect to url with the trailing / */
398 strlcpy(c->sbuf, c->iri.path, sizeof(c->sbuf));
399 strlcat(c->sbuf, "/", sizeof(c->sbuf));
400 if (!start_reply(fds, c, TEMP_REDIRECT, c->sbuf))
401 return;
402 goodbye(fds, c);
403 return;
406 strlcpy(c->sbuf, c->iri.path, sizeof(c->sbuf));
407 if (len != 0)
408 strlcat(c->sbuf, "/", sizeof(c->sbuf));
409 len = strlcat(c->sbuf, "index.gmi", sizeof(c->sbuf));
411 if (len >= sizeof(c->sbuf)) {
412 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
413 return;
414 goodbye(fds, c);
415 return;
418 close(c->fd);
419 c->iri.path = c->sbuf;
420 open_file(fds, c);
423 void
424 cgi_poll_on_child(struct pollfd *fds, struct client *c)
426 int fd;
428 if (c->waiting_on_child)
429 return;
430 c->waiting_on_child = 1;
432 fds->events = POLLIN;
434 fd = fds->fd;
435 fds->fd = c->fd;
436 c->fd = fd;
439 void
440 cgi_poll_on_client(struct pollfd *fds, struct client *c)
442 int fd;
444 if (!c->waiting_on_child)
445 return;
446 c->waiting_on_child = 0;
448 fd = fds->fd;
449 fds->fd = c->fd;
450 c->fd = fd;
453 void
454 handle_cgi(struct pollfd *fds, struct client *c)
456 ssize_t r;
458 /* ensure c->fd is the child and fds->fd the client */
459 cgi_poll_on_client(fds, c);
461 while (1) {
462 if (c->len == 0) {
463 if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
464 goto end;
465 if (r == -1) {
466 if (errno == EAGAIN || errno == EWOULDBLOCK) {
467 cgi_poll_on_child(fds, c);
468 return;
470 goto end;
472 c->len = r;
473 c->off = 0;
475 /* XXX: if we haven't still read a whole
476 * reply line, we should go back to poll! */
477 if (c->code == -1) {
478 c->code = 0;
479 log_request(c, c->sbuf, sizeof(c->sbuf));
483 while (c->len > 0) {
484 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
485 case -1:
486 goto end;
488 case TLS_WANT_POLLOUT:
489 fds->events = POLLOUT;
490 return;
492 case TLS_WANT_POLLIN:
493 fds->events = POLLIN;
494 return;
496 default:
497 c->off += r;
498 c->len -= r;
499 break;
504 end:
505 goodbye(fds, c);
508 void
509 goodbye(struct pollfd *pfd, struct client *c)
511 c->state = S_CLOSING;
513 switch (tls_close(c->ctx)) {
514 case TLS_WANT_POLLIN:
515 pfd->events = POLLIN;
516 return;
517 case TLS_WANT_POLLOUT:
518 pfd->events = POLLOUT;
519 return;
522 connected_clients--;
524 tls_free(c->ctx);
525 c->ctx = NULL;
527 if (c->buf != MAP_FAILED)
528 munmap(c->buf, c->len);
530 if (c->fd != -1)
531 close(c->fd);
533 close(pfd->fd);
534 pfd->fd = -1;
537 void
538 do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
540 int i, fd;
541 struct sockaddr_storage addr;
542 socklen_t len;
544 len = sizeof(addr);
545 if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
546 if (errno == EWOULDBLOCK)
547 return;
548 fatal("accept: %s", strerror(errno));
551 mark_nonblock(fd);
553 for (i = 0; i < MAX_USERS; ++i) {
554 if (fds[i].fd == -1) {
555 bzero(&clients[i], sizeof(struct client));
556 if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
557 break; /* goodbye fd! */
559 fds[i].fd = fd;
560 fds[i].events = POLLIN;
562 clients[i].state = S_HANDSHAKE;
563 clients[i].fd = -1;
564 clients[i].child = 0;
565 clients[i].waiting_on_child = 0;
566 clients[i].buf = MAP_FAILED;
567 clients[i].addr = addr;
569 connected_clients++;
570 return;
574 close(fd);
577 void
578 handle(struct pollfd *fds, struct client *client)
580 switch (client->state) {
581 case S_HANDSHAKE:
582 handle_handshake(fds, client);
583 break;
585 case S_OPEN:
586 handle_open_conn(fds, client);
587 break;
589 case S_INITIALIZING:
590 if (!start_reply(fds, client, client->code, client->meta))
591 return;
593 if (client->code != SUCCESS) {
594 /* we don't need a body */
595 goodbye(fds, client);
596 return;
599 client->state = S_SENDING;
601 /* fallthrough */
603 case S_SENDING:
604 if (client->child)
605 handle_cgi(fds, client);
606 else
607 send_file(fds, client);
608 break;
610 case S_CLOSING:
611 goodbye(fds, client);
612 break;
614 default:
615 /* unreachable */
616 abort();
620 void
621 loop(struct tls *ctx, int sock4, int sock6)
623 int i;
624 struct client clients[MAX_USERS];
625 struct pollfd fds[MAX_USERS];
627 connected_clients = 0;
629 for (i = 0; i < MAX_USERS; ++i) {
630 fds[i].fd = -1;
631 fds[i].events = POLLIN;
632 bzero(&clients[i], sizeof(struct client));
635 fds[0].fd = sock4;
636 fds[1].fd = sock6;
638 for (;;) {
639 if (poll(fds, MAX_USERS, INFTIM) == -1) {
640 if (errno == EINTR) {
641 fprintf(stderr, "connected clients: %d\n",
642 connected_clients);
643 continue;
645 fatal("poll: %s", strerror(errno));
648 for (i = 0; i < MAX_USERS; i++) {
649 if (fds[i].revents == 0)
650 continue;
652 if (fds[i].revents & (POLLERR|POLLNVAL))
653 fatal("bad fd %d: %s", fds[i].fd,
654 strerror(errno));
656 if (fds[i].revents & POLLHUP) {
657 /* fds[i] may be the fd of the stdin
658 * of a cgi script that has exited. */
659 if (!clients[i].waiting_on_child) {
660 goodbye(&fds[i], &clients[i]);
661 continue;
665 if (fds[i].fd == sock4)
666 do_accept(sock4, ctx, fds, clients);
667 else if (fds[i].fd == sock6)
668 do_accept(sock6, ctx, fds, clients);
669 else
670 handle(&fds[i], &clients[i]);