smb-msg 0.11.2

SMB Messages and structures for `smb-rs`
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Tree (share) connect & disconnect messages

use binrw::prelude::*;
use binrw::{NullWideString, io::TakeSeekExt};
use modular_bitfield::prelude::*;
use smb_dtyp::{
    binrw_util::prelude::*,
    security::{ACL, ClaimSecurityAttributeRelativeV1, SID},
};

/// Flags for SMB2 TREE_CONNECT Request
///
/// Reference: MS-SMB2 2.2.9
#[smb_dtyp::mbitfield]
pub struct TreeConnectRequestFlags {
    /// Client has previously connected to the specified cluster share using the SMB dialect of the connection
    pub cluster_reconnect: bool,
    /// Client can handle synchronous share redirects via a Share Redirect error context response
    pub redirect_to_owner: bool,
    /// Tree connect request extension is present, starting at the Buffer field
    pub extension_present: bool,
    #[skip]
    __: B13,
}

/// SMB2 TREE_CONNECT Request
///
/// Sent by a client to request access to a particular share on the server.
/// Supports both the base and extension variants.
/// - On read, uses extension iff `flags.extension_present()` - parses just like the server intends.
/// - On write, uses extension iff `tree_connect_contexts` is non-empty.
///
/// Reference: MS-SMB2 2.2.9
#[smb_request(size = 9)]
pub struct TreeConnectRequest {
    /// Flags indicating how to process the operation
    pub flags: TreeConnectRequestFlags,
    #[bw(calc = PosMarker::default())]
    #[br(temp)]
    _path_offset: PosMarker<u16>,
    #[bw(try_calc = buffer.size().try_into())]
    #[br(temp)]
    path_length: u16,

    // -- Extension --
    #[br(if(flags.extension_present()))]
    #[br(temp)]
    #[bw(calc = if tree_connect_contexts.is_empty() { None } else { Some(PosMarker::default()) })]
    tree_connect_context_offset: Option<PosMarker<u32>>,

    #[br(if(flags.extension_present()))]
    #[bw(if(!tree_connect_contexts.is_empty()))]
    #[bw(calc = if tree_connect_contexts.is_empty() { None } else { Some(tree_connect_contexts.len().try_into().unwrap()) })]
    #[br(temp)]
    tree_connect_context_count: Option<u16>,

    #[br(if(flags.extension_present()))]
    #[bw(if(!tree_connect_contexts.is_empty()))]
    #[bw(calc = Some([0u8; 10]))]
    #[br(temp)]
    _reserved: Option<[u8; 10]>,
    // -- Extension End --
    // ------------------------------------------------
    // -- Base --
    #[brw(little)]
    #[br(args { size: SizedStringSize::bytes16(path_length) })]
    #[bw(write_with = PosMarker::write_aoff, args(&_path_offset))]
    /// Full share path name in Unicode format "\\server\share"
    pub buffer: SizedWideString,

    // -- Extension --
    #[br(if(flags.extension_present()))]
    #[br(seek_before = tree_connect_context_offset.unwrap().seek_relative(true))]
    #[br(count = tree_connect_context_count.unwrap_or(0))]
    #[bw(if(!tree_connect_contexts.is_empty()))]
    #[bw(write_with = PosMarker::write_aoff_m, args(tree_connect_context_offset.as_ref()))]
    tree_connect_contexts: Vec<TreeConnectContext>,
}

/// SMB2 TREE_CONNECT_CONTEXT Request structure
///
/// Used to encode additional properties in SMB2 TREE_CONNECT requests and responses.
///
/// Reference: MS-SMB2 2.2.9.2
#[smb_request_binrw]
pub struct TreeConnectContext {
    /// Type of context in the Data field
    #[bw(calc = 1)]
    #[br(assert(context_type == 1))]
    context_type: u16,
    /// Length in bytes of the Data field
    data_length: u16,
    reserved: u32,
    data: RemotedIdentityTreeConnect,
}

macro_rules! make_remoted_identity_connect{
    (
        $($field:ident: $value:ty),*
    ) => {
        pastey::paste! {

#[binwrite]
#[derive(Debug, BinRead, PartialEq, Eq)]
/// SMB2_REMOTED_IDENTITY_TREE_CONNECT Context
///
/// Contains remoted identity tree connect context data with user information,
/// groups, privileges, and other security attributes.
///
/// Reference: MS-SMB2 2.2.9.2.1
pub struct RemotedIdentityTreeConnect {
    #[bw(calc = PosMarker::new(1))]
    #[br(assert(_ticket_type.value == 1))]
    _ticket_type: PosMarker<u16>,
    /// Total size of this structure
    ticket_size: u16,

    // Offsets
    $(
        #[bw(calc = PosMarker::default())]
        #[br(temp)]
        [<_$field _offset>]: PosMarker<u16>,
    )*

    // Values
    $(
        #[br(seek_before = _ticket_type.seek_from([<_$field _offset>].value as u64))]
        #[bw(write_with = PosMarker::write_roff_b, args(&[<_$field _offset>], &_ticket_type))]
        $field: $value,
    )*
}
        }
    }
}

