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