sftpserver.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package sftpserver
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "net"
  9. "github.com/pkg/sftp"
  10. "golang.org/x/crypto/ssh"
  11. user "imuslab.com/arozos/mod/user"
  12. )
  13. type SFTPConfig struct {
  14. ListeningIP string
  15. KeyFile string
  16. ReadOnly bool
  17. UserManager *user.UserHandler
  18. }
  19. type Instance struct {
  20. }
  21. //Create a new SFTP Server
  22. //listeningIP in the format of 0.0.0.0:2022
  23. func NewSFTPServer(sftpConfig *SFTPConfig) (*Instance, error) {
  24. // An SSH server is represented by a ServerConfig, which holds
  25. // certificate details and handles authentication of ServerConns.
  26. config := &ssh.ServerConfig{
  27. PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
  28. // Should use constant-time compare (or better, salt+hash) in
  29. // a production setting.
  30. fmt.Printf("Login: %s\n", c.User())
  31. ok := sftpConfig.UserManager.GetAuthAgent().ValidateUsernameAndPassword(c.User(), string(pass))
  32. if !ok {
  33. return nil, errors.New("password rejected for " + c.User())
  34. }
  35. return nil, nil
  36. },
  37. }
  38. privateBytes, err := ioutil.ReadFile(sftpConfig.KeyFile)
  39. if err != nil {
  40. return nil, err
  41. }
  42. private, err := ssh.ParsePrivateKey(privateBytes)
  43. if err != nil {
  44. return nil, err
  45. }
  46. config.AddHostKey(private)
  47. // Once a ServerConfig has been configured, connections can be accepted.
  48. listener, err := net.Listen("tcp", sftpConfig.ListeningIP)
  49. if err != nil {
  50. return nil, err
  51. }
  52. fmt.Printf("Listening on %v\n", listener.Addr())
  53. //Start the ssh server listener in go routine
  54. go func() error {
  55. for {
  56. nConn, err := listener.Accept()
  57. if err != nil {
  58. return err
  59. }
  60. go func(nConn net.Conn) error {
  61. // Before use, a handshake must be performed on the incoming
  62. // net.Conn.
  63. cx, chans, reqs, err := ssh.NewServerConn(nConn, config)
  64. if err != nil {
  65. return err
  66. }
  67. fmt.Println("SSH server established\n")
  68. fmt.Println("Connected username", cx.User())
  69. userinfo, err := sftpConfig.UserManager.GetUserInfoFromUsername(cx.User())
  70. if err != nil {
  71. return err
  72. }
  73. // The incoming Request channel must be serviced.
  74. go ssh.DiscardRequests(reqs)
  75. // Service the incoming Channel channel.
  76. for newChannel := range chans {
  77. // Channels have a type, depending on the application level
  78. // protocol intended. In the case of an SFTP session, this is "subsystem"
  79. // with a payload string of "<length=4>sftp"
  80. fmt.Println("Incoming channel: %s\n", newChannel.ChannelType())
  81. if newChannel.ChannelType() != "session" {
  82. newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
  83. fmt.Println("Unknown channel type: %s\n", newChannel.ChannelType())
  84. continue
  85. }
  86. channel, requests, err := newChannel.Accept()
  87. if err != nil {
  88. return err
  89. }
  90. fmt.Println("Channel accepted\n")
  91. // Sessions have out-of-band requests such as "shell",
  92. // "pty-req" and "env". Here we handle only the
  93. // "subsystem" request.
  94. go func(in <-chan *ssh.Request) {
  95. for req := range in {
  96. fmt.Println("Request: %v\n", req.Type)
  97. ok := false
  98. switch req.Type {
  99. case "subsystem":
  100. fmt.Println("Subsystem: %s\n", req.Payload[4:])
  101. if string(req.Payload[4:]) == "sftp" {
  102. ok = true
  103. }
  104. }
  105. fmt.Println(" - accepted: %v\n", ok)
  106. req.Reply(ok, nil)
  107. }
  108. }(requests)
  109. serverOptions := []sftp.ServerOption{}
  110. if sftpConfig.ReadOnly {
  111. serverOptions = append(serverOptions, sftp.ReadOnly())
  112. fmt.Println("Read-only server\n")
  113. } else {
  114. fmt.Println("Read write server\n")
  115. }
  116. /*
  117. server, err := sftp.NewServer(
  118. channel,
  119. serverOptions...,
  120. )
  121. if err != nil {
  122. return err
  123. }
  124. */
  125. //Create a virtual SSH Server that contains all this user's fsh
  126. root := GetNewSFTPRoot(userinfo.Username, userinfo.GetAllFileSystemHandler())
  127. server := sftp.NewRequestServer(channel, root)
  128. if err := server.Serve(); err == io.EOF {
  129. server.Close()
  130. log.Print("sftp client exited session.")
  131. } else if err != nil {
  132. log.Fatal("sftp server completed with error:", err)
  133. }
  134. }
  135. return nil
  136. }(nConn)
  137. }
  138. }()
  139. return &Instance{}, nil
  140. }