-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.go
More file actions
346 lines (280 loc) · 8.56 KB
/
Copy pathparser.go
File metadata and controls
346 lines (280 loc) · 8.56 KB
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
package gospf
import (
"errors"
_ "fmt"
"net"
"strings"
)
/*
RFC 4408: (grammar for SPF record)
This section is normative and any discrepancies with the ABNF
fragments in the preceding text are to be resolved in favor of this
grammar.
See [RFC4234] for ABNF notation. Please note that as per this ABNF
definition, literal text strings (those in quotes) are case-
insensitive. Hence, "mx" matches "mx", "MX", "mX", and "Mx".
record = version terms *SP
version = "v=spf1"
terms = *( 1*SP ( directive / modifier ) )
directive = [ qualifier ] mechanism
qualifier = "+" / "-" / "?" / "~"
mechanism = ( all / include
/ A / MX / PTR / IP4 / IP6 / exists )
all = "all"
include = "include" ":" domain-spec
A = "a" [ ":" domain-spec ] [ dual-cidr-length ]
MX = "mx" [ ":" domain-spec ] [ dual-cidr-length ]
PTR = "ptr" [ ":" domain-spec ]
IP4 = "ip4" ":" ip4-network [ ip4-cidr-length ]
IP6 = "ip6" ":" ip6-network [ ip6-cidr-length ]
exists = "exists" ":" domain-spec
modifier = redirect / explanation / unknown-modifier
redirect = "redirect" "=" domain-spec
explanation = "exp" "=" domain-spec
unknown-modifier = name "=" macro-string
ip4-cidr-length = "/" 1*DIGIT
ip6-cidr-length = "/" 1*DIGIT
dual-cidr-length = [ ip4-cidr-length ] [ "/" ip6-cidr-length ]
ip4-network = qnum "." qnum "." qnum "." qnum
qnum = DIGIT ; 0-9
/ %x31-39 DIGIT ; 10-99
/ "1" 2DIGIT ; 100-199
/ "2" %x30-34 DIGIT ; 200-249
/ "25" %x30-35 ; 250-255
; conventional dotted quad notation. e.g., 192.0.2.0
ip6-network = <as per [RFC 3513], section 2.2>
; e.g., 2001:DB8::CD30
domain-spec = macro-string domain-end
domain-end = ( "." toplabel [ "." ] ) / macro-expand
toplabel = ( *alphanum ALPHA *alphanum ) /
( 1*alphanum "-" *( alphanum / "-" ) alphanum )
; LDH rule plus additional TLD restrictions
; (see [RFC3696], Section 2)
alphanum = ALPHA / DIGIT
explain-string = *( macro-string / SP )
macro-string = *( macro-expand / macro-literal )
macro-expand = ( "%{" macro-letter transformers *delimiter "}" )
/ "%%" / "%_" / "%-"
macro-literal = %x21-24 / %x26-7E
; visible characters except "%"
macro-letter = "s" / "l" / "o" / "d" / "i" / "p" / "h" /
"c" / "r" / "t"
transformers = *DIGIT [ "r" ]
delimiter = "." / "-" / "+" / "," / "/" / "_" / "="
name = ALPHA *( ALPHA / DIGIT / "-" / "_" / "." )
header-field = "Received-SPF:" [CFWS] result FWS [comment FWS]
[ key-value-list ] CRLF
result = "Pass" / "Fail" / "SoftFail" / "Neutral" /
"None" / "TempError" / "PermError"
key-value-list = key-value-pair *( ";" [CFWS] key-value-pair )
[";"]
key-value-pair = key [CFWS] "=" ( dot-atom / quoted-string )
key = "client-ip" / "envelope-from" / "helo" /
"problem" / "receiver" / "identity" /
mechanism / "x-" name / name
identity = "mailfrom" ; for the "MAIL FROM" identity
/ "helo" ; for the "HELO" identity
/ name ; other identities
dot-atom = <unquoted word as per [RFC2822]>
quoted-string = <quoted string as per [RFC2822]>
comment = <comment string as per [RFC2822]>
CFWS = <comment or folding white space as per [RFC2822]>
FWS = <folding white space as per [RFC2822]>
CRLF = <standard end-of-line token as per [RFC2822]>
*/
/*
directive = [ qualifier ] mechanism
qualifier = "+" / "-" / "?" / "~"
mechanism = ( all / include
/ A / MX / PTR / IP4 / IP6 / exists )
all = "all"
include = "include" ":" domain-spec
A = "a" [ ":" domain-spec ] [ dual-cidr-length ]
MX = "mx" [ ":" domain-spec ] [ dual-cidr-length ]
PTR = "ptr" [ ":" domain-spec ]
IP4 = "ip4" ":" ip4-network [ ip4-cidr-length ]
IP6 = "ip6" ":" ip6-network [ ip6-cidr-length ]
exists = "exists" ":" domain-spec
*/
type Directive struct {
term string
Qualifier string
Mechanism string
Arguments map[string]string // everything after ':'
}
func (d Directive) String() string {
out := "{"
out += "Qualifier: " + d.Qualifier + ", "
out += "Mechanism: " + d.Mechanism + ", "
//out += "Arguments: " + d.Arguments.String()
out += "}"
return out
}
type Directives []Directive
func isQualifier(char uint8) bool {
qualifiers := []uint8{
'+',
'-',
'?',
'~',
}
for _, q := range qualifiers {
if q == char {
return true
}
}
return false
}
// Get the qualifier (i.e. +,?,~,-)
func (d *Directive) getQualifier() string {
if len(d.term) <= 0 {
return ""
}
if isQualifier(d.term[0]) {
return string(d.term[0])
} else {
return ""
}
}
// Get the mechanism (i.e. mx, a, all, ip4, ...)
func (d *Directive) getMechanism() string {
if len(d.term) <= 0 {
return ""
}
term := d.term
if isQualifier(d.term[0]) {
term = term[1:]
}
index := strings.Index(term, ":")
if index == -1 {
return term
}
return term[0:index]
}
// Get the arguments (i.e. domain-spec, ip4-network, ip6-network, dual-cidr-length, ip4-cidr-length, ip6-cidr-length)
// See 'TestDirective -> directive.getArguments()' for possible return values
func (d *Directive) getArguments() map[string]string {
arguments := make(map[string]string)
index := -1
ip := false
term := d.term
index = strings.Index(term, ":")
if index != -1 {
// "a" [ ":" domain-spec ]
// "ip4" ":" ip4-network
// ...
term := term[index+1 : len(term)]
spec := ""
index = strings.Index(term, "/")
if index == -1 {
spec = term
} else {
spec = term[0:index]
term = term[index+1 : len(term)]
}
if net.ParseIP(spec) != nil {
arguments["ip"] = spec
ip = true
} else {
arguments["domain"] = spec
}
}
index = strings.Index(term, "/")
if index == -1 {
arguments["ip4-cidr"] = ""
arguments["ip6-cidr"] = ""
return arguments
}
if term[index:index+2] != "//" {
term = term[index+1 : len(term)]
} else {
term = term[index:len(term)]
}
cidrs := strings.Split(term, "//")
if ip {
if !strings.Contains(arguments["ip"], ":") {
arguments["ip4-cidr"] = cidrs[0]
} else {
arguments["ip6-cidr"] = cidrs[0]
}
} else {
if len(cidrs) == 2 {
// ip4-cidr + "/" + ip6-cidr
arguments["ip4-cidr"] = cidrs[0]
arguments["ip6-cidr"] = cidrs[1]
}
if len(cidrs) == 1 {
// ip4
arguments["ip4-cidr"] = cidrs[0]
arguments["ip6-cidr"] = ""
}
}
return arguments
}
func (directives *Directives) process() *Directives {
out := make(Directives, 0)
for _, d := range *directives {
d.Qualifier = d.getQualifier()
d.Mechanism = d.getMechanism()
d.Arguments = d.getArguments()
out = append(out, d)
}
*directives = out
return directives
}
/*
RFC 7208 and 4408:
Modifiers are name/value pairs that provide additional information.
Modifiers always have an "=" separating the name and the value.
modifier = redirect / explanation / unknown-modifier
redirect = "redirect" "=" domain-spec
explanation = "exp" "=" domain-spec
unknown-modifier = name "=" macro-string
*/
type Modifier struct {
term string
Key string
Value string
}
type Modifiers []Modifier
func isModifier(term string) bool {
if strings.Index(term, "=") != -1 {
return true
} else {
return false
}
}
func (modifiers *Modifiers) process() *Modifiers {
out := make(Modifiers, 0)
for _, m := range *modifiers {
index := strings.Index(m.term, "=")
m.Key = m.term[0:index]
if index >= len(m.term) {
m.Value = ""
} else {
m.Value = m.term[index+1 : len(m.term)]
}
out = append(out, m)
}
*modifiers = out
return modifiers
}
func getTerms(record string) ([]Directive, []Modifier, error) {
// As per the definition of the ABNF notation in RFC 5234, names are case insensitive
record = strings.ToLower(record)
version := "v=spf1"
terms := strings.Split(record, " ")
if terms[0] != version {
return nil, nil, errors.New("Unsupported SPF version: " + terms[0])
}
directives := make([]Directive, 0)
modifiers := make([]Modifier, 0)
for _, term := range terms[1:len(terms)] {
if isModifier(term) {
modifiers = append(modifiers, Modifier{term: term})
} else {
directives = append(directives, Directive{term: term})
}
}
return directives, modifiers, nil
}