serve.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build !ts_omit_serve
  4. package localapi
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "net/http"
  10. "runtime"
  11. "tailscale.com/ipn"
  12. "tailscale.com/ipn/ipnlocal"
  13. "tailscale.com/util/httpm"
  14. "tailscale.com/version"
  15. )
  16. func init() {
  17. Register("serve-config", (*Handler).serveServeConfig)
  18. }
  19. func (h *Handler) serveServeConfig(w http.ResponseWriter, r *http.Request) {
  20. switch r.Method {
  21. case httpm.GET:
  22. if !h.PermitRead {
  23. http.Error(w, "serve config denied", http.StatusForbidden)
  24. return
  25. }
  26. config, etag, err := h.b.ServeConfigETag()
  27. if err != nil {
  28. http.Error(w, err.Error(), http.StatusInternalServerError)
  29. return
  30. }
  31. bts, err := json.Marshal(config)
  32. if err != nil {
  33. http.Error(w, "error encoding config: "+err.Error(), http.StatusInternalServerError)
  34. return
  35. }
  36. w.Header().Set("Etag", etag)
  37. w.Header().Set("Content-Type", "application/json")
  38. w.Write(bts)
  39. case httpm.POST:
  40. if !h.PermitWrite {
  41. http.Error(w, "serve config denied", http.StatusForbidden)
  42. return
  43. }
  44. configIn := new(ipn.ServeConfig)
  45. if err := json.NewDecoder(r.Body).Decode(configIn); err != nil {
  46. WriteErrorJSON(w, fmt.Errorf("decoding config: %w", err))
  47. return
  48. }
  49. // require a local admin when setting a path handler
  50. // TODO: roll-up this Windows-specific check into either PermitWrite
  51. // or a global admin escalation check.
  52. if err := authorizeServeConfigForGOOSAndUserContext(runtime.GOOS, configIn, h); err != nil {
  53. http.Error(w, err.Error(), http.StatusUnauthorized)
  54. return
  55. }
  56. etag := r.Header.Get("If-Match")
  57. if err := h.b.SetServeConfig(configIn, etag); err != nil {
  58. if errors.Is(err, ipnlocal.ErrETagMismatch) {
  59. http.Error(w, err.Error(), http.StatusPreconditionFailed)
  60. return
  61. }
  62. WriteErrorJSON(w, fmt.Errorf("updating config: %w", err))
  63. return
  64. }
  65. w.WriteHeader(http.StatusOK)
  66. default:
  67. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  68. }
  69. }
  70. func authorizeServeConfigForGOOSAndUserContext(goos string, configIn *ipn.ServeConfig, h *Handler) error {
  71. switch goos {
  72. case "windows", "linux", "darwin", "illumos", "solaris":
  73. default:
  74. return nil
  75. }
  76. // Only check for local admin on tailscaled-on-mac (based on "sudo"
  77. // permissions). On sandboxed variants (MacSys and AppStore), tailscaled
  78. // cannot serve files outside of the sandbox and this check is not
  79. // relevant.
  80. if goos == "darwin" && version.IsSandboxedMacOS() {
  81. return nil
  82. }
  83. if !configIn.HasPathHandler() {
  84. return nil
  85. }
  86. if h.Actor.IsLocalAdmin(h.b.OperatorUserID()) {
  87. return nil
  88. }
  89. switch goos {
  90. case "windows":
  91. return errors.New("must be a Windows local admin to serve a path")
  92. case "linux", "darwin", "illumos", "solaris":
  93. return errors.New("must be root, or be an operator and able to run 'sudo tailscale' to serve a path")
  94. default:
  95. // We filter goos at the start of the func, this default case
  96. // should never happen.
  97. panic("unreachable")
  98. }
  99. }