1 { lib, pkgs, config, nodes, ... }:
3 config = lib.mkIf config.myServices.mail.enable {
4 services.duplyBackup.profiles.mail.excludeFile = ''
9 dest = "postfix/mysql_alias_maps";
10 user = config.services.postfix.user;
11 group = config.services.postfix.group;
14 # We need to specify that option to trigger ssl connection
16 user = ${config.myEnv.mail.postfix.mysql.user}
17 password = ${config.myEnv.mail.postfix.mysql.password}
18 hosts = unix:${config.myEnv.mail.postfix.mysql.socket}
19 dbname = ${config.myEnv.mail.postfix.mysql.database}
20 query = SELECT DISTINCT destination
21 FROM forwardings_merge
23 ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s'))
28 FROM forwardings_blacklisted
31 SELECT 'devnull@immae.eu'
32 FROM forwardings_blacklisted
37 dest = "postfix/mysql_mailbox_maps";
38 user = config.services.postfix.user;
39 group = config.services.postfix.group;
42 # We need to specify that option to trigger ssl connection
44 user = ${config.myEnv.mail.postfix.mysql.user}
45 password = ${config.myEnv.mail.postfix.mysql.password}
46 hosts = unix:${config.myEnv.mail.postfix.mysql.socket}
47 dbname = ${config.myEnv.mail.postfix.mysql.database}
48 result_format = /%d/%u
49 query = SELECT DISTINCT '%s'
53 (domain = '%d' AND user = '%u' AND regex = 0)
56 AND '%d' REGEXP CONCAT('^',domain,'$')
57 AND '%u' REGEXP CONCAT('^',user,'$')
64 dest = "postfix/mysql_sender_login_maps";
65 user = config.services.postfix.user;
66 group = config.services.postfix.group;
69 # We need to specify that option to trigger ssl connection
71 user = ${config.myEnv.mail.postfix.mysql.user}
72 password = ${config.myEnv.mail.postfix.mysql.password}
73 hosts = unix:${config.myEnv.mail.postfix.mysql.socket}
74 dbname = ${config.myEnv.mail.postfix.mysql.database}
75 query = SELECT DISTINCT destination
76 FROM forwardings_merge
78 ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s'))
80 UNION SELECT '%s' AS destination
84 dest = "postfix/mysql_sender_relays_maps";
85 user = config.services.postfix.user;
86 group = config.services.postfix.group;
89 # We need to specify that option to trigger ssl connection
91 user = ${config.myEnv.mail.postfix.mysql.user}
92 password = ${config.myEnv.mail.postfix.mysql.password}
93 hosts = unix:${config.myEnv.mail.postfix.mysql.socket}
94 dbname = ${config.myEnv.mail.postfix.mysql.database}
95 # INSERT INTO sender_relays
96 # (`from`, owner, relay, login, password, regex, active)
98 # ( 'sender@otherhost.org'
99 # , 'me@mail.immae.eu'
100 # , '[otherhost.org]:587'
102 # , AES_ENCRYPT('otherhostpassword', '${config.myEnv.mail.postfix.mysql.password_encrypt}')
106 query = SELECT DISTINCT `owner`
109 ((regex = 1 AND '%s' REGEXP CONCAT('^',`from`,'$') ) OR (regex = 0 AND `from` = '%s'))
114 dest = "postfix/mysql_sender_relays_hosts";
115 user = config.services.postfix.user;
116 group = config.services.postfix.group;
117 permissions = "0440";
119 # We need to specify that option to trigger ssl connection
120 tls_ciphers = TLSv1.2
121 user = ${config.myEnv.mail.postfix.mysql.user}
122 password = ${config.myEnv.mail.postfix.mysql.password}
123 hosts = unix:${config.myEnv.mail.postfix.mysql.socket}
124 dbname = ${config.myEnv.mail.postfix.mysql.database}
126 query = SELECT DISTINCT relay
129 ((regex = 1 AND '%s' REGEXP CONCAT('^',`from`,'$') ) OR (regex = 0 AND `from` = '%s'))
134 dest = "postfix/mysql_sender_relays_creds";
135 user = config.services.postfix.user;
136 group = config.services.postfix.group;
137 permissions = "0440";
139 # We need to specify that option to trigger ssl connection
140 tls_ciphers = TLSv1.2
141 user = ${config.myEnv.mail.postfix.mysql.user}
142 password = ${config.myEnv.mail.postfix.mysql.password}
143 hosts = unix:${config.myEnv.mail.postfix.mysql.socket}
144 dbname = ${config.myEnv.mail.postfix.mysql.database}
146 query = SELECT DISTINCT CONCAT(`login`, ':', AES_DECRYPT(`password`, '${config.myEnv.mail.postfix.mysql.password_encrypt}'))
149 ((regex = 1 AND '%s' REGEXP CONCAT('^',`from`,'$') ) OR (regex = 0 AND `from` = '%s'))
154 dest = "postfix/ldap_ejabberd_users_immae_fr";
155 user = config.services.postfix.user;
156 group = config.services.postfix.group;
157 permissions = "0440";
159 server_host = ldaps://${config.myEnv.jabber.ldap.host}:636
160 search_base = ${config.myEnv.jabber.ldap.base}
161 query_filter = ${config.myEnv.jabber.postfix_user_filter}
163 bind_dn = ${config.myEnv.jabber.ldap.dn}
164 bind_pw = ${config.myEnv.jabber.ldap.password}
165 result_attribute = immaeXmppUid
166 result_format = ejabberd@localhost
172 networking.firewall.allowedTCPPorts = [ 25 465 587 ];
174 nixpkgs.overlays = [ (self: super: {
175 postfix = super.postfix.override { withMySQL = true; };
177 users.users."${config.services.postfix.user}".extraGroups = [ "keys" ];
178 services.filesWatcher.postfix = {
181 config.secrets.fullPaths."postfix/mysql_alias_maps"
182 config.secrets.fullPaths."postfix/mysql_mailbox_maps"
183 config.secrets.fullPaths."postfix/mysql_sender_login_maps"
184 config.secrets.fullPaths."postfix/ldap_ejabberd_users_immae_fr"
189 toScript = name: script: pkgs.writeScript name ''
190 #! ${pkgs.stdenv.shell}
191 mail=$(${pkgs.coreutils}/bin/cat -)
192 output=$(echo "$mail" | ${script} 2>&1)
195 if [ "$ret" != "0" ]; then
197 | ${pkgs.procmail}/bin/formail -i "X-Return-Code: $ret" \
198 | /run/wrappers/bin/sendmail -i scripts_error+${name}@mail.immae.eu
200 messageId=$(echo "$mail" | ${pkgs.procmail}/bin/formail -x "Message-Id:")
201 repeat=$(echo "$mail" | ${pkgs.procmail}/bin/formail -X "From:" -X "Received:")
203 ${pkgs.coreutils}/bin/cat <<EOF | /run/wrappers/bin/sendmail -i scripts_error+${name}@mail.immae.eu
205 To: scripts_error+${name}@mail.immae.eu
206 Subject: Log from script error
207 Content-Type: text/plain; charset="UTF-8"
208 Content-Transfer-Encoding: 8bit
209 References:$messageId
221 scripts = lib.attrsets.mapAttrs (n: v:
222 toScript n (pkgs.callPackage (builtins.fetchGit { url = v.src.url; ref = "master"; rev = v.src.rev; }) { scriptEnv = v.env; })
223 ) config.myEnv.mail.scripts // {
224 testmail = pkgs.writeScript "testmail" ''
225 #! ${pkgs.stdenv.shell}
226 ${pkgs.coreutils}/bin/touch \
227 "/var/lib/naemon/checks/email/$(${pkgs.procmail}/bin/formail -x To: | ${pkgs.coreutils}/bin/tr -d ' <>')"
230 in builtins.concatStringsSep "\n" (lib.attrsets.mapAttrsToList (n: v: ''${n}: "|${v}"'') scripts);
233 name = n: i: "relay_${n}_${toString i}";
234 pair = n: i: m: lib.attrsets.nameValuePair (name n i) (
236 then pkgs.writeText (name n i) m.content
239 pairs = n: v: lib.imap1 (i: m: pair n i m) v.recipient_maps;
240 in lib.attrsets.filterAttrs (k: v: v != null) (
241 lib.attrsets.listToAttrs (lib.flatten (
242 lib.attrsets.mapAttrsToList pairs config.myEnv.mail.postfix.backup_domains
245 relay_restrictions = lib.attrsets.filterAttrs (k: v: v != null) (
246 lib.attrsets.mapAttrs' (n: v:
247 lib.attrsets.nameValuePair "recipient_access_${n}" (
248 if lib.attrsets.hasAttr "relay_restrictions" v
249 then pkgs.writeText "recipient_access_${n}" v.relay_restrictions
252 ) config.myEnv.mail.postfix.backup_domains
256 cfg = config.myEnv.monitoring.email_check.eldiron;
257 address = "${cfg.mail_address}@${cfg.mail_domain}";
258 in pkgs.writeText "postfix-virtual" (
259 builtins.concatStringsSep "\n" (
260 ["${address} testmail@localhost"] ++
261 lib.attrsets.mapAttrsToList (
262 n: v: lib.optionalString v.external ''
263 script_${n}@mail.immae.eu ${n}@localhost, scripts@mail.immae.eu
265 ) config.myEnv.mail.scripts
270 host_sender_login = with lib.attrsets; let
271 addresses = zipAttrs (lib.flatten (mapAttrsToList
272 (n: v: (map (e: { "${e}" = "${n}@immae.eu"; }) v.emails)) config.myEnv.servers));
273 joined = builtins.concatStringsSep ",";
274 in pkgs.writeText "host-sender-login"
275 (builtins.concatStringsSep "\n" (mapAttrsToList (n: v: "${n} ${joined v}") addresses));
276 host_dummy_mailboxes = pkgs.writeText "host-virtual-mailbox"
277 (builtins.concatStringsSep "\n" (["immae-eu@immae.eu dummy"] ++ lib.attrsets.mapAttrsToList (n: v: "${n}@immae.eu dummy") nodes));
280 recipient_maps // relay_restrictions // virtual_map // sasl_access;
282 ### postfix module overrides
283 readme_directory = "${pkgs.postfix}/share/postfix/doc";
284 smtp_tls_CAfile = lib.mkForce "";
285 smtp_tls_cert_file = lib.mkForce "";
286 smtp_tls_key_file = lib.mkForce "";
288 message_size_limit = "1073741824"; # Don't put 0 here, it's not equivalent to "unlimited"
289 mailbox_size_limit = "1073741825"; # Workaround, local delivered mails should all go through scripts
290 alias_database = "\$alias_maps";
292 ### Virtual mailboxes config
293 virtual_alias_maps = "hash:/etc/postfix/virtual mysql:${config.secrets.fullPaths."postfix/mysql_alias_maps"} ldap:${config.secrets.fullPaths."postfix/ldap_ejabberd_users_immae_fr"}";
294 virtual_mailbox_domains = config.myEnv.mail.postfix.additional_mailbox_domains
295 ++ lib.remove null (lib.flatten (map
298 then "${e.domain}${lib.optionalString (e.domain != "") "."}${zone.name}"
301 (zone.withEmail or [])
303 config.myEnv.dns.masterZones
305 virtual_mailbox_maps = "hash:/etc/postfix/host_dummy_mailboxes mysql:${config.secrets.fullPaths."postfix/mysql_mailbox_maps"}";
306 dovecot_destination_recipient_limit = "1";
307 virtual_transport = "dovecot";
310 relay_domains = lib.flatten (lib.attrsets.mapAttrsToList (n: v: v.domains or []) config.myEnv.mail.postfix.backup_domains);
311 relay_recipient_maps = lib.flatten (lib.attrsets.mapAttrsToList (n: v:
312 lib.imap1 (i: m: "${m.type}:/etc/postfix/relay_${n}_${toString i}") v.recipient_maps
313 ) config.myEnv.mail.postfix.backup_domains);
314 smtpd_relay_restrictions = [
315 "defer_unauth_destination"
316 ] ++ lib.flatten (lib.attrsets.mapAttrsToList (n: v:
317 if lib.attrsets.hasAttr "relay_restrictions" v
318 then [ "check_recipient_access hash:/etc/postfix/recipient_access_${n}" ]
320 ) config.myEnv.mail.postfix.backup_domains);
322 ### Additional smtpd configuration
323 smtpd_tls_received_header = "yes";
324 smtpd_tls_loglevel = "1";
326 ### Email sending configuration
327 smtp_tls_security_level = "may";
328 smtp_tls_loglevel = "1";
330 ### Force ip bind for smtp
331 smtp_bind_address = config.hostEnv.ips.main.ip4;
332 smtp_bind_address6 = builtins.head config.hostEnv.ips.main.ip6;
334 # Use some relays when authorized senders are not myself
335 smtp_sasl_mechanism_filter = "plain,login"; # GSSAPI Not correctly supported by postfix
336 smtp_sasl_auth_enable = "yes";
337 smtp_sasl_password_maps =
338 "mysql:${config.secrets.fullPaths."postfix/mysql_sender_relays_creds"}";
339 smtp_sasl_security_options = "noanonymous";
340 smtp_sender_dependent_authentication = "yes";
341 sender_dependent_relayhost_maps =
342 "mysql:${config.secrets.fullPaths."postfix/mysql_sender_relays_hosts"}";
344 ### opendkim, opendmarc, openarc milters
345 non_smtpd_milters = [
346 "unix:${config.myServices.mail.milters.sockets.opendkim}"
349 "unix:${config.myServices.mail.milters.sockets.opendkim}"
350 "unix:${config.myServices.mail.milters.sockets.openarc}"
351 "unix:${config.myServices.mail.milters.sockets.opendmarc}"
356 enableSubmission = true;
357 submissionOptions = {
358 # Don’t use "long form", only commas (cf
359 # http://www.postfix.org/master.5.html long form is not handled
360 # well by the submission function)
361 smtpd_tls_security_level = "encrypt";
362 smtpd_sasl_auth_enable = "yes";
363 smtpd_tls_auth_only = "yes";
364 smtpd_sasl_tls_security_options = "noanonymous";
365 smtpd_sasl_type = "dovecot";
366 smtpd_sasl_path = "private/auth";
367 smtpd_reject_unlisted_recipient = "no";
368 smtpd_client_restrictions = "permit_sasl_authenticated,reject";
369 smtpd_relay_restrictions = "permit_sasl_authenticated,reject";
370 # Refuse to send e-mails with a From that is not handled
371 smtpd_sender_restrictions =
372 "reject_sender_login_mismatch,reject_unlisted_sender,permit_sasl_authenticated,reject";
373 smtpd_sender_login_maps = builtins.concatStringsSep "," [
374 "hash:/etc/postfix/host_sender_login"
375 "mysql:${config.secrets.fullPaths."postfix/mysql_sender_relays_maps"}"
376 "mysql:${config.secrets.fullPaths."postfix/mysql_sender_login_maps"}"
378 smtpd_recipient_restrictions = "permit_sasl_authenticated,reject";
379 milter_macro_daemon_name = "ORIGINATING";
380 smtpd_milters = builtins.concatStringsSep "," [
381 # FIXME: put it back when opensmtpd is upgraded and able to
382 # rewrite the from header
383 #"unix:/run/milter_verify_from/verify_from.sock"
384 "unix:${config.myServices.mail.milters.sockets.opendkim}"
387 destination = ["localhost"];
388 # This needs to reverse DNS
389 hostname = config.hostEnv.fqdn;
391 sslCert = "/var/lib/acme/mail/fullchain.pem";
392 sslKey = "/var/lib/acme/mail/key.pem";
393 recipientDelimiter = "+";
399 args = ["-o" "smtpd_tls_wrappermode=yes" ] ++ (let
400 mkKeyVal = opt: val: [ "-o" (opt + "=" + val) ];
401 in lib.concatLists (lib.mapAttrsToList mkKeyVal config.services.postfix.submissionOptions)
410 # rspamd could be used as a milter, but then it cannot apply
411 # its checks "per user" (milter is not yet dispatched to
412 # users), so we wrap dovecot-lda inside rspamc per recipient
414 rspamc_dovecot = pkgs.writeScriptBin "rspamc_dovecot" ''
415 #! ${pkgs.stdenv.shell}
417 original_recipient="$2"
420 ${pkgs.coreutils}/bin/cat - | \
421 (${pkgs.rspamd}/bin/rspamc -h ${config.myServices.mail.rspamd.sockets.worker-controller} -c bayes -d "$user" --mime || true) | \
422 ${pkgs.dovecot}/libexec/dovecot/dovecot-lda -f "$sender" -a "$original_recipient" -d "$user"
425 "flags=DRhu" "user=vhost:vhost"
426 "argv=${rspamc_dovecot}/bin/rspamc_dovecot \${sender} \${original_recipient} \${user}@\${nexthop}"
431 security.acme2.certs."mail" = {
433 systemctl restart postfix.service
436 "smtp.immae.eu" = null;
439 system.activationScripts.testmail = {
442 allCfg = config.myEnv.monitoring.email_check;
443 cfg = allCfg.eldiron;
444 reverseTargets = builtins.attrNames (lib.attrsets.filterAttrs (k: v: builtins.elem "eldiron" v.targets) allCfg);
445 to_email = cfg': host':
446 let sep = if lib.strings.hasInfix "+" cfg'.mail_address then "_" else "+";
447 in "${cfg'.mail_address}${sep}${host'}@${cfg'.mail_domain}";
448 mails_to_receive = builtins.concatStringsSep " " (map (to_email cfg) reverseTargets);
450 install -m 0555 -o nobody -g nogroup -d /var/lib/naemon/checks/email
451 for f in ${mails_to_receive}; do
452 if [ ! -f /var/lib/naemon/checks/email/$f ]; then
453 install -m 0644 -o nobody -g nogroup /dev/null -T /var/lib/naemon/checks/email/$f
454 touch -m -d @0 /var/lib/naemon/checks/email/$f