smb-server 0.7.0

SMB2/3 file-sharing server library with pluggable storage backends.
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
//! NEGOTIATE handler.

use std::sync::Arc;

use crate::proto::auth::spnego::encode_init_response;
use crate::proto::crypto::{Cipher, SigningAlgo};
use crate::proto::header::{SMB2_HEADER_LEN, Smb2Header};
use crate::proto::messages::{
    Dialect, EncryptionCapabilities, NegotiateContext, NegotiateRequest, NegotiateResponse,
    PreauthIntegrityCapabilities, SigningCapabilities,
};
use binrw::BinRead;
use tracing::info;
use uuid::Uuid;

use crate::conn::state::Connection;
use crate::dispatch::HandlerResponse;
use crate::ntstatus;
use crate::server::ServerState;
use crate::utils::{fill_random, now_filetime};

// MS-SMB2 2.2.4 SecurityMode bits.
const SIGNING_ENABLED: u16 = 0x0001;
const SIGNING_REQUIRED: u16 = 0x0002;

const CAP_LEASING: u32 = 0x0000_0002;
const CAP_LARGE_MTU: u32 = 0x0000_0004;
const CAP_ENCRYPTION: u32 = 0x0000_0040;

/// Size of the fixed part of a NEGOTIATE request, before Dialects.
const NEGOTIATE_FIXED_LEN: usize = 36;

/// Values from the client's NEGOTIATE request (MS-SMB2 3.3.5.4).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientNegotiate {
    pub capabilities: u32,
    pub security_mode: u16,
    pub guid: [u8; 16],
    pub dialects: Vec<u16>,
}

impl ClientNegotiate {
    /// Connection.ShouldSign: the client set SMB2_NEGOTIATE_SIGNING_REQUIRED.
    pub fn should_sign(&self) -> bool {
        self.security_mode & SIGNING_REQUIRED != 0
    }
}

/// SecurityMode the server sends (Connection.ServerSecurityMode).
pub(crate) fn server_security_mode(server: &ServerState) -> u16 {
    if server.auth.settings.require_signing {
        SIGNING_ENABLED | SIGNING_REQUIRED
    } else {
        SIGNING_ENABLED
    }
}

/// Capabilities the server sends for `dialect` to a client that sent
/// `client_capabilities` (Connection.ServerCapabilities). LEASING is not
/// defined for SMB 2.0.2, and LARGE_MTU needs multi-credit support, which
/// SMB 2.0.2 lacks. ENCRYPTION goes to SMB 3.0 and 3.0.2 clients that have
/// it; 3.1.1 negotiates a cipher instead.
pub(crate) fn server_capabilities(dialect: Dialect, client_capabilities: u32) -> u32 {
    let mut caps = match dialect {
        Dialect::Smb202 => 0,
        _ => CAP_LEASING | CAP_LARGE_MTU,
    };
    if matches!(dialect, Dialect::Smb300 | Dialect::Smb302)
        && client_capabilities & CAP_ENCRYPTION != 0
    {
        caps |= CAP_ENCRYPTION;
    }
    caps
}

/// Connection.CipherId for an SMB 3.0 or 3.0.2 connection: AES-128-CCM
/// when the client has the ENCRYPTION capability.
fn cipher_30(client_capabilities: u32) -> Option<Cipher> {
    (client_capabilities & CAP_ENCRYPTION != 0).then_some(Cipher::Aes128Ccm)
}

/// The first cipher in the client's list that the server implements.
fn select_cipher(ciphers: &[u16]) -> Option<Cipher> {
    ciphers.iter().find_map(|&id| Cipher::from_id(id))
}

/// What the client's 3.1.1 NegotiateContextList asks of the response.
struct ClientContexts {
    /// The signing algorithm picked from the client's
    /// SMB2_SIGNING_CAPABILITIES, when it sent one.
    signing_algorithm: Option<u16>,
    /// The cipher picked from the client's SMB2_ENCRYPTION_CAPABILITIES,
    /// when it sent one: `Some(None)` when no cipher is in common.
    cipher: Option<Option<Cipher>>,
}

