Skip to main content

Crate smb_server

Crate smb_server 

Source
Expand description

SMB2/3 file-sharing server with pluggable storage backends.

Example:

use smb_server::{SmbServer, Share, Access, ShareBackend};
SmbServer::builder()
    .listen("0.0.0.0:4445".parse()?)
    .user("alice", "password")
    .share(Share::new("home", backend).user("alice", Access::ReadWrite))
    .build()?
    .serve()
    .await?;

§Network discovery

With the discovery feature, SmbServerBuilder::discovery announces the server on the local network while SmbServer::serve runs, and withdraws it before serve returns. Nothing is announced unless it is called, and the listen address must not be a loopback address.

The Finder, GNOME Files and Dolphin find the server over mDNS and DNS-SD. Listen on [::]:445, as below: Windows may connect over IPv6 alone, so it cannot open a server that listens on IPv4 only. Discovery has the options and what each platform does.

use smb_server::{Discovery, Share, ShareBackend, SmbServer};
SmbServer::builder()
    .listen("[::]:445".parse()?)
    .share(Share::new("public", backend).public_read_only())
    .discovery(Discovery::new())
    .build()?
    .serve()
    .await?;

§Writing a backend

A share serves any ShareBackend. Each CREATE calls ShareBackend::open, which returns a Handle and the OpenAction it took; CREATE reports the action to the client. The handle serves READ, WRITE, FLUSH, QUERY_INFO, QUERY_DIRECTORY and SET_INFO on that open until the open is closed. LocalFsBackend (feature localfs) is the reference implementation; its documentation says where it keeps named streams and extended attributes on each host. S3Backend (feature s3) serves a bucket of an S3-compatible store. EncryptedBackend (feature encryption) wraps another backend and stores file contents, and by default names, encrypted under a key.

