smb_server/proto/messages/
aapl.rs1use crate::proto::error::{ProtoError, ProtoResult};
6
7pub const AAPL_SERVER_QUERY: u32 = 1;
9
10pub const AAPL_SERVER_CAPS: u64 = 0x01;
12pub const AAPL_VOLUME_CAPS: u64 = 0x02;
14pub const AAPL_MODEL_INFO: u64 = 0x04;
16
17pub const AAPL_SUPPORTS_READ_DIR_ATTR: u64 = 0x01;
20pub const AAPL_SUPPORTS_OSX_COPYFILE: u64 = 0x02;
23pub const AAPL_UNIX_BASED: u64 = 0x04;
25
26pub const AAPL_CASE_SENSITIVE: u64 = 0x02;
28
29pub const AAPL_QUERY_LEN: usize = 24;
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub struct AaplQuery {
35 pub bitmap: u64,
37 pub client_caps: u64,
39}
40
41impl AaplQuery {
42 pub fn parse(data: &[u8]) -> ProtoResult<Self> {
45 let data: &[u8; AAPL_QUERY_LEN] = data
46 .try_into()
47 .map_err(|_| ProtoError::Malformed("AAPL context size"))?;
48 let u64_at = |at: usize| u64::from_le_bytes(data[at..at + 8].try_into().expect("8 bytes"));
49 if u32::from_le_bytes(data[..4].try_into().expect("4 bytes")) != AAPL_SERVER_QUERY {
50 return Err(ProtoError::Malformed("AAPL command"));
51 }
52 Ok(Self {
53 bitmap: u64_at(8),
54 client_caps: u64_at(16),
55 })
56 }
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub struct AaplReply<'a> {
62 pub bitmap: u64,
65 pub server_caps: u64,
66 pub volume_caps: u64,
67 pub model: &'a str,
68}
69
70impl AaplReply<'_> {
71 pub fn encode(&self) -> Vec<u8> {
75 let bitmap = self.bitmap & (AAPL_SERVER_CAPS | AAPL_VOLUME_CAPS | AAPL_MODEL_INFO);
76 let mut out = Vec::with_capacity(40 + 2 * self.model.len());
77 out.extend_from_slice(&AAPL_SERVER_QUERY.to_le_bytes());
78 out.extend_from_slice(&0u32.to_le_bytes());
79 out.extend_from_slice(&bitmap.to_le_bytes());
80 if bitmap & AAPL_SERVER_CAPS != 0 {
81 out.extend_from_slice(&self.server_caps.to_le_bytes());
82 }
83 if bitmap & AAPL_VOLUME_CAPS != 0 {
84 out.extend_from_slice(&self.volume_caps.to_le_bytes());
85 }
86 if bitmap & AAPL_MODEL_INFO != 0 {
87 let model = crate::utils::utf16le(self.model);
88 out.extend_from_slice(&0u32.to_le_bytes());
89 out.extend_from_slice(&(model.len() as u32).to_le_bytes());
90 out.extend_from_slice(&model);
91 }
92 out
93 }
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99
100 const QUERY: [u8; 24] = [
103 0x01, 0, 0, 0, 0, 0, 0, 0, 0x07, 0, 0, 0, 0, 0, 0, 0, 0x3F, 0, 0, 0, 0, 0, 0, 0,
106 ];
107
108 #[test]
109 fn a_server_query_parses() {
110 assert_eq!(
111 AaplQuery::parse(&QUERY).unwrap(),
112 AaplQuery {
113 bitmap: 0x07,
114 client_caps: 0x3F,
115 }
116 );
117 let mut reserved = QUERY;
118 reserved[4] = 0xFF;
119 assert!(AaplQuery::parse(&reserved).is_ok());
120 }
121
122 #[test]
123 fn a_query_of_another_size_or_command_is_malformed() {
124 for len in [0, 16, 23, 25, 32] {
125 let mut data = QUERY.to_vec();
126 data.resize(len, 0);
127 assert!(AaplQuery::parse(&data).is_err(), "{len} bytes");
128 }
129 for command in [0u32, 2, 0x0100_0001] {
130 let mut data = QUERY;
131 data[..4].copy_from_slice(&command.to_le_bytes());
132 assert!(AaplQuery::parse(&data).is_err(), "command {command:#x}");
133 }
134 }
135
136 #[test]
137 fn a_reply_with_every_part_has_them_in_bit_order() {
138 let reply = AaplReply {
139 bitmap: 0x07,
140 server_caps: 0x0D,
141 volume_caps: 0,
142 model: "MacSamba",
143 };
144 let expected = [
145 "01000000 00000000 0700000000000000",
146 "0d00000000000000 0000000000000000",
147 "00000000 10000000 4d0061006300530061006d0062006100",
148 ]
149 .concat()
150 .replace(' ', "");
151 assert_eq!(hex::encode(reply.encode()), expected);
152 }
153
154 #[test]
155 fn a_reply_carries_only_the_known_parts_asked_for() {
156 let reply = |bitmap| {
157 AaplReply {
158 bitmap,
159 server_caps: 0x04,
160 volume_caps: 0x02,
161 model: "smb-server",
162 }
163 .encode()
164 };
165 let head = |bitmap: u64| {
166 let mut out = vec![1, 0, 0, 0, 0, 0, 0, 0];
167 out.extend_from_slice(&bitmap.to_le_bytes());
168 out
169 };
170 assert_eq!(reply(0), head(0));
171 assert_eq!(reply(0xF8), head(0));
172 assert_eq!(
173 reply(0x02),
174 [head(0x02), 0x02u64.to_le_bytes().to_vec()].concat()
175 );
176 let model = [
177 head(0x05),
178 0x04u64.to_le_bytes().to_vec(),
179 vec![0, 0, 0, 0, 20, 0, 0, 0],
180 crate::utils::utf16le("smb-server"),
181 ]
182 .concat();
183 assert_eq!(reply(0x0D), model);
184 }
185}