Skip to main content

smb_server/proto/messages/
set_info.rs

1//! SET_INFO Request/Response (MS-SMB2 §2.2.39 / §2.2.40).
2
3use binrw::{BinRead, BinWrite, binrw};
4use std::io::Cursor;
5
6use super::create::FileId;
7use crate::proto::error::ProtoResult;
8
9/// SMB2_SET_INFO_REQUEST (MS-SMB2 §2.2.39).
10#[binrw]
11#[brw(little)]
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct SetInfoRequest {
14    pub structure_size: u16,
15    pub info_type: u8,
16    pub file_information_class: u8,
17    pub buffer_length: u32,
18    pub buffer_offset: u16,
19    pub reserved: u16,
20    pub additional_information: u32,
21    pub file_id: FileId,
22    /// `buffer_length` bytes at `buffer_offset`, counted from the start of
23    /// the SMB2 header and not before the end of the fixed fields.
24    #[br(parse_with = super::read_buf_at, args(
25        buffer_offset.into(),
26        buffer_length as usize,
27        SET_INFO_BUFFER_OFFSET,
28    ))]
29    pub buffer: Vec<u8>,
30}
31
32/// Offset of the first byte after the fixed SET_INFO request fields,
33/// counted from the start of the SMB2 header: 64 + 32.
34const SET_INFO_BUFFER_OFFSET: u64 = 0x60;
35
36impl SetInfoRequest {
37    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
38        Ok(Self::read(&mut Cursor::new(buf))?)
39    }
40    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
41        let mut c = Cursor::new(Vec::new());
42        BinWrite::write(self, &mut c)?;
43        out.extend_from_slice(&c.into_inner());
44        Ok(())
45    }
46}
47
48/// SMB2_SET_INFO_RESPONSE (MS-SMB2 §2.2.40).
49#[binrw]
50#[brw(little)]
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct SetInfoResponse {
53    pub structure_size: u16,
54}
55
56impl Default for SetInfoResponse {
57    fn default() -> Self {
58        Self { structure_size: 2 }
59    }
60}
61
62impl SetInfoResponse {
63    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
64        Ok(Self::read(&mut Cursor::new(buf))?)
65    }
66    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
67        let mut c = Cursor::new(Vec::new());
68        BinWrite::write(self, &mut c)?;
69        out.extend_from_slice(&c.into_inner());
70        Ok(())
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn request_round_trips() {
80        let r = SetInfoRequest {
81            structure_size: 33,
82            info_type: 0x01,              // File
83            file_information_class: 0x14, // FileEndOfFileInformation
84            buffer_length: 8,
85            buffer_offset: 0x60,
86            reserved: 0,
87            additional_information: 0,
88            file_id: FileId::new(1, 2),
89            buffer: vec![0, 0, 0, 0x10, 0, 0, 0, 0],
90        };
91        let mut buf = Vec::new();
92        r.write_to(&mut buf).unwrap();
93        assert_eq!(SetInfoRequest::parse(&buf).unwrap(), r);
94    }
95
96    #[test]
97    fn buffer_is_read_at_its_offset() {
98        let mut body = Vec::new();
99        SetInfoRequest {
100            structure_size: 33,
101            info_type: 0x01,
102            file_information_class: 0x14,
103            buffer_length: 8,
104            buffer_offset: 0x68,
105            reserved: 0,
106            additional_information: 0,
107            file_id: FileId::new(1, 2),
108            buffer: vec![0xEE; 8],
109        }
110        .write_to(&mut body)
111        .unwrap();
112        body.extend_from_slice(&[7; 8]);
113        assert_eq!(SetInfoRequest::parse(&body).unwrap().buffer, [7; 8]);
114
115        let mut below = body.clone();
116        below[8..10].copy_from_slice(&0x58u16.to_le_bytes());
117        assert!(SetInfoRequest::parse(&below).is_err());
118    }
119
120    #[test]
121    fn response_round_trips() {
122        let r = SetInfoResponse::default();
123        let mut buf = Vec::new();
124        r.write_to(&mut buf).unwrap();
125        assert_eq!(SetInfoResponse::parse(&buf).unwrap(), r);
126        assert_eq!(buf.len(), 2);
127    }
128}