2018-10-18 15:57:43 +02:00
|
|
|
from mailu import models
|
|
|
|
from flask import current_app as app
|
2017-09-24 15:43:23 +02:00
|
|
|
|
2018-01-24 01:55:43 +02:00
|
|
|
import re
|
2017-10-21 15:22:40 +02:00
|
|
|
import socket
|
2017-10-22 10:49:31 +02:00
|
|
|
import urllib
|
2017-10-21 15:22:40 +02:00
|
|
|
|
2017-09-24 15:43:23 +02:00
|
|
|
|
|
|
|
SUPPORTED_AUTH_METHODS = ["none", "plain"]
|
|
|
|
|
2017-10-22 16:43:06 +02:00
|
|
|
|
2017-09-24 15:43:23 +02:00
|
|
|
STATUSES = {
|
|
|
|
"authentication": ("Authentication credentials invalid", {
|
|
|
|
"imap": "AUTHENTICATIONFAILED",
|
|
|
|
"smtp": "535 5.7.8",
|
2017-11-10 11:14:49 +02:00
|
|
|
"pop3": "-ERR Authentication failed"
|
2017-09-24 15:43:23 +02:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def handle_authentication(headers):
|
|
|
|
""" Handle an HTTP nginx authentication request
|
|
|
|
See: http://nginx.org/en/docs/mail/ngx_mail_auth_http_module.html#protocol
|
|
|
|
"""
|
|
|
|
method = headers["Auth-Method"]
|
|
|
|
protocol = headers["Auth-Protocol"]
|
|
|
|
# Incoming mail, no authentication
|
|
|
|
if method == "none" and protocol == "smtp":
|
2017-10-22 15:44:44 +02:00
|
|
|
server, port = get_server(headers["Auth-Protocol"], False)
|
2017-09-24 15:43:23 +02:00
|
|
|
return {
|
|
|
|
"Auth-Status": "OK",
|
|
|
|
"Auth-Server": server,
|
|
|
|
"Auth-Port": port
|
|
|
|
}
|
|
|
|
# Authenticated user
|
|
|
|
elif method == "plain":
|
2017-10-22 15:44:44 +02:00
|
|
|
server, port = get_server(headers["Auth-Protocol"], True)
|
2017-10-22 10:49:31 +02:00
|
|
|
user_email = urllib.parse.unquote(headers["Auth-User"])
|
|
|
|
password = urllib.parse.unquote(headers["Auth-Pass"])
|
2017-10-29 16:39:28 +02:00
|
|
|
ip = urllib.parse.unquote(headers["Client-Ip"])
|
2017-09-24 15:43:23 +02:00
|
|
|
user = models.User.query.get(user_email)
|
2017-11-21 21:46:18 +02:00
|
|
|
status = False
|
2017-11-05 19:12:50 +02:00
|
|
|
if user:
|
|
|
|
for token in user.tokens:
|
|
|
|
if (token.check_password(password) and
|
|
|
|
(not token.ip or token.ip == ip)):
|
2017-11-21 21:46:18 +02:00
|
|
|
status = True
|
2017-11-05 19:12:50 +02:00
|
|
|
if user.check_password(password):
|
2017-11-21 21:46:18 +02:00
|
|
|
status = True
|
|
|
|
if status:
|
|
|
|
if protocol == "imap" and not user.enable_imap:
|
|
|
|
status = False
|
|
|
|
elif protocol == "pop3" and not user.enable_pop:
|
|
|
|
status = False
|
2018-04-15 13:43:30 +02:00
|
|
|
if status and user.enabled:
|
2017-11-21 21:46:18 +02:00
|
|
|
return {
|
|
|
|
"Auth-Status": "OK",
|
|
|
|
"Auth-Server": server,
|
|
|
|
"Auth-Port": port
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
status, code = get_status(protocol, "authentication")
|
|
|
|
return {
|
|
|
|
"Auth-Status": status,
|
|
|
|
"Auth-Error-Code": code,
|
|
|
|
"Auth-Wait": 0
|
|
|
|
}
|
2017-09-24 15:43:23 +02:00
|
|
|
# Unexpected
|
2017-11-10 11:14:49 +02:00
|
|
|
return {}
|
2017-09-24 15:43:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_status(protocol, status):
|
|
|
|
""" Return the proper error code depending on the protocol
|
|
|
|
"""
|
|
|
|
status, codes = STATUSES[status]
|
|
|
|
return status, codes[protocol]
|
|
|
|
|
2018-01-24 01:55:43 +02:00
|
|
|
def extract_host_port(host_and_port, default_port):
|
|
|
|
host, _, port = re.match('^(.*)(:([0-9]*))?$', host_and_port).groups()
|
|
|
|
return host, int(port) if port else default_port
|
2017-09-24 15:43:23 +02:00
|
|
|
|
2017-10-22 15:44:44 +02:00
|
|
|
def get_server(protocol, authenticated=False):
|
|
|
|
if protocol == "imap":
|
2018-01-24 01:55:43 +02:00
|
|
|
hostname, port = extract_host_port(app.config['HOST_IMAP'], 143)
|
2017-10-22 15:44:44 +02:00
|
|
|
elif protocol == "pop3":
|
2018-01-24 01:55:43 +02:00
|
|
|
hostname, port = extract_host_port(app.config['HOST_POP3'], 110)
|
2017-10-22 15:44:44 +02:00
|
|
|
elif protocol == "smtp":
|
2018-01-24 01:55:43 +02:00
|
|
|
if authenticated:
|
|
|
|
hostname, port = extract_host_port(app.config['HOST_AUTHSMTP'], 10025)
|
|
|
|
else:
|
|
|
|
hostname, port = extract_host_port(app.config['HOST_SMTP'], 25)
|
2017-10-21 15:22:40 +02:00
|
|
|
address = socket.gethostbyname(hostname)
|
|
|
|
return address, port
|