rusftp 0.2.0-beta-1

SFTP library based on russh
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
// This file is part of the rusftp project
//
// Copyright (C) ANEO, 2024-2024. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Collection of all SFTP messages.
//!
//! This module defines all the SFTP messages defined by the SFTP protocol RFC version 3.
//! See: <https://datatracker.ietf.org/doc/html/draft-ietf-secsh-filexfer-02>
//!
//! Each message has its own type, and the type [`Message`] is an `enum` of all messages

use std::borrow::Cow;

use bytes::{Buf, BufMut, Bytes};
use serde::{ser::SerializeTuple, Deserialize, Serialize};

use crate::wire::{Error, SftpDecoder, SftpEncoder};

mod attrs;
mod close;
mod data;
mod extended;
mod extended_reply;
mod fsetstat;
mod fstat;
mod handle;
mod init;
mod lstat;
mod mkdir;
mod name;
mod open;
mod opendir;
mod path;
mod read;
mod readdir;
mod readlink;
mod realpath;
mod remove;
mod rename;
mod rmdir;
mod setstat;
mod stat;
mod status;
mod symlink;
mod version;
mod write;

#[cfg(test)]
mod test_utils;

pub use attrs::{Attrs, Owner, Permisions, Time};
pub use close::Close;
pub use data::Data;
pub use extended::Extended;
pub use extended_reply::ExtendedReply;
pub use fsetstat::FSetStat;
pub use fstat::FStat;
pub use handle::Handle;
pub use init::Init;
pub use lstat::LStat;
pub use mkdir::MkDir;
pub use name::{Name, NameEntry};
pub use open::{Open, PFlags};
pub use opendir::OpenDir;
pub use path::Path;
pub use read::Read;
pub use readdir::ReadDir;
pub use readlink::ReadLink;
pub use realpath::RealPath;
pub use remove::Remove;
pub use rename::Rename;
pub use rmdir::RmDir;
pub use setstat::SetStat;
pub use stat::Stat;
pub use status::{Status, StatusCode};
pub use symlink::Symlink;
pub use version::Version;
pub use write::Write;

macro_rules! messages {
    ($($name:ident: $discriminant:expr)*) => {
        #[derive(Debug, PartialEq, Eq, Clone)]
        #[repr(u8)]
        #[non_exhaustive]
        pub enum Message {
            $($name($name) = $discriminant,)*
        }

        #[derive(Debug, PartialEq, Eq, Clone, Copy)]
        #[repr(u8)]
        #[non_exhaustive]
        pub enum MessageKind {
            $($name = $discriminant,)*
        }

        impl Message {
            pub fn kind(&self) -> MessageKind {
                match self {
                    $(Self::$name(_) => MessageKind::$name,)*
                }
            }
        }

        impl MessageKind {
            pub fn code(&self) -> u8 {
                match self {
                    $(Self::$name => $discriminant,)*
                }
            }
        }

        impl From<Message> for MessageKind {
            fn from(value: Message) -> Self {
                value.kind()
            }
        }

        impl Serialize for Message {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {

                let mut state = serializer.serialize_tuple(2)?;
                state.serialize_element(&self.code())?;

                match self {
                    $(Message::$name(value) => state.serialize_element(value)?,)*
                }
                state.end()
            }
        }

        impl<'de> Deserialize<'de> for Message {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                struct Visitor;

                impl<'de> serde::de::Visitor<'de> for Visitor {
                    type Value = Message;

                    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                        write!(formatter, "a type code and a message content")
                    }

                    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
                    where
                        A: serde::de::SeqAccess<'de>,
                    {
                        let no_value = || ::serde::de::Error::custom("no value found");
                        let code = seq.next_element::<u8>()?.ok_or_else(no_value)?;
                        let content = match code {
                            $($discriminant => seq.next_element::<$name>()?.ok_or_else(no_value)?.into(),)*
                            _ => return Err(::serde::de::Error::custom("invalid message type")),
                        };
                        Ok(content)
                    }
                }

                deserializer.deserialize_tuple(3, Visitor)
            }
        }

        impl<'a> Serialize for MessageWithId<'a> {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::Serializer,
            {
                let id = match self.message.as_ref() {
                    Message::Init(_) | Message::Version(_) => None,
                    _ => Some(self.id),
                };

                let mut state = serializer.serialize_tuple(3)?;
                state.serialize_element(&self.message.code())?;
                state.serialize_element(&id)?;

                match self.message.as_ref() {
                    $(Message::$name(value) => state.serialize_element(&value)?,)*
                }
                state.end()
            }
        }

        impl<'de> Deserialize<'de> for MessageWithId<'de> {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::Deserializer<'de>,
            {
                struct Visitor;

                impl<'de> serde::de::Visitor<'de> for Visitor {
                    type Value = MessageWithId<'de>;

                    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                        write!(formatter, "a type code, an id, and a message content")
                    }

                    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
                    where
                        A: serde::de::SeqAccess<'de>,
                    {
                        let no_value = || ::serde::de::Error::custom("no value found");
                        let code = seq.next_element::<u8>()?.ok_or_else(no_value)?;
                        let (id, message) = if code == Init::DISCRIMINANT || code == Version::DISCRIMINANT {
                            seq.next_element()?.ok_or_else(no_value)?;
                            if code == Init::DISCRIMINANT {
                                (0, Message::Init(seq.next_element()?.ok_or_else(no_value)?))
                            } else {
                                (0, Message::Version(seq.next_element()?.ok_or_else(no_value)?))
                            }
                        } else {
                            let id = seq.next_element()?.ok_or_else(no_value)?;
                            let message = match code {
                                $($discriminant => seq.next_element::<$name>()?.ok_or_else(no_value)?.into(),)*
                                _ => return Err(::serde::de::Error::custom("invalid message type")),
                            };
                            (id, message)
                        };
                        Ok(MessageWithId { id, message: Cow::Owned(message) })
                    }
                }

                deserializer.deserialize_tuple(3, Visitor)
            }
        }

        $(
            impl $name {
                #[allow(dead_code)]
                const DISCRIMINANT: u8 = $discriminant;
            }
            impl From<$name> for Message {
                fn from(value: $name) -> Self {
                    Self::$name(value)
                }
            }

            impl TryFrom<Message> for $name {
                type Error = Message;
                fn try_from(value: Message) -> Result<Self, Self::Error> {
                    if let Message::$name(value) = value {
                        Ok(value)
                    } else {
                        Err(value)
                    }
                }
            }
        )*
    };
}

