-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
255 lines (216 loc) · 7.19 KB
/
config.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
package main
import (
"crypto/tls"
"fmt"
"os"
"regexp"
"strings"
"text/template"
"time"
)
type Config struct {
// Options for listening for incoming messages.
BindAddr string `help:"local bind address"`
SocketFd int `help:"file descriptor of socket to listen on"`
Credentials string `help:"username:password for authenticating to failmail"`
TlsCert string `help:"PEM certificate file for TLS"`
TlsKey string `help:"PEM key file for TLS"`
Ssl bool `help:"enable TLS immediately (disables STARTTLS)"`
ShutdownTimeout time.Duration `help:"wait this long for open connections to finish when shutting down or reloading"`
DebugReceiver bool `help:"log traffic sent to and from downstream connections"`
RewriteSrc string `help:"pattern to match on recipients for address rewriting"`
RewriteDest string `help:"rewrite matching recipients to this address"`
AllowUnencryptedAuth bool `help:"allow non-hashed authentication over unencrypted connections"`
// Options for storing messages.
MemoryStore bool `help:"store messages in memory instead of an on-disk maildir"`
MessageStore string `help:"use this directory as a maildir for holding received messages"`
// Options for summarizing messages.
From string `help:"from address"`
WaitPeriod time.Duration `help:"wait this long for more batchable messages"`
MaxWait time.Duration `help:"wait at most this long from first message to send summary"`
Poll time.Duration `help:"check the store for new messages this frequently"`
BatchExpr string `help:"an expression used to determine how messages are batched into summary emails"`
GroupExpr string `help:"an expression used to determine how messages are grouped within summary emails"`
Template string `help:"path to a summary message template file"`
// Options for relaying outgoing messages.
RelayAddr string `help:"upstream relay server address"`
RelayUser string `help:"username for auth to relay server"`
RelayPassword string `help:"password for auth to relay server"`
FailDir string `help:"write failed sends to this maildir"`
AllDir string `help:"write all sends to this maildir"`
// Options that control what gets run.
Receiver bool `help:"receive and store incoming messages"`
Sender bool `help:"summarize and send messages"`
// Monitoring options.
BindHTTP string `help:"local bind address for the HTTP server"`
Pidfile string `help:"write a pidfile to this path"`
Version bool `help:"show the version number and exit"`
}
func Defaults() *Config {
return &Config{
BindAddr: "localhost:2525",
ShutdownTimeout: 5 * time.Second,
MessageStore: "incoming",
From: DefaultFromAddress("failmail"),
WaitPeriod: 30 * time.Second,
MaxWait: 5 * time.Minute,
Poll: 5 * time.Second,
BatchExpr: `{{.Header.Get "X-Failmail-Split"}}`,
GroupExpr: `{{.Header.Get "Subject"}}`,
RelayAddr: "localhost:25",
FailDir: "failed",
BindHTTP: "localhost:8025",
}
}
func (c *Config) Auth() (Auth, error) {
if c.Credentials == "" {
return nil, nil
}
parts := strings.SplitN(c.Credentials, ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("credentials must be in username:password format")
}
return &SingleUserPlainAuth{parts[0], parts[1], c.AllowUnencryptedAuth}, nil
}
func (c *Config) Batch() GroupBy {
return GroupByExpr("batch", c.BatchExpr)
}
func (c *Config) Group() GroupBy {
return GroupByExpr("group", c.GroupExpr)
}
func (c *Config) Upstream() (Upstream, error) {
var upstream Upstream
if c.RelayAddr == "debug" {
upstream = &DebugUpstream{os.Stdout}
} else {
upstream = &LiveUpstream{c.RelayAddr, c.RelayUser, c.RelayPassword}
}
if c.AllDir != "" {
allMaildir := &Maildir{Path: c.AllDir}
if err := allMaildir.Create(); err != nil {
return upstream, err
}
upstream = NewMultiUpstream(&MaildirUpstream{allMaildir}, upstream)
}
return upstream, nil
}
func (c *Config) TLSConfig() (SessionSecurity, *tls.Config, error) {
if c.TlsCert == "" || c.TlsKey == "" {
return UNENCRYPTED, nil, nil
}
cert, err := tls.LoadX509KeyPair(c.TlsCert, c.TlsKey)
if err != nil {
return UNENCRYPTED, nil, err
}
tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}}
if c.Ssl {
return SSL, tlsConfig, nil
} else {
return TLS_PRE_STARTTLS, tlsConfig, nil
}
}
func (c *Config) SocketWithoutTLS() (ServerSocket, error) {
if c.SocketFd > 0 {
return NewFileServerSocket(uintptr(c.SocketFd))
} else {
return NewTCPServerSocket(c.BindAddr)
}
}
func (c *Config) Socket() (ServerSocket, error) {
socket, err := c.SocketWithoutTLS()
if err != nil {
return nil, err
}
if !c.Ssl {
return socket, nil
}
security, conf, err := c.TLSConfig()
if err != nil {
return nil, err
}
if security == SSL {
return NewSSLServerSocket(socket, conf), nil
} else {
return socket, nil
}
}
func (c *Config) SummaryRenderer() SummaryRenderer {
if c.Template != "" {
tmpl := template.Must(template.New(c.Template).Funcs(SUMMARY_TEMPLATE_FUNCS).ParseFiles(c.Template))
return &TemplateRenderer{tmpl}
}
return &NoRenderer{}
}
func (c *Config) Store() (MessageStore, error) {
switch {
case c.MemoryStore:
return NewMemoryStore(), nil
case c.MessageStore == "":
return nil, fmt.Errorf("must have either a memory store or a disk-backed store")
default:
maildir := &Maildir{Path: c.MessageStore}
err := maildir.Create()
if err != nil {
return nil, err
}
return NewDiskStore(maildir)
}
}
func (c *Config) MakeReceiver() (*Listener, error) {
auth, err := c.Auth()
if err != nil {
return nil, err
}
security, tlsConfig, err := c.TLSConfig()
if err != nil {
return nil, err
}
rewriter := AddressRewriter{}
if c.RewriteSrc != "" && c.RewriteDest != "" {
rewriter.Source = regexp.MustCompile(c.RewriteSrc)
rewriter.Dest = c.RewriteDest
} else if c.RewriteSrc != "" || c.RewriteDest != "" {
return nil, fmt.Errorf("--rewrite-src and --rewrite-dest must be given together")
}
// The listener talks SMTP to clients, and puts any messages they send onto
// the `received` channel.
if socket, err := c.Socket(); err != nil {
return nil, err
} else {
return &Listener{Socket: socket, Auth: auth, Security: security, TLSConfig: tlsConfig, Debug: c.DebugReceiver, Rewriter: rewriter}, nil
}
}
func (c *Config) MakeWriter() (*MessageWriter, error) {
if store, err := c.Store(); err != nil {
return nil, err
} else {
return &MessageWriter{store}, nil
}
}
func (c *Config) MakeSummarizer() (*MessageBuffer, error) {
if store, err := c.Store(); err != nil {
return nil, err
} else {
return &MessageBuffer{
SoftLimit: c.WaitPeriod,
HardLimit: c.MaxWait,
Batch: c.Batch(),
Group: c.Group(),
From: c.From,
Store: store,
Renderer: c.SummaryRenderer(),
batches: NewBatches(),
}, nil
}
}
func (c *Config) MakeSender() (*Sender, error) {
upstream, err := c.Upstream()
if err != nil {
return nil, err
}
failedMaildir := &Maildir{Path: c.FailDir}
if err := failedMaildir.Create(); err != nil {
return nil, err
}
return &Sender{upstream, failedMaildir}, nil
}