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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. rr := strings.SplitN(repoPath, "/", 2)
  194. if len(rr) != 2 {
  195. return fail(ctx, "Invalid repository path", "Invalid repository path: %v", repoPath)
  196. }
  197. username := rr[0]
  198. reponame := strings.TrimSuffix(rr[1], ".git")
  199. // LowerCase and trim the repoPath as that's how they are stored.
  200. // This should be done after splitting the repoPath into username and reponame
  201. // so that username and reponame are not affected.
  202. repoPath = strings.ToLower(strings.TrimSpace(repoPath))
  203. if alphaDashDotPattern.MatchString(reponame) {
  204. return fail(ctx, "Invalid repo name", "Invalid repo name: %s", reponame)
  205. }
  206. if c.Bool("enable-pprof") {
  207. if err := os.MkdirAll(setting.PprofDataPath, os.ModePerm); err != nil {
  208. return fail(ctx, "Error while trying to create PPROF_DATA_PATH", "Error while trying to create PPROF_DATA_PATH: %v", err)
  209. }
  210. stopCPUProfiler, err := pprof.DumpCPUProfileForUsername(setting.PprofDataPath, username)
  211. if err != nil {
  212. return fail(ctx, "Unable to start CPU profiler", "Unable to start CPU profile: %v", err)
  213. }
  214. defer func() {
  215. stopCPUProfiler()
  216. err := pprof.DumpMemProfileForUsername(setting.PprofDataPath, username)
  217. if err != nil {
  218. _ = fail(ctx, "Unable to dump Mem profile", "Unable to dump Mem Profile: %v", err)
  219. }
  220. }()
  221. }
  222. requestedMode, has := allowedCommands[verb]
  223. if !has {
  224. return fail(ctx, "Unknown git command", "Unknown git command %s", verb)
  225. }
  226. if verb == lfsAuthenticateVerb {
  227. if lfsVerb == "upload" {
  228. requestedMode = perm.AccessModeWrite
  229. } else if lfsVerb == "download" {
  230. requestedMode = perm.AccessModeRead
  231. } else {
  232. return fail(ctx, "Unknown LFS verb", "Unknown lfs verb %s", lfsVerb)
  233. }
  234. }
  235. results, extra := private.ServCommand(ctx, keyID, username, reponame, requestedMode, verb, lfsVerb)
  236. if extra.HasError() {
  237. return fail(ctx, extra.UserMsg, "ServCommand failed: %s", extra.Error)
  238. }
  239. // LFS token authentication
  240. if verb == lfsAuthenticateVerb {
  241. url := fmt.Sprintf("%s%s/%s.git/info/lfs", setting.AppURL, url.PathEscape(results.OwnerName), url.PathEscape(results.RepoName))
  242. now := time.Now()
  243. claims := lfs.Claims{
  244. RegisteredClaims: jwt.RegisteredClaims{
  245. ExpiresAt: jwt.NewNumericDate(now.Add(setting.LFS.HTTPAuthExpiry)),
  246. NotBefore: jwt.NewNumericDate(now),
  247. },
  248. RepoID: results.RepoID,
  249. Op: lfsVerb,
  250. UserID: results.UserID,
  251. }
  252. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  253. // Sign and get the complete encoded token as a string using the secret
  254. tokenString, err := token.SignedString(setting.LFS.JWTSecretBytes)
  255. if err != nil {
  256. return fail(ctx, "Failed to sign JWT Token", "Failed to sign JWT token: %v", err)
  257. }
  258. tokenAuthentication := &git_model.LFSTokenResponse{
  259. Header: make(map[string]string),
  260. Href: url,
  261. }
  262. tokenAuthentication.Header["Authorization"] = fmt.Sprintf("Bearer %s", tokenString)
  263. enc := json.NewEncoder(os.Stdout)
  264. err = enc.Encode(tokenAuthentication)
  265. if err != nil {
  266. return fail(ctx, "Failed to encode LFS json response", "Failed to encode LFS json response: %v", err)
  267. }
  268. return nil
  269. }
  270. var gitcmd *exec.Cmd
  271. gitBinPath := filepath.Dir(git.GitExecutable) // e.g. /usr/bin
  272. gitBinVerb := filepath.Join(gitBinPath, verb) // e.g. /usr/bin/git-upload-pack
  273. if _, err := os.Stat(gitBinVerb); err != nil {
  274. // if the command "git-upload-pack" doesn't exist, try to split "git-upload-pack" to use the sub-command with git
  275. // ps: Windows only has "git.exe" in the bin path, so Windows always uses this way
  276. verbFields := strings.SplitN(verb, "-", 2)
  277. if len(verbFields) == 2 {
  278. // use git binary with the sub-command part: "C:\...\bin\git.exe", "upload-pack", ...
  279. gitcmd = exec.CommandContext(ctx, git.GitExecutable, verbFields[1], repoPath)
  280. }
  281. }
  282. if gitcmd == nil {
  283. // by default, use the verb (it has been checked above by allowedCommands)
  284. gitcmd = exec.CommandContext(ctx, gitBinVerb, repoPath)
  285. }
  286. process.SetSysProcAttribute(gitcmd)
  287. gitcmd.Dir = setting.RepoRootPath
  288. gitcmd.Stdout = os.Stdout
  289. gitcmd.Stdin = os.Stdin
  290. gitcmd.Stderr = os.Stderr
  291. gitcmd.Env = append(gitcmd.Env, os.Environ()...)
  292. gitcmd.Env = append(gitcmd.Env,
  293. repo_module.EnvRepoIsWiki+"="+strconv.FormatBool(results.IsWiki),
  294. repo_module.EnvRepoName+"="+results.RepoName,
  295. repo_module.EnvRepoUsername+"="+results.OwnerName,
  296. repo_module.EnvPusherName+"="+results.UserName,
  297. repo_module.EnvPusherEmail+"="+results.UserEmail,
  298. repo_module.EnvPusherID+"="+strconv.FormatInt(results.UserID, 10),
  299. repo_module.EnvRepoID+"="+strconv.FormatInt(results.RepoID, 10),
  300. repo_module.EnvPRID+"="+fmt.Sprintf("%d", 0),
  301. repo_module.EnvDeployKeyID+"="+fmt.Sprintf("%d", results.DeployKeyID),
  302. repo_module.EnvKeyID+"="+fmt.Sprintf("%d", results.KeyID),
  303. repo_module.EnvAppURL+"="+setting.AppURL,
  304. )
  305. // to avoid breaking, here only use the minimal environment variables for the "gitea serv" command.
  306. // it could be re-considered whether to use the same git.CommonGitCmdEnvs() as "git" command later.
  307. gitcmd.Env = append(gitcmd.Env, git.CommonCmdServEnvs()...)
  308. if err = gitcmd.Run(); err != nil {
  309. return fail(ctx, "Failed to execute git command", "Failed to execute git command: %v", err)
  310. }
  311. // Update user key activity.
  312. if results.KeyID > 0 {
  313. if err = private.UpdatePublicKeyInRepo(ctx, results.KeyID, results.RepoID); err != nil {
  314. return fail(ctx, "Failed to update public key", "UpdatePublicKeyInRepo: %v", err)
  315. }
  316. }
  317. return nil
  318. }