]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - modules/private/mail/postfix.nix
Remove duply-backup
[perso/Immae/Config/Nix.git] / modules / private / mail / postfix.nix
CommitLineData
71a2425e 1{ lib, pkgs, config, nodes, ... }:
a929614f 2{
8415083e 3 config = lib.mkIf config.myServices.mail.enable {
4c4652aa
IB
4 secrets.keys = {
5 "postfix/mysql_alias_maps" = {
8415083e
IB
6 user = config.services.postfix.user;
7 group = config.services.postfix.group;
8 permissions = "0440";
9 text = ''
10 # We need to specify that option to trigger ssl connection
11 tls_ciphers = TLSv1.2
ab8f306d
IB
12 user = ${config.myEnv.mail.postfix.mysql.user}
13 password = ${config.myEnv.mail.postfix.mysql.password}
14 hosts = unix:${config.myEnv.mail.postfix.mysql.socket}
15 dbname = ${config.myEnv.mail.postfix.mysql.database}
8415083e 16 query = SELECT DISTINCT destination
418a4ed7 17 FROM forwardings
8415083e
IB
18 WHERE
19 ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s'))
20 AND active = 1
21 AND '%s' NOT IN
22 (
23 SELECT source
24 FROM forwardings_blacklisted
25 WHERE source = '%s'
26 ) UNION
27 SELECT 'devnull@immae.eu'
a929614f
IB
28 FROM forwardings_blacklisted
29 WHERE source = '%s'
8415083e 30 '';
4c4652aa
IB
31 };
32 "postfix/ldap_mailboxes" = {
8415083e
IB
33 user = config.services.postfix.user;
34 group = config.services.postfix.group;
35 permissions = "0440";
36 text = ''
22b4bd78
IB
37 server_host = ldaps://${config.myEnv.mail.dovecot.ldap.host}:636
38 search_base = ${config.myEnv.mail.dovecot.ldap.base}
39 query_filter = ${config.myEnv.mail.dovecot.ldap.postfix_mailbox_filter}
40 bind_dn = ${config.myEnv.mail.dovecot.ldap.dn}
41 bind_pw = ${config.myEnv.mail.dovecot.ldap.password}
42 result_attribute = immaePostfixAddress
43 result_format = dummy
44 version = 3
a929614f 45 '';
4c4652aa
IB
46 };
47 "postfix/mysql_sender_login_maps" = {
8415083e
IB
48 user = config.services.postfix.user;
49 group = config.services.postfix.group;
50 permissions = "0440";
51 text = ''
52 # We need to specify that option to trigger ssl connection
53 tls_ciphers = TLSv1.2
ab8f306d
IB
54 user = ${config.myEnv.mail.postfix.mysql.user}
55 password = ${config.myEnv.mail.postfix.mysql.password}
56 hosts = unix:${config.myEnv.mail.postfix.mysql.socket}
57 dbname = ${config.myEnv.mail.postfix.mysql.database}
8415083e 58 query = SELECT DISTINCT destination
418a4ed7 59 FROM forwardings
8415083e 60 WHERE
d8c20bc3
IB
61 (
62 (regex = 1 AND CONCAT(SUBSTRING_INDEX('%u', '+', 1), '@%d') REGEXP CONCAT('^',source,'$') )
63 OR
64 (regex = 0 AND source = CONCAT(SUBSTRING_INDEX('%u', '+', 1), '@%d'))
65 )
8415083e 66 AND active = 1
d8c20bc3 67 UNION SELECT CONCAT(SUBSTRING_INDEX('%u', '+', 1), '@%d') AS destination
8415083e 68 '';
4c4652aa
IB
69 };
70 "postfix/mysql_sender_relays_maps" = {
87a8bffd
IB
71 user = config.services.postfix.user;
72 group = config.services.postfix.group;
73 permissions = "0440";
74 text = ''
75 # We need to specify that option to trigger ssl connection
76 tls_ciphers = TLSv1.2
77 user = ${config.myEnv.mail.postfix.mysql.user}
78 password = ${config.myEnv.mail.postfix.mysql.password}
79 hosts = unix:${config.myEnv.mail.postfix.mysql.socket}
80 dbname = ${config.myEnv.mail.postfix.mysql.database}
81 # INSERT INTO sender_relays
82 # (`from`, owner, relay, login, password, regex, active)
83 # VALUES
84 # ( 'sender@otherhost.org'
85 # , 'me@mail.immae.eu'
86 # , '[otherhost.org]:587'
87 # , 'otherhostlogin'
88 # , AES_ENCRYPT('otherhostpassword', '${config.myEnv.mail.postfix.mysql.password_encrypt}')
89 # , '0'
90 # , '1');
91
92 query = SELECT DISTINCT `owner`
93 FROM sender_relays
94 WHERE
95 ((regex = 1 AND '%s' REGEXP CONCAT('^',`from`,'$') ) OR (regex = 0 AND `from` = '%s'))
96 AND active = 1
97 '';
4c4652aa
IB
98 };
99 "postfix/mysql_sender_relays_hosts" = {
87a8bffd
IB
100 user = config.services.postfix.user;
101 group = config.services.postfix.group;
102 permissions = "0440";
103 text = ''
104 # We need to specify that option to trigger ssl connection
105 tls_ciphers = TLSv1.2
106 user = ${config.myEnv.mail.postfix.mysql.user}
107 password = ${config.myEnv.mail.postfix.mysql.password}
108 hosts = unix:${config.myEnv.mail.postfix.mysql.socket}
109 dbname = ${config.myEnv.mail.postfix.mysql.database}
110
111 query = SELECT DISTINCT relay
112 FROM sender_relays
113 WHERE
114 ((regex = 1 AND '%s' REGEXP CONCAT('^',`from`,'$') ) OR (regex = 0 AND `from` = '%s'))
115 AND active = 1
116 '';
4c4652aa
IB
117 };
118 "postfix/mysql_sender_relays_creds" = {
87a8bffd
IB
119 user = config.services.postfix.user;
120 group = config.services.postfix.group;
121 permissions = "0440";
122 text = ''
123 # We need to specify that option to trigger ssl connection
124 tls_ciphers = TLSv1.2
125 user = ${config.myEnv.mail.postfix.mysql.user}
126 password = ${config.myEnv.mail.postfix.mysql.password}
127 hosts = unix:${config.myEnv.mail.postfix.mysql.socket}
128 dbname = ${config.myEnv.mail.postfix.mysql.database}
129
130 query = SELECT DISTINCT CONCAT(`login`, ':', AES_DECRYPT(`password`, '${config.myEnv.mail.postfix.mysql.password_encrypt}'))
131 FROM sender_relays
132 WHERE
133 ((regex = 1 AND '%s' REGEXP CONCAT('^',`from`,'$') ) OR (regex = 0 AND `from` = '%s'))
134 AND active = 1
135 '';
4c4652aa
IB
136 };
137 "postfix/ldap_ejabberd_users_immae_fr" = {
5b53d86f
IB
138 user = config.services.postfix.user;
139 group = config.services.postfix.group;
140 permissions = "0440";
141 text = ''
142 server_host = ldaps://${config.myEnv.jabber.ldap.host}:636
143 search_base = ${config.myEnv.jabber.ldap.base}
144 query_filter = ${config.myEnv.jabber.postfix_user_filter}
145 domain = immae.fr
146 bind_dn = ${config.myEnv.jabber.ldap.dn}
147 bind_pw = ${config.myEnv.jabber.ldap.password}
148 result_attribute = immaeXmppUid
149 result_format = ejabberd@localhost
150 version = 3
151 '';
4c4652aa
IB
152 };
153 } // lib.mapAttrs' (name: v: lib.nameValuePair "postfix/scripts/${name}-env" {
31d99b75
IB
154 user = "postfixscripts";
155 group = "root";
156 permissions = "0400";
157 text = builtins.toJSON v.env;
4c4652aa 158 }) config.myEnv.mail.scripts;
a929614f 159
8415083e 160 networking.firewall.allowedTCPPorts = [ 25 465 587 ];
a929614f 161
31d99b75
IB
162 users.users.postfixscripts = {
163 group = "keys";
164 uid = config.ids.uids.postfixscripts;
165 description = "Postfix scripts user";
166 };
8415083e
IB
167 users.users."${config.services.postfix.user}".extraGroups = [ "keys" ];
168 services.filesWatcher.postfix = {
169 restart = true;
170 paths = [
171 config.secrets.fullPaths."postfix/mysql_alias_maps"
22b4bd78 172 config.secrets.fullPaths."postfix/ldap_mailboxes"
8415083e 173 config.secrets.fullPaths."postfix/mysql_sender_login_maps"
5b53d86f 174 config.secrets.fullPaths."postfix/ldap_ejabberd_users_immae_fr"
8415083e
IB
175 ];
176 };
177 services.postfix = {
2a61e9da
IB
178 extraAliases = let
179 toScript = name: script: pkgs.writeScript name ''
180 #! ${pkgs.stdenv.shell}
181 mail=$(${pkgs.coreutils}/bin/cat -)
182 output=$(echo "$mail" | ${script} 2>&1)
183 ret=$?
184
185 if [ "$ret" != "0" ]; then
186 echo "$mail" \
187 | ${pkgs.procmail}/bin/formail -i "X-Return-Code: $ret" \
188 | /run/wrappers/bin/sendmail -i scripts_error+${name}@mail.immae.eu
189
190 messageId=$(echo "$mail" | ${pkgs.procmail}/bin/formail -x "Message-Id:")
191 repeat=$(echo "$mail" | ${pkgs.procmail}/bin/formail -X "From:" -X "Received:")
192
193 ${pkgs.coreutils}/bin/cat <<EOF | /run/wrappers/bin/sendmail -i scripts_error+${name}@mail.immae.eu
194 $repeat
195 To: scripts_error+${name}@mail.immae.eu
196 Subject: Log from script error
197 Content-Type: text/plain; charset="UTF-8"
198 Content-Transfer-Encoding: 8bit
199 References:$messageId
200 MIME-Version: 1.0
201 X-Return-Code: $ret
202
203 Error code: $ret
204 Output of message:
205 --------------
206 $output
207 --------------
208 EOF
209 fi
210 '';
211 scripts = lib.attrsets.mapAttrs (n: v:
da30ae4f 212 toScript n (pkgs.callPackage (builtins.fetchGit { url = v.src.url; ref = "master"; rev = v.src.rev; }) { scriptEnv = config.secrets.fullPaths."postfix/scripts/${n}-env"; })
71a2425e
IB
213 ) config.myEnv.mail.scripts // {
214 testmail = pkgs.writeScript "testmail" ''
215 #! ${pkgs.stdenv.shell}
216 ${pkgs.coreutils}/bin/touch \
217 "/var/lib/naemon/checks/email/$(${pkgs.procmail}/bin/formail -x To: | ${pkgs.coreutils}/bin/tr -d ' <>')"
218 '';
219 };
2a61e9da 220 in builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList (n: v: ''${n}: "|${v}"'') scripts);
8415083e
IB
221 mapFiles = let
222 recipient_maps = let
223 name = n: i: "relay_${n}_${toString i}";
224 pair = n: i: m: lib.attrsets.nameValuePair (name n i) (
225 if m.type == "hash"
226 then pkgs.writeText (name n i) m.content
04b2ab97 227 else null
8415083e
IB
228 );
229 pairs = n: v: lib.imap1 (i: m: pair n i m) v.recipient_maps;
230 in lib.attrsets.filterAttrs (k: v: v != null) (
231 lib.attrsets.listToAttrs (lib.flatten (
ab8f306d 232 lib.attrsets.mapAttrsToList pairs config.myEnv.mail.postfix.backup_domains
8415083e
IB
233 ))
234 );
235 relay_restrictions = lib.attrsets.filterAttrs (k: v: v != null) (
236 lib.attrsets.mapAttrs' (n: v:
237 lib.attrsets.nameValuePair "recipient_access_${n}" (
238 if lib.attrsets.hasAttr "relay_restrictions" v
239 then pkgs.writeText "recipient_access_${n}" v.relay_restrictions
240 else null
241 )
ab8f306d 242 ) config.myEnv.mail.postfix.backup_domains
8415083e 243 );
2a61e9da 244 virtual_map = {
71a2425e
IB
245 virtual = let
246 cfg = config.myEnv.monitoring.email_check.eldiron;
247 address = "${cfg.mail_address}@${cfg.mail_domain}";
248 in pkgs.writeText "postfix-virtual" (
2a61e9da 249 builtins.concatStringsSep "\n" (
71a2425e 250 ["${address} testmail@localhost"] ++
2a61e9da 251 lib.attrsets.mapAttrsToList (
5b53d86f 252 n: v: lib.optionalString v.external ''
2a61e9da
IB
253 script_${n}@mail.immae.eu ${n}@localhost, scripts@mail.immae.eu
254 ''
ab8f306d 255 ) config.myEnv.mail.scripts
2a61e9da
IB
256 )
257 );
258 };
deca5e9b 259 sasl_access = {
ef0a9217
IB
260 host_sender_login = with lib.attrsets; let
261 addresses = zipAttrs (lib.flatten (mapAttrsToList
262 (n: v: (map (e: { "${e}" = "${n}@immae.eu"; }) v.emails)) config.myEnv.servers));
263 joined = builtins.concatStringsSep ",";
264 in pkgs.writeText "host-sender-login"
265 (builtins.concatStringsSep "\n" (mapAttrsToList (n: v: "${n} ${joined v}") addresses));
deca5e9b 266 };
8415083e 267 in
deca5e9b 268 recipient_maps // relay_restrictions // virtual_map // sasl_access;
8415083e
IB
269 config = {
270 ### postfix module overrides
271 readme_directory = "${pkgs.postfix}/share/postfix/doc";
272 smtp_tls_CAfile = lib.mkForce "";
273 smtp_tls_cert_file = lib.mkForce "";
274 smtp_tls_key_file = lib.mkForce "";
a929614f 275
8415083e 276 message_size_limit = "1073741824"; # Don't put 0 here, it's not equivalent to "unlimited"
2a61e9da 277 mailbox_size_limit = "1073741825"; # Workaround, local delivered mails should all go through scripts
8415083e 278 alias_database = "\$alias_maps";
a929614f 279
31d99b75
IB
280 ### Aliases scripts user
281 default_privs = "postfixscripts";
282
8415083e 283 ### Virtual mailboxes config
418a4ed7
IB
284 virtual_alias_maps = [
285 "hash:/etc/postfix/virtual"
286 "mysql:${config.secrets.fullPaths."postfix/mysql_alias_maps"}"
287 "ldap:${config.secrets.fullPaths."postfix/ldap_ejabberd_users_immae_fr"}"
288 ];
ab8f306d 289 virtual_mailbox_domains = config.myEnv.mail.postfix.additional_mailbox_domains
ccb8bab4 290 ++ lib.remove null (lib.flatten (map
8415083e
IB
291 (zone: map
292 (e: if e.receive
293 then "${e.domain}${lib.optionalString (e.domain != "") "."}${zone.name}"
294 else null
295 )
296 (zone.withEmail or [])
a929614f 297 )
ab8f306d 298 config.myEnv.dns.masterZones
ccb8bab4 299 ));
418a4ed7 300 virtual_mailbox_maps = [
22b4bd78 301 "ldap:${config.secrets.fullPaths."postfix/ldap_mailboxes"}"
418a4ed7 302 ];
8415083e
IB
303 dovecot_destination_recipient_limit = "1";
304 virtual_transport = "dovecot";
a929614f 305
8415083e 306 ### Relay domains
ab8f306d 307 relay_domains = lib.flatten (lib.attrsets.mapAttrsToList (n: v: v.domains or []) config.myEnv.mail.postfix.backup_domains);
8415083e
IB
308 relay_recipient_maps = lib.flatten (lib.attrsets.mapAttrsToList (n: v:
309 lib.imap1 (i: m: "${m.type}:/etc/postfix/relay_${n}_${toString i}") v.recipient_maps
ab8f306d 310 ) config.myEnv.mail.postfix.backup_domains);
8415083e 311 smtpd_relay_restrictions = [
8415083e
IB
312 "defer_unauth_destination"
313 ] ++ lib.flatten (lib.attrsets.mapAttrsToList (n: v:
314 if lib.attrsets.hasAttr "relay_restrictions" v
315 then [ "check_recipient_access hash:/etc/postfix/recipient_access_${n}" ]
316 else []
ab8f306d 317 ) config.myEnv.mail.postfix.backup_domains);
a929614f 318
8415083e
IB
319 ### Additional smtpd configuration
320 smtpd_tls_received_header = "yes";
321 smtpd_tls_loglevel = "1";
a929614f 322
8415083e
IB
323 ### Email sending configuration
324 smtp_tls_security_level = "may";
325 smtp_tls_loglevel = "1";
a929614f 326
8415083e 327 ### Force ip bind for smtp
619e4f46
IB
328 smtp_bind_address = config.hostEnv.ips.main.ip4;
329 smtp_bind_address6 = builtins.head config.hostEnv.ips.main.ip6;
a929614f 330
87a8bffd
IB
331 # Use some relays when authorized senders are not myself
332 smtp_sasl_mechanism_filter = "plain,login"; # GSSAPI Not correctly supported by postfix
333 smtp_sasl_auth_enable = "yes";
334 smtp_sasl_password_maps =
335 "mysql:${config.secrets.fullPaths."postfix/mysql_sender_relays_creds"}";
336 smtp_sasl_security_options = "noanonymous";
337 smtp_sender_dependent_authentication = "yes";
338 sender_dependent_relayhost_maps =
339 "mysql:${config.secrets.fullPaths."postfix/mysql_sender_relays_hosts"}";
a929614f 340
8415083e
IB
341 ### opendkim, opendmarc, openarc milters
342 non_smtpd_milters = [
343 "unix:${config.myServices.mail.milters.sockets.opendkim}"
8415083e
IB
344 ];
345 smtpd_milters = [
346 "unix:${config.myServices.mail.milters.sockets.opendkim}"
8415083e 347 "unix:${config.myServices.mail.milters.sockets.openarc}"
619e4f46 348 "unix:${config.myServices.mail.milters.sockets.opendmarc}"
a929614f 349 ];
5153eb54
IB
350
351 smtp_use_tls = true;
352 smtpd_use_tls = true;
353 smtpd_tls_chain_files = builtins.concatStringsSep "," [ "/var/lib/acme/mail/full.pem" "/var/lib/acme/mail-rsa/full.pem" ];
6ea94404
IB
354
355 maximal_queue_lifetime = "6w";
356 bounce_queue_lifetime = "6w";
a929614f 357 };
8415083e
IB
358 enable = true;
359 enableSmtp = true;
360 enableSubmission = true;
361 submissionOptions = {
87a8bffd
IB
362 # Don’t use "long form", only commas (cf
363 # http://www.postfix.org/master.5.html long form is not handled
364 # well by the submission function)
8415083e
IB
365 smtpd_tls_security_level = "encrypt";
366 smtpd_sasl_auth_enable = "yes";
367 smtpd_tls_auth_only = "yes";
368 smtpd_sasl_tls_security_options = "noanonymous";
369 smtpd_sasl_type = "dovecot";
370 smtpd_sasl_path = "private/auth";
371 smtpd_reject_unlisted_recipient = "no";
372 smtpd_client_restrictions = "permit_sasl_authenticated,reject";
619e4f46 373 smtpd_relay_restrictions = "permit_sasl_authenticated,reject";
8415083e
IB
374 # Refuse to send e-mails with a From that is not handled
375 smtpd_sender_restrictions =
376 "reject_sender_login_mismatch,reject_unlisted_sender,permit_sasl_authenticated,reject";
87a8bffd
IB
377 smtpd_sender_login_maps = builtins.concatStringsSep "," [
378 "hash:/etc/postfix/host_sender_login"
379 "mysql:${config.secrets.fullPaths."postfix/mysql_sender_relays_maps"}"
380 "mysql:${config.secrets.fullPaths."postfix/mysql_sender_login_maps"}"
381 ];
8415083e
IB
382 smtpd_recipient_restrictions = "permit_sasl_authenticated,reject";
383 milter_macro_daemon_name = "ORIGINATING";
45730653
IB
384 smtpd_milters = builtins.concatStringsSep "," [
385 # FIXME: put it back when opensmtpd is upgraded and able to
386 # rewrite the from header
387 #"unix:/run/milter_verify_from/verify_from.sock"
388 "unix:${config.myServices.mail.milters.sockets.opendkim}"
389 ];
8415083e 390 };
8415083e
IB
391 destination = ["localhost"];
392 # This needs to reverse DNS
619e4f46 393 hostname = config.hostEnv.fqdn;
8415083e 394 setSendmail = true;
8415083e
IB
395 recipientDelimiter = "+";
396 masterConfig = {
397 submissions = {
398 type = "inet";
399 private = false;
400 command = "smtpd";
401 args = ["-o" "smtpd_tls_wrappermode=yes" ] ++ (let
402 mkKeyVal = opt: val: [ "-o" (opt + "=" + val) ];
403 in lib.concatLists (lib.mapAttrsToList mkKeyVal config.services.postfix.submissionOptions)
404 );
405 };
406 dovecot = {
407 type = "unix";
408 privileged = true;
409 chroot = false;
410 command = "pipe";
411 args = let
412 # rspamd could be used as a milter, but then it cannot apply
413 # its checks "per user" (milter is not yet dispatched to
414 # users), so we wrap dovecot-lda inside rspamc per recipient
415 # here.
dc6d3af9
IB
416 rspamc_dovecot = pkgs.writeScriptBin "rspamc_dovecot" ''
417 #! ${pkgs.stdenv.shell}
418 sender="$1"
419 original_recipient="$2"
420 user="$3"
421
422 ${pkgs.coreutils}/bin/cat - | \
423 (${pkgs.rspamd}/bin/rspamc -h ${config.myServices.mail.rspamd.sockets.worker-controller} -c bayes -d "$user" --mime || true) | \
424 ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -f "$sender" -a "$original_recipient" -d "$user"
425 '';
8415083e 426 in [
fb7611c1 427 "flags=ODRhu" "user=vhost:vhost"
dc6d3af9 428 "argv=${rspamc_dovecot}/bin/rspamc_dovecot \${sender} \${original_recipient} \${user}@\${nexthop}"
8415083e
IB
429 ];
430 };
431 };
a929614f 432 };
5400b9b6 433 security.acme.certs."mail" = {
8415083e
IB
434 postRun = ''
435 systemctl restart postfix.service
436 '';
437 extraDomains = {
514f9ec3
IB
438 "smtp.immae.eu" = null;
439 };
440 };
441 security.acme.certs."mail-rsa" = {
442 postRun = ''
443 systemctl restart postfix.service
444 '';
445 extraDomains = {
8415083e
IB
446 "smtp.immae.eu" = null;
447 };
a929614f 448 };
71a2425e
IB
449 system.activationScripts.testmail = {
450 deps = [ "users" ];
451 text = let
452 allCfg = config.myEnv.monitoring.email_check;
453 cfg = allCfg.eldiron;
454 reverseTargets = builtins.attrNames (lib.attrsets.filterAttrs (k: v: builtins.elem "eldiron" v.targets) allCfg);
455 to_email = cfg': host':
456 let sep = if lib.strings.hasInfix "+" cfg'.mail_address then "_" else "+";
457 in "${cfg'.mail_address}${sep}${host'}@${cfg'.mail_domain}";
458 mails_to_receive = builtins.concatStringsSep " " (map (to_email cfg) reverseTargets);
459 in ''
31d99b75 460 install -m 0555 -o postfixscripts -g keys -d /var/lib/naemon/checks/email
71a2425e
IB
461 for f in ${mails_to_receive}; do
462 if [ ! -f /var/lib/naemon/checks/email/$f ]; then
31d99b75 463 install -m 0644 -o postfixscripts -g keys /dev/null -T /var/lib/naemon/checks/email/$f
71a2425e
IB
464 touch -m -d @0 /var/lib/naemon/checks/email/$f
465 fi
466 done
467 '';
468 };
850adcf4 469 systemd.services.postfix.serviceConfig.Slice = "mail.slice";
a929614f
IB
470 };
471}