162 lines
3.9 KiB
Go
162 lines
3.9 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"golang.org/x/net/webdav"
|
|
)
|
|
|
|
// DriveShareAgentVersion is the first macOS agent that applies dynamic drive mounts.
|
|
const DriveShareAgentVersion = 4
|
|
|
|
// NormalizeDriveLetter accepts C, C:, or C:\ and returns the canonical letter C.
|
|
func NormalizeDriveLetter(raw string) (string, bool) {
|
|
s := strings.ToUpper(strings.TrimSpace(raw))
|
|
s = strings.TrimSuffix(s, `\`)
|
|
s = strings.TrimSuffix(s, ":")
|
|
if len(s) != 1 || s[0] < 'A' || s[0] > 'Z' {
|
|
return "", false
|
|
}
|
|
return s, true
|
|
}
|
|
|
|
func configuredDriveRoots(cfg VMConfig) map[string]string {
|
|
roots := make(map[string]string)
|
|
for _, raw := range cfg.SharedDrives {
|
|
drive, ok := NormalizeDriveLetter(raw)
|
|
if !ok {
|
|
continue
|
|
}
|
|
root := cfg.DriveRoots[drive]
|
|
if root == "" {
|
|
continue
|
|
}
|
|
roots[drive] = root
|
|
}
|
|
return roots
|
|
}
|
|
|
|
func cloneStringMap(src map[string]string) map[string]string {
|
|
if src == nil {
|
|
return nil
|
|
}
|
|
dst := make(map[string]string, len(src))
|
|
for k, v := range src {
|
|
dst[k] = v
|
|
}
|
|
return dst
|
|
}
|
|
|
|
func enabledDriveLetters(vm *registeredVM) []string {
|
|
out := []string{}
|
|
if vm == nil || !vm.driveSharingEnabled {
|
|
return out
|
|
}
|
|
for drive := range vm.sharedDriveRoots {
|
|
out = append(out, drive)
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
// readOnlyDriveFS exposes an existing fixed-drive root without ever creating it.
|
|
// Every read resolves junctions/symlinks and verifies that the final target stays
|
|
// inside the authorized root. All mutating filesystem operations are denied.
|
|
type readOnlyDriveFS struct {
|
|
rootAbs string
|
|
inner webdav.Dir
|
|
}
|
|
|
|
func newReadOnlyDriveFS(root string) (*readOnlyDriveFS, error) {
|
|
abs, err := filepath.Abs(root)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
info, err := os.Stat(abs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !info.IsDir() {
|
|
return nil, os.ErrInvalid
|
|
}
|
|
resolved, err := filepath.EvalSymlinks(abs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &readOnlyDriveFS{rootAbs: resolved, inner: webdav.Dir(resolved)}, nil
|
|
}
|
|
|
|
func pathWithinRoot(root, target string) bool {
|
|
rel, err := filepath.Rel(root, target)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return rel == "." || (rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator)) && !filepath.IsAbs(rel))
|
|
}
|
|
|
|
func (f *readOnlyDriveFS) validate(name string) error {
|
|
if strings.ContainsRune(name, 0) || strings.Contains(name, `\`) {
|
|
return os.ErrInvalid
|
|
}
|
|
clean := path.Clean("/" + name)
|
|
full := filepath.Join(f.rootAbs, filepath.FromSlash(strings.TrimPrefix(clean, "/")))
|
|
if !pathWithinRoot(f.rootAbs, full) {
|
|
return os.ErrPermission
|
|
}
|
|
resolved, err := filepath.EvalSymlinks(full)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !pathWithinRoot(f.rootAbs, resolved) {
|
|
return os.ErrPermission
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *readOnlyDriveFS) Mkdir(context.Context, string, os.FileMode) error {
|
|
return os.ErrPermission
|
|
}
|
|
|
|
func (f *readOnlyDriveFS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
|
|
const writeFlags = os.O_WRONLY | os.O_RDWR | os.O_APPEND | os.O_CREATE | os.O_EXCL | os.O_TRUNC
|
|
if flag&writeFlags != 0 {
|
|
return nil, os.ErrPermission
|
|
}
|
|
if err := f.validate(name); err != nil {
|
|
return nil, err
|
|
}
|
|
return f.inner.OpenFile(ctx, name, os.O_RDONLY, perm)
|
|
}
|
|
|
|
func (f *readOnlyDriveFS) RemoveAll(context.Context, string) error {
|
|
return os.ErrPermission
|
|
}
|
|
|
|
func (f *readOnlyDriveFS) Rename(context.Context, string, string) error {
|
|
return os.ErrPermission
|
|
}
|
|
|
|
func (f *readOnlyDriveFS) Stat(ctx context.Context, name string) (os.FileInfo, error) {
|
|
if err := f.validate(name); err != nil {
|
|
return nil, err
|
|
}
|
|
return f.inner.Stat(ctx, name)
|
|
}
|
|
|
|
func allowReadOnlyDAV(w http.ResponseWriter, r *http.Request) bool {
|
|
switch r.Method {
|
|
case http.MethodGet, http.MethodHead, http.MethodOptions, "PROPFIND":
|
|
return true
|
|
default:
|
|
w.Header().Set("Allow", "OPTIONS, GET, HEAD, PROPFIND")
|
|
http.Error(w, "read-only drive share", http.StatusMethodNotAllowed)
|
|
return false
|
|
}
|
|
}
|