moulinette/yunohost_tools.py

190 lines
6.3 KiB
Python
Raw Normal View History

2012-10-23 17:28:35 +02:00
# -*- coding: utf-8 -*-
2012-10-26 15:26:50 +02:00
import os
import sys
2012-10-23 17:28:35 +02:00
import yaml
2012-10-26 15:26:50 +02:00
import re
2012-10-27 17:06:43 +02:00
import getpass
2012-10-25 21:17:26 +02:00
from yunohost import YunoHostError, validate, colorize, get_required_args
2012-10-23 17:28:35 +02:00
2012-10-27 17:06:43 +02:00
2012-10-26 15:26:50 +02:00
def tools_ldapinit(args, connections):
2012-10-27 17:06:43 +02:00
"""
Initialize YunoHost LDAP scheme
Keyword arguments:
args
connections
Returns:
dict
"""
2012-10-23 17:28:35 +02:00
yldap = connections['ldap']
2012-10-26 15:26:50 +02:00
2012-10-23 17:28:35 +02:00
with open('ldap_scheme.yml') as f:
ldap_map = yaml.load(f)
2012-10-23 18:10:39 +02:00
for rdn, attr_dict in ldap_map['parents'].items():
yldap.add(rdn, attr_dict)
for rdn, attr_dict in ldap_map['childs'].items():
2012-10-23 17:28:35 +02:00
yldap.add(rdn, attr_dict)
2012-10-23 19:55:40 +02:00
admin_dict = {
'cn': 'admin',
'uid': 'admin',
'description': 'LDAP Administrator',
'gidNumber': '1007',
'uidNumber': '1007',
'homeDirectory': '/home/admin',
'loginShell': '/bin/bash',
'objectClass': ['organizationalRole', 'posixAccount', 'simpleSecurityObject']
}
yldap.update('cn=admin', admin_dict)
2012-10-26 15:26:50 +02:00
return { 'Success' : _("LDAP has been successfully initialized") }
2012-10-27 17:06:43 +02:00
def tools_adminpw(args):
"""
Change admin password
Keyword arguments:
args
Returns:
dict
"""
2012-10-25 21:17:26 +02:00
if not 'old' in args:
2012-10-27 17:06:43 +02:00
args['old'] = getpass.getpass(colorize('Actual admin password: ', 'cyan'))
2012-10-25 21:17:26 +02:00
if not 'new' in args:
2012-10-27 17:06:43 +02:00
args['new'] = getpass.getpass(colorize('New admin password: ', 'cyan'))
pwd2 = getpass.getpass(colorize('Retype new password: ', 'cyan'))
if args['new'] != pwd2:
raise YunoHostError(22, _("Passwords doesn't match"))
2012-10-26 15:26:50 +02:00
# Validate password length
if len(args['new']) < 4:
raise YunoHostError(22, _("Password is too short"))
2012-10-25 21:17:26 +02:00
result = os.system('ldappasswd -h localhost -D cn=admin,dc=yunohost,dc=org -w "'+ args['old'] +'" -a "'+ args['old'] +'" -s "' + args['new'] + '"')
2012-10-27 17:06:43 +02:00
2012-10-25 21:17:26 +02:00
if result == 0:
return { 'Success' : _("Admin password has been changed") }
else:
raise YunoHostError(22, _("Invalid password"))
2012-10-27 17:06:43 +02:00
def tools_maindomain(args):
"""
Change admin password
Keyword arguments:
args
Returns:
dict
"""
2012-10-25 20:01:25 +02:00
args = get_required_args(args, {'new_domain' : _('New main domain name')})
2012-10-27 17:06:43 +02:00
2012-10-25 20:01:25 +02:00
if not args['old_domain']:
2012-10-27 17:06:43 +02:00
with open('/usr/share/yunohost/yunohost-config/others/current_host', 'r') as f:
2012-10-25 19:54:36 +02:00
args['old_domain'] = f.readline().rstrip()
2012-10-27 17:06:43 +02:00
validate({
2012-10-25 20:01:25 +02:00
args['new_domain'] : r'^([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])*)$',
args['old_domain'] : r'^([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])*)$'
2012-10-27 17:06:43 +02:00
})
2012-10-26 15:26:50 +02:00
config_files = [
'/etc/postfix/main.cf',
2012-10-27 17:06:43 +02:00
'/etc/dovecot/dovecot.conf',
2012-10-26 15:26:50 +02:00
'/etc/mailname',
'/etc/ejabberd/ejabberd.cfg',
'/etc/lemonldap-ng/lemonldap-ng.ini',
'/etc/hosts',
]
config_dir = [
'/var/lib/lemonldap-ng/conf',
'/etc/apache2/sites-available',
]
for dir in config_dir:
for file in os.listdir(dir):
config_files.append(dir + '/' + file)
for file in config_files:
with open(file, "r") as sources:
lines = sources.readlines()
with open(file, "w") as sources:
for line in lines:
2012-10-25 20:01:25 +02:00
sources.write(re.sub(r''+ args['old_domain'] +'', args['new_domain'], line))
2012-10-26 15:26:50 +02:00
os.system('/etc/init.d/hostname.sh')
2012-10-27 17:06:43 +02:00
# Regenerate certificate
tmp = '/usr/share/yunohost/yunohost-config'
2012-10-28 15:24:10 +01:00
a = os.system('echo "01" > '+ tmp +'/ssl/yunoCA/serial')
b = os.system('rm '+ tmp +'/ssl/yunoCA/index.txt')
c = os.system('touch '+ tmp +'/ssl/yunoCA/index.txt')
d = os.system('sed -i "s/' + args['old_domain'] + '/' + args['new_domain'] + '/g" '+ tmp +'/ssl/yunoCA/openssl.cnf')
e = os.system('openssl req -x509 -new -config '+ tmp +'/ssl/yunoCA/openssl.cnf -days 3650 -out '+ tmp +'/ssl/yunoCA/ca/cacert.pem -keyout '+ tmp +'/ssl/yunoCA/ca/cakey.pem -nodes -batch')
f = os.system('openssl req -new -config '+ tmp +'/ssl/yunoCA/openssl.cnf -days 730 -out '+ tmp +'/ssl/yunoCA/certs/yunohost_csr.pem -keyout '+ tmp +'/ssl/yunoCA/certs/yunohost_key.pem -nodes -batch')
g = os.system('openssl ca -config '+ tmp +'/ssl/yunoCA/openssl.cnf -days 730 -in '+ tmp +'/ssl/yunoCA/certs/yunohost_csr.pem -out '+ tmp +'/ssl/yunoCA/certs/yunohost_crt.pem -batch')
h = os.system('cp '+ tmp +'/ssl/yunoCA/ca/cacert.pem /etc/ssl/certs/ca-yunohost_crt.pem')
i = os.system('cp '+ tmp +'/ssl/yunoCA/certs/yunohost_key.pem /etc/ssl/private/')
j = os.system('cp '+ tmp +'/ssl/yunoCA/newcerts/01.pem /etc/ssl/certs/yunohost_crt.pem')
k = os.system('cp '+ tmp +'/ssl/yunoCA/newcerts/01.pem /etc/ejabberd/ejabberd.pem')
l = os.system('echo '+ args['new_domain'] +' > /usr/share/yunohost/yunohost-config/others/current_host')
2012-10-27 17:06:43 +02:00
# Restart services
2012-10-28 15:24:10 +01:00
m = os.system('/etc/init.d/apache2 restart')
n = os.system('/etc/init.d/postfix restart')
o = os.system('/etc/init.d/ejabberd restart')
2012-10-27 17:06:43 +02:00
2012-10-28 15:24:10 +01:00
if a == b == c == d == e == f == g == h == i == j == k == l == m == n == o:
return { 'Success' : _("YunoHost main domain has been successfully changed") }
else:
return { 'Error' : _("There were a problem during domain changing") }
2012-10-27 17:06:43 +02:00
2012-10-26 15:26:50 +02:00
def tools_postinstall(args, connections):
2012-10-27 17:06:43 +02:00
"""
Post-install configuration
Keyword arguments:
args
connection
Returns:
dict
"""
2012-10-25 19:52:59 +02:00
try:
with open('/usr/share/yunohost/yunohost-config/others/installed') as f: pass
except IOError:
print('Installing YunoHost')
else:
raise YunoHostError(17, _("YunoHost is already installed"))
2012-10-26 15:26:50 +02:00
2012-10-25 19:50:14 +02:00
args = get_required_args(args, {'domain' : _('Main domain name'), 'password' : _('New admin password') }, True)
2012-10-28 15:24:10 +01:00
# New domain config
tools_maindomain({ 'old_domain' : 'yunohost.org', 'new_domain' : args['domain']})
2012-10-25 19:52:59 +02:00
# Initialize YunoHost LDAP base
2012-10-25 19:50:14 +02:00
tools_ldapinit(args, connections)
2012-10-26 15:26:50 +02:00
2012-10-25 19:52:59 +02:00
# Change LDAP admin password
tools_adminpw({ 'old' : 'yunohost', 'new' : args['password']})
2012-10-26 15:26:50 +02:00
2012-10-25 19:52:59 +02:00
os.system('touch /usr/share/yunohost/yunohost-config/others/installed')
2012-10-26 15:26:50 +02:00
return { 'Success' : _("YunoHost has been successfully configured") }