Skip to content
API Documentation
BastionGuard™
CaInstaller Namespace Reference

Functions

static fs::path ca_src_path ()
static fs::path stamp_path ()
static std::string ca_fingerprint (const fs::path &ca_path)
static bool stamp_is_valid ()
static void write_stamp ()
static bool run (const std::vector< std::string > &args)
static std::string current_home ()
static std::vector< std::string > glob_expand (const std::string &pat)
static bool is_system_trusted ()
static void nss_add (const std::string &nssdb)
static void install_nss_user (const std::string &home)
static bool install_system_via_pkexec ()
static bool ensure_trusted ()

Variables

static constexpr const char * CA_NAME = "BastionGuard-CA"
static constexpr const char * HELPER

Function Documentation

◆ ca_fingerprint()

std::string CaInstaller::ca_fingerprint ( const fs::path & ca_path)
inlinestatic

Definition at line 59 of file ca_installer.hpp.

59 {
60 const auto result = bg_process::run({"openssl", "x509", "-in", ca_path.string(),
61 "-noout", "-fingerprint", "-sha1"});
62 if (!result.ok()) return {};
63 const auto eq = result.output.find('=');
64 if (eq == std::string::npos) return {};
65 std::string fingerprint = result.output.substr(eq + 1);
66 while (!fingerprint.empty() && (fingerprint.back() == '\n' || fingerprint.back() == '\r'))
67 fingerprint.pop_back();
68 return fingerprint;
69}
Result run(const std::vector< std::string > &args, bool merge_stderr=false, std::size_t limit=8U *1024U *1024U, const std::map< std::string, std::string > &environment={}, const std::function< bool(const std::string &)> &on_line={})

References ca_fingerprint(), bg_process::Result::output, and bg_process::run().

Referenced by ca_fingerprint(), stamp_is_valid(), and write_stamp().

◆ ca_src_path()

fs::path CaInstaller::ca_src_path ( )
inlinestatic

Definition at line 47 of file ca_installer.hpp.

47 {
48 const char* h = std::getenv("HOME");
49 return fs::path(h ? h : "/tmp")
50 / ".local/share/BastionGuard/certs/intercept-ca.crt.pem";
51}

References ca_src_path().

Referenced by ca_src_path(), ensure_trusted(), install_nss_user(), install_system_via_pkexec(), is_system_trusted(), nss_add(), stamp_is_valid(), and write_stamp().

◆ current_home()

std::string CaInstaller::current_home ( )
inlinestatic

Definition at line 92 of file ca_installer.hpp.

92 {
93 const char* h = std::getenv("HOME");
94 if (h && *h) return h;
95 const passwd* pw = getpwuid(getuid());
96 return pw ? pw->pw_dir : "";
97}

References current_home().

Referenced by current_home(), and ensure_trusted().

◆ ensure_trusted()

bool CaInstaller::ensure_trusted ( )
inlinestatic

Definition at line 179 of file ca_installer.hpp.

179 {
181 std::cerr << _("[ca-installer] CEF/CA disabilitati su Fedora/RHEL/Alma/Rocky\n");
182 return false;
183 }
184 const fs::path ca_path = ca_src_path();
185
186 if (!fs::exists(ca_path)) {
187 std::cerr << _("[ca-installer] ❌ CA intercept non trovata: ") << ca_path << "\n";
188 std::cerr << _("[ca-installer] ℹ️ Verrà generata al primo avvio del proxy TLS\n");
189 return false;
190 }
191
192 const std::string home = current_home();
194 std::cerr << _("[ca-installer] ✅ CA già trusted (sentinel valido)\n");
195 install_nss_user(home);
196 return true;
197 }
198
199 std::cerr << _("[ca-installer] 🔄 CA nuova o non ancora trusted — installazione...\n");
201 std::cerr << _("[ca-installer] ⚠️ Fallback sudo...\n");
202 const bool sudo_ok =
203 run({"sudo", HELPER, ca_path.string()});
204 if (!sudo_ok) {
205 std::cerr << _("[ca-installer] ❌ Impossibile installare CA nel sistema\n");
206 install_nss_user(home);
207 return false;
208 }
209 write_stamp();
210 }
211
212 install_nss_user(home);
213 return true;
214}
#define _(s)
static void write_stamp()
static constexpr const char * HELPER
static bool is_system_trusted()
static fs::path ca_src_path()
static bool install_system_via_pkexec()
static std::string current_home()
static bool run(const std::vector< std::string > &args)
static void install_nss_user(const std::string &home)
static bool stamp_is_valid()

