2 * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
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.
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.
27 struct vhost hosts[HOSTSLEN];
35 struct etm { /* file extension to mime */
39 {"application/pdf", "pdf"},
42 {"image/jpeg", "jpg"},
43 {"image/jpeg", "jpeg"},
45 {"image/svg+xml", "svg"},
47 {"text/gemini", "gemini"},
48 {"text/gemini", "gmi"},
49 {"text/markdown", "markdown"},
50 {"text/markdown", "md"},
51 {"text/plain", "txt"},
58 fatal(const char *fmt, ...)
64 if (conf.foreground) {
65 vfprintf(stderr, fmt, ap);
66 fprintf(stderr, "\n");
68 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
75 logs(int priority, struct client *c,
78 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
87 strncpy(hbuf, "<internal>", sizeof(hbuf));
90 len = sizeof(c->addr);
91 ec = getnameinfo((struct sockaddr*)&c->addr, len,
94 NI_NUMERICHOST | NI_NUMERICSERV);
96 fatal("getnameinfo: %s", gai_strerror(ec));
99 if (vasprintf(&fmted, fmt, ap) == -1)
100 fatal("vasprintf: %s", strerror(errno));
103 fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
105 if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
106 fatal("asprintf: %s", strerror(errno));
107 syslog(priority | LOG_DAEMON, "%s", s);
123 starts_with(const char *str, const char *prefix)
127 for (i = 0; prefix[i] != '\0'; ++i)
128 if (str[i] != prefix[i])
138 if ((len = lseek(fd, 0, SEEK_END)) == -1)
140 if (lseek(fd, 0, SEEK_SET) == -1)
146 path_ext(const char *path)
150 end = path + strlen(path)-1;
151 for (; end != path; --end) {
162 mime(const char *path)
164 const char *ext, *def = "application/octet-stream";
167 if ((ext = path_ext(path)) == NULL)
170 for (t = filetypes; t->mime != NULL; ++t)
171 if (!strcmp(ext, t->ext))
178 absolutify_path(const char *path)
185 wd = getcwd(NULL, 0);
186 if (asprintf(&r, "%s/%s", wd, path) == -1)
187 fatal("asprintf: %s", strerror(errno));
193 yyerror(const char *msg)
196 fprintf(stderr, "%d: %s\n", yylineno, msg);
200 parse_portno(const char *p)
206 lval = strtol(p, &ep, 10);
207 if (p[0] == '\0' || *ep != '\0')
208 fatal("not a number: %s", p);
209 if (lval < 0 || lval > UINT16_MAX)
210 fatal("port number out of range for domain %s: %ld", p, lval);
215 parse_conf(const char *path)
217 if ((yyin = fopen(path, "r")) == NULL)
218 fatal("cannot open config %s", path);
227 load_vhosts(struct tls_config *tlsconf)
231 /* we need to set something, then we can add how many key we want */
232 if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
233 fatal("tls_config_set_keypair_file failed");
235 for (h = hosts; h->domain != NULL; ++h) {
236 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
237 fatal("failed to load the keypair (%s, %s)",
240 if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
241 fatal("open %s for domain %s", h->dir, h->domain);
246 make_socket(int port, int family)
249 struct sockaddr_in addr4;
250 struct sockaddr_in6 addr6;
251 struct sockaddr *addr;
256 bzero(&addr4, sizeof(addr4));
257 addr4.sin_family = family;
258 addr4.sin_port = htons(port);
259 addr4.sin_addr.s_addr = INADDR_ANY;
260 addr = (struct sockaddr*)&addr4;
265 bzero(&addr6, sizeof(addr6));
266 addr6.sin6_family = AF_INET6;
267 addr6.sin6_port = htons(port);
268 addr6.sin6_addr = in6addr_any;
269 addr = (struct sockaddr*)&addr6;
278 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
279 fatal("socket: %s", strerror(errno));
282 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
283 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
286 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
287 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
291 if (bind(sock, addr, len) == -1)
292 fatal("bind: %s", strerror(errno));
294 if (listen(sock, 16) == -1)
295 fatal("listen: %s", strerror(errno));
304 struct tls *ctx = NULL;
305 struct tls_config *tlsconf;
307 if ((tlsconf = tls_config_new()) == NULL)
308 fatal("tls_config_new");
310 /* optionally accept client certs, but don't try to verify them */
311 tls_config_verify_client_optional(tlsconf);
312 tls_config_insecure_noverifycert(tlsconf);
314 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
315 fatal("tls_config_set_protocols");
317 if ((ctx = tls_server()) == NULL)
318 fatal("tls_server failure");
320 load_vhosts(tlsconf);
322 if (tls_configure(ctx, tlsconf) == -1)
323 fatal("tls_configure: %s", tls_error(ctx));
325 if (!conf.foreground && daemon(0, 1) == -1)
328 sock4 = make_socket(conf.port, AF_INET);
331 sock6 = make_socket(conf.port, AF_INET6);
334 loop(ctx, sock4, sock6);
340 usage(const char *me)
343 "USAGE: %s [-n] [-c config] | [-6fh] [-C cert] [-d root] [-K key] "
344 "[-p port] [-x cgi-bin]\n",
349 main(int argc, char **argv)
352 const char *config_path = NULL;
356 bzero(hosts, sizeof(hosts));
357 for (i = 0; i < HOSTSLEN; ++i)
363 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
365 while ((ch = getopt(argc, argv, "6C:c:d:fhK:np:x:")) != -1) {
372 hosts[0].cert = optarg;
376 config_path = optarg;
380 free((char*)hosts[0].dir);
381 if ((hosts[0].dir = absolutify_path(optarg)) == NULL)
382 fatal("absolutify_path");
394 hosts[0].key = optarg;
402 conf.port = parse_portno(optarg);
406 /* drop the starting / (if any) */
409 hosts[0].cgi = optarg;
418 if (config_path != NULL) {
419 if (hosts[0].cert != NULL || hosts[0].key != NULL ||
420 hosts[0].dir != NULL)
421 fatal("can't specify options in conf mode");
422 parse_conf(config_path);
424 if (hosts[0].cert == NULL || hosts[0].key == NULL ||
425 hosts[0].dir == NULL)
426 fatal("missing cert, key or root directory to serve");
427 hosts[0].domain = "*";
435 signal(SIGPIPE, SIG_IGN);
436 signal(SIGCHLD, SIG_IGN);
439 signal(SIGINFO, sig_handler);
441 signal(SIGUSR2, sig_handler);
443 if (!conf.foreground) {
444 signal(SIGHUP, SIG_IGN);
445 if (daemon(1, 1) == -1)
446 fatal("daemon: %s", strerror(errno));
449 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
450 fatal("socketpair: %s", strerror(errno));
454 fatal("fork: %s", strerror(errno));
462 default: /* parent */
464 return executor_main(p[0]);