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.
31 static const char *opts = "D:df:hnP:Vv";
33 static const struct option longopts[] = {
34 {"help", no_argument, NULL, 'h'},
35 {"version", no_argument, NULL, 'V'},
39 struct fcgi fcgi[FCGI_MAX];
41 struct vhosthead hosts;
45 struct imsgbuf logibuf, servibuf[PROC_MAX];
47 const char *config_path = "/etc/gmid.conf";
52 struct tls_config *tlsconf;
56 dummy_handler(int signo)
67 TAILQ_FOREACH(h, &hosts, vhosts) {
68 TAILQ_FOREACH(l, &h->locations, locations) {
71 if ((l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY)) == -1)
72 fatal("open %s for domain %s: %s", l->dir, h->domain,
79 make_socket(int port, int family)
82 struct sockaddr_in addr4;
83 struct sockaddr_in6 addr6;
84 struct sockaddr *addr;
89 memset(&addr4, 0, sizeof(addr4));
90 addr4.sin_family = family;
91 addr4.sin_port = htons(port);
92 addr4.sin_addr.s_addr = INADDR_ANY;
93 addr = (struct sockaddr*)&addr4;
98 memset(&addr6, 0, sizeof(addr6));
99 addr6.sin6_family = AF_INET6;
100 addr6.sin6_port = htons(port);
101 addr6.sin6_addr = in6addr_any;
102 addr = (struct sockaddr*)&addr6;
111 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
112 fatal("socket: %s", strerror(errno));
115 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
116 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
119 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
120 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
124 if (bind(sock, addr, len) == -1)
125 fatal("bind: %s", strerror(errno));
127 if (listen(sock, 16) == -1)
128 fatal("listen: %s", strerror(errno));
134 add_keypair(struct vhost *h)
136 if (*h->ocsp == '\0') {
137 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
138 fatal("failed to load the keypair (%s, %s)",
141 if (tls_config_add_keypair_ocsp_file(tlsconf, h->cert, h->key,
143 fatal("failed to load the keypair (%s, %s, %s)",
144 h->cert, h->key, h->ocsp);
153 if ((tlsconf = tls_config_new()) == NULL)
154 fatal("tls_config_new");
156 /* optionally accept client certs, but don't try to verify them */
157 tls_config_verify_client_optional(tlsconf);
158 tls_config_insecure_noverifycert(tlsconf);
160 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
161 fatal("tls_config_set_protocols");
163 if ((ctx = tls_server()) == NULL)
164 fatal("tls_server failure");
166 h = TAILQ_FIRST(&hosts);
168 /* we need to set something, then we can add how many key we want */
169 if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
170 fatal("tls_config_set_keypair_file failed for (%s, %s)",
174 if (*h->ocsp != '\0' &&
175 tls_config_set_ocsp_staple_file(tlsconf, h->ocsp) == -1)
176 fatal("tls_config_set_ocsp_staple_file failed for (%s)",
179 while ((h = TAILQ_NEXT(h, vhosts)) != NULL)
182 if (tls_configure(ctx, tlsconf) == -1)
183 fatal("tls_configure: %s", tls_error(ctx));
187 listener_main(struct imsgbuf *ibuf)
190 if (load_default_mime(&conf.mime) == -1)
191 fatal("load_default_mime: %s", strerror(errno));
192 sort_mime(&conf.mime);
194 loop(ctx, sock4, sock6, ibuf);
205 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
207 init_mime(&conf.mime);
215 struct vhost *h, *th;
216 struct location *l, *tl;
217 struct proxy *p, *tp;
218 struct envlist *e, *te;
219 struct alist *a, *ta;
224 free_mime(&conf.mime);
225 memset(&conf, 0, sizeof(conf));
229 TAILQ_FOREACH_SAFE(h, &hosts, vhosts, th) {
230 TAILQ_FOREACH_SAFE(l, &h->locations, locations, tl) {
231 TAILQ_REMOVE(&h->locations, l, locations);
239 TAILQ_FOREACH_SAFE(e, &h->params, envs, te) {
240 TAILQ_REMOVE(&h->params, e, envs);
244 TAILQ_FOREACH_SAFE(a, &h->aliases, aliases, ta) {
245 TAILQ_REMOVE(&h->aliases, a, aliases);
249 TAILQ_FOREACH_SAFE(p, &h->proxies, proxies, tp) {
250 TAILQ_REMOVE(&h->proxies, p, proxies);
251 tls_unload_file(p->cert, p->certlen);
252 tls_unload_file(p->key, p->keylen);
256 TAILQ_REMOVE(&hosts, h, vhosts);
260 memset(fcgi, 0, sizeof(fcgi));
263 tls_config_free(tlsconf);
273 sigaddset(&mask, SIGHUP);
274 sigaddset(&mask, SIGINT);
275 sigaddset(&mask, SIGTERM);
276 sigwait(&mask, &signo);
278 return signo == SIGHUP;
284 struct passwd *pw = NULL;
286 if (*conf.chroot != '\0' && *conf.user == '\0')
287 fatal("can't chroot without an user to switch to after.");
289 if (*conf.user != '\0') {
290 if ((pw = getpwnam(conf.user)) == NULL)
291 fatal("can't find user %s", conf.user);
294 if (*conf.chroot != '\0') {
295 if (chroot(conf.chroot) != 0 || chdir("/") != 0)
296 fatal("%s: %s", conf.chroot, strerror(errno));
300 if (setgroups(1, &pw->pw_gid) == -1 ||
301 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
302 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
303 fatal("cannot drop privileges");
307 log_warn(NULL, "not a good idea to run a network daemon as root");
314 "Version: " GMID_STRING "\n"
315 "Usage: %s [-dhnVv] [-D macro=value] [-f config] [-P pidfile]\n",
324 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
325 err(1, "socketpair");
331 signal(SIGHUP, SIG_IGN);
333 setproctitle("logger");
334 imsg_init(&logibuf, p[1]);
336 _exit(logger_main(p[1], &logibuf));
339 imsg_init(&logibuf, p[0]);
349 /* setup tls before dropping privileges: we don't want user
350 * to put private certs inside the chroot. */
353 for (i = 0; i < conf.prefork; ++i) {
354 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
356 fatal("socketpair: %s", strerror(errno));
360 fatal("fork: %s", strerror(errno));
363 imsg_init(&servibuf[i], p[1]);
364 setproctitle("server");
365 _exit(listener_main(&servibuf[i]));
368 imsg_init(&servibuf[i], p[0]);
374 write_pidfile(const char *pidfile)
382 if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
383 fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
387 lock.l_type = F_WRLCK;
388 lock.l_whence = SEEK_SET;
390 if (fcntl(fd, F_SETLK, &lock) == -1)
391 fatal("can't lock %s, gmid is already running?", pidfile);
393 if (ftruncate(fd, 0) == -1)
394 fatal("ftruncate: %s: %s", pidfile, strerror(errno));
396 dprintf(fd, "%d\n", getpid());
402 main(int argc, char **argv)
404 int i, ch, conftest = 0;
405 int pidfd, old_ipv6, old_port;
410 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
413 if (cmdline_symset(optarg) == -1)
414 fatal("could not parse macro definition: %s",
421 config_path = absolutify_path(optarg);
433 puts("Version: " GMID_STRING);
449 parse_conf(config_path);
452 fprintf(stderr, "config OK\n");
458 if (!conf.foreground) {
460 imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0);
461 imsg_flush(&logibuf);
463 if (daemon(1, 1) == -1)
464 fatal("daemon: %s", strerror(errno));
467 sock4 = make_socket(conf.port, AF_INET);
470 sock6 = make_socket(conf.port, AF_INET6);
472 signal(SIGPIPE, SIG_IGN);
474 pidfd = write_pidfile(pidfile);
477 * Linux seems to call the event handlers even when we're
478 * doing a sigwait. These dummy handlers are here to avoid
479 * being terminated on SIGHUP, SIGINT or SIGTERM.
481 signal(SIGHUP, dummy_handler);
482 signal(SIGINT, dummy_handler);
483 signal(SIGTERM, dummy_handler);
485 /* wait a sighup and reload the daemon */
492 log_info(NULL, "reloading configuration %s", config_path);
494 /* close the servers */
495 for (i = 0; i < conf.prefork; ++i) {
496 imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
497 imsg_flush(&servibuf[i]);
498 close(servibuf[i].fd);
501 old_ipv6 = conf.ipv6;
502 old_port = conf.port;
506 parse_conf(config_path);
508 if (old_port != conf.port) {
515 if (sock6 != -1 && old_ipv6 != conf.ipv6) {
521 sock4 = make_socket(conf.port, AF_INET);
522 if (sock6 == -1 && conf.ipv6)
523 sock6 = make_socket(conf.port, AF_INET6);
526 for (i = 0; i < conf.prefork; ++i) {
527 imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
528 imsg_flush(&servibuf[i]);
529 close(servibuf[i].fd);
532 imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
533 imsg_flush(&logibuf);