2016-11-10 00:27:08 +01:00
|
|
|
package fusefrontend
|
|
|
|
|
|
|
|
import (
|
2017-05-07 21:01:39 +02:00
|
|
|
"path"
|
2019-01-02 21:45:40 +01:00
|
|
|
"path/filepath"
|
2017-05-07 21:01:39 +02:00
|
|
|
"strings"
|
2018-10-01 21:28:54 +02:00
|
|
|
"syscall"
|
2016-11-10 00:27:08 +01:00
|
|
|
|
2020-05-09 17:36:41 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/ctlsocksrv"
|
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"
|
2019-01-02 21:45:40 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2016-11-10 00:27:08 +01:00
|
|
|
)
|
|
|
|
|
2020-07-17 22:14:40 +02:00
|
|
|
var _ ctlsocksrv.Interface = &RootNode{} // Verify that interface is implemented.
|
2016-11-10 00:27:08 +01:00
|
|
|
|
|
|
|
// EncryptPath implements ctlsock.Backend
|
2018-11-04 21:27:13 +01:00
|
|
|
//
|
2019-01-02 21:45:40 +01:00
|
|
|
// Symlink-safe through openBackingDir().
|
2021-06-26 18:39:23 +02:00
|
|
|
func (rn *RootNode) EncryptPath(plainPath string) (cipherPath string, err error) {
|
2021-06-26 18:42:36 +02:00
|
|
|
if rn.args.PlaintextNames || plainPath == "" {
|
2019-01-02 21:45:40 +01:00
|
|
|
return plainPath, nil
|
|
|
|
}
|
2021-06-26 18:39:23 +02:00
|
|
|
|
|
|
|
dirfd, _, errno := rn.prepareAtSyscallMyself()
|
|
|
|
if errno != 0 {
|
|
|
|
return "", errno
|
|
|
|
}
|
|
|
|
defer syscall.Close(dirfd)
|
|
|
|
|
|
|
|
// Encrypt path level by level
|
2019-01-02 21:45:40 +01:00
|
|
|
parts := strings.Split(plainPath, "/")
|
2021-06-26 18:39:23 +02:00
|
|
|
wd := dirfd
|
|
|
|
for i, part := range parts {
|
|
|
|
dirIV, err := nametransform.ReadDirIVAt(wd)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
cPart, err := rn.nameTransform.EncryptAndHashName(part, dirIV)
|
2019-01-02 21:45:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-06-26 18:39:23 +02:00
|
|
|
cipherPath = filepath.Join(cipherPath, cPart)
|
|
|
|
// Last path component? We are done.
|
|
|
|
if i == len(parts)-1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Descend into next directory
|
|
|
|
wd, err = syscallcompat.Openat(wd, cPart, syscall.O_NOFOLLOW|syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// Yes this is somewhat wasteful in terms of used file descriptors:
|
|
|
|
// we keep them all open until the function returns. But it is simple
|
|
|
|
// and reliable.
|
|
|
|
defer syscall.Close(wd)
|
2019-01-02 21:45:40 +01:00
|
|
|
}
|
2021-06-26 18:39:23 +02:00
|
|
|
tlog.Debug.Printf("EncryptPath %q -> %q", plainPath, cipherPath)
|
|
|
|
return cipherPath, nil
|
2016-11-10 00:27:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptPath implements ctlsock.Backend
|
2018-11-04 21:27:13 +01:00
|
|
|
//
|
|
|
|
// DecryptPath is symlink-safe because openBackingDir() and decryptPathAt()
|
|
|
|
// are symlink-safe.
|
2020-07-17 22:14:40 +02:00
|
|
|
func (rn *RootNode) DecryptPath(cipherPath string) (plainPath string, err error) {
|
2021-06-26 18:42:36 +02:00
|
|
|
if rn.args.PlaintextNames || cipherPath == "" {
|
|
|
|
return cipherPath, nil
|
|
|
|
}
|
|
|
|
|
2021-06-26 18:39:23 +02:00
|
|
|
dirfd, _, errno := rn.prepareAtSyscallMyself()
|
|
|
|
if errno != 0 {
|
|
|
|
return "", errno
|
2018-10-01 21:28:54 +02:00
|
|
|
}
|
|
|
|
defer syscall.Close(dirfd)
|
|
|
|
|
2021-06-26 18:42:36 +02:00
|
|
|
// Decrypt path level by level
|
2017-05-07 21:01:39 +02:00
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2020-07-17 22:14:40 +02:00
|
|
|
name, err := rn.nameTransform.DecryptName(longPart, dirIV)
|
2017-05-07 21:01:39 +02:00
|
|
|
if err != nil {
|
|
|
|
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
|
2019-01-03 17:54:38 +01:00
|
|
|
wd, err = syscallcompat.Openat(wd, part, syscall.O_NOFOLLOW|syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
|
2018-10-01 21:28:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-01-01 20:49:56 +01:00
|
|
|
// Yes this is somewhat wasteful in terms of used file descriptors:
|
|
|
|
// we keep them all open until the function returns. But it is simple
|
|
|
|
// and reliable.
|
|
|
|
defer syscall.Close(wd)
|
2017-05-07 21:01:39 +02:00
|
|
|
}
|
|
|
|
return plainPath, nil
|
2016-11-10 00:27:08 +01:00
|
|
|
}
|