server.go 35 KB

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