2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-10-29 18:23:33 +00:00
|
|
|
package sftpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/pkg/sftp"
|
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
2020-10-29 18:23:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2021-02-16 18:11:36 +00:00
|
|
|
func ServeSubSystemConnection(user *dataprovider.User, connectionID string, reader io.Reader, writer io.Writer) error {
|
2021-03-21 18:15:47 +00:00
|
|
|
err := user.CheckFsRoot(connectionID)
|
2020-10-29 18:23:33 +00:00
|
|
|
if err != nil {
|
2021-03-21 18:15:47 +00:00
|
|
|
errClose := user.CloseFs()
|
|
|
|
logger.Warn(logSender, connectionID, "unable to check fs root: %v close fs error: %v", err, errClose)
|
2020-10-29 18:23:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
connection := &Connection{
|
2021-07-24 18:11:17 +00:00
|
|
|
BaseConnection: common.NewBaseConnection(connectionID, common.ProtocolSFTP, "", "", *user),
|
2020-10-29 18:23:33 +00:00
|
|
|
ClientVersion: "",
|
|
|
|
RemoteAddr: &net.IPAddr{},
|
2021-07-24 18:11:17 +00:00
|
|
|
LocalAddr: &net.IPAddr{},
|
2020-10-29 18:23:33 +00:00
|
|
|
channel: newSubsystemChannel(reader, writer),
|
|
|
|
}
|
2022-04-14 17:07:41 +00:00
|
|
|
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
|
|
|
|
}
|
2020-10-29 18:23:33 +00:00
|
|
|
defer common.Connections.Remove(connection.GetID())
|
|
|
|
|
2022-04-14 17:07:41 +00:00
|
|
|
dataprovider.UpdateLastLogin(user)
|
2022-06-25 16:05:13 +00:00
|
|
|
sftp.SetSFTPExtensions(sftpExtensions...) //nolint:errcheck
|
2020-10-29 18:23:33 +00:00
|
|
|
server := sftp.NewRequestServer(connection.channel, sftp.Handlers{
|
|
|
|
FileGet: connection,
|
|
|
|
FilePut: connection,
|
|
|
|
FileCmd: connection,
|
|
|
|
FileList: connection,
|
|
|
|
}, sftp.WithRSAllocator())
|
|
|
|
|
2020-11-18 18:06:12 +00:00
|
|
|
defer server.Close()
|
2020-10-29 18:23:33 +00:00
|
|
|
return server.Serve()
|
|
|
|
}
|