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 vhosthead hosts;
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)
100 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
102 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
105 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
106 gen_certificate(hostname, cert, key);
108 h = TAILQ_FIRST(&hosts);
111 h->domain = hostname;
119 TAILQ_FOREACH(h, &hosts, vhosts) {
120 if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
121 fatal("open %s for domain %s", h->dir, h->domain);
126 make_socket(int port, int family)
129 struct sockaddr_in addr4;
130 struct sockaddr_in6 addr6;
131 struct sockaddr *addr;
136 bzero(&addr4, sizeof(addr4));
137 addr4.sin_family = family;
138 addr4.sin_port = htons(port);
139 addr4.sin_addr.s_addr = INADDR_ANY;
140 addr = (struct sockaddr*)&addr4;
145 bzero(&addr6, sizeof(addr6));
146 addr6.sin6_family = AF_INET6;
147 addr6.sin6_port = htons(port);
148 addr6.sin6_addr = in6addr_any;
149 addr = (struct sockaddr*)&addr6;
158 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
159 fatal("socket: %s", strerror(errno));
162 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
163 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
166 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
167 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
171 if (bind(sock, addr, len) == -1)
172 fatal("bind: %s", strerror(errno));
174 if (listen(sock, 16) == -1)
175 fatal("listen: %s", strerror(errno));
185 if ((tlsconf = tls_config_new()) == NULL)
186 fatal("tls_config_new");
188 /* optionally accept client certs, but don't try to verify them */
189 tls_config_verify_client_optional(tlsconf);
190 tls_config_insecure_noverifycert(tlsconf);
192 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
193 fatal("tls_config_set_protocols");
195 if ((ctx = tls_server()) == NULL)
196 fatal("tls_server failure");
198 h = TAILQ_FIRST(&hosts);
200 /* we need to set something, then we can add how many key we want */
201 if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
202 fatal("tls_config_set_keypair_file failed for (%s, %s)",
205 while ((h = TAILQ_NEXT(h, vhosts)) != NULL) {
206 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
207 fatal("failed to load the keypair (%s, %s)",
211 if (tls_configure(ctx, tlsconf) == -1)
212 fatal("tls_configure: %s", tls_error(ctx));
216 listener_main(struct imsgbuf *ibuf)
219 load_default_mime(&conf.mime);
221 loop(ctx, sock4, sock6, ibuf);
232 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
234 init_mime(&conf.mime);
245 struct vhost *h, *th;
246 struct location *l, *tl;
247 struct envlist *e, *te;
248 struct alist *a, *ta;
255 memset(&conf, 0, sizeof(conf));
259 TAILQ_FOREACH_SAFE(h, &hosts, vhosts, th) {
260 TAILQ_FOREACH_SAFE(l, &h->locations, locations, tl) {
261 TAILQ_REMOVE(&h->locations, l, locations);
263 free((char*)l->match);
264 free((char*)l->lang);
265 free((char*)l->default_mime);
266 free((char*)l->index);
267 free((char*)l->block_fmt);
271 TAILQ_FOREACH_SAFE(e, &h->env, envs, te) {
277 TAILQ_FOREACH_SAFE(a, &h->aliases, aliases, ta) {
282 TAILQ_REMOVE(&hosts, h, vhosts);
287 tls_config_free(tlsconf);
297 sigaddset(&mask, SIGHUP);
298 sigaddset(&mask, SIGINT);
299 sigaddset(&mask, SIGTERM);
300 sigwait(&mask, &signo);
302 return signo == SIGHUP;
308 struct passwd *pw = NULL;
310 if (conf.chroot != NULL && conf.user == NULL)
311 fatal("can't chroot without an user to switch to after.");
313 if (conf.user != NULL) {
314 if ((pw = getpwnam(conf.user)) == NULL)
315 fatal("can't find user %s", conf.user);
318 if (conf.chroot != NULL) {
319 if (chroot(conf.chroot) != 0 || chdir("/") != 0)
320 fatal("%s: %s", conf.chroot, strerror(errno));
324 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
325 fatal("setresuid(%d): %s", pw->pw_uid,
330 log_warn(NULL, "not a good idea to run a network daemon as root");
334 usage(const char *me)
337 "USAGE: %s [-fn] [-c config] [-P pidfile] | [-6h] [-d certs-dir] [-H host]\n"
338 " [-p port] [-x cgi] [dir]\n",
347 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
348 err(1, "socketpair");
354 signal(SIGHUP, SIG_IGN);
356 setproctitle("logger");
357 imsg_init(&logibuf, p[1]);
359 _exit(logger_main(p[1], &logibuf));
362 imsg_init(&logibuf, p[0]);
368 serve(int argc, char **argv, struct imsgbuf *ibuf)
375 if (config_path == NULL) {
376 if (hostname == NULL)
377 hostname = "localhost";
378 if (certs_dir == NULL)
379 certs_dir = data_dir();
380 load_local_cert(hostname, certs_dir);
382 h = TAILQ_FIRST(&hosts);
385 l = TAILQ_FIRST(&h->locations);
391 h->dir = getcwd(path, sizeof(path));
394 h->dir = absolutify_path(argv[0]);
397 usage(getprogname());
401 log_notice(NULL, "serving %s on port %d", h->dir, conf.port);
404 /* setup tls before dropping privileges: we don't want user
405 * to put private certs inside the chroot. */
408 for (i = 0; i < conf.prefork; ++i) {
409 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
411 fatal("socketpair: %s", strerror(errno));
415 fatal("fork: %s", strerror(errno));
418 imsg_init(&exibuf, p[1]);
419 setproctitle("server");
420 _exit(listener_main(&exibuf));
423 imsg_init(&servibuf[i], p[0]);
427 setproctitle("executor");
429 _exit(executor_main(ibuf));
433 write_pidfile(const char *pidfile)
441 if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
442 fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
446 lock.l_type = F_WRLCK;
447 lock.l_whence = SEEK_SET;
449 if (fcntl(fd, F_SETLK, &lock) == -1)
450 fatal("can't lock %s, gmid is already running?", pidfile);
452 if (ftruncate(fd, 0) == -1)
453 fatal("ftruncate: %s: %s", pidfile, strerror(errno));
455 dprintf(fd, "%d\n", getpid());
461 setup_configless(int argc, char **argv, const char *cgi)
464 struct location *loc;
466 host = xcalloc(1, sizeof(*host));
468 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
470 loc = xcalloc(1, sizeof(*loc));
471 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
473 serve(argc, argv, NULL);
474 imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
475 imsg_flush(&logibuf);
479 main(int argc, char **argv)
481 struct imsgbuf exibuf;
482 int ch, conftest = 0, configless = 0;
483 int pidfd, old_ipv6, old_port;
484 const char *pidfile = NULL, *cgi = NULL;
488 while ((ch = getopt(argc, argv, "6c:d:fH:hnP:p:vx:")) != -1) {
496 config_path = absolutify_path(optarg);
526 conf.port = parse_portno(optarg);
535 /* drop the starting / (if any) */
550 if (config_path == NULL) {
557 if (config_path != NULL && (argc > 0 || configless))
558 errx(1, "can't specify options in config mode.");
561 parse_conf(config_path);
566 if (!conf.foreground && !configless) {
567 if (daemon(1, 1) == -1)
571 if (config_path != NULL)
572 parse_conf(config_path);
576 sock4 = make_socket(conf.port, AF_INET);
579 sock6 = make_socket(conf.port, AF_INET6);
581 signal(SIGPIPE, SIG_IGN);
582 signal(SIGCHLD, SIG_IGN);
585 setup_configless(argc, argv, cgi);
589 pidfd = write_pidfile(pidfile);
591 /* Linux seems to call the event handlers even when we're
592 * doing a sigwait. These dummy handlers are here to avoid
593 * being terminated on SIGHUP, SIGINT or SIGTERM. */
594 signal(SIGHUP, dummy_handler);
595 signal(SIGINT, dummy_handler);
596 signal(SIGTERM, dummy_handler);
598 /* wait a sighup and reload the daemon */
602 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
604 fatal("socketpair: %s", strerror(errno));
608 fatal("fork: %s", strerror(errno));
611 imsg_init(&exibuf, p[1]);
612 _exit(serve(argc, argv, &exibuf));
616 imsg_init(&exibuf, p[0]);
621 log_info(NULL, "reloading configuration %s", config_path);
623 /* close the executor (it'll close the servers too) */
624 imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
628 old_ipv6 = conf.ipv6;
629 old_port = conf.port;
633 parse_conf(config_path);
635 if (old_port != conf.port) {
642 if (sock6 != -1 && old_ipv6 != conf.ipv6) {
648 sock4 = make_socket(conf.port, AF_INET);
649 if (sock6 == -1 && conf.ipv6)
650 sock6 = make_socket(conf.port, AF_INET6);
653 imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
656 imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
657 imsg_flush(&logibuf);