Merge branch 'module-rewrite'

This commit is contained in:
Robin Raymond 2017-09-03 15:31:37 +02:00
commit 1e0c203bf8
13 changed files with 533 additions and 366 deletions

View file

@ -14,10 +14,15 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
{ virus_scanning }:
{ config, pkgs, lib, ... }:
let
cfg = config.mailserver;
in
{
daemon.enable = virus_scanning;
updater.enable = virus_scanning;
config = lib.mkIf cfg.virusScanning {
services.clamav.daemon.enable = true;
services.clamav.updater.enable = true;
};
}

36
mail-server/common.nix Normal file
View file

@ -0,0 +1,36 @@
# nixos-mailserver: a simple mail server
# Copyright (C) 2016-2017 Robin Raymond
#
# 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/>
{ config }:
let
cfg = config.mailserver;
in
{
# cert :: PATH
certificatePath = if cfg.certificateScheme == 1
then cfg.certificateFile
else if cfg.certificateScheme == 2
then "${cfg.certificateDirectory}/cert-${cfg.domain}.pem"
else "";
# key :: PATH
keyPath = if cfg.certificateScheme == 1
then cfg.keyFile
else if cfg.certificateScheme == 2
then "${cfg.certificateDirectory}/key-${cfg.domain}.pem"
else "";
}

View file

@ -14,73 +14,80 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
{ vmail_group_name, vmail_user_name, mail_dir, enable_imap, enable_pop3, cert,
key }:
{ config, pkgs, lib, ... }:
with (import ./common.nix { inherit config; });
let
cfg = config.mailserver;
# maildir in format "/${domain}/${user}/"
dovecot_maildir = "maildir:${mail_dir}/%d/%n/";
dovecot_maildir = "maildir:${cfg.mailDirectory}/%d/%n/";
in
{
enable = true;
enableImap = enable_imap;
enablePop3 = enable_pop3;
mailGroup = vmail_group_name;
mailUser = vmail_user_name;
mailLocation = dovecot_maildir;
sslServerCert = cert;
sslServerKey = key;
enableLmtp = true;
extraConfig = ''
#Extra Config
mail_access_groups = ${vmail_group_name}
ssl = required
config = with cfg; lib.mkIf enable {
services.dovecot2 = {
enable = true;
enableImap = enableImap;
enablePop3 = enablePop3;
mailGroup = vmailGroupName;
mailUser = vmailUserName;
mailLocation = dovecot_maildir;
sslServerCert = certificatePath;
sslServerKey = keyPath;
enableLmtp = true;
extraConfig = ''
#Extra Config
mail_access_groups = ${vmailGroupName}
ssl = required
service lmtp {
unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
service lmtp {
unix_listener /var/lib/postfix/queue/private/dovecot-lmtp {
group = postfix
mode = 0600
user = postfix # TODO: < make variable
}
}
}
}
service auth {
unix_listener /var/lib/postfix/queue/private/auth {
service auth {
unix_listener /var/lib/postfix/queue/private/auth {
mode = 0660
user = postfix # TODO: < make variable
group = postfix # TODO: < make variable
}
}
}
}
auth_mechanisms = plain login
auth_mechanisms = plain login
namespace inbox {
namespace inbox {
#prefix = INBOX.
# the namespace prefix isn't added again to the mailbox names.
inbox = yes
# ...
#prefix = INBOX.
# the namespace prefix isn't added again to the mailbox names.
inbox = yes
# ...
mailbox "Trash" {
auto = no
special_use = \Trash
}
mailbox "Trash" {
auto = no
special_use = \Trash
}
mailbox "Junk" {
auto = subscribe
special_use = \Junk
}
mailbox "Junk" {
auto = subscribe
special_use = \Junk
}
mailbox "Drafts" {
auto = subscribe
special_use = \Drafts
}
mailbox "Drafts" {
auto = subscribe
special_use = \Drafts
}
mailbox "Sent" {
auto = subscribe
special_use = \Sent
}
}
'';
mailbox "Sent" {
auto = subscribe
special_use = \Sent
}
}
'';
};
};
}

