mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
* sftp: add -stdio to run as an OpenSSH Subsystem
weed sftp -stdio serves one SFTP session on stdin/stdout, so sshd can run it
as its sftp subsystem and handle authentication itself (PAM, SSSD, 2FA,
AllowUsers, Match blocks):
Subsystem sftp /usr/local/bin/weed sftp -stdio -filer=filer:8888
The session belongs to the OS user sshd started it as. With -userStoreFile
that user must be in the store, and its HomeDir and Permissions apply as
over SSH; without one, the session reaches the whole filer, which the
process's filer access allows anyway. The SSH listener options are ignored
and nothing but SFTP is written to stdout.
Checked with OpenSSH's sftp -D: put/ls/rm on the whole filer without a
store; with a store the user is jailed to its home, and a user missing from
the store is refused.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* sftp -stdio: explain an unreadable user store, test the stream
The session runs as the logged-in OS user, who must read the store; a
store without passwords or keys can be shared with them. Tell that user
why the store is refused instead of a bare permission error.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* sftp: stdio sessions reuse handleSFTP
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* sftp: a refused -stdio session exits 1 without printing usage
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* sftp -stdio: give up after 30s when the filer is unreachable
The listener keeps waiting for the filer; a stdio session is one login and
should not hold a process for the whole outage.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* sftp -stdio: bound the filer configuration request by the wait
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* sftpd: skip the unreadable-store test on Windows
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* sftp: detect the sshd Subsystem launch instead of a -stdio flag
weed sftp serves one session on stdin/stdout when sshd starts it: SSH_CONNECTION
is set and stdin and stdout are both pipes or sockets. A listener started by
systemd, a container, nohup or a shell has a terminal, /dev/null or a file
there, so it keeps listening.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Denis Maier <denis@dAir.lab>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
95 lines
2.6 KiB
Go
95 lines
2.6 KiB
Go
package sftpd
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/pkg/sftp"
|
|
)
|
|
|
|
func TestStdioUser(t *testing.T) {
|
|
u, err := StdioUser("", "alice", 1000, 1000)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.HomeDir != "/" || u.Uid != 1000 || len(u.Permissions["/"]) != 1 || u.Permissions["/"][0] != PermAll {
|
|
t.Errorf("without a store got %+v, want the whole filer as uid 1000", u)
|
|
}
|
|
|
|
store := filepath.Join(t.TempDir(), "users.json")
|
|
if err := os.WriteFile(store, []byte(`[{"Username":"alice","HomeDir":"/home/alice","Uid":1001,"Permissions":{"/home/alice":["read"]}}]`), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
u, err = StdioUser(store, "alice", 1000, 1000)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if u.HomeDir != "/home/alice" || u.Uid != 1001 {
|
|
t.Errorf("with a store got %+v, want the stored account", u)
|
|
}
|
|
if _, err := StdioUser(store, "mallory", 1000, 1000); err == nil {
|
|
t.Error("a user missing from the store got a session")
|
|
}
|
|
if _, err := StdioUser(filepath.Join(t.TempDir(), "missing.json"), "alice", 1000, 1000); err == nil {
|
|
t.Error("a missing store file got a session")
|
|
}
|
|
}
|
|
|
|
// An SSH user who cannot read the store is told why, not just refused.
|
|
func TestStdioUserUnreadableStore(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("mode 0 does not deny reads on Windows")
|
|
}
|
|
if os.Getuid() == 0 {
|
|
t.Skip("root reads any file")
|
|
}
|
|
store := filepath.Join(t.TempDir(), "users.json")
|
|
if err := os.WriteFile(store, []byte(`[]`), 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := StdioUser(store, "alice", 1000, 1000)
|
|
if err == nil || !strings.Contains(err.Error(), "not readable by alice") {
|
|
t.Fatalf("unreadable store: err = %v", err)
|
|
}
|
|
}
|
|
|
|
type pipeStream struct {
|
|
io.Reader
|
|
io.WriteCloser
|
|
}
|
|
|
|
// A session on a plain stream answers the SFTP handshake and ends cleanly
|
|
// when the client goes away, as sshd's Subsystem pipes do.
|
|
func TestServeStdio(t *testing.T) {
|
|
clientToServer, fromClient := io.Pipe()
|
|
toClient, serverToClient := io.Pipe()
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
u, _ := StdioUser("", "alice", 1000, 1000)
|
|
done <- ServeStdio(&SFTPServiceOptions{Filer: "127.0.0.1:1"}, u, pipeStream{clientToServer, serverToClient})
|
|
}()
|
|
|
|
client, err := sftp.NewClientPipe(toClient, fromClient)
|
|
if err != nil {
|
|
t.Fatalf("handshake: %v", err)
|
|
}
|
|
if wd, err := client.Getwd(); err != nil || wd != "/" {
|
|
t.Errorf("Getwd = %q, %v; want /", wd, err)
|
|
}
|
|
client.Close()
|
|
|
|
select {
|
|
case err := <-done:
|
|
if err != nil {
|
|
t.Fatalf("ServeStdio: %v", err)
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("ServeStdio did not return after the client closed")
|
|
}
|
|
}
|