-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffdns.py
268 lines (214 loc) · 8.01 KB
/
diffdns.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
import os
import sys
import re
from dns import zone
from iscpy.iscpy_dns.named_importer_lib import MakeNamedDict
import argparse
from lib.validate import diff_nameservers
from lib.paths import swap_paths
import blacklist
def diff_zones(ns1, ns2, zone_file, named_path):
zones = MakeNamedDict(open(zone_file).read())
for zone_name, zone_meta in zones['orphan_zones'].iteritems():
if zone_meta['type'] != 'master':
continue
if is_bl(zone_name, zone_meta['file'], zone_meta['type']):
continue
handle_zone(ns1, ns2, zone_name, zone_meta, named_path)
def diff_zone(ns1, ns2, zone_name, zone_file, named_path):
handle_zone(ns1, ns2, zone_name, {'file': zone_file}, named_path)
def is_bl(zone_name, zone_file, zone_type):
if zone_name in blacklist.zone_name_bl:
return True
for prefix in blacklist.file_prefix_bl:
if zone_file.startswith(prefix):
return True
if zone_type == 'slave' and blacklist.ignore_slave:
return True
return False
def diff_view(ns1, ns2, view_file1, named_path1, named_path2=None,
view_file2=None):
if view_file2 and named_path2:
if diff_view_contents(named_path1, named_path2, view_file1,
view_file2):
print "-- Found differences, not continuing"
return
zones = parse_view_config_data(named_path1, view_file1)
print "Using files under {0} as the datasource.".format(named_path1)
for zone_name, zone_meta in zones.iteritems():
if is_bl(zone_name, zone_meta['file'], zone_meta['type']):
continue
handle_zone(ns1, ns2, zone_name, zone_meta, named_path1)
def diff_view_contents(named_path1, named_path2, view_file1, view_file2):
zones1 = set(parse_view_config_data(named_path1, view_file1))
zones2 = set(parse_view_config_data(named_path2, view_file2))
ldiff = zones1 - zones2
rdiff = zones2 - zones1
difference = False
if ldiff:
difference = True
msg = "zones statments that were in {0} but not in {1}:".format(
view_file1, view_file2
)
print '=' * len(msg)
print msg
print '=' * len(msg)
for lz in ldiff:
print lz
if rdiff:
difference = True
msg = "zones statments that were in {0} but not in {1}:".format(
view_file2, view_file1
)
print '=' * len(msg)
print msg
print '=' * len(msg)
for rz in rdiff:
print rz
return difference
def parse_view_config_data(named_path, view_file):
include_m = re.compile("\s+include\s+['\"](\S+)['\"]")
includes = []
with open(view_file) as fd:
for line in fd:
m = include_m.match(line)
if m:
path = m.groups()[0]
includes.append(swap_fpaths(path))
zones = {}
for conf_file in includes:
parsed = parse_config_data(named_path, conf_file)
zones.update(parsed)
return zones
def swap_fpaths(path):
for s in swap_paths:
path = path.replace(*s)
return path
def parse_config_data(named_path, filepath):
cwd = os.getcwd()
os.chdir(named_path)
zones = MakeNamedDict(open(filepath).read())
os.chdir(cwd)
return zones['orphan_zones']
def get_zone_data(zone_name, filepath, dirpath):
cwd = os.getcwd()
os.chdir(dirpath)
mzone = zone.from_file(filepath, zone_name, relativize=False)
os.chdir(cwd)
return mzone
def handle_zone(ns1, ns2, zone_name, zone_meta, ZONE_PATH):
if not zone_meta['file']:
print "No zone file for {0}".format(zone_name)
return
print "== Diffing {0}. ({1})".format(zone_name, zone_meta['file'])
mzone = get_zone_data(zone_name, zone_meta['file'], ZONE_PATH)
diff_nameservers(ns1, ns2, zone_name, mzone)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Diff some nameservers')
parser.add_argument('--ns1', required=True, help="The first nameserver to"
" diff", type=str)
parser.add_argument(
'--ns1-knows-more', dest='ns1_knows_more', required=False,
default=False, action='store_true',
help="If ns1 is allowed to know more than ns2 add this flag. It will "
"ignore differences when ns1 sees records that ns2 does not",
)
parser.add_argument(
'--ns1-port', dest='ns1_port', required=False, default='53',
help="The first nameservers DNS port", type=str
)
parser.add_argument(
'--ns1-tcp', dest='ns1_tcp', required=False,
help="Use tcp for queries to ns1", action='store_true',
default=False
)
parser.add_argument('--ns2', required=True, help="The second nameserver "
" to diff")
parser.add_argument(
'--ns2-port', dest='ns2_port', required=False, default='53',
help="The second nameservers DNS port", type=str
)
parser.add_argument(
'--ns2-tcp', dest='ns2_tcp', required=False,
help="Use tcp for queries to ns2", action='store_true',
default=False
)
parser.add_argument('--file', dest="data_file", help="A single zone file."
" Use this option with the --zone option", type=str)
parser.add_argument(
'--named-path', dest='named_path1', type=str, required=True,
help="A full path to where named would be running."
)
parser.add_argument(
'--second-named-path', dest='named_path2', type=str, default=None,
help="Used to help resolve file paths when --second-view-file is used."
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'--zones-file', help="A file containing bind zone statements."
)
group.add_argument(
'--zone-name', help="A single zone's name. Use the "
"--file option to specify where to find it's zone "
"file."
)
group.add_argument(
'--view-file', dest='view_file1', type=str,
default=None, help="A full file path to a view file"
)
parser.add_argument(
'--second-view-file', dest='view_file2', type=str,
default=None, help="An optional file. If both view-file and "
"second-view-file are provided, their contents will be compared "
"statically"
)
try:
nas = parser.parse_args(sys.argv[1:])
except:
with open('/tmp/debug', 'w+') as fd:
fd.write(str(sys.argv))
sys.exit(1)
if nas.zone_name and not nas.data_file:
print ("When using the --zone-name option you must specify the "
"zone's file with --file")
sys.exit(1)
if nas.view_file2 and not nas.view_file1:
print "Speciefy view-file when using second-view-file"
sys.exit(1)
if nas.view_file2 and not nas.named_path2:
print "Speciefy second-named-path when using second-view-file"
sys.exit(1)
class Nameserver(object):
def __str__(self):
return "{0}://{1}:{2}".format(
'udp' if self.proto == 'notcp' else 'tcp',
self.name, self.port
)
def __repr__(self):
return str(self)
def configure_ns(nas, nsname):
ns = Nameserver()
setattr(ns, 'name', getattr(nas, nsname))
setattr(ns, 'port', getattr(nas, nsname + '_port', '53'))
setattr(
ns, 'proto',
'tcp' if getattr(nas, nsname + '_tcp', False) else 'notcp'
)
return ns
ns1 = configure_ns(nas, 'ns1')
ns2 = configure_ns(nas, 'ns2')
ns1.knows_more = True if nas.ns1_knows_more else False
if nas.view_file1:
diff_view(
ns1, ns2, nas.view_file1, nas.named_path1,
named_path2=nas.named_path2, view_file2=nas.view_file2
)
elif nas.zones_file:
diff_zones(
ns1, ns2, nas.zones_file, nas.named_path1,
)
elif nas.zone_name:
diff_zone(
ns1, ns2, nas.zone_name, nas.data_file, nas.named_path1
)
print "Done diffing!"