smb2

package module
v0.0.0-...-4e61768 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2020 License: BSD-2-Clause Imports: 30 Imported by: 0

README

smb2

Build Status GoDoc

Description

SMB2/3 client implementation.

Installation

go get github.com/hirochachacha/go-smb2

Documentation

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

MY TODO
  • 拆分一个高级接口和低级接口

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

Examples

Constants

View Source
const MaxReadSizeLimit = 0x100000

MaxReadSizeLimit limits maxReadSize from negoticate 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.

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(netConn net.Conn) (*Client, error)

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

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
}

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

func (f *RemoteFile) SetBasicInfo(encoder *FileBasicInformationEncoder) (err error)

SetBasicAttr set file basic information

func (*RemoteFile) Stat

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

func (*RemoteFile) Sync

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

func (*RemoteFile) Truncate

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

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

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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