Skip to content

Commit e91450b

Browse files
committed
see if this makes a difference on windows (#386)
1 parent d6c6ec6 commit e91450b

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

git-sec/src/lib.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ pub mod identity {
5252
use std::borrow::Cow;
5353
use std::path::Path;
5454

55-
fn err(msg: &str) -> std::io::Error {
56-
std::io::Error::new(std::io::ErrorKind::Other, msg)
55+
fn err(msg: impl Into<String>) -> std::io::Error {
56+
std::io::Error::new(std::io::ErrorKind::Other, msg.into())
5757
}
5858

5959
pub fn is_path_owned_by_current_user(path: Cow<'_, Path>) -> std::io::Result<bool> {
@@ -75,14 +75,14 @@ pub mod identity {
7575
.map_err(|_| err("Failed to open process token"))?;
7676

7777
let mut len = 0_u32;
78-
if Security::GetTokenInformation(&handle, Security::TokenUser, std::ptr::null_mut(), 0, &mut len)
78+
if !Security::GetTokenInformation(handle, Security::TokenUser, std::ptr::null_mut(), 0, &mut len)
7979
.as_bool()
8080
{
81-
let mut token_user = Security::TOKEN_USER::default();
81+
let mut info = Security::TOKEN_USER::default();
8282
if Security::GetTokenInformation(
83-
&handle,
83+
handle,
8484
Security::TokenUser,
85-
&mut token_user as *mut _ as *mut std::ffi::c_void,
85+
&mut info as *mut _ as *mut std::ffi::c_void,
8686
len,
8787
&mut len,
8888
)
@@ -91,16 +91,13 @@ pub mod identity {
9191
// NOTE: we avoid to copy the sid or cache it in any way for now, even though it should be possible
9292
// with a custom allocation/vec/box and it's just very raw. Can the `windows` crate do better?
9393
// When/If yes, then let's improve this.
94-
if Security::IsValidSid(token_user.User.Sid).as_bool() {
94+
if Security::IsValidSid(info.User.Sid).as_bool() {
9595
use std::os::windows::ffi::OsStrExt;
9696
let mut wide_path: Vec<_> = path.as_ref().as_os_str().encode_wide().collect();
97-
// err = GetNamedSecurityInfoW(wpath, SE_FILE_OBJECT,
98-
// OWNER_SECURITY_INFORMATION |
99-
// DACL_SECURITY_INFORMATION,
100-
// &sid, NULL, NULL, NULL, &descriptor);
97+
wide_path.push(0);
10198
let mut path_sid = PSID::default();
10299
let res = Security::Authorization::GetNamedSecurityInfoW(
103-
windows::core::PCWSTR(wide_path.as_mut_ptr()),
100+
windows::core::PCWSTR(wide_path.as_ptr()),
104101
SE_FILE_OBJECT,
105102
Security::OWNER_SECURITY_INFORMATION | Security::DACL_SECURITY_INFORMATION,
106103
&mut path_sid,
@@ -111,18 +108,19 @@ pub mod identity {
111108
);
112109

113110
if res == ERROR_SUCCESS.0 && Security::IsValidSid(path_sid).as_bool() {
114-
is_owned = Security::EqualSid(path_sid, token_user.User.Sid).as_bool();
111+
is_owned = Security::EqualSid(path_sid, info.User.Sid).as_bool();
112+
dbg!(is_owned, path.as_ref());
115113
} else {
116-
err_msg = "couldn't get owner for path or it wasn't valid".into();
114+
err_msg = format!("couldn't get owner for path or it wasn't valid: {}", res).into();
117115
}
118116
} else {
119-
err_msg = "owner id of current process wasn't set or valid".into();
117+
err_msg = String::from("owner id of current process wasn't set or valid").into();
120118
}
121119
} else {
122-
err_msg = "Could not get information about the token user".into();
120+
err_msg = String::from("Could not get information about the token user").into();
123121
}
124122
} else {
125-
err_msg = "Could not get token information for length of token user".into();
123+
err_msg = String::from("Could not get token information for length of token user").into();
126124
}
127125
CloseHandle(handle);
128126
if !descriptor.is_invalid() {

0 commit comments

Comments
 (0)