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