-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmechanism.go
281 lines (237 loc) · 5.71 KB
/
mechanism.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
package spf
import (
"bytes"
"errors"
"fmt"
"net"
"strings"
)
type Result string
const (
Pass Result = "Pass"
Neutral Result = "Neutral"
Fail Result = "Fail"
SoftFail Result = "SoftFail"
None Result = "None"
TempError Result = "TempError"
PermError Result = "PermError"
)
var (
ErrNoMatch = errors.New("Client was not covered by the mechanism.")
)
// Mechanism represents a single mechanism in an SPF record.
type Mechanism struct {
Name string
Domain string
Prefix string
Result Result
Count int
}
// Return a Mechanism as a string
func (m *Mechanism) String() string {
var buf bytes.Buffer
buf.WriteString(m.Name)
if len(m.Domain) != 0 {
buf.WriteString(fmt.Sprintf(":%s", m.Domain))
}
if len(m.Prefix) != 0 {
buf.WriteString(fmt.Sprintf("/%s", m.Prefix))
}
buf.WriteString(fmt.Sprintf(" - %s", m.Result))
return buf.String()
}
// ResultTag maps the Result code to a suitable char
func (m *Mechanism) ResultTag() string {
switch m.Result {
case Fail:
return "-"
case SoftFail:
return "~"
case Pass:
return "+"
case Neutral:
return "?"
}
return "+"
}
// SPFString return a string representation of a mechanism, suitable for using
// in a TXT record.
func (m *Mechanism) SPFString() string {
var buf bytes.Buffer
tag := m.ResultTag()
switch m.Name {
case "redirect":
buf.WriteString(fmt.Sprintf("%s=%s", m.Name, m.Domain))
case "all":
buf.WriteString(fmt.Sprintf("%s%s", tag, m.Name))
default:
if tag != "+" {
buf.WriteString(tag)
}
buf.WriteString(m.Name)
if len(m.Domain) != 0 {
buf.WriteString(fmt.Sprintf(":%s", m.Domain))
}
if len(m.Prefix) != 0 {
buf.WriteString(fmt.Sprintf("/%s", m.Prefix))
}
}
return buf.String()
}
// Ensure the mechanism is valid
func (m *Mechanism) Valid() bool {
var hasResult bool
var hasName bool
var isIP bool
switch m.Result {
case Pass, Fail, SoftFail, Neutral:
hasResult = true
default:
hasResult = false
}
switch m.Name {
case "all", "a", "mx", "ip4", "ip6", "exists", "include", "ptr", "redirect":
hasName = true
default:
hasName = false
}
isIP = true
if m.Name == "ip4" || m.Name == "ip6" {
valid := net.ParseIP(m.Domain)
isIP = (valid != nil)
}
return hasResult && hasName && isIP
}
// Evaluate determines if the given IP address is covered by the mechanism.
// If the IP is covered, the mechanism result is returned and error is nil.
// If the IP is not covered an error is returned. The caller must check for
// the error to determine if the result is valid.
func (m *Mechanism) Evaluate(ip string, count int) (Result, error) {
parsedIP := net.ParseIP(ip)
switch m.Name {
case "all":
return m.Result, nil
case "exists":
_, err := net.LookupHost(m.Domain)
if err == nil {
return m.Result, nil
}
case "redirect":
spf, err := NewSPF(m.Domain, "", count)
// There is no clear definition of what to do with errors on a
// redirected domain. Trying to make wise choices here.
switch err {
case ErrFailedLookup:
return TempError, nil
default:
return PermError, nil
}
return spf.Test(ip), nil
case "include":
spf, err := NewSPF(m.Domain, "", count)
// If there is no SPF record for the included domain or if we have too
// many mechanisms that require DNS lookups it is considered a
// PermError. Any other error is ok to ignore.
if err == ErrNoRecord || err == ErrMaxCount {
return PermError, nil
}
// The include statment is meant to be used as an if-pass or on-pass
// statement. Meaning if we get a result other than Pass or PermError,
// it is ok to ignore it and move on to the other mechanisms.
result := spf.Test(ip)
if result == Pass || result == PermError {
return result, nil
}
case "a":
networks := aNetworks(m)
if ipInNetworks(parsedIP, networks) {
return m.Result, nil
}
case "mx":
networks := mxNetworks(m)
if ipInNetworks(parsedIP, networks) {
return m.Result, nil
}
case "ptr":
if testPTR(m, ip) {
return m.Result, nil
}
default:
network, err := networkCIDR(m.Domain, m.Prefix)
if err == nil {
if network.Contains(parsedIP) {
return m.Result, nil
}
}
}
return None, ErrNoMatch
}
// NewMechanism creates a new Mechanism struct using the given string and
// domain name. When the mechanism does not define the domain, the provided
// domain is used as the default.
func NewMechanism(str, domain string) (Mechanism, error) {
var m Mechanism
var err error
switch string(str[0]) {
case "-":
m, err = parseMechanism(Fail, str[1:], domain)
case "~":
m, err = parseMechanism(SoftFail, str[1:], domain)
case "+":
m, err = parseMechanism(Pass, str[1:], domain)
case "?":
m, err = parseMechanism(Neutral, str[1:], domain)
default:
m, err = parseMechanism(Pass, str, domain)
}
return m, err
}
func parseMechanism(r Result, str, domain string) (Mechanism, error) {
var m Mechanism
var n string
var d string
var p string
ci := strings.Index(str, ":")
pi := strings.Index(str, "/")
ei := strings.Index(str, "=")
switch {
case ei != -1:
n = str[:ei]
d = str[ei+1:]
// Domain should not be empty
if d == "" {
return m, ErrInvalidMechanism
}
case ci != -1 && pi != -1 && ci < pi: // name:domain/prefix
n = str[:ci]
d = str[ci+1 : pi]
p = str[pi+1:]
// Domain and prefix should not be empty
if d == "" || p == "" {
return m, ErrInvalidMechanism
}
case ci != -1: // name:domain
n = str[:ci]
d = str[ci+1:]
// Domain should not be empty
if d == "" {
return m, ErrInvalidMechanism
}
case pi != -1: // name/prefix
n = str[:pi]
d = domain
p = str[pi+1:]
// Prefix should not be empty
if p == "" {
return m, ErrInvalidMechanism
}
default: // name
n = str
d = domain
}
m.Result = r
m.Domain = d
m.Name = n
m.Prefix = p
return m, nil
}