-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpdns-dynamic-reverse-backend.py
executable file
·276 lines (239 loc) · 10.5 KB
/
pdns-dynamic-reverse-backend.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
#!/usr/bin/env python
"""
PowerDNS pipe backend for generating reverse DNS entries and their
forward lookup.
pdns.conf example: passing command name, config file & log level
launch=pipe
pipe-command=/usr/sbin/pdns-dynamic-reverse-backend.py /etc/pdns/dynrev.yml 1
pipe-timeout=500
if you use other backends include them all in the one launch statement
e.g.
launch=gmysql,pipe
### LICENSE ###
The MIT License
Copyright (c) 2009 Wijnand "maze" Modderman
Copyright (c) 2010 Stefan "ZaphodB" Schmidt
Copyright (c) 2011 Endre Szabo
Copyright (c) 2017 Technical University of Munich (Lukas Erlacher)
Copyright (c) 2019 David Beveridge (bevhost)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import sys, os
import re
import syslog
import time
import netaddr
import IPy
import radix
import yaml
LOGLEVEL = 2
CONFIG = 'dynrev.yml'
VERSION = 0.9
DIGITS = '0123456789bcdfghjklmnpqrstvwxyz'
SCRIPTNAME=os.path.basename(sys.argv[0])
#xrange() backwards compatibility for python3
try:
xrange
except NameError:
xrange = range
try:
long
except NameError:
long = int
class HierDict(dict):
def __init__(self, parent=None, default=None):
self._parent = parent
if default != None:
self.update(default)
def __getitem__(self, name):
try:
return super(HierDict,self).__getitem__(name)
except KeyError as e:
if self._parent is None:
raise
return self._parent[name]
def base36encode(n):
s = ''
while True:
n, r = divmod(n, len(DIGITS))
s = DIGITS[r] + s
if n == 0:
break
return s
def base36decode(s):
n, s = 0, s[::-1]
for i in xrange(0, len(s)):
r = DIGITS.index(s[i])
n += r * (len(DIGITS) ** i)
return n
def parse(prefixes, rtree, fd, out):
def log(level=LOGLEVEL, message=None, **kwargs):
if level<=LOGLEVEL:
message="%s; %s" % (message, ", ".join(filter(None, map(lambda k: "%s=%s" % (k, repr(str(kwargs[k]))), kwargs.keys()))))
#out.write("LOG\t%s\n" % message)
syslog.syslog(message)
def logd(level=LOGLEVEL, message=None, kwargs={}):
if level<=LOGLEVEL:
message="%s; %s" % (message, ", ".join(filter(None, map(lambda k: "%s=%s" % (k, repr(str(kwargs[k]))), kwargs.keys()))))
#out.write("LOG\t%s\n" % message)
syslog.syslog(message)
log(0,"starting up")
line = fd.readline().strip()
if not line.startswith('HELO'):
out.write("FAIL\n")
out.flush()
log(1,"unknown line received from powerdns, expected HELO", line=line)
sys.exit(1)
else:
out.write("OK\t%s ready with %d prefixes configured\n" % (SCRIPTNAME, len(prefixes)))
log(0,'powerdns HELO received, ready to process requests', prefixes_count=len(prefixes))
out.flush()
lastnet=0
while True:
line = fd.readline().strip()
if not line:
break
#syslog.syslog('<<< %s' % (line,))
log(4, "got input line from powerdns", line=line)
request = line.split('\t')
if request[0] == 'AXFR':
if not lastnet == 0:
out.write("DATA\t%s\t%s\tSOA\t%d\t%s\t%s %s %s 10800 3600 604800 3600\n" % \
(lastnet['forward'], 'IN', lastnet['ttl'], qid, lastnet['dns'], lastnet['email'], time.strftime('%Y%m%d%H')))
lastnet=lastnet
for ns in lastnet['nameserver']:
out.write("DATA\t%s\t%s\tNS\t%d\t%s\t%s\n" % \
(lastnet['forward'], 'IN', lastnet['ttl'], qid, ns))
out.write("END\n")
out.flush()
continue
if len(request) < 6:
out.write("LOG\tPowerDNS sent unparsable line\n")
out.write("FAIL\n")
out.flush()
continue
#q&d handling of different pdns pipe backend protocol versions
try:
kind, qname, qclass, qtype, qid, ip = request
except ValueError:
kind, qname, qclass, qtype, qid, ip, their_ip = request
log(2, "parsed query", qname=qname, qtype=qtype, qclass=qclass, qid=qid, ip=ip)
if qtype in ['AAAA', 'ANY']:
for range in prefixes.keys():
key=prefixes[range]
if qname.endswith('.%s' % (key['forward'],)) and key['version'] == 6 and qname.startswith(key['prefix']):
node = qname[len(key['prefix']):].replace('%s.%s' % (key['postfix'], key['forward'],), '')
try:
node = base36decode(node)
ipv6 = netaddr.IPAddress(long(range.value) + long(node))
out.write("DATA\t%s\t%s\tAAAA\t%d\t%s\t%s\n" % \
(qname, qclass, key['ttl'], qid, ipv6))
break
except ValueError:
node = None
if qtype in ['A', 'ANY']:
for range in prefixes.keys():
key=prefixes[range]
if qname.endswith('.%s' % (key['forward'],)) and key['version'] == 4 and qname.startswith(key['prefix']):
node = qname[len(key['prefix']):].replace('%s.%s' % (key['postfix'], key['forward'],), '')
try:
node = base36decode(node)
ipv4 = netaddr.IPAddress(long(range.value) + long(node))
out.write("DATA\t%s\t%s\tA\t%d\t%s\t%s\n" % \
(qname, qclass, key['ttl'], qid, ipv4))
break
except ValueError:
log(3,'failed to base36 decode host value',node=node)
if qtype in ['PTR', 'ANY'] and qname.endswith('.ip6.arpa'):
ptr = qname.split('.')[:-2][::-1]
ipv6 = ':'.join(''.join(ptr[x:x+4]) for x in xrange(0, len(ptr), 4))
try:
ipv6 = netaddr.IPAddress(ipv6)
except:
ipv6 = netaddr.IPAddress('::')
node=rtree.search_best(str(ipv6))
if node:
range, key = node.data['prefix'], prefixes[node.data['prefix']]
node = ipv6.value - range.value
node = base36encode(node)
out.write("DATA\t%s\t%s\tPTR\t%d\t%s\t%s%s%s.%s\n" % \
(qname, qclass, key['ttl'], qid, key['prefix'], node, key['postfix'], key['forward']))
if qtype in ['PTR', 'ANY'] and qname.endswith('.in-addr.arpa'):
ptr = qname.split('.')[:-2][::-1]
ipv4='.'.join(''.join(ptr[x:x+1]) for x in xrange(0, len(ptr), 1))
try:
ipv4 = netaddr.IPAddress(ipv4)
except:
ipv4 = netaddr.IPAddress('127.0.0.1')
node=rtree.search_best(str(ipv4))
if node:
range, key = node.data['prefix'], prefixes[node.data['prefix']]
node = ipv4.value - range.value
node = base36encode(node)
out.write("DATA\t%s\t%s\tPTR\t%d\t%s\t%s%s%s.%s\n" % \
(qname, qclass, key['ttl'], qid, key['prefix'], node, key['postfix'], key['forward']))
if qtype in ['SOA', 'ANY', 'NS']:
for range in prefixes.keys():
key=prefixes[range]
if qname == key['domain']:
if not qtype == 'NS':
out.write("DATA\t%s\t%s\tSOA\t%d\t%s\t%s %s %s 10800 3600 604800 3600\n" % \
(key['domain'], qclass, key['ttl'], qid, key['dns'], key['email'], time.strftime('%Y%m%d%H')))
lastnet=key
if qtype in ['ANY', 'NS']:
for ns in key['nameserver']:
out.write("DATA\t%s\t%s\tNS\t%d\t%s\t%s\n" % \
(key['domain'], qclass, key['ttl'], qid, ns))
break
elif qname == key['forward']:
if not qtype == 'NS':
out.write("DATA\t%s\t%s\tSOA\t%d\t%s\t%s %s %s 10800 3600 604800 3600\n" % \
(key['forward'], qclass, key['ttl'], qid, key['dns'], key['email'], time.strftime('%Y%m%d%H')))
lastnet=key
if qtype in ['ANY', 'NS']:
for ns in key['nameserver']:
out.write("DATA\t%s\t%s\tNS\t%d\t%s\t%s" % \
(key['forward'], qclass, key['ttl'], qid, ns))
break
out.write("END\n")
out.flush()
log(1, "terminating")
return 0
def parse_config(config_path):
with open(config_path) as config_file:
config_dict = yaml.load(config_file)
defaults = config_dict.get('defaults', {})
prefixes = { netaddr.IPNetwork(prefix) : HierDict(defaults, info) for prefix, info in config_dict['prefixes'].items()}
for zone in prefixes:
if not 'domain' in prefixes[zone]:
from IPy import IP
prefixes[zone]['domain']=IP(str(zone.cidr)).reverseName()[:-1]
rtree=radix.Radix()
for prefix in prefixes.keys():
node=rtree.add(str(prefix))
node.data['prefix']=prefix
return prefixes, rtree
if __name__ == '__main__':
syslog.openlog(os.path.basename(sys.argv[0]), syslog.LOG_PID)
if len(sys.argv) > 1:
config_path = sys.argv[1]
if len(sys.argv) > 2:
LOGLEVEL = int(sys.argv[2])
else:
config_path = CONFIG
prefixes, rtree = parse_config(config_path)
sys.exit(parse(prefixes, rtree, sys.stdin, sys.stdout))