What the protocol layer does around the backend calls:

  • Paths arrive as SmbPath, relative to the share root. . components are dropped; .., empty components, <>:"|?* and control characters are rejected before the backend sees them. A name ending in ::$DATA opens the file with OpenOptions::non_directory set, and one ending in ::$INDEX_ALLOCATION the directory with OpenOptions::directory set. The empty path is the share root.

  • A named stream (file.txt:name) arrives as a StreamName beside the file’s path, on shares whose backend reports BackendCapabilities::named_streams unless Share::named_streams turns them off: CREATE opens the file with open, creating it when the disposition allows, then the stream with ShareBackend::open_stream, which gets the file’s handle. An overwrite of the file removes its named streams through ShareBackend::remove_stream, each once no open is on it. A rename to :name or ::$DATA goes to ShareBackend::rename_stream. An open of a named stream reports the file’s times and attributes with the stream’s sizes, without FILE_ATTRIBUTE_DIRECTORY on a directory’s stream. Elsewhere named streams are rejected before the backend sees them.

  • Extended attributes, on shares whose backend reports BackendCapabilities::extended_attributes unless Share::extended_attributes turns them off: FileFullEaInformation queries call Handle::eas, sets Handle::set_eas, and an ExtA create context gives a new file its attributes. The protocol layer checks the names and the 65531-byte limit before the backend is called, matches names without regard to ASCII case, keeps each open’s position in the attribute list across queries, replaces a file’s attributes with those of a CREATE that overwrites or supersedes it, and refuses an open with FILE_NO_EA_KNOWLEDGE of a file that has one flagged FILE_NEED_EA. EaSize in FileEaInformation, FileAllInformation and directory listings is FileInfo::ea_size. A set breaks no lease and is reported, under EA and ATTRIBUTES, when the open that made it closes. A named stream has none of its own.

  • Apple’s extensions: unless SmbServerBuilder::apple_extensions turns them off, a CREATE that carries the AAPL server query, as macOS clients send when they mount a share, gets a reply with the model string of SmbServerBuilder::apple_model, UNIX_BASED on Unix hosts and the case sensitivity the backend reports. A client that asks for READ_DIR_ATTR on a share with named streams gets it for its connection. FileIdBothDirectoryInformation listings on that connection then carry Finder data in place of EaSize and the short name: the access the client may ask for, the low 16 bits of FileInfo::unix_mode, and, on shares with named streams, the Finder info of each entry’s AFP_AfpInfo stream and the size of its AFP_Resource stream, read through one Handle::peek_streams call per response. A client that asks for the server caps on a share that serves named streams and allows server-side copies is offered OSX_COPYFILE for its connection, unless SmbServerBuilder::apple_copyfile turns it off.

  • open fails with SmbError::PathNotFound when a directory on the way to the path is missing, and with SmbError::NotFound when only the last component is. Clients see STATUS_OBJECT_PATH_NOT_FOUND and STATUS_OBJECT_NAME_NOT_FOUND.

  • Share permissions and the access the client asked for are checked before open. OpenOptions carries the result: read, write and write_attributes reflect the access granted to the open. READ and WRITE on a handle without the matching access fail before the handle is called. When BackendCapabilities::is_read_only is set, trees on the share get read access only, so opens that ask for write access, and dispositions that create or truncate, fail before open.

  • Share modes, byte-range locks and the delete-pending flag are kept by the protocol layer per share, across every connection, keyed by FileInfo::file_index, or by path when it is 0. Two shares over the same directory do not see each other’s opens. Each named stream of a file has its own share modes, byte-range locks and delete-pending flag, and the last close of a stream marked for deletion calls ShareBackend::remove_stream. A CREATE that would truncate first opens the file without truncating it, for reading or, when reading is refused, with the access the CREATE was granted. It runs the checks, enters the open in the table and waits for lease breaks, so the sharing mode holds and clients caching writes have written back before open truncates. When that first open finds no file, the CREATE calls open with OpenIntent::Create instead and starts over if the file appeared meanwhile. Any other CREATE refused by the share-mode check has already called open and closes the new handle again.

  • Handle::close runs on every path that ends an open: CLOSE, TREE_DISCONNECT, LOGOFF, the end of a session or connection, and ConfigHandle changes that end sessions or trees. A durable open, described below, outlives LOGOFF and the end of its connection or session for a while. Before close, Handle::set_times puts back access and write times that FileBasicInformation froze or set through that open.

  • Delete-on-close: OpenOptions::delete_on_close and FileDispositionInformation make the file delete-pending. When its last open ends, on any of the paths above, the protocol layer calls Handle::close and then ShareBackend::unlink on the file’s current path. open doesn’t delete anything itself. Read-only files are refused before that point, and so, for SET_INFO, are directories that Handle::dir_is_empty reports as not empty. unlink returns SmbError::NotEmpty for a directory that has entries; the directory then stays.

  • A rename through SET_INFO of a directory with a file or directory open below it first asks the clients caching handles to those files to close them, and waits until they answer or the break times out; files kept open for a client to reconnect are closed. If an open below remains, the rename fails with STATUS_ACCESS_DENIED, and so does a rename of the share root. Otherwise the rename calls ShareBackend::rename and then moves the path of every open of the old name. Renames on one share run one at a time, and a CREATE or a delete waits for a rename in progress. The backend keeps those handles working on the renamed file.

  • A CREATE that makes or overwrites a file passes the attributes it asks for to Handle::set_attributes when they differ from what Handle::stat reports. If that returns SmbError::NotSupported, it retries with READONLY alone when that was asked for; the CREATE succeeds either way.

  • FileBasicInformation passes Handle::set_attributes and Handle::set_times only the values that differ from what Handle::stat reports. After a write or truncate, set_times also puts back the times FileBasicInformation froze or set. Handle::truncate is never called on a directory.

  • QUERY_DIRECTORY calls Handle::list_dir with the search pattern on the first query and on a restart, then pages through the returned entries itself. It adds . and .. when the pattern matches them, and opens the parent directory to describe ..; at the share root, .. describes the root itself, with file id 0.

  • FileStandardInformation and FileAllInformation report Handle::link_count as NumberOfLinks, and DeletePending when the file is delete-pending or has no links left. The filesystem QUERY_INFO classes, and the VolumeId of the on-disk id CREATE context, come from ShareBackend::volume_info.

  • Leases: on every share unless Share::leases turns them off, files get read, handle and write caching leases. A client holding one 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 is attribute-only. An open, write, size change, byte-range lock, rename or delete made through an open that does not hold the lease breaks it when it needs what the lease caches, whether that open belongs to another client or to the same client under another lease key or none; attribute-only opens and opens holding the same lease break nothing. Each named stream of a file or directory has leases of its own, which only opens of that stream grant and break, except that an overwrite of the file breaks their handle caching; a lease key held on one stream fails with STATUS_INVALID_PARAMETER on another stream or file. An open or rename that needs the client to write back or close first waits until the client acknowledges the break or closes, or until the break times out 35 seconds after the server sent it; meanwhile its backend handle stays open, except for one that truncates, which calls open after the wait. A byte-range lock asked for while another lease still caches writes is refused with STATUS_LOCK_NOT_GRANTED until that client has written back. Changes made outside the server, including through another share over the same directory, break nothing: clients keep serving stale cached data. Turn leases off on such shares.

  • Durable handles: on a share with leases, unless Share::durable_handles turns them off, a file opened with a durable handle request (DHnQ, or DH2Q on SMB 3.x) and a lease that caches handles stays open when the client’s connection drops, when it logs off, when its session’s reauthentication fails, or when a new session names its session as the previous one. A request without such a lease gets an open that is not durable and no durable context in the response. The backend handle stays open, and the file keeps its share mode, byte-range locks and lease, for SmbServerBuilder::durable_handle_timeout, or for the time a DH2Q asked for, up to that timeout or 5 minutes, whichever is longer; the DH2Q response carries the time granted. The PERSISTENT flag is ignored. A file whose lease is being broken, or that holds byte-range locks under a lease that does not cache writes, closes with its session instead. Meanwhile the same user, from the same client, takes the file back with a durable handle reconnect (DHnC, or DH2C naming the CreateGuid of a DH2Q open) on a tree of the same share that names the file, or its named stream, and its lease; the backend handle, share mode, byte-range locks and lease carry on, and later lease breaks go to the new connection. A reconnect fails with STATUS_ACCESS_DENIED from another user, STATUS_INVALID_PARAMETER for a DHnC naming another file, and STATUS_INSUFFICIENT_RESOURCES past the connection’s open limit; from another client, on another share, naming another lease, lease version or CreateGuid, a DH2C naming another file, or for a file that is not kept, with STATUS_OBJECT_NAME_NOT_FOUND. The file stays kept after a failed reconnect. One that comes while the connection holding the file is still closing waits up to 5 seconds for it. A file whose rights exceed what the user’s tree on the share grants now is closed instead of reconnected. Handle::close runs when the time runs out; at once when an open, write, size change, byte-range lock, rename or delete through an open that does not hold the lease needs what the lease caches, or when the lease stops caching handles; for an open of a named stream, also when its file is renamed, or deleted while no connected open holds the same lease; on ConfigHandle changes that end the user’s sessions or the share’s trees; and at shutdown. At most SmbServerBuilder::max_disconnected_opens files are kept at once, and a quarter of that, rounded up, for one user; past that, files close with their session. Anonymous sessions and clients that send a nil ClientGuid get no durable handle. A DH2Q that repeats the CreateGuid of a live version 2 open of the same client, with access to its data or to delete it, fails with STATUS_DUPLICATE_OBJECTID unless it is marked as a replay; one marked as a replay opens the file anew rather than answering with that open.

  • Change notifications: on every share unless Share::change_notify turns them off, a CHANGE_NOTIFY on a directory waits until a file or directory in it, or below it when the client asks for the whole tree, changes through any connection to the share:

    • A create or delete is reported as an addition or a removal.
    • A rename within one directory is reported as its old and new name together, a move to another directory as a removal from one and an addition to the other. Either is followed by a modification, under LAST_WRITE, of the directory the entry is in afterwards, unless that is the share root. An entry the rename replaces, or the renamed entry itself when only the case of its name changes, is first reported removed under its name as stored. A watched directory can be renamed, and its watches follow it.
    • Attributes and times set through FileBasicInformation are reported at once as a modification, under ATTRIBUTES and the bit of each time changed but the change time. What changed is what Handle::stat shows differently afterwards, so a value the backend does not keep reports nothing.
    • New contents are reported as a modification under SIZE, LAST_WRITE and ATTRIBUTES: a write once, when the open that wrote closes, unless that close deletes the file; a change of size through FileEndOfFileInformation, a truncation through FileAllocationInformation, and an overwrite or supersede by CREATE, at once.
    • Changes of a named stream are named file:stream, under the STREAM_* bits: a new stream as an added stream; a write, a size change, an overwrite, a rename (under both names) and a deletion as a modified stream, after a modification of the file under LAST_WRITE and LAST_ACCESS for all but the deletion; and the named streams an overwrite of the file removes as removed streams. A write is reported when the open that wrote closes; an overwrite or a size change of a stream at once, and the stream again when that open closes. A deletion of the file reports the file alone. Changes of a stream whose name has a " are not reported: clients refuse a response that names one.

    A watched directory marked for deletion, through FileDispositionInformation or by the close of an open that asked for delete-on-close while other opens of it remain, ends every watch of it: the requests waiting on them, and any request on an open of the directory while it stays delete-pending, get STATUS_DELETE_PENDING. An open that marked the directory, and any open made with delete-on-close, always gets STATUS_DELETE_PENDING; once the mark is cleared, other opens watch again. Watches of the directories above it stay, and hear the removal once the directory is deleted.

    Changes are buffered on the directory handle between requests, up to the buffer size of its first request and at most 64 KiB; when more arrive, or they do not fit the request being answered, the answer is STATUS_NOTIFY_ENUM_DIR. Watches follow paths: a change made through a symlink inside the share reaches the watchers of the path it was made through, and on Linux, as a change on disk, those of the directory the symlink leads to. Names are spelled as the client that made the change spelled them, but for the stored names above.

    Changes made outside the server, or through another share over the same directory, are reported when the backend’s Handle::watch reports them. LocalFsBackend does on Linux, through inotify, unless its watch_host_changes turns it off: additions, removals, renames within the directory, new contents when a writer closes the file (under SIZE, LAST_WRITE and ATTRIBUTES) and attribute or time changes (under ATTRIBUTES) of the entries directly in a watched directory, also for a client that asks for the whole tree. A move out of or into the directory is a removal or an addition, and so is a rename from or to a name the server cannot use. A lost kernel queue, and a watched directory renamed or moved outside the server, are answered STATUS_NOTIFY_ENUM_DIR; such a directory stays watched under its old name. A watched directory deleted outside the server, or replaced by a rename, ends its watches with STATUS_DELETE_PENDING, and every later CHANGE_NOTIFY on its handles gets the same. The parent of each watched directory is watched too, the share root’s parent included, only to see the directory go. A change in a parent is reported only to the watchers of that parent, so a change in the share root’s parent is reported to no one. One backend holds at most 4096 kernel watches, parents included. A directory past that, or one the kernel will not watch, past fs.inotify.max_user_watches or fs.inotify.max_user_instances or without /proc, gets only the changes made through the server, with a warning logged once. Other backends and platforms report only changes made through the server.

    The server leaves out the backend’s reports of its own changes. Each addition, removal or rename it made in the last 5 seconds drops one matching backend report. A modification or attribute change is dropped while an open through the server that may write the entry’s data or attributes has it open, for 5 seconds after the server reported a change to it or closed an open that wrote it or restored times fixed through it, and once after it closed an open that could write it, or created or overwrote it, and wrote nothing. A backend report waits up to 200 milliseconds for that comparison, and goes out before any change the server reports after it arrived in the same directory, or below it to a watch of the whole tree.

    The backend is called for these reports in four cases: Handle::watch, through an open of a watched directory, on the first request on that directory and on the next request after the backend’s reports stopped; a FileBasicInformation that passed something to the backend calls Handle::stat once more; a size change on a share with watches calls stat before and after; and a rename into a watched directory that may replace an entry or changes the case of a name calls, on a case-sensitive backend, ShareBackend::open of the target and stat, otherwise open of the target’s directory and Handle::list_dir with the target’s name.

  • Share listing: unless SmbServerBuilder::share_listing turns it off, IPC$ serves the srvsvc pipe, so clients can list the server’s shares. A session sees the shares it may connect to, in the order they were added, and IPC$; for an anonymous session that is the public shares, and a session that cannot encrypt does not see shares whose traffic is encrypted. Share::listed leaves a share out of the list without hiding it from lookups by name, and Share::remark sets the comment shown next to it. Backends are not called. Other pipe names fail with STATUS_OBJECT_NAME_NOT_FOUND, DCE/RPC binds that carry authentication are refused, and one connection may hold 16 pipes at once.

  • READ and WRITE lengths are bounded by the negotiated maximum sizes.

  • Each SmbError maps to one NTSTATUS for the client; see SmbError::to_nt_status.

