1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use crate::{wrappers, LocalBox};
use std::fmt;
use std::hash::Hash;
use std::io;
use std::str::FromStr;
/// A SID (Security Identifier) that can be used with Windows API calls.
#[repr(C)]
pub struct Sid {
_opaque: [u8; 0],
}
impl Sid {
/// Create a new SID from raw parts
///
/// ```
/// use windows_permissions::Sid;
///
/// let sid_8 = Sid::new([1, 2, 3, 4, 5, 6], &[1, 2, 3, 4, 5, 6, 7, 8]).unwrap();
///
/// assert_eq!(sid_8.id_authority(), &[1, 2, 3, 4, 5, 6]);
/// assert_eq!(sid_8.sub_authority_count(), 8);
/// assert_eq!(sid_8.sub_authorities(), &[1, 2, 3, 4, 5, 6, 7, 8]);
/// ```
///
/// No more than 8 sub-authorities can be made using this function. If more
/// are needed, you can parse SDDL or use a wrapper function directly.
///
/// ```
/// use windows_permissions::Sid;
///
/// assert!(Sid::new([1, 2, 3, 4, 5, 6], &[1, 2, 3, 4, 5, 6, 7, 8]).is_ok());
/// assert!(Sid::new([1, 2, 3, 4, 5, 6], &[1, 2, 3, 4, 5, 6, 7, 8, 9]).is_err());
/// ```
///
pub fn new(id_auth: [u8; 6], sub_auths: &[u32]) -> io::Result<LocalBox<Sid>> {
wrappers::AllocateAndInitializeSid(id_auth, sub_auths)
}
/// Create a new well-known SID
///
/// This is equivalent to calling [`wrappers::CreateWellKnownSid`] with
/// `None` as the domain.
///
/// ```
/// use windows_permissions::{Sid, LocalBox};
/// use winapi::um::winnt::WinWorldSid;
///
/// let win_world_sid = Sid::well_known_sid(WinWorldSid).unwrap();
/// let another_sid = "S-1-1-0".parse().unwrap();
///
/// assert_eq!(win_world_sid, another_sid);
/// ```
pub fn well_known_sid(well_known_sid_type: u32) -> io::Result<LocalBox<Sid>> {
wrappers::CreateWellKnownSid(well_known_sid_type, None)
}
/// Get the number of sub-authorities in the SID
///
/// ```
/// use windows_permissions::{Sid, LocalBox};
///
/// let sid1: LocalBox<Sid> = "S-1-5-1".parse().unwrap();
/// let sid2: LocalBox<Sid> = "S-1-5-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15".parse().unwrap();
///
/// assert_eq!(sid1.sub_authority_count(), 1);
/// assert_eq!(sid2.sub_authority_count(), 15);
/// ```
pub fn sub_authority_count(&self) -> u8 {
wrappers::GetSidSubAuthorityCount(self)
}
/// Get the ID authority of the SID
///
/// ```
/// use windows_permissions::{Sid, LocalBox};
///
/// let sid1: LocalBox<Sid> = "S-1-5-12-62341".parse().unwrap();
/// let sid2: LocalBox<Sid> = "S-1-211111900160837-1".parse().unwrap();
///
/// assert_eq!(sid1.id_authority(), &[0, 0, 0, 0, 0, 5]);
/// assert_eq!(sid2.id_authority(), &[0xC0, 0x01, 0x51, 0xD1, 0x23, 0x45]);
/// ```
pub fn id_authority(&self) -> &[u8; 6] {
wrappers::GetSidIdentifierAuthority(self)
}
/// Get a sub-authority of the SID if it is available
///
/// Returns `None` if the SID has too few sub-authorities.
///
/// ```
/// use windows_permissions::{Sid, LocalBox};
///
/// let sid: LocalBox<Sid> = "S-1-5-12-62341".parse().unwrap();
///
/// assert_eq!(sid.sub_authority(0), Some(12));
/// assert_eq!(sid.sub_authority(1), Some(62341));
/// assert_eq!(sid.sub_authority(2), None);
/// ```
pub fn sub_authority(&self, index: u8) -> Option<u32> {
wrappers::GetSidSubAuthorityChecked(self, index)
}
/// Generate a list of the sub-authorities in the SID
///
/// Changes in the returned `Vec` are not reflected in the SID
pub fn sub_authorities(&self) -> Vec<u32> {
let mut vec = Vec::with_capacity(self.sub_authority_count() as usize);
for index in 0..self.sub_authority_count() {
vec.push(self.sub_authority(index).expect("Already checked count"));
}
vec
}
/// Get the numeric value of an ID authority
///
/// ```
/// use windows_permissions::Sid;
///
/// assert_eq!(
/// Sid::id_auth_to_number([0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC]),
/// 0x123456789ABCu64
/// );
/// ```
pub fn id_auth_to_number(id_auth: [u8; 6]) -> u64 {
id_auth[5] as u64
| (id_auth[4] as u64) << 8
| (id_auth[3] as u64) << 16
| (id_auth[2] as u64) << 24
| (id_auth[1] as u64) << 32
| (id_auth[0] as u64) << 40
}
}
#[cfg(test)]
impl Sid {
/// Return an iterator that yields a whole bunch of SIDs you can test
/// against, along with the things that got fed into `Sid::new` for each
///
/// Only built on `cfg(test)`.
pub fn test_sids() -> impl Iterator<Item = (LocalBox<Sid>, [u8; 6], &'static [u32])> {
extern crate itertools;
use itertools::Itertools;
const ID_AUTHS: &[[u8; 6]] = &[
[0, 0, 0, 0, 0, 0],
[0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
[0xba, 0xd5, 0x1d, 0xba, 0xd5, 0x1d],
[0xc0, 0x00, 0x15, 0x1d, 0xab, 0xcd],
];
const SUB_AUTHS: &[[u32; 8]] = &[
[0, 0, 0, 0, 0, 0, 0, 0],
[
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff,
0xffffffff,
],
[1, 2, 3, 4, 5, 6, 7, 8],
];
ID_AUTHS
.iter()
.cartesian_product(SUB_AUTHS)
.cartesian_product(1..=8)
.map(|((id, sa), sa_len)| {
let chopped_sa = &sa[..sa_len];
(
Sid::new(id.clone(), chopped_sa).unwrap(),
id.clone(),
chopped_sa,
)
})
}
}
impl fmt::Debug for Sid {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_map()
.entry(&"string_sid", &self.to_string())
.entry(&"id_auth", &self.id_authority())
.entry(&"sub_auth_count", &self.sub_authority_count())
.entry(&"sub_auths", &self.sub_authorities())
.finish()
}
}
impl fmt::Display for Sid {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(
fmt,
"{}",
wrappers::ConvertSidToStringSid(&self)
.expect("Passed a safe Sid to ConvertSidToStringSid but got an error")
.to_string_lossy()
)
}
}
impl Hash for Sid {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id_authority().hash(state);
for index in 0..self.sub_authority_count() {
self.sub_authority(index)
.expect("Already checked count")
.hash(state);
}
}
}
impl FromStr for LocalBox<Sid> {
type Err = io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
wrappers::ConvertStringSidToSid(s)
}
}
impl Eq for Sid {}
impl PartialEq for Sid {
fn eq(&self, other: &Sid) -> bool {
wrappers::EqualSid(self, other)
}
}
impl Clone for LocalBox<Sid> {
fn clone(&self) -> Self {
wrappers::CopySid(self)
// internally, CopySid is just memmove with a length check.
// This cannot panic unless allocation fails.
.expect("Failed to clone SID")
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn create_and_read_sids() {
for (sid, id_auth, sub_auths) in Sid::test_sids() {
assert_eq!(*sid.id_authority(), id_auth);
assert_eq!(sid.sub_authority_count() as usize, sub_auths.len());
for i in 0..sub_auths.len() {
assert_eq!(sid.sub_authority(i as u8), Some(sub_auths[i]));
}
}
}
}