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
|
// Package sftpfs implements [wfs.FS] using [github.com/pkg/sftp].
package sftpfs
import (
"errors"
"fmt"
"io"
"io/fs"
"net"
"os"
"path/filepath"
"strings"
"sync"
"github.com/pkg/sftp"
"github.com/rhogenson/ccp/internal/wfs"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"golang.org/x/crypto/ssh/knownhosts"
"golang.org/x/term"
)
var (
_ wfs.FS = (*FS)(nil)
_ wfs.ReadLinkFS = (*FS)(nil)
_ fs.StatFS = (*FS)(nil)
_ fs.ReadDirFS = (*FS)(nil)
)
// An FS holds an SFTP connection and wraps its operations into the
// [wfs.FS] interface.
type FS struct {
User, Host string
conn *sftp.Client
sshConn *ssh.Client
}
var sshAgent = sync.OnceValue(func() agent.ExtendedAgent {
socket := os.Getenv("SSH_AUTH_SOCK")
if socket == "" {
return nil
}
conn, err := net.Dial("unix", socket)
if err != nil {
return nil
}
return agent.NewClient(conn)
})
// sshKeys returns the available ssh public keys. If an ssh agent can be
// contacted with $SSH_AUTH_SOCK, sshKeys uses the keys from the agent if
// possible. Otherwise sshKeys loads keys from ~/.ssh. If there are any password
// protected keys, sshKeys may prompt the user for the password (although it
// will do so at most once).
//
// If a password-protected key is loaded from ~/.ssh, it will be added to the
// ssh agent if possible.
func sshKeys() ([]ssh.Signer, error) {
sshAgent := sshAgent()
if sshAgent != nil {
if signers, err := sshAgent.Signers(); err == nil && len(signers) > 0 {
return signers, nil
}
}
sshDir := filepath.Join(os.Getenv("HOME"), ".ssh")
sshFiles, err := os.ReadDir(sshDir)
if len(sshFiles) == 0 {
return nil, err
}
var keys []ssh.Signer
var passwordProtectedKey []byte
var passwordProtectedKeyFile string
for _, f := range sshFiles {
if f.Name() == "known_hosts" || strings.HasSuffix(f.Name(), ".pub") {
continue
}
fileName := filepath.Join(sshDir, f.Name())
keyBytes, err := os.ReadFile(fileName)
if err != nil {
continue
}
key, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
if passwordProtectedKey == nil && errors.As(err, new(*ssh.PassphraseMissingError)) {
passwordProtectedKey = keyBytes
passwordProtectedKeyFile = fileName
}
continue
}
keys = append(keys, key)
}
if len(keys) == 0 && passwordProtectedKey != nil {
fmt.Fprintf(os.Stderr, "Enter password for %s: ", passwordProtectedKeyFile)
for i := range 3 {
if i > 0 {
fmt.Fprintf(os.Stderr, "Incorrect password, try again: ")
}
password, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Fprintln(os.Stderr)
if err != nil {
return nil, err
}
key, err := ssh.ParseRawPrivateKeyWithPassphrase(passwordProtectedKey, password)
if err != nil {
continue
}
if sshAgent != nil {
sshAgent.Add(agent.AddedKey{PrivateKey: key})
}
signer, err := ssh.NewSignerFromKey(key)
if err != nil {
return nil, err
}
return []ssh.Signer{signer}, nil
}
return nil, errors.New("user couldn't remember her password")
}
return keys, nil
}
func appendToKnownHosts(hostname string, key ssh.PublicKey) error {
f, err := os.OpenFile(filepath.Join(os.Getenv("HOME"), ".ssh/known_hosts"), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString(knownhosts.Line([]string{hostname}, key) + "\n"); err != nil {
return err
}
return f.Close()
}
// Dial establishes a new SFTP connection to the given host.
func Dial(target string) (*FS, error) {
knownHostChecker, err := knownhosts.New(filepath.Join(os.Getenv("HOME"), ".ssh/known_hosts"))
if err != nil {
knownHostChecker = func(string, net.Addr, ssh.PublicKey) error { return &knownhosts.KeyError{} }
}
var user string
if i := strings.Index(target, "@"); i >= 0 {
user, target = target[:i], target[i+1:]
} else {
user = os.Getenv("USER")
}
sshConn, err := ssh.Dial("tcp", target+":22", &ssh.ClientConfig{
User: user,
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(sshKeys),
ssh.RetryableAuthMethod(ssh.PasswordCallback(func() (string, error) {
fmt.Fprintf(os.Stderr, "Enter password for %s@%s: ", user, target)
password, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Fprintln(os.Stderr)
return string(password), err
}), 3),
},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
err := knownHostChecker(hostname, remote, key)
if err == nil {
return nil
}
var keyErr *knownhosts.KeyError
if !errors.As(err, &keyErr) || len(keyErr.Want) > 0 {
return err
}
// scp prompts the user if the host is not found in
// known_hosts, but when is that ever useful? We'll just
// add it to known_hosts without bothering the user.
appendToKnownHosts(hostname, key)
return nil
},
})
if err != nil {
return nil, err
}
sftpConn, err := sftp.NewClient(sshConn)
if err != nil {
sshConn.Close()
return nil, err
}
return &FS{
User: user,
Host: target,
conn: sftpConn,
sshConn: sshConn,
}, nil
}
// Close closes the underlying SFTP connection.
func (f *FS) Close() error {
sftpErr := f.conn.Close()
if err := f.sshConn.Close(); err != nil {
return err
}
return sftpErr
}
func (f *FS) err(op, path string, err error) error {
// github.com/pkg/sftp's errors are pretty terrible.
// We'll wrap them to be more similar to the amazing package os errors.
return fmt.Errorf("%s %q: %w", op, f.User+"@"+f.Host+":"+path, err)
}
// wfs.FS implementation:
func (f *FS) Open(name string) (fs.File, error) {
file, err := f.conn.Open(name)
if err != nil {
return nil, f.err("open", name, err)
}
return file, nil
}
func (f *FS) ReadDir(name string) ([]fs.DirEntry, error) {
entriesFileInfo, err := f.conn.ReadDir(name)
entries := make([]fs.DirEntry, len(entriesFileInfo))
for i, entry := range entriesFileInfo {
entries[i] = fs.FileInfoToDirEntry(entry)
}
if err != nil {
return entries, f.err("readdir", name, err)
}
return entries, err
}
func (f *FS) Stat(name string) (fs.FileInfo, error) {
fi, err := f.conn.Stat(name)
if err != nil {
return nil, f.err("stat", name, err)
}
return fi, nil
}
func (f *FS) Lstat(name string) (fs.FileInfo, error) {
fi, err := f.conn.Lstat(name)
if err != nil {
return nil, f.err("lstat", name, err)
}
return fi, nil
}
func (f *FS) ReadLink(name string) (string, error) {
target, err := f.conn.ReadLink(name)
if err != nil {
return "", f.err("readlink", name, err)
}
return target, nil
}
func (f *FS) Create(name string, perm fs.FileMode) (io.WriteCloser, error) {
file, err := f.conn.Create(name)
if err != nil {
return nil, f.err("open", name, err)
}
if err := file.Chmod(perm); err != nil {
file.Close()
return nil, f.err("chmod", name, err)
}
return file, nil
}
func (f *FS) Remove(name string) error {
if err := f.conn.Remove(name); err != nil {
return f.err("remove", name, err)
}
return nil
}
func (f *FS) Mkdir(name string) error {
if err := f.conn.Mkdir(name); err != nil {
return f.err("mkdir", name, err)
}
return nil
}
func (f *FS) Symlink(oldname, newname string) error {
if err := f.conn.Symlink(oldname, newname); err != nil {
return f.err("symlink", newname, err)
}
return nil
}
func (f *FS) Chmod(name string, mode fs.FileMode) error {
if err := f.conn.Chmod(name, mode); err != nil {
return f.err("chmod", name, err)
}
return nil
}
|