-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspf.go
More file actions
655 lines (589 loc) · 17.5 KB
/
Copy pathspf.go
File metadata and controls
655 lines (589 loc) · 17.5 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
package auth
import (
"fmt"
"net"
"net/url"
"strconv"
"strings"
"time"
)
// SPFResult represents the outcome of an SPF check.
type SPFResult string
const (
SPFPass SPFResult = "pass"
SPFFail SPFResult = "fail"
SPFSoftFail SPFResult = "softfail"
SPFNeutral SPFResult = "neutral"
SPFNone SPFResult = "none"
SPFTempError SPFResult = "temperror"
SPFPermError SPFResult = "permerror"
)
// CheckSPF verifies the SPF record for the sender's domain against the connecting IP.
// RFC 7208 compliant with DNS lookup counter (max 10), exists mechanism, exp= modifier, and macro expansion.
func CheckSPF(ip, mailFrom string) (SPFResult, string) {
if mailFrom == "" {
return SPFNone, "no envelope sender"
}
parts := strings.SplitN(mailFrom, "@", 2)
if len(parts) != 2 {
return SPFNone, "invalid envelope sender"
}
domain := parts[1]
// Parse local part for macro expansion
localPart := parts[0]
// Create context for macro expansion and DNS tracking
ctx := &spfContext{
senderIP: ip,
localPart: localPart,
domain: domain,
helo: domain, // Default to domain if HELO not provided
receiverDomain: domain, // Default to domain; caller can override
lookups: 0,
voidLookups: 0,
timestamp: time.Now().Unix(),
}
clientIP := net.ParseIP(ip)
if clientIP == nil {
return SPFPermError, "invalid client IP"
}
var explanation string
result, msg := checkSPFDomainWithContext(clientIP, domain, 0, ctx, &explanation)
return result, msg
}
// spfContext holds state for SPF checking (DNS lookups, macro context).
type spfContext struct {
senderIP string // Client connecting IP
localPart string // Local part of mail-from
domain string // Mail-from domain
helo string // HELO/EHLO domain
receiverDomain string // Receiver's domain (for %{r} macro)
lookups int // DNS lookup counter (max 10 per RFC 7208)
voidLookups int // Void lookup counter (max 2 per RFC 7208 §4.6.4)
timestamp int64 // Current timestamp for %{t} macro
}
// checkSPFDomainWithContext recursively checks SPF for included/redirected domains with full RFC 7208 support.
func checkSPFDomainWithContext(clientIP net.IP, domain string, depth int, ctx *spfContext, explanation *string) (SPFResult, string) {
// Check lookup counter (RFC 7208 §4.6.4)
if ctx.lookups >= 10 {
return SPFPermError, "too many DNS lookups (max 10 allowed)"
}
// Look up SPF record (counts as one DNS lookup for mechanisms that need it)
ctx.lookups++
txtRecords, err := net.LookupTXT(domain)
if err != nil {
// RFC 7208 §4.3: NXDOMAIN or NODATA at top level → SPFNone
// At nested level (include/redirect) → SPFPermError
// Other DNS errors → SPFTempError
if depth == 0 {
// Top-level domain: treat NXDOMAIN same as "no SPF record"
// (both cases mean "no policy to check")
return SPFNone, "no SPF record found"
}
// At included/redirected level: NXDOMAIN is a permerror
return SPFPermError, fmt.Sprintf("DNS lookup failed for included domain: %v", err)
}
var spfRecord string
for _, txt := range txtRecords {
if strings.HasPrefix(txt, "v=spf1 ") || txt == "v=spf1" {
spfRecord = txt
break
}
}
if spfRecord == "" {
if depth == 0 {
return SPFNone, "no SPF record found"
}
return SPFNeutral, fmt.Sprintf("no SPF record for %s", domain)
}
// Parse terms - separate mechanisms from modifiers
terms := strings.Fields(spfRecord)
var redirectDomain, expModifier string
// First pass: extract modifiers (redirect=, exp=)
var mechanisms []string
for _, term := range terms[1:] { // Skip "v=spf1"
if strings.HasPrefix(term, "redirect=") {
redirectDomain = term[9:]
} else if strings.HasPrefix(term, "exp=") {
expModifier = term[4:]
} else {
mechanisms = append(mechanisms, term)
}
}
// Second pass: evaluate mechanisms
for _, term := range mechanisms {
qualifier := SPFPass
mechanism := term
// Check for qualifier prefix
switch {
case strings.HasPrefix(mechanism, "+"):
qualifier = SPFPass
mechanism = mechanism[1:]
case strings.HasPrefix(mechanism, "-"):
qualifier = SPFFail
mechanism = mechanism[1:]
case strings.HasPrefix(mechanism, "~"):
qualifier = SPFSoftFail
mechanism = mechanism[1:]
case strings.HasPrefix(mechanism, "?"):
qualifier = SPFNeutral
mechanism = mechanism[1:]
}
matched, mechResult := evaluateMechanism(clientIP, domain, mechanism, ctx, explanation)
// Handle mechanism-specific error propagation
if mechResult != "" {
return SPFResult(mechResult), fmt.Sprintf("mechanism %s: %s", term, mechResult)
}
if matched {
// If fail result, fetch exp= explanation
if qualifier == SPFFail && expModifier != "" && *explanation == "" {
fetchExplanation(expModifier, ctx, explanation)
}
return qualifier, fmt.Sprintf("matched mechanism: %s", term)
}
}
// RFC 7208 §6.1: redirect MUST only be evaluated after all mechanisms fail to match
if redirectDomain != "" {
redirectDomain = expandMacros(redirectDomain, ctx, false)
return checkSPFDomainWithContext(clientIP, redirectDomain, depth+1, ctx, explanation)
}
if depth == 0 {
return SPFNeutral, "no mechanism matched"
}
return SPFNeutral, fmt.Sprintf("no match in %s", domain)
}
// evaluateMechanism evaluates a single SPF mechanism.
// Returns (matched bool, errorResult string). errorResult is non-empty for temperror/permerror.
func evaluateMechanism(clientIP net.IP, domain, mechanism string, ctx *spfContext, explanation *string) (bool, string) {
switch {
case mechanism == "all":
return true, ""
case mechanism == "a" || strings.HasPrefix(mechanism, "a:") || strings.HasPrefix(mechanism, "a/"):
checkDomain, cidr4, cidr6 := parseAMXMechanism(mechanism, "a", domain, ctx)
return matchAWithCIDR(clientIP, checkDomain, cidr4, cidr6, ctx), ""
case mechanism == "mx" || strings.HasPrefix(mechanism, "mx:") || strings.HasPrefix(mechanism, "mx/"):
checkDomain, cidr4, cidr6 := parseAMXMechanism(mechanism, "mx", domain, ctx)
return matchMXWithCIDR(clientIP, checkDomain, cidr4, cidr6, ctx), ""
case strings.HasPrefix(mechanism, "ip4:"):
cidr := mechanism[4:]
return matchIP(clientIP, cidr), ""
case strings.HasPrefix(mechanism, "ip6:"):
cidr := mechanism[4:]
return matchIP(clientIP, cidr), ""
case strings.HasPrefix(mechanism, "exists:"):
queryDomain := expandMacros(mechanism[7:], ctx, false)
return matchExists(queryDomain, ctx), ""
case strings.HasPrefix(mechanism, "include:"):
includeDomain := expandMacros(mechanism[8:], ctx, false)
result, _ := checkSPFDomainWithContext(clientIP, includeDomain, 1, ctx, explanation)
// RFC 7208 §5.2: include error semantics
switch result {
case SPFPass:
return true, ""
case SPFTempError:
return false, "temperror" // Propagate temperror
case SPFPermError, SPFNone:
return false, "permerror" // Propagate permerror/none as permerror
default:
// fail, softfail, neutral → no match, continue
return false, ""
}
case mechanism == "ptr" || strings.HasPrefix(mechanism, "ptr:"):
// ptr mechanism (deprecated but required for compatibility)
checkDomain := domain
if strings.HasPrefix(mechanism, "ptr:") {
checkDomain = expandMacros(mechanism[4:], ctx, false)
}
return matchPTR(clientIP, checkDomain, ctx), ""
}
return false, ""
}
// parseAMXMechanism parses a or mx mechanism with optional domain and CIDR.
// Format: a, a:domain, a/cidr4, a:domain/cidr4, a/cidr4//cidr6, a:domain/cidr4//cidr6
func parseAMXMechanism(mechanism, prefix, defaultDomain string, ctx *spfContext) (domain string, cidr4, cidr6 int) {
domain = defaultDomain
cidr4, cidr6 = 32, 128 // defaults
// Remove prefix
rest := mechanism
if mechanism == prefix {
return domain, cidr4, cidr6
}
if strings.HasPrefix(mechanism, prefix+":") {
rest = mechanism[len(prefix)+1:]
} else if strings.HasPrefix(mechanism, prefix+"/") {
rest = mechanism[len(prefix):]
} else {
return domain, cidr4, cidr6
}
// Parse dual CIDR (//cidr6)
if idx := strings.Index(rest, "//"); idx >= 0 {
if c6, err := strconv.Atoi(rest[idx+2:]); err == nil {
cidr6 = c6
}
rest = rest[:idx]
}
// Parse CIDR4 and domain
if idx := strings.LastIndex(rest, "/"); idx >= 0 {
if c4, err := strconv.Atoi(rest[idx+1:]); err == nil {
cidr4 = c4
}
rest = rest[:idx]
}
if rest != "" {
domain = expandMacros(rest, ctx, false)
}
return domain, cidr4, cidr6
}
// fetchExplanation fetches the exp= explanation text.
// RFC 7208 §6.2: exp= points to a TXT record containing plain explanation text (not SPF record).
func fetchExplanation(expDomain string, ctx *spfContext, explanation *string) {
if ctx.lookups >= 10 {
return
}
ctx.lookups++
expDomain = expandMacros(expDomain, ctx, false)
if txtRecords, err := net.LookupTXT(expDomain); err == nil && len(txtRecords) > 0 {
// Concatenate all TXT record strings and expand macros
*explanation = expandMacros(strings.Join(txtRecords, ""), ctx, false)
}
}
// expandMacros implements RFC 7208 §7 macro expansion.
// urlEncode controls whether to URL-encode the result (for uppercase macros).
func expandMacros(input string, ctx *spfContext, urlEncode bool) string {
var result strings.Builder
i := 0
for i < len(input) {
if input[i] == '%' && i+1 < len(input) {
if input[i+1] == '%' {
result.WriteByte('%')
i += 2
continue
}
if input[i+1] == '_' {
result.WriteByte(' ')
i += 2
continue
}
if input[i+1] == '-' {
result.WriteString("%20")
i += 2
continue
}
if input[i+1] == '{' {
// Find closing brace
end := strings.Index(input[i:], "}")
if end == -1 {
result.WriteByte(input[i])
i++
continue
}
macro := input[i+2 : i+end]
expanded := expandSingleMacro(macro, ctx)
result.WriteString(expanded)
i += end + 1
continue
}
}
result.WriteByte(input[i])
i++
}
return result.String()
}
// expandSingleMacro expands a single macro like "i", "ir", "d2", etc.
func expandSingleMacro(macro string, ctx *spfContext) string {
if len(macro) == 0 {
return ""
}
// Parse macro letter
letter := macro[0]
rest := macro[1:]
shouldURLEncode := letter >= 'A' && letter <= 'Z'
letter = byte(strings.ToLower(string(letter))[0])
// Get base value
var value string
switch letter {
case 's': // sender (localpart@domain)
value = ctx.localPart + "@" + ctx.domain
case 'l': // local-part
value = ctx.localPart
case 'o': // domain (same as d for mail-from)
value = ctx.domain
case 'd': // domain
value = ctx.domain
case 'i': // IP address (dotted for IPv4, colon-expanded for IPv6)
value = expandIP(ctx.senderIP)
case 'p': // validated domain name of IP (deprecated, use "unknown")
value = "unknown"
case 'v': // "in-addr" for IPv4, "ip6" for IPv6
if strings.Contains(ctx.senderIP, ":") {
value = "ip6"
} else {
value = "in-addr"
}
case 'h': // HELO/EHLO domain
value = ctx.helo
// exp-only macros (RFC 7208 §7.2)
case 'c': // SMTP client IP (readable format)
value = ctx.senderIP
case 'r': // domain name of host performing the check
value = ctx.receiverDomain
case 't': // current timestamp (seconds since epoch)
value = strconv.FormatInt(ctx.timestamp, 10)
default:
return "%" + "{" + macro + "}"
}
// Parse transformers: digit, 'r', and delimiter (RFC 7208 §7.1)
reverse := false
numParts := 0
delimiter := "." // default delimiter
for len(rest) > 0 {
if rest[0] >= '0' && rest[0] <= '9' {
// Parse number
j := 0
for j < len(rest) && rest[j] >= '0' && rest[j] <= '9' {
j++
}
numParts, _ = strconv.Atoi(rest[:j])
rest = rest[j:]
} else if rest[0] == 'r' || rest[0] == 'R' {
reverse = true
rest = rest[1:]
} else {
// Remaining characters are delimiter specification (RFC 7208 §7.1)
// Valid delimiters: . - + , / _ =
delimiter = ""
for _, c := range rest {
if strings.ContainsRune(".-+,/_=", c) {
delimiter += string(c)
}
}
if delimiter == "" {
delimiter = "."
}
break
}
}
// Apply transformers - split by any char in delimiter set
var parts []string
if len(delimiter) == 1 {
parts = strings.Split(value, delimiter)
} else {
// Multiple delimiters: split by any of them
parts = splitByAny(value, delimiter)
}
if reverse {
// Reverse the parts
for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 {
parts[i], parts[j] = parts[j], parts[i]
}
}
if numParts > 0 && numParts < len(parts) {
// Take rightmost N parts
parts = parts[len(parts)-numParts:]
}
value = strings.Join(parts, ".")
if shouldURLEncode {
value = url.QueryEscape(value)
}
return value
}
// splitByAny splits a string by any character in the delimiters string.
func splitByAny(s, delimiters string) []string {
if len(delimiters) == 0 {
return []string{s}
}
return strings.FieldsFunc(s, func(r rune) bool {
return strings.ContainsRune(delimiters, r)
})
}
// expandIP expands an IP address for macro use.
// IPv4: dotted quad. IPv6: expanded with dots between each hex digit.
func expandIP(ip string) string {
parsed := net.ParseIP(ip)
if parsed == nil {
return ip
}
if v4 := parsed.To4(); v4 != nil {
return ip
}
// IPv6: expand to 32 hex digits separated by dots
var parts []string
for _, b := range parsed.To16() {
parts = append(parts, fmt.Sprintf("%x", b>>4))
parts = append(parts, fmt.Sprintf("%x", b&0xf))
}
return strings.Join(parts, ".")
}
// matchAWithCIDR checks if clientIP matches A/AAAA records for domain with CIDR prefix.
func matchAWithCIDR(clientIP net.IP, domain string, cidr4, cidr6 int, ctx *spfContext) bool {
if ctx.lookups >= 10 {
return false
}
ctx.lookups++
addrs, err := net.LookupHost(domain)
if err != nil {
// RFC 7208 §4.6.4: Track void lookups (NXDOMAIN or empty)
ctx.voidLookups++
if ctx.voidLookups > 2 {
return false // SHOULD limit void lookups to 2
}
return false
}
if len(addrs) == 0 {
ctx.voidLookups++
}
for _, addr := range addrs {
ip := net.ParseIP(addr)
if ip == nil {
continue
}
cidr := cidr6
if ip.To4() != nil {
cidr = cidr4
}
if matchIPWithPrefix(clientIP, ip, cidr) {
return true
}
}
return false
}
// matchMXWithCIDR checks if clientIP matches any MX host's A/AAAA records with CIDR.
// RFC 7208 §4.6.4: Limits MX records processed to 10.
func matchMXWithCIDR(clientIP net.IP, domain string, cidr4, cidr6 int, ctx *spfContext) bool {
if ctx.lookups >= 10 {
return false
}
ctx.lookups++
mxRecords, err := net.LookupMX(domain)
if err != nil {
ctx.voidLookups++
return false
}
if len(mxRecords) == 0 {
ctx.voidLookups++
return false
}
// RFC 7208 §4.6.4: SHOULD limit MX records to 10
maxMX := 10
if len(mxRecords) > maxMX {
mxRecords = mxRecords[:maxMX]
}
for _, mx := range mxRecords {
host := strings.TrimSuffix(mx.Host, ".")
// A lookup for each MX host counts against limit
if matchAWithCIDR(clientIP, host, cidr4, cidr6, ctx) {
return true
}
}
return false
}
// matchIPWithPrefix checks if two IPs are in the same prefix.
func matchIPWithPrefix(clientIP, recordIP net.IP, prefixLen int) bool {
// Normalize to same form
client4 := clientIP.To4()
record4 := recordIP.To4()
if client4 != nil && record4 != nil {
// Both IPv4
if prefixLen > 32 {
prefixLen = 32
}
cidr := fmt.Sprintf("%s/%d", record4.String(), prefixLen)
_, network, err := net.ParseCIDR(cidr)
if err != nil {
return false
}
return network.Contains(clientIP)
}
if client4 == nil && record4 == nil {
// Both IPv6
if prefixLen > 128 {
prefixLen = 128
}
cidr := fmt.Sprintf("%s/%d", recordIP.String(), prefixLen)
_, network, err := net.ParseCIDR(cidr)
if err != nil {
return false
}
return network.Contains(clientIP)
}
return false
}
// matchExists checks if any A record exists for the domain.
func matchExists(queryDomain string, ctx *spfContext) bool {
if ctx.lookups >= 10 {
return false
}
ctx.lookups++
addrs, err := net.LookupHost(queryDomain)
if err != nil {
ctx.voidLookups++
return false
}
if len(addrs) == 0 {
ctx.voidLookups++
return false
}
return true
}
// matchPTR implements the ptr mechanism (deprecated but required for compatibility).
// RFC 7208 §5.5: Check if any PTR record for clientIP resolves to a name under checkDomain.
// Limits PTR records processed to 10 per RFC 7208 §4.6.4.
func matchPTR(clientIP net.IP, checkDomain string, ctx *spfContext) bool {
if ctx.lookups >= 10 {
return false
}
ctx.lookups++
// Get PTR records for the IP
names, err := net.LookupAddr(clientIP.String())
if err != nil {
ctx.voidLookups++
return false
}
if len(names) == 0 {
ctx.voidLookups++
return false
}
// RFC 7208 §5.5: SHOULD limit PTR records to 10
maxPTR := 10
if len(names) > maxPTR {
names = names[:maxPTR]
}
checkDomain = strings.ToLower(strings.TrimSuffix(checkDomain, "."))
for _, name := range names {
name = strings.ToLower(strings.TrimSuffix(name, "."))
// Check if name ends with checkDomain or equals checkDomain
if name == checkDomain || strings.HasSuffix(name, "."+checkDomain) {
// Validate: the name must resolve back to the client IP
if ctx.lookups >= 10 {
return false
}
ctx.lookups++
addrs, err := net.LookupHost(name)
if err != nil {
ctx.voidLookups++
continue
}
for _, addr := range addrs {
if net.ParseIP(addr).Equal(clientIP) {
return true
}
}
}
}
return false
}
// matchIP checks if clientIP is within the given CIDR or matches the IP.
func matchIP(clientIP net.IP, cidr string) bool {
// If no prefix length, add default
if !strings.Contains(cidr, "/") {
if clientIP.To4() != nil {
cidr += "/32"
} else {
cidr += "/128"
}
}
_, network, err := net.ParseCIDR(cidr)
if err != nil {
return false
}
return network.Contains(clientIP)
}