2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-02-15 19:59:21 +00:00
|
|
|
|
2023-01-06 23:39:34 +00:00
|
|
|
//go:build linux || (darwin && !ios) || freebsd || openbsd
|
2022-02-15 19:59:21 +00:00
|
|
|
|
|
|
|
// Package tailssh is an SSH server integrated into Tailscale.
|
|
|
|
package tailssh
|
|
|
|
|
|
|
|
import (
|
2022-04-01 19:57:12 +00:00
|
|
|
"bytes"
|
2022-02-23 23:47:57 +00:00
|
|
|
"context"
|
2022-03-13 20:01:59 +00:00
|
|
|
"crypto/rand"
|
2022-04-01 19:57:12 +00:00
|
|
|
"encoding/base64"
|
2022-02-15 19:59:21 +00:00
|
|
|
"encoding/json"
|
2022-02-18 22:10:26 +00:00
|
|
|
"errors"
|
2022-02-15 19:59:21 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2022-03-10 18:28:42 +00:00
|
|
|
"net/http"
|
2022-07-26 03:55:44 +00:00
|
|
|
"net/netip"
|
2022-03-23 00:36:55 +00:00
|
|
|
"net/url"
|
2022-02-15 19:59:21 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-02-19 03:07:04 +00:00
|
|
|
"os/user"
|
2022-03-14 20:26:06 +00:00
|
|
|
"path/filepath"
|
2022-03-13 05:32:17 +00:00
|
|
|
"runtime"
|
2022-03-14 20:26:06 +00:00
|
|
|
"strconv"
|
2022-02-19 03:07:04 +00:00
|
|
|
"strings"
|
2022-03-09 05:35:55 +00:00
|
|
|
"sync"
|
2022-11-15 19:46:15 +00:00
|
|
|
"sync/atomic"
|
2022-02-18 22:10:26 +00:00
|
|
|
"time"
|
2022-02-15 19:59:21 +00:00
|
|
|
|
2022-03-30 00:31:44 +00:00
|
|
|
gossh "github.com/tailscale/golang-x-crypto/ssh"
|
2022-02-15 19:59:21 +00:00
|
|
|
"tailscale.com/envknob"
|
|
|
|
"tailscale.com/ipn/ipnlocal"
|
2022-03-10 18:28:42 +00:00
|
|
|
"tailscale.com/logtail/backoff"
|
2022-02-15 19:59:21 +00:00
|
|
|
"tailscale.com/net/tsaddr"
|
2022-02-18 22:10:26 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2022-03-25 22:35:36 +00:00
|
|
|
"tailscale.com/tempfork/gliderlabs/ssh"
|
2022-02-15 19:59:21 +00:00
|
|
|
"tailscale.com/types/logger"
|
2022-10-09 00:54:53 +00:00
|
|
|
"tailscale.com/types/netmap"
|
2022-06-02 03:48:22 +00:00
|
|
|
"tailscale.com/util/clientmetric"
|
2022-04-22 03:25:24 +00:00
|
|
|
"tailscale.com/util/mak"
|
2022-02-15 19:59:21 +00:00
|
|
|
)
|
|
|
|
|
2022-04-15 20:19:13 +00:00
|
|
|
var (
|
2022-09-14 19:49:39 +00:00
|
|
|
sshVerboseLogging = envknob.RegisterBool("TS_DEBUG_SSH_VLOG")
|
2022-04-15 20:19:13 +00:00
|
|
|
)
|
2022-04-01 19:57:12 +00:00
|
|
|
|
2022-10-11 21:48:01 +00:00
|
|
|
const (
|
|
|
|
// forcePasswordSuffix is the suffix at the end of a username that forces
|
|
|
|
// Tailscale SSH into password authentication mode to work around buggy SSH
|
|
|
|
// clients that get confused by successful replies to auth type "none".
|
|
|
|
forcePasswordSuffix = "+password"
|
|
|
|
)
|
|
|
|
|
2022-10-09 00:54:53 +00:00
|
|
|
// ipnLocalBackend is the subset of ipnlocal.LocalBackend that we use.
|
|
|
|
// It is used for testing.
|
|
|
|
type ipnLocalBackend interface {
|
|
|
|
GetSSH_HostKeys() ([]gossh.Signer, error)
|
|
|
|
ShouldRunSSH() bool
|
|
|
|
NetMap() *netmap.NetworkMap
|
|
|
|
WhoIs(ipp netip.AddrPort) (n *tailcfg.Node, u tailcfg.UserProfile, ok bool)
|
|
|
|
DoNoiseRequest(req *http.Request) (*http.Response, error)
|
|
|
|
TailscaleVarRoot() string
|
|
|
|
}
|
|
|
|
|
2022-04-15 20:19:13 +00:00
|
|
|
type server struct {
|
2022-10-09 00:54:53 +00:00
|
|
|
lb ipnLocalBackend
|
2022-04-15 20:19:13 +00:00
|
|
|
logf logger.Logf
|
|
|
|
tailscaledPath string
|
2022-02-15 19:59:21 +00:00
|
|
|
|
2022-04-17 04:49:22 +00:00
|
|
|
pubKeyHTTPClient *http.Client // or nil for http.DefaultClient
|
|
|
|
timeNow func() time.Time // or nil for time.Now
|
|
|
|
|
2022-05-28 11:33:46 +00:00
|
|
|
sessionWaitGroup sync.WaitGroup
|
|
|
|
|
2022-04-17 04:49:22 +00:00
|
|
|
// mu protects the following
|
2022-06-27 18:50:11 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
activeConns map[*conn]bool // set; value is always true
|
|
|
|
fetchPublicKeysCache map[string]pubKeyCacheEntry // by https URL
|
|
|
|
shutdownCalled bool
|
2022-04-17 04:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *server) now() time.Time {
|
2022-06-27 19:13:50 +00:00
|
|
|
if srv != nil && srv.timeNow != nil {
|
2022-04-17 04:49:22 +00:00
|
|
|
return srv.timeNow()
|
|
|
|
}
|
|
|
|
return time.Now()
|
2022-04-15 20:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
ipnlocal.RegisterNewSSHServer(func(logf logger.Logf, lb *ipnlocal.LocalBackend) (ipnlocal.SSHServer, error) {
|
|
|
|
tsd, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
srv := &server{
|
|
|
|
lb: lb,
|
|
|
|
logf: logf,
|
|
|
|
tailscaledPath: tsd,
|
|
|
|
}
|
|
|
|
return srv, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-06 17:34:58 +00:00
|
|
|
// attachSessionToConnIfNotShutdown ensures that srv is not shutdown before
|
|
|
|
// attaching the session to the conn. This ensures that once Shutdown is called,
|
|
|
|
// new sessions are not allowed and existing ones are cleaned up.
|
|
|
|
// It reports whether ss was attached to the conn.
|
|
|
|
func (srv *server) attachSessionToConnIfNotShutdown(ss *sshSession) bool {
|
|
|
|
srv.mu.Lock()
|
|
|
|
defer srv.mu.Unlock()
|
|
|
|
if srv.shutdownCalled {
|
|
|
|
// Do not start any new sessions.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
ss.conn.attachSession(ss)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-06-27 18:50:11 +00:00
|
|
|
func (srv *server) trackActiveConn(c *conn, add bool) {
|
|
|
|
srv.mu.Lock()
|
|
|
|
defer srv.mu.Unlock()
|
|
|
|
if add {
|
|
|
|
mak.Set(&srv.activeConns, c, true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(srv.activeConns, c)
|
|
|
|
}
|
|
|
|
|
2022-04-15 20:19:13 +00:00
|
|
|
// HandleSSHConn handles a Tailscale SSH connection from c.
|
2022-06-27 18:50:11 +00:00
|
|
|
// This is the entry point for all SSH connections.
|
|
|
|
// When this returns, the connection is closed.
|
|
|
|
func (srv *server) HandleSSHConn(nc net.Conn) error {
|
2022-06-02 03:48:22 +00:00
|
|
|
metricIncomingConnections.Add(1)
|
2022-06-27 18:50:11 +00:00
|
|
|
c, err := srv.newConn()
|
2022-02-24 21:31:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-27 18:50:11 +00:00
|
|
|
srv.trackActiveConn(c, true) // add
|
|
|
|
defer srv.trackActiveConn(c, false) // remove
|
|
|
|
c.HandleConn(nc)
|
2022-04-01 19:57:12 +00:00
|
|
|
|
|
|
|
// Return nil to signal to netstack's interception that it doesn't need to
|
|
|
|
// log. If ss.HandleConn had problems, it can log itself (ideally on an
|
|
|
|
// sshSession.logf).
|
2022-02-24 21:31:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-28 11:33:46 +00:00
|
|
|
// Shutdown terminates all active sessions.
|
|
|
|
func (srv *server) Shutdown() {
|
|
|
|
srv.mu.Lock()
|
|
|
|
srv.shutdownCalled = true
|
2022-06-27 18:50:11 +00:00
|
|
|
for c := range srv.activeConns {
|
2022-10-06 17:34:58 +00:00
|
|
|
c.Close()
|
2022-05-28 11:33:46 +00:00
|
|
|
}
|
|
|
|
srv.mu.Unlock()
|
|
|
|
srv.sessionWaitGroup.Wait()
|
|
|
|
}
|
|
|
|
|
2022-04-17 18:49:56 +00:00
|
|
|
// OnPolicyChange terminates any active sessions that no longer match
|
|
|
|
// the SSH access policy.
|
|
|
|
func (srv *server) OnPolicyChange() {
|
|
|
|
srv.mu.Lock()
|
|
|
|
defer srv.mu.Unlock()
|
2022-06-27 18:50:11 +00:00
|
|
|
for c := range srv.activeConns {
|
2022-10-06 17:34:58 +00:00
|
|
|
if c.info == nil {
|
2022-07-21 15:46:55 +00:00
|
|
|
// c.info is nil when the connection hasn't been authenticated yet.
|
|
|
|
// In that case, the connection will be terminated when it is.
|
|
|
|
continue
|
|
|
|
}
|
2022-06-27 18:50:11 +00:00
|
|
|
go c.checkStillValid()
|
2022-04-17 18:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 20:39:15 +00:00
|
|
|
// conn represents a single SSH connection and its associated
|
|
|
|
// ssh.Server.
|
2022-10-06 17:34:58 +00:00
|
|
|
//
|
|
|
|
// During the lifecycle of a connection, the following are called in order:
|
|
|
|
// Setup and discover server info
|
|
|
|
// - ServerConfigCallback
|
|
|
|
//
|
|
|
|
// Do the user auth
|
|
|
|
// - NoClientAuthHandler
|
|
|
|
// - PublicKeyHandler (only if NoClientAuthHandler returns errPubKeyRequired)
|
|
|
|
//
|
|
|
|
// Once auth is done, the conn can be multiplexed with multiple sessions and
|
|
|
|
// channels concurrently. At which point any of the following can be called
|
|
|
|
// in any order.
|
|
|
|
// - c.handleSessionPostSSHAuth
|
|
|
|
// - c.mayForwardLocalPortTo followed by ssh.DirectTCPIPHandler
|
2022-04-20 20:39:15 +00:00
|
|
|
type conn struct {
|
|
|
|
*ssh.Server
|
2022-10-06 17:34:58 +00:00
|
|
|
srv *server
|
2022-04-21 00:36:19 +00:00
|
|
|
|
2022-06-27 18:50:11 +00:00
|
|
|
insecureSkipTailscaleAuth bool // used by tests.
|
|
|
|
|
|
|
|
// idH is the RFC4253 sec8 hash H. It is used to identify the connection,
|
|
|
|
// and is shared among all sessions. It should not be shared outside
|
|
|
|
// process. It is confusingly referred to as SessionID by the gliderlabs/ssh
|
|
|
|
// library.
|
2022-10-06 17:34:58 +00:00
|
|
|
idH string
|
|
|
|
connID string // ID that's shared with control
|
|
|
|
|
2022-10-11 21:48:01 +00:00
|
|
|
// anyPasswordIsOkay is whether the client is authorized but has requested
|
|
|
|
// password-based auth to work around their buggy SSH client. When set, we
|
|
|
|
// accept any password in the PasswordHandler.
|
|
|
|
anyPasswordIsOkay bool // set by NoClientAuthCallback
|
2022-10-06 17:34:58 +00:00
|
|
|
|
|
|
|
action0 *tailcfg.SSHAction // set by doPolicyAuth; first matching action
|
|
|
|
currentAction *tailcfg.SSHAction // set by doPolicyAuth, updated by resolveNextAction
|
|
|
|
finalAction *tailcfg.SSHAction // set by doPolicyAuth or resolveNextAction
|
|
|
|
finalActionErr error // set by doPolicyAuth or resolveNextAction
|
|
|
|
|
|
|
|
info *sshConnInfo // set by setInfo
|
|
|
|
localUser *user.User // set by doPolicyAuth
|
|
|
|
userGroupIDs []string // set by doPolicyAuth
|
|
|
|
pubKey gossh.PublicKey // set by doPolicyAuth
|
|
|
|
|
|
|
|
// mu protects the following fields.
|
|
|
|
//
|
|
|
|
// srv.mu should be acquired prior to mu.
|
|
|
|
// It is safe to just acquire mu, but unsafe to
|
|
|
|
// acquire mu and then srv.mu.
|
|
|
|
mu sync.Mutex // protects the following
|
|
|
|
sessions []*sshSession
|
2022-04-20 20:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) logf(format string, args ...any) {
|
2022-06-27 18:50:11 +00:00
|
|
|
format = fmt.Sprintf("%v: %v", c.connID, format)
|
2022-04-20 20:39:15 +00:00
|
|
|
c.srv.logf(format, args...)
|
|
|
|
}
|
|
|
|
|
2022-10-09 00:54:53 +00:00
|
|
|
// isAuthorized walks through the action chain and returns nil if the connection
|
|
|
|
// is authorized. If the connection is not authorized, it returns
|
|
|
|
// gossh.ErrDenied. If the action chain resolution fails, it returns the
|
|
|
|
// resolution error.
|
2022-10-06 17:34:58 +00:00
|
|
|
func (c *conn) isAuthorized(ctx ssh.Context) error {
|
|
|
|
action := c.currentAction
|
|
|
|
for {
|
|
|
|
if action.Accept {
|
|
|
|
if c.pubKey != nil {
|
|
|
|
metricPublicKeyAccepts.Add(1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if action.Reject || action.HoldAndDelegate == "" {
|
|
|
|
return gossh.ErrDenied
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
action, err = c.resolveNextAction(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-09 17:31:19 +00:00
|
|
|
if action.Message != "" {
|
|
|
|
if err := ctx.SendAuthBanner(action.Message); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// errPubKeyRequired is returned by NoClientAuthCallback to make the client
|
|
|
|
// resort to public-key auth; not user visible.
|
|
|
|
var errPubKeyRequired = errors.New("ssh publickey required")
|
|
|
|
|
|
|
|
// NoClientAuthCallback implements gossh.NoClientAuthCallback and is called by
|
2022-09-25 18:29:55 +00:00
|
|
|
// the ssh.Server when the client first connects with the "none"
|
2022-04-20 20:39:15 +00:00
|
|
|
// authentication method.
|
2022-10-06 17:34:58 +00:00
|
|
|
//
|
|
|
|
// It is responsible for continuing policy evaluation from BannerCallback (or
|
|
|
|
// starting it afresh). It returns an error if the policy evaluation fails, or
|
|
|
|
// if the decision is "reject"
|
|
|
|
//
|
|
|
|
// It either returns nil (accept) or errPubKeyRequired or gossh.ErrDenied
|
|
|
|
// (reject). The errors may be wrapped.
|
|
|
|
func (c *conn) NoClientAuthCallback(ctx ssh.Context) error {
|
2022-04-20 20:39:15 +00:00
|
|
|
if c.insecureSkipTailscaleAuth {
|
2022-10-06 17:34:58 +00:00
|
|
|
return nil
|
2022-04-20 20:39:15 +00:00
|
|
|
}
|
2022-10-09 17:31:19 +00:00
|
|
|
if err := c.doPolicyAuth(ctx, nil /* no pub key */); err != nil {
|
|
|
|
return err
|
2022-04-20 20:39:15 +00:00
|
|
|
}
|
2022-10-11 21:48:01 +00:00
|
|
|
if err := c.isAuthorized(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let users specify a username ending in +password to force password auth.
|
|
|
|
// This exists for buggy SSH clients that get confused by success from
|
|
|
|
// "none" auth.
|
|
|
|
if strings.HasSuffix(ctx.User(), forcePasswordSuffix) {
|
|
|
|
c.anyPasswordIsOkay = true
|
|
|
|
return errors.New("any password please") // not shown to users
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) nextAuthMethodCallback(cm gossh.ConnMetadata, prevErrors []error) (nextMethod []string) {
|
|
|
|
switch {
|
|
|
|
case c.anyPasswordIsOkay:
|
|
|
|
nextMethod = append(nextMethod, "password")
|
|
|
|
case len(prevErrors) > 0 && prevErrors[len(prevErrors)-1] == errPubKeyRequired:
|
|
|
|
nextMethod = append(nextMethod, "publickey")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The fake "tailscale" method is always appended to next so OpenSSH renders
|
|
|
|
// that in parens as the final failure. (It also shows up in "ssh -v", etc)
|
|
|
|
nextMethod = append(nextMethod, "tailscale")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fakePasswordHandler is our implementation of the PasswordHandler hook that
|
|
|
|
// checks whether the user's password is correct. But we don't actually use
|
|
|
|
// passwords. This exists only for when the user's username ends in "+password"
|
|
|
|
// to signal that their SSH client is buggy and gets confused by auth type
|
|
|
|
// "none" succeeding and they want our SSH server to require a dummy password
|
|
|
|
// prompt instead. We then accept any password since we've already authenticated
|
|
|
|
// & authorized them.
|
|
|
|
func (c *conn) fakePasswordHandler(ctx ssh.Context, password string) bool {
|
|
|
|
return c.anyPasswordIsOkay
|
2022-04-21 00:36:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 17:34:58 +00:00
|
|
|
// PublicKeyHandler implements ssh.PublicKeyHandler is called by the
|
|
|
|
// ssh.Server when the client presents a public key.
|
|
|
|
func (c *conn) PublicKeyHandler(ctx ssh.Context, pubKey ssh.PublicKey) error {
|
|
|
|
if err := c.doPolicyAuth(ctx, pubKey); err != nil {
|
|
|
|
// TODO(maisem/bradfitz): surface the error here.
|
|
|
|
c.logf("rejecting SSH public key %s: %v", bytes.TrimSpace(gossh.MarshalAuthorizedKey(pubKey)), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.isAuthorized(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.logf("accepting SSH public key %s", bytes.TrimSpace(gossh.MarshalAuthorizedKey(pubKey)))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// doPolicyAuth verifies that conn can proceed with the specified (optional)
|
2022-04-21 00:36:19 +00:00
|
|
|
// pubKey. It returns nil if the matching policy action is Accept or
|
|
|
|
// HoldAndDelegate. If pubKey is nil, there was no policy match but there is a
|
|
|
|
// policy that might match a public key it returns errPubKeyRequired. Otherwise,
|
2022-10-09 17:31:19 +00:00
|
|
|
// it returns gossh.ErrDenied.
|
2022-10-06 17:34:58 +00:00
|
|
|
func (c *conn) doPolicyAuth(ctx ssh.Context, pubKey ssh.PublicKey) error {
|
2022-10-09 17:31:19 +00:00
|
|
|
if err := c.setInfo(ctx); err != nil {
|
|
|
|
c.logf("failed to get conninfo: %v", err)
|
|
|
|
return gossh.ErrDenied
|
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
a, localUser, err := c.evaluatePolicy(pubKey)
|
|
|
|
if err != nil {
|
2022-07-21 15:46:55 +00:00
|
|
|
if pubKey == nil && c.havePubKeyPolicy() {
|
2022-04-21 00:36:19 +00:00
|
|
|
return errPubKeyRequired
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%w: %v", gossh.ErrDenied, err)
|
2022-04-20 20:39:15 +00:00
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
c.action0 = a
|
2022-10-06 17:34:58 +00:00
|
|
|
c.currentAction = a
|
|
|
|
c.pubKey = pubKey
|
2022-10-09 17:31:19 +00:00
|
|
|
if a.Message != "" {
|
|
|
|
if err := ctx.SendAuthBanner(a.Message); err != nil {
|
|
|
|
return fmt.Errorf("SendBanner: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
if a.Accept || a.HoldAndDelegate != "" {
|
2022-10-06 17:34:58 +00:00
|
|
|
if a.Accept {
|
|
|
|
c.finalAction = a
|
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
lu, err := user.Lookup(localUser)
|
|
|
|
if err != nil {
|
2022-12-20 19:26:50 +00:00
|
|
|
c.logf("failed to look up %v: %v", localUser, err)
|
|
|
|
ctx.SendAuthBanner(fmt.Sprintf("failed to look up %v\r\n", localUser))
|
2022-10-09 17:31:19 +00:00
|
|
|
return err
|
2022-04-21 00:36:19 +00:00
|
|
|
}
|
2022-04-21 21:44:39 +00:00
|
|
|
gids, err := lu.GroupIds()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.userGroupIDs = gids
|
2022-04-21 00:36:19 +00:00
|
|
|
c.localUser = lu
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if a.Reject {
|
2022-10-06 17:34:58 +00:00
|
|
|
c.finalAction = a
|
2022-10-09 17:31:19 +00:00
|
|
|
return gossh.ErrDenied
|
2022-04-20 20:39:15 +00:00
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
// Shouldn't get here, but:
|
|
|
|
return gossh.ErrDenied
|
2022-04-20 20:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServerConfig implements ssh.ServerConfigCallback.
|
|
|
|
func (c *conn) ServerConfig(ctx ssh.Context) *gossh.ServerConfig {
|
|
|
|
return &gossh.ServerConfig{
|
2022-10-11 21:48:01 +00:00
|
|
|
NoClientAuth: true, // required for the NoClientAuthCallback to run
|
|
|
|
NextAuthMethodCallback: c.nextAuthMethodCallback,
|
2022-04-20 20:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *server) newConn() (*conn, error) {
|
2022-05-28 11:33:46 +00:00
|
|
|
srv.mu.Lock()
|
2022-06-27 18:50:11 +00:00
|
|
|
if srv.shutdownCalled {
|
|
|
|
srv.mu.Unlock()
|
2022-05-28 11:33:46 +00:00
|
|
|
// Stop accepting new connections.
|
|
|
|
// Connections in the auth phase are handled in handleConnPostSSHAuth.
|
|
|
|
// Existing sessions are terminated by Shutdown.
|
|
|
|
return nil, gossh.ErrDenied
|
|
|
|
}
|
2022-06-27 18:50:11 +00:00
|
|
|
srv.mu.Unlock()
|
2022-06-27 19:13:50 +00:00
|
|
|
c := &conn{srv: srv}
|
|
|
|
now := srv.now()
|
2022-07-21 18:37:34 +00:00
|
|
|
c.connID = fmt.Sprintf("ssh-conn-%s-%02x", now.UTC().Format("20060102T150405"), randBytes(5))
|
2022-04-20 20:39:15 +00:00
|
|
|
c.Server = &ssh.Server{
|
2022-10-06 17:34:58 +00:00
|
|
|
Version: "Tailscale",
|
|
|
|
ServerConfigCallback: c.ServerConfig,
|
|
|
|
|
|
|
|
NoClientAuthHandler: c.NoClientAuthCallback,
|
|
|
|
PublicKeyHandler: c.PublicKeyHandler,
|
2022-10-11 21:48:01 +00:00
|
|
|
PasswordHandler: c.fakePasswordHandler,
|
2022-10-06 17:34:58 +00:00
|
|
|
|
|
|
|
Handler: c.handleSessionPostSSHAuth,
|
|
|
|
LocalPortForwardingCallback: c.mayForwardLocalPortTo,
|
2022-04-21 17:11:16 +00:00
|
|
|
SubsystemHandlers: map[string]ssh.SubsystemHandler{
|
2022-06-27 18:50:11 +00:00
|
|
|
"sftp": c.handleSessionPostSSHAuth,
|
2022-04-21 17:11:16 +00:00
|
|
|
},
|
2022-03-09 19:25:07 +00:00
|
|
|
// Note: the direct-tcpip channel handler and LocalPortForwardingCallback
|
|
|
|
// only adds support for forwarding ports from the local machine.
|
|
|
|
// TODO(maisem/bradfitz): add remote port forwarding support.
|
|
|
|
ChannelHandlers: map[string]ssh.ChannelHandler{
|
|
|
|
"direct-tcpip": ssh.DirectTCPIPHandler,
|
|
|
|
},
|
2022-10-06 17:34:58 +00:00
|
|
|
RequestHandlers: map[string]ssh.RequestHandler{},
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
ss := c.Server
|
2022-02-15 19:59:21 +00:00
|
|
|
for k, v := range ssh.DefaultRequestHandlers {
|
2022-02-24 21:31:54 +00:00
|
|
|
ss.RequestHandlers[k] = v
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
|
|
|
for k, v := range ssh.DefaultChannelHandlers {
|
2022-02-24 21:31:54 +00:00
|
|
|
ss.ChannelHandlers[k] = v
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
|
|
|
for k, v := range ssh.DefaultSubsystemHandlers {
|
2022-02-24 21:31:54 +00:00
|
|
|
ss.SubsystemHandlers[k] = v
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
2022-02-24 21:31:54 +00:00
|
|
|
keys, err := srv.lb.GetSSH_HostKeys()
|
2022-02-17 21:28:14 +00:00
|
|
|
if err != nil {
|
2022-02-24 21:31:54 +00:00
|
|
|
return nil, err
|
2022-02-17 21:28:14 +00:00
|
|
|
}
|
|
|
|
for _, signer := range keys {
|
2022-02-24 21:31:54 +00:00
|
|
|
ss.AddHostKey(signer)
|
2022-02-17 21:28:14 +00:00
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
return c, nil
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 20:23:17 +00:00
|
|
|
// mayForwardLocalPortTo reports whether the ctx should be allowed to port forward
|
2022-03-09 19:25:07 +00:00
|
|
|
// to the specified host and port.
|
|
|
|
// TODO(bradfitz/maisem): should we have more checks on host/port?
|
2022-06-27 18:50:11 +00:00
|
|
|
func (c *conn) mayForwardLocalPortTo(ctx ssh.Context, destinationHost string, destinationPort uint32) bool {
|
|
|
|
if c.finalAction != nil && c.finalAction.AllowLocalPortForwarding {
|
|
|
|
metricLocalPortForward.Add(1)
|
|
|
|
return true
|
2022-03-14 20:23:17 +00:00
|
|
|
}
|
2022-06-27 18:50:11 +00:00
|
|
|
return false
|
2022-03-09 19:25:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 20:39:15 +00:00
|
|
|
// havePubKeyPolicy reports whether any policy rule may provide access by means
|
|
|
|
// of a ssh.PublicKey.
|
2022-07-21 15:46:55 +00:00
|
|
|
func (c *conn) havePubKeyPolicy() bool {
|
2022-10-06 17:34:58 +00:00
|
|
|
if c.info == nil {
|
2022-07-21 15:46:55 +00:00
|
|
|
panic("havePubKeyPolicy called before setInfo")
|
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
// Is there any rule that looks like it'd require a public key for this
|
|
|
|
// sshUser?
|
2022-04-21 00:36:19 +00:00
|
|
|
pol, ok := c.sshPolicy()
|
2022-04-01 19:57:12 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, r := range pol.Rules {
|
2022-04-21 00:36:19 +00:00
|
|
|
if c.ruleExpired(r) {
|
2022-04-01 19:57:12 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-10-06 17:34:58 +00:00
|
|
|
if mapLocalUser(r.SSHUsers, c.info.sshUser) == "" {
|
2022-04-01 19:57:12 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, p := range r.Principals {
|
2022-04-21 00:36:19 +00:00
|
|
|
if len(p.PubKeys) > 0 && c.principalMatchesTailscaleIdentity(p) {
|
2022-04-01 19:57:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-09 05:35:55 +00:00
|
|
|
// sshPolicy returns the SSHPolicy for current node.
|
|
|
|
// If there is no SSHPolicy in the netmap, it returns a debugPolicy
|
|
|
|
// if one is defined.
|
2022-04-21 00:36:19 +00:00
|
|
|
func (c *conn) sshPolicy() (_ *tailcfg.SSHPolicy, ok bool) {
|
|
|
|
lb := c.srv.lb
|
2022-04-29 22:57:55 +00:00
|
|
|
if !lb.ShouldRunSSH() {
|
|
|
|
return nil, false
|
|
|
|
}
|
2022-02-18 22:10:26 +00:00
|
|
|
nm := lb.NetMap()
|
|
|
|
if nm == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2022-09-14 19:49:39 +00:00
|
|
|
if pol := nm.SSHPolicy; pol != nil && !envknob.SSHIgnoreTailnetPolicy() {
|
2022-02-18 22:10:26 +00:00
|
|
|
return pol, true
|
|
|
|
}
|
2022-09-14 19:49:39 +00:00
|
|
|
debugPolicyFile := envknob.SSHPolicyFile()
|
2022-02-18 22:10:26 +00:00
|
|
|
if debugPolicyFile != "" {
|
2022-04-21 00:36:19 +00:00
|
|
|
c.logf("reading debug SSH policy file: %v", debugPolicyFile)
|
2022-02-18 22:10:26 +00:00
|
|
|
f, err := os.ReadFile(debugPolicyFile)
|
|
|
|
if err != nil {
|
2022-04-21 00:36:19 +00:00
|
|
|
c.logf("error reading debug SSH policy file: %v", err)
|
2022-02-18 22:10:26 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
p := new(tailcfg.SSHPolicy)
|
|
|
|
if err := json.Unmarshal(f, p); err != nil {
|
2022-04-21 00:36:19 +00:00
|
|
|
c.logf("invalid JSON in %v: %v", debugPolicyFile, err)
|
2022-02-18 22:10:26 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return p, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
func toIPPort(a net.Addr) (ipp netip.AddrPort) {
|
2022-03-09 05:35:55 +00:00
|
|
|
ta, ok := a.(*net.TCPAddr)
|
2022-02-15 19:59:21 +00:00
|
|
|
if !ok {
|
2022-04-17 18:49:56 +00:00
|
|
|
return
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
2022-08-02 20:38:11 +00:00
|
|
|
tanetaddr, ok := netip.AddrFromSlice(ta.IP)
|
2022-02-15 19:59:21 +00:00
|
|
|
if !ok {
|
2022-04-17 18:49:56 +00:00
|
|
|
return
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
2022-08-02 20:38:11 +00:00
|
|
|
return netip.AddrPortFrom(tanetaddr.Unmap(), uint16(ta.Port))
|
2022-03-09 05:35:55 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 20:39:15 +00:00
|
|
|
// connInfo returns a populated sshConnInfo from the provided arguments,
|
|
|
|
// validating only that they represent a known Tailscale identity.
|
2022-10-06 17:34:58 +00:00
|
|
|
func (c *conn) setInfo(ctx ssh.Context) error {
|
2022-10-09 17:31:19 +00:00
|
|
|
if c.info != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
ci := &sshConnInfo{
|
2022-10-11 21:48:01 +00:00
|
|
|
sshUser: strings.TrimSuffix(ctx.User(), forcePasswordSuffix),
|
2022-10-06 17:34:58 +00:00
|
|
|
src: toIPPort(ctx.RemoteAddr()),
|
|
|
|
dst: toIPPort(ctx.LocalAddr()),
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if !tsaddr.IsTailscaleIP(ci.dst.Addr()) {
|
2022-04-21 00:36:19 +00:00
|
|
|
return fmt.Errorf("tailssh: rejecting non-Tailscale local address %v", ci.dst)
|
2022-03-09 05:35:55 +00:00
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
if !tsaddr.IsTailscaleIP(ci.src.Addr()) {
|
2022-04-21 00:36:19 +00:00
|
|
|
return fmt.Errorf("tailssh: rejecting non-Tailscale remote address %v", ci.src)
|
2022-03-09 05:35:55 +00:00
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
node, uprof, ok := c.srv.lb.WhoIs(ci.src)
|
2022-02-15 19:59:21 +00:00
|
|
|
if !ok {
|
2022-04-21 00:36:19 +00:00
|
|
|
return fmt.Errorf("unknown Tailscale identity from src %v", ci.src)
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
ci.node = node
|
2022-10-09 00:54:53 +00:00
|
|
|
ci.uprof = uprof
|
2022-04-21 00:36:19 +00:00
|
|
|
|
2022-10-06 17:34:58 +00:00
|
|
|
c.idH = ctx.SessionID()
|
2022-04-21 00:36:19 +00:00
|
|
|
c.info = ci
|
2022-06-27 18:50:11 +00:00
|
|
|
c.logf("handling conn: %v", ci.String())
|
2022-04-21 00:36:19 +00:00
|
|
|
return nil
|
2022-04-20 20:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 21:52:05 +00:00
|
|
|
// evaluatePolicy returns the SSHAction and localUser after evaluating
|
|
|
|
// the SSHPolicy for this conn. The pubKey may be nil for "none" auth.
|
2022-04-21 00:36:19 +00:00
|
|
|
func (c *conn) evaluatePolicy(pubKey gossh.PublicKey) (_ *tailcfg.SSHAction, localUser string, _ error) {
|
|
|
|
pol, ok := c.sshPolicy()
|
2022-04-20 20:39:15 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, "", fmt.Errorf("tailssh: rejecting connection; no SSH policy")
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
a, localUser, ok := c.evalSSHPolicy(pol, pubKey)
|
2022-03-09 05:35:55 +00:00
|
|
|
if !ok {
|
2022-04-20 20:39:15 +00:00
|
|
|
return nil, "", fmt.Errorf("tailssh: rejecting connection; no matching policy")
|
2022-03-09 05:35:55 +00:00
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
return a, localUser, nil
|
2022-03-09 05:35:55 +00:00
|
|
|
}
|
|
|
|
|
2022-04-17 04:49:22 +00:00
|
|
|
// pubKeyCacheEntry is the cache value for an HTTPS URL of public keys (like
|
|
|
|
// "https://github.com/foo.keys")
|
|
|
|
type pubKeyCacheEntry struct {
|
|
|
|
lines []string
|
|
|
|
etag string // if sent by server
|
|
|
|
at time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
pubKeyCacheDuration = time.Minute // how long to cache non-empty public keys
|
|
|
|
pubKeyCacheEmptyDuration = 15 * time.Second // how long to cache empty responses
|
|
|
|
)
|
|
|
|
|
|
|
|
func (srv *server) fetchPublicKeysURLCached(url string) (ce pubKeyCacheEntry, ok bool) {
|
|
|
|
srv.mu.Lock()
|
|
|
|
defer srv.mu.Unlock()
|
|
|
|
// Mostly don't care about the size of this cache. Clean rarely.
|
|
|
|
if m := srv.fetchPublicKeysCache; len(m) > 50 {
|
|
|
|
tooOld := srv.now().Add(pubKeyCacheDuration * 10)
|
|
|
|
for k, ce := range m {
|
|
|
|
if ce.at.Before(tooOld) {
|
|
|
|
delete(m, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ce, ok = srv.fetchPublicKeysCache[url]
|
|
|
|
if !ok {
|
|
|
|
return ce, false
|
|
|
|
}
|
|
|
|
maxAge := pubKeyCacheDuration
|
|
|
|
if len(ce.lines) == 0 {
|
|
|
|
maxAge = pubKeyCacheEmptyDuration
|
|
|
|
}
|
|
|
|
return ce, srv.now().Sub(ce.at) < maxAge
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv *server) pubKeyClient() *http.Client {
|
|
|
|
if srv.pubKeyHTTPClient != nil {
|
|
|
|
return srv.pubKeyHTTPClient
|
|
|
|
}
|
|
|
|
return http.DefaultClient
|
|
|
|
}
|
|
|
|
|
2022-04-21 00:36:19 +00:00
|
|
|
// fetchPublicKeysURL fetches the public keys from a URL. The strings are in the
|
|
|
|
// the typical public key "type base64-string [comment]" format seen at e.g.
|
|
|
|
// https://github.com/USER.keys
|
2022-04-01 19:57:12 +00:00
|
|
|
func (srv *server) fetchPublicKeysURL(url string) ([]string, error) {
|
|
|
|
if !strings.HasPrefix(url, "https://") {
|
|
|
|
return nil, errors.New("invalid URL scheme")
|
|
|
|
}
|
2022-04-17 04:49:22 +00:00
|
|
|
|
|
|
|
ce, ok := srv.fetchPublicKeysURLCached(url)
|
|
|
|
if ok {
|
|
|
|
return ce.lines, nil
|
|
|
|
}
|
2022-04-01 19:57:12 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-17 04:49:22 +00:00
|
|
|
if ce.etag != "" {
|
|
|
|
req.Header.Add("If-None-Match", ce.etag)
|
|
|
|
}
|
|
|
|
res, err := srv.pubKeyClient().Do(req)
|
2022-04-01 19:57:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
2022-04-17 04:49:22 +00:00
|
|
|
var lines []string
|
|
|
|
var etag string
|
|
|
|
switch res.StatusCode {
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unexpected status %v", res.Status)
|
|
|
|
srv.logf("fetching public keys from %s: %v", url, err)
|
|
|
|
case http.StatusNotModified:
|
|
|
|
lines = ce.lines
|
|
|
|
etag = ce.etag
|
|
|
|
case http.StatusOK:
|
|
|
|
var all []byte
|
|
|
|
all, err = io.ReadAll(io.LimitReader(res.Body, 4<<10))
|
|
|
|
if s := strings.TrimSpace(string(all)); s != "" {
|
|
|
|
lines = strings.Split(s, "\n")
|
|
|
|
}
|
|
|
|
etag = res.Header.Get("Etag")
|
2022-04-01 19:57:12 +00:00
|
|
|
}
|
2022-04-17 04:49:22 +00:00
|
|
|
|
|
|
|
srv.mu.Lock()
|
|
|
|
defer srv.mu.Unlock()
|
2022-04-22 03:25:24 +00:00
|
|
|
mak.Set(&srv.fetchPublicKeysCache, url, pubKeyCacheEntry{
|
2022-04-17 04:49:22 +00:00
|
|
|
at: srv.now(),
|
|
|
|
lines: lines,
|
|
|
|
etag: etag,
|
|
|
|
})
|
|
|
|
return lines, err
|
2022-04-01 19:57:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-27 18:50:11 +00:00
|
|
|
// handleSessionPostSSHAuth runs an SSH session after the SSH-level authentication,
|
|
|
|
// but not necessarily before all the Tailscale-level extra verification has
|
|
|
|
// completed. It also handles SFTP requests.
|
|
|
|
func (c *conn) handleSessionPostSSHAuth(s ssh.Session) {
|
2022-04-21 17:11:16 +00:00
|
|
|
// Do this check after auth, but before starting the session.
|
|
|
|
switch s.Subsystem() {
|
|
|
|
case "sftp", "":
|
2022-06-02 03:48:22 +00:00
|
|
|
metricSFTP.Add(1)
|
2022-04-21 17:11:16 +00:00
|
|
|
default:
|
2022-06-02 03:48:22 +00:00
|
|
|
fmt.Fprintf(s.Stderr(), "Unsupported subsystem %q\r\n", s.Subsystem())
|
2022-04-21 17:11:16 +00:00
|
|
|
s.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-27 18:50:11 +00:00
|
|
|
ss := c.newSSHSession(s)
|
2022-07-25 03:08:42 +00:00
|
|
|
ss.logf("handling new SSH connection from %v (%v) to ssh-user %q", c.info.uprof.LoginName, c.info.src.Addr(), c.localUser.Username)
|
2022-06-27 20:17:56 +00:00
|
|
|
ss.logf("access granted to %v as ssh-user %q", c.info.uprof.LoginName, c.localUser.Username)
|
2022-03-22 22:37:17 +00:00
|
|
|
ss.run()
|
|
|
|
}
|
|
|
|
|
2022-10-06 17:34:58 +00:00
|
|
|
// resolveNextAction starts at c.currentAction and makes it way through the
|
|
|
|
// action chain one step at a time. An action without a HoldAndDelegate is
|
|
|
|
// considered the final action. Once a final action is reached, this function
|
|
|
|
// will keep returning that action. It updates c.currentAction to the next
|
|
|
|
// action in the chain. When the final action is reached, it also sets
|
|
|
|
// c.finalAction to the final action.
|
|
|
|
func (c *conn) resolveNextAction(sctx ssh.Context) (action *tailcfg.SSHAction, err error) {
|
2022-06-27 18:50:11 +00:00
|
|
|
if c.finalAction != nil || c.finalActionErr != nil {
|
|
|
|
return c.finalAction, c.finalActionErr
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2022-10-06 17:34:58 +00:00
|
|
|
if action != nil {
|
|
|
|
c.currentAction = action
|
|
|
|
if action.Accept || action.Reject {
|
|
|
|
c.finalAction = action
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.finalActionErr = err
|
2022-06-27 18:50:11 +00:00
|
|
|
}
|
|
|
|
}()
|
2022-04-27 20:23:13 +00:00
|
|
|
|
2022-10-06 17:34:58 +00:00
|
|
|
ctx, cancel := context.WithCancel(sctx)
|
2022-04-27 20:23:13 +00:00
|
|
|
defer cancel()
|
|
|
|
|
2022-03-10 18:28:42 +00:00
|
|
|
// Loop processing/fetching Actions until one reaches a
|
|
|
|
// terminal state (Accept, Reject, or invalid Action), or
|
|
|
|
// until fetchSSHAction times out due to the context being
|
|
|
|
// done (client disconnect) or its 30 minute timeout passes.
|
|
|
|
// (Which is a long time for somebody to see login
|
|
|
|
// instructions and go to a URL to do something.)
|
2022-10-06 17:34:58 +00:00
|
|
|
action = c.currentAction
|
|
|
|
if action.Accept || action.Reject {
|
|
|
|
if action.Reject {
|
|
|
|
metricTerminalReject.Add(1)
|
|
|
|
} else {
|
|
|
|
metricTerminalAccept.Add(1)
|
2022-03-10 18:28:42 +00:00
|
|
|
}
|
2022-10-06 17:34:58 +00:00
|
|
|
return action, nil
|
|
|
|
}
|
|
|
|
url := action.HoldAndDelegate
|
|
|
|
if url == "" {
|
|
|
|
metricTerminalMalformed.Add(1)
|
|
|
|
return nil, errors.New("reached Action that lacked Accept, Reject, and HoldAndDelegate")
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
2022-10-06 17:34:58 +00:00
|
|
|
metricHolds.Add(1)
|
|
|
|
url = c.expandDelegateURLLocked(url)
|
|
|
|
nextAction, err := c.fetchSSHAction(ctx, url)
|
|
|
|
if err != nil {
|
|
|
|
metricTerminalFetchError.Add(1)
|
|
|
|
return nil, fmt.Errorf("fetching SSHAction from %s: %w", url, err)
|
|
|
|
}
|
|
|
|
return nextAction, nil
|
2022-03-22 22:37:17 +00:00
|
|
|
}
|
2022-03-10 18:28:42 +00:00
|
|
|
|
2022-08-02 19:21:32 +00:00
|
|
|
func (c *conn) expandDelegateURLLocked(actionURL string) string {
|
2022-04-21 00:36:19 +00:00
|
|
|
nm := c.srv.lb.NetMap()
|
|
|
|
ci := c.info
|
2022-07-21 15:46:55 +00:00
|
|
|
lu := c.localUser
|
2022-03-22 22:37:17 +00:00
|
|
|
var dstNodeID string
|
|
|
|
if nm != nil {
|
|
|
|
dstNodeID = fmt.Sprint(int64(nm.SelfNode.ID))
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
2022-03-22 22:37:17 +00:00
|
|
|
return strings.NewReplacer(
|
2022-07-25 03:08:42 +00:00
|
|
|
"$SRC_NODE_IP", url.QueryEscape(ci.src.Addr().String()),
|
2022-04-20 20:39:15 +00:00
|
|
|
"$SRC_NODE_ID", fmt.Sprint(int64(ci.node.ID)),
|
2022-07-25 03:08:42 +00:00
|
|
|
"$DST_NODE_IP", url.QueryEscape(ci.dst.Addr().String()),
|
2022-03-22 22:37:17 +00:00
|
|
|
"$DST_NODE_ID", dstNodeID,
|
2022-04-20 20:39:15 +00:00
|
|
|
"$SSH_USER", url.QueryEscape(ci.sshUser),
|
2022-07-21 15:46:55 +00:00
|
|
|
"$LOCAL_USER", url.QueryEscape(lu.Username),
|
2022-03-23 00:36:55 +00:00
|
|
|
).Replace(actionURL)
|
2022-03-13 20:01:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 00:36:19 +00:00
|
|
|
func (c *conn) expandPublicKeyURL(pubKeyURL string) string {
|
2022-04-20 20:53:01 +00:00
|
|
|
if !strings.Contains(pubKeyURL, "$") {
|
|
|
|
return pubKeyURL
|
|
|
|
}
|
2022-10-09 00:54:53 +00:00
|
|
|
loginName := c.info.uprof.LoginName
|
|
|
|
localPart, _, _ := strings.Cut(loginName, "@")
|
2022-04-20 20:53:01 +00:00
|
|
|
return strings.NewReplacer(
|
|
|
|
"$LOGINNAME_EMAIL", loginName,
|
|
|
|
"$LOGINNAME_LOCALPART", localPart,
|
|
|
|
).Replace(pubKeyURL)
|
|
|
|
}
|
|
|
|
|
2022-03-13 20:01:59 +00:00
|
|
|
// sshSession is an accepted Tailscale SSH session.
|
|
|
|
type sshSession struct {
|
|
|
|
ssh.Session
|
|
|
|
sharedID string // ID that's shared with control
|
|
|
|
logf logger.Logf
|
|
|
|
|
2022-03-14 20:26:06 +00:00
|
|
|
ctx *sshContext // implements context.Context
|
2022-04-21 00:36:19 +00:00
|
|
|
conn *conn
|
2022-03-14 20:26:06 +00:00
|
|
|
agentListener net.Listener // non-nil if agent-forwarding requested+allowed
|
2022-03-13 20:01:59 +00:00
|
|
|
|
|
|
|
// initialized by launchProcess:
|
|
|
|
cmd *exec.Cmd
|
|
|
|
stdin io.WriteCloser
|
2022-07-15 15:40:20 +00:00
|
|
|
stdout io.ReadCloser
|
2022-03-13 20:01:59 +00:00
|
|
|
stderr io.Reader // nil for pty sessions
|
|
|
|
ptyReq *ssh.Pty // non-nil for pty sessions
|
|
|
|
|
|
|
|
// We use this sync.Once to ensure that we only terminate the process once,
|
|
|
|
// either it exits itself or is terminated
|
|
|
|
exitOnce sync.Once
|
|
|
|
}
|
|
|
|
|
2022-04-01 19:57:12 +00:00
|
|
|
func (ss *sshSession) vlogf(format string, args ...interface{}) {
|
2022-09-14 19:49:39 +00:00
|
|
|
if sshVerboseLogging() {
|
2022-04-01 19:57:12 +00:00
|
|
|
ss.logf(format, args...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 18:50:11 +00:00
|
|
|
func (c *conn) newSSHSession(s ssh.Session) *sshSession {
|
2022-06-27 19:13:50 +00:00
|
|
|
sharedID := fmt.Sprintf("sess-%s-%02x", c.srv.now().UTC().Format("20060102T150405"), randBytes(5))
|
2022-04-21 00:36:19 +00:00
|
|
|
c.logf("starting session: %v", sharedID)
|
2022-03-13 20:01:59 +00:00
|
|
|
return &sshSession{
|
2022-04-21 00:36:19 +00:00
|
|
|
Session: s,
|
|
|
|
sharedID: sharedID,
|
2022-10-09 21:17:38 +00:00
|
|
|
ctx: newSSHContext(s.Context()),
|
2022-04-21 00:36:19 +00:00
|
|
|
conn: c,
|
|
|
|
logf: logger.WithPrefix(c.srv.logf, "ssh-session("+sharedID+"): "),
|
2022-02-23 23:47:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 18:50:11 +00:00
|
|
|
// isStillValid reports whether the conn is still valid.
|
|
|
|
func (c *conn) isStillValid() bool {
|
|
|
|
a, localUser, err := c.evaluatePolicy(c.pubKey)
|
2022-04-21 00:36:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !a.Accept && a.HoldAndDelegate == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return c.localUser.Username == localUser
|
|
|
|
}
|
|
|
|
|
2022-06-27 18:50:11 +00:00
|
|
|
// checkStillValid checks that the conn is still valid per the latest SSHPolicy.
|
|
|
|
// If not, it terminates all sessions associated with the conn.
|
|
|
|
func (c *conn) checkStillValid() {
|
|
|
|
if c.isStillValid() {
|
2022-04-17 18:49:56 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-02 03:48:22 +00:00
|
|
|
metricPolicyChangeKick.Add(1)
|
2022-06-27 18:50:11 +00:00
|
|
|
c.logf("session no longer valid per new SSH policy; closing")
|
2022-10-06 17:34:58 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2022-06-27 18:50:11 +00:00
|
|
|
for _, s := range c.sessions {
|
|
|
|
s.ctx.CloseWithError(userVisibleError{
|
|
|
|
fmt.Sprintf("Access revoked.\r\n"),
|
|
|
|
context.Canceled,
|
|
|
|
})
|
|
|
|
}
|
2022-04-17 18:49:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 00:36:19 +00:00
|
|
|
func (c *conn) fetchSSHAction(ctx context.Context, url string) (*tailcfg.SSHAction, error) {
|
2022-03-10 18:28:42 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
|
|
|
|
defer cancel()
|
2022-04-21 00:36:19 +00:00
|
|
|
bo := backoff.NewBackoff("fetch-ssh-action", c.logf, 10*time.Second)
|
2022-03-10 18:28:42 +00:00
|
|
|
for {
|
|
|
|
if err := ctx.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
res, err := c.srv.lb.DoNoiseRequest(req)
|
2022-03-10 18:28:42 +00:00
|
|
|
if err != nil {
|
|
|
|
bo.BackOff(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if res.StatusCode != 200 {
|
2022-03-22 22:37:17 +00:00
|
|
|
body, _ := io.ReadAll(res.Body)
|
2022-03-10 18:28:42 +00:00
|
|
|
res.Body.Close()
|
2022-03-22 22:37:17 +00:00
|
|
|
if len(body) > 1<<10 {
|
|
|
|
body = body[:1<<10]
|
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
c.logf("fetch of %v: %s, %s", url, res.Status, body)
|
2022-03-10 18:28:42 +00:00
|
|
|
bo.BackOff(ctx, fmt.Errorf("unexpected status: %v", res.Status))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
a := new(tailcfg.SSHAction)
|
2022-03-22 22:37:17 +00:00
|
|
|
err = json.NewDecoder(res.Body).Decode(a)
|
|
|
|
res.Body.Close()
|
|
|
|
if err != nil {
|
2022-04-21 00:36:19 +00:00
|
|
|
c.logf("invalid next SSHAction JSON from %v: %v", url, err)
|
2022-03-10 18:28:42 +00:00
|
|
|
bo.BackOff(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-13 20:01:59 +00:00
|
|
|
// killProcessOnContextDone waits for ss.ctx to be done and kills the process,
|
|
|
|
// unless the process has already exited.
|
|
|
|
func (ss *sshSession) killProcessOnContextDone() {
|
|
|
|
<-ss.ctx.Done()
|
2022-04-21 21:52:05 +00:00
|
|
|
// Either the process has already exited, in which case this does nothing.
|
2022-03-09 05:35:55 +00:00
|
|
|
// Or, the process is still running in which case this will kill it.
|
2022-03-13 20:01:59 +00:00
|
|
|
ss.exitOnce.Do(func() {
|
|
|
|
err := ss.ctx.Err()
|
2022-03-09 05:35:55 +00:00
|
|
|
if serr, ok := err.(SSHTerminationError); ok {
|
|
|
|
msg := serr.SSHTerminationMessage()
|
|
|
|
if msg != "" {
|
2022-03-13 20:01:59 +00:00
|
|
|
io.WriteString(ss.Stderr(), "\r\n\r\n"+msg+"\r\n\r\n")
|
2022-03-09 05:35:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-25 03:08:42 +00:00
|
|
|
ss.logf("terminating SSH session from %v: %v", ss.conn.info.src.Addr(), err)
|
2022-04-21 21:52:05 +00:00
|
|
|
// We don't need to Process.Wait here, sshSession.run() does
|
|
|
|
// the waiting regardless of termination reason.
|
2022-07-15 15:40:20 +00:00
|
|
|
|
|
|
|
// TODO(maisem): should this be a SIGTERM followed by a SIGKILL?
|
2022-03-13 20:01:59 +00:00
|
|
|
ss.cmd.Process.Kill()
|
2022-03-09 05:35:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-06 17:34:58 +00:00
|
|
|
// attachSession registers ss as an active session.
|
|
|
|
func (c *conn) attachSession(ss *sshSession) {
|
2022-06-27 18:50:11 +00:00
|
|
|
c.srv.sessionWaitGroup.Add(1)
|
2022-03-13 20:01:59 +00:00
|
|
|
if ss.sharedID == "" {
|
|
|
|
panic("empty sharedID")
|
|
|
|
}
|
2022-10-06 17:34:58 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2022-06-27 18:50:11 +00:00
|
|
|
c.sessions = append(c.sessions, ss)
|
2022-03-09 19:25:07 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 17:34:58 +00:00
|
|
|
// detachSession unregisters s from the list of active sessions.
|
|
|
|
func (c *conn) detachSession(ss *sshSession) {
|
2022-06-27 18:50:11 +00:00
|
|
|
defer c.srv.sessionWaitGroup.Done()
|
2022-10-06 17:34:58 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2022-06-27 18:50:11 +00:00
|
|
|
for i, s := range c.sessions {
|
|
|
|
if s == ss {
|
|
|
|
c.sessions = append(c.sessions[:i], c.sessions[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-03-09 19:25:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-13 20:01:59 +00:00
|
|
|
var errSessionDone = errors.New("session is done")
|
|
|
|
|
2022-03-14 20:26:06 +00:00
|
|
|
// handleSSHAgentForwarding starts a Unix socket listener and in the background
|
2022-04-21 21:52:05 +00:00
|
|
|
// forwards agent connections between the listener and the ssh.Session.
|
2022-03-14 20:26:06 +00:00
|
|
|
// On success, it assigns ss.agentListener.
|
|
|
|
func (ss *sshSession) handleSSHAgentForwarding(s ssh.Session, lu *user.User) error {
|
2022-06-27 18:50:11 +00:00
|
|
|
if !ssh.AgentRequested(ss) || !ss.conn.finalAction.AllowAgentForwarding {
|
2022-03-14 20:26:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ss.logf("ssh: agent forwarding requested")
|
|
|
|
ln, err := ssh.NewAgentListener()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil && ln != nil {
|
|
|
|
ln.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
uid, err := strconv.ParseUint(lu.Uid, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gid, err := strconv.ParseUint(lu.Gid, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
socket := ln.Addr().String()
|
|
|
|
dir := filepath.Dir(socket)
|
2022-04-21 21:45:36 +00:00
|
|
|
// Make sure the socket is accessible only by the user.
|
|
|
|
if err := os.Chmod(socket, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-14 20:26:06 +00:00
|
|
|
if err := os.Chown(socket, int(uid), int(gid)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-21 21:45:36 +00:00
|
|
|
// Make sure the dir is also accessible.
|
2022-03-14 20:26:06 +00:00
|
|
|
if err := os.Chmod(dir, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go ssh.ForwardAgentConnections(ln, s)
|
|
|
|
ss.agentListener = ln
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-13 05:32:17 +00:00
|
|
|
// recordSSH is a temporary dev knob to test the SSH recording
|
|
|
|
// functionality and support off-node streaming.
|
|
|
|
//
|
|
|
|
// TODO(bradfitz,maisem): move this to SSHPolicy.
|
2022-09-14 19:49:39 +00:00
|
|
|
var recordSSH = envknob.RegisterBool("TS_DEBUG_LOG_SSH")
|
2022-03-13 05:32:17 +00:00
|
|
|
|
2022-03-13 20:01:59 +00:00
|
|
|
// run is the entrypoint for a newly accepted SSH session.
|
2022-02-23 23:47:57 +00:00
|
|
|
//
|
2022-03-13 05:32:17 +00:00
|
|
|
// It handles ss once it's been accepted and determined
|
|
|
|
// that it should run.
|
2022-03-13 20:01:59 +00:00
|
|
|
func (ss *sshSession) run() {
|
2022-06-02 03:48:22 +00:00
|
|
|
metricActiveSessions.Add(1)
|
|
|
|
defer metricActiveSessions.Add(-1)
|
2022-05-28 11:33:46 +00:00
|
|
|
defer ss.ctx.CloseWithError(errSessionDone)
|
2022-03-13 20:01:59 +00:00
|
|
|
|
2022-10-06 17:34:58 +00:00
|
|
|
if attached := ss.conn.srv.attachSessionToConnIfNotShutdown(ss); !attached {
|
2022-05-30 09:06:46 +00:00
|
|
|
fmt.Fprintf(ss, "Tailscale SSH is shutting down\r\n")
|
2022-05-28 11:33:46 +00:00
|
|
|
ss.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
2022-10-06 17:34:58 +00:00
|
|
|
defer ss.conn.detachSession(ss)
|
2022-05-28 11:33:46 +00:00
|
|
|
|
2022-10-06 17:34:58 +00:00
|
|
|
lu := ss.conn.localUser
|
|
|
|
logf := ss.logf
|
2022-03-13 20:01:59 +00:00
|
|
|
|
2022-06-27 18:50:11 +00:00
|
|
|
if ss.conn.finalAction.SessionDuration != 0 {
|
|
|
|
t := time.AfterFunc(ss.conn.finalAction.SessionDuration, func() {
|
2022-03-13 20:01:59 +00:00
|
|
|
ss.ctx.CloseWithError(userVisibleError{
|
2022-06-27 18:50:11 +00:00
|
|
|
fmt.Sprintf("Session timeout of %v elapsed.", ss.conn.finalAction.SessionDuration),
|
2022-03-13 20:01:59 +00:00
|
|
|
context.DeadlineExceeded,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
defer t.Stop()
|
|
|
|
}
|
|
|
|
|
2022-02-19 23:37:13 +00:00
|
|
|
if euid := os.Geteuid(); euid != 0 {
|
|
|
|
if lu.Uid != fmt.Sprint(euid) {
|
2022-10-06 17:34:58 +00:00
|
|
|
ss.logf("can't switch to user %q from process euid %v", lu.Username, euid)
|
2022-04-20 20:39:15 +00:00
|
|
|
fmt.Fprintf(ss, "can't switch user\r\n")
|
2022-03-13 20:01:59 +00:00
|
|
|
ss.Exit(1)
|
2022-02-19 03:07:04 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-19 23:37:13 +00:00
|
|
|
}
|
2022-03-09 05:35:55 +00:00
|
|
|
|
2022-03-13 01:40:40 +00:00
|
|
|
// Take control of the PTY so that we can configure it below.
|
|
|
|
// See https://github.com/tailscale/tailscale/issues/4146
|
2022-03-13 20:01:59 +00:00
|
|
|
ss.DisablePTYEmulation()
|
2022-03-13 01:40:40 +00:00
|
|
|
|
2022-03-13 05:32:17 +00:00
|
|
|
var rec *recording // or nil if disabled
|
2022-04-21 17:11:16 +00:00
|
|
|
if ss.Subsystem() != "sftp" {
|
|
|
|
if err := ss.handleSSHAgentForwarding(ss, lu); err != nil {
|
|
|
|
ss.logf("agent forwarding failed: %v", err)
|
|
|
|
} else if ss.agentListener != nil {
|
|
|
|
// TODO(maisem/bradfitz): add a way to close all session resources
|
|
|
|
defer ss.agentListener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if ss.shouldRecord() {
|
|
|
|
var err error
|
|
|
|
rec, err = ss.startNewRecording()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(ss, "can't start new recording\r\n")
|
|
|
|
ss.logf("startNewRecording: %v", err)
|
|
|
|
ss.Exit(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rec.Close()
|
2022-03-13 05:32:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-21 17:11:16 +00:00
|
|
|
err := ss.launchProcess()
|
2022-03-09 05:35:55 +00:00
|
|
|
if err != nil {
|
|
|
|
logf("start failed: %v", err.Error())
|
2022-03-13 20:01:59 +00:00
|
|
|
ss.Exit(1)
|
2022-03-09 05:35:55 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-13 20:01:59 +00:00
|
|
|
go ss.killProcessOnContextDone()
|
|
|
|
|
2022-03-09 05:35:55 +00:00
|
|
|
go func() {
|
2022-07-15 15:40:20 +00:00
|
|
|
defer ss.stdin.Close()
|
|
|
|
if _, err := io.Copy(rec.writer("i", ss.stdin), ss); err != nil {
|
2022-05-28 11:36:30 +00:00
|
|
|
logf("stdin copy: %v", err)
|
2022-07-15 15:40:20 +00:00
|
|
|
ss.ctx.CloseWithError(err)
|
2022-02-19 03:07:04 +00:00
|
|
|
}
|
2022-03-09 05:35:55 +00:00
|
|
|
}()
|
2022-11-15 19:46:15 +00:00
|
|
|
var openOutputStreams atomic.Int32
|
|
|
|
if ss.stderr != nil {
|
|
|
|
openOutputStreams.Store(2)
|
|
|
|
} else {
|
|
|
|
openOutputStreams.Store(1)
|
|
|
|
}
|
2022-03-09 05:35:55 +00:00
|
|
|
go func() {
|
2022-07-15 15:40:20 +00:00
|
|
|
defer ss.stdout.Close()
|
2022-03-13 05:32:17 +00:00
|
|
|
_, err := io.Copy(rec.writer("o", ss), ss.stdout)
|
2022-07-15 15:40:20 +00:00
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
2022-05-28 11:36:30 +00:00
|
|
|
logf("stdout copy: %v", err)
|
|
|
|
ss.ctx.CloseWithError(err)
|
2022-11-15 19:46:15 +00:00
|
|
|
}
|
|
|
|
if openOutputStreams.Add(-1) == 0 {
|
2022-07-15 15:40:20 +00:00
|
|
|
ss.CloseWrite()
|
2022-02-19 23:37:13 +00:00
|
|
|
}
|
2022-03-09 05:35:55 +00:00
|
|
|
}()
|
|
|
|
// stderr is nil for ptys.
|
2022-03-13 20:01:59 +00:00
|
|
|
if ss.stderr != nil {
|
2022-02-23 23:47:57 +00:00
|
|
|
go func() {
|
2022-03-13 20:01:59 +00:00
|
|
|
_, err := io.Copy(ss.Stderr(), ss.stderr)
|
2022-03-09 05:35:55 +00:00
|
|
|
if err != nil {
|
2022-05-28 11:36:30 +00:00
|
|
|
logf("stderr copy: %v", err)
|
2022-02-23 23:47:57 +00:00
|
|
|
}
|
2022-11-15 19:46:15 +00:00
|
|
|
if openOutputStreams.Add(-1) == 0 {
|
|
|
|
ss.CloseWrite()
|
|
|
|
}
|
2022-02-23 23:47:57 +00:00
|
|
|
}()
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
2022-07-15 15:40:20 +00:00
|
|
|
|
2022-03-13 20:01:59 +00:00
|
|
|
err = ss.cmd.Wait()
|
2022-03-09 05:35:55 +00:00
|
|
|
// This will either make the SSH Termination goroutine be a no-op,
|
|
|
|
// or itself will be a no-op because the process was killed by the
|
|
|
|
// aforementioned goroutine.
|
2022-03-13 20:01:59 +00:00
|
|
|
ss.exitOnce.Do(func() {})
|
2022-03-09 05:35:55 +00:00
|
|
|
|
2022-02-19 23:37:13 +00:00
|
|
|
if err == nil {
|
2022-07-15 15:40:20 +00:00
|
|
|
ss.logf("Session complete")
|
2022-03-13 20:01:59 +00:00
|
|
|
ss.Exit(0)
|
2022-02-19 23:37:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if ee, ok := err.(*exec.ExitError); ok {
|
|
|
|
code := ee.ProcessState.ExitCode()
|
2022-04-01 19:57:12 +00:00
|
|
|
ss.logf("Wait: code=%v", code)
|
2022-03-13 20:01:59 +00:00
|
|
|
ss.Exit(code)
|
2022-02-19 23:37:13 +00:00
|
|
|
return
|
2022-02-15 19:59:21 +00:00
|
|
|
}
|
2022-02-19 23:37:13 +00:00
|
|
|
|
2022-04-01 19:57:12 +00:00
|
|
|
ss.logf("Wait: %v", err)
|
2022-03-13 20:01:59 +00:00
|
|
|
ss.Exit(1)
|
2022-02-15 19:59:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-13 05:32:17 +00:00
|
|
|
func (ss *sshSession) shouldRecord() bool {
|
|
|
|
// for now only record pty sessions
|
|
|
|
// TODO(bradfitz,maisem): make configurable on SSHPolicy and
|
|
|
|
// support recording non-pty stuff too.
|
|
|
|
_, _, isPtyReq := ss.Pty()
|
2022-09-14 19:49:39 +00:00
|
|
|
return recordSSH() && isPtyReq
|
2022-03-13 05:32:17 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 16:58:53 +00:00
|
|
|
type sshConnInfo struct {
|
2022-02-18 22:10:26 +00:00
|
|
|
// sshUser is the requested local SSH username ("root", "alice", etc).
|
|
|
|
sshUser string
|
|
|
|
|
2022-03-09 05:35:55 +00:00
|
|
|
// src is the Tailscale IP and port that the connection came from.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
src netip.AddrPort
|
2022-03-09 05:35:55 +00:00
|
|
|
|
|
|
|
// dst is the Tailscale IP and port that the connection came for.
|
all: convert more code to use net/netip directly
perl -i -npe 's,netaddr.IPPrefixFrom,netip.PrefixFrom,' $(git grep -l -F netaddr.)
perl -i -npe 's,netaddr.IPPortFrom,netip.AddrPortFrom,' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPrefix,netip.Prefix,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPPort,netip.AddrPort,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IP\b,netip.Addr,g' $(git grep -l -F netaddr. )
perl -i -npe 's,netaddr.IPv6Raw\b,netip.AddrFrom16,g' $(git grep -l -F netaddr. )
goimports -w .
Then delete some stuff from the net/netaddr shim package which is no
longer neeed.
Updates #5162
Change-Id: Ia7a86893fe21c7e3ee1ec823e8aba288d4566cd8
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-07-26 04:14:09 +00:00
|
|
|
dst netip.AddrPort
|
2022-02-18 22:10:26 +00:00
|
|
|
|
|
|
|
// node is srcIP's node.
|
|
|
|
node *tailcfg.Node
|
|
|
|
|
|
|
|
// uprof is node's UserProfile.
|
2022-10-09 00:54:53 +00:00
|
|
|
uprof tailcfg.UserProfile
|
2022-04-20 20:39:15 +00:00
|
|
|
}
|
2022-04-01 19:57:12 +00:00
|
|
|
|
2022-04-20 20:39:15 +00:00
|
|
|
func (ci *sshConnInfo) String() string {
|
|
|
|
return fmt.Sprintf("%v->%v@%v", ci.src, ci.sshUser, ci.dst)
|
2022-04-01 19:57:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 00:36:19 +00:00
|
|
|
func (c *conn) ruleExpired(r *tailcfg.SSHRule) bool {
|
2022-04-01 19:57:12 +00:00
|
|
|
if r.RuleExpires == nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-06-27 19:13:50 +00:00
|
|
|
return r.RuleExpires.Before(c.srv.now())
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 00:36:19 +00:00
|
|
|
func (c *conn) evalSSHPolicy(pol *tailcfg.SSHPolicy, pubKey gossh.PublicKey) (a *tailcfg.SSHAction, localUser string, ok bool) {
|
2022-02-18 22:10:26 +00:00
|
|
|
for _, r := range pol.Rules {
|
2022-04-21 00:36:19 +00:00
|
|
|
if a, localUser, err := c.matchRule(r, pubKey); err == nil {
|
2022-02-18 22:10:26 +00:00
|
|
|
return a, localUser, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal errors for testing; they don't escape to callers or logs.
|
|
|
|
var (
|
|
|
|
errNilRule = errors.New("nil rule")
|
|
|
|
errNilAction = errors.New("nil action")
|
|
|
|
errRuleExpired = errors.New("rule expired")
|
|
|
|
errPrincipalMatch = errors.New("principal didn't match")
|
|
|
|
errUserMatch = errors.New("user didn't match")
|
2022-07-21 15:46:55 +00:00
|
|
|
errInvalidConn = errors.New("invalid connection state")
|
2022-02-18 22:10:26 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 00:36:19 +00:00
|
|
|
func (c *conn) matchRule(r *tailcfg.SSHRule, pubKey gossh.PublicKey) (a *tailcfg.SSHAction, localUser string, err error) {
|
2022-07-21 15:46:55 +00:00
|
|
|
if c == nil {
|
|
|
|
return nil, "", errInvalidConn
|
|
|
|
}
|
2022-10-06 17:34:58 +00:00
|
|
|
if c.info == nil {
|
2022-07-21 15:46:55 +00:00
|
|
|
c.logf("invalid connection state")
|
|
|
|
return nil, "", errInvalidConn
|
|
|
|
}
|
2022-02-18 22:10:26 +00:00
|
|
|
if r == nil {
|
|
|
|
return nil, "", errNilRule
|
|
|
|
}
|
|
|
|
if r.Action == nil {
|
|
|
|
return nil, "", errNilAction
|
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
if c.ruleExpired(r) {
|
2022-02-18 22:10:26 +00:00
|
|
|
return nil, "", errRuleExpired
|
|
|
|
}
|
2022-04-21 21:58:41 +00:00
|
|
|
if !r.Action.Reject {
|
|
|
|
// For all but Reject rules, SSHUsers is required.
|
|
|
|
// If SSHUsers is nil or empty, mapLocalUser will return an
|
|
|
|
// empty string anyway.
|
2022-10-06 17:34:58 +00:00
|
|
|
localUser = mapLocalUser(r.SSHUsers, c.info.sshUser)
|
2022-02-18 22:10:26 +00:00
|
|
|
if localUser == "" {
|
|
|
|
return nil, "", errUserMatch
|
|
|
|
}
|
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
if ok, err := c.anyPrincipalMatches(r.Principals, pubKey); err != nil {
|
2022-04-20 20:39:15 +00:00
|
|
|
return nil, "", err
|
|
|
|
} else if !ok {
|
2022-04-01 19:57:12 +00:00
|
|
|
return nil, "", errPrincipalMatch
|
|
|
|
}
|
2022-02-18 22:10:26 +00:00
|
|
|
return r.Action, localUser, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mapLocalUser(ruleSSHUsers map[string]string, reqSSHUser string) (localUser string) {
|
2022-03-21 17:39:54 +00:00
|
|
|
v, ok := ruleSSHUsers[reqSSHUser]
|
|
|
|
if !ok {
|
|
|
|
v = ruleSSHUsers["*"]
|
|
|
|
}
|
|
|
|
if v == "=" {
|
|
|
|
return reqSSHUser
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
2022-03-21 17:39:54 +00:00
|
|
|
return v
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 00:36:19 +00:00
|
|
|
func (c *conn) anyPrincipalMatches(ps []*tailcfg.SSHPrincipal, pubKey gossh.PublicKey) (bool, error) {
|
2022-02-18 22:10:26 +00:00
|
|
|
for _, p := range ps {
|
|
|
|
if p == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
if ok, err := c.principalMatches(p, pubKey); err != nil {
|
2022-04-20 20:39:15 +00:00
|
|
|
return false, err
|
|
|
|
} else if ok {
|
|
|
|
return true, nil
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
2022-04-01 19:57:12 +00:00
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
return false, nil
|
2022-04-01 19:57:12 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 00:36:19 +00:00
|
|
|
func (c *conn) principalMatches(p *tailcfg.SSHPrincipal, pubKey gossh.PublicKey) (bool, error) {
|
|
|
|
if !c.principalMatchesTailscaleIdentity(p) {
|
2022-04-20 20:39:15 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
return c.principalMatchesPubKey(p, pubKey)
|
2022-04-01 19:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// principalMatchesTailscaleIdentity reports whether one of p's four fields
|
|
|
|
// that match the Tailscale identity match (Node, NodeIP, UserLogin, Any).
|
|
|
|
// This function does not consider PubKeys.
|
2022-04-21 00:36:19 +00:00
|
|
|
func (c *conn) principalMatchesTailscaleIdentity(p *tailcfg.SSHPrincipal) bool {
|
|
|
|
ci := c.info
|
2022-04-01 19:57:12 +00:00
|
|
|
if p.Any {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !p.Node.IsZero() && ci.node != nil && p.Node == ci.node.StableID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if p.NodeIP != "" {
|
2022-07-26 03:55:44 +00:00
|
|
|
if ip, _ := netip.ParseAddr(p.NodeIP); ip == ci.src.Addr() {
|
2022-02-18 22:10:26 +00:00
|
|
|
return true
|
|
|
|
}
|
2022-04-01 19:57:12 +00:00
|
|
|
}
|
2022-10-09 00:54:53 +00:00
|
|
|
if p.UserLogin != "" && ci.uprof.LoginName == p.UserLogin {
|
2022-04-01 19:57:12 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-21 00:36:19 +00:00
|
|
|
func (c *conn) principalMatchesPubKey(p *tailcfg.SSHPrincipal, clientPubKey gossh.PublicKey) (bool, error) {
|
2022-04-01 19:57:12 +00:00
|
|
|
if len(p.PubKeys) == 0 {
|
2022-04-20 20:39:15 +00:00
|
|
|
return true, nil
|
2022-04-01 19:57:12 +00:00
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
if clientPubKey == nil {
|
|
|
|
return false, nil
|
2022-04-01 19:57:12 +00:00
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
knownKeys := p.PubKeys
|
|
|
|
if len(knownKeys) == 1 && strings.HasPrefix(knownKeys[0], "https://") {
|
2022-04-01 19:57:12 +00:00
|
|
|
var err error
|
2022-04-21 00:36:19 +00:00
|
|
|
knownKeys, err = c.srv.fetchPublicKeysURL(c.expandPublicKeyURL(knownKeys[0]))
|
2022-04-01 19:57:12 +00:00
|
|
|
if err != nil {
|
2022-04-20 20:39:15 +00:00
|
|
|
return false, err
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
2022-04-01 19:57:12 +00:00
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
for _, knownKey := range knownKeys {
|
|
|
|
if pubKeyMatchesAuthorizedKey(clientPubKey, knownKey) {
|
|
|
|
return true, nil
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-20 20:39:15 +00:00
|
|
|
return false, nil
|
2022-02-18 22:10:26 +00:00
|
|
|
}
|
2022-03-13 20:01:59 +00:00
|
|
|
|
2022-04-01 19:57:12 +00:00
|
|
|
func pubKeyMatchesAuthorizedKey(pubKey ssh.PublicKey, wantKey string) bool {
|
|
|
|
wantKeyType, rest, ok := strings.Cut(wantKey, " ")
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if pubKey.Type() != wantKeyType {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
wantKeyB64, _, _ := strings.Cut(rest, " ")
|
|
|
|
wantKeyData, _ := base64.StdEncoding.DecodeString(wantKeyB64)
|
|
|
|
return len(wantKeyData) > 0 && bytes.Equal(pubKey.Marshal(), wantKeyData)
|
|
|
|
}
|
|
|
|
|
2022-03-13 20:01:59 +00:00
|
|
|
func randBytes(n int) []byte {
|
|
|
|
b := make([]byte, n)
|
|
|
|
if _, err := rand.Read(b); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
2022-03-13 05:32:17 +00:00
|
|
|
|
|
|
|
// startNewRecording starts a new SSH session recording.
|
|
|
|
//
|
|
|
|
// It writes an asciinema file to
|
|
|
|
// $TAILSCALE_VAR_ROOT/ssh-sessions/ssh-session-<unixtime>-*.cast.
|
2022-09-26 22:37:27 +00:00
|
|
|
func (ss *sshSession) startNewRecording() (_ *recording, err error) {
|
2022-03-13 05:32:17 +00:00
|
|
|
var w ssh.Window
|
|
|
|
if ptyReq, _, isPtyReq := ss.Pty(); isPtyReq {
|
|
|
|
w = ptyReq.Window
|
|
|
|
}
|
|
|
|
|
|
|
|
term := envValFromList(ss.Environ(), "TERM")
|
|
|
|
if term == "" {
|
|
|
|
term = "xterm-256color" // something non-empty
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
rec := &recording{
|
|
|
|
ss: ss,
|
|
|
|
start: now,
|
|
|
|
}
|
2022-04-21 00:36:19 +00:00
|
|
|
varRoot := ss.conn.srv.lb.TailscaleVarRoot()
|
2022-03-13 05:32:17 +00:00
|
|
|
if varRoot == "" {
|
|
|
|
return nil, errors.New("no var root for recording storage")
|
|
|
|
}
|
|
|
|
dir := filepath.Join(varRoot, "ssh-sessions")
|
|
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-26 22:37:27 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
rec.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-09-15 12:06:59 +00:00
|
|
|
f, err := os.CreateTemp(dir, fmt.Sprintf("ssh-session-%v-*.cast", now.UnixNano()))
|
2022-03-13 05:32:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rec.out = f
|
|
|
|
|
|
|
|
// {"version": 2, "width": 221, "height": 84, "timestamp": 1647146075, "env": {"SHELL": "/bin/bash", "TERM": "screen"}}
|
|
|
|
type CastHeader struct {
|
|
|
|
Version int `json:"version"`
|
|
|
|
Width int `json:"width"`
|
|
|
|
Height int `json:"height"`
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
Env map[string]string `json:"env"`
|
|
|
|
}
|
|
|
|
j, err := json.Marshal(CastHeader{
|
|
|
|
Version: 2,
|
|
|
|
Width: w.Width,
|
|
|
|
Height: w.Height,
|
|
|
|
Timestamp: now.Unix(),
|
|
|
|
Env: map[string]string{
|
|
|
|
"TERM": term,
|
2022-09-25 18:29:55 +00:00
|
|
|
// TODO(bradfitz): anything else important?
|
2022-03-13 05:32:17 +00:00
|
|
|
// including all seems noisey, but maybe we should
|
|
|
|
// for auditing. But first need to break
|
|
|
|
// launchProcess's startWithStdPipes and
|
|
|
|
// startWithPTY up so that they first return the cmd
|
|
|
|
// without starting it, and then a step that starts
|
|
|
|
// it. Then we can (1) make the cmd, (2) start the
|
|
|
|
// recording, (3) start the process.
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
f.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ss.logf("starting asciinema recording to %s", f.Name())
|
|
|
|
j = append(j, '\n')
|
|
|
|
if _, err := f.Write(j); err != nil {
|
|
|
|
f.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// recording is the state for an SSH session recording.
|
|
|
|
type recording struct {
|
|
|
|
ss *sshSession
|
|
|
|
start time.Time
|
|
|
|
|
|
|
|
mu sync.Mutex // guards writes to, close of out
|
|
|
|
out *os.File // nil if closed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *recording) Close() error {
|
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
if r.out == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := r.out.Close()
|
|
|
|
r.out = nil
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// writer returns an io.Writer around w that first records the write.
|
|
|
|
//
|
|
|
|
// The dir should be "i" for input or "o" for output.
|
|
|
|
//
|
|
|
|
// If r is nil, it returns w unchanged.
|
|
|
|
func (r *recording) writer(dir string, w io.Writer) io.Writer {
|
|
|
|
if r == nil {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
return &loggingWriter{r, dir, w}
|
|
|
|
}
|
|
|
|
|
|
|
|
// loggingWriter is an io.Writer wrapper that writes first an
|
|
|
|
// asciinema JSON cast format recording line, and then writes to w.
|
|
|
|
type loggingWriter struct {
|
|
|
|
r *recording
|
|
|
|
dir string // "i" or "o" (input or output)
|
|
|
|
w io.Writer // underlying Writer, after writing to r.out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w loggingWriter) Write(p []byte) (n int, err error) {
|
|
|
|
j, err := json.Marshal([]interface{}{
|
|
|
|
time.Since(w.r.start).Seconds(),
|
|
|
|
w.dir,
|
|
|
|
string(p),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
j = append(j, '\n')
|
|
|
|
if err := w.writeCastLine(j); err != nil {
|
2022-04-21 21:52:05 +00:00
|
|
|
return 0, err
|
2022-03-13 05:32:17 +00:00
|
|
|
}
|
|
|
|
return w.w.Write(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w loggingWriter) writeCastLine(j []byte) error {
|
|
|
|
w.r.mu.Lock()
|
|
|
|
defer w.r.mu.Unlock()
|
|
|
|
if w.r.out == nil {
|
|
|
|
return errors.New("logger closed")
|
|
|
|
}
|
|
|
|
_, err := w.r.out.Write(j)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("logger Write: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func envValFromList(env []string, wantKey string) (v string) {
|
|
|
|
for _, kv := range env {
|
|
|
|
if thisKey, v, ok := strings.Cut(kv, "="); ok && envEq(thisKey, wantKey) {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// envEq reports whether environment variable a == b for the current
|
|
|
|
// operating system.
|
|
|
|
func envEq(a, b string) bool {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return strings.EqualFold(a, b)
|
|
|
|
}
|
|
|
|
return a == b
|
|
|
|
}
|
2022-06-02 03:48:22 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
metricActiveSessions = clientmetric.NewGauge("ssh_active_sessions")
|
|
|
|
metricIncomingConnections = clientmetric.NewCounter("ssh_incoming_connections")
|
|
|
|
metricPublicKeyConnections = clientmetric.NewCounter("ssh_publickey_connections") // total
|
|
|
|
metricPublicKeyAccepts = clientmetric.NewCounter("ssh_publickey_accepts") // accepted subset of ssh_publickey_connections
|
|
|
|
metricTerminalAccept = clientmetric.NewCounter("ssh_terminalaction_accept")
|
|
|
|
metricTerminalReject = clientmetric.NewCounter("ssh_terminalaction_reject")
|
|
|
|
metricTerminalInterrupt = clientmetric.NewCounter("ssh_terminalaction_interrupt")
|
|
|
|
metricTerminalMalformed = clientmetric.NewCounter("ssh_terminalaction_malformed")
|
|
|
|
metricTerminalFetchError = clientmetric.NewCounter("ssh_terminalaction_fetch_error")
|
|
|
|
metricHolds = clientmetric.NewCounter("ssh_holds")
|
|
|
|
metricPolicyChangeKick = clientmetric.NewCounter("ssh_policy_change_kick")
|
|
|
|
metricSFTP = clientmetric.NewCounter("ssh_sftp_requests")
|
|
|
|
metricLocalPortForward = clientmetric.NewCounter("ssh_local_port_forward_requests")
|
|
|
|
)
|