fruitbasket/modules/mail.nix

304 lines
8.9 KiB
Nix
Raw Normal View History

2023-03-01 16:34:15 +01:00
{ config, pkgs, lib, ... }:
2022-12-17 21:33:45 +01:00
let
2023-01-06 16:57:26 +01:00
hostname = "mail.${config.fsr.domain}";
domain = config.fsr.domain;
rspamd-domain = "rspamd.${config.fsr.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
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-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" ];
2023-03-24 16:24:18 +01:00
extraAliases = ''
# Taken from kaki, maybe we can throw out some at some point
# General redirections for pseudo accounts
bin: root
daemon: root
named: root
nobody: root
uucp: root
www: root
ftp-bugs: root
postfix: root
# Well-known aliases
manager: root
dumper: root
operator: root
abuse: postmaster
postmaster: root
2023-03-24 16:24:18 +01:00
# trap decode to catch security attacks
decode: root
# yeet into the void
noreply: /dev/null
2023-03-24 16:24:18 +01:00
'';
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
2023-09-01 10:42:22 +02:00
# smtp_helo_name = "x8d1e1ea9.agdsn.tu-dresden.de";
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"
"check_policy_service inet:localhost:12340"
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" ];
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 = {
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 = "create";
specialUse = "Junk";
};
Sent = {
auto = "create";
specialUse = "Sent";
};
Drafts = {
auto = "create";
specialUse = "Drafts";
};
Trash = {
auto = "create";
specialUse = "Trash";
};
};
2023-07-04 14:00:06 +02:00
modules = [
pkgs.dovecot_pigeonhole
];
2022-12-17 21:33:45 +01:00
extraConfig = ''
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
2023-07-04 14:00:06 +02:00
}
}
service managesieve-login {
inet_listener sieve {
port = 4190
2023-07-04 14:00:06 +02:00
}
service_count = 1
}
service lmtp {
unix_listener dovecot-lmtp {
group = postfix
mode = 0600
user = postfix
2023-07-04 14:00:06 +02:00
}
client_limit = 1
}
plugin {
sieve_plugins = sieve_imapsieve sieve_extprograms
sieve_global_extensions = +vnd.dovecot.pipe
sieve_pipe_bin_dir = /etc/dovecot/sieve-pipe
# 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:/var/lib/dovecot/imap_sieve/report-spam.sieve
# Ham: From Spam folder to elsewhere
imapsieve_mailbox2_name = *
imapsieve_mailbox2_from = Spam
imapsieve_mailbox2_causes = COPY
imapsieve_mailbox1_before = file:/var/lib/dovecot/imap_sieve/report-ham.sieve
}
2023-03-01 15:23:07 +01:00
'';
};
opendkim = {
enable = true;
domains = "csl:${config.fsr.domain}";
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"];
'';
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
}