2017-08-12 11:27:19 +02:00
|
|
|
# nixos-mailserver: a simple mail server
|
2018-01-29 10:34:27 +01:00
|
|
|
# Copyright (C) 2016-2018 Robin Raymond
|
2017-08-12 11:27:19 +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-02 13:58:42 +02:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
with config.mailserver;
|
2017-08-12 11:27:19 +02:00
|
|
|
|
2017-08-12 12:28:46 +02:00
|
|
|
let
|
2017-11-05 09:42:39 +01:00
|
|
|
vmail_user = {
|
2017-10-18 09:20:44 +02:00
|
|
|
name = vmailUserName;
|
2021-04-14 22:50:54 +02:00
|
|
|
isSystemUser = true;
|
2017-11-18 21:38:26 +01:00
|
|
|
uid = vmailUID;
|
2017-09-02 13:23:37 +02:00
|
|
|
home = mailDirectory;
|
2017-08-12 12:28:46 +02:00
|
|
|
createHome = true;
|
2017-10-18 09:20:44 +02:00
|
|
|
group = vmailGroupName;
|
2017-11-05 09:42:39 +01:00
|
|
|
};
|
2017-08-12 12:28:46 +02:00
|
|
|
|
|
|
|
|
2017-11-18 04:37:59 +01:00
|
|
|
virtualMailUsersActivationScript = pkgs.writeScript "activate-virtual-mail-users" ''
|
|
|
|
#!${pkgs.stdenv.shell}
|
2017-08-12 11:27:19 +02:00
|
|
|
|
2017-11-18 04:37:59 +01:00
|
|
|
set -euo pipefail
|
|
|
|
|
2023-06-28 10:22:07 +02:00
|
|
|
# Prevent world-readable paths, even temporarily.
|
|
|
|
umask 007
|
|
|
|
|
2017-11-18 04:37:59 +01:00
|
|
|
# Create directory to store user sieve scripts if it doesn't exist
|
2020-11-21 14:18:40 +01:00
|
|
|
if (! test -d "${sieveDirectory}"); then
|
|
|
|
mkdir "${sieveDirectory}"
|
|
|
|
chown "${vmailUserName}:${vmailGroupName}" "${sieveDirectory}"
|
|
|
|
chmod 770 "${sieveDirectory}"
|
2017-11-18 04:37:59 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Copy user's sieve script to the correct location (if it exists). If it
|
|
|
|
# is null, remove the file.
|
|
|
|
${lib.concatMapStringsSep "\n" ({ name, sieveScript }:
|
|
|
|
if lib.isString sieveScript then ''
|
2020-11-21 14:18:40 +01:00
|
|
|
if (! test -d "${sieveDirectory}/${name}"); then
|
|
|
|
mkdir -p "${sieveDirectory}/${name}"
|
|
|
|
chown "${vmailUserName}:${vmailGroupName}" "${sieveDirectory}/${name}"
|
|
|
|
chmod 770 "${sieveDirectory}/${name}"
|
2018-01-07 13:06:05 +01:00
|
|
|
fi
|
2020-11-21 14:18:40 +01:00
|
|
|
cat << 'EOF' > "${sieveDirectory}/${name}/default.sieve"
|
2017-11-18 04:37:59 +01:00
|
|
|
${sieveScript}
|
|
|
|
EOF
|
2020-11-21 14:18:40 +01:00
|
|
|
chown "${vmailUserName}:${vmailGroupName}" "${sieveDirectory}/${name}/default.sieve"
|
2017-11-18 04:37:59 +01:00
|
|
|
'' else ''
|
2020-11-21 14:18:40 +01:00
|
|
|
if (test -f "${sieveDirectory}/${name}/default.sieve"); then
|
|
|
|
rm "${sieveDirectory}/${name}/default.sieve"
|
2017-11-18 04:37:59 +01:00
|
|
|
fi
|
2020-11-21 14:18:40 +01:00
|
|
|
if (test -f "${sieveDirectory}/${name}.svbin"); then
|
|
|
|
rm "${sieveDirectory}/${name}/default.svbin"
|
2017-11-18 04:37:59 +01:00
|
|
|
fi
|
|
|
|
'') (map (user: { inherit (user) name sieveScript; })
|
|
|
|
(lib.attrValues loginAccounts))}
|
|
|
|
'';
|
|
|
|
in {
|
2017-09-02 13:58:42 +02:00
|
|
|
config = lib.mkIf enable {
|
2020-03-06 18:27:47 +01:00
|
|
|
# assert that all accounts provide a password
|
|
|
|
assertions = (map (acct: {
|
|
|
|
assertion = (acct.hashedPassword != null || acct.hashedPasswordFile != null);
|
|
|
|
message = "${acct.name} must provide either a hashed password or a password hash file";
|
|
|
|
}) (lib.attrValues loginAccounts));
|
|
|
|
|
|
|
|
# warn for accounts that specify both password and file
|
|
|
|
warnings = (map
|
|
|
|
(acct: "${acct.name} specifies both a password hash and hash file; hash file will be used")
|
|
|
|
(lib.filter
|
|
|
|
(acct: (acct.hashedPassword != null && acct.hashedPasswordFile != null))
|
|
|
|
(lib.attrValues loginAccounts)));
|
|
|
|
|
2017-09-02 13:58:42 +02:00
|
|
|
# set the vmail gid to a specific value
|
|
|
|
users.groups = {
|
2017-11-18 21:38:26 +01:00
|
|
|
"${vmailGroupName}" = { gid = vmailUID; };
|
2017-09-02 13:58:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
# define all users
|
2017-12-22 16:08:42 +01:00
|
|
|
users.users = {
|
2017-11-05 09:42:39 +01:00
|
|
|
"${vmail_user.name}" = lib.mkForce vmail_user;
|
|
|
|
};
|
2017-11-18 04:37:59 +01:00
|
|
|
|
|
|
|
systemd.services.activate-virtual-mail-users = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
before = [ "dovecot2.service" ];
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStart = virtualMailUsersActivationScript;
|
|
|
|
};
|
|
|
|
enable = true;
|
|
|
|
};
|
2017-09-02 13:58:42 +02:00
|
|
|
};
|
2017-08-12 11:27:19 +02:00
|
|
|
}
|