fruitbasket/modules/mail/default.nix

344 lines
10 KiB
Nix
Raw Normal View History

2023-09-17 20:14:32 +02:00
{ config, pkgs, ... }:
2022-12-17 21:33:45 +01:00
let
hostname = "mail.${config.networking.domain}";
domain = config.networking.domain;
rspamd-domain = "rspamd.${config.networking.domain}";
2023-01-27 16:39:25 +01:00
dovecot-ldap-args = pkgs.writeText "ldap-args" ''
2023-02-03 15:37:56 +01:00
uris = ldap://localhost
dn = uid=search, ou=users, dc=ifsr, dc=de
2023-01-27 16:39:25 +01:00
auth_bind = yes
2023-02-15 14:47:14 +01:00
!include ${config.sops.secrets."dovecot_ldap_search".path}
2023-02-03 15:37:56 +01:00
2023-01-27 16:39:25 +01:00
ldap_version = 3
scope = subtree
2023-02-03 15:37:56 +01:00
base = dc=ifsr, dc=de
2023-06-13 20:17:20 +02:00
user_filter = (&(objectClass=posixAccount)(uid=%n))
pass_filter = (&(objectClass=posixAccount)(uid=%n))
2023-01-27 16:39:25 +01:00
'';
# see https://www.kuketz-blog.de/e-mail-anbieter-ip-stripping-aus-datenschutzgruenden/
header_cleanup = pkgs.writeText "header_cleanup_outgoing" ''
/^\s*(Received: from)[^\n]*(.*)/ REPLACE $1 127.0.0.1 (localhost [127.0.0.1])$2
/^\s*User-Agent/ IGNORE
/^\s*X-Enigmail/ IGNORE
/^\s*X-Mailer/ IGNORE
/^\s*X-Originating-IP/ IGNORE
/^\s*Mime-Version/ IGNORE
'';
2022-12-17 21:33:45 +01:00
in
{
2023-01-09 18:14:32 +01:00
sops.secrets."rspamd-password".owner = config.users.users.rspamd.name;
2023-02-15 14:47:14 +01:00
sops.secrets."dovecot_ldap_search".owner = config.services.dovecot2.user;
sops.secrets."postfix_ldap_aliases".owner = config.services.postfix.user;
2023-01-06 16:57:26 +01:00
networking.firewall.allowedTCPPorts = [
25 # insecure SMTP
2023-09-17 16:00:19 +02:00
143
465
587 # SMTP
993 # IMAP
2023-07-04 14:00:06 +02:00
4190 # sieve
];
2023-03-01 15:44:04 +01:00
users.users.postfix.extraGroups = [ "opendkim" ];
environment.etc = {
"dovecot/sieve-pipe/sa-learn-spam.sh" = {
text = ''
#!/bin/sh
${pkgs.rspamd}/bin/rspamc learn_spam
'';
mode = "0555";
};
"dovecot/sieve-pipe/sa-learn-ham.sh" = {
text = ''
#!/bin/sh
${pkgs.rspamd}/bin/rspamc learn_ham
'';
mode = "0555";
};
2023-09-04 13:10:01 +02:00
"dovecot/sieve/report-spam.sieve" = {
source = ./report-spam.sieve;
user = "dovecot2";
group = "dovecot2";
mode = "0544";
};
"dovecot/sieve/report-ham.sieve" = {
source = ./report-ham.sieve;
user = "dovecot2";
group = "dovecot2";
mode = "0544";
};
};
2023-01-06 16:57:26 +01:00
2022-12-17 21:33:45 +01:00
services = {
postfix = {
enable = true;
2023-05-31 14:58:26 +02:00
enableSubmission = true;
2023-02-24 17:11:53 +01:00
enableSubmissions = true;
2022-12-17 21:33:45 +01:00
hostname = "${hostname}";
domain = "${domain}";
origin = "${domain}";
destination = [ "${hostname}" "${domain}" "localhost" ];
networksStyle = "host"; # localhost and own public IP
2023-01-06 16:57:26 +01:00
sslCert = "/var/lib/acme/${hostname}/fullchain.pem";
sslKey = "/var/lib/acme/${hostname}/key.pem";
2023-04-04 00:04:04 +02:00
relayDomains = [ "hash:/var/lib/mailman/data/postfix_domains" ];
2022-12-17 21:33:45 +01:00
config = {
2023-03-22 11:19:15 +01:00
home_mailbox = "Maildir/";
# hostname used in helo command. It is recommended to have this match the reverse dns entry
smtp_helo_name = config.networking.rDNS;
2023-03-01 15:44:04 +01:00
smtp_use_tls = true;
# smtp_tls_security_level = "encrypt";
2023-03-01 15:44:04 +01:00
smtpd_use_tls = true;
# smtpd_tls_security_level = lib.mkForce "encrypt";
# smtpd_tls_auth_only = true;
2023-03-01 15:44:04 +01:00
smtpd_tls_protocols = [
"!SSLv2"
"!SSLv3"
"!TLSv1"
"!TLSv1.1"
];
# "reject_non_fqdn_hostname"
2022-12-17 21:33:45 +01:00
smtpd_recipient_restrictions = [
2023-02-24 17:11:53 +01:00
"permit_sasl_authenticated"
"permit_mynetworks"
2022-12-17 21:33:45 +01:00
"reject_unauth_destination"
2023-03-01 15:44:04 +01:00
"reject_non_fqdn_sender"
"reject_non_fqdn_recipient"
"reject_unknown_sender_domain"
"reject_unknown_recipient_domain"
"reject_unauth_destination"
"reject_unauth_pipelining"
"reject_invalid_hostname"
2023-09-18 09:48:55 +02:00
# "check_policy_service inet:localhost:12340" # disabled since it breaks mails to root@ifsr.de
2023-02-24 17:11:53 +01:00
];
smtpd_relay_restrictions = [
2022-12-17 21:33:45 +01:00
"permit_sasl_authenticated"
"permit_mynetworks"
2023-02-24 17:11:53 +01:00
"reject_unauth_destination"
2022-12-17 21:33:45 +01:00
];
smtp_header_checks = "pcre:${header_cleanup}";
# smtpd_sender_login_maps = [ "ldap:${ldap-senders}" ];
alias_maps = [ "hash:/etc/aliases" ];
2023-07-07 10:14:02 +02:00
alias_database = [ "hash:/etc/aliases" ];
# alias_maps = [ "hash:/etc/aliases" "ldap:${ldap-aliases}" ];
2023-03-01 15:44:04 +01:00
smtpd_milters = [ "local:/run/opendkim/opendkim.sock" ];
2023-03-01 15:23:07 +01:00
non_smtpd_milters = [ "local:/var/run/opendkim/opendkim.sock" ];
2022-12-17 21:33:45 +01:00
smtpd_sasl_auth_enable = true;
smtpd_sasl_path = "/var/lib/postfix/auth";
2023-02-24 17:11:53 +01:00
smtpd_sasl_type = "dovecot";
2023-04-04 00:04:04 +02:00
#mailman stuff
2023-07-07 10:14:02 +02:00
mailbox_transport = "lmtp:unix:/run/dovecot2/dovecot-lmtp";
2023-07-04 14:00:06 +02:00
2023-04-04 00:04:04 +02:00
transport_maps = [ "hash:/var/lib/mailman/data/postfix_lmtp" ];
2023-09-17 16:00:19 +02:00
virtual_alias_maps = [ "hash:/var/lib/mailman/data/postfix_vmap" ];
local_recipient_maps = [ "hash:/var/lib/mailman/data/postfix_lmtp" "ldap:${config.sops.secrets."postfix_ldap_aliases".path}" "$alias_maps" ];
2022-12-17 21:33:45 +01:00
};
};
dovecot2 = {
enable = true;
enableImap = true;
enableQuota = true;
quotaGlobalPerUser = "10G";
2023-07-04 14:00:06 +02:00
enableLmtp = true;
2023-03-24 15:51:57 +01:00
mailLocation = "maildir:~/Maildir";
2023-01-06 16:57:26 +01:00
sslServerCert = "/var/lib/acme/${hostname}/fullchain.pem";
sslServerKey = "/var/lib/acme/${hostname}/key.pem";
2023-07-04 14:00:06 +02:00
protocols = [ "imap" "sieve" ];
mailPlugins = {
2023-09-18 17:17:51 +02:00
globally.enable = [ "listescape" ];
2023-07-04 14:00:06 +02:00
perProtocol = {
imap = {
enable = [ "imap_sieve" ];
2023-07-04 14:00:06 +02:00
};
lmtp = {
enable = [ "sieve" ];
};
};
};
2022-12-17 21:33:45 +01:00
mailboxes = {
Spam = {
auto = "subscribe";
2022-12-17 21:33:45 +01:00
specialUse = "Junk";
autoexpunge = "60d";
2022-12-17 21:33:45 +01:00
};
Sent = {
auto = "subscribe";
2022-12-17 21:33:45 +01:00
specialUse = "Sent";
};
Drafts = {
auto = "subscribe";
2022-12-17 21:33:45 +01:00
specialUse = "Drafts";
};
Trash = {
auto = "subscribe";
2022-12-17 21:33:45 +01:00
specialUse = "Trash";
};
};
2023-07-04 14:00:06 +02:00
modules = [
pkgs.dovecot_pigeonhole
];
2022-12-17 21:33:45 +01:00
extraConfig = ''
2023-09-17 17:05:02 +02:00
auth_username_format = %Ln
passdb {
driver = ldap
args = ${dovecot-ldap-args}
}
userdb {
driver = ldap
args = ${dovecot-ldap-args}
}
service auth {
unix_listener /var/lib/postfix/auth {
group = postfix
mode = 0660
user = postfix
}
}
service managesieve-login {
inet_listener sieve {
port = 4190
}
service_count = 1
}
2023-09-17 16:00:19 +02:00
2023-09-17 17:05:02 +02:00
namespace inbox {
separator = /
inbox = yes
}
2023-09-17 16:00:19 +02:00
2023-09-17 17:05:02 +02:00
service lmtp {
unix_listener dovecot-lmtp {
group = postfix
mode = 0600
user = postfix
}
client_limit = 1
}
2023-09-17 17:05:02 +02:00
plugin {
sieve_plugins = sieve_imapsieve sieve_extprograms
sieve_global_extensions = +vnd.dovecot.pipe
sieve_pipe_bin_dir = /etc/dovecot/sieve-pipe
2023-09-17 17:05:02 +02:00
# Spam: From elsewhere to Spam folder or flag changed in Spam folder
imapsieve_mailbox1_name = Spam
imapsieve_mailbox1_causes = COPY APPEND FLAG
imapsieve_mailbox1_before = file:/etc/dovecot/sieve/report-spam.sieve
2023-09-17 17:05:02 +02:00
# Ham: From Spam folder to elsewhere
imapsieve_mailbox2_name = *
imapsieve_mailbox2_from = Spam
imapsieve_mailbox2_causes = COPY
imapsieve_mailbox2_before = file:/etc/dovecot/sieve/report-ham.sieve
2023-09-17 17:05:02 +02:00
# https://doc.dovecot.org/configuration_manual/plugins/listescape_plugin/
listescape_char = "\\"
}
2023-03-01 15:23:07 +01:00
'';
};
opendkim = {
enable = true;
domains = "csl:${config.networking.domain}";
2023-03-01 15:23:07 +01:00
selector = config.networking.hostName;
configFile = pkgs.writeText "opendkim-config" ''
UMask 0117
2022-12-17 21:33:45 +01:00
'';
};
rspamd = {
enable = true;
2023-01-06 16:57:26 +01:00
postfix.enable = true;
locals = {
"worker-controller.inc".source = config.sops.secrets."rspamd-password".path;
2023-01-09 18:14:32 +01:00
"redis.conf".text = ''
read_servers = "127.0.0.1";
write_servers = "127.0.0.1";
'';
# headers in spamassasin style to not break old sieve scripts
"worker-proxy.inc".text = ''
spam_header = "X-Spam-Flag";
'';
"milter_headers.conf".text = ''
use = ["x-spam-level", "x-spam-status"];
'';
"neural.conf".text = ''
servers = "127.0.0.1:6379";
enabled = true;
'';
neural_group.conf.text = ''
symbols = {
"NEURAL_SPAM" {
weight = 0.5; # fairly low weight since we don't know how this will behave
description = "Neural network spam";
}
"NEURAL_HAM" {
weight = -0.5;
description = "Neural network ham";
}
}
'';
2023-09-21 21:20:48 +02:00
"multimap.conf".text = ''
WHITELIST_SENDER_DOMAIN {
type = "from";
filter = "email:domain";
map = "/var/lib/rspamd/whitelist.sender.domain.map";
action = "accept";
}
WHITELIST_SENDER_EMAIL {
type = "from";
map = "/var/lib/rspamd/whitelist.sender.email.map";
action = "accept";
}
BLACKLIST_SENDER_DOMAIN {
type = "from";
filter = "email:domain";
map = "/var/lib/rspamd/blacklist.sender.domain.map";
action = "reject";
}
BLACKLIST_SENDER_EMAIL {
type = "from";
map = "/var/lib/rspamd/blacklist.sender.email.map";
action = "reject";
}
'';
2023-02-03 15:50:36 +01:00
};
2023-01-09 18:14:32 +01:00
};
redis = {
vmOverCommit = true;
servers.rspamd = {
enable = true;
port = 6379;
2022-12-17 21:33:45 +01:00
};
};
2023-01-06 16:57:26 +01:00
nginx = {
2022-12-17 21:33:45 +01:00
enable = true;
2023-01-09 18:14:32 +01:00
recommendedGzipSettings = true;
recommendedOptimisation = true;
recommendedProxySettings = true;
recommendedTlsSettings = true;
2023-01-06 16:57:26 +01:00
virtualHosts."${hostname}" = {
forceSSL = true;
enableACME = true;
};
virtualHosts."${rspamd-domain}" = {
forceSSL = true;
enableACME = true;
2023-01-09 18:14:32 +01:00
locations = {
"/" = {
proxyPass = "http://127.0.0.1:11334";
2023-01-09 18:14:32 +01:00
proxyWebsockets = true;
};
};
2023-01-06 16:57:26 +01:00
};
2022-12-17 21:33:45 +01:00
};
};
2023-05-25 22:43:16 +02:00
security.acme.certs."${domain}" = {
reloadServices = [
"postfix.service"
"dovecot2.service"
];
};
2022-12-17 21:33:45 +01:00
}