server.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. // Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ipnserver
  5. import (
  6. "bufio"
  7. "bytes"
  8. "context"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "io"
  13. "io/ioutil"
  14. "log"
  15. "net"
  16. "net/http"
  17. "os"
  18. "os/exec"
  19. "os/signal"
  20. "os/user"
  21. "runtime"
  22. "strconv"
  23. "strings"
  24. "sync"
  25. "syscall"
  26. "time"
  27. "go4.org/mem"
  28. "inet.af/netaddr"
  29. "inet.af/peercred"
  30. "tailscale.com/control/controlclient"
  31. "tailscale.com/ipn"
  32. "tailscale.com/ipn/ipnlocal"
  33. "tailscale.com/ipn/localapi"
  34. "tailscale.com/ipn/store/aws"
  35. "tailscale.com/log/filelogger"
  36. "tailscale.com/logtail/backoff"
  37. "tailscale.com/net/netstat"
  38. "tailscale.com/paths"
  39. "tailscale.com/safesocket"
  40. "tailscale.com/smallzstd"
  41. "tailscale.com/types/logger"
  42. "tailscale.com/util/groupmember"
  43. "tailscale.com/util/pidowner"
  44. "tailscale.com/util/systemd"
  45. "tailscale.com/version"
  46. "tailscale.com/version/distro"
  47. "tailscale.com/wgengine"
  48. )
  49. // Options is the configuration of the Tailscale node agent.
  50. type Options struct {
  51. // VarRoot is the the Tailscale daemon's private writable
  52. // directory (usually "/var/lib/tailscale" on Linux) that
  53. // contains the "tailscaled.state" file, the "certs" directory
  54. // for TLS certs, and the "files" directory for incoming
  55. // Taildrop files before they're moved to a user directory.
  56. // If empty, Taildrop and TLS certs don't function.
  57. VarRoot string
  58. // AutostartStateKey, if non-empty, immediately starts the agent
  59. // using the given StateKey. If empty, the agent stays idle and
  60. // waits for a frontend to start it.
  61. AutostartStateKey ipn.StateKey
  62. // SurviveDisconnects specifies how the server reacts to its
  63. // frontend disconnecting. If true, the server keeps running on
  64. // its existing state, and accepts new frontend connections. If
  65. // false, the server dumps its state and becomes idle.
  66. //
  67. // This is effectively whether the platform is in "server
  68. // mode" by default. On Linux, it's true; on Windows, it's
  69. // false. But on some platforms (currently only Windows), the
  70. // "server mode" can be overridden at runtime with a change in
  71. // Prefs.ForceDaemon/WantRunning.
  72. //
  73. // To support CLI connections (notably, "tailscale status"),
  74. // the actual definition of "disconnect" is when the
  75. // connection count transitions from 1 to 0.
  76. SurviveDisconnects bool
  77. }
  78. // Server is an IPN backend and its set of 0 or more active localhost
  79. // TCP or unix socket connections talking to that backend.
  80. type Server struct {
  81. b *ipnlocal.LocalBackend
  82. logf logger.Logf
  83. backendLogID string
  84. // resetOnZero is whether to call bs.Reset on transition from
  85. // 1->0 connections. That is, this is whether the backend is
  86. // being run in "client mode" that requires an active GUI
  87. // connection (such as on Windows by default). Even if this
  88. // is true, the ForceDaemon pref can override this.
  89. resetOnZero bool
  90. autostartStateKey ipn.StateKey
  91. bsMu sync.Mutex // lock order: bsMu, then mu
  92. bs *ipn.BackendServer
  93. mu sync.Mutex
  94. serverModeUser *user.User // or nil if not in server mode
  95. lastUserID string // tracks last userid; on change, Reset state for paranoia
  96. allClients map[net.Conn]connIdentity // HTTP or IPN
  97. clients map[net.Conn]bool // subset of allClients; only IPN protocol
  98. disconnectSub map[chan<- struct{}]struct{} // keys are subscribers of disconnects
  99. }
  100. // LocalBackend returns the server's LocalBackend.
  101. func (s *Server) LocalBackend() *ipnlocal.LocalBackend { return s.b }
  102. // connIdentity represents the owner of a localhost TCP or unix socket connection.
  103. type connIdentity struct {
  104. Conn net.Conn
  105. NotWindows bool // runtime.GOOS != "windows"
  106. // Fields used when NotWindows:
  107. IsUnixSock bool // Conn is a *net.UnixConn
  108. Creds *peercred.Creds // or nil
  109. // Used on Windows:
  110. // TODO(bradfitz): merge these into the peercreds package and
  111. // use that for all.
  112. Pid int
  113. UserID string
  114. User *user.User
  115. }
  116. // getConnIdentity returns the localhost TCP connection's identity information
  117. // (pid, userid, user). If it's not Windows (for now), it returns a nil error
  118. // and a ConnIdentity with NotWindows set true. It's only an error if we expected
  119. // to be able to map it and couldn't.
  120. func (s *Server) getConnIdentity(c net.Conn) (ci connIdentity, err error) {
  121. ci = connIdentity{Conn: c}
  122. if runtime.GOOS != "windows" { // for now; TODO: expand to other OSes
  123. ci.NotWindows = true
  124. _, ci.IsUnixSock = c.(*net.UnixConn)
  125. ci.Creds, _ = peercred.Get(c)
  126. return ci, nil
  127. }
  128. la, err := netaddr.ParseIPPort(c.LocalAddr().String())
  129. if err != nil {
  130. return ci, fmt.Errorf("parsing local address: %w", err)
  131. }
  132. ra, err := netaddr.ParseIPPort(c.RemoteAddr().String())
  133. if err != nil {
  134. return ci, fmt.Errorf("parsing local remote: %w", err)
  135. }
  136. if !la.IP().IsLoopback() || !ra.IP().IsLoopback() {
  137. return ci, errors.New("non-loopback connection")
  138. }
  139. tab, err := netstat.Get()
  140. if err != nil {
  141. return ci, fmt.Errorf("failed to get local connection table: %w", err)
  142. }
  143. pid := peerPid(tab.Entries, la, ra)
  144. if pid == 0 {
  145. return ci, errors.New("no local process found matching localhost connection")
  146. }
  147. ci.Pid = pid
  148. uid, err := pidowner.OwnerOfPID(pid)
  149. if err != nil {
  150. var hint string
  151. if runtime.GOOS == "windows" {
  152. hint = " (WSL?)"
  153. }
  154. return ci, fmt.Errorf("failed to map connection's pid to a user%s: %w", hint, err)
  155. }
  156. ci.UserID = uid
  157. u, err := lookupUserFromID(s.logf, uid)
  158. if err != nil {
  159. return ci, fmt.Errorf("failed to look up user from userid: %w", err)
  160. }
  161. ci.User = u
  162. return ci, nil
  163. }
  164. func lookupUserFromID(logf logger.Logf, uid string) (*user.User, error) {
  165. u, err := user.LookupId(uid)
  166. if err != nil && runtime.GOOS == "windows" && errors.Is(err, syscall.Errno(0x534)) {
  167. logf("[warning] issue 869: os/user.LookupId failed; ignoring")
  168. // Work around https://github.com/tailscale/tailscale/issues/869 for
  169. // now. We don't strictly need the username. It's just a nice-to-have.
  170. // So make up a *user.User if their machine is broken in this way.
  171. return &user.User{
  172. Uid: uid,
  173. Username: "unknown-user-" + uid,
  174. Name: "unknown user " + uid,
  175. }, nil
  176. }
  177. return u, err
  178. }
  179. // blockWhileInUse blocks while until either a Read from conn fails
  180. // (i.e. it's closed) or until the server is able to accept ci as a
  181. // user.
  182. func (s *Server) blockWhileInUse(conn io.Reader, ci connIdentity) {
  183. s.logf("blocking client while server in use; connIdentity=%v", ci)
  184. connDone := make(chan struct{})
  185. go func() {
  186. io.Copy(ioutil.Discard, conn)
  187. close(connDone)
  188. }()
  189. ch := make(chan struct{}, 1)
  190. s.registerDisconnectSub(ch, true)
  191. defer s.registerDisconnectSub(ch, false)
  192. for {
  193. select {
  194. case <-connDone:
  195. s.logf("blocked client Read completed; connIdentity=%v", ci)
  196. return
  197. case <-ch:
  198. s.mu.Lock()
  199. err := s.checkConnIdentityLocked(ci)
  200. s.mu.Unlock()
  201. if err == nil {
  202. s.logf("unblocking client, server is free; connIdentity=%v", ci)
  203. // Server is now available again for a new user.
  204. // TODO(bradfitz): keep this connection alive. But for
  205. // now just return and have our caller close the connection
  206. // (which unblocks the io.Copy goroutine we started above)
  207. // and then the client (e.g. Windows) will reconnect and
  208. // discover that it works.
  209. return
  210. }
  211. }
  212. }
  213. }
  214. // bufferHasHTTPRequest reports whether br looks like it has an HTTP
  215. // request in it, without reading any bytes from it.
  216. func bufferHasHTTPRequest(br *bufio.Reader) bool {
  217. peek, _ := br.Peek(br.Buffered())
  218. return mem.HasPrefix(mem.B(peek), mem.S("GET ")) ||
  219. mem.HasPrefix(mem.B(peek), mem.S("POST ")) ||
  220. mem.Contains(mem.B(peek), mem.S(" HTTP/"))
  221. }
  222. func (s *Server) serveConn(ctx context.Context, c net.Conn, logf logger.Logf) {
  223. // First see if it's an HTTP request.
  224. br := bufio.NewReader(c)
  225. c.SetReadDeadline(time.Now().Add(time.Second))
  226. br.Peek(4)
  227. c.SetReadDeadline(time.Time{})
  228. isHTTPReq := bufferHasHTTPRequest(br)
  229. ci, err := s.addConn(c, isHTTPReq)
  230. if err != nil {
  231. if isHTTPReq {
  232. fmt.Fprintf(c, "HTTP/1.0 500 Nope\r\nContent-Type: text/plain\r\nX-Content-Type-Options: nosniff\r\n\r\n%s\n", err.Error())
  233. c.Close()
  234. return
  235. }
  236. defer c.Close()
  237. bs := ipn.NewBackendServer(logf, nil, jsonNotifier(c, s.logf))
  238. _, occupied := err.(inUseOtherUserError)
  239. if occupied {
  240. bs.SendInUseOtherUserErrorMessage(err.Error())
  241. s.blockWhileInUse(c, ci)
  242. } else {
  243. bs.SendErrorMessage(err.Error())
  244. time.Sleep(time.Second)
  245. }
  246. return
  247. }
  248. // Tell the LocalBackend about the identity we're now running as.
  249. s.b.SetCurrentUserID(ci.UserID)
  250. if isHTTPReq {
  251. httpServer := &http.Server{
  252. // Localhost connections are cheap; so only do
  253. // keep-alives for a short period of time, as these
  254. // active connections lock the server into only serving
  255. // that user. If the user has this page open, we don't
  256. // want another switching user to be locked out for
  257. // minutes. 5 seconds is enough to let browser hit
  258. // favicon.ico and such.
  259. IdleTimeout: 5 * time.Second,
  260. ErrorLog: logger.StdLogger(logf),
  261. Handler: s.localhostHandler(ci),
  262. }
  263. httpServer.Serve(&oneConnListener{&protoSwitchConn{s: s, br: br, Conn: c}})
  264. return
  265. }
  266. defer s.removeAndCloseConn(c)
  267. logf("[v1] incoming control connection")
  268. if isReadonlyConn(ci, s.b.OperatorUserID(), logf) {
  269. ctx = ipn.ReadonlyContextOf(ctx)
  270. }
  271. for ctx.Err() == nil {
  272. msg, err := ipn.ReadMsg(br)
  273. if err != nil {
  274. if errors.Is(err, io.EOF) {
  275. logf("[v1] ReadMsg: %v", err)
  276. } else if ctx.Err() == nil {
  277. logf("ReadMsg: %v", err)
  278. }
  279. return
  280. }
  281. s.bsMu.Lock()
  282. if err := s.bs.GotCommandMsg(ctx, msg); err != nil {
  283. logf("GotCommandMsg: %v", err)
  284. }
  285. gotQuit := s.bs.GotQuit
  286. s.bsMu.Unlock()
  287. if gotQuit {
  288. return
  289. }
  290. }
  291. }
  292. func isReadonlyConn(ci connIdentity, operatorUID string, logf logger.Logf) bool {
  293. if runtime.GOOS == "windows" {
  294. // Windows doesn't need/use this mechanism, at least yet. It
  295. // has a different last-user-wins auth model.
  296. return false
  297. }
  298. const ro = true
  299. const rw = false
  300. if !safesocket.PlatformUsesPeerCreds() {
  301. return rw
  302. }
  303. creds := ci.Creds
  304. if creds == nil {
  305. logf("connection from unknown peer; read-only")
  306. return ro
  307. }
  308. uid, ok := creds.UserID()
  309. if !ok {
  310. logf("connection from peer with unknown userid; read-only")
  311. return ro
  312. }
  313. if uid == "0" {
  314. logf("connection from userid %v; root has access", uid)
  315. return rw
  316. }
  317. if selfUID := os.Getuid(); selfUID != 0 && uid == strconv.Itoa(selfUID) {
  318. logf("connection from userid %v; connection from non-root user matching daemon has access", uid)
  319. return rw
  320. }
  321. if operatorUID != "" && uid == operatorUID {
  322. logf("connection from userid %v; is configured operator", uid)
  323. return rw
  324. }
  325. if yes, err := isLocalAdmin(uid); err != nil {
  326. logf("connection from userid %v; read-only; %v", uid, err)
  327. return ro
  328. } else if yes {
  329. logf("connection from userid %v; is local admin, has access", uid)
  330. return rw
  331. }
  332. logf("connection from userid %v; read-only", uid)
  333. return ro
  334. }
  335. func isLocalAdmin(uid string) (bool, error) {
  336. u, err := user.LookupId(uid)
  337. if err != nil {
  338. return false, err
  339. }
  340. var adminGroup string
  341. switch {
  342. case runtime.GOOS == "darwin":
  343. adminGroup = "admin"
  344. case distro.Get() == distro.QNAP:
  345. adminGroup = "administrators"
  346. default:
  347. return false, fmt.Errorf("no system admin group found")
  348. }
  349. return groupmember.IsMemberOfGroup(adminGroup, u.Username)
  350. }
  351. // inUseOtherUserError is the error type for when the server is in use
  352. // by a different local user.
  353. type inUseOtherUserError struct{ error }
  354. func (e inUseOtherUserError) Unwrap() error { return e.error }
  355. // checkConnIdentityLocked checks whether the provided identity is
  356. // allowed to connect to the server.
  357. //
  358. // The returned error, when non-nil, will be of type inUseOtherUserError.
  359. //
  360. // s.mu must be held.
  361. func (s *Server) checkConnIdentityLocked(ci connIdentity) error {
  362. // If clients are already connected, verify they're the same user.
  363. // This mostly matters on Windows at the moment.
  364. if len(s.allClients) > 0 {
  365. var active connIdentity
  366. for _, active = range s.allClients {
  367. break
  368. }
  369. if ci.UserID != active.UserID {
  370. return inUseOtherUserError{fmt.Errorf("Tailscale already in use by %s, pid %d", active.User.Username, active.Pid)}
  371. }
  372. }
  373. if su := s.serverModeUser; su != nil && ci.UserID != su.Uid {
  374. return inUseOtherUserError{fmt.Errorf("Tailscale already in use by %s", su.Username)}
  375. }
  376. return nil
  377. }
  378. // localAPIPermissions returns the permissions for the given identity accessing
  379. // the Tailscale local daemon API.
  380. //
  381. // s.mu must not be held.
  382. func (s *Server) localAPIPermissions(ci connIdentity) (read, write bool) {
  383. switch runtime.GOOS {
  384. case "windows":
  385. s.mu.Lock()
  386. defer s.mu.Unlock()
  387. if s.checkConnIdentityLocked(ci) == nil {
  388. return true, true
  389. }
  390. return false, false
  391. case "js":
  392. return true, true
  393. }
  394. if ci.IsUnixSock {
  395. return true, !isReadonlyConn(ci, s.b.OperatorUserID(), logger.Discard)
  396. }
  397. return false, false
  398. }
  399. // registerDisconnectSub adds ch as a subscribe to connection disconnect
  400. // events. If add is false, the subscriber is removed.
  401. func (s *Server) registerDisconnectSub(ch chan<- struct{}, add bool) {
  402. s.mu.Lock()
  403. defer s.mu.Unlock()
  404. if add {
  405. if s.disconnectSub == nil {
  406. s.disconnectSub = make(map[chan<- struct{}]struct{})
  407. }
  408. s.disconnectSub[ch] = struct{}{}
  409. } else {
  410. delete(s.disconnectSub, ch)
  411. }
  412. }
  413. // addConn adds c to the server's list of clients.
  414. //
  415. // If the returned error is of type inUseOtherUserError then the
  416. // returned connIdentity is also valid.
  417. func (s *Server) addConn(c net.Conn, isHTTP bool) (ci connIdentity, err error) {
  418. ci, err = s.getConnIdentity(c)
  419. if err != nil {
  420. return
  421. }
  422. // If the connected user changes, reset the backend server state to make
  423. // sure node keys don't leak between users.
  424. var doReset bool
  425. defer func() {
  426. if doReset {
  427. s.logf("identity changed; resetting server")
  428. s.b.ResetForClientDisconnect()
  429. }
  430. }()
  431. s.mu.Lock()
  432. defer s.mu.Unlock()
  433. if s.clients == nil {
  434. s.clients = map[net.Conn]bool{}
  435. }
  436. if s.allClients == nil {
  437. s.allClients = map[net.Conn]connIdentity{}
  438. }
  439. if err := s.checkConnIdentityLocked(ci); err != nil {
  440. return ci, err
  441. }
  442. if !isHTTP {
  443. s.clients[c] = true
  444. }
  445. s.allClients[c] = ci
  446. if s.lastUserID != ci.UserID {
  447. if s.lastUserID != "" {
  448. doReset = true
  449. }
  450. s.lastUserID = ci.UserID
  451. }
  452. return ci, nil
  453. }
  454. func (s *Server) removeAndCloseConn(c net.Conn) {
  455. s.mu.Lock()
  456. delete(s.clients, c)
  457. delete(s.allClients, c)
  458. remain := len(s.allClients)
  459. for sub := range s.disconnectSub {
  460. select {
  461. case sub <- struct{}{}:
  462. default:
  463. }
  464. }
  465. s.mu.Unlock()
  466. if remain == 0 && s.resetOnZero {
  467. if s.b.InServerMode() {
  468. s.logf("client disconnected; staying alive in server mode")
  469. } else {
  470. s.logf("client disconnected; stopping server")
  471. s.b.ResetForClientDisconnect()
  472. }
  473. }
  474. c.Close()
  475. }
  476. func (s *Server) stopAll() {
  477. s.mu.Lock()
  478. defer s.mu.Unlock()
  479. for c := range s.clients {
  480. safesocket.ConnCloseRead(c)
  481. safesocket.ConnCloseWrite(c)
  482. }
  483. s.clients = nil
  484. }
  485. // setServerModeUserLocked is called when we're in server mode but our s.serverModeUser is nil.
  486. //
  487. // s.mu must be held
  488. func (s *Server) setServerModeUserLocked() {
  489. var ci connIdentity
  490. var ok bool
  491. for _, ci = range s.allClients {
  492. ok = true
  493. break
  494. }
  495. if !ok {
  496. s.logf("ipnserver: [unexpected] now in server mode, but no connected client")
  497. return
  498. }
  499. if ci.NotWindows {
  500. return
  501. }
  502. if ci.User != nil {
  503. s.logf("ipnserver: now in server mode; user=%v", ci.User.Username)
  504. s.serverModeUser = ci.User
  505. } else {
  506. s.logf("ipnserver: [unexpected] now in server mode, but nil User")
  507. }
  508. }
  509. var jsonEscapedZero = []byte(`\u0000`)
  510. func (s *Server) writeToClients(n ipn.Notify) {
  511. inServerMode := s.b.InServerMode()
  512. s.mu.Lock()
  513. defer s.mu.Unlock()
  514. if inServerMode {
  515. if s.serverModeUser == nil {
  516. s.setServerModeUserLocked()
  517. }
  518. } else {
  519. if s.serverModeUser != nil {
  520. s.logf("ipnserver: no longer in server mode")
  521. s.serverModeUser = nil
  522. }
  523. }
  524. if len(s.clients) == 0 {
  525. // Common case (at least on busy servers): nobody
  526. // connected (no GUI, etc), so return before
  527. // serializing JSON.
  528. return
  529. }
  530. if b, ok := marshalNotify(n, s.logf); ok {
  531. for c := range s.clients {
  532. ipn.WriteMsg(c, b)
  533. }
  534. }
  535. }
  536. // tryWindowsAppDataMigration attempts to copy the Windows state file
  537. // from its old location to the new location. (Issue 2856)
  538. //
  539. // Tailscale 1.14 and before stored state under %LocalAppData%
  540. // (usually "C:\WINDOWS\system32\config\systemprofile\AppData\Local"
  541. // when tailscaled.exe is running as a non-user system service).
  542. // However it is frequently cleared for almost any reason: Windows
  543. // updates, System Restore, even various System Cleaner utilities.
  544. //
  545. // Returns a string of the path to use for the state file.
  546. // This will be a fallback %LocalAppData% path if migration fails,
  547. // a %ProgramData% path otherwise.
  548. func tryWindowsAppDataMigration(logf logger.Logf, path string) string {
  549. if path != paths.DefaultTailscaledStateFile() {
  550. // If they're specifying a non-default path, just trust that they know
  551. // what they are doing.
  552. return path
  553. }
  554. oldFile := paths.LegacyStateFilePath()
  555. return paths.TryConfigFileMigration(logf, oldFile, path)
  556. }
  557. // StateStore returns a StateStore from path.
  558. //
  559. // The path should be an absolute path to a file.
  560. //
  561. // Special cases:
  562. //
  563. // * empty string means to use an in-memory store
  564. // * if the string begins with "kube:", the suffix
  565. // is a Kubernetes secret name
  566. // * if the string begins with "arn:", the value is
  567. // an AWS ARN for an SSM.
  568. func StateStore(path string, logf logger.Logf) (ipn.StateStore, error) {
  569. if path == "" {
  570. return &ipn.MemoryStore{}, nil
  571. }
  572. const kubePrefix = "kube:"
  573. const arnPrefix = "arn:"
  574. switch {
  575. case strings.HasPrefix(path, kubePrefix):
  576. secretName := strings.TrimPrefix(path, kubePrefix)
  577. store, err := ipn.NewKubeStore(secretName)
  578. if err != nil {
  579. return nil, fmt.Errorf("ipn.NewKubeStore(%q): %v", secretName, err)
  580. }
  581. return store, nil
  582. case strings.HasPrefix(path, arnPrefix):
  583. store, err := aws.NewStore(path)
  584. if err != nil {
  585. return nil, fmt.Errorf("aws.NewStore(%q): %v", path, err)
  586. }
  587. return store, nil
  588. }
  589. if runtime.GOOS == "windows" {
  590. path = tryWindowsAppDataMigration(logf, path)
  591. }
  592. store, err := ipn.NewFileStore(path)
  593. if err != nil {
  594. return nil, fmt.Errorf("ipn.NewFileStore(%q): %v", path, err)
  595. }
  596. return store, nil
  597. }
  598. // Run runs a Tailscale backend service.
  599. // The getEngine func is called repeatedly, once per connection, until it returns an engine successfully.
  600. //
  601. // Deprecated: use New and Server.Run instead.
  602. func Run(ctx context.Context, logf logger.Logf, ln net.Listener, store ipn.StateStore, logid string, getEngine func() (wgengine.Engine, error), opts Options) error {
  603. getEngine = getEngineUntilItWorksWrapper(getEngine)
  604. runDone := make(chan struct{})
  605. defer close(runDone)
  606. var serverMu sync.Mutex
  607. var serverOrNil *Server
  608. // When the context is closed or when we return, whichever is first, close our listener
  609. // and all open connections.
  610. go func() {
  611. select {
  612. case <-ctx.Done():
  613. case <-runDone:
  614. }
  615. serverMu.Lock()
  616. if s := serverOrNil; s != nil {
  617. s.stopAll()
  618. }
  619. serverMu.Unlock()
  620. ln.Close()
  621. }()
  622. logf("Listening on %v", ln.Addr())
  623. var serverModeUser *user.User
  624. if opts.AutostartStateKey == "" {
  625. autoStartKey, err := store.ReadState(ipn.ServerModeStartKey)
  626. if err != nil && err != ipn.ErrStateNotExist {
  627. return fmt.Errorf("calling ReadState on state store: %w", err)
  628. }
  629. key := string(autoStartKey)
  630. if strings.HasPrefix(key, "user-") {
  631. uid := strings.TrimPrefix(key, "user-")
  632. u, err := lookupUserFromID(logf, uid)
  633. if err != nil {
  634. logf("ipnserver: found server mode auto-start key %q; failed to load user: %v", key, err)
  635. } else {
  636. logf("ipnserver: found server mode auto-start key %q (user %s)", key, u.Username)
  637. serverModeUser = u
  638. }
  639. opts.AutostartStateKey = ipn.StateKey(key)
  640. }
  641. }
  642. bo := backoff.NewBackoff("ipnserver", logf, 30*time.Second)
  643. var unservedConn net.Conn // if non-nil, accepted, but hasn't served yet
  644. eng, err := getEngine()
  645. if err != nil {
  646. logf("ipnserver: initial getEngine call: %v", err)
  647. for i := 1; ctx.Err() == nil; i++ {
  648. c, err := ln.Accept()
  649. if err != nil {
  650. logf("%d: Accept: %v", i, err)
  651. bo.BackOff(ctx, err)
  652. continue
  653. }
  654. logf("ipnserver: try%d: trying getEngine again...", i)
  655. eng, err = getEngine()
  656. if err == nil {
  657. logf("%d: GetEngine worked; exiting failure loop", i)
  658. unservedConn = c
  659. break
  660. }
  661. logf("ipnserver%d: getEngine failed again: %v", i, err)
  662. errMsg := err.Error()
  663. go func() {
  664. defer c.Close()
  665. bs := ipn.NewBackendServer(logf, nil, jsonNotifier(c, logf))
  666. bs.SendErrorMessage(errMsg)
  667. time.Sleep(time.Second)
  668. }()
  669. }
  670. if err := ctx.Err(); err != nil {
  671. return err
  672. }
  673. }
  674. if unservedConn != nil {
  675. ln = &listenerWithReadyConn{
  676. Listener: ln,
  677. c: unservedConn,
  678. }
  679. }
  680. server, err := New(logf, logid, store, eng, serverModeUser, opts)
  681. if err != nil {
  682. return err
  683. }
  684. serverMu.Lock()
  685. serverOrNil = server
  686. serverMu.Unlock()
  687. return server.Run(ctx, ln)
  688. }
  689. // New returns a new Server.
  690. //
  691. // To start it, use the Server.Run method.
  692. func New(logf logger.Logf, logid string, store ipn.StateStore, eng wgengine.Engine, serverModeUser *user.User, opts Options) (*Server, error) {
  693. b, err := ipnlocal.NewLocalBackend(logf, logid, store, eng)
  694. if err != nil {
  695. return nil, fmt.Errorf("NewLocalBackend: %v", err)
  696. }
  697. b.SetVarRoot(opts.VarRoot)
  698. b.SetDecompressor(func() (controlclient.Decompressor, error) {
  699. return smallzstd.NewDecoder(nil)
  700. })
  701. if opts.AutostartStateKey == "" {
  702. autoStartKey, err := store.ReadState(ipn.ServerModeStartKey)
  703. if err != nil && err != ipn.ErrStateNotExist {
  704. return nil, fmt.Errorf("calling ReadState on store: %w", err)
  705. }
  706. key := string(autoStartKey)
  707. if strings.HasPrefix(key, "user-") {
  708. uid := strings.TrimPrefix(key, "user-")
  709. u, err := lookupUserFromID(logf, uid)
  710. if err != nil {
  711. logf("ipnserver: found server mode auto-start key %q; failed to load user: %v", key, err)
  712. } else {
  713. logf("ipnserver: found server mode auto-start key %q (user %s)", key, u.Username)
  714. serverModeUser = u
  715. }
  716. opts.AutostartStateKey = ipn.StateKey(key)
  717. }
  718. }
  719. server := &Server{
  720. b: b,
  721. backendLogID: logid,
  722. logf: logf,
  723. resetOnZero: !opts.SurviveDisconnects,
  724. serverModeUser: serverModeUser,
  725. autostartStateKey: opts.AutostartStateKey,
  726. }
  727. server.bs = ipn.NewBackendServer(logf, b, server.writeToClients)
  728. return server, nil
  729. }
  730. // Run runs the server, accepting connections from ln forever.
  731. //
  732. // If the context is done, the listener is closed.
  733. func (s *Server) Run(ctx context.Context, ln net.Listener) error {
  734. defer s.b.Shutdown()
  735. runDone := make(chan struct{})
  736. defer close(runDone)
  737. // When the context is closed or when we return, whichever is first, close our listener
  738. // and all open connections.
  739. go func() {
  740. select {
  741. case <-ctx.Done():
  742. case <-runDone:
  743. }
  744. s.stopAll()
  745. ln.Close()
  746. }()
  747. if s.autostartStateKey != "" {
  748. s.bs.GotCommand(ctx, &ipn.Command{
  749. Version: version.Long,
  750. Start: &ipn.StartArgs{
  751. Opts: ipn.Options{StateKey: s.autostartStateKey},
  752. },
  753. })
  754. }
  755. systemd.Ready()
  756. bo := backoff.NewBackoff("ipnserver", s.logf, 30*time.Second)
  757. var connNum int
  758. for {
  759. if ctx.Err() != nil {
  760. return ctx.Err()
  761. }
  762. c, err := ln.Accept()
  763. if err != nil {
  764. if ctx.Err() != nil {
  765. return ctx.Err()
  766. }
  767. s.logf("ipnserver: Accept: %v", err)
  768. bo.BackOff(ctx, err)
  769. continue
  770. }
  771. connNum++
  772. go s.serveConn(ctx, c, logger.WithPrefix(s.logf, fmt.Sprintf("ipnserver: conn%d: ", connNum)))
  773. }
  774. }
  775. // BabysitProc runs the current executable as a child process with the
  776. // provided args, capturing its output, writing it to files, and
  777. // restarting the process on any crashes.
  778. //
  779. // It's only currently (2020-10-29) used on Windows.
  780. func BabysitProc(ctx context.Context, args []string, logf logger.Logf) {
  781. executable, err := os.Executable()
  782. if err != nil {
  783. panic("cannot determine executable: " + err.Error())
  784. }
  785. if runtime.GOOS == "windows" {
  786. if len(args) != 2 && args[0] != "/subproc" {
  787. panic(fmt.Sprintf("unexpected arguments %q", args))
  788. }
  789. logID := args[1]
  790. logf = filelogger.New("tailscale-service", logID, logf)
  791. }
  792. var proc struct {
  793. mu sync.Mutex
  794. p *os.Process
  795. }
  796. done := make(chan struct{})
  797. go func() {
  798. interrupt := make(chan os.Signal, 1)
  799. signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM)
  800. var sig os.Signal
  801. select {
  802. case sig = <-interrupt:
  803. logf("BabysitProc: got signal: %v", sig)
  804. close(done)
  805. case <-ctx.Done():
  806. logf("BabysitProc: context done")
  807. sig = os.Kill
  808. close(done)
  809. }
  810. proc.mu.Lock()
  811. proc.p.Signal(sig)
  812. proc.mu.Unlock()
  813. }()
  814. bo := backoff.NewBackoff("BabysitProc", logf, 30*time.Second)
  815. for {
  816. startTime := time.Now()
  817. log.Printf("exec: %#v %v", executable, args)
  818. cmd := exec.Command(executable, args...)
  819. // Create a pipe object to use as the subproc's stdin.
  820. // When the writer goes away, the reader gets EOF.
  821. // A subproc can watch its stdin and exit when it gets EOF;
  822. // this is a very reliable way to have a subproc die when
  823. // its parent (us) disappears.
  824. // We never need to actually write to wStdin.
  825. rStdin, wStdin, err := os.Pipe()
  826. if err != nil {
  827. log.Printf("os.Pipe 1: %v", err)
  828. return
  829. }
  830. // Create a pipe object to use as the subproc's stdout/stderr.
  831. // We'll read from this pipe and send it to logf, line by line.
  832. // We can't use os.exec's io.Writer for this because it
  833. // doesn't care about lines, and thus ends up merging multiple
  834. // log lines into one or splitting one line into multiple
  835. // logf() calls. bufio is more appropriate.
  836. rStdout, wStdout, err := os.Pipe()
  837. if err != nil {
  838. log.Printf("os.Pipe 2: %v", err)
  839. }
  840. go func(r *os.File) {
  841. defer r.Close()
  842. rb := bufio.NewReader(r)
  843. for {
  844. s, err := rb.ReadString('\n')
  845. if s != "" {
  846. logf("%s", s)
  847. }
  848. if err != nil {
  849. break
  850. }
  851. }
  852. }(rStdout)
  853. cmd.Stdin = rStdin
  854. cmd.Stdout = wStdout
  855. cmd.Stderr = wStdout
  856. err = cmd.Start()
  857. // Now that the subproc is started, get rid of our copy of the
  858. // pipe reader. Bad things happen on Windows if more than one
  859. // process owns the read side of a pipe.
  860. rStdin.Close()
  861. wStdout.Close()
  862. if err != nil {
  863. log.Printf("starting subprocess failed: %v", err)
  864. } else {
  865. proc.mu.Lock()
  866. proc.p = cmd.Process
  867. proc.mu.Unlock()
  868. err = cmd.Wait()
  869. log.Printf("subprocess exited: %v", err)
  870. }
  871. // If the process finishes, clean up the write side of the
  872. // pipe. We'll make a new one when we restart the subproc.
  873. wStdin.Close()
  874. if os.Getenv("TS_DEBUG_RESTART_CRASHED") == "0" {
  875. log.Fatalf("Process ended.")
  876. }
  877. if time.Since(startTime) < 60*time.Second {
  878. bo.BackOff(ctx, fmt.Errorf("subproc early exit: %v", err))
  879. } else {
  880. // Reset the timeout, since the process ran for a while.
  881. bo.BackOff(ctx, nil)
  882. }
  883. select {
  884. case <-done:
  885. return
  886. default:
  887. }
  888. }
  889. }
  890. // FixedEngine returns a func that returns eng and a nil error.
  891. func FixedEngine(eng wgengine.Engine) func() (wgengine.Engine, error) {
  892. return func() (wgengine.Engine, error) { return eng, nil }
  893. }
  894. // getEngineUntilItWorksWrapper returns a getEngine wrapper that does
  895. // not call getEngine concurrently and stops calling getEngine once
  896. // it's returned a working engine.
  897. func getEngineUntilItWorksWrapper(getEngine func() (wgengine.Engine, error)) func() (wgengine.Engine, error) {
  898. var mu sync.Mutex
  899. var engGood wgengine.Engine
  900. return func() (wgengine.Engine, error) {
  901. mu.Lock()
  902. defer mu.Unlock()
  903. if engGood != nil {
  904. return engGood, nil
  905. }
  906. e, err := getEngine()
  907. if err != nil {
  908. return nil, err
  909. }
  910. engGood = e
  911. return e, nil
  912. }
  913. }
  914. type dummyAddr string
  915. type oneConnListener struct {
  916. conn net.Conn
  917. }
  918. func (l *oneConnListener) Accept() (c net.Conn, err error) {
  919. c = l.conn
  920. if c == nil {
  921. err = io.EOF
  922. return
  923. }
  924. err = nil
  925. l.conn = nil
  926. return
  927. }
  928. func (l *oneConnListener) Close() error { return nil }
  929. func (l *oneConnListener) Addr() net.Addr { return dummyAddr("unused-address") }
  930. func (a dummyAddr) Network() string { return string(a) }
  931. func (a dummyAddr) String() string { return string(a) }
  932. // protoSwitchConn is a net.Conn that's we want to speak HTTP to but
  933. // it's already had a few bytes read from it to determine that it's
  934. // HTTP. So we Read from its bufio.Reader. On Close, we we tell the
  935. // server it's closed, so the server can account the who's connected.
  936. type protoSwitchConn struct {
  937. s *Server
  938. net.Conn
  939. br *bufio.Reader
  940. closeOnce sync.Once
  941. }
  942. func (psc *protoSwitchConn) Read(p []byte) (int, error) { return psc.br.Read(p) }
  943. func (psc *protoSwitchConn) Close() error {
  944. psc.closeOnce.Do(func() { psc.s.removeAndCloseConn(psc.Conn) })
  945. return nil
  946. }
  947. func (s *Server) localhostHandler(ci connIdentity) http.Handler {
  948. lah := localapi.NewHandler(s.b, s.logf, s.backendLogID)
  949. lah.PermitRead, lah.PermitWrite = s.localAPIPermissions(ci)
  950. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  951. if strings.HasPrefix(r.URL.Path, "/localapi/") {
  952. lah.ServeHTTP(w, r)
  953. return
  954. }
  955. if ci.NotWindows {
  956. io.WriteString(w, "<html><title>Tailscale</title><body><h1>Tailscale</h1>This is the local Tailscale daemon.")
  957. return
  958. }
  959. s.ServeHTMLStatus(w, r)
  960. })
  961. }
  962. func (s *Server) ServeHTMLStatus(w http.ResponseWriter, r *http.Request) {
  963. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  964. st := s.b.Status()
  965. // TODO(bradfitz): add LogID and opts to st?
  966. st.WriteHTML(w)
  967. }
  968. func peerPid(entries []netstat.Entry, la, ra netaddr.IPPort) int {
  969. for _, e := range entries {
  970. if e.Local == ra && e.Remote == la {
  971. return e.Pid
  972. }
  973. }
  974. return 0
  975. }
  976. // jsonNotifier returns a notify-writer func that writes ipn.Notify
  977. // messages to w.
  978. func jsonNotifier(w io.Writer, logf logger.Logf) func(ipn.Notify) {
  979. return func(n ipn.Notify) {
  980. if b, ok := marshalNotify(n, logf); ok {
  981. ipn.WriteMsg(w, b)
  982. }
  983. }
  984. }
  985. func marshalNotify(n ipn.Notify, logf logger.Logf) (b []byte, ok bool) {
  986. b, err := json.Marshal(n)
  987. if err != nil {
  988. logf("ipnserver: [unexpected] error serializing JSON: %v", err)
  989. return nil, false
  990. }
  991. if bytes.Contains(b, jsonEscapedZero) {
  992. logf("[unexpected] zero byte in BackendServer.send notify message: %q", b)
  993. }
  994. return b, true
  995. }
  996. // listenerWithReadyConn is a net.Listener wrapper that has
  997. // one net.Conn ready to be accepted already.
  998. type listenerWithReadyConn struct {
  999. net.Listener
  1000. mu sync.Mutex
  1001. c net.Conn // if non-nil, ready to be Accepted
  1002. }
  1003. func (ln *listenerWithReadyConn) Accept() (net.Conn, error) {
  1004. ln.mu.Lock()
  1005. c := ln.c
  1006. ln.c = nil
  1007. ln.mu.Unlock()
  1008. if c != nil {
  1009. return c, nil
  1010. }
  1011. return ln.Listener.Accept()
  1012. }