mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-03 05:45:27 +09:00
ssh/tailssh: move agent forwarding and policy helpers to shared files
Move handleSSHAgentForwarding and closeAgentForwarding from process_unix.go into tailssh.go, since they only use the platform seams (fwdListenAgent and removeAgentForwardingDir) and have no Unix-specific code of their own. Move the sshForwardGatewayPorts knob into fwdmonitor.go next to its only user, and move the evalSSHPolicy and matchRule convenience wrappers into the test file, which is the only place that calls them. This is a no-op refactor in preparation for a port of the SSH server to another platform, so that the port does not need its own copies of these functions or carry unused declarations. Updates #cleanup Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com> Change-Id: I7c3e1f0a9d2b4e6f8a1c3e5d7f9b1d3f5a7c9e1b
This commit is contained in:
1 parent
2711281ce4
commit
217b17246f
4 files changed
+74
-70
No files matched your search
@@ -45,12 +45,17 @@ import (
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
|
||||
"tailscale.com/envknob"
|
||||
"tailscale.com/types/logger"
|
||||
|
||||
gliderssh "github.com/tailscale/gliderssh"
|
||||
gossh "golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
// sshForwardGatewayPorts permits reverse TCP forwards to bind
|
||||
// non-loopback addresses. It is the equivalent of sshd's GatewayPorts.
|
||||
var sshForwardGatewayPorts = envknob.RegisterBool("TS_SSH_FORWARD_GATEWAY_PORTS")
|
||||
|
||||
// fwdMaxSunPathLen is the sun_path capacity. It is the lexical length
|
||||
// cap on unix socket paths, the same one gliderssh's path validator
|
||||
// enforced. The cap is 108 on Linux and 104 on macOS.
|
||||
|
||||
@@ -11,12 +11,9 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
gliderssh "github.com/tailscale/gliderssh"
|
||||
)
|
||||
|
||||
// monitorProcess identifies one exec child. ID is the exec request ID, which
|
||||
@@ -149,58 +146,3 @@ func systemHostKeyFile(typ string) string {
|
||||
}
|
||||
return "/etc/ssh/ssh_host_" + typ + "_key"
|
||||
}
|
||||
|
||||
// handleSSHAgentForwarding starts a Unix socket listener and in the background
|
||||
// forwards agent connections between the listener and the gliderssh.Session.
|
||||
// On success, it assigns ss.agentListener.
|
||||
//
|
||||
// The listener and its containing directory are created by a user-cred
|
||||
// child through conn.fwdListenAgent. See the docs there for the modes.
|
||||
func (ss *sshSession) handleSSHAgentForwarding(s gliderssh.Session, lu *userMeta) error {
|
||||
if !gliderssh.AgentRequested(ss) || !ss.conn.finalAction.AllowAgentForwarding {
|
||||
return nil
|
||||
}
|
||||
// The latched AllowAgentForwarding bit may be stale. See recheckPolicy.
|
||||
if !ss.conn.recheckPolicy() {
|
||||
return nil
|
||||
}
|
||||
if sshDisableForwarding() {
|
||||
// TODO(bradfitz): or do we want to return an error here instead so the user
|
||||
// gets an error if they ran with ssh -A? But for now we just silently
|
||||
// don't work, like the condition above.
|
||||
return nil
|
||||
}
|
||||
ss.logf("ssh: agent forwarding requested")
|
||||
ln, socket, err := ss.conn.fwdListenAgent(s.Context(), lu)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// The listener's own address is the kernel's bound path; socket is
|
||||
// the path the child reported and the monitor validated. They must
|
||||
// agree: the cleanup removes the directory of the validated path, so
|
||||
// a child whose report and actual bind disagree is refused.
|
||||
if got := ln.Addr().String(); got != socket {
|
||||
ln.Close()
|
||||
return fmt.Errorf("agent forwarding socket path %q does not match the listener's bound path %q", socket, got)
|
||||
}
|
||||
ss.agentListener = ln
|
||||
go gliderssh.ForwardAgentConnections(ln, s)
|
||||
return nil
|
||||
}
|
||||
|
||||
// closeAgentForwarding closes the agent forwarding listener and removes the
|
||||
// temporary directory that holds its socket, so neither outlives the
|
||||
// session. The removal rules are platform-specific: on the monitor
|
||||
// platforms the directory was created by a user-cred child while the
|
||||
// removal runs with the daemon's credentials; see
|
||||
// removeAgentForwardingDir in fwdmonitor.go and fwdplan9.go.
|
||||
func (ss *sshSession) closeAgentForwarding() {
|
||||
ln := ss.agentListener
|
||||
if ln == nil {
|
||||
return
|
||||
}
|
||||
ss.agentListener = nil
|
||||
dir := filepath.Dir(ln.Addr().String())
|
||||
ln.Close()
|
||||
removeAgentForwardingDir(dir, ss.conn.localUser, ss.logf)
|
||||
}
|
||||
+57
-12
@@ -55,10 +55,7 @@ var (
|
||||
sshVerboseLogging = envknob.RegisterBool("TS_DEBUG_SSH_VLOG")
|
||||
sshDisableSFTP = envknob.RegisterBool("TS_SSH_DISABLE_SFTP")
|
||||
sshDisableForwarding = envknob.RegisterBool("TS_SSH_DISABLE_FORWARDING")
|
||||
// sshForwardGatewayPorts permits reverse TCP forwards to bind
|
||||
// non-loopback addresses. It is the equivalent of sshd's GatewayPorts.
|
||||
sshForwardGatewayPorts = envknob.RegisterBool("TS_SSH_FORWARD_GATEWAY_PORTS")
|
||||
sshDisablePTY = envknob.RegisterBool("TS_SSH_DISABLE_PTY")
|
||||
sshDisablePTY = envknob.RegisterBool("TS_SSH_DISABLE_PTY")
|
||||
|
||||
// Session teardown timeouts. Unset or non-positive values use the defaults.
|
||||
sshShutdownTimeout = envknob.RegisterDuration("TS_SSH_SHUTDOWN_TIMEOUT")
|
||||
@@ -1439,6 +1436,62 @@ func (c *conn) detachSession(ss *sshSession) {
|
||||
|
||||
var errSessionDone = errors.New("session is done")
|
||||
|
||||
// handleSSHAgentForwarding starts a Unix socket listener and in the background
|
||||
// forwards agent connections between the listener and the gliderssh.Session.
|
||||
// On success, it assigns ss.agentListener.
|
||||
//
|
||||
// The listener and its containing directory come from the platform's
|
||||
// conn.fwdListenAgent: on the monitor platforms a user-cred child creates
|
||||
// them, on plan9 tailscaled does. See the docs there for the modes.
|
||||
func (ss *sshSession) handleSSHAgentForwarding(s gliderssh.Session, lu *userMeta) error {
|
||||
if !gliderssh.AgentRequested(ss) || !ss.conn.finalAction.AllowAgentForwarding {
|
||||
return nil
|
||||
}
|
||||
// The latched AllowAgentForwarding bit may be stale. See recheckPolicy.
|
||||
if !ss.conn.recheckPolicy() {
|
||||
return nil
|
||||
}
|
||||
if sshDisableForwarding() {
|
||||
// TODO(bradfitz): or do we want to return an error here instead so the user
|
||||
// gets an error if they ran with ssh -A? But for now we just silently
|
||||
// don't work, like the condition above.
|
||||
return nil
|
||||
}
|
||||
ss.logf("ssh: agent forwarding requested")
|
||||
ln, socket, err := ss.conn.fwdListenAgent(s.Context(), lu)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// The listener's own address is the kernel's bound path; socket is
|
||||
// the path the child reported and the monitor validated. They must
|
||||
// agree: the cleanup removes the directory of the validated path, so
|
||||
// a child whose report and actual bind disagree is refused.
|
||||
if got := ln.Addr().String(); got != socket {
|
||||
ln.Close()
|
||||
return fmt.Errorf("agent forwarding socket path %q does not match the listener's bound path %q", socket, got)
|
||||
}
|
||||
ss.agentListener = ln
|
||||
go gliderssh.ForwardAgentConnections(ln, s)
|
||||
return nil
|
||||
}
|
||||
|
||||
// closeAgentForwarding closes the agent forwarding listener and removes the
|
||||
// temporary directory that holds its socket, so neither outlives the
|
||||
// session. The removal rules are platform-specific: on the monitor
|
||||
// platforms the directory was created by a user-cred child while the
|
||||
// removal runs with the daemon's credentials; see
|
||||
// removeAgentForwardingDir in fwdmonitor.go and fwdplan9.go.
|
||||
func (ss *sshSession) closeAgentForwarding() {
|
||||
ln := ss.agentListener
|
||||
if ln == nil {
|
||||
return
|
||||
}
|
||||
ss.agentListener = nil
|
||||
dir := filepath.Dir(ln.Addr().String())
|
||||
ln.Close()
|
||||
removeAgentForwardingDir(dir, ss.conn.localUser, ss.logf)
|
||||
}
|
||||
|
||||
// run is the entrypoint for a newly accepted SSH session.
|
||||
//
|
||||
// It handles ss once it's been accepted and determined
|
||||
@@ -1676,10 +1729,6 @@ func (c *conn) ruleExpired(r *tailcfg.SSHRule) bool {
|
||||
return r.RuleExpires.Before(c.srv.now())
|
||||
}
|
||||
|
||||
func (c *conn) evalSSHPolicy(pol *tailcfg.SSHPolicy) (*tailcfg.SSHAction, string, []string, *tailcfg.SSHRule, evalResult) {
|
||||
return c.evalSSHPolicyWithUserLookup(pol, userLookup)
|
||||
}
|
||||
|
||||
func (c *conn) evalSSHPolicyWithUserLookup(pol *tailcfg.SSHPolicy, lookup func(string) (*userMeta, error)) (a *tailcfg.SSHAction, localUser string, acceptEnv []string, rule *tailcfg.SSHRule, result evalResult) {
|
||||
failedOnUser := false
|
||||
for _, r := range pol.Rules {
|
||||
@@ -1706,10 +1755,6 @@ var (
|
||||
errInvalidConn = errors.New("invalid connection state")
|
||||
)
|
||||
|
||||
func (c *conn) matchRule(r *tailcfg.SSHRule) (*tailcfg.SSHAction, string, []string, error) {
|
||||
return c.matchRuleWithUserLookup(r, userLookup)
|
||||
}
|
||||
|
||||
func (c *conn) matchRuleWithUserLookup(r *tailcfg.SSHRule, lookup func(string) (*userMeta, error)) (a *tailcfg.SSHAction, localUser string, acceptEnv []string, err error) {
|
||||
defer func() {
|
||||
c.vlogf("matchRule(%+v): %v", r, err)
|
||||
|
||||
@@ -1760,3 +1760,15 @@ func TestIsRootUser(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// evalSSHPolicy is [conn.evalSSHPolicyWithUserLookup] with the real user
|
||||
// lookup, for tests. Production callers go through evaluatePolicy.
|
||||
func (c *conn) evalSSHPolicy(pol *tailcfg.SSHPolicy) (*tailcfg.SSHAction, string, []string, *tailcfg.SSHRule, evalResult) {
|
||||
return c.evalSSHPolicyWithUserLookup(pol, userLookup)
|
||||
}
|
||||
|
||||
// matchRule is [conn.matchRuleWithUserLookup] with the real user lookup,
|
||||
// for tests.
|
||||
func (c *conn) matchRule(r *tailcfg.SSHRule) (*tailcfg.SSHAction, string, []string, error) {
|
||||
return c.matchRuleWithUserLookup(r, userLookup)
|
||||
}
|
||||
Reference in new issue
Block a user