Skip to main content

smb_server/proto/messages/
session_setup.rs

1//! SESSION_SETUP Request/Response (MS-SMB2 §2.2.5 / §2.2.6).
2
3use binrw::{BinRead, BinWrite, binrw};
4use std::io::Cursor;
5
6use crate::proto::error::ProtoResult;
7
8/// SMB2_SESSION_SETUP_REQUEST (MS-SMB2 §2.2.5).
9///
10/// `security_buffer` is opaque GSS-API/SPNEGO data, decoded by the SPNEGO
11/// layer. The wire offset is from the start of the SMB2 header; the buffer is
12/// read and written immediately after the fixed prefix.
13#[binrw]
14#[brw(little)]
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct SessionSetupRequest {
17    pub structure_size: u16,
18    pub flags: u8,
19    pub security_mode: u8,
20    pub capabilities: u32,
21    pub channel: u32,
22    pub security_buffer_offset: u16,
23    pub security_buffer_length: u16,
24    pub previous_session_id: u64,
25    #[br(parse_with = super::read_buf, args(security_buffer_length as usize))]
26    pub security_buffer: Vec<u8>,
27}
28
29impl SessionSetupRequest {
30    /// Flag: SMB2_SESSION_FLAG_BINDING - bind to existing session (3.x).
31    pub const FLAG_BINDING: u8 = 0x01;
32
33    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
34        Ok(Self::read(&mut Cursor::new(buf))?)
35    }
36    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
37        let mut c = Cursor::new(Vec::new());
38        BinWrite::write(self, &mut c)?;
39        out.extend_from_slice(&c.into_inner());
40        Ok(())
41    }
42}
43
44/// SMB2_SESSION_SETUP_RESPONSE (MS-SMB2 §2.2.6).
45#[binrw]
46#[brw(little)]
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct SessionSetupResponse {
49    pub structure_size: u16,
50    pub session_flags: u16,
51    pub security_buffer_offset: u16,
52    pub security_buffer_length: u16,
53    #[br(parse_with = super::read_buf, args(security_buffer_length as usize))]
54    pub security_buffer: Vec<u8>,
55}
56
57impl SessionSetupResponse {
58    /// Session flag: IS_GUEST.
59    pub const FLAG_IS_GUEST: u16 = 0x0001;
60    /// Session flag: IS_NULL (anonymous).
61    pub const FLAG_IS_NULL: u16 = 0x0002;
62    /// Session flag: ENCRYPT_DATA.
63    pub const FLAG_ENCRYPT_DATA: u16 = 0x0004;
64
65    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
66        Ok(Self::read(&mut Cursor::new(buf))?)
67    }
68    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
69        let mut c = Cursor::new(Vec::new());
70        BinWrite::write(self, &mut c)?;
71        out.extend_from_slice(&c.into_inner());
72        Ok(())
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn request_round_trips() {
82        let r = SessionSetupRequest {
83            structure_size: 25,
84            flags: 0,
85            security_mode: 0x01,
86            capabilities: 0x01,
87            channel: 0,
88            security_buffer_offset: 0x58,
89            security_buffer_length: 6,
90            previous_session_id: 0,
91            security_buffer: vec![0xDE, 0xAD, 0xBE, 0xEF, 0x01, 0x02],
92        };
93        let mut buf = Vec::new();
94        r.write_to(&mut buf).unwrap();
95        assert_eq!(SessionSetupRequest::parse(&buf).unwrap(), r);
96    }
97
98    #[test]
99    fn response_round_trips() {
100        let r = SessionSetupResponse {
101            structure_size: 9,
102            session_flags: SessionSetupResponse::FLAG_IS_GUEST,
103            security_buffer_offset: 0x48,
104            security_buffer_length: 4,
105            security_buffer: vec![1, 2, 3, 4],
106        };
107        let mut buf = Vec::new();
108        r.write_to(&mut buf).unwrap();
109        assert_eq!(SessionSetupResponse::parse(&buf).unwrap(), r);
110    }
111}