-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (184 loc) · 5.7 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
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"path"
"golang.org/x/crypto/ssh"
"github.com/howeyc/gopass"
"github.com/voxelbrain/goptions"
)
const (
PUBKEY_NAME = "%v"
PRIVKEY_NAME = "%v.priv"
)
func main() {
options := struct {
Host *net.TCPAddr `goptions:"-s, --server"`
Help goptions.Help `goptions:"-h, --help, description='Show this help'"`
goptions.Verbs
Token struct {
User string `goptions:"-u, --username, obligatory, description='Username to authenticate with'"`
} `goptions:"token"`
Get struct {
To string `goptions:"-s, --save-to, description='Directory to save keypair in'"`
User string `goptions:"-u, --username, obligatory, description='Target user to fetch keys of'"`
} `goptions:"get"`
Set struct {
Pubkey string `goptions:"-p, --pubkey, obligatory, description='Filename of the public key'"`
Privkey string `goptions:"-i, --privkey, obligatory, description='Filename of the private key'"`
User string `goptions:"-u, --username, obligatory, description='Target user to fetch keys of'"`
} `goptions:"set"`
Dispatch struct {
User string `goptions:"-u, --username, obligatory, description='Target user to fetch keys of'"`
} `goptions:"dispatch"`
Generate struct {
User string `goptions:"-u, --username, obligatory, description='Target user to fetch keys of'"`
} `goptions:"generate"`
}{ // Default values go here
Host: &net.TCPAddr{
IP: net.ParseIP("10.3.3.2"),
Port: 7654,
},
}
//options.Host.IP = net.ParseIP(os.Getenv("KE_HOST"))
fmt.Println("CDC Key Escrow Client v1")
goptions.ParseAndFail(&options)
if len(options.Verbs) <= 0 {
fmt.Println("You must specify a verb")
return
}
switch options.Verbs {
case "token":
fmt.Print("password> ")
password, _ := gopass.GetPasswd()
authToken(options.Host, options.Token.User, string(password))
break
case "get":
fmt.Print("token> ")
token, _ := gopass.GetPasswd()
getKey(options.Host, options.Get.User, string(token), options.Get.To)
break
case "set":
fmt.Print("token> ")
token, _ := gopass.GetPasswd()
setKey(options.Host, options.Set.User, string(token), options.Set.Pubkey, options.Set.Privkey)
break
case "dispatch":
fmt.Print("token> ")
token, _ := gopass.GetPasswd()
dispatchKey(options.Host, options.Dispatch.User, string(token))
break
case "generate":
fmt.Print("token> ")
token, _ := gopass.GetPasswd()
user := options.Generate.User
pubLoc := path.Join("/tmp", fmt.Sprintf("%v.pub", user))
priLoc := path.Join("/tmp", user)
// https://stackoverflow.com/questions/21151714/go-generate-an-ssh-public-key
privateKey, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
log.Fatalf("There was an error generating the private key")
}
// generate and write private key as PEM
privKeyFile, err := os.Create(priLoc)
defer privKeyFile.Close()
if err != nil {
log.Fatalln("Error generating private key")
}
privKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}
if err := pem.Encode(privKeyFile, privKeyPEM); err != nil {
log.Fatalln("Error generating key")
}
// generate and write public key
pub, err := ssh.NewPublicKey(&privateKey.PublicKey)
if err != nil {
log.Fatalf("Error generating public key")
}
ioutil.WriteFile(pubLoc, ssh.MarshalAuthorizedKey(pub), 0777)
setKey(options.Host, user, string(token), pubLoc, priLoc)
break
}
}
func authToken(host *net.TCPAddr, username string, password string) {
client, err := NewClient(host)
if err != nil {
log.Fatalf("Error connecting to server: %v", err.Error())
}
// Close the connection by the end of the function
defer client.Close()
token, err := client.GetToken(username, password)
if err != nil {
log.Fatalf("Error retrieving token: %v", err.Error())
}
fmt.Println()
fmt.Printf("User: %v\n", token.User)
fmt.Printf("Token: %v\n", token.Token)
}
func getKey(host *net.TCPAddr, username string, token string, to string) {
client, err := NewClient(host)
if err != nil {
log.Fatalf("Error connecting to server: %v", err.Error())
}
// Close the connection by the end of the function
defer client.Close()
key, err := client.GetKey(username, token)
if err != nil {
log.Fatalf("Error retrieving key: %v", err.Error())
}
if to == "" {
fmt.Println()
fmt.Printf("User: %v\n", key.User)
fmt.Printf("Public Key: %v\n", key.PublicKey)
fmt.Printf("Private Key: %v\n", key.PrivateKey)
} else {
if _, err = os.Stat(to); os.IsNotExist(err) {
os.MkdirAll(to, 0700)
}
f, err := os.Create(path.Join(to, fmt.Sprintf(PUBKEY_NAME, key.User)))
if err != nil {
log.Printf("Could not create public key")
}
_, err = f.WriteString(key.PublicKey)
if err != nil {
log.Printf("Could not write public key")
}
f, err = os.Create(path.Join(to, fmt.Sprintf(PRIVKEY_NAME, key.User)))
if err != nil {
log.Printf("Could not create private key")
}
f.Chmod(0600)
_, err = f.WriteString(key.PrivateKey)
if err != nil {
log.Printf("Could not write private key")
}
}
}
func setKey(host *net.TCPAddr, username string, token string, pubkey string, privkey string) {
client, err := NewClient(host)
if err != nil {
log.Fatalf("Error connecting to server: %v", err.Error())
}
defer client.Close()
err = client.SetKey(username, string(token), pubkey, privkey)
if err != nil {
log.Fatalf("Error setting key: %v", err.Error())
}
}
func dispatchKey(host *net.TCPAddr, username string, token string) {
client, err := NewClient(host)
if err != nil {
log.Fatalf("Error connecting to server: %v", err.Error())
}
defer client.Close()
err = client.Dispatch(username, token)
if err != nil {
log.Fatalf("Error setting key: %v", err.Error())
}
}