2016-07-25 15:48:40 +00:00
|
|
|
# nixos-mailserver: a simple mail server
|
2018-01-29 09:34:27 +00:00
|
|
|
# Copyright (C) 2016-2018 Robin Raymond
|
2016-07-25 15:48:40 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
2017-09-03 09:13:34 +00:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
2017-12-22 15:08:42 +00:00
|
|
|
with (import ./common.nix { inherit config lib; });
|
2017-08-12 09:27:19 +00:00
|
|
|
|
2017-08-12 14:14:16 +00:00
|
|
|
let
|
2017-11-09 21:13:27 +00:00
|
|
|
inherit (lib.strings) concatStringsSep;
|
2017-09-03 09:13:34 +00:00
|
|
|
cfg = config.mailserver;
|
|
|
|
|
2017-08-12 14:14:16 +00:00
|
|
|
# valiases_postfix :: [ String ]
|
2017-11-21 10:18:07 +00:00
|
|
|
valiases_postfix = lib.flatten (lib.mapAttrsToList
|
|
|
|
(name: value:
|
|
|
|
let to = name;
|
2017-12-20 00:24:57 +00:00
|
|
|
in map (from: "${from} ${to}") (value.aliases ++ lib.singleton name))
|
2017-11-21 10:18:07 +00:00
|
|
|
cfg.loginAccounts);
|
|
|
|
|
2017-12-18 11:26:54 +00:00
|
|
|
# catchAllPostfix :: [ String ]
|
|
|
|
catchAllPostfix = lib.flatten (lib.mapAttrsToList
|
|
|
|
(name: value:
|
|
|
|
let to = name;
|
|
|
|
in map (from: "@${from} ${to}") value.catchAll)
|
|
|
|
cfg.loginAccounts);
|
|
|
|
|
2017-11-21 10:18:07 +00:00
|
|
|
# extra_valiases_postfix :: [ String ]
|
2018-02-23 15:48:13 +00:00
|
|
|
extra_valiases_postfix =
|
2017-11-21 10:52:16 +00:00
|
|
|
(map
|
|
|
|
(from:
|
|
|
|
let to = cfg.extraVirtualAliases.${from};
|
2018-04-02 13:42:58 +00:00
|
|
|
aliasList = (l: let aliasStr = builtins.foldl' (x: y: x + y + ", ") "" l;
|
|
|
|
in builtins.substring 0 (builtins.stringLength aliasStr - 2) aliasStr);
|
|
|
|
in if (builtins.isList to) then "${from} " + (aliasList to)
|
|
|
|
else "${from} ${to}")
|
2017-11-21 10:52:16 +00:00
|
|
|
(builtins.attrNames cfg.extraVirtualAliases));
|
2017-08-12 14:14:16 +00:00
|
|
|
|
2017-11-21 10:35:52 +00:00
|
|
|
# all_valiases_postfix :: [ String ]
|
|
|
|
all_valiases_postfix = valiases_postfix ++ extra_valiases_postfix;
|
2017-11-21 10:18:07 +00:00
|
|
|
|
2017-08-12 14:14:16 +00:00
|
|
|
# valiases_file :: Path
|
2017-12-18 11:26:54 +00:00
|
|
|
valiases_file = builtins.toFile "valias"
|
|
|
|
(lib.concatStringsSep "\n" (all_valiases_postfix ++
|
|
|
|
catchAllPostfix));
|
2017-08-12 14:14:16 +00:00
|
|
|
|
2018-11-10 13:29:16 +00:00
|
|
|
reject_senders_postfix = (map
|
|
|
|
(sender:
|
|
|
|
"${sender} REJECT")
|
|
|
|
(cfg.rejectSender));
|
|
|
|
reject_senders_file = builtins.toFile "reject_senders" (lib.concatStringsSep "\n" (reject_senders_postfix)) ;
|
|
|
|
|
|
|
|
|
2018-04-14 23:37:30 +00:00
|
|
|
reject_recipients_postfix = (map
|
|
|
|
(recipient:
|
|
|
|
"${recipient} REJECT")
|
|
|
|
(cfg.rejectRecipients));
|
|
|
|
# rejectRecipients :: [ Path ]
|
|
|
|
reject_recipients_file = builtins.toFile "reject_recipients" (lib.concatStringsSep "\n" (reject_recipients_postfix)) ;
|
|
|
|
|
2017-08-12 14:14:16 +00:00
|
|
|
# vhosts_file :: Path
|
2017-11-11 09:44:45 +00:00
|
|
|
vhosts_file = builtins.toFile "vhosts" (concatStringsSep "\n" cfg.domains);
|
2017-08-12 16:27:22 +00:00
|
|
|
|
2017-08-13 12:05:40 +00:00
|
|
|
# vaccounts_file :: Path
|
|
|
|
# see
|
|
|
|
# https://blog.grimneko.de/2011/12/24/a-bunch-of-tips-for-improving-your-postfix-setup/
|
|
|
|
# for details on how this file looks. By using the same file as valiases,
|
2018-11-23 14:29:23 +00:00
|
|
|
# every alias is owned (uniquely) by its user.
|
2018-03-03 10:43:42 +00:00
|
|
|
# The user's own address is already in all_valiases_postfix.
|
|
|
|
vaccounts_file = builtins.toFile "vaccounts" (lib.concatStringsSep "\n" all_valiases_postfix);
|
2017-11-09 21:13:27 +00:00
|
|
|
|
2018-04-08 18:58:30 +00:00
|
|
|
submissionHeaderCleanupRules = pkgs.writeText "submission_header_cleanup_rules" (''
|
2017-09-23 07:56:09 +00:00
|
|
|
# Removes sensitive headers from mails handed in via the submission port.
|
|
|
|
# See https://thomas-leister.de/mailserver-debian-stretch/
|
|
|
|
# Uses "pcre" style regex.
|
2017-09-20 06:38:40 +00:00
|
|
|
|
|
|
|
/^Received:/ IGNORE
|
|
|
|
/^X-Originating-IP:/ IGNORE
|
|
|
|
/^X-Mailer:/ IGNORE
|
|
|
|
/^User-Agent:/ IGNORE
|
|
|
|
/^X-Enigmail:/ IGNORE
|
2018-04-08 18:58:30 +00:00
|
|
|
'' + lib.optionalString cfg.rewriteMessageId ''
|
|
|
|
|
|
|
|
# Replaces the user submitted hostname with the server's FQDN to hide the
|
|
|
|
# user's host or network.
|
|
|
|
|
|
|
|
/^Message-ID:\s+<(.*?)@.*?>/ REPLACE Message-ID: <$1@${cfg.fqdn}>
|
|
|
|
'');
|
2018-05-04 16:17:51 +00:00
|
|
|
|
|
|
|
inetSocket = addr: port: "inet:[${toString port}@${addr}]";
|
|
|
|
unixSocket = sock: "unix:${sock}";
|
|
|
|
|
|
|
|
smtpdMilters =
|
|
|
|
(lib.optional cfg.dkimSigning "unix:/run/opendkim/opendkim.sock")
|
2018-05-13 01:35:03 +00:00
|
|
|
++ [ "unix:/run/rspamd/rspamd-milter.sock" ];
|
2018-05-12 16:02:37 +00:00
|
|
|
|
2018-05-17 00:01:49 +00:00
|
|
|
policyd-spf = pkgs.writeText "policyd-spf.conf" (
|
|
|
|
cfg.policydSPFExtraConfig
|
|
|
|
+ (lib.optionalString cfg.debug ''
|
2018-05-12 16:02:37 +00:00
|
|
|
debugLevel = 4
|
|
|
|
''));
|
2018-11-10 13:29:16 +00:00
|
|
|
|
|
|
|
mappedFile = name: "hash:/var/lib/postfix/conf/${name}";
|
2017-08-12 14:14:16 +00:00
|
|
|
in
|
2016-07-25 15:48:40 +00:00
|
|
|
{
|
2017-09-03 09:13:34 +00:00
|
|
|
config = with cfg; lib.mkIf enable {
|
2017-08-12 14:14:16 +00:00
|
|
|
|
2017-09-03 09:13:34 +00:00
|
|
|
services.postfix = {
|
|
|
|
enable = true;
|
2017-11-26 10:56:34 +00:00
|
|
|
hostname = "${fqdn}";
|
2017-09-03 09:13:34 +00:00
|
|
|
networksStyle = "host";
|
|
|
|
mapFiles."valias" = valiases_file;
|
2017-11-09 21:13:27 +00:00
|
|
|
mapFiles."vaccounts" = vaccounts_file;
|
2018-11-10 13:29:16 +00:00
|
|
|
mapFiles."reject_senders" = reject_senders_file;
|
2018-04-14 23:37:30 +00:00
|
|
|
mapFiles."reject_recipients" = reject_recipients_file;
|
2017-09-03 09:13:34 +00:00
|
|
|
sslCert = certificatePath;
|
|
|
|
sslKey = keyPath;
|
|
|
|
enableSubmission = true;
|
2019-05-03 11:25:23 +00:00
|
|
|
virtual =
|
|
|
|
(lib.concatStringsSep "\n" (all_valiases_postfix ++ catchAllPostfix));
|
2017-08-12 14:14:16 +00:00
|
|
|
|
2017-11-09 21:13:27 +00:00
|
|
|
extraConfig =
|
2017-09-03 09:13:34 +00:00
|
|
|
''
|
2017-09-12 20:47:13 +00:00
|
|
|
# Extra Config
|
2018-03-10 16:13:30 +00:00
|
|
|
mydestination =
|
2017-08-12 14:14:16 +00:00
|
|
|
|
2017-11-11 13:19:05 +00:00
|
|
|
smtpd_banner = ${fqdn} ESMTP NO UCE
|
2017-09-03 09:13:34 +00:00
|
|
|
disable_vrfy_command = yes
|
2018-11-23 14:29:23 +00:00
|
|
|
message_size_limit = ${builtins.toString cfg.messageSizeLimit}
|
2017-08-12 14:14:16 +00:00
|
|
|
|
2017-09-03 09:13:34 +00:00
|
|
|
# virtual mail system
|
|
|
|
virtual_uid_maps = static:5000
|
|
|
|
virtual_gid_maps = static:5000
|
|
|
|
virtual_mailbox_base = ${mailDirectory}
|
|
|
|
virtual_mailbox_domains = ${vhosts_file}
|
2018-11-10 13:29:16 +00:00
|
|
|
virtual_mailbox_maps = ${mappedFile "valias"}
|
2018-05-04 14:52:58 +00:00
|
|
|
virtual_transport = lmtp:unix:/run/dovecot2/dovecot-lmtp
|
2017-08-12 14:14:16 +00:00
|
|
|
|
2017-09-03 09:13:34 +00:00
|
|
|
# sasl with dovecot
|
|
|
|
smtpd_sasl_type = dovecot
|
2018-05-04 14:52:58 +00:00
|
|
|
smtpd_sasl_path = /run/dovecot2/auth
|
2017-09-03 09:13:34 +00:00
|
|
|
smtpd_sasl_auth_enable = yes
|
|
|
|
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
|
2017-09-20 07:26:42 +00:00
|
|
|
|
2018-05-12 16:02:37 +00:00
|
|
|
policy-spf_time_limit = 3600s
|
|
|
|
|
2018-11-10 13:29:16 +00:00
|
|
|
# reject selected senders
|
|
|
|
smtpd_sender_restrictions = check_sender_access ${mappedFile "reject_senders"}
|
|
|
|
|
2018-05-12 16:02:37 +00:00
|
|
|
# quota and spf checking
|
|
|
|
smtpd_recipient_restrictions =
|
2018-11-10 13:29:16 +00:00
|
|
|
check_recipient_access ${mappedFile "reject_recipients"},
|
2018-05-12 16:02:37 +00:00
|
|
|
check_policy_service inet:localhost:12340,
|
|
|
|
check_policy_service unix:private/policy-spf
|
2017-12-22 15:58:35 +00:00
|
|
|
|
2017-09-19 21:54:40 +00:00
|
|
|
# TLS settings, inspired by https://github.com/jeaye/nix-files
|
2017-09-20 07:26:42 +00:00
|
|
|
# Submission by mail clients is handled in submissionOptions
|
|
|
|
smtpd_tls_security_level = may
|
2019-06-21 09:09:30 +00:00
|
|
|
|
2017-09-20 07:26:42 +00:00
|
|
|
# strong might suffice and is computationally less expensive
|
|
|
|
smtpd_tls_eecdh_grade = ultra
|
2019-06-21 09:09:30 +00:00
|
|
|
|
|
|
|
# Disable obselete protocols
|
|
|
|
smtpd_tls_protocols = TLSv1.2, TLSv1.1, !TLSv1, !SSLv2, !SSLv3
|
|
|
|
smtp_tls_protocols = TLSv1.2, TLSv1.1, !TLSv1, !SSLv2, !SSLv3
|
|
|
|
smtpd_tls_mandatory_protocols = TLSv1.2, TLSv1.1, !TLSv1, !SSLv2, !SSLv3
|
|
|
|
smtp_tls_mandatory_protocols = TLSv1.2, TLSv1.1, !TLSv1, !SSLv2, !SSLv3
|
|
|
|
|
|
|
|
smtp_tls_ciphers = high
|
|
|
|
smtpd_tls_ciphers = high
|
|
|
|
smtp_tls_mandatory_ciphers = high
|
|
|
|
smtpd_tls_mandatory_ciphers = high
|
|
|
|
|
|
|
|
# Disable deprecated ciphers
|
|
|
|
smtpd_tls_mandatory_exclude_ciphers = MD5, DES, ADH, RC4, PSD, SRP, 3DES, eNULL, aNULL
|
|
|
|
smtpd_tls_exclude_ciphers = MD5, DES, ADH, RC4, PSD, SRP, 3DES, eNULL, aNULL
|
|
|
|
smtp_tls_mandatory_exclude_ciphers = MD5, DES, ADH, RC4, PSD, SRP, 3DES, eNULL, aNULL
|
|
|
|
smtp_tls_exclude_ciphers = MD5, DES, ADH, RC4, PSD, SRP, 3DES, eNULL, aNULL
|
|
|
|
|
|
|
|
tls_preempt_cipherlist = yes
|
|
|
|
|
2017-09-20 07:26:42 +00:00
|
|
|
# Allowing AUTH on a non encrypted connection poses a security risk
|
|
|
|
smtpd_tls_auth_only = yes
|
|
|
|
# Log only a summary message on TLS handshake completion
|
|
|
|
smtpd_tls_loglevel = 1
|
|
|
|
|
2017-09-19 21:54:40 +00:00
|
|
|
# Configure a non blocking source of randomness
|
|
|
|
tls_random_source = dev:/dev/urandom
|
2018-05-04 16:17:51 +00:00
|
|
|
|
|
|
|
smtpd_milters = ${lib.concatStringsSep "," smtpdMilters}
|
|
|
|
${lib.optionalString cfg.dkimSigning "non_smtpd_milters = unix:/run/opendkim/opendkim.sock"}
|
|
|
|
milter_protocol = 6
|
|
|
|
milter_mail_macros = i {mail_addr} {client_addr} {client_name} {auth_type} {auth_authen} {auth_author} {mail_addr} {mail_host} {mail_mailer}
|
2017-09-03 09:13:34 +00:00
|
|
|
'';
|
2017-08-12 14:14:16 +00:00
|
|
|
|
2017-09-03 09:13:34 +00:00
|
|
|
submissionOptions =
|
2017-11-09 21:13:27 +00:00
|
|
|
{
|
2017-09-03 09:13:34 +00:00
|
|
|
smtpd_tls_security_level = "encrypt";
|
|
|
|
smtpd_sasl_auth_enable = "yes";
|
|
|
|
smtpd_sasl_type = "dovecot";
|
2018-05-04 14:52:58 +00:00
|
|
|
smtpd_sasl_path = "/run/dovecot2/auth";
|
2017-09-03 09:13:34 +00:00
|
|
|
smtpd_sasl_security_options = "noanonymous";
|
|
|
|
smtpd_sasl_local_domain = "$myhostname";
|
|
|
|
smtpd_client_restrictions = "permit_sasl_authenticated,reject";
|
|
|
|
smtpd_sender_login_maps = "hash:/etc/postfix/vaccounts";
|
|
|
|
smtpd_sender_restrictions = "reject_sender_login_mismatch";
|
|
|
|
smtpd_recipient_restrictions = "reject_non_fqdn_recipient,reject_unknown_recipient_domain,permit_sasl_authenticated,reject";
|
2017-09-19 22:05:01 +00:00
|
|
|
cleanup_service_name = "submission-header-cleanup";
|
2017-09-03 09:13:34 +00:00
|
|
|
};
|
2018-05-12 16:00:14 +00:00
|
|
|
masterConfig = {
|
2018-05-12 16:02:37 +00:00
|
|
|
"policy-spf" = {
|
|
|
|
type = "unix";
|
|
|
|
privileged = true;
|
|
|
|
chroot = false;
|
|
|
|
command = "spawn";
|
|
|
|
args = [ "user=nobody" "argv=${pkgs.pypolicyd-spf}/bin/policyd-spf" "${policyd-spf}"];
|
|
|
|
};
|
2018-05-12 16:00:14 +00:00
|
|
|
"submission-header-cleanup" = {
|
|
|
|
type = "unix";
|
|
|
|
private = false;
|
|
|
|
chroot = false;
|
|
|
|
maxproc = 0;
|
|
|
|
command = "cleanup";
|
|
|
|
args = ["-o" "header_checks=pcre:${submissionHeaderCleanupRules}"];
|
|
|
|
};
|
|
|
|
};
|
2017-09-03 09:13:34 +00:00
|
|
|
};
|
2017-08-13 12:05:40 +00:00
|
|
|
};
|
2016-07-25 15:48:40 +00:00
|
|
|
}
|