forked from lima-vm/sshocker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreversesshfs.go
More file actions
317 lines (306 loc) · 9.72 KB
/
reversesshfs.go
File metadata and controls
317 lines (306 loc) · 9.72 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package reversesshfs
import (
"bytes"
"errors"
"fmt"
"html/template"
"io"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/lima-vm/sshocker/pkg/ssh"
"github.com/lima-vm/sshocker/pkg/util"
"github.com/pkg/sftp"
"github.com/sirupsen/logrus"
)
type Driver = string
const (
DriverAuto = Driver("auto") // Default
DriverBuiltin = Driver("builtin") // Legacy. Unrecommended.
DriverOpensshSftpServer = Driver("openssh-sftp-server") // More robust and secure. Recommended.
)
type ReverseSSHFS struct {
*ssh.SSHConfig
Driver Driver
OpensshSftpServerBinary string // used only when Driver == DriverOpensshSftpServer
LocalPath string
Host string
Port int
RemotePath string
Readonly bool
sshCmd *exec.Cmd
opensshSftpServerCmd *exec.Cmd
SSHFSAdditionalArgs []string
}
func (rsf *ReverseSSHFS) Prepare() error {
sshBinary := rsf.SSHConfig.Binary()
sshArgs := rsf.SSHConfig.Args()
if !path.IsAbs(rsf.RemotePath) {
return fmt.Errorf("unexpected relative path: %q", rsf.RemotePath)
}
if rsf.Port != 0 {
sshArgs = append(sshArgs, "-p", strconv.Itoa(rsf.Port))
}
sshArgs = append(sshArgs, rsf.Host, "--")
sshArgs = append(sshArgs, "mkdir", "-p", strconv.Quote(rsf.RemotePath))
sshCmd := exec.Command(sshBinary, sshArgs...)
logrus.Debugf("executing ssh for preparing sshfs: %s %v", sshCmd.Path, sshCmd.Args)
out, err := sshCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to mkdir %q (remote): %q: %w", rsf.RemotePath, string(out), err)
}
return nil
}
func DetectOpensshSftpServerBinary() string {
if exe, err := exec.LookPath("sftp-server"); err == nil {
return exe
}
homebrewSSHD := []string{
"/usr/local/sbin/sshd",
"/opt/homebrew/sbin/sshd",
}
for _, f := range homebrewSSHD {
// sshd is like "/usr/local/Cellar/openssh/8.9p1/sbin/sshd"
sshd, err := filepath.EvalSymlinks(f)
if err != nil {
continue
}
// local is like "/usr/local/Cellar/openssh"
local := filepath.Dir(filepath.Dir(sshd))
// sftpServer is like "/usr/local/Cellar/openssh/8.9p1/libexec/sftp-server"
sftpServer := filepath.Join(local, "libexec", "sftp-server")
if exe, err := exec.LookPath(sftpServer); err == nil {
return exe
}
}
if runtime.GOOS == "windows" {
// unix path is like "/usr/lib/ssh/sftp-server"
cygpathCmd := exec.Command("cygpath", "-w", "/usr/lib/ssh/sftp-server")
// windows path is like `C:\msys64\usr\lib\ssh\sftp-server.exe`
if out, err := cygpathCmd.Output(); err == nil {
sftpServer := strings.TrimSpace(string(out))
if exe, err := exec.LookPath(sftpServer); err == nil {
return exe
}
}
}
candidates := []string{
"/usr/libexec/sftp-server", // macOS, OpenWrt
"/usr/libexec/openssh/sftp-server", // Fedora
"/usr/lib/sftp-server", // Debian (symlink to openssh/sftp-server)
"/usr/lib/openssh/sftp-server", // Debian
"/usr/lib/ssh/sftp-server", // Alpine
}
for _, cand := range candidates {
if exe, err := exec.LookPath(cand); err == nil {
return exe
}
}
return ""
}
func DetectDriver(explicitOpensshSftpServerBinary string) (Driver, string, error) {
if explicitOpensshSftpServerBinary != "" {
exe, err := exec.LookPath(explicitOpensshSftpServerBinary)
if err != nil {
return "", "", err
}
return DriverOpensshSftpServer, exe, nil
}
exe := DetectOpensshSftpServerBinary()
if exe != "" {
return DriverOpensshSftpServer, exe, nil
}
return DriverBuiltin, "", nil
}
func (rsf *ReverseSSHFS) Start() error {
sshBinary := rsf.SSHConfig.Binary()
sshArgs := rsf.SSHConfig.Args()
if !filepath.IsAbs(rsf.LocalPath) && !path.IsAbs(rsf.LocalPath) {
return fmt.Errorf("unexpected relative path: %q", rsf.LocalPath)
}
if runtime.GOOS == "windows" && path.IsAbs(rsf.LocalPath) {
logrus.Infof("Accepting %q Unix path, assuming Cygwin/msys2 OpenSSH", rsf.LocalPath)
}
if !path.IsAbs(rsf.RemotePath) {
return fmt.Errorf("unexpected relative path: %q", rsf.RemotePath)
}
if rsf.Port != 0 {
sshArgs = append(sshArgs, "-p", strconv.Itoa(rsf.Port))
}
sshArgs = append(sshArgs, rsf.Host, "--")
sshArgs = append(sshArgs, "sshfs", strconv.Quote(":"+rsf.LocalPath), strconv.Quote(rsf.RemotePath), "-o", "slave")
if rsf.Readonly {
sshArgs = append(sshArgs, "-o", "ro")
}
sshArgs = append(sshArgs, rsf.SSHFSAdditionalArgs...)
rsf.sshCmd = exec.Command(sshBinary, sshArgs...)
rsf.sshCmd.Stderr = os.Stderr
driver := rsf.Driver
opensshSftpServerBinary := rsf.OpensshSftpServerBinary
switch driver {
case DriverBuiltin, DriverOpensshSftpServer:
// NOP
case "", DriverAuto:
var err error
driver, opensshSftpServerBinary, err = DetectDriver(opensshSftpServerBinary)
if err != nil {
return fmt.Errorf("failed to choose driver automatically: %w", err)
}
logrus.Debugf("Chosen driver %q", driver)
default:
return fmt.Errorf("unknown driver %q", driver)
}
var builtinSftpServer *sftp.Server
switch driver {
case DriverBuiltin:
stdinPipe, err := rsf.sshCmd.StdinPipe()
if err != nil {
return err
}
stdoutPipe, err := rsf.sshCmd.StdoutPipe()
if err != nil {
return err
}
stdio := &util.RWC{
ReadCloser: stdoutPipe,
WriteCloser: stdinPipe,
}
var sftpOpts []sftp.ServerOption
if rsf.Readonly {
sftpOpts = append(sftpOpts, sftp.ReadOnly())
}
// NOTE: sftp.NewServer doesn't support specifying the root.
// https://github.com/pkg/sftp/pull/238
//
// TODO: use sftp.NewRequestServer with custom handlers to mitigate potential vulnerabilities.
builtinSftpServer, err = sftp.NewServer(stdio, sftpOpts...)
if err != nil {
return err
}
case DriverOpensshSftpServer:
if opensshSftpServerBinary == "" {
opensshSftpServerBinary = DetectOpensshSftpServerBinary()
if opensshSftpServerBinary == "" {
return errors.New("no openssh sftp-server found")
}
}
logrus.Debugf("Using OpenSSH SFTP Server %q", opensshSftpServerBinary)
sftpServerArgs := []string{
// `-e` available since OpenSSH 5.4p1 (2010) https://github.com/openssh/openssh-portable/commit/7bee06ab
"-e",
// `-d` available since OpenSSH 6.2p1 (2013) https://github.com/openssh/openssh-portable/commit/502ab0ef
// NOTE: `-d` just chdirs the sftp server process to the specified directory.
// This is expected to be used in conjunction with chroot (in future), however, macOS does not support unprivileged chroot.
"-d", strings.ReplaceAll(rsf.LocalPath, "%", "%%"),
}
if rsf.Readonly {
// `-R` available since OpenSSH 5.4p1 (2010) https://github.com/openssh/openssh-portable/commit/db7bf825
sftpServerArgs = append(sftpServerArgs, "-R")
}
rsf.opensshSftpServerCmd = exec.Command(opensshSftpServerBinary, sftpServerArgs...)
rsf.opensshSftpServerCmd.Stderr = os.Stderr
var err error
rsf.opensshSftpServerCmd.Stdin, err = rsf.sshCmd.StdoutPipe()
if err != nil {
return err
}
rsf.sshCmd.Stdin, err = rsf.opensshSftpServerCmd.StdoutPipe()
if err != nil {
return err
}
}
logrus.Debugf("executing ssh for remote sshfs: %s %v", rsf.sshCmd.Path, rsf.sshCmd.Args)
if err := rsf.sshCmd.Start(); err != nil {
return err
}
logrus.Debugf("starting sftp server for %v", rsf.LocalPath)
switch driver {
case DriverBuiltin:
go func() {
if srvErr := builtinSftpServer.Serve(); srvErr != nil {
if errors.Is(srvErr, io.EOF) {
logrus.WithError(srvErr).Debugf("sftp server for %v exited with EOF (negligible)", rsf.LocalPath)
} else {
logrus.WithError(srvErr).Errorf("sftp server for %v exited", rsf.LocalPath)
}
}
}()
case DriverOpensshSftpServer:
logrus.Debugf("executing OpenSSH SFTP Server: %s %v", rsf.opensshSftpServerCmd.Path, rsf.opensshSftpServerCmd.Args)
if err := rsf.opensshSftpServerCmd.Start(); err != nil {
return err
}
}
logrus.Debugf("waiting for remote ready")
if err := rsf.waitForRemoteReady(); err != nil {
// not a fatal error
logrus.WithError(err).Warnf("failed to confirm whether %v [remote] is successfully mounted", rsf.RemotePath)
}
return nil
}
func (rsf *ReverseSSHFS) waitForRemoteReady() error {
scriptName := "wait-for-remote-ready"
scriptTemplate := `#!/bin/sh
set -eu
dir="{{.Dir}}"
max_trial="{{.MaxTrial}}"
LANG=C
LC_ALL=C
export LANG LC_ALL
i=0
while : ; do
# FIXME: not really robust
# spaces in file names are encoded as '\040' in the mount table
if mount | sed 's/\\040/ /g' | grep "on ${dir}" | egrep -qw "fuse.sshfs|osxfuse"; then
echo '{"return":{}}'
exit 0
fi
sleep 1
if [ $i -ge ${max_trial} ]; then
echo >&2 "sshfs does not seem to be mounted on ${dir}"
exit 1
fi
i=$((i + 1))
done
`
t, err := template.New(scriptName).Parse(scriptTemplate)
if err != nil {
return err
}
m := map[string]string{
// rsf.RemotePath should have been verified during rsf.Prepare()
"Dir": rsf.RemotePath,
"MaxTrial": "30",
}
var b bytes.Buffer
if err := t.Execute(&b, m); err != nil {
return err
}
script := b.String()
logrus.Debugf("generated script %q with map %v: %q", scriptName, m, script)
stdout, stderr, err := ssh.ExecuteScript(rsf.Host, rsf.Port, rsf.SSHConfig, script, scriptName)
logrus.Debugf("executed script %q, stdout=%q, stderr=%q, err=%v", scriptName, stdout, stderr, err)
return err
}
func (rsf *ReverseSSHFS) Close() error {
logrus.Debugf("killing processes for remote sshfs: %s %v", rsf.sshCmd.Path, rsf.sshCmd.Args)
var errors []error
if rsf.sshCmd != nil && rsf.sshCmd.Process != nil {
if err := rsf.sshCmd.Process.Kill(); err != nil {
errors = append(errors, err)
}
}
if rsf.opensshSftpServerCmd != nil && rsf.opensshSftpServerCmd.Process != nil {
if err := rsf.opensshSftpServerCmd.Process.Kill(); err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
return fmt.Errorf("%v", errors)
}
return nil
}