#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SamAccountType {
DomainObject,
GroupObject,
NonSecurityGroupObject,
AliasObject,
NonSecurityAliasObject,
NormalUserAccount,
MachineAccount,
TrustAccount,
AppBasicGroup,
AppQueryGroup,
Unknown(u32),
}
impl SamAccountType {
pub fn from_bits(v: u32) -> Self {
match v {
0x0000_0000 => SamAccountType::DomainObject,
0x1000_0000 => SamAccountType::GroupObject,
0x1000_0001 => SamAccountType::NonSecurityGroupObject,
0x2000_0000 => SamAccountType::AliasObject,
0x2000_0001 => SamAccountType::NonSecurityAliasObject,
0x3000_0000 => SamAccountType::NormalUserAccount,
0x3000_0001 => SamAccountType::MachineAccount,
0x3000_0002 => SamAccountType::TrustAccount,
0x4000_0000 => SamAccountType::AppBasicGroup,
0x4000_0001 => SamAccountType::AppQueryGroup,
other => SamAccountType::Unknown(other),
}
}
pub fn has_password(self) -> bool {
matches!(
self,
SamAccountType::NormalUserAccount
| SamAccountType::MachineAccount
| SamAccountType::TrustAccount
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decode_common_values() {
assert_eq!(
SamAccountType::from_bits(0x3000_0000),
SamAccountType::NormalUserAccount
);
assert_eq!(
SamAccountType::from_bits(0x3000_0001),
SamAccountType::MachineAccount
);
assert_eq!(
SamAccountType::from_bits(0x1000_0000),
SamAccountType::GroupObject
);
}
#[test]
fn unknown_preserved() {
let v = 0xdead_beef;
assert_eq!(SamAccountType::from_bits(v), SamAccountType::Unknown(v));
}
#[test]
fn only_user_computer_trust_have_password() {
assert!(SamAccountType::NormalUserAccount.has_password());
assert!(SamAccountType::MachineAccount.has_password());
assert!(SamAccountType::TrustAccount.has_password());
assert!(!SamAccountType::GroupObject.has_password());
assert!(!SamAccountType::AliasObject.has_password());
}
}