2 a555e0d6 2022-07-04 op * Copyright (c) 2020, 2021, 2022 Omar Polo <op@omarpolo.com>
4 3e4749f7 2020-10-02 op * Permission to use, copy, modify, and distribute this software for any
5 3e4749f7 2020-10-02 op * purpose with or without fee is hereby granted, provided that the above
6 3e4749f7 2020-10-02 op * copyright notice and this permission notice appear in all copies.
8 3e4749f7 2020-10-02 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3e4749f7 2020-10-02 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3e4749f7 2020-10-02 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3e4749f7 2020-10-02 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3e4749f7 2020-10-02 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3e4749f7 2020-10-02 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3e4749f7 2020-10-02 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 52418c8d 2021-02-12 op #include "gmid.h"
19 8443bff7 2021-01-25 op #include <sys/stat.h>
21 592fd624 2020-10-07 op #include <errno.h>
22 3e4749f7 2020-10-02 op #include <fcntl.h>
23 5777923b 2021-06-29 op #include <getopt.h>
24 7e1df73d 2021-03-31 op #include <libgen.h>
25 bcf5d929 2021-02-01 op #include <limits.h>
26 ae08ec7d 2021-01-25 op #include <pwd.h>
27 0cf902af 2020-11-03 op #include <signal.h>
28 3e4749f7 2020-10-02 op #include <string.h>
30 aae8f6bf 2022-09-08 op static const char *opts = "D:df:hnP:Vv";
32 e5d82d94 2022-03-19 op static const struct option longopts[] = {
33 5777923b 2021-06-29 op {"help", no_argument, NULL, 'h'},
34 5777923b 2021-06-29 op {"version", no_argument, NULL, 'V'},
35 5777923b 2021-06-29 op {NULL, 0, NULL, 0},
38 8ad1c570 2021-05-09 op struct fcgi fcgi[FCGI_MAX];
40 b8e64ccd 2021-03-31 op struct vhosthead hosts;
42 bc99d868 2021-03-19 op int sock4, sock6;
44 5c485529 2022-09-10 op struct imsgbuf logibuf, servibuf[PROC_MAX];
46 32fbc478 2022-09-08 op const char *config_path = "/etc/gmid.conf";
47 32fbc478 2022-09-08 op const char *pidfile;
49 15902770 2021-01-15 op struct conf conf;
51 ca21e100 2021-02-04 op struct tls_config *tlsconf;
52 ae08ec7d 2021-01-25 op struct tls *ctx;
55 b9c9123b 2021-03-20 op dummy_handler(int signo)
61 ae08ec7d 2021-01-25 op load_vhosts(void)
63 fdea6aa0 2021-04-30 op struct vhost *h;
64 fdea6aa0 2021-04-30 op struct location *l;
66 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
67 fdea6aa0 2021-04-30 op TAILQ_FOREACH(l, &h->locations, locations) {
68 534afd0d 2022-10-05 op if (*l->dir == '\0')
70 fdea6aa0 2021-04-30 op if ((l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY)) == -1)
71 807869c1 2021-10-07 op fatal("open %s for domain %s: %s", l->dir, h->domain,
72 807869c1 2021-10-07 op strerror(errno));
78 9468027b 2020-10-15 op make_socket(int port, int family)
81 9468027b 2020-10-15 op struct sockaddr_in addr4;
82 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
83 9468027b 2020-10-15 op struct sockaddr *addr;
84 9468027b 2020-10-15 op socklen_t len;
86 81e0f000 2021-09-24 op switch (family) {
88 df0c2926 2021-09-24 op memset(&addr4, 0, sizeof(addr4));
89 9468027b 2020-10-15 op addr4.sin_family = family;
90 9468027b 2020-10-15 op addr4.sin_port = htons(port);
91 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
92 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
93 9468027b 2020-10-15 op len = sizeof(addr4);
96 9468027b 2020-10-15 op case AF_INET6:
97 df0c2926 2021-09-24 op memset(&addr6, 0, sizeof(addr6));
98 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
99 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
100 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
101 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
102 9468027b 2020-10-15 op len = sizeof(addr6);
106 9468027b 2020-10-15 op /* unreachable */
110 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
111 80bbcad5 2021-01-10 op fatal("socket: %s", strerror(errno));
114 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
115 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
118 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
119 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
121 592fd624 2020-10-07 op mark_nonblock(sock);
123 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
124 80bbcad5 2021-01-10 op fatal("bind: %s", strerror(errno));
126 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
127 80bbcad5 2021-01-10 op fatal("listen: %s", strerror(errno));
133 ff05125e 2021-10-15 op add_keypair(struct vhost *h)
135 534afd0d 2022-10-05 op if (*h->ocsp == '\0') {
136 ff05125e 2021-10-15 op if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
137 ff05125e 2021-10-15 op fatal("failed to load the keypair (%s, %s)",
138 ff05125e 2021-10-15 op h->cert, h->key);
140 7fa67176 2021-10-15 op if (tls_config_add_keypair_ocsp_file(tlsconf, h->cert, h->key,
141 7fa67176 2021-10-15 op h->ocsp) == -1)
142 ff05125e 2021-10-15 op fatal("failed to load the keypair (%s, %s, %s)",
143 ff05125e 2021-10-15 op h->cert, h->key, h->ocsp);
148 ae08ec7d 2021-01-25 op setup_tls(void)
150 ae08ec7d 2021-01-25 op struct vhost *h;
152 881a9dd9 2021-01-16 op if ((tlsconf = tls_config_new()) == NULL)
153 132cae8c 2021-01-18 op fatal("tls_config_new");
155 881a9dd9 2021-01-16 op /* optionally accept client certs, but don't try to verify them */
156 881a9dd9 2021-01-16 op tls_config_verify_client_optional(tlsconf);
157 881a9dd9 2021-01-16 op tls_config_insecure_noverifycert(tlsconf);
159 881a9dd9 2021-01-16 op if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
160 132cae8c 2021-01-18 op fatal("tls_config_set_protocols");
162 881a9dd9 2021-01-16 op if ((ctx = tls_server()) == NULL)
163 132cae8c 2021-01-18 op fatal("tls_server failure");
165 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
167 ae08ec7d 2021-01-25 op /* we need to set something, then we can add how many key we want */
168 b8e64ccd 2021-03-31 op if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
169 ca21e100 2021-02-04 op fatal("tls_config_set_keypair_file failed for (%s, %s)",
170 b8e64ccd 2021-03-31 op h->cert, h->key);
172 7fa67176 2021-10-15 op /* same for OCSP */
173 534afd0d 2022-10-05 op if (*h->ocsp != '\0' &&
174 ff05125e 2021-10-15 op tls_config_set_ocsp_staple_file(tlsconf, h->ocsp) == -1)
175 ff05125e 2021-10-15 op fatal("tls_config_set_ocsp_staple_file failed for (%s)",
178 ff05125e 2021-10-15 op while ((h = TAILQ_NEXT(h, vhosts)) != NULL)
179 ff05125e 2021-10-15 op add_keypair(h);
181 881a9dd9 2021-01-16 op if (tls_configure(ctx, tlsconf) == -1)
182 132cae8c 2021-01-18 op fatal("tls_configure: %s", tls_error(ctx));
186 bc99d868 2021-03-19 op listener_main(struct imsgbuf *ibuf)
189 cd5826b8 2022-09-10 op if (load_default_mime(&conf.mime) == -1)
190 d8d170aa 2022-04-08 op fatal("load_default_mime: %s", strerror(errno));
191 18bd8391 2022-04-08 op sort_mime(&conf.mime);
192 ae08ec7d 2021-01-25 op load_vhosts();
193 bc99d868 2021-03-19 op loop(ctx, sock4, sock6, ibuf);
198 8d6ae384 2021-01-24 op init_config(void)
200 b8e64ccd 2021-03-31 op TAILQ_INIT(&hosts);
202 15902770 2021-01-15 op conf.port = 1965;
203 15902770 2021-01-15 op conf.ipv6 = 0;
204 5bc3c98e 2021-01-15 op conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
206 b2a6b613 2021-01-21 op init_mime(&conf.mime);
208 a709ddf5 2021-02-07 op conf.prefork = 3;
212 ca21e100 2021-02-04 op free_config(void)
214 b8e64ccd 2021-03-31 op struct vhost *h, *th;
215 b8e64ccd 2021-03-31 op struct location *l, *tl;
216 b7967bc1 2021-01-02 op struct proxy *p, *tp;
217 9cc630aa 2021-04-28 op struct envlist *e, *te;
218 cc8c2901 2021-04-29 op struct alist *a, *ta;
221 419a4235 2021-04-28 op v = conf.verbose;
223 d8d170aa 2022-04-08 op free_mime(&conf.mime);
224 ca21e100 2021-02-04 op memset(&conf, 0, sizeof(conf));
226 419a4235 2021-04-28 op conf.verbose = v;
228 b8e64ccd 2021-03-31 op TAILQ_FOREACH_SAFE(h, &hosts, vhosts, th) {
229 b8e64ccd 2021-03-31 op TAILQ_FOREACH_SAFE(l, &h->locations, locations, tl) {
230 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&h->locations, l, locations);
232 fdea6aa0 2021-04-30 op if (l->dirfd != -1)
233 fdea6aa0 2021-04-30 op close(l->dirfd);
238 ab1e0169 2021-06-12 op TAILQ_FOREACH_SAFE(e, &h->params, envs, te) {
239 ab1e0169 2021-06-12 op TAILQ_REMOVE(&h->params, e, envs);
243 cc8c2901 2021-04-29 op TAILQ_FOREACH_SAFE(a, &h->aliases, aliases, ta) {
244 3b33eab3 2021-06-12 op TAILQ_REMOVE(&h->aliases, a, aliases);
248 b7967bc1 2021-01-02 op TAILQ_FOREACH_SAFE(p, &h->proxies, proxies, tp) {
249 b7967bc1 2021-01-02 op TAILQ_REMOVE(&h->proxies, p, proxies);
250 b7967bc1 2021-01-02 op tls_unload_file(p->cert, p->certlen);
251 b7967bc1 2021-01-02 op tls_unload_file(p->key, p->keylen);
255 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&hosts, h, vhosts);
259 534afd0d 2022-10-05 op memset(fcgi, 0, sizeof(fcgi));
261 ca21e100 2021-02-04 op tls_free(ctx);
262 ca21e100 2021-02-04 op tls_config_free(tlsconf);
266 1d3eb470 2021-03-20 op wait_signal(void)
268 ca21e100 2021-02-04 op sigset_t mask;
271 ca21e100 2021-02-04 op sigemptyset(&mask);
272 ca21e100 2021-02-04 op sigaddset(&mask, SIGHUP);
273 1d3eb470 2021-03-20 op sigaddset(&mask, SIGINT);
274 1d3eb470 2021-03-20 op sigaddset(&mask, SIGTERM);
275 ca21e100 2021-02-04 op sigwait(&mask, &signo);
277 1d3eb470 2021-03-20 op return signo == SIGHUP;
281 ae08ec7d 2021-01-25 op drop_priv(void)
283 ae08ec7d 2021-01-25 op struct passwd *pw = NULL;
285 7277bb7d 2022-09-10 op if (*conf.chroot != '\0' && *conf.user == '\0')
286 ae08ec7d 2021-01-25 op fatal("can't chroot without an user to switch to after.");
288 7277bb7d 2022-09-10 op if (*conf.user != '\0') {
289 ae08ec7d 2021-01-25 op if ((pw = getpwnam(conf.user)) == NULL)
290 ae08ec7d 2021-01-25 op fatal("can't find user %s", conf.user);
293 7277bb7d 2022-09-10 op if (*conf.chroot != '\0') {
294 ae08ec7d 2021-01-25 op if (chroot(conf.chroot) != 0 || chdir("/") != 0)
295 ae08ec7d 2021-01-25 op fatal("%s: %s", conf.chroot, strerror(errno));
298 ae08ec7d 2021-01-25 op if (pw != NULL) {
299 872a7176 2022-11-27 op if (setgroups(1, &pw->pw_gid) == -1 ||
300 872a7176 2022-11-27 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
301 872a7176 2022-11-27 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
302 872a7176 2022-11-27 op fatal("cannot drop privileges");
305 ae08ec7d 2021-01-25 op if (getuid() == 0)
306 3abf91b0 2021-02-07 op log_warn(NULL, "not a good idea to run a network daemon as root");
312 3abf91b0 2021-02-07 op fprintf(stderr,
313 0be2a537 2021-06-29 op "Version: " GMID_STRING "\n"
314 aae8f6bf 2022-09-08 op "Usage: %s [-dhnVv] [-D macro=value] [-f config] [-P pidfile]\n",
315 9327bc04 2021-06-29 op getprogname());
319 376a5407 2021-02-23 op logger_init(void)
323 376a5407 2021-02-23 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
324 376a5407 2021-02-23 op err(1, "socketpair");
326 376a5407 2021-02-23 op switch (fork()) {
328 376a5407 2021-02-23 op err(1, "fork");
330 bc99d868 2021-03-19 op signal(SIGHUP, SIG_IGN);
332 376a5407 2021-02-23 op setproctitle("logger");
333 bc99d868 2021-03-19 op imsg_init(&logibuf, p[1]);
335 bc99d868 2021-03-19 op _exit(logger_main(p[1], &logibuf));
338 bc99d868 2021-03-19 op imsg_init(&logibuf, p[0]);
348 d672b8fb 2021-02-03 op /* setup tls before dropping privileges: we don't want user
349 d672b8fb 2021-02-03 op * to put private certs inside the chroot. */
352 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
353 2c3e53da 2021-03-03 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
354 2c3e53da 2021-03-03 op PF_UNSPEC, p) == -1)
355 2c3e53da 2021-03-03 op fatal("socketpair: %s", strerror(errno));
357 2c3e53da 2021-03-03 op switch (fork()) {
359 2c3e53da 2021-03-03 op fatal("fork: %s", strerror(errno));
360 2c3e53da 2021-03-03 op case 0: /* child */
362 5c485529 2022-09-10 op imsg_init(&servibuf[i], p[1]);
363 2c3e53da 2021-03-03 op setproctitle("server");
364 5c485529 2022-09-10 op _exit(listener_main(&servibuf[i]));
367 bc99d868 2021-03-19 op imsg_init(&servibuf[i], p[0]);
373 419a4235 2021-04-28 op write_pidfile(const char *pidfile)
375 419a4235 2021-04-28 op struct flock lock;
378 419a4235 2021-04-28 op if (pidfile == NULL)
381 419a4235 2021-04-28 op if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
382 419a4235 2021-04-28 op fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
384 419a4235 2021-04-28 op lock.l_start = 0;
385 419a4235 2021-04-28 op lock.l_len = 0;
386 419a4235 2021-04-28 op lock.l_type = F_WRLCK;
387 419a4235 2021-04-28 op lock.l_whence = SEEK_SET;
389 419a4235 2021-04-28 op if (fcntl(fd, F_SETLK, &lock) == -1)
390 419a4235 2021-04-28 op fatal("can't lock %s, gmid is already running?", pidfile);
392 419a4235 2021-04-28 op if (ftruncate(fd, 0) == -1)
393 419a4235 2021-04-28 op fatal("ftruncate: %s: %s", pidfile, strerror(errno));
395 419a4235 2021-04-28 op dprintf(fd, "%d\n", getpid());
401 8d6ae384 2021-01-24 op main(int argc, char **argv)
403 32fbc478 2022-09-08 op int i, ch, conftest = 0;
404 8e8b2e25 2021-04-28 op int pidfd, old_ipv6, old_port;
406 8a50fc03 2021-07-07 op logger_init();
407 501e489c 2021-01-24 op init_config();
409 5777923b 2021-06-29 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
410 3e4749f7 2020-10-02 op switch (ch) {
412 f98e9045 2021-06-29 op if (cmdline_symset(optarg) == -1)
413 8a50fc03 2021-07-07 op fatal("could not parse macro definition: %s",
417 3abf91b0 2021-02-07 op conf.foreground = 1;
420 aae8f6bf 2022-09-08 op config_path = absolutify_path(optarg);
429 8e8b2e25 2021-04-28 op pidfile = optarg;
432 fdb43a4c 2021-06-29 op puts("Version: " GMID_STRING);
435 3abf91b0 2021-02-07 op conf.verbose++;
442 8443bff7 2021-01-25 op argc -= optind;
443 8443bff7 2021-01-25 op argv += optind;
445 32fbc478 2022-09-08 op if (argc != 0)
448 32fbc478 2022-09-08 op parse_conf(config_path);
450 132cae8c 2021-01-18 op if (conftest) {
451 f0a01fc7 2021-10-09 op fprintf(stderr, "config OK\n");
452 f0a01fc7 2021-10-09 op if (conftest > 1)
453 f0a01fc7 2021-10-09 op print_conf();
457 32fbc478 2022-09-08 op if (!conf.foreground) {
458 8a50fc03 2021-07-07 op /* log to syslog */
459 8a50fc03 2021-07-07 op imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0);
460 8a50fc03 2021-07-07 op imsg_flush(&logibuf);
462 0170ba02 2021-01-17 op if (daemon(1, 1) == -1)
463 8a50fc03 2021-07-07 op fatal("daemon: %s", strerror(errno));
466 d672b8fb 2021-02-03 op sock4 = make_socket(conf.port, AF_INET);
468 d672b8fb 2021-02-03 op if (conf.ipv6)
469 d672b8fb 2021-02-03 op sock6 = make_socket(conf.port, AF_INET6);
471 3841a369 2021-04-20 op signal(SIGPIPE, SIG_IGN);
473 8e8b2e25 2021-04-28 op pidfd = write_pidfile(pidfile);
476 a8a1f439 2021-07-07 op * Linux seems to call the event handlers even when we're
477 1b333122 2021-04-26 op * doing a sigwait. These dummy handlers are here to avoid
478 a8a1f439 2021-07-07 op * being terminated on SIGHUP, SIGINT or SIGTERM.
480 b9c9123b 2021-03-20 op signal(SIGHUP, dummy_handler);
481 b9c9123b 2021-03-20 op signal(SIGINT, dummy_handler);
482 b9c9123b 2021-03-20 op signal(SIGTERM, dummy_handler);
484 ca21e100 2021-02-04 op /* wait a sighup and reload the daemon */
488 32fbc478 2022-09-08 op if (!wait_signal())
491 3abf91b0 2021-02-07 op log_info(NULL, "reloading configuration %s", config_path);
493 d29a2ee2 2022-09-06 op /* close the servers */
494 d29a2ee2 2022-09-06 op for (i = 0; i < conf.prefork; ++i) {
495 d29a2ee2 2022-09-06 op imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
496 d29a2ee2 2022-09-06 op imsg_flush(&servibuf[i]);
497 d29a2ee2 2022-09-06 op close(servibuf[i].fd);
500 ca21e100 2021-02-04 op old_ipv6 = conf.ipv6;
501 ca21e100 2021-02-04 op old_port = conf.port;
503 ca21e100 2021-02-04 op free_config();
504 ca21e100 2021-02-04 op init_config();
505 ca21e100 2021-02-04 op parse_conf(config_path);
507 ca21e100 2021-02-04 op if (old_port != conf.port) {
508 ca21e100 2021-02-04 op close(sock4);
509 ca21e100 2021-02-04 op close(sock6);
514 ca21e100 2021-02-04 op if (sock6 != -1 && old_ipv6 != conf.ipv6) {
515 ca21e100 2021-02-04 op close(sock6);
519 ca21e100 2021-02-04 op if (sock4 == -1)
520 ca21e100 2021-02-04 op sock4 = make_socket(conf.port, AF_INET);
521 ca21e100 2021-02-04 op if (sock6 == -1 && conf.ipv6)
522 ca21e100 2021-02-04 op sock6 = make_socket(conf.port, AF_INET6);
525 5c485529 2022-09-10 op for (i = 0; i < conf.prefork; ++i) {
526 5c485529 2022-09-10 op imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
527 5c485529 2022-09-10 op imsg_flush(&servibuf[i]);
528 5c485529 2022-09-10 op close(servibuf[i].fd);
531 1d3eb470 2021-03-20 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
532 1d3eb470 2021-03-20 op imsg_flush(&logibuf);
533 5c485529 2022-09-10 op close(logibuf.fd);
535 8e8b2e25 2021-04-28 op if (pidfd != -1)
536 8e8b2e25 2021-04-28 op close(pidfd);