Skip to content
API Documentation
BastionGuard™
FirewallRuleManager Class Reference

#include <FirewallRuleManager.hpp>

Static Public Member Functions

static void apply (FirewallType fw, const std::vector< std::string > &ips)

Static Private Member Functions

static bool run (const std::vector< std::string > &argv)
static void applyWithFirewalld (const std::vector< std::string > &ips)
static void applyWithUfw (const std::vector< std::string > &ips)

Detailed Description

Definition at line 28 of file FirewallRuleManager.hpp.

Member Function Documentation

◆ apply()

void FirewallRuleManager::apply ( FirewallType fw,
const std::vector< std::string > & ips )
static

Definition at line 108 of file FirewallRuleManager.cpp.

108 {
109 switch (fw) {
111 std::cout << _("[Firewall] Applying rules with firewalld (ipset)\n");
113 break;
115 std::cout << _("[Firewall] Applying rules with UFW\n");
116 applyWithUfw(ips);
117 break;
118 default:
119 std::cerr << _("[Firewall] Nessun firewall selezionato.\n");
120 break;
121 }
122}
#define _(s)
static void applyWithUfw(const std::vector< std::string > &ips)
static void applyWithFirewalld(const std::vector< std::string > &ips)

References _, applyWithFirewalld(), applyWithUfw(), FIREWALLD, and UFW.

◆ applyWithFirewalld()

void FirewallRuleManager::applyWithFirewalld ( const std::vector< std::string > & ips)
staticprivate

Definition at line 61 of file FirewallRuleManager.cpp.

61 {
62 std::unordered_set<std::string> ipv4;
63 std::unordered_set<std::string> ipv6;
64 for (const auto& ip : ips) {
65 switch (ip_family(ip)) {
66 case AF_INET: ipv4.insert(ip); break;
67 case AF_INET6: ipv6.insert(ip); break;
68 default: std::cerr << _("[Firewall] SKIP non-IP: ") << ip << "\n"; break;
69 }
70 }
71 if (ipv4.empty() && ipv6.empty()) {
72 std::cerr << _("[Firewall] Nessun IP valido da applicare.\n");
73 return;
74 }
75 const auto add_family = [&](const std::unordered_set<std::string>& entries,
76 const std::string& name, const std::string& family,
77 const std::string& ipset_family) {
78 if (entries.empty()) return;
79 // Already-existing sets may return nonzero; adding entries is idempotent.
80 run({"pkexec", "firewall-cmd", "--permanent", "--new-ipset=" + name,
81 "--type=hash:ip", "--option=family=" + ipset_family});
82 for (const auto& ip : entries) {
83 run({"pkexec", "firewall-cmd", "--permanent", "--ipset=" + name,
84 "--add-entry=" + ip});
85 }
86 run({"pkexec", "firewall-cmd", "--permanent",
87 "--add-rich-rule=rule family=\"" + family + "\" source ipset=\"" + name + "\" reject"});
88 };
89 add_family(ipv4, "bastionguard-blocklist", "ipv4", "inet");
90 add_family(ipv6, "bastionguard-blocklist-v6", "ipv6", "inet6");
91 run({"pkexec", "firewall-cmd", "--reload"});
92}
#define AF_INET
Definition bsc_ebpf.c:56
#define AF_INET6
Definition bsc_ebpf.c:57
static bool run(const std::vector< std::string > &argv)

References _, AF_INET, AF_INET6, and run().

Referenced by apply().

◆ applyWithUfw()

void FirewallRuleManager::applyWithUfw ( const std::vector< std::string > & ips)
staticprivate

Definition at line 94 of file FirewallRuleManager.cpp.

94 {
95 std::unordered_set<std::string> filtered;
96 for (const auto& ip : ips) {
97 if (ip_family(ip) != AF_UNSPEC) filtered.insert(ip);
98 else std::cerr << _("[Firewall] SKIP non-IP: ") << ip << "\n";
99 }
100 if (filtered.empty()) {
101 std::cerr << _("[Firewall] Nessun IP valido da applicare.\n");
102 return;
103 }
104 for (const auto& ip : filtered) run({"pkexec", "ufw", "deny", "from", ip});
105 run({"pkexec", "ufw", "reload"});
106}

References _, and run().

Referenced by apply().

◆ run()

bool FirewallRuleManager::run ( const std::vector< std::string > & argv)
staticprivate

Definition at line 42 of file FirewallRuleManager.cpp.

42 {
43 if (command.empty()) return false;
44 const pid_t pid = ::fork();
45 if (pid < 0) return false;
46 if (pid == 0) {
47 std::vector<char*> argv;
48 for (const auto& arg : command) argv.push_back(const_cast<char*>(arg.c_str()));
49 argv.push_back(nullptr);
50 ::execvp(argv.front(), argv.data());
51 _exit(127);
52 }
53 int status = 0;
54 pid_t result;
55 do { result = ::waitpid(pid, &status, 0); } while (result < 0 && errno == EINTR);
56 const bool ok = result == pid && WIFEXITED(status) && WEXITSTATUS(status) == 0;
57 if (!ok) std::cerr << _("[Firewall] Command failed: ") << command[0] << "\n";
58 return ok;
59}

References _.

Referenced by applyWithFirewalld(), and applyWithUfw().


The documentation for this class was generated from the following files: