aboutsummaryrefslogtreecommitdiff
path: root/modules/private/mail
diff options
context:
space:
mode:
Diffstat (limited to 'modules/private/mail')
-rw-r--r--modules/private/mail/default.nix8
-rw-r--r--modules/private/mail/milters.nix20
-rw-r--r--modules/private/mail/opensmtpd.nix6
-rw-r--r--modules/private/mail/postfix.nix15
-rw-r--r--modules/private/mail/relay.nix233
5 files changed, 261 insertions, 21 deletions
diff --git a/modules/private/mail/default.nix b/modules/private/mail/default.nix
index a617934..b50e346 100644
--- a/modules/private/mail/default.nix
+++ b/modules/private/mail/default.nix
@@ -1,23 +1,25 @@
1{ lib, pkgs, config, ... }: 1{ lib, pkgs, config, ... }:
2{ 2{
3 imports = [ 3 imports = [
4 ./milters.nix 4 ./milters.nix
5 ./postfix.nix 5 ./postfix.nix
6 ./dovecot.nix 6 ./dovecot.nix
7 ./relay.nix
7 ./rspamd.nix 8 ./rspamd.nix
8 ./opensmtpd.nix 9 ./opensmtpd.nix
9 ]; 10 ];
10 options.myServices.mail.enable = lib.mkEnableOption "enable Mail services"; 11 options.myServices.mail.enable = lib.mkEnableOption "enable Mail services";
11 options.myServices.mailRelay.enable = lib.mkEnableOption "enable Mail relay services"; 12 options.myServices.mailRelay.enable = lib.mkEnableOption "enable Mail relay services";
13 options.myServices.mailBackup.enable = lib.mkEnableOption "enable MX backup services";
12 14
13 config = lib.mkIf config.myServices.mail.enable { 15 config = lib.mkIf config.myServices.mail.enable {
14 security.acme.certs."mail" = config.myServices.certificates.certConfig // { 16 security.acme.certs."mail" = config.myServices.certificates.certConfig // {
15 domain = "eldiron.immae.eu"; 17 domain = config.hostEnv.fqdn;
16 extraDomains = let 18 extraDomains = let
17 zonesWithMx = builtins.filter (zone: 19 zonesWithMx = builtins.filter (zone:
18 lib.attrsets.hasAttr "withEmail" zone && lib.lists.length zone.withEmail > 0 20 lib.attrsets.hasAttr "withEmail" zone && lib.lists.length zone.withEmail > 0
19 ) config.myEnv.dns.masterZones; 21 ) config.myEnv.dns.masterZones;
20 mxs = map (zone: "mx-1.${zone.name}") zonesWithMx; 22 mxs = map (zone: "${config.hostEnv.mx.subdomain}.${zone.name}") zonesWithMx;
21 in builtins.listToAttrs (map (mx: lib.attrsets.nameValuePair mx null) mxs); 23 in builtins.listToAttrs (map (mx: lib.attrsets.nameValuePair mx null) mxs);
22 }; 24 };
23 services.duplyBackup.profiles = { 25 services.duplyBackup.profiles = {
diff --git a/modules/private/mail/milters.nix b/modules/private/mail/milters.nix
index 6b033e8..16c8a7a 100644
--- a/modules/private/mail/milters.nix
+++ b/modules/private/mail/milters.nix
@@ -12,7 +12,7 @@
12 milters sockets 12 milters sockets
13 ''; 13 '';
14 }; 14 };
15 config = lib.mkIf config.myServices.mail.enable { 15 config = lib.mkIf (config.myServices.mail.enable || config.myServices.mailBackup.enable) {
16 secrets.keys = [ 16 secrets.keys = [
17 { 17 {
18 dest = "opendkim/eldiron.private"; 18 dest = "opendkim/eldiron.private";
@@ -34,7 +34,14 @@
34 user = config.services.opendmarc.user; 34 user = config.services.opendmarc.user;
35 group = config.services.opendmarc.group; 35 group = config.services.opendmarc.group;
36 permissions = "0400"; 36 permissions = "0400";
37 text = config.myEnv.mail.dmarc.ignore_hosts; 37 text = let
38 mxes = lib.attrsets.filterAttrs
39 (n: v: v.mx.enable)
40 config.myEnv.servers;
41 in
42 builtins.concatStringsSep "\n" ([
43 config.myEnv.mail.dmarc.ignore_hosts
44 ] ++ lib.mapAttrsToList (n: v: v.fqdn) mxes);
38 } 45 }
39 ]; 46 ];
40 users.users."${config.services.opendkim.user}".extraGroups = [ "keys" ]; 47 users.users."${config.services.opendkim.user}".extraGroups = [ "keys" ];
@@ -51,8 +58,9 @@
51 keyPath = "${config.secrets.location}/opendkim"; 58 keyPath = "${config.secrets.location}/opendkim";
52 selector = "eldiron"; 59 selector = "eldiron";
53 configFile = pkgs.writeText "opendkim.conf" '' 60 configFile = pkgs.writeText "opendkim.conf" ''
54 SubDomains yes 61 SubDomains yes
55 UMask 002 62 UMask 002
63 AlwaysAddARHeader yes
56 ''; 64 '';
57 group = config.services.postfix.group; 65 group = config.services.postfix.group;
58 }; 66 };
@@ -74,14 +82,14 @@
74 configFile = pkgs.writeText "opendmarc.conf" '' 82 configFile = pkgs.writeText "opendmarc.conf" ''
75 AuthservID HOSTNAME 83 AuthservID HOSTNAME
76 FailureReports false 84 FailureReports false
77 FailureReportsBcc postmaster@localhost.immae.eu 85 FailureReportsBcc postmaster@immae.eu
78 FailureReportsOnNone true 86 FailureReportsOnNone true
79 FailureReportsSentBy postmaster@immae.eu 87 FailureReportsSentBy postmaster@immae.eu
80 IgnoreAuthenticatedClients true 88 IgnoreAuthenticatedClients true
81 IgnoreHosts ${config.secrets.fullPaths."opendmarc/ignore.hosts"} 89 IgnoreHosts ${config.secrets.fullPaths."opendmarc/ignore.hosts"}
82 SoftwareHeader true 90 SoftwareHeader true
91 SPFIgnoreResults true
83 SPFSelfValidate true 92 SPFSelfValidate true
84 TrustedAuthservIDs HOSTNAME, immae.eu, nef2.ens.fr
85 UMask 002 93 UMask 002
86 ''; 94 '';
87 group = config.services.postfix.group; 95 group = config.services.postfix.group;
diff --git a/modules/private/mail/opensmtpd.nix b/modules/private/mail/opensmtpd.nix
index 7831ac0..e4a6140 100644
--- a/modules/private/mail/opensmtpd.nix
+++ b/modules/private/mail/opensmtpd.nix
@@ -8,7 +8,7 @@
8 group = "smtpd"; 8 group = "smtpd";
9 permissions = "0400"; 9 permissions = "0400";
10 text = '' 10 text = ''
11 eldiron ${name}:${config.myEnv.servers."${name}".ldap.password} 11 eldiron ${name}:${config.hostEnv.ldap.password}
12 ''; 12 '';
13 } 13 }
14 ]; 14 ];
@@ -22,12 +22,12 @@
22 # filter "fixfrom" \ 22 # filter "fixfrom" \
23 # proc-exec "${pkgs.procmail}/bin/formail -i 'From: ${name}@immae.eu'" 23 # proc-exec "${pkgs.procmail}/bin/formail -i 'From: ${name}@immae.eu'"
24 action "relay-rewrite-from" relay \ 24 action "relay-rewrite-from" relay \
25 helo ${config.hostEnv.FQDN} \ 25 helo ${config.hostEnv.fqdn} \
26 host smtp+tls://eldiron@eldiron.immae.eu:587 \ 26 host smtp+tls://eldiron@eldiron.immae.eu:587 \
27 auth <creds> \ 27 auth <creds> \
28 mail-from ${name}@immae.eu 28 mail-from ${name}@immae.eu
29 action "relay" relay \ 29 action "relay" relay \
30 helo ${config.hostEnv.FQDN} \ 30 helo ${config.hostEnv.fqdn} \
31 host smtp+tls://eldiron@eldiron.immae.eu:587 \ 31 host smtp+tls://eldiron@eldiron.immae.eu:587 \
32 auth <creds> 32 auth <creds>
33 match for any !mail-from "@immae.eu" action "relay-rewrite-from" 33 match for any !mail-from "@immae.eu" action "relay-rewrite-from"
diff --git a/modules/private/mail/postfix.nix b/modules/private/mail/postfix.nix
index 6623735..bd284cb 100644
--- a/modules/private/mail/postfix.nix
+++ b/modules/private/mail/postfix.nix
@@ -1,4 +1,4 @@
1{ lib, pkgs, config, nodes, ... }: 1{ lib, pkgs, config, nodes, name, ... }:
2{ 2{
3 config = lib.mkIf config.myServices.mail.enable { 3 config = lib.mkIf config.myServices.mail.enable {
4 services.duplyBackup.profiles.mail.excludeFile = '' 4 services.duplyBackup.profiles.mail.excludeFile = ''
@@ -299,8 +299,6 @@
299 lib.imap1 (i: m: "${m.type}:/etc/postfix/relay_${n}_${toString i}") v.recipient_maps 299 lib.imap1 (i: m: "${m.type}:/etc/postfix/relay_${n}_${toString i}") v.recipient_maps
300 ) config.myEnv.mail.postfix.backup_domains); 300 ) config.myEnv.mail.postfix.backup_domains);
301 smtpd_relay_restrictions = [ 301 smtpd_relay_restrictions = [
302 "permit_mynetworks"
303 "permit_sasl_authenticated"
304 "defer_unauth_destination" 302 "defer_unauth_destination"
305 ] ++ lib.flatten (lib.attrsets.mapAttrsToList (n: v: 303 ] ++ lib.flatten (lib.attrsets.mapAttrsToList (n: v:
306 if lib.attrsets.hasAttr "relay_restrictions" v 304 if lib.attrsets.hasAttr "relay_restrictions" v
@@ -317,8 +315,8 @@
317 smtp_tls_loglevel = "1"; 315 smtp_tls_loglevel = "1";
318 316
319 ### Force ip bind for smtp 317 ### Force ip bind for smtp
320 smtp_bind_address = config.myEnv.servers.eldiron.ips.main.ip4; 318 smtp_bind_address = config.hostEnv.ips.main.ip4;
321 smtp_bind_address6 = builtins.head config.myEnv.servers.eldiron.ips.main.ip6; 319 smtp_bind_address6 = builtins.head config.hostEnv.ips.main.ip6;
322 320
323 # Use some relays when authorized senders are not myself 321 # Use some relays when authorized senders are not myself
324 smtp_sasl_mechanism_filter = "plain,login"; # GSSAPI Not correctly supported by postfix 322 smtp_sasl_mechanism_filter = "plain,login"; # GSSAPI Not correctly supported by postfix
@@ -333,13 +331,11 @@
333 ### opendkim, opendmarc, openarc milters 331 ### opendkim, opendmarc, openarc milters
334 non_smtpd_milters = [ 332 non_smtpd_milters = [
335 "unix:${config.myServices.mail.milters.sockets.opendkim}" 333 "unix:${config.myServices.mail.milters.sockets.opendkim}"
336 "unix:${config.myServices.mail.milters.sockets.opendmarc}"
337 "unix:${config.myServices.mail.milters.sockets.openarc}"
338 ]; 334 ];
339 smtpd_milters = [ 335 smtpd_milters = [
340 "unix:${config.myServices.mail.milters.sockets.opendkim}" 336 "unix:${config.myServices.mail.milters.sockets.opendkim}"
341 "unix:${config.myServices.mail.milters.sockets.opendmarc}"
342 "unix:${config.myServices.mail.milters.sockets.openarc}" 337 "unix:${config.myServices.mail.milters.sockets.openarc}"
338 "unix:${config.myServices.mail.milters.sockets.opendmarc}"
343 ]; 339 ];
344 }; 340 };
345 enable = true; 341 enable = true;
@@ -357,6 +353,7 @@
357 smtpd_sasl_path = "private/auth"; 353 smtpd_sasl_path = "private/auth";
358 smtpd_reject_unlisted_recipient = "no"; 354 smtpd_reject_unlisted_recipient = "no";
359 smtpd_client_restrictions = "permit_sasl_authenticated,reject"; 355 smtpd_client_restrictions = "permit_sasl_authenticated,reject";
356 smtpd_relay_restrictions = "permit_sasl_authenticated,reject";
360 # Refuse to send e-mails with a From that is not handled 357 # Refuse to send e-mails with a From that is not handled
361 smtpd_sender_restrictions = 358 smtpd_sender_restrictions =
362 "reject_sender_login_mismatch,reject_unlisted_sender,permit_sasl_authenticated,reject"; 359 "reject_sender_login_mismatch,reject_unlisted_sender,permit_sasl_authenticated,reject";
@@ -378,7 +375,7 @@
378 ''; 375 '';
379 destination = ["localhost"]; 376 destination = ["localhost"];
380 # This needs to reverse DNS 377 # This needs to reverse DNS
381 hostname = "eldiron.immae.eu"; 378 hostname = config.hostEnv.fqdn;
382 setSendmail = true; 379 setSendmail = true;
383 sslCert = "/var/lib/acme/mail/fullchain.pem"; 380 sslCert = "/var/lib/acme/mail/fullchain.pem";
384 sslKey = "/var/lib/acme/mail/key.pem"; 381 sslKey = "/var/lib/acme/mail/key.pem";
diff --git a/modules/private/mail/relay.nix b/modules/private/mail/relay.nix
new file mode 100644
index 0000000..9111350
--- /dev/null
+++ b/modules/private/mail/relay.nix
@@ -0,0 +1,233 @@
1{ lib, pkgs, config, nodes, name, ... }:
2{
3 config = lib.mkIf config.myServices.mailBackup.enable {
4 security.acme.certs."mail" = config.myServices.certificates.certConfig // {
5 postRun = ''
6 systemctl restart postfix.service
7 '';
8 domain = config.hostEnv.fqdn;
9 extraDomains = let
10 zonesWithMx = builtins.filter (zone:
11 lib.attrsets.hasAttr "withEmail" zone && lib.lists.length zone.withEmail > 0
12 ) config.myEnv.dns.masterZones;
13 mxs = map (zone: "${config.myEnv.servers."${name}".mx.subdomain}.${zone.name}") zonesWithMx;
14 in builtins.listToAttrs (map (mx: lib.attrsets.nameValuePair mx null) mxs);
15 };
16 secrets.keys = [
17 {
18 dest = "postfix/mysql_alias_maps";
19 user = config.services.postfix.user;
20 group = config.services.postfix.group;
21 permissions = "0440";
22 text = ''
23 # We need to specify that option to trigger ssl connection
24 tls_ciphers = TLSv1.2
25 user = ${config.myEnv.mail.postfix.mysql.user}
26 password = ${config.myEnv.mail.postfix.mysql.password}
27 hosts = ${config.myEnv.mail.postfix.mysql.remoteHost}
28 dbname = ${config.myEnv.mail.postfix.mysql.database}
29 query = SELECT DISTINCT 1
30 FROM forwardings_merge
31 WHERE
32 ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s'))
33 AND active = 1
34 AND '%s' NOT IN
35 (
36 SELECT source
37 FROM forwardings_blacklisted
38 WHERE source = '%s'
39 ) UNION
40 SELECT 'devnull@immae.eu'
41 FROM forwardings_blacklisted
42 WHERE source = '%s'
43 '';
44 }
45 {
46 dest = "postfix/mysql_mailbox_maps";
47 user = config.services.postfix.user;
48 group = config.services.postfix.group;
49 permissions = "0440";
50 text = ''
51 # We need to specify that option to trigger ssl connection
52 tls_ciphers = TLSv1.2
53 user = ${config.myEnv.mail.postfix.mysql.user}
54 password = ${config.myEnv.mail.postfix.mysql.password}
55 hosts = ${config.myEnv.mail.postfix.mysql.remoteHost}
56 dbname = ${config.myEnv.mail.postfix.mysql.database}
57 query = SELECT DISTINCT 1
58 FROM mailboxes
59 WHERE active = 1
60 AND (
61 (domain = '%d' AND user = '%u' AND regex = 0)
62 OR (
63 regex = 1
64 AND '%d' REGEXP CONCAT('^',domain,'$')
65 AND '%u' REGEXP CONCAT('^',user,'$')
66 )
67 )
68 LIMIT 1
69 '';
70 }
71 {
72 dest = "postfix/ldap_ejabberd_users_immae_fr";
73 user = config.services.postfix.user;
74 group = config.services.postfix.group;
75 permissions = "0440";
76 text = ''
77 server_host = ldaps://${config.myEnv.jabber.ldap.host}:636
78 search_base = ${config.myEnv.jabber.ldap.base}
79 query_filter = ${config.myEnv.jabber.postfix_user_filter}
80 domain = immae.fr
81 bind_dn = ${config.myEnv.jabber.ldap.dn}
82 bind_pw = ${config.myEnv.jabber.ldap.password}
83 result_attribute = immaeXmppUid
84 result_format = ejabberd@localhost
85 version = 3
86 '';
87 }
88 ];
89
90 networking.firewall.allowedTCPPorts = [ 25 ];
91
92 nixpkgs.overlays = [ (self: super: {
93 postfix = super.postfix.override { withMySQL = true; };
94 }) ];
95 users.users."${config.services.postfix.user}".extraGroups = [ "keys" ];
96 services.filesWatcher.postfix = {
97 restart = true;
98 paths = [
99 config.secrets.fullPaths."postfix/mysql_alias_maps"
100 config.secrets.fullPaths."postfix/mysql_mailbox_maps"
101 config.secrets.fullPaths."postfix/ldap_ejabberd_users_immae_fr"
102 ];
103 };
104 services.postfix = {
105 mapFiles = let
106 recipient_maps = let
107 name = n: i: "relay_${n}_${toString i}";
108 pair = n: i: m: lib.attrsets.nameValuePair (name n i) (
109 if m.type == "hash"
110 then pkgs.writeText (name n i) m.content
111 else null
112 );
113 pairs = n: v: lib.imap1 (i: m: pair n i m) v.recipient_maps;
114 in lib.attrsets.filterAttrs (k: v: v != null) (
115 lib.attrsets.listToAttrs (lib.flatten (
116 lib.attrsets.mapAttrsToList pairs config.myEnv.mail.postfix.backup_domains
117 ))
118 );
119 relay_restrictions = lib.attrsets.filterAttrs (k: v: v != null) (
120 lib.attrsets.mapAttrs' (n: v:
121 lib.attrsets.nameValuePair "recipient_access_${n}" (
122 if lib.attrsets.hasAttr "relay_restrictions" v
123 then pkgs.writeText "recipient_access_${n}" v.relay_restrictions
124 else null
125 )
126 ) config.myEnv.mail.postfix.backup_domains
127 );
128 virtual_map = {
129 virtual = pkgs.writeText "postfix-virtual" (
130 builtins.concatStringsSep "\n" (
131 lib.attrsets.mapAttrsToList (
132 n: v: lib.optionalString v.external ''
133 script_${n}@mail.immae.eu 1
134 ''
135 ) config.myEnv.mail.scripts
136 )
137 );
138 };
139 sasl_access = {
140 host_dummy_mailboxes = pkgs.writeText "host-virtual-mailbox"
141 (builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList (n: v: "${n}@immae.eu 1") nodes));
142 };
143 in
144 recipient_maps // relay_restrictions // virtual_map // sasl_access;
145 config = {
146 ### postfix module overrides
147 readme_directory = "${pkgs.postfix}/share/postfix/doc";
148 smtp_tls_CAfile = lib.mkForce "";
149 smtp_tls_cert_file = lib.mkForce "";
150 smtp_tls_key_file = lib.mkForce "";
151
152 message_size_limit = "1073741824"; # Don't put 0 here, it's not equivalent to "unlimited"
153 mailbox_size_limit = "1073741825"; # Workaround, local delivered mails should all go through scripts
154 alias_database = "\$alias_maps";
155
156 ### Relay domains
157 relay_domains = let
158 backups = lib.flatten (lib.attrsets.mapAttrsToList (n: v: v.domains or []) config.myEnv.mail.postfix.backup_domains);
159 virtual_domains = config.myEnv.mail.postfix.additional_mailbox_domains
160 ++ lib.remove "localhost.immae.eu" (lib.remove null (lib.flatten (map
161 (zone: map
162 (e: if e.receive
163 then "${e.domain}${lib.optionalString (e.domain != "") "."}${zone.name}"
164 else null
165 )
166 (zone.withEmail or [])
167 )
168 config.myEnv.dns.masterZones
169 )));
170 in
171 backups ++ virtual_domains;
172 relay_recipient_maps = let
173 backup_recipients = lib.flatten (lib.attrsets.mapAttrsToList (n: v:
174 lib.imap1 (i: m: "${m.type}:/etc/postfix/relay_${n}_${toString i}") v.recipient_maps
175 ) config.myEnv.mail.postfix.backup_domains);
176 virtual_alias_maps = [
177 "hash:/etc/postfix/virtual"
178 "mysql:${config.secrets.fullPaths."postfix/mysql_alias_maps"}"
179 "ldap:${config.secrets.fullPaths."postfix/ldap_ejabberd_users_immae_fr"}"
180 ];
181 virtual_mailbox_maps = [
182 "hash:/etc/postfix/host_dummy_mailboxes"
183 "mysql:${config.secrets.fullPaths."postfix/mysql_mailbox_maps"}"
184 ];
185 in
186 backup_recipients ++ virtual_alias_maps ++ virtual_mailbox_maps;
187 smtpd_relay_restrictions = [
188 "defer_unauth_destination"
189 ] ++ lib.flatten (lib.attrsets.mapAttrsToList (n: v:
190 if lib.attrsets.hasAttr "relay_restrictions" v
191 then [ "check_recipient_access hash:/etc/postfix/recipient_access_${n}" ]
192 else []
193 ) config.myEnv.mail.postfix.backup_domains);
194
195 ### Additional smtpd configuration
196 smtpd_tls_received_header = "yes";
197 smtpd_tls_loglevel = "1";
198
199 ### Email sending configuration
200 smtp_tls_security_level = "may";
201 smtp_tls_loglevel = "1";
202
203 ### Force ip bind for smtp
204 smtp_bind_address = config.myEnv.servers."${name}".ips.main.ip4;
205 smtp_bind_address6 = builtins.head config.myEnv.servers."${name}".ips.main.ip6;
206
207 smtpd_milters = [
208 "unix:${config.myServices.mail.milters.sockets.opendkim}"
209 "unix:${config.myServices.mail.milters.sockets.openarc}"
210 "unix:${config.myServices.mail.milters.sockets.opendmarc}"
211 ];
212 };
213 enable = true;
214 enableSmtp = true;
215 enableSubmission = false;
216 # FIXME: Mail adressed to localhost.immae.eu will still have mx-1 as
217 # prioritized MX, which provokes "mail for localhost.immae.eu loops
218 # back to myself" errors. This transport entry forces to push
219 # e-mails to its right destination.
220 transport = ''
221 localhost.immae.eu smtp:[immae.eu]:25
222 '';
223 destination = ["localhost"];
224 # This needs to reverse DNS
225 hostname = config.hostEnv.fqdn;
226 setSendmail = false;
227 sslCert = "/var/lib/acme/mail/fullchain.pem";
228 sslKey = "/var/lib/acme/mail/key.pem";
229 recipientDelimiter = "+";
230 };
231 };
232}
233