smb 0.11.2

A Pure Rust SMB Client implementation
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use crate::session::{SessionAndChannel, SessionInfo};
use crate::sync_helpers::*;
use crate::{compression::*, msg_handler::*};
use binrw::prelude::*;
use maybe_async::*;
use smb_msg::*;
use smb_transport::IoVec;
use std::{collections::HashMap, io::Cursor, sync::Arc};

use super::connection_info::ConnectionInfo;

/// The [`Transformer`] structure is responsible for transforming messages to and from bytes,
/// send over NetBios TCP connection.
///
/// See [`Transformer::transform_outgoing`] and [`Transformer::transform_incoming`] for transformation functions.
#[derive(Default)]
pub struct Transformer {
    /// Sessions opened from this connection.
    // This structure is performance-critical, so it uses RwLock to allow concurrent reads.
    // Writes are only done when a session is started or ended - which is *very* rare in high-performance scenarios.
    sessions: RwLock<HashMap<u64, Arc<RwLock<SessionAndChannel>>>>,

    config: RwLock<TransformerConfig>,
}

#[derive(Default, Debug)]
struct TransformerConfig {
    /// Compressors for this connection.
    compress: Option<(Compressor, Decompressor)>,

    negotiated: bool,
}

#[maybe_async(AFIT)]
impl Transformer {
    /// Notifies that the connection negotiation has been completed,
    /// with the given [`ConnectionInfo`].
    pub async fn negotiated(&self, neg_info: &ConnectionInfo) -> crate::Result<()> {
        {
            let config = self.config.read().await?;
            if config.negotiated {
                return Err(crate::Error::InvalidState(
                    "Connection is already negotiated!".into(),
                ));
            }
        }

        let mut config = self.config.write().await?;
        if neg_info.dialect.supports_compression() && neg_info.config.compression_enabled {
            let compress = neg_info
                .negotiation
                .compression
                .as_ref()
                .map(|c| (Compressor::new(c), Decompressor::new(c)));
            config.compress = compress;
        }

        config.negotiated = true;

        Ok(())
    }

    /// Notifies that a session has started.
    pub async fn session_started(
        &self,
        session: &Arc<RwLock<SessionAndChannel>>,
    ) -> crate::Result<()> {
        let rconfig = self.config.read().await?;
        if !rconfig.negotiated {
            return Err(crate::Error::InvalidState(
                "Connection is not negotiated yet!".to_string(),
            ));
        }

        let session_id = { session.read().await?.session_id };
        self.sessions
            .write()
            .await?
            .insert(session_id, session.clone());

        log::trace!(
            "Session {} started and inserted to worker {:p}.",
            session_id,
            self
        );

        Ok(())
    }

    /// Notifies that a session has ended.
    pub async fn session_ended(
        &self,
        session: &Arc<RwLock<SessionAndChannel>>,
    ) -> crate::Result<()> {
        let session_id = { session.read().await?.session_id };
        self.sessions
            .write()
            .await?
            .remove(&session_id)
            .ok_or(crate::Error::InvalidState(format!(
                "Session {session_id} not found!",
            )))?;

        log::trace!(
            "Session {} ended and removed from worker {:p}.",
            session_id,
            self
        );

        Ok(())
    }

    /// (Internal)
    ///
    /// Locates the current channel per the provded session ID,
    /// and invokes the provided closure with the channel information.
    ///
    /// Note: this function WILL deadlock if any lock attempt is performed within the closure on `self.sessions`.
    #[maybe_async]
    #[inline]
    async fn _with_channel<F, R>(&self, session_id: u64, f: F) -> crate::Result<R>
    where
        F: FnOnce(&SessionAndChannel) -> crate::Result<R>,
    {
        let sessions = self.sessions.read().await?;
        let session = sessions
            .get(&session_id)
            .ok_or(crate::Error::InvalidState(format!(
                "Session {session_id} not found!",
            )))?;
        let session = session.read().await?;
        f(&session)
    }

    /// (Internal)
    ///
    /// Locates the current session per the provided session ID,
    /// and invokes the provided closure with the session information.
    ///
    /// Note: this function WILL deadlock if any lock attempt is performed within the closure on `self.sessions`.
    #[maybe_async]
    #[inline]
    async fn _with_session<F, R>(&self, session_id: u64, f: F) -> crate::Result<R>
    where
        F: FnOnce(&SessionInfo) -> crate::Result<R>,
    {
        let sessions = self.sessions.read().await?;
        let session = sessions
            .get(&session_id)
            .ok_or(crate::Error::InvalidState(format!(
                "Session {session_id} not found!",
            )))?;
        let session = session.read().await?;
        let session_info = session.session.read().await?;
        f(&session_info)
    }

