mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[mod] autopep8 on all files that aren't concerned by a PR
This commit is contained in:
parent
a94733c1f9
commit
aad3740933
10 changed files with 181 additions and 154 deletions
|
@ -47,7 +47,7 @@ from yunohost.hook import (
|
||||||
from yunohost.monitor import binary_to_human
|
from yunohost.monitor import binary_to_human
|
||||||
from yunohost.tools import tools_postinstall
|
from yunohost.tools import tools_postinstall
|
||||||
|
|
||||||
backup_path = '/home/yunohost.backup'
|
backup_path = '/home/yunohost.backup'
|
||||||
archives_path = '%s/archives' % backup_path
|
archives_path = '%s/archives' % backup_path
|
||||||
|
|
||||||
logger = getActionLogger('yunohost.backup')
|
logger = getActionLogger('yunohost.backup')
|
||||||
|
@ -313,8 +313,6 @@ def backup_create(name=None, description=None, output_directory=None,
|
||||||
link = "%s/%s.tar.gz" % (archives_path, name)
|
link = "%s/%s.tar.gz" % (archives_path, name)
|
||||||
os.symlink(archive_file, link)
|
os.symlink(archive_file, link)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Clean temporary directory
|
# Clean temporary directory
|
||||||
if tmp_dir != output_directory:
|
if tmp_dir != output_directory:
|
||||||
_clean_tmp_dir()
|
_clean_tmp_dir()
|
||||||
|
@ -323,7 +321,7 @@ def backup_create(name=None, description=None, output_directory=None,
|
||||||
|
|
||||||
# Return backup info
|
# Return backup info
|
||||||
info['name'] = name
|
info['name'] = name
|
||||||
return { 'archive': info }
|
return {'archive': info}
|
||||||
|
|
||||||
|
|
||||||
def backup_restore(auth, name, hooks=[], ignore_hooks=False,
|
def backup_restore(auth, name, hooks=[], ignore_hooks=False,
|
||||||
|
@ -601,7 +599,7 @@ def backup_list(with_info=False, human_readable=False):
|
||||||
d[a] = backup_info(a, human_readable=human_readable)
|
d[a] = backup_info(a, human_readable=human_readable)
|
||||||
result = d
|
result = d
|
||||||
|
|
||||||
return { 'archives': result }
|
return {'archives': result}
|
||||||
|
|
||||||
|
|
||||||
def backup_info(name, with_details=False, human_readable=False):
|
def backup_info(name, with_details=False, human_readable=False):
|
||||||
|
@ -645,7 +643,7 @@ def backup_info(name, with_details=False, human_readable=False):
|
||||||
size = info.get('size', 0)
|
size = info.get('size', 0)
|
||||||
if not size:
|
if not size:
|
||||||
tar = tarfile.open(archive_file, "r:gz")
|
tar = tarfile.open(archive_file, "r:gz")
|
||||||
size = reduce(lambda x,y: getattr(x, 'size', x)+getattr(y, 'size', y),
|
size = reduce(lambda x, y: getattr(x, 'size', x) + getattr(y, 'size', y),
|
||||||
tar.getmembers())
|
tar.getmembers())
|
||||||
tar.close()
|
tar.close()
|
||||||
if human_readable:
|
if human_readable:
|
||||||
|
@ -678,7 +676,7 @@ def backup_delete(name):
|
||||||
archive_file = '%s/%s.tar.gz' % (archives_path, name)
|
archive_file = '%s/%s.tar.gz' % (archives_path, name)
|
||||||
|
|
||||||
info_file = "%s/%s.info.json" % (archives_path, name)
|
info_file = "%s/%s.info.json" % (archives_path, name)
|
||||||
for backup_file in [archive_file,info_file]:
|
for backup_file in [archive_file, info_file]:
|
||||||
if not os.path.isfile(backup_file):
|
if not os.path.isfile(backup_file):
|
||||||
raise MoulinetteError(errno.EIO,
|
raise MoulinetteError(errno.EIO,
|
||||||
m18n.n('backup_archive_name_unknown', name=backup_file))
|
m18n.n('backup_archive_name_unknown', name=backup_file))
|
||||||
|
|
|
@ -193,9 +193,9 @@ def _certificate_install_selfsigned(domain_list, force=False):
|
||||||
# and self-sign the cert
|
# and self-sign the cert
|
||||||
commands = [
|
commands = [
|
||||||
"openssl req -new -config %s -days 3650 -out %s -keyout %s -nodes -batch"
|
"openssl req -new -config %s -days 3650 -out %s -keyout %s -nodes -batch"
|
||||||
% (conf_file, csr_file, key_file),
|
% (conf_file, csr_file, key_file),
|
||||||
"openssl ca -config %s -days 3650 -in %s -out %s -batch"
|
"openssl ca -config %s -days 3650 -in %s -out %s -batch"
|
||||||
% (conf_file, csr_file, crt_file),
|
% (conf_file, csr_file, crt_file),
|
||||||
]
|
]
|
||||||
|
|
||||||
for command in commands:
|
for command in commands:
|
||||||
|
@ -528,7 +528,7 @@ def _fetch_and_enable_new_certificate(domain, staging=False):
|
||||||
CA=certification_authority)
|
CA=certification_authority)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
if "urn:acme:error:rateLimited" in str(e):
|
if "urn:acme:error:rateLimited" in str(e):
|
||||||
raise MoulinetteError(errno.EINVAL, m18n.n(
|
raise MoulinetteError(errno.EINVAL, m18n.n(
|
||||||
'certmanager_hit_rate_limit', domain=domain))
|
'certmanager_hit_rate_limit', domain=domain))
|
||||||
else:
|
else:
|
||||||
logger.error(str(e))
|
logger.error(str(e))
|
||||||
|
|
|
@ -66,10 +66,10 @@ def domain_list(auth, filter=None, limit=None, offset=None):
|
||||||
result = auth.search('ou=domains,dc=yunohost,dc=org', filter, ['virtualdomain'])
|
result = auth.search('ou=domains,dc=yunohost,dc=org', filter, ['virtualdomain'])
|
||||||
|
|
||||||
if len(result) > offset and limit > 0:
|
if len(result) > offset and limit > 0:
|
||||||
for domain in result[offset:offset+limit]:
|
for domain in result[offset:offset + limit]:
|
||||||
result_list.append(domain['virtualdomain'][0])
|
result_list.append(domain['virtualdomain'][0])
|
||||||
|
|
||||||
return { 'domains': result_list }
|
return {'domains': result_list}
|
||||||
|
|
||||||
|
|
||||||
def domain_add(auth, domain, dyndns=False):
|
def domain_add(auth, domain, dyndns=False):
|
||||||
|
@ -83,7 +83,7 @@ def domain_add(auth, domain, dyndns=False):
|
||||||
"""
|
"""
|
||||||
from yunohost.hook import hook_callback
|
from yunohost.hook import hook_callback
|
||||||
|
|
||||||
attr_dict = { 'objectClass' : ['mailDomain', 'top'] }
|
attr_dict = {'objectClass': ['mailDomain', 'top']}
|
||||||
|
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
timestamp = str(now.year) + str(now.month) + str(now.day)
|
timestamp = str(now.year) + str(now.month) + str(now.day)
|
||||||
|
@ -103,7 +103,7 @@ def domain_add(auth, domain, dyndns=False):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
dyndomains = json.loads(r.text)
|
dyndomains = json.loads(r.text)
|
||||||
dyndomain = '.'.join(domain.split('.')[1:])
|
dyndomain = '.'.join(domain.split('.')[1:])
|
||||||
if dyndomain in dyndomains:
|
if dyndomain in dyndomains:
|
||||||
if os.path.exists('/etc/cron.d/yunohost-dyndns'):
|
if os.path.exists('/etc/cron.d/yunohost-dyndns'):
|
||||||
raise MoulinetteError(errno.EPERM,
|
raise MoulinetteError(errno.EPERM,
|
||||||
|
@ -113,7 +113,6 @@ def domain_add(auth, domain, dyndns=False):
|
||||||
raise MoulinetteError(errno.EINVAL,
|
raise MoulinetteError(errno.EINVAL,
|
||||||
m18n.n('domain_dyndns_root_unknown'))
|
m18n.n('domain_dyndns_root_unknown'))
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
yunohost.certificate._certificate_install_selfsigned([domain], False)
|
yunohost.certificate._certificate_install_selfsigned([domain], False)
|
||||||
|
|
||||||
|
@ -122,7 +121,6 @@ def domain_add(auth, domain, dyndns=False):
|
||||||
except MoulinetteError:
|
except MoulinetteError:
|
||||||
raise MoulinetteError(errno.EEXIST, m18n.n('domain_exists'))
|
raise MoulinetteError(errno.EEXIST, m18n.n('domain_exists'))
|
||||||
|
|
||||||
|
|
||||||
attr_dict['virtualdomain'] = domain
|
attr_dict['virtualdomain'] = domain
|
||||||
|
|
||||||
if not auth.add('virtualdomain=%s,ou=domains' % domain, attr_dict):
|
if not auth.add('virtualdomain=%s,ou=domains' % domain, attr_dict):
|
||||||
|
@ -133,11 +131,14 @@ def domain_add(auth, domain, dyndns=False):
|
||||||
service_regen_conf(names=[
|
service_regen_conf(names=[
|
||||||
'nginx', 'metronome', 'dnsmasq', 'rmilter'])
|
'nginx', 'metronome', 'dnsmasq', 'rmilter'])
|
||||||
os.system('yunohost app ssowatconf > /dev/null 2>&1')
|
os.system('yunohost app ssowatconf > /dev/null 2>&1')
|
||||||
except IOError: pass
|
except IOError:
|
||||||
|
pass
|
||||||
except:
|
except:
|
||||||
# Force domain removal silently
|
# Force domain removal silently
|
||||||
try: domain_remove(auth, domain, True)
|
try:
|
||||||
except: pass
|
domain_remove(auth, domain, True)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
raise
|
raise
|
||||||
|
|
||||||
hook_callback('post_domain_add', args=[domain])
|
hook_callback('post_domain_add', args=[domain])
|
||||||
|
@ -165,7 +166,7 @@ def domain_remove(auth, domain, force=False):
|
||||||
|
|
||||||
# Check if apps are installed on the domain
|
# Check if apps are installed on the domain
|
||||||
for app in os.listdir('/etc/yunohost/apps/'):
|
for app in os.listdir('/etc/yunohost/apps/'):
|
||||||
with open('/etc/yunohost/apps/' + app +'/settings.yml') as f:
|
with open('/etc/yunohost/apps/' + app + '/settings.yml') as f:
|
||||||
try:
|
try:
|
||||||
app_domain = yaml.load(f)['domain']
|
app_domain = yaml.load(f)['domain']
|
||||||
except:
|
except:
|
||||||
|
@ -224,13 +225,13 @@ def domain_dns_conf(domain, ttl=None):
|
||||||
"muc {ttl} IN CNAME @\n"
|
"muc {ttl} IN CNAME @\n"
|
||||||
"pubsub {ttl} IN CNAME @\n"
|
"pubsub {ttl} IN CNAME @\n"
|
||||||
"vjud {ttl} IN CNAME @\n"
|
"vjud {ttl} IN CNAME @\n"
|
||||||
).format(ttl=ttl, domain=domain)
|
).format(ttl=ttl, domain=domain)
|
||||||
|
|
||||||
# Email
|
# Email
|
||||||
result += ('\n'
|
result += ('\n'
|
||||||
'@ {ttl} IN MX 10 {domain}.\n'
|
'@ {ttl} IN MX 10 {domain}.\n'
|
||||||
'@ {ttl} IN TXT "v=spf1 a mx ip4:{ip4}'
|
'@ {ttl} IN TXT "v=spf1 a mx ip4:{ip4}'
|
||||||
).format(ttl=ttl, domain=domain, ip4=ip4)
|
).format(ttl=ttl, domain=domain, ip4=ip4)
|
||||||
if ip6 is not None:
|
if ip6 is not None:
|
||||||
result += ' ip6:{ip6}'.format(ip6=ip6)
|
result += ' ip6:{ip6}'.format(ip6=ip6)
|
||||||
result += ' -all"'
|
result += ' -all"'
|
||||||
|
@ -246,7 +247,7 @@ def domain_dns_conf(domain, ttl=None):
|
||||||
r'^(?P<host>[a-z_\-\.]+)[\s]+([0-9]+[\s]+)?IN[\s]+TXT[\s]+[^"]*'
|
r'^(?P<host>[a-z_\-\.]+)[\s]+([0-9]+[\s]+)?IN[\s]+TXT[\s]+[^"]*'
|
||||||
'(?=.*(;[\s]*|")v=(?P<v>[^";]+))'
|
'(?=.*(;[\s]*|")v=(?P<v>[^";]+))'
|
||||||
'(?=.*(;[\s]*|")k=(?P<k>[^";]+))'
|
'(?=.*(;[\s]*|")k=(?P<k>[^";]+))'
|
||||||
'(?=.*(;[\s]*|")p=(?P<p>[^";]+))'), dkim_content, re.M|re.S
|
'(?=.*(;[\s]*|")p=(?P<p>[^";]+))'), dkim_content, re.M | re.S
|
||||||
)
|
)
|
||||||
if dkim:
|
if dkim:
|
||||||
result += '\n{host}. {ttl} IN TXT "v={v}; k={k}; p={p}"'.format(
|
result += '\n{host}. {ttl} IN TXT "v={v}; k={k}; p={p}"'.format(
|
||||||
|
@ -296,6 +297,7 @@ def _get_maindomain():
|
||||||
maindomain = f.readline().rstrip()
|
maindomain = f.readline().rstrip()
|
||||||
return maindomain
|
return maindomain
|
||||||
|
|
||||||
|
|
||||||
def _set_maindomain(domain):
|
def _set_maindomain(domain):
|
||||||
with open('/etc/yunohost/current_host', 'w') as f:
|
with open('/etc/yunohost/current_host', 'w') as f:
|
||||||
f.write(domain)
|
f.write(domain)
|
||||||
|
|
|
@ -94,7 +94,7 @@ def dyndns_subscribe(subscribe_host="dyndns.yunohost.org", domain=None, key=None
|
||||||
|
|
||||||
logger.info(m18n.n('dyndns_key_generating'))
|
logger.info(m18n.n('dyndns_key_generating'))
|
||||||
|
|
||||||
os.system('cd /etc/yunohost/dyndns && ' \
|
os.system('cd /etc/yunohost/dyndns && '
|
||||||
'dnssec-keygen -a hmac-md5 -b 128 -r /dev/urandom -n USER %s' % domain)
|
'dnssec-keygen -a hmac-md5 -b 128 -r /dev/urandom -n USER %s' % domain)
|
||||||
os.system('chmod 600 /etc/yunohost/dyndns/*.key /etc/yunohost/dyndns/*.private')
|
os.system('chmod 600 /etc/yunohost/dyndns/*.key /etc/yunohost/dyndns/*.private')
|
||||||
|
|
||||||
|
@ -104,12 +104,14 @@ def dyndns_subscribe(subscribe_host="dyndns.yunohost.org", domain=None, key=None
|
||||||
|
|
||||||
# Send subscription
|
# Send subscription
|
||||||
try:
|
try:
|
||||||
r = requests.post('https://%s/key/%s' % (subscribe_host, base64.b64encode(key)), data={ 'subdomain': domain })
|
r = requests.post('https://%s/key/%s' % (subscribe_host, base64.b64encode(key)), data={'subdomain': domain})
|
||||||
except requests.ConnectionError:
|
except requests.ConnectionError:
|
||||||
raise MoulinetteError(errno.ENETUNREACH, m18n.n('no_internet_connection'))
|
raise MoulinetteError(errno.ENETUNREACH, m18n.n('no_internet_connection'))
|
||||||
if r.status_code != 201:
|
if r.status_code != 201:
|
||||||
try: error = json.loads(r.text)['error']
|
try:
|
||||||
except: error = "Server error"
|
error = json.loads(r.text)['error']
|
||||||
|
except:
|
||||||
|
error = "Server error"
|
||||||
raise MoulinetteError(errno.EPERM,
|
raise MoulinetteError(errno.EPERM,
|
||||||
m18n.n('dyndns_registration_failed', error=error))
|
m18n.n('dyndns_registration_failed', error=error))
|
||||||
|
|
||||||
|
@ -204,33 +206,33 @@ def dyndns_update(dyn_host="dyndns.yunohost.org", domain=None, key=None,
|
||||||
lines = [
|
lines = [
|
||||||
'server %s' % dyn_host,
|
'server %s' % dyn_host,
|
||||||
'zone %s' % host,
|
'zone %s' % host,
|
||||||
'update delete %s. A' % domain,
|
'update delete %s. A' % domain,
|
||||||
'update delete %s. AAAA' % domain,
|
'update delete %s. AAAA' % domain,
|
||||||
'update delete %s. MX' % domain,
|
'update delete %s. MX' % domain,
|
||||||
'update delete %s. TXT' % domain,
|
'update delete %s. TXT' % domain,
|
||||||
'update delete pubsub.%s. A' % domain,
|
'update delete pubsub.%s. A' % domain,
|
||||||
'update delete pubsub.%s. AAAA' % domain,
|
'update delete pubsub.%s. AAAA' % domain,
|
||||||
'update delete muc.%s. A' % domain,
|
'update delete muc.%s. A' % domain,
|
||||||
'update delete muc.%s. AAAA' % domain,
|
'update delete muc.%s. AAAA' % domain,
|
||||||
'update delete vjud.%s. A' % domain,
|
'update delete vjud.%s. A' % domain,
|
||||||
'update delete vjud.%s. AAAA' % domain,
|
'update delete vjud.%s. AAAA' % domain,
|
||||||
'update delete _xmpp-client._tcp.%s. SRV' % domain,
|
'update delete _xmpp-client._tcp.%s. SRV' % domain,
|
||||||
'update delete _xmpp-server._tcp.%s. SRV' % domain,
|
'update delete _xmpp-server._tcp.%s. SRV' % domain,
|
||||||
'update add %s. 1800 A %s' % (domain, ipv4),
|
'update add %s. 1800 A %s' % (domain, ipv4),
|
||||||
'update add %s. 14400 MX 5 %s.' % (domain, domain),
|
'update add %s. 14400 MX 5 %s.' % (domain, domain),
|
||||||
'update add %s. 14400 TXT "v=spf1 a mx -all"' % domain,
|
'update add %s. 14400 TXT "v=spf1 a mx -all"' % domain,
|
||||||
'update add pubsub.%s. 1800 A %s' % (domain, ipv4),
|
'update add pubsub.%s. 1800 A %s' % (domain, ipv4),
|
||||||
'update add muc.%s. 1800 A %s' % (domain, ipv4),
|
'update add muc.%s. 1800 A %s' % (domain, ipv4),
|
||||||
'update add vjud.%s. 1800 A %s' % (domain, ipv4),
|
'update add vjud.%s. 1800 A %s' % (domain, ipv4),
|
||||||
'update add _xmpp-client._tcp.%s. 14400 SRV 0 5 5222 %s.' % (domain, domain),
|
'update add _xmpp-client._tcp.%s. 14400 SRV 0 5 5222 %s.' % (domain, domain),
|
||||||
'update add _xmpp-server._tcp.%s. 14400 SRV 0 5 5269 %s.' % (domain, domain)
|
'update add _xmpp-server._tcp.%s. 14400 SRV 0 5 5269 %s.' % (domain, domain)
|
||||||
]
|
]
|
||||||
if ipv6 is not None:
|
if ipv6 is not None:
|
||||||
lines += [
|
lines += [
|
||||||
'update add %s. 1800 AAAA %s' % (domain, ipv6),
|
'update add %s. 1800 AAAA %s' % (domain, ipv6),
|
||||||
'update add pubsub.%s. 1800 AAAA %s' % (domain, ipv6),
|
'update add pubsub.%s. 1800 AAAA %s' % (domain, ipv6),
|
||||||
'update add muc.%s. 1800 AAAA %s' % (domain, ipv6),
|
'update add muc.%s. 1800 AAAA %s' % (domain, ipv6),
|
||||||
'update add vjud.%s. 1800 AAAA %s' % (domain, ipv6),
|
'update add vjud.%s. 1800 AAAA %s' % (domain, ipv6),
|
||||||
]
|
]
|
||||||
lines += [
|
lines += [
|
||||||
'show',
|
'show',
|
||||||
|
|
|
@ -67,14 +67,14 @@ def firewall_allow(protocol, port, ipv4_only=False, ipv6_only=False,
|
||||||
# Validate protocols
|
# Validate protocols
|
||||||
protocols = ['TCP', 'UDP']
|
protocols = ['TCP', 'UDP']
|
||||||
if protocol != 'Both' and protocol in protocols:
|
if protocol != 'Both' and protocol in protocols:
|
||||||
protocols = [protocol,]
|
protocols = [protocol, ]
|
||||||
|
|
||||||
# Validate IP versions
|
# Validate IP versions
|
||||||
ipvs = ['ipv4', 'ipv6']
|
ipvs = ['ipv4', 'ipv6']
|
||||||
if ipv4_only and not ipv6_only:
|
if ipv4_only and not ipv6_only:
|
||||||
ipvs = ['ipv4',]
|
ipvs = ['ipv4', ]
|
||||||
elif ipv6_only and not ipv4_only:
|
elif ipv6_only and not ipv4_only:
|
||||||
ipvs = ['ipv6',]
|
ipvs = ['ipv6', ]
|
||||||
|
|
||||||
for p in protocols:
|
for p in protocols:
|
||||||
# Iterate over IP versions to add port
|
# Iterate over IP versions to add port
|
||||||
|
@ -117,18 +117,18 @@ def firewall_disallow(protocol, port, ipv4_only=False, ipv6_only=False,
|
||||||
# Validate protocols
|
# Validate protocols
|
||||||
protocols = ['TCP', 'UDP']
|
protocols = ['TCP', 'UDP']
|
||||||
if protocol != 'Both' and protocol in protocols:
|
if protocol != 'Both' and protocol in protocols:
|
||||||
protocols = [protocol,]
|
protocols = [protocol, ]
|
||||||
|
|
||||||
# Validate IP versions and UPnP
|
# Validate IP versions and UPnP
|
||||||
ipvs = ['ipv4', 'ipv6']
|
ipvs = ['ipv4', 'ipv6']
|
||||||
upnp = True
|
upnp = True
|
||||||
if ipv4_only and ipv6_only:
|
if ipv4_only and ipv6_only:
|
||||||
upnp = True # automatically disallow UPnP
|
upnp = True # automatically disallow UPnP
|
||||||
elif ipv4_only:
|
elif ipv4_only:
|
||||||
ipvs = ['ipv4',]
|
ipvs = ['ipv4', ]
|
||||||
upnp = upnp_only
|
upnp = upnp_only
|
||||||
elif ipv6_only:
|
elif ipv6_only:
|
||||||
ipvs = ['ipv6',]
|
ipvs = ['ipv6', ]
|
||||||
upnp = upnp_only
|
upnp = upnp_only
|
||||||
elif upnp_only:
|
elif upnp_only:
|
||||||
ipvs = []
|
ipvs = []
|
||||||
|
@ -178,7 +178,7 @@ def firewall_list(raw=False, by_ip_version=False, list_forwarded=False):
|
||||||
ports = sorted(set(ports['ipv4']) | set(ports['ipv6']))
|
ports = sorted(set(ports['ipv4']) | set(ports['ipv6']))
|
||||||
|
|
||||||
# Format returned dict
|
# Format returned dict
|
||||||
ret = { "opened_ports": ports }
|
ret = {"opened_ports": ports}
|
||||||
if list_forwarded:
|
if list_forwarded:
|
||||||
# Combine TCP and UDP forwarded ports
|
# Combine TCP and UDP forwarded ports
|
||||||
ret['forwarded_ports'] = sorted(
|
ret['forwarded_ports'] = sorted(
|
||||||
|
@ -224,8 +224,8 @@ def firewall_reload(skip_upnp=False):
|
||||||
# Iterate over ports and add rule
|
# Iterate over ports and add rule
|
||||||
for protocol in ['TCP', 'UDP']:
|
for protocol in ['TCP', 'UDP']:
|
||||||
for port in firewall['ipv4'][protocol]:
|
for port in firewall['ipv4'][protocol]:
|
||||||
rules.append("iptables -w -A INPUT -p %s --dport %s -j ACCEPT" \
|
rules.append("iptables -w -A INPUT -p %s --dport %s -j ACCEPT"
|
||||||
% (protocol, process.quote(str(port))))
|
% (protocol, process.quote(str(port))))
|
||||||
rules += [
|
rules += [
|
||||||
"iptables -w -A INPUT -i lo -j ACCEPT",
|
"iptables -w -A INPUT -i lo -j ACCEPT",
|
||||||
"iptables -w -A INPUT -p icmp -j ACCEPT",
|
"iptables -w -A INPUT -p icmp -j ACCEPT",
|
||||||
|
@ -253,8 +253,8 @@ def firewall_reload(skip_upnp=False):
|
||||||
# Iterate over ports and add rule
|
# Iterate over ports and add rule
|
||||||
for protocol in ['TCP', 'UDP']:
|
for protocol in ['TCP', 'UDP']:
|
||||||
for port in firewall['ipv6'][protocol]:
|
for port in firewall['ipv6'][protocol]:
|
||||||
rules.append("ip6tables -w -A INPUT -p %s --dport %s -j ACCEPT" \
|
rules.append("ip6tables -w -A INPUT -p %s --dport %s -j ACCEPT"
|
||||||
% (protocol, process.quote(str(port))))
|
% (protocol, process.quote(str(port))))
|
||||||
rules += [
|
rules += [
|
||||||
"ip6tables -w -A INPUT -i lo -j ACCEPT",
|
"ip6tables -w -A INPUT -i lo -j ACCEPT",
|
||||||
"ip6tables -w -A INPUT -p icmpv6 -j ACCEPT",
|
"ip6tables -w -A INPUT -p icmpv6 -j ACCEPT",
|
||||||
|
@ -308,13 +308,14 @@ def firewall_upnp(action='status', no_refresh=False):
|
||||||
try:
|
try:
|
||||||
# Remove old cron job
|
# Remove old cron job
|
||||||
os.remove('/etc/cron.d/yunohost-firewall')
|
os.remove('/etc/cron.d/yunohost-firewall')
|
||||||
except: pass
|
except:
|
||||||
|
pass
|
||||||
action = 'status'
|
action = 'status'
|
||||||
no_refresh = False
|
no_refresh = False
|
||||||
|
|
||||||
if action == 'status' and no_refresh:
|
if action == 'status' and no_refresh:
|
||||||
# Only return current state
|
# Only return current state
|
||||||
return { 'enabled': enabled }
|
return {'enabled': enabled}
|
||||||
elif action == 'enable' or (enabled and action == 'status'):
|
elif action == 'enable' or (enabled and action == 'status'):
|
||||||
# Add cron job
|
# Add cron job
|
||||||
with open(upnp_cron_job, 'w+') as f:
|
with open(upnp_cron_job, 'w+') as f:
|
||||||
|
@ -330,7 +331,8 @@ def firewall_upnp(action='status', no_refresh=False):
|
||||||
try:
|
try:
|
||||||
# Remove cron job
|
# Remove cron job
|
||||||
os.remove(upnp_cron_job)
|
os.remove(upnp_cron_job)
|
||||||
except: pass
|
except:
|
||||||
|
pass
|
||||||
enabled = False
|
enabled = False
|
||||||
if action == 'status':
|
if action == 'status':
|
||||||
no_refresh = True
|
no_refresh = True
|
||||||
|
@ -364,7 +366,8 @@ def firewall_upnp(action='status', no_refresh=False):
|
||||||
if upnpc.getspecificportmapping(port, protocol):
|
if upnpc.getspecificportmapping(port, protocol):
|
||||||
try:
|
try:
|
||||||
upnpc.deleteportmapping(port, protocol)
|
upnpc.deleteportmapping(port, protocol)
|
||||||
except: pass
|
except:
|
||||||
|
pass
|
||||||
if not enabled:
|
if not enabled:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
|
@ -403,7 +406,7 @@ def firewall_upnp(action='status', no_refresh=False):
|
||||||
|
|
||||||
if action == 'enable' and not enabled:
|
if action == 'enable' and not enabled:
|
||||||
raise MoulinetteError(errno.ENXIO, m18n.n('upnp_port_open_failed'))
|
raise MoulinetteError(errno.ENXIO, m18n.n('upnp_port_open_failed'))
|
||||||
return { 'enabled': enabled }
|
return {'enabled': enabled}
|
||||||
|
|
||||||
|
|
||||||
def firewall_stop():
|
def firewall_stop():
|
||||||
|
@ -444,12 +447,14 @@ def _get_ssh_port(default=22):
|
||||||
pass
|
pass
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
def _update_firewall_file(rules):
|
def _update_firewall_file(rules):
|
||||||
"""Make a backup and write new rules to firewall file"""
|
"""Make a backup and write new rules to firewall file"""
|
||||||
os.system("cp {0} {0}.old".format(firewall_file))
|
os.system("cp {0} {0}.old".format(firewall_file))
|
||||||
with open(firewall_file, 'w') as f:
|
with open(firewall_file, 'w') as f:
|
||||||
yaml.safe_dump(rules, f, default_flow_style=False)
|
yaml.safe_dump(rules, f, default_flow_style=False)
|
||||||
|
|
||||||
|
|
||||||
def _on_rule_command_error(returncode, cmd, output):
|
def _on_rule_command_error(returncode, cmd, output):
|
||||||
"""Callback for rules commands error"""
|
"""Callback for rules commands error"""
|
||||||
# Log error and continue commands execution
|
# Log error and continue commands execution
|
||||||
|
|
|
@ -44,8 +44,8 @@ from yunohost.domain import get_public_ip
|
||||||
|
|
||||||
logger = getActionLogger('yunohost.monitor')
|
logger = getActionLogger('yunohost.monitor')
|
||||||
|
|
||||||
glances_uri = 'http://127.0.0.1:61209'
|
glances_uri = 'http://127.0.0.1:61209'
|
||||||
stats_path = '/var/lib/yunohost/stats'
|
stats_path = '/var/lib/yunohost/stats'
|
||||||
crontab_path = '/etc/cron.d/yunohost-monitor'
|
crontab_path = '/etc/cron.d/yunohost-monitor'
|
||||||
|
|
||||||
|
|
||||||
|
@ -87,13 +87,13 @@ def monitor_disk(units=None, mountpoint=None, human_readable=False):
|
||||||
# Retrieve monitoring for unit(s)
|
# Retrieve monitoring for unit(s)
|
||||||
for u in units:
|
for u in units:
|
||||||
if u == 'io':
|
if u == 'io':
|
||||||
## Define setter
|
# Define setter
|
||||||
if len(units) > 1:
|
if len(units) > 1:
|
||||||
def _set(dn, dvalue):
|
def _set(dn, dvalue):
|
||||||
try:
|
try:
|
||||||
result[dn][u] = dvalue
|
result[dn][u] = dvalue
|
||||||
except KeyError:
|
except KeyError:
|
||||||
result[dn] = { u: dvalue }
|
result[dn] = {u: dvalue}
|
||||||
else:
|
else:
|
||||||
def _set(dn, dvalue):
|
def _set(dn, dvalue):
|
||||||
result[dn] = dvalue
|
result[dn] = dvalue
|
||||||
|
@ -111,13 +111,13 @@ def monitor_disk(units=None, mountpoint=None, human_readable=False):
|
||||||
for dname in devices_names:
|
for dname in devices_names:
|
||||||
_set(dname, 'not-available')
|
_set(dname, 'not-available')
|
||||||
elif u == 'filesystem':
|
elif u == 'filesystem':
|
||||||
## Define setter
|
# Define setter
|
||||||
if len(units) > 1:
|
if len(units) > 1:
|
||||||
def _set(dn, dvalue):
|
def _set(dn, dvalue):
|
||||||
try:
|
try:
|
||||||
result[dn][u] = dvalue
|
result[dn][u] = dvalue
|
||||||
except KeyError:
|
except KeyError:
|
||||||
result[dn] = { u: dvalue }
|
result[dn] = {u: dvalue}
|
||||||
else:
|
else:
|
||||||
def _set(dn, dvalue):
|
def _set(dn, dvalue):
|
||||||
result[dn] = dvalue
|
result[dn] = dvalue
|
||||||
|
@ -183,11 +183,11 @@ def monitor_network(units=None, human_readable=False):
|
||||||
smtp_check = m18n.n('network_check_smtp_ko')
|
smtp_check = m18n.n('network_check_smtp_ko')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
answers = dns.resolver.query(domain,'MX')
|
answers = dns.resolver.query(domain, 'MX')
|
||||||
mx_check = {}
|
mx_check = {}
|
||||||
i = 0
|
i = 0
|
||||||
for server in answers:
|
for server in answers:
|
||||||
mx_id = 'mx%s' %i
|
mx_id = 'mx%s' % i
|
||||||
mx_check[mx_id] = server
|
mx_check[mx_id] = server
|
||||||
i = i + 1
|
i = i + 1
|
||||||
except:
|
except:
|
||||||
|
@ -307,7 +307,7 @@ def monitor_update_stats(period):
|
||||||
|
|
||||||
stats = _retrieve_stats(period)
|
stats = _retrieve_stats(period)
|
||||||
if not stats:
|
if not stats:
|
||||||
stats = { 'disk': {}, 'network': {}, 'system': {}, 'timestamp': [] }
|
stats = {'disk': {}, 'network': {}, 'system': {}, 'timestamp': []}
|
||||||
|
|
||||||
monitor = None
|
monitor = None
|
||||||
# Get monitoring stats
|
# Get monitoring stats
|
||||||
|
@ -357,7 +357,7 @@ def monitor_update_stats(period):
|
||||||
if 'usage' in stats['network'] and iname in stats['network']['usage']:
|
if 'usage' in stats['network'] and iname in stats['network']['usage']:
|
||||||
curr = stats['network']['usage'][iname]
|
curr = stats['network']['usage'][iname]
|
||||||
net_usage[iname] = _append_to_stats(curr, values, 'time_since_update')
|
net_usage[iname] = _append_to_stats(curr, values, 'time_since_update')
|
||||||
stats['network'] = { 'usage': net_usage, 'infos': monitor['network']['infos'] }
|
stats['network'] = {'usage': net_usage, 'infos': monitor['network']['infos']}
|
||||||
|
|
||||||
# Append system stats
|
# Append system stats
|
||||||
for unit, values in monitor['system'].items():
|
for unit, values in monitor['system'].items():
|
||||||
|
@ -421,7 +421,7 @@ def monitor_enable(with_stats=False):
|
||||||
rules = ('*/5 * * * * root {cmd} day >> /dev/null\n'
|
rules = ('*/5 * * * * root {cmd} day >> /dev/null\n'
|
||||||
'3 * * * * root {cmd} week >> /dev/null\n'
|
'3 * * * * root {cmd} week >> /dev/null\n'
|
||||||
'6 */4 * * * root {cmd} month >> /dev/null').format(
|
'6 */4 * * * root {cmd} month >> /dev/null').format(
|
||||||
cmd='/usr/bin/yunohost --quiet monitor update-stats')
|
cmd='/usr/bin/yunohost --quiet monitor update-stats')
|
||||||
with open(crontab_path, 'w') as f:
|
with open(crontab_path, 'w') as f:
|
||||||
f.write(rules)
|
f.write(rules)
|
||||||
|
|
||||||
|
@ -530,7 +530,7 @@ def binary_to_human(n, customary=False):
|
||||||
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
|
symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
|
||||||
prefix = {}
|
prefix = {}
|
||||||
for i, s in enumerate(symbols):
|
for i, s in enumerate(symbols):
|
||||||
prefix[s] = 1 << (i+1)*10
|
prefix[s] = 1 << (i + 1) * 10
|
||||||
for s in reversed(symbols):
|
for s in reversed(symbols):
|
||||||
if n >= prefix[s]:
|
if n >= prefix[s]:
|
||||||
value = float(n) / prefix[s]
|
value = float(n) / prefix[s]
|
||||||
|
@ -590,7 +590,7 @@ def _save_stats(stats, period, date=None):
|
||||||
# Limit stats
|
# Limit stats
|
||||||
if date is None:
|
if date is None:
|
||||||
t = stats['timestamp']
|
t = stats['timestamp']
|
||||||
limit = { 'day': 86400, 'week': 604800, 'month': 2419200 }
|
limit = {'day': 86400, 'week': 604800, 'month': 2419200}
|
||||||
if (t[len(t) - 1] - t[0]) > limit[period]:
|
if (t[len(t) - 1] - t[0]) > limit[period]:
|
||||||
begin = t[len(t) - 1] - limit[period]
|
begin = t[len(t) - 1] - limit[period]
|
||||||
stats = _filter_stats(stats, begin)
|
stats = _filter_stats(stats, begin)
|
||||||
|
@ -612,7 +612,7 @@ def _monitor_all(period=None, since=None):
|
||||||
since -- Timestamp of the stats beginning
|
since -- Timestamp of the stats beginning
|
||||||
|
|
||||||
"""
|
"""
|
||||||
result = { 'disk': {}, 'network': {}, 'system': {} }
|
result = {'disk': {}, 'network': {}, 'system': {}}
|
||||||
|
|
||||||
# Real-time stats
|
# Real-time stats
|
||||||
if period == 'day' and since is None:
|
if period == 'day' and since is None:
|
||||||
|
@ -697,7 +697,7 @@ def _calculate_stats_mean(stats):
|
||||||
s[k] = _mean(v, t, ts)
|
s[k] = _mean(v, t, ts)
|
||||||
elif isinstance(v, list):
|
elif isinstance(v, list):
|
||||||
try:
|
try:
|
||||||
nums = [ float(x * t[i]) for i, x in enumerate(v) ]
|
nums = [float(x * t[i]) for i, x in enumerate(v)]
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -60,9 +60,9 @@ def service_add(name, status=None, log=None, runlevel=None):
|
||||||
services = _get_services()
|
services = _get_services()
|
||||||
|
|
||||||
if not status:
|
if not status:
|
||||||
services[name] = { 'status': 'service' }
|
services[name] = {'status': 'service'}
|
||||||
else:
|
else:
|
||||||
services[name] = { 'status': status }
|
services[name] = {'status': status}
|
||||||
|
|
||||||
if log is not None:
|
if log is not None:
|
||||||
services[name]['log'] = log
|
services[name]['log'] = log
|
||||||
|
@ -202,7 +202,7 @@ def service_status(names=[]):
|
||||||
|
|
||||||
status = None
|
status = None
|
||||||
if 'status' not in services[name] or \
|
if 'status' not in services[name] or \
|
||||||
services[name]['status'] == 'service':
|
services[name]['status'] == 'service':
|
||||||
status = 'service %s status' % name
|
status = 'service %s status' % name
|
||||||
else:
|
else:
|
||||||
status = str(services[name]['status'])
|
status = str(services[name]['status'])
|
||||||
|
@ -211,7 +211,7 @@ def service_status(names=[]):
|
||||||
if 'runlevel' in services[name].keys():
|
if 'runlevel' in services[name].keys():
|
||||||
runlevel = int(services[name]['runlevel'])
|
runlevel = int(services[name]['runlevel'])
|
||||||
|
|
||||||
result[name] = { 'status': 'unknown', 'loaded': 'unknown' }
|
result[name] = {'status': 'unknown', 'loaded': 'unknown'}
|
||||||
|
|
||||||
# Retrieve service status
|
# Retrieve service status
|
||||||
try:
|
try:
|
||||||
|
@ -261,7 +261,7 @@ def service_log(name, number=50):
|
||||||
|
|
||||||
for log_path in log_list:
|
for log_path in log_list:
|
||||||
if os.path.isdir(log_path):
|
if os.path.isdir(log_path):
|
||||||
for log in [ f for f in os.listdir(log_path) if os.path.isfile(os.path.join(log_path, f)) and f[-4:] == '.log' ]:
|
for log in [f for f in os.listdir(log_path) if os.path.isfile(os.path.join(log_path, f)) and f[-4:] == '.log']:
|
||||||
result[os.path.join(log_path, log)] = _tail(os.path.join(log_path, log), int(number))
|
result[os.path.join(log_path, log)] = _tail(os.path.join(log_path, log), int(number))
|
||||||
else:
|
else:
|
||||||
result[log_path] = _tail(log_path, int(number))
|
result[log_path] = _tail(log_path, int(number))
|
||||||
|
@ -314,13 +314,14 @@ def service_regen_conf(names=[], with_diff=False, force=False, dry_run=False,
|
||||||
common_args = [1 if force else 0, 1 if dry_run else 0]
|
common_args = [1 if force else 0, 1 if dry_run else 0]
|
||||||
|
|
||||||
# Execute hooks for pre-regen
|
# Execute hooks for pre-regen
|
||||||
pre_args = ['pre',] + common_args
|
pre_args = ['pre', ] + common_args
|
||||||
|
|
||||||
def _pre_call(name, priority, path, args):
|
def _pre_call(name, priority, path, args):
|
||||||
# create the pending conf directory for the service
|
# create the pending conf directory for the service
|
||||||
service_pending_path = os.path.join(pending_conf_dir, name)
|
service_pending_path = os.path.join(pending_conf_dir, name)
|
||||||
filesystem.mkdir(service_pending_path, 0755, True, uid='admin')
|
filesystem.mkdir(service_pending_path, 0755, True, uid='admin')
|
||||||
# return the arguments to pass to the script
|
# return the arguments to pass to the script
|
||||||
return pre_args + [service_pending_path,]
|
return pre_args + [service_pending_path, ]
|
||||||
pre_result = hook_callback('conf_regen', names, pre_callback=_pre_call)
|
pre_result = hook_callback('conf_regen', names, pre_callback=_pre_call)
|
||||||
|
|
||||||
# Update the services name
|
# Update the services name
|
||||||
|
@ -336,8 +337,8 @@ def service_regen_conf(names=[], with_diff=False, force=False, dry_run=False,
|
||||||
# Iterate over services and process pending conf
|
# Iterate over services and process pending conf
|
||||||
for service, conf_files in _get_pending_conf(names).items():
|
for service, conf_files in _get_pending_conf(names).items():
|
||||||
logger.info(m18n.n(
|
logger.info(m18n.n(
|
||||||
'service_regenconf_pending_applying' if not dry_run else \
|
'service_regenconf_pending_applying' if not dry_run else
|
||||||
'service_regenconf_dry_pending_applying',
|
'service_regenconf_dry_pending_applying',
|
||||||
service=service))
|
service=service))
|
||||||
|
|
||||||
conf_hashes = _get_conf_hashes(service)
|
conf_hashes = _get_conf_hashes(service)
|
||||||
|
@ -444,8 +445,8 @@ def service_regen_conf(names=[], with_diff=False, force=False, dry_run=False,
|
||||||
continue
|
continue
|
||||||
elif not failed_regen:
|
elif not failed_regen:
|
||||||
logger.success(m18n.n(
|
logger.success(m18n.n(
|
||||||
'service_conf_updated' if not dry_run else \
|
'service_conf_updated' if not dry_run else
|
||||||
'service_conf_would_be_updated',
|
'service_conf_would_be_updated',
|
||||||
service=service))
|
service=service))
|
||||||
if succeed_regen and not dry_run:
|
if succeed_regen and not dry_run:
|
||||||
_update_conf_hashes(service, conf_hashes)
|
_update_conf_hashes(service, conf_hashes)
|
||||||
|
@ -461,14 +462,15 @@ def service_regen_conf(names=[], with_diff=False, force=False, dry_run=False,
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# Execute hooks for post-regen
|
# Execute hooks for post-regen
|
||||||
post_args = ['post',] + common_args
|
post_args = ['post', ] + common_args
|
||||||
|
|
||||||
def _pre_call(name, priority, path, args):
|
def _pre_call(name, priority, path, args):
|
||||||
# append coma-separated applied changes for the service
|
# append coma-separated applied changes for the service
|
||||||
if name in result and result[name]['applied']:
|
if name in result and result[name]['applied']:
|
||||||
regen_conf_files = ','.join(result[name]['applied'].keys())
|
regen_conf_files = ','.join(result[name]['applied'].keys())
|
||||||
else:
|
else:
|
||||||
regen_conf_files = ''
|
regen_conf_files = ''
|
||||||
return post_args + [regen_conf_files,]
|
return post_args + [regen_conf_files, ]
|
||||||
hook_callback('conf_regen', names, pre_callback=_pre_call)
|
hook_callback('conf_regen', names, pre_callback=_pre_call)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
@ -556,7 +558,8 @@ def _tail(file, n, offset=None):
|
||||||
return lines[-to_read:offset and -offset or None]
|
return lines[-to_read:offset and -offset or None]
|
||||||
avg_line_length *= 1.3
|
avg_line_length *= 1.3
|
||||||
|
|
||||||
except IOError: return []
|
except IOError:
|
||||||
|
return []
|
||||||
|
|
||||||
|
|
||||||
def _get_files_diff(orig_file, new_file, as_string=False, skip_header=True):
|
def _get_files_diff(orig_file, new_file, as_string=False, skip_header=True):
|
||||||
|
|
|
@ -50,12 +50,12 @@ def user_list(auth, fields=None, filter=None, limit=None, offset=None):
|
||||||
fields -- fields to fetch
|
fields -- fields to fetch
|
||||||
|
|
||||||
"""
|
"""
|
||||||
user_attrs = { 'uid': 'username',
|
user_attrs = {'uid': 'username',
|
||||||
'cn': 'fullname',
|
'cn': 'fullname',
|
||||||
'mail': 'mail',
|
'mail': 'mail',
|
||||||
'maildrop': 'mail-forward',
|
'maildrop': 'mail-forward',
|
||||||
'mailuserquota': 'mailbox-quota' }
|
'mailuserquota': 'mailbox-quota'}
|
||||||
attrs = [ 'uid' ]
|
attrs = ['uid']
|
||||||
users = {}
|
users = {}
|
||||||
|
|
||||||
# Set default arguments values
|
# Set default arguments values
|
||||||
|
@ -74,12 +74,12 @@ def user_list(auth, fields=None, filter=None, limit=None, offset=None):
|
||||||
raise MoulinetteError(errno.EINVAL,
|
raise MoulinetteError(errno.EINVAL,
|
||||||
m18n.n('field_invalid', attr))
|
m18n.n('field_invalid', attr))
|
||||||
else:
|
else:
|
||||||
attrs = [ 'uid', 'cn', 'mail', 'mailuserquota' ]
|
attrs = ['uid', 'cn', 'mail', 'mailuserquota']
|
||||||
|
|
||||||
result = auth.search('ou=users,dc=yunohost,dc=org', filter, attrs)
|
result = auth.search('ou=users,dc=yunohost,dc=org', filter, attrs)
|
||||||
|
|
||||||
if len(result) > offset and limit > 0:
|
if len(result) > offset and limit > 0:
|
||||||
for user in result[offset:offset+limit]:
|
for user in result[offset:offset + limit]:
|
||||||
entry = {}
|
entry = {}
|
||||||
for attr, values in user.items():
|
for attr, values in user.items():
|
||||||
try:
|
try:
|
||||||
|
@ -88,7 +88,7 @@ def user_list(auth, fields=None, filter=None, limit=None, offset=None):
|
||||||
pass
|
pass
|
||||||
uid = entry[user_attrs['uid']]
|
uid = entry[user_attrs['uid']]
|
||||||
users[uid] = entry
|
users[uid] = entry
|
||||||
return { 'users' : users }
|
return {'users': users}
|
||||||
|
|
||||||
|
|
||||||
def user_create(auth, username, firstname, lastname, mail, password,
|
def user_create(auth, username, firstname, lastname, mail, password,
|
||||||
|
@ -112,8 +112,8 @@ def user_create(auth, username, firstname, lastname, mail, password,
|
||||||
|
|
||||||
# Validate uniqueness of username and mail in LDAP
|
# Validate uniqueness of username and mail in LDAP
|
||||||
auth.validate_uniqueness({
|
auth.validate_uniqueness({
|
||||||
'uid' : username,
|
'uid': username,
|
||||||
'mail' : mail
|
'mail': mail
|
||||||
})
|
})
|
||||||
|
|
||||||
# Validate uniqueness of username in system users
|
# Validate uniqueness of username in system users
|
||||||
|
@ -125,10 +125,10 @@ def user_create(auth, username, firstname, lastname, mail, password,
|
||||||
raise MoulinetteError(errno.EEXIST, m18n.n('system_username_exists'))
|
raise MoulinetteError(errno.EEXIST, m18n.n('system_username_exists'))
|
||||||
|
|
||||||
# Check that the mail domain exists
|
# Check that the mail domain exists
|
||||||
if mail[mail.find('@')+1:] not in domain_list(auth)['domains']:
|
if mail[mail.find('@') + 1:] not in domain_list(auth)['domains']:
|
||||||
raise MoulinetteError(errno.EINVAL,
|
raise MoulinetteError(errno.EINVAL,
|
||||||
m18n.n('mail_domain_unknown',
|
m18n.n('mail_domain_unknown',
|
||||||
domain=mail[mail.find('@')+1:]))
|
domain=mail[mail.find('@') + 1:]))
|
||||||
|
|
||||||
# Get random UID/GID
|
# Get random UID/GID
|
||||||
uid_check = gid_check = 0
|
uid_check = gid_check = 0
|
||||||
|
@ -141,24 +141,24 @@ def user_create(auth, username, firstname, lastname, mail, password,
|
||||||
fullname = '%s %s' % (firstname, lastname)
|
fullname = '%s %s' % (firstname, lastname)
|
||||||
rdn = 'uid=%s,ou=users' % username
|
rdn = 'uid=%s,ou=users' % username
|
||||||
char_set = string.ascii_uppercase + string.digits
|
char_set = string.ascii_uppercase + string.digits
|
||||||
salt = ''.join(random.sample(char_set,8))
|
salt = ''.join(random.sample(char_set, 8))
|
||||||
salt = '$1$' + salt + '$'
|
salt = '$1$' + salt + '$'
|
||||||
user_pwd = '{CRYPT}' + crypt.crypt(str(password), salt)
|
user_pwd = '{CRYPT}' + crypt.crypt(str(password), salt)
|
||||||
attr_dict = {
|
attr_dict = {
|
||||||
'objectClass' : ['mailAccount', 'inetOrgPerson', 'posixAccount'],
|
'objectClass': ['mailAccount', 'inetOrgPerson', 'posixAccount'],
|
||||||
'givenName' : firstname,
|
'givenName': firstname,
|
||||||
'sn' : lastname,
|
'sn': lastname,
|
||||||
'displayName' : fullname,
|
'displayName': fullname,
|
||||||
'cn' : fullname,
|
'cn': fullname,
|
||||||
'uid' : username,
|
'uid': username,
|
||||||
'mail' : mail,
|
'mail': mail,
|
||||||
'maildrop' : username,
|
'maildrop': username,
|
||||||
'mailuserquota' : mailbox_quota,
|
'mailuserquota': mailbox_quota,
|
||||||
'userPassword' : user_pwd,
|
'userPassword': user_pwd,
|
||||||
'gidNumber' : uid,
|
'gidNumber': uid,
|
||||||
'uidNumber' : uid,
|
'uidNumber': uid,
|
||||||
'homeDirectory' : '/home/' + username,
|
'homeDirectory': '/home/' + username,
|
||||||
'loginShell' : '/bin/false'
|
'loginShell': '/bin/false'
|
||||||
}
|
}
|
||||||
|
|
||||||
# If it is the first user, add some aliases
|
# If it is the first user, add some aliases
|
||||||
|
@ -166,12 +166,12 @@ def user_create(auth, username, firstname, lastname, mail, password,
|
||||||
with open('/etc/yunohost/current_host') as f:
|
with open('/etc/yunohost/current_host') as f:
|
||||||
main_domain = f.readline().rstrip()
|
main_domain = f.readline().rstrip()
|
||||||
aliases = [
|
aliases = [
|
||||||
'root@'+ main_domain,
|
'root@' + main_domain,
|
||||||
'admin@'+ main_domain,
|
'admin@' + main_domain,
|
||||||
'webmaster@'+ main_domain,
|
'webmaster@' + main_domain,
|
||||||
'postmaster@'+ main_domain,
|
'postmaster@' + main_domain,
|
||||||
]
|
]
|
||||||
attr_dict['mail'] = [ attr_dict['mail'] ] + aliases
|
attr_dict['mail'] = [attr_dict['mail']] + aliases
|
||||||
|
|
||||||
# If exists, remove the redirection from the SSO
|
# If exists, remove the redirection from the SSO
|
||||||
try:
|
try:
|
||||||
|
@ -192,7 +192,6 @@ def user_create(auth, username, firstname, lastname, mail, password,
|
||||||
raise MoulinetteError(errno.EPERM,
|
raise MoulinetteError(errno.EPERM,
|
||||||
m18n.n('ssowat_persistent_conf_write_error', error=e.strerror))
|
m18n.n('ssowat_persistent_conf_write_error', error=e.strerror))
|
||||||
|
|
||||||
|
|
||||||
if auth.add(rdn, attr_dict):
|
if auth.add(rdn, attr_dict):
|
||||||
# Invalidate passwd to take user creation into account
|
# Invalidate passwd to take user creation into account
|
||||||
subprocess.call(['nscd', '-i', 'passwd'])
|
subprocess.call(['nscd', '-i', 'passwd'])
|
||||||
|
@ -200,7 +199,7 @@ def user_create(auth, username, firstname, lastname, mail, password,
|
||||||
# Update SFTP user group
|
# Update SFTP user group
|
||||||
memberlist = auth.search(filter='cn=sftpusers', attrs=['memberUid'])[0]['memberUid']
|
memberlist = auth.search(filter='cn=sftpusers', attrs=['memberUid'])[0]['memberUid']
|
||||||
memberlist.append(username)
|
memberlist.append(username)
|
||||||
if auth.update('cn=sftpusers,ou=groups', { 'memberUid': memberlist }):
|
if auth.update('cn=sftpusers,ou=groups', {'memberUid': memberlist}):
|
||||||
try:
|
try:
|
||||||
# Attempt to create user home folder
|
# Attempt to create user home folder
|
||||||
subprocess.check_call(
|
subprocess.check_call(
|
||||||
|
@ -210,12 +209,12 @@ def user_create(auth, username, firstname, lastname, mail, password,
|
||||||
logger.warning(m18n.n('user_home_creation_failed'),
|
logger.warning(m18n.n('user_home_creation_failed'),
|
||||||
exc_info=1)
|
exc_info=1)
|
||||||
app_ssowatconf(auth)
|
app_ssowatconf(auth)
|
||||||
#TODO: Send a welcome mail to user
|
# TODO: Send a welcome mail to user
|
||||||
logger.success(m18n.n('user_created'))
|
logger.success(m18n.n('user_created'))
|
||||||
hook_callback('post_user_create',
|
hook_callback('post_user_create',
|
||||||
args=[username, mail, password, firstname, lastname])
|
args=[username, mail, password, firstname, lastname])
|
||||||
|
|
||||||
return { 'fullname' : fullname, 'username' : username, 'mail' : mail }
|
return {'fullname': fullname, 'username': username, 'mail': mail}
|
||||||
|
|
||||||
raise MoulinetteError(169, m18n.n('user_creation_failed'))
|
raise MoulinetteError(169, m18n.n('user_creation_failed'))
|
||||||
|
|
||||||
|
@ -238,9 +237,11 @@ def user_delete(auth, username, purge=False):
|
||||||
|
|
||||||
# Update SFTP user group
|
# Update SFTP user group
|
||||||
memberlist = auth.search(filter='cn=sftpusers', attrs=['memberUid'])[0]['memberUid']
|
memberlist = auth.search(filter='cn=sftpusers', attrs=['memberUid'])[0]['memberUid']
|
||||||
try: memberlist.remove(username)
|
try:
|
||||||
except: pass
|
memberlist.remove(username)
|
||||||
if auth.update('cn=sftpusers,ou=groups', { 'memberUid': memberlist }):
|
except:
|
||||||
|
pass
|
||||||
|
if auth.update('cn=sftpusers,ou=groups', {'memberUid': memberlist}):
|
||||||
if purge:
|
if purge:
|
||||||
subprocess.call(['rm', '-rf', '/home/{0}'.format(username)])
|
subprocess.call(['rm', '-rf', '/home/{0}'.format(username)])
|
||||||
else:
|
else:
|
||||||
|
@ -286,11 +287,11 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None,
|
||||||
|
|
||||||
# Get modifications from arguments
|
# Get modifications from arguments
|
||||||
if firstname:
|
if firstname:
|
||||||
new_attr_dict['givenName'] = firstname # TODO: Validate
|
new_attr_dict['givenName'] = firstname # TODO: Validate
|
||||||
new_attr_dict['cn'] = new_attr_dict['displayName'] = firstname + ' ' + user['sn'][0]
|
new_attr_dict['cn'] = new_attr_dict['displayName'] = firstname + ' ' + user['sn'][0]
|
||||||
|
|
||||||
if lastname:
|
if lastname:
|
||||||
new_attr_dict['sn'] = lastname # TODO: Validate
|
new_attr_dict['sn'] = lastname # TODO: Validate
|
||||||
new_attr_dict['cn'] = new_attr_dict['displayName'] = user['givenName'][0] + ' ' + lastname
|
new_attr_dict['cn'] = new_attr_dict['displayName'] = user['givenName'][0] + ' ' + lastname
|
||||||
|
|
||||||
if lastname and firstname:
|
if lastname and firstname:
|
||||||
|
@ -298,34 +299,34 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None,
|
||||||
|
|
||||||
if change_password:
|
if change_password:
|
||||||
char_set = string.ascii_uppercase + string.digits
|
char_set = string.ascii_uppercase + string.digits
|
||||||
salt = ''.join(random.sample(char_set,8))
|
salt = ''.join(random.sample(char_set, 8))
|
||||||
salt = '$1$' + salt + '$'
|
salt = '$1$' + salt + '$'
|
||||||
new_attr_dict['userPassword'] = '{CRYPT}' + crypt.crypt(str(change_password), salt)
|
new_attr_dict['userPassword'] = '{CRYPT}' + crypt.crypt(str(change_password), salt)
|
||||||
|
|
||||||
if mail:
|
if mail:
|
||||||
auth.validate_uniqueness({ 'mail': mail })
|
auth.validate_uniqueness({'mail': mail})
|
||||||
if mail[mail.find('@')+1:] not in domains:
|
if mail[mail.find('@') + 1:] not in domains:
|
||||||
raise MoulinetteError(errno.EINVAL,
|
raise MoulinetteError(errno.EINVAL,
|
||||||
m18n.n('mail_domain_unknown',
|
m18n.n('mail_domain_unknown',
|
||||||
domain=mail[mail.find('@')+1:]))
|
domain=mail[mail.find('@') + 1:]))
|
||||||
del user['mail'][0]
|
del user['mail'][0]
|
||||||
new_attr_dict['mail'] = [mail] + user['mail']
|
new_attr_dict['mail'] = [mail] + user['mail']
|
||||||
|
|
||||||
if add_mailalias:
|
if add_mailalias:
|
||||||
if not isinstance(add_mailalias, list):
|
if not isinstance(add_mailalias, list):
|
||||||
add_mailalias = [ add_mailalias ]
|
add_mailalias = [add_mailalias]
|
||||||
for mail in add_mailalias:
|
for mail in add_mailalias:
|
||||||
auth.validate_uniqueness({ 'mail': mail })
|
auth.validate_uniqueness({'mail': mail})
|
||||||
if mail[mail.find('@')+1:] not in domains:
|
if mail[mail.find('@') + 1:] not in domains:
|
||||||
raise MoulinetteError(errno.EINVAL,
|
raise MoulinetteError(errno.EINVAL,
|
||||||
m18n.n('mail_domain_unknown',
|
m18n.n('mail_domain_unknown',
|
||||||
domain=mail[mail.find('@')+1:]))
|
domain=mail[mail.find('@') + 1:]))
|
||||||
user['mail'].append(mail)
|
user['mail'].append(mail)
|
||||||
new_attr_dict['mail'] = user['mail']
|
new_attr_dict['mail'] = user['mail']
|
||||||
|
|
||||||
if remove_mailalias:
|
if remove_mailalias:
|
||||||
if not isinstance(remove_mailalias, list):
|
if not isinstance(remove_mailalias, list):
|
||||||
remove_mailalias = [ remove_mailalias ]
|
remove_mailalias = [remove_mailalias]
|
||||||
for mail in remove_mailalias:
|
for mail in remove_mailalias:
|
||||||
if len(user['mail']) > 1 and mail in user['mail'][1:]:
|
if len(user['mail']) > 1 and mail in user['mail'][1:]:
|
||||||
user['mail'].remove(mail)
|
user['mail'].remove(mail)
|
||||||
|
@ -336,7 +337,7 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None,
|
||||||
|
|
||||||
if add_mailforward:
|
if add_mailforward:
|
||||||
if not isinstance(add_mailforward, list):
|
if not isinstance(add_mailforward, list):
|
||||||
add_mailforward = [ add_mailforward ]
|
add_mailforward = [add_mailforward]
|
||||||
for mail in add_mailforward:
|
for mail in add_mailforward:
|
||||||
if mail in user['maildrop'][1:]:
|
if mail in user['maildrop'][1:]:
|
||||||
continue
|
continue
|
||||||
|
@ -345,7 +346,7 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None,
|
||||||
|
|
||||||
if remove_mailforward:
|
if remove_mailforward:
|
||||||
if not isinstance(remove_mailforward, list):
|
if not isinstance(remove_mailforward, list):
|
||||||
remove_mailforward = [ remove_mailforward ]
|
remove_mailforward = [remove_mailforward]
|
||||||
for mail in remove_mailforward:
|
for mail in remove_mailforward:
|
||||||
if len(user['maildrop']) > 1 and mail in user['maildrop'][1:]:
|
if len(user['maildrop']) > 1 and mail in user['maildrop'][1:]:
|
||||||
user['maildrop'].remove(mail)
|
user['maildrop'].remove(mail)
|
||||||
|
@ -358,11 +359,11 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None,
|
||||||
new_attr_dict['mailuserquota'] = mailbox_quota
|
new_attr_dict['mailuserquota'] = mailbox_quota
|
||||||
|
|
||||||
if auth.update('uid=%s,ou=users' % username, new_attr_dict):
|
if auth.update('uid=%s,ou=users' % username, new_attr_dict):
|
||||||
logger.success(m18n.n('user_updated'))
|
logger.success(m18n.n('user_updated'))
|
||||||
app_ssowatconf(auth)
|
app_ssowatconf(auth)
|
||||||
return user_info(auth, username)
|
return user_info(auth, username)
|
||||||
else:
|
else:
|
||||||
raise MoulinetteError(169, m18n.n('user_update_failed'))
|
raise MoulinetteError(169, m18n.n('user_update_failed'))
|
||||||
|
|
||||||
|
|
||||||
def user_info(auth, username):
|
def user_info(auth, username):
|
||||||
|
@ -378,9 +379,9 @@ def user_info(auth, username):
|
||||||
]
|
]
|
||||||
|
|
||||||
if len(username.split('@')) is 2:
|
if len(username.split('@')) is 2:
|
||||||
filter = 'mail='+ username
|
filter = 'mail=' + username
|
||||||
else:
|
else:
|
||||||
filter = 'uid='+ username
|
filter = 'uid=' + username
|
||||||
|
|
||||||
result = auth.search('ou=users,dc=yunohost,dc=org', filter, user_attrs)
|
result = auth.search('ou=users,dc=yunohost,dc=org', filter, user_attrs)
|
||||||
|
|
||||||
|
@ -436,8 +437,8 @@ def user_info(auth, username):
|
||||||
storage_use += ' (%s%%)' % percentage
|
storage_use += ' (%s%%)' % percentage
|
||||||
|
|
||||||
result_dict['mailbox-quota'] = {
|
result_dict['mailbox-quota'] = {
|
||||||
'limit' : userquota if is_limited else m18n.n('unlimit'),
|
'limit': userquota if is_limited else m18n.n('unlimit'),
|
||||||
'use' : storage_use
|
'use': storage_use
|
||||||
}
|
}
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
|
@ -445,8 +446,9 @@ def user_info(auth, username):
|
||||||
else:
|
else:
|
||||||
raise MoulinetteError(167, m18n.n('user_info_failed'))
|
raise MoulinetteError(167, m18n.n('user_info_failed'))
|
||||||
|
|
||||||
|
|
||||||
def _convertSize(num, suffix=''):
|
def _convertSize(num, suffix=''):
|
||||||
for unit in ['K','M','G','T','P','E','Z']:
|
for unit in ['K', 'M', 'G', 'T', 'P', 'E', 'Z']:
|
||||||
if abs(num) < 1024.0:
|
if abs(num) < 1024.0:
|
||||||
return "%3.1f%s%s" % (num, unit, suffix)
|
return "%3.1f%s%s" % (num, unit, suffix)
|
||||||
num /= 1024.0
|
num /= 1024.0
|
||||||
|
|
|
@ -424,6 +424,7 @@ def get_installed_version(*pkgnames, **kwargs):
|
||||||
return versions[pkgnames[0]]
|
return versions[pkgnames[0]]
|
||||||
return versions
|
return versions
|
||||||
|
|
||||||
|
|
||||||
def meets_version_specifier(pkgname, specifier):
|
def meets_version_specifier(pkgname, specifier):
|
||||||
"""Check if a package installed version meets specifier"""
|
"""Check if a package installed version meets specifier"""
|
||||||
spec = SpecifierSet(specifier)
|
spec = SpecifierSet(specifier)
|
||||||
|
|
26
src/yunohost/vendor/acme_tiny/acme_tiny.py
vendored
26
src/yunohost/vendor/acme_tiny/acme_tiny.py
vendored
|
@ -1,9 +1,21 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import argparse, subprocess, json, os, sys, base64, binascii, time, hashlib, re, copy, textwrap, logging
|
import argparse
|
||||||
|
import subprocess
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import base64
|
||||||
|
import binascii
|
||||||
|
import time
|
||||||
|
import hashlib
|
||||||
|
import re
|
||||||
|
import copy
|
||||||
|
import textwrap
|
||||||
|
import logging
|
||||||
try:
|
try:
|
||||||
from urllib.request import urlopen # Python 3
|
from urllib.request import urlopen # Python 3
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from urllib2 import urlopen # Python 2
|
from urllib2 import urlopen # Python 2
|
||||||
|
|
||||||
#DEFAULT_CA = "https://acme-staging.api.letsencrypt.org"
|
#DEFAULT_CA = "https://acme-staging.api.letsencrypt.org"
|
||||||
DEFAULT_CA = "https://acme-v01.api.letsencrypt.org"
|
DEFAULT_CA = "https://acme-v01.api.letsencrypt.org"
|
||||||
|
@ -12,6 +24,7 @@ LOGGER = logging.getLogger(__name__)
|
||||||
LOGGER.addHandler(logging.StreamHandler())
|
LOGGER.addHandler(logging.StreamHandler())
|
||||||
LOGGER.setLevel(logging.INFO)
|
LOGGER.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
def get_crt(account_key, csr, acme_dir, log=LOGGER, CA=DEFAULT_CA):
|
def get_crt(account_key, csr, acme_dir, log=LOGGER, CA=DEFAULT_CA):
|
||||||
# helper function base64 encode for jose spec
|
# helper function base64 encode for jose spec
|
||||||
def _b64(b):
|
def _b64(b):
|
||||||
|
@ -26,7 +39,7 @@ def get_crt(account_key, csr, acme_dir, log=LOGGER, CA=DEFAULT_CA):
|
||||||
raise IOError("OpenSSL Error: {0}".format(err))
|
raise IOError("OpenSSL Error: {0}".format(err))
|
||||||
pub_hex, pub_exp = re.search(
|
pub_hex, pub_exp = re.search(
|
||||||
r"modulus:\n\s+00:([a-f0-9\:\s]+?)\npublicExponent: ([0-9]+)",
|
r"modulus:\n\s+00:([a-f0-9\:\s]+?)\npublicExponent: ([0-9]+)",
|
||||||
out.decode('utf8'), re.MULTILINE|re.DOTALL).groups()
|
out.decode('utf8'), re.MULTILINE | re.DOTALL).groups()
|
||||||
pub_exp = "{0:x}".format(int(pub_exp))
|
pub_exp = "{0:x}".format(int(pub_exp))
|
||||||
pub_exp = "0{0}".format(pub_exp) if len(pub_exp) % 2 else pub_exp
|
pub_exp = "0{0}".format(pub_exp) if len(pub_exp) % 2 else pub_exp
|
||||||
header = {
|
header = {
|
||||||
|
@ -72,7 +85,7 @@ def get_crt(account_key, csr, acme_dir, log=LOGGER, CA=DEFAULT_CA):
|
||||||
common_name = re.search(r"Subject:.*? CN=([^\s,;/]+)", out.decode('utf8'))
|
common_name = re.search(r"Subject:.*? CN=([^\s,;/]+)", out.decode('utf8'))
|
||||||
if common_name is not None:
|
if common_name is not None:
|
||||||
domains.add(common_name.group(1))
|
domains.add(common_name.group(1))
|
||||||
subject_alt_names = re.search(r"X509v3 Subject Alternative Name: \n +([^\n]+)\n", out.decode('utf8'), re.MULTILINE|re.DOTALL)
|
subject_alt_names = re.search(r"X509v3 Subject Alternative Name: \n +([^\n]+)\n", out.decode('utf8'), re.MULTILINE | re.DOTALL)
|
||||||
if subject_alt_names is not None:
|
if subject_alt_names is not None:
|
||||||
for san in subject_alt_names.group(1).split(", "):
|
for san in subject_alt_names.group(1).split(", "):
|
||||||
if san.startswith("DNS:"):
|
if san.startswith("DNS:"):
|
||||||
|
@ -165,6 +178,7 @@ def get_crt(account_key, csr, acme_dir, log=LOGGER, CA=DEFAULT_CA):
|
||||||
return """-----BEGIN CERTIFICATE-----\n{0}\n-----END CERTIFICATE-----\n""".format(
|
return """-----BEGIN CERTIFICATE-----\n{0}\n-----END CERTIFICATE-----\n""".format(
|
||||||
"\n".join(textwrap.wrap(base64.b64encode(result).decode('utf8'), 64)))
|
"\n".join(textwrap.wrap(base64.b64encode(result).decode('utf8'), 64)))
|
||||||
|
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||||
|
@ -194,5 +208,5 @@ def main(argv):
|
||||||
signed_crt = get_crt(args.account_key, args.csr, args.acme_dir, log=LOGGER, CA=args.ca)
|
signed_crt = get_crt(args.account_key, args.csr, args.acme_dir, log=LOGGER, CA=args.ca)
|
||||||
sys.stdout.write(signed_crt)
|
sys.stdout.write(signed_crt)
|
||||||
|
|
||||||
if __name__ == "__main__": # pragma: no cover
|
if __name__ == "__main__": # pragma: no cover
|
||||||
main(sys.argv[1:])
|
main(sys.argv[1:])
|
||||||
|
|
Loading…
Add table
Reference in a new issue