server.go 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package sftpd
  15. import (
  16. "bytes"
  17. "encoding/hex"
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "io"
  22. "io/fs"
  23. "net"
  24. "os"
  25. "path"
  26. "path/filepath"
  27. "runtime/debug"
  28. "strings"
  29. "sync"
  30. "time"
  31. "github.com/pkg/sftp"
  32. "github.com/sftpgo/sdk/plugin/notifier"
  33. "golang.org/x/crypto/ssh"
  34. "github.com/drakkan/sftpgo/v2/internal/common"
  35. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  36. "github.com/drakkan/sftpgo/v2/internal/logger"
  37. "github.com/drakkan/sftpgo/v2/internal/metric"
  38. "github.com/drakkan/sftpgo/v2/internal/plugin"
  39. "github.com/drakkan/sftpgo/v2/internal/util"
  40. "github.com/drakkan/sftpgo/v2/internal/vfs"
  41. )
  42. const (
  43. defaultPrivateRSAKeyName = "id_rsa"
  44. defaultPrivateECDSAKeyName = "id_ecdsa"
  45. defaultPrivateEd25519KeyName = "id_ed25519"
  46. sourceAddressCriticalOption = "source-address"
  47. kexDHGroupExchangeSHA1 = "diffie-hellman-group-exchange-sha1"
  48. kexDHGroupExchangeSHA256 = "diffie-hellman-group-exchange-sha256"
  49. )
  50. var (
  51. sftpExtensions = []string{"[email protected]"}
  52. supportedHostKeyAlgos = []string{
  53. ssh.CertAlgoRSASHA512v01, ssh.CertAlgoRSASHA256v01,
  54. ssh.CertAlgoRSAv01, ssh.CertAlgoDSAv01, ssh.CertAlgoECDSA256v01,
  55. ssh.CertAlgoECDSA384v01, ssh.CertAlgoECDSA521v01, ssh.CertAlgoED25519v01,
  56. ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521,
  57. ssh.KeyAlgoRSASHA512, ssh.KeyAlgoRSASHA256,
  58. ssh.KeyAlgoRSA, ssh.KeyAlgoDSA,
  59. ssh.KeyAlgoED25519,
  60. }
  61. preferredHostKeyAlgos = []string{
  62. ssh.KeyAlgoRSASHA256, ssh.KeyAlgoRSASHA512,
  63. ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521,
  64. ssh.KeyAlgoED25519,
  65. }
  66. supportedPublicKeyAlgos = []string{
  67. ssh.KeyAlgoED25519,
  68. ssh.KeyAlgoSKED25519, ssh.KeyAlgoSKECDSA256,
  69. ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521,
  70. ssh.KeyAlgoRSASHA256, ssh.KeyAlgoRSASHA512, ssh.KeyAlgoRSA,
  71. ssh.KeyAlgoDSA,
  72. }
  73. preferredPublicKeyAlgos = []string{
  74. ssh.KeyAlgoED25519,
  75. ssh.KeyAlgoSKED25519, ssh.KeyAlgoSKECDSA256,
  76. ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521,
  77. ssh.KeyAlgoRSASHA256, ssh.KeyAlgoRSASHA512,
  78. }
  79. supportedKexAlgos = []string{
  80. "curve25519-sha256", "[email protected]",
  81. "ecdh-sha2-nistp256", "ecdh-sha2-nistp384", "ecdh-sha2-nistp521",
  82. "diffie-hellman-group14-sha256", "diffie-hellman-group16-sha512",
  83. "diffie-hellman-group18-sha512", "diffie-hellman-group14-sha1",
  84. "diffie-hellman-group1-sha1",
  85. }
  86. preferredKexAlgos = []string{
  87. "curve25519-sha256", "[email protected]",
  88. "ecdh-sha2-nistp256", "ecdh-sha2-nistp384", "ecdh-sha2-nistp521",
  89. "diffie-hellman-group14-sha256",
  90. }
  91. supportedCiphers = []string{
  92. "[email protected]", "[email protected]",
  93. "[email protected]",
  94. "aes128-ctr", "aes192-ctr", "aes256-ctr",
  95. "aes128-cbc", "aes192-cbc", "aes256-cbc",
  96. "3des-cbc",
  97. "arcfour", "arcfour128", "arcfour256",
  98. }
  99. preferredCiphers = []string{
  100. "[email protected]", "[email protected]",
  101. "[email protected]",
  102. "aes128-ctr", "aes192-ctr", "aes256-ctr",
  103. }
  104. supportedMACs = []string{
  105. "[email protected]", "hmac-sha2-256",
  106. "[email protected]", "hmac-sha2-512",
  107. "hmac-sha1", "hmac-sha1-96",
  108. }
  109. preferredMACs = []string{
  110. "[email protected]", "hmac-sha2-256",
  111. }
  112. revokedCertManager = revokedCertificates{
  113. certs: map[string]bool{},
  114. }
  115. sftpAuthError = newAuthenticationError(nil, "")
  116. )
  117. // Binding defines the configuration for a network listener
  118. type Binding struct {
  119. // The address to listen on. A blank value means listen on all available network interfaces.
  120. Address string `json:"address" mapstructure:"address"`
  121. // The port used for serving requests
  122. Port int `json:"port" mapstructure:"port"`
  123. // Apply the proxy configuration, if any, for this binding
  124. ApplyProxyConfig bool `json:"apply_proxy_config" mapstructure:"apply_proxy_config"`
  125. }
  126. // GetAddress returns the binding address
  127. func (b *Binding) GetAddress() string {
  128. return fmt.Sprintf("%s:%d", b.Address, b.Port)
  129. }
  130. // IsValid returns true if the binding port is > 0
  131. func (b *Binding) IsValid() bool {
  132. return b.Port > 0
  133. }
  134. // HasProxy returns true if the proxy protocol is active for this binding
  135. func (b *Binding) HasProxy() bool {
  136. return b.ApplyProxyConfig && common.Config.ProxyProtocol > 0
  137. }
  138. // Configuration for the SFTP server
  139. type Configuration struct {
  140. // Identification string used by the server
  141. Banner string `json:"banner" mapstructure:"banner"`
  142. // Addresses and ports to bind to
  143. Bindings []Binding `json:"bindings" mapstructure:"bindings"`
  144. // Maximum number of authentication attempts permitted per connection.
  145. // If set to a negative number, the number of attempts is unlimited.
  146. // If set to zero, the number of attempts are limited to 6.
  147. MaxAuthTries int `json:"max_auth_tries" mapstructure:"max_auth_tries"`
  148. // HostKeys define the daemon's private host keys.
  149. // Each host key can be defined as a path relative to the configuration directory or an absolute one.
  150. // If empty or missing, the daemon will search or try to generate "id_rsa" and "id_ecdsa" host keys
  151. // inside the configuration directory.
  152. HostKeys []string `json:"host_keys" mapstructure:"host_keys"`
  153. // HostCertificates defines public host certificates.
  154. // Each certificate can be defined as a path relative to the configuration directory or an absolute one.
  155. // Certificate's public key must match a private host key otherwise it will be silently ignored.
  156. HostCertificates []string `json:"host_certificates" mapstructure:"host_certificates"`
  157. // HostKeyAlgorithms lists the public key algorithms that the server will accept for host
  158. // key authentication.
  159. HostKeyAlgorithms []string `json:"host_key_algorithms" mapstructure:"host_key_algorithms"`
  160. // Diffie-Hellman moduli files.
  161. // Each moduli file can be defined as a path relative to the configuration directory or an absolute one.
  162. // If set and valid, "diffie-hellman-group-exchange-sha256" and "diffie-hellman-group-exchange-sha1" KEX algorithms
  163. // will be available, `diffie-hellman-group-exchange-sha256` will be enabled by default if you
  164. // don't explicitly set KEXs
  165. Moduli []string `json:"moduli" mapstructure:"moduli"`
  166. // KexAlgorithms specifies the available KEX (Key Exchange) algorithms in
  167. // preference order.
  168. KexAlgorithms []string `json:"kex_algorithms" mapstructure:"kex_algorithms"`
  169. // Ciphers specifies the ciphers allowed
  170. Ciphers []string `json:"ciphers" mapstructure:"ciphers"`
  171. // MACs Specifies the available MAC (message authentication code) algorithms
  172. // in preference order
  173. MACs []string `json:"macs" mapstructure:"macs"`
  174. // PublicKeyAlgorithms lists the supported public key algorithms for client authentication.
  175. PublicKeyAlgorithms []string `json:"public_key_algorithms" mapstructure:"public_key_algorithms"`
  176. // TrustedUserCAKeys specifies a list of public keys paths of certificate authorities
  177. // that are trusted to sign user certificates for authentication.
  178. // The paths can be absolute or relative to the configuration directory
  179. TrustedUserCAKeys []string `json:"trusted_user_ca_keys" mapstructure:"trusted_user_ca_keys"`
  180. // Path to a file containing the revoked user certificates.
  181. // This file must contain a JSON list with the public key fingerprints of the revoked certificates.
  182. // Example content:
  183. // ["SHA256:bsBRHC/xgiqBJdSuvSTNpJNLTISP/G356jNMCRYC5Es","SHA256:119+8cL/HH+NLMawRsJx6CzPF1I3xC+jpM60bQHXGE8"]
  184. RevokedUserCertsFile string `json:"revoked_user_certs_file" mapstructure:"revoked_user_certs_file"`
  185. // LoginBannerFile the contents of the specified file, if any, are sent to
  186. // the remote user before authentication is allowed.
  187. LoginBannerFile string `json:"login_banner_file" mapstructure:"login_banner_file"`
  188. // List of enabled SSH commands.
  189. // We support the following SSH commands:
  190. // - "scp". SCP is an experimental feature, we have our own SCP implementation since
  191. // we can't rely on scp system command to proper handle permissions, quota and
  192. // user's home dir restrictions.
  193. // The SCP protocol is quite simple but there is no official docs about it,
  194. // so we need more testing and feedbacks before enabling it by default.
  195. // We may not handle some borderline cases or have sneaky bugs.
  196. // Please do accurate tests yourself before enabling SCP and let us known
  197. // if something does not work as expected for your use cases.
  198. // SCP between two remote hosts is supported using the `-3` scp option.
  199. // - "md5sum", "sha1sum", "sha256sum", "sha384sum", "sha512sum". Useful to check message
  200. // digests for uploaded files. These commands are implemented inside SFTPGo so they
  201. // work even if the matching system commands are not available, for example on Windows.
  202. // - "cd", "pwd". Some mobile SFTP clients does not support the SFTP SSH_FXP_REALPATH and so
  203. // they use "cd" and "pwd" SSH commands to get the initial directory.
  204. // Currently `cd` do nothing and `pwd` always returns the "/" path.
  205. //
  206. // The following SSH commands are enabled by default: "md5sum", "sha1sum", "cd", "pwd".
  207. // "*" enables all supported SSH commands.
  208. EnabledSSHCommands []string `json:"enabled_ssh_commands" mapstructure:"enabled_ssh_commands"`
  209. // KeyboardInteractiveAuthentication specifies whether keyboard interactive authentication is allowed.
  210. // If no keyboard interactive hook or auth plugin is defined the default is to prompt for the user password and then the
  211. // one time authentication code, if defined.
  212. KeyboardInteractiveAuthentication bool `json:"keyboard_interactive_authentication" mapstructure:"keyboard_interactive_authentication"`
  213. // Absolute path to an external program or an HTTP URL to invoke for keyboard interactive authentication.
  214. // Leave empty to disable this authentication mode.
  215. KeyboardInteractiveHook string `json:"keyboard_interactive_auth_hook" mapstructure:"keyboard_interactive_auth_hook"`
  216. // PasswordAuthentication specifies whether password authentication is allowed.
  217. PasswordAuthentication bool `json:"password_authentication" mapstructure:"password_authentication"`
  218. // Virtual root folder prefix to include in all file operations (ex: /files).
  219. // The virtual paths used for per-directory permissions, file patterns etc. must not include the folder prefix.
  220. // The prefix is only applied to SFTP requests, SCP and other SSH commands will be automatically disabled if
  221. // you configure a prefix.
  222. // This setting can help some migrations from OpenSSH. It is not recommended for general usage.
  223. FolderPrefix string `json:"folder_prefix" mapstructure:"folder_prefix"`
  224. certChecker *ssh.CertChecker
  225. parsedUserCAKeys []ssh.PublicKey
  226. }
  227. type authenticationError struct {
  228. err error
  229. loginMethod string
  230. }
  231. func (e *authenticationError) Error() string {
  232. return fmt.Sprintf("Authentication error: %v", e.err)
  233. }
  234. // Is reports if target matches
  235. func (e *authenticationError) Is(target error) bool {
  236. _, ok := target.(*authenticationError)
  237. return ok
  238. }
  239. // Unwrap returns the wrapped error
  240. func (e *authenticationError) Unwrap() error {
  241. return e.err
  242. }
  243. func (e *authenticationError) getLoginMethod() string {
  244. return e.loginMethod
  245. }
  246. func newAuthenticationError(err error, loginMethod string) *authenticationError {
  247. return &authenticationError{err: err, loginMethod: loginMethod}
  248. }
  249. // ShouldBind returns true if there is at least a valid binding
  250. func (c *Configuration) ShouldBind() bool {
  251. for _, binding := range c.Bindings {
  252. if binding.IsValid() {
  253. return true
  254. }
  255. }
  256. return false
  257. }
  258. func (c *Configuration) getServerConfig() *ssh.ServerConfig {
  259. serverConfig := &ssh.ServerConfig{
  260. NoClientAuth: false,
  261. MaxAuthTries: c.MaxAuthTries,
  262. PublicKeyCallback: func(conn ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
  263. sp, err := c.validatePublicKeyCredentials(conn, pubKey)
  264. var partialSuccess *ssh.PartialSuccessError
  265. if errors.As(err, &partialSuccess) {
  266. return sp, err
  267. }
  268. if err != nil {
  269. return nil, newAuthenticationError(fmt.Errorf("could not validate public key credentials: %w", err),
  270. dataprovider.SSHLoginMethodPublicKey)
  271. }
  272. return sp, nil
  273. },
  274. ServerVersion: fmt.Sprintf("SSH-2.0-%s", c.Banner),
  275. }
  276. if c.PasswordAuthentication {
  277. serverConfig.PasswordCallback = func(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
  278. return c.validatePasswordCredentials(conn, password, dataprovider.LoginMethodPassword)
  279. }
  280. serviceStatus.Authentications = append(serviceStatus.Authentications, dataprovider.LoginMethodPassword)
  281. }
  282. serviceStatus.Authentications = append(serviceStatus.Authentications, dataprovider.SSHLoginMethodPublicKey)
  283. return serverConfig
  284. }
  285. func (c *Configuration) updateSupportedAuthentications() {
  286. serviceStatus.Authentications = util.RemoveDuplicates(serviceStatus.Authentications, false)
  287. if util.Contains(serviceStatus.Authentications, dataprovider.LoginMethodPassword) &&
  288. util.Contains(serviceStatus.Authentications, dataprovider.SSHLoginMethodPublicKey) {
  289. serviceStatus.Authentications = append(serviceStatus.Authentications, dataprovider.SSHLoginMethodKeyAndPassword)
  290. }
  291. if util.Contains(serviceStatus.Authentications, dataprovider.SSHLoginMethodKeyboardInteractive) &&
  292. util.Contains(serviceStatus.Authentications, dataprovider.SSHLoginMethodPublicKey) {
  293. serviceStatus.Authentications = append(serviceStatus.Authentications, dataprovider.SSHLoginMethodKeyAndKeyboardInt)
  294. }
  295. }
  296. func (c *Configuration) loadFromProvider() error {
  297. configs, err := dataprovider.GetConfigs()
  298. if err != nil {
  299. return fmt.Errorf("unable to load config from provider: %w", err)
  300. }
  301. configs.SetNilsToEmpty()
  302. if len(configs.SFTPD.HostKeyAlgos) > 0 {
  303. if len(c.HostKeyAlgorithms) == 0 {
  304. c.HostKeyAlgorithms = preferredHostKeyAlgos
  305. }
  306. c.HostKeyAlgorithms = append(c.HostKeyAlgorithms, configs.SFTPD.HostKeyAlgos...)
  307. }
  308. if len(configs.SFTPD.PublicKeyAlgos) > 0 {
  309. if len(c.PublicKeyAlgorithms) == 0 {
  310. c.PublicKeyAlgorithms = preferredPublicKeyAlgos
  311. }
  312. c.PublicKeyAlgorithms = append(c.PublicKeyAlgorithms, configs.SFTPD.PublicKeyAlgos...)
  313. }
  314. if len(configs.SFTPD.KexAlgorithms) > 0 {
  315. if len(c.KexAlgorithms) == 0 {
  316. c.KexAlgorithms = preferredKexAlgos
  317. }
  318. c.KexAlgorithms = append(c.KexAlgorithms, configs.SFTPD.KexAlgorithms...)
  319. }
  320. if len(configs.SFTPD.Ciphers) > 0 {
  321. if len(c.Ciphers) == 0 {
  322. c.Ciphers = preferredCiphers
  323. }
  324. c.Ciphers = append(c.Ciphers, configs.SFTPD.Ciphers...)
  325. }
  326. if len(configs.SFTPD.MACs) > 0 {
  327. if len(c.MACs) == 0 {
  328. c.MACs = preferredMACs
  329. }
  330. c.MACs = append(c.MACs, configs.SFTPD.MACs...)
  331. }
  332. return nil
  333. }
  334. // Initialize the SFTP server and add a persistent listener to handle inbound SFTP connections.
  335. func (c *Configuration) Initialize(configDir string) error {
  336. if err := c.loadFromProvider(); err != nil {
  337. return fmt.Errorf("unable to load configs from provider: %w", err)
  338. }
  339. serviceStatus = ServiceStatus{}
  340. serverConfig := c.getServerConfig()
  341. if !c.ShouldBind() {
  342. return common.ErrNoBinding
  343. }
  344. c.loadModuli(configDir)
  345. sftp.SetSFTPExtensions(sftpExtensions...) //nolint:errcheck // we configure valid SFTP Extensions so we cannot get an error
  346. sftp.MaxFilelist = vfs.ListerBatchSize
  347. if err := c.configureSecurityOptions(serverConfig); err != nil {
  348. return err
  349. }
  350. if err := c.checkAndLoadHostKeys(configDir, serverConfig); err != nil {
  351. serviceStatus.HostKeys = nil
  352. return err
  353. }
  354. if err := c.initializeCertChecker(configDir); err != nil {
  355. return err
  356. }
  357. c.configureKeyboardInteractiveAuth(serverConfig)
  358. c.configureLoginBanner(serverConfig, configDir)
  359. c.checkSSHCommands()
  360. c.checkFolderPrefix()
  361. exitChannel := make(chan error, 1)
  362. serviceStatus.Bindings = nil
  363. for _, binding := range c.Bindings {
  364. if !binding.IsValid() {
  365. continue
  366. }
  367. serviceStatus.Bindings = append(serviceStatus.Bindings, binding)
  368. go func(binding Binding) {
  369. addr := binding.GetAddress()
  370. util.CheckTCP4Port(binding.Port)
  371. listener, err := net.Listen("tcp", addr)
  372. if err != nil {
  373. logger.Warn(logSender, "", "error starting listener on address %v: %v", addr, err)
  374. exitChannel <- err
  375. return
  376. }
  377. if binding.ApplyProxyConfig && common.Config.ProxyProtocol > 0 {
  378. proxyListener, err := common.Config.GetProxyListener(listener)
  379. if err != nil {
  380. logger.Warn(logSender, "", "error enabling proxy listener: %v", err)
  381. exitChannel <- err
  382. return
  383. }
  384. listener = proxyListener
  385. }
  386. exitChannel <- c.serve(listener, serverConfig)
  387. }(binding)
  388. }
  389. serviceStatus.IsActive = true
  390. serviceStatus.SSHCommands = c.EnabledSSHCommands
  391. c.updateSupportedAuthentications()
  392. return <-exitChannel
  393. }
  394. func (c *Configuration) serve(listener net.Listener, serverConfig *ssh.ServerConfig) error {
  395. logger.Info(logSender, "", "server listener registered, address: %s", listener.Addr().String())
  396. var tempDelay time.Duration // how long to sleep on accept failure
  397. for {
  398. conn, err := listener.Accept()
  399. if err != nil {
  400. // see https://github.com/golang/go/blob/4aa1efed4853ea067d665a952eee77c52faac774/src/net/http/server.go#L3046
  401. if ne, ok := err.(net.Error); ok && ne.Temporary() { //nolint:staticcheck
  402. if tempDelay == 0 {
  403. tempDelay = 5 * time.Millisecond
  404. } else {
  405. tempDelay *= 2
  406. }
  407. if max := 1 * time.Second; tempDelay > max {
  408. tempDelay = max
  409. }
  410. logger.Warn(logSender, "", "accept error: %v; retrying in %v", err, tempDelay)
  411. time.Sleep(tempDelay)
  412. continue
  413. }
  414. logger.Warn(logSender, "", "unrecoverable accept error: %v", err)
  415. return err
  416. }
  417. tempDelay = 0
  418. go c.AcceptInboundConnection(conn, serverConfig)
  419. }
  420. }
  421. func (c *Configuration) configureKeyAlgos(serverConfig *ssh.ServerConfig) error {
  422. if len(c.HostKeyAlgorithms) == 0 {
  423. c.HostKeyAlgorithms = preferredHostKeyAlgos
  424. } else {
  425. c.HostKeyAlgorithms = util.RemoveDuplicates(c.HostKeyAlgorithms, true)
  426. }
  427. for _, hostKeyAlgo := range c.HostKeyAlgorithms {
  428. if !util.Contains(supportedHostKeyAlgos, hostKeyAlgo) {
  429. return fmt.Errorf("unsupported host key algorithm %q", hostKeyAlgo)
  430. }
  431. }
  432. if len(c.PublicKeyAlgorithms) > 0 {
  433. c.PublicKeyAlgorithms = util.RemoveDuplicates(c.PublicKeyAlgorithms, true)
  434. for _, algo := range c.PublicKeyAlgorithms {
  435. if !util.Contains(supportedPublicKeyAlgos, algo) {
  436. return fmt.Errorf("unsupported public key authentication algorithm %q", algo)
  437. }
  438. }
  439. } else {
  440. c.PublicKeyAlgorithms = preferredPublicKeyAlgos
  441. }
  442. serverConfig.PublicKeyAuthAlgorithms = c.PublicKeyAlgorithms
  443. serviceStatus.PublicKeyAlgorithms = c.PublicKeyAlgorithms
  444. return nil
  445. }
  446. func (c *Configuration) configureSecurityOptions(serverConfig *ssh.ServerConfig) error {
  447. if err := c.configureKeyAlgos(serverConfig); err != nil {
  448. return err
  449. }
  450. if len(c.KexAlgorithms) > 0 {
  451. hasDHGroupKEX := util.Contains(supportedKexAlgos, kexDHGroupExchangeSHA256)
  452. if !hasDHGroupKEX {
  453. c.KexAlgorithms = util.Remove(c.KexAlgorithms, kexDHGroupExchangeSHA1)
  454. c.KexAlgorithms = util.Remove(c.KexAlgorithms, kexDHGroupExchangeSHA256)
  455. }
  456. c.KexAlgorithms = util.RemoveDuplicates(c.KexAlgorithms, true)
  457. for _, kex := range c.KexAlgorithms {
  458. if kex == "diffie-hellman-group18-sha512" {
  459. logger.Warn(logSender, "", "KEX %q is not supported and will be ignored", kex)
  460. }
  461. if !util.Contains(supportedKexAlgos, kex) {
  462. return fmt.Errorf("unsupported key-exchange algorithm %q", kex)
  463. }
  464. }
  465. } else {
  466. c.KexAlgorithms = preferredKexAlgos
  467. }
  468. serverConfig.KeyExchanges = c.KexAlgorithms
  469. serviceStatus.KexAlgorithms = c.KexAlgorithms
  470. if len(c.Ciphers) > 0 {
  471. c.Ciphers = util.RemoveDuplicates(c.Ciphers, true)
  472. for _, cipher := range c.Ciphers {
  473. if !util.Contains(supportedCiphers, cipher) {
  474. return fmt.Errorf("unsupported cipher %q", cipher)
  475. }
  476. }
  477. } else {
  478. c.Ciphers = preferredCiphers
  479. }
  480. serverConfig.Ciphers = c.Ciphers
  481. serviceStatus.Ciphers = c.Ciphers
  482. if len(c.MACs) > 0 {
  483. c.MACs = util.RemoveDuplicates(c.MACs, true)
  484. for _, mac := range c.MACs {
  485. if !util.Contains(supportedMACs, mac) {
  486. return fmt.Errorf("unsupported MAC algorithm %q", mac)
  487. }
  488. }
  489. } else {
  490. c.MACs = preferredMACs
  491. }
  492. serverConfig.MACs = c.MACs
  493. serviceStatus.MACs = c.MACs
  494. return nil
  495. }
  496. func (c *Configuration) configureLoginBanner(serverConfig *ssh.ServerConfig, configDir string) {
  497. if len(c.LoginBannerFile) > 0 {
  498. bannerFilePath := c.LoginBannerFile
  499. if !filepath.IsAbs(bannerFilePath) {
  500. bannerFilePath = filepath.Join(configDir, bannerFilePath)
  501. }
  502. bannerContent, err := os.ReadFile(bannerFilePath)
  503. if err == nil {
  504. banner := string(bannerContent)
  505. serverConfig.BannerCallback = func(_ ssh.ConnMetadata) string {
  506. return banner
  507. }
  508. } else {
  509. logger.WarnToConsole("unable to read SFTPD login banner file: %v", err)
  510. logger.Warn(logSender, "", "unable to read login banner file: %v", err)
  511. }
  512. }
  513. }
  514. func (c *Configuration) configureKeyboardInteractiveAuth(serverConfig *ssh.ServerConfig) {
  515. if !c.KeyboardInteractiveAuthentication {
  516. return
  517. }
  518. if c.KeyboardInteractiveHook != "" {
  519. if !strings.HasPrefix(c.KeyboardInteractiveHook, "http") {
  520. if !filepath.IsAbs(c.KeyboardInteractiveHook) {
  521. c.KeyboardInteractiveAuthentication = false
  522. logger.WarnToConsole("invalid keyboard interactive authentication program: %q must be an absolute path",
  523. c.KeyboardInteractiveHook)
  524. logger.Warn(logSender, "", "invalid keyboard interactive authentication program: %q must be an absolute path",
  525. c.KeyboardInteractiveHook)
  526. return
  527. }
  528. _, err := os.Stat(c.KeyboardInteractiveHook)
  529. if err != nil {
  530. c.KeyboardInteractiveAuthentication = false
  531. logger.WarnToConsole("invalid keyboard interactive authentication program:: %v", err)
  532. logger.Warn(logSender, "", "invalid keyboard interactive authentication program:: %v", err)
  533. return
  534. }
  535. }
  536. }
  537. serverConfig.KeyboardInteractiveCallback = func(conn ssh.ConnMetadata, client ssh.KeyboardInteractiveChallenge) (*ssh.Permissions, error) {
  538. return c.validateKeyboardInteractiveCredentials(conn, client, dataprovider.SSHLoginMethodKeyboardInteractive, false)
  539. }
  540. serviceStatus.Authentications = append(serviceStatus.Authentications, dataprovider.SSHLoginMethodKeyboardInteractive)
  541. }
  542. // AcceptInboundConnection handles an inbound connection to the server instance and determines if the request should be served or not.
  543. func (c *Configuration) AcceptInboundConnection(conn net.Conn, config *ssh.ServerConfig) {
  544. defer func() {
  545. if r := recover(); r != nil {
  546. logger.Error(logSender, "", "panic in AcceptInboundConnection: %q stack trace: %v", r, string(debug.Stack()))
  547. }
  548. }()
  549. ipAddr := util.GetIPFromRemoteAddress(conn.RemoteAddr().String())
  550. common.Connections.AddClientConnection(ipAddr)
  551. defer common.Connections.RemoveClientConnection(ipAddr)
  552. if !canAcceptConnection(ipAddr) {
  553. conn.Close()
  554. return
  555. }
  556. // Before beginning a handshake must be performed on the incoming net.Conn
  557. // we'll set a Deadline for handshake to complete, the default is 2 minutes as OpenSSH
  558. conn.SetDeadline(time.Now().Add(handshakeTimeout)) //nolint:errcheck
  559. sconn, chans, reqs, err := ssh.NewServerConn(conn, config)
  560. if err != nil {
  561. logger.Debug(logSender, "", "failed to accept an incoming connection from ip %q: %v", ipAddr, err)
  562. checkAuthError(ipAddr, err)
  563. return
  564. }
  565. // handshake completed so remove the deadline, we'll use IdleTimeout configuration from now on
  566. conn.SetDeadline(time.Time{}) //nolint:errcheck
  567. go ssh.DiscardRequests(reqs)
  568. defer conn.Close()
  569. var user dataprovider.User
  570. // Unmarshal cannot fails here and even if it fails we'll have a user with no permissions
  571. json.Unmarshal([]byte(sconn.Permissions.Extensions["sftpgo_user"]), &user) //nolint:errcheck
  572. loginType := sconn.Permissions.Extensions["sftpgo_login_method"]
  573. connectionID := hex.EncodeToString(sconn.SessionID())
  574. defer user.CloseFs() //nolint:errcheck
  575. if err = user.CheckFsRoot(connectionID); err != nil {
  576. logger.Warn(logSender, connectionID, "unable to check fs root for user %q: %v", user.Username, err)
  577. go discardAllChannels(chans, "invalid root fs", connectionID)
  578. return
  579. }
  580. logger.Log(logger.LevelInfo, common.ProtocolSSH, connectionID,
  581. "User %q logged in with %q, from ip %q, client version %q", user.Username, loginType,
  582. ipAddr, string(sconn.ClientVersion()))
  583. dataprovider.UpdateLastLogin(&user)
  584. sshConnection := common.NewSSHConnection(connectionID, conn)
  585. common.Connections.AddSSHConnection(sshConnection)
  586. defer common.Connections.RemoveSSHConnection(connectionID)
  587. channelCounter := int64(0)
  588. for newChannel := range chans {
  589. // If its not a session channel we just move on because its not something we
  590. // know how to handle at this point.
  591. if newChannel.ChannelType() != "session" {
  592. logger.Log(logger.LevelDebug, common.ProtocolSSH, connectionID, "received an unknown channel type: %v",
  593. newChannel.ChannelType())
  594. newChannel.Reject(ssh.UnknownChannelType, "unknown channel type") //nolint:errcheck
  595. continue
  596. }
  597. channel, requests, err := newChannel.Accept()
  598. if err != nil {
  599. logger.Log(logger.LevelWarn, common.ProtocolSSH, connectionID, "could not accept a channel: %v", err)
  600. continue
  601. }
  602. channelCounter++
  603. sshConnection.UpdateLastActivity()
  604. // Channels have a type that is dependent on the protocol. For SFTP this is "subsystem"
  605. // with a payload that (should) be "sftp". Discard anything else we receive ("pty", "shell", etc)
  606. go func(in <-chan *ssh.Request, counter int64) {
  607. for req := range in {
  608. ok := false
  609. connID := fmt.Sprintf("%s_%d", connectionID, counter)
  610. switch req.Type {
  611. case "subsystem":
  612. if string(req.Payload[4:]) == "sftp" {
  613. ok = true
  614. connection := &Connection{
  615. BaseConnection: common.NewBaseConnection(connID, common.ProtocolSFTP, conn.LocalAddr().String(),
  616. conn.RemoteAddr().String(), user),
  617. ClientVersion: string(sconn.ClientVersion()),
  618. RemoteAddr: conn.RemoteAddr(),
  619. LocalAddr: conn.LocalAddr(),
  620. channel: channel,
  621. folderPrefix: c.FolderPrefix,
  622. }
  623. go c.handleSftpConnection(channel, connection)
  624. }
  625. case "exec":
  626. // protocol will be set later inside processSSHCommand it could be SSH or SCP
  627. connection := Connection{
  628. BaseConnection: common.NewBaseConnection(connID, "sshd_exec", conn.LocalAddr().String(),
  629. conn.RemoteAddr().String(), user),
  630. ClientVersion: string(sconn.ClientVersion()),
  631. RemoteAddr: conn.RemoteAddr(),
  632. LocalAddr: conn.LocalAddr(),
  633. channel: channel,
  634. folderPrefix: c.FolderPrefix,
  635. }
  636. ok = processSSHCommand(req.Payload, &connection, c.EnabledSSHCommands)
  637. }
  638. if req.WantReply {
  639. req.Reply(ok, nil) //nolint:errcheck
  640. }
  641. }
  642. }(requests, channelCounter)
  643. }
  644. }
  645. func (c *Configuration) handleSftpConnection(channel ssh.Channel, connection *Connection) {
  646. defer func() {
  647. if r := recover(); r != nil {
  648. logger.Error(logSender, "", "panic in handleSftpConnection: %q stack trace: %v", r, string(debug.Stack()))
  649. }
  650. }()
  651. if err := common.Connections.Add(connection); err != nil {
  652. errClose := connection.Disconnect()
  653. logger.Info(logSender, "", "unable to add connection: %v, close err: %v", err, errClose)
  654. return
  655. }
  656. defer common.Connections.Remove(connection.GetID())
  657. // Create the server instance for the channel using the handler we created above.
  658. server := sftp.NewRequestServer(channel, c.createHandlers(connection), sftp.WithRSAllocator(),
  659. sftp.WithStartDirectory(connection.User.Filters.StartDirectory))
  660. defer server.Close()
  661. if err := server.Serve(); errors.Is(err, io.EOF) {
  662. exitStatus := sshSubsystemExitStatus{Status: uint32(0)}
  663. _, err = channel.SendRequest("exit-status", false, ssh.Marshal(&exitStatus))
  664. connection.Log(logger.LevelInfo, "connection closed, sent exit status %+v error: %v", exitStatus, err)
  665. } else if err != nil {
  666. connection.Log(logger.LevelError, "connection closed with error: %v", err)
  667. }
  668. }
  669. func (c *Configuration) createHandlers(connection *Connection) sftp.Handlers {
  670. if c.FolderPrefix != "" {
  671. prefixMiddleware := newPrefixMiddleware(c.FolderPrefix, connection)
  672. return sftp.Handlers{
  673. FileGet: prefixMiddleware,
  674. FilePut: prefixMiddleware,
  675. FileCmd: prefixMiddleware,
  676. FileList: prefixMiddleware,
  677. }
  678. }
  679. return sftp.Handlers{
  680. FileGet: connection,
  681. FilePut: connection,
  682. FileCmd: connection,
  683. FileList: connection,
  684. }
  685. }
  686. func canAcceptConnection(ip string) bool {
  687. if common.IsBanned(ip, common.ProtocolSSH) {
  688. logger.Log(logger.LevelDebug, common.ProtocolSSH, "", "connection refused, ip %q is banned", ip)
  689. return false
  690. }
  691. if err := common.Connections.IsNewConnectionAllowed(ip, common.ProtocolSSH); err != nil {
  692. logger.Log(logger.LevelDebug, common.ProtocolSSH, "", "connection not allowed from ip %q: %v", ip, err)
  693. return false
  694. }
  695. _, err := common.LimitRate(common.ProtocolSSH, ip)
  696. if err != nil {
  697. return false
  698. }
  699. if err := common.Config.ExecutePostConnectHook(ip, common.ProtocolSSH); err != nil {
  700. return false
  701. }
  702. return true
  703. }
  704. func discardAllChannels(in <-chan ssh.NewChannel, message, connectionID string) {
  705. for req := range in {
  706. err := req.Reject(ssh.ConnectionFailed, message)
  707. logger.Debug(logSender, connectionID, "discarded channel request, message %q err: %v", message, err)
  708. }
  709. }
  710. func checkAuthError(ip string, err error) {
  711. if authErrors, ok := err.(*ssh.ServerAuthError); ok {
  712. // check public key auth errors here
  713. for _, err := range authErrors.Errors {
  714. var sftpAuthErr *authenticationError
  715. if errors.As(err, &sftpAuthErr) {
  716. if sftpAuthErr.getLoginMethod() == dataprovider.SSHLoginMethodPublicKey {
  717. event := common.HostEventLoginFailed
  718. logEv := notifier.LogEventTypeLoginFailed
  719. if errors.Is(err, util.ErrNotFound) {
  720. event = common.HostEventUserNotFound
  721. logEv = notifier.LogEventTypeLoginNoUser
  722. }
  723. common.AddDefenderEvent(ip, common.ProtocolSSH, event)
  724. plugin.Handler.NotifyLogEvent(logEv, common.ProtocolSSH, "", ip, "", err)
  725. return
  726. }
  727. }
  728. }
  729. } else {
  730. logger.ConnectionFailedLog("", ip, dataprovider.LoginMethodNoAuthTried, common.ProtocolSSH, err.Error())
  731. metric.AddNoAuthTried()
  732. common.AddDefenderEvent(ip, common.ProtocolSSH, common.HostEventNoLoginTried)
  733. dataprovider.ExecutePostLoginHook(&dataprovider.User{}, dataprovider.LoginMethodNoAuthTried, ip, common.ProtocolSSH, err)
  734. logEv := notifier.LogEventTypeNoLoginTried
  735. if errors.Is(err, ssh.ErrNoCommonAlgo) {
  736. logEv = notifier.LogEventTypeNotNegotiated
  737. }
  738. plugin.Handler.NotifyLogEvent(logEv, common.ProtocolSSH, "", ip, "", err)
  739. }
  740. }
  741. func loginUser(user *dataprovider.User, loginMethod, publicKey string, conn ssh.ConnMetadata) (*ssh.Permissions, error) {
  742. connectionID := ""
  743. if conn != nil {
  744. connectionID = hex.EncodeToString(conn.SessionID())
  745. }
  746. if !filepath.IsAbs(user.HomeDir) {
  747. logger.Warn(logSender, connectionID, "user %q has an invalid home dir: %q. Home dir must be an absolute path, login not allowed",
  748. user.Username, user.HomeDir)
  749. return nil, fmt.Errorf("cannot login user with invalid home dir: %q", user.HomeDir)
  750. }
  751. if util.Contains(user.Filters.DeniedProtocols, common.ProtocolSSH) {
  752. logger.Info(logSender, connectionID, "cannot login user %q, protocol SSH is not allowed", user.Username)
  753. return nil, fmt.Errorf("protocol SSH is not allowed for user %q", user.Username)
  754. }
  755. if user.MaxSessions > 0 {
  756. activeSessions := common.Connections.GetActiveSessions(user.Username)
  757. if activeSessions >= user.MaxSessions {
  758. logger.Info(logSender, "", "authentication refused for user: %q, too many open sessions: %v/%v", user.Username,
  759. activeSessions, user.MaxSessions)
  760. return nil, fmt.Errorf("too many open sessions: %v", activeSessions)
  761. }
  762. }
  763. if !user.IsLoginMethodAllowed(loginMethod, common.ProtocolSSH) {
  764. logger.Info(logSender, connectionID, "cannot login user %q, login method %q is not allowed",
  765. user.Username, loginMethod)
  766. return nil, fmt.Errorf("login method %q is not allowed for user %q", loginMethod, user.Username)
  767. }
  768. if user.MustSetSecondFactorForProtocol(common.ProtocolSSH) {
  769. logger.Info(logSender, connectionID, "cannot login user %q, second factor authentication is not set",
  770. user.Username)
  771. return nil, fmt.Errorf("second factor authentication is not set for user %q", user.Username)
  772. }
  773. remoteAddr := conn.RemoteAddr().String()
  774. if !user.IsLoginFromAddrAllowed(remoteAddr) {
  775. logger.Info(logSender, connectionID, "cannot login user %q, remote address is not allowed: %v",
  776. user.Username, remoteAddr)
  777. return nil, fmt.Errorf("login for user %q is not allowed from this address: %v", user.Username, remoteAddr)
  778. }
  779. json, err := json.Marshal(user)
  780. if err != nil {
  781. logger.Warn(logSender, connectionID, "error serializing user info: %v, authentication rejected", err)
  782. return nil, err
  783. }
  784. if publicKey != "" {
  785. loginMethod = fmt.Sprintf("%v: %v", loginMethod, publicKey)
  786. }
  787. p := &ssh.Permissions{}
  788. p.Extensions = make(map[string]string)
  789. p.Extensions["sftpgo_user"] = string(json)
  790. p.Extensions["sftpgo_login_method"] = loginMethod
  791. return p, nil
  792. }
  793. func (c *Configuration) checkSSHCommands() {
  794. if util.Contains(c.EnabledSSHCommands, "*") {
  795. c.EnabledSSHCommands = GetSupportedSSHCommands()
  796. return
  797. }
  798. sshCommands := []string{}
  799. for _, command := range c.EnabledSSHCommands {
  800. command = strings.TrimSpace(command)
  801. if util.Contains(supportedSSHCommands, command) {
  802. sshCommands = append(sshCommands, command)
  803. } else {
  804. logger.Warn(logSender, "", "unsupported ssh command: %q ignored", command)
  805. logger.WarnToConsole("unsupported ssh command: %q ignored", command)
  806. }
  807. }
  808. c.EnabledSSHCommands = sshCommands
  809. logger.Debug(logSender, "", "enabled SSH commands %v", c.EnabledSSHCommands)
  810. }
  811. func (c *Configuration) checkFolderPrefix() {
  812. if c.FolderPrefix != "" {
  813. c.FolderPrefix = path.Join("/", c.FolderPrefix)
  814. if c.FolderPrefix == "/" {
  815. c.FolderPrefix = ""
  816. }
  817. }
  818. if c.FolderPrefix != "" {
  819. c.EnabledSSHCommands = nil
  820. logger.Debug(logSender, "", "folder prefix %q configured, SSH commands are disabled", c.FolderPrefix)
  821. }
  822. }
  823. func (c *Configuration) generateDefaultHostKeys(configDir string) error {
  824. var err error
  825. defaultHostKeys := []string{defaultPrivateRSAKeyName, defaultPrivateECDSAKeyName, defaultPrivateEd25519KeyName}
  826. for _, k := range defaultHostKeys {
  827. autoFile := filepath.Join(configDir, k)
  828. if _, err = os.Stat(autoFile); errors.Is(err, fs.ErrNotExist) {
  829. logger.Info(logSender, "", "No host keys configured and %q does not exist; try to create a new host key", autoFile)
  830. logger.InfoToConsole("No host keys configured and %q does not exist; try to create a new host key", autoFile)
  831. if k == defaultPrivateRSAKeyName {
  832. err = util.GenerateRSAKeys(autoFile)
  833. } else if k == defaultPrivateECDSAKeyName {
  834. err = util.GenerateECDSAKeys(autoFile)
  835. } else {
  836. err = util.GenerateEd25519Keys(autoFile)
  837. }
  838. if err != nil {
  839. logger.Warn(logSender, "", "error creating host key %q: %v", autoFile, err)
  840. logger.WarnToConsole("error creating host key %q: %v", autoFile, err)
  841. return err
  842. }
  843. }
  844. c.HostKeys = append(c.HostKeys, k)
  845. }
  846. return err
  847. }
  848. func (c *Configuration) checkHostKeyAutoGeneration(configDir string) error {
  849. for _, k := range c.HostKeys {
  850. k = strings.TrimSpace(k)
  851. if filepath.IsAbs(k) {
  852. if _, err := os.Stat(k); errors.Is(err, fs.ErrNotExist) {
  853. keyName := filepath.Base(k)
  854. switch keyName {
  855. case defaultPrivateRSAKeyName:
  856. logger.Info(logSender, "", "try to create non-existent host key %q", k)
  857. logger.InfoToConsole("try to create non-existent host key %q", k)
  858. err = util.GenerateRSAKeys(k)
  859. if err != nil {
  860. logger.Warn(logSender, "", "error creating host key %q: %v", k, err)
  861. logger.WarnToConsole("error creating host key %q: %v", k, err)
  862. return err
  863. }
  864. case defaultPrivateECDSAKeyName:
  865. logger.Info(logSender, "", "try to create non-existent host key %q", k)
  866. logger.InfoToConsole("try to create non-existent host key %q", k)
  867. err = util.GenerateECDSAKeys(k)
  868. if err != nil {
  869. logger.Warn(logSender, "", "error creating host key %q: %v", k, err)
  870. logger.WarnToConsole("error creating host key %q: %v", k, err)
  871. return err
  872. }
  873. case defaultPrivateEd25519KeyName:
  874. logger.Info(logSender, "", "try to create non-existent host key %q", k)
  875. logger.InfoToConsole("try to create non-existent host key %q", k)
  876. err = util.GenerateEd25519Keys(k)
  877. if err != nil {
  878. logger.Warn(logSender, "", "error creating host key %q: %v", k, err)
  879. logger.WarnToConsole("error creating host key %q: %v", k, err)
  880. return err
  881. }
  882. default:
  883. logger.Warn(logSender, "", "non-existent host key %q will not be created", k)
  884. logger.WarnToConsole("non-existent host key %q will not be created", k)
  885. }
  886. }
  887. }
  888. }
  889. if len(c.HostKeys) == 0 {
  890. if err := c.generateDefaultHostKeys(configDir); err != nil {
  891. return err
  892. }
  893. }
  894. return nil
  895. }
  896. func (c *Configuration) loadModuli(configDir string) {
  897. supportedKexAlgos = util.Remove(supportedKexAlgos, kexDHGroupExchangeSHA1)
  898. supportedKexAlgos = util.Remove(supportedKexAlgos, kexDHGroupExchangeSHA256)
  899. preferredKexAlgos = util.Remove(preferredKexAlgos, kexDHGroupExchangeSHA256)
  900. c.Moduli = util.RemoveDuplicates(c.Moduli, false)
  901. for _, m := range c.Moduli {
  902. m = strings.TrimSpace(m)
  903. if !util.IsFileInputValid(m) {
  904. logger.Warn(logSender, "", "unable to load invalid moduli file %q", m)
  905. logger.WarnToConsole("unable to load invalid host moduli file %q", m)
  906. continue
  907. }
  908. if !filepath.IsAbs(m) {
  909. m = filepath.Join(configDir, m)
  910. }
  911. logger.Info(logSender, "", "loading moduli file %q", m)
  912. if err := ssh.ParseModuli(m); err != nil {
  913. logger.Warn(logSender, "", "ignoring moduli file %q, error: %v", m, err)
  914. continue
  915. }
  916. if !util.Contains(supportedKexAlgos, kexDHGroupExchangeSHA1) {
  917. supportedKexAlgos = append(supportedKexAlgos, kexDHGroupExchangeSHA1)
  918. }
  919. if !util.Contains(supportedKexAlgos, kexDHGroupExchangeSHA256) {
  920. supportedKexAlgos = append(supportedKexAlgos, kexDHGroupExchangeSHA256)
  921. }
  922. if !util.Contains(preferredKexAlgos, kexDHGroupExchangeSHA256) {
  923. preferredKexAlgos = append(preferredKexAlgos, kexDHGroupExchangeSHA256)
  924. }
  925. }
  926. }
  927. func (c *Configuration) getHostKeyAlgorithms(keyFormat string) []string {
  928. var algos []string
  929. for _, algo := range algorithmsForKeyFormat(keyFormat) {
  930. if util.Contains(c.HostKeyAlgorithms, algo) {
  931. algos = append(algos, algo)
  932. }
  933. }
  934. return algos
  935. }
  936. // If no host keys are defined we try to use or generate the default ones.
  937. func (c *Configuration) checkAndLoadHostKeys(configDir string, serverConfig *ssh.ServerConfig) error {
  938. if err := c.checkHostKeyAutoGeneration(configDir); err != nil {
  939. return err
  940. }
  941. hostCertificates, err := c.loadHostCertificates(configDir)
  942. if err != nil {
  943. return err
  944. }
  945. serviceStatus.HostKeys = nil
  946. for _, hostKey := range c.HostKeys {
  947. hostKey = strings.TrimSpace(hostKey)
  948. if !util.IsFileInputValid(hostKey) {
  949. logger.Warn(logSender, "", "unable to load invalid host key %q", hostKey)
  950. logger.WarnToConsole("unable to load invalid host key %q", hostKey)
  951. continue
  952. }
  953. if !filepath.IsAbs(hostKey) {
  954. hostKey = filepath.Join(configDir, hostKey)
  955. }
  956. logger.Info(logSender, "", "Loading private host key %q", hostKey)
  957. privateBytes, err := os.ReadFile(hostKey)
  958. if err != nil {
  959. return err
  960. }
  961. private, err := ssh.ParsePrivateKey(privateBytes)
  962. if err != nil {
  963. return err
  964. }
  965. k := HostKey{
  966. Path: hostKey,
  967. Fingerprint: ssh.FingerprintSHA256(private.PublicKey()),
  968. Algorithms: c.getHostKeyAlgorithms(private.PublicKey().Type()),
  969. }
  970. mas, err := ssh.NewSignerWithAlgorithms(private.(ssh.AlgorithmSigner), k.Algorithms)
  971. if err != nil {
  972. return fmt.Errorf("could not create signer for key %q with algorithms %+v: %w", k.Path, k.Algorithms, err)
  973. }
  974. serviceStatus.HostKeys = append(serviceStatus.HostKeys, k)
  975. logger.Info(logSender, "", "Host key %q loaded, type %q, fingerprint %q, algorithms %+v", hostKey,
  976. private.PublicKey().Type(), k.Fingerprint, k.Algorithms)
  977. // Add private key to the server configuration.
  978. serverConfig.AddHostKey(mas)
  979. for _, cert := range hostCertificates {
  980. signer, err := ssh.NewCertSigner(cert.Certificate, mas)
  981. if err == nil {
  982. var algos []string
  983. for _, algo := range algorithmsForKeyFormat(signer.PublicKey().Type()) {
  984. if underlyingAlgo, ok := certKeyAlgoNames[algo]; ok {
  985. if util.Contains(mas.Algorithms(), underlyingAlgo) {
  986. algos = append(algos, algo)
  987. }
  988. }
  989. }
  990. serviceStatus.HostKeys = append(serviceStatus.HostKeys, HostKey{
  991. Path: cert.Path,
  992. Fingerprint: ssh.FingerprintSHA256(signer.PublicKey()),
  993. Algorithms: algos,
  994. })
  995. serverConfig.AddHostKey(signer)
  996. logger.Info(logSender, "", "Host certificate loaded for host key %q, fingerprint %q, algorithms %+v",
  997. hostKey, ssh.FingerprintSHA256(signer.PublicKey()), algos)
  998. }
  999. }
  1000. }
  1001. var fp []string
  1002. for idx := range serviceStatus.HostKeys {
  1003. h := &serviceStatus.HostKeys[idx]
  1004. fp = append(fp, h.Fingerprint)
  1005. }
  1006. vfs.SetSFTPFingerprints(fp)
  1007. return nil
  1008. }
  1009. func (c *Configuration) loadHostCertificates(configDir string) ([]hostCertificate, error) {
  1010. var certs []hostCertificate
  1011. for _, certPath := range c.HostCertificates {
  1012. certPath = strings.TrimSpace(certPath)
  1013. if !util.IsFileInputValid(certPath) {
  1014. logger.Warn(logSender, "", "unable to load invalid host certificate %q", certPath)
  1015. logger.WarnToConsole("unable to load invalid host certificate %q", certPath)
  1016. continue
  1017. }
  1018. if !filepath.IsAbs(certPath) {
  1019. certPath = filepath.Join(configDir, certPath)
  1020. }
  1021. certBytes, err := os.ReadFile(certPath)
  1022. if err != nil {
  1023. return certs, fmt.Errorf("unable to load host certificate %q: %w", certPath, err)
  1024. }
  1025. parsed, _, _, _, err := ssh.ParseAuthorizedKey(certBytes)
  1026. if err != nil {
  1027. return nil, fmt.Errorf("unable to parse host certificate %q: %w", certPath, err)
  1028. }
  1029. cert, ok := parsed.(*ssh.Certificate)
  1030. if !ok {
  1031. return nil, fmt.Errorf("the file %q is not an SSH certificate", certPath)
  1032. }
  1033. if cert.CertType != ssh.HostCert {
  1034. return nil, fmt.Errorf("the file %q is not an host certificate", certPath)
  1035. }
  1036. certs = append(certs, hostCertificate{
  1037. Path: certPath,
  1038. Certificate: cert,
  1039. })
  1040. }
  1041. return certs, nil
  1042. }
  1043. func (c *Configuration) initializeCertChecker(configDir string) error {
  1044. for _, keyPath := range c.TrustedUserCAKeys {
  1045. keyPath = strings.TrimSpace(keyPath)
  1046. if !util.IsFileInputValid(keyPath) {
  1047. logger.Warn(logSender, "", "unable to load invalid trusted user CA key %q", keyPath)
  1048. logger.WarnToConsole("unable to load invalid trusted user CA key %q", keyPath)
  1049. continue
  1050. }
  1051. if !filepath.IsAbs(keyPath) {
  1052. keyPath = filepath.Join(configDir, keyPath)
  1053. }
  1054. keyBytes, err := os.ReadFile(keyPath)
  1055. if err != nil {
  1056. logger.Warn(logSender, "", "error loading trusted user CA key %q: %v", keyPath, err)
  1057. logger.WarnToConsole("error loading trusted user CA key %q: %v", keyPath, err)
  1058. return err
  1059. }
  1060. parsedKey, _, _, _, err := ssh.ParseAuthorizedKey(keyBytes)
  1061. if err != nil {
  1062. logger.Warn(logSender, "", "error parsing trusted user CA key %q: %v", keyPath, err)
  1063. logger.WarnToConsole("error parsing trusted user CA key %q: %v", keyPath, err)
  1064. return err
  1065. }
  1066. c.parsedUserCAKeys = append(c.parsedUserCAKeys, parsedKey)
  1067. }
  1068. c.certChecker = &ssh.CertChecker{
  1069. SupportedCriticalOptions: []string{
  1070. sourceAddressCriticalOption,
  1071. },
  1072. IsUserAuthority: func(k ssh.PublicKey) bool {
  1073. for _, key := range c.parsedUserCAKeys {
  1074. if bytes.Equal(k.Marshal(), key.Marshal()) {
  1075. return true
  1076. }
  1077. }
  1078. return false
  1079. },
  1080. }
  1081. if c.RevokedUserCertsFile != "" {
  1082. if !util.IsFileInputValid(c.RevokedUserCertsFile) {
  1083. return fmt.Errorf("invalid revoked user certificate: %q", c.RevokedUserCertsFile)
  1084. }
  1085. if !filepath.IsAbs(c.RevokedUserCertsFile) {
  1086. c.RevokedUserCertsFile = filepath.Join(configDir, c.RevokedUserCertsFile)
  1087. }
  1088. }
  1089. revokedCertManager.filePath = c.RevokedUserCertsFile
  1090. return revokedCertManager.load()
  1091. }
  1092. func (c *Configuration) getPartialSuccessError(nextAuthMethods []string) error {
  1093. err := &ssh.PartialSuccessError{}
  1094. if c.PasswordAuthentication && util.Contains(nextAuthMethods, dataprovider.LoginMethodPassword) {
  1095. err.PasswordCallback = func(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) {
  1096. return c.validatePasswordCredentials(conn, password, dataprovider.SSHLoginMethodKeyAndPassword)
  1097. }
  1098. }
  1099. if c.KeyboardInteractiveAuthentication && util.Contains(nextAuthMethods, dataprovider.SSHLoginMethodKeyboardInteractive) {
  1100. err.KeyboardInteractiveCallback = func(conn ssh.ConnMetadata, client ssh.KeyboardInteractiveChallenge) (*ssh.Permissions, error) {
  1101. return c.validateKeyboardInteractiveCredentials(conn, client, dataprovider.SSHLoginMethodKeyAndKeyboardInt, true)
  1102. }
  1103. }
  1104. return err
  1105. }
  1106. func (c *Configuration) validatePublicKeyCredentials(conn ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
  1107. var err error
  1108. var user dataprovider.User
  1109. var keyID string
  1110. var sshPerm *ssh.Permissions
  1111. var certPerm *ssh.Permissions
  1112. connectionID := hex.EncodeToString(conn.SessionID())
  1113. method := dataprovider.SSHLoginMethodPublicKey
  1114. ipAddr := util.GetIPFromRemoteAddress(conn.RemoteAddr().String())
  1115. cert, ok := pubKey.(*ssh.Certificate)
  1116. var certFingerprint string
  1117. if ok {
  1118. certFingerprint = ssh.FingerprintSHA256(cert.Key)
  1119. if cert.CertType != ssh.UserCert {
  1120. err = fmt.Errorf("ssh: cert has type %d", cert.CertType)
  1121. user.Username = conn.User()
  1122. updateLoginMetrics(&user, ipAddr, method, err)
  1123. return nil, err
  1124. }
  1125. if !c.certChecker.IsUserAuthority(cert.SignatureKey) {
  1126. err = errors.New("ssh: certificate signed by unrecognized authority")
  1127. user.Username = conn.User()
  1128. updateLoginMetrics(&user, ipAddr, method, err)
  1129. return nil, err
  1130. }
  1131. if len(cert.ValidPrincipals) == 0 {
  1132. err = fmt.Errorf("ssh: certificate %s has no valid principals, user: \"%s\"", certFingerprint, conn.User())
  1133. user.Username = conn.User()
  1134. updateLoginMetrics(&user, ipAddr, method, err)
  1135. return nil, err
  1136. }
  1137. if revokedCertManager.isRevoked(certFingerprint) {
  1138. err = fmt.Errorf("ssh: certificate %s is revoked", certFingerprint)
  1139. user.Username = conn.User()
  1140. updateLoginMetrics(&user, ipAddr, method, err)
  1141. return nil, err
  1142. }
  1143. if err := c.certChecker.CheckCert(conn.User(), cert); err != nil {
  1144. user.Username = conn.User()
  1145. updateLoginMetrics(&user, ipAddr, method, err)
  1146. return nil, err
  1147. }
  1148. certPerm = &cert.Permissions
  1149. }
  1150. if user, keyID, err = dataprovider.CheckUserAndPubKey(conn.User(), pubKey.Marshal(), ipAddr, common.ProtocolSSH, ok); err == nil {
  1151. if ok {
  1152. keyID = fmt.Sprintf("%s: ID: %s, serial: %v, CA %s %s", certFingerprint,
  1153. cert.KeyId, cert.Serial, cert.Type(), ssh.FingerprintSHA256(cert.SignatureKey))
  1154. }
  1155. if user.IsPartialAuth() {
  1156. logger.Debug(logSender, connectionID, "user %q authenticated with partial success", conn.User())
  1157. return certPerm, c.getPartialSuccessError(user.GetNextAuthMethods())
  1158. }
  1159. sshPerm, err = loginUser(&user, method, keyID, conn)
  1160. if err == nil && certPerm != nil {
  1161. // if we have a SSH user cert we need to merge certificate permissions with our ones
  1162. // we only set Extensions, so CriticalOptions are always the ones from the certificate
  1163. sshPerm.CriticalOptions = certPerm.CriticalOptions
  1164. if certPerm.Extensions != nil {
  1165. for k, v := range certPerm.Extensions {
  1166. sshPerm.Extensions[k] = v
  1167. }
  1168. }
  1169. }
  1170. }
  1171. user.Username = conn.User()
  1172. updateLoginMetrics(&user, ipAddr, method, err)
  1173. return sshPerm, err
  1174. }
  1175. func (c *Configuration) validatePasswordCredentials(conn ssh.ConnMetadata, pass []byte, method string) (*ssh.Permissions, error) {
  1176. var err error
  1177. var user dataprovider.User
  1178. var sshPerm *ssh.Permissions
  1179. ipAddr := util.GetIPFromRemoteAddress(conn.RemoteAddr().String())
  1180. if user, err = dataprovider.CheckUserAndPass(conn.User(), string(pass), ipAddr, common.ProtocolSSH); err == nil {
  1181. sshPerm, err = loginUser(&user, method, "", conn)
  1182. }
  1183. user.Username = conn.User()
  1184. updateLoginMetrics(&user, ipAddr, method, err)
  1185. if err != nil {
  1186. return nil, newAuthenticationError(fmt.Errorf("could not validate password credentials: %w", err), method)
  1187. }
  1188. return sshPerm, nil
  1189. }
  1190. func (c *Configuration) validateKeyboardInteractiveCredentials(conn ssh.ConnMetadata, client ssh.KeyboardInteractiveChallenge,
  1191. method string, isPartialAuth bool,
  1192. ) (*ssh.Permissions, error) {
  1193. var err error
  1194. var user dataprovider.User
  1195. var sshPerm *ssh.Permissions
  1196. ipAddr := util.GetIPFromRemoteAddress(conn.RemoteAddr().String())
  1197. if user, err = dataprovider.CheckKeyboardInteractiveAuth(conn.User(), c.KeyboardInteractiveHook, client,
  1198. ipAddr, common.ProtocolSSH, isPartialAuth); err == nil {
  1199. sshPerm, err = loginUser(&user, method, "", conn)
  1200. }
  1201. user.Username = conn.User()
  1202. updateLoginMetrics(&user, ipAddr, method, err)
  1203. if err != nil {
  1204. return nil, newAuthenticationError(fmt.Errorf("could not validate keyboard interactive credentials: %w", err), method)
  1205. }
  1206. return sshPerm, nil
  1207. }
  1208. func updateLoginMetrics(user *dataprovider.User, ip, method string, err error) {
  1209. metric.AddLoginAttempt(method)
  1210. if err != nil {
  1211. logger.ConnectionFailedLog(user.Username, ip, method, common.ProtocolSSH, err.Error())
  1212. if method != dataprovider.SSHLoginMethodPublicKey {
  1213. // some clients try all available public keys for a user, we
  1214. // record failed login key auth only once for session if the
  1215. // authentication fails in checkAuthError
  1216. event := common.HostEventLoginFailed
  1217. logEv := notifier.LogEventTypeLoginFailed
  1218. if errors.Is(err, util.ErrNotFound) {
  1219. event = common.HostEventUserNotFound
  1220. logEv = notifier.LogEventTypeLoginNoUser
  1221. }
  1222. common.AddDefenderEvent(ip, common.ProtocolSSH, event)
  1223. plugin.Handler.NotifyLogEvent(logEv, common.ProtocolSSH, user.Username, ip, "", err)
  1224. }
  1225. }
  1226. metric.AddLoginResult(method, err)
  1227. dataprovider.ExecutePostLoginHook(user, method, ip, common.ProtocolSSH, err)
  1228. }
  1229. type revokedCertificates struct {
  1230. filePath string
  1231. mu sync.RWMutex
  1232. certs map[string]bool
  1233. }
  1234. func (r *revokedCertificates) load() error {
  1235. if r.filePath == "" {
  1236. return nil
  1237. }
  1238. logger.Debug(logSender, "", "loading revoked user certificate file %q", r.filePath)
  1239. info, err := os.Stat(r.filePath)
  1240. if err != nil {
  1241. return fmt.Errorf("unable to load revoked user certificate file %q: %w", r.filePath, err)
  1242. }
  1243. maxSize := int64(1048576 * 5) // 5MB
  1244. if info.Size() > maxSize {
  1245. return fmt.Errorf("unable to load revoked user certificate file %q size too big: %v/%v bytes",
  1246. r.filePath, info.Size(), maxSize)
  1247. }
  1248. content, err := os.ReadFile(r.filePath)
  1249. if err != nil {
  1250. return fmt.Errorf("unable to read revoked user certificate file %q: %w", r.filePath, err)
  1251. }
  1252. var certs []string
  1253. err = json.Unmarshal(content, &certs)
  1254. if err != nil {
  1255. return fmt.Errorf("unable to parse revoked user certificate file %q: %w", r.filePath, err)
  1256. }
  1257. r.mu.Lock()
  1258. defer r.mu.Unlock()
  1259. r.certs = map[string]bool{}
  1260. for _, fp := range certs {
  1261. r.certs[fp] = true
  1262. }
  1263. logger.Debug(logSender, "", "revoked user certificate file %q loaded, entries: %v", r.filePath, len(r.certs))
  1264. return nil
  1265. }
  1266. func (r *revokedCertificates) isRevoked(fp string) bool {
  1267. r.mu.RLock()
  1268. defer r.mu.RUnlock()
  1269. return r.certs[fp]
  1270. }
  1271. // Reload reloads the list of revoked user certificates
  1272. func Reload() error {
  1273. return revokedCertManager.load()
  1274. }
  1275. func algorithmsForKeyFormat(keyFormat string) []string {
  1276. switch keyFormat {
  1277. case ssh.KeyAlgoRSA:
  1278. return []string{ssh.KeyAlgoRSASHA256, ssh.KeyAlgoRSASHA512, ssh.KeyAlgoRSA}
  1279. case ssh.CertAlgoRSAv01:
  1280. return []string{ssh.CertAlgoRSASHA256v01, ssh.CertAlgoRSASHA512v01, ssh.CertAlgoRSAv01}
  1281. default:
  1282. return []string{keyFormat}
  1283. }
  1284. }