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 (
|
|
|
|
shortNameMax = 176
|
|
|
|
)
|
|
|
|
|
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()
|
|
|
|
hit := longnameParentCache[longname]
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
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)
|
2016-09-24 22:40:00 +02:00
|
|
|
longnameParentCache[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
|
2016-09-22 23:28:11 +02:00
|
|
|
cDir := saneDir(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
|
|
|
}
|