This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
keys.go
157 lines (122 loc) · 3.16 KB
/
keys.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
package main
import (
"fmt"
"os"
"os/exec"
"code.google.com/p/go.crypto/openpgp"
)
var cmdAddKey = &Command{
Run: runAddKey,
Name: "add-key",
Short: "adds a key to postcrypt's gpg keyring",
Long: `
Usage: postcrypt add-key <keyid>
Help:
Adds a key to postcrypt's keyring. The keyid can be specified by any format
the gpg binary knows to add keys.
This command is just a shortcut to:
gpg --no-default-keyring --keyring $options[keyring] --recv-keys <keyid>
The location of the keyring file can be changed via the configuration
option "keyring".
To print all keys postcrypt knows, see 'postcrypt help list-keys'.
`,
}
var cmdShowKey = &Command{
Run: runShowKey,
Name: "show-key",
Short: "prints details about a key",
Long: `
Usage: postcrypt show-key <keyid>
Help:
Prints details of a key such as the key's identities.
`,
}
var cmdListKeys = &Command{
Run: runListKeys,
Name: "list-keys",
Short: "prints all to postcrypt known public keys and identities",
Long: `
Usage: postcrypt list-keys
Help:
Prints all to postcrypt known public keys and associated identities.
To add keys, see 'postcrypt help add-key'.
`,
}
func runAddKey(cmd *Command, args []string) {
var err error
log := NewTee("postcrypt")
path, _ := cmd.Config.GetString("main", "keyring")
if len(args) < 1 {
log.Err("too few arguments. run `go help " + cmd.Name + "`.")
return
}
log.Info("Adding key " + args[0])
exe := exec.Command("gpg", "--keyring", path, "--no-default-keyring", "--recv-keys", args[0])
err = exe.Run()
if err != nil {
log.Err("gpg returned: " + err.Error())
return
}
}
func runShowKey(cmd *Command, args []string) {
var err error
log := NewTee("postcrypt")
path, _ := cmd.Config.GetString("main", "keyring")
if len(args) < 1 {
log.Err("too few arguments. run `go help " + cmd.Name + "`.")
return
}
// open gpg keyring file
fh, _ := os.Open(path)
if err != nil {
log.Crit("could not open keyring: " + err.Error())
return
}
// read keyring
keyring, err := openpgp.ReadKeyRing(fh)
if err != nil {
log.Crit("could not read keyring: " + err.Error())
return
}
for _, entity := range keyring {
if args[0] == getKeyId(entity) {
fmt.Printf("%s:\n", getKeyId(entity))
for _, ident := range entity.Identities {
fmt.Printf("\t%s\n", ident.Name)
}
}
}
}
func runListKeys(cmd *Command, args []string) {
var err error
var emails []string
log := NewTee("postcrypt")
path, _ := cmd.Config.GetString("main", "keyring")
// open gpg keyring file
fh, _ := os.Open(path)
if err != nil {
log.Crit("could not open keyring: " + err.Error())
return
}
// read keyring
keyring, err := openpgp.ReadKeyRing(fh)
if err != nil {
log.Crit("could not read keyring: " + err.Error())
return
}
emails, _ = cmd.Config.GetOptions("keys")
emails = append(emails, getAllEmails(keyring)...)
fmt.Println("# Note: keys with (!!!) could not be found in keyring")
for _, e := range emails {
ids := getIdsByEmails(cmd.Config, keyring, []string{e})
fmt.Printf("%s = ", e)
for _, i := range ids {
if len(getKeysByIds(keyring, []string{i})) > 0 {
fmt.Printf("%s ", i)
} else {
fmt.Printf("%s(!!!) ", i)
}
}
fmt.Printf("\n")
}
}