tailssh.go 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build linux || (darwin && !ios) || freebsd || openbsd
  4. // Package tailssh is an SSH server integrated into Tailscale.
  5. package tailssh
  6. import (
  7. "bytes"
  8. "context"
  9. "crypto/rand"
  10. "encoding/base64"
  11. "encoding/json"
  12. "errors"
  13. "fmt"
  14. "io"
  15. "net"
  16. "net/http"
  17. "net/http/httptrace"
  18. "net/netip"
  19. "net/url"
  20. "os"
  21. "os/exec"
  22. "path/filepath"
  23. "runtime"
  24. "strconv"
  25. "strings"
  26. "sync"
  27. "sync/atomic"
  28. "syscall"
  29. "time"
  30. gossh "github.com/tailscale/golang-x-crypto/ssh"
  31. "tailscale.com/envknob"
  32. "tailscale.com/ipn/ipnlocal"
  33. "tailscale.com/logtail/backoff"
  34. "tailscale.com/net/tsaddr"
  35. "tailscale.com/net/tsdial"
  36. "tailscale.com/tailcfg"
  37. "tailscale.com/tempfork/gliderlabs/ssh"
  38. "tailscale.com/types/key"
  39. "tailscale.com/types/logger"
  40. "tailscale.com/types/netmap"
  41. "tailscale.com/util/clientmetric"
  42. "tailscale.com/util/httpm"
  43. "tailscale.com/util/mak"
  44. "tailscale.com/util/multierr"
  45. )
  46. var (
  47. sshVerboseLogging = envknob.RegisterBool("TS_DEBUG_SSH_VLOG")
  48. )
  49. const (
  50. // forcePasswordSuffix is the suffix at the end of a username that forces
  51. // Tailscale SSH into password authentication mode to work around buggy SSH
  52. // clients that get confused by successful replies to auth type "none".
  53. forcePasswordSuffix = "+password"
  54. )
  55. // ipnLocalBackend is the subset of ipnlocal.LocalBackend that we use.
  56. // It is used for testing.
  57. type ipnLocalBackend interface {
  58. GetSSH_HostKeys() ([]gossh.Signer, error)
  59. ShouldRunSSH() bool
  60. NetMap() *netmap.NetworkMap
  61. WhoIs(ipp netip.AddrPort) (n tailcfg.NodeView, u tailcfg.UserProfile, ok bool)
  62. DoNoiseRequest(req *http.Request) (*http.Response, error)
  63. Dialer() *tsdial.Dialer
  64. TailscaleVarRoot() string
  65. NodeKey() key.NodePublic
  66. }
  67. type server struct {
  68. lb ipnLocalBackend
  69. logf logger.Logf
  70. tailscaledPath string
  71. pubKeyHTTPClient *http.Client // or nil for http.DefaultClient
  72. timeNow func() time.Time // or nil for time.Now
  73. sessionWaitGroup sync.WaitGroup
  74. // mu protects the following
  75. mu sync.Mutex
  76. activeConns map[*conn]bool // set; value is always true
  77. fetchPublicKeysCache map[string]pubKeyCacheEntry // by https URL
  78. shutdownCalled bool
  79. }
  80. func (srv *server) now() time.Time {
  81. if srv != nil && srv.timeNow != nil {
  82. return srv.timeNow()
  83. }
  84. return time.Now()
  85. }
  86. func init() {
  87. ipnlocal.RegisterNewSSHServer(func(logf logger.Logf, lb *ipnlocal.LocalBackend) (ipnlocal.SSHServer, error) {
  88. tsd, err := os.Executable()
  89. if err != nil {
  90. return nil, err
  91. }
  92. srv := &server{
  93. lb: lb,
  94. logf: logf,
  95. tailscaledPath: tsd,
  96. }
  97. return srv, nil
  98. })
  99. }
  100. // attachSessionToConnIfNotShutdown ensures that srv is not shutdown before
  101. // attaching the session to the conn. This ensures that once Shutdown is called,
  102. // new sessions are not allowed and existing ones are cleaned up.
  103. // It reports whether ss was attached to the conn.
  104. func (srv *server) attachSessionToConnIfNotShutdown(ss *sshSession) bool {
  105. srv.mu.Lock()
  106. defer srv.mu.Unlock()
  107. if srv.shutdownCalled {
  108. // Do not start any new sessions.
  109. return false
  110. }
  111. ss.conn.attachSession(ss)
  112. return true
  113. }
  114. func (srv *server) trackActiveConn(c *conn, add bool) {
  115. srv.mu.Lock()
  116. defer srv.mu.Unlock()
  117. if add {
  118. mak.Set(&srv.activeConns, c, true)
  119. return
  120. }
  121. delete(srv.activeConns, c)
  122. }
  123. // HandleSSHConn handles a Tailscale SSH connection from c.
  124. // This is the entry point for all SSH connections.
  125. // When this returns, the connection is closed.
  126. func (srv *server) HandleSSHConn(nc net.Conn) error {
  127. metricIncomingConnections.Add(1)
  128. c, err := srv.newConn()
  129. if err != nil {
  130. return err
  131. }
  132. srv.trackActiveConn(c, true) // add
  133. defer srv.trackActiveConn(c, false) // remove
  134. c.HandleConn(nc)
  135. // Return nil to signal to netstack's interception that it doesn't need to
  136. // log. If ss.HandleConn had problems, it can log itself (ideally on an
  137. // sshSession.logf).
  138. return nil
  139. }
  140. // Shutdown terminates all active sessions.
  141. func (srv *server) Shutdown() {
  142. srv.mu.Lock()
  143. srv.shutdownCalled = true
  144. for c := range srv.activeConns {
  145. c.Close()
  146. }
  147. srv.mu.Unlock()
  148. srv.sessionWaitGroup.Wait()
  149. }
  150. // OnPolicyChange terminates any active sessions that no longer match
  151. // the SSH access policy.
  152. func (srv *server) OnPolicyChange() {
  153. srv.mu.Lock()
  154. defer srv.mu.Unlock()
  155. for c := range srv.activeConns {
  156. if c.info == nil {
  157. // c.info is nil when the connection hasn't been authenticated yet.
  158. // In that case, the connection will be terminated when it is.
  159. continue
  160. }
  161. go c.checkStillValid()
  162. }
  163. }
  164. // conn represents a single SSH connection and its associated
  165. // ssh.Server.
  166. //
  167. // During the lifecycle of a connection, the following are called in order:
  168. // Setup and discover server info
  169. // - ServerConfigCallback
  170. //
  171. // Do the user auth
  172. // - NoClientAuthHandler
  173. // - PublicKeyHandler (only if NoClientAuthHandler returns errPubKeyRequired)
  174. //
  175. // Once auth is done, the conn can be multiplexed with multiple sessions and
  176. // channels concurrently. At which point any of the following can be called
  177. // in any order.
  178. // - c.handleSessionPostSSHAuth
  179. // - c.mayForwardLocalPortTo followed by ssh.DirectTCPIPHandler
  180. type conn struct {
  181. *ssh.Server
  182. srv *server
  183. insecureSkipTailscaleAuth bool // used by tests.
  184. // idH is the RFC4253 sec8 hash H. It is used to identify the connection,
  185. // and is shared among all sessions. It should not be shared outside
  186. // process. It is confusingly referred to as SessionID by the gliderlabs/ssh
  187. // library.
  188. idH string
  189. connID string // ID that's shared with control
  190. // anyPasswordIsOkay is whether the client is authorized but has requested
  191. // password-based auth to work around their buggy SSH client. When set, we
  192. // accept any password in the PasswordHandler.
  193. anyPasswordIsOkay bool // set by NoClientAuthCallback
  194. action0 *tailcfg.SSHAction // set by doPolicyAuth; first matching action
  195. currentAction *tailcfg.SSHAction // set by doPolicyAuth, updated by resolveNextAction
  196. finalAction *tailcfg.SSHAction // set by doPolicyAuth or resolveNextAction
  197. finalActionErr error // set by doPolicyAuth or resolveNextAction
  198. info *sshConnInfo // set by setInfo
  199. localUser *userMeta // set by doPolicyAuth
  200. userGroupIDs []string // set by doPolicyAuth
  201. pubKey gossh.PublicKey // set by doPolicyAuth
  202. // mu protects the following fields.
  203. //
  204. // srv.mu should be acquired prior to mu.
  205. // It is safe to just acquire mu, but unsafe to
  206. // acquire mu and then srv.mu.
  207. mu sync.Mutex // protects the following
  208. sessions []*sshSession
  209. }
  210. func (c *conn) logf(format string, args ...any) {
  211. format = fmt.Sprintf("%v: %v", c.connID, format)
  212. c.srv.logf(format, args...)
  213. }
  214. func (c *conn) vlogf(format string, args ...any) {
  215. if sshVerboseLogging() {
  216. c.logf(format, args...)
  217. }
  218. }
  219. // isAuthorized walks through the action chain and returns nil if the connection
  220. // is authorized. If the connection is not authorized, it returns
  221. // gossh.ErrDenied. If the action chain resolution fails, it returns the
  222. // resolution error.
  223. func (c *conn) isAuthorized(ctx ssh.Context) error {
  224. action := c.currentAction
  225. for {
  226. if action.Accept {
  227. if c.pubKey != nil {
  228. metricPublicKeyAccepts.Add(1)
  229. }
  230. return nil
  231. }
  232. if action.Reject || action.HoldAndDelegate == "" {
  233. return gossh.ErrDenied
  234. }
  235. var err error
  236. action, err = c.resolveNextAction(ctx)
  237. if err != nil {
  238. return err
  239. }
  240. if action.Message != "" {
  241. if err := ctx.SendAuthBanner(action.Message); err != nil {
  242. return err
  243. }
  244. }
  245. }
  246. }
  247. // errPubKeyRequired is returned by NoClientAuthCallback to make the client
  248. // resort to public-key auth; not user visible.
  249. var errPubKeyRequired = errors.New("ssh publickey required")
  250. // NoClientAuthCallback implements gossh.NoClientAuthCallback and is called by
  251. // the ssh.Server when the client first connects with the "none"
  252. // authentication method.
  253. //
  254. // It is responsible for continuing policy evaluation from BannerCallback (or
  255. // starting it afresh). It returns an error if the policy evaluation fails, or
  256. // if the decision is "reject"
  257. //
  258. // It either returns nil (accept) or errPubKeyRequired or gossh.ErrDenied
  259. // (reject). The errors may be wrapped.
  260. func (c *conn) NoClientAuthCallback(ctx ssh.Context) error {
  261. if c.insecureSkipTailscaleAuth {
  262. return nil
  263. }
  264. if err := c.doPolicyAuth(ctx, nil /* no pub key */); err != nil {
  265. return err
  266. }
  267. if err := c.isAuthorized(ctx); err != nil {
  268. return err
  269. }
  270. // Let users specify a username ending in +password to force password auth.
  271. // This exists for buggy SSH clients that get confused by success from
  272. // "none" auth.
  273. if strings.HasSuffix(ctx.User(), forcePasswordSuffix) {
  274. c.anyPasswordIsOkay = true
  275. return errors.New("any password please") // not shown to users
  276. }
  277. return nil
  278. }
  279. func (c *conn) nextAuthMethodCallback(cm gossh.ConnMetadata, prevErrors []error) (nextMethod []string) {
  280. switch {
  281. case c.anyPasswordIsOkay:
  282. nextMethod = append(nextMethod, "password")
  283. case len(prevErrors) > 0 && prevErrors[len(prevErrors)-1] == errPubKeyRequired:
  284. nextMethod = append(nextMethod, "publickey")
  285. }
  286. // The fake "tailscale" method is always appended to next so OpenSSH renders
  287. // that in parens as the final failure. (It also shows up in "ssh -v", etc)
  288. nextMethod = append(nextMethod, "tailscale")
  289. return
  290. }
  291. // fakePasswordHandler is our implementation of the PasswordHandler hook that
  292. // checks whether the user's password is correct. But we don't actually use
  293. // passwords. This exists only for when the user's username ends in "+password"
  294. // to signal that their SSH client is buggy and gets confused by auth type
  295. // "none" succeeding and they want our SSH server to require a dummy password
  296. // prompt instead. We then accept any password since we've already authenticated
  297. // & authorized them.
  298. func (c *conn) fakePasswordHandler(ctx ssh.Context, password string) bool {
  299. return c.anyPasswordIsOkay
  300. }
  301. // PublicKeyHandler implements ssh.PublicKeyHandler is called by the
  302. // ssh.Server when the client presents a public key.
  303. func (c *conn) PublicKeyHandler(ctx ssh.Context, pubKey ssh.PublicKey) error {
  304. if err := c.doPolicyAuth(ctx, pubKey); err != nil {
  305. // TODO(maisem/bradfitz): surface the error here.
  306. c.logf("rejecting SSH public key %s: %v", bytes.TrimSpace(gossh.MarshalAuthorizedKey(pubKey)), err)
  307. return err
  308. }
  309. if err := c.isAuthorized(ctx); err != nil {
  310. return err
  311. }
  312. c.logf("accepting SSH public key %s", bytes.TrimSpace(gossh.MarshalAuthorizedKey(pubKey)))
  313. return nil
  314. }
  315. // doPolicyAuth verifies that conn can proceed with the specified (optional)
  316. // pubKey. It returns nil if the matching policy action is Accept or
  317. // HoldAndDelegate. If pubKey is nil, there was no policy match but there is a
  318. // policy that might match a public key it returns errPubKeyRequired. Otherwise,
  319. // it returns gossh.ErrDenied.
  320. func (c *conn) doPolicyAuth(ctx ssh.Context, pubKey ssh.PublicKey) error {
  321. if err := c.setInfo(ctx); err != nil {
  322. c.logf("failed to get conninfo: %v", err)
  323. return gossh.ErrDenied
  324. }
  325. a, localUser, err := c.evaluatePolicy(pubKey)
  326. if err != nil {
  327. if pubKey == nil && c.havePubKeyPolicy() {
  328. return errPubKeyRequired
  329. }
  330. return fmt.Errorf("%w: %v", gossh.ErrDenied, err)
  331. }
  332. c.action0 = a
  333. c.currentAction = a
  334. c.pubKey = pubKey
  335. if a.Message != "" {
  336. if err := ctx.SendAuthBanner(a.Message); err != nil {
  337. return fmt.Errorf("SendBanner: %w", err)
  338. }
  339. }
  340. if a.Accept || a.HoldAndDelegate != "" {
  341. if a.Accept {
  342. c.finalAction = a
  343. }
  344. lu, err := userLookup(localUser)
  345. if err != nil {
  346. c.logf("failed to look up %v: %v", localUser, err)
  347. ctx.SendAuthBanner(fmt.Sprintf("failed to look up %v\r\n", localUser))
  348. return err
  349. }
  350. gids, err := lu.GroupIds()
  351. if err != nil {
  352. c.logf("failed to look up local user's group IDs: %v", err)
  353. return err
  354. }
  355. c.userGroupIDs = gids
  356. c.localUser = lu
  357. return nil
  358. }
  359. if a.Reject {
  360. c.finalAction = a
  361. return gossh.ErrDenied
  362. }
  363. // Shouldn't get here, but:
  364. return gossh.ErrDenied
  365. }
  366. // ServerConfig implements ssh.ServerConfigCallback.
  367. func (c *conn) ServerConfig(ctx ssh.Context) *gossh.ServerConfig {
  368. return &gossh.ServerConfig{
  369. NoClientAuth: true, // required for the NoClientAuthCallback to run
  370. NextAuthMethodCallback: c.nextAuthMethodCallback,
  371. }
  372. }
  373. func (srv *server) newConn() (*conn, error) {
  374. srv.mu.Lock()
  375. if srv.shutdownCalled {
  376. srv.mu.Unlock()
  377. // Stop accepting new connections.
  378. // Connections in the auth phase are handled in handleConnPostSSHAuth.
  379. // Existing sessions are terminated by Shutdown.
  380. return nil, gossh.ErrDenied
  381. }
  382. srv.mu.Unlock()
  383. c := &conn{srv: srv}
  384. now := srv.now()
  385. c.connID = fmt.Sprintf("ssh-conn-%s-%02x", now.UTC().Format("20060102T150405"), randBytes(5))
  386. fwdHandler := &ssh.ForwardedTCPHandler{}
  387. c.Server = &ssh.Server{
  388. Version: "Tailscale",
  389. ServerConfigCallback: c.ServerConfig,
  390. NoClientAuthHandler: c.NoClientAuthCallback,
  391. PublicKeyHandler: c.PublicKeyHandler,
  392. PasswordHandler: c.fakePasswordHandler,
  393. Handler: c.handleSessionPostSSHAuth,
  394. LocalPortForwardingCallback: c.mayForwardLocalPortTo,
  395. ReversePortForwardingCallback: c.mayReversePortForwardTo,
  396. SubsystemHandlers: map[string]ssh.SubsystemHandler{
  397. "sftp": c.handleSessionPostSSHAuth,
  398. },
  399. // Note: the direct-tcpip channel handler and LocalPortForwardingCallback
  400. // only adds support for forwarding ports from the local machine.
  401. // TODO(maisem/bradfitz): add remote port forwarding support.
  402. ChannelHandlers: map[string]ssh.ChannelHandler{
  403. "direct-tcpip": ssh.DirectTCPIPHandler,
  404. },
  405. RequestHandlers: map[string]ssh.RequestHandler{
  406. "tcpip-forward": fwdHandler.HandleSSHRequest,
  407. "cancel-tcpip-forward": fwdHandler.HandleSSHRequest,
  408. },
  409. }
  410. ss := c.Server
  411. for k, v := range ssh.DefaultRequestHandlers {
  412. ss.RequestHandlers[k] = v
  413. }
  414. for k, v := range ssh.DefaultChannelHandlers {
  415. ss.ChannelHandlers[k] = v
  416. }
  417. for k, v := range ssh.DefaultSubsystemHandlers {
  418. ss.SubsystemHandlers[k] = v
  419. }
  420. keys, err := srv.lb.GetSSH_HostKeys()
  421. if err != nil {
  422. return nil, err
  423. }
  424. for _, signer := range keys {
  425. ss.AddHostKey(signer)
  426. }
  427. return c, nil
  428. }
  429. // mayReversePortPortForwardTo reports whether the ctx should be allowed to port forward
  430. // to the specified host and port.
  431. // TODO(bradfitz/maisem): should we have more checks on host/port?
  432. func (c *conn) mayReversePortForwardTo(ctx ssh.Context, destinationHost string, destinationPort uint32) bool {
  433. if c.finalAction != nil && c.finalAction.AllowRemotePortForwarding {
  434. metricRemotePortForward.Add(1)
  435. return true
  436. }
  437. return false
  438. }
  439. // mayForwardLocalPortTo reports whether the ctx should be allowed to port forward
  440. // to the specified host and port.
  441. // TODO(bradfitz/maisem): should we have more checks on host/port?
  442. func (c *conn) mayForwardLocalPortTo(ctx ssh.Context, destinationHost string, destinationPort uint32) bool {
  443. if c.finalAction != nil && c.finalAction.AllowLocalPortForwarding {
  444. metricLocalPortForward.Add(1)
  445. return true
  446. }
  447. return false
  448. }
  449. // havePubKeyPolicy reports whether any policy rule may provide access by means
  450. // of a ssh.PublicKey.
  451. func (c *conn) havePubKeyPolicy() bool {
  452. if c.info == nil {
  453. panic("havePubKeyPolicy called before setInfo")
  454. }
  455. // Is there any rule that looks like it'd require a public key for this
  456. // sshUser?
  457. pol, ok := c.sshPolicy()
  458. if !ok {
  459. return false
  460. }
  461. for _, r := range pol.Rules {
  462. if c.ruleExpired(r) {
  463. continue
  464. }
  465. if mapLocalUser(r.SSHUsers, c.info.sshUser) == "" {
  466. continue
  467. }
  468. for _, p := range r.Principals {
  469. if len(p.PubKeys) > 0 && c.principalMatchesTailscaleIdentity(p) {
  470. return true
  471. }
  472. }
  473. }
  474. return false
  475. }
  476. // sshPolicy returns the SSHPolicy for current node.
  477. // If there is no SSHPolicy in the netmap, it returns a debugPolicy
  478. // if one is defined.
  479. func (c *conn) sshPolicy() (_ *tailcfg.SSHPolicy, ok bool) {
  480. lb := c.srv.lb
  481. if !lb.ShouldRunSSH() {
  482. return nil, false
  483. }
  484. nm := lb.NetMap()
  485. if nm == nil {
  486. return nil, false
  487. }
  488. if pol := nm.SSHPolicy; pol != nil && !envknob.SSHIgnoreTailnetPolicy() {
  489. return pol, true
  490. }
  491. debugPolicyFile := envknob.SSHPolicyFile()
  492. if debugPolicyFile != "" {
  493. c.logf("reading debug SSH policy file: %v", debugPolicyFile)
  494. f, err := os.ReadFile(debugPolicyFile)
  495. if err != nil {
  496. c.logf("error reading debug SSH policy file: %v", err)
  497. return nil, false
  498. }
  499. p := new(tailcfg.SSHPolicy)
  500. if err := json.Unmarshal(f, p); err != nil {
  501. c.logf("invalid JSON in %v: %v", debugPolicyFile, err)
  502. return nil, false
  503. }
  504. return p, true
  505. }
  506. return nil, false
  507. }
  508. func toIPPort(a net.Addr) (ipp netip.AddrPort) {
  509. ta, ok := a.(*net.TCPAddr)
  510. if !ok {
  511. return
  512. }
  513. tanetaddr, ok := netip.AddrFromSlice(ta.IP)
  514. if !ok {
  515. return
  516. }
  517. return netip.AddrPortFrom(tanetaddr.Unmap(), uint16(ta.Port))
  518. }
  519. // connInfo returns a populated sshConnInfo from the provided arguments,
  520. // validating only that they represent a known Tailscale identity.
  521. func (c *conn) setInfo(ctx ssh.Context) error {
  522. if c.info != nil {
  523. return nil
  524. }
  525. ci := &sshConnInfo{
  526. sshUser: strings.TrimSuffix(ctx.User(), forcePasswordSuffix),
  527. src: toIPPort(ctx.RemoteAddr()),
  528. dst: toIPPort(ctx.LocalAddr()),
  529. }
  530. if !tsaddr.IsTailscaleIP(ci.dst.Addr()) {
  531. return fmt.Errorf("tailssh: rejecting non-Tailscale local address %v", ci.dst)
  532. }
  533. if !tsaddr.IsTailscaleIP(ci.src.Addr()) {
  534. return fmt.Errorf("tailssh: rejecting non-Tailscale remote address %v", ci.src)
  535. }
  536. node, uprof, ok := c.srv.lb.WhoIs(ci.src)
  537. if !ok {
  538. return fmt.Errorf("unknown Tailscale identity from src %v", ci.src)
  539. }
  540. ci.node = node
  541. ci.uprof = uprof
  542. c.idH = ctx.SessionID()
  543. c.info = ci
  544. c.logf("handling conn: %v", ci.String())
  545. return nil
  546. }
  547. // evaluatePolicy returns the SSHAction and localUser after evaluating
  548. // the SSHPolicy for this conn. The pubKey may be nil for "none" auth.
  549. func (c *conn) evaluatePolicy(pubKey gossh.PublicKey) (_ *tailcfg.SSHAction, localUser string, _ error) {
  550. pol, ok := c.sshPolicy()
  551. if !ok {
  552. return nil, "", fmt.Errorf("tailssh: rejecting connection; no SSH policy")
  553. }
  554. a, localUser, ok := c.evalSSHPolicy(pol, pubKey)
  555. if !ok {
  556. return nil, "", fmt.Errorf("tailssh: rejecting connection; no matching policy")
  557. }
  558. return a, localUser, nil
  559. }
  560. // pubKeyCacheEntry is the cache value for an HTTPS URL of public keys (like
  561. // "https://github.com/foo.keys")
  562. type pubKeyCacheEntry struct {
  563. lines []string
  564. etag string // if sent by server
  565. at time.Time
  566. }
  567. const (
  568. pubKeyCacheDuration = time.Minute // how long to cache non-empty public keys
  569. pubKeyCacheEmptyDuration = 15 * time.Second // how long to cache empty responses
  570. )
  571. func (srv *server) fetchPublicKeysURLCached(url string) (ce pubKeyCacheEntry, ok bool) {
  572. srv.mu.Lock()
  573. defer srv.mu.Unlock()
  574. // Mostly don't care about the size of this cache. Clean rarely.
  575. if m := srv.fetchPublicKeysCache; len(m) > 50 {
  576. tooOld := srv.now().Add(pubKeyCacheDuration * 10)
  577. for k, ce := range m {
  578. if ce.at.Before(tooOld) {
  579. delete(m, k)
  580. }
  581. }
  582. }
  583. ce, ok = srv.fetchPublicKeysCache[url]
  584. if !ok {
  585. return ce, false
  586. }
  587. maxAge := pubKeyCacheDuration
  588. if len(ce.lines) == 0 {
  589. maxAge = pubKeyCacheEmptyDuration
  590. }
  591. return ce, srv.now().Sub(ce.at) < maxAge
  592. }
  593. func (srv *server) pubKeyClient() *http.Client {
  594. if srv.pubKeyHTTPClient != nil {
  595. return srv.pubKeyHTTPClient
  596. }
  597. return http.DefaultClient
  598. }
  599. // fetchPublicKeysURL fetches the public keys from a URL. The strings are in the
  600. // the typical public key "type base64-string [comment]" format seen at e.g.
  601. // https://github.com/USER.keys
  602. func (srv *server) fetchPublicKeysURL(url string) ([]string, error) {
  603. if !strings.HasPrefix(url, "https://") {
  604. return nil, errors.New("invalid URL scheme")
  605. }
  606. ce, ok := srv.fetchPublicKeysURLCached(url)
  607. if ok {
  608. return ce.lines, nil
  609. }
  610. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  611. defer cancel()
  612. req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
  613. if err != nil {
  614. return nil, err
  615. }
  616. if ce.etag != "" {
  617. req.Header.Add("If-None-Match", ce.etag)
  618. }
  619. res, err := srv.pubKeyClient().Do(req)
  620. if err != nil {
  621. return nil, err
  622. }
  623. defer res.Body.Close()
  624. var lines []string
  625. var etag string
  626. switch res.StatusCode {
  627. default:
  628. err = fmt.Errorf("unexpected status %v", res.Status)
  629. srv.logf("fetching public keys from %s: %v", url, err)
  630. case http.StatusNotModified:
  631. lines = ce.lines
  632. etag = ce.etag
  633. case http.StatusOK:
  634. var all []byte
  635. all, err = io.ReadAll(io.LimitReader(res.Body, 4<<10))
  636. if s := strings.TrimSpace(string(all)); s != "" {
  637. lines = strings.Split(s, "\n")
  638. }
  639. etag = res.Header.Get("Etag")
  640. }
  641. srv.mu.Lock()
  642. defer srv.mu.Unlock()
  643. mak.Set(&srv.fetchPublicKeysCache, url, pubKeyCacheEntry{
  644. at: srv.now(),
  645. lines: lines,
  646. etag: etag,
  647. })
  648. return lines, err
  649. }
  650. // handleSessionPostSSHAuth runs an SSH session after the SSH-level authentication,
  651. // but not necessarily before all the Tailscale-level extra verification has
  652. // completed. It also handles SFTP requests.
  653. func (c *conn) handleSessionPostSSHAuth(s ssh.Session) {
  654. // Do this check after auth, but before starting the session.
  655. switch s.Subsystem() {
  656. case "sftp", "":
  657. metricSFTP.Add(1)
  658. default:
  659. fmt.Fprintf(s.Stderr(), "Unsupported subsystem %q\r\n", s.Subsystem())
  660. s.Exit(1)
  661. return
  662. }
  663. ss := c.newSSHSession(s)
  664. ss.logf("handling new SSH connection from %v (%v) to ssh-user %q", c.info.uprof.LoginName, c.info.src.Addr(), c.localUser.Username)
  665. ss.logf("access granted to %v as ssh-user %q", c.info.uprof.LoginName, c.localUser.Username)
  666. ss.run()
  667. }
  668. // resolveNextAction starts at c.currentAction and makes it way through the
  669. // action chain one step at a time. An action without a HoldAndDelegate is
  670. // considered the final action. Once a final action is reached, this function
  671. // will keep returning that action. It updates c.currentAction to the next
  672. // action in the chain. When the final action is reached, it also sets
  673. // c.finalAction to the final action.
  674. func (c *conn) resolveNextAction(sctx ssh.Context) (action *tailcfg.SSHAction, err error) {
  675. if c.finalAction != nil || c.finalActionErr != nil {
  676. return c.finalAction, c.finalActionErr
  677. }
  678. defer func() {
  679. if action != nil {
  680. c.currentAction = action
  681. if action.Accept || action.Reject {
  682. c.finalAction = action
  683. }
  684. }
  685. if err != nil {
  686. c.finalActionErr = err
  687. }
  688. }()
  689. ctx, cancel := context.WithCancel(sctx)
  690. defer cancel()
  691. // Loop processing/fetching Actions until one reaches a
  692. // terminal state (Accept, Reject, or invalid Action), or
  693. // until fetchSSHAction times out due to the context being
  694. // done (client disconnect) or its 30 minute timeout passes.
  695. // (Which is a long time for somebody to see login
  696. // instructions and go to a URL to do something.)
  697. action = c.currentAction
  698. if action.Accept || action.Reject {
  699. if action.Reject {
  700. metricTerminalReject.Add(1)
  701. } else {
  702. metricTerminalAccept.Add(1)
  703. }
  704. return action, nil
  705. }
  706. url := action.HoldAndDelegate
  707. if url == "" {
  708. metricTerminalMalformed.Add(1)
  709. return nil, errors.New("reached Action that lacked Accept, Reject, and HoldAndDelegate")
  710. }
  711. metricHolds.Add(1)
  712. url = c.expandDelegateURLLocked(url)
  713. nextAction, err := c.fetchSSHAction(ctx, url)
  714. if err != nil {
  715. metricTerminalFetchError.Add(1)
  716. return nil, fmt.Errorf("fetching SSHAction from %s: %w", url, err)
  717. }
  718. return nextAction, nil
  719. }
  720. func (c *conn) expandDelegateURLLocked(actionURL string) string {
  721. nm := c.srv.lb.NetMap()
  722. ci := c.info
  723. lu := c.localUser
  724. var dstNodeID string
  725. if nm != nil {
  726. dstNodeID = fmt.Sprint(int64(nm.SelfNode.ID()))
  727. }
  728. return strings.NewReplacer(
  729. "$SRC_NODE_IP", url.QueryEscape(ci.src.Addr().String()),
  730. "$SRC_NODE_ID", fmt.Sprint(int64(ci.node.ID())),
  731. "$DST_NODE_IP", url.QueryEscape(ci.dst.Addr().String()),
  732. "$DST_NODE_ID", dstNodeID,
  733. "$SSH_USER", url.QueryEscape(ci.sshUser),
  734. "$LOCAL_USER", url.QueryEscape(lu.Username),
  735. ).Replace(actionURL)
  736. }
  737. func (c *conn) expandPublicKeyURL(pubKeyURL string) string {
  738. if !strings.Contains(pubKeyURL, "$") {
  739. return pubKeyURL
  740. }
  741. loginName := c.info.uprof.LoginName
  742. localPart, _, _ := strings.Cut(loginName, "@")
  743. return strings.NewReplacer(
  744. "$LOGINNAME_EMAIL", loginName,
  745. "$LOGINNAME_LOCALPART", localPart,
  746. ).Replace(pubKeyURL)
  747. }
  748. // sshSession is an accepted Tailscale SSH session.
  749. type sshSession struct {
  750. ssh.Session
  751. sharedID string // ID that's shared with control
  752. logf logger.Logf
  753. ctx context.Context
  754. cancelCtx context.CancelCauseFunc
  755. conn *conn
  756. agentListener net.Listener // non-nil if agent-forwarding requested+allowed
  757. // initialized by launchProcess:
  758. cmd *exec.Cmd
  759. wrStdin io.WriteCloser
  760. rdStdout io.ReadCloser
  761. rdStderr io.ReadCloser // rdStderr is nil for pty sessions
  762. ptyReq *ssh.Pty // non-nil for pty sessions
  763. // childPipes is a list of pipes that need to be closed when the process exits.
  764. // For pty sessions, this is the tty fd.
  765. // For non-pty sessions, this is the stdin, stdout, stderr fds.
  766. childPipes []io.Closer
  767. // We use this sync.Once to ensure that we only terminate the process once,
  768. // either it exits itself or is terminated
  769. exitOnce sync.Once
  770. }
  771. func (ss *sshSession) vlogf(format string, args ...any) {
  772. if sshVerboseLogging() {
  773. ss.logf(format, args...)
  774. }
  775. }
  776. func (c *conn) newSSHSession(s ssh.Session) *sshSession {
  777. sharedID := fmt.Sprintf("sess-%s-%02x", c.srv.now().UTC().Format("20060102T150405"), randBytes(5))
  778. c.logf("starting session: %v", sharedID)
  779. ctx, cancel := context.WithCancelCause(s.Context())
  780. return &sshSession{
  781. Session: s,
  782. sharedID: sharedID,
  783. ctx: ctx,
  784. cancelCtx: cancel,
  785. conn: c,
  786. logf: logger.WithPrefix(c.srv.logf, "ssh-session("+sharedID+"): "),
  787. }
  788. }
  789. // isStillValid reports whether the conn is still valid.
  790. func (c *conn) isStillValid() bool {
  791. a, localUser, err := c.evaluatePolicy(c.pubKey)
  792. c.vlogf("stillValid: %+v %v %v", a, localUser, err)
  793. if err != nil {
  794. return false
  795. }
  796. if !a.Accept && a.HoldAndDelegate == "" {
  797. return false
  798. }
  799. return c.localUser.Username == localUser
  800. }
  801. // checkStillValid checks that the conn is still valid per the latest SSHPolicy.
  802. // If not, it terminates all sessions associated with the conn.
  803. func (c *conn) checkStillValid() {
  804. if c.isStillValid() {
  805. return
  806. }
  807. metricPolicyChangeKick.Add(1)
  808. c.logf("session no longer valid per new SSH policy; closing")
  809. c.mu.Lock()
  810. defer c.mu.Unlock()
  811. for _, s := range c.sessions {
  812. s.cancelCtx(userVisibleError{
  813. fmt.Sprintf("Access revoked.\r\n"),
  814. context.Canceled,
  815. })
  816. }
  817. }
  818. func (c *conn) fetchSSHAction(ctx context.Context, url string) (*tailcfg.SSHAction, error) {
  819. ctx, cancel := context.WithTimeout(ctx, 30*time.Minute)
  820. defer cancel()
  821. bo := backoff.NewBackoff("fetch-ssh-action", c.logf, 10*time.Second)
  822. for {
  823. if err := ctx.Err(); err != nil {
  824. return nil, err
  825. }
  826. req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
  827. if err != nil {
  828. return nil, err
  829. }
  830. res, err := c.srv.lb.DoNoiseRequest(req)
  831. if err != nil {
  832. bo.BackOff(ctx, err)
  833. continue
  834. }
  835. if res.StatusCode != 200 {
  836. body, _ := io.ReadAll(res.Body)
  837. res.Body.Close()
  838. if len(body) > 1<<10 {
  839. body = body[:1<<10]
  840. }
  841. c.logf("fetch of %v: %s, %s", url, res.Status, body)
  842. bo.BackOff(ctx, fmt.Errorf("unexpected status: %v", res.Status))
  843. continue
  844. }
  845. a := new(tailcfg.SSHAction)
  846. err = json.NewDecoder(res.Body).Decode(a)
  847. res.Body.Close()
  848. if err != nil {
  849. c.logf("invalid next SSHAction JSON from %v: %v", url, err)
  850. bo.BackOff(ctx, err)
  851. continue
  852. }
  853. return a, nil
  854. }
  855. }
  856. // killProcessOnContextDone waits for ss.ctx to be done and kills the process,
  857. // unless the process has already exited.
  858. func (ss *sshSession) killProcessOnContextDone() {
  859. <-ss.ctx.Done()
  860. // Either the process has already exited, in which case this does nothing.
  861. // Or, the process is still running in which case this will kill it.
  862. ss.exitOnce.Do(func() {
  863. err := context.Cause(ss.ctx)
  864. if serr, ok := err.(SSHTerminationError); ok {
  865. msg := serr.SSHTerminationMessage()
  866. if msg != "" {
  867. io.WriteString(ss.Stderr(), "\r\n\r\n"+msg+"\r\n\r\n")
  868. }
  869. }
  870. ss.logf("terminating SSH session from %v: %v", ss.conn.info.src.Addr(), err)
  871. // We don't need to Process.Wait here, sshSession.run() does
  872. // the waiting regardless of termination reason.
  873. // TODO(maisem): should this be a SIGTERM followed by a SIGKILL?
  874. ss.cmd.Process.Kill()
  875. })
  876. }
  877. // attachSession registers ss as an active session.
  878. func (c *conn) attachSession(ss *sshSession) {
  879. c.srv.sessionWaitGroup.Add(1)
  880. if ss.sharedID == "" {
  881. panic("empty sharedID")
  882. }
  883. c.mu.Lock()
  884. defer c.mu.Unlock()
  885. c.sessions = append(c.sessions, ss)
  886. }
  887. // detachSession unregisters s from the list of active sessions.
  888. func (c *conn) detachSession(ss *sshSession) {
  889. defer c.srv.sessionWaitGroup.Done()
  890. c.mu.Lock()
  891. defer c.mu.Unlock()
  892. for i, s := range c.sessions {
  893. if s == ss {
  894. c.sessions = append(c.sessions[:i], c.sessions[i+1:]...)
  895. break
  896. }
  897. }
  898. }
  899. var errSessionDone = errors.New("session is done")
  900. // handleSSHAgentForwarding starts a Unix socket listener and in the background
  901. // forwards agent connections between the listener and the ssh.Session.
  902. // On success, it assigns ss.agentListener.
  903. func (ss *sshSession) handleSSHAgentForwarding(s ssh.Session, lu *userMeta) error {
  904. if !ssh.AgentRequested(ss) || !ss.conn.finalAction.AllowAgentForwarding {
  905. return nil
  906. }
  907. ss.logf("ssh: agent forwarding requested")
  908. ln, err := ssh.NewAgentListener()
  909. if err != nil {
  910. return err
  911. }
  912. defer func() {
  913. if err != nil && ln != nil {
  914. ln.Close()
  915. }
  916. }()
  917. uid, err := strconv.ParseUint(lu.Uid, 10, 32)
  918. if err != nil {
  919. return err
  920. }
  921. gid, err := strconv.ParseUint(lu.Gid, 10, 32)
  922. if err != nil {
  923. return err
  924. }
  925. socket := ln.Addr().String()
  926. dir := filepath.Dir(socket)
  927. // Make sure the socket is accessible only by the user.
  928. if err := os.Chmod(socket, 0600); err != nil {
  929. return err
  930. }
  931. if err := os.Chown(socket, int(uid), int(gid)); err != nil {
  932. return err
  933. }
  934. // Make sure the dir is also accessible.
  935. if err := os.Chmod(dir, 0755); err != nil {
  936. return err
  937. }
  938. go ssh.ForwardAgentConnections(ln, s)
  939. ss.agentListener = ln
  940. return nil
  941. }
  942. // run is the entrypoint for a newly accepted SSH session.
  943. //
  944. // It handles ss once it's been accepted and determined
  945. // that it should run.
  946. func (ss *sshSession) run() {
  947. metricActiveSessions.Add(1)
  948. defer metricActiveSessions.Add(-1)
  949. defer ss.cancelCtx(errSessionDone)
  950. if attached := ss.conn.srv.attachSessionToConnIfNotShutdown(ss); !attached {
  951. fmt.Fprintf(ss, "Tailscale SSH is shutting down\r\n")
  952. ss.Exit(1)
  953. return
  954. }
  955. defer ss.conn.detachSession(ss)
  956. lu := ss.conn.localUser
  957. logf := ss.logf
  958. if ss.conn.finalAction.SessionDuration != 0 {
  959. t := time.AfterFunc(ss.conn.finalAction.SessionDuration, func() {
  960. ss.cancelCtx(userVisibleError{
  961. fmt.Sprintf("Session timeout of %v elapsed.", ss.conn.finalAction.SessionDuration),
  962. context.DeadlineExceeded,
  963. })
  964. })
  965. defer t.Stop()
  966. }
  967. if euid := os.Geteuid(); euid != 0 {
  968. if lu.Uid != fmt.Sprint(euid) {
  969. ss.logf("can't switch to user %q from process euid %v", lu.Username, euid)
  970. fmt.Fprintf(ss, "can't switch user\r\n")
  971. ss.Exit(1)
  972. return
  973. }
  974. }
  975. // Take control of the PTY so that we can configure it below.
  976. // See https://github.com/tailscale/tailscale/issues/4146
  977. ss.DisablePTYEmulation()
  978. var rec *recording // or nil if disabled
  979. if ss.Subsystem() != "sftp" {
  980. if err := ss.handleSSHAgentForwarding(ss, lu); err != nil {
  981. ss.logf("agent forwarding failed: %v", err)
  982. } else if ss.agentListener != nil {
  983. // TODO(maisem/bradfitz): add a way to close all session resources
  984. defer ss.agentListener.Close()
  985. }
  986. if ss.shouldRecord() {
  987. var err error
  988. rec, err = ss.startNewRecording()
  989. if err != nil {
  990. var uve userVisibleError
  991. if errors.As(err, &uve) {
  992. fmt.Fprintf(ss, "%s\r\n", uve.SSHTerminationMessage())
  993. } else {
  994. fmt.Fprintf(ss, "can't start new recording\r\n")
  995. }
  996. ss.logf("startNewRecording: %v", err)
  997. ss.Exit(1)
  998. return
  999. }
  1000. ss.logf("startNewRecording: <nil>")
  1001. if rec != nil {
  1002. defer rec.Close()
  1003. }
  1004. }
  1005. }
  1006. err := ss.launchProcess()
  1007. if err != nil {
  1008. logf("start failed: %v", err.Error())
  1009. if errors.Is(err, context.Canceled) {
  1010. err := context.Cause(ss.ctx)
  1011. var uve userVisibleError
  1012. if errors.As(err, &uve) {
  1013. fmt.Fprintf(ss, "%s\r\n", uve)
  1014. }
  1015. }
  1016. ss.Exit(1)
  1017. return
  1018. }
  1019. go ss.killProcessOnContextDone()
  1020. var processDone atomic.Bool
  1021. go func() {
  1022. defer ss.wrStdin.Close()
  1023. if _, err := io.Copy(rec.writer("i", ss.wrStdin), ss); err != nil {
  1024. logf("stdin copy: %v", err)
  1025. ss.cancelCtx(err)
  1026. }
  1027. }()
  1028. outputDone := make(chan struct{})
  1029. var openOutputStreams atomic.Int32
  1030. if ss.rdStderr != nil {
  1031. openOutputStreams.Store(2)
  1032. } else {
  1033. openOutputStreams.Store(1)
  1034. }
  1035. go func() {
  1036. defer ss.rdStdout.Close()
  1037. _, err := io.Copy(rec.writer("o", ss), ss.rdStdout)
  1038. if err != nil && !errors.Is(err, io.EOF) {
  1039. isErrBecauseProcessExited := processDone.Load() && errors.Is(err, syscall.EIO)
  1040. if !isErrBecauseProcessExited {
  1041. logf("stdout copy: %v, %T", err)
  1042. ss.cancelCtx(err)
  1043. }
  1044. }
  1045. if openOutputStreams.Add(-1) == 0 {
  1046. ss.CloseWrite()
  1047. close(outputDone)
  1048. }
  1049. }()
  1050. // rdStderr is nil for ptys.
  1051. if ss.rdStderr != nil {
  1052. go func() {
  1053. defer ss.rdStderr.Close()
  1054. _, err := io.Copy(ss.Stderr(), ss.rdStderr)
  1055. if err != nil {
  1056. logf("stderr copy: %v", err)
  1057. }
  1058. if openOutputStreams.Add(-1) == 0 {
  1059. ss.CloseWrite()
  1060. close(outputDone)
  1061. }
  1062. }()
  1063. }
  1064. err = ss.cmd.Wait()
  1065. processDone.Store(true)
  1066. // This will either make the SSH Termination goroutine be a no-op,
  1067. // or itself will be a no-op because the process was killed by the
  1068. // aforementioned goroutine.
  1069. ss.exitOnce.Do(func() {})
  1070. // Close the process-side of all pipes to signal the asynchronous
  1071. // io.Copy routines reading/writing from the pipes to terminate.
  1072. // Block for the io.Copy to finish before calling ss.Exit below.
  1073. closeAll(ss.childPipes...)
  1074. select {
  1075. case <-outputDone:
  1076. case <-ss.ctx.Done():
  1077. }
  1078. if err == nil {
  1079. ss.logf("Session complete")
  1080. ss.Exit(0)
  1081. return
  1082. }
  1083. if ee, ok := err.(*exec.ExitError); ok {
  1084. code := ee.ProcessState.ExitCode()
  1085. ss.logf("Wait: code=%v", code)
  1086. ss.Exit(code)
  1087. return
  1088. }
  1089. ss.logf("Wait: %v", err)
  1090. ss.Exit(1)
  1091. return
  1092. }
  1093. // recordSSHToLocalDisk is a deprecated dev knob to allow recording SSH sessions
  1094. // to local storage. It is only used if there is no recording configured by the
  1095. // coordination server. This will be removed in the future.
  1096. var recordSSHToLocalDisk = envknob.RegisterBool("TS_DEBUG_LOG_SSH")
  1097. // recorders returns the list of recorders to use for this session.
  1098. // If the final action has a non-empty list of recorders, that list is
  1099. // returned. Otherwise, the list of recorders from the initial action
  1100. // is returned.
  1101. func (ss *sshSession) recorders() ([]netip.AddrPort, *tailcfg.SSHRecorderFailureAction) {
  1102. if len(ss.conn.finalAction.Recorders) > 0 {
  1103. return ss.conn.finalAction.Recorders, ss.conn.finalAction.OnRecordingFailure
  1104. }
  1105. return ss.conn.action0.Recorders, ss.conn.action0.OnRecordingFailure
  1106. }
  1107. func (ss *sshSession) shouldRecord() bool {
  1108. recs, _ := ss.recorders()
  1109. return len(recs) > 0 || recordSSHToLocalDisk()
  1110. }
  1111. type sshConnInfo struct {
  1112. // sshUser is the requested local SSH username ("root", "alice", etc).
  1113. sshUser string
  1114. // src is the Tailscale IP and port that the connection came from.
  1115. src netip.AddrPort
  1116. // dst is the Tailscale IP and port that the connection came for.
  1117. dst netip.AddrPort
  1118. // node is srcIP's node.
  1119. node tailcfg.NodeView
  1120. // uprof is node's UserProfile.
  1121. uprof tailcfg.UserProfile
  1122. }
  1123. func (ci *sshConnInfo) String() string {
  1124. return fmt.Sprintf("%v->%v@%v", ci.src, ci.sshUser, ci.dst)
  1125. }
  1126. func (c *conn) ruleExpired(r *tailcfg.SSHRule) bool {
  1127. if r.RuleExpires == nil {
  1128. return false
  1129. }
  1130. return r.RuleExpires.Before(c.srv.now())
  1131. }
  1132. func (c *conn) evalSSHPolicy(pol *tailcfg.SSHPolicy, pubKey gossh.PublicKey) (a *tailcfg.SSHAction, localUser string, ok bool) {
  1133. for _, r := range pol.Rules {
  1134. if a, localUser, err := c.matchRule(r, pubKey); err == nil {
  1135. return a, localUser, true
  1136. }
  1137. }
  1138. return nil, "", false
  1139. }
  1140. // internal errors for testing; they don't escape to callers or logs.
  1141. var (
  1142. errNilRule = errors.New("nil rule")
  1143. errNilAction = errors.New("nil action")
  1144. errRuleExpired = errors.New("rule expired")
  1145. errPrincipalMatch = errors.New("principal didn't match")
  1146. errUserMatch = errors.New("user didn't match")
  1147. errInvalidConn = errors.New("invalid connection state")
  1148. )
  1149. func (c *conn) matchRule(r *tailcfg.SSHRule, pubKey gossh.PublicKey) (a *tailcfg.SSHAction, localUser string, err error) {
  1150. defer func() {
  1151. c.vlogf("matchRule(%+v): %v", r, err)
  1152. }()
  1153. if c == nil {
  1154. return nil, "", errInvalidConn
  1155. }
  1156. if c.info == nil {
  1157. c.logf("invalid connection state")
  1158. return nil, "", errInvalidConn
  1159. }
  1160. if r == nil {
  1161. return nil, "", errNilRule
  1162. }
  1163. if r.Action == nil {
  1164. return nil, "", errNilAction
  1165. }
  1166. if c.ruleExpired(r) {
  1167. return nil, "", errRuleExpired
  1168. }
  1169. if !r.Action.Reject {
  1170. // For all but Reject rules, SSHUsers is required.
  1171. // If SSHUsers is nil or empty, mapLocalUser will return an
  1172. // empty string anyway.
  1173. localUser = mapLocalUser(r.SSHUsers, c.info.sshUser)
  1174. if localUser == "" {
  1175. return nil, "", errUserMatch
  1176. }
  1177. }
  1178. if ok, err := c.anyPrincipalMatches(r.Principals, pubKey); err != nil {
  1179. return nil, "", err
  1180. } else if !ok {
  1181. return nil, "", errPrincipalMatch
  1182. }
  1183. return r.Action, localUser, nil
  1184. }
  1185. func mapLocalUser(ruleSSHUsers map[string]string, reqSSHUser string) (localUser string) {
  1186. v, ok := ruleSSHUsers[reqSSHUser]
  1187. if !ok {
  1188. v = ruleSSHUsers["*"]
  1189. }
  1190. if v == "=" {
  1191. return reqSSHUser
  1192. }
  1193. return v
  1194. }
  1195. func (c *conn) anyPrincipalMatches(ps []*tailcfg.SSHPrincipal, pubKey gossh.PublicKey) (bool, error) {
  1196. for _, p := range ps {
  1197. if p == nil {
  1198. continue
  1199. }
  1200. if ok, err := c.principalMatches(p, pubKey); err != nil {
  1201. return false, err
  1202. } else if ok {
  1203. return true, nil
  1204. }
  1205. }
  1206. return false, nil
  1207. }
  1208. func (c *conn) principalMatches(p *tailcfg.SSHPrincipal, pubKey gossh.PublicKey) (bool, error) {
  1209. if !c.principalMatchesTailscaleIdentity(p) {
  1210. return false, nil
  1211. }
  1212. return c.principalMatchesPubKey(p, pubKey)
  1213. }
  1214. // principalMatchesTailscaleIdentity reports whether one of p's four fields
  1215. // that match the Tailscale identity match (Node, NodeIP, UserLogin, Any).
  1216. // This function does not consider PubKeys.
  1217. func (c *conn) principalMatchesTailscaleIdentity(p *tailcfg.SSHPrincipal) bool {
  1218. ci := c.info
  1219. if p.Any {
  1220. return true
  1221. }
  1222. if !p.Node.IsZero() && ci.node.Valid() && p.Node == ci.node.StableID() {
  1223. return true
  1224. }
  1225. if p.NodeIP != "" {
  1226. if ip, _ := netip.ParseAddr(p.NodeIP); ip == ci.src.Addr() {
  1227. return true
  1228. }
  1229. }
  1230. if p.UserLogin != "" && ci.uprof.LoginName == p.UserLogin {
  1231. return true
  1232. }
  1233. return false
  1234. }
  1235. func (c *conn) principalMatchesPubKey(p *tailcfg.SSHPrincipal, clientPubKey gossh.PublicKey) (bool, error) {
  1236. if len(p.PubKeys) == 0 {
  1237. return true, nil
  1238. }
  1239. if clientPubKey == nil {
  1240. return false, nil
  1241. }
  1242. knownKeys := p.PubKeys
  1243. if len(knownKeys) == 1 && strings.HasPrefix(knownKeys[0], "https://") {
  1244. var err error
  1245. knownKeys, err = c.srv.fetchPublicKeysURL(c.expandPublicKeyURL(knownKeys[0]))
  1246. if err != nil {
  1247. return false, err
  1248. }
  1249. }
  1250. for _, knownKey := range knownKeys {
  1251. if pubKeyMatchesAuthorizedKey(clientPubKey, knownKey) {
  1252. return true, nil
  1253. }
  1254. }
  1255. return false, nil
  1256. }
  1257. func pubKeyMatchesAuthorizedKey(pubKey ssh.PublicKey, wantKey string) bool {
  1258. wantKeyType, rest, ok := strings.Cut(wantKey, " ")
  1259. if !ok {
  1260. return false
  1261. }
  1262. if pubKey.Type() != wantKeyType {
  1263. return false
  1264. }
  1265. wantKeyB64, _, _ := strings.Cut(rest, " ")
  1266. wantKeyData, _ := base64.StdEncoding.DecodeString(wantKeyB64)
  1267. return len(wantKeyData) > 0 && bytes.Equal(pubKey.Marshal(), wantKeyData)
  1268. }
  1269. func randBytes(n int) []byte {
  1270. b := make([]byte, n)
  1271. if _, err := rand.Read(b); err != nil {
  1272. panic(err)
  1273. }
  1274. return b
  1275. }
  1276. // CastHeader is the header of an asciinema file.
  1277. type CastHeader struct {
  1278. // Version is the asciinema file format version.
  1279. Version int `json:"version"`
  1280. // Width is the terminal width in characters.
  1281. // It is non-zero for Pty sessions.
  1282. Width int `json:"width"`
  1283. // Height is the terminal height in characters.
  1284. // It is non-zero for Pty sessions.
  1285. Height int `json:"height"`
  1286. // Timestamp is the unix timestamp of when the recording started.
  1287. Timestamp int64 `json:"timestamp"`
  1288. // Env is the environment variables of the session.
  1289. // Only "TERM" is set (2023-03-22).
  1290. Env map[string]string `json:"env"`
  1291. // Command is the command that was executed.
  1292. // Typically empty for shell sessions.
  1293. Command string `json:"command,omitempty"`
  1294. // Tailscale-specific fields:
  1295. // SrcNode is the FQDN of the node originating the connection.
  1296. // It is also the MagicDNS name for the node.
  1297. // It does not have a trailing dot.
  1298. // e.g. "host.tail-scale.ts.net"
  1299. SrcNode string `json:"srcNode"`
  1300. // SrcNodeID is the node ID of the node originating the connection.
  1301. SrcNodeID tailcfg.StableNodeID `json:"srcNodeID"`
  1302. // SrcNodeTags is the list of tags on the node originating the connection (if any).
  1303. SrcNodeTags []string `json:"srcNodeTags,omitempty"`
  1304. // SrcNodeUserID is the user ID of the node originating the connection (if not tagged).
  1305. SrcNodeUserID tailcfg.UserID `json:"srcNodeUserID,omitempty"` // if not tagged
  1306. // SrcNodeUser is the LoginName of the node originating the connection (if not tagged).
  1307. SrcNodeUser string `json:"srcNodeUser,omitempty"`
  1308. // SSHUser is the username as presented by the client.
  1309. SSHUser string `json:"sshUser"` // as presented by the client
  1310. // LocalUser is the effective username on the server.
  1311. LocalUser string `json:"localUser"`
  1312. // ConnectionID uniquely identifies a connection made to the SSH server.
  1313. // It may be shared across multiple sessions over the same connection in
  1314. // case of SSH multiplexing.
  1315. ConnectionID string `json:"connectionID"`
  1316. }
  1317. // sessionRecordingClient returns an http.Client that uses srv.lb.Dialer() to
  1318. // dial connections. This is used to make requests to the session recording
  1319. // server to upload session recordings.
  1320. // It uses the provided dialCtx to dial connections, and limits a single dial
  1321. // to 5 seconds.
  1322. func (ss *sshSession) sessionRecordingClient(dialCtx context.Context) (*http.Client, error) {
  1323. dialer := ss.conn.srv.lb.Dialer()
  1324. if dialer == nil {
  1325. return nil, errors.New("no peer API transport")
  1326. }
  1327. tr := dialer.PeerAPITransport().Clone()
  1328. dialContextFn := tr.DialContext
  1329. tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
  1330. perAttemptCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
  1331. defer cancel()
  1332. go func() {
  1333. select {
  1334. case <-perAttemptCtx.Done():
  1335. case <-dialCtx.Done():
  1336. cancel()
  1337. }
  1338. }()
  1339. return dialContextFn(perAttemptCtx, network, addr)
  1340. }
  1341. return &http.Client{
  1342. Transport: tr,
  1343. }, nil
  1344. }
  1345. // connectToRecorder connects to the recorder at any of the provided addresses.
  1346. // It returns the first successful response, or a multierr if all attempts fail.
  1347. //
  1348. // On success, it returns a WriteCloser that can be used to upload the
  1349. // recording, and a channel that will be sent an error (or nil) when the upload
  1350. // fails or completes.
  1351. //
  1352. // In both cases, a slice of SSHRecordingAttempts is returned which detail the
  1353. // attempted recorder IP and the error message, if the attempt failed. The
  1354. // attempts are in order the recorder(s) was attempted. If successful a
  1355. // successful connection is made, the last attempt in the slice is the
  1356. // attempt for connected recorder.
  1357. func (ss *sshSession) connectToRecorder(ctx context.Context, recs []netip.AddrPort) (io.WriteCloser, []*tailcfg.SSHRecordingAttempt, <-chan error, error) {
  1358. if len(recs) == 0 {
  1359. return nil, nil, nil, errors.New("no recorders configured")
  1360. }
  1361. // We use a special context for dialing the recorder, so that we can
  1362. // limit the time we spend dialing to 30 seconds and still have an
  1363. // unbounded context for the upload.
  1364. dialCtx, dialCancel := context.WithTimeout(ctx, 30*time.Second)
  1365. defer dialCancel()
  1366. hc, err := ss.sessionRecordingClient(dialCtx)
  1367. if err != nil {
  1368. return nil, nil, nil, err
  1369. }
  1370. var errs []error
  1371. var attempts []*tailcfg.SSHRecordingAttempt
  1372. for _, ap := range recs {
  1373. attempt := &tailcfg.SSHRecordingAttempt{
  1374. Recorder: ap,
  1375. }
  1376. attempts = append(attempts, attempt)
  1377. // We dial the recorder and wait for it to send a 100-continue
  1378. // response before returning from this function. This ensures that
  1379. // the recorder is ready to accept the recording.
  1380. // got100 is closed when we receive the 100-continue response.
  1381. got100 := make(chan struct{})
  1382. ctx = httptrace.WithClientTrace(ctx, &httptrace.ClientTrace{
  1383. Got100Continue: func() {
  1384. close(got100)
  1385. },
  1386. })
  1387. pr, pw := io.Pipe()
  1388. req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("http://%s:%d/record", ap.Addr(), ap.Port()), pr)
  1389. if err != nil {
  1390. err = fmt.Errorf("recording: error starting recording: %w", err)
  1391. attempt.FailureMessage = err.Error()
  1392. errs = append(errs, err)
  1393. continue
  1394. }
  1395. // We set the Expect header to 100-continue, so that the recorder
  1396. // will send a 100-continue response before it starts reading the
  1397. // request body.
  1398. req.Header.Set("Expect", "100-continue")
  1399. // errChan is used to indicate the result of the request.
  1400. errChan := make(chan error, 1)
  1401. go func() {
  1402. resp, err := hc.Do(req)
  1403. if err != nil {
  1404. errChan <- fmt.Errorf("recording: error starting recording: %w", err)
  1405. return
  1406. }
  1407. if resp.StatusCode != 200 {
  1408. errChan <- fmt.Errorf("recording: unexpected status: %v", resp.Status)
  1409. return
  1410. }
  1411. errChan <- nil
  1412. }()
  1413. select {
  1414. case <-got100:
  1415. case err := <-errChan:
  1416. // If we get an error before we get the 100-continue response,
  1417. // we need to try another recorder.
  1418. if err == nil {
  1419. // If the error is nil, we got a 200 response, which
  1420. // is unexpected as we haven't sent any data yet.
  1421. err = errors.New("recording: unexpected EOF")
  1422. }
  1423. attempt.FailureMessage = err.Error()
  1424. errs = append(errs, err)
  1425. continue
  1426. }
  1427. return pw, attempts, errChan, nil
  1428. }
  1429. return nil, attempts, nil, multierr.New(errs...)
  1430. }
  1431. func (ss *sshSession) openFileForRecording(now time.Time) (_ io.WriteCloser, err error) {
  1432. varRoot := ss.conn.srv.lb.TailscaleVarRoot()
  1433. if varRoot == "" {
  1434. return nil, errors.New("no var root for recording storage")
  1435. }
  1436. dir := filepath.Join(varRoot, "ssh-sessions")
  1437. if err := os.MkdirAll(dir, 0700); err != nil {
  1438. return nil, err
  1439. }
  1440. f, err := os.CreateTemp(dir, fmt.Sprintf("ssh-session-%v-*.cast", now.UnixNano()))
  1441. if err != nil {
  1442. return nil, err
  1443. }
  1444. return f, nil
  1445. }
  1446. // startNewRecording starts a new SSH session recording.
  1447. // It may return a nil recording if recording is not available.
  1448. func (ss *sshSession) startNewRecording() (_ *recording, err error) {
  1449. // We store the node key as soon as possible when creating
  1450. // a new recording incase of FUS.
  1451. nodeKey := ss.conn.srv.lb.NodeKey()
  1452. if nodeKey.IsZero() {
  1453. return nil, errors.New("ssh server is unavailable: no node key")
  1454. }
  1455. recorders, onFailure := ss.recorders()
  1456. var localRecording bool
  1457. if len(recorders) == 0 {
  1458. if recordSSHToLocalDisk() {
  1459. localRecording = true
  1460. } else {
  1461. return nil, errors.New("no recorders configured")
  1462. }
  1463. }
  1464. var w ssh.Window
  1465. if ptyReq, _, isPtyReq := ss.Pty(); isPtyReq {
  1466. w = ptyReq.Window
  1467. }
  1468. term := envValFromList(ss.Environ(), "TERM")
  1469. if term == "" {
  1470. term = "xterm-256color" // something non-empty
  1471. }
  1472. now := time.Now()
  1473. rec := &recording{
  1474. ss: ss,
  1475. start: now,
  1476. failOpen: onFailure == nil || onFailure.TerminateSessionWithMessage == "",
  1477. }
  1478. // We want to use a background context for uploading and not ss.ctx.
  1479. // ss.ctx is closed when the session closes, but we don't want to break the upload at that time.
  1480. // Instead we want to wait for the session to close the writer when it finishes.
  1481. ctx := context.Background()
  1482. if localRecording {
  1483. rec.out, err = ss.openFileForRecording(now)
  1484. if err != nil {
  1485. return nil, err
  1486. }
  1487. } else {
  1488. var errChan <-chan error
  1489. var attempts []*tailcfg.SSHRecordingAttempt
  1490. rec.out, attempts, errChan, err = ss.connectToRecorder(ctx, recorders)
  1491. if err != nil {
  1492. if onFailure != nil && onFailure.NotifyURL != "" && len(attempts) > 0 {
  1493. eventType := tailcfg.SSHSessionRecordingFailed
  1494. if onFailure.RejectSessionWithMessage != "" {
  1495. eventType = tailcfg.SSHSessionRecordingRejected
  1496. }
  1497. ss.notifyControl(ctx, nodeKey, eventType, attempts, onFailure.NotifyURL)
  1498. }
  1499. if onFailure != nil && onFailure.RejectSessionWithMessage != "" {
  1500. ss.logf("recording: error starting recording (rejecting session): %v", err)
  1501. return nil, userVisibleError{
  1502. error: err,
  1503. msg: onFailure.RejectSessionWithMessage,
  1504. }
  1505. }
  1506. ss.logf("recording: error starting recording (failing open): %v", err)
  1507. return nil, nil
  1508. }
  1509. go func() {
  1510. err := <-errChan
  1511. if err == nil {
  1512. // Success.
  1513. ss.logf("recording: finished uploading recording")
  1514. return
  1515. }
  1516. if onFailure != nil && onFailure.NotifyURL != "" && len(attempts) > 0 {
  1517. lastAttempt := attempts[len(attempts)-1]
  1518. lastAttempt.FailureMessage = err.Error()
  1519. eventType := tailcfg.SSHSessionRecordingFailed
  1520. if onFailure.TerminateSessionWithMessage != "" {
  1521. eventType = tailcfg.SSHSessionRecordingTerminated
  1522. }
  1523. ss.notifyControl(ctx, nodeKey, eventType, attempts, onFailure.NotifyURL)
  1524. }
  1525. if onFailure != nil && onFailure.TerminateSessionWithMessage != "" {
  1526. ss.logf("recording: error uploading recording (closing session): %v", err)
  1527. ss.cancelCtx(userVisibleError{
  1528. error: err,
  1529. msg: onFailure.TerminateSessionWithMessage,
  1530. })
  1531. return
  1532. }
  1533. ss.logf("recording: error uploading recording (failing open): %v", err)
  1534. }()
  1535. }
  1536. ch := CastHeader{
  1537. Version: 2,
  1538. Width: w.Width,
  1539. Height: w.Height,
  1540. Timestamp: now.Unix(),
  1541. Command: strings.Join(ss.Command(), " "),
  1542. Env: map[string]string{
  1543. "TERM": term,
  1544. // TODO(bradfitz): anything else important?
  1545. // including all seems noisey, but maybe we should
  1546. // for auditing. But first need to break
  1547. // launchProcess's startWithStdPipes and
  1548. // startWithPTY up so that they first return the cmd
  1549. // without starting it, and then a step that starts
  1550. // it. Then we can (1) make the cmd, (2) start the
  1551. // recording, (3) start the process.
  1552. },
  1553. SSHUser: ss.conn.info.sshUser,
  1554. LocalUser: ss.conn.localUser.Username,
  1555. SrcNode: strings.TrimSuffix(ss.conn.info.node.Name(), "."),
  1556. SrcNodeID: ss.conn.info.node.StableID(),
  1557. ConnectionID: ss.conn.connID,
  1558. }
  1559. if !ss.conn.info.node.IsTagged() {
  1560. ch.SrcNodeUser = ss.conn.info.uprof.LoginName
  1561. ch.SrcNodeUserID = ss.conn.info.node.User()
  1562. } else {
  1563. ch.SrcNodeTags = ss.conn.info.node.Tags().AsSlice()
  1564. }
  1565. j, err := json.Marshal(ch)
  1566. if err != nil {
  1567. return nil, err
  1568. }
  1569. j = append(j, '\n')
  1570. if _, err := rec.out.Write(j); err != nil {
  1571. if errors.Is(err, io.ErrClosedPipe) && ss.ctx.Err() != nil {
  1572. // If we got an io.ErrClosedPipe, it's likely because
  1573. // the recording server closed the connection on us. Return
  1574. // the original context error instead.
  1575. return nil, context.Cause(ss.ctx)
  1576. }
  1577. return nil, err
  1578. }
  1579. return rec, nil
  1580. }
  1581. // notifyControl sends a SSHEventNotifyRequest to control over noise.
  1582. // A SSHEventNotifyRequest is sent when an action or state reached during
  1583. // an SSH session is a defined EventType.
  1584. func (ss *sshSession) notifyControl(ctx context.Context, nodeKey key.NodePublic, notifyType tailcfg.SSHEventType, attempts []*tailcfg.SSHRecordingAttempt, url string) {
  1585. re := tailcfg.SSHEventNotifyRequest{
  1586. EventType: notifyType,
  1587. ConnectionID: ss.conn.connID,
  1588. CapVersion: tailcfg.CurrentCapabilityVersion,
  1589. NodeKey: nodeKey,
  1590. SrcNode: ss.conn.info.node.ID(),
  1591. SSHUser: ss.conn.info.sshUser,
  1592. LocalUser: ss.conn.localUser.Username,
  1593. RecordingAttempts: attempts,
  1594. }
  1595. body, err := json.Marshal(re)
  1596. if err != nil {
  1597. ss.logf("notifyControl: unable to marshal SSHNotifyRequest:", err)
  1598. return
  1599. }
  1600. req, err := http.NewRequestWithContext(ctx, httpm.POST, url, bytes.NewReader(body))
  1601. if err != nil {
  1602. ss.logf("notifyControl: unable to create request:", err)
  1603. return
  1604. }
  1605. resp, err := ss.conn.srv.lb.DoNoiseRequest(req)
  1606. if err != nil {
  1607. ss.logf("notifyControl: unable to send noise request:", err)
  1608. return
  1609. }
  1610. if resp.StatusCode != http.StatusCreated {
  1611. ss.logf("notifyControl: noise request returned status code %v", resp.StatusCode)
  1612. return
  1613. }
  1614. }
  1615. // recording is the state for an SSH session recording.
  1616. type recording struct {
  1617. ss *sshSession
  1618. start time.Time
  1619. // failOpen specifies whether the session should be allowed to
  1620. // continue if writing to the recording fails.
  1621. failOpen bool
  1622. mu sync.Mutex // guards writes to, close of out
  1623. out io.WriteCloser
  1624. }
  1625. func (r *recording) Close() error {
  1626. r.mu.Lock()
  1627. defer r.mu.Unlock()
  1628. if r.out == nil {
  1629. return nil
  1630. }
  1631. err := r.out.Close()
  1632. r.out = nil
  1633. return err
  1634. }
  1635. // writer returns an io.Writer around w that first records the write.
  1636. //
  1637. // The dir should be "i" for input or "o" for output.
  1638. //
  1639. // If r is nil, it returns w unchanged.
  1640. //
  1641. // Currently (2023-03-21) we only record output, not input.
  1642. func (r *recording) writer(dir string, w io.Writer) io.Writer {
  1643. if r == nil {
  1644. return w
  1645. }
  1646. if dir == "i" {
  1647. // TODO: record input? Maybe not, since it might contain
  1648. // passwords.
  1649. return w
  1650. }
  1651. return &loggingWriter{r: r, dir: dir, w: w}
  1652. }
  1653. // loggingWriter is an io.Writer wrapper that writes first an
  1654. // asciinema JSON cast format recording line, and then writes to w.
  1655. type loggingWriter struct {
  1656. r *recording
  1657. dir string // "i" or "o" (input or output)
  1658. w io.Writer // underlying Writer, after writing to r.out
  1659. // recordingFailedOpen specifies whether we've failed to write to
  1660. // r.out and should stop trying. It is set to true if we fail to write
  1661. // to r.out and r.failOpen is set.
  1662. recordingFailedOpen bool
  1663. }
  1664. func (w *loggingWriter) Write(p []byte) (n int, err error) {
  1665. if !w.recordingFailedOpen {
  1666. j, err := json.Marshal([]any{
  1667. time.Since(w.r.start).Seconds(),
  1668. w.dir,
  1669. string(p),
  1670. })
  1671. if err != nil {
  1672. return 0, err
  1673. }
  1674. j = append(j, '\n')
  1675. if err := w.writeCastLine(j); err != nil {
  1676. if !w.r.failOpen {
  1677. return 0, err
  1678. }
  1679. w.recordingFailedOpen = true
  1680. }
  1681. }
  1682. return w.w.Write(p)
  1683. }
  1684. func (w loggingWriter) writeCastLine(j []byte) error {
  1685. w.r.mu.Lock()
  1686. defer w.r.mu.Unlock()
  1687. if w.r.out == nil {
  1688. return errors.New("logger closed")
  1689. }
  1690. _, err := w.r.out.Write(j)
  1691. if err != nil {
  1692. return fmt.Errorf("logger Write: %w", err)
  1693. }
  1694. return nil
  1695. }
  1696. func envValFromList(env []string, wantKey string) (v string) {
  1697. for _, kv := range env {
  1698. if thisKey, v, ok := strings.Cut(kv, "="); ok && envEq(thisKey, wantKey) {
  1699. return v
  1700. }
  1701. }
  1702. return ""
  1703. }
  1704. // envEq reports whether environment variable a == b for the current
  1705. // operating system.
  1706. func envEq(a, b string) bool {
  1707. if runtime.GOOS == "windows" {
  1708. return strings.EqualFold(a, b)
  1709. }
  1710. return a == b
  1711. }
  1712. var (
  1713. metricActiveSessions = clientmetric.NewGauge("ssh_active_sessions")
  1714. metricIncomingConnections = clientmetric.NewCounter("ssh_incoming_connections")
  1715. metricPublicKeyConnections = clientmetric.NewCounter("ssh_publickey_connections") // total
  1716. metricPublicKeyAccepts = clientmetric.NewCounter("ssh_publickey_accepts") // accepted subset of ssh_publickey_connections
  1717. metricTerminalAccept = clientmetric.NewCounter("ssh_terminalaction_accept")
  1718. metricTerminalReject = clientmetric.NewCounter("ssh_terminalaction_reject")
  1719. metricTerminalInterrupt = clientmetric.NewCounter("ssh_terminalaction_interrupt")
  1720. metricTerminalMalformed = clientmetric.NewCounter("ssh_terminalaction_malformed")
  1721. metricTerminalFetchError = clientmetric.NewCounter("ssh_terminalaction_fetch_error")
  1722. metricHolds = clientmetric.NewCounter("ssh_holds")
  1723. metricPolicyChangeKick = clientmetric.NewCounter("ssh_policy_change_kick")
  1724. metricSFTP = clientmetric.NewCounter("ssh_sftp_requests")
  1725. metricLocalPortForward = clientmetric.NewCounter("ssh_local_port_forward_requests")
  1726. metricRemotePortForward = clientmetric.NewCounter("ssh_remote_port_forward_requests")
  1727. )
  1728. // userVisibleError is a wrapper around an error that implements
  1729. // SSHTerminationError, so msg is written to their session.
  1730. type userVisibleError struct {
  1731. msg string
  1732. error
  1733. }
  1734. func (ue userVisibleError) SSHTerminationMessage() string { return ue.msg }
  1735. // SSHTerminationError is implemented by errors that terminate an SSH
  1736. // session and should be written to user's sessions.
  1737. type SSHTerminationError interface {
  1738. error
  1739. SSHTerminationMessage() string
  1740. }
  1741. func closeAll(cs ...io.Closer) {
  1742. for _, c := range cs {
  1743. if c != nil {
  1744. c.Close()
  1745. }
  1746. }
  1747. }