serve.go 5.2 KB

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