serve.go 5.8 KB

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