gmid/gmid.c

396 lines
7.7 KiB
C
Raw Normal View History

2020-10-02 19:39:00 +02:00
/*
2022-07-04 11:48:39 +02:00
* Copyright (c) 2020, 2021, 2022 Omar Polo <op@omarpolo.com>
2020-10-02 19:39:00 +02:00
*
* 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.
*/
#include "gmid.h"
#include <sys/stat.h>
#include <errno.h>
2020-10-02 19:39:00 +02:00
#include <fcntl.h>
#include <getopt.h>
2023-06-06 10:11:30 +02:00
#include <locale.h>
#include <libgen.h>
#include <limits.h>
2022-12-24 09:41:09 +01:00
#include <grp.h>
2021-01-25 11:30:07 +01:00
#include <pwd.h>
#include <signal.h>
2020-10-02 19:39:00 +02:00
#include <string.h>
2023-06-06 13:46:40 +02:00
#include <syslog.h>
2020-12-20 20:03:55 +01:00
2023-06-06 10:50:54 +02:00
#include "logger.h"
2023-06-06 13:46:40 +02:00
#include "log.h"
static const char *opts = "c:D:fhnP:Vv";
static const struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0},
};
struct fcgi fcgi[FCGI_MAX];
struct vhosthead hosts;
int sock4, sock6;
2023-06-06 10:34:31 +02:00
struct imsgbuf logibuf, servibuf[PREFORK_MAX];
2021-02-03 17:28:00 +01:00
const char *config_path = "/etc/gmid.conf";
const char *pidfile;
struct conf conf;
static void
dummy_handler(int signo)
{
return;
}
2020-10-02 19:39:00 +02:00
int
make_socket(int port, int family)
2020-10-02 19:39:00 +02:00
{
int sock, v;
struct sockaddr_in addr4;
struct sockaddr_in6 addr6;
struct sockaddr *addr;
socklen_t len;
2021-09-24 10:10:07 +02:00
switch (family) {
case AF_INET:
memset(&addr4, 0, sizeof(addr4));
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:
memset(&addr6, 0, sizeof(addr6));
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
default:
/* unreachable */
abort();
}
if ((sock = socket(family, SOCK_STREAM, 0)) == -1)
fatal("socket");
2020-10-02 19:39:00 +02:00
v = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1)
fatal("setsockopt(SO_REUSEADDR)");
2020-10-02 19:39:00 +02:00
v = 1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1)
fatal("setsockopt(SO_REUSEPORT)");
2020-10-02 19:39:00 +02:00
mark_nonblock(sock);
if (bind(sock, addr, len) == -1)
fatal("bind");
2020-10-02 19:39:00 +02:00
if (listen(sock, 16) == -1)
fatal("listen");
2020-10-02 19:39:00 +02:00
return sock;
}
static int
wait_signal(void)
2021-02-04 14:23:15 +01:00
{
sigset_t mask;
int signo;
sigemptyset(&mask);
sigaddset(&mask, SIGHUP);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
2021-02-04 14:23:15 +01:00
sigwait(&mask, &signo);
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 != '\0' && *conf.user == '\0')
fatalx("can't chroot without an user to switch to after.");
2021-01-25 11:30:07 +01:00
if (*conf.user != '\0') {
2021-01-25 11:30:07 +01:00
if ((pw = getpwnam(conf.user)) == NULL)
fatalx("can't find user %s", conf.user);
2021-01-25 11:30:07 +01:00
}
if (*conf.chroot != '\0') {
2021-01-25 11:30:07 +01:00
if (chroot(conf.chroot) != 0 || chdir("/") != 0)
fatal("%s", conf.chroot);
2021-01-25 11:30:07 +01:00
}
if (pw != NULL) {
if (setgroups(1, &pw->pw_gid) == -1 ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) == -1 ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) == -1)
fatal("cannot drop privileges");
2021-01-25 11:30:07 +01:00
}
if (getuid() == 0)
2023-06-06 13:46:40 +02:00
log_warnx("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,
"Version: " GMID_STRING "\n"
"Usage: %s [-fnv] [-c config] [-D macro=value] [-P pidfile]\n",
2021-06-29 12:50:39 +02:00
getprogname());
2021-02-07 16:30:28 +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:
signal(SIGHUP, SIG_IGN);
close(p[0]);
setproctitle("logger");
imsg_init(&logibuf, p[1]);
drop_priv();
_exit(logger_main(p[1], &logibuf));
default:
close(p[1]);
imsg_init(&logibuf, p[0]);
return;
}
}
static void
serve(void)
2021-02-03 17:28:00 +01:00
{
int i, p[2];
2021-02-03 17:28:00 +01:00
for (i = 0; i < conf.prefork; ++i) {
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC,
PF_UNSPEC, p) == -1)
fatal("socketpair");
2021-02-03 17:28:00 +01:00
switch (fork()) {
case -1:
fatal("fork");
case 0: /* child */
close(p[0]);
imsg_init(&servibuf[i], p[1]);
setproctitle("server");
_exit(server_main(&servibuf[i], sock4, sock6));
default:
close(p[1]);
imsg_init(&servibuf[i], p[0]);
}
2021-02-03 17:28:00 +01: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", pidfile);
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)
fatalx("can't lock %s, gmid is already running?", pidfile);
if (ftruncate(fd, 0) == -1)
fatal("ftruncate %s", pidfile);
dprintf(fd, "%d\n", getpid());
return fd;
}
int
main(int argc, char **argv)
{
int i, ch, conftest = 0;
2021-04-28 14:45:22 +02:00
int pidfd, old_ipv6, old_port;
2023-06-06 10:11:30 +02:00
setlocale(LC_CTYPE, "");
2023-06-06 13:46:40 +02:00
/* log to stderr until daemonized */
log_init(1, LOG_DAEMON);
logger_init();
config_init();
2021-01-24 13:45:22 +01:00
while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
2020-10-02 19:39:00 +02:00
switch (ch) {
case 'c':
config_path = absolutify_path(optarg);
break;
case 'D':
if (cmdline_symset(optarg) == -1)
fatalx("could not parse macro definition: %s",
optarg);
break;
case 'f':
conf.foreground = 1;
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;
case 'n':
conftest++;
2020-11-18 09:12:27 +01:00
break;
2021-04-28 14:45:22 +02:00
case 'P':
pidfile = optarg;
break;
case 'V':
puts("Version: " GMID_STRING);
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-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;
}
}
argc -= optind;
argv += optind;
2020-10-02 19:39:00 +02:00
if (argc != 0)
usage();
parse_conf(config_path);
2021-02-03 17:28:00 +01:00
if (conftest) {
fprintf(stderr, "config OK\n");
if (conftest > 1)
print_conf();
return 0;
}
if (!conf.foreground) {
/* log to syslog */
imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0);
imsg_flush(&logibuf);
2023-06-06 13:46:40 +02:00
log_init(0, LOG_DAEMON);
if (daemon(1, 1) == -1)
fatal("daemon");
}
2023-06-06 13:46:40 +02:00
log_setverbose(conf.verbose);
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);
signal(SIGPIPE, SIG_IGN);
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.
*/
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 (;;) {
serve();
2021-02-04 14:23:15 +01:00
if (!wait_signal())
break;
2023-06-06 13:46:40 +02:00
log_info("reloading configuration %s", config_path);
2021-02-04 14:23:15 +01:00
/* close the servers */
for (i = 0; i < conf.prefork; ++i) {
2023-06-05 17:07:24 +02:00
imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1,
NULL, 0);
imsg_flush(&servibuf[i]);
close(servibuf[i].fd);
}
2021-02-04 14:23:15 +01:00
old_ipv6 = conf.ipv6;
old_port = conf.port;
config_free();
config_init();
2021-02-04 14:23:15 +01:00
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);
}
for (i = 0; i < conf.prefork; ++i) {
imsg_compose(&servibuf[i], IMSG_QUIT, 0, 0, -1, NULL, 0);
imsg_flush(&servibuf[i]);
close(servibuf[i].fd);
}
imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
imsg_flush(&logibuf);
close(logibuf.fd);
2021-04-28 14:45:22 +02:00
if (pidfd != -1)
close(pidfd);
return 0;
2020-10-02 19:39:00 +02:00
}