forked from nocproject/noc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdnszone.py
590 lines (562 loc) · 19.4 KB
/
dnszone.py
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
# ---------------------------------------------------------------------
# Import DNS Zone
# ---------------------------------------------------------------------
# Copyright (C) 2007-2021 The NOC Project
# See LICENSE for details
# ---------------------------------------------------------------------
# Python modules
import argparse
import re
import subprocess
from itertools import zip_longest
# NOC modules
from noc.core.management.base import BaseCommand, CommandError
from noc.core.mongo.connection import connect
from noc.dns.models.dnszone import DNSZone
from noc.dns.models.dnszonerecord import DNSZoneRecord
from noc.dns.models.dnszoneprofile import DNSZoneProfile
from noc.ip.models.vrf import VRF
from noc.ip.models.addressprofile import AddressProfile
from noc.ip.models.address import Address
from noc.core.validators import is_int
from noc.core.dns.rr import RR
from noc.config import config
from noc.core.text import split_alnum
from noc.core.comp import smart_text, smart_bytes
class Command(BaseCommand):
help = "DNS zone manipulation tool"
# Time multipliers
TIMES = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800}
def add_arguments(self, parser):
subparsers = parser.add_subparsers(dest="cmd")
#
import_parser = subparsers.add_parser("import")
import_parser.add_argument(
"--dry-run", dest="dry_run", action="store_true", help="Test only. Do not save records"
),
import_parser.add_argument(
"-c", "--clean", dest="clean", action="store_true", help="Clean up zone before store"
),
import_parser.add_argument(
"--zone-profile", dest="zone_profile", action="store", help="Set Zone Profile"
),
import_parser.add_argument(
"--address-profile", dest="address_profile", action="store", help="Set Address Profile"
),
import_parser.add_argument(
"-f",
"--force",
dest="force",
action="store_true",
help="Forcefully update FQDN for A records",
),
import_parser.add_argument("paths", nargs=argparse.REMAINDER, help="Path to zone files")
import_parser.add_argument(
"--axfr",
action="store_true",
help="Upload list of allocated IP addresses from existing DNS servers via AXFR request",
)
import_parser.add_argument(
"--nameserver",
dest="nameserver",
action="store",
help="Name server IP address. NS must have zone transfer enabled for NOC host",
),
import_parser.add_argument(
"--transfer-zone",
dest="transfer_zone",
action="store",
help="DNS Zone name to transfer",
),
import_parser.add_argument(
"--source", dest="source", action="store", help="Source address to issue zone transfer"
)
self.print((import_parser.print_help()))
RR_TYPES = [
"A",
"AAAA",
"AFSDB",
"AXFR",
"CERT",
"CNAME",
"DHCID",
"DLV",
"DNAME",
"DNSKEY",
"DS",
"HIP",
"IPSECKEY",
"IXFR",
"KEY",
"LOC",
"MX",
"NAPTR",
"NS",
"NSEC",
"NSEC3",
"NSEC3PARAM",
"OPT",
"PTR",
"RRSIG",
"SIG",
"SPF",
"SRV",
"SSHFP",
"TA",
"TKEY",
"TSIG",
"TXT",
]
def handle(self, cmd, *args, **options):
connect()
return getattr(self, "handle_%s" % cmd)(*args, **options)
def handle_import(
self,
paths,
axfr=False,
force=False,
clean=False,
dry_run=False,
zone_profile=None,
address_profile=None,
source=None,
transfer_zone=None,
nameserver=None,
):
if not axfr and not zone_profile:
self.die("--zone-profile is not set")
if not address_profile:
self.die("--address-profile is not set")
zp = DNSZoneProfile.get_by_name(zone_profile)
if not axfr and not zp:
self.die("Invalid zone profile '%s'" % zone_profile)
ap = AddressProfile.get_by_name(address_profile)
if not ap:
self.die("Invalid address profile '%s'" % address_profile)
if paths:
for path in paths:
self.import_zone(
path=path,
force=force,
clean=clean,
dry_run=dry_run,
zone_profile=zp,
address_profile=ap,
)
if axfr:
if not source:
self.die("--source is not set")
if not transfer_zone:
self.die("--transfer_zone is not set")
if not nameserver:
self.die("--nameserver is not set")
self.import_zone(
axfr=axfr,
force=force,
clean=clean,
dry_run=dry_run,
zone_profile=zp,
address_profile=ap,
transfer_zone=transfer_zone,
nameserver=nameserver,
source_address=source,
)
def load_axfr(self, nameserver, transfer_zone, source_address):
opts = []
opts += ["-b", source_address]
pipe = subprocess.Popen(
[config.path.dig] + opts + ["axfr", "@%s" % nameserver, transfer_zone],
shell=False,
stdout=subprocess.PIPE,
).stdout
data = pipe.read()
pipe.close()
return smart_text(smart_bytes(data)).split("\n")
def dns_zone(self, zone, zone_profile, dry_run=False, clean=False):
z = DNSZone.get_by_name(zone)
if z:
self.print("Using existing zone '%s'" % zone)
else:
self.print("Creating zone '%s'" % zone)
z = DNSZone(name=zone, profile=zone_profile)
clean = False # Nothing to clean
if z.profile.id != zone_profile.id:
self.print("Setting profile to '%s'" % zone_profile.name)
z.profile = zone_profile
# Apply changes
if dry_run:
z.clean() # Set type
else:
z.save()
# Clean zone when necessary
if clean:
self.print("Cleaning zone")
for rr in DNSZoneRecord.objects.filter(zone=z):
self.print("Removing %s %s" % (rr.type, rr.name))
if not dry_run:
rr.delete()
return z
def import_zone(
self,
path=None,
axfr=False,
zone_profile=None,
address_profile=None,
transfer_zone=None,
nameserver=None,
source_address=None,
dry_run=False,
force=False,
clean=False,
):
self.print("Loading zone file '%s'" % path)
self.print("Parsing zone file using BIND parser")
if path:
with open(path) as f:
rrs = self.iter_bind_zone_rr(f)
try:
soa = next(rrs)
except StopIteration:
raise CommandError("Unable to parse zone file from %s" % path)
zone = self.from_idna(soa.zone)
z = self.dns_zone(zone, zone_profile, dry_run, clean)
# Populate zone
vrf = VRF.get_global()
zz = zone + "."
lz = len(zz)
if z.is_forward:
zp = None
elif z.is_reverse_ipv4:
# Calculate prefix for reverse zone
zp = ".".join(reversed(zone[:-13].split("."))) + "."
elif z.is_reverse_ipv6:
raise CommandError("IPv6 reverse import is not implemented")
else:
raise CommandError("Unknown zone type")
for rr in rrs:
name = rr.name
if name.endswith(zz):
name = name[:-lz]
if name.endswith("."):
name = name[:-1]
# rr = None
# Skip zone NS
if rr.type == "NS" and not name:
continue
if rr.type in ("A", "AAAA"):
self.create_address(
zone,
vrf,
rr.rdata,
"%s.%s" % (name, zone) if name else zone,
address_profile,
dry_run=dry_run,
force=force,
)
elif rr.type == "PTR":
if "." in name:
address = zp + ".".join(reversed(name.split(".")))
else:
address = zp + name
self.create_address(
zone,
vrf,
address,
rr.rdata,
address_profile,
dry_run=dry_run,
force=force,
)
else:
zrr = DNSZoneRecord(
zone=z,
name=name,
type=rr.type,
ttl=rr.ttl,
priority=rr.priority,
content=rr.rdata,
)
self.print("Creating %s %s" % (rr.type, rr.name))
if not dry_run:
zrr.save()
if axfr:
data = self.load_axfr(nameserver, transfer_zone, source_address)
zone = self.from_idna(transfer_zone)
z = self.dns_zone(zone, zone_profile, dry_run, clean)
# Populate zone
vrf = VRF.get_global()
zz = zone + "."
lz = len(zz)
if z.is_forward:
zp = None
elif z.is_reverse_ipv4:
# Calculate prefix for reverse zone
zp = ".".join(reversed(zone[:-13].split("."))) + "."
elif z.is_reverse_ipv6:
raise CommandError("IPv6 reverse import is not implemented")
else:
raise CommandError("Unknown zone type")
for row in data:
row = row.strip()
if row == "" or row.startswith(";"):
continue
row = row.split()
if len(row) != 5 or row[2] != "IN" or row[3] not in ("A", "AAAA", "PTR"):
continue
if row[3] in ("A", "AAAA"):
name = row[0]
if name.endswith(zz):
name = name[:-lz]
if name.endswith("."):
name = name[:-1]
self.create_address(
zone,
vrf,
row[4],
"%s.%s" % (name, zone) if name else zone,
address_profile,
dry_run=dry_run,
force=force,
)
if row[3] == "PTR":
name = row[4]
if name.endswith(zz):
name = name[:-lz]
if name.endswith("."):
name = name[:-1]
# @todo: IPv6
if "." in row[0]:
address = ".".join(reversed(row[0].split(".")[:-3]))
else:
address = zp + name
fqdn = row[4]
if fqdn.endswith("."):
fqdn = fqdn[:-1]
self.create_address(
zone, vrf, address, fqdn, address_profile, dry_run=dry_run, force=force
)
def create_address(self, zone, vrf, address, fqdn, address_profile, dry_run=False, force=False):
"""
Create IPAM record
"""
afi = "6" if ":" in address else "4"
a = Address.objects.filter(vrf=vrf, afi=afi, address=address).first()
if a:
if force:
if a.fqdn != fqdn:
self.print("Updating FQDN %s (%s)" % (a.address, a.fqdn))
a.fqdn = fqdn
a.name = fqdn
if not dry_run:
a.save()
else:
self.print(
"Address %s (%s) is already exists in IPAM, ignoring" % (a.address, a.fqdn)
)
else:
# Not found
a = Address(
vrf=vrf,
afi=afi,
address=address,
profile=address_profile,
fqdn=fqdn,
name=fqdn,
description="Imported from %s zone" % zone,
)
self.print("Creating address %s (%s)" % (a.address, a.fqdn))
if not dry_run:
a.save()
rx_q = re.compile('("[^"]*")')
@classmethod
def iter_tokenize(cls, s):
start = 0
for match in cls.rx_q.finditer(s):
if start < match.start():
yield s[start : match.start()]
yield match.group(0)
start = match.end()
if start < len(s) - 1:
yield s[start:]
@classmethod
def iter_tabify(cls, iter):
"""
Replace tabs to spaces in non-quoted parts
:param iter:
:return:
"""
for item in iter:
if cls.has_unquoted(item, "\t"):
yield item.replace("\t", " ")
else:
yield item
@classmethod
def iter_strip_comments(cls, iter):
"""
Cut comments to end of line
:param iter:
:return:
"""
for item in iter:
if cls.has_unquoted(item, ";"):
p = item.split(";", 1)[0].rstrip()
if p:
yield p
break
else:
yield item
@staticmethod
def is_quoted(item):
return item.startswith('"')
@staticmethod
def has_unquoted(item, v):
return not item.startswith('"') and v in item
rx_mq = re.compile(r'"\s+"')
@classmethod
def merge_mq(cls, value):
return cls.rx_mq.sub("", value)
@classmethod
def iter_zone_lines(cls, f):
"""
Yields zone data line by line
:param f: File object
:return:
"""
enclosed_line = []
for line in f:
collected = []
for item in cls.iter_strip_comments(cls.iter_tabify(cls.iter_tokenize(line))):
if enclosed_line:
if cls.has_unquoted(item, ")"):
# Closing )
p = item.split(")", 1)
enclosed_line += [p[0] + " "]
if len(p) > 1:
enclosed_line += [p[1] + " "]
collected += enclosed_line
enclosed_line = []
else:
enclosed_line += [item]
else:
if cls.has_unquoted(item, "("):
# Starting (
p = item.split("(", 1)
enclosed_line += [p[0] + " "]
if len(p) > 1:
enclosed_line += [p[1] + " "]
else:
# Plain item
collected += [item]
if collected and not enclosed_line:
line = "".join(collected)
if line.strip():
# Not empty
yield line.rstrip()
rx_soa = re.compile(
r"^(?P<zone>\S+)\s+(?:IN\s+)?SOA\s+(\S+)\s+(\S+)\s+"
r"(\d+)\s+(\d+[smhdw]?)+\s+(\d+[smhdw]?)+\s+(\d+[smhdw]?)+\s+(\d+[smhdw]?)+",
re.IGNORECASE,
)
def iter_bind_zone_rr(self, data):
"""
Parse bind-style zone and yields RRs
:param data: Zone text
"""
# ttl = None
zone = None
ttl = None
seen_soa = False
for line in self.iter_zone_lines(data):
if line.startswith("$TTL "):
ttl = self.parse_ttl(line[5:])
continue
if line.startswith("$ORIGIN "):
zone = line[8:].strip()
continue
if not seen_soa:
# Wait for SOA
match = self.rx_soa.match(line)
if match:
z = match.group("zone")
if z and z != "@":
if z.endswith("."):
zone = z
else:
zone = "%s.%s" % (z, zone)
yield RR(
zone=zone.strip("."),
name="",
type="SOA",
rdata=" ".join(match.groups()[-7:]),
ttl=ttl,
)
seen_soa = True
else:
parts = line.split()
if parts[0] == "IN" or parts[0] in self.RR_TYPES:
# missed name
parts = [""] + parts
# Record ttl
if is_int(parts[1]):
rttl = int(parts.pop(1))
else:
rttl = None
if parts[1] == "IN":
# Remove IN
parts = [parts[0]] + parts[2:]
# Normalize name
name = parts[0]
if name == "@" or not name:
name = zone
elif not name.endswith("."):
name = name + "." + zone
# Process value
t = parts[1]
v = parts[2:]
if len(v) > 1 and is_int(v[0]):
rprio = int(v[0])
v = v[1:]
else:
rprio = None
value = " ".join(v)
if t in ("CNAME", "PTR"):
value = self.from_idna(value)
elif t == "TXT":
# Merge multiple values
value = self.merge_mq(value)
yield RR(
zone=zone,
name=self.from_idna(name),
type=t,
rdata=value,
ttl=rttl,
priority=rprio,
)
@staticmethod
def from_idna(s):
"""
Convert IDNA domain name to unicode
"""
if not s:
return
return ".".join(smart_text(x, encoding="idna") for x in s.split("."))
@classmethod
def parse_ttl(cls, line):
"""
Parse RFC2308 TTL
:param line:
:return:
"""
parts = split_alnum(line.strip())
v = 0
for t, mult in zip_longest(parts[::2], parts[1::2]):
if mult is None:
v += t
break
m = cls.TIMES.get(mult.lower())
if not m:
raise ValueError("Invalid TTL")
v += t * m
return v
if __name__ == "__main__":
Command().run()