Skip to content

Commit

Permalink
Authenticate echobot by passing /run/echobot/password to doveauth
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed May 5, 2024
1 parent 0493e27 commit e1b1a94
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
10 changes: 9 additions & 1 deletion chatmaild/src/chatmaild/doveauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import json
import crypt
from pathlib import Path
from socketserver import (
UnixStreamServer,
StreamRequestHandler,
Expand Down Expand Up @@ -86,11 +87,18 @@ def lookup_userdb(db, config: Config, user):

def lookup_passdb(db, config: Config, user, cleartext_password):
if user == f"echo@{config.mail_domain}":
# Echobot writes password it wants to log in with into /run/echobot/password
try:
password = Path("/run/echobot/password").read_text()
except Exception:
logging.exception("Exception when trying to read /run/echobot/password")
return None

return dict(
home=f"/home/vmail/mail/{config.mail_domain}/echo@{config.mail_domain}",
uid="vmail",
gid="vmail",
password=encrypt_password("eiPhiez0eo8raighoh0C"), # FIXME read from config
password=encrypt_password(password),
)

with db.write_transaction() as conn:
Expand Down
21 changes: 19 additions & 2 deletions chatmaild/src/chatmaild/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import logging
import os
import sys
import subprocess

from deltachat_rpc_client import Bot, DeltaChat, EventType, Rpc, events
from pathlib import Path

from chatmaild.config import read_config
from chatmaild.newemail import create_newemail_dict

hooks = events.HookCollection()

Expand Down Expand Up @@ -75,9 +78,23 @@ def main():
account = accounts[0] if accounts else deltachat.add_account()

bot = Bot(account, hooks)

config = read_config(sys.argv[1])

# Create password file
if bot.is_configured():
password = bot.account.get_config("mail_pw")
else:
password = create_newemail_dict(config)["password"]
Path("/run/echobot/password").write_text(password)

# Give the user which doveauth runs as access to the password file.
subprocess.run(
["/usr/bin/setfacl", "-m", "user:vmail:r", "/run/echobot/password"],
check=True,
)

if not bot.is_configured():
config = read_config(sys.argv[1])
password = "eiPhiez0eo8raighoh0C" # FIXME read from config
email = "echo@" + config.mail_domain
bot.configure(email, password)
bot.run_forever()
Expand Down
11 changes: 10 additions & 1 deletion cmdeploy/src/cmdeploy/service/echobot.service.f
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
# Create /var/lib/echobot
StateDirectory=echobot

# Create /run/echobot
#
# echobot stores /run/echobot/password
# with a password there, which doveauth then reads.
RuntimeDirectory=echobot

WorkingDirectory=/var/lib/echobot

# Apply security restrictions suggested by
Expand All @@ -24,7 +30,10 @@
PrivateDevices=true
PrivateMounts=true
PrivateTmp=true
PrivateUsers=true

# We need to know about doveauth user to give it access to /run/echobot/password
PrivateUsers=false

ProtectClock=true
ProtectControlGroups=true
ProtectHostname=true
Expand Down

0 comments on commit e1b1a94

Please sign in to comment.