-
Notifications
You must be signed in to change notification settings - Fork 8
/
spf.go
86 lines (72 loc) · 1.67 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
package main
import (
"context"
"strings"
"github.com/foxcpp/mailsec-check/dns"
)
func evaluateSPF(domain string, res *Results) error {
res.spf = LevelSecure
ad, txts, err := extR.AuthLookupTXT(context.Background(), domain)
if err == dns.ErrNxDomain {
res.spf = LevelMissing
res.spfDesc = "no domain;"
return nil
} else if err != nil {
res.spf = LevelInvalid
res.spfDesc = "domain query error: " + err.Error() + ";"
return err
}
spfRecPresent := false
for _, txt := range txts {
if strings.HasPrefix(txt, "v=spf1") {
spfRecPresent = true
res.spfDesc += "present; "
if err := evalSPFRecord(txt, res); err != nil {
return err
}
}
}
if !spfRecPresent {
res.spf = LevelMissing
res.spfDesc += "no policy;"
return nil
}
if res.spfDesc == "present; " {
res.spfDesc += "strict; "
}
if !ad {
res.spf = LevelInsecure
res.spfDesc += "no DNSSEC; "
} else {
res.spf = LevelSecure
res.spfDesc += "DNSSEC-signed; "
}
return nil
}
func evalSPFRecord(txt string, res *Results) error {
res.spfRec = txt
parts := strings.Split(txt, " ")
if len(parts) == 0 {
res.spf = LevelMissing
res.spfDesc += "missing policy;"
}
for _, part := range parts {
if strings.HasPrefix(part, "redirect=") {
_, txts, err := extR.AuthLookupTXT(context.Background(), strings.TrimPrefix(part, "redirect="))
if err != nil {
return err
}
newTxt := strings.Join(txts, "")
return evalSPFRecord(newTxt, res)
}
switch part {
case "+all", "all":
res.spf = LevelInsecure
res.spfDesc += "policy allows any host; "
case "?all":
res.spf = LevelInsecure
res.spfDesc += "policy defines neutral result as default; "
}
}
return nil
}