Skip to content
API Documentation
BastionGuard™
xdg-main.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
21
22#include "xdg-open.hpp"
23#include "SecureBrowser.hpp"
24#include <include/cef_app.h>
25#include <iostream>
26#include <cstdlib>
27#include <cstring>
28#include <sys/wait.h>
29#include <unistd.h>
30#include <glibmm/i18n.h>
31
32int main(int argc, char** argv) {
33 CefMainArgs cef_args(argc, argv);
34 int rc = CefExecuteProcess(cef_args, nullptr, nullptr);
35 if (rc >= 0)
36 return rc;
37
38 if (argc < 2) {
39 std::cerr << _("Usage: BastionGuard-bankopener <url>\n");
40 return 2;
41 }
42
43 std::string url = argv[1];
44 std::cout << _("[BankOpener] 🔍 Verifico dominio: ") << url << std::endl;
45
46 const char* wayland = std::getenv("WAYLAND_DISPLAY");
47 const char* x11 = std::getenv("DISPLAY");
48
49 if (wayland && *wayland) {
50 std::cout << _("[BankOpener] 🖥️ Sessione Wayland rilevata → uso backend Wayland per CEF.\n");
51 setenv("GDK_BACKEND", "wayland", 1);
52 setenv("OZONE_PLATFORM", "wayland", 1);
53 // opzionale: rimuovi forcing software se la GPU è stabile
54 // unsetenv("LIBGL_ALWAYS_SOFTWARE");
55 }
56 else if (x11 && *x11) {
57 std::cout << _("[BankOpener] 🖥️ Sessione X11 rilevata → uso backend X11 per CEF.\n");
58 setenv("GDK_BACKEND", "x11", 1);
59 setenv("OZONE_PLATFORM", "x11", 1);
60 }
61 else {
62 std::cout << _("[BankOpener] ⚠️ Nessuna sessione grafica rilevata → fallback X11 software.\n");
63 setenv("GDK_BACKEND", "x11", 1);
64 setenv("OZONE_PLATFORM", "x11", 1);
65 setenv("LIBGL_ALWAYS_SOFTWARE", "1", 1);
66 }
67
68 setenv("LIBGL_ALWAYS_SOFTWARE", "1", 1);
69
71 return 0;
72 }
73
74 // ANTI-LOOP CRITICO (vedi nota in BastionGuard-secure.cpp).
75 // Il browser di sistema usa il proxy MITM di BastionGuard: aprire qui un
76 // dominio bank/secure lo farebbe re-intercettare dal proxy, che
77 // rilancerebbe BastionGuard-bankopener → xdg-open → loop infinito
78 // (migliaia di finestre). Se siamo già dentro la catena opener, non
79 // riapriamo; e comunque neutralizziamo il proxy per il processo figlio.
80 if (std::getenv("BG_OPENER_DEPTH")) {
81 std::cerr << _("[BankOpener] ⛔ Anti-loop: apertura già in corso, non rilancio: ")
82 << url << std::endl;
83 return 0;
84 }
85
86 std::cout << _("[BankOpener] 🌍 Apertura browser di sistema: ") << url << std::endl;
87
88 const pid_t pid = ::fork();
89 if (pid < 0) {
90 std::cerr << _("[BankOpener] ❌ Impossibile avviare il browser di sistema.\n");
91 return 1;
92 }
93
94 if (pid == 0) {
95 // Imposta l'ambiente del solo processo figlio senza passare da una shell.
96 ::setenv("BG_OPENER_DEPTH", "1", 1);
97 ::setenv("no_proxy", "*", 1);
98 ::setenv("NO_PROXY", "*", 1);
99 static const char* proxy_vars[] = {
100 "http_proxy", "https_proxy", "HTTP_PROXY", "HTTPS_PROXY", "all_proxy", "ALL_PROXY"
101 };
102 for (const char* name : proxy_vars)
103 ::unsetenv(name);
104
105 const char* gio_argv[] = {"gio", "open", url.c_str(), nullptr};
106 ::execvp("gio", const_cast<char* const*>(gio_argv));
107
108 const char* xdg_argv[] = {"xdg-open", url.c_str(), nullptr};
109 ::execvp("xdg-open", const_cast<char* const*>(xdg_argv));
110 _exit(127);
111 }
112
113 int status = 0;
114 if (::waitpid(pid, &status, 0) < 0) {
115 std::cerr << _("[BankOpener] ⚠️ waitpid fallita per browser di sistema.\n");
116 return 1;
117 }
118 if (WIFEXITED(status))
119 return WEXITSTATUS(status);
120 return 1;
121}
#define _(s)
int main(int argc, char *argv[])
static bool open_if_trusted(const std::string &url)
Definition xdg-open.cpp:37