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

Functions

static bool run_cmd_capture (const std::string &cmd, std::string *out=nullptr, int *exit_code=nullptr)
static std::string elev_cmd (const std::string &inner, const std::string &action_id="org.BastionGuard.maintenance")
static std::string user_data_dir ()
static bool write_temp_script_and_elevate (const std::string &script_contents, std::string *out_log=nullptr)
static bool file_exists (const std::string &path)
static std::string run_capture (const std::string &cmd)
static bool detect_resolvectl ()
static bool detect_nmcli ()
static std::string find_active_interface_nmcli ()
static std::string find_active_link_resolvectl ()

Function Documentation

◆ detect_nmcli()

bool anonymous_namespace{wizard_setup.cpp}::detect_nmcli ( )
static

Definition at line 169 of file wizard_setup.cpp.

169 {
170 return run_cmd_capture("command -v nmcli >/dev/null 2>&1");
171}
static bool run_cmd_capture(const std::string &cmd, std::string *out=nullptr, int *exit_code=nullptr)

References detect_nmcli(), and run_cmd_capture().

Referenced by detect_nmcli(), and find_active_link_resolvectl().

◆ detect_resolvectl()

bool anonymous_namespace{wizard_setup.cpp}::detect_resolvectl ( )
static

Definition at line 164 of file wizard_setup.cpp.

164 {
165 if (run_cmd_capture("command -v resolvectl >/dev/null 2>&1")) return true;
166 return false;
167}

References detect_resolvectl(), and run_cmd_capture().

Referenced by detect_resolvectl().

◆ elev_cmd()

std::string anonymous_namespace{wizard_setup.cpp}::elev_cmd ( const std::string & inner,
const std::string & action_id = "org.BastionGuard.maintenance" )
static

Definition at line 61 of file wizard_setup.cpp.

62 {
63 std::ostringstream cmd;
64
65 bool has_action_id = run_cmd_capture("pkexec --help | grep -q action-id");
66
67 if (fs::exists("/usr/bin/pkexec")) {
68 if (has_action_id) {
69 cmd << "pkexec --disable-internal-agent --action-id " << action_id
70 << " /bin/bash -c " << BastionGuard::wizard::shell_quote(inner);
71 } else {
72
73 cmd << "pkexec /bin/bash -c " << BastionGuard::wizard::shell_quote(inner);
74 }
75 } else {
76 cmd << "sudo /bin/bash -c " << BastionGuard::wizard::shell_quote(inner);
77 }
78
79 return cmd.str();
80}
std::string shell_quote(const std::string &argument)

References elev_cmd(), run_cmd_capture(), and BastionGuard::wizard::shell_quote().

Referenced by elev_cmd().

◆ file_exists()

bool anonymous_namespace{wizard_setup.cpp}::file_exists ( const std::string & path)
static

Definition at line 151 of file wizard_setup.cpp.

151 {
152 struct stat st;
153 return (stat(path.c_str(), &st) == 0);
154}

References file_exists().

Referenced by file_exists().

◆ find_active_interface_nmcli()

std::string anonymous_namespace{wizard_setup.cpp}::find_active_interface_nmcli ( )
static

Definition at line 173 of file wizard_setup.cpp.

173 {
174 std::string cmd = "nmcli -t -f DEVICE,STATE device status 2>/dev/null | awk -F: '$2==\"connected\"{print $1; exit}'";
175 return run_capture(cmd);
176}
static std::string run_capture(const std::string &cmd)

References find_active_interface_nmcli(), and run_capture().

Referenced by find_active_interface_nmcli(), and find_active_link_resolvectl().

◆ find_active_link_resolvectl()

std::string anonymous_namespace{wizard_setup.cpp}::find_active_link_resolvectl ( )
static

Definition at line 178 of file wizard_setup.cpp.

178 {
179 std::string out = run_capture("ip route get 1.1.1.1 2>/dev/null | awk '{for(i=1;i<=NF;i++){ if($i==\"dev\") {print $(i+1); exit}}}'");
180 if (!out.empty()) return out;
181 if (detect_nmcli()) {
183 }
184 return {};
185}
static std::string find_active_interface_nmcli()

References detect_nmcli(), find_active_interface_nmcli(), find_active_link_resolvectl(), and run_capture().

