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

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