Add the email need: each app gets its own sending-only mail account

needs = ["email"] (coreapp new --email) makes an LDAP account
coreapp-<name> (not a person, no mailbox) and adds SMTP_* settings.
Teardown removes it only if it's the app's; the installer warns about
an entry it left alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 15:01:11 -07:00
co-authored by Claude Opus 5.5
parent bef3b86970
commit 6ab1db839b
12 changed files with 173 additions and 13 deletions
+16 -1
View File
@@ -67,7 +67,7 @@ FIRST, LAST = 2, 254
RESERVED = {"www", "mail", "ns1", "ns2", "autodiscover", "autoconfig",
"panel", "dashboard", "mcphub", "api", "admin"}
# What an app can ask for, in the order they're set up
NEEDS = ["postgres", "sso"]
NEEDS = ["postgres", "sso", "email"]
# What coreapp.toml may contain. Values end up in shell-sourced and YAML
# files on the Core, so each is limited to characters that are safe there.
@@ -768,6 +768,7 @@ def install_app(m, p, folder, origin, addon_ok, recreate):
compose_up(m, log, recreate)
wait_until_answering(m, p)
check_needs(m)
if p["state"] == "update":
remove_old_images(m)
say(f"{'Installed' if p['state'] == 'new' else 'Updated'}: {p['url']}")
@@ -797,6 +798,20 @@ def remove_images(image, keep=None):
run(["docker", "image", "rm", f"{short_name(image)}:{tag}"])
def check_needs(m):
"""Say so if a need couldn't be set up (its setup only writes to the
Core's install log)."""
if "email" in m["needs"]:
out = run(["bash", "-c", (
'B=$(sed -n "s/^LDAP_SEARCH_BASE=ou=people,//p" /federated/apps/mail/.env); '
'docker exec ldap ldapsearch -LLL -x -H ldap://localhost -D "cn=admin,$B" '
'-w "$(cat /federated/apps/ldap/.ldap.secret)" '
f'-b "uid=coreapp-{m["name"]},ou=people,$B" -s base description')])
if f"description: coreapps: {m['name']}" not in out.stdout:
say(f"Warning: {m['name']} can't send mail: LDAP has another entry with uid "
f"coreapp-{m['name']} (or its address), which was left alone.")
def remove_old_images(m):
"""The app's images with other versions, now that this one runs."""
remove_images(m["image"], keep=m["version"])
+1
View File
@@ -0,0 +1 @@
mail
+8
View File
@@ -0,0 +1,8 @@
# Sending mail through the Core's mail server with its own account (made
# by needs/email/setup): STARTTLS on port 587, user and password below
SMTP_HOST=mail.@DOMAIN@
SMTP_PORT=587
SMTP_SECURITY=starttls
SMTP_USER=coreapp-{{NAME}}
SMTP_PASSWORD=@SMTP_PASSWORD:RANDOM:LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL@
SMTP_FROM={{NAME}}@@DOMAIN@
+39
View File
@@ -0,0 +1,39 @@
# Its own mail account in the Core's LDAP, for sending only, with the
# password from .env. Not a person (objectClass account, not
# inetOrgPerson), so it can't sign in to Authelia, Nextcloud or Superpanel;
# the mail server checks its password with LDAP. mailEnabled FALSE: no
# mailbox. Its address, coreapp-{{NAME}}@<domain>, is its own, so a
# person's mail login never finds two entries. Safe to run again.
LDAP_BASE="$(sed -n 's/^LDAP_SEARCH_BASE=ou=people,//p' /federated/apps/mail/.env)"
LDAP_ADMIN_PW="$(cat /federated/apps/ldap/.ldap.secret)"
MAIL_DN="uid=coreapp-{{NAME}},ou=people,$LDAP_BASE"
SMTP_PASSWORD="$(sed -n 's/^SMTP_PASSWORD=//p' /federated/apps/{{NAME}}/.env)"
# Entries with its uid or address: none, or only its own
MAIL_FOUND="$(docker exec ldap ldapsearch -LLL -o ldif-wrap=no -x -H ldap://localhost \
-D "cn=admin,$LDAP_BASE" -w "$LDAP_ADMIN_PW" -b "$LDAP_BASE" \
"(|(uid=coreapp-{{NAME}})(mail=coreapp-{{NAME}}@$DOMAIN))" dn description)"
if [ -z "$MAIL_FOUND" ]; then
docker exec -i ldap ldapadd -x -H ldap://localhost -D "cn=admin,$LDAP_BASE" -w "$LDAP_ADMIN_PW" \
> /dev/null <<LDIF
dn: $MAIL_DN
objectClass: account
objectClass: simpleSecurityObject
objectClass: PostfixBookMailAccount
uid: coreapp-{{NAME}}
description: coreapps: {{NAME}}
mail: coreapp-{{NAME}}@$DOMAIN
mailEnabled: FALSE
userPassword: {CRYPT}!
LDIF
MAIL_FOUND="dn: $MAIL_DN
description: coreapps: {{NAME}}"
fi
if [ "$MAIL_FOUND" = "dn: $MAIL_DN
description: coreapps: {{NAME}}" ]; then
# The password, on stdin; the server hashes it
printf '%s' "$SMTP_PASSWORD" | docker exec -i ldap ldappasswd -x -H ldap://localhost \
-D "cn=admin,$LDAP_BASE" -w "$LDAP_ADMIN_PW" -T /dev/stdin "$MAIL_DN"
else
echo "LDAP already has an entry with uid coreapp-{{NAME}} or its address that isn't this app's mail account; left it alone, so the app can't send mail." >&2
fi
unset LDAP_BASE LDAP_ADMIN_PW MAIL_DN SMTP_PASSWORD MAIL_FOUND
+11
View File
@@ -0,0 +1,11 @@
# Delete its mail account from the Core's LDAP (only if it's this app's)
LDAP_BASE="$(sed -n 's/^LDAP_SEARCH_BASE=ou=people,//p' /federated/apps/mail/.env)"
LDAP_ADMIN_PW="$(cat /federated/apps/ldap/.ldap.secret)"
MAIL_DN="uid=coreapp-{{NAME}},ou=people,$LDAP_BASE"
if docker exec ldap ldapsearch -LLL -x -H ldap://localhost -D "cn=admin,$LDAP_BASE" \
-w "$LDAP_ADMIN_PW" -b "$MAIL_DN" -s base description 2>/dev/null \
| grep -qx "description: coreapps: {{NAME}}"; then
docker exec ldap ldapdelete -x -H ldap://localhost -D "cn=admin,$LDAP_BASE" \
-w "$LDAP_ADMIN_PW" "$MAIL_DN"
fi
unset LDAP_BASE LDAP_ADMIN_PW MAIL_DN