View file

@ -14,10 +14,15 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
{ pkgs, certificate_scheme }:
{ config, pkgs, lib, ... }:
let
cfg = config.mailserver;
in
{
systemPackages = with pkgs; [
dovecot opendkim openssh postfix clamav rspamd rmilter
] ++ (if certificate_scheme == 2 then [ openssl ] else []);
config = with cfg; lib.mkIf enable {
environment.systemPackages = with pkgs; [
dovecot opendkim openssh postfix clamav rspamd rmilter
] ++ (if certificateScheme == 2 then [ openssl ] else []);
};
}

View file

@ -14,14 +14,20 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
{ domain, host_prefix, enable_imap, enable_pop3 }:
{ config, pkgs, lib, ... }:
let
cfg = config.mailserver;
in
{
hostName = "${host_prefix}.${domain}";
config = with cfg; lib.mkIf enable {
firewall = {
allowedTCPPorts = [ 25 587 ]
++ (if enable_imap then [ 143 ] else [])
++ (if enable_pop3 then [ 110 ] else []);
networking.hostName = "${hostPrefix}.${domain}";
networking.firewall = {
allowedTCPPorts = [ 25 587 ]
++ (if enableImap then [ 143 ] else [])
++ (if enablePop3 then [ 110 ] else []);
};
};
}

View file

@ -14,25 +14,25 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
{ mail_dir, domain, valiases, cert, key }:
{ config, pkgs, lib, ... }:
with (import ./common.nix { inherit config; });
let
# valiasToString :: { from = "..."; to = "..." } -> String
valiasToString = x: "${x.from}@${domain} ${x.to}@${domain}\n";
cfg = config.mailserver;
# valiases_postfix :: [ String ]
valiases_postfix = map valiasToString valiases;
# concatString :: [ String ] -> String
concatString = l: if l == []
then ""
else (builtins.head l) + (concatString (builtins.tail l));
valiases_postfix = map
(from:
let to = cfg.virtualAliases.${from};
in "${from}@${cfg.domain} ${to}@${cfg.domain}")
(builtins.attrNames cfg.virtualAliases);
# valiases_file :: Path
valiases_file = builtins.toFile "valias" (concatString valiases_postfix);
valiases_file = builtins.toFile "valias" (lib.concatStringsSep "\n" valiases_postfix);
# vhosts_file :: Path
vhosts_file = builtins.toFile "vhosts" domain;
vhosts_file = builtins.toFile "vhosts" cfg.domain;
# vaccounts_file :: Path
# see
@ -43,50 +43,54 @@ let
in
{
enable = true;
networksStyle = "host";
mapFiles."valias" = valiases_file;
mapFiles."vaccounts" = vaccounts_file;
sslCert = cert;
sslKey = key;
enableSubmission = true;
config = with cfg; lib.mkIf enable {
extraConfig =
''
services.postfix = {
enable = true;
networksStyle = "host";
mapFiles."valias" = valiases_file;
mapFiles."vaccounts" = vaccounts_file;
sslCert = certificatePath;
sslKey = keyPath;
enableSubmission = true;
# Extra Config
extraConfig =
''
# Extra Config
smtpd_banner = $myhostname ESMTP NO UCE
smtpd_tls_auth_only = yes
disable_vrfy_command = yes
message_size_limit = 20971520
smtpd_banner = $myhostname ESMTP NO UCE
smtpd_tls_auth_only = yes
disable_vrfy_command = yes
message_size_limit = 20971520
# virtual mail system
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
virtual_mailbox_base = ${mail_dir}
virtual_mailbox_domains = ${vhosts_file}
virtual_alias_maps = hash:/var/lib/postfix/conf/valias
virtual_transport = lmtp:unix:private/dovecot-lmtp
# 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
# 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
'';
# 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
'';
submissionOptions =
{
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";
submissionOptions =
{
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";
};
};
};
}

View file

@ -14,10 +14,12 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
{ domain, virus_scanning, dkim_signing, dkim_dir, dkim_selector }:
{ config, pkgs, lib, ... }:
let
clamav = if virus_scanning
cfg = config.mailserver;
clamav = if cfg.virusScanning
then
''
clamav {
@ -25,14 +27,14 @@ let
};
''
else "";
dkim = if dkim_signing
dkim = if cfg.dkimSigning
then
''
dkim {
domain {
key = "${dkim_dir}";
key = "${cfg.dkimKeyDirectory}";
domain = "*";
selector = "${dkim_selector}";
selector = "${cfg.dkimSelector}";
};
sign_alg = sha256;
auth_only = yes;
@ -41,15 +43,19 @@ let
else "";
in
{
enable = true;
# debug = true;
postfix.enable = true;
rspamd.enable = true;
extraConfig =
''
${clamav}
config = with cfg; lib.mkIf enable {
services.rmilter = {
enable = true;
#debug = true;
postfix.enable = true;
rspamd.enable = true;
extraConfig =
''
${clamav}
${dkim}
'';
${dkim}
'';
};
};
}

View file

@ -14,45 +14,31 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
{ mail_dir, vmail_user_name, vmail_group_name, valiases, domain, enable_imap,
enable_pop3, virus_scanning, dkim_signing, dkim_selector, dkim_dir,
certificate_scheme, cert_file, key_file, cert_dir }:
{ config, pkgs, lib, ... }:
let
cfg = config.mailserver;
# cert :: PATH
cert = if certificate_scheme == 1
then cert_file
else if certificate_scheme == 2
then "${cert_dir}/cert-${domain}.pem"
cert = if cfg.certificateScheme == 1
then cfg.certificateFile
else if cfg.certificateScheme == 2
then "${cfg.certificateDirectory}/cert-${cfg.domain}.pem"
else "";
# key :: PATH
key = if certificate_scheme == 1
then key_file
else if certificate_scheme == 2
then "${cert_dir}/key-${domain}.pem"
key = if cfg.certificateScheme == 1
then cfg.keyFile
else if cfg.certificateScheme == 2
then "${cfg.certificateDirectory}/key-${cfg.domain}.pem"
else "";
in
{
# rspamd
rspamd = {
enable = true;
};
rmilter = import ./rmilter.nix {
inherit domain virus_scanning dkim_signing dkim_selector dkim_dir;
};
postfix = import ./postfix.nix {
inherit mail_dir domain valiases cert key;
};
dovecot2 = import ./dovecot.nix {
inherit vmail_group_name vmail_user_name mail_dir enable_imap
enable_pop3 cert key;
};
clamav = import ./clamav.nix {
inherit virus_scanning;
};
imports = [
./rmilter.nix
./postfix.nix key
./dovecot.nix
];
}

View file

@ -14,22 +14,23 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
{ pkgs, mail_dir, vmail_group_name, certificate_scheme, cert_dir, host_prefix,
domain, dkim_selector, dkim_dir}:
{ config, pkgs, lib, ... }:
let
create_certificate = if certificate_scheme == 2 then
cfg = config.mailserver;
create_certificate = if cfg.certificateScheme == 2 then
''
# Create certificates if they do not exist yet
dir="${cert_dir}"
fqdn="${host_prefix}.${domain}"
dir="${cfg.certificateDirectory}"
fqdn="${cfg.hostPrefix}.${cfg.domain}"
case $fqdn in /*) fqdn=$(cat "$fqdn");; esac
key="''${dir}/key-${domain}.pem";
cert="''${dir}/cert-${domain}.pem";
key="''${dir}/key-${cfg.domain}.pem";
cert="''${dir}/cert-${cfg.domain}.pem";
if [ ! -f "''${key}" ] || [ ! -f "''${cert}" ]
then
mkdir -p "${cert_dir}"
mkdir -p "${cfg.certificateDirectory}"
(umask 077; "${pkgs.openssl}/bin/openssl" genrsa -out "''${key}" 2048) &&
"${pkgs.openssl}/bin/openssl" req -new -key "''${key}" -x509 -subj "/CN=''${fqdn}" \
-days 3650 -out "''${cert}"
@ -37,47 +38,50 @@ let
''
else "";
dkim_key = "${dkim_dir}/${dkim_selector}.private";
dkim_txt = "${dkim_dir}/${dkim_selector}.txt";
dkim_key = "${cfg.dkimKeyDirectory}/${cfg.dkimSelector}.private";
dkim_txt = "${cfg.dkimKeyDirectory}/${cfg.dkimSelector}.txt";
create_dkim_cert =
''
# Create dkim dir
mkdir -p "${dkim_dir}"
chown rmilter:rmilter "${dkim_dir}"
mkdir -p "${cfg.dkimKeyDirectory}"
chown rmilter:rmilter "${cfg.dkimKeyDirectory}"
if [ ! -f "${dkim_key}" ] || [ ! -f "${dkim_txt}" ]
then
${pkgs.opendkim}/bin/opendkim-genkey -s "${dkim_selector}" \
-d ${domain} \
--directory="${dkim_dir}"
${pkgs.opendkim}/bin/opendkim-genkey -s "${cfg.dkimSelector}" \
-d ${cfg.domain} \
--directory="${cfg.dkimKeyDirectory}"
chown rmilter:rmilter "${dkim_key}"
fi
'';
in
{
# Make sure postfix gets started first, so that the certificates are in place
services.dovecot2.after = [ "postfix.service" ];
config = with cfg; lib.mkIf enable {
# Make sure postfix gets started first, so that the certificates are in place
systemd.services.dovecot2.after = [ "postfix.service" ];
# Create certificates and maildir folder
services.postfix = {
preStart =
''
# Create certificates and maildir folder
systemd.services.postfix = {
preStart =
''
# Create mail directory and set permissions. See
# <http://wiki2.dovecot.org/SharedMailboxes/Permissions>.
mkdir -p "${mail_dir}"
chgrp "${vmail_group_name}" "${mail_dir}"
chmod 02770 "${mail_dir}"
${create_certificate}
'';
};
${create_certificate}
'';
};
# Create dkim certificates
systemd.services.rmilter = {
preStart =
''
${create_dkim_cert}
'';
};
# Create dkim certificates
services.rmilter = {
preStart =
''
${create_dkim_cert}
'';
};
}

View file

@ -14,37 +14,41 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
{ vmail_id_start, vmail_user_name, vmail_group_name, domain, mail_dir,
login_accounts }:
{ config, pkgs, lib, ... }:
with config.mailserver;
let
vmail_user = [{
name = vmail_user_name;
name = vmailUserName;
isNormalUser = false;
uid = vmail_id_start;
home = mail_dir;
uid = vmailUIDStart;
home = mailDirectory;
createHome = true;
group = vmail_group_name;
group = vmailGroupName;
}];
# accountsToUser :: String -> UserRecord
accountsToUser = x: {
name = x.name + "@" + domain;
accountsToUser = account: {
name = account.name + "@" + domain;
isNormalUser = false;
group = vmail_group_name;
hashedPassword = x.password;
group = vmailGroupName;
inherit (account) hashedPassword;
};
# mail_user :: [ UserRecord ]
mail_user = map accountsToUser login_accounts;
mail_user = map accountsToUser (lib.attrValues loginAccounts);
in
{
# set the vmail gid to a specific value
groups = {
vmail = { gid = vmail_id_start; };
};
# define all users
extraUsers = vmail_user ++ mail_user;
config = lib.mkIf enable {
# set the vmail gid to a specific value
users.groups = {
vmail = { gid = vmailUIDStart; };
};
# define all users
users.extraUsers = vmail_user ++ mail_user;
};
}