2016-09-20 19:59:08 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/configfile"
|
2017-05-07 22:15:01 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/exitcodes"
|
2016-09-20 19:59:08 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/nametransform"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/readpassword"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
|
|
|
)
|
|
|
|
|
2016-09-20 20:15:55 +02:00
|
|
|
// initDir prepares a directory for use as a gocryptfs storage directory.
|
|
|
|
// 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
|
|
|
|
// not 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 {
|
2016-09-20 20:15:55 +02:00
|
|
|
err = checkDirEmpty(args.cipherdir)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Printf("Invalid cipherdir: %v", err)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.Init)
|
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
|
2016-09-20 19:59:08 +02:00
|
|
|
if args.extpass == "" {
|
|
|
|
tlog.Info.Printf("Choose a password for protecting your files.")
|
|
|
|
}
|
|
|
|
password := readpassword.Twice(args.extpass)
|
2017-02-12 15:35:50 +01:00
|
|
|
readpassword.CheckTrailingGarbage()
|
2016-09-20 19:59:08 +02:00
|
|
|
creator := tlog.ProgramName + " " + GitVersion
|
2017-11-19 13:30:04 +01:00
|
|
|
err = configfile.CreateConfFile(args.config, password, args.plaintextnames, args.scryptn, creator, args.aessiv, args.devrandom)
|
2016-09-20 19:59:08 +02:00
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Println(err)
|
2017-05-14 14:30:50 +02:00
|
|
|
os.Exit(exitcodes.WriteConf)
|
2016-09-20 19:59:08 +02:00
|
|
|
}
|
2016-09-20 20:15:55 +02:00
|
|
|
// Forward mode with filename encryption enabled needs a gocryptfs.diriv
|
|
|
|
// in the root dir
|
|
|
|
if !args.plaintextnames && !args.reverse {
|
2017-11-29 13:21:28 +01:00
|
|
|
err = nametransform.WriteDirIV(nil, args.cipherdir)
|
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
|
|
|
os.Exit(0)
|
|
|
|
}
|