Skip to main content

smb_server/proto/messages/
tree_connect.rs

1//! TREE_CONNECT Request/Response (MS-SMB2 §2.2.9 / §2.2.10).
2
3use binrw::{BinRead, BinWrite, binrw};
4use std::io::Cursor;
5
6use crate::proto::error::ProtoResult;
7
8/// SMB2_TREE_CONNECT_REQUEST (MS-SMB2 §2.2.9).
9///
10/// `path` is UTF-16LE. The wire format gives `PathOffset` (from the start of
11/// the SMB2 header) and `PathLength`; we encode/decode the path immediately
12/// following the fixed prefix. The 3.1.1 tree-connect-context machinery
13/// (extension `flags`, `path_offset`/`path_length` interpretation) is
14/// preserved on the wire and the server crate inspects `flags` if needed.
15#[binrw]
16#[brw(little)]
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct TreeConnectRequest {
19    pub structure_size: u16,
20    /// 3.1.1: flags. 2.x/3.0/3.0.2: reserved.
21    pub flags: u16,
22    pub path_offset: u16,
23    pub path_length: u16,
24    /// UTF-16LE share path bytes (e.g. `\\server\share`).
25    #[br(count = path_length as usize)]
26    pub path: Vec<u8>,
27}
28
29impl TreeConnectRequest {
30    /// Flag: SMB2_TREE_CONNECT_FLAG_CLUSTER_RECONNECT (3.1.1).
31    pub const FLAG_CLUSTER_RECONNECT: u16 = 0x0001;
32    /// Flag: SMB2_TREE_CONNECT_FLAG_REDIRECT_TO_OWNER (3.1.1).
33    pub const FLAG_REDIRECT_TO_OWNER: u16 = 0x0002;
34    /// Flag: SMB2_TREE_CONNECT_FLAG_EXTENSION_PRESENT (3.1.1).
35    pub const FLAG_EXTENSION_PRESENT: u16 = 0x0004;
36
37    /// Decode the UTF-16LE share path into a `String`. Returns `None` if the
38    /// stored bytes are not an even length (malformed UTF-16LE).
39    pub fn path_str(&self) -> Option<String> {
40        if !self.path.len().is_multiple_of(2) {
41            return None;
42        }
43        let units: Vec<u16> = self
44            .path
45            .chunks_exact(2)
46            .map(|c| u16::from_le_bytes([c[0], c[1]]))
47            .collect();
48        Some(String::from_utf16_lossy(&units))
49    }
50
51    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
52        Ok(Self::read(&mut Cursor::new(buf))?)
53    }
54    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
55        let mut c = Cursor::new(Vec::new());
56        BinWrite::write(self, &mut c)?;
57        out.extend_from_slice(&c.into_inner());
58        Ok(())
59    }
60}
61
62/// SMB2_TREE_CONNECT_RESPONSE (MS-SMB2 §2.2.10).
63#[binrw]
64#[brw(little)]
65#[derive(Debug, Clone, PartialEq, Eq)]
66pub struct TreeConnectResponse {
67    pub structure_size: u16,
68    pub share_type: u8,
69    pub reserved: u8,
70    pub share_flags: u32,
71    pub capabilities: u32,
72    pub maximal_access: u32,
73}
74
75impl TreeConnectResponse {
76    /// Share type: SMB2_SHARE_TYPE_DISK.
77    pub const SHARE_TYPE_DISK: u8 = 0x01;
78    pub const SHARE_TYPE_PIPE: u8 = 0x02;
79    pub const SHARE_TYPE_PRINT: u8 = 0x03;
80
81    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
82        Ok(Self::read(&mut Cursor::new(buf))?)
83    }
84    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
85        let mut c = Cursor::new(Vec::new());
86        BinWrite::write(self, &mut c)?;
87        out.extend_from_slice(&c.into_inner());
88        Ok(())
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    fn utf16le(s: &str) -> Vec<u8> {
97        s.encode_utf16().flat_map(u16::to_le_bytes).collect()
98    }
99
100    #[test]
101    fn request_round_trips() {
102        let path = utf16le(r"\\server\share");
103        let r = TreeConnectRequest {
104            structure_size: 9,
105            flags: 0,
106            path_offset: 0x48,
107            path_length: path.len() as u16,
108            path,
109        };
110        let mut buf = Vec::new();
111        r.write_to(&mut buf).unwrap();
112        let decoded = TreeConnectRequest::parse(&buf).unwrap();
113        assert_eq!(decoded, r);
114        assert_eq!(decoded.path_str().unwrap(), r"\\server\share");
115    }
116
117    #[test]
118    fn response_round_trips() {
119        let r = TreeConnectResponse {
120            structure_size: 16,
121            share_type: TreeConnectResponse::SHARE_TYPE_DISK,
122            reserved: 0,
123            share_flags: 0,
124            capabilities: 0,
125            maximal_access: 0x001F_01FF,
126        };
127        let mut buf = Vec::new();
128        r.write_to(&mut buf).unwrap();
129        assert_eq!(TreeConnectResponse::parse(&buf).unwrap(), r);
130    }
131}