A handle is dropped without Handle::close when the task serving its connection panics or is cancelled, and when a ConfigHandle call that ends its session or tree is cancelled, so release resources in Drop as well.

Later releases may add methods with default bodies, fields to FileInfo, BackendCapabilities, VolumeInfo and DirChange, and variants to OpenIntent and DirChangeKind. A backend that implements the required methods, builds those types through FileInfo::new, DirChange::new and the Default impls, and matches OpenIntent and DirChangeKind with a wildcard arm keeps compiling.

§Server-side copy

COPYCHUNK and COPYCHUNK_WRITE copy ranges between two open files, within a share or between two shares of one session, and either side may be a named stream. A request holds at most 256 chunks of up to 1 MiB, 16 MiB in all. On a connection that was offered OSX_COPYFILE through the AAPL context, a COPYCHUNK or COPYCHUNK_WRITE without chunks between the default streams of two files copies the whole source in one request: it truncates the target with Handle::truncate, copies the data in pieces of 1 MiB the way it copies chunks, copies the named streams where both shares serve them, and passes the creation and last write times to Handle::set_times. FSCTL_DUPLICATE_EXTENTS_TO_FILE makes a range of one file share its storage with a range of another on shares whose backend reports BackendCapabilities::block_cloning; those shares report FILE_SUPPORTS_BLOCK_REFCOUNTING and accept FSCTL_SET_SPARSE.

