Files

349 lines
12 KiB
Go
Raw Permalink Normal View History

2014-04-10 14:20:58 -04:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2016 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2014-04-10 14:20:58 -04:00
2014-05-01 21:21:46 -04:00
package cmd
2014-04-10 14:20:58 -04:00
import (
"context"
2014-04-10 14:20:58 -04:00
"fmt"
"net/url"
2014-04-10 14:20:58 -04:00
"os"
"os/exec"
"path/filepath"
"strconv"
2014-04-10 14:20:58 -04:00
"strings"
"unicode"
2014-04-10 14:20:58 -04:00
2021-12-10 16:14:24 +08:00
asymkey_model "code.gitea.io/gitea/models/asymkey"
2022-06-12 23:51:54 +08:00
git_model "code.gitea.io/gitea/models/git"
2021-11-28 19:58:28 +08:00
"code.gitea.io/gitea/models/perm"
2025-11-02 00:52:59 -07:00
repo_model "code.gitea.io/gitea/models/repo"
2021-07-28 17:42:56 +08:00
"code.gitea.io/gitea/modules/git"
2025-09-15 23:33:12 -07:00
"code.gitea.io/gitea/modules/git/gitcmd"
"code.gitea.io/gitea/modules/json"
2024-09-27 19:57:37 +05:30
"code.gitea.io/gitea/modules/lfstransfer"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/pprof"
"code.gitea.io/gitea/modules/private"
2022-06-03 15:36:18 +01:00
"code.gitea.io/gitea/modules/process"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
2021-04-09 00:25:57 +02:00
"code.gitea.io/gitea/services/lfs"
"github.com/kballard/go-shellquote"
2025-06-10 14:35:12 +02:00
"github.com/urfave/cli/v3"
2014-04-10 14:20:58 -04:00
)
func newServCommand() *cli.Command {
return &cli.Command{
Name: "serv",
Usage: "(internal) Should only be called by SSH shell",
Description: "Serv provides access auth for repositories",
Hidden: true, // Internal commands shouldn't be visible in help
Before: PrepareConsoleLoggerLevel(log.FATAL),
Action: runServ,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "enable-pprof",
},
&cli.BoolFlag{
Name: "debug",
},
},
}
2014-04-10 14:20:58 -04:00
}
func setup(ctx context.Context, debug bool) {
if debug {
2023-05-22 06:35:11 +08:00
setupConsoleLogger(log.TRACE, false, os.Stderr)
} else {
2023-05-22 06:35:11 +08:00
setupConsoleLogger(log.FATAL, false, os.Stderr)
}
2023-06-21 13:50:26 +08:00
setting.MustInstalled()
if _, err := os.Stat(setting.RepoRootPath); err != nil {
_ = fail(ctx, "Unable to access repository path", "Unable to access repository path %q, err: %v", setting.RepoRootPath, err)
return
}
2025-08-27 19:00:01 +08:00
if err := git.InitSimple(); err != nil {
_ = fail(ctx, "Failed to init git", "Failed to init git, err: %v", err)
}
2014-05-21 21:37:13 -04:00
}
// fail prints message to stdout, it's mainly used for git serv and git hook commands.
// The output will be passed to git client and shown to user.
2023-07-04 20:36:08 +02:00
func fail(ctx context.Context, userMessage, logMsgFmt string, args ...any) error {
if userMessage == "" {
userMessage = "Internal Server Error (no specific error)"
}
// There appears to be a chance to cause a zombie process and failure to read the Exit status
// if nothing is outputted on stdout.
_, _ = fmt.Fprintln(os.Stdout, "")
// add extra empty lines to separate our message from other git errors to get more attention
_, _ = fmt.Fprintln(os.Stderr, "error:")
_, _ = fmt.Fprintln(os.Stderr, "error:", userMessage)
_, _ = fmt.Fprintln(os.Stderr, "error:")
2015-11-08 14:31:49 -05:00
if logMsgFmt != "" {
logMsg := fmt.Sprintf(logMsgFmt, args...)
if !setting.IsProd {
_, _ = fmt.Fprintln(os.Stderr, "Gitea:", logMsg)
}
if unicode.IsPunct(rune(userMessage[len(userMessage)-1])) {
logMsg = userMessage + " " + logMsg
} else {
logMsg = userMessage + ". " + logMsg
}
_ = private.SSHLog(ctx, true, logMsg)
2015-11-08 14:31:49 -05:00
}
2023-07-21 17:28:19 +08:00
return cli.Exit("", 1)
}
2015-11-08 14:31:49 -05:00
// handleCliResponseExtra handles the extra response from the cli sub-commands
// If there is a user message it will be printed to stdout
// If the command failed it will return an error (the error will be printed by cli framework)
func handleCliResponseExtra(extra private.ResponseExtra) error {
if extra.UserMsg != "" {
_, _ = fmt.Fprintln(os.Stdout, extra.UserMsg)
}
if extra.HasError() {
2023-07-21 17:28:19 +08:00
return cli.Exit(extra.Error, 1)
}
return nil
2015-08-06 22:48:11 +08:00
}
2024-09-27 19:57:37 +05:30
func getAccessMode(verb, lfsVerb string) perm.AccessMode {
switch verb {
case git.CmdVerbUploadPack, git.CmdVerbUploadArchive:
2024-09-27 19:57:37 +05:30
return perm.AccessModeRead
case git.CmdVerbReceivePack:
2024-09-27 19:57:37 +05:30
return perm.AccessModeWrite
case git.CmdVerbLfsAuthenticate, git.CmdVerbLfsTransfer:
2024-09-27 19:57:37 +05:30
switch lfsVerb {
case git.CmdSubVerbLfsUpload:
2024-09-27 19:57:37 +05:30
return perm.AccessModeWrite
case git.CmdSubVerbLfsDownload:
2024-09-27 19:57:37 +05:30
return perm.AccessModeRead
}
}
// should be unreachable
setting.PanicInDevOrTesting("unknown verb: %s %s", verb, lfsVerb)
2024-09-27 19:57:37 +05:30
return perm.AccessModeNone
}
2025-06-10 14:35:12 +02:00
func runServ(ctx context.Context, c *cli.Command) error {
// FIXME: This needs to internationalised
setup(ctx, c.Bool("debug"))
2014-04-10 14:20:58 -04:00
2016-02-27 20:48:39 -05:00
if setting.SSH.Disabled {
println("Gitea: SSH has been disabled")
2016-05-12 14:32:28 -04:00
return nil
2016-02-21 21:55:59 -05:00
}
2023-07-21 17:28:19 +08:00
if c.NArg() < 1 {
2019-06-12 21:41:28 +02:00
if err := cli.ShowSubcommandHelp(c); err != nil {
fmt.Printf("error showing subcommand help: %v\n", err)
}
return nil
2015-02-09 12:32:42 +02:00
}
2015-02-16 16:38:01 +02:00
defer func() {
if err := recover(); err != nil {
_ = fail(ctx, "Internal Server Error", "Panic: %v\n%s", err, log.Stack(2))
}
}()
2023-07-21 17:28:19 +08:00
keys := strings.Split(c.Args().First(), "-")
if len(keys) != 2 || keys[0] != "key" {
2023-07-21 17:28:19 +08:00
return fail(ctx, "Key ID format error", "Invalid key argument: %s", c.Args().First())
}
2020-12-25 09:59:32 +00:00
keyID, err := strconv.ParseInt(keys[1], 10, 64)
if err != nil {
2023-07-21 17:28:19 +08:00
return fail(ctx, "Key ID parsing error", "Invalid key argument: %s", c.Args().Get(1))
2020-12-25 09:59:32 +00:00
}
2014-04-10 14:20:58 -04:00
cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
2015-08-05 11:14:17 +08:00
if len(cmd) == 0 {
key, user, err := private.ServNoCommand(ctx, keyID)
if err != nil {
return fail(ctx, "Key check failed", "Failed to check provided key: %v", err)
}
2020-10-11 02:38:09 +02:00
switch key.Type {
2021-12-10 16:14:24 +08:00
case asymkey_model.KeyTypeDeploy:
println("Hi there! You've successfully authenticated with the deploy key named " + key.Name + ", but Gitea does not provide shell access.")
2021-12-10 16:14:24 +08:00
case asymkey_model.KeyTypePrincipal:
2020-10-11 02:38:09 +02:00
println("Hi there! You've successfully authenticated with the principal " + key.Content + ", but Gitea does not provide shell access.")
default:
2019-08-11 14:15:58 +02:00
println("Hi there, " + user.Name + "! You've successfully authenticated with the key named " + key.Name + ", but Gitea does not provide shell access.")
}
println("If this is unexpected, please log in with password and setup Gitea under another user.")
2016-05-12 14:32:28 -04:00
return nil
} else if c.Bool("debug") {
log.Debug("SSH_ORIGINAL_COMMAND: %s", os.Getenv("SSH_ORIGINAL_COMMAND"))
2014-04-10 14:20:58 -04:00
}
sshCmdArgs, err := shellquote.Split(cmd)
if err != nil {
return fail(ctx, "Error parsing arguments", "Failed to parse arguments: %v", err)
}
if len(sshCmdArgs) < 2 {
if git.DefaultFeatures().SupportProcReceive {
2021-07-28 17:42:56 +08:00
// for AGit Flow
if cmd == "ssh_info" {
2025-06-13 00:19:24 +08:00
fmt.Print(`{"type":"agit","version":1}`)
2021-07-28 17:42:56 +08:00
return nil
}
}
return fail(ctx, "Too few arguments", "Too few arguments in cmd: %s", cmd)
}
repoPath := strings.TrimPrefix(sshCmdArgs[1], "/")
repoPathFields := strings.SplitN(repoPath, "/", 2)
if len(repoPathFields) != 2 {
return fail(ctx, "Invalid repository path", "Invalid repository path: %v", repoPath)
2014-04-10 14:20:58 -04:00
}
username := repoPathFields[0]
reponame := strings.TrimSuffix(repoPathFields[1], ".git") // “the-repo-name" or "the-repo-name.wiki"
2025-11-02 00:52:59 -07:00
if !repo_model.IsValidSSHAccessRepoName(reponame) {
return fail(ctx, "Invalid repo name", "Invalid repo name: %s", reponame)
2020-02-05 03:40:35 -06:00
}
if c.Bool("enable-pprof") {
if err := os.MkdirAll(setting.PprofDataPath, os.ModePerm); err != nil {
return fail(ctx, "Error while trying to create PPROF_DATA_PATH", "Error while trying to create PPROF_DATA_PATH: %v", err)
}
stopCPUProfiler, err := pprof.DumpCPUProfileForUsername(setting.PprofDataPath, username)
if err != nil {
return fail(ctx, "Unable to start CPU profiler", "Unable to start CPU profile: %v", err)
}
defer func() {
stopCPUProfiler()
err := pprof.DumpMemProfileForUsername(setting.PprofDataPath, username)
if err != nil {
_ = fail(ctx, "Unable to dump Mem profile", "Unable to dump Mem Profile: %v", err)
}
}()
}
verb, lfsVerb := sshCmdArgs[0], ""
if !git.IsAllowedVerbForServe(verb) {
return fail(ctx, "Unknown git command", "Unknown git command %s", verb)
2015-02-16 16:38:01 +02:00
}
2014-04-10 14:20:58 -04:00
if git.IsAllowedVerbForServeLfs(verb) {
if !setting.LFS.StartServer {
return fail(ctx, "LFS Server is not enabled", "")
}
if verb == git.CmdVerbLfsTransfer && !setting.LFS.AllowPureSSH {
return fail(ctx, "LFS SSH transfer is not enabled", "")
}
if len(sshCmdArgs) > 2 {
lfsVerb = sshCmdArgs[2]
}
}
2024-09-27 19:57:37 +05:30
requestedMode := getAccessMode(verb, lfsVerb)
2016-12-26 02:16:37 +01:00
results, extra := private.ServCommand(ctx, keyID, username, reponame, requestedMode, verb, lfsVerb)
if extra.HasError() {
return fail(ctx, extra.UserMsg, "ServCommand failed: %s", extra.Error)
2014-04-10 14:20:58 -04:00
}
2025-11-02 00:52:59 -07:00
// because the original repoPath maybe redirected, we need to use the returned actual repository information
if results.IsWiki {
repoPath = repo_model.RelativeWikiPath(results.OwnerName, results.RepoName)
} else {
repoPath = repo_model.RelativePath(results.OwnerName, results.RepoName)
}
2024-09-27 19:57:37 +05:30
// LFS SSH protocol
if verb == git.CmdVerbLfsTransfer {
2025-10-21 02:43:08 +08:00
token, err := lfs.GetLFSAuthTokenWithBearer(lfs.AuthTokenOptions{Op: lfsVerb, UserID: results.UserID, RepoID: results.RepoID})
2024-09-27 19:57:37 +05:30
if err != nil {
return err
}
return lfstransfer.Main(ctx, repoPath, lfsVerb, token)
}
2022-01-20 18:46:10 +01:00
// LFS token authentication
if verb == git.CmdVerbLfsAuthenticate {
url := fmt.Sprintf("%s%s/%s.git/info/lfs", setting.AppURL, url.PathEscape(results.OwnerName), url.PathEscape(results.RepoName))
2016-12-26 02:16:37 +01:00
2025-10-21 02:43:08 +08:00
token, err := lfs.GetLFSAuthTokenWithBearer(lfs.AuthTokenOptions{Op: lfsVerb, UserID: results.UserID, RepoID: results.RepoID})
2016-12-26 02:16:37 +01:00
if err != nil {
2024-09-27 19:57:37 +05:30
return err
2016-12-26 02:16:37 +01:00
}
2022-06-12 23:51:54 +08:00
tokenAuthentication := &git_model.LFSTokenResponse{
2016-12-26 02:16:37 +01:00
Header: make(map[string]string),
Href: url,
}
2024-09-27 19:57:37 +05:30
tokenAuthentication.Header["Authorization"] = token
2016-12-26 02:16:37 +01:00
enc := json.NewEncoder(os.Stdout)
err = enc.Encode(tokenAuthentication)
if err != nil {
return fail(ctx, "Failed to encode LFS json response", "Failed to encode LFS json response: %v", err)
2016-12-26 02:16:37 +01:00
}
return nil
}
2025-09-15 23:33:12 -07:00
var command *exec.Cmd
gitBinPath := filepath.Dir(gitcmd.GitExecutable) // e.g. /usr/bin
gitBinVerb := filepath.Join(gitBinPath, verb) // e.g. /usr/bin/git-upload-pack
if _, err := os.Stat(gitBinVerb); err != nil {
// if the command "git-upload-pack" doesn't exist, try to split "git-upload-pack" to use the sub-command with git
// ps: Windows only has "git.exe" in the bin path, so Windows always uses this way
verbFields := strings.SplitN(verb, "-", 2)
if len(verbFields) == 2 {
// use git binary with the sub-command part: "C:\...\bin\git.exe", "upload-pack", ...
2025-09-15 23:33:12 -07:00
command = exec.CommandContext(ctx, gitcmd.GitExecutable, verbFields[1], repoPath)
}
}
2025-09-15 23:33:12 -07:00
if command == nil {
// by default, use the verb (it has been checked above by allowedCommands)
2025-09-15 23:33:12 -07:00
command = exec.CommandContext(ctx, gitBinVerb, repoPath)
2014-10-01 07:40:48 -04:00
}
2017-03-17 12:59:42 +08:00
2025-09-15 23:33:12 -07:00
process.SetSysProcAttribute(command)
command.Dir = setting.RepoRootPath
command.Stdout = os.Stdout
command.Stdin = os.Stdin
command.Stderr = os.Stderr
command.Env = append(command.Env, os.Environ()...)
command.Env = append(command.Env,
repo_module.EnvRepoIsWiki+"="+strconv.FormatBool(results.IsWiki),
repo_module.EnvRepoName+"="+results.RepoName,
repo_module.EnvRepoUsername+"="+results.OwnerName,
repo_module.EnvPusherName+"="+results.UserName,
repo_module.EnvPusherEmail+"="+results.UserEmail,
repo_module.EnvPusherID+"="+strconv.FormatInt(results.UserID, 10),
repo_module.EnvRepoID+"="+strconv.FormatInt(results.RepoID, 10),
2025-04-01 12:14:01 +02:00
repo_module.EnvPRID+"="+strconv.Itoa(0),
repo_module.EnvDeployKeyID+"="+strconv.FormatInt(results.DeployKeyID, 10),
repo_module.EnvKeyID+"="+strconv.FormatInt(results.KeyID, 10),
repo_module.EnvAppURL+"="+setting.AppURL,
)
// to avoid breaking, here only use the minimal environment variables for the "gitea serv" command.
// it could be re-considered whether to use the same git.CommonGitCmdEnvs() as "git" command later.
2025-09-15 23:33:12 -07:00
command.Env = append(command.Env, gitcmd.CommonCmdServEnvs()...)
2025-09-15 23:33:12 -07:00
if err = command.Run(); err != nil {
return fail(ctx, "Failed to execute git command", "Failed to execute git command: %v", err)
2014-04-10 14:20:58 -04:00
}
2014-06-28 23:56:41 +08:00
2015-08-06 22:48:11 +08:00
// Update user key activity.
if results.KeyID > 0 {
if err = private.UpdatePublicKeyInRepo(ctx, results.KeyID, results.RepoID); err != nil {
return fail(ctx, "Failed to update public key", "UpdatePublicKeyInRepo: %v", err)
2015-08-05 11:14:17 +08:00
}
2014-08-09 15:40:10 -07:00
}
2016-05-12 14:32:28 -04:00
return nil
2014-04-10 14:20:58 -04:00
}