2016-08-25 00:14:36 +02:00
|
|
|
package fusefrontend_reverse
|
|
|
|
|
|
|
|
import (
|
2016-09-02 23:45:52 +02:00
|
|
|
"fmt"
|
2016-09-19 23:40:43 +02:00
|
|
|
"path/filepath"
|
2016-08-30 00:23:55 +02:00
|
|
|
"syscall"
|
2016-08-25 00:14:36 +02:00
|
|
|
|
2017-12-06 00:05:28 +01:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2020-05-17 14:18:23 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse/nodefs"
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse/pathfs"
|
2016-08-25 00:14:36 +02:00
|
|
|
|
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"
|
2020-05-03 15:22:10 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/inomap"
|
2016-08-25 00:14:36 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
2017-05-28 18:09:02 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/pathiv"
|
2017-12-05 23:11:46 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2016-10-08 20:57:38 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2019-02-16 21:55:54 +01:00
|
|
|
|
|
|
|
"github.com/sabhiram/go-gitignore"
|
2016-08-25 00:14:36 +02:00
|
|
|
)
|
|
|
|
|
2016-11-10 00:38:01 +01:00
|
|
|
// ReverseFS implements the pathfs.FileSystem interface and provides an
|
|
|
|
// encrypted view of a plaintext directory.
|
|
|
|
type ReverseFS struct {
|
2016-08-30 00:23:55 +02:00
|
|
|
// 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
|
2019-02-16 21:55:54 +01:00
|
|
|
nameTransform nametransform.NameTransformer
|
2016-08-25 00:14:36 +02:00
|
|
|
// Content encryption helper
|
|
|
|
contentEnc *contentenc.ContentEnc
|
2020-05-10 00:25:49 +02:00
|
|
|
// Tests whether a path is excluded (hiden) from the user. Used by -exclude.
|
2019-02-16 21:55:54 +01:00
|
|
|
excluder ignore.IgnoreParser
|
2020-05-03 15:22:10 +02:00
|
|
|
// inoMap translates inode numbers from different devices to unique inode
|
|
|
|
// numbers.
|
|
|
|
inoMap *inomap.InoMap
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
|
2016-11-10 00:38:01 +01:00
|
|
|
var _ pathfs.FileSystem = &ReverseFS{}
|
2016-10-02 06:14:18 +02:00
|
|
|
|
2016-10-09 17:05:12 +02:00
|
|
|
// NewFS returns an encrypted FUSE overlay filesystem.
|
|
|
|
// In this case (reverse mode) the backing directory is plain-text and
|
2016-11-10 00:38:01 +01:00
|
|
|
// ReverseFS provides an encrypted view.
|
2019-02-16 21:55:54 +01:00
|
|
|
func NewFS(args fusefrontend.Args, c *contentenc.ContentEnc, n nametransform.NameTransformer) *ReverseFS {
|
2016-10-05 22:22:28 +02:00
|
|
|
initLongnameCache()
|
2018-08-11 23:26:49 +02:00
|
|
|
fs := &ReverseFS{
|
2016-08-30 00:23:55 +02:00
|
|
|
// 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,
|
2018-02-17 16:26:35 +01:00
|
|
|
nameTransform: n,
|
|
|
|
contentEnc: c,
|
2020-05-03 15:22:10 +02:00
|
|
|
inoMap: inomap.New(),
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
fs.prepareExcluder(args)
|
2018-08-11 23:26:49 +02:00
|
|
|
return fs
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-16 21:55:54 +01:00
|
|
|
// getFileInfo returns information on a ciphertext path "relPath":
|
|
|
|
// - ftype: file type (as returned by getFileType)
|
|
|
|
// - excluded: if the path is excluded
|
|
|
|
// - pPath: if it's not a special file, the decrypted path
|
|
|
|
// - err: non nil if any error happens
|
|
|
|
func (rfs *ReverseFS) getFileInfo(relPath string) (ftype fileType, excluded bool, pPath string, err error) {
|
|
|
|
ftype = rfs.getFileType(relPath)
|
|
|
|
if ftype == typeConfig {
|
|
|
|
excluded, pPath, err = false, "", nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ftype == typeDiriv {
|
|
|
|
parentDir := nametransform.Dir(relPath)
|
|
|
|
_, excluded, _, err = rfs.getFileInfo(parentDir)
|
|
|
|
pPath = ""
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ftype == typeName {
|
|
|
|
parentDir := nametransform.Dir(relPath)
|
|
|
|
var parentExcluded bool
|
|
|
|
_, parentExcluded, _, err = rfs.getFileInfo(parentDir)
|
|
|
|
if parentExcluded || err != nil {
|
|
|
|
excluded, pPath = parentExcluded, ""
|
|
|
|
return
|
2018-08-11 23:26:49 +02:00
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
relPath = nametransform.RemoveLongNameSuffix(relPath)
|
2018-08-11 23:26:49 +02:00
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
pPath, err = rfs.decryptPath(relPath)
|
|
|
|
excluded = err == nil && rfs.isExcludedPlain(pPath)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type fileType int
|
|
|
|
|
|
|
|
// Values returned by getFileType
|
|
|
|
const (
|
|
|
|
// A regular file/directory/symlink
|
|
|
|
typeRegular fileType = iota
|
|
|
|
// A DirIV (gocryptfs.diriv) file
|
|
|
|
typeDiriv
|
|
|
|
// A .name file for a file with a long name
|
|
|
|
typeName
|
|
|
|
// The config file
|
|
|
|
typeConfig
|
|
|
|
)
|
|
|
|
|
|
|
|
// getFileType returns the type of file. Only the name is checked
|
|
|
|
func (rfs *ReverseFS) getFileType(cPath string) fileType {
|
|
|
|
if rfs.isDirIV(cPath) {
|
|
|
|
return typeDiriv
|
|
|
|
}
|
|
|
|
if rfs.isNameFile(cPath) {
|
|
|
|
return typeName
|
|
|
|
}
|
|
|
|
if rfs.isTranslatedConfig(cPath) {
|
|
|
|
return typeConfig
|
|
|
|
}
|
|
|
|
return typeRegular
|
2018-08-11 23:26:49 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 23:40:43 +02:00
|
|
|
// isDirIV determines if the path points to a gocryptfs.diriv file
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) isDirIV(relPath string) bool {
|
2016-10-08 20:57:38 +02:00
|
|
|
if rfs.args.PlaintextNames {
|
|
|
|
return false
|
|
|
|
}
|
2016-09-19 23:40:43 +02:00
|
|
|
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
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) isNameFile(relPath string) bool {
|
2016-10-08 20:57:38 +02:00
|
|
|
if rfs.args.PlaintextNames {
|
|
|
|
return false
|
|
|
|
}
|
2016-09-22 23:28:11 +02:00
|
|
|
fileType := nametransform.NameType(filepath.Base(relPath))
|
|
|
|
return fileType == nametransform.LongNameFilename
|
|
|
|
}
|
|
|
|
|
2017-04-01 17:19:15 +02:00
|
|
|
// isTranslatedConfig returns true if the default config file name is in use
|
2016-10-08 21:14:16 +02:00
|
|
|
// and the ciphertext path is "gocryptfs.conf".
|
|
|
|
// "gocryptfs.conf" then maps to ".gocryptfs.reverse.conf" in the plaintext
|
|
|
|
// directory.
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) isTranslatedConfig(relPath string) bool {
|
2016-10-08 21:14:16 +02:00
|
|
|
if rfs.args.ConfigCustom {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if relPath == configfile.ConfDefaultName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-09-20 19:32:28 +02:00
|
|
|
// GetAttr - FUSE call
|
2017-04-01 17:19:15 +02:00
|
|
|
// "relPath" is the relative ciphertext path
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) GetAttr(relPath string, context *fuse.Context) (*fuse.Attr, fuse.Status) {
|
2019-02-16 21:55:54 +01:00
|
|
|
ftype, excluded, pPath, err := rfs.getFileInfo(relPath)
|
|
|
|
if excluded {
|
2018-08-11 23:26:49 +02:00
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2017-04-01 17:19:15 +02:00
|
|
|
// Handle "gocryptfs.conf"
|
2019-02-16 21:55:54 +01:00
|
|
|
if ftype == typeConfig {
|
2017-04-01 17:19:15 +02:00
|
|
|
absConfPath, _ := rfs.abs(configfile.ConfReverseName, nil)
|
|
|
|
var st syscall.Stat_t
|
2019-02-16 21:55:54 +01:00
|
|
|
err = syscall.Lstat(absConfPath, &st)
|
2017-04-01 17:19:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2020-05-03 15:22:10 +02:00
|
|
|
rfs.inoMap.TranslateStat(&st)
|
2017-04-01 17:19:15 +02:00
|
|
|
var a fuse.Attr
|
|
|
|
a.FromStat(&st)
|
2017-05-30 23:01:06 +02:00
|
|
|
if rfs.args.ForceOwner != nil {
|
|
|
|
a.Owner = *rfs.args.ForceOwner
|
|
|
|
}
|
2017-04-01 17:19:15 +02:00
|
|
|
return &a, fuse.OK
|
2016-09-20 20:59:02 +02:00
|
|
|
}
|
2017-04-01 17:19:15 +02:00
|
|
|
// Handle virtual files (gocryptfs.diriv, *.name)
|
2016-09-22 23:28:11 +02:00
|
|
|
var f nodefs.File
|
|
|
|
var status fuse.Status
|
|
|
|
virtual := false
|
2019-02-16 21:55:54 +01:00
|
|
|
if ftype == typeDiriv {
|
2016-09-22 23:28:11 +02:00
|
|
|
virtual = true
|
|
|
|
f, status = rfs.newDirIVFile(relPath)
|
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
if ftype == typeName {
|
2016-09-22 23:28:11 +02:00
|
|
|
virtual = true
|
|
|
|
f, status = rfs.newNameFile(relPath)
|
|
|
|
}
|
|
|
|
if virtual {
|
|
|
|
if !status.Ok() {
|
2017-04-01 15:49:53 +02:00
|
|
|
tlog.Warn.Printf("GetAttr %q: newXFile failed: %v\n", relPath, status)
|
2016-09-22 23:28:11 +02:00
|
|
|
return nil, status
|
|
|
|
}
|
|
|
|
var a fuse.Attr
|
|
|
|
status = f.GetAttr(&a)
|
2017-05-30 23:01:06 +02:00
|
|
|
if rfs.args.ForceOwner != nil {
|
|
|
|
a.Owner = *rfs.args.ForceOwner
|
|
|
|
}
|
2016-09-22 23:28:11 +02:00
|
|
|
return &a, status
|
|
|
|
}
|
2020-05-03 15:22:10 +02:00
|
|
|
// Normal file / directory
|
2019-02-16 21:55:54 +01:00
|
|
|
dirfd, name, err := rfs.openBackingDir(pPath)
|
2017-04-01 17:19:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2017-12-06 00:05:28 +01:00
|
|
|
// Stat the backing file/dir using Fstatat
|
2020-05-03 15:22:10 +02:00
|
|
|
var st syscall.Stat_t
|
|
|
|
{
|
|
|
|
var st2 unix.Stat_t
|
|
|
|
err = syscallcompat.Fstatat(dirfd, name, &st2, unix.AT_SYMLINK_NOFOLLOW)
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
st = syscallcompat.Unix2syscall(st2)
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
2020-05-03 15:22:10 +02:00
|
|
|
rfs.inoMap.TranslateStat(&st)
|
2017-04-01 17:19:15 +02:00
|
|
|
var a fuse.Attr
|
2020-05-03 15:22:10 +02:00
|
|
|
a.FromStat(&st)
|
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)
|
2017-03-07 10:09:09 +01:00
|
|
|
} else if a.IsSymlink() {
|
|
|
|
var linkTarget string
|
|
|
|
var readlinkStatus fuse.Status
|
|
|
|
|
|
|
|
linkTarget, readlinkStatus = rfs.Readlink(relPath, context)
|
|
|
|
if !readlinkStatus.Ok() {
|
|
|
|
return nil, readlinkStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Size = uint64(len(linkTarget))
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
2017-05-30 23:01:06 +02:00
|
|
|
if rfs.args.ForceOwner != nil {
|
|
|
|
a.Owner = *rfs.args.ForceOwner
|
|
|
|
}
|
2017-04-01 17:19:15 +02:00
|
|
|
return &a, fuse.OK
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 19:32:28 +02:00
|
|
|
// Access - FUSE call
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) Access(relPath string, mode uint32, context *fuse.Context) fuse.Status {
|
2019-02-16 21:55:54 +01:00
|
|
|
ftype, excluded, pPath, err := rfs.getFileInfo(relPath)
|
|
|
|
if excluded {
|
2018-08-11 23:26:49 +02:00
|
|
|
return fuse.ENOENT
|
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
if ftype != typeRegular {
|
2017-04-29 14:50:58 +02:00
|
|
|
// access(2) R_OK flag for checking if the file is readable, always 4 as defined in POSIX.
|
|
|
|
ROK := uint32(0x4)
|
2017-02-16 21:16:42 +01:00
|
|
|
// Virtual files can always be read and never written
|
2017-04-29 14:50:58 +02:00
|
|
|
if mode == ROK || mode == 0 {
|
2017-02-16 21:16:42 +01:00
|
|
|
return fuse.OK
|
|
|
|
}
|
2017-04-29 14:50:58 +02:00
|
|
|
return fuse.EPERM
|
2016-08-30 00:23:55 +02:00
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
dirfd, name, err := rfs.openBackingDir(pPath)
|
2016-08-30 00:23:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2019-01-20 13:10:59 +01:00
|
|
|
err = syscallcompat.Faccessat(dirfd, name, mode)
|
2017-12-07 00:08:10 +01:00
|
|
|
syscall.Close(dirfd)
|
|
|
|
return fuse.ToStatus(err)
|
2016-08-30 00:23:55 +02:00
|
|
|
}
|
|
|
|
|
2016-09-20 19:32:28 +02:00
|
|
|
// Open - FUSE call
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) Open(relPath string, flags uint32, context *fuse.Context) (fuseFile nodefs.File, status fuse.Status) {
|
2019-02-16 21:55:54 +01:00
|
|
|
ftype, excluded, pPath, err := rfs.getFileInfo(relPath)
|
|
|
|
if excluded {
|
2018-08-11 23:26:49 +02:00
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
if ftype == typeConfig {
|
2016-09-20 19:32:28 +02:00
|
|
|
return rfs.loopbackfs.Open(configfile.ConfReverseName, flags, context)
|
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
if ftype == typeDiriv {
|
2016-09-22 23:28:11 +02:00
|
|
|
return rfs.newDirIVFile(relPath)
|
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
if ftype == typeName {
|
2016-09-22 23:28:11 +02:00
|
|
|
return rfs.newNameFile(relPath)
|
2016-09-19 23:40:43 +02:00
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
return rfs.newFile(relPath, pPath)
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) openDirPlaintextnames(relPath string, entries []fuse.DirEntry) ([]fuse.DirEntry, fuse.Status) {
|
2016-10-08 20:57:38 +02:00
|
|
|
if relPath != "" || rfs.args.ConfigCustom {
|
|
|
|
return entries, fuse.OK
|
|
|
|
}
|
|
|
|
// We are in the root dir and the default config file name
|
|
|
|
// ".gocryptfs.reverse.conf" is used. We map it to "gocryptfs.conf".
|
|
|
|
dupe := -1
|
|
|
|
status := fuse.OK
|
|
|
|
for i := range entries {
|
|
|
|
if entries[i].Name == configfile.ConfReverseName {
|
|
|
|
entries[i].Name = configfile.ConfDefaultName
|
|
|
|
} else if entries[i].Name == configfile.ConfDefaultName {
|
|
|
|
dupe = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if dupe >= 0 {
|
|
|
|
// Warn the user loudly: The gocryptfs.conf_NAME_COLLISION file will
|
|
|
|
// throw ENOENT errors that are hard to miss.
|
2019-01-04 17:34:21 +01:00
|
|
|
tlog.Warn.Printf("The file %q is mapped to %q and shadows another file. Please rename %q in directory %q.",
|
2016-10-08 20:57:38 +02:00
|
|
|
configfile.ConfReverseName, configfile.ConfDefaultName, configfile.ConfDefaultName, rfs.args.Cipherdir)
|
|
|
|
entries[dupe].Name = "gocryptfs.conf_NAME_COLLISION_" + fmt.Sprintf("%d", cryptocore.RandUint64())
|
|
|
|
}
|
|
|
|
return entries, status
|
|
|
|
}
|
|
|
|
|
2016-09-20 19:32:28 +02:00
|
|
|
// OpenDir - FUSE readdir call
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) OpenDir(cipherPath string, context *fuse.Context) ([]fuse.DirEntry, fuse.Status) {
|
2019-02-16 21:55:54 +01:00
|
|
|
ftype, excluded, relPath, err := rfs.getFileInfo(cipherPath)
|
|
|
|
if excluded {
|
2018-08-11 23:26:49 +02:00
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
2016-08-25 00:14:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
if ftype != typeRegular {
|
|
|
|
return nil, fuse.ENOTDIR
|
|
|
|
}
|
2016-08-25 00:14:36 +02:00
|
|
|
// Read plaintext dir
|
2018-09-08 18:06:33 +02:00
|
|
|
dirfd, err := syscallcompat.OpenDirNofollow(rfs.args.Cipherdir, filepath.Dir(relPath))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
fd, err := syscallcompat.Openat(dirfd, filepath.Base(relPath), syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
|
|
|
syscall.Close(dirfd)
|
2017-12-05 23:11:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
entries, err := syscallcompat.Getdents(fd)
|
2017-12-08 06:16:16 +01:00
|
|
|
syscall.Close(fd)
|
2017-12-05 23:11:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
2016-10-08 20:57:38 +02:00
|
|
|
if rfs.args.PlaintextNames {
|
2019-01-04 19:52:17 +01:00
|
|
|
var status fuse.Status
|
|
|
|
entries, status = rfs.openDirPlaintextnames(cipherPath, entries)
|
2019-01-04 17:34:21 +01:00
|
|
|
if !status.Ok() {
|
|
|
|
return nil, status
|
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
entries = rfs.excludeDirEntries(relPath, entries)
|
2019-01-04 17:34:21 +01:00
|
|
|
return entries, fuse.OK
|
2016-10-08 20:57:38 +02:00
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
// Filter out excluded entries
|
|
|
|
entries = rfs.excludeDirEntries(relPath, entries)
|
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{
|
2017-03-27 22:47:45 +02:00
|
|
|
Mode: virtualFileMode,
|
2016-09-25 19:01:50 +02:00
|
|
|
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
|
2017-05-28 18:09:02 +02:00
|
|
|
dirIV := pathiv.Derive(cipherPath, pathiv.PurposeDirIV)
|
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"
|
2019-03-26 20:59:52 +01:00
|
|
|
if cipherPath == "" && entries[i].Name == configfile.ConfReverseName &&
|
|
|
|
!rfs.args.ConfigCustom {
|
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)
|
2018-02-01 23:46:02 +01:00
|
|
|
if len(cName) > unix.NAME_MAX {
|
2017-03-05 22:25:41 +01:00
|
|
|
cName = rfs.nameTransform.HashLongName(cName)
|
2016-09-25 19:01:50 +02:00
|
|
|
dotNameFile := fuse.DirEntry{
|
2017-03-27 22:47:45 +02:00
|
|
|
Mode: virtualFileMode,
|
2016-09-25 19:01:50 +02:00
|
|
|
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
|
|
|
}
|
2019-01-04 17:34:21 +01:00
|
|
|
// Add virtual files
|
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
|
|
|
|
2019-01-04 17:34:21 +01:00
|
|
|
// excludeDirEntries filters out directory entries that are "-exclude"d.
|
2019-02-16 21:55:54 +01:00
|
|
|
// pDir is the relative plaintext path to the directory these entries are
|
|
|
|
// from. The entries should be plaintext files.
|
|
|
|
func (rfs *ReverseFS) excludeDirEntries(pDir string, entries []fuse.DirEntry) (filtered []fuse.DirEntry) {
|
|
|
|
if rfs.excluder == nil {
|
2019-01-04 17:34:21 +01:00
|
|
|
return entries
|
|
|
|
}
|
|
|
|
filtered = make([]fuse.DirEntry, 0, len(entries))
|
|
|
|
for _, entry := range entries {
|
2019-02-16 21:55:54 +01:00
|
|
|
// filepath.Join handles the case of pDir="" correctly:
|
|
|
|
// Join("", "foo") -> "foo". This does not: pDir + "/" + name"
|
|
|
|
p := filepath.Join(pDir, entry.Name)
|
|
|
|
if rfs.isExcludedPlain(p) {
|
2019-01-04 17:34:21 +01:00
|
|
|
// Skip file
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filtered = append(filtered, entry)
|
|
|
|
}
|
|
|
|
return filtered
|
|
|
|
}
|
|
|
|
|
2017-12-02 21:36:07 +01:00
|
|
|
// StatFs - FUSE call. Returns information about the filesystem (free space
|
|
|
|
// etc).
|
|
|
|
// Securing statfs against symlink races seems to be more trouble than
|
|
|
|
// it's worth, so we just ignore the path and always return info about the
|
|
|
|
// backing storage root dir.
|
2018-08-11 23:26:49 +02:00
|
|
|
func (rfs *ReverseFS) StatFs(relPath string) *fuse.StatfsOut {
|
2019-02-16 21:55:54 +01:00
|
|
|
_, excluded, _, err := rfs.getFileInfo(relPath)
|
|
|
|
if excluded || err != nil {
|
2018-08-11 23:26:49 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-12-02 21:36:07 +01:00
|
|
|
var s syscall.Statfs_t
|
2019-02-16 21:55:54 +01:00
|
|
|
err = syscall.Statfs(rfs.args.Cipherdir, &s)
|
2017-12-02 21:36:07 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
out := &fuse.StatfsOut{}
|
|
|
|
out.FromStatfsT(&s)
|
|
|
|
return out
|
2016-09-21 21:25:55 +02:00
|
|
|
}
|
2016-09-25 18:01:24 +02:00
|
|
|
|
|
|
|
// Readlink - FUSE call
|
2017-12-06 21:10:49 +01:00
|
|
|
func (rfs *ReverseFS) Readlink(relPath string, context *fuse.Context) (string, fuse.Status) {
|
2019-02-16 21:55:54 +01:00
|
|
|
ftype, excluded, pPath, err := rfs.getFileInfo(relPath)
|
|
|
|
if excluded {
|
2018-08-11 23:26:49 +02:00
|
|
|
return "", fuse.ENOENT
|
|
|
|
}
|
2019-02-16 21:55:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
if ftype != typeRegular {
|
|
|
|
return "", fuse.EINVAL
|
|
|
|
}
|
|
|
|
dirfd, name, err := rfs.openBackingDir(pPath)
|
2017-12-06 21:10:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
// read the link target using Readlinkat
|
2017-12-07 18:36:52 +01:00
|
|
|
plainTarget, err := syscallcompat.Readlinkat(dirfd, name)
|
|
|
|
syscall.Close(dirfd)
|
2016-09-25 18:01:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
if rfs.args.PlaintextNames {
|
|
|
|
return plainTarget, fuse.OK
|
|
|
|
}
|
2017-12-06 21:10:49 +01:00
|
|
|
nonce := pathiv.Derive(relPath, pathiv.PurposeSymlinkIV)
|
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)
|
2019-02-16 21:55:54 +01:00
|
|
|
cTarget := rfs.nameTransform.B64EncodeToString(cBinTarget)
|
2017-11-26 21:27:29 +01:00
|
|
|
// The kernel will reject a symlink target above 4096 chars and return
|
|
|
|
// and I/O error to the user. Better emit the proper error ourselves.
|
2018-06-08 00:47:48 +02:00
|
|
|
if len(cTarget) > syscallcompat.PATH_MAX {
|
2017-11-26 21:27:29 +01:00
|
|
|
return "", fuse.Status(syscall.ENAMETOOLONG)
|
|
|
|
}
|
2016-09-25 18:01:24 +02:00
|
|
|
return cTarget, fuse.OK
|
|
|
|
}
|