1
0
mirror of https://github.com/Mailu/Mailu.git synced 2024-12-16 10:59:53 +02:00
Mailu/core/admin/mailu/internal/views/postfix.py

174 lines
6.2 KiB
Python
Raw Normal View History

2021-08-08 09:21:14 +02:00
from mailu import models, utils
from mailu.internal import internal
2021-08-08 09:21:14 +02:00
from flask import current_app as app
import flask
import idna
import re
2020-01-14 02:18:30 +02:00
import srslib
@internal.route("/postfix/dane/<domain_name>")
def postfix_dane_map(domain_name):
return flask.jsonify('dane-only') if utils.has_dane_record(domain_name) else flask.abort(404)
@internal.route("/postfix/domain/<domain_name>")
def postfix_mailbox_domain(domain_name):
if re.match("^\[.*\]$", domain_name):
return flask.abort(404)
domain = models.Domain.query.get(domain_name) or \
models.Alternative.query.get(domain_name) or \
flask.abort(404)
return flask.jsonify(domain.name)
2019-01-04 22:18:51 +02:00
@internal.route("/postfix/mailbox/<path:email>")
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>")
def postfix_alias_map(alias):
localpart, domain_name = models.Email.resolve_domain(alias)
if localpart is None:
return flask.jsonify(domain_name)
destination = models.Email.resolve_destination(localpart, domain_name)
return flask.jsonify(",".join(destination)) if destination else flask.abort(404)
2019-01-04 22:18:51 +02:00
@internal.route("/postfix/transport/<path:email>")
def postfix_transport(email):
if email == '*' or re.match("(^|.*@)\[.*\]$", email):
return flask.abort(404)
2021-06-16 16:19:31 +02:00
_, domain_name = models.Email.resolve_domain(email)
relay = models.Relay.query.get(domain_name) or flask.abort(404)
target = relay.smtp.lower()
port = None
2021-06-16 16:19:31 +02:00
use_lmtp = False
use_mx = False
# strip prefixes mx: and lmtp:
if target.startswith('mx:'):
target = target[3:]
2021-06-16 16:19:31 +02:00
use_mx = True
elif target.startswith('lmtp:'):
target = target[5:]
use_lmtp = True
# split host:port or [host]:port
if target.startswith('['):
if use_mx or ']' not in target:
2021-06-16 16:19:31 +02:00
# invalid target (mx: and [] or missing ])
flask.abort(400)
host, rest = target[1:].split(']', 1)
if rest.startswith(':'):
port = rest[1:]
elif rest:
# invalid target (rest should be :port)
flask.abort(400)
else:
if ':' in target:
host, port = target.rsplit(':', 1)
else:
host = target
2021-06-16 16:19:31 +02:00
# default for empty host part is mx:domain
if not host:
2021-06-16 16:19:31 +02:00
if not use_lmtp:
host = relay.name.lower()
use_mx = True
else:
# lmtp: needs a host part
flask.abort(400)
# detect ipv6 address or encode host
if ':' in host:
host = f'ipv6:{host}'
else:
try:
host = idna.encode(host).decode('ascii')
except idna.IDNAError:
2021-06-16 16:19:31 +02:00
# invalid host (fqdn not encodable)
flask.abort(400)
2021-06-16 16:19:31 +02:00
# validate port
if port is not None:
try:
port = int(port, 10)
except ValueError:
2021-06-16 16:19:31 +02:00
# invalid port (should be numeric)
flask.abort(400)
2021-06-16 16:19:31 +02:00
# create transport
transport = 'lmtp' if use_lmtp else 'smtp'
# use [] when not using MX lookups or host is an ipv6 address
if host.startswith('ipv6:') or (not use_lmtp and not use_mx):
host = f'[{host}]'
2021-06-16 16:19:31 +02:00
# create port suffix
port = '' if port is None else f':{port}'
2021-06-16 16:19:31 +02:00
return flask.jsonify(f'{transport}:{host}{port}')
2020-01-14 02:18:30 +02:00
@internal.route("/postfix/recipient/map/<path:recipient>")
def postfix_recipient_map(recipient):
""" Rewrite the envelope recipient if it is a valid SRS address.
This is meant for bounces to go back to the original sender.
"""
2021-10-01 15:00:10 +02:00
srs = srslib.SRS(flask.current_app.srs_key)
2020-01-14 02:18:30 +02:00
if srslib.SRS.is_srs_address(recipient):
try:
return flask.jsonify(srs.reverse(recipient))
except srslib.Error as error:
return flask.abort(404)
return flask.abort(404)
@internal.route("/postfix/sender/map/<path:sender>")
def postfix_sender_map(sender):
""" Rewrite the envelope sender in case the mail was not emitted by us.
This is for bounces to come back the reverse path properly.
"""
2021-10-01 15:00:10 +02:00
srs = srslib.SRS(flask.current_app.srs_key)
2020-01-14 02:18:30 +02:00
domain = flask.current_app.config["DOMAIN"]
try:
localpart, domain_name = models.Email.resolve_domain(sender)
except Exception as error:
return flask.abort(404)
if models.Domain.query.get(domain_name):
return flask.abort(404)
return flask.jsonify(srs.forward(sender, domain))
2019-01-04 22:18:51 +02:00
@internal.route("/postfix/sender/login/<path:sender>")
def postfix_sender_login(sender):
2021-08-19 15:21:39 +02:00
wildcard_senders = [s for s in flask.current_app.config.get('WILDCARD_SENDERS', '').lower().replace(' ', '').split(',') if s]
localpart, domain_name = models.Email.resolve_domain(sender)
if localpart is None:
2021-08-19 11:02:03 +02:00
return flask.jsonify(",".join(wildcard_senders)) if wildcard_senders else flask.abort(404)
localpart = localpart[:next((i for i, ch in enumerate(localpart) if ch in flask.current_app.config.get('RECIPIENT_DELIMITER')), None)]
2021-10-09 17:43:09 +02:00
destination = models.Email.resolve_destination(localpart, domain_name, True)
destination = [*destination, *wildcard_senders] if destination else [*wildcard_senders]
return flask.jsonify(",".join(destination)) if destination else flask.abort(404)
2021-08-08 09:21:14 +02:00
@internal.route("/postfix/sender/rate/<path:sender>")
def postfix_sender_rate(sender):
""" Rate limit outbound emails per sender login
"""
user = models.User.get(sender) or flask.abort(404)
2021-08-09 09:28:19 +02:00
return flask.abort(404) if user.sender_limiter.hit() else flask.jsonify("450 4.2.1 You are sending too many emails too fast.")
2019-01-04 22:18:51 +02:00
@internal.route("/postfix/sender/access/<path:sender>")
def postfix_sender_access(sender):
""" Simply reject any sender that pretends to be from a local domain
"""
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