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];
36 fatal(const char *fmt, ...)
42 if (conf.foreground) {
43 vfprintf(stderr, fmt, ap);
44 fprintf(stderr, "\n");
46 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
53 logs(int priority, struct client *c,
56 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
65 strncpy(hbuf, "<internal>", sizeof(hbuf));
68 len = sizeof(c->addr);
69 ec = getnameinfo((struct sockaddr*)&c->addr, len,
72 NI_NUMERICHOST | NI_NUMERICSERV);
74 fatal("getnameinfo: %s", gai_strerror(ec));
77 if (vasprintf(&fmted, fmt, ap) == -1)
78 fatal("vasprintf: %s", strerror(errno));
81 fprintf(stderr, "%s:%s %s\n", hbuf, sbuf, fmted);
83 if (asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted) == -1)
84 fatal("asprintf: %s", strerror(errno));
85 syslog(priority | LOG_DAEMON, "%s", s);
101 starts_with(const char *str, const char *prefix)
105 for (i = 0; prefix[i] != '\0'; ++i)
106 if (str[i] != prefix[i])
116 if ((len = lseek(fd, 0, SEEK_END)) == -1)
118 if (lseek(fd, 0, SEEK_SET) == -1)
124 absolutify_path(const char *path)
131 wd = getcwd(NULL, 0);
132 if (asprintf(&r, "%s/%s", wd, path) == -1)
133 fatal("asprintf: %s", strerror(errno));
139 yyerror(const char *msg)
142 fprintf(stderr, "%d: %s\n", yylineno, msg);
146 parse_portno(const char *p)
152 lval = strtol(p, &ep, 10);
153 if (p[0] == '\0' || *ep != '\0')
154 fatal("not a number: %s", p);
155 if (lval < 0 || lval > UINT16_MAX)
156 fatal("port number out of range for domain %s: %ld", p, lval);
161 parse_conf(const char *path)
163 if ((yyin = fopen(path, "r")) == NULL)
164 fatal("cannot open config %s", path);
173 load_vhosts(struct tls_config *tlsconf)
177 /* we need to set something, then we can add how many key we want */
178 if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
179 fatal("tls_config_set_keypair_file failed");
181 for (h = hosts; h->domain != NULL; ++h) {
182 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
183 fatal("failed to load the keypair (%s, %s)",
186 if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
187 fatal("open %s for domain %s", h->dir, h->domain);
192 make_socket(int port, int family)
195 struct sockaddr_in addr4;
196 struct sockaddr_in6 addr6;
197 struct sockaddr *addr;
202 bzero(&addr4, sizeof(addr4));
203 addr4.sin_family = family;
204 addr4.sin_port = htons(port);
205 addr4.sin_addr.s_addr = INADDR_ANY;
206 addr = (struct sockaddr*)&addr4;
211 bzero(&addr6, sizeof(addr6));
212 addr6.sin6_family = AF_INET6;
213 addr6.sin6_port = htons(port);
214 addr6.sin6_addr = in6addr_any;
215 addr = (struct sockaddr*)&addr6;
224 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
225 fatal("socket: %s", strerror(errno));
228 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
229 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
232 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
233 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
237 if (bind(sock, addr, len) == -1)
238 fatal("bind: %s", strerror(errno));
240 if (listen(sock, 16) == -1)
241 fatal("listen: %s", strerror(errno));
250 struct tls *ctx = NULL;
251 struct tls_config *tlsconf;
255 if ((tlsconf = tls_config_new()) == NULL)
256 fatal("tls_config_new");
258 /* optionally accept client certs, but don't try to verify them */
259 tls_config_verify_client_optional(tlsconf);
260 tls_config_insecure_noverifycert(tlsconf);
262 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
263 fatal("tls_config_set_protocols");
265 if ((ctx = tls_server()) == NULL)
266 fatal("tls_server failure");
268 load_vhosts(tlsconf);
270 if (tls_configure(ctx, tlsconf) == -1)
271 fatal("tls_configure: %s", tls_error(ctx));
273 if (!conf.foreground && daemon(0, 1) == -1)
276 sock4 = make_socket(conf.port, AF_INET);
279 sock6 = make_socket(conf.port, AF_INET6);
282 loop(ctx, sock4, sock6);
288 usage(const char *me)
291 "USAGE: %s [-n] [-c config] | [-6fh] [-C cert] [-d root] [-K key] "
292 "[-p port] [-x cgi-bin]\n",
297 main(int argc, char **argv)
300 const char *config_path = NULL;
304 bzero(hosts, sizeof(hosts));
305 for (i = 0; i < HOSTSLEN; ++i)
311 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
315 while ((ch = getopt(argc, argv, "6C:c:d:fhK:np:x:")) != -1) {
322 hosts[0].cert = optarg;
326 config_path = optarg;
330 free((char*)hosts[0].dir);
331 if ((hosts[0].dir = absolutify_path(optarg)) == NULL)
332 fatal("absolutify_path");
344 hosts[0].key = optarg;
352 conf.port = parse_portno(optarg);
356 /* drop the starting / (if any) */
359 hosts[0].cgi = optarg;
368 if (config_path != NULL) {
369 if (hosts[0].cert != NULL || hosts[0].key != NULL ||
370 hosts[0].dir != NULL)
371 fatal("can't specify options in conf mode");
372 parse_conf(config_path);
374 if (hosts[0].cert == NULL || hosts[0].key == NULL ||
375 hosts[0].dir == NULL)
376 fatal("missing cert, key or root directory to serve");
377 hosts[0].domain = "*";
385 signal(SIGPIPE, SIG_IGN);
386 signal(SIGCHLD, SIG_IGN);
389 signal(SIGINFO, sig_handler);
391 signal(SIGUSR2, sig_handler);
393 if (!conf.foreground) {
394 signal(SIGHUP, SIG_IGN);
395 if (daemon(1, 1) == -1)
396 fatal("daemon: %s", strerror(errno));
399 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
400 fatal("socketpair: %s", strerror(errno));
404 fatal("fork: %s", strerror(errno));
412 default: /* parent */
414 return executor_main(p[0]);