|
|
|
1 |
#!/usr/bin/python2.7 |
2 |
# -*- coding: utf-8 -*- |
3 |
""" |
4 |
Univention BIND listener script |
5 |
""" |
6 |
# Copyright 2015 Univention GmbH |
7 |
# |
8 |
# http://www.univention.de/ |
9 |
# |
10 |
# All rights reserved. |
11 |
# |
12 |
# The source code of this program is made available |
13 |
# under the terms of the GNU Affero General Public License version 3 |
14 |
# (GNU AGPL V3) as published by the Free Software Foundation. |
15 |
# |
16 |
# Binary versions of this program provided by Univention to you as |
17 |
# well as other copyrighted, protected or trademarked materials like |
18 |
# Logos, graphics, fonts, specific documentations and configurations, |
19 |
# cryptographic keys etc. are subject to a license agreement between |
20 |
# you and Univention and not subject to the GNU AGPL V3. |
21 |
# |
22 |
# In the case you use this program under the terms of the GNU AGPL V3, |
23 |
# the program is provided in the hope that it will be useful, |
24 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26 |
# GNU Affero General Public License for more details. |
27 |
# |
28 |
# You should have received a copy of the GNU Affero General Public |
29 |
# License with the Debian GNU/Linux or Univention distribution in file |
30 |
# /usr/share/common-licenses/AGPL-3; if not, see |
31 |
# <http://www.gnu.org/licenses/>. |
32 |
|
33 |
__package__ = '' # workaround for PEP 366 |
34 |
import ldap |
35 |
import listener |
36 |
import ipaddr |
37 |
import subprocess |
38 |
|
39 |
name = 'bind-acl' |
40 |
description = 'Update BIND ACLs' |
41 |
filter = '(objectClass=univentionNetworkClass)' |
42 |
attributes = ['univentionNetmask', 'univentionNetwork'] |
43 |
|
44 |
ACL_CONF_FILE = "/var/lib/bind/network-acls.conf" |
45 |
RNDC_BIN = "/usr/sbin/rndc" |
46 |
|
47 |
conf = { |
48 |
'ldapserver': None, |
49 |
'ldapport': 7389, |
50 |
'basedn': None, |
51 |
'binddn': None, |
52 |
'bindpw': None, |
53 |
} |
54 |
|
55 |
|
56 |
def setdata(key, value): |
57 |
conf[key] = value |
58 |
|
59 |
|
60 |
def handler(dn, new, old): |
61 |
ldap_uri = "ldap://%(ldapserver)s:%(ldapport)d" % conf |
62 |
ldap_con = ldap.initialize(ldap_uri) |
63 |
ldap_con.bind_s(conf['binddn'], conf['bindpw']) |
64 |
networks = set() |
65 |
for dn, values in ldap_con.search_s(conf['basedn'], ldap.SCOPE_SUBTREE, filter, attributes): |
66 |
network = ipaddr.IPNetwork('%s/%s' % ( |
67 |
values['univentionNetwork'][0], |
68 |
values['univentionNetmask'][0], |
69 |
)) |
70 |
networks.add(network.masked()) |
71 |
|
72 |
listener.setuid(0) |
73 |
try: |
74 |
with open(ACL_CONF_FILE, 'w') as acl: |
75 |
print >> acl, '# THIS FILE IS GENERATED BY bind-acl.py' |
76 |
print >> acl, 'acl ucs_networks {' |
77 |
print >> acl, '\tlocalhost;' |
78 |
for network in networks: |
79 |
print >> acl, '\t%s;' % (network,) |
80 |
print >> acl, '};' |
81 |
subprocess.call(('rndc', '-p', '953', 'reconfig')) |
82 |
finally: |
83 |
listener.unsetuid() |
84 |
|
85 |
|
86 |
if __name__ == '__main__': |
87 |
from univention.config_registry import ConfigRegistry |
88 |
ucr = ConfigRegistry() |
89 |
ucr.load() |
90 |
setdata('ldapserver', ucr['ldap/server/name']) |
91 |
setdata('basedn', ucr['ldap/base']) |
92 |
setdata('binddn', ucr['ldap/hostdn']) |
93 |
setdata('bindpw', open('/etc/machine.secret', 'r').read()) |
94 |
handler('', None, None) |