rust-smb-server
SMB server in Rust.
Install
Or in Cargo.toml:
[]
= "0.7"
Run the example
Listens on 127.0.0.1:4445 and serves directories under smb-example in the system temp directory. Override with SMB_LISTEN and SMB_ROOT.
Shares: public (anon rw), media (anon ro), home (alice rw, bob ro).
alice and bob use the password in SMB_PASSWORD. Without it, the example generates one and logs it at startup.
Embed
use ;
let server = builder
.listen
.user
.share
.build?;
server.bind.await?;
server.serve.await?;
Runtime config
Create a ConfigHandle before serve() and keep it in the task that owns admin/config updates:
use ;
let server = builder
.listen
.share
.build?;
let config = server.config_handle;
config.add_user.await?;
let home = new
.user;
config
.add_share
.await?;
config.revoke_share_user.await?;
config.remove_user.await?;
server.serve.await?;
Removing a user revokes that user's active sessions. Removing a share or revoking share access closes matching active tree connections and open handles.
Encryption
SMB 3.1.1 clients negotiate AES-128 or AES-256 in CCM or GCM mode; SMB 3.0 and 3.0.2 clients with the encryption capability use AES-128-CCM. A client that sends encrypted requests gets encrypted replies.
To require encryption on one share:
use ;
let server = builder
.listen
.user
.share
.build?;
To require it for every session, call .encrypt_data(true) on the builder.
Where encryption is required, clients that cannot encrypt are refused: SMB 2.x, SMB 3.x without a cipher in common, and anonymous sessions, which get a session but no tree. reject_unencrypted_access(false) serves them unencrypted instead.
Leases
Files on every share get SMB2 leases with read, handle and write caching: a client caches what it reads, keeps files open after its applications close them, and caches writes while every other open of the file holds the same lease or only reads attributes. An open, write, lock, rename or delete through any open that doesn't hold the lease, including the same client's opens under another lease key or none, breaks it when it needs what the lease caches. An open or rename that needs the client to write back or close its handles first waits until the client acknowledges or closes, or until 35 seconds after the break is sent; a lock is refused until the client has written back.
Changes made outside the server, or through another share over the same directory, break no lease, so clients keep serving their cached copies. Call .leases(false) on such a Share to turn leases off there.
Durable handles
A file opened with a durable handle request, version 1 or 2, and a lease that caches handles stays open, for 2 minutes by default or the time a version 2 request asks for, up to 5 minutes or the configured time if that is longer, after the client's connection drops or it logs off, with its share mode, byte-range locks and lease. When the same user reconnects from the same client, it takes the file back and carries on with the same handle, locks and lease. An open, write, lock, rename or delete that needs what the lease caches closes a kept file at once, and so do ConfigHandle calls that remove the user or the share, revoke the user's access or lower it to read-only, change the share's mode, or change the user's password.
durable_handle_timeout and max_disconnected_opens on the builder set the time and the most files kept at once. Call .durable_handles(false) on a Share to turn them off there.
Share listing
Clients that list a server's shares, such as Explorer and net view on Windows, the Finder and smbutil view on macOS, and smbclient -L, get the shares they may connect to, in the order they were added, and IPC$. An anonymous session gets the public shares, and a session that cannot encrypt does not get shares whose traffic is encrypted. Shares whose name ends in $ are listed too; clients hide them from ordinary lists.
use ;
let server = builder
.listen
.user
.share
.share
.build?;
remark sets the comment shown next to a share. listed(false) leaves a share out of the list; it is still reachable and can be looked up by name, so it is not access control. share_listing(false) on the builder turns listing off.
Limitations
Not supported:
- Kerberos; authentication is NTLM only
- oplocks and directory leases
- answering a replayed CREATE with the open it repeats, and persistent and resilient handles
- change notifications for changes made outside the server or through another share over the same directory, except with
LocalFsBackendon Linux for the entries directly in a watched folder - named pipes other than
srvsvc, and RPC calls other than listing shares and describing the server - DFS
- multichannel and compression
- alternate data streams and extended attributes
- network discovery (mDNS, NetBIOS, WS-Discovery): clients connect by name or address
License
MIT.