You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
3.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "github.com/codegangsta/cli"
  9. "github.com/gogits/gogs/models"
  10. )
  11. var (
  12. COMMANDS_READONLY = map[string]int{
  13. "git-upload-pack": models.AU_WRITABLE,
  14. "git upload-pack": models.AU_WRITABLE,
  15. "git-upload-archive": models.AU_WRITABLE,
  16. }
  17. COMMANDS_WRITE = map[string]int{
  18. "git-receive-pack": models.AU_READABLE,
  19. "git receive-pack": models.AU_READABLE,
  20. }
  21. )
  22. var CmdServ = cli.Command{
  23. Name: "serv",
  24. Usage: "This command just should be called by ssh shell",
  25. Description: `
  26. gogs serv provide access auth for repositories`,
  27. Action: runServ,
  28. Flags: []cli.Flag{
  29. //cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"},
  30. //cli.BoolFlag{"verbose, v", "show process details"},
  31. },
  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 %s! You've successfully authenticated, but Gogits does not provide shell access.\n", user.Name)
  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. isWrite := In(verb, COMMANDS_WRITE)
  70. isRead := In(verb, COMMANDS_READONLY)
  71. switch {
  72. case isWrite:
  73. has, err := models.HasAccess(user.Name, repoName, models.AU_WRITABLE)
  74. if err != nil {
  75. println("Inernel error:", err)
  76. return
  77. }
  78. if !has {
  79. println("You have no right to write this repository")
  80. return
  81. }
  82. case isRead:
  83. has, err := models.HasAccess(user.Name, repoName, models.AU_READABLE)
  84. if err != nil {
  85. println("Inernel error")
  86. return
  87. }
  88. if !has {
  89. has, err = models.HasAccess(user.Name, repoName, models.AU_WRITABLE)
  90. if err != nil {
  91. println("Inernel error")
  92. return
  93. }
  94. }
  95. if !has {
  96. println("You have no right to access this repository")
  97. return
  98. }
  99. default:
  100. println("Unknown command")
  101. return
  102. }
  103. isExist, err := models.IsRepositoryExist(user, repoName)
  104. if err != nil {
  105. println("Inernel error:", err.Error())
  106. return
  107. }
  108. if !isExist {
  109. if isRead {
  110. println("Repository", user.Name+"/"+repoName, "is not exist")
  111. return
  112. } else if isWrite {
  113. _, err := models.CreateRepository(user, repoName)
  114. if err != nil {
  115. println("Create repository failed")
  116. return
  117. }
  118. }
  119. }
  120. gitcmd := exec.Command(verb, rRepo)
  121. gitcmd.Dir = models.RepoRootPath
  122. gitcmd.Stdout = os.Stdout
  123. gitcmd.Stdin = os.Stdin
  124. gitcmd.Stderr = os.Stderr
  125. err = gitcmd.Run()
  126. if err != nil {
  127. println("execute command error:", err.Error())
  128. }
  129. }
  130. func parseCmd(cmd string) (string, string) {
  131. ss := strings.SplitN(cmd, " ", 2)
  132. if len(ss) != 2 {
  133. return "", ""
  134. }
  135. verb, args := ss[0], ss[1]
  136. if verb == "git" {
  137. ss = strings.SplitN(args, " ", 2)
  138. args = ss[1]
  139. verb = fmt.Sprintf("%s %s", verb, ss[0])
  140. }
  141. return verb, args
  142. }