Merge pull request #4392 from fufesou/refact/macos_elevate

Refact/macos elevate
This commit is contained in:
RustDesk 2023-05-16 18:52:03 +08:00 committed by GitHub
commit b491a23f41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 24 deletions

View File

@ -17,15 +17,11 @@ use core_graphics::{
display::{kCGNullWindowID, kCGWindowListOptionOnScreenOnly, CGWindowListCopyWindowInfo}, display::{kCGNullWindowID, kCGWindowListOptionOnScreenOnly, CGWindowListCopyWindowInfo},
window::{kCGWindowName, kCGWindowOwnerPID}, window::{kCGWindowName, kCGWindowOwnerPID},
}; };
use hbb_common::{allow_err, anyhow::anyhow, bail, libc, log, message_proto::Resolution}; use hbb_common::{allow_err, anyhow::anyhow, bail, log, message_proto::Resolution};
use include_dir::{include_dir, Dir}; use include_dir::{include_dir, Dir};
use objc::{class, msg_send, sel, sel_impl}; use objc::{class, msg_send, sel, sel_impl};
use scrap::{libc::c_void, quartz::ffi::*}; use scrap::{libc::c_void, quartz::ffi::*};
use std::{ use std::{ffi::c_char, path::PathBuf};
ffi::{c_char, CString},
mem::size_of,
path::PathBuf,
};
static PRIVILEGES_SCRIPTS_DIR: Dir = static PRIVILEGES_SCRIPTS_DIR: Dir =
include_dir!("$CARGO_MANIFEST_DIR/src/platform/privileges_scripts"); include_dir!("$CARGO_MANIFEST_DIR/src/platform/privileges_scripts");
@ -39,7 +35,6 @@ extern "C" {
fn AXIsProcessTrustedWithOptions(options: CFDictionaryRef) -> BOOL; fn AXIsProcessTrustedWithOptions(options: CFDictionaryRef) -> BOOL;
fn InputMonitoringAuthStatus(_: BOOL) -> BOOL; fn InputMonitoringAuthStatus(_: BOOL) -> BOOL;
fn MacCheckAdminAuthorization() -> BOOL; fn MacCheckAdminAuthorization() -> BOOL;
fn Elevate(process: *const c_char, args: *const *const c_char) -> BOOL;
fn MacGetModeNum(display: u32, numModes: *mut u32) -> BOOL; fn MacGetModeNum(display: u32, numModes: *mut u32) -> BOOL;
fn MacGetModes( fn MacGetModes(
display: u32, display: u32,
@ -677,25 +672,28 @@ pub fn check_super_user_permission() -> ResultType<bool> {
unsafe { Ok(MacCheckAdminAuthorization() == YES) } unsafe { Ok(MacCheckAdminAuthorization() == YES) }
} }
pub fn elevate(args: Vec<&str>) -> ResultType<bool> { pub fn elevate(args: Vec<&str>, prompt: &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) => {
let cmd = CString::new(cmd)?; let mut cmd_with_args = cmd.to_string();
let mut cstring_args = Vec::new(); for arg in args {
for arg in args.iter() { cmd_with_args = format!("{} {}", cmd_with_args, arg);
cstring_args.push(CString::new(*arg)?);
} }
unsafe { let script = format!(
let args_ptr: *mut *const c_char = r#"do shell script "{}" with prompt "{}" with administrator privileges"#,
libc::malloc((cstring_args.len() + 1) * size_of::<*const c_char>()) as _; cmd_with_args, prompt
for i in 0..cstring_args.len() { );
*args_ptr.add(i) = cstring_args[i].as_ptr() as _; match std::process::Command::new("osascript")
.arg("-e")
.arg(script)
.arg(&get_active_username())
.status()
{
Err(e) => {
bail!("Failed to run osascript: {}", e);
} }
*args_ptr.add(cstring_args.len()) = std::ptr::null() as _; Ok(status) => Ok(status.success() && status.code() == Some(0)),
let r = Elevate(cmd.as_ptr() as _, args_ptr as _);
libc::free(args_ptr as _);
Ok(r == YES)
} }
} }
None => { None => {

View File

@ -173,7 +173,7 @@ fn elevate_install(
crate::platform::elevate(&args) crate::platform::elevate(&args)
} }
#[cfg(any(target_os = "linux", target_os = "macos"))] #[cfg(target_os = "linux")]
fn elevate_install( fn elevate_install(
plugin_id: &str, plugin_id: &str,
plugin_url: &str, plugin_url: &str,
@ -186,6 +186,19 @@ fn elevate_install(
crate::platform::elevate(args) crate::platform::elevate(args)
} }
#[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);
}
crate::platform::elevate(args, "RustDesk wants to install then plugin")
}
#[inline] #[inline]
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
fn elevate_uninstall(plugin_id: &str) -> ResultType<bool> { fn elevate_uninstall(plugin_id: &str) -> ResultType<bool> {
@ -193,11 +206,20 @@ fn elevate_uninstall(plugin_id: &str) -> ResultType<bool> {
} }
#[inline] #[inline]
#[cfg(any(target_os = "linux", target_os = "macos"))] #[cfg(target_os = "linux")]
fn elevate_install(plugin_id: &str) -> ResultType<bool> { fn elevate_uninstall(plugin_id: &str) -> ResultType<bool> {
crate::platform::elevate(vec!["--plugin-uninstall", plugin_id]) crate::platform::elevate(vec!["--plugin-uninstall", plugin_id])
} }
#[inline]
#[cfg(target_os = "macos")]
fn elevate_uninstall(plugin_id: &str) -> ResultType<bool> {
crate::platform::elevate(
vec!["--plugin-uninstall", plugin_id],
"RustDesk wants to uninstall the plugin",
)
}
pub fn install_plugin(id: &str) -> ResultType<()> { pub fn install_plugin(id: &str) -> ResultType<()> {
match PLUGIN_INFO.lock().unwrap().get(id) { match PLUGIN_INFO.lock().unwrap().get(id) {
Some(plugin) => { Some(plugin) => {