This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
browserpass.go
276 lines (237 loc) · 6.57 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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"`
URL string `json:"url"`
AutoSubmit *bool `json:"autoSubmit,omitempty"`
}
var endianness = binary.LittleEndian
// Settings info for the browserpass program.
//
// The browser extension will look up settings in its localstorage and find
// which options have been selected by the user, and put them in a JSON object
// which is then passed along with the command over the native messaging api.
// Config defines the root config structure sent from the browser extension
type Config struct {
// Manual searches use FuzzySearch if true, GlobSearch otherwise
UseFuzzy bool `json:"use_fuzzy_search"`
CustomStores []pass.StoreDefinition `json:"customStores"`
}
// msg defines a message sent from a browser extension.
type msg struct {
Settings Config `json:"settings"`
Action string `json:"action"`
Domain string `json:"domain"`
Entry string `json:"entry"`
}
func SendError(err error, stdout io.Writer) error {
var buf bytes.Buffer
if writeError := json.NewEncoder(&buf).Encode(err.Error()); writeError != nil {
return err
}
if writeError := binary.Write(stdout, endianness, uint32(buf.Len())); writeError != nil {
return err
}
buf.WriteTo(stdout)
return err
}
// Run starts browserpass.
func Run(stdin io.Reader, stdout io.Writer) error {
protector.Protect("stdio rpath proc exec getpw")
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 SendError(err, stdout)
}
// Get message body
var data msg
lr := &io.LimitedReader{R: stdin, N: int64(n)}
if err := json.NewDecoder(lr).Decode(&data); err != nil {
return SendError(err, stdout)
}
s, err := pass.NewDefaultStore(data.Settings.CustomStores, data.Settings.UseFuzzy)
if err != nil {
return SendError(err, stdout)
}
var resp interface{}
switch data.Action {
case "search":
list, err := s.Search(data.Domain)
if err != nil {
return SendError(err, stdout)
}
resp = list
case "match_domain":
list, err := s.GlobSearch(data.Domain)
if err != nil {
return SendError(err, stdout)
}
resp = list
case "get":
rc, err := s.Open(data.Entry)
if err != nil {
return SendError(err, stdout)
}
defer rc.Close()
login, err := readLoginGPG(rc)
if err != nil {
return SendError(err, stdout)
}
if login.Username == "" {
login.Username = guessUsername(data.Entry)
}
resp = login
default:
return SendError(errors.New("Invalid action"), stdout)
}
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(resp); err != nil {
return SendError(err, stdout)
}
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 {
urlPattern := regexp.MustCompile("^otpauth.*$")
ourl := urlPattern.FindString(str)
if ourl == "" {
tokenPattern := regexp.MustCompile("(?i)^totp(-secret)?:")
token := tokenPattern.ReplaceAllString(str, "")
if len(token) != len(str) {
ourl = "otpauth://totp/?secret=" + strings.TrimSpace(token)
}
}
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).
userPattern := regexp.MustCompile("(?i)^(login|username|user):")
urlPattern := regexp.MustCompile("(?i)^(url|link|website|web|site):")
autoSubmitPattern := regexp.MustCompile("(?i)^autosubmit:")
for scanner.Scan() {
line := scanner.Text()
if login.OTP == "" {
parseTotp(line, login)
}
if login.Username == "" {
replaced := userPattern.ReplaceAllString(line, "")
if len(replaced) != len(line) {
login.Username = strings.TrimSpace(replaced)
}
}
if login.URL == "" {
replaced := urlPattern.ReplaceAllString(line, "")
if len(replaced) != len(line) {
login.URL = strings.TrimSpace(replaced)
}
}
if login.AutoSubmit == nil {
replaced := autoSubmitPattern.ReplaceAllString(line, "")
if len(replaced) != len(line) {
value := strings.ToLower(strings.TrimSpace(replaced)) == "true"
login.AutoSubmit = &value
}
}
}
// if an unlabelled OTP is present, label it with the username
if strings.TrimSpace(login.OTPLabel) == "" && login.OTP != "" {
login.OTPLabel = login.Username
}
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 ""
}