dynette/dynette.cron.py

121 lines
4.1 KiB
Python
Raw Normal View History

2013-06-16 01:06:25 +02:00
#!/usr/bin/python
2013-06-17 10:40:53 +02:00
### Configuration ###
2013-06-16 01:06:25 +02:00
2013-06-17 10:08:26 +02:00
conf_file = '/etc/bind/named.conf.local' # Include this filename in '/etc/bind/named.conf'
2013-08-07 12:40:49 +02:00
zone_dir = '/var/lib/bind/' # Do not forget the trailing '/'
subs_urls = ['https://dyndns.yunohost.org'] # 127.0.0.1 if you install subscribe server locally
2016-04-26 10:12:47 +02:00
ns0 = 'ns0.yunohost.org' # Name servers
ns1 = 'ns1.yunohost.org'
rname = 'hostmaster@yunohost.org' # Responsible person (https://tools.ietf.org/html/rfc1035#section-3.3.13)
2013-06-17 10:08:26 +02:00
allowed_operations = {
2014-05-10 00:58:20 +02:00
'.' : ['A', 'AAAA', 'TXT', 'MX'],
'pubsub.' : ['A', 'AAAA'],
'muc.' : ['A', 'AAAA'],
'vjud.' : ['A', 'AAAA'],
2013-06-17 10:08:26 +02:00
'_xmpp-client._tcp.' : ['SRV'],
'_xmpp-server._tcp.' : ['SRV']
}
2013-06-16 09:45:02 +02:00
2013-06-17 10:40:53 +02:00
### Script ###
import os
import sys
import json
from urllib import urlopen
2016-04-26 11:57:30 +02:00
# Get master key
master_key_path = os.path.join(os.path.dirname(__file__), 'master.key')
master_key = open(master_key_path).read().rstrip()
2016-04-26 12:03:18 +02:00
# Bind configuration
2013-06-17 10:40:53 +02:00
lines = ['// Generated by Dynette CRON']
2016-04-26 12:03:18 +02:00
# Loop through Dynette servers
2013-06-17 10:08:26 +02:00
for url in subs_urls:
2013-06-16 09:45:02 +02:00
lines.extend([
'key dynette. {',
' algorithm hmac-md5;',
' secret "'+ master_key +'";',
'};',
])
2016-04-26 12:03:18 +02:00
# Get available DynDNS domains
domains = json.loads(str(urlopen(url +'/domains').read()))
2013-06-16 09:45:02 +02:00
for domain in domains:
2016-04-26 12:03:18 +02:00
# Create zone database if not present
if not os.path.exists(zone_dir + domain +'.db'):
2013-06-16 14:31:31 +02:00
db_lines = [
'$ORIGIN .',
'$TTL 10 ; 10 seconds',
2016-04-26 10:12:47 +02:00
domain+'. IN SOA '+ ns0 +'. '+ rname +'. (',
2013-06-16 14:31:31 +02:00
' 18 ; serial',
' 10800 ; refresh (3 hours)',
' 3600 ; retry (1 hour)',
' 604800 ; expire (1 week)',
' 10 ; minimum (10 seconds)',
' )',
'$TTL 3600 ; 1 hour',
2016-04-26 10:12:47 +02:00
' NS '+ ns0 +'.',
2013-06-17 10:08:26 +02:00
' NS '+ ns1 +'.',
2013-06-16 14:31:31 +02:00
'',
'$ORIGIN '+ domain +'.',
]
2013-06-17 10:08:26 +02:00
with open(zone_dir + domain +'.db', 'w') as zone:
2013-06-16 14:31:31 +02:00
for line in db_lines:
zone.write(line + '\n')
2016-04-26 12:03:18 +02:00
2013-06-16 09:45:02 +02:00
lines.extend([
'zone "'+ domain +'" {',
' type master;',
2013-06-17 10:08:26 +02:00
' file "'+ zone_dir + domain +'.db"; ',
2013-06-16 09:45:02 +02:00
' update-policy {',
' grant dynette. wildcard *.'+ domain +'. ANY;',
2013-06-16 09:45:02 +02:00
])
2016-04-26 12:03:18 +02:00
# Get registered sub-domains
result = json.loads(str(urlopen(url +'/all/'+ domain).read()))
2013-06-16 09:45:02 +02:00
for entry in result:
2013-06-17 10:08:26 +02:00
for subd, type in allowed_operations.items():
if subd == '.': subd = ''
lines.append(' grant '+ entry['subdomain'] +'. name '+ subd + entry['subdomain'] +'. ' + ' '.join(type) +';')
2013-06-16 09:45:02 +02:00
lines.extend([
' };',
'};'
'',
2013-06-16 09:45:02 +02:00
])
for entry in result:
lines.extend([
2013-06-17 10:08:26 +02:00
'key '+ entry['subdomain'] +'. {',
2013-06-16 09:45:02 +02:00
' algorithm hmac-md5;',
' secret "'+ entry['public_key'] +'";',
'};',
])
2016-04-26 12:03:18 +02:00
# Backup old Bind configuration file.
2013-06-17 10:08:26 +02:00
os.system('cp '+ conf_file +' '+ conf_file +'.back')
2013-06-16 01:06:25 +02:00
2016-04-26 12:03:18 +02:00
# Write Bind configuration file.
2013-06-17 10:08:26 +02:00
with open(conf_file, 'w') as zone:
2013-06-16 01:06:25 +02:00
for line in lines:
zone.write(line + '\n')
2016-04-26 12:03:18 +02:00
# Restore ownership
2013-06-17 10:08:26 +02:00
os.system('chown -R bind:bind '+ zone_dir +' '+ conf_file)
2016-04-26 12:03:18 +02:00
# Reload Bind
2013-07-07 10:24:15 +02:00
if os.system('/usr/sbin/rndc reload') == 0:
2013-06-16 09:45:02 +02:00
exit(0)
else:
2013-06-17 10:08:26 +02:00
os.system('cp '+ conf_file +' '+ conf_file +'.bad')
os.system('cp '+ conf_file +'.back '+ conf_file)
2013-07-07 10:24:15 +02:00
os.system('/usr/sbin/rndc reload')
2013-06-16 09:45:02 +02:00
print("An error occured ! Please check daemon.log and your conf.bad")
exit(1)