Blame


1 3e4749f7 2020-10-02 op /*
2 a555e0d6 2022-07-04 op * Copyright (c) 2020, 2021, 2022 Omar Polo <op@omarpolo.com>
3 3e4749f7 2020-10-02 op *
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.
7 3e4749f7 2020-10-02 op *
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.
15 3e4749f7 2020-10-02 op */
16 52418c8d 2021-02-12 op
17 52418c8d 2021-02-12 op #include "gmid.h"
18 3e4749f7 2020-10-02 op
19 8443bff7 2021-01-25 op #include <sys/stat.h>
20 8443bff7 2021-01-25 op
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 c9e97a6e 2022-12-24 op #include <grp.h>
27 ae08ec7d 2021-01-25 op #include <pwd.h>
28 0cf902af 2020-11-03 op #include <signal.h>
29 3e4749f7 2020-10-02 op #include <string.h>
30 cc68fe70 2020-10-07 op
31 aae8f6bf 2022-09-08 op static const char *opts = "D:df:hnP:Vv";
32 5777923b 2021-06-29 op
33 e5d82d94 2022-03-19 op static const struct option longopts[] = {
34 5777923b 2021-06-29 op {"help", no_argument, NULL, 'h'},
35 5777923b 2021-06-29 op {"version", no_argument, NULL, 'V'},
36 5777923b 2021-06-29 op {NULL, 0, NULL, 0},
37 5777923b 2021-06-29 op };
38 5777923b 2021-06-29 op
39 8ad1c570 2021-05-09 op struct fcgi fcgi[FCGI_MAX];
40 8ad1c570 2021-05-09 op
41 b8e64ccd 2021-03-31 op struct vhosthead hosts;
42 15902770 2021-01-15 op
43 bc99d868 2021-03-19 op int sock4, sock6;
44 d672b8fb 2021-02-03 op
45 5c485529 2022-09-10 op struct imsgbuf logibuf, servibuf[PROC_MAX];
46 376a5407 2021-02-23 op
47 32fbc478 2022-09-08 op const char *config_path = "/etc/gmid.conf";
48 32fbc478 2022-09-08 op const char *pidfile;
49 881a9dd9 2021-01-16 op
50 15902770 2021-01-15 op struct conf conf;
51 15902770 2021-01-15 op
52 ca21e100 2021-02-04 op struct tls_config *tlsconf;
53 ae08ec7d 2021-01-25 op struct tls *ctx;
54 0be51733 2021-01-20 op
55 b9c9123b 2021-03-20 op static void
56 b9c9123b 2021-03-20 op dummy_handler(int signo)
57 b9c9123b 2021-03-20 op {
58 b9c9123b 2021-03-20 op return;
59 b9c9123b 2021-03-20 op }
60 b9c9123b 2021-03-20 op
61 8443bff7 2021-01-25 op void
62 ae08ec7d 2021-01-25 op load_vhosts(void)
63 3e4749f7 2020-10-02 op {
64 fdea6aa0 2021-04-30 op struct vhost *h;
65 fdea6aa0 2021-04-30 op struct location *l;
66 15902770 2021-01-15 op
67 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
68 fdea6aa0 2021-04-30 op TAILQ_FOREACH(l, &h->locations, locations) {
69 534afd0d 2022-10-05 op if (*l->dir == '\0')
70 fdea6aa0 2021-04-30 op continue;
71 fdea6aa0 2021-04-30 op if ((l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY)) == -1)
72 807869c1 2021-10-07 op fatal("open %s for domain %s: %s", l->dir, h->domain,
73 807869c1 2021-10-07 op strerror(errno));
74 fdea6aa0 2021-04-30 op }
75 9862b637 2021-01-13 op }
76 9862b637 2021-01-13 op }
77 592fd624 2020-10-07 op
78 3e4749f7 2020-10-02 op int
79 9468027b 2020-10-15 op make_socket(int port, int family)
80 3e4749f7 2020-10-02 op {
81 3e4749f7 2020-10-02 op int sock, v;
82 9468027b 2020-10-15 op struct sockaddr_in addr4;
83 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
84 9468027b 2020-10-15 op struct sockaddr *addr;
85 9468027b 2020-10-15 op socklen_t len;
86 3e4749f7 2020-10-02 op
87 81e0f000 2021-09-24 op switch (family) {
88 9468027b 2020-10-15 op case AF_INET:
89 df0c2926 2021-09-24 op memset(&addr4, 0, sizeof(addr4));
90 9468027b 2020-10-15 op addr4.sin_family = family;
91 9468027b 2020-10-15 op addr4.sin_port = htons(port);
92 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
93 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
94 9468027b 2020-10-15 op len = sizeof(addr4);
95 9468027b 2020-10-15 op break;
96 9468027b 2020-10-15 op
97 9468027b 2020-10-15 op case AF_INET6:
98 df0c2926 2021-09-24 op memset(&addr6, 0, sizeof(addr6));
99 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
100 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
101 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
102 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
103 9468027b 2020-10-15 op len = sizeof(addr6);
104 9468027b 2020-10-15 op break;
105 9468027b 2020-10-15 op
106 9468027b 2020-10-15 op default:
107 9468027b 2020-10-15 op /* unreachable */
108 9468027b 2020-10-15 op abort();
109 9468027b 2020-10-15 op }
110 9468027b 2020-10-15 op
111 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
112 80bbcad5 2021-01-10 op fatal("socket: %s", strerror(errno));
113 3e4749f7 2020-10-02 op
114 3e4749f7 2020-10-02 op v = 1;
115 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
116 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
117 3e4749f7 2020-10-02 op
118 3e4749f7 2020-10-02 op v = 1;
119 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
120 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
121 3e4749f7 2020-10-02 op
122 592fd624 2020-10-07 op mark_nonblock(sock);
123 592fd624 2020-10-07 op
124 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
125 80bbcad5 2021-01-10 op fatal("bind: %s", strerror(errno));
126 3e4749f7 2020-10-02 op
127 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
128 80bbcad5 2021-01-10 op fatal("listen: %s", strerror(errno));
129 3e4749f7 2020-10-02 op
130 3e4749f7 2020-10-02 op return sock;
131 3e4749f7 2020-10-02 op }
132 3e4749f7 2020-10-02 op
133 ff05125e 2021-10-15 op static void
134 ff05125e 2021-10-15 op add_keypair(struct vhost *h)
135 ff05125e 2021-10-15 op {
136 534afd0d 2022-10-05 op if (*h->ocsp == '\0') {
137 ff05125e 2021-10-15 op if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
138 ff05125e 2021-10-15 op fatal("failed to load the keypair (%s, %s)",
139 ff05125e 2021-10-15 op h->cert, h->key);
140 ff05125e 2021-10-15 op } else {
141 7fa67176 2021-10-15 op if (tls_config_add_keypair_ocsp_file(tlsconf, h->cert, h->key,
142 7fa67176 2021-10-15 op h->ocsp) == -1)
143 ff05125e 2021-10-15 op fatal("failed to load the keypair (%s, %s, %s)",
144 ff05125e 2021-10-15 op h->cert, h->key, h->ocsp);
145 ff05125e 2021-10-15 op }
146 ff05125e 2021-10-15 op }
147 ff05125e 2021-10-15 op
148 ae08ec7d 2021-01-25 op void
149 ae08ec7d 2021-01-25 op setup_tls(void)
150 881a9dd9 2021-01-16 op {
151 ae08ec7d 2021-01-25 op struct vhost *h;
152 881a9dd9 2021-01-16 op
153 881a9dd9 2021-01-16 op if ((tlsconf = tls_config_new()) == NULL)
154 132cae8c 2021-01-18 op fatal("tls_config_new");
155 881a9dd9 2021-01-16 op
156 881a9dd9 2021-01-16 op /* optionally accept client certs, but don't try to verify them */
157 881a9dd9 2021-01-16 op tls_config_verify_client_optional(tlsconf);
158 881a9dd9 2021-01-16 op tls_config_insecure_noverifycert(tlsconf);
159 881a9dd9 2021-01-16 op
160 881a9dd9 2021-01-16 op if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
161 132cae8c 2021-01-18 op fatal("tls_config_set_protocols");
162 881a9dd9 2021-01-16 op
163 881a9dd9 2021-01-16 op if ((ctx = tls_server()) == NULL)
164 132cae8c 2021-01-18 op fatal("tls_server failure");
165 881a9dd9 2021-01-16 op
166 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
167 b8e64ccd 2021-03-31 op
168 ae08ec7d 2021-01-25 op /* we need to set something, then we can add how many key we want */
169 b8e64ccd 2021-03-31 op if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
170 ca21e100 2021-02-04 op fatal("tls_config_set_keypair_file failed for (%s, %s)",
171 b8e64ccd 2021-03-31 op h->cert, h->key);
172 7fa67176 2021-10-15 op
173 7fa67176 2021-10-15 op /* same for OCSP */
174 534afd0d 2022-10-05 op if (*h->ocsp != '\0' &&
175 ff05125e 2021-10-15 op tls_config_set_ocsp_staple_file(tlsconf, h->ocsp) == -1)
176 ff05125e 2021-10-15 op fatal("tls_config_set_ocsp_staple_file failed for (%s)",
177 ff05125e 2021-10-15 op h->ocsp);
178 881a9dd9 2021-01-16 op
179 ff05125e 2021-10-15 op while ((h = TAILQ_NEXT(h, vhosts)) != NULL)
180 ff05125e 2021-10-15 op add_keypair(h);
181 ae08ec7d 2021-01-25 op
182 881a9dd9 2021-01-16 op if (tls_configure(ctx, tlsconf) == -1)
183 132cae8c 2021-01-18 op fatal("tls_configure: %s", tls_error(ctx));
184 ae08ec7d 2021-01-25 op }
185 881a9dd9 2021-01-16 op
186 d672b8fb 2021-02-03 op static int
187 bc99d868 2021-03-19 op listener_main(struct imsgbuf *ibuf)
188 ae08ec7d 2021-01-25 op {
189 a709ddf5 2021-02-07 op drop_priv();
190 cd5826b8 2022-09-10 op if (load_default_mime(&conf.mime) == -1)
191 d8d170aa 2022-04-08 op fatal("load_default_mime: %s", strerror(errno));
192 18bd8391 2022-04-08 op sort_mime(&conf.mime);
193 ae08ec7d 2021-01-25 op load_vhosts();
194 bc99d868 2021-03-19 op loop(ctx, sock4, sock6, ibuf);
195 881a9dd9 2021-01-16 op return 0;
196 3e4749f7 2020-10-02 op }
197 3e4749f7 2020-10-02 op
198 8d6ae384 2021-01-24 op void
199 8d6ae384 2021-01-24 op init_config(void)
200 3e4749f7 2020-10-02 op {
201 b8e64ccd 2021-03-31 op TAILQ_INIT(&hosts);
202 51d876f0 2020-12-21 op
203 15902770 2021-01-15 op conf.port = 1965;
204 15902770 2021-01-15 op conf.ipv6 = 0;
205 5bc3c98e 2021-01-15 op conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
206 85dff1f9 2021-01-11 op
207 b2a6b613 2021-01-21 op init_mime(&conf.mime);
208 ae08ec7d 2021-01-25 op
209 a709ddf5 2021-02-07 op conf.prefork = 3;
210 8d6ae384 2021-01-24 op }
211 0fbe79b3 2021-01-18 op
212 2030e314 2021-01-25 op void
213 ca21e100 2021-02-04 op free_config(void)
214 ca21e100 2021-02-04 op {
215 b8e64ccd 2021-03-31 op struct vhost *h, *th;
216 b8e64ccd 2021-03-31 op struct location *l, *tl;
217 b7967bc1 2021-01-02 op struct proxy *p, *tp;
218 9cc630aa 2021-04-28 op struct envlist *e, *te;
219 cc8c2901 2021-04-29 op struct alist *a, *ta;
220 534afd0d 2022-10-05 op int v;
221 ca21e100 2021-02-04 op
222 419a4235 2021-04-28 op v = conf.verbose;
223 419a4235 2021-04-28 op
224 d8d170aa 2022-04-08 op free_mime(&conf.mime);
225 ca21e100 2021-02-04 op memset(&conf, 0, sizeof(conf));
226 419a4235 2021-04-28 op
227 419a4235 2021-04-28 op conf.verbose = v;
228 ca21e100 2021-02-04 op
229 b8e64ccd 2021-03-31 op TAILQ_FOREACH_SAFE(h, &hosts, vhosts, th) {
230 b8e64ccd 2021-03-31 op TAILQ_FOREACH_SAFE(l, &h->locations, locations, tl) {
231 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&h->locations, l, locations);
232 ca21e100 2021-02-04 op
233 fdea6aa0 2021-04-30 op if (l->dirfd != -1)
234 fdea6aa0 2021-04-30 op close(l->dirfd);
235 fdea6aa0 2021-04-30 op
236 b8e64ccd 2021-03-31 op free(l);
237 ab1e0169 2021-06-12 op }
238 3b33eab3 2021-06-12 op
239 ab1e0169 2021-06-12 op TAILQ_FOREACH_SAFE(e, &h->params, envs, te) {
240 ab1e0169 2021-06-12 op TAILQ_REMOVE(&h->params, e, envs);
241 9cc630aa 2021-04-28 op free(e);
242 cc8c2901 2021-04-29 op }
243 cc8c2901 2021-04-29 op
244 cc8c2901 2021-04-29 op TAILQ_FOREACH_SAFE(a, &h->aliases, aliases, ta) {
245 3b33eab3 2021-06-12 op TAILQ_REMOVE(&h->aliases, a, aliases);
246 cc8c2901 2021-04-29 op free(a);
247 b7967bc1 2021-01-02 op }
248 b7967bc1 2021-01-02 op
249 b7967bc1 2021-01-02 op TAILQ_FOREACH_SAFE(p, &h->proxies, proxies, tp) {
250 b7967bc1 2021-01-02 op TAILQ_REMOVE(&h->proxies, p, proxies);
251 b7967bc1 2021-01-02 op tls_unload_file(p->cert, p->certlen);
252 b7967bc1 2021-01-02 op tls_unload_file(p->key, p->keylen);
253 b7967bc1 2021-01-02 op free(p);
254 9cc630aa 2021-04-28 op }
255 fdea6aa0 2021-04-30 op
256 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&hosts, h, vhosts);
257 b8e64ccd 2021-03-31 op free(h);
258 8ad1c570 2021-05-09 op }
259 8ad1c570 2021-05-09 op
260 534afd0d 2022-10-05 op memset(fcgi, 0, sizeof(fcgi));
261 ca21e100 2021-02-04 op
262 ca21e100 2021-02-04 op tls_free(ctx);
263 ca21e100 2021-02-04 op tls_config_free(tlsconf);
264 ca21e100 2021-02-04 op }
265 ca21e100 2021-02-04 op
266 1d3eb470 2021-03-20 op static int
267 1d3eb470 2021-03-20 op wait_signal(void)
268 ca21e100 2021-02-04 op {
269 ca21e100 2021-02-04 op sigset_t mask;
270 ca21e100 2021-02-04 op int signo;
271 ca21e100 2021-02-04 op
272 ca21e100 2021-02-04 op sigemptyset(&mask);
273 ca21e100 2021-02-04 op sigaddset(&mask, SIGHUP);
274 1d3eb470 2021-03-20 op sigaddset(&mask, SIGINT);
275 1d3eb470 2021-03-20 op sigaddset(&mask, SIGTERM);
276 ca21e100 2021-02-04 op sigwait(&mask, &signo);
277 1d3eb470 2021-03-20 op
278 1d3eb470 2021-03-20 op return signo == SIGHUP;
279 ca21e100 2021-02-04 op }
280 ca21e100 2021-02-04 op
281 ca21e100 2021-02-04 op void
282 ae08ec7d 2021-01-25 op drop_priv(void)
283 ae08ec7d 2021-01-25 op {
284 ae08ec7d 2021-01-25 op struct passwd *pw = NULL;
285 ae08ec7d 2021-01-25 op
286 7277bb7d 2022-09-10 op if (*conf.chroot != '\0' && *conf.user == '\0')
287 ae08ec7d 2021-01-25 op fatal("can't chroot without an user to switch to after.");
288 ae08ec7d 2021-01-25 op
289 7277bb7d 2022-09-10 op if (*conf.user != '\0') {
290 ae08ec7d 2021-01-25 op if ((pw = getpwnam(conf.user)) == NULL)
291 ae08ec7d 2021-01-25 op fatal("can't find user %s", conf.user);
292 ae08ec7d 2021-01-25 op }
293 ae08ec7d 2021-01-25 op
294 7277bb7d 2022-09-10 op if (*conf.chroot != '\0') {
295 ae08ec7d 2021-01-25 op if (chroot(conf.chroot) != 0 || chdir("/") != 0)
296 ae08ec7d 2021-01-25 op fatal("%s: %s", conf.chroot, strerror(errno));
297 ae08ec7d 2021-01-25 op }
298 ae08ec7d 2021-01-25 op
299 ae08ec7d 2021-01-25 op if (pw != NULL) {
300 872a7176 2022-11-27 op if (setgroups(1, &pw->pw_gid) == -1 ||
301 872a7176 2022-11-27 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
302 872a7176 2022-11-27 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
303 872a7176 2022-11-27 op fatal("cannot drop privileges");
304 ae08ec7d 2021-01-25 op }
305 ae08ec7d 2021-01-25 op
306 ae08ec7d 2021-01-25 op if (getuid() == 0)
307 3abf91b0 2021-02-07 op log_warn(NULL, "not a good idea to run a network daemon as root");
308 2030e314 2021-01-25 op }
309 d672b8fb 2021-02-03 op
310 3abf91b0 2021-02-07 op static void
311 9327bc04 2021-06-29 op usage(void)
312 3abf91b0 2021-02-07 op {
313 3abf91b0 2021-02-07 op fprintf(stderr,
314 0be2a537 2021-06-29 op "Version: " GMID_STRING "\n"
315 aae8f6bf 2022-09-08 op "Usage: %s [-dhnVv] [-D macro=value] [-f config] [-P pidfile]\n",
316 9327bc04 2021-06-29 op getprogname());
317 3abf91b0 2021-02-07 op }
318 3abf91b0 2021-02-07 op
319 376a5407 2021-02-23 op static void
320 376a5407 2021-02-23 op logger_init(void)
321 376a5407 2021-02-23 op {
322 376a5407 2021-02-23 op int p[2];
323 376a5407 2021-02-23 op
324 376a5407 2021-02-23 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
325 376a5407 2021-02-23 op err(1, "socketpair");
326 376a5407 2021-02-23 op
327 376a5407 2021-02-23 op switch (fork()) {
328 376a5407 2021-02-23 op case -1:
329 376a5407 2021-02-23 op err(1, "fork");
330 376a5407 2021-02-23 op case 0:
331 bc99d868 2021-03-19 op signal(SIGHUP, SIG_IGN);
332 376a5407 2021-02-23 op close(p[0]);
333 376a5407 2021-02-23 op setproctitle("logger");
334 bc99d868 2021-03-19 op imsg_init(&logibuf, p[1]);
335 376a5407 2021-02-23 op drop_priv();
336 bc99d868 2021-03-19 op _exit(logger_main(p[1], &logibuf));
337 376a5407 2021-02-23 op default:
338 376a5407 2021-02-23 op close(p[1]);
339 bc99d868 2021-03-19 op imsg_init(&logibuf, p[0]);
340 376a5407 2021-02-23 op return;
341 376a5407 2021-02-23 op }
342 376a5407 2021-02-23 op }
343 376a5407 2021-02-23 op
344 d29a2ee2 2022-09-06 op static void
345 d29a2ee2 2022-09-06 op serve(void)
346 d672b8fb 2021-02-03 op {
347 d29a2ee2 2022-09-06 op int i, p[2];
348 2030e314 2021-01-25 op
349 d672b8fb 2021-02-03 op /* setup tls before dropping privileges: we don't want user
350 d672b8fb 2021-02-03 op * to put private certs inside the chroot. */
351 d672b8fb 2021-02-03 op setup_tls();
352 d672b8fb 2021-02-03 op
353 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
354 2c3e53da 2021-03-03 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
355 2c3e53da 2021-03-03 op PF_UNSPEC, p) == -1)
356 2c3e53da 2021-03-03 op fatal("socketpair: %s", strerror(errno));
357 d672b8fb 2021-02-03 op
358 2c3e53da 2021-03-03 op switch (fork()) {
359 2c3e53da 2021-03-03 op case -1:
360 2c3e53da 2021-03-03 op fatal("fork: %s", strerror(errno));
361 2c3e53da 2021-03-03 op case 0: /* child */
362 2c3e53da 2021-03-03 op close(p[0]);
363 5c485529 2022-09-10 op imsg_init(&servibuf[i], p[1]);
364 2c3e53da 2021-03-03 op setproctitle("server");
365 5c485529 2022-09-10 op _exit(listener_main(&servibuf[i]));
366 2c3e53da 2021-03-03 op default:
367 2c3e53da 2021-03-03 op close(p[1]);
368 bc99d868 2021-03-19 op imsg_init(&servibuf[i], p[0]);
369 2c3e53da 2021-03-03 op }
370 d672b8fb 2021-02-03 op }
371 d672b8fb 2021-02-03 op }
372 419a4235 2021-04-28 op
373 419a4235 2021-04-28 op static int
374 419a4235 2021-04-28 op write_pidfile(const char *pidfile)
375 419a4235 2021-04-28 op {
376 419a4235 2021-04-28 op struct flock lock;
377 419a4235 2021-04-28 op int fd;
378 d672b8fb 2021-02-03 op
379 419a4235 2021-04-28 op if (pidfile == NULL)
380 419a4235 2021-04-28 op return -1;
381 419a4235 2021-04-28 op
382 419a4235 2021-04-28 op if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
383 419a4235 2021-04-28 op fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
384 419a4235 2021-04-28 op
385 419a4235 2021-04-28 op lock.l_start = 0;
386 419a4235 2021-04-28 op lock.l_len = 0;
387 419a4235 2021-04-28 op lock.l_type = F_WRLCK;
388 419a4235 2021-04-28 op lock.l_whence = SEEK_SET;
389 419a4235 2021-04-28 op
390 419a4235 2021-04-28 op if (fcntl(fd, F_SETLK, &lock) == -1)
391 419a4235 2021-04-28 op fatal("can't lock %s, gmid is already running?", pidfile);
392 419a4235 2021-04-28 op
393 419a4235 2021-04-28 op if (ftruncate(fd, 0) == -1)
394 419a4235 2021-04-28 op fatal("ftruncate: %s: %s", pidfile, strerror(errno));
395 419a4235 2021-04-28 op
396 419a4235 2021-04-28 op dprintf(fd, "%d\n", getpid());
397 419a4235 2021-04-28 op
398 419a4235 2021-04-28 op return fd;
399 419a4235 2021-04-28 op }
400 419a4235 2021-04-28 op
401 8d6ae384 2021-01-24 op int
402 8d6ae384 2021-01-24 op main(int argc, char **argv)
403 8d6ae384 2021-01-24 op {
404 32fbc478 2022-09-08 op int i, ch, conftest = 0;
405 8e8b2e25 2021-04-28 op int pidfd, old_ipv6, old_port;
406 501e489c 2021-01-24 op
407 8a50fc03 2021-07-07 op logger_init();
408 501e489c 2021-01-24 op init_config();
409 8d6ae384 2021-01-24 op
410 5777923b 2021-06-29 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
411 3e4749f7 2020-10-02 op switch (ch) {
412 f98e9045 2021-06-29 op case 'D':
413 f98e9045 2021-06-29 op if (cmdline_symset(optarg) == -1)
414 8a50fc03 2021-07-07 op fatal("could not parse macro definition: %s",
415 8a50fc03 2021-07-07 op optarg);
416 f98e9045 2021-06-29 op break;
417 aae8f6bf 2022-09-08 op case 'd':
418 3abf91b0 2021-02-07 op conf.foreground = 1;
419 46af8c6c 2021-01-27 op break;
420 aae8f6bf 2022-09-08 op case 'f':
421 aae8f6bf 2022-09-08 op config_path = absolutify_path(optarg);
422 aae8f6bf 2022-09-08 op break;
423 3e4749f7 2020-10-02 op case 'h':
424 9327bc04 2021-06-29 op usage();
425 3e4749f7 2020-10-02 op return 0;
426 15902770 2021-01-15 op case 'n':
427 f0a01fc7 2021-10-09 op conftest++;
428 721e2325 2020-11-18 op break;
429 8e8b2e25 2021-04-28 op case 'P':
430 8e8b2e25 2021-04-28 op pidfile = optarg;
431 8e8b2e25 2021-04-28 op break;
432 5777923b 2021-06-29 op case 'V':
433 fdb43a4c 2021-06-29 op puts("Version: " GMID_STRING);
434 5777923b 2021-06-29 op return 0;
435 8904fa0e 2021-01-27 op case 'v':
436 3abf91b0 2021-02-07 op conf.verbose++;
437 8904fa0e 2021-01-27 op break;
438 3e4749f7 2020-10-02 op default:
439 9327bc04 2021-06-29 op usage();
440 3e4749f7 2020-10-02 op return 1;
441 3e4749f7 2020-10-02 op }
442 3e4749f7 2020-10-02 op }
443 8443bff7 2021-01-25 op argc -= optind;
444 8443bff7 2021-01-25 op argv += optind;
445 3e4749f7 2020-10-02 op
446 32fbc478 2022-09-08 op if (argc != 0)
447 d29a2ee2 2022-09-06 op usage();
448 d29a2ee2 2022-09-06 op
449 32fbc478 2022-09-08 op parse_conf(config_path);
450 d672b8fb 2021-02-03 op
451 132cae8c 2021-01-18 op if (conftest) {
452 f0a01fc7 2021-10-09 op fprintf(stderr, "config OK\n");
453 f0a01fc7 2021-10-09 op if (conftest > 1)
454 f0a01fc7 2021-10-09 op print_conf();
455 132cae8c 2021-01-18 op return 0;
456 132cae8c 2021-01-18 op }
457 4a28dd01 2020-12-28 op
458 32fbc478 2022-09-08 op if (!conf.foreground) {
459 8a50fc03 2021-07-07 op /* log to syslog */
460 8a50fc03 2021-07-07 op imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0);
461 8a50fc03 2021-07-07 op imsg_flush(&logibuf);
462 8a50fc03 2021-07-07 op
463 0170ba02 2021-01-17 op if (daemon(1, 1) == -1)
464 8a50fc03 2021-07-07 op fatal("daemon: %s", strerror(errno));
465 0170ba02 2021-01-17 op }
466 4a28dd01 2020-12-28 op
467 d672b8fb 2021-02-03 op sock4 = make_socket(conf.port, AF_INET);
468 d672b8fb 2021-02-03 op sock6 = -1;
469 d672b8fb 2021-02-03 op if (conf.ipv6)
470 d672b8fb 2021-02-03 op sock6 = make_socket(conf.port, AF_INET6);
471 3e4749f7 2020-10-02 op
472 3841a369 2021-04-20 op signal(SIGPIPE, SIG_IGN);
473 3841a369 2021-04-20 op
474 8e8b2e25 2021-04-28 op pidfd = write_pidfile(pidfile);
475 8e8b2e25 2021-04-28 op
476 a8a1f439 2021-07-07 op /*
477 a8a1f439 2021-07-07 op * Linux seems to call the event handlers even when we're
478 1b333122 2021-04-26 op * doing a sigwait. These dummy handlers are here to avoid
479 a8a1f439 2021-07-07 op * being terminated on SIGHUP, SIGINT or SIGTERM.
480 a8a1f439 2021-07-07 op */
481 b9c9123b 2021-03-20 op signal(SIGHUP, dummy_handler);
482 b9c9123b 2021-03-20 op signal(SIGINT, dummy_handler);
483 b9c9123b 2021-03-20 op signal(SIGTERM, dummy_handler);
484 b9c9123b 2021-03-20 op
485 ca21e100 2021-02-04 op /* wait a sighup and reload the daemon */
486 ca21e100 2021-02-04 op for (;;) {
487 d29a2ee2 2022-09-06 op serve();
488 ca21e100 2021-02-04 op
489 32fbc478 2022-09-08 op if (!wait_signal())
490 1d3eb470 2021-03-20 op break;
491 1d3eb470 2021-03-20 op
492 3abf91b0 2021-02-07 op log_info(NULL, "reloading configuration %s", config_path);
493 ca21e100 2021-02-04 op
494 d29a2ee2 2022-09-06 op /* close the servers */
495 d29a2ee2 2022-09-06 op for (i = 0; i < conf.prefork; ++i) {
496 d29a2ee2 2022-09-06 op imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
497 d29a2ee2 2022-09-06 op imsg_flush(&servibuf[i]);
498 d29a2ee2 2022-09-06 op close(servibuf[i].fd);
499 d29a2ee2 2022-09-06 op }
500 bc99d868 2021-03-19 op
501 ca21e100 2021-02-04 op old_ipv6 = conf.ipv6;
502 ca21e100 2021-02-04 op old_port = conf.port;
503 ca21e100 2021-02-04 op
504 ca21e100 2021-02-04 op free_config();
505 ca21e100 2021-02-04 op init_config();
506 ca21e100 2021-02-04 op parse_conf(config_path);
507 ca21e100 2021-02-04 op
508 ca21e100 2021-02-04 op if (old_port != conf.port) {
509 ca21e100 2021-02-04 op close(sock4);
510 ca21e100 2021-02-04 op close(sock6);
511 ca21e100 2021-02-04 op sock4 = -1;
512 ca21e100 2021-02-04 op sock6 = -1;
513 ca21e100 2021-02-04 op }
514 ca21e100 2021-02-04 op
515 ca21e100 2021-02-04 op if (sock6 != -1 && old_ipv6 != conf.ipv6) {
516 ca21e100 2021-02-04 op close(sock6);
517 ca21e100 2021-02-04 op sock6 = -1;
518 ca21e100 2021-02-04 op }
519 ca21e100 2021-02-04 op
520 ca21e100 2021-02-04 op if (sock4 == -1)
521 ca21e100 2021-02-04 op sock4 = make_socket(conf.port, AF_INET);
522 ca21e100 2021-02-04 op if (sock6 == -1 && conf.ipv6)
523 ca21e100 2021-02-04 op sock6 = make_socket(conf.port, AF_INET6);
524 ca21e100 2021-02-04 op }
525 1d3eb470 2021-03-20 op
526 5c485529 2022-09-10 op for (i = 0; i < conf.prefork; ++i) {
527 5c485529 2022-09-10 op imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
528 5c485529 2022-09-10 op imsg_flush(&servibuf[i]);
529 5c485529 2022-09-10 op close(servibuf[i].fd);
530 5c485529 2022-09-10 op }
531 1d3eb470 2021-03-20 op
532 1d3eb470 2021-03-20 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
533 1d3eb470 2021-03-20 op imsg_flush(&logibuf);
534 5c485529 2022-09-10 op close(logibuf.fd);
535 1d3eb470 2021-03-20 op
536 8e8b2e25 2021-04-28 op if (pidfd != -1)
537 8e8b2e25 2021-04-28 op close(pidfd);
538 8e8b2e25 2021-04-28 op
539 1d3eb470 2021-03-20 op return 0;
540 3e4749f7 2020-10-02 op }