128 {
129 if (std::getenv("BASTIONGUARD_IN_BWRAP")) return;
131
132 const std::string resolv = "/tmp/bastionguard-resolv.conf";
134
135 const char* home = std::getenv("HOME");
136 if (!home || !*home) {
137 std::cerr <<
_(
"[BastionGuard-secure] HOME non impostata.\n");
138 return;
139 }
140
141 const std::string host_cache = std::string(home) + "/.cache/BastionGuard/cef_sandbox";
142 const std::string host_config = std::string(home) + "/.config/BastionGuard";
143
144
145 {
146 ::mkdir((std::string(home) + "/.cache").c_str(), 0700);
147 ::mkdir((std::string(home) + "/.cache/BastionGuard").c_str(), 0700);
148 ::mkdir(host_cache.c_str(), 0700);
149 }
150
151 const char* xdg_rt = std::getenv("XDG_RUNTIME_DIR");
152 const char* wl_disp = std::getenv("WAYLAND_DISPLAY");
153 const char* x_disp = std::getenv("DISPLAY");
154 const char* xauth = std::getenv("XAUTHORITY");
155
156 const bool have_wayland = wl_disp && *wl_disp && xdg_rt && *xdg_rt;
157 const bool have_x11 = x_disp && *x_disp;
158
159 BwrapArgs a;
160 a.add("bwrap");
161 a.add("--new-session");
162 a.add("--die-with-parent");
163
164
165 a.add("--unshare-uts");
166 a.add("--hostname"); a.add("BastionGuard-secure");
167
168
169 a.add("--ro-bind"); a.add("/"); a.add("/");
170
171
172 a.add("--dev"); a.add("/dev");
173 a.add("--proc"); a.add("/proc");
174
175
176 a.add("--tmpfs"); a.add("/tmp");
177 a.add("--ro-bind"); a.add(resolv); a.add("/etc/resolv.conf");
178
179
181 a.add("--bind"); a.add(host_cache); a.add(host_cache);
182 }
183
184
186 a.add("--ro-bind"); a.add(host_config); a.add(host_config);
187 }
188
189
190
191
192 if (have_wayland) {
194 a.add("--bind"), a.add(xdg_rt), a.add(xdg_rt);
195 a.add("--setenv"); a.add("WAYLAND_DISPLAY"); a.add(wl_disp);
196 std::cout <<
_(
"[BastionGuard-secure] bwrap: Wayland abilitato (")
197 << wl_disp << ")\n";
198 }
else if (xdg_rt && *xdg_rt &&
path_safe(xdg_rt)) {
199
200
201 a.add("--bind"); a.add(xdg_rt); a.add(xdg_rt);
202 }
203
204
205
206
207
208 if (have_x11) {
210 a.add("--ro-bind"); a.add("/tmp/.X11-unix"); a.add("/tmp/.X11-unix");
211 }
212
213 std::string xauth_path;
215 xauth_path = xauth;
216 } else {
217 const std::string default_xauth = std::string(home) + "/.Xauthority";
218 if (
path_exists(default_xauth)) xauth_path = default_xauth;
219 }
220 if (!xauth_path.empty()) {
221 a.add("--ro-bind"); a.add(xauth_path); a.add(xauth_path);
222 a.add("--setenv"); a.add("XAUTHORITY"); a.add(xauth_path);
223 }
224
225 a.add("--setenv"); a.add("DISPLAY"); a.add(x_disp);
226 std::cout <<
_(
"[BastionGuard-secure] bwrap: X11 abilitato (DISPLAY=")
227 << x_disp << ")\n";
228 }
229
230 if (!have_wayland && !have_x11) {
231 std::cerr <<
_(
"[BastionGuard-secure] ⚠ Nessun display Wayland/X11 rilevato; "
232 "il browser non riuscirà ad aprirsi.\n");
233 }
234
235
236 a.add("--setenv"); a.add("BASTIONGUARD_IN_BWRAP"); a.add("1");
237
238
239 if (const char* fb = std::getenv("BastionGuard_CEF_BACKEND")) {
240 a.add("--setenv"); a.add("BastionGuard_CEF_BACKEND"); a.add(fb);
241 }
242
243
244 if (argc < 1 || !argv[0]) {
245 std::cerr <<
_(
"[BastionGuard-secure] argv[0] mancante, abort bwrap.\n");
246 return;
247 }
248 a.add(argv[0]);
249 for (int i = 1; i < argc; ++i) {
250 if (argv[i]) a.add(argv[i]);
251 }
252
253 auto cargv = a.to_argv();
254 execvp("bwrap", cargv.data());
255 std::perror("execvp(bwrap)");
256}
static void write_resolv_conf(const std::string &path)
static bool path_safe(const char *p)
static bool path_exists(const std::string &p)
bool exists_on_path(const std::string &name)