messages! {
    Init: 1
    Version: 2
    Open: 3
    Close: 4
    Read: 5
    Write: 6
    LStat: 7
    FStat: 8
    SetStat: 9
    FSetStat: 10
    OpenDir: 11
    ReadDir: 12
    Remove: 13
    MkDir: 14
    RmDir: 15
    RealPath: 16
    Stat: 17
    Rename: 18
    ReadLink: 19
    Symlink: 20
    Status: 101
    Handle: 102
    Data: 103
    Name: 104
    Attrs: 105
    Extended: 200
    ExtendedReply: 201
}

impl From<Init> for Version {
    fn from(value: Init) -> Self {
        Self {
            version: value.version,
            extensions: value.extensions,
        }
    }
}
impl From<Version> for Init {
    fn from(value: Version) -> Self {
        Self {
            version: value.version,
            extensions: value.extensions,
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct MessageWithId<'a> {
    id: u32,
    message: Cow<'a, Message>,
}

impl Message {
    pub fn code(&self) -> u8 {
        self.kind().code()
    }
    pub fn encode(&self, id: u32) -> Result<Bytes, Error> {
        let mut encoder = SftpEncoder::with_vec(Vec::with_capacity(16));

        // Reserve space for frame length
        encoder.buf.put_u32(0);

        MessageWithId {
            id,
            message: Cow::Borrowed(self),
        }
        .serialize(&mut encoder)?;

        // write frame length at the beginning of the frame
        let frame_length = (encoder.buf.len() - std::mem::size_of::<u32>()) as u32;
        let mut buf = encoder.buf.as_mut_slice();
        buf.put_u32(frame_length);

        Ok(encoder.buf.into())
    }

    pub fn decode(mut buf: &[u8]) -> Result<(u32, Self), Error> {
        let frame_length = buf.get_u32() as usize;

        // Limit the read to this very frame
        let mut decoder = SftpDecoder::new(&buf[0..frame_length]);

        let message_with_id = MessageWithId::deserialize(&mut decoder).map_err(Into::into)?;

        Ok((message_with_id.id, message_with_id.message.into_owned()))
    }
}

/*
SFTP protocol frame

Frame:
| u32: frame length | u8: type | u8[frame length - 1]: frame body |



SSH_FXP_INIT: 1
(VERSION) | u32: version | u32: ext0 name length | u8[ext0 name length]: ext0 name | u32: ext0 value length | u8[ext0 value length]: ext0 value | ...
-> VERSION

SSH_FXP_VERSION: 2
(VERSION) | u32: version | u32: ext0 name length | u8[ext0 name length]: ext0 name | u32: ext0 value length | u8[ext0 value length]: ext0 value | ...

SSH_FXP_OPEN: 3
| u32: id | u32: filename length | u8[filename length]: filename | u32: pflags | u32: attr flags | {if size in flags} u64: size | {if uid/gid in flags} u32 : uid | {if uid/gid in flags} u32 : gid | {if perm in flags} u32: perms | {if acmodtime in flags} u32: atime | {if acmodtime in flags} u32: mtime |
-> HANDLE | STATUS

    PFLAGS:
        READ: 0x00000001
        WRITE: 0x00000002
        APPEND: 0x00000004
        CREATE: 0x00000008
        TRUNCATE: 0x00000010
        EXCLUDE: 0x00000020

    Flags:
        SIZE: 0x00000001
        UIDGID: 0x00000002
        PERMISSIONS: 0x00000004
        ACMODTIME: 0x00000008

    File type: (on permission)
        FIFO: 0x1000
        CHR: 0x2000
        DIR: 0x4000
        BLK: 0x6000
        REG: 0x8000
        LNK: 0xA000
        NAM: 0x5000


SSH_FXP_CLOSE: 4
(HANDLE) | u32: id | u32: handle length | u8[handle length]: handle |
-> STATUS

SSH_FXP_READ: 5
| u32: id | u32: handle length | u8[handle length]: handle | u64: offset | u32: length |
-> DATA | STATUS

SSH_FXP_WRITE: 6
| u32: id | u32: handle length | u8[handle length]: handle | u64: offset | u32: data length | u8[data length]: data |
-> STATUS

SSH_FXP_LSTAT: 7
(PATH) | u32: id | u32: path length | u8[path length]: path |
-> ATTRS

SSH_FXP_FSTAT: 8
(HANDLE) | u32: id | u32: handle length | u8[handle length]: handle |
-> ATTRS

SSH_FXP_SETSTAT: 9
(PATH ATTR) | u32: id | u32: path length | u8[path length]: path | u32: attrs length | u8[attrs length]: attrs |
-> STATUS

SSH_FXP_FSETSTAT: 10
(HANDLE ATTR) | u32: id | u32: handle length | u8[handle length]: path | u32: attrs length | u8[attrs length]: attrs |
-> STATUS

SSH_FXP_OPENDIR: 11
(PATH) | u32: id | u32: path length | u8[path length]: path |
-> HANDLE | STATUS

SSH_FXP_READDIR: 12
(HANDLE) | u32: id | u32: handle length | u8[handle length]: handle |
-> NAME | STATUS

SSH_FXP_REMOVE: 13
(PATH) | u32: id | u32: path length | u8[path length]: path |
-> STATUS

SSH_FXP_MKDIR: 14
(PATH ATTR) | u32: id | u32: path length | u8[path length]: path | u32: attrs length | u8[attrs length]: attrs |
-> STATUS

SSH_FXP_RMDIR: 15
(PATH) | u32: id | u32: path length | u8[path length]: path |
-> STATUS

SSH_FXP_REALPATH: 16
(PATH) | u32: id | u32: path length | u8[path length]: path |
-> NAME | STATUS

SSH_FXP_STAT: 17
(PATH) | u32: id | u32: path length | u8[path length]: path |
-> ATTRS | STATUS

SSH_FXP_RENAME: 18
| u32: id | u32: old path length | u8[old path length]: old path | u32: new path length | u8[new path length]: new path |
-> STATUS

SSH_FXP_READLINK: 19
(PATH) | u32: id | u32: path length | u8[path length]: path |
-> NAME | STATUS

SSH_FXP_SYMLINK: 20
| u32: id | u32: link path length | u8[link path length]: link path | u32: target path length | u8[target path length]: target path |
-> STATUS

SSH_FXP_STATUS: 101
| u32: id | u32: status code | u32: error length | u8[error length]: error | u32: language length | u8[language length]: language |

    Status Code:
        Ok: 0
        Eof: 1
        NoSuchFile: 2
        PermissionDenied: 3
        Failure: 4
        BadMessage: 5
        NoConnection: 6
        ConnectionLost: 7
        OpUnsupported: 8

SSH_FXP_HANDLE: 102
(HANDLE) | u32: id | u32: handle length | u8[handle length]: handle |

SSH_FXP_DATA: 103
(DATA) | u32: id | u32: data length | u8[data length]: data |

SSH_FXP_NAME: 104
| u32: id | u32: file count | u32: file0 name length | u8[file0 name length]: file0 name | u32: file0 long name length | u8[file0 long name length]: file0 long name | u32: file0 attr flags | {if size in file0 flags} u64: file0 size | {if uid/gid in file0 flags} u32 : file0 uid | {if uid/gid in file0 flags} u32 : file0 gid | {if perm in file0 flags} u32: file0 perms | {if acmodtime in file0 flags} u32: file0 atime | {if acmodtime in file0 flags} u32: file0 mtime | ...

    File type: (on permission)
        FIFO: 0x1000
        CHR: 0x2000
        DIR: 0x4000
        BLK: 0x6000
        REG: 0x8000
        LNK: 0xA000
        NAM: 0x5000

SSH_FXP_ATTRS: 105
| u32: id | u32: attr flags | {if size in flags} u64: size | {if uid/gid in flags} u32 : uid | {if uid/gid in flags} u32 : gid | {if perm in flags} u32: perms | {if acmodtime in flags} u32: atime | {if acmodtime in flags} u32: mtime |

    Flags:
        SIZE: 0x00000001
        UIDGID: 0x00000002
        PERMISSIONS: 0x00000004
        ACMODTIME: 0x00000008

    File type: (on permission)
        FIFO: 0x1000
        CHR: 0x2000
        DIR: 0x4000
        BLK: 0x6000
        REG: 0x8000
        LNK: 0xA000
        NAM: 0x5000

SSH_FXP_EXTENDED: 200
| u32: id | u32: request length | u8[request length]: request | u8[frame length - 9 - request length]: data |
-> EXTENDED REPLY

SSH_FXP_EXTENDED_REPLY: 201
| u32: id | u8[frame length - 5]: data |
 */