    /// Transforms an outgoing message to a raw SMB message.
    pub async fn transform_outgoing(&self, mut msg: OutgoingMessage) -> crate::Result<IoVec> {
        let should_encrypt = msg.encrypt;
        let should_sign = msg.message.header.flags.signed();
        let session_id = msg.message.header.session_id;

        let mut outgoing_data = IoVec::default();
        // Plain header + content
        {
            let buffer = outgoing_data.add_owned(Vec::with_capacity(Header::STRUCT_SIZE));
            msg.message.write(&mut Cursor::new(buffer))?;
        }
        // Additional data, if any
        if msg.additional_data.as_ref().is_some_and(|d| !d.is_empty()) {
            outgoing_data.add_shared(msg.additional_data.unwrap().clone());
        }

        // 1. Sign
        if should_sign {
            debug_assert!(
                !should_encrypt,
                "Should not sign and encrypt at the same time!"
            );

            let mut signer = self
                ._with_channel(session_id, |session| {
                    let channel_info =
                        session
                            .channel
                            .as_ref()
                            .ok_or(crate::Error::TranformFailed(TransformError {
                                outgoing: true,
                                phase: TransformPhase::SignVerify,
                                session_id: Some(session_id),
                                why: "Message is required to be signed, but no channel is set up!",
                                msg_id: Some(msg.message.header.message_id),
                            }))?;

                    Ok(channel_info.signer()?.clone())
                })
                .await?;

            signer.sign_message(&mut msg.message.header, &mut outgoing_data)?;

            log::debug!(
                "Message #{} signed (signature={}).",
                msg.message.header.message_id,
                msg.message.header.signature
            );
        };

        // 2. Compress
        const COMPRESSION_THRESHOLD: usize = 1024;
        outgoing_data = {
            if msg.compress && outgoing_data.total_size() > COMPRESSION_THRESHOLD {
                let rconfig = self.config.read().await?;
                if let Some(compress) = &rconfig.compress {
                    // Build a vector of the entire data. In the future, this may be optimized to avoid copying.
                    // currently, there's not chained compression, and copy will occur anyway.
                    outgoing_data.consolidate();
                    let compressed = compress.0.compress(outgoing_data.first().unwrap())?;

                    let mut compressed_result = IoVec::default();
                    let write_compressed =
                        compressed_result.add_owned(Vec::with_capacity(compressed.total_size()));
                    compressed.write(&mut Cursor::new(write_compressed))?;
                    compressed_result
                } else {
                    outgoing_data
                }
            } else {
                outgoing_data
            }
        };

        // 3. Encrypt
        if should_encrypt {
            let mut encryptor = self
                ._with_session(session_id, |session| {
                    let encryptor = session.encryptor()?.ok_or(crate::Error::TranformFailed(
                        TransformError {
                            outgoing: true,
                            phase: TransformPhase::EncryptDecrypt,
                            session_id: Some(session_id),
                            why: "Message is required to be encrypted, but no encryptor is set up!",
                            msg_id: Some(msg.message.header.message_id),
                        },
                    ))?;
                    Ok(encryptor.clone())
                })
                .await?;

            debug_assert!(should_encrypt && !should_sign);

            let encrypted_header = encryptor.encrypt_message(&mut outgoing_data, session_id)?;

            let write_encryption_header =
                outgoing_data.insert_owned(0, Vec::with_capacity(EncryptedHeader::STRUCTURE_SIZE));

            encrypted_header.write(&mut Cursor::new(write_encryption_header))?;
        }

        Ok(outgoing_data)
    }

    /// Transforms an incoming message buffer to an [`IncomingMessage`].
    pub async fn transform_incoming(&self, data: Vec<u8>) -> crate::Result<IncomingMessage> {
        let message = Response::try_from(data.as_ref())?;

        let mut form = MessageForm::default();

        // 3. Decrpt
        let (message, raw) = if let Response::Encrypted(encrypted_message) = message {
            let session_id = encrypted_message.header.session_id;

            let mut decryptor = self
                ._with_session(session_id, |session| {
                    let decryptor = session.decryptor()?.ok_or(crate::Error::TranformFailed(
                        TransformError {
                            outgoing: false,
                            phase: TransformPhase::EncryptDecrypt,
                            session_id: Some(session_id),
                            why: "Message is required to be encrypted, but no decryptor is set up!",
                            msg_id: None,
                        },
                    ))?;
                    Ok(decryptor.clone())
                })
                .await?;
            form.encrypted = true;
            decryptor.decrypt_message(encrypted_message)?
        } else {
            (message, data)
        };

        // 2. Decompress
        debug_assert!(!matches!(message, Response::Encrypted(_)));
        let (message, raw) = if let Response::Compressed(compressed_message) = message {
            let rconfig = self.config.read().await?;
            form.compressed = true;
            match &rconfig.compress {
                Some(compress) => compress.1.decompress(&compressed_message)?,
                None => {
                    return Err(crate::Error::TranformFailed(TransformError {
                        outgoing: false,
                        phase: TransformPhase::CompressDecompress,
                        session_id: None,
                        why: "Compression is requested, but no decompressor is set up!",
                        msg_id: None,
                    }));
                }
            }
        } else {
            (message, raw)
        };

        let mut message = match message {
            Response::Plain(message) => message,
            _ => panic!("Unexpected message type"),
        };

        let iovec = IoVec::from(raw);
        // If fails, return TranformFailed, with message id.
        // this allows to notify the error to the task that was waiting for this message.
        match self
            .verify_plain_incoming(&mut message, &iovec, &mut form)
            .await
        {
            Ok(_) => {}
            Err(e) => {
                log::error!("Failed to verify incoming message: {e:?}",);
                return Err(crate::Error::TranformFailed(TransformError {
                    outgoing: false,
                    phase: TransformPhase::SignVerify,
                    session_id: Some(message.header.session_id),
                    why: "Failed to verify incoming message!",
                    msg_id: Some(message.header.message_id),
                }));
            }
        };

        Ok(IncomingMessage::new(message, iovec, form))
    }

