2016-08-25 00:14:36 +02:00
|
|
|
package fusefrontend_reverse
|
|
|
|
|
|
|
|
import (
|
2016-08-30 00:23:55 +02:00
|
|
|
"encoding/base64"
|
2016-08-25 00:14:36 +02:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2016-08-30 00:23:55 +02:00
|
|
|
"syscall"
|
2016-09-22 23:28:11 +02:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
2017-05-28 18:09:02 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/pathiv"
|
2016-11-10 23:51:47 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2016-08-25 00:14:36 +02:00
|
|
|
)
|
|
|
|
|
2016-09-22 23:28:11 +02:00
|
|
|
// saneDir is like filepath.Dir but returns "" instead of "."
|
|
|
|
func saneDir(path string) string {
|
|
|
|
d := filepath.Dir(path)
|
|
|
|
if d == "." {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2017-04-01 17:19:15 +02:00
|
|
|
// abs basically returns storage dir + "/" + relPath.
|
|
|
|
// It takes an error parameter so it can directly wrap decryptPath like this:
|
|
|
|
// a, err := rfs.abs(rfs.decryptPath(relPath))
|
|
|
|
// abs never generates an error on its own. In other words, abs(p, nil) never
|
|
|
|
// fails.
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) abs(relPath string, err error) (string, error) {
|
2016-08-25 00:14:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-08-30 00:23:55 +02:00
|
|
|
return filepath.Join(rfs.args.Cipherdir, relPath), nil
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 17:46:11 +01:00
|
|
|
func (rfs *ReverseFS) rDecryptName(cName string, dirIV []byte, pDir string) (pName string, err error) {
|
|
|
|
nameType := nametransform.NameType(cName)
|
|
|
|
if nameType == nametransform.LongNameNone {
|
|
|
|
pName, err = rfs.nameTransform.DecryptName(cName, dirIV)
|
|
|
|
if err != nil {
|
|
|
|
// We get lots of decrypt requests for names like ".Trash" that
|
|
|
|
// are invalid base64. Convert them to ENOENT so the correct
|
|
|
|
// error gets returned to the user.
|
|
|
|
if _, ok := err.(base64.CorruptInputError); ok {
|
|
|
|
return "", syscall.ENOENT
|
|
|
|
}
|
|
|
|
// Stat attempts on the link target of encrypted symlinks.
|
|
|
|
// These are always valid base64 but the length is not a
|
|
|
|
// multiple of 16.
|
2017-07-27 20:31:22 +02:00
|
|
|
if err == syscall.EBADMSG {
|
2017-01-03 17:46:11 +01:00
|
|
|
return "", syscall.ENOENT
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else if nameType == nametransform.LongNameContent {
|
|
|
|
pName, err = rfs.findLongnameParent(pDir, dirIV, cName)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// It makes no sense to decrypt a ".name" file. This is a virtual file
|
2017-02-16 21:20:29 +01:00
|
|
|
// that has no representation in the plaintext filesystem. ".name"
|
2017-01-03 17:46:11 +01:00
|
|
|
// files should have already been handled in virtualfile.go.
|
2017-02-16 21:20:29 +01:00
|
|
|
tlog.Warn.Printf("rDecryptName: cannot decrypt virtual file %q", cName)
|
2017-01-03 17:46:11 +01:00
|
|
|
return "", syscall.EINVAL
|
|
|
|
}
|
|
|
|
return pName, nil
|
|
|
|
}
|
|
|
|
|
2016-11-10 00:38:01 +01:00
|
|
|
func (rfs *ReverseFS) decryptPath(relPath string) (string, error) {
|
2016-08-30 00:23:55 +02:00
|
|
|
if rfs.args.PlaintextNames || relPath == "" {
|
2016-08-25 00:14:36 +02:00
|
|
|
return relPath, nil
|
|
|
|
}
|
2017-01-03 18:14:01 +01:00
|
|
|
// Check if the parent dir is in the cache
|
|
|
|
cDir := saneDir(relPath)
|
|
|
|
dirIV, pDir := rPathCache.lookup(cDir)
|
|
|
|
if dirIV != nil {
|
|
|
|
cName := filepath.Base(relPath)
|
|
|
|
pName, err := rfs.rDecryptName(cName, dirIV, pDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(pDir, pName), nil
|
|
|
|
}
|
2016-08-25 00:14:36 +02:00
|
|
|
parts := strings.Split(relPath, "/")
|
2017-01-03 17:46:11 +01:00
|
|
|
var transformedParts []string
|
|
|
|
for i := range parts {
|
2016-09-22 23:28:11 +02:00
|
|
|
// Start at the top and recurse
|
2016-11-10 23:25:37 +01:00
|
|
|
currentCipherDir := filepath.Join(parts[:i]...)
|
2017-01-03 17:46:11 +01:00
|
|
|
currentPlainDir := filepath.Join(transformedParts[:i]...)
|
2017-05-28 18:09:02 +02:00
|
|
|
dirIV = pathiv.Derive(currentCipherDir, pathiv.PurposeDirIV)
|
2017-01-03 17:46:11 +01:00
|
|
|
transformedPart, err := rfs.rDecryptName(parts[i], dirIV, currentPlainDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
transformedParts = append(transformedParts, transformedPart)
|
|
|
|
}
|
2017-01-03 18:14:01 +01:00
|
|
|
pRelPath := filepath.Join(transformedParts...)
|
|
|
|
rPathCache.store(cDir, dirIV, saneDir(pRelPath))
|
|
|
|
return pRelPath, nil
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|