localapi_drive.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build !ts_omit_drive
  4. package localapi
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "io"
  9. "net/http"
  10. "os"
  11. "path"
  12. "tailscale.com/drive"
  13. "tailscale.com/util/httpm"
  14. )
  15. func init() {
  16. Register("drive/fileserver-address", (*Handler).serveDriveServerAddr)
  17. Register("drive/shares", (*Handler).serveShares)
  18. }
  19. // serveDriveServerAddr handles updates of the Taildrive file server address.
  20. func (h *Handler) serveDriveServerAddr(w http.ResponseWriter, r *http.Request) {
  21. if r.Method != httpm.PUT {
  22. http.Error(w, "only PUT allowed", http.StatusMethodNotAllowed)
  23. return
  24. }
  25. b, err := io.ReadAll(r.Body)
  26. if err != nil {
  27. http.Error(w, err.Error(), http.StatusBadRequest)
  28. return
  29. }
  30. h.b.DriveSetServerAddr(string(b))
  31. w.WriteHeader(http.StatusCreated)
  32. }
  33. // serveShares handles the management of Taildrive shares.
  34. //
  35. // PUT - adds or updates an existing share
  36. // DELETE - removes a share
  37. // GET - gets a list of all shares, sorted by name
  38. // POST - renames an existing share
  39. func (h *Handler) serveShares(w http.ResponseWriter, r *http.Request) {
  40. if !h.b.DriveSharingEnabled() {
  41. http.Error(w, `taildrive sharing not enabled, please add the attribute "drive:share" to this node in your ACLs' "nodeAttrs" section`, http.StatusForbidden)
  42. return
  43. }
  44. switch r.Method {
  45. case httpm.PUT:
  46. var share drive.Share
  47. err := json.NewDecoder(r.Body).Decode(&share)
  48. if err != nil {
  49. http.Error(w, err.Error(), http.StatusBadRequest)
  50. return
  51. }
  52. share.Path = path.Clean(share.Path)
  53. fi, err := os.Stat(share.Path)
  54. if err != nil {
  55. http.Error(w, err.Error(), http.StatusBadRequest)
  56. return
  57. }
  58. if !fi.IsDir() {
  59. http.Error(w, "not a directory", http.StatusBadRequest)
  60. return
  61. }
  62. if drive.AllowShareAs() {
  63. // share as the connected user
  64. username, err := h.Actor.Username()
  65. if err != nil {
  66. http.Error(w, err.Error(), http.StatusInternalServerError)
  67. return
  68. }
  69. share.As = username
  70. }
  71. err = h.b.DriveSetShare(&share)
  72. if err != nil {
  73. if errors.Is(err, drive.ErrInvalidShareName) {
  74. http.Error(w, "invalid share name", http.StatusBadRequest)
  75. return
  76. }
  77. http.Error(w, err.Error(), http.StatusInternalServerError)
  78. return
  79. }
  80. w.WriteHeader(http.StatusCreated)
  81. case httpm.DELETE:
  82. b, err := io.ReadAll(r.Body)
  83. if err != nil {
  84. http.Error(w, err.Error(), http.StatusBadRequest)
  85. return
  86. }
  87. err = h.b.DriveRemoveShare(string(b))
  88. if err != nil {
  89. if os.IsNotExist(err) {
  90. http.Error(w, "share not found", http.StatusNotFound)
  91. return
  92. }
  93. http.Error(w, err.Error(), http.StatusInternalServerError)
  94. return
  95. }
  96. w.WriteHeader(http.StatusNoContent)
  97. case httpm.POST:
  98. var names [2]string
  99. err := json.NewDecoder(r.Body).Decode(&names)
  100. if err != nil {
  101. http.Error(w, err.Error(), http.StatusBadRequest)
  102. return
  103. }
  104. err = h.b.DriveRenameShare(names[0], names[1])
  105. if err != nil {
  106. if os.IsNotExist(err) {
  107. http.Error(w, "share not found", http.StatusNotFound)
  108. return
  109. }
  110. if os.IsExist(err) {
  111. http.Error(w, "share name already used", http.StatusBadRequest)
  112. return
  113. }
  114. if errors.Is(err, drive.ErrInvalidShareName) {
  115. http.Error(w, "invalid share name", http.StatusBadRequest)
  116. return
  117. }
  118. http.Error(w, err.Error(), http.StatusInternalServerError)
  119. return
  120. }
  121. w.WriteHeader(http.StatusNoContent)
  122. case httpm.GET:
  123. shares := h.b.DriveGetShares()
  124. err := json.NewEncoder(w).Encode(shares)
  125. if err != nil {
  126. http.Error(w, err.Error(), http.StatusInternalServerError)
  127. return
  128. }
  129. default:
  130. http.Error(w, "unsupported method", http.StatusMethodNotAllowed)
  131. }
  132. }