serve.go 3.7 KB

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