serve.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "github.com/gogits/gogs/utils/log"
  11. )
  12. var (
  13. COMMANDS_READONLY = map[string]int{
  14. "git-upload-pack": models.AU_WRITABLE,
  15. "git upload-pack": 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: "just run",
  25. Description: `
  26. gogs serv`,
  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. fmt.Printf("Hi %s! You've successfully authenticated, but Gogits does not provide shell access.\n", user.Name)
  56. return
  57. }
  58. println(cmd)
  59. verb, args := parseCmd(cmd)
  60. rr := strings.SplitN(strings.Trim(args, "'"), "/", 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. println("repoPath:", models.RepoPath(user.Name, repoName))
  72. switch {
  73. case isWrite:
  74. has, err := models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb])
  75. if err != nil {
  76. fmt.Println("Inernel error")
  77. return
  78. }
  79. if !has {
  80. println("You have no right to access this repository")
  81. return
  82. }
  83. case isRead:
  84. has, err := models.HasAccess(user.Name, repoName, COMMANDS_READONLY[verb])
  85. if err != nil {
  86. fmt.Println("Inernel error")
  87. return
  88. }
  89. if !has {
  90. has, err = models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb])
  91. if err != nil {
  92. fmt.Println("Inernel error")
  93. return
  94. }
  95. }
  96. if !has {
  97. println("You have no right to access this repository")
  98. return
  99. }
  100. default:
  101. println("Unknown command")
  102. return
  103. }
  104. isExist, err := models.IsRepositoryExist(user, repoName)
  105. if err != nil {
  106. println("Inernel error:", err.Error())
  107. return
  108. }
  109. if !isExist {
  110. if isRead {
  111. println("Repository", user.Name+"/"+repoName, "is not exist")
  112. return
  113. } else if isWrite {
  114. _, err := models.CreateRepository(user, repoName)
  115. if err != nil {
  116. println("Create repository failed")
  117. return
  118. }
  119. }
  120. }
  121. fullPath := models.RepoPath(user.Name, repoName)
  122. newcmd := fmt.Sprintf("%s '%s'", verb, fullPath)
  123. println(newcmd)
  124. gitcmd := exec.Command("git", "shell", "-c", newcmd)
  125. gitcmd.Stdout = os.Stdout
  126. gitcmd.Stderr = os.Stderr
  127. err = gitcmd.Run()
  128. if err != nil {
  129. log.Error("execute command error: %s", err)
  130. }
  131. }
  132. func parseCmd(cmd string) (string, string) {
  133. ss := strings.SplitN(cmd, " ", 2)
  134. if len(ss) != 2 {
  135. return "", ""
  136. }
  137. verb, args := ss[0], ss[1]
  138. if verb == "git" {
  139. ss = strings.SplitN(args, " ", 2)
  140. args = ss[1]
  141. verb = fmt.Sprintf("%s %s", verb, ss[0])
  142. }
  143. return verb, args
  144. }