/
spf.go
410 lines (361 loc) · 11.3 KB
/
spf.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
/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package spf
import (
"context"
"errors"
"fmt"
"net"
"runtime/debug"
"runtime/trace"
"blitiri.com.ar/go/spf"
"github.com/emersion/go-message/textproto"
"github.com/emersion/go-msgauth/authres"
"github.com/emersion/go-msgauth/dmarc"
"github.com/foxcpp/maddy/framework/address"
"github.com/foxcpp/maddy/framework/buffer"
"github.com/foxcpp/maddy/framework/config"
modconfig "github.com/foxcpp/maddy/framework/config/module"
"github.com/foxcpp/maddy/framework/dns"
"github.com/foxcpp/maddy/framework/exterrors"
"github.com/foxcpp/maddy/framework/log"
"github.com/foxcpp/maddy/framework/module"
maddydmarc "github.com/foxcpp/maddy/internal/dmarc"
"github.com/foxcpp/maddy/internal/target"
"golang.org/x/net/idna"
)
const modName = "check.spf"
type Check struct {
instName string
enforceEarly bool
noneAction modconfig.FailAction
neutralAction modconfig.FailAction
failAction modconfig.FailAction
softfailAction modconfig.FailAction
permerrAction modconfig.FailAction
temperrAction modconfig.FailAction
log log.Logger
resolver dns.Resolver
}
func New(_, instName string, _, _ []string) (module.Module, error) {
return &Check{
instName: instName,
log: log.Logger{Name: modName},
resolver: dns.DefaultResolver(),
}, nil
}
func (c *Check) Name() string {
return modName
}
func (c *Check) InstanceName() string {
return c.instName
}
func (c *Check) Init(cfg *config.Map) error {
cfg.Bool("debug", true, false, &c.log.Debug)
cfg.Bool("enforce_early", true, false, &c.enforceEarly)
cfg.Custom("none_action", false, false,
func() (interface{}, error) {
return modconfig.FailAction{}, nil
}, modconfig.FailActionDirective, &c.noneAction)
cfg.Custom("neutral_action", false, false,
func() (interface{}, error) {
return modconfig.FailAction{}, nil
}, modconfig.FailActionDirective, &c.neutralAction)
cfg.Custom("fail_action", false, false,
func() (interface{}, error) {
return modconfig.FailAction{Quarantine: true}, nil
}, modconfig.FailActionDirective, &c.failAction)
cfg.Custom("softfail_action", false, false,
func() (interface{}, error) {
return modconfig.FailAction{}, nil
}, modconfig.FailActionDirective, &c.softfailAction)
cfg.Custom("permerr_action", false, false,
func() (interface{}, error) {
return modconfig.FailAction{Reject: true}, nil
}, modconfig.FailActionDirective, &c.permerrAction)
cfg.Custom("temperr_action", false, false,
func() (interface{}, error) {
return modconfig.FailAction{Reject: true}, nil
}, modconfig.FailActionDirective, &c.temperrAction)
_, err := cfg.Process()
if err != nil {
return err
}
return nil
}
type spfRes struct {
res spf.Result
err error
}
type state struct {
c *Check
msgMeta *module.MsgMetadata
spfFetch chan spfRes
log log.Logger
skip bool
}
func (c *Check) CheckStateForMsg(ctx context.Context, msgMeta *module.MsgMetadata) (module.CheckState, error) {
return &state{
c: c,
msgMeta: msgMeta,
spfFetch: make(chan spfRes, 1),
log: target.DeliveryLogger(c.log, msgMeta),
}, nil
}
func (s *state) spfResult(res spf.Result, err error) module.CheckResult {
_, fromDomain, _ := address.Split(s.msgMeta.OriginalFrom)
spfAuth := &authres.SPFResult{
Value: authres.ResultNone,
Helo: s.msgMeta.Conn.Hostname,
From: fromDomain,
}
if err != nil {
spfAuth.Reason = err.Error()
} else if res == spf.None {
spfAuth.Reason = "no policy"
}
switch res {
case spf.None:
spfAuth.Value = authres.ResultNone
return s.c.noneAction.Apply(module.CheckResult{
Reason: &exterrors.SMTPError{
Code: 550,
EnhancedCode: exterrors.EnhancedCode{5, 7, 23},
Message: "No SPF policy",
CheckName: modName,
Err: err,
},
AuthResult: []authres.Result{spfAuth},
})
case spf.Neutral:
spfAuth.Value = authres.ResultNeutral
return s.c.neutralAction.Apply(module.CheckResult{
Reason: &exterrors.SMTPError{
Code: 550,
EnhancedCode: exterrors.EnhancedCode{5, 7, 23},
Message: "Neutral SPF result is not permitted",
CheckName: modName,
Err: err,
},
AuthResult: []authres.Result{spfAuth},
})
case spf.Pass:
spfAuth.Value = authres.ResultPass
return module.CheckResult{AuthResult: []authres.Result{spfAuth}}
case spf.Fail:
spfAuth.Value = authres.ResultFail
return s.c.failAction.Apply(module.CheckResult{
Reason: &exterrors.SMTPError{
Code: 550,
EnhancedCode: exterrors.EnhancedCode{5, 7, 23},
Message: "SPF authentication failed",
CheckName: modName,
Err: err,
},
AuthResult: []authres.Result{spfAuth},
})
case spf.SoftFail:
spfAuth.Value = authres.ResultSoftFail
return s.c.softfailAction.Apply(module.CheckResult{
Reason: &exterrors.SMTPError{
Code: 550,
EnhancedCode: exterrors.EnhancedCode{5, 7, 23},
Message: "SPF authentication soft-failed",
CheckName: modName,
Err: err,
},
AuthResult: []authres.Result{spfAuth},
})
case spf.TempError:
spfAuth.Value = authres.ResultTempError
return s.c.temperrAction.Apply(module.CheckResult{
Reason: &exterrors.SMTPError{
Code: 451,
EnhancedCode: exterrors.EnhancedCode{4, 7, 23},
Message: "SPF authentication failed with a temporary error",
CheckName: modName,
Err: err,
},
AuthResult: []authres.Result{spfAuth},
})
case spf.PermError:
spfAuth.Value = authres.ResultPermError
return s.c.permerrAction.Apply(module.CheckResult{
Reason: &exterrors.SMTPError{
Code: 550,
EnhancedCode: exterrors.EnhancedCode{5, 7, 23},
Message: "SPF authentication failed with a permanent error",
CheckName: modName,
Err: err,
},
AuthResult: []authres.Result{spfAuth},
})
}
return module.CheckResult{
Reason: &exterrors.SMTPError{
Code: 550,
EnhancedCode: exterrors.EnhancedCode{4, 7, 23},
Message: fmt.Sprintf("Unknown SPF status: %s", res),
CheckName: modName,
Err: err,
},
AuthResult: []authres.Result{spfAuth},
}
}
func (s *state) relyOnDMARC(ctx context.Context, hdr textproto.Header) bool {
fromDomain, err := maddydmarc.ExtractFromDomain(hdr)
if err != nil {
s.log.Error("DMARC domains extract", err)
return false
}
policyDomain, record, err := maddydmarc.FetchRecord(ctx, s.c.resolver, fromDomain)
if err != nil {
s.log.Error("DMARC fetch", err, "from_domain", fromDomain)
return false
}
if record == nil {
return false
}
policy := record.Policy
// We check for subdomain using non-equality since fromDomain is either the
// subdomain of policyDomain or policyDomain itself (due to the way
// FetchRecord handles it).
if !dns.Equal(policyDomain, fromDomain) && record.SubdomainPolicy != "" {
policy = record.SubdomainPolicy
}
return policy != dmarc.PolicyNone
}
func prepareMailFrom(from string) (string, error) {
// INTERNATIONALIZATION: RFC 8616, Section 4
// Hostname is already in A-labels per SMTPUTF8 requirement.
// MAIL FROM domain should be converted to A-labels before doing
// anything.
fromMbox, fromDomain, err := address.Split(from)
if err != nil {
return "", &exterrors.SMTPError{
Code: 550,
EnhancedCode: exterrors.EnhancedCode{5, 1, 7},
Message: "Malformed address",
CheckName: "spf",
}
}
fromDomain, err = idna.ToASCII(fromDomain)
if err != nil {
return "", &exterrors.SMTPError{
Code: 550,
EnhancedCode: exterrors.EnhancedCode{5, 1, 7},
Message: "Malformed address",
CheckName: "spf",
}
}
// %{s} and %{l} do not match anything if it is non-ASCII.
// Since spf lib does not seem to care, strip it.
if !address.IsASCII(fromMbox) {
fromMbox = ""
}
return fromMbox + "@" + dns.FQDN(fromDomain), nil
}
func (s *state) CheckConnection(ctx context.Context) module.CheckResult {
defer trace.StartRegion(ctx, "check.spf/CheckConnection").End()
if s.msgMeta.Conn == nil {
s.skip = true
s.log.Println("locally generated message, skipping")
return module.CheckResult{}
}
ip, ok := s.msgMeta.Conn.RemoteAddr.(*net.TCPAddr)
if !ok {
s.skip = true
s.log.Println("non-IP SrcAddr")
return module.CheckResult{}
}
mailFrom, err := prepareMailFrom(s.msgMeta.OriginalFrom)
if err != nil {
s.skip = true
return module.CheckResult{
Reason: err,
Reject: true,
}
}
if s.c.enforceEarly {
res, err := spf.CheckHostWithSender(ip.IP,
dns.FQDN(s.msgMeta.Conn.Hostname), mailFrom,
spf.WithContext(ctx), spf.WithResolver(s.c.resolver))
s.log.Debugf("result: %s (%v)", res, err)
return s.spfResult(res, err)
}
// We start evaluation in parallel to other message processing,
// once we get the body, we fetch DMARC policy and see if it exists
// and not p=none. In that case, we rely on DMARC alignment to define result.
// Otherwise, we take action based on SPF only.
go func() {
defer func() {
if err := recover(); err != nil {
stack := debug.Stack()
log.Printf("panic during spf.CheckHostWithSender: %v\n%s", err, stack)
close(s.spfFetch)
}
}()
defer trace.StartRegion(ctx, "check.spf/CheckConnection (Async)").End()
res, err := spf.CheckHostWithSender(ip.IP, dns.FQDN(s.msgMeta.Conn.Hostname), mailFrom,
spf.WithContext(ctx), spf.WithResolver(s.c.resolver))
s.log.Debugf("result: %s (%v)", res, err)
s.spfFetch <- spfRes{res, err}
}()
return module.CheckResult{}
}
func (s *state) CheckSender(ctx context.Context, mailFrom string) module.CheckResult {
return module.CheckResult{}
}
func (s *state) CheckRcpt(ctx context.Context, rcptTo string) module.CheckResult {
return module.CheckResult{}
}
func (s *state) CheckBody(ctx context.Context, header textproto.Header, body buffer.Buffer) module.CheckResult {
if s.c.enforceEarly || s.skip {
// Already applied in CheckConnection.
return module.CheckResult{}
}
defer trace.StartRegion(ctx, "check.spf/CheckBody").End()
res, ok := <-s.spfFetch
if !ok {
return module.CheckResult{
Reject: true,
Reason: exterrors.WithTemporary(
exterrors.WithFields(errors.New("panic recovered"), map[string]interface{}{
"check": "spf",
"smtp_msg": "Internal error during policy check",
}),
true,
),
}
}
if s.relyOnDMARC(ctx, header) {
if res.res != spf.Pass {
s.log.Msg("deferring action due to a DMARC policy", "result", res.res, "err", res.err)
} else {
s.log.DebugMsg("deferring action due to a DMARC policy", "result", res.res, "err", res.err)
}
checkRes := s.spfResult(res.res, res.err)
checkRes.Quarantine = false
checkRes.Reject = false
return checkRes
}
return s.spfResult(res.res, res.err)
}
func (s *state) Close() error {
return nil
}
func init() {
module.Register(modName, New)
}