2016-08-25 00:14:36 +02:00
|
|
|
package fusefrontend_reverse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2016-08-30 00:23:55 +02:00
|
|
|
"syscall"
|
2016-08-25 00:14:36 +02:00
|
|
|
|
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
|
|
|
"github.com/hanwen/go-fuse/fuse/nodefs"
|
|
|
|
"github.com/hanwen/go-fuse/fuse/pathfs"
|
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/contentenc"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/cryptocore"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/fusefrontend"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
|
|
|
)
|
|
|
|
|
2016-08-30 00:23:55 +02:00
|
|
|
type reverseFS struct {
|
|
|
|
// Embed pathfs.defaultFileSystem for a ENOSYS implementation of all methods
|
2016-08-25 00:14:36 +02:00
|
|
|
pathfs.FileSystem
|
2016-08-30 00:23:55 +02:00
|
|
|
// pathfs.loopbackFileSystem, see go-fuse/fuse/pathfs/loopback.go
|
|
|
|
loopbackfs pathfs.FileSystem
|
2016-08-25 00:14:36 +02:00
|
|
|
// Stores configuration arguments
|
|
|
|
args fusefrontend.Args
|
|
|
|
// Filename encryption helper
|
|
|
|
nameTransform *nametransform.NameTransform
|
|
|
|
// Content encryption helper
|
|
|
|
contentEnc *contentenc.ContentEnc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypted FUSE overlay filesystem
|
2016-08-30 00:23:55 +02:00
|
|
|
func NewFS(args fusefrontend.Args) *reverseFS {
|
2016-08-25 00:14:36 +02:00
|
|
|
cryptoCore := cryptocore.New(args.Masterkey, args.OpenSSL, true)
|
|
|
|
contentEnc := contentenc.New(cryptoCore, contentenc.DefaultBS)
|
|
|
|
nameTransform := nametransform.New(cryptoCore, args.LongNames)
|
|
|
|
|
2016-08-30 00:23:55 +02:00
|
|
|
return &reverseFS{
|
|
|
|
// pathfs.defaultFileSystem returns ENOSYS for all operations
|
|
|
|
FileSystem: pathfs.NewDefaultFileSystem(),
|
|
|
|
loopbackfs: pathfs.NewLoopbackFileSystem(args.Cipherdir),
|
2016-08-25 00:14:36 +02:00
|
|
|
args: args,
|
|
|
|
nameTransform: nameTransform,
|
|
|
|
contentEnc: contentEnc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 00:23:55 +02:00
|
|
|
func (rfs *reverseFS) GetAttr(relPath string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
|
|
|
|
if rfs.isFiltered(relPath) {
|
2016-08-25 00:14:36 +02:00
|
|
|
return nil, fuse.EPERM
|
|
|
|
}
|
2016-08-30 00:23:55 +02:00
|
|
|
relPath, err := rfs.decryptPath(relPath)
|
2016-08-25 00:14:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2016-08-30 00:23:55 +02:00
|
|
|
a, status := rfs.loopbackfs.GetAttr(relPath, context)
|
2016-08-25 00:14:36 +02:00
|
|
|
if a == nil {
|
|
|
|
return a, status
|
|
|
|
}
|
|
|
|
// Calculate encrypted file size
|
|
|
|
if a.IsRegular() {
|
2016-08-30 00:23:55 +02:00
|
|
|
a.Size = rfs.contentEnc.PlainSizeToCipherSize(a.Size)
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
return a, fuse.OK
|
|
|
|
}
|
|
|
|
|
2016-08-30 00:23:55 +02:00
|
|
|
func (rfs *reverseFS) Access(relPath string, mode uint32, context *fuse.Context) fuse.Status {
|
|
|
|
if rfs.isFiltered(relPath) {
|
|
|
|
return fuse.EPERM
|
|
|
|
}
|
|
|
|
cPath, err := rfs.abs(rfs.encryptPath(relPath))
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
return fuse.ToStatus(syscall.Access(cPath, mode))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rfs *reverseFS) Open(relPath string, flags uint32, context *fuse.Context) (fuseFile nodefs.File, status fuse.Status) {
|
|
|
|
if rfs.isFiltered(relPath) {
|
2016-08-25 00:14:36 +02:00
|
|
|
return nil, fuse.EPERM
|
|
|
|
}
|
2016-08-30 00:23:55 +02:00
|
|
|
absPath, err := rfs.abs(rfs.decryptPath(relPath))
|
2016-08-25 00:14:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(absPath, int(flags), 0666)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2016-08-30 00:23:55 +02:00
|
|
|
return NewFile(f, rfs.contentEnc)
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
|
2016-08-30 00:23:55 +02:00
|
|
|
func (rfs *reverseFS) OpenDir(relPath string, context *fuse.Context) ([]fuse.DirEntry, fuse.Status) {
|
|
|
|
relPath, err := rfs.decryptPath(relPath)
|
2016-08-25 00:14:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
// Read plaintext dir
|
2016-08-30 00:23:55 +02:00
|
|
|
entries, status := rfs.loopbackfs.OpenDir(relPath, context)
|
2016-08-25 00:14:36 +02:00
|
|
|
if entries == nil {
|
|
|
|
return nil, status
|
|
|
|
}
|
|
|
|
// Encrypt names
|
|
|
|
for i := range entries {
|
2016-08-30 00:23:55 +02:00
|
|
|
entries[i].Name, err = rfs.encryptPath(entries[i].Name)
|
2016-08-25 00:14:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return entries, fuse.OK
|
|
|
|
}
|