make_remoted_identity_connect! {
    user: SidAttrData,
    user_name: NullWideString,
    domain: NullWideString,
    groups: SidArrayData,
    restricted_groups: SidArrayData,
    privileges: PrivilegeArrayData,
    primary_group: SidArrayData,
    owner: BlobData<SID>,
    default_dacl: BlobData<ACL>,
    device_groups: SidArrayData,
    user_claims: BlobData<ClaimSecurityAttributeRelativeV1>,
    device_claims: BlobData<ClaimSecurityAttributeRelativeV1>
}

/// BLOB_DATA structure containing variable-length binary data
///
/// Reference: MS-SMB2 2.2.9.2.1.1
#[binrw::binrw]
#[derive(Debug, PartialEq, Eq)]
pub struct BlobData<T>
where
    T: BinRead + BinWrite,
    for<'a> <T as BinRead>::Args<'a>: Default,
    for<'b> <T as BinWrite>::Args<'b>: Default,
{
    /// Size of the blob data
    blob_size: PosMarker<u16>,
    #[br(map_stream = |s| s.take_seek(blob_size.value as u64))]
    pub blob_data: T,
}

/// Array data structure for variable-length arrays
#[binrw::binrw]
#[derive(Debug, PartialEq, Eq)]
pub struct ArrayData<T>
where
    T: BinRead + BinWrite + 'static,
    for<'a> <T as BinRead>::Args<'a>: Default + Clone,
    for<'b> <T as BinWrite>::Args<'b>: Default + Clone,
{
    #[bw(try_calc = list.len().try_into())]
    lcount: u16,
    #[br(count = lcount)]
    pub list: Vec<T>,
}

/// SID_ATTR_DATA structure containing SID and attributes
///
/// Reference: MS-SMB2 2.2.9.2.1.2
#[binrw::binrw]
#[derive(Debug, PartialEq, Eq)]
pub struct SidAttrData {
    /// Security identifier
    pub sid_data: SID,
    /// Attributes associated with the SID
    pub attr: SidAttrSeGroup,
}

type SidArrayData = ArrayData<SidAttrData>;

/// SE_GROUP attributes for SID
///
/// Reference: MS-SMB2 2.2.9.2.1.2
#[smb_dtyp::mbitfield]
pub struct SidAttrSeGroup {
    /// This SID is mandatory
    pub mandatory: bool,
    /// This SID is enabled by default
    pub enabled_by_default: bool,
    /// This SID is enabled for access checks
    pub group_enabled: bool,
    /// This SID is the owner SID for objects created by this user
    pub group_owner: bool,
    /// This SID cannot be disabled
    pub group_use_for_deny_only: bool,
    /// This SID identifies an integrity level
    pub group_integrity: bool,
    /// This SID is integrity-enabled
    pub group_integrity_enabled: bool,
    #[skip]
    __: B21,
    /// Identifies the logon session
    pub group_logon_id: B4,
}

/// LUID_ATTR_DATA structure containing LUID and attributes
#[binrw::binrw]
#[derive(Debug, PartialEq, Eq)]
pub struct LuidAttrData {
    /// Locally unique identifier
    pub luid: u64,
    /// Attributes for the LUID
    pub attr: LsaprLuidAttributes,
}

mod lsapr_luid_attributes {
    use super::*;
    /// LSAPR_LUID_ATTRIBUTES structure
    ///
    /// Reference: MS-LSAD 2.2.5.4
    #[smb_dtyp::mbitfield]
    pub struct LsaprLuidAttributes {
        /// Default privilege that is enabled by default
        pub is_default: bool,
        /// Privilege is enabled
        pub is_enabled: bool,
        #[skip]
        __: B30,
    }
}

use lsapr_luid_attributes::LsaprLuidAttributes;
use smb_msg_derive::*;

type PrivilegeData = BlobData<LuidAttrData>;

type PrivilegeArrayData = ArrayData<PrivilegeData>;

impl TreeConnectRequest {
    pub fn new(name: &str) -> TreeConnectRequest {
        TreeConnectRequest {
            flags: TreeConnectRequestFlags::new(),
            buffer: name.into(),
            tree_connect_contexts: vec![],
        }
    }
}

/// SMB2 TREE_CONNECT Response
///
/// Sent by the server when an SMB2 TREE_CONNECT request is processed successfully.
///
/// Reference: MS-SMB2 2.2.10
#[smb_response(size = 16)]
pub struct TreeConnectResponse {
    /// Type of share being accessed
    pub share_type: ShareType,
    reserved: u8,
    /// Properties for this share
    pub share_flags: ShareFlags,
    /// Capabilities for this share
    pub capabilities: TreeCapabilities,
    /// Maximal access for the user that establishes the tree connect on the share
    pub maximal_access: u32,
}

