blob: 8cf58a60c7821188f62974963b49e52beef20f98 [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>
5#include <sysrepo-cpp/Session.hpp>
6#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"
Václav Kubernátd386aba2021-01-19 10:03:28 +010015#include "utils/waitUntilSignalled.h"
16
Václav Kubernátd386aba2021-01-19 10:03:28 +010017static const char usage[] =
18 R"(Bridge between sysrepo and nftables.
19
20Usage:
21 veliad-firewall
22 [--sysrepo-log-level=<Level>]
23 [--firewall-log-level=<Level>]
24 veliad-firewall (-h | --help)
25 veliad-firewall --version
26
27Options:
28 -h --help Show this screen.
29 --version Show version.
30 --firewall-log-level=<N> Log level for the firewall [default: 3]
Václav Kubernát5f57dec2021-02-11 02:02:46 +010031 --sysrepo-log-level=<N> Log level for the sysrepo library [default: 2]
32 (0 -> critical, 1 -> error, 2 -> warning, 3 -> info,
33 4 -> debug, 5 -> trace)
Václav Kubernátd386aba2021-01-19 10:03:28 +010034)";
35
36int main(int argc, char* argv[])
37{
38 std::shared_ptr<spdlog::sinks::sink> loggingSink;
39 if (velia::utils::isJournaldActive()) {
40 loggingSink = velia::utils::create_journald_sink();
41 } else {
42 loggingSink = std::make_shared<spdlog::sinks::ansicolor_stderr_sink_mt>();
43 }
44
Tomáš Peckac50ec9e2021-01-27 12:22:42 +010045 auto args = docopt::docopt(usage, {argv + 1, argv + argc}, true, "veliad-firewall " VELIA_VERSION, true);
Václav Kubernátd386aba2021-01-19 10:03:28 +010046
47 velia::utils::initLogs(loggingSink);
48 spdlog::set_level(spdlog::level::info);
49
50 try {
51 spdlog::get("firewall")->set_level(parseLogLevel("Firewall logging", args["--firewall-log-level"]));
52 spdlog::get("sysrepo")->set_level(parseLogLevel("Sysrepo library", args["--sysrepo-log-level"]));
53
54 spdlog::get("main")->debug("Opening Sysrepo connection");
55 auto srConn = std::make_shared<sysrepo::Connection>();
56 auto srSess = std::make_shared<sysrepo::Session>(srConn);
57 velia::firewall::SysrepoFirewall firewall(srSess, [] (const auto& config) {
58 spdlog::get("firewall")->debug("running nft...");
Václav Kubernátde0e4e62021-02-08 17:46:14 +010059 velia::utils::execAndWait(spdlog::get("firewall"), NFT_EXECUTABLE, {"-f", "-"}, config);
Václav Kubernátd386aba2021-01-19 10:03:28 +010060
Václav Kubernátd386aba2021-01-19 10:03:28 +010061 spdlog::get("firewall")->debug("nft config applied.");
62 });
63
64 waitUntilSignaled();
65
66 spdlog::get("main")->info("Exiting.");
67
68 return 0;
69 } catch (std::exception& e) {
70 velia::utils::fatalException(spdlog::get("main"), e, "main");
71 }
72}