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?;

§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.
  • 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. 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 while open truncates. 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. Before it, 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 calls ShareBackend::rename and 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_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. Below the share root it adds . and .., and opens the parent directory to describe ...
  • 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.
  • 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 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§

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
DirEntry
One entry of a directory listing.
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.
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.
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
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.
ShareMode
SmbError
Errors returned by ShareBackend and 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_connection and ConfigHandle::set_max_opens_per_connection.

Traits§

Handle
A live open file or directory handle.
ShareBackend
Pluggable storage backend mounted as a share.

Type Aliases§

SmbResult