/// Share caching mode for offline file access
#[derive(BitfieldSpecifier, Debug, Clone, Copy)]
#[bits = 4]
pub enum ShareCacheMode {
    /// Manual caching - client can cache files explicitly selected by user
    Manual,
    /// Automatic caching - client can automatically cache files used by user
    Auto,
    /// VDO caching - client can use cached files even when share is available
    Vdo,
    /// No caching - offline caching must not occur
    NoCache,
    All = 0xf,
}

/// Share flags indicating various share properties
///
/// Reference: MS-SMB2 2.2.10
#[smb_dtyp::mbitfield]
pub struct ShareFlags {
    /// Share is present in a Distributed File System tree structure
    pub dfs: bool,
    /// Share is present in a DFS Root tree structure
    pub dfs_root: bool,
    #[skip]
    __: B2,
    /// Offline caching behavior for this share
    pub caching_mode: ShareCacheMode,

    /// Share disallows exclusive file opens that deny reads to an open file
    pub restrict_exclusive_opens: bool,
    /// Share disallows clients from opening files in exclusive mode that prevents deletion
    pub force_shared_delete: bool,
    /// Namespace caching is allowed (client must ignore this flag)
    pub allow_namespace_caching: bool,
    /// Server will filter directory entries based on client access permissions
    pub access_based_directory_enum: bool,
    /// Server will not issue exclusive caching rights on this share
    pub force_levelii_oplock: bool,
    /// Share supports hash generation for branch cache retrieval of data
    pub enable_hash_v1: bool,
    /// Share supports v2 hash generation for branch cache retrieval of data
    pub enable_hash_v2: bool,
    /// Server requires encryption of remote file access messages on this share
    pub encrypt_data: bool,

    #[skip]
    __: B2,
    /// Share supports identity remoting via SMB2_REMOTED_IDENTITY_TREE_CONNECT context
    pub identity_remoting: bool,
    #[skip]
    __: B1,
    /// Server supports compression of read/write messages on this share
    pub compress_data: bool,
    /// Server indicates preference to isolate communication on separate connections
    pub isolated_transport: bool,
    #[skip]
    __: B10,
}

/// Tree capabilities indicating various share capabilities
///
/// Reference: MS-SMB2 2.2.10
#[smb_dtyp::mbitfield]
pub struct TreeCapabilities {
    #[skip]
    __: B3,
    /// Share is present in a DFS tree structure
    pub dfs: bool,
    /// Share is continuously available
    pub continuous_availability: bool,
    /// Share facilitates faster recovery of durable handles
    pub scaleout: bool,
    /// Share provides monitoring through the Witness service
    pub cluster: bool,
    /// Share allows dynamic changes in ownership
    pub asymmetric: bool,

    /// Share supports synchronous share level redirection
    pub redirect_to_owner: bool,
    #[skip]
    __: B23,
}

/// Type of share being accessed
///
/// Reference: MS-SMB2 2.2.10
#[smb_response_binrw]
#[derive(Clone, Copy)]
#[brw(repr(u8))]
pub enum ShareType {
    /// Physical disk share
    Disk = 0x1,
    /// Named pipe share
    Pipe = 0x2,
    /// Printer share
    Print = 0x3,
}

/// SMB2 TREE_DISCONNECT Request
///
/// Sent by a client to request that the tree connect that is specified in the TreeId within
/// the SMB2 header be disconnected.
///
/// Reference: MS-SMB2 2.2.11
#[smb_request(size = 4)]
#[derive(Default)]
pub struct TreeDisconnectRequest {
    reserved: u16,
}

/// SMB2 TREE_DISCONNECT Response
///
/// Sent by the server when an SMB2 TREE_DISCONNECT Request is processed successfully.
///
/// Reference: MS-SMB2 2.2.12
#[smb_response(size = 4)]
#[derive(Default)]
pub struct TreeDisconnectResponse {
    reserved: u16,
}

#[cfg(test)]
mod tests {
    use crate::*;

    use super::*;

    // TODO(test): Add tests with tree connect contexts.
    test_request! {
        TreeConnect {
            flags: TreeConnectRequestFlags::new(),
            buffer: r"\\adc.aviv.local\IPC$".into(),
            tree_connect_contexts: vec![],
        } => "0900000048002a005c005c006100640063002e0061007600690076002e006c006f00630061006c005c004900500043002400"
    }

    test_binrw_response! {
        struct TreeConnectResponse {
            share_type: ShareType::Disk,
            share_flags: ShareFlags::new().with_access_based_directory_enum(true),
            capabilities: TreeCapabilities::new(),
            maximal_access: 0x001f01ff,
        } => "100001000008000000000000ff011f00"
    }
}