mirror of
https://github.com/drakkan/sftpgo.git
synced 2024-11-28 18:40:35 +00:00
002a06629e
Fixes #792 Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package sftpd
|
|
|
|
import (
|
|
"io"
|
|
"net"
|
|
|
|
"github.com/pkg/sftp"
|
|
|
|
"github.com/drakkan/sftpgo/v2/common"
|
|
"github.com/drakkan/sftpgo/v2/dataprovider"
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
|
)
|
|
|
|
type subsystemChannel struct {
|
|
reader io.Reader
|
|
writer io.Writer
|
|
}
|
|
|
|
func (s *subsystemChannel) Read(p []byte) (int, error) {
|
|
return s.reader.Read(p)
|
|
}
|
|
|
|
func (s *subsystemChannel) Write(p []byte) (int, error) {
|
|
return s.writer.Write(p)
|
|
}
|
|
|
|
func (s *subsystemChannel) Close() error {
|
|
return nil
|
|
}
|
|
|
|
func newSubsystemChannel(reader io.Reader, writer io.Writer) *subsystemChannel {
|
|
return &subsystemChannel{
|
|
reader: reader,
|
|
writer: writer,
|
|
}
|
|
}
|
|
|
|
// ServeSubSystemConnection handles a connection as SSH subsystem
|
|
func ServeSubSystemConnection(user *dataprovider.User, connectionID string, reader io.Reader, writer io.Writer) error {
|
|
err := user.CheckFsRoot(connectionID)
|
|
if err != nil {
|
|
errClose := user.CloseFs()
|
|
logger.Warn(logSender, connectionID, "unable to check fs root: %v close fs error: %v", err, errClose)
|
|
return err
|
|
}
|
|
|
|
connection := &Connection{
|
|
BaseConnection: common.NewBaseConnection(connectionID, common.ProtocolSFTP, "", "", *user),
|
|
ClientVersion: "",
|
|
RemoteAddr: &net.IPAddr{},
|
|
LocalAddr: &net.IPAddr{},
|
|
channel: newSubsystemChannel(reader, writer),
|
|
}
|
|
err = common.Connections.Add(connection)
|
|
if err != nil {
|
|
errClose := user.CloseFs()
|
|
logger.Warn(logSender, connectionID, "unable to add connection: %v close fs error: %v", err, errClose)
|
|
return err
|
|
}
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
dataprovider.UpdateLastLogin(user)
|
|
server := sftp.NewRequestServer(connection.channel, sftp.Handlers{
|
|
FileGet: connection,
|
|
FilePut: connection,
|
|
FileCmd: connection,
|
|
FileList: connection,
|
|
}, sftp.WithRSAllocator())
|
|
|
|
defer server.Close()
|
|
return server.Serve()
|
|
}
|