326 lines
7.3 KiB
Go
326 lines
7.3 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
|
|
"golang.org/x/net/webdav"
|
|
)
|
|
|
|
type rootWebDAVFS struct {
|
|
root sharedRoot
|
|
readOnly bool
|
|
createRoot bool
|
|
verify func() error
|
|
verifyRoot func(*os.Root) error
|
|
accessMu *sync.RWMutex
|
|
}
|
|
|
|
func newRootWebDAVFS(root sharedRoot, readOnly, createRoot bool, verify func() error) (*rootWebDAVFS, error) {
|
|
f := &rootWebDAVFS{root: root, readOnly: readOnly, createRoot: createRoot, verify: verify}
|
|
if err := f.probe(); err != nil {
|
|
return nil, err
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
func (f *rootWebDAVFS) probe() error {
|
|
if f.root.path == "" && f.root.err == nil {
|
|
return os.ErrInvalid
|
|
}
|
|
r, _, err := f.open("/", false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return r.Close()
|
|
}
|
|
|
|
type sharedRoot struct {
|
|
path string
|
|
canonical string
|
|
err error
|
|
}
|
|
|
|
func configureSharedRoot(name string) sharedRoot {
|
|
abs, err := filepath.Abs(name)
|
|
if err != nil {
|
|
return sharedRoot{err: err}
|
|
}
|
|
canonical, err := canonicalSharedPath(abs)
|
|
if err != nil {
|
|
return sharedRoot{err: err}
|
|
}
|
|
// Normalize existing parent aliases such as /var, but reject a link at
|
|
// the share directory itself.
|
|
if parent := filepath.Dir(abs); parent != abs {
|
|
r, err := os.OpenRoot(parent)
|
|
if err == nil {
|
|
_, err = statDAVEntry(r, filepath.Base(abs))
|
|
r.Close()
|
|
}
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return sharedRoot{err: err}
|
|
}
|
|
}
|
|
return sharedRoot{path: abs, canonical: canonical}
|
|
}
|
|
|
|
// Resolve the existing portion of a path before the share has been created.
|
|
func canonicalSharedPath(name string) (string, error) {
|
|
resolved, err := canonicalExistingPath(name)
|
|
parent := filepath.Dir(name)
|
|
if !errors.Is(err, os.ErrNotExist) || parent == name {
|
|
return resolved, err
|
|
}
|
|
resolved, err = canonicalSharedPath(parent)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(resolved, filepath.Base(name)), nil
|
|
}
|
|
|
|
func openSharedRoot(root sharedRoot, createRoot bool) (*os.Root, error) {
|
|
if root.err != nil {
|
|
return nil, root.err
|
|
}
|
|
r, err := os.OpenRoot(root.path)
|
|
if errors.Is(err, os.ErrNotExist) && createRoot {
|
|
resolved, resolveErr := canonicalSharedPath(root.path)
|
|
if resolveErr != nil {
|
|
return nil, resolveErr
|
|
}
|
|
if resolved != root.canonical {
|
|
return nil, os.ErrPermission
|
|
}
|
|
if err := os.MkdirAll(root.canonical, 0o755); err != nil {
|
|
return nil, err
|
|
}
|
|
r, err = os.OpenRoot(root.path)
|
|
}
|
|
if err != nil || root.canonical == "" {
|
|
return r, err
|
|
}
|
|
return verifySharedRoot(r, root.canonical)
|
|
}
|
|
|
|
func verifySharedRoot(root *os.Root, canonical string) (*os.Root, error) {
|
|
if err := checkSharedRootPath(root, canonical); err != nil {
|
|
root.Close()
|
|
return nil, err
|
|
}
|
|
return root, nil
|
|
}
|
|
|
|
func davRelativePath(name string) (string, error) {
|
|
if strings.ContainsAny(name, "\x00\\") {
|
|
return "", os.ErrInvalid
|
|
}
|
|
clean := path.Clean("/" + name)
|
|
if clean == "/" {
|
|
return ".", nil
|
|
}
|
|
return strings.TrimPrefix(clean, "/"), nil
|
|
}
|
|
|
|
func (f *rootWebDAVFS) open(name string, write bool) (*os.Root, string, error) {
|
|
if f.verify != nil {
|
|
if err := f.verify(); err != nil {
|
|
return nil, "", err
|
|
}
|
|
}
|
|
if write && f.readOnly {
|
|
return nil, "", os.ErrPermission
|
|
}
|
|
name, err := davRelativePath(name)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
if write && name == "." {
|
|
return nil, "", os.ErrPermission
|
|
}
|
|
r, err := openSharedRoot(f.root, f.createRoot)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
if f.verifyRoot != nil {
|
|
if err := f.verifyRoot(r); err != nil {
|
|
r.Close()
|
|
return nil, "", err
|
|
}
|
|
}
|
|
return r, name, nil
|
|
}
|
|
|
|
func (f *rootWebDAVFS) Mkdir(_ context.Context, name string, perm os.FileMode) error {
|
|
if f.accessMu != nil {
|
|
f.accessMu.RLock()
|
|
defer f.accessMu.RUnlock()
|
|
}
|
|
r, name, err := f.open(name, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Close()
|
|
return r.Mkdir(name, perm)
|
|
}
|
|
|
|
func (f *rootWebDAVFS) OpenFile(_ 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
|
|
write := flag&writeFlags != 0
|
|
if write && f.accessMu != nil {
|
|
f.accessMu.RLock()
|
|
defer f.accessMu.RUnlock()
|
|
}
|
|
r, name, err := f.open(name, write)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer r.Close()
|
|
if !f.readOnly {
|
|
if _, err := statDAVEntry(r, name); err != nil && !os.IsNotExist(err) {
|
|
return nil, err
|
|
}
|
|
}
|
|
file, err := r.OpenFile(name, flag, perm)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if write && f.accessMu != nil {
|
|
return &verifiedDAVFile{File: file, fs: f}, nil
|
|
}
|
|
return file, nil
|
|
}
|
|
|
|
type verifiedDAVFile struct {
|
|
webdav.File
|
|
fs *rootWebDAVFS
|
|
}
|
|
|
|
func (f *verifiedDAVFile) Write(p []byte) (int, error) {
|
|
f.fs.accessMu.RLock()
|
|
defer f.fs.accessMu.RUnlock()
|
|
if err := f.fs.verify(); err != nil {
|
|
return 0, err
|
|
}
|
|
return f.File.Write(p)
|
|
}
|
|
|
|
func (f *rootWebDAVFS) RemoveAll(_ context.Context, name string) error {
|
|
if f.accessMu != nil {
|
|
f.accessMu.RLock()
|
|
defer f.accessMu.RUnlock()
|
|
}
|
|
r, name, err := f.open(name, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Close()
|
|
if _, err := statDAVEntry(r, name); err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
return r.RemoveAll(name)
|
|
}
|
|
|
|
func (f *rootWebDAVFS) Rename(_ context.Context, oldName, newName string) error {
|
|
if f.accessMu != nil {
|
|
f.accessMu.RLock()
|
|
defer f.accessMu.RUnlock()
|
|
}
|
|
r, oldName, err := f.open(oldName, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Close()
|
|
newName, err = davRelativePath(newName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if newName == "." {
|
|
return os.ErrPermission
|
|
}
|
|
if _, err := statDAVEntry(r, oldName); err != nil {
|
|
return err
|
|
}
|
|
if _, err := statDAVEntry(r, newName); err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
return r.Rename(oldName, newName)
|
|
}
|
|
|
|
func (f *rootWebDAVFS) Stat(_ context.Context, name string) (os.FileInfo, error) {
|
|
r, name, err := f.open(name, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer r.Close()
|
|
if f.readOnly {
|
|
return r.Stat(name)
|
|
}
|
|
return statDAVEntry(r, name)
|
|
}
|
|
|
|
// Keep links out of recursive WebDAV copies and reject junctions as entries.
|
|
func statDAVEntry(r *os.Root, name string) (os.FileInfo, error) {
|
|
info, err := r.Lstat(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
return nil, os.ErrPermission
|
|
}
|
|
if info.Mode()&os.ModeIrregular != 0 {
|
|
if err := checkIrregularEntry(r, name); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return info, nil
|
|
}
|
|
|
|
func (f *rootWebDAVFS) checkTransfer(src, dst string) error {
|
|
r, src, err := f.open(src, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer r.Close()
|
|
from, err := statDAVEntry(r, src)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return os.ErrNotExist
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
to, err := statDAVEntry(r, dst)
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return err
|
|
}
|
|
if to != nil && os.SameFile(from, to) {
|
|
return os.ErrPermission
|
|
}
|
|
if err := rejectDAVPathIdentity(r, path.Dir(dst), from); err != nil {
|
|
return err
|
|
}
|
|
if to == nil {
|
|
return nil
|
|
}
|
|
return rejectDAVPathIdentity(r, path.Dir(src), to)
|
|
}
|
|
|
|
// The filesystem can recognize aliases that URL path comparison cannot.
|
|
func rejectDAVPathIdentity(r *os.Root, name string, entry os.FileInfo) error {
|
|
for ; name != "."; name = path.Dir(name) {
|
|
to, err := statDAVEntry(r, name)
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
if err == nil && os.SameFile(entry, to) {
|
|
return os.ErrPermission
|
|
}
|
|
}
|
|
return nil
|
|
}
|