Blame


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