/// Checks the 3.1.1 NegotiateContextList of `req` (MS-SMB2 3.3.5.4).
/// `body` is the request after the SMB2 header.
fn read_contexts(body: &[u8], req: &NegotiateRequest) -> Result<ClientContexts, u32> {
    let invalid = ntstatus::STATUS_INVALID_PARAMETER;
    let field = req.negotiate_context_offset_or_client_start_time;
    let offset = (field & 0xFFFF_FFFF) as usize;
    let count = ((field >> 32) & 0xFFFF) as u16;
    let dialects_end = NEGOTIATE_FIXED_LEN + 2 * req.dialects.len();
    let start = offset.checked_sub(SMB2_HEADER_LEN).ok_or(invalid)?;
    if start < dialects_end || start > body.len() {
        return Err(invalid);
    }
    let list = NegotiateContext::parse_list(&body[start..], count).map_err(|_| invalid)?;

    let of_type = |t: u16| list.iter().filter(move |c| c.context_type == t);
    let mut preauth = of_type(NegotiateContext::TYPE_PREAUTH_INTEGRITY);
    let (Some(preauth), None) = (preauth.next(), preauth.next()) else {
        return Err(invalid);
    };
    for t in [
        NegotiateContext::TYPE_ENCRYPTION,
        NegotiateContext::TYPE_COMPRESSION,
        NegotiateContext::TYPE_RDMA_TRANSFORM,
        NegotiateContext::TYPE_SIGNING,
    ] {
        if of_type(t).count() > 1 {
            return Err(invalid);
        }
    }

    let caps = PreauthIntegrityCapabilities::read(&mut std::io::Cursor::new(&preauth.data))
        .map_err(|_| invalid)?;
    if !caps
        .hash_algorithms
        .contains(&PreauthIntegrityCapabilities::HASH_SHA512)
    {
        return Err(ntstatus::STATUS_SMB_NO_PREAUTH_INTEGRITY_HASH_OVERLAP);
    }

    let signing_algorithm = match of_type(NegotiateContext::TYPE_SIGNING).next() {
        Some(ctx) => {
            let caps = SigningCapabilities::read(&mut std::io::Cursor::new(&ctx.data))
                .map_err(|_| invalid)?;
            if caps.signing_algorithm_count == 0 {
                return Err(invalid);
            }
            // AES-CMAC is the one 3.1.1 algorithm implemented, and the
            // answer when the client lists none of the server's.
            Some(SigningCapabilities::ALGORITHM_AES_CMAC)
        }
        None => None,
    };
    let cipher = match of_type(NegotiateContext::TYPE_ENCRYPTION).next() {
        Some(ctx) => {
            let caps = EncryptionCapabilities::read(&mut std::io::Cursor::new(&ctx.data))
                .map_err(|_| invalid)?;
            if caps.cipher_count == 0 {
                return Err(invalid);
            }
            Some(select_cipher(&caps.ciphers))
        }
        None => None,
    };
    Ok(ClientContexts {
        signing_algorithm,
        cipher,
    })
}

