This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
261 lines (225 loc) · 6.56 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
package main
import (
"fmt"
"log"
"os"
"runtime/debug"
"strings"
"github.com/TNG/openpgp-validation-server/gpg"
"github.com/TNG/openpgp-validation-server/smtp"
"github.com/TNG/openpgp-validation-server/storage"
"github.com/TNG/openpgp-validation-server/validator"
"github.com/urfave/cli"
)
const (
okExitCode = 0
errorExitCode = 1
)
var (
gpgUtil *gpg.GPG // This service is mandatory.
store storage.GetSetDeleter // This service is optional, when not available no data will be stored.
mailSender smtp.MailSender // This service is optional, when not available no outgoing mail will be sent.
)
var smtpMailFrom string
func initGpgUtil(c *cli.Context) error {
privateKeyPath := c.String("private-key")
if privateKeyPath == "" {
return fmt.Errorf("Invalid private key file path: %s", privateKeyPath)
}
privateKeyInput, err := os.Open(privateKeyPath)
if err != nil {
return fmt.Errorf("Cannot open private key file '%s': %s", privateKeyPath, err)
}
defer func() {
err = privateKeyInput.Close()
if err != nil {
log.Fatalf("Close of private key file '%s' failed: %s", privateKeyPath, err)
}
}()
util, err := gpg.NewGPG(privateKeyInput, c.String("passphrase"))
if err != nil {
return fmt.Errorf("Cannot initialize GPG: %s", err)
}
gpgUtil = util
return nil
}
func initGlobalServices(c *cli.Context) (err error) {
if err = initGpgUtil(c); err != nil {
return err
}
if store, err = storage.NewStore(c.String("storage")); err != nil {
return err
}
smtpMailFrom = c.String("mail-from")
log.Printf("Sending mail from '%s'", smtpMailFrom)
smtpOutHost := fmt.Sprintf("%v:%v", c.String("smtp-out-host"), c.Int("smtp-out-port"))
log.Println("Using outgoing SMTP server at: ", smtpOutHost)
mailSender = smtp.NewSingleServerSendMailer(smtpOutHost)
return nil
}
func runServers(c *cli.Context) error {
if err := initGlobalServices(c); err != nil {
return err
}
httpHost := fmt.Sprintf("%v:%v", c.String("host"), c.Int("http-port"))
smtpInHost := fmt.Sprintf("%v:%v", c.String("host"), c.Int("smtp-in-port"))
log.Println("Setting up SMTP server listening at: ", smtpInHost)
go serveSMTPRequestReceiver(smtpInHost, c.String("external-http-host"))
log.Println("Setting up HTTP server listening at: ", httpHost)
log.Panic(serveNonceConfirmer(httpHost))
return nil
}
func processMailAction(c *cli.Context) (err error) {
var inputMail *os.File
inputFilePath := c.String("file")
if inputFilePath == "" {
inputMail = os.Stdin
} else {
inputMail, err = os.Open(inputFilePath)
if err != nil {
return fmt.Errorf("Cannot open mail file '%s': %s", inputFilePath, err)
}
defer func() { _ = inputMail.Close() }()
}
if err = initGlobalServices(c); err != nil {
return err
}
processMail := getIncomingMailHandler(c.String("external-http-host"))
processMail(inputMail)
return nil
}
func confirmNonceAction(c *cli.Context) error {
if err := initGlobalServices(c); err != nil {
return err
}
nonceString := c.String("nonce")
nonce, err := validator.NonceFromString(nonceString)
if err != nil {
return fmt.Errorf("Cannot parse nonce '%v': %v", nonceString, err)
}
handleNonceConfirmation(nonce)
return nil
}
func cliErrorHandler(action func(*cli.Context) error) func(*cli.Context) error {
return func(c *cli.Context) (e error) {
defer func() {
if r := recover(); r != nil {
debug.PrintStack()
e = cli.NewExitError(fmt.Sprintf("Panic: %v", r), errorExitCode)
}
}()
if err := action(c); err != nil {
return cli.NewExitError(fmt.Sprintf("Error: %v", err), errorExitCode)
}
return nil
}
}
// subCommands to execute single aspects of the key validation process without requiring the full server startup.
var subCommands = []cli.Command{
{
Name: "process-mail",
Usage: "process an incoming mail",
Action: cliErrorHandler(processMailAction),
Flags: append(
[]cli.Flag{
cli.StringFlag{
Name: "file",
Value: "./test/mails/signed_request_enigmail.eml",
// TODO Handle missing value, use better default
Usage: "`FILE_PATH` of the mail file, omit to read from stdin",
},
},
commonFlags...,
),
},
{
Name: "confirm-nonce",
Usage: "process an nonce that has been confirmed",
Action: cliErrorHandler(confirmNonceAction),
Flags: append(
[]cli.Flag{
cli.StringFlag{
Name: "nonce",
Value: "<missing>",
Usage: "String value of the Nonce",
},
},
commonFlags...,
),
},
}
var commonFlags = []cli.Flag{
cli.StringFlag{
Name: "private-key",
Value: "./test/keys/test-gpg-validation@server.local (0x87144E5E) sec.asc.gpg",
// TODO Handle missing value, use better default
Usage: "`PRIVATE_KEY_PATH` to the private OpenPGP key of the server",
},
cli.StringFlag{
Name: "passphrase",
Value: "validation",
// TODO Handle missing value, use better default.
Usage: "`PASSPHRASE` of the private key",
},
cli.StringFlag{
Name: "storage",
Value: "file",
Usage: fmt.Sprintf("Storage type, possible values: [%s]", strings.Join(storage.StorageTypes[:], ", ")),
},
cli.IntFlag{
Name: "smtp-out-port",
Value: 25,
Usage: "`SMTP_OUT_PORT` of the SMTP server where outgoing mails will be sent to",
},
cli.StringFlag{
Name: "smtp-out-host",
Value: "localhost",
Usage: "`SMTP_HOST` of the SMTP server where outgoing mails will be sent to",
},
cli.StringFlag{
Name: "mail-from",
Value: "openpgp-validation-server@server.local",
Usage: "`MAIL_FROM` of outgoing mails. This is NOT the FROM header of the mail.",
},
}
// RunApp starts the server with the provided arguments.
func RunApp(args []string) {
app := cli.NewApp()
app.Name = "OpenPGP Validation Service"
app.Usage = "Run a server that manages email verification and signs verified keys with the servers OpenPGP key."
app.Commands = subCommands
app.Action = cliErrorHandler(runServers)
app.Flags = append(
[]cli.Flag{
cli.StringFlag{
Name: "host",
Value: "localhost",
Usage: "`HOST` of the mail and http servers. Set to the blank value to bind to all interfaces.",
},
cli.IntFlag{
Name: "http-port",
Value: 8080,
Usage: "`PORT` for the HTTP nonce listener",
},
cli.StringFlag{
Name: "external-http-host",
Value: "localhost:8080",
Usage: "External HTTP host for the nonce validation (link in the email)",
},
cli.IntFlag{
Name: "smtp-in-port",
Value: 2525,
Usage: "`SMTP_IN_PORT` on which the service will listen for incoming mails",
},
},
commonFlags...,
)
if err := app.Run(args); err != nil {
cli.OsExiter(errorExitCode)
} else {
cli.OsExiter(okExitCode)
}
}
func main() {
RunApp(os.Args)
}