serve.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. "strconv"
  11. "strings"
  12. "github.com/codegangsta/cli"
  13. "github.com/gogits/gogs/modules/log"
  14. //"github.com/gogits/git"
  15. "github.com/gogits/gogs/models"
  16. "github.com/gogits/gogs/modules/base"
  17. )
  18. var (
  19. COMMANDS_READONLY = map[string]int{
  20. "git-upload-pack": models.AU_WRITABLE,
  21. "git upload-pack": models.AU_WRITABLE,
  22. "git-upload-archive": models.AU_WRITABLE,
  23. }
  24. COMMANDS_WRITE = map[string]int{
  25. "git-receive-pack": models.AU_READABLE,
  26. "git receive-pack": models.AU_READABLE,
  27. }
  28. )
  29. var CmdServ = cli.Command{
  30. Name: "serv",
  31. Usage: "This command just should be called by ssh shell",
  32. Description: `
  33. gogs serv provide access auth for repositories`,
  34. Action: runServ,
  35. Flags: []cli.Flag{},
  36. }
  37. func init() {
  38. level := "0"
  39. os.MkdirAll("log", os.ModePerm)
  40. log.NewLogger(10000, "file", fmt.Sprintf(`{"level":%s,"filename":"%s"}`, level, "log/serv.log"))
  41. log.Info("start logging...")
  42. }
  43. func parseCmd(cmd string) (string, string) {
  44. ss := strings.SplitN(cmd, " ", 2)
  45. if len(ss) != 2 {
  46. return "", ""
  47. }
  48. verb, args := ss[0], ss[1]
  49. if verb == "git" {
  50. ss = strings.SplitN(args, " ", 2)
  51. args = ss[1]
  52. verb = fmt.Sprintf("%s %s", verb, ss[0])
  53. }
  54. return verb, args
  55. }
  56. func In(b string, sl map[string]int) bool {
  57. _, e := sl[b]
  58. return e
  59. }
  60. func runServ(k *cli.Context) {
  61. base.NewConfigContext()
  62. models.LoadModelsConfig()
  63. models.NewEngine()
  64. keys := strings.Split(os.Args[2], "-")
  65. if len(keys) != 2 {
  66. fmt.Println("auth file format error")
  67. return
  68. }
  69. keyId, err := strconv.ParseInt(keys[1], 10, 64)
  70. if err != nil {
  71. fmt.Println("auth file format error")
  72. return
  73. }
  74. user, err := models.GetUserByKeyId(keyId)
  75. if err != nil {
  76. fmt.Println("You have no right to access")
  77. return
  78. }
  79. cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
  80. if cmd == "" {
  81. println("Hi", user.Name, "! You've successfully authenticated, but Gogs does not provide shell access.")
  82. return
  83. }
  84. verb, args := parseCmd(cmd)
  85. rRepo := strings.Trim(args, "'")
  86. rr := strings.SplitN(rRepo, "/", 2)
  87. if len(rr) != 2 {
  88. println("Unavilable repository", args)
  89. return
  90. }
  91. repoName := rr[1]
  92. if strings.HasSuffix(repoName, ".git") {
  93. repoName = repoName[:len(repoName)-4]
  94. }
  95. isWrite := In(verb, COMMANDS_WRITE)
  96. isRead := In(verb, COMMANDS_READONLY)
  97. //repo, err := models.GetRepositoryByName(user.Id, repoName)
  98. //var isExist bool = true
  99. if err != nil {
  100. if err == models.ErrRepoNotExist {
  101. //isExist = false
  102. if isRead {
  103. println("Repository", user.Name+"/"+repoName, "is not exist")
  104. return
  105. }
  106. } else {
  107. println("Get repository error:", err)
  108. log.Error(err.Error())
  109. return
  110. }
  111. }
  112. // access check
  113. switch {
  114. case isWrite:
  115. println(user.Name, repoName, models.AU_WRITABLE)
  116. has, err := models.HasAccess(user.Name, 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. return
  125. }
  126. case isRead:
  127. has, err := models.HasAccess(user.Name, repoName, models.AU_READABLE)
  128. if err != nil {
  129. println("Inernel error")
  130. log.Error(err.Error())
  131. return
  132. }
  133. if !has {
  134. has, err = models.HasAccess(user.Name, repoName, models.AU_WRITABLE)
  135. if err != nil {
  136. println("Inernel error")
  137. log.Error(err.Error())
  138. return
  139. }
  140. }
  141. if !has {
  142. println("You have no right to access this repository")
  143. return
  144. }
  145. default:
  146. println("Unknown command")
  147. return
  148. }
  149. // for update use
  150. os.Setenv("userName", user.Name)
  151. os.Setenv("userId", strconv.Itoa(int(user.Id)))
  152. os.Setenv("repoName", repoName)
  153. gitcmd := exec.Command(verb, rRepo)
  154. gitcmd.Dir = base.RepoRootPath
  155. gitcmd.Stdout = os.Stdout
  156. gitcmd.Stdin = os.Stdin
  157. gitcmd.Stderr = os.Stderr
  158. if err = gitcmd.Run(); err != nil {
  159. println("execute command error:", err.Error())
  160. log.Error(err.Error())
  161. return
  162. }
  163. }