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_UNIX_BASED: u64 = 0x04;
22
23pub const AAPL_CASE_SENSITIVE: u64 = 0x02;
25
26pub const AAPL_QUERY_LEN: usize = 24;
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31pub struct AaplQuery {
32 pub bitmap: u64,
34 pub client_caps: u64,
36}
37
38impl AaplQuery {
39 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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub struct AaplReply<'a> {
59 pub bitmap: u64,
62 pub server_caps: u64,
63 pub volume_caps: u64,
64 pub model: &'a str,
65}
66
67impl AaplReply<'_> {
68 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 const QUERY: [u8; 24] = [
100 0x01, 0, 0, 0, 0, 0, 0, 0, 0x07, 0, 0, 0, 0, 0, 0, 0, 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}