Benedek Nagy 00a4ff3b41
email: Make rspamd learn spam/ham when the user marks mails as junk or not junk.
Add two sieve scripts for spam/ham learning. When the user moves a mail
from anywhere to junk, or from junk to anywhere (except for trash) the
mail is piped into the respective rspamc learn_spam/learn_ham command.
The rspamc command is run as the mail user and the command requires that
the user can connect to localhost:11334. Because of that, add the mail
user to the allowed users that can access protected services.

The sievec compilation of the new scripts requre the dovecot-antispam
package, so install it and increment the email version number.

Closes: #2487
Imroves: #56

Tests done:
1. Apply the patches on an existing install
2. Confirm the firewall and the email app get updated
3. Move a mail from inbox to junk and confirm that rspamd statistics for
   "Learned" mails increment by one.
4. Move back the mail from junk to inbox and confirm the number
   increments again.
5. Move the mail to trash and confirm the script doesn't execute.
6. Repeat steps 3-5 with mail_debug = yes in /etc/dovecot/dovecot.conf
   and confirm the script esxecution further by reading the debug logs.

[Sunil]

- Split the configuration file 90-freedombox-sieve.conf into
90-freedombox-imap.conf and merge the remaining with 95-freedombox-sieve.conf.

- These changes do not need dovecot-anitspam package. Remove it from packages
list for the app.

Signed-off-by: Benedek Nagy <contact@nbenedek.me>
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
2025-01-23 15:36:32 -08:00

65 lines
2.0 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Configures rspamd to handle incoming and outgoing spam.
See: http://www.postfix.org/MILTER_README.html
See: https://rspamd.com/doc/configuration/ucl.html
For testing DKIM signatures: https://www.mail-tester.com/
"""
import pathlib
import re
import subprocess
from plinth.actions import privileged
from plinth.modules.email import postfix
_milter_config = {
'smtpd_milters': 'inet:127.0.0.1:11332',
'non_smtpd_milters': 'inet:127.0.0.1:11332',
}
@privileged
def setup_spam():
"""Compile sieve filters and set rspamd/postfix configuration."""
_compile_sieve()
_setup_rspamd()
postfix.set_config(_milter_config)
def _compile_sieve():
"""Compile all .sieve script to binary format for performance."""
sieve_dirs = ['/etc/dovecot/freedombox-sieve-after/',
'/etc/dovecot/freedombox-sieve']
for sieve_dir in sieve_dirs:
subprocess.run(['sievec', sieve_dir], check=True)
def _setup_rspamd():
"""Adjust configuration to include FreedomBox configuration files."""
configs = [('milter_headers.conf', 'freedombox-milter-headers.conf'),
('redis.conf', 'freedombox-redis.conf'),
('logging.inc', 'freedombox-logging.inc'),
('dkim_signing.conf', 'freedombox-dkim-signing.conf')]
base_path = pathlib.Path('/etc/rspamd/local.d')
for orig_path, include_path in configs:
_setup_local_include(base_path / orig_path, base_path / include_path)
def _setup_local_include(orig_path, include_path):
"""Adjust configuration to include a FreedomBox configuration file."""
lines = []
if orig_path.exists():
lines = orig_path.read_text().splitlines()
file_name = include_path.name
for line in lines:
if re.match(rf'\s*.include\(.*\)\s+".*/{file_name}"', line):
return
lines.append('.include(priority=2,duplicate=merge) '
f'"$LOCAL_CONFDIR/local.d/{file_name}"\n')
orig_path.write_text('\n'.join(lines))