2019-07-20 10:26:52 +00:00
|
|
|
package sftpd
|
|
|
|
|
|
|
|
import (
|
2020-05-15 18:08:53 +00:00
|
|
|
"bytes"
|
2019-07-20 10:26:52 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2020-04-01 21:25:23 +00:00
|
|
|
"strings"
|
2019-07-20 10:26:52 +00:00
|
|
|
"time"
|
|
|
|
|
2020-05-06 17:36:34 +00:00
|
|
|
"github.com/pires/go-proxyproto"
|
|
|
|
"github.com/pkg/sftp"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2019-09-13 16:45:36 +00:00
|
|
|
"github.com/drakkan/sftpgo/metrics"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
|
|
|
)
|
|
|
|
|
2020-02-16 17:17:39 +00:00
|
|
|
const (
|
2020-05-15 18:08:53 +00:00
|
|
|
defaultPrivateRSAKeyName = "id_rsa"
|
|
|
|
defaultPrivateECDSAKeyName = "id_ecdsa"
|
|
|
|
sourceAddressCriticalOption = "source-address"
|
2020-02-16 17:17:39 +00:00
|
|
|
)
|
2019-08-01 07:42:15 +00:00
|
|
|
|
2020-02-28 23:02:06 +00:00
|
|
|
var (
|
2020-04-30 12:23:55 +00:00
|
|
|
sftpExtensions = []string{"posix-rename@openssh.com"}
|
2020-02-28 23:02:06 +00:00
|
|
|
)
|
2019-11-12 06:37:47 +00:00
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Configuration for the SFTP server
|
2019-07-20 10:26:52 +00:00
|
|
|
type Configuration struct {
|
2019-07-30 18:51:29 +00:00
|
|
|
// Identification string used by the server
|
2019-08-07 20:46:13 +00:00
|
|
|
Banner string `json:"banner" mapstructure:"banner"`
|
2019-07-30 18:51:29 +00:00
|
|
|
// The port used for serving SFTP requests
|
2019-08-07 20:46:13 +00:00
|
|
|
BindPort int `json:"bind_port" mapstructure:"bind_port"`
|
2019-07-30 18:51:29 +00:00
|
|
|
// The address to listen on. A blank value means listen on all available network interfaces.
|
2019-08-07 20:46:13 +00:00
|
|
|
BindAddress string `json:"bind_address" mapstructure:"bind_address"`
|
2020-03-03 22:25:23 +00:00
|
|
|
// Maximum idle timeout as minutes. If a client is idle for a time that exceeds this setting it will be disconnected.
|
|
|
|
// 0 means disabled
|
2019-08-07 20:46:13 +00:00
|
|
|
IdleTimeout int `json:"idle_timeout" mapstructure:"idle_timeout"`
|
2019-07-30 18:51:29 +00:00
|
|
|
// Maximum number of authentication attempts permitted per connection.
|
2020-03-03 22:25:23 +00:00
|
|
|
// If set to a negative number, the number of attempts is unlimited.
|
2019-07-30 18:51:29 +00:00
|
|
|
// If set to zero, the number of attempts are limited to 6.
|
2019-08-07 20:46:13 +00:00
|
|
|
MaxAuthTries int `json:"max_auth_tries" mapstructure:"max_auth_tries"`
|
2019-07-30 18:51:29 +00:00
|
|
|
// Umask for new files
|
2019-08-07 20:46:13 +00:00
|
|
|
Umask string `json:"umask" mapstructure:"umask"`
|
2019-08-04 07:37:58 +00:00
|
|
|
// UploadMode 0 means standard, the files are uploaded directly to the requested path.
|
|
|
|
// 1 means atomic: the files are uploaded to a temporary path and renamed to the requested path
|
|
|
|
// when the client ends the upload. Atomic mode avoid problems such as a web server that
|
|
|
|
// serves partial files when the files are being uploaded.
|
2019-10-09 15:33:30 +00:00
|
|
|
// In atomic mode if there is an upload error the temporary file is deleted and so the requested
|
|
|
|
// upload path will not contain a partial file.
|
|
|
|
// 2 means atomic with resume support: as atomic but if there is an upload error the temporary
|
|
|
|
// file is renamed to the requested path and not deleted, this way a client can reconnect and resume
|
|
|
|
// the upload.
|
2019-08-07 20:46:13 +00:00
|
|
|
UploadMode int `json:"upload_mode" mapstructure:"upload_mode"`
|
2019-07-30 18:51:29 +00:00
|
|
|
// Actions to execute on SFTP create, download, delete and rename
|
2019-08-07 20:46:13 +00:00
|
|
|
Actions Actions `json:"actions" mapstructure:"actions"`
|
2019-08-01 07:42:15 +00:00
|
|
|
// Keys are a list of host keys
|
2019-08-07 20:46:13 +00:00
|
|
|
Keys []Key `json:"keys" mapstructure:"keys"`
|
2019-09-03 10:08:09 +00:00
|
|
|
// KexAlgorithms specifies the available KEX (Key Exchange) algorithms in
|
|
|
|
// preference order.
|
|
|
|
KexAlgorithms []string `json:"kex_algorithms" mapstructure:"kex_algorithms"`
|
|
|
|
// Ciphers specifies the ciphers allowed
|
|
|
|
Ciphers []string `json:"ciphers" mapstructure:"ciphers"`
|
|
|
|
// MACs Specifies the available MAC (message authentication code) algorithms
|
|
|
|
// in preference order
|
|
|
|
MACs []string `json:"macs" mapstructure:"macs"`
|
2020-05-15 18:08:53 +00:00
|
|
|
// TrustedUserCAKeys specifies a list of public keys paths of certificate authorities
|
|
|
|
// that are trusted to sign user certificates for authentication.
|
|
|
|
// The paths can be absolute or relative to the configuration directory
|
|
|
|
TrustedUserCAKeys []string `json:"trusted_user_ca_keys" mapstructure:"trusted_user_ca_keys"`
|
2019-09-03 10:08:09 +00:00
|
|
|
// LoginBannerFile the contents of the specified file, if any, are sent to
|
|
|
|
// the remote user before authentication is allowed.
|
|
|
|
LoginBannerFile string `json:"login_banner_file" mapstructure:"login_banner_file"`
|
2019-11-15 11:15:07 +00:00
|
|
|
// SetstatMode 0 means "normal mode": requests for changing permissions and owner/group are executed.
|
|
|
|
// 1 means "ignore mode": requests for changing permissions and owner/group are silently ignored.
|
|
|
|
SetstatMode int `json:"setstat_mode" mapstructure:"setstat_mode"`
|
2019-11-18 22:30:37 +00:00
|
|
|
// List of enabled SSH commands.
|
|
|
|
// We support the following SSH commands:
|
|
|
|
// - "scp". SCP is an experimental feature, we have our own SCP implementation since
|
|
|
|
// we can't rely on scp system command to proper handle permissions, quota and
|
|
|
|
// user's home dir restrictions.
|
|
|
|
// The SCP protocol is quite simple but there is no official docs about it,
|
|
|
|
// so we need more testing and feedbacks before enabling it by default.
|
|
|
|
// We may not handle some borderline cases or have sneaky bugs.
|
|
|
|
// Please do accurate tests yourself before enabling SCP and let us known
|
|
|
|
// if something does not work as expected for your use cases.
|
|
|
|
// SCP between two remote hosts is supported using the `-3` scp option.
|
|
|
|
// - "md5sum", "sha1sum", "sha256sum", "sha384sum", "sha512sum". Useful to check message
|
|
|
|
// digests for uploaded files. These commands are implemented inside SFTPGo so they
|
|
|
|
// work even if the matching system commands are not available, for example on Windows.
|
|
|
|
// - "cd", "pwd". Some mobile SFTP clients does not support the SFTP SSH_FXP_REALPATH and so
|
|
|
|
// they use "cd" and "pwd" SSH commands to get the initial directory.
|
|
|
|
// Currently `cd` do nothing and `pwd` always returns the "/" path.
|
|
|
|
//
|
|
|
|
// The following SSH commands are enabled by default: "md5sum", "sha1sum", "cd", "pwd".
|
|
|
|
// "*" enables all supported SSH commands.
|
|
|
|
EnabledSSHCommands []string `json:"enabled_ssh_commands" mapstructure:"enabled_ssh_commands"`
|
2020-04-01 21:25:23 +00:00
|
|
|
// Deprecated: please use KeyboardInteractiveHook
|
2020-01-21 09:54:05 +00:00
|
|
|
KeyboardInteractiveProgram string `json:"keyboard_interactive_auth_program" mapstructure:"keyboard_interactive_auth_program"`
|
2020-04-01 21:25:23 +00:00
|
|
|
// Absolute path to an external program or an HTTP URL to invoke for keyboard interactive authentication.
|
|
|
|
// Leave empty to disable this authentication mode.
|
|
|
|
KeyboardInteractiveHook string `json:"keyboard_interactive_auth_hook" mapstructure:"keyboard_interactive_auth_hook"`
|
2020-02-27 08:21:30 +00:00
|
|
|
// Support for HAProxy PROXY protocol.
|
|
|
|
// If you are running SFTPGo behind a proxy server such as HAProxy, AWS ELB or NGNIX, you can enable
|
|
|
|
// the proxy protocol. It provides a convenient way to safely transport connection information
|
|
|
|
// such as a client's address across multiple layers of NAT or TCP proxies to get the real
|
2020-03-04 22:51:16 +00:00
|
|
|
// client IP address instead of the proxy IP. Both protocol versions 1 and 2 are supported.
|
2020-02-28 23:02:06 +00:00
|
|
|
// - 0 means disabled
|
|
|
|
// - 1 means proxy protocol enabled. Proxy header will be used and requests without proxy header will be accepted.
|
|
|
|
// - 2 means proxy protocol required. Proxy header will be used and requests without proxy header will be rejected.
|
|
|
|
// If the proxy protocol is enabled in SFTPGo then you have to enable the protocol in your proxy configuration too,
|
|
|
|
// for example for HAProxy add "send-proxy" or "send-proxy-v2" to each server configuration line.
|
2020-02-27 08:21:30 +00:00
|
|
|
ProxyProtocol int `json:"proxy_protocol" mapstructure:"proxy_protocol"`
|
2020-03-01 22:12:28 +00:00
|
|
|
// List of IP addresses and IP ranges allowed to send the proxy header.
|
|
|
|
// If proxy protocol is set to 1 and we receive a proxy header from an IP that is not in the list then the
|
|
|
|
// connection will be accepted and the header will be ignored.
|
|
|
|
// If proxy protocol is set to 2 and we receive a proxy header from an IP that is not in the list then the
|
|
|
|
// connection will be rejected.
|
2020-05-15 18:08:53 +00:00
|
|
|
ProxyAllowed []string `json:"proxy_allowed" mapstructure:"proxy_allowed"`
|
|
|
|
certChecker *ssh.CertChecker
|
|
|
|
parsedUserCAKeys []ssh.PublicKey
|
2019-08-01 07:42:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 13:39:30 +00:00
|
|
|
// Key contains information about host keys
|
2019-08-01 07:42:15 +00:00
|
|
|
type Key struct {
|
2020-05-15 18:08:53 +00:00
|
|
|
// The private key path as absolute path or relative to the configuration directory
|
2019-08-07 20:46:13 +00:00
|
|
|
PrivateKey string `json:"private_key" mapstructure:"private_key"`
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 14:20:00 +00:00
|
|
|
type authenticationError struct {
|
|
|
|
err string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *authenticationError) Error() string {
|
|
|
|
return fmt.Sprintf("Authentication error: %s", e.err)
|
|
|
|
}
|
|
|
|
|
2019-07-20 19:17:53 +00:00
|
|
|
// Initialize the SFTP server and add a persistent listener to handle inbound SFTP connections.
|
|
|
|
func (c Configuration) Initialize(configDir string) error {
|
2019-07-20 10:26:52 +00:00
|
|
|
umask, err := strconv.ParseUint(c.Umask, 8, 8)
|
|
|
|
if err == nil {
|
|
|
|
utils.SetUmask(int(umask), c.Umask)
|
|
|
|
} else {
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Warn(logSender, "", "error reading umask, please fix your config file: %v", err)
|
2019-07-31 06:14:31 +00:00
|
|
|
logger.WarnToConsole("error reading umask, please fix your config file: %v", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
serverConfig := &ssh.ServerConfig{
|
|
|
|
NoClientAuth: false,
|
2019-07-21 10:02:24 +00:00
|
|
|
MaxAuthTries: c.MaxAuthTries,
|
2019-07-20 10:26:52 +00:00
|
|
|
PasswordCallback: func(conn ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
|
|
|
|
sp, err := c.validatePasswordCredentials(conn, pass)
|
|
|
|
if err != nil {
|
2019-11-11 14:20:00 +00:00
|
|
|
return nil, &authenticationError{err: fmt.Sprintf("could not validate password credentials: %v", err)}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sp, nil
|
|
|
|
},
|
|
|
|
PublicKeyCallback: func(conn ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
|
2020-05-15 18:08:53 +00:00
|
|
|
sp, err := c.validatePublicKeyCredentials(conn, pubKey)
|
2020-04-09 21:32:42 +00:00
|
|
|
if err == ssh.ErrPartialSuccess {
|
2020-05-16 13:15:32 +00:00
|
|
|
return sp, err
|
2020-04-09 21:32:42 +00:00
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
if err != nil {
|
2019-11-11 14:20:00 +00:00
|
|
|
return nil, &authenticationError{err: fmt.Sprintf("could not validate public key credentials: %v", err)}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sp, nil
|
|
|
|
},
|
2020-04-09 21:32:42 +00:00
|
|
|
NextAuthMethodsCallback: func(conn ssh.ConnMetadata) []string {
|
|
|
|
var nextMethods []string
|
|
|
|
user, err := dataprovider.UserExists(dataProvider, conn.User())
|
|
|
|
if err == nil {
|
|
|
|
nextMethods = user.GetNextAuthMethods(conn.PartialSuccessMethods())
|
|
|
|
}
|
|
|
|
return nextMethods
|
|
|
|
},
|
|
|
|
ServerVersion: fmt.Sprintf("SSH-2.0-%v", c.Banner),
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 18:08:53 +00:00
|
|
|
if err = c.checkAndLoadHostKeys(configDir, serverConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.initializeCertChecker(configDir); err != nil {
|
2019-08-02 09:17:23 +00:00
|
|
|
return err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 12:23:55 +00:00
|
|
|
sftp.SetSFTPExtensions(sftpExtensions...) //nolint:errcheck // we configure valid SFTP Extensions so we cannot get an error
|
|
|
|
|
2019-09-03 10:08:09 +00:00
|
|
|
c.configureSecurityOptions(serverConfig)
|
2020-01-21 09:54:05 +00:00
|
|
|
c.configureKeyboardInteractiveAuth(serverConfig)
|
2019-09-03 10:08:09 +00:00
|
|
|
c.configureLoginBanner(serverConfig, configDir)
|
2019-11-18 22:30:37 +00:00
|
|
|
c.checkSSHCommands()
|
2019-09-03 10:08:09 +00:00
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", c.BindAddress, c.BindPort))
|
|
|
|
if err != nil {
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Warn(logSender, "", "error starting listener on address %s:%d: %v", c.BindAddress, c.BindPort, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-03-01 22:12:28 +00:00
|
|
|
proxyListener, err := c.getProxyListener(listener)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "error enabling proxy listener: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2019-07-31 12:11:44 +00:00
|
|
|
actions = c.Actions
|
2019-08-04 07:37:58 +00:00
|
|
|
uploadMode = c.UploadMode
|
2019-11-15 11:15:07 +00:00
|
|
|
setstatMode = c.SetstatMode
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Info(logSender, "", "server listener registered address: %v", listener.Addr().String())
|
2020-02-27 08:21:30 +00:00
|
|
|
c.checkIdleTimer()
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
for {
|
2020-02-27 08:21:30 +00:00
|
|
|
var conn net.Conn
|
|
|
|
if proxyListener != nil {
|
|
|
|
conn, err = proxyListener.Accept()
|
|
|
|
} else {
|
|
|
|
conn, err = listener.Accept()
|
|
|
|
}
|
|
|
|
if conn != nil && err == nil {
|
2019-07-20 10:26:52 +00:00
|
|
|
go c.AcceptInboundConnection(conn, serverConfig)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-01 22:12:28 +00:00
|
|
|
func (c *Configuration) getProxyListener(listener net.Listener) (*proxyproto.Listener, error) {
|
2020-02-28 23:02:06 +00:00
|
|
|
var proxyListener *proxyproto.Listener
|
2020-03-01 22:12:28 +00:00
|
|
|
var err error
|
2020-02-28 23:02:06 +00:00
|
|
|
if c.ProxyProtocol > 0 {
|
|
|
|
var policyFunc func(upstream net.Addr) (proxyproto.Policy, error)
|
2020-03-01 22:12:28 +00:00
|
|
|
if c.ProxyProtocol == 1 && len(c.ProxyAllowed) > 0 {
|
|
|
|
policyFunc, err = proxyproto.LaxWhiteListPolicy(c.ProxyAllowed)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 23:02:06 +00:00
|
|
|
if c.ProxyProtocol == 2 {
|
2020-03-01 22:12:28 +00:00
|
|
|
if len(c.ProxyAllowed) == 0 {
|
|
|
|
policyFunc = func(upstream net.Addr) (proxyproto.Policy, error) {
|
|
|
|
return proxyproto.REQUIRE, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
policyFunc, err = proxyproto.StrictWhiteListPolicy(c.ProxyAllowed)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-28 23:02:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
proxyListener = &proxyproto.Listener{
|
|
|
|
Listener: listener,
|
|
|
|
Policy: policyFunc,
|
|
|
|
}
|
|
|
|
}
|
2020-03-01 22:12:28 +00:00
|
|
|
return proxyListener, nil
|
2020-02-28 23:02:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 08:21:30 +00:00
|
|
|
func (c Configuration) checkIdleTimer() {
|
|
|
|
if c.IdleTimeout > 0 {
|
|
|
|
startIdleTimer(time.Duration(c.IdleTimeout) * time.Minute)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 10:08:09 +00:00
|
|
|
func (c Configuration) configureSecurityOptions(serverConfig *ssh.ServerConfig) {
|
|
|
|
if len(c.KexAlgorithms) > 0 {
|
|
|
|
serverConfig.KeyExchanges = c.KexAlgorithms
|
|
|
|
}
|
|
|
|
if len(c.Ciphers) > 0 {
|
|
|
|
serverConfig.Ciphers = c.Ciphers
|
|
|
|
}
|
|
|
|
if len(c.MACs) > 0 {
|
|
|
|
serverConfig.MACs = c.MACs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:23:55 +00:00
|
|
|
func (c Configuration) configureLoginBanner(serverConfig *ssh.ServerConfig, configDir string) {
|
2019-09-03 10:08:09 +00:00
|
|
|
if len(c.LoginBannerFile) > 0 {
|
|
|
|
bannerFilePath := c.LoginBannerFile
|
|
|
|
if !filepath.IsAbs(bannerFilePath) {
|
|
|
|
bannerFilePath = filepath.Join(configDir, bannerFilePath)
|
|
|
|
}
|
2020-04-30 12:23:55 +00:00
|
|
|
bannerContent, err := ioutil.ReadFile(bannerFilePath)
|
2019-09-03 10:08:09 +00:00
|
|
|
if err == nil {
|
2020-04-09 21:32:42 +00:00
|
|
|
banner := string(bannerContent)
|
2019-09-03 10:08:09 +00:00
|
|
|
serverConfig.BannerCallback = func(conn ssh.ConnMetadata) string {
|
2020-04-30 12:23:55 +00:00
|
|
|
return banner
|
2019-09-03 10:08:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.WarnToConsole("unable to read login banner file: %v", err)
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Warn(logSender, "", "unable to read login banner file: %v", err)
|
2019-09-03 10:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 09:54:05 +00:00
|
|
|
func (c Configuration) configureKeyboardInteractiveAuth(serverConfig *ssh.ServerConfig) {
|
2020-04-01 21:25:23 +00:00
|
|
|
if len(c.KeyboardInteractiveHook) == 0 {
|
2020-01-21 09:54:05 +00:00
|
|
|
return
|
|
|
|
}
|
2020-04-01 21:25:23 +00:00
|
|
|
if !strings.HasPrefix(c.KeyboardInteractiveHook, "http") {
|
|
|
|
if !filepath.IsAbs(c.KeyboardInteractiveHook) {
|
|
|
|
logger.WarnToConsole("invalid keyboard interactive authentication program: %#v must be an absolute path",
|
|
|
|
c.KeyboardInteractiveHook)
|
|
|
|
logger.Warn(logSender, "", "invalid keyboard interactive authentication program: %#v must be an absolute path",
|
|
|
|
c.KeyboardInteractiveHook)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err := os.Stat(c.KeyboardInteractiveHook)
|
|
|
|
if err != nil {
|
|
|
|
logger.WarnToConsole("invalid keyboard interactive authentication program:: %v", err)
|
|
|
|
logger.Warn(logSender, "", "invalid keyboard interactive authentication program:: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2020-01-21 09:54:05 +00:00
|
|
|
}
|
|
|
|
serverConfig.KeyboardInteractiveCallback = func(conn ssh.ConnMetadata, client ssh.KeyboardInteractiveChallenge) (*ssh.Permissions, error) {
|
|
|
|
sp, err := c.validateKeyboardInteractiveCredentials(conn, client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &authenticationError{err: fmt.Sprintf("could not validate keyboard interactive credentials: %v", err)}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// AcceptInboundConnection handles an inbound connection to the server instance and determines if the request should be served or not.
|
2019-07-20 10:26:52 +00:00
|
|
|
func (c Configuration) AcceptInboundConnection(conn net.Conn, config *ssh.ServerConfig) {
|
|
|
|
// Before beginning a handshake must be performed on the incoming net.Conn
|
2019-10-09 17:07:35 +00:00
|
|
|
// we'll set a Deadline for handshake to complete, the default is 2 minutes as OpenSSH
|
2020-04-30 12:23:55 +00:00
|
|
|
conn.SetDeadline(time.Now().Add(handshakeTimeout)) //nolint:errcheck
|
2019-11-11 14:20:00 +00:00
|
|
|
remoteAddr := conn.RemoteAddr()
|
2019-07-20 10:26:52 +00:00
|
|
|
sconn, chans, reqs, err := ssh.NewServerConn(conn, config)
|
|
|
|
if err != nil {
|
2019-09-05 14:21:35 +00:00
|
|
|
logger.Warn(logSender, "", "failed to accept an incoming connection: %v", err)
|
2019-11-11 14:20:00 +00:00
|
|
|
if _, ok := err.(*ssh.ServerAuthError); !ok {
|
|
|
|
logger.ConnectionFailedLog("", utils.GetIPFromRemoteAddress(remoteAddr.String()), "no_auth_tryed", err.Error())
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
return
|
|
|
|
}
|
2019-10-09 17:07:35 +00:00
|
|
|
// handshake completed so remove the deadline, we'll use IdleTimeout configuration from now on
|
2020-04-30 12:23:55 +00:00
|
|
|
conn.SetDeadline(time.Time{}) //nolint:errcheck
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
var user dataprovider.User
|
|
|
|
|
2019-10-09 17:07:35 +00:00
|
|
|
// Unmarshal cannot fails here and even if it fails we'll have a user with no permissions
|
2020-05-15 18:08:53 +00:00
|
|
|
json.Unmarshal([]byte(sconn.Permissions.Extensions["sftpgo_user"]), &user) //nolint:errcheck
|
2019-08-24 12:41:15 +00:00
|
|
|
|
2020-05-15 18:08:53 +00:00
|
|
|
loginType := sconn.Permissions.Extensions["sftpgo_login_method"]
|
2019-08-24 12:41:15 +00:00
|
|
|
connectionID := hex.EncodeToString(sconn.SessionID())
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
fs, err := user.GetFilesystem(connectionID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "could create filesystem for user %#v err: %v", user.Username, err)
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
connection := Connection{
|
|
|
|
ID: connectionID,
|
|
|
|
User: user,
|
|
|
|
ClientVersion: string(sconn.ClientVersion()),
|
2019-11-11 14:20:00 +00:00
|
|
|
RemoteAddr: remoteAddr,
|
2019-08-24 12:41:15 +00:00
|
|
|
StartTime: time.Now(),
|
|
|
|
lastActivity: time.Now(),
|
2019-09-11 07:41:46 +00:00
|
|
|
netConn: conn,
|
2019-09-11 14:29:56 +00:00
|
|
|
channel: nil,
|
2020-01-19 06:41:05 +00:00
|
|
|
fs: fs,
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
2020-01-19 06:41:05 +00:00
|
|
|
|
2020-01-19 12:58:55 +00:00
|
|
|
connection.fs.CheckRootPath(user.Username, user.GetUID(), user.GetGID())
|
2020-01-19 06:41:05 +00:00
|
|
|
|
2019-11-11 14:20:00 +00:00
|
|
|
connection.Log(logger.LevelInfo, logSender, "User id: %d, logged in with: %#v, username: %#v, home_dir: %#v remote addr: %#v",
|
|
|
|
user.ID, loginType, user.Username, user.HomeDir, remoteAddr.String())
|
2020-04-30 12:23:55 +00:00
|
|
|
dataprovider.UpdateLastLogin(dataProvider, user) //nolint:errcheck
|
2019-08-24 12:41:15 +00:00
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
go ssh.DiscardRequests(reqs)
|
|
|
|
|
|
|
|
for newChannel := range chans {
|
|
|
|
// If its not a session channel we just move on because its not something we
|
|
|
|
// know how to handle at this point.
|
|
|
|
if newChannel.ChannelType() != "session" {
|
2019-09-06 13:19:01 +00:00
|
|
|
connection.Log(logger.LevelDebug, logSender, "received an unknown channel type: %v", newChannel.ChannelType())
|
2020-04-30 12:23:55 +00:00
|
|
|
newChannel.Reject(ssh.UnknownChannelType, "unknown channel type") //nolint:errcheck
|
2019-07-20 10:26:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
channel, requests, err := newChannel.Accept()
|
|
|
|
if err != nil {
|
2019-09-06 13:19:01 +00:00
|
|
|
connection.Log(logger.LevelWarn, logSender, "could not accept a channel: %v", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Channels have a type that is dependent on the protocol. For SFTP this is "subsystem"
|
|
|
|
// with a payload that (should) be "sftp". Discard anything else we receive ("pty", "shell", etc)
|
|
|
|
go func(in <-chan *ssh.Request) {
|
|
|
|
for req := range in {
|
|
|
|
ok := false
|
|
|
|
|
|
|
|
switch req.Type {
|
|
|
|
case "subsystem":
|
|
|
|
if string(req.Payload[4:]) == "sftp" {
|
|
|
|
ok = true
|
2019-08-24 12:41:15 +00:00
|
|
|
connection.protocol = protocolSFTP
|
2019-09-11 14:29:56 +00:00
|
|
|
connection.channel = channel
|
2019-08-24 12:41:15 +00:00
|
|
|
go c.handleSftpConnection(channel, connection)
|
|
|
|
}
|
|
|
|
case "exec":
|
2019-11-18 22:30:37 +00:00
|
|
|
ok = processSSHCommand(req.Payload, &connection, channel, c.EnabledSSHCommands)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2020-04-30 12:23:55 +00:00
|
|
|
req.Reply(ok, nil) //nolint:errcheck
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}(requests)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-11 14:29:56 +00:00
|
|
|
func (c Configuration) handleSftpConnection(channel ssh.Channel, connection Connection) {
|
2019-10-10 07:04:17 +00:00
|
|
|
addConnection(connection)
|
|
|
|
defer removeConnection(connection)
|
2019-08-24 12:41:15 +00:00
|
|
|
// Create a new handler for the currently logged in user's server.
|
|
|
|
handler := c.createHandler(connection)
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2019-08-24 12:41:15 +00:00
|
|
|
// Create the server instance for the channel using the handler we created above.
|
2020-04-10 21:35:57 +00:00
|
|
|
server := sftp.NewRequestServer(channel, handler, sftp.WithRSAllocator())
|
2019-08-24 12:41:15 +00:00
|
|
|
|
|
|
|
if err := server.Serve(); err == io.EOF {
|
2019-11-15 11:15:07 +00:00
|
|
|
connection.Log(logger.LevelDebug, logSender, "connection closed, sending exit status")
|
|
|
|
exitStatus := sshSubsystemExitStatus{Status: uint32(0)}
|
|
|
|
_, err = channel.SendRequest("exit-status", false, ssh.Marshal(&exitStatus))
|
|
|
|
connection.Log(logger.LevelDebug, logSender, "sent exit status %+v error: %v", exitStatus, err)
|
2019-08-24 12:41:15 +00:00
|
|
|
server.Close()
|
|
|
|
} else if err != nil {
|
2019-09-07 16:21:03 +00:00
|
|
|
connection.Log(logger.LevelWarn, logSender, "connection closed with error: %v", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2019-08-24 12:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Configuration) createHandler(connection Connection) sftp.Handlers {
|
2019-07-20 10:26:52 +00:00
|
|
|
return sftp.Handlers{
|
|
|
|
FileGet: connection,
|
|
|
|
FilePut: connection,
|
|
|
|
FileCmd: connection,
|
|
|
|
FileList: connection,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 21:32:42 +00:00
|
|
|
func loginUser(user dataprovider.User, loginMethod, publicKey string, conn ssh.ConnMetadata) (*ssh.Permissions, error) {
|
|
|
|
connectionID := ""
|
|
|
|
if conn != nil {
|
|
|
|
connectionID = hex.EncodeToString(conn.SessionID())
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
if !filepath.IsAbs(user.HomeDir) {
|
2020-04-09 21:32:42 +00:00
|
|
|
logger.Warn(logSender, connectionID, "user %#v has an invalid home dir: %#v. Home dir must be an absolute path, login not allowed",
|
2019-07-20 10:26:52 +00:00
|
|
|
user.Username, user.HomeDir)
|
2019-11-15 11:15:07 +00:00
|
|
|
return nil, fmt.Errorf("cannot login user with invalid home dir: %#v", user.HomeDir)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
if user.MaxSessions > 0 {
|
|
|
|
activeSessions := getActiveSessions(user.Username)
|
|
|
|
if activeSessions >= user.MaxSessions {
|
2019-11-15 11:15:07 +00:00
|
|
|
logger.Debug(logSender, "", "authentication refused for user: %#v, too many open sessions: %v/%v", user.Username,
|
2019-07-20 10:26:52 +00:00
|
|
|
activeSessions, user.MaxSessions)
|
2019-10-24 16:50:35 +00:00
|
|
|
return nil, fmt.Errorf("too many open sessions: %v", activeSessions)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-09 21:32:42 +00:00
|
|
|
if !user.IsLoginMethodAllowed(loginMethod, conn.PartialSuccessMethods()) {
|
|
|
|
logger.Debug(logSender, connectionID, "cannot login user %#v, login method %#v is not allowed", user.Username, loginMethod)
|
2020-02-19 21:39:30 +00:00
|
|
|
return nil, fmt.Errorf("Login method %#v is not allowed for user %#v", loginMethod, user.Username)
|
|
|
|
}
|
2020-04-09 21:32:42 +00:00
|
|
|
remoteAddr := conn.RemoteAddr().String()
|
2020-02-19 21:39:30 +00:00
|
|
|
if !user.IsLoginFromAddrAllowed(remoteAddr) {
|
2020-04-09 21:32:42 +00:00
|
|
|
logger.Debug(logSender, connectionID, "cannot login user %#v, remote address is not allowed: %v", user.Username, remoteAddr)
|
2020-02-19 21:39:30 +00:00
|
|
|
return nil, fmt.Errorf("Login for user %#v is not allowed from this address: %v", user.Username, remoteAddr)
|
2019-12-30 17:37:50 +00:00
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
|
|
|
|
json, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
2020-04-09 21:32:42 +00:00
|
|
|
logger.Warn(logSender, connectionID, "error serializing user info: %v, authentication rejected", err)
|
2019-07-20 10:26:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-19 21:39:30 +00:00
|
|
|
if len(publicKey) > 0 {
|
|
|
|
loginMethod = fmt.Sprintf("%v: %v", loginMethod, publicKey)
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
p := &ssh.Permissions{}
|
|
|
|
p.Extensions = make(map[string]string)
|
2020-05-15 18:08:53 +00:00
|
|
|
p.Extensions["sftpgo_user"] = string(json)
|
|
|
|
p.Extensions["sftpgo_login_method"] = loginMethod
|
2019-07-20 10:26:52 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2019-11-18 22:30:37 +00:00
|
|
|
func (c *Configuration) checkSSHCommands() {
|
|
|
|
if utils.IsStringInSlice("*", c.EnabledSSHCommands) {
|
|
|
|
c.EnabledSSHCommands = GetSupportedSSHCommands()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sshCommands := []string{}
|
|
|
|
for _, command := range c.EnabledSSHCommands {
|
|
|
|
if utils.IsStringInSlice(command, supportedSSHCommands) {
|
|
|
|
sshCommands = append(sshCommands, command)
|
|
|
|
} else {
|
|
|
|
logger.Warn(logSender, "", "unsupported ssh command: %#v ignored", command)
|
|
|
|
logger.WarnToConsole("unsupported ssh command: %#v ignored", command)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.EnabledSSHCommands = sshCommands
|
|
|
|
}
|
|
|
|
|
2020-04-09 21:32:42 +00:00
|
|
|
// If no host keys are defined we try to use or generate the default ones.
|
|
|
|
func (c *Configuration) checkAndLoadHostKeys(configDir string, serverConfig *ssh.ServerConfig) error {
|
2019-08-02 09:17:23 +00:00
|
|
|
if len(c.Keys) == 0 {
|
2020-02-16 17:17:39 +00:00
|
|
|
defaultKeys := []string{defaultPrivateRSAKeyName, defaultPrivateECDSAKeyName}
|
|
|
|
for _, k := range defaultKeys {
|
|
|
|
autoFile := filepath.Join(configDir, k)
|
|
|
|
if _, err := os.Stat(autoFile); os.IsNotExist(err) {
|
|
|
|
logger.Info(logSender, "", "No host keys configured and %#v does not exist; creating new key for server", autoFile)
|
|
|
|
logger.InfoToConsole("No host keys configured and %#v does not exist; creating new key for server", autoFile)
|
|
|
|
if k == defaultPrivateRSAKeyName {
|
|
|
|
err = utils.GenerateRSAKeys(autoFile)
|
|
|
|
} else {
|
|
|
|
err = utils.GenerateECDSAKeys(autoFile)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.Keys = append(c.Keys, Key{PrivateKey: k})
|
2019-08-02 09:17:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-09 21:32:42 +00:00
|
|
|
for _, k := range c.Keys {
|
|
|
|
privateFile := k.PrivateKey
|
2020-05-16 13:15:32 +00:00
|
|
|
if !utils.IsFileInputValid(privateFile) {
|
|
|
|
logger.Warn(logSender, "", "unable to load invalid host key: %#v", privateFile)
|
|
|
|
logger.WarnToConsole("unable to load invalid host key: %#v", privateFile)
|
|
|
|
continue
|
|
|
|
}
|
2020-04-09 21:32:42 +00:00
|
|
|
if !filepath.IsAbs(privateFile) {
|
|
|
|
privateFile = filepath.Join(configDir, privateFile)
|
|
|
|
}
|
|
|
|
logger.Info(logSender, "", "Loading private key: %s", privateFile)
|
|
|
|
|
|
|
|
privateBytes, err := ioutil.ReadFile(privateFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
private, err := ssh.ParsePrivateKey(privateBytes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add private key to the server configuration.
|
|
|
|
serverConfig.AddHostKey(private)
|
|
|
|
}
|
2020-02-16 17:17:39 +00:00
|
|
|
return nil
|
2019-08-02 09:17:23 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 18:08:53 +00:00
|
|
|
func (c *Configuration) initializeCertChecker(configDir string) error {
|
|
|
|
for _, keyPath := range c.TrustedUserCAKeys {
|
2020-05-16 13:15:32 +00:00
|
|
|
if !utils.IsFileInputValid(keyPath) {
|
|
|
|
logger.Warn(logSender, "", "unable to load invalid trusted user CA key: %#v", keyPath)
|
|
|
|
logger.WarnToConsole("unable to load invalid trusted user CA key: %#v", keyPath)
|
|
|
|
continue
|
|
|
|
}
|
2020-05-15 18:08:53 +00:00
|
|
|
if !filepath.IsAbs(keyPath) {
|
|
|
|
keyPath = filepath.Join(configDir, keyPath)
|
|
|
|
}
|
|
|
|
keyBytes, err := ioutil.ReadFile(keyPath)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "error loading trusted user CA key %#v: %v", keyPath, err)
|
|
|
|
logger.WarnToConsole("error loading trusted user CA key %#v: %v", keyPath, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
parsedKey, _, _, _, err := ssh.ParseAuthorizedKey(keyBytes)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "error parsing trusted user CA key %#v: %v", keyPath, err)
|
|
|
|
logger.WarnToConsole("error parsing trusted user CA key %#v: %v", keyPath, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.parsedUserCAKeys = append(c.parsedUserCAKeys, parsedKey)
|
|
|
|
}
|
|
|
|
c.certChecker = &ssh.CertChecker{
|
|
|
|
SupportedCriticalOptions: []string{
|
|
|
|
sourceAddressCriticalOption,
|
|
|
|
},
|
|
|
|
IsUserAuthority: func(k ssh.PublicKey) bool {
|
|
|
|
for _, key := range c.parsedUserCAKeys {
|
|
|
|
if bytes.Equal(k.Marshal(), key.Marshal()) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Configuration) validatePublicKeyCredentials(conn ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
|
2019-07-20 10:26:52 +00:00
|
|
|
var err error
|
|
|
|
var user dataprovider.User
|
2019-09-05 19:35:53 +00:00
|
|
|
var keyID string
|
2019-09-13 16:45:36 +00:00
|
|
|
var sshPerm *ssh.Permissions
|
2020-05-15 18:08:53 +00:00
|
|
|
var certPerm *ssh.Permissions
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2020-04-09 21:32:42 +00:00
|
|
|
connectionID := hex.EncodeToString(conn.SessionID())
|
2020-02-19 21:39:30 +00:00
|
|
|
method := dataprovider.SSHLoginMethodPublicKey
|
2020-05-15 18:08:53 +00:00
|
|
|
cert, ok := pubKey.(*ssh.Certificate)
|
|
|
|
if ok {
|
|
|
|
if cert.CertType != ssh.UserCert {
|
|
|
|
err = fmt.Errorf("ssh: cert has type %d", cert.CertType)
|
|
|
|
updateLoginMetrics(conn, method, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !c.certChecker.IsUserAuthority(cert.SignatureKey) {
|
|
|
|
err = fmt.Errorf("ssh: certificate signed by unrecognized authority")
|
|
|
|
updateLoginMetrics(conn, method, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := c.certChecker.CheckCert(conn.User(), cert); err != nil {
|
|
|
|
updateLoginMetrics(conn, method, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
certPerm = &cert.Permissions
|
|
|
|
}
|
|
|
|
if user, keyID, err = dataprovider.CheckUserAndPubKey(dataProvider, conn.User(), pubKey.Marshal()); err == nil {
|
2020-04-09 21:32:42 +00:00
|
|
|
if user.IsPartialAuth(method) {
|
|
|
|
logger.Debug(logSender, connectionID, "user %#v authenticated with partial success", conn.User())
|
2020-05-16 13:15:32 +00:00
|
|
|
return certPerm, ssh.ErrPartialSuccess
|
2020-04-09 21:32:42 +00:00
|
|
|
}
|
|
|
|
sshPerm, err = loginUser(user, method, keyID, conn)
|
2020-05-15 18:08:53 +00:00
|
|
|
if err == nil && certPerm != nil {
|
|
|
|
// if we have a SSH user cert we need to merge certificate permissions with our ones
|
|
|
|
// we only set Extensions, so CriticalOptions are always the ones from the certificate
|
|
|
|
sshPerm.CriticalOptions = certPerm.CriticalOptions
|
|
|
|
if certPerm.Extensions != nil {
|
|
|
|
for k, v := range certPerm.Extensions {
|
|
|
|
sshPerm.Extensions[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 21:39:30 +00:00
|
|
|
}
|
2020-05-15 18:08:53 +00:00
|
|
|
updateLoginMetrics(conn, method, err)
|
2019-09-13 16:45:36 +00:00
|
|
|
return sshPerm, err
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c Configuration) validatePasswordCredentials(conn ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
|
|
|
|
var err error
|
|
|
|
var user dataprovider.User
|
2019-09-13 16:45:36 +00:00
|
|
|
var sshPerm *ssh.Permissions
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2020-02-19 21:39:30 +00:00
|
|
|
method := dataprovider.SSHLoginMethodPassword
|
2020-04-09 21:32:42 +00:00
|
|
|
if len(conn.PartialSuccessMethods()) == 1 {
|
|
|
|
method = dataprovider.SSHLoginMethodKeyAndPassword
|
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
if user, err = dataprovider.CheckUserAndPass(dataProvider, conn.User(), string(pass)); err == nil {
|
2020-04-09 21:32:42 +00:00
|
|
|
sshPerm, err = loginUser(user, method, "", conn)
|
2020-02-19 21:39:30 +00:00
|
|
|
}
|
2020-05-15 18:08:53 +00:00
|
|
|
updateLoginMetrics(conn, method, err)
|
2020-01-21 09:54:05 +00:00
|
|
|
return sshPerm, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Configuration) validateKeyboardInteractiveCredentials(conn ssh.ConnMetadata, client ssh.KeyboardInteractiveChallenge) (*ssh.Permissions, error) {
|
|
|
|
var err error
|
|
|
|
var user dataprovider.User
|
|
|
|
var sshPerm *ssh.Permissions
|
|
|
|
|
2020-02-19 21:39:30 +00:00
|
|
|
method := dataprovider.SSHLoginMethodKeyboardInteractive
|
2020-04-09 21:32:42 +00:00
|
|
|
if len(conn.PartialSuccessMethods()) == 1 {
|
|
|
|
method = dataprovider.SSHLoginMethodKeyAndKeyboardInt
|
|
|
|
}
|
2020-04-01 21:25:23 +00:00
|
|
|
if user, err = dataprovider.CheckKeyboardInteractiveAuth(dataProvider, conn.User(), c.KeyboardInteractiveHook, client); err == nil {
|
2020-04-09 21:32:42 +00:00
|
|
|
sshPerm, err = loginUser(user, method, "", conn)
|
2020-02-19 21:39:30 +00:00
|
|
|
}
|
2020-05-15 18:08:53 +00:00
|
|
|
updateLoginMetrics(conn, method, err)
|
|
|
|
return sshPerm, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateLoginMetrics(conn ssh.ConnMetadata, method string, err error) {
|
|
|
|
metrics.AddLoginAttempt(method)
|
2020-02-19 21:39:30 +00:00
|
|
|
if err != nil {
|
2020-01-21 09:54:05 +00:00
|
|
|
logger.ConnectionFailedLog(conn.User(), utils.GetIPFromRemoteAddress(conn.RemoteAddr().String()), method, err.Error())
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
2020-01-21 09:54:05 +00:00
|
|
|
metrics.AddLoginResult(method, err)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|