smb-server 0.12.1

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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! The HTTP client the S3 requests go through.

use std::future::Future;
use std::io;
use std::pin::{Pin, pin};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::task::{Context, Poll, ready};
use std::time::Duration;

use bytes::Bytes;
use http::Uri;
use hyper::body::{Body, Frame, SizeHint};
use hyper_rustls::{HttpsConnector, HttpsConnectorBuilder};
use hyper_util::client::legacy::Client;
use hyper_util::client::legacy::connect::HttpConnector;
use hyper_util::rt::{TokioExecutor, TokioTimer};
use rustls::crypto::ring::default_provider;
use rustls::{ClientConfig, RootCertStore};
use tokio::time::Instant;
use tower_service::Service;
use tracing::warn;

use super::error::BoxError;
use super::payload::{BODY_CHUNK, BodyError, Fault, FileBody, Payload, Source};

/// Longest a new connection may take, name lookup and TLS handshake included.
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);

/// Most bytes hyper buffers for a connection, for writing and for reading. It
/// polls a request body only while its write buffer is below this, so the data
/// queued behind the last piece the peer took is two pieces at most.
const MAX_BUFFER: usize = BODY_CHUNK;

/// A pooled HTTP/1.1 client for `https://` and `http://` endpoints.
pub(crate) type HttpClient = Client<ConnectTimeout<HttpsConnector<HttpConnector>>, RequestBody>;

/// A client that verifies servers against the platform's root certificates.
/// TLS runs on the ring provider, whatever the process default is. Fails
/// when the platform has no usable root certificate.
pub(crate) fn client() -> io::Result<HttpClient> {
    client_with(CONNECT_TIMEOUT)
}

fn client_with(connect_timeout: Duration) -> io::Result<HttpClient> {
    let config = ClientConfig::builder_with_provider(Arc::new(default_provider()))
        .with_safe_default_protocol_versions()
        .map_err(io::Error::other)?
        .with_root_certificates(platform_roots()?)
        .with_no_client_auth();
    let mut http = HttpConnector::new();
    http.enforce_http(false);
    let connector = HttpsConnectorBuilder::new()
        .with_tls_config(config)
        .https_or_http()
        .enable_http1()
        .wrap_connector(http);
    Ok(Client::builder(TokioExecutor::new())
        .pool_timer(TokioTimer::new())
        .http1_max_buf_size(MAX_BUFFER)
        .build(ConnectTimeout {
            inner: connector,
            limit: connect_timeout,
        }))
}

/// A connector that fails a connection not finished within `limit`.
#[derive(Clone)]
pub(crate) struct ConnectTimeout<S> {
    inner: S,
    limit: Duration,
}

impl<S> Service<Uri> for ConnectTimeout<S>
where
    S: Service<Uri>,
    S::Response: Send + 'static,
    S::Error: Into<BoxError>,
    S::Future: Send + 'static,
{
    type Response = S::Response;
    type Error = BoxError;
    type Future = Pin<Box<dyn Future<Output = Result<S::Response, BoxError>> + Send>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), BoxError>> {
        self.inner.poll_ready(cx).map_err(Into::into)
    }

    fn call(&mut self, uri: Uri) -> Self::Future {
        let connecting = self.inner.call(uri);
        let limit = self.limit;
        Box::pin(async move {
            match tokio::time::timeout(limit, connecting).await {
                Ok(connected) => connected.map_err(Into::into),
                Err(_) => Err(io::Error::new(
                    io::ErrorKind::TimedOut,
                    format!("no connection within {limit:?}"),
                )
                .into()),
            }
        })
    }
}

/// When a request last moved data. The body that sends the request and the
/// code that waits for the answer share one.
#[derive(Clone, Debug)]
pub(crate) struct Activity(Arc<ActivityClock>);

#[derive(Debug)]
struct ActivityClock {
    start: Instant,
    /// Milliseconds after `start` of the last activity.
    last: AtomicU64,
}

impl Activity {
    /// An activity that last moved data just now.
    pub(crate) fn new() -> Self {
        Activity(Arc::new(ActivityClock {
            start: Instant::now(),
            last: AtomicU64::new(0),
        }))
    }

    /// Notes that data moved just now.
    pub(crate) fn touch(&self) {
        let elapsed = self.0.start.elapsed().as_millis();
        self.0.last.store(
            u64::try_from(elapsed).unwrap_or(u64::MAX),
            Ordering::Relaxed,
        );
    }

