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 15902770 2021-01-15 op struct vhost hosts[HOSTSLEN];
34 120381c9 2020-11-06 op int connected_clients;
39 15902770 2021-01-15 op struct conf conf;
41 33d32d1f 2020-12-25 op struct etm { /* file extension to mime */
42 33d32d1f 2020-12-25 op const char *mime;
43 33d32d1f 2020-12-25 op const char *ext;
44 33d32d1f 2020-12-25 op } filetypes[] = {
45 33d32d1f 2020-12-25 op {"application/pdf", "pdf"},
47 33d32d1f 2020-12-25 op {"image/gif", "gif"},
48 33d32d1f 2020-12-25 op {"image/jpeg", "jpg"},
49 33d32d1f 2020-12-25 op {"image/jpeg", "jpeg"},
50 33d32d1f 2020-12-25 op {"image/png", "png"},
51 33d32d1f 2020-12-25 op {"image/svg+xml", "svg"},
53 33d32d1f 2020-12-25 op {"text/gemini", "gemini"},
54 33d32d1f 2020-12-25 op {"text/gemini", "gmi"},
55 33d32d1f 2020-12-25 op {"text/markdown", "markdown"},
56 33d32d1f 2020-12-25 op {"text/markdown", "md"},
57 33d32d1f 2020-12-25 op {"text/plain", "txt"},
58 33d32d1f 2020-12-25 op {"text/xml", "xml"},
63 80bbcad5 2021-01-10 op __attribute__ ((format (printf, 1, 2)))
64 0d7a38c4 2021-01-11 op __attribute__ ((__noreturn__))
65 0d7a38c4 2021-01-11 op static inline void
66 80bbcad5 2021-01-10 op fatal(const char *fmt, ...)
70 80bbcad5 2021-01-10 op va_start(ap, fmt);
72 15902770 2021-01-15 op if (conf.foreground) {
73 80bbcad5 2021-01-10 op vfprintf(stderr, fmt, ap);
74 80bbcad5 2021-01-10 op fprintf(stderr, "\n");
76 80bbcad5 2021-01-10 op vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
83 709d6e5e 2021-01-10 op logs(int priority, struct client *c,
84 709d6e5e 2021-01-10 op const char *fmt, ...)
86 709d6e5e 2021-01-10 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
87 709d6e5e 2021-01-10 op char *fmted, *s;
92 80bbcad5 2021-01-10 op va_start(ap, fmt);
94 dafb57b8 2021-01-15 op if (c == NULL) {
95 dafb57b8 2021-01-15 op strncpy(hbuf, "<internal>", sizeof(hbuf));
96 dafb57b8 2021-01-15 op sbuf[0] = '\0';
98 dafb57b8 2021-01-15 op len = sizeof(c->addr);
99 dafb57b8 2021-01-15 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
100 f7b816dc 2021-01-15 op hbuf, sizeof(hbuf),
101 f7b816dc 2021-01-15 op sbuf, sizeof(sbuf),
102 f7b816dc 2021-01-15 op NI_NUMERICHOST | NI_NUMERICSERV);
104 dafb57b8 2021-01-15 op fatal("getnameinfo: %s", gai_strerror(ec));
107 709d6e5e 2021-01-10 op if (vasprintf(&fmted, fmt, ap) == -1)
108 709d6e5e 2021-01-10 op fatal("vasprintf: %s", strerror(errno));
110 15902770 2021-01-15 op if (conf.foreground)
111 709d6e5e 2021-01-10 op fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
113 709d6e5e 2021-01-10 op if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
114 709d6e5e 2021-01-10 op fatal("asprintf: %s", strerror(errno));
115 709d6e5e 2021-01-10 op syslog(priority | LOG_DAEMON, "%s", s);
125 4a28dd01 2020-12-28 op sig_handler(int sig)
131 72f653b6 2020-11-06 op starts_with(const char *str, const char *prefix)
135 72f653b6 2020-11-06 op for (i = 0; prefix[i] != '\0'; ++i)
136 72f653b6 2020-11-06 op if (str[i] != prefix[i])
142 592fd624 2020-10-07 op start_reply(struct pollfd *pfd, struct client *client, int code, const char *reason)
144 f7b816dc 2021-01-15 op char buf[1030]; /* status + ' ' + max reply len + \r\n\0 */
147 592fd624 2020-10-07 op client->code = code;
148 592fd624 2020-10-07 op client->meta = reason;
149 592fd624 2020-10-07 op client->state = S_INITIALIZING;
151 3e4749f7 2020-10-02 op len = snprintf(buf, sizeof(buf), "%d %s\r\n", code, reason);
152 3e4749f7 2020-10-02 op assert(len < (int)sizeof(buf));
154 f7b816dc 2021-01-15 op switch (tls_write(client->ctx, buf, len)) {
155 f7b816dc 2021-01-15 op case TLS_WANT_POLLIN:
156 592fd624 2020-10-07 op pfd->events = POLLIN;
158 f7b816dc 2021-01-15 op case TLS_WANT_POLLOUT:
159 592fd624 2020-10-07 op pfd->events = POLLOUT;
167 f28f9311 2020-10-14 op filesize(int fd)
171 f28f9311 2020-10-14 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
173 f28f9311 2020-10-14 op if (lseek(fd, 0, SEEK_SET) == -1)
179 cc68fe70 2020-10-07 op path_ext(const char *path)
181 cc68fe70 2020-10-07 op const char *end;
183 f7b816dc 2021-01-15 op end = path + strlen(path)-1;
184 cc68fe70 2020-10-07 op for (; end != path; --end) {
185 cc68fe70 2020-10-07 op if (*end == '.')
186 cc68fe70 2020-10-07 op return end+1;
187 cc68fe70 2020-10-07 op if (*end == '/')
195 cc68fe70 2020-10-07 op mime(const char *path)
197 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
198 cc68fe70 2020-10-07 op struct etm *t;
200 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
203 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
204 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
205 cc68fe70 2020-10-07 op return t->mime;
211 d7802bb4 2020-12-02 op check_path(struct client *c, const char *path, int *fd)
213 72342dc9 2020-11-06 op struct stat sb;
215 f28f9311 2020-10-14 op assert(path != NULL);
216 15902770 2021-01-15 op if ((*fd = openat(c->host->dirfd, *path ? path : ".",
217 f7b816dc 2021-01-15 op O_RDONLY | O_NOFOLLOW | O_CLOEXEC)) == -1) {
218 a5d310bc 2020-11-10 op return FILE_MISSING;
221 a5d310bc 2020-11-10 op if (fstat(*fd, &sb) == -1) {
222 d7802bb4 2020-12-02 op LOGN(c, "failed stat for %s: %s", path, strerror(errno));
223 a5d310bc 2020-11-10 op return FILE_MISSING;
226 a5d310bc 2020-11-10 op if (S_ISDIR(sb.st_mode))
227 a5d310bc 2020-11-10 op return FILE_DIRECTORY;
229 a5d310bc 2020-11-10 op if (sb.st_mode & S_IXUSR)
230 a5d310bc 2020-11-10 op return FILE_EXECUTABLE;
232 a5d310bc 2020-11-10 op return FILE_EXISTS;
236 33d32d1f 2020-12-25 op open_file(char *fpath, char *query, struct pollfd *fds, struct client *c)
238 d7802bb4 2020-12-02 op switch (check_path(c, fpath, &c->fd)) {
239 a5d310bc 2020-11-10 op case FILE_EXECUTABLE:
240 15902770 2021-01-15 op if (c->host->cgi != NULL && starts_with(fpath, c->host->cgi))
241 a5d310bc 2020-11-10 op return start_cgi(fpath, "", query, fds, c);
243 a5d310bc 2020-11-10 op /* fallthrough */
245 a5d310bc 2020-11-10 op case FILE_EXISTS:
246 a5d310bc 2020-11-10 op if ((c->len = filesize(c->fd)) == -1) {
247 d7802bb4 2020-12-02 op LOGE(c, "failed to get file size for %s", fpath);
248 a5d310bc 2020-11-10 op goodbye(fds, c);
252 a5d310bc 2020-11-10 op if ((c->buf = mmap(NULL, c->len, PROT_READ, MAP_PRIVATE,
253 a5d310bc 2020-11-10 op c->fd, 0)) == MAP_FAILED) {
254 a5d310bc 2020-11-10 op warn("mmap: %s", fpath);
255 a5d310bc 2020-11-10 op goodbye(fds, c);
258 a5d310bc 2020-11-10 op c->i = c->buf;
259 a5d310bc 2020-11-10 op return start_reply(fds, c, SUCCESS, mime(fpath));
261 a5d310bc 2020-11-10 op case FILE_DIRECTORY:
262 d7802bb4 2020-12-02 op LOGD(c, "%s is a directory, trying %s/index.gmi", fpath, fpath);
263 f28f9311 2020-10-14 op close(c->fd);
265 f28f9311 2020-10-14 op send_dir(fpath, fds, c);
268 a5d310bc 2020-11-10 op case FILE_MISSING:
269 15902770 2021-01-15 op if (c->host->cgi != NULL && starts_with(fpath, c->host->cgi))
270 a5d310bc 2020-11-10 op return check_for_cgi(fpath, query, fds, c);
272 a5d310bc 2020-11-10 op if (!start_reply(fds, c, NOT_FOUND, "not found"))
274 f28f9311 2020-10-14 op goodbye(fds, c);
278 a5d310bc 2020-11-10 op /* unreachable */
284 75d233f0 2020-11-06 op send_file(char *path, char *query, struct pollfd *fds, struct client *c)
286 f28f9311 2020-10-14 op ssize_t ret, len;
288 f28f9311 2020-10-14 op if (c->fd == -1) {
289 75d233f0 2020-11-06 op if (!open_file(path, query, fds, c))
291 f28f9311 2020-10-14 op c->state = S_SENDING;
294 f28f9311 2020-10-14 op len = (c->buf + c->len) - c->i;
296 f28f9311 2020-10-14 op while (len > 0) {
297 f28f9311 2020-10-14 op switch (ret = tls_write(c->ctx, c->i, len)) {
299 d7802bb4 2020-12-02 op LOGE(c, "tls_write: %s", tls_error(c->ctx));
300 f28f9311 2020-10-14 op goodbye(fds, c);
303 f28f9311 2020-10-14 op case TLS_WANT_POLLIN:
304 f28f9311 2020-10-14 op fds->events = POLLIN;
307 f28f9311 2020-10-14 op case TLS_WANT_POLLOUT:
308 f28f9311 2020-10-14 op fds->events = POLLOUT;
318 f28f9311 2020-10-14 op goodbye(fds, c);
322 592fd624 2020-10-07 op send_dir(char *path, struct pollfd *fds, struct client *client)
324 3e4749f7 2020-10-02 op char fpath[PATHBUF];
327 3e4749f7 2020-10-02 op bzero(fpath, PATHBUF);
329 9c56b0a7 2020-10-14 op if (path[0] != '.')
330 3e4749f7 2020-10-02 op fpath[0] = '.';
332 3e4749f7 2020-10-02 op /* this cannot fail since sizeof(fpath) > maxlen of path */
333 3e4749f7 2020-10-02 op strlcat(fpath, path, PATHBUF);
334 3e4749f7 2020-10-02 op len = strlen(fpath);
336 3e4749f7 2020-10-02 op /* add a trailing / in case. */
337 3e4749f7 2020-10-02 op if (fpath[len-1] != '/') {
338 3e4749f7 2020-10-02 op fpath[len] = '/';
341 3e4749f7 2020-10-02 op strlcat(fpath, "index.gmi", sizeof(fpath));
343 75d233f0 2020-11-06 op send_file(fpath, NULL, fds, client);
347 9862b637 2021-01-13 op handle_handshake(struct pollfd *fds, struct client *c)
349 15902770 2021-01-15 op struct vhost *h;
350 15902770 2021-01-15 op const char *servname;
352 9862b637 2021-01-13 op switch (tls_handshake(c->ctx)) {
353 9862b637 2021-01-13 op case 0: /* success */
354 9862b637 2021-01-13 op case -1: /* already handshaked */
356 9862b637 2021-01-13 op case TLS_WANT_POLLIN:
357 9862b637 2021-01-13 op fds->events = POLLIN;
359 9862b637 2021-01-13 op case TLS_WANT_POLLOUT:
360 9862b637 2021-01-13 op fds->events = POLLOUT;
363 9862b637 2021-01-13 op /* unreachable */
367 15902770 2021-01-15 op servname = tls_conn_servername(c->ctx);
368 15902770 2021-01-15 op if (servname == NULL)
369 f7b816dc 2021-01-15 op goto hostnotfound;
371 15902770 2021-01-15 op for (h = hosts; h->domain != NULL; ++h) {
372 15902770 2021-01-15 op if (!strcmp(h->domain, servname) || !strcmp(h->domain, "*"))
376 15902770 2021-01-15 op if (h->domain != NULL) {
377 15902770 2021-01-15 op c->state = S_OPEN;
379 15902770 2021-01-15 op handle_open_conn(fds, c);
383 f7b816dc 2021-01-15 op hostnotfound:
384 15902770 2021-01-15 op /* XXX: check the correct response */
385 15902770 2021-01-15 op if (!start_reply(fds, c, BAD_REQUEST, "Wrong host or missing SNI"))
387 15902770 2021-01-15 op goodbye(fds, c);
391 9862b637 2021-01-13 op handle_open_conn(struct pollfd *fds, struct client *c)
393 592fd624 2020-10-07 op char buf[GEMINI_URL_LEN];
394 9862b637 2021-01-13 op const char *parse_err = "invalid request";
395 3c1cf9d0 2021-01-11 op struct iri iri;
397 9862b637 2021-01-13 op bzero(buf, sizeof(buf));
399 9862b637 2021-01-13 op switch (tls_read(c->ctx, buf, sizeof(buf)-1)) {
401 9862b637 2021-01-13 op LOGE(c, "tls_read: %s", tls_error(c->ctx));
402 9862b637 2021-01-13 op goodbye(fds, c);
405 9862b637 2021-01-13 op case TLS_WANT_POLLIN:
406 9862b637 2021-01-13 op fds->events = POLLIN;
409 9862b637 2021-01-13 op case TLS_WANT_POLLOUT:
410 9862b637 2021-01-13 op fds->events = POLLOUT;
414 9862b637 2021-01-13 op if (!trim_req_iri(buf) || !parse_iri(buf, &iri, &parse_err)) {
415 9862b637 2021-01-13 op if (!start_reply(fds, c, BAD_REQUEST, parse_err))
417 9862b637 2021-01-13 op goodbye(fds, c);
421 b777bf4b 2021-01-15 op if (strcmp(iri.schema, "gemini") || iri.port_no != conf.port) {
422 9862b637 2021-01-13 op if (!start_reply(fds, c, PROXY_REFUSED, "won't proxy request"))
424 9862b637 2021-01-13 op goodbye(fds, c);
428 9862b637 2021-01-13 op LOGI(c, "GET %s%s%s",
429 f7b816dc 2021-01-15 op *iri.path ? iri.path : "/",
430 f7b816dc 2021-01-15 op *iri.query ? "?" : "",
431 f7b816dc 2021-01-15 op *iri.query ? iri.query : "");
433 9862b637 2021-01-13 op send_file(iri.path, iri.query, fds, c);
437 9862b637 2021-01-13 op handle(struct pollfd *fds, struct client *client)
439 9862b637 2021-01-13 op switch (client->state) {
440 9862b637 2021-01-13 op case S_HANDSHAKE:
441 9862b637 2021-01-13 op handle_handshake(fds, client);
445 9862b637 2021-01-13 op handle_open_conn(fds, client);
448 592fd624 2020-10-07 op case S_INITIALIZING:
449 592fd624 2020-10-07 op if (!start_reply(fds, client, client->code, client->meta))
452 592fd624 2020-10-07 op if (client->code != SUCCESS) {
453 592fd624 2020-10-07 op /* we don't need a body */
454 592fd624 2020-10-07 op goodbye(fds, client);
458 592fd624 2020-10-07 op client->state = S_SENDING;
460 592fd624 2020-10-07 op /* fallthrough */
462 592fd624 2020-10-07 op case S_SENDING:
463 881a9dd9 2021-01-16 op if (client->child)
464 aff8d190 2020-11-06 op handle_cgi(fds, client);
466 aff8d190 2020-11-06 op send_file(NULL, NULL, fds, client);
469 592fd624 2020-10-07 op case S_CLOSING:
470 592fd624 2020-10-07 op goodbye(fds, client);
474 592fd624 2020-10-07 op /* unreachable */
480 592fd624 2020-10-07 op mark_nonblock(int fd)
484 592fd624 2020-10-07 op if ((flags = fcntl(fd, F_GETFL)) == -1)
485 80bbcad5 2021-01-10 op fatal("fcntl(F_GETFL): %s", strerror(errno));
486 592fd624 2020-10-07 op if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
487 80bbcad5 2021-01-10 op fatal("fcntl(F_SETFL): %s", strerror(errno));
491 9468027b 2020-10-15 op make_socket(int port, int family)
494 9468027b 2020-10-15 op struct sockaddr_in addr4;
495 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
496 9468027b 2020-10-15 op struct sockaddr *addr;
497 9468027b 2020-10-15 op socklen_t len;
499 9468027b 2020-10-15 op switch (family) {
500 9468027b 2020-10-15 op case AF_INET:
501 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
502 9468027b 2020-10-15 op addr4.sin_family = family;
503 9468027b 2020-10-15 op addr4.sin_port = htons(port);
504 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
505 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
506 9468027b 2020-10-15 op len = sizeof(addr4);
509 9468027b 2020-10-15 op case AF_INET6:
510 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
511 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
512 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
513 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
514 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
515 9468027b 2020-10-15 op len = sizeof(addr6);
519 9468027b 2020-10-15 op /* unreachable */
523 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
524 80bbcad5 2021-01-10 op fatal("socket: %s", strerror(errno));
527 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
528 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
531 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
532 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
534 592fd624 2020-10-07 op mark_nonblock(sock);
536 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
537 80bbcad5 2021-01-10 op fatal("bind: %s", strerror(errno));
539 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
540 80bbcad5 2021-01-10 op fatal("listen: %s", strerror(errno));
546 592fd624 2020-10-07 op do_accept(int sock, struct tls *ctx, struct pollfd *fds, struct client *clients)
549 709d6e5e 2021-01-10 op struct sockaddr_storage addr;
550 3e4749f7 2020-10-02 op socklen_t len;
552 592fd624 2020-10-07 op len = sizeof(addr);
553 592fd624 2020-10-07 op if ((fd = accept(sock, (struct sockaddr*)&addr, &len)) == -1) {
554 592fd624 2020-10-07 op if (errno == EWOULDBLOCK)
556 80bbcad5 2021-01-10 op fatal("accept: %s", strerror(errno));
559 592fd624 2020-10-07 op mark_nonblock(fd);
561 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
562 592fd624 2020-10-07 op if (fds[i].fd == -1) {
563 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
564 592fd624 2020-10-07 op if (tls_accept_socket(ctx, &clients[i].ctx, fd) == -1)
565 592fd624 2020-10-07 op break; /* goodbye fd! */
567 592fd624 2020-10-07 op fds[i].fd = fd;
568 592fd624 2020-10-07 op fds[i].events = POLLIN;
570 9862b637 2021-01-13 op clients[i].state = S_HANDSHAKE;
571 592fd624 2020-10-07 op clients[i].fd = -1;
572 881a9dd9 2021-01-16 op clients[i].child = 0;
573 881a9dd9 2021-01-16 op clients[i].waiting_on_child = 0;
574 f28f9311 2020-10-14 op clients[i].buf = MAP_FAILED;
575 2c3a40fa 2020-11-06 op clients[i].af = AF_INET;
576 709d6e5e 2021-01-10 op clients[i].addr = addr;
578 120381c9 2020-11-06 op connected_clients++;
587 592fd624 2020-10-07 op goodbye(struct pollfd *pfd, struct client *c)
589 592fd624 2020-10-07 op c->state = S_CLOSING;
591 f7b816dc 2021-01-15 op switch (tls_close(c->ctx)) {
592 f7b816dc 2021-01-15 op case TLS_WANT_POLLIN:
593 592fd624 2020-10-07 op pfd->events = POLLIN;
595 f7b816dc 2021-01-15 op case TLS_WANT_POLLOUT:
596 592fd624 2020-10-07 op pfd->events = POLLOUT;
600 120381c9 2020-11-06 op connected_clients--;
602 592fd624 2020-10-07 op tls_free(c->ctx);
603 592fd624 2020-10-07 op c->ctx = NULL;
605 f28f9311 2020-10-14 op if (c->buf != MAP_FAILED)
606 f28f9311 2020-10-14 op munmap(c->buf, c->len);
608 592fd624 2020-10-07 op if (c->fd != -1)
609 592fd624 2020-10-07 op close(c->fd);
611 592fd624 2020-10-07 op close(pfd->fd);
612 592fd624 2020-10-07 op pfd->fd = -1;
616 33756bd2 2021-01-10 op loop(struct tls *ctx, int sock4, int sock6)
619 592fd624 2020-10-07 op struct client clients[MAX_USERS];
620 592fd624 2020-10-07 op struct pollfd fds[MAX_USERS];
622 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; ++i) {
623 592fd624 2020-10-07 op fds[i].fd = -1;
624 592fd624 2020-10-07 op fds[i].events = POLLIN;
625 592fd624 2020-10-07 op bzero(&clients[i], sizeof(struct client));
628 33756bd2 2021-01-10 op fds[0].fd = sock4;
629 33756bd2 2021-01-10 op fds[1].fd = sock6;
632 16ace04d 2021-01-08 op if (poll(fds, MAX_USERS, INFTIM) == -1) {
633 120381c9 2020-11-06 op if (errno == EINTR) {
634 d7802bb4 2020-12-02 op warnx("connected clients: %d",
635 d7802bb4 2020-12-02 op connected_clients);
638 80bbcad5 2021-01-10 op fatal("poll: %s", strerror(errno));
641 592fd624 2020-10-07 op for (i = 0; i < MAX_USERS; i++) {
642 592fd624 2020-10-07 op if (fds[i].revents == 0)
645 592fd624 2020-10-07 op if (fds[i].revents & (POLLERR|POLLNVAL))
646 80bbcad5 2021-01-10 op fatal("bad fd %d: %s", fds[i].fd,
647 d7802bb4 2020-12-02 op strerror(errno));
649 592fd624 2020-10-07 op if (fds[i].revents & POLLHUP) {
650 aff8d190 2020-11-06 op /* fds[i] may be the fd of the stdin
651 aff8d190 2020-11-06 op * of a cgi script that has exited. */
652 aff8d190 2020-11-06 op if (!clients[i].waiting_on_child) {
653 aff8d190 2020-11-06 op goodbye(&fds[i], &clients[i]);
658 33756bd2 2021-01-10 op if (fds[i].fd == sock4)
659 33756bd2 2021-01-10 op do_accept(sock4, ctx, fds, clients);
660 33756bd2 2021-01-10 op else if (fds[i].fd == sock6)
661 33756bd2 2021-01-10 op do_accept(sock6, ctx, fds, clients);
663 33756bd2 2021-01-10 op handle(&fds[i], &clients[i]);
669 231bfcdc 2020-12-21 op absolutify_path(const char *path)
671 231bfcdc 2020-12-21 op char *wd, *r;
673 231bfcdc 2020-12-21 op if (*path == '/')
674 231bfcdc 2020-12-21 op return strdup(path);
676 d939d0f0 2021-01-17 op wd = getcwd(NULL, 0);
677 231bfcdc 2020-12-21 op if (asprintf(&r, "%s/%s", wd, path) == -1)
678 231bfcdc 2020-12-21 op err(1, "asprintf");
684 15902770 2021-01-15 op yyerror(const char *msg)
686 15902770 2021-01-15 op goterror = 1;
687 15902770 2021-01-15 op fprintf(stderr, "%d: %s\n", yylineno, msg);
691 15902770 2021-01-15 op parse_portno(const char *p)
697 15902770 2021-01-15 op lval = strtol(p, &ep, 10);
698 15902770 2021-01-15 op if (p[0] == '\0' || *ep != '\0')
699 15902770 2021-01-15 op errx(1, "not a number: %s", p);
700 15902770 2021-01-15 op if (lval < 0 || lval > UINT16_MAX)
701 15902770 2021-01-15 op errx(1, "port number out of range for domain %s: %ld", p, lval);
706 15902770 2021-01-15 op parse_conf(const char *path)
708 15902770 2021-01-15 op if ((yyin = fopen(path, "r")) == NULL)
709 15902770 2021-01-15 op err(1, "cannot open config %s", path);
711 15902770 2021-01-15 op fclose(yyin);
713 15902770 2021-01-15 op if (goterror)
718 15902770 2021-01-15 op load_vhosts(struct tls_config *tlsconf)
720 15902770 2021-01-15 op struct vhost *h;
722 15902770 2021-01-15 op /* we need to set something, then we can add how many key we want */
723 15902770 2021-01-15 op if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
724 15902770 2021-01-15 op errx(1, "tls_config_set_keypair_file failed");
726 15902770 2021-01-15 op for (h = hosts; h->domain != NULL; ++h) {
727 15902770 2021-01-15 op if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
728 15902770 2021-01-15 op errx(1, "failed to load the keypair (%s, %s)",
729 15902770 2021-01-15 op h->cert, h->key);
731 15902770 2021-01-15 op if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
732 15902770 2021-01-15 op err(1, "open %s for domain %s", h->dir, h->domain);
737 881a9dd9 2021-01-16 op listener_main()
739 881a9dd9 2021-01-16 op int sock4, sock6;
740 881a9dd9 2021-01-16 op struct tls *ctx = NULL;
741 881a9dd9 2021-01-16 op struct tls_config *tlsconf;
743 881a9dd9 2021-01-16 op if ((tlsconf = tls_config_new()) == NULL)
744 881a9dd9 2021-01-16 op err(1, "tls_config_new");
746 881a9dd9 2021-01-16 op /* optionally accept client certs, but don't try to verify them */
747 881a9dd9 2021-01-16 op tls_config_verify_client_optional(tlsconf);
748 881a9dd9 2021-01-16 op tls_config_insecure_noverifycert(tlsconf);
750 881a9dd9 2021-01-16 op if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
751 881a9dd9 2021-01-16 op err(1, "tls_config_set_protocols");
753 881a9dd9 2021-01-16 op if ((ctx = tls_server()) == NULL)
754 881a9dd9 2021-01-16 op errx(1, "tls_server failure");
756 881a9dd9 2021-01-16 op load_vhosts(tlsconf);
758 881a9dd9 2021-01-16 op if (tls_configure(ctx, tlsconf) == -1)
759 881a9dd9 2021-01-16 op errx(1, "tls_configure: %s", tls_error(ctx));
761 881a9dd9 2021-01-16 op if (!conf.foreground && daemon(0, 1) == -1)
764 881a9dd9 2021-01-16 op sock4 = make_socket(conf.port, AF_INET);
766 881a9dd9 2021-01-16 op if (conf.ipv6)
767 881a9dd9 2021-01-16 op sock6 = make_socket(conf.port, AF_INET6);
770 881a9dd9 2021-01-16 op loop(ctx, sock4, sock6);
776 3e4749f7 2020-10-02 op usage(const char *me)
778 3e4749f7 2020-10-02 op fprintf(stderr,
779 15902770 2021-01-15 op "USAGE: %s [-n] [-c config] | [-6fh] [-C cert] [-d root] [-K key] "
780 15902770 2021-01-15 op "[-p port] [-x cgi-bin]\n",
785 3e4749f7 2020-10-02 op main(int argc, char **argv)
787 881a9dd9 2021-01-16 op int ch, p[2];
788 15902770 2021-01-15 op const char *config_path = NULL;
790 dafb57b8 2021-01-15 op int conftest = 0;
792 15902770 2021-01-15 op bzero(hosts, sizeof(hosts));
793 15902770 2021-01-15 op for (i = 0; i < HOSTSLEN; ++i)
794 15902770 2021-01-15 op hosts[i].dirfd = -1;
796 15902770 2021-01-15 op conf.foreground = 1;
797 15902770 2021-01-15 op conf.port = 1965;
798 15902770 2021-01-15 op conf.ipv6 = 0;
799 5bc3c98e 2021-01-15 op conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
801 15902770 2021-01-15 op connected_clients = 0;
803 15902770 2021-01-15 op while ((ch = getopt(argc, argv, "6C:c:d:fhK:np:x:")) != -1) {
804 3e4749f7 2020-10-02 op switch (ch) {
806 15902770 2021-01-15 op conf.ipv6 = 1;
810 15902770 2021-01-15 op hosts[0].cert = optarg;
814 15902770 2021-01-15 op config_path = optarg;
818 15902770 2021-01-15 op free((char*)hosts[0].dir);
819 15902770 2021-01-15 op if ((hosts[0].dir = absolutify_path(optarg)) == NULL)
820 231bfcdc 2020-12-21 op err(1, "absolutify_path");
824 15902770 2021-01-15 op conf.foreground = 1;
828 3e4749f7 2020-10-02 op usage(*argv);
832 15902770 2021-01-15 op hosts[0].key = optarg;
836 15902770 2021-01-15 op conftest = 1;
840 15902770 2021-01-15 op conf.port = parse_portno(optarg);
844 15902770 2021-01-15 op /* drop the starting / (if any) */
845 15902770 2021-01-15 op if (*optarg == '/')
847 15902770 2021-01-15 op hosts[0].cgi = optarg;
851 3e4749f7 2020-10-02 op usage(*argv);
856 15902770 2021-01-15 op if (config_path != NULL) {
857 15902770 2021-01-15 op if (hosts[0].cert != NULL || hosts[0].key != NULL ||
858 15902770 2021-01-15 op hosts[0].dir != NULL)
859 15902770 2021-01-15 op errx(1, "can't specify options in conf mode");
860 15902770 2021-01-15 op parse_conf(config_path);
862 15902770 2021-01-15 op if (hosts[0].cert == NULL || hosts[0].key == NULL ||
863 15902770 2021-01-15 op hosts[0].dir == NULL)
864 15902770 2021-01-15 op errx(1, "missing cert, key or root directory to serve");
865 15902770 2021-01-15 op hosts[0].domain = "*";
868 15902770 2021-01-15 op if (conftest)
869 15902770 2021-01-15 op errx(0, "config OK");
871 4a28dd01 2020-12-28 op signal(SIGPIPE, SIG_IGN);
872 4a28dd01 2020-12-28 op signal(SIGCHLD, SIG_IGN);
874 4a28dd01 2020-12-28 op #ifdef SIGINFO
875 4a28dd01 2020-12-28 op signal(SIGINFO, sig_handler);
877 4a28dd01 2020-12-28 op signal(SIGUSR2, sig_handler);
879 15902770 2021-01-15 op if (!conf.foreground)
880 4a28dd01 2020-12-28 op signal(SIGHUP, SIG_IGN);
882 881a9dd9 2021-01-16 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
883 881a9dd9 2021-01-16 op fatal("socketpair: %s", strerror(errno));
885 881a9dd9 2021-01-16 op switch (fork()) {
887 881a9dd9 2021-01-16 op fatal("fork: %s", strerror(errno));
889 881a9dd9 2021-01-16 op case 0: /* child */
892 881a9dd9 2021-01-16 op listener_main();
895 881a9dd9 2021-01-16 op default: /* parent */
897 881a9dd9 2021-01-16 op return executor_main(p[0]);