Skip to content
API Documentation
BastionGuard™
PacManager Class Reference

#include <PacManager.hpp>

Classes

struct  Result

Static Public Member Functions

static Result enable_pac (const std::string &pac_url)
static Result disable_pac ()
static Result start_pacd_user ()
static Result stop_pacd_user ()
static Result enable_pac_with_pacd (const std::string &pac_url)
static Result disable_pac_with_pacd ()

Static Private Member Functions

static bool cmd_exists (const std::string &cmd)
static Result enable_gnome (const std::string &pac_url)
static Result disable_gnome ()
static Result enable_kde (const std::string &pac_url)
static Result disable_kde ()
static Result enable_xfce (const std::string &pac_url)
static Result disable_xfce ()
static Result enable_env_fallback ()
static Result disable_env_fallback ()

Detailed Description

Definition at line 24 of file PacManager.hpp.

Member Function Documentation

◆ cmd_exists()

bool PacManager::cmd_exists ( const std::string & cmd)
staticprivate

Definition at line 36 of file PacManager.cpp.

36 {
37 if (cmd.empty()) return false;
38 if (cmd.find('/') != std::string::npos) return access(cmd.c_str(), X_OK) == 0;
39 const char* configured_path = std::getenv("PATH");
40 std::istringstream dirs(configured_path ? configured_path : "/usr/local/bin:/usr/bin:/bin");
41 std::string dir;
42 while (std::getline(dirs, dir, ':')) {
43 if (dir.empty()) dir = ".";
44 if (access((fs::path(dir) / cmd).c_str(), X_OK) == 0) return true;
45 }
46 return false;
47}

Referenced by disable_gnome(), disable_kde(), disable_xfce(), enable_gnome(), enable_kde(), enable_xfce(), start_pacd_user(), and stop_pacd_user().

◆ disable_env_fallback()

PacManager::Result PacManager::disable_env_fallback ( )
staticprivate

Definition at line 335 of file PacManager.cpp.

335 {
336 Result r; r.backend = "env";
337 try {
338 fs::path f = envd_file();
339 if (fs::exists(f)) fs::remove(f);
340 r.ok = true;
341 r.message = _("Fallback headless: environment.d rimosso");
342 return r;
343 } catch (...) {
344 r.message = _("Impossibile rimuovere environment.d fallback");
345 return r;
346 }
347}
#define _(s)
static fs::path envd_file()

References _, and envd_file().

Referenced by disable_pac().

◆ disable_gnome()

PacManager::Result PacManager::disable_gnome ( )
staticprivate

Definition at line 142 of file PacManager.cpp.

142 {
143 Result r; r.backend = "gnome";
144
145 if (!cmd_exists("gsettings")) {
146 r.message = _("gsettings non disponibile");
147 return r;
148 }
149
150 int rc = bg_process::run({"gsettings", "set", "org.gnome.system.proxy", "mode", "none"}).exit_code;
151 if (rc == 0) {
152 r.ok = true;
153 r.message = _("Proxy GNOME ripristinato a NONE");
154 return r;
155 }
156 r.message = _("Impossibile disabilitare proxy GNOME");
157 return r;
158}
static bool cmd_exists(const std::string &cmd)
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 _, cmd_exists(), and bg_process::run().

Referenced by disable_pac().

◆ disable_kde()

PacManager::Result PacManager::disable_kde ( )
staticprivate

Definition at line 214 of file PacManager.cpp.

214 {
215 Result r; r.backend = "kde";
216
217 fs::path p = kde_kioslaverc();
218 if (!fs::exists(p)) {
219 r.message = _("kioslaverc non esiste");
220 return r;
221 }
222
223 if (cmd_exists("kwriteconfig6")) {
224 if (bg_process::run({"kwriteconfig6", "--file", p.string(),
225 "--group", "Proxy Settings", "--key", "ProxyType", "0"}).ok()) {
226 r.ok = true;
227 r.message = _("Proxy KDE ripristinato a NONE");
228 return r;
229 }
230 }
231
232 try {
233 std::string content;
234 {
235 std::ifstream in(p);
236 std::stringstream ss;
237 ss << in.rdbuf();
238 content = ss.str();
239 }
240
241 auto tpos = content.find("ProxyType=");
242 if (tpos != std::string::npos) {
243 auto eol = content.find('\n', tpos);
244 content.replace(tpos, (eol == std::string::npos ? content.size()-tpos : eol-tpos),
245 "ProxyType=0");
246 } else {
247 content += "\n[Proxy Settings]\nProxyType=0\n";
248 }
249
250 std::ofstream out(p, std::ios::trunc);
251 out << content;
252 out.close();
253
254 r.ok = true;
255 r.message = _("Proxy KDE ripristinato a NONE (fallback)");
256 return r;
257 } catch (...) {
258 r.message = _("Errore scrittura kioslaverc");
259 return r;
260 }
261}
static fs::path kde_kioslaverc()

References _, cmd_exists(), kde_kioslaverc(), PacManager::Result::ok, and bg_process::run().

