Zack Williams | 6dc2d45 | 2017-12-20 17:50:49 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Copyright 2017-present Open Networking Foundation |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # unbound_revdns - ansible filter - ipv4 address or cidr and generates a |
| 18 | # reverse DNS string suitable for use with unbound, which requires them in a |
| 19 | # slightly unusual format when a RFC1918 address is used with a local-zone |
| 20 | # definition: |
| 21 | # https://www.unbound.net/documentation/unbound.conf.html |
| 22 | # https://www.claudiokuenzler.com/blog/699/unbound-dns-not-serving-reverse-lookup-for-internal-addresses-rfc1918 |
| 23 | |
| 24 | import netaddr |
| 25 | |
| 26 | |
| 27 | class FilterModule(object): |
| 28 | |
| 29 | def filters(self): |
| 30 | return { |
| 31 | 'unbound_revdns': self.unbound_revdns, |
| 32 | } |
| 33 | |
| 34 | def unbound_revdns(self, var): |
| 35 | (o1, o2, o3, o4) = netaddr.IPNetwork(var).network.words |
| 36 | |
| 37 | revdns = "%d.%d.%d.%d.in-addr.arpa." % (o4, o3, o2, o1) |
| 38 | |
| 39 | if (o2 == 0 or o1 == 10): |
| 40 | revdns = "%d.in-addr.arpa." % (o1) |
| 41 | elif (o3 == 0 |
| 42 | or (o1 == 172 and o2 >= 16 and o2 <= 31) |
| 43 | or (o1 == 192 and o2 == 168)): |
| 44 | revdns = "%d.%d.in-addr.arpa." % (o2, o1) |
| 45 | elif(o4 == 0): |
| 46 | revdns = "%d.%d.%d.in-addr.arpa." % (o3, o2, o1) |
| 47 | |
| 48 | return revdns |