summary refs log tree commit diff stats
path: root/modules/by-name/fa/fail2ban/module.nix
blob: c619ef9031cdac42c280f1c81d269df272948e4d (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
{
  config,
  lib,
  ...
}: let
  cfg = config.vhack.fail2ban;
in {
  options.vhack.fail2ban = {
    enable = lib.mkEnableOption "fail2ban";
  };

  config = lib.mkIf cfg.enable {
    vhack.persist.directories = [
      {
        directory = "/var/lib/fail2ban";
        # TODO: Fail2ban should probably run under a dedicated `fail2ban` user. <2024-12-25>
        user = "root";
        group = "root";
        mode = "0700";
      }
    ];

    services.fail2ban = {
      enable = true;
      maxretry = 7; # ban after 7 failures
      daemonSettings = {
        Definition = {
          logtarget = "SYSLOG";
          socket = "/run/fail2ban/fail2ban.sock";
          pidfile = "/run/fail2ban/fail2ban.pid";
          dbfile = "/var/lib/fail2ban/db.sqlite3";
        };
      };
      bantime-increment = {
        enable = true;
        rndtime = "8m";
        overalljails = true;
        multipliers = "2 4 16 128 256";
        maxtime = "72h";
      };
      jails = {
        dovecot = ''
          # block IPs which failed to log-in
          # aggressive mode add blocking for aborted connections
          enabled = true
          filter = dovecot[mode=aggressive]
          maxretry = 2
        '';
        postfix = ''
          enabled = true
          filter = postfix[mode=aggressive]
          findtime = 600
          maxretry = 3
        '';
      };
    };
  };
}