aboutsummaryrefslogtreecommitdiffstats
path: root/modules/vhack/ma/mail/module.nix
blob: 034924ae7d9c06112f9748a2714350f1733d8ac1 (plain) (blame)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
{
  config,
  lib,
  ...
}: let
  cfg = config.vhack.mail;
in {
  options.vhack.mail = {
    enable = lib.mkEnableOption "sophisticated mail setup with simple-nixos-mailserver";
    fqdn = lib.mkOption {
      type = lib.types.str;
      description = "The fqdn mailserver should be served on.";
    };

    domains = lib.mkOption {
      type = lib.types.listOf lib.types.str;
      example = ["example.com"];
      default = [];
      description = "The domains that this mail server serves.";
    };

    accounts = lib.mkOption {
      type = lib.types.attrsOf (
        lib.types.submodule (
          {name, ...}: {
            options = {
              name = lib.mkOption {
                type = lib.types.str;
                default = name;
                example = "user1@example.com";
                readOnly = true;
                internal = true;
                description = ''
                  The login username for this account.
                '';
              };

              hashedPassword = lib.mkOption {
                type = lib.types.nullOr lib.types.str;
                default = null;
                example = "$y$j9T$vfGrwkAaXCjCEWtVNMQck1$383uIXQmn2z0hnmVAA8kwFQmjNj78.nYbvWeyNLIaP1";
                description = ''
                  The hashed login password for this account.

                  Use `mkpasswd` to create password hashes:
                  ```
                  nix-shell -p mkpasswd --run 'mkpasswd -s'
                  ```

                  :::{note}
                  This is a convenience option, when your threat model allows
                  storing hashed secrets in the world-readable Nix store.

                  Passing the hash through
                  {option}`mailserver.accounts.<name>.hashedPasswordFile`
                  allows relying on filesystem discretionary access control as
                  another security boundary.
                  :::
                '';
              };

              hashedPasswordFile = lib.mkOption {
                type = lib.types.nullOr lib.types.path;
                default = null;
                example = "/run/keys/user1-pw-hash";
                description = ''
                  The hashed login password for this account read from a file.

                  Use `mkpasswd to create password hashes:
                  ```
                  nix-shell -p mkpasswd --run 'mkpasswd -s'
                  ```
                '';
              };

              passwordFile = lib.mkOption {
                type = with lib.types;
                  nullOr (pathWith {
                    inStore = false;
                  });
                default = null;
                example = "/run/keys/user1-pw";
                description = ''
                  The plaintext login password for this account read from a file.

                  :::{note}
                  The password is hashed before it is passed on to Dovecot.
                  :::
                '';
              };

              aliases = lib.mkOption {
                type = with lib.types; listOf types.str;
                example = [
                  "abuse@example.com"
                  "postmaster@example.com"
                ];
                default = [];
                description = ''
                  List of additional mail addresses (aliases) that get routed to this account.

                  :::{admonition} Catch-all with sending permissions
                  :class: tip
                  Configure `@example.com` to create a catch-all for this domain
                  that also allows sending from all addresses.
                  :::
                '';
              };

              aliasesRegexp = lib.mkOption {
                type = with lib.types; listOf types.str;
                example = [''/^tom\..*@domain\.com$/''];
                default = [];
                description = ''
                  Same as {option}`mailserver.accounts.<name>.aliases` but
                  using PCRE (Perl compatible regex).
                '';
              };

              catchAll = lib.mkOption {
                type = with lib.types; listOf (enum cfg.domains);
                example = [
                  "example.com"
                  "example2.com"
                ];
                default = [];
                description = ''
                  For which domains should this account act as a catch all?

                  :::{warning}
                  Does not allow sending from all addresses of these domains.
                  Use {option}`mailserver.accounts.<name>.aliases` if that
                  is required.
                  :::
                '';
              };

              quota = lib.mkOption {
                type = with lib.types; nullOr types.str;
                default = null;
                example = "2G";
                description = ''
                  The quota limit for this user.

                  The value is must use a size format like `500M`, `2G`, `10G`.

                  If unset, will fall back to {option}`mailserver.quota.defaults.perUser` if set.
                '';
              };

              sieveScript = lib.mkOption {
                type = with lib.types; nullOr lines;
                default = null;
                example = ''
                  require ["fileinto", "mailbox"];

                  if address :is "from" "gitlab@mg.gitlab.com" {
                    fileinto :create "GitLab";
                    stop;
                  }

                  # This must be the last rule, it will check if list-id is set, and
                  # file the message into the Lists folder for further investigation
                  elsif header :matches "list-id" "<?*>" {
                    fileinto :create "Lists";
                    stop;
                  }
                '';
                description = ''
                  Per-user sieve script.
                '';
              };

              sendOnly = lib.mkOption {
                type = lib.types.bool;
                default = false;
                description = ''
                  Specifies if the account should be a send-only account.

                  Emails sent to send-only accounts will
                  be rejected with the reason configured in
                  {option}`mailserver.accounts.<name>.sendOnlyRejectMessage`.
                '';
              };

              sendOnlyRejectMessage = lib.mkOption {
                type = lib.types.str;
                default = "This account cannot receive emails.";
                description = ''
                  The message returned to the sender for a send-only account.

                  See {option}`mailserver.accounts.<name>.sendOnly`.
                '';
              };
            };
          }
        )
      );
      example = lib.literalExpression ''
        {
          user1 = {
            # This password hash leaks into the Nix store
            hashedPassword = "$y$j9T$y6eZ1o.IvVNfdGMAsUEvh1$6K/llP52uw2iDh4iSwtAn54/JYy7FzCcoCHmjmx00H5";
          };
          user2 = {
            # Hashed password passed as a file
            hashedPasswordFile = "/run/keys/user2-pw-hash";
          };
          user3 = {
            # Plaintext password file
            passwordFile = "/run/keys/user3-pw";
          };
        }
      '';
      description = ''
        Attribute set of mail accounts.

        Each entry defines a mailbox and login credentials, where the attribute
        name is used as the login username and optionally routed mail address.

        Use `mkpasswd` to generate password hashes.
      '';
      default = {};
    };

    security = lib.mkOption {
      type = lib.types.submodule {
        options = {
          dkimKeys = lib.mkOption {
            type = lib.types.nullOr (lib.types.attrsOf (lib.types.submodule {
              options = {
                dkimPublicKey = lib.mkOption {
                  type = lib.types.str;
                  description = ''
                    The base 64 encoded representation of the public dkim key.
                  '';
                };
                dkimPrivateKeyPath = lib.mkOption {
                  type = lib.types.path;
                  description = ''
                    Path to the file for the pre-generated dkim key file.
                    Should not point to the nixos store.
                  '';
                };
                keyAlgorithm = lib.mkOption {
                  type = lib.types.enum ["ed25519-sha256" "rsa-sha-256" "rsa-sha-1"];
                  description = "The algorithm of the used key";
                };
              };
            }));
            description = ''
              Which key to use for which domain. The attr keys are the domains
            '';
            default = null;
          };
        };
      };
      description = ''
        Security options.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    vhack.persist.directories = [
      {
        directory = "/var/lib/mail/backup";
        user = "virtualMail";
        group = "virtualMail";
        mode = "0700";
      }
      {
        directory = "/var/lib/mail/vmail";
        user = "virtualMail";
        group = "virtualMail";
        mode = "0700";
      }
      {
        directory = "/var/lib/mail/dkim";
        user = "opendkim";
        group = "opendkim";
        mode = "0700";
      }
      {
        directory = "/var/lib/postfix/data";
        user = "postfix";
        group = "postfix";
        mode = "0700";
      }
      {
        directory = "/var/lib/postfix/queue";
        user = "postfix";
        group = "postfix";
        mode = "0700";
      }
      {
        directory = "/var/lib/rspamd";
        user = "rspamd";
        group = "rspamd";
        mode = "0700";
      }
    ];

    vhack.nginx.enable = true;

    services.nginx.virtualHosts = {
      "${cfg.fqdn}" = {
        enableACME = true;
        forceSSL = true;
      };
    };

    mailserver = {
      enable = true;
      inherit (cfg) fqdn;

      stateVersion = 5;

      storage = {
        directoryLayout = "fs";
        path = "/var/lib/mail/vmail";
      };

      dkim = let
        dkimKeys =
          builtins.mapAttrs (name: value: {
            selectors."mail".keyFile =
              value.dkimPrivateKeyPath;
          })
          cfg.security.dkimKeys;
      in {
        # domains."${cfg.fqdn}".selectors."mail" = lib.mkIf (cfg.dkimKey != null) {
        #   keyFile = cfg.dkimKey;
        # };
        domains = lib.mkIf (cfg.security.dkimKeys != null) dkimKeys;
        keyDirectory = lib.mkIf (cfg.security.dkimKeys == null) "/var/lib/mail/dkim";
      };

      backup.snapshotRoot = "/var/lib/mail/backup";

      enableImap = false;
      enableImapSsl = true;
      enablePop3 = false;
      enablePop3Ssl = true;
      # SMTP
      enableSubmission = false;
      enableSubmissionSsl = true;
      openFirewall = true;

      # Reference the existing ACME configuration created by nginx
      x509.useACMEHost = cfg.fqdn;

      inherit (cfg) domains accounts;
    };

    users = {
      users = {
        knot-resolver = {
          uid = config.vhack.constants.ids.uids.knot-resolver;
          group = "knot-resolver";
        };
        redis-rspamd.uid = config.vhack.constants.ids.uids.redis-rspamd;
        rspamd.uid = config.vhack.constants.ids.uids.rspamd;
        postfix-tlspol.uid = config.vhack.constants.ids.uids.postfix-tlspol;
      };
      groups = {
        knot-resolver.gid = lib.mkForce config.vhack.constants.ids.gids.knot-resolver;
        redis-rspamd.gid = config.vhack.constants.ids.gids.redis-rspamd;
        rspamd.gid = config.vhack.constants.ids.gids.rspamd;
        postfix-tlspol.gid = config.vhack.constants.ids.gids.postfix-tlspol;
      };
    };
  };
}