2 * Copyright (c) 2020 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 volatile sig_atomic_t hupped;
30 struct vhost hosts[HOSTSLEN];
32 int exfd, sock4, sock6;
34 const char *config_path, *certs_dir, *hostname;
38 struct tls_config *tlsconf;
46 hupped = sig == SIGHUP;
49 /* XXX: create recursively */
51 mkdirs(const char *path)
53 if (mkdir(path, 0755) == -1 && errno != EEXIST)
54 fatal("can't mkdir %s: %s", path, strerror(errno));
57 /* $XDG_DATA_HOME/gmid */
61 const char *home, *xdg;
64 if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
65 if ((home = getenv("HOME")) == NULL)
66 errx(1, "XDG_DATA_HOME and HOME both empty");
67 if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
73 if (asprintf(&t, "%s/gmid", xdg) == -1)
80 load_local_cert(const char *hostname, const char *dir)
84 if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
86 if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
89 if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
90 gen_certificate(hostname, cert, key);
94 hosts[0].domain = hostname;
102 for (h = hosts; h->domain != NULL; ++h) {
103 if ((h->dirfd = open(h->dir, O_RDONLY | O_DIRECTORY)) == -1)
104 fatal("open %s for domain %s", h->dir, h->domain);
109 make_socket(int port, int family)
112 struct sockaddr_in addr4;
113 struct sockaddr_in6 addr6;
114 struct sockaddr *addr;
119 bzero(&addr4, sizeof(addr4));
120 addr4.sin_family = family;
121 addr4.sin_port = htons(port);
122 addr4.sin_addr.s_addr = INADDR_ANY;
123 addr = (struct sockaddr*)&addr4;
128 bzero(&addr6, sizeof(addr6));
129 addr6.sin6_family = AF_INET6;
130 addr6.sin6_port = htons(port);
131 addr6.sin6_addr = in6addr_any;
132 addr = (struct sockaddr*)&addr6;
141 if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
142 fatal("socket: %s", strerror(errno));
145 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
146 fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
149 if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
150 fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
154 if (bind(sock, addr, len) == -1)
155 fatal("bind: %s", strerror(errno));
157 if (listen(sock, 16) == -1)
158 fatal("listen: %s", strerror(errno));
168 if ((tlsconf = tls_config_new()) == NULL)
169 fatal("tls_config_new");
171 /* optionally accept client certs, but don't try to verify them */
172 tls_config_verify_client_optional(tlsconf);
173 tls_config_insecure_noverifycert(tlsconf);
175 if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
176 fatal("tls_config_set_protocols");
178 if ((ctx = tls_server()) == NULL)
179 fatal("tls_server failure");
181 /* we need to set something, then we can add how many key we want */
182 if (tls_config_set_keypair_file(tlsconf, hosts->cert, hosts->key))
183 fatal("tls_config_set_keypair_file failed for (%s, %s)",
184 hosts->cert, hosts->key);
186 for (h = &hosts[1]; h->domain != NULL; ++h) {
187 if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
188 fatal("failed to load the keypair (%s, %s)",
192 if (tls_configure(ctx, tlsconf) == -1)
193 fatal("tls_configure: %s", tls_error(ctx));
201 load_default_mime(&conf.mime);
203 loop(ctx, sock4, sock6);
212 bzero(hosts, sizeof(hosts));
213 for (i = 0; i < HOSTSLEN; ++i)
218 conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
220 init_mime(&conf.mime);
225 /* we'll change this to 0 when running without config. */
237 memset(&conf, 0, sizeof(conf));
239 for (h = hosts; h->domain != NULL; ++h) {
240 free((char*)h->domain);
241 free((char*)h->cert);
245 free((char*)h->entrypoint);
247 for (l = h->locations; l->match != NULL; ++l) {
248 free((char*)l->match);
249 free((char*)l->lang);
250 free((char*)l->default_mime);
251 free((char*)l->index);
252 free((char*)l->block_fmt);
255 memset(hosts, 0, sizeof(hosts));
258 tls_config_free(tlsconf);
268 sigaddset(&mask, SIGHUP);
269 sigwait(&mask, &signo);
275 struct passwd *pw = NULL;
277 if (conf.chroot != NULL && conf.user == NULL)
278 fatal("can't chroot without an user to switch to after.");
280 if (conf.user != NULL) {
281 if ((pw = getpwnam(conf.user)) == NULL)
282 fatal("can't find user %s", conf.user);
285 if (conf.chroot != NULL) {
286 if (chroot(conf.chroot) != 0 || chdir("/") != 0)
287 fatal("%s: %s", conf.chroot, strerror(errno));
291 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
292 fatal("setresuid(%d): %s", pw->pw_uid,
297 log_warn(NULL, "not a good idea to run a network daemon as root");
301 spawn_listeners(int *p)
308 for (i = 0; i < conf.prefork -1; ++i) {
311 fatal("fork: %s", strerror(errno));
313 setproctitle("server(%d)", i+1);
314 return listener_main();
318 if (conf.prefork == 0)
319 setproctitle("server");
321 setproctitle("server(%d)", conf.prefork);
322 return listener_main();
326 usage(const char *me)
329 "USAGE: %s [-fn] [-c config] | [-6h] [-d certs-dir] [-H host]"
330 " [-p port] [-x cgi] [dir]",
335 serve(int argc, char **argv, int *p)
339 if (config_path == NULL) {
340 if (hostname == NULL)
341 hostname = "localhost";
342 if (certs_dir == NULL)
343 certs_dir = data_dir();
344 load_local_cert(hostname, certs_dir);
346 hosts[0].domain = "*";
347 hosts[0].locations[0].auto_index = 1;
348 hosts[0].locations[0].match = "*";
352 hosts[0].dir = getcwd(path, sizeof(path));
355 hosts[0].dir = absolutify_path(argv[0]);
358 usage(getprogname());
362 log_notice(NULL, "serving %s on port %d", hosts[0].dir, conf.port);
365 /* setup tls before dropping privileges: we don't want user
366 * to put private certs inside the chroot. */
371 fatal("fork: %s", strerror(errno));
374 return spawn_listeners(p);
376 default: /* parent */
377 setproctitle("executor");
382 return executor_main();
387 main(int argc, char **argv)
390 int conftest = 0, configless = 0;
391 int old_ipv6, old_port;
395 while ((ch = getopt(argc, argv, "6c:d:fH:hnp:vx:")) != -1) {
403 config_path = absolutify_path(optarg);
429 conf.port = parse_portno(optarg);
438 /* drop the starting / (if any) */
441 hosts[0].cgi = optarg;
453 if (config_path == NULL) {
460 if (config_path != NULL && (argc > 0 || configless))
461 err(1, "can't specify options in config mode.");
464 parse_conf(config_path);
469 signal(SIGPIPE, SIG_IGN);
470 signal(SIGCHLD, SIG_IGN);
473 signal(SIGINFO, sig_handler);
475 signal(SIGUSR2, sig_handler);
476 signal(SIGHUP, sig_handler);
478 if (!conf.foreground && !configless) {
479 if (daemon(1, 1) == -1)
480 fatal("daemon: %s", strerror(errno));
483 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
485 fatal("socketpair: %s", strerror(errno));
487 if (config_path != NULL)
488 parse_conf(config_path);
490 sock4 = make_socket(conf.port, AF_INET);
493 sock6 = make_socket(conf.port, AF_INET6);
496 return serve(argc, argv, p);
498 /* wait a sighup and reload the daemon */
505 fatal("fork: %s", strerror(errno));
507 return serve(argc, argv, p);
515 log_info(NULL, "reloading configuration %s", config_path);
517 old_ipv6 = conf.ipv6;
518 old_port = conf.port;
522 parse_conf(config_path);
524 if (old_port != conf.port) {
531 if (sock6 != -1 && old_ipv6 != conf.ipv6) {
537 sock4 = make_socket(conf.port, AF_INET);
538 if (sock6 == -1 && conf.ipv6)
539 sock6 = make_socket(conf.port, AF_INET6);
541 if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
543 fatal("socketpair: %s", strerror(errno));