mirror of
https://github.com/chatmail/relay.git
synced 2026-05-14 18:04:38 +00:00
Compare commits
2 Commits
link2xt/au
...
hpk/readme
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d5c14e20a7 | ||
|
|
1ba3c410dd |
@@ -21,32 +21,19 @@ def encrypt_password(password: str):
|
|||||||
return "{SHA512-CRYPT}" + passhash
|
return "{SHA512-CRYPT}" + passhash
|
||||||
|
|
||||||
|
|
||||||
def is_allowed_to_create(user, cleartext_password) -> bool:
|
def check_password(password) -> bool:
|
||||||
"""Return True if user and password are admissable."""
|
"""Check password policy"""
|
||||||
if os.path.exists(NOCREATE_FILE):
|
if len(password) < 10:
|
||||||
logging.warning(f"blocked account creation because {NOCREATE_FILE!r} exists.")
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if len(cleartext_password) < 10:
|
|
||||||
logging.warning("Password needs to be at least 10 characters long")
|
|
||||||
return False
|
|
||||||
|
|
||||||
parts = user.split("@")
|
|
||||||
if len(parts) != 2:
|
|
||||||
logging.warning(f"user {user!r} is not a proper e-mail address")
|
|
||||||
return False
|
|
||||||
localpart, domain = parts
|
|
||||||
|
|
||||||
if domain == "nine.testrun.org":
|
|
||||||
# nine.testrun.org policy, username has to be exactly nine chars
|
|
||||||
if len(localpart) != 9:
|
|
||||||
logging.warning(f"localpart {localpart!r} has not exactly nine chars")
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def create_user(db, user, encrypted_password):
|
def create_user(db, user, encrypted_password):
|
||||||
|
if os.path.exists(NOCREATE_FILE):
|
||||||
|
logging.warning(
|
||||||
|
f"Didn't create account: {NOCREATE_FILE} exists. Delete the file to enable account creation."
|
||||||
|
)
|
||||||
|
return
|
||||||
with db.write_transaction() as conn:
|
with db.write_transaction() as conn:
|
||||||
conn.create_user(user, encrypted_password)
|
conn.create_user(user, encrypted_password)
|
||||||
return dict(
|
return dict(
|
||||||
@@ -70,18 +57,19 @@ def lookup_userdb(db, user):
|
|||||||
return get_user_data(db, user)
|
return get_user_data(db, user)
|
||||||
|
|
||||||
|
|
||||||
def lookup_passdb(db, user, cleartext_password):
|
def lookup_passdb(db, user, password):
|
||||||
userdata = get_user_data(db, user)
|
userdata = get_user_data(db, user)
|
||||||
if not userdata:
|
if not userdata:
|
||||||
if not is_allowed_to_create(user, cleartext_password):
|
if not check_password(password):
|
||||||
|
logging.warning("Attempt to create an account with a weak password.")
|
||||||
return
|
return
|
||||||
encrypted_password = encrypt_password(cleartext_password)
|
return create_user(db, user, encrypt_password(password))
|
||||||
userdata = create_user(db=db, user=user, encrypted_password=encrypted_password)
|
|
||||||
userdata["password"] = userdata["password"].strip()
|
userdata["password"] = userdata["password"].strip()
|
||||||
return userdata
|
return userdata
|
||||||
|
|
||||||
|
|
||||||
def handle_dovecot_request(msg, db, mail_domain):
|
def handle_dovecot_request(msg, db, mail_domain):
|
||||||
|
print(f"received msg: {msg!r}", file=sys.stderr)
|
||||||
short_command = msg[0]
|
short_command = msg[0]
|
||||||
if short_command == "L": # LOOKUP
|
if short_command == "L": # LOOKUP
|
||||||
parts = msg[1:].split("\t")
|
parts = msg[1:].split("\t")
|
||||||
@@ -99,11 +87,12 @@ def handle_dovecot_request(msg, db, mail_domain):
|
|||||||
reply_command = "N"
|
reply_command = "N"
|
||||||
elif type == "passdb":
|
elif type == "passdb":
|
||||||
if user.endswith(f"@{mail_domain}"):
|
if user.endswith(f"@{mail_domain}"):
|
||||||
res = lookup_passdb(db, user, cleartext_password=args[0])
|
res = lookup_passdb(db, user, password=args[0])
|
||||||
if res:
|
if res:
|
||||||
reply_command = "O"
|
reply_command = "O"
|
||||||
else:
|
else:
|
||||||
reply_command = "N"
|
reply_command = "N"
|
||||||
|
print(f"res: {res!r}", file=sys.stderr)
|
||||||
json_res = json.dumps(res) if res else ""
|
json_res = json.dumps(res) if res else ""
|
||||||
return f"{reply_command}{json_res}\n"
|
return f"{reply_command}{json_res}\n"
|
||||||
return None
|
return None
|
||||||
@@ -128,6 +117,7 @@ def main():
|
|||||||
break
|
break
|
||||||
res = handle_dovecot_request(msg, db, mail_domain)
|
res = handle_dovecot_request(msg, db, mail_domain)
|
||||||
if res:
|
if res:
|
||||||
|
print(f"sending result: {res!r}", file=sys.stderr)
|
||||||
self.wfile.write(res.encode("ascii"))
|
self.wfile.write(res.encode("ascii"))
|
||||||
self.wfile.flush()
|
self.wfile.flush()
|
||||||
|
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ def _configure_nginx(domain: str, debug: bool = False) -> bool:
|
|||||||
need_restart = False
|
need_restart = False
|
||||||
|
|
||||||
main_config = files.template(
|
main_config = files.template(
|
||||||
src=importlib.resources.files(__package__).joinpath("nginx/nginx.conf.j2"),
|
src=importlib.resources.files(__package__).joinpath("nginx.conf.j2"),
|
||||||
dest="/etc/nginx/nginx.conf",
|
dest="/etc/nginx/nginx.conf",
|
||||||
user="root",
|
user="root",
|
||||||
group="root",
|
group="root",
|
||||||
@@ -212,7 +212,7 @@ def _configure_nginx(domain: str, debug: bool = False) -> bool:
|
|||||||
need_restart |= main_config.changed
|
need_restart |= main_config.changed
|
||||||
|
|
||||||
autoconfig = files.template(
|
autoconfig = files.template(
|
||||||
src=importlib.resources.files(__package__).joinpath("nginx/autoconfig.xml.j2"),
|
src=importlib.resources.files(__package__).joinpath("autoconfig.xml.j2"),
|
||||||
dest="/var/www/html/.well-known/autoconfig/mail/config-v1.1.xml",
|
dest="/var/www/html/.well-known/autoconfig/mail/config-v1.1.xml",
|
||||||
user="root",
|
user="root",
|
||||||
group="root",
|
group="root",
|
||||||
@@ -277,12 +277,6 @@ def deploy_chatmail(mail_domain: str, mail_server: str, dkim_selector: str) -> N
|
|||||||
opendkim_need_restart = _configure_opendkim(mail_domain, dkim_selector)
|
opendkim_need_restart = _configure_opendkim(mail_domain, dkim_selector)
|
||||||
nginx_need_restart = _configure_nginx(mail_domain)
|
nginx_need_restart = _configure_nginx(mail_domain)
|
||||||
|
|
||||||
# deploy web pages and info if we have them
|
|
||||||
pkg_root = importlib.resources.files(__package__)
|
|
||||||
www_path = pkg_root.joinpath(f"../../../www/{mail_domain}").resolve()
|
|
||||||
if www_path.is_dir():
|
|
||||||
files.rsync(f"{www_path}/", "/var/www/html", flags=["-avz"])
|
|
||||||
|
|
||||||
systemd.service(
|
systemd.service(
|
||||||
name="Start and enable OpenDKIM",
|
name="Start and enable OpenDKIM",
|
||||||
service="opendkim.service",
|
service="opendkim.service",
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
import os
|
import os
|
||||||
import json
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import chatmaild.dictproxy
|
import chatmaild.dictproxy
|
||||||
from chatmaild.dictproxy import get_user_data, lookup_passdb, handle_dovecot_request
|
from chatmaild.dictproxy import get_user_data, lookup_passdb
|
||||||
from chatmaild.database import Database, DBError
|
from chatmaild.database import Database, DBError
|
||||||
|
|
||||||
|
|
||||||
@@ -15,13 +14,13 @@ def db(tmpdir):
|
|||||||
return Database(db_path)
|
return Database(db_path)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_basic(db):
|
def test_basic(db):
|
||||||
|
chatmaild.dictproxy.NOCREATE_FILE = "/tmp/nocreate"
|
||||||
|
if os.path.exists(chatmaild.dictproxy.NOCREATE_FILE):
|
||||||
|
os.remove(chatmaild.dictproxy.NOCREATE_FILE)
|
||||||
lookup_passdb(db, "link2xt@c1.testrun.org", "Pieg9aeToe3eghuthe5u")
|
lookup_passdb(db, "link2xt@c1.testrun.org", "Pieg9aeToe3eghuthe5u")
|
||||||
data = get_user_data(db, "link2xt@c1.testrun.org")
|
data = get_user_data(db, "link2xt@c1.testrun.org")
|
||||||
assert data
|
assert data
|
||||||
data2 = lookup_passdb(db, "link2xt@c1.testrun.org", "Pieg9aeToe3eghuthe5u")
|
|
||||||
assert data == data2
|
|
||||||
|
|
||||||
|
|
||||||
def test_dont_overwrite_password_on_wrong_login(db):
|
def test_dont_overwrite_password_on_wrong_login(db):
|
||||||
@@ -33,12 +32,14 @@ def test_dont_overwrite_password_on_wrong_login(db):
|
|||||||
assert res["password"] == res2["password"]
|
assert res["password"] == res2["password"]
|
||||||
|
|
||||||
|
|
||||||
def test_nocreate_file(db, monkeypatch, tmpdir):
|
def test_nocreate_file(db):
|
||||||
p = tmpdir.join("nocreate")
|
chatmaild.dictproxy.NOCREATE_FILE = "/tmp/nocreate"
|
||||||
p.write("")
|
with open(chatmaild.dictproxy.NOCREATE_FILE, "w+") as f:
|
||||||
monkeypatch.setattr(chatmaild.dictproxy, "NOCREATE_FILE", str(p))
|
f.write("")
|
||||||
|
assert os.path.exists(chatmaild.dictproxy.NOCREATE_FILE)
|
||||||
lookup_passdb(db, "newuser1@something.org", "zequ0Aimuchoodaechik")
|
lookup_passdb(db, "newuser1@something.org", "zequ0Aimuchoodaechik")
|
||||||
assert not get_user_data(db, "newuser1@something.org")
|
assert not get_user_data(db, "newuser1@something.org")
|
||||||
|
os.remove(chatmaild.dictproxy.NOCREATE_FILE)
|
||||||
|
|
||||||
|
|
||||||
def test_db_version(db):
|
def test_db_version(db):
|
||||||
@@ -50,15 +51,3 @@ def test_too_high_db_version(db):
|
|||||||
conn.execute("PRAGMA user_version=%s;" % (999,))
|
conn.execute("PRAGMA user_version=%s;" % (999,))
|
||||||
with pytest.raises(DBError):
|
with pytest.raises(DBError):
|
||||||
db.ensure_tables()
|
db.ensure_tables()
|
||||||
|
|
||||||
|
|
||||||
def test_handle_dovecot_request(db):
|
|
||||||
msg = ('Lshared/passdb/laksjdlaksjdlaksjdlk12j3l1k2j3123/'
|
|
||||||
'some42@c3.testrun.org\tsome42@c3.testrun.org')
|
|
||||||
res = handle_dovecot_request(msg, db, "c3.testrun.org")
|
|
||||||
assert res
|
|
||||||
assert res[0] == "O" and res.endswith("\n")
|
|
||||||
userdata = json.loads(res[1:].strip())
|
|
||||||
assert userdata["home"] == "/home/vmail/some42@c3.testrun.org"
|
|
||||||
assert userdata["uid"] == userdata["gid"] == "vmail"
|
|
||||||
assert userdata["password"].startswith("{SHA512-CRYPT}")
|
|
||||||
|
|||||||
@@ -195,8 +195,8 @@ def gencreds(maildomain):
|
|||||||
num = next(count)
|
num = next(count)
|
||||||
alphanumeric = "abcdefghijklmnopqrstuvwxyz1234567890"
|
alphanumeric = "abcdefghijklmnopqrstuvwxyz1234567890"
|
||||||
user = "".join(random.choices(alphanumeric, k=10))
|
user = "".join(random.choices(alphanumeric, k=10))
|
||||||
user = f"ac{num}_{user}"[:9]
|
user = f"ac{num}_{user}"
|
||||||
password = "".join(random.choices(alphanumeric, k=12))
|
password = "".join(random.choices(alphanumeric, k=10))
|
||||||
yield f"{user}@{domain}", f"{password}"
|
yield f"{user}@{domain}", f"{password}"
|
||||||
|
|
||||||
return lambda domain=None: next(gen(domain))
|
return lambda domain=None: next(gen(domain))
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 96 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 66 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 163 KiB |
@@ -1,61 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<title>nine.testrun.org - Experimenting with the Future of Email</title>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
||||||
<style>
|
|
||||||
.wrapper {
|
|
||||||
width: 100%;
|
|
||||||
max-width: 596px;
|
|
||||||
margin: 0 auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.section {
|
|
||||||
width: 100%;
|
|
||||||
max-width: 596px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.text {
|
|
||||||
box-sizing: border-box;
|
|
||||||
padding: 9px;
|
|
||||||
font-size: 18px;
|
|
||||||
font-family: "Courier New", monospace;
|
|
||||||
color: white;
|
|
||||||
background-position: left top;
|
|
||||||
background-image: url(collage-bg.png);
|
|
||||||
background-repeat: no-repeat;
|
|
||||||
background-size: 100% 100%;
|
|
||||||
}
|
|
||||||
h1, h2, h3 {
|
|
||||||
font-size: 16px;
|
|
||||||
font-weight: bold;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="wrapper">
|
|
||||||
<img class="section" src="collage-top.png" />
|
|
||||||
<div class="section text">
|
|
||||||
<h1>welcome to nine.testrun.org</h1>
|
|
||||||
<p>
|
|
||||||
to get an account,
|
|
||||||
invent a word with <i>exactly</i> nine characters
|
|
||||||
and append @nine.testrun.org to it.
|
|
||||||
eg. <b>hellofits@nine.testrun.org</b>
|
|
||||||
</p>
|
|
||||||
<p>
|
|
||||||
if the email address is not yet taken, you'll get that account.
|
|
||||||
the first login sets your password.
|
|
||||||
that's it.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<img class="section" src="collage-down.png" />
|
|
||||||
<div class="section text">
|
|
||||||
<h1>faq</h1>
|
|
||||||
<p><i>why are other email providers 1000 times more complicated?</i></p>
|
|
||||||
<p>because they want to for $reasons</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
Reference in New Issue
Block a user