From 4c213a238af15b9d5198c64abe1e702d3f40896d Mon Sep 17 00:00:00 2001 From: Carbonari Date: Fri, 17 Mar 2023 12:36:35 +0100 Subject: [PATCH] Allow Active Directory credentials for elevation --- src/platform/windows.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/platform/windows.rs b/src/platform/windows.rs index 696a18ab9..e27339865 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -1762,9 +1762,15 @@ pub fn send_message_to_hnwd( } pub fn create_process_with_logon(user: &str, pwd: &str, exe: &str, arg: &str) -> ResultType<()> { + let last_error_table = HashMap::from([ + (ERROR_LOGON_FAILURE, "The user name or password is incorrect."), + (ERROR_ACCESS_DENIED, "Access is denied.") + ]); + unsafe { - let wuser = wide_string(user); - let wpc = wide_string(""); + let user_split = user.split("\\").collect::>(); + let wuser = wide_string(user_split.get(1).unwrap_or(&user)); + let wpc = wide_string(user_split.get(0).unwrap_or(&"")); let wpwd = wide_string(pwd); let cmd = if arg.is_empty() { format!("\"{}\"", exe) @@ -1794,7 +1800,14 @@ pub fn create_process_with_logon(user: &str, pwd: &str, exe: &str, arg: &str) -> &mut pi as *mut PROCESS_INFORMATION, ) { - bail!("CreateProcessWithLogonW failed, errno={}", GetLastError()); + let last_error = GetLastError(); + bail!( + "CreateProcessWithLogonW failed : \"{}\", errno={}", + last_error_table + .get(&last_error) + .unwrap_or(&"Unknown error"), + last_error + ); } } return Ok(());