serve.go 3.6 KB

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