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.

serve.go 4.5KB

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 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(0, "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. base.NewConfigContext()
  66. models.LoadModelsConfig()
  67. if models.UseSQLite3 {
  68. os.Chdir(execDir)
  69. }
  70. models.SetEngine()
  71. keys := strings.Split(os.Args[2], "-")
  72. if len(keys) != 2 {
  73. println("auth file format error")
  74. log.Error("auth file format error")
  75. return
  76. }
  77. keyId, err := strconv.ParseInt(keys[1], 10, 64)
  78. if err != nil {
  79. println("auth file format error")
  80. log.Error("auth file format error", err)
  81. return
  82. }
  83. user, err := models.GetUserByKeyId(keyId)
  84. if err != nil {
  85. println("You have no right to access")
  86. log.Error("SSH visit error: %v", err)
  87. return
  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. log.Error("Unavilable repository %v", args)
  100. return
  101. }
  102. repoUserName := rr[0]
  103. repoName := rr[1]
  104. if strings.HasSuffix(repoName, ".git") {
  105. repoName = repoName[:len(repoName)-4]
  106. }
  107. isWrite := In(verb, COMMANDS_WRITE)
  108. isRead := In(verb, COMMANDS_READONLY)
  109. repoUser, err := models.GetUserByName(repoUserName)
  110. if err != nil {
  111. fmt.Println("You have no right to access")
  112. log.Error("Get user failed", err)
  113. return
  114. }
  115. // access check
  116. switch {
  117. case isWrite:
  118. has, err := models.HasAccess(user.LowerName, path.Join(repoUserName, repoName), models.AU_WRITABLE)
  119. if err != nil {
  120. println("Inernel error:", err)
  121. log.Error(err.Error())
  122. return
  123. } else if !has {
  124. println("You have no right to write this repository")
  125. log.Error("User %s has no right to write repository %s", user.Name, repoPath)
  126. return
  127. }
  128. case isRead:
  129. repo, err := models.GetRepositoryByName(repoUser.Id, repoName)
  130. if err != nil {
  131. println("Get repository error:", err)
  132. log.Error("Get repository error: " + err.Error())
  133. return
  134. }
  135. if !repo.IsPrivate {
  136. break
  137. }
  138. has, err := models.HasAccess(user.Name, repoPath, models.AU_READABLE)
  139. if err != nil {
  140. println("Inernel error")
  141. log.Error(err.Error())
  142. return
  143. }
  144. if !has {
  145. has, err = models.HasAccess(user.Name, repoPath, models.AU_WRITABLE)
  146. if err != nil {
  147. println("Inernel error")
  148. log.Error(err.Error())
  149. return
  150. }
  151. }
  152. if !has {
  153. println("You have no right to access this repository")
  154. log.Error("You have no right to access this repository")
  155. return
  156. }
  157. default:
  158. println("Unknown command")
  159. log.Error("Unknown command")
  160. return
  161. }
  162. // for update use
  163. os.Setenv("userName", user.Name)
  164. os.Setenv("userId", strconv.Itoa(int(user.Id)))
  165. os.Setenv("repoName", repoName)
  166. gitcmd := exec.Command(verb, repoPath)
  167. gitcmd.Dir = base.RepoRootPath
  168. gitcmd.Stdout = os.Stdout
  169. gitcmd.Stdin = os.Stdin
  170. gitcmd.Stderr = os.Stderr
  171. if err = gitcmd.Run(); err != nil {
  172. println("execute command error:", err.Error())
  173. log.Error("execute command error: " + err.Error())
  174. return
  175. }
  176. }