aboutsummaryrefslogtreecommitdiff
path: root/password.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2016-06-15 22:43:31 +0200
committerJakob Unterwurzacher2016-06-15 22:44:24 +0200
commitc89455063cfd9c531c0a671251ccfcd46f09403d (patch)
tree1bd5330aad0ac7b16ecb5b35150a304e56271be3 /password.go
parent218bf83ce399832a0eccfbd025e5dd0399db6bed (diff)
readpassword: create internal package for password reading
* Supports stdin * Add tests for extpass and stdin As per user request at https://github.com/rfjakob/gocryptfs/issues/30
Diffstat (limited to 'password.go')
-rw-r--r--password.go64
1 files changed, 0 insertions, 64 deletions
diff --git a/password.go b/password.go
deleted file mode 100644
index 01c71a7..0000000
--- a/password.go
+++ /dev/null
@@ -1,64 +0,0 @@
-package main
-
-import (
- "fmt"
- "os"
- "os/exec"
- "strings"
-
- "golang.org/x/crypto/ssh/terminal"
-
- "github.com/rfjakob/gocryptfs/internal/toggledlog"
-)
-
-func readPasswordTwice(extpass string) string {
- if extpass == "" {
- fmt.Fprintf(os.Stderr, "Password: ")
- p1 := readPassword("")
- fmt.Fprintf(os.Stderr, "Repeat: ")
- p2 := readPassword("")
- if p1 != p2 {
- toggledlog.Fatal.Println(colorRed + "Passwords do not match" + colorReset)
- os.Exit(ERREXIT_PASSWORD)
- }
- return p1
- } else {
- return readPassword(extpass)
- }
-}
-
-// readPassword - get password from terminal
-// or from the "extpass" program
-func readPassword(extpass string) string {
- var password string
- var err error
- var output []byte
- if extpass != "" {
- parts := strings.Split(extpass, " ")
- cmd := exec.Command(parts[0], parts[1:]...)
- cmd.Stderr = os.Stderr
- output, err = cmd.Output()
- if err != nil {
- toggledlog.Fatal.Printf(colorRed+"extpass program returned error: %v\n"+colorReset, err)
- os.Exit(ERREXIT_PASSWORD)
- }
- // Trim trailing newline like terminal.ReadPassword() does
- if output[len(output)-1] == '\n' {
- output = output[:len(output)-1]
- }
- } else {
- fd := int(os.Stdin.Fd())
- output, err = terminal.ReadPassword(fd)
- if err != nil {
- toggledlog.Fatal.Printf(colorRed+"Could not read password from terminal: %v\n"+colorReset, err)
- os.Exit(ERREXIT_PASSWORD)
- }
- fmt.Fprintf(os.Stderr, "\n")
- }
- password = string(output)
- if password == "" {
- toggledlog.Fatal.Printf(colorRed + "Password is empty\n" + colorReset)
- os.Exit(ERREXIT_PASSWORD)
- }
- return password
-}