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.
28 volatile sig_atomic_t hupped;
30 struct vhost hosts[HOSTSLEN];
32 int exfd, sock4, sock6;
34 const char *config_path, *certs_dir, *hostname;
38 struct tls_config *tlsconf;
46 hupped = sig == SIGHUP;
49 /* XXX: create recursively */
51 mkdirs(const char *path)
53 if (mkdir(path, 0755) == -1 && errno != EEXIST)
54 fatal("can't mkdir %s: %s", path, strerror(errno));
57 /* $XDG_DATA_HOME/gmid */
61 const char *home, *xdg;
64 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
65 if ((home = getenv("HOME")) == NULL)
66 errx(1, "XDG_DATA_HOME and HOME both empty");
67 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
73 if (asprintf(&t, "%s/gmid", xdg) == -1)
80 load_local_cert(const char *hostname, const char *dir)
84 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
86 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
89 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
90 gen_certificate(hostname, cert, key);
94 hosts[0].domain = hostname;
102 for (h = hosts; h->domain != NULL; ++h) {
103 if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
104 fatal("open %s for domain %s", h->dir, h->domain);
109 make_socket(int port, int family)
112 struct sockaddr_in addr4;
113 struct sockaddr_in6 addr6;
114 struct sockaddr *addr;
119 bzero(&addr4, sizeof(addr4));
120 addr4.sin_family = family;
121 addr4.sin_port = htons(port);
122 addr4.sin_addr.s_addr = INADDR_ANY;
123 addr = (struct sockaddr*)&addr4;
128 bzero(&addr6, sizeof(addr6));
129 addr6.sin6_family = AF_INET6;
130 addr6.sin6_port = htons(port);
131 addr6.sin6_addr = in6addr_any;
132 addr = (struct sockaddr*)&addr6;
141 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
142 fatal("socket: %s", strerror(errno));
145 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
146 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
149 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
150 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
154 if (bind(sock, addr, len) == -1)
155 fatal("bind: %s", strerror(errno));
157 if (listen(sock, 16) == -1)
158 fatal("listen: %s", strerror(errno));
168 if ((tlsconf = tls_config_new()) == NULL)
169 fatal("tls_config_new");
171 /* optionally accept client certs, but don't try to verify them */
172 tls_config_verify_client_optional(tlsconf);
173 tls_config_insecure_noverifycert(tlsconf);
175 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
176 fatal("tls_config_set_protocols");
178 if ((ctx = tls_server()) == NULL)
179 fatal("tls_server failure");
181 /* we need to set something, then we can add how many key we want */
182 if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
183 fatal("tls_config_set_keypair_file failed for (%s, %s)",
184 hosts->cert, hosts->key);
186 for (h = &hosts[1]; h->domain != NULL; ++h) {
187 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
188 fatal("failed to load the keypair (%s, %s)",
192 if (tls_configure(ctx, tlsconf) == -1)
193 fatal("tls_configure: %s", tls_error(ctx));
201 load_default_mime(&conf.mime);
204 loop(ctx, sock4, sock6);
213 bzero(hosts, sizeof(hosts));
214 for (i = 0; i < HOSTSLEN; ++i)
219 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
221 init_mime(&conf.mime);
226 /* we'll change this to 0 when running without config. */
238 memset(&conf, 0, sizeof(conf));
240 for (h = hosts; h->domain != NULL; ++h) {
241 free((char*)h->domain);
242 free((char*)h->cert);
246 free((char*)h->entrypoint);
248 for (l = h->locations; l->match != NULL; ++l) {
249 free((char*)l->match);
250 free((char*)l->lang);
251 free((char*)l->default_mime);
252 free((char*)l->index);
253 free((char*)l->block_fmt);
256 memset(hosts, 0, sizeof(hosts));
259 tls_config_free(tlsconf);
269 sigaddset(&mask, SIGHUP);
270 sigwait(&mask, &signo);
276 struct passwd *pw = NULL;
278 if (conf.chroot != NULL && conf.user == NULL)
279 fatal("can't chroot without an user to switch to after.");
281 if (conf.user != NULL) {
282 if ((pw = getpwnam(conf.user)) == NULL)
283 fatal("can't find user %s", conf.user);
286 if (conf.chroot != NULL) {
287 if (chroot(conf.chroot) != 0 || chdir("/") != 0)
288 fatal("%s: %s", conf.chroot, strerror(errno));
292 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
293 fatal("setresuid(%d): %s", pw->pw_uid,
298 log_warn(NULL, "not a good idea to run a network daemon as root");
302 spawn_listeners(int *p)
309 for (i = 0; i < conf.prefork -1; ++i) {
312 fatal("fork: %s", strerror(errno));
314 setproctitle("server(%d)", i+1);
315 return listener_main();
319 if (conf.prefork == 0)
320 setproctitle("server");
322 setproctitle("server(%d)", conf.prefork);
323 return listener_main();
327 usage(const char *me)
330 "USAGE: %s [-fn] [-c config] | [-6h] [-d certs-dir] [-H host]"
331 " [-p port] [-x cgi] [dir]",
336 serve(int argc, char **argv, int *p)
340 if (config_path == NULL) {
341 if (hostname == NULL)
342 hostname = "localhost";
343 if (certs_dir == NULL)
344 certs_dir = data_dir();
345 load_local_cert(hostname, certs_dir);
347 hosts[0].domain = "*";
348 hosts[0].locations[0].auto_index = 1;
349 hosts[0].locations[0].match = "*";
353 hosts[0].dir = getcwd(path, sizeof(path));
356 hosts[0].dir = absolutify_path(argv[0]);
359 usage(getprogname());
363 log_notice(NULL, "serving %s on port %d", hosts[0].dir, conf.port);
366 /* setup tls before dropping privileges: we don't want user
367 * to put private certs inside the chroot. */
372 fatal("fork: %s", strerror(errno));
375 return spawn_listeners(p);
377 default: /* parent */
378 setproctitle("executor");
383 return executor_main();
388 main(int argc, char **argv)
391 int conftest = 0, configless = 0;
392 int old_ipv6, old_port;
396 while ((ch = getopt(argc, argv, "6c:d:fH:hnp:vx:")) != -1) {
404 config_path = absolutify_path(optarg);
430 conf.port = parse_portno(optarg);
439 /* drop the starting / (if any) */
442 hosts[0].cgi = optarg;
454 if (config_path == NULL) {
461 if (config_path != NULL && (argc > 0 || configless))
462 err(1, "can't specify options in config mode.");
465 parse_conf(config_path);
470 signal(SIGPIPE, SIG_IGN);
471 signal(SIGCHLD, SIG_IGN);
474 signal(SIGINFO, sig_handler);
476 signal(SIGUSR2, sig_handler);
477 signal(SIGHUP, sig_handler);
479 if (!conf.foreground && !configless) {
480 if (daemon(1, 1) == -1)
481 fatal("daemon: %s", strerror(errno));
484 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
486 fatal("socketpair: %s", strerror(errno));
488 if (config_path != NULL)
489 parse_conf(config_path);
491 sock4 = make_socket(conf.port, AF_INET);
494 sock6 = make_socket(conf.port, AF_INET6);
497 return serve(argc, argv, p);
499 /* wait a sighup and reload the daemon */
506 fatal("fork: %s", strerror(errno));
508 return serve(argc, argv, p);
516 log_info(NULL, "reloading configuration %s", config_path);
518 old_ipv6 = conf.ipv6;
519 old_port = conf.port;
523 parse_conf(config_path);
525 if (old_port != conf.port) {
532 if (sock6 != -1 && old_ipv6 != conf.ipv6) {
538 sock4 = make_socket(conf.port, AF_INET);
539 if (sock6 == -1 && conf.ipv6)
540 sock6 = make_socket(conf.port, AF_INET6);
542 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
544 fatal("socketpair: %s", strerror(errno));