2017-07-23 12:24:45 +00:00
|
|
|
package sftp
|
|
|
|
|
|
|
|
import (
|
2018-03-30 10:37:16 +00:00
|
|
|
"context"
|
2017-07-23 12:24:45 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-09-13 12:09:48 +00:00
|
|
|
// MaxFilelist is the max number of files to return in a readdir batch.
|
|
|
|
var MaxFilelist int64 = 100
|
|
|
|
|
2017-07-23 12:24:45 +00:00
|
|
|
// Request contains the data and state for the incoming service request.
|
|
|
|
type Request struct {
|
|
|
|
// Get, Put, Setstat, Stat, Rename, Remove
|
|
|
|
// Rmdir, Mkdir, List, Readlink, Symlink
|
|
|
|
Method string
|
|
|
|
Filepath string
|
|
|
|
Flags uint32
|
|
|
|
Attrs []byte // convert to sub-struct
|
|
|
|
Target string // for renames and sym-links
|
2019-01-27 20:07:57 +00:00
|
|
|
handle string
|
2017-07-23 12:24:45 +00:00
|
|
|
// reader/writer/readdir from handlers
|
2018-03-30 10:37:16 +00:00
|
|
|
state state
|
|
|
|
// context lasts duration of request
|
|
|
|
ctx context.Context
|
|
|
|
cancelCtx context.CancelFunc
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type state struct {
|
2018-03-30 10:37:16 +00:00
|
|
|
*sync.RWMutex
|
2017-09-13 12:09:48 +00:00
|
|
|
writerAt io.WriterAt
|
|
|
|
readerAt io.ReaderAt
|
|
|
|
listerAt ListerAt
|
|
|
|
lsoffset int64
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New Request initialized based on packet data
|
2018-03-30 10:37:16 +00:00
|
|
|
func requestFromPacket(ctx context.Context, pkt hasPath) *Request {
|
2017-07-23 12:24:45 +00:00
|
|
|
method := requestMethod(pkt)
|
|
|
|
request := NewRequest(method, pkt.getPath())
|
2018-03-30 10:37:16 +00:00
|
|
|
request.ctx, request.cancelCtx = context.WithCancel(ctx)
|
|
|
|
|
2017-07-23 12:24:45 +00:00
|
|
|
switch p := pkt.(type) {
|
2018-01-23 18:40:42 +00:00
|
|
|
case *sshFxpOpenPacket:
|
|
|
|
request.Flags = p.Pflags
|
2017-07-23 12:24:45 +00:00
|
|
|
case *sshFxpSetstatPacket:
|
|
|
|
request.Flags = p.Flags
|
|
|
|
request.Attrs = p.Attrs.([]byte)
|
|
|
|
case *sshFxpRenamePacket:
|
2017-09-13 12:09:48 +00:00
|
|
|
request.Target = cleanPath(p.Newpath)
|
2017-07-23 12:24:45 +00:00
|
|
|
case *sshFxpSymlinkPacket:
|
2017-09-13 12:09:48 +00:00
|
|
|
request.Target = cleanPath(p.Linkpath)
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
return request
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRequest creates a new Request object.
|
2017-09-13 12:09:48 +00:00
|
|
|
func NewRequest(method, path string) *Request {
|
2018-03-30 10:37:16 +00:00
|
|
|
return &Request{Method: method, Filepath: cleanPath(path),
|
|
|
|
state: state{RWMutex: new(sync.RWMutex)}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// shallow copy of existing request
|
|
|
|
func (r *Request) copy() *Request {
|
|
|
|
r.state.Lock()
|
|
|
|
defer r.state.Unlock()
|
|
|
|
r2 := new(Request)
|
|
|
|
*r2 = *r
|
|
|
|
return r2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context returns the request's context. To change the context,
|
|
|
|
// use WithContext.
|
|
|
|
//
|
|
|
|
// The returned context is always non-nil; it defaults to the
|
|
|
|
// background context.
|
|
|
|
//
|
|
|
|
// For incoming server requests, the context is canceled when the
|
|
|
|
// request is complete or the client's connection closes.
|
|
|
|
func (r *Request) Context() context.Context {
|
|
|
|
if r.ctx != nil {
|
|
|
|
return r.ctx
|
|
|
|
}
|
|
|
|
return context.Background()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithContext returns a copy of r with its context changed to ctx.
|
|
|
|
// The provided ctx must be non-nil.
|
|
|
|
func (r *Request) WithContext(ctx context.Context) *Request {
|
|
|
|
if ctx == nil {
|
|
|
|
panic("nil context")
|
|
|
|
}
|
|
|
|
r2 := r.copy()
|
|
|
|
r2.ctx = ctx
|
|
|
|
r2.cancelCtx = nil
|
|
|
|
return r2
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 12:09:48 +00:00
|
|
|
// Returns current offset for file list
|
|
|
|
func (r *Request) lsNext() int64 {
|
2018-03-30 10:37:16 +00:00
|
|
|
r.state.RLock()
|
|
|
|
defer r.state.RUnlock()
|
2017-09-13 12:09:48 +00:00
|
|
|
return r.state.lsoffset
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 12:09:48 +00:00
|
|
|
// Increases next offset
|
|
|
|
func (r *Request) lsInc(offset int64) {
|
2018-03-30 10:37:16 +00:00
|
|
|
r.state.Lock()
|
|
|
|
defer r.state.Unlock()
|
2017-09-13 12:09:48 +00:00
|
|
|
r.state.lsoffset = r.state.lsoffset + offset
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// manage file read/write state
|
2018-01-23 18:40:42 +00:00
|
|
|
func (r *Request) setListerState(la ListerAt) {
|
2018-03-30 10:37:16 +00:00
|
|
|
r.state.Lock()
|
|
|
|
defer r.state.Unlock()
|
2018-01-23 18:40:42 +00:00
|
|
|
r.state.listerAt = la
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 12:09:48 +00:00
|
|
|
func (r *Request) getLister() ListerAt {
|
2018-03-30 10:37:16 +00:00
|
|
|
r.state.RLock()
|
|
|
|
defer r.state.RUnlock()
|
2017-09-13 12:09:48 +00:00
|
|
|
return r.state.listerAt
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close reader/writer if possible
|
2018-01-23 18:40:42 +00:00
|
|
|
func (r *Request) close() error {
|
2018-08-01 17:43:44 +00:00
|
|
|
defer func() {
|
|
|
|
if r.cancelCtx != nil {
|
|
|
|
r.cancelCtx()
|
|
|
|
}
|
|
|
|
}()
|
2019-01-27 20:07:57 +00:00
|
|
|
r.state.RLock()
|
|
|
|
rd := r.state.readerAt
|
|
|
|
r.state.RUnlock()
|
2017-07-23 12:24:45 +00:00
|
|
|
if c, ok := rd.(io.Closer); ok {
|
2018-01-23 18:40:42 +00:00
|
|
|
return c.Close()
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
2019-01-27 20:07:57 +00:00
|
|
|
r.state.RLock()
|
|
|
|
wt := r.state.writerAt
|
|
|
|
r.state.RUnlock()
|
2017-07-23 12:24:45 +00:00
|
|
|
if c, ok := wt.(io.Closer); ok {
|
2018-01-23 18:40:42 +00:00
|
|
|
return c.Close()
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
return nil
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// called from worker to handle packet/request
|
2017-09-13 12:09:48 +00:00
|
|
|
func (r *Request) call(handlers Handlers, pkt requestPacket) responsePacket {
|
2017-07-23 12:24:45 +00:00
|
|
|
switch r.Method {
|
|
|
|
case "Get":
|
2018-01-23 18:40:42 +00:00
|
|
|
return fileget(handlers.FileGet, r, pkt)
|
2019-01-27 20:07:57 +00:00
|
|
|
case "Put":
|
2018-01-23 18:40:42 +00:00
|
|
|
return fileput(handlers.FilePut, r, pkt)
|
2017-07-23 12:24:45 +00:00
|
|
|
case "Setstat", "Rename", "Rmdir", "Mkdir", "Symlink", "Remove":
|
2018-01-23 18:40:42 +00:00
|
|
|
return filecmd(handlers.FileCmd, r, pkt)
|
2018-08-01 17:43:44 +00:00
|
|
|
case "List":
|
2018-01-23 18:40:42 +00:00
|
|
|
return filelist(handlers.FileList, r, pkt)
|
2018-08-01 17:43:44 +00:00
|
|
|
case "Stat", "Readlink":
|
|
|
|
return filestat(handlers.FileList, r, pkt)
|
2017-07-23 12:24:45 +00:00
|
|
|
default:
|
2017-09-13 12:09:48 +00:00
|
|
|
return statusFromError(pkt,
|
|
|
|
errors.Errorf("unexpected method: %s", r.Method))
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
2017-09-13 12:09:48 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 20:07:57 +00:00
|
|
|
// Additional initialization for Open packets
|
|
|
|
func (r *Request) open(h Handlers, pkt requestPacket) responsePacket {
|
|
|
|
flags := r.Pflags()
|
|
|
|
var err error
|
|
|
|
switch {
|
|
|
|
case flags.Write, flags.Append, flags.Creat, flags.Trunc:
|
|
|
|
r.Method = "Put"
|
|
|
|
r.state.writerAt, err = h.FilePut.Filewrite(r)
|
|
|
|
case flags.Read:
|
|
|
|
r.Method = "Get"
|
|
|
|
r.state.readerAt, err = h.FileGet.Fileread(r)
|
|
|
|
default:
|
|
|
|
return statusFromError(pkt, errors.New("bad file flags"))
|
2017-09-13 12:09:48 +00:00
|
|
|
}
|
2019-01-27 20:07:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return statusFromError(pkt, err)
|
|
|
|
}
|
|
|
|
return &sshFxpHandlePacket{ID: pkt.id(), Handle: r.handle}
|
|
|
|
}
|
|
|
|
func (r *Request) opendir(h Handlers, pkt requestPacket) responsePacket {
|
|
|
|
var err error
|
|
|
|
r.Method = "List"
|
|
|
|
r.state.listerAt, err = h.FileList.Filelist(r)
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case syscall.Errno:
|
|
|
|
err = &os.PathError{Path: r.Filepath, Err: err}
|
|
|
|
}
|
|
|
|
return statusFromError(pkt, err)
|
|
|
|
}
|
|
|
|
return &sshFxpHandlePacket{ID: pkt.id(), Handle: r.handle}
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrap FileReader handler
|
2018-01-23 18:40:42 +00:00
|
|
|
func fileget(h FileReader, r *Request, pkt requestPacket) responsePacket {
|
2019-01-27 20:07:57 +00:00
|
|
|
//fmt.Println("fileget", r)
|
|
|
|
r.state.RLock()
|
|
|
|
reader := r.state.readerAt
|
|
|
|
r.state.RUnlock()
|
2017-07-23 12:24:45 +00:00
|
|
|
if reader == nil {
|
2019-01-27 20:07:57 +00:00
|
|
|
return statusFromError(pkt, errors.New("unexpected read packet"))
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
_, offset, length := packetData(pkt)
|
|
|
|
data := make([]byte, clamp(length, maxTxPacket))
|
|
|
|
n, err := reader.ReadAt(data, offset)
|
2017-09-13 12:09:48 +00:00
|
|
|
// only return EOF erro if no data left to read
|
2017-07-23 12:24:45 +00:00
|
|
|
if err != nil && (err != io.EOF || n == 0) {
|
2018-01-23 18:40:42 +00:00
|
|
|
return statusFromError(pkt, err)
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
return &sshFxpDataPacket{
|
2018-01-23 18:40:42 +00:00
|
|
|
ID: pkt.id(),
|
2017-07-23 12:24:45 +00:00
|
|
|
Length: uint32(n),
|
|
|
|
Data: data[:n],
|
2017-09-13 12:09:48 +00:00
|
|
|
}
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrap FileWriter handler
|
2018-01-23 18:40:42 +00:00
|
|
|
func fileput(h FileWriter, r *Request, pkt requestPacket) responsePacket {
|
2019-01-27 20:07:57 +00:00
|
|
|
//fmt.Println("fileput", r)
|
|
|
|
r.state.RLock()
|
|
|
|
writer := r.state.writerAt
|
|
|
|
r.state.RUnlock()
|
2017-07-23 12:24:45 +00:00
|
|
|
if writer == nil {
|
2019-01-27 20:07:57 +00:00
|
|
|
return statusFromError(pkt, errors.New("unexpected write packet"))
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
data, offset, _ := packetData(pkt)
|
2019-01-27 20:07:57 +00:00
|
|
|
_, err := writer.WriteAt(data, offset)
|
2018-01-23 18:40:42 +00:00
|
|
|
return statusFromError(pkt, err)
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 20:07:57 +00:00
|
|
|
// file data for additional read/write packets
|
|
|
|
func packetData(p requestPacket) (data []byte, offset int64, length uint32) {
|
|
|
|
switch p := p.(type) {
|
|
|
|
case *sshFxpReadPacket:
|
|
|
|
length = p.Len
|
|
|
|
offset = int64(p.Offset)
|
|
|
|
case *sshFxpWritePacket:
|
|
|
|
data = p.Data
|
|
|
|
length = p.Length
|
|
|
|
offset = int64(p.Offset)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-23 12:24:45 +00:00
|
|
|
// wrap FileCmder handler
|
2018-01-23 18:40:42 +00:00
|
|
|
func filecmd(h FileCmder, r *Request, pkt requestPacket) responsePacket {
|
2018-08-01 17:43:44 +00:00
|
|
|
|
|
|
|
switch p := pkt.(type) {
|
|
|
|
case *sshFxpFsetstatPacket:
|
|
|
|
r.Flags = p.Flags
|
|
|
|
r.Attrs = p.Attrs.([]byte)
|
|
|
|
}
|
2017-07-23 12:24:45 +00:00
|
|
|
err := h.Filecmd(r)
|
2018-01-23 18:40:42 +00:00
|
|
|
return statusFromError(pkt, err)
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 12:09:48 +00:00
|
|
|
// wrap FileLister handler
|
2018-01-23 18:40:42 +00:00
|
|
|
func filelist(h FileLister, r *Request, pkt requestPacket) responsePacket {
|
2017-09-13 12:09:48 +00:00
|
|
|
var err error
|
|
|
|
lister := r.getLister()
|
|
|
|
if lister == nil {
|
2019-01-27 20:07:57 +00:00
|
|
|
return statusFromError(pkt, errors.New("unexpected dir packet"))
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-13 12:09:48 +00:00
|
|
|
offset := r.lsNext()
|
|
|
|
finfo := make([]os.FileInfo, MaxFilelist)
|
|
|
|
n, err := lister.ListAt(finfo, offset)
|
|
|
|
r.lsInc(int64(n))
|
|
|
|
// ignore EOF as we only return it when there are no results
|
|
|
|
finfo = finfo[:n] // avoid need for nil tests below
|
|
|
|
|
2017-07-23 12:24:45 +00:00
|
|
|
switch r.Method {
|
|
|
|
case "List":
|
2017-09-13 12:09:48 +00:00
|
|
|
if err != nil && err != io.EOF {
|
2018-01-23 18:40:42 +00:00
|
|
|
return statusFromError(pkt, err)
|
2017-09-13 12:09:48 +00:00
|
|
|
}
|
2018-08-01 17:43:44 +00:00
|
|
|
if err == io.EOF && n == 0 {
|
2018-01-23 18:40:42 +00:00
|
|
|
return statusFromError(pkt, io.EOF)
|
2017-09-13 12:09:48 +00:00
|
|
|
}
|
|
|
|
dirname := filepath.ToSlash(path.Base(r.Filepath))
|
2018-01-23 18:40:42 +00:00
|
|
|
ret := &sshFxpNamePacket{ID: pkt.id()}
|
2017-09-13 12:09:48 +00:00
|
|
|
|
2017-07-23 12:24:45 +00:00
|
|
|
for _, fi := range finfo {
|
|
|
|
ret.NameAttrs = append(ret.NameAttrs, sshFxpNameAttr{
|
|
|
|
Name: fi.Name(),
|
|
|
|
LongName: runLs(dirname, fi),
|
|
|
|
Attrs: []interface{}{fi},
|
|
|
|
})
|
|
|
|
}
|
2017-09-13 12:09:48 +00:00
|
|
|
return ret
|
2018-08-01 17:43:44 +00:00
|
|
|
default:
|
|
|
|
err = errors.Errorf("unexpected method: %s", r.Method)
|
|
|
|
return statusFromError(pkt, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func filestat(h FileLister, r *Request, pkt requestPacket) responsePacket {
|
|
|
|
lister, err := h.Filelist(r)
|
|
|
|
if err != nil {
|
|
|
|
return statusFromError(pkt, err)
|
|
|
|
}
|
|
|
|
finfo := make([]os.FileInfo, 1)
|
|
|
|
n, err := lister.ListAt(finfo, 0)
|
|
|
|
finfo = finfo[:n] // avoid need for nil tests below
|
|
|
|
|
|
|
|
switch r.Method {
|
2017-07-23 12:24:45 +00:00
|
|
|
case "Stat":
|
2017-09-13 12:09:48 +00:00
|
|
|
if err != nil && err != io.EOF {
|
2018-01-23 18:40:42 +00:00
|
|
|
return statusFromError(pkt, err)
|
2017-09-13 12:09:48 +00:00
|
|
|
}
|
|
|
|
if n == 0 {
|
2017-07-23 12:24:45 +00:00
|
|
|
err = &os.PathError{Op: "stat", Path: r.Filepath,
|
|
|
|
Err: syscall.ENOENT}
|
2018-01-23 18:40:42 +00:00
|
|
|
return statusFromError(pkt, err)
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
return &sshFxpStatResponse{
|
2018-01-23 18:40:42 +00:00
|
|
|
ID: pkt.id(),
|
2017-07-23 12:24:45 +00:00
|
|
|
info: finfo[0],
|
2017-09-13 12:09:48 +00:00
|
|
|
}
|
2017-07-23 12:24:45 +00:00
|
|
|
case "Readlink":
|
2017-09-13 12:09:48 +00:00
|
|
|
if err != nil && err != io.EOF {
|
2018-01-23 18:40:42 +00:00
|
|
|
return statusFromError(pkt, err)
|
2017-09-13 12:09:48 +00:00
|
|
|
}
|
|
|
|
if n == 0 {
|
2017-07-23 12:24:45 +00:00
|
|
|
err = &os.PathError{Op: "readlink", Path: r.Filepath,
|
|
|
|
Err: syscall.ENOENT}
|
2018-01-23 18:40:42 +00:00
|
|
|
return statusFromError(pkt, err)
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
filename := finfo[0].Name()
|
|
|
|
return &sshFxpNamePacket{
|
2018-01-23 18:40:42 +00:00
|
|
|
ID: pkt.id(),
|
2017-07-23 12:24:45 +00:00
|
|
|
NameAttrs: []sshFxpNameAttr{{
|
|
|
|
Name: filename,
|
|
|
|
LongName: filename,
|
|
|
|
Attrs: emptyFileStat,
|
|
|
|
}},
|
2017-09-13 12:09:48 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
err = errors.Errorf("unexpected method: %s", r.Method)
|
2018-01-23 18:40:42 +00:00
|
|
|
return statusFromError(pkt, err)
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
// init attributes of request object from packet data
|
|
|
|
func requestMethod(p requestPacket) (method string) {
|
|
|
|
switch p.(type) {
|
2019-01-27 20:07:57 +00:00
|
|
|
case *sshFxpReadPacket, *sshFxpWritePacket, *sshFxpOpenPacket:
|
|
|
|
// set in open() above
|
|
|
|
case *sshFxpOpendirPacket, *sshFxpReaddirPacket:
|
|
|
|
// set in opendir() above
|
2018-01-23 18:40:42 +00:00
|
|
|
case *sshFxpSetstatPacket, *sshFxpFsetstatPacket:
|
2017-07-23 12:24:45 +00:00
|
|
|
method = "Setstat"
|
|
|
|
case *sshFxpRenamePacket:
|
|
|
|
method = "Rename"
|
|
|
|
case *sshFxpSymlinkPacket:
|
|
|
|
method = "Symlink"
|
|
|
|
case *sshFxpRemovePacket:
|
|
|
|
method = "Remove"
|
2018-01-23 18:40:42 +00:00
|
|
|
case *sshFxpStatPacket, *sshFxpLstatPacket, *sshFxpFstatPacket:
|
2017-07-23 12:24:45 +00:00
|
|
|
method = "Stat"
|
|
|
|
case *sshFxpRmdirPacket:
|
|
|
|
method = "Rmdir"
|
|
|
|
case *sshFxpReadlinkPacket:
|
|
|
|
method = "Readlink"
|
|
|
|
case *sshFxpMkdirPacket:
|
|
|
|
method = "Mkdir"
|
|
|
|
}
|
|
|
|
return method
|
|
|
|
}
|