mirror of
https://github.com/YunoHost/moulinette.git
synced 2024-09-03 20:06:31 +02:00
Update and review yunohost.domain
This commit is contained in:
parent
f567d1d03e
commit
1c7f54fb5c
4 changed files with 235 additions and 231 deletions
|
@ -199,6 +199,8 @@ domain:
|
||||||
list:
|
list:
|
||||||
action_help: List domains
|
action_help: List domains
|
||||||
api: GET /domains
|
api: GET /domains
|
||||||
|
configuration:
|
||||||
|
authenticate: all
|
||||||
arguments:
|
arguments:
|
||||||
-f:
|
-f:
|
||||||
full: --filter
|
full: --filter
|
||||||
|
@ -206,14 +208,18 @@ domain:
|
||||||
-l:
|
-l:
|
||||||
full: --limit
|
full: --limit
|
||||||
help: Maximum number of domain fetched
|
help: Maximum number of domain fetched
|
||||||
|
type: int
|
||||||
-o:
|
-o:
|
||||||
full: --offset
|
full: --offset
|
||||||
help: Starting number for domain fetching
|
help: Starting number for domain fetching
|
||||||
|
type: int
|
||||||
|
|
||||||
### domain_add()
|
### domain_add()
|
||||||
add:
|
add:
|
||||||
action_help: Create a custom domain
|
action_help: Create a custom domain
|
||||||
api: POST /domains
|
api: POST /domains
|
||||||
|
configuration:
|
||||||
|
authenticate: all
|
||||||
arguments:
|
arguments:
|
||||||
domains:
|
domains:
|
||||||
help: Domain name to add
|
help: Domain name to add
|
||||||
|
@ -235,6 +241,8 @@ domain:
|
||||||
remove:
|
remove:
|
||||||
action_help: Delete domains
|
action_help: Delete domains
|
||||||
api: 'DELETE /domains/{domains}'
|
api: 'DELETE /domains/{domains}'
|
||||||
|
configuration:
|
||||||
|
authenticate: all
|
||||||
arguments:
|
arguments:
|
||||||
domains:
|
domains:
|
||||||
help: Domain(s) to delete
|
help: Domain(s) to delete
|
||||||
|
@ -245,16 +253,16 @@ domain:
|
||||||
- "Must be a valid domain name (e.g. my-domain.org)"
|
- "Must be a valid domain name (e.g. my-domain.org)"
|
||||||
|
|
||||||
### domain_info()
|
### domain_info()
|
||||||
info:
|
# info:
|
||||||
action_help: Get domain informations
|
# action_help: Get domain informations
|
||||||
api: 'GET /domains/<domain>'
|
# api: 'GET /domains/<domain>'
|
||||||
arguments:
|
# arguments:
|
||||||
domain:
|
# domain:
|
||||||
help: ""
|
# help: ""
|
||||||
extra:
|
# extra:
|
||||||
pattern:
|
# 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])*)$'
|
# - '^([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])*)$'
|
||||||
- "Must be a valid domain name (e.g. my-domain.org)"
|
# - "Must be a valid domain name (e.g. my-domain.org)"
|
||||||
|
|
||||||
|
|
||||||
#############################
|
#############################
|
||||||
|
|
|
@ -534,8 +534,8 @@ def app_addaccess(apps, users):
|
||||||
"""
|
"""
|
||||||
if not users:
|
if not users:
|
||||||
users = []
|
users = []
|
||||||
for user in user_list()['Users']:
|
for user in user_list()['users']:
|
||||||
users.append(user['Username'])
|
users.append(user['username'])
|
||||||
|
|
||||||
if not isinstance(users, list): users = [users]
|
if not isinstance(users, list): users = [users]
|
||||||
if not isinstance(apps, list): apps = [apps]
|
if not isinstance(apps, list): apps = [apps]
|
||||||
|
@ -610,11 +610,11 @@ def app_removeaccess(apps, users):
|
||||||
new_users = new_users +','+ allowed_user
|
new_users = new_users +','+ allowed_user
|
||||||
else:
|
else:
|
||||||
new_users=''
|
new_users=''
|
||||||
for user in user_list()['Users']:
|
for user in user_list()['users']:
|
||||||
if user['Username'] not in users:
|
if user['username'] not in users:
|
||||||
if new_users == '':
|
if new_users == '':
|
||||||
new_users = user['Username']
|
new_users = user['username']
|
||||||
new_users=new_users+','+user['Username']
|
new_users=new_users+','+user['username']
|
||||||
|
|
||||||
app_setting(app, 'allowed_users', new_users.strip())
|
app_setting(app, 'allowed_users', new_users.strip())
|
||||||
|
|
||||||
|
@ -771,7 +771,7 @@ def app_checkurl(url, app=None):
|
||||||
apps_map = app_map(raw=True)
|
apps_map = app_map(raw=True)
|
||||||
validate(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])*)$', domain)
|
validate(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])*)$', domain)
|
||||||
|
|
||||||
if domain not in domain_list()['Domains']:
|
if domain not in domain_list(YunoHostLDAP())['domains']:
|
||||||
raise YunoHostError(22, _("Domain doesn't exists"))
|
raise YunoHostError(22, _("Domain doesn't exists"))
|
||||||
|
|
||||||
if domain in apps_map:
|
if domain in apps_map:
|
||||||
|
@ -828,11 +828,11 @@ def app_ssowatconf():
|
||||||
with open('/etc/yunohost/current_host', 'r') as f:
|
with open('/etc/yunohost/current_host', 'r') as f:
|
||||||
main_domain = f.readline().rstrip()
|
main_domain = f.readline().rstrip()
|
||||||
|
|
||||||
domains = domain_list()['Domains']
|
domains = domain_list(YunoHostLDAP())['domains']
|
||||||
|
|
||||||
users = {}
|
users = {}
|
||||||
for user in user_list()['Users']:
|
for user in user_list()['users']:
|
||||||
users[user['Username']] = app_map(user=user['Username'])
|
users[user['username']] = app_map(user=user['username'])
|
||||||
|
|
||||||
skipped_urls = []
|
skipped_urls = []
|
||||||
skipped_regex = []
|
skipped_regex = []
|
||||||
|
|
|
@ -23,9 +23,6 @@
|
||||||
|
|
||||||
Manage domains
|
Manage domains
|
||||||
"""
|
"""
|
||||||
import logging
|
|
||||||
logging.warning('the module yunohost.backup has not been revisited and updated yet')
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import datetime
|
import datetime
|
||||||
|
@ -33,14 +30,12 @@ import re
|
||||||
import shutil
|
import shutil
|
||||||
import json
|
import json
|
||||||
import yaml
|
import yaml
|
||||||
import requests
|
|
||||||
from urllib import urlopen
|
from urllib import urlopen
|
||||||
from dyndns import dyndns_subscribe
|
|
||||||
|
|
||||||
from moulinette.helpers import YunoHostError, YunoHostLDAP, win_msg, colorize, validate, get_required_args
|
from moulinette.core import MoulinetteError
|
||||||
|
|
||||||
|
|
||||||
def domain_list(filter=None, limit=None, offset=None):
|
def domain_list(auth, filter=None, limit=None, offset=None):
|
||||||
"""
|
"""
|
||||||
List domains
|
List domains
|
||||||
|
|
||||||
|
@ -50,29 +45,25 @@ def domain_list(filter=None, limit=None, offset=None):
|
||||||
limit -- Maximum number of domain fetched
|
limit -- Maximum number of domain fetched
|
||||||
|
|
||||||
"""
|
"""
|
||||||
with YunoHostLDAP() as yldap:
|
|
||||||
result_list = []
|
result_list = []
|
||||||
if offset: offset = int(offset)
|
|
||||||
else: offset = 0
|
|
||||||
if limit: limit = int(limit)
|
|
||||||
else: limit = 1000
|
|
||||||
if not filter: filter = 'virtualdomain=*'
|
|
||||||
|
|
||||||
result = yldap.search('ou=domains,dc=yunohost,dc=org', filter, attrs=['virtualdomain'])
|
# Set default arguments values
|
||||||
|
if offset is None:
|
||||||
|
offset = 0
|
||||||
|
if limit is None:
|
||||||
|
limit = 1000
|
||||||
|
if filter is None:
|
||||||
|
filter = 'virtualdomain=*'
|
||||||
|
|
||||||
if result and len(result) > (0 + offset) and limit > 0:
|
result = auth.search('ou=domains,dc=yunohost,dc=org', filter, ['virtualdomain'])
|
||||||
i = 0 + offset
|
|
||||||
for domain in result[i:]:
|
if len(result) > offset and limit > 0:
|
||||||
if i <= limit:
|
for domain in result[offset:offset+limit]:
|
||||||
result_list.append(domain['virtualdomain'][0])
|
result_list.append(domain['virtualdomain'][0])
|
||||||
i += 1
|
return { 'domains': result_list }
|
||||||
else:
|
|
||||||
raise YunoHostError(167, _("No domain found"))
|
|
||||||
|
|
||||||
return { 'Domains': result_list }
|
|
||||||
|
|
||||||
|
|
||||||
def domain_add(domains, main=False, dyndns=False):
|
def domain_add(auth, domains, main=False, dyndns=False):
|
||||||
"""
|
"""
|
||||||
Create a custom domain
|
Create a custom domain
|
||||||
|
|
||||||
|
@ -82,7 +73,6 @@ def domain_add(domains, main=False, dyndns=False):
|
||||||
dyndns -- Subscribe to DynDNS
|
dyndns -- Subscribe to DynDNS
|
||||||
|
|
||||||
"""
|
"""
|
||||||
with YunoHostLDAP() as yldap:
|
|
||||||
attr_dict = { 'objectClass' : ['mailDomain', 'top'] }
|
attr_dict = { 'objectClass' : ['mailDomain', 'top'] }
|
||||||
ip = str(urlopen('http://ip.yunohost.org').read())
|
ip = str(urlopen('http://ip.yunohost.org').read())
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
|
@ -93,20 +83,25 @@ def domain_add(domains, main=False, dyndns=False):
|
||||||
domains = [ domains ]
|
domains = [ domains ]
|
||||||
|
|
||||||
for domain in domains:
|
for domain in domains:
|
||||||
try:
|
if domain in domain_list(auth)['domains']:
|
||||||
if domain in domain_list()['Domains']: continue
|
continue
|
||||||
except YunoHostError: pass
|
|
||||||
|
|
||||||
# DynDNS domain
|
# DynDNS domain
|
||||||
if dyndns and len(domain.split('.')) >= 3:
|
if dyndns:
|
||||||
|
if len(domain.split('.')) < 3:
|
||||||
|
raise MoulinetteError(22, _("Invalid domain '%s' for DynDNS" % domain))
|
||||||
|
import requests
|
||||||
|
from yunohost.dyndns import dyndns_subscribe
|
||||||
|
|
||||||
r = requests.get('http://dyndns.yunohost.org/domains')
|
r = requests.get('http://dyndns.yunohost.org/domains')
|
||||||
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 YunoHostError(22, _("You already have a DynDNS domain"))
|
raise MoulinetteError(22, _("You already have a DynDNS domain"))
|
||||||
else:
|
|
||||||
dyndns_subscribe(domain=domain)
|
dyndns_subscribe(domain=domain)
|
||||||
|
else:
|
||||||
|
raise MoulinetteError(22, _("Unknown DynDNS domain '%s'" % dyndomain))
|
||||||
|
|
||||||
# Commands
|
# Commands
|
||||||
ssl_dir = '/usr/share/yunohost/yunohost-config/ssl/yunoCA'
|
ssl_dir = '/usr/share/yunohost/yunohost-config/ssl/yunoCA'
|
||||||
|
@ -134,12 +129,12 @@ def domain_add(domains, main=False, dyndns=False):
|
||||||
|
|
||||||
for command in command_list:
|
for command in command_list:
|
||||||
if os.system(command) != 0:
|
if os.system(command) != 0:
|
||||||
raise YunoHostError(17, _("An error occurred during certificate generation"))
|
raise MoulinetteError(17, _("An error occurred during certificate generation"))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
yldap.validate_uniqueness({ 'virtualdomain' : domain })
|
auth.validate_uniqueness({ 'virtualdomain': domain })
|
||||||
except YunoHostError:
|
except MoulinetteError:
|
||||||
raise YunoHostError(17, _("Domain already created"))
|
raise MoulinetteError(17, _("Domain already created"))
|
||||||
|
|
||||||
|
|
||||||
attr_dict['virtualdomain'] = domain
|
attr_dict['virtualdomain'] = domain
|
||||||
|
@ -172,7 +167,7 @@ def domain_add(domains, main=False, dyndns=False):
|
||||||
os.system('chown bind /var/lib/bind/' + domain + '.zone')
|
os.system('chown bind /var/lib/bind/' + domain + '.zone')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise YunoHostError(17, _("Zone file already exists for ") + domain)
|
raise MoulinetteError(17, _("Zone file already exists for ") + domain)
|
||||||
|
|
||||||
conf_lines = [
|
conf_lines = [
|
||||||
'zone "'+ domain +'" {',
|
'zone "'+ domain +'" {',
|
||||||
|
@ -227,21 +222,20 @@ def domain_add(domains, main=False, dyndns=False):
|
||||||
os.system('sed -i s/yunohost.org/'+ domain +'/g /etc/nginx/conf.d/'+ domain +'.conf')
|
os.system('sed -i s/yunohost.org/'+ domain +'/g /etc/nginx/conf.d/'+ domain +'.conf')
|
||||||
os.system('service nginx reload')
|
os.system('service nginx reload')
|
||||||
|
|
||||||
if yldap.add('virtualdomain=' + domain + ',ou=domains', attr_dict):
|
if auth.add('virtualdomain=' + domain + ',ou=domains', attr_dict):
|
||||||
result.append(domain)
|
result.append(domain)
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
raise YunoHostError(169, _("An error occured during domain creation"))
|
raise MoulinetteError(169, _("An error occurred during domain creation"))
|
||||||
|
|
||||||
|
|
||||||
os.system('yunohost app ssowatconf > /dev/null 2>&1')
|
os.system('yunohost app ssowatconf > /dev/null 2>&1')
|
||||||
|
|
||||||
win_msg(_("Domain(s) successfully created"))
|
msignals.display(_("Domain(s) successfully created."), 'success')
|
||||||
|
return { 'domains': result }
|
||||||
return { 'Domains' : result }
|
|
||||||
|
|
||||||
|
|
||||||
def domain_remove(domains):
|
def domain_remove(auth, domains):
|
||||||
"""
|
"""
|
||||||
Delete domains
|
Delete domains
|
||||||
|
|
||||||
|
@ -249,20 +243,23 @@ def domain_remove(domains):
|
||||||
domains -- Domain(s) to delete
|
domains -- Domain(s) to delete
|
||||||
|
|
||||||
"""
|
"""
|
||||||
with YunoHostLDAP() as yldap:
|
|
||||||
result = []
|
result = []
|
||||||
|
domains_list = domain_list(auth)['domains']
|
||||||
|
|
||||||
if not isinstance(domains, list):
|
if not isinstance(domains, list):
|
||||||
domains = [ domains ]
|
domains = [ domains ]
|
||||||
|
|
||||||
for domain in domains:
|
for domain in domains:
|
||||||
|
if domain not in domains_list:
|
||||||
|
raise MoulinetteError(22, _("Unknown domain '%s'") % domain)
|
||||||
|
|
||||||
# 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:
|
||||||
if yaml.load(f)['domain'] == domain:
|
if yaml.load(f)['domain'] == domain:
|
||||||
raise YunoHostError(1, _("One or more apps are installed on this domain, please uninstall them before proceed to domain removal"))
|
raise MoulinetteError(1, _("One or more apps are installed on this domain, please uninstall them before proceed to domain removal"))
|
||||||
|
|
||||||
if yldap.remove('virtualdomain=' + domain + ',ou=domains'):
|
if auth.remove('virtualdomain=' + domain + ',ou=domains'):
|
||||||
try:
|
try:
|
||||||
shutil.rmtree('/etc/yunohost/certs/'+ domain)
|
shutil.rmtree('/etc/yunohost/certs/'+ domain)
|
||||||
os.remove('/var/lib/bind/'+ domain +'.zone')
|
os.remove('/var/lib/bind/'+ domain +'.zone')
|
||||||
|
@ -287,14 +284,13 @@ def domain_remove(domains):
|
||||||
result.append(domain)
|
result.append(domain)
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
raise YunoHostError(169, _("An error occured during domain deletion"))
|
raise MoulinetteError(169, _("An error occurred during domain deletion"))
|
||||||
|
|
||||||
os.system('yunohost app ssowatconf > /dev/null 2>&1')
|
os.system('yunohost app ssowatconf > /dev/null 2>&1')
|
||||||
os.system('service nginx reload')
|
os.system('service nginx reload')
|
||||||
os.system('service bind9 reload')
|
os.system('service bind9 reload')
|
||||||
os.system('service metronome restart')
|
os.system('service metronome restart')
|
||||||
|
|
||||||
win_msg(_("Domain(s) successfully deleted"))
|
msignals.display(_("Domain(s) successfully deleted."), 'success')
|
||||||
|
return { 'domains': result }
|
||||||
return { 'Domains' : result }
|
|
||||||
|
|
||||||
|
|
|
@ -105,7 +105,7 @@ def user_create(auth, username, firstname, lastname, mail, password):
|
||||||
'mail' : mail
|
'mail' : mail
|
||||||
})
|
})
|
||||||
|
|
||||||
if mail[mail.find('@')+1:] not in domain_list()['Domains']:
|
if mail[mail.find('@')+1:] not in domain_list(auth)['domains']:
|
||||||
raise MoulinetteError(22, _("Unknown domain '%s'") % mail[mail.find('@')+1:])
|
raise MoulinetteError(22, _("Unknown domain '%s'") % mail[mail.find('@')+1:])
|
||||||
|
|
||||||
# Get random UID/GID
|
# Get random UID/GID
|
||||||
|
@ -199,7 +199,7 @@ def user_update(auth, username, firstname=None, lastname=None, mail=None, change
|
||||||
|
|
||||||
attrs_to_fetch = ['givenName', 'sn', 'mail', 'maildrop']
|
attrs_to_fetch = ['givenName', 'sn', 'mail', 'maildrop']
|
||||||
new_attr_dict = {}
|
new_attr_dict = {}
|
||||||
domains = domain_list()['Domains']
|
domains = domain_list(auth)['domains']
|
||||||
|
|
||||||
# Populate user informations
|
# Populate user informations
|
||||||
result = auth.search(base='ou=users,dc=yunohost,dc=org', filter='uid=' + username, attrs=attrs_to_fetch)
|
result = auth.search(base='ou=users,dc=yunohost,dc=org', filter='uid=' + username, attrs=attrs_to_fetch)
|
||||||
|
|
Loading…
Add table
Reference in a new issue