summaryrefslogtreecommitdiff
path: root/password.go
blob: 01c71a77287cf5e67bf54bccd8f65093ac7d713a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
}