References _, ca_src_path(), current_home(), ensure_trusted(), HELPER, install_nss_user(), install_system_via_pkexec(), BastionGuard::Platform::is_fedora_rhel_family(), is_system_trusted(), run(), stamp_is_valid(), and write_stamp().

Referenced by ensure_trusted().

◆ glob_expand()

std::vector< std::string > CaInstaller::glob_expand ( const std::string & pat)
inlinestatic

Definition at line 99 of file ca_installer.hpp.

99 {
100 std::vector<std::string> out;
101 glob_t g{};
102 if (glob(pat.c_str(), GLOB_NOSORT | GLOB_TILDE, nullptr, &g) == 0)
103 for (size_t i = 0; i < g.gl_pathc; ++i)
104 out.emplace_back(g.gl_pathv[i]);
105 globfree(&g);
106 return out;
107}

References glob_expand().

Referenced by glob_expand(), and install_nss_user().

◆ install_nss_user()

void CaInstaller::install_nss_user ( const std::string & home)
inlinestatic

Definition at line 137 of file ca_installer.hpp.

137 {
138 if (home.empty() || !fs::exists(ca_src_path())) return;
139
140 for (const auto& pat : std::vector<std::string>{
141 home + "/.mozilla/firefox/*.default",
142 home + "/.mozilla/firefox/*.default-release",
143 home + "/.mozilla/firefox/*.default-esr",
144 home + "/.var/app/org.mozilla.firefox/.mozilla/firefox/*.default",
145 home + "/.var/app/org.mozilla.firefox/.mozilla/firefox/*.default-release",
146 home + "/.pki/nssdb",
147 home + "/snap/chromium/current/.pki/nssdb",
148 })
149 {
150 for (const auto& match : glob_expand(pat))
151 nss_add(match);
152 }
153
154 std::cerr << _("[ca-installer] NSS utente aggiornato\n");
155}
static std::vector< std::string > glob_expand(const std::string &pat)
static void nss_add(const std::string &nssdb)

References _, ca_src_path(), glob_expand(), install_nss_user(), and nss_add().

Referenced by ensure_trusted(), and install_nss_user().

◆ install_system_via_pkexec()

bool CaInstaller::install_system_via_pkexec ( )
inlinestatic

Definition at line 157 of file ca_installer.hpp.

157 {
159 std::cerr << _("[ca-installer] CA di sistema disabilitata su Fedora/RHEL/Alma/Rocky\n");
160 return false;
161 }
162 if (!fs::exists(HELPER)) {
163 std::cerr << _("[ca-installer] ❌ helper non trovato: ") << HELPER << "\n";
164 return false;
165 }
166
167 std::cerr << _("[ca-installer] 🔐 Richiesta autorizzazione installazione CA...\n");
168 if (!run({"pkexec", HELPER, ca_src_path().string()})) {
169 std::cerr << _("[ca-installer] ❌ pkexec fallito o rifiutato dall'utente\n");
170 return false;
171 }
172
173 write_stamp();
174
175 std::cerr << _("[ca-installer] ✅ CA installata nel sistema via pkexec\n");
176 return true;
177}

References _, ca_src_path(), HELPER, install_system_via_pkexec(), BastionGuard::Platform::is_fedora_rhel_family(), run(), and write_stamp().

Referenced by ensure_trusted(), and install_system_via_pkexec().

◆ is_system_trusted()

bool CaInstaller::is_system_trusted ( )
inlinestatic

Definition at line 109 of file ca_installer.hpp.

109 {
110 const std::string ca = ca_src_path().string();
111 if (!fs::exists(ca)) return false;
112 for (const auto& bundle : {
113 std::string("/etc/ssl/certs/ca-certificates.crt"),
114 std::string("/etc/pki/tls/certs/ca-bundle.crt"),
115 std::string("/etc/ssl/ca-bundle.pem"),
116 std::string("/etc/pki/tls/cacert.pem"),
117 std::string("/usr/share/ca-certificates/ca-bundle.crt"),
118 std::string("/etc/ssl/certs/ca-bundle.crt"),
119 })
120 {
121 if (fs::exists(bundle) &&
122 run({"openssl", "verify", "-CAfile", bundle, ca}))
123 return true;
124 }
125 return false;
126}

References ca_src_path(), is_system_trusted(), and run().

Referenced by ensure_trusted(), and is_system_trusted().

◆ nss_add()

void CaInstaller::nss_add ( const std::string & nssdb)
inlinestatic

Definition at line 128 of file ca_installer.hpp.

128 {
129 if (!fs::exists(nssdb)) return;
130 const std::string ca = ca_src_path().string();
131 if (!fs::exists(ca)) return;
132 const std::string prefix = fs::exists(nssdb + "/cert9.db") ? "sql:" : "dbm:";
133 run({"certutil", "-A", "-d", prefix + nssdb,
134 "-n", CA_NAME, "-t", "CT,,", "-i", ca});
135}
static constexpr const char * CA_NAME

References CA_NAME, ca_src_path(), nss_add(), and run().

Referenced by install_nss_user(), and nss_add().

◆ run()

bool CaInstaller::run ( const std::vector< std::string > & args)
inlinestatic

Definition at line 88 of file ca_installer.hpp.

88 {
89 return bg_process::run(args).ok();
90}

References bg_process::Result::ok(), bg_process::run(), and run().

Referenced by ensure_trusted(), install_system_via_pkexec(), is_system_trusted(), nss_add(), and run().

◆ stamp_is_valid()

bool CaInstaller::stamp_is_valid ( )
inlinestatic

Definition at line 71 of file ca_installer.hpp.

71 {
72 if (!fs::exists(stamp_path())) return false;
73 std::ifstream f(stamp_path());
74 std::string stamped_fp;
75 std::getline(f, stamped_fp);
76 const std::string current_fp = ca_fingerprint(ca_src_path());
77 return !current_fp.empty() && (stamped_fp == current_fp);
78}
static fs::path stamp_path()
static std::string ca_fingerprint(const fs::path &ca_path)

References ca_fingerprint(), ca_src_path(), stamp_is_valid(), and stamp_path().

Referenced by ensure_trusted(), and stamp_is_valid().

◆ stamp_path()

fs::path CaInstaller::stamp_path ( )
inlinestatic

Definition at line 53 of file ca_installer.hpp.

53 {
54 const char* h = std::getenv("HOME");
55 return fs::path(h ? h : "/tmp")
56 / ".local/share/BastionGuard/ca-installed.stamp";
57}

References stamp_path().

Referenced by stamp_is_valid(), stamp_path(), and write_stamp().

◆ write_stamp()

void CaInstaller::write_stamp ( )
inlinestatic

Definition at line 80 of file ca_installer.hpp.

80 {
81 try {
82 fs::create_directories(stamp_path().parent_path());
83 std::ofstream f(stamp_path());
84 f << ca_fingerprint(ca_src_path()) << "\n";
85 } catch (...) {}
86}

References ca_fingerprint(), ca_src_path(), stamp_path(), and write_stamp().

Referenced by ensure_trusted(), install_system_via_pkexec(), and write_stamp().

Variable Documentation

◆ CA_NAME

const char* CaInstaller::CA_NAME = "BastionGuard-CA"
staticconstexpr

Definition at line 42 of file ca_installer.hpp.

Referenced by nss_add().

◆ HELPER

const char* CaInstaller::HELPER
staticconstexpr
Initial value:
=
"/usr/share/BastionGuard/data/scripts/install-ca-system.sh"

Definition at line 44 of file ca_installer.hpp.

Referenced by ensure_trusted(), and install_system_via_pkexec().