2016-08-25 00:14:36 +02:00
|
|
|
package fusefrontend_reverse
|
|
|
|
|
|
|
|
import (
|
2016-09-25 18:01:24 +02:00
|
|
|
"encoding/base64"
|
2016-09-02 23:45:52 +02:00
|
|
|
"fmt"
|
2016-08-25 00:14:36 +02:00
|
|
|
"os"
|
2016-09-19 23:40:43 +02:00
|
|
|
"path/filepath"
|
2016-09-21 21:25:55 +02:00
|
|
|
"sync"
|
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"
|
|
|
|
|
2016-09-20 19:32:28 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/configfile"
|
2016-08-25 00:14:36 +02:00
|
|
|
"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-09-02 23:45:52 +02:00
|
|
|
const (
|
2016-10-02 06:14:18 +02:00
|
|
|
// DirIVMode is the mode to use for Dir IV files.
|
2016-09-02 23:45:52 +02:00
|
|
|
DirIVMode = syscall.S_IFREG | 0400
|
|
|
|
)
|
|
|
|
|
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
|
2016-09-21 21:25:55 +02:00
|
|
|
// Inode number generator
|
|
|
|
inoGen *inoGenT
|
|
|
|
// Maps backing files device+inode pairs to user-facing unique inode numbers
|
|
|
|
inoMap map[devIno]uint64
|
|
|
|
// Protects map access
|
|
|
|
inoMapLock sync.Mutex
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
var _ pathfs.FileSystem = &reverseFS{}
|
|
|
|
|
|
|
|
// NewFS returns an encrypted FUSE overlay filesystem
|
|
|
|
func NewFS(args fusefrontend.Args) pathfs.FileSystem {
|
2016-10-05 22:22:28 +02:00
|
|
|
initLongnameCache()
|
|
|
|
|
2016-09-25 18:04:44 +02:00
|
|
|
cryptoCore := cryptocore.New(args.Masterkey, args.CryptoBackend, contentenc.DefaultIVBits)
|
2016-08-25 00:14:36 +02:00
|
|
|
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-10-02 06:14:18 +02:00
|
|
|
inoGen: newInoGen(),
|
2016-09-21 21:25:55 +02:00
|
|
|
inoMap: map[devIno]uint64{},
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-19 23:40:43 +02:00
|
|
|
// relDir is identical to filepath.Dir excepts that it returns "" when
|
|
|
|
// filepath.Dir would return ".".
|
|
|
|
// In the FUSE API, the root directory is called "", and we actually want that.
|
|
|
|
func relDir(path string) string {
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
if dir == "." {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return dir
|
|
|
|
}
|
|
|
|
|
|
|
|
// dirIVAttr handles GetAttr requests for the virtual gocryptfs.diriv files.
|
|
|
|
func (rfs *reverseFS) dirIVAttr(relPath string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
|
|
|
|
cDir := relDir(relPath)
|
|
|
|
dir, err := rfs.decryptPath(cDir)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("decrypt err %q\n", cDir)
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
// Does the parent dir exist?
|
|
|
|
a, status := rfs.loopbackfs.GetAttr(dir, context)
|
|
|
|
if !status.Ok() {
|
|
|
|
fmt.Printf("missing parent\n")
|
|
|
|
return nil, status
|
|
|
|
}
|
|
|
|
// Is it a dir at all?
|
|
|
|
if !a.IsDir() {
|
|
|
|
fmt.Printf("not isdir\n")
|
|
|
|
return nil, fuse.ENOTDIR
|
|
|
|
}
|
|
|
|
// Does the user have execute permissions?
|
|
|
|
if a.Mode&syscall.S_IXUSR == 0 {
|
|
|
|
fmt.Printf("not exec")
|
|
|
|
return nil, fuse.EPERM
|
2016-09-02 23:45:52 +02:00
|
|
|
}
|
2016-09-21 21:25:55 +02:00
|
|
|
// All good. Let's fake the file. We use the timestamps from the parent dir.
|
2016-09-19 23:40:43 +02:00
|
|
|
a.Mode = DirIVMode
|
|
|
|
a.Size = nametransform.DirIVLen
|
|
|
|
a.Nlink = 1
|
2016-09-21 21:25:55 +02:00
|
|
|
a.Ino = rfs.inoGen.next()
|
2016-09-02 23:45:52 +02:00
|
|
|
|
2016-09-19 23:40:43 +02:00
|
|
|
return a, fuse.OK
|
|
|
|
}
|
|
|
|
|
|
|
|
// isDirIV determines if the path points to a gocryptfs.diriv file
|
|
|
|
func isDirIV(relPath string) bool {
|
|
|
|
return filepath.Base(relPath) == nametransform.DirIVFilename
|
|
|
|
}
|
|
|
|
|
2016-09-22 23:28:11 +02:00
|
|
|
// isNameFile determines if the path points to a gocryptfs.longname.*.name
|
|
|
|
// file
|
|
|
|
func isNameFile(relPath string) bool {
|
|
|
|
fileType := nametransform.NameType(filepath.Base(relPath))
|
|
|
|
return fileType == nametransform.LongNameFilename
|
|
|
|
}
|
|
|
|
|
2016-09-21 21:25:55 +02:00
|
|
|
func (rfs *reverseFS) inoAwareStat(relPlainPath string) (*fuse.Attr, fuse.Status) {
|
|
|
|
absPath, err := rfs.abs(relPlainPath, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
var fi os.FileInfo
|
|
|
|
if relPlainPath == "" {
|
|
|
|
// Look through symlinks for the root dir
|
|
|
|
fi, err = os.Stat(absPath)
|
|
|
|
} else {
|
|
|
|
fi, err = os.Lstat(absPath)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
st := fi.Sys().(*syscall.Stat_t)
|
|
|
|
// The file has hard links. We have to give it a stable inode number so
|
|
|
|
// tar or rsync can find them.
|
|
|
|
if fi.Mode().IsRegular() && st.Nlink > 1 {
|
2016-10-04 15:03:47 +02:00
|
|
|
di := devIno{uint64(st.Dev), st.Ino}
|
2016-09-21 21:25:55 +02:00
|
|
|
rfs.inoMapLock.Lock()
|
|
|
|
stableIno := rfs.inoMap[di]
|
|
|
|
if stableIno == 0 {
|
|
|
|
rfs.inoMap[di] = rfs.inoGen.next()
|
|
|
|
}
|
|
|
|
rfs.inoMapLock.Unlock()
|
|
|
|
st.Ino = stableIno
|
|
|
|
} else {
|
|
|
|
st.Ino = rfs.inoGen.next()
|
|
|
|
}
|
|
|
|
a := &fuse.Attr{}
|
|
|
|
a.FromStat(st)
|
|
|
|
return a, fuse.OK
|
|
|
|
}
|
|
|
|
|
2016-09-20 19:32:28 +02:00
|
|
|
// GetAttr - FUSE call
|
2016-09-19 23:40:43 +02:00
|
|
|
func (rfs *reverseFS) GetAttr(relPath string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
|
2016-09-20 20:59:02 +02:00
|
|
|
if relPath == configfile.ConfDefaultName {
|
2016-09-21 21:25:55 +02:00
|
|
|
return rfs.inoAwareStat(configfile.ConfReverseName)
|
2016-09-20 20:59:02 +02:00
|
|
|
}
|
2016-08-30 00:23:55 +02:00
|
|
|
if rfs.isFiltered(relPath) {
|
2016-08-25 00:14:36 +02:00
|
|
|
return nil, fuse.EPERM
|
|
|
|
}
|
2016-09-22 23:28:11 +02:00
|
|
|
|
|
|
|
// Handle virtual files
|
|
|
|
var f nodefs.File
|
|
|
|
var status fuse.Status
|
|
|
|
virtual := false
|
|
|
|
if isDirIV(relPath) {
|
|
|
|
virtual = true
|
|
|
|
f, status = rfs.newDirIVFile(relPath)
|
|
|
|
}
|
|
|
|
if isNameFile(relPath) {
|
|
|
|
virtual = true
|
|
|
|
f, status = rfs.newNameFile(relPath)
|
|
|
|
}
|
|
|
|
if virtual {
|
|
|
|
if !status.Ok() {
|
|
|
|
fmt.Printf("GetAttr %q: newXFile failed: %v\n", relPath, status)
|
|
|
|
return nil, status
|
|
|
|
}
|
|
|
|
var a fuse.Attr
|
|
|
|
status = f.GetAttr(&a)
|
|
|
|
return &a, status
|
|
|
|
}
|
|
|
|
|
2016-09-21 21:25:55 +02:00
|
|
|
cPath, err := rfs.decryptPath(relPath)
|
2016-08-25 00:14:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2016-09-21 21:25:55 +02:00
|
|
|
a, status := rfs.inoAwareStat(cPath)
|
2016-09-02 23:45:52 +02:00
|
|
|
if !status.Ok() {
|
|
|
|
return nil, status
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
// 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-09-20 19:32:28 +02:00
|
|
|
// Access - FUSE call
|
2016-08-30 00:23:55 +02:00
|
|
|
func (rfs *reverseFS) Access(relPath string, mode uint32, context *fuse.Context) fuse.Status {
|
2016-09-19 23:40:43 +02:00
|
|
|
if isDirIV(relPath) {
|
|
|
|
return fuse.OK
|
|
|
|
}
|
2016-08-30 00:23:55 +02:00
|
|
|
if rfs.isFiltered(relPath) {
|
|
|
|
return fuse.EPERM
|
|
|
|
}
|
2016-09-19 23:40:43 +02:00
|
|
|
absPath, err := rfs.abs(rfs.decryptPath(relPath))
|
2016-08-30 00:23:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-09-19 23:40:43 +02:00
|
|
|
return fuse.ToStatus(syscall.Access(absPath, mode))
|
2016-08-30 00:23:55 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 19:32:28 +02:00
|
|
|
// Open - FUSE call
|
2016-08-30 00:23:55 +02:00
|
|
|
func (rfs *reverseFS) Open(relPath string, flags uint32, context *fuse.Context) (fuseFile nodefs.File, status fuse.Status) {
|
2016-09-20 19:32:28 +02:00
|
|
|
if relPath == configfile.ConfDefaultName {
|
|
|
|
// gocryptfs.conf maps to .gocryptfs.reverse.conf in the plaintext directory
|
|
|
|
return rfs.loopbackfs.Open(configfile.ConfReverseName, flags, context)
|
|
|
|
}
|
2016-09-19 23:40:43 +02:00
|
|
|
if isDirIV(relPath) {
|
2016-09-22 23:28:11 +02:00
|
|
|
return rfs.newDirIVFile(relPath)
|
|
|
|
}
|
|
|
|
if isNameFile(relPath) {
|
|
|
|
return rfs.newNameFile(relPath)
|
2016-09-19 23:40:43 +02:00
|
|
|
}
|
2016-08-30 00:23:55 +02:00
|
|
|
if rfs.isFiltered(relPath) {
|
2016-08-25 00:14:36 +02:00
|
|
|
return nil, fuse.EPERM
|
|
|
|
}
|
2016-09-25 11:20:10 +02:00
|
|
|
return rfs.NewFile(relPath, flags)
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 19:32:28 +02:00
|
|
|
// OpenDir - FUSE readdir call
|
2016-09-19 23:40:43 +02:00
|
|
|
func (rfs *reverseFS) OpenDir(cipherPath string, context *fuse.Context) ([]fuse.DirEntry, fuse.Status) {
|
|
|
|
relPath, err := rfs.decryptPath(cipherPath)
|
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
|
|
|
|
}
|
2016-09-24 22:40:00 +02:00
|
|
|
// Allocate maximum possible number of virtual files.
|
|
|
|
// If all files have long names we need a virtual ".name" file for each,
|
|
|
|
// plus one for gocryptfs.diriv.
|
|
|
|
virtualFiles := make([]fuse.DirEntry, len(entries)+1)
|
2016-09-22 19:54:37 +02:00
|
|
|
// Virtual gocryptfs.diriv file
|
2016-09-25 19:01:50 +02:00
|
|
|
virtualFiles[0] = fuse.DirEntry{
|
|
|
|
Mode: syscall.S_IFREG | 0400,
|
|
|
|
Name: nametransform.DirIVFilename,
|
|
|
|
}
|
2016-09-24 22:40:00 +02:00
|
|
|
// Actually used entries
|
|
|
|
nVirtual := 1
|
|
|
|
|
2016-08-25 00:14:36 +02:00
|
|
|
// Encrypt names
|
2016-09-29 21:29:45 +02:00
|
|
|
dirIV := derivePathIV(cipherPath, ivPurposeDirIV)
|
2016-08-25 00:14:36 +02:00
|
|
|
for i := range entries {
|
2016-09-22 19:54:37 +02:00
|
|
|
var cName string
|
2016-09-20 20:59:02 +02:00
|
|
|
// ".gocryptfs.reverse.conf" in the root directory is mapped to "gocryptfs.conf"
|
|
|
|
if cipherPath == "" && entries[i].Name == configfile.ConfReverseName {
|
2016-09-22 19:54:37 +02:00
|
|
|
cName = configfile.ConfDefaultName
|
2016-09-20 20:59:02 +02:00
|
|
|
} else {
|
2016-09-22 19:54:37 +02:00
|
|
|
cName = rfs.nameTransform.EncryptName(entries[i].Name, dirIV)
|
|
|
|
if len(cName) > syscall.NAME_MAX {
|
|
|
|
cName = nametransform.HashLongName(cName)
|
2016-09-25 19:01:50 +02:00
|
|
|
dotNameFile := fuse.DirEntry{
|
|
|
|
Mode: syscall.S_IFREG | 0600,
|
|
|
|
Name: cName + nametransform.LongNameSuffix,
|
|
|
|
}
|
2016-09-24 22:40:00 +02:00
|
|
|
virtualFiles[nVirtual] = dotNameFile
|
|
|
|
nVirtual++
|
2016-09-22 19:54:37 +02:00
|
|
|
}
|
2016-09-20 20:59:02 +02:00
|
|
|
}
|
2016-09-22 19:54:37 +02:00
|
|
|
entries[i].Name = cName
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
2016-09-24 22:40:00 +02:00
|
|
|
entries = append(entries, virtualFiles[:nVirtual]...)
|
2016-08-25 00:14:36 +02:00
|
|
|
return entries, fuse.OK
|
|
|
|
}
|
2016-09-21 21:25:55 +02:00
|
|
|
|
|
|
|
// StatFs - FUSE call
|
|
|
|
func (rfs *reverseFS) StatFs(name string) *fuse.StatfsOut {
|
|
|
|
return rfs.loopbackfs.StatFs(name)
|
|
|
|
}
|
2016-09-25 18:01:24 +02:00
|
|
|
|
|
|
|
// Readlink - FUSE call
|
|
|
|
func (rfs *reverseFS) Readlink(cipherPath string, context *fuse.Context) (string, fuse.Status) {
|
|
|
|
absPath, err := rfs.abs(rfs.decryptPath(cipherPath))
|
|
|
|
if err != nil {
|
|
|
|
return "", fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
plainTarget, err := os.Readlink(absPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
if rfs.args.PlaintextNames {
|
|
|
|
return plainTarget, fuse.OK
|
|
|
|
}
|
2016-09-29 21:29:45 +02:00
|
|
|
nonce := derivePathIV(cipherPath, ivPurposeSymlinkIV)
|
2016-09-25 18:01:24 +02:00
|
|
|
// Symlinks are encrypted like file contents and base64-encoded
|
2016-09-29 21:29:45 +02:00
|
|
|
cBinTarget := rfs.contentEnc.EncryptBlockNonce([]byte(plainTarget), 0, nil, nonce)
|
2016-09-25 18:01:24 +02:00
|
|
|
cTarget := base64.URLEncoding.EncodeToString(cBinTarget)
|
|
|
|
return cTarget, fuse.OK
|
|
|
|
}
|