rclone/vendor/github.com/pkg/sftp/request-server.go

220 lines
5.5 KiB
Go
Raw Normal View History

package sftp
import (
2018-03-19 23:51:38 +08:00
"context"
"io"
"path"
"path/filepath"
"strconv"
"sync"
"syscall"
"github.com/pkg/errors"
)
var maxTxPacket uint32 = 1 << 15
// Handlers contains the 4 SFTP server request handlers.
type Handlers struct {
FileGet FileReader
FilePut FileWriter
FileCmd FileCmder
FileList FileLister
}
// RequestServer abstracts the sftp protocol with an http request-like protocol
type RequestServer struct {
2017-07-23 15:51:42 +08:00
*serverConn
Handlers Handlers
pktMgr *packetManager
openRequests map[string]*Request
openRequestLock sync.RWMutex
handleCount int
}
// NewRequestServer creates/allocates/returns new RequestServer.
// Normally there there will be one server per user-session.
func NewRequestServer(rwc io.ReadWriteCloser, h Handlers) *RequestServer {
2017-07-23 15:51:42 +08:00
svrConn := &serverConn{
conn: conn{
Reader: rwc,
WriteCloser: rwc,
},
}
return &RequestServer{
serverConn: svrConn,
Handlers: h,
2017-07-23 15:51:42 +08:00
pktMgr: newPktMgr(svrConn),
openRequests: make(map[string]*Request),
}
}
// New Open packet/Request
func (rs *RequestServer) nextRequest(r *Request) string {
rs.openRequestLock.Lock()
defer rs.openRequestLock.Unlock()
rs.handleCount++
handle := strconv.Itoa(rs.handleCount)
2019-02-09 20:50:35 +08:00
r.handle = handle
rs.openRequests[handle] = r
return handle
}
2019-02-09 20:50:35 +08:00
// Returns Request from openRequests, bool is false if it is missing.
//
// The Requests in openRequests work essentially as open file descriptors that
// you can do different things with. What you are doing with it are denoted by
2019-02-09 20:50:35 +08:00
// the first packet of that type (read/write/etc).
func (rs *RequestServer) getRequest(handle string) (*Request, bool) {
rs.openRequestLock.RLock()
2019-02-09 20:50:35 +08:00
defer rs.openRequestLock.RUnlock()
r, ok := rs.openRequests[handle]
return r, ok
}
2019-02-09 20:50:35 +08:00
// Close the Request and clear from openRequests map
func (rs *RequestServer) closeRequest(handle string) error {
rs.openRequestLock.Lock()
defer rs.openRequestLock.Unlock()
if r, ok := rs.openRequests[handle]; ok {
delete(rs.openRequests, handle)
return r.close()
}
return syscall.EBADF
}
// Close the read/write/closer to trigger exiting the main server loop
func (rs *RequestServer) Close() error { return rs.conn.Close() }
// Serve requests for user session
func (rs *RequestServer) Serve() error {
2018-03-19 23:51:38 +08:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var wg sync.WaitGroup
runWorker := func(ch chan orderedRequest) {
wg.Add(1)
2017-07-23 15:51:42 +08:00
go func() {
defer wg.Done()
2018-03-19 23:51:38 +08:00
if err := rs.packetWorker(ctx, ch); err != nil {
2017-07-23 15:51:42 +08:00
rs.conn.Close() // shuts down recvPacket
}
}()
}
2017-07-23 15:51:42 +08:00
pktChan := rs.pktMgr.workerChan(runWorker)
var err error
var pkt requestPacket
var pktType uint8
var pktBytes []byte
for {
pktType, pktBytes, err = rs.recvPacket()
if err != nil {
break
}
pkt, err = makePacket(rxPacket{fxp(pktType), pktBytes})
if err != nil {
switch errors.Cause(err) {
case errUnknownExtendedPacket:
if err := rs.serverConn.sendError(pkt, ErrSshFxOpUnsupported); err != nil {
debug("failed to send err packet: %v", err)
rs.conn.Close() // shuts down recvPacket
break
}
default:
debug("makePacket err: %v", err)
rs.conn.Close() // shuts down recvPacket
break
}
}
pktChan <- rs.pktMgr.newOrderedRequest(pkt)
}
close(pktChan) // shuts down sftpServerWorkers
wg.Wait() // wait for all workers to exit
2018-03-19 23:51:38 +08:00
// make sure all open requests are properly closed
// (eg. possible on dropped connections, client crashes, etc.)
for handle, req := range rs.openRequests {
delete(rs.openRequests, handle)
req.close()
}
return err
}
2018-03-19 23:51:38 +08:00
func (rs *RequestServer) packetWorker(
ctx context.Context, pktChan chan orderedRequest,
2018-03-19 23:51:38 +08:00
) error {
for pkt := range pktChan {
var rpkt responsePacket
switch pkt := pkt.requestPacket.(type) {
case *sshFxInitPacket:
rpkt = sshFxVersionPacket{Version: sftpProtocolVersion}
case *sshFxpClosePacket:
handle := pkt.getHandle()
rpkt = statusFromError(pkt, rs.closeRequest(handle))
case *sshFxpRealpathPacket:
rpkt = cleanPacketPath(pkt)
case *sshFxpOpendirPacket:
2018-03-19 23:51:38 +08:00
request := requestFromPacket(ctx, pkt)
2019-02-09 20:50:35 +08:00
rs.nextRequest(request)
rpkt = request.opendir(rs.Handlers, pkt)
case *sshFxpOpenPacket:
2018-03-19 23:51:38 +08:00
request := requestFromPacket(ctx, pkt)
2019-02-09 20:50:35 +08:00
rs.nextRequest(request)
rpkt = request.open(rs.Handlers, pkt)
case *sshFxpFstatPacket:
handle := pkt.getHandle()
request, ok := rs.getRequest(handle)
if !ok {
rpkt = statusFromError(pkt, syscall.EBADF)
} else {
request = NewRequest("Stat", request.Filepath)
rpkt = request.call(rs.Handlers, pkt)
}
case hasHandle:
handle := pkt.getHandle()
2019-02-09 20:50:35 +08:00
request, ok := rs.getRequest(handle)
if !ok {
rpkt = statusFromError(pkt, syscall.EBADF)
} else {
rpkt = request.call(rs.Handlers, pkt)
}
case hasPath:
2018-03-19 23:51:38 +08:00
request := requestFromPacket(ctx, pkt)
rpkt = request.call(rs.Handlers, pkt)
2018-03-19 23:51:38 +08:00
request.close()
default:
return errors.Errorf("unexpected packet type %T", pkt)
}
rs.pktMgr.readyPacket(
rs.pktMgr.newOrderedResponse(rpkt, pkt.orderId()))
}
return nil
}
// clean and return name packet for file
func cleanPacketPath(pkt *sshFxpRealpathPacket) responsePacket {
path := cleanPath(pkt.getPath())
return &sshFxpNamePacket{
ID: pkt.id(),
NameAttrs: []sshFxpNameAttr{{
Name: path,
LongName: path,
Attrs: emptyFileStat,
}},
}
}
// Makes sure we have a clean POSIX (/) absolute path to work with
func cleanPath(p string) string {
p = filepath.ToSlash(p)
if !filepath.IsAbs(p) {
p = "/" + p
}
return path.Clean(p)
}