Skip to content

Commit

Permalink
Suppress password prompt when using -extpass
Browse files Browse the repository at this point in the history
Also, add color to the error messages.
  • Loading branch information
rfjakob committed Jan 23, 2016
1 parent e799ae6 commit 1030522
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ func loadConfig(args *argContainer) (masterkey []byte, confFile *cryptfs.ConfFil
fmt.Println(err)
os.Exit(ERREXIT_LOADCONF)
}
fmt.Printf("Password: ")
if args.extpass == "" {
fmt.Printf("Password: ")
}
pw := readPassword(args.extpass)
cryptfs.Info.Printf("Decrypting master key... ")
cryptfs.Warn.Enabled = false // Silence DecryptBlock() error messages on incorrect password
Expand Down
27 changes: 15 additions & 12 deletions password.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ import (
)

func readPasswordTwice(extpass string) string {
fmt.Printf("Password: ")
p1 := readPassword(extpass)
fmt.Printf("Repeat: ")
p2 := readPassword(extpass)
if p1 != p2 {
fmt.Printf("Passwords do not match\n")
os.Exit(ERREXIT_PASSWORD)
if extpass == "" {
fmt.Printf("Password: ")
p1 := readPassword("")
fmt.Printf("Repeat: ")
p2 := readPassword("")
if p1 != p2 {
fmt.Println(colorRed + "Passwords do not match" + colorReset)
os.Exit(ERREXIT_PASSWORD)
}
return p1
} else {
return readPassword(extpass)
}
return p1
}

// readPassword - get password from terminal
Expand All @@ -33,10 +37,9 @@ func readPassword(extpass string) string {
cmd.Stderr = os.Stderr
output, err = cmd.Output()
if err != nil {
fmt.Printf("extpass program returned error: %v\n", err)
fmt.Printf(colorRed+"extpass program returned error: %v\n"+colorReset, err)
os.Exit(ERREXIT_PASSWORD)
}
fmt.Printf("(extpass)\n")
// Trim trailing newline like terminal.ReadPassword() does
if output[len(output)-1] == '\n' {
output = output[:len(output)-1]
Expand All @@ -45,14 +48,14 @@ func readPassword(extpass string) string {
fd := int(os.Stdin.Fd())
output, err = terminal.ReadPassword(fd)
if err != nil {
fmt.Printf("Error: Could not read password from terminal: %v\n", err)
fmt.Printf(colorRed+"Could not read password from terminal: %v\n"+colorReset, err)
os.Exit(ERREXIT_PASSWORD)
}
fmt.Printf("\n")
}
password = string(output)
if password == "" {
fmt.Printf("Error: password is empty\n")
fmt.Printf(colorRed + "Password is empty\n" + colorReset)
os.Exit(ERREXIT_PASSWORD)
}
return password
Expand Down

0 comments on commit 1030522

Please sign in to comment.