pub async fn handle(
    server: &Arc<ServerState>,
    conn: &Arc<Connection>,
    _hdr: &Smb2Header,
    body: &[u8],
) -> HandlerResponse {
    let req = match NegotiateRequest::parse(body) {
        Ok(r) => r,
        Err(_) => return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER),
    };
    *conn
        .client_negotiate
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(ClientNegotiate {
        capabilities: req.capabilities,
        security_mode: req.security_mode,
        guid: req.client_guid,
        dialects: req.dialects.clone(),
    });
    if req.dialects.is_empty() {
        return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER);
    }

    let Some(dialect) = select_dialect(&req.dialects) else {
        return HandlerResponse::err(ntstatus::STATUS_NOT_SUPPORTED);
    };
    let chosen = dialect.as_u16();
    let contexts = if dialect == Dialect::Smb311 {
        match read_contexts(body, &req) {
            Ok(c) => Some(c),
            Err(status) => return HandlerResponse::err(status),
        }
    } else {
        None
    };

    *conn.dialect.write().await = Some(dialect);
    *conn.client_guid.write().await = Uuid::from_bytes(req.client_guid);
    *conn.signing_algo.write().await = match dialect {
        Dialect::Smb202 | Dialect::Smb210 => SigningAlgo::HmacSha256,
        _ => SigningAlgo::AesCmac,
    };
    *conn.cipher.write().await = match (dialect, &contexts) {
        (Dialect::Smb300 | Dialect::Smb302, _) => cipher_30(req.capabilities),
        (Dialect::Smb311, Some(c)) => c.cipher.flatten(),
        _ => None,
    };

    // Build SPNEGO security blob (mech-list-only, advertising NTLMSSP).
    let security_blob = encode_init_response();
    let security_buffer_offset: u16 = 64 + 64; // SMB2 header + fixed NEG response (64 bytes)
    let security_buffer_length: u16 = security_blob.len() as u16;

    // For 3.1.1 build negotiate contexts.
    let mut contexts_bytes: Vec<u8> = Vec::new();
    let mut context_count: u16 = 0;
    let mut negotiate_context_offset: u32 = 0;

    if let Some(client_contexts) = contexts {
        // PREAUTH_INTEGRITY_CAPABILITIES
        let mut salt = [0u8; 32];
        if let Err(e) = fill_random(&mut salt) {
            tracing::error!(error = %e, "OS RNG failed; cannot generate preauth salt");
            return HandlerResponse::err(ntstatus::STATUS_INTERNAL_ERROR);
        }
        let preauth_caps = PreauthIntegrityCapabilities {
            hash_algorithm_count: 1,
            salt_length: 32,
            hash_algorithms: vec![PreauthIntegrityCapabilities::HASH_SHA512],
            salt: salt.to_vec(),
        };
        let preauth_data = {
            use binrw::BinWrite;
            let mut c = std::io::Cursor::new(Vec::new());
            BinWrite::write(&preauth_caps, &mut c).expect("preauth negotiate context encodes");
            c.into_inner()
        };
        let preauth_ctx = NegotiateContext {
            context_type: NegotiateContext::TYPE_PREAUTH_INTEGRITY,
            data_length: preauth_data.len() as u16,
            reserved: 0,
            data: preauth_data,
        };

        let mut ctxs = vec![preauth_ctx];
        // SIGNING_CAPABILITIES goes back only to a client that sent one.
        if let Some(algorithm) = client_contexts.signing_algorithm {
            let signing_caps = SigningCapabilities {
                signing_algorithm_count: 1,
                signing_algorithms: vec![algorithm],
            };
            let signing_data = {
                use binrw::BinWrite;
                let mut c = std::io::Cursor::new(Vec::new());
                BinWrite::write(&signing_caps, &mut c).expect("signing negotiate context encodes");
                c.into_inner()
            };
            ctxs.push(NegotiateContext {
                context_type: NegotiateContext::TYPE_SIGNING,
                data_length: signing_data.len() as u16,
                reserved: 0,
                data: signing_data,
            });
        }
        // ENCRYPTION_CAPABILITIES too, with cipher 0 when none is in common.
        if let Some(cipher) = client_contexts.cipher {
            let encryption_caps = EncryptionCapabilities {
                cipher_count: 1,
                ciphers: vec![cipher.map_or(0, Cipher::id)],
            };
            let encryption_data = {
                use binrw::BinWrite;
                let mut c = std::io::Cursor::new(Vec::new());
                BinWrite::write(&encryption_caps, &mut c)
                    .expect("encryption negotiate context encodes");
                c.into_inner()
            };
            ctxs.push(NegotiateContext {
                context_type: NegotiateContext::TYPE_ENCRYPTION,
                data_length: encryption_data.len() as u16,
                reserved: 0,
                data: encryption_data,
            });
        }
        if let Err(e) = NegotiateContext::encode_list(&ctxs, &mut contexts_bytes) {
            tracing::error!(error = %e, "encode_list failed");
            return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER);
        }
        context_count = ctxs.len() as u16;

        // The contexts go after security buffer, 8-byte aligned.
        let post_security = security_buffer_offset as u32 + security_buffer_length as u32;
        // Round up to next multiple of 8 from the start of the SMB2 header.
        negotiate_context_offset = (post_security + 7) & !7;
    }

    let max_read_size = *conn.max_read_size.read().await;
    let max_write_size = *conn.max_write_size.read().await;
    let max_transact_size = max_read_size;

    let resp = NegotiateResponse {
        structure_size: 65,
        security_mode: server_security_mode(server),
        dialect_revision: chosen,
        negotiate_context_count_or_reserved: context_count,
        server_guid: *server.config.server_guid.as_bytes(),
        capabilities: server_capabilities(dialect, req.capabilities),
        max_transact_size,
        max_read_size,
        max_write_size,
        system_time: now_filetime(),
        server_start_time: 0,
        security_buffer_offset,
        security_buffer_length,
        negotiate_context_offset_or_reserved2: negotiate_context_offset,
        security_buffer: security_blob,
    };

    let mut body_out = Vec::new();
    if let Err(e) = resp.write_to(&mut body_out) {
        tracing::error!(error = %e, "encode NEGOTIATE response");
        return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER);
    }
    // Append padding to align contexts at `negotiate_context_offset`.
    if dialect == Dialect::Smb311 && context_count > 0 {
        let cur = 64 + body_out.len() as u32; // header + body so far
        if cur < negotiate_context_offset {
            let pad = (negotiate_context_offset - cur) as usize;
            body_out.extend(std::iter::repeat_n(0u8, pad));
        }
        body_out.extend_from_slice(&contexts_bytes);
    }
    info!(?dialect, "NEGOTIATE complete");
    let mut hr = HandlerResponse::ok(body_out);
    hr.skip_signing = true;
    hr
}

