Skip to main content

smb_server/proto/messages/
query_info.rs

1//! QUERY_INFO Request/Response (MS-SMB2 §2.2.37 / §2.2.38).
2
3use binrw::{BinRead, BinWrite, binrw};
4use std::io::Cursor;
5
6use super::create::FileId;
7use crate::proto::error::ProtoResult;
8
9/// `InfoType` values (MS-SMB2 §2.2.37 InfoType field).
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11#[repr(u8)]
12pub enum InfoType {
13    File = 0x01,
14    FileSystem = 0x02,
15    Security = 0x03,
16    Quota = 0x04,
17}
18
19impl InfoType {
20    pub fn from_u8(v: u8) -> Option<Self> {
21        Some(match v {
22            0x01 => Self::File,
23            0x02 => Self::FileSystem,
24            0x03 => Self::Security,
25            0x04 => Self::Quota,
26            _ => return None,
27        })
28    }
29}
30
31/// SMB2_QUERY_INFO_REQUEST (MS-SMB2 §2.2.37).
32#[binrw]
33#[brw(little)]
34#[derive(Debug, Clone, PartialEq, Eq)]
35pub struct QueryInfoRequest {
36    pub structure_size: u16,
37    pub info_type: u8,
38    pub file_information_class: u8,
39    pub output_buffer_length: u32,
40    pub input_buffer_offset: u16,
41    pub reserved: u16,
42    pub input_buffer_length: u32,
43    /// `AdditionalInformation`: which fields of the security descriptor to
44    /// return when `info_type == Security`, or the starting EA index for a
45    /// `FileFullEaInformation` query. Zero for all other queries.
46    pub additional_information: u32,
47    pub flags: u32,
48    pub file_id: FileId,
49    /// `input_buffer_length` bytes at `input_buffer_offset`, counted from
50    /// the start of the SMB2 header and not before the end of the fixed
51    /// fields: the extended attribute names of a `FileFullEaInformation`
52    /// query.
53    #[br(parse_with = super::read_buf_at, args(
54        input_buffer_offset.into(),
55        input_buffer_length as usize,
56        QUERY_INFO_BUFFER_OFFSET,
57    ))]
58    #[bw(write_with = super::write_buf_at, args(u64::from(*input_buffer_offset)))]
59    pub input_buffer: Vec<u8>,
60}
61
62/// Offset of the first byte after the fixed QUERY_INFO request fields,
63/// counted from the start of the SMB2 header: 64 + 40.
64const QUERY_INFO_BUFFER_OFFSET: u64 = 0x68;
65
66impl QueryInfoRequest {
67    /// Flag: SL_RESTART_SCAN.
68    pub const FLAG_RESTART_SCAN: u32 = 0x0000_0001;
69    /// Flag: SL_RETURN_SINGLE_ENTRY.
70    pub const FLAG_RETURN_SINGLE_ENTRY: u32 = 0x0000_0002;
71    /// Flag: SL_INDEX_SPECIFIED.
72    pub const FLAG_INDEX_SPECIFIED: u32 = 0x0000_0004;
73
74    pub fn info_type_enum(&self) -> Option<InfoType> {
75        InfoType::from_u8(self.info_type)
76    }
77
78    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
79        Ok(Self::read(&mut Cursor::new(buf))?)
80    }
81    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
82        let mut c = Cursor::new(Vec::new());
83        BinWrite::write(self, &mut c)?;
84        out.extend_from_slice(&c.into_inner());
85        Ok(())
86    }
87}
88
89/// SMB2_QUERY_INFO_RESPONSE (MS-SMB2 §2.2.38).
90#[binrw]
91#[brw(little)]
92#[derive(Debug, Clone, PartialEq, Eq)]
93pub struct QueryInfoResponse {
94    pub structure_size: u16,
95    pub output_buffer_offset: u16,
96    pub output_buffer_length: u32,
97    #[br(parse_with = super::read_buf, args(output_buffer_length as usize))]
98    pub buffer: Vec<u8>,
99}
100
101impl QueryInfoResponse {
102    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
103        Ok(Self::read(&mut Cursor::new(buf))?)
104    }
105    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
106        let mut c = Cursor::new(Vec::new());
107        BinWrite::write(self, &mut c)?;
108        out.extend_from_slice(&c.into_inner());
109        Ok(())
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn request_round_trips() {
119        let r = QueryInfoRequest {
120            structure_size: 41,
121            info_type: InfoType::File as u8,
122            file_information_class: 0x05, // FileStandardInformation
123            output_buffer_length: 0x1000,
124            input_buffer_offset: 0,
125            reserved: 0,
126            input_buffer_length: 0,
127            additional_information: 0,
128            flags: 0,
129            file_id: FileId::new(1, 2),
130            input_buffer: vec![],
131        };
132        let mut buf = Vec::new();
133        r.write_to(&mut buf).unwrap();
134        let decoded = QueryInfoRequest::parse(&buf).unwrap();
135        assert_eq!(decoded, r);
136        assert_eq!(decoded.info_type_enum(), Some(InfoType::File));
137    }
138
139    #[test]
140    fn the_input_buffer_is_written_and_read_at_its_offset() {
141        let request = QueryInfoRequest {
142            structure_size: 41,
143            info_type: InfoType::File as u8,
144            file_information_class: 0x0F,
145            output_buffer_length: 0x1000,
146            input_buffer_offset: 0x70,
147            reserved: 0,
148            input_buffer_length: 4,
149            additional_information: 0,
150            flags: 0,
151            file_id: FileId::new(1, 2),
152            input_buffer: vec![1, 2, 3, 4],
153        };
154        let mut body = Vec::new();
155        request.write_to(&mut body).unwrap();
156        // 0x70 is 8 bytes past the end of the fixed fields.
157        assert_eq!(body[40..], [0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4]);
158        assert_eq!(QueryInfoRequest::parse(&body).unwrap(), request);
159        // Inside the fixed fields.
160        body[8..10].copy_from_slice(&0x60u16.to_le_bytes());
161        assert!(QueryInfoRequest::parse(&body).is_err());
162    }
163
164    #[test]
165    fn response_round_trips() {
166        let r = QueryInfoResponse {
167            structure_size: 9,
168            output_buffer_offset: 0x48,
169            output_buffer_length: 4,
170            buffer: vec![0xAB, 0xCD, 0xEF, 0x01],
171        };
172        let mut buf = Vec::new();
173        r.write_to(&mut buf).unwrap();
174        assert_eq!(QueryInfoResponse::parse(&buf).unwrap(), r);
175    }
176}