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
|
|
|
"os"
|
|
|
|
"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
|
|
|
|
|
|
|
"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"
|
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
|
|
|
)
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// findLongnameParent converts "gocryptfs.longname.XYZ" to the plaintext name
|
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()
|
2017-11-24 00:44:06 +01:00
|
|
|
hit := longnameParentCache[dir + "/" + longname]
|
2016-09-24 22:40:00 +02:00
|
|
|
longnameCacheLock.Unlock()
|
|
|
|
if hit != "" {
|
|
|
|
return hit, nil
|
|
|
|
}
|
2016-09-22 23:28:11 +02:00
|
|
|
absDir := filepath.Join(rfs.args.Cipherdir, dir)
|
|
|
|
dirfd, err := os.Open(absDir)
|
|
|
|
if err != nil {
|
2016-11-10 23:25:37 +01:00
|
|
|
tlog.Warn.Printf("findLongnameParent: opendir failed: %v\n", err)
|
2016-09-22 23:28:11 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
dirEntries, err := dirfd.Readdirnames(-1)
|
2017-11-22 03:29:01 +01:00
|
|
|
dirfd.Close()
|
2016-09-22 23:28:11 +02:00
|
|
|
if err != nil {
|
2017-10-01 13:50:25 +02:00
|
|
|
tlog.Warn.Printf("findLongnameParent: Readdirnames 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()
|
|
|
|
for _, plaintextName = range dirEntries {
|
|
|
|
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)
|
2016-09-22 23:28:11 +02:00
|
|
|
if len(cName) <= syscall.NAME_MAX {
|
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)
|
2017-11-24 00:44:06 +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) {
|
2016-09-22 23:28:11 +02:00
|
|
|
dotName := filepath.Base(relPath) // gocryptfs.longname.XYZ.name
|
|
|
|
longname := dotName[:len(dotName)-len(nametransform.LongNameSuffix)] // 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))
|
|
|
|
parentFile := filepath.Join(rfs.args.Cipherdir, pDir, pName)
|
2017-07-29 16:13:38 +02:00
|
|
|
return rfs.newVirtualFile(content, parentFile, inoBaseNameFile)
|
2016-09-22 23:28:11 +02:00
|
|
|
}
|