Skip to main content

ntds_parse/
sam.rs

1//! Decode of `sAMAccountType` — the numeric object-class field.
2//!
3//! Values verified against MS-ADTS §2.2.20 "sAMAccountType Attribute".
4
5/// Symbolic decode of `sAMAccountType`.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum SamAccountType {
9    /// `SAM_DOMAIN_OBJECT` (0x0000_0000).
10    DomainObject,
11    /// `SAM_GROUP_OBJECT` (0x1000_0000) — a global security group.
12    GroupObject,
13    /// `SAM_NON_SECURITY_GROUP_OBJECT` (0x1000_0001) — a distribution group.
14    NonSecurityGroupObject,
15    /// `SAM_ALIAS_OBJECT` (0x2000_0000) — a domain-local security group.
16    AliasObject,
17    /// `SAM_NON_SECURITY_ALIAS_OBJECT` (0x2000_0001).
18    NonSecurityAliasObject,
19    /// `SAM_USER_OBJECT` / `SAM_NORMAL_USER_ACCOUNT` (0x3000_0000) — a regular user.
20    NormalUserAccount,
21    /// `SAM_MACHINE_ACCOUNT` (0x3000_0001) — a computer account.
22    MachineAccount,
23    /// `SAM_TRUST_ACCOUNT` (0x3000_0002) — an inter-domain trust account.
24    TrustAccount,
25    /// `SAM_APP_BASIC_GROUP` (0x4000_0000).
26    AppBasicGroup,
27    /// `SAM_APP_QUERY_GROUP` (0x4000_0001).
28    AppQueryGroup,
29    /// An unknown value (with the original bits preserved).
30    Unknown(u32),
31}
32
33impl SamAccountType {
34    /// Decode a raw `sAMAccountType` value.
35    pub fn from_bits(v: u32) -> Self {
36        match v {
37            0x0000_0000 => SamAccountType::DomainObject,
38            0x1000_0000 => SamAccountType::GroupObject,
39            0x1000_0001 => SamAccountType::NonSecurityGroupObject,
40            0x2000_0000 => SamAccountType::AliasObject,
41            0x2000_0001 => SamAccountType::NonSecurityAliasObject,
42            0x3000_0000 => SamAccountType::NormalUserAccount,
43            0x3000_0001 => SamAccountType::MachineAccount,
44            0x3000_0002 => SamAccountType::TrustAccount,
45            0x4000_0000 => SamAccountType::AppBasicGroup,
46            0x4000_0001 => SamAccountType::AppQueryGroup,
47            other => SamAccountType::Unknown(other),
48        }
49    }
50
51    /// True if this object type carries a password (users, computers, trusts).
52    pub fn has_password(self) -> bool {
53        matches!(
54            self,
55            SamAccountType::NormalUserAccount
56                | SamAccountType::MachineAccount
57                | SamAccountType::TrustAccount
58        )
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn decode_common_values() {
68        assert_eq!(
69            SamAccountType::from_bits(0x3000_0000),
70            SamAccountType::NormalUserAccount
71        );
72        assert_eq!(
73            SamAccountType::from_bits(0x3000_0001),
74            SamAccountType::MachineAccount
75        );
76        assert_eq!(
77            SamAccountType::from_bits(0x1000_0000),
78            SamAccountType::GroupObject
79        );
80    }
81
82    #[test]
83    fn unknown_preserved() {
84        let v = 0xdead_beef;
85        assert_eq!(SamAccountType::from_bits(v), SamAccountType::Unknown(v));
86    }
87
88    #[test]
89    fn only_user_computer_trust_have_password() {
90        assert!(SamAccountType::NormalUserAccount.has_password());
91        assert!(SamAccountType::MachineAccount.has_password());
92        assert!(SamAccountType::TrustAccount.has_password());
93        assert!(!SamAccountType::GroupObject.has_password());
94        assert!(!SamAccountType::AliasObject.has_password());
95    }
96}