2017-09-24 12:00:39 +02:00
|
|
|
from mailu import app, db, models
|
|
|
|
from mailu.ui import ui, forms, access
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
import flask
|
2018-04-18 20:31:32 +02:00
|
|
|
import flask_login
|
2016-03-22 22:22:49 +02:00
|
|
|
import wtforms_components
|
2018-04-18 20:31:32 +02:00
|
|
|
import dns.resolver
|
2016-03-19 21:37:48 +02:00
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/domain', methods=['GET'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.authenticated
|
2016-03-19 21:37:48 +02:00
|
|
|
def domain_list():
|
|
|
|
return flask.render_template('domain/list.html')
|
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/domain/create', methods=['GET', 'POST'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.global_admin
|
2016-03-19 21:37:48 +02:00
|
|
|
def domain_create():
|
2016-03-22 22:22:49 +02:00
|
|
|
form = forms.DomainForm()
|
2016-03-19 21:37:48 +02:00
|
|
|
if form.validate_on_submit():
|
2017-09-03 18:30:00 +02:00
|
|
|
conflicting_domain = models.Domain.query.get(form.name.data)
|
|
|
|
conflicting_alternative = models.Alternative.query.get(form.name.data)
|
2017-09-10 20:49:49 +02:00
|
|
|
conflicting_relay = models.Relay.query.get(form.name.data)
|
|
|
|
if conflicting_domain or conflicting_alternative or conflicting_relay:
|
2016-03-19 21:37:48 +02:00
|
|
|
flask.flash('Domain %s is already used' % form.name.data, 'error')
|
|
|
|
else:
|
2016-05-01 21:12:08 +02:00
|
|
|
domain = models.Domain()
|
|
|
|
form.populate_obj(domain)
|
2016-03-19 21:37:48 +02:00
|
|
|
db.session.add(domain)
|
|
|
|
db.session.commit()
|
|
|
|
flask.flash('Domain %s created' % domain)
|
2016-03-20 16:36:56 +02:00
|
|
|
return flask.redirect(flask.url_for('.domain_list'))
|
2016-03-19 21:37:48 +02:00
|
|
|
return flask.render_template('domain/create.html', form=form)
|
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/domain/edit/<domain_name>', methods=['GET', 'POST'])
|
2016-08-29 20:22:44 +02:00
|
|
|
@access.global_admin
|
2016-03-19 21:37:48 +02:00
|
|
|
def domain_edit(domain_name):
|
2016-08-29 19:24:39 +02:00
|
|
|
domain = models.Domain.query.get(domain_name) or flask.abort(404)
|
2016-03-22 22:22:49 +02:00
|
|
|
form = forms.DomainForm(obj=domain)
|
|
|
|
wtforms_components.read_only(form.name)
|
2017-01-25 01:05:03 +02:00
|
|
|
form.name.validators = []
|
2016-03-19 21:37:48 +02:00
|
|
|
if form.validate_on_submit():
|
2016-05-01 21:12:08 +02:00
|
|
|
form.populate_obj(domain)
|
2016-03-19 21:37:48 +02:00
|
|
|
db.session.commit()
|
|
|
|
flask.flash('Domain %s saved' % domain)
|
2016-03-20 16:36:56 +02:00
|
|
|
return flask.redirect(flask.url_for('.domain_list'))
|
2016-03-19 21:37:48 +02:00
|
|
|
return flask.render_template('domain/edit.html', form=form,
|
|
|
|
domain=domain)
|
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/domain/delete/<domain_name>', methods=['GET', 'POST'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.global_admin
|
2016-08-29 19:35:09 +02:00
|
|
|
@access.confirmation_required("delete {domain_name}")
|
2016-03-19 21:37:48 +02:00
|
|
|
def domain_delete(domain_name):
|
2016-08-29 19:24:39 +02:00
|
|
|
domain = models.Domain.query.get(domain_name) or flask.abort(404)
|
2016-03-19 21:37:48 +02:00
|
|
|
db.session.delete(domain)
|
|
|
|
db.session.commit()
|
|
|
|
flask.flash('Domain %s deleted' % domain)
|
2016-03-20 16:36:56 +02:00
|
|
|
return flask.redirect(flask.url_for('.domain_list'))
|
2016-06-25 14:51:02 +02:00
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/domain/details/<domain_name>', methods=['GET'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.domain_admin(models.Domain, 'domain_name')
|
2016-06-25 14:51:02 +02:00
|
|
|
def domain_details(domain_name):
|
2016-08-29 19:24:39 +02:00
|
|
|
domain = models.Domain.query.get(domain_name) or flask.abort(404)
|
2017-10-29 19:13:59 +02:00
|
|
|
return flask.render_template('domain/details.html', domain=domain)
|
2016-06-25 15:50:05 +02:00
|
|
|
|
|
|
|
|
2017-09-24 12:00:39 +02:00
|
|
|
@ui.route('/domain/genkeys/<domain_name>', methods=['GET', 'POST'])
|
2016-08-29 19:24:39 +02:00
|
|
|
@access.domain_admin(models.Domain, 'domain_name')
|
2016-08-29 19:35:09 +02:00
|
|
|
@access.confirmation_required("regenerate keys for {domain_name}")
|
2016-06-25 15:50:05 +02:00
|
|
|
def domain_genkeys(domain_name):
|
2016-08-29 19:24:39 +02:00
|
|
|
domain = models.Domain.query.get(domain_name) or flask.abort(404)
|
2016-06-25 15:50:05 +02:00
|
|
|
domain.generate_dkim_key()
|
|
|
|
return flask.redirect(
|
|
|
|
flask.url_for(".domain_details", domain_name=domain_name))
|
2018-04-18 20:31:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
@ui.route('/domain/signup', methods=['GET', 'POST'])
|
|
|
|
def domain_signup(domain_name=None):
|
|
|
|
if not app.config['DOMAIN_REGISTRATION']:
|
|
|
|
flask.abort(403)
|
|
|
|
form = forms.DomainSignupForm()
|
|
|
|
if flask_login.current_user.is_authenticated:
|
|
|
|
del form.localpart
|
|
|
|
del form.pw
|
|
|
|
del form.pw2
|
|
|
|
if form.validate_on_submit():
|
|
|
|
conflicting_domain = models.Domain.query.get(form.name.data)
|
|
|
|
conflicting_alternative = models.Alternative.query.get(form.name.data)
|
|
|
|
conflicting_relay = models.Relay.query.get(form.name.data)
|
|
|
|
hostnames = app.config['HOSTNAMES'].split(',')
|
|
|
|
if conflicting_domain or conflicting_alternative or conflicting_relay:
|
|
|
|
flask.flash('Domain %s is already used' % form.name.data, 'error')
|
|
|
|
else:
|
|
|
|
# Check if the domain MX actually points to this server
|
|
|
|
try:
|
|
|
|
mxok = any(str(rset).split()[-1][:-1] in hostnames
|
|
|
|
for rset in dns.resolver.query(form.name.data, 'MX'))
|
|
|
|
except Exception as e:
|
|
|
|
mxok = False
|
|
|
|
if mxok:
|
|
|
|
# Actually create the domain
|
|
|
|
domain = models.Domain()
|
|
|
|
form.populate_obj(domain)
|
|
|
|
domain.max_quota_bytes = app.config['DEFAULT_QUOTA']
|
|
|
|
domain.max_users = 10
|
|
|
|
domain.max_aliases = 10
|
|
|
|
db.session.add(domain)
|
|
|
|
if flask_login.current_user.is_authenticated:
|
|
|
|
user = models.User.query.get(flask_login.current_user.email)
|
|
|
|
else:
|
|
|
|
user = models.User()
|
|
|
|
user.domain = domain
|
|
|
|
form.populate_obj(user)
|
|
|
|
user.set_password(form.pw.data)
|
|
|
|
user.quota_bytes = domain.max_quota_bytes
|
|
|
|
db.session.add(user)
|
|
|
|
domain.managers.append(user)
|
|
|
|
db.session.commit()
|
|
|
|
flask.flash('Domain %s created' % domain)
|
|
|
|
return flask.redirect(flask.url_for('.domain_list'))
|
|
|
|
else:
|
|
|
|
flask.flash('The MX record was not properly set', 'error')
|
|
|
|
return flask.render_template('domain/signup.html', form=form)
|