Skip to content
API Documentation
BastionGuard™
PrivilegeUtils Class Reference

#include <privilege_utils.hpp>

Static Public Member Functions

static bool is_running_as_root ()
static bool can_read_source (const std::string &path)
static std::string self_executable_path ()
static std::string shell_quote (const std::string &s)
static std::string build_pkexec_relaunch_command (const std::string &extra_args="")
static bool launch_pkexec_relaunch (std::string &error, const std::string &extra_args="")

Detailed Description

Definition at line 5 of file privilege_utils.hpp.

Member Function Documentation

◆ build_pkexec_relaunch_command()

std::string PrivilegeUtils::build_pkexec_relaunch_command ( const std::string & extra_args = "")
static

Definition at line 39 of file privilege_utils.cpp.

39 {
40 const std::string exe = shell_quote(self_executable_path());
41 std::string cmd = "pkexec ";
42 cmd += exe;
43 if (!extra_args.empty()) {
44 cmd += " ";
45 cmd += extra_args;
46 }
47 return cmd;
48}
static std::string shell_quote(const std::string &s)
static std::string self_executable_path()

References self_executable_path(), and shell_quote().

Referenced by launch_pkexec_relaunch().

◆ can_read_source()

bool PrivilegeUtils::can_read_source ( const std::string & path)
static

Definition at line 13 of file privilege_utils.cpp.

13 {
14 return !path.empty() && ::access(path.c_str(), R_OK) == 0;
15}

Referenced by MainWindow::on_scan_now(), MainWindow::populate_device_lists(), and MainWindow::update_source_state().

◆ is_running_as_root()

bool PrivilegeUtils::is_running_as_root ( )
static

◆ launch_pkexec_relaunch()

bool PrivilegeUtils::launch_pkexec_relaunch ( std::string & error,
const std::string & extra_args = "" )
static

Definition at line 50 of file privilege_utils.cpp.

50 {
51 const std::string cmd = build_pkexec_relaunch_command(extra_args) + " >/dev/null 2>&1 &";
52 const int rc = std::system(cmd.c_str());
53 if (rc != 0) {
54 error = std::string("pkexec launch failed: ") + std::strerror(errno);
55 return false;
56 }
57 return true;
58}
static std::string build_pkexec_relaunch_command(const std::string &extra_args="")

References build_pkexec_relaunch_command().

Referenced by MainWindow::on_launch_elevated().

◆ self_executable_path()

std::string PrivilegeUtils::self_executable_path ( )
static

Definition at line 17 of file privilege_utils.cpp.

17 {
18#ifdef __linux__
19 char buf[4096] = {0};
20 const ssize_t n = ::readlink("/proc/self/exe", buf, sizeof(buf) - 1);
21 if (n > 0) {
22 buf[n] = '\0';
23 return std::string(buf);
24 }
25#endif
26 return "bastionguard-recovery-data";
27}

Referenced by build_pkexec_relaunch_command().

◆ shell_quote()

std::string PrivilegeUtils::shell_quote ( const std::string & s)
static

Definition at line 29 of file privilege_utils.cpp.

29 {
30 std::string out = "'";
31 for (char c : s) {
32 if (c == '\'') out += "'\\''";
33 else out.push_back(c);
34 }
35 out += "'";
36 return out;
37}

Referenced by build_pkexec_relaunch_command().


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