-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap2zone.py
executable file
·437 lines (357 loc) · 15 KB
/
ldap2zone.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#!/usr/bin/env python
# coding: utf-8
# vim: set si ai et sw=4 sts=4 ts=4 ft=python:
#
# Copyright (c) 2015, Paul Tötterman <paul.totterman@iki.fi>
# Copyright (c) 2015, ZenRobotics Ltd.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""Generate bind9 zone file from LDAP.
Usage:
ldap2zone [options] [-z ZONE]...
ldap2zone -h
ldap2zone -V
Options:
-h --help Show this usage message.
-V --version Show version.
-d --debug Debug output.
-v --verbose Verbose output.
-q --quiet Less output.
-l --ldap-conf CONF Location of ldap.conf [default: /etc/ldap/ldap.conf]
-H --uri URI LDAP URI (from ldap.conf by default).
-b --base BASE LDAP search base (from ldap.conf by default).
-D --binddn DN LDAP bind DN.
-p --bindpw PASS LDAP bind password.
-P --prompt-password Prompt for LDAP bind password.
-o --output-dir DIR Output directory [default: .].
-s --serial SERIAL DNS zone serial (UNIX ts by default).
-r --reverse Output reverse DNS zones [default: false].
-z --zone ZONE DNS zones to process (from LDAP base by default).
"""
import docopt
import getpass
import ldap
import ldap.filter
import logging
import os
import re
import sys
import time
RECORDLINE = '%(name)s\t%(ttl)s\t%(type)s\t%(data)s\n'
ZONEFILE = 'db.%(zone)s'
REVERSEZONE = '%(third)s.%(second)s.%(first)s.in-addr.arpa'
RECORD_ATTRIBUTES = ['a6Record',
'aAAARecord',
'aFSDBRecord',
'aPLRecord',
'aRecord',
'cERTRecord',
'cNAMERecord',
'dHCIDRecord',
'dLVRecord',
'dNAMERecord',
'dNSKEYRecord',
'dSRecord',
'hINFORecord',
'hIPRecord',
'iPSECKEYRecord',
'kEYRecord',
'kXRecord',
'lOCRecord',
'mDRecord',
'mINFORecord',
'mXRecord',
'nAPTRRecord',
'nSEC3PARAMRecord',
'nSEC3Record',
'nSECRecord',
'nSRecord',
'nXTRecord',
'pTRRecord',
'rPRecord',
'rRSIGRecord',
'sIGRecord',
#'sOARecord', # special case
'sPFRecord',
'sRVRecord',
'sSHFPRecord',
'tARecord',
'tKEYRecord',
'tSIGRecord',
'tXTRecord']
SOA_ATTRIBUTES = {'sOANameServer': 'ns',
'sOAEmail': 'email',
'sOASerial': 'serial',
'sOARefresh': 'refresh',
'sOARetry': 'retry',
'sOAExpire': 'expire',
'sOANegCache': 'negcache',
'defaultTTL': 'dttl'} # doesn't actually belong here
SOA_DEFAULTS = {'refresh': 1200, # RFC1912
'retry': 180,
'expire': 1209600, # RFC1912
'negcache': 60}
ZONEHEADER = '''\
; autogenerated bind zone file by ldap2zone.py
; vim: set ft=bindzone:
$ORIGIN %(zone)s.
$TTL\t%(ttl)s
'''
def parse_ldap_base(ldap_conf):
"""Parse LDAP base from ldap.conf(5)."""
# pylint: disable-msg=W0141
ldap_conf = open(ldap_conf, 'rb')
pattern = re.compile(r'''^\s*[bB][aA][sS][eE]\s+(.+)\s*$''')
matches = [pattern.search(x) for x in ldap_conf.readlines()]
base = filter(None, matches)[0].group(1)
return base
def get_ldap_base(conn, ldap_conf, persist={}):
"""Find out LDAP base."""
# pylint: disable-msg=W0102,C0103
if 'base' in persist:
return persist['base']
entries = conn.search_s('', ldap.SCOPE_BASE, 'objectClass=*', ('+',))
attrs = entries[0][1]
if len(attrs['namingContexts']) == 1:
base = attrs['namingContexts'][0]
else:
base = parse_ldap_base(ldap_conf)
persist['base'] = base
return base
def write_record(ofile, values):
"""Output a single DNS record."""
ofile.write(RECORDLINE % values)
def mtime2epoch(mtimestr):
"""Convert LDAP modifyTimestamp attribute value to unix epoch timestamp."""
return int(time.mktime(time.strptime(mtimestr, '%Y%m%d%H%M%SZ')))
def attribute2type(attribute):
"""Convert LDAP attribute name to DNS record type."""
return attribute.split('Record')[0].upper()
def entry2cn(entry):
"""Figure out a suitable reverse mapping for an entry."""
# pylint: disable-msg=C0103
dn = entry[0]
rdns = [rdn.split('=') for rdn in dn.split(',')]
cns = [v for k, v in rdns if k in ('relativeDomainName', 'cn')]
attrs = entry[1]
zone = attrs['zoneName'][0]
if cns:
if len(cns) > 1:
logging.warn('multiple "cns" for %s, picking first: %s', dn, cns)
return '%s.%s' % (cns[0], zone)
if 'relativeDomainName' in attrs:
rdns = [rdn for rdn in attrs['relativeDomainName'] if rdn != '@']
if rdns:
if len(rdns) > 1:
logging.warn('multiple "rdns" for %s, picking first: %s', dn,
rdns)
return '%s.%s' % (rdns[0], zone)
if 'cn' in attrs:
logging.warn('multiple "cn"s for %s, picking first: %s', dn,
attrs['cn'])
return '%s.%s' % (attrs['cn'][0], zone)
logging.warn('no suitable cns or rdns for %s, picking %s', dn, zone)
return zone
def add_reverses(reverse_maps, entry):
"""Add the reverses implied by entry."""
# pylint: disable-msg=C0103
def add_reverse(ip, cn):
"""Add a single reverse."""
parts = ip.split('.')
reverse_map = reverse_maps
for part in parts[:3]:
if part not in reverse_map:
reverse_map[part] = {}
reverse_map = reverse_map[part]
if parts[3] not in reverse_map:
reverse_map[parts[3]] = cn
else:
logging.warn('multiple reverses for IP %s: %s %s', ip,
reverse_map[parts[3]], cn)
cn = entry2cn(entry)
attrs = entry[1]
ips = []
for attr in ('aRecord', 'ipHostNumber'):
if attr in attrs:
ips += attrs[attr]
for ip in ips:
add_reverse(ip, cn)
def build_soa(zone_entry):
"""Return SOA record based on existing values in zone entry or guesses.
>>> sorted(build_soa({'relativeDomainName': (None, ['@']),
... 'sOARecord': (None,
... ['ns.example.com. hostmaster.example.com. '
... '(1 600 300 3600 60)'])}).items())
... # doctest: +NORMALIZE_WHITESPACE
[('data', 'ns.example.com. hostmaster.example.com. (1 600 300 3600 60)'),
('name', '@'), ('ttl', ''), ('type', 'SOA')]
>>> sorted(build_soa({'sOANameServer': (None, ['ns.example.com.']),
... 'sOAEmail': (None, ['hostmaster.example.com.']),
... 'sOASerial': (None, ['1']),}).items())
... # doctest: +NORMALIZE_WHITESPACE
[('data',
'ns.example.com. hostmaster.example.com. (1 1200 180 1209600 60)'),
('name', '@'), ('ttl', ''), ('type', 'SOA')]
"""
if 'sOARecord' in zone_entry:
return {'name': zone_entry['relativeDomainName'][1][0],
'ttl': '',
'type': attribute2type('sOARecord'),
'data': zone_entry['sOARecord'][1][0]}
soa_values = SOA_DEFAULTS.copy()
for attr in SOA_ATTRIBUTES.keys():
if attr in zone_entry:
soa_values[SOA_ATTRIBUTES[attr]] = zone_entry[attr][1][0]
return {'name': '@',
'ttl': '',
'type': 'SOA',
'data': ('%(ns)s %(email)s (%(serial)s %(refresh)s %(retry)s '
'%(expire)s %(negcache)s)' % soa_values)}
def write_reverse_zones(reverse_maps, serial, nameserver, email, outdir):
"""Write reverse zones based on the extracted reverse maps."""
zones = []
for first in reverse_maps:
for second in reverse_maps[first]:
for third in reverse_maps[first][second]:
zone = REVERSEZONE % {'first': first,
'second': second,
'third': third}
zones.append(zone)
zonefile = ZONEFILE % {'zone': zone}
with open(os.path.join(outdir, zonefile), 'w') as ofile:
ofile.write(ZONEHEADER % {'zone': zone, 'ttl': 60})
write_record(ofile, build_soa(
{'sOANameServer': (None, [nameserver]),
'sOASerial': (None, [serial]),
'sOAEmail': (None, [email])}))
write_record(ofile, {'name': '@',
'ttl': '',
'type': 'NS',
'data': nameserver})
for fourth in reverse_maps[first][second][third]:
data = ('%s.' %
reverse_maps[first][second][third][fourth])
write_record(ofile,
{'name': fourth,
'ttl': '',
'type': 'PTR',
'data': data})
def build_zone(conn, base, zone, reverse_maps, serial=None):
"""Build zone information from LDAP."""
query_attrs = RECORD_ATTRIBUTES + SOA_ATTRIBUTES.keys() + [
'relativeDomainName', 'zoneName', 'modifyTimestamp', 'dNSTTL',
'defaultTTL']
filterstr = ldap.filter.filter_format('(&(objectClass=dNSRecord)'
'(zoneName=%s))', (zone,))
entries = conn.search_s(base, ldap.SCOPE_SUBTREE, filterstr, query_attrs)
epochs = [mtime2epoch(e[1]['modifyTimestamp'][0]) for e in entries]
unified_entries = {}
unified_entries['@'] = {}
unified_entries['@']['sOASerial'] = (None, [serial or max([0]+epochs)])
for entry in entries:
attrs = entry[1]
for name in attrs['relativeDomainName']:
if not name in unified_entries:
unified_entries[name] = {}
unified_attrs = unified_entries[name]
for attr in RECORD_ATTRIBUTES + SOA_ATTRIBUTES.keys():
ttl = None
if 'dNSTTL' in attrs:
ttl = attrs['dNSTTL'][0]
if attr in attrs:
unified_attrs[attr] = (ttl, attrs[attr])
add_reverses(reverse_maps, entry)
return unified_entries
def write_zonefile(zone, unified_entries, outdir):
"""Write a single zonefile to outdir."""
zone_entry = unified_entries['@']
default_ttl = 60
if 'defaultTTL' in zone_entry:
default_ttl = zone_entry['defaultTTL'][1][0]
zonefile = ZONEFILE % {'zone': zone}
with open(os.path.join(outdir, zonefile), 'w') as ofile:
ofile.write(ZONEHEADER % {'zone': zone, 'ttl': default_ttl})
write_record(ofile, build_soa(zone_entry))
for name in unified_entries:
attrs = unified_entries[name]
ttl = ''
if 'dNSTTL' in attrs and attrs['dNSTTL'][0] != default_ttl:
ttl = attrs['dNSTTL'][0]
for attr in RECORD_ATTRIBUTES:
if attr in attrs:
if (attrs[attr][0] is not None and
attrs[attr][0] != default_ttl):
ttl = attrs[attr][0]
for value in attrs[attr][1]:
write_record(ofile,
{'name': name,
'ttl': ttl,
'type': attribute2type(attr),
'data': value})
def generate_zonefiles(conn, base, zones, outdir, serial=None, reverse=False):
"""Generates the zonefiles from LDAP. Does the actual work."""
# pylint: disable-msg=C0103,R0912,R0914
reverse_maps = {}
if not os.path.exists(outdir):
os.makedirs(outdir)
max_serial = 0
rzone_ns = None
rzone_email = None
for zone in zones:
unified_entries = build_zone(conn, base, zone, reverse_maps, serial)
zone_serial = unified_entries['@']['sOASerial'][1][0]
max_serial = zone_serial if zone_serial > max_serial else max_serial
email = unified_entries['@']['sOAEmail'][1][0]
if rzone_email is not None and rzone_email != email:
logging.warn('Zones with different emails, picking %s', email)
rzone_email = email
nameserver = unified_entries['@']['sOANameServer'][1][0]
if rzone_ns is not None and rzone_ns != nameserver:
logging.warn('Zones with different nameservers, picking %s',
nameserver)
rzone_ns = nameserver
write_zonefile(zone, unified_entries, outdir)
if serial is None:
serial = max_serial
if reverse:
write_reverse_zones(reverse_maps, serial, rzone_ns, rzone_email, outdir)
def main():
"""The main function."""
opts = docopt.docopt(__doc__, version='0.1.0')
loglevel = logging.WARNING
if opts['--debug']:
loglevel = logging.DEBUG
elif opts['--verbose']:
loglevel = logging.INFO
elif opts['--quiet']:
loglevel = logging.ERROR
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s',
level=loglevel, stream=sys.stderr)
if opts['--prompt-password']:
opts['--bindpw'] = getpass.getpass()
uri = opts['--uri'] if opts['--uri'] else ldap.get_option(ldap.OPT_URI)
conn = ldap.initialize(uri)
base = opts['--base'] if opts['--base'] else ('ou=Hosts,%s' %
get_ldap_base(conn, opts['--ldap-conf']))
# pylint: disable-msg=C0103
dn = [rdn.split('=') for rdn in base.split(',')]
dcs = [v for k, v in dn if k == 'dc']
zones = ['.'.join(dcs)]
if opts['--zone']:
zones = opts['--zone']
if opts['--binddn']:
conn.simple_bind_s(opts['--binddn'], opts['--bindpw'])
generate_zonefiles(conn, base, zones, outdir=opts['--output-dir'],
serial=opts['--serial'], reverse=opts['--reverse'])
if __name__ == '__main__':
main()