2016-02-06 22:54:14 +01:00
|
|
|
package nametransform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2016-02-07 14:02:09 +01:00
|
|
|
"io/ioutil"
|
2016-04-10 19:32:10 +02:00
|
|
|
"os"
|
2016-02-07 14:02:09 +01:00
|
|
|
"path/filepath"
|
2016-02-06 22:54:14 +01:00
|
|
|
"strings"
|
2016-02-07 14:02:09 +01:00
|
|
|
"syscall"
|
2016-02-06 22:54:14 +01:00
|
|
|
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2016-02-06 22:54:14 +01:00
|
|
|
)
|
|
|
|
|
2016-04-10 19:32:10 +02:00
|
|
|
const (
|
|
|
|
// Files with long names are stored in two files:
|
|
|
|
// gocryptfs.longname.[sha256] <--- File content, prefix = gocryptfs.longname.
|
|
|
|
// gocryptfs.longname.[sha256].name <--- File name, suffix = .name
|
|
|
|
LongNameSuffix = ".name"
|
|
|
|
longNamePrefix = "gocryptfs.longname."
|
|
|
|
)
|
2016-02-06 22:54:14 +01:00
|
|
|
|
|
|
|
// HashLongName - take the hash of a long string "name" and return
|
|
|
|
// "gocryptfs.longname.[sha256]"
|
|
|
|
func HashLongName(name string) string {
|
|
|
|
hashBin := sha256.Sum256([]byte(name))
|
|
|
|
hashBase64 := base64.URLEncoding.EncodeToString(hashBin[:])
|
|
|
|
return longNamePrefix + hashBase64
|
|
|
|
}
|
|
|
|
|
2016-04-10 12:36:43 +02:00
|
|
|
// Values returned by IsLongName
|
|
|
|
const (
|
|
|
|
LongNameContent = iota
|
|
|
|
LongNameFilename = iota
|
|
|
|
LongNameNone = iota
|
|
|
|
)
|
|
|
|
|
2016-04-10 19:32:10 +02:00
|
|
|
// NameType - detect if cName is
|
2016-04-10 12:36:43 +02:00
|
|
|
// gocryptfs.longname.[sha256] ........ LongNameContent (content of a long name file)
|
|
|
|
// gocryptfs.longname.[sha256].name .... LongNameFilename (full file name of a long name file)
|
|
|
|
// else ................................ LongNameNone (normal file)
|
2016-04-10 19:32:10 +02:00
|
|
|
func NameType(cName string) int {
|
2016-02-06 22:54:14 +01:00
|
|
|
if !strings.HasPrefix(cName, longNamePrefix) {
|
2016-04-10 12:36:43 +02:00
|
|
|
return LongNameNone
|
2016-02-06 22:54:14 +01:00
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
if strings.HasSuffix(cName, LongNameSuffix) {
|
2016-04-10 12:36:43 +02:00
|
|
|
return LongNameFilename
|
2016-02-06 22:54:14 +01:00
|
|
|
}
|
2016-04-10 12:36:43 +02:00
|
|
|
return LongNameContent
|
2016-02-06 22:54:14 +01:00
|
|
|
}
|
|
|
|
|
2016-04-10 19:32:10 +02:00
|
|
|
// IsLongContent returns true if "cName" is the content store of a long name file.
|
|
|
|
func IsLongContent(cName string) bool {
|
|
|
|
return NameType(cName) == LongNameContent
|
|
|
|
}
|
|
|
|
|
2016-02-07 14:02:09 +01:00
|
|
|
// ReadLongName - read path.name
|
2016-02-06 22:54:14 +01:00
|
|
|
func ReadLongName(path string) (string, error) {
|
2016-04-10 19:32:10 +02:00
|
|
|
content, err := ioutil.ReadFile(path + LongNameSuffix)
|
2016-02-06 22:54:14 +01:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("ReadLongName: %v", err)
|
2016-02-06 22:54:14 +01:00
|
|
|
}
|
|
|
|
return string(content), err
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:32:10 +02:00
|
|
|
// DeleteLongName deletes "hashName.name".
|
|
|
|
func DeleteLongName(dirfd *os.File, hashName string) error {
|
|
|
|
err := syscall.Unlinkat(int(dirfd.Fd()), hashName+LongNameSuffix)
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("DeleteLongName: %v", err)
|
2016-02-06 22:54:14 +01:00
|
|
|
}
|
2016-04-10 19:32:10 +02:00
|
|
|
return err
|
2016-02-07 14:02:09 +01:00
|
|
|
}
|
2016-02-06 22:54:14 +01:00
|
|
|
|
2016-04-10 19:32:10 +02:00
|
|
|
// WriteLongName encrypts plainName and writes it into "hashName.name".
|
|
|
|
// For the convenience of the caller, plainName may also be a path and will be
|
|
|
|
// converted internally.
|
|
|
|
func (n *NameTransform) WriteLongName(dirfd *os.File, hashName string, plainName string) (err error) {
|
|
|
|
plainName = filepath.Base(plainName)
|
|
|
|
|
|
|
|
// Encrypt the basename
|
|
|
|
dirIV, err := ReadDirIVAt(dirfd)
|
2016-02-06 22:54:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cName := n.EncryptName(plainName, dirIV)
|
2016-04-10 19:32:10 +02:00
|
|
|
|
|
|
|
// Write the encrypted name into hashName.name
|
|
|
|
fdRaw, err := syscall.Openat(int(dirfd.Fd()), hashName+LongNameSuffix,
|
|
|
|
syscall.O_WRONLY|syscall.O_CREAT|syscall.O_EXCL, 0600)
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("WriteLongName: Openat: %v", err)
|
2016-04-10 19:32:10 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
fd := os.NewFile(uintptr(fdRaw), hashName+LongNameSuffix)
|
|
|
|
defer fd.Close()
|
|
|
|
_, err = fd.Write([]byte(cName))
|
2016-02-06 22:54:14 +01:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Printf("WriteLongName: Write: %v", err)
|
2016-02-06 22:54:14 +01:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|