Skip to content
API Documentation
BastionGuard™
anonymous_namespace{Backend.cpp} Namespace Reference

Functions

bool create_secure_firewall_input (const std::vector< std::string > &blacklistLines, std::string &outPath)
int run_firewall_helper (const std::string &inputPath)

Function Documentation

◆ create_secure_firewall_input()

bool anonymous_namespace{Backend.cpp}::create_secure_firewall_input ( const std::vector< std::string > & blacklistLines,
std::string & outPath )

Definition at line 3003 of file Backend.cpp.

3006 {
3007 std::string pathTemplate = "/tmp/bastionguard-firewall-ips-XXXXXX";
3008 std::vector<char> pathBuffer(pathTemplate.begin(), pathTemplate.end());
3009 pathBuffer.push_back('\0');
3010
3011 const int fd = ::mkstemp(pathBuffer.data());
3012 if (fd < 0) {
3013 std::cerr << _("[Firewall] Impossibile creare il file temporaneo sicuro\n");
3014 return false;
3015 }
3016
3017 outPath.assign(pathBuffer.data());
3018
3019 if (::fchmod(fd, S_IRUSR | S_IWUSR) != 0) {
3020 ::close(fd);
3021 ::unlink(outPath.c_str());
3022 outPath.clear();
3023 std::cerr << _("[Firewall] Impossibile proteggere il file temporaneo\n");
3024 return false;
3025 }
3026
3027 ::close(fd);
3028
3029 if (!BlacklistIpExtractor::extractAndWrite(blacklistLines, outPath)) {
3030 ::unlink(outPath.c_str());
3031 outPath.clear();
3032 return false;
3033 }
3034
3035 // extractAndWrite() riapre il file: imponiamo nuovamente 0600 e
3036 // verifichiamo che il pathname indichi ancora un singolo file regolare
3037 // appartenente all'utente che ha avviato BastionGuard.
3038 if (::chmod(outPath.c_str(), S_IRUSR | S_IWUSR) != 0) {
3039 ::unlink(outPath.c_str());
3040 outPath.clear();
3041 std::cerr << _("[Firewall] Impossibile impostare i permessi del file temporaneo\n");
3042 return false;
3043 }
3044
3045 struct stat st {};
3046 if (::lstat(outPath.c_str(), &st) != 0 ||
3047 !S_ISREG(st.st_mode) ||
3048 st.st_uid != ::getuid() ||
3049 st.st_nlink != 1 ||
3050 (st.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
3051 ::unlink(outPath.c_str());
3052 outPath.clear();
3053 std::cerr << _("[Firewall] File temporaneo non sicuro\n");
3054 return false;
3055 }
3056
3057 return true;
3058}
#define _(s)
static bool extractAndWrite(const std::vector< std::string > &lines, const std::string &outPath="/tmp/bastionguard-firewall-ips.txt")
#define S_IWOTH
#define S_IRUSR
#define S_IWUSR
#define S_IWGRP

References _, create_secure_firewall_input(), BlacklistIpExtractor::extractAndWrite(), S_IRUSR, S_IWGRP, S_IWOTH, and S_IWUSR.

Referenced by create_secure_firewall_input().

◆ run_firewall_helper()

int anonymous_namespace{Backend.cpp}::run_firewall_helper ( const std::string & inputPath)

Definition at line 3060 of file Backend.cpp.

3060 {
3061 const pid_t pid = ::fork();
3062 if (pid < 0)
3063 return -1;
3064
3065 if (pid == 0) {
3066 ::execl(
3067 "/usr/bin/pkexec",
3068 "pkexec",
3069 "/usr/bin/bastionguard-firewall",
3070 "apply",
3071 inputPath.c_str(),
3072 static_cast<char*>(nullptr)
3073 );
3074 _exit(127);
3075 }
3076
3077 int status = 0;
3078 while (::waitpid(pid, &status, 0) < 0) {
3079 if (errno == EINTR)
3080 continue;
3081 return -1;
3082 }
3083
3084 if (!WIFEXITED(status))
3085 return -1;
3086
3087 return WEXITSTATUS(status);
3088}

References run_firewall_helper().

Referenced by run_firewall_helper().