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