use std::fmt;
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Sid {
pub revision: u8,
pub identifier_authority: u64, pub sub_authorities: Vec<u32>,
}
impl Sid {
pub fn from_bytes(b: &[u8]) -> Option<Sid> {
if b.len() < 8 {
return None;
}
let revision = b[0];
let count = b[1] as usize;
let mut authority: u64 = 0;
for &byte in &b[2..8] {
authority = (authority << 8) | byte as u64; }
if b.len() < 8 + count * 4 {
return None;
}
let mut subs = Vec::with_capacity(count);
for i in 0..count {
let off = 8 + i * 4;
subs.push(u32::from_le_bytes([b[off], b[off + 1], b[off + 2], b[off + 3]]));
}
Some(Sid { revision, identifier_authority: authority, sub_authorities: subs })
}
pub fn parse(s: &str) -> Option<Sid> {
let mut it = s.split('-');
if it.next()? != "S" {
return None;
}
let revision = it.next()?.parse().ok()?;
let identifier_authority = it.next()?.parse().ok()?;
let sub_authorities = it.map(|p| p.parse().ok()).collect::<Option<Vec<u32>>>()?;
Some(Sid { revision, identifier_authority, sub_authorities })
}
pub fn rid(&self) -> Option<u32> {
self.sub_authorities.last().copied()
}
pub fn is_well_known(&self) -> bool {
matches!(self.identifier_authority, 1 | 3) || (self.identifier_authority == 5 && self.sub_authorities.first() == Some(&32))
}
}
impl fmt::Display for Sid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "S-{}-{}", self.revision, self.identifier_authority)?;
for s in &self.sub_authorities {
write!(f, "-{s}")?;
}
Ok(())
}
}
impl fmt::Debug for Sid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}
pub mod rid {
pub const ADMINISTRATOR: u32 = 500;
pub const GUEST: u32 = 501;
pub const KRBTGT: u32 = 502;
pub const DOMAIN_ADMINS: u32 = 512;
pub const DOMAIN_CONTROLLERS: u32 = 516;
pub const SCHEMA_ADMINS: u32 = 518;
pub const ENTERPRISE_ADMINS: u32 = 519;
pub const ADMINISTRATORS_BUILTIN: u32 = 544; }
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Guid(pub [u8; 16]);
impl Guid {
pub fn from_bytes(b: &[u8]) -> Option<Guid> {
Some(Guid(b.get(..16)?.try_into().ok()?))
}
pub fn parse(s: &str) -> Option<Guid> {
let s = s.trim_matches(|c| c == '{' || c == '}');
let hex: String = s.chars().filter(|c| *c != '-').collect();
if hex.len() != 32 {
return None;
}
let raw: Vec<u8> = (0..16)
.map(|i| u8::from_str_radix(&hex[i * 2..i * 2 + 2], 16).ok())
.collect::<Option<_>>()?;
let mut b = [0u8; 16];
b[0..4].copy_from_slice(&[raw[3], raw[2], raw[1], raw[0]]);
b[4..6].copy_from_slice(&[raw[5], raw[4]]);
b[6..8].copy_from_slice(&[raw[7], raw[6]]);
b[8..16].copy_from_slice(&raw[8..16]);
Some(Guid(b))
}
}
impl fmt::Display for Guid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let b = &self.0;
write!(
f,
"{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
b[3], b[2], b[1], b[0], b[5], b[4], b[7], b[6],
b[8], b[9], b[10], b[11], b[12], b[13], b[14], b[15]
)
}
}
impl fmt::Debug for Guid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}