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 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>
29 cc68fe70 2020-10-07 op
30 aae8f6bf 2022-09-08 op static const char *opts = "D:df:hnP:Vv";
31 5777923b 2021-06-29 op
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},
36 5777923b 2021-06-29 op };
37 5777923b 2021-06-29 op
38 8ad1c570 2021-05-09 op struct fcgi fcgi[FCGI_MAX];
39 8ad1c570 2021-05-09 op
40 b8e64ccd 2021-03-31 op struct vhosthead hosts;
41 15902770 2021-01-15 op
42 bc99d868 2021-03-19 op int sock4, sock6;
43 d672b8fb 2021-02-03 op
44 bc99d868 2021-03-19 op struct imsgbuf logibuf, exibuf, servibuf[PROC_MAX];
45 376a5407 2021-02-23 op
46 32fbc478 2022-09-08 op const char *config_path = "/etc/gmid.conf";
47 32fbc478 2022-09-08 op const char *pidfile;
48 881a9dd9 2021-01-16 op
49 15902770 2021-01-15 op struct conf conf;
50 15902770 2021-01-15 op
51 ca21e100 2021-02-04 op struct tls_config *tlsconf;
52 ae08ec7d 2021-01-25 op struct tls *ctx;
53 0be51733 2021-01-20 op
54 b9c9123b 2021-03-20 op static void
55 b9c9123b 2021-03-20 op dummy_handler(int signo)
56 b9c9123b 2021-03-20 op {
57 b9c9123b 2021-03-20 op return;
58 b9c9123b 2021-03-20 op }
59 b9c9123b 2021-03-20 op
60 8443bff7 2021-01-25 op void
61 ae08ec7d 2021-01-25 op load_vhosts(void)
62 3e4749f7 2020-10-02 op {
63 fdea6aa0 2021-04-30 op struct vhost *h;
64 fdea6aa0 2021-04-30 op struct location *l;
65 15902770 2021-01-15 op
66 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
67 fdea6aa0 2021-04-30 op TAILQ_FOREACH(l, &h->locations, locations) {
68 fdea6aa0 2021-04-30 op if (l->dir == NULL)
69 fdea6aa0 2021-04-30 op continue;
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));
73 fdea6aa0 2021-04-30 op }
74 9862b637 2021-01-13 op }
75 9862b637 2021-01-13 op }
76 592fd624 2020-10-07 op
77 3e4749f7 2020-10-02 op int
78 9468027b 2020-10-15 op make_socket(int port, int family)
79 3e4749f7 2020-10-02 op {
80 3e4749f7 2020-10-02 op int sock, v;
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;
85 3e4749f7 2020-10-02 op
86 81e0f000 2021-09-24 op switch (family) {
87 9468027b 2020-10-15 op case AF_INET:
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);
94 9468027b 2020-10-15 op break;
95 9468027b 2020-10-15 op
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);
103 9468027b 2020-10-15 op break;
104 9468027b 2020-10-15 op
105 9468027b 2020-10-15 op default:
106 9468027b 2020-10-15 op /* unreachable */
107 9468027b 2020-10-15 op abort();
108 9468027b 2020-10-15 op }
109 9468027b 2020-10-15 op
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));
112 3e4749f7 2020-10-02 op
113 3e4749f7 2020-10-02 op v = 1;
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));
116 3e4749f7 2020-10-02 op
117 3e4749f7 2020-10-02 op v = 1;
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));
120 3e4749f7 2020-10-02 op
121 592fd624 2020-10-07 op mark_nonblock(sock);
122 592fd624 2020-10-07 op
123 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
124 80bbcad5 2021-01-10 op fatal("bind: %s", strerror(errno));
125 3e4749f7 2020-10-02 op
126 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
127 80bbcad5 2021-01-10 op fatal("listen: %s", strerror(errno));
128 3e4749f7 2020-10-02 op
129 3e4749f7 2020-10-02 op return sock;
130 3e4749f7 2020-10-02 op }
131 3e4749f7 2020-10-02 op
132 ff05125e 2021-10-15 op static void
133 ff05125e 2021-10-15 op add_keypair(struct vhost *h)
134 ff05125e 2021-10-15 op {
135 ff05125e 2021-10-15 op if (h->ocsp == NULL) {
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);
139 ff05125e 2021-10-15 op } else {
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);
144 ff05125e 2021-10-15 op }
145 ff05125e 2021-10-15 op }
146 ff05125e 2021-10-15 op
147 ae08ec7d 2021-01-25 op void
148 ae08ec7d 2021-01-25 op setup_tls(void)
149 881a9dd9 2021-01-16 op {
150 ae08ec7d 2021-01-25 op struct vhost *h;
151 881a9dd9 2021-01-16 op
152 881a9dd9 2021-01-16 op if ((tlsconf = tls_config_new()) == NULL)
153 132cae8c 2021-01-18 op fatal("tls_config_new");
154 881a9dd9 2021-01-16 op
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);
158 881a9dd9 2021-01-16 op
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");
161 881a9dd9 2021-01-16 op
162 881a9dd9 2021-01-16 op if ((ctx = tls_server()) == NULL)
163 132cae8c 2021-01-18 op fatal("tls_server failure");
164 881a9dd9 2021-01-16 op
165 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
166 b8e64ccd 2021-03-31 op
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);
171 7fa67176 2021-10-15 op
172 7fa67176 2021-10-15 op /* same for OCSP */
173 ff05125e 2021-10-15 op if (h->ocsp != NULL &&
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)",
176 ff05125e 2021-10-15 op h->ocsp);
177 881a9dd9 2021-01-16 op
178 ff05125e 2021-10-15 op while ((h = TAILQ_NEXT(h, vhosts)) != NULL)
179 ff05125e 2021-10-15 op add_keypair(h);
180 ae08ec7d 2021-01-25 op
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));
183 ae08ec7d 2021-01-25 op }
184 881a9dd9 2021-01-16 op
185 d672b8fb 2021-02-03 op static int
186 bc99d868 2021-03-19 op listener_main(struct imsgbuf *ibuf)
187 ae08ec7d 2021-01-25 op {
188 a709ddf5 2021-02-07 op drop_priv();
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);
194 881a9dd9 2021-01-16 op return 0;
195 3e4749f7 2020-10-02 op }
196 3e4749f7 2020-10-02 op
197 8d6ae384 2021-01-24 op void
198 8d6ae384 2021-01-24 op init_config(void)
199 3e4749f7 2020-10-02 op {
200 b8e64ccd 2021-03-31 op TAILQ_INIT(&hosts);
201 51d876f0 2020-12-21 op
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;
205 85dff1f9 2021-01-11 op
206 b2a6b613 2021-01-21 op init_mime(&conf.mime);
207 ae08ec7d 2021-01-25 op
208 a709ddf5 2021-02-07 op conf.prefork = 3;
209 8d6ae384 2021-01-24 op }
210 0fbe79b3 2021-01-18 op
211 2030e314 2021-01-25 op void
212 ca21e100 2021-02-04 op free_config(void)
213 ca21e100 2021-02-04 op {
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;
219 8ad1c570 2021-05-09 op int v, i;
220 ca21e100 2021-02-04 op
221 419a4235 2021-04-28 op v = conf.verbose;
222 419a4235 2021-04-28 op
223 d8d170aa 2022-04-08 op free_mime(&conf.mime);
224 ca21e100 2021-02-04 op memset(&conf, 0, sizeof(conf));
225 419a4235 2021-04-28 op
226 419a4235 2021-04-28 op conf.verbose = v;
227 ca21e100 2021-02-04 op
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);
231 ca21e100 2021-02-04 op
232 ca21e100 2021-02-04 op free((char*)l->match);
233 ca21e100 2021-02-04 op free((char*)l->lang);
234 ca21e100 2021-02-04 op free((char*)l->default_mime);
235 ca21e100 2021-02-04 op free((char*)l->index);
236 6abda252 2021-02-06 op free((char*)l->block_fmt);
237 fdea6aa0 2021-04-30 op free((char*)l->dir);
238 fdea6aa0 2021-04-30 op
239 fdea6aa0 2021-04-30 op if (l->dirfd != -1)
240 fdea6aa0 2021-04-30 op close(l->dirfd);
241 fdea6aa0 2021-04-30 op
242 b8e64ccd 2021-03-31 op free(l);
243 ca21e100 2021-02-04 op }
244 b8e64ccd 2021-03-31 op
245 9cc630aa 2021-04-28 op TAILQ_FOREACH_SAFE(e, &h->env, envs, te) {
246 3b33eab3 2021-06-12 op TAILQ_REMOVE(&h->env, e, envs);
247 ab1e0169 2021-06-12 op
248 ab1e0169 2021-06-12 op free(e->name);
249 ab1e0169 2021-06-12 op free(e->value);
250 ab1e0169 2021-06-12 op free(e);
251 ab1e0169 2021-06-12 op }
252 3b33eab3 2021-06-12 op
253 ab1e0169 2021-06-12 op TAILQ_FOREACH_SAFE(e, &h->params, envs, te) {
254 ab1e0169 2021-06-12 op TAILQ_REMOVE(&h->params, e, envs);
255 ab1e0169 2021-06-12 op
256 9cc630aa 2021-04-28 op free(e->name);
257 9cc630aa 2021-04-28 op free(e->value);
258 9cc630aa 2021-04-28 op free(e);
259 cc8c2901 2021-04-29 op }
260 cc8c2901 2021-04-29 op
261 cc8c2901 2021-04-29 op TAILQ_FOREACH_SAFE(a, &h->aliases, aliases, ta) {
262 3b33eab3 2021-06-12 op TAILQ_REMOVE(&h->aliases, a, aliases);
263 3b33eab3 2021-06-12 op
264 cc8c2901 2021-04-29 op free(a->alias);
265 cc8c2901 2021-04-29 op free(a);
266 b7967bc1 2021-01-02 op }
267 b7967bc1 2021-01-02 op
268 b7967bc1 2021-01-02 op TAILQ_FOREACH_SAFE(p, &h->proxies, proxies, tp) {
269 b7967bc1 2021-01-02 op TAILQ_REMOVE(&h->proxies, p, proxies);
270 b7967bc1 2021-01-02 op
271 b7967bc1 2021-01-02 op free(p->match_proto);
272 b7967bc1 2021-01-02 op free(p->match_host);
273 b7967bc1 2021-01-02 op free(p->host);
274 1cdea97b 2022-01-30 op free(p->sni);
275 b7967bc1 2021-01-02 op tls_unload_file(p->cert, p->certlen);
276 b7967bc1 2021-01-02 op tls_unload_file(p->key, p->keylen);
277 b7967bc1 2021-01-02 op free(p);
278 9cc630aa 2021-04-28 op }
279 9cc630aa 2021-04-28 op
280 fdea6aa0 2021-04-30 op free((char*)h->domain);
281 fdea6aa0 2021-04-30 op free((char*)h->cert);
282 fdea6aa0 2021-04-30 op free((char*)h->key);
283 193380ea 2021-10-24 op free((char*)h->ocsp);
284 fdea6aa0 2021-04-30 op free((char*)h->entrypoint);
285 fdea6aa0 2021-04-30 op
286 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&hosts, h, vhosts);
287 b8e64ccd 2021-03-31 op free(h);
288 8ad1c570 2021-05-09 op }
289 8ad1c570 2021-05-09 op
290 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
291 8ad1c570 2021-05-09 op if (fcgi[i].path == NULL && fcgi[i].prog == NULL)
292 8ad1c570 2021-05-09 op break;
293 8ad1c570 2021-05-09 op free(fcgi[i].path);
294 8ad1c570 2021-05-09 op free(fcgi[i].port);
295 8ad1c570 2021-05-09 op free(fcgi[i].prog);
296 ef945cf4 2021-07-06 op
297 ef945cf4 2021-07-06 op fcgi[i].path = NULL;
298 ef945cf4 2021-07-06 op fcgi[i].port = NULL;
299 ef945cf4 2021-07-06 op fcgi[i].prog = NULL;
300 ca21e100 2021-02-04 op }
301 ca21e100 2021-02-04 op
302 ca21e100 2021-02-04 op tls_free(ctx);
303 ca21e100 2021-02-04 op tls_config_free(tlsconf);
304 ca21e100 2021-02-04 op }
305 ca21e100 2021-02-04 op
306 1d3eb470 2021-03-20 op static int
307 1d3eb470 2021-03-20 op wait_signal(void)
308 ca21e100 2021-02-04 op {
309 ca21e100 2021-02-04 op sigset_t mask;
310 ca21e100 2021-02-04 op int signo;
311 ca21e100 2021-02-04 op
312 ca21e100 2021-02-04 op sigemptyset(&mask);
313 ca21e100 2021-02-04 op sigaddset(&mask, SIGHUP);
314 1d3eb470 2021-03-20 op sigaddset(&mask, SIGINT);
315 1d3eb470 2021-03-20 op sigaddset(&mask, SIGTERM);
316 ca21e100 2021-02-04 op sigwait(&mask, &signo);
317 1d3eb470 2021-03-20 op
318 1d3eb470 2021-03-20 op return signo == SIGHUP;
319 ca21e100 2021-02-04 op }
320 ca21e100 2021-02-04 op
321 ca21e100 2021-02-04 op void
322 ae08ec7d 2021-01-25 op drop_priv(void)
323 ae08ec7d 2021-01-25 op {
324 ae08ec7d 2021-01-25 op struct passwd *pw = NULL;
325 ae08ec7d 2021-01-25 op
326 7277bb7d 2022-09-10 op if (*conf.chroot != '\0' && *conf.user == '\0')
327 ae08ec7d 2021-01-25 op fatal("can't chroot without an user to switch to after.");
328 ae08ec7d 2021-01-25 op
329 7277bb7d 2022-09-10 op if (*conf.user != '\0') {
330 ae08ec7d 2021-01-25 op if ((pw = getpwnam(conf.user)) == NULL)
331 ae08ec7d 2021-01-25 op fatal("can't find user %s", conf.user);
332 ae08ec7d 2021-01-25 op }
333 ae08ec7d 2021-01-25 op
334 7277bb7d 2022-09-10 op if (*conf.chroot != '\0') {
335 ae08ec7d 2021-01-25 op if (chroot(conf.chroot) != 0 || chdir("/") != 0)
336 ae08ec7d 2021-01-25 op fatal("%s: %s", conf.chroot, strerror(errno));
337 ae08ec7d 2021-01-25 op }
338 ae08ec7d 2021-01-25 op
339 ae08ec7d 2021-01-25 op if (pw != NULL) {
340 ae08ec7d 2021-01-25 op if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
341 ae08ec7d 2021-01-25 op fatal("setresuid(%d): %s", pw->pw_uid,
342 ae08ec7d 2021-01-25 op strerror(errno));
343 ae08ec7d 2021-01-25 op }
344 ae08ec7d 2021-01-25 op
345 ae08ec7d 2021-01-25 op if (getuid() == 0)
346 3abf91b0 2021-02-07 op log_warn(NULL, "not a good idea to run a network daemon as root");
347 2030e314 2021-01-25 op }
348 d672b8fb 2021-02-03 op
349 3abf91b0 2021-02-07 op static void
350 9327bc04 2021-06-29 op usage(void)
351 3abf91b0 2021-02-07 op {
352 3abf91b0 2021-02-07 op fprintf(stderr,
353 0be2a537 2021-06-29 op "Version: " GMID_STRING "\n"
354 aae8f6bf 2022-09-08 op "Usage: %s [-dhnVv] [-D macro=value] [-f config] [-P pidfile]\n",
355 9327bc04 2021-06-29 op getprogname());
356 3abf91b0 2021-02-07 op }
357 3abf91b0 2021-02-07 op
358 376a5407 2021-02-23 op static void
359 376a5407 2021-02-23 op logger_init(void)
360 376a5407 2021-02-23 op {
361 376a5407 2021-02-23 op int p[2];
362 376a5407 2021-02-23 op
363 376a5407 2021-02-23 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
364 376a5407 2021-02-23 op err(1, "socketpair");
365 376a5407 2021-02-23 op
366 376a5407 2021-02-23 op switch (fork()) {
367 376a5407 2021-02-23 op case -1:
368 376a5407 2021-02-23 op err(1, "fork");
369 376a5407 2021-02-23 op case 0:
370 bc99d868 2021-03-19 op signal(SIGHUP, SIG_IGN);
371 376a5407 2021-02-23 op close(p[0]);
372 376a5407 2021-02-23 op setproctitle("logger");
373 bc99d868 2021-03-19 op imsg_init(&logibuf, p[1]);
374 376a5407 2021-02-23 op drop_priv();
375 bc99d868 2021-03-19 op _exit(logger_main(p[1], &logibuf));
376 376a5407 2021-02-23 op default:
377 376a5407 2021-02-23 op close(p[1]);
378 bc99d868 2021-03-19 op imsg_init(&logibuf, p[0]);
379 376a5407 2021-02-23 op return;
380 376a5407 2021-02-23 op }
381 376a5407 2021-02-23 op }
382 376a5407 2021-02-23 op
383 d29a2ee2 2022-09-06 op static void
384 d29a2ee2 2022-09-06 op serve(void)
385 d672b8fb 2021-02-03 op {
386 d29a2ee2 2022-09-06 op int i, p[2];
387 2030e314 2021-01-25 op
388 d672b8fb 2021-02-03 op /* setup tls before dropping privileges: we don't want user
389 d672b8fb 2021-02-03 op * to put private certs inside the chroot. */
390 d672b8fb 2021-02-03 op setup_tls();
391 d672b8fb 2021-02-03 op
392 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
393 2c3e53da 2021-03-03 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
394 2c3e53da 2021-03-03 op PF_UNSPEC, p) == -1)
395 2c3e53da 2021-03-03 op fatal("socketpair: %s", strerror(errno));
396 d672b8fb 2021-02-03 op
397 2c3e53da 2021-03-03 op switch (fork()) {
398 2c3e53da 2021-03-03 op case -1:
399 2c3e53da 2021-03-03 op fatal("fork: %s", strerror(errno));
400 2c3e53da 2021-03-03 op case 0: /* child */
401 2c3e53da 2021-03-03 op close(p[0]);
402 bc99d868 2021-03-19 op imsg_init(&exibuf, p[1]);
403 2c3e53da 2021-03-03 op setproctitle("server");
404 bc99d868 2021-03-19 op _exit(listener_main(&exibuf));
405 2c3e53da 2021-03-03 op default:
406 2c3e53da 2021-03-03 op close(p[1]);
407 bc99d868 2021-03-19 op imsg_init(&servibuf[i], p[0]);
408 2c3e53da 2021-03-03 op }
409 d672b8fb 2021-02-03 op }
410 d672b8fb 2021-02-03 op }
411 419a4235 2021-04-28 op
412 419a4235 2021-04-28 op static int
413 419a4235 2021-04-28 op write_pidfile(const char *pidfile)
414 419a4235 2021-04-28 op {
415 419a4235 2021-04-28 op struct flock lock;
416 419a4235 2021-04-28 op int fd;
417 d672b8fb 2021-02-03 op
418 419a4235 2021-04-28 op if (pidfile == NULL)
419 419a4235 2021-04-28 op return -1;
420 419a4235 2021-04-28 op
421 419a4235 2021-04-28 op if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
422 419a4235 2021-04-28 op fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
423 419a4235 2021-04-28 op
424 419a4235 2021-04-28 op lock.l_start = 0;
425 419a4235 2021-04-28 op lock.l_len = 0;
426 419a4235 2021-04-28 op lock.l_type = F_WRLCK;
427 419a4235 2021-04-28 op lock.l_whence = SEEK_SET;
428 419a4235 2021-04-28 op
429 419a4235 2021-04-28 op if (fcntl(fd, F_SETLK, &lock) == -1)
430 419a4235 2021-04-28 op fatal("can't lock %s, gmid is already running?", pidfile);
431 419a4235 2021-04-28 op
432 419a4235 2021-04-28 op if (ftruncate(fd, 0) == -1)
433 419a4235 2021-04-28 op fatal("ftruncate: %s: %s", pidfile, strerror(errno));
434 419a4235 2021-04-28 op
435 419a4235 2021-04-28 op dprintf(fd, "%d\n", getpid());
436 419a4235 2021-04-28 op
437 419a4235 2021-04-28 op return fd;
438 419a4235 2021-04-28 op }
439 419a4235 2021-04-28 op
440 8d6ae384 2021-01-24 op int
441 8d6ae384 2021-01-24 op main(int argc, char **argv)
442 8d6ae384 2021-01-24 op {
443 32fbc478 2022-09-08 op int i, ch, conftest = 0;
444 8e8b2e25 2021-04-28 op int pidfd, old_ipv6, old_port;
445 501e489c 2021-01-24 op
446 8a50fc03 2021-07-07 op logger_init();
447 501e489c 2021-01-24 op init_config();
448 8d6ae384 2021-01-24 op
449 5777923b 2021-06-29 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
450 3e4749f7 2020-10-02 op switch (ch) {
451 f98e9045 2021-06-29 op case 'D':
452 f98e9045 2021-06-29 op if (cmdline_symset(optarg) == -1)
453 8a50fc03 2021-07-07 op fatal("could not parse macro definition: %s",
454 8a50fc03 2021-07-07 op optarg);
455 f98e9045 2021-06-29 op break;
456 f98e9045 2021-06-29 op
457 aae8f6bf 2022-09-08 op case 'd':
458 3abf91b0 2021-02-07 op conf.foreground = 1;
459 46af8c6c 2021-01-27 op break;
460 46af8c6c 2021-01-27 op
461 aae8f6bf 2022-09-08 op case 'f':
462 aae8f6bf 2022-09-08 op config_path = absolutify_path(optarg);
463 aae8f6bf 2022-09-08 op break;
464 aae8f6bf 2022-09-08 op
465 3e4749f7 2020-10-02 op case 'h':
466 9327bc04 2021-06-29 op usage();
467 3e4749f7 2020-10-02 op return 0;
468 3e4749f7 2020-10-02 op
469 15902770 2021-01-15 op case 'n':
470 f0a01fc7 2021-10-09 op conftest++;
471 721e2325 2020-11-18 op break;
472 721e2325 2020-11-18 op
473 8e8b2e25 2021-04-28 op case 'P':
474 8e8b2e25 2021-04-28 op pidfile = optarg;
475 8e8b2e25 2021-04-28 op break;
476 8e8b2e25 2021-04-28 op
477 5777923b 2021-06-29 op case 'V':
478 fdb43a4c 2021-06-29 op puts("Version: " GMID_STRING);
479 5777923b 2021-06-29 op return 0;
480 5777923b 2021-06-29 op
481 8904fa0e 2021-01-27 op case 'v':
482 3abf91b0 2021-02-07 op conf.verbose++;
483 8904fa0e 2021-01-27 op break;
484 8904fa0e 2021-01-27 op
485 3e4749f7 2020-10-02 op default:
486 9327bc04 2021-06-29 op usage();
487 3e4749f7 2020-10-02 op return 1;
488 3e4749f7 2020-10-02 op }
489 3e4749f7 2020-10-02 op }
490 8443bff7 2021-01-25 op argc -= optind;
491 8443bff7 2021-01-25 op argv += optind;
492 3e4749f7 2020-10-02 op
493 32fbc478 2022-09-08 op if (argc != 0)
494 d29a2ee2 2022-09-06 op usage();
495 d29a2ee2 2022-09-06 op
496 32fbc478 2022-09-08 op parse_conf(config_path);
497 d672b8fb 2021-02-03 op
498 132cae8c 2021-01-18 op if (conftest) {
499 f0a01fc7 2021-10-09 op fprintf(stderr, "config OK\n");
500 f0a01fc7 2021-10-09 op if (conftest > 1)
501 f0a01fc7 2021-10-09 op print_conf();
502 132cae8c 2021-01-18 op return 0;
503 132cae8c 2021-01-18 op }
504 4a28dd01 2020-12-28 op
505 32fbc478 2022-09-08 op if (!conf.foreground) {
506 8a50fc03 2021-07-07 op /* log to syslog */
507 8a50fc03 2021-07-07 op imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0);
508 8a50fc03 2021-07-07 op imsg_flush(&logibuf);
509 8a50fc03 2021-07-07 op
510 0170ba02 2021-01-17 op if (daemon(1, 1) == -1)
511 8a50fc03 2021-07-07 op fatal("daemon: %s", strerror(errno));
512 0170ba02 2021-01-17 op }
513 4a28dd01 2020-12-28 op
514 d672b8fb 2021-02-03 op sock4 = make_socket(conf.port, AF_INET);
515 d672b8fb 2021-02-03 op sock6 = -1;
516 d672b8fb 2021-02-03 op if (conf.ipv6)
517 d672b8fb 2021-02-03 op sock6 = make_socket(conf.port, AF_INET6);
518 3e4749f7 2020-10-02 op
519 3841a369 2021-04-20 op signal(SIGPIPE, SIG_IGN);
520 3841a369 2021-04-20 op
521 8e8b2e25 2021-04-28 op pidfd = write_pidfile(pidfile);
522 8e8b2e25 2021-04-28 op
523 a8a1f439 2021-07-07 op /*
524 a8a1f439 2021-07-07 op * Linux seems to call the event handlers even when we're
525 1b333122 2021-04-26 op * doing a sigwait. These dummy handlers are here to avoid
526 a8a1f439 2021-07-07 op * being terminated on SIGHUP, SIGINT or SIGTERM.
527 a8a1f439 2021-07-07 op */
528 b9c9123b 2021-03-20 op signal(SIGHUP, dummy_handler);
529 b9c9123b 2021-03-20 op signal(SIGINT, dummy_handler);
530 b9c9123b 2021-03-20 op signal(SIGTERM, dummy_handler);
531 b9c9123b 2021-03-20 op
532 ca21e100 2021-02-04 op /* wait a sighup and reload the daemon */
533 ca21e100 2021-02-04 op for (;;) {
534 d29a2ee2 2022-09-06 op serve();
535 ca21e100 2021-02-04 op
536 32fbc478 2022-09-08 op if (!wait_signal())
537 1d3eb470 2021-03-20 op break;
538 1d3eb470 2021-03-20 op
539 3abf91b0 2021-02-07 op log_info(NULL, "reloading configuration %s", config_path);
540 ca21e100 2021-02-04 op
541 d29a2ee2 2022-09-06 op /* close the servers */
542 d29a2ee2 2022-09-06 op for (i = 0; i < conf.prefork; ++i) {
543 d29a2ee2 2022-09-06 op imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
544 d29a2ee2 2022-09-06 op imsg_flush(&servibuf[i]);
545 d29a2ee2 2022-09-06 op close(servibuf[i].fd);
546 d29a2ee2 2022-09-06 op }
547 bc99d868 2021-03-19 op
548 ca21e100 2021-02-04 op old_ipv6 = conf.ipv6;
549 ca21e100 2021-02-04 op old_port = conf.port;
550 ca21e100 2021-02-04 op
551 ca21e100 2021-02-04 op free_config();
552 ca21e100 2021-02-04 op init_config();
553 ca21e100 2021-02-04 op parse_conf(config_path);
554 ca21e100 2021-02-04 op
555 ca21e100 2021-02-04 op if (old_port != conf.port) {
556 ca21e100 2021-02-04 op close(sock4);
557 ca21e100 2021-02-04 op close(sock6);
558 ca21e100 2021-02-04 op sock4 = -1;
559 ca21e100 2021-02-04 op sock6 = -1;
560 ca21e100 2021-02-04 op }
561 ca21e100 2021-02-04 op
562 ca21e100 2021-02-04 op if (sock6 != -1 && old_ipv6 != conf.ipv6) {
563 ca21e100 2021-02-04 op close(sock6);
564 ca21e100 2021-02-04 op sock6 = -1;
565 ca21e100 2021-02-04 op }
566 ca21e100 2021-02-04 op
567 ca21e100 2021-02-04 op if (sock4 == -1)
568 ca21e100 2021-02-04 op sock4 = make_socket(conf.port, AF_INET);
569 ca21e100 2021-02-04 op if (sock6 == -1 && conf.ipv6)
570 ca21e100 2021-02-04 op sock6 = make_socket(conf.port, AF_INET6);
571 ca21e100 2021-02-04 op }
572 1d3eb470 2021-03-20 op
573 1d3eb470 2021-03-20 op imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
574 1d3eb470 2021-03-20 op imsg_flush(&exibuf);
575 1d3eb470 2021-03-20 op
576 1d3eb470 2021-03-20 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
577 1d3eb470 2021-03-20 op imsg_flush(&logibuf);
578 1d3eb470 2021-03-20 op
579 8e8b2e25 2021-04-28 op if (pidfd != -1)
580 8e8b2e25 2021-04-28 op close(pidfd);
581 8e8b2e25 2021-04-28 op
582 1d3eb470 2021-03-20 op return 0;
583 3e4749f7 2020-10-02 op }