summaryrefslogtreecommitdiff
path: root/password.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2015-11-15 13:38:19 +0100
committerJakob Unterwurzacher2015-11-15 13:42:04 +0100
commitd95fc2333aa5c05de713694c0893c7690655a584 (patch)
tree676402f6a85ab21839c7757dfbf9493ca921770d /password.go
parent066c2c90eb0a156efe9d1a407b32350673a09fed (diff)
Add "-extpass" cli option and associated tests
Diffstat (limited to 'password.go')
-rw-r--r--password.go54
1 files changed, 41 insertions, 13 deletions
diff --git a/password.go b/password.go
index 6b3452a..ede65dd 100644
--- a/password.go
+++ b/password.go
@@ -2,15 +2,18 @@ package main
import (
"fmt"
- "golang.org/x/crypto/ssh/terminal"
"os"
+ "os/exec"
+ "strings"
+
+ "golang.org/x/crypto/ssh/terminal"
)
-func readPasswordTwice() string {
+func readPasswordTwice(extpass string) string {
fmt.Printf("Password: ")
- p1 := readPassword()
- fmt.Printf("Repeat: ")
- p2 := readPassword()
+ p1 := readPassword(extpass)
+ fmt.Printf("Repeat: ")
+ p2 := readPassword(extpass)
if p1 != p2 {
fmt.Printf("Passwords do not match\n")
os.Exit(ERREXIT_PASSWORD)
@@ -18,14 +21,39 @@ func readPasswordTwice() string {
return p1
}
-// Get password from terminal
-func readPassword() string {
- fd := int(os.Stdin.Fd())
- p, err := terminal.ReadPassword(fd)
- fmt.Printf("\n")
- if err != nil {
- fmt.Printf("Error: Could not read password: %v\n", err)
+// 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 {
+ fmt.Printf("extpass program returned error: %v\n", 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]
+ }
+ } else {
+ 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)
+ os.Exit(ERREXIT_PASSWORD)
+ }
+ fmt.Printf("\n")
+ }
+ password = string(output)
+ if password == "" {
+ fmt.Printf("Error: password is empty\n")
os.Exit(ERREXIT_PASSWORD)
}
- return string(p)
+ return password
}