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
|
|
|
"encoding"
|
|
|
|
"io"
|
2018-08-01 17:43:44 +00:00
|
|
|
"os"
|
2018-01-23 18:40:42 +00:00
|
|
|
"path"
|
2017-07-23 12:24:45 +00:00
|
|
|
"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
|
2017-09-13 12:09:48 +00:00
|
|
|
FileList FileLister
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RequestServer abstracts the sftp protocol with an http request-like protocol
|
|
|
|
type RequestServer struct {
|
|
|
|
*serverConn
|
|
|
|
Handlers Handlers
|
2017-09-13 12:09:48 +00:00
|
|
|
pktMgr *packetManager
|
2018-01-23 18:40:42 +00:00
|
|
|
openRequests map[string]*Request
|
2017-07-23 12:24:45 +00:00
|
|
|
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 {
|
|
|
|
svrConn := &serverConn{
|
|
|
|
conn: conn{
|
|
|
|
Reader: rwc,
|
|
|
|
WriteCloser: rwc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return &RequestServer{
|
|
|
|
serverConn: svrConn,
|
|
|
|
Handlers: h,
|
|
|
|
pktMgr: newPktMgr(svrConn),
|
2018-01-23 18:40:42 +00:00
|
|
|
openRequests: make(map[string]*Request),
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
// New Open packet/Request
|
2017-09-13 12:09:48 +00:00
|
|
|
func (rs *RequestServer) nextRequest(r *Request) string {
|
2017-07-23 12:24:45 +00:00
|
|
|
rs.openRequestLock.Lock()
|
|
|
|
defer rs.openRequestLock.Unlock()
|
|
|
|
rs.handleCount++
|
|
|
|
handle := strconv.Itoa(rs.handleCount)
|
2018-01-23 18:40:42 +00:00
|
|
|
rs.openRequests[handle] = r
|
2017-07-23 12:24:45 +00:00
|
|
|
return handle
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
// Returns Request from openRequests, bool is false if it is missing
|
|
|
|
// If the method is different, save/return a new Request w/ that Method.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// the first packet of that type (read/write/etc). We create a new Request when
|
|
|
|
// it changes to set the request.Method attribute in a thread safe way.
|
|
|
|
func (rs *RequestServer) getRequest(handle, method string) (*Request, bool) {
|
2017-07-23 12:24:45 +00:00
|
|
|
rs.openRequestLock.RLock()
|
|
|
|
r, ok := rs.openRequests[handle]
|
2018-01-23 18:40:42 +00:00
|
|
|
rs.openRequestLock.RUnlock()
|
|
|
|
if !ok || r.Method == method {
|
|
|
|
return r, ok
|
|
|
|
}
|
|
|
|
// if we make it here we need to replace the request
|
|
|
|
rs.openRequestLock.Lock()
|
|
|
|
defer rs.openRequestLock.Unlock()
|
|
|
|
r, ok = rs.openRequests[handle]
|
|
|
|
if !ok || r.Method == method { // re-check needed b/c lock race
|
|
|
|
return r, ok
|
|
|
|
}
|
2018-03-30 10:37:16 +00:00
|
|
|
r = r.copy()
|
|
|
|
r.Method = method
|
2018-01-23 18:40:42 +00:00
|
|
|
rs.openRequests[handle] = r
|
|
|
|
return r, ok
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
func (rs *RequestServer) closeRequest(handle string) error {
|
2017-07-23 12:24:45 +00:00
|
|
|
rs.openRequestLock.Lock()
|
|
|
|
defer rs.openRequestLock.Unlock()
|
|
|
|
if r, ok := rs.openRequests[handle]; ok {
|
|
|
|
delete(rs.openRequests, handle)
|
2018-01-23 18:40:42 +00:00
|
|
|
return r.close()
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
return syscall.EBADF
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-30 10:37:16 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2017-07-23 12:24:45 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
runWorker := func(ch requestChan) {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2018-03-30 10:37:16 +00:00
|
|
|
if err := rs.packetWorker(ctx, ch); err != nil {
|
2017-07-23 12:24:45 +00:00
|
|
|
rs.conn.Close() // shuts down recvPacket
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
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 {
|
2018-08-01 17:43:44 +00:00
|
|
|
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
|
|
|
|
}
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pktChan <- pkt
|
|
|
|
}
|
|
|
|
|
|
|
|
close(pktChan) // shuts down sftpServerWorkers
|
|
|
|
wg.Wait() // wait for all workers to exit
|
|
|
|
|
2018-03-30 10:37:16 +00: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()
|
|
|
|
}
|
|
|
|
|
2017-07-23 12:24:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-30 10:37:16 +00:00
|
|
|
func (rs *RequestServer) packetWorker(
|
|
|
|
ctx context.Context, pktChan chan requestPacket,
|
|
|
|
) error {
|
2017-07-23 12:24:45 +00:00
|
|
|
for pkt := range pktChan {
|
|
|
|
var rpkt responsePacket
|
|
|
|
switch pkt := pkt.(type) {
|
|
|
|
case *sshFxInitPacket:
|
|
|
|
rpkt = sshFxVersionPacket{sftpProtocolVersion, nil}
|
|
|
|
case *sshFxpClosePacket:
|
|
|
|
handle := pkt.getHandle()
|
2018-01-23 18:40:42 +00:00
|
|
|
rpkt = statusFromError(pkt, rs.closeRequest(handle))
|
2017-07-23 12:24:45 +00:00
|
|
|
case *sshFxpRealpathPacket:
|
2017-09-13 12:09:48 +00:00
|
|
|
rpkt = cleanPacketPath(pkt)
|
2018-01-23 18:40:42 +00:00
|
|
|
case *sshFxpOpendirPacket:
|
2018-03-30 10:37:16 +00:00
|
|
|
request := requestFromPacket(ctx, pkt)
|
2018-08-01 17:43:44 +00:00
|
|
|
rpkt = request.call(rs.Handlers, pkt)
|
|
|
|
if stat, ok := rpkt.(*sshFxpStatResponse); ok {
|
|
|
|
if stat.info.IsDir() {
|
|
|
|
handle := rs.nextRequest(request)
|
|
|
|
rpkt = sshFxpHandlePacket{pkt.id(), handle}
|
|
|
|
} else {
|
|
|
|
rpkt = statusFromError(pkt, &os.PathError{
|
|
|
|
Path: request.Filepath, Err: syscall.ENOTDIR})
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
case *sshFxpOpenPacket:
|
2018-03-30 10:37:16 +00:00
|
|
|
request := requestFromPacket(ctx, pkt)
|
2018-01-23 18:40:42 +00:00
|
|
|
handle := rs.nextRequest(request)
|
|
|
|
rpkt = sshFxpHandlePacket{pkt.id(), handle}
|
|
|
|
if pkt.hasPflags(ssh_FXF_CREAT) {
|
2018-03-30 10:37:16 +00:00
|
|
|
if p := request.call(rs.Handlers, pkt); !statusOk(p) {
|
2018-01-23 18:40:42 +00:00
|
|
|
rpkt = p // if error in write, return it
|
|
|
|
}
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
case hasHandle:
|
|
|
|
handle := pkt.getHandle()
|
2018-01-23 18:40:42 +00:00
|
|
|
request, ok := rs.getRequest(handle, requestMethod(pkt))
|
|
|
|
if !ok {
|
2017-07-23 12:24:45 +00:00
|
|
|
rpkt = statusFromError(pkt, syscall.EBADF)
|
|
|
|
} else {
|
2017-09-13 12:09:48 +00:00
|
|
|
rpkt = request.call(rs.Handlers, pkt)
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
case hasPath:
|
2018-03-30 10:37:16 +00:00
|
|
|
request := requestFromPacket(ctx, pkt)
|
2017-09-13 12:09:48 +00:00
|
|
|
rpkt = request.call(rs.Handlers, pkt)
|
2018-03-30 10:37:16 +00:00
|
|
|
request.close()
|
2017-07-23 12:24:45 +00:00
|
|
|
default:
|
|
|
|
return errors.Errorf("unexpected packet type %T", pkt)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := rs.sendPacket(rpkt)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
// True is responsePacket is an OK status packet
|
2018-03-30 10:37:16 +00:00
|
|
|
func statusOk(rpkt responsePacket) bool {
|
2018-01-23 18:40:42 +00:00
|
|
|
p, ok := rpkt.(sshFxpStatusPacket)
|
|
|
|
return ok && p.StatusError.Code == ssh_FX_OK
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean and return name packet for file
|
2017-09-13 12:09:48 +00:00
|
|
|
func cleanPacketPath(pkt *sshFxpRealpathPacket) responsePacket {
|
|
|
|
path := cleanPath(pkt.getPath())
|
2017-07-23 12:24:45 +00:00
|
|
|
return &sshFxpNamePacket{
|
|
|
|
ID: pkt.id(),
|
|
|
|
NameAttrs: []sshFxpNameAttr{{
|
2017-09-13 12:09:48 +00:00
|
|
|
Name: path,
|
|
|
|
LongName: path,
|
2017-07-23 12:24:45 +00:00
|
|
|
Attrs: emptyFileStat,
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
// 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
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
return path.Clean(p)
|
2017-07-23 12:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wrap underlying connection methods to use packetManager
|
|
|
|
func (rs *RequestServer) sendPacket(m encoding.BinaryMarshaler) error {
|
|
|
|
if pkt, ok := m.(responsePacket); ok {
|
|
|
|
rs.pktMgr.readyPacket(pkt)
|
|
|
|
} else {
|
|
|
|
return errors.Errorf("unexpected packet type %T", m)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|