forked from browserpass/browserpass-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browserpass.go
206 lines (172 loc) · 4.18 KB
/
browserpass.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
package browserpass
import (
"bufio"
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"io"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/dannyvankooten/browserpass/pass"
"github.com/dannyvankooten/browserpass/protector"
"github.com/gokyle/twofactor"
)
// Login represents a single pass login.
type Login struct {
Username string `json:"u"`
Password string `json:"p"`
OTP string `json:"digits"`
OTPLabel string `json:"label"`
}
var endianness = binary.LittleEndian
// msg defines a message sent from a browser extension.
type msg struct {
Action string `json:"action"`
Domain string `json:"domain"`
Entry string `json:"entry"`
}
// Run starts browserpass.
func Run(stdin io.Reader, stdout io.Writer, s pass.Store) error {
protector.Protect("stdio rpath proc exec")
for {
// Get message length, 4 bytes
var n uint32
if err := binary.Read(stdin, endianness, &n); err == io.EOF {
return nil
} else if err != nil {
return err
}
// Get message body
var data msg
lr := &io.LimitedReader{R: stdin, N: int64(n)}
if err := json.NewDecoder(lr).Decode(&data); err != nil {
return err
}
var resp interface{}
switch data.Action {
case "search":
list, err := s.Search(data.Domain)
if err != nil {
return err
}
resp = list
case "get":
rc, err := s.Open(data.Entry)
if err != nil {
return err
}
defer rc.Close()
login, err := readLoginGPG(rc)
if err != nil {
return err
}
if login.Username == "" {
login.Username = guessUsername(data.Entry)
}
resp = login
default:
return errors.New("Invalid action")
}
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(resp); err != nil {
return err
}
if err := binary.Write(stdout, endianness, uint32(b.Len())); err != nil {
return err
}
if _, err := b.WriteTo(stdout); err != nil {
return err
}
}
}
func detectGPGBin() (string, error) {
binPriorityList := []string{
"gpg2", "/bin/gpg2", "/usr/bin/gpg2", "/usr/local/bin/gpg2",
"gpg", "/bin/gpg", "/usr/bin/gpg", "/usr/local/bin/gpg",
}
binToUse := ""
for _, bin := range binPriorityList {
binCheck := exec.Command(bin, "--version")
if err := binCheck.Run(); err == nil {
binToUse = bin
break
}
}
if binToUse == "" {
return "", errors.New("Unable to detect the location of gpg binary")
}
return binToUse, nil
}
// readLoginGPG reads a encrypted login from r using the system's GPG binary.
func readLoginGPG(r io.Reader) (*Login, error) {
gpgbin, err := detectGPGBin()
if err != nil {
return nil, err
}
opts := []string{"--decrypt", "--yes", "--quiet", "--batch", "-"}
// Run gpg
cmd := exec.Command(gpgbin, opts...)
cmd.Stdin = r
rc, err := cmd.StdoutPipe()
if err != nil {
return nil, err
}
var errbuf bytes.Buffer
cmd.Stderr = &errbuf
if err := cmd.Start(); err != nil {
return nil, err
}
protector.Protect("stdio")
// Read decrypted output
login, err := parseLogin(rc)
if err != nil {
return nil, err
}
defer rc.Close()
if err := cmd.Wait(); err != nil {
return nil, errors.New(err.Error() + "\n" + errbuf.String())
}
return login, nil
}
func parseTotp(str string, l *Login) error {
re := regexp.MustCompile("^otpauth.*$")
ourl := re.FindString(str)
if ourl != "" {
o, label, err := twofactor.FromURL(ourl)
if err != nil {
return err
}
l.OTP = o.OTP()
l.OTPLabel = label
}
return nil
}
// parseLogin parses a login and a password from a decrypted password file.
func parseLogin(r io.Reader) (*Login, error) {
login := new(Login)
scanner := bufio.NewScanner(r)
// The first line is the password
scanner.Scan()
login.Password = scanner.Text()
// Keep reading file for string in "login:", "username:" or "user:" format (case insensitive).
re := regexp.MustCompile("(?i)^(login|username|user):")
for scanner.Scan() {
line := scanner.Text()
parseTotp(line, login)
replaced := re.ReplaceAllString(line, "")
if len(replaced) != len(line) {
login.Username = strings.TrimSpace(replaced)
}
}
return login, nil
}
// guessLogin tries to guess a username from an entry's name.
func guessUsername(name string) string {
if strings.Count(filepath.ToSlash(name), "/") >= 1 {
return filepath.Base(name)
}
return ""
}