本站源代码
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.

233 lines
5.1KB

  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 listen(&srv)
  151. }
  152. // GenKeyPair make a pair of public and private keys for SSH access.
  153. // Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
  154. // Private Key generated is PEM encoded
  155. func GenKeyPair(keyPath string) error {
  156. privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  157. if err != nil {
  158. return err
  159. }
  160. privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
  161. f, err := os.OpenFile(keyPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  162. if err != nil {
  163. return err
  164. }
  165. defer func() {
  166. if err = f.Close(); err != nil {
  167. log.Error("Close: %v", err)
  168. }
  169. }()
  170. if err := pem.Encode(f, privateKeyPEM); err != nil {
  171. return err
  172. }
  173. // generate public key
  174. pub, err := gossh.NewPublicKey(&privateKey.PublicKey)
  175. if err != nil {
  176. return err
  177. }
  178. public := gossh.MarshalAuthorizedKey(pub)
  179. p, err := os.OpenFile(keyPath+".pub", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  180. if err != nil {
  181. return err
  182. }
  183. defer func() {
  184. if err = p.Close(); err != nil {
  185. log.Error("Close: %v", err)
  186. }
  187. }()
  188. _, err = p.Write(public)
  189. return err
  190. }
上海开阖软件有限公司 沪ICP备12045867号-1