2012-10-29 17:38:05 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2012-10-31 18:57:48 +01:00
|
|
|
import datetime
|
2012-11-06 09:45:28 +01:00
|
|
|
import re
|
|
|
|
from urllib import urlopen
|
2012-11-09 18:04:15 +01:00
|
|
|
from yunohost import YunoHostError, YunoHostLDAP, win_msg, colorize, validate, get_required_args
|
2012-10-29 17:38:05 +01:00
|
|
|
|
2012-11-29 14:45:06 +01:00
|
|
|
def domain_list(filter=None, limit=None, offset=None):
|
2012-10-29 17:38:05 +01:00
|
|
|
"""
|
|
|
|
List YunoHost domains
|
|
|
|
|
|
|
|
Keyword argument:
|
2012-11-29 14:45:06 +01:00
|
|
|
filter -- LDAP filter to search with
|
|
|
|
limit
|
|
|
|
offset
|
2012-10-29 17:38:05 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dict
|
|
|
|
"""
|
2012-11-09 18:04:15 +01:00
|
|
|
with YunoHostLDAP() as yldap:
|
2013-02-27 20:06:17 +01:00
|
|
|
result_list = []
|
2012-11-29 14:45:06 +01:00
|
|
|
if offset: offset = int(offset)
|
2012-11-09 18:04:15 +01:00
|
|
|
else: offset = 0
|
2012-11-29 14:45:06 +01:00
|
|
|
if limit: limit = int(limit)
|
2012-11-09 18:04:15 +01:00
|
|
|
else: limit = 1000
|
2012-11-29 14:45:06 +01:00
|
|
|
if not filter: filter = 'virtualdomain=*'
|
2012-11-09 18:04:15 +01:00
|
|
|
|
|
|
|
result = yldap.search('ou=domains,dc=yunohost,dc=org', filter, attrs=['virtualdomain'])
|
2013-02-27 20:06:17 +01:00
|
|
|
|
2012-11-09 18:04:15 +01:00
|
|
|
if result and len(result) > (0 + offset) and limit > 0:
|
|
|
|
i = 0 + offset
|
|
|
|
for domain in result[i:]:
|
2013-02-12 13:52:11 +01:00
|
|
|
if i <= limit:
|
2013-02-27 20:06:17 +01:00
|
|
|
result_list.append(domain['virtualdomain'][0])
|
2012-11-09 18:04:15 +01:00
|
|
|
i += 1
|
|
|
|
else:
|
|
|
|
raise YunoHostError(167, _("No domain found"))
|
|
|
|
|
2013-02-27 20:06:17 +01:00
|
|
|
return { 'Domains': result_list }
|
2012-11-09 18:04:15 +01:00
|
|
|
|
|
|
|
|
2012-11-29 14:45:06 +01:00
|
|
|
def domain_add(domains):
|
2012-10-29 17:38:05 +01:00
|
|
|
"""
|
|
|
|
Add one or more domains
|
|
|
|
|
|
|
|
Keyword argument:
|
2012-11-29 14:45:06 +01:00
|
|
|
domains -- List of domains to add
|
2012-10-29 17:38:05 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dict
|
|
|
|
"""
|
2012-11-09 18:04:15 +01:00
|
|
|
with YunoHostLDAP() as yldap:
|
|
|
|
attr_dict = { 'objectClass' : ['mailDomain', 'top'] }
|
|
|
|
ip = str(urlopen('http://ip.yunohost.org').read())
|
|
|
|
now = datetime.datetime.now()
|
2013-02-27 20:06:17 +01:00
|
|
|
timestamp = str(now.year) + str(now.month) + str(now.day)
|
2012-11-09 18:04:15 +01:00
|
|
|
result = []
|
|
|
|
|
2012-11-29 14:45:06 +01:00
|
|
|
if not isinstance(domains, list):
|
|
|
|
domains = [ domains ]
|
2013-02-27 20:06:17 +01:00
|
|
|
|
|
|
|
for domain in domains:
|
2012-11-09 18:04:15 +01:00
|
|
|
yldap.validate_uniqueness({ 'virtualdomain' : domain })
|
|
|
|
attr_dict['virtualdomain'] = domain
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open('/var/lib/bind/'+ domain +'.zone') as f: pass
|
|
|
|
except IOError as e:
|
|
|
|
zone_lines = [
|
|
|
|
'$TTL 38400',
|
|
|
|
domain +'. IN SOA ns.'+ domain +'. root.'+ domain +'. '+ timestamp +' 10800 3600 604800 38400',
|
|
|
|
domain +'. IN NS ns.'+ domain +'.',
|
|
|
|
domain +'. IN A '+ ip,
|
|
|
|
domain +'. IN MX 5 mail.'+ domain +'.',
|
|
|
|
domain +'. IN TXT "v=spf1 a mx a:'+ domain +' ?all"',
|
|
|
|
'mail.'+ domain +'. IN A '+ ip,
|
|
|
|
'ns.'+ domain +'. IN A '+ ip,
|
|
|
|
'root.'+ domain +'. IN A '+ ip
|
|
|
|
]
|
|
|
|
with open('/var/lib/bind/' + domain + '.zone', 'w') as zone:
|
|
|
|
for line in zone_lines:
|
|
|
|
zone.write(line + '\n')
|
|
|
|
else:
|
|
|
|
raise YunoHostError(17, _("Zone file already exists for ") + domain)
|
|
|
|
|
|
|
|
conf_lines = [
|
|
|
|
'zone "'+ domain +'" {',
|
|
|
|
' type master;',
|
|
|
|
' file "/var/lib/bind/'+ domain +'.zone";',
|
|
|
|
' allow-transfer {',
|
|
|
|
' 127.0.0.1;',
|
|
|
|
' localnets;',
|
|
|
|
' };',
|
|
|
|
'};'
|
2012-10-31 18:57:48 +01:00
|
|
|
]
|
2012-11-09 18:04:15 +01:00
|
|
|
with open('/etc/bind/named.conf.local', 'a') as conf:
|
|
|
|
for line in conf_lines:
|
|
|
|
conf.write(line + '\n')
|
|
|
|
|
|
|
|
if yldap.add('virtualdomain=' + domain + ',ou=domains', attr_dict):
|
|
|
|
result.append(domain)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
raise YunoHostError(169, _("An error occured during domain creation"))
|
2012-10-29 17:38:05 +01:00
|
|
|
|
2012-11-09 18:04:15 +01:00
|
|
|
win_msg(_("Domain(s) successfully created"))
|
2012-10-29 17:38:05 +01:00
|
|
|
|
2013-02-27 20:06:17 +01:00
|
|
|
return { 'Domains' : result }
|
2012-10-29 17:38:05 +01:00
|
|
|
|
|
|
|
|
2012-11-29 14:45:06 +01:00
|
|
|
def domain_remove(domains):
|
2012-10-29 17:47:37 +01:00
|
|
|
"""
|
|
|
|
Remove domain from LDAP
|
|
|
|
|
|
|
|
Keyword argument:
|
2012-11-29 14:45:06 +01:00
|
|
|
domains -- List of domains to remove
|
2012-10-29 17:47:37 +01:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dict
|
|
|
|
"""
|
2012-11-09 18:04:15 +01:00
|
|
|
with YunoHostLDAP() as yldap:
|
|
|
|
result = []
|
|
|
|
|
2012-11-29 14:45:06 +01:00
|
|
|
if not isinstance(domains, list):
|
|
|
|
domains = [ domains ]
|
2012-11-09 18:04:15 +01:00
|
|
|
|
2012-11-29 14:45:06 +01:00
|
|
|
for domain in domains:
|
2012-11-09 18:04:15 +01:00
|
|
|
if yldap.remove('virtualdomain=' + domain + ',ou=domains'):
|
|
|
|
try:
|
|
|
|
os.remove('/var/lib/bind/'+ domain +'.zone')
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
with open('/etc/bind/named.conf.local', 'r') as conf:
|
|
|
|
conf_lines = conf.readlines()
|
|
|
|
with open('/etc/bind/named.conf.local', 'w') as conf:
|
|
|
|
in_block = False
|
|
|
|
for line in conf_lines:
|
|
|
|
if re.search(r'^zone "'+ domain, line):
|
|
|
|
in_block = True
|
|
|
|
if in_block:
|
|
|
|
if re.search(r'^};$', line):
|
|
|
|
in_block = False
|
|
|
|
else:
|
|
|
|
conf.write(line)
|
|
|
|
result.append(domain)
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
raise YunoHostError(169, _("An error occured during domain deletion"))
|
|
|
|
|
|
|
|
win_msg(_("Domain(s) successfully deleted"))
|
|
|
|
|
|
|
|
return { 'Domains' : result }
|
2012-11-06 09:45:28 +01:00
|
|
|
|