2021-12-05 21:45:35 +01:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2021-06-02 14:32:20 +02:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.parsedmarc;
|
2021-12-05 21:45:35 +01:00
|
|
|
opt = options.services.parsedmarc;
|
2022-03-31 15:58:26 +02:00
|
|
|
isSecret = v: isAttrs v && v ? _secret && isString v._secret;
|
|
|
|
ini = pkgs.formats.ini {
|
|
|
|
mkKeyValue = lib.flip lib.generators.mkKeyValueDefault "=" rec {
|
|
|
|
mkValueString = v:
|
|
|
|
if isInt v then toString v
|
|
|
|
else if isString v then v
|
|
|
|
else if true == v then "True"
|
|
|
|
else if false == v then "False"
|
|
|
|
else if isSecret v then hashString "sha256" v._secret
|
|
|
|
else throw "unsupported type ${typeOf v}: ${(lib.generators.toPretty {}) v}";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
inherit (builtins) elem isAttrs isString isInt isList typeOf hashString;
|
2021-06-02 14:32:20 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.parsedmarc = {
|
|
|
|
|
2022-08-28 21:18:44 +02:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
parsedmarc, a DMARC report monitoring service
|
2022-08-28 21:18:44 +02:00
|
|
|
'');
|
2021-06-02 14:32:20 +02:00
|
|
|
|
|
|
|
provision = {
|
|
|
|
localMail = {
|
|
|
|
enable = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
2022-08-03 22:46:41 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
Whether Postfix and Dovecot should be set up to receive
|
|
|
|
mail locally. parsedmarc will be configured to watch the
|
|
|
|
local inbox as the automatically created user specified in
|
2022-08-03 22:46:41 +02:00
|
|
|
[](#opt-services.parsedmarc.provision.localMail.recipientName)
|
2021-06-02 14:32:20 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
recipientName = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "dmarc";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
The DMARC mail recipient name, i.e. the name part of the
|
|
|
|
email address which receives DMARC reports.
|
|
|
|
|
|
|
|
A local user with this name will be set up and assigned a
|
|
|
|
randomized password on service start.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
hostname = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = config.networking.fqdn;
|
2021-10-03 18:06:03 +02:00
|
|
|
defaultText = lib.literalExpression "config.networking.fqdn";
|
2021-06-02 14:32:20 +02:00
|
|
|
example = "monitoring.example.com";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
The hostname to use when configuring Postfix.
|
|
|
|
|
|
|
|
Should correspond to the host's fully qualified domain
|
|
|
|
name and the domain part of the email address which
|
|
|
|
receives DMARC reports. You also have to set up an MX record
|
|
|
|
pointing to this domain name.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
geoIp = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
2022-08-03 22:46:41 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to enable and configure the [geoipupdate](#opt-services.geoipupdate.enable)
|
2021-06-02 14:32:20 +02:00
|
|
|
service to automatically fetch GeoIP databases. Not crucial,
|
|
|
|
but recommended for full functionality.
|
|
|
|
|
2022-08-03 22:46:41 +02:00
|
|
|
To finish the setup, you need to manually set the [](#opt-services.geoipupdate.settings.AccountID) and
|
|
|
|
[](#opt-services.geoipupdate.settings.LicenseKey)
|
2021-06-02 14:32:20 +02:00
|
|
|
options.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
elasticsearch = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
Whether to set up and use a local instance of Elasticsearch.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
grafana = {
|
|
|
|
datasource = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = cfg.provision.elasticsearch && config.services.grafana.enable;
|
2021-12-05 21:45:35 +01:00
|
|
|
defaultText = lib.literalExpression ''
|
|
|
|
config.${opt.provision.elasticsearch} && config.${options.services.grafana.enable}
|
|
|
|
'';
|
2021-06-02 14:32:20 +02:00
|
|
|
apply = x: x && cfg.provision.elasticsearch;
|
2022-08-03 22:46:41 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
Whether the automatically provisioned Elasticsearch
|
|
|
|
instance should be added as a grafana datasource. Has no
|
|
|
|
effect unless
|
2022-08-03 22:46:41 +02:00
|
|
|
[](#opt-services.parsedmarc.provision.elasticsearch)
|
2021-06-02 14:32:20 +02:00
|
|
|
is also enabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
dashboard = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = config.services.grafana.enable;
|
2021-11-26 01:16:05 +01:00
|
|
|
defaultText = lib.literalExpression "config.services.grafana.enable";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
Whether the official parsedmarc grafana dashboard should
|
|
|
|
be provisioned to the local grafana instance.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
settings = lib.mkOption {
|
2022-03-31 15:58:26 +02:00
|
|
|
example = lib.literalExpression ''
|
|
|
|
{
|
|
|
|
imap = {
|
|
|
|
host = "imap.example.com";
|
|
|
|
user = "alice@example.com";
|
|
|
|
password = { _secret = "/run/keys/imap_password" };
|
2022-07-31 13:45:25 +02:00
|
|
|
};
|
|
|
|
mailbox = {
|
2022-03-31 15:58:26 +02:00
|
|
|
watch = true;
|
2022-07-31 13:45:25 +02:00
|
|
|
batch_size = 30;
|
2022-03-31 15:58:26 +02:00
|
|
|
};
|
|
|
|
splunk_hec = {
|
|
|
|
url = "https://splunkhec.example.com";
|
|
|
|
token = { _secret = "/run/keys/splunk_token" };
|
|
|
|
index = "email";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
Configuration parameters to set in
|
2022-07-28 23:19:15 +02:00
|
|
|
{file}`parsedmarc.ini`. For a full list of
|
2021-06-02 14:32:20 +02:00
|
|
|
available parameters, see
|
2022-07-28 23:19:15 +02:00
|
|
|
<https://domainaware.github.io/parsedmarc/#configuration-file>.
|
2022-03-31 15:58:26 +02:00
|
|
|
|
|
|
|
Settings containing secret data should be set to an attribute
|
2022-07-28 23:19:15 +02:00
|
|
|
set containing the attribute `_secret` - a
|
2022-03-31 15:58:26 +02:00
|
|
|
string pointing to a file containing the value the option
|
|
|
|
should be set to. See the example to get a better picture of
|
2022-07-28 23:19:15 +02:00
|
|
|
this: in the resulting {file}`parsedmarc.ini`
|
|
|
|
file, the `splunk_hec.token` key will be set
|
2022-03-31 15:58:26 +02:00
|
|
|
to the contents of the
|
2022-07-28 23:19:15 +02:00
|
|
|
{file}`/run/keys/splunk_token` file.
|
2021-06-02 14:32:20 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
type = lib.types.submodule {
|
|
|
|
freeformType = ini.type;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
general = {
|
|
|
|
save_aggregate = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
Save aggregate report data to Elasticsearch and/or Splunk.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
save_forensic = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
Save forensic report data to Elasticsearch and/or Splunk.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-07-31 13:45:25 +02:00
|
|
|
mailbox = {
|
|
|
|
watch = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Use the IMAP IDLE command to process messages as they arrive.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
delete = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Delete messages after processing them, instead of archiving them.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-06-02 14:32:20 +02:00
|
|
|
imap = {
|
|
|
|
host = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "localhost";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
The IMAP server hostname or IP address.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = lib.mkOption {
|
|
|
|
type = lib.types.port;
|
|
|
|
default = 993;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
The IMAP server port.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
ssl = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
Use an encrypted SSL/TLS connection.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
user = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr str;
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
The IMAP server username.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
password = lib.mkOption {
|
2022-03-31 15:58:26 +02:00
|
|
|
type = with lib.types; nullOr (either path (attrsOf path));
|
2021-06-02 14:32:20 +02:00
|
|
|
default = null;
|
2022-08-03 22:46:41 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-03-31 15:58:26 +02:00
|
|
|
The IMAP server password.
|
|
|
|
|
|
|
|
Always handled as a secret whether the value is
|
2022-08-03 22:46:41 +02:00
|
|
|
wrapped in a `{ _secret = ...; }`
|
|
|
|
attrset or not (refer to [](#opt-services.parsedmarc.settings) for
|
2022-03-31 15:58:26 +02:00
|
|
|
details).
|
2021-06-02 14:32:20 +02:00
|
|
|
'';
|
2022-03-31 15:58:26 +02:00
|
|
|
apply = x: if isAttrs x || x == null then x else { _secret = x; };
|
2021-06-02 14:32:20 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
smtp = {
|
|
|
|
host = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr str;
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
The SMTP server hostname or IP address.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr port;
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
The SMTP server port.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
ssl = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr bool;
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
Use an encrypted SSL/TLS connection.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
user = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr str;
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
The SMTP server username.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
password = lib.mkOption {
|
2022-03-31 15:58:26 +02:00
|
|
|
type = with lib.types; nullOr (either path (attrsOf path));
|
2021-06-02 14:32:20 +02:00
|
|
|
default = null;
|
2022-08-03 22:46:41 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-03-31 15:58:26 +02:00
|
|
|
The SMTP server password.
|
|
|
|
|
|
|
|
Always handled as a secret whether the value is
|
2022-08-03 22:46:41 +02:00
|
|
|
wrapped in a `{ _secret = ...; }`
|
|
|
|
attrset or not (refer to [](#opt-services.parsedmarc.settings) for
|
2022-03-31 15:58:26 +02:00
|
|
|
details).
|
2021-06-02 14:32:20 +02:00
|
|
|
'';
|
2022-03-31 15:58:26 +02:00
|
|
|
apply = x: if isAttrs x || x == null then x else { _secret = x; };
|
2021-06-02 14:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
from = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr str;
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The `From` address to use for the
|
2021-06-02 14:32:20 +02:00
|
|
|
outgoing mail.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
to = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr (listOf str);
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
The addresses to send outgoing mail to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
elasticsearch = {
|
|
|
|
hosts = lib.mkOption {
|
|
|
|
default = [];
|
|
|
|
type = with lib.types; listOf str;
|
|
|
|
apply = x: if x == [] then null else lib.concatStringsSep "," x;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
A list of Elasticsearch hosts to push parsed reports
|
|
|
|
to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
user = lib.mkOption {
|
|
|
|
type = with lib.types; nullOr str;
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
Username to use when connecting to Elasticsearch, if
|
|
|
|
required.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
password = lib.mkOption {
|
2022-03-31 15:58:26 +02:00
|
|
|
type = with lib.types; nullOr (either path (attrsOf path));
|
2021-06-02 14:32:20 +02:00
|
|
|
default = null;
|
2022-08-03 22:46:41 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-03-31 15:58:26 +02:00
|
|
|
The password to use when connecting to Elasticsearch,
|
|
|
|
if required.
|
|
|
|
|
|
|
|
Always handled as a secret whether the value is
|
2022-08-03 22:46:41 +02:00
|
|
|
wrapped in a `{ _secret = ...; }`
|
|
|
|
attrset or not (refer to [](#opt-services.parsedmarc.settings) for
|
2022-03-31 15:58:26 +02:00
|
|
|
details).
|
2021-06-02 14:32:20 +02:00
|
|
|
'';
|
2022-03-31 15:58:26 +02:00
|
|
|
apply = x: if isAttrs x || x == null then x else { _secret = x; };
|
2021-06-02 14:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ssl = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = false;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
Whether to use an encrypted SSL/TLS connection.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
cert_path = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
|
|
|
default = "/etc/ssl/certs/ca-certificates.crt";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2021-06-02 14:32:20 +02:00
|
|
|
The path to a TLS certificate bundle used to verify
|
|
|
|
the server's certificate.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
|
2022-07-31 13:45:25 +02:00
|
|
|
warnings = let
|
|
|
|
deprecationWarning = optname: "Starting in 8.0.0, the `${optname}` option has been moved from the `services.parsedmarc.settings.imap`"
|
|
|
|
+ "configuration section to the `services.parsedmarc.settings.mailbox` configuration section.";
|
|
|
|
hasImapOpt = lib.flip builtins.hasAttr cfg.settings.imap;
|
|
|
|
movedOptions = [ "reports_folder" "archive_folder" "watch" "delete" "test" "batch_size" ];
|
|
|
|
in builtins.map deprecationWarning (builtins.filter hasImapOpt movedOptions);
|
|
|
|
|
2021-06-02 14:32:20 +02:00
|
|
|
services.elasticsearch.enable = lib.mkDefault cfg.provision.elasticsearch;
|
|
|
|
|
|
|
|
services.geoipupdate = lib.mkIf cfg.provision.geoIp {
|
|
|
|
enable = true;
|
|
|
|
settings = {
|
|
|
|
EditionIDs = [
|
|
|
|
"GeoLite2-ASN"
|
|
|
|
"GeoLite2-City"
|
|
|
|
"GeoLite2-Country"
|
|
|
|
];
|
|
|
|
DatabaseDirectory = "/var/lib/GeoIP";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.dovecot2 = lib.mkIf cfg.provision.localMail.enable {
|
|
|
|
enable = true;
|
|
|
|
protocols = [ "imap" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
services.postfix = lib.mkIf cfg.provision.localMail.enable {
|
|
|
|
enable = true;
|
|
|
|
origin = cfg.provision.localMail.hostname;
|
|
|
|
config = {
|
|
|
|
myhostname = cfg.provision.localMail.hostname;
|
|
|
|
mydestination = cfg.provision.localMail.hostname;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.grafana = {
|
|
|
|
declarativePlugins = with pkgs.grafanaPlugins;
|
|
|
|
lib.mkIf cfg.provision.grafana.dashboard [
|
|
|
|
grafana-worldmap-panel
|
|
|
|
grafana-piechart-panel
|
|
|
|
];
|
|
|
|
|
|
|
|
provision = {
|
|
|
|
enable = cfg.provision.grafana.datasource || cfg.provision.grafana.dashboard;
|
2023-01-05 12:20:12 +01:00
|
|
|
datasources.settings.datasources =
|
2021-06-02 14:32:20 +02:00
|
|
|
let
|
2022-03-31 15:53:49 +02:00
|
|
|
esVersion = lib.getVersion config.services.elasticsearch.package;
|
2021-06-02 14:32:20 +02:00
|
|
|
in
|
|
|
|
lib.mkIf cfg.provision.grafana.datasource [
|
|
|
|
{
|
|
|
|
name = "dmarc-ag";
|
|
|
|
type = "elasticsearch";
|
|
|
|
access = "proxy";
|
2022-03-31 15:53:49 +02:00
|
|
|
url = "http://localhost:9200";
|
2021-06-02 14:32:20 +02:00
|
|
|
jsonData = {
|
|
|
|
timeField = "date_range";
|
|
|
|
inherit esVersion;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
{
|
|
|
|
name = "dmarc-fo";
|
|
|
|
type = "elasticsearch";
|
|
|
|
access = "proxy";
|
2022-03-31 15:53:49 +02:00
|
|
|
url = "http://localhost:9200";
|
2021-06-02 14:32:20 +02:00
|
|
|
jsonData = {
|
|
|
|
timeField = "date_range";
|
|
|
|
inherit esVersion;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
2023-01-05 12:20:12 +01:00
|
|
|
dashboards.settings.providers = lib.mkIf cfg.provision.grafana.dashboard [{
|
2021-06-02 14:32:20 +02:00
|
|
|
name = "parsedmarc";
|
|
|
|
options.path = "${pkgs.python3Packages.parsedmarc.dashboard}";
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
services.parsedmarc.settings = lib.mkMerge [
|
|
|
|
(lib.mkIf cfg.provision.elasticsearch {
|
|
|
|
elasticsearch = {
|
|
|
|
hosts = [ "localhost:9200" ];
|
|
|
|
ssl = false;
|
|
|
|
};
|
|
|
|
})
|
|
|
|
(lib.mkIf cfg.provision.localMail.enable {
|
|
|
|
imap = {
|
|
|
|
host = "localhost";
|
|
|
|
port = 143;
|
|
|
|
ssl = false;
|
|
|
|
user = cfg.provision.localMail.recipientName;
|
|
|
|
password = "${pkgs.writeText "imap-password" "@imap-password@"}";
|
2022-07-31 13:45:25 +02:00
|
|
|
};
|
|
|
|
mailbox = {
|
2021-06-02 14:32:20 +02:00
|
|
|
watch = true;
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
|
|
|
|
|
|
|
systemd.services.parsedmarc =
|
|
|
|
let
|
|
|
|
# Remove any empty attributes from the config, i.e. empty
|
|
|
|
# lists, empty attrsets and null. This makes it possible to
|
|
|
|
# list interesting options in `settings` without them always
|
|
|
|
# ending up in the resulting config.
|
2022-03-31 15:58:26 +02:00
|
|
|
filteredConfig = lib.converge (lib.filterAttrsRecursive (_: v: ! elem v [ null [] {} ])) cfg.settings;
|
|
|
|
|
|
|
|
# Extract secrets (attributes set to an attrset with a
|
|
|
|
# "_secret" key) from the settings and generate the commands
|
|
|
|
# to run to perform the secret replacements.
|
|
|
|
secretPaths = lib.catAttrs "_secret" (lib.collect isSecret filteredConfig);
|
2021-06-02 14:32:20 +02:00
|
|
|
parsedmarcConfig = ini.generate "parsedmarc.ini" filteredConfig;
|
2022-03-31 15:58:26 +02:00
|
|
|
mkSecretReplacement = file: ''
|
|
|
|
replace-secret ${lib.escapeShellArgs [ (hashString "sha256" file) file "/run/parsedmarc/parsedmarc.ini" ]}
|
|
|
|
'';
|
|
|
|
secretReplacements = lib.concatMapStrings mkSecretReplacement secretPaths;
|
2021-06-02 14:32:20 +02:00
|
|
|
in
|
|
|
|
{
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "postfix.service" "dovecot2.service" "elasticsearch.service" ];
|
|
|
|
path = with pkgs; [ replace-secret openssl shadow ];
|
|
|
|
serviceConfig = {
|
|
|
|
ExecStartPre = let
|
|
|
|
startPreFullPrivileges = ''
|
|
|
|
set -o errexit -o pipefail -o nounset -o errtrace
|
|
|
|
shopt -s inherit_errexit
|
|
|
|
|
|
|
|
umask u=rwx,g=,o=
|
|
|
|
cp ${parsedmarcConfig} /run/parsedmarc/parsedmarc.ini
|
|
|
|
chown parsedmarc:parsedmarc /run/parsedmarc/parsedmarc.ini
|
2022-03-31 15:58:26 +02:00
|
|
|
${secretReplacements}
|
2021-06-02 14:32:20 +02:00
|
|
|
'' + lib.optionalString cfg.provision.localMail.enable ''
|
|
|
|
openssl rand -hex 64 >/run/parsedmarc/dmarc_user_passwd
|
|
|
|
replace-secret '@imap-password@' '/run/parsedmarc/dmarc_user_passwd' /run/parsedmarc/parsedmarc.ini
|
|
|
|
echo "Setting new randomized password for user '${cfg.provision.localMail.recipientName}'."
|
|
|
|
cat <(echo -n "${cfg.provision.localMail.recipientName}:") /run/parsedmarc/dmarc_user_passwd | chpasswd
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
"+${pkgs.writeShellScript "parsedmarc-start-pre-full-privileges" startPreFullPrivileges}";
|
|
|
|
Type = "simple";
|
|
|
|
User = "parsedmarc";
|
|
|
|
Group = "parsedmarc";
|
|
|
|
DynamicUser = true;
|
|
|
|
RuntimeDirectory = "parsedmarc";
|
2022-10-28 17:23:44 +02:00
|
|
|
RuntimeDirectoryMode = "0700";
|
2021-06-02 14:32:20 +02:00
|
|
|
CapabilityBoundingSet = "";
|
|
|
|
PrivateDevices = true;
|
|
|
|
PrivateMounts = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
ProcSubset = "pid";
|
|
|
|
SystemCallFilter = [ "@system-service" "~@privileged" "~@resources" ];
|
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" "AF_INET6" ];
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
LockPersonality = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
ExecStart = "${pkgs.python3Packages.parsedmarc}/bin/parsedmarc -c /run/parsedmarc/parsedmarc.ini";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users.${cfg.provision.localMail.recipientName} = lib.mkIf cfg.provision.localMail.enable {
|
|
|
|
isNormalUser = true;
|
|
|
|
description = "DMARC mail recipient";
|
|
|
|
};
|
|
|
|
};
|
2021-06-03 18:31:15 +02:00
|
|
|
|
2023-01-25 00:33:40 +01:00
|
|
|
meta.doc = ./parsedmarc.md;
|
2021-06-03 18:31:15 +02:00
|
|
|
meta.maintainers = [ lib.maintainers.talyz ];
|
2021-06-02 14:32:20 +02:00
|
|
|
}
|