gitea源码

serv.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. "strconv"
  13. "strings"
  14. "time"
  15. "unicode"
  16. asymkey_model "code.gitea.io/gitea/models/asymkey"
  17. git_model "code.gitea.io/gitea/models/git"
  18. "code.gitea.io/gitea/models/perm"
  19. "code.gitea.io/gitea/models/repo"
  20. "code.gitea.io/gitea/modules/git"
  21. "code.gitea.io/gitea/modules/git/gitcmd"
  22. "code.gitea.io/gitea/modules/json"
  23. "code.gitea.io/gitea/modules/lfstransfer"
  24. "code.gitea.io/gitea/modules/log"
  25. "code.gitea.io/gitea/modules/pprof"
  26. "code.gitea.io/gitea/modules/private"
  27. "code.gitea.io/gitea/modules/process"
  28. repo_module "code.gitea.io/gitea/modules/repository"
  29. "code.gitea.io/gitea/modules/setting"
  30. "code.gitea.io/gitea/services/lfs"
  31. "github.com/golang-jwt/jwt/v5"
  32. "github.com/kballard/go-shellquote"
  33. "github.com/urfave/cli/v3"
  34. )
  35. // CmdServ represents the available serv sub-command.
  36. var CmdServ = &cli.Command{
  37. Name: "serv",
  38. Usage: "(internal) Should only be called by SSH shell",
  39. Description: "Serv provides access auth for repositories",
  40. Hidden: true, // Internal commands shouldn't be visible in help
  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 _, err := os.Stat(setting.RepoRootPath); err != nil {
  60. _ = fail(ctx, "Unable to access repository path", "Unable to access repository path %q, err: %v", setting.RepoRootPath, err)
  61. return
  62. }
  63. if err := git.InitSimple(); err != nil {
  64. _ = fail(ctx, "Failed to init git", "Failed to init git, err: %v", err)
  65. }
  66. }
  67. // fail prints message to stdout, it's mainly used for git serv and git hook commands.
  68. // The output will be passed to git client and shown to user.
  69. func fail(ctx context.Context, userMessage, logMsgFmt string, args ...any) error {
  70. if userMessage == "" {
  71. userMessage = "Internal Server Error (no specific error)"
  72. }
  73. // There appears to be a chance to cause a zombie process and failure to read the Exit status
  74. // if nothing is outputted on stdout.
  75. _, _ = fmt.Fprintln(os.Stdout, "")
  76. // add extra empty lines to separate our message from other git errors to get more attention
  77. _, _ = fmt.Fprintln(os.Stderr, "error:")
  78. _, _ = fmt.Fprintln(os.Stderr, "error:", userMessage)
  79. _, _ = fmt.Fprintln(os.Stderr, "error:")
  80. if logMsgFmt != "" {
  81. logMsg := fmt.Sprintf(logMsgFmt, args...)
  82. if !setting.IsProd {
  83. _, _ = fmt.Fprintln(os.Stderr, "Gitea:", logMsg)
  84. }
  85. if unicode.IsPunct(rune(userMessage[len(userMessage)-1])) {
  86. logMsg = userMessage + " " + logMsg
  87. } else {
  88. logMsg = userMessage + ". " + logMsg
  89. }
  90. _ = private.SSHLog(ctx, true, logMsg)
  91. }
  92. return cli.Exit("", 1)
  93. }
  94. // handleCliResponseExtra handles the extra response from the cli sub-commands
  95. // If there is a user message it will be printed to stdout
  96. // If the command failed it will return an error (the error will be printed by cli framework)
  97. func handleCliResponseExtra(extra private.ResponseExtra) error {
  98. if extra.UserMsg != "" {
  99. _, _ = fmt.Fprintln(os.Stdout, extra.UserMsg)
  100. }
  101. if extra.HasError() {
  102. return cli.Exit(extra.Error, 1)
  103. }
  104. return nil
  105. }
  106. func getAccessMode(verb, lfsVerb string) perm.AccessMode {
  107. switch verb {
  108. case git.CmdVerbUploadPack, git.CmdVerbUploadArchive:
  109. return perm.AccessModeRead
  110. case git.CmdVerbReceivePack:
  111. return perm.AccessModeWrite
  112. case git.CmdVerbLfsAuthenticate, git.CmdVerbLfsTransfer:
  113. switch lfsVerb {
  114. case git.CmdSubVerbLfsUpload:
  115. return perm.AccessModeWrite
  116. case git.CmdSubVerbLfsDownload:
  117. return perm.AccessModeRead
  118. }
  119. }
  120. // should be unreachable
  121. setting.PanicInDevOrTesting("unknown verb: %s %s", verb, lfsVerb)
  122. return perm.AccessModeNone
  123. }
  124. func getLFSAuthToken(ctx context.Context, lfsVerb string, results *private.ServCommandResults) (string, error) {
  125. now := time.Now()
  126. claims := lfs.Claims{
  127. RegisteredClaims: jwt.RegisteredClaims{
  128. ExpiresAt: jwt.NewNumericDate(now.Add(setting.LFS.HTTPAuthExpiry)),
  129. NotBefore: jwt.NewNumericDate(now),
  130. },
  131. RepoID: results.RepoID,
  132. Op: lfsVerb,
  133. UserID: results.UserID,
  134. }
  135. token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
  136. // Sign and get the complete encoded token as a string using the secret
  137. tokenString, err := token.SignedString(setting.LFS.JWTSecretBytes)
  138. if err != nil {
  139. return "", fail(ctx, "Failed to sign JWT Token", "Failed to sign JWT token: %v", err)
  140. }
  141. return "Bearer " + tokenString, nil
  142. }
  143. func runServ(ctx context.Context, c *cli.Command) error {
  144. // FIXME: This needs to internationalised
  145. setup(ctx, c.Bool("debug"))
  146. if setting.SSH.Disabled {
  147. println("Gitea: SSH has been disabled")
  148. return nil
  149. }
  150. if c.NArg() < 1 {
  151. if err := cli.ShowSubcommandHelp(c); err != nil {
  152. fmt.Printf("error showing subcommand help: %v\n", err)
  153. }
  154. return nil
  155. }
  156. defer func() {
  157. if err := recover(); err != nil {
  158. _ = fail(ctx, "Internal Server Error", "Panic: %v\n%s", err, log.Stack(2))
  159. }
  160. }()
  161. keys := strings.Split(c.Args().First(), "-")
  162. if len(keys) != 2 || keys[0] != "key" {
  163. return fail(ctx, "Key ID format error", "Invalid key argument: %s", c.Args().First())
  164. }
  165. keyID, err := strconv.ParseInt(keys[1], 10, 64)
  166. if err != nil {
  167. return fail(ctx, "Key ID parsing error", "Invalid key argument: %s", c.Args().Get(1))
  168. }
  169. cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
  170. if len(cmd) == 0 {
  171. key, user, err := private.ServNoCommand(ctx, keyID)
  172. if err != nil {
  173. return fail(ctx, "Key check failed", "Failed to check provided key: %v", err)
  174. }
  175. switch key.Type {
  176. case asymkey_model.KeyTypeDeploy:
  177. println("Hi there! You've successfully authenticated with the deploy key named " + key.Name + ", but Gitea does not provide shell access.")
  178. case asymkey_model.KeyTypePrincipal:
  179. println("Hi there! You've successfully authenticated with the principal " + key.Content + ", but Gitea does not provide shell access.")
  180. default:
  181. println("Hi there, " + user.Name + "! You've successfully authenticated with the key named " + key.Name + ", but Gitea does not provide shell access.")
  182. }
  183. println("If this is unexpected, please log in with password and setup Gitea under another user.")
  184. return nil
  185. } else if c.Bool("debug") {
  186. log.Debug("SSH_ORIGINAL_COMMAND: %s", os.Getenv("SSH_ORIGINAL_COMMAND"))
  187. }
  188. sshCmdArgs, err := shellquote.Split(cmd)
  189. if err != nil {
  190. return fail(ctx, "Error parsing arguments", "Failed to parse arguments: %v", err)
  191. }
  192. if len(sshCmdArgs) < 2 {
  193. if git.DefaultFeatures().SupportProcReceive {
  194. // for AGit Flow
  195. if cmd == "ssh_info" {
  196. fmt.Print(`{"type":"agit","version":1}`)
  197. return nil
  198. }
  199. }
  200. return fail(ctx, "Too few arguments", "Too few arguments in cmd: %s", cmd)
  201. }
  202. repoPath := strings.TrimPrefix(sshCmdArgs[1], "/")
  203. repoPathFields := strings.SplitN(repoPath, "/", 2)
  204. if len(repoPathFields) != 2 {
  205. return fail(ctx, "Invalid repository path", "Invalid repository path: %v", repoPath)
  206. }
  207. username := repoPathFields[0]
  208. reponame := strings.TrimSuffix(repoPathFields[1], ".git") // “the-repo-name" or "the-repo-name.wiki"
  209. if !repo.IsValidSSHAccessRepoName(reponame) {
  210. return fail(ctx, "Invalid repo name", "Invalid repo name: %s", reponame)
  211. }
  212. if c.Bool("enable-pprof") {
  213. if err := os.MkdirAll(setting.PprofDataPath, os.ModePerm); err != nil {
  214. return fail(ctx, "Error while trying to create PPROF_DATA_PATH", "Error while trying to create PPROF_DATA_PATH: %v", err)
  215. }
  216. stopCPUProfiler, err := pprof.DumpCPUProfileForUsername(setting.PprofDataPath, username)
  217. if err != nil {
  218. return fail(ctx, "Unable to start CPU profiler", "Unable to start CPU profile: %v", err)
  219. }
  220. defer func() {
  221. stopCPUProfiler()
  222. err := pprof.DumpMemProfileForUsername(setting.PprofDataPath, username)
  223. if err != nil {
  224. _ = fail(ctx, "Unable to dump Mem profile", "Unable to dump Mem Profile: %v", err)
  225. }
  226. }()
  227. }
  228. verb, lfsVerb := sshCmdArgs[0], ""
  229. if !git.IsAllowedVerbForServe(verb) {
  230. return fail(ctx, "Unknown git command", "Unknown git command %s", verb)
  231. }
  232. if git.IsAllowedVerbForServeLfs(verb) {
  233. if !setting.LFS.StartServer {
  234. return fail(ctx, "LFS Server is not enabled", "")
  235. }
  236. if verb == git.CmdVerbLfsTransfer && !setting.LFS.AllowPureSSH {
  237. return fail(ctx, "LFS SSH transfer is not enabled", "")
  238. }
  239. if len(sshCmdArgs) > 2 {
  240. lfsVerb = sshCmdArgs[2]
  241. }
  242. }
  243. requestedMode := getAccessMode(verb, lfsVerb)
  244. results, extra := private.ServCommand(ctx, keyID, username, reponame, requestedMode, verb, lfsVerb)
  245. if extra.HasError() {
  246. return fail(ctx, extra.UserMsg, "ServCommand failed: %s", extra.Error)
  247. }
  248. // LowerCase and trim the repoPath as that's how they are stored.
  249. // This should be done after splitting the repoPath into username and reponame
  250. // so that username and reponame are not affected.
  251. repoPath = strings.ToLower(results.OwnerName + "/" + results.RepoName + ".git")
  252. // LFS SSH protocol
  253. if verb == git.CmdVerbLfsTransfer {
  254. token, err := getLFSAuthToken(ctx, lfsVerb, results)
  255. if err != nil {
  256. return err
  257. }
  258. return lfstransfer.Main(ctx, repoPath, lfsVerb, token)
  259. }
  260. // LFS token authentication
  261. if verb == git.CmdVerbLfsAuthenticate {
  262. url := fmt.Sprintf("%s%s/%s.git/info/lfs", setting.AppURL, url.PathEscape(results.OwnerName), url.PathEscape(results.RepoName))
  263. token, err := getLFSAuthToken(ctx, lfsVerb, results)
  264. if err != nil {
  265. return err
  266. }
  267. tokenAuthentication := &git_model.LFSTokenResponse{
  268. Header: make(map[string]string),
  269. Href: url,
  270. }
  271. tokenAuthentication.Header["Authorization"] = token
  272. enc := json.NewEncoder(os.Stdout)
  273. err = enc.Encode(tokenAuthentication)
  274. if err != nil {
  275. return fail(ctx, "Failed to encode LFS json response", "Failed to encode LFS json response: %v", err)
  276. }
  277. return nil
  278. }
  279. var command *exec.Cmd
  280. gitBinPath := filepath.Dir(gitcmd.GitExecutable) // e.g. /usr/bin
  281. gitBinVerb := filepath.Join(gitBinPath, verb) // e.g. /usr/bin/git-upload-pack
  282. if _, err := os.Stat(gitBinVerb); err != nil {
  283. // if the command "git-upload-pack" doesn't exist, try to split "git-upload-pack" to use the sub-command with git
  284. // ps: Windows only has "git.exe" in the bin path, so Windows always uses this way
  285. verbFields := strings.SplitN(verb, "-", 2)
  286. if len(verbFields) == 2 {
  287. // use git binary with the sub-command part: "C:\...\bin\git.exe", "upload-pack", ...
  288. command = exec.CommandContext(ctx, gitcmd.GitExecutable, verbFields[1], repoPath)
  289. }
  290. }
  291. if command == nil {
  292. // by default, use the verb (it has been checked above by allowedCommands)
  293. command = exec.CommandContext(ctx, gitBinVerb, repoPath)
  294. }
  295. process.SetSysProcAttribute(command)
  296. command.Dir = setting.RepoRootPath
  297. command.Stdout = os.Stdout
  298. command.Stdin = os.Stdin
  299. command.Stderr = os.Stderr
  300. command.Env = append(command.Env, os.Environ()...)
  301. command.Env = append(command.Env,
  302. repo_module.EnvRepoIsWiki+"="+strconv.FormatBool(results.IsWiki),
  303. repo_module.EnvRepoName+"="+results.RepoName,
  304. repo_module.EnvRepoUsername+"="+results.OwnerName,
  305. repo_module.EnvPusherName+"="+results.UserName,
  306. repo_module.EnvPusherEmail+"="+results.UserEmail,
  307. repo_module.EnvPusherID+"="+strconv.FormatInt(results.UserID, 10),
  308. repo_module.EnvRepoID+"="+strconv.FormatInt(results.RepoID, 10),
  309. repo_module.EnvPRID+"="+strconv.Itoa(0),
  310. repo_module.EnvDeployKeyID+"="+strconv.FormatInt(results.DeployKeyID, 10),
  311. repo_module.EnvKeyID+"="+strconv.FormatInt(results.KeyID, 10),
  312. repo_module.EnvAppURL+"="+setting.AppURL,
  313. )
  314. // to avoid breaking, here only use the minimal environment variables for the "gitea serv" command.
  315. // it could be re-considered whether to use the same git.CommonGitCmdEnvs() as "git" command later.
  316. command.Env = append(command.Env, gitcmd.CommonCmdServEnvs()...)
  317. if err = command.Run(); err != nil {
  318. return fail(ctx, "Failed to execute git command", "Failed to execute git command: %v", err)
  319. }
  320. // Update user key activity.
  321. if results.KeyID > 0 {
  322. if err = private.UpdatePublicKeyInRepo(ctx, results.KeyID, results.RepoID); err != nil {
  323. return fail(ctx, "Failed to update public key", "UpdatePublicKeyInRepo: %v", err)
  324. }
  325. }
  326. return nil
  327. }