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 70763d7f 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 f98e9045 2021-06-29 op static const char *opts = "6c:D:d:fH:hnP:p:Vvx:";
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 bc99d868 2021-03-19 op struct imsgbuf logibuf, exibuf, servibuf[PROC_MAX];
46 376a5407 2021-02-23 op
47 7b2d7432 2021-05-01 op const char *config_path, *certs_dir, *hostname, *pidfile, *cgi;
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 7e1df73d 2021-03-31 op /* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
61 7e1df73d 2021-03-31 op static void
62 7e1df73d 2021-03-31 op pdirname(const char *path, char *dn)
63 8443bff7 2021-01-25 op {
64 7e1df73d 2021-03-31 op char p[PATH_MAX+1];
65 7e1df73d 2021-03-31 op char *t;
66 7e1df73d 2021-03-31 op
67 7e1df73d 2021-03-31 op strlcpy(p, path, sizeof(p));
68 7e1df73d 2021-03-31 op t = dirname(p);
69 7e1df73d 2021-03-31 op memmove(dn, t, strlen(t)+1);
70 7e1df73d 2021-03-31 op }
71 7e1df73d 2021-03-31 op
72 7e1df73d 2021-03-31 op static void
73 7e1df73d 2021-03-31 op mkdirs(const char *path, mode_t mode)
74 7e1df73d 2021-03-31 op {
75 7e1df73d 2021-03-31 op char dname[PATH_MAX+1];
76 7e1df73d 2021-03-31 op
77 7e1df73d 2021-03-31 op pdirname(path, dname);
78 7e1df73d 2021-03-31 op if (!strcmp(dname, "/"))
79 7e1df73d 2021-03-31 op return;
80 7e1df73d 2021-03-31 op mkdirs(dname, mode);
81 7e1df73d 2021-03-31 op if (mkdir(path, mode) != 0 && errno != EEXIST)
82 8443bff7 2021-01-25 op fatal("can't mkdir %s: %s", path, strerror(errno));
83 8443bff7 2021-01-25 op }
84 8443bff7 2021-01-25 op
85 8443bff7 2021-01-25 op /* $XDG_DATA_HOME/gmid */
86 8443bff7 2021-01-25 op char *
87 8443bff7 2021-01-25 op data_dir(void)
88 8443bff7 2021-01-25 op {
89 8443bff7 2021-01-25 op const char *home, *xdg;
90 8443bff7 2021-01-25 op char *t;
91 8443bff7 2021-01-25 op
92 8443bff7 2021-01-25 op if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
93 8443bff7 2021-01-25 op if ((home = getenv("HOME")) == NULL)
94 8443bff7 2021-01-25 op errx(1, "XDG_DATA_HOME and HOME both empty");
95 8443bff7 2021-01-25 op if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
96 8443bff7 2021-01-25 op err(1, "asprintf");
97 7e1df73d 2021-03-31 op } else {
98 7e1df73d 2021-03-31 op if (asprintf(&t, "%s/gmid", xdg) == -1)
99 7e1df73d 2021-03-31 op err(1, "asprintf");
100 8443bff7 2021-01-25 op }
101 8443bff7 2021-01-25 op
102 7e1df73d 2021-03-31 op mkdirs(t, 0755);
103 8443bff7 2021-01-25 op return t;
104 d3a08f4d 2021-01-17 op }
105 a5d310bc 2020-11-10 op
106 d3a08f4d 2021-01-17 op void
107 8443bff7 2021-01-25 op load_local_cert(const char *hostname, const char *dir)
108 8443bff7 2021-01-25 op {
109 8443bff7 2021-01-25 op char *cert, *key;
110 b8e64ccd 2021-03-31 op struct vhost *h;
111 8443bff7 2021-01-25 op
112 8443bff7 2021-01-25 op if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
113 8443bff7 2021-01-25 op errx(1, "asprintf");
114 8443bff7 2021-01-25 op if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
115 8443bff7 2021-01-25 op errx(1, "asprintf");
116 8443bff7 2021-01-25 op
117 8443bff7 2021-01-25 op if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
118 8443bff7 2021-01-25 op gen_certificate(hostname, cert, key);
119 8443bff7 2021-01-25 op
120 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
121 b8e64ccd 2021-03-31 op h->cert = cert;
122 b8e64ccd 2021-03-31 op h->key = key;
123 b8e64ccd 2021-03-31 op h->domain = hostname;
124 8443bff7 2021-01-25 op }
125 8443bff7 2021-01-25 op
126 8443bff7 2021-01-25 op void
127 ae08ec7d 2021-01-25 op load_vhosts(void)
128 3e4749f7 2020-10-02 op {
129 fdea6aa0 2021-04-30 op struct vhost *h;
130 fdea6aa0 2021-04-30 op struct location *l;
131 15902770 2021-01-15 op
132 b8e64ccd 2021-03-31 op TAILQ_FOREACH(h, &hosts, vhosts) {
133 fdea6aa0 2021-04-30 op TAILQ_FOREACH(l, &h->locations, locations) {
134 fdea6aa0 2021-04-30 op if (l->dir == NULL)
135 fdea6aa0 2021-04-30 op continue;
136 fdea6aa0 2021-04-30 op if ((l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY)) == -1)
137 807869c1 2021-10-07 op fatal("open %s for domain %s: %s", l->dir, h->domain,
138 807869c1 2021-10-07 op strerror(errno));
139 fdea6aa0 2021-04-30 op }
140 9862b637 2021-01-13 op }
141 9862b637 2021-01-13 op }
142 592fd624 2020-10-07 op
143 3e4749f7 2020-10-02 op int
144 9468027b 2020-10-15 op make_socket(int port, int family)
145 3e4749f7 2020-10-02 op {
146 3e4749f7 2020-10-02 op int sock, v;
147 9468027b 2020-10-15 op struct sockaddr_in addr4;
148 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
149 9468027b 2020-10-15 op struct sockaddr *addr;
150 9468027b 2020-10-15 op socklen_t len;
151 3e4749f7 2020-10-02 op
152 81e0f000 2021-09-24 op switch (family) {
153 9468027b 2020-10-15 op case AF_INET:
154 df0c2926 2021-09-24 op memset(&addr4, 0, sizeof(addr4));
155 9468027b 2020-10-15 op addr4.sin_family = family;
156 9468027b 2020-10-15 op addr4.sin_port = htons(port);
157 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
158 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
159 9468027b 2020-10-15 op len = sizeof(addr4);
160 9468027b 2020-10-15 op break;
161 9468027b 2020-10-15 op
162 9468027b 2020-10-15 op case AF_INET6:
163 df0c2926 2021-09-24 op memset(&addr6, 0, sizeof(addr6));
164 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
165 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
166 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
167 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
168 9468027b 2020-10-15 op len = sizeof(addr6);
169 9468027b 2020-10-15 op break;
170 9468027b 2020-10-15 op
171 9468027b 2020-10-15 op default:
172 9468027b 2020-10-15 op /* unreachable */
173 9468027b 2020-10-15 op abort();
174 9468027b 2020-10-15 op }
175 9468027b 2020-10-15 op
176 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
177 80bbcad5 2021-01-10 op fatal("socket: %s", strerror(errno));
178 3e4749f7 2020-10-02 op
179 3e4749f7 2020-10-02 op v = 1;
180 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
181 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
182 3e4749f7 2020-10-02 op
183 3e4749f7 2020-10-02 op v = 1;
184 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
185 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
186 3e4749f7 2020-10-02 op
187 592fd624 2020-10-07 op mark_nonblock(sock);
188 592fd624 2020-10-07 op
189 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
190 80bbcad5 2021-01-10 op fatal("bind: %s", strerror(errno));
191 3e4749f7 2020-10-02 op
192 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
193 80bbcad5 2021-01-10 op fatal("listen: %s", strerror(errno));
194 3e4749f7 2020-10-02 op
195 3e4749f7 2020-10-02 op return sock;
196 3e4749f7 2020-10-02 op }
197 3e4749f7 2020-10-02 op
198 ff05125e 2021-10-15 op static void
199 ff05125e 2021-10-15 op add_keypair(struct vhost *h)
200 ff05125e 2021-10-15 op {
201 ff05125e 2021-10-15 op if (h->ocsp == NULL) {
202 ff05125e 2021-10-15 op if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
203 ff05125e 2021-10-15 op fatal("failed to load the keypair (%s, %s)",
204 ff05125e 2021-10-15 op h->cert, h->key);
205 ff05125e 2021-10-15 op } else {
206 7fa67176 2021-10-15 op if (tls_config_add_keypair_ocsp_file(tlsconf, h->cert, h->key,
207 7fa67176 2021-10-15 op h->ocsp) == -1)
208 ff05125e 2021-10-15 op fatal("failed to load the keypair (%s, %s, %s)",
209 ff05125e 2021-10-15 op h->cert, h->key, h->ocsp);
210 ff05125e 2021-10-15 op }
211 ff05125e 2021-10-15 op }
212 ff05125e 2021-10-15 op
213 ae08ec7d 2021-01-25 op void
214 ae08ec7d 2021-01-25 op setup_tls(void)
215 881a9dd9 2021-01-16 op {
216 ae08ec7d 2021-01-25 op struct vhost *h;
217 881a9dd9 2021-01-16 op
218 881a9dd9 2021-01-16 op if ((tlsconf = tls_config_new()) == NULL)
219 132cae8c 2021-01-18 op fatal("tls_config_new");
220 881a9dd9 2021-01-16 op
221 881a9dd9 2021-01-16 op /* optionally accept client certs, but don't try to verify them */
222 881a9dd9 2021-01-16 op tls_config_verify_client_optional(tlsconf);
223 881a9dd9 2021-01-16 op tls_config_insecure_noverifycert(tlsconf);
224 881a9dd9 2021-01-16 op
225 881a9dd9 2021-01-16 op if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
226 132cae8c 2021-01-18 op fatal("tls_config_set_protocols");
227 881a9dd9 2021-01-16 op
228 881a9dd9 2021-01-16 op if ((ctx = tls_server()) == NULL)
229 132cae8c 2021-01-18 op fatal("tls_server failure");
230 881a9dd9 2021-01-16 op
231 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
232 b8e64ccd 2021-03-31 op
233 ae08ec7d 2021-01-25 op /* we need to set something, then we can add how many key we want */
234 b8e64ccd 2021-03-31 op if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
235 ca21e100 2021-02-04 op fatal("tls_config_set_keypair_file failed for (%s, %s)",
236 b8e64ccd 2021-03-31 op h->cert, h->key);
237 7fa67176 2021-10-15 op
238 7fa67176 2021-10-15 op /* same for OCSP */
239 ff05125e 2021-10-15 op if (h->ocsp != NULL &&
240 ff05125e 2021-10-15 op tls_config_set_ocsp_staple_file(tlsconf, h->ocsp) == -1)
241 ff05125e 2021-10-15 op fatal("tls_config_set_ocsp_staple_file failed for (%s)",
242 ff05125e 2021-10-15 op h->ocsp);
243 881a9dd9 2021-01-16 op
244 ff05125e 2021-10-15 op while ((h = TAILQ_NEXT(h, vhosts)) != NULL)
245 ff05125e 2021-10-15 op add_keypair(h);
246 ae08ec7d 2021-01-25 op
247 881a9dd9 2021-01-16 op if (tls_configure(ctx, tlsconf) == -1)
248 132cae8c 2021-01-18 op fatal("tls_configure: %s", tls_error(ctx));
249 ae08ec7d 2021-01-25 op }
250 881a9dd9 2021-01-16 op
251 d672b8fb 2021-02-03 op static int
252 bc99d868 2021-03-19 op listener_main(struct imsgbuf *ibuf)
253 ae08ec7d 2021-01-25 op {
254 a709ddf5 2021-02-07 op drop_priv();
255 54203115 2022-04-08 op if (!conf.mime.skip_defaults && load_default_mime(&conf.mime) == -1)
256 d8d170aa 2022-04-08 op fatal("load_default_mime: %s", strerror(errno));
257 18bd8391 2022-04-08 op sort_mime(&conf.mime);
258 ae08ec7d 2021-01-25 op load_vhosts();
259 bc99d868 2021-03-19 op loop(ctx, sock4, sock6, ibuf);
260 881a9dd9 2021-01-16 op return 0;
261 3e4749f7 2020-10-02 op }
262 3e4749f7 2020-10-02 op
263 8d6ae384 2021-01-24 op void
264 8d6ae384 2021-01-24 op init_config(void)
265 3e4749f7 2020-10-02 op {
266 b8e64ccd 2021-03-31 op TAILQ_INIT(&hosts);
267 51d876f0 2020-12-21 op
268 15902770 2021-01-15 op conf.port = 1965;
269 15902770 2021-01-15 op conf.ipv6 = 0;
270 5bc3c98e 2021-01-15 op conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
271 85dff1f9 2021-01-11 op
272 b2a6b613 2021-01-21 op init_mime(&conf.mime);
273 ae08ec7d 2021-01-25 op
274 ae08ec7d 2021-01-25 op conf.chroot = NULL;
275 ae08ec7d 2021-01-25 op conf.user = NULL;
276 a709ddf5 2021-02-07 op
277 a709ddf5 2021-02-07 op conf.prefork = 3;
278 8d6ae384 2021-01-24 op }
279 0fbe79b3 2021-01-18 op
280 2030e314 2021-01-25 op void
281 ca21e100 2021-02-04 op free_config(void)
282 ca21e100 2021-02-04 op {
283 b8e64ccd 2021-03-31 op struct vhost *h, *th;
284 b8e64ccd 2021-03-31 op struct location *l, *tl;
285 b7967bc1 2021-01-02 op struct proxy *p, *tp;
286 9cc630aa 2021-04-28 op struct envlist *e, *te;
287 cc8c2901 2021-04-29 op struct alist *a, *ta;
288 8ad1c570 2021-05-09 op int v, i;
289 ca21e100 2021-02-04 op
290 419a4235 2021-04-28 op v = conf.verbose;
291 419a4235 2021-04-28 op
292 d8d170aa 2022-04-08 op free_mime(&conf.mime);
293 ca21e100 2021-02-04 op free(conf.chroot);
294 ca21e100 2021-02-04 op free(conf.user);
295 ca21e100 2021-02-04 op memset(&conf, 0, sizeof(conf));
296 419a4235 2021-04-28 op
297 419a4235 2021-04-28 op conf.verbose = v;
298 ca21e100 2021-02-04 op
299 b8e64ccd 2021-03-31 op TAILQ_FOREACH_SAFE(h, &hosts, vhosts, th) {
300 b8e64ccd 2021-03-31 op TAILQ_FOREACH_SAFE(l, &h->locations, locations, tl) {
301 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&h->locations, l, locations);
302 ca21e100 2021-02-04 op
303 ca21e100 2021-02-04 op free((char*)l->match);
304 ca21e100 2021-02-04 op free((char*)l->lang);
305 ca21e100 2021-02-04 op free((char*)l->default_mime);
306 ca21e100 2021-02-04 op free((char*)l->index);
307 6abda252 2021-02-06 op free((char*)l->block_fmt);
308 fdea6aa0 2021-04-30 op free((char*)l->dir);
309 fdea6aa0 2021-04-30 op
310 fdea6aa0 2021-04-30 op if (l->dirfd != -1)
311 fdea6aa0 2021-04-30 op close(l->dirfd);
312 fdea6aa0 2021-04-30 op
313 b8e64ccd 2021-03-31 op free(l);
314 ca21e100 2021-02-04 op }
315 b8e64ccd 2021-03-31 op
316 9cc630aa 2021-04-28 op TAILQ_FOREACH_SAFE(e, &h->env, envs, te) {
317 3b33eab3 2021-06-12 op TAILQ_REMOVE(&h->env, e, envs);
318 ab1e0169 2021-06-12 op
319 ab1e0169 2021-06-12 op free(e->name);
320 ab1e0169 2021-06-12 op free(e->value);
321 ab1e0169 2021-06-12 op free(e);
322 ab1e0169 2021-06-12 op }
323 3b33eab3 2021-06-12 op
324 ab1e0169 2021-06-12 op TAILQ_FOREACH_SAFE(e, &h->params, envs, te) {
325 ab1e0169 2021-06-12 op TAILQ_REMOVE(&h->params, e, envs);
326 ab1e0169 2021-06-12 op
327 9cc630aa 2021-04-28 op free(e->name);
328 9cc630aa 2021-04-28 op free(e->value);
329 9cc630aa 2021-04-28 op free(e);
330 cc8c2901 2021-04-29 op }
331 cc8c2901 2021-04-29 op
332 cc8c2901 2021-04-29 op TAILQ_FOREACH_SAFE(a, &h->aliases, aliases, ta) {
333 3b33eab3 2021-06-12 op TAILQ_REMOVE(&h->aliases, a, aliases);
334 3b33eab3 2021-06-12 op
335 cc8c2901 2021-04-29 op free(a->alias);
336 cc8c2901 2021-04-29 op free(a);
337 b7967bc1 2021-01-02 op }
338 b7967bc1 2021-01-02 op
339 b7967bc1 2021-01-02 op TAILQ_FOREACH_SAFE(p, &h->proxies, proxies, tp) {
340 b7967bc1 2021-01-02 op TAILQ_REMOVE(&h->proxies, p, proxies);
341 b7967bc1 2021-01-02 op
342 b7967bc1 2021-01-02 op free(p->match_proto);
343 b7967bc1 2021-01-02 op free(p->match_host);
344 b7967bc1 2021-01-02 op free(p->host);
345 1cdea97b 2022-01-30 op free(p->sni);
346 b7967bc1 2021-01-02 op tls_unload_file(p->cert, p->certlen);
347 b7967bc1 2021-01-02 op tls_unload_file(p->key, p->keylen);
348 b7967bc1 2021-01-02 op free(p);
349 9cc630aa 2021-04-28 op }
350 9cc630aa 2021-04-28 op
351 fdea6aa0 2021-04-30 op free((char*)h->domain);
352 fdea6aa0 2021-04-30 op free((char*)h->cert);
353 fdea6aa0 2021-04-30 op free((char*)h->key);
354 193380ea 2021-10-24 op free((char*)h->ocsp);
355 fdea6aa0 2021-04-30 op free((char*)h->cgi);
356 fdea6aa0 2021-04-30 op free((char*)h->entrypoint);
357 fdea6aa0 2021-04-30 op
358 b8e64ccd 2021-03-31 op TAILQ_REMOVE(&hosts, h, vhosts);
359 b8e64ccd 2021-03-31 op free(h);
360 8ad1c570 2021-05-09 op }
361 8ad1c570 2021-05-09 op
362 8ad1c570 2021-05-09 op for (i = 0; i < FCGI_MAX; ++i) {
363 8ad1c570 2021-05-09 op if (fcgi[i].path == NULL && fcgi[i].prog == NULL)
364 8ad1c570 2021-05-09 op break;
365 8ad1c570 2021-05-09 op free(fcgi[i].path);
366 8ad1c570 2021-05-09 op free(fcgi[i].port);
367 8ad1c570 2021-05-09 op free(fcgi[i].prog);
368 ef945cf4 2021-07-06 op
369 ef945cf4 2021-07-06 op fcgi[i].path = NULL;
370 ef945cf4 2021-07-06 op fcgi[i].port = NULL;
371 ef945cf4 2021-07-06 op fcgi[i].prog = NULL;
372 ca21e100 2021-02-04 op }
373 ca21e100 2021-02-04 op
374 ca21e100 2021-02-04 op tls_free(ctx);
375 ca21e100 2021-02-04 op tls_config_free(tlsconf);
376 ca21e100 2021-02-04 op }
377 ca21e100 2021-02-04 op
378 1d3eb470 2021-03-20 op static int
379 1d3eb470 2021-03-20 op wait_signal(void)
380 ca21e100 2021-02-04 op {
381 ca21e100 2021-02-04 op sigset_t mask;
382 ca21e100 2021-02-04 op int signo;
383 ca21e100 2021-02-04 op
384 ca21e100 2021-02-04 op sigemptyset(&mask);
385 ca21e100 2021-02-04 op sigaddset(&mask, SIGHUP);
386 1d3eb470 2021-03-20 op sigaddset(&mask, SIGINT);
387 1d3eb470 2021-03-20 op sigaddset(&mask, SIGTERM);
388 ca21e100 2021-02-04 op sigwait(&mask, &signo);
389 1d3eb470 2021-03-20 op
390 1d3eb470 2021-03-20 op return signo == SIGHUP;
391 ca21e100 2021-02-04 op }
392 ca21e100 2021-02-04 op
393 ca21e100 2021-02-04 op void
394 ae08ec7d 2021-01-25 op drop_priv(void)
395 ae08ec7d 2021-01-25 op {
396 ae08ec7d 2021-01-25 op struct passwd *pw = NULL;
397 ae08ec7d 2021-01-25 op
398 ae08ec7d 2021-01-25 op if (conf.chroot != NULL && conf.user == NULL)
399 ae08ec7d 2021-01-25 op fatal("can't chroot without an user to switch to after.");
400 ae08ec7d 2021-01-25 op
401 ae08ec7d 2021-01-25 op if (conf.user != NULL) {
402 ae08ec7d 2021-01-25 op if ((pw = getpwnam(conf.user)) == NULL)
403 ae08ec7d 2021-01-25 op fatal("can't find user %s", conf.user);
404 ae08ec7d 2021-01-25 op }
405 ae08ec7d 2021-01-25 op
406 ae08ec7d 2021-01-25 op if (conf.chroot != NULL) {
407 ae08ec7d 2021-01-25 op if (chroot(conf.chroot) != 0 || chdir("/") != 0)
408 ae08ec7d 2021-01-25 op fatal("%s: %s", conf.chroot, strerror(errno));
409 ae08ec7d 2021-01-25 op }
410 ae08ec7d 2021-01-25 op
411 ae08ec7d 2021-01-25 op if (pw != NULL) {
412 c197ad99 2022-11-29 op if (setgroups(1, &pw->pw_gid) == -1 ||
413 c197ad99 2022-11-29 op setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
414 c197ad99 2022-11-29 op setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
415 c197ad99 2022-11-29 op fatal("cannot drop privileges");
416 ae08ec7d 2021-01-25 op }
417 ae08ec7d 2021-01-25 op
418 ae08ec7d 2021-01-25 op if (getuid() == 0)
419 3abf91b0 2021-02-07 op log_warn(NULL, "not a good idea to run a network daemon as root");
420 2030e314 2021-01-25 op }
421 d672b8fb 2021-02-03 op
422 3abf91b0 2021-02-07 op static void
423 9327bc04 2021-06-29 op usage(void)
424 3abf91b0 2021-02-07 op {
425 3abf91b0 2021-02-07 op fprintf(stderr,
426 0be2a537 2021-06-29 op "Version: " GMID_STRING "\n"
427 f98e9045 2021-06-29 op "Usage: %s [-fnv] [-c config] [-D macro=value] [-P pidfile]\n"
428 0be2a537 2021-06-29 op " %s [-6hVv] [-d certs-dir] [-H hostname] [-p port] [-x cgi] [dir]\n",
429 0be2a537 2021-06-29 op getprogname(),
430 9327bc04 2021-06-29 op getprogname());
431 3abf91b0 2021-02-07 op }
432 3abf91b0 2021-02-07 op
433 376a5407 2021-02-23 op static void
434 376a5407 2021-02-23 op logger_init(void)
435 376a5407 2021-02-23 op {
436 376a5407 2021-02-23 op int p[2];
437 376a5407 2021-02-23 op
438 376a5407 2021-02-23 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
439 376a5407 2021-02-23 op err(1, "socketpair");
440 376a5407 2021-02-23 op
441 376a5407 2021-02-23 op switch (fork()) {
442 376a5407 2021-02-23 op case -1:
443 376a5407 2021-02-23 op err(1, "fork");
444 376a5407 2021-02-23 op case 0:
445 bc99d868 2021-03-19 op signal(SIGHUP, SIG_IGN);
446 376a5407 2021-02-23 op close(p[0]);
447 376a5407 2021-02-23 op setproctitle("logger");
448 bc99d868 2021-03-19 op imsg_init(&logibuf, p[1]);
449 376a5407 2021-02-23 op drop_priv();
450 bc99d868 2021-03-19 op _exit(logger_main(p[1], &logibuf));
451 376a5407 2021-02-23 op default:
452 376a5407 2021-02-23 op close(p[1]);
453 bc99d868 2021-03-19 op imsg_init(&logibuf, p[0]);
454 376a5407 2021-02-23 op return;
455 376a5407 2021-02-23 op }
456 376a5407 2021-02-23 op }
457 376a5407 2021-02-23 op
458 d672b8fb 2021-02-03 op static int
459 bc99d868 2021-03-19 op serve(int argc, char **argv, struct imsgbuf *ibuf)
460 d672b8fb 2021-02-03 op {
461 b8e64ccd 2021-03-31 op char path[PATH_MAX];
462 b8e64ccd 2021-03-31 op int i, p[2];
463 b8e64ccd 2021-03-31 op struct vhost *h;
464 b8e64ccd 2021-03-31 op struct location *l;
465 d672b8fb 2021-02-03 op
466 81e0f000 2021-09-24 op if (config_path == NULL) {
467 d672b8fb 2021-02-03 op if (hostname == NULL)
468 d672b8fb 2021-02-03 op hostname = "localhost";
469 d672b8fb 2021-02-03 op if (certs_dir == NULL)
470 d672b8fb 2021-02-03 op certs_dir = data_dir();
471 d672b8fb 2021-02-03 op load_local_cert(hostname, certs_dir);
472 d672b8fb 2021-02-03 op
473 b8e64ccd 2021-03-31 op h = TAILQ_FIRST(&hosts);
474 b8e64ccd 2021-03-31 op h->domain = "*";
475 d672b8fb 2021-02-03 op
476 b8e64ccd 2021-03-31 op l = TAILQ_FIRST(&h->locations);
477 b8e64ccd 2021-03-31 op l->auto_index = 1;
478 b8e64ccd 2021-03-31 op l->match = "*";
479 b8e64ccd 2021-03-31 op
480 d672b8fb 2021-02-03 op switch (argc) {
481 d672b8fb 2021-02-03 op case 0:
482 fdea6aa0 2021-04-30 op l->dir = getcwd(path, sizeof(path));
483 d672b8fb 2021-02-03 op break;
484 d672b8fb 2021-02-03 op case 1:
485 fdea6aa0 2021-04-30 op l->dir = absolutify_path(argv[0]);
486 d672b8fb 2021-02-03 op break;
487 d672b8fb 2021-02-03 op default:
488 9327bc04 2021-06-29 op usage();
489 d672b8fb 2021-02-03 op return 1;
490 d672b8fb 2021-02-03 op }
491 2030e314 2021-01-25 op
492 fdea6aa0 2021-04-30 op log_notice(NULL, "serving %s on port %d", l->dir, conf.port);
493 d672b8fb 2021-02-03 op }
494 d672b8fb 2021-02-03 op
495 d672b8fb 2021-02-03 op /* setup tls before dropping privileges: we don't want user
496 d672b8fb 2021-02-03 op * to put private certs inside the chroot. */
497 d672b8fb 2021-02-03 op setup_tls();
498 d672b8fb 2021-02-03 op
499 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
500 2c3e53da 2021-03-03 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
501 2c3e53da 2021-03-03 op PF_UNSPEC, p) == -1)
502 2c3e53da 2021-03-03 op fatal("socketpair: %s", strerror(errno));
503 d672b8fb 2021-02-03 op
504 2c3e53da 2021-03-03 op switch (fork()) {
505 2c3e53da 2021-03-03 op case -1:
506 2c3e53da 2021-03-03 op fatal("fork: %s", strerror(errno));
507 2c3e53da 2021-03-03 op case 0: /* child */
508 2c3e53da 2021-03-03 op close(p[0]);
509 bc99d868 2021-03-19 op imsg_init(&exibuf, p[1]);
510 2c3e53da 2021-03-03 op setproctitle("server");
511 bc99d868 2021-03-19 op _exit(listener_main(&exibuf));
512 2c3e53da 2021-03-03 op default:
513 2c3e53da 2021-03-03 op close(p[1]);
514 bc99d868 2021-03-19 op imsg_init(&servibuf[i], p[0]);
515 2c3e53da 2021-03-03 op }
516 d672b8fb 2021-02-03 op }
517 2c3e53da 2021-03-03 op
518 2c3e53da 2021-03-03 op setproctitle("executor");
519 2c3e53da 2021-03-03 op drop_priv();
520 bc99d868 2021-03-19 op _exit(executor_main(ibuf));
521 d672b8fb 2021-02-03 op }
522 419a4235 2021-04-28 op
523 419a4235 2021-04-28 op static int
524 419a4235 2021-04-28 op write_pidfile(const char *pidfile)
525 419a4235 2021-04-28 op {
526 419a4235 2021-04-28 op struct flock lock;
527 419a4235 2021-04-28 op int fd;
528 d672b8fb 2021-02-03 op
529 419a4235 2021-04-28 op if (pidfile == NULL)
530 419a4235 2021-04-28 op return -1;
531 419a4235 2021-04-28 op
532 419a4235 2021-04-28 op if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
533 419a4235 2021-04-28 op fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
534 419a4235 2021-04-28 op
535 419a4235 2021-04-28 op lock.l_start = 0;
536 419a4235 2021-04-28 op lock.l_len = 0;
537 419a4235 2021-04-28 op lock.l_type = F_WRLCK;
538 419a4235 2021-04-28 op lock.l_whence = SEEK_SET;
539 419a4235 2021-04-28 op
540 419a4235 2021-04-28 op if (fcntl(fd, F_SETLK, &lock) == -1)
541 419a4235 2021-04-28 op fatal("can't lock %s, gmid is already running?", pidfile);
542 419a4235 2021-04-28 op
543 419a4235 2021-04-28 op if (ftruncate(fd, 0) == -1)
544 419a4235 2021-04-28 op fatal("ftruncate: %s: %s", pidfile, strerror(errno));
545 419a4235 2021-04-28 op
546 419a4235 2021-04-28 op dprintf(fd, "%d\n", getpid());
547 419a4235 2021-04-28 op
548 419a4235 2021-04-28 op return fd;
549 419a4235 2021-04-28 op }
550 419a4235 2021-04-28 op
551 b8e64ccd 2021-03-31 op static void
552 b8e64ccd 2021-03-31 op setup_configless(int argc, char **argv, const char *cgi)
553 b8e64ccd 2021-03-31 op {
554 b8e64ccd 2021-03-31 op struct vhost *host;
555 b8e64ccd 2021-03-31 op struct location *loc;
556 b8e64ccd 2021-03-31 op
557 b8e64ccd 2021-03-31 op host = xcalloc(1, sizeof(*host));
558 b8e64ccd 2021-03-31 op host->cgi = cgi;
559 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&hosts, host, vhosts);
560 b8e64ccd 2021-03-31 op
561 b8e64ccd 2021-03-31 op loc = xcalloc(1, sizeof(*loc));
562 735336b2 2021-07-19 op loc->fcgi = -1;
563 b8e64ccd 2021-03-31 op TAILQ_INSERT_HEAD(&host->locations, loc, locations);
564 e952c505 2021-06-15 op
565 b8e64ccd 2021-03-31 op serve(argc, argv, NULL);
566 e952c505 2021-06-15 op
567 b8e64ccd 2021-03-31 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
568 b8e64ccd 2021-03-31 op imsg_flush(&logibuf);
569 391825e3 2021-07-09 op }
570 391825e3 2021-07-09 op
571 391825e3 2021-07-09 op static int
572 391825e3 2021-07-09 op parse_portno(const char *p)
573 391825e3 2021-07-09 op {
574 391825e3 2021-07-09 op const char *errstr;
575 391825e3 2021-07-09 op int n;
576 391825e3 2021-07-09 op
577 391825e3 2021-07-09 op n = strtonum(p, 0, UINT16_MAX, &errstr);
578 391825e3 2021-07-09 op if (errstr != NULL)
579 391825e3 2021-07-09 op yyerror("port number is %s: %s", errstr, p);
580 391825e3 2021-07-09 op return n;
581 b8e64ccd 2021-03-31 op }
582 b8e64ccd 2021-03-31 op
583 8d6ae384 2021-01-24 op int
584 8d6ae384 2021-01-24 op main(int argc, char **argv)
585 8d6ae384 2021-01-24 op {
586 d632468d 2021-03-20 op struct imsgbuf exibuf;
587 2c3e53da 2021-03-03 op int ch, conftest = 0, configless = 0;
588 8e8b2e25 2021-04-28 op int pidfd, old_ipv6, old_port;
589 501e489c 2021-01-24 op
590 8a50fc03 2021-07-07 op logger_init();
591 501e489c 2021-01-24 op init_config();
592 8d6ae384 2021-01-24 op
593 5777923b 2021-06-29 op while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
594 3e4749f7 2020-10-02 op switch (ch) {
595 85dff1f9 2021-01-11 op case '6':
596 15902770 2021-01-15 op conf.ipv6 = 1;
597 8443bff7 2021-01-25 op configless = 1;
598 85dff1f9 2021-01-11 op break;
599 85dff1f9 2021-01-11 op
600 3e4749f7 2020-10-02 op case 'c':
601 ca21e100 2021-02-04 op config_path = absolutify_path(optarg);
602 3e4749f7 2020-10-02 op break;
603 3e4749f7 2020-10-02 op
604 f98e9045 2021-06-29 op case 'D':
605 f98e9045 2021-06-29 op if (cmdline_symset(optarg) == -1)
606 8a50fc03 2021-07-07 op fatal("could not parse macro definition: %s",
607 8a50fc03 2021-07-07 op optarg);
608 f98e9045 2021-06-29 op break;
609 f98e9045 2021-06-29 op
610 3e4749f7 2020-10-02 op case 'd':
611 8443bff7 2021-01-25 op certs_dir = optarg;
612 8443bff7 2021-01-25 op configless = 1;
613 3e4749f7 2020-10-02 op break;
614 3e4749f7 2020-10-02 op
615 46af8c6c 2021-01-27 op case 'f':
616 3abf91b0 2021-02-07 op conf.foreground = 1;
617 46af8c6c 2021-01-27 op break;
618 46af8c6c 2021-01-27 op
619 8443bff7 2021-01-25 op case 'H':
620 8443bff7 2021-01-25 op hostname = optarg;
621 8443bff7 2021-01-25 op configless = 1;
622 d7802bb4 2020-12-02 op break;
623 d7802bb4 2020-12-02 op
624 3e4749f7 2020-10-02 op case 'h':
625 9327bc04 2021-06-29 op usage();
626 3e4749f7 2020-10-02 op return 0;
627 3e4749f7 2020-10-02 op
628 15902770 2021-01-15 op case 'n':
629 f0a01fc7 2021-10-09 op conftest++;
630 721e2325 2020-11-18 op break;
631 721e2325 2020-11-18 op
632 8e8b2e25 2021-04-28 op case 'P':
633 8e8b2e25 2021-04-28 op pidfile = optarg;
634 8e8b2e25 2021-04-28 op break;
635 8e8b2e25 2021-04-28 op
636 15902770 2021-01-15 op case 'p':
637 15902770 2021-01-15 op conf.port = parse_portno(optarg);
638 8443bff7 2021-01-25 op configless = 1;
639 7146dd55 2021-01-17 op break;
640 15902770 2021-01-15 op
641 5777923b 2021-06-29 op case 'V':
642 fdb43a4c 2021-06-29 op puts("Version: " GMID_STRING);
643 5777923b 2021-06-29 op return 0;
644 5777923b 2021-06-29 op
645 8904fa0e 2021-01-27 op case 'v':
646 3abf91b0 2021-02-07 op conf.verbose++;
647 8904fa0e 2021-01-27 op break;
648 8904fa0e 2021-01-27 op
649 72342dc9 2020-11-06 op case 'x':
650 15902770 2021-01-15 op /* drop the starting / (if any) */
651 15902770 2021-01-15 op if (*optarg == '/')
652 15902770 2021-01-15 op optarg++;
653 b8e64ccd 2021-03-31 op cgi = optarg;
654 8443bff7 2021-01-25 op configless = 1;
655 72342dc9 2020-11-06 op break;
656 72342dc9 2020-11-06 op
657 3e4749f7 2020-10-02 op default:
658 9327bc04 2021-06-29 op usage();
659 3e4749f7 2020-10-02 op return 1;
660 3e4749f7 2020-10-02 op }
661 3e4749f7 2020-10-02 op }
662 8443bff7 2021-01-25 op argc -= optind;
663 8443bff7 2021-01-25 op argv += optind;
664 3e4749f7 2020-10-02 op
665 d672b8fb 2021-02-03 op if (config_path == NULL) {
666 72bbed91 2021-01-27 op configless = 1;
667 3abf91b0 2021-02-07 op conf.foreground = 1;
668 2c3e53da 2021-03-03 op conf.prefork = 1;
669 3abf91b0 2021-02-07 op conf.verbose++;
670 15902770 2021-01-15 op }
671 15902770 2021-01-15 op
672 d672b8fb 2021-02-03 op if (config_path != NULL && (argc > 0 || configless))
673 8a50fc03 2021-07-07 op fatal("can't specify options in config mode.");
674 d672b8fb 2021-02-03 op
675 132cae8c 2021-01-18 op if (conftest) {
676 2e319276 2021-08-23 op if (config_path == NULL)
677 2e319276 2021-08-23 op fatal("missing configuration");
678 d672b8fb 2021-02-03 op parse_conf(config_path);
679 f0a01fc7 2021-10-09 op fprintf(stderr, "config OK\n");
680 f0a01fc7 2021-10-09 op if (conftest > 1)
681 f0a01fc7 2021-10-09 op print_conf();
682 132cae8c 2021-01-18 op return 0;
683 132cae8c 2021-01-18 op }
684 4a28dd01 2020-12-28 op
685 3abf91b0 2021-02-07 op if (!conf.foreground && !configless) {
686 8a50fc03 2021-07-07 op /* log to syslog */
687 8a50fc03 2021-07-07 op imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0);
688 8a50fc03 2021-07-07 op imsg_flush(&logibuf);
689 8a50fc03 2021-07-07 op
690 0170ba02 2021-01-17 op if (daemon(1, 1) == -1)
691 8a50fc03 2021-07-07 op fatal("daemon: %s", strerror(errno));
692 0170ba02 2021-01-17 op }
693 4a28dd01 2020-12-28 op
694 d672b8fb 2021-02-03 op if (config_path != NULL)
695 d672b8fb 2021-02-03 op parse_conf(config_path);
696 677afbd3 2020-12-02 op
697 d672b8fb 2021-02-03 op sock4 = make_socket(conf.port, AF_INET);
698 d672b8fb 2021-02-03 op sock6 = -1;
699 d672b8fb 2021-02-03 op if (conf.ipv6)
700 d672b8fb 2021-02-03 op sock6 = make_socket(conf.port, AF_INET6);
701 3e4749f7 2020-10-02 op
702 3841a369 2021-04-20 op signal(SIGPIPE, SIG_IGN);
703 3841a369 2021-04-20 op signal(SIGCHLD, SIG_IGN);
704 3841a369 2021-04-20 op
705 bc99d868 2021-03-19 op if (configless) {
706 b8e64ccd 2021-03-31 op setup_configless(argc, argv, cgi);
707 bc99d868 2021-03-19 op return 0;
708 bc99d868 2021-03-19 op }
709 ca21e100 2021-02-04 op
710 8e8b2e25 2021-04-28 op pidfd = write_pidfile(pidfile);
711 8e8b2e25 2021-04-28 op
712 a8a1f439 2021-07-07 op /*
713 a8a1f439 2021-07-07 op * Linux seems to call the event handlers even when we're
714 1b333122 2021-04-26 op * doing a sigwait. These dummy handlers are here to avoid
715 a8a1f439 2021-07-07 op * being terminated on SIGHUP, SIGINT or SIGTERM.
716 a8a1f439 2021-07-07 op */
717 b9c9123b 2021-03-20 op signal(SIGHUP, dummy_handler);
718 b9c9123b 2021-03-20 op signal(SIGINT, dummy_handler);
719 b9c9123b 2021-03-20 op signal(SIGTERM, dummy_handler);
720 b9c9123b 2021-03-20 op
721 ca21e100 2021-02-04 op /* wait a sighup and reload the daemon */
722 ca21e100 2021-02-04 op for (;;) {
723 bc99d868 2021-03-19 op int p[2];
724 ca21e100 2021-02-04 op
725 bc99d868 2021-03-19 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
726 bc99d868 2021-03-19 op PF_UNSPEC, p) == -1)
727 bc99d868 2021-03-19 op fatal("socketpair: %s", strerror(errno));
728 bc99d868 2021-03-19 op
729 ca21e100 2021-02-04 op switch (fork()) {
730 ca21e100 2021-02-04 op case -1:
731 ca21e100 2021-02-04 op fatal("fork: %s", strerror(errno));
732 ca21e100 2021-02-04 op case 0:
733 bc99d868 2021-03-19 op close(p[0]);
734 bc99d868 2021-03-19 op imsg_init(&exibuf, p[1]);
735 bc99d868 2021-03-19 op _exit(serve(argc, argv, &exibuf));
736 ca21e100 2021-02-04 op }
737 ca21e100 2021-02-04 op
738 bc99d868 2021-03-19 op close(p[1]);
739 bc99d868 2021-03-19 op imsg_init(&exibuf, p[0]);
740 bc99d868 2021-03-19 op
741 1d3eb470 2021-03-20 op if (!wait_signal())
742 1d3eb470 2021-03-20 op break;
743 1d3eb470 2021-03-20 op
744 3abf91b0 2021-02-07 op log_info(NULL, "reloading configuration %s", config_path);
745 ca21e100 2021-02-04 op
746 bc99d868 2021-03-19 op /* close the executor (it'll close the servers too) */
747 bc99d868 2021-03-19 op imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
748 bc99d868 2021-03-19 op imsg_flush(&exibuf);
749 bc99d868 2021-03-19 op close(p[0]);
750 bc99d868 2021-03-19 op
751 ca21e100 2021-02-04 op old_ipv6 = conf.ipv6;
752 ca21e100 2021-02-04 op old_port = conf.port;
753 ca21e100 2021-02-04 op
754 ca21e100 2021-02-04 op free_config();
755 ca21e100 2021-02-04 op init_config();
756 ca21e100 2021-02-04 op parse_conf(config_path);
757 ca21e100 2021-02-04 op
758 ca21e100 2021-02-04 op if (old_port != conf.port) {
759 ca21e100 2021-02-04 op close(sock4);
760 ca21e100 2021-02-04 op close(sock6);
761 ca21e100 2021-02-04 op sock4 = -1;
762 ca21e100 2021-02-04 op sock6 = -1;
763 ca21e100 2021-02-04 op }
764 ca21e100 2021-02-04 op
765 ca21e100 2021-02-04 op if (sock6 != -1 && old_ipv6 != conf.ipv6) {
766 ca21e100 2021-02-04 op close(sock6);
767 ca21e100 2021-02-04 op sock6 = -1;
768 ca21e100 2021-02-04 op }
769 ca21e100 2021-02-04 op
770 ca21e100 2021-02-04 op if (sock4 == -1)
771 ca21e100 2021-02-04 op sock4 = make_socket(conf.port, AF_INET);
772 ca21e100 2021-02-04 op if (sock6 == -1 && conf.ipv6)
773 ca21e100 2021-02-04 op sock6 = make_socket(conf.port, AF_INET6);
774 ca21e100 2021-02-04 op }
775 1d3eb470 2021-03-20 op
776 1d3eb470 2021-03-20 op imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
777 1d3eb470 2021-03-20 op imsg_flush(&exibuf);
778 1d3eb470 2021-03-20 op
779 1d3eb470 2021-03-20 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
780 1d3eb470 2021-03-20 op imsg_flush(&logibuf);
781 1d3eb470 2021-03-20 op
782 8e8b2e25 2021-04-28 op if (pidfd != -1)
783 8e8b2e25 2021-04-28 op close(pidfd);
784 8e8b2e25 2021-04-28 op
785 1d3eb470 2021-03-20 op return 0;
786 3e4749f7 2020-10-02 op }