    /// (Internal)
    ///
    /// A helper method to verify the incoming message.
    /// This method is used to verify the signature of the incoming message,
    /// if such verification is required.
    #[maybe_async]
    async fn verify_plain_incoming(
        &self,
        message: &mut PlainResponse,
        raw: &IoVec,
        form: &mut MessageForm,
    ) -> crate::Result<()> {
        // Check if signing check is required.
        if form.encrypted
            || message.header.message_id == u64::MAX
            || message.header.status == Status::Pending as u32
            || !(message.header.flags.signed() || self.is_message_signed_ksmbd(message).await)
        {
            return Ok(());
        }

        // Verify signature (if required, according to the spec)
        let session_id = message.header.session_id;
        let mut signer = self
            ._with_channel(session_id, |session| {
                let channel_info = session
                    .channel
                    .as_ref()
                    .ok_or(crate::Error::TranformFailed(TransformError {
                        outgoing: false,
                        phase: TransformPhase::SignVerify,
                        session_id: Some(session_id),
                        why: "Message is required to be signed, but no channel is set up!",
                        msg_id: Some(message.header.message_id),
                    }))?;

                Ok(channel_info.signer()?.clone())
            })
            .await?;

        signer.verify_signature(&mut message.header, raw)?;
        log::debug!(
            "Message #{} verified (signature={}).",
            message.header.message_id,
            message.header.signature
        );
        form.signed = true;
        Ok(())
    }

    /// (Internal)
    ///
    /// ksmbd multichannel setup compatibility check.
    ///
    // ksmbd has a subtle, but irritating bug, where it does not set the "signed" flag
    // for responses during multi channel session setups. To resolve this, we check if the
    // current channel is defined as "binding-only" channel. The feature `ksmbd-multichannel-compat`
    // must also be enabled, or else this code will not be compiled.
    // This behavior is actually against the spec - MS-SMB2 3.2.4.1.1:
    // > "If the client signs the request, it MUST set the SMB2_FLAGS_SIGNED bit in the Flags field of the SMB2 header."
    #[maybe_async]
    async fn is_message_signed_ksmbd(&self, _message: &PlainResponse) -> bool {
        #[cfg(feature = "ksmbd-multichannel-compat")]
        {
            if _message.header.command != Command::SessionSetup || _message.header.signature == 0 {
                return false;
            }

            let session_id = _message.header.session_id;
            let is_binding = self
                ._with_channel(session_id, |session| {
                    let channel_info = session.channel.as_ref().ok_or(crate::Error::Other(
                        "Get channel info for ksmbd sign test failed",
                    ))?;

                    Ok(channel_info.is_binding())
                })
                .await;

            return matches!(is_binding, Ok(true));
        }

        #[cfg(not(feature = "ksmbd-multichannel-compat"))]
        return false;
    }
}

/// An error that can occur during the transformation of messages.
#[derive(Debug)]
pub struct TransformError {
    /// If true, the error occurred while transforming an outgoing message.
    /// If false, it occurred while transforming an incoming message.
    pub outgoing: bool,
    pub phase: TransformPhase,
    pub session_id: Option<u64>,
    pub why: &'static str,
    /// If a message ID is available, it will be set here,
    /// for error-handling purposes.
    pub msg_id: Option<u64>,
}

impl std::fmt::Display for TransformError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.outgoing {
            write!(
                f,
                "Failed to transform outgoing message: {:?} (session_id: {:?}) - {}",
                self.phase, self.session_id, self.why
            )
        } else {
            write!(
                f,
                "Failed to transform incoming message: {:?} (session_id: {:?}) - {}",
                self.phase, self.session_id, self.why
            )
        }
    }
}

/// The phase of the transformation process.
#[derive(Debug)]
pub enum TransformPhase {
    /// Initial to/from bytes.
    EncodeDecode,
    /// Signature calculation and verification.
    SignVerify,
    /// Compression and decompression.
    CompressDecompress,
    /// Encryption and decryption.
    EncryptDecrypt,
}