The server passes each chunk to ShareBackend::copy_range, except a chunk whose source and target ranges overlap in one file, which it reads and writes itself. When the backend does not implement copy_range, returns SmbError::NotSupported or SmbError::NotSameDevice, or copies fewer bytes than asked, the server reads the rest with Handle::read and writes it with Handle::write. Handle::as_any lets copy_range tell the handles of its backend from others. ShareBackend::clone_range serves duplicate extents on a backend that reports block_cloning. Byte-range locks, leases and change notifications stay with the server.

Share::server_side_copy turns these requests off for a share, cloning included, and a copy between two shares needs both to serve them.

Modules§

ntstatus
NTSTATUS values (MS-ERREF §2.3.1).
wire

Structs§

BackendCapabilities
Static, advertised capabilities of a backend. Built from BackendCapabilities::default; every field is public.
Bytes
A cheaply cloneable and sliceable chunk of contiguous memory.
ConfigHandle
DirChange
A change to an entry of a watched directory, as the backend saw it.
DirEntry
One entry of a directory listing.
Discovery
Announces the server on the local network through multicast DNS and DNS service discovery (RFC 6762, RFC 6763). Clients that browse DNS-SD, such as the Finder, GNOME Files and Dolphin, list it. Pass it to SmbServerBuilder::discovery.
Ea
One extended attribute of a file or directory (MS-FSCC 2.4.16). Built with Ea::new; every field is public.
EncryptedBackend
Serves the files of another backend with their contents encrypted, and by default their names too, under a 32-byte key. The inner backend stores ciphertext; clients see plaintext.
EncryptedBackendBuilder
Settings of an EncryptedBackend, set through the methods below and applied by build.
FileInfo
Filesystem-style metadata for a single file or directory. Built with FileInfo::new; every field is public.
FileTimes
Optional FILETIME values for set_times. None means “leave unchanged”.
LocalFsBackend
Local-filesystem backend, sandboxed at a single root directory.
OpenOptions
Options passed to ShareBackend::open.
S3Backend
Serves the objects of an S3 bucket as files and directories.
S3BackendBuilder
Settings of an S3Backend, set through the methods below and applied by build.
Share
One share definition, attached to a single backend.
ShutdownHandle
Cheaply-clonable shutdown handle. Outlives SmbServer::serve consuming the server.
SmbPath
A validated, component-list path. No .., no Windows-forbidden chars, no alternate streams. Always relative to the share root; the empty path is the root.
SmbServer
A built but not-yet-running SMB server.
SmbServerBuilder
Builder for SmbServer. See SmbServer::builder.
StreamInfo
A named stream of a file, as Handle::streams lists it. Built with StreamInfo::new; every field is public.
StreamName
Name of a named data stream: the name in file.txt:name:$DATA.
VolumeInfo
Volume behind a share, as reported by FileFsVolumeInformation, FileFsSizeInformation, FileFsFullSizeInformation, FileFsAttributeInformation and FileFsSectorSizeInformation (MS-FSCC 2.5).

