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::Pipe;pub use resource::PipeRpcConnection;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
- client
- High-level SMB client interface.
- 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.
- docs
- Additional crate docs are submodules.
- echo
- Echo request and response messages
- encrypted
- Encrypted messages
- error
- file
- File-related messages: Flush, Read, Write.
- 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.
- resource
- security
- MS-DTYP 2.4
- session
- SMB Session logic module.
- session_
setup - 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.
- guid
- A macro to create a
Guidfrom a string literal at compile time. - make_
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 - This struct was partially generated by the
smb_dtyp::access_mask!macro. Read more about generic access rights and standard access rights - Access
Object Ace - Access
Object Callback Ace - AceFlags
- Additional
Info - Allocation
Size - AppInstance
Id - AppInstance
Version - Array
Data - Blob
Data - Cancel
Request - 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 - This is meant to be used within a
ChainedItemList<T>! - 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 - Directory Access Mask
- Durable
Handle Reconnect - Durable
Handle Reconnect V2 - Durable
Handle Request - Durable
Handle Request V2 - Durable
Handle Response - Durable
Handle V2Flags - EaFlags
- Extended Attribute (EA) Flags
- 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 - Query the access rights of a file that were granted when the file was opened.
- File
Access Mask - File Access Mask
- File
AllInformation - Query a collection of file information structures.
- File
Allocation Information - Set the allocation size for a file.
- File
Alternate Name Information - Query the alternate name (8.3 short name) of a file.
- File
Attribute TagInformation - Query file attribute and reparse tag information for a file.
- File
Attributes - Attributes of a file or directory.
- File
Basic Information - Query or Set file information.
- File
Both Directory Information - Query detailed information for the files in a directory.
- File
Compression Information - Query compression information for a file.
- File
Directory Information - Query detailed information for the files in a directory.
- File
Disposition Information - Mark a file for deletion.
- File
EaInformation - Query the size of the extended attributes (EA) for a file.
- File
EndOf File Information - Set end-of-file information for a file.
- File
FsAttribute Information - Query attribute information for a file system.
- File
FsControl Information - Query or Set quota and content indexing control information for a file system volume.
- File
FsDevice Information - File
FsFull Size Information - Query sector size information for a file system volume.
- File
FsObject IdInformation - Query or Set the object ID for a file system data element. The operation MUST fail if the file system does not support object IDs.
- File
FsSector Size Information - Query for the extended sector size and alignment information for a volume.
- File
FsSize Information - Query sector size information for a file system volume.
- File
FsVolume Information - Query information on a volume on which a file system is mounted.
- File
Full Directory Information - Query detailed information for the files in a directory.
- File
Full EaInformation Inner - Query or Set extended attribute (EA) information for a file.
- File
GetEa Information - Query extended attributes for a file.
- File
GetQuota Information - This structure is used to provide the list of SIDs for which quota query information is requested.
- FileId
- 2.2.14.1: SMB2_FILEID
- File
Id64 Extd Both Directory Information - Query detailed information for the files in a directory.
- File
Id64 Extd Directory Information - Query detailed information for the files in a directory.
- File
IdAll Extd Both Directory Information - Query detailed information for the files in a directory.
- File
IdAll Extd Directory Information - Query detailed information for the files in a directory.
- File
IdBoth Directory Information - Query detailed information for the files in a directory.
- File
IdExtd Directory Information - Query detailed information for the files in a directory.
- File
IdFull Directory Information - Query detailed information for the files in a directory.
- File
IdInformation - Query the file system’s 8-byte file reference number for a file.
- File
Internal Information - Query the file system’s 8-byte file reference number for a file.
- File
Level Trim Range - MSDN
- File
Level Trim Request - File
Level Trim Response - MS-FSCC 2.3.14
- File
Link Information - Create a hard link to an existing file via the SMB Version 2 Protocol, as specified in [MS-SMB2].
- File
Mode Information - Query or Set file mode information.
- File
Name Information - Query the name of a file.
- File
Names Information - Query the names of the files in a directory.
- File
Network Open Information - Query network file open information for a file.
- File
Normalized Name Information - Query the normalized name of a file.
- File
Notify Information - FILE_NOTIFY_INFORMATION - MS-FSCC 2.7.1
- File
Pipe Information - Query or Set named pipe information.
- File
Pipe Local Information - Query information associated with a named pipe that is not specific to one end of the pipe or another.
- File
Pipe Remote Information - Query information that is associated with the remote end of a named pipe.
- File
Position Information - Query or Set the current byte offset of the file pointer.
- File
Quota Information - Query or to set file quota information for a volume.
- File
Rename Information - Rename a file within the SMB2 protocol.
- File
Short Name Information - change a file’s short name.
- File
Standard Information - Query standard information for a file.
- File
Stream Information Inner - Enumerate the data streams for a file.
- File
System Attributes - File system attributes.
- File
System Control Flags - File system control flags.
- File
Valid Data Length Information - set the valid data length information for a file.
- Flush
Request - Flush
Response - FsDevice
Characteristics - Characteristics of a file system volume.
- 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 - This struct was partially generated by the
smb_dtyp::access_mask!macro. Read more about generic access rights and standard access rights - 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 - This struct was partially generated by the
smb_dtyp::access_mask!macro. Read more about generic access rights and standard access rights - 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 - File system sector 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 - 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 - The
ReqCreate Context data enum. This contains all the possible context types for Req - Create
Context Resp Data - The
RespCreate Context data enum. This contains all the possible context types for Resp - Create
Context Type - Create
Disposition - DfsServer
Type - Type of server hosting the target
- Dialect
- Encryption
Cipher - Entry
V3Value - ErrorId
- File
Alignment Information - Query the buffer alignment required by the underlying device.
- File
Compression Format - Compression format values for file compression.
- 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 configuration values.
- Named
Pipe End - Named pipe end values.
- Named
Pipe State - Named pipe state values.
- Named
Pipe Type - Named pipe type values.
- 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 - See
FileNotifyInformation - Oplock
Level - Pipe
Completion Mode - Named pipe completion mode values.
- Pipe
Read Mode - Named pipe read mode values.
- Query
Directory Info - Query (list) directory information classes.
- Query
Directory Info Class - Information class IDs for
QueryDirectoryInfo - Query
File Info - Query file information classes.
- Query
File Info Class - Information class IDs for
QueryFileInfo - Query
File System Info - Query file system information classes.
- Query
File System Info Class - Information class IDs for
QueryFileSystemInfo - Query
Info Class - Query
Info Data - Enum to hold the different info types for QueryInfoData, that are used within SMB requests for querying or setting information.
- Rdma
Transform Id - Referral
Entry Value - Referral
Level - The DFS referral version supported by the client.
See
ReqGetDfsReferral::max_referral_level. - Reparse
Tag - Reparse Tag Values
- Request
- Request
Content - Request
Lease - Response
- Response
Content - SetFile
Info - Set file information classes.
- SetFile
Info Class - Information class IDs for
SetFileInfo - SetFile
System Info - Set file system information classes.
- SetFile
System Info Class - Information class IDs for
SetFileSystemInfo - 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 - Enum to hold the different info types for SetInfoData, that are used within SMB requests for querying or setting information.
- Share
Cache Mode - Share
Type - Signing
Algorithm Id - SmbFscc
Error - Errors specific to the smb-fscc crate.
- SmbMsg
Error - SMB Message related errors
- Socket
Addr Storage - SrvHash
Retrieval Type - Status
- NT Status codes.
- Svhdx
Open Device Context
Constants§
- CHAINED_
ITEM_ PREFIX_ SIZE - The size of added fields to the size of each entry in
ChainedItemList<T>, when bin-writing the data, before the actual T data.
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 - Trait for inner values in
QueryDirectoryInfo. - Query
File Info Value - Trait for inner values in
QueryFileInfo. - Query
File System Info Value - Trait for inner values in
QueryFileSystemInfo. - SetFile
Info Value - Trait for inner values in
SetFileInfo. - SetFile
System Info Value - Trait for inner values in
SetFileSystemInfo.
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
Full EaInformation - 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 - ReqCreate
Context - Resp
Create Context - Result
- SMB Result type
- SdBuffer