Expand description
§SMB
the smb crate is a pure rust SMB client, supports the SMB2 protocol (including SMB3).
§Basic usage
The most basic functionality that an SMB client should provide is the ability to connect to an SMB server, authenticate, and perform simple file operations.
The Client struct provides a simple interface for interacting with an SMB server. Let’s see how we use it.
use smb::{Client, ClientConfig, UncPath, FileCreateArgs, FileAccessMask};
use std::str::FromStr;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// instantiate the client
let client = Client::new(ClientConfig::default());
// Connect to a share
let target_path = UncPath::from_str(r"\\server\share").unwrap();
client.share_connect(&target_path, "username", "password".to_string()).await?;
// And open a file on the server
let file_to_open = target_path.with_path("file.txt");
let file_open_args = FileCreateArgs::make_open_existing(FileAccessMask::new().with_generic_read(true));
let file = client.create_file(&file_to_open, &file_open_args).await?;
// now, you can do a bunch of operations against `file`, and close it at the end.
Ok(())
}Cool! we got ourselves a live connection to an SMB server, and we also got a file open.
But wait… How do we know it’s actually a file? Well, we don’t. The Client::create_file method returns the Resource struct, which is a union to a file, directory, or a pipe - the supported SMB resources in this crate (printers are currently not implemented specifically). What we need to do next, is to find out what type of resource we’ve got:
match &file {
Resource::File(file) => {
// We have a file
}
Resource::Directory(dir) => {
// We have a directory
}
Resource::Pipe(pipe) => {
// We have a pipe
}
}
// Note: we could also use `.unwrap_file()` here,
// or similar method provided by Resource to find out what kind of resource this is!Cool! Let’s assume we got ourselves a file. Then, we can do the obvious operation of reading or writing a block of data from or to the file:
let file: File = file.unwrap_file();
let mut data: [u8; 1024] = [0; 1024];
file.read_at(&mut data, 0).await?;
file.write_at(&data, 0).await?;At the end, close the file.
file.close().await?;§Feature flags
| Type | Algorithm | Async | Multi-threaded | Single-threaded | Feature Name |
|---|---|---|---|---|---|
| Authentication | Kerberos | ✅ | ✅ | ✅ | kerberos |
| Transport | QUIC | ✅ | ❌ | ❌ | quic |
| Signing | * | sign | |||
| Signing | HMAC_SHA256 | ✅ | ✅ | ✅ | sign_hmac |
| Signing | AES-128-GCM | ✅ | ✅ | ✅ | sign_gmac |
| Signing | AES-128-CCM | ✅ | ✅ | ✅ | sign_cmac |
| Encryption | * | encrypt | |||
| Encryption | AES-128-CCM | ✅ | ✅ | ✅ | encrypt_aes128ccm |
| Encryption | AES-128-GCM | ✅ | ✅ | ✅ | encrypt_aes128gcm |
| Encryption | AES-256-CCM | ✅ | ✅ | ✅ | encrypt_aes256ccm |
| Encryption | AES-256-GCM | ✅ | ✅ | ✅ | encrypt_aes256gcm |
| Compression | * | compress | |||
| Compression | LZ4 | ✅ | ✅ | ✅ | compress_lz4 |
| Compression | Pattern_V1 | 🟡 | 🟡 | 🟡 | compress_pattern_v1* |
| Compression | LZNT1/LZ77/+Huffman | ❌ | ❌ | ❌ | - |
- The Pattern_V1 compression algorithm currently supports in-bound decompression only.
§Advanced documentation
Re-exports§
pub use client::Client;pub use client::ClientConfig;pub use client::UncPath;pub use connection::Connection;pub use connection::ConnectionConfig;pub use error::Error;pub use resource::Directory;pub use resource::File;pub use resource::FileCreateArgs;pub use resource::GetLen;pub use resource::ReadAt;pub use resource::ReadAtChannel;pub use resource::Resource;pub use resource::ResourceHandle;pub use resource::WriteAt;pub use resource::WriteAtChannel;pub use session::Session;pub use tree::DfsRootTreeRef;pub use tree::Tree;pub use smb_transport as transport;
Modules§
- binrw_
util - This module contains utility types for the binrw crate.
- cancel
- Cancel Request
- chained_
item - A genric utility struct to wrap “chained”-encoded entries.
Many fscc-query structs have a common “next entry offset” field,
which is used to chain multiple entries together.
This struct wraps the value, and the offset, and provides a way to iterate over them.
See
ChainedItem<T>::write_chainedto see how to write this type when in a list. - client
- High-level SMB client interface.
- common_
info - compressed
- Compressed messages
- compression
- Implements (de)compression logic.
- connection
- create
- Create & Close (files) requests and responses.
- crypto
- dfsc
- dialects
- Implements SMB-dialect-specific types and functions.
- directory_
info - docs
- Additional crate docs are submodules.
- echo
- Echo request and response messages
- encrypted
- Encrypted messages
- error
- file
- File-related messages: Flush, Read, Write.
- filesystem_
info - guid
- header
- info
- SMB2 Query and Set Info messages.
- ioctl
- SMB2 IOCTL packet implementation
- lock
- message
- msg_
handler - negotiate
- notify
- SMB2 Change Notify Request and Response, and Server to Client Notification
- oplock
- plain
- query_
dir - Directory-related messages.
- query_
file_ info - resource
- security
- MS-DTYP 2.4
- session
- SMB Session logic module.
- session_
setup - set_
file_ info - smb1
- SMBv1 negotiation packet support.
- sync_
helpers - This is a helper module that allows easy access and usage of Async/Multi-threaded features in the library, according to the features enabled.
- tree
- tree_
connect - util
Macros§
- access_
mask - Macro for defining a bitfield for an access mask.
- file_
info_ classes - A macro for generating a file class enums, for both the file information class, and information value. including a trait for the value types.
- guid
- A macro to create a
Guidfrom a string literal at compile time. - query_
info_ data - Internal helper macro to easily generate fields & methods for QueryInfoData.
Structs§
- ACE
- ACL
- Access
Ace - Access
Callback Ace - Access
Mask - Access
Object Ace - Access
Object Callback Ace - AceFlags
- Additional
Info - Allocation
Size - AppInstance
Id - AppInstance
Version - Array
Data - Blob
Data - Cancel
Request - Chained
Item - Chained
Item List - Implements a chained item list.
- Change
Notify Request - Change
Notify Response - Claim
Security Attribute Relative V1 - Close
Flags - Close
Request - Close
Response - Compressed
Chained Item - Compressed
Chained Message - Compressed
Data - Compressed
Unchained Message - Compression
Capabilities - Compression
Caps Flags - Create
Context - Create
Options - Create
Request - Create
Response - Create
Response Flags - DH2Q
Resp - DfsRequest
Data - RequestData is part of the REQ_GET_DFS_REFERRAL_EX message (section 2.2.3).
- DfsRequest
Flags - DirAccess
Mask - Durable
Handle Reconnect - Durable
Handle Reconnect V2 - Durable
Handle Request - Durable
Handle Request V2 - Durable
Handle Response - Durable
Handle V2Flags - EaBuffer
- Echo
Mesasge - Encrypted
Header - Encrypted
Message - Encryption
Capabilities - Entry
V3DC Refs - 2.2.5.3.2 NameListReferral Flag Set to 1
- Entry
V3V4 DfsPaths - 2.2.5.3.1 NameListReferral Flag Set to 0
- Error
Response - Error
Response Context - FciClaim
Security Attributes - File
Access Information - File
Access Mask - File
AllInformation - File
Allocation Information - File
Alternate Name Information - File
Attribute TagInformation - File
Attributes - MS-FSCC 2.6
- File
Basic Information - File
Both Directory Information Inner - File
Compression Information - File
Directory Information Inner - File
Disposition Information - File
EaInformation - File
EndOf File Information - File
FsAttribute Information - File
FsControl Information - File
FsDevice Information - File
FsFull Size Information - File
FsObject IdInformation - File
FsSector Size Information - File
FsSize Information - File
FsVolume Information - File
Full Directory Information Inner - File
Full EaInformation Inner - File
GetEa Information Inner - File
GetQuota Information Inner - FileId
- 2.2.14.1: SMB2_FILEID
- File
Id64 Extd Both Directory Information Inner - File
Id64 Extd Directory Information Inner - File
IdAll Extd Both Directory Information Inner - File
IdAll Extd Directory Information Inner - File
IdBoth Directory Information Inner - File
IdExtd Directory Information Inner - File
IdFull Directory Information Inner - File
IdInformation - File
Internal Information - File
Level Trim Range - MSDN
- File
Level Trim Request - File
Level Trim Response - MS-FSCC 2.3.14
- File
Link Information - 2.4.27.2 - FileLinkInformation for SMB2 protocol
- File
Mode Information - File
Name Information - File
Names Information Inner - File
Network Open Information - File
Normalized Name Information - File
Notify Information Inner - File
Pipe Information - File
Pipe Local Information - File
Pipe Remote Information - File
Position Information - File
Quota Information Inner - File
Rename Information2 - File
Short Name Information - File
Standard Information - File
Stream Information Inner - File
System Attributes - File
System Control Flags - File
Valid Data Length Information - Flush
Request - Flush
Response - FsDevice
Characteristics - GetEa
Info List - Global
Capabilities - Guid
- Represents a standard, 16-byte GUID.
- Header
- Sync and Async SMB2 Message header.
- Header
Flags - Ioctl
Buffer - Ioctl
Request - Ioctl
Request Flags - Ioctl
Response - Lease
Break AckResponse - Lease
Break Notify - Lease
State - LmrRequest
Resiliency Response - Lock
Element - Lock
Flag - Lock
Request - Lock
Response - Lock
Sequence - Logoff
Request - Logoff
Response - Luid
Attr Data - Mandatory
Label Access Mask - Negotiate
Capabilities - Negotiate
Context - Negotiate
Request - Negotiate
Response - Negotiate
Security Mode - Netname
Negotiate Context Id - Network
Interface Capability - Network
Interface Info - Network
Resiliency Request - Sent to request resiliency for a specified open file. This request is not valid for the SMB 2.0.2 dialect.
- Notify
Filter - Notify
Flags - Notify
Session Closed - Null
Byte - Object
Access Mask - Object
AceFlags - Offload
Read Request - MS-FSCC 2.3.41
- Offload
Read Response - MS-FSCC 2.3.42
- Oplock
Break Msg - Pipe
Peek Request - Pipe
Peek Response - MS-FSCC 2.3.46
- Pipe
Transceive Request - Pipe
Transceive Response - Pipe
Wait Request - The FSCTL_PIPE_WAIT Request requests that the server wait until either a time-out interval elapses, or an instance of the specified named pipe is available for connection.
- Pipe
Wait Response - Plain
Request - A plain, single, SMB2 message.
- Plain
Response - A plain, single, SMB2 message.
- Preauth
Integrity Capabilities - Query
Alloc Ranges Item - Query
Alloc Ranges Result - Query
Directory Flags - Query
Directory Request - Query
Directory Response - Query
Info Flags - Query
Info Request - Query
Info Response - Query
Info Response Data - A helpers struct that contains the raw data of a query info response or a set info request,
and can be parsed using the
QueryInfoResponseData::parsemethod, to a specific info type. - Query
Maximal Access Request - Query
Maximal Access Response - Query
Network Interface Info Request - Query
OnDisk IdReq - Query
OnDisk IdResp - Query
Quota Info - RawQuery
Info Data - RawSet
Info Data - Rdma
Transform Capabilities - Read
Flags - Read
Request - Read
Response - Referral
Entry - Referral
Entry Flags - Referral
Entry Value V1 - Referral
Entry Value V2 - DO NOT use this struct directly when bin read/writing.
Use an instance of
ReferralEntryinstead. - Referral
Entry Value V3 - Referral
Entry Value V4 - Referral
Header Flags - Remoted
Identity Tree Connect - ReqGet
DfsReferral - MS-DFSC 2.2.2: DFS referral requests are sent in the form of an REQ_GET_DFS_REFERRAL message, by using an appropriate transport as specified in section 2.1.
- ReqGet
DfsReferral Ex - Request
Lease V1 - Request
Lease V2 - Resp
GetDfs Referral - SID
- SID (Security identifier)
- Sector
Size Info Flags - Security
Descriptor - Security Descriptor - MS-DTYP 2.4.6
- Security
Descriptor Control - Server
ToClient Notification - Session
Flags - Session
Security Mode - Session
Setup Request - Session
Setup Response - SetFile
Full EaInformation - A FileFullEaInformation structure to be used when setting for extended attributes. You may use super::QueryFileFullEaInformation for querying.
- SetInfo
Request - SetInfo
Response - SetReparse
Point Request - Stores data for a reparse point.
- SetReparse
Point Response - Setup
Request Flags - Share
Access Flags - Share
Flags - SidAttr
Data - SidAttr
SeGroup - Signing
Capabilities - Socket
Addr Storage V4 - Socket
Addr Storage V6 - SrvCopy
Chunk Copy Write - SrvCopychunk
Copy - The Length of source/dest keys in SrvCopyChunk* FSCTLs contents. MS-SMB 2.2.31.1
- SrvCopychunk
Item - SrvCopychunk
Response - SrvEnumerate
Snapshots Request - SrvEnumerate
Snapshots Response - MS-SMB 2.2.7.2.2.1
- SrvHash
Retrieve File Based - SrvHash
Retrieve Hash Based - SrvRead
Hash Req - SrvRead
Hash Res - SrvRequest
Resume Key - SrvRequest
Resume KeyRequest - SrvSnapshot
Array - Svhdx
Open Device Context V1 - MS-RSVD sections 2.2.4.12 and 2.2.4.32.
- Svhdx
Open Device Context V2 - System
Mandatory Label Ace - System
Resource Attribute Ace - Timewarp
Token - Transport
Capabilities - Tree
Capabilities - Tree
Connect Context - Tree
Connect Request - Tree Connect Request
- Tree
Connect Request Flags - Tree
Connect Response - Tree
Disconnect Request - Tree
Disconnect Response - Validate
Negotiate Info Request - Validate
Negotiate Info Response - Write
Flags - Write
Request - Zero-copy write request.
- Write
Response
Enums§
- AceType
- AceValue
- AclRevision
- Claim
Security Attribute Type - Command
- Communication
Channel - Compressed
Message - Compression
Algorithm - Create
Action - Create
Context ReqData - Create
Context Resp Data - Create
Context Type - Create
Disposition - DfsServer
Type - Type of server hosting the target
- Dialect
- Encryption
Cipher - Entry
V3Value - ErrorId
- File
Alignment Information - File
Compression Format - FsDevice
Type - Fsctl
Codes - GetInfo
Request Data - This struct describes the payload to be added in the QueryInfoRequest when asking for information about Quota or Extended Attributes. In other cases, it is empty.
- Hash
Algorithm - Impersonation
Level - Info
Type - Ioctl
ReqData - Named
Pipe Configuration - Named
Pipe End - Named
Pipe State - Named
Pipe Type - Negotiate
Context Type - Negotiate
Context Value - Negotiate
Dialect - Dialects that may be used in the SMB Negotiate Request. The same as Dialect but with a wildcard for SMB 2.0.
- Notification
- Notification
Type - Notify
Action - Oplock
Level - Pipe
Completion Mode - Pipe
Read Mode - Query
Directory Info - Query
Directory Info Class - Query
File Info - Query
File Info Class - Query
File System Info - Query
File System Info Class - Query
Info Class - Query
Info Data - Represents information passed in get/set info requests. This is the information matching InfoType, and should be used in the get info response and in the set info request.
- Rdma
Transform Id - Referral
Entry Value - Referral
Level - The DFS referral version supported by the client.
See
ReqGetDfsReferral::max_referral_level. - Reparse
Tag - MS-FSCC 2.1.2.1: Each reparse point has a reparse tag. The reparse tag uniquely identifies the owner of that reparse point. The owner is the implementer of the file system filter driver associated with a reparse tag.
- Request
- Request
Content - Request
Lease - Response
- Response
Content - SetFile
Info - SetFile
Info Class - SetFile
System Info - SetFile
System Info Class - SetInfo
Class - A helper class for SetInfoRequest to contain the information class to set. In cases of no class, it will be set to a null byte (0u8).
- SetInfo
Data - Represents information passed in get/set info requests. This is the information matching InfoType, and should be used in the get info response and in the set info request.
- Share
Cache Mode - Share
Type - Signing
Algorithm Id - SmbFscc
Error - FSCC errors
- SmbMsg
Error - SMB Message related errors
- Socket
Addr Storage - SrvHash
Retrieval Type - Status
- NT Status codes.
- Svhdx
Open Device Context
Traits§
- Create
Context Data ReqValue - Create
Context Data Resp Value - File
Info Type - Trait for file information types. This trait contains all types of all file info types and classes, specified in MS-FSCC.
- Fsctl
Request - This is a helper trait that defines, for a certain FSCTL request type, the response type and their matching FSCTL code.
- Fsctl
Response Content - A trait that helps parsing FSCTL responses by matching the FSCTL code.
- Ioctl
Request Content - A trait that helps calculating the size of the buffer for IOCTL requests.
- Query
Directory Info Value - Query
File Info Value - Query
File System Info Value - SetFile
Info Value - SetFile
System Info Value
Type Aliases§
- Echo
Request - Echo
Response - Encryption
Nonce - The nonce used for encryption. Depending on the encryption algorithm, the nonce may be trimmed to a smaller size when used, or padded with zeroes to match the required size. When transmitted, the full 16 bytes are used.
- File
Both Directory Information - File
Directory Information - File
Full Directory Information - File
Full EaInformation Common - File
GetEa Information - File
GetQuota Information - File
Id64 Extd Both Directory Information - File
Id64 Extd Directory Information - File
IdAll Extd Both Directory Information - File
IdAll Extd Directory Information - File
IdBoth Directory Information - File
IdExtd Directory Information - File
IdFull Directory Information - File
Names Information - File
Notify Information - File
Quota Information - File
Stream Information - Lease
Break Ack - Lease
Break Response - Network
Interfaces Info - Oplock
Break Ack - Oplock
Break Notify - Oplock
Break Response - Query
File Full EaInformation - A FileFullEaInformation structure to be used when querying for extended attributes. You may use super::SetFileFullEaInformation for setting.
- ReqCreate
Context - Resp
Create Context - Result
- SMB Result type
- SdBuffer