2016-11-10 00:27:08 +01:00
|
|
|
package fusefrontend
|
|
|
|
|
|
|
|
import (
|
2017-05-07 21:01:39 +02:00
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2018-10-01 21:28:54 +02:00
|
|
|
"syscall"
|
2016-11-10 00:27:08 +01:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/ctlsock"
|
2017-05-07 21:01:39 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
2018-10-01 21:28:54 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2016-11-10 00:27:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ ctlsock.Interface = &FS{} // Verify that interface is implemented.
|
|
|
|
|
|
|
|
// EncryptPath implements ctlsock.Backend
|
2018-11-04 21:27:13 +01:00
|
|
|
//
|
|
|
|
// TODO: this function is NOT symlink-safe.
|
2016-11-10 00:27:08 +01:00
|
|
|
func (fs *FS) EncryptPath(plainPath string) (string, error) {
|
|
|
|
return fs.encryptPath(plainPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptPath implements ctlsock.Backend
|
2018-11-04 21:27:13 +01:00
|
|
|
//
|
|
|
|
// DecryptPath is symlink-safe because openBackingDir() and decryptPathAt()
|
|
|
|
// are symlink-safe.
|
2018-10-01 21:28:54 +02:00
|
|
|
func (fs *FS) DecryptPath(cipherPath string) (plainPath string, err error) {
|
2018-11-04 21:27:13 +01:00
|
|
|
dirfd, _, err := fs.openBackingDir("")
|
2018-10-01 21:28:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer syscall.Close(dirfd)
|
|
|
|
return fs.decryptPathAt(dirfd, cipherPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decryptPathAt decrypts a ciphertext path relative to dirfd.
|
2018-11-04 21:27:13 +01:00
|
|
|
//
|
|
|
|
// Symlink-safe through ReadDirIVAt() and ReadLongNameAt().
|
2018-10-01 21:28:54 +02:00
|
|
|
func (fs *FS) decryptPathAt(dirfd int, cipherPath string) (plainPath string, err error) {
|
2017-05-07 21:01:39 +02:00
|
|
|
if fs.args.PlaintextNames || cipherPath == "" {
|
|
|
|
return cipherPath, nil
|
|
|
|
}
|
|
|
|
parts := strings.Split(cipherPath, "/")
|
2018-10-01 21:28:54 +02:00
|
|
|
wd := dirfd
|
|
|
|
for i, part := range parts {
|
|
|
|
dirIV, err := nametransform.ReadDirIVAt(wd)
|
2017-05-07 21:01:39 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ReadDirIV: %v\n", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
longPart := part
|
|
|
|
if nametransform.IsLongContent(part) {
|
2018-10-01 21:28:54 +02:00
|
|
|
longPart, err = nametransform.ReadLongNameAt(wd, part)
|
2017-05-07 21:01:39 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ReadLongName: %v\n", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
name, err := fs.nameTransform.DecryptName(longPart, dirIV)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("DecryptName: %v\n", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
plainPath = path.Join(plainPath, name)
|
2018-10-01 21:28:54 +02:00
|
|
|
// Last path component? We are done.
|
|
|
|
if i == len(parts)-1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Descend into next directory
|
|
|
|
oldWd := wd
|
|
|
|
wd, err = syscallcompat.Openat(wd, part, syscall.O_NOFOLLOW, 0)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// Unless we are in the first iteration, where dirfd is our wd, close
|
|
|
|
// the old working directory.
|
|
|
|
if i > 0 {
|
|
|
|
syscall.Close(oldWd)
|
|
|
|
}
|
2017-05-07 21:01:39 +02:00
|
|
|
}
|
|
|
|
return plainPath, nil
|
2016-11-10 00:27:08 +01:00
|
|
|
}
|