Referenced by disable_pac().

◆ disable_pac()

PacManager::Result PacManager::disable_pac ( )
static

Definition at line 106 of file PacManager.cpp.

106 {
107 auto r = disable_gnome();
108 if (r.ok) return r;
109
110 r = disable_kde();
111 if (r.ok) return r;
112
113 r = disable_xfce();
114 if (r.ok) return r;
115
117 return r;
118}
static Result disable_gnome()
static Result disable_kde()
static Result disable_xfce()
static Result disable_env_fallback()

References disable_env_fallback(), disable_gnome(), disable_kde(), and disable_xfce().

Referenced by disable_pac_with_pacd().

◆ disable_pac_with_pacd()

PacManager::Result PacManager::disable_pac_with_pacd ( )
static

Definition at line 356 of file PacManager.cpp.

356 {
357 auto d = disable_pac();
358
360
361 return d;
362}
static Result disable_pac()
static Result stop_pacd_user()

References disable_pac(), and stop_pacd_user().

◆ disable_xfce()

PacManager::Result PacManager::disable_xfce ( )
staticprivate

Definition at line 288 of file PacManager.cpp.

288 {
289 Result r; r.backend = "xfce";
290
291 if (!cmd_exists("xfconf-query")) {
292 r.message = "xfconf-query non disponibile";
293 return r;
294 }
295
296 int rc = bg_process::run({"xfconf-query", "-c", "xfce4-settings-manager", "-p", "/proxy/mode", "-s", "0"}).exit_code;
297 if (rc == 0) {
298 r.ok = true;
299 r.message = _("Proxy XFCE ripristinato a NONE");
300 return r;
301 }
302
303 r.message = _("Impossibile disabilitare proxy XFCE");
304 return r;
305}

References _, cmd_exists(), and bg_process::run().

Referenced by disable_pac().

◆ enable_env_fallback()

PacManager::Result PacManager::enable_env_fallback ( )
staticprivate

Definition at line 312 of file PacManager.cpp.

312 {
313 Result r; r.backend = "env";
314 try {
315 fs::path f = envd_file();
316 fs::create_directories(f.parent_path());
317
318 // Proxy locale (adattalo alla tua porta reale)
319 std::ofstream out(f, std::ios::trunc);
320 out << "http_proxy=http://127.0.0.1:3129\n";
321 out << "https_proxy=http://127.0.0.1:3129\n";
322 out << "HTTP_PROXY=http://127.0.0.1:3129\n";
323 out << "HTTPS_PROXY=http://127.0.0.1:3129\n";
324 out.close();
325
326 r.ok = true;
327 r.message = _("Fallback headless: environment.d impostato (richiede relogin/riavvio sessione)");
328 return r;
329 } catch (...) {
330 r.message = _("Impossibile scrivere environment.d fallback");
331 return r;
332 }
333}

References _, and envd_file().

Referenced by enable_pac().

◆ enable_gnome()

PacManager::Result PacManager::enable_gnome ( const std::string & pac_url)
staticprivate

Definition at line 120 of file PacManager.cpp.

120 {
121 Result r; r.backend = "gnome";
122
123 if (!cmd_exists("gsettings")) {
124 r.message = _("gsettings non disponibile");
125 return r;
126 }
127
128 // Imposta PAC (AUTO)
129 int rc1 = bg_process::run({"gsettings", "set", "org.gnome.system.proxy", "mode", "auto"}).exit_code;
130 int rc2 = bg_process::run({"gsettings", "set", "org.gnome.system.proxy", "autoconfig-url", pac_url}).exit_code;
131
132 if (rc1 == 0 && rc2 == 0) {
133 r.ok = true;
134 r.message = _("Proxy GNOME impostato su AUTO (PAC)");
135 return r;
136 }
137
138 r.message = _("Impossibile impostare proxy GNOME via gsettings");
139 return r;
140}

References _, cmd_exists(), and bg_process::run().

Referenced by enable_pac().

◆ enable_kde()

PacManager::Result PacManager::enable_kde ( const std::string & pac_url)
staticprivate

Definition at line 165 of file PacManager.cpp.

165 {
166 Result r; r.backend = "kde";
167
168 fs::path p = kde_kioslaverc();
169 try {
170 fs::create_directories(p.parent_path());
171 } catch (...) {}
172
173 if (cmd_exists("kwriteconfig6")) {
174 auto first = bg_process::run({"kwriteconfig6", "--file", p.string(),
175 "--group", "Proxy Settings", "--key", "ProxyType", "2"});
176 if (first.ok() && bg_process::run({"kwriteconfig6", "--file", p.string(),
177 "--group", "Proxy Settings", "--key", "AutoConfigScript", pac_url}).ok()) {
178 r.ok = true;
179 r.message = _("Proxy KDE impostato su PAC (kioslaverc)");
180 return r;
181 }
182 }
183
184 try {
185 std::string content;
186 if (fs::exists(p)) {
187 std::ifstream in(p);
188 std::stringstream ss;
189 ss << in.rdbuf();
190 content = ss.str();
191 }
192 auto pos = content.find("[Proxy Settings]");
193 if (pos != std::string::npos) {
194 auto next = content.find("\n[", pos + 1);
195 if (next == std::string::npos) content.erase(pos);
196 else content.erase(pos, next - pos);
197 }
198
199 std::ofstream out(p, std::ios::app);
200 out << "\n[Proxy Settings]\n";
201 out << "ProxyType=2\n";
202 out << "AutoConfigScript=" << pac_url << "\n";
203 out.close();
204
205 r.ok = true;
206 r.message = _("Proxy KDE impostato su PAC (fallback write kioslaverc)");
207 return r;
208 } catch (...) {
209 r.message = _("Errore scrittura kioslaverc");
210 return r;
211 }
212}

