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

serve.go 4.0 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. has, err := models.HasAccess(user.Name, repoName, models.AU_WRITABLE)
  116. if err != nil {
  117. println("Inernel error:", err)
  118. log.Error(err.Error())
  119. return
  120. }
  121. if !has {
  122. println("You have no right to write this repository")
  123. return
  124. }
  125. case isRead:
  126. has, err := models.HasAccess(user.Name, repoName, models.AU_READABLE)
  127. if err != nil {
  128. println("Inernel error")
  129. log.Error(err.Error())
  130. return
  131. }
  132. if !has {
  133. has, err = models.HasAccess(user.Name, repoName, models.AU_WRITABLE)
  134. if err != nil {
  135. println("Inernel error")
  136. log.Error(err.Error())
  137. return
  138. }
  139. }
  140. if !has {
  141. println("You have no right to access this repository")
  142. return
  143. }
  144. default:
  145. println("Unknown command")
  146. return
  147. }
  148. // for update use
  149. os.Setenv("userName", user.Name)
  150. os.Setenv("userId", strconv.Itoa(int(user.Id)))
  151. os.Setenv("repoName", repoName)
  152. gitcmd := exec.Command(verb, rRepo)
  153. gitcmd.Dir = base.RepoRootPath
  154. gitcmd.Stdout = os.Stdout
  155. gitcmd.Stdin = os.Stdin
  156. gitcmd.Stderr = os.Stderr
  157. if err = gitcmd.Run(); err != nil {
  158. println("execute command error:", err.Error())
  159. log.Error(err.Error())
  160. return
  161. }
  162. }