Sunil Mohan Adapa ae882fea70
email_server: Simplify domain configuration form
- By default, receive mail for all the domains on the system.

- Allow user to select a primary domain. This domain is used for TLS
certificate, automatically adding domain to sender address, etc.

- Don't expose postfix configuration parameters.

Tests:

- On installation, the domain list populated in postfix. Primary domain is
the one set in the config module. If it is not set, any other domain from
configured domains is taken.

- When not installed, adding/removing domains does not cause errors.

- Changing the domain in the domain view works. mydomain has the primary domain
set. myhostname has primary domain set. mydestination has default values and in
addition has all the domains on the system.

- /etc/mailname is populated with the primary domain.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2021-12-06 17:39:43 -05:00

58 lines
1.9 KiB
Python

"""Configure email domains"""
# SPDX-License-Identifier: AGPL-3.0-or-later
import pathlib
import re
import subprocess
from plinth.actions import superuser_run
from plinth.modules import config
from plinth.modules.email_server import postconf
from plinth.modules.names.components import DomainName
def get_domains():
"""Return the current domain configuration."""
conf = postconf.get_many(['mydomain', 'mydestination'])
domains = set(postconf.parse_maps(conf['mydestination']))
defaults = {'$myhostname', 'localhost.$mydomain', 'localhost'}
domains.difference_update(defaults)
return {'primary_domain': conf['mydomain'], 'all_domains': domains}
def set_domains(primary_domain=None):
"""Set the primary domain and all the domains for postfix. """
all_domains = DomainName.list_names()
if not primary_domain:
primary_domain = get_domains()['primary_domain']
if primary_domain not in all_domains:
primary_domain = config.get_domainname() or list(all_domains)[0]
superuser_run(
'email_server',
['domain', 'set_domains', primary_domain, ','.join(all_domains)])
def action_set_domains(primary_domain, all_domains):
"""Set the primary domain and all the domains for postfix. """
all_domains = [_clean_domain(domain) for domain in all_domains.split(',')]
primary_domain = _clean_domain(primary_domain)
defaults = {'$myhostname', 'localhost.$mydomain', 'localhost'}
all_domains = set(all_domains).union(defaults)
conf = {
'myhostname': primary_domain,
'mydomain': primary_domain,
'mydestination': ', '.join(all_domains)
}
postconf.set_many(conf)
pathlib.Path('/etc/mailname').write_text(primary_domain + '\n')
subprocess.run(['systemctl', 'try-reload-or-restart', 'postfix'],
check=True)
def _clean_domain(domain):
domain = domain.lower().strip()
assert re.match('^[a-z0-9-\\.]+$', domain)
return domain