2016-09-22 23:28:11 +02:00
|
|
|
package fusefrontend_reverse
|
|
|
|
|
|
|
|
import (
|
2016-12-10 11:50:16 +01:00
|
|
|
"log"
|
2016-09-22 23:28:11 +02:00
|
|
|
"path/filepath"
|
2016-09-24 22:40:00 +02:00
|
|
|
"sync"
|
2016-09-22 23:28:11 +02:00
|
|
|
"syscall"
|
2016-09-24 22:40:00 +02:00
|
|
|
"time"
|
2016-09-22 23:28:11 +02:00
|
|
|
|
2018-02-01 23:46:02 +01:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2016-09-22 23:28:11 +02:00
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
|
|
|
"github.com/hanwen/go-fuse/fuse/nodefs"
|
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
2017-05-28 18:09:02 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/pathiv"
|
2018-01-17 20:52:52 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2016-11-10 23:25:37 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2016-09-22 23:28:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-10-01 13:50:25 +02:00
|
|
|
// File names are padded to 16-byte multiples, encrypted and
|
|
|
|
// base64-encoded. We can encode at most 176 bytes to stay below the 255
|
|
|
|
// bytes limit:
|
|
|
|
// * base64(176 bytes) = 235 bytes
|
|
|
|
// * base64(192 bytes) = 256 bytes (over 255!)
|
|
|
|
// But the PKCS#7 padding is at least one byte. This means we can only use
|
|
|
|
// 175 bytes for the file name.
|
|
|
|
shortNameMax = 175
|
2016-09-22 23:28:11 +02:00
|
|
|
)
|
|
|
|
|
2018-01-17 20:52:52 +01:00
|
|
|
// longnameParentCache maps dir+"/"+longname to plaintextname.
|
|
|
|
// Yes, the combination of relative plaintext dir path and encrypted
|
|
|
|
// longname is strange, but works fine as a map index.
|
2016-09-24 22:40:00 +02:00
|
|
|
var longnameParentCache map[string]string
|
|
|
|
var longnameCacheLock sync.Mutex
|
|
|
|
|
|
|
|
// Very simple cache cleaner: Nuke it every hour
|
|
|
|
func longnameCacheCleaner() {
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Hour)
|
|
|
|
longnameCacheLock.Lock()
|
|
|
|
longnameParentCache = map[string]string{}
|
|
|
|
longnameCacheLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 22:22:28 +02:00
|
|
|
func initLongnameCache() {
|
|
|
|
if longnameParentCache != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-09-24 22:40:00 +02:00
|
|
|
longnameParentCache = map[string]string{}
|
|
|
|
go longnameCacheCleaner()
|
|
|
|
}
|
|
|
|
|
2018-01-17 20:52:52 +01:00
|
|
|
// findLongnameParent converts "longname" = "gocryptfs.longname.XYZ" to the
|
|
|
|
// plaintext name. "dir" = relative plaintext path to the directory the
|
|
|
|
// longname file is in, "dirIV" = directory IV of the directory.
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) findLongnameParent(dir string, dirIV []byte, longname string) (plaintextName string, err error) {
|
2016-09-24 22:40:00 +02:00
|
|
|
longnameCacheLock.Lock()
|
2018-01-16 23:16:54 +01:00
|
|
|
hit := longnameParentCache[dir+"/"+longname]
|
2016-09-24 22:40:00 +02:00
|
|
|
longnameCacheLock.Unlock()
|
|
|
|
if hit != "" {
|
|
|
|
return hit, nil
|
|
|
|
}
|
2018-09-08 18:06:33 +02:00
|
|
|
dirfd, err := syscallcompat.OpenDirNofollow(rfs.args.Cipherdir, filepath.Dir(dir))
|
2016-09-22 23:28:11 +02:00
|
|
|
if err != nil {
|
2018-09-08 18:06:33 +02:00
|
|
|
tlog.Warn.Printf("findLongnameParent: OpenDirNofollow failed: %v\n", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
fd, err := syscallcompat.Openat(dirfd, filepath.Base(dir), syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW, 0)
|
|
|
|
syscall.Close(dirfd)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("findLongnameParent: Openat failed: %v\n", err)
|
2016-09-22 23:28:11 +02:00
|
|
|
return "", err
|
|
|
|
}
|
2018-01-17 20:52:52 +01:00
|
|
|
dirEntries, err := syscallcompat.Getdents(fd)
|
|
|
|
syscall.Close(fd)
|
2016-09-22 23:28:11 +02:00
|
|
|
if err != nil {
|
2018-01-17 20:52:52 +01:00
|
|
|
tlog.Warn.Printf("findLongnameParent: Getdents failed: %v\n", err)
|
2016-09-22 23:28:11 +02:00
|
|
|
return "", err
|
|
|
|
}
|
2016-09-24 22:40:00 +02:00
|
|
|
longnameCacheLock.Lock()
|
|
|
|
defer longnameCacheLock.Unlock()
|
2018-01-17 20:52:52 +01:00
|
|
|
for _, entry := range dirEntries {
|
|
|
|
plaintextName := entry.Name
|
2016-09-24 22:40:00 +02:00
|
|
|
if len(plaintextName) <= shortNameMax {
|
2016-09-22 23:28:11 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-09-24 22:40:00 +02:00
|
|
|
cName := rfs.nameTransform.EncryptName(plaintextName, dirIV)
|
2018-02-01 23:46:02 +01:00
|
|
|
if len(cName) <= unix.NAME_MAX {
|
2018-01-17 20:52:52 +01:00
|
|
|
// Entry should have been skipped by the "continue" above
|
2016-12-10 11:50:16 +01:00
|
|
|
log.Panic("logic error or wrong shortNameMax constant?")
|
2016-09-22 23:28:11 +02:00
|
|
|
}
|
2017-03-05 22:25:41 +01:00
|
|
|
hName := rfs.nameTransform.HashLongName(cName)
|
2018-01-16 23:16:54 +01:00
|
|
|
longnameParentCache[dir+"/"+hName] = plaintextName
|
2016-09-22 23:28:11 +02:00
|
|
|
if longname == hName {
|
2016-09-24 22:40:00 +02:00
|
|
|
hit = plaintextName
|
2016-09-22 23:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-24 22:40:00 +02:00
|
|
|
if hit == "" {
|
|
|
|
return "", syscall.ENOENT
|
|
|
|
}
|
2016-10-02 06:14:18 +02:00
|
|
|
return hit, nil
|
2016-09-22 23:28:11 +02:00
|
|
|
}
|
|
|
|
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) newNameFile(relPath string) (nodefs.File, fuse.Status) {
|
2019-04-08 20:18:45 +02:00
|
|
|
dotName := filepath.Base(relPath) // gocryptfs.longname.XYZ.name
|
2019-02-16 21:55:54 +01:00
|
|
|
longname := nametransform.RemoveLongNameSuffix(dotName) // gocryptfs.longname.XYZ
|
2017-04-01 14:17:54 +02:00
|
|
|
// cipher directory
|
2017-08-06 23:12:27 +02:00
|
|
|
cDir := nametransform.Dir(relPath)
|
2017-04-01 14:17:54 +02:00
|
|
|
// plain directory
|
2016-09-22 23:28:11 +02:00
|
|
|
pDir, err := rfs.decryptPath(cDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2017-05-28 18:09:02 +02:00
|
|
|
dirIV := pathiv.Derive(cDir, pathiv.PurposeDirIV)
|
2017-04-01 14:17:54 +02:00
|
|
|
// plain name
|
|
|
|
pName, err := rfs.findLongnameParent(pDir, dirIV, longname)
|
2016-09-22 23:28:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
2017-04-01 14:17:54 +02:00
|
|
|
content := []byte(rfs.nameTransform.EncryptName(pName, dirIV))
|
2018-01-17 21:36:38 +01:00
|
|
|
parentFile := filepath.Join(pDir, pName)
|
2020-05-03 15:22:10 +02:00
|
|
|
return rfs.newVirtualFile(content, rfs.args.Cipherdir, parentFile, inoTagNameFile)
|
2016-09-22 23:28:11 +02:00
|
|
|
}
|