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 bcf5d929 2021-02-01 op #include <limits.h>
24 ae08ec7d 2021-01-25 op #include <pwd.h>
25 0cf902af 2020-11-03 op #include <signal.h>
26 3e4749f7 2020-10-02 op #include <string.h>
27 cc68fe70 2020-10-07 op
28 15902770 2021-01-15 op struct vhost hosts[HOSTSLEN];
29 15902770 2021-01-15 op
30 bc99d868 2021-03-19 op int sock4, sock6;
31 d672b8fb 2021-02-03 op
32 bc99d868 2021-03-19 op struct imsgbuf logibuf, exibuf, servibuf[PROC_MAX];
33 376a5407 2021-02-23 op
34 d672b8fb 2021-02-03 op const char *config_path, *certs_dir, *hostname;
35 881a9dd9 2021-01-16 op
36 15902770 2021-01-15 op struct conf conf;
37 15902770 2021-01-15 op
38 ca21e100 2021-02-04 op struct tls_config *tlsconf;
39 ae08ec7d 2021-01-25 op struct tls *ctx;
40 0be51733 2021-01-20 op
41 b9c9123b 2021-03-20 op static void
42 b9c9123b 2021-03-20 op dummy_handler(int signo)
43 b9c9123b 2021-03-20 op {
44 b9c9123b 2021-03-20 op return;
45 b9c9123b 2021-03-20 op }
46 b9c9123b 2021-03-20 op
47 8443bff7 2021-01-25 op /* XXX: create recursively */
48 8443bff7 2021-01-25 op void
49 8443bff7 2021-01-25 op mkdirs(const char *path)
50 8443bff7 2021-01-25 op {
51 8443bff7 2021-01-25 op if (mkdir(path, 0755) == -1 && errno != EEXIST)
52 8443bff7 2021-01-25 op fatal("can't mkdir %s: %s", path, strerror(errno));
53 8443bff7 2021-01-25 op }
54 8443bff7 2021-01-25 op
55 8443bff7 2021-01-25 op /* $XDG_DATA_HOME/gmid */
56 8443bff7 2021-01-25 op char *
57 8443bff7 2021-01-25 op data_dir(void)
58 8443bff7 2021-01-25 op {
59 8443bff7 2021-01-25 op const char *home, *xdg;
60 8443bff7 2021-01-25 op char *t;
61 8443bff7 2021-01-25 op
62 8443bff7 2021-01-25 op if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
63 8443bff7 2021-01-25 op if ((home = getenv("HOME")) == NULL)
64 8443bff7 2021-01-25 op errx(1, "XDG_DATA_HOME and HOME both empty");
65 8443bff7 2021-01-25 op if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
66 8443bff7 2021-01-25 op err(1, "asprintf");
67 8443bff7 2021-01-25 op mkdirs(t);
68 8443bff7 2021-01-25 op return t;
69 8443bff7 2021-01-25 op }
70 8443bff7 2021-01-25 op
71 8443bff7 2021-01-25 op if (asprintf(&t, "%s/gmid", xdg) == -1)
72 8443bff7 2021-01-25 op err(1, "asprintf");
73 8443bff7 2021-01-25 op mkdirs(t);
74 8443bff7 2021-01-25 op return t;
75 d3a08f4d 2021-01-17 op }
76 a5d310bc 2020-11-10 op
77 d3a08f4d 2021-01-17 op void
78 8443bff7 2021-01-25 op load_local_cert(const char *hostname, const char *dir)
79 8443bff7 2021-01-25 op {
80 8443bff7 2021-01-25 op char *cert, *key;
81 8443bff7 2021-01-25 op
82 8443bff7 2021-01-25 op if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
83 8443bff7 2021-01-25 op errx(1, "asprintf");
84 8443bff7 2021-01-25 op if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
85 8443bff7 2021-01-25 op errx(1, "asprintf");
86 8443bff7 2021-01-25 op
87 8443bff7 2021-01-25 op if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
88 8443bff7 2021-01-25 op gen_certificate(hostname, cert, key);
89 8443bff7 2021-01-25 op
90 8443bff7 2021-01-25 op hosts[0].cert = cert;
91 8443bff7 2021-01-25 op hosts[0].key = key;
92 8443bff7 2021-01-25 op hosts[0].domain = hostname;
93 8443bff7 2021-01-25 op }
94 8443bff7 2021-01-25 op
95 8443bff7 2021-01-25 op void
96 ae08ec7d 2021-01-25 op load_vhosts(void)
97 3e4749f7 2020-10-02 op {
98 15902770 2021-01-15 op struct vhost *h;
99 15902770 2021-01-15 op
100 15902770 2021-01-15 op for (h = hosts; h->domain != NULL; ++h) {
101 d3a08f4d 2021-01-17 op if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
102 132cae8c 2021-01-18 op fatal("open %s for domain %s", h->dir, h->domain);
103 9862b637 2021-01-13 op }
104 9862b637 2021-01-13 op }
105 592fd624 2020-10-07 op
106 3e4749f7 2020-10-02 op int
107 9468027b 2020-10-15 op make_socket(int port, int family)
108 3e4749f7 2020-10-02 op {
109 3e4749f7 2020-10-02 op int sock, v;
110 9468027b 2020-10-15 op struct sockaddr_in addr4;
111 9468027b 2020-10-15 op struct sockaddr_in6 addr6;
112 9468027b 2020-10-15 op struct sockaddr *addr;
113 9468027b 2020-10-15 op socklen_t len;
114 3e4749f7 2020-10-02 op
115 9468027b 2020-10-15 op switch (family) {
116 9468027b 2020-10-15 op case AF_INET:
117 9468027b 2020-10-15 op bzero(&addr4, sizeof(addr4));
118 9468027b 2020-10-15 op addr4.sin_family = family;
119 9468027b 2020-10-15 op addr4.sin_port = htons(port);
120 9468027b 2020-10-15 op addr4.sin_addr.s_addr = INADDR_ANY;
121 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr4;
122 9468027b 2020-10-15 op len = sizeof(addr4);
123 9468027b 2020-10-15 op break;
124 9468027b 2020-10-15 op
125 9468027b 2020-10-15 op case AF_INET6:
126 9468027b 2020-10-15 op bzero(&addr6, sizeof(addr6));
127 9468027b 2020-10-15 op addr6.sin6_family = AF_INET6;
128 9468027b 2020-10-15 op addr6.sin6_port = htons(port);
129 9468027b 2020-10-15 op addr6.sin6_addr = in6addr_any;
130 9468027b 2020-10-15 op addr = (struct sockaddr*)&addr6;
131 9468027b 2020-10-15 op len = sizeof(addr6);
132 9468027b 2020-10-15 op break;
133 9468027b 2020-10-15 op
134 9468027b 2020-10-15 op default:
135 9468027b 2020-10-15 op /* unreachable */
136 9468027b 2020-10-15 op abort();
137 9468027b 2020-10-15 op }
138 9468027b 2020-10-15 op
139 9468027b 2020-10-15 op if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
140 80bbcad5 2021-01-10 op fatal("socket: %s", strerror(errno));
141 3e4749f7 2020-10-02 op
142 3e4749f7 2020-10-02 op v = 1;
143 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
144 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
145 3e4749f7 2020-10-02 op
146 3e4749f7 2020-10-02 op v = 1;
147 3e4749f7 2020-10-02 op if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
148 80bbcad5 2021-01-10 op fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
149 3e4749f7 2020-10-02 op
150 592fd624 2020-10-07 op mark_nonblock(sock);
151 592fd624 2020-10-07 op
152 9468027b 2020-10-15 op if (bind(sock, addr, len) == -1)
153 80bbcad5 2021-01-10 op fatal("bind: %s", strerror(errno));
154 3e4749f7 2020-10-02 op
155 3e4749f7 2020-10-02 op if (listen(sock, 16) == -1)
156 80bbcad5 2021-01-10 op fatal("listen: %s", strerror(errno));
157 3e4749f7 2020-10-02 op
158 3e4749f7 2020-10-02 op return sock;
159 3e4749f7 2020-10-02 op }
160 3e4749f7 2020-10-02 op
161 ae08ec7d 2021-01-25 op void
162 ae08ec7d 2021-01-25 op setup_tls(void)
163 881a9dd9 2021-01-16 op {
164 ae08ec7d 2021-01-25 op struct vhost *h;
165 881a9dd9 2021-01-16 op
166 881a9dd9 2021-01-16 op if ((tlsconf = tls_config_new()) == NULL)
167 132cae8c 2021-01-18 op fatal("tls_config_new");
168 881a9dd9 2021-01-16 op
169 881a9dd9 2021-01-16 op /* optionally accept client certs, but don't try to verify them */
170 881a9dd9 2021-01-16 op tls_config_verify_client_optional(tlsconf);
171 881a9dd9 2021-01-16 op tls_config_insecure_noverifycert(tlsconf);
172 881a9dd9 2021-01-16 op
173 881a9dd9 2021-01-16 op if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
174 132cae8c 2021-01-18 op fatal("tls_config_set_protocols");
175 881a9dd9 2021-01-16 op
176 881a9dd9 2021-01-16 op if ((ctx = tls_server()) == NULL)
177 132cae8c 2021-01-18 op fatal("tls_server failure");
178 881a9dd9 2021-01-16 op
179 ae08ec7d 2021-01-25 op /* we need to set something, then we can add how many key we want */
180 ae08ec7d 2021-01-25 op if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
181 ca21e100 2021-02-04 op fatal("tls_config_set_keypair_file failed for (%s, %s)",
182 ca21e100 2021-02-04 op hosts->cert, hosts->key);
183 881a9dd9 2021-01-16 op
184 8443bff7 2021-01-25 op for (h = &hosts[1]; h->domain != NULL; ++h) {
185 ae08ec7d 2021-01-25 op if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
186 ae08ec7d 2021-01-25 op fatal("failed to load the keypair (%s, %s)",
187 ae08ec7d 2021-01-25 op h->cert, h->key);
188 ae08ec7d 2021-01-25 op }
189 ae08ec7d 2021-01-25 op
190 881a9dd9 2021-01-16 op if (tls_configure(ctx, tlsconf) == -1)
191 132cae8c 2021-01-18 op fatal("tls_configure: %s", tls_error(ctx));
192 ae08ec7d 2021-01-25 op }
193 881a9dd9 2021-01-16 op
194 d672b8fb 2021-02-03 op static int
195 bc99d868 2021-03-19 op listener_main(struct imsgbuf *ibuf)
196 ae08ec7d 2021-01-25 op {
197 a709ddf5 2021-02-07 op drop_priv();
198 ae08ec7d 2021-01-25 op load_default_mime(&conf.mime);
199 ae08ec7d 2021-01-25 op load_vhosts();
200 bc99d868 2021-03-19 op loop(ctx, sock4, sock6, ibuf);
201 881a9dd9 2021-01-16 op return 0;
202 3e4749f7 2020-10-02 op }
203 3e4749f7 2020-10-02 op
204 8d6ae384 2021-01-24 op void
205 8d6ae384 2021-01-24 op init_config(void)
206 3e4749f7 2020-10-02 op {
207 15902770 2021-01-15 op size_t i;
208 120381c9 2020-11-06 op
209 15902770 2021-01-15 op bzero(hosts, sizeof(hosts));
210 15902770 2021-01-15 op for (i = 0; i < HOSTSLEN; ++i)
211 15902770 2021-01-15 op hosts[i].dirfd = -1;
212 51d876f0 2020-12-21 op
213 15902770 2021-01-15 op conf.port = 1965;
214 15902770 2021-01-15 op conf.ipv6 = 0;
215 5bc3c98e 2021-01-15 op conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
216 85dff1f9 2021-01-11 op
217 b2a6b613 2021-01-21 op init_mime(&conf.mime);
218 ae08ec7d 2021-01-25 op
219 ae08ec7d 2021-01-25 op conf.chroot = NULL;
220 ae08ec7d 2021-01-25 op conf.user = NULL;
221 a709ddf5 2021-02-07 op
222 a709ddf5 2021-02-07 op /* we'll change this to 0 when running without config. */
223 a709ddf5 2021-02-07 op conf.prefork = 3;
224 8d6ae384 2021-01-24 op }
225 0fbe79b3 2021-01-18 op
226 2030e314 2021-01-25 op void
227 ca21e100 2021-02-04 op free_config(void)
228 ca21e100 2021-02-04 op {
229 ca21e100 2021-02-04 op struct vhost *h;
230 ca21e100 2021-02-04 op struct location *l;
231 ca21e100 2021-02-04 op
232 ca21e100 2021-02-04 op free(conf.chroot);
233 ca21e100 2021-02-04 op free(conf.user);
234 ca21e100 2021-02-04 op memset(&conf, 0, sizeof(conf));
235 ca21e100 2021-02-04 op
236 ca21e100 2021-02-04 op for (h = hosts; h->domain != NULL; ++h) {
237 ca21e100 2021-02-04 op free((char*)h->domain);
238 ca21e100 2021-02-04 op free((char*)h->cert);
239 ca21e100 2021-02-04 op free((char*)h->key);
240 ca21e100 2021-02-04 op free((char*)h->dir);
241 ca21e100 2021-02-04 op free((char*)h->cgi);
242 e3ddf390 2021-02-06 op free((char*)h->entrypoint);
243 ca21e100 2021-02-04 op
244 ca21e100 2021-02-04 op for (l = h->locations; l->match != NULL; ++l) {
245 ca21e100 2021-02-04 op free((char*)l->match);
246 ca21e100 2021-02-04 op free((char*)l->lang);
247 ca21e100 2021-02-04 op free((char*)l->default_mime);
248 ca21e100 2021-02-04 op free((char*)l->index);
249 6abda252 2021-02-06 op free((char*)l->block_fmt);
250 ca21e100 2021-02-04 op }
251 ca21e100 2021-02-04 op }
252 ca21e100 2021-02-04 op memset(hosts, 0, sizeof(hosts));
253 ca21e100 2021-02-04 op
254 ca21e100 2021-02-04 op tls_free(ctx);
255 ca21e100 2021-02-04 op tls_config_free(tlsconf);
256 ca21e100 2021-02-04 op }
257 ca21e100 2021-02-04 op
258 1d3eb470 2021-03-20 op static int
259 1d3eb470 2021-03-20 op wait_signal(void)
260 ca21e100 2021-02-04 op {
261 ca21e100 2021-02-04 op sigset_t mask;
262 ca21e100 2021-02-04 op int signo;
263 ca21e100 2021-02-04 op
264 ca21e100 2021-02-04 op sigemptyset(&mask);
265 ca21e100 2021-02-04 op sigaddset(&mask, SIGHUP);
266 1d3eb470 2021-03-20 op sigaddset(&mask, SIGINT);
267 1d3eb470 2021-03-20 op sigaddset(&mask, SIGTERM);
268 ca21e100 2021-02-04 op sigwait(&mask, &signo);
269 1d3eb470 2021-03-20 op
270 1d3eb470 2021-03-20 op return signo == SIGHUP;
271 ca21e100 2021-02-04 op }
272 ca21e100 2021-02-04 op
273 ca21e100 2021-02-04 op void
274 ae08ec7d 2021-01-25 op drop_priv(void)
275 ae08ec7d 2021-01-25 op {
276 ae08ec7d 2021-01-25 op struct passwd *pw = NULL;
277 ae08ec7d 2021-01-25 op
278 ae08ec7d 2021-01-25 op if (conf.chroot != NULL && conf.user == NULL)
279 ae08ec7d 2021-01-25 op fatal("can't chroot without an user to switch to after.");
280 ae08ec7d 2021-01-25 op
281 ae08ec7d 2021-01-25 op if (conf.user != NULL) {
282 ae08ec7d 2021-01-25 op if ((pw = getpwnam(conf.user)) == NULL)
283 ae08ec7d 2021-01-25 op fatal("can't find user %s", conf.user);
284 ae08ec7d 2021-01-25 op }
285 ae08ec7d 2021-01-25 op
286 ae08ec7d 2021-01-25 op if (conf.chroot != NULL) {
287 ae08ec7d 2021-01-25 op if (chroot(conf.chroot) != 0 || chdir("/") != 0)
288 ae08ec7d 2021-01-25 op fatal("%s: %s", conf.chroot, strerror(errno));
289 ae08ec7d 2021-01-25 op }
290 ae08ec7d 2021-01-25 op
291 ae08ec7d 2021-01-25 op if (pw != NULL) {
292 ae08ec7d 2021-01-25 op if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
293 ae08ec7d 2021-01-25 op fatal("setresuid(%d): %s", pw->pw_uid,
294 ae08ec7d 2021-01-25 op strerror(errno));
295 ae08ec7d 2021-01-25 op }
296 ae08ec7d 2021-01-25 op
297 ae08ec7d 2021-01-25 op if (getuid() == 0)
298 3abf91b0 2021-02-07 op log_warn(NULL, "not a good idea to run a network daemon as root");
299 2030e314 2021-01-25 op }
300 d672b8fb 2021-02-03 op
301 3abf91b0 2021-02-07 op static void
302 3abf91b0 2021-02-07 op usage(const char *me)
303 3abf91b0 2021-02-07 op {
304 3abf91b0 2021-02-07 op fprintf(stderr,
305 dbe262a4 2021-03-03 op "USAGE: %s [-fn] [-c config] | [-6h] [-d certs-dir] [-H host]\n"
306 aa372875 2021-02-10 op " [-p port] [-x cgi] [dir]\n",
307 3abf91b0 2021-02-07 op me);
308 3abf91b0 2021-02-07 op }
309 3abf91b0 2021-02-07 op
310 376a5407 2021-02-23 op static void
311 376a5407 2021-02-23 op logger_init(void)
312 376a5407 2021-02-23 op {
313 376a5407 2021-02-23 op int p[2];
314 376a5407 2021-02-23 op
315 376a5407 2021-02-23 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
316 376a5407 2021-02-23 op err(1, "socketpair");
317 376a5407 2021-02-23 op
318 376a5407 2021-02-23 op switch (fork()) {
319 376a5407 2021-02-23 op case -1:
320 376a5407 2021-02-23 op err(1, "fork");
321 376a5407 2021-02-23 op case 0:
322 bc99d868 2021-03-19 op signal(SIGHUP, SIG_IGN);
323 376a5407 2021-02-23 op close(p[0]);
324 376a5407 2021-02-23 op setproctitle("logger");
325 bc99d868 2021-03-19 op imsg_init(&logibuf, p[1]);
326 376a5407 2021-02-23 op drop_priv();
327 bc99d868 2021-03-19 op _exit(logger_main(p[1], &logibuf));
328 376a5407 2021-02-23 op default:
329 376a5407 2021-02-23 op close(p[1]);
330 bc99d868 2021-03-19 op imsg_init(&logibuf, p[0]);
331 376a5407 2021-02-23 op return;
332 376a5407 2021-02-23 op }
333 376a5407 2021-02-23 op }
334 376a5407 2021-02-23 op
335 d672b8fb 2021-02-03 op static int
336 bc99d868 2021-03-19 op serve(int argc, char **argv, struct imsgbuf *ibuf)
337 d672b8fb 2021-02-03 op {
338 2c3e53da 2021-03-03 op char path[PATH_MAX];
339 2c3e53da 2021-03-03 op int i, p[2];
340 d672b8fb 2021-02-03 op
341 2c3e53da 2021-03-03 op if (config_path == NULL) {
342 d672b8fb 2021-02-03 op if (hostname == NULL)
343 d672b8fb 2021-02-03 op hostname = "localhost";
344 d672b8fb 2021-02-03 op if (certs_dir == NULL)
345 d672b8fb 2021-02-03 op certs_dir = data_dir();
346 d672b8fb 2021-02-03 op load_local_cert(hostname, certs_dir);
347 d672b8fb 2021-02-03 op
348 d672b8fb 2021-02-03 op hosts[0].domain = "*";
349 d672b8fb 2021-02-03 op hosts[0].locations[0].auto_index = 1;
350 d672b8fb 2021-02-03 op hosts[0].locations[0].match = "*";
351 d672b8fb 2021-02-03 op
352 d672b8fb 2021-02-03 op switch (argc) {
353 d672b8fb 2021-02-03 op case 0:
354 d672b8fb 2021-02-03 op hosts[0].dir = getcwd(path, sizeof(path));
355 d672b8fb 2021-02-03 op break;
356 d672b8fb 2021-02-03 op case 1:
357 d672b8fb 2021-02-03 op hosts[0].dir = absolutify_path(argv[0]);
358 d672b8fb 2021-02-03 op break;
359 d672b8fb 2021-02-03 op default:
360 d672b8fb 2021-02-03 op usage(getprogname());
361 d672b8fb 2021-02-03 op return 1;
362 d672b8fb 2021-02-03 op }
363 2030e314 2021-01-25 op
364 3abf91b0 2021-02-07 op log_notice(NULL, "serving %s on port %d", hosts[0].dir, conf.port);
365 d672b8fb 2021-02-03 op }
366 d672b8fb 2021-02-03 op
367 d672b8fb 2021-02-03 op /* setup tls before dropping privileges: we don't want user
368 d672b8fb 2021-02-03 op * to put private certs inside the chroot. */
369 d672b8fb 2021-02-03 op setup_tls();
370 d672b8fb 2021-02-03 op
371 2c3e53da 2021-03-03 op for (i = 0; i < conf.prefork; ++i) {
372 2c3e53da 2021-03-03 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
373 2c3e53da 2021-03-03 op PF_UNSPEC, p) == -1)
374 2c3e53da 2021-03-03 op fatal("socketpair: %s", strerror(errno));
375 d672b8fb 2021-02-03 op
376 2c3e53da 2021-03-03 op switch (fork()) {
377 2c3e53da 2021-03-03 op case -1:
378 2c3e53da 2021-03-03 op fatal("fork: %s", strerror(errno));
379 2c3e53da 2021-03-03 op case 0: /* child */
380 2c3e53da 2021-03-03 op close(p[0]);
381 bc99d868 2021-03-19 op imsg_init(&exibuf, p[1]);
382 2c3e53da 2021-03-03 op setproctitle("server");
383 bc99d868 2021-03-19 op _exit(listener_main(&exibuf));
384 2c3e53da 2021-03-03 op default:
385 2c3e53da 2021-03-03 op close(p[1]);
386 bc99d868 2021-03-19 op imsg_init(&servibuf[i], p[0]);
387 2c3e53da 2021-03-03 op }
388 d672b8fb 2021-02-03 op }
389 2c3e53da 2021-03-03 op
390 2c3e53da 2021-03-03 op setproctitle("executor");
391 2c3e53da 2021-03-03 op drop_priv();
392 bc99d868 2021-03-19 op _exit(executor_main(ibuf));
393 d672b8fb 2021-02-03 op }
394 d672b8fb 2021-02-03 op
395 8d6ae384 2021-01-24 op int
396 8d6ae384 2021-01-24 op main(int argc, char **argv)
397 8d6ae384 2021-01-24 op {
398 d632468d 2021-03-20 op struct imsgbuf exibuf;
399 2c3e53da 2021-03-03 op int ch, conftest = 0, configless = 0;
400 ca21e100 2021-02-04 op int old_ipv6, old_port;
401 501e489c 2021-01-24 op
402 501e489c 2021-01-24 op init_config();
403 8d6ae384 2021-01-24 op
404 8904fa0e 2021-01-27 op while ((ch = getopt(argc, argv, "6c:d:fH:hnp:vx:")) != -1) {
405 3e4749f7 2020-10-02 op switch (ch) {
406 85dff1f9 2021-01-11 op case '6':
407 15902770 2021-01-15 op conf.ipv6 = 1;
408 8443bff7 2021-01-25 op configless = 1;
409 85dff1f9 2021-01-11 op break;
410 85dff1f9 2021-01-11 op
411 3e4749f7 2020-10-02 op case 'c':
412 ca21e100 2021-02-04 op config_path = absolutify_path(optarg);
413 3e4749f7 2020-10-02 op break;
414 3e4749f7 2020-10-02 op
415 3e4749f7 2020-10-02 op case 'd':
416 8443bff7 2021-01-25 op certs_dir = optarg;
417 8443bff7 2021-01-25 op configless = 1;
418 3e4749f7 2020-10-02 op break;
419 3e4749f7 2020-10-02 op
420 46af8c6c 2021-01-27 op case 'f':
421 3abf91b0 2021-02-07 op conf.foreground = 1;
422 46af8c6c 2021-01-27 op break;
423 46af8c6c 2021-01-27 op
424 8443bff7 2021-01-25 op case 'H':
425 8443bff7 2021-01-25 op hostname = optarg;
426 8443bff7 2021-01-25 op configless = 1;
427 d7802bb4 2020-12-02 op break;
428 d7802bb4 2020-12-02 op
429 3e4749f7 2020-10-02 op case 'h':
430 3e4749f7 2020-10-02 op usage(*argv);
431 3e4749f7 2020-10-02 op return 0;
432 3e4749f7 2020-10-02 op
433 15902770 2021-01-15 op case 'n':
434 15902770 2021-01-15 op conftest = 1;
435 721e2325 2020-11-18 op break;
436 721e2325 2020-11-18 op
437 15902770 2021-01-15 op case 'p':
438 15902770 2021-01-15 op conf.port = parse_portno(optarg);
439 8443bff7 2021-01-25 op configless = 1;
440 7146dd55 2021-01-17 op break;
441 15902770 2021-01-15 op
442 8904fa0e 2021-01-27 op case 'v':
443 3abf91b0 2021-02-07 op conf.verbose++;
444 8904fa0e 2021-01-27 op break;
445 8904fa0e 2021-01-27 op
446 72342dc9 2020-11-06 op case 'x':
447 15902770 2021-01-15 op /* drop the starting / (if any) */
448 15902770 2021-01-15 op if (*optarg == '/')
449 15902770 2021-01-15 op optarg++;
450 15902770 2021-01-15 op hosts[0].cgi = optarg;
451 8443bff7 2021-01-25 op configless = 1;
452 72342dc9 2020-11-06 op break;
453 72342dc9 2020-11-06 op
454 3e4749f7 2020-10-02 op default:
455 3e4749f7 2020-10-02 op usage(*argv);
456 3e4749f7 2020-10-02 op return 1;
457 3e4749f7 2020-10-02 op }
458 3e4749f7 2020-10-02 op }
459 8443bff7 2021-01-25 op argc -= optind;
460 8443bff7 2021-01-25 op argv += optind;
461 3e4749f7 2020-10-02 op
462 d672b8fb 2021-02-03 op if (config_path == NULL) {
463 72bbed91 2021-01-27 op configless = 1;
464 3abf91b0 2021-02-07 op conf.foreground = 1;
465 2c3e53da 2021-03-03 op conf.prefork = 1;
466 3abf91b0 2021-02-07 op conf.verbose++;
467 15902770 2021-01-15 op }
468 15902770 2021-01-15 op
469 d672b8fb 2021-02-03 op if (config_path != NULL && (argc > 0 || configless))
470 d672b8fb 2021-02-03 op err(1, "can't specify options in config mode.");
471 d672b8fb 2021-02-03 op
472 132cae8c 2021-01-18 op if (conftest) {
473 d672b8fb 2021-02-03 op parse_conf(config_path);
474 132cae8c 2021-01-18 op puts("config OK");
475 132cae8c 2021-01-18 op return 0;
476 132cae8c 2021-01-18 op }
477 15902770 2021-01-15 op
478 4a28dd01 2020-12-28 op signal(SIGPIPE, SIG_IGN);
479 4a28dd01 2020-12-28 op signal(SIGCHLD, SIG_IGN);
480 4a28dd01 2020-12-28 op
481 3abf91b0 2021-02-07 op if (!conf.foreground && !configless) {
482 0170ba02 2021-01-17 op if (daemon(1, 1) == -1)
483 d278a0c3 2021-02-23 op err(1, "daemon");
484 0170ba02 2021-01-17 op }
485 4a28dd01 2020-12-28 op
486 d672b8fb 2021-02-03 op if (config_path != NULL)
487 d672b8fb 2021-02-03 op parse_conf(config_path);
488 677afbd3 2020-12-02 op
489 d278a0c3 2021-02-23 op logger_init();
490 d278a0c3 2021-02-23 op
491 d672b8fb 2021-02-03 op sock4 = make_socket(conf.port, AF_INET);
492 d672b8fb 2021-02-03 op sock6 = -1;
493 d672b8fb 2021-02-03 op if (conf.ipv6)
494 d672b8fb 2021-02-03 op sock6 = make_socket(conf.port, AF_INET6);
495 3e4749f7 2020-10-02 op
496 bc99d868 2021-03-19 op if (configless) {
497 bc99d868 2021-03-19 op serve(argc, argv, NULL);
498 bc99d868 2021-03-19 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
499 bc99d868 2021-03-19 op imsg_flush(&logibuf);
500 bc99d868 2021-03-19 op return 0;
501 bc99d868 2021-03-19 op }
502 ca21e100 2021-02-04 op
503 b9c9123b 2021-03-20 op
504 b9c9123b 2021-03-20 op /* Linux seems to call the event handlers even when we're
505 8e09ee1d 2021-03-20 op * doing a sigwait. These dummy handlers is here to avoid
506 b9c9123b 2021-03-20 op * being terminated on SIGHUP, SIGTERM or SIGINFO. */
507 b9c9123b 2021-03-20 op signal(SIGHUP, dummy_handler);
508 b9c9123b 2021-03-20 op signal(SIGINT, dummy_handler);
509 b9c9123b 2021-03-20 op signal(SIGTERM, dummy_handler);
510 b9c9123b 2021-03-20 op
511 ca21e100 2021-02-04 op /* wait a sighup and reload the daemon */
512 ca21e100 2021-02-04 op for (;;) {
513 bc99d868 2021-03-19 op int p[2];
514 ca21e100 2021-02-04 op
515 bc99d868 2021-03-19 op if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
516 bc99d868 2021-03-19 op PF_UNSPEC, p) == -1)
517 bc99d868 2021-03-19 op fatal("socketpair: %s", strerror(errno));
518 bc99d868 2021-03-19 op
519 ca21e100 2021-02-04 op switch (fork()) {
520 ca21e100 2021-02-04 op case -1:
521 ca21e100 2021-02-04 op fatal("fork: %s", strerror(errno));
522 ca21e100 2021-02-04 op case 0:
523 bc99d868 2021-03-19 op close(p[0]);
524 bc99d868 2021-03-19 op imsg_init(&exibuf, p[1]);
525 bc99d868 2021-03-19 op _exit(serve(argc, argv, &exibuf));
526 ca21e100 2021-02-04 op }
527 ca21e100 2021-02-04 op
528 bc99d868 2021-03-19 op close(p[1]);
529 bc99d868 2021-03-19 op imsg_init(&exibuf, p[0]);
530 bc99d868 2021-03-19 op
531 1d3eb470 2021-03-20 op if (!wait_signal())
532 1d3eb470 2021-03-20 op break;
533 1d3eb470 2021-03-20 op
534 3abf91b0 2021-02-07 op log_info(NULL, "reloading configuration %s", config_path);
535 ca21e100 2021-02-04 op
536 bc99d868 2021-03-19 op /* close the executor (it'll close the servers too) */
537 bc99d868 2021-03-19 op imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
538 bc99d868 2021-03-19 op imsg_flush(&exibuf);
539 bc99d868 2021-03-19 op close(p[0]);
540 bc99d868 2021-03-19 op
541 ca21e100 2021-02-04 op old_ipv6 = conf.ipv6;
542 ca21e100 2021-02-04 op old_port = conf.port;
543 ca21e100 2021-02-04 op
544 ca21e100 2021-02-04 op free_config();
545 ca21e100 2021-02-04 op init_config();
546 ca21e100 2021-02-04 op parse_conf(config_path);
547 ca21e100 2021-02-04 op
548 ca21e100 2021-02-04 op if (old_port != conf.port) {
549 ca21e100 2021-02-04 op close(sock4);
550 ca21e100 2021-02-04 op close(sock6);
551 ca21e100 2021-02-04 op sock4 = -1;
552 ca21e100 2021-02-04 op sock6 = -1;
553 ca21e100 2021-02-04 op }
554 ca21e100 2021-02-04 op
555 ca21e100 2021-02-04 op if (sock6 != -1 && old_ipv6 != conf.ipv6) {
556 ca21e100 2021-02-04 op close(sock6);
557 ca21e100 2021-02-04 op sock6 = -1;
558 ca21e100 2021-02-04 op }
559 ca21e100 2021-02-04 op
560 ca21e100 2021-02-04 op if (sock4 == -1)
561 ca21e100 2021-02-04 op sock4 = make_socket(conf.port, AF_INET);
562 ca21e100 2021-02-04 op if (sock6 == -1 && conf.ipv6)
563 ca21e100 2021-02-04 op sock6 = make_socket(conf.port, AF_INET6);
564 ca21e100 2021-02-04 op }
565 1d3eb470 2021-03-20 op
566 1d3eb470 2021-03-20 op imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
567 1d3eb470 2021-03-20 op imsg_flush(&exibuf);
568 1d3eb470 2021-03-20 op
569 1d3eb470 2021-03-20 op imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
570 1d3eb470 2021-03-20 op imsg_flush(&logibuf);
571 1d3eb470 2021-03-20 op
572 1d3eb470 2021-03-20 op return 0;
573 3e4749f7 2020-10-02 op }