/// The greatest dialect in `offered` that the server implements.
pub(crate) fn select_dialect(offered: &[u16]) -> Option<Dialect> {
    const SUPPORTED: &[u16] = &[0x0202, 0x0210, 0x0300, 0x0302, 0x0311];
    offered
        .iter()
        .copied()
        .filter(|d| SUPPORTED.contains(d))
        .max()
        .and_then(Dialect::from_u16)
}

/// Build the SMB2 NEGOTIATE response sent in reply to an SMB1 multi-protocol
/// NEGOTIATE_REQUEST that listed an SMB2 dialect (MS-SMB2 ยง3.3.5.3.1).
///
/// Leaves the connection dialect unset. The client follows up with an SMB2
/// NEGOTIATE, which goes through [`handle`].
pub async fn multi_protocol_response(
    server: &Arc<ServerState>,
    conn: &Arc<Connection>,
    chosen: u16,
) -> HandlerResponse {
    let security_blob = encode_init_response();
    let security_buffer_offset: u16 = 64 + 64;
    let security_buffer_length: u16 = security_blob.len() as u16;
    let max_read_size = *conn.max_read_size.read().await;
    let max_write_size = *conn.max_write_size.read().await;
    let max_transact_size = max_read_size;

    let resp = NegotiateResponse {
        structure_size: 65,
        security_mode: server_security_mode(server),
        dialect_revision: chosen,
        negotiate_context_count_or_reserved: 0,
        server_guid: *server.config.server_guid.as_bytes(),
        // MS-SMB2 3.3.5.3.1: the "SMB 2.???" reply advertises leasing.
        capabilities: if chosen == Dialect::Smb2Wildcard.as_u16() {
            CAP_LEASING
        } else {
            0
        },
        max_transact_size,
        max_read_size,
        max_write_size,
        system_time: now_filetime(),
        server_start_time: 0,
        security_buffer_offset,
        security_buffer_length,
        negotiate_context_offset_or_reserved2: 0,
        security_buffer: security_blob,
    };

    let mut body_out = Vec::new();
    if let Err(e) = resp.write_to(&mut body_out) {
        tracing::error!(error = %e, "encode multi-protocol NEGOTIATE response");
        return HandlerResponse::err(ntstatus::STATUS_INVALID_PARAMETER);
    }
    info!(
        chosen = %format_args!("0x{chosen:04X}"),
        "SMB1 multi-protocol -> SMB2"
    );
    let mut hr = HandlerResponse::ok(body_out);
    hr.skip_signing = true;
    hr
}