mirror of
https://github.com/chatmail/relay.git
synced 2026-05-10 16:04:37 +00:00
Compare commits
3 Commits
simple-per
...
hpk/lua-sp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4ba5cd0ce3 | ||
|
|
e918b3a507 | ||
|
|
16680014ff |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -10,7 +10,6 @@ __pycache__/
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
doveauth/dist/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
|
||||
59
README.md
59
README.md
@@ -4,58 +4,7 @@ This package deploys Postfix and Dovecot servers, including OpenDKIM for DKIM si
|
||||
|
||||
Postfix uses Dovecot for authentication as described in <https://www.postfix.org/SASL_README.html#server_dovecot>
|
||||
|
||||
## Getting started
|
||||
|
||||
prepare:
|
||||
|
||||
pip install -e chatmail-infra
|
||||
|
||||
|
||||
then run with pyinfra command line tool:
|
||||
|
||||
CHATMAIL_DOMAIN=c1.testrun.org pyinfra --ssh-user root c1.testrun.org deploy.py
|
||||
|
||||
|
||||
## Structure (wip)
|
||||
```
|
||||
|
||||
# package doveauth tool and deploy chatmail server to a envvar-specified ssh-reachable host
|
||||
deploy.py
|
||||
|
||||
# chatmail pyinfra deploy package
|
||||
chatmail-pyinfra
|
||||
pyproject.toml
|
||||
chatmail/__init__ ...
|
||||
|
||||
# tests against the deployed system
|
||||
tests/test_online_test.py
|
||||
|
||||
# doveauth tool used by dovecot's auth mechanism on the host system
|
||||
doveauth
|
||||
README.md
|
||||
pyproject.toml
|
||||
doveauth.py
|
||||
doveauth.lua
|
||||
test_doveauth.py
|
||||
|
||||
# lmtp server to block (outgoing) unencrypted messages
|
||||
filtermail
|
||||
README.md
|
||||
pyproject.toml
|
||||
....
|
||||
|
||||
|
||||
# scripts for setup/development/deployment
|
||||
|
||||
scripts/
|
||||
init.sh # create venv/other perequires
|
||||
deploy.sh # run pyinfra based deploy of everything
|
||||
|
||||
```
|
||||
|
||||
## Dovecot/Postfix configuration
|
||||
|
||||
### Ports
|
||||
## Ports
|
||||
|
||||
Postfix listens on ports 25 (smtp) and 587 (submission) and 465 (submissions).
|
||||
Dovecot listens on ports 143(imap) and 993 (imaps).
|
||||
@@ -63,3 +12,9 @@ Dovecot listens on ports 143(imap) and 993 (imaps).
|
||||
## DNS
|
||||
|
||||
For DKIM you must add a DNS entry as in /etc/opendkim/selector.txt (where selector is the opendkim_selector configured in the chatmail inventory).
|
||||
|
||||
## Run with pyinfra
|
||||
|
||||
```
|
||||
CHATMAIL_DOMAIN=c1.testrun.org pyinfra c1.testrun.org deploy.py
|
||||
```
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=45"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "chatmail"
|
||||
version = "0.1"
|
||||
dependencies = [
|
||||
"pyinfra",
|
||||
]
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
addopts = "-v -ra --strict-markers"
|
||||
|
||||
[tool.tox]
|
||||
legacy_tox_ini = """
|
||||
[tox]
|
||||
isolated_build = true
|
||||
envlist = lint
|
||||
|
||||
[testenv:lint]
|
||||
skipdist = True
|
||||
skip_install = True
|
||||
deps =
|
||||
ruff
|
||||
black
|
||||
commands =
|
||||
black --quiet --check --diff src/
|
||||
ruff src/
|
||||
"""
|
||||
@@ -1,28 +0,0 @@
|
||||
import pytest
|
||||
import imaplib
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def conn():
|
||||
return connect("c1.testrun.org")
|
||||
|
||||
|
||||
def login(conn, user, password):
|
||||
print("trying to login", user, password)
|
||||
conn.login(user, password)
|
||||
|
||||
|
||||
def connect(host):
|
||||
print(f"connecting to {host}")
|
||||
conn = imaplib.IMAP4_SSL(host)
|
||||
return conn
|
||||
|
||||
|
||||
def test_login_ok(conn):
|
||||
login(conn, "link2xt@c1.testrun.org", "Ahyei6ie")
|
||||
|
||||
|
||||
def test_login_fail(conn):
|
||||
with pytest.raises(imaplib.IMAP4.error) as excinfo:
|
||||
login(conn, "link2xt@c1.testrun.org", "qweqwe")
|
||||
assert "AUTHENTICATIONFAILED" in str(excinfo)
|
||||
@@ -1,5 +1,5 @@
|
||||
import os
|
||||
import pyinfra
|
||||
from pyinfra import host, facts
|
||||
from chatmail import deploy_chatmail
|
||||
|
||||
|
||||
@@ -8,12 +8,7 @@ def main():
|
||||
mail_server = os.getenv("CHATMAIL_SERVER", mail_domain)
|
||||
dkim_selector = os.getenv("CHATMAIL_DKIM_SELECTOR", "2023")
|
||||
|
||||
assert mail_domain
|
||||
assert mail_server
|
||||
assert dkim_selector
|
||||
|
||||
deploy_chatmail(mail_domain, mail_server, dkim_selector)
|
||||
|
||||
|
||||
if pyinfra.is_cli:
|
||||
main()
|
||||
main()
|
||||
@@ -1,7 +0,0 @@
|
||||
# doveauth
|
||||
|
||||
doveauth is a python tool
|
||||
to create dovecot users on login.
|
||||
It is called by the
|
||||
[dovecot lua authentication module](https://doc.dovecot.org/configuration_manual/authentication/lua_based_authentication/)
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=45"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "doveauth"
|
||||
version = "0.1"
|
||||
|
||||
[project.scripts]
|
||||
doveauth = "doveauth.doveauth:main"
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
addopts = "-v -ra --strict-markers"
|
||||
|
||||
[tool.tox]
|
||||
legacy_tox_ini = """
|
||||
[tox]
|
||||
isolated_build = true
|
||||
envlist = lint
|
||||
|
||||
[testenv:lint]
|
||||
skipdist = True
|
||||
skip_install = True
|
||||
deps =
|
||||
ruff
|
||||
black
|
||||
commands =
|
||||
black --quiet --check --diff src/
|
||||
ruff src/
|
||||
"""
|
||||
@@ -1,153 +0,0 @@
|
||||
import sqlite3
|
||||
import contextlib
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class DBError(Exception):
|
||||
"""error during an operation on the database."""
|
||||
|
||||
|
||||
class Connection:
|
||||
def __init__(self, sqlconn, write):
|
||||
self._sqlconn = sqlconn
|
||||
self._write = write
|
||||
|
||||
def close(self):
|
||||
self._sqlconn.close()
|
||||
|
||||
def commit(self):
|
||||
self._sqlconn.commit()
|
||||
|
||||
def rollback(self):
|
||||
self._sqlconn.rollback()
|
||||
|
||||
def execute(self, query, params=()):
|
||||
cur = self.cursor()
|
||||
try:
|
||||
cur.execute(query, params)
|
||||
except sqlite3.IntegrityError as e:
|
||||
raise DBError(e)
|
||||
return cur
|
||||
|
||||
def cursor(self):
|
||||
return self._sqlconn.cursor()
|
||||
|
||||
def create_user(self, addr: str, password: str):
|
||||
"""Create a row in the users table."""
|
||||
self.execute("PRAGMA foreign_keys=on;")
|
||||
q = """INSERT INTO users (addr, password, last_login)
|
||||
VALUES (?, ?, ?)"""
|
||||
self.execute(q, (addr, password, int(time.time())))
|
||||
|
||||
def get_user(self, addr: str) -> {}:
|
||||
"""Get a row from the users table."""
|
||||
q = "SELECT addr, password, last_login from users WHERE addr = ?"
|
||||
row = self._sqlconn.execute(q, (addr,)).fetchone()
|
||||
result = {}
|
||||
if row:
|
||||
result = dict(
|
||||
user=row[0],
|
||||
password=row[1],
|
||||
last_login=row[2],
|
||||
)
|
||||
return result
|
||||
|
||||
def set_config(self, name: str, value: str) -> str:
|
||||
ok = [
|
||||
"dbversion",
|
||||
]
|
||||
assert name in ok, name
|
||||
q = "INSERT OR REPLACE INTO config (key, value) VALUES (?, ?)"
|
||||
self.cursor().execute(q, (name, value)).fetchone()
|
||||
return value
|
||||
|
||||
def get_config(self, key: str) -> str:
|
||||
q = "SELECT key, value from config WHERE name = ?"
|
||||
c = self._sqlconn.cursor()
|
||||
try:
|
||||
return c.execute(q, key).fetchone()
|
||||
except sqlite3.OperationalError:
|
||||
return None
|
||||
|
||||
|
||||
class Database:
|
||||
def __init__(self, path: str):
|
||||
self.path = Path(path)
|
||||
self.ensure_tables()
|
||||
|
||||
def _get_connection(self, write=False, transaction=False, closing=False) -> Connection:
|
||||
# we let the database serialize all writers at connection time
|
||||
# to play it very safe (we don't have massive amounts of writes).
|
||||
mode = "ro"
|
||||
if write:
|
||||
mode = "rw"
|
||||
if not self.path.exists():
|
||||
mode = "rwc"
|
||||
uri = "file:%s?mode=%s" % (self.path, mode)
|
||||
sqlconn = sqlite3.connect(
|
||||
uri,
|
||||
timeout=60,
|
||||
isolation_level=None if transaction else "DEFERRED",
|
||||
uri=True,
|
||||
)
|
||||
|
||||
# Enable Write-Ahead Logging to avoid readers blocking writers and vice versa.
|
||||
if write:
|
||||
sqlconn.execute("PRAGMA journal_mode=wal")
|
||||
|
||||
if transaction:
|
||||
start_time = time.time()
|
||||
while 1:
|
||||
try:
|
||||
sqlconn.execute("begin immediate")
|
||||
break
|
||||
except sqlite3.OperationalError:
|
||||
# another thread may be writing, give it a chance to finish
|
||||
time.sleep(0.1)
|
||||
if time.time() - start_time > 5:
|
||||
# if it takes this long, something is wrong
|
||||
raise
|
||||
conn = Connection(sqlconn, write=write)
|
||||
if closing:
|
||||
conn = contextlib.closing(conn)
|
||||
return conn
|
||||
|
||||
@contextlib.contextmanager
|
||||
def write_transaction(self):
|
||||
conn = self._get_connection(closing=False, write=True, transaction=True)
|
||||
try:
|
||||
yield conn
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
conn.close()
|
||||
raise
|
||||
else:
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def read_connection(self, closing=True) -> Connection:
|
||||
return self._get_connection(closing=closing, write=False)
|
||||
|
||||
CURRENT_DBVERSION = 1
|
||||
|
||||
def ensure_tables(self):
|
||||
with self.write_transaction() as conn:
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
addr TEXT PRIMARY KEY,
|
||||
password TEXT,
|
||||
last_login INTEGER
|
||||
)
|
||||
""",
|
||||
)
|
||||
conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS config (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT
|
||||
)
|
||||
""",
|
||||
)
|
||||
conn.set_config("dbversion", self.CURRENT_DBVERSION)
|
||||
@@ -1,59 +0,0 @@
|
||||
|
||||
-- Escape shell argument by hex encoding it and wrapping in quotes.
|
||||
function escape(data)
|
||||
b16 = data:gsub(".", function(char) return string.format("%2X", char:byte()) end)
|
||||
return ("'"..b16.."'")
|
||||
end
|
||||
|
||||
-- call out to python program to actually manage authentication for dovecot
|
||||
|
||||
function chatctl_verify(user, password)
|
||||
local cmd = "doveauth hexauth "..escape(user).." "..escape(password)
|
||||
print("executing: "..cmd)
|
||||
local handle = io.popen(cmd)
|
||||
local result = handle:read("*a")
|
||||
handle:close()
|
||||
return split_chatctl(result)
|
||||
end
|
||||
|
||||
function chatctl_lookup(user)
|
||||
local cmd = "doveauth hexlookup "..escape(user)
|
||||
assert(user)
|
||||
print("executing: "..cmd)
|
||||
local handle = io.popen(cmd)
|
||||
local result = handle:read("*a")
|
||||
handle:close()
|
||||
return split_chatctl(result)
|
||||
end
|
||||
|
||||
function get_extra_dovecot_output(res)
|
||||
return {home=res.home, uid=res.uid, gid=res.gid}
|
||||
end
|
||||
|
||||
|
||||
function auth_password_verify(request, password)
|
||||
local res = chatctl_verify(request.user, password)
|
||||
-- request:log_error("auth_password_verify "..request.user.." "..password)
|
||||
if res.status == "ok" then
|
||||
local extra = get_extra_dovecot_output(res)
|
||||
return dovecot.auth.PASSDB_RESULT_OK, get_extra_dovecot_output(res)
|
||||
end
|
||||
return dovecot.auth.PASSDB_RESULT_PASSWORD_MISMATCH, ""
|
||||
end
|
||||
|
||||
|
||||
function auth_userdb_lookup(request)
|
||||
local res = chatctl_lookup(request.user)
|
||||
if res.status == "ok" then
|
||||
return dovecot.auth.USERDB_RESULT_OK, get_extra_dovecot_output(res)
|
||||
end
|
||||
return dovecot.auth.USERDB_RESULT_USER_UNKNOWN, "no such user"
|
||||
end
|
||||
|
||||
function split_chatctl(output)
|
||||
local ret = {}
|
||||
for key, value in output:gmatch "(%w+)%s*=%s*(%w+)" do
|
||||
ret[key] = value
|
||||
end
|
||||
return ret
|
||||
end
|
||||
@@ -1,65 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
import base64
|
||||
import sys
|
||||
|
||||
from .database import Database
|
||||
|
||||
|
||||
def get_user_data(db, user):
|
||||
with db.read_connection() as conn:
|
||||
result = conn.get_user(user)
|
||||
if result:
|
||||
result['uid'] = "vmail"
|
||||
result['gid'] = "vmail"
|
||||
return result
|
||||
|
||||
|
||||
def create_user(db, user, password):
|
||||
with db.write_transaction() as conn:
|
||||
conn.create_user(user, password)
|
||||
return dict(home=f"/home/vmail/{user}", uid="vmail", gid="vmail", password=password)
|
||||
|
||||
|
||||
def verify_user(db, user, password):
|
||||
userdata = get_user_data(db, user)
|
||||
if userdata:
|
||||
if userdata.get("password") == password:
|
||||
userdata["status"] = "ok"
|
||||
else:
|
||||
userdata["status"] = "fail"
|
||||
else:
|
||||
userdata = create_user(db, user, password)
|
||||
userdata["status"] = "ok"
|
||||
|
||||
return userdata
|
||||
|
||||
|
||||
def lookup_user(db, user):
|
||||
userdata = get_user_data(db, user)
|
||||
if userdata:
|
||||
userdata["status"] = "ok"
|
||||
else:
|
||||
userdata["status"] = "fail"
|
||||
return userdata
|
||||
|
||||
|
||||
def dump_result(res):
|
||||
for key, value in res.items():
|
||||
print(f"{key}={value}")
|
||||
|
||||
|
||||
def main():
|
||||
db = Database("/home/vmail/passdb.sqlite")
|
||||
if sys.argv[1] == "hexauth":
|
||||
login = base64.b16decode(sys.argv[2]).decode()
|
||||
password = base64.b16decode(sys.argv[3]).decode()
|
||||
res = verify_user(db, login, password)
|
||||
dump_result(res)
|
||||
elif sys.argv[1] == "hexlookup":
|
||||
login = base64.b16decode(sys.argv[2]).decode()
|
||||
res = lookup_user(db, login)
|
||||
dump_result(res)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,78 +0,0 @@
|
||||
|
||||
require "doveauth"
|
||||
|
||||
-- simulate dovecot defined result codes
|
||||
|
||||
dovecot = {
|
||||
auth = {
|
||||
PASSDB_RESULT_OK="PASSWORD-OK",
|
||||
PASSDB_RESULT_PASSWORD_MISMATCH="PASSWORD-MISMATCH",
|
||||
USERDB_RESULT_OK="USERDB-OK",
|
||||
USERDB_RESULT_USER_UNKNOWN="USERDB-UNKNOWN"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
-- Tests for testing the lua<->python interaction
|
||||
|
||||
function test_password_verify_ok(user, password)
|
||||
local res, extra = auth_password_verify({user=user}, password)
|
||||
assert(res==dovecot.auth.PASSDB_RESULT_OK)
|
||||
assert(extra.uid == "vmail")
|
||||
assert(extra.gid == "vmail")
|
||||
-- assert(extra.homedir == "/home/vmail/link2xt")
|
||||
print("OK test_password_verify_ok "..user.." "..password)
|
||||
end
|
||||
|
||||
function test_password_verify_mismatch(user, password)
|
||||
local res = auth_password_verify({user=user}, password)
|
||||
assert(res == dovecot.auth.PASSDB_RESULT_PASSWORD_MISMATCH)
|
||||
print("OK test_password_verify_mismatch "..user.." "..password)
|
||||
end
|
||||
|
||||
function test_userdb_lookup_ok(user)
|
||||
local res, extra = auth_userdb_lookup({user=user})
|
||||
assert(extra.uid == "vmail")
|
||||
assert(extra.gid == "vmail")
|
||||
assert(res == dovecot.auth.USERDB_RESULT_OK)
|
||||
print("OK test_userdb_lookup_ok "..user)
|
||||
end
|
||||
|
||||
function test_userdb_lookup_mismatch(user)
|
||||
local res, extra = auth_userdb_lookup({user=user})
|
||||
assert(res == dovecot.auth.USERDB_RESULT_USER_UNKNOWN)
|
||||
print("OK test_userdb_lookup_mismatch "..user)
|
||||
end
|
||||
|
||||
function test_passdb_lookup_ok(user)
|
||||
local res, extra = auth_passdb_lookup({user=user})
|
||||
assert(extra.uid == "vmail")
|
||||
assert(extra.gid == "vmail")
|
||||
assert(res == dovecot.auth.PASSDB_RESULT_OK)
|
||||
print("OK test_passdb_lookup_ok "..user)
|
||||
end
|
||||
|
||||
function test_passdb_lookup_mismatch(user)
|
||||
local res, extra = auth_passdb_lookup({user=user})
|
||||
assert(res == dovecot.auth.PASSDB_RESULT_USER_UNKNOWN)
|
||||
print("OK test_passdb_lookup_mismatch "..user)
|
||||
end
|
||||
|
||||
function test_split_chatctl()
|
||||
local res = split_chatctl("a=3 b=4\nc=5")
|
||||
assert(res["a"] == "3")
|
||||
assert(res["b"] == "4")
|
||||
assert(res["c"] == "5")
|
||||
print("OK test_split_chatctl")
|
||||
end
|
||||
|
||||
test_split_chatctl()
|
||||
test_password_verify_ok("link2xt@c1.testrun.org", "Ahyei6ie")
|
||||
test_password_verify_mismatch("link2xt@c1.testrun.org", "Aqwlek")
|
||||
test_userdb_lookup_ok("link2xt@c1.testrun.org")
|
||||
test_userdb_lookup_mismatch("wlekqjlew@xyz.org")
|
||||
|
||||
-- probably not needed by dovecot?
|
||||
-- test_passdb_lookup_ok("link2xt@c1.testrun.org")
|
||||
-- test_passdb_lookup_mismatch("llqkwjelqwe@xyz.org")
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
import subprocess
|
||||
import pytest
|
||||
|
||||
from doveauth.doveauth import get_user_data, verify_user, Database
|
||||
|
||||
|
||||
def test_basic(tmpdir):
|
||||
db = Database(tmpdir / "passdb.sqlite")
|
||||
verify_user(db, "link2xt@c1.testrun.org", "asdf")
|
||||
data = get_user_data(db, "link2xt@c1.testrun.org")
|
||||
assert data
|
||||
|
||||
|
||||
def test_verify_or_create(tmpdir):
|
||||
db = Database(tmpdir / "passdb.sqlite")
|
||||
res = verify_user(db, "newuser1@something.org", "kajdlkajsldk12l3kj1983")
|
||||
assert res["status"] == "ok"
|
||||
res = verify_user(db, "newuser1@something.org", "kajdlqweqwe")
|
||||
assert res["status"] == "fail"
|
||||
|
||||
|
||||
def test_lua_integration(request):
|
||||
p = request.fspath.dirpath("test_doveauth.lua")
|
||||
proc = subprocess.run(["lua", str(p)])
|
||||
assert proc.returncode == 0
|
||||
11
hpk-inv.py
Normal file
11
hpk-inv.py
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
chatmail = [
|
||||
(
|
||||
"c1.testrun.org",
|
||||
{
|
||||
"ssh_user": "root",
|
||||
"domain": "c1.testrun.org",
|
||||
"dkim_selector": "2023",
|
||||
},
|
||||
),
|
||||
]
|
||||
37
plan.txt
37
plan.txt
@@ -1,37 +0,0 @@
|
||||
# Chat-mail server development (up until Oct 18th)
|
||||
|
||||
## Dovecot goals/steps
|
||||
|
||||
1. create-user-on-login ("doveauth")
|
||||
|
||||
2. per-user quota (adaptive)
|
||||
|
||||
3. automatic expiry of messages older than M days
|
||||
|
||||
4. automatic expiry of users that haven't logged in for N days
|
||||
|
||||
|
||||
## Postfix goals/steps
|
||||
|
||||
1. block all outgoing mails with our own LMTP program
|
||||
|
||||
2. only allow (outgoing) mails if secure-join or autocrypt-pgp-encrypted format
|
||||
(probably via an lmtp service)
|
||||
|
||||
3. basic outgoing send rate/limits (depending on "account-rating")
|
||||
|
||||
|
||||
## online tests (first with plain python/pytest)
|
||||
|
||||
- write tests for dovecot login (exists)
|
||||
- write tests for postfix logins
|
||||
- write A<>B send/receive tests
|
||||
|
||||
|
||||
## Delta Chat
|
||||
|
||||
1. qr code that defines access to a chatmail instance (like mailadm but without http etc.)
|
||||
|
||||
2. support for creating username/password and verifying login works
|
||||
|
||||
|
||||
10
pyproject.toml
Normal file
10
pyproject.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[build-system]
|
||||
requires = ["setuptools>=45"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "chatmail"
|
||||
version = "0.1"
|
||||
dependencies = [
|
||||
"pyinfra",
|
||||
]
|
||||
@@ -1,7 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
: ${CHATMAIL_DOMAIN:=c1.testrun.org}
|
||||
export CHATMAIL_DOMAIN
|
||||
cd doveauth
|
||||
venv/bin/python3 -m build
|
||||
../chatmail-pyinfra/venv/bin/pyinfra --ssh-user root "$CHATMAIL_DOMAIN" ../deploy.py
|
||||
rm -r dist/
|
||||
@@ -1,8 +0,0 @@
|
||||
#!/bin/sh
|
||||
python3 -m venv chatmail-pyinfra/venv
|
||||
chatmail-pyinfra/venv/bin/pip install pyinfra pytest
|
||||
chatmail-pyinfra/venv/bin/pip install -e chatmail-pyinfra
|
||||
chatmail-pyinfra/venv/bin/pip install -e doveauth
|
||||
python3 -m venv doveauth/venv
|
||||
doveauth/venv/bin/pip install pytest build
|
||||
doveauth/venv/bin/pip install -e doveauth
|
||||
@@ -1,4 +0,0 @@
|
||||
#!/bin/sh
|
||||
chatmail-pyinfra/venv/bin/pytest chatmail-pyinfra/tests
|
||||
cd doveauth/src/doveauth
|
||||
../../venv/bin/pytest
|
||||
@@ -2,7 +2,7 @@
|
||||
Chat Mail pyinfra deploy.
|
||||
"""
|
||||
import importlib.resources
|
||||
from pathlib import Path
|
||||
from io import StringIO
|
||||
|
||||
from pyinfra import host, logger
|
||||
from pyinfra.operations import apt, files, server, systemd, python
|
||||
@@ -10,29 +10,17 @@ from pyinfra.facts.files import File
|
||||
from .acmetool import deploy_acmetool
|
||||
|
||||
|
||||
def _install_doveauth() -> None:
|
||||
def _install_chatctl() -> None:
|
||||
"""Setup chatctl."""
|
||||
doveauth_filename = "doveauth-0.1.tar.gz"
|
||||
doveauth_path = importlib.resources.files(__package__).joinpath(
|
||||
f"../../../doveauth/dist/{doveauth_filename}"
|
||||
files.put(
|
||||
src=importlib.resources.files(__package__)
|
||||
.joinpath("chatctl/chatctl.py")
|
||||
.open("rb"),
|
||||
dest="/home/vmail/chatctl",
|
||||
user="vmail",
|
||||
group="vmail",
|
||||
mode="755",
|
||||
)
|
||||
remote_path = f"/tmp/{doveauth_filename}"
|
||||
if Path(str(doveauth_path)).exists():
|
||||
files.put(
|
||||
name="upload local doveauth build",
|
||||
src=doveauth_path.open("rb"),
|
||||
dest=remote_path,
|
||||
)
|
||||
apt.packages(
|
||||
name="apt install python3-pip",
|
||||
packages="python3-pip",
|
||||
)
|
||||
# Maybe if we introduce dependencies to the doveauth package at some point, we should not install doveauth
|
||||
# system-wide anymore. For now it's fine though.
|
||||
server.shell(
|
||||
name="install local doveauth build with pip",
|
||||
commands=[f"pip install --break-system-packages {remote_path}"],
|
||||
)
|
||||
|
||||
|
||||
def _configure_opendkim(domain: str, dkim_selector: str) -> bool:
|
||||
@@ -116,8 +104,8 @@ def _configure_dovecot(mail_server: str) -> bool:
|
||||
|
||||
# luarocks install http lpeg_patterns fifo
|
||||
auth_script = files.put(
|
||||
src=importlib.resources.files("doveauth").joinpath("doveauth.lua"),
|
||||
dest="/etc/dovecot/doveauth.lua",
|
||||
src=importlib.resources.files(__package__).joinpath("dovecot/auth.lua"),
|
||||
dest="/etc/dovecot/auth.lua",
|
||||
user="root",
|
||||
group="root",
|
||||
mode="644",
|
||||
@@ -130,7 +118,7 @@ def _configure_dovecot(mail_server: str) -> bool:
|
||||
def deploy_chatmail(mail_domain: str, mail_server: str, dkim_selector: str) -> None:
|
||||
"""Deploy a chat-mail instance.
|
||||
|
||||
:param mail_domain: domain part of your future email addresses
|
||||
:param mail_domain: the domain part of your future email addresses, so "example.org" in user@example.org
|
||||
:param mail_server: the DNS name under which your mail server is reachable
|
||||
:param dkim_selector:
|
||||
"""
|
||||
@@ -172,7 +160,7 @@ def deploy_chatmail(mail_domain: str, mail_server: str, dkim_selector: str) -> N
|
||||
],
|
||||
)
|
||||
|
||||
_install_doveauth()
|
||||
_install_chatctl()
|
||||
dovecot_need_restart = _configure_dovecot(mail_server)
|
||||
postfix_need_restart = _configure_postfix(mail_domain)
|
||||
opendkim_need_restart = _configure_opendkim(mail_domain, dkim_selector)
|
||||
17
src/chatmail/chatctl/chatctl.py
Normal file
17
src/chatmail/chatctl/chatctl.py
Normal file
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python3
|
||||
import base64
|
||||
import sys
|
||||
|
||||
if sys.argv[1] == "hexauth":
|
||||
login = base64.b16decode(sys.argv[2])
|
||||
password = base64.b16decode(sys.argv[3])
|
||||
if login == b"link2xt@instant2.testrun.org" and password == b"Ahyei6ie":
|
||||
sys.exit(0)
|
||||
else:
|
||||
sys.exit(1)
|
||||
elif sys.argv[1] == "hexlookup":
|
||||
login = base64.b16decode(sys.argv[2])
|
||||
if login == b"link2xt@instant2.testrun.org":
|
||||
sys.exit(0)
|
||||
else:
|
||||
sys.exit(1)
|
||||
73
src/chatmail/chatctl/test.lua
Normal file
73
src/chatmail/chatctl/test.lua
Normal file
@@ -0,0 +1,73 @@
|
||||
|
||||
-- To run this test: run "lua test.lua" while in the same directory as chatctl.py
|
||||
|
||||
if dovecot == nil then
|
||||
dovecot = {
|
||||
auth = {
|
||||
PASSDB_RESULT_OK="OK",
|
||||
PASSDB_RESULT_PASSWORD_MISMATCH="MISMATCH"
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
-- Escape shell argument by hex encoding it and wrapping in quotes.
|
||||
function escape(data)
|
||||
b16 = data:gsub(".", function(char) return string.format("%2X", char:byte()) end)
|
||||
return ("'"..b16.."'")
|
||||
end
|
||||
|
||||
-- call out to python program to actually manage authentication for dovecot
|
||||
|
||||
function chatctl_verify(user, password)
|
||||
return os.execute("python chatctl.py hexauth "..escape(user).." "..escape(password))
|
||||
end
|
||||
|
||||
function chatctl_lookup(hex, user)
|
||||
return os.execute("python chatctl.py hexlookup "..escape(user))
|
||||
end
|
||||
|
||||
|
||||
function auth_password_verify(request, password)
|
||||
if chatctl_verify(request.user, password) then
|
||||
return dovecot.auth.PASSDB_RESULT_OK, {}
|
||||
end
|
||||
return dovecot.auth.PASSDB_RESULT_PASSWORD_MISMATCH, ""
|
||||
end
|
||||
|
||||
function auth_passdb_lookup(request)
|
||||
if chatctl_lookup(request.user) then
|
||||
return dovecot.auth.PASSDB_RESULT_OK, {}
|
||||
end
|
||||
return dovecot.auth.PASSDB_RESULT_USER_UNKNOWN, "no such user"
|
||||
end
|
||||
|
||||
function auth_userdb_lookup(request)
|
||||
if chatctl_lookup(request.user) then
|
||||
return dovecot.auth.USERDB_RESULT_OK, "uid=vmail gid=vmail"
|
||||
end
|
||||
return dovecot.auth.USERDB_RESULT_USER_UNKNOWN, "no such user"
|
||||
end
|
||||
|
||||
function split_chatctl_results(output)
|
||||
local ret = {}
|
||||
for key, value in output:gmatch "(%w+)%s*=%s*(%w+)" do
|
||||
ret[key] = value
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
function test_ok(user, password)
|
||||
local res = auth_password_verify({user=user}, password)
|
||||
assert(res=="OK")
|
||||
print("OK test_ok "..user.." "..password)
|
||||
end
|
||||
|
||||
function test_mismatch(user, password)
|
||||
local res = auth_password_verify({user=user}, password)
|
||||
assert(res == "MISMATCH")
|
||||
print("OK test_mismatch "..user.." "..password)
|
||||
end
|
||||
|
||||
test_ok("link2xt@instant2.testrun.org", "Ahyei6ie")
|
||||
test_mismatch("link2xt@instant2.testrun.org", "Aqwlek")
|
||||
|
||||
35
src/chatmail/dovecot/auth.lua
Normal file
35
src/chatmail/dovecot/auth.lua
Normal file
@@ -0,0 +1,35 @@
|
||||
-- Lua based authentication script for Dovecot.
|
||||
--
|
||||
-- It calls external chatctl command to answer requests.
|
||||
|
||||
-- Hexadecimal aka base16 encoding.
|
||||
function hex(data)
|
||||
return (data:gsub(".", function(char) return string.format("%2X", char:byte()) end))
|
||||
end
|
||||
|
||||
-- Escape shell argument by hex encoding it and wrapping in quotes.
|
||||
function escape(data)
|
||||
return ("'"..hex(data).."'")
|
||||
end
|
||||
|
||||
function auth_password_verify(request, password)
|
||||
if os.execute("/home/vmail/chatctl hexauth "..escape(request.user).." "..escape(password)) then
|
||||
return dovecot.auth.PASSDB_RESULT_OK, {}
|
||||
end
|
||||
return dovecot.auth.PASSDB_RESULT_PASSWORD_MISMATCH, ""
|
||||
end
|
||||
|
||||
function auth_passdb_lookup(request)
|
||||
if os.execute("/home/vmail/chatctl hexlookup "..escape(request.user)) then
|
||||
return dovecot.auth.PASSDB_RESULT_OK, {}
|
||||
end
|
||||
return dovecot.auth.PASSDB_RESULT_USER_UNKNOWN, "no such user"
|
||||
end
|
||||
|
||||
function auth_userdb_lookup(request)
|
||||
if os.execute("/home/vmail/chatctl hexlookup "..escape(request.user)) then
|
||||
return dovecot.auth.USERDB_RESULT_OK, "uid=vmail gid=vmail"
|
||||
end
|
||||
|
||||
return dovecot.auth.USERDB_RESULT_USER_UNKNOWN, "no such user"
|
||||
end
|
||||
@@ -4,19 +4,14 @@ protocols = imap lmtp
|
||||
|
||||
auth_mechanisms = plain
|
||||
|
||||
auth_verbose = yes
|
||||
auth_debug = yes
|
||||
auth_debug_passwords = yes
|
||||
auth_verbose_passwords = plain
|
||||
|
||||
# Authentication for system users.
|
||||
passdb {
|
||||
driver = lua
|
||||
args = file=/etc/dovecot/doveauth.lua
|
||||
args = file=/etc/dovecot/auth.lua
|
||||
}
|
||||
userdb {
|
||||
driver = lua
|
||||
args = file=/etc/dovecot/doveauth.lua
|
||||
args = file=/etc/dovecot/auth.lua
|
||||
}
|
||||
|
||||
##
|
||||
@@ -88,7 +83,7 @@ service auth {
|
||||
service auth-worker {
|
||||
# Default is root.
|
||||
# Drop privileges we don't need.
|
||||
user = vmail
|
||||
user = $default_internal_user
|
||||
}
|
||||
|
||||
ssl = required
|
||||
Reference in New Issue
Block a user