serve.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2014 The Gogs 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 cmd
  5. import (
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "strconv"
  11. "strings"
  12. "github.com/codegangsta/cli"
  13. "github.com/gogits/gogs/models"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. var CmdServ = cli.Command{
  18. Name: "serv",
  19. Usage: "This command should only be called by SSH shell",
  20. Description: `Serv provide access auth for repositories`,
  21. Action: runServ,
  22. Flags: []cli.Flag{},
  23. }
  24. func setup(logPath string) {
  25. setting.NewConfigContext()
  26. log.NewGitLogger(path.Join(setting.LogRootPath, logPath))
  27. models.LoadModelsConfig()
  28. if models.UseSQLite3 {
  29. workDir, _ := setting.WorkDir()
  30. os.Chdir(workDir)
  31. }
  32. models.SetEngine()
  33. }
  34. func parseCmd(cmd string) (string, string) {
  35. ss := strings.SplitN(cmd, " ", 2)
  36. if len(ss) != 2 {
  37. return "", ""
  38. }
  39. verb, args := ss[0], ss[1]
  40. if verb == "git" {
  41. ss = strings.SplitN(args, " ", 2)
  42. args = ss[1]
  43. verb = fmt.Sprintf("%s %s", verb, ss[0])
  44. }
  45. return verb, strings.Replace(args, "'/", "'", 1)
  46. }
  47. var (
  48. COMMANDS_READONLY = map[string]models.AccessType{
  49. "git-upload-pack": models.WRITABLE,
  50. "git upload-pack": models.WRITABLE,
  51. "git-upload-archive": models.WRITABLE,
  52. }
  53. COMMANDS_WRITE = map[string]models.AccessType{
  54. "git-receive-pack": models.READABLE,
  55. "git receive-pack": models.READABLE,
  56. }
  57. )
  58. func In(b string, sl map[string]models.AccessType) bool {
  59. _, e := sl[b]
  60. return e
  61. }
  62. func runServ(k *cli.Context) {
  63. setup("serv.log")
  64. keys := strings.Split(os.Args[2], "-")
  65. if len(keys) != 2 {
  66. println("Gogs: auth file format error")
  67. log.GitLogger.Fatal("Invalid auth file format: %s", os.Args[2])
  68. }
  69. keyId, err := strconv.ParseInt(keys[1], 10, 64)
  70. if err != nil {
  71. println("Gogs: auth file format error")
  72. log.GitLogger.Fatal("Invalid auth file format: %v", err)
  73. }
  74. user, err := models.GetUserByKeyId(keyId)
  75. if err != nil {
  76. if err == models.ErrUserNotKeyOwner {
  77. println("Gogs: you are not the owner of SSH key")
  78. log.GitLogger.Fatal("Invalid owner of SSH key: %d", keyId)
  79. }
  80. println("Gogs: internal error:", err)
  81. log.GitLogger.Fatal("Fail to get user by key ID(%d): %v", keyId, err)
  82. }
  83. cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
  84. if cmd == "" {
  85. println("Hi", user.Name, "! You've successfully authenticated, but Gogs does not provide shell access.")
  86. return
  87. }
  88. verb, args := parseCmd(cmd)
  89. repoPath := strings.Trim(args, "'")
  90. rr := strings.SplitN(repoPath, "/", 2)
  91. if len(rr) != 2 {
  92. println("Gogs: unavailable repository", args)
  93. log.GitLogger.Fatal("Unavailable repository: %v", args)
  94. }
  95. repoUserName := rr[0]
  96. repoName := strings.TrimSuffix(rr[1], ".git")
  97. isWrite := In(verb, COMMANDS_WRITE)
  98. isRead := In(verb, COMMANDS_READONLY)
  99. repoUser, err := models.GetUserByName(repoUserName)
  100. if err != nil {
  101. if err == models.ErrUserNotExist {
  102. println("Gogs: given repository owner are not registered")
  103. log.GitLogger.Fatal("Unregistered owner: %s", repoUserName)
  104. }
  105. println("Gogs: internal error:", err)
  106. log.GitLogger.Fatal("Fail to get repository owner(%s): %v", repoUserName, err)
  107. }
  108. // Access check.
  109. switch {
  110. case isWrite:
  111. has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.WRITABLE)
  112. if err != nil {
  113. println("Gogs: internal error:", err)
  114. log.GitLogger.Fatal("Fail to check write access:", err)
  115. } else if !has {
  116. println("You have no right to write this repository")
  117. log.GitLogger.Fatal("User %s has no right to write repository %s", user.Name, repoPath)
  118. }
  119. case isRead:
  120. repo, err := models.GetRepositoryByName(repoUser.Id, repoName)
  121. if err != nil {
  122. if err == models.ErrRepoNotExist {
  123. println("Gogs: given repository does not exist")
  124. log.GitLogger.Fatal("Repository does not exist: %s/%s", repoUser.Name, repoName)
  125. }
  126. println("Gogs: internal error:", err)
  127. log.GitLogger.Fatal("Fail to get repository: %v", err)
  128. }
  129. if !repo.IsPrivate {
  130. break
  131. }
  132. has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.READABLE)
  133. if err != nil {
  134. println("Gogs: internal error:", err)
  135. log.GitLogger.Fatal("Fail to check read access:", err)
  136. } else if !has {
  137. println("You have no right to access this repository")
  138. log.GitLogger.Fatal("User %s has no right to read repository %s", user.Name, repoPath)
  139. }
  140. default:
  141. println("Unknown command")
  142. return
  143. }
  144. models.SetRepoEnvs(user.Id, user.Name, repoName, repoUserName)
  145. gitcmd := exec.Command(verb, repoPath)
  146. gitcmd.Dir = setting.RepoRootPath
  147. gitcmd.Stdout = os.Stdout
  148. gitcmd.Stdin = os.Stdin
  149. gitcmd.Stderr = os.Stderr
  150. err = gitcmd.Run()
  151. if err != nil {
  152. println("Gogs: internal error:", err)
  153. log.GitLogger.Fatal("Fail to execute git command: %v", err)
  154. }
  155. }