Documentation ¶
Overview ¶
Package smb2 implements the SMB2/3 client in [MS-SMB2].
https://msdn.microsoft.com/en-us/library/cc246482.aspx
This package doesn't support CAP_UNIX extension. Symlink is supported by FSCTL_SET_REPARSE_POINT and FSCTL_GET_REPARSE_POINT. The symlink-following algorithm is explained in 2.2.2.2.1 and 2.2.2.2.1.1.
https://msdn.microsoft.com/en-us/library/cc246542.aspx
Path restrictions:
You cannot use slash as a separator, use backslash instead. You cannot use leading backslash in pathname. (except mount path and symlink target)
Supported features and protocol versions are declared in feature.go.
Example ¶
conn, err := net.Dial("tcp", "localhost:445") if err != nil { panic(err) } defer conn.Close() d := &smb2.Dialer{ Initiator: &smb2.NTLMInitiator{ User: "Guest", Password: "", Domain: "MicrosoftAccount", }, } c, err := d.Dial(conn) if err != nil { panic(err) } defer c.Logoff() fs, err := c.Mount(`\\localhost\share`) if err != nil { panic(err) } defer fs.Umount() f, err := fs.Create("hello.txt") if err != nil { panic(err) } defer fs.Remove("hello.txt") defer f.Close() _, err = f.Write([]byte("Hello world!")) if err != nil { panic(err) } _, err = f.Seek(0, os.SEEK_SET) if err != nil { panic(err) } bs, err := ioutil.ReadAll(f) if err != nil { panic(err) } fmt.Println(string(bs)) // Hello world!
Output:
Index ¶
- Constants
- func IsExist(err error) bool
- func IsNotExist(err error) bool
- func IsPathSeparator(c uint8) bool
- func IsPermission(err error) bool
- type Client
- type Dialer
- type Initiator
- type InternalError
- type InvalidResponseError
- type MultipleError
- type NTLMInitiator
- type Negotiator
- type RemoteFile
- func (f *RemoteFile) Close() error
- func (f *RemoteFile) Name() string
- func (f *RemoteFile) Read(b []byte) (n int, err error)
- func (f *RemoteFile) ReadAt(b []byte, off int64) (n int, err error)
- func (f *RemoteFile) ReadFrom(r io.Reader) (n int64, err error)
- func (f *RemoteFile) Readdir(n int) (fi []os.FileInfo, err error)
- func (f *RemoteFile) Readdirnames(n int) (names []string, err error)
- func (f *RemoteFile) Seek(offset int64, whence int) (ret int64, err error)
- func (f *RemoteFile) SetBasicInfo(encoder *FileBasicInformationEncoder) (err error)
- func (f *RemoteFile) Stat() (os.FileInfo, error)
- func (f *RemoteFile) Sync() (err error)
- func (f *RemoteFile) Truncate(size int64) error
- func (f *RemoteFile) Write(b []byte) (n int, err error)
- func (f *RemoteFile) WriteAt(b []byte, off int64) (n int, err error)
- func (f *RemoteFile) WriteTo(w io.Writer) (n int64, err error)
- type RemoteFileStat
- type RemoteFileSystem
- func (fs *RemoteFileSystem) Attr(name string, desiredAccess uint32) (FileAllInformationDecoder, error)
- func (fs *RemoteFileSystem) Create(name string) (*RemoteFile, error)
- func (fs *RemoteFileSystem) LMkdir(name string, desiredAccess uint32) error
- func (fs *RemoteFileSystem) Lstat(name string) (os.FileInfo, error)
- func (fs *RemoteFileSystem) Mkdir(name string, perm os.FileMode) error
- func (fs *RemoteFileSystem) MkdirAll(path string, perm os.FileMode) error
- func (fs *RemoteFileSystem) Open(name string) (*RemoteFile, error)
- func (fs *RemoteFileSystem) OpenFile(name string, flag int, perm os.FileMode) (*RemoteFile, error)
- func (fs *RemoteFileSystem) ReadDir(path string) (fi []os.FileInfo, err error)
- func (fs *RemoteFileSystem) Readlink(name string) (string, error)
- func (fs *RemoteFileSystem) Remove(name string) error
- func (fs *RemoteFileSystem) RemoveAll(path string) error
- func (fs *RemoteFileSystem) Rename(oldpath, newpath string) error
- func (fs *RemoteFileSystem) Stat(name string) (os.FileInfo, error)
- func (fs *RemoteFileSystem) Symlink(target, linkpath string) error
- func (fs *RemoteFileSystem) Truncate(name string, size int64) error
- func (fs *RemoteFileSystem) Umount() error
- type ResponseError
- type TimeoutError
- type TransportError
Examples ¶
Constants ¶
const MaxReadSizeLimit = 0x100000
MaxReadSizeLimit limits maxReadSize from negoticate data
const PathSeparator = '\\'
Variables ¶
This section is empty.
Functions ¶
func IsNotExist ¶
func IsPathSeparator ¶
func IsPermission ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents a SMB session.
type Dialer ¶
type Dialer struct { MaxCreditBalance uint16 // if it's zero, clientMaxCreditBalance is used. (See feature.go for more details) Negotiator Negotiator Initiator Initiator }
Dialer contains options for func (*Dialer) Dial.
type InternalError ¶
type InternalError struct {
Message string
}
InternalError represents internal error.
func (*InternalError) Error ¶
func (err *InternalError) Error() string
type InvalidResponseError ¶
type InvalidResponseError struct {
Message string
}
InvalidResponseError represents a data sent by the server is corrupted or unexpected.
func (*InvalidResponseError) Error ¶
func (err *InvalidResponseError) Error() string
type MultipleError ¶
type MultipleError []error
func (MultipleError) Error ¶
func (err MultipleError) Error() string
type NTLMInitiator ¶
type NTLMInitiator struct { User string Password string Hash []byte Domain string Workstation string TargetSPN string }
NTLMInitiator implements session-setup through NTLMv2. It doesn't support NTLMv1. You can use Hash instead of Password.
type Negotiator ¶
type Negotiator struct { RequireMessageSigning bool // enforce signing? ClientGuid [16]byte // if it's zero, generated by crypto/rand. SpecifiedDialect uint16 // if it's zero, clientDialects is used. (See feature.go for more details) }
Negotiator contains options for func (*Dialer) Dial.
type RemoteFile ¶
type RemoteFile struct {
// contains filtered or unexported fields
}
func (*RemoteFile) Close ¶
func (f *RemoteFile) Close() error
func (*RemoteFile) Name ¶
func (f *RemoteFile) Name() string
func (*RemoteFile) ReadAt ¶
func (f *RemoteFile) ReadAt(b []byte, off int64) (n int, err error)
ReadAt implements io.ReaderAt.
func (*RemoteFile) ReadFrom ¶
func (f *RemoteFile) ReadFrom(r io.Reader) (n int64, err error)
ReadFrom implements io.ReadFrom. If r is *RemoteFile on the same *RemoteFileSystem as f, it invokes server-side copy.
func (*RemoteFile) Readdirnames ¶
func (f *RemoteFile) Readdirnames(n int) (names []string, err error)
func (*RemoteFile) Seek ¶
func (f *RemoteFile) Seek(offset int64, whence int) (ret int64, err error)
Seek implements io.Seeker.
func (*RemoteFile) SetBasicInfo ¶
func (f *RemoteFile) SetBasicInfo(encoder *FileBasicInformationEncoder) (err error)
SetBasicAttr set file basic information
func (*RemoteFile) Sync ¶
func (f *RemoteFile) Sync() (err error)
func (*RemoteFile) Truncate ¶
func (f *RemoteFile) Truncate(size int64) error
type RemoteFileStat ¶
type RemoteFileStat struct { CreationTime time.Time LastAccessTime time.Time LastWriteTime time.Time ChangeTime time.Time EndOfFile int64 AllocationSize int64 FileAttributes uint32 FileName string }
func (*RemoteFileStat) IsDir ¶
func (fs *RemoteFileStat) IsDir() bool
func (*RemoteFileStat) ModTime ¶
func (fs *RemoteFileStat) ModTime() time.Time
func (*RemoteFileStat) Mode ¶
func (fs *RemoteFileStat) Mode() os.FileMode
func (*RemoteFileStat) Name ¶
func (fs *RemoteFileStat) Name() string
func (*RemoteFileStat) Size ¶
func (fs *RemoteFileStat) Size() int64
func (*RemoteFileStat) Sys ¶
func (fs *RemoteFileStat) Sys() interface{}
type RemoteFileSystem ¶
type RemoteFileSystem struct {
// contains filtered or unexported fields
}
RemoteFileSystem represents a SMB tree connection with VFS interface.
func (*RemoteFileSystem) Attr ¶
func (fs *RemoteFileSystem) Attr(name string, desiredAccess uint32) (FileAllInformationDecoder, error)
Attr get file all attr
func (*RemoteFileSystem) Create ¶
func (fs *RemoteFileSystem) Create(name string) (*RemoteFile, error)
func (*RemoteFileSystem) LMkdir ¶
func (fs *RemoteFileSystem) LMkdir(name string, desiredAccess uint32) error
LMkdir Low level mkdir
func (*RemoteFileSystem) Lstat ¶
func (fs *RemoteFileSystem) Lstat(name string) (os.FileInfo, error)
func (*RemoteFileSystem) Mkdir ¶
func (fs *RemoteFileSystem) Mkdir(name string, perm os.FileMode) error
func (*RemoteFileSystem) MkdirAll ¶
func (fs *RemoteFileSystem) MkdirAll(path string, perm os.FileMode) error
MkdirAll mimics os.MkdirAll
func (*RemoteFileSystem) Open ¶
func (fs *RemoteFileSystem) Open(name string) (*RemoteFile, error)
func (*RemoteFileSystem) OpenFile ¶
func (fs *RemoteFileSystem) OpenFile(name string, flag int, perm os.FileMode) (*RemoteFile, error)
func (*RemoteFileSystem) ReadDir ¶
func (fs *RemoteFileSystem) ReadDir(path string) (fi []os.FileInfo, err error)
func (*RemoteFileSystem) Readlink ¶
func (fs *RemoteFileSystem) Readlink(name string) (string, error)
func (*RemoteFileSystem) Remove ¶
func (fs *RemoteFileSystem) Remove(name string) error
func (*RemoteFileSystem) RemoveAll ¶
func (fs *RemoteFileSystem) RemoveAll(path string) error
RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil (no error).
func (*RemoteFileSystem) Rename ¶
func (fs *RemoteFileSystem) Rename(oldpath, newpath string) error
func (*RemoteFileSystem) Symlink ¶
func (fs *RemoteFileSystem) Symlink(target, linkpath string) error
Symlink mimics os.Symlink. There is a restriction about target pathname. Generally, a pathname begins with leading backslash (e.g `\dir\name`) can be interpreted as two ways. In windows, it is evaluated as a relative path, in other systems, it is evaluated as an absolute path. This implementation always assumes that format is absolute path. So, if you know the server is windows, you should avoid that format. If you want to use an absolute target path on windows, you can use `C:\dir\name` format.
func (*RemoteFileSystem) Truncate ¶
func (fs *RemoteFileSystem) Truncate(name string, size int64) error
func (*RemoteFileSystem) Umount ¶
func (fs *RemoteFileSystem) Umount() error
Umount disconects the current SMB tree.
type ResponseError ¶
type ResponseError struct { Code uint32 // NTSTATUS // contains filtered or unexported fields }
ResponseError represents a error with a nt status code sent by the server. The NTSTATUS is defined in [MS-ERREF]. https://msdn.microsoft.com/en-au/library/cc704588.aspx
func (*ResponseError) Error ¶
func (err *ResponseError) Error() string
type TimeoutError ¶
type TimeoutError struct {
// contains filtered or unexported fields
}
func (*TimeoutError) Error ¶
func (err *TimeoutError) Error() string
type TransportError ¶
type TransportError struct {
Err error
}
TransportError represents a error come from net.Conn layer.
func (*TransportError) Error ¶
func (err *TransportError) Error() string