-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
495 lines (416 loc) · 10.3 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package main
import (
"bytes"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"filippo.io/age"
"github.com/e-zk/page/store"
"github.com/e-zk/page/term"
"github.com/e-zk/subc"
"github.com/e-zk/wslcheck"
"github.com/atotto/clipboard"
)
const (
tmpName = "page-*"
wslClipPath = "/mnt/c/Windows/system32/clip.exe"
usageString = `usage: page [command] [args ...]
where [command] can be:
help show this help message
init generate age keypair
ls list password entries
open open/view a password entry
save save/add a new password entry
rm remove password entry
for help with subcommands type: page [command] -h
`
)
var (
recipientsPath string
privateKeyPath string
storePath string
)
// wrapper for Fprintf to print to stderr
func errPrint(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
}
// main usage/help message
func usage() {
errPrint(usageString)
}
// clipboard function
func clip(text string) (err error) {
wsl, err := wslcheck.Check()
if err != nil {
return err
}
if wsl {
cmd := exec.Command(wslClipPath)
cmd.Stdin = bytes.NewBuffer([]byte(text))
err = cmd.Run()
if err != nil {
return err
}
} else {
err = clipboard.WriteAll(text)
if err != nil {
return err
}
}
return nil
}
// get XDG_DATA_HOME
func userDataDir() (string, error) {
var path string
path = os.Getenv("XDG_DATA_HOME")
if path == "" {
path = os.Getenv("HOME")
if path == "" {
return "", errors.New("neither $XDG_DATA_HOME nor $HOME are defined")
}
path += "/.local/share"
}
return path, nil
}
// Read the recipient file and return an age.X25519Recipient
func getRecipients() (*age.X25519Recipient, error) {
var pubkey []byte
if _, err := os.Stat(recipientsPath); os.IsNotExist(err) {
return nil, fmt.Errorf("recipients file does not exist.\ndid you forget to run `page init'?")
}
pubkey, err := os.ReadFile(recipientsPath)
if err != nil {
return nil, nil
}
return age.ParseX25519Recipient(string(pubkey))
}
// Read the private key and return an age.X25519Identity
func getIdentity() (*age.X25519Identity, error) {
var privkey []byte
if _, err := os.Stat(privateKeyPath); os.IsNotExist(err) {
return nil, fmt.Errorf("private key file does not exist.\ndid you forget to run `page init'?")
}
privkey, err := os.ReadFile(privateKeyPath)
if err != nil {
return nil, nil
}
var strippedPrivkey string
for _, line := range strings.Split(string(privkey), "\n") {
if strings.HasPrefix(line, "#") || line == "" {
continue
}
strippedPrivkey = line
}
return age.ParseX25519Identity(strippedPrivkey)
}
// generate age key pair
func initKeys() {
pk, err := age.GenerateX25519Identity()
if err != nil {
log.Fatal(err)
}
pkFd, err := os.OpenFile(privateKeyPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
log.Fatal(err)
}
rFd, err := os.OpenFile(recipientsPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := pkFd.Close(); err != nil {
log.Fatal(err)
}
if err := rFd.Close(); err != nil {
log.Fatal(err)
}
}()
fmt.Fprintf(pkFd, "%s", pk)
fmt.Fprintf(rFd, "%s", pk.Recipient())
}
// List all password entries
func list() {
s := store.Store{Path: storePath}
entries, err := s.Entries()
if err != nil {
log.Fatal(err)
}
for _, e := range entries {
fmt.Printf("%s\n", e)
}
}
// Remove a password entry
func remove(force bool) {
entry := subc.Sub("rm").Arg(0)
// create a new store
// (we do not require identity/recipient for this operation)
s := store.Store{Path: storePath}
// check if the entry exists
ok, err := s.EntryExists(entry)
if err != nil {
log.Fatal(err)
}
if !ok {
log.Fatalf("entry %s does not exist", entry)
}
if !force {
ch, err := term.Ask("remove entry " + entry + "?")
if err != nil {
log.Fatal(err)
}
if !ch {
log.Println("aborted.")
return
}
}
err = s.RemoveEntry(entry)
if err != nil {
log.Fatal(err)
}
}
// open a password entry
func open(printPasswd bool) {
var content string
entry := subc.Sub("open").Arg(0)
id, err := getIdentity()
if err != nil {
log.Fatalf("error parsing private key file: %s", err)
}
rec, err := getRecipients()
if err != nil {
log.Fatalf("error parsing recipients file: %v", err)
}
s := store.Store{Path: storePath, Identity: id, Recipient: rec}
passwd, err := s.ReadEntry(entry)
if err != nil {
log.Fatal(err)
}
for _, line := range strings.Split(string(passwd), "\n") {
if strings.HasPrefix(line, "#") || line == "" {
continue
}
content = content + line + "\n"
}
if printPasswd {
fmt.Printf("%s\n", content)
return
}
err = clip(content)
if err != nil {
log.Fatal(err)
}
}
func openEditor(editor string, path string) error {
// open $EDITOR on the tempfile
cmd := exec.Command(editor, path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
return err
}
return nil
}
func edit(editor string) {
entry := subc.Sub("edit").Arg(0)
// setup store
id, err := getIdentity()
if err != nil {
log.Fatalf("error parsing private key file: %s", err)
}
rec, err := getRecipients()
if err != nil {
log.Fatalf("error parsing recipients file: %v", err)
}
s := store.Store{Path: storePath, Identity: id, Recipient: rec}
// create temporary file
tmp, err := os.CreateTemp(os.TempDir(), tmpName)
if err != nil {
log.Fatalf("error creating temporary file: %v", err)
}
// when this function returns the file will be closed and removed
defer func() {
tmp.Close()
os.Remove(tmp.Name())
}()
// does the entry exist?
if _, err = os.Stat(storePath + "/" + entry); !os.IsNotExist(err) {
var contentBuffer *bytes.Buffer
// otherwise, copy the content to the tmpfile
content, err := s.ReadEntry(entry)
if err != nil {
log.Fatalf("error during decryption: %v", err)
}
contentBuffer = bytes.NewBuffer(content)
if _, err = io.Copy(tmp, contentBuffer); err != nil {
log.Fatalf("error writing to temporary file: %v", err)
}
}
openEditor(editor, tmp.Name())
// overwrite the entry with the temporary file content
tmpContent, err := os.ReadFile(tmp.Name())
if err != nil {
log.Fatal(err)
}
s.WriteEntry(entry, tmpContent)
}
func generate(length int) {
entry := subc.Sub("gen").Arg(0)
// setup store
id, err := getIdentity()
if err != nil {
log.Fatalf("error parsing private key file: %s", err)
}
rec, err := getRecipients()
if err != nil {
log.Fatalf("error parsing recipients file: %v", err)
}
s := store.Store{Path: storePath, Identity: id, Recipient: rec}
// create temporary file
tmp, err := os.CreateTemp(os.TempDir(), tmpName)
if err != nil {
log.Fatalf("error creating temporary file: %v", err)
}
defer func() {
tmp.Close()
os.Remove(tmp.Name())
}()
if _, err = os.Stat(storePath + "/" + entry); os.IsNotExist(err) {
contentBuffer := &bytes.Buffer{}
// get (too many) random bytes
randBytes := make([]byte, length)
_, err := rand.Read(randBytes)
if err != nil {
log.Fatal(err)
}
encoder := base64.NewEncoder(base64.StdEncoding, contentBuffer)
encoder.Write(randBytes)
encoder.Close()
// only copy length bytes (runes) to tmp
if _, err = io.CopyN(tmp, contentBuffer, int64(length)); err != nil {
log.Fatalf("error writing to temporary file: %v", err)
}
} else {
log.Fatal(errors.New("entry already exists!"))
}
// overwrite the entry with the temporary file content
tmpContent, err := os.ReadFile(tmp.Name())
if err != nil {
log.Fatal(err)
}
s.WriteEntry(entry, tmpContent)
}
func main() {
log.SetFlags(0 | log.Lshortfile)
//log.SetFlags(0)
log.SetPrefix("page: ")
var (
editor string
length int
printPasswd bool
force bool
)
/*
env vars
*/
configHome, err := os.UserConfigDir()
if err != nil {
log.Fatal(err)
}
dataHome, err := userDataDir()
if err != nil {
log.Fatal(err)
}
editor, ok := os.LookupEnv("EDITOR")
if !ok {
// default editor to 'vi'
editor = "vi"
}
storePath = dataHome + "/page/secrets"
recipientsPath = configHome + "/page/recipients"
privateKeyPath = configHome + "/page/privkey"
/*
subcommands
*/
//subc.Usage = usage
subc.Sub("help").Usage = func() {
errPrint("help: show help\n")
}
subc.Sub("ls").Usage = func() {
errPrint("ls: list all secrets\n")
errPrint("usage: page ls\n")
}
subc.Sub("init").Usage = func() {
errPrint("init: generate key pair\n")
errPrint("usage: page init\n")
}
subc.Sub("edit").StringVar(&editor, "e", editor, "editor")
subc.Sub("edit").Usage = func() {
errPrint("edit: create/edit a secret\n")
errPrint("usage: page edit [-e editor] <secret>\nwhere:\n")
errPrint(" -e editor specify editor to use instead of $EDITOR\n")
errPrint(" <secret> is the filename of the secret to edit\n")
}
subc.Sub("gen").StringVar(&editor, "e", editor, "editor")
subc.Sub("gen").IntVar(&length, "l", 12, "length")
subc.Sub("gen").Usage = func() {
errPrint("gen: randomly generate a secret\n")
errPrint("usage: page gen [-l length] <secret>\nwhere:\n")
errPrint(" -l length specify length of string to be generated\n")
errPrint(" <secret> is the filename of the secret to edit\n")
}
subc.Sub("rm").BoolVar(&force, "f", false, "force remove entry (do not prompt)")
subc.Sub("rm").Usage = func() {
errPrint("rm: delete a secret\n")
errPrint("usage: page rm [-f] <secret>\nwhere:\n")
errPrint(" -f forces deletion (does not prompt)\n")
errPrint(" <secret> is the filename of the secret to remove\n")
}
subc.Sub("open").BoolVar(&printPasswd, "p", false, "copy/print password to stdout")
subc.Sub("open").Usage = func() {
errPrint("open: copy secret content to clipboard\n")
errPrint("usage: page open [-p] <secret>\nwhere:\n")
errPrint(" -p prints the secret instead of adding to clipboard\n")
errPrint(" <secret> is the filename of the secret to remove\n")
}
subcommand, err := subc.Parse()
if errors.Is(err, subc.ErrNoSubc) {
usage()
os.Exit(1)
} else if errors.Is(err, subc.ErrUsage) {
os.Exit(0)
} else if err != nil {
log.Fatal(err)
}
switch subcommand {
case "help":
usage()
case "ls":
list()
case "init":
initKeys()
case "edit":
edit(editor)
case "gen":
generate(length)
case "rm":
remove(force)
case "open":
open(printPasswd)
default:
errPrint("unknown command `%s'\n", os.Args[1])
usage()
os.Exit(1)
}
}