Skip to content

Commit 6d64dfe

Browse files
committed
Only print masterkey once on -init
It is no longer printed at all when mounting a filesystem, printing on -init can be disabled with -q. #76
1 parent 991891a commit 6d64dfe

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

internal/configfile/config_file.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func Create(filename string, password []byte, plaintextNames bool,
104104
} else {
105105
key = cryptocore.RandBytes(cryptocore.KeyLen)
106106
}
107+
tlog.PrintMasterkeyReminder(key)
107108
// Encrypt it using the password
108109
// This sets ScryptObject and EncryptedKey
109110
// Note: this looks at the FeatureFlags, so call it AFTER setting them.

internal/tlog/log.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package tlog
44

55
import (
6+
"encoding/hex"
67
"encoding/json"
78
"fmt"
89
"log"
@@ -144,3 +145,39 @@ func SwitchLoggerToSyslog(p syslog.Priority) {
144145
log.SetOutput(w)
145146
}
146147
}
148+
149+
// PrintMasterkeyReminder reminds the user that he should store the master key in
150+
// a safe place.
151+
func PrintMasterkeyReminder(key []byte) {
152+
if !Info.Enabled {
153+
// Quiet mode
154+
return
155+
}
156+
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
157+
// We don't want the master key to end up in a log file
158+
Info.Printf("Not running on a terminal, suppressing master key display\n")
159+
return
160+
}
161+
h := hex.EncodeToString(key)
162+
var hChunked string
163+
// Try to make it less scary by splitting it up in chunks
164+
for i := 0; i < len(h); i += 8 {
165+
hChunked += h[i : i+8]
166+
if i < 52 {
167+
hChunked += "-"
168+
}
169+
if i == 24 {
170+
hChunked += "\n "
171+
}
172+
}
173+
Info.Printf(`
174+
Your master key is:
175+
176+
%s
177+
178+
If the gocryptfs.conf file becomes corrupted or you ever forget your password,
179+
there is only one hope for recovery: The master key. Print it to a piece of
180+
paper and store it in a drawer. This message is only printed once.
181+
182+
`, ColorGrey+hChunked+ColorReset)
183+
}

masterkey.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,13 @@ import (
55
"os"
66
"strings"
77

8-
"golang.org/x/crypto/ssh/terminal"
9-
108
"github.com/rfjakob/gocryptfs/internal/configfile"
119
"github.com/rfjakob/gocryptfs/internal/cryptocore"
1210
"github.com/rfjakob/gocryptfs/internal/exitcodes"
1311
"github.com/rfjakob/gocryptfs/internal/readpassword"
1412
"github.com/rfjakob/gocryptfs/internal/tlog"
1513
)
1614

17-
// printMasterKey - remind the user that he should store the master key in
18-
// a safe place
19-
func printMasterKey(key []byte) {
20-
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
21-
// We don't want the master key to end up in a log file
22-
tlog.Info.Printf("Not running on a terminal, suppressing master key display\n")
23-
return
24-
}
25-
h := hex.EncodeToString(key)
26-
var hChunked string
27-
// Try to make it less scary by splitting it up in chunks
28-
for i := 0; i < len(h); i += 8 {
29-
hChunked += h[i : i+8]
30-
if i < 52 {
31-
hChunked += "-"
32-
}
33-
if i == 24 {
34-
hChunked += "\n "
35-
}
36-
}
37-
tlog.Info.Printf(`
38-
Your master key is:
39-
40-
%s
41-
42-
If the gocryptfs.conf file becomes corrupted or you ever forget your password,
43-
there is only one hope for recovery: The master key. Print it to a piece of
44-
paper and store it in a drawer. Use "-q" to suppress this message.
45-
46-
`, tlog.ColorGrey+hChunked+tlog.ColorReset)
47-
}
48-
4915
// parseMasterKey - Parse a hex-encoded master key that was passed on the command line
5016
// Calls os.Exit on failure
5117
func parseMasterKey(masterkey string, fromStdin bool) []byte {
@@ -106,9 +72,5 @@ func getMasterKey(args *argContainer) (masterkey []byte, confFile *configfile.Co
10672
if !args.trezor {
10773
readpassword.CheckTrailingGarbage()
10874
}
109-
if !args.fsck {
110-
// We only want to print the masterkey message on a normal mount.
111-
printMasterKey(masterkey)
112-
}
11375
return masterkey, confFile
11476
}

0 commit comments

Comments
 (0)