2 * Copyright (c) 2020, 2021, 2022 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.
30 static const char *opts = "D:df:hnP:Vv";
32 static const struct option longopts[] = {
33 {"help", no_argument, NULL, 'h'},
34 {"version", no_argument, NULL, 'V'},
38 struct fcgi fcgi[FCGI_MAX];
40 struct vhosthead hosts;
44 struct imsgbuf logibuf, servibuf[PROC_MAX];
46 const char *config_path = "/etc/gmid.conf";
51 struct tls_config *tlsconf;
55 dummy_handler(int signo)
66 TAILQ_FOREACH(h, &hosts, vhosts) {
67 TAILQ_FOREACH(l, &h->locations, locations) {
70 if ((l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY)) == -1)
71 fatal("open %s for domain %s: %s", l->dir, h->domain,
78 make_socket(int port, int family)
81 struct sockaddr_in addr4;
82 struct sockaddr_in6 addr6;
83 struct sockaddr *addr;
88 memset(&addr4, 0, sizeof(addr4));
89 addr4.sin_family = family;
90 addr4.sin_port = htons(port);
91 addr4.sin_addr.s_addr = INADDR_ANY;
92 addr = (struct sockaddr*)&addr4;
97 memset(&addr6, 0, sizeof(addr6));
98 addr6.sin6_family = AF_INET6;
99 addr6.sin6_port = htons(port);
100 addr6.sin6_addr = in6addr_any;
101 addr = (struct sockaddr*)&addr6;
110 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
111 fatal("socket: %s", strerror(errno));
114 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
115 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
118 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
119 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
123 if (bind(sock, addr, len) == -1)
124 fatal("bind: %s", strerror(errno));
126 if (listen(sock, 16) == -1)
127 fatal("listen: %s", strerror(errno));
133 add_keypair(struct vhost *h)
135 if (*h->ocsp == '\0') {
136 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
137 fatal("failed to load the keypair (%s, %s)",
140 if (tls_config_add_keypair_ocsp_file(tlsconf, h->cert, h->key,
142 fatal("failed to load the keypair (%s, %s, %s)",
143 h->cert, h->key, h->ocsp);
152 if ((tlsconf = tls_config_new()) == NULL)
153 fatal("tls_config_new");
155 /* optionally accept client certs, but don't try to verify them */
156 tls_config_verify_client_optional(tlsconf);
157 tls_config_insecure_noverifycert(tlsconf);
159 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
160 fatal("tls_config_set_protocols");
162 if ((ctx = tls_server()) == NULL)
163 fatal("tls_server failure");
165 h = TAILQ_FIRST(&hosts);
167 /* we need to set something, then we can add how many key we want */
168 if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
169 fatal("tls_config_set_keypair_file failed for (%s, %s)",
173 if (*h->ocsp != '\0' &&
174 tls_config_set_ocsp_staple_file(tlsconf, h->ocsp) == -1)
175 fatal("tls_config_set_ocsp_staple_file failed for (%s)",
178 while ((h = TAILQ_NEXT(h, vhosts)) != NULL)
181 if (tls_configure(ctx, tlsconf) == -1)
182 fatal("tls_configure: %s", tls_error(ctx));
186 listener_main(struct imsgbuf *ibuf)
189 if (load_default_mime(&conf.mime) == -1)
190 fatal("load_default_mime: %s", strerror(errno));
191 sort_mime(&conf.mime);
193 loop(ctx, sock4, sock6, ibuf);
204 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
206 init_mime(&conf.mime);
214 struct vhost *h, *th;
215 struct location *l, *tl;
216 struct proxy *p, *tp;
217 struct envlist *e, *te;
218 struct alist *a, *ta;
223 free_mime(&conf.mime);
224 memset(&conf, 0, sizeof(conf));
228 TAILQ_FOREACH_SAFE(h, &hosts, vhosts, th) {
229 TAILQ_FOREACH_SAFE(l, &h->locations, locations, tl) {
230 TAILQ_REMOVE(&h->locations, l, locations);
238 TAILQ_FOREACH_SAFE(e, &h->params, envs, te) {
239 TAILQ_REMOVE(&h->params, e, envs);
243 TAILQ_FOREACH_SAFE(a, &h->aliases, aliases, ta) {
244 TAILQ_REMOVE(&h->aliases, a, aliases);
248 TAILQ_FOREACH_SAFE(p, &h->proxies, proxies, tp) {
249 TAILQ_REMOVE(&h->proxies, p, proxies);
250 tls_unload_file(p->cert, p->certlen);
251 tls_unload_file(p->key, p->keylen);
255 TAILQ_REMOVE(&hosts, h, vhosts);
259 memset(fcgi, 0, sizeof(fcgi));
262 tls_config_free(tlsconf);
272 sigaddset(&mask, SIGHUP);
273 sigaddset(&mask, SIGINT);
274 sigaddset(&mask, SIGTERM);
275 sigwait(&mask, &signo);
277 return signo == SIGHUP;
283 struct passwd *pw = NULL;
285 if (*conf.chroot != '\0' && *conf.user == '\0')
286 fatal("can't chroot without an user to switch to after.");
288 if (*conf.user != '\0') {
289 if ((pw = getpwnam(conf.user)) == NULL)
290 fatal("can't find user %s", conf.user);
293 if (*conf.chroot != '\0') {
294 if (chroot(conf.chroot) != 0 || chdir("/") != 0)
295 fatal("%s: %s", conf.chroot, strerror(errno));
299 if (setgroups(1, &pw->pw_gid) == -1 ||
300 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
301 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
302 fatal("cannot drop privileges");
306 log_warn(NULL, "not a good idea to run a network daemon as root");
313 "Version: " GMID_STRING "\n"
314 "Usage: %s [-dhnVv] [-D macro=value] [-f config] [-P pidfile]\n",
323 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
324 err(1, "socketpair");
330 signal(SIGHUP, SIG_IGN);
332 setproctitle("logger");
333 imsg_init(&logibuf, p[1]);
335 _exit(logger_main(p[1], &logibuf));
338 imsg_init(&logibuf, p[0]);
348 /* setup tls before dropping privileges: we don't want user
349 * to put private certs inside the chroot. */
352 for (i = 0; i < conf.prefork; ++i) {
353 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
355 fatal("socketpair: %s", strerror(errno));
359 fatal("fork: %s", strerror(errno));
362 imsg_init(&servibuf[i], p[1]);
363 setproctitle("server");
364 _exit(listener_main(&servibuf[i]));
367 imsg_init(&servibuf[i], p[0]);
373 write_pidfile(const char *pidfile)
381 if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
382 fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
386 lock.l_type = F_WRLCK;
387 lock.l_whence = SEEK_SET;
389 if (fcntl(fd, F_SETLK, &lock) == -1)
390 fatal("can't lock %s, gmid is already running?", pidfile);
392 if (ftruncate(fd, 0) == -1)
393 fatal("ftruncate: %s: %s", pidfile, strerror(errno));
395 dprintf(fd, "%d\n", getpid());
401 main(int argc, char **argv)
403 int i, ch, conftest = 0;
404 int pidfd, old_ipv6, old_port;
409 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
412 if (cmdline_symset(optarg) == -1)
413 fatal("could not parse macro definition: %s",
420 config_path = absolutify_path(optarg);
432 puts("Version: " GMID_STRING);
448 parse_conf(config_path);
451 fprintf(stderr, "config OK\n");
457 if (!conf.foreground) {
459 imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0);
460 imsg_flush(&logibuf);
462 if (daemon(1, 1) == -1)
463 fatal("daemon: %s", strerror(errno));
466 sock4 = make_socket(conf.port, AF_INET);
469 sock6 = make_socket(conf.port, AF_INET6);
471 signal(SIGPIPE, SIG_IGN);
473 pidfd = write_pidfile(pidfile);
476 * Linux seems to call the event handlers even when we're
477 * doing a sigwait. These dummy handlers are here to avoid
478 * being terminated on SIGHUP, SIGINT or SIGTERM.
480 signal(SIGHUP, dummy_handler);
481 signal(SIGINT, dummy_handler);
482 signal(SIGTERM, dummy_handler);
484 /* wait a sighup and reload the daemon */
491 log_info(NULL, "reloading configuration %s", config_path);
493 /* close the servers */
494 for (i = 0; i < conf.prefork; ++i) {
495 imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
496 imsg_flush(&servibuf[i]);
497 close(servibuf[i].fd);
500 old_ipv6 = conf.ipv6;
501 old_port = conf.port;
505 parse_conf(config_path);
507 if (old_port != conf.port) {
514 if (sock6 != -1 && old_ipv6 != conf.ipv6) {
520 sock4 = make_socket(conf.port, AF_INET);
521 if (sock6 == -1 && conf.ipv6)
522 sock6 = make_socket(conf.port, AF_INET6);
525 for (i = 0; i < conf.prefork; ++i) {
526 imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
527 imsg_flush(&servibuf[i]);
528 close(servibuf[i].fd);
531 imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
532 imsg_flush(&logibuf);