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"
|
2017-06-05 22:45:11 +02:00
|
|
|
"runtime/trace"
|
2016-05-11 23:36:57 +02:00
|
|
|
"strconv"
|
2017-05-30 23:01:06 +02:00
|
|
|
"strings"
|
2015-10-04 14:36:20 +02:00
|
|
|
"time"
|
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
|
|
|
|
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
|
|
|
|
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) {
|
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)
|
2017-05-14 14:30:50 +02:00
|
|
|
return nil, nil, exitcodes.NewErr(err.Error(), exitcodes.OpenConf)
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
2016-10-08 17:19:06 +02:00
|
|
|
fd.Close()
|
2017-01-26 21:32:08 +01:00
|
|
|
// The user has passed the master key (probably because he forgot the
|
|
|
|
// password).
|
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)
|
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"
|
2015-11-15 13:38:19 +01:00
|
|
|
func changePassword(args *argContainer) {
|
2017-01-26 21:32:08 +01:00
|
|
|
masterkey, confFile, err := loadConfig(args)
|
|
|
|
if err != nil {
|
2017-05-14 13:14:00 +02:00
|
|
|
exitcodes.Exit(err)
|
2017-01-26 21:32:08 +01:00
|
|
|
}
|
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)
|
2017-02-12 15:35:50 +01:00
|
|
|
readpassword.CheckTrailingGarbage()
|
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"
|
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
|
|
|
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())
|
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)
|
|
|
|
}
|
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
|
|
|
|
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)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.CipherDir)
|
2015-11-14 21:25:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
2017-05-30 17:59:13 +02:00
|
|
|
helpShort()
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.Usage)
|
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 != "" {
|
|
|
|
tlog.Info.Printf("Writing CPU profile to %s", args.cpuprofile)
|
|
|
|
var f *os.File
|
|
|
|
f, err = os.Create(args.cpuprofile)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Println(err)
|
2017-06-05 22:45:11 +02:00
|
|
|
os.Exit(exitcodes.Profiler)
|
|
|
|
}
|
|
|
|
err = pprof.StartCPUProfile(f)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Println(err)
|
|
|
|
os.Exit(exitcodes.Profiler)
|
2017-06-05 22:03:15 +02:00
|
|
|
}
|
|
|
|
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)
|
2017-06-05 22:45:11 +02:00
|
|
|
os.Exit(exitcodes.Profiler)
|
2016-01-21 23:55:37 +01:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
pprof.WriteHeapProfile(f)
|
|
|
|
f.Close()
|
|
|
|
return
|
|
|
|
}()
|
|
|
|
}
|
2017-06-05 22:45:11 +02:00
|
|
|
// "-trace"
|
|
|
|
if args.trace != "" {
|
|
|
|
tlog.Info.Printf("Writing execution trace to %s", args.trace)
|
|
|
|
f, err := os.Create(args.trace)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Println(err)
|
|
|
|
os.Exit(exitcodes.Profiler)
|
|
|
|
}
|
|
|
|
err = trace.Start(f)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Fatal.Println(err)
|
|
|
|
os.Exit(exitcodes.Profiler)
|
|
|
|
}
|
|
|
|
defer trace.Stop()
|
|
|
|
}
|
|
|
|
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
|
|
|
|
if args.info && args.init || args.info && args.passwd || args.passwd && args.init {
|
|
|
|
tlog.Fatal.Printf("At most one of -info, -init, -passwd is allowed")
|
|
|
|
os.Exit(exitcodes.Usage)
|
|
|
|
}
|
|
|
|
// "-info"
|
|
|
|
if args.info {
|
|
|
|
if flagSet.NArg() > 1 {
|
|
|
|
tlog.Fatal.Printf("Usage: %s -info CIPHERDIR", tlog.ProgramName)
|
|
|
|
os.Exit(exitcodes.Usage)
|
|
|
|
}
|
|
|
|
info(args.config) // does not return
|
|
|
|
}
|
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)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.Usage)
|
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)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.Usage)
|
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)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.Usage)
|
2016-10-09 18:17:04 +02:00
|
|
|
}
|
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
|
|
|
}
|