serve.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.SetEngine()
  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", err)
  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("SSH visit error", err)
  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. repoPath := strings.Trim(args, "'")
  94. rr := strings.SplitN(repoPath, "/", 2)
  95. if len(rr) != 2 {
  96. println("Unavilable repository", args)
  97. log.Error("Unavilable repository %v", args)
  98. return
  99. }
  100. repoUserName := rr[0]
  101. repoName := rr[1]
  102. if strings.HasSuffix(repoName, ".git") {
  103. repoName = repoName[:len(repoName)-4]
  104. }
  105. isWrite := In(verb, COMMANDS_WRITE)
  106. isRead := In(verb, COMMANDS_READONLY)
  107. repoUser, err := models.GetUserByName(repoUserName)
  108. if err != nil {
  109. fmt.Println("You have no right to access")
  110. log.Error("Get user failed", err)
  111. return
  112. }
  113. // access check
  114. switch {
  115. case isWrite:
  116. has, err := models.HasAccess(user.Name, strings.ToLower(path.Join(repoUserName, repoName)), models.AU_WRITABLE)
  117. if err != nil {
  118. println("Inernel error:", err)
  119. log.Error(err.Error())
  120. return
  121. }
  122. if !has {
  123. println("You have no right to write this repository")
  124. log.Error("User %s has no right to write repository %s", user.Name, repoPath)
  125. return
  126. }
  127. case isRead:
  128. repo, err := models.GetRepositoryByName(repoUser.Id, repoName)
  129. if err != nil {
  130. println("Get repository error:", err)
  131. log.Error("Get repository error: " + err.Error())
  132. return
  133. }
  134. if !repo.IsPrivate {
  135. break
  136. }
  137. has, err := models.HasAccess(user.Name, repoPath, 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, repoPath, 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, repoPath)
  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. }