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