mirror of
https://github.com/tailscale/tailscale.git
synced 2026-09-22 11:35:12 -04:00
The main server file mixed portable session handling with Unix details: sending SIGHUP to end a session, decoding exec.ExitError, the euid check for whether the process can switch users, agent forwarding's chown of the socket, and reading /etc/ssh host keys as root. Those now sit behind small functions (hangupProcess, waitProcess, canSwitchToLocalUser, handleSSHAgentForwarding, systemHostKeyFile, isRootUser) in the new process_unix.go, along with the incubator's forwarded-environment pipe helpers, and the session's *exec.Cmd moves into an embedded osSessionState struct defined there, so that the portable code no longer refers to the process representation at all. user.go keeps only the portable userMeta and userLookup; the login shell and default PATH logic moves to user_unix.go. The SFTP child entrypoint and its stdio adapter, which incubator.go and incubator_plan9.go each had a copy of, move to sftp.go. The c2n usernames handler gains a hook for platforms that list users some other way than /etc/passwd. The agent socket's uid and gid are parsed as 31-bit rather than 32-bit unsigned values so that the conversion to int for os.Chown cannot overflow on 32-bit platforms, which is the pattern CodeQL flags. Updates #cleanup Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com> Change-Id: I4c7e2b9a0d3f5e1c8b6a4d2f0e9c7b5a3d1f8e6c
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build (linux && !android) || (darwin && !ios) || freebsd || openbsd || plan9
|
|
|
|
package tailssh
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/pkg/sftp"
|
|
)
|
|
|
|
// stdRWC is an io.ReadWriteCloser over the process's stdin and stdout, for
|
|
// serving SFTP in a child process whose standard handles are connected to
|
|
// the SSH session.
|
|
type stdRWC struct{}
|
|
|
|
func (stdRWC) Read(p []byte) (n int, err error) {
|
|
return os.Stdin.Read(p)
|
|
}
|
|
|
|
func (stdRWC) Write(b []byte) (n int, err error) {
|
|
return os.Stdout.Write(b)
|
|
}
|
|
|
|
func (stdRWC) Close() error {
|
|
os.Exit(0)
|
|
return nil
|
|
}
|
|
|
|
// beSFTP is the entrypoint to the "tailscaled be-child sftp" subcommand.
|
|
// It serves SFTP in-process over stdin and stdout.
|
|
func beSFTP(args []string) error {
|
|
return serveSFTP()
|
|
}
|
|
|
|
func serveSFTP() error {
|
|
server, err := sftp.NewServer(stdRWC{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// TODO(https://github.com/pkg/sftp/pull/554): Revert the check for io.EOF,
|
|
// when sftp is patched to report clean termination.
|
|
if err := server.Serve(); err != nil && err != io.EOF {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|