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