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
//! DirSync control — 1.2.840.113556.1.4.841 (MS-ADTS §3.1.1.3.4.1.3).
//!
//! ```asn1
//! DirSyncRequestValue ::= SEQUENCE {
//!     Flags               INTEGER,
//!     MaxAttributeCount   INTEGER,
//!     Cookie              OCTET STRING
//! }
//! ```
//! Response uses the paired OID 1.2.840.113556.1.4.842 with an identical
//! shape; we decode both here.

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

pub const OID_REQUEST: &str = "1.2.840.113556.1.4.841";
pub const OID_RESPONSE: &str = "1.2.840.113556.1.4.842";

// DirSync request flag bits (MS-ADTS §3.1.1.3.4.1.3).
pub const OBJECT_SECURITY: u32 = 0x1;
pub const ANCESTORS_FIRST_ORDER: u32 = 0x800;
pub const PUBLIC_DATA_ONLY: u32 = 0x2000;
pub const INCREMENTAL_VALUES: u32 = 0x80000000;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DirSyncControl {
    pub flags: u32,
    pub max_attribute_count: u32,
    pub cookie: Vec<u8>,
}

impl DirSyncControl {
    /// Initial request with empty cookie.
    pub fn initial(flags: u32, max_attribute_count: u32) -> Self {
        Self {
            flags,
            max_attribute_count,
            cookie: Vec::new(),
        }
    }

    pub fn encode(&self) -> Vec<u8> {
        let mut body = Vec::with_capacity(16);
        ber::write_u32(&mut body, self.flags);
        ber::write_u32(&mut body, self.max_attribute_count);
        ber::write_octet_string(&mut body, &self.cookie);
        ber::wrap_sequence(&body)
    }

    pub fn decode(buf: &[u8]) -> Result<Self, Error> {
        let (inner, _) = ber::read_sequence(buf)?;
        let (flags, c1) = ber::read_u32(inner)?;
        let (max_attribute_count, c2) = ber::read_u32(&inner[c1..])?;
        let (cookie, _) = ber::read_octet_string(&inner[c1 + c2..])?;
        Ok(Self {
            flags,
            max_attribute_count,
            cookie: cookie.to_vec(),
        })
    }
}