From 9ec3e29edb5e8280cc09e0d82e1863e954e87c88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Thu, 24 Jul 2014 22:20:22 +0200 Subject: [PATCH] [fix] Update DNS zone files on main domain change --- actionsmap/yunohost.yml | 4 ---- domain.py | 9 +-------- locales/en.json | 3 ++- locales/fr.json | 3 ++- tools.py | 31 ++++++++++++++++++++++++++++++- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/actionsmap/yunohost.yml b/actionsmap/yunohost.yml index 3614e450e..75899f3cf 100644 --- a/actionsmap/yunohost.yml +++ b/actionsmap/yunohost.yml @@ -249,10 +249,6 @@ domain: pattern: - '^([a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)(\.[a-zA-Z0-9]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)*(\.[a-zA-Z]{1}([a-zA-Z0-9\-]*[a-zA-Z0-9])*)$' - pattern_domain - -m: - full: --main - help: Is the main domain - action: store_true -d: full: --dyndns help: Subscribe to the DynDNS service diff --git a/domain.py b/domain.py index 1b7bcf7df..8218a9f65 100644 --- a/domain.py +++ b/domain.py @@ -64,13 +64,12 @@ def domain_list(auth, filter=None, limit=None, offset=None): return { 'domains': result_list } -def domain_add(auth, domain, main=False, dyndns=False): +def domain_add(auth, domain, dyndns=False): """ Create a custom domain Keyword argument: domain -- Domain name to add - main -- Is the main domain dyndns -- Subscribe to DynDNS """ @@ -163,12 +162,6 @@ def domain_add(auth, domain, main=False, dyndns=False): '_xmpp-server._tcp.%s. IN SRV 0 5 5269 %s.' % (domain, domain), '_jabber._tcp.%s. IN SRV 0 5 5269 %s.' % (domain, domain), ] - if main: - zone_lines.extend([ - 'pubsub.%s. IN A %s' % (domain, ip), - 'muc.%s. IN A %s' % (domain, ip), - 'vjud.%s. IN A %s' % (domain, ip) - ]) with open('/var/lib/bind/%s.zone' % domain, 'w') as zone: for line in zone_lines: zone.write(line + '\n') diff --git a/locales/en.json b/locales/en.json index c3a534d2c..43a8e05f4 100644 --- a/locales/en.json +++ b/locales/en.json @@ -46,7 +46,8 @@ "domain_dyndns_root_unknown" : "Unknown DynDNS root domain", "domain_cert_gen_failed" : "Unable to generate certificate", "domain_exists" : "Domain already exists", - "domain_zone_exists" : "Zone file already exists", + "domain_zone_exists" : "DNS zone file already exists", + "domain_zone_not_found" : "DNS zone file not found for domain {:s}", "domain_creation_failed" : "Unable to create domain", "domain_created" : "Domain successfully created", "domain_uninstall_app_first" : "One or more apps are installed on this domain. Please uninstall them before proceed to domain removal.", diff --git a/locales/fr.json b/locales/fr.json index 1fedb512d..063279a57 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -46,7 +46,8 @@ "domain_dyndns_root_unknown" : "Domaine DynDNS principal inconnu", "domain_cert_gen_failed" : "Impossible de générer le certificat", "domain_exists" : "Le domaine existe déjà", - "domain_zone_exists" : "Le fichier de zone existe déjà", + "domain_zone_exists" : "Le fichier de zone DNS existe déjà", + "domain_zone_not_found" : "Fichier de zone DNS introuvable pour le domaine {:s}", "domain_creation_failed" : "Impossible de créer le domaine", "domain_created" : "Domaine créé avec succès", "domain_uninstall_app_first" : "Une ou plusieurs applications sont installées sur ce domaine. Veuillez d'abord les désinstaller avant de supprimer ce domaine.", diff --git a/tools.py b/tools.py index f7a7fcdc6..7728f74e6 100644 --- a/tools.py +++ b/tools.py @@ -120,7 +120,7 @@ def tools_maindomain(auth, old_domain=None, new_domain=None, dyndns=False): if not new_domain: raise MoulinetteError(errno.EINVAL, m18n.n('new_domain_required')) if new_domain not in domain_list(auth)['domains']: - domain_add(auth, new_domain, main=True) + domain_add(auth, new_domain) config_files = [ '/etc/postfix/main.cf', @@ -145,6 +145,35 @@ def tools_maindomain(auth, old_domain=None, new_domain=None, dyndns=False): for line in lines: sources.write(re.sub(r''+ old_domain +'', new_domain, line)) + ## Update DNS zone file for old and new domains + main_subdomains = ['pubsub', 'muc', 'vjud'] + try: + with open('/var/lib/bind/%s.zone' % old_domain, 'r') as f: + old_zone = f.read() + except IOError: + pass + else: + # Remove unneeded subdomains entries + for sub in main_subdomains: + old_zone = re.sub( + r'^({sub}.{domain}.|{sub})[\ \t]+(IN).*$[\n]?'.format( + sub=sub, domain=old_domain), + '', old_zone, 1, re.MULTILINE) + with open('/var/lib/bind/%s.zone' % old_domain, 'w') as f: + f.write(old_zone) + try: + with open('/var/lib/bind/%s.zone' % new_domain, 'r') as f: + new_zone = f.read() + except IOError: + msignals.display(m18n.n('domain_zone_not_found', new_domain), 'warning') + else: + # Add main subdomains entries + for sub in main_subdomains: + new_zone += '{sub} IN CNAME {domain}.\n'.format( + sub=sub, domain=new_domain) + with open('/var/lib/bind/%s.zone' % new_domain, 'w') as f: + f.write(new_zone) + os.system('rm /etc/ssl/private/yunohost_key.pem') os.system('rm /etc/ssl/certs/yunohost_crt.pem')