You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ssh.go 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package ssh
  5. import (
  6. "crypto/rand"
  7. "crypto/rsa"
  8. "crypto/x509"
  9. "encoding/pem"
  10. "fmt"
  11. "io"
  12. "os"
  13. "os/exec"
  14. "path/filepath"
  15. "strings"
  16. "sync"
  17. "syscall"
  18. "code.gitea.io/gitea/models"
  19. "code.gitea.io/gitea/modules/log"
  20. "code.gitea.io/gitea/modules/setting"
  21. "github.com/gliderlabs/ssh"
  22. "github.com/unknwon/com"
  23. gossh "golang.org/x/crypto/ssh"
  24. )
  25. type contextKey string
  26. const giteaKeyID = contextKey("gitea-key-id")
  27. func getExitStatusFromError(err error) int {
  28. if err == nil {
  29. return 0
  30. }
  31. exitErr, ok := err.(*exec.ExitError)
  32. if !ok {
  33. return 1
  34. }
  35. waitStatus, ok := exitErr.Sys().(syscall.WaitStatus)
  36. if !ok {
  37. // This is a fallback and should at least let us return something useful
  38. // when running on Windows, even if it isn't completely accurate.
  39. if exitErr.Success() {
  40. return 0
  41. }
  42. return 1
  43. }
  44. return waitStatus.ExitStatus()
  45. }
  46. func sessionHandler(session ssh.Session) {
  47. keyID := session.Context().Value(giteaKeyID).(int64)
  48. command := session.RawCommand()
  49. log.Trace("SSH: Payload: %v", command)
  50. args := []string{"serv", "key-" + com.ToStr(keyID), "--config=" + setting.CustomConf}
  51. log.Trace("SSH: Arguments: %v", args)
  52. cmd := exec.Command(setting.AppPath, args...)
  53. cmd.Env = append(
  54. os.Environ(),
  55. "SSH_ORIGINAL_COMMAND="+command,
  56. "SKIP_MINWINSVC=1",
  57. )
  58. stdout, err := cmd.StdoutPipe()
  59. if err != nil {
  60. log.Error("SSH: StdoutPipe: %v", err)
  61. return
  62. }
  63. stderr, err := cmd.StderrPipe()
  64. if err != nil {
  65. log.Error("SSH: StderrPipe: %v", err)
  66. return
  67. }
  68. stdin, err := cmd.StdinPipe()
  69. if err != nil {
  70. log.Error("SSH: StdinPipe: %v", err)
  71. return
  72. }
  73. wg := &sync.WaitGroup{}
  74. wg.Add(2)
  75. if err = cmd.Start(); err != nil {
  76. log.Error("SSH: Start: %v", err)
  77. return
  78. }
  79. go func() {
  80. defer stdin.Close()
  81. if _, err := io.Copy(stdin, session); err != nil {
  82. log.Error("Failed to write session to stdin. %s", err)
  83. }
  84. }()
  85. go func() {
  86. defer wg.Done()
  87. if _, err := io.Copy(session, stdout); err != nil {
  88. log.Error("Failed to write stdout to session. %s", err)
  89. }
  90. }()
  91. go func() {
  92. defer wg.Done()
  93. if _, err := io.Copy(session.Stderr(), stderr); err != nil {
  94. log.Error("Failed to write stderr to session. %s", err)
  95. }
  96. }()
  97. // Ensure all the output has been written before we wait on the command
  98. // to exit.
  99. wg.Wait()
  100. // Wait for the command to exit and log any errors we get
  101. err = cmd.Wait()
  102. if err != nil {
  103. log.Error("SSH: Wait: %v", err)
  104. }
  105. if err := session.Exit(getExitStatusFromError(err)); err != nil {
  106. log.Error("Session failed to exit. %s", err)
  107. }
  108. }
  109. func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
  110. if ctx.User() != setting.SSH.BuiltinServerUser {
  111. return false
  112. }
  113. pkey, err := models.SearchPublicKeyByContent(strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key))))
  114. if err != nil {
  115. log.Error("SearchPublicKeyByContent: %v", err)
  116. return false
  117. }
  118. ctx.SetValue(giteaKeyID, pkey.ID)
  119. return true
  120. }
  121. // Listen starts a SSH server listens on given port.
  122. func Listen(host string, port int, ciphers []string, keyExchanges []string, macs []string) {
  123. // TODO: Handle ciphers, keyExchanges, and macs
  124. srv := ssh.Server{
  125. Addr: fmt.Sprintf("%s:%d", host, port),
  126. PublicKeyHandler: publicKeyHandler,
  127. Handler: sessionHandler,
  128. // We need to explicitly disable the PtyCallback so text displays
  129. // properly.
  130. PtyCallback: func(ctx ssh.Context, pty ssh.Pty) bool {
  131. return false
  132. },
  133. }
  134. keyPath := filepath.Join(setting.AppDataPath, "ssh/gogs.rsa")
  135. if !com.IsExist(keyPath) {
  136. filePath := filepath.Dir(keyPath)
  137. if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
  138. log.Error("Failed to create dir %s: %v", filePath, err)
  139. }
  140. err := GenKeyPair(keyPath)
  141. if err != nil {
  142. log.Fatal("Failed to generate private key: %v", err)
  143. }
  144. log.Trace("New private key is generated: %s", keyPath)
  145. }
  146. err := srv.SetOption(ssh.HostKeyFile(keyPath))
  147. if err != nil {
  148. log.Error("Failed to set Host Key. %s", err)
  149. }
  150. go func() {
  151. err := srv.ListenAndServe()
  152. if err != nil {
  153. log.Error("Failed to serve with builtin SSH server. %s", err)
  154. }
  155. }()
  156. }
  157. // GenKeyPair make a pair of public and private keys for SSH access.
  158. // Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
  159. // Private Key generated is PEM encoded
  160. func GenKeyPair(keyPath string) error {
  161. privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  162. if err != nil {
  163. return err
  164. }
  165. privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
  166. f, err := os.OpenFile(keyPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  167. if err != nil {
  168. return err
  169. }
  170. defer func() {
  171. if err = f.Close(); err != nil {
  172. log.Error("Close: %v", err)
  173. }
  174. }()
  175. if err := pem.Encode(f, privateKeyPEM); err != nil {
  176. return err
  177. }
  178. // generate public key
  179. pub, err := gossh.NewPublicKey(&privateKey.PublicKey)
  180. if err != nil {
  181. return err
  182. }
  183. public := gossh.MarshalAuthorizedKey(pub)
  184. p, err := os.OpenFile(keyPath+".pub", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  185. if err != nil {
  186. return err
  187. }
  188. defer func() {
  189. if err = p.Close(); err != nil {
  190. log.Error("Close: %v", err)
  191. }
  192. }()
  193. _, err = p.Write(public)
  194. return err
  195. }