2018-10-18 15:57:43 +02:00
|
|
|
from mailu import models
|
2018-09-27 16:09:38 +02:00
|
|
|
from mailu.internal import internal
|
|
|
|
|
|
|
|
import flask
|
2019-08-14 01:21:25 +02:00
|
|
|
import re
|
2018-09-27 16:09:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
@internal.route("/postfix/domain/<domain_name>")
|
|
|
|
def postfix_mailbox_domain(domain_name):
|
2019-08-14 01:21:25 +02:00
|
|
|
if re.match("^\[.*\]$", domain_name):
|
|
|
|
return flask.abort(404)
|
2018-12-02 12:46:47 +02:00
|
|
|
domain = models.Domain.query.get(domain_name) or \
|
|
|
|
models.Alternative.query.get(domain_name) or \
|
|
|
|
flask.abort(404)
|
2018-09-27 16:09:38 +02:00
|
|
|
return flask.jsonify(domain.name)
|
|
|
|
|
|
|
|
|
2019-01-04 22:18:51 +02:00
|
|
|
@internal.route("/postfix/mailbox/<path:email>")
|
2018-09-27 16:09:38 +02:00
|
|
|
def postfix_mailbox_map(email):
|
|
|
|
user = models.User.query.get(email) or flask.abort(404)
|
|
|
|
return flask.jsonify(user.email)
|
|
|
|
|
|
|
|
|
2019-01-04 22:18:51 +02:00
|
|
|
@internal.route("/postfix/alias/<path:alias>")
|
2018-09-27 16:09:38 +02:00
|
|
|
def postfix_alias_map(alias):
|
2018-10-07 16:24:48 +02:00
|
|
|
localpart, domain_name = models.Email.resolve_domain(alias)
|
2018-09-27 16:09:38 +02:00
|
|
|
if localpart is None:
|
2018-10-07 16:24:48 +02:00
|
|
|
return flask.jsonify(domain_name)
|
|
|
|
destination = models.Email.resolve_destination(localpart, domain_name)
|
|
|
|
return flask.jsonify(",".join(destination)) if destination else flask.abort(404)
|
2018-09-27 16:09:38 +02:00
|
|
|
|
|
|
|
|
2019-01-04 22:18:51 +02:00
|
|
|
@internal.route("/postfix/transport/<path:email>")
|
2018-09-27 16:09:38 +02:00
|
|
|
def postfix_transport(email):
|
2019-08-14 01:21:25 +02:00
|
|
|
if email == '*' or re.match("(^|.*@)\[.*\]$", email):
|
2018-12-04 15:40:07 +02:00
|
|
|
return flask.abort(404)
|
2018-10-23 10:52:15 +02:00
|
|
|
localpart, domain_name = models.Email.resolve_domain(email)
|
2018-10-07 16:24:48 +02:00
|
|
|
relay = models.Relay.query.get(domain_name) or flask.abort(404)
|
2018-09-27 16:30:20 +02:00
|
|
|
return flask.jsonify("smtp:[{}]".format(relay.smtp))
|
2018-10-07 01:52:01 +02:00
|
|
|
|
|
|
|
|
2019-01-04 22:18:51 +02:00
|
|
|
@internal.route("/postfix/sender/login/<path:sender>")
|
2018-10-07 16:24:48 +02:00
|
|
|
def postfix_sender_login(sender):
|
|
|
|
localpart, domain_name = models.Email.resolve_domain(sender)
|
|
|
|
if localpart is None:
|
|
|
|
return flask.abort(404)
|
|
|
|
destination = models.Email.resolve_destination(localpart, domain_name, True)
|
|
|
|
return flask.jsonify(",".join(destination)) if destination else flask.abort(404)
|
|
|
|
|
|
|
|
|
2019-01-04 22:18:51 +02:00
|
|
|
@internal.route("/postfix/sender/access/<path:sender>")
|
2018-10-07 16:24:48 +02:00
|
|
|
def postfix_sender_access(sender):
|
2018-10-07 01:52:01 +02:00
|
|
|
""" Simply reject any sender that pretends to be from a local domain
|
|
|
|
"""
|
2019-01-19 13:42:42 +02:00
|
|
|
if not is_void_address(sender):
|
|
|
|
localpart, domain_name = models.Email.resolve_domain(sender)
|
|
|
|
return flask.jsonify("REJECT") if models.Domain.query.get(domain_name) else flask.abort(404)
|
|
|
|
else:
|
|
|
|
return flask.abort(404)
|
|
|
|
|
|
|
|
|
|
|
|
def is_void_address(email):
|
|
|
|
'''True if the email is void (null) email address.
|
|
|
|
'''
|
|
|
|
if email.startswith('<') and email.endswith('>'):
|
|
|
|
email = email[1:-1]
|
|
|
|
# Some MTAs use things like '<MAILER-DAEMON>' instead of '<>'; so let's
|
|
|
|
# consider void any such thing.
|
|
|
|
return '@' not in email
|