References _, cmd_exists(), kde_kioslaverc(), PacManager::Result::ok, and bg_process::run().

Referenced by enable_pac().

◆ enable_pac()

PacManager::Result PacManager::enable_pac ( const std::string & pac_url)
static

Definition at line 91 of file PacManager.cpp.

91 {
92 auto r = enable_gnome(pac_url);
93 if (r.ok) return r;
94
95 r = enable_kde(pac_url);
96 if (r.ok) return r;
97
98 r = enable_xfce(pac_url);
99 if (r.ok) return r;
100
101 // headless fallback (NON PAC)
103 return r;
104}
static Result enable_xfce(const std::string &pac_url)
static Result enable_gnome(const std::string &pac_url)
static Result enable_kde(const std::string &pac_url)
static Result enable_env_fallback()

References enable_env_fallback(), enable_gnome(), enable_kde(), and enable_xfce().

Referenced by enable_pac_with_pacd().

◆ enable_pac_with_pacd()

PacManager::Result PacManager::enable_pac_with_pacd ( const std::string & pac_url)
static

Definition at line 349 of file PacManager.cpp.

349 {
350 auto s = start_pacd_user();
351 if (!s.ok) return s;
352
353 return enable_pac(pac_url);
354}
static Result start_pacd_user()
static Result enable_pac(const std::string &pac_url)

References enable_pac(), and start_pacd_user().

◆ enable_xfce()

PacManager::Result PacManager::enable_xfce ( const std::string & pac_url)
staticprivate

Definition at line 265 of file PacManager.cpp.

265 {
266 Result r; r.backend = "xfce";
267
268 if (!cmd_exists("xfconf-query")) {
269 r.message = "xfconf-query non disponibile";
270 return r;
271 }
272
273 const int rc1 = bg_process::run({"xfconf-query", "-c", "xfce4-settings-manager",
274 "-p", "/proxy/mode", "-s", "2"}).exit_code;
275 const int rc2 = bg_process::run({"xfconf-query", "-c", "xfce4-settings-manager",
276 "-p", "/proxy/autoconfig-url", "-s", pac_url}).exit_code;
277
278 if (rc1 == 0 && rc2 == 0) {
279 r.ok = true;
280 r.message = _("Proxy XFCE impostato su AUTO (PAC)");
281 return r;
282 }
283
284 r.message = _("Impossibile impostare proxy XFCE (canale/proprietà potrebbero differire)");
285 return r;
286}

References _, cmd_exists(), and bg_process::run().

Referenced by enable_pac().

◆ start_pacd_user()

PacManager::Result PacManager::start_pacd_user ( )
static

Definition at line 56 of file PacManager.cpp.

56 {
57 Result r; r.backend = "user-service-manager";
58
59 if (!cmd_exists("/usr/libexec/bastionguard/bastionguard-service")) {
60 r.message = _("gestore servizi BastionGuard non disponibile");
61 return r;
62 }
63
64 if (run_user_servicectl({"daemon-reload"}) &&
65 run_user_servicectl({"enable", "--now", "BastionGuard-pacd.service"})) {
66 r.ok = true;
67 r.message = _("bastionguard-pacd avviato (servizio utente)");
68 return r;
69 }
70
71 r.message = _("Impossibile avviare bastionguard-pacd (servizio utente)");
72 return r;
73}
static bool run_user_servicectl(const std::vector< std::string > &args)

References _, cmd_exists(), and run_user_servicectl().

Referenced by enable_pac_with_pacd().

◆ stop_pacd_user()

PacManager::Result PacManager::stop_pacd_user ( )
static

Definition at line 75 of file PacManager.cpp.

75 {
76 Result r; r.backend = "user-service-manager";
77
78 if (!cmd_exists("/usr/libexec/bastionguard/bastionguard-service")) {
79 r.message = _("gestore servizi BastionGuard non disponibile");
80 return r;
81 }
82
83 // stop best-effort
84 run_user_servicectl({"disable", "--now", "BastionGuard-pacd.service"});
85 r.ok = true;
86 r.message = _("bastionguard-pacd fermato (servizio utente)");
87 return r;
88}

References _, cmd_exists(), and run_user_servicectl().

Referenced by disable_pac_with_pacd().


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