2015-09-03 19:27:07 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-05 11:49:05 +02:00
|
|
|
"fmt"
|
2015-09-06 12:12:14 +02:00
|
|
|
"os"
|
2016-10-08 18:27:31 +02:00
|
|
|
|
2015-09-06 12:12:14 +02:00
|
|
|
"path/filepath"
|
2015-09-16 19:35:40 +02:00
|
|
|
"runtime"
|
2015-10-04 14:36:20 +02:00
|
|
|
"runtime/pprof"
|
2016-05-11 23:36:57 +02:00
|
|
|
"strconv"
|
2015-10-04 14:36:20 +02:00
|
|
|
"time"
|
2015-09-08 00:54:24 +02:00
|
|
|
|
2016-02-06 19:20:54 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/configfile"
|
|
|
|
"github.com/rfjakob/gocryptfs/internal/contentenc"
|
2016-06-15 22:43:31 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/readpassword"
|
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
|
|
|
// Exit codes
|
2015-09-05 11:49:05 +02:00
|
|
|
const (
|
2016-10-02 06:14:18 +02:00
|
|
|
ErrExitUsage = 1
|
|
|
|
ErrExitMount = 3
|
|
|
|
ErrExitCipherDir = 6
|
|
|
|
ErrExitInit = 7
|
|
|
|
ErrExitLoadConf = 8
|
|
|
|
ErrExitMountPoint = 10
|
2015-09-05 11:49:05 +02:00
|
|
|
)
|
|
|
|
|
2016-07-03 16:49:42 +02:00
|
|
|
const pleaseBuildBash = "[not set - please compile using ./build.bash]"
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// GitVersion is the gocryptfs version according to git, set by build.bash
|
2016-07-03 16:49:42 +02:00
|
|
|
var GitVersion = pleaseBuildBash
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// GitVersionFuse is the go-fuse library version, set by build.bash
|
2016-07-03 16:49:42 +02:00
|
|
|
var GitVersionFuse = pleaseBuildBash
|
|
|
|
|
2016-10-02 06:14:18 +02:00
|
|
|
// BuildTime is the Unix timestamp, set by build.bash
|
2016-07-03 16:49:42 +02:00
|
|
|
var BuildTime = "0"
|
2015-11-01 13:55:35 +01:00
|
|
|
|
2015-10-07 21:59:54 +02:00
|
|
|
func usageText() {
|
2015-11-14 21:25:10 +01:00
|
|
|
printVersion()
|
2016-06-05 14:26:16 +02:00
|
|
|
fmt.Printf(`
|
|
|
|
Usage: %s -init|-passwd [OPTIONS] CIPHERDIR
|
2016-10-09 19:38:49 +02:00
|
|
|
or %s [OPTIONS] CIPHERDIR MOUNTPOINT [-o COMMA-SEPARATED-OPTIONS]
|
2016-06-05 14:26:16 +02:00
|
|
|
|
|
|
|
Options:
|
2016-06-15 23:30:44 +02:00
|
|
|
`, tlog.ProgramName, tlog.ProgramName)
|
2016-06-05 14:26:16 +02:00
|
|
|
|
2015-11-09 23:11:38 +01:00
|
|
|
flagSet.PrintDefaults()
|
2015-10-07 21:59:54 +02:00
|
|
|
}
|
|
|
|
|
2016-10-08 18:27:31 +02:00
|
|
|
// loadConfig loads the config file "args.config", prompting the user for the password
|
2016-02-06 19:20:54 +01:00
|
|
|
func loadConfig(args *argContainer) (masterkey []byte, confFile *configfile.ConfFile) {
|
2016-10-08 17:19:06 +02:00
|
|
|
// Check if the file can be opened at all before prompting for a password
|
|
|
|
fd, err := os.Open(args.config)
|
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)
|
2016-10-02 06:14:18 +02:00
|
|
|
os.Exit(ErrExitLoadConf)
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
2016-10-08 17:19:06 +02:00
|
|
|
fd.Close()
|
2016-10-16 18:17:28 +02:00
|
|
|
if args.masterkey != "" {
|
|
|
|
masterkey = parseMasterKey(args.masterkey)
|
|
|
|
_, confFile, err = configfile.LoadConfFile(args.config, "")
|
|
|
|
} else {
|
|
|
|
pw := readpassword.Once(args.extpass)
|
|
|
|
tlog.Info.Println("Decrypting master key")
|
|
|
|
masterkey, confFile, err = configfile.LoadConfFile(args.config, pw)
|
|
|
|
}
|
2016-06-19 20:01:04 +02:00
|
|
|
if err != nil {
|
2016-06-16 23:23:09 +02:00
|
|
|
tlog.Fatal.Println(err)
|
2016-10-02 06:14:18 +02:00
|
|
|
os.Exit(ErrExitLoadConf)
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
|
|
|
return masterkey, confFile
|
|
|
|
}
|
|
|
|
|
|
|
|
// changePassword - change the password of config file "filename"
|
2015-11-15 13:38:19 +01:00
|
|
|
func changePassword(args *argContainer) {
|
|
|
|
masterkey, confFile := loadConfig(args)
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Info.Println("Please enter your new password.")
|
2016-06-15 22:43:31 +02:00
|
|
|
newPw := readpassword.Twice(args.extpass)
|
2015-11-29 18:52:58 +01:00
|
|
|
confFile.EncryptKey(masterkey, newPw, confFile.ScryptObject.LogN())
|
2016-10-16 18:17:28 +02:00
|
|
|
if args.masterkey != "" {
|
|
|
|
bak := args.config + ".bak"
|
|
|
|
err := os.Link(args.config, bak)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Printf("Could not create backup file: %v", err)
|
|
|
|
os.Exit(ErrExitInit)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2015-11-14 21:25:10 +01:00
|
|
|
err := confFile.WriteFile()
|
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Println(err)
|
2016-10-02 06:14:18 +02:00
|
|
|
os.Exit(ErrExitInit)
|
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
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
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-07-03 16:49:42 +02:00
|
|
|
humanTime := "0000-00-00"
|
|
|
|
if i, _ := strconv.ParseInt(BuildTime, 10, 64); i > 0 {
|
|
|
|
t := time.Unix(i, 0).UTC()
|
|
|
|
humanTime = fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day())
|
|
|
|
}
|
2016-10-04 09:51:14 +02:00
|
|
|
buildFlags := ""
|
|
|
|
if stupidgcm.BuiltWithoutOpenssl {
|
|
|
|
buildFlags = " without_openssl"
|
|
|
|
}
|
2016-07-03 16:49:42 +02:00
|
|
|
built := fmt.Sprintf("%s %s", humanTime, runtime.Version())
|
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() {
|
|
|
|
runtime.GOMAXPROCS(4)
|
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)
|
|
|
|
}
|
2016-01-31 18:09:39 +01:00
|
|
|
if args.wpanic {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Warn.Wpanic = true
|
|
|
|
tlog.Debug.Printf("Panicing on warnings")
|
2016-01-31 18:09:39 +01:00
|
|
|
}
|
2015-11-14 21:25:10 +01:00
|
|
|
// Every operation below requires CIPHERDIR. Check that we have it.
|
|
|
|
if flagSet.NArg() >= 1 {
|
|
|
|
args.cipherdir, _ = filepath.Abs(flagSet.Arg(0))
|
2016-06-14 22:45:33 +02:00
|
|
|
err = checkDir(args.cipherdir)
|
2015-11-14 21:25:10 +01:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Printf("Invalid cipherdir: %v", err)
|
2016-10-02 06:14:18 +02:00
|
|
|
os.Exit(ErrExitCipherDir)
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
usageText()
|
2016-10-02 06:14:18 +02:00
|
|
|
os.Exit(ErrExitUsage)
|
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)
|
2016-10-02 06:14:18 +02:00
|
|
|
os.Exit(ErrExitInit)
|
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
|
|
|
}
|
2015-11-14 21:25:10 +01:00
|
|
|
// "-cpuprofile"
|
2015-11-14 17:16:17 +01:00
|
|
|
if args.cpuprofile != "" {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Info.Printf("Writing CPU profile to %s", args.cpuprofile)
|
2016-04-10 21:29:42 +02:00
|
|
|
var f *os.File
|
|
|
|
f, err = os.Create(args.cpuprofile)
|
2015-10-04 14:36:20 +02:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Println(err)
|
2016-10-02 06:14:18 +02:00
|
|
|
os.Exit(ErrExitInit)
|
2015-10-04 14:36:20 +02:00
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
2016-01-21 23:55:37 +01:00
|
|
|
// "-memprofile"
|
|
|
|
if args.memprofile != "" {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Info.Printf("Writing mem profile to %s", args.memprofile)
|
2016-04-10 21:29:42 +02:00
|
|
|
var f *os.File
|
|
|
|
f, err = os.Create(args.memprofile)
|
2016-01-21 23:55:37 +01:00
|
|
|
if err != nil {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Println(err)
|
2016-10-02 06:14:18 +02:00
|
|
|
os.Exit(ErrExitInit)
|
2016-01-21 23:55:37 +01:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
pprof.WriteHeapProfile(f)
|
|
|
|
f.Close()
|
|
|
|
return
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if args.cpuprofile != "" || args.memprofile != "" {
|
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
|
|
|
}
|
2016-09-25 15:05:09 +02:00
|
|
|
// Operation flags: -init or -passwd; otherwise: mount
|
2015-11-14 21:25:10 +01:00
|
|
|
// "-init"
|
2015-11-02 23:08:51 +01:00
|
|
|
if args.init {
|
2015-11-14 21:25:10 +01:00
|
|
|
if flagSet.NArg() > 1 {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Printf("Usage: %s -init [OPTIONS] CIPHERDIR", tlog.ProgramName)
|
2016-10-02 06:14:18 +02:00
|
|
|
os.Exit(ErrExitUsage)
|
2015-09-13 17:55:07 +02:00
|
|
|
}
|
2015-11-14 21:25:10 +01:00
|
|
|
initDir(&args) // does not return
|
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 {
|
2015-11-14 21:25:10 +01:00
|
|
|
if flagSet.NArg() > 1 {
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Fatal.Printf("Usage: %s -passwd [OPTIONS] CIPHERDIR", tlog.ProgramName)
|
2016-10-02 06:14:18 +02:00
|
|
|
os.Exit(ErrExitUsage)
|
2015-10-11 18:33:28 +02:00
|
|
|
}
|
2015-11-15 13:38:19 +01:00
|
|
|
changePassword(&args) // does not return
|
2015-09-03 19:27:07 +02:00
|
|
|
}
|
2016-10-09 18:17:04 +02:00
|
|
|
// Default operation: mount.
|
|
|
|
if flagSet.NArg() != 2 {
|
2016-10-09 20:55:33 +02:00
|
|
|
prettyArgs := prettyArgs()
|
2016-10-09 18:17:04 +02:00
|
|
|
tlog.Info.Printf("Wrong number of arguments (have %d, want 2). You passed: %s",
|
|
|
|
flagSet.NArg(), prettyArgs)
|
2016-10-09 19:38:49 +02:00
|
|
|
tlog.Fatal.Printf("Usage: %s [OPTIONS] CIPHERDIR MOUNTPOINT [-o COMMA-SEPARATED-OPTIONS]", tlog.ProgramName)
|
2016-10-09 18:17:04 +02:00
|
|
|
os.Exit(ErrExitUsage)
|
|
|
|
}
|
2016-11-23 23:49:28 +01:00
|
|
|
ret := doMount(&args)
|
|
|
|
if ret != 0 {
|
|
|
|
os.Exit(ret)
|
|
|
|
}
|
|
|
|
// Don't call os.Exit on success to give deferred functions a chance to
|
|
|
|
// run
|
2015-11-01 13:28:58 +01:00
|
|
|
}
|