From 20973b44b4919dbffad40c4db39ac367aaac17a3 Mon Sep 17 00:00:00 2001 From: AntwortEinesLebens Date: Sat, 19 Oct 2024 15:42:56 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=94=A8=20Rewrite=20"is=5Fadmi?= =?UTF-8?q?nistrator"=20to=20use=20"Owned"=20type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/windows/users.rs | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/windows/users.rs b/src/windows/users.rs index 272ae94..f7fa1c6 100644 --- a/src/windows/users.rs +++ b/src/windows/users.rs @@ -3,21 +3,19 @@ // SPDX-License-Identifier: GPL-3.0-or-later use windows::{ - core::Error as WindowsError, + core::{Owned, Result as WindowsResult}, Win32::{ - Foundation::BOOL, - Security::{ - AllocateAndInitializeSid, CheckTokenMembership, FreeSid, PSID, SECURITY_NT_AUTHORITY, - }, + Security::{AllocateAndInitializeSid, CheckTokenMembership, PSID, SECURITY_NT_AUTHORITY}, System::SystemServices::{DOMAIN_ALIAS_RID_ADMINS, SECURITY_BUILTIN_DOMAIN_RID}, }, }; -pub fn is_administrator() -> Result { - let is_admin: *mut BOOL = &mut BOOL::from(false); - let mut administrators_group: PSID = PSID::default(); +pub fn is_administrator() -> WindowsResult { + let mut is_admin: bool = false; unsafe { + let mut administrators_group: Owned = Owned::new(PSID::default()); + AllocateAndInitializeSid( &SECURITY_NT_AUTHORITY, 2, @@ -29,16 +27,15 @@ pub fn is_administrator() -> Result { 0, 0, 0, - &mut administrators_group, + &mut *administrators_group, )?; - let result: Result<(), WindowsError> = - CheckTokenMembership(None, administrators_group, is_admin); - - FreeSid(administrators_group); - - result?; + CheckTokenMembership( + None, + *administrators_group, + &mut is_admin as *mut _ as *mut _, + )?; } - Ok(unsafe { (*is_admin).into() }) + Ok(is_admin) }