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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
// sftp_service.go
package sftpd
import (
"context"
"fmt"
"io"
"log"
"net"
"os"
"path/filepath"
"time"
"github.com/pkg/sftp"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb"
filer_pb "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/sftpd/auth"
"github.com/seaweedfs/seaweedfs/weed/sftpd/user"
"github.com/seaweedfs/seaweedfs/weed/util"
"golang.org/x/crypto/ssh"
"google.golang.org/grpc"
)
// SFTPService holds configuration for the SFTP service.
type SFTPService struct {
options SFTPServiceOptions
userStore user.Store
authManager *auth.Manager
homeManager *user.HomeManager
}
// SFTPServiceOptions contains all configuration options for the SFTP service.
type SFTPServiceOptions struct {
GrpcDialOption grpc.DialOption
DataCenter string
FilerGroup string
Filer pb.ServerAddress
// SSH Configuration
SshPrivateKey string // Legacy single host key
HostKeysFolder string // Multiple host keys for different algorithms
AuthMethods []string // Enabled auth methods: "password", "publickey", "keyboard-interactive"
MaxAuthTries int // Limit authentication attempts
BannerMessage string // Pre-auth banner message
LoginGraceTime time.Duration // Timeout for authentication
// Connection Management
ClientAliveInterval time.Duration // Keep-alive check interval
ClientAliveCountMax int // Max missed keep-alives before disconnect
// User Management
UserStoreFile string // Path to user store file
}
// NewSFTPService creates a new service instance.
func NewSFTPService(options *SFTPServiceOptions) *SFTPService {
service := SFTPService{options: *options}
// Initialize user store
userStore, err := user.NewFileStore(options.UserStoreFile)
if err != nil {
glog.Fatalf("Failed to initialize user store: %v", err)
}
service.userStore = userStore
// Initialize file system helper for permission checking
fsHelper := NewFileSystemHelper(
options.Filer,
options.GrpcDialOption,
options.DataCenter,
options.FilerGroup,
)
// Initialize auth manager
service.authManager = auth.NewManager(userStore, fsHelper, options.AuthMethods)
// Initialize home directory manager
service.homeManager = user.NewHomeManager(fsHelper)
return &service
}
// FileSystemHelper implements auth.FileSystemHelper interface
type FileSystemHelper struct {
filerAddr pb.ServerAddress
grpcDialOption grpc.DialOption
dataCenter string
filerGroup string
}
func NewFileSystemHelper(filerAddr pb.ServerAddress, grpcDialOption grpc.DialOption, dataCenter, filerGroup string) *FileSystemHelper {
return &FileSystemHelper{
filerAddr: filerAddr,
grpcDialOption: grpcDialOption,
dataCenter: dataCenter,
filerGroup: filerGroup,
}
}
// GetEntry implements auth.FileSystemHelper interface
func (fs *FileSystemHelper) GetEntry(path string) (*auth.Entry, error) {
dir, name := util.FullPath(path).DirAndName()
var entry *filer_pb.Entry
err := fs.withTimeoutContext(func(ctx context.Context) error {
return fs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
resp, err := client.LookupDirectoryEntry(ctx, &filer_pb.LookupDirectoryEntryRequest{
Directory: dir,
Name: name,
})
if err != nil {
return err
}
if resp.Entry == nil {
return fmt.Errorf("entry not found")
}
entry = resp.Entry
return nil
})
})
if err != nil {
return nil, err
}
return &auth.Entry{
IsDirectory: entry.IsDirectory,
Attributes: &auth.EntryAttributes{
Uid: entry.Attributes.GetUid(),
Gid: entry.Attributes.GetGid(),
FileMode: entry.Attributes.GetFileMode(),
SymlinkTarget: entry.Attributes.GetSymlinkTarget(),
},
IsSymlink: entry.Attributes.GetSymlinkTarget() != "",
}, nil
}
// Implement FilerClient interface for FileSystemHelper
func (fs *FileSystemHelper) AdjustedUrl(location *filer_pb.Location) string {
return location.Url
}
func (fs *FileSystemHelper) GetDataCenter() string {
return fs.dataCenter
}
func (fs *FileSystemHelper) WithFilerClient(streamingMode bool, fn func(filer_pb.SeaweedFilerClient) error) error {
addr := fs.filerAddr.ToGrpcAddress()
return pb.WithGrpcClient(streamingMode, util.RandomInt32(), func(conn *grpc.ClientConn) error {
return fn(filer_pb.NewSeaweedFilerClient(conn))
}, addr, false, fs.grpcDialOption)
}
func (fs *FileSystemHelper) withTimeoutContext(fn func(ctx context.Context) error) error {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
return fn(ctx)
}
// Serve accepts incoming connections on the provided listener and handles them.
func (s *SFTPService) Serve(listener net.Listener) error {
// Build SSH server config
sshConfig, err := s.buildSSHConfig()
if err != nil {
return fmt.Errorf("failed to create SSH config: %v", err)
}
glog.V(0).Infof("Starting Seaweed SFTP service on %s", listener.Addr().String())
for {
conn, err := listener.Accept()
if err != nil {
return fmt.Errorf("failed to accept incoming connection: %v", err)
}
go s.handleSSHConnection(conn, sshConfig)
}
}
// buildSSHConfig creates the SSH server configuration with proper authentication.
func (s *SFTPService) buildSSHConfig() (*ssh.ServerConfig, error) {
// Get base config from auth manager
config := s.authManager.GetSSHServerConfig()
// Set additional options
config.MaxAuthTries = s.options.MaxAuthTries
config.BannerCallback = func(conn ssh.ConnMetadata) string {
return s.options.BannerMessage
}
config.ServerVersion = "SSH-2.0-SeaweedFS-SFTP" // Custom server version
hostKeysAdded := 0
// Add legacy host key if specified
if s.options.SshPrivateKey != "" {
if err := s.addHostKey(config, s.options.SshPrivateKey); err != nil {
return nil, err
}
hostKeysAdded++
}
// Add all host keys from the specified folder
if s.options.HostKeysFolder != "" {
files, err := os.ReadDir(s.options.HostKeysFolder)
if err != nil {
return nil, fmt.Errorf("failed to read host keys folder: %v", err)
}
for _, file := range files {
if file.IsDir() {
continue // Skip directories
}
keyPath := filepath.Join(s.options.HostKeysFolder, file.Name())
if err := s.addHostKey(config, keyPath); err != nil {
// Log the error but continue with other keys
log.Printf("Warning: failed to add host key %s: %v", keyPath, err)
continue
}
hostKeysAdded++
}
if hostKeysAdded == 0 {
log.Printf("Warning: no valid host keys found in folder %s", s.options.HostKeysFolder)
}
}
// Ensure we have at least one host key
if hostKeysAdded == 0 {
return nil, fmt.Errorf("no host keys provided")
}
return config, nil
}
// addHostKey adds a host key to the SSH server configuration.
func (s *SFTPService) addHostKey(config *ssh.ServerConfig, keyPath string) error {
keyBytes, err := os.ReadFile(keyPath)
if err != nil {
return fmt.Errorf("failed to read host key %s: %v", keyPath, err)
}
// Try parsing as private key
signer, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
// Try parsing with passphrase if available
if passphraseErr, ok := err.(*ssh.PassphraseMissingError); ok {
return fmt.Errorf("host key %s requires passphrase: %v", keyPath, passphraseErr)
}
return fmt.Errorf("failed to parse host key %s: %v", keyPath, err)
}
config.AddHostKey(signer)
glog.V(0).Infof("Added host key %s (%s)", keyPath, signer.PublicKey().Type())
return nil
}
// handleSSHConnection handles an incoming SSH connection.
func (s *SFTPService) handleSSHConnection(conn net.Conn, config *ssh.ServerConfig) {
// Set connection deadline for handshake
_ = conn.SetDeadline(time.Now().Add(s.options.LoginGraceTime))
// Perform SSH handshake
sshConn, chans, reqs, err := ssh.NewServerConn(conn, config)
if err != nil {
glog.Errorf("Failed to handshake: %v", err)
conn.Close()
return
}
// Clear deadline after successful handshake
_ = conn.SetDeadline(time.Time{})
// Set up connection monitoring
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start keep-alive monitoring
go s.monitorConnection(ctx, sshConn)
username := sshConn.Permissions.Extensions["username"]
glog.V(0).Infof("New SSH connection from %s (%s) as user %s",
sshConn.RemoteAddr(), sshConn.ClientVersion(), username)
// Get user from store
sftpUser, err := s.authManager.GetUser(username)
if err != nil {
glog.Errorf("Failed to retrieve user %s: %v", username, err)
sshConn.Close()
return
}
// Create user-specific filesystem
userFs := NewSftpServer(
s.options.Filer,
s.options.GrpcDialOption,
s.options.DataCenter,
s.options.FilerGroup,
sftpUser,
)
// Ensure home directory exists with proper permissions
if err := s.homeManager.EnsureHomeDirectory(sftpUser); err != nil {
glog.Errorf("Failed to ensure home directory for user %s: %v", username, err)
// We don't close the connection here, as the user might still be able to access other directories
}
// Handle SSH requests and channels
go ssh.DiscardRequests(reqs)
for newChannel := range chans {
go s.handleChannel(newChannel, &userFs)
}
}
// monitorConnection monitors an SSH connection with keep-alives.
func (s *SFTPService) monitorConnection(ctx context.Context, sshConn *ssh.ServerConn) {
if s.options.ClientAliveInterval <= 0 {
return
}
ticker := time.NewTicker(s.options.ClientAliveInterval)
defer ticker.Stop()
missedCount := 0
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
// Send keep-alive request
_, _, err := sshConn.SendRequest("keepalive@openssh.com", true, nil)
if err != nil {
missedCount++
glog.V(0).Infof("Keep-alive missed for %s: %v (%d/%d)",
sshConn.RemoteAddr(), err, missedCount, s.options.ClientAliveCountMax)
if missedCount >= s.options.ClientAliveCountMax {
glog.Warningf("Closing unresponsive connection from %s", sshConn.RemoteAddr())
sshConn.Close()
return
}
} else {
missedCount = 0
}
}
}
}
// handleChannel handles a single SSH channel.
func (s *SFTPService) handleChannel(newChannel ssh.NewChannel, fs *SftpServer) {
if newChannel.ChannelType() != "session" {
_ = newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
return
}
channel, requests, err := newChannel.Accept()
if err != nil {
glog.Errorf("Could not accept channel: %v", err)
return
}
go func(in <-chan *ssh.Request) {
for req := range in {
switch req.Type {
case "subsystem":
// Check that the subsystem is "sftp".
if string(req.Payload[4:]) == "sftp" {
_ = req.Reply(true, nil)
s.handleSFTP(channel, fs)
} else {
_ = req.Reply(false, nil)
}
default:
_ = req.Reply(false, nil)
}
}
}(requests)
}
// handleSFTP starts the SFTP server on the SSH channel.
func (s *SFTPService) handleSFTP(channel ssh.Channel, fs *SftpServer) {
// Create server options with initial working directory set to user's home
serverOptions := sftp.WithStartDirectory(fs.user.HomeDir)
server := sftp.NewRequestServer(channel, sftp.Handlers{
FileGet: fs,
FilePut: fs,
FileCmd: fs,
FileList: fs,
}, serverOptions)
if err := server.Serve(); err == io.EOF {
server.Close()
glog.V(0).Info("SFTP client exited session.")
} else if err != nil {
glog.Errorf("SFTP server finished with error: %v", err)
}
}
|