Skip to content
API Documentation
BastionGuard™
FirewallRuleManager.cpp
Go to the documentation of this file.
1/*
2 * BastionGuard™
3 * Copyright (C) 2025–2026 Calogero Scarnà
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * BastionGuard™ is a trademark of Calogero Scarnà.
18 * The BastionGuard™ name and branding are not licensed under the GPL.
19 */
20
22#include <cerrno>
23#include <iostream>
24#include <unordered_set>
25#include <vector>
26#include <arpa/inet.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <unistd.h>
30#include <glib/gi18n.h>
31
32namespace {
33int ip_family(const std::string& ip) {
34 struct in_addr ipv4 {};
35 if (::inet_pton(AF_INET, ip.c_str(), &ipv4) == 1) return AF_INET;
36 struct in6_addr ipv6 {};
37 if (::inet_pton(AF_INET6, ip.c_str(), &ipv6) == 1) return AF_INET6;
38 return AF_UNSPEC;
39}
40}
41
42bool FirewallRuleManager::run(const std::vector<std::string>& command) {
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}
60
61void FirewallRuleManager::applyWithFirewalld(const std::vector<std::string>& ips) {
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}
93
94void FirewallRuleManager::applyWithUfw(const std::vector<std::string>& ips) {
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}
107
108void FirewallRuleManager::apply(FirewallType fw, const std::vector<std::string>& ips) {
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)
FirewallType
#define AF_INET
Definition bsc_ebpf.c:56
#define AF_INET6
Definition bsc_ebpf.c:57
static void applyWithUfw(const std::vector< std::string > &ips)
static void apply(FirewallType fw, const std::vector< std::string > &ips)
static bool run(const std::vector< std::string > &argv)
static void applyWithFirewalld(const std::vector< std::string > &ips)