sftpserver.go 4.8 KB

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