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 5.5 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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 cmd
  5. import (
  6. "fmt"
  7. "os"
  8. "os/exec"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/Unknwon/com"
  13. "github.com/codegangsta/cli"
  14. "github.com/gogits/gogs/models"
  15. "github.com/gogits/gogs/modules/log"
  16. "github.com/gogits/gogs/modules/setting"
  17. "github.com/gogits/gogs/modules/uuid"
  18. )
  19. const (
  20. _ACCESS_DENIED_MESSAGE = "Repository does not exist or you do not have access"
  21. )
  22. var CmdServ = cli.Command{
  23. Name: "serv",
  24. Usage: "This command should only be called by SSH shell",
  25. Description: `Serv provide access auth for repositories`,
  26. Action: runServ,
  27. Flags: []cli.Flag{
  28. cli.StringFlag{"config, c", "custom/conf/app.ini", "Custom configuration file path", ""},
  29. },
  30. }
  31. func setup(logPath string) {
  32. setting.NewConfigContext()
  33. log.NewGitLogger(filepath.Join(setting.LogRootPath, logPath))
  34. if setting.DisableSSH {
  35. println("Gogs: SSH has been disabled")
  36. os.Exit(1)
  37. }
  38. models.LoadModelsConfig()
  39. if setting.UseSQLite3 {
  40. workDir, _ := setting.WorkDir()
  41. os.Chdir(workDir)
  42. }
  43. models.SetEngine()
  44. }
  45. func parseCmd(cmd string) (string, string) {
  46. ss := strings.SplitN(cmd, " ", 2)
  47. if len(ss) != 2 {
  48. return "", ""
  49. }
  50. return ss[0], strings.Replace(ss[1], "'/", "'", 1)
  51. }
  52. var (
  53. COMMANDS = map[string]models.AccessMode{
  54. "git-upload-pack": models.ACCESS_MODE_READ,
  55. "git-upload-archive": models.ACCESS_MODE_READ,
  56. "git-receive-pack": models.ACCESS_MODE_WRITE,
  57. }
  58. )
  59. func runServ(c *cli.Context) {
  60. if c.IsSet("config") {
  61. setting.CustomConf = c.String("config")
  62. }
  63. setup("serv.log")
  64. fail := func(userMessage, logMessage string, args ...interface{}) {
  65. fmt.Fprintln(os.Stderr, "Gogs: ", userMessage)
  66. log.GitLogger.Fatal(2, logMessage, args...)
  67. }
  68. if len(c.Args()) < 1 {
  69. fail("Not enough arguments", "Not enough arguments")
  70. }
  71. keys := strings.Split(c.Args()[0], "-")
  72. if len(keys) != 2 {
  73. fail("key-id format error", "Invalid key id: %s", c.Args()[0])
  74. }
  75. keyId, err := com.StrTo(keys[1]).Int64()
  76. if err != nil {
  77. fail("key-id format error", "Invalid key id: %s", err)
  78. }
  79. user, err := models.GetUserByKeyId(keyId)
  80. if err != nil {
  81. fail("internal error", "Failed to get user by key ID(%d): %v", keyId, err)
  82. }
  83. cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
  84. if cmd == "" {
  85. fmt.Printf("Hi, %s! You've successfully authenticated, but Gogs does not provide shell access.\n", user.Name)
  86. if user.IsAdmin {
  87. println("If this is unexpected, please log in with password and setup Gogs under another user.")
  88. }
  89. return
  90. }
  91. verb, args := parseCmd(cmd)
  92. repoPath := strings.Trim(args, "'")
  93. rr := strings.SplitN(repoPath, "/", 2)
  94. if len(rr) != 2 {
  95. fail("Invalid repository path", "Invalid repository path: %v", args)
  96. }
  97. repoUserName := rr[0]
  98. repoName := strings.TrimSuffix(rr[1], ".git")
  99. repoUser, err := models.GetUserByName(repoUserName)
  100. if err != nil {
  101. if err == models.ErrUserNotExist {
  102. fail("Repository owner does not exist", "Unregistered owner: %s", repoUserName)
  103. }
  104. fail("Internal error", "Failed to get repository owner(%s): %v", repoUserName, err)
  105. }
  106. repo, err := models.GetRepositoryByName(repoUser.Id, repoName)
  107. if err != nil {
  108. if models.IsErrRepoNotExist(err) {
  109. if user.Id == repoUser.Id || repoUser.IsOwnedBy(user.Id) {
  110. fail("Repository does not exist", "Repository does not exist: %s/%s", repoUser.Name, repoName)
  111. } else {
  112. fail(_ACCESS_DENIED_MESSAGE, "Repository does not exist: %s/%s", repoUser.Name, repoName)
  113. }
  114. }
  115. fail("Internal error", "Failed to get repository: %v", err)
  116. }
  117. requestedMode, has := COMMANDS[verb]
  118. if !has {
  119. fail("Unknown git command", "Unknown git command %s", verb)
  120. }
  121. mode, err := models.AccessLevel(user, repo)
  122. if err != nil {
  123. fail("Internal error", "Fail to check access: %v", err)
  124. } else if mode < requestedMode {
  125. clientMessage := _ACCESS_DENIED_MESSAGE
  126. if mode >= models.ACCESS_MODE_READ {
  127. clientMessage = "You do not have sufficient authorization for this action"
  128. }
  129. fail(clientMessage,
  130. "User %s does not have level %v access to repository %s",
  131. user.Name, requestedMode, repoPath)
  132. }
  133. uuid := uuid.NewV4().String()
  134. os.Setenv("uuid", uuid)
  135. var gitcmd *exec.Cmd
  136. verbs := strings.Split(verb, " ")
  137. if len(verbs) == 2 {
  138. gitcmd = exec.Command(verbs[0], verbs[1], repoPath)
  139. } else {
  140. gitcmd = exec.Command(verb, repoPath)
  141. }
  142. gitcmd.Dir = setting.RepoRootPath
  143. gitcmd.Stdout = os.Stdout
  144. gitcmd.Stdin = os.Stdin
  145. gitcmd.Stderr = os.Stderr
  146. if err = gitcmd.Run(); err != nil {
  147. fail("Internal error", "Failed to execute git command: %v", err)
  148. }
  149. if requestedMode == models.ACCESS_MODE_WRITE {
  150. tasks, err := models.GetUpdateTasksByUuid(uuid)
  151. if err != nil {
  152. log.GitLogger.Fatal(2, "GetUpdateTasksByUuid: %v", err)
  153. }
  154. for _, task := range tasks {
  155. err = models.Update(task.RefName, task.OldCommitId, task.NewCommitId,
  156. user.Name, repoUserName, repoName, user.Id)
  157. if err != nil {
  158. log.GitLogger.Error(2, "Failed to update: %v", err)
  159. }
  160. }
  161. if err = models.DelUpdateTasksByUuid(uuid); err != nil {
  162. log.GitLogger.Fatal(2, "DelUpdateTasksByUuid: %v", err)
  163. }
  164. }
  165. // Update key activity.
  166. key, err := models.GetPublicKeyById(keyId)
  167. if err != nil {
  168. fail("Internal error", "GetPublicKeyById: %v", err)
  169. }
  170. key.Updated = time.Now()
  171. if err = models.UpdatePublicKey(key); err != nil {
  172. fail("Internal error", "UpdatePublicKey: %v", err)
  173. }
  174. }