serve.go 5.4 KB

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