tailssh.go 57 KB

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