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() {
super.initState();
Get.put<DesktopTabController>(tabController);
const lable = "install";
const label = "install";
tabController.add(TabInfo(
key: lable,
label: lable,
key: label,
label: label,
closable: false,
page: _InstallPageBody(
key: const ValueKey(lable),
key: const ValueKey(label),
)));
}

View File

@ -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;
@ -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) => {
@ -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");

View File

@ -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(
@ -166,10 +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")]
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn elevate_install(
plugin_id: &str,
plugin_url: &str,
@ -179,44 +183,7 @@ fn elevate_install(
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")]
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)?)
crate::platform::elevate(args)
}
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) {
#[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)]
{