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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package ssh
  4. import (
  5. "bytes"
  6. "context"
  7. "crypto/rand"
  8. "crypto/rsa"
  9. "crypto/x509"
  10. "encoding/pem"
  11. "errors"
  12. "fmt"
  13. "io"
  14. "net"
  15. "os"
  16. "os/exec"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "syscall"
  22. asymkey_model "code.gitea.io/gitea/models/asymkey"
  23. "code.gitea.io/gitea/modules/graceful"
  24. "code.gitea.io/gitea/modules/log"
  25. "code.gitea.io/gitea/modules/process"
  26. "code.gitea.io/gitea/modules/setting"
  27. "code.gitea.io/gitea/modules/util"
  28. "github.com/gliderlabs/ssh"
  29. gossh "golang.org/x/crypto/ssh"
  30. )
  31. type contextKey string
  32. const giteaKeyID = contextKey("gitea-key-id")
  33. func getExitStatusFromError(err error) int {
  34. if err == nil {
  35. return 0
  36. }
  37. exitErr, ok := err.(*exec.ExitError)
  38. if !ok {
  39. return 1
  40. }
  41. waitStatus, ok := exitErr.Sys().(syscall.WaitStatus)
  42. if !ok {
  43. // This is a fallback and should at least let us return something useful
  44. // when running on Windows, even if it isn't completely accurate.
  45. if exitErr.Success() {
  46. return 0
  47. }
  48. return 1
  49. }
  50. return waitStatus.ExitStatus()
  51. }
  52. func sessionHandler(session ssh.Session) {
  53. keyID := fmt.Sprintf("%d", session.Context().Value(giteaKeyID).(int64))
  54. command := session.RawCommand()
  55. log.Trace("SSH: Payload: %v", command)
  56. args := []string{"--config=" + setting.CustomConf, "serv", "key-" + keyID}
  57. log.Trace("SSH: Arguments: %v", args)
  58. ctx, cancel := context.WithCancel(session.Context())
  59. defer cancel()
  60. gitProtocol := ""
  61. for _, env := range session.Environ() {
  62. if strings.HasPrefix(env, "GIT_PROTOCOL=") {
  63. _, gitProtocol, _ = strings.Cut(env, "=")
  64. break
  65. }
  66. }
  67. cmd := exec.CommandContext(ctx, setting.AppPath, args...)
  68. cmd.Env = append(
  69. os.Environ(),
  70. "SSH_ORIGINAL_COMMAND="+command,
  71. "SKIP_MINWINSVC=1",
  72. "GIT_PROTOCOL="+gitProtocol,
  73. )
  74. stdout, err := cmd.StdoutPipe()
  75. if err != nil {
  76. log.Error("SSH: StdoutPipe: %v", err)
  77. return
  78. }
  79. defer stdout.Close()
  80. stderr, err := cmd.StderrPipe()
  81. if err != nil {
  82. log.Error("SSH: StderrPipe: %v", err)
  83. return
  84. }
  85. defer stderr.Close()
  86. stdin, err := cmd.StdinPipe()
  87. if err != nil {
  88. log.Error("SSH: StdinPipe: %v", err)
  89. return
  90. }
  91. defer stdin.Close()
  92. process.SetSysProcAttribute(cmd)
  93. wg := &sync.WaitGroup{}
  94. wg.Add(2)
  95. if err = cmd.Start(); err != nil {
  96. log.Error("SSH: Start: %v", err)
  97. return
  98. }
  99. go func() {
  100. defer stdin.Close()
  101. if _, err := io.Copy(stdin, session); err != nil {
  102. log.Error("Failed to write session to stdin. %s", err)
  103. }
  104. }()
  105. go func() {
  106. defer wg.Done()
  107. defer stdout.Close()
  108. if _, err := io.Copy(session, stdout); err != nil {
  109. log.Error("Failed to write stdout to session. %s", err)
  110. }
  111. }()
  112. go func() {
  113. defer wg.Done()
  114. defer stderr.Close()
  115. if _, err := io.Copy(session.Stderr(), stderr); err != nil {
  116. log.Error("Failed to write stderr to session. %s", err)
  117. }
  118. }()
  119. // Ensure all the output has been written before we wait on the command
  120. // to exit.
  121. wg.Wait()
  122. // Wait for the command to exit and log any errors we get
  123. err = cmd.Wait()
  124. if err != nil {
  125. // Cannot use errors.Is here because ExitError doesn't implement Is
  126. // Thus errors.Is will do equality test NOT type comparison
  127. if _, ok := err.(*exec.ExitError); !ok {
  128. log.Error("SSH: Wait: %v", err)
  129. }
  130. }
  131. if err := session.Exit(getExitStatusFromError(err)); err != nil && !errors.Is(err, io.EOF) {
  132. log.Error("Session failed to exit. %s", err)
  133. }
  134. }
  135. func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
  136. if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
  137. log.Debug("Handle Public Key: Fingerprint: %s from %s", gossh.FingerprintSHA256(key), ctx.RemoteAddr())
  138. }
  139. if ctx.User() != setting.SSH.BuiltinServerUser {
  140. log.Warn("Invalid SSH username %s - must use %s for all git operations via ssh", ctx.User(), setting.SSH.BuiltinServerUser)
  141. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  142. return false
  143. }
  144. // check if we have a certificate
  145. if cert, ok := key.(*gossh.Certificate); ok {
  146. if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
  147. log.Debug("Handle Certificate: %s Fingerprint: %s is a certificate", ctx.RemoteAddr(), gossh.FingerprintSHA256(key))
  148. }
  149. if len(setting.SSH.TrustedUserCAKeys) == 0 {
  150. log.Warn("Certificate Rejected: No trusted certificate authorities for this server")
  151. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  152. return false
  153. }
  154. if cert.CertType != gossh.UserCert {
  155. log.Warn("Certificate Rejected: Not a user certificate")
  156. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  157. return false
  158. }
  159. // look for the exact principal
  160. principalLoop:
  161. for _, principal := range cert.ValidPrincipals {
  162. pkey, err := asymkey_model.SearchPublicKeyByContentExact(ctx, principal)
  163. if err != nil {
  164. if asymkey_model.IsErrKeyNotExist(err) {
  165. log.Debug("Principal Rejected: %s Unknown Principal: %s", ctx.RemoteAddr(), principal)
  166. continue principalLoop
  167. }
  168. log.Error("SearchPublicKeyByContentExact: %v", err)
  169. return false
  170. }
  171. c := &gossh.CertChecker{
  172. IsUserAuthority: func(auth gossh.PublicKey) bool {
  173. marshaled := auth.Marshal()
  174. for _, k := range setting.SSH.TrustedUserCAKeysParsed {
  175. if bytes.Equal(marshaled, k.Marshal()) {
  176. return true
  177. }
  178. }
  179. return false
  180. },
  181. }
  182. // check the CA of the cert
  183. if !c.IsUserAuthority(cert.SignatureKey) {
  184. if log.IsDebug() {
  185. log.Debug("Principal Rejected: %s Untrusted Authority Signature Fingerprint %s for Principal: %s", ctx.RemoteAddr(), gossh.FingerprintSHA256(cert.SignatureKey), principal)
  186. }
  187. continue principalLoop
  188. }
  189. // validate the cert for this principal
  190. if err := c.CheckCert(principal, cert); err != nil {
  191. // User is presenting an invalid certificate - STOP any further processing
  192. log.Error("Invalid Certificate KeyID %s with Signature Fingerprint %s presented for Principal: %s from %s", cert.KeyId, gossh.FingerprintSHA256(cert.SignatureKey), principal, ctx.RemoteAddr())
  193. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  194. return false
  195. }
  196. if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
  197. log.Debug("Successfully authenticated: %s Certificate Fingerprint: %s Principal: %s", ctx.RemoteAddr(), gossh.FingerprintSHA256(key), principal)
  198. }
  199. ctx.SetValue(giteaKeyID, pkey.ID)
  200. return true
  201. }
  202. log.Warn("From %s Fingerprint: %s is a certificate, but no valid principals found", ctx.RemoteAddr(), gossh.FingerprintSHA256(key))
  203. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  204. return false
  205. }
  206. if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
  207. log.Debug("Handle Public Key: %s Fingerprint: %s is not a certificate", ctx.RemoteAddr(), gossh.FingerprintSHA256(key))
  208. }
  209. pkey, err := asymkey_model.SearchPublicKeyByContent(ctx, strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key))))
  210. if err != nil {
  211. if asymkey_model.IsErrKeyNotExist(err) {
  212. log.Warn("Unknown public key: %s from %s", gossh.FingerprintSHA256(key), ctx.RemoteAddr())
  213. log.Warn("Failed authentication attempt from %s", ctx.RemoteAddr())
  214. return false
  215. }
  216. log.Error("SearchPublicKeyByContent: %v", err)
  217. return false
  218. }
  219. if log.IsDebug() { // <- FingerprintSHA256 is kinda expensive so only calculate it if necessary
  220. log.Debug("Successfully authenticated: %s Public Key Fingerprint: %s", ctx.RemoteAddr(), gossh.FingerprintSHA256(key))
  221. }
  222. ctx.SetValue(giteaKeyID, pkey.ID)
  223. return true
  224. }
  225. // sshConnectionFailed logs a failed connection
  226. // - this mainly exists to give a nice function name in logging
  227. func sshConnectionFailed(conn net.Conn, err error) {
  228. // Log the underlying error with a specific message
  229. log.Warn("Failed connection from %s with error: %v", conn.RemoteAddr(), err)
  230. // Log with the standard failed authentication from message for simpler fail2ban configuration
  231. log.Warn("Failed authentication attempt from %s", conn.RemoteAddr())
  232. }
  233. // Listen starts a SSH server listens on given port.
  234. func Listen(host string, port int, ciphers, keyExchanges, macs []string) {
  235. srv := ssh.Server{
  236. Addr: net.JoinHostPort(host, strconv.Itoa(port)),
  237. PublicKeyHandler: publicKeyHandler,
  238. Handler: sessionHandler,
  239. ServerConfigCallback: func(ctx ssh.Context) *gossh.ServerConfig {
  240. config := &gossh.ServerConfig{}
  241. config.KeyExchanges = keyExchanges
  242. config.MACs = macs
  243. config.Ciphers = ciphers
  244. return config
  245. },
  246. ConnectionFailedCallback: sshConnectionFailed,
  247. // We need to explicitly disable the PtyCallback so text displays
  248. // properly.
  249. PtyCallback: func(ctx ssh.Context, pty ssh.Pty) bool {
  250. return false
  251. },
  252. }
  253. keys := make([]string, 0, len(setting.SSH.ServerHostKeys))
  254. for _, key := range setting.SSH.ServerHostKeys {
  255. isExist, err := util.IsExist(key)
  256. if err != nil {
  257. log.Fatal("Unable to check if %s exists. Error: %v", setting.SSH.ServerHostKeys, err)
  258. }
  259. if isExist {
  260. keys = append(keys, key)
  261. }
  262. }
  263. if len(keys) == 0 {
  264. filePath := filepath.Dir(setting.SSH.ServerHostKeys[0])
  265. if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
  266. log.Error("Failed to create dir %s: %v", filePath, err)
  267. }
  268. err := GenKeyPair(setting.SSH.ServerHostKeys[0])
  269. if err != nil {
  270. log.Fatal("Failed to generate private key: %v", err)
  271. }
  272. log.Trace("New private key is generated: %s", setting.SSH.ServerHostKeys[0])
  273. keys = append(keys, setting.SSH.ServerHostKeys[0])
  274. }
  275. for _, key := range keys {
  276. log.Info("Adding SSH host key: %s", key)
  277. err := srv.SetOption(ssh.HostKeyFile(key))
  278. if err != nil {
  279. log.Error("Failed to set Host Key. %s", err)
  280. }
  281. }
  282. go func() {
  283. _, _, finished := process.GetManager().AddTypedContext(graceful.GetManager().HammerContext(), "Service: Built-in SSH server", process.SystemProcessType, true)
  284. defer finished()
  285. listen(&srv)
  286. }()
  287. }
  288. // GenKeyPair make a pair of public and private keys for SSH access.
  289. // Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
  290. // Private Key generated is PEM encoded
  291. func GenKeyPair(keyPath string) error {
  292. privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
  293. if err != nil {
  294. return err
  295. }
  296. privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
  297. f, err := os.OpenFile(keyPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
  298. if err != nil {
  299. return err
  300. }
  301. defer func() {
  302. if err = f.Close(); err != nil {
  303. log.Error("Close: %v", err)
  304. }
  305. }()
  306. if err := pem.Encode(f, privateKeyPEM); err != nil {
  307. return err
  308. }
  309. // generate public key
  310. pub, err := gossh.NewPublicKey(&privateKey.PublicKey)
  311. if err != nil {
  312. return err
  313. }
  314. public := gossh.MarshalAuthorizedKey(pub)
  315. p, err := os.OpenFile(keyPath+".pub", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o600)
  316. if err != nil {
  317. return err
  318. }
  319. defer func() {
  320. if err = p.Close(); err != nil {
  321. log.Error("Close: %v", err)
  322. }
  323. }()
  324. _, err = p.Write(public)
  325. return err
  326. }