msldap-ext 0.1.0-dev

Encoder/decoder for MS-ADTS LDAP extension controls (Paged Results, ExtendedDN, SDFlags, ShowDeleted, DirSync, VLV). Pure BER — no transport.
Documentation
//! SD_FLAGS control — 1.2.840.113556.1.4.801 (MS-ADTS §3.1.1.3.4.1.11).
//!
//! Selects which components of the nTSecurityDescriptor a search returns
//! (or a modify writes). Value is a SEQUENCE containing a single INTEGER
//! bitmask.
//!
//! ```asn1
//! SdFlagsRequestValue ::= SEQUENCE { flags INTEGER }
//! ```

use crate::ber;
use crate::Error;

pub const OID: &str = "1.2.840.113556.1.4.801";

pub const OWNER: u32 = 0x1;
pub const GROUP: u32 = 0x2;
pub const DACL: u32 = 0x4;
pub const SACL: u32 = 0x8;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SdFlags(pub u32);

impl SdFlags {
    pub const fn new(bits: u32) -> Self {
        Self(bits)
    }

    /// Owner + DACL — the common case for BloodHound-style ACL harvesting
    /// that avoids the SeSecurityPrivilege check needed for SACL.
    pub const fn owner_dacl() -> Self {
        Self(OWNER | DACL)
    }

    /// SACL only. Requires SeSecurityPrivilege on the DC session.
    pub const fn sacl() -> Self {
        Self(SACL)
    }

    pub const fn all() -> Self {
        Self(OWNER | GROUP | DACL | SACL)
    }

    pub fn encode(&self) -> Vec<u8> {
        let mut body = Vec::with_capacity(6);
        ber::write_u32(&mut body, self.0);
        ber::wrap_sequence(&body)
    }

    pub fn decode(buf: &[u8]) -> Result<Self, Error> {
        let (inner, _) = ber::read_sequence(buf)?;
        let (v, _) = ber::read_u32(inner)?;
        Ok(Self(v))
    }
}