Files
seaweedfs/weed/sftpd/sftp_stdio.go
T
6b402aa87c sftp: serve one session on stdin/stdout when run as an OpenSSH Subsystem (#13278)
* 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>
2026-12-02 04:02:12 -08:00

81 lines
2.5 KiB
Go

package sftpd
import (
"errors"
"fmt"
"io"
"os"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/sftpd/user"
)
// StdioUser picks the account for an SFTP session on stdin/stdout, where
// sshd has already authenticated username. With a user store the user must
// be in it, and its home and permissions apply. Without one the session gets
// the whole filer, as the process's own filer access already allows.
//
// The session runs as the logged-in OS user, so that user must be able to
// read the store. Only usernames, homes and permissions are needed: sshd
// does the authentication, so this store should hold no passwords or keys,
// and can then be shared with the sshd users' group (for example 0640).
func StdioUser(userStoreFile, username string, uid, gid uint32) (*user.User, error) {
if userStoreFile == "" {
return &user.User{
Username: username,
HomeDir: "/",
Uid: uid,
Gid: gid,
Permissions: map[string][]string{"/": {PermAll}},
}, nil
}
f, err := os.Open(userStoreFile)
if err != nil {
if errors.Is(err, os.ErrPermission) {
return nil, fmt.Errorf("user store %s is not readable by %s; as an sshd Subsystem, use a store without passwords or keys that the SSH users can read", userStoreFile, username)
}
return nil, fmt.Errorf("user store: %w", err)
}
f.Close()
store, err := user.NewFileStore(userStoreFile)
if err != nil {
return nil, fmt.Errorf("user store: %w", err)
}
u, err := store.GetUser(username)
if err != nil {
return nil, fmt.Errorf("user %q is not in %s", username, userStoreFile)
}
return u, nil
}
// ServeStdio serves one SFTP session for u on rw, normally the stdin and
// stdout sshd gives a Subsystem command. Nothing else may write to stdout.
func ServeStdio(options *SFTPServiceOptions, u *user.User, rw io.ReadWriteCloser) error {
fs := NewSftpServer(
options.Filer,
options.GrpcDialOption,
options.DataCenter,
options.FilerGroup,
u,
options.FilerSigningKey,
options.FilerSigningExpiresAfter,
options.BucketsDir,
)
if u.HomeDir != "/" {
if err := fs.EnsureHomeDirectory(); err != nil {
glog.Errorf("Failed to ensure home directory for user %s: %v", u.Username, err)
}
}
return handleSFTP(rw, &fs)
}
// Stdio joins stdin and stdout into one stream
type Stdio struct{}
func (Stdio) Read(p []byte) (int, error) { return os.Stdin.Read(p) }
func (Stdio) Write(p []byte) (int, error) { return os.Stdout.Write(p) }
func (Stdio) Close() error {
_ = os.Stdin.Close()
return os.Stdout.Close()
}