2025-06-18 19:53:44 +03:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}@nixos-args:
|
2024-12-20 16:13:59 +04:00
|
|
|
let
|
2024-12-20 18:41:07 +04:00
|
|
|
inherit (import ./common.nix nixos-args)
|
2025-01-31 14:31:09 +04:00
|
|
|
appendSetting
|
2024-12-26 18:27:25 +04:00
|
|
|
auth-passthru
|
2025-01-17 15:53:21 +04:00
|
|
|
is-auth-enabled
|
2024-12-20 18:41:07 +04:00
|
|
|
;
|
2024-12-20 16:13:59 +04:00
|
|
|
|
2024-12-20 18:41:07 +04:00
|
|
|
cfg = config.mailserver;
|
2024-12-20 16:13:59 +04:00
|
|
|
|
|
|
|
ldapSenderLoginMapFile = "/run/postfix/ldap-sender-login-map.cf";
|
2025-06-18 19:53:44 +03:00
|
|
|
submissionOptions.smtpd_sender_login_maps = lib.mkForce "hash:/etc/postfix/vaccounts,ldap:${ldapSenderLoginMapFile}";
|
2024-12-20 16:13:59 +04:00
|
|
|
commonLdapConfig = ''
|
|
|
|
server_host = ${lib.concatStringsSep " " cfg.ldap.uris}
|
|
|
|
start_tls = ${if cfg.ldap.startTls then "yes" else "no"}
|
|
|
|
version = 3
|
2024-12-27 07:46:36 +04:00
|
|
|
tls_ca_cert_file = ${cfg.ldap.tlsCAFile}
|
|
|
|
tls_require_cert = yes
|
2024-12-20 16:13:59 +04:00
|
|
|
|
|
|
|
search_base = ${cfg.ldap.searchBase}
|
|
|
|
scope = ${cfg.ldap.searchScope}
|
|
|
|
|
|
|
|
bind = yes
|
|
|
|
bind_dn = ${cfg.ldap.bind.dn}
|
|
|
|
'';
|
|
|
|
ldapSenderLoginMap = pkgs.writeText "ldap-sender-login-map.cf" ''
|
|
|
|
${commonLdapConfig}
|
|
|
|
query_filter = ${cfg.ldap.postfix.filter}
|
|
|
|
result_attribute = ${cfg.ldap.postfix.mailAttribute}
|
|
|
|
'';
|
2025-01-31 14:31:09 +04:00
|
|
|
appendPwdInSenderLoginMap = appendSetting {
|
2024-12-20 16:13:59 +04:00
|
|
|
name = "ldap-sender-login-map";
|
|
|
|
file = ldapSenderLoginMap;
|
|
|
|
prefix = "bind_pw = ";
|
|
|
|
passwordFile = cfg.ldap.bind.passwordFile;
|
|
|
|
destination = ldapSenderLoginMapFile;
|
|
|
|
};
|
|
|
|
|
|
|
|
ldapVirtualMailboxMap = pkgs.writeText "ldap-virtual-mailbox-map.cf" ''
|
|
|
|
${commonLdapConfig}
|
|
|
|
query_filter = ${cfg.ldap.postfix.filter}
|
|
|
|
result_attribute = ${cfg.ldap.postfix.uidAttribute}
|
|
|
|
'';
|
|
|
|
ldapVirtualMailboxMapFile = "/run/postfix/ldap-virtual-mailbox-map.cf";
|
2025-01-31 14:31:09 +04:00
|
|
|
appendPwdInVirtualMailboxMap = appendSetting {
|
2024-12-20 16:13:59 +04:00
|
|
|
name = "ldap-virtual-mailbox-map";
|
|
|
|
file = ldapVirtualMailboxMap;
|
|
|
|
prefix = "bind_pw = ";
|
|
|
|
passwordFile = cfg.ldap.bind.passwordFile;
|
|
|
|
destination = ldapVirtualMailboxMapFile;
|
|
|
|
};
|
|
|
|
in
|
2025-01-25 01:08:41 +04:00
|
|
|
{
|
2024-12-20 18:41:07 +04:00
|
|
|
mailserver.ldap = {
|
|
|
|
postfix.mailAttribute = "mail";
|
|
|
|
postfix.uidAttribute = "uid";
|
|
|
|
};
|
2024-12-20 16:13:59 +04:00
|
|
|
systemd.services.postfix-setup = {
|
|
|
|
preStart = ''
|
|
|
|
${appendPwdInVirtualMailboxMap}
|
|
|
|
${appendPwdInSenderLoginMap}
|
|
|
|
'';
|
2025-06-18 19:53:44 +03:00
|
|
|
restartTriggers = [
|
|
|
|
appendPwdInVirtualMailboxMap
|
|
|
|
appendPwdInSenderLoginMap
|
|
|
|
];
|
2024-12-26 18:27:25 +04:00
|
|
|
wants = [ auth-passthru.oauth2-systemd-service ];
|
2024-12-27 07:46:36 +04:00
|
|
|
after = [ auth-passthru.oauth2-systemd-service ];
|
2024-12-20 16:13:59 +04:00
|
|
|
};
|
|
|
|
services.postfix = {
|
|
|
|
# the list should be merged with other options from nixos-mailserver
|
|
|
|
config.virtual_mailbox_maps = [ "ldap:${ldapVirtualMailboxMapFile}" ];
|
2024-12-20 18:41:07 +04:00
|
|
|
inherit submissionOptions;
|
2024-12-20 16:13:59 +04:00
|
|
|
submissionsOptions = submissionOptions;
|
|
|
|
};
|
|
|
|
}
|