Skip to main content

smb_server/proto/messages/
aapl.rs

1//! Apple's `AAPL` create context, which macOS clients send to find out
2//! whether the server has Apple's SMB2 extensions. The layout is the one
3//! Apple's SMB client and Samba use. All integers are little-endian.
4
5use crate::proto::error::{ProtoError, ProtoResult};
6
7/// CommandCode of a server query.
8pub const AAPL_SERVER_QUERY: u32 = 1;
9
10/// RequestBitmap and ReplyBitmap: the reply carries the server caps.
11pub const AAPL_SERVER_CAPS: u64 = 0x01;
12/// RequestBitmap and ReplyBitmap: the reply carries the volume caps.
13pub const AAPL_VOLUME_CAPS: u64 = 0x02;
14/// RequestBitmap and ReplyBitmap: the reply carries the model string.
15pub const AAPL_MODEL_INFO: u64 = 0x04;
16
17/// Client and server caps: directory listings carry Finder data
18/// (READ_DIR_ATTR).
19pub const AAPL_SUPPORTS_READ_DIR_ATTR: u64 = 0x01;
20/// Server caps: the server runs on a Unix system.
21pub const AAPL_UNIX_BASED: u64 = 0x04;
22
23/// Volume caps: names on the share are case-sensitive.
24pub const AAPL_CASE_SENSITIVE: u64 = 0x02;
25
26/// Data of a server query.
27pub const AAPL_QUERY_LEN: usize = 24;
28
29/// A server query: CommandCode, Reserved, RequestBitmap and ClientCaps.
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub struct AaplQuery {
32    /// What the client asks the reply to carry.
33    pub bitmap: u64,
34    /// The extensions the client has.
35    pub client_caps: u64,
36}
37
38impl AaplQuery {
39    /// Parses exactly [`AAPL_QUERY_LEN`] bytes whose CommandCode is
40    /// [`AAPL_SERVER_QUERY`]. Reserved is ignored.
41    pub fn parse(data: &[u8]) -> ProtoResult<Self> {
42        let data: &[u8; AAPL_QUERY_LEN] = data
43            .try_into()
44            .map_err(|_| ProtoError::Malformed("AAPL context size"))?;
45        let u64_at = |at: usize| u64::from_le_bytes(data[at..at + 8].try_into().expect("8 bytes"));
46        if u32::from_le_bytes(data[..4].try_into().expect("4 bytes")) != AAPL_SERVER_QUERY {
47            return Err(ProtoError::Malformed("AAPL command"));
48        }
49        Ok(Self {
50            bitmap: u64_at(8),
51            client_caps: u64_at(16),
52        })
53    }
54}
55
56/// The reply to a server query.
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub struct AaplReply<'a> {
59    /// The parts the reply carries. Bits other than [`AAPL_SERVER_CAPS`],
60    /// [`AAPL_VOLUME_CAPS`] and [`AAPL_MODEL_INFO`] are left out.
61    pub bitmap: u64,
62    pub server_caps: u64,
63    pub volume_caps: u64,
64    pub model: &'a str,
65}
66
67impl AaplReply<'_> {
68    /// CommandCode, Reserved and ReplyBitmap, then, for each bit of the
69    /// bitmap in bit order: ServerCaps; VolumeCaps; Pad, the length of the
70    /// model in bytes and the model in UTF-16LE without a terminator.
71    pub fn encode(&self) -> Vec<u8> {
72        let bitmap = self.bitmap & (AAPL_SERVER_CAPS | AAPL_VOLUME_CAPS | AAPL_MODEL_INFO);
73        let mut out = Vec::with_capacity(40 + 2 * self.model.len());
74        out.extend_from_slice(&AAPL_SERVER_QUERY.to_le_bytes());
75        out.extend_from_slice(&0u32.to_le_bytes());
76        out.extend_from_slice(&bitmap.to_le_bytes());
77        if bitmap & AAPL_SERVER_CAPS != 0 {
78            out.extend_from_slice(&self.server_caps.to_le_bytes());
79        }
80        if bitmap & AAPL_VOLUME_CAPS != 0 {
81            out.extend_from_slice(&self.volume_caps.to_le_bytes());
82        }
83        if bitmap & AAPL_MODEL_INFO != 0 {
84            let model = crate::utils::utf16le(self.model);
85            out.extend_from_slice(&0u32.to_le_bytes());
86            out.extend_from_slice(&(model.len() as u32).to_le_bytes());
87            out.extend_from_slice(&model);
88        }
89        out
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    /// A query asking for all three parts from a client with every
98    /// extension bit set, as macOS sends it.
99    const QUERY: [u8; 24] = [
100        0x01, 0, 0, 0, 0, 0, 0, 0, //
101        0x07, 0, 0, 0, 0, 0, 0, 0, //
102        0x3F, 0, 0, 0, 0, 0, 0, 0,
103    ];
104
105    #[test]
106    fn a_server_query_parses() {
107        assert_eq!(
108            AaplQuery::parse(&QUERY).unwrap(),
109            AaplQuery {
110                bitmap: 0x07,
111                client_caps: 0x3F,
112            }
113        );
114        let mut reserved = QUERY;
115        reserved[4] = 0xFF;
116        assert!(AaplQuery::parse(&reserved).is_ok());
117    }
118
119    #[test]
120    fn a_query_of_another_size_or_command_is_malformed() {
121        for len in [0, 16, 23, 25, 32] {
122            let mut data = QUERY.to_vec();
123            data.resize(len, 0);
124            assert!(AaplQuery::parse(&data).is_err(), "{len} bytes");
125        }
126        for command in [0u32, 2, 0x0100_0001] {
127            let mut data = QUERY;
128            data[..4].copy_from_slice(&command.to_le_bytes());
129            assert!(AaplQuery::parse(&data).is_err(), "command {command:#x}");
130        }
131    }
132
133    #[test]
134    fn a_reply_with_every_part_has_them_in_bit_order() {
135        let reply = AaplReply {
136            bitmap: 0x07,
137            server_caps: 0x0D,
138            volume_caps: 0,
139            model: "MacSamba",
140        };
141        let expected = [
142            "01000000 00000000 0700000000000000",
143            "0d00000000000000 0000000000000000",
144            "00000000 10000000 4d0061006300530061006d0062006100",
145        ]
146        .concat()
147        .replace(' ', "");
148        assert_eq!(hex::encode(reply.encode()), expected);
149    }
150
151    #[test]
152    fn a_reply_carries_only_the_known_parts_asked_for() {
153        let reply = |bitmap| {
154            AaplReply {
155                bitmap,
156                server_caps: 0x04,
157                volume_caps: 0x02,
158                model: "smb-server",
159            }
160            .encode()
161        };
162        let head = |bitmap: u64| {
163            let mut out = vec![1, 0, 0, 0, 0, 0, 0, 0];
164            out.extend_from_slice(&bitmap.to_le_bytes());
165            out
166        };
167        assert_eq!(reply(0), head(0));
168        assert_eq!(reply(0xF8), head(0));
169        assert_eq!(
170            reply(0x02),
171            [head(0x02), 0x02u64.to_le_bytes().to_vec()].concat()
172        );
173        let model = [
174            head(0x05),
175            0x04u64.to_le_bytes().to_vec(),
176            vec![0, 0, 0, 0, 20, 0, 0, 0],
177            crate::utils::utf16le("smb-server"),
178        ]
179        .concat();
180        assert_eq!(reply(0x0D), model);
181    }
182}