serve.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. keys := strings.Split(os.Args[2], "-")
  40. if len(keys) != 2 {
  41. fmt.Println("auth file format error")
  42. return
  43. }
  44. keyId, err := strconv.ParseInt(keys[1], 10, 64)
  45. if err != nil {
  46. fmt.Println("auth file format error")
  47. return
  48. }
  49. user, err := models.GetUserByKeyId(keyId)
  50. if err != nil {
  51. fmt.Println("You have no right to access")
  52. return
  53. }
  54. cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
  55. if cmd == "" {
  56. println("Hi", user.Name, "! You've successfully authenticated, but Gogs does not provide shell access.")
  57. return
  58. }
  59. verb, args := parseCmd(cmd)
  60. rRepo := strings.Trim(args, "'")
  61. rr := strings.SplitN(rRepo, "/", 2)
  62. if len(rr) != 2 {
  63. println("Unavilable repository", args)
  64. return
  65. }
  66. repoName := rr[1]
  67. if strings.HasSuffix(repoName, ".git") {
  68. repoName = repoName[:len(repoName)-4]
  69. }
  70. os.Setenv("userName", user.Name)
  71. os.Setenv("userId", strconv.Itoa(int(user.Id)))
  72. repo, err := models.GetRepositoryByName(user, repoName)
  73. if err != nil {
  74. println("Unavilable repository", err)
  75. return
  76. }
  77. os.Setenv("repoId", strconv.Itoa(int(repo.Id)))
  78. os.Setenv("repoName", repoName)
  79. isWrite := In(verb, COMMANDS_WRITE)
  80. isRead := In(verb, COMMANDS_READONLY)
  81. switch {
  82. case isWrite:
  83. has, err := models.HasAccess(user.Name, repoName, models.AU_WRITABLE)
  84. if err != nil {
  85. println("Inernel error:", err)
  86. return
  87. }
  88. if !has {
  89. println("You have no right to write this repository")
  90. return
  91. }
  92. case isRead:
  93. has, err := models.HasAccess(user.Name, repoName, models.AU_READABLE)
  94. if err != nil {
  95. println("Inernel error")
  96. return
  97. }
  98. if !has {
  99. has, err = models.HasAccess(user.Name, repoName, models.AU_WRITABLE)
  100. if err != nil {
  101. println("Inernel error")
  102. return
  103. }
  104. }
  105. if !has {
  106. println("You have no right to access this repository")
  107. return
  108. }
  109. default:
  110. println("Unknown command")
  111. return
  112. }
  113. isExist, err := models.IsRepositoryExist(user, repoName)
  114. if err != nil {
  115. println("Inernel error:", err.Error())
  116. return
  117. }
  118. if !isExist {
  119. if isRead {
  120. println("Repository", user.Name+"/"+repoName, "is not exist")
  121. return
  122. } else if isWrite {
  123. _, err := models.CreateRepository(user, repoName, "", "", "", false, true)
  124. if err != nil {
  125. println("Create repository failed")
  126. return
  127. }
  128. }
  129. }
  130. gitcmd := exec.Command(verb, rRepo)
  131. gitcmd.Dir = base.RepoRootPath
  132. gitcmd.Stdout = os.Stdout
  133. gitcmd.Stdin = os.Stdin
  134. gitcmd.Stderr = os.Stderr
  135. if err = gitcmd.Run(); err != nil {
  136. println("execute command error:", err.Error())
  137. }
  138. }
  139. func parseCmd(cmd string) (string, string) {
  140. ss := strings.SplitN(cmd, " ", 2)
  141. if len(ss) != 2 {
  142. return "", ""
  143. }
  144. verb, args := ss[0], ss[1]
  145. if verb == "git" {
  146. ss = strings.SplitN(args, " ", 2)
  147. args = ss[1]
  148. verb = fmt.Sprintf("%s %s", verb, ss[0])
  149. }
  150. return verb, args
  151. }