-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzone.go
259 lines (210 loc) · 6.5 KB
/
zone.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
// @license
// Copyright (C) 2021 Dinko Korunic
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"context"
"flag"
"fmt"
"net"
"net/netip"
"os"
"strings"
"sync"
"github.com/miekg/dns"
"github.com/monoidic/cidranger"
)
const (
wildcard = "*"
fileZoneSeparator = "="
)
// processRemoteZone is calling zoneTransfer() for AXFR and processRecords() for handling each valid RR.
func processRemoteZone(zone string, server string, doCIDR bool, ranger cidranger.Ranger, hosts chan<- HostEntry) {
if *verbose {
fmt.Fprintf(os.Stderr, "Info: doing AXFR for zone %q / server %q\n", zone, server)
}
zoneRecords := zoneTransfer(zone, server)
processRecords(zone, doCIDR, ranger, hosts, zoneRecords)
}
// processLocalZone is calling zoneParser() for local zone parse and processRecords() for handling valid RR.
func processLocalZone(zone string, doCIDR bool, ranger cidranger.Ranger, hosts chan<- HostEntry) {
var domain string
if strings.Contains(zone, fileZoneSeparator) {
t := strings.Split(zone, fileZoneSeparator)
if len(t) == 2 {
zone = t[0] // filename
domain = t[1] // domain
// make sure domain always ends with dot
if !strings.HasSuffix(domain, endingDot) {
domain = strings.Join([]string{domain, endingDot}, "")
}
} else {
fmt.Fprintf(os.Stderr, "Error: invalid file=domain option format: %q\n", zone)
flag.Usage()
}
}
if *verbose {
fmt.Fprintf(os.Stderr, "Info: loading and parsing zone %q / domain %q\n", zone, domain)
}
zoneRecords := zoneParser(zone, domain)
if len(zoneRecords) == 0 && domain == "" {
fmt.Fprintf(os.Stderr, "Error: no detected records in %q file. Try next time with \"%v=domain\"\n",
zone, zone)
}
processRecords(zone, doCIDR, ranger, hosts, zoneRecords)
}
// processRecords is processing each RR and calling processHost() for each valid RR.
func processRecords(zone string, doCIDR bool, ranger cidranger.Ranger, hosts chan<- HostEntry,
zoneRecords []dns.RR,
) {
var wg sync.WaitGroup
var r net.Resolver
if *resolverAddress != "" {
// custom DNS resolver
r = net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, _ string) (net.Conn, error) {
d := net.Dialer{}
return d.DialContext(ctx, network, *resolverAddress)
},
}
} else {
r = net.Resolver{PreferGo: true}
}
// process each RR
for _, rr := range zoneRecords {
switch t := rr.(type) {
case *dns.A:
wg.Add(1)
go func() {
defer wg.Done()
// ignore wildcards if ignoreStar is used
if *ignoreStar && strings.Contains(t.Hdr.Name, wildcard) {
return
}
ipAddr, ok := unmapAddrFromSlice(t.A)
if !ok {
return
}
// if CIDR matching is true, check if IP is whitelisted
if doCIDR && ranger != nil {
if c, _ := ranger.Contains(ipAddr); !c {
return
}
}
processHost(t.Hdr.Name, zone, ipAddr, hosts)
}()
case *dns.AAAA:
wg.Add(1)
go func() {
defer wg.Done()
// ignore wildcards if ignoreStar is used
if *ignoreStar && strings.Contains(t.Hdr.Name, wildcard) {
return
}
ipAddr6, ok := unmapAddrFromSlice(t.AAAA)
if !ok {
return
}
// if CIDR matching is true, check if IP is whitelisted
if doCIDR && ranger != nil {
if c, _ := ranger.Contains(ipAddr6); !c {
return
}
}
processHost(t.Hdr.Name, zone, ipAddr6, hosts)
}()
case *dns.CNAME:
wg.Add(1)
go func() {
defer wg.Done()
ctx := context.Background()
// ignore out-of-zone targets if not using greedyCNAME
if !*greedyCNAME {
cnames, err := lookup(ctx, t.Hdr.Name, dns.TypeCNAME, &r)
if err != nil {
return
}
if len(cnames) > 0 && !strings.HasSuffix(cnames[0], zone) {
return
}
}
addrs, err := lookup(ctx, t.Hdr.Name, dns.TypeA, &r)
if err != nil {
return
}
// loop through resolved array
for _, a := range addrs {
ipAddr, err := unmapParseAddr(a)
if err != nil {
continue
}
// if CIDR matching is true, check if IP is whitelisted
if doCIDR && ranger != nil {
if c, _ := ranger.Contains(ipAddr); !c {
continue
}
}
processHost(t.Hdr.Name, zone, ipAddr, hosts)
}
}()
// every other RR type is skipped over
default:
}
}
wg.Wait()
}
// zoneParser is parsing loading zones into memory and parsing them, returning slice of RRs.
func zoneParser(zone, domain string) []dns.RR {
var records []dns.RR
// read a whole zone into memory
z, err := os.ReadFile(zone)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: problem reading zone file: %q: %v\n", zone, err)
return records
}
// initialize RFC 1035 zone parser
zp := dns.NewZoneParser(strings.NewReader(string(z)), domain, "")
// fetch all RRs
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
if err := zp.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error: problem parsing zone %q but will try to continue: %v\n", zone, err)
continue
}
records = append(records, rr)
}
return records
}
// unmapAddrFromSlice parses 4 or 16-byte slice as IPv4 or IPv6 address and removes any IPv4-mapped IPv6 prefix.
func unmapAddrFromSlice(slice []byte) (netip.Addr, bool) {
ipAddr, ok := netip.AddrFromSlice(slice)
if !ok {
return ipAddr, false
}
return ipAddr.Unmap(), true
}
// unmapParseAddr parses string as an IP address, returning result and removes any IPv4-mapped IPv6 prefix.
func unmapParseAddr(s string) (netip.Addr, error) {
ipAddr, err := netip.ParseAddr(s)
if err != nil {
return ipAddr, err
}
return ipAddr.Unmap(), nil
}