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];
33 struct imsgbuf logibuf, exibuf, servibuf[PROC_MAX];
35 const char *config_path, *certs_dir, *hostname;
39 struct tls_config *tlsconf;
43 dummy_handler(int signo)
48 /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
50 pdirname(const char *path, char *dn)
55 strlcpy(p, path, sizeof(p));
57 memmove(dn, t, strlen(t)+1);
61 mkdirs(const char *path, mode_t mode)
63 char dname[PATH_MAX+1];
65 pdirname(path, dname);
66 if (!strcmp(dname, "/"))
69 if (mkdir(path, mode) != 0 && errno != EEXIST)
70 fatal("can't mkdir %s: %s", path, strerror(errno));
73 /* $XDG_DATA_HOME/gmid */
77 const char *home, *xdg;
80 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
81 if ((home = getenv("HOME")) == NULL)
82 errx(1, "XDG_DATA_HOME and HOME both empty");
83 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
86 if (asprintf(&t, "%s/gmid", xdg) == -1)
95 load_local_cert(const char *hostname, const char *dir)
99 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
101 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
104 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
105 gen_certificate(hostname, cert, key);
107 hosts[0].cert = cert;
109 hosts[0].domain = hostname;
117 for (h = hosts; h->domain != NULL; ++h) {
118 if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
119 fatal("open %s for domain %s", h->dir, h->domain);
124 make_socket(int port, int family)
127 struct sockaddr_in addr4;
128 struct sockaddr_in6 addr6;
129 struct sockaddr *addr;
134 bzero(&addr4, sizeof(addr4));
135 addr4.sin_family = family;
136 addr4.sin_port = htons(port);
137 addr4.sin_addr.s_addr = INADDR_ANY;
138 addr = (struct sockaddr*)&addr4;
143 bzero(&addr6, sizeof(addr6));
144 addr6.sin6_family = AF_INET6;
145 addr6.sin6_port = htons(port);
146 addr6.sin6_addr = in6addr_any;
147 addr = (struct sockaddr*)&addr6;
156 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
157 fatal("socket: %s", strerror(errno));
160 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
161 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
164 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
165 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
169 if (bind(sock, addr, len) == -1)
170 fatal("bind: %s", strerror(errno));
172 if (listen(sock, 16) == -1)
173 fatal("listen: %s", strerror(errno));
183 if ((tlsconf = tls_config_new()) == NULL)
184 fatal("tls_config_new");
186 /* optionally accept client certs, but don't try to verify them */
187 tls_config_verify_client_optional(tlsconf);
188 tls_config_insecure_noverifycert(tlsconf);
190 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
191 fatal("tls_config_set_protocols");
193 if ((ctx = tls_server()) == NULL)
194 fatal("tls_server failure");
196 /* we need to set something, then we can add how many key we want */
197 if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
198 fatal("tls_config_set_keypair_file failed for (%s, %s)",
199 hosts->cert, hosts->key);
201 for (h = &hosts[1]; h->domain != NULL; ++h) {
202 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
203 fatal("failed to load the keypair (%s, %s)",
207 if (tls_configure(ctx, tlsconf) == -1)
208 fatal("tls_configure: %s", tls_error(ctx));
212 listener_main(struct imsgbuf *ibuf)
215 load_default_mime(&conf.mime);
217 loop(ctx, sock4, sock6, ibuf);
226 bzero(hosts, sizeof(hosts));
227 for (i = 0; i < HOSTSLEN; ++i)
232 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
234 init_mime(&conf.mime);
239 /* we'll change this to 0 when running without config. */
251 memset(&conf, 0, sizeof(conf));
253 for (h = hosts; h->domain != NULL; ++h) {
254 free((char*)h->domain);
255 free((char*)h->cert);
259 free((char*)h->entrypoint);
261 for (l = h->locations; l->match != NULL; ++l) {
262 free((char*)l->match);
263 free((char*)l->lang);
264 free((char*)l->default_mime);
265 free((char*)l->index);
266 free((char*)l->block_fmt);
269 memset(hosts, 0, sizeof(hosts));
272 tls_config_free(tlsconf);
282 sigaddset(&mask, SIGHUP);
283 sigaddset(&mask, SIGINT);
284 sigaddset(&mask, SIGTERM);
285 sigwait(&mask, &signo);
287 return signo == SIGHUP;
293 struct passwd *pw = NULL;
295 if (conf.chroot != NULL && conf.user == NULL)
296 fatal("can't chroot without an user to switch to after.");
298 if (conf.user != NULL) {
299 if ((pw = getpwnam(conf.user)) == NULL)
300 fatal("can't find user %s", conf.user);
303 if (conf.chroot != NULL) {
304 if (chroot(conf.chroot) != 0 || chdir("/") != 0)
305 fatal("%s: %s", conf.chroot, strerror(errno));
309 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
310 fatal("setresuid(%d): %s", pw->pw_uid,
315 log_warn(NULL, "not a good idea to run a network daemon as root");
319 usage(const char *me)
322 "USAGE: %s [-fn] [-c config] | [-6h] [-d certs-dir] [-H host]\n"
323 " [-p port] [-x cgi] [dir]\n",
332 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
333 err(1, "socketpair");
339 signal(SIGHUP, SIG_IGN);
341 setproctitle("logger");
342 imsg_init(&logibuf, p[1]);
344 _exit(logger_main(p[1], &logibuf));
347 imsg_init(&logibuf, p[0]);
353 serve(int argc, char **argv, struct imsgbuf *ibuf)
358 if (config_path == NULL) {
359 if (hostname == NULL)
360 hostname = "localhost";
361 if (certs_dir == NULL)
362 certs_dir = data_dir();
363 load_local_cert(hostname, certs_dir);
365 hosts[0].domain = "*";
366 hosts[0].locations[0].auto_index = 1;
367 hosts[0].locations[0].match = "*";
371 hosts[0].dir = getcwd(path, sizeof(path));
374 hosts[0].dir = absolutify_path(argv[0]);
377 usage(getprogname());
381 log_notice(NULL, "serving %s on port %d", hosts[0].dir, conf.port);
384 /* setup tls before dropping privileges: we don't want user
385 * to put private certs inside the chroot. */
388 for (i = 0; i < conf.prefork; ++i) {
389 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
391 fatal("socketpair: %s", strerror(errno));
395 fatal("fork: %s", strerror(errno));
398 imsg_init(&exibuf, p[1]);
399 setproctitle("server");
400 _exit(listener_main(&exibuf));
403 imsg_init(&servibuf[i], p[0]);
407 setproctitle("executor");
409 _exit(executor_main(ibuf));
413 main(int argc, char **argv)
415 struct imsgbuf exibuf;
416 int ch, conftest = 0, configless = 0;
417 int old_ipv6, old_port;
421 while ((ch = getopt(argc, argv, "6c:d:fH:hnp:vx:")) != -1) {
429 config_path = absolutify_path(optarg);
455 conf.port = parse_portno(optarg);
464 /* drop the starting / (if any) */
467 hosts[0].cgi = optarg;
479 if (config_path == NULL) {
486 if (config_path != NULL && (argc > 0 || configless))
487 err(1, "can't specify options in config mode.");
490 parse_conf(config_path);
495 signal(SIGPIPE, SIG_IGN);
496 signal(SIGCHLD, SIG_IGN);
498 if (!conf.foreground && !configless) {
499 if (daemon(1, 1) == -1)
503 if (config_path != NULL)
504 parse_conf(config_path);
508 sock4 = make_socket(conf.port, AF_INET);
511 sock6 = make_socket(conf.port, AF_INET6);
514 serve(argc, argv, NULL);
515 imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
516 imsg_flush(&logibuf);
521 /* Linux seems to call the event handlers even when we're
522 * doing a sigwait. These dummy handlers is here to avoid
523 * being terminated on SIGHUP, SIGTERM or SIGINFO. */
524 signal(SIGHUP, dummy_handler);
525 signal(SIGINT, dummy_handler);
526 signal(SIGTERM, dummy_handler);
528 /* wait a sighup and reload the daemon */
532 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
534 fatal("socketpair: %s", strerror(errno));
538 fatal("fork: %s", strerror(errno));
541 imsg_init(&exibuf, p[1]);
542 _exit(serve(argc, argv, &exibuf));
546 imsg_init(&exibuf, p[0]);
551 log_info(NULL, "reloading configuration %s", config_path);
553 /* close the executor (it'll close the servers too) */
554 imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
558 old_ipv6 = conf.ipv6;
559 old_port = conf.port;
563 parse_conf(config_path);
565 if (old_port != conf.port) {
572 if (sock6 != -1 && old_ipv6 != conf.ipv6) {
578 sock4 = make_socket(conf.port, AF_INET);
579 if (sock6 == -1 && conf.ipv6)
580 sock6 = make_socket(conf.port, AF_INET6);
583 imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
586 imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
587 imsg_flush(&logibuf);