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.

198 lines
4.5 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
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. "github.com/gogits/gogs/modules/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. level := "0"
  40. logPath := execDir + "/log/serv.log"
  41. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  42. log.NewLogger(10000, "file", fmt.Sprintf(`{"level":%s,"filename":"%s"}`, level, logPath))
  43. log.Trace("start logging...")
  44. }
  45. func parseCmd(cmd string) (string, string) {
  46. ss := strings.SplitN(cmd, " ", 2)
  47. if len(ss) != 2 {
  48. return "", ""
  49. }
  50. verb, args := ss[0], ss[1]
  51. if verb == "git" {
  52. ss = strings.SplitN(args, " ", 2)
  53. args = ss[1]
  54. verb = fmt.Sprintf("%s %s", verb, ss[0])
  55. }
  56. return verb, args
  57. }
  58. func In(b string, sl map[string]int) bool {
  59. _, e := sl[b]
  60. return e
  61. }
  62. func runServ(k *cli.Context) {
  63. execDir, _ := base.ExecDir()
  64. newLogger(execDir)
  65. log.Trace("new serv request " + log.Mode + ":" + log.Config)
  66. base.NewConfigContext()
  67. models.LoadModelsConfig()
  68. models.NewEngine()
  69. keys := strings.Split(os.Args[2], "-")
  70. if len(keys) != 2 {
  71. fmt.Println("auth file format error")
  72. log.Error("auth file format error")
  73. return
  74. }
  75. keyId, err := strconv.ParseInt(keys[1], 10, 64)
  76. if err != nil {
  77. fmt.Println("auth file format error")
  78. log.Error("auth file format error")
  79. return
  80. }
  81. user, err := models.GetUserByKeyId(keyId)
  82. if err != nil {
  83. fmt.Println("You have no right to access")
  84. log.Error("You have no right to access")
  85. return
  86. }
  87. cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
  88. if cmd == "" {
  89. println("Hi", user.Name, "! You've successfully authenticated, but Gogs does not provide shell access.")
  90. return
  91. }
  92. verb, args := parseCmd(cmd)
  93. rRepo := strings.Trim(args, "'")
  94. rr := strings.SplitN(rRepo, "/", 2)
  95. if len(rr) != 2 {
  96. println("Unavilable repository", args)
  97. log.Error("Unavilable repository %v", args)
  98. return
  99. }
  100. repoName := rr[1]
  101. if strings.HasSuffix(repoName, ".git") {
  102. repoName = repoName[:len(repoName)-4]
  103. }
  104. isWrite := In(verb, COMMANDS_WRITE)
  105. isRead := In(verb, COMMANDS_READONLY)
  106. /*//repo, err := models.GetRepositoryByName(user.Id, repoName)
  107. //var isExist bool = true
  108. if err != nil {
  109. if err == models.ErrRepoNotExist {
  110. //isExist = false
  111. if isRead {
  112. println("Repository", user.Name+"/"+repoName, "is not exist")
  113. log.Error("Repository " + user.Name + "/" + repoName + " is not exist")
  114. return
  115. }
  116. } else {
  117. println("Get repository error:", err)
  118. log.Error("Get repository error: " + err.Error())
  119. return
  120. }
  121. }*/
  122. // access check
  123. switch {
  124. case isWrite:
  125. has, err := models.HasAccess(user.Name, repoName, models.AU_WRITABLE)
  126. if err != nil {
  127. println("Inernel error:", err)
  128. log.Error(err.Error())
  129. return
  130. }
  131. if !has {
  132. println("You have no right to write this repository")
  133. log.Error("You have no right to access this repository")
  134. return
  135. }
  136. case isRead:
  137. has, err := models.HasAccess(user.Name, repoName, models.AU_READABLE)
  138. if err != nil {
  139. println("Inernel error")
  140. log.Error(err.Error())
  141. return
  142. }
  143. if !has {
  144. has, err = models.HasAccess(user.Name, repoName, models.AU_WRITABLE)
  145. if err != nil {
  146. println("Inernel error")
  147. log.Error(err.Error())
  148. return
  149. }
  150. }
  151. if !has {
  152. println("You have no right to access this repository")
  153. log.Error("You have no right to access this repository")
  154. return
  155. }
  156. default:
  157. println("Unknown command")
  158. log.Error("Unknown command")
  159. return
  160. }
  161. // for update use
  162. os.Setenv("userName", user.Name)
  163. os.Setenv("userId", strconv.Itoa(int(user.Id)))
  164. os.Setenv("repoName", repoName)
  165. gitcmd := exec.Command(verb, rRepo)
  166. gitcmd.Dir = base.RepoRootPath
  167. gitcmd.Stdout = os.Stdout
  168. gitcmd.Stdin = os.Stdin
  169. gitcmd.Stderr = os.Stderr
  170. if err = gitcmd.Run(); err != nil {
  171. println("execute command error:", err.Error())
  172. log.Error("execute command error: " + err.Error())
  173. return
  174. }
  175. }