sftpd.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (C) 2019-2022 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 implements the SSH File Transfer Protocol as described in https://tools.ietf.org/html/draft-ietf-secsh-filexfer-02.
  15. // It uses pkg/sftp library:
  16. // https://github.com/pkg/sftp
  17. package sftpd
  18. import (
  19. "strings"
  20. "time"
  21. )
  22. const (
  23. logSender = "sftpd"
  24. handshakeTimeout = 2 * time.Minute
  25. )
  26. var (
  27. supportedSSHCommands = []string{"scp", "md5sum", "sha1sum", "sha256sum", "sha384sum", "sha512sum", "cd", "pwd",
  28. "git-receive-pack", "git-upload-pack", "git-upload-archive", "rsync", "sftpgo-copy", "sftpgo-remove"}
  29. defaultSSHCommands = []string{"md5sum", "sha1sum", "sha256sum", "cd", "pwd", "scp"}
  30. sshHashCommands = []string{"md5sum", "sha1sum", "sha256sum", "sha384sum", "sha512sum"}
  31. systemCommands = []string{"git-receive-pack", "git-upload-pack", "git-upload-archive", "rsync"}
  32. serviceStatus ServiceStatus
  33. )
  34. type sshSubsystemExitStatus struct {
  35. Status uint32
  36. }
  37. type sshSubsystemExecMsg struct {
  38. Command string
  39. }
  40. // HostKey defines the details for a used host key
  41. type HostKey struct {
  42. Path string `json:"path"`
  43. Fingerprint string `json:"fingerprint"`
  44. }
  45. // ServiceStatus defines the service status
  46. type ServiceStatus struct {
  47. IsActive bool `json:"is_active"`
  48. Bindings []Binding `json:"bindings"`
  49. SSHCommands []string `json:"ssh_commands"`
  50. HostKeys []HostKey `json:"host_keys"`
  51. Authentications []string `json:"authentications"`
  52. }
  53. // GetSSHCommandsAsString returns enabled SSH commands as comma separated string
  54. func (s *ServiceStatus) GetSSHCommandsAsString() string {
  55. return strings.Join(s.SSHCommands, ", ")
  56. }
  57. // GetSupportedAuthsAsString returns the supported authentications as comma separated string
  58. func (s *ServiceStatus) GetSupportedAuthsAsString() string {
  59. return strings.Join(s.Authentications, ", ")
  60. }
  61. // GetStatus returns the server status
  62. func GetStatus() ServiceStatus {
  63. return serviceStatus
  64. }
  65. // GetDefaultSSHCommands returns the SSH commands enabled as default
  66. func GetDefaultSSHCommands() []string {
  67. result := make([]string, len(defaultSSHCommands))
  68. copy(result, defaultSSHCommands)
  69. return result
  70. }
  71. // GetSupportedSSHCommands returns the supported SSH commands
  72. func GetSupportedSSHCommands() []string {
  73. result := make([]string, len(supportedSSHCommands))
  74. copy(result, supportedSSHCommands)
  75. return result
  76. }