-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ssh.go
205 lines (178 loc) · 5.96 KB
/
ssh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package ssh
import (
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/git-lfs/git-lfs/v3/config"
"github.com/git-lfs/git-lfs/v3/subprocess"
"github.com/git-lfs/git-lfs/v3/tools"
"github.com/rubyist/tracerx"
)
type sshVariant string
const (
variantSSH = sshVariant("ssh")
variantSimple = sshVariant("simple")
variantPutty = sshVariant("putty")
variantTortoise = sshVariant("tortoiseplink")
)
type SSHMetadata struct {
UserAndHost string
Port string
Path string
}
func FormatArgs(cmd string, args []string, needShell bool, multiplex bool, controlPath string) (string, []string) {
if !needShell {
return cmd, args
}
return subprocess.FormatForShellQuotedArgs(cmd, args)
}
func GetLFSExeAndArgs(osEnv config.Environment, gitEnv config.Environment, meta *SSHMetadata, command, operation string, multiplexDesired bool, multiplexControlPath string) (exe string, args []string, multiplexing bool, controlPath string) {
exe, args, needShell, multiplexing, controlPath := GetExeAndArgs(osEnv, gitEnv, meta, multiplexDesired, multiplexControlPath)
args = append(args, fmt.Sprintf("%s %s %s", command, meta.Path, operation))
exe, args = FormatArgs(exe, args, needShell, multiplexing, controlPath)
tracerx.Printf("run_command: %s %s", exe, strings.Join(args, " "))
return exe, args, multiplexing, controlPath
}
// Parse command, and if it looks like a valid command, return the ssh binary
// name, the command to run, and whether we need a shell. If not, return
// existing as the ssh binary name.
func parseShellCommand(command string, existing string) (ssh string, cmd string, needShell bool) {
ssh = existing
if cmdArgs := tools.QuotedFields(command); len(cmdArgs) > 0 {
needShell = true
ssh = cmdArgs[0]
cmd = command
}
return
}
func findVariant(variant string) (bool, sshVariant) {
switch variant {
case "ssh", "simple", "putty", "tortoiseplink":
return false, sshVariant(variant)
case "plink":
return false, variantPutty
case "auto":
return true, ""
default:
return false, variantSSH
}
}
func autodetectVariant(osEnv config.Environment, gitEnv config.Environment, basessh string) sshVariant {
if basessh != defaultSSHCmd {
// Strip extension for easier comparison
if ext := filepath.Ext(basessh); len(ext) > 0 {
basessh = basessh[:len(basessh)-len(ext)]
}
if strings.EqualFold(basessh, "plink") {
return variantPutty
}
if strings.EqualFold(basessh, "tortoiseplink") {
return variantTortoise
}
}
return "ssh"
}
func getVariant(osEnv config.Environment, gitEnv config.Environment, basessh string) sshVariant {
variant, ok := osEnv.Get("GIT_SSH_VARIANT")
if !ok {
variant, ok = gitEnv.Get("ssh.variant")
}
autodetect, val := findVariant(variant)
if ok && !autodetect {
return val
}
return autodetectVariant(osEnv, gitEnv, basessh)
}
// findRuntimeDir returns a path to the runtime directory if one exists and is
// guaranteed to be private.
func findRuntimeDir(osEnv config.Environment) string {
if dir, ok := osEnv.Get("XDG_RUNTIME_DIR"); ok {
return dir
}
return ""
}
func getControlDir(osEnv config.Environment) (string, error) {
tmpdir, pattern := "", "sock-*"
if runtime.GOOS == "darwin" {
// On Darwin, the default temporary directory results in a socket path that's too long.
tmpdir = "/tmp"
}
dir := findRuntimeDir(osEnv)
if dir == "" {
return os.MkdirTemp(tmpdir, pattern)
}
return os.MkdirTemp(dir, pattern)
}
// Return the executable name for ssh on this machine and the base args
// Base args includes port settings, user/host, everything pre the command to execute
func GetExeAndArgs(osEnv config.Environment, gitEnv config.Environment, meta *SSHMetadata, multiplexDesired bool, multiplexControlPath string) (exe string, baseargs []string, needShell bool, multiplexing bool, controlPath string) {
var cmd string
ssh, _ := osEnv.Get("GIT_SSH")
sshCmd, _ := osEnv.Get("GIT_SSH_COMMAND")
ssh, cmd, needShell = parseShellCommand(sshCmd, ssh)
if ssh == "" {
sshCmd, _ := gitEnv.Get("core.sshcommand")
ssh, cmd, needShell = parseShellCommand(sshCmd, defaultSSHCmd)
}
if cmd == "" {
cmd = ssh
}
basessh := filepath.Base(ssh)
variant := getVariant(osEnv, gitEnv, basessh)
args := make([]string, 0, 7)
if variant == variantTortoise {
// TortoisePlink requires the -batch argument to behave like ssh/plink
args = append(args, "-batch")
}
multiplexing = false
multiplexEnabled := gitEnv.Bool("lfs.ssh.automultiplex", runtime.GOOS != "windows")
if variant == variantSSH && multiplexDesired && multiplexEnabled {
controlMasterArg := "-oControlMaster=no"
controlPath = multiplexControlPath
if multiplexControlPath == "" {
controlMasterArg = "-oControlMaster=yes"
controlDir, err := getControlDir(osEnv)
if err == nil {
controlPath = path.Join(controlDir, "lfs.sock")
}
}
if controlPath != "" {
multiplexing = true
args = append(args, controlMasterArg, fmt.Sprintf("-oControlPath=%s", controlPath))
}
}
if len(meta.Port) > 0 {
if variant == variantPutty || variant == variantTortoise {
args = append(args, "-P")
} else {
args = append(args, "-p")
}
args = append(args, meta.Port)
}
if sshOptPrefixRE.MatchString(meta.UserAndHost) {
if variant == variantSSH {
// inserts a separator between cli -options and host/cmd commands
// example: $ ssh -p 12345 -- [email protected] git-lfs-authenticate ...
args = append(args, "--", meta.UserAndHost)
} else {
// no prefix supported, strip leading - off host to prevent cmd like:
// $ git config lfs.url ssh://-proxycmd=whatever
// $ plink -P 12345 -proxycmd=foo git-lfs-authenticate ...
//
// Instead, it'll attempt this, and eventually return an error
// $ plink -P 12345 proxycmd=foo git-lfs-authenticate ...
args = append(args, sshOptPrefixRE.ReplaceAllString(meta.UserAndHost, ""))
}
} else {
args = append(args, meta.UserAndHost)
}
return cmd, args, needShell, multiplexing, controlPath
}
const defaultSSHCmd = "ssh"
var (
sshOptPrefixRE = regexp.MustCompile(`\A\-+`)
)