This repository has been archived by the owner on Jan 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pinentry.go
164 lines (146 loc) · 3.37 KB
/
pinentry.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
// Package pinentry implements a cross platform pinentry client. It can be used
// to obtain credentials from the user through a simple UI application.
package pinentry
import (
"bufio"
"bytes"
"fmt"
"io"
"net/url"
"os"
"os/exec"
"strings"
)
var (
// Unescape enables unescaping of percent encoded values,
// disabled by default for backward compatibility. See #1621
Unescape bool
)
func init() {
if sv := os.Getenv("GOPASS_PINENTRY_UNESCAPE"); sv == "on" || sv == "true" {
Unescape = true
}
}
// Client is a pinentry client
type Client struct {
cmd *exec.Cmd
in io.WriteCloser
out *bufio.Reader
bin string
}
// New creates a new pinentry client
func New() (*Client, error) {
bin := GetBinary()
cmd := exec.Command(bin)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
br := bufio.NewReader(stdout)
if err := cmd.Start(); err != nil {
return nil, err
}
// check welcome message
banner, _, err := br.ReadLine()
if err != nil {
return nil, err
}
if !bytes.HasPrefix(banner, []byte("OK")) {
return nil, fmt.Errorf("wrong banner from %s: %q", bin, banner)
}
cl := &Client{
cmd: cmd,
in: stdin,
out: br,
bin: bin,
}
return cl, nil
}
// Close closes the client
func (c *Client) Close() {
_ = c.in.Close()
}
// Confirm sends the confirm message
func (c *Client) Confirm() bool {
if err := c.Set("confirm", ""); err == nil {
return true
}
return false
}
// Set sets a key
func (c *Client) Set(key, value string) error {
key = strings.ToUpper(key)
if value != "" {
value = " " + value
}
val := "SET" + key + value + "\n"
if _, err := c.in.Write([]byte(val)); err != nil {
return err
}
line, _, _ := c.out.ReadLine()
if string(line) != "OK" {
return fmt.Errorf("error from %s: %q", c.bin, line)
}
return nil
}
// Option sets an option, e.g. "default-cancel=Abbruch" or "allow-external-password-cache"
func (c *Client) Option(value string) error {
val := "OPTION " + value + "\n"
if _, err := c.in.Write([]byte(val)); err != nil {
return err
}
line, _, _ := c.out.ReadLine()
if string(line) != "OK" {
return fmt.Errorf("error from %s: %q", c.bin, line)
}
return nil
}
// GetPin asks for the pin
func (c *Client) GetPin() ([]byte, error) {
if _, err := c.in.Write([]byte("GETPIN\n")); err != nil {
return nil, err
}
buf, _, err := c.out.ReadLine()
if err != nil {
return nil, err
}
if bytes.HasPrefix(buf, []byte("OK")) {
return nil, nil
}
// handle status messages
for bytes.HasPrefix(buf, []byte("S ")) {
buf, _, err = c.out.ReadLine()
if err != nil {
return nil, err
}
}
// now there should be some data
if !bytes.HasPrefix(buf, []byte("D ")) {
return nil, fmt.Errorf("unexpected response from %s: %s", c.bin, buf)
}
pin := make([]byte, len(buf))
if n := copy(pin, buf); n != len(buf) {
return nil, fmt.Errorf("failed to copy pin: expected %d bytes only copied %d", len(buf), n)
}
ok, _, err := c.out.ReadLine()
if err != nil {
return nil, err
}
if !bytes.HasPrefix(ok, []byte("OK")) {
return nil, fmt.Errorf("unexpected response from %s: %s", c.bin, ok)
}
pin = pin[2:]
// Handle unescaping, if enabled
if bytes.Contains(pin, []byte("%")) && Unescape {
sv, err := url.QueryUnescape(string(pin))
if err != nil {
return nil, fmt.Errorf("failed to unescape pin: %q", err)
}
pin = []byte(sv)
}
return pin, nil
}