Referenced by find_active_link_resolvectl().

◆ run_capture()

std::string anonymous_namespace{wizard_setup.cpp}::run_capture ( const std::string & cmd)
static

Definition at line 156 of file wizard_setup.cpp.

156 {
157 const auto capture = bg_process::run({"/bin/sh", "-c", cmd});
158 if (!capture.ok()) return {};
159 std::string out = capture.output;
160 while (!out.empty() && (out.back() == '\n' || out.back() == '\r')) out.pop_back();
161 return out;
162}
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 bg_process::Result::output, bg_process::run(), and run_capture().

Referenced by find_active_interface_nmcli(), find_active_link_resolvectl(), and run_capture().

◆ run_cmd_capture()

bool anonymous_namespace{wizard_setup.cpp}::run_cmd_capture ( const std::string & cmd,
std::string * out = nullptr,
int * exit_code = nullptr )
static

Definition at line 52 of file wizard_setup.cpp.

52 {
53 // The legacy setup command plans contain fixed firewall shell operators.
54 // Do not add an extra /bin/sh layer or interpolate unquoted user data.
55 const auto result = bg_process::run({"/bin/sh", "-c", cmd}, true);
56 if (out) *out = result.output;
57 if (exit_code) *exit_code = result.exit_code;
58 return result.ok();
59}

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

Referenced by detect_nmcli(), detect_resolvectl(), elev_cmd(), run_cmd_capture(), and write_temp_script_and_elevate().

◆ user_data_dir()

std::string anonymous_namespace{wizard_setup.cpp}::user_data_dir ( )
static

Definition at line 86 of file wizard_setup.cpp.

86 {
87 const char* xdg = getenv("XDG_DATA_HOME");
88 if (xdg && *xdg) return std::string(xdg) + "/BastionGuard";
89 const char* home = getenv("HOME");
90 if (!home) home = "";
91 return std::string(home) + "/.local/share/BastionGuard";
92}

References user_data_dir().

Referenced by user_data_dir().

◆ write_temp_script_and_elevate()

bool anonymous_namespace{wizard_setup.cpp}::write_temp_script_and_elevate ( const std::string & script_contents,
std::string * out_log = nullptr )
static

Definition at line 94 of file wizard_setup.cpp.

94 {
95 char tmpl[] = "/tmp/BastionGuard_tmp_XXXXXX";
96 int fd = mkstemp(tmpl);
97 if (fd < 0) {
98 if (out_log) *out_log = _("Impossibile creare file temporaneo");
99 return false;
100 }
101
102 std::string tmpname = tmpl;
103 FILE* f = fdopen(fd, "w");
104 if (!f) {
105 close(fd);
106 if (out_log) *out_log = _("fdopen fallita");
107 return false;
108 }
109
110 fputs(script_contents.c_str(), f);
111 fclose(f);
112 chmod(tmpname.c_str(), 0755);
113
114 bool has_action_id = run_cmd_capture("pkexec --help | grep -q action-id");
115
116 std::ostringstream elev;
117 if (fs::exists("/usr/bin/pkexec")) {
118 if (has_action_id) {
119 elev << "pkexec --disable-internal-agent --action-id org.BastionGuard.maintenance " << tmpname;
120 } else {
121 elev << "pkexec " << tmpname;
122 }
123 } else {
124 elev << "sudo " << tmpname;
125 }
126
127 std::string out;
128 int rc = 0;
129 bool ok = run_cmd_capture(elev.str(), &out, &rc);
130 std::cerr << _("\n===== [BastionGuard DEBUG: write_temp_script_and_elevate] =====\n");
131 std::cerr << _("Comando elevato eseguito:\n") << elev.str() << "\n";
132 std::cerr << "Exit code: " << rc << (ok ? " (OK)" : " (FAIL)") << "\n";
133 if (!out.empty())
134 std::cerr << "Output:\n" << out << "\n";
135 else
136 std::cerr << _("(nessun output catturato)\n");
137 std::cerr << "===========================================================\n";
138
139 std::error_code ec;
140 fs::remove(tmpname, ec);
141
142 if (out_log) *out_log = out;
143 return ok && rc == 0;
144}
#define _(s)

References _, run_cmd_capture(), and write_temp_script_and_elevate().

Referenced by write_temp_script_and_elevate().