server.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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/types/logger"
  25. "tailscale.com/types/logid"
  26. "tailscale.com/util/mak"
  27. "tailscale.com/util/set"
  28. "tailscale.com/util/systemd"
  29. )
  30. // Server is an IPN backend and its set of 0 or more active localhost
  31. // TCP or unix socket connections talking to that backend.
  32. type Server struct {
  33. lb atomic.Pointer[ipnlocal.LocalBackend]
  34. logf logger.Logf
  35. backendLogID logid.PublicID
  36. // resetOnZero is whether to call bs.Reset on transition from
  37. // 1->0 active HTTP requests. That is, this is whether the backend is
  38. // being run in "client mode" that requires an active GUI
  39. // connection (such as on Windows by default). Even if this
  40. // is true, the ForceDaemon pref can override this.
  41. resetOnZero bool
  42. startBackendOnce sync.Once
  43. runCalled atomic.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.ServeHTTP(w, r)
  180. return
  181. }
  182. if r.URL.Path != "/" {
  183. http.NotFound(w, r)
  184. return
  185. }
  186. if envknob.GOOS() == "windows" {
  187. // TODO(bradfitz): remove this once we moved to named pipes for LocalAPI
  188. // on Windows. This could then move to all platforms instead at
  189. // 100.100.100.100 or something (quad100 handler in LocalAPI)
  190. s.ServeHTMLStatus(w, r)
  191. return
  192. }
  193. io.WriteString(w, "<html><title>Tailscale</title><body><h1>Tailscale</h1>This is the local Tailscale daemon.\n")
  194. }
  195. // inUseOtherUserError is the error type for when the server is in use
  196. // by a different local user.
  197. type inUseOtherUserError struct{ error }
  198. func (e inUseOtherUserError) Unwrap() error { return e.error }
  199. // checkConnIdentityLocked checks whether the provided identity is
  200. // allowed to connect to the server.
  201. //
  202. // The returned error, when non-nil, will be of type inUseOtherUserError.
  203. //
  204. // s.mu must be held.
  205. func (s *Server) checkConnIdentityLocked(ci *ipnauth.ConnIdentity) error {
  206. // If clients are already connected, verify they're the same user.
  207. // This mostly matters on Windows at the moment.
  208. if len(s.activeReqs) > 0 {
  209. var active *ipnauth.ConnIdentity
  210. for _, active = range s.activeReqs {
  211. break
  212. }
  213. if active != nil && ci.WindowsUserID() != active.WindowsUserID() {
  214. return inUseOtherUserError{fmt.Errorf("Tailscale already in use by %s, pid %d", active.User().Username, active.Pid())}
  215. }
  216. }
  217. if err := s.mustBackend().CheckIPNConnectionAllowed(ci); err != nil {
  218. return inUseOtherUserError{err}
  219. }
  220. return nil
  221. }
  222. // blockWhileIdentityInUse blocks while ci can't connect to the server because
  223. // the server is in use by a different user.
  224. //
  225. // This is primarily used for the Windows GUI, to block until one user's done
  226. // controlling the tailscaled process.
  227. func (s *Server) blockWhileIdentityInUse(ctx context.Context, ci *ipnauth.ConnIdentity) error {
  228. inUse := func() bool {
  229. s.mu.Lock()
  230. defer s.mu.Unlock()
  231. _, ok := s.checkConnIdentityLocked(ci).(inUseOtherUserError)
  232. return ok
  233. }
  234. for inUse() {
  235. // Check whenever the connection count drops down to zero.
  236. ready, cleanup := s.zeroReqWaiter.add(&s.mu, ctx)
  237. <-ready
  238. cleanup()
  239. if err := ctx.Err(); err != nil {
  240. return err
  241. }
  242. }
  243. return nil
  244. }
  245. // localAPIPermissions returns the permissions for the given identity accessing
  246. // the Tailscale local daemon API.
  247. //
  248. // s.mu must not be held.
  249. func (s *Server) localAPIPermissions(ci *ipnauth.ConnIdentity) (read, write bool) {
  250. switch envknob.GOOS() {
  251. case "windows":
  252. s.mu.Lock()
  253. defer s.mu.Unlock()
  254. if s.checkConnIdentityLocked(ci) == nil {
  255. return true, true
  256. }
  257. return false, false
  258. case "js":
  259. return true, true
  260. }
  261. if ci.IsUnixSock() {
  262. return true, !ci.IsReadonlyConn(s.mustBackend().OperatorUserID(), logger.Discard)
  263. }
  264. return false, false
  265. }
  266. // userIDFromString maps from either a numeric user id in string form
  267. // ("998") or username ("caddy") to its string userid ("998").
  268. // It returns the empty string on error.
  269. func userIDFromString(v string) string {
  270. if v == "" || isAllDigit(v) {
  271. return v
  272. }
  273. u, err := user.Lookup(v)
  274. if err != nil {
  275. return ""
  276. }
  277. return u.Uid
  278. }
  279. func isAllDigit(s string) bool {
  280. for i := 0; i < len(s); i++ {
  281. if b := s[i]; b < '0' || b > '9' {
  282. return false
  283. }
  284. }
  285. return true
  286. }
  287. // connCanFetchCerts reports whether ci is allowed to fetch HTTPS
  288. // certs from this server when it wouldn't otherwise be able to.
  289. //
  290. // That is, this reports whether ci should grant additional
  291. // capabilities over what the conn would otherwise be able to do.
  292. //
  293. // For now this only returns true on Unix machines when
  294. // TS_PERMIT_CERT_UID is set the to the userid of the peer
  295. // connection. It's intended to give your non-root webserver access
  296. // (www-data, caddy, nginx, etc) to certs.
  297. func (s *Server) connCanFetchCerts(ci *ipnauth.ConnIdentity) bool {
  298. if ci.IsUnixSock() && ci.Creds() != nil {
  299. connUID, ok := ci.Creds().UserID()
  300. if ok && connUID == userIDFromString(envknob.String("TS_PERMIT_CERT_UID")) {
  301. return true
  302. }
  303. }
  304. return false
  305. }
  306. // addActiveHTTPRequest adds c to the server's list of active HTTP requests.
  307. //
  308. // If the returned error may be of type inUseOtherUserError.
  309. //
  310. // onDone must be called when the HTTP request is done.
  311. func (s *Server) addActiveHTTPRequest(req *http.Request, ci *ipnauth.ConnIdentity) (onDone func(), err error) {
  312. if ci == nil {
  313. return nil, errors.New("internal error: nil connIdentity")
  314. }
  315. lb := s.mustBackend()
  316. // If the connected user changes, reset the backend server state to make
  317. // sure node keys don't leak between users.
  318. var doReset bool
  319. defer func() {
  320. if doReset {
  321. s.logf("identity changed; resetting server")
  322. lb.ResetForClientDisconnect()
  323. }
  324. }()
  325. s.mu.Lock()
  326. defer s.mu.Unlock()
  327. if err := s.checkConnIdentityLocked(ci); err != nil {
  328. return nil, err
  329. }
  330. mak.Set(&s.activeReqs, req, ci)
  331. if uid := ci.WindowsUserID(); uid != "" && len(s.activeReqs) == 1 {
  332. // Tell the LocalBackend about the identity we're now running as.
  333. lb.SetCurrentUserID(uid)
  334. if s.lastUserID != uid {
  335. if s.lastUserID != "" {
  336. doReset = true
  337. }
  338. s.lastUserID = uid
  339. }
  340. }
  341. onDone = func() {
  342. s.mu.Lock()
  343. delete(s.activeReqs, req)
  344. remain := len(s.activeReqs)
  345. s.mu.Unlock()
  346. if remain == 0 && s.resetOnZero {
  347. if lb.InServerMode() {
  348. s.logf("client disconnected; staying alive in server mode")
  349. } else {
  350. s.logf("client disconnected; stopping server")
  351. lb.ResetForClientDisconnect()
  352. }
  353. }
  354. // Wake up callers waiting for the server to be idle:
  355. if remain == 0 {
  356. s.mu.Lock()
  357. s.zeroReqWaiter.wakeAll()
  358. s.mu.Unlock()
  359. }
  360. }
  361. return onDone, nil
  362. }
  363. // New returns a new Server.
  364. //
  365. // To start it, use the Server.Run method.
  366. //
  367. // At some point, either before or after Run, the Server's SetLocalBackend
  368. // method must also be called before Server can do anything useful.
  369. func New(logf logger.Logf, logID logid.PublicID) *Server {
  370. return &Server{
  371. backendLogID: logID,
  372. logf: logf,
  373. resetOnZero: envknob.GOOS() == "windows",
  374. }
  375. }
  376. // SetLocalBackend sets the server's LocalBackend.
  377. //
  378. // If b.Run has already been called, then lb.Start will be called.
  379. // Otherwise Start will be called once Run is called.
  380. func (s *Server) SetLocalBackend(lb *ipnlocal.LocalBackend) {
  381. if lb == nil {
  382. panic("nil LocalBackend")
  383. }
  384. if !s.lb.CompareAndSwap(nil, lb) {
  385. panic("already set")
  386. }
  387. s.startBackendIfNeeded()
  388. s.mu.Lock()
  389. s.backendWaiter.wakeAll()
  390. s.mu.Unlock()
  391. // TODO(bradfitz): send status update to GUI long poller waiter. See
  392. // https://github.com/tailscale/tailscale/issues/6522
  393. }
  394. func (b *Server) startBackendIfNeeded() {
  395. if !b.runCalled.Load() {
  396. return
  397. }
  398. lb := b.lb.Load()
  399. if lb == nil {
  400. return
  401. }
  402. if lb.Prefs().Valid() {
  403. b.startBackendOnce.Do(func() {
  404. lb.Start(ipn.Options{})
  405. })
  406. }
  407. }
  408. // connIdentityContextKey is the http.Request.Context's context.Value key for either an
  409. // *ipnauth.ConnIdentity or an error.
  410. type connIdentityContextKey struct{}
  411. // Run runs the server, accepting connections from ln forever.
  412. //
  413. // If the context is done, the listener is closed. It is also the base context
  414. // of all HTTP requests.
  415. //
  416. // If the Server's LocalBackend has already been set, Run starts it.
  417. // Otherwise, the next call to SetLocalBackend will start it.
  418. func (s *Server) Run(ctx context.Context, ln net.Listener) error {
  419. s.runCalled.Store(true)
  420. defer func() {
  421. if lb := s.lb.Load(); lb != nil {
  422. lb.Shutdown()
  423. }
  424. }()
  425. runDone := make(chan struct{})
  426. defer close(runDone)
  427. // When the context is closed or when we return, whichever is first, close our listener
  428. // and all open connections.
  429. go func() {
  430. select {
  431. case <-ctx.Done():
  432. case <-runDone:
  433. }
  434. ln.Close()
  435. }()
  436. s.startBackendIfNeeded()
  437. systemd.Ready()
  438. hs := &http.Server{
  439. Handler: http.HandlerFunc(s.serveHTTP),
  440. BaseContext: func(_ net.Listener) context.Context { return ctx },
  441. ConnContext: func(ctx context.Context, c net.Conn) context.Context {
  442. ci, err := ipnauth.GetConnIdentity(s.logf, c)
  443. if err != nil {
  444. return context.WithValue(ctx, connIdentityContextKey{}, err)
  445. }
  446. return context.WithValue(ctx, connIdentityContextKey{}, ci)
  447. },
  448. // Localhost connections are cheap; so only do
  449. // keep-alives for a short period of time, as these
  450. // active connections lock the server into only serving
  451. // that user. If the user has this page open, we don't
  452. // want another switching user to be locked out for
  453. // minutes. 5 seconds is enough to let browser hit
  454. // favicon.ico and such.
  455. IdleTimeout: 5 * time.Second,
  456. ErrorLog: logger.StdLogger(logger.WithPrefix(s.logf, "ipnserver: ")),
  457. }
  458. if err := hs.Serve(ln); err != nil {
  459. if err := ctx.Err(); err != nil {
  460. return err
  461. }
  462. return err
  463. }
  464. return nil
  465. }
  466. // ServeHTMLStatus serves an HTML status page at http://localhost:41112/ for
  467. // Windows and via $DEBUG_LISTENER/debug/ipn when tailscaled's --debug flag
  468. // is used to run a debug server.
  469. func (s *Server) ServeHTMLStatus(w http.ResponseWriter, r *http.Request) {
  470. lb := s.lb.Load()
  471. if lb == nil {
  472. http.Error(w, "no LocalBackend", http.StatusServiceUnavailable)
  473. return
  474. }
  475. // As this is only meant for debug, verify there's no DNS name being used to
  476. // access this.
  477. if !strings.HasPrefix(r.Host, "localhost:") && strings.IndexFunc(r.Host, unicode.IsLetter) != -1 {
  478. http.Error(w, "invalid host", http.StatusForbidden)
  479. return
  480. }
  481. w.Header().Set("Content-Security-Policy", `default-src 'none'; frame-ancestors 'none'; script-src 'none'; script-src-elem 'none'; script-src-attr 'none'`)
  482. w.Header().Set("X-Frame-Options", "DENY")
  483. w.Header().Set("X-Content-Type-Options", "nosniff")
  484. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  485. st := lb.Status()
  486. // TODO(bradfitz): add LogID and opts to st?
  487. st.WriteHTML(w)
  488. }