Blame


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