2015-10-06 21:16:39 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2015-10-07 22:58:22 +02:00
|
|
|
"os"
|
|
|
|
"strings"
|
2016-02-06 19:20:54 +01:00
|
|
|
|
2016-10-11 09:13:51 +02:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
|
2018-04-01 14:51:53 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/configfile"
|
2016-02-06 19:20:54 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/cryptocore"
|
2017-05-07 22:15:01 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/exitcodes"
|
2018-04-01 14:51:53 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/readpassword"
|
2016-06-15 23:30:44 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/tlog"
|
2015-10-06 21:16:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// printMasterKey - remind the user that he should store the master key in
|
|
|
|
// a safe place
|
|
|
|
func printMasterKey(key []byte) {
|
2016-10-11 09:13:51 +02:00
|
|
|
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
|
|
|
|
// We don't want the master key to end up in a log file
|
|
|
|
tlog.Info.Printf("Not running on a terminal, suppressing master key display\n")
|
|
|
|
return
|
|
|
|
}
|
2015-10-06 21:16:39 +02:00
|
|
|
h := hex.EncodeToString(key)
|
|
|
|
var hChunked string
|
|
|
|
// Try to make it less scary by splitting it up in chunks
|
2015-10-07 22:58:22 +02:00
|
|
|
for i := 0; i < len(h); i += 8 {
|
|
|
|
hChunked += h[i : i+8]
|
2015-10-06 21:16:39 +02:00
|
|
|
if i < 52 {
|
|
|
|
hChunked += "-"
|
|
|
|
}
|
|
|
|
if i == 24 {
|
2015-12-19 15:46:19 +01:00
|
|
|
hChunked += "\n "
|
2015-10-06 21:16:39 +02:00
|
|
|
}
|
|
|
|
}
|
2016-06-15 23:30:44 +02:00
|
|
|
tlog.Info.Printf(`
|
2015-12-19 15:46:19 +01:00
|
|
|
Your master key is:
|
2015-10-06 21:16:39 +02:00
|
|
|
|
2015-12-19 15:46:19 +01:00
|
|
|
%s
|
2015-10-06 21:16:39 +02:00
|
|
|
|
|
|
|
If the gocryptfs.conf file becomes corrupted or you ever forget your password,
|
|
|
|
there is only one hope for recovery: The master key. Print it to a piece of
|
2017-02-12 12:23:16 +01:00
|
|
|
paper and store it in a drawer. Use "-q" to suppress this message.
|
2015-10-06 21:16:39 +02:00
|
|
|
|
2016-06-15 23:30:44 +02:00
|
|
|
`, tlog.ColorGrey+hChunked+tlog.ColorReset)
|
2015-10-06 21:16:39 +02:00
|
|
|
}
|
|
|
|
|
2015-11-14 21:25:10 +01:00
|
|
|
// parseMasterKey - Parse a hex-encoded master key that was passed on the command line
|
|
|
|
// Calls os.Exit on failure
|
2018-03-22 00:02:10 +01:00
|
|
|
func parseMasterKey(masterkey string, fromStdin bool) []byte {
|
2015-10-06 21:16:39 +02:00
|
|
|
masterkey = strings.Replace(masterkey, "-", "", -1)
|
|
|
|
key, err := hex.DecodeString(masterkey)
|
|
|
|
if err != nil {
|
2016-10-16 16:19:12 +02:00
|
|
|
tlog.Fatal.Printf("Could not parse master key: %v", err)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.MasterKey)
|
2015-10-06 21:16:39 +02:00
|
|
|
}
|
2016-02-06 19:20:54 +01:00
|
|
|
if len(key) != cryptocore.KeyLen {
|
2016-10-16 16:19:12 +02:00
|
|
|
tlog.Fatal.Printf("Master key has length %d but we require length %d", len(key), cryptocore.KeyLen)
|
2017-05-07 22:15:01 +02:00
|
|
|
os.Exit(exitcodes.MasterKey)
|
2015-10-06 21:16:39 +02:00
|
|
|
}
|
2016-10-16 16:19:12 +02:00
|
|
|
tlog.Info.Printf("Using explicit master key.")
|
2018-03-22 00:02:10 +01:00
|
|
|
if !fromStdin {
|
|
|
|
tlog.Info.Printf(tlog.ColorYellow +
|
|
|
|
"THE MASTER KEY IS VISIBLE VIA \"ps ax\" AND MAY BE STORED IN YOUR SHELL HISTORY!\n" +
|
|
|
|
"ONLY USE THIS MODE FOR EMERGENCIES" + tlog.ColorReset)
|
|
|
|
}
|
2015-10-06 21:16:39 +02:00
|
|
|
return key
|
|
|
|
}
|
2018-04-01 14:51:53 +02:00
|
|
|
|
|
|
|
// getMasterKey looks at "args" to determine where the master key should come
|
|
|
|
// from (-masterkey=a-b-c-d or stdin or from the config file).
|
|
|
|
// If it comes from the config file, the user is prompted for the password
|
|
|
|
// and a ConfFile instance is returned.
|
|
|
|
// Calls os.Exit on failure.
|
|
|
|
func getMasterKey(args *argContainer) (masterkey []byte, confFile *configfile.ConfFile) {
|
|
|
|
masterkeyFromStdin := false
|
|
|
|
// "-masterkey=stdin"
|
|
|
|
if args.masterkey == "stdin" {
|
|
|
|
args.masterkey = string(readpassword.Once("", "Masterkey"))
|
|
|
|
masterkeyFromStdin = true
|
|
|
|
}
|
|
|
|
// "-masterkey=941a6029-3adc6a1c-..."
|
|
|
|
if args.masterkey != "" {
|
|
|
|
return parseMasterKey(args.masterkey, masterkeyFromStdin), nil
|
|
|
|
}
|
|
|
|
// "-zerokey"
|
|
|
|
if args.zerokey {
|
|
|
|
tlog.Info.Printf("Using all-zero dummy master key.")
|
|
|
|
tlog.Info.Printf(tlog.ColorYellow +
|
|
|
|
"ZEROKEY MODE PROVIDES NO SECURITY AT ALL AND SHOULD ONLY BE USED FOR TESTING." +
|
|
|
|
tlog.ColorReset)
|
|
|
|
return make([]byte, cryptocore.KeyLen), nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
// Load master key from config file (normal operation).
|
|
|
|
// Prompts the user for the password.
|
|
|
|
masterkey, confFile, err = loadConfig(args)
|
|
|
|
if err != nil {
|
|
|
|
if args._ctlsockFd != nil {
|
|
|
|
// Close the socket file (which also deletes it)
|
|
|
|
args._ctlsockFd.Close()
|
|
|
|
}
|
|
|
|
exitcodes.Exit(err)
|
|
|
|
}
|
|
|
|
readpassword.CheckTrailingGarbage()
|
|
|
|
if !args.fsck {
|
|
|
|
// We only want to print the masterkey message on a normal mount.
|
|
|
|
printMasterKey(masterkey)
|
|
|
|
}
|
|
|
|
return masterkey, confFile
|
|
|
|
}
|