feat(guesttools): add custom shared folders with volume checks, agent v9, and settings UI
211 lines
5.9 KiB
Go
211 lines
5.9 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"golang.org/x/net/webdav"
|
|
)
|
|
|
|
type davCacheEntry struct {
|
|
handler *webdav.Handler
|
|
root string
|
|
readOnly bool
|
|
generation uint64
|
|
}
|
|
|
|
// handleWebDAV serves RFC 4918 WebDAV at /dav/<vmName>/... authenticated via
|
|
// HTTP Basic (username "reims", password = per-VM 256-bit token) or Bearer token.
|
|
func (s *Server) handleWebDAV(w http.ResponseWriter, r *http.Request) {
|
|
vm := s.authenticate(r)
|
|
if vm == nil {
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="ReimsShare"`)
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
trimmed := strings.TrimPrefix(r.URL.Path, "/dav/")
|
|
vmPart, _, _ := strings.Cut(trimmed, "/")
|
|
if vmPart != vm.name {
|
|
http.Error(w, "forbidden", http.StatusForbidden)
|
|
return
|
|
}
|
|
if drivePath, ok := strings.CutPrefix(trimmed, vm.name+"/drives/"); ok {
|
|
s.handleDriveWebDAV(w, r, vm, drivePath)
|
|
return
|
|
}
|
|
if vm.sharedDir == "" {
|
|
http.Error(w, "shared directory not configured", http.StatusNotFound)
|
|
return
|
|
}
|
|
if !allowPROPFINDDepth(w, r) {
|
|
return
|
|
}
|
|
|
|
var src, dst string
|
|
if r.Method == "MOVE" || r.Method == "COPY" {
|
|
var status int
|
|
src, dst, status = davTransferPaths(r, "/dav/"+vm.name, false)
|
|
if status != http.StatusOK {
|
|
http.Error(w, http.StatusText(status), status)
|
|
return
|
|
}
|
|
}
|
|
handler, err := s.getOrCreateDAVHandler(vm, "")
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if dst != "" {
|
|
if err := handler.FileSystem.(*rootWebDAVFS).checkTransfer(src, dst); err != nil {
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
http.Error(w, "invalid transfer paths", http.StatusForbidden)
|
|
return
|
|
}
|
|
}
|
|
handler.ServeHTTP(w, r)
|
|
}
|
|
|
|
func davTransferPaths(r *http.Request, prefix string, driveShare bool) (string, string, int) {
|
|
destination, err := url.Parse(r.Header.Get("Destination"))
|
|
if err != nil || destination.Path == "" {
|
|
return "", "", http.StatusBadRequest
|
|
}
|
|
src, status := davTransferPath(r.URL.Path, prefix, driveShare)
|
|
if status != http.StatusOK {
|
|
return "", "", status
|
|
}
|
|
dst, status := davTransferPath(destination.Path, prefix, driveShare)
|
|
if status != http.StatusOK {
|
|
return "", "", status
|
|
}
|
|
if src == "." || dst == "." || dst == src || strings.HasPrefix(dst, src+"/") || strings.HasPrefix(src, dst+"/") {
|
|
return "", "", http.StatusForbidden
|
|
}
|
|
if destination.Host != "" && destination.Host != r.Host {
|
|
return "", "", http.StatusBadGateway
|
|
}
|
|
return src, dst, http.StatusOK
|
|
}
|
|
|
|
func davTransferPath(requestPath, prefix string, driveShare bool) (string, int) {
|
|
rel, ok := strings.CutPrefix(requestPath, prefix)
|
|
if !ok || (rel != "" && !strings.HasPrefix(rel, "/")) {
|
|
return "", http.StatusForbidden
|
|
}
|
|
name, err := davRelativePath(rel)
|
|
if err != nil {
|
|
return "", http.StatusBadRequest
|
|
}
|
|
if path.Join(prefix, name) != path.Clean(requestPath) ||
|
|
(!driveShare && (strings.HasPrefix(rel, "/drives/") || name == "drives" || strings.HasPrefix(name, "drives/"))) {
|
|
return "", http.StatusForbidden
|
|
}
|
|
return name, http.StatusOK
|
|
}
|
|
|
|
func (s *Server) handleDriveWebDAV(w http.ResponseWriter, r *http.Request, vm *registeredVM, drivePath string) {
|
|
if !vm.driveSharingEnabled {
|
|
http.Error(w, "drive sharing disabled", http.StatusForbidden)
|
|
return
|
|
}
|
|
if !allowPROPFINDDepth(w, r) {
|
|
return
|
|
}
|
|
drive, _, _ := strings.Cut(drivePath, "/")
|
|
if canonical, ok := NormalizeDriveLetter(drive); !ok || drive != canonical {
|
|
http.Error(w, "invalid drive", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if _, ok := vm.sharedDriveRoots[drive]; !ok {
|
|
http.Error(w, "drive not shared", http.StatusForbidden)
|
|
return
|
|
}
|
|
if (r.Method == "MOVE" || r.Method == "COPY") && r.Header.Get("Destination") != "" {
|
|
_, _, status := davTransferPaths(r, "/dav/"+vm.name+"/drives/"+drive, true)
|
|
if status != http.StatusOK {
|
|
http.Error(w, http.StatusText(status), status)
|
|
return
|
|
}
|
|
}
|
|
if !allowReadOnlyDAV(w, r) {
|
|
return
|
|
}
|
|
handler, err := s.getOrCreateDAVHandler(vm, drive)
|
|
if err != nil {
|
|
http.Error(w, "drive unavailable", http.StatusNotFound)
|
|
return
|
|
}
|
|
handler.ServeHTTP(w, r)
|
|
}
|
|
|
|
func (s *Server) getOrCreateDAVHandler(vm *registeredVM, drive string) (*webdav.Handler, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
live := s.vms[vm.key]
|
|
if live == nil || live.token != vm.token {
|
|
return nil, errors.New("VM registration changed")
|
|
}
|
|
cacheKey, prefix, root := vm.key, "/dav/"+vm.name, vm.sharedDir
|
|
readOnly := drive != ""
|
|
guid := vm.driveVolumeGUIDs[drive]
|
|
if readOnly {
|
|
cacheKey += "/drives/" + drive
|
|
prefix += "/drives/" + drive
|
|
root = vm.sharedDriveRoots[drive]
|
|
if root == "" || !live.driveSharingEnabled || root != live.sharedDriveRoots[drive] ||
|
|
!strings.EqualFold(guid, live.driveVolumeGUIDs[drive]) {
|
|
return nil, errors.New("drive registration changed")
|
|
}
|
|
} else if root != live.sharedDir {
|
|
return nil, errors.New("shared directory registration changed")
|
|
}
|
|
if entry, ok := s.davCache[cacheKey]; ok && entry.root == root && entry.readOnly == readOnly {
|
|
return entry.handler, nil
|
|
}
|
|
share := vm.sharedRoot
|
|
if readOnly {
|
|
abs, err := filepath.Abs(root)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
share = sharedRoot{path: abs}
|
|
}
|
|
fsys, err := newRootWebDAVFS(share, readOnly, !readOnly, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if readOnly {
|
|
fsys.verifyRoot = func(opened *os.Root) error {
|
|
s.mu.RLock()
|
|
cur := s.vms[vm.key]
|
|
if cur == nil || cur.token != vm.token || !cur.driveSharingEnabled ||
|
|
root != cur.sharedDriveRoots[drive] || !strings.EqualFold(guid, cur.driveVolumeGUIDs[drive]) {
|
|
s.mu.RUnlock()
|
|
return os.ErrPermission
|
|
}
|
|
verify := cur.verifyDriveRoot
|
|
s.mu.RUnlock()
|
|
if verify != nil {
|
|
return verify(opened, root, guid)
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
handler := &webdav.Handler{
|
|
Prefix: prefix,
|
|
FileSystem: fsys,
|
|
LockSystem: webdav.NewMemLS(),
|
|
}
|
|
s.davCache[cacheKey] = davCacheEntry{handler: handler, root: root, readOnly: readOnly}
|
|
return handler, nil
|
|
}
|