summary refs log tree commit diff
path: root/nixos/modules/services/mail/rmilter.nix
blob: 492c645832190b5f91fda4092d7ed8c3f96a637e (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
{ config, lib, pkgs, ... }:

with lib;

let

  rspamdCfg = config.services.rspamd;
  postfixCfg = config.services.postfix;
  cfg = config.services.rmilter;

  inetSocket = addr: port: "inet:[${toString port}@${addr}]";
  unixSocket = sock: "unix:${sock}";

  systemdSocket = if cfg.bindSocket.type == "unix" then cfg.bindSocket.path
    else "${cfg.bindSocket.address}:${toString cfg.bindSocket.port}";
  rmilterSocket = if cfg.bindSocket.type == "unix" then unixSocket cfg.bindSocket.path
    else inetSocket cfg.bindSocket.address cfg.bindSocket.port;

  rmilterConf = ''
    pidfile = /run/rmilter/rmilter.pid;
    bind_socket = ${if cfg.socketActivation then "fd:3" else rmilterSocket};
    tempdir = /tmp;
  '' + (with cfg.rspamd; if enable then ''
    spamd {
      servers = ${concatStringsSep ", " servers};
      connect_timeout = 1s;
      results_timeout = 20s;
      error_time = 10;
      dead_time = 300;
      maxerrors = 10;
      reject_message = "${rejectMessage}";
      ${optionalString (length whitelist != 0)  "whitelist = ${concatStringsSep ", " whitelist};"}

      # rspamd_metric - metric for using with rspamd
      # Default: "default"
      rspamd_metric = "default";
      ${extraConfig}
    };
  '' else "") + cfg.extraConfig;

  rmilterConfigFile = pkgs.writeText "rmilter.conf" rmilterConf;

in

{

  ###### interface

  options = {

    services.rmilter = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to run the rmilter daemon.";
      };

      debug = mkOption {
        type = types.bool;
        default = false;
        description = "Whether to run the rmilter daemon in debug mode.";
      };

      user = mkOption {
        type = types.string;
        default = "rmilter";
        description = ''
          User to use when no root privileges are required.
        '';
       };

      group = mkOption {
        type = types.string;
        default = "rmilter";
        description = ''
          Group to use when no root privileges are required.
        '';
       };

      bindSocket.type = mkOption {
        type = types.enum [ "unix" "inet" ];
        default = "unix";
        description = ''
          What kind of socket rmilter should listen on. Either "unix"
          for an Unix domain socket or "inet" for a TCP socket.
        '';
      };

      bindSocket.path = mkOption {
       type = types.str;
       default = "/run/rmilter.sock";
       description = ''
          Path to Unix domain socket to listen on.
        '';
      };

      bindSocket.address = mkOption {
        type = types.str;
        default = "::1";
        example = "0.0.0.0";
        description = ''
          Inet address to listen on.
        '';
      };

      bindSocket.port = mkOption {
        type = types.int;
        default = 11990;
        description = ''
          Inet port to listen on.
        '';
      };

      socketActivation = mkOption {
        type = types.bool;
        default = true;
        description = ''
          Enable systemd socket activation for rmilter.

          Disabling socket activation is not recommended when a Unix
          domain socket is used and could lead to incorrect
          permissions.
        '';
      };

      rspamd = {
        enable = mkOption {
          type = types.bool;
          default = rspamdCfg.enable;
          description = "Whether to use rspamd to filter mails";
        };

        servers = mkOption {
          type = types.listOf types.str;
          default = ["r:/run/rspamd/rspamd.sock"];
          description = ''
            Spamd socket definitions.
            Is server name is prefixed with r: it is rspamd server.
          '';
        };

        whitelist = mkOption {
          type = types.listOf types.str;
          default = [ ];
          description = "list of ips or nets that should be not checked with spamd";
        };

        rejectMessage = mkOption {
          type = types.str;
          default = "Spam message rejected; If this is not spam contact abuse";
          description = "reject message for spam";
        };

        extraConfig = mkOption {
          type = types.lines;
          default = "";
          description = "Custom snippet to append to end of `spamd' section";
        };
      };

      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = "Custom snippet to append to rmilter config";
      };

      postfix = {
        enable = mkOption {
          type = types.bool;
          default = false;
          description = "Add rmilter to postfix main.conf";
        };

        configFragment = mkOption {
          type = types.str;
          description = "Addon to postfix configuration";
          default = ''
            smtpd_milters = ${rmilterSocket}
            milter_protocol = 6
            milter_mail_macros = i {mail_addr} {client_addr} {client_name} {auth_authen}
          '';
        };
      };

    };

  };


  ###### implementation

  config = mkMerge [

    (mkIf cfg.enable {
      warnings = [
        ''`config.services.rmilter' is deprecated, `rmilter' deprecated and unsupported by upstream, and will be removed from next releases. Use built-in rspamd milter instead.''
      ];

      users.users = singleton {
        name = cfg.user;
        description = "rmilter daemon";
        uid = config.ids.uids.rmilter;
        group = cfg.group;
      };

      users.groups = singleton {
        name = cfg.group;
        gid = config.ids.gids.rmilter;
      };

      systemd.services.rmilter = {
        description = "Rmilter Service";

        wantedBy = [ "multi-user.target" ];
        after = [ "network.target" ];

        serviceConfig = {
          ExecStart = "${pkgs.rmilter}/bin/rmilter ${optionalString cfg.debug "-d"} -n -c ${rmilterConfigFile}";
          ExecReload = "${pkgs.coreutils}/bin/kill -USR1 $MAINPID";
          User = cfg.user;
          Group = cfg.group;
          PermissionsStartOnly = true;
          Restart = "always";
          RuntimeDirectory = "rmilter";
          RuntimeDirectoryMode = "0750";
        };

      };

      systemd.sockets.rmilter = mkIf cfg.socketActivation {
        description = "Rmilter service socket";
        wantedBy = [ "sockets.target" ];
        socketConfig = {
          ListenStream = systemdSocket;
          SocketUser = cfg.user;
          SocketGroup = cfg.group;
          SocketMode = "0660";
        };
      };
    })

    (mkIf (cfg.enable && cfg.rspamd.enable && rspamdCfg.enable) {
      users.users.${cfg.user}.extraGroups = [ rspamdCfg.group ];
    })

    (mkIf (cfg.enable && cfg.postfix.enable) {
      services.postfix.extraConfig = cfg.postfix.configFragment;
      users.users.${postfixCfg.user}.extraGroups = [ cfg.group ];
    })
  ];
}