You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
4.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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 main
  5. import (
  6. //"container/list"
  7. "fmt"
  8. "os"
  9. "os/exec"
  10. "path"
  11. "strconv"
  12. "strings"
  13. "github.com/codegangsta/cli"
  14. qlog "github.com/qiniu/log"
  15. //"github.com/gogits/git"
  16. "github.com/gogits/gogs/models"
  17. "github.com/gogits/gogs/modules/base"
  18. )
  19. var (
  20. COMMANDS_READONLY = map[string]int{
  21. "git-upload-pack": models.AU_WRITABLE,
  22. "git upload-pack": models.AU_WRITABLE,
  23. "git-upload-archive": models.AU_WRITABLE,
  24. }
  25. COMMANDS_WRITE = map[string]int{
  26. "git-receive-pack": models.AU_READABLE,
  27. "git receive-pack": models.AU_READABLE,
  28. }
  29. )
  30. var CmdServ = cli.Command{
  31. Name: "serv",
  32. Usage: "This command just should be called by ssh shell",
  33. Description: `
  34. gogs serv provide access auth for repositories`,
  35. Action: runServ,
  36. Flags: []cli.Flag{},
  37. }
  38. func newLogger(execDir string) {
  39. logPath := execDir + "/log/serv.log"
  40. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  41. f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm)
  42. if err != nil {
  43. qlog.Fatal(err)
  44. }
  45. qlog.SetOutput(f)
  46. qlog.Info("Start logging serv...")
  47. }
  48. func parseCmd(cmd string) (string, string) {
  49. ss := strings.SplitN(cmd, " ", 2)
  50. if len(ss) != 2 {
  51. return "", ""
  52. }
  53. verb, args := ss[0], ss[1]
  54. if verb == "git" {
  55. ss = strings.SplitN(args, " ", 2)
  56. args = ss[1]
  57. verb = fmt.Sprintf("%s %s", verb, ss[0])
  58. }
  59. return verb, args
  60. }
  61. func In(b string, sl map[string]int) bool {
  62. _, e := sl[b]
  63. return e
  64. }
  65. func runServ(k *cli.Context) {
  66. execDir, _ := base.ExecDir()
  67. newLogger(execDir)
  68. base.NewConfigContext()
  69. models.LoadModelsConfig()
  70. if models.UseSQLite3 {
  71. os.Chdir(execDir)
  72. }
  73. models.SetEngine()
  74. keys := strings.Split(os.Args[2], "-")
  75. if len(keys) != 2 {
  76. println("auth file format error")
  77. qlog.Fatal("auth file format error")
  78. }
  79. keyId, err := strconv.ParseInt(keys[1], 10, 64)
  80. if err != nil {
  81. println("auth file format error")
  82. qlog.Fatal("auth file format error", err)
  83. }
  84. user, err := models.GetUserByKeyId(keyId)
  85. if err != nil {
  86. println("You have no right to access")
  87. qlog.Fatalf("SSH visit error: %v", err)
  88. }
  89. cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
  90. if cmd == "" {
  91. println("Hi", user.Name, "! You've successfully authenticated, but Gogs does not provide shell access.")
  92. return
  93. }
  94. verb, args := parseCmd(cmd)
  95. repoPath := strings.Trim(args, "'")
  96. rr := strings.SplitN(repoPath, "/", 2)
  97. if len(rr) != 2 {
  98. println("Unavilable repository", args)
  99. qlog.Fatalf("Unavilable repository %v", args)
  100. }
  101. repoUserName := rr[0]
  102. repoName := rr[1]
  103. if strings.HasSuffix(repoName, ".git") {
  104. repoName = repoName[:len(repoName)-4]
  105. }
  106. isWrite := In(verb, COMMANDS_WRITE)
  107. isRead := In(verb, COMMANDS_READONLY)
  108. repoUser, err := models.GetUserByName(repoUserName)
  109. if err != nil {
  110. println("You have no right to access")
  111. qlog.Fatal("Get user failed", err)
  112. }
  113. // access check
  114. switch {
  115. case isWrite:
  116. has, err := models.HasAccess(user.LowerName, path.Join(repoUserName, repoName), models.AU_WRITABLE)
  117. if err != nil {
  118. println("Inernel error:", err)
  119. qlog.Fatal(err)
  120. } else if !has {
  121. println("You have no right to write this repository")
  122. qlog.Fatalf("User %s has no right to write repository %s", user.Name, repoPath)
  123. }
  124. case isRead:
  125. repo, err := models.GetRepositoryByName(repoUser.Id, repoName)
  126. if err != nil {
  127. println("Get repository error:", err)
  128. qlog.Fatal("Get repository error: " + err.Error())
  129. }
  130. if !repo.IsPrivate {
  131. break
  132. }
  133. has, err := models.HasAccess(user.Name, repoPath, models.AU_READABLE)
  134. if err != nil {
  135. println("Inernel error")
  136. qlog.Fatal(err)
  137. }
  138. if !has {
  139. has, err = models.HasAccess(user.Name, repoPath, models.AU_WRITABLE)
  140. if err != nil {
  141. println("Inernel error")
  142. qlog.Fatal(err)
  143. }
  144. }
  145. if !has {
  146. println("You have no right to access this repository")
  147. qlog.Fatal("You have no right to access this repository")
  148. }
  149. default:
  150. println("Unknown command")
  151. qlog.Fatal("Unknown command")
  152. }
  153. models.SetRepoEnvs(user.Id, user.Name, repoName)
  154. gitcmd := exec.Command(verb, repoPath)
  155. gitcmd.Dir = base.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("execute command error:", err.Error())
  161. qlog.Fatal("execute command error: " + err.Error())
  162. }
  163. }