forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clihelper.go
211 lines (189 loc) · 4.7 KB
/
clihelper.go
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package action
import (
"bufio"
"fmt"
"os"
"os/signal"
"sort"
"strconv"
"strings"
"github.com/justwatchcom/gopass/gpg"
"golang.org/x/crypto/ssh/terminal"
)
// confirmRecipients asks the user to confirm a given set of recipients
func (s *Action) confirmRecipients(name string, recipients []string) ([]string, error) {
if s.Store.NoConfirm {
return recipients, nil
}
for {
fmt.Printf("gopass: Encrypting %s for these recipients:\n", name)
sort.Strings(recipients)
for _, r := range recipients {
kl, err := gpg.ListPublicKeys(r)
if err != nil {
fmt.Println(err)
continue
}
if len(kl) < 1 {
fmt.Println("key not found", r)
continue
}
fmt.Printf(" - %s\n", kl[0].OneLine())
}
fmt.Println("")
yes, err := askForBool("Do you want to continue?", true)
if err != nil {
return recipients, err
}
if yes {
return recipients, nil
}
return recipients, fmt.Errorf("user aborted")
}
}
// askForConfirmation asks a yes/no question until the user
// replies yes or no
func askForConfirmation(text string) bool {
for {
if choice, err := askForBool(text, false); err == nil {
return choice
}
}
}
// askForBool ask for a bool (yes or no) exactly once.
// The empty answer uses the specified default, any other answer
// is an error.
func askForBool(text string, def bool) (bool, error) {
choices := "y/N"
if def {
choices = "Y/n"
}
str, err := askForString(text, choices)
if err != nil {
return false, err
}
switch str {
case "Y/n":
return true, nil
case "y/N":
return false, nil
}
str = strings.ToLower(string(str[0]))
switch str {
case "y":
return true, nil
case "n":
return false, nil
default:
return false, fmt.Errorf("Unknown answer: %s", str)
}
}
// askForString asks for a string once, using the default if the
// anser is empty. Errors are only returned on I/O errors
func askForString(text, def string) (string, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s [%s]: ", text, def)
input, err := reader.ReadString('\n')
if err != nil {
return "", err
}
input = strings.TrimSpace(input)
if input == "" {
input = def
}
return input, nil
}
// askForInt asks for an valid interger once. If the input
// can not be converted to an int it returns an error
func askForInt(text string, def int) (int, error) {
str, err := askForString(text, strconv.Itoa(def))
if err != nil {
return 0, err
}
intVal, err := strconv.Atoi(str)
if err != nil {
return 0, err
}
return intVal, nil
}
// askForPassword prompts for a password twice until both match
func askForPassword(name string, askFn func(string) (string, error)) (string, error) {
if askFn == nil {
askFn = promptPass
}
for {
pass, err := askFn(fmt.Sprintf("Enter password for %s", name))
if err != nil {
return "", err
}
passAgain, err := askFn(fmt.Sprintf("Retype password for %s", name))
if err != nil {
return "", err
}
if pass == passAgain {
return strings.TrimSpace(pass), nil
}
fmt.Println("Error: the entered password do not match")
}
}
// askForKeyImport asks for permissions to import the named key
func askForKeyImport(key string) bool {
ok, err := askForBool("Do you want to import the public key '%s' into your keyring?", false)
if err != nil {
return false
}
return ok
}
// askforPrivateKey promts the user to select from a list of private keys
func askForPrivateKey(prompt string) (string, error) {
kl, err := gpg.ListPrivateKeys()
if err != nil {
return "", err
}
kl = kl.UseableKeys()
if len(kl) < 1 {
return "", fmt.Errorf("No useable private keys found")
}
for {
fmt.Println(prompt)
for i, k := range kl {
fmt.Printf("[%d] %s\n", i, k.OneLine())
}
iv, err := askForInt(fmt.Sprintf("Please enter the number of a key (0-%d)", len(kl)-1), 0)
if err != nil {
continue
}
if iv >= 0 && iv < len(kl) {
return kl[iv].Fingerprint, nil
}
}
}
// promptPass will prompt user's for a password by terminal.
func promptPass(prompt string) (pass string, err error) {
// Make a copy of STDIN's state to restore afterward
fd := int(os.Stdin.Fd())
oldState, err := terminal.GetState(fd)
if err != nil {
return "", fmt.Errorf("Could not get state of terminal: %s", err)
}
defer func() {
if err := terminal.Restore(fd, oldState); err != nil {
fmt.Printf("Failed to restore terminal: %s\n", err)
}
}()
// Restore STDIN in the event of a signal interruption
sigch := make(chan os.Signal, 1)
signal.Notify(sigch, os.Interrupt)
go func() {
for range sigch {
if err := terminal.Restore(fd, oldState); err != nil {
fmt.Printf("Failed to restore terminal: %s\n", err)
}
os.Exit(1)
}
}()
fmt.Printf("%s: ", prompt)
passBytes, err := terminal.ReadPassword(fd)
fmt.Println("")
return string(passBytes), err
}