summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/mail/postfix-tlspol.nix
blob: f49a179918423753ce50d9c081d12f1ad84c59d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
{
  config,
  lib,
  pkgs,
  ...
}:

let
  inherit (lib)
    hasPrefix
    mkEnableOption
    mkIf
    mkMerge
    mkOption
    mkPackageOption
    types
    ;

  cfg = config.services.postfix-tlspol;

  format = pkgs.formats.yaml_1_2 { };
  configFile = format.generate "postfix-tlspol.yaml" cfg.settings;
in

{
  meta.maintainers = pkgs.postfix-tlspol.meta.maintainers;

  options.services.postfix-tlspol = {
    enable = mkEnableOption "postfix-tlspol";

    package = mkPackageOption pkgs "postfix-tlspol" { };

    settings = mkOption {
      type = types.submodule {
        freeformType = format.type;
        options = {
          server = {
            address = mkOption {
              type = types.str;
              default = "unix:/run/postfix-tlspol/tlspol.sock";
              example = "127.0.0.1:8642";
              description = ''
                Path or address/port where postfix-tlspol binds its socket to.
              '';
            };

            socket-permissions = mkOption {
              type = types.str;
              default = "0660";
              readOnly = true;
              description = ''
                Permissions to the UNIX socket, if configured.

                ::: {.note}
                Due to hardening on the systemd unit the socket can never be created world readable/writable.
                :::
              '';
              apply = value: (fromTOML "v=0o${value}").v;
            };

            log-level = mkOption {
              type = types.enum [
                "debug"
                "info"
                "warn"
                "error"
              ];
              default = "info";
              example = "warn";
              description = ''
                Log level
              '';
            };

            prefetch = mkOption {
              type = types.bool;
              default = true;
              example = false;
              description = ''
                Whether to prefetch DNS records when the TTL of a cached record is about to expire.
              '';
            };

            cache-file = mkOption {
              type = types.path;
              default = "/var/cache/postfix-tlspol/cache.db";
              readOnly = true;
              description = ''
                Path to the cache file.
              '';
            };
          };

          dns = {
            address = mkOption {
              type = with types; nullOr str;
              default = null;
              example = "127.0.0.1:53";
              description = ''
                IP and port to your DNS resolver.

                Uses resolvers from /etc/resolv.conf if unset.

                ::: {.note}
                The configured DNS resolver must validate DNSSEC signatures.
                :::
              '';
            };
          };
        };
      };

      default = { };
      description = ''
        The postfix-tlspol configuration file as a Nix attribute set.

        See the reference documentation for possible options.
        <https://github.com/Zuplu/postfix-tlspol/blob/main/configs/config.default.yaml>
      '';
    };

    configurePostfix = mkOption {
      type = types.bool;
      default = true;
      description = ''
        Whether to configure the required settings to use postfix-tlspol in the local Postfix instance.
      '';
    };
  };

  config = mkMerge [
    (mkIf (cfg.enable && config.services.postfix.enable && cfg.configurePostfix) {
      # https://github.com/Zuplu/postfix-tlspol#postfix-configuration
      services.postfix.settings.main = {
        smtp_dns_support_level = "dnssec";
        smtp_tls_security_level = "dane";
        smtp_tls_policy_maps =
          let
            address =
              if (hasPrefix "unix:" cfg.settings.server.address) then
                cfg.settings.server.address
              else
                "inet:${cfg.settings.server.address}";
          in
          [ "socketmap:${address}:QUERYwithTLSRPT" ];
      };

      systemd.services.postfix = {
        wants = [ "postfix-tlspol.service" ];
        after = [ "postfix-tlspol.service" ];
      };

      users.users.postfix.extraGroups = [ "postfix-tlspol" ];
    })

    (mkIf cfg.enable {
      environment.etc."postfix-tlspol/config.yaml".source = configFile;

      environment.systemPackages = [ cfg.package ];

      users.users.postfix-tlspol = {
        isSystemUser = true;
        group = "postfix-tlspol";
      };
      users.groups.postfix-tlspol = { };

      systemd.sockets.postfix-tlspol = {
        wantedBy = [ "sockets.target" ];
        socketConfig = {
          Accept = false;
          ListenStream = [
            (lib.removePrefix "unix:" cfg.settings.server.address)
          ];
          SocketUser = "postfix-tlspol";
          SocketGroup = "postfix-tlspol";
          SocketMode = cfg.settings.server.socket-permissions;
          DirectoryMode = "0755";
        };
      };

      systemd.services.postfix-tlspol = {
        after = [
          "nss-lookup.target"
          "network-online.target"
        ];
        wants = [
          "nss-lookup.target"
          "network-online.target"
        ];

        description = "Postfix DANE/MTA-STS TLS policy socketmap service";
        documentation = [ "https://github.com/Zuplu/postfix-tlspol" ];

        restartTriggers = [ configFile ];

        # https://github.com/Zuplu/postfix-tlspol/blob/main/init/postfix-tlspol.service
        serviceConfig = {
          ExecStart = toString [
            (lib.getExe cfg.package)
            "-config"
            "/etc/postfix-tlspol/config.yaml"
          ];
          ExecReload = "${lib.getExe' pkgs.util-linux "kill"} -HUP $MAINPID";
          Restart = "always";
          RestartSec = 5;

          User = "postfix-tlspol";
          Group = "postfix-tlspol";

          CacheDirectory = "postfix-tlspol";
          CapabilityBoundingSet = [ "" ];
          LockPersonality = true;
          MemoryDenyWriteExecute = true;
          NoNewPrivileges = true;
          PrivateDevices = true;
          PrivateTmp = true;
          PrivateUsers = true;
          ProcSubset = "pid";
          ProtectClock = true;
          ProtectControlGroups = true;
          ProtectHome = true;
          ProtectHostname = true;
          ProtectKernelLogs = true;
          ProtectKernelModules = true;
          ProtectKernelTunables = true;
          ProtectProc = "invisible";
          ProtectSystem = "strict";
          ReadOnlyPaths = [ "/etc/postfix-tlspol/config.yaml" ];
          RemoveIPC = true;
          RestrictAddressFamilies = [
            "AF_INET"
            "AF_INET6"
          ];
          RestrictNamespaces = true;
          RestrictRealtime = true;
          RestrictSUIDSGID = true;
          SystemCallArchitectures = "native";
          SystemCallFilter = [
            "@system-service"
            "~@privileged @resources"
          ];
          SystemCallErrorNumber = "EPERM";
          SecureBits = [
            "noroot"
            "noroot-locked"
          ];
          RuntimeDirectory = "postfix-tlspol";
          RuntimeDirectoryMode = "1750";
          WorkingDirectory = "/var/cache/postfix-tlspol";
          UMask = "0077";
        };
      };
    })
  ];
}