server.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package ipnserver
  4. import (
  5. "context"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "net"
  11. "net/http"
  12. "os/user"
  13. "strconv"
  14. "strings"
  15. "sync"
  16. "sync/atomic"
  17. "time"
  18. "unicode"
  19. "tailscale.com/envknob"
  20. "tailscale.com/ipn"
  21. "tailscale.com/ipn/ipnauth"
  22. "tailscale.com/ipn/ipnlocal"
  23. "tailscale.com/ipn/localapi"
  24. "tailscale.com/net/netmon"
  25. "tailscale.com/types/logger"
  26. "tailscale.com/types/logid"
  27. "tailscale.com/util/mak"
  28. "tailscale.com/util/set"
  29. "tailscale.com/util/systemd"
  30. )
  31. // Server is an IPN backend and its set of 0 or more active localhost
  32. // TCP or unix socket connections talking to that backend.
  33. type Server struct {
  34. lb atomic.Pointer[ipnlocal.LocalBackend]
  35. logf logger.Logf
  36. netMon *netmon.Monitor // must be non-nil
  37. backendLogID logid.PublicID
  38. // resetOnZero is whether to call bs.Reset on transition from
  39. // 1->0 active HTTP requests. That is, this is whether the backend is
  40. // being run in "client mode" that requires an active GUI
  41. // connection (such as on Windows by default). Even if this
  42. // is true, the ForceDaemon pref can override this.
  43. resetOnZero bool
  44. // mu guards the fields that follow.
  45. // lock order: mu, then LocalBackend.mu
  46. mu sync.Mutex
  47. lastUserID ipn.WindowsUserID // tracks last userid; on change, Reset state for paranoia
  48. activeReqs map[*http.Request]*ipnauth.ConnIdentity
  49. backendWaiter waiterSet // of LocalBackend waiters
  50. zeroReqWaiter waiterSet // of blockUntilZeroConnections waiters
  51. }
  52. func (s *Server) mustBackend() *ipnlocal.LocalBackend {
  53. lb := s.lb.Load()
  54. if lb == nil {
  55. panic("unexpected: call to mustBackend in path where SetLocalBackend should've been called")
  56. }
  57. return lb
  58. }
  59. // waiterSet is a set of callers waiting on something. Each item (map value) in
  60. // the set is a func that wakes up that waiter's context. The waiter is responsible
  61. // for removing itself from the set when woken up. The (*waiterSet).add method
  62. // returns a cleanup method which does that removal. The caller than defers that
  63. // cleanup.
  64. //
  65. // TODO(bradfitz): this is a generally useful pattern. Move elsewhere?
  66. type waiterSet set.HandleSet[context.CancelFunc]
  67. // add registers a new waiter in the set.
  68. // It aquires mu to add the waiter, and does so again when cleanup is called to remove it.
  69. // ready is closed when the waiter is ready (or ctx is done).
  70. func (s *waiterSet) add(mu *sync.Mutex, ctx context.Context) (ready <-chan struct{}, cleanup func()) {
  71. ctx, cancel := context.WithCancel(ctx)
  72. hs := (*set.HandleSet[context.CancelFunc])(s) // change method set
  73. mu.Lock()
  74. h := hs.Add(cancel)
  75. mu.Unlock()
  76. return ctx.Done(), func() {
  77. mu.Lock()
  78. delete(*hs, h)
  79. mu.Unlock()
  80. cancel()
  81. }
  82. }
  83. // wakeAll wakes up all waiters in the set.
  84. func (w waiterSet) wakeAll() {
  85. for _, cancel := range w {
  86. cancel() // they'll remove themselves
  87. }
  88. }
  89. func (s *Server) awaitBackend(ctx context.Context) (_ *ipnlocal.LocalBackend, ok bool) {
  90. lb := s.lb.Load()
  91. if lb != nil {
  92. return lb, true
  93. }
  94. ready, cleanup := s.backendWaiter.add(&s.mu, ctx)
  95. defer cleanup()
  96. // Try again, now that we've registered, in case there was a
  97. // race.
  98. lb = s.lb.Load()
  99. if lb != nil {
  100. return lb, true
  101. }
  102. <-ready
  103. lb = s.lb.Load()
  104. return lb, lb != nil
  105. }
  106. // serveServerStatus serves the /server-status endpoint which reports whether
  107. // the LocalBackend is up yet.
  108. // This is primarily for the Windows GUI, because wintun can take awhile to
  109. // come up. See https://github.com/tailscale/tailscale/issues/6522.
  110. func (s *Server) serveServerStatus(w http.ResponseWriter, r *http.Request) {
  111. ctx := r.Context()
  112. w.Header().Set("Content-Type", "application/json")
  113. var res struct {
  114. Error string `json:"error,omitempty"`
  115. }
  116. lb := s.lb.Load()
  117. if lb == nil {
  118. w.WriteHeader(http.StatusServiceUnavailable)
  119. if wait, _ := strconv.ParseBool(r.FormValue("wait")); wait {
  120. w.(http.Flusher).Flush()
  121. lb, _ = s.awaitBackend(ctx)
  122. }
  123. }
  124. if lb == nil {
  125. res.Error = "backend not ready"
  126. }
  127. json.NewEncoder(w).Encode(res)
  128. }
  129. func (s *Server) serveHTTP(w http.ResponseWriter, r *http.Request) {
  130. ctx := r.Context()
  131. if r.Method == "CONNECT" {
  132. if envknob.GOOS() == "windows" {
  133. // For the GUI client when using an exit node. See docs on handleProxyConnectConn.
  134. s.handleProxyConnectConn(w, r)
  135. } else {
  136. http.Error(w, "bad method for platform", http.StatusMethodNotAllowed)
  137. }
  138. return
  139. }
  140. // Check for this method before the awaitBackend call, as it reports whether
  141. // the backend is available.
  142. if r.Method == "GET" && r.URL.Path == "/server-status" {
  143. s.serveServerStatus(w, r)
  144. return
  145. }
  146. lb, ok := s.awaitBackend(ctx)
  147. if !ok {
  148. // Almost certainly because the context was canceled so the response
  149. // here doesn't really matter. The client is gone.
  150. http.Error(w, "no backend", http.StatusServiceUnavailable)
  151. return
  152. }
  153. var ci *ipnauth.ConnIdentity
  154. switch v := r.Context().Value(connIdentityContextKey{}).(type) {
  155. case *ipnauth.ConnIdentity:
  156. ci = v
  157. case error:
  158. http.Error(w, v.Error(), http.StatusUnauthorized)
  159. return
  160. case nil:
  161. http.Error(w, "internal error: no connIdentityContextKey", http.StatusInternalServerError)
  162. return
  163. }
  164. onDone, err := s.addActiveHTTPRequest(r, ci)
  165. if err != nil {
  166. if ou, ok := err.(inUseOtherUserError); ok && localapi.InUseOtherUserIPNStream(w, r, ou.Unwrap()) {
  167. w.(http.Flusher).Flush()
  168. s.blockWhileIdentityInUse(ctx, ci)
  169. return
  170. }
  171. http.Error(w, err.Error(), http.StatusUnauthorized)
  172. return
  173. }
  174. defer onDone()
  175. if strings.HasPrefix(r.URL.Path, "/localapi/") {
  176. lah := localapi.NewHandler(lb, s.logf, s.backendLogID)
  177. lah.PermitRead, lah.PermitWrite = s.localAPIPermissions(ci)
  178. lah.PermitCert = s.connCanFetchCerts(ci)
  179. lah.ConnIdentity = ci
  180. lah.ServeHTTP(w, r)
  181. return
  182. }
  183. if r.URL.Path != "/" {
  184. http.NotFound(w, r)
  185. return
  186. }
  187. if envknob.GOOS() == "windows" {
  188. // TODO(bradfitz): remove this once we moved to named pipes for LocalAPI
  189. // on Windows. This could then move to all platforms instead at
  190. // 100.100.100.100 or something (quad100 handler in LocalAPI)
  191. s.ServeHTMLStatus(w, r)
  192. return
  193. }
  194. io.WriteString(w, "<html><title>Tailscale</title><body><h1>Tailscale</h1>This is the local Tailscale daemon.\n")
  195. }
  196. // inUseOtherUserError is the error type for when the server is in use
  197. // by a different local user.
  198. type inUseOtherUserError struct{ error }
  199. func (e inUseOtherUserError) Unwrap() error { return e.error }
  200. // checkConnIdentityLocked checks whether the provided identity is
  201. // allowed to connect to the server.
  202. //
  203. // The returned error, when non-nil, will be of type inUseOtherUserError.
  204. //
  205. // s.mu must be held.
  206. func (s *Server) checkConnIdentityLocked(ci *ipnauth.ConnIdentity) error {
  207. // If clients are already connected, verify they're the same user.
  208. // This mostly matters on Windows at the moment.
  209. if len(s.activeReqs) > 0 {
  210. var active *ipnauth.ConnIdentity
  211. for _, active = range s.activeReqs {
  212. break
  213. }
  214. if active != nil {
  215. chkTok, err := ci.WindowsToken()
  216. if err == nil {
  217. defer chkTok.Close()
  218. } else if !errors.Is(err, ipnauth.ErrNotImplemented) {
  219. return err
  220. }
  221. // Always allow Windows SYSTEM user to connect,
  222. // even if Tailscale is currently being used by another user.
  223. if chkTok != nil && chkTok.IsLocalSystem() {
  224. return nil
  225. }
  226. activeTok, err := active.WindowsToken()
  227. if err == nil {
  228. defer activeTok.Close()
  229. } else if !errors.Is(err, ipnauth.ErrNotImplemented) {
  230. return err
  231. }
  232. if chkTok != nil && !chkTok.EqualUIDs(activeTok) {
  233. var b strings.Builder
  234. b.WriteString("Tailscale already in use")
  235. if username, err := activeTok.Username(); err == nil {
  236. fmt.Fprintf(&b, " by %s", username)
  237. }
  238. fmt.Fprintf(&b, ", pid %d", active.Pid())
  239. return inUseOtherUserError{errors.New(b.String())}
  240. }
  241. }
  242. }
  243. if err := s.mustBackend().CheckIPNConnectionAllowed(ci); err != nil {
  244. return inUseOtherUserError{err}
  245. }
  246. return nil
  247. }
  248. // blockWhileIdentityInUse blocks while ci can't connect to the server because
  249. // the server is in use by a different user.
  250. //
  251. // This is primarily used for the Windows GUI, to block until one user's done
  252. // controlling the tailscaled process.
  253. func (s *Server) blockWhileIdentityInUse(ctx context.Context, ci *ipnauth.ConnIdentity) error {
  254. inUse := func() bool {
  255. s.mu.Lock()
  256. defer s.mu.Unlock()
  257. _, ok := s.checkConnIdentityLocked(ci).(inUseOtherUserError)
  258. return ok
  259. }
  260. for inUse() {
  261. // Check whenever the connection count drops down to zero.
  262. ready, cleanup := s.zeroReqWaiter.add(&s.mu, ctx)
  263. <-ready
  264. cleanup()
  265. if err := ctx.Err(); err != nil {
  266. return err
  267. }
  268. }
  269. return nil
  270. }
  271. // localAPIPermissions returns the permissions for the given identity accessing
  272. // the Tailscale local daemon API.
  273. //
  274. // s.mu must not be held.
  275. func (s *Server) localAPIPermissions(ci *ipnauth.ConnIdentity) (read, write bool) {
  276. switch envknob.GOOS() {
  277. case "windows":
  278. s.mu.Lock()
  279. defer s.mu.Unlock()
  280. if s.checkConnIdentityLocked(ci) == nil {
  281. return true, true
  282. }
  283. return false, false
  284. case "js":
  285. return true, true
  286. }
  287. if ci.IsUnixSock() {
  288. return true, !ci.IsReadonlyConn(s.mustBackend().OperatorUserID(), logger.Discard)
  289. }
  290. return false, false
  291. }
  292. // userIDFromString maps from either a numeric user id in string form
  293. // ("998") or username ("caddy") to its string userid ("998").
  294. // It returns the empty string on error.
  295. func userIDFromString(v string) string {
  296. if v == "" || isAllDigit(v) {
  297. return v
  298. }
  299. u, err := user.Lookup(v)
  300. if err != nil {
  301. return ""
  302. }
  303. return u.Uid
  304. }
  305. func isAllDigit(s string) bool {
  306. for i := range len(s) {
  307. if b := s[i]; b < '0' || b > '9' {
  308. return false
  309. }
  310. }
  311. return true
  312. }
  313. // connCanFetchCerts reports whether ci is allowed to fetch HTTPS
  314. // certs from this server when it wouldn't otherwise be able to.
  315. //
  316. // That is, this reports whether ci should grant additional
  317. // capabilities over what the conn would otherwise be able to do.
  318. //
  319. // For now this only returns true on Unix machines when
  320. // TS_PERMIT_CERT_UID is set the to the userid of the peer
  321. // connection. It's intended to give your non-root webserver access
  322. // (www-data, caddy, nginx, etc) to certs.
  323. func (s *Server) connCanFetchCerts(ci *ipnauth.ConnIdentity) bool {
  324. if ci.IsUnixSock() && ci.Creds() != nil {
  325. connUID, ok := ci.Creds().UserID()
  326. if ok && connUID == userIDFromString(envknob.String("TS_PERMIT_CERT_UID")) {
  327. return true
  328. }
  329. }
  330. return false
  331. }
  332. // addActiveHTTPRequest adds c to the server's list of active HTTP requests.
  333. //
  334. // If the returned error may be of type inUseOtherUserError.
  335. //
  336. // onDone must be called when the HTTP request is done.
  337. func (s *Server) addActiveHTTPRequest(req *http.Request, ci *ipnauth.ConnIdentity) (onDone func(), err error) {
  338. if ci == nil {
  339. return nil, errors.New("internal error: nil connIdentity")
  340. }
  341. lb := s.mustBackend()
  342. // If the connected user changes, reset the backend server state to make
  343. // sure node keys don't leak between users.
  344. var doReset bool
  345. defer func() {
  346. if doReset {
  347. s.logf("identity changed; resetting server")
  348. lb.ResetForClientDisconnect()
  349. }
  350. }()
  351. s.mu.Lock()
  352. defer s.mu.Unlock()
  353. if err := s.checkConnIdentityLocked(ci); err != nil {
  354. return nil, err
  355. }
  356. mak.Set(&s.activeReqs, req, ci)
  357. if len(s.activeReqs) == 1 {
  358. token, err := ci.WindowsToken()
  359. if err != nil {
  360. if !errors.Is(err, ipnauth.ErrNotImplemented) {
  361. s.logf("error obtaining access token: %v", err)
  362. }
  363. } else if !token.IsLocalSystem() {
  364. // Tell the LocalBackend about the identity we're now running as,
  365. // unless its the SYSTEM user. That user is not a real account and
  366. // doesn't have a home directory.
  367. uid, err := lb.SetCurrentUser(token)
  368. if err != nil {
  369. token.Close()
  370. return nil, err
  371. }
  372. if s.lastUserID != uid {
  373. if s.lastUserID != "" {
  374. doReset = true
  375. }
  376. s.lastUserID = uid
  377. }
  378. }
  379. }
  380. onDone = func() {
  381. s.mu.Lock()
  382. delete(s.activeReqs, req)
  383. remain := len(s.activeReqs)
  384. s.mu.Unlock()
  385. if remain == 0 && s.resetOnZero {
  386. if lb.InServerMode() {
  387. s.logf("client disconnected; staying alive in server mode")
  388. } else {
  389. s.logf("client disconnected; stopping server")
  390. lb.ResetForClientDisconnect()
  391. }
  392. }
  393. // Wake up callers waiting for the server to be idle:
  394. if remain == 0 {
  395. s.mu.Lock()
  396. s.zeroReqWaiter.wakeAll()
  397. s.mu.Unlock()
  398. }
  399. }
  400. return onDone, nil
  401. }
  402. // New returns a new Server.
  403. //
  404. // To start it, use the Server.Run method.
  405. //
  406. // At some point, either before or after Run, the Server's SetLocalBackend
  407. // method must also be called before Server can do anything useful.
  408. func New(logf logger.Logf, logID logid.PublicID, netMon *netmon.Monitor) *Server {
  409. if netMon == nil {
  410. panic("nil netMon")
  411. }
  412. return &Server{
  413. backendLogID: logID,
  414. logf: logf,
  415. netMon: netMon,
  416. resetOnZero: envknob.GOOS() == "windows",
  417. }
  418. }
  419. // SetLocalBackend sets the server's LocalBackend.
  420. //
  421. // It should only call be called after calling lb.Start.
  422. func (s *Server) SetLocalBackend(lb *ipnlocal.LocalBackend) {
  423. if lb == nil {
  424. panic("nil LocalBackend")
  425. }
  426. if !s.lb.CompareAndSwap(nil, lb) {
  427. panic("already set")
  428. }
  429. s.mu.Lock()
  430. s.backendWaiter.wakeAll()
  431. s.mu.Unlock()
  432. // TODO(bradfitz): send status update to GUI long poller waiter. See
  433. // https://github.com/tailscale/tailscale/issues/6522
  434. }
  435. // connIdentityContextKey is the http.Request.Context's context.Value key for either an
  436. // *ipnauth.ConnIdentity or an error.
  437. type connIdentityContextKey struct{}
  438. // Run runs the server, accepting connections from ln forever.
  439. //
  440. // If the context is done, the listener is closed. It is also the base context
  441. // of all HTTP requests.
  442. //
  443. // If the Server's LocalBackend has already been set, Run starts it.
  444. // Otherwise, the next call to SetLocalBackend will start it.
  445. func (s *Server) Run(ctx context.Context, ln net.Listener) error {
  446. defer func() {
  447. if lb := s.lb.Load(); lb != nil {
  448. lb.Shutdown()
  449. }
  450. }()
  451. runDone := make(chan struct{})
  452. defer close(runDone)
  453. // When the context is closed or when we return, whichever is first, close our listener
  454. // and all open connections.
  455. go func() {
  456. select {
  457. case <-ctx.Done():
  458. case <-runDone:
  459. }
  460. ln.Close()
  461. }()
  462. systemd.Ready()
  463. hs := &http.Server{
  464. Handler: http.HandlerFunc(s.serveHTTP),
  465. BaseContext: func(_ net.Listener) context.Context { return ctx },
  466. ConnContext: func(ctx context.Context, c net.Conn) context.Context {
  467. ci, err := ipnauth.GetConnIdentity(s.logf, c)
  468. if err != nil {
  469. return context.WithValue(ctx, connIdentityContextKey{}, err)
  470. }
  471. return context.WithValue(ctx, connIdentityContextKey{}, ci)
  472. },
  473. // Localhost connections are cheap; so only do
  474. // keep-alives for a short period of time, as these
  475. // active connections lock the server into only serving
  476. // that user. If the user has this page open, we don't
  477. // want another switching user to be locked out for
  478. // minutes. 5 seconds is enough to let browser hit
  479. // favicon.ico and such.
  480. IdleTimeout: 5 * time.Second,
  481. ErrorLog: logger.StdLogger(logger.WithPrefix(s.logf, "ipnserver: ")),
  482. }
  483. if err := hs.Serve(ln); err != nil {
  484. if err := ctx.Err(); err != nil {
  485. return err
  486. }
  487. return err
  488. }
  489. return nil
  490. }
  491. // ServeHTMLStatus serves an HTML status page at http://localhost:41112/ for
  492. // Windows and via $DEBUG_LISTENER/debug/ipn when tailscaled's --debug flag
  493. // is used to run a debug server.
  494. func (s *Server) ServeHTMLStatus(w http.ResponseWriter, r *http.Request) {
  495. lb := s.lb.Load()
  496. if lb == nil {
  497. http.Error(w, "no LocalBackend", http.StatusServiceUnavailable)
  498. return
  499. }
  500. // As this is only meant for debug, verify there's no DNS name being used to
  501. // access this.
  502. if !strings.HasPrefix(r.Host, "localhost:") && strings.IndexFunc(r.Host, unicode.IsLetter) != -1 {
  503. http.Error(w, "invalid host", http.StatusForbidden)
  504. return
  505. }
  506. w.Header().Set("Content-Security-Policy", `default-src 'none'; frame-ancestors 'none'; script-src 'none'; script-src-elem 'none'; script-src-attr 'none'`)
  507. w.Header().Set("X-Frame-Options", "DENY")
  508. w.Header().Set("X-Content-Type-Options", "nosniff")
  509. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  510. st := lb.Status()
  511. // TODO(bradfitz): add LogID and opts to st?
  512. st.WriteHTML(w)
  513. }