2023-08-31 01:55:45 +03:00
|
|
|
{ config, pkgs, depot, ... }:
|
2021-10-16 21:22:48 +03:00
|
|
|
let
|
2023-08-31 01:55:45 +03:00
|
|
|
inherit (depot.lib.identity) ldap;
|
|
|
|
inherit (depot.lib.meta) domain;
|
2021-10-16 21:22:48 +03:00
|
|
|
|
|
|
|
postfixCfg = config.services.postfix;
|
|
|
|
|
|
|
|
# TODO: switch to proper certdir
|
|
|
|
certDir = config.security.acme.certs."mail.${domain}".directory;
|
|
|
|
|
|
|
|
# TODO: check how this thing does lookups, apply bind dn
|
2023-10-25 20:30:49 +03:00
|
|
|
|
|
|
|
ldapConfigBase = with ldap.accounts; pkgs.writeText "dovecot-ldap.conf.ext" ''
|
2021-10-16 21:22:48 +03:00
|
|
|
uris = ${ldap.server.url}
|
|
|
|
|
|
|
|
auth_bind = yes
|
|
|
|
auth_bind_userdn = ${uidAttribute}=%n,${userSearchBase}
|
|
|
|
base = ${userSearchBase}
|
2023-10-25 20:30:49 +03:00
|
|
|
pass_filter = (&(objectClass=person)(${uidAttribute}=%n))
|
2021-10-16 21:22:48 +03:00
|
|
|
pass_attrs = uid=user
|
2023-10-25 20:30:49 +03:00
|
|
|
dn = dn=token
|
|
|
|
dnpass = @DOVECOT2_LDAP_DNPASS@
|
|
|
|
'';
|
|
|
|
|
|
|
|
ldapConfig = "/run/dovecot2/dovecot-ldap.conf.ext";
|
|
|
|
|
|
|
|
writeLdapConfig = pkgs.writeShellScriptBin "write-ldap-config" ''
|
|
|
|
cp ${ldapConfigBase} ${ldapConfig}
|
|
|
|
chmod 600 ${ldapConfig}
|
|
|
|
${pkgs.replace-secret}/bin/replace-secret '@DOVECOT2_LDAP_DNPASS@' "${config.age.secrets.dovecotLdapToken.path}" ${ldapConfig}
|
|
|
|
chmod 400 ${ldapConfig}
|
2021-10-16 21:22:48 +03:00
|
|
|
'';
|
|
|
|
in {
|
2023-10-25 20:30:49 +03:00
|
|
|
|
2024-07-22 00:23:42 +03:00
|
|
|
age.secrets.dovecotLdapToken.file = ../../../secrets/dovecot-ldap-token.age;
|
2023-10-25 20:30:49 +03:00
|
|
|
|
2021-10-16 21:22:48 +03:00
|
|
|
networking.firewall.allowedTCPPorts = [ 143 993 ];
|
|
|
|
|
|
|
|
services.dovecot2 = {
|
|
|
|
enable = true;
|
|
|
|
enableLmtp = true;
|
|
|
|
enableImap = true;
|
|
|
|
enablePAM = false;
|
|
|
|
mailUser = "vmail";
|
|
|
|
mailGroup = "vmail";
|
|
|
|
sslServerCert = "${certDir}/fullchain.pem";
|
|
|
|
sslServerKey = "${certDir}/key.pem";
|
|
|
|
|
|
|
|
modules = [ pkgs.dovecot_pigeonhole ];
|
|
|
|
|
2024-06-07 02:05:17 +03:00
|
|
|
sieve = {
|
|
|
|
extensions = [
|
|
|
|
"variables"
|
|
|
|
"envelope"
|
|
|
|
"fileinto"
|
|
|
|
"subaddress"
|
|
|
|
"mailbox"
|
|
|
|
];
|
|
|
|
scripts.after = ./sieve;
|
|
|
|
};
|
2021-10-16 21:22:48 +03:00
|
|
|
|
|
|
|
extraConfig = with config.services.dovecot2; ''
|
|
|
|
auth_username_format = %n
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
inbox = yes
|
|
|
|
separator = /
|
|
|
|
}
|
|
|
|
userdb {
|
|
|
|
driver = static
|
|
|
|
args = allow_all_users=yes uid=${mailUser} gid=${mailUser} home=/var/mail/virtual/%d/%n
|
|
|
|
}
|
|
|
|
passdb {
|
|
|
|
driver = ldap
|
|
|
|
args = ${ldapConfig}
|
|
|
|
}
|
|
|
|
|
|
|
|
service auth {
|
|
|
|
unix_listener auth {
|
|
|
|
mode = 0660
|
|
|
|
user = ${postfixCfg.user}
|
|
|
|
group = ${postfixCfg.group}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auth_mechanisms = plain login
|
|
|
|
'';
|
|
|
|
};
|
2023-10-25 20:30:49 +03:00
|
|
|
|
|
|
|
systemd.services.dovecot2.serviceConfig.ExecStartPre = [ "${writeLdapConfig}/bin/write-ldap-config" ];
|
|
|
|
|
2023-12-02 02:14:52 +02:00
|
|
|
services.fail2ban.jails.dovecot = {};
|
2021-10-16 21:22:48 +03:00
|
|
|
}
|