serve.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "path"
  11. "strconv"
  12. "strings"
  13. "github.com/codegangsta/cli"
  14. qlog "github.com/qiniu/log"
  15. //"github.com/gogits/git"
  16. "github.com/gogits/gogs/models"
  17. "github.com/gogits/gogs/modules/base"
  18. )
  19. var (
  20. COMMANDS_READONLY = map[string]int{
  21. "git-upload-pack": models.AU_WRITABLE,
  22. "git upload-pack": models.AU_WRITABLE,
  23. "git-upload-archive": models.AU_WRITABLE,
  24. }
  25. COMMANDS_WRITE = map[string]int{
  26. "git-receive-pack": models.AU_READABLE,
  27. "git receive-pack": models.AU_READABLE,
  28. }
  29. )
  30. var CmdServ = cli.Command{
  31. Name: "serv",
  32. Usage: "This command just should be called by ssh shell",
  33. Description: `
  34. gogs serv provide access auth for repositories`,
  35. Action: runServ,
  36. Flags: []cli.Flag{},
  37. }
  38. func newLogger(execDir string) {
  39. logPath := execDir + "/log/serv.log"
  40. os.MkdirAll(path.Dir(logPath), os.ModePerm)
  41. f, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, os.ModePerm)
  42. if err != nil {
  43. qlog.Fatal(err)
  44. }
  45. qlog.SetOutput(f)
  46. //qlog.SetOutputLevel(qlog.Ldebug)
  47. qlog.Info("Start logging serv...")
  48. }
  49. func parseCmd(cmd string) (string, string) {
  50. ss := strings.SplitN(cmd, " ", 2)
  51. if len(ss) != 2 {
  52. return "", ""
  53. }
  54. verb, args := ss[0], ss[1]
  55. if verb == "git" {
  56. ss = strings.SplitN(args, " ", 2)
  57. args = ss[1]
  58. verb = fmt.Sprintf("%s %s", verb, ss[0])
  59. }
  60. return verb, args
  61. }
  62. func In(b string, sl map[string]int) bool {
  63. _, e := sl[b]
  64. return e
  65. }
  66. func runServ(k *cli.Context) {
  67. execDir, _ := base.ExecDir()
  68. newLogger(execDir)
  69. base.NewConfigContext()
  70. models.LoadModelsConfig()
  71. if models.UseSQLite3 {
  72. os.Chdir(execDir)
  73. }
  74. models.SetEngine()
  75. keys := strings.Split(os.Args[2], "-")
  76. if len(keys) != 2 {
  77. println("auth file format error")
  78. qlog.Fatal("auth file format error")
  79. }
  80. keyId, err := strconv.ParseInt(keys[1], 10, 64)
  81. if err != nil {
  82. println("auth file format error")
  83. qlog.Fatal("auth file format error", err)
  84. }
  85. user, err := models.GetUserByKeyId(keyId)
  86. if err != nil {
  87. println("You have no right to access")
  88. qlog.Fatalf("SSH visit error: %v", err)
  89. }
  90. cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
  91. if cmd == "" {
  92. println("Hi", user.Name, "! You've successfully authenticated, but Gogs does not provide shell access.")
  93. return
  94. }
  95. verb, args := parseCmd(cmd)
  96. repoPath := strings.Trim(args, "'")
  97. rr := strings.SplitN(repoPath, "/", 2)
  98. if len(rr) != 2 {
  99. println("Unavailable repository", args)
  100. qlog.Fatalf("Unavailable repository %v", args)
  101. }
  102. repoUserName := rr[0]
  103. repoName := strings.TrimSuffix(rr[1], ".git")
  104. isWrite := In(verb, COMMANDS_WRITE)
  105. isRead := In(verb, COMMANDS_READONLY)
  106. repoUser, err := models.GetUserByName(repoUserName)
  107. if err != nil {
  108. println("You have no right to access")
  109. qlog.Fatal("Get user failed", err)
  110. }
  111. // access check
  112. switch {
  113. case isWrite:
  114. has, err := models.HasAccess(user.LowerName, path.Join(repoUserName, repoName), models.AU_WRITABLE)
  115. if err != nil {
  116. println("Internal error:", err)
  117. qlog.Fatal(err)
  118. } else if !has {
  119. println("You have no right to write this repository")
  120. qlog.Fatalf("User %s has no right to write repository %s", user.Name, repoPath)
  121. }
  122. case isRead:
  123. repo, err := models.GetRepositoryByName(repoUser.Id, repoName)
  124. if err != nil {
  125. println("Get repository error:", err)
  126. qlog.Fatal("Get repository error: " + err.Error())
  127. }
  128. if !repo.IsPrivate {
  129. break
  130. }
  131. has, err := models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_READABLE)
  132. if err != nil {
  133. println("Internal error")
  134. qlog.Fatal(err)
  135. }
  136. if !has {
  137. has, err = models.HasAccess(user.Name, path.Join(repoUserName, repoName), models.AU_WRITABLE)
  138. if err != nil {
  139. println("Internal error")
  140. qlog.Fatal(err)
  141. }
  142. }
  143. if !has {
  144. println("You have no right to access this repository")
  145. qlog.Fatal("You have no right to access this repository")
  146. }
  147. default:
  148. println("Unknown command")
  149. qlog.Fatal("Unknown command")
  150. }
  151. models.SetRepoEnvs(user.Id, user.Name, repoName)
  152. gitcmd := exec.Command(verb, repoPath)
  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. qlog.Fatal("execute command error: " + err.Error())
  160. }
  161. //refName := os.Getenv("refName")
  162. //oldCommitId := os.Getenv("oldCommitId")
  163. //newCommitId := os.Getenv("newCommitId")
  164. //qlog.Error("get envs:", refName, oldCommitId, newCommitId)
  165. // update
  166. //models.Update(refName, oldCommitId, newCommitId, repoUserName, repoName, user.Id)
  167. }