2016-07-25 17:48:40 +02:00
|
|
|
# nixos-mailserver: a simple mail server
|
2018-01-29 10:34:27 +01:00
|
|
|
# Copyright (C) 2016-2018 Robin Raymond
|
2016-07-25 17:48:40 +02: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 11:13:34 +02:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
2017-12-22 16:08:42 +01:00
|
|
|
with (import ./common.nix { inherit config lib; });
|
2017-08-12 11:27:19 +02:00
|
|
|
|
2017-08-12 16:14:16 +02:00
|
|
|
let
|
2017-11-09 22:13:27 +01:00
|
|
|
inherit (lib.strings) concatStringsSep;
|
2017-09-03 11:13:34 +02:00
|
|
|
cfg = config.mailserver;
|
|
|
|
|
2017-08-12 16:14:16 +02:00
|
|
|
# valiases_postfix :: [ String ]
|
2017-11-21 11:18:07 +01:00
|
|
|
valiases_postfix = lib.flatten (lib.mapAttrsToList
|
|
|
|
(name: value:
|
|
|
|
let to = name;
|
2017-12-20 01:24:57 +01:00
|
|
|
in map (from: "${from} ${to}") (value.aliases ++ lib.singleton name))
|
2017-11-21 11:18:07 +01:00
|
|
|
cfg.loginAccounts);
|
|
|
|
|
2017-12-18 12:26:54 +01: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 11:18:07 +01:00
|
|
|
# extra_valiases_postfix :: [ String ]
|
2017-11-21 11:52:16 +01:00
|
|
|
# TODO: Remove virtualAliases when deprecated -> removed
|
|
|
|
extra_valiases_postfix = (map
|
2017-08-30 00:58:44 +02:00
|
|
|
(from:
|
2017-09-03 11:13:34 +02:00
|
|
|
let to = cfg.virtualAliases.${from};
|
2017-11-11 10:44:45 +01:00
|
|
|
in "${from} ${to}")
|
2017-11-21 11:52:16 +01:00
|
|
|
(builtins.attrNames cfg.virtualAliases))
|
|
|
|
++
|
|
|
|
(map
|
|
|
|
(from:
|
|
|
|
let to = cfg.extraVirtualAliases.${from};
|
|
|
|
in "${from} ${to}")
|
|
|
|
(builtins.attrNames cfg.extraVirtualAliases));
|
2017-08-12 16:14:16 +02:00
|
|
|
|
2017-11-21 11:35:52 +01:00
|
|
|
# all_valiases_postfix :: [ String ]
|
|
|
|
all_valiases_postfix = valiases_postfix ++ extra_valiases_postfix;
|
2017-11-21 11:18:07 +01:00
|
|
|
|
2017-08-12 16:14:16 +02:00
|
|
|
# valiases_file :: Path
|
2017-12-18 12:26:54 +01:00
|
|
|
valiases_file = builtins.toFile "valias"
|
|
|
|
(lib.concatStringsSep "\n" (all_valiases_postfix ++
|
|
|
|
catchAllPostfix));
|
2017-08-12 16:14:16 +02:00
|
|
|
|
|
|
|
# vhosts_file :: Path
|
2017-11-11 10:44:45 +01:00
|
|
|
vhosts_file = builtins.toFile "vhosts" (concatStringsSep "\n" cfg.domains);
|
2017-08-12 18:27:22 +02:00
|
|
|
|
2017-08-13 14:05:40 +02: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-03-03 11:43:42 +01:00
|
|
|
# every alias is owned (uniquely) by its user.
|
|
|
|
# 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 22:13:27 +01:00
|
|
|
|
2017-09-20 08:38:40 +02:00
|
|
|
submissionHeaderCleanupRules = pkgs.writeText "submission_header_cleanup_rules" ''
|
2017-09-23 09:56:09 +02: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 08:38:40 +02:00
|
|
|
|
|
|
|
/^Received:/ IGNORE
|
|
|
|
/^X-Originating-IP:/ IGNORE
|
|
|
|
/^X-Mailer:/ IGNORE
|
|
|
|
/^User-Agent:/ IGNORE
|
|
|
|
/^X-Enigmail:/ IGNORE
|
|
|
|
'';
|
2017-08-12 16:14:16 +02:00
|
|
|
in
|
2016-07-25 17:48:40 +02:00
|
|
|
{
|
2017-09-03 11:13:34 +02:00
|
|
|
config = with cfg; lib.mkIf enable {
|
2017-08-12 16:14:16 +02:00
|
|
|
|
2017-09-03 11:13:34 +02:00
|
|
|
services.postfix = {
|
|
|
|
enable = true;
|
2017-11-26 11:56:34 +01:00
|
|
|
hostname = "${fqdn}";
|
2017-09-03 11:13:34 +02:00
|
|
|
networksStyle = "host";
|
|
|
|
mapFiles."valias" = valiases_file;
|
2017-11-09 22:13:27 +01:00
|
|
|
mapFiles."vaccounts" = vaccounts_file;
|
2017-09-03 11:13:34 +02:00
|
|
|
sslCert = certificatePath;
|
|
|
|
sslKey = keyPath;
|
|
|
|
enableSubmission = true;
|
2017-08-12 16:14:16 +02:00
|
|
|
|
2017-11-09 22:13:27 +01:00
|
|
|
extraConfig =
|
2017-09-03 11:13:34 +02:00
|
|
|
''
|
2017-09-12 22:47:13 +02:00
|
|
|
# Extra Config
|
2017-11-13 23:43:29 +01:00
|
|
|
mydestination = localhost
|
2017-08-12 16:14:16 +02:00
|
|
|
|
2017-11-11 14:19:05 +01:00
|
|
|
smtpd_banner = ${fqdn} ESMTP NO UCE
|
2017-09-03 11:13:34 +02:00
|
|
|
disable_vrfy_command = yes
|
|
|
|
message_size_limit = 20971520
|
2017-08-12 16:14:16 +02:00
|
|
|
|
2017-09-03 11:13:34 +02:00
|
|
|
# virtual mail system
|
|
|
|
virtual_uid_maps = static:5000
|
|
|
|
virtual_gid_maps = static:5000
|
|
|
|
virtual_mailbox_base = ${mailDirectory}
|
|
|
|
virtual_mailbox_domains = ${vhosts_file}
|
|
|
|
virtual_alias_maps = hash:/var/lib/postfix/conf/valias
|
|
|
|
virtual_transport = lmtp:unix:private/dovecot-lmtp
|
2017-08-12 16:14:16 +02:00
|
|
|
|
2017-09-03 11:13:34 +02:00
|
|
|
# sasl with dovecot
|
|
|
|
smtpd_sasl_type = dovecot
|
|
|
|
smtpd_sasl_path = private/auth
|
|
|
|
smtpd_sasl_auth_enable = yes
|
|
|
|
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
|
2017-09-20 09:26:42 +02:00
|
|
|
|
2017-12-22 16:58:35 +01:00
|
|
|
# quota
|
2018-02-23 14:52:11 +01:00
|
|
|
smtpd_recipient_restrictions = check_policy_service inet:localhost:12340
|
2017-12-22 16:58:35 +01:00
|
|
|
|
2017-09-19 23:54:40 +02:00
|
|
|
# TLS settings, inspired by https://github.com/jeaye/nix-files
|
2017-09-20 09:26:42 +02:00
|
|
|
# Submission by mail clients is handled in submissionOptions
|
|
|
|
smtpd_tls_security_level = may
|
|
|
|
# strong might suffice and is computationally less expensive
|
|
|
|
smtpd_tls_eecdh_grade = ultra
|
|
|
|
# Disable predecessors to TLS
|
|
|
|
smtpd_tls_protocols = !SSLv2, !SSLv3
|
|
|
|
# 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 23:54:40 +02:00
|
|
|
# Disable weak ciphers as reported by https://ssl-tools.net
|
|
|
|
# https://serverfault.com/questions/744168/how-to-disable-rc4-on-postfix
|
|
|
|
smtpd_tls_exclude_ciphers = RC4, aNULL
|
|
|
|
smtp_tls_exclude_ciphers = RC4, aNULL
|
|
|
|
|
|
|
|
# Configure a non blocking source of randomness
|
|
|
|
tls_random_source = dev:/dev/urandom
|
2017-09-03 11:13:34 +02:00
|
|
|
'';
|
2017-08-12 16:14:16 +02:00
|
|
|
|
2017-09-03 11:13:34 +02:00
|
|
|
submissionOptions =
|
2017-11-09 22:13:27 +01:00
|
|
|
{
|
2017-09-03 11:13:34 +02:00
|
|
|
smtpd_tls_security_level = "encrypt";
|
|
|
|
smtpd_sasl_auth_enable = "yes";
|
|
|
|
smtpd_sasl_type = "dovecot";
|
|
|
|
smtpd_sasl_path = "private/auth";
|
|
|
|
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-20 00:05:01 +02:00
|
|
|
cleanup_service_name = "submission-header-cleanup";
|
2017-09-03 11:13:34 +02:00
|
|
|
};
|
2017-09-20 08:38:40 +02:00
|
|
|
|
2017-09-20 00:05:01 +02:00
|
|
|
extraMasterConf = ''
|
|
|
|
submission-header-cleanup unix n - n - 0 cleanup
|
2017-09-20 08:38:40 +02:00
|
|
|
-o header_checks=pcre:${submissionHeaderCleanupRules}
|
2017-09-20 00:05:01 +02:00
|
|
|
'';
|
2017-09-03 11:13:34 +02:00
|
|
|
};
|
2017-08-13 14:05:40 +02:00
|
|
|
};
|
2016-07-25 17:48:40 +02:00
|
|
|
}
|