    fn last(&self) -> Instant {
        self.0.start + Duration::from_millis(self.0.last.load(Ordering::Relaxed))
    }

    /// Waits for `work`. Gives up with `None` once `idle` has passed since the
    /// last [`touch`](Self::touch) and `work` is still pending.
    pub(crate) async fn watch<F: Future>(&self, idle: Duration, work: F) -> Option<F::Output> {
        let mut work = pin!(work);
        loop {
            tokio::select! {
                biased;
                output = &mut work => return Some(output),
                () = tokio::time::sleep_until(self.last() + idle) => {
                    if self.last() + idle <= Instant::now() {
                        return None;
                    }
                }
            }
        }
    }
}

/// The body of a request. It gives its bytes to the connection in pieces, and
/// the connection asks for the next piece only after it has written the
/// previous ones, so polls stop while the peer takes no data. Each piece marks
/// the [`Activity`] of the request. Once the body is [cut](Self::cut_on_drop),
/// a poll that still has bytes to give fails, which ends the connection.
pub(crate) struct RequestBody {
    kind: Kind,
    activity: Activity,
    fault: Fault,
    cut: Arc<AtomicBool>,
}

enum Kind {
    Memory(Bytes),
    File(FileBody),
}

impl RequestBody {
    /// A body that sends `payload` from its start.
    pub(crate) fn new(payload: &Payload, activity: Activity) -> Self {
        let fault = Fault::default();
        let kind = match &payload.source {
            Source::Memory(data) => Kind::Memory(data.clone()),
            Source::File(range) => Kind::File(FileBody::new(
                range.clone(),
                payload.sha256(),
                fault.clone(),
            )),
        };
        RequestBody {
            kind,
            activity,
            fault,
            cut: Arc::new(AtomicBool::new(false)),
        }
    }

    /// The error that stopped this body, if one did. Where hyper fails a
    /// request for it, this says why.
    pub(crate) fn fault(&self) -> Fault {
        self.fault.clone()
    }

    /// A guard that cuts this body when it is dropped. A store may answer
    /// before it has read the whole body, and the connection goes on sending
    /// the rest, with the file open, after the call that waited for the answer
    /// has returned.
    pub(crate) fn cut_on_drop(&self) -> CutOnDrop {
        CutOnDrop(self.cut.clone())
    }

    /// The bytes not handed out yet.
    fn remaining(&self) -> u64 {
        match &self.kind {
            Kind::Memory(rest) => rest.len() as u64,
            Kind::File(file) => file.remaining(),
        }
    }
}

/// Cuts a [`RequestBody`] when dropped.
pub(crate) struct CutOnDrop(Arc<AtomicBool>);

impl Drop for CutOnDrop {
    fn drop(&mut self) {
        self.0.store(true, Ordering::Release);
    }
}

impl Body for RequestBody {
    type Data = Bytes;
    type Error = BodyError;

    fn poll_frame(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Bytes>, BodyError>>> {
        if self.cut.load(Ordering::Acquire) && self.remaining() > 0 {
            return Poll::Ready(Some(Err(BodyError)));
        }
        let this = &mut *self;
        let piece = match &mut this.kind {
            Kind::Memory(rest) => {
                this.activity.touch();
                if rest.is_empty() {
                    return Poll::Ready(None);
                }
                let len = rest.len().min(BODY_CHUNK);
                Some(Ok(rest.split_to(len)))
            }
            Kind::File(file) => {
                // A poll that finds a read still running is not progress.
                if !file.is_reading() {
                    this.activity.touch();
                }
                let piece = ready!(file.poll_piece(cx));
                this.activity.touch();
                piece
            }
        };
        Poll::Ready(piece.map(|piece| piece.map(Frame::data)))
    }

    fn is_end_stream(&self) -> bool {
        self.remaining() == 0
    }

    fn size_hint(&self) -> SizeHint {
        SizeHint::with_exact(self.remaining())
    }
}

fn platform_roots() -> io::Result<RootCertStore> {
    let loaded = rustls_native_certs::load_native_certs();
    let mut roots = RootCertStore::empty();
    let (added, ignored) = roots.add_parsable_certificates(loaded.certs);
    if ignored > 0 || !loaded.errors.is_empty() {
        warn!(
            added,
            ignored,
            errors = ?loaded.errors,
            "some platform root certificates could not be loaded"
        );
    }
    if added == 0 {
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            "no usable root certificates in the platform store",
        ));
    }
    Ok(roots)
}

#[cfg(test)]
mod tests {
    use std::error::Error;
    use std::io::Write as _;
    use std::iter;

