serve.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "github.com/codegangsta/cli"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/utils/log"
  12. )
  13. var (
  14. COMMANDS_READONLY = map[string]int{
  15. "git-upload-pack": models.AU_WRITABLE,
  16. "git upload-pack": models.AU_WRITABLE,
  17. }
  18. COMMANDS_WRITE = map[string]int{
  19. "git-receive-pack": models.AU_READABLE,
  20. "git receive-pack": models.AU_READABLE,
  21. }
  22. )
  23. var CmdServ = cli.Command{
  24. Name: "serv",
  25. Usage: "just run",
  26. Description: `
  27. gogs serv`,
  28. Action: runServ,
  29. Flags: []cli.Flag{
  30. //cli.BoolFlag{"update, u", "update pakcage(s) and dependencies if any"},
  31. //cli.BoolFlag{"verbose, v", "show process details"},
  32. },
  33. }
  34. func In(b string, sl map[string]int) bool {
  35. _, e := sl[b]
  36. return e
  37. }
  38. func runServ(*cli.Context) {
  39. keys := strings.Split(os.Args[2], "-")
  40. if len(keys) != 2 {
  41. fmt.Println("auth file format error")
  42. return
  43. }
  44. keyId, err := strconv.ParseInt(keys[1], 10, 64)
  45. if err != nil {
  46. fmt.Println("auth file format error")
  47. return
  48. }
  49. user, err := models.GetUserByKeyId(keyId)
  50. if err != nil {
  51. fmt.Println("You have no right to access")
  52. return
  53. }
  54. cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
  55. if cmd == "" {
  56. fmt.Printf("Hi %s! You've successfully authenticated, but Gogits does not provide shell access.\n", user.Name)
  57. return
  58. }
  59. f, _ := os.Create("test2.log")
  60. f.WriteString(cmd)
  61. f.Close()
  62. log.Info("cmd is %s", cmd)
  63. verb, args := parseCmd(cmd)
  64. rr := strings.SplitN(strings.Trim(args, "'"), "/", 1)
  65. if len(rr) != 2 {
  66. fmt.Printf("Unavilable repository")
  67. return
  68. }
  69. repoName := rr[1]
  70. if strings.HasSuffix(repoName, ".git") {
  71. repoName = repoName[:len(repoName)-4]
  72. }
  73. isWrite := In(verb, COMMANDS_WRITE)
  74. isRead := In(verb, COMMANDS_READONLY)
  75. switch {
  76. case isWrite:
  77. has, err := models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb])
  78. if err != nil {
  79. fmt.Println("Inernel error")
  80. return
  81. }
  82. if !has {
  83. fmt.Println("You have no right to access this repository")
  84. return
  85. }
  86. case isRead:
  87. has, err := models.HasAccess(user.Name, repoName, COMMANDS_READONLY[verb])
  88. if err != nil {
  89. fmt.Println("Inernel error")
  90. return
  91. }
  92. if !has {
  93. has, err = models.HasAccess(user.Name, repoName, COMMANDS_WRITE[verb])
  94. if err != nil {
  95. fmt.Println("Inernel error")
  96. return
  97. }
  98. }
  99. if !has {
  100. fmt.Println("You have no right to access this repository")
  101. return
  102. }
  103. default:
  104. fmt.Println("Unknown command")
  105. return
  106. }
  107. isExist, err := models.IsRepositoryExist(user, repoName)
  108. if err != nil {
  109. fmt.Println("Inernel error")
  110. return
  111. }
  112. if !isExist {
  113. if isRead {
  114. fmt.Println("Repository is not exist")
  115. return
  116. } else if isWrite {
  117. _, err := models.CreateRepository(user, repoName)
  118. if err != nil {
  119. fmt.Println("Create repository failed")
  120. return
  121. }
  122. }
  123. }
  124. fullPath := filepath.Join(models.RepoRootPath, user.Name, repoName+".git")
  125. newcmd := fmt.Sprintf("%s '%s'", verb, fullPath)
  126. fmt.Println(newcmd)
  127. gitcmd := exec.Command("git", "shell", "-c", newcmd)
  128. gitcmd.Stdout = os.Stdout
  129. gitcmd.Stderr = os.Stderr
  130. err = gitcmd.Run()
  131. if err != nil {
  132. log.Error("execute command error: %s", err)
  133. }
  134. }
  135. func parseCmd(cmd string) (string, string) {
  136. ss := strings.SplitN(cmd, " ", 1)
  137. if len(ss) != 2 {
  138. return "", ""
  139. }
  140. verb, args := ss[0], ss[1]
  141. if verb == "git" {
  142. ss = strings.SplitN(args, " ", 1)
  143. args = ss[1]
  144. verb = fmt.Sprintf("%s %s", verb, ss[0])
  145. }
  146. return verb, args
  147. }