Zack Williams | 13644cc | 2020-08-30 15:16:43 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # SPDX-FileCopyrightText: © 2017-2020 Open Networking Foundation <support@opennetworking.org> |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | # unbound_revdns - ansible filter - ipv4 address or cidr and generates a |
| 7 | # reverse DNS string suitable for use with unbound, which requires them in a |
| 8 | # slightly unusual format when a RFC1918 address is used with a local-zone |
| 9 | # definition: |
| 10 | # https://nlnetlabs.nl/documentation/unbound/unbound.conf/#local-zone |
| 11 | # https://www.claudiokuenzler.com/blog/699/unbound-dns-not-serving-reverse-lookup-for-internal-addresses-rfc1918 |
| 12 | |
| 13 | from __future__ import absolute_import |
| 14 | import netaddr |
| 15 | |
| 16 | |
| 17 | class FilterModule(object): |
| 18 | def filters(self): |
| 19 | return { |
| 20 | "unbound_revdns": self.unbound_revdns, |
| 21 | } |
| 22 | |
| 23 | def unbound_revdns(self, var): |
| 24 | (o1, o2, o3, o4) = netaddr.IPNetwork(var).network.words |
| 25 | |
| 26 | revdns = "%d.%d.%d.%d.in-addr.arpa." % (o4, o3, o2, o1) |
| 27 | |
| 28 | if o2 == 0 or o1 == 10: |
| 29 | revdns = "%d.in-addr.arpa." % (o1) |
| 30 | elif ( |
| 31 | o3 == 0 |
| 32 | or (o1 == 172 and o2 >= 16 and o2 <= 31) |
| 33 | or (o1 == 192 and o2 == 168) |
| 34 | ): |
| 35 | revdns = "%d.%d.in-addr.arpa." % (o2, o1) |
| 36 | elif o4 == 0: |
| 37 | revdns = "%d.%d.%d.in-addr.arpa." % (o3, o2, o1) |
| 38 | |
| 39 | return revdns |