generate compliant IP-address email addresses

This commit is contained in:
holger krekel
2026-04-17 09:52:04 +02:00
parent 0a77b3339b
commit 14dfabf2ff
5 changed files with 47 additions and 7 deletions

View File

@@ -53,7 +53,7 @@ jobs:
lxc-test: lxc-test:
name: LXC deploy and test name: LXC deploy and test
uses: chatmail/cmlxc/.github/workflows/lxc-test.yml@main uses: chatmail/cmlxc/.github/workflows/lxc-test.yml@v0.10.0
with: with:
cmlxc_commands: | cmlxc_commands: |
cmlxc init cmlxc init

View File

@@ -2,6 +2,7 @@
"""CGI script for creating new accounts.""" """CGI script for creating new accounts."""
import ipaddress
import json import json
import secrets import secrets
import string import string
@@ -14,6 +15,16 @@ ALPHANUMERIC = string.ascii_lowercase + string.digits
ALPHANUMERIC_PUNCT = string.ascii_letters + string.digits + string.punctuation ALPHANUMERIC_PUNCT = string.ascii_letters + string.digits + string.punctuation
def wrap_ip(host):
if host.startswith("[") and host.endswith("]"):
return host
try:
ipaddress.ip_address(host)
return f"[{host}]"
except ValueError:
return host
def create_newemail_dict(config: Config): def create_newemail_dict(config: Config):
user = "".join( user = "".join(
secrets.choice(ALPHANUMERIC) for _ in range(config.username_max_length) secrets.choice(ALPHANUMERIC) for _ in range(config.username_max_length)
@@ -22,7 +33,7 @@ def create_newemail_dict(config: Config):
secrets.choice(ALPHANUMERIC_PUNCT) secrets.choice(ALPHANUMERIC_PUNCT)
for _ in range(config.password_min_length + 3) for _ in range(config.password_min_length + 3)
) )
return dict(email=f"{user}@{config.mail_domain}", password=f"{password}") return dict(email=f"{user}@{wrap_ip(config.mail_domain)}", password=f"{password}")
def create_dclogin_url(email, password): def create_dclogin_url(email, password):

View File

@@ -19,6 +19,12 @@ def test_create_newemail_dict(example_config):
assert ac1["password"] != ac2["password"] assert ac1["password"] != ac2["password"]
def test_create_newemail_dict_ip(make_config):
config = make_config("1.2.3.4")
ac = create_newemail_dict(config)
assert ac["email"].endswith("@[1.2.3.4]")
def test_create_dclogin_url(): def test_create_dclogin_url():
url = create_dclogin_url("user@example.org", "p@ss w+rd") url = create_dclogin_url("user@example.org", "p@ss w+rd")
assert url.startswith("dclogin:") assert url.startswith("dclogin:")

View File

@@ -1,4 +1,5 @@
import imaplib import imaplib
import ipaddress
import itertools import itertools
import os import os
import random import random
@@ -14,6 +15,14 @@ from chatmaild.config import read_config
conftestdir = Path(__file__).parent conftestdir = Path(__file__).parent
def _is_ip(domain):
try:
ipaddress.ip_address(domain)
return True
except ValueError:
return False
def pytest_addoption(parser): def pytest_addoption(parser):
parser.addoption( parser.addoption(
"--slow", action="store_true", default=False, help="also run slow tests" "--slow", action="store_true", default=False, help="also run slow tests"
@@ -282,6 +291,7 @@ def gencreds(chatmail_config):
def gen(domain=None): def gen(domain=None):
domain = domain if domain else chatmail_config.mail_domain domain = domain if domain else chatmail_config.mail_domain
addr_domain = f"[{domain}]" if _is_ip(domain) else domain
while 1: while 1:
num = next(count) num = next(count)
alphanumeric = "abcdefghijklmnopqrstuvwxyz1234567890" alphanumeric = "abcdefghijklmnopqrstuvwxyz1234567890"
@@ -295,7 +305,7 @@ def gencreds(chatmail_config):
password = "".join( password = "".join(
random.choices(alphanumeric, k=chatmail_config.password_min_length) random.choices(alphanumeric, k=chatmail_config.password_min_length)
) )
yield f"{user}@{domain}", f"{password}" yield f"{user}@{addr_domain}", f"{password}"
return lambda domain=None: next(gen(domain)) return lambda domain=None: next(gen(domain))
@@ -344,9 +354,22 @@ class ChatmailACFactory:
accounts = [] accounts = []
for _ in range(num): for _ in range(num):
account = self.dc.add_account() account = self.dc.add_account()
future = account.add_or_update_transport.future( addr, password = self.gencreds(domain)
self._make_transport(domain) if _is_ip(domain):
) # Use DCLOGIN scheme with explicit server hosts,
# matching how madmail presents its addresses to users.
qr = (
f"dclogin:{addr}"
f"?p={password}&v=1"
f"&ih={domain}&ip=993"
f"&sh={domain}&sp=465"
f"&ic=3&ss=default"
)
future = account.add_transport_from_qr.future(qr)
else:
future = account.add_or_update_transport.future(
self._make_transport(domain)
)
futures.append(future) futures.append(future)
# ensure messages stay in INBOX so that they can be # ensure messages stay in INBOX so that they can be

View File

@@ -2,9 +2,9 @@ from contextlib import nullcontext
from types import SimpleNamespace from types import SimpleNamespace
import pytest import pytest
from pyinfra.facts.deb import DebPackages
from cmdeploy.dovecot import deployer as dovecot_deployer from cmdeploy.dovecot import deployer as dovecot_deployer
from pyinfra.facts.deb import DebPackages
def make_host(*fact_pairs): def make_host(*fact_pairs):