    use http::Request;
    use http_body_util::BodyExt as _;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;

    use sha2::{Digest as _, Sha256};

    use super::super::encode::hex;
    use super::*;

    fn memory(data: Bytes, activity: Activity) -> RequestBody {
        RequestBody::new(&Payload::from(data), activity)
    }

    fn get(url: String) -> Request<RequestBody> {
        let body = memory(Bytes::new(), Activity::new());
        Request::get(url).body(body).unwrap()
    }

    #[tokio::test]
    async fn a_connection_that_never_completes_times_out() {
        // Nothing accepts on the listener, so the handshake gets no reply.
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let request = get(format!("https://{}/", listener.local_addr().unwrap()));
        let client = client_with(Duration::from_millis(300)).unwrap();
        let error = tokio::time::timeout(Duration::from_secs(2), client.request(request))
            .await
            .expect("the request outlived the connect timeout")
            .unwrap_err();
        let timed_out = iter::successors(error.source(), |e| (*e).source()).any(|e| {
            e.downcast_ref::<io::Error>()
                .is_some_and(|e| e.kind() == io::ErrorKind::TimedOut)
        });
        assert!(timed_out, "{error:?}");
    }

    #[tokio::test]
    async fn an_https_request_reaches_the_tls_layer() {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let request = get(format!("https://{}/", listener.local_addr().unwrap()));
        tokio::spawn(async move {
            let (mut conn, _) = listener.accept().await.unwrap();
            let mut hello = [0; 4096];
            let _ = conn.read(&mut hello).await;
            let _ = conn.write_all(b"HTTP/1.1 400 Bad Request\r\n\r\n").await;
            let _ = conn.shutdown().await;
            let _ = conn.read(&mut hello).await;
        });
        let client = client().unwrap();
        let error = tokio::time::timeout(Duration::from_secs(5), client.request(request))
            .await
            .expect("the request got no answer")
            .unwrap_err();
        assert!(caused_by_rustls(&error), "{error:?}");
    }

