@@ -52,8 +52,8 @@ pub mod identity {
52
52
use std:: borrow:: Cow ;
53
53
use std:: path:: Path ;
54
54
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 ( ) )
57
57
}
58
58
59
59
pub fn is_path_owned_by_current_user ( path : Cow < ' _ , Path > ) -> std:: io:: Result < bool > {
@@ -75,14 +75,15 @@ pub mod identity {
75
75
. map_err ( |_| err ( "Failed to open process token" ) ) ?;
76
76
77
77
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)
79
79
. as_bool ( )
80
80
{
81
- let mut token_user = Security :: TOKEN_USER :: default ( ) ;
81
+ let mut info_buf = Vec :: < u8 > :: new ( ) ;
82
+ info_buf. reserve_exact ( len as usize ) ;
82
83
if Security :: GetTokenInformation (
83
- & handle,
84
+ handle,
84
85
Security :: TokenUser ,
85
- & mut token_user as * mut _ as * mut std:: ffi:: c_void ,
86
+ info_buf . as_mut_ptr ( ) as * mut std:: ffi:: c_void ,
86
87
len,
87
88
& mut len,
88
89
)
@@ -91,38 +92,36 @@ pub mod identity {
91
92
// NOTE: we avoid to copy the sid or cache it in any way for now, even though it should be possible
92
93
// with a custom allocation/vec/box and it's just very raw. Can the `windows` crate do better?
93
94
// When/If yes, then let's improve this.
94
- if Security :: IsValidSid ( token_user. User . Sid ) . as_bool ( ) {
95
- use std:: os:: windows:: ffi:: OsStrExt ;
96
- 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);
95
+ // It should however be possible to create strings from SIDs, check this once more.
96
+ let info: * const Security :: TOKEN_USER = std:: mem:: transmute ( info_buf. as_ptr ( ) ) ;
97
+ if Security :: IsValidSid ( ( * info) . User . Sid ) . as_bool ( ) {
98
+ let wide_path = to_wide_path ( & path) ;
101
99
let mut path_sid = PSID :: default ( ) ;
102
100
let res = Security :: Authorization :: GetNamedSecurityInfoW (
103
- windows:: core:: PCWSTR ( wide_path. as_mut_ptr ( ) ) ,
101
+ windows:: core:: PCWSTR ( wide_path. as_ptr ( ) ) ,
104
102
SE_FILE_OBJECT ,
105
103
Security :: OWNER_SECURITY_INFORMATION | Security :: DACL_SECURITY_INFORMATION ,
106
- & mut path_sid,
104
+ & mut path_sid as * mut _ ,
107
105
std:: ptr:: null_mut ( ) ,
108
106
std:: ptr:: null_mut ( ) ,
109
107
std:: ptr:: null_mut ( ) ,
110
- & mut descriptor,
108
+ & mut descriptor as * mut _ ,
111
109
) ;
112
110
113
111
if res == ERROR_SUCCESS . 0 && Security :: IsValidSid ( path_sid) . as_bool ( ) {
114
- is_owned = Security :: EqualSid ( path_sid, token_user. User . Sid ) . as_bool ( ) ;
112
+ is_owned = Security :: EqualSid ( path_sid, ( * info) . User . Sid ) . as_bool ( ) ;
113
+ dbg ! ( is_owned, path. as_ref( ) ) ;
115
114
} else {
116
- err_msg = "couldn't get owner for path or it wasn't valid" . into ( ) ;
115
+ err_msg = format ! ( "couldn't get owner for path or it wasn't valid: {}" , res ) . into ( ) ;
117
116
}
118
117
} else {
119
- err_msg = "owner id of current process wasn't set or valid" . into ( ) ;
118
+ err_msg = String :: from ( "owner id of current process wasn't set or valid" ) . into ( ) ;
120
119
}
121
120
} else {
122
- err_msg = "Could not get information about the token user" . into ( ) ;
121
+ err_msg = String :: from ( "Could not get information about the token user" ) . into ( ) ;
123
122
}
124
123
} else {
125
- err_msg = "Could not get token information for length of token user" . into ( ) ;
124
+ err_msg = String :: from ( "Could not get token information for length of token user" ) . into ( ) ;
126
125
}
127
126
CloseHandle ( handle) ;
128
127
if !descriptor. is_invalid ( ) {
@@ -131,5 +130,12 @@ pub mod identity {
131
130
}
132
131
err_msg. map ( |msg| Err ( err ( msg) ) ) . unwrap_or ( Ok ( is_owned) )
133
132
}
133
+
134
+ fn to_wide_path ( path : & Path ) -> Vec < u16 > {
135
+ use std:: os:: windows:: ffi:: OsStrExt ;
136
+ let mut wide_path: Vec < _ > = path. as_os_str ( ) . encode_wide ( ) . collect ( ) ;
137
+ wide_path. push ( 0 ) ;
138
+ wide_path
139
+ }
134
140
}
135
141
}
0 commit comments