-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmtasts.go
175 lines (151 loc) · 3.9 KB
/
mtasts.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
// The mtasts policy implements parsing, caching and checking of
// MTA-STS (RFC 8461) policies.
package mtasts
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
)
type MalformedDNSRecordError struct {
// Additional description of the error.
Desc string
}
func (e MalformedDNSRecordError) Error() string {
return fmt.Sprintf("mtasts: malformed DNS record: %s", e.Desc)
}
func readDNSRecord(raw string) (id string, err error) {
parts := strings.Split(raw, ";")
versionPresent := false
for _, part := range parts {
part = strings.TrimSpace(part)
// handle k=v;k=v;
// ^
if part == "" {
continue
}
kv := strings.Split(part, "=")
if len(kv) != 2 {
return "", MalformedDNSRecordError{Desc: "invalid record part: " + part}
}
if strings.ContainsAny(kv[0], " \t") || strings.ContainsAny(kv[1], " \t") {
return "", MalformedDNSRecordError{Desc: "whitespace is not allowed in name or value"}
}
switch kv[0] {
case "v":
if kv[1] != "STSv1" {
return "", MalformedDNSRecordError{Desc: "unsupported version: " + kv[1]}
}
versionPresent = true
case "id":
id = kv[1]
}
}
if !versionPresent {
return "", MalformedDNSRecordError{Desc: "missing version value"}
}
if id == "" {
return "", MalformedDNSRecordError{Desc: "missing id value"}
}
return
}
type MalformedPolicyError struct {
// Additional description of the error.
Desc string
}
func (e MalformedPolicyError) Error() string {
return fmt.Sprintf("mtasts: malformed policy: %s", e.Desc)
}
type Mode string
const (
ModeEnforce Mode = "enforce"
ModeTesting Mode = "testing"
ModeNone Mode = "none"
)
type Policy struct {
Mode Mode
MaxAge int
MX []string
}
func readPolicy(contents io.Reader) (*Policy, error) {
scnr := bufio.NewScanner(contents)
policy := Policy{}
present := make(map[string]struct{})
for scnr.Scan() {
fieldParts := strings.Split(scnr.Text(), ":")
if len(fieldParts) != 2 {
return nil, MalformedPolicyError{Desc: "invalid field: " + scnr.Text()}
}
// Arbitrary whitespace after colon:
// sts-policy-field-delim = ":" *WSP
fieldName := fieldParts[0]
fieldValue := strings.TrimSpace(fieldParts[1])
switch fieldName {
case "version":
if fieldValue != "STSv1" {
return nil, MalformedPolicyError{Desc: "unsupported policy version: " + fieldValue}
}
case "mode":
switch Mode(fieldValue) {
case ModeEnforce, ModeTesting, ModeNone:
policy.Mode = Mode(fieldValue)
default:
return nil, MalformedPolicyError{Desc: "invalid mode value: " + fieldValue}
}
case "max_age":
var err error
policy.MaxAge, err = strconv.Atoi(fieldValue)
if err != nil {
return nil, MalformedPolicyError{Desc: "invalid max_age value: " + err.Error()}
}
case "mx":
policy.MX = append(policy.MX, fieldValue)
}
present[fieldName] = struct{}{}
}
if err := scnr.Err(); err != nil {
return nil, err
}
if _, ok := present["version"]; !ok {
return nil, MalformedPolicyError{Desc: "version field required"}
}
if _, ok := present["mode"]; !ok {
return nil, MalformedPolicyError{Desc: "mode field required"}
}
if _, ok := present["max_age"]; !ok {
return nil, MalformedPolicyError{Desc: "max_age field required"}
}
if policy.Mode != ModeNone && len(policy.MX) == 0 {
return nil, MalformedPolicyError{Desc: "at least one mx field required when mode is not none"}
}
return &policy, nil
}
func (p Policy) Match(mx string) bool {
normMX, err := forLookup(mx)
if err != nil {
return false
}
for _, pattern := range p.MX {
normPattern, err := forLookup(pattern)
if err != nil {
continue
}
// Direct comparison is valid since both values are prepared using
// dns.ForLookup.
if strings.HasPrefix(normPattern, "*.") {
firstDot := strings.Index(mx, ".")
if firstDot == -1 {
continue
}
if normMX[firstDot:] == normPattern[1:] {
return true
}
continue
}
if normMX == normPattern {
return true
}
}
return false
}