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 3e4749f7 2020-10-02 op #include <err.h>
18 592fd624 2020-10-07 op #include <errno.h>
20 3e4749f7 2020-10-02 op #include <fcntl.h>
21 231bfcdc 2020-12-21 op #include <limits.h>
22 709d6e5e 2021-01-10 op #include <netdb.h>
23 0cf902af 2020-11-03 op #include <signal.h>
24 80bbcad5 2021-01-10 op #include <stdarg.h>
25 3e4749f7 2020-10-02 op #include <string.h>
27 488f059a 2020-12-24 op #include "gmid.h"
29 15902770 2021-01-15 op struct vhost hosts[HOSTSLEN];
35 15902770 2021-01-15 op struct conf conf;
37 33d32d1f 2020-12-25 op struct etm { /* file extension to mime */
38 33d32d1f 2020-12-25 op const char *mime;
39 33d32d1f 2020-12-25 op const char *ext;
40 33d32d1f 2020-12-25 op } filetypes[] = {
41 33d32d1f 2020-12-25 op {"application/pdf", "pdf"},
43 33d32d1f 2020-12-25 op {"image/gif", "gif"},
44 33d32d1f 2020-12-25 op {"image/jpeg", "jpg"},
45 33d32d1f 2020-12-25 op {"image/jpeg", "jpeg"},
46 33d32d1f 2020-12-25 op {"image/png", "png"},
47 33d32d1f 2020-12-25 op {"image/svg+xml", "svg"},
49 33d32d1f 2020-12-25 op {"text/gemini", "gemini"},
50 33d32d1f 2020-12-25 op {"text/gemini", "gmi"},
51 33d32d1f 2020-12-25 op {"text/markdown", "markdown"},
52 33d32d1f 2020-12-25 op {"text/markdown", "md"},
53 33d32d1f 2020-12-25 op {"text/plain", "txt"},
54 33d32d1f 2020-12-25 op {"text/xml", "xml"},
60 80bbcad5 2021-01-10 op fatal(const char *fmt, ...)
64 80bbcad5 2021-01-10 op va_start(ap, fmt);
66 15902770 2021-01-15 op if (conf.foreground) {
67 80bbcad5 2021-01-10 op vfprintf(stderr, fmt, ap);
68 80bbcad5 2021-01-10 op fprintf(stderr, "\n");
70 80bbcad5 2021-01-10 op vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
77 709d6e5e 2021-01-10 op logs(int priority, struct client *c,
78 709d6e5e 2021-01-10 op const char *fmt, ...)
80 709d6e5e 2021-01-10 op char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
81 709d6e5e 2021-01-10 op char *fmted, *s;
86 80bbcad5 2021-01-10 op va_start(ap, fmt);
88 dafb57b8 2021-01-15 op if (c == NULL) {
89 dafb57b8 2021-01-15 op strncpy(hbuf, "<internal>", sizeof(hbuf));
90 dafb57b8 2021-01-15 op sbuf[0] = '\0';
92 dafb57b8 2021-01-15 op len = sizeof(c->addr);
93 dafb57b8 2021-01-15 op ec = getnameinfo((struct sockaddr*)&c->addr, len,
94 f7b816dc 2021-01-15 op hbuf, sizeof(hbuf),
95 f7b816dc 2021-01-15 op sbuf, sizeof(sbuf),
96 f7b816dc 2021-01-15 op NI_NUMERICHOST | NI_NUMERICSERV);
98 dafb57b8 2021-01-15 op fatal("getnameinfo: %s", gai_strerror(ec));
101 709d6e5e 2021-01-10 op if (vasprintf(&fmted, fmt, ap) == -1)
102 709d6e5e 2021-01-10 op fatal("vasprintf: %s", strerror(errno));
104 15902770 2021-01-15 op if (conf.foreground)
105 709d6e5e 2021-01-10 op fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
107 709d6e5e 2021-01-10 op if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
108 709d6e5e 2021-01-10 op fatal("asprintf: %s", strerror(errno));
109 709d6e5e 2021-01-10 op syslog(priority | LOG_DAEMON, "%s", s);
119 4a28dd01 2020-12-28 op sig_handler(int sig)
125 72f653b6 2020-11-06 op starts_with(const char *str, const char *prefix)
129 72f653b6 2020-11-06 op for (i = 0; prefix[i] != '\0'; ++i)
130 72f653b6 2020-11-06 op if (str[i] != prefix[i])
136 f28f9311 2020-10-14 op filesize(int fd)
140 f28f9311 2020-10-14 op if ((len = lseek(fd, 0, SEEK_END)) == -1)
142 f28f9311 2020-10-14 op if (lseek(fd, 0, SEEK_SET) == -1)
148 cc68fe70 2020-10-07 op path_ext(const char *path)
150 cc68fe70 2020-10-07 op const char *end;
152 f7b816dc 2021-01-15 op end = path + strlen(path)-1;
153 cc68fe70 2020-10-07 op for (; end != path; --end) {
154 cc68fe70 2020-10-07 op if (*end == '.')
155 cc68fe70 2020-10-07 op return end+1;
156 cc68fe70 2020-10-07 op if (*end == '/')
164 cc68fe70 2020-10-07 op mime(const char *path)
166 cc68fe70 2020-10-07 op const char *ext, *def = "application/octet-stream";
167 cc68fe70 2020-10-07 op struct etm *t;
169 cc68fe70 2020-10-07 op if ((ext = path_ext(path)) == NULL)
172 cc68fe70 2020-10-07 op for (t = filetypes; t->mime != NULL; ++t)
173 cc68fe70 2020-10-07 op if (!strcmp(ext, t->ext))
174 cc68fe70 2020-10-07 op return t->mime;
180 d3a08f4d 2021-01-17 op absolutify_path(const char *path)
182 d3a08f4d 2021-01-17 op char *wd, *r;
184 d3a08f4d 2021-01-17 op if (*path == '/')
185 d3a08f4d 2021-01-17 op return strdup(path);
187 d3a08f4d 2021-01-17 op wd = getcwd(NULL, 0);
188 d3a08f4d 2021-01-17 op if (asprintf(&r, "%s/%s", wd, path) == -1)
189 d3a08f4d 2021-01-17 op err(1, "asprintf");
195 d3a08f4d 2021-01-17 op yyerror(const char *msg)
197 d3a08f4d 2021-01-17 op goterror = 1;
198 d3a08f4d 2021-01-17 op fprintf(stderr, "%d: %s\n", yylineno, msg);
202 d3a08f4d 2021-01-17 op parse_portno(const char *p)
208 d3a08f4d 2021-01-17 op lval = strtol(p, &ep, 10);
209 d3a08f4d 2021-01-17 op if (p[0] == '\0' || *ep != '\0')
210 d3a08f4d 2021-01-17 op errx(1, "not a number: %s", p);
211 d3a08f4d 2021-01-17 op if (lval < 0 || lval > UINT16_MAX)
212 d3a08f4d 2021-01-17 op errx(1, "port number out of range for domain %s: %ld", p, lval);
217 d3a08f4d 2021-01-17 op parse_conf(const char *path)
219 d3a08f4d 2021-01-17 op if ((yyin = fopen(path, "r")) == NULL)
220 d3a08f4d 2021-01-17 op err(1, "cannot open config %s", path);
222 d3a08f4d 2021-01-17 op fclose(yyin);
224 d3a08f4d 2021-01-17 op if (goterror)
229 d3a08f4d 2021-01-17 op load_vhosts(struct tls_config *tlsconf)
231 15902770 2021-01-15 op struct vhost *h;
233 d3a08f4d 2021-01-17 op /* we need to set something, then we can add how many key we want */
234 d3a08f4d 2021-01-17 op if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
235 d3a08f4d 2021-01-17 op errx(1, "tls_config_set_keypair_file failed");
237 15902770 2021-01-15 op for (h = hosts; h->domain != NULL; ++h) {
238 d3a08f4d 2021-01-17 op if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
239 d3a08f4d 2021-01-17 op errx(1, "failed to load the keypair (%s, %s)",
240 d3a08f4d 2021-01-17 op h->cert, h->key);
242 d3a08f4d 2021-01-17 op if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
243 d3a08f4d 2021-01-17 op err(1, "open %s for domain %s", h->dir, h->domain);
248 9468027b 2020-10-15 op make_socket(int port, int family)
251 9468027b 2020-10-15 op struct sockaddr_in addr4;
252 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
253 9468027b 2020-10-15 op struct sockaddr *addr;
254 9468027b 2020-10-15 op socklen_t len;
256 9468027b 2020-10-15 op switch (family) {
257 9468027b 2020-10-15 op case AF_INET:
258 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
259 9468027b 2020-10-15 op addr4.sin_family = family;
260 9468027b 2020-10-15 op addr4.sin_port = htons(port);
261 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
262 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
263 9468027b 2020-10-15 op len = sizeof(addr4);
266 9468027b 2020-10-15 op case AF_INET6:
267 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
268 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
269 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
270 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
271 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
272 9468027b 2020-10-15 op len = sizeof(addr6);
276 9468027b 2020-10-15 op /* unreachable */
280 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
281 80bbcad5 2021-01-10 op fatal("socket: %s", strerror(errno));
284 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
285 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
288 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
289 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
291 592fd624 2020-10-07 op mark_nonblock(sock);
293 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
294 80bbcad5 2021-01-10 op fatal("bind: %s", strerror(errno));
296 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
297 80bbcad5 2021-01-10 op fatal("listen: %s", strerror(errno));
303 881a9dd9 2021-01-16 op listener_main()
305 881a9dd9 2021-01-16 op int sock4, sock6;
306 881a9dd9 2021-01-16 op struct tls *ctx = NULL;
307 881a9dd9 2021-01-16 op struct tls_config *tlsconf;
309 881a9dd9 2021-01-16 op if ((tlsconf = tls_config_new()) == NULL)
310 881a9dd9 2021-01-16 op err(1, "tls_config_new");
312 881a9dd9 2021-01-16 op /* optionally accept client certs, but don't try to verify them */
313 881a9dd9 2021-01-16 op tls_config_verify_client_optional(tlsconf);
314 881a9dd9 2021-01-16 op tls_config_insecure_noverifycert(tlsconf);
316 881a9dd9 2021-01-16 op if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
317 881a9dd9 2021-01-16 op err(1, "tls_config_set_protocols");
319 881a9dd9 2021-01-16 op if ((ctx = tls_server()) == NULL)
320 881a9dd9 2021-01-16 op errx(1, "tls_server failure");
322 881a9dd9 2021-01-16 op load_vhosts(tlsconf);
324 881a9dd9 2021-01-16 op if (tls_configure(ctx, tlsconf) == -1)
325 881a9dd9 2021-01-16 op errx(1, "tls_configure: %s", tls_error(ctx));
327 881a9dd9 2021-01-16 op if (!conf.foreground && daemon(0, 1) == -1)
330 881a9dd9 2021-01-16 op sock4 = make_socket(conf.port, AF_INET);
332 881a9dd9 2021-01-16 op if (conf.ipv6)
333 881a9dd9 2021-01-16 op sock6 = make_socket(conf.port, AF_INET6);
336 881a9dd9 2021-01-16 op loop(ctx, sock4, sock6);
342 3e4749f7 2020-10-02 op usage(const char *me)
344 3e4749f7 2020-10-02 op fprintf(stderr,
345 15902770 2021-01-15 op "USAGE: %s [-n] [-c config] | [-6fh] [-C cert] [-d root] [-K key] "
346 15902770 2021-01-15 op "[-p port] [-x cgi-bin]\n",
351 3e4749f7 2020-10-02 op main(int argc, char **argv)
353 881a9dd9 2021-01-16 op int ch, p[2];
354 15902770 2021-01-15 op const char *config_path = NULL;
356 dafb57b8 2021-01-15 op int conftest = 0;
358 15902770 2021-01-15 op bzero(hosts, sizeof(hosts));
359 15902770 2021-01-15 op for (i = 0; i < HOSTSLEN; ++i)
360 15902770 2021-01-15 op hosts[i].dirfd = -1;
362 15902770 2021-01-15 op conf.foreground = 1;
363 15902770 2021-01-15 op conf.port = 1965;
364 15902770 2021-01-15 op conf.ipv6 = 0;
365 5bc3c98e 2021-01-15 op conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
367 15902770 2021-01-15 op while ((ch = getopt(argc, argv, "6C:c:d:fhK:np:x:")) != -1) {
368 3e4749f7 2020-10-02 op switch (ch) {
370 15902770 2021-01-15 op conf.ipv6 = 1;
374 15902770 2021-01-15 op hosts[0].cert = optarg;
378 15902770 2021-01-15 op config_path = optarg;
382 15902770 2021-01-15 op free((char*)hosts[0].dir);
383 15902770 2021-01-15 op if ((hosts[0].dir = absolutify_path(optarg)) == NULL)
384 231bfcdc 2020-12-21 op err(1, "absolutify_path");
388 15902770 2021-01-15 op conf.foreground = 1;
392 3e4749f7 2020-10-02 op usage(*argv);
396 15902770 2021-01-15 op hosts[0].key = optarg;
400 15902770 2021-01-15 op conftest = 1;
404 15902770 2021-01-15 op conf.port = parse_portno(optarg);
408 15902770 2021-01-15 op /* drop the starting / (if any) */
409 15902770 2021-01-15 op if (*optarg == '/')
411 15902770 2021-01-15 op hosts[0].cgi = optarg;
415 3e4749f7 2020-10-02 op usage(*argv);
420 15902770 2021-01-15 op if (config_path != NULL) {
421 15902770 2021-01-15 op if (hosts[0].cert != NULL || hosts[0].key != NULL ||
422 15902770 2021-01-15 op hosts[0].dir != NULL)
423 15902770 2021-01-15 op errx(1, "can't specify options in conf mode");
424 15902770 2021-01-15 op parse_conf(config_path);
426 15902770 2021-01-15 op if (hosts[0].cert == NULL || hosts[0].key == NULL ||
427 15902770 2021-01-15 op hosts[0].dir == NULL)
428 15902770 2021-01-15 op errx(1, "missing cert, key or root directory to serve");
429 15902770 2021-01-15 op hosts[0].domain = "*";
432 15902770 2021-01-15 op if (conftest)
433 15902770 2021-01-15 op errx(0, "config OK");
435 4a28dd01 2020-12-28 op signal(SIGPIPE, SIG_IGN);
436 4a28dd01 2020-12-28 op signal(SIGCHLD, SIG_IGN);
438 4a28dd01 2020-12-28 op #ifdef SIGINFO
439 4a28dd01 2020-12-28 op signal(SIGINFO, sig_handler);
441 4a28dd01 2020-12-28 op signal(SIGUSR2, sig_handler);
443 0170ba02 2021-01-17 op if (!conf.foreground) {
444 4a28dd01 2020-12-28 op signal(SIGHUP, SIG_IGN);
445 0170ba02 2021-01-17 op if (daemon(1, 1) == -1)
446 0170ba02 2021-01-17 op err(1, "daemon");
449 881a9dd9 2021-01-16 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
450 881a9dd9 2021-01-16 op fatal("socketpair: %s", strerror(errno));
452 881a9dd9 2021-01-16 op switch (fork()) {
454 881a9dd9 2021-01-16 op fatal("fork: %s", strerror(errno));
456 881a9dd9 2021-01-16 op case 0: /* child */
459 881a9dd9 2021-01-16 op listener_main();
462 881a9dd9 2021-01-16 op default: /* parent */
464 881a9dd9 2021-01-16 op return executor_main(p[0]);