-
Notifications
You must be signed in to change notification settings - Fork 18
/
dnsrewrite.go
314 lines (270 loc) · 7.55 KB
/
dnsrewrite.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
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
package rules
import (
"errors"
"fmt"
"net"
"strconv"
"strings"
"github.com/miekg/dns"
)
// RCode is a semantic alias for int when used as a DNS response code RCODE.
type RCode = int
// RRType is a semantic alias for uint16 when used as a DNS resource record (RR)
// type.
type RRType = uint16
// RRValue is the value of a resource record.
//
// If the coresponding RRType is either dns.TypeA or dns.TypeAAAA, the
// underlying type of RRValue is net.IP.
//
// If the RRType is dns.TypeMX, the underlying value is a non-nil *DNSMX.
//
// If the RRType is either dns.TypePTR or dns.TypeTXT, the underlying type of
// Value is string.
//
// If the RRType is either dns.TypeHTTPS or dns.TypeSVCB, the underlying value
// is a non-nil *DNSSVCB.
//
// Otherwise, currently, it is nil. New types may be added in the future.
type RRValue = interface{}
// DNSRewrite is a DNS rewrite ($dnsrewrite) rule.
type DNSRewrite struct {
// Value is the value for the record. See the RRValue documentation for
// more details.
Value RRValue
// NewCNAME is the new CNAME. If set, clients must ignore other fields,
// resolve the CNAME, and set the new A and AAAA records accordingly.
NewCNAME string
// RCode is the new DNS RCODE.
RCode RCode
// RRType is the new DNS resource record (RR) type. It is only non-zero
// if RCode is dns.RCodeSuccess.
RRType RRType
}
// loadDNSRewrite loads the $dnsrewrite modifier.
func loadDNSRewrite(s string) (rewrite *DNSRewrite, err error) {
parts := strings.SplitN(s, ";", 3)
switch len(parts) {
case 1:
return loadDNSRewriteShort(s)
case 2:
return nil, errors.New("invalid dnsrewrite: expected zero or two delimiters")
case 3:
return loadDNSRewriteNormal(parts[0], parts[1], parts[2])
default:
// TODO(a.garipov): Use panic("unreachable") instead?
return nil, fmt.Errorf("SplitN returned %d parts", len(parts))
}
}
// allUppercaseASCII returns true if s is not empty and all characters in s are
// uppercase ASCII letters.
func allUppercaseASCII(s string) (ok bool) {
if s == "" {
return false
}
for _, r := range s {
if r < 'A' || r > 'Z' {
return false
}
}
return true
}
// loadDNSRewritesShort loads the shorthand version of the $dnsrewrite modifier.
func loadDNSRewriteShort(s string) (rewrite *DNSRewrite, err error) {
if s == "" {
// Return an empty DNSRewrite, because an empty string most
// probalby means that this is a disabling allowlist case.
return &DNSRewrite{}, nil
} else if allUppercaseASCII(s) {
if s == "REFUSED" {
return &DNSRewrite{
RCode: dns.RcodeRefused,
}, nil
}
return nil, fmt.Errorf("unknown keyword: %q", s)
}
ip := net.ParseIP(s)
if ip != nil {
if ip4 := ip.To4(); ip4 != nil {
return &DNSRewrite{
RCode: dns.RcodeSuccess,
RRType: dns.TypeA,
Value: ip,
}, nil
}
return &DNSRewrite{
RCode: dns.RcodeSuccess,
RRType: dns.TypeAAAA,
Value: ip,
}, nil
}
return &DNSRewrite{
NewCNAME: s,
}, nil
}
// DNSMX is the type of RRValue values returned for MX records in DNS rewrites.
type DNSMX struct {
Exchange string
Preference uint16
}
// DNSSVCB is the type of RRValue values returned for HTTPS and SVCB records in
// dns rewrites.
//
// See https://tools.ietf.org/html/draft-ietf-dnsop-svcb-https-01.
type DNSSVCB struct {
Params map[string]string
Target string
Priority uint16
}
// dnsRewriteRRHandler is a function that parses values for specific resource
// record types.
type dnsRewriteRRHandler func(rcode RCode, rr RRType, valStr string) (dnsr *DNSRewrite, err error)
// strDNSRewriteRRHandler is a simple DNS rewrite handler that returns
// a *DNSRewrite with Value st to valStr.
func strDNSRewriteRRHandler(rcode RCode, rr RRType, valStr string) (dnsr *DNSRewrite, err error) {
return &DNSRewrite{
RCode: rcode,
RRType: rr,
Value: valStr,
}, nil
}
// svcbDNSRewriteRRHandler is a DNS rewrite handler that parses SVCB and HTTPS
// rewrites.
//
// See https://tools.ietf.org/html/draft-ietf-dnsop-svcb-https-01.
//
// TODO(a.garipov): Currently, we only support the contiguous type of
// char-string values from the RFC.
func svcbDNSRewriteRRHandler(rcode RCode, rr RRType, valStr string) (dnsr *DNSRewrite, err error) {
var name string
switch rr {
case dns.TypeHTTPS:
name = "https"
case dns.TypeSVCB:
name = "svcb"
default:
return nil, fmt.Errorf("unsupported svcb-like rr type: %d", rr)
}
fields := strings.Split(valStr, " ")
if len(fields) < 2 {
return nil, fmt.Errorf("invalid %s %q: need at least two fields", name, valStr)
}
var prio64 uint64
prio64, err = strconv.ParseUint(fields[0], 10, 16)
if err != nil {
return nil, fmt.Errorf("invalid %s priority: %w", name, err)
}
if len(fields) == 2 {
v := &DNSSVCB{
Priority: uint16(prio64),
Target: fields[1],
}
return &DNSRewrite{
RCode: rcode,
RRType: rr,
Value: v,
}, nil
}
params := make(map[string]string, len(fields)-2)
for i, pair := range fields[2:] {
kv := strings.Split(pair, "=")
if l := len(kv); l != 2 {
return nil, fmt.Errorf("invalid %s param at index %d: got %d fields", name, i, l)
}
// TODO(a.garipov): Validate for uniqueness? Validate against
// the currently specified list of params from the RFC?
params[kv[0]] = kv[1]
}
v := &DNSSVCB{
Priority: uint16(prio64),
Target: fields[1],
Params: params,
}
return &DNSRewrite{
RCode: rcode,
RRType: rr,
Value: v,
}, nil
}
// dnsRewriteRRHandlers are the supported resource record types' rewrite
// handlers.
var dnsRewriteRRHandlers = map[RRType]dnsRewriteRRHandler{
dns.TypeA: func(rcode RCode, rr RRType, valStr string) (dnsr *DNSRewrite, err error) {
ip := net.ParseIP(valStr)
if ip4 := ip.To4(); ip4 == nil {
return nil, fmt.Errorf("invalid ipv4: %q", valStr)
}
return &DNSRewrite{
RCode: rcode,
RRType: rr,
Value: ip,
}, nil
},
dns.TypeAAAA: func(rcode RCode, rr RRType, valStr string) (dnsr *DNSRewrite, err error) {
ip := net.ParseIP(valStr)
if ip == nil {
return nil, fmt.Errorf("invalid ipv6: %q", valStr)
} else if ip4 := ip.To4(); ip4 != nil {
return nil, fmt.Errorf("want ipv6, got ipv4: %q", valStr)
}
return &DNSRewrite{
RCode: rcode,
RRType: rr,
Value: ip,
}, nil
},
dns.TypeCNAME: func(rcode RCode, rr RRType, valStr string) (dnsr *DNSRewrite, err error) {
return &DNSRewrite{
NewCNAME: valStr,
}, nil
},
dns.TypeMX: func(rcode RCode, rr RRType, valStr string) (dnsr *DNSRewrite, err error) {
parts := strings.SplitN(valStr, " ", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid mx: %q", valStr)
}
var pref64 uint64
pref64, err = strconv.ParseUint(parts[0], 10, 16)
if err != nil {
return nil, fmt.Errorf("invalid mx preference: %w", err)
}
v := &DNSMX{
Exchange: parts[1],
Preference: uint16(pref64),
}
return &DNSRewrite{
RCode: rcode,
RRType: rr,
Value: v,
}, nil
},
dns.TypePTR: strDNSRewriteRRHandler,
dns.TypeTXT: strDNSRewriteRRHandler,
dns.TypeHTTPS: svcbDNSRewriteRRHandler,
dns.TypeSVCB: svcbDNSRewriteRRHandler,
}
// loadDNSRewritesNormal loads the normal version for of the $dnsrewrite
// modifier.
func loadDNSRewriteNormal(rcodeStr, rrStr, valStr string) (rewrite *DNSRewrite, err error) {
rcode, ok := dns.StringToRcode[strings.ToUpper(rcodeStr)]
if !ok {
return nil, fmt.Errorf("unknown rcode: %q", rcodeStr)
}
if rcode != dns.RcodeSuccess {
return &DNSRewrite{
RCode: rcode,
}, nil
}
rr, err := strToRRType(rrStr)
if err != nil {
return nil, err
}
handler, ok := dnsRewriteRRHandlers[rr]
if !ok {
return &DNSRewrite{
RCode: rcode,
RRType: rr,
}, nil
}
return handler(rcode, rr, valStr)
}