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 fcgi fcgi[FCGI_MAX];
31 struct vhosthead hosts;
35 struct imsgbuf logibuf, exibuf, servibuf[PROC_MAX];
37 const char *config_path, *certs_dir, *hostname, *pidfile, *cgi;
41 struct tls_config *tlsconf;
45 dummy_handler(int signo)
50 /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
52 pdirname(const char *path, char *dn)
57 strlcpy(p, path, sizeof(p));
59 memmove(dn, t, strlen(t)+1);
63 mkdirs(const char *path, mode_t mode)
65 char dname[PATH_MAX+1];
67 pdirname(path, dname);
68 if (!strcmp(dname, "/"))
71 if (mkdir(path, mode) != 0 && errno != EEXIST)
72 fatal("can't mkdir %s: %s", path, strerror(errno));
75 /* $XDG_DATA_HOME/gmid */
79 const char *home, *xdg;
82 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
83 if ((home = getenv("HOME")) == NULL)
84 errx(1, "XDG_DATA_HOME and HOME both empty");
85 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
88 if (asprintf(&t, "%s/gmid", xdg) == -1)
97 load_local_cert(const char *hostname, const char *dir)
102 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
104 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
107 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
108 gen_certificate(hostname, cert, key);
110 h = TAILQ_FIRST(&hosts);
113 h->domain = hostname;
122 TAILQ_FOREACH(h, &hosts, vhosts) {
123 TAILQ_FOREACH(l, &h->locations, locations) {
126 if ((l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY)) == -1)
127 fatal("open %s for domain %s", l->dir, h->domain);
133 make_socket(int port, int family)
136 struct sockaddr_in addr4;
137 struct sockaddr_in6 addr6;
138 struct sockaddr *addr;
143 bzero(&addr4, sizeof(addr4));
144 addr4.sin_family = family;
145 addr4.sin_port = htons(port);
146 addr4.sin_addr.s_addr = INADDR_ANY;
147 addr = (struct sockaddr*)&addr4;
152 bzero(&addr6, sizeof(addr6));
153 addr6.sin6_family = AF_INET6;
154 addr6.sin6_port = htons(port);
155 addr6.sin6_addr = in6addr_any;
156 addr = (struct sockaddr*)&addr6;
165 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
166 fatal("socket: %s", strerror(errno));
169 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
170 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
173 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
174 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
178 if (bind(sock, addr, len) == -1)
179 fatal("bind: %s", strerror(errno));
181 if (listen(sock, 16) == -1)
182 fatal("listen: %s", strerror(errno));
192 if ((tlsconf = tls_config_new()) == NULL)
193 fatal("tls_config_new");
195 /* optionally accept client certs, but don't try to verify them */
196 tls_config_verify_client_optional(tlsconf);
197 tls_config_insecure_noverifycert(tlsconf);
199 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
200 fatal("tls_config_set_protocols");
202 if ((ctx = tls_server()) == NULL)
203 fatal("tls_server failure");
205 h = TAILQ_FIRST(&hosts);
207 /* we need to set something, then we can add how many key we want */
208 if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
209 fatal("tls_config_set_keypair_file failed for (%s, %s)",
212 while ((h = TAILQ_NEXT(h, vhosts)) != NULL) {
213 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
214 fatal("failed to load the keypair (%s, %s)",
218 if (tls_configure(ctx, tlsconf) == -1)
219 fatal("tls_configure: %s", tls_error(ctx));
223 listener_main(struct imsgbuf *ibuf)
226 load_default_mime(&conf.mime);
228 loop(ctx, sock4, sock6, ibuf);
239 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
241 init_mime(&conf.mime);
252 struct vhost *h, *th;
253 struct location *l, *tl;
254 struct envlist *e, *te;
255 struct alist *a, *ta;
262 memset(&conf, 0, sizeof(conf));
266 TAILQ_FOREACH_SAFE(h, &hosts, vhosts, th) {
267 TAILQ_FOREACH_SAFE(l, &h->locations, locations, tl) {
268 TAILQ_REMOVE(&h->locations, l, locations);
270 free((char*)l->match);
271 free((char*)l->lang);
272 free((char*)l->default_mime);
273 free((char*)l->index);
274 free((char*)l->block_fmt);
283 TAILQ_FOREACH_SAFE(e, &h->env, envs, te) {
284 TAILQ_REMOVE(&h->env, e, envs);
291 TAILQ_FOREACH_SAFE(e, &h->params, envs, te) {
292 TAILQ_REMOVE(&h->params, e, envs);
299 TAILQ_FOREACH_SAFE(a, &h->aliases, aliases, ta) {
300 TAILQ_REMOVE(&h->aliases, a, aliases);
306 free((char*)h->domain);
307 free((char*)h->cert);
310 free((char*)h->entrypoint);
312 TAILQ_REMOVE(&hosts, h, vhosts);
316 for (i = 0; i < FCGI_MAX; ++i) {
317 if (fcgi[i].path == NULL && fcgi[i].prog == NULL)
325 tls_config_free(tlsconf);
335 sigaddset(&mask, SIGHUP);
336 sigaddset(&mask, SIGINT);
337 sigaddset(&mask, SIGTERM);
338 sigwait(&mask, &signo);
340 return signo == SIGHUP;
346 struct passwd *pw = NULL;
348 if (conf.chroot != NULL && conf.user == NULL)
349 fatal("can't chroot without an user to switch to after.");
351 if (conf.user != NULL) {
352 if ((pw = getpwnam(conf.user)) == NULL)
353 fatal("can't find user %s", conf.user);
356 if (conf.chroot != NULL) {
357 if (chroot(conf.chroot) != 0 || chdir("/") != 0)
358 fatal("%s: %s", conf.chroot, strerror(errno));
362 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
363 fatal("setresuid(%d): %s", pw->pw_uid,
368 log_warn(NULL, "not a good idea to run a network daemon as root");
372 usage(const char *me)
375 "USAGE: %s [-fn] [-c config] [-P pidfile] | [-6h] [-d certs-dir] [-H host]\n"
376 " [-p port] [-x cgi] [dir]\n",
385 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
386 err(1, "socketpair");
392 signal(SIGHUP, SIG_IGN);
394 setproctitle("logger");
395 imsg_init(&logibuf, p[1]);
397 _exit(logger_main(p[1], &logibuf));
400 imsg_init(&logibuf, p[0]);
406 serve(int argc, char **argv, struct imsgbuf *ibuf)
413 if (config_path == NULL) {
414 if (hostname == NULL)
415 hostname = "localhost";
416 if (certs_dir == NULL)
417 certs_dir = data_dir();
418 load_local_cert(hostname, certs_dir);
420 h = TAILQ_FIRST(&hosts);
423 l = TAILQ_FIRST(&h->locations);
429 l->dir = getcwd(path, sizeof(path));
432 l->dir = absolutify_path(argv[0]);
435 usage(getprogname());
439 log_notice(NULL, "serving %s on port %d", l->dir, conf.port);
442 /* setup tls before dropping privileges: we don't want user
443 * to put private certs inside the chroot. */
446 for (i = 0; i < conf.prefork; ++i) {
447 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
449 fatal("socketpair: %s", strerror(errno));
453 fatal("fork: %s", strerror(errno));
456 imsg_init(&exibuf, p[1]);
457 setproctitle("server");
458 _exit(listener_main(&exibuf));
461 imsg_init(&servibuf[i], p[0]);
465 setproctitle("executor");
467 _exit(executor_main(ibuf));
471 write_pidfile(const char *pidfile)
479 if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
480 fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
484 lock.l_type = F_WRLCK;
485 lock.l_whence = SEEK_SET;
487 if (fcntl(fd, F_SETLK, &lock) == -1)
488 fatal("can't lock %s, gmid is already running?", pidfile);
490 if (ftruncate(fd, 0) == -1)
491 fatal("ftruncate: %s: %s", pidfile, strerror(errno));
493 dprintf(fd, "%d\n", getpid());
499 setup_configless(int argc, char **argv, const char *cgi)
502 struct location *loc;
504 host = xcalloc(1, sizeof(*host));
506 TAILQ_INSERT_HEAD(&hosts, host, vhosts);
508 loc = xcalloc(1, sizeof(*loc));
509 TAILQ_INSERT_HEAD(&host->locations, loc, locations);
511 imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, 2, NULL, 0);
512 imsg_flush(&logibuf);
514 serve(argc, argv, NULL);
516 imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
517 imsg_flush(&logibuf);
521 main(int argc, char **argv)
523 struct imsgbuf exibuf;
524 int ch, conftest = 0, configless = 0;
525 int pidfd, old_ipv6, old_port;
529 while ((ch = getopt(argc, argv, "6c:d:fH:hnP:p:vx:")) != -1) {
537 config_path = absolutify_path(optarg);
567 conf.port = parse_portno(optarg);
576 /* drop the starting / (if any) */
591 if (config_path == NULL) {
598 if (config_path != NULL && (argc > 0 || configless))
599 errx(1, "can't specify options in config mode.");
602 parse_conf(config_path);
607 if (!conf.foreground && !configless) {
608 if (daemon(1, 1) == -1)
612 if (config_path != NULL)
613 parse_conf(config_path);
617 sock4 = make_socket(conf.port, AF_INET);
620 sock6 = make_socket(conf.port, AF_INET6);
622 signal(SIGPIPE, SIG_IGN);
623 signal(SIGCHLD, SIG_IGN);
626 setup_configless(argc, argv, cgi);
630 if (conf.foreground) {
631 imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, 2, NULL, 0);
632 imsg_flush(&logibuf);
635 pidfd = write_pidfile(pidfile);
637 /* Linux seems to call the event handlers even when we're
638 * doing a sigwait. These dummy handlers are here to avoid
639 * being terminated on SIGHUP, SIGINT or SIGTERM. */
640 signal(SIGHUP, dummy_handler);
641 signal(SIGINT, dummy_handler);
642 signal(SIGTERM, dummy_handler);
644 /* wait a sighup and reload the daemon */
648 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
650 fatal("socketpair: %s", strerror(errno));
654 fatal("fork: %s", strerror(errno));
657 imsg_init(&exibuf, p[1]);
658 _exit(serve(argc, argv, &exibuf));
662 imsg_init(&exibuf, p[0]);
667 log_info(NULL, "reloading configuration %s", config_path);
669 /* close the executor (it'll close the servers too) */
670 imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
674 old_ipv6 = conf.ipv6;
675 old_port = conf.port;
679 parse_conf(config_path);
681 if (old_port != conf.port) {
688 if (sock6 != -1 && old_ipv6 != conf.ipv6) {
694 sock4 = make_socket(conf.port, AF_INET);
695 if (sock6 == -1 && conf.ipv6)
696 sock6 = make_socket(conf.port, AF_INET6);
699 imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
702 imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
703 imsg_flush(&logibuf);