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.
29 struct vhost hosts[HOSTSLEN];
37 struct etm { /* file extension to mime */
41 {"application/pdf", "pdf"},
44 {"image/jpeg", "jpg"},
45 {"image/jpeg", "jpeg"},
47 {"image/svg+xml", "svg"},
49 {"text/gemini", "gemini"},
50 {"text/gemini", "gmi"},
51 {"text/markdown", "markdown"},
52 {"text/markdown", "md"},
53 {"text/plain", "txt"},
60 fatal(const char *fmt, ...)
66 if (conf.foreground) {
67 vfprintf(stderr, fmt, ap);
68 fprintf(stderr, "\n");
70 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
77 logs(int priority, struct client *c,
80 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
89 strncpy(hbuf, "<internal>", sizeof(hbuf));
92 len = sizeof(c->addr);
93 ec = getnameinfo((struct sockaddr*)&c->addr, len,
96 NI_NUMERICHOST | NI_NUMERICSERV);
98 fatal("getnameinfo: %s", gai_strerror(ec));
101 if (vasprintf(&fmted, fmt, ap) == -1)
102 fatal("vasprintf: %s", strerror(errno));
105 fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
107 if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
108 fatal("asprintf: %s", strerror(errno));
109 syslog(priority | LOG_DAEMON, "%s", s);
125 starts_with(const char *str, const char *prefix)
129 for (i = 0; prefix[i] != '\0'; ++i)
130 if (str[i] != prefix[i])
140 if ((len = lseek(fd, 0, SEEK_END)) == -1)
142 if (lseek(fd, 0, SEEK_SET) == -1)
148 path_ext(const char *path)
152 end = path + strlen(path)-1;
153 for (; end != path; --end) {
164 mime(const char *path)
166 const char *ext, *def = "application/octet-stream";
169 if ((ext = path_ext(path)) == NULL)
172 for (t = filetypes; t->mime != NULL; ++t)
173 if (!strcmp(ext, t->ext))
180 absolutify_path(const char *path)
187 wd = getcwd(NULL, 0);
188 if (asprintf(&r, "%s/%s", wd, path) == -1)
195 yyerror(const char *msg)
198 fprintf(stderr, "%d: %s\n", yylineno, msg);
202 parse_portno(const char *p)
208 lval = strtol(p, &ep, 10);
209 if (p[0] == '\0' || *ep != '\0')
210 errx(1, "not a number: %s", p);
211 if (lval < 0 || lval > UINT16_MAX)
212 errx(1, "port number out of range for domain %s: %ld", p, lval);
217 parse_conf(const char *path)
219 if ((yyin = fopen(path, "r")) == NULL)
220 err(1, "cannot open config %s", path);
229 load_vhosts(struct tls_config *tlsconf)
233 /* we need to set something, then we can add how many key we want */
234 if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
235 errx(1, "tls_config_set_keypair_file failed");
237 for (h = hosts; h->domain != NULL; ++h) {
238 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
239 errx(1, "failed to load the keypair (%s, %s)",
242 if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
243 err(1, "open %s for domain %s", h->dir, h->domain);
248 make_socket(int port, int family)
251 struct sockaddr_in addr4;
252 struct sockaddr_in6 addr6;
253 struct sockaddr *addr;
258 bzero(&addr4, sizeof(addr4));
259 addr4.sin_family = family;
260 addr4.sin_port = htons(port);
261 addr4.sin_addr.s_addr = INADDR_ANY;
262 addr = (struct sockaddr*)&addr4;
267 bzero(&addr6, sizeof(addr6));
268 addr6.sin6_family = AF_INET6;
269 addr6.sin6_port = htons(port);
270 addr6.sin6_addr = in6addr_any;
271 addr = (struct sockaddr*)&addr6;
280 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
281 fatal("socket: %s", strerror(errno));
284 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
285 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
288 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
289 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
293 if (bind(sock, addr, len) == -1)
294 fatal("bind: %s", strerror(errno));
296 if (listen(sock, 16) == -1)
297 fatal("listen: %s", strerror(errno));
306 struct tls *ctx = NULL;
307 struct tls_config *tlsconf;
309 if ((tlsconf = tls_config_new()) == NULL)
310 err(1, "tls_config_new");
312 /* optionally accept client certs, but don't try to verify them */
313 tls_config_verify_client_optional(tlsconf);
314 tls_config_insecure_noverifycert(tlsconf);
316 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
317 err(1, "tls_config_set_protocols");
319 if ((ctx = tls_server()) == NULL)
320 errx(1, "tls_server failure");
322 load_vhosts(tlsconf);
324 if (tls_configure(ctx, tlsconf) == -1)
325 errx(1, "tls_configure: %s", tls_error(ctx));
327 if (!conf.foreground && daemon(0, 1) == -1)
330 sock4 = make_socket(conf.port, AF_INET);
333 sock6 = make_socket(conf.port, AF_INET6);
336 loop(ctx, sock4, sock6);
342 usage(const char *me)
345 "USAGE: %s [-n] [-c config] | [-6fh] [-C cert] [-d root] [-K key] "
346 "[-p port] [-x cgi-bin]\n",
351 main(int argc, char **argv)
354 const char *config_path = NULL;
358 bzero(hosts, sizeof(hosts));
359 for (i = 0; i < HOSTSLEN; ++i)
365 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
367 while ((ch = getopt(argc, argv, "6C:c:d:fhK:np:x:")) != -1) {
374 hosts[0].cert = optarg;
378 config_path = optarg;
382 free((char*)hosts[0].dir);
383 if ((hosts[0].dir = absolutify_path(optarg)) == NULL)
384 err(1, "absolutify_path");
396 hosts[0].key = optarg;
404 conf.port = parse_portno(optarg);
408 /* drop the starting / (if any) */
411 hosts[0].cgi = optarg;
420 if (config_path != NULL) {
421 if (hosts[0].cert != NULL || hosts[0].key != NULL ||
422 hosts[0].dir != NULL)
423 errx(1, "can't specify options in conf mode");
424 parse_conf(config_path);
426 if (hosts[0].cert == NULL || hosts[0].key == NULL ||
427 hosts[0].dir == NULL)
428 errx(1, "missing cert, key or root directory to serve");
429 hosts[0].domain = "*";
433 errx(0, "config OK");
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)
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]);