2015-09-03 19:27:07 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-05 11:49:05 +02:00
|
|
|
"fmt"
|
2018-06-25 22:45:44 +02:00
|
|
|
"log"
|
2015-09-06 12:12:14 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-09-16 19:35:40 +02:00
|
|
|
"runtime"
|
2016-05-11 23:36:57 +02:00
|
|
|
"strconv"
|
2017-05-30 23:01:06 +02:00
|
|
|
"strings"
|
2015-09-08 00:54:24 +02:00
|
|
|
|
2017-06-01 20:53:03 +02:00
|
|
|
"github.com/hanwen/go-fuse/fuse"
|
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/configfile"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/contentenc"
|
2017-05-07 22:15:01 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/exitcodes"
|
2016-06-15 22:43:31 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/readpassword"
|
2017-02-22 23:55:43 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/speed"
|
2016-10-04 09:51:14 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/stupidgcm"
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2015-09-03 19:27:07 +02:00
|
|
|
)
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// GitVersion is the gocryptfs version according to git, set by build.bash
|
2017-05-06 14:29:34 +02:00
|
|
|
var GitVersion = "[GitVersion not set - please compile using ./build.bash]"
|
2016-07-03 16:49:42 +02:00
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// GitVersionFuse is the go-fuse library version, set by build.bash
|
2017-05-06 14:29:34 +02:00
|
|
|
var GitVersionFuse = "[GitVersionFuse not set - please compile using ./build.bash]"
|
2016-07-03 16:49:42 +02:00
|
|
|
|
2017-09-06 21:41:22 +02:00
|
|
|
// BuildDate is a date string like "2017-09-06", set by build.bash
|
|
|
|
var BuildDate = "0000-00-00"
|
2015-11-01 13:55:35 +01:00
|
|
|
|
2017-05-06 14:26:34 +02:00
|
|
|
// raceDetector is set to true by race.go if we are compiled with "go build -race"
|
|
|
|
var raceDetector bool
|
|
|
|
|
2016-10-08 18:27:31 +02:00
|
|
|
// loadConfig loads the config file "args.config", prompting the user for the password
|
2017-01-26 21:32:08 +01:00
|
|
|
func loadConfig(args *argContainer) (masterkey []byte, confFile *configfile.ConfFile, err error) {
|
2018-06-17 15:25:09 +02:00
|
|
|
// First check if the file can be read at all, and find out if a Trezor should
|
|
|
|
// be used instead of a password.
|
2018-06-25 22:02:05 +02:00
|
|
|
_, cf1, err := configfile.Load(args.config, nil)
|
2015-11-14 21:25:10 +01:00
|
|
|
if err != nil {
|
2016-10-08 17:19:06 +02:00
|
|
|
tlog.Fatal.Printf("Cannot open config file: %v", err)
|
2018-06-17 15:25:09 +02:00
|
|
|
return nil, nil, err
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
2018-06-17 15:25:09 +02:00
|
|
|
// The user has passed the master key on the command line (probably because
|
|
|
|
// he forgot the password).
|
2016-10-16 18:17:28 +02:00
|
|
|
if args.masterkey != "" {
|
2018-03-22 00:02:10 +01:00
|
|
|
masterkey = parseMasterKey(args.masterkey, false)
|
2018-06-17 15:25:09 +02:00
|
|
|
return masterkey, cf1, nil
|
|
|
|
}
|
|
|
|
var pw []byte
|
|
|
|
if cf1.IsFeatureFlagSet(configfile.FlagTrezor) {
|
|
|
|
// Get binary data from from Trezor
|
2018-06-25 22:27:15 +02:00
|
|
|
pw = readpassword.Trezor(cf1.TrezorPayload)
|
2016-10-16 18:17:28 +02:00
|
|
|
} else {
|
2018-06-17 15:25:09 +02:00
|
|
|
// Normal password entry
|
|
|
|
pw = readpassword.Once(args.extpass, "")
|
2016-10-16 18:17:28 +02:00
|
|
|
}
|
2018-06-17 15:25:09 +02:00
|
|
|
tlog.Info.Println("Decrypting master key")
|
2018-06-25 22:02:05 +02:00
|
|
|
masterkey, confFile, err = configfile.Load(args.config, pw)
|
2018-06-17 15:25:09 +02:00
|
|
|
for i := range pw {
|
|
|
|
pw[i] = 0
|
|
|
|
}
|
|
|
|
|
2016-06-19 20:01:04 +02:00
|
|
|
if err != nil {
|
2016-06-16 23:23:09 +02:00
|
|
|
tlog.Fatal.Println(err)
|
2017-01-26 21:32:08 +01:00
|
|
|
return nil, nil, err
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
2017-01-26 21:32:08 +01:00
|
|
|
return masterkey, confFile, nil
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// changePassword - change the password of config file "filename"
|
2018-02-18 15:33:35 +01:00
|
|
|
// Does not return (calls os.Exit both on success and on error).
|
2015-11-15 13:38:19 +01:00
|
|
|
func changePassword(args *argContainer) {
|
2018-06-25 22:38:27 +02:00
|
|
|
// Parse the config file, but do not unlock the master key. We only want to
|
|
|
|
// know if the Trezor flag is set.
|
|
|
|
_, cf1, err := configfile.Load(args.config, nil)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Printf("Cannot open config file: %v", err)
|
|
|
|
os.Exit(exitcodes.LoadConf)
|
|
|
|
}
|
|
|
|
if cf1.IsFeatureFlagSet(configfile.FlagTrezor) {
|
|
|
|
tlog.Fatal.Printf("Password change is not supported on Trezor-enabled filesystems.")
|
|
|
|
os.Exit(exitcodes.Usage)
|
|
|
|
}
|
2018-02-18 15:33:35 +01:00
|
|
|
var confFile *configfile.ConfFile
|
2018-02-18 15:22:22 +01:00
|
|
|
{
|
2018-02-18 15:33:35 +01:00
|
|
|
var masterkey []byte
|
|
|
|
masterkey, confFile, err = loadConfig(args)
|
|
|
|
if err != nil {
|
|
|
|
exitcodes.Exit(err)
|
|
|
|
}
|
2018-06-17 15:25:09 +02:00
|
|
|
if len(masterkey) == 0 {
|
2018-06-25 22:45:44 +02:00
|
|
|
log.Panic("empty masterkey")
|
2018-06-17 15:25:09 +02:00
|
|
|
}
|
2018-02-18 15:33:35 +01:00
|
|
|
tlog.Info.Println("Please enter your new password.")
|
2018-02-18 15:22:22 +01:00
|
|
|
newPw := readpassword.Twice(args.extpass)
|
|
|
|
readpassword.CheckTrailingGarbage()
|
|
|
|
confFile.EncryptKey(masterkey, newPw, confFile.ScryptObject.LogN())
|
|
|
|
for i := range newPw {
|
|
|
|
newPw[i] = 0
|
|
|
|
}
|
2018-02-18 15:33:35 +01:00
|
|
|
for i := range masterkey {
|
|
|
|
masterkey[i] = 0
|
|
|
|
}
|
|
|
|
// masterkey and newPw run out of scope here
|
2018-02-18 15:22:22 +01:00
|
|
|
}
|
2018-02-18 12:41:11 +01:00
|
|
|
// Are we resetting the password without knowing the old one using
|
|
|
|
// "-masterkey"?
|
2016-10-16 18:17:28 +02:00
|
|
|
if args.masterkey != "" {
|
|
|
|
bak := args.config + ".bak"
|
2017-01-26 21:32:08 +01:00
|
|
|
err = os.Link(args.config, bak)
|
2016-10-16 18:17:28 +02:00
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Printf("Could not create backup file: %v", err)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.Init)
|
2016-10-16 18:17:28 +02:00
|
|
|
}
|
|
|
|
tlog.Info.Printf(tlog.ColorGrey+
|
|
|
|
"A copy of the old config file has been created at %q.\n"+
|
|
|
|
"Delete it after you have verified that you can access your files with the new password."+
|
|
|
|
tlog.ColorReset, bak)
|
|
|
|
}
|
2017-01-26 21:32:08 +01:00
|
|
|
err = confFile.WriteFile()
|
2015-11-14 21:25:10 +01:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Println(err)
|
2017-05-14 14:30:50 +02:00
|
|
|
os.Exit(exitcodes.WriteConf)
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
2016-10-16 18:17:28 +02:00
|
|
|
tlog.Info.Printf(tlog.ColorGreen + "Password changed." + tlog.ColorReset)
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 16:49:42 +02:00
|
|
|
// printVersion prints a version string like this:
|
|
|
|
// gocryptfs v0.12-36-ge021b9d-dirty; go-fuse a4c968c; 2016-07-03 go1.6.2
|
2015-11-14 21:25:10 +01:00
|
|
|
func printVersion() {
|
2016-10-04 09:51:14 +02:00
|
|
|
buildFlags := ""
|
|
|
|
if stupidgcm.BuiltWithoutOpenssl {
|
|
|
|
buildFlags = " without_openssl"
|
|
|
|
}
|
2017-09-06 21:41:22 +02:00
|
|
|
built := fmt.Sprintf("%s %s", BuildDate, runtime.Version())
|
2017-05-06 14:26:34 +02:00
|
|
|
if raceDetector {
|
|
|
|
built += " -race"
|
|
|
|
}
|
2016-10-04 09:51:14 +02:00
|
|
|
fmt.Printf("%s %s%s; go-fuse %s; %s\n",
|
|
|
|
tlog.ProgramName, GitVersion, buildFlags, GitVersionFuse, built)
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
|
|
|
|
2015-11-09 23:11:38 +01:00
|
|
|
func main() {
|
2017-06-01 20:53:03 +02:00
|
|
|
mxp := runtime.GOMAXPROCS(0)
|
|
|
|
if mxp < 4 {
|
|
|
|
// On a 2-core machine, setting maxprocs to 4 gives 10% better performance
|
|
|
|
runtime.GOMAXPROCS(4)
|
|
|
|
}
|
2018-05-06 18:08:30 +02:00
|
|
|
// mount(1) unsets PATH. Since exec.Command does not handle this case, we set
|
|
|
|
// PATH to a default value if it's empty or unset.
|
|
|
|
if os.Getenv("PATH") == "" {
|
|
|
|
os.Setenv("PATH", "/usr/sbin:/usr/bin:/sbin:/bin")
|
|
|
|
}
|
2015-11-14 21:25:10 +01:00
|
|
|
var err error
|
2016-09-20 19:59:08 +02:00
|
|
|
// Parse all command-line options (i.e. arguments starting with "-")
|
|
|
|
// into "args". Path arguments are parsed below.
|
|
|
|
args := parseCliOpts()
|
2016-11-01 19:04:49 +01:00
|
|
|
// Fork a child into the background if "-fg" is not set AND we are mounting
|
2016-09-20 19:49:44 +02:00
|
|
|
// a filesystem. The child will do all the work.
|
2016-11-01 18:59:34 +01:00
|
|
|
if !args.fg && flagSet.NArg() == 2 {
|
2016-09-20 19:49:44 +02:00
|
|
|
ret := forkChild()
|
|
|
|
os.Exit(ret)
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
2016-05-11 23:36:57 +02:00
|
|
|
if args.debug {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Enabled = true
|
2016-05-11 23:36:57 +02:00
|
|
|
}
|
2015-11-14 21:25:10 +01:00
|
|
|
// "-v"
|
2015-11-02 23:08:51 +01:00
|
|
|
if args.version {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("openssl=%v\n", args.openssl)
|
2016-06-19 19:33:15 +02:00
|
|
|
tlog.Debug.Printf("on-disk format %d\n", contentenc.CurrentVersion)
|
2015-11-14 21:25:10 +01:00
|
|
|
printVersion()
|
2015-11-01 13:55:35 +01:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2017-05-30 17:59:13 +02:00
|
|
|
// "-hh"
|
|
|
|
if args.hh {
|
|
|
|
helpLong()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2017-02-22 23:55:43 +01:00
|
|
|
// "-speed"
|
|
|
|
if args.speed {
|
|
|
|
speed.Run()
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2016-01-31 18:09:39 +01:00
|
|
|
if args.wpanic {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Wpanic = true
|
2018-02-04 20:38:22 +01:00
|
|
|
tlog.Debug.Printf("Panicking on warnings")
|
2016-01-31 18:09:39 +01:00
|
|
|
}
|
2017-11-15 20:30:21 +01:00
|
|
|
// Every operation below requires CIPHERDIR. Exit if we don't have it.
|
|
|
|
if flagSet.NArg() == 0 {
|
|
|
|
if flagSet.NFlag() == 0 {
|
|
|
|
// Naked call to "gocryptfs". Just print the help text.
|
|
|
|
helpShort()
|
|
|
|
} else {
|
|
|
|
// The user has passed some flags, but CIPHERDIR is missing. State
|
|
|
|
// what is wrong.
|
|
|
|
tlog.Fatal.Printf("CIPHERDIR argument is missing")
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.Usage)
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
2017-11-15 20:30:21 +01:00
|
|
|
// Check that CIPHERDIR exists
|
|
|
|
args.cipherdir, _ = filepath.Abs(flagSet.Arg(0))
|
2018-04-01 12:31:44 +02:00
|
|
|
err = isDir(args.cipherdir)
|
2017-11-15 20:30:21 +01:00
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Printf("Invalid cipherdir: %v", err)
|
|
|
|
os.Exit(exitcodes.CipherDir)
|
|
|
|
}
|
2015-11-14 21:25:10 +01:00
|
|
|
// "-q"
|
2015-11-09 22:33:42 +01:00
|
|
|
if args.quiet {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Info.Enabled = false
|
2015-11-09 22:33:42 +01:00
|
|
|
}
|
2016-09-26 23:25:13 +02:00
|
|
|
// "-reverse" implies "-aessiv"
|
2016-09-25 15:05:09 +02:00
|
|
|
if args.reverse {
|
2016-09-26 23:25:13 +02:00
|
|
|
args.aessiv = true
|
2016-09-25 15:05:09 +02:00
|
|
|
}
|
2015-11-14 21:25:10 +01:00
|
|
|
// "-config"
|
|
|
|
if args.config != "" {
|
|
|
|
args.config, err = filepath.Abs(args.config)
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Printf("Invalid \"-config\" setting: %v", err)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.Init)
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Info.Printf("Using config file at custom location %s", args.config)
|
2016-10-08 20:57:38 +02:00
|
|
|
args._configCustom = true
|
2016-08-29 22:05:39 +02:00
|
|
|
} else if args.reverse {
|
|
|
|
args.config = filepath.Join(args.cipherdir, configfile.ConfReverseName)
|
2015-11-14 21:25:10 +01:00
|
|
|
} else {
|
2016-02-06 19:20:54 +01:00
|
|
|
args.config = filepath.Join(args.cipherdir, configfile.ConfDefaultName)
|
2015-10-11 18:02:48 +02:00
|
|
|
}
|
2017-05-30 23:01:06 +02:00
|
|
|
// "-force_owner"
|
|
|
|
if args.force_owner != "" {
|
|
|
|
var uidNum, gidNum int64
|
|
|
|
ownerPieces := strings.SplitN(args.force_owner, ":", 2)
|
|
|
|
if len(ownerPieces) != 2 {
|
|
|
|
tlog.Fatal.Printf("force_owner must be in form UID:GID")
|
|
|
|
os.Exit(exitcodes.Usage)
|
|
|
|
}
|
|
|
|
uidNum, err = strconv.ParseInt(ownerPieces[0], 0, 32)
|
|
|
|
if err != nil || uidNum < 0 {
|
|
|
|
tlog.Fatal.Printf("force_owner: Unable to parse UID %v as positive integer", ownerPieces[0])
|
|
|
|
os.Exit(exitcodes.Usage)
|
|
|
|
}
|
|
|
|
gidNum, err = strconv.ParseInt(ownerPieces[1], 0, 32)
|
|
|
|
if err != nil || gidNum < 0 {
|
|
|
|
tlog.Fatal.Printf("force_owner: Unable to parse GID %v as positive integer", ownerPieces[1])
|
|
|
|
os.Exit(exitcodes.Usage)
|
|
|
|
}
|
|
|
|
args._forceOwner = &fuse.Owner{Uid: uint32(uidNum), Gid: uint32(gidNum)}
|
|
|
|
}
|
2017-06-05 22:03:15 +02:00
|
|
|
// "-cpuprofile"
|
|
|
|
if args.cpuprofile != "" {
|
2017-07-30 13:26:56 +02:00
|
|
|
onExitFunc := setupCpuprofile(args.cpuprofile)
|
|
|
|
defer onExitFunc()
|
2017-06-05 22:03:15 +02:00
|
|
|
}
|
2016-01-21 23:55:37 +01:00
|
|
|
// "-memprofile"
|
|
|
|
if args.memprofile != "" {
|
2017-07-30 13:26:56 +02:00
|
|
|
onExitFunc := setupMemprofile(args.memprofile)
|
|
|
|
defer onExitFunc()
|
2016-01-21 23:55:37 +01:00
|
|
|
}
|
2017-06-05 22:45:11 +02:00
|
|
|
// "-trace"
|
|
|
|
if args.trace != "" {
|
2017-07-30 13:26:56 +02:00
|
|
|
onExitFunc := setupTrace(args.trace)
|
|
|
|
defer onExitFunc()
|
2017-06-05 22:45:11 +02:00
|
|
|
}
|
|
|
|
if args.cpuprofile != "" || args.memprofile != "" || args.trace != "" {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Info.Printf("Note: You must unmount gracefully, otherwise the profile file(s) will stay empty!\n")
|
2016-01-21 23:55:37 +01:00
|
|
|
}
|
2015-11-14 21:25:10 +01:00
|
|
|
// "-openssl"
|
2016-09-25 19:48:21 +02:00
|
|
|
if !args.openssl {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("OpenSSL disabled, using Go GCM")
|
2016-05-11 23:36:57 +02:00
|
|
|
} else {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Debug.Printf("OpenSSL enabled")
|
2015-10-06 20:24:52 +02:00
|
|
|
}
|
2017-05-30 19:01:32 +02:00
|
|
|
// Operation flags
|
2018-04-01 14:25:10 +02:00
|
|
|
nOps := countOpFlags(&args)
|
|
|
|
if nOps == 0 {
|
|
|
|
// Default operation: mount.
|
|
|
|
if flagSet.NArg() != 2 {
|
|
|
|
prettyArgs := prettyArgs()
|
|
|
|
tlog.Info.Printf("Wrong number of arguments (have %d, want 2). You passed: %s",
|
|
|
|
flagSet.NArg(), prettyArgs)
|
|
|
|
tlog.Fatal.Printf("Usage: %s [OPTIONS] CIPHERDIR MOUNTPOINT [-o COMMA-SEPARATED-OPTIONS]", tlog.ProgramName)
|
|
|
|
os.Exit(exitcodes.Usage)
|
|
|
|
}
|
|
|
|
doMount(&args)
|
|
|
|
// Don't call os.Exit to give deferred functions a chance to run
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if nOps > 1 {
|
|
|
|
tlog.Fatal.Printf("At most one of -info, -init, -passwd, -fsck is allowed")
|
|
|
|
os.Exit(exitcodes.Usage)
|
|
|
|
}
|
|
|
|
if flagSet.NArg() != 1 {
|
|
|
|
tlog.Fatal.Printf("The options -info, -init, -passwd, -fsck take exactly one argument, %d given",
|
|
|
|
flagSet.NArg())
|
2017-05-30 19:01:32 +02:00
|
|
|
os.Exit(exitcodes.Usage)
|
|
|
|
}
|
|
|
|
// "-info"
|
|
|
|
if args.info {
|
2018-04-01 12:12:47 +02:00
|
|
|
info(args.config)
|
|
|
|
os.Exit(0)
|
2017-05-30 19:01:32 +02:00
|
|
|
}
|
2015-11-14 21:25:10 +01:00
|
|
|
// "-init"
|
2015-11-02 23:08:51 +01:00
|
|
|
if args.init {
|
2018-04-01 12:12:47 +02:00
|
|
|
initDir(&args)
|
|
|
|
os.Exit(0)
|
2015-10-11 18:33:28 +02:00
|
|
|
}
|
2015-11-14 21:25:10 +01:00
|
|
|
// "-passwd"
|
2015-11-02 23:08:51 +01:00
|
|
|
if args.passwd {
|
2018-04-01 12:12:47 +02:00
|
|
|
changePassword(&args)
|
|
|
|
os.Exit(0)
|
2015-09-03 19:27:07 +02:00
|
|
|
}
|
2018-04-01 14:25:10 +02:00
|
|
|
// "-fsck"
|
|
|
|
if args.fsck {
|
|
|
|
fsck(&args)
|
2018-04-01 21:23:32 +02:00
|
|
|
os.Exit(0)
|
2016-10-09 18:17:04 +02:00
|
|
|
}
|
2015-11-01 13:28:58 +01:00
|
|
|
}
|