go-smb2: github.com/hirochachacha/go-smb2 Index | Examples | Files | Directories

package smb2

import "github.com/hirochachacha/go-smb2"

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

Supported features and protocol versions are declared in feature.go.

Code:

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, io.SeekStart)
if err != nil {
    panic(err)
}

bs, err := ioutil.ReadAll(f)
if err != nil {
    panic(err)
}

fmt.Println(string(bs))

// Hello world!

Index

Examples

Package Files

alias.go all.go client.go conn.go credit.go errors.go feature.go initiator.go kdf.go path.go session.go smb2.go spnego.go transport.go tree_conn.go

Constants

const MaxReadSizeLimit = 0x100000

MaxReadSizeLimit limits maxReadSize from negotiate data

const PathSeparator = '\\'

Variables

var NORMALIZE_PATH = true // normalize path arguments automatically

func IsPathSeparator Uses

func IsPathSeparator(c uint8) bool

type Client Uses

type Client = Session // deprecated type name

type ContextError Uses

type ContextError struct {
    Err error
}

ContextError wraps a context error to support os.IsTimeout function.

func (*ContextError) Error Uses

func (err *ContextError) Error() string

func (*ContextError) Timeout Uses

func (err *ContextError) Timeout() bool

type Dialer Uses

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.

func (*Dialer) Dial Uses

func (d *Dialer) Dial(tcpConn net.Conn) (*Session, error)

Dial performs negotiation and authentication. It returns a session. It doesn't support NetBIOS transport. This implementation doesn't support multi-session on the same TCP connection. If you want to use another session, you need to prepare another TCP connection at first.

func (*Dialer) DialContext Uses

func (d *Dialer) DialContext(ctx context.Context, tcpConn net.Conn) (*Session, error)

DialContext performs negotiation and authentication using the provided context. Note that returned session doesn't inherit context. If you want to use the same context, call Session.WithContext manually. This implementation doesn't support multi-session on the same TCP connection. If you want to use another session, you need to prepare another TCP connection at first.

type File Uses

type File struct {
    // contains filtered or unexported fields
}

func (*File) Chmod Uses

func (f *File) Chmod(mode os.FileMode) error

func (*File) Close Uses

func (f *File) Close() error

func (*File) Name Uses

func (f *File) Name() string

func (*File) Read Uses

func (f *File) Read(b []byte) (n int, err error)

func (*File) ReadAt Uses

func (f *File) ReadAt(b []byte, off int64) (n int, err error)

ReadAt implements io.ReaderAt.

func (*File) ReadFrom Uses

func (f *File) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom implements io.ReadFrom. If r is *File on the same *Share as f, it invokes server-side copy.

func (*File) Readdir Uses

func (f *File) Readdir(n int) (fi []os.FileInfo, err error)

func (*File) Readdirnames Uses

func (f *File) Readdirnames(n int) (names []string, err error)

func (*File) Seek Uses

func (f *File) Seek(offset int64, whence int) (ret int64, err error)

Seek implements io.Seeker.

func (*File) Stat Uses

func (f *File) Stat() (os.FileInfo, error)

func (*File) Statfs Uses

func (f *File) Statfs() (FileFsInfo, error)

func (*File) Sync Uses

func (f *File) Sync() (err error)

func (*File) Truncate Uses

func (f *File) Truncate(size int64) error

func (*File) Write Uses

func (f *File) Write(b []byte) (n int, err error)

func (*File) WriteAt Uses

func (f *File) WriteAt(b []byte, off int64) (n int, err error)

WriteAt implements io.WriterAt.

func (*File) WriteString Uses

func (f *File) WriteString(s string) (n int, err error)

func (*File) WriteTo Uses

func (f *File) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements io.WriteTo. If w is *File on the same *Share as f, it invokes server-side copy.

type FileFsInfo Uses

type FileFsInfo interface {
    BlockSize() uint64
    FragmentSize() uint64
    TotalBlockCount() uint64
    FreeBlockCount() uint64
    AvailableBlockCount() uint64
}

type FileStat Uses

type FileStat struct {
    CreationTime   time.Time
    LastAccessTime time.Time
    LastWriteTime  time.Time
    ChangeTime     time.Time
    EndOfFile      int64
    AllocationSize int64
    FileAttributes uint32
    FileName       string
}

func (*FileStat) IsDir Uses

func (fs *FileStat) IsDir() bool

func (*FileStat) ModTime Uses

func (fs *FileStat) ModTime() time.Time

func (*FileStat) Mode Uses

func (fs *FileStat) Mode() os.FileMode

func (*FileStat) Name Uses

func (fs *FileStat) Name() string

func (*FileStat) Size Uses

func (fs *FileStat) Size() int64

func (*FileStat) Sys Uses

func (fs *FileStat) Sys() interface{}

type Initiator Uses

type Initiator interface {
    // contains filtered or unexported methods
}

type InternalError Uses

