moulinette/yunohost_tools.py

184 lines
5.8 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-26 15:26:50 +02:00
from yunohost import 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
"""
if not args['old']:
args['old'] = getpass.getpass(colorize('Actual admin password: ', 'cyan'))
if not args['new']:
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-27 17:06:43 +02:00
os.system('ldappasswd -h localhost -D cn=admin,dc=yunohost,dc=org -w "'+ args['old'] +'" -a "'+ args['old'] +'" -s "' + args['new'] + '"')
return { 'Success' : _("Admin password has been changed") }
def tools_maindomain(args):
"""
Change admin password
Keyword arguments:
args
Returns:
dict
"""
2012-10-25 19:52:26 +02:00
args = get_required_args(args, {'new' : _('New main domain name')})
2012-10-27 17:06:43 +02:00
if not args['old']:
with open('/usr/share/yunohost/yunohost-config/others/current_host', 'r') as f:
args['old'] = f.readline()
validate({
args['new'] : 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'] : 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-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-27 17:06:43 +02:00
sources.write(re.sub(r''+ args['old'] +'', args['new'], 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'
os.system('echo "01" > '+ tmp +'/ssl/yunoCA/serial')
os.system('rm '+ tmp +'/ssl/yunoCA/index.txt')
os.system('touch '+ tmp +'/ssl/yunoCA/index.txt')
os.system('sed -i "s/' + args['old'] + '/' + args['new'] + '/g" '+ tmp +'/ssl/yunoCA/openssl.cnf')
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')
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')
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')
os.system('cp '+ tmp +'/ssl/yunoCA/ca/cacert.pem /etc/ssl/certs/ca-yunohost_crt.pem')
os.system('cp '+ tmp +'/ssl/yunoCA/certs/yunohost_key.pem /etc/ssl/private/')
os.system('cp '+ tmp +'/ssl/yunoCA/newcerts/01.pem /etc/ssl/certs/yunohost_crt.pem')
os.system('cp '+ tmp +'/ssl/yunoCA/newcerts/01.pem /etc/ejabberd/ejabberd.pem')
os.system('echo '+ args['new'] +' > /usr/share/yunohost/yunohost-config/others/current_host')
# Restart services
os.system('/etc/init.d/apache2 restart')
os.system('/etc/init.d/postfix restart')
os.system('/etc/init.d/ejabberd restart')
return { 'Success' : _("YunoHost main domain has been successfully changed") }
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
"""
args = get_required_args(args, {'domain' : _('Main domain name'), 'password' : _('New admin password') }, True)
2012-10-26 15:26:50 +02:00
2012-10-27 17:06:43 +02:00
try:
with open('/usr/share/yunohost/yunohost-config/others/installed') as f: pass
except IOError:
2012-10-25 19:52:26 +02:00
print('Installing YunoHost')
2012-10-27 17:06:43 +02:00
else:
2012-10-25 19:52:26 +02:00
raise YunoHostError(17, _("YunoHost is already installed"))
2012-10-26 15:26:50 +02:00
# Initialize YunoHost LDAP base
2012-10-25 19:52:26 +02:00
tools_ldapinit(args, connections)
2012-10-26 15:26:50 +02:00
# Change LDAP admin password
2012-10-27 17:06:43 +02:00
tools_adminpw({ 'old' : 'yunohost', 'new' : args['password']})
2012-10-26 15:26:50 +02:00
# New domain config
2012-10-27 17:06:43 +02:00
tools_maindomain({ 'old' : 'yunohost.org', 'new' : args['domain']})
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") }