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?;§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.
What the protocol layer does around the backend calls:
- Paths arrive as
SmbPath, relative to the share root..components are dropped;.., empty components,<>:"|?*, control characters and stream names are rejected before the backend sees them. The empty path is the share root. openfails withSmbError::PathNotFoundwhen a directory on the way to the path is missing, and withSmbError::NotFoundwhen 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.OpenOptionscarries the result:read,writeandwrite_attributesreflect the access granted to the open. READ and WRITE on a handle without the matching access fail before the handle is called. WhenBackendCapabilities::is_read_onlyis set, trees on the share get read access only, so opens that ask for write access, and dispositions that create or truncate, fail beforeopen. - 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. A CREATE that would truncate first opens the file without write access, runs the checks and enters the open in the table, so the sharing mode holds whileopentruncates. Any other CREATE refused by the share-mode check has already calledopenand closes the new handle again. Handle::closeruns on every path that ends an open: CLOSE, TREE_DISCONNECT, LOGOFF, the end of a session or connection, andConfigHandlechanges that end sessions or trees. Before it,Handle::set_timesputs back access and write times that FileBasicInformation froze or set through that open.- Delete-on-close:
OpenOptions::delete_on_closeand FileDispositionInformation make the file delete-pending. When its last open ends, on any of the paths above, the protocol layer callsHandle::closeand thenShareBackend::unlinkon the file’s current path.opendoesn’t delete anything itself. Read-only files are refused before that point, and so, for SET_INFO, are directories thatHandle::dir_is_emptyreports as not empty.unlinkreturnsSmbError::NotEmptyfor a directory that has entries; the directory then stays. - A rename through SET_INFO calls
ShareBackend::renameand then moves the path of every open at or below the old name. Renames on one share run one at a time. 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_attributeswhen they differ from whatHandle::statreports. If that returnsSmbError::NotSupported, it retries with READONLY alone when that was asked for; the CREATE succeeds either way. - FileBasicInformation passes
Handle::set_attributesandHandle::set_timesonly the values that differ from whatHandle::statreports. After a write or truncate,set_timesalso puts back the times FileBasicInformation froze or set.Handle::truncateis never called on a directory. - QUERY_DIRECTORY calls
Handle::list_dirwith the search pattern on the first query and on a restart, then pages through the returned entries itself. Below the share root it adds.and.., and opens the parent directory to describe... - FileStandardInformation and FileAllInformation report
Handle::link_countas 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 fromShareBackend::volume_info. - READ and WRITE lengths are bounded by the negotiated maximum sizes.
- Each
SmbErrormaps to one NTSTATUS for the client; seeSmbError::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 and VolumeInfo, and variants
to OpenIntent. A backend that implements the required methods,
builds those types through FileInfo::new and the Default impls, and
matches OpenIntent with a wildcard arm keeps compiling.
Modules§
Structs§
- Backend
Capabilities - Static, advertised capabilities of a backend. Built from
BackendCapabilities::default; every field is public. - Bytes
- A cheaply cloneable and sliceable chunk of contiguous memory.
- Config
Handle - DirEntry
- One entry of a directory listing.
- File
Info - Filesystem-style metadata for a single file or directory. Built with
FileInfo::new; every field is public. - File
Times - Optional FILETIME values for
set_times.Nonemeans “leave unchanged”. - Local
FsBackend - Local-filesystem backend, sandboxed at a single root directory.
- Open
Options - Options passed to
ShareBackend::open. - Share
- One share definition, attached to a single backend.
- Shutdown
Handle - Cheaply-clonable shutdown handle. Outlives
SmbServer::serveconsuming 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.
- SmbServer
Builder - Builder for
SmbServer. SeeSmbServer::builder. - Volume
Info - 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.
- Build
Error - Errors raised by
SmbServerBuilder::build. - Config
Error - Identity
- Identity recovered from a successful (or anonymous) authentication.
- Open
Action - What
ShareBackend::opendid to the file (MS-SMB2 2.2.14 CreateAction). - Open
Intent - SMB CREATE disposition as seen by a backend.
- Share
Mode - SmbError
- Errors returned by
ShareBackendand surfaced through the SMB protocol.
Constants§
- DEFAULT_
MAX_ LOCKS_ PER_ OPEN - Default for
SmbServerBuilder::max_locks_per_open. - DEFAULT_
MAX_ OPENS_ PER_ CONNECTION - Default for
SmbServerBuilder::max_opens_per_connectionandConfigHandle::set_max_opens_per_connection.
Traits§
- Handle
- A live open file or directory handle.
- Share
Backend - Pluggable storage backend mounted as a share.