blob: 5772f2fc4c4b3d0230104e76bf7bba40678b95c0 [file] [log] [blame]
Václav Kubernátd386aba2021-01-19 10:03:28 +01001#include <boost/process.hpp>
2#include <docopt.h>
3#include <spdlog/sinks/ansicolor_sink.h>
4#include <spdlog/spdlog.h>
Václav Kubernát7efd6d52021-11-09 01:31:11 +01005#include <sysrepo-cpp/Connection.hpp>
Václav Kubernátd386aba2021-01-19 10:03:28 +01006#include <unistd.h>
7#include "VELIA_VERSION.h"
8#include "firewall/Firewall.h"
Václav Kubernátde0e4e62021-02-08 17:46:14 +01009#include "system_vars.h"
Václav Kubernátd386aba2021-01-19 10:03:28 +010010#include "utils/exceptions.h"
Václav Kubernát6d9357e2021-01-28 15:38:24 +010011#include "utils/exec.h"
Václav Kubernátd386aba2021-01-19 10:03:28 +010012#include "utils/journal.h"
13#include "utils/log-init.h"
Václav Kubernát6018f082021-02-11 01:32:18 +010014#include "utils/log.h"
Tomáš Pecka41784702021-05-26 13:57:32 +020015#include "utils/sysrepo.h"
Václav Kubernátd386aba2021-01-19 10:03:28 +010016#include "utils/waitUntilSignalled.h"
17
Václav Kubernátd386aba2021-01-19 10:03:28 +010018static const char usage[] =
19 R"(Bridge between sysrepo and nftables.
20
21Usage:
22 veliad-firewall
23 [--sysrepo-log-level=<Level>]
24 [--firewall-log-level=<Level>]
25 veliad-firewall (-h | --help)
26 veliad-firewall --version
27
28Options:
29 -h --help Show this screen.
30 --version Show version.
31 --firewall-log-level=<N> Log level for the firewall [default: 3]
Václav Kubernát5f57dec2021-02-11 02:02:46 +010032 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 2]
33 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
34 4 -> debug, 5 -> trace)
Václav Kubernátd386aba2021-01-19 10:03:28 +010035)";
36
37int main(int argc, char* argv[])
38{
39 std::shared_ptr<spdlog::sinks::sink> loggingSink;
40 if (velia::utils::isJournaldActive()) {
41 loggingSink = velia::utils::create_journald_sink();
42 } else {
43 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
44 }
45
Tomáš Peckac50ec9e2021-01-27 12:22:42 +010046 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-firewall " VELIA_VERSION, true);
Václav Kubernátd386aba2021-01-19 10:03:28 +010047
48 velia::utils::initLogs(loggingSink);
Tomáš Pecka41784702021-05-26 13:57:32 +020049 velia::utils::initLogsSysrepo();
Václav Kubernátd386aba2021-01-19 10:03:28 +010050 spdlog::set_level(spdlog::level::info);
51
52 try {
53 spdlog::get("firewall")->set_level(parseLogLevel("Firewall logging", args["--firewall-log-level"]));
54 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
55
Václav Kubernát7efd6d52021-11-09 01:31:11 +010056 auto srConn = sysrepo::Connection{};
57 auto srSess = srConn.sessionStart();
Václav Kubernátd386aba2021-01-19 10:03:28 +010058 velia::firewall::SysrepoFirewall firewall(srSess, [] (const auto& config) {
59 spdlog::get("firewall")->debug("running nft...");
Václav Kubernátde0e4e62021-02-08 17:46:14 +010060 velia::utils::execAndWait(spdlog::get("firewall"), NFT_EXECUTABLE, {"-f", "-"}, config);
Václav Kubernátd386aba2021-01-19 10:03:28 +010061
Václav Kubernátd386aba2021-01-19 10:03:28 +010062 spdlog::get("firewall")->debug("nft config applied.");
63 });
64
65 waitUntilSignaled();
66
Václav Kubernátd386aba2021-01-19 10:03:28 +010067 return 0;
68 } catch (std::exception& e) {
69 velia::utils::fatalException(spdlog::get("main"), e, "main");
70 }
71}