2020-08-01 20:48:32 +02:00
|
|
|
package fusefrontend_reverse
|
|
|
|
|
|
|
|
import (
|
2020-08-01 21:13:00 +02:00
|
|
|
"context"
|
2020-08-09 22:11:46 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-08-08 18:45:47 +02:00
|
|
|
"path/filepath"
|
2020-08-01 21:13:00 +02:00
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2020-08-01 20:48:32 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
2020-08-01 21:13:00 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
|
|
|
|
2020-08-09 22:11:46 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/contentenc"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/pathiv"
|
2020-08-01 21:13:00 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2020-08-09 22:11:46 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2020-08-01 20:48:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Node is a file or directory in the filesystem tree
|
|
|
|
// in a `gocryptfs -reverse` mount.
|
|
|
|
type Node struct {
|
|
|
|
fs.Inode
|
|
|
|
}
|
2020-08-01 21:13:00 +02:00
|
|
|
|
|
|
|
// Lookup - FUSE call for discovering a file.
|
2020-08-08 18:45:47 +02:00
|
|
|
func (n *Node) Lookup(ctx context.Context, cName string, out *fuse.EntryOut) (ch *fs.Inode, errno syscall.Errno) {
|
2020-08-15 17:31:25 +02:00
|
|
|
var d *dirfdPlus
|
2020-08-08 18:45:47 +02:00
|
|
|
t := n.lookupFileType(cName)
|
2020-08-02 19:33:12 +02:00
|
|
|
if t == typeDiriv {
|
|
|
|
// gocryptfs.diriv
|
|
|
|
return n.lookupDiriv(ctx, out)
|
|
|
|
} else if t == typeName {
|
|
|
|
// gocryptfs.longname.*.name
|
2020-08-08 18:45:47 +02:00
|
|
|
return n.lookupLongnameName(ctx, cName, out)
|
2020-08-02 19:33:12 +02:00
|
|
|
} else if t == typeConfig {
|
|
|
|
// gocryptfs.conf
|
2020-08-09 22:11:46 +02:00
|
|
|
return n.lookupConf(ctx, out)
|
2020-08-02 13:25:53 +02:00
|
|
|
} else if t == typeReal {
|
|
|
|
// real file
|
2020-08-15 17:31:25 +02:00
|
|
|
d, errno = n.prepareAtSyscall(cName)
|
|
|
|
//fmt.Printf("Lookup: prepareAtSyscall -> d=%#v, errno=%d\n", d, errno)
|
2020-08-02 13:25:53 +02:00
|
|
|
if errno != 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-08-15 17:31:25 +02:00
|
|
|
defer syscall.Close(d.dirfd)
|
2020-08-01 21:13:00 +02:00
|
|
|
}
|
|
|
|
// Get device number and inode number into `st`
|
2020-08-15 17:31:25 +02:00
|
|
|
st, err := syscallcompat.Fstatat2(d.dirfd, d.pName, unix.AT_SYMLINK_NOFOLLOW)
|
2020-08-01 21:13:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fs.ToErrno(err)
|
|
|
|
}
|
|
|
|
// Create new inode and fill `out`
|
|
|
|
ch = n.newChild(ctx, st, out)
|
2020-08-02 19:33:12 +02:00
|
|
|
// Translate ciphertext size in `out.Attr.Size` to plaintext size
|
2020-08-02 13:25:53 +02:00
|
|
|
if t == typeReal {
|
2020-08-15 17:31:25 +02:00
|
|
|
n.translateSize(d.dirfd, cName, d.pName, &out.Attr)
|
2020-08-02 13:25:53 +02:00
|
|
|
}
|
2020-08-01 21:13:00 +02:00
|
|
|
return ch, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAttr - FUSE call for stat()ing a file.
|
|
|
|
//
|
|
|
|
// GetAttr is symlink-safe through use of openBackingDir() and Fstatat().
|
|
|
|
func (n *Node) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) (errno syscall.Errno) {
|
|
|
|
// If the kernel gives us a file handle, use it.
|
|
|
|
if f != nil {
|
|
|
|
return f.(fs.FileGetattrer).Getattr(ctx, out)
|
|
|
|
}
|
|
|
|
|
2020-08-15 17:31:25 +02:00
|
|
|
d, errno := n.prepareAtSyscall("")
|
2020-08-01 21:13:00 +02:00
|
|
|
if errno != 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-08-15 17:31:25 +02:00
|
|
|
defer syscall.Close(d.dirfd)
|
2020-08-01 21:13:00 +02:00
|
|
|
|
2020-08-15 17:31:25 +02:00
|
|
|
st, err := syscallcompat.Fstatat2(d.dirfd, d.pName, unix.AT_SYMLINK_NOFOLLOW)
|
2020-08-01 21:13:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return fs.ToErrno(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix inode number
|
|
|
|
rn := n.rootNode()
|
|
|
|
rn.inoMap.TranslateStat(st)
|
|
|
|
out.Attr.FromStat(st)
|
|
|
|
|
|
|
|
// Translate ciphertext size in `out.Attr.Size` to plaintext size
|
2020-08-08 18:45:47 +02:00
|
|
|
cName := filepath.Base(n.Path())
|
2020-08-15 17:31:25 +02:00
|
|
|
n.translateSize(d.dirfd, cName, d.pName, &out.Attr)
|
2020-08-01 21:13:00 +02:00
|
|
|
|
|
|
|
if rn.args.ForceOwner != nil {
|
|
|
|
out.Owner = *rn.args.ForceOwner
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
2020-08-08 18:45:47 +02:00
|
|
|
|
|
|
|
// Readlink - FUSE call.
|
|
|
|
//
|
|
|
|
// Symlink-safe through openBackingDir() + Readlinkat().
|
|
|
|
func (n *Node) Readlink(ctx context.Context) (out []byte, errno syscall.Errno) {
|
2020-08-15 17:31:25 +02:00
|
|
|
d, errno := n.prepareAtSyscall("")
|
2020-08-08 18:45:47 +02:00
|
|
|
if errno != 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-08-15 17:31:25 +02:00
|
|
|
defer syscall.Close(d.dirfd)
|
2020-08-08 18:45:47 +02:00
|
|
|
|
2020-08-15 17:31:25 +02:00
|
|
|
return n.readlink(d.dirfd, d.cName, d.pName)
|
2020-08-08 18:45:47 +02:00
|
|
|
}
|
2020-08-09 22:11:46 +02:00
|
|
|
|
|
|
|
// Open - FUSE call. Open already-existing file.
|
|
|
|
//
|
|
|
|
// Symlink-safe through Openat().
|
|
|
|
func (n *Node) Open(ctx context.Context, flags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
|
2020-08-15 17:31:25 +02:00
|
|
|
d, errno := n.prepareAtSyscall("")
|
2020-08-09 22:11:46 +02:00
|
|
|
if errno != 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-08-15 17:31:25 +02:00
|
|
|
defer syscall.Close(d.dirfd)
|
2020-08-09 22:11:46 +02:00
|
|
|
|
2020-08-15 17:31:25 +02:00
|
|
|
fd, err := syscallcompat.Openat(d.dirfd, d.pName, syscall.O_RDONLY|syscall.O_NOFOLLOW, 0)
|
2020-08-09 22:11:46 +02:00
|
|
|
if err != nil {
|
|
|
|
errno = fs.ToErrno(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject access if the file descriptor does not refer to a regular file.
|
|
|
|
var st syscall.Stat_t
|
|
|
|
err = syscall.Fstat(fd, &st)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("Open: Fstat error: %v", err)
|
|
|
|
syscall.Close(fd)
|
|
|
|
errno = fs.ToErrno(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var a fuse.Attr
|
|
|
|
a.FromStat(&st)
|
|
|
|
if !a.IsRegular() {
|
|
|
|
tlog.Warn.Printf("ino%d: newFile: not a regular file", st.Ino)
|
|
|
|
syscall.Close(fd)
|
|
|
|
errno = syscall.EACCES
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// See if we have that inode number already in the table
|
|
|
|
// (even if Nlink has dropped to 1)
|
|
|
|
var derivedIVs pathiv.FileIVs
|
|
|
|
v, found := inodeTable.Load(st.Ino)
|
|
|
|
if found {
|
|
|
|
tlog.Debug.Printf("ino%d: newFile: found in the inode table", st.Ino)
|
|
|
|
derivedIVs = v.(pathiv.FileIVs)
|
|
|
|
} else {
|
|
|
|
p := n.Path()
|
|
|
|
derivedIVs = pathiv.DeriveFile(p)
|
|
|
|
// Nlink > 1 means there is more than one path to this file.
|
|
|
|
// Store the derived values so we always return the same data,
|
|
|
|
// regardless of the path that is used to access the file.
|
|
|
|
// This means that the first path wins.
|
|
|
|
if st.Nlink > 1 {
|
|
|
|
v, found = inodeTable.LoadOrStore(st.Ino, derivedIVs)
|
|
|
|
if found {
|
|
|
|
// Another thread has stored a different value before we could.
|
|
|
|
derivedIVs = v.(pathiv.FileIVs)
|
|
|
|
} else {
|
|
|
|
tlog.Debug.Printf("ino%d: newFile: Nlink=%d, stored in the inode table", st.Ino, st.Nlink)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
header := contentenc.FileHeader{
|
|
|
|
Version: contentenc.CurrentVersion,
|
|
|
|
ID: derivedIVs.ID,
|
|
|
|
}
|
|
|
|
fh = &File{
|
|
|
|
fd: os.NewFile(uintptr(fd), fmt.Sprintf("fd%d", fd)),
|
|
|
|
header: header,
|
|
|
|
block0IV: derivedIVs.Block0IV,
|
|
|
|
contentEnc: n.rootNode().contentEnc,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-08-16 19:41:00 +02:00
|
|
|
|
|
|
|
// StatFs - FUSE call. Returns information about the filesystem.
|
|
|
|
//
|
|
|
|
// Symlink-safe because the path is ignored.
|
|
|
|
func (n *Node) Statfs(ctx context.Context, out *fuse.StatfsOut) syscall.Errno {
|
|
|
|
p := n.rootNode().args.Cipherdir
|
|
|
|
var st syscall.Statfs_t
|
|
|
|
err := syscall.Statfs(p, &st)
|
|
|
|
if err != nil {
|
|
|
|
return fs.ToErrno(err)
|
|
|
|
}
|
|
|
|
out.FromStatfsT(&st)
|
|
|
|
return 0
|
|
|
|
}
|