X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=modules%2Fprivate%2Fmail%2Fpostfix.nix;h=f8f86f6fea521b87be331b5d00b0a9e875ea21de;hb=ab8f306d7c2c49b8116e1af7b355ed2384617ed9;hp=dfe6129af9ef847db361a819a69ec8b96f833388;hpb=afcc5de071dfffdc507995d1845372ba40dc1dc2;p=perso%2FImmae%2FConfig%2FNix.git diff --git a/modules/private/mail/postfix.nix b/modules/private/mail/postfix.nix index dfe6129..f8f86f6 100644 --- a/modules/private/mail/postfix.nix +++ b/modules/private/mail/postfix.nix @@ -1,234 +1,327 @@ -{ lib, pkgs, config, myconfig, ... }: +{ lib, pkgs, config, ... }: { - config.secrets.keys = [ - { - dest = "postfix/mysql_alias_maps"; - user = config.services.postfix.user; - group = config.services.postfix.group; - permissions = "0440"; - text = '' - # We need to specify that option to trigger ssl connection - tls_ciphers = TLSv1.2 - user = ${myconfig.env.mail.postfix.mysql.user} - password = ${myconfig.env.mail.postfix.mysql.password} - hosts = unix:${myconfig.env.mail.postfix.mysql.socket} - dbname = ${myconfig.env.mail.postfix.mysql.database} - query = SELECT DISTINCT destination - FROM forwardings_merge - WHERE - ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s')) - AND active = 1 - AND '%s' NOT IN - ( - SELECT source + config = lib.mkIf config.myServices.mail.enable { + services.duplyBackup.profiles.mail.excludeFile = '' + + /var/lib/postfix + ''; + secrets.keys = [ + { + dest = "postfix/mysql_alias_maps"; + user = config.services.postfix.user; + group = config.services.postfix.group; + permissions = "0440"; + text = '' + # We need to specify that option to trigger ssl connection + tls_ciphers = TLSv1.2 + user = ${config.myEnv.mail.postfix.mysql.user} + password = ${config.myEnv.mail.postfix.mysql.password} + hosts = unix:${config.myEnv.mail.postfix.mysql.socket} + dbname = ${config.myEnv.mail.postfix.mysql.database} + query = SELECT DISTINCT destination + FROM forwardings_merge + WHERE + ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s')) + AND active = 1 + AND '%s' NOT IN + ( + SELECT source + FROM forwardings_blacklisted + WHERE source = '%s' + ) UNION + SELECT 'devnull@immae.eu' FROM forwardings_blacklisted WHERE source = '%s' - ) UNION - SELECT 'devnull@immae.eu' - FROM forwardings_blacklisted - WHERE source = '%s' - ''; - } - { - dest = "postfix/mysql_mailbox_maps"; - user = config.services.postfix.user; - group = config.services.postfix.group; - permissions = "0440"; - text = '' - # We need to specify that option to trigger ssl connection - tls_ciphers = TLSv1.2 - user = ${myconfig.env.mail.postfix.mysql.user} - password = ${myconfig.env.mail.postfix.mysql.password} - hosts = unix:${myconfig.env.mail.postfix.mysql.socket} - dbname = ${myconfig.env.mail.postfix.mysql.database} - result_format = /%d/%u - query = SELECT DISTINCT '%s' - FROM mailboxes - WHERE active = 1 - AND ( - (domain = '%d' AND user = '%u' AND regex = 0) - OR ( - regex = 1 - AND '%d' REGEXP CONCAT('^',domain,'$') - AND '%u' REGEXP CONCAT('^',user,'$') + ''; + } + { + dest = "postfix/mysql_mailbox_maps"; + user = config.services.postfix.user; + group = config.services.postfix.group; + permissions = "0440"; + text = '' + # We need to specify that option to trigger ssl connection + tls_ciphers = TLSv1.2 + user = ${config.myEnv.mail.postfix.mysql.user} + password = ${config.myEnv.mail.postfix.mysql.password} + hosts = unix:${config.myEnv.mail.postfix.mysql.socket} + dbname = ${config.myEnv.mail.postfix.mysql.database} + result_format = /%d/%u + query = SELECT DISTINCT '%s' + FROM mailboxes + WHERE active = 1 + AND ( + (domain = '%d' AND user = '%u' AND regex = 0) + OR ( + regex = 1 + AND '%d' REGEXP CONCAT('^',domain,'$') + AND '%u' REGEXP CONCAT('^',user,'$') + ) ) - ) - LIMIT 1 - ''; - } - { - dest = "postfix/mysql_sender_login_maps"; - user = config.services.postfix.user; - group = config.services.postfix.group; - permissions = "0440"; - text = '' - # We need to specify that option to trigger ssl connection - tls_ciphers = TLSv1.2 - user = ${myconfig.env.mail.postfix.mysql.user} - password = ${myconfig.env.mail.postfix.mysql.password} - hosts = unix:${myconfig.env.mail.postfix.mysql.socket} - dbname = ${myconfig.env.mail.postfix.mysql.database} - query = SELECT DISTINCT destination - FROM forwardings_merge - WHERE - ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s')) - AND active = 1 + LIMIT 1 ''; - } - ]; + } + { + dest = "postfix/mysql_sender_login_maps"; + user = config.services.postfix.user; + group = config.services.postfix.group; + permissions = "0440"; + text = '' + # We need to specify that option to trigger ssl connection + tls_ciphers = TLSv1.2 + user = ${config.myEnv.mail.postfix.mysql.user} + password = ${config.myEnv.mail.postfix.mysql.password} + hosts = unix:${config.myEnv.mail.postfix.mysql.socket} + dbname = ${config.myEnv.mail.postfix.mysql.database} + query = SELECT DISTINCT destination + FROM forwardings_merge + WHERE + ((regex = 1 AND '%s' REGEXP CONCAT('^',source,'$') ) OR (regex = 0 AND source = '%s')) + AND active = 1 + UNION SELECT '%s' AS destination + ''; + } + ]; - config.networking.firewall.allowedTCPPorts = [ 25 587 ]; + networking.firewall.allowedTCPPorts = [ 25 465 587 ]; - config.nixpkgs.overlays = [ (self: super: { - postfix = super.postfix.override { withMySQL = true; }; - }) ]; - config.users.users."${config.services.postfix.user}".extraGroups = [ "keys" ]; - config.services.filesWatcher.postfix = { - restart = true; - paths = [ - config.secrets.fullPaths."postfix/mysql_alias_maps" - config.secrets.fullPaths."postfix/mysql_mailbox_maps" - config.secrets.fullPaths."postfix/mysql_sender_login_maps" - ]; - }; - config.services.postfix = { - mapFiles = let - name = n: i: "relay_${n}_${toString i}"; - pair = n: i: m: lib.attrsets.nameValuePair (name n i) ( - if m.type == "hash" - then pkgs.writeText (name n i) m.content - else null - ); - pairs = n: v: lib.imap1 (i: m: pair n i m) v.recipient_maps; - in - lib.attrsets.filterAttrs (k: v: v != null) ( - lib.attrsets.listToAttrs (lib.flatten ( - lib.attrsets.mapAttrsToList pairs myconfig.env.mail.postfix.backup_domains - )) - ); - config = { - ### postfix module overrides - readme_directory = "${pkgs.postfix}/share/postfix/doc"; - smtp_tls_CAfile = lib.mkForce ""; - smtp_tls_cert_file = lib.mkForce ""; - smtp_tls_key_file = lib.mkForce ""; + nixpkgs.overlays = [ (self: super: { + postfix = super.postfix.override { withMySQL = true; }; + }) ]; + users.users."${config.services.postfix.user}".extraGroups = [ "keys" ]; + services.filesWatcher.postfix = { + restart = true; + paths = [ + config.secrets.fullPaths."postfix/mysql_alias_maps" + config.secrets.fullPaths."postfix/mysql_mailbox_maps" + config.secrets.fullPaths."postfix/mysql_sender_login_maps" + ]; + }; + services.postfix = { + extraAliases = let + toScript = name: script: pkgs.writeScript name '' + #! ${pkgs.stdenv.shell} + mail=$(${pkgs.coreutils}/bin/cat -) + output=$(echo "$mail" | ${script} 2>&1) + ret=$? + + if [ "$ret" != "0" ]; then + echo "$mail" \ + | ${pkgs.procmail}/bin/formail -i "X-Return-Code: $ret" \ + | /run/wrappers/bin/sendmail -i scripts_error+${name}@mail.immae.eu - message_size_limit = "1073741824"; # Don't put 0 here, it's not equivalent to "unlimited" - alias_database = "\$alias_maps"; + messageId=$(echo "$mail" | ${pkgs.procmail}/bin/formail -x "Message-Id:") + repeat=$(echo "$mail" | ${pkgs.procmail}/bin/formail -X "From:" -X "Received:") - ### Virtual mailboxes config - virtual_alias_maps = "mysql:${config.secrets.fullPaths."postfix/mysql_alias_maps"}"; - virtual_mailbox_domains = myconfig.env.mail.postfix.additional_mailbox_domains - ++ lib.remove "localhost.immae.eu" (lib.remove null (lib.flatten (map - (zone: map - (e: if e.receive - then "${e.domain}${lib.optionalString (e.domain != "") "."}${zone.name}" + ${pkgs.coreutils}/bin/cat <