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 5c485529 2022-09-10 op struct imsgbuf logibuf, 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 534afd0d 2022-10-05 op if (*l->dir == '\0')
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 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);
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 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)",
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 534afd0d 2022-10-05 op int v;
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 fdea6aa0 2021-04-30 op if (l->dirfd != -1)
233 fdea6aa0 2021-04-30 op close(l->dirfd);
234 fdea6aa0 2021-04-30 op
235 b8e64ccd 2021-03-31 op free(l);
236 ab1e0169 2021-06-12 op }
237 3b33eab3 2021-06-12 op
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);
240 9cc630aa 2021-04-28 op free(e);
241 cc8c2901 2021-04-29 op }
242 cc8c2901 2021-04-29 op
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);
245 cc8c2901 2021-04-29 op free(a);
246 b7967bc1 2021-01-02 op }
247 b7967bc1 2021-01-02 op
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);
252 b7967bc1 2021-01-02 op free(p);
253 9cc630aa 2021-04-28 op }
254 fdea6aa0 2021-04-30 op
255 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&hosts, h, vhosts);
256 b8e64ccd 2021-03-31 op free(h);
257 8ad1c570 2021-05-09 op }
258 8ad1c570 2021-05-09 op
259 534afd0d 2022-10-05 op memset(fcgi, 0, sizeof(fcgi));
260 ca21e100 2021-02-04 op
261 ca21e100 2021-02-04 op tls_free(ctx);
262 ca21e100 2021-02-04 op tls_config_free(tlsconf);
263 ca21e100 2021-02-04 op }
264 ca21e100 2021-02-04 op
265 1d3eb470 2021-03-20 op static int
266 1d3eb470 2021-03-20 op wait_signal(void)
267 ca21e100 2021-02-04 op {
268 ca21e100 2021-02-04 op sigset_t mask;
269 ca21e100 2021-02-04 op int signo;
270 ca21e100 2021-02-04 op
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);
276 1d3eb470 2021-03-20 op
277 1d3eb470 2021-03-20 op return signo == SIGHUP;
278 ca21e100 2021-02-04 op }
279 ca21e100 2021-02-04 op
280 ca21e100 2021-02-04 op void
281 ae08ec7d 2021-01-25 op drop_priv(void)
282 ae08ec7d 2021-01-25 op {
283 ae08ec7d 2021-01-25 op struct passwd *pw = NULL;
284 ae08ec7d 2021-01-25 op
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.");
287 ae08ec7d 2021-01-25 op
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);
291 ae08ec7d 2021-01-25 op }
292 ae08ec7d 2021-01-25 op
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));
296 ae08ec7d 2021-01-25 op }
297 ae08ec7d 2021-01-25 op
298 ae08ec7d 2021-01-25 op if (pw != NULL) {
299 ae08ec7d 2021-01-25 op if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
300 ae08ec7d 2021-01-25 op fatal("setresuid(%d): %s", pw->pw_uid,
301 ae08ec7d 2021-01-25 op strerror(errno));
302 ae08ec7d 2021-01-25 op }
303 ae08ec7d 2021-01-25 op
304 ae08ec7d 2021-01-25 op if (getuid() == 0)
305 3abf91b0 2021-02-07 op log_warn(NULL, "not a good idea to run a network daemon as root");
306 2030e314 2021-01-25 op }
307 d672b8fb 2021-02-03 op
308 3abf91b0 2021-02-07 op static void
309 9327bc04 2021-06-29 op usage(void)
310 3abf91b0 2021-02-07 op {
311 3abf91b0 2021-02-07 op fprintf(stderr,
312 0be2a537 2021-06-29 op "Version: " GMID_STRING "\n"
313 aae8f6bf 2022-09-08 op "Usage: %s [-dhnVv] [-D macro=value] [-f config] [-P pidfile]\n",
314 9327bc04 2021-06-29 op getprogname());
315 3abf91b0 2021-02-07 op }
316 3abf91b0 2021-02-07 op
317 376a5407 2021-02-23 op static void
318 376a5407 2021-02-23 op logger_init(void)
319 376a5407 2021-02-23 op {
320 376a5407 2021-02-23 op int p[2];
321 376a5407 2021-02-23 op
322 376a5407 2021-02-23 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
323 376a5407 2021-02-23 op err(1, "socketpair");
324 376a5407 2021-02-23 op
325 376a5407 2021-02-23 op switch (fork()) {
326 376a5407 2021-02-23 op case -1:
327 376a5407 2021-02-23 op err(1, "fork");
328 376a5407 2021-02-23 op case 0:
329 bc99d868 2021-03-19 op signal(SIGHUP, SIG_IGN);
330 376a5407 2021-02-23 op close(p[0]);
331 376a5407 2021-02-23 op setproctitle("logger");
332 bc99d868 2021-03-19 op imsg_init(&logibuf, p[1]);
333 376a5407 2021-02-23 op drop_priv();
334 bc99d868 2021-03-19 op _exit(logger_main(p[1], &logibuf));
335 376a5407 2021-02-23 op default:
336 376a5407 2021-02-23 op close(p[1]);
337 bc99d868 2021-03-19 op imsg_init(&logibuf, p[0]);
338 376a5407 2021-02-23 op return;
339 376a5407 2021-02-23 op }
340 376a5407 2021-02-23 op }
341 376a5407 2021-02-23 op
342 d29a2ee2 2022-09-06 op static void
343 d29a2ee2 2022-09-06 op serve(void)
344 d672b8fb 2021-02-03 op {
345 d29a2ee2 2022-09-06 op int i, p[2];
346 2030e314 2021-01-25 op
347 d672b8fb 2021-02-03 op /* setup tls before dropping privileges: we don't want user
348 d672b8fb 2021-02-03 op * to put private certs inside the chroot. */
349 d672b8fb 2021-02-03 op setup_tls();
350 d672b8fb 2021-02-03 op
351 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
352 2c3e53da 2021-03-03 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
353 2c3e53da 2021-03-03 op PF_UNSPEC, p) == -1)
354 2c3e53da 2021-03-03 op fatal("socketpair: %s", strerror(errno));
355 d672b8fb 2021-02-03 op
356 2c3e53da 2021-03-03 op switch (fork()) {
357 2c3e53da 2021-03-03 op case -1:
358 2c3e53da 2021-03-03 op fatal("fork: %s", strerror(errno));
359 2c3e53da 2021-03-03 op case 0: /* child */
360 2c3e53da 2021-03-03 op close(p[0]);
361 5c485529 2022-09-10 op imsg_init(&servibuf[i], p[1]);
362 2c3e53da 2021-03-03 op setproctitle("server");
363 5c485529 2022-09-10 op _exit(listener_main(&servibuf[i]));
364 2c3e53da 2021-03-03 op default:
365 2c3e53da 2021-03-03 op close(p[1]);
366 bc99d868 2021-03-19 op imsg_init(&servibuf[i], p[0]);
367 2c3e53da 2021-03-03 op }
368 d672b8fb 2021-02-03 op }
369 d672b8fb 2021-02-03 op }
370 419a4235 2021-04-28 op
371 419a4235 2021-04-28 op static int
372 419a4235 2021-04-28 op write_pidfile(const char *pidfile)
373 419a4235 2021-04-28 op {
374 419a4235 2021-04-28 op struct flock lock;
375 419a4235 2021-04-28 op int fd;
376 d672b8fb 2021-02-03 op
377 419a4235 2021-04-28 op if (pidfile == NULL)
378 419a4235 2021-04-28 op return -1;
379 419a4235 2021-04-28 op
380 419a4235 2021-04-28 op if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
381 419a4235 2021-04-28 op fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
382 419a4235 2021-04-28 op
383 419a4235 2021-04-28 op lock.l_start = 0;
384 419a4235 2021-04-28 op lock.l_len = 0;
385 419a4235 2021-04-28 op lock.l_type = F_WRLCK;
386 419a4235 2021-04-28 op lock.l_whence = SEEK_SET;
387 419a4235 2021-04-28 op
388 419a4235 2021-04-28 op if (fcntl(fd, F_SETLK, &lock) == -1)
389 419a4235 2021-04-28 op fatal("can't lock %s, gmid is already running?", pidfile);
390 419a4235 2021-04-28 op
391 419a4235 2021-04-28 op if (ftruncate(fd, 0) == -1)
392 419a4235 2021-04-28 op fatal("ftruncate: %s: %s", pidfile, strerror(errno));
393 419a4235 2021-04-28 op
394 419a4235 2021-04-28 op dprintf(fd, "%d\n", getpid());
395 419a4235 2021-04-28 op
396 419a4235 2021-04-28 op return fd;
397 419a4235 2021-04-28 op }
398 419a4235 2021-04-28 op
399 8d6ae384 2021-01-24 op int
400 8d6ae384 2021-01-24 op main(int argc, char **argv)
401 8d6ae384 2021-01-24 op {
402 32fbc478 2022-09-08 op int i, ch, conftest = 0;
403 8e8b2e25 2021-04-28 op int pidfd, old_ipv6, old_port;
404 501e489c 2021-01-24 op
405 8a50fc03 2021-07-07 op logger_init();
406 501e489c 2021-01-24 op init_config();
407 8d6ae384 2021-01-24 op
408 5777923b 2021-06-29 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
409 3e4749f7 2020-10-02 op switch (ch) {
410 f98e9045 2021-06-29 op case 'D':
411 f98e9045 2021-06-29 op if (cmdline_symset(optarg) == -1)
412 8a50fc03 2021-07-07 op fatal("could not parse macro definition: %s",
413 8a50fc03 2021-07-07 op optarg);
414 f98e9045 2021-06-29 op break;
415 aae8f6bf 2022-09-08 op case 'd':
416 3abf91b0 2021-02-07 op conf.foreground = 1;
417 46af8c6c 2021-01-27 op break;
418 aae8f6bf 2022-09-08 op case 'f':
419 aae8f6bf 2022-09-08 op config_path = absolutify_path(optarg);
420 aae8f6bf 2022-09-08 op break;
421 3e4749f7 2020-10-02 op case 'h':
422 9327bc04 2021-06-29 op usage();
423 3e4749f7 2020-10-02 op return 0;
424 15902770 2021-01-15 op case 'n':
425 f0a01fc7 2021-10-09 op conftest++;
426 721e2325 2020-11-18 op break;
427 8e8b2e25 2021-04-28 op case 'P':
428 8e8b2e25 2021-04-28 op pidfile = optarg;
429 8e8b2e25 2021-04-28 op break;
430 5777923b 2021-06-29 op case 'V':
431 fdb43a4c 2021-06-29 op puts("Version: " GMID_STRING);
432 5777923b 2021-06-29 op return 0;
433 8904fa0e 2021-01-27 op case 'v':
434 3abf91b0 2021-02-07 op conf.verbose++;
435 8904fa0e 2021-01-27 op break;
436 3e4749f7 2020-10-02 op default:
437 9327bc04 2021-06-29 op usage();
438 3e4749f7 2020-10-02 op return 1;
439 3e4749f7 2020-10-02 op }
440 3e4749f7 2020-10-02 op }
441 8443bff7 2021-01-25 op argc -= optind;
442 8443bff7 2021-01-25 op argv += optind;
443 3e4749f7 2020-10-02 op
444 32fbc478 2022-09-08 op if (argc != 0)
445 d29a2ee2 2022-09-06 op usage();
446 d29a2ee2 2022-09-06 op
447 32fbc478 2022-09-08 op parse_conf(config_path);
448 d672b8fb 2021-02-03 op
449 132cae8c 2021-01-18 op if (conftest) {
450 f0a01fc7 2021-10-09 op fprintf(stderr, "config OK\n");
451 f0a01fc7 2021-10-09 op if (conftest > 1)
452 f0a01fc7 2021-10-09 op print_conf();
453 132cae8c 2021-01-18 op return 0;
454 132cae8c 2021-01-18 op }
455 4a28dd01 2020-12-28 op
456 32fbc478 2022-09-08 op if (!conf.foreground) {
457 8a50fc03 2021-07-07 op /* log to syslog */
458 8a50fc03 2021-07-07 op imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0);
459 8a50fc03 2021-07-07 op imsg_flush(&logibuf);
460 8a50fc03 2021-07-07 op
461 0170ba02 2021-01-17 op if (daemon(1, 1) == -1)
462 8a50fc03 2021-07-07 op fatal("daemon: %s", strerror(errno));
463 0170ba02 2021-01-17 op }
464 4a28dd01 2020-12-28 op
465 d672b8fb 2021-02-03 op sock4 = make_socket(conf.port, AF_INET);
466 d672b8fb 2021-02-03 op sock6 = -1;
467 d672b8fb 2021-02-03 op if (conf.ipv6)
468 d672b8fb 2021-02-03 op sock6 = make_socket(conf.port, AF_INET6);
469 3e4749f7 2020-10-02 op
470 3841a369 2021-04-20 op signal(SIGPIPE, SIG_IGN);
471 3841a369 2021-04-20 op
472 8e8b2e25 2021-04-28 op pidfd = write_pidfile(pidfile);
473 8e8b2e25 2021-04-28 op
474 a8a1f439 2021-07-07 op /*
475 a8a1f439 2021-07-07 op * Linux seems to call the event handlers even when we're
476 1b333122 2021-04-26 op * doing a sigwait. These dummy handlers are here to avoid
477 a8a1f439 2021-07-07 op * being terminated on SIGHUP, SIGINT or SIGTERM.
478 a8a1f439 2021-07-07 op */
479 b9c9123b 2021-03-20 op signal(SIGHUP, dummy_handler);
480 b9c9123b 2021-03-20 op signal(SIGINT, dummy_handler);
481 b9c9123b 2021-03-20 op signal(SIGTERM, dummy_handler);
482 b9c9123b 2021-03-20 op
483 ca21e100 2021-02-04 op /* wait a sighup and reload the daemon */
484 ca21e100 2021-02-04 op for (;;) {
485 d29a2ee2 2022-09-06 op serve();
486 ca21e100 2021-02-04 op
487 32fbc478 2022-09-08 op if (!wait_signal())
488 1d3eb470 2021-03-20 op break;
489 1d3eb470 2021-03-20 op
490 3abf91b0 2021-02-07 op log_info(NULL, "reloading configuration %s", config_path);
491 ca21e100 2021-02-04 op
492 d29a2ee2 2022-09-06 op /* close the servers */
493 d29a2ee2 2022-09-06 op for (i = 0; i < conf.prefork; ++i) {
494 d29a2ee2 2022-09-06 op imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
495 d29a2ee2 2022-09-06 op imsg_flush(&servibuf[i]);
496 d29a2ee2 2022-09-06 op close(servibuf[i].fd);
497 d29a2ee2 2022-09-06 op }
498 bc99d868 2021-03-19 op
499 ca21e100 2021-02-04 op old_ipv6 = conf.ipv6;
500 ca21e100 2021-02-04 op old_port = conf.port;
501 ca21e100 2021-02-04 op
502 ca21e100 2021-02-04 op free_config();
503 ca21e100 2021-02-04 op init_config();
504 ca21e100 2021-02-04 op parse_conf(config_path);
505 ca21e100 2021-02-04 op
506 ca21e100 2021-02-04 op if (old_port != conf.port) {
507 ca21e100 2021-02-04 op close(sock4);
508 ca21e100 2021-02-04 op close(sock6);
509 ca21e100 2021-02-04 op sock4 = -1;
510 ca21e100 2021-02-04 op sock6 = -1;
511 ca21e100 2021-02-04 op }
512 ca21e100 2021-02-04 op
513 ca21e100 2021-02-04 op if (sock6 != -1 && old_ipv6 != conf.ipv6) {
514 ca21e100 2021-02-04 op close(sock6);
515 ca21e100 2021-02-04 op sock6 = -1;
516 ca21e100 2021-02-04 op }
517 ca21e100 2021-02-04 op
518 ca21e100 2021-02-04 op if (sock4 == -1)
519 ca21e100 2021-02-04 op sock4 = make_socket(conf.port, AF_INET);
520 ca21e100 2021-02-04 op if (sock6 == -1 && conf.ipv6)
521 ca21e100 2021-02-04 op sock6 = make_socket(conf.port, AF_INET6);
522 ca21e100 2021-02-04 op }
523 1d3eb470 2021-03-20 op
524 5c485529 2022-09-10 op for (i = 0; i < conf.prefork; ++i) {
525 5c485529 2022-09-10 op imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
526 5c485529 2022-09-10 op imsg_flush(&servibuf[i]);
527 5c485529 2022-09-10 op close(servibuf[i].fd);
528 5c485529 2022-09-10 op }
529 1d3eb470 2021-03-20 op
530 1d3eb470 2021-03-20 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
531 1d3eb470 2021-03-20 op imsg_flush(&logibuf);
532 5c485529 2022-09-10 op close(logibuf.fd);
533 1d3eb470 2021-03-20 op
534 8e8b2e25 2021-04-28 op if (pidfd != -1)
535 8e8b2e25 2021-04-28 op close(pidfd);
536 8e8b2e25 2021-04-28 op
537 1d3eb470 2021-03-20 op return 0;
538 3e4749f7 2020-10-02 op }