2020-10-02 19:39:00 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Omar Polo <op@omarpolo.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-02-12 13:47:20 +01:00
|
|
|
#include "gmid.h"
|
|
|
|
|
2021-01-25 15:08:31 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2020-10-07 13:50:18 +02:00
|
|
|
#include <errno.h>
|
2020-10-02 19:39:00 +02:00
|
|
|
#include <fcntl.h>
|
2021-06-29 12:49:59 +02:00
|
|
|
#include <getopt.h>
|
2021-03-31 15:56:58 +02:00
|
|
|
#include <libgen.h>
|
2020-12-21 14:38:31 +01:00
|
|
|
#include <limits.h>
|
2021-01-25 11:30:07 +01:00
|
|
|
#include <pwd.h>
|
2020-11-03 15:48:26 +01:00
|
|
|
#include <signal.h>
|
2020-10-02 19:39:00 +02:00
|
|
|
#include <string.h>
|
2020-12-20 20:03:55 +01:00
|
|
|
|
2021-06-29 14:39:34 +02:00
|
|
|
static const char *opts = "6c:D:d:fH:hnP:p:Vvx:";
|
2021-06-29 12:49:59 +02:00
|
|
|
|
|
|
|
static struct option longopts[] = {
|
|
|
|
{"help", no_argument, NULL, 'h'},
|
|
|
|
{"version", no_argument, NULL, 'V'},
|
|
|
|
{NULL, 0, NULL, 0},
|
|
|
|
};
|
|
|
|
|
2021-05-09 20:23:36 +02:00
|
|
|
struct fcgi fcgi[FCGI_MAX];
|
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
struct vhosthead hosts;
|
2021-01-15 10:17:43 +01:00
|
|
|
|
2021-03-19 20:21:29 +01:00
|
|
|
int sock4, sock6;
|
2021-02-23 13:05:54 +01:00
|
|
|
|
2021-03-19 20:21:29 +01:00
|
|
|
struct imsgbuf logibuf, exibuf, servibuf[PROC_MAX];
|
2021-02-03 17:28:00 +01:00
|
|
|
|
2021-05-01 14:17:42 +02:00
|
|
|
const char *config_path, *certs_dir, *hostname, *pidfile, *cgi;
|
2021-01-16 20:41:34 +01:00
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
struct conf conf;
|
2020-11-06 16:18:38 +01:00
|
|
|
|
2021-02-04 14:23:15 +01:00
|
|
|
struct tls_config *tlsconf;
|
2021-01-25 11:30:07 +01:00
|
|
|
struct tls *ctx;
|
|
|
|
|
2021-03-20 11:43:23 +01:00
|
|
|
static void
|
|
|
|
dummy_handler(int signo)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:56:58 +02:00
|
|
|
/* wrapper around dirname(3). dn must be PATH_MAX+1 at least. */
|
|
|
|
static void
|
|
|
|
pdirname(const char *path, char *dn)
|
2021-01-25 15:08:31 +01:00
|
|
|
{
|
2021-03-31 15:56:58 +02:00
|
|
|
char p[PATH_MAX+1];
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
strlcpy(p, path, sizeof(p));
|
|
|
|
t = dirname(p);
|
|
|
|
memmove(dn, t, strlen(t)+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mkdirs(const char *path, mode_t mode)
|
|
|
|
{
|
|
|
|
char dname[PATH_MAX+1];
|
|
|
|
|
|
|
|
pdirname(path, dname);
|
|
|
|
if (!strcmp(dname, "/"))
|
|
|
|
return;
|
|
|
|
mkdirs(dname, mode);
|
|
|
|
if (mkdir(path, mode) != 0 && errno != EEXIST)
|
2021-01-25 15:08:31 +01:00
|
|
|
fatal("can't mkdir %s: %s", path, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* $XDG_DATA_HOME/gmid */
|
|
|
|
char *
|
|
|
|
data_dir(void)
|
|
|
|
{
|
|
|
|
const char *home, *xdg;
|
|
|
|
char *t;
|
|
|
|
|
|
|
|
if ((xdg = getenv("XDG_DATA_HOME")) == NULL) {
|
|
|
|
if ((home = getenv("HOME")) == NULL)
|
|
|
|
errx(1, "XDG_DATA_HOME and HOME both empty");
|
|
|
|
if (asprintf(&t, "%s/.local/share/gmid", home) == -1)
|
|
|
|
err(1, "asprintf");
|
2021-03-31 15:56:58 +02:00
|
|
|
} else {
|
|
|
|
if (asprintf(&t, "%s/gmid", xdg) == -1)
|
|
|
|
err(1, "asprintf");
|
2021-01-25 15:08:31 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 15:56:58 +02:00
|
|
|
mkdirs(t, 0755);
|
2021-01-25 15:08:31 +01:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
load_local_cert(const char *hostname, const char *dir)
|
|
|
|
{
|
|
|
|
char *cert, *key;
|
2021-03-31 18:32:18 +02:00
|
|
|
struct vhost *h;
|
2021-01-25 15:08:31 +01:00
|
|
|
|
|
|
|
if (asprintf(&cert, "%s/%s.cert.pem", dir, hostname) == -1)
|
|
|
|
errx(1, "asprintf");
|
|
|
|
if (asprintf(&key, "%s/%s.key.pem", dir, hostname) == -1)
|
|
|
|
errx(1, "asprintf");
|
|
|
|
|
|
|
|
if (access(cert, R_OK) == -1 || access(key, R_OK) == -1)
|
|
|
|
gen_certificate(hostname, cert, key);
|
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
h = TAILQ_FIRST(&hosts);
|
|
|
|
h->cert = cert;
|
|
|
|
h->key = key;
|
|
|
|
h->domain = hostname;
|
2021-01-25 15:08:31 +01:00
|
|
|
}
|
|
|
|
|
2020-10-02 19:39:00 +02:00
|
|
|
void
|
2021-01-25 11:30:07 +01:00
|
|
|
load_vhosts(void)
|
2021-01-13 19:40:18 +01:00
|
|
|
{
|
2021-04-30 19:16:34 +02:00
|
|
|
struct vhost *h;
|
|
|
|
struct location *l;
|
2021-01-15 10:17:43 +01:00
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
TAILQ_FOREACH(h, &hosts, vhosts) {
|
2021-04-30 19:16:34 +02:00
|
|
|
TAILQ_FOREACH(l, &h->locations, locations) {
|
|
|
|
if (l->dir == NULL)
|
|
|
|
continue;
|
|
|
|
if ((l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY)) == -1)
|
2021-10-07 19:19:45 +02:00
|
|
|
fatal("open %s for domain %s: %s", l->dir, h->domain,
|
|
|
|
strerror(errno));
|
2021-04-30 19:16:34 +02:00
|
|
|
}
|
2020-10-07 13:50:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 19:39:00 +02:00
|
|
|
int
|
2020-10-15 13:45:57 +02:00
|
|
|
make_socket(int port, int family)
|
2020-10-02 19:39:00 +02:00
|
|
|
{
|
|
|
|
int sock, v;
|
2020-10-15 13:45:57 +02:00
|
|
|
struct sockaddr_in addr4;
|
|
|
|
struct sockaddr_in6 addr6;
|
|
|
|
struct sockaddr *addr;
|
|
|
|
socklen_t len;
|
|
|
|
|
2021-09-24 10:10:07 +02:00
|
|
|
switch (family) {
|
2020-10-15 13:45:57 +02:00
|
|
|
case AF_INET:
|
2021-09-24 10:08:49 +02:00
|
|
|
memset(&addr4, 0, sizeof(addr4));
|
2020-10-15 13:45:57 +02:00
|
|
|
addr4.sin_family = family;
|
|
|
|
addr4.sin_port = htons(port);
|
|
|
|
addr4.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr = (struct sockaddr*)&addr4;
|
|
|
|
len = sizeof(addr4);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AF_INET6:
|
2021-09-24 10:08:49 +02:00
|
|
|
memset(&addr6, 0, sizeof(addr6));
|
2020-10-15 13:45:57 +02:00
|
|
|
addr6.sin6_family = AF_INET6;
|
|
|
|
addr6.sin6_port = htons(port);
|
|
|
|
addr6.sin6_addr = in6addr_any;
|
|
|
|
addr = (struct sockaddr*)&addr6;
|
|
|
|
len = sizeof(addr6);
|
|
|
|
break;
|
2020-10-02 19:39:00 +02:00
|
|
|
|
2020-10-15 13:45:57 +02:00
|
|
|
default:
|
|
|
|
/* unreachable */
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
|
2021-01-10 16:50:27 +01:00
|
|
|
fatal("socket: %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
|
|
|
v = 1;
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
|
2021-01-10 16:50:27 +01:00
|
|
|
fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
|
|
|
v = 1;
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
|
2021-01-10 16:50:27 +01:00
|
|
|
fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
2020-10-07 13:50:18 +02:00
|
|
|
mark_nonblock(sock);
|
|
|
|
|
2020-10-15 13:45:57 +02:00
|
|
|
if (bind(sock, addr, len) == -1)
|
2021-01-10 16:50:27 +01:00
|
|
|
fatal("bind: %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
|
|
|
if (listen(sock, 16) == -1)
|
2021-01-10 16:50:27 +01:00
|
|
|
fatal("listen: %s", strerror(errno));
|
2020-10-02 19:39:00 +02:00
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2021-10-15 08:30:42 +02:00
|
|
|
static void
|
|
|
|
add_keypair(struct vhost *h)
|
|
|
|
{
|
|
|
|
if (h->ocsp == NULL) {
|
|
|
|
if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1)
|
|
|
|
fatal("failed to load the keypair (%s, %s)",
|
|
|
|
h->cert, h->key);
|
|
|
|
} else {
|
2021-10-15 10:03:11 +02:00
|
|
|
if (tls_config_add_keypair_ocsp_file(tlsconf, h->cert, h->key,
|
|
|
|
h->ocsp) == -1)
|
2021-10-15 08:30:42 +02:00
|
|
|
fatal("failed to load the keypair (%s, %s, %s)",
|
|
|
|
h->cert, h->key, h->ocsp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 11:30:07 +01:00
|
|
|
void
|
|
|
|
setup_tls(void)
|
2021-01-16 20:41:34 +01:00
|
|
|
{
|
2021-01-25 11:30:07 +01:00
|
|
|
struct vhost *h;
|
2021-01-18 19:18:57 +01:00
|
|
|
|
2021-01-16 20:41:34 +01:00
|
|
|
if ((tlsconf = tls_config_new()) == NULL)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("tls_config_new");
|
2021-01-16 20:41:34 +01:00
|
|
|
|
|
|
|
/* optionally accept client certs, but don't try to verify them */
|
|
|
|
tls_config_verify_client_optional(tlsconf);
|
|
|
|
tls_config_insecure_noverifycert(tlsconf);
|
|
|
|
|
|
|
|
if (tls_config_set_protocols(tlsconf, conf.protos) == -1)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("tls_config_set_protocols");
|
2021-01-16 20:41:34 +01:00
|
|
|
|
|
|
|
if ((ctx = tls_server()) == NULL)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("tls_server failure");
|
2021-01-16 20:41:34 +01:00
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
h = TAILQ_FIRST(&hosts);
|
|
|
|
|
2021-01-25 11:30:07 +01:00
|
|
|
/* we need to set something, then we can add how many key we want */
|
2021-03-31 18:32:18 +02:00
|
|
|
if (tls_config_set_keypair_file(tlsconf, h->cert, h->key))
|
2021-02-04 14:23:15 +01:00
|
|
|
fatal("tls_config_set_keypair_file failed for (%s, %s)",
|
2021-03-31 18:32:18 +02:00
|
|
|
h->cert, h->key);
|
2021-10-15 10:03:11 +02:00
|
|
|
|
|
|
|
/* same for OCSP */
|
2021-10-15 08:30:42 +02:00
|
|
|
if (h->ocsp != NULL &&
|
|
|
|
tls_config_set_ocsp_staple_file(tlsconf, h->ocsp) == -1)
|
|
|
|
fatal("tls_config_set_ocsp_staple_file failed for (%s)",
|
|
|
|
h->ocsp);
|
2021-01-25 11:30:07 +01:00
|
|
|
|
2021-10-15 08:30:42 +02:00
|
|
|
while ((h = TAILQ_NEXT(h, vhosts)) != NULL)
|
|
|
|
add_keypair(h);
|
2021-01-16 20:41:34 +01:00
|
|
|
|
|
|
|
if (tls_configure(ctx, tlsconf) == -1)
|
2021-01-18 19:15:58 +01:00
|
|
|
fatal("tls_configure: %s", tls_error(ctx));
|
2021-01-25 11:30:07 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 17:28:00 +01:00
|
|
|
static int
|
2021-03-19 20:21:29 +01:00
|
|
|
listener_main(struct imsgbuf *ibuf)
|
2021-01-25 11:30:07 +01:00
|
|
|
{
|
2021-02-07 13:05:32 +01:00
|
|
|
drop_priv();
|
2021-01-25 11:30:07 +01:00
|
|
|
load_default_mime(&conf.mime);
|
|
|
|
load_vhosts();
|
2021-03-19 20:21:29 +01:00
|
|
|
loop(ctx, sock4, sock6, ibuf);
|
2021-01-16 20:41:34 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-24 11:39:48 +01:00
|
|
|
void
|
|
|
|
init_config(void)
|
2020-10-02 19:39:00 +02:00
|
|
|
{
|
2021-03-31 18:32:18 +02:00
|
|
|
TAILQ_INIT(&hosts);
|
2020-12-21 15:42:06 +01:00
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
conf.port = 1965;
|
|
|
|
conf.ipv6 = 0;
|
2021-01-15 19:55:05 +01:00
|
|
|
conf.protos = TLS_PROTOCOL_TLSv1_2 | TLS_PROTOCOL_TLSv1_3;
|
2020-11-06 10:58:30 +01:00
|
|
|
|
2021-01-21 16:01:37 +01:00
|
|
|
init_mime(&conf.mime);
|
2021-01-25 11:30:07 +01:00
|
|
|
|
|
|
|
conf.chroot = NULL;
|
|
|
|
conf.user = NULL;
|
2021-02-07 13:05:32 +01:00
|
|
|
|
|
|
|
conf.prefork = 3;
|
2021-01-25 11:30:07 +01:00
|
|
|
}
|
|
|
|
|
2021-02-04 14:23:15 +01:00
|
|
|
void
|
|
|
|
free_config(void)
|
|
|
|
{
|
2021-03-31 18:32:18 +02:00
|
|
|
struct vhost *h, *th;
|
|
|
|
struct location *l, *tl;
|
2021-04-28 14:43:17 +02:00
|
|
|
struct envlist *e, *te;
|
2021-04-29 20:23:35 +02:00
|
|
|
struct alist *a, *ta;
|
2021-05-09 20:23:36 +02:00
|
|
|
int v, i;
|
2021-04-28 14:42:36 +02:00
|
|
|
|
|
|
|
v = conf.verbose;
|
2021-02-04 14:23:15 +01:00
|
|
|
|
|
|
|
free(conf.chroot);
|
|
|
|
free(conf.user);
|
|
|
|
memset(&conf, 0, sizeof(conf));
|
|
|
|
|
2021-04-28 14:42:36 +02:00
|
|
|
conf.verbose = v;
|
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
TAILQ_FOREACH_SAFE(h, &hosts, vhosts, th) {
|
|
|
|
TAILQ_FOREACH_SAFE(l, &h->locations, locations, tl) {
|
|
|
|
TAILQ_REMOVE(&h->locations, l, locations);
|
2021-02-04 14:23:15 +01:00
|
|
|
|
|
|
|
free((char*)l->match);
|
|
|
|
free((char*)l->lang);
|
|
|
|
free((char*)l->default_mime);
|
|
|
|
free((char*)l->index);
|
2021-02-06 18:22:37 +01:00
|
|
|
free((char*)l->block_fmt);
|
2021-04-30 19:16:34 +02:00
|
|
|
free((char*)l->dir);
|
|
|
|
|
|
|
|
if (l->dirfd != -1)
|
|
|
|
close(l->dirfd);
|
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
free(l);
|
2021-02-04 14:23:15 +01:00
|
|
|
}
|
2021-03-31 18:32:18 +02:00
|
|
|
|
2021-04-28 14:43:17 +02:00
|
|
|
TAILQ_FOREACH_SAFE(e, &h->env, envs, te) {
|
2021-06-12 12:19:17 +02:00
|
|
|
TAILQ_REMOVE(&h->env, e, envs);
|
|
|
|
|
2021-04-28 14:43:17 +02:00
|
|
|
free(e->name);
|
|
|
|
free(e->value);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
2021-06-12 12:20:36 +02:00
|
|
|
TAILQ_FOREACH_SAFE(e, &h->params, envs, te) {
|
|
|
|
TAILQ_REMOVE(&h->params, e, envs);
|
|
|
|
|
|
|
|
free(e->name);
|
|
|
|
free(e->value);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
2021-04-29 20:23:35 +02:00
|
|
|
TAILQ_FOREACH_SAFE(a, &h->aliases, aliases, ta) {
|
2021-06-12 12:19:17 +02:00
|
|
|
TAILQ_REMOVE(&h->aliases, a, aliases);
|
|
|
|
|
2021-04-29 20:23:35 +02:00
|
|
|
free(a->alias);
|
|
|
|
free(a);
|
|
|
|
}
|
|
|
|
|
2021-04-30 19:16:34 +02:00
|
|
|
free((char*)h->domain);
|
|
|
|
free((char*)h->cert);
|
|
|
|
free((char*)h->key);
|
2021-10-24 18:57:31 +02:00
|
|
|
free((char*)h->ocsp);
|
2021-04-30 19:16:34 +02:00
|
|
|
free((char*)h->cgi);
|
|
|
|
free((char*)h->entrypoint);
|
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
TAILQ_REMOVE(&hosts, h, vhosts);
|
|
|
|
free(h);
|
2021-02-04 14:23:15 +01:00
|
|
|
}
|
|
|
|
|
2021-05-09 20:23:36 +02:00
|
|
|
for (i = 0; i < FCGI_MAX; ++i) {
|
|
|
|
if (fcgi[i].path == NULL && fcgi[i].prog == NULL)
|
|
|
|
break;
|
|
|
|
free(fcgi[i].path);
|
|
|
|
free(fcgi[i].port);
|
|
|
|
free(fcgi[i].prog);
|
2021-07-06 12:46:50 +02:00
|
|
|
|
|
|
|
fcgi[i].path = NULL;
|
|
|
|
fcgi[i].port = NULL;
|
|
|
|
fcgi[i].prog = NULL;
|
2021-05-09 20:23:36 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 14:23:15 +01:00
|
|
|
tls_free(ctx);
|
|
|
|
tls_config_free(tlsconf);
|
|
|
|
}
|
|
|
|
|
2021-03-20 09:51:17 +01:00
|
|
|
static int
|
|
|
|
wait_signal(void)
|
2021-02-04 14:23:15 +01:00
|
|
|
{
|
|
|
|
sigset_t mask;
|
|
|
|
int signo;
|
|
|
|
|
|
|
|
sigemptyset(&mask);
|
|
|
|
sigaddset(&mask, SIGHUP);
|
2021-03-20 09:51:17 +01:00
|
|
|
sigaddset(&mask, SIGINT);
|
|
|
|
sigaddset(&mask, SIGTERM);
|
2021-02-04 14:23:15 +01:00
|
|
|
sigwait(&mask, &signo);
|
2021-03-20 09:51:17 +01:00
|
|
|
|
|
|
|
return signo == SIGHUP;
|
2021-02-04 14:23:15 +01:00
|
|
|
}
|
|
|
|
|
2021-01-25 11:30:07 +01:00
|
|
|
void
|
|
|
|
drop_priv(void)
|
|
|
|
{
|
|
|
|
struct passwd *pw = NULL;
|
|
|
|
|
|
|
|
if (conf.chroot != NULL && conf.user == NULL)
|
|
|
|
fatal("can't chroot without an user to switch to after.");
|
|
|
|
|
|
|
|
if (conf.user != NULL) {
|
|
|
|
if ((pw = getpwnam(conf.user)) == NULL)
|
|
|
|
fatal("can't find user %s", conf.user);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conf.chroot != NULL) {
|
|
|
|
if (chroot(conf.chroot) != 0 || chdir("/") != 0)
|
|
|
|
fatal("%s: %s", conf.chroot, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pw != NULL) {
|
|
|
|
if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
|
|
|
|
fatal("setresuid(%d): %s", pw->pw_uid,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getuid() == 0)
|
2021-02-07 16:30:28 +01:00
|
|
|
log_warn(NULL, "not a good idea to run a network daemon as root");
|
2021-01-25 10:45:09 +01:00
|
|
|
}
|
|
|
|
|
2021-02-07 16:30:28 +01:00
|
|
|
static void
|
2021-06-29 12:50:39 +02:00
|
|
|
usage(void)
|
2021-02-07 16:30:28 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2021-06-29 13:04:29 +02:00
|
|
|
"Version: " GMID_STRING "\n"
|
2021-06-29 14:39:34 +02:00
|
|
|
"Usage: %s [-fnv] [-c config] [-D macro=value] [-P pidfile]\n"
|
2021-06-29 13:04:29 +02:00
|
|
|
" %s [-6hVv] [-d certs-dir] [-H hostname] [-p port] [-x cgi] [dir]\n",
|
|
|
|
getprogname(),
|
2021-06-29 12:50:39 +02:00
|
|
|
getprogname());
|
2021-02-07 16:30:28 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 13:05:54 +01:00
|
|
|
static void
|
|
|
|
logger_init(void)
|
|
|
|
{
|
|
|
|
int p[2];
|
|
|
|
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
|
|
|
|
err(1, "socketpair");
|
|
|
|
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
err(1, "fork");
|
|
|
|
case 0:
|
2021-03-19 20:21:29 +01:00
|
|
|
signal(SIGHUP, SIG_IGN);
|
2021-02-23 13:05:54 +01:00
|
|
|
close(p[0]);
|
|
|
|
setproctitle("logger");
|
2021-03-19 20:21:29 +01:00
|
|
|
imsg_init(&logibuf, p[1]);
|
2021-02-23 13:05:54 +01:00
|
|
|
drop_priv();
|
2021-03-19 20:21:29 +01:00
|
|
|
_exit(logger_main(p[1], &logibuf));
|
2021-02-23 13:05:54 +01:00
|
|
|
default:
|
|
|
|
close(p[1]);
|
2021-03-19 20:21:29 +01:00
|
|
|
imsg_init(&logibuf, p[0]);
|
2021-02-23 13:05:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 17:28:00 +01:00
|
|
|
static int
|
2021-03-19 20:21:29 +01:00
|
|
|
serve(int argc, char **argv, struct imsgbuf *ibuf)
|
2021-02-03 17:28:00 +01:00
|
|
|
{
|
2021-03-31 18:32:18 +02:00
|
|
|
char path[PATH_MAX];
|
|
|
|
int i, p[2];
|
|
|
|
struct vhost *h;
|
|
|
|
struct location *l;
|
2021-02-03 17:28:00 +01:00
|
|
|
|
2021-09-24 10:10:07 +02:00
|
|
|
if (config_path == NULL) {
|
2021-02-03 17:28:00 +01:00
|
|
|
if (hostname == NULL)
|
|
|
|
hostname = "localhost";
|
|
|
|
if (certs_dir == NULL)
|
|
|
|
certs_dir = data_dir();
|
|
|
|
load_local_cert(hostname, certs_dir);
|
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
h = TAILQ_FIRST(&hosts);
|
|
|
|
h->domain = "*";
|
|
|
|
|
|
|
|
l = TAILQ_FIRST(&h->locations);
|
|
|
|
l->auto_index = 1;
|
|
|
|
l->match = "*";
|
2021-02-03 17:28:00 +01:00
|
|
|
|
|
|
|
switch (argc) {
|
|
|
|
case 0:
|
2021-04-30 19:16:34 +02:00
|
|
|
l->dir = getcwd(path, sizeof(path));
|
2021-02-03 17:28:00 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2021-04-30 19:16:34 +02:00
|
|
|
l->dir = absolutify_path(argv[0]);
|
2021-02-03 17:28:00 +01:00
|
|
|
break;
|
|
|
|
default:
|
2021-06-29 12:50:39 +02:00
|
|
|
usage();
|
2021-02-03 17:28:00 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-30 19:16:34 +02:00
|
|
|
log_notice(NULL, "serving %s on port %d", l->dir, conf.port);
|
2021-02-03 17:28:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* setup tls before dropping privileges: we don't want user
|
|
|
|
* to put private certs inside the chroot. */
|
|
|
|
setup_tls();
|
|
|
|
|
2021-03-03 18:22:01 +01:00
|
|
|
for (i = 0; i < conf.prefork; ++i) {
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
|
|
|
|
PF_UNSPEC, p) == -1)
|
|
|
|
fatal("socketpair: %s", strerror(errno));
|
2021-02-03 17:28:00 +01:00
|
|
|
|
2021-03-03 18:22:01 +01:00
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
fatal("fork: %s", strerror(errno));
|
|
|
|
case 0: /* child */
|
|
|
|
close(p[0]);
|
2021-03-19 20:21:29 +01:00
|
|
|
imsg_init(&exibuf, p[1]);
|
2021-03-03 18:22:01 +01:00
|
|
|
setproctitle("server");
|
2021-03-19 20:21:29 +01:00
|
|
|
_exit(listener_main(&exibuf));
|
2021-03-03 18:22:01 +01:00
|
|
|
default:
|
|
|
|
close(p[1]);
|
2021-03-19 20:21:29 +01:00
|
|
|
imsg_init(&servibuf[i], p[0]);
|
2021-03-03 18:22:01 +01:00
|
|
|
}
|
2021-02-03 17:28:00 +01:00
|
|
|
}
|
2021-03-03 18:22:01 +01:00
|
|
|
|
|
|
|
setproctitle("executor");
|
|
|
|
drop_priv();
|
2021-03-19 20:21:29 +01:00
|
|
|
_exit(executor_main(ibuf));
|
2021-02-03 17:28:00 +01:00
|
|
|
}
|
|
|
|
|
2021-04-28 14:42:36 +02:00
|
|
|
static int
|
|
|
|
write_pidfile(const char *pidfile)
|
|
|
|
{
|
|
|
|
struct flock lock;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (pidfile == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1)
|
|
|
|
fatal("can't open pidfile %s: %s", pidfile, strerror(errno));
|
|
|
|
|
|
|
|
lock.l_start = 0;
|
|
|
|
lock.l_len = 0;
|
|
|
|
lock.l_type = F_WRLCK;
|
|
|
|
lock.l_whence = SEEK_SET;
|
|
|
|
|
|
|
|
if (fcntl(fd, F_SETLK, &lock) == -1)
|
|
|
|
fatal("can't lock %s, gmid is already running?", pidfile);
|
|
|
|
|
|
|
|
if (ftruncate(fd, 0) == -1)
|
|
|
|
fatal("ftruncate: %s: %s", pidfile, strerror(errno));
|
|
|
|
|
|
|
|
dprintf(fd, "%d\n", getpid());
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
static void
|
|
|
|
setup_configless(int argc, char **argv, const char *cgi)
|
|
|
|
{
|
|
|
|
struct vhost *host;
|
|
|
|
struct location *loc;
|
|
|
|
|
|
|
|
host = xcalloc(1, sizeof(*host));
|
|
|
|
host->cgi = cgi;
|
|
|
|
TAILQ_INSERT_HEAD(&hosts, host, vhosts);
|
|
|
|
|
|
|
|
loc = xcalloc(1, sizeof(*loc));
|
2021-07-19 13:15:44 +02:00
|
|
|
loc->fcgi = -1;
|
2021-03-31 18:32:18 +02:00
|
|
|
TAILQ_INSERT_HEAD(&host->locations, loc, locations);
|
|
|
|
|
|
|
|
serve(argc, argv, NULL);
|
2021-06-15 10:06:10 +02:00
|
|
|
|
2021-03-31 18:32:18 +02:00
|
|
|
imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
|
|
|
|
imsg_flush(&logibuf);
|
|
|
|
}
|
|
|
|
|
2021-07-09 15:06:58 +02:00
|
|
|
static int
|
|
|
|
parse_portno(const char *p)
|
|
|
|
{
|
|
|
|
const char *errstr;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
n = strtonum(p, 0, UINT16_MAX, &errstr);
|
|
|
|
if (errstr != NULL)
|
|
|
|
yyerror("port number is %s: %s", errstr, p);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2021-01-24 11:39:48 +01:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2021-03-20 10:23:53 +01:00
|
|
|
struct imsgbuf exibuf;
|
2021-03-03 18:22:01 +01:00
|
|
|
int ch, conftest = 0, configless = 0;
|
2021-04-28 14:45:22 +02:00
|
|
|
int pidfd, old_ipv6, old_port;
|
2021-01-18 19:18:57 +01:00
|
|
|
|
2021-07-07 11:18:24 +02:00
|
|
|
logger_init();
|
2021-01-24 13:45:22 +01:00
|
|
|
init_config();
|
|
|
|
|
2021-06-29 12:49:59 +02:00
|
|
|
while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
|
2020-10-02 19:39:00 +02:00
|
|
|
switch (ch) {
|
2021-01-11 13:08:50 +01:00
|
|
|
case '6':
|
2021-01-15 10:17:43 +01:00
|
|
|
conf.ipv6 = 1;
|
2021-01-25 15:08:31 +01:00
|
|
|
configless = 1;
|
2021-01-11 13:08:50 +01:00
|
|
|
break;
|
|
|
|
|
2020-10-02 19:39:00 +02:00
|
|
|
case 'c':
|
2021-02-04 14:23:15 +01:00
|
|
|
config_path = absolutify_path(optarg);
|
2020-10-02 19:39:00 +02:00
|
|
|
break;
|
|
|
|
|
2021-06-29 14:39:34 +02:00
|
|
|
case 'D':
|
|
|
|
if (cmdline_symset(optarg) == -1)
|
2021-07-07 11:18:24 +02:00
|
|
|
fatal("could not parse macro definition: %s",
|
|
|
|
optarg);
|
2021-06-29 14:39:34 +02:00
|
|
|
break;
|
|
|
|
|
2020-10-02 19:39:00 +02:00
|
|
|
case 'd':
|
2021-01-25 15:08:31 +01:00
|
|
|
certs_dir = optarg;
|
|
|
|
configless = 1;
|
2020-10-02 19:39:00 +02:00
|
|
|
break;
|
|
|
|
|
2021-01-27 13:04:37 +01:00
|
|
|
case 'f':
|
2021-02-07 16:30:28 +01:00
|
|
|
conf.foreground = 1;
|
2021-01-27 13:04:37 +01:00
|
|
|
break;
|
|
|
|
|
2021-01-25 15:08:31 +01:00
|
|
|
case 'H':
|
|
|
|
hostname = optarg;
|
|
|
|
configless = 1;
|
2020-12-02 21:18:01 +01:00
|
|
|
break;
|
|
|
|
|
2020-10-02 19:39:00 +02:00
|
|
|
case 'h':
|
2021-06-29 12:50:39 +02:00
|
|
|
usage();
|
2020-10-02 19:39:00 +02:00
|
|
|
return 0;
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
case 'n':
|
2021-10-09 23:40:55 +02:00
|
|
|
conftest++;
|
2020-11-18 09:12:27 +01:00
|
|
|
break;
|
2021-01-15 10:17:43 +01:00
|
|
|
|
2021-04-28 14:45:22 +02:00
|
|
|
case 'P':
|
|
|
|
pidfile = optarg;
|
|
|
|
break;
|
|
|
|
|
2021-01-15 10:17:43 +01:00
|
|
|
case 'p':
|
|
|
|
conf.port = parse_portno(optarg);
|
2021-01-25 15:08:31 +01:00
|
|
|
configless = 1;
|
2021-01-17 10:34:06 +01:00
|
|
|
break;
|
2020-11-18 09:12:27 +01:00
|
|
|
|
2021-06-29 12:49:59 +02:00
|
|
|
case 'V':
|
2021-06-29 12:56:07 +02:00
|
|
|
puts("Version: " GMID_STRING);
|
2021-06-29 12:49:59 +02:00
|
|
|
return 0;
|
|
|
|
|
2021-01-28 00:14:16 +01:00
|
|
|
case 'v':
|
2021-02-07 16:30:28 +01:00
|
|
|
conf.verbose++;
|
2021-01-28 00:14:16 +01:00
|
|
|
break;
|
|
|
|
|
2020-11-06 13:01:31 +01:00
|
|
|
case 'x':
|
2021-01-15 10:17:43 +01:00
|
|
|
/* drop the starting / (if any) */
|
|
|
|
if (*optarg == '/')
|
|
|
|
optarg++;
|
2021-03-31 18:32:18 +02:00
|
|
|
cgi = optarg;
|
2021-01-25 15:08:31 +01:00
|
|
|
configless = 1;
|
2020-11-06 13:01:31 +01:00
|
|
|
break;
|
|
|
|
|
2020-10-02 19:39:00 +02:00
|
|
|
default:
|
2021-06-29 12:50:39 +02:00
|
|
|
usage();
|
2020-10-02 19:39:00 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2021-01-25 15:08:31 +01:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
2020-10-02 19:39:00 +02:00
|
|
|
|
2021-02-03 17:28:00 +01:00
|
|
|
if (config_path == NULL) {
|
2021-01-27 16:00:55 +01:00
|
|
|
configless = 1;
|
2021-02-07 16:30:28 +01:00
|
|
|
conf.foreground = 1;
|
2021-03-03 18:22:01 +01:00
|
|
|
conf.prefork = 1;
|
2021-02-07 16:30:28 +01:00
|
|
|
conf.verbose++;
|
2021-01-15 10:17:43 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 17:28:00 +01:00
|
|
|
if (config_path != NULL && (argc > 0 || configless))
|
2021-07-07 11:18:24 +02:00
|
|
|
fatal("can't specify options in config mode.");
|
2021-02-03 17:28:00 +01:00
|
|
|
|
2021-01-18 19:15:58 +01:00
|
|
|
if (conftest) {
|
2021-08-24 00:43:56 +02:00
|
|
|
if (config_path == NULL)
|
|
|
|
fatal("missing configuration");
|
2021-02-03 17:28:00 +01:00
|
|
|
parse_conf(config_path);
|
2021-10-09 23:40:55 +02:00
|
|
|
fprintf(stderr, "config OK\n");
|
|
|
|
if (conftest > 1)
|
|
|
|
print_conf();
|
2021-01-18 19:15:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2021-01-15 10:17:43 +01:00
|
|
|
|
2021-02-07 16:30:28 +01:00
|
|
|
if (!conf.foreground && !configless) {
|
2021-07-07 11:18:24 +02:00
|
|
|
/* log to syslog */
|
|
|
|
imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0);
|
|
|
|
imsg_flush(&logibuf);
|
|
|
|
|
2021-01-17 12:56:50 +01:00
|
|
|
if (daemon(1, 1) == -1)
|
2021-07-07 11:18:24 +02:00
|
|
|
fatal("daemon: %s", strerror(errno));
|
2021-01-17 12:56:50 +01:00
|
|
|
}
|
2020-12-28 09:55:56 +01:00
|
|
|
|
2021-02-03 17:28:00 +01:00
|
|
|
if (config_path != NULL)
|
|
|
|
parse_conf(config_path);
|
2020-10-02 19:39:00 +02:00
|
|
|
|
2021-02-03 17:28:00 +01:00
|
|
|
sock4 = make_socket(conf.port, AF_INET);
|
|
|
|
sock6 = -1;
|
|
|
|
if (conf.ipv6)
|
|
|
|
sock6 = make_socket(conf.port, AF_INET6);
|
2021-01-11 16:28:25 +01:00
|
|
|
|
2021-04-20 11:53:03 +02:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
|
2021-03-19 20:21:29 +01:00
|
|
|
if (configless) {
|
2021-03-31 18:32:18 +02:00
|
|
|
setup_configless(argc, argv, cgi);
|
2021-03-19 20:21:29 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2021-02-04 14:23:15 +01:00
|
|
|
|
2021-04-28 14:45:22 +02:00
|
|
|
pidfd = write_pidfile(pidfile);
|
|
|
|
|
2021-07-07 11:46:37 +02:00
|
|
|
/*
|
|
|
|
* Linux seems to call the event handlers even when we're
|
2021-04-26 22:58:06 +02:00
|
|
|
* doing a sigwait. These dummy handlers are here to avoid
|
2021-07-07 11:46:37 +02:00
|
|
|
* being terminated on SIGHUP, SIGINT or SIGTERM.
|
|
|
|
*/
|
2021-03-20 11:43:23 +01:00
|
|
|
signal(SIGHUP, dummy_handler);
|
|
|
|
signal(SIGINT, dummy_handler);
|
|
|
|
signal(SIGTERM, dummy_handler);
|
|
|
|
|
2021-02-04 14:23:15 +01:00
|
|
|
/* wait a sighup and reload the daemon */
|
|
|
|
for (;;) {
|
2021-03-19 20:21:29 +01:00
|
|
|
int p[2];
|
|
|
|
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
|
|
|
|
PF_UNSPEC, p) == -1)
|
|
|
|
fatal("socketpair: %s", strerror(errno));
|
2021-02-04 14:23:15 +01:00
|
|
|
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
fatal("fork: %s", strerror(errno));
|
|
|
|
case 0:
|
2021-03-19 20:21:29 +01:00
|
|
|
close(p[0]);
|
|
|
|
imsg_init(&exibuf, p[1]);
|
|
|
|
_exit(serve(argc, argv, &exibuf));
|
2021-02-04 14:23:15 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 20:21:29 +01:00
|
|
|
close(p[1]);
|
|
|
|
imsg_init(&exibuf, p[0]);
|
|
|
|
|
2021-03-20 09:51:17 +01:00
|
|
|
if (!wait_signal())
|
|
|
|
break;
|
|
|
|
|
2021-02-07 16:30:28 +01:00
|
|
|
log_info(NULL, "reloading configuration %s", config_path);
|
2021-02-04 14:23:15 +01:00
|
|
|
|
2021-03-19 20:21:29 +01:00
|
|
|
/* close the executor (it'll close the servers too) */
|
|
|
|
imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
|
|
|
|
imsg_flush(&exibuf);
|
|
|
|
close(p[0]);
|
|
|
|
|
2021-02-04 14:23:15 +01:00
|
|
|
old_ipv6 = conf.ipv6;
|
|
|
|
old_port = conf.port;
|
|
|
|
|
|
|
|
free_config();
|
|
|
|
init_config();
|
|
|
|
parse_conf(config_path);
|
|
|
|
|
|
|
|
if (old_port != conf.port) {
|
|
|
|
close(sock4);
|
|
|
|
close(sock6);
|
|
|
|
sock4 = -1;
|
|
|
|
sock6 = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock6 != -1 && old_ipv6 != conf.ipv6) {
|
|
|
|
close(sock6);
|
|
|
|
sock6 = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sock4 == -1)
|
|
|
|
sock4 = make_socket(conf.port, AF_INET);
|
|
|
|
if (sock6 == -1 && conf.ipv6)
|
|
|
|
sock6 = make_socket(conf.port, AF_INET6);
|
|
|
|
}
|
2021-03-20 09:51:17 +01:00
|
|
|
|
|
|
|
imsg_compose(&exibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
|
|
|
|
imsg_flush(&exibuf);
|
|
|
|
|
|
|
|
imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
|
|
|
|
imsg_flush(&logibuf);
|
|
|
|
|
2021-04-28 14:45:22 +02:00
|
|
|
if (pidfd != -1)
|
|
|
|
close(pidfd);
|
|
|
|
|
2021-03-20 09:51:17 +01:00
|
|
|
return 0;
|
2020-10-02 19:39:00 +02:00
|
|
|
}
|