2016-09-20 19:59:08 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-04-01 12:31:44 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2016-09-20 19:59:08 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2019-01-03 13:32:13 +01:00
|
|
|
"syscall"
|
2016-09-20 19:59:08 +02:00
|
|
|
|
2021-08-23 15:05:15 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/configfile"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/cryptocore"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/exitcodes"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/fido2"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/nametransform"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/readpassword"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/syscallcompat"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/tlog"
|
2016-09-20 19:59:08 +02:00
|
|
|
)
|
|
|
|
|
2019-08-04 14:13:00 +02:00
|
|
|
// isEmptyDir checks if "dir" exists and is an empty directory.
|
2018-04-01 12:31:44 +02:00
|
|
|
// Returns an *os.PathError if Stat() on the path fails.
|
2019-08-04 14:13:00 +02:00
|
|
|
func isEmptyDir(dir string) error {
|
2018-04-01 12:31:44 +02:00
|
|
|
err := isDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
entries, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(entries) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("directory %s not empty", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// isDir checks if "dir" exists and is a directory.
|
|
|
|
func isDir(dir string) error {
|
|
|
|
fi, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return fmt.Errorf("%s is not a directory", dir)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-17 15:25:09 +02:00
|
|
|
// initDir handles "gocryptfs -init". It prepares a directory for use as a
|
|
|
|
// gocryptfs storage directory.
|
2016-09-20 20:15:55 +02:00
|
|
|
// In forward mode, this means creating the gocryptfs.conf and gocryptfs.diriv
|
|
|
|
// files in an empty directory.
|
|
|
|
// In reverse mode, we create .gocryptfs.reverse.conf and the directory does
|
2018-06-17 15:25:09 +02:00
|
|
|
// not need to be empty.
|
2016-09-20 19:59:08 +02:00
|
|
|
func initDir(args *argContainer) {
|
2016-09-20 20:15:55 +02:00
|
|
|
var err error
|
2016-09-20 22:49:23 +02:00
|
|
|
if args.reverse {
|
|
|
|
_, err = os.Stat(args.config)
|
|
|
|
if err == nil {
|
|
|
|
tlog.Fatal.Printf("Config file %q already exists", args.config)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.Init)
|
2016-09-20 22:49:23 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-08-04 14:13:00 +02:00
|
|
|
err = isEmptyDir(args.cipherdir)
|
2016-09-20 20:15:55 +02:00
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Printf("Invalid cipherdir: %v", err)
|
2020-09-06 11:35:25 +02:00
|
|
|
os.Exit(exitcodes.CipherDir)
|
2016-09-20 20:15:55 +02:00
|
|
|
}
|
2016-09-20 19:59:08 +02:00
|
|
|
}
|
2016-09-20 20:15:55 +02:00
|
|
|
// Choose password for config file
|
2021-08-10 19:09:58 +02:00
|
|
|
if len(args.extpass) == 0 && args.fido2 == "" {
|
2016-09-20 19:59:08 +02:00
|
|
|
tlog.Info.Printf("Choose a password for protecting your files.")
|
|
|
|
}
|
2018-02-18 12:42:22 +01:00
|
|
|
{
|
2020-09-05 22:42:15 +02:00
|
|
|
var password []byte
|
|
|
|
var fido2CredentialID, fido2HmacSalt []byte
|
|
|
|
if args.fido2 != "" {
|
|
|
|
fido2CredentialID = fido2.Register(args.fido2, filepath.Base(args.cipherdir))
|
|
|
|
fido2HmacSalt = cryptocore.RandBytes(32)
|
|
|
|
password = fido2.Secret(args.fido2, fido2CredentialID, fido2HmacSalt)
|
|
|
|
} else {
|
|
|
|
// normal password entry
|
|
|
|
password = readpassword.Twice([]string(args.extpass), []string(args.passfile))
|
|
|
|
fido2CredentialID = nil
|
|
|
|
fido2HmacSalt = nil
|
|
|
|
}
|
2018-02-18 12:42:22 +01:00
|
|
|
creator := tlog.ProgramName + " " + GitVersion
|
2021-08-21 14:04:04 +02:00
|
|
|
err = configfile.Create(&configfile.CreateArgs{
|
2021-08-21 14:01:58 +02:00
|
|
|
Filename: args.config,
|
|
|
|
Password: password,
|
|
|
|
PlaintextNames: args.plaintextnames,
|
|
|
|
LogN: args.scryptn,
|
|
|
|
Creator: creator,
|
|
|
|
AESSIV: args.aessiv,
|
|
|
|
Fido2CredentialID: fido2CredentialID,
|
|
|
|
Fido2HmacSalt: fido2HmacSalt,
|
2021-08-21 21:43:26 +02:00
|
|
|
DeterministicNames: args.deterministic_names,
|
|
|
|
XChaCha20Poly1305: args.xchacha})
|
2018-02-18 12:42:22 +01:00
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Println(err)
|
|
|
|
os.Exit(exitcodes.WriteConf)
|
|
|
|
}
|
2018-02-18 15:22:22 +01:00
|
|
|
for i := range password {
|
|
|
|
password[i] = 0
|
|
|
|
}
|
2018-02-18 12:42:22 +01:00
|
|
|
// password runs out of scope here
|
2016-09-20 19:59:08 +02:00
|
|
|
}
|
2018-06-17 15:25:09 +02:00
|
|
|
// Forward mode with filename encryption enabled needs a gocryptfs.diriv file
|
2016-09-20 20:15:55 +02:00
|
|
|
// in the root dir
|
2021-08-20 10:57:26 +02:00
|
|
|
if !args.plaintextnames && !args.reverse && !args.deterministic_names {
|
2019-01-03 13:32:13 +01:00
|
|
|
// Open cipherdir (following symlinks)
|
2019-01-03 18:11:07 +01:00
|
|
|
dirfd, err := syscall.Open(args.cipherdir, syscall.O_DIRECTORY|syscallcompat.O_PATH, 0)
|
2019-01-03 13:32:13 +01:00
|
|
|
if err == nil {
|
2021-08-20 10:57:26 +02:00
|
|
|
err = nametransform.WriteDirIVAt(dirfd)
|
2019-01-03 13:32:13 +01:00
|
|
|
syscall.Close(dirfd)
|
|
|
|
}
|
2016-09-20 19:59:08 +02:00
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Println(err)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.Init)
|
2016-09-20 19:59:08 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-25 15:05:09 +02:00
|
|
|
mountArgs := ""
|
|
|
|
fsName := "gocryptfs"
|
|
|
|
if args.reverse {
|
|
|
|
mountArgs = " -reverse"
|
|
|
|
fsName = "gocryptfs-reverse"
|
|
|
|
}
|
|
|
|
tlog.Info.Printf(tlog.ColorGreen+"The %s filesystem has been created successfully."+tlog.ColorReset,
|
|
|
|
fsName)
|
2016-09-20 19:59:08 +02:00
|
|
|
wd, _ := os.Getwd()
|
|
|
|
friendlyPath, _ := filepath.Rel(wd, args.cipherdir)
|
|
|
|
if strings.HasPrefix(friendlyPath, "../") {
|
|
|
|
// A relative path that starts with "../" is pretty unfriendly, just
|
|
|
|
// keep the absolute path.
|
|
|
|
friendlyPath = args.cipherdir
|
|
|
|
}
|
2016-10-09 18:27:03 +02:00
|
|
|
if strings.Contains(friendlyPath, " ") {
|
|
|
|
friendlyPath = "\"" + friendlyPath + "\""
|
|
|
|
}
|
2016-09-25 15:05:09 +02:00
|
|
|
tlog.Info.Printf(tlog.ColorGrey+"You can now mount it using: %s%s %s MOUNTPOINT"+tlog.ColorReset,
|
|
|
|
tlog.ProgramName, mountArgs, friendlyPath)
|
2016-09-20 19:59:08 +02:00
|
|
|
}
|