-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathspf.go
325 lines (275 loc) · 6.53 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
// RFC 7208
package spf
import (
"fmt"
"net"
"regexp"
"strings"
)
// Result of SPF check
type Result string
// SPF results
const (
None = Result("NONE")
Neutral = Result("NEUTRAL")
Pass = Result("PASS")
Fail = Result("FAIL")
Softfail = Result("SOFTFAIL")
TempError = Result("TEMPERROR")
PermError = Result("PERMERROR")
)
var (
mDirective = regexp.MustCompile("^(\\+|\\-|\\?|\\~)?(all|include|a|mx|ptr|ip4|ip6|exists):?(.*)$")
mModifier = regexp.MustCompile("^([a-z0-9\\-\\_\\.]+)=(.*)$")
)
// String representation of Result type
func (r Result) String() string {
return string(r)
}
// IsSet returns true if Result var is set to some value
func (r Result) IsSet() bool {
return string(r) != ""
}
type check struct {
cnt int
}
// CheckHost for SPF rules.
// ip - the IP address of the SMTP client that is emitting the mail, either IPv4 or IPv6.
// domain - the domain that provides the sought-after authorization information; initially, the domain portion of the "MAIL FROM" or "HELO" identity.
// sender - the "MAIL FROM" or "HELO" identity.
// helo - domain from helo, used as sender domain if sender is not specified.
func CheckHost(ip net.IP, domain, sender, helo string) Result {
if sender == "" {
sender = "postmaster@" + helo
}
c := check{
cnt: 0,
}
return c.checkHost(ip, domain, sender)
}
func (c *check) checkHost(ip net.IP, domain, sender string) Result {
defRes := None
spf, r := LookupSPF(domain)
if r.IsSet() {
return r
}
// log.Println("\n\n", spf, "\n------------------------")
terms := parseSPF(spf)
for _, term := range terms {
switch t := term.(type) {
case directive:
d := t
var r Result
switch d.mechanism {
case "a":
dom := d.domain(domain)
r = c.check(ip, dom, d.cidr(), d.qualifier)
case "mx":
dom := d.domain(domain)
r = c.checkMX(ip, dom, d.cidr(), d.qualifier)
case "include":
dom := d.domain(domain)
r = c.checkHost(ip, dom, sender)
// TODO: r is not as returned, see page 22
case "ptr":
// not recommended for use, but must be implemented
dom := d.domain(domain)
r = c.checkPTR(ip, dom, d.qualifier)
case "ip4":
if ip.To4() != nil { // check only if ip is IPv4 address
r = checkIP(ip, d.param, d.qualifier)
}
case "ip6":
if ip.To4() == nil { // check only if ip is IPv6 address
r = checkIP(ip, d.param, d.qualifier)
}
case "all":
return evalQualifier(d.qualifier)
case "exists":
dom, res := c.macro(d.param, ip, domain, sender, "")
if res == PermError {
return PermError
}
ips, _ := lookupA(dom)
if len(ips) > 0 {
return Pass
}
}
// check result from mechanism
switch r {
case Pass, PermError:
return r
case TempError:
defRes = r
default:
defRes = Neutral
}
case modifier:
switch t.name {
case "redirect":
return c.checkHost(ip, t.value, sender)
case "exp":
default:
}
// something to do with modifiers
}
}
return defRes
}
// check record of specific domain for IP, return true if match
func (c *check) check(ip net.IP, domain, cidr, qualifier string) Result {
if c.cnt == 10 {
return PermError
}
c.cnt++
var ips []net.IP
var err error
if ip.To4() == nil {
ips, err = lookupAAAA(domain)
} else {
ips, err = lookupA(domain)
}
if err != nil {
return TempError
}
for _, a := range ips {
if r := checkIP(ip, a.String()+cidr, qualifier); r != Neutral {
return r
}
}
return Neutral
}
func checkIP(ip net.IP, ipstr, qualifier string) Result {
_, ips, err := net.ParseCIDR(ipstr)
if err == nil {
//log.Println("Check range", ips.String())
if ips.Contains(ip) {
return evalQualifier(qualifier)
}
} else {
ipaddr := net.ParseIP(ipstr)
if ip.Equal(ipaddr) {
return evalQualifier(qualifier)
}
}
return Neutral
}
// evalQualifier returns Pass if qualifier is + or "" or other spf results accordingly
func evalQualifier(q string) Result {
switch q {
case "~":
return Softfail
case "-":
return Fail
case "?":
return Neutral
default:
return Pass
}
}
// checkA record of specific domain for IP, return true if match
func (c *check) checkMX(ip net.IP, domain, cidr, qualifier string) Result {
defRes := None
mxs, err := lookupMX(domain)
if err != nil {
return TempError
}
for _, mx := range mxs {
r := c.check(ip, mx, cidr, qualifier)
switch r {
case Pass, PermError, Fail:
return r
case TempError:
// on TempError continue to check other mx records, but remember temperror (dns unavailable)
defRes = r
}
}
return defRes
}
// checkPTR match
func (c *check) checkPTR(ip net.IP, domain, qualifier string) Result {
defRes := None
hosts, err := lookupPTR(ip)
if err != nil {
return TempError
}
var validated []string
for _, h := range hosts {
fmt.Println("PTR host:", h)
ips, _ := lookupA(h)
if len(ips) != 0 {
validated = append(validated, h)
fmt.Println("Validated", h)
}
}
for _, dom := range validated {
if dom == domain {
return evalQualifier(qualifier)
}
}
return defRes
}
type modifier struct {
name string
value string
}
type directive struct {
qualifier string
mechanism string
param string
}
// domain returns default domain (param) or domain specified in spf record after : sign
func (d directive) domain(domain string) string {
if d.param != "" {
parts := strings.SplitN(d.param, "/", 2)
return parts[0]
}
return domain
}
func (d directive) cidr() string {
n := strings.Index(d.param, "/")
if n != -1 {
return d.param[n:]
}
return ""
}
// directive
// qualifier
// mechanism
// = *( 1*SP ( directive / modifier ) )
// = [ qualifier ] mechanism
// = "+" / "-" / "?" / "~"
// = ( all / include / a / mx / ptr / ip4 / ip6 / exists )
// ParseSPF record and return slice with directives and modifiers
func parseSPF(spf string) []interface{} {
spf = strings.TrimSpace(strings.TrimPrefix(spf, "v=spf1"))
var terms []interface{}
parts := strings.Fields(spf)
for _, t := range parts {
dirMatch := mDirective.FindStringSubmatch(t)
if len(dirMatch) > 0 {
terms = append(terms, directive{
qualifier: dirMatch[1],
mechanism: dirMatch[2],
param: dirMatch[3],
})
continue
} else {
modMatch := mModifier.FindStringSubmatch(t)
if len(modMatch) > 0 {
terms = append(terms, modifier{
name: modMatch[1],
value: modMatch[2],
})
}
}
}
return terms
}
// v=spf1
//
// 550 5.7.1 SPF MAIL FROM check failed:
// 550 5.7.1 The domain example.com explains:
// 550 5.7.1 Please see http://www.example.com/mailpolicy.html
// Received-SPF:
// Authentication-Results: