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-07-03 20:17:40 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/syscallcompat"
|
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 (
|
2016-10-02 06:14:18 +02:00
|
|
|
// LongNameSuffix is the suffix used for files with long names.
|
2016-04-10 19:32:10 +02:00
|
|
|
// 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 (
|
2016-10-02 06:14:18 +02:00
|
|
|
// LongNameContent is the file that stores the file content.
|
2016-09-22 23:28:11 +02:00
|
|
|
// Example: gocryptfs.longname.URrM8kgxTKYMgCk4hKk7RO9Lcfr30XQof4L_5bD9Iro=
|
|
|
|
LongNameContent = iota
|
2016-10-02 06:14:18 +02:00
|
|
|
// LongNameFilename is the file that stores the full encrypted filename.
|
2016-09-22 23:28:11 +02:00
|
|
|
// Example: gocryptfs.longname.URrM8kgxTKYMgCk4hKk7RO9Lcfr30XQof4L_5bD9Iro=.name
|
2016-04-10 12:36:43 +02:00
|
|
|
LongNameFilename = iota
|
2016-10-02 06:14:18 +02:00
|
|
|
// LongNameNone is used when the file does not have a long name.
|
2016-09-22 23:28:11 +02:00
|
|
|
// Example: i1bpTaVLZq7sRNA9mL_2Ig==
|
|
|
|
LongNameNone = iota
|
2016-04-10 12:36:43 +02:00
|
|
|
)
|
|
|
|
|
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 {
|
2016-07-03 20:17:40 +02:00
|
|
|
err := syscallcompat.Unlinkat(int(dirfd.Fd()), hashName+LongNameSuffix)
|
2016-04-10 19:32:10 +02:00
|
|
|
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
|
2016-07-03 20:17:40 +02:00
|
|
|
fdRaw, err := syscallcompat.Openat(int(dirfd.Fd()), hashName+LongNameSuffix,
|
2016-04-10 19:32:10 +02:00
|
|
|
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
|
|
|
|
}
|