Skip to content
API Documentation
BastionGuard™
bastionguard-firewall.cpp File Reference

Go to the source code of this file.

Classes

struct  anonymous_namespace{bastionguard-firewall.cpp}::SecureInput

Namespaces

namespace  anonymous_namespace{bastionguard-firewall.cpp}

Functions

std::optional< uid_t > anonymous_namespace{bastionguard-firewall.cpp}::parse_uid (const char *value)
uid_t anonymous_namespace{bastionguard-firewall.cpp}::original_caller_uid ()
bool anonymous_namespace{bastionguard-firewall.cpp}::allowed_input_path (const std::string &path)
std::optional< SecureInput > anonymous_namespace{bastionguard-firewall.cpp}::open_input_securely (const std::string &path, uid_t caller_uid)
std::string anonymous_namespace{bastionguard-firewall.cpp}::trim (std::string value)
bool anonymous_namespace{bastionguard-firewall.cpp}::is_valid_ip (const std::string &value)
std::vector< std::string > anonymous_namespace{bastionguard-firewall.cpp}::load_and_validate_ips (int fd)
std::optional< std::string > anonymous_namespace{bastionguard-firewall.cpp}::find_program (std::initializer_list< const char * > candidates)
bool anonymous_namespace{bastionguard-firewall.cpp}::run_program (const std::string &executable, const std::vector< std::string > &arguments, bool silence_stderr=false)
bool anonymous_namespace{bastionguard-firewall.cpp}::apply_ufw (const std::vector< std::string > &ips)
bool anonymous_namespace{bastionguard-firewall.cpp}::apply_firewalld (const std::vector< std::string > &ips)
void anonymous_namespace{bastionguard-firewall.cpp}::clear_firewall ()
int main (int argc, char *argv[])

Variables

constexpr std::string_view anonymous_namespace{bastionguard-firewall.cpp}::kInputPrefix = "/tmp/bastionguard-firewall-ips-"
constexpr off_t anonymous_namespace{bastionguard-firewall.cpp}::kMaxInputBytes = 16 * 1024 * 1024
constexpr std::size_t anonymous_namespace{bastionguard-firewall.cpp}::kMaxEntries = 250000
constexpr std::size_t anonymous_namespace{bastionguard-firewall.cpp}::kMaxLineLength = 128

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 335 of file bastionguard-firewall.cpp.

335 {
336 const uid_t caller_uid = original_caller_uid();
337
338 ::clearenv();
339 ::setenv("PATH", "/usr/sbin:/usr/bin:/sbin:/bin", 1);
340
341 if (::geteuid() != 0) {
342 std::cerr << _("[Firewall] Deve essere eseguito come root tramite pkexec.\n");
343 return 10;
344 }
345
346 if (argc < 2) {
347 std::cerr << "Uso: " << argv[0] << " apply <input-file>|clear\n";
348 return 1;
349 }
350
351 const std::string action = argv[1];
352
353 if (action == "clear") {
354 if (argc != 2) return 1;
356 std::cout << _("[Firewall] Regole rimosse.\n");
357 return 0;
358 }
359
360 if (action != "apply" || argc != 3) {
361 std::cerr << _("[Firewall] Comando non valido.\n");
362 return 2;
363 }
364
365 auto input = open_input_securely(argv[2], caller_uid);
366 if (!input) return 3;
367
368 auto ips = load_and_validate_ips(input->fd);
369 if (ips.empty()) {
370 std::cerr << _("[Firewall] Blacklist vuota o non valida.\n");
371 return 4;
372 }
373
374 const bool ok = apply_firewalld(ips) || apply_ufw(ips);
375
376 if (ok) {
377 std::cout << _("[Firewall] Regole applicate (")
378 << ips.size() << _(" IP).\n");
379 return 0;
380 }
381
382 std::cerr << _("[Firewall] Nessun firewall supportato o applicazione fallita.\n");
383 return 5;
384}
#define _(s)
bool apply_firewalld(const std::vector< std::string > &ips)
std::optional< SecureInput > open_input_securely(const std::string &path, uid_t caller_uid)
bool apply_ufw(const std::vector< std::string > &ips)
std::vector< std::string > load_and_validate_ips(int fd)

References _, anonymous_namespace{bastionguard-firewall.cpp}::apply_firewalld(), anonymous_namespace{bastionguard-firewall.cpp}::apply_ufw(), anonymous_namespace{bastionguard-firewall.cpp}::clear_firewall(), anonymous_namespace{bastionguard-firewall.cpp}::load_and_validate_ips(), main(), anonymous_namespace{bastionguard-firewall.cpp}::open_input_securely(), and anonymous_namespace{bastionguard-firewall.cpp}::original_caller_uid().