Skip to main content

smb_server/proto/messages/
durable.rs

1//! Durable handle create contexts (MS-SMB2 2.2.13.2.3, 2.2.13.2.4,
2//! 2.2.13.2.11, 2.2.13.2.12, 2.2.14.2.3, 2.2.14.2.12).
3
4use crate::proto::error::{ProtoError, ProtoResult};
5
6use super::create::FileId;
7
8/// SMB2_DHANDLE_FLAG_PERSISTENT.
9pub const DHANDLE_FLAG_PERSISTENT: u32 = 0x0000_0002;
10
11/// Data of a `DHnQ` request context.
12pub const DURABLE_REQUEST_LEN: usize = 16;
13/// Data of a `DHnC` context.
14pub const DURABLE_RECONNECT_LEN: usize = 16;
15/// Data of a `DH2Q` request context.
16pub const DURABLE_REQUEST_V2_LEN: usize = 32;
17/// Data of a `DH2C` context.
18pub const DURABLE_RECONNECT_V2_LEN: usize = 36;
19
20fn u32_at(data: &[u8], at: usize) -> u32 {
21    u32::from_le_bytes(data[at..at + 4].try_into().expect("4 bytes"))
22}
23
24fn u64_at(data: &[u8], at: usize) -> u64 {
25    u64::from_le_bytes(data[at..at + 8].try_into().expect("8 bytes"))
26}
27
28fn file_id_at(data: &[u8], at: usize) -> FileId {
29    FileId::new(u64_at(data, at), u64_at(data, at + 8))
30}
31
32fn check_len(data: &[u8], len: usize, what: &'static str) -> ProtoResult<()> {
33    if data.len() == len {
34        Ok(())
35    } else {
36        Err(ProtoError::Malformed(what))
37    }
38}
39
40/// SMB2_CREATE_DURABLE_HANDLE_REQUEST, the data of a `DHnQ` request
41/// context: 16 reserved bytes.
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
43pub struct DurableRequest;
44
45impl DurableRequest {
46    pub fn parse(data: &[u8]) -> ProtoResult<Self> {
47        check_len(data, DURABLE_REQUEST_LEN, "durable request size")?;
48        Ok(Self)
49    }
50}
51
52/// SMB2_CREATE_DURABLE_HANDLE_RECONNECT, the data of a `DHnC` context.
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
54pub struct DurableReconnect {
55    pub file_id: FileId,
56}
57
58impl DurableReconnect {
59    pub fn parse(data: &[u8]) -> ProtoResult<Self> {
60        check_len(data, DURABLE_RECONNECT_LEN, "durable reconnect size")?;
61        Ok(Self {
62            file_id: file_id_at(data, 0),
63        })
64    }
65}
66
67/// SMB2_CREATE_DURABLE_HANDLE_REQUEST_V2, the data of a `DH2Q` context.
68/// The CreateGuid is kept as the 16 bytes on the wire.
69#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
70pub struct DurableRequestV2 {
71    pub timeout_ms: u32,
72    pub flags: u32,
73    pub create_guid: [u8; 16],
74}
75
76impl DurableRequestV2 {
77    pub fn parse(data: &[u8]) -> ProtoResult<Self> {
78        check_len(data, DURABLE_REQUEST_V2_LEN, "durable v2 request size")?;
79        Ok(Self {
80            timeout_ms: u32_at(data, 0),
81            flags: u32_at(data, 4),
82            create_guid: data[16..32].try_into().expect("16 bytes"),
83        })
84    }
85}
86
87/// SMB2_CREATE_DURABLE_HANDLE_RECONNECT_V2, the data of a `DH2C` context.
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
89pub struct DurableReconnectV2 {
90    pub file_id: FileId,
91    pub create_guid: [u8; 16],
92    pub flags: u32,
93}
94
95impl DurableReconnectV2 {
96    pub fn parse(data: &[u8]) -> ProtoResult<Self> {
97        check_len(data, DURABLE_RECONNECT_V2_LEN, "durable v2 reconnect size")?;
98        Ok(Self {
99            file_id: file_id_at(data, 0),
100            create_guid: data[16..32].try_into().expect("16 bytes"),
101            flags: u32_at(data, 32),
102        })
103    }
104}
105
106/// SMB2_CREATE_DURABLE_HANDLE_RESPONSE: 8 reserved bytes.
107#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
108pub struct DurableResponse;
109
110impl DurableResponse {
111    pub fn encode(&self) -> Vec<u8> {
112        vec![0; 8]
113    }
114}
115
116/// SMB2_CREATE_DURABLE_HANDLE_RESPONSE_V2.
117#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
118pub struct DurableResponseV2 {
119    pub timeout_ms: u32,
120    pub flags: u32,
121}
122
123impl DurableResponseV2 {
124    pub fn encode(&self) -> Vec<u8> {
125        let mut out = Vec::with_capacity(8);
126        out.extend_from_slice(&self.timeout_ms.to_le_bytes());
127        out.extend_from_slice(&self.flags.to_le_bytes());
128        out
129    }
130}
131
132#[cfg(test)]
133mod tests {
134    use super::*;
135
136    #[test]
137    fn each_context_has_one_size() {
138        for len in [0, 15, 16, 17, 31, 32, 33, 35, 36, 37] {
139            let data = vec![0; len];
140            assert_eq!(DurableRequest::parse(&data).is_ok(), len == 16, "{len}");
141            assert_eq!(DurableReconnect::parse(&data).is_ok(), len == 16, "{len}");
142            assert_eq!(DurableRequestV2::parse(&data).is_ok(), len == 32, "{len}");
143            assert_eq!(DurableReconnectV2::parse(&data).is_ok(), len == 36, "{len}");
144        }
145    }
146
147    #[test]
148    fn fields_sit_at_their_offsets() {
149        let data: Vec<u8> = (0..36).collect();
150        let id = FileId::new(
151            u64::from_le_bytes([0, 1, 2, 3, 4, 5, 6, 7]),
152            u64::from_le_bytes([8, 9, 10, 11, 12, 13, 14, 15]),
153        );
154        let guid: [u8; 16] = (16..32).collect::<Vec<u8>>().try_into().unwrap();
155
156        assert_eq!(DurableReconnect::parse(&data[..16]).unwrap().file_id, id);
157        let v2 = DurableRequestV2::parse(&data[..32]).unwrap();
158        assert_eq!(v2.timeout_ms, u32::from_le_bytes([0, 1, 2, 3]));
159        assert_eq!(v2.flags, u32::from_le_bytes([4, 5, 6, 7]));
160        assert_eq!(v2.create_guid, guid);
161        let reconnect = DurableReconnectV2::parse(&data).unwrap();
162        assert_eq!(reconnect.file_id, id);
163        assert_eq!(reconnect.create_guid, guid);
164        assert_eq!(reconnect.flags, u32::from_le_bytes([32, 33, 34, 35]));
165    }
166
167    #[test]
168    fn responses_have_their_fixed_layouts() {
169        assert_eq!(DurableResponse.encode(), [0; 8]);
170        let v2 = DurableResponseV2 {
171            timeout_ms: 120_000,
172            flags: DHANDLE_FLAG_PERSISTENT,
173        };
174        assert_eq!(v2.encode(), [0xC0, 0xD4, 0x01, 0x00, 2, 0, 0, 0]);
175    }
176}