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

ssh.go 6.3 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. "code.gitea.io/gitea/services/repository"
  7. "crypto/rand"
  8. "crypto/rsa"
  9. "crypto/x509"
  10. "encoding/pem"
  11. "fmt"
  12. "io"
  13. "os"
  14. "os/exec"
  15. "path/filepath"
  16. "strings"
  17. "sync"
  18. "syscall"
  19. "code.gitea.io/gitea/models"
  20. "code.gitea.io/gitea/modules/log"
  21. "code.gitea.io/gitea/modules/setting"
  22. "github.com/gliderlabs/ssh"
  23. "github.com/unknwon/com"
  24. gossh "golang.org/x/crypto/ssh"
  25. )
  26. type contextKey string
  27. const giteaKeyID = contextKey("gitea-key-id")
  28. func getExitStatusFromError(err error) int {
  29. if err == nil {
  30. return 0
  31. }
  32. exitErr, ok := err.(*exec.ExitError)
  33. if !ok {
  34. return 1
  35. }
  36. waitStatus, ok := exitErr.Sys().(syscall.WaitStatus)
  37. if !ok {
  38. // This is a fallback and should at least let us return something useful
  39. // when running on Windows, even if it isn't completely accurate.
  40. if exitErr.Success() {
  41. return 0
  42. }
  43. return 1
  44. }
  45. return waitStatus.ExitStatus()
  46. }
  47. func sessionHandler(session ssh.Session) {
  48. keyID := session.Context().Value(giteaKeyID).(int64)
  49. command := session.RawCommand()
  50. log.Trace("SSH: Payload: %v", command)
  51. args := []string{"serv", "key-" + com.ToStr(keyID), "--config=" + setting.CustomConf}
  52. log.Trace("SSH: Arguments: %v", args)
  53. cmd := exec.Command(setting.AppPath, args...)
  54. cmd.Env = append(
  55. os.Environ(),
  56. "SSH_ORIGINAL_COMMAND="+command,
  57. "SKIP_MINWINSVC=1",
  58. models.EnvRepoMaxFileSize+"="+fmt.Sprint(setting.Repository.Upload.FileMaxSize),
  59. models.EnvRepoMaxSize+"="+fmt.Sprint(setting.Repository.RepoMaxSize),
  60. models.EnvPushSizeCheckFlag+"="+fmt.Sprint(setting.Repository.Upload.ShellFlag),
  61. )
  62. if strings.HasPrefix(command, "git-receive-pack") && setting.Repository.Upload.ShellFlag == repository.SHELL_FLAG_ON {
  63. repo := getRepoFromCommandStr(command)
  64. if repo != nil {
  65. cmd.Env = append(cmd.Env, models.EnvRepoSize+"="+fmt.Sprint(repo.Size))
  66. }
  67. }
  68. stdout, err := cmd.StdoutPipe()
  69. if err != nil {
  70. log.Error("SSH: StdoutPipe: %v", err)
  71. return
  72. }
  73. stderr, err := cmd.StderrPipe()
  74. if err != nil {
  75. log.Error("SSH: StderrPipe: %v", err)
  76. return
  77. }
  78. stdin, err := cmd.StdinPipe()
  79. if err != nil {
  80. log.Error("SSH: StdinPipe: %v", err)
  81. return
  82. }
  83. wg := &sync.WaitGroup{}
  84. wg.Add(2)
  85. if err = cmd.Start(); err != nil {
  86. log.Error("SSH: Start: %v", err)
  87. return
  88. }
  89. go func() {
  90. defer stdin.Close()
  91. if _, err := io.Copy(stdin, session); err != nil {
  92. log.Error("Failed to write session to stdin. %s", err)
  93. }
  94. }()
  95. go func() {
  96. defer wg.Done()
  97. if _, err := io.Copy(session, stdout); err != nil {
  98. log.Error("Failed to write stdout to session. %s", err)
  99. }
  100. }()
  101. go func() {
  102. defer wg.Done()
  103. if _, err := io.Copy(session.Stderr(), stderr); err != nil {
  104. log.Error("Failed to write stderr to session. %s", err)
  105. }
  106. }()
  107. // Ensure all the output has been written before we wait on the command
  108. // to exit.
  109. wg.Wait()
  110. // Wait for the command to exit and log any errors we get
  111. err = cmd.Wait()
  112. if err != nil {
  113. log.Error("SSH: Wait: %v", err)
  114. }
  115. if err := session.Exit(getExitStatusFromError(err)); err != nil {
  116. log.Error("Session failed to exit. %s", err)
  117. }
  118. }
  119. func getRepoFromCommandStr(command string) *models.Repository {
  120. repoPath := strings.TrimPrefix(command, "git-receive-pack '")
  121. repoPath = strings.TrimSuffix(repoPath, ".git'")
  122. if repoPath != "" {
  123. nameArray := strings.Split(repoPath, "/")
  124. if len(nameArray) >= 2 {
  125. ownerName := nameArray[0]
  126. repoName := nameArray[1]
  127. if repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName); err == nil {
  128. return repo
  129. }
  130. }
  131. }
  132. return nil
  133. }
  134. func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
  135. if ctx.User() != setting.SSH.BuiltinServerUser {
  136. return false
  137. }
  138. pkey, err := models.SearchPublicKeyByContent(strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key))))
  139. if err != nil {
  140. log.Error("SearchPublicKeyByContent: %v", err)
  141. return false
  142. }
  143. ctx.SetValue(giteaKeyID, pkey.ID)
  144. return true
  145. }
  146. // Listen starts a SSH server listens on given port.
  147. func Listen(host string, port int, ciphers []string, keyExchanges []string, macs []string) {
  148. // TODO: Handle ciphers, keyExchanges, and macs
  149. srv := ssh.Server{
  150. Addr: fmt.Sprintf("%s:%d", host, port),
  151. PublicKeyHandler: publicKeyHandler,
  152. Handler: sessionHandler,
  153. // We need to explicitly disable the PtyCallback so text displays
  154. // properly.
  155. PtyCallback: func(ctx ssh.Context, pty ssh.Pty) bool {
  156. return false
  157. },
  158. }
  159. keyPath := filepath.Join(setting.AppDataPath, "ssh/gogs.rsa")
  160. if !com.IsExist(keyPath) {
  161. filePath := filepath.Dir(keyPath)
  162. if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
  163. log.Error("Failed to create dir %s: %v", filePath, err)
  164. }
  165. err := GenKeyPair(keyPath)
  166. if err != nil {
  167. log.Fatal("Failed to generate private key: %v", err)
  168. }
  169. log.Trace("New private key is generated: %s", keyPath)
  170. }
  171. err := srv.SetOption(ssh.HostKeyFile(keyPath))
  172. if err != nil {
  173. log.Error("Failed to set Host Key. %s", err)
  174. }
  175. go listen(&srv)
  176. }
  177. // GenKeyPair make a pair of public and private keys for SSH access.
  178. // Public key is encoded in the format for inclusion in an OpenSSH authorized_keys file.
  179. // Private Key generated is PEM encoded
  180. func GenKeyPair(keyPath string) error {
  181. privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
  182. if err != nil {
  183. return err
  184. }
  185. privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
  186. f, err := os.OpenFile(keyPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  187. if err != nil {
  188. return err
  189. }
  190. defer func() {
  191. if err = f.Close(); err != nil {
  192. log.Error("Close: %v", err)
  193. }
  194. }()
  195. if err := pem.Encode(f, privateKeyPEM); err != nil {
  196. return err
  197. }
  198. // generate public key
  199. pub, err := gossh.NewPublicKey(&privateKey.PublicKey)
  200. if err != nil {
  201. return err
  202. }
  203. public := gossh.MarshalAuthorizedKey(pub)
  204. p, err := os.OpenFile(keyPath+".pub", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
  205. if err != nil {
  206. return err
  207. }
  208. defer func() {
  209. if err = p.Close(); err != nil {
  210. log.Error("Close: %v", err)
  211. }
  212. }()
  213. _, err = p.Write(public)
  214. return err
  215. }