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

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