2 3e4749f7 2020-10-02 op * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 3e4749f7 2020-10-02 op * Permission to use, copy, modify, and distribute this software for any
5 3e4749f7 2020-10-02 op * purpose with or without fee is hereby granted, provided that the above
6 3e4749f7 2020-10-02 op * copyright notice and this permission notice appear in all copies.
8 3e4749f7 2020-10-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3e4749f7 2020-10-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3e4749f7 2020-10-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3e4749f7 2020-10-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3e4749f7 2020-10-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3e4749f7 2020-10-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3e4749f7 2020-10-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 f28f9311 2020-10-14 op #include <sys/mman.h>
18 3e4749f7 2020-10-02 op #include <sys/stat.h>
20 3e4749f7 2020-10-02 op #include <assert.h>
21 3e4749f7 2020-10-02 op #include <err.h>
22 592fd624 2020-10-07 op #include <errno.h>
23 3e4749f7 2020-10-02 op #include <fcntl.h>
24 231bfcdc 2020-12-21 op #include <limits.h>
25 709d6e5e 2021-01-10 op #include <netdb.h>
26 0cf902af 2020-11-03 op #include <signal.h>
27 80bbcad5 2021-01-10 op #include <stdarg.h>
28 3e4749f7 2020-10-02 op #include <string.h>
30 488f059a 2020-12-24 op #include "gmid.h"
32 80bbcad5 2021-01-10 op #define LOGE(c, fmt, ...) logs(LOG_ERR, c, fmt, __VA_ARGS__)
33 80bbcad5 2021-01-10 op #define LOGN(c, fmt, ...) logs(LOG_NOTICE, c, fmt, __VA_ARGS__)
34 80bbcad5 2021-01-10 op #define LOGI(c, fmt, ...) logs(LOG_INFO, c, fmt, __VA_ARGS__)
35 80bbcad5 2021-01-10 op #define LOGD(c, fmt, ...) logs(LOG_DEBUG, c, fmt, __VA_ARGS__)
37 15902770 2021-01-15 op struct vhost hosts[HOSTSLEN];
39 120381c9 2020-11-06 op int connected_clients;
42 15902770 2021-01-15 op struct conf conf;
44 33d32d1f 2020-12-25 op struct etm { /* file extension to mime */
45 33d32d1f 2020-12-25 op const char *mime;
46 33d32d1f 2020-12-25 op const char *ext;
47 33d32d1f 2020-12-25 op } filetypes[] = {
48 33d32d1f 2020-12-25 op {"application/pdf", "pdf"},
50 33d32d1f 2020-12-25 op {"image/gif", "gif"},
51 33d32d1f 2020-12-25 op {"image/jpeg", "jpg"},
52 33d32d1f 2020-12-25 op {"image/jpeg", "jpeg"},
53 33d32d1f 2020-12-25 op {"image/png", "png"},
54 33d32d1f 2020-12-25 op {"image/svg+xml", "svg"},
56 33d32d1f 2020-12-25 op {"text/gemini", "gemini"},
57 33d32d1f 2020-12-25 op {"text/gemini", "gmi"},
58 33d32d1f 2020-12-25 op {"text/markdown", "markdown"},
59 33d32d1f 2020-12-25 op {"text/markdown", "md"},
60 33d32d1f 2020-12-25 op {"text/plain", "txt"},
61 33d32d1f 2020-12-25 op {"text/xml", "xml"},
66 9b374f41 2021-01-10 op static inline void
67 9b374f41 2021-01-10 op safe_setenv(const char *name, const char *val)
69 9b374f41 2021-01-10 op if (val == NULL)
71 9b374f41 2021-01-10 op setenv(name, val, 1);
74 80bbcad5 2021-01-10 op __attribute__ ((format (printf, 1, 2)))
75 0d7a38c4 2021-01-11 op __attribute__ ((__noreturn__))
76 0d7a38c4 2021-01-11 op static inline void
77 80bbcad5 2021-01-10 op fatal(const char *fmt, ...)
81 80bbcad5 2021-01-10 op va_start(ap, fmt);
83 15902770 2021-01-15 op if (conf.foreground) {
84 80bbcad5 2021-01-10 op vfprintf(stderr, fmt, ap);
85 80bbcad5 2021-01-10 op fprintf(stderr, "\n");
87 80bbcad5 2021-01-10 op vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
93 80bbcad5 2021-01-10 op __attribute__ ((format (printf, 3, 4)))
94 80bbcad5 2021-01-10 op static inline void
95 709d6e5e 2021-01-10 op logs(int priority, struct client *c,
96 709d6e5e 2021-01-10 op const char *fmt, ...)
98 709d6e5e 2021-01-10 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
99 709d6e5e 2021-01-10 op char *fmted, *s;
104 80bbcad5 2021-01-10 op va_start(ap, fmt);
106 709d6e5e 2021-01-10 op len = sizeof(c->addr);
107 709d6e5e 2021-01-10 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
108 709d6e5e 2021-01-10 op hbuf, sizeof(hbuf),
109 709d6e5e 2021-01-10 op sbuf, sizeof(sbuf),
110 709d6e5e 2021-01-10 op NI_NUMERICHOST | NI_NUMERICSERV);
112 709d6e5e 2021-01-10 op fatal("getnameinfo: %s", gai_strerror(ec));
114 709d6e5e 2021-01-10 op if (vasprintf(&fmted, fmt, ap) == -1)
115 709d6e5e 2021-01-10 op fatal("vasprintf: %s", strerror(errno));
117 15902770 2021-01-15 op if (conf.foreground)
118 709d6e5e 2021-01-10 op fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
120 709d6e5e 2021-01-10 op if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
121 709d6e5e 2021-01-10 op fatal("asprintf: %s", strerror(errno));
122 709d6e5e 2021-01-10 op syslog(priority | LOG_DAEMON, "%s", s);
132 4a28dd01 2020-12-28 op sig_handler(int sig)
138 72f653b6 2020-11-06 op starts_with(const char *str, const char *prefix)
142 72f653b6 2020-11-06 op for (i = 0; prefix[i] != '\0'; ++i)
143 72f653b6 2020-11-06 op if (str[i] != prefix[i])
149 592fd624 2020-10-07 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
151 3e4749f7 2020-10-02 op char buf[1030] = {0}; /* status + ' ' + max reply len + \r\n\0 */
155 592fd624 2020-10-07 op client->code = code;
156 592fd624 2020-10-07 op client->meta = reason;
157 592fd624 2020-10-07 op client->state = S_INITIALIZING;
159 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
160 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
161 592fd624 2020-10-07 op ret = tls_write(client->ctx, buf, len);
162 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
163 592fd624 2020-10-07 op pfd->events = POLLIN;
167 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
168 592fd624 2020-10-07 op pfd->events = POLLOUT;
176 f28f9311 2020-10-14 op filesize(int fd)
180 f28f9311 2020-10-14 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
182 f28f9311 2020-10-14 op if (lseek(fd, 0, SEEK_SET) == -1)
188 cc68fe70 2020-10-07 op path_ext(const char *path)
190 cc68fe70 2020-10-07 op const char *end;
192 cc68fe70 2020-10-07 op end = path + strlen(path)-1; /* the last byte before the NUL */
193 cc68fe70 2020-10-07 op for (; end != path; --end) {
194 cc68fe70 2020-10-07 op if (*end == '.')
195 cc68fe70 2020-10-07 op return end+1;
196 cc68fe70 2020-10-07 op if (*end == '/')
204 cc68fe70 2020-10-07 op mime(const char *path)
206 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
207 cc68fe70 2020-10-07 op struct etm *t;
209 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
212 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
213 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
214 cc68fe70 2020-10-07 op return t->mime;
220 d7802bb4 2020-12-02 op check_path(struct client *c, const char *path, int *fd)
222 72342dc9 2020-11-06 op struct stat sb;
224 f28f9311 2020-10-14 op assert(path != NULL);
225 15902770 2021-01-15 op if ((*fd = openat(c->host->dirfd, *path ? path : ".",
226 a5d310bc 2020-11-10 op O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
227 a5d310bc 2020-11-10 op return FILE_MISSING;
230 a5d310bc 2020-11-10 op if (fstat(*fd, &sb) == -1) {
231 d7802bb4 2020-12-02 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
232 a5d310bc 2020-11-10 op return FILE_MISSING;
235 a5d310bc 2020-11-10 op if (S_ISDIR(sb.st_mode))
236 a5d310bc 2020-11-10 op return FILE_DIRECTORY;
238 a5d310bc 2020-11-10 op if (sb.st_mode & S_IXUSR)
239 a5d310bc 2020-11-10 op return FILE_EXECUTABLE;
241 a5d310bc 2020-11-10 op return FILE_EXISTS;
245 a5d310bc 2020-11-10 op * the inverse of this algorithm, i.e. starting from the start of the
246 a5d310bc 2020-11-10 op * path + strlen(cgi), and checking if each component, should be
247 a5d310bc 2020-11-10 op * faster. But it's tedious to write. This does the opposite: starts
248 a5d310bc 2020-11-10 op * from the end and strip one component at a time, until either an
249 a5d310bc 2020-11-10 op * executable is found or we emptied the path.
252 a5d310bc 2020-11-10 op check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
255 a5d310bc 2020-11-10 op end = strchr(path, '\0');
257 a5d310bc 2020-11-10 op /* NB: assume CGI is enabled and path matches cgi */
259 a5d310bc 2020-11-10 op while (end > path) {
260 a5d310bc 2020-11-10 op /* go up one level. UNIX paths are simple and POSIX
261 a5d310bc 2020-11-10 op * dirname, with its ambiguities on if the given path
262 a5d310bc 2020-11-10 op * is changed or not, gives me headaches. */
263 a5d310bc 2020-11-10 op while (*end != '/')
267 d7802bb4 2020-12-02 op switch (check_path(c, path, &c->fd)) {
268 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
269 a5d310bc 2020-11-10 op return start_cgi(path, end+1, query, fds,c);
270 a5d310bc 2020-11-10 op case FILE_MISSING:
281 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
283 a5d310bc 2020-11-10 op goodbye(fds, c);
289 33d32d1f 2020-12-25 op open_file(char *fpath, char *query, struct pollfd *fds, struct client *c)
291 d7802bb4 2020-12-02 op switch (check_path(c, fpath, &c->fd)) {
292 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
293 15902770 2021-01-15 op if (c->host->cgi != NULL && starts_with(fpath, c->host->cgi))
294 a5d310bc 2020-11-10 op return start_cgi(fpath, "", query, fds, c);
296 a5d310bc 2020-11-10 op /* fallthrough */
298 a5d310bc 2020-11-10 op case FILE_EXISTS:
299 a5d310bc 2020-11-10 op if ((c->len = filesize(c->fd)) == -1) {
300 d7802bb4 2020-12-02 op LOGE(c, "failed to get file size for %s", fpath);
301 a5d310bc 2020-11-10 op goodbye(fds, c);
305 a5d310bc 2020-11-10 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
306 a5d310bc 2020-11-10 op c->fd, 0)) == MAP_FAILED) {
307 a5d310bc 2020-11-10 op warn("mmap: %s", fpath);
308 a5d310bc 2020-11-10 op goodbye(fds, c);
311 a5d310bc 2020-11-10 op c->i = c->buf;
312 a5d310bc 2020-11-10 op return start_reply(fds, c, SUCCESS, mime(fpath));
314 a5d310bc 2020-11-10 op case FILE_DIRECTORY:
315 d7802bb4 2020-12-02 op LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
316 f28f9311 2020-10-14 op close(c->fd);
318 f28f9311 2020-10-14 op send_dir(fpath, fds, c);
321 a5d310bc 2020-11-10 op case FILE_MISSING:
322 15902770 2021-01-15 op if (c->host->cgi != NULL && starts_with(fpath, c->host->cgi))
323 a5d310bc 2020-11-10 op return check_for_cgi(fpath, query, fds, c);
325 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
327 f28f9311 2020-10-14 op goodbye(fds, c);
331 a5d310bc 2020-11-10 op /* unreachable */
337 a5d310bc 2020-11-10 op start_cgi(const char *spath, const char *relpath, const char *query,
338 75d233f0 2020-11-06 op struct pollfd *fds, struct client *c)
341 a5d310bc 2020-11-10 op int p[2]; /* read end, write end */
343 72342dc9 2020-11-06 op if (pipe(p) == -1)
346 72342dc9 2020-11-06 op switch (pid = fork()) {
350 a5d310bc 2020-11-10 op case 0: { /* child */
351 721e2325 2020-11-18 op char *ex, *requri, *portno;
352 85dff1f9 2021-01-11 op char addr[NI_MAXHOST];
353 72342dc9 2020-11-06 op char *argv[] = { NULL, NULL, NULL };
357 72342dc9 2020-11-06 op if (dup2(p[1], 1) == -1)
358 72342dc9 2020-11-06 op goto childerr;
360 72342dc9 2020-11-06 op if (inet_ntop(c->af, &c->addr, addr, sizeof(addr)) == NULL)
361 a5d310bc 2020-11-10 op goto childerr;
363 85dff1f9 2021-01-11 op ec = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
364 85dff1f9 2021-01-11 op addr, sizeof(addr),
366 85dff1f9 2021-01-11 op NI_NUMERICHOST | NI_NUMERICSERV);
368 85dff1f9 2021-01-11 op goto childerr;
370 15902770 2021-01-15 op if (asprintf(&portno, "%d", conf.port) == -1)
371 721e2325 2020-11-18 op goto childerr;
373 15902770 2021-01-15 op if (asprintf(&ex, "%s/%s", c->host->dir, spath) == -1)
374 72342dc9 2020-11-06 op goto childerr;
376 a5d310bc 2020-11-10 op if (asprintf(&requri, "%s%s%s", spath,
377 a5d310bc 2020-11-10 op *relpath == '\0' ? "" : "/",
378 a5d310bc 2020-11-10 op relpath) == -1)
379 a5d310bc 2020-11-10 op goto childerr;
381 a5d310bc 2020-11-10 op argv[0] = argv[1] = ex;
383 60ba426e 2020-11-06 op /* fix the env */
384 9b374f41 2021-01-10 op safe_setenv("GATEWAY_INTERFACE", "CGI/1.1");
385 9b374f41 2021-01-10 op safe_setenv("SERVER_SOFTWARE", "gmid");
386 9b374f41 2021-01-10 op safe_setenv("SERVER_PORT", portno);
387 a5d310bc 2020-11-10 op /* setenv("SERVER_NAME", "", 1); */
388 9b374f41 2021-01-10 op safe_setenv("SCRIPT_NAME", spath);
389 9b374f41 2021-01-10 op safe_setenv("SCRIPT_EXECUTABLE", ex);
390 9b374f41 2021-01-10 op safe_setenv("REQUEST_URI", requri);
391 9b374f41 2021-01-10 op safe_setenv("REQUEST_RELATIVE", relpath);
392 9b374f41 2021-01-10 op safe_setenv("QUERY_STRING", query);
393 9b374f41 2021-01-10 op safe_setenv("REMOTE_HOST", addr);
394 9b374f41 2021-01-10 op safe_setenv("REMOTE_ADDR", addr);
395 15902770 2021-01-15 op safe_setenv("DOCUMENT_ROOT", c->host->dir);
397 677afbd3 2020-12-02 op if (tls_peer_cert_provided(c->ctx)) {
398 9b374f41 2021-01-10 op safe_setenv("AUTH_TYPE", "Certificate");
399 9b374f41 2021-01-10 op safe_setenv("REMOTE_USER", tls_peer_cert_subject(c->ctx));
400 9b374f41 2021-01-10 op safe_setenv("TLS_CLIENT_ISSUER", tls_peer_cert_issuer(c->ctx));
401 9b374f41 2021-01-10 op safe_setenv("TLS_CLIENT_HASH", tls_peer_cert_hash(c->ctx));
404 a5d310bc 2020-11-10 op execvp(ex, argv);
405 72342dc9 2020-11-06 op goto childerr;
408 72342dc9 2020-11-06 op default: /* parent */
410 72342dc9 2020-11-06 op close(c->fd);
411 72342dc9 2020-11-06 op c->fd = p[0];
412 72342dc9 2020-11-06 op c->child = pid;
413 aff8d190 2020-11-06 op mark_nonblock(c->fd);
414 aff8d190 2020-11-06 op c->state = S_SENDING;
415 72342dc9 2020-11-06 op handle_cgi(fds, c);
420 72342dc9 2020-11-06 op if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
422 72342dc9 2020-11-06 op goodbye(fds, c);
426 72342dc9 2020-11-06 op dprintf(p[1], "%d internal server error\r\n", TEMP_FAILURE);
432 6c6c7a0e 2020-12-28 op cgi_poll_on_child(struct pollfd *fds, struct client *c)
436 aff8d190 2020-11-06 op if (c->waiting_on_child)
438 aff8d190 2020-11-06 op c->waiting_on_child = 1;
440 aff8d190 2020-11-06 op fds->events = POLLIN;
442 aff8d190 2020-11-06 op fd = fds->fd;
443 aff8d190 2020-11-06 op fds->fd = c->fd;
448 6c6c7a0e 2020-12-28 op cgi_poll_on_client(struct pollfd *fds, struct client *c)
452 aff8d190 2020-11-06 op if (!c->waiting_on_child)
454 aff8d190 2020-11-06 op c->waiting_on_child = 0;
456 aff8d190 2020-11-06 op fd = fds->fd;
457 aff8d190 2020-11-06 op fds->fd = c->fd;
462 aff8d190 2020-11-06 op handle_cgi(struct pollfd *fds, struct client *c)
466 aff8d190 2020-11-06 op /* ensure c->fd is the child and fds->fd the client */
467 6c6c7a0e 2020-12-28 op cgi_poll_on_client(fds, c);
470 aff8d190 2020-11-06 op if (c->len == 0) {
471 aff8d190 2020-11-06 op if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
473 aff8d190 2020-11-06 op if (r == -1) {
474 aff8d190 2020-11-06 op if (errno == EAGAIN || errno == EWOULDBLOCK) {
475 6c6c7a0e 2020-12-28 op cgi_poll_on_child(fds, c);
484 aff8d190 2020-11-06 op while (c->len > 0) {
485 aff8d190 2020-11-06 op switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
489 72342dc9 2020-11-06 op case TLS_WANT_POLLOUT:
490 aff8d190 2020-11-06 op fds->events = POLLOUT;
493 72342dc9 2020-11-06 op case TLS_WANT_POLLIN:
494 aff8d190 2020-11-06 op fds->events = POLLIN;
506 72342dc9 2020-11-06 op goodbye(fds, c);
510 75d233f0 2020-11-06 op send_file(char *path, char *query, struct pollfd *fds, struct client *c)
512 f28f9311 2020-10-14 op ssize_t ret, len;
514 f28f9311 2020-10-14 op if (c->fd == -1) {
515 75d233f0 2020-11-06 op if (!open_file(path, query, fds, c))
517 f28f9311 2020-10-14 op c->state = S_SENDING;
520 f28f9311 2020-10-14 op len = (c->buf + c->len) - c->i;
522 f28f9311 2020-10-14 op while (len > 0) {
523 f28f9311 2020-10-14 op switch (ret = tls_write(c->ctx, c->i, len)) {
525 d7802bb4 2020-12-02 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
526 f28f9311 2020-10-14 op goodbye(fds, c);
529 f28f9311 2020-10-14 op case TLS_WANT_POLLIN:
530 f28f9311 2020-10-14 op fds->events = POLLIN;
533 f28f9311 2020-10-14 op case TLS_WANT_POLLOUT:
534 f28f9311 2020-10-14 op fds->events = POLLOUT;
544 f28f9311 2020-10-14 op goodbye(fds, c);
548 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
550 3e4749f7 2020-10-02 op char fpath[PATHBUF];
553 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
555 9c56b0a7 2020-10-14 op if (path[0] != '.')
556 3e4749f7 2020-10-02 op fpath[0] = '.';
558 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
559 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
560 3e4749f7 2020-10-02 op len = strlen(fpath);
562 3e4749f7 2020-10-02 op /* add a trailing / in case. */
563 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
564 3e4749f7 2020-10-02 op fpath[len] = '/';
567 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
569 75d233f0 2020-11-06 op send_file(fpath, NULL, fds, client);
573 9862b637 2021-01-13 op handle_handshake(struct pollfd *fds, struct client *c)
575 15902770 2021-01-15 op struct vhost *h;
576 15902770 2021-01-15 op const char *servname;
578 9862b637 2021-01-13 op switch (tls_handshake(c->ctx)) {
579 9862b637 2021-01-13 op case 0: /* success */
580 9862b637 2021-01-13 op case -1: /* already handshaked */
582 9862b637 2021-01-13 op case TLS_WANT_POLLIN:
583 9862b637 2021-01-13 op fds->events = POLLIN;
585 9862b637 2021-01-13 op case TLS_WANT_POLLOUT:
586 9862b637 2021-01-13 op fds->events = POLLOUT;
589 9862b637 2021-01-13 op /* unreachable */
593 15902770 2021-01-15 op servname = tls_conn_servername(c->ctx);
594 15902770 2021-01-15 op if (servname == NULL)
595 15902770 2021-01-15 op goto wronghost;
597 15902770 2021-01-15 op for (h = hosts; h->domain != NULL; ++h) {
598 15902770 2021-01-15 op if (!strcmp(h->domain, servname) || !strcmp(h->domain, "*"))
602 15902770 2021-01-15 op if (h->domain != NULL) {
603 15902770 2021-01-15 op c->state = S_OPEN;
605 15902770 2021-01-15 op handle_open_conn(fds, c);
610 15902770 2021-01-15 op /* XXX: check the correct response */
611 15902770 2021-01-15 op if (!start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI"))
613 15902770 2021-01-15 op goodbye(fds, c);
617 9862b637 2021-01-13 op handle_open_conn(struct pollfd *fds, struct client *c)
619 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
620 9862b637 2021-01-13 op const char *parse_err = "invalid request";
621 3c1cf9d0 2021-01-11 op struct iri iri;
623 9862b637 2021-01-13 op bzero(buf, sizeof(buf));
625 9862b637 2021-01-13 op switch (tls_read(c->ctx, buf, sizeof(buf)-1)) {
627 9862b637 2021-01-13 op LOGE(c, "tls_read: %s", tls_error(c->ctx));
628 9862b637 2021-01-13 op goodbye(fds, c);
631 9862b637 2021-01-13 op case TLS_WANT_POLLIN:
632 9862b637 2021-01-13 op fds->events = POLLIN;
635 9862b637 2021-01-13 op case TLS_WANT_POLLOUT:
636 9862b637 2021-01-13 op fds->events = POLLOUT;
640 9862b637 2021-01-13 op if (!trim_req_iri(buf) || !parse_iri(buf, &iri, &parse_err)) {
641 9862b637 2021-01-13 op if (!start_reply(fds, c, BAD_REQUEST, parse_err))
643 9862b637 2021-01-13 op goodbye(fds, c);
647 9862b637 2021-01-13 op if (strcmp(iri.schema, "gemini")) {
648 9862b637 2021-01-13 op if (!start_reply(fds, c, PROXY_REFUSED, "won't proxy request"))
650 9862b637 2021-01-13 op goodbye(fds, c);
654 9862b637 2021-01-13 op LOGI(c, "GET %s%s%s",
655 9862b637 2021-01-13 op *iri.path ? iri.path : "/",
656 9862b637 2021-01-13 op *iri.query ? "?" : "",
657 9862b637 2021-01-13 op *iri.query ? iri.query : "");
659 9862b637 2021-01-13 op send_file(iri.path, iri.query, fds, c);
663 9862b637 2021-01-13 op handle(struct pollfd *fds, struct client *client)
665 9862b637 2021-01-13 op switch (client->state) {
666 9862b637 2021-01-13 op case S_HANDSHAKE:
667 9862b637 2021-01-13 op handle_handshake(fds, client);
671 9862b637 2021-01-13 op handle_open_conn(fds, client);
674 592fd624 2020-10-07 op case S_INITIALIZING:
675 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
678 592fd624 2020-10-07 op if (client->code != SUCCESS) {
679 592fd624 2020-10-07 op /* we don't need a body */
680 592fd624 2020-10-07 op goodbye(fds, client);
684 592fd624 2020-10-07 op client->state = S_SENDING;
686 592fd624 2020-10-07 op /* fallthrough */
688 592fd624 2020-10-07 op case S_SENDING:
689 aff8d190 2020-11-06 op if (client->child != -1)
690 aff8d190 2020-11-06 op handle_cgi(fds, client);
692 aff8d190 2020-11-06 op send_file(NULL, NULL, fds, client);
695 592fd624 2020-10-07 op case S_CLOSING:
696 592fd624 2020-10-07 op goodbye(fds, client);
700 592fd624 2020-10-07 op /* unreachable */
706 592fd624 2020-10-07 op mark_nonblock(int fd)
710 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
711 80bbcad5 2021-01-10 op fatal("fcntl(F_GETFL): %s", strerror(errno));
712 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
713 80bbcad5 2021-01-10 op fatal("fcntl(F_SETFL): %s", strerror(errno));
717 9468027b 2020-10-15 op make_socket(int port, int family)
720 9468027b 2020-10-15 op struct sockaddr_in addr4;
721 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
722 9468027b 2020-10-15 op struct sockaddr *addr;
723 9468027b 2020-10-15 op socklen_t len;
725 9468027b 2020-10-15 op switch (family) {
726 9468027b 2020-10-15 op case AF_INET:
727 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
728 9468027b 2020-10-15 op addr4.sin_family = family;
729 9468027b 2020-10-15 op addr4.sin_port = htons(port);
730 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
731 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
732 9468027b 2020-10-15 op len = sizeof(addr4);
735 9468027b 2020-10-15 op case AF_INET6:
736 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
737 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
738 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
739 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
740 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
741 9468027b 2020-10-15 op len = sizeof(addr6);
745 9468027b 2020-10-15 op /* unreachable */
749 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
750 80bbcad5 2021-01-10 op fatal("socket: %s", strerror(errno));
753 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
754 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
757 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
758 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
760 592fd624 2020-10-07 op mark_nonblock(sock);
762 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
763 80bbcad5 2021-01-10 op fatal("bind: %s", strerror(errno));
765 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
766 80bbcad5 2021-01-10 op fatal("listen: %s", strerror(errno));
772 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
775 709d6e5e 2021-01-10 op struct sockaddr_storage addr;
776 3e4749f7 2020-10-02 op socklen_t len;
778 592fd624 2020-10-07 op len = sizeof(addr);
779 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
780 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
782 80bbcad5 2021-01-10 op fatal("accept: %s", strerror(errno));
785 592fd624 2020-10-07 op mark_nonblock(fd);
787 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
788 592fd624 2020-10-07 op if (fds[i].fd == -1) {
789 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
790 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
791 592fd624 2020-10-07 op break; /* goodbye fd! */
793 592fd624 2020-10-07 op fds[i].fd = fd;
794 592fd624 2020-10-07 op fds[i].events = POLLIN;
796 9862b637 2021-01-13 op clients[i].state = S_HANDSHAKE;
797 592fd624 2020-10-07 op clients[i].fd = -1;
798 72342dc9 2020-11-06 op clients[i].child = -1;
799 f28f9311 2020-10-14 op clients[i].buf = MAP_FAILED;
800 2c3a40fa 2020-11-06 op clients[i].af = AF_INET;
801 709d6e5e 2021-01-10 op clients[i].addr = addr;
803 120381c9 2020-11-06 op connected_clients++;
812 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
816 592fd624 2020-10-07 op c->state = S_CLOSING;
818 592fd624 2020-10-07 op ret = tls_close(c->ctx);
819 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLIN) {
820 592fd624 2020-10-07 op pfd->events = POLLIN;
823 592fd624 2020-10-07 op if (ret == TLS_WANT_POLLOUT) {
824 592fd624 2020-10-07 op pfd->events = POLLOUT;
828 120381c9 2020-11-06 op connected_clients--;
830 592fd624 2020-10-07 op tls_free(c->ctx);
831 592fd624 2020-10-07 op c->ctx = NULL;
833 f28f9311 2020-10-14 op if (c->buf != MAP_FAILED)
834 f28f9311 2020-10-14 op munmap(c->buf, c->len);
836 592fd624 2020-10-07 op if (c->fd != -1)
837 592fd624 2020-10-07 op close(c->fd);
839 592fd624 2020-10-07 op close(pfd->fd);
840 592fd624 2020-10-07 op pfd->fd = -1;
844 33756bd2 2021-01-10 op loop(struct tls *ctx, int sock4, int sock6)
847 592fd624 2020-10-07 op struct client clients[MAX_USERS];
848 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
850 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
851 592fd624 2020-10-07 op fds[i].fd = -1;
852 592fd624 2020-10-07 op fds[i].events = POLLIN;
853 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
856 33756bd2 2021-01-10 op fds[0].fd = sock4;
857 33756bd2 2021-01-10 op fds[1].fd = sock6;
860 16ace04d 2021-01-08 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
861 120381c9 2020-11-06 op if (errno == EINTR) {
862 d7802bb4 2020-12-02 op warnx("connected clients: %d",
863 d7802bb4 2020-12-02 op connected_clients);
866 80bbcad5 2021-01-10 op fatal("poll: %s", strerror(errno));
869 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
870 592fd624 2020-10-07 op if (fds[i].revents == 0)
873 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
874 80bbcad5 2021-01-10 op fatal("bad fd %d: %s", fds[i].fd,
875 d7802bb4 2020-12-02 op strerror(errno));
877 592fd624 2020-10-07 op if (fds[i].revents & POLLHUP) {
878 aff8d190 2020-11-06 op /* fds[i] may be the fd of the stdin
879 aff8d190 2020-11-06 op * of a cgi script that has exited. */
880 aff8d190 2020-11-06 op if (!clients[i].waiting_on_child) {
881 aff8d190 2020-11-06 op goodbye(&fds[i], &clients[i]);
886 33756bd2 2021-01-10 op if (fds[i].fd == sock4)
887 33756bd2 2021-01-10 op do_accept(sock4, ctx, fds, clients);
888 33756bd2 2021-01-10 op else if (fds[i].fd == sock6)
889 33756bd2 2021-01-10 op do_accept(sock6, ctx, fds, clients);
891 33756bd2 2021-01-10 op handle(&fds[i], &clients[i]);
897 231bfcdc 2020-12-21 op absolutify_path(const char *path)
899 231bfcdc 2020-12-21 op char *wd, *r;
901 231bfcdc 2020-12-21 op if (*path == '/')
902 231bfcdc 2020-12-21 op return strdup(path);
904 231bfcdc 2020-12-21 op wd = getwd(NULL);
905 231bfcdc 2020-12-21 op if (asprintf(&r, "%s/%s", wd, path) == -1)
906 231bfcdc 2020-12-21 op err(1, "asprintf");
912 15902770 2021-01-15 op yyerror(const char *msg)
914 15902770 2021-01-15 op goterror = 1;
915 15902770 2021-01-15 op fprintf(stderr, "%d: %s\n", yylineno, msg);
919 15902770 2021-01-15 op parse_portno(const char *p)
925 15902770 2021-01-15 op lval = strtol(p, &ep, 10);
926 15902770 2021-01-15 op if (p[0] == '\0' || *ep != '\0')
927 15902770 2021-01-15 op errx(1, "not a number: %s", p);
928 15902770 2021-01-15 op if (lval < 0 || lval > UINT16_MAX)
929 15902770 2021-01-15 op errx(1, "port number out of range for domain %s: %ld", p, lval);
934 15902770 2021-01-15 op parse_conf(const char *path)
936 15902770 2021-01-15 op if ((yyin = fopen(path, "r")) == NULL)
937 15902770 2021-01-15 op err(1, "cannot open config %s", path);
939 15902770 2021-01-15 op fclose(yyin);
941 15902770 2021-01-15 op if (goterror)
946 15902770 2021-01-15 op load_vhosts(struct tls_config *tlsconf)
948 15902770 2021-01-15 op struct vhost *h;
950 15902770 2021-01-15 op /* we need to set something, then we can add how many key we want */
951 15902770 2021-01-15 op if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
952 15902770 2021-01-15 op errx(1, "tls_config_set_keypair_file failed");
954 15902770 2021-01-15 op for (h = hosts; h->domain != NULL; ++h) {
955 15902770 2021-01-15 op if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
956 15902770 2021-01-15 op errx(1, "failed to load the keypair (%s, %s)",
957 15902770 2021-01-15 op h->cert, h->key);
959 15902770 2021-01-15 op if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
960 15902770 2021-01-15 op err(1, "open %s for domain %s", h->dir, h->domain);
964 15902770 2021-01-15 op /* we can augment this function to handle also capsicum and seccomp eventually */
968 15902770 2021-01-15 op struct vhost *h;
969 15902770 2021-01-15 op int has_cgi = 0;
971 15902770 2021-01-15 op for (h = hosts; h->domain != NULL; ++h) {
972 15902770 2021-01-15 op if (unveil(h->dir, "rx") == -1)
973 15902770 2021-01-15 op err(1, "unveil %s for domain %s", h->dir, h->domain);
975 15902770 2021-01-15 op if (h->cgi != NULL)
979 15902770 2021-01-15 op if (pledge("stdio rpath inet proc exec", NULL) == -1)
980 15902770 2021-01-15 op err(1, "pledge");
982 15902770 2021-01-15 op /* drop proc and exec if cgi isn't enabled */
983 15902770 2021-01-15 op if (!has_cgi)
984 15902770 2021-01-15 op if (pledge("stdio rpath inet", NULL) == -1)
985 15902770 2021-01-15 op err(1, "pledge");
989 3e4749f7 2020-10-02 op usage(const char *me)
991 3e4749f7 2020-10-02 op fprintf(stderr,
992 15902770 2021-01-15 op "USAGE: %s [-n] [-c config] | [-6fh] [-C cert] [-d root] [-K key] "
993 15902770 2021-01-15 op "[-p port] [-x cgi-bin]\n",
998 3e4749f7 2020-10-02 op main(int argc, char **argv)
1000 3e4749f7 2020-10-02 op struct tls *ctx = NULL;
1001 15902770 2021-01-15 op struct tls_config *tlsconf;
1002 15902770 2021-01-15 op int sock4, sock6, ch;
1003 15902770 2021-01-15 op const char *config_path = NULL;
1005 15902770 2021-01-15 op int conftest = 0, has_cgi = 0;
1007 15902770 2021-01-15 op bzero(hosts, sizeof(hosts));
1008 15902770 2021-01-15 op for (i = 0; i < HOSTSLEN; ++i)
1009 15902770 2021-01-15 op hosts[i].dirfd = -1;
1011 15902770 2021-01-15 op conf.foreground = 1;
1012 15902770 2021-01-15 op conf.port = 1965;
1013 15902770 2021-01-15 op conf.ipv6 = 0;
1015 15902770 2021-01-15 op connected_clients = 0;
1017 15902770 2021-01-15 op while ((ch = getopt(argc, argv, "6C:c:d:fhK:np:x:")) != -1) {
1018 3e4749f7 2020-10-02 op switch (ch) {
1020 15902770 2021-01-15 op conf.ipv6 = 1;
1024 15902770 2021-01-15 op hosts[0].cert = optarg;
1028 15902770 2021-01-15 op config_path = optarg;
1032 15902770 2021-01-15 op free((char*)hosts[0].dir);
1033 15902770 2021-01-15 op if ((hosts[0].dir = absolutify_path(optarg)) == NULL)
1034 231bfcdc 2020-12-21 op err(1, "absolutify_path");
1038 15902770 2021-01-15 op conf.foreground = 1;
1042 3e4749f7 2020-10-02 op usage(*argv);
1046 15902770 2021-01-15 op hosts[0].key = optarg;
1050 15902770 2021-01-15 op conftest = 1;
1054 15902770 2021-01-15 op conf.port = parse_portno(optarg);
1057 15902770 2021-01-15 op /* drop the starting / (if any) */
1058 15902770 2021-01-15 op if (*optarg == '/')
1060 15902770 2021-01-15 op hosts[0].cgi = optarg;
1064 3e4749f7 2020-10-02 op usage(*argv);
1069 15902770 2021-01-15 op if (config_path != NULL) {
1070 15902770 2021-01-15 op if (hosts[0].cert != NULL || hosts[0].key != NULL ||
1071 15902770 2021-01-15 op hosts[0].dir != NULL)
1072 15902770 2021-01-15 op errx(1, "can't specify options in conf mode");
1073 15902770 2021-01-15 op parse_conf(config_path);
1075 15902770 2021-01-15 op if (hosts[0].cert == NULL || hosts[0].key == NULL ||
1076 15902770 2021-01-15 op hosts[0].dir == NULL)
1077 15902770 2021-01-15 op errx(1, "missing cert, key or root directory to serve");
1078 15902770 2021-01-15 op hosts[0].domain = "*";
1081 15902770 2021-01-15 op if (conftest)
1082 15902770 2021-01-15 op errx(0, "config OK");
1084 4a28dd01 2020-12-28 op signal(SIGPIPE, SIG_IGN);
1085 4a28dd01 2020-12-28 op signal(SIGCHLD, SIG_IGN);
1087 4a28dd01 2020-12-28 op #ifdef SIGINFO
1088 4a28dd01 2020-12-28 op signal(SIGINFO, sig_handler);
1090 4a28dd01 2020-12-28 op signal(SIGUSR2, sig_handler);
1092 15902770 2021-01-15 op if (!conf.foreground)
1093 4a28dd01 2020-12-28 op signal(SIGHUP, SIG_IGN);
1095 15902770 2021-01-15 op if ((tlsconf = tls_config_new()) == NULL)
1096 3e4749f7 2020-10-02 op err(1, "tls_config_new");
1098 677afbd3 2020-12-02 op /* optionally accept client certs, but don't try to verify them */
1099 15902770 2021-01-15 op tls_config_verify_client_optional(tlsconf);
1100 15902770 2021-01-15 op tls_config_insecure_noverifycert(tlsconf);
1102 15902770 2021-01-15 op if (tls_config_set_protocols(tlsconf,
1103 0d8ca45a 2020-10-03 op TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3) == -1)
1104 0d8ca45a 2020-10-03 op err(1, "tls_config_set_protocols");
1106 15902770 2021-01-15 op load_vhosts(tlsconf);
1108 3e4749f7 2020-10-02 op if ((ctx = tls_server()) == NULL)
1109 3e4749f7 2020-10-02 op err(1, "tls_server");
1111 15902770 2021-01-15 op if (tls_configure(ctx, tlsconf) == -1)
1112 3e4749f7 2020-10-02 op errx(1, "tls_configure: %s", tls_error(ctx));
1114 15902770 2021-01-15 op sock4 = make_socket(conf.port, AF_INET);
1115 15902770 2021-01-15 op if (conf.ipv6)
1116 15902770 2021-01-15 op sock6 = make_socket(conf.port, AF_INET6);
1121 15902770 2021-01-15 op if (!conf.foreground && daemon(0, 1) == -1)
1126 33756bd2 2021-01-10 op loop(ctx, sock4, sock6);
1128 33756bd2 2021-01-10 op close(sock4);
1129 33756bd2 2021-01-10 op close(sock6);
1130 3e4749f7 2020-10-02 op tls_free(ctx);
1131 15902770 2021-01-15 op tls_config_free(tlsconf);