Skip to main content

msldap_ext/controls/
dirsync.rs

1//! DirSync control — 1.2.840.113556.1.4.841 (MS-ADTS §3.1.1.3.4.1.3).
2//!
3//! ```asn1
4//! DirSyncRequestValue ::= SEQUENCE {
5//!     Flags               INTEGER,
6//!     MaxAttributeCount   INTEGER,
7//!     Cookie              OCTET STRING
8//! }
9//! ```
10//! Response uses the paired OID 1.2.840.113556.1.4.842 with an identical
11//! shape; we decode both here.
12
13use crate::ber;
14use crate::Error;
15
16pub const OID_REQUEST: &str = "1.2.840.113556.1.4.841";
17pub const OID_RESPONSE: &str = "1.2.840.113556.1.4.842";
18
19// DirSync request flag bits (MS-ADTS §3.1.1.3.4.1.3).
20pub const OBJECT_SECURITY: u32 = 0x1;
21pub const ANCESTORS_FIRST_ORDER: u32 = 0x800;
22pub const PUBLIC_DATA_ONLY: u32 = 0x2000;
23pub const INCREMENTAL_VALUES: u32 = 0x80000000;
24
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct DirSyncControl {
27    pub flags: u32,
28    pub max_attribute_count: u32,
29    pub cookie: Vec<u8>,
30}
31
32impl DirSyncControl {
33    /// Initial request with empty cookie.
34    pub fn initial(flags: u32, max_attribute_count: u32) -> Self {
35        Self {
36            flags,
37            max_attribute_count,
38            cookie: Vec::new(),
39        }
40    }
41
42    pub fn encode(&self) -> Vec<u8> {
43        let mut body = Vec::with_capacity(16);
44        ber::write_u32(&mut body, self.flags);
45        ber::write_u32(&mut body, self.max_attribute_count);
46        ber::write_octet_string(&mut body, &self.cookie);
47        ber::wrap_sequence(&body)
48    }
49
50    pub fn decode(buf: &[u8]) -> Result<Self, Error> {
51        let (inner, _) = ber::read_sequence(buf)?;
52        let (flags, c1) = ber::read_u32(inner)?;
53        let (max_attribute_count, c2) = ber::read_u32(&inner[c1..])?;
54        let (cookie, _) = ber::read_octet_string(&inner[c1 + c2..])?;
55        Ok(Self {
56            flags,
57            max_attribute_count,
58            cookie: cookie.to_vec(),
59        })
60    }
61}