type InternalError struct {
    Message string
}

InternalError represents internal error.

func (*InternalError) Error Uses

func (err *InternalError) Error() string

type InvalidResponseError Uses

type InvalidResponseError struct {
    Message string
}

InvalidResponseError represents a data sent by the server is corrupted or unexpected.

func (*InvalidResponseError) Error Uses

func (err *InvalidResponseError) Error() string

type NTLMInitiator Uses

type NTLMInitiator struct {
    User        string
    Password    string
    Hash        []byte
    Domain      string
    Workstation string
    TargetSPN   string
    // contains filtered or unexported fields
}

NTLMInitiator implements session-setup through NTLMv2. It doesn't support NTLMv1. You can use Hash instead of Password.

type Negotiator Uses

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 Uses

type RemoteFile = File // deprecated type name

type RemoteFileStat Uses

type RemoteFileStat = FileStat // deprecated type name

type RemoteFileSystem Uses

type RemoteFileSystem = Share // deprecated type name

type ResponseError Uses

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 Uses

func (err *ResponseError) Error() string

type Session Uses

type Session struct {
    // contains filtered or unexported fields
}

Session represents a SMB session.

func (*Session) ListSharenames Uses

func (c *Session) ListSharenames() ([]string, error)

func (*Session) Logoff Uses

func (c *Session) Logoff() error

Logoff invalidates the current SMB session.

func (*Session) Mount Uses

func (c *Session) Mount(sharename string) (*Share, error)

Mount mounts the SMB share. sharename must follow format like `<share>` or `\\<server>\<share>`. Note that the mounted share doesn't inherit session's context. If you want to use the same context, call Share.WithContext manually.

func (*Session) WithContext Uses

func (c *Session) WithContext(ctx context.Context) *Session

type Share Uses

type Share struct {
    // contains filtered or unexported fields
}

Share represents a SMB tree connection with VFS interface.

func (*Share) Chmod Uses

func (fs *Share) Chmod(name string, mode os.FileMode) error

func (*Share) Chtimes Uses

func (fs *Share) Chtimes(name string, atime time.Time, mtime time.Time) error

func (*Share) Create Uses

func (fs *Share) Create(name string) (*File, error)

func (*Share) Lstat Uses

func (fs *Share) Lstat(name string) (os.FileInfo, error)

func (*Share) Mkdir Uses

func (fs *Share) Mkdir(name string, perm os.FileMode) error

func (*Share) MkdirAll Uses

func (fs *Share) MkdirAll(path string, perm os.FileMode) error

MkdirAll mimics os.MkdirAll

func (*Share) Open Uses

func (fs *Share) Open(name string) (*File, error)

func (*Share) OpenFile Uses

func (fs *Share) OpenFile(name string, flag int, perm os.FileMode) (*File, error)

func (*Share) ReadDir Uses

func (fs *Share) ReadDir(dirname string) ([]os.FileInfo, error)

func (*Share) ReadFile Uses

func (fs *Share) ReadFile(filename string) ([]byte, error)
func (fs *Share) Readlink(name string) (string, error)

func (*Share) Remove Uses

func (fs *Share) Remove(name string) error

func (*Share) RemoveAll Uses

func (fs *Share) 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 (*Share) Rename Uses

func (fs *Share) Rename(oldpath, newpath string) error

func (*Share) Stat Uses

func (fs *Share) Stat(name string) (os.FileInfo, error)

func (*Share) Statfs Uses

func (fs *Share) Statfs(name string) (FileFsInfo, error)
func (fs *Share) Symlink(target, linkpath string) error

Symlink mimics os.Symlink. This API should work on latest Windows and latest MacOS. However it may not work on Linux because Samba doesn't support reparse point well. Also there is a restriction on target pathname. Generally, a pathname begins with leading backslash (e.g `\dir\name`) can be interpreted as two ways. On windows, it is evaluated as a relative path, on other systems, it is evaluated as an absolute path. This implementation always assumes that format is absolute path. So, if you know the target 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 instead.

func (*Share) Truncate Uses

func (fs *Share) Truncate(name string, size int64) error

func (*Share) Umount Uses

func (fs *Share) Umount() error

Umount disconects the current SMB tree.

func (*Share) WithContext Uses

func (fs *Share) WithContext(ctx context.Context) *Share

func (*Share) WriteFile Uses

func (fs *Share) WriteFile(filename string, data []byte, perm os.FileMode) error

type TransportError Uses

type TransportError struct {
    Err error
}

TransportError represents a error come from net.Conn layer.

func (*TransportError) Error Uses

func (err *TransportError) Error() string

Directories

PathSynopsis
internal/crypto/ccm
internal/crypto/cmac
internal/erref
internal/msrpc
internal/ntlm
internal/smb2
internal/spnego
internal/utf16le

Package smb2 imports 34 packages (graph) and is imported by 1 packages. Updated 2020-10-30. Refresh now. Tools for package owners.