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.

serv.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package cmd
  5. import (
  6. "context"
  7. "fmt"
  8. "net/url"
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. "regexp"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "unicode"
  17. asymkey_model "code.gitea.io/gitea/models/asymkey"
  18. git_model "code.gitea.io/gitea/models/git"
  19. "code.gitea.io/gitea/models/perm"
  20. "code.gitea.io/gitea/modules/git"
  21. "code.gitea.io/gitea/modules/json"
  22. "code.gitea.io/gitea/modules/log"
  23. "code.gitea.io/gitea/modules/pprof"
  24. "code.gitea.io/gitea/modules/private"
  25. "code.gitea.io/gitea/modules/process"
  26. repo_module "code.gitea.io/gitea/modules/repository"
  27. "code.gitea.io/gitea/modules/setting"
  28. "code.gitea.io/gitea/services/lfs"
  29. "github.com/golang-jwt/jwt/v4"
  30. "github.com/kballard/go-shellquote"
  31. "github.com/urfave/cli"
  32. )
  33. const (
  34. lfsAuthenticateVerb = "git-lfs-authenticate"
  35. )
  36. // CmdServ represents the available serv sub-command.
  37. var CmdServ = cli.Command{
  38. Name: "serv",
  39. Usage: "This command should only be called by SSH shell",
  40. Description: "Serv provides access auth for repositories",
  41. Action: runServ,
  42. Flags: []cli.Flag{
  43. cli.BoolFlag{
  44. Name: "enable-pprof",
  45. },
  46. cli.BoolFlag{
  47. Name: "debug",
  48. },
  49. },
  50. }
  51. func setup(ctx context.Context, debug bool) {
  52. if debug {
  53. setupConsoleLogger(log.TRACE, false, os.Stderr)
  54. } else {
  55. setupConsoleLogger(log.FATAL, false, os.Stderr)
  56. }
  57. setting.Init(&setting.Options{})
  58. if debug {
  59. setting.RunMode = "dev"
  60. }
  61. // Check if setting.RepoRootPath exists. It could be the case that it doesn't exist, this can happen when
  62. // `[repository]` `ROOT` is a relative path and $GITEA_WORK_DIR isn't passed to the SSH connection.
  63. if _, err := os.Stat(setting.RepoRootPath); err != nil {
  64. if os.IsNotExist(err) {
  65. _ = fail(ctx, "Incorrect configuration, no repository directory.", "Directory `[repository].ROOT` %q was not found, please check if $GITEA_WORK_DIR is passed to the SSH connection or make `[repository].ROOT` an absolute value.", setting.RepoRootPath)
  66. } else {
  67. _ = fail(ctx, "Incorrect configuration, repository directory is inaccessible", "Directory `[repository].ROOT` %q is inaccessible. err: %v", setting.RepoRootPath, err)
  68. }
  69. return
  70. }
  71. if err := git.InitSimple(context.Background()); err != nil {
  72. _ = fail(ctx, "Failed to init git", "Failed to init git, err: %v", err)
  73. }
  74. }
  75. var (
  76. allowedCommands = map[string]perm.AccessMode{
  77. "git-upload-pack": perm.AccessModeRead,
  78. "git-upload-archive": perm.AccessModeRead,
  79. "git-receive-pack": perm.AccessModeWrite,
  80. lfsAuthenticateVerb: perm.AccessModeNone,
  81. }
  82. alphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`)
  83. )
  84. // fail prints message to stdout, it's mainly used for git serv and git hook commands.
  85. // The output will be passed to git client and shown to user.
  86. func fail(ctx context.Context, userMessage, logMsgFmt string, args ...interface{}) error {
  87. if userMessage == "" {
  88. userMessage = "Internal Server Error (no specific error)"
  89. }
  90. // There appears to be a chance to cause a zombie process and failure to read the Exit status
  91. // if nothing is outputted on stdout.
  92. _, _ = fmt.Fprintln(os.Stdout, "")
  93. _, _ = fmt.Fprintln(os.Stderr, "Gitea:", userMessage)
  94. if logMsgFmt != "" {
  95. logMsg := fmt.Sprintf(logMsgFmt, args...)
  96. if !setting.IsProd {
  97. _, _ = fmt.Fprintln(os.Stderr, "Gitea:", logMsg)
  98. }
  99. if userMessage != "" {
  100. if unicode.IsPunct(rune(userMessage[len(userMessage)-1])) {
  101. logMsg = userMessage + " " + logMsg
  102. } else {
  103. logMsg = userMessage + ". " + logMsg
  104. }
  105. }
  106. _ = private.SSHLog(ctx, true, logMsg)
  107. }
  108. return cli.NewExitError("", 1)
  109. }
  110. // handleCliResponseExtra handles the extra response from the cli sub-commands
  111. // If there is a user message it will be printed to stdout
  112. // If the command failed it will return an error (the error will be printed by cli framework)
  113. func handleCliResponseExtra(extra private.ResponseExtra) error {
  114. if extra.UserMsg != "" {
  115. _, _ = fmt.Fprintln(os.Stdout, extra.UserMsg)
  116. }
  117. if extra.HasError() {
  118. return cli.NewExitError(extra.Error, 1)
  119. }
  120. return nil
  121. }
  122. func runServ(c *cli.Context) error {
  123. ctx, cancel := installSignals()
  124. defer cancel()
  125. // FIXME: This needs to internationalised
  126. setup(ctx, c.Bool("debug"))
  127. if setting.SSH.Disabled {
  128. println("Gitea: SSH has been disabled")
  129. return nil
  130. }
  131. if len(c.Args()) < 1 {
  132. if err := cli.ShowSubcommandHelp(c); err != nil {
  133. fmt.Printf("error showing subcommand help: %v\n", err)
  134. }
  135. return nil
  136. }
  137. keys := strings.Split(c.Args()[0], "-")
  138. if len(keys) != 2 || keys[0] != "key" {
  139. return fail(ctx, "Key ID format error", "Invalid key argument: %s", c.Args()[0])
  140. }
  141. keyID, err := strconv.ParseInt(keys[1], 10, 64)
  142. if err != nil {
  143. return fail(ctx, "Key ID parsing error", "Invalid key argument: %s", c.Args()[1])
  144. }
  145. cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
  146. if len(cmd) == 0 {
  147. key, user, err := private.ServNoCommand(ctx, keyID)
  148. if err != nil {
  149. return fail(ctx, "Key check failed", "Failed to check provided key: %v", err)
  150. }
  151. switch key.Type {
  152. case asymkey_model.KeyTypeDeploy:
  153. println("Hi there! You've successfully authenticated with the deploy key named " + key.Name + ", but Gitea does not provide shell access.")
  154. case asymkey_model.KeyTypePrincipal:
  155. println("Hi there! You've successfully authenticated with the principal " + key.Content + ", but Gitea does not provide shell access.")
  156. default:
  157. println("Hi there, " + user.Name + "! You've successfully authenticated with the key named " + key.Name + ", but Gitea does not provide shell access.")
  158. }
  159. println("If this is unexpected, please log in with password and setup Gitea under another user.")
  160. return nil
  161. } else if c.Bool("debug") {
  162. log.Debug("SSH_ORIGINAL_COMMAND: %s", os.Getenv("SSH_ORIGINAL_COMMAND"))
  163. }
  164. words, err := shellquote.Split(cmd)
  165. if err != nil {
  166. return fail(ctx, "Error parsing arguments", "Failed to parse arguments: %v", err)
  167. }
  168. if len(words) < 2 {
  169. if git.CheckGitVersionAtLeast("2.29") == nil {
  170. // for AGit Flow
  171. if cmd == "ssh_info" {
  172. fmt.Print(`{"type":"gitea","version":1}`)
  173. return nil
  174. }
  175. }
  176. return fail(ctx, "Too few arguments", "Too few arguments in cmd: %s", cmd)
  177. }
  178. verb := words[0]
  179. repoPath := words[1]
  180. if repoPath[0] == '/' {
  181. repoPath = repoPath[1:]
  182. }
  183. var lfsVerb string
  184. if verb == lfsAuthenticateVerb {
  185. if !setting.LFS.StartServer {
  186. return fail(ctx, "Unknown git command", "LFS authentication request over SSH denied, LFS support is disabled")
  187. }
  188. if len(words) > 2 {
  189. lfsVerb = words[2]
  190. }
  191. }
  192. // LowerCase and trim the repoPath as that's how they are stored.
  193. repoPath = strings.ToLower(strings.TrimSpace(repoPath))
  194. rr := strings.SplitN(repoPath, "/", 2)
  195. if len(rr) != 2 {
  196. return fail(ctx, "Invalid repository path", "Invalid repository path: %v", repoPath)
  197. }
  198. username := strings.ToLower(rr[0])
  199. reponame := strings.ToLower(strings.TrimSuffix(rr[1], ".git"))
  200. if alphaDashDotPattern.MatchString(reponame) {
  201. return fail(ctx, "Invalid repo name", "Invalid repo name: %s", reponame)
  202. }
  203. if c.Bool("enable-pprof") {
  204. if err := os.MkdirAll(setting.PprofDataPath, os.ModePerm); err != nil {
  205. return fail(ctx, "Error while trying to create PPROF_DATA_PATH", "Error while trying to create PPROF_DATA_PATH: %v", err)
  206. }
  207. stopCPUProfiler, err := pprof.DumpCPUProfileForUsername(setting.PprofDataPath, username)
  208. if err != nil {
  209. return fail(ctx, "Unable to start CPU profiler", "Unable to start CPU profile: %v", err)
  210. }
  211. defer func() {
  212. stopCPUProfiler()
  213. err := pprof.DumpMemProfileForUsername(setting.PprofDataPath, username)
  214. if err != nil {
  215. _ = fail(ctx, "Unable to dump Mem profile", "Unable to dump Mem Profile: %v", err)
  216. }
  217. }()
  218. }
  219. requestedMode, has := allowedCommands[verb]
  220. if !has {
  221. return fail(ctx, "Unknown git command", "Unknown git command %s", verb)
  222. }
  223. if verb == lfsAuthenticateVerb {
  224. if lfsVerb == "upload" {
  225. requestedMode = perm.AccessModeWrite
  226. } else if lfsVerb == "download" {
  227. requestedMode = perm.AccessModeRead
  228. } else {
  229. return fail(ctx, "Unknown LFS verb", "Unknown lfs verb %s", lfsVerb)
  230. }
  231. }
  232. results, extra := private.ServCommand(ctx, keyID, username, reponame, requestedMode, verb, lfsVerb)
  233. if extra.HasError() {
  234. return fail(ctx, extra.UserMsg, "ServCommand failed: %s", extra.Error)
  235. }
  236. // LFS token authentication
  237. if verb == lfsAuthenticateVerb {
  238. url := fmt.Sprintf("%s%s/%s.git/info/lfs", setting.AppURL, url.PathEscape(results.OwnerName), url.PathEscape(results.RepoName))
  239. now := time.Now()
  240. claims := lfs.Claims{
  241. RegisteredClaims: jwt.RegisteredClaims{
  242. ExpiresAt: jwt.NewNumericDate(now.Add(setting.LFS.HTTPAuthExpiry)),
  243. NotBefore: jwt.NewNumericDate(now),
  244. },
  245. RepoID: results.RepoID,
  246. Op: lfsVerb,
  247. UserID: results.UserID,
  248. }
  249. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  250. // Sign and get the complete encoded token as a string using the secret
  251. tokenString, err := token.SignedString(setting.LFS.JWTSecretBytes)
  252. if err != nil {
  253. return fail(ctx, "Failed to sign JWT Token", "Failed to sign JWT token: %v", err)
  254. }
  255. tokenAuthentication := &git_model.LFSTokenResponse{
  256. Header: make(map[string]string),
  257. Href: url,
  258. }
  259. tokenAuthentication.Header["Authorization"] = fmt.Sprintf("Bearer %s", tokenString)
  260. enc := json.NewEncoder(os.Stdout)
  261. err = enc.Encode(tokenAuthentication)
  262. if err != nil {
  263. return fail(ctx, "Failed to encode LFS json response", "Failed to encode LFS json response: %v", err)
  264. }
  265. return nil
  266. }
  267. var gitcmd *exec.Cmd
  268. gitBinPath := filepath.Dir(git.GitExecutable) // e.g. /usr/bin
  269. gitBinVerb := filepath.Join(gitBinPath, verb) // e.g. /usr/bin/git-upload-pack
  270. if _, err := os.Stat(gitBinVerb); err != nil {
  271. // if the command "git-upload-pack" doesn't exist, try to split "git-upload-pack" to use the sub-command with git
  272. // ps: Windows only has "git.exe" in the bin path, so Windows always uses this way
  273. verbFields := strings.SplitN(verb, "-", 2)
  274. if len(verbFields) == 2 {
  275. // use git binary with the sub-command part: "C:\...\bin\git.exe", "upload-pack", ...
  276. gitcmd = exec.CommandContext(ctx, git.GitExecutable, verbFields[1], repoPath)
  277. }
  278. }
  279. if gitcmd == nil {
  280. // by default, use the verb (it has been checked above by allowedCommands)
  281. gitcmd = exec.CommandContext(ctx, gitBinVerb, repoPath)
  282. }
  283. process.SetSysProcAttribute(gitcmd)
  284. gitcmd.Dir = setting.RepoRootPath
  285. gitcmd.Stdout = os.Stdout
  286. gitcmd.Stdin = os.Stdin
  287. gitcmd.Stderr = os.Stderr
  288. gitcmd.Env = append(gitcmd.Env, os.Environ()...)
  289. gitcmd.Env = append(gitcmd.Env,
  290. repo_module.EnvRepoIsWiki+"="+strconv.FormatBool(results.IsWiki),
  291. repo_module.EnvRepoName+"="+results.RepoName,
  292. repo_module.EnvRepoUsername+"="+results.OwnerName,
  293. repo_module.EnvPusherName+"="+results.UserName,
  294. repo_module.EnvPusherEmail+"="+results.UserEmail,
  295. repo_module.EnvPusherID+"="+strconv.FormatInt(results.UserID, 10),
  296. repo_module.EnvRepoID+"="+strconv.FormatInt(results.RepoID, 10),
  297. repo_module.EnvPRID+"="+fmt.Sprintf("%d", 0),
  298. repo_module.EnvDeployKeyID+"="+fmt.Sprintf("%d", results.DeployKeyID),
  299. repo_module.EnvKeyID+"="+fmt.Sprintf("%d", results.KeyID),
  300. repo_module.EnvAppURL+"="+setting.AppURL,
  301. )
  302. // to avoid breaking, here only use the minimal environment variables for the "gitea serv" command.
  303. // it could be re-considered whether to use the same git.CommonGitCmdEnvs() as "git" command later.
  304. gitcmd.Env = append(gitcmd.Env, git.CommonCmdServEnvs()...)
  305. if err = gitcmd.Run(); err != nil {
  306. return fail(ctx, "Failed to execute git command", "Failed to execute git command: %v", err)
  307. }
  308. // Update user key activity.
  309. if results.KeyID > 0 {
  310. if err = private.UpdatePublicKeyInRepo(ctx, results.KeyID, results.RepoID); err != nil {
  311. return fail(ctx, "Failed to update public key", "UpdatePublicKeyInRepo: %v", err)
  312. }
  313. }
  314. return nil
  315. }