/
pinentry.go
152 lines (139 loc) · 3.57 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
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package pinentry interfaces with the pinentry(1) command to securely
// prompt the user for a password using whichever user interface the
// user is currently using.
package pinentry // import "camlistore.org/pkg/misc/pinentry"
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"strings"
)
// ErrCancel is returned when the user explicitly aborts the password
// request.
var ErrCancel = errors.New("pinentry: Cancel")
// Request describes what the user should see during the request for
// their password.
type Request struct {
Desc, Prompt, OK, Cancel, Error string
}
func catch(err *error) {
rerr := recover()
if rerr == nil {
return
}
if e, ok := rerr.(string); ok {
*err = errors.New(e)
}
if e, ok := rerr.(error); ok {
*err = e
}
}
func check(err error) {
if err != nil {
panic(err)
}
}
func (r *Request) GetPIN() (pin string, outerr error) {
defer catch(&outerr)
bin, err := exec.LookPath("pinentry")
if err != nil {
return r.getPINNaïve()
}
cmd := exec.Command(bin)
stdin, _ := cmd.StdinPipe()
stdout, _ := cmd.StdoutPipe()
check(cmd.Start())
defer cmd.Wait()
defer stdin.Close()
br := bufio.NewReader(stdout)
lineb, _, err := br.ReadLine()
if err != nil {
return "", fmt.Errorf("Failed to get getpin greeting")
}
line := string(lineb)
if !strings.HasPrefix(line, "OK") {
return "", fmt.Errorf("getpin greeting said %q", line)
}
set := func(cmd string, val string) {
if val == "" {
return
}
fmt.Fprintf(stdin, "%s %s\n", cmd, val)
line, _, err := br.ReadLine()
if err != nil {
panic("Failed to " + cmd)
}
if string(line) != "OK" {
panic("Response to " + cmd + " was " + string(line))
}
}
set("SETPROMPT", r.Prompt)
set("SETDESC", r.Desc)
set("SETOK", r.OK)
set("SETCANCEL", r.Cancel)
set("SETERROR", r.Error)
set("OPTION", "ttytype="+os.Getenv("TERM"))
tty, err := os.Readlink("/proc/self/fd/0")
if err == nil {
set("OPTION", "ttyname="+tty)
}
fmt.Fprintf(stdin, "GETPIN\n")
lineb, _, err = br.ReadLine()
if err != nil {
return "", fmt.Errorf("Failed to read line after GETPIN: %v", err)
}
line = string(lineb)
if strings.HasPrefix(line, "D ") {
return line[2:], nil
}
if strings.HasPrefix(line, "ERR 83886179 ") {
return "", ErrCancel
}
return "", fmt.Errorf("GETPIN response didn't start with D; got %q", line)
}
func runPass(bin string, args ...string) error {
cmd := exec.Command(bin, args...)
cmd.Stdin = os.Stdin
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("%v error: %v, %s", bin, err, out)
}
return nil
}
func (r *Request) getPINNaïve() (string, error) {
stty, err := exec.LookPath("stty")
if err != nil {
return "", errors.New("no pinentry or stty found")
}
if err := runPass(stty, "-echo"); err != nil {
return "", err
}
defer runPass(stty, "echo")
if r.Desc != "" {
fmt.Printf("%s\n\n", r.Desc)
}
prompt := r.Prompt
if prompt == "" {
prompt = "Password"
}
fmt.Printf("%s: ", prompt)
br := bufio.NewReader(os.Stdin)
line, _, err := br.ReadLine()
if err != nil {
return "", err
}
return string(line), nil
}