2015-09-08 00:54:24 +02:00
|
|
|
package pathfs_frontend
|
|
|
|
|
|
|
|
import (
|
2015-10-04 14:36:20 +02:00
|
|
|
"fmt"
|
2015-09-08 00:54:24 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
|
|
|
"github.com/hanwen/go-fuse/fuse/nodefs"
|
|
|
|
"github.com/hanwen/go-fuse/fuse/pathfs"
|
|
|
|
"github.com/rfjakob/gocryptfs/cryptfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FS struct {
|
|
|
|
*cryptfs.CryptFS
|
2015-10-04 14:36:20 +02:00
|
|
|
pathfs.FileSystem // loopbackFileSystem
|
|
|
|
backing string // Backing directory
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encrypted FUSE overlay filesystem
|
2015-11-03 00:00:13 +01:00
|
|
|
func NewFS(key []byte, backing string, useOpenssl bool, plaintextNames bool) *FS {
|
2015-09-08 00:54:24 +02:00
|
|
|
return &FS{
|
2015-11-03 00:00:13 +01:00
|
|
|
CryptFS: cryptfs.NewCryptFS(key, useOpenssl, plaintextNames),
|
2015-10-04 14:36:20 +02:00
|
|
|
FileSystem: pathfs.NewLoopbackFileSystem(backing),
|
|
|
|
backing: backing,
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPath - get the absolute path of the backing file
|
|
|
|
func (fs *FS) GetPath(relPath string) string {
|
|
|
|
return filepath.Join(fs.backing, fs.EncryptPath(relPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) GetAttr(name string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
|
2015-09-16 19:32:37 +02:00
|
|
|
cryptfs.Debug.Printf("FS.GetAttr('%s')\n", name)
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(name) {
|
|
|
|
return nil, fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-09-16 19:32:37 +02:00
|
|
|
cName := fs.EncryptPath(name)
|
|
|
|
a, status := fs.FileSystem.GetAttr(cName, context)
|
2015-09-08 00:54:24 +02:00
|
|
|
if a == nil {
|
2015-09-17 22:08:49 +02:00
|
|
|
cryptfs.Debug.Printf("FS.GetAttr failed: %s\n", status.String())
|
2015-09-08 00:54:24 +02:00
|
|
|
return a, status
|
|
|
|
}
|
2015-09-16 19:32:37 +02:00
|
|
|
if a.IsRegular() {
|
2015-11-01 12:11:36 +01:00
|
|
|
a.Size = fs.CipherSizeToPlainSize(a.Size)
|
2015-09-16 19:32:37 +02:00
|
|
|
} else if a.IsSymlink() {
|
|
|
|
target, _ := fs.Readlink(name, context)
|
|
|
|
a.Size = uint64(len(target))
|
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
return a, status
|
|
|
|
}
|
|
|
|
|
2015-09-13 22:09:48 +02:00
|
|
|
func (fs *FS) OpenDir(dirName string, context *fuse.Context) ([]fuse.DirEntry, fuse.Status) {
|
2015-09-16 19:32:37 +02:00
|
|
|
cryptfs.Debug.Printf("OpenDir(%s)\n", dirName)
|
2015-10-04 14:36:20 +02:00
|
|
|
cipherEntries, status := fs.FileSystem.OpenDir(fs.EncryptPath(dirName), context)
|
2015-09-08 00:54:24 +02:00
|
|
|
var plain []fuse.DirEntry
|
|
|
|
if cipherEntries != nil {
|
|
|
|
for i := range cipherEntries {
|
2015-09-13 22:09:48 +02:00
|
|
|
cName := cipherEntries[i].Name
|
2015-11-03 00:00:13 +01:00
|
|
|
if dirName == "" && cName == cryptfs.ConfDefaultName {
|
2015-11-03 22:25:29 +01:00
|
|
|
// silently ignore "gocryptfs.conf" in the top level dir
|
2015-11-03 00:00:13 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-09-13 22:09:48 +02:00
|
|
|
name, err := fs.DecryptPath(cName)
|
2015-09-08 00:54:24 +02:00
|
|
|
if err != nil {
|
2015-09-13 22:09:48 +02:00
|
|
|
fmt.Printf("Invalid name \"%s\" in dir \"%s\": %s\n", cName, name, err)
|
2015-09-08 00:54:24 +02:00
|
|
|
continue
|
|
|
|
}
|
2015-09-13 22:09:48 +02:00
|
|
|
cipherEntries[i].Name = name
|
2015-09-08 00:54:24 +02:00
|
|
|
plain = append(plain, cipherEntries[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return plain, status
|
|
|
|
}
|
|
|
|
|
|
|
|
// We always need read access to do read-modify-write cycles
|
|
|
|
func (fs *FS) mangleOpenFlags(flags uint32) (newFlags int, writeOnly bool) {
|
|
|
|
newFlags = int(flags)
|
2015-10-04 14:36:20 +02:00
|
|
|
if newFlags&os.O_WRONLY > 0 {
|
2015-09-08 00:54:24 +02:00
|
|
|
writeOnly = true
|
|
|
|
newFlags = newFlags ^ os.O_WRONLY | os.O_RDWR
|
|
|
|
}
|
|
|
|
// We also cannot open the file in append mode, we need to seek back for RMW
|
|
|
|
newFlags = newFlags &^ os.O_APPEND
|
|
|
|
|
|
|
|
return newFlags, writeOnly
|
|
|
|
}
|
|
|
|
|
2015-11-03 00:00:13 +01:00
|
|
|
func (fs *FS) Open(path string, flags uint32, context *fuse.Context) (fuseFile nodefs.File, status fuse.Status) {
|
|
|
|
cryptfs.Debug.Printf("Open(%s)\n", path)
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(path){
|
2015-11-03 00:00:13 +01:00
|
|
|
return nil, fuse.EPERM
|
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
iflags, writeOnly := fs.mangleOpenFlags(flags)
|
2015-11-03 00:00:13 +01:00
|
|
|
f, err := os.OpenFile(fs.GetPath(path), iflags, 0666)
|
2015-09-08 00:54:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewFile(f, writeOnly, fs.CryptFS), fuse.OK
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Create(path string, flags uint32, mode uint32, context *fuse.Context) (fuseFile nodefs.File, code fuse.Status) {
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(path){
|
|
|
|
return nil, fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
iflags, writeOnly := fs.mangleOpenFlags(flags)
|
|
|
|
f, err := os.OpenFile(fs.GetPath(path), iflags|os.O_CREATE, os.FileMode(mode))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
return NewFile(f, writeOnly, fs.CryptFS), fuse.OK
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Chmod(path string, mode uint32, context *fuse.Context) (code fuse.Status) {
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(path){
|
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
return fs.FileSystem.Chmod(fs.EncryptPath(path), mode, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Chown(path string, uid uint32, gid uint32, context *fuse.Context) (code fuse.Status) {
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(path){
|
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-09-18 22:22:11 +02:00
|
|
|
return fs.FileSystem.Chown(fs.EncryptPath(path), uid, gid, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Mknod(name string, mode uint32, dev uint32, context *fuse.Context) (code fuse.Status) {
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(name){
|
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-09-18 22:22:11 +02:00
|
|
|
return fs.FileSystem.Mknod(fs.EncryptPath(name), mode, dev, context)
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Truncate(path string, offset uint64, context *fuse.Context) (code fuse.Status) {
|
2015-11-01 01:32:33 +01:00
|
|
|
cryptfs.Warn.Printf("Truncate of a closed file is not supported, returning ENOSYS\n")
|
|
|
|
return fuse.ENOSYS
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Utimens(path string, Atime *time.Time, Mtime *time.Time, context *fuse.Context) (code fuse.Status) {
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(path){
|
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
return fs.FileSystem.Utimens(fs.EncryptPath(path), Atime, Mtime, context)
|
|
|
|
}
|
|
|
|
|
2015-09-09 19:32:59 +02:00
|
|
|
func (fs *FS) Readlink(name string, context *fuse.Context) (out string, status fuse.Status) {
|
|
|
|
dst, status := fs.FileSystem.Readlink(fs.EncryptPath(name), context)
|
|
|
|
if status != fuse.OK {
|
|
|
|
return "", status
|
|
|
|
}
|
|
|
|
dstPlain, err := fs.DecryptPath(dst)
|
|
|
|
if err != nil {
|
|
|
|
cryptfs.Warn.Printf("Failed decrypting symlink: %s\n", err.Error())
|
|
|
|
return "", fuse.EIO
|
|
|
|
}
|
|
|
|
return dstPlain, status
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Mkdir(path string, mode uint32, context *fuse.Context) (code fuse.Status) {
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(path){
|
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
return fs.FileSystem.Mkdir(fs.EncryptPath(path), mode, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Unlink(name string, context *fuse.Context) (code fuse.Status) {
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(name){
|
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-09-16 19:32:37 +02:00
|
|
|
cName := fs.EncryptPath(name)
|
|
|
|
code = fs.FileSystem.Unlink(cName, context)
|
|
|
|
if code != fuse.OK {
|
2015-11-09 22:33:42 +01:00
|
|
|
cryptfs.Warn.Printf("Unlink failed on %s [%s], code=%s\n", name, cName, code.String())
|
2015-09-16 19:32:37 +02:00
|
|
|
}
|
|
|
|
return code
|
2015-09-08 00:54:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Rmdir(name string, context *fuse.Context) (code fuse.Status) {
|
|
|
|
return fs.FileSystem.Rmdir(fs.EncryptPath(name), context)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Symlink(pointedTo string, linkName string, context *fuse.Context) (code fuse.Status) {
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(linkName){
|
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
// TODO symlink encryption
|
2015-09-08 22:53:06 +02:00
|
|
|
cryptfs.Debug.Printf("Symlink(\"%s\", \"%s\")\n", pointedTo, linkName)
|
2015-09-08 00:54:24 +02:00
|
|
|
return fs.FileSystem.Symlink(fs.EncryptPath(pointedTo), fs.EncryptPath(linkName), context)
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:25:29 +01:00
|
|
|
func (fs *FS) Rename(oldPath string, newPath string, context *fuse.Context) (code fuse.Status) {
|
|
|
|
if fs.CryptFS.IsFiltered(newPath){
|
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
return fs.FileSystem.Rename(fs.EncryptPath(oldPath), fs.EncryptPath(newPath), context)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Link(orig string, newName string, context *fuse.Context) (code fuse.Status) {
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(newName){
|
|
|
|
return fuse.EPERM
|
2015-11-03 00:00:13 +01:00
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
return fs.FileSystem.Link(fs.EncryptPath(orig), fs.EncryptPath(newName), context)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Access(name string, mode uint32, context *fuse.Context) (code fuse.Status) {
|
2015-11-03 22:25:29 +01:00
|
|
|
if fs.CryptFS.IsFiltered(name){
|
|
|
|
return fuse.EPERM
|
|
|
|
}
|
2015-09-08 00:54:24 +02:00
|
|
|
return fs.FileSystem.Access(fs.EncryptPath(name), mode, context)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) GetXAttr(name string, attr string, context *fuse.Context) ([]byte, fuse.Status) {
|
|
|
|
return nil, fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) SetXAttr(name string, attr string, data []byte, flags int, context *fuse.Context) fuse.Status {
|
|
|
|
return fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) ListXAttr(name string, context *fuse.Context) ([]string, fuse.Status) {
|
|
|
|
return nil, fuse.ENOSYS
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) RemoveXAttr(name string, attr string, context *fuse.Context) fuse.Status {
|
|
|
|
return fuse.ENOSYS
|
|
|
|
}
|