server.go 15 KB

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