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