| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# Small library and commandline tool to do logical diffs of zonefiles |
| 4 |
# ./zonediff -h gives you help output |
| 5 |
# |
| 6 |
# Requires dnspython to do all the heavy lifting |
| 7 |
# |
| 8 |
# (c)2009 Dennis Kaarsemaker <[email protected]> |
| 9 |
# |
| 10 |
# Permission to use, copy, modify, and distribute this software and its |
| 11 |
# documentation for any purpose with or without fee is hereby granted, |
| 12 |
# provided that the above copyright notice and this permission notice |
| 13 |
# appear in all copies. |
| 14 |
# |
| 15 |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 16 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 17 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 18 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 |
"""See diff_zones.__doc__ for more information""" |
| 23 |
|
| 24 |
from typing import Any, Union, cast # pylint: disable=unused-import |
| 25 |
|
| 26 |
__all__ = ["diff_zones", "format_changes_plain", "format_changes_html"] |
| 27 |
|
| 28 |
try: |
| 29 |
import dns.node |
| 30 |
import dns.zone |
| 31 |
except ImportError: |
| 32 |
raise SystemExit("Please install dnspython") |
| 33 |
|
| 34 |
|
| 35 |
def diff_zones( |
| 36 |
zone1, # type: dns.zone.Zone |
| 37 |
zone2, # type: dns.zone.Zone |
| 38 |
ignore_ttl=False, |
| 39 |
ignore_soa=False, |
| 40 |
): # type: (...) -> list |
| 41 |
"""diff_zones(zone1, zone2, ignore_ttl=False, ignore_soa=False) -> changes |
| 42 |
Compares two dns.zone.Zone objects and returns a list of all changes |
| 43 |
in the format (name, oldnode, newnode). |
| 44 |
|
| 45 |
If ignore_ttl is true, a node will not be added to this list if the |
| 46 |
only change is its TTL. |
| 47 |
|
| 48 |
If ignore_soa is true, a node will not be added to this list if the |
| 49 |
only changes is a change in a SOA Rdata set. |
| 50 |
|
| 51 |
The returned nodes do include all Rdata sets, including unchanged ones. |
| 52 |
""" |
| 53 |
|
| 54 |
changes = [] |
| 55 |
for name in zone1: |
| 56 |
namestr = str(name) |
| 57 |
n1 = cast(dns.node.Node, zone1.get_node(namestr)) |
| 58 |
n2 = cast(dns.node.Node, zone2.get_node(namestr)) |
| 59 |
if not n2: |
| 60 |
changes.append((str(name), n1, n2)) |
| 61 |
elif _nodes_differ(n1, n2, ignore_ttl, ignore_soa): |
| 62 |
changes.append((str(name), n1, n2)) |
| 63 |
|
| 64 |
for name in zone2: |
| 65 |
n3 = cast(dns.node.Node, zone1.get_node(name)) |
| 66 |
if not n3: |
| 67 |
n4 = cast(dns.node.Node, zone2.get_node(name)) |
| 68 |
changes.append((str(name), n3, n4)) |
| 69 |
return changes |
| 70 |
|
| 71 |
|
| 72 |
def _nodes_differ( |
| 73 |
n1, # type: dns.node.Node |
| 74 |
n2, # type: dns.node.Node |
| 75 |
ignore_ttl, # type: bool |
| 76 |
ignore_soa, # type: bool |
| 77 |
): # type: (...) -> bool |
| 78 |
if ignore_soa or not ignore_ttl: |
| 79 |
# Compare datasets directly |
| 80 |
for r in n1.rdatasets: |
| 81 |
if ignore_soa and r.rdtype == dns.rdatatype.SOA: |
| 82 |
continue |
| 83 |
if r not in n2.rdatasets: |
| 84 |
return True |
| 85 |
if not ignore_ttl: |
| 86 |
return r.ttl != n2.find_rdataset(r.rdclass, r.rdtype).ttl |
| 87 |
|
| 88 |
for r in n2.rdatasets: |
| 89 |
if ignore_soa and r.rdtype == dns.rdatatype.SOA: |
| 90 |
continue |
| 91 |
if r not in n1.rdatasets: |
| 92 |
return True |
| 93 |
assert False |
| 94 |
else: |
| 95 |
return n1 != n2 |
| 96 |
|
| 97 |
|
| 98 |
def format_changes_plain( |
| 99 |
oldf, # type: str |
| 100 |
newf, # type: str |
| 101 |
changes, # type: list |
| 102 |
ignore_ttl=False, |
| 103 |
): # type: (...) -> str |
| 104 |
"""format_changes(oldfile, newfile, changes, ignore_ttl=False) -> str |
| 105 |
Given 2 filenames and a list of changes from diff_zones, produce diff-like |
| 106 |
output. If ignore_ttl is True, TTL-only changes are not displayed""" |
| 107 |
|
| 108 |
ret = "--- {}\n+++ {}\n".format(oldf, newf) |
| 109 |
for name, old, new in changes: |
| 110 |
ret += "@ %s\n" % name |
| 111 |
if not old: |
| 112 |
for r in new.rdatasets: |
| 113 |
ret += "+ %s\n" % str(r).replace("\n", "\n+ ") |
| 114 |
elif not new: |
| 115 |
for r in old.rdatasets: |
| 116 |
ret += "- %s\n" % str(r).replace("\n", "\n+ ") |
| 117 |
else: |
| 118 |
for r in old.rdatasets: |
| 119 |
if r not in new.rdatasets or ( |
| 120 |
r.ttl != new.find_rdataset(r.rdclass, r.rdtype).ttl |
| 121 |
and not ignore_ttl |
| 122 |
): |
| 123 |
ret += "- %s\n" % str(r).replace("\n", "\n+ ") |
| 124 |
for r in new.rdatasets: |
| 125 |
if r not in old.rdatasets or ( |
| 126 |
r.ttl != old.find_rdataset(r.rdclass, r.rdtype).ttl |
| 127 |
and not ignore_ttl |
| 128 |
): |
| 129 |
ret += "+ %s\n" % str(r).replace("\n", "\n+ ") |
| 130 |
return ret |
| 131 |
|
| 132 |
|
| 133 |
def format_changes_html( |
| 134 |
oldf, # type: str |
| 135 |
newf, # type: str |
| 136 |
changes, # type: list |
| 137 |
ignore_ttl=False, |
| 138 |
): # type: (...) -> str |
| 139 |
"""format_changes(oldfile, newfile, changes, ignore_ttl=False) -> str |
| 140 |
Given 2 filenames and a list of changes from diff_zones, produce nice html |
| 141 |
output. If ignore_ttl is True, TTL-only changes are not displayed""" |
| 142 |
|
| 143 |
ret = """<table class="zonediff"> |
| 144 |
<thead> |
| 145 |
<tr> |
| 146 |
<th> </th> |
| 147 |
<th class="old">%s</th> |
| 148 |
<th class="new">%s</th> |
| 149 |
</tr> |
| 150 |
</thead> |
| 151 |
<tbody>\n""" % ( |
| 152 |
oldf, |
| 153 |
newf, |
| 154 |
) |
| 155 |
|
| 156 |
for name, old, new in changes: |
| 157 |
ret += ' <tr class="rdata">\n <td class="rdname">%s</td>\n' % name |
| 158 |
if not old: |
| 159 |
for r in new.rdatasets: |
| 160 |
ret += ( |
| 161 |
' <td class="old"> </td>\n' |
| 162 |
' <td class="new">%s</td>\n' |
| 163 |
) % str(r).replace("\n", "<br />") |
| 164 |
elif not new: |
| 165 |
for r in old.rdatasets: |
| 166 |
ret += ( |
| 167 |
' <td class="old">%s</td>\n' |
| 168 |
' <td class="new"> </td>\n' |
| 169 |
) % str(r).replace("\n", "<br />") |
| 170 |
else: |
| 171 |
ret += ' <td class="old">' |
| 172 |
for r in old.rdatasets: |
| 173 |
if r not in new.rdatasets or ( |
| 174 |
r.ttl != new.find_rdataset(r.rdclass, r.rdtype).ttl |
| 175 |
and not ignore_ttl |
| 176 |
): |
| 177 |
ret += str(r).replace("\n", "<br />") |
| 178 |
ret += "</td>\n" |
| 179 |
ret += ' <td class="new">' |
| 180 |
for r in new.rdatasets: |
| 181 |
if r not in old.rdatasets or ( |
| 182 |
r.ttl != old.find_rdataset(r.rdclass, r.rdtype).ttl |
| 183 |
and not ignore_ttl |
| 184 |
): |
| 185 |
ret += str(r).replace("\n", "<br />") |
| 186 |
ret += "</td>\n" |
| 187 |
ret += " </tr>\n" |
| 188 |
return ret + " </tbody>\n</table>" |
| 189 |
|
| 190 |
|
| 191 |
# Make this module usable as a script too. |
| 192 |
def main(): # type: () -> None |
| 193 |
import argparse |
| 194 |
import subprocess |
| 195 |
import sys |
| 196 |
import traceback |
| 197 |
|
| 198 |
usage = """%prog zonefile1 zonefile2 - Show differences between zones in a diff-like format |
| 199 |
%prog [--git|--bzr|--rcs] zonefile rev1 [rev2] - Show differences between two revisions of a zonefile |
| 200 |
|
| 201 |
The differences shown will be logical differences, not textual differences. |
| 202 |
""" |
| 203 |
p = argparse.ArgumentParser(usage=usage) |
| 204 |
p.add_argument( |
| 205 |
"-s", |
| 206 |
"--ignore-soa", |
| 207 |
action="store_true", |
| 208 |
default=False, |
| 209 |
dest="ignore_soa", |
| 210 |
help="Ignore SOA-only changes to records", |
| 211 |
) |
| 212 |
p.add_argument( |
| 213 |
"-t", |
| 214 |
"--ignore-ttl", |
| 215 |
action="store_true", |
| 216 |
default=False, |
| 217 |
dest="ignore_ttl", |
| 218 |
help="Ignore TTL-only changes to Rdata", |
| 219 |
) |
| 220 |
p.add_argument( |
| 221 |
"-T", |
| 222 |
"--traceback", |
| 223 |
action="store_true", |
| 224 |
default=False, |
| 225 |
dest="tracebacks", |
| 226 |
help="Show python tracebacks when errors occur", |
| 227 |
) |
| 228 |
p.add_argument( |
| 229 |
"-H", |
| 230 |
"--html", |
| 231 |
action="store_true", |
| 232 |
default=False, |
| 233 |
dest="html", |
| 234 |
help="Print HTML output", |
| 235 |
) |
| 236 |
p.add_argument( |
| 237 |
"-g", |
| 238 |
"--git", |
| 239 |
action="store_true", |
| 240 |
default=False, |
| 241 |
dest="use_git", |
| 242 |
help="Use git revisions instead of real files", |
| 243 |
) |
| 244 |
p.add_argument( |
| 245 |
"-b", |
| 246 |
"--bzr", |
| 247 |
action="store_true", |
| 248 |
default=False, |
| 249 |
dest="use_bzr", |
| 250 |
help="Use bzr revisions instead of real files", |
| 251 |
) |
| 252 |
p.add_argument( |
| 253 |
"-r", |
| 254 |
"--rcs", |
| 255 |
action="store_true", |
| 256 |
default=False, |
| 257 |
dest="use_rcs", |
| 258 |
help="Use rcs revisions instead of real files", |
| 259 |
) |
| 260 |
opts, args = p.parse_args() |
| 261 |
opts.use_vc = opts.use_git or opts.use_bzr or opts.use_rcs |
| 262 |
|
| 263 |
def _open(what, err): # type: (Union[list,str], str) -> Any |
| 264 |
if isinstance(what, list): |
| 265 |
# Must be a list, open subprocess |
| 266 |
try: |
| 267 |
proc = subprocess.Popen(what, stdout=subprocess.PIPE) |
| 268 |
proc.wait() |
| 269 |
if proc.returncode == 0: |
| 270 |
return proc.stdout |
| 271 |
sys.stderr.write(err + "\n") |
| 272 |
except Exception: |
| 273 |
sys.stderr.write(err + "\n") |
| 274 |
if opts.tracebacks: |
| 275 |
traceback.print_exc() |
| 276 |
else: |
| 277 |
# Open as normal file |
| 278 |
try: |
| 279 |
return open(what, "rb") |
| 280 |
except IOError: |
| 281 |
sys.stderr.write(err + "\n") |
| 282 |
if opts.tracebacks: |
| 283 |
traceback.print_exc() |
| 284 |
|
| 285 |
if not opts.use_vc and len(args) != 2: |
| 286 |
p.print_help() |
| 287 |
sys.exit(64) |
| 288 |
if opts.use_vc and len(args) not in (2, 3): |
| 289 |
p.print_help() |
| 290 |
sys.exit(64) |
| 291 |
|
| 292 |
# Open file descriptors |
| 293 |
if not opts.use_vc: |
| 294 |
oldn, newn = args |
| 295 |
else: |
| 296 |
if len(args) == 3: |
| 297 |
filename, oldr, newr = args |
| 298 |
oldn = "{}:{}".format(oldr, filename) |
| 299 |
newn = "{}:{}".format(newr, filename) |
| 300 |
else: |
| 301 |
filename, oldr = args |
| 302 |
newr = None |
| 303 |
oldn = "{}:{}".format(oldr, filename) |
| 304 |
newn = filename |
| 305 |
|
| 306 |
old, new = None, None |
| 307 |
oldz, newz = None, None |
| 308 |
if opts.use_bzr: |
| 309 |
old = _open( |
| 310 |
["bzr", "cat", "-r" + oldr, filename], |
| 311 |
"Unable to retrieve revision {} of {}".format(oldr, filename), |
| 312 |
) |
| 313 |
if newr is not None: |
| 314 |
new = _open( |
| 315 |
["bzr", "cat", "-r" + newr, filename], |
| 316 |
"Unable to retrieve revision {} of {}".format(newr, filename), |
| 317 |
) |
| 318 |
elif opts.use_git: |
| 319 |
old = _open( |
| 320 |
["git", "show", oldn], |
| 321 |
"Unable to retrieve revision {} of {}".format(oldr, filename), |
| 322 |
) |
| 323 |
if newr is not None: |
| 324 |
new = _open( |
| 325 |
["git", "show", newn], |
| 326 |
"Unable to retrieve revision {} of {}".format(newr, filename), |
| 327 |
) |
| 328 |
elif opts.use_rcs: |
| 329 |
old = _open( |
| 330 |
["co", "-q", "-p", "-r" + oldr, filename], |
| 331 |
"Unable to retrieve revision {} of {}".format(oldr, filename), |
| 332 |
) |
| 333 |
if newr is not None: |
| 334 |
new = _open( |
| 335 |
["co", "-q", "-p", "-r" + newr, filename], |
| 336 |
"Unable to retrieve revision {} of {}".format(newr, filename), |
| 337 |
) |
| 338 |
if not opts.use_vc: |
| 339 |
old = _open(oldn, "Unable to open %s" % oldn) |
| 340 |
if not opts.use_vc or newr is None: |
| 341 |
new = _open(newn, "Unable to open %s" % newn) |
| 342 |
|
| 343 |
if not old or not new: |
| 344 |
sys.exit(65) |
| 345 |
|
| 346 |
# Parse the zones |
| 347 |
try: |
| 348 |
oldz = dns.zone.from_file(old, origin=".", check_origin=False) |
| 349 |
except dns.exception.DNSException: |
| 350 |
sys.stderr.write("Incorrect zonefile: %s\n" % old) |
| 351 |
if opts.tracebacks: |
| 352 |
traceback.print_exc() |
| 353 |
try: |
| 354 |
newz = dns.zone.from_file(new, origin=".", check_origin=False) |
| 355 |
except dns.exception.DNSException: |
| 356 |
sys.stderr.write("Incorrect zonefile: %s\n" % new) |
| 357 |
if opts.tracebacks: |
| 358 |
traceback.print_exc() |
| 359 |
if not oldz or not newz: |
| 360 |
sys.exit(65) |
| 361 |
|
| 362 |
changes = diff_zones(oldz, newz, opts.ignore_ttl, opts.ignore_soa) |
| 363 |
changes.sort() |
| 364 |
|
| 365 |
if not changes: |
| 366 |
sys.exit(0) |
| 367 |
if opts.html: |
| 368 |
print(format_changes_html(oldn, newn, changes, opts.ignore_ttl)) |
| 369 |
else: |
| 370 |
print(format_changes_plain(oldn, newn, changes, opts.ignore_ttl)) |
| 371 |
sys.exit(1) |
| 372 |
|
| 373 |
|
| 374 |
if __name__ == "__main__": |
| 375 |
main() |