Merge pull request #4344 from fufesou/feat/linux_exec_privileged

Feat/linux exec privileged
This commit is contained in:
RustDesk 2023-05-12 16:51:54 +08:00 committed by GitHub
commit 305a04a785
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 55 deletions

View File

@ -24,13 +24,13 @@ class _InstallPageState extends State<InstallPage> {
void initState() { void initState() {
super.initState(); super.initState();
Get.put<DesktopTabController>(tabController); Get.put<DesktopTabController>(tabController);
const lable = "install"; const label = "install";
tabController.add(TabInfo( tabController.add(TabInfo(
key: lable, key: label,
label: lable, label: label,
closable: false, closable: false,
page: _InstallPageBody( page: _InstallPageBody(
key: const ValueKey(lable), key: const ValueKey(label),
))); )));
} }

View File

@ -648,6 +648,10 @@ pub fn quit_gui() {
unsafe { gtk_main_quit() }; 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> { pub fn check_super_user_permission() -> ResultType<bool> {
let file = "/usr/share/rustdesk/files/polkit"; let file = "/usr/share/rustdesk/files/polkit";
let arg; let arg;
@ -656,11 +660,11 @@ pub fn check_super_user_permission() -> ResultType<bool> {
} else { } else {
arg = "echo"; arg = "echo";
} }
let status = Command::new("pkexec").arg(arg).status()?; let status = exec_privileged(&[arg])?.wait()?;
Ok(status.success() && status.code() == Some(0)) 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()?; let cmd = std::env::current_exe()?;
match cmd.to_str() { match cmd.to_str() {
Some(cmd) => { Some(cmd) => {
@ -670,8 +674,24 @@ pub fn elevate(args: Vec<&str>) -> ResultType<Option<Child>> {
if is_opensuse() { if is_opensuse() {
args_with_exe.insert(0, "-E"); args_with_exe.insert(0, "-E");
} }
let task = Command::new("pkexec").args(args_with_exe).spawn()?; let res = match exec_privileged(&args_with_exe)?.wait() {
Ok(Some(task)) 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 => { None => {
hbb_common::bail!("Failed to get current exe as str"); hbb_common::bail!("Failed to get current exe as str");

View File

@ -80,7 +80,11 @@ fn get_source_plugins() -> HashMap<String, PluginInfo> {
match toml::from_str::<ManagerMeta>(&text) { match toml::from_str::<ManagerMeta>(&text) {
Ok(manager_meta) => { Ok(manager_meta) => {
for meta in manager_meta.plugins.iter() { 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; continue;
} }
plugins.insert( plugins.insert(
@ -166,10 +170,10 @@ fn elevate_install(
} else { } else {
format!("--plugin-install {} {}", plugin_id, plugin_url) format!("--plugin-install {} {}", plugin_id, plugin_url)
}; };
Ok(crate::platform::elevate(&args)?) crate::platform::elevate(&args)
} }
#[cfg(target_os = "linux")] #[cfg(any(target_os = "linux", target_os = "macos"))]
fn elevate_install( fn elevate_install(
plugin_id: &str, plugin_id: &str,
plugin_url: &str, plugin_url: &str,
@ -179,44 +183,7 @@ fn elevate_install(
if !same_plugin_exists { if !same_plugin_exists {
args.push(&plugin_url); args.push(&plugin_url);
} }
let allowed_install = match crate::platform::elevate(args) { 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")]
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);
}
Ok(crate::platform::elevate(args)?)
} }
pub fn install_plugin(id: &str) -> ResultType<()> { pub fn install_plugin(id: &str) -> ResultType<()> {

View File

@ -77,12 +77,11 @@ pub fn install_me(_options: String, _path: String, _silent: bool, _debug: bool)
pub fn update_me(_path: String) { pub fn update_me(_path: String) {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
std::process::Command::new("pkexec") allow_err!(crate::platform::linux::exec_privileged(&[
.args(&["apt", "install", "-f", &_path]) "apt", "install", "-f", &_path
.spawn() ]));
.ok(); allow_err!(std::fs::remove_file(&_path));
std::fs::remove_file(&_path).ok(); allow_err!(crate::run_me(Vec::<&str>::new()));
crate::run_me(Vec::<&str>::new()).ok();
} }
#[cfg(windows)] #[cfg(windows)]
{ {