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, logfd, sock4, sock6;
34 struct imsgbuf logpibuf, logcibuf;
36 const char *config_path, *certs_dir, *hostname;
40 struct tls_config *tlsconf;
48 hupped = sig == SIGHUP;
51 /* XXX: create recursively */
53 mkdirs(const char *path)
55 if (mkdir(path, 0755) == -1 && errno != EEXIST)
56 fatal("can't mkdir %s: %s", path, strerror(errno));
59 /* $XDG_DATA_HOME/gmid */
63 const char *home, *xdg;
66 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
67 if ((home = getenv("HOME")) == NULL)
68 errx(1, "XDG_DATA_HOME and HOME both empty");
69 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
75 if (asprintf(&t, "%s/gmid", xdg) == -1)
82 load_local_cert(const char *hostname, const char *dir)
86 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
88 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
91 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
92 gen_certificate(hostname, cert, key);
96 hosts[0].domain = hostname;
104 for (h = hosts; h->domain != NULL; ++h) {
105 if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
106 fatal("open %s for domain %s", h->dir, h->domain);
111 make_socket(int port, int family)
114 struct sockaddr_in addr4;
115 struct sockaddr_in6 addr6;
116 struct sockaddr *addr;
121 bzero(&addr4, sizeof(addr4));
122 addr4.sin_family = family;
123 addr4.sin_port = htons(port);
124 addr4.sin_addr.s_addr = INADDR_ANY;
125 addr = (struct sockaddr*)&addr4;
130 bzero(&addr6, sizeof(addr6));
131 addr6.sin6_family = AF_INET6;
132 addr6.sin6_port = htons(port);
133 addr6.sin6_addr = in6addr_any;
134 addr = (struct sockaddr*)&addr6;
143 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
144 fatal("socket: %s", strerror(errno));
147 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
148 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
151 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
152 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
156 if (bind(sock, addr, len) == -1)
157 fatal("bind: %s", strerror(errno));
159 if (listen(sock, 16) == -1)
160 fatal("listen: %s", strerror(errno));
170 if ((tlsconf = tls_config_new()) == NULL)
171 fatal("tls_config_new");
173 /* optionally accept client certs, but don't try to verify them */
174 tls_config_verify_client_optional(tlsconf);
175 tls_config_insecure_noverifycert(tlsconf);
177 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
178 fatal("tls_config_set_protocols");
180 if ((ctx = tls_server()) == NULL)
181 fatal("tls_server failure");
183 /* we need to set something, then we can add how many key we want */
184 if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
185 fatal("tls_config_set_keypair_file failed for (%s, %s)",
186 hosts->cert, hosts->key);
188 for (h = &hosts[1]; h->domain != NULL; ++h) {
189 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
190 fatal("failed to load the keypair (%s, %s)",
194 if (tls_configure(ctx, tlsconf) == -1)
195 fatal("tls_configure: %s", tls_error(ctx));
203 load_default_mime(&conf.mime);
205 loop(ctx, sock4, sock6);
214 bzero(hosts, sizeof(hosts));
215 for (i = 0; i < HOSTSLEN; ++i)
220 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
222 init_mime(&conf.mime);
227 /* we'll change this to 0 when running without config. */
239 memset(&conf, 0, sizeof(conf));
241 for (h = hosts; h->domain != NULL; ++h) {
242 free((char*)h->domain);
243 free((char*)h->cert);
247 free((char*)h->entrypoint);
249 for (l = h->locations; l->match != NULL; ++l) {
250 free((char*)l->match);
251 free((char*)l->lang);
252 free((char*)l->default_mime);
253 free((char*)l->index);
254 free((char*)l->block_fmt);
257 memset(hosts, 0, sizeof(hosts));
260 tls_config_free(tlsconf);
270 sigaddset(&mask, SIGHUP);
271 sigwait(&mask, &signo);
277 struct passwd *pw = NULL;
279 if (conf.chroot != NULL && conf.user == NULL)
280 fatal("can't chroot without an user to switch to after.");
282 if (conf.user != NULL) {
283 if ((pw = getpwnam(conf.user)) == NULL)
284 fatal("can't find user %s", conf.user);
287 if (conf.chroot != NULL) {
288 if (chroot(conf.chroot) != 0 || chdir("/") != 0)
289 fatal("%s: %s", conf.chroot, strerror(errno));
293 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
294 fatal("setresuid(%d): %s", pw->pw_uid,
299 log_warn(NULL, "not a good idea to run a network daemon as root");
303 spawn_listeners(int *p)
310 for (i = 0; i < conf.prefork -1; ++i) {
313 fatal("fork: %s", strerror(errno));
315 setproctitle("server(%d)", i+1);
316 return listener_main();
320 if (conf.prefork == 0)
321 setproctitle("server");
323 setproctitle("server(%d)", conf.prefork);
324 return listener_main();
328 usage(const char *me)
331 "USAGE: %s [-fn] [-c config] | [-6h] [-d certs-dir] [-H host]"
332 " [-p port] [-x cgi] [dir]\n",
341 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
342 err(1, "socketpair");
350 setproctitle("logger");
351 imsg_init(&logcibuf, p[1]);
353 _exit(logger_main(p[1], &logcibuf));
357 imsg_init(&logpibuf, p[0]);
364 serve(int argc, char **argv, int *p)
368 if (config_path == NULL) {
369 if (hostname == NULL)
370 hostname = "localhost";
371 if (certs_dir == NULL)
372 certs_dir = data_dir();
373 load_local_cert(hostname, certs_dir);
375 hosts[0].domain = "*";
376 hosts[0].locations[0].auto_index = 1;
377 hosts[0].locations[0].match = "*";
381 hosts[0].dir = getcwd(path, sizeof(path));
384 hosts[0].dir = absolutify_path(argv[0]);
387 usage(getprogname());
391 log_notice(NULL, "serving %s on port %d", hosts[0].dir, conf.port);
394 /* setup tls before dropping privileges: we don't want user
395 * to put private certs inside the chroot. */
400 fatal("fork: %s", strerror(errno));
403 return spawn_listeners(p);
405 default: /* parent */
406 setproctitle("executor");
411 return executor_main();
416 main(int argc, char **argv)
419 int conftest = 0, configless = 0;
420 int old_ipv6, old_port;
424 while ((ch = getopt(argc, argv, "6c:d:fH:hnp:vx:")) != -1) {
432 config_path = absolutify_path(optarg);
458 conf.port = parse_portno(optarg);
467 /* drop the starting / (if any) */
470 hosts[0].cgi = optarg;
482 if (config_path == NULL) {
489 if (config_path != NULL && (argc > 0 || configless))
490 err(1, "can't specify options in config mode.");
493 parse_conf(config_path);
498 signal(SIGPIPE, SIG_IGN);
499 signal(SIGCHLD, SIG_IGN);
502 signal(SIGINFO, sig_handler);
504 signal(SIGUSR2, sig_handler);
505 signal(SIGHUP, sig_handler);
507 if (!conf.foreground && !configless) {
508 if (daemon(1, 1) == -1)
512 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
514 err(1, "socketpair");
516 if (config_path != NULL)
517 parse_conf(config_path);
521 sock4 = make_socket(conf.port, AF_INET);
524 sock6 = make_socket(conf.port, AF_INET6);
527 return serve(argc, argv, p);
529 /* wait a sighup and reload the daemon */
536 fatal("fork: %s", strerror(errno));
538 return serve(argc, argv, p);
546 log_info(NULL, "reloading configuration %s", config_path);
548 old_ipv6 = conf.ipv6;
549 old_port = conf.port;
553 parse_conf(config_path);
555 if (old_port != conf.port) {
562 if (sock6 != -1 && old_ipv6 != conf.ipv6) {
568 sock4 = make_socket(conf.port, AF_INET);
569 if (sock6 == -1 && conf.ipv6)
570 sock6 = make_socket(conf.port, AF_INET6);
572 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
574 fatal("socketpair: %s", strerror(errno));