-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmacro.go
153 lines (134 loc) · 2.92 KB
/
macro.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
package spf
import (
"bytes"
"net"
"regexp"
"strconv"
"strings"
)
var (
spfDelimiters = []byte(".-+,/_=")
macroSyntax = regexp.MustCompile("%[^{_\\-%}]")
macroMatch = regexp.MustCompile("%{[^}]+}")
macroReplacer = strings.NewReplacer("%%", "%", "%_", " ", "%-", "%20")
)
type macro struct {
ip net.IP
domain string
sender string
helo string
}
func (c check) macro(m string, ip net.IP, domain, sender, helo string) (string, Result) {
if len(macroSyntax.FindAllString(m, -1)) != 0 {
return m, PermError
}
m = macroReplacer.Replace(m)
mac := macro{
ip: ip,
domain: domain,
sender: sender,
helo: helo,
}
return macroMatch.ReplaceAllStringFunc(m, mac.eval), None
}
func (m macro) eval(s string) string {
s = strings.TrimSuffix(strings.TrimPrefix(s, "%{"), "}")
sep := ""
n := 0
reverse := false
var t string
for i, b := range s {
switch {
case i == 0:
switch b {
case 'i':
t = m.ip.String()
case 's':
t = m.sender
case 'l':
l, _ := senderParts(m.sender)
t = l
case 'o':
_, o := senderParts(m.sender)
t = o
case 'd':
t = m.domain
case 'p':
// TODO: p
case 'v':
if m.ip.To4() != nil {
t = "in-addr"
} else {
t = "ip6"
}
case 'h':
t = m.helo
}
case b >= '0' && b <= '9':
n, _ = strconv.Atoi(string(b))
case b == 'r':
reverse = true
case bytes.ContainsRune(spfDelimiters, b):
sep = string(b)
}
}
if sep != "" || reverse || n != 0 {
if sep == "" {
sep = "."
}
p := strings.Split(t, sep)
if reverse {
for i := len(p)/2 - 1; i >= 0; i-- {
opp := len(p) - 1 - i
p[i], p[opp] = p[opp], p[i]
}
}
t = ""
c := 0
for i := len(p) - 1; i >= 0; i-- {
if t != "" {
t = "." + t
}
t = p[i] + t
c++
if n != 0 && c == n {
break
}
}
}
return t
}
func senderParts(s string) (string, string) {
parts := strings.SplitN(s, "@", 2)
switch len(parts) {
case 0:
return "", ""
case 1:
return parts[0], ""
default:
return parts[0], parts[1]
}
}
/*
Some special cases:
o A literal "%" is expressed by "%%".
o "%_" expands to a single " " space.
o "%-" expands to a URL-encoded space, viz., "%20".
7.2. Macro Definitions
The following macro letters are expanded in term arguments:
s = <sender>
l = local-part of <sender>
o = domain of <sender>
d = <domain>
i = <ip>
p = the validated domain name of <ip> (do not use)
v = the string "in-addr" if <ip> is ipv4, or "ip6" if <ip> is ipv6
h = HELO/EHLO domain
Kitterman Standards Track [Page 29]
RFC 7208 Sender Policy Framework (SPF) April 2014
<domain>, <sender>, and <ip> are defined in Section 4.1.
The following macro letters are allowed only in "exp" text:
c = SMTP client IP (easily readable format)
r = domain name of host performing the check
t = current timestamp
*/