Enums§

Access
Access level granted to a user on a share, or to anonymous on a public share.
BuildError
Errors raised by SmbServerBuilder::build.
ConfigError
DirChangeKind
What a backend saw happen to an entry of a watched directory.
EncryptionError
Why EncryptedBackendBuilder::build failed.
Identity
Identity recovered from a successful (or anonymous) authentication.
OpenAction
What ShareBackend::open did to the file (MS-SMB2 2.2.14 CreateAction).
OpenIntent
SMB CREATE disposition as seen by a backend.
S3Error
Why an S3 call, or the cache directory of the backend, failed.
ShareMode
SmbError
Errors returned by ShareBackend and surfaced through the SMB protocol.

Constants§

DEFAULT_DURABLE_HANDLE_TIMEOUT
Default for SmbServerBuilder::durable_handle_timeout.
DEFAULT_MAX_DISCONNECTED_OPENS
Default for SmbServerBuilder::max_disconnected_opens.
DEFAULT_MAX_LOCKS_PER_OPEN
Default for SmbServerBuilder::max_locks_per_open.
DEFAULT_MAX_OPENS_PER_CONNECTION
Default for SmbServerBuilder::max_opens_per_connection and ConfigHandle::set_max_opens_per_connection.

Traits§

DirWatch
Changes to the entries of one directory, from Handle::watch.
Handle
A live open file or directory handle.
ShareBackend
Pluggable storage backend mounted as a share.

Type Aliases§

SmbResult