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`. Otherwise an additional info-class
45    /// selector for FS info.
46    pub additional_information: u32,
47    pub flags: u32,
48    pub file_id: FileId,
49    /// Optional input buffer (used by FILE/FS info classes that need it, e.g.
50    /// `FileFullEaInformation` extended-attribute name lists).
51    #[br(count = input_buffer_length as usize)]
52    pub input_buffer: Vec<u8>,
53}
54
55impl QueryInfoRequest {
56    /// Flag: SL_RESTART_SCAN.
57    pub const FLAG_RESTART_SCAN: u32 = 0x0000_0001;
58    /// Flag: SL_RETURN_SINGLE_ENTRY.
59    pub const FLAG_RETURN_SINGLE_ENTRY: u32 = 0x0000_0002;
60    /// Flag: SL_INDEX_SPECIFIED.
61    pub const FLAG_INDEX_SPECIFIED: u32 = 0x0000_0004;
62
63    pub fn info_type_enum(&self) -> Option<InfoType> {
64        InfoType::from_u8(self.info_type)
65    }
66
67    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
68        Ok(Self::read(&mut Cursor::new(buf))?)
69    }
70    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
71        let mut c = Cursor::new(Vec::new());
72        BinWrite::write(self, &mut c)?;
73        out.extend_from_slice(&c.into_inner());
74        Ok(())
75    }
76}
77
78/// SMB2_QUERY_INFO_RESPONSE (MS-SMB2 §2.2.38).
79#[binrw]
80#[brw(little)]
81#[derive(Debug, Clone, PartialEq, Eq)]
82pub struct QueryInfoResponse {
83    pub structure_size: u16,
84    pub output_buffer_offset: u16,
85    pub output_buffer_length: u32,
86    #[br(count = output_buffer_length as usize)]
87    pub buffer: Vec<u8>,
88}
89
90impl QueryInfoResponse {
91    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
92        Ok(Self::read(&mut Cursor::new(buf))?)
93    }
94    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
95        let mut c = Cursor::new(Vec::new());
96        BinWrite::write(self, &mut c)?;
97        out.extend_from_slice(&c.into_inner());
98        Ok(())
99    }
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105
106    #[test]
107    fn request_round_trips() {
108        let r = QueryInfoRequest {
109            structure_size: 41,
110            info_type: InfoType::File as u8,
111            file_information_class: 0x05, // FileStandardInformation
112            output_buffer_length: 0x1000,
113            input_buffer_offset: 0,
114            reserved: 0,
115            input_buffer_length: 0,
116            additional_information: 0,
117            flags: 0,
118            file_id: FileId::new(1, 2),
119            input_buffer: vec![],
120        };
121        let mut buf = Vec::new();
122        r.write_to(&mut buf).unwrap();
123        let decoded = QueryInfoRequest::parse(&buf).unwrap();
124        assert_eq!(decoded, r);
125        assert_eq!(decoded.info_type_enum(), Some(InfoType::File));
126    }
127
128    #[test]
129    fn response_round_trips() {
130        let r = QueryInfoResponse {
131            structure_size: 9,
132            output_buffer_offset: 0x48,
133            output_buffer_length: 4,
134            buffer: vec![0xAB, 0xCD, 0xEF, 0x01],
135        };
136        let mut buf = Vec::new();
137        r.write_to(&mut buf).unwrap();
138        assert_eq!(QueryInfoResponse::parse(&buf).unwrap(), r);
139    }
140}