Skip to content

Commit

Permalink
linux, wrap pkexec
Browse files Browse the repository at this point in the history
Signed-off-by: fufesou <[email protected]>
  • Loading branch information
fufesou committed May 12, 2023
1 parent 98a5f5e commit 59987f6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 51 deletions.
28 changes: 24 additions & 4 deletions src/platform/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,10 @@ pub fn quit_gui() {
unsafe { gtk_main_quit() };
}

pub fn exec_privileged(args: &[&str]) -> ResultType<Child> {
Ok(Command::new("pkexec").args(args).spawn()?)
}

pub fn check_super_user_permission() -> ResultType<bool> {
let file = "/usr/share/rustdesk/files/polkit";
let arg;
Expand All @@ -656,11 +660,11 @@ pub fn check_super_user_permission() -> ResultType<bool> {
} else {
arg = "echo";
}
let status = Command::new("pkexec").arg(arg).status()?;
let status = exec_privileged(&[arg])?.wait()?;
Ok(status.success() && status.code() == Some(0))
}

pub fn elevate(args: Vec<&str>) -> ResultType<Option<Child>> {
pub fn elevate(args: Vec<&str>) -> ResultType<bool> {
let cmd = std::env::current_exe()?;
match cmd.to_str() {
Some(cmd) => {
Expand All @@ -670,8 +674,24 @@ pub fn elevate(args: Vec<&str>) -> ResultType<Option<Child>> {
if is_opensuse() {
args_with_exe.insert(0, "-E");
}
let task = Command::new("pkexec").args(args_with_exe).spawn()?;
Ok(Some(task))
let res = match exec_privileged(&args_with_exe)?.wait() {
Ok(status) => {
if status.success() {
true
} else {
log::error!(
"Failed to wait install process, process status: {:?}",
status
);
false
}
}
Err(e) => {
log::error!("Failed to wait install process, error: {}", e);
false
}
};
Ok(res)
}
None => {
hbb_common::bail!("Failed to get current exe as str");
Expand Down
49 changes: 8 additions & 41 deletions src/plugin/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ fn get_source_plugins() -> HashMap<String, PluginInfo> {
match toml::from_str::<ManagerMeta>(&text) {
Ok(manager_meta) => {
for meta in manager_meta.plugins.iter() {
if !meta.platforms.to_uppercase().contains(&PLUGIN_PLATFORM.to_uppercase()) {
if !meta
.platforms
.to_uppercase()
.contains(&PLUGIN_PLATFORM.to_uppercase())
{
continue;
}
plugins.insert(
Expand Down Expand Up @@ -166,47 +170,10 @@ fn elevate_install(
} else {
format!("--plugin-install {} {}", plugin_id, plugin_url)
};
Ok(crate::platform::elevate(&args)?)
crate::platform::elevate(&args)
}

#[cfg(target_os = "linux")]
fn elevate_install(
plugin_id: &str,
plugin_url: &str,
same_plugin_exists: bool,
) -> ResultType<bool> {
let mut args = vec!["--plugin-install", plugin_id];
if !same_plugin_exists {
args.push(&plugin_url);
}
let allowed_install = match crate::platform::elevate(args) {
Ok(Some(mut child)) => match child.wait() {
Ok(status) => {
if status.success() {
true
} else {
log::error!(
"Failed to wait install process, process status: {:?}",
status
);
false
}
}
Err(e) => {
log::error!("Failed to wait install process, error: {}", e);
false
}
},
Ok(None) => false,
Err(e) => {
log::error!("Failed to run install process, error: {}", e);
false
}
};
Ok(allowed_install)
}

#[cfg(target_os = "macos")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn elevate_install(
plugin_id: &str,
plugin_url: &str,
Expand All @@ -216,7 +183,7 @@ fn elevate_install(
if !same_plugin_exists {
args.push(&plugin_url);
}
Ok(crate::platform::elevate(args)?)
crate::platform::elevate(args)
}

pub fn install_plugin(id: &str) -> ResultType<()> {
Expand Down
11 changes: 5 additions & 6 deletions src/ui_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ pub fn install_me(_options: String, _path: String, _silent: bool, _debug: bool)
pub fn update_me(_path: String) {
#[cfg(target_os = "linux")]
{
std::process::Command::new("pkexec")
.args(&["apt", "install", "-f", &_path])
.spawn()
.ok();
std::fs::remove_file(&_path).ok();
crate::run_me(Vec::<&str>::new()).ok();
allow_err!(crate::platform::linux::exec_privileged(&[
"apt", "install", "-f", &_path
]));
allow_err!(std::fs::remove_file(&_path));
allow_err!(crate::run_me(Vec::<&str>::new()));
}
#[cfg(windows)]
{
Expand Down

0 comments on commit 59987f6

Please sign in to comment.