2016-02-06 19:27:59 +01:00
|
|
|
package fusefrontend
|
2015-12-11 19:54:53 +01:00
|
|
|
|
2015-12-19 13:21:15 +01:00
|
|
|
// Mkdir and Rmdir
|
|
|
|
|
2015-12-11 19:54:53 +01:00
|
|
|
import (
|
2015-12-13 20:10:52 +01:00
|
|
|
"fmt"
|
2016-06-27 00:25:56 +02:00
|
|
|
"io"
|
2015-12-11 19:54:53 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
2016-02-07 14:02:09 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/configfile"
|
2016-02-06 19:20:54 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/cryptocore"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
2016-07-03 20:05:32 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2015-12-11 19:54:53 +01:00
|
|
|
)
|
|
|
|
|
2016-04-10 19:32:10 +02:00
|
|
|
func (fs *FS) mkdirWithIv(cPath string, mode uint32) error {
|
|
|
|
// Between the creation of the directory and the creation of gocryptfs.diriv
|
|
|
|
// the directory is inconsistent. Take the lock to prevent other readers.
|
|
|
|
fs.dirIVLock.Lock()
|
|
|
|
// The new directory may take the place of an older one that is still in the cache
|
|
|
|
fs.nameTransform.DirIVCache.Clear()
|
|
|
|
defer fs.dirIVLock.Unlock()
|
|
|
|
err := os.Mkdir(cPath, os.FileMode(mode))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Create gocryptfs.diriv
|
|
|
|
err = nametransform.WriteDirIV(cPath)
|
|
|
|
if err != nil {
|
|
|
|
err2 := syscall.Rmdir(cPath)
|
|
|
|
if err2 != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("mkdirWithIv: rollback failed: %v", err2)
|
2016-04-10 19:32:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Mkdir(newPath string, mode uint32, context *fuse.Context) (code fuse.Status) {
|
|
|
|
if fs.isFiltered(newPath) {
|
2015-12-11 19:54:53 +01:00
|
|
|
return fuse.EPERM
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
cPath, err := fs.getBackingPath(newPath)
|
2015-12-11 19:54:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-06-27 00:25:56 +02:00
|
|
|
if fs.args.PlaintextNames {
|
|
|
|
err = os.Mkdir(cPath, os.FileMode(mode))
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
2015-12-11 19:54:53 +01:00
|
|
|
// We need write and execute permissions to create gocryptfs.diriv
|
|
|
|
origMode := mode
|
|
|
|
mode = mode | 0300
|
2016-04-10 19:32:10 +02:00
|
|
|
|
|
|
|
// Handle long file name
|
|
|
|
cName := filepath.Base(cPath)
|
|
|
|
if nametransform.IsLongContent(cName) {
|
2016-04-10 21:29:42 +02:00
|
|
|
var dirfd *os.File
|
|
|
|
dirfd, err = os.Open(filepath.Dir(cPath))
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
defer dirfd.Close()
|
|
|
|
|
|
|
|
// Create ".name"
|
|
|
|
err = fs.nameTransform.WriteLongName(dirfd, cName, newPath)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create directory
|
|
|
|
err = fs.mkdirWithIv(cPath, mode)
|
|
|
|
if err != nil {
|
|
|
|
nametransform.DeleteLongName(dirfd, cName)
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = fs.mkdirWithIv(cPath, mode)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set permissions back to what the user wanted
|
|
|
|
if origMode != mode {
|
2016-04-10 19:32:10 +02:00
|
|
|
err = os.Chmod(cPath, os.FileMode(origMode))
|
2015-12-11 19:54:53 +01:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("Mkdir: Chmod failed: %v", err)
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fuse.OK
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:32:10 +02:00
|
|
|
func (fs *FS) Rmdir(path string, context *fuse.Context) (code fuse.Status) {
|
|
|
|
cPath, err := fs.getBackingPath(path)
|
2015-12-11 19:54:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-06-27 00:25:56 +02:00
|
|
|
if fs.args.PlaintextNames {
|
|
|
|
err = syscall.Rmdir(cPath)
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
parentDir := filepath.Dir(cPath)
|
|
|
|
parentDirFd, err := os.Open(parentDir)
|
|
|
|
if err != nil {
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
defer parentDirFd.Close()
|
|
|
|
|
|
|
|
cName := filepath.Base(cPath)
|
2016-07-03 20:17:40 +02:00
|
|
|
dirfdRaw, err := syscallcompat.Openat(int(parentDirFd.Fd()), cName,
|
2016-04-10 19:32:10 +02:00
|
|
|
syscall.O_RDONLY, 0)
|
|
|
|
if err == syscall.EACCES {
|
2015-12-11 19:54:53 +01:00
|
|
|
// We need permission to read and modify the directory
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("Rmdir: handling EACCESS")
|
2016-04-10 19:32:10 +02:00
|
|
|
// TODO use syscall.Fstatat once it is available in Go
|
|
|
|
var fi os.FileInfo
|
|
|
|
fi, err = os.Lstat(cPath)
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("Rmdir: Stat: %v", err)
|
2016-04-10 19:32:10 +02:00
|
|
|
return fuse.ToStatus(err)
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
|
|
|
origMode := fi.Mode()
|
2016-04-10 19:32:10 +02:00
|
|
|
// TODO use syscall.Chmodat once it is available in Go
|
|
|
|
err = os.Chmod(cPath, origMode|0700)
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("Rmdir: Chmod failed: %v", err)
|
2015-12-11 19:54:53 +01:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
// Retry open
|
|
|
|
var st syscall.Stat_t
|
|
|
|
syscall.Lstat(cPath, &st)
|
2016-07-03 20:17:40 +02:00
|
|
|
dirfdRaw, err = syscallcompat.Openat(int(parentDirFd.Fd()), cName,
|
2016-04-10 19:32:10 +02:00
|
|
|
syscall.O_RDONLY, 0)
|
|
|
|
// Undo the chmod if removing the directory failed
|
2015-12-13 20:10:52 +01:00
|
|
|
defer func() {
|
2015-12-11 19:54:53 +01:00
|
|
|
if code != fuse.OK {
|
2016-06-14 22:45:33 +02:00
|
|
|
err = os.Chmod(cPath, origMode)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("Rmdir: Chmod rollback failed: %v", err)
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("Rmdir: Open: %v", err)
|
2015-12-11 19:54:53 +01:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
dirfd := os.NewFile(uintptr(dirfdRaw), cName)
|
|
|
|
defer dirfd.Close()
|
|
|
|
|
|
|
|
children, err := dirfd.Readdirnames(10)
|
2016-06-27 00:25:56 +02:00
|
|
|
if err == nil {
|
|
|
|
// If the directory is not empty besides gocryptfs.diriv, do not even
|
|
|
|
// attempt the dance around gocryptfs.diriv.
|
|
|
|
if len(children) > 1 {
|
|
|
|
return fuse.ToStatus(syscall.ENOTEMPTY)
|
|
|
|
}
|
|
|
|
// Move "gocryptfs.diriv" to the parent dir as "gocryptfs.diriv.rmdir.XYZ"
|
|
|
|
tmpName := fmt.Sprintf("gocryptfs.diriv.rmdir.%d", cryptocore.RandUint64())
|
|
|
|
tlog.Debug.Printf("Rmdir: Renaming %s to %s", nametransform.DirIVFilename, tmpName)
|
|
|
|
// The directory is in an inconsistent state between rename and rmdir.
|
|
|
|
// Protect against concurrent readers.
|
|
|
|
fs.dirIVLock.Lock()
|
|
|
|
defer fs.dirIVLock.Unlock()
|
2016-07-03 20:05:32 +02:00
|
|
|
err = syscallcompat.Renameat(int(dirfd.Fd()), nametransform.DirIVFilename,
|
2016-06-27 00:25:56 +02:00
|
|
|
int(parentDirFd.Fd()), tmpName)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("Rmdir: Renaming %s to %s failed: %v",
|
|
|
|
nametransform.DirIVFilename, tmpName, err)
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
// Actual Rmdir
|
|
|
|
// TODO Use syscall.Unlinkat with the AT_REMOVEDIR flag once it is available
|
|
|
|
// in Go
|
|
|
|
err = syscall.Rmdir(cPath)
|
|
|
|
if err != nil {
|
|
|
|
// This can happen if another file in the directory was created in the
|
|
|
|
// meantime, undo the rename
|
2016-07-03 20:05:32 +02:00
|
|
|
err2 := syscallcompat.Renameat(int(parentDirFd.Fd()), tmpName,
|
2016-06-27 00:25:56 +02:00
|
|
|
int(dirfd.Fd()), nametransform.DirIVFilename)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("Rmdir: Rename rollback failed: %v", err2)
|
|
|
|
}
|
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
// Delete "gocryptfs.diriv.rmdir.XYZ"
|
2016-07-03 20:17:40 +02:00
|
|
|
err = syscallcompat.Unlinkat(int(parentDirFd.Fd()), tmpName)
|
2016-06-27 00:25:56 +02:00
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("Rmdir: Could not clean up %s: %v", tmpName, err)
|
|
|
|
}
|
|
|
|
} else if err == io.EOF {
|
|
|
|
// The directory is empty
|
|
|
|
tlog.Warn.Printf("Rmdir: %q: gocryptfs.diriv is missing", cPath)
|
|
|
|
err = syscall.Rmdir(cPath)
|
2016-04-10 19:32:10 +02:00
|
|
|
if err != nil {
|
2016-06-27 00:25:56 +02:00
|
|
|
return fuse.ToStatus(err)
|
2015-12-11 19:54:53 +01:00
|
|
|
}
|
2016-06-27 00:25:56 +02:00
|
|
|
} else {
|
|
|
|
tlog.Warn.Printf("Rmdir: Readdirnames: %v", err)
|
2015-12-11 19:54:53 +01:00
|
|
|
return fuse.ToStatus(err)
|
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
// Delete .name file
|
|
|
|
if nametransform.IsLongContent(cName) {
|
|
|
|
nametransform.DeleteLongName(parentDirFd, cName)
|
2016-02-07 14:02:09 +01:00
|
|
|
}
|
2015-12-11 19:54:53 +01:00
|
|
|
// The now-deleted directory may have been in the DirIV cache. Clear it.
|
2016-02-06 19:20:54 +01:00
|
|
|
fs.nameTransform.DirIVCache.Clear()
|
2015-12-11 19:54:53 +01:00
|
|
|
return fuse.OK
|
|
|
|
}
|
2016-02-07 11:29:54 +01:00
|
|
|
|
|
|
|
func (fs *FS) OpenDir(dirName string, context *fuse.Context) ([]fuse.DirEntry, fuse.Status) {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("OpenDir(%s)", dirName)
|
2016-02-07 11:29:54 +01:00
|
|
|
cDirName, err := fs.encryptPath(dirName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
// Read ciphertext directory
|
|
|
|
cipherEntries, status := fs.FileSystem.OpenDir(cDirName, context)
|
|
|
|
if cipherEntries == nil {
|
|
|
|
return nil, status
|
|
|
|
}
|
2016-06-23 21:29:00 +02:00
|
|
|
// Get DirIV (stays nil if PlaintextNames is used)
|
2016-02-07 11:29:54 +01:00
|
|
|
var cachedIV []byte
|
|
|
|
var cDirAbsPath string
|
2016-06-23 21:29:00 +02:00
|
|
|
if !fs.args.PlaintextNames {
|
2016-02-07 11:29:54 +01:00
|
|
|
// Read the DirIV once and use it for all later name decryptions
|
|
|
|
cDirAbsPath = filepath.Join(fs.args.Cipherdir, cDirName)
|
|
|
|
cachedIV, err = nametransform.ReadDirIV(cDirAbsPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fuse.ToStatus(err)
|
|
|
|
}
|
|
|
|
}
|
2016-06-04 16:39:27 +02:00
|
|
|
|
|
|
|
// Decrypted directory entries
|
2016-02-07 11:29:54 +01:00
|
|
|
var plain []fuse.DirEntry
|
2016-06-04 16:39:27 +02:00
|
|
|
var errorCount int
|
|
|
|
// Filter and decrypt filenames
|
2016-02-07 11:29:54 +01:00
|
|
|
for i := range cipherEntries {
|
|
|
|
cName := cipherEntries[i].Name
|
|
|
|
if dirName == "" && cName == configfile.ConfDefaultName {
|
|
|
|
// silently ignore "gocryptfs.conf" in the top level dir
|
|
|
|
continue
|
|
|
|
}
|
2016-06-23 21:29:00 +02:00
|
|
|
if !fs.args.PlaintextNames && cName == nametransform.DirIVFilename {
|
2016-02-07 11:29:54 +01:00
|
|
|
// silently ignore "gocryptfs.diriv" everywhere if dirIV is enabled
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if fs.args.PlaintextNames {
|
|
|
|
plain = append(plain, cipherEntries[i])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-06-04 16:39:27 +02:00
|
|
|
// Handle long file name
|
|
|
|
isLong := nametransform.LongNameNone
|
2016-02-07 11:29:54 +01:00
|
|
|
if fs.args.LongNames {
|
2016-06-04 16:39:27 +02:00
|
|
|
isLong = nametransform.NameType(cName)
|
|
|
|
}
|
|
|
|
if isLong == nametransform.LongNameContent {
|
|
|
|
cNameLong, err := nametransform.ReadLongName(filepath.Join(cDirAbsPath, cName))
|
|
|
|
if err != nil {
|
2016-07-03 15:35:58 +02:00
|
|
|
tlog.Warn.Printf("Skipping entry %q in dir %q: Could not read .name: %v",
|
2016-06-04 16:39:27 +02:00
|
|
|
cName, cDirName, err)
|
|
|
|
errorCount++
|
2016-02-07 11:29:54 +01:00
|
|
|
continue
|
|
|
|
}
|
2016-06-04 16:39:27 +02:00
|
|
|
cName = cNameLong
|
|
|
|
} else if isLong == nametransform.LongNameFilename {
|
|
|
|
// ignore "gocryptfs.longname.*.name"
|
|
|
|
continue
|
2016-02-07 11:29:54 +01:00
|
|
|
}
|
2016-06-04 16:39:27 +02:00
|
|
|
|
2016-02-07 11:29:54 +01:00
|
|
|
name, err := fs.nameTransform.DecryptName(cName, cachedIV)
|
|
|
|
if err != nil {
|
2016-07-03 15:35:58 +02:00
|
|
|
tlog.Warn.Printf("Skipping entry %q in dir %q: %s",
|
2016-04-10 12:36:43 +02:00
|
|
|
cName, cDirName, err)
|
2016-06-04 16:39:27 +02:00
|
|
|
errorCount++
|
2016-02-07 11:29:54 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
cipherEntries[i].Name = name
|
|
|
|
plain = append(plain, cipherEntries[i])
|
|
|
|
}
|
2016-06-04 16:39:27 +02:00
|
|
|
|
|
|
|
if errorCount > 0 && len(plain) == 0 {
|
|
|
|
// Don't let the user stare on an empty directory. Report that things went
|
|
|
|
// wrong.
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("All %d entries in directory %q were invalid, returning EIO",
|
2016-06-04 16:39:27 +02:00
|
|
|
errorCount, cDirName)
|
|
|
|
status = fuse.EIO
|
|
|
|
}
|
|
|
|
|
2016-02-07 11:29:54 +01:00
|
|
|
return plain, status
|
|
|
|
}
|