smb2

package module
v0.0.0-...-b3662dc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2020 License: BSD-2-Clause Imports: 32 Imported by: 0

README

smb2

Build Status GoDoc

Description

SMB2/3 client implementation.

Installation

go get github.com/joshartman/go-smb2

Documentation

http://godoc.org/github.com/joshartman/go-smb2

I have cloned this repo as I was to make sure bugs get fixed. Cloned from github.com/hirochachacha/go-smb2

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

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

fmt.Println(string(bs))

// Hello world!
Output:

Index

Examples

Constants

View Source
const MaxReadSizeLimit = 0x100000

MaxReadSizeLimit limits maxReadSize from negotiate data

View Source
const PathSeparator = '\\'

Variables

This section is empty.

Functions

func IsExist

func IsExist(err error) bool

func IsNotExist

func IsNotExist(err error) bool

func IsPathSeparator

func IsPathSeparator(c uint8) bool

func IsPermission

func IsPermission(err error) bool

Types

type Client

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

Client represents a SMB session.

func (*Client) Logoff

func (c *Client) Logoff() error

Logoff invalidates the current SMB session.

func (*Client) Mount

func (c *Client) Mount(path string) (*RemoteFileSystem, error)

Mount connects to a SMB tree.

func (*Client) WithContext

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

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.

func (*Dialer) Dial

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

Dial performs negotiation and authentication. It returns a client. It doesn't support NetBIOS transport.

func (*Dialer) DialContext

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

type FileFsInfo

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

type Initiator

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

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
	// 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

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) Chmod

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

func (*RemoteFile) Close

func (f *RemoteFile) Close() error

func (*RemoteFile) Name

func (f *RemoteFile) Name() string

func (*RemoteFile) Read

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

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) Readdir

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

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) Stat

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

func (*RemoteFile) Statfs

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

func (*RemoteFile) Sync

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

func (*RemoteFile) Truncate

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

func (*RemoteFile) WithContext

func (f *RemoteFile) WithContext(ctx context.Context) *RemoteFile

func (*RemoteFile) Write

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

func (*RemoteFile) WriteAt

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

WriteAt implements io.WriterAt.

func (*RemoteFile) WriteString

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

func (*RemoteFile) WriteTo

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

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

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) Chmod

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

func (*RemoteFileSystem) Chtimes

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

func (*RemoteFileSystem) Create

func (fs *RemoteFileSystem) Create(name string) (*RemoteFile, error)

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(dirname string) ([]os.FileInfo, error)

func (*RemoteFileSystem) ReadFile

func (fs *RemoteFileSystem) ReadFile(filename string) ([]byte, error)
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) Stat

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

func (*RemoteFileSystem) Statfs

func (fs *RemoteFileSystem) Statfs(name string) (FileFsInfo, error)
func (fs *RemoteFileSystem) 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 (*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.

func (*RemoteFileSystem) WithContext

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

func (*RemoteFileSystem) WriteFile

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

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 TransportError

type TransportError struct {
	Err error
}

TransportError represents a error come from net.Conn layer.

func (*TransportError) Error

func (err *TransportError) Error() string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL