-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathspf.go
202 lines (165 loc) · 4.45 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
// Package spf can parse an SPF record and determine if a given IP address is
// allowed to send email based on that record. SPF can handle all of the
// mechanisms defined at http://www.openspf.org/SPF_Record_Syntax. The redirect
// mechanism is ignored.
package spf
import (
"bytes"
"errors"
"fmt"
"net"
"strings"
)
const (
MaxCount = 10
)
var (
ErrNoRecord = errors.New("No SPF Record found.")
ErrFailedLookup = errors.New("DNS Lookup failed.")
ErrInvalidSPF = errors.New("Invalid SPF string.")
ErrIncludeLoop = errors.New("Include loop detected.")
ErrInvalidMechanism = errors.New("Invalid mechanism in SPF string.")
ErrMaxCount = errors.New("Exceeded maximum lookups.")
)
// SPF represents an SPF record for a particular Domain. The SPF record
// holds all of the Allow, Deny, and Neutral mechanisms.
type SPF struct {
Raw string
Domain string
Version string
Mechanisms []Mechanism
Count int
}
// Test evaluates each mechanism to determine the result for the client.
// Mechanisms are evaluated in order until one of them provides a valid
// result. If no valid results are provided, the default result of "Neutral"
// is returned.
func (s *SPF) Test(ip string) Result {
for _, m := range s.Mechanisms {
result, err := m.Evaluate(ip, s.Count)
if err == nil {
return result
}
}
return Neutral
}
// Return an SPF record as a string.
func (s *SPF) String() string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("Raw: %s\n", s.Raw))
buf.WriteString(fmt.Sprintf("Domain: %s\n", s.Domain))
buf.WriteString(fmt.Sprintf("Version: %s\n", s.Version))
buf.WriteString("Mechanisms:\n")
for _, m := range s.Mechanisms {
buf.WriteString(fmt.Sprintf("\t%s\n", m.String()))
}
return buf.String()
}
// SPFString returns a formatted SPF object as a string suitable for use in a
// TXT record.
func (s *SPF) SPFString() string {
var buf bytes.Buffer
buf.WriteString(fmt.Sprintf("v=%s", s.Version))
for _, m := range s.Mechanisms {
buf.WriteString(fmt.Sprintf(" %s", m.SPFString()))
}
return buf.String()
}
func getSPFRecord(domain string) (string, error) {
var spfText string
// DNS errors during domain name lookup should result in "TempError".
records, err := net.LookupTXT(domain)
if err != nil {
return "", ErrFailedLookup
}
// Find the SPF record among the TXT records for the domain.
for _, record := range records {
if strings.HasPrefix(record, "v=spf1") {
spfText = record
break
}
}
return spfText, nil
}
// Create a new SPF record for the given domain using the provided string. If
// the provided string is not valid an error is returned.
func NewSPF(domain, record string, count int) (SPF, error) {
var spf SPF
if record == "" {
spfText, err := getSPFRecord(domain)
if err != nil {
return spf, err
}
if spfText == "" {
return spf, ErrNoRecord
}
record = spfText
}
spf.Count = count
spf.Raw = record
spf.Domain = domain
if !strings.HasPrefix(record, "v=spf1") {
return spf, ErrInvalidSPF
}
for _, f := range strings.Fields(record) {
switch {
case strings.HasPrefix(f, "v="):
spf.Version = f[2:]
default:
mechanism, err := NewMechanism(f, domain)
if err != nil {
return spf, err
}
if !mechanism.Valid() {
return spf, ErrInvalidMechanism
}
switch mechanism.Name {
case "include":
spf.Count = spf.Count + 1
if mechanism.Domain == domain {
return spf, ErrIncludeLoop
}
case "redirect", "exists", "a", "mx", "ptr":
spf.Count = spf.Count + 1
default:
// No action
}
spf.Mechanisms = append(spf.Mechanisms, mechanism)
}
}
if spf.Count >= MaxCount {
return spf, ErrMaxCount
}
return spf, nil
}
/*
Exported functions.
*/
// SPFTest determines the clients sending status for the given email addres.
//
// SPFTest will return one of the following results:
// Pass, Fail, SoftFail, Neutral, None, TempError, or PermError
func SPFTest(ip, email string) (Result, error) {
var domain string
// Get domain name from email address.
if strings.Contains(email, "@") {
parts := strings.Split(email, "@")
domain = parts[1]
} else {
return None, errors.New("Email address must contain an @ sign.")
}
spfText, err := getSPFRecord(domain)
if err != nil {
return TempError, err
}
// No SPF record should result in None.
if spfText == "" {
return None, nil
}
// Create a new SPF struct
spf, err := NewSPF(domain, spfText, 0)
if err != nil {
return PermError, err
}
return spf.Test(ip), nil
}