    /// Whether `rustls::Error` is in the chain of `error`. `io::Error` hides
    /// the error it wraps from `source`, so that one is read with `get_ref`.
    fn caused_by_rustls(error: &(dyn Error + 'static)) -> bool {
        error.is::<rustls::Error>()
            || error
                .downcast_ref::<io::Error>()
                .and_then(io::Error::get_ref)
                .is_some_and(|inner| caused_by_rustls(inner))
            || error.source().is_some_and(caused_by_rustls)
    }

    #[tokio::test(start_paused = true)]
    async fn a_request_body_hands_out_its_bytes_in_pieces() {
        let data = Bytes::from(
            (0..=255)
                .cycle()
                .take(BODY_CHUNK * 2 + 10)
                .collect::<Vec<u8>>(),
        );
        let mut body = memory(data.clone(), Activity::new());
        assert_eq!(body.size_hint().exact(), Some(data.len() as u64));
        assert!(!body.is_end_stream());
        let mut sizes = Vec::new();
        let mut received = Vec::new();
        while let Some(frame) = body.frame().await {
            let piece = frame.unwrap().into_data().unwrap();
            sizes.push(piece.len());
            received.extend_from_slice(&piece);
            assert_eq!(
                body.size_hint().exact(),
                Some((data.len() - received.len()) as u64)
            );
        }
        assert_eq!(sizes, [BODY_CHUNK, BODY_CHUNK, 10]);
        assert_eq!(received, data);
        assert!(body.is_end_stream());
    }

    #[tokio::test(start_paused = true)]
    async fn an_empty_request_body_ends_at_once() {
        let mut body = memory(Bytes::new(), Activity::new());
        assert!(body.is_end_stream());
        assert_eq!(body.size_hint().exact(), Some(0));
        assert!(body.frame().await.is_none());
    }

    #[tokio::test(start_paused = true)]
    async fn a_cut_body_fails_while_it_has_bytes_left() {
        let mut body = memory(Bytes::from(vec![7; BODY_CHUNK + 1]), Activity::new());
        let cut = body.cut_on_drop();
        body.frame().await.unwrap().unwrap();
        drop(cut);
        assert!(body.frame().await.unwrap().is_err());
        assert!(body.frame().await.unwrap().is_err());
    }

    #[tokio::test]
    async fn a_cut_file_body_fails_without_a_cause_of_its_own() {
        let contents = vec![9; BODY_CHUNK * 2];
        let mut body = RequestBody::new(&file_payload(&contents), Activity::new());
        let cut = body.cut_on_drop();
        body.frame().await.unwrap().unwrap();
        drop(cut);
        assert!(body.frame().await.unwrap().is_err());
        assert!(body.fault().get().is_none());
    }

    #[tokio::test(start_paused = true)]
    async fn a_cut_body_with_nothing_left_ends_as_usual() {
        let mut body = memory(Bytes::from_static(b"data"), Activity::new());
        let cut = body.cut_on_drop();
        body.frame().await.unwrap().unwrap();
        drop(cut);
        assert!(body.is_end_stream());
        assert!(body.frame().await.is_none());
    }

    #[tokio::test(start_paused = true)]
    async fn a_body_is_not_cut_while_its_guard_lives() {
        let mut body = memory(Bytes::from(vec![7; BODY_CHUNK + 1]), Activity::new());
        let _cut = body.cut_on_drop();
        body.frame().await.unwrap().unwrap();
        body.frame().await.unwrap().unwrap();
        assert!(body.frame().await.is_none());
    }

    #[tokio::test(start_paused = true)]
    async fn sending_a_piece_marks_activity() {
        let activity = Activity::new();
        let started = activity.last();
        let mut body = memory(Bytes::from_static(b"data"), activity.clone());
        tokio::time::sleep(Duration::from_secs(7)).await;
        assert_eq!(activity.last(), started);
        body.frame().await.unwrap().unwrap();
        assert_eq!(activity.last(), started + Duration::from_secs(7));
    }

    #[tokio::test(start_paused = true)]
    async fn work_that_moves_no_data_is_given_up_after_the_idle_time() {
        let activity = Activity::new();
        let began = Instant::now();
        let outcome = activity
            .watch(
                Duration::from_secs(5),
                tokio::time::sleep(Duration::from_secs(60)),
            )
            .await;
        assert!(outcome.is_none());
        assert_eq!(began.elapsed(), Duration::from_secs(5));
    }

    #[tokio::test(start_paused = true)]
    async fn work_that_keeps_moving_data_may_run_past_the_idle_time() {
        let activity = Activity::new();
        let ticker = activity.clone();
        let began = Instant::now();
        let outcome = activity
            .watch(Duration::from_secs(5), async move {
                for _ in 0..4 {
                    tokio::time::sleep(Duration::from_secs(4)).await;
                    ticker.touch();
                }
                "done"
            })
            .await;
        assert_eq!(outcome, Some("done"));
        assert_eq!(began.elapsed(), Duration::from_secs(16));
    }

    #[tokio::test(start_paused = true)]
    async fn work_that_stops_moving_data_is_given_up_after_the_last_activity() {
        let activity = Activity::new();
        let ticker = activity.clone();
        let began = Instant::now();
        let outcome = activity
            .watch(Duration::from_secs(5), async move {
                tokio::time::sleep(Duration::from_secs(3)).await;
                ticker.touch();
                tokio::time::sleep(Duration::from_secs(60)).await;
            })
            .await;
        assert!(outcome.is_none());
        assert_eq!(began.elapsed(), Duration::from_secs(8));
    }

    fn file_payload(contents: &[u8]) -> Payload {
        let mut file = tempfile::tempfile().unwrap();
        file.write_all(contents).unwrap();
        let hash = hex(&Sha256::digest(contents));
        Payload::file(Arc::new(file), 0, contents.len() as u64, &hash).unwrap()
    }

    #[tokio::test]
    async fn a_file_request_body_hands_out_its_bytes_in_pieces() {
        let contents: Vec<u8> = (0..=250).cycle().take(BODY_CHUNK + 10).collect();
        let mut body = RequestBody::new(&file_payload(&contents), Activity::new());
        assert_eq!(body.size_hint().exact(), Some(contents.len() as u64));
        assert!(!body.is_end_stream());
        let mut received = Vec::new();
        while let Some(frame) = body.frame().await {
            received.extend_from_slice(&frame.unwrap().into_data().unwrap());
        }
        assert_eq!(received, contents);
        assert!(body.is_end_stream());
        assert!(body.fault().get().is_none());
    }

    #[tokio::test]
    async fn reading_a_piece_of_a_file_marks_activity() {
        let activity = Activity::new();
        let mut body = RequestBody::new(&file_payload(b"data"), activity.clone());
        let before = activity.last();
        tokio::time::sleep(Duration::from_millis(30)).await;
        body.frame().await.unwrap().unwrap();
        assert!(activity.last() >= before + Duration::from_millis(30));
    }
}