2016-08-25 00:14:36 +02:00
|
|
|
package fusefrontend_reverse
|
|
|
|
|
|
|
|
import (
|
2016-09-28 23:30:13 +02:00
|
|
|
"crypto/sha256"
|
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"
|
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
|
|
|
|
}
|
|
|
|
|
2016-09-29 21:29:45 +02:00
|
|
|
type ivPurposeType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ivPurposeDirIV ivPurposeType = "DIRIV"
|
|
|
|
ivPurposeFileID ivPurposeType = "FILEID"
|
|
|
|
ivPurposeSymlinkIV ivPurposeType = "SYMLINKIV"
|
|
|
|
ivPurposeBlock0IV ivPurposeType = "BLOCK0IV"
|
|
|
|
)
|
|
|
|
|
|
|
|
// derivePathIV derives an IV from an encrypted path by hashing it with sha256
|
|
|
|
func derivePathIV(path string, purpose ivPurposeType) []byte {
|
2016-10-24 19:18:13 +02:00
|
|
|
// Use null byte as separator as it cannot occur in the path
|
2016-09-29 21:29:45 +02:00
|
|
|
extended := []byte(path + "\000" + string(purpose))
|
|
|
|
hash := sha256.Sum256(extended)
|
2016-09-28 23:30:13 +02:00
|
|
|
return hash[:nametransform.DirIVLen]
|
|
|
|
}
|
|
|
|
|
2016-08-30 00:23:55 +02: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
|
|
|
}
|
|
|
|
|
2016-08-30 00:23:55 +02:00
|
|
|
func (rfs *reverseFS) decryptPath(relPath string) (string, error) {
|
|
|
|
if rfs.args.PlaintextNames || relPath == "" {
|
2016-08-25 00:14:36 +02:00
|
|
|
return relPath, nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
var transformedParts []string
|
|
|
|
parts := strings.Split(relPath, "/")
|
2016-09-19 23:40:43 +02:00
|
|
|
for i, part := range parts {
|
2016-09-22 23:28:11 +02:00
|
|
|
// Start at the top and recurse
|
|
|
|
currentDir := filepath.Join(parts[:i]...)
|
|
|
|
nameType := nametransform.NameType(part)
|
2016-09-29 21:29:45 +02:00
|
|
|
dirIV := derivePathIV(currentDir, ivPurposeDirIV)
|
2016-08-25 00:14:36 +02:00
|
|
|
var transformedPart string
|
2016-09-22 23:28:11 +02:00
|
|
|
if nameType == nametransform.LongNameNone {
|
|
|
|
transformedPart, err = rfs.nameTransform.DecryptName(part, 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
|
|
|
|
}
|
2016-09-25 18:01:24 +02:00
|
|
|
// Stat attempts on the link target of encrypted symlinks.
|
|
|
|
// These are always valid base64 but the length is not a
|
|
|
|
// multiple of 16.
|
|
|
|
if err == syscall.EINVAL {
|
|
|
|
return "", syscall.ENOENT
|
|
|
|
}
|
2016-09-22 23:28:11 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else if nameType == nametransform.LongNameContent {
|
|
|
|
transformedPart, err = rfs.findLongnameParent(currentDir, dirIV, part)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
2016-09-22 23:28:11 +02:00
|
|
|
} else {
|
|
|
|
panic("longname bug, .name files should have been handled earlier")
|
2016-08-25 00:14:36 +02:00
|
|
|
}
|
|
|
|
transformedParts = append(transformedParts, transformedPart)
|
|
|
|
}
|
|
|
|
return filepath.Join(transformedParts...), nil
|
|
|
|
}
|