-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
427 lines (367 loc) · 9.45 KB
/
main.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Copyright 2019 github.com/ucirello and https://cirello.io. All rights reserved.
//
// 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 writing, software distributed
// under the License is distributed on a "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.
// Command otp manages one-time passwords tokens, protecting them with a local
// private key (usually $HOME/.ssh/id_rsa) and storing its information in a
// encrypted db (usually at $HOME/.ssh/auth.db).
package main // import "cirello.io/otp"
import (
"bufio"
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"database/sql"
"encoding/pem"
"errors"
"fmt"
"image"
"image/png"
"io"
"log"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"text/tabwriter"
"time"
otp "github.com/pquerna/otp/totp"
"github.com/urfave/cli"
_ "modernc.org/sqlite"
"rsc.io/qr"
)
var homeDir string
func init() {
log.SetPrefix("")
log.SetFlags(0)
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
homeDir = usr.HomeDir
}
func main() {
app := cli.NewApp()
app.Name = "OTP client"
app.Usage = "command interface"
app.Version = "1.0.0"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "db",
Value: filepath.Join(homeDir, ".ssh", "auth.db"),
EnvVar: "OTP_DB",
},
cli.StringFlag{
Name: "private-key",
Value: filepath.Join(homeDir, ".ssh", "id_rsa"),
EnvVar: "OTP_PRIVKEY",
},
}
app.Commands = []cli.Command{
initdb(),
add(),
get(),
list(),
genqr(),
rm(),
servehttp(),
}
if err := app.Run(os.Args); err != nil {
log.Fatalf("error: %v", err)
}
}
func initdb() cli.Command {
return cli.Command{
Name: "init",
Usage: "initialize the OTP database",
Action: func(c *cli.Context) error {
db, err := sql.Open("sqlite", c.GlobalString("db"))
if err != nil {
return err
}
defer db.Close()
queries := []string{
"CREATE TABLE IF NOT EXISTS `otps` (`id` INTEGER PRIMARY KEY, `account` char, `issuer` char, `password` blob);",
"CREATE UNIQUE INDEX `otps_account_issuer` ON `otps`(`account`, `issuer`);",
}
for _, q := range queries {
_, err = db.Exec(q)
if err != nil {
return err
}
}
log.Println("database initialized")
return nil
},
}
}
func add() cli.Command {
return cli.Command{
Name: "add",
Usage: "a new OTP key",
ArgsUsage: "`secret` `issuer` `account-name`",
Action: func(c *cli.Context) error {
priv, err := privkeyfile(c.GlobalString("private-key"))
if err != nil {
return err
}
secretkey := c.Args().Get(0)
issuer := c.Args().Get(1)
account := c.Args().Get(2)
switch {
case secretkey == "":
return errors.New("secret key is missing")
case issuer == "":
return errors.New("issuer is missing")
case account == "":
return errors.New("account name is missing")
}
enckey, err := priv.encrypted([]byte(secretkey), cryptlabel(account, issuer))
if err != nil {
return err
}
db, err := sql.Open("sqlite", c.GlobalString("db"))
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec("REPLACE INTO `otps` (`issuer`, `account`, `password`) VALUES (?, ?, ?);", issuer, account, enckey)
return err
},
}
}
func get() cli.Command {
return cli.Command{
Name: "get",
Usage: "generate OTP",
Action: func(c *cli.Context) error {
filter := c.Args().First()
if filter == "" {
return load(c, os.Stdout)
}
var buf bytes.Buffer
if err := load(c, &buf); err != nil {
return err
}
scanner := bufio.NewScanner(&buf)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, filter) {
fields := strings.Fields(scanner.Text())
fmt.Println(fields[len(fields)-1])
}
}
return scanner.Err()
},
}
}
func servehttp() cli.Command {
return cli.Command{
Name: "http",
Usage: "serve OTP in a HTTP interface",
Action: func(c *cli.Context) error {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "<html><body><pre>")
load(c, w)
fmt.Fprintln(w, "</pre></body></html>")
})
http.ListenAndServe(":9999", nil)
return nil
},
}
}
func load(c *cli.Context, w io.Writer) error {
priv, err := privkeyfile(c.GlobalString("private-key"))
if err != nil {
return err
}
db, err := sql.Open("sqlite", c.GlobalString("db"))
if err != nil {
return err
}
defer db.Close()
rows, err := db.Query("SELECT `account`, `issuer`, `password` FROM `otps` ORDER BY `account` ASC, `issuer` ASC;")
if err != nil {
return err
}
defer rows.Close()
tabw := tabwriter.NewWriter(w, 8, 8, 2, ' ', 0)
defer tabw.Flush()
fmt.Fprintln(tabw, "account\tissuer\texpiration\tcode")
for rows.Next() {
var (
account, issuer string
pw []byte
)
rows.Scan(&account, &issuer, &pw)
decrypted, err := priv.decrypted(pw, cryptlabel(account, issuer))
if err != nil {
return err
}
key := strings.ToUpper(strings.ReplaceAll(string(decrypted), " ", ""))
token, err := otp.GenerateCode(key, time.Now())
if err != nil {
return err
}
line := fmt.Sprintf("%s\t%s\t%vs\t%s", account, issuer, (30 - time.Now().Unix()%30), token)
fmt.Fprintln(tabw, line)
}
return nil
}
func list() cli.Command {
return cli.Command{
Name: "list",
Usage: "list all keys",
Action: func(c *cli.Context) error {
db, err := sql.Open("sqlite", c.GlobalString("db"))
if err != nil {
return err
}
defer db.Close()
rows, err := db.Query("SELECT account, issuer FROM `otps` ORDER BY account ASC, issuer ASC;")
if err != nil {
return err
}
defer rows.Close()
w := tabwriter.NewWriter(os.Stdout, 8, 8, 2, ' ', 0)
defer w.Flush()
fmt.Fprintln(w, "account\tissuer")
for rows.Next() {
var account, issuer string
rows.Scan(&account, &issuer)
fmt.Fprintln(w, fmt.Sprintf("%s\t%s", account, issuer))
}
return nil
},
}
}
func genqr() cli.Command {
return cli.Command{
Name: "qr",
Usage: "generate QR codes",
Action: func(c *cli.Context) error {
priv, err := privkeyfile(c.GlobalString("private-key"))
if err != nil {
return err
}
db, err := sql.Open("sqlite", c.GlobalString("db"))
if err != nil {
return err
}
defer db.Close()
rows, err := db.Query("SELECT `account`, `issuer`, `password` FROM `otps` ORDER BY `account` ASC, `issuer` ASC;")
if err != nil {
return err
}
defer rows.Close()
w := tabwriter.NewWriter(os.Stdout, 8, 8, 2, ' ', 0)
defer w.Flush()
fmt.Fprintln(w, "account\tissuer\tfile")
for rows.Next() {
var account, issuer string
var pw []byte
rows.Scan(&account, &issuer, &pw)
decrypted, err := priv.decrypted(pw, cryptlabel(account, issuer))
if err != nil {
return err
}
qrfn, err := generateQR(issuer, account, string(decrypted))
if err != nil {
line := fmt.Sprintf("%s\t%s\t%s", account, issuer, err)
fmt.Fprintln(w, line)
continue
}
line := fmt.Sprintf("%s\t%s\t%s", account, issuer, qrfn)
fmt.Fprintln(w, line)
}
return nil
},
}
}
func rm() cli.Command {
return cli.Command{
Name: "rm",
Usage: "delete a OTP key",
ArgsUsage: "`issuer` `account-name`",
Action: func(c *cli.Context) error {
issuer := c.Args().Get(0)
account := c.Args().Get(1)
switch {
case issuer == "":
return errors.New("issuer is missing")
case account == "":
return errors.New("account name is missing")
}
db, err := sql.Open("sqlite", c.GlobalString("db"))
if err != nil {
return err
}
defer db.Close()
_, err = db.Exec("DELETE FROM `otps` WHERE `issuer` = ? AND `account` = ?;", issuer, account)
return err
},
}
}
type privkey struct {
*rsa.PrivateKey
}
func privkeyfile(fn string) (*privkey, error) {
pemdata, err := os.ReadFile(fn)
if err != nil {
return nil, fmt.Errorf("cannot read key file: %s", err)
}
block, _ := pem.Decode(pemdata)
if block == nil {
return nil, errors.New("key data is not PEM encoded")
}
if got, want := block.Type, "RSA PRIVATE KEY"; got != want {
return nil, fmt.Errorf("mismatched key type. got: %q want: %q", got, want)
}
priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("invalid private key: %s", err)
}
return &privkey{PrivateKey: priv}, nil
}
func (p privkey) encrypted(in, label []byte) ([]byte, error) {
return rsa.EncryptOAEP(sha256.New(), rand.Reader, &p.PublicKey, in, label)
}
func (p privkey) decrypted(in, label []byte) ([]byte, error) {
return rsa.DecryptOAEP(sha256.New(), rand.Reader, p.PrivateKey, in, label)
}
func cryptlabel(account, issuer string) []byte {
return []byte(fmt.Sprint(account, issuer))
}
func generateQR(issuer, account, password string) (string, error) {
otpauth := fmt.Sprintf("otpauth://totp/%s:%s?secret=%s&issuer=%s", issuer, account, password, issuer)
code, err := qr.Encode(otpauth, qr.H)
if err != nil {
return "", err
}
img, _, err := image.Decode(bytes.NewReader(code.PNG()))
if err != nil {
panic(err)
}
fn := fmt.Sprintf("otp-qr-%s-%s.png", issuer, account)
out, err := os.Create(fn)
if err != nil {
return "", err
}
err = png.Encode(out, img)
if err != nil {
return "", err
}
return fn, nil
}