2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2012-08-03 15:11:28 +02:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2012-08-03 15:11:28 +02:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.spamassassin;
|
2017-06-10 09:35:35 +02:00
|
|
|
spamassassin-local-cf = pkgs.writeText "local.cf" cfg.config;
|
|
|
|
|
2012-08-03 15:11:28 +02:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.spamassassin = {
|
2020-04-20 20:05:26 +02:00
|
|
|
enable = mkEnableOption "the SpamAssassin daemon";
|
2012-08-03 15:11:28 +02:00
|
|
|
|
2012-09-27 17:12:25 +02:00
|
|
|
debug = mkOption {
|
2020-04-27 09:04:07 +02:00
|
|
|
type = types.bool;
|
2012-09-27 17:12:25 +02:00
|
|
|
default = false;
|
2017-06-10 09:35:35 +02:00
|
|
|
description = "Whether to run the SpamAssassin daemon in debug mode";
|
2012-09-27 17:12:25 +02:00
|
|
|
};
|
|
|
|
|
2017-06-10 09:35:35 +02:00
|
|
|
config = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
description = ''
|
|
|
|
The SpamAssassin local.cf config
|
|
|
|
|
|
|
|
If you are using this configuration:
|
|
|
|
add_header all Status _YESNO_, score=_SCORE_ required=_REQD_ tests=_TESTS_ autolearn=_AUTOLEARN_ version=_VERSION_
|
|
|
|
|
|
|
|
Then you can Use this sieve filter:
|
|
|
|
require ["fileinto", "reject", "envelope"];
|
2017-09-27 14:37:56 +02:00
|
|
|
|
2017-06-10 09:35:35 +02:00
|
|
|
if header :contains "X-Spam-Flag" "YES" {
|
|
|
|
fileinto "spam";
|
|
|
|
}
|
|
|
|
|
|
|
|
Or this procmail filter:
|
|
|
|
:0:
|
|
|
|
* ^X-Spam-Flag: YES
|
|
|
|
/var/vpopmail/domains/lastlog.de/js/.maildir/.spam/new
|
|
|
|
|
|
|
|
To filter your messages based on the additional mail headers added by spamassassin.
|
|
|
|
'';
|
|
|
|
example = ''
|
|
|
|
#rewrite_header Subject [***** SPAM _SCORE_ *****]
|
|
|
|
required_score 5.0
|
|
|
|
use_bayes 1
|
|
|
|
bayes_auto_learn 1
|
|
|
|
add_header all Status _YESNO_, score=_SCORE_ required=_REQD_ tests=_TESTS_ autolearn=_AUTOLEARN_ version=_VERSION_
|
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
};
|
2012-08-03 15:11:28 +02:00
|
|
|
|
2017-06-10 09:35:35 +02:00
|
|
|
initPreConf = mkOption {
|
2019-12-24 23:19:05 +01:00
|
|
|
type = with types; either str path;
|
2017-06-10 09:35:35 +02:00
|
|
|
description = "The SpamAssassin init.pre config.";
|
2019-12-24 23:19:05 +01:00
|
|
|
apply = val: if builtins.isPath val then val else pkgs.writeText "init.pre" val;
|
2017-09-27 14:37:56 +02:00
|
|
|
default =
|
|
|
|
''
|
2017-06-10 09:35:35 +02:00
|
|
|
#
|
|
|
|
# to update this list, run this command in the rules directory:
|
2017-09-27 14:37:56 +02:00
|
|
|
# grep 'loadplugin.*Mail::SpamAssassin::Plugin::.*' -o -h * | sort | uniq
|
2017-06-10 09:35:35 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
#loadplugin Mail::SpamAssassin::Plugin::AccessDB
|
|
|
|
#loadplugin Mail::SpamAssassin::Plugin::AntiVirus
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::AskDNS
|
|
|
|
# loadplugin Mail::SpamAssassin::Plugin::ASN
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::AutoLearnThreshold
|
|
|
|
#loadplugin Mail::SpamAssassin::Plugin::AWL
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::Bayes
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::BodyEval
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::Check
|
|
|
|
#loadplugin Mail::SpamAssassin::Plugin::DCC
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::DKIM
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::DNSEval
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::FreeMail
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::Hashcash
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::HeaderEval
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::HTMLEval
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::HTTPSMismatch
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::ImageInfo
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::MIMEEval
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::MIMEHeader
|
|
|
|
# loadplugin Mail::SpamAssassin::Plugin::PDFInfo
|
|
|
|
#loadplugin Mail::SpamAssassin::Plugin::PhishTag
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::Pyzor
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::Razor2
|
|
|
|
# loadplugin Mail::SpamAssassin::Plugin::RelayCountry
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::RelayEval
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::ReplaceTags
|
|
|
|
# loadplugin Mail::SpamAssassin::Plugin::Rule2XSBody
|
|
|
|
# loadplugin Mail::SpamAssassin::Plugin::Shortcircuit
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::SpamCop
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::SPF
|
|
|
|
#loadplugin Mail::SpamAssassin::Plugin::TextCat
|
|
|
|
# loadplugin Mail::SpamAssassin::Plugin::TxRep
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::URIDetail
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::URIDNSBL
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::URIEval
|
|
|
|
# loadplugin Mail::SpamAssassin::Plugin::URILocalBL
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::VBounce
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::WhiteListSubject
|
|
|
|
loadplugin Mail::SpamAssassin::Plugin::WLBLEval
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2012-08-03 15:11:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2019-12-24 23:15:05 +01:00
|
|
|
environment.etc."mail/spamassassin/init.pre".source = cfg.initPreConf;
|
|
|
|
environment.etc."mail/spamassassin/local.cf".source = spamassassin-local-cf;
|
2012-08-03 15:11:28 +02:00
|
|
|
|
2012-08-03 18:07:06 +02:00
|
|
|
# Allow users to run 'spamc'.
|
2019-12-24 23:15:05 +01:00
|
|
|
environment.systemPackages = [ pkgs.spamassassin ];
|
2012-08-03 15:11:28 +02:00
|
|
|
|
2019-09-14 19:51:29 +02:00
|
|
|
users.users.spamd = {
|
2012-09-28 00:06:52 +02:00
|
|
|
description = "Spam Assassin Daemon";
|
|
|
|
uid = config.ids.uids.spamd;
|
|
|
|
group = "spamd";
|
|
|
|
};
|
|
|
|
|
2019-09-14 19:51:29 +02:00
|
|
|
users.groups.spamd = {
|
2012-09-28 00:06:52 +02:00
|
|
|
gid = config.ids.gids.spamd;
|
|
|
|
};
|
2012-08-28 16:27:28 +02:00
|
|
|
|
2017-06-10 09:35:35 +02:00
|
|
|
systemd.services.sa-update = {
|
2021-01-01 19:56:52 +01:00
|
|
|
# Needs to be able to contact the update server.
|
2020-11-15 11:02:28 +01:00
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
2021-01-01 19:56:52 +01:00
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = "spamd";
|
|
|
|
Group = "spamd";
|
|
|
|
StateDirectory = "spamassassin";
|
|
|
|
ExecStartPost = "+${pkgs.systemd}/bin/systemctl -q --no-block try-reload-or-restart spamd.service";
|
|
|
|
};
|
2021-01-05 14:53:14 +01:00
|
|
|
|
|
|
|
script = ''
|
|
|
|
set +e
|
2021-01-10 12:15:38 +01:00
|
|
|
${pkgs.spamassassin}/bin/sa-update --verbose --gpghomedir=/var/lib/spamassassin/sa-update-keys/
|
2021-01-05 14:53:14 +01:00
|
|
|
rc=$?
|
|
|
|
set -e
|
|
|
|
|
2021-01-10 12:15:38 +01:00
|
|
|
if [[ $rc -gt 1 ]]; then
|
|
|
|
# sa-update failed.
|
|
|
|
exit $rc
|
2021-01-05 14:53:14 +01:00
|
|
|
fi
|
2021-01-10 12:15:38 +01:00
|
|
|
|
|
|
|
if [[ $rc -eq 1 ]]; then
|
|
|
|
# No update was available, exit successfully.
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# An update was available and installed. Compile the rules.
|
|
|
|
${pkgs.spamassassin}/bin/sa-compile
|
2021-01-05 14:53:14 +01:00
|
|
|
'';
|
2017-06-10 09:35:35 +02:00
|
|
|
};
|
|
|
|
|
2017-09-27 14:37:56 +02:00
|
|
|
systemd.timers.sa-update = {
|
2017-06-10 09:35:35 +02:00
|
|
|
description = "sa-update-service";
|
|
|
|
partOf = [ "sa-update.service" ];
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
|
|
|
OnCalendar = "1:*";
|
|
|
|
Persistent = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-01-06 07:50:18 +01:00
|
|
|
systemd.services.spamd = {
|
2020-11-15 11:02:28 +01:00
|
|
|
description = "SpamAssassin Server";
|
2013-01-24 18:34:13 +01:00
|
|
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-01-01 19:56:52 +01:00
|
|
|
wants = [ "sa-update.service" ];
|
2020-11-15 11:02:28 +01:00
|
|
|
after = [
|
|
|
|
"network.target"
|
2021-01-01 19:56:52 +01:00
|
|
|
"sa-update.service"
|
2020-11-15 11:02:28 +01:00
|
|
|
];
|
2013-01-24 18:34:13 +01:00
|
|
|
|
2017-06-10 09:35:35 +02:00
|
|
|
serviceConfig = {
|
2021-01-01 19:56:52 +01:00
|
|
|
User = "spamd";
|
|
|
|
Group = "spamd";
|
|
|
|
ExecStart = "+${pkgs.spamassassin}/bin/spamd ${optionalString cfg.debug "-D"} --username=spamd --groupname=spamd --virtual-config-dir=%S/spamassassin/user-%u --allow-tell --pidfile=/run/spamd.pid";
|
|
|
|
ExecReload = "+${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
|
|
|
StateDirectory = "spamassassin";
|
2017-06-10 09:35:35 +02:00
|
|
